from django.shortcuts import render, redirect
from django.contrib import messages
from django.views.generic import ListView
from django.views.generic.edit import CreateView, UpdateView
from .models import Ticket, Flight
from .forms import TicketForm, FlightFormSet

class TicketInline:
    form_class = TicketForm
    model = Ticket
    template_name = 'ticket/ticket_create_update.html'

    def form_valid(self, form):
        named_formsets = self.get_named_formsets()
        if not all((x.is_valid() for x in named_formsets.values())):
            return self.render_to_response(self.get_context_data(form=form))

        self.object = form.save()

        for name, formset in named_formsets.items():
            formset_value_func = getattr(self, 'formset_{0}_valid'.format(name), None)
            if formset_value_func is not None:
                formset_value_func(formset)
            else:
                formset.save()

            return redirect('ticket:list_tickets')

    def flight_formset_valid(self, formset):
        flights = self.save_formset(formset, contact)  # formset.save(commit=False)

        for obj in formset.deleted_objects:
            obj.delete()
        for flight in flights:
            flight.ticket = self.object
            flight.save()

class TicketCreate(TicketInline, CreateView):
    def get_context_data(self, **kwargs):
        ctx = super(TicketCreate, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx

    def get_named_formsets(self):
        if self.request.method == "GET":
            return {
                'flights': FlightFormSet(prefix='flights'),
            }
        else:
            return {
                'flights': FlightFormSet(self.request.POST or None, self.request.FILES or None, prefix='flights')
            }

class TicketUpdate(TicketInline, UpdateView):
    def get_context_data(self, **kwargs):
        ctx = super(TicketUpdate, self).get_context_data(**kwargs)
        ctx['named_formsets'] = self.get_named_formsets()
        return ctx

    def get_named_formsets(self):
        return {
            'flights': FlightFormSet(self.request.POST or None, self.request.FILES or None, instance=self.object,
                                     prefix='flights'),
        }

class TicketList(ListView):
    model = Ticket
    template_name = 'ticket/ticket_list.html'
    context_object_name = 'tickets'

def delete_ticket(request, pk):
    try:
        flight = Flight.objects.get(id=pk)
    except Flight.DoesNotExist:
        messages.success(
            request, 'Object Does not exit'
        )
        return redirect('tickets:update_product', pk=flight.product.id)

    flight.delete()
    messages.success(
        request, 'Flight deleted successfully'
    )
    return redirect('tickets:update_product', pk=flight.product.id)


from Bio import Phylo
from io import StringIO
import sys
import os
import json
import jplace_parse
from Bio import SeqIO
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
import subprocess
import random
import gzip
import partition_sequences
import shutil
import glob
#from memory_profiler import profile

def rooting(nwkfilepath,newnwkpath,root):
    with open(nwkfilepath) as handle:
        tree1=Phylo.read(handle,"newick")
        tree1.root_with_outgroup(root)
        tree2=Phylo.BaseTree.Tree(tree1.clade)
    Phylo.write(tree2,newnwkpath,"newick")

def rooting_and_remove(nwkfilepath,newnwkpath,root):
    with open(nwkfilepath) as handle:
        tree1=Phylo.read(handle,"newick")
        tree1.root_with_outgroup(root)
        tree2=Phylo.BaseTree.Tree(tree1.clade[0])
    Phylo.write(tree2,newnwkpath,"newick")

def make_unrooted_after_rooting(treefile,new_treefile,root):
    ref_tree=Phylo.read(treefile,'newick')
    ref_tree.root_with_outgroup(root)
    if len(ref_tree.clade.clades) == 2:
        if len(ref_tree.clade.clades[0].clades) == 2 :
            newtree=Phylo.BaseTree.Tree(ref_tree.clade.clades[0])
            newtree.clade.clades.append(ref_tree.clade.clades[1])
        elif len(ref_tree.clade.clades[1].clades) == 2 :
            newtree=Phylo.BaseTree.Tree(ref_tree.clade.clades[1])
            newtree.clade.clades.append(ref_tree.clade.clades[0])
    Phylo.write(newtree,new_treefile,"newick")

def partition(treefile, edge_to_sequence_file, jpartitionfname, depth):
    partition={}; leaf_to_Nseq={}
    paraphyletic=[]
    stack=[]
    leaves={}

    # get tree information
    with open(treefile,'r') as handle:
        treestr = handle.read()
    # get relation of 'sXXX' <--> {YYY}
    relation=jplace_parse.correspond(treestr)
    corr=relation[0]
    root=relation[1]
    ref_tree=Phylo.read(treefile,'newick')
    ref_tree.root_with_outgroup(root)

    # edge2seqlist
    edge2seqlist = {}
    with open(edge_to_sequence_file,'r') as handle:
        for line in handle:
            edge = line.split("\t")[0]
            seq  = line.split("\t")[1].split("\n")[0]
            if (edge in edge2seqlist.keys()):
                edge2seqlist[edge].append(seq)
            else:
                edge2seqlist[edge] = [seq]
    for node in ref_tree.get_terminals() + ref_tree.get_nonterminals():
        if node.name not in edge2seqlist.keys():
            edge2seqlist[node.name] = []

    # get paraphyletic group
    for seqname in edge2seqlist[root]:
        partition[seqname]="paraphyletic"
        paraphyletic.append(seqname)
    # get monophyletic groups
    stack.extend(ref_tree.clade.clades[0].clades) #ref_tree.clade.clades[1] : outgroup
    while(stack!=[]):
        cstate=stack.pop()
        d=len(ref_tree.get_path(cstate)) # current depth
        cedge =int(cstate.name.strip('{').strip('}'))
        if(edge2seqlist[cstate.name]==[] and d<depth):
            if(len(cstate.clades)!=0):
                stack.extend(cstate.clades)
            else:
                leaves[cstate.name]=[cstate.name]
        else: # if some seqnames were placed to the edge
            leaves[cstate.name]=list(ter.name for ter in cstate.get_terminals())
            downstream=[]
            downstream.extend(cstate.get_terminals())
            downstream.extend(cstate.get_nonterminals())
            downstream_edge=list(state.name for state in downstream)
            for edge in downstream_edge:
                for seqname in edge2seqlist[edge]:
                    if seqname != "root":
                        partition[seqname]=cedge
                        if(cedge in leaf_to_Nseq.keys()): leaf_to_Nseq[cedge]+=1
                        else: leaf_to_Nseq[cedge]=1
            cstate.clades=[]
    tree=Phylo.BaseTree.Tree(ref_tree.clade.clades[0])
    Phylo.write(tree, 'tmp.nwk', 'newick')
    with open('tmp.nwk','r') as tmp:
        treestr=tmp.read()
    outputdict = {}
    outputdict["tree"]=treestr
    outputdict["paraphyletic"]=paraphyletic
    outputdict["partition"]=partition
    outputdict["leaves"]=leaves
    outputdict["corr"]=corr
    with open(jpartitionfname,'w') as out:
        out.write(json.dumps(outputdict))
    return len(paraphyletic), max(list(leaf_to_Nseq.values()))

def add_paraphyletic_edit(jpartfname, outputfname, splitted_file_dir, subsample_size, num_of_para):
    # open .jpart file
    with open(jpartfname,"r") as jf:
        jp = jf.read()
    # parse json format
    js = json.loads(jp)

    all_file_pathlist = []
    for all_file in os.listdir(splitted_file_dir):
        if   (all_file.split(".")[-1]=='edit'):
            all_file_pathlist.append(splitted_file_dir + "/" + all_file)
        elif (all_file.split(".")[-1]=='gz'):
            if (all_file.split(".")[-2]=='edit'):
                all_file_pathlist.append(splitted_file_dir + "/" + all_file)

    is_gzipped = (all_file_pathlist[0].split(".")[-1] == "gz")
    if (is_gzipped):
        out     = gzip.open(outputfname, 'at')
    else:
        out     = open(outputfname, 'a')
    Nwritten = 0

    for all_file in all_file_pathlist:
        is_gzipped = (all_file.split(".")[-1] == "gz")
        if (is_gzipped):
            ist   = gzip.open(all_file, 'rt')
        else:
            ist   = open(all_file, 'r')
        for line in ist:
            name = line.split()[0]
            if (name != "root"):
                if (len(line.split())>1):
                    editlist_str = line.split()[1]
                else:
                    editlist_str = ""
                if(js["partition"][name]=="paraphyletic"):
                    out.write(line)
                    Nwritten += 1
                    if (Nwritten == num_of_para): break
        ist.close()
        if (Nwritten == num_of_para): break
    out.close()

def partition_fasta(
    in_fasta_dirlist,
    num_file,
    OUT_DIR,
    wd,
    jpart,
    treefile,
    ROOTING,
    file_format="fa",
    nodenum=1,
    codedir=None
    ):

    # open .jpart file
    with open(jpart,"r") as jf:
        jp = jf.read()
    # parse json format
    js = json.loads(jp)
    # get tree information
    treestr = js['tree']
    tree = Phylo.read(StringIO(treestr),"newick")
    num_mono = len(tree.get_terminals())

    # get number (decides dir name) and make dir
    num=0
    while(True):
        with open(num_file,'r') as nf:
            try:
                num=int(nf.read())
            except Exception as e:
                print(e)
        with open(num_file,'w') as nf:
            try:
                nf.write(str(num+num_mono))
            except Exception as e:
                print(e)
        try:
            for i in range(num_mono):
                os.mkdir(OUT_DIR+"/d"+str(num+i))
                os.mkdir(OUT_DIR+"/d"+str(num+i)+"/INPUT")
                os.mkdir(OUT_DIR+"/d"+str(num+i)+"/INPUT/edit")
                os.mkdir(OUT_DIR+"/d"+str(num+i)+"/INPUT/aligned")
                os.mkdir(OUT_DIR+"/d"+str(num+i)+"/INPUT/unaligned")
                os.mkdir(OUT_DIR+"/d"+str(num+i)+"/INPUT/count")
                os.mkdir(OUT_DIR+"/d"+str(num+i)+"/INPUT/root")
            break
        except Exception as e:
            print(e)
    # create dictionary of leaf -> OUT_DIR/dir path
    DIRdict = {}
    NUMdict = {}
    i = 0
    for leaf in tree.get_terminals():
        DIRdict[leaf.name] = [OUT_DIR+"/d"+str(num+i),0]
        NUMdict[leaf.name.strip('{').strip('}')] = i
        i=i+1

    dirpath_set = set()
    with open(wd + "/seqname_dirpath.txt", 'w') as handle:
        for seqname in list(js["partition"].keys()):
            if js["partition"][seqname] == "paraphyletic":
                handle.write(seqname +'\t' + wd + '\tparaphyletic\n')
            else:
                dirpath = DIRdict['{'+str(js["partition"][seqname])+'}'][0]
                dirpath_set.add(dirpath)
                handle.write(seqname +'\t' + dirpath + '\n')
    subprocess.call(
        "cat " + wd + "/seqname_dirpath.txt | cut -f2 | sort | uniq -c > " + wd + "/Nseq_dirpath.txt",
        shell = True
    )

    for fasta_count, splitted_fasta_dir in enumerate(in_fasta_dirlist):
        print("Partitioning sequences...")

        # edit, unaligned, or aligned
        data_type = splitted_fasta_dir.split("/")[-2]

        print("Target:"+str(fasta_count), splitted_fasta_dir, "is", data_type)

        is_gzipped = (os.listdir(splitted_fasta_dir)[0].split(".")[-1] == "gz")
        splitted_fasta_list = os.listdir(splitted_fasta_dir)
        splitted_fpath_list = [splitted_fasta_dir + "/" + splitted_fasta for splitted_fasta in splitted_fasta_list]
        Nfiles_total = len(splitted_fasta_list)
        dirpath_list = list(sorted([wd] + list(dirpath_set)))

        if (is_gzipped):
            gzip_extention = ".gz"
            gzip_command   = "|gzip"
        else:
            gzip_extention = ""
            gzip_command   = ""

        if (file_format=="fa"):

            if (nodenum > 1):

                # assign splitted files to each node: same as distributed placement
                #Nfiles_per_node = len(splitted_fasta_list) // nodenum # Only the last node may treat more number of files
                node2filelist = []
                for i in range(nodenum):
                    node2filelist.append([])
                for j, file_name in enumerate(splitted_fasta_list):
                    node2filelist[j%nodenum].append(file_name)

                dname = wd.split("/")[-1]
                PATH = (subprocess.\
                            Popen(
                                'echo $PATH',
                                stdout=subprocess.PIPE,
                                shell=True
                            ).communicate()[0]
                        ).decode('utf-8')
                PATH = (PATH.split('\n'))[0]
                LD_LIBRARY_PATH = (
                    subprocess.\
                        Popen(
                            'echo $LD_LIBRARY_PATH',
                            stdout=subprocess.PIPE,
                            shell=True
                        ).communicate()[0]
                    ).decode('utf-8')
                LD_LIBRARY_PATH = (LD_LIBRARY_PATH.split('\n'))[0]
                for i in range(nodenum):
                    with open(wd+"/../../prep_dir/qsub_"+dname+"."+str(i)+".partition.sh", 'w') as handle:
                        handle.write("#!/bin/bash\n")
                        handle.write("#$ -S /bin/bash\n")
                        handle.write("PATH={}\n".format(PATH))
                        handle.write("LD_LIBRARY_PATH={}\n".format(LD_LIBRARY_PATH))
                        inputFASTA_filepathlist = [splitted_fasta_dir + "/" + splitted_file for splitted_file in node2filelist[i] ]
                        handle.write(
                            "python3 "                               +
                            codedir + "/python/partition_sequences.py "  +
                            ":".join(inputFASTA_filepathlist) + " "  +
                            ":".join([ dirpath+ "/INPUT/" + data_type for dirpath in dirpath_list ])+" "+
                            wd + "/seqname_dirpath.txt"              +
                            "\n"
                            )
                        handle.write(
                            "echo finished > " + splitted_fasta_dir+"/"+dname+"."+str(i)+".partition.sh.finished\n"
                            )
                    shutil.move(wd+"/../../prep_dir/qsub_"+dname+"."+str(i)+".partition.sh", wd+"/../../qsub_dir/qsub_"+dname+"."+str(i)+".partition.sh")
                # wait for all partition jobs finish
                Nunclassified    = len(os.listdir(splitted_fasta_dir))
                Nfinished        = 0
                while(Nfinished < nodenum):
                    file_list= os.listdir(splitted_fasta_dir)
                    Nunclassified    = 0
                    Nfinished        = 0
                    for filename in file_list:
                        if (filename.split(".")[-1] == 'finished'):
                            Nfinished += 1
                        elif (filename.split(".")[-1] == 'fa' or filename.split(".")[-1]=='gz'):
                            Nunclassified += 1
                    #if (Nunclassified > 0 and Nfinished == nodenum):
                    #    subprocess.call(
                    #        "mv "+wd+"/../../executed/qsub_"+dname+".*.partition.sh " + wd+"/../../qsub_dir",
                    #        shell=True
                    #    )

            else: # sequential mode
                partition_sequences.partition_sequences(splitted_fpath_list, [ dirpath+ "/INPUT/" + data_type for dirpath in dirpath_list ], wd + "/seqname_dirpath.txt")

            if (fasta_count == 0):
                problematic_filenames      = wd + "/INPUT/"+data_type+"/*.part*fa"+gzip_extention
                problematic_concatfilename = wd+"/INPUT.fa.problematic"+gzip_extention
                if (len(glob.glob(problematic_filenames))>0):
                    subprocess.call(
                        "cat "                                   +
                        problematic_filenames                    +
                        "|seqkit grep -r -p ^root -v"            +
                        ">> " + problematic_concatfilename       +
                        " 2> /dev/null;"                         +
                        "rm " + problematic_filenames            +
                        " 2> /dev/null",
                        shell = True
                        )

                for dirpath in dirpath_list:
                    if (dirpath != wd):
                        subprocess.call(
                            "cat "+wd+"/INPUT/root/root.fa         > "+dirpath+"/INPUT/root/root.fa;       " +
                            "if [ -e "+wd+"/INPUT/root/root.aligned.fa ]; then cat "+wd+"/INPUT/root/root.aligned.fa > "+dirpath+"/INPUT/root/root.aligned.fa; fi",
                            shell=True
                        )

        elif(file_format=="edit"):
            partition_sequences.partition_sequences(splitted_fpath_list, [ dirpath+ "/INPUT/" + data_type for dirpath in dirpath_list ], wd + "/seqname_dirpath.txt", file_format = 'edit')
            problematic_filenames      = wd + "/INPUT/"+data_type+"/*.*edit" + gzip_extention
            problematic_concatfilename = wd + "/INPUT.edit.problematic" + gzip_extention
            if (len(glob.glob(problematic_filenames))>0):
                subprocess.call(
                    "cat "                                   +
                    problematic_filenames                    +
                    "|seqkit grep -r -p ^root -v"            +
                    ">> " + problematic_concatfilename       +
                    " 2> /dev/null;"                         +
                    "rm " + problematic_filenames            +
                    " 2> /dev/null"
                    ,shell = True
                    )

            for dirpath in dirpath_list:
                if (dirpath != wd):
                    with open(dirpath+"/root.edit", 'w') as ost:
                        ost.write("root\t\n")

    for leaf in tree.get_terminals():
        newname=DIRdict[leaf.name][0]
        leaf.name=newname
    Phylo.write(tree, treefile, 'newick')
    return DIRdict

def qsub_prep(ARGVS, WD, DIRdict, INITIAL_SEQ_COUNT, seq_count_when_aligned,dirpath2Nseq_filepath,mem_req_threshold):

    dirpath2Nseq = {}
    with open(dirpath2Nseq_filepath,'r') as handle:
        for line in handle:
            dirpath = line.split()[1]
            Nseq    = int(line.split()[0])
            dirpath2Nseq[dirpath] = Nseq

    for key in DIRdict.keys():
        dirpath = DIRdict[key][0]
        ls      = DIRdict[key][0].split("/")
        num=ls[len(ls)-1]

        PATH = (
            subprocess.Popen(
                'echo $PATH',
                stdout=subprocess.PIPE,
                shell=True)
                .communicate()[0]
                ).decode('utf-8')

        PATH = PATH.split('\n')[0]

        LD_LIBRARY_PATH = (
            subprocess.Popen(
                'echo $LD_LIBRARY_PATH',
                stdout=subprocess.PIPE,
                shell=True
                ).communicate()[0]
                ).decode('utf-8')

        LD_LIBRARY_PATH = (LD_LIBRARY_PATH.split('\n'))[0]

        if (dirpath in dirpath2Nseq.keys()):
            if (dirpath2Nseq[dirpath] > mem_req_threshold):
                job_script_filepath = WD+"/../../prep_dir"+"/qsub_"+num+".cycle.largemem.sh"
            else:
                job_script_filepath = WD+"/../../prep_dir"+"/qsub_"+num+".cycle.sh"

            with open(job_script_filepath, 'w') as qf:
                qf.write("#!/bin/bash\n")
                qf.write("#$ -S /bin/bash\n")
                qf.write("PATH={}\n".format(PATH))
                qf.write("LD_LIBRARY_PATH={}\n".format(LD_LIBRARY_PATH))

                # change arguments for the next FRACTAL iteration
                ARGVS[1]  = DIRdict[key][0]
                ARGVS[20] = INITIAL_SEQ_COUNT
                if (ARGVS[14] == "unaligned"):
                    ARGVS[30] = seq_count_when_aligned

                command="python3 "
                for arg in ARGVS:
                    if str(arg)=="":
                        arg="\"\""
                    command += "\"" + str(arg) + "\" "
                qf.write(command)
            shutil.move(job_script_filepath, WD+"/../../qsub_dir")

def tiny_tree(INPUTfile,OUTPUTnwk, file_format="fasta"):
    is_gzipped = (INPUTfile.split(".")[-1] == "gz")
    if is_gzipped:
        handle  = gzip.open(INPUTfile, 'rt')
    else:
        handle  = open(INPUTfile, 'r')

    names=[]
    if (file_format == "fasta"):
        records = SeqIO.parse(handle, "fasta")
        for record in records:
            if(record.id!="root"):
                names.append(record.id)
    elif(file_format == "edit"):
        for line in handle:
            name = line.split()[0]
            if (name != "root"): names.append(name)

    if(len(names)==1):
        init_clade = Phylo.BaseTree.Clade(name=names[0])
        tree = Phylo.BaseTree.Tree(init_clade)
    elif(len(names)==2):
        init_clade = Phylo.BaseTree.Clade()
        tree = Phylo.BaseTree.Tree(init_clade)
        tree.clade.clades.extend(list(Phylo.BaseTree.Clade(name=name) for name in names))
    else:
        print("tiny_tree() Error : len(names)=")
        print(len(names))
    Phylo.write(tree, OUTPUTnwk, 'newick')

    handle.close()


## Finding time period T of a pendulum using Simpson's method of integration

# Imports
from library import integrate_simpson
from math import sin, sqrt, pi

# Define limits and other constants
lims = [0, pi/2]
a = sin(pi/8)
g = 9.8
L = 1

# Define function for time period
def time_period(phi):
    return 4 * sqrt(L/g) * 1/sqrt(1 - a**2 * sin(phi)**2)

# Solve for T using Simpson's method
T = integrate_simpson(time_period, lims, N=10)

# Print the time period
print("The time period of the pendulum is {} seconds.".format(T))

##############################################################
##################### OUTPUT #################################
##############################################################
# The time period of the pendulum is 2.0873200174795916 seconds.


from django import forms
from .models import Producto

class ProductoForm(forms.ModelForm):
    class Meta:
        model = Producto
        fields = [
            'nombre',
            'precio',
            'talla',
            'peso',
            'color',
        ]

        labels = {
            'nombre': 'Nombre',
            'precio': 'Precio',
            'talla': 'Talla',
            'peso': 'Peso',
            'color': 'Color',
        }


# List of topics to be watched. Messages received will be written to stdout.
topic_prefixes_to_watch = ['']

# Seconds between hearbeat publishes
heartbeat_period = 10

# Volttron address and keys used to create agents
agent_kwargs = {
    # Volttron VIP address
    'address': 'tcp://127.0.0.1:22916',

    # Required keys for establishing an encrypted VIP connection
    'secretkey': '',
    'publickey': '',
    'serverkey': '',

    # Don't use the configuration store
    'enable_store': False,
}


import streamlit as st
import asyncio
import calendar
import datetime
import pandas as pd
import numpy as np
import plotly.graph_objects as go
from matplotlib import pyplot as plt

from utils.widgets import update_session_state_var
from utils.graph import (
    get_graph_data,
    get_arc_graph
)
from utils.display import display_section_title
from src.redis_db import execute_user_query
from config_files import config
from statsmodels.tsa.holtwinters import Holt

def get_user_search_query():
    """
    Get user search query.
    """
    st.text_input(
        label="Enter topic of interest:",
        value=st.session_state.user_search_query,
        max_chars=100,
        key='user_search_query_text_input',
        on_change=update_session_state_var,
        kwargs={
            'session_state_var_name': 'user_search_query',
            'widget_key': 'user_search_query_text_input'
        }
    )

def get_search_filters():
    """
    Get search filters to be applied to the query.
    """
    col_1, col_2, col_3, col_4 = st.columns(4)
    with col_1:
        st.number_input(
            label="Select the number of similar papers",
            min_value=1,
            max_value=1000,
            value=st.session_state.k_similar,
            step=5,
            key='k_similar_number_input',
            on_change=update_session_state_var,
            kwargs={
                'session_state_var_name': 'k_similar',
                'widget_key': 'k_similar_number_input'
            }
        )

    with col_2:
        st.number_input(
            label="Select year lower bound",
            min_value=1990,
            max_value=st.session_state.year_max - 1,
            value=st.session_state.year_min,
            step=1,
            key='year_min_number_input',
            on_change=update_session_state_var,
            kwargs={
                'session_state_var_name': 'year_min',
                'widget_key': 'year_min_number_input'
            }
        )

    with col_3:
        st.number_input(
            label="Select year upper bound",
            min_value=st.session_state.year_min + 1,
            max_value=datetime.datetime.now().year,
            value=st.session_state.year_max,
            step=1,
            key='year_max_number_input',
            on_change=update_session_state_var,
            kwargs={
                'session_state_var_name': 'year_max',
                'widget_key': 'year_max_number_input'
            }
        )

    with col_4:
        st.multiselect(
            label='Select categories',
            options=list(config.arxiv_categories_mapping.keys()),
            default=st.session_state.categories,
            key='categories_multiselect',
            on_change=update_session_state_var,
            kwargs={
                'session_state_var_name': 'categories',
                'widget_key': 'categories_multiselect'
            }
        )

def update_sub_params():
    """
    Update 'sub' session state variables. Those variables are used to simulate a form behavior for the
    submit button, while allowing a dynamic behavior of the widgets inside the form.
    """
    st.session_state.user_search_query_sub = st.session_state.user_search_query
    st.session_state.categories_sub = st.session_state.categories
    st.session_state.year_min_sub = st.session_state.year_min
    st.session_state.year_max_sub = st.session_state.year_max
    st.session_state.k_similar_sub = st.session_state.k_similar

def set_submit_button():
    """
    Create a submit button widget to validate the query parameters.
    """
    st.button(
        label='Submit',
        type='primary',
        on_click=update_sub_params
    )

@st.experimental_memo
def get_search_query_results(user_search_query, k_similar, year_min, year_max, categories):
    """
    Get the search query results based on the user's query and search filters.

    Parameters
    ----------
    user_search_query : str
        User search query.
    k_similar : int
        Number of most similar papers to be retrieved.
    year_min : int
        Lower bound of the publication date for the papers to be retrieved
    year_max : int
        Upper bound of the publication date for the papers to be retrieved

    Returns
    ----------
    search_query_results : List(dict)
        List of search query results
    """
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    search_query_results = execute_user_query(
        user_text=user_search_query,
        k=k_similar,
        year_min=year_min,
        year_max=year_max,
        categories=[config.arxiv_categories_mapping[category] + "*" for category in categories]
    )
    # Add index based on position in result
    for i, paper in enumerate(search_query_results):
        paper['list_index'] = i

    return search_query_results

@st.experimental_memo
def display_topic_trend(query_results):
    """
    Display topic trend + optional prediction for the next two years if more than 5 non zero data points are available.

    Parameters
    ----------
    query_results : list(dict)
        List of most similar papers
    """
    dates = [int(el['year']) for el in query_results]
    weights = [1 for _ in query_results]
    all_dates = [year for year in range(min(dates), max(dates) + 1, 1)]
    zeros = [0] * len(all_dates)
    time_series_df = pd.DataFrame(
        {
            'date': dates + all_dates,
            'count_papers': weights + zeros
        }
    )
    time_series_df = time_series_df.groupby('date').sum().reset_index()

    fig = go.Figure(
        layout=go.Layout(
            xaxis=dict(
                title="Year"
            ),
            yaxis=dict(
                title="Papers count"
            ),
            margin=dict(t=30, b=30, l=1, r=1)
        )
    )
    fig.add_trace(
        go.Scatter(
            x=time_series_df['date'].values,
            y=time_series_df['count_papers'].values,
            name='Topic evolution',
            line=dict(color='#0097a7', width=4)
        )
    )

    # Predict topic trend for next 2 years if we have at least 5 year of history
    if len(all_dates) >= 5:
        ts_index = pd.date_range(start=str(min(dates)), end=str(max(dates) + 1), freq="A")
        ts_series = pd.Series(time_series_df['count_papers'].values, ts_index)
        pred = (
            Holt(ts_series, damped_trend=True, initialization_method="estimated")
            .fit(smoothing_level=0.8, smoothing_trend=0.2)
            .forecast(2)
        )
        fig.add_trace(
            go.Scatter(
                x=[year for year in range(max(dates), max(dates) + 3, 1)],
                y=np.concatenate([time_series_df['count_papers'].values[-1:], pred.values], axis=0),
                name='Predicted evolution',
                line=dict(color='#e71869 ', width=4, dash='dash')
            )
        )

    st.plotly_chart(
        fig,
        use_container_width=True
    )

def display_arc_graph(query_results):
    """
    Display arc graph with citations relationships.

    Parameters
    ----------
    query_results : list(dict)
        List of most similar papers
    """
    col, _ = st.columns(2)
    col.info(
        "Please note that:  \n"
        "- Papers are ordered in chronological order.  \n"
        "- The size of a bubble is related to the number of citations of the paper.  \n"
        "- Links between bubbles represent citations."
    )
    graph_fig = get_arc_graph(get_graph_data(query_results))
    st.plotly_chart(
        figure_or_data=graph_fig,
        use_container_width=True
    )

@st.experimental_memo
def get_reading_list(query_results, K_reading_list):
    """
    Display arc graph with citations relationships.

    Parameters
    ----------
    query_results : list(dict)
        List of most similar papers
    """
    query_results = query_results.copy()
    for paper in query_results:
        paper['reading_score'] = len(paper['citations'].split(',')) * paper['similarity_score']
    # Select top K relevant
    reading_result = sorted(query_results, key=lambda d: d['reading_score'], reverse=True)[:K_reading_list]
    # Order by date
    reading_result = sorted(reading_result, key=lambda d: d['year'] + '-' + d['month'])
    return reading_result

def display_reading_list(query_results):
    """
    Display reading list with title, publication date, similarity score and number of citations.

    Parameters
    ----------
    query_results : list(dict)
        List of most similar papers
    """
    st.info(
        "The suggested reading list is based on the papers with the highest number of citations and similarity score. "
        "It is ordered by ascending publication date."
    )
    col_1, _, _ = st.columns(3)
    K_reading_list = col_1.number_input(
        label="Select the number of papers you're willing to read about this topic",
        min_value=1,
        max_value=st.session_state.k_similar,
        value=min(st.session_state.k_similar, 5),
        step=1
    )
    reading_result = get_reading_list(query_results, K_reading_list)

    ref_col, date_col, sim_col, citations_col = st.columns(4)
    with ref_col:
        display_section_title('Title (link)', 'tab_header')
    with date_col:
        display_section_title('Publication date', 'tab_header')
    with sim_col:
        display_section_title('Similarity score', 'tab_header')
    with citations_col:
        display_section_title('Citations', 'tab_header')

    max_citations = max([len(paper['citations'].split(',')) for paper in reading_result])
    for paper in reading_result:
        ref_col, date_col, sim_col, citations_col = st.columns(4)
        with ref_col:
            st.markdown(
                f'''{paper['title']}''',
                unsafe_allow_html=True
            )
        with date_col:
            date = f"{calendar.month_name[int(paper['month'])]} {paper['year']}"
            st.markdown(
                f"
{date}
",
                unsafe_allow_html=True
            )
        with sim_col:
            plt.rcParams["figure.autolayout"] = True
            plt.rcParams["figure.figsize"] = [7.50, 0.9]
            fig, ax = plt.subplots()
            ax.barh([''], [paper['similarity_score']], color="#49b6c2")
            ax.barh([''], [1 - paper['similarity_score']], left=[paper['similarity_score']], color="#939492")
            ax.spines['top'].set_visible(False)
            ax.spines['right'].set_visible(False)
            ax.spines['bottom'].set_visible(False)
            ax.spines['left'].set_visible(False)
            ax.get_xaxis().set_ticks([0, paper['similarity_score'], 1])
            ax.get_yaxis().set_ticks([])
            st.pyplot(fig)
        with citations_col:
            n_citations = len(paper['citations'].split(','))
            plt.rcParams["figure.autolayout"] = True
            plt.rcParams["figure.figsize"] = [7.50, 0.9]
            fig, ax = plt.subplots()
            ax.barh([''], [n_citations], color="#49b6c2")
            ax.barh([''], [max_citations - n_citations], left=[n_citations], color="#939492")
            ax.spines['top'].set_visible(False)
            ax.spines['right'].set_visible(False)
            ax.spines['bottom'].set_visible(False)
            ax.spines['left'].set_visible(False)
            ax.get_xaxis().set_ticks([0, n_citations, max_citations])
            ax.get_yaxis().set_ticks([])
            st.pyplot(fig)

def display_search_query_results(user_search_query_results):
    """
    Display a list of all results with detailed information about each paper. The list is ordered by descending similarity.

    Parameters
    ----------
    query_results : list(dict)
        List of most similar papers
    """
    st.markdown(
        """

        """,
        unsafe_allow_html=True,
    )
    for i in range(len(user_search_query_results)):
        res = user_search_query_results[i]
        st.markdown(
            f"""
            \n
            """,
            unsafe_allow_html=True
        )
        expander = st.expander(
            label=(
                f"{i + 1} - {res['title']}"
            )
        )
        with expander:
            st.markdown(
                (
                    f"**Author(s):** {res['authors']}  \n"
                    f"**Publication date:** {calendar.month_name[int(res['month'])]} {res['year']}  \n"
                    f"**Categories:** {res['categories']}  \n"
                    f"**Similarity score:** {round(res['similarity_score'], 2)}/1  \n"
                    f"**Number of citations:** {len(res['citations'].split(','))}  \n"
                    f"**Abstract:** {res['abstract']}  \n"
                    f"[Go to paper](https://arxiv.org/abs/{res['paper_id']})"
                )
            )


#Reference: E. Burman, M. Nechita, L. Oksanen, Unique continuation for the Helmholtz equation using stabilized finite element methods, J. Math. Pures Appl., 2018
#FEniCS implementation for the finited element method described in the above reference
from dolfin import *
import numpy as np

def boundary(x, on_boundary):
    return on_boundary

def stfem(ue, f, k, ind_omega, nele):
    k2 = Constant(k*k)
    mesh = UnitSquareMesh(nele, nele, 'right/left')
    hmax = mesh.hmax()
    h2 = hmax*hmax
    n = FacetNormal(mesh)

    V = FiniteElement("CG", mesh.ufl_cell(), 1)
    W  = FiniteElement("CG", mesh.ufl_cell(), 1)
    VW = FunctionSpace(mesh, V * W)

    (u, z) = TrialFunctions(VW) #discrete solution
    (v, w) = TestFunctions(VW)

    pgamma = Constant(1e-5) #fixed stabilization parameter
    a = ((dot(grad(u),grad(w)) - k2 * dot(u,w))) * dx \
      - dot(grad(z),grad(w)) * dx \
      + ind_omega * dot(u,v) * dx \
      + pgamma * hmax * dot(jump(grad(u),n), jump(grad(v),n)) * dS + pgamma * h2 * k2 * k2 * dot(u,v) * dx \
      + (dot(grad(v),grad(z)) - k2 * dot(v,z)) * dx
    L = f*w*dx + ind_omega*ue*v*dx

    w0 = Constant(0)
    boundary_cond = DirichletBC(VW.sub(1), w0, boundary) #impose zero Dirichlet boundary conditions on the space W

    sol = Function(VW)
    solve(a == L, sol, boundary_cond, solver_parameters={"linear_solver": "mumps"})
    (u, z) = sol.split()

    return [u, z]


#!/usr/bin/env python

def get_abc(bias, key):
    a = ord('a')
    abc = ''.join([chr(i) for i in range(a, a+26)])
    for sym in key:
        abc = abc.replace(sym, '')
    abc = key + abc
    for i in range(bias):
        abc = abc[-1] + abc[:-1]
    return abc

inp = "input text"
bias = 3
key = "key"

abc = get_abc(bias, key)
message1 = ''
for sym in inp:
    if sym.isalpha():
        message1 += abc[(ord(sym) - 97)]
    else:
        message1 += sym
print(message1)
print(abc)


import sys
import numpy as np
import pandas as pd
import glob
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from os.path import join
def get_common_markers(files):
	common_markers = []
	for file in files:
		if file == files[0]:
			common_markers = pd.read_csv(file).columns.str.lower()
		else:
			common_markers = np.intersect1d(common_markers, pd.read_csv(file).columns.str.lower())
	common_markers = common_markers[common_markers != 'ID']
	common_markers = np.insert(common_markers, 0, 'ID', axis=0)
	return common_markers

def get_intensity_matrix(files):
	common_markers = ['ID', 'CD11c', 'CD21', 'CD4', 'CD8', 'Ki67']
	print(common_markers)
	intensity_image_pieces = []
	cell_num = []
	for file in files:
		print(file)
		intensity = pd.read_csv(file)[common_markers].values
		intensity_image_pieces.append(intensity)
		cell_num.append(intensity.shape[0])
	intensity_image_stacked = np.vstack(intensity_image_pieces)
	cell_id = intensity_image_stacked[:, 0].astype(int)
	return intensity_image_stacked[:, 1:], cell_id, cell_num

if __name__ == '__main__':
	data_dir = sys.argv[1]
	for intensity_type in ['total']:
		all_feature_matrix_z_5_tissues_pieces = []
		cell_file_list_5_tissues = []
		cell_id_5_tissues = []
		cell_num_5_tissues = []
		tissue_type = ['LN', 'THYMUS', 'SPLEEN', 'LI', 'SI']
		for tissue in tissue_type:
			cytoplasm_intensity_file_list = sorted(glob.glob(join(data_dir, '**', tissue, '**', '*cell_channel_' + intensity_type + '.csv'), recursive=True))
			print(tissue)
			print(len(cytoplasm_intensity_file_list))
			# print(cytoplasm_intensity_file_list)
			cytoplasm_feature_matrix, cell_id, cell_num = get_intensity_matrix(cytoplasm_intensity_file_list)
			cell_file_list_5_tissues = cell_file_list_5_tissues + cytoplasm_intensity_file_list
			cell_id_5_tissues = cell_id_5_tissues + cell_id.tolist()
			cell_num_5_tissues = cell_num_5_tissues + cell_num
			nuclear_intensity_file_list = sorted(glob.glob(join(data_dir, '**', tissue, '**', '*nuclei_channel_' + intensity_type + '.csv'), recursive=True))
			nuclear_feature_matrix, _, _ = get_intensity_matrix(nuclear_intensity_file_list)

			nuclear_membrane_intensity_file_list = sorted(glob.glob(join(data_dir, '**',  tissue, '**', '*nucleus_boundaries_channel_' + intensity_type + '.csv'), recursive=True))
			nuclear_membrane_feature_matrix, _, _ = get_intensity_matrix(nuclear_membrane_intensity_file_list)

			cell_membrane_intensity_file_list = sorted(glob.glob(join(data_dir, '**', tissue, '**', '*cell_boundaries_channel_' + intensity_type + '.csv'), recursive=True))
			cell_membrane_feature_matrix, _, _ = get_intensity_matrix(cell_membrane_intensity_file_list)

			all_feature_matrix = cell_membrane_feature_matrix + cytoplasm_feature_matrix + nuclear_membrane_feature_matrix + nuclear_feature_matrix

			ss = StandardScaler()
			all_feature_matrix_z = ss.fit_transform(all_feature_matrix)
			all_feature_matrix_z_5_tissues_pieces.append(all_feature_matrix_z)
		all_feature_matrix_z = np.vstack(all_feature_matrix_z_5_tissues_pieces)
		cc = 5
		for c in range(cc, cc+46):
			print(c)
			model = KMeans(n_clusters=c, random_state=100).fit(all_feature_matrix_z)
			labels = model.labels_.astype(int)
			idx = 0
			for i in range(len(cell_file_list_5_tissues)):
				save_file_name = cell_file_list_5_tissues[i].split('-')[0] + '_individual_tissue_cluster_' + str(c) + '_label_' + intensity_type + '_5_tissues_individual_z_score.csv'
				cell_id_image = cell_id_5_tissues[idx:(idx+cell_num_5_tissues[i])]
				cell_label_image = labels[idx:(idx+cell_num_5_tissues[i])]
				cell_type_total_num = []
				for a in range(0, c):
					cell_type_total_num.append(sum(cell_label_image == a))
				save_matrix = pd.DataFrame(data=np.vstack((cell_id_image, cell_label_image)).T, columns=['ID', 'Cluster'])
				# save_matrix.to_csv(save_file_name, index=False)
				idx += cell_num_5_tissues[i]
				print(save_file_name)
				print(cell_type_total_num)


import csv
import os
import re
import logging
import struct
import pyscca

import base.job
from plugins.common.RVT_filesystem import FileSystem
from base.utils import check_folder, check_directory, save_csv
from base.commands import run_command
from plugins.common.RVT_files import GetFiles

def parse_RFC_file(fname):
    """ Parses RecentFileCache.bcf

    Args:
        fname (str): file path
    """
    magics = [b'\xfe\xff\xee\xff', b'\x11\x22\x00\x00', b'\x03\x00\x00\x00', b'\x01\x00\x00\x00']
    entries = []
    filesize = os.path.getsize(fname)
    if filesize <= 20:
        return ''

    with open(fname, "rb") as fh:
        fh.seek(0)
        for i in range(0, len(magics)):
            header = fh.read(4)
            if not header == magics[i]:
                return ''
        fh.read(4)  # Disregard this value

        while fh.tell() < filesize:
            tmp_buffer = fh.read(4)
            entry_len = (struct.unpack('<i', tmp_buffer)[0]) * 2  # For unicode
            entry = fh.read(entry_len)
            entries.append(entry.decode("utf-16"))
            fh.read(2)  # Disregard last two unicode null terminators as they break in decode

    return entries

def parse_prefetch_file(pf_file):
    """ Parse individual file. Output is placed in 'output' dictionary

    Args:
        pf_file (str): list of filenames
    Returns:
        dict: dict with prefetch file information
    """
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)

    logger.info("Parsing {}".format(pf_file))
    item = {}

    try:
        scca = pyscca.open(pf_file)
        res_loaded = []
        for x in range(scca.get_number_of_file_metrics_entries()):
            res_loaded.append(scca.get_filename(x))
        item["resources loaded"] = res_loaded

        last_run_times = []
        n_runs = 1
        if scca.get_format_version() > 23:
            n_runs = 8

        for x in range(n_runs):
            if scca.get_last_run_time_as_integer(x) > 0:
                last_run_times.append(scca.get_last_run_time(x).strftime("%Y-%m-%d %H:%M:%S"))  # str conversion utilized to change from datetime into human-readable
            else:
                last_run_times.append('')

        item["last run times"] = last_run_times
        item["filename"] = scca.executable_filename
        item["prefetch hash"] = format(scca.prefetch_hash, 'x').upper()
        item["run count"] = str(scca.run_count)

        volumes = []
        for i in range(scca.number_of_volumes):
            volume = [str(scca.get_volume_information(i).device_path), scca.get_volume_information(i).creation_time.strftime(
                "%Y-%m-%d %H:%M:%S"), format(scca.get_volume_information(i).serial_number, 'x').upper()]
            volumes.append(volume)
        item["Volumes"] = volumes

        return item
    except IOError as e:
        logger.error("I/O Error: {}".format(e))
        return -1
    except SystemError as e:
        logger.error("Bad signature for pf file: {}".format(e))
        return -1
    except Exception:
        logger.error("Unexpected error")
        return -1

class Prefetch(base.job.BaseModule):
    """ Parse prefetch """

    def run(self, path=""):
        self.search = GetFiles(self.config, vss=self.myflag("vss"))
        self.vss = self.myflag('vss')
        self.filesystem = FileSystem(self.config)
        self.parse_Prefetch()
        return []

    def parse_Prefetch(self):
        self.logger().info("Parsing prefetch files")

        base_path = self.myconfig('{}outdir'.format('v' if self.vss else ''))
        check_directory(base_path, create=True)
        prefetch_dir = self.search.search(r"Windows/Prefetch$")

        if not prefetch_dir:
            self.logger().info('Prefetch file not found')
            return []

        for pdir in prefetch_dir:
            partition = pdir.split("/")[-3]
            csv_file = open(os.path.join(base_path, "prefetch_%s.csv" % partition), "w")
            writer = csv.writer(csv_file, delimiter=";", quotechar='"')
            pf_output1 = open(os.path.join(base_path, "prefetch_dump_%s.txt" % partition), "w")
            pdir_path = os.path.join(self.myconfig('casedir'), pdir)
            try:
                file_list = [os.path.join(pdir, file) for file in os.listdir(pdir_path) if file.endswith(".pf")]
            except IOError:
                self.logger().error('Unable to list files in directory {}'.format(pdir_path))
                continue
            except Exception as exc:
                self.logger().error(exc)
                continue

            if len(file_list) == 0:
                continue

            tl_files = self.filesystem.get_macb(file_list, self.vss)

            flag = True
            for file in os.listdir(pdir_path):
                prefetch_file = os.path.join(pdir_path, file)
                prefetch_rel_path = os.path.join(pdir, file)
                if file.endswith(".pf") and os.path.getsize(prefetch_file) > 0:  # Parse only non empty .pf files
                    item = parse_prefetch_file(prefetch_file)
                else:
                    continue

                if item == -1:
                    self.logger().error("Problems parsing {}".format(file))
                    pf_output1.write("Filename:\t\t{}\nBirth time:\t\t{}\nPrefetch Hash:\t\t\nExecutable Filename:\t\nRun count:\t\t\n".format(
                        file, tl_files[prefetch_rel_path][3]))
                    writer.writerow([file, "", "", tl_files[prefetch_rel_path][3], tl_files[prefetch_rel_path][0]] + [""] * 7)
                    continue

                if flag:
                    headers = ["Filename", "Executable", "Run count", "Birth time"] + ["Run time {}".format(str(i)) for i in range(len(item["last run times"]))]
                    writer.writerow(headers)
                    flag = False

                pf_output1.write("Filename:\t\t{}\nCreation time:\t\t{}\nPrefetch Hash:\t\t{}\nExecutable Filename:\t{}\nRun count:\t\t{}\n".format(
                    file, tl_files[prefetch_rel_path][3], item["prefetch hash"], item["filename"], item["run count"]))
                for i, fecha in enumerate(item["last run times"]):
                    pf_output1.write("\tRun time {}:\t\t{}\n".format(str(i), fecha))
                pf_output1.write("\nFilenames\nNumber of Filenames:\t{}\n".format(str(len(item["resources loaded"]))))
                for i in item["resources loaded"]:
                    pf_output1.write("\t{}\n".format(i))
                pf_output1.write("\nVolumes\nNumber of Volumes:\t{}\n".format(str(len(item["Volumes"]))))
                for v in item["Volumes"]:
                    pf_output1.write("\tDevice path:\t{}\n\tCreation time:\t{}\n\tSerial Number:\t{}\n\n".format(v[0], v[1], v[2]))
                pf_output1.write("################################################\n")

                writer.writerow([file, item["filename"], item["run count"], tl_files[prefetch_rel_path][3]] + [i for i in item["last run times"]])
            pf_output1.close()
            csv_file.close()

        self.logger().info("Parsing prefetch files finished")

class RFC(base.job.BaseModule):
    """ Parses RecentFileCache.bcf """

    def run(self, path=""):
        self.search = GetFiles(self.config, vss=self.myflag("vss"))
        self.logger().info("Parsing RecentFileCache.bcf")
        self.parse_RFC()
        return []

    def parse_RFC(self):
        base_path = self.myconfig('outdir')
        check_folder(base_path)

        rfc_list = list(self.search.search("RecentFileCache.bcf$"))
        if len(rfc_list) == 0:
            self.logger().info("No RecentFileCache.bcf files founded in disk")
            return

        for file in rfc_list:
            self.logger().info("Parsing {}".format(file))
            partition = file.split("/")[2]
            outfile = os.path.join(base_path, "rfc_{}.csv".format(partition))
            try:
                rfc = ({'Application': i} for i in parse_RFC_file(os.path.join(self.myconfig('casedir'), file)))
                save_csv(rfc, config=self.config, outfile=outfile, quoting=0, file_exists='OVERWRITE')
            except Exception:
                self.logger().warning("Problems parsing {}".format(file))

        self.logger().info("Parsing RecentFileCache.bcf finished")

class BAM(base.job.BaseModule):

    def run(self, path=""):
        self.search = GetFiles(self.config, vss=self.myflag("vss"))
        self.vss = self.myflag('vss')
        self.logger().info("Parsing BAM from registry")
        self.parse_BAM()
        return []

    def parse_BAM(self):
        base_path = self.myconfig('{}outdir'.format('v' if self.vss else ''))
        check_folder(base_path)
        SYSTEM = list(self.search.search(r"windows/System32/config/SYSTEM$"))

        partition_list = set()
        for f in SYSTEM:
            aux = re.search(r"([vp\d]*)/windows/System32/config", f, re.I)
            partition_list.add(aux.group(1))

        bam_file = {p: os.path.join(base_path, "bam_%s.txt" % p) for p in partition_list}
        ripcmd = self.config.get('plugins.common', 'rip', '/opt/regripper/rip.pl')

        for f in SYSTEM:
            with open(bam_file[f.split("/")[2]], 'w') as of:
                of.write("-----------------------------------------------------------------------\n{}\n-----------------------------------------------------------------------\n\n".format(f))
                of.write(run_command([ripcmd, "-r", os.path.join(self.myconfig('casedir'), f), "-p", "bam"], logger=self.logger()))
                of.write("\n\n")

        self.logger().info("Finished extraction of Background Activity Moderator (BAM)")


import igraph
import leidenalg
import numpy as np
import itertools
import pandas as pd
import nwtools.communities, nwtools.consensus, nwtools.common
import click
import os

def load_graph(edge_file, node_file, sepn=',', sepe=',', idcol='id', weightcol='Weight'):
    edges_df = pd.read_csv(edge_file, na_filter=False, sep=sepe)
    edges_df = edges_df.rename({weightcol: 'weight'}, axis=1)
    edges_df['distance'] = 1.0 / edges_df['weight']
    edges_df['Source'] = edges_df.Source.astype(str)
    edges_df['Target'] = edges_df.Target.astype(str)

    # Reorder source and target so that source < target
    source = edges_df[['Source', 'Target']].apply(min, axis=1).values
    target = edges_df[['Source', 'Target']].apply(max, axis=1).values
    edges_df['Source'] = source
    edges_df['Target'] = target

    nodes = pd.read_csv(node_file, index_col=idcol, sep=sepn)
    nodes.index = nodes.index.astype(str)

    graph = nwtools.common.igraph_from_pandas_edgelist(edges_df, attributes=['weight', 'distance'])

    return edges_df, nodes, graph

def get_node_consistency(graph, edges_df, thres_list=None):
    suffix = ''
    names = graph.vs.get_attribute_values('name')
    nodes_df = pd.DataFrame(index=names)
    nodes_df['degree'+suffix] = pd.Series(graph.degree(), index=names)
    nodes_df['weighted_degree'+suffix] = pd.Series(graph.strength(weights='weight'), index=names)
    nodes_df['eigenvector_centrality'+suffix] = pd.Series(
        graph.eigenvector_centrality(directed=False), index=names)
    nodes_df['betweenness'+suffix] = pd.Series(graph.betweenness(directed=False), index=names)
    edges_unstacked = edges_df.set_index(['Source', 'Target']).consistency.unstack()

    stabilities_unstacked = edges_unstacked.add(edges_unstacked.transpose(), fill_value=0)

    nodes_df['consistency_mean'] = stabilities_unstacked.mean(axis=0)
    nodes_df['consistency_min'] = stabilities_unstacked.min(axis=0)
    nodes_df['consistency_max'] = stabilities_unstacked.max(axis=0)
    nodes_df['consistency_std'] = stabilities_unstacked.apply(np.std, axis=0)
    nodes_df['consistency_mean_min_std'] = nodes_df['consistency_mean'] - nodes_df['consistency_std']
    if thres_list is None:
        thres_list = [0.8, 0.9, 1.0]
    for thres in thres_list:
        perc_consistent_neigbors =  (stabilities_unstacked>=thres).sum(axis=0) / (~stabilities_unstacked.isna()).sum(axis=0)
        nodes_df['consistency_neighbors_{:.1f}'.format(thres)] =perc_consistent_neigbors

    return nodes_df

@click.command()
@click.argument('edge_file', type=click.Path(exists=True))
@click.argument('node_file', type=click.Path(exists=True))
@click.option('--sepn', '-sn', default=',')
@click.option('--sepe', '-se', default=',')
@click.option('--idcol', '-i', default='id')
@click.option('--weightcol', '-w', default='Weight')
@click.option('--threshold', '-t', default=0.5)
@click.option('--nriter', '-n', default=100)
@click.option('--out_dir', '-o', default=os.getcwd(), type=click.Path())
def run_consistency(edge_file, node_file, out_dir, sepn, sepe, idcol, weightcol, threshold, nriter):
    print('Load data...')
    edges_df, nodes_df, graph = load_graph(edge_file, node_file, sepn, sepe, idcol, weightcol)
    graph.write_pickle('graph.pkl')
    node_names = graph.vs['name']

    # Get initial partitions
    print('Get initial {} partitions'.format(nriter))
    partitions = nwtools.consensus.get_initial_partitions(graph, weights='weight', nr_partitions=nriter)
    partitions_df = pd.DataFrame([p.membership for p in partitions], columns=graph.vs['name'])
    partitions_df.to_csv(os.path.join(out_dir, 'partitions.csv'), index=False)

    # Get consensusmatrix
    print('Consensus clustering with t={:.2f}'.format(threshold))
    consensus_matrix, consensus_membership = nwtools.consensus.consensus_partition(graph, initial_partition=partitions,
                        nr_partitions=len(partitions),
                        threshold=threshold,
                        weights='weight')
    np.save('consensus.npy', consensus_matrix)

    # Get edge consistency and create edge dataframe
    print('Calculating edge consistency..')
    graph = nwtools.consensus.get_edge_consistency(graph, consensus_matrix)
    edges_att_df = consensus_df = pd.DataFrame({'Source': [min(node_names[e.source], node_names[e.target])
                                                           for e in graph.es],
                                 'Target': [max(node_names[e.source], node_names[e.target])  for e in graph.es],
                                'consensus': graph.es['consensus'],
                                'consistency': graph.es['consistency']})

    edges_att_df = edges_att_df.merge(edges_df,
                                      left_on=['Source', 'Target'],
                                      right_on=['Source', 'Target'])
    edges_att_df.to_csv('edges-consistency.csv', index=False)

    # Get node consistency
    print('Calculating node consistency..')
    nodes_att_df = get_node_consistency(graph, edges_att_df)
    nodes_att_df = nodes_att_df.merge(nodes_df, left_index=True, right_index=True, how='left')
    nodes_att_df['consensus'] = pd.Series(consensus_membership, index=node_names)
    nodes_att_df.to_csv('nodes-consistency.csv')

    print('Done.')

if __name__ == '__main__':
    run_consistency()


from django.urls import path

from . import views

app_name = "timetable"

urlpatterns = [
    path('', views.TimetableView.as_view(), name='timetable'),
    path('<int:pk>/', views.SingleTimetableView.as_view(), name='single_timetable'),
    path('subscribe/', views.SubscribeView.as_view(), name='subscribe')
]


import torch
from vedo import Picture, show
from _canny_edge_maker import canny

class UtilityWorker:
    def __init__(self,
                 input_data_clean,
                 prediction,
                 device,
                 warning_m):
        self.prediction = prediction
        self.device = device
        self.input_data_clean = input_data_clean
        self.red = torch.tensor([255, 0, 0]).cpu()
        self.green = torch.tensor([0, 255, 0]).cpu()
        self.checker = torch.tensor([1, 1, 1]).cpu()
        self.threshold = 0.038
        self.warning = warning_m

    def forward(self):
        resume = self.cell_amount_finder()
        prediction_1, prediction_2 = self.prediction_splitter()
        canny_1 = self.canny_edge_maker(prediction_1)
        canny_2 = self.canny_edge_maker(prediction_2)
        final_data = self.canny_edge_applier(canny_1, canny_2)
        self.show_save(self.input_data_clean, resume, self.prediction, final_data)

    def cell_amount_finder(self):
        amount_finder = self.prediction.unique(return_counts=True)[1]
        good_c, bad_c = amount_finder[2], amount_finder[1]

        return bad_c / good_c

    def prediction_splitter(self):
        prediction_1 = torch.where(self.prediction == 1, 1, 0)
        prediction_2 = torch.where(self.prediction == 2, 1, 0)

        return prediction_1, prediction_2

    def canny_edge_maker(self, some_data):
        data = torch.unsqueeze(some_data, 0)
        data = data.expand(3, 1920, 2560).to(torch.float).cpu()
        image = canny(data)
        image = torch.squeeze(image, 0)
        image = torch.movedim(image, 0, -1) / torch.max(image).item()
        image = torch.where(image > self.threshold, 1, 0)

        return image.expand(1920, 2560, 3)

    def canny_edge_applier(self, canny_1, canny_2):
        data = self.input_data_clean.to(torch.int64)
        canny_applier = torch.where(canny_1 == self.checker,
                                    self.red, data)

        return torch.where(canny_2 == self.checker,
                           self.green, canny_applier)

    def show_save(self, victim, resume,
                  prediction_filled,
                  prediction_combined):
        data_clean = victim.to(torch.uint8).cpu().detach().numpy()
        data_mask = torch.unsqueeze(prediction_filled, -1)\
                         .expand(1920, 2560, 3)\
                         .to(device='cpu', dtype=torch.int64)
        data_mask = torch.where(data_mask == self.checker, self.red,
                                data_mask*self.green).detach().numpy()
        diagnosis = 'You are healthy\n' if resume < self.warning else 'Warning\n'
        combi = prediction_combined.to(torch.uint8).detach().numpy()
        p1 = Picture(data_clean)
        p2 = Picture(data_mask).z(1200)
        p3 = Picture(combi).z(2400)
        show((p1, 'Input sample'),
             (p2, '\nPrediction masks'),
             (p3, '\n\nPrediction edge&mask\n\nDiagnose\n'+diagnosis+str(round(resume.item(), 3))),
             __doc__, axes=7, viewup="y", bg='black').close()


from django import template
from django.core.cache import cache

from contact.models import Social, About

register = template.Library()

@register.simple_tag()
def get_social_links():
    socials = cache.get('socials')
    if not socials:
        socials = Social.objects.all()
        cache.set('socials', socials, 3600)
    return socials

@register.simple_tag()
def get_about():
    about = cache.get('about')
    if not about:
        about = About.objects.last()
        cache.set('about', about, 3600)
    return about


import requests

targets = ('area', 'population', 'iso', 'country', 'capital', 'continent',
           'tld', 'currency_code', 'currency_name', 'phone', 'postal_code_format',
           'postal_code_regex', 'languages', 'neighbours')

def download(url, num_retries=2, user_agent='wswp', proxies=None):
    '''下载一个指定的URL并返回网页内容
        参数：
            url(str): URL
        关键字参数：
            user_agent(str):用户代理（默认值：wswp）
            proxies（dict）： 代理（字典）: 键：‘http’'https'
            值：字符串（‘http(s)://IP’）
            num_retries(int):如果有5xx错误就重试（默认：2）
            #5xx服务器错误，表示服务器无法完成明显有效的请求。
            #https://zh.wikipedia.org/wiki/HTTP%E7%8A%B6%E6%80%81%E7%A0%81
    '''
    print('==========================================')
    print('Downloading:', url)
    headers = {'User-Agent': user_agent} #头部设置，默认头部有时候会被网页反扒而出错
    try:
        resp = requests.get(url, headers=headers, proxies=proxies) #简单粗暴，.get(url)
        html = resp.text #获取网页内容，字符串形式
        if resp.status_code >= 400: #异常处理，4xx客户端错误 返回None
            print('Download error:', resp.text)
            html = None
            if num_retries and 500 <= resp.status_code < 600:
                # 5类错误
                return download(url, num_retries - 1)#如果有服务器错误就重试两次

    except requests.exceptions.RequestException as e: #其他错误，正常报错
        print('Download error:', e)
        html = None
    return html #返回html


import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import sys
from utils import get_data, rename_headers, get_dataframe
import matplotlib.animation as animation
import matplotlib
import seaborn as sns

# 1f77b4
# ff7f0e
# 2ca02c
# d62728
# 9467bd
# 8c564b

def animate(i):
    ann = fea.iloc[:int(i+1)] #select data range
    exp = chaboche.iloc[:int(i+1)] #select data range

    p = sns.lineplot(x=exp['ET12'], y=exp['S12'], data=exp, color='b')
    p = sns.lineplot(x=ann['ET12'], y=ann['S12'], data=ann, color='r',marker='.')

    # p = sns.lineplot(x=exp['Time'], y=exp['X11'], data=exp, color='#1f77b4')
    # p = sns.lineplot(x=ann['t'], y=ann['X11'], data=ann, color='#ff7f0e',marker='.')
    # p = sns.lineplot(x=exp['Time'], y=exp['X11'], data=exp, color='#1f77b4')
    #
    # p = sns.lineplot(x=ann['t'], y=ann['X22'], data=ann, color='#d62728', marker='.')
    # p = sns.lineplot(x=exp['Time'], y=exp['X22'], data=exp, color='#2ca02c')
    #
    # p = sns.lineplot(x=ann['t'], y=ann['X12'], data=ann, color='#8c564b', marker='.')
    # p = sns.lineplot(x=exp['Time'], y=exp['X12'], data=exp, color='#9467bd')
    p.tick_params(labelsize=7)
    plt.setp(p.lines,linewidth=3)

# Driver program
if __name__ == "__main__":
    workdir = '/home/miguel/Documents/tese/ViscoPlastic-ML/2D/butterfly/'
    abaqus_dir = '/home/miguel/Documents/tese/ViscoPlastic-ML/abaqus_deploy/butterfly/'
    workvid = '/home/miguel/Documents/tese/ViscoPlastic-ML/2D/butterfly/videos/'
    P = 'P_1'
    el_num = 75

    P = ['P_1', 'P_2', 'P_3']
    el_num = [75, 322, 229]

    for n in range(0,3):
        data_dir = []
        data_dir.append(abaqus_dir + 'state_butterfly_hist.txt')
        data_dir.append(workdir + '/results/' + P[n] + '/data_' + P[n] + '.csv')
        data_dir.append(abaqus_dir + 'deriv_butterfly_hist.txt')
        abaqus, chaboche = get_data(data_dir)
        chaboche = chaboche.drop(chaboche.index[len(chaboche)-1])
        abaqus = rename_headers(abaqus)

        # Select from abaqus dataframe info about el_num and int_point
        fea = get_dataframe(abaqus, el_num[n], 2)
        exit()
        # remove repeated values
        # Plot and save graphs of Back stress
        title = 'Back Stress'
        Writer = animation.writers['ffmpeg']
        writer = Writer(fps=2, metadata=dict(artist='Me'), bitrate=-1)
        fig = plt.figure(figsize=(10,6))
        plt.xlim(0, 0.05)
        plt.ylim(0, 75)
        ani = matplotlib.animation.FuncAnimation(fig, animate, frames=24, repeat=True)
        ani.save(workvid + 'S12.mp4', writer=writer)
        plt.close()
        break


"""django_conf URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from rest_framework.routers import SimpleRouter
from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView

from django_application.views import ArticleViewSet, ArticleProfileViewSet, ArticleDetailViewSet

router = SimpleRouter()
router.register('api/articles', ArticleViewSet),
router.register('api/article', ArticleDetailViewSet),
router.register('api/profile_article', ArticleProfileViewSet),

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/auth/', include('djoser.urls')),
    path('api/auth/', include('djoser.urls.authtoken')),
    path('api/auth/', include('djoser.urls.jwt')),

    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
    path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'),

    path('', include('react_application.urls')),
    path('registration/', include('react_application.urls'))
]

urlpatterns += router.urls

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

# urlpatterns api/auth/:
# # # /users/
# # # /users/me/
# # # /users/confirm/
# # # /users/resend_activation/
# # # /users/set_password/
# # # /users/reset_password/
# # # /users/reset_password_confirm/
# # # /users/set_username/
# # # /users/reset_username/
# # # /users/reset_username_confirm/
# # # /token/login/ (Token Based Authentication)
# # # /token/logout/ (Token Based Authentication)
# # # /jwt/create/ (JSON Web Token Authentication)
# # # /jwt/refresh/ (JSON Web Token Authentication)
# # # /jwt/verify/ (JSON Web Token Authentication)


# -*- coding: utf-8 -*-
"""
Created on Mon Sep 10 15:16:34 2018

@author: Akshay Jagadale
"""

class Solution:
    def canConstruct(self, ransomNote, magazine):
        """
        :type ransomNote: str
        :type magazine: str
        :rtype: bool
        """

        #method 1
#        ransomNotelist = list(ransomNote)
#        magazinelist = list(magazine)
#
#        for i in ransomNotelist:
#            #print(i)
#            if i not in magazinelist:
#                return False
#            magazinelist.remove(i)
#        return True

#        #method 2: using collections
#        from collections import Counter
#        return not (Counter(ransomNote) - Counter(magazine))

        #method 3 using set, count
        for i in set(ransomNote):
            if ransomNote.count(i) > magazine.count(i):
                return False
        return True

myobj = Solution()
print(myobj.canConstruct("aaa", "abb"))


#!/usr/bin/env python3

"""The setup script."""

from setuptools import find_packages, setup

with open('requirements.txt') as f:
    requirements = f.read().strip().split('\n')

with open('README.md') as f:
    long_description = f.read()
setup(
    maintainer='Xdev',
    maintainer_email='xdev@ucar.edu',
    python_requires='>=3.7',
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'License :: OSI Approved :: Apache Software License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.7',
        'Programming Language :: Python :: 3.8',
        'Programming Language :: Python :: 3.9',
        'Topic :: Scientific/Engineering',
        'Operating System :: OS Independent',
        'Intended Audience :: Science/Research',
    ],
    description='Jupyter Lab Port Forwarding Utility',
    install_requires=requirements,
    license='Apache Software License 2.0',
    long_description_content_type='text/markdown',
    long_description=long_description,
    include_package_data=True,
    keywords='jupyter-forward',
    name='jupyter-forward',
    packages=find_packages(include=['jupyter_forward', 'jupyter_forward.*']),
    entry_points={
        'console_scripts': [
            'jupyter-forward = jupyter_forward.cli:main',
            'jlab-forward = jupyter_forward.cli:main',
        ]
    },
    url='https://github.com/ncar-xdev/jupyter-forward',
    project_urls={
        'Documentation': 'https://github.com/ncar-xdev/jupyter-forward',
        'Source': 'https://github.com/ncar-xdev/jupyter-forward',
        'Tracker': 'https://github.com/ncar-xdev/jupyter-forward/issues',
    },
    use_scm_version={
        'version_scheme': 'post-release',
        'local_scheme': 'dirty-tag',
    },
    zip_safe=False,
)


#!/usr/bin/env python
# -*- coding: utf-H -*-
# @Time     : 2020/7/30 7:34
# @File     : code.py
# ----------------------------------------------
# ☆ ☆ ☆ ☆ ☆ ☆ ☆
# >>> Author    : Alex 007
# >>> QQ        : 2426671397
# >>> WeChat    : Alex-Paddle
# >>> Mail      : alex18812649207@gmail.com
# >>> Github    : https://github.com/koking0
# >>> Blog      : https://alex007.blog.csdn.net/
# ☆ ☆ ☆ ☆ ☆ ☆ ☆

class Solution:
    def integerBreak(self, n: int) -> int:
        dp = [0 for _ in range(n + 1)]
        for i in range(2, n + 1):
            for j in range(1, i):
                dp[i] = max(dp[i], j * (i - j), j * dp[i - j])
        return dp[n]


import pygame as pyg
import os
import time
from random import randint

# constants for the screen
WIDTH = 960
HEIGHT = 540
BORDER = 20

# Background object contains the display, and draws and updates the animated background
# Animation derived from instructions from the Nutt.net pygame tutoral
class Background(pyg.sprite.Sprite):
    screen = pyg.display.set_mode((WIDTH, HEIGHT), pyg.HWSURFACE)

    def __init__(self):
        pyg.sprite.Sprite.__init__(self)
        # loading the background image - two copies, to allow it to scroll
        self.bg1 = pyg.image.load(os.path.join("images", "background.jpg")).convert()
        self.bg2 = pyg.image.load(os.path.join("images", "background.jpg")).convert()
        self.bg1_x = 0
        self.bg2_x = self.bg1.get_width()

    def update(self):
        # updating the background positions so it scrolls
        self.bg1_x -= 1
        self.bg2_x -= 1
        if self.bg1_x <= -(self.bg1.get_width()):
                self.bg1_x = self.bg2_x + self.bg2.get_width()
        if self.bg2_x <= -(self.bg2.get_width()):
                self.bg2_x = self.bg1_x + self.bg1.get_width()

    def draw(self):
        self.screen.blit(self.bg1, (self.bg1_x, 0))
        self.screen.blit(self.bg2, (self.bg2_x, 0))

class Player(pyg.sprite.Sprite):
    # loading the player sprite and getting it's dimensions
    sprite = pyg.image.load(os.path.join("images", "hero_side.png")).convert_alpha()
    ln = sprite.get_width()
    ht = sprite.get_height()
    # loading the images for the sprite with animated thrusters
    animated_sprite = [pyg.image.load(os.path.join("images", "hero_side1.png")).convert_alpha(),
                       pyg.image.load(os.path.join("images", "hero_side2.png")).convert_alpha(),
                       pyg.image.load(os.path.join("images", "hero_side3.png")).convert_alpha()]

    def __init__(self):
        # sprite stuff, setting image, rectangle and mask (for collision detection)
        pyg.sprite.Sprite.__init__(self)
        self.image = self.sprite
        self.rect = self.image.get_rect()
        self.col_mask = pyg.mask.from_surface(self.image)
        # flag to hide the player image when player is killed
        self.hidden = False
        self.hide_timer = pyg.time.get_ticks()
        # initialises starting position on the board.
        self.x_new = self.rect.x = BORDER
        self.y_new = self.rect.y = HEIGHT//2 - self.ht
        self.speed = 4
        # animation loop - as it increments up, different images of the sprite are drawn, resulting in animation
        self.animation_loop = 0
        # x-axis velocity, used to determine whether to animate thrusters
        self.x_vel = 0
        self.altering_name = False
        self.player_name = ''

    def move(self, x_dir, y_dir):
        # takes the direction of movement from the event handler, multiplies by speed
        if (x_dir != 0) and (y_dir != 0):
            # this modifies the speed if moving diagonally
            velocity = int(self.speed * 0.8)
        else:
            velocity = self.speed
        # this step is here for animation purposes - it flags whether to animate thrusters
        self.x_vel = x_dir
        self.x_new = self.rect.x + (x_dir * velocity)
        self.y_new = self.rect.y + (y_dir * velocity)

    def update(self):
        # checks if the new positions are in the play area, and updates if they are
        if (self.x_new > BORDER) and (self.x_new < (WIDTH//2)):
            self.rect.x = self.x_new
        if (self.y_new > BORDER) and (self.y_new < HEIGHT-BORDER-self.ht):
            self.rect.y = self.y_new
        # if the player gets killed, hidden flag is true - hides player image, and resets position to starting point
        if self.hidden and pyg.time.get_ticks() - self.hide_timer > 1000:
            self.hidden = False
            self.x_new = self.rect.x = BORDER
            self.y_new = self.rect.y = HEIGHT//2 - self.ht
        # if player moving backwards, draws ship without thrusters
        if self.x_vel < 0:
            self.image = self.sprite
        # otherwise draws animated ship, and increments through animation loop
        else:
            self.image = self.animated_sprite[self.animation_loop//3]
            self.animation_loop += 1
            # reseting animation loop as only has three images to cycle through
            if self.animation_loop >= 9:
                self.animation_loop = 0

    def hide(self):
        # hide the player temporarily when the player is killed
        self.hidden = True
        self.hide_timer = pyg.time.get_ticks()
        self.rect.center = (WIDTH / 2, HEIGHT + 200)

    # logic for getting the user's name for the highscore board
    def update_name(self, new_letter="", delete=0):
        if not delete:
            self.player_name += new_letter
        else:
            self.player_name = self.player_name[:-1]

class Bullet(pyg.sprite.Sprite):
    # loading the bullet sprite and getting it's dimensions
    sprite = pyg.image.load(os.path.join("images", "bullet.png")).convert_alpha()
    ln = sprite.get_width()
    ht = sprite.get_height()

    def __init__(self):
        pyg.sprite.Sprite.__init__(self)
        self.image = self.sprite
        self.rect = self.image.get_rect()
        # bullet velocity
        self.vx = 15

    def update(self):
        self.rect.x += self.vx

class Alien(pyg.sprite.Sprite):
    # loading the alien sprite and getting it's dimensions
    sprite = pyg.image.load(os.path.join("images", "enemy2.png")).convert_alpha()
    ln = sprite.get_width()
    ht = sprite.get_height()
    animated_sprite = [pyg.image.load(os.path.join("images", "enemy1.png")).convert_alpha(),
                       pyg.image.load(os.path.join("images", "enemy2.png")).convert_alpha(),
                       pyg.image.load(os.path.join("images", "enemy3.png")).convert_alpha()]
    shielded_sprite = [pyg.image.load(os.path.join("images", "shield1.png")).convert_alpha(),
                       pyg.image.load(os.path.join("images", "shield2.png")).convert_alpha(),
                       pyg.image.load(os.path.join("images", "shield3.png")).convert_alpha()]

    def __init__(self, main):
        # pass main to alien, so it can access the player position and add alien bullets to the groups
        self.main = main
        # the sprite stuff, to get its image, rectangle and mask (for collision detection)
        pyg.sprite.Sprite.__init__(self)
        self.image = self.sprite
        self.rect = self.image.get_rect()
        self.col_mask = pyg.mask.from_surface(self.image)
        # sets the alien's position on the right of the screen, at a random y coordinate
        self.rect.x = WIDTH + self.ln + randint(1, 100)
        self.rect.y = randint(BORDER, HEIGHT - BORDER - self.ht)
        # alien velocity
        self.vx = -2
        # fire rate - the SMALLER the number, the MORE OFTEN they fire
        self.fire_rate = 400
        self.firing_solution = 1
        self.animation_loop = 0
        self.kill_score = 20
        # hit points - determines whether shield is up or down (displays different image)
        self.hitpoints = 1

    def collide(self, sprite_group):
        # collision detection only for when spawning new aliens, so they don't overlap
        if pyg.sprite.spritecollide(self, sprite_group, False):
            self.rect.x += self.ln * randint(2, 5)
            self.collide(sprite_group)

    def shoot(self, rate):
        # aliens fire based on fire_rate, adds alien bullets to the groups
        if (randint(1, rate) == self.firing_solution) and (self.rect.x < WIDTH):
            albull = AlBullet(self.rect.x, (self.rect.y + self.ht//2))
            self.main.alienbullets.add(albull)
            self.main.all_sprites.add(albull)

    def animate(self):
        if self.hitpoints == 2:
            self.image = self.shielded_sprite[self.animation_loop // 3]
        else:
            self.image = self.animated_sprite[self.animation_loop // 3]
        self.animation_loop += 1
        # reseting animation loop as only has three images to cycle through
        if self.animation_loop >= 9:
            self.animation_loop = 0

    def update(self):
        self.animate()
        # moves across the screen to the left, and fires
        self.rect.x += self.vx
        self.shoot(self.fire_rate)

    def on_hit(self, main, damage):
        self.hitpoints -= damage
        if self.hitpoints <= 0:
            self.kill()
            main.score += self.kill_score
            main.sounds.boom.play()
            main.expl = Explosion(self.rect.center, 'lg')
            main.all_sprites.add(main.expl)
        else:
            main.sounds.ping.play()

class ShieldAlien(Alien):
    def __init__(self, main):
        Alien.__init__(self, main)
        self.hitpoints = 2

class SmartAlien(ShieldAlien):

    def __init__(self, main):
        ShieldAlien.__init__(self, main)
        # limit the distance across the smart alien moves, and sets y velocity
        self.width_limit = WIDTH*0.8
        self.vy = 1
        # smart aliens fire more often than regular aliens
        self.fire_rate = 75

    def update(self):
        self.animate()
        # moves to the position on the right of the screen
        if self.rect.x > self.width_limit:
            self.rect.x += self.vx
        # tracks player movement and shoots
        if self.rect.y + self.ht//2 < self.main.player.rect.y + self.main.player.ht//2:
            self.rect.y += self.vy
        elif self.rect.y + self.ht//2 > self.main.player.rect.y + self.main.player.ht//2:
            self.rect.y -= self.vy
        self.shoot(self.fire_rate)

class Boss(SmartAlien):
    sprite = pyg.image.load(os.path.join("images", "boss1.png")).convert_alpha()
    damaged = [pyg.image.load(os.path.join("images", "boss5.png")).convert_alpha(),
               pyg.image.load(os.path.join("images", "boss4.png")).convert_alpha(),
               pyg.image.load(os.path.join("images", "boss3.png")).convert_alpha(),
               pyg.image.load(os.path.join("images", "boss2.png")).convert_alpha(),
               pyg.image.load(os.path.join("images", "boss1.png")).convert_alpha()]
    ln = sprite.get_width()
    ht = sprite.get_height()
    def __init__(self, main):
        SmartAlien.__init__(self, main)
        self.hitpoints = 50
        self.fire_rate = 40
        self.kill_score = 1000
        self.width_limit = WIDTH - self.ln

    def animate(self):
        if self.hitpoints < 50:
            self.image = self.damaged[self.hitpoints//10]
        bar = pyg.Rect((WIDTH//2 - 100), (HEIGHT - 20), 200, 18)
        healthbar = pyg.Rect((WIDTH//2 - 100), (HEIGHT - 19), self.hitpoints*4, 16)
        pyg.draw.rect(Background.screen, pyg.Color("black"), bar)
        pyg.draw.rect(Background.screen, pyg.Color("red"), healthbar)

    def update(self):
        if self.hitpoints > 15:
            SmartAlien.update(self)
        else:
            self.animate()
            new_x = self.rect.x + self.vx
            new_y = self.rect.y + self.vy
            if (new_x < 0) or (new_x > WIDTH-self.ln):
                self.vx = -self.vx
            if (new_y + self.ht//2 < BORDER) or (new_y + self.ht//2 > HEIGHT-BORDER):
                self.vy = -self.vy
            self.rect.x = new_x
            self.rect.y = new_y

class AlBullet(Bullet):
    # inherits most stuff from bullet, but moves right to left, and is slower
    def __init__(self, x, y):
        Bullet.__init__(self)
        self.rect.x = x
        self.rect.y = y
        self.vx = -10

# This class was built from instructions given in the pygame tutorial by KidsCanCode.org
class Explosion(pyg.sprite.Sprite):
    def __init__(self, center, size):
        pyg.sprite.Sprite.__init__(self)
        self.size = size
        self.frame = 0
        self.last_update = pyg.time.get_ticks()
        self.frame_rate = 50
        self.explosion_anim = {}
        self.explosion_anim['lg'] = []
        self.explosion_anim['sm'] = []
        for i in range(1,6):
            filename = 'boom{}.png'.format(i)
            img = pyg.image.load(os.path.join("images", filename)).convert_alpha()
            img_lg = pyg.transform.scale(img, (100, 100))
            self.explosion_anim['lg'].append(img_lg)
            img_sm = pyg.transform.scale(img, (75, 75))
            self.explosion_anim['sm'].append(img_sm)
        self.image = self.explosion_anim[self.size][0]
        self.rect = self.image.get_rect()
        self.rect.center = center

    def update(self):
        now = pyg.time.get_ticks()
        if now - self.last_update > self.frame_rate:
            self.last_update = now
            self.frame += 1
            if self.frame == len(self.explosion_anim[self.size]):
                self.kill()
            else:
                center = self.rect.center
                self.image = self.explosion_anim[self.size][self.frame]
                self.rect = self.image.get_rect()
                self.rect.center = center

class PowerUp(pyg.sprite.Sprite):

    animated_life = [pyg.image.load(os.path.join("images", "hero_life.png")).convert_alpha(),
                     pyg.image.load(os.path.join("images", "hero_life1.png")).convert_alpha(),
                     pyg.image.load(os.path.join("images", "hero_life2.png")).convert_alpha()]

    animated_bombs = [pyg.image.load(os.path.join("images", "bomb1.png")).convert_alpha(),
                      pyg.image.load(os.path.join("images", "bomb2.png")).convert_alpha(),
                      pyg.image.load(os.path.join("images", "bomb3.png")).convert_alpha()]

    def __init__(self, main):
        self.main = main
        pyg.sprite.Sprite.__init__(self)
        self.spawned = False
        self.rewound = False
        self.powers_dict = {0: pyg.image.load(os.path.join("images", "1pixelimage.png")).convert_alpha(),
                            1: [self.animated_life, 93, 25, self.extra_life],
                            2: [self.animated_bombs, 58, 100, self.extra_bomb]}
        self.starttime = time.time()
        self.animation_loop = 0
        self.power_up = []
        self.spawn_pos = (0, 0)
        self.image = self.powers_dict[0]
        self.rect = self.image.get_rect()

    def update(self):
        if (time.time() - self.starttime)//1 == 3 and not self.spawned:
            # generate a random number between 1 and however many
            self.power_up = self.powers_dict[randint(1, 2)]
            self.image = self.power_up[0][0]
            self.rect = self.image.get_rect()
            self.rect.x = randint(BORDER, (WIDTH//2)-self.power_up[1])
            self.rect.y = randint(BORDER, HEIGHT-BORDER-self.power_up[2])
            self.spawned = True
        elif (time.time() - self.starttime)//1 == 10:
            self.starttime = time.time()
            self.image = self.powers_dict[0]
            self.rect.x = 0
            self.rect.y = 0
            self.spawned = False

        elif self.spawned:
            self.image = self.power_up[0][self.animation_loop // 3]
            self.animation_loop += 1
            if self.animation_loop >= 9:
                self.animation_loop -= 5
                self.rewound = True
            elif self.animation_loop >= 7 and self.rewound:
                self.animation_loop = 0
                self.rewound = False

    def extra_life(self):
        self.main.lives += 1

    def extra_bomb(self):
        self.main.bombs += 1


def moveZerosToEnd(nums):

    # 0을 빼면 인덱스가 모두 바뀌어버리기 떄문에 0을빼는 방법이아닌
    # 0 이 아닌숫자를 순서대로 앞으로 넣는 방법이 자연스러운 방법이다.
    curr = 0
    for i in range(len(nums)):
        if nums[i] != 0:
            nums[curr] = nums[i]
            nums[i] = 0
            # nums[i] = 0
            curr += 1
    return nums

    # curr = 0
    # for i in range(len(nums)):
    #     if nums[i] != 0:
    #         # print('nums['+str(i)+']가 0값을가짐')
    #         # print(nums)
    #         nums[curr] = nums[i]
    #         # print(nums[curr])
    #         # print(nums)
    #         # if i != curr:
    #         nums[i] = 0
    #         # print(nums)
    #         # nums[i] = 0
    #         curr += 1
    # return nums

    # for i in range(len(nums)):

    # for 문의 index가 바뀌기 때문에 while문을 통해 문제를 해결하려고 했으나 이 경우에도 오류가 생기는것을 알 수 있다.

    # def moveZerosToEnd(nums):
    # for i in range(len(nums)):
    #     if nums[i] == 0:
    #         nums.pop(i)
    #         nums.append(0)

    # return nums

    # 따라서 이를 while문을 통해서 해결하려고 했다.
    # 0이면 pop한 뒤에 append를 하고, 아니면 i를 증가시키는것으로 ..
    # 그런데 이 경우 [0,0,1] 그리고 [0,0] 으로만 채워져있는경우를 해결하지 못한다.
    # 따라서 pop과 append를 사용하기보다는 , curr을 이용하는것이 더 좋은 방법인것같다.
    i = 0
    print(len(nums)-nums.count(0))
    while i < len(nums)-nums.count(0) :
        print(i)
        print(nums)
        if nums[i] == 0:
            # print('if문 통과')
            nums.pop(i)
            # nums.pop(nums[i])
            # print(nums)
            nums.append(0)
            print(nums)
            i = 0
            if i == len(nums)-nums.count(0) -1:
                break

        else:
            i += 1

        # else :
        #     print('if문을 통과하지 못함')

        # print('결과후의')
        # print(nums)
    return nums

    # temp = []
    # for i in range(len(nums)):
    #     if nums[i] == 0:
    #         temp.append(i)
    #         print(temp)
    # for i in range(len(temp)):
    #     # nums.pop(temp[i])
    #     print(temp[i])
    # pop을 할때마다 nums의 길이가 달라져서 list out of range가 나오게 된다.

def main():
    # print(moveZerosToEnd( [0, 0, 0, 37, 4, 5, 0, 50, 0, 34, 0, 0] ))

    # print(moveZerosToEnd([0, 8, 0, 37]))
    # print(moveZerosToEnd([0, 8, 0, 37, 4, 5, 0, 50, 0, 34, 0, 0]))

    print(moveZerosToEnd([0,0,1]))

if __name__ == "__main__":
    main()


#Calculate average temperature

numDays = int(input("How many day's temperature? "))
total = 0
#empty array to store temperatures
temp = []
#range second parameter will go to parameter -1, that's why we use +1 so it consider param itself
for i in range(numDays):
    nextDay = int(input("Day " + str(i+1) + "'s high temp:"))
    temp.append(nextDay)
    #temp total
    total += temp[i]

#round by 2
avg = round(total/numDays,2)
print("\nAverage = " + str(avg))

above = 0
#finds how many days are above avg
for i in temp:
    if i > avg:
        above += 1

print(str(above) + " day(s) above average")


def shifting(sf, shift:int):
    sf['cons_T-'+str(shift)] = sf['consumption'].shift(periods=shift, freq='H')
    return sf

temp_df = df.filter(items=['consumption'])
day_temp_df = df.filter(items=['consumption'])

#week
for i in range(24, 168+1):
    temp_df = shifting(temp_df, i)
temp_df = temp_df.drop(['consumption'], axis=1)

#day
for i in range(24, 48+1):
    day_temp_df = shifting(day_temp_df, i)
day_temp_df = day_temp_df.drop(['consumption'], axis=1)

#columns added
df['day_mean'] = day_temp_df.mean(axis=1, skipna=True)
df['week_mean'] = temp_df.mean(axis=1, skipna=True)

df = df.fillna(0)


# -*- coding: utf-8 -*-
# @Time    : 2020-05-01 14:16
# @Author  : speeding_motor

from Model import DarkNet19
from loss import YoloLoss
import pascal
from config import EPOCHS
import tensorflow as tf

def main():
    yolo = DarkNet19()

    batch_data = pascal.generate_batch_data()

    yololoss = YoloLoss()
    optimizer = tf.optimizers.Adam(learning_rate=0.01)

    for epoch in range(EPOCHS):
        i = 0
        for batch in batch_data.as_numpy_iterator():

            batch_names, batch_boxs = pascal.parse_batch_set(batch)
            train_image = pascal.get_train_images(batch_names)
            train_labels = pascal.get_train_labels(batch_boxs)

            with tf.GradientTape() as tape:
                y_pred = yolo(train_image)
                loss = yololoss(y_true=train_labels, y_pred=y_pred)

            grads = tape.gradient(loss, yolo.trainable_variables)
            optimizer.apply_gradients(zip(grads, yolo.trainable_weights))
            i = i + 1
            print("epoch {} batch_index={}  loss={}".format(epoch, i, loss.numpy()))

        if epoch % 1 == 0:
            print("epoch is {}  loss is {}".format(epoch, loss))

if __name__ == '__main__':
    main()


from cmd.helpers.ObjectHelper import rot_center
import pygame
import random
from cmd.config.config import (
    RES_X,
    RES_Y,
    BONUS_IMG
)

class BaseBonus:
    """
    Базовый класс для бонусов - хранит состояние, координаты, угол, id и таймер
    """
    def __init__(self, id, object_positions, img, screen, pos_x=0, pos_y=0, type='power'):
        self.id = id
        self.object_positions = object_positions
        self.img = img
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.abs_pos_x = 0
        self.abs_pos_y = 0
        self.screen = screen
        self.age = 0
        self.set_img(img)
        self.type = type

    def move(self,
             left=None,
             right=None,
             up=None,
             down=None):
        if left:
            self.abs_pos_x += left
            self.pos_x += left
        if right:
            self.abs_pos_x -= right
            self.pos_x -= right
        if up:
            self.abs_pos_y += up
            self.pos_y += up
        if down:
            self.abs_pos_y -= down
            self.pos_y -= down

    def set_img(self, img: str = None):
        self.img = pygame.image.load(img)

    def draw_bonus(self):
        self.screen.blit(self.img,
                         (self.abs_pos_x - 10,
                          self.abs_pos_y - 10))

    def spawn(self):
        self.abs_pos_x = self.pos_x
        self.abs_pos_y = self.pos_y

    def spawn_random(self):
        self.pos_x = random.randint(0, RES_X)
        self.pos_y = random.randint(0, RES_Y)
        self.spawn()

    def spawn_coords(self, pos_x, pos_y):
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.spawn()


import pygame

import config.colors as color

from config.Resources import Constants
from runnable.ControlButton import ControlButton
from runnable.Runnable import Runnable
import runnable
from models.TextObject import TextObject
from runnable.TestMenu import TestMenu

class PauseMenu(Runnable):
    def handle_player_events(self):
        for event in self.events:
            if event.type is pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.runnable_stack.pop()

    def __init__(self, width, height, runnable_stack, bg_color, background=None, x=0, y=0):
        Runnable.__init__(self, width, height, runnable_stack, x, y)
        # self.menu_surface = pygame.display.set_mode((width, height))
        self.is_menu_running = False
        self.surface.fill(bg_color)
        self.surface.set_alpha(Constants.menu_alpha)
        self.title = TextObject("Pause", Constants.font, 90, color.white)
        self.surface.blit(self.title.Text, (width // 4, 0))
        quit_button = ControlButton("", "quit", width // 4, height // 2, width // 6, width // 6,
                                    color.black, color.black,
                                    lambda: runnable_stack.set_one_task(
                                        runnable.MainMenu.MainMenu(Constants.screen_width, Constants.screen_height, runnable_stack,
                                                 color.brick)),
                                    Constants.quit_button_image)
        resume_button = ControlButton("", "resume", 3 * width // 4 - width // 6, height // 2, width // 6, width // 6,
                                      color.black, color.black, lambda: runnable_stack.pop(),
                                      Constants.resume_button_image)
        self.add_control_button(quit_button)
        self.add_control_button(resume_button)


# 최대/최소값 : 데이터 중 가장 큰 값과 작은 값을 찾는 알고리즘

q = "세 정수 입력 : "
n1, n2, n3 = map(int, input(q).split())

print(f"n1 = {n1}, n2 = {n2}, n3 = {n3}")

mx = n1

if mx < n2 :    # 현재 최대(mx)보다 큰 값이야?
    mx = n2     # 이제 니가 최대 값이다~~
if mx < n3 :
    mx = n3

print("최대값 :", mx)

# ※ Python에는 max(), min() 함수가 기본 제공된다
mn = min(n1, n2, n3)
print("최소값 :", mn)


import matplotlib.pyplot as plt
import numpy as np

# 图片大小和编号
# 图线颜色和宽度、格式
x = np.linspace(-1,1,50)
y1 = 2*x+1
y2 = x**2
plt.figure()
plt.plot(x,y1)

plt.figure(num=3,figsize=(8,5)) #num 图片序号 figsize 图片大小
plt.plot(x,y2)
plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--')
plt.show()


import requests

def course_currency(wallet):
    url = "https://www.cbr-xml-daily.ru/daily_json.js"
    wallet = wallet.upper()
    res = requests.get(url).json()
    wallete = res["Valute"]

    try:
        row = "Валюта:\n{}\nПокупка:\n{}\nПродажа:\n{}".format(wallete[wallet]["Name"],
                                                               wallete[wallet]["Previous"],
                                                               wallete[wallet]["Value"])
    except:
        return "Такой валюты не существует"
    return row