import keras
import numpy as np
from keras.applications import vgg16
from keras.utils.vis_utils import plot_model

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.imagenet_utils import decode_predictions
from keras.layers import Input, Activation, BatchNormalization, Dot, add
from keras.models import Model, Sequential
from keras.layers.core import Reshape, Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, Conv2DTranspose, UpSampling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.optimizers import Adam
from keras import backend as K
from keras.preprocessing import image
from keras import initializers
import matplotlib.pyplot as plt
from keras.optimizers import SGD
import glob
from tqdm import tqdm
import cv2
#Load the VGG model
count_running_shoe_cat = 0
vgg_model = vgg16.VGG16(weights='imagenet')

EPOCH_MAX = 200000
EPOCH_MIN = 0
EPOCH_STEP = 200

K.set_image_dim_ordering('tf')

BATCH_SIZE = 1000
HEIGHT, WIDTH, CHANNEL = 128,256, 3
input_shape = (HEIGHT, WIDTH, CHANNEL)

adam = Adam(lr=0.0002, beta_1=0.5)
dLosses = []
gLosses = []

def generate_noise(batch_size, noise_shape):
    #input noise to gen seems to be very important!
    return np.random.normal(0, 1, size=(batch_size,)+noise_shape)

def generator(noise_shape):
    c4, c8, c16, c32, c64 = 512, 256, 128, 64, 32  # channel num
    output_dim = CHANNEL
    gen_input = Input(shape = noise_shape)
    generator = Conv2DTranspose(c4, kernel_size = (4,4), strides = (1,1), padding = "valid", data_format = "channels_last", kernel_initializer = "glorot_uniform")(gen_input)
    generator = BatchNormalization(momentum = 0.5)(generator)
    generator = LeakyReLU(0.2)(generator)

    generator = Conv2DTranspose(c8, kernel_size = (4,4), strides = (2,4), padding = "same", data_format = "channels_last", kernel_initializer = "glorot_uniform")(generator)
    generator = BatchNormalization(momentum = 0.5)(generator)
    generator = LeakyReLU(0.2)(generator)

    generator = Conv2DTranspose(c16, kernel_size = (4,4), strides = (2,2), padding = "same", data_format = "channels_last", kernel_initializer = "glorot_uniform")(generator)
    generator = BatchNormalization(momentum = 0.5)(generator)
    generator = LeakyReLU(0.2)(generator)

    generator = Conv2DTranspose(c32, kernel_size = (4,4), strides = (2,2), padding = "same", data_format = "channels_last", kernel_initializer = "glorot_uniform")(generator)
    generator = BatchNormalization(momentum = 0.5)(generator)
    generator = LeakyReLU(0.2)(generator)

    generator = Conv2D(c64, kernel_size = (3,3), strides = (1,1), padding = "same", data_format = "channels_last", kernel_initializer = "glorot_uniform")(generator)
    generator = BatchNormalization(momentum = 0.5)(generator)
    generator = LeakyReLU(0.2)(generator)

    generator = Conv2DTranspose(c64, kernel_size = (4,4), strides = (2,2), padding = "same", data_format = "channels_last", kernel_initializer = "glorot_uniform")(generator)
    generator = BatchNormalization(momentum = 0.5)(generator)
    generator = LeakyReLU(0.2)(generator)

    generator = Conv2DTranspose(output_dim, kernel_size = (4,4), strides = (2,2), padding = "same", data_format = "channels_last", kernel_initializer = "glorot_uniform")(generator)
    generator = Activation('tanh')(generator)

    gen_opt = Adam(lr=0.00015, beta_1=0.5)
    generator_model = Model(input = gen_input, output = generator)
    generator_model.compile(loss='binary_crossentropy', optimizer=gen_opt, metrics=['accuracy'])
    return generator_model

if __name__== "__main__":
    for epoch in range (29999, 30002):
        if  epoch == 30000:
            noise_shape = (1,1,100)
            count_running_shoe_cat = 0
            model = generator(noise_shape)
            model.summary()
            plot_model(model, to_file='./generator.png', show_shapes=True)
            #Model Loading
            model.load_weights("models_test2/dcgan_generator_epoch_30000.h5")

            #Image generation
            noise = generate_noise(BATCH_SIZE, noise_shape)
            gen_imgs = model.predict(noise)
            i = 0
            #Save images in a folder called Generated
            for im in gen_imgs:
                im = im * 127.5 + 127.5
                cv2.imwrite("generated/"+str(i)+".png",im)
                i += 1

            #Image classification
            image_count = 0
            for filename in glob.glob("generated/*.png"):
                image_count += 1

                original = load_img(filename, target_size=(224, 224))

                numpy_image = img_to_array(original)

                image_batch = np.expand_dims(numpy_image, axis=0)

                processed_image = vgg16.preprocess_input(image_batch.copy())

                # get the predicted probabilities for each class
                predictions = vgg_model.predict(processed_image)
                #print (predictions)
                cat = 'n04120489'
                cat2 = 'n03680355'
                cat3 = 'n03047690'
                cat4 = 'n04133789'

                label = decode_predictions(predictions)
                #print (label)

                #TOP 5 ACCURACY -> 1 se classe trovata nei primi 5 risultati
                for i in label:
                    for x in i:
                        if x[0] == cat or x[0] == cat2 or x[0] == cat3 or x[0] == cat4:
                            count_running_shoe_cat += 1 #ranking
                            break
                        #ranking -= 1
            accuracy = count_running_shoe_cat / image_count #(((EPOCH_MAX-EPOCH_MIN) / (EPOCH_STEP) * 15) * 5)
            accuracy_percentage = accuracy * 100
            #print(count_running_shoe_cat)
            #print(accuracy)
            print(epoch, accuracy_percentage)
        #Test1 (1-200k) 6406 0.08541376040213534 8.541376040213535
        #Test2 (100k-200k) 3673 0.09794666666666667 9.794666666666666
        #Test3 (100k-130k) 1173 0.10426666666666666 10.426666666666666
        #Test4 (80k-150k) 1173 0.10426666666666666 10.426666666666666
        #Test5 (110k-160k) 1835 0.09786666666666667 9.786666666666667


# Imports

from __future__ import unicode_literals
from django.db import models
from django.utils.translation import ugettext_lazy as _

# Exports

__all__ = (
    "DefaultModel",
    "PrimaryModel",
)

# Constants

# Models

class DefaultModel(models.Model):
    """Allows a record to be marked as "default".

    This model supplies the following:

    - A ``NullBooleanField`` called ``is_default``.
    - Two static methods for use with the ``pre_save`` signal: ``allow_one_default()`` and ``require_one_default()``.

    In your model:

    .. code-block:: py

        from django.db import models
        from superdjango.constraints.managers import DefaultManager
        from superdjango.constraints.models import DefaultModel
        from django.db.models.signals import pre_save

        class AddressType(DefaultModel):
            objects = DefaultManager()

        pre_save.connect(
            DefaultModel.allow_one_default,
            AddressType,
            dispath_uid="addresstype_allow_one_default"
        )

        # Or to require a default record.
        pre_save.connect(
            DefaultModel.require_one_default,
            AddressType,
            dispath_uid="addresstype_require_one_default"
        )

    To implement the default in a form:

    .. code-block:: py

        from django import forms

        class EmployeeForm(forms.ModelForm):

            class Meta:
                model = Employee
                fields = [
                    "address_type",
                ]

            def __init__(*args, **kwargs):
                super(EmployeeForm, self).__init__(*args, **kwargs)

                self.fields['address_type'].initial = AddressType.objects.default()

    """

    is_default = models.NullBooleanField(
        _("default"),
        help_text=_("Indicates that this is the default record.")
    )

    class Meta:
        abstract = True

    @staticmethod
    def allow_one_default(sender, **kwargs):
        """Use this when there can be only one ``is_default`` that is ``True``.

        """
        # noinspection PyTypeChecker
        there_can_be_only_one(sender, kwargs['instance'], "is_default")

    @staticmethod
    def require_one_default(sender, **kwargs):
        """Works like ``allow_one_default()`` but also insures that the first record entered becomes the default.

        """
        # noinspection PyTypeChecker
        instance = kwargs['instance']
        if not instance.is_default:
            if sender.objects.filter(is_default=True).count() == 0:
                instance.is_default = True

        there_can_be_only_one(sender, instance, "is_default")

class PrimaryModel(models.Model):
    """Allows a record to be marked as "primary".

    This model supplies the following:

    - A ``NullBooleanField`` called ``is_primary``.
    - Two static methods for use with the ``pre_save`` signal: ``allow_one_primary()`` and ``require_one_primary()``.

    .. code-block:: py

        from django.db import models
        from superdjango.constraints.models import PrimaryModel
        from django.db.models.signals import pre_save

        class Address(models.Model):
            # A contact's address.

        class Contact(models.Model):
            # A contact in an address book.

        class LinkContactAddress(PrimaryModel):
            # Connect contacts with addresses.

            address = models.ForeignKey(Address)
            contact = models.ForeignKey(Contact)

        pre_save.connect(
            PrimaryModel.allow_one_primary,
            JoinContactAddress,
            dispath_uid="joincontactaddress_allow_one_primary"
        )

        # Or to require a primary record.
        pre_save.connect(
            PrimaryModel.require_one_primary,
            JoinContactAddress,
            dispath_uid="joincontactaddress_require_one_primary"
        )

    """

    is_primary = models.NullBooleanField(
        _("primary"),
        help_text=_("Indicates that this is the primary record.")
    )

    class Meta:
        abstract = True

    @staticmethod
    def allow_one_primary(sender, **kwargs):
        """Use this when there can be only one ``is_primary`` that is ``True``.  See ``allow_one_default`` for
        implementation example.

        """
        # noinspection PyTypeChecker
        there_can_be_only_one(sender, kwargs['instance'], "is_primary")

    @staticmethod
    def require_one_primary(sender, **kwargs):
        """Works like ``allow_one_primary()`` but also insures that the first record entered becomes the primary.

        """
        # noinspection PyTypeChecker
        instance = kwargs['instance']
        if not instance.is_primary:
            if sender.objects.filter(is_primary=True).count() == 0:
                instance.is_primary = True

        there_can_be_only_one(sender, instance, "is_primary")

# Utilities

def there_can_be_only_one(cls, instance, field_name):
    """Helper function for the ``allow_*`` methods.

    :param cls: The class (sender) that emitted the signal.
    :type cls: class

    :param instance: The instance to be checked.
    :type: instance: object

    :param field_name: The name of the field to be checked.

    """
    try:
        field_value = getattr(instance, field_name, False)
    except AttributeError:
        return

    if field_value:
        cls.objects.update(**{field_name: False})


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

import ogr
import osr
import networkx as nx
import networker.geomath as gm
from networker.classes.geograph import GeoGraph
import warnings
import os

"""
Package for reading/writing networkx based GeoGraphs
Note:  these wrap existing networkx functions for custom behavior
"""

# TEMPORARY FIX:
# Copied from networkx library and modified, but I have a PR
# for this to be included in networkx lib
# https://github.com/networkx/networkx/pull/1491
def read_shp(path, simplify=True):
    """Generates a networkx.DiGraph from shapefiles. Point geometries are
    translated into nodes, lines into edges. Coordinate tuples are used as
    keys. Attributes are preserved, line geometries are simplified into start
    and end coordinates. Accepts a single shapefile or directory of many
    shapefiles.

    "The Esri Shapefile or simply a shapefile is a popular geospatial vector
    data format for geographic information systems software [1]_."

    Parameters
    ----------
    path : file or string
       File, directory, or filename to read.

    simplify:  Simplify line geometries to start and end coordinates
        If False, and line feature geometry has multiple segments, the
        attributes for that feature will be repeated for each edge comprising
        that feature

    Returns
    -------
    G : NetworkX graph

    Examples
    --------
    >>> G=nx.read_shp('test.shp') # doctest: +SKIP

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Shapefile
    """
    try:
        from osgeo import ogr
    except ImportError:
        raise ImportError("read_shp requires OGR: http://www.gdal.org/")

    if not isinstance(path, str):
        return

    net = nx.DiGraph()
    shp = ogr.Open(path)
    for lyr in shp:
        fields = [x.GetName() for x in lyr.schema]
        for f in lyr:
            flddata = [f.GetField(f.GetFieldIndex(x)) for x in fields]
            g = f.geometry()
            attributes = dict(zip(fields, flddata))
            attributes["ShpName"] = lyr.GetName()
            if g.GetGeometryType() == 1:  # point
                net.add_node((g.GetPoint_2D(0)), attributes)
            if g.GetGeometryType() == 2:  # linestring
                attributes["Wkb"] = g.ExportToWkb()
                attributes["Wkt"] = g.ExportToWkt()
                attributes["Json"] = g.ExportToJson()
                if simplify:
                    last = g.GetPointCount() - 1
                    net.add_edge(g.GetPoint_2D(0), g.GetPoint_2D(last), attributes)
                else:
                    for i in range(0, g.GetPointCount() - 1):
                        net.add_edge(g.GetPoint_2D(i), g.GetPoint_2D(i+1), attributes)
    return net

def load_shp(shp_path, simplify=True):
    """ loads a shapefile into a networkx based GeoGraph object

    Args:
        shp_path:  string path to a line or point shapefile

        simplify:  Only retain start/end nodes of multi-segment lines

    Returns:
        geograph:  GeoGraph

    """

    # NOTE:  if shp_path is unicode io doesn't work for some reason
    shp_path = shp_path.encode('ascii', 'ignore')
    # TODO:  Call from networkx lib once PR has been
    # accepted and released
    # g = nx.read_shp(shp_path, simplify=simplify)
    g = read_shp(shp_path, simplify=simplify)
    coords = dict(enumerate(g.nodes()))

    driver = ogr.GetDriverByName('ESRI Shapefile')
    shp = driver.Open(shp_path)
    layer = shp.GetLayer()

    spatial_ref = layer.GetSpatialRef()
    proj4 = None
    if not spatial_ref:
        if gm.is_in_lon_lat(coords):
            proj4 = gm.PROJ4_LATLONG
        else:
            warnings.warn("Spatial Reference could not be set for {}".
                format(shp_path))

    else:
        proj4 = spatial_ref.ExportToProj4()

    g = nx.convert_node_labels_to_integers(g)

    return GeoGraph(srs=proj4, coords=coords, data=g)

def write_shp(geograph, shp_dir):
    """ writes a shapefile from a networkx based GeoGraph object

    Args:
        geograph: GeoGraph object
        shp_dir:  string path to dir to write shape files

    """

    assert geograph.is_aligned()

    # looks like networkx wants us to relabel nodes by their coords
    tup_map = {i: tuple(coords) for i, coords in geograph.coords.items()}

    # copy geograph to plain networkx graph
    # (relabeling a GeoGraph doesn't seem to work)
    nx_coord_graph = nx.Graph(data=geograph)
    nx.relabel_nodes(nx_coord_graph, tup_map, copy=False)

    nx.write_shp(nx_coord_graph, shp_dir)

    if geograph.srs:
        # write srs info to prj file (nx seems to miss this)
        sr = osr.SpatialReference()
        sr.ImportFromProj4(geograph.srs)
        main_prj_filename = shp_dir + '.prj'
        edge_prj_filename = os.path.join(shp_dir, 'edges.prj')
        node_prj_filename = os.path.join(shp_dir, 'nodes.prj')

        def write_prj(prj_filename):
            out = open(prj_filename, 'w')
            out.write(sr.ExportToWkt())
            out.close()

        write_prj(main_prj_filename)
        write_prj(edge_prj_filename)
        write_prj(node_prj_filename)


import pandas as pd
from PIL import Image
import fix_csv
import os.path

class Model:
    def __init__(self , csv_file_name, image_name):
        self.df = self.convert_csv_to_pickle(csv_file_name)
        # aa = self.df.head(25)
        # aa.to_pickle('../data/sample1.pkl.xz')
        # aa.to_csv('../data/sample1.csv')
        self.objs = self.get_objs()
        self.df_by_obj = self.get_df_by_obj()
        self.img = Image.open(image_name)

    def load_pickle_file(self, file_name):
        return pd.read_pickle(file_name)

    def convert_csv_to_pickle(self, csv_file_name):
        pickle_file_name = f"{csv_file_name[:csv_file_name.rfind('.')]}.pkl.xz"

        if os.path.isfile(pickle_file_name):
            return pd.read_pickle(pickle_file_name)

        csv_fixed_file_name = fix_csv.fix_csv_file(csv_file_name)[0]
        df = pd.read_csv(csv_fixed_file_name, usecols=[1, 2, 3, 5, 9, 10, 12])
        df.columns = ['x', 'y', 'obj', 'seq', 'filename', 'time', 'delta_time']
        df['time'] = pd.to_datetime(df['time'])
        df['delta_time'] = pd.to_timedelta(df['delta_time'])
        df['time'] += df['delta_time']

        df_int = df.select_dtypes(include=['int64'])
        converted_int = df_int.apply(pd.to_numeric, downcast='unsigned')
        df[converted_int.columns] = converted_int
        df_obj = df.select_dtypes(include=['object']).copy()
        converted_obj = pd.DataFrame()

        for col in df_obj.columns:
            num_unique_values = len(df_obj[col].unique())
            num_total_values = len(df_obj[col])
            if num_unique_values / num_total_values < 0.5:
                converted_obj.loc[:, col] = df_obj[col].astype('category')
            else:
                converted_obj.loc[:, col] = df_obj[col]

        df[converted_obj.columns] = converted_obj

        df = df.drop('delta_time', 1)
        df = df.drop_duplicates()
        df.to_pickle(pickle_file_name)
        return pd.read_pickle(pickle_file_name)

    def get_objs(self):
        self.df.groupby([
            "filename",
            "obj"]
        ).size().sort_values(ascending=False)

    def get_df_by_obj(self):
        return self.df.set_index(['filename', 'obj']).sort_index()


#! /usr/bin/env python2

# This script adds sequences h4238 to 5' end of a read sequence in order to fit Nextera experimental pipeline.
# SYNTAX: python h4328_add.py read_2_file.fastq > read_2_with_h4328_file.fastq

import sys, os.path
import re

#System for opening arguments and automatically adding the -h option. Possible to add more options.
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("read2_file", help="Read 2 file in fastq format")
args = parser.parse_args()

add_seq = 'TAGGACCAGCGTCTCAGTAT'

file_list = []
# Opens fastq file
with open(args.read2_file) as fastqopen:
    for stuff in fastqopen:
        file_list.append(stuff)

counter = 0
for line in file_list:

    counter = counter +1
    if counter == 1:
        sys.stdout.write(line)
    elif counter == 2:
        sys.stdout.write(''.join([add_seq,line.rstrip()]))
        sys.stdout.write('\n')
    elif counter == 3:
        sys.stdout.write(line)
    elif counter == 4:
        sys.stdout.write(''.join(['~'*len(add_seq),line.rstrip()]))
        sys.stdout.write('\n')
        counter = 0

#counter = 0
#for line in file_list:
#
#    counter = counter +1
#    if counter == 1:
#        sys.stdout.write(line)
#    elif counter == 2:
#        sys.stdout.write(line[:-20])
#        sys.stdout.write('\n')
#    elif counter == 3:
#        sys.stdout.write(line)
#    elif counter == 4:
#        sys.stdout.write(line[:-20])
#        sys.stdout.write('\n')
#        counter = 0


import os
import shutil
import logging
from local_module import (
    Uplink, BucketNotEmptyError, BucketNotFoundError, ListObjectsOptions
)

# Storj database
STORJ_API_KEY = os.environ.get('STORJ_API_KEY')
STORJ_SATELLITE = os.environ.get('STORJ_SATELLITE')
STORJ_ENCRYPTION_PASSPHRASE = os.environ.get('STORJ_ENCRYPTION_PASSPHRASE')

uplink = Uplink()

access = uplink.request_access_with_passphrase(
    satellite=STORJ_SATELLITE,
    api_key=STORJ_API_KEY,
    passphrase=STORJ_ENCRYPTION_PASSPHRASE
)

project = access.open_project()

# Download options
DOWNLOAD_BASE_PATH = './bot/buffer/storj_download'

async def list_file(bucket_name: str, options: dict = None):
    # default options
    options = options or {
        "prefix": '',
        "recursive": True,
        "system": False
    }

    objects_list = project.list_objects(
        bucket_name,
        ListObjectsOptions(**options)
    )

    return [obj.key for obj in objects_list]

async def delete_file(bucket_name: str, storj_path: str) -> bool:
    try:
        project.delete_object(bucket_name, storj_path)
        return True
    except BaseException:
        logging.warning(f'Error while deleting files: {bucket_name}, {storj_path}')
        return False

async def delete_folder(bucket_name: str, storj_path: str):
    search_options = {
        "prefix": storj_path,
        "recursive": True,
        "system": False
    }
    for file in list_file(bucket_name, search_options):
        if '.' in file.split('/')[-1]:
            await delete_file(bucket_name, file)

async def create_bucket(bucket_name: str) -> bool:
    try:
        project.create_bucket(bucket_name)
        return True
    except BaseException:
        logging.warning(f'Error while creating bucket: {bucket_name}')
        return False

async def delete_bucket(bucket_name: str) -> bool:
    try:
        project.delete_bucket(bucket_name)
        return True
    # if delete bucket fails due to "not empty", delete all the objects and try again
    except BucketNotEmptyError as exception:
        logging.warning(f'Error while deleting bucket: {exception.message}')
        logging.warning("Deleting object's inside bucket and try to delete bucket again...")

        # list objects in given bucket recursively using ListObjectsOptions
        objects_list = project.list_objects(bucket_name, ListObjectsOptions(recursive=True))
        # iterate through all objects path
        for obj in objects_list:
            # delete selected object
            project.delete_object(bucket_name, obj.key)
        return False
    except BucketNotFoundError as exception:
        logging.warning(f'Desired bucket delete error: {exception.message}')
        return False

async def upload_file(bucket_name: str, local_path: str, storj_path: str):
    with open(local_path, 'r+b') as file_handle:
        # get upload handle to specified bucket and upload file path
        upload = project.upload_object(bucket_name, storj_path)
        # upload file on storj
        upload.write_file(file_handle)
        # commit the upload
        upload.commit()

async def download_file(bucket_name: str, local_path: str, storj_path: str):
    with open(local_path, 'w+b') as file_handle:
        download = project.download_object(bucket_name, storj_path)
        # download data from storj to file
        download.read_file(file_handle)
        # close the download stream
        download.close()

async def download_folder(bucket_name: str, storj_path: str):
    download_path = (
        f'{DOWNLOAD_BASE_PATH}/{"_".join(storj_path[:-1].split("/"))}'
    )
    try:
        shutil.rmtree(DOWNLOAD_BASE_PATH)
    except BaseException:
        pass

    os.makedirs(download_path)

    async def traverse_folder(bucket_name: str, storj_path: str):
        search_options = {
            "prefix": storj_path,
            "recursive": False,
            "system": False
        }
        for child in list_file(bucket_name, search_options):
            if child.endswith('/'):
                # [:-1]: 'a/b/c/' -> 'a/b/c'
                child_local_path = f'{download_path}/{child[:-1]}'
                if not os.path.exists(child_local_path):
                    os.makedirs(child_local_path)

                await traverse_folder(
                    bucket_name=bucket_name,
                    storj_path=child
                )
            elif '.' in child.split('/')[-1]:
                child_local_path = f'{download_path}/{child}'
                await download_file(
                    bucket_name=bucket_name,
                    local_path=child_local_path,
                    storj_path=child
                )

    await traverse_folder(
        bucket_name=bucket_name,
        storj_path=storj_path
    )


# -*- coding: utf-8 -*-
import logging
import pymongo

from flask import (
    Blueprint,
    render_template,
    flash,
    redirect,
    request,
    Response,
    url_for,
)
from flask_login import current_user
from werkzeug.datastructures import Headers

from . import controllers
from scout.constants import (
    PHENOTYPE_GROUPS,
    CASEDATA_HEADER,
    CLINVAR_HEADER,
    ACMG_COMPLETE_MAP,
    ACMG_MAP,
)
from scout.server.extensions import store
from scout.server.utils import user_institutes, templated, institute_and_case
from .forms import InstituteForm, GeneVariantFiltersForm

LOG = logging.getLogger(__name__)

blueprint = Blueprint(
    "overview",
    __name__,
    template_folder="templates",
    static_folder="static",
    static_url_path="/overview/static",
)

@blueprint.route("/overview")
def institutes():
    """Display a list of all user institutes."""
    institute_objs = user_institutes(store, current_user)
    institutes = []
    for ins_obj in institute_objs:
        sanger_recipients = []
        for user_mail in ins_obj.get("sanger_recipients", []):
            user_obj = store.user(user_mail)
            if not user_obj:
                continue
            sanger_recipients.append(user_obj["name"])
        institutes.append(
            {
                "display_name": ins_obj["display_name"],
                "internal_id": ins_obj["_id"],
                "coverage_cutoff": ins_obj.get("coverage_cutoff", "None"),
                "sanger_recipients": sanger_recipients,
                "frequency_cutoff": ins_obj.get("frequency_cutoff", "None"),
                "phenotype_groups": ins_obj.get("phenotype_groups", PHENOTYPE_GROUPS),
                "case_count": sum(1 for i in store.cases(collaborator=ins_obj["_id"])),
            }
        )

    data = dict(institutes=institutes)
    return render_template("overview/institutes.html", **data)

@blueprint.route("/<institute_id>/cases")
@templated("overview/cases.html")
def cases(institute_id):
    """Display a list of cases for an institute."""

    institute_obj = institute_and_case(store, institute_id)
    query = request.args.get("query")

    limit = 100
    if request.args.get("limit"):
        limit = int(request.args.get("limit"))

    skip_assigned = request.args.get("skip_assigned")
    is_research = request.args.get("is_research")
    all_cases = store.cases(
        collaborator=institute_id,
        name_query=query,
        skip_assigned=skip_assigned,
        is_research=is_research,
    )

    sort_by = request.args.get("sort")
    sort_order = request.args.get("order") or "asc"
    if sort_by:
        pymongo_sort = pymongo.ASCENDING
        if sort_order == "desc":
            pymongo_sort = pymongo.DESCENDING
        if sort_by == "analysis_date":
            all_cases.sort("analysis_date", pymongo_sort)
        elif sort_by == "track":
            all_cases.sort("track", pymongo_sort)
        elif sort_by == "status":
            all_cases.sort("status", pymongo_sort)

    LOG.debug("Prepare all cases")

    prioritized_cases = store.prioritized_cases(institute_id=institute_id)

    data = controllers.cases(store, all_cases, prioritized_cases, limit)
    data["sort_order"] = sort_order
    data["sort_by"] = sort_by
    data["nr_cases"] = store.nr_cases(institute_id=institute_id)

    sanger_unevaluated = controllers.get_sanger_unevaluated(store, institute_id, current_user.email)
    if len(sanger_unevaluated) > 0:
        data["sanger_unevaluated"] = sanger_unevaluated

    return dict(
        institute=institute_obj,
        skip_assigned=skip_assigned,
        is_research=is_research,
        query=query,
        **data,
    )

@blueprint.route("/<institute_id>/causatives")
@templated("overview/causatives.html")
def causatives(institute_id):
    institute_obj = institute_and_case(store, institute_id)
    query = request.args.get("query", "")
    hgnc_id = None
    if "|" in query:
        # filter accepts an array of IDs. Provide an array with one ID element
        try:
            hgnc_id = [int(query.split(" | ", 1)[0])]
        except ValueError:
            flash("Provided gene info could not be parsed!", "warning")

    variants = store.check_causatives(institute_obj=institute_obj, limit_genes=hgnc_id)
    if variants:
        variants.sort("hgnc_symbols", pymongo.ASCENDING)
    all_variants = {}
    all_cases = {}
    for variant_obj in variants:
        if variant_obj["case_id"] not in all_cases:
            case_obj = store.case(variant_obj["case_id"])
            all_cases[variant_obj["case_id"]] = case_obj
        else:
            case_obj = all_cases[variant_obj["case_id"]]

        if variant_obj["variant_id"] not in all_variants:
            all_variants[variant_obj["variant_id"]] = []

        all_variants[variant_obj["variant_id"]].append((case_obj, variant_obj))

    acmg_map = {key: ACMG_COMPLETE_MAP[value] for key, value in ACMG_MAP.items()}

    return dict(institute=institute_obj, variant_groups=all_variants, acmg_map=acmg_map)

@blueprint.route("/<institute_id>/gene_variants", methods=["GET", "POST"])
@templated("overview/gene_variants.html")
def gene_variants(institute_id):
    """Display a list of SNV variants."""
    page = int(request.form.get("page", 1))

    institute_obj = institute_and_case(store, institute_id)

    # populate form, conditional on request method
    if request.method == "POST":
        form = GeneVariantFiltersForm(request.form)
    else:
        form = GeneVariantFiltersForm(request.args)

    if form.variant_type.data == []:
        form.variant_type.data = ["clinical"]

    variant_type = form.data.get("variant_type")

    # check if supplied gene symbols exist
    hgnc_symbols = []
    non_clinical_symbols = []
    not_found_symbols = []
    not_found_ids = []
    data = {}
    if (form.hgnc_symbols.data) and len(form.hgnc_symbols.data) > 0:
        is_clinical = form.data.get("variant_type", "clinical") == "clinical"
        # clinical_symbols = store.clinical_symbols(case_obj) if is_clinical else None
        for hgnc_symbol in form.hgnc_symbols.data:
            if hgnc_symbol.isdigit():
                hgnc_gene = store.hgnc_gene(int(hgnc_symbol))
                if hgnc_gene is None:
                    not_found_ids.append(hgnc_symbol)
                else:
                    hgnc_symbols.append(hgnc_gene["hgnc_symbol"])
            elif store.hgnc_genes(hgnc_symbol).count() == 0:
                not_found_symbols.append(hgnc_symbol)
            # elif is_clinical and (hgnc_symbol not in clinical_symbols):
            #     non_clinical_symbols.append(hgnc_symbol)
            else:
                hgnc_symbols.append(hgnc_symbol)

        if not_found_ids:
            flash("HGNC id not found: {}".format(", ".join(not_found_ids)), "warning")
        if not_found_symbols:
            flash(
                "HGNC symbol not found: {}".format(", ".join(not_found_symbols)), "warning",
            )
        if non_clinical_symbols:
            flash(
                "Gene not included in clinical list: {}".format(", ".join(non_clinical_symbols)),
                "warning",
            )
        form.hgnc_symbols.data = hgnc_symbols

        LOG.debug("query {}".format(form.data))

        variants_query = store.gene_variants(
            query=form.data, institute_id=institute_id, category="snv", variant_type=variant_type,
        )

        data = controllers.gene_variants(store, variants_query, institute_id, page)

    return dict(institute=institute_obj, form=form, page=page, **data)

@blueprint.route("/overview/<institute_id>/settings", methods=["GET", "POST"])
def institute_settings(institute_id):
    """Show institute settings page"""

    if institute_id not in current_user.institutes and current_user.is_admin is False:
        flash(
            "Current user doesn't have the permission to modify this institute", "warning",
        )
        return redirect(request.referrer)

    institute_obj = store.institute(institute_id)
    form = InstituteForm(request.form)

    # if institute is to be updated
    if request.method == "POST" and form.validate_on_submit():
        institute_obj = controllers.update_institute_settings(store, institute_obj, request.form)
        if isinstance(institute_obj, dict):
            flash("institute was updated ", "success")
        else:  # an error message was retuned
            flash(institute_obj, "warning")
            return redirect(request.referrer)

    data = controllers.institute(store, institute_id)
    default_phenotypes = controllers.populate_institute_form(form, institute_obj)

    return render_template(
        "/overview/institute_settings.html",
        form=form,
        default_phenotypes=default_phenotypes,
        panel=1,
        **data,
    )

@blueprint.route("/overview/<institute_id>/users", methods=["GET"])
def institute_users(institute_id):
    """Should institute users list"""

    if institute_id not in current_user.institutes and current_user.is_admin is False:
        flash(
            "Current user doesn't have the permission to modify this institute", "warning",
        )
        return redirect(request.referrer)
    data = controllers.institute(store, institute_id)
    return render_template("/overview/users.html", panel=2, **data)

@blueprint.route("/<submission>/<case>/rename/<old_name>", methods=["POST"])
def clinvar_rename_casedata(submission, case, old_name):
    """Rename one or more casedata individuals belonging to the same clinvar submission, same case"""

    new_name = request.form.get("new_name")
    controllers.update_clinvar_sample_names(
        store, submission, case, old_name, new_name,
    )
    return redirect(request.referrer)

@blueprint.route("/<institute_id>/<submission>/update_status", methods=["POST"])
def clinvar_update_submission(institute_id, submission):
    """Update a submission status to open/closed, register an official SUB number or delete the entire submission"""

    controllers.update_clinvar_submission_status(store, request, institute_id, submission)
    return redirect(request.referrer)

@blueprint.route("/<submission>/<object_type>", methods=["POST"])
def clinvar_delete_object(submission, object_type):
    """Delete a single object (variant_data or case_data) associated with a clinvar submission"""

    store.delete_clinvar_object(
        object_id=request.form.get("delete_object"),
        object_type=object_type,
        submission_id=submission,
    )
    return redirect(request.referrer)

@blueprint.route("/<submission>/download/<csv_type>/<clinvar_id>", methods=["GET"])
def clinvar_download_csv(submission, csv_type, clinvar_id):
    """Download a csv (Variant file or CaseData file) for a clinVar submission"""

    def generate_csv(header, lines):
        """Return downloaded header and lines with quoted fields"""
        yield header + "\n"
        for line in lines:
            yield line + "\n"

    clinvar_file_data = controllers.clinvar_submission_file(store, submission, csv_type, clinvar_id)

    if clinvar_file_data is None:
        return redirect(request.referrer)

    headers = Headers()
    headers.add(
        "Content-Disposition", "attachment", filename=clinvar_file_data[0],
    )
    return Response(
        generate_csv(",".join(clinvar_file_data[1]), clinvar_file_data[2]),
        mimetype="text/csv",
        headers=headers,
    )

@blueprint.route("/<institute_id>/clinvar_submissions", methods=["GET"])
@templated("overview/clinvar_submissions.html")
def clinvar_submissions(institute_id):
    """Handle clinVar submission objects and files"""

    institute_obj = institute_and_case(store, institute_id)

    data = {
        "submissions": controllers.clinvar_submissions(store, institute_id),
        "institute": institute_obj,
        "variant_header_fields": CLINVAR_HEADER,
        "casedata_header_fields": CASEDATA_HEADER,
    }
    return data


#!usr/bin/python3

import matplotlib

matplotlib.use("agg")

import numpy as np

import matplotlib.pyplot as plt

points=100000

file=open('/home/richard/NetBeansProjects/inapik/inapi18d3.txt',"r")
x,y,z,a=[],[],[],[]
for k in file:
	row=k.split()
	x.append(float(row[0]))
	y.append(float(row[1]))
ax=np.array(x)
ay=np.array(y)

v2=np.zeros(points)
vav=np.mean(ay)
for i in range(0,points):
	v2[i]=(ay[i]-vav)**2
v2av=np.mean(v2)

plt.figure()
plt.xlabel('time')
plt.ylabel('membrane voltage')
plt.xlim(50,70)
plt.plot(ax,ay,label='%.6f' %v2av)
#plt.plot(ax,az)
#plt.plot(ax,aa)
plt.legend()
plt.savefig('inapivar3.pdf')


from django.urls import path
from . import views
urlpatterns = [
    path("", views.index,name ="ShopName"),
    path("about/", views.about,name ="AboutUs"),
    path("contect/", views.contect,name ="contect"),
    path("tracker/", views.tracker,name ="TrackingStatus"),
    path("search/", views.search,name ="Search"),
    path("products/<int:myid>", views.productview,name ="productview"),
    path("checkout/", views.checkout,name ="Checkout"),
    path("handlerequest/", views.handlerequest,name ="HandleRequest"),

]


import openpyxl

wb = openpyxl.load_workbook("censuspopdata.xlsx") #this is my workbook object

sheets = wb.get_sheet_names()
print(sheets) #['Population by Census Tract']

sheet = wb.get_sheet_by_name("Population by Census Tract")

# now that we have the sheet, let us make a list of the columns:
# before that we need to get the total number of rows and total number of columns...

rows = sheet.max_row
columns = sheet.max_column

colist = []
for i in range(1,columns+1):
    colist.append(openpyxl.utils.get_column_letter(i))

# now let us get into the main loop that will get all the details from the sheet:

for i in range(1,rows+1):
    for j in colist:
        print(sheet["{}{}".format(j,i)].value, end=" ")
    print()

# this code is working very well....


from copy import deepcopy

from utils.algo import join_words, permutation
from utils.print_utils import shift

class DepNode:
    def __init__(self, deprel=None, parent=None, postag=None, word=None):
        self.index = 0
        self.deprel = deprel
        self.parent = parent
        self.postag = postag
        self.word = word
        self.children = []

    def collect_nodes(self):
        """
        找出所有的节点（当前节点和自己的）
        :return: [DepNode]
        """
        yield self
        for c in self.children:
            for n in c.collect_nodes():
                yield n

    def find_root(self):
        """
        找到根节点
        :return: DepNode
        """
        if not self.parent:
            return self
        return self.parent.find_root()

    def is_splitable_coo(self, child):
        if child.deprel != 'COO':
            return False
        if self.deprel == 'VOB':
            return True
        return self.postag in ('n', 'nz', 'i', 'ws', 'nx') and child.postag in ('n', 'nz', 'i', 'ws', 'nx')

    def clone(self):
        me = deepcopy(self)
        me.children = [child.clone() for child in self.children]
        for child in me.children:
            child.parent = me
        return me

    def add_child(self, child):
        child.parent = self
        if child not in self.children:
            self.children.append(child)

    def borrow_sibling_level(self):
        if not self.parent:
            return
        if self.parent.deprel == 'VOB':
            return

        for child in self.children:
            if child.deprel == 'ATT' and child.postag in ('a', 'd', 'p'):
                return
        for child in self.parent.children:
            if child != self and child.deprel == 'ATT' and child.postag == 'a':
                self.add_child(child.clone())
                return

    def can_truncate(self, child):
        if child.index == 0 and child.postag == 'c':
            return True

        if child.deprel in ('WP', 'APP'):  # or child.postag == 'c':
            return True

        if not child.children:
            if child.deprel in ('WP',):  # 'ADJ'):
                return True

            if child.deprel in ('ADJ',) and child.postag == 'u':
                return True

        if self.deprel == 'COO' and child.deprel in ('LAD',):
            return True

        if child.deprel == 'RAD' and child.word in ('等',):
            return True

        if self.deprel == 'COO' and self.postag in ('n',):
            if child.deprel == 'COO' and child.postag == 'c':
                return True

        return False

    def is_sbv(self):
        return self.contains_children(deprels=['SBV'])

    def can_merge(self, c):
        if c.deprel == 'ATT':
            if c.postag in ('nd', 'a'):
                return False
        if c.deprel == 'ADV':
            return False
        if not c.children:
            if c.deprel in ('ATT', 'ADV',) and self.is_neighbour(c):
                return True
        if c.deprel == 'COO':
            if (self.postag in ('n', 'ws', 'nz', 'i')) or c.is_sbv():
                return False
            return self.is_neighbour(c)
        return False

    def find_ic(self):
        """
        找出独立子句(IC)
        :return: AttrNode
        """
        if self.deprel in ('IC', 'IS'):
            self.disconnect()
            return self
        for c in self.children:
            ic = c.find_ic()
            if ic is not None:
                return ic
        return None

    def disconnect(self):
        """
        当前节点脱离父节点，成为独立语句
        :return: None
        """
        if self.parent:
            self.parent.children.remove(self)
        self.parent = None
        self.deprel = 'HED'

    def cut_into_ics(self):
        ics = [self]
        while True:
            expr = self.find_ic()
            if expr is not None:
                ics.append(expr)
            else:
                return ics

    def reform_and_compact(self):
        self.compact_index()
        while self.reform_one():
            self.compact_index()

    def min_index(self):
        return min([n.index for n in self.collect_nodes()])

    def max_index(self):
        return max([n.index for n in self.collect_nodes()])

    def reform_one(self):
        for c in self.children:
            if self.can_truncate(c):
                self.children.remove(c)
                return True
            # if self.can_merge(c):
            #     self.merge_word_of(c)
            #     self.children.remove(c)
            #     return True
            if c.reform_one():
                return True
        return False

    def is_neighbour(self, child):
        return (self.index == child.min_index() - 1) or (self.index == child.max_index() + 1)

    def merge_word_of(self, other):
        ns = sorted([self] + list(other.collect_nodes()), key=lambda n: n.index)
        self.word = join_words([n.word for n in ns])

    def compact_index(self):
        old_indexes = [n.index for n in self.collect_nodes()]
        old_indexes.sort()
        new_index_map = dict([(old, new) for (new, old) in enumerate(old_indexes)])

        def compact(n):
            n.index = new_index_map[n.index]
            for c in n.children:
                compact(c)

        compact(self)

    def seek_children(self, postags=None, deprels=None):
        if postags is None:
            if deprels is None:
                return []
            else:
                return [c for c in self.children if c.deprel in deprels]
        else:
            if deprels is None:
                return [c for c in self.children if (c.postag in postags)]
            else:
                return [c for c in self.children if (c.deprel in deprels) and (c.postag in postags)]

    def contains_children(self, postags=None, deprels=None):
        return len(self.seek_children(postags, deprels)) > 0

    def level(self):
        if self.postag == 'v':
            for c in self.children:
                if c.deprel == 'ADV' and c.postag == 'a':
                    return str(c)
            return self.word

        if self.postag == 'n':
            for c in self.children:
                if c.deprel in ('ADV', 'ATT') and c.postag in ('a', 'nd', 'v'):
                    return str(c)
            return self.word

        if self.contains_children(deprels=['SBV']):
            return self.word

        if self.postag == 'i':
            return self.word

        return None

    def subject(self):
        if not self.children:
            return self.word
        for c in self.children:
            if c.deprel in ('VOB', 'FOB'):
                sub = deepcopy(self)
                sub.reform_for_sub()
                return str(sub)
        for c in self.children:
            if c.deprel == 'SBV':
                sub = deepcopy(c)
                sub.reform_for_sub()
                return str(sub)

        if self.postag == 'n':
            sub = deepcopy(self)
            sub.reform_for_sub()
            return str(sub)

        if self.contains_children(deprels=['CMP']):
            return self.word

        return None

    def reform_for_sub(self):
        while True:
            if not self.reform_for_sub_one():
                return False

    def reform_for_sub_one(self):
        for c in self.children:

            # xx的 | xx以上
            if c.deprel in ('ATT', 'DE', 'ADV') and c.postag in ('f', 'a', 'nd'):
                self.children.remove(c)
                return True

            if c.deprel == 'DE' and any([x.postag == 'a' for x in c.children]):
                self.children.remove(c)
                return True

            if c.reform_for_sub():
                return True
        return False

    def show(self, shift_count=0):
        print(shift('{rel:4}{postag:3}{index:3}:{word}'.format(
            word=self.word,
            index=self.index,
            postag=self.postag,
            rel=self.deprel
        ), shift_count))
        for a in self.children:
            a.show(shift_count + 8)

    def split_one(self):
        if not self.children:
            yield self
        else:
            children, coos = [], []
            for child in self.children:
                if self.is_splitable_coo(child):
                    coos.append(child)
                else:
                    children.append(child)

            self.children = []
            for coo in coos:
                coo.borrow_sibling_level()
                for x in coo.split_one():
                    x.deprel = self.deprel
                    yield x

            if not children:
                yield self
            else:
                xxs = [list(c.split_one()) for c in children]
                for xs in permutation(xxs):
                    x = deepcopy(self)
                    x.children = xs
                    yield x

    def __repr__(self):
        return join_words([n.word for n in sorted(self.collect_nodes(), key=lambda x: x.index)])

    def __iter__(self):
        """
        广度优先的遍历节点及其子孙节点
        :return:
        """
        ns = [self]
        while ns:
            n = ns[0]
            ns = ns[1:] + n.children
            yield n


import asyncio
import logging
import pickle

logging.basicConfig(level=logging.DEBUG)

class Client:
    def __init__(self, pid, host='127.0.0.1', port=8888):
        self.pid = pid
        self.host = host
        self.port = port
        self.reader = None
        self.writer = None

    async def __aenter__(self):
        self.reader, self.writer = await asyncio.open_connection(
            self.host, self.port)
        logging.info(f'Connected to {(self.host, self.port)}')
        return self

    async def __aexit__(self, *args):
        logging.info(f'Disconnected from {(self.host, self.port)}')
        self.writer.close()

    async def send(self, obj):
        obj = pickle.dumps(obj)
        self.writer.write(len(obj).to_bytes(4, byteorder='big'))
        self.writer.write(obj)
        await self.writer.drain()

    async def recv(self):
        size = int.from_bytes(await self.reader.readexactly(4),
                              byteorder='big')
        obj = pickle.loads(await self.reader.read(size))
        return obj

async def coro(id_):
    async with Client(pid=id_) as client:
        for i in range(3):
            req = ('get', str(i), i)
            logging.info(f'{[client.pid]} -> {req}')
            await client.send(req)
            resp = await client.recv()
            logging.info(f'{[client.pid]} <- {resp}')

async def main():
    tasks = [asyncio.ensure_future(coro(id_)) for id_ in range(3)]
    await asyncio.gather(*tasks)

if __name__ == '__main__':
    asyncio.run(main())


from pymongo import MongoClient
from pymongo.cursor import CursorType

class MongodbSetting():
    def __init__(self):
        self.host = "localhost"
        self.port = "27017"
        self.mongo = MongoClient(self.host, int(self.port))
        print()
        print(self.mongo)
        print()

class WetterMongodb(MongodbSetting):
    def __init__(self):
        MongodbSetting.__init__(self)
        self.db = self.mongo['weather_database']
        self.weather_collection = self.db['weather']
        print(self.db)
        print(self.weather_collection)
        print()

    def alldatafind(self):
        self.data_list = []
        for self.data in self.weather_collection.find():
            # print(self.data)
            self.data_list.append(self.data)
        return self.data_list

    def wetterlistsave(self, city, info, temp, temp_min, temp_max):

        self.city = city
        self.info = info
        self.temp = temp
        self.temp_min = temp_min
        self.temp_max = temp_max
        self.weather_save_template = {
            "도시":self.city,
            "날씨":self.info,
            "온도":self.temp,
            "최저온도":self.temp_min,
            "최고온도":self.temp_max
        }
        return self.weather_save_template

    def datasave(self, time, data):
        self.time = time
        self.data = data
        self.weather_collection.save({
            "날짜":self.time,
            "일기예보":self.data
        })

a = WetterMongodb()

b = a.alldatafind()
# # weather_collection.save(b)
# # print("b : ", b)
# print()
for idx, result in enumerate(b):
    print(f"{idx}: ",result)
    # print(result['날씨'])


from netCDF4 import Dataset
import sys

from . import utilities as util

def main(cfg):
    """
    This script takes two processed inventories
    and copies all of their variables into a new one
    """

    with Dataset(cfg.inv_out,'w') as outf:
        util.prepare_output_file(cfg.cosmo_grid, cfg.nc_metadata, outf)

        for inv,name in zip([cfg.inv_1,cfg.inv_2],
                            [cfg.inv_name_1,cfg.inv_name_2]):
            with Dataset(inv) as inf:
                if inv == cfg.inv_1:
                    util.add_country_mask(inf['country_ids'][:].T, outf)

                for v in inf.variables:
                    if v in ['rotated_pole','rlon','rlat','country_ids','lon','lat']:
                        continue

                    var = inf[v]
                    nc_var = outf.createVariable(v+name, var.dtype, var.dimensions)
                    nc_var.units = var.units
                    nc_var.grid_mapping = var.grid_mapping
                    nc_var[:] = var[:]


import redis
import json
from message_queue.utils import redis_config, LogWrapper, _logger
import requests

logger = LogWrapper(_logger)

r = redis.Redis(**redis_config)

def run():
    while True:
        json_data = r.brpop('notification')
        ok_data = json_data[1].decode('utf-8')
        data = json.loads(ok_data)

        logger.info("data: {}".format(ok_data))

        req = requests.post('http://localhost:8000/social/save/notif/', json=data)

        logger.info("data: {}, status: {}".format(ok_data, req.status_code))

if __name__ == '__main__':
    run()


import io
import sys
import types
from thrift.protocol.TCompactProtocol import TCompactProtocolAccelerated as TCompactProtocol
from thrift.protocol.TProtocol import TProtocolException

from .parquet_thrift.parquet import ttypes as parquet_thrift
from .util import ParquetException

def read_thrift(file_obj, ttype):
    """Read a thrift structure from the given fo."""
    from thrift.transport.TTransport import TFileObjectTransport, TBufferedTransport
    starting_pos = file_obj.tell()

    # set up the protocol chain
    ft = TFileObjectTransport(file_obj)
    bufsize = 2 ** 16
    # for accelerated reading ensure that we wrap this so that the CReadable transport can be used.
    bt = TBufferedTransport(ft, bufsize)
    pin = TCompactProtocol(bt)

    # read out type
    obj = ttype()
    obj.read(pin)

    # The read will actually overshoot due to the buffering that thrift does. Seek backwards to the correct spot,.
    buffer_pos = bt.cstringio_buf.tell()
    ending_pos = file_obj.tell()
    blocks = ((ending_pos - starting_pos) // bufsize) - 1
    if blocks < 0:
        blocks = 0
    file_obj.seek(starting_pos + blocks * bufsize + buffer_pos)
    return obj

def write_thrift(fobj, thrift):
    """Write binary compact representation of thiftpy structured object

    Parameters
    ----------
    fobj: open file-like object (binary mode)
    thrift: thriftpy object to write

    Returns
    -------
    Number of bytes written
    """
    t0 = fobj.tell()
    pout = TCompactProtocol(fobj)
    try:
        thrift.write(pout)
        fail = False
    except TProtocolException as e:
        typ, val, tb = sys.exc_info()
        frames = []
        while tb is not None:
            frames.append(tb)
            tb = tb.tb_next
        frame = [tb for tb in frames if 'write_struct' in str(tb.tb_frame.f_code)]
        variables = frame[0].tb_frame.f_locals
        obj = variables['obj']
        name = variables['fname']
        fail = True
    if fail:
        raise ParquetException('Thrift parameter validation failure %s'
                               ' when writing: %s-> Field: %s' % (
            val.args[0], obj, name
        ))
    return fobj.tell() - t0

def is_thrift_item(item):
    return hasattr(item, 'thrift_spec') and hasattr(item, 'read')

def thrift_print(structure, offset=0):
    """
    Handy recursive text ouput for thrift structures
    """
    if not is_thrift_item(structure):
        return str(structure)
    s = str(structure.__class__) + '\n'
    for key in dir(structure):
        if key.startswith('_') or key in ['thrift_spec', 'read', 'write',
                                          'default_spec', 'validate']:
            continue
        s = s + ' ' * offset + key + ': ' + thrift_print(getattr(structure, key)
                                                         , offset+2) + '\n'
    return s

def bind_method(cls, name, func):
    if sys.version_info.major == 2:
        setattr(cls, name, types.MethodType(func, None, cls))
    else:
        setattr(cls, name, func)

for clsname in dir(parquet_thrift):
    if clsname[0].isupper():
        cls = getattr(parquet_thrift, clsname)
        bind_method(cls, '__repr__', thrift_print)

def getstate_method(ob):
    b = io.BytesIO()
    write_thrift(b, ob)
    b.seek(0)
    return b.read()

def copy_method(self):
    cls = type(self)
    out = cls.__new__(cls)
    out.__dict__ = self.__dict__.copy()
    return out

def setstate_method(self, state):
    b = io.BytesIO(state)
    out = read_thrift(b, type(self))
    self.__dict__ = out.__dict__

for cls in [parquet_thrift.FileMetaData,
            parquet_thrift.RowGroup,
            parquet_thrift.ColumnChunk]:
    bind_method(cls, '__getstate__', getstate_method)
    bind_method(cls, '__setstate__', setstate_method)
    bind_method(cls, '__copy__', copy_method)


import datetime
import logging
import urllib

import socorro.lib.util as util
import socorro.webapi.webapiService as webapi
import socorro.database.database as db
from socorro.lib.datetimeutil import UTC, string_to_datetime

logger = logging.getLogger("webapi")

#=================================================================================================================
class EmailCampaignVolume(webapi.JsonServiceBase):
  """ Hoopsnake API which estimates the total volume of emails
      a given campaign will generate. A campaign is based on:
      * Product
      * Crash Signature
      * Date Range

      The API returns a raw number
  """
  #-----------------------------------------------------------------------------------------------------------------
  def __init__(self, configContext):
    super(EmailCampaignVolume, self).__init__(configContext)
    self.database = db.Database(configContext)

  #-----------------------------------------------------------------------------------------------------------------
  # curl http://localhost:8085/emailcampaigns/volume/p/Firefox/v/4.0b6/sig/js_FinishSharingTitle/start/2010-06-05/end/2010-06-13
  "/emailcampaigns/volume/p/{product}/v/{versions}/sig/{signature}/start/{start_date}/end/{end_date}"
  uri = '/emailcampaigns/volume/p/(.*)/v/(.*)/sig/(.*)/start/(.*)/end/(.*)'

  #-----------------------------------------------------------------------------------------------------------------
  def get(self, *args):
    " Webpy method receives inputs from uri "
    stringListFromCSV = lambda s: tuple([x.strip() for x in s.split(',')])
    convertedArgs = webapi.typeConversion([str, stringListFromCSV, str, string_to_datetime, string_to_datetime], args)
    parameters = util.DotDict(zip(['product', 'versions', 'signature', 'start_date', 'end_date'], convertedArgs))

    connection = self.database.connection()
    try:
      return {'emails': self.estimate_campaign_volume(connection, parameters)}
    finally:
      connection.close()

  #-----------------------------------------------------------------------------------------------------------------
  def estimate_campaign_volume(self, connection, parameters):
    " Queries reports tables for a count of unique email addresses "

    # Yeah, it isn't a half day or anything like that -- lumbergh
    # start date at 00:00:00 and end date at 23:59:59
    end_date = parameters['end_date']
    parameters['end_date'] = datetime.datetime(end_date.year, end_date.month, end_date.day, 23, 59, 59, tzinfo=UTC)

    encoded_signature = parameters['signature']
    parameters['signature'] = urllib.unquote_plus(encoded_signature)

    parameters['version_clause'] = ''
    if len(parameters['versions']) > 0:
      parameters['version_clause'] = " version IN %(versions)s AND "

    sql = """
        SELECT count(distinct email) as total FROM reports
        WHERE TIMESTAMP WITH TIME ZONE '%(start_date)s' <= reports.date_processed AND
              TIMESTAMP WITH TIME ZONE '%(end_date)s' > reports.date_processed AND
              product = %%(product)s AND
              %(version_clause)s
              signature = %%(signature)s AND
              email IS NOT NULL AND
              email ~ '.*@.*\.[a-zA-Z]{2,4}';
    """ % parameters
    cursor = connection.cursor()
    logger.info(cursor.mogrify(sql, parameters))
    cursor.execute(sql, parameters)
    return cursor.fetchone()[0]


# TODO: implement fully convolution encoder and decoder for image compression
# date: 2018-04-25
# author: bta1489@gmail.com

import tensorflow as tf
import numpy as np
import tensorflow.contrib.slim as slim
from functools import partial

# ------------------
# Define leak relu
# ------------------
def leak_relu(x, leak, scope=None):
    with tf.name_scope(scope, 'leak_relu', [x, leak]):
        if leak < 1:
            y = tf.maximum(x, leak * x)
        else:
            y = tf.minimum(x, leak * x)
        return y

# ------------------------
# Define standard layer
# ------------------------
batch_norm = partial(slim.batch_norm, decay=0.9, scale=True, epsilon=1e-5, updates_collections=None)
conv = partial(slim.conv2d, activation_fn=None, weights_initializer=tf.truncated_normal_initializer(stddev=0.02))
dconv = partial(slim.conv2d_transpose, activation_fn=None, weights_initializer=tf.random_normal_initializer(stddev=0.02))
relu = tf.nn.relu
lrelu = partial(leak_relu, leak=0.2)

# --------------------------------------------------------------------------------------
# Define some preset model
# Model Structure:
# 	n_layers: number of convolution layers
# 	n_filter_each_layers: number of filters (channels) in each layer
# 	n_kernel_each_layers: size of kernel in each layer, i.e 3 x 3
# 	n_stride_each_layers: stride value in each layer, i.e 2
# NOTE:
# 	last layer size is the output channel size, in encoder and decoder will be different.
# 		i.e. encoder is 64 channels, but the decoder always is 3
#
# ---------------------------------------------------------------------------------------
class model_param(object):

	def __init__(self, preset_model):
		self.preset_model = preset_model
		self.get_param()

	def model_5x_encode(self):
		self.n_layers = 5
		self.n_filter_each_layers = [64, 128, 256, 512, 16]
		self.n_kernel_each_layers = [3, 3, 3, 3, 1]
		self.n_stride_each_layers = [2, 2, 2, 2, 1]

	def model_5x_decode(self):
		self.n_layers = 5
		self.n_filter_each_layers = [512, 256, 128, 64, 3]
		self.n_kernel_each_layers = [1, 3, 3, 3, 3]
		self.n_stride_each_layers = [1, 2, 2, 2, 2]

	def get_param(self):

		if self.preset_model == 'FullyConv_5x_encode':
			self.model_5x_encode()
		elif self.preset_model == 'FullyConv_5x_decode':
			self.model_5x_decode()
		else:
			raise ValueError

		print('Get Model %s - Done !' % (self.preset_model))
		print('n_layers', self.n_layers)
		print('n_filter_each_layers', self.n_filter_each_layers)
		print('n_kernel_each_layers', self.n_kernel_each_layers)
		print('n_stride_each_layers', self.n_stride_each_layers)

# --------------------------------
# Build Fully Convolution Encoder
# --------------------------------
def build_fc_encoder(inputs, preset_model, dropout_p=0.2, scope=None, reuse=True, is_training=True):

	# -----------------
	# Get model params
	# -----------------
	model = model_param(preset_model=preset_model + '_encode')
	n_layers = model.n_layers
	n_filter = model.n_filter_each_layers
	n_kernel = model.n_kernel_each_layers
	n_stride = model.n_stride_each_layers

	# ----------------------
	# Define standard module
	# ----------------------
	bn = partial(batch_norm, is_training=is_training)
	conv_bn_lrelu = partial(conv, normalizer_fn=bn, activation_fn=lrelu, biases_initializer=None)

	with tf.variable_scope(scope, reuse=reuse):

		print('build_fc_encoder, inputs size', inputs.get_shape().as_list())
		# First convolution
		y = lrelu(conv(inputs, n_filter[0], n_kernel[0], n_stride[0]))
		print('build_fc_encoder, y - 0', inputs.get_shape().as_list())

		# Second to second last layers
		for i in range(1, n_layers-1):
			y = conv_bn_lrelu(y, n_filter[i], n_kernel[i], n_stride[i])
			if dropout_p != 0.0:
				y = slim.dropout(y, keep_prob=(1.0-dropout_p))
			print('build_fc_encoder, y - %d' % (i), y.get_shape().as_list())

		# last layers
		y = conv(y, n_filter[-1], n_kernel[-1], n_stride[-1])
		print('build_fc_encoder, y - %d last layer' % (n_layers-1), y.get_shape().as_list())

		return y

# -----------------------------------
# Build Fully Convolution Decoder
# -----------------------------------
def build_fc_decoder(inputs, preset_model, dropout_p=0.2, scope=None, reuse=True, is_training=True):

	# -------------------
	# Get model params
	# -------------------
	model = model_param(preset_model=preset_model + '_decode')
	n_layers = model.n_layers
	n_filter = model.n_filter_each_layers
	n_kernel = model.n_kernel_each_layers
	n_stride = model.n_stride_each_layers

	# -----------------------
	# Define standard modules
	# -----------------------
	bn = partial(batch_norm, is_training=is_training)
	dconv_bn_relu = partial(dconv, normalizer_fn=bn, activation_fn=relu, biases_initializer=None)

	y = inputs
	with tf.variable_scope(scope, reuse=reuse):

		print('build_fc_decoder, inputs size', inputs.get_shape().as_list)
		# First to second last layers
		for i in range(0, n_layers-1):
			y = dconv_bn_relu(y, n_filter[i], n_kernel[i], n_stride[i])
			if dropout_p != 0.0:
				y = slim.dropout(y, keep_prob=(1.0-dropout_p))
			print('build_fc_decoder, y - %d' % (i), y.get_shape().as_list())

		# last layers
		y = dconv(y, n_filter[-1], n_kernel[-1], n_stride[-1])
		print('build_fc_decoder, y - %d last layers' % (n_layers-1), y.get_shape().as_list())

		return tf.tanh(y)


### Title: WebMapBackup
### Description: THIS TOOL MUST BE RUN IN PYTHON 3
###              The intent of this tool is to back up a copy
###              of a web map into a designated, pre-created folder
###
### Company: Foresite Group
### Author: Darren Foster

print ("Running... \n The imports will take a few moments...")
print("\n")

from arcgis.gis import GIS
from IPython.display import display
from arcgis.mapping import WebMap
import datetime

day = datetime.date.today()
today = day.strftime("%m_%d_%y")

#Provide log-in credentials for authentication#
gis = GIS(url = "***", \
          username = "******", \
          password = "******")

#Specify the desired existing web map using the query and item type#
webmap_search = gis.content.search(query = "Knoxville_QC_Map_Live", item_type = "Web Map")

#There should only be one result, but you still must specify its index, which is [0]#
webmap_result = webmap_search[0]

#Set the Title of the web map#
name = webmap_result.title

print ("The title of the map is " + name)
print("\n")

#Place the search result into the WebMap class#
webmap = WebMap(webmap_result)

#Collect and display a list of the layers within the web map#
layersList = webmap.layers

print ("Layers: \n")

for item in layersList:
    print (item.title)

print("\n")

#Establish the required Item Properties#
itemProperties = {'title':'Knoxville_QC_Map_Live_BACKUP_' + today,
                   'snippet':'Backup Knoxville_QC_Map_Live map created using Python API',
                   'tags':['automation', 'Backup']}

#Save the web map as a copy into the desired folder#
webmap.save(item_properties = itemProperties, folder = "Backup Web Maps")

print ("Backup Complete")


import sys

from multiprocessing import Process
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QComboBox
from PyQt5.QtWidgets import QDialog
from PyQt5.QtWidgets import QInputDialog
from PyQt5.QtGui import QFont
from PyQt5.QtGui import QStandardItemModel, QStandardItem
from PyQt5.QtCore import Qt
from Image_Manipulation.artScreen import artScreen
from Image_Manipulation.randomArt import randomArt
import stroopy_words
import record

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        appName = 'RemBRAINdt'

        self.setWindowTitle(appName)
        self.resize(800, 400)
        self.setStyleSheet("QMainWindow { background-color: black; color: white }")

        label = QLabel(appName)
        label.setFont(QFont('Arial', 20))
        label.setStyleSheet("QLabel { color: white}")
        label.setAlignment(Qt.AlignCenter)

        btn1 = QPushButton('Set Baseline')
        btn2 = QPushButton('Start')
        btn1.setStyleSheet("QPushButton { max-width: 15em}")
        btn2.setStyleSheet("QPushButton { max-width: 15em}")

        beta = QLabel('β')
        beta.setStyleSheet("QLabel { color: white}")
        beta.setFont(QFont('Arial', 22))
        alpha = QLabel('α')
        alpha.setStyleSheet("QLabel { color: white}")
        alpha.setFont(QFont('Arial', 22))
        theta = QLabel('θ')
        theta.setStyleSheet("QLabel { color: white}")
        theta.setFont(QFont('Arial', 22))
        delta = QLabel('δ')
        delta.setStyleSheet("QLabel { color: white}")
        delta.setFont(QFont('Arial', 22))

        def addFeatures(self):
            for f in features:
                self.addItem(f)

        artFeatures1 = QComboBox()
        addFeatures(artFeatures1)

        artFeatures2 = QComboBox()
        addFeatures(artFeatures2)

        artFeatures3 = QComboBox()
        addFeatures(artFeatures3)

        artFeatures4 = QComboBox()
        addFeatures(artFeatures4)

        currentStates = [0, 1, 2, 3]
        print(currentStates)

        def setStates():
            artFeatures1.setCurrentIndex(currentStates[0])
            artFeatures2.setCurrentIndex(currentStates[1])
            artFeatures3.setCurrentIndex(currentStates[2])
            artFeatures4.setCurrentIndex(currentStates[3])

        setStates()

        def changeFeatures(index):
            if index == 0:
                wave = artFeatures1
            if index == 1:
                wave = artFeatures2
            if index == 2:
                wave = artFeatures3
            if index == 3:
                wave = artFeatures4
            new = wave.currentIndex() # set new = 1
            old = currentStates[index] #get old value
            change = currentStates.index(new) #find last position of 1
            currentStates[change] = old # change found position to old value in map
            currentStates[index] = new # change old value to new value in map
            print(currentStates)
            setStates()

        artFeatures1.currentIndexChanged.connect(lambda: changeFeatures(0)) #if 0 changed to 1
        artFeatures2.currentIndexChanged.connect(lambda: changeFeatures(1)) #if 0 changed to 1
        artFeatures3.currentIndexChanged.connect(lambda: changeFeatures(2)) #if 0 changed to 1
        artFeatures4.currentIndexChanged.connect(lambda: changeFeatures(3)) #if 0 changed to 1

        layout1 = QVBoxLayout()

        layout2 = QHBoxLayout()

        layout2b = QGridLayout()
        layout2b.addWidget(beta, 0, 0)
        layout2b.addWidget(alpha, 1, 0)
        layout2b.addWidget(theta, 2, 0)
        layout2b.addWidget(delta, 3, 0)
        layout2b.addWidget(artFeatures1, 0, 1)
        layout2b.addWidget(artFeatures2, 1, 1)
        layout2b.addWidget(artFeatures3, 2, 1)
        layout2b.addWidget(artFeatures4, 3, 1)

        layout2.addLayout(layout2b)

        layout3 = QHBoxLayout()
        layout3.addWidget(btn1)
        layout3.addWidget(btn2)

        layout1.addWidget(label)
        layout1.addLayout(layout2)
        layout1.addLayout(layout3)

        self.widget = QWidget()
        self.widget.setLayout(layout1)
        self.setCentralWidget(self.widget)

        btn1.clicked.connect(self.RecordBaseline)
        btn2.clicked.connect(self.open_artScreen)

    def open_artScreen(self):
        art_screen_size = [526,526]
        screen = artScreen(art_screen_size)
        screen.updateScreen(randomArt(art_screen_size))

    def RecordBaseline(self):
        default = 120
        minTime = 5
        maxTime = 300
        increment = 5
        time,ok = QInputDialog.getInt(self, "Enter recording duration", "Time (s):", default, minTime, maxTime, increment)
        if ok:
            stimulus = Process(target=stroopy_words.present, args=(time,))
            recording = Process(target=record.record, args=(time, 'data\csvtest.txt'))
            stimulus.start()
            recording.start()

features = ["Feature 1",
            "Feature 2",
            "Feature 3",
            "Feature 4",
            ]

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec())


# coding=utf-8
from django.conf import settings
from django.db import models
from django.template.loader import render_to_string
from django.utils import translation
from feincms.content.richtext.models import RichTextContent
from feincms.module.page.extensions.navigation import NavigationExtension
from feincms.module.page.models import Page
from django.utils.translation import ugettext_lazy as _
from gallery.models import CatalogContent

Page._meta.verbose_name = u'CMS'
Page._meta.verbose_name_plural = u'CMS'

Page.register_templates(
    {
        'title': u'Szablon strony głównej',
        'path': 'cms/home.html',
        'regions': (
            ('content_about', u'content_about'),
            ('content_contact', u'content_contact'),
            ('content_gallery', u'content_gallery'),
            ('content_demo', u'content_demo'),
            ('character', u'postać'),
        ),
    }, {
        'title': u'Szablon prosty',
        'path': 'cms/simple.html',
        'regions': (
            ('main', u'main'),
        ),
    }, {
        'title': u'Szablon prosty z ustami w tle',
        'path': 'cms/simple_with_mouth_bg.html',
        'regions': (
            ('main', u'main'),
        ),
    }, {
        'title': u'Rączki',
        'path': 'cms/hands.html',
        'regions': (
            ('left', u'left'),
            ('right', u'right'),
        ),
    }, {
        'title': u'Usta',
        'path': 'cms/lips.html',
        'regions': (
            ('left', u'left'),
            ('right', u'right'),
        ),
    }, {
        'title': u'Stopy',
        'path': 'cms/foots.html',
        'regions': (
            ('left', u'left'),
            ('right', u'right'),
        ),
    },
)

Page.create_content_type(RichTextContent)
Page.create_content_type(CatalogContent)

class CurrentLanguageOnly(NavigationExtension):
    name = 'show entries in current language only'

    def children(self, page, **kwargs):
        return page.children.in_navigation().filter(language=translation.get_language())

Page.register_extensions('feincms.module.extensions.translations', 'feincms.module.page.extensions.navigation')

class CharacterContent(models.Model):
    name = models.CharField(_('name'), max_length=80, blank=True)
    mouth = models.ForeignKey(Page, related_name=_('mouth'), verbose_name=_('mouth'))
    body = models.ForeignKey(Page, related_name=_('body'), verbose_name=_('body'))
    hands = models.ForeignKey(Page, related_name=_('hands'), verbose_name=_('hands'))
    foots = models.ForeignKey(Page, related_name=_('foots'), verbose_name=_('foots'))

    class Meta:
        abstract = True
        verbose_name = _('Character content')
        verbose_name_plural = _('Characters contents')

    def render(self, **kwargs):
        return render_to_string('cms/_character.html', {'character': self, 'STATIC_URL': settings.STATIC_URL})

Page.create_content_type(CharacterContent)


import os
from collections import *

from utils import get_actual
from utils import *

class Halt(Exception):
    pass

def run_input(pos, l, input_=0):
    opcode = l[pos]

    if opcode == 99:
        raise Halt("found 99, exiting")

    if opcode >= 100:
        inp = str(opcode)
        while len(inp) < 5:
            inp = f"0{inp}"
        opcode = int(inp[-2:])
        pa, pb, pc = inp[:-2]
    else:
        pa, pb, pc = "", "", ""
    del pa

    if opcode in (1, 2):
        pos1 = l[pos + 1]
        pos2 = l[pos + 2]
        pos3 = l[pos + 3]
        c = l[pos1] if pc != "1" else pos1
        b = l[pos2] if pb != "1" else pos2
        l[pos3] = c + b if opcode == 1 else c * b

        if pos3 != pos:
            return pos + 4

        return l[pos3]

    if opcode == 3:
        pos1 = l[pos + 1]
        l[pos1] = input_

        if pos1 != pos:
            return pos + 2

        return l[pos1]

    if opcode == 4:
        pos1 = l[pos + 1]
        output = l[pos1] if pc != "1" else pos1
        print(f"value is {output}")

        return pos + 2

    if opcode in (5, 6):
        pos1 = l[pos + 1]
        pos2 = l[pos + 2]
        if opcode == 5:
            check = l[pos1] if pc != "1" else pos1
            if check != 0:
                l[pos] = l[pos2] if pb != "1" else pos2
                return l[pos]

        if opcode == 6:
            check = l[pos1] if pc != "1" else pos1
            if check == 0:
                l[pos] = l[pos2] if pb != "1" else pos2
                return l[pos]

        return pos + 3

    if opcode in (7, 8):
        pos1 = l[pos + 1]
        pos2 = l[pos + 2]
        pos3 = l[pos + 3]

        if opcode == 7:
            c = l[pos1] if pc != "1" else pos1
            b = l[pos2] if pb != "1" else pos2
            l[pos3] = 1 if c < b else 0

        if opcode == 8:
            c = l[pos1] if pc != "1" else pos1
            b = l[pos2] if pb != "1" else pos2
            l[pos3] = 1 if c == b else 0

        if pos3 != pos:
            return pos + 4

        return l[pos3]

if __name__ == "__main__":
    day = os.path.dirname(os.path.abspath(__file__)).rsplit("/", 1)[-1]
    _input = get_actual(day=int(day), year=2019)

    _input = ints(_input)

    try:
        pos = 0
        while True:
            pos = run_input(pos, _input, 5)
    except Halt as e:
        print(e)


import time
import sys
import threading

from neopixel import NeoPixel
from .tools import cmath

from .animations import *

class AnimatorController():
    def __init__(self,
                pixels: NeoPixel,
                animation: str = "sunrise",
                update_rate: int = 60):

        super(AnimatorController, self)
        self._stop = threading.Event()

        self._pixels = pixels

        self.FPS = update_rate

        self._update_time = (1.0/update_rate)
        self._running = False
        self._animation = self.__get_animation(animation)
        self._interlace = False

        self._thread = threading.Thread(target=self.__update)
        self._thread.daemon = True

        self._thread.setName("Update pixels Cycle")

    def run(self):
        """ Start the animator controller routine"""
        try:
            self._running = True
            self._thread.start()
        except (KeyboardInterrupt, SystemExit):
            print ('\n! Received keyboard interrupt, quitting threads.\n')

    def stop(self):
        """ Stop the animator controller routine"""
        self._running = False
        self._thread.join()

    def __update(self):
        """ Update the state of animation and draw it into pixels"""

        self._animation.start()
        while self._running:
            if not self._animation.loop and self._animation.done:
                self.stop()
                break

            # Starting the execution cycle
            start_cycle_time = time.process_time()

            delta_time = 1.0 / self.FPS
            self._animation.update(delta_time)
            self._pixels.show()

            # Ending the execution cycle
            end_cycle_time = time.process_time()

            cycle_time = (end_cycle_time - start_cycle_time)
            self.__calc_FPS(cycle_time + self.__wait_next_update(cycle_time))
            self.__print_FPS()

    def __wait_next_update(self, cycle_time):
        """
        Wait for next update cycle if the executation cycle was executed quicker
        than refresh rate
        """
        waitTime = cmath.clamp(self._update_time - cycle_time)

        if(waitTime > 0):
            time.sleep(waitTime)

        return waitTime

    def __calc_FPS(self, cycle_time):
        """ Calculate the 'Frames' per second """
        self.FPS = int(1.0/cycle_time)

    def __print_FPS(self):
        """ Print the FPS in console """
        sys.stdout.write(f"\rFPS: {self.FPS}")
        sys.stdout.flush()

    def __get_animation(self, animation: str):
        animations_list = {
            "fire"          : FireAnimation(self._pixels),
            "fade"          : FadeAnimation(self._pixels),
            "pingpong"      : PingPongAnimation(self._pixels),
            "test"          : TestColorsAnimation(self._pixels),
            "rainbow"       : RainbowPerlinAnimation(self._pixels),
            "rainbowwheel"  : RainbowWheelAnimation(self._pixels),
            "sunrise"       : SunriseAnimation(self._pixels)
        }

        return animations_list[str.lower(animation)]


from nltk.tokenize import word_tokenize

example = "고기를 아무렇게나 구우려고 하면 안 돼. 고기라고 다 같은 게 아니거든. 예컨대 삼겹살을 구울 때는 중요한 게 있지."
stop_words = "아무거나 아무렇게나 어찌하든지 같다 비슷하다 예컨대 이럴정도로 하면 아니거든".split()

token_words = word_tokenize(example)
result_words = [word for word in token_words if not word in stop_words]

print(token_words)
print(result_words)


import numpy as np
from six.moves import range
import splinart as spl

img_size, channels = 1000, 4
img = np.ones((img_size, img_size, channels), dtype=np.float32)

nb_circles = 10
theta_circles = []
path_circles = []
color_circles = []
for i in range(nb_circles):
    radius = .1 + np.random.random()*.1
    center = .2 + np.random.random(2)*.6
    theta, path = spl.circle(center, radius, npoints=75)
    theta_circles.append(theta)
    path_circles.append(path)
    color = np.random.random(4)*.3
    color[-1] = 1.
    color_circles.append(color)

def xs_func():
    nsamples = 500
    return (np.random.random() + 2 * np.pi * np.linspace(0, 1, nsamples))%(2*np.pi)

for i in range(nb_circles):
    img1 = np.ones_like(img)
    spl.update_img(img1, path_circles[i], xs_func, nrep=1000, scale_value=.00005, x=theta_circles[i], color=color_circles[i])
    mask = img1 < 1.
    img[mask] += img1[mask]

spl.save_img(img, './output', 'circles.png')


from tensorflow.python import keras

from keras import models
from keras import layers
# need slurm
# Keras with data augmentation

model = models.Sequential()
#search for 128 to correct, change channel number, but check
#clone the paths on the crc, and dont change the paths
#add additional models to see if it makes sense, search "keras sequencial"
#calibration
#Tenserflow for questions
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape=(128,128,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dropout(0.2))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

print(model.summary())

from keras import optimizers

model.compile(loss='binary_crossentropy',
  optimizer=optimizers.RMSprop(lr=1e-5),
  metrics=['acc'])

import os
#change paths
train_dir = 'o4images/train'
validation_dir = 'o4images/validation'
test_dir = 'o4images/test'

train_o4neg_dir = os.path.join(train_dir, 'o4neg')
train_o4pos_dir = os.path.join(train_dir, 'o4pos')
train_size = len(os.listdir(train_o4pos_dir)) + len(os.listdir(train_o4neg_dir))

analysis_name = 'o4counter_wAug_5.1'

batch_size = 200
epochs = 100

from keras.preprocessing.image import ImageDataGenerator

train_datagen = ImageDataGenerator(
  rescale=1./255,
  rotation_range=90,
  horizontal_flip=True,
  fill_mode='nearest')
#check out rescale and what it does
validation_datagen = ImageDataGenerator(
  rescale=1./255)

train_generator = train_datagen.flow_from_directory(
  train_dir,
  #Need to crop all to same size, add smaller
  target_size=(128,128),
  batch_size=batch_size,
  class_mode='binary')

validation_generator = validation_datagen.flow_from_directory(
  validation_dir,
  target_size=(128,128),
  batch_size=batch_size,
  class_mode='binary')

history = model.fit_generator(
  train_generator,
  steps_per_epoch=int(5*train_size/batch_size), # oversample 5x
  epochs=epochs,
  validation_data=validation_generator,
  validation_steps=50)

model.save(analysis_name + '.h5')

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

# output results as csv
import csv
filename = 'results_' + analysis_name + '.csv'
with open(filename,'w',newline='') as f:
    w = csv.writer(f)
    w.writerow(['acc','val_acc','loss','val_loss'])
    for i in range(len(history.history['acc'])):
        w.writerow([acc[i], val_acc[i], loss[i], val_loss[i]])

# Evaluate model
test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = test_datagen.flow_from_directory(
  test_dir,
  target_size=(128,128),
  batch_size=20,
  class_mode='binary')

test_loss, test_acc = model.evaluate_generator(test_generator, steps=50)
print('test acc:', test_acc)


#!/usr/bin/env python

import os
import sys
import json
import re

sys.path.append(os.path.join(os.environ['REPO_DIR'], 'utilities'))
from metadata import *
from utilities2015 import *

import argparse

parser = argparse.ArgumentParser(
    formatter_class=argparse.RawDescriptionHelpFormatter,
    description='Align consecutive images. Possible bad alignment pairs are written into a separate file.')

parser.add_argument("output_dir", type=str, help="output dir. Files for each pairwise transform are stored in sub-folder <currImageName>_to_<prevImageName>.")
parser.add_argument("kwargs_str", type=str, help="json-encoded list of dict (keyworded inputs). Each dict entry has four keys: prev_fp (previous image file path), curr_fp (current image file path).")
parser.add_argument("-p", "--param_fp", type=str, help="elastix parameter file path", default=None)
parser.add_argument("-r", help="re-generate even if already exists", action='store_true')

args = parser.parse_args()

output_dir = args.output_dir
kwargs_str = json.loads(args.kwargs_str)
param_fp = args.param_fp
regenerate = args.r

if param_fp is None:
    param_fp = os.path.join(os.environ['REPO_DIR'], 'preprocess', 'parameters', "Parameters_Rigid_MutualInfo.txt")

failed_pairs = []

for kwarg in kwargs_str:

    prev_img_name = kwarg['prev_img_name']
    curr_img_name = kwarg['curr_img_name']
    prev_fp = kwarg['prev_fp']
    curr_fp = kwarg['curr_fp']

    output_subdir = os.path.join(output_dir, curr_img_name + '_to_' + prev_img_name)

    if os.path.exists(output_subdir) and 'TransformParameters.0.txt' in os.listdir(output_subdir):
        sys.stderr.write('Result for aligning %s to %s already exists.\n' % (curr_img_name, prev_img_name))
        if not regenerate:
            sys.stderr.write('Skip.\n' % (curr_img_name, prev_img_name))
            continue

    execute_command('rm -rf \"%s\"' % output_subdir)
    create_if_not_exists(output_subdir)

    ret = execute_command('%(elastix_bin)s -f \"%(fixed_fp)s\" -m \"%(moving_fp)s\" -out \"%(output_subdir)s\" -p \"%(param_fp)s\"' % \
            {'elastix_bin': ELASTIX_BIN,
            'param_fp': param_fp,
            'output_subdir': output_subdir,
            'fixed_fp': prev_fp,
            'moving_fp': curr_fp
            })

    if ret == 1:
        failed_pairs.append((prev_img_name, curr_img_name))
    else:
        with open(os.path.join(output_subdir, 'elastix.log'), 'r') as f:
            t = f.read()
            g = re.search("Final metric value  = (.*?)\n", t)
            assert g is not None
            metric = float(g.groups()[0])
            sys.stderr.write("Metric = %.2f\n" % metric)

import subprocess
hostname = subprocess.check_output("hostname", shell=True).strip()

if len(failed_pairs) > 0:
    with open(os.path.join(output_dir, 'intra_stack_alignment_failed_pairs_%s.txt' % (hostname.split('.')[0])), 'w') as f:
        for pf, cf in failed_pairs:
            f.write(pf + ' ' + cf + '\n')


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 22 16:14:51 2021

@author: madhavrai
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 14 14:27:05 2021

@author: madhavrai
"""
import requests

import cryptocompare
from web3 import Web3
import math

def get_price(symbol , token):
    symbol = symbol.upper()
    a = cryptocompare.get_price(symbol, currency=token)
    return float(a[symbol][token])

def buy_token2(amountToken1=0 , amountToken2=0, token1 = "USDC" , token2="ETH"):
  token1 = token1.upper()

  token2= token2.upper()

  token_map = {"USDC":["0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", 6], "USDT" : ["0xdac17f958d2ee523a2206206994597c13d831ec7",6] , "WBTC":["0x2260fac5e5542a773aa44fbcfedf7c193bc2c599",8],"ETH":["0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",18]}

  decimals_1 = token_map[token1][1]

  address_1 = token_map[token1][0]

  decimals_2 = token_map[token2][1]

  address_2 = token_map[token2][0]
  amount = amountToken1
  price = get_price(token2,token1)
  print(price)
  if amountToken2>0:
    amount = amount + amountToken2*price

  amount = str(int(amount*10**decimals_1))

  r= requests.get('https://api.1inch.exchange/v3.0/1/quote?fromTokenAddress=' + address_1 + "&toTokenAddress="  + address_2 +  "&amount=" + amount).json()

  if token2!="ETH":
    token2 = float(r["toTokenAmount"]) - float(r["estimatedGas"]*10**(decimals_2-9))*get_price("ETH",token2)
  else:
    token2 = float(r["toTokenAmount"]) - float(r["estimatedGas"]*10**9)

  token1 = float(r["fromTokenAmount"])

  executed_price = token1*10**(decimals_2-decimals_1)/token2
  print(address_1)

  slippage = (1-price/executed_price)*100
  implied_volatility_daily = 5

  implied_volatility_30s = implied_volatility_daily/math.sqrt(2880)
  estimated_slippage_percent = slippage + 0.5*implied_volatility_30s
  print(estimated_slippage_percent)
  safe_slippage_percent = slippage + 6*implied_volatility_30s

  tx = get_transaction(amount , safe_slippage_percent , "buy" ,address_1 , address_2 )

  return estimated_slippage_percent, tx

def get_transaction(amount,slippage, side,address_1, address_2):
    print(slippage)

    slippage = round(slippage,3)
    #public_adress = os.environ.get("public-key")
    print(amount)
    public_adress ='0x73bceb1cd57c711feac4224d062b0f6ff338501e'#largest eth wallet added for testing purposes(can comment out public_adress = os.environ.get("public-key"))
    link = 'https://api.1inch.exchange/v3.0/1/swap?fromTokenAddress=' + address_1 + "&toTokenAddress=" + address_2  + "&amount=" + str(amount) + "&fromAddress=" + public_adress + "&slippage=" + str(slippage)
    print(link)

    r = requests.get(link)

    return r.json()

def execute_transaction(tx):
  w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/699154aa8e20430a9e615fd70f98d5cd'))

  connected = w3.isConnected()

  print(connected)

  public_adress = os.environ.get("public-key")

  private_key = os.environ.get("private-key")

  adress1 = Web3.toChecksumAddress(public_adress)

  adress2 = Web3.toChecksumAddress(tx["to"])

  nonce = w3.eth.getTransactionCount(adress1)

  print(nonce)

  tx["nonce"] = nonce

  tx["value"] = int(tx["value"])

  tx["gasPrice"] = int(tx["gasPrice"])
  tx["to"] = adress2

  signed_tx = w3.eth.account.signTransaction(tx,private_key)

  print(signed_tx)

  tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)

  return "done"

slippage, tx = buy_token2(token1="eth" , token2 = "usdc" , amountToken2 = 100000)

#a = execute_transaction(tx)

#0.5 rights side of the bell curve


import collections
import enum
import itertools
import queue

import intcode

class Robot:
    class MoveCommand(enum.IntEnum):
        NORTH = 1
        SOUTH = 2
        WEST = 3
        EAST = 4

        def opposite(self):
            if self == self.NORTH:
                return self.SOUTH
            elif self == self.SOUTH:
                return self.NORTH
            elif self == self.WEST:
                return self.EAST
            elif self == self.EAST:
                return self.WEST

    class Status(enum.IntEnum):
        WALL = 0
        MOVED = 1
        MOVED_OXYGEN = 2

    program = intcode.read_program('input.txt')

    def __init__(self):
        self.thread, self.input_queue, self.output_queue, _ = \
            intcode.intcode(self.program)

    def move(self, direction: MoveCommand) -> Status:
        self.input_queue.put(direction.value)
        return self.Status(self.output_queue.get())

class Cell(enum.Enum):
    UNKNOWN = enum.auto()
    EMPTY = enum.auto()
    WALL = enum.auto()
    OXYGEN = enum.auto()

# adapted from
# https://www.laurentluce.com/posts/solving-mazes-using-python-simple-recursivity-and-a-search/
def explore():
    grid = collections.defaultdict(
        lambda: collections.defaultdict(
            lambda: Cell.UNKNOWN))

    robot = Robot()

    oxygen_location = None

    def search(x, y, move_command=None):
        nonlocal oxygen_location

        cell = grid[y][x]
        if cell != Cell.UNKNOWN:
            return

        # Try to move into the cell
        if move_command:
            robot_status = robot.move(move_command)

            if robot_status == Robot.Status.WALL:
                grid[y][x] = Cell.WALL
                return

            if robot_status == Robot.Status.MOVED:
                grid[y][x] = Cell.EMPTY
            elif robot_status == Robot.Status.MOVED_OXYGEN:
                grid[y][x] = Cell.OXYGEN
                oxygen_location = (x, y)

        # explore neighbors
        search(x, y+1, Robot.MoveCommand.NORTH)
        search(x, y-1, Robot.MoveCommand.SOUTH)
        search(x-1, y, Robot.MoveCommand.WEST)
        search(x+1, y, Robot.MoveCommand.EAST)

        # Leave the cell
        if move_command:
            robot_status = robot.move(move_command.opposite())
            assert(robot_status == Robot.Status.MOVED)

    search(0, 0)
    return grid, oxygen_location

# explore the entire maze
board, oxygen_location = explore()

# adapted from
# https://www.redblobgames.com/pathfinding/a-star/implementation.html#python-astar
def heuristic(a, b):
    x1, y1 = a
    x2, y2 = b
    return abs(x1 - x2) + abs(y1 - y2)

def neighbors(board, location):
    x, y = location
    for x_, y_ in ((x, y+1), (x, y-1), (x-1, y), (x+1, y)):
        cell = board[y_][x_]
        if cell == Cell.EMPTY or cell == Cell.OXYGEN:
            yield (x_, y_)

def a_star_search(board, start, goal):
    frontier = queue.PriorityQueue()
    frontier.put(start, 0)
    came_from = {}
    cost_so_far = {}
    came_from[start] = None
    cost_so_far[start] = 0

    while not frontier.empty():
        current = frontier.get()

        if current == goal:
            return cost_so_far[goal]

        for next_ in neighbors(board, current):
            new_cost = cost_so_far[current] + 1
            if next_ not in cost_so_far or new_cost < cost_so_far[next_]:
                cost_so_far[next_] = new_cost
                priority = new_cost + heuristic(goal, next_)
                frontier.put(next_, priority)
                came_from[next_] = current

# use A* to determine the shortest path from (0, 0) to the oxygen system
print(a_star_search(board, (0, 0), oxygen_location))


from telegram import Bot, Update
from telegram.ext import (Updater, CommandHandler, MessageHandler,Filters)
from echo.config import TG_TOKEN,TG_API_URL

def do_start(bot:Bot, update:Update):
    bot.send_message(
        chat_id=update.message.chat_id,
        text="Привет, ответь чтонибудь"
    )

def do_echo(bot:Bot, update:Update):
    chat_id = update.message.chat_id
    text = "Ваш id = {}\n\n{}".format(chat_id, update.message.text )
    bot.send_message(
        chat_id=chat_id,
        text=text,
    )

def main():
    bot = Bot(token=TG_TOKEN, base_url=TG_API_URL)
    updater = Updater(bot=bot)

    start_handler = CommandHandler(
        "start", do_start
    )
    message_handler= MessageHandler(Filters.text, do_echo)

    updater.dispatcher.add_handler(start_handler)
    updater.dispatcher.add_handler(message_handler)

    updater.start_polling()
    updater.idle()

if __name__=="__main__":
    main()