#a executer depuis le repertoire ou se trouve le script (exercice_BOW_Camille)

from glob import glob
import os
import random
# from scripts.Evaluation import Evaluation
from PredictBOW_cam_V3 import *
#from Evaluation import Evaluation
import spacy
predicted = []
expected = []

nlp=spacy.load('en_core_web_sm')
# from Predict import Predict

import pandas as pd
import numpy as np
from nltk.tokenize import word_tokenize
from nltk import pos_tag
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer
from sklearn.preprocessing import LabelEncoder
from collections import defaultdict
from nltk.corpus import wordnet as wn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import model_selection, naive_bayes, svm
from sklearn.metrics import accuracy_score, classification_report

def pipeline(fic):
    corpus=pd.read_csv(fic,sep='\t')
    corpus['text']=[elem.lower() for elem in corpus['text']]
    return corpus
    #print(corpus.head())
    #Train_X, Test_X, Train_Y, Test_Y = model_selection.train_test_split(corpus['text'],corpus['label'],test_size=0.2)
    #Encoder = LabelEncoder()
    #Train_Y = Encoder.fit_transform(Train_Y)

    #Test_Y = Encoder.fit_transform(Test_Y)
    #print(Train_Y)
    #print(Test_Y)

def files2csv(text,out,label):

   texte_clean=re.sub(r'(<[^>]*>)|\(|\)|\n|\t',' ',text)
   with open(out, 'a') as file:
       file.write(texte_clean+"\t"+label+"\n")

def getFile(fic,pol):
    x = 0
    for f in glob(fic+'/*') :
        x += 1
        if x >= 2000 :
            os.system(f'mv {f} /home/schaub/Téléchargements/aclImdb_v1/aclImdb/test/{pol}/')

        if x == 8000 :
            break

def getcontentlabel(file) :

    expect = ''
    if 'pos/' in file :
        expect = 'pos'
    else :
        expect = 'neg'

    return open(file).read(),expect

def prediction(file,expect) :

    pred = Predict(file, pw, nw, nlp)
    pred.predict()
    predicted.append(pred.predicted)
    expected.append(expect)

if __name__ == '__main__':

    #with open("./csv_test.csv", 'w') as file:
        #file.write("text\tlabel\n")

    #for fic in random.sample(glob('../../../imdb_test/*/*.txt'),len(glob('../../../imdb_test/*/*.txt'))) :
        #text,label=getcontentlabel(fic)
        #files2csv(text,"./csv_test.csv",label)

    corpus=CorpusCSV(pipeline("./csv_test.csv"))
    print(corpus.voc)
    corpus.getBOW()
    print(corpus.bow)
    print(corpus.bigram)
    print(corpus.trigram)
    corpus.getTFIDF_voc()
    print(corpus.tfidf_voc)
    corpus.getTFIDF_bigram()
    print(corpus.tfidf_bigram)
    corpus.getTFIDF_trigram()
    print(corpus.tfidf_trigram)
    print("ca marche ! bravo")


# Implementation and visualization of BOLL Bands (+- 2std dev)
import numpy as np
from discountedAverage import DiscountedAveragerator
from dataCollector import DataCollector
import matplotlib.pyplot as plt

# The moving average algorithm
class BollBands():
    def __init__(self, data, times, alpha):
        self.data = data
        self.averagerator = DiscountedAveragerator(alpha=alpha)
        self.upper = []
        self.lower = []
        self.average = []
        self.iArr = [0]
        self.times = times

    def algo(self):
        # Compare against data[0]
        self.averagerator.add(self.data[0])
        self.average.append(self.averagerator.avg)
        self.upper.append(self.averagerator.avg + self.times * self.averagerator.std)
        self.lower.append(self.averagerator.avg - self.times * self.averagerator.std)

        # Initialize variables
        bought = False
        i = 1
        n = len(self.data)

        while (i < n-1):
            curr = self.data[i]
            # calc new average
            self.averagerator.add(curr)
            average = self.averagerator.avg
            deviation = self.averagerator.std
            self.average.append(average)
            self.upper.append(average + self.times * deviation)
            self.lower.append(average - self.times * deviation)
            self.iArr.append(i)
            i += 1
        return

    def graph(self):
        plt.plot(self.data, color='0')                                    # plot data
        plt.plot(self.average, color='m', ls='dashed')                    # plot average
        plt.plot(self.upper, color='m')                      # plot upper band
        plt.plot(self.lower, color='m')                      # plot lower band
        plt.fill_between(x=self.iArr, y1=self.upper, y2=self.lower, color='c')
        plt.show()

if __name__ == '__main__':
    # Company and Investment Info
    info = {
        'company': 'AMZN',
        'period' : '1d',
        'interval': '1m',
        'invest': 10000
    }

    # Collect data
    collector = DataCollector(ticker=info['company'], period=info['period'], interval=info['interval'])
    collector.gather()

    # Run Algo & Print Stats
    boll = BollBands(data=collector.data, times=2, alpha=0.8)
    boll.algo()
    boll.graph()


# -*- coding: utf-8 -*-
"""
Created on Tue Mar 16 12:38:04 2021

@author: Manu Bhardwaj
"""
a = int(input())
if a > 1:
    for i in range(2 , a):
        if a % i == 0:
            print(a, " is not prime")
            break
    else:
        print(a ," is prime")
else:
    print(a, "is not prime")


"""Helper functionality to read and write ``.tfrecord`` files."""
import tensorflow as tf

"""
Mapping of type identifiers (numeric) to TensorFlow types.
"""
parse_tensor_type_lookup = {
    1: tf.float32,
    2: tf.float64,
    3: tf.int32,
    4: tf.uint8,
    5: tf.int16,
    6: tf.int8,
    8: tf.complex64,
    9: tf.int64,
    10: tf.bool,
    14: tf.bfloat16,
    17: tf.uint16,
    18: tf.complex128,
    19: tf.float16,
}

@tf.function
def get_tensor_magic_number(tensor):
    """
    Return the type identifier of a serialized TensorFlow tensor proto.

    :param tensor:
    :return:
    """
    magic = tf.reshape(tf.io.decode_raw(tf.strings.substr(tensor, 1, 1), tf.uint8), [])
    return magic

@tf.function
def parse_tensor(tensor, coerce):
    """
    Parse a binary tensor containing a serialized tensor proto to a Tensor.

    Coercing it into a specific type.

    :param tensor:
    :param coerce:
    :return:
    """
    magic = get_tensor_magic_number(tensor)
    arguments = []
    for n in range(0, max(parse_tensor_type_lookup.keys()) + 1):
        if n in parse_tensor_type_lookup:
            dtype = parse_tensor_type_lookup[n]
            arguments.append(
                (n, lambda: tf.cast(tf.io.parse_tensor(tensor, dtype), coerce))
            )
        else:
            arguments.append((n, lambda: tf.zeros(0, dtype=coerce)))

    return tf.switch_case(tf.cast(magic, tf.int32), arguments)

def create_example(x, y):
    """
    Create an ``tf.train.Example`` with JUNN-convention contents.

    :param x:
    :param y:
    :return:
    """

    def _bytes_feature(value):
        if isinstance(value, type(tf.constant(0))):
            value = value.numpy()
        return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))

    return tf.train.Example(
        features=tf.train.Features(
            feature=dict(
                x=_bytes_feature(tf.io.serialize_tensor(x)),
                y=_bytes_feature(tf.io.serialize_tensor(y)),
            )
        )
    ).SerializeToString()

def read_junn_tfrecord(file_name):
    """
    Read a ``.tfrecord`` file following JUNN convention and yields Dataset.

    :param file_name:
    :return:
    """
    feature_description = dict(
        x=tf.io.FixedLenFeature([], tf.string), y=tf.io.FixedLenFeature([], tf.string)
    )

    @tf.function
    def _parse(example_proto):
        return tf.io.parse_single_example(example_proto, feature_description)

    # sad but true: we need to peek into the dataset to get the dtype
    first = next(iter(tf.data.TFRecordDataset(file_name).map(_parse)))

    x_magic_number, y_magic_number = (
        get_tensor_magic_number(first['x']),
        get_tensor_magic_number(first['y']),
    )
    x_type, y_type = (
        parse_tensor_type_lookup[x_magic_number.numpy()],
        parse_tensor_type_lookup[y_magic_number.numpy()],
    )

    @tf.function
    def _unserialize(example):
        return tf.io.parse_tensor(example['x'], x_type), tf.io.parse_tensor(
            example['y'], y_type
        )

    raw_dataset = tf.data.TFRecordDataset(file_name)
    dataset = raw_dataset
    dataset = dataset.map(_parse)
    dataset = dataset.map(_unserialize)

    return dataset


import requests
from datetime import datetime, timedelta
def bookRoom():
    email = ["ektakati","hparthas","syedaqhi","arunsure","aaa'"]
    time1 = ["10","12","14","16","18"]
    time2 = ["12","14","16","18","20"]

    todayDate = datetime.now()
    dayAftrTmrw = todayDate + timedelta(days=3)
    dayAftrTmrw = dayAftrTmrw.strftime("%Y-%m-%d")
    print(dayAftrTmrw)

    for idx, val in enumerate(email):
        url = "https://booking.lib.buffalo.edu/ajax/space/book?formData%5Bfname%5D=firstname&formData%5Blname%5D=lastname&formData%5Bemail%5D="+email[idx]+"%40buffalo.edu&forcedEmail=&bookings%5B0%5D%5Bid%5D=4&bookings%5B0%5D%5Beid%5D=19762&bookings%5B0%5D%5Bgid%5D=5235&bookings%5B0%5D%5Blid%5D=3154&bookings%5B0%5D%5Bstart%5D="+dayAftrTmrw+"+"+time1[idx]+"%3A00&bookings%5B0%5D%5Bend%5D="+dayAftrTmrw+"+"+time2[idx]+"%3A00&returnUrl=%2Freserve%2Fsilverman"

        payload = { }
        headers = {
        'Referer': "https://booking.lib.buffalo.edu/reserve/silverman"
        }

        response = requests.request("POST", url, data=payload, headers=headers)

        print(response.text)


from itertools import izip
import os.path # exists

from pyffi.formats.nif import NifFormat
from pyffi.utils import unique_map
import pyffi.utils.tristrip
import pyffi.utils.vertex_cache
import pyffi.spells
import pyffi.spells.nif
import pyffi.spells.nif.fix
import pyffi.spells.nif.modify
from pyffi.spells.nif.optimize import *

# localization
#import gettext
#_ = gettext.translation('pyffi').ugettext
_ = lambda msg: msg # stub, for now

# set flag to overwrite files
__readonly__ = False

# example usage
__examples__ = """* Standard usage:

    python niftoaster.py optimize /path/to/copy/of/my/nifs

* Optimize, but do not merge NiMaterialProperty blocks:

    python niftoaster.py optimize --exclude=NiMaterialProperty /path/to/copy/of/my/nifs
"""

class SpellOptimizeGeometry2(pyffi.spells.nif.NifSpell):
    """Optimize all geometries:
      - remove duplicate vertices
      - triangulate
      - recalculate skin partition
      - recalculate tangent space
    """

    SPELLNAME = "opt_geometry"
    READONLY = False

    # spell parameters
    VERTEXPRECISION = 3
    NORMALPRECISION = 3
    UVPRECISION = 5
    VCOLPRECISION = 3

    def __init__(self, *args, **kwargs):
        pyffi.spells.nif.NifSpell.__init__(self, *args, **kwargs)
        # list of all optimized geometries so far
        # (to avoid optimizing the same geometry twice)
        self.optimized = []

    def datainspect(self):
        # do not optimize if an egm or tri file is detected
        filename = self.stream.name
        if (os.path.exists(filename[:-3] + "egm")
            or os.path.exists(filename[:-3] + "tri")):
            return False
        # so far, only reference lists in NiObjectNET blocks, NiAVObject
        # blocks, and NiNode blocks are checked
        return self.inspectblocktype(NifFormat.NiTriBasedGeom)

    def branchinspect(self, branch):
        # only inspect the NiAVObject branch
        return isinstance(branch, NifFormat.NiAVObject)

    def optimize_vertices(self, data):
        self.toaster.msg("removing duplicate vertices")
        # get map, deleting unused vertices
        return unique_map(
            vhash
            for i, vhash in enumerate(data.get_vertex_hash_generator(
                vertexprecision=self.VERTEXPRECISION,
                normalprecision=self.NORMALPRECISION,
                uvprecision=self.UVPRECISION,
                vcolprecision=self.VCOLPRECISION)))

    def branchentry(self, branch):
        """Optimize a NiTriStrips or NiTriShape block:
          - remove duplicate vertices
          - retriangulate for vertex cache
          - recalculate skin partition
          - recalculate tangent space

        @todo: Limit the length of strips (see operation optimization mod for
            Oblivion!)
        """
        if not isinstance(branch, NifFormat.NiTriBasedGeom):
            # keep recursing
            return True

        if branch in self.optimized:
            # already optimized
            return False

        if branch.data.additional_data:
            # occurs in fallout nv
            # not sure how to deal with additional data
            # so skipping to be on the safe side
            self.toaster.msg(
                "mesh has additional geometry data"
                " which is not well understood: not optimizing")
            return False

        # we found a geometry to optimize

        # we're going to change the data
        self.changed = True

        # cover degenerate case
        if branch.data.num_vertices < 3 or branch.data.num_triangles == 0:
            self.toaster.msg(
                "less than 3 vertices or no triangles: removing branch")
            self.data.replace_global_node(branch, None)
            return False

        self.optimized.append(branch)

        # shortcut
        data = branch.data

        v_map, v_map_inverse = self.optimize_vertices(data)

        self.toaster.msg("(num vertices was %i and is now %i)"
                         % (len(v_map), len(v_map_inverse)))

        # optimizing triangle ordering
        # first, get new triangle indices, with duplicate vertices removed
        triangles = list(pyffi.utils.vertex_cache.get_unique_triangles(
            (v_map[v0], v_map[v1], v_map[v2])
            for v0, v1, v2 in data.get_triangles()))
#        old_atvr = pyffi.utils.vertex_cache.average_transform_to_vertex_ratio(
#            triangles)
#        self.toaster.msg("optimizing triangle ordering")
#        new_triangles = pyffi.utils.vertex_cache.get_cache_optimized_triangles(
#            triangles)
#        new_atvr = pyffi.utils.vertex_cache.average_transform_to_vertex_ratio(
#            new_triangles)
#        if new_atvr < old_atvr:
#            triangles = new_triangles
#            self.toaster.msg(
#                "(ATVR reduced from %.3f to %.3f)" % (old_atvr, new_atvr))
#        else:
#            self.toaster.msg(
#                "(ATVR stable at %.3f)" % old_atvr)
        # optimize triangles to have sequentially ordered indices
        self.toaster.msg("optimizing vertex ordering")
        v_map_opt = pyffi.utils.vertex_cache.get_cache_optimized_vertex_map(
            triangles)
        triangles = [(v_map_opt[v0], v_map_opt[v1], v_map_opt[v2])
                      for v0, v1, v2 in triangles]
        # update vertex map and its inverse
        for i in xrange(data.num_vertices):
            try:
                v_map[i] = v_map_opt[v_map[i]]
            except IndexError:
                # found a trailing vertex which is not used
                v_map[i] = None
            if v_map[i] is not None:
                v_map_inverse[v_map[i]] = i
            else:
                self.toaster.logger.warn("unused vertex")
        try:
            new_numvertices = max(v for v in v_map if v is not None) + 1
        except ValueError:
            # max() arg is an empty sequence
            # this means that there are no vertices
            self.toaster.msg(
                "less than 3 vertices or no triangles: removing branch")
            self.data.replace_global_node(branch, None)
            return False
        del v_map_inverse[new_numvertices:]

        # use a triangle representation
        if not isinstance(branch, NifFormat.NiTriShape):
            self.toaster.msg("replacing branch by NiTriShape")
            newbranch = branch.get_interchangeable_tri_shape(
                triangles=triangles)
            self.data.replace_global_node(branch, newbranch)
            branch = newbranch
            data = newbranch.data
        else:
            data.set_triangles(triangles)

        # copy old data
        oldverts = [(v.x, v.y, v.z) for v in data.vertices]
        oldnorms = [(n.x, n.y, n.z) for n in data.normals]
        olduvs   = [[(uv.u, uv.v) for uv in uvset] for uvset in data.uv_sets]
        oldvcols = [(c.r, c.g, c.b, c.a) for c in data.vertex_colors]
        if branch.skin_instance: # for later
            oldweights = branch.get_vertex_weights()
        # set new data
        data.num_vertices = new_numvertices
        if data.has_vertices:
            data.vertices.update_size()
            for i, v in enumerate(data.vertices):
                old_i = v_map_inverse[i]
                v.x = oldverts[old_i][0]
                v.y = oldverts[old_i][1]
                v.z = oldverts[old_i][2]
        if data.has_normals:
            data.normals.update_size()
            for i, n in enumerate(data.normals):
                old_i = v_map_inverse[i]
                n.x = oldnorms[old_i][0]
                n.y = oldnorms[old_i][1]
                n.z = oldnorms[old_i][2]
        # XXX todo: if ...has_uv_sets...:
        data.uv_sets.update_size()
        for j, uvset in enumerate(data.uv_sets):
            for i, uv in enumerate(uvset):
                old_i = v_map_inverse[i]
                uv.u = olduvs[j][old_i][0]
                uv.v = olduvs[j][old_i][1]
        if data.has_vertex_colors:
            data.vertex_colors.update_size()
            for i, c in enumerate(data.vertex_colors):
                old_i = v_map_inverse[i]
                c.r = oldvcols[old_i][0]
                c.g = oldvcols[old_i][1]
                c.b = oldvcols[old_i][2]
                c.a = oldvcols[old_i][3]
        del oldverts
        del oldnorms
        del olduvs
        del oldvcols

        # update skin data
        if branch.skin_instance:
            self.toaster.msg("update skin data vertex mapping")
            skindata = branch.skin_instance.data
            newweights = []
            for i in xrange(new_numvertices):
                newweights.append(oldweights[v_map_inverse[i]])
            for bonenum, bonedata in enumerate(skindata.bone_list):
                w = []
                for i, weightlist in enumerate(newweights):
                    for bonenum_i, weight_i in weightlist:
                        if bonenum == bonenum_i:
                            w.append((i, weight_i))
                bonedata.num_vertices = len(w)
                bonedata.vertex_weights.update_size()
                for j, (i, weight_i) in enumerate(w):
                    bonedata.vertex_weights[j].index = i
                    bonedata.vertex_weights[j].weight = weight_i

            # update skin partition (only if branch already exists)
            if branch.get_skin_partition():
                self.toaster.msg("updating skin partition")
                if isinstance(branch.skin_instance,
                              NifFormat.BSDismemberSkinInstance):
                    # get body part indices (in the old system!)
                    triangles, trianglepartmap = (
                        branch.skin_instance.get_dismember_partitions())
                    maximize_bone_sharing = True
                    # update mapping
                    new_triangles = []
                    new_trianglepartmap = []
                    for triangle, trianglepart in izip(triangles, trianglepartmap):
                        new_triangle = tuple(v_map[i] for i in triangle)
                        # it could happen that v_map[i] is None
                        # these triangles are skipped
                        # see for instance
                        # falloutnv/meshes/armor/greatkhans/greatkhan_v3.nif
                        # falloutnv/meshes/armor/tunnelsnake01/m/outfitm.nif
                        if None not in new_triangle:
                            new_triangles.append(new_triangle)
                            new_trianglepartmap.append(trianglepart)
                    triangles = new_triangles
                    trianglepartmap = new_trianglepartmap
                else:
                    # no body parts
                    triangles = None
                    trianglepartmap = None
                    maximize_bone_sharing = False
                # use Oblivion settings
                branch.update_skin_partition(
                    maxbonesperpartition=18, maxbonespervertex=4,
                    stripify=False, verbose=0,
                    triangles=triangles, trianglepartmap=trianglepartmap,
                    maximize_bone_sharing=maximize_bone_sharing)

        # update morph data
        for morphctrl in branch.get_controllers():
            if isinstance(morphctrl, NifFormat.NiGeomMorpherController):
                morphdata = morphctrl.data
                # skip empty morph data
                if not morphdata:
                    continue
                # convert morphs
                self.toaster.msg("updating morphs")
                # check size and fix it if needed
                # (see issue #3395484 reported by rlibiez)
                # remap of morph vertices works only if
                # morph.num_vertices == len(v_map)
                if morphdata.num_vertices != len(v_map):
                    self.toaster.logger.warn(
                        "number of vertices in morph ({0}) does not match"
                        " number of vertices in shape ({1}):"
                        " resizing morph, graphical glitches might result"
                        .format(morphdata.num_vertices, len(v_map)))
                    morphdata.num_vertices = len(v_map)
                    for morph in morphdata.morphs:
                        morph.arg = morphdata.num_vertices # manual argument passing
                        morph.vectors.update_size()
                # now remap morph vertices
                for morph in morphdata.morphs:
                    # store a copy of the old vectors
                    oldmorphvectors = [(vec.x, vec.y, vec.z)
                                       for vec in morph.vectors]
                    for old_i, vec in izip(v_map_inverse, morph.vectors):
                        vec.x = oldmorphvectors[old_i][0]
                        vec.y = oldmorphvectors[old_i][1]
                        vec.z = oldmorphvectors[old_i][2]
                    del oldmorphvectors
                # resize matrices
                morphdata.num_vertices = new_numvertices
                for morph in morphdata.morphs:
                    morph.arg = morphdata.num_vertices # manual argument passing
                    morph.vectors.update_size()

        # recalculate tangent space (only if the branch already exists)
        if (branch.find(block_name='Tangent space (binormal & tangent vectors)',
                        block_type=NifFormat.NiBinaryExtraData)
            or (data.num_uv_sets & 61440)
            or (data.bs_num_uv_sets & 61440)):
            self.toaster.msg("recalculating tangent space")
            branch.update_tangent_space()

        # stop recursion
        return False


provost_section = {
	"code": "provost_section",
	"descr": "Provost",
	"header": "Provost",
	"componentType": "Container",
    "affordanceType":"Section",
	"optional": False,
	"enabled": True,
	"accessPermissions": [],
	"viewPermissions": [],
	"blockers": [],
	"statusMsg": "",
	"className": "Container",
	"containers": ["provost_packet","provost_submit_date","provost_vote"]
}


from performance_log import performance_log
from Timer import Timer

def is_prime(val):
    if val<2: return False
    for i in range(2, val):
        if val%i == 0: return False
    return val

def primes_in_range(min, max=None):
    if not max:
        max = min
        min = 1
    for val in range(min, max+1):
        if is_prime(val):
            yield val

@performance_log
def find_primes(min, max=None):
    return [x for x in primes_in_range(min, max)]

def main():
    # x = find_primes(1000)
    # print(x)
    # print(len(x))

    t = Timer()
    with t:
        p = find_primes(2, 20000)
        print('total primes: ', len(p))
    print('total time taken', t.time_taken)

if __name__ == '__main__':
    main()


import random
from data import rooms, facilitators, time_slots

# Selection
def softmax_selection(population, normalized_scores):
    selected_schedules = []

    while len(selected_schedules) < len(population):
        selected_index = random.choices(range(len(population)), weights=normalized_scores, k=1)[0]
        selected_schedule = population[selected_index]

        # Check if any room in the selected schedule exceeds capacity
        room_usage = {room: 0 for room in rooms}

        valid_schedule = True
        for activity in selected_schedule:
            room = activity['room']
            if room_usage[room] + activity['expected_enrollment'] > rooms[room]:
                valid_schedule = False
                break
            room_usage[room] += activity['expected_enrollment']

        if valid_schedule:
            selected_schedules.append(selected_schedule)

    return selected_schedules

# Crossover
def pmx_crossover(parent1, parent2):
    # Initialize child schedules with the same structure as parents
    child1 = [None] * len(parent1)
    child2 = [None] * len(parent2)

    # Choose two random crossover points
    point1 = random.randint(0, len(parent1) - 2)
    point2 = random.randint(point1 + 1, len(parent1) - 1)

    # Copy the selected portion from parent1 to child1, and from parent2 to child2
    child1[point1:point2 + 1] = parent1[point1:point2 + 1]
    child2[point1:point2 + 1] = parent2[point1:point2 + 1]

    # Fill in the remaining slots in child1 and child2
    for i in range(len(parent1)):
        if i < point1 or i > point2:
            # Find values from parent2 that are not in child1
            while parent2[i] in child1[point1:point2 + 1]:
                i = parent2.index(parent1[i])
            child1[i] = parent2[i]

            # Find values from parent1 that are not in child2
            while parent1[i] in child2[point1:point2 + 1]:
                i = parent1.index(parent2[i])
            child2[i] = parent1[i]

    return child1, child2

# Mutation
def mutate_schedule(schedule):
    mutated_schedule = schedule.copy()

    # Select a random activity to mutate
    activity_index = random.randint(0, len(mutated_schedule) - 1)
    mutated_activity = mutated_schedule[activity_index]

    # Mutate time and facilitator (room assignment remains unchanged)
    mutation_type = random.choice(["time", "facilitator"])

    if mutation_type == "time":
        # Change the time to a random valid time slot
        mutated_schedule[activity_index]['time'] = random.choice(time_slots)
    else:
        # Change the facilitator to a random valid facilitator
        mutated_schedule[activity_index]['facilitator'] = random.choice(facilitators)

    return mutated_schedule


import os

import faiss
import numpy as np

from descriptor_lib.descriptor_group import DescriptorGroup
import config

class FaissEngine(object):

    def __init__(self,
            descriptor_group,
            filename):
        """Descriptor indexing and storage engine powered by Faiss.

        Args:
            descriptor_group: DescriptorGroup, the descriptor_group
                to be indexing or to be stored.
            filename: str, filename for store index file.

        """
        self.descriptor_group = descriptor_group
        self.X = None
        self.index = None
        self.filename = filename

    def _get_X(self):
        """Convert feature of descriptor_group to matrix."""
        if self.X is None:
            dg = self.descriptor_group
            self.X = np.zeros(
                    len(dg), dg.feature_size,
                    dtype=dg.dtype)
            for i, desc in enumerate(dg.description_table.values()):
                self.X[i, :] = desc.feature_vec
        return self.X

    def get_index(self):
        """Get index of """
        if self.index is None:
            self.index = faiss.IndexFlatL2(self._get_X())
        return self.index

    def save_index(self):
        """Save the index file."""
        index = self.get_index()
        path = os.path.join(config.db_feature_index_path, self.filename)
        faiss.write_index(index, path)

    def load_index(self):
        """Load index from file."""
        path = os.path.join(config.db_feature_index_path, self.filename)
        self.index = faiss.read_index(path)

    def k_similar_samples(self, query, k=1):
        """Query the most similar k samples."""
        index = self.get_index()
        D, I = index.search(query, k)
        return D, I


from PyQt5.QtWidgets import QDialog
from src.UI.ui_range_slider_thresholding import UiRangeSliderThresholding
from PyQt5.QtGui import QPixmap, QImage
import cv2

FORMATS = {
    1: QImage.Format_Grayscale8
}

OPERATIONS = {
    'Thresholding': 1,
    'Adaptive Thresholding': 2,
    "Otsu thresholding": 3,
}

def threshold_single_channel(channel, value, adaptive_method):
    _, channel_thresh = cv2.threshold(channel, value, 255, adaptive_method)
    return channel_thresh

def threshold_channels(image, value, adaptive_method):
    b, g, r = cv2.split(image)

class PointOperationThresholding(QDialog, UiRangeSliderThresholding):
    def __init__(self, image_data, gray):
        super().__init__()
        self.setup_ui(self)
        self.setWindowTitle("Thresholding")
        self.pixmap = None
        self.image_data = image_data
        self.image_origin = image_data
        self.gray = gray
        self.fill_combo_boxes()
        self.po_thresholding()
        self.cB_operations.currentIndexChanged.connect(self.po_thresholding)
        self.slider_max.valueChanged.connect(self.po_thresholding)
        #self.cb_block_size.currentIndexChanged.connect(self.po_thresholding)
        self.cb_block_size.valueChanged.connect(self.po_thresholding)

    def fill_combo_boxes(self):
        for key in OPERATIONS.keys():
            self.cB_operations.addItem(key)

    def update_window(self):
        height, width = self.image_data.shape[:2]
        if len(self.image_data.shape) < 3:
            image = QImage(self.image_data, width, height, width, FORMATS[self.image_data.dtype.itemsize])
        else:
            image = QImage(self.image_data, width, height, 3 * width, QImage.Format_BGR888)
        self.pixmap = QPixmap(image)
        self.setFixedSize(self.pixmap.width() + 10, self.pixmap.height() + 30)
        self.label_image.setPixmap(self.pixmap)

    def po_thresholding(self):
        value = self.slider_max.value()
        self.label_max.setText("Threshold: {0}".format(value))
        number_operation = OPERATIONS[self.cB_operations.currentText()]
        if not self.gray:
            b, g, r = cv2.split(self.image_origin)
            if number_operation == 1:
                self.cb_block_size.setEnabled(False)
                self.image_data = cv2.merge((threshold_single_channel(b, value, cv2.THRESH_BINARY),
                                            threshold_single_channel(g, value, cv2.THRESH_BINARY),
                                            threshold_single_channel(r, value, cv2.THRESH_BINARY)))
            elif number_operation == 2:
                self.cb_block_size.setEnabled(True)
                b_size = self.cb_block_size.value()
                if b_size % 2 == 0:
                    b_size = b_size + 1
                    self.cb_block_size.setValue(b_size)
                b_thresh = cv2.adaptiveThreshold(b, value, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, b_size, 5)
                g_thresh = cv2.adaptiveThreshold(g, value, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, b_size, 5)
                r_thresh = cv2.adaptiveThreshold(r, value, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, b_size, 5)
                self.image_data = cv2.merge((b_thresh, g_thresh, r_thresh))
            elif number_operation == 3:
                self.cb_block_size.setEnabled(False)
                _, b_thresh = cv2.threshold(b, 0, value, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                _, g_thresh = cv2.threshold(g, 0, value, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                _, r_thresh = cv2.threshold(r, 0, value, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
                self.image_data = cv2.merge((b_thresh, g_thresh, r_thresh))
        else:
            if number_operation == 1:
                self.cb_block_size.setEnabled(False)
                _, self.image_data = cv2.threshold(self.image_origin, value, 255, cv2.THRESH_BINARY)
            elif number_operation == 2:
                self.cb_block_size.setEnabled(True)
                b_size = self.cb_block_size.value()
                if b_size % 2 == 0:
                    b_size = b_size + 1
                    self.cb_block_size.setValue(b_size)
                self.image_data = cv2.adaptiveThreshold(self.image_origin, value, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                                        cv2.THRESH_BINARY, b_size, 5)
            elif number_operation == 3:
                self.cb_block_size.setEnabled(False)
                _, self.image_data = cv2.threshold(self.image_origin, 0, value, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        self.update_window()


# -*- coding: utf-8 -*-

import sys
import torch
import torch.nn as nn
import torch.nn.functional as F
import random
import numpy as np
#from torch.autograd import Variable
from utils.utils import print_time, lens2mask

class Attention(nn.Module):

    def __init__(self, hidden_size, method, coverage, cuda):
        super(Attention, self).__init__()
        self.method = method
        self.cuda = cuda

        if method == 'dot':
            pass #no need to use a layer

        elif method == 'general':
            self.W_a = nn.Linear(hidden_size, hidden_size, bias=False)
            if cuda: self.W_a.cuda()

        elif method == 'concat':
            self.W_a_h = nn.Linear(hidden_size, hidden_size, bias=False) #linear layer for enc hidden states (enc_output) 'context'
            self.W_a_s = nn.Linear(hidden_size, hidden_size, bias=True)  #linear layer for dec_state (dec_hidden) 'query' (contains bias: b_attn)
            self.v = nn.Linear(hidden_size, 1, bias=False)
            if coverage: self.W_a_c = nn.Linear(1, hidden_size, bias=False) #lineary layer for coverage vector
            if cuda:
                self.W_a_h.cuda()
                self.W_a_s.cuda()
                self.v.cuda()
                if coverage: self.W_a_c.cuda()

        else: sys.exit("error: bad attention method {} option. Use: dot OR general OR concat\n".format(method))

    def forward(self, query, context, len_src_batch, coverage=None):
        #query [B, H] (dec_output, h_t in Luong)
        #context [S, B, H] (enc_hidden, \hat{h}_s in Luong)
        #coverage [B, S] sum over the previous attention vectors
        self.B = query.size(0)
        self.H = query.size(1)
        self.S = context.size(0)

        if self.method in ["general", "dot"]:
            if self.method == "general":
                query = self.W_a(query) #[B,H]
            query = query.view(self.B, 1, self.H)  #[B,1,H]
            context = context.permute(1,2,0) #[B,H,S]
            scores = torch.bmm(query, context).squeeze(1) # [B,1,H] x [B,H,S] --> [B,1,S] --> [B,S]

        else:
            ### context
            context = context.permute(1,0,2) #[B,S,H]
            context = context.contiguous().view(-1, self.H) #[B*S,H]
            context = self.W_a_h(context)
            ### query
            query = self.W_a_s(query) #query [B, H]
            query = query.unsqueeze(1).expand(self.B, self.S, self.H) #[B, 1, H] => [B, S, H]
            query = query.contiguous().view(-1, self.H) # [B*S,H]
            scores = context + query # [B*S,H]
            ### coverage
            if coverage is not None:
                coverage = coverage.view(-1, 1) # [B*S,1]
                coverage = self.W_a_c(coverage) # [B*S,H]
                scores = scores + coverage # [B*S,H]
            scores = torch.tanh(scores) # [B*S,H]
            scores = self.v(scores)  # [B*S,1]
            scores = scores.view(-1, self.S)  # [B,S]

        mask = lens2mask(len_src_batch, self.S) #[B,S]
        if self.cuda:
            mask = mask.cuda()
#        mask = mask.unsqueeze(1)  #[B,1,S]
        scores.masked_fill_(1-mask, -float('inf')) ### Fills elements of scores with -inf where 1-mask is one (padded words)

        #normalize scores
        align = F.softmax(scores, dim=1) #[B, S]
        return align


num=int(input())
list1=input().split()
list1.sort()
while(list1[0]=='0'):
    del list1[0]
    num=num-1

mark=list1[0]
count=1
for i in range(1,num):
    if(list1[i]!=mark):
        mark=list1[i]
        count=count+1

print(count)


import tensorflow as tf
import numpy as np
import random
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import matplotlib.pyplot as plt
import time
import os

physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)

model = tf.keras.models.load_model(
    '/home/amritpal/PycharmProjects/100-days-of-code/100_days_of_code/Skin_disease_working_version_minimal'
    '/saved_model/model_name_RGB_224__0.55__1.61__12-3_2:59')
image_directory = 'img.jpg'

model_save_path = 'model_layers/' + str(time.time())
os.mkdir(model_save_path)

def Look_into_filter(model, image_directory):
    # Let's define a new Model that will take an image as input, and will output
    # intermediate representations for all layers in the previous model after
    # the first.
    successive_outputs = [layer.output for layer in model.layers[1:]]

    # visualization_model = Model(img_input, successive_outputs)
    visualization_model = tf.keras.models.Model(inputs=model.input, outputs=successive_outputs)

    '''# Let's pick a random input image of a normal or pneumonia from the training set.
    normal_img_files = [os.path.join(train_normal_dir, f) for f in train_normal_fnames]
    pneumonia_img_files = [os.path.join(train_pneumonia_dir, f) for f in train_pneumonia_fnames]

    img_path = random.choice(normal_img_files + pneumonia_img_files)'''

    img = load_img(image_directory, target_size=(224, 224))  # this is a PIL image
    x = img_to_array(img)  # Numpy array with shape (150, 150, 3)
    x = x.reshape((1,) + x.shape)  # Numpy array with shape (1, 150, 150, 3)
    x /= 255.0  # Rescale by 1/255

    # Let's run our image through our network, thus obtaining all intermediate representations for this image.
    successive_feature_maps = visualization_model.predict(x)
    layer_names = [layer.name for layer in model.layers]  # names of the layers, to make our plot

    # Now let's display our representations
    for layer_name, feature_map in zip(layer_names, successive_feature_maps):
        if len(feature_map.shape) == 4:
            # Just do this for the conv / maxpool layers, not the fully-connected layers
            n_features = feature_map.shape[-1]  # number of features in the feature map
            size = feature_map.shape[1]  # feature map shape (1, size, size, n_features)

            # We will tile our images in this matrix
            display_grid = np.zeros((size, size * n_features))

            # Postprocess the feature to be visually palatable
            for i in range(n_features):
                x = feature_map[0, :, :, i]
                x -= x.mean()
                x /= x.std()
                x *= 64
                x += 128
                x = np.clip(x, 0, 255).astype('uint8')
                display_grid[:, i * size: (i + 1) * size] = x
            # Tile each filter into a horizontal grid

            # Display the grid
            scale = 20. / n_features
            plt.figure(figsize=(scale * n_features, scale))
            plt.title(layer_name)
            plt.grid(False)
            plt.imshow(display_grid, aspect='auto', cmap='viridis')
            plt.savefig(model_save_path + '/' + layer_name + str(time.time()) + '.png')

Look_into_filter(model, image_directory)


#!/usr/bin/python3
import argparse
import base64
import csv
import os
import re
import smtplib
import socket
import sys
import time
import schedule
import json
import pandas as pd
from datetime import datetime
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import mechanicalsoup
from bs4 import BeautifulSoup
from selenium import webdriver
from mailjet_rest import Client
from decimal import Decimal
from slackclient import SlackClient

btcPrice = {}

class LocalBitcoinsSpider:
    __previos_data = []
    __data = {}
    __total_updated = 0

    def __init__(self, input_url, csv_file, from_address=None, to_address=None, subject=None,
                 email_body=None, api_public_key = None, api_secret_key = None, slack_token=None, btc=None):
        self.__url = input_url
        self.__output_csv = csv_file + '.csv' if not str(csv_file).endswith('.csv') else csv_file
        self.__from_address = from_address
        self.__to_address = to_address
        self.__subject = subject
        self.__email_body = email_body
        self.__api_public_key = api_public_key
        self.__api_secret_key = api_secret_key
        self.__slack_token = slack_token
        self.btc = btc
        try:
            self.__data = pd.read_csv(self.__output_csv, index_col='trader')
        except:
            self.__data = pd.DataFrame(
                             {'tx_total': 0,
                              'reputation': 0,
                              'price': 0,
                              'volume': 0,
                              'age': 0}, index=['null'])

    def __enter__(self):
        self.__build_opener()
        self.__previos_data.clear()
        if os.path.exists(self.__output_csv):
            self.__data = pd.read_csv(self.__output_csv, index_col='trader')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if self.__browser:
            del self.__browser

    def __build_opener(self):
        socket.setdefaulttimeout(60)
        self.__browser = mechanicalsoup.Browser(
            soup_config={'features': 'lxml'}
        )

    def grab_data(self):
        try:
            print('=== URL: {} ==='.format(self.__url))
            cap = webdriver.DesiredCapabilities.PHANTOMJS
            cap["phantomjs.page.settings.javascriptEnabled"] = True
            cap[
                "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'
            driver = webdriver.PhantomJS('./phantomjs', desired_capabilities=cap)
            driver.get(self.__url)

            print('Script is going to sleep for 10 seconds...')
            time.sleep(10)

            print('Trying to get results from page...')
            driver.get(self.__url)
            data = driver.page_source
            driver.close()

            soup = BeautifulSoup(data)
            if not soup:
                print('No response data found from url: {}'.format(self.__url))
                return

            results_link = soup.find('a', {'name': 'results'})
            if not results_link:
                print('No result found!')
                return

            table = results_link.find_next_sibling('table')
            if not table:
                print('No result found!')
                return

            tr_list = table.find_all('tr')
            if tr_list and len(tr_list) > 1:
                for tr in tr_list[1:]:
                    td_list = tr.find_all('td')
                    if not td_list or len(td_list) < 4:
                        continue

                    try:
                        trader1, trader2, trader3 = '', '', ''
                        trader_content = self.__clean_data(td_list[0].text.strip())
                        trader_m = re.search(r'^([^\(]*?)\(([^\)]*?)\)', trader_content)
                        if trader_m:
                            trader_part1 = trader_m.group(1).strip()
                            trader_part2 = trader_m.group(2).strip()
                            trader_part2_split = re.split(r';', trader_part2)
                            trader = trader_part1 + ', ' + ', '.join(
                                [re.sub(r'[^\d]', '', word) for word in trader_part2_split])
                            trader1, trader2, trader3 = trader.split(', ')
                        payment_method = self.__clean_data(td_list[1].text.strip())
                        price = self.__clean_data(td_list[2].text.strip().strip("USD"))
                        limits = self.__clean_data(td_list[3].text.strip())
                        csv_data = [trader1, trader2, trader3, price, limits]
                        print(csv_data)
                        if trader1 in self.__data.index:
                            print('Already exists. Skip writing...')
                            continue
                        value = Decimal(re.sub(r'[^\d.]', '', price))
                        if value<self.btc*1.17:
                            #todo raise exception instead
                            print("Price low, skipping")
                            continue
                        if (int(trader3) < 95):
                            print('Bad Reputation Skip writing...')
                            continue
                        if (int(trader2) < 31):
                            print('Bad Reputation Skip writing...')
                            continue
                        newRow = pd.DataFrame({'tx_total': trader2,
                              'reputation': trader3,
                              'price': price,
                              'volume': limits,
                              'age': 0}, index=[trader1])
                        self.__data = self.__data.append(newRow)
                        self.__total_updated+=1
                    except Exception as ex:
                        print("Exception Found:", ex)
            self.__write_data_to_csv()
        except Exception as x:
            print('Error grabbing page: ')
            print(x)
        finally:
            print("Finally")
            if (self.__total_updated > 0):
                self.send_alarm_via_slack()
                #self.send_email_via_mailjet()

    @staticmethod
    def __clean_data(data):
        try:
            result = re.sub(r'\n+', ' ', data)
            return re.sub(r'\s+', ' ', result)
        except Exception as x:
            print('Error when cleanup data.' + str(x))
        return ''

    def __write_data_to_csv(self):
        df = self.__data
        df['age'] += 1
        df=df[df['age']<3]
        df.to_csv(self.__output_csv, index_label='trader')

    def send_alarm_via_slack(self):
        print("Sending ALARM")
        self.sc=SlackClient(self.__slack_token)
        try:
           self.sc.api_call(
            "chat.postMessage",
            channel="robot",
            text="Novos traders:" + str(self.__data.index.values)
            )
        except:
           print(sys.exc_info()[0])

    def send_email_via_mailjet(self):
        try:
            if not self.__from_address or not self.__to_address:
                return
            print('Sending email...')
            mailjet = Client(auth=(self.__api_public_key, self.__api_secret_key), version='v3.1')
            data = {
                'Messages': [
                    {
                        "From": {
                            "Email": self.__from_address,
                            "Name": ""
                         },
                        "To": [
                            {
                                "Email": self.__to_address,
                                "Name": ""
                            }
                        ],
                        "Subject": self.__subject,
                        "TextPart": self.__email_body + str(self.__data.index.values)
                    }
                ]
            }
            result = mailjet.send.create(data=data)
            print(result.status_code)
            #print(result.json())
            print('Email send successfully.')
        except Exception as x:
            print(x)

def updatedBtcPrice( btc ):
    date = datetime.now().date()
    global btcPrice
    if date in btcPrice:
        return btcPrice[date]
    else:
        try:
          btcPrice[date] = ccxt.gdax().fetch_ticker('BTC/USD')['ask']
          return btcPrice[date]
        except:
          return btc

def job():
    print("Starting a job")
    with open('crawler.conf', 'r') as f:
        args = json.load(f)

    url = args['url']
    #https://localbitcoins.com/instant-bitcoins/?action=sell&country_code=US&amount=&currency=USD&place_country=US&online_provider=ALL_ONLINE&find-offers=Search

    file = 'df'
    email = args['src_email']
    subject = 'New alarms'
    body = "The following traders are available: "
    dest_email = args['dest_email']
    api_public_key = args['api_public_key']
    api_secret_key = args['api_secret_key']
    slack_token = args['slack_token']
    btc = args['btc']
    btc = updatedBtcPrice(btc)
    with LocalBitcoinsSpider(url, file, email, dest_email, subject, body, api_public_key, api_secret_key , slack_token, btc=btc) as spider:
        spider.grab_data()

if __name__ == '__main__':
    job()
    schedule.every(19).minutes.do(job)
    while 1:
        schedule.run_pending()
        time.sleep(1)


import sys

sys.path.append('../')
from model.BasicBert.BertEmbedding import BertEmbeddings
from model.BasicBert.Bert import BertAttention
from model.BasicBert.Bert import BertLayer
from model.BasicBert.Bert import BertEncoder
from model.BasicBert.Bert import BertModel
from model.BasicBert.BertConfig import BertConfig
import torch
from utils.log_helper import logger_init

if __name__ == '__main__':
    logger_init()
    json_file = '../bert_base_chinese/config.json'
    config = BertConfig.from_json_file(json_file)
    config.__dict__['use_torch_multi_head'] = True  # 表示使用 torch框架中的MultiHeadAttention 注意力实现方法
    config.max_position_embeddings = 518 # 测试大于512时的情况
    src = torch.tensor([[1, 3, 5, 7, 9, 2, 3], [2, 4, 6, 8, 10, 0, 0]], dtype=torch.long)
    src = src.transpose(0, 1)  # [src_len, batch_size]
    print(f"input shape [src_len,batch_size]: ", src.shape)
    token_type_ids = torch.LongTensor([[0, 0, 0, 1, 1, 1, 1], [0, 0, 1, 1, 1, 0, 0]]).transpose(0, 1)
    attention_mask = torch.tensor([[True, True, True, True, True, True, True],
                                   [True, True, True, True, True, False, False]])
    # attention_mask 实际就是Transformer中指代的key_padding_mask

    # ------ BertEmbedding -------
    bert_embedding = BertEmbeddings(config)
    bert_embedding_result = bert_embedding(src, token_type_ids=token_type_ids)
    # [src_len, batch_size, hidden_size]

    # 测试类BertAttention
    bert_attention = BertAttention(config)
    bert_attention_output = bert_attention(bert_embedding_result, attention_mask=attention_mask)
    print(f"BertAttention output shape [src_len, batch_size, hidden_size]: ", bert_attention_output.shape)

    # 测试类BertLayer
    bert_layer = BertLayer(config)
    bert_layer_output = bert_layer(bert_embedding_result, attention_mask)
    print(f"BertLayer output shape [src_len, batch_size, hidden_size]: ", bert_layer_output.shape)

    # 测试类BertEncoder
    bert_encoder = BertEncoder(config)
    bert_encoder_outputs = bert_encoder(bert_embedding_result, attention_mask)
    print(f"num of BertEncoder [config.num_hidden_layers]: ", len(bert_encoder_outputs))
    print(f"each output shape in BertEncoder [src_len, batch_size, hidden_size]: ", bert_encoder_outputs[0].shape)

    # 测试类BertModel
    position_ids = torch.arange(src.size()[0]).expand((1, -1))  # [1,src_len]
    bert_model = BertModel(config)
    bert_model_output = bert_model(input_ids=src,
                                   attention_mask=attention_mask,
                                   token_type_ids=token_type_ids,
                                   position_ids=position_ids)[0]
    print(f"BertModel's pooler output shape [batch_size, hidden_size]: ", bert_model_output.shape)
    print("\n  =======  BertMolde 参数: ========")
    for param_tensor in bert_model.state_dict():
        print(param_tensor, "\t", bert_model.state_dict()[param_tensor].size())

    print(f"\n  =======  测试BertModel载入预训练模型： ========")
    model = BertModel.from_pretrained(config, pretrained_model_dir="../bert_base_chinese")


from django.conf import settings
from django.contrib.gis.geos import GEOSGeometry
from rest_framework import serializers
from rest_framework.fields import empty
from rest_framework.utils import model_meta

from issues.api.utils import XMLDict
from issues.excs import MultipleJurisdictionsError
from issues.extensions import get_extensions
from issues.models import Issue, Jurisdiction, Service

class ServiceSerializer(serializers.ModelSerializer):

    class Meta:
        model = Service
        fields = ['service_code', 'service_name', 'description', 'metadata', 'type', 'keywords', 'group']

class IssueSerializer(serializers.ModelSerializer):
    distance = serializers.SerializerMethodField()
    extended_attributes = serializers.SerializerMethodField()
    jurisdiction_id = serializers.SlugRelatedField(
        queryset=Jurisdiction.objects.all(),
        source='jurisdiction',
        required=False,
        slug_field='identifier',
        write_only=True,
    )
    lat = serializers.FloatField(required=False)
    long = serializers.FloatField(required=False)
    service_name = serializers.SerializerMethodField(read_only=True)
    service_request_id = serializers.CharField(read_only=True, source='identifier')

    class Meta:
        model = Issue
        fields = [
            'address',
            'agency_responsible',
            'description',
            'expected_datetime',
            'extended_attributes',
            'distance',
            'jurisdiction_id',
            'lat',
            'long',
            'media_url',
            'requested_datetime',
            'service_code',
            'service_name',
            'service_notice',
            'service_request_id',
            'status',
            'status_notes',
            'updated_datetime',
        ]
        write_only_fields = [  # This is not directly supported by DRF; see below for the patch
            'address_string',
            'first_name',
            'last_name',
            'email',
            'phone',
        ]
        extra_kwargs = {
            'service_code': {'required': True},
        }
        for f in write_only_fields:
            extra_kwargs.setdefault(f, {})["write_only"] = True

    def __init__(self, instance=None, data=empty, **kwargs):
        super().__init__(instance, data, **kwargs)

        for ext in self.context.get('extensions', ()):
            ext.extend_issue_serializer(self)

    def get_distance(self, obj):
        if hasattr(obj, 'distance'):
            return float(obj.distance.m)
        else:
            return None

    def get_service_name(self, obj):
        return obj.service.safe_translation_getter(
            "service_name",
            default=obj.service.service_code,
            any_language=True
        )

    def get_extended_attributes(self, obj):
        extensions = self.context.get('extensions', ())
        extended_attributes = {}
        for ex in extensions:
            extended_attributes.update(
                ex.get_extended_attributes(
                    issue=obj,
                    context=self.context
                ) or {}
            )
        return extended_attributes

    def to_representation(self, instance):
        """

        :type instance: issues.models.Issue
        :rtype: dict
        """
        representation = super().to_representation(instance)
        if not (instance.lat and instance.long):  # Remove null coordinate fields
            representation.pop('lat', None)
            representation.pop('long', None)

        if not self.context.get('extensions', ()):
            representation.pop('extended_attributes', None)

        if representation.get('distance') is None:
            representation.pop('distance')

        return XMLDict(representation, "request")

    def validate(self, data):
        application = self.context.get('application')
        if application:
            data['application'] = application

        service_code = data.pop('service_code')
        service = Service.objects.filter(service_code=service_code).first()
        if not service:
            raise serializers.ValidationError(f'Service code {service_code} is invalid')
        data['service'] = service

        lat = data.pop('lat', None)
        long = data.pop('long', None)
        if lat and long:
            data['location'] = GEOSGeometry(  # TODO: Maybe remove? Looks like a GEOS dependency
                f'SRID=4326;POINT({long} {lat})'
            )

        if not data.get('jurisdiction'):
            try:
                data['jurisdiction'] = Jurisdiction.autodetermine()
            except MultipleJurisdictionsError as mjd:
                raise serializers.ValidationError(mjd.args[0])

        for ex in self.context.get('extensions', ()):
            new_data = ex.validate_issue_data(self, data)
            if new_data:
                data = new_data

        return data

    def create(self, validated_data):
        validated_data['moderation'] = getattr(settings, 'ISSUES_DEFAULT_MODERATION_STATUS', 'public')
        instance = self._create(validated_data)
        extensions = get_extensions()
        request = self.context['request']
        for ex in extensions:
            ex().post_create_issue(request=request, issue=instance, data=validated_data)
        return instance

    def _create(self, validated_data):
        validated_data = validated_data.copy()  # We're going to pop stuff off here.
        # This logic is mostly copied from super().create(),
        # aside from the concrete_data stuff and `.clean()`ing the model before saving.
        ModelClass = self.Meta.model
        info = model_meta.get_field_info(ModelClass)
        many_to_many = {}
        for field_name, relation_info in info.relations.items():
            if relation_info.to_many and (field_name in validated_data):
                many_to_many[field_name] = validated_data.pop(field_name)

        # Build a dict of fields we're guaranteed to be able to pass to `Model.create()`
        concrete_fields = set(info.fields) | set(info.forward_relations)
        concrete_data = {
            field: value
            for (field, value)
            in validated_data.items()
            if field in concrete_fields
        }
        instance = ModelClass(**concrete_data)
        instance.clean()
        instance.save()
        for field_name, value in many_to_many.items():
            setattr(instance, field_name, value)
        return instance


# O(log(n)) time - since we reduce the values we have to search for by half on each iteration
# O(1) space
def binarySearch(array, target):
    # use left and right pointer to help determine the "range" of the array we
    # are searching through since we will be cutting our array by half on each iteration
    left = 0
    right = len(array) - 1
    # iterate through array as long as left pointer has not passed the right pointer
    # if this condition is not met it means that our target does not exist in array
    while left <= right:
        # middle is used as the midpoint of values being searched through. We round down middle using // 2
        middle = (left + right) // 2
        # store potentialMatch value using middle index position
        potentialMatch = array[middle]
        # if target matches potentialMatch then return the index position where it was found (middle)
        if target == potentialMatch:
            return middle
        # if target is less than potentialMatch then shift right pointer to the left by 1
        elif target < potentialMatch:
            right = middle - 1
        # otherwise shift left pointer to the right by 1. Shifting left and right pointers reduces the number
        # of values we have to search through
        else:
            left = middle + 1
    return -1

print(binarySearch([0, 1, 21, 33, 45, 45, 61, 71, 72, 73], 33))


import os
import sys

current = os.path.dirname(os.path.realpath(__file__))
parent = os.path.dirname(current)
sys.path.append(parent)

from argparse import ArgumentParser
from models.voice_encoder import VoiceEncoder
from models.face_decoder import FaceDecoder
import torch
from matplotlib import pyplot as plt
import librosa
import data_preprocessing.audio_spectrograms as audio_processing

parser = ArgumentParser("Use trained VoiceEncoder and FaceDecoder to generate face image out of audio file")

parser.add_argument("--audio-file-path", type=str, required=True,
                    help="Path to the audio file")

parser.add_argument("--voice-encoder-path", type=str, required=True,
                    help="Path to the saved trained model of voice encoder")

parser.add_argument("--face-decoder-path", type=str, required=True,
                    help="Path to the saved trained model of face decoder")

parser.add_argument("--gpu", type=int, default=0,
                    help="-1 for cpu prediction")

parser.add_argument("--audio-length", default=6, type=int,
                    help="length of the audio that will be converted to spectogram. Longer audio \
                          files will be clipped, and shorter files will be repeated")

parser.add_argument("--sampling-rate", default=16000, type=int,
                    help="The number of samples per second of audio")

parser.add_argument("--hop-length", default=10, type=int,
                    help="Period of time between adjacent STFT columns specified in milliseconds")

parser.add_argument("--window-length", default=25, type=int,
                    help="Length of the window of STFT columns ")

parser.add_argument("--n-fft", default=512, type=int,
                    help="Length of the windowed signal after padding with zeros")

parser.add_argument("--power", default=0.3, type=float,
                    help="power applied during power-law compression")

def get_device(choice):
    if choice >= 0:
        if torch.cuda.is_available():
            device = torch.device(f"cuda:{choice}")
        else:
            raise ValueError("GPU training was chosen but cuda is not available")
    else:
        device = torch.device("cpu")
    return device

def main():
    args = parser.parse_args()

    device = get_device(args.gpu)

    face_decoder = FaceDecoder().to(device)
    face_decoder.eval()
    face_decoder_checkpoint = torch.load(args.face_decoder_path)
    face_decoder.load_state_dict(face_decoder_checkpoint["model_state_dict"])

    voice_encoder = VoiceEncoder().to(device)
    voice_encoder.eval()
    voice_encoder_checkpoint = torch.load(args.voice_encoder_path)
    voice_encoder.load_state_dict(voice_encoder_checkpoint["model_state_dict"])

    root = os.path.dirname(args.audio_file_path)
    file_base, _ = os.path.splitext(os.path.basename(args.audio_file_path))
    normalized_file_path = audio_processing.normalize_audio(args.audio_file_path, root, file_base)
    waveform, _ = librosa.load(normalized_file_path, duration=args.audio_length, sr=args.sampling_rate, mono=True)
    waveform = audio_processing.stretch_audio(args, waveform)
    spectrogram = audio_processing.compute_spectrograms(args, waveform)
    spectrogram = audio_processing.power_law_compression(args, spectrogram)
    spectrogram = torch.tensor(spectrogram).unsqueeze(0).to(device)
    os.remove(normalized_file_path)

    with torch.no_grad():
        voice_encoder_embeddings = voice_encoder(spectrogram)
        landmarks_predicted, images_predicted = face_decoder(voice_encoder_embeddings)

    _, axes = plt.subplots(1, 1, figsize=(12, 8))
    temp_image = (images_predicted[0].cpu() * 255).to(torch.uint8)
    axes.imshow(temp_image.permute(1, 2, 0))
    axes.set_title("Reconstructed image")
    axes.axis("off")
    temp_image = landmarks_predicted[0].cpu().view(72, 2)
    x, y =zip(*temp_image.squeeze(0))
    axes.scatter(x, y, c='red', marker='o', s=2)
    plt.show()

if __name__ == "__main__":
    main()


import os
import os.path
from shutil import copyfile

def createDirectory(path):
    if not os.path.exists(path):
        os.mkdir(path)

main_dir = 'COMP/all'
train_percent = 70
valid_percent = 30
test_percent = 0

if (int(train_percent + valid_percent + test_percent) > 100):
    print("The Split between train-valid-test is not proper")
    print(quit)

CENT = 100

TRAIN_LMT = train_percent / CENT
VALID_LMT = valid_percent / CENT
TEST_LMT = test_percent / CENT

train_dir = main_dir.replace("/all", "/train")
val_dir = main_dir.replace("/all", "/val")
test_dir = main_dir.replace("/all", "/test")

createDirectory(train_dir)
createDirectory(test_dir)
createDirectory(val_dir)

#main_dir = os.path.join(main_dir, 'clean_amzon')

i = 0

for class_name in os.listdir(main_dir):
    if class_name == ".DS_Store":
        continue
    src_dir = os.path.join(main_dir, class_name)
    NOF = len(os.listdir(src_dir))
    print(class_name , ' - ', NOF)
    print(int((TRAIN_LMT) * NOF))
    dest = None
    i = 0
    if class_name not in ('test', 'train', 'valid'):
        createDirectory(os.path.join(test_dir, class_name))
        createDirectory(os.path.join(train_dir, class_name))
        createDirectory(os.path.join(val_dir, class_name))
        for file_name in os.listdir(src_dir):

            if i > int((TRAIN_LMT) * NOF):
                dest = os.path.join(val_dir, class_name, file_name)
            else:
                dest = os.path.join(train_dir, class_name, file_name)

            src = os.path.join(src_dir, file_name)

            copyfile(src, dest)

            i = i + 1

print('Symlink Generation Completed')


# Exercise 17: Blood Alcohol Calculator
# 2019-05-24
# Notes: create BAC calculator
# Errors in BAC formula... not validating

import sys

# Declare and define variables and constraints
alcohol_dist: 0.0
alcohol_dist_male = 0.73
alcohol_dist_female = 0.66
user_weight = int(input("What is your weight? "))
user_gender = str(input("What is your gender (M/F)? "))
user_drink_count = int(input("How many drinks have you had (ounces)? "))
user_drink_alcohol_content = float(input("Alcohol by volume of drinks? "))
user_time_last_drink = int(input("How many hours since last drink? "))

# Validate gender and assign alcohol distribution ratio
# in later programs run this immediately after input
if user_gender == 'M':
    alcohol_dist = alcohol_dist_male
elif user_gender == 'F':
    alcohol_dist = alcohol_dist_female
else:
    print("Invalid gender entry! Please try again")
    sys.exit()

bac_calc_consumed = user_drink_count * (user_drink_alcohol_content / 100)

b_a_c = round((bac_calc_consumed * 5.14 / user_weight * alcohol_dist) - 0.015 * user_time_last_drink, 4)

message_bac = f'Your BAC is {b_a_c}.'

if b_a_c <= 0.08:
    message_return = 'It is legal for you to drive.'
else:
    message_return = 'It is not legal for you to drive.'

message_output = f'{message_bac} \n{message_return}'

print(message_output)


# print recursive function to calculate the sum of first n natural number

def sum(n):
    if n<=1:
        return n
    else:
        return n + sum(n-1)

n=16
if n<0:
    print("enter a positive number ")
else:
    print("the sum is ",sum(n))
# n! = (n-1)! * n
# sum(n) = sum(n-1) + n


from binary import Bin
from binary import Bin16

class Memory():
	def __init__(self):
		self.memory = [Bin()] * 65536

	def __getitem__(self, key):
		if isinstance(key, Bin16):
			return self.memory[key.toDecimal()]
		else:
			return self.memory[key]

	def __setitem__(self, key, value):
		if isinstance(key, Bin16):
			self.memory[key.toDecimal()] = value
		else:
			self.memory[key] = value
		return self.memory

	def getROM(self):
		temp = Memory()
		temp.memory = self.memory[:32767+1]
		return temp

	def load(self, file):
		with open(file, "rb") as file:
			byte = file.read(1)
			counter = 0
			while len(byte) != 0:
				byte = Bin.fromDecimal(ord(byte))
				self.memory[counter] = byte
				counter += 1
				byte = file.read(1)

class MemoryBus(Memory):
	def __init__(self, memory):
		self.memory = memory

if __name__ == "__main__":
	a = Memory()
	a.loadROM("game.gb")
	a.getROM()


from typing import Generator
from collections import defaultdict as dd
import numpy as np
import networkx as nx
import re, math, itertools

offsets = [(1, 0), (-1, 0), (0, 1), (0, -1)]

def contains(a, b):
    return a[0] <= b[0] and a[1] >= b[1]

def overlaps(a, b):
    return a[0] <= b[0] <= a[1]

def solve(input: str) -> Generator[any, None, None]:
    lines = [line.strip() for line in input.split("\n") if line]

    pairs = [
        [list(map(int, grp.split("-"))) for grp in line.split(",")] for line in lines
    ]

    total = 0
    for [a, b] in pairs:
        if contains(a, b) or contains(b, a):
            total += 1

    yield total

    count = 0

    for [a, b] in pairs:
        if overlaps(a, b) or overlaps(b, a):
            count += 1

    yield count


# Multiple Inheritance

# Base calss 1
class Investment:
    def __init__(self, symbol, price):
        self.symbol = symbol
        self.price = price

    def investment_info(self):
        print("Inside Investment class")
        print("Symbol:", self.symbol, "Price: ", self.price)

# Base class 2
class Portfolio:
    def __init__(self, investments):
        self.investments = investments

    def portfolio_info(self):
        print(" Inside the Portfolio")
        for investment in self.investments:
            print("Symbol: ", investment.symbol, "Price: ", investment.price)

# Derived Class
class Trader(Investment, Portfolio):
    def __init__(self, symbol, price, investments):
        Investment.__init__(self, symbol, price)
        Portfolio.__init__(self, investments)

    def trader_info(self, name, strategy):
        print("Inside Trader Class")
        print("Name: ", name, "Strategy", strategy)

# Create object of Investment and Trader

inv1 = Investment("AAPL", 170.49)
inv2 = Investment("GOOG", 2890.78)
inv3 = Investment("TSLA", 2000.34)

trader1 = Trader("MSFT", 2130, [inv1,inv2])
trader2 = Trader("AMZN", 3400, [inv2, inv3])

# Access data
trader1.investment_info()
trader1.portfolio_info()
trader1.trader_info('Sakib', 'Long-term')

trader2.investment_info()
trader2.portfolio_info()
trader2.trader_info('Robin', 'Short-term')


from .FileElement import FileElement
from .FileShort import FileShort
from .FileString import FileString
from .FileInt import FileInt
from .FileDate import FileDate
from .FileByte import FileByte
from .FileTri import FileTri
class Prc(FileElement):
    def __init__(self, settings, records):
        super().__init__({
            "title": FileString(32),
            "attributes": FileShort(),
            "version": FileShort(),
            "creationTime": FileDate(),
            "modificationTime": FileDate(),
            "backupTime": FileDate(),
            "modificationNumber": FileInt(),
            "appInfoID": FileInt(),
            "sortInfoID": FileInt(),
            "prcType": FileString(4),
            "creator": FileString(4),
            "uniqueIDSeed": FileInt(),
            "nextRecordListID": FileInt(),
            "numberRecords": FileShort(),
            "recordList": FileElement(),
            "filler": FileShort(),
            "records": FileElement()
        })

        # Set values from the info block
        for name, val in self.elements.items():
            if name in settings.items():
                val.set(settings.items()[name])

        self.get("numberRecords").set(len(records))

        i = 0
        for record in records:
            offset = FileInt()
            attr = FileByte()
            uniqueID = FileTri(i)

            self.elements["recordList"].add("Rec" + str(i), FileElement({
                "offset": offset,
                "attribute": attr,
                "uniqueID": uniqueID
            }))

            self.elements["records"].add("Rec" + str(i), record)
            i += 1

        self.update_offsets(records)

    def get_byte_length(self):
        raise Exception("Test")

    def update_offsets(self, records):
        base = self.offset_to_entry("records")

        i = 0
        for record in records:
            el = self.elements["recordList"].get("Rec" + str(i))
            local = self.elements["records"].offset_to_entry("Rec" + str(i))
            el.get("offset").set(base + local)
            i += 1

    def save(self, file):
        with open(file, "w") as handle:
            handle.write(self.serialize())

    def output(self):
        print(self.serialize())

    def __str__(self):
        output = f"Prc ({self.get_byte_length()} bytes): {{\n"
        for key, value in self.elements.items():
            output += f"\t{key}: {value}\n"
        output += "}"
        return output


import pandas as pd
import numpy as np
import spacy
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report, confusion_matrix
import pickle

#Replace dataset_path with the dataset you want to use
df = pd.read_csv(dataset_path)
queries = np.array(df['text'])
labels = np.array(df['intent'])

# Install trained pipeline with command 'python -m spacy download en_core_web_lg'
nlp = spacy.load('en_core_web_lg')

n_queries = len(queries)
dim_embedding = nlp.vocab.vectors_length
X = np.zeros((n_queries, dim_embedding))

for idx, sentence in enumerate(queries):
    doc = nlp(sentence)
    X[idx, :] = doc.vector
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.20, random_state=42)
neigh = KNeighborsClassifier(n_neighbors=1)
neigh.fit(X_train, y_train)

#Save the weights in neighpickle_file
neigh_pickle = open('neighpickle_file', 'wb')
pickle.dump(neigh, neigh_pickle)

print("Accuracy on Test Set: ", np.count_nonzero(neigh.predict(X_test) == y_test)/len(y_test))

y_true = labels
y_pred = []
for text in queries:
    #If you want to find out every wrong prediction, uncomment the two prints
    #print(text)
    doc = nlp(text)
    prediction = neigh.predict(doc.vector.reshape(1, -1))
    #print(prediction)
    y_pred.append(prediction)

print(classification_report(y_true, y_pred))
print(confusion_matrix(y_true, y_pred))


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import uuid

class Migration(migrations.Migration):

    dependencies = [
        ('message', '0004_team_uid'),
    ]

    operations = [
        migrations.AlterField(
            model_name='team',
            name='uid',
            field=models.CharField(default=uuid.uuid4, unique=True, max_length=100),
            preserve_default=True,
        ),
    ]


import pandas as pd
import collections

def make_transitions(feature_dict, clause_ends=True):
    '''
    --input--
    feature dictionary
    dict[book] = list(list(strings))

    --output--
    x2 df(trans. frequencies x book), df(trans. ratios x book)
    '''

    df_Transition_freq = dict() # Transition matrix with frequences
    df_Transition_prob = dict() # Normalized transition matrix

    # a dict to count the transition pairs per book
    transition_pairs = collections.defaultdict(lambda: collections.Counter())

    for bookname, clauses in feature_dict.items():

        # count transitions using bigrams
        transitions = list()
        for clause in clauses:

            if clause_ends:
                transitions.append("Clause_Begin")
                transitions.extend(clause)
                transitions.append("Clause_End")
            else:
                transitions.extend(clause)

        for i in range(0, len(transitions)-1):

            # assemble transition pair string
            state1 = transitions[i]
            state2 = transitions[i+1]
            transition_string = state1 + '->' + state2

            if transition_string == 'Clause_End->Clause_Begin': # ignore clause_begin to end transition pairs
                continue

            transition_pairs[bookname][transition_string] += 1

        # create the dataframes
        df_trans = pd.DataFrame(transition_pairs).fillna(0)

        # normalize:
        df_mean = df_trans.sum().mean()
        df_norm = df_trans.apply(lambda col: col * df_mean / col.sum())

    return df_trans, df_norm

def make_stacked_transitions(data_dict):
    '''
    --input--
    output from bhsa.get_data()
    i.e. dict[feature][domain][book] = list(list(strings))

    --output--
    x2 df(trans. frequencies x book), df(trans. ratios x book)
    stacked for all features/domains by book
    '''

    freq_matrices = {}
    norm_matrices = {}

    for feature, domains in data_dict.items():
        for domain in domains:

            clause_ends = True if feature != 'clause_types' else False

            # add all data together
            data = data_dict[feature][domain]
            name = feature+'|'+domain
            trans_freq, trans_norm = make_transitions(data, clause_ends)

            # save to dicts
            freq_matrices[name] = trans_freq
            norm_matrices[name] = trans_norm

    stacked_freq = pd.DataFrame()
    stacked_norm = pd.DataFrame()

    # make the frequency stacked matrices
    for data_name, matrix in freq_matrices.items():
        stacked_freq = stacked_freq.append(matrix)

    # make the normalized stacked matrices
    for data_name, matrix in norm_matrices.items():
        stacked_norm = stacked_norm.append(matrix)

    return stacked_freq.fillna(0), stacked_norm.fillna(0)


from aiogram.types import Message, InlineQuery
from loader import dp, bot, upload_client
from utils.db_api.database import Requests
from utils.instagram import InstagramStory

@dp.message_handler(is_subscriber=True, instagram_story=True, state='*')
async def instagram_story_handler(message: Message):
    post = InstagramStory(message.text)
    await post.start()
    await post.send_to(bot=bot, upload_client=upload_client, chat_id=message.chat.id)
    await Requests.add(user_id=message.from_user.id, content_type='s')

@dp.inline_handler(is_subscriber=True, instagram_inline_story=True, state='*')
async def instagram_story_inline_handler(query: InlineQuery):
    story = InstagramStory(query.query.strip())
    await story.start()
    await story.inline_to(query=query)
    await Requests.add(user_id=query.from_user.id, content_type='s')


# %%
import matplotlib.pyplot as plt
import numpy as np
import torch
from tqdm import trange

from src.utils_exact_diagonalization import QuantumSpinSystem
from src.utils_sparse_diag import transverse_ising_sparse_simulator_sample

l = 16
j1 = -1
eps_breaking = 10 ** -3
data = np.load(
    "data/constant_field_ising_1nn_dmrg/test_unet_periodic_1nn_l_16_h_0.0_ndata_10.npz"
)

f_dmrg = data["F"]
z_dmrg = data["density"]
dens_f_dmrg = data["density_F"]
pot = data["potential"]
e_dmrg = data["energy"]
x_dmrg = data["magnetization_x"]

#%%

# %%
plt.plot(z_dmrg)
plt.plot(dens_f_dmrg)
plt.plot(pot)
plt.show()
# %%

for i in trange(10):

    (file_name, e, h, z, f_dens, f, x,) = transverse_ising_sparse_simulator_sample(
        l=16,
        h_max=2.71,
        hs=pot[i],
        j1=j1,
        j2=-1,
        pbc=True,
        file_name="nothing",
        check_2nn=False,
        eps_breaking=eps_breaking,
    )

    if i == 0:
        e_quspin = e
        hs = h
        z_quspin = z
        fs_dens_quspin = f_dens
        f_quspin = f
        x_quspin = x
    else:
        e_quspin = np.append(e_quspin, e)
        hs = np.append(hs, h, axis=0)
        z_quspin = np.append(z_quspin, z, axis=0)
        fs_dens_quspin = np.append(fs_dens_quspin, f_dens, axis=0)
        f_quspin = np.append(f_quspin, f)
        x_quspin = np.append(x_quspin, x, axis=0)

# %% exact diagonalization
# hs = pot
# for r in trange(10):
#     device = "cpu"
#     qs = QuantumSpinSystem(l=l, device=device)
#     # hamiltonian
#     hamiltonian = torch.zeros((2 ** l, 2 ** l), device=device)
#     for k in range(l):
#         hamiltonian = (
#             hamiltonian
#             + j1 * torch.matmul(qs.s_x[k], qs.s_x[(k + 1) % l])
#             + hs[r, k] * qs.s_z[k] + eps_breaking*qs.s_x[k]
#         )

#     e, psi = torch.linalg.eigh(hamiltonian)

#     m_z = []
#     m_x = []
#     for i in range(l):
#         m_z.append(
#             torch.einsum(
#                 "i,ij,j->", torch.conj(psi[:, 0]
#                                        ), qs.s_z[i].to(torch.float), psi[:, 0]
#             )
#             .detach()
#             .cpu()
#             .numpy()
#         )
#         m_x.append(
#             torch.einsum(
#                 "i,ij,j->", torch.conj(psi[:, 0]
#                                        ), qs.s_x[i].to(torch.float), psi[:, 0]
#             )
#             .detach()
#             .cpu()
#             .numpy()
#         )
#     m_z = np.asarray(m_z)
#     m_x = np.asarray(m_x)

#     t_xx = []
#     for i in range(l):
#         t_xx.append(
#             -1
#             * torch.einsum(
#                 "i,ij,jk,k->",
#                 torch.conj(psi[:, 0]),
#                 qs.s_x[i].to(torch.float),
#                 qs.s_x[(i + 1) % l].to(torch.float),
#                 psi[:, 0],
#             )
#             .detach()
#             .cpu()
#             .numpy()
#             # + torch.einsum(
#             #     "i,ij,jk,k->",
#             #     torch.conj(psi[:, 0]),
#             #     qs.s_z[i].to(torch.float),
#             #     qs.s_z[(i + 2) % l].to(torch.float),
#             #     psi[:, 0],
#             # )
#             # .detach()
#             # .cpu()
#             # .numpy()
#         )
#     t_xx = np.asarray(t_xx)

#     if r == 0:
#         t_xx_ed = t_xx.reshape(1, -1)
#         z_ed = m_z.reshape(1, -1)
#         x_ed = m_x.reshape(1, -1)
#     else:
#         t_xx_ed = np.append(t_xx_ed, t_xx.reshape(1, -1), axis=0)
#         z_ed = np.append(z_ed, m_z.reshape(1, -1), axis=0)
#         x_ed = np.append(x_ed, m_x.reshape(1, -1), axis=0)
# %%
for i in range(10):

    plt.plot(z_dmrg[i], label="dmrg", linestyle="--", color="green")
    # plt.plot(z_ed[i], label="ed", linestyle=":", color="blue")
    plt.plot(z_quspin[i], label="quspin", color="red")
    plt.legend()
    plt.show()
# %%
# for i in range(10):

#     plt.plot(dens_f_dmrg[i], label="dmrg", linestyle="--", color="green")
#     plt.plot(t_xx_ed[i], label="ed", linestyle=":", color="blue")
#     plt.plot(fs_dens_quspin[i], label="quspin", color="red")
#     plt.legend()
#     plt.show()
# %%
for i in range(10):

    plt.plot(np.abs(x_dmrg[i]), label="dmrg", linestyle="--", color="green")
    # plt.plot(x_ed[i], label="ed", linestyle=":", color="blue")
    plt.plot(np.abs(x_quspin[i]), label="quspin", color="red")
    plt.legend()
    plt.show()
# %% Phase transition in a costant field case

import matplotlib.pyplot as plt
import numpy as np
import torch
from tqdm import trange

from src.utils_exact_diagonalization import QuantumSpinSystem
from src.utils_sparse_diag import transverse_ising_sparse_simulator_sample

ls = [8, 16, 32, 64, 128]

for l in ls:
    j1 = -1
    eps_breaking = 10 ** -2
    data = np.load(
        f"data/constant_field_ising_1nn_dmrg/test_unet_periodic_1nn_l_{l}_h_0.0_ndata_10.npz"
    )

    f_dmrg = data["F"]
    z_dmrg = data["density"]
    dens_f_dmrg = data["density_F"]
    pot = data["potential"]
    e_dmrg = data["energy"]
    x_dmrg = data["magnetization_x"]

    mx = np.average(np.abs(x_dmrg), axis=-1)
    h = np.average(pot, axis=-1)

    plt.plot(h, mx, label=f"size={l}")
plt.legend(fontsize=10)
plt.show()
# %% with quspin
mx_quspin = np.average(x_quspin, axis=-1)
plt.plot(h, mx_quspin)
plt.show()

# %%


from rtlsdr import RtlSdr
import numpy as np
import time
import scipy
import math
import matplotlib.pyplot as plt

#  zadejte prvni a posledni frekvenci pro definovani okna ktere ma byt zmereno, v MHz
f_start = input("Vloz startovni frekvenci [ MHz ] :")
print ("Fstart =", f_start)
f_stop = input("Vloz koncovou frekvenci [ MHz ] :")
print ("Fstop =", f_stop)
sirka_volba = raw_input("Vloz volbu pro rozliseni, RBW ( a = 244 Hz // b = 61 Hz // c = 15 Hz // d = 1 Hz):")
print ("volba =", sirka_volba)
log_volba = raw_input("Logaritmicka stupnice :  a = ano // n = ne   :")
print ("volba =", log_volba)
pozadi_volba = raw_input("Odecist sumove pozadi :  a = ano // n = ne   :")
print ("volba =", pozadi_volba)

if	sirka_volba is	'a' :
	pocet_vzorku = 8 * 512

if	sirka_volba is	'b' :
	pocet_vzorku = 32 * 512

if	sirka_volba ==	'c' :
	pocet_vzorku = 128 * 512

if	sirka_volba ==	'd' :
	pocet_vzorku = 1024 * 1024

sdr = RtlSdr()
pocet_oken = f_stop-f_start
f_start = f_start * 1e6
f_stop = f_stop * 1e6
sdr.sample_rate = 1.2e6  # Hz
sdr.freq_correction = +30   # korekce ochylky hodin, v PPM
sdr.gain = 12

sdr.center_freq = f_start + 0.5e6    # Hz
x = np.linspace(f_start, f_stop , pocet_oken *(int(round(pocet_vzorku*(1/1.2))))) # definice velikosti pole pro vyslednou FFT - jen pro indexy

data = sdr.read_samples(pocet_vzorku) # testovaci aktivace prijimace, prvni vzorky nejsou kvalitni
provede_mereni = 1
pozadi_temp = 0
if	pozadi_volba ==	'a' :
	pozadi_temp = 1
	raw_input("Probehne mereni pozadi, pro pokracovani stiskni ENTER")

while provede_mereni==1 :  # test zda-li se provede dvakrat pro kontrolu pozadi
	fft = np.linspace(0,0,  pocet_oken *(int(round(pocet_vzorku*(1/1.2))))) # definice velikosti pole pro vyslednou FFT - zde se zapisuje
	for aktualni_okno in range(1, pocet_oken+1):
		print('Merim')
		time.sleep(0.08) # bezpecnostni prodleva pro preladeni a ustaleni
		data = sdr.read_samples(pocet_vzorku)
		sdr.center_freq = sdr.center_freq + 1e6;
		data = abs(scipy.fft(data)) # provede se FFT

		for index in range(-1,1): # vymaze se pripadna velika stejnosmerna hodnota
	        	data[index] = 0

		data = np.roll(data, pocet_vzorku/2) # vycentruje se

		for index in range(0, int(round(pocet_vzorku*(1/1.2)))): # sklada obraz do jednoho velkeho po 1 MHz oknech, ovsem navzorkovanych je 1.2 MHz, okraje 100kHz se zahodi
		        fft[index+(int(round(pocet_vzorku*(1/1.2)))*(aktualni_okno - 1))] = data[index+int(round(pocet_vzorku*(0.1/1.2)))]
		provede_mereni = 0

	if 	pozadi_temp == 1 :      # pozadi_temp je "1" pri prvnim kole mereni pozadi, pri mereni uzitecneho signalu je "0"
		pozadi_data = fft     # zaloha pozadi
		provede_mereni = 1
		pozadi_temp = 0
		raw_input("Probehne mereni s pritomnosti uzitecneho signalu, pro pokracovani stiskni ENTER")
		for index in range(0, pocet_oken *(int(round(pocet_vzorku*(1/1.2))))):
			fft[index] = 0

max_hodnota = 0
for index in range(0, pocet_oken *(int(round(pocet_vzorku*(1/1.2))))): # proscanuje indexy vysledneho FFT okna
	if fft[index] > max_hodnota:
		max_hodnota = fft[index]
		index_max_hodnoty = index
	if	log_volba is	'a' :
		if	fft[index] < 1 :
			fft[index]= 1
		fft[index]=20*math.log10(fft[index])
	if	pozadi_volba ==	'a' :
		fft[index] = fft[index] - pozadi_data[index]

plt.close('all')
f, ax = plt.subplots()
ax.plot(x,fft )
ax.set_title('FFT')
ax.set_ylabel('Amplituda [-]')
if	log_volba is	'a' :
	ax.set_ylabel('Amplituda [dB]')
ax.set_xlabel('Frekvence [ MHz ]')

print('*********')
print('Start [ MHz ]:')
print(f_start/1e6)
print('---------')
print('Stop  [ MHz ]:')
print(f_stop/1e6)
print('---------')
print('Sirka pasma  [ MHz ]:')
print((f_stop - f_start )/1e6)
print('---------')
print('Pocet oken  [ - ]:')
print(pocet_oken)
print('---------')
print('Rozliseni, RBW :')
print(1e6/pocet_vzorku)
print('---------')
print('Dominantni frekvence:')
print(x[index_max_hodnoty])
print('*********')
plt.show()


import collections
import heapq
import functools
import itertools
import re
import sys
import math
import bisect
from typing import *

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
  def maxDepth(self, root: TreeNode) -> int:
    if root is None:
        return 0
    result = []
    def rec(tree, depth):
      if tree.left is None and tree.right is None:
        result.append(depth)
        return

      if tree.left:
        rec(tree.left, depth+1)
      if tree.right:
        rec(tree.right, depth+1)

    rec(root, 0)
    return max(result) + 1

  def book(self, root: TreeNode) -> int:
    if root is None:
      return 0

    queue = collections.deque([root])
    depth = 0

    while queue:
      depth += 1
      for _ in range(len(queue)):
        cur_root = queue.popleft()
        if cur_root.left:
          queue.append(cur_root.left)

        if cur_root.right:
          queue.append(cur_root.right)

    return depth

a = Solution()


#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import (unicode_literals, division, absolute_import,
                        print_function)

__license__   = 'GPL v3'
__copyright__ = '2022, Grant Drake'

from calibre.gui2.actions import menu_action_unique_name
from calibre.constants import numeric_version as calibre_version
from common_icons import get_icon

# ----------------------------------------------
#          Global resources / state
# ----------------------------------------------

# Global definition of our menu actions. Used to ensure we can cleanly unregister
# keyboard shortcuts when rebuilding our menus.
plugin_menu_actions = []

# ----------------------------------------------
#                Menu functions
# ----------------------------------------------

def unregister_menu_actions(ia):
    '''
    For plugins that dynamically rebuild their menus, we need to ensure that any
    keyboard shortcuts are unregistered for them each time.
    Make sure to call this before .clear() of the menu items.
    '''
    global plugin_menu_actions
    for action in plugin_menu_actions:
        if hasattr(action, 'calibre_shortcut_unique_name'):
            ia.gui.keyboard.unregister_shortcut(action.calibre_shortcut_unique_name)
        # starting in calibre 2.10.0, actions are registers at
        # the top gui level for OSX' benefit.
        if calibre_version >= (2,10,0):
            ia.gui.removeAction(action)
    plugin_menu_actions = []

def create_menu_action_unique(ia, parent_menu, menu_text, image=None, tooltip=None,
                       shortcut=None, triggered=None, is_checked=None, shortcut_name=None,
                       unique_name=None, favourites_menu_unique_name=None):
    '''
    Create a menu action with the specified criteria and action, using the new
    InterfaceAction.create_menu_action() function which ensures that regardless of
    whether a shortcut is specified it will appear in Preferences->Keyboard

    For a full description of the parameters, see: calibre\gui2\actions\__init__.py
    '''
    orig_shortcut = shortcut
    kb = ia.gui.keyboard
    if unique_name is None:
        unique_name = menu_text
    if not shortcut == False:
        full_unique_name = menu_action_unique_name(ia, unique_name)
        if full_unique_name in kb.shortcuts:
            shortcut = False
        else:
            if shortcut is not None and not shortcut == False:
                if len(shortcut) == 0:
                    shortcut = None

    if shortcut_name is None:
        shortcut_name = menu_text.replace('&','')

    if calibre_version >= (5,4,0):
        # The persist_shortcut parameter only added from 5.4.0 onwards.
        # Used so that shortcuts specific to other libraries aren't discarded.
        ac = ia.create_menu_action(parent_menu, unique_name, menu_text, icon=None,
                                   shortcut=shortcut, description=tooltip,
                                   triggered=triggered, shortcut_name=shortcut_name,
                                   persist_shortcut=True)
    else:
        ac = ia.create_menu_action(parent_menu, unique_name, menu_text, icon=None,
                                   shortcut=shortcut, description=tooltip,
                                   triggered=triggered, shortcut_name=shortcut_name)
    if shortcut == False and not orig_shortcut == False:
        if ac.calibre_shortcut_unique_name in ia.gui.keyboard.shortcuts:
            kb.replace_action(ac.calibre_shortcut_unique_name, ac)
    if image:
        ac.setIcon(get_icon(image))
    if is_checked is not None:
        ac.setCheckable(True)
        if is_checked:
            ac.setChecked(True)
    # For use by the Favourites Menu plugin. If this menu action has text
    # that is not constant through the life of this plugin, then we need
    # to attribute it with something that will be constant that the
    # Favourites Menu plugin can use to identify it.
    if favourites_menu_unique_name:
        ac.favourites_menu_unique_name = favourites_menu_unique_name

    # Append to our list of actions for this plugin to unregister when menu rebuilt
    global plugin_menu_actions
    plugin_menu_actions.append(ac)

    return ac

def create_menu_item(ia, parent_menu, menu_text, image=None, tooltip=None,
                     shortcut=(), triggered=None, is_checked=None):
    '''
    Create a menu action with the specified criteria and action
    Note that if no shortcut is specified, will not appear in Preferences->Keyboard
    This method should only be used for actions which either have no shortcuts,
    or register their menus only once. Use create_menu_action_unique for all else.

    Currently this function is only used by open_with and search_the_internet plugins
    and would like to investigate one day if it can be removed from them.
    '''
    if shortcut is not None:
        if len(shortcut) == 0:
            shortcut = ()
    ac = ia.create_action(spec=(menu_text, None, tooltip, shortcut),
        attr=menu_text)
    if image:
        ac.setIcon(get_icon(image))
    if triggered is not None:
        ac.triggered.connect(triggered)
    if is_checked is not None:
        ac.setCheckable(True)
        if is_checked:
            ac.setChecked(True)

    parent_menu.addAction(ac)

    # Append to our list of actions for this plugin to unregister when menu rebuilt
    global plugin_menu_actions
    plugin_menu_actions.append(ac)

    return ac


import numpy as np
import matplotlib.pyplot as plt
import random

class GridWorld:
    ACTIONS = [(1, 0), (0, 1), (-1, 0), (0, -1)]

    def __init__(self, height, width, starting_state, terminal_state):
        self.height = height
        self.width = width
        self.starting_state = starting_state
        self.grid = np.zeros((height, width))
        self._current_location = starting_state
        self.terminal_state = terminal_state
        self.terminated = False

    def action(self, a):
        new_location = tuple(np.add(self.current_location, a))

        if new_location[0] >= self.height or new_location[1] >= self.width or np.any(np.array(new_location) < 0):
            return -1  # throws index error if outside grid

        self._current_location = new_location
        if tuple(self.current_location) == self.terminal_state:
            self.terminated = True
            return 0
        return -1

    def reset(self):
        self._current_location = self.starting_state
        self.terminated = False

    @property
    def current_location(self):
        return tuple(self._current_location)

class Agent:

    def __init__(self, epsilon, alpha, gamma, env: GridWorld):
        self.Qs = {}
        self.epsilon = epsilon
        self.env = env
        self.alpha = alpha
        self.gamma = gamma

        inds = [(i, j) for i in range(env.height) for j in range(env.width)]
        for pos in (inds):
            self.Qs[pos] = {}
            for a in env.ACTIONS:
                self.Qs[pos][a] = 0  # initialize all Qs to 0

    def act(self):
        if np.random.uniform(0, 1) < self.epsilon:
            return random.choice(self.env.ACTIONS)
        else:
            return max(self.Qs[env.current_location], key=self.Qs[env.current_location].get)

    def update_qs(self, old_state, reward, new_state, a):
        self.Qs[old_state][a] = (1 - self.alpha) * self.Qs[old_state][a] \
                                + self.alpha * (reward + self.gamma * max(self.Qs[new_state].values()))

env = GridWorld(height=5, width=5, starting_state=(1, 1), terminal_state=(4, 4))

agent = Agent(env=env, alpha=0.1, gamma=1.0, epsilon=0.1)
reward_per_episode = []
for trial in range(500):  # Run trials
    cumulative_reward = 0  # Initialise values of each game
    env.reset()  # re-init environment
    while not env.terminated:  # Run until max steps or until game is finished
        old_state = env.current_location
        a = agent.act()
        reward = env.action(a)
        assert type(a) == tuple
        new_state = env.current_location
        assert new_state in agent.Qs.keys()
        agent.update_qs(old_state, reward, new_state, a)
        cumulative_reward += reward
    reward_per_episode.append(cumulative_reward)  # Append reward for current trial to performance log
plt.plot(reward_per_episode)
plt.show()


# A very simple phonebook using a dictionary

from cs50 import get_string

people = {
    "Brian": "+1-617-495-1000",
    "David": "1-949-468-2750"
}

name = get_string("Name: ")
if name in people:
    number = people[name]
    print(f"Number: {number}")


import random
import string
import os

sample_size = 2000
# path = "../random_strings"
path = "../uniform"
min_length = 5
max_length = 500

if not os.path.isdir(path):
	os.makedirs(path)

def get_random_string(length):
    # choose from all lowercase letter
    letters = string.printable
    result_str = ''.join(random.choice(letters) for i in range(length))
    return result_str

def generate_files():
	prefix = 'str_'
	file_counter = 0
	ext = '.in'

	while file_counter < sample_size:
		try:
			length = random.randint(min_length,max_length)
			string = get_random_string(length)
			with open(os.path.join(path, prefix+str(file_counter)+ext), 'w') as str_file:
				str_file.write(string)
				str_file.close
				file_counter += 1

		except Exception as e:
			print("error:")
			print(e)

if __name__ ==  '__main__':
	generate_files()


"""pytest file for testing pb2df"""
from pb2df import schema_for
from pb.example_pb2 import TimeMessage

def _test_schema(pb_msg_type, expected_schema):
    schema = schema_for(pb_msg_type.DESCRIPTOR)
    assert schema == expected_schema

def test_schema(param):
    _test_schema(param.pb_msg_type, param.expected_schema)

if __name__ == "__main__":

    schema = schema_for(TimeMessage.DESCRIPTOR)
    print(schema)