from docx import Document
import ui

document = Document()

def file_namer():
    file = ui.user_file_name.get("1.0", ui.END).strip()
    for n in document_components:
        x = str(n.get("1.0", ui.END))
        document.add_paragraph(x)
        document.save("./output/" + file + ".docx")
    # file = ui.user_file_name.get("1.0", ui.END)
    # for n in document_components:
    #     x = str(n.get("1.0", ui.END))
    #     with open("../.././Desktop/" + file + ".doc", "a") as cover_letter:
    #         cover_letter.write(x + "\n")
def restore():
    for n in document_components:
        n.delete("1.0", ui.END)

# BUTTONS
submit = ui.Button(text="Create Document", command=file_namer)
submit.grid(column=1, row=0)

restore = ui.Button(text="Restore", command=restore)
restore.grid(column=1, row=8)

document_components = [ui.greeting_text, ui.opening_text, ui.qualifications_text, ui.hobbies_text, ui.conclusion_text,
                       ui.goodbye_text]

ui.window.mainloop()


from flask import Flask, render_template
from flask import send_from_directory
import os
import RPi.GPIO as GPIO
import time
import subprocess
import sys
import requests

GPIO.setmode(GPIO.BCM)
SENSOR_PIN = 27
LED = 18
GPIO.setup(SENSOR_PIN, GPIO.IN)
GPIO.setup(LED, GPIO.OUT)

app = Flask(__name__)

@app.route("/")
def index():
	#while True:
	try:

		i = GPIO.input(SENSOR_PIN)
		if i == 0:
			print ("No motion", i)
			GPIO.output(LED, False)
			motion = "not detected"
			time.sleep(3)
		elif i == 1:
			print ("Motion detected", i)
			GPIO.output(LED, True)
			motion = "detected"
			time.sleep(3)
	except KeyboardInterrupt:
		GPIO.cleanup()
		exit()
	return render_template("index.html", motion = motion)
@app.route('/favicon.ico')
def favicon():
	return send_from_directory(os.path.join(app.root_path, 'static'),
                              	'favicon.ico',
                              	mimetype='image/vnd.microsoft.icon')

if __name__ == "__main__":
	app.run(debug = True, port = 5000, host = '0.0.0.0')


import os
import sys
import copy
import time
import random
import pyspark
from pyspark.sql import DataFrame
from pyspark.sql import SparkSession
from functools import reduce
from pyspark.sql.functions import lit,rand,col,explode,array
'''
 In this project we are going to classify different IoT Botnets
'''
data_folder_loc='/'.join(os.path.abspath(os.getcwd()).split('/')[:-1])+'/data'

def init_spark():
    spark = SparkSession \
        .builder \
        .appName("Pyspark IoT Botnet Detector") \
        .config('spark.executor.cores',4)\
        .getOrCreate()

    return spark
def read_csv(spark,filename):
    '''
      csv read into data frame
    '''

    df=spark.read.csv(filename,inferSchema =True,header='true')
    #import pdb;pdb.set_trace()
    return df

def read_all_and_merge(spark,label_map=False):
    '''
      all data read from data folder
      data format 1.class.x.csv
      This method reads each csv and assigns label column with class
      merges df
    '''
    if not label_map:
       label_map={'benign':1,
               'mirai':2,
               'gafgyt':3}
    whole_df=''
    first=True
    for files in os.listdir(data_folder_loc):
        label=label_map[files.split('.')[1]] # assign numeric label
        df=read_csv(spark,data_folder_loc+'/'+files)
        labeled_df=df.withColumn('label',lit(label))
        if first:
           whole_df=labeled_df
           first=False
        else:
           whole_df=whole_df.unionByName(labeled_df)

    return whole_df
def random_shuffle(df):
    return df.orderBy(rand(seed=1375))
def remove_decimal_points_colname(dataset_df):
    input_feature_list = []
    columns=dataset_df.columns
    for col in columns: # part of data preprocessing
        new_name = col.strip()
        new_name = "".join(new_name.split())
        new_name = new_name.replace('.','')
        input_feature_list.append(new_name)
    dataset_df = dataset_df.toDF(*input_feature_list)
    return dataset_df
def oversample_df(major_df_count,minor_df_count,major_df,minor_df):
    ratio=range(round(major_df_count/minor_df_count))
    oversampled_df = minor_df.withColumn("nv",\
                     explode(array([lit(x) for x in ratio]))).drop('nv')
    combined_df = major_df.unionAll(oversampled_df)
    return combined_df
def print_sample_counts(df):
    benign_df = df.filter(col("label") == 1)
    mirai_df = df.filter(col("label") == 2)
    gafgyt_df = df.filter(col("label") == 3)
    benign_count=benign_df.count()
    mirai_count=mirai_df.count()
    gafgyt_count=gafgyt_df.count()
    print(('Benign Sample Count:%s')%str(benign_count))
    print(('\nMirai Sample Count:%s')%str(mirai_count))
    print(('\nBashlite Sample Count:%s')%str(gafgyt_count))
def remove_class_imbalance(df,stype='undersample'):
    benign_df = df.filter(col("label") == 1)
    mirai_df = df.filter(col("label") == 2)
    gafgyt_df = df.filter(col("label") == 3)
    benign_count=benign_df.count()
    mirai_count=mirai_df.count()
    gafgyt_count=gafgyt_df.count()
    print(('Benign Sample Count:%s')%str(benign_count))
    print(('\nMirai Sample Count:%s')%str(mirai_count))
    print(('\nBashlite Sample Count:%s')%str(gafgyt_count))
    if stype=='undersample':
       minordf=''
       if gafgyt_count < benign_count and   gafgyt_count < mirai_count:
          minordf=under_sample(benign_df, benign_count, mirai_df, \
            mirai_count, gafgyt_df, gafgyt_count)
          return minordf
       elif benign_count< gafgyt_count and  benign_count < mirai_count:
            minordf=under_sample(gafgyt_df, gafgyt_count, mirai_df, \
            mirai_count,benign_df, benign_count)
            return minordf
       elif mirai_count < benign_count and mirai_count < gafgyt_count:
            minordf=under_sample(gafgyt_df, gafgyt_count,  \
            benign_df, benign_count, mirai_df,mirai_count)
            return minordf
    else:
        major_df = ''
        if benign_count > mirai_count and benign_count > gafgyt_count:
            major_df = benign_df
            major_df = oversample_df(benign_count, mirai_count, major_df, mirai_df)
            major_df = oversample_df(benign_count, gafgyt_count, major_df, gafgyt_df)
        elif mirai_count > benign_count and mirai_count > gafgyt_count:
            major_df = mirai_df
            major_df = oversample_df(mirai_count, benign_count, major_df, benign_df)
            major_df = oversample_df(mirai_count, gafgyt_count, major_df, gafgyt_df)
        elif gafgyt_count > benign_count and mirai_count < gafgyt_count:
            major_df = gafgyt_df
            major_df = oversample_df(gafgyt_count, benign_count, major_df, benign_df)
            major_df = oversample_df(gafgyt_count, mirai_count, major_df, mirai_df)
        return major_df

def under_sample(major_df,major_df_count,major_df2,major_df_count2,minor_df,minor_df_count):
    ratio=int(major_df_count/minor_df_count)
    sampled_maj_df = major_df.sample(False, 1/ratio)
    #import pdb;pdb.set_trace()
    ratio=int(major_df_count2/minor_df_count)
    sampled_maj_df2 = major_df2.sample(False, 1/ratio)
    #import pdb;pdb.set_trace()
    combined_df=unionAll(minor_df,  sampled_maj_df,sampled_maj_df2)
    return combined_df
def unionAll(*dfs):
    return reduce(DataFrame.unionAll, dfs)


from gettext import Catalog
import json
import pandas as pd
import yaml
import time

from py2neo import Node

from Database.connection import db_connect

counter = 1
class KnowledgeGraph:
    """
    Instance of knowledge graph, that is hosted on neo4j server. Contains functions for creation of the knowledge graph object
    """
    def __init__(self) -> None:
        try:
            file = open('Config/knowledge_graph.yaml', 'r')
            self.configurations = yaml.safe_load(file)
            file.close()
        except FileNotFoundError as e:
            print("knowledge graph config file not found. \n", e)
            exit(1)

        try:
            file = open('Database/queries.yaml')
            self.db_queries = yaml.safe_load(file)
            file.close()
        except FileNotFoundError as e:
            print("Database queries file not found. \n", e)
            exit(1)

        self.dataset = self.load_dataset(self.configurations['DATASET_PATH'])
        self.graph = db_connect()

        self.attribute_matching = {
            "category": ["category", "belongs_to_category"],
            "symptom": ["symptom", "has_symptom"],
            "prevent": ["prevent", "has_prevention"],
            "cause": ["cause", "has_cause"],
            "acompany": ["disease", "accompanies"],
            "cure_department": ["department", "cured_by_department"],
            "cure_way": ["cure_way", "cured_by"],
            "check": ["way_to_check", "required_check"],
            "recommand_drug": ["drug", "has_recommended_drug"],
            "common_drug": ["drug", "has_common_drug"],
            "do_eat": ["food", "do_eat"],
            "not_eat": ["food", "not_eat"],
            "recommand_eat": ["food", "recommended_eat"],
            "easy_get": ["prone_to", "prone_to"]
            }

    def load_dataset(self, file_path):
        """
        Loads json file into pandas dataframe
        :param file_path: Path of a json file (with indent = 4, and newlines after every record)
        :return: pandas dataframe for the loaded file
        """
        try:
            df = pd.read_json(file_path)
        except FileNotFoundError as e:
            print("Dataset cannot be found. \n", e)
            exit(1)
        return df

    def create_node(self, category, label, properties=None):
        label = str(label)
        label = label.lstrip().rstrip().lower()
        if category == "disease":
            if str(self.graph.run(f"""MATCH (x:disease) WHERE x.name="{label}" RETURN x.name""")) != "(No data)":
                return

            cost_money = "No Data"
            if properties['cost_money'] != None and properties['cost_money'] != []:
                if properties['cost_money'] != [] and len(properties['cost_money']) == 2:
                    cost_money = str(properties['cost_money'][0]) + " - " + str(properties['cost_money'][1])
                else:
                    cost_money =  str(properties['cost_money'][0])

            node = Node(
                category,
                name = label,
                description = str(properties['desc']).lower(),
                propogation_way = str(properties['get_way']).lower(),
                get_probability = str(properties['get_prob']).lower(),
                cure_last_time = str(properties['cure_lasttime']).lower(),
                cured_probabiltity = str(properties['cured_prob']).lower(),
                cost_money = cost_money
            )
        else:
            node = Node(
                category,
                name = label
            )
        self.graph.create(node)

    def delete_all_nodes(self):
        self.graph.run(self.db_queries['DELETE_ALL_NODES'])

    def get_unique(self, df_col):
        unique_list = list()
        for cell in df_col:
            for ele in cell:
                unique_list.append(ele)

        return unique_list

    def create_all_graph_nodes(self):
        def preprocess_labels(list_of_attribute):
            for i in range(len(list_of_attribute)):
                list_of_attribute[i] = list_of_attribute[i].lstrip().rstrip().lower()
            list_of_attribute = set(list_of_attribute)
            return list_of_attribute

        # Create all disease nodes
        for index, row, in self.dataset.iterrows():
            self.create_node(category="disease", label=row['name'], properties=row)

        # Get all unique properties
        disease_categories = self.get_unique(self.dataset.category)
        symptoms = self.get_unique(self.dataset.symptom)
        departments = self.get_unique(self.dataset.cure_department)
        cure_ways = self.get_unique(self.dataset.cure_way)
        ways_to_check = self.get_unique(self.dataset.check)
        drugs = self.get_unique(self.dataset.recommand_drug)
        drugs += self.get_unique(self.dataset.common_drug)
        food = self.get_unique(self.dataset.do_eat)
        food += self.get_unique(self.dataset.not_eat)
        food += self.get_unique(self.dataset.recommand_eat)
        prevent = self.get_unique(self.dataset.prevent)
        cause = self.get_unique(self.dataset.cause)
        easy_get = self.get_unique(self.dataset.easy_get)

        symptoms = preprocess_labels(symptoms)
        disease_categories = preprocess_labels(disease_categories)
        departments = preprocess_labels(departments)
        cure_ways = preprocess_labels(cure_ways)
        ways_to_check = preprocess_labels(ways_to_check)
        drugs = preprocess_labels(drugs)
        food = preprocess_labels(food)
        prevent = preprocess_labels(prevent)
        cause = preprocess_labels(cause)
        easy_get = preprocess_labels(easy_get)

        # Create Nodes for all unique properties
        for ele in disease_categories:
            self.create_node(category="disease category", label=ele)
        for ele in symptoms:
            self.create_node(category="symptom", label=ele)
        for ele in departments:
            self.create_node(category="department", label=ele)
        for ele in cure_ways:
            self.create_node(category="cure_way", label=ele)
        for ele in ways_to_check:
            self.create_node(category="way_to_check", label=ele)
        for ele in drugs:
            self.create_node(category="drug", label=ele)
        for ele in food:
            self.create_node(category="food", label=ele)
        for ele in prevent:
            self.create_node(category="prevention", label=ele)
        for ele in cause:
            self.create_node(category='cause', label=ele)
        for ele in easy_get:
            self.create_node(category="prone_to", label=ele)

    def create_relationship(self, start_node_type, end_node_type, start_node_name, end_node_name, relation_type):
        global counter
        print(counter)
        counter += 1
        start_node_name = str(start_node_name).lower().lstrip().rstrip()
        end_node_name = str(end_node_name).lower().lstrip().rstrip()
        end_node_name =  end_node_name.replace("\"", "")
        if relation_type == "acompanies":
            if str(self.graph.run(f"""MATCH (x:disease) WHERE x.name="{end_node_name}" RETURN x.name""")) != "(No data)":
                query = f"""
                    MATCH (a:{start_node_type}), (b:{end_node_type})
                    WHERE a.name = "{start_node_name}" AND b.name = "{end_node_name}"
                    MERGE (a)-[:{relation_type}]->(b)
                    """
                self.graph.run(query)
        else:
            query = f"""
            MATCH (a:{start_node_type}), (b:{end_node_type})
            WHERE a.name = "{start_node_name}" AND b.name = "{end_node_name}"
            MERGE (a)-[:{relation_type}]->(b)
            """
            x = self.graph.run(query)
            print(query)

    def create_all_graph_edges(self):
        for index, row in self.dataset.iterrows():
            disease = row['name']
            for att in self.attribute_matching.keys():
                prop = row[att]
                for node in prop:
                    self.create_relationship(
                        start_node_type="disease",
                        end_node_type=self.attribute_matching[att][0],
                        start_node_name=disease,
                        end_node_name=node,
                        relation_type=self.attribute_matching[att][1]
                    )

    def build_graph(self):
        print("BUILDING KNOWLEDGE GRAPH STARTED")

        # print("PREVIOUS DATA DELETION STARTED")
        # start = time.time()
        # self.delete_all_nodes()
        # end = time.time()
        # print("PREVIOUS DATA DELETION FINISHED, TIME TAKEN ", end-start, " seconds")

        print("GRAPH NODES CREATION STARTED")
        start = time.time()
        self.create_all_graph_nodes()
        end = time.time()
        print("GRAPH NODES CREATION FINISHED, TIME TAKEN ", end   -start, " seconds")

        print("GRAPH EDGE CREATION STARTED")
        start = time.time()
        self.create_all_graph_edges()
        end = time.time()
        print("GRAPH EDGE CREATION FINISHED, TIME TAKEN ", end-start, " seconds")
        print("KNOWLEDGE GRAPH CREATED")

# obj = KnowledgeGraph()
# obj.build_graph()


import MDAnalysis as mda
import os
import sys
if (sys.version_info[0] < 3) or (sys.version_info[1] <= 4):
    import subprocess32 as subprocess
else:
    import subprocess
import warnings

PACKMOL_INP = 'packmol.inp'  # name of .inp file given to packmol
PACKMOL_STRUCTURE_FILES = 'MDAPMinput{}.pdb'
PACKMOL_OUT = 'output.pdb'

class PackmolError(Exception):
    # if packmol didn't find solution
    pass

class PackmolStructure(object):
    """A Molecule to add to the Packmol system

    Parameters
    ----------
    ag : MDAnalysis.AtomGroup
      a single template molecule for Packmol to use
    number : int
      quantity of this molecule to add to new system
    instructions : list of strings
      list of instructions to Packmol for this molecule
      eg 'inside box 0. 0. 0. 40. 40. 40.'
      each item in the list should be a single line instruction
    """
    def __init__(self, ag, number, instructions):
        self.ag = ag
        self.number = number
        self.instructions = instructions

    def to_packmol_inp(self, index):
        """Create portion of packmol.inp file from this molecule

        Parameters
        ----------
        index : int
          the index of this template molecule within entire system

        Returns
        -------
        output : str
          text to write to Packmol input for this molecule
        """
        output = 'structure {}\n'.format(PACKMOL_STRUCTURE_FILES.format(index))
        output += '  number {}\n'.format(self.number)
        for instruction in self.instructions:
            output += '  ' + instruction + '\n'
        output += 'end structure\n\n'

        return output

    def save_structure(self, index):
        """Save this molecule for Packmol to use

        Parameters
        ----------
        index : int
          the index of this template molecule within entire system
        """
        # we mangle Resnames to keep track of which molecule is which
        # so copy the true names, change, write out, change back to real
        old_resnames = self.ag.residues.resnames.copy()
        self.ag.residues.resnames = 'R{}'.format(index)
        with mda.Writer(PACKMOL_STRUCTURE_FILES.format(index)) as w:
            w.write(self.ag)
        self.ag.residues.resnames = old_resnames

def make_packmol_input(structures, tolerance=None):
    """Convert the call into a Packmol usable input file

    Parameters
    ----------
    structures : list
      list of PackmolStructure objects
    tolerance : float, optional
      minimum distance between molecules, defaults to 2.0
    """
    # Check if all structures are suitable, fix them if needed
    for s in structures:
        if not hasattr(s.ag, 'resnames'):
            s.ag.universe.add_TopologyAttr('resnames')

    if tolerance is None:
        tolerance = 2.0

    with open(PACKMOL_INP, 'w') as out:
        out.write("# autogenerated packmol input\n\n")

        out.write('tolerance {}\n\n'.format(tolerance))
        out.write('filetype pdb\n\n')

        for i, structure in enumerate(structures):
            out.write(structure.to_packmol_inp(i))
            structure.save_structure(i)

        out.write('output {}\n\n'.format(PACKMOL_OUT))

def run_packmol():
    """Run and check that Packmol worked correctly"""
    try:
        p = subprocess.run('packmol < {}'.format(PACKMOL_INP),
                           check=True,
                           shell=True,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE)
    except subprocess.CalledProcessError as e:
        raise ValueError("Packmol failed with errorcode {}"
                         " and stderr: {}".format(e.returncode, e.stderr))
    else:
        with open('packmol.stdout', 'w') as out:
            out.write(p.stdout.decode())

def load_packmol_output():
    """Parse the output of Packmol"""
    return mda.Universe(PACKMOL_OUT, transfer_to_memory=True)

def clean_tempfiles(structures):
    """Delete files generated by MDAPackmol"""
    structure_files = [PACKMOL_STRUCTURE_FILES.format(i) for i in range(len(structures))]

    for f in [PACKMOL_INP, PACKMOL_OUT] + structure_files:
        try:
            os.remove(f)
        except FileNotFoundError:
            pass

def reassign_topology(structures, new):
    """Take Packmol created Universe and add old topology features back in

    Attempts to reassign:
     - types
     - names
     - charges
     - masses
     - bonds
     - angles
     - torsions
     - impropers
     - resnames

    Parameters
    ----------
    structures : list
      list of Packmol structures used to create the Packmol Universe
    new : Universe
      the raw output of Packmol

    Returns
    -------
    new : Universe
      the raw output modified to best match the templates given to it
    """
    index = 0

    bonds = []
    angles = []
    dihedrals = []
    impropers = []

    # add required attributes
    for attr in ['types', 'names', 'charges', 'masses']:
        if any(hasattr(pms.ag, attr) for pms in structures):
            new.add_TopologyAttr(attr)

            if not all(hasattr(pms.ag, attr) for pms in structures):
                warnings.warn("added attribute which not all templates had")

    while index < len(new.atoms):
        # first atom we haven't dealt with yet
        start = new.atoms[index]
        # the resname was altered to give a hint to what template it was from
        template = structures[int(start.resname[1:])].ag
        # grab atomgroup which matches template
        to_change = new.atoms[index:index + len(template.atoms)]

        # Update residue names
        nres = len(template.residues)
        new.residues[start.resindex:start.resindex + nres].resnames = template.residues.resnames

        # atom attributes
        for attr in ['types', 'names', 'charges', 'masses']:
            if hasattr(template.atoms, attr):
                setattr(to_change, attr, getattr(template.atoms, attr))

        # bonds/angles/torsions
        if hasattr(template, 'bonds'):
            bonds.extend((template.bonds.to_indices() + index).tolist())
        if hasattr(template, 'angles'):
            angles.extend((template.angles.to_indices() + index).tolist())
        if hasattr(template, 'dihedrals'):
            dihedrals.extend((template.dihedrals.to_indices() + index).tolist())
        if hasattr(template, 'impropers'):
            impropers.extend((template.impropers.to_indices() + index).tolist())

        # update the index pointer to be on next unknown atom
        index += len(template.atoms)

    if bonds:
        # convert to tuples for hashability
        bonds = [tuple(val) for val in bonds]
        new.add_TopologyAttr('bonds', values=bonds)
    if angles:
        angles = [tuple(val) for val in angles]
        new.add_TopologyAttr('angles', values=angles)
    if dihedrals:
        dihedrals = [tuple(val) for val in dihedrals]
        new.add_TopologyAttr('dihedrals', values=dihedrals)
    if impropers:
        impropers = [tuple(val) for val in impropers]
        new.add_TopologyAttr('impropers', values=impropers)

    return new

def packmol(structures, tolerance=None):
    """"Take molecules and settings and create a larger system

    Parameters
    ----------
    structures : list
      list of PackmolStruture objects
    tolerance : float, optional
      Packmol tolerance, defaults to 2.0

    Returns
    -------
    new : MDAnalysis.Universe
      Universe object of the system created by Packmol
    """
    try:
        make_packmol_input(structures, tolerance=tolerance)

        run_packmol()
    except PackmolError:
        # todo: Deal with error
        new = None
    else:
        new = load_packmol_output()
        reassign_topology(structures, new)
    finally:
        clean_tempfiles(structures)

    return new


# Import some utilities
import numpy as np
import mpi4py.MPI as MPI
import matplotlib.pyplot as plt

# Import ParOpt
from paropt import ParOpt

# Create the rosenbrock function class
class Rosenbrock(ParOpt.Problem):
    def __init__(self):
        # Set the communicator pointer
        self.comm = MPI.COMM_WORLD
        self.nvars = 2
        self.ncon = 0
        self.nwcon = 1
        self.nwblock = 1

        # The design history file
        self.x_hist = []

        # Initialize the base class
        super(Rosenbrock, self).__init__(
            self.comm,
            nvars=self.nvars,
            ncon=self.ncon,
            nwcon=self.nwcon,
            nwblock=self.nwblock,
        )
        return

    def getVarsAndBounds(self, x, lb, ub):
        """Set the values of the bounds"""
        x[:] = -2.0 + np.random.uniform(size=len(x))
        lb[:] = -2.0
        ub[:] = 2.0
        return

    def evalObjCon(self, x):
        """Evaluate the objective and constraint"""
        # Append the point to the solution history
        self.x_hist.append(np.array(x))

        # Evaluate the objective and dense constraints
        fail = 0
        con = np.zeros(1)
        fobj = 100 * (x[1] - x[0] ** 2) ** 2 + (1 - x[0]) ** 2
        return fail, fobj, con

    def evalObjConGradient(self, x, g, A):
        """Evaluate the objective and constraint gradient"""
        fail = 0

        # The objective gradient
        g[0] = 200 * (x[1] - x[0] ** 2) * (-2 * x[0]) - 2 * (1 - x[0])
        g[1] = 200 * (x[1] - x[0] ** 2)

        # Evaluate the dense constraint gradient, if any
        return fail

    def evalSparseCon(self, x, con):
        """Evaluate the sparse constraints"""
        con[0] = x[0] + x[1] + 5.0
        return

    def addSparseJacobian(self, alpha, x, px, con):
        """Compute the Jacobian-vector product con += alpha*J(x)*px"""
        con[0] += alpha * (px[0] + px[1])
        return

    def addSparseJacobianTranspose(self, alpha, x, pz, out):
        """Compute the transpose Jacobian-vector product out += alpha*J^{T}*pz"""
        out[0] += alpha * pz[0]
        out[1] += alpha * pz[0]
        return

    def addSparseInnerProduct(self, alpha, x, c, A):
        """Add the results from the product alpha * J(x)*C*J(x)^{T} to A"""
        A[0] += alpha * (c[0] + c[1])
        return

def plot_it_all(problem):
    """
    Plot a carpet plot with the search histories for steepest descent,
    conjugate gradient and BFGS from the same starting point.
    """

    # Create the data for the carpet plot
    n = 150
    xlow = -4.0
    xhigh = 4.0
    x1 = np.linspace(xlow, xhigh, n)
    r = np.zeros((n, n))

    for j in range(n):
        for i in range(n):
            fail, fobj, con = problem.evalObjCon([x1[i], x1[j]])
            r[j, i] = fobj

    # Assign the contour levels
    levels = np.min(r) + np.linspace(0, 1.0, 75) ** 2 * (np.max(r) - np.min(r))

    # Create the plot
    fig = plt.figure(facecolor="w", figsize=(6.0, 6.0))
    plt.contour(x1, x1, r, levels)

    colours = ["-bo", "-ko", "-co", "-mo", "-yo", "-bx", "-kx", "-cx", "-mx", "-yx"]

    options = {
        "algorithm": "tr",
        "tr_init_size": 0.5,
        "tr_min_size": 1e-6,
        "tr_max_size": 10.0,
        "tr_eta": 0.1,
        "tr_adaptive_gamma_update": True,
        "tr_max_iterations": 200,
    }

    for k in range(len(colours)):
        # Optimize the problem
        problem.x_hist = []

        opt = ParOpt.Optimizer(rosen, options)
        opt.optimize()

        # Copy out the steepest descent points
        sd = np.zeros((2, len(problem.x_hist)))
        for i in range(len(problem.x_hist)):
            sd[0, i] = problem.x_hist[i][0]
            sd[1, i] = problem.x_hist[i][1]

        plt.plot(sd[0, :], sd[1, :], colours[k], label="IP nit:%d" % (sd.shape[1]))
        plt.plot(sd[0, -1], sd[1, -1], "-ro")

    plt.legend()
    plt.axis([xlow, xhigh, xlow, xhigh])
    plt.gca().set_aspect("equal")
    plt.show()

# Create the Rosenbrock problem class
rosen = Rosenbrock()
rosen.checkGradients()

plot_it_all(rosen)


import requests

url = "https://api-uat.kushkipagos.com/analytics/v1/transactions-list"

headers = {
    'Content-Type': "/",
    'Private-Merchant-Id': "064e0baa810a4191bcd23e0c46918f1b"
    }

response = requests.request("GET", url, headers=headers,params={"from": "2022-07-19T13:39:00.836",  "to": "2022-07-29T13:39:00.836" })

print(response.text)


from django.shortcuts import render, redirect
from django.contrib import messages
from dj_messages.forms import *

def home(request):
    if request.method == 'POST':
        fm = StudentRegistrationForm(request.POST)
        if fm.is_valid():
            fm.save()
            #messages.add_message(request,messages.SUCCESS,'User registration successful.')
            messages.success(request,'User registration successful.')
            return redirect('dj-messages')
    else:
        fm = StudentRegistrationForm()
    context = {
        'form': fm
    }
    return render(request, 'dj_messages/messages.html',context)


from django.urls import path
# from django.contrib import admin

from labsoil.views import (
    labsoil_list,
    labsoil_create,
    labsoil_detail,
    labsoil_update,
    labsoil_delete,
    lab_soil_result
    )

app_name = 'labsoil'
urlpatterns = [
    path('', labsoil_list, name='list'),
    path('create/', labsoil_create, name='create'),
    path('(<slug:slug>)/', labsoil_detail, name='detail'),
    path('(<slug:slug>)/edit/', labsoil_update, name='update'),
    path('(<slug:slug>)/delete/', labsoil_delete, name='delete'),
    path('(<slug:slug>)/result/', lab_soil_result, name='result'),

    # path('posts/', "<appname>.views.<function_name>"),
]


from django.shortcuts import render

# Create your views here.
def Employee(request):
    return render(request,'basic.html')
    #render(request,'html file,context = variablename)

# How to inject dynamic content from views to template folder
# DTL : django template librabry     "  {{}} "

# Variables¶
# A variable outputs a value from the context,
# which is a dict-like object mapping keys to values.
#
# Variables are surrounded by {{ and }} like this:

def Student(request):
    stu_dict = {
        'name':'Padma Priya',
        'age': 20,
        'location':'Smartcity'
    }
    return render(request,'stu_Info.html',context=stu_dict)


#Write a program which contains filter(), map() and reduce() in it.
# Python application which contains one list of numbers. List contains the numbers which are accepted from user.
# Filter should filter out all such numbers which greater than or equal to 70 and less than or equal to 90.
# Map function will increase each number by 10.
# Reduce will return product of all that numbers.

from functools import reduce

def Greater(no):
    if no>=70 and no<=90:
        return True
    else:
        return False
def Addition(no):
    return no+10
def Summation(no1,no2):
    return no1+no2

def main():
    arr=[]
    size=int(input("Enter the number of elements:"))
    for i in range(size):
        no=int(input(f"Enter the element {i+1}:"))
        arr.append(no)
    print("The list is:",arr)

    FilterList=list(filter(Greater,arr))
    print("The output after filter is:",FilterList)

    MapList = list(map(Addition, FilterList))
    print("The output after map is:", MapList)

    output = reduce(Summation, MapList)
    print("The output after reduce is:", output)

if __name__=="__main__":
    main()

4 Python_Programs/Programs to demo python concepts/filter_map_reuduce_ass1.py


import contextlib
import itertools
import sqlite3
from contextlib import closing
from flask import g

DATABASE = "users.db"

def init_db():
    tasks = [
        "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)",
        "CREATE TABLE IF NOT EXISTS steps (id INTEGER PRIMARY KEY, user_id INTEGER, day INTEGER, value INTEGER, "
        "UNIQUE(user_id, day) ON CONFLICT REPLACE, FOREIGN KEY (user_id) REFERENCES users(id))",
        "CREATE TABLE IF NOT EXISTS anxieties (id INTEGER PRIMARY KEY, user_id INTEGER, timestamp INTEGER, value INTEGER, "
        "UNIQUE(user_id, timestamp) ON CONFLICT REPLACE, FOREIGN KEY (user_id) REFERENCES users(id))",
        "CREATE TABLE IF NOT EXISTS heartrates (id INTEGER PRIMARY KEY, user_id INTEGER, timestamp INTEGER, value INTEGER, "
        "UNIQUE(user_id, timestamp) ON CONFLICT REPLACE, FOREIGN KEY (user_id) REFERENCES users(id))",
    ]

    with closing(sqlite3.connect(DATABASE)) as db:
        execute_multiple_tasks(db, tasks, itertools.repeat((), len(tasks)))

def get_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

# INSERT
def execute_single_task_no_return(db, command, param):
    execute_multiple_tasks_no_return(db, [command], [param])

def execute_multiple_tasks_no_return(db, commands, params):
    with contextlib.closing(db.cursor()) as cursor:
        for command, param in zip(commands, params):
            cursor.execute(
                command,
                param,
            )
        db.commit()

# SELECT
def execute_single_task(db, command, param):
    return execute_multiple_tasks(db, [command], [param])[0]

def execute_multiple_tasks(db, commands, params):
    with contextlib.closing(db.cursor()) as cursor:
        return list(
            map(
                lambda command_param_pair: cursor.execute(
                    command_param_pair[0], command_param_pair[1]
                ).fetchall(),
                zip(commands, params),
            )
        )


from typing import Optional, Sequence

from linebot.v3 import messaging

from ..utils import find_indexes
from .emoji import Emoji

class TextMessage(messaging.TextMessage):
    """
    https://developers.line.biz/en/reference/messaging-api/#text-message
    """

    def __init__(
        self,
        text: str,
        *,
        quick_reply: Optional[messaging.QuickReply] = None,
        emojis: Optional[Sequence[Emoji]] = None,
        quote_token: Optional[str] = None,
    ) -> None:
        if len(text) > 5000:
            raise ValueError("text must be less than or equal to 5000 characters")

        line_emojis: Optional[Sequence[messaging.Emoji]] = None
        if emojis:
            line_emojis = []
            indexes = find_indexes(text, "$")
            for i, emoji in enumerate(emojis):
                line_emojis.append(
                    messaging.Emoji(
                        index=indexes[i],
                        productId=emoji.product_id,
                        emojiId=emoji.emoji_id,
                    )
                )

        super().__init__(
            text=text,
            quickReply=quick_reply,
            emojis=line_emojis,
            quoteToken=quote_token,
        )

class TemplateMessage(messaging.TemplateMessage):
    """
    https://developers.line.biz/en/reference/messaging-api/#template-messages
    """

    def __init__(
        self,
        alt_text: str,
        *,
        template: messaging.Template,
        quick_reply: Optional[messaging.QuickReply] = None,
    ) -> None:
        if len(alt_text) > 400:
            raise ValueError("altText must be less than or equal to 400 characters")

        super().__init__(altText=alt_text, template=template, quickReply=quick_reply)  # type: ignore

class ImageMessage(messaging.ImageMessage):
    """
    https://developers.line.biz/en/reference/messaging-api/#image-message
    """

    def __init__(
        self,
        original_content_url: str,
        preview_image_url: str,
        *,
        quick_reply: Optional[messaging.QuickReply] = None,
    ) -> None:
        if len(original_content_url) > 2000:
            raise ValueError(
                "originalContentUrl must be less than or equal to 2000 characters"
            )
        if len(preview_image_url) > 2000:
            raise ValueError(
                "previewImageUrl must be less than or equal to 2000 characters"
            )

        super().__init__(
            originalContentUrl=original_content_url,
            previewImageUrl=preview_image_url,
            quickReply=quick_reply,
        )


#!/usr/bin/env python3
import firebase_admin
import datetime
from firebase_admin import credentials, db
from firebase_admin import firestore

from chair_calculations import chair_analysis

last_read_timestamps = {'SonicWaves-C-001': 0, 'SonicWaves-C-002':0, 'SonicWaves-C-003':0,
                        'SonicWaves-T-001': 0, 'SonicWaves-T-002':0, 'SonicWaves-D-001': 0}

# Used for global access to firebase client
db_pointer = None

def init_firestore():
    # Using a service account credentials to verify the user
    cred = credentials.Certificate('./iot-app-3386d-firebase-adminsdk-notbj-6b009a7f9d.json')
    firebase_admin.initialize_app(cred, {'databaseURL': 'https://iot-app-3386d.firebaseio.com'})

def filter_sort_data(documents, last_timestamp, device_name):
    data = []
    largest_timestamp = datetime.datetime(year=datetime.MINYEAR, month=1, day=1)
    for document in documents:
    # for document in documents:
        timestamp = document.id
        doc = document.to_dict()
        data_datetime = parse_datetime(timestamp)
        if data_datetime >= last_timestamp:
            if data_datetime > largest_timestamp:
                largest_timestamp = data_datetime
            data.append((doc, data_datetime))
    last_read_timestamps[device_name] = largest_timestamp
    data = sorted(data, key=lambda x: x[1])
    data = [x[0] for x in data]
    print("Working")
    return data

def get_firebase_data(db, name):
    if '-C-' in name:
        ref = db.collection('chair_data').document(name)
    elif '-T-' in name:
        ref = db.collection('table_data').document(name)
    elif '-D-' in name:
        ref = db.collection('door_data').document(name)
    else:
        print("Did not recognize name of change")
        return
    docs = ref.get()
    print(docs)
    doc = docs.to_dict()
    print(doc)
    # print(docs.toString())
    data = []
    if not docs.to_dict() == {}:
        data = filter_sort_data(docs, last_read_timestamps[name], name)
    return data

def setFirebaseData():
        doc_ref = db.collection(u'data-visual').document(u'room_occupancy')
        doc_ref.set({
            'current_occupants': 4
        })

def calculate_chairs():
    filter_values = last_read_timestamps['']
    pass

def calculate_occupancy(timestamp):
    pass

def calculate_chair_position(timestamp):
    pass

def chair_snapshot(chairs, changes, readTime):
    print("Chair Triggered!")
    current_used_chairs = 0
    chair_history = {}
    for doc in changes:
        print('In Snapshot {}'.format(doc.document.id))
        chair_data = get_firebase_data(db_pointer, doc.document.id)
        current_flag, chair_usage = chair_analysis(chair_data)
        if current_flag:
            current_used_chairs+=1
        else:
            current_used_chairs-=1
        chair_history["chair_id"] = chair_usage #TODO add in chair id

    return (current_used_chairs, chair_history)
    # TODO call data_visual to update the values there

def table_snapshot(tables, changes, readTime):
    print("Tables Triggered!")
    for doc in tables:
        print('{}'.format(doc.id))

def door_snapshot(doors, changes, readTime):
    print("Door Triggered!")
    person_count = 0
    for doc in doors:
        print('{}'.format(doc.id))
        # make a count of the latest people in the room
        # TODO need to pull the values in data_visual and update them with this new count
        door_data = get_firebase_data(db_pointer, doc.document.id)
        if door_data['activated']:
            person_count+=1
        else:
            person_count-=1

def make_timestamp_pairs():
    pass

def update_averages(value_dictionary, weeks=True):
    if weeks:
        collection_names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
    else:
        collection_names = []
        for i in range(0, 25):
            if i == 24:
                break
            time = str(i) + '-' + str(i+1)
            collection_names.append(time)
        collection_names.append('24-0')
    for i in collection_names:
        for document_name in ['average_chair_count', 'average_missing_chairs', 'average_table_count', 'total_occupancy']:
            collection = realtime_db.collection('data-visual').document('average_week').collection(i).document(
                document_name)
            values = collection.get().to_dict()
            average_count = values[document_name]
            num_updates = values['num_updates']
            total_count = average_count * num_updates
            total_count += value_dictionary[document_name]
            new_average = total_count / num_updates + 1
            collection.set({document_name: new_average, 'num_updates': num_updates + 1})

if __name__ == '__main__':
    init_firestore()

    db_pointer = firestore.client()
    db_pointer.collection('chair_data').document('SonicWaves-C-001').collection('detections').on_snapshot(chair_snapshot)
    db_pointer.collection('chair_data').document('SonicWaves-C-002').collection('detections').on_snapshot(chair_snapshot)
    db_pointer.collection('chair_data').document('SonicWaves-C-003').collection('detections').on_snapshot(chair_snapshot)

    door_callback = db_pointer.collection('door_data').on_snapshot(door_snapshot)
    table_callback = db_pointer.collection('table_data').on_snapshot(table_snapshot)

    while True:
        continue
    # update_averages(value_dictionary={}, weeks=True)

    # getFirebaseData()
    # setFirebaseData()


from dodge.components import Position, Projectile, Actor, RetaliatoryDeath, DamageBonus, Attacker, Renderable
from dodge.components.component import Component
from dodge.event import Event
from dodge.constants import ComponentType, EventType, EventParam
from dodge.entity import Entity
import uuid

class Weapon(Component):
    def __init__(self, event_stack, projectile_name, path, power, speed, targeting_radius, render_info, cooldown=0):
        super().__init__(component_type=ComponentType.WEAPON,
                         target_events=[EventType.FIRE_ALL],
                         emittable_events=[EventType.ADD_TO_LEVEL],
                         event_stack=event_stack)
        self.projectile_name = projectile_name
        self.path = path
        self.power = power
        self.speed = speed
        self.targeting_range = targeting_radius
        self.render_info = render_info
        self.cooldown = cooldown

    def _target_nearest(self, position, target_faction, level):
        nearby_entities = level.get_entities_in_radius(position.x, position.y, self.targeting_range)
        targets = []
        for entity in nearby_entities:
            if entity.has_component(ComponentType.FACTION) and \
                            entity.get_component(ComponentType.FACTION).faction == target_faction and \
                            entity.has_component(ComponentType.DESTRUCTIBLE):
                targets.append(entity)
        targets.sort(key=position.distance_to)

        if targets:
            target_pos = targets[0].get_component(ComponentType.POSITION)
            return target_pos.x, target_pos.y
        else:
            return None, None

    def _build_projectile(self, shooter_pos, tx, ty):
        path = self.path.build_path(shooter_pos.x, shooter_pos.y, tx, ty)
        projectile = Entity(uuid.uuid4(), self.projectile_name,
                            [RetaliatoryDeath(self._event_stack),
                             Actor(self._event_stack, self.speed),
                             DamageBonus(self.power),
                             Projectile(path, self._event_stack),
                             Position(self._event_stack, shooter_pos.x, shooter_pos.y, False),
                             Attacker(self._event_stack),
                             Renderable.build_from_info(self.render_info)])
        return projectile

    def _handle_event(self, event: Event):
        shooter = event[EventParam.HANDLER]
        shooter_pos = shooter.get_component(ComponentType.POSITION)
        level = event[EventParam.LEVEL]

        (tx, ty) = self._target_nearest(shooter_pos, event[EventParam.FACTION], level)
        if tx is None:
            return event
        else:
            projectile = self._build_projectile(shooter_pos, tx, ty)
            self.emit_event(Event(EventType.ADD_TO_LEVEL, {EventParam.LEVEL: level,
                                                           EventParam.TARGET: projectile,
                                                           EventParam.TAKES_TURN_IMMEDIATELY: True}))
            return event


from django.urls import re_path, reverse_lazy
from django.views.generic import RedirectView

from pages import views

urlpatterns = [
    re_path(r"^home/", views.HomePage.as_view(), name="home"),
    re_path(
        r"^msg/(?P<msg>.+)/$", views.SendMessageView.as_view(), name="send-message"
    ),
    re_path(r"", RedirectView.as_view(url=reverse_lazy("home"))),
]


import numpy as np
import pandas as pd
import scipy.spatial
from sklearn.preprocessing import normalize

# read data
raw_data = pd.read_csv('data/tracks.csv')
data = np.array(raw_data[['id', 'name', 'artists', 'popularity', 'duration_ms', 'danceability', 'instrumentalness', 'energy', 'valence', 'acousticness', 'tempo', 'liveness', 'loudness', 'speechiness']])

def normalize_data(data_in):
    for i in range(3, np.shape(data_in)[1]):
        data_min = np.min(data_in[:,i])
        data_max = np.max(data_in[:,i])
        first_part = data_in[:,:i]
        second_part = np.array([( data_in[:,i] + data_min ) / (np.max(data_in[:,i]) + data_min)]).T
        data_a = np.concatenate((first_part, second_part), axis=1)
        data_in = np.concatenate((data_a, data_in[:,i+1:]), axis=1)
    return data_in

data = normalize_data(data)
data = np.concatenate((data[:,:3], data[:,3:]), axis=1)
song_names_lower = np.char.lower(np.array(data[:,1], dtype=str))
visited = set()

# Given a song ID, get the index of the song in data
def getIndexFromId(newData, songId):
    return np.argwhere(newData[:,0] == songId)[0][0]

# Given a song name, get the index of the song in data
def getIndexFromName(newData, songName):
    return np.argwhere(newData[:,1] == songName)[0][0]

# Given a song name, get the id of the song in data
def getIdFromName(songName):
    return data[getIndexFromName(data, songName)][0]

# Given a song ID, get the k most similar songs
def getKSimilarSongs(songName, k, filters):
    songIndex = getIndexFromName(data, songName)
    songId, artists = data[songIndex][0], data[songIndex][2].strip('][\'').split(', ')
    root = [[songId, songName, artists, 0.0, None]]
    visited.clear()
    visited.add(songId)
    newData = data
    for i in range(len(filters)):
        low, high = getRange(filters[i])
        if low is not None:
            newData = newData[low <= newData[:,i+3]]
            newData = newData[newData[:,i+3] <= high]
    return root + getKSimilarSongsHelper(songId, k, 0, newData)

def getRange(filter_val):
    if filter_val == "Any":
        return [None,None]
    elif filter_val == "Low" or filter_val == "Short":
        return [0.0,0.33]
    elif filter_val == "Medium":
        return [0.33,0.67]
    elif filter_val == "High" or filter_val == "Long":
        return [0.67,1.0]

def getKSimilarSongsHelper(songId, k, similarity, newData):
    # create new array with input song
    inputSong = np.expand_dims(data[getIndexFromId(data, songId)], axis=0)

    # get distance between input song and all other songs
    # note that the first column (id) is cut off since it is a string
    diff = scipy.spatial.distance.cdist(inputSong[:,3:], newData[:,3:], metric='euclidean')[0]

    # get k most similar indexes of songs
    kMostSimlarIndexes = np.argpartition(diff, k)[1:k+1]

    # return array containing k pairs of [songId, songName, artists, similarityRating, parentId]
    songs = (np.column_stack((newData[kMostSimlarIndexes][:,0],
                              newData[kMostSimlarIndexes][:,1],
                              newData[kMostSimlarIndexes][:,2],
                              diff[kMostSimlarIndexes],
                              [songId]*len(kMostSimlarIndexes)))).tolist()

    newSongs = []
    for song in songs:
        if song[3] != 0.0 and song[0] not in visited: # if similarity is 0, it's the same song so filter it out
            visited.add(song[0])
            song[3] += similarity # add parents similarity to root
            song[2] = song[2].strip('][\'').split(', ') # parse string representation of list of artists
            newSongs.append(song)

            # recursively add more songs with half as many songs at each step
            newK = k // 2
            if newK > 1:
                newSongs += getKSimilarSongsHelper(song[0], newK, song[3], newData) # add children

    return newSongs

def finishSongName(partialName):
    partialName = partialName.lower()
    length = len(partialName)
    sliced_data = slicer_vectorized(song_names_lower, 0, length)
    indices = np.argwhere(sliced_data == partialName)
    song_names = data[indices][:,0][:,1][:8]
    return song_names.tolist()

def slicer_vectorized(a,start,end):
    b = a.view((str,1)).reshape(len(a),-1)[:,start:end]
    return np.array(b).view((str,end-start)).flatten()

# print(finishSongName("car"))
# sample input:
# print(getKSimilarSongs("Easy Living (with Teddy Wilson & His Orchestra)", 4))


import torch
import torch.nn.functional as F
import numpy as np
from torch import nn
from torchvision import models

from .base import accuracy, time_distributed, time_distributed_apply, load_network
from .framework import Framework
from .lstm import SeqPredFramework

def stack_frames(frames, reverse=False):
    '''
    frames : [time_step, batch_size, channels, ...]
    output : [time_step - 1, batch_size, channels * 2, ...]
    '''
    T = frames.size(0)
    if reverse:
        return torch.cat([frames.narrow(0, 1, T - 1), frames.narrow(0, 0, T - 1)], dim=2)
    else:
        return torch.cat([frames.narrow(0, 0, T - 1), frames.narrow(0, 1, T - 1)], dim=2)

class StackCL(nn.Module):
    def __init__(self, classes, lstm_units, sync_bn=False, load_lstm=None, load_backbone=None):
        super().__init__()
        resnet = models.resnet50(pretrained=False)
        resnet.conv1 = nn.Conv2d(3 * 2, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
        in_planes = resnet.fc.in_features
        resnet.fc = nn.Sequential()
        if load_backbone is not None:
            load_network(resnet, load_backbone, 'module.backbone.')
        if sync_bn:
            print('Convert model using sync bn')
            resnet = convert_model(resnet)
        self.backbone = resnet

        self.lstm = nn.LSTM(in_planes, lstm_units)
        if load_lstm is not None:
            load_network(self.lstm, load_lstm, 'module.lstm.')
        self.fc = nn.Linear(lstm_units, classes)

    def forward(self, frames):
        self.lstm.flatten_parameters()
        frames = stack_frames(frames)
        features = time_distributed(self.backbone, frames)
        features, _ = self.lstm(features)
        return time_distributed(self.fc, features)

class StackCLOrder(nn.Module):
    def __init__(self, lstm_units, sync_bn=False, load_lstm=None, load_backbone=None):
        super().__init__()
        resnet = models.resnet50(pretrained=False)
        resnet.conv1 = nn.Conv2d(3 * 2, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
        in_planes = resnet.fc.in_features
        resnet.fc = nn.Sequential()
        if load_backbone is not None:
            load_network(resnet, load_backbone, 'module.backbone.')
        if sync_bn:
            print('Convert model using sync bn')
            resnet = convert_model(resnet)
        self.backbone = resnet

        self.lstm = nn.LSTM(in_planes, lstm_units)
        self.fc = nn.Sequential(
            nn.Linear(in_planes, lstm_units))
        self.predict_mlp = nn.Sequential(
            nn.Linear(lstm_units * 2, lstm_units * 2),
            nn.ReLU(),
            nn.Linear(lstm_units * 2, 2)
            )

        self.register_buffer('label', torch.LongTensor([1, 0]))

    def forward(self, orig_frames):
        self.lstm.flatten_parameters()
        # stack sequence with length - 1
        frames = stack_frames(orig_frames)
        T, batch_size = frames.size()[:2]
        features = time_distributed(self.backbone, frames)
        lstm_features, _ = self.lstm(features.narrow(0, 0, T - 1))

        frames_r = stack_frames(orig_frames, reverse=True).narrow(0, 1, T - 1) # remove first pair
        features_r = time_distributed_apply(frames_r, self.backbone, self.fc)
        features = time_distributed_apply(features.narrow(0, 1, T - 1), self.fc) # remove first pair

        T = T - 1
        lstm_features = lstm_features.repeat(1, 1, 2).view(T * batch_size * 2, -1)
        diff_features = torch.cat([features, features_r], dim = -1).view(T * batch_size * 2, -1) # P, N, P, N, ...
        concat_features = torch.cat([lstm_features, diff_features], dim = -1)
        logits = self.predict_mlp(concat_features).view(T, batch_size * 2, 2)
        labels = self.label.repeat(T, batch_size, 1).view(T, batch_size * 2)

        return logits, labels

class StackCLPredFramework(SeqPredFramework):
    def build_network(self):
        self.model = StackCLOrder(**self.config['network'])

def stackcl_framework(config):
    if config['framework'] == 'stackcl-order':
        return StackCLPredFramework(config)


from __future__ import print_function, division

import importlib
import os
import sys
from argparse import ArgumentParser

import settings
import regression

def parseArguments(argv=None):
    default_msg = ' [default %(default)s]'

    parser = ArgumentParser(
        description='Help run regression tests with given testplan name and test level(s)'
    )
    parser.add_argument(
        'testplan',
        help='testplan name'
    )
    parser.add_argument(
        'levels',
        type=str,
        help='test level(s), eg. 0 or 0,1,3'
    )
    parser.add_argument(
        '-P', '--project',
        type=str, default='dla',
        help='Set project name' + default_msg
    )
    parser.add_argument(
        '--os',
        choices=['linux'], default='linux',
        help='Choose device OS' + default_msg
    )
    parser.add_argument(
        '--target',
        choices=['sim', 'ufpga'], default='sim',
        help='Set the target' + default_msg
    )
    parser.add_argument(
        '--noclean',
        action='store_true', default=False,
        help="Don't clean output directory when test completes [default to clean]"
    )
    parser.add_argument(
        '--show',
        action='store_true', default=False,
        help="Fork test output to stdout" + default_msg
    )
    parser.add_argument(
        '-d', '--debug',
        type=int, default=0,
        help="Set debug level" + default_msg
    )
    parser.add_argument(
        '-l', '--list',
        action='store_true', default=False,
        help="Print tests in testlist"
    )
    parser.add_argument(
        '--force', dest='forceTests',
        action='store_true', default=False,
        help="Force running staged tests"
    )
    parser.add_argument(
        '--testhome',
        type=str, default=settings.TEST_HOME,
        help="Path to find test files" + default_msg
    )
    parser.add_argument(
        '--goldhome',
        type=str, default=settings.GOLD_HOME,
        help="Path to find gold files" + default_msg
    )

    filter_group = parser.add_argument_group(
        title='Filter options',
        description='Options to help run tests selectively'
    )
    filter_group.add_argument(
        '-f', '--filter',
        type=str, default=None,
        help="Apply a match string to filter tests"
    )
    filter_group.add_argument(
        '-r', '--rfilter',
        type=str, default=None,
        help="Apply a regular expression to filter tests"
    )

    save_group = parser.add_argument_group()
    save_group.add_argument(
        '--savelead',
        action='store_true', default=False,
        help='Update lead files with test results if they mismatch'
    )
    save_group.add_argument(
        '--savegold',
        action='store_true', default=False,
        help='Update gold files with test results if they mismatch'
    )

    options = parser.parse_args(argv)
    return options

def setup_env(options):
    if 'DLA_TOP' not in os.environ:
        os.environ['DLA_TOP'] = settings.DLA_HOME
    sys.path.insert(0, settings.TESTPLAN_HOME)

    if 'OUT' not in os.environ:
        os.environ['OUT'] = settings.OUTPUT_DIR

def list_tests(options, testplan):
    """ List tests in given testplan with given level num"""
    for level in options.levels.split(','):
        testlist = testplan.get_testlist(
            level,
            options.target,
            options.filter,
            options.rfilter
        )
        regression.list_tests(testlist, options)

def deploy_tests(options):
    """Resolve testing dependency on target running env

    KUM/firmware: always run in host mode
    UMD: from device or host
    """
    pass

def run_tests(options, testplan):
    """Run all given tests, and list result stats"""
    deploy_tests(options)

    target = options.target

    for level in options.levels.split(','):
        print("Running testplan[%s] level[%s] [%s] tests"
              % (testplan.name, level, target))
        testlist = testplan.get_testlist(
            level,
            target,
            options.filter,
            options.rfilter
        )

        # Execute testlist
        results = regression.run_tests(testlist, options)

        # List result stats
        written = testplan.num_written(level, target)
        total = testplan.num_total(level, target)
        regression.list_results(results, written, total)

def main():
    options = parseArguments()

    # set environment vars
    setup_env(options)

    # obtain testplan object
    mod = importlib.import_module(options.testplan)
    testplan = mod.Module()

    testplan.register_tests()

    if options.list:
        list_tests(options, testplan)
    else:
        run_tests(options, testplan)

if __name__ == "__main__":
    main()


from django.contrib import admin
from .models import UserTypeMaster,UserMaster

# class UserAdmin(admin.ModelAdmin):
# 	list_display=['UserName','UserPassword']
# 	list_filter=['UserName','UserEmail']
# 	search_fields=['UserPassword','UserEmail']
# 	ordering=['UserPassword','UserName']

ModelField= lambda model: type('Subclass'+model.__name__,(admin.ModelAdmin,),{
	'list_display':[x.name for x in model._meta.fields],
})

# Register your models here.

admin.site.register(UserTypeMaster,ModelField(UserTypeMaster))
admin.site.register(UserMaster,ModelField(UserMaster))


import pytest
from sos import execute_workflow
from sos.targets import file_target

def test_py_module(clear_now_and_after):
    '''Test target Py_Module'''
    clear_now_and_after('report.md')
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate', autoinstall=True)
from tabulate import tabulate
table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
    ["Moon",1737,73.5],["Mars",3390,641.85]]
report: output='report.md', expand=True
    {tabulate(table)}
''')
    assert file_target('report.md').target_exists('target')
    with open('report.md') as rep:
        assert rep.read().strip() == '''
-----  ------  -------------
Sun    696000     1.9891e+09
Earth    6371  5973.6
Moon     1737    73.5
Mars     3390   641.85
-----  ------  -------------
'''.strip()

def test_py_module_with_version():
    '''Test target Py_Module'''
    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate', version='2.0', autoinstall=True)
''')

        #
    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate>=2.0')
''')
        #

    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate==20.0')
''')
        #
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate<2.0')
''')

def test_upgrade_py_module():
    '''Test upgrade py module #1246'''
    # first install tabulate == 0.7.5
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate==0.7.5', autoinstall=True)
''')
    # test should pass
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate'), Py_Module('tabulate==0.7.5')
''')
    # test for newer version should fail

    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate==0.8.3')
''')

    # auto install should work
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate==0.8.3', autoinstall=True)
''')

    # test for old version should fail
    execute_workflow(r'''
        [10]
        depends: Py_Module('tabulate'), Py_Module('tabulate==0.8.3')
        ''')
    # test for old version should fail
    with pytest.raises(Exception):
        execute_workflow(r'''
        [10]
        depends: Py_Module('tabulate==0.7.5')
        ''')


#!/usr/bin/env python3

__license__ = 'MIT'

import argparse
import json
import hashlib
import os
import subprocess
import tempfile
import urllib.request
import shlex
from collections import OrderedDict

parser = argparse.ArgumentParser()
parser.add_argument('packages', nargs='+')
parser.add_argument('--python2', action='store_true',
                    help='Look for a Python 2 package')
parser.add_argument('--cleanup', choices=['scripts', 'all'],
                    help='Select what to clean up after build')
parser.add_argument('--build-only', action='store_const',
                    dest='cleanup', const='all',
                    help='Clean up all files after build')
parser.add_argument('--output',
                    help='Specify output file name')
opts = parser.parse_args()

def get_pypi_url(name: str, filename: str) -> str:
    url = 'https://pypi.python.org/pypi/{}/json'.format(name)
    print('Extracting download url for', name)
    with urllib.request.urlopen(url) as response:
        body = json.loads(response.read().decode('utf-8'))
        for release in body['releases'].values():
            for source in release:
                if source['filename'] == filename:
                    return source['url']
        else:
            raise Exception('Failed to extract url from {}'.format(url))

def get_file_hash(filename: str) -> str:
    sha = hashlib.sha256()
    print('Generating hash for', filename)
    with open(filename, 'rb') as f:
        while True:
            data = f.read(1024 * 1024 * 32)
            if not data:
                break
            sha.update(data)
        return sha.hexdigest()

package_name = 'python{}-{}'.format('2' if opts.python2 else '3',
                                    opts.packages[0])
tempdir_prefix = 'pip-generator-{}-'.format(package_name)

with tempfile.TemporaryDirectory(prefix=tempdir_prefix) as tempdir:
    if opts.python2:
        pip_executable = 'pip2'
        pip_install_prefix = '--install-option="--prefix=${FLATPAK_DEST}"'
    else:
        pip_executable = 'pip3'
        pip_install_prefix = '--prefix=${FLATPAK_DEST}'

    pip_download = [
        pip_executable,
        'download',
        '--dest',
        tempdir
    ]

    subprocess.run(pip_download + [
        '--no-binary', ':all:',
    ] + opts.packages, check=True)

    pip_command = [
        pip_executable,
        'install',
        '--no-index',
        '--find-links="file://${PWD}"',
        pip_install_prefix,
    ] + [
        shlex.quote(package)
        for package in opts.packages
    ]

    main_module = OrderedDict([
        ('name', package_name),
        ('buildsystem', 'simple'),
        ('build-commands', [' '.join(pip_command)]),
        ('sources', []),
    ])

    if opts.cleanup == 'all':
        main_module['cleanup'] = ['*']
    elif opts.cleanup == 'scripts':
        main_module['cleanup'] = ['/bin', '/share/man/man1']

    for filename in os.listdir(tempdir):
        name = filename.rsplit('-', 1)[0]
        if name == 'setuptools':  # Already installed
            continue
        sha256 = get_file_hash(os.path.join(tempdir, filename))
        url = get_pypi_url(name, filename)
        source = OrderedDict([
            ('type', 'file'),
            ('url', url),
            ('sha256', sha256),
        ])
        main_module['sources'].append(source)

output_filename = opts.output or package_name + '.json'
with open(output_filename, 'w') as output:
    output.write(json.dumps(main_module, indent=4))


import os
import re
import torch
import math
import numpy as np

import torchvision.utils as vutils

from PIL import Image, ImageDraw, ImageFont
from utils.checkpoints import Checkpoint

try: # backward compatibility
    from tensorboardX import SummaryWriter
except ImportError:
    from torch.utils.tensorboard import SummaryWriter

from core.config import cfg, cfg_from_file, cfg_from_list

class BaseTrainer(object):

    def __del__(self):
        # commented out, because hangs on exit
        # (presumably some bug with threading in TensorboardX)
        """
        if not self.quiet:
            self.writer.close()
            self.writer_val.close()
        """
        pass

    def __init__(self, args, quiet=False):
        self.args = args
        self.quiet = quiet

        # config
        # Reading the config
        if type(args.cfg_file) is str \
                and os.path.isfile(args.cfg_file):

            cfg_from_file(args.cfg_file)
            if args.set_cfgs is not None:
                cfg_from_list(args.set_cfgs)

        self.start_epoch = 0
        self.best_score = -1e16
        self.checkpoint = Checkpoint(args.snapshot_dir, max_n = 5)

        if not quiet:
            #self.model_id = "%s" % args.run
            logdir = os.path.join(args.logdir, 'train')
            logdir_val = os.path.join(args.logdir, 'val')

            self.writer = SummaryWriter(logdir)
            self.writer_val = SummaryWriter(logdir_val)

    def _define_checkpoint(self, name, model, optim):
        self.checkpoint.add_model(name, model, optim)

    def _load_checkpoint(self, suffix):
        if self.checkpoint.load(suffix):
            # loading the epoch and the best score
            tmpl = re.compile("^e(\d+)Xs([\.\d+\-]+)$")
            match = tmpl.match(suffix)
            if not match:
                print("Warning: epoch and score could not be recovered")
                return
            else:
                epoch, score = match.groups()
                self.start_epoch = int(epoch) + 1
                self.best_score = float(score)

    def checkpoint_epoch(self, score, epoch):

        if score > self.best_score:
            self.best_score = score

        print(">>> Saving checkpoint with score {:3.2e}, epoch {}".format(score, epoch))
        suffix = "e{:03d}Xs{:4.3f}".format(epoch, score)
        self.checkpoint.checkpoint(suffix)

        return True

    def checkpoint_best(self, score, epoch):

        if score > self.best_score:
            print(">>> Saving checkpoint with score {:3.2e}, epoch {}".format(score, epoch))
            self.best_score= score

            suffix = "e{:03d}Xs{:4.3f}".format(epoch, score)
            self.checkpoint.checkpoint(suffix)

            return True

        return False

    @staticmethod
    def get_optim(params, cfg):

        if not hasattr(torch.optim, cfg.OPT):
            print("Optimiser {} not supported".format(cfg.OPT))
            raise NotImplementedError

        optim = getattr(torch.optim, cfg.OPT)

        if cfg.OPT == 'Adam':
            upd = torch.optim.Adam(params, lr=cfg.LR, \
                                   betas=(cfg.BETA1, 0.999), \
                                   weight_decay=cfg.WEIGHT_DECAY)
        elif cfg.OPT == 'SGD':
            print("Using SGD >>> learning rate = {:4.3e}, momentum = {:4.3e}, weight decay = {:4.3e}".format(cfg.LR, cfg.MOMENTUM, cfg.WEIGHT_DECAY))
            upd = torch.optim.SGD(params, lr=cfg.LR, \
                                  momentum=cfg.MOMENTUM, \
                                  weight_decay=cfg.WEIGHT_DECAY)

        else:
            upd = optim(params, lr=cfg.LR)

        upd.zero_grad()

        return upd

    @staticmethod
    def set_lr(optim, lr):
        for param_group in optim.param_groups:
            param_group['lr'] = lr

    def _visualise_grid(self, x_all, labels, t, ious=None, tag="visualisation", scores=None):

        # adding the labels to images
        bs, ch, h, w = x_all.size()
        x_all_new = torch.zeros(bs, ch, h + 16, w)
        _, y_labels_idx = torch.max(labels, -1)
        classNamesOffset = len(self.classNames) - labels.size(1)
        classNames = self.classNames[classNamesOffset:]
        for b in range(bs):
            label_idx = labels[b]
            label_names = [name for i,name in enumerate(classNames) if label_idx[i].item()]
            label_name = ", ".join(label_names)

            ndarr = x_all[b].mul(255).clamp(0, 255).byte().permute(1, 2, 0).cpu().numpy()
            arr = np.zeros((16, w, ch), dtype=ndarr.dtype)
            ndarr = np.concatenate((arr, ndarr), 0)
            im = Image.fromarray(ndarr)
            draw = ImageDraw.Draw(im)

            font = ImageFont.truetype("fonts/UbuntuMono-R.ttf", 12)

            # draw.text((x, y),"Sample Text",(r,g,b))
            draw.text((5, 1), label_name, (255,255,255), font=font)
            im_np = np.array(im).astype(np.float)
            x_all_new[b] = (torch.from_numpy(im_np)/255.0).permute(2,0,1)

        summary_grid = vutils.make_grid(x_all_new, nrow=1, padding=8, pad_value=0.9)
        self.writer.add_image(tag, summary_grid, t)

    def _apply_cmap(self, mask_idx, mask_conf):
        palette = self.trainloader.dataset.get_palette()

        masks = []
        col = Colorize()
        mask_conf = mask_conf.float() / 255.0
        for mask, conf in zip(mask_idx.split(1), mask_conf.split(1)):
            m = col(mask).float()
            m = m * conf
            masks.append(m[None, ...])

        return torch.cat(masks, 0)

    def _mask_rgb(self, masks, image_norm, alpha=0.3):
        # visualising masks
        masks_conf, masks_idx = torch.max(masks, 1)
        masks_conf = masks_conf - F.relu(masks_conf - 1, 0)

        masks_idx_rgb = self._apply_cmap(masks_idx.cpu(), masks_conf.cpu())
        return alpha * image_norm + (1 - alpha) * masks_idx_rgb

    def _init_norm(self):
        self.trainloader.dataset.set_norm(self.enc.normalize)
        self.valloader.dataset.set_norm(self.enc.normalize)
        self.trainloader_val.dataset.set_norm(self.enc.normalize)


import bpy
from bpy.types import PropertyGroup
from bpy.props import BoolProperty, IntVectorProperty, StringProperty, IntProperty, EnumProperty
from .get_slot_info import get_slot_info
from .image_is_visible import image_is_visible
from .ImagePropertiesAccessors import *
from .multiple_image_objects import multiple_image_objects
from .all_objects_have_slots import all_objects_have_slots
from ..Common import image_object
from .get_or_make_animation_pattern import get_or_make_animation_pattern
default_animation_pattern = 'f"filename{iz(3)}.png"'

def set_visible(o, value):
    # type: (bpy.EmptyImageObjectTypeWithData, bool) -> None
    o.show_empty_image_orthographic = value
    o.keyframe_insert(
        "show_empty_image_orthographic",
        frame=bpy.context.scene.frame_current
    )

def set_is_a_slot(o, value):
    # type: (bpy.EmptyImageObjectTypeWithData, bool) -> None
    if not value:
        o["font_is_a_slot"] = False
        return
    o["font_is_a_slot"] = True
    if "font_slot_number" not in o:
        o['font_slot_number'] = 0
    if "font_slot_name" not in o:
        o['font_slot_name'] = ""

class ImageProperties(PropertyGroup):
    """Property for editing images"""

    def get_number_of_objects(self):
        # type: () -> int
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return 0
        return len(objects)

    def set_number_of_objects(self, value):
        # type: (int) -> None
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return
        num_objects = len(objects)
        if num_objects == value:
            return
        if num_objects < value:
            number_to_create = value - num_objects
            last_object = objects[num_objects - 1]
            for _ in range(number_to_create):
                o = bpy.data.objects.new(
                    name=last_object.name,
                    object_data=None
                )
                bpy.context.view_layer.active_layer_collection.collection.objects.link(
                    o)
                o.data = last_object.data
                o.empty_display_type = "IMAGE"
                o.parent = last_object.parent
                o.location[0] = last_object.location[0]
                o.location[2] = last_object.location[2]
                o.select_set(True)
            return
        for o in objects[value:]:
            o.parent = None
            o.select_set(False)
            bpy.context.view_layer.active_layer_collection.collection.objects.unlink(o)

    animation_number_of_objects: IntProperty(
        name="Number of Images",
        description="The number of images in this animation",
        min=2,
        get=get_number_of_objects,
        set=set_number_of_objects
    )

    animation_show_all_preview: BoolProperty(
        name="Preview all selected names",
        description="Preview all selected names",
        default=True
    )

    animation_number_of_preview_name: IntProperty(
        name="Number of images to preview",
        description="Number of images to preview",
        min=0,
        default=1
    )

    animation_types = [
        ("INCREMENT", "Increment", "Load files counting upwards", 1),
        ("DECREMENT", "Decrement", "Load files counting downwards", 2),
        ("PATTERN", "Pattern",
         "Load files in a pattern", 3),
        ("PYTHON", "Python", "Load files in a pattern given a python script", 4)
    ]

    def get_animation_pattern_type(self):
        # type: () -> int
        o = image_object(bpy.context.active_object)
        if o is None or "font_animation_pattern_info" not in o:
            return 1
        type = o["font_animation_pattern_info"]["type"]
        if type == "INCREMENT":
            return 1
        if type == "DECREMENT":
            return 2
        if type == "PATTERN":
            return 3
        if type == "PYTHON":
            return 4
        return 1

    def set_animation_pattern_type(self, value):
        # type: (int) -> None
        o = image_object(bpy.context.active_object)
        if o is None:
            return
        pattern = get_or_make_animation_pattern(o)

        if value == 1:
            pattern["type"] = "INCREMENT"
        elif value == 2:
            pattern["type"] = "DECREMENT"
        elif value == 3:
            pattern["type"] = "PATTERN"
        elif value == 4:
            pattern["type"] = "PYTHON"

    animation_pattern_type: EnumProperty(
        items=animation_types,
        name="Pattern Type",
        get=get_animation_pattern_type,
        set=set_animation_pattern_type)

    animation_num_digits: IntProperty(
        name="Number of Digits",
        get=lambda s: pattern_get_prop("num_digits", 2),
        set=lambda s, v: pattern_set_prop("num_digits", v)
    )

    animation_start_number: IntProperty(
        name="First Number",
        min=0,
        get=lambda s: pattern_get_prop("start_number", 0),
        set=lambda s, v: pattern_set_prop("start_number", v)
    )
    animation_end_number: IntProperty(
        name="Last Number",
        min=0,
        get=lambda s: pattern_get_prop("end_number", 0),
        set=lambda s, v: pattern_set_prop("end_number", v)
    )

    animation_wrap_around: BoolProperty(
        name="Wrap Around",
        description="After you go above end number, start again from the beginning",
        get=lambda s: pattern_get_prop("wrap_around", True),
        set=lambda s, v: pattern_set_prop("wrap_around", v)
    )

    animation_file_name: StringProperty(
        name="Filename pattern",
        description="The name of the file, don't include the number at the end. or the extension Example: if your file is image_000, then type in image_",
        get=lambda s: pattern_get_prop("filename", ""),
        set=lambda s, v: pattern_set_prop("filename", v)
    )
    animation_file_extension: StringProperty(
        name="File extension",
        description="The name of the file extension. Please include the .beforehand, like .png",
        get=lambda s: pattern_get_prop("extension", ".png"),
        set=lambda s, v: pattern_set_prop("extension", v)
    )

    animation_python_snippet: StringProperty(
        name="Python Snippet",
        description="A python expression to return the filename given the index i (starting at zero).\n Look at the docs for the list of helper functions",
        get=lambda s: pattern_get_prop(
            "python", ""),
        set=lambda s, v: pattern_set_prop("python", v)
    )

    animation_pattern_size: IntProperty(
        name="Pattern Size",
        min=1,
        max=14,
        get=lambda s: pattern_get_prop("pattern_size", 1),
        set=lambda s, v: pattern_set_prop("pattern_size", v)
    )

    animation_pattern1: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=1,
        get=lambda s: get_pattern_of_length(1),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern2: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=2,
        get=lambda s: get_pattern_of_length(2),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern3: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=3,
        get=lambda s: get_pattern_of_length(3),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern4: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=4,
        get=lambda s: get_pattern_of_length(4),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern5: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=5,
        get=lambda s: get_pattern_of_length(5),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern6: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=6,
        get=lambda s: get_pattern_of_length(6),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern7: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=7,
        get=lambda s: get_pattern_of_length(7),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern8: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=8,
        get=lambda s: get_pattern_of_length(8),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern9: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=9,
        get=lambda s: get_pattern_of_length(9),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern10: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=10,
        get=lambda s: get_pattern_of_length(10),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern11: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=11,
        get=lambda s: get_pattern_of_length(11),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern12: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=12,
        get=lambda s: get_pattern_of_length(12),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern13: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=13,
        get=lambda s: get_pattern_of_length(13),
        set=lambda s, v: set_pattern_of_length(v),
    )
    animation_pattern14: IntVectorProperty(
        name="Pattern",
        min=0,
        description="A pattern of numbers like [0,3,2] will load filename00, filename03, filename02, filename00, filename003 and so on",
        size=14,
        get=lambda s: get_pattern_of_length(14),
        set=lambda s, v: set_pattern_of_length(v),
    )

    animation_key_before: BoolProperty(
        name="Invisible before",
        get=lambda s: get_prop("font_animation_key_before", True),
        set=lambda s, v: set_prop("font_animation_key_before", v),
        description="Add a key, setting all frames to invisible, before the animation"
    )
    animation_key_after: BoolProperty(
        name="Invisible After",
        get=lambda s: get_prop("font_animation_key_after", True),
        set=lambda s, v: set_prop("font_animation_key_after", v),
        description="Add a key, setting all frames to invisible, after the animation"
    )

    animation_repeat_times: IntProperty(
        name="Number Of Times",
        min=1,
        description="Play the animation this number of times. If you put 1, it will play once.",
        get=lambda s: get_prop("font_animation_repeat_times", 1),
        set=lambda s, v: set_prop("font_animation_repeat_times", v),
    )

    animation_use_current_frame: BoolProperty(
        name="Use Current Frame",
        description="Use the current frame as the start of the animation",
        get=lambda s: get_prop(
            "font_animation_use_current_frame", default=True),
        set=lambda s, v: set_prop("font_animation_use_current_frame", v)
    )

    animation_frame_number: IntProperty(
        name="Frame Number",
        description="The frame number to start the animation",
        get=lambda s: get_prop(
            "font_animation_start_frame", 0),
        set=lambda s, v: set_prop("font_animation_start_frame", v)
    )

    slot_name: StringProperty(
        name="Slot Name",
        description="(Optional) The name of the slot, only for your convenience.\nThis name will appear in the scene tree editor",
        get=lambda s: get_prop("font_slot_name", ""),
        set=lambda s, v: set_prop(
            "font_slot_name", v)
    )

    slot_number: IntProperty(
        name="Slot Number",
        description="All slots that share this slot number will be replaced by the same image",
        get=lambda s: get_prop("font_slot_number", 0),
        set=lambda s, v: set_prop("font_slot_number", v)
    )

    def get_is_slot(self):
        # type: () -> bool
        o = image_object(bpy.context.active_object)
        if o is None:
            return False
        return get_slot_info(o) is not None

    def set_is_slot(self, value):
        # type: (bool) -> None
        o = image_object(bpy.context.active_object)
        if o is None:
            return
        set_is_a_slot(o, value=value)

    is_slot: BoolProperty(
        name="Slot",
        description="If this is image is a slot, then the template image will be replaced by 'slot' image when you export the scene",
        get=get_is_slot,
        set=set_is_slot)

    def get_is_visible(self):
        # type: () -> bool
        o = image_object(bpy.context.active_object)
        if o is None:
            return False
        return image_is_visible(o, bpy.context.scene.frame_current)

    def set_is_visible(self, value):
        # type: (bool) -> None
        o = image_object(bpy.context.active_object)
        if o is None:
            return
        set_visible(o, value=value)

    is_visible: BoolProperty(
        name="Visible",
        description="If the image is visible or invisible at this frame.",
        get=get_is_visible,
        set=set_is_visible)

    def get_all_are_visible(self):
        # type: () -> bool
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return False
        for o in objects:
            if not image_is_visible(o, bpy.context.scene.frame_current):
                return False
        return True

    def set_all_are_visible(self, value):
        # type: (bool) -> None
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return
        for o in objects:
            set_visible(o, value=value)

    all_visible: BoolProperty(
        name="All Visible",
        description="If all selected images are visible.",
        get=get_all_are_visible,
        set=set_all_are_visible)

    def get_all_are_slots(self):
        # type: () -> bool
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return False
        return all_objects_have_slots(objects)

    def set_all_are_slots(self, value):
        # type: (bool) -> None
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return
        for o in objects:
            set_is_a_slot(o, value=value)

    all_are_slots: BoolProperty(
        name="Are all slots",
        description="If these is images are slots, then the template image will be replaced by 'slot' image when you export the scene",
        get=get_all_are_slots,
        set=set_all_are_slots)

    def get_all_slot_numbers(self):
        # type: () -> int
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return -1
        slot_num = -1
        for o in objects:
            slot_info = get_slot_info(o)
            if slot_info is None:
                return -1
            if slot_info.number == slot_num:
                continue
            if slot_num != -1:
                return -1
            slot_num = slot_info.number
        return slot_num

    def set_all_slot_numbers(self, value):
        # type: (int) -> None
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return
        for o in objects:
            o["font_slot_number"] = value

    all_slot_numbers: IntProperty(
        name="All Slot Numbers (-1 if they disagree)",
        description="All slots that share this slot number will be replaced by the same image",
        get=get_all_slot_numbers,
        set=set_all_slot_numbers
    )

    def get_all_slot_names(self):
        # type: () -> str
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return "No names"
        names = {o['font_slot_name']
                 for o in objects if 'font_slot_name' in o and o['font_slot_name'] != ""}
        if len(names) <= 0:
            return "No names"
        return " & ".join(names)

    def set_all_slot_names(self, value):
        # type: (str) -> None
        objects = multiple_image_objects(bpy.context)
        if objects is None:
            return
        for o in objects:
            o["font_slot_name"] = value

    all_slot_names: StringProperty(
        name="All Slot Names",
        description="(Optional) The name of the slot, only for your convenience.\nThis name will appear in the scene tree editor",
        get=get_all_slot_names,
        set=set_all_slot_names
    )

    all_slot_start: IntProperty(name="Starting at slot number", default=0)
    all_slot_name_pattern: StringProperty(
        name="Pattern",
        description="A python expression to return the filename given the index i (starting at zero).",
        default="f\"slot{i}\""
    )


# Copyright (c) OpenMMLab. All rights reserved.

import torch
import torch.nn as nn
import torch.nn.functional as F
from mmcv.cnn import ConvModule

from ..builder import BACKBONES
from ..builder import HEADS
from mmcv.cnn import build_norm_layer
from mmcv.runner import BaseModule
from mmseg.ops import resize

class QKVBlock(nn.Module):
    def __init__(self, in_channels):
        super(QKVBlock, self).__init__()
        self.conv_q = ConvModule(in_channels, in_channels, 1, norm_cfg=None, act_cfg=None)
        self.conv_k = ConvModule(in_channels, in_channels, 1, norm_cfg=None, act_cfg=None)
        self.conv_v = ConvModule(in_channels, in_channels, 1, norm_cfg=None, act_cfg=None)
        self.softmax = nn.Softmax(dim=-1)

    def forward(self, x):
        N, C, H, W = x.shape

        x_q = self.conv_q(x).reshape(N, C, H*W).permute(0, 2, 1).contiguous()
        x_k = self.conv_k(x).reshape(N, C, H*W)
        x_v = self.conv_v(x).reshape(N, C, H*W).permute(0, 2, 1).contiguous()

        att = self.softmax(torch.bmm(x_q, x_k))

        return x_v, att

class MCAM(nn.Module):
    def __init__(self, in_channels):
        super(MCAM, self).__init__()
        self.qkv_opt = QKVBlock(in_channels)
        self.qkv_sar = QKVBlock(in_channels)

    def forward(self, x_opt, x_sar):

        N, C, H, W = x_opt.shape

        v_opt, att_opt = self.qkv_opt(x_opt)
        v_sar, att_sar = self.qkv_sar(x_sar)
        att = att_opt * att_sar

        s_opt = torch.bmm(att, v_opt).permute(0, 2, 1).contiguous().reshape(N, C, H, W)
        s_sar = torch.bmm(att, v_sar).permute(0, 2, 1).contiguous().reshape(N, C, H, W)

        out = s_opt * s_sar
        return out

'''
Copied from mmseg.models.decode_heads.aspp_head
'''
class ASPPModule(nn.ModuleList):
    """Atrous Spatial Pyramid Pooling (ASPP) Module.

    Args:
        dilations (tuple[int]): Dilation rate of each layer.
        in_channels (int): Input channels.
        channels (int): Channels after modules, before conv_seg.
        conv_cfg (dict|None): Config of conv layers.
        norm_cfg (dict|None): Config of norm layers.
        act_cfg (dict): Config of activation layers.
    """

    def __init__(self, dilations, in_channels, channels, conv_cfg, norm_cfg,
                 act_cfg):
        super(ASPPModule, self).__init__()
        self.dilations = dilations
        self.in_channels = in_channels
        self.channels = channels
        self.conv_cfg = conv_cfg
        self.norm_cfg = norm_cfg
        self.act_cfg = act_cfg
        for dilation in dilations:
            self.append(
                ConvModule(
                    self.in_channels,
                    self.channels,
                    1 if dilation == 1 else 3,
                    dilation=dilation,
                    padding=0 if dilation == 1 else dilation,
                    conv_cfg=self.conv_cfg,
                    norm_cfg=self.norm_cfg,
                    act_cfg=self.act_cfg))

    def forward(self, x):
        """Forward function."""
        aspp_outs = []
        for aspp_module in self:
            aspp_outs.append(aspp_module(x))

        return aspp_outs

class ASPP(nn.Module):
    def __init__(self, dilations, in_channels, channels, conv_cfg, norm_cfg, act_cfg):
        super(ASPP, self).__init__()
        self.image_pool = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            ConvModule(
                in_channels,
                channels,
                1,
                conv_cfg=conv_cfg,
                norm_cfg=norm_cfg,
                act_cfg=act_cfg))
        self.aspp_modules = ASPPModule(dilations, in_channels, channels, conv_cfg, norm_cfg, act_cfg)
        self.bottleneck = ConvModule(
            (len(dilations) + 1) * channels,
            channels,
            3,
            padding=1,
            conv_cfg=conv_cfg,
            norm_cfg=norm_cfg,
            act_cfg=act_cfg)

    def forward(self, x):
        """Forward function."""

        aspp_outs = [
            resize(
                self.image_pool(x),
                size=x.size()[2:],
                mode='bilinear',
                align_corners=False)
        ]
        aspp_outs.extend(self.aspp_modules(x))
        aspp_outs = torch.cat(aspp_outs, dim=1)
        output = self.bottleneck(aspp_outs)
        return output

@BACKBONES.register_module()
class MCANet(BaseModule):
    def __init__(self,
                 enc_dims=[256, 2048],
                 backbone_opt_cfg=dict(
                     type='ResNetV1c',
                     depth=50,
                     in_channels=4,
                     num_stages=4,
                     out_indices=(0, 3),
                     dilations=(1, 1, 2, 4),
                     strides=(1, 2, 1, 1),
                     norm_cfg=dict(type='BN', requires_grad=True),
                     norm_eval=False,
                     style='pytorch',
                     contract_dilation=True,
                     pretrained='open-mmlab://resnet50_v1c'
                 ),
                 backbone_sar_cfg=dict(
                     type='ResNetV1c',
                     depth=50,
                     in_channels=1,
                     num_stages=4,
                     out_indices=(0, 3),
                     dilations=(1, 1, 2, 4),
                     strides=(1, 2, 1, 1),
                     norm_cfg=dict(type='BN', requires_grad=True),
                     norm_eval=False,
                     style='pytorch',
                     contract_dilation=True,
                     pretrained='open-mmlab://resnet50_v1c'
                 ),
                 norm_cfg=dict(type='BN')):
        super(MCANet, self).__init__()

        self.backbone_opt = BACKBONES.build(backbone_opt_cfg)
        self.backbone_sar = BACKBONES.build(backbone_sar_cfg)

        self.mcam_low = MCAM(enc_dims[0])
        self.conv_fuse_low = ConvModule(enc_dims[0]*3, 48, 1, norm_cfg=norm_cfg)

        self.mcam_high = MCAM(enc_dims[1])
        self.conv_opt_high = ConvModule(enc_dims[1], 256, 1, norm_cfg=norm_cfg)
        self.conv_sar_high = ConvModule(enc_dims[1], 256, 1, norm_cfg=norm_cfg)
        self.aspp = ASPP(dilations=(1, 6, 12, 18),
                         in_channels=enc_dims[1]+2*256,
                         channels=256,
                         conv_cfg=None,
                         norm_cfg=norm_cfg,
                         act_cfg=dict(type='ReLU'))

    def forward(self, x):
        # fetch inputs
        x_opt, x_sar = torch.split(x, (4, 1), dim=1)

        # pseudo-siamese feature extraction
        x_opt_low, x_opt_high = self.backbone_opt(x_opt)
        x_sar_low, x_sar_high = self.backbone_sar(x_sar)

        # multimodal-cross attention
        x_fuse_low = self.mcam_low(x_opt_low, x_sar_low)
        x_fuse_low = torch.cat([x_fuse_low, x_opt_low, x_sar_low], dim=1)
        x_fuse_low = self.conv_fuse_low(x_fuse_low)
        low_h, low_w = x_fuse_low.size(2), x_fuse_low.size(3)

        x_fuse_high = self.mcam_high(x_opt_high, x_sar_high)
        x_fuse_high = torch.cat([x_fuse_high, self.conv_opt_high(x_opt_high), self.conv_sar_high(x_sar_high)], dim=1)
        x_fuse_high = self.aspp(x_fuse_high)
        x_fuse_high = F.interpolate(x_fuse_high, size=(int(low_h), int(low_w)), mode='bilinear')
        # decoder
        x_merge = torch.cat([x_fuse_high, x_fuse_low], dim=1)
        return (x_merge, )