from ajax_select.fields import AutoCompleteSelectField
from ajax_select.fields import AutoCompleteSelectMultipleField
from ajax_select import make_ajax_field
from django import forms
from django.forms import Form, ModelForm
from django.contrib import messages
from newsroom.models import Article, Author
from payment.models import Commission, Invoice, Fund, COMMISSION_DESCRIPTION_CHOICES
from payment.models import PayeRequisition

BIRTH_YEAR_CHOICES = range(1920,2016)
COMMISSION_YEAR_CHOICES = range(2012,2050)

class BaseInvoiceForm(ModelForm):
    dob = forms.DateField(widget=
                          forms.SelectDateWidget(years=BIRTH_YEAR_CHOICES, attrs={"type": "date"}),
                          label="Date of birth",
                          required=False)
    address = forms.CharField(widget=forms.Textarea(attrs={'rows': '4'}),
                              required=False,
                              help_text="Required by SARS")

class InvoiceStaffForm(BaseInvoiceForm):
    fund = forms.ModelChoiceField(queryset=Fund.objects.filter(ledger=False).
                                filter(deprecated=False),
                                required=False)
    merge = forms.ChoiceField(required=False)

    class Meta:
        model = Invoice
        fields = ['identification', 'dob', 'invoicing_company', 'address',
                  'bank_name', 'bank_account_number',
                  'bank_account_type', 'bank_branch_name', 'bank_branch_code',
                  'swift_code', 'iban', 'tax_no', 'tax_percent', 'vat',
                  'level', 'query', 'additional_emails',
                  'requisition', 'requisition_number', 'payment_method',
                  'description', 'fund', 'vouchers_attached',
                  'prepared_by', 'approved_by', 'authorised_by', 'merge',]

class InvoiceForm(BaseInvoiceForm):
    identification = forms.CharField(max_length=20, required=True,
                                     help_text=
                                     "SA ID, passport or some form "
                                     "of official identification")
    dob = forms.DateField(widget=
                          forms.SelectDateWidget(years=BIRTH_YEAR_CHOICES, attrs={'type': 'date'}),
                          label="Date of birth",
                          required=True)
    address = forms.CharField(widget=forms.Textarea(attrs={'rows': '4'}),
                              required=True,
                              help_text="Required by SARS")

    bank_name = forms.CharField(max_length=20, required=True)
    bank_account_number = forms.CharField(max_length=20, required=True)

    class Meta:
        model = Invoice
        fields = ['identification', 'dob', 'invoicing_company', 'address',
                   'bank_name', 'bank_account_number',
                   'bank_account_type', 'bank_branch_name', 'bank_branch_code',
                   'swift_code', 'iban', 'tax_no', 'tax_percent', 'vat',
                   'query',
        ]

    def clean(self):
        cleaned_data = super(InvoiceForm, self).clean()
        query = cleaned_data.get("query")
        if "query_button" in self.data and query == "":
            self.add_error("query",
                           "Please explain why you're querying "
                           "the invoice")

class CommissionForm(ModelForm):
    author = AutoCompleteSelectField("authors", required=False,
                                     help_text=None, label="Payee")
    article = AutoCompleteSelectField('articles', required=False,
                                      help_text=None)
    fund = forms.ModelChoiceField(
        queryset=Fund.objects.filter(ledger=True).filter(deprecated=False),
        label='Pastel', required=False)

    def clean_author(self):
        data = self.cleaned_data['author']
        if data is None:
            raise forms.ValidationError("Please enter a user")
        return data

    class Meta:
        model = Commission
        fields = ['author', 'article', 'fund', 'description', 'notes',
                  'commission_due', 'taxable', 'vatable', 'vat_amount' ]

class CommissionFormset(ModelForm):
    fund = forms.ModelChoiceField(
        # queryset=Fund.objects.filter(ledger=True).filter(deprecated=False),
        queryset=Fund.objects.filter(deprecated=False),
        label='Ledger', required=False)

    def clean_fund(self):
        fund = self.cleaned_data['fund']
        if 'commission_due' in self.cleaned_data:
            due = self.cleaned_data['commission_due']
        else:
            due = None
        if due and due != 0.0 and fund is None:
            raise forms.ValidationError("Please select a fund")
        return fund

class CommissionAnalysisForm(ModelForm):
    descriptions = forms.MultipleChoiceField(choices=COMMISSION_DESCRIPTION_CHOICES,
                                            help_text="You can select multiple choices "
                                            "using the ctrl button and left click.",
                                            required=False)
    funds = forms.MultipleChoiceField(choices=Fund.get_funds,
                                      help_text="You can select multiple choices "
                                            "using the ctrl button and left click.",
                                      required=False)
    date_from = forms.DateField(widget=forms.SelectDateWidget
                                (empty_label=("Year", "Month", "Day"),
                                 years=COMMISSION_YEAR_CHOICES,
                                 attrs={'class': 'date_field', 'type': 'date'}),
                                required=False)
    date_to = forms.DateField(widget=forms.SelectDateWidget
                                (empty_label=("Year", "Month", "Day"),
                                 years=COMMISSION_YEAR_CHOICES,
                                 attrs={'class': 'date_field', 'type': 'date'}),
                                required=False)
    authors = AutoCompleteSelectMultipleField('authors', required=False,
                                             help_text="Enter text to search. "
                                              "You can select multiple payees",
                                              label="Payees")

    class Meta:
        model = Commission
        fields = ['descriptions', 'funds', 'authors', 'date_from', 'date_to', ]

class PayeRequisitionForm(ModelForm):

    def generate_invoices(self):
        PayeRequisition.generate_invoices(self.cleaned_data['payee'],
                                          self.cleaned_data['date_from'],
                                          self.cleaned_data['date_to'])

    class Meta:
        model = PayeRequisition
        fields = ['payee', 'date_from', 'date_to', ]


import sys
sys.path.append('../dao/')

import urllib.request
from icalendar import Calendar #, Event
#from dateutil.parser import parse
#import datetime
from datetime import timezone, time
from model.teaminformationdictionaries import CLeagueTeamtoTeamInformationDaoDict, DOneLeagueTeamtoTeamInformationDaoDict
from dao.gameinformationdao import GameInformationDao

class WebcalReader():

    def parseTeamNames(self, summary):
        # parse team names
        teamsStr = summary.replace(" - Regular Season GAME ",  "")
        awayTeam = teamsStr.split(" @ ")[0]
        homeTeam = teamsStr.split(" @ ")[1]
        return teamsStr.split(" @ ")

    # get team names and create GameInformationDao
    # put game dao in appropriate TeamInformationDict
    def addGameAtLeastElevenToTeam(self, isCLeagueGame):
        teamsList = self.parseTeamNames("Ice-o-Topes C @ Yaks C")
        awayTeam = teamsList[0]
        homeTeam = teamsList[1]
        gameDao = GameInformationDao(homeTeam, awayTeam )

        if isCLeagueGame == True:
            CLeagueTeamtoTeamInformationDaoDict.get(awayTeam).numGamesAtLeastEleven = CLeagueTeamtoTeamInformationDaoDict.get(awayTeam).numGamesAtLeastEleven + 1

    def addGameNotOnTuesdaySaturdayToTeam(self):
        print ("placeholder")

#    def ts2str(ts):
##        '''Convert a UTC timestamp into a UTC datetime, then format it to a string'''
#        dt = datetime.utcfromtimestamp(ts)      # Convert a UTC timestamp to a naive datetime object
#        dt = dt.replace(tzinfo=pytz.utc)        # Convert naive datetime to non-naive
#        return dt.strftime('%Y-%m-%d %H:%M:%S.%f%z')

    print("Hello World from webcalreader.py webCalReader!")
    #real link is webcal://<link> but we need to change this to http so that urllib can read it
    webcalUrl = "webcal://cdn.goalline.ca/subscribe_ical_league_team.php?cal=1c7fa-201601-201604-41040-510366-1721&1"
    httpUrl = webcalUrl.replace("webcal",  "http")
    req = urllib.request.urlopen(httpUrl)
    data = req.read()

    cal = Calendar.from_ical(data)
    numEvents = 0
    timeElevenPM = time(23,0,0)

#ends early for some reason when running
    for event in cal.walk('VEVENT'):
        print ("inside event")
        numEvents += 1
        startDate = (event.get('DTSTART'))
        endDate = (event.get('DTEND'))
        summary = event.get('SUMMARY')
        eventUID = event.get('UID')

        startDateDT = startDate.dt
#        convert timezone to local timezone for day of the week
        startDate.dt = startDate.dt.replace(tzinfo=timezone.utc).astimezone(tz=None)

        if startDate.dt.time > timeElevenPM:
            gameDao = GameInformationDao()
            # parse

        #Return the day of the week as an integer, where Monday is 0 and Sunday is 6.
        dayOfWeek = startDate.dt.weekday()
        print ("day of week: ",  dayOfWeek)

#        parse(date.dt)

        print (startDate.dt)
        print (startDate.dt.time())

#        printing date.to_ical gets more results than print date.dt,  but still not all of them consistently
#        print (date.to_ical())

        print (summary)

        print (eventUID.to_ical())

#    for component in cal.walk():
#        numEvents += 1
#        if (component.name == "VEVENT"):
#            date = event.get('dstart')
#            print (date.to_ical())

    print ("number of events: %d" %  numEvents)


import time
import json
import nxppy
import picamera
from subprocess import Popen
from threading import Thread
from flask import Flask, request, send_from_directory
from os import path

app = Flask(__name__)
camera = picamera.PiCamera()

records = [
    {"id": 0, "url": "/assets/img_0.jpg"}
]

data = {
    "0": "Zach's Card"
}

currentProc = None

@app.route('/')
def start():
    return send_from_directory("client", "index.html")

@app.route('/js/<file_name>')
def send_js (file_name):
    return send_from_directory("client/js", file_name)

@app.route('/css/<file_name>')
def send_css (file_name):
    return send_from_directory("client/css", file_name)

@app.route('/assets/<file_name>')
def send_asset (file_name):
    return send_from_directory("assets", file_name)

@app.route('/data')
def send_data():
    return json.dumps(records)

@app.route('/add')
def add_figure():
    global records, data, camera
    mifare = nxppy.Mifare()
    print("Starting")
    while True:
        try:
            val = mifare.select()
            value = ""
            for i in range(64):
                try:
                    value += mifare.read_block(i)
                except Exception:
                    break
            time.sleep(1)
            name = path.join("assets", "img_{}.jpg".format(len(records)))
            camera.capture(name)
            data[str(len(records))] = value
            records.append({"id": len(records), "url": name})
            return ""
        except Exception as e:
            print (e)
            pass
        time.sleep(1)
    time.sleep(4)

@app.route('/emu/<id_name>')
def start_emulation(id_name):
    global currentProc
    if currentProc is not None:
        currentProc.kill()
        currentProc = None
    try:
            currentProc = Popen(["explorenfc-cardemulation", "-t", data[id_name]])
    except Exception:
        pass
    return ""

@app.route('/cancel')
def stop_emulation():
    global currentProc
    if currentProc is not None:
        currentProc.kill()
    return ""

if __name__ == "__main__":
    app.run("0.0.0.0", 1337)


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import pickle
import random

dataset = pd.read_csv('E:/celeba-dataset/list_attr_celeba.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:,[0,21]].values
print(y[0][1])

DATADIR="E:/celeba-dataset/img_align_celeba/img_align_celeba"
categories=["Female","Male"]

training_data=[]
img_size=50

def create_training_data():
    i=0
    m=0
    f=0
    for img in os.listdir(DATADIR):
        try:
            if(y[i][1]==1 or (y[i][1]==-1 and f<20946)):
                img_array=cv2.imread(os.path.join(DATADIR,img),cv2.IMREAD_GRAYSCALE)
                new_array=cv2.resize(img_array,(img_size,img_size))
                if y[i][1]==1 :
                    gender=1
                    m=m+1
                else:
                    gender=0
                    f=f+1
                # i=i+1
                training_data.append([new_array,gender])
        except Exception as e:
            pass
        finally:
            if(i%100==0):
                print(i)
            i=i+1
            if i==50000:
                print("breaking")
                break
    print(m)
    print(f)

create_training_data()
print(training_data[0])

random.shuffle(training_data)

X=[]
y=[]

for a,b in training_data:
    X.append(a)
    y.append(b)

X=np.array(X).reshape(-1,img_size,img_size,1)
# X=X/255.0
f=open("X_pickle1.pkl","wb")
pickle.dump(X,f)
f.close()

w=open("y_pickle1.pkl","wb")
pickle.dump(y,w)
w.close()


#!/usr/bin/env python

import argparse
import csv
import json
import os
from pathlib import Path
import sys

import pysam

class Variant:
    def __init__(self, contig, position, reference, alt):
        self.contig = contig
        self.position = position
        self.reference = reference
        self.alt = alt
        self.name = None

    def key(self):
        return ",".join([str(x) for x in [self.contig, self.position, self.reference, self.alt]])

def load_vcf(filename):
    variants = list()
    f = pysam.VariantFile(filename,'r')
    for record in f:
        if len(record.alts) > 1:
            sys.stderr.write("Multi-allelic VCF not supported\n")
            sys.exit(1)

        # Skip any line in the .vcf that starts with '#'
        if not record.chrom.startswith('#'):
            v = Variant(record.chrom, record.pos, record.ref, record.alts[0])
            if "Name" in record.info:
                v.name = record.info["Name"]
            variants.append(v)

    return variants

def parse_sample_ids(sample_ids_path):
    """
    input: "path/to/sample_ids.tsv"
    output: ["sample-01", "sample-02", ...]
    """
    with open(sample_ids_path, 'r') as f:
        all_sample_ids = [line.strip() for line in f]

    return all_sample_ids

def parse_ncov_watch_ouput_by_sample_id(ncov_watch_output_path, all_sample_ids):
    """
    input: "/path/to/ncov_watch_output.tsv", ["sample-01", "sample-02", ...]
    output: {
              "sample-01": {
                             "MN908947.3,11287,GTCTGGTTTT,G": "ORF1ab:del3675-3677",
                             "MN908947.3,23063,A,T": "S:N501Y"
                           },
              "sample-02": {
                             ...
                           },
              ...
            }
    """
    ncov_watch_output_by_sample_id = {}

    for sample_id in all_sample_ids:
        ncov_watch_output_by_sample_id[sample_id] = {}

    try:
        with open(ncov_watch_output_path, 'r') as f:
            reader = csv.DictReader(f, delimiter='\t')
            for record in reader:
                sample_id = record['sample'].split('.')[0]
                if sample_id not in ncov_watch_output_by_sample_id.keys():
                    ncov_watch_output_by_sample_id[sample_id] = {}
                concatenated_key = ','.join([record['contig'], record['position'], record['reference'], record['alt']])
                ncov_watch_output_by_sample_id[sample_id][concatenated_key] = record['mutation']
    except:
        pass

    return ncov_watch_output_by_sample_id

def main(args):
    all_sample_ids = parse_sample_ids(args.sample_ids)

    ncov_watch_output_by_sample_id = parse_ncov_watch_ouput_by_sample_id(args.ncov_watch_output, all_sample_ids)

    watch_variants = load_vcf(args.watchlist)

    # Only count watchlist mutations with unique names
    unique_mutation_names_in_watchlist = list({var.name for var in watch_variants})
    num_unique_mutation_names_in_watchlist = len(unique_mutation_names_in_watchlist)

    print("\t".join(["sample_id", "watchlist_id", "num_observed_mutations", "num_mutations_in_watchlist", "proportion_watchlist_mutations_observed"]))
    for sample_id, observed_mutations in ncov_watch_output_by_sample_id.items():
        # Only count observed mutations with unique names
        unique_observed_mutation_names = list({mut for mut in observed_mutations.values()})
        num_unique_observed_mutation_names = len(unique_observed_mutation_names)
        proportion_watchlist_mutations_observed = num_unique_observed_mutation_names / num_unique_mutation_names_in_watchlist
        sample_summary_record = '\t'.join([
            sample_id, args.watchlist_id, str(num_unique_observed_mutation_names), str(num_unique_mutation_names_in_watchlist), "{:.3f}".format(proportion_watchlist_mutations_observed)
        ])
        print(sample_summary_record)

if __name__ == "__main__":
    description = 'Summarize mutations detected per sample and watchlist'
    parser = argparse.ArgumentParser(description=description)
    parser.add_argument('-s', '--sample-ids', help='file containing a single list of all sample IDs')
    parser.add_argument('-w', '--watchlist', help='file containing the mutations to screen for (.vcf format)')
    parser.add_argument('-i', '--watchlist-id', help='identifier for the watchlist')
    parser.add_argument('ncov_watch_output', help='output from ncov-watch.py')
    args = parser.parse_args()
    main(args)


import logging
import global_vars
import time
import threading
import socket
import pickle
import server
import inspect
import ctypes
# membership_list = []  # membership_list = [(ip,port,time,type,listening),()]

class Spout():
    def __init__(self):
        self.ip = socket.gethostbyname(socket.gethostname())
        self.file_length = 0
        self.stop = global_vars.fault_stop
        self.is_stop = False
        self.line_total = []
        self.lines = 0
        self.sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    def start(self):
        self.stop = False
        if self.ip == global_vars.MASTER:
            if not self.is_ready():
                logging.info("There is no topology, please assign it.")

            else:
                self.read_stream()
                self.spout_work()

    def spout_work(self):
        global id
        spout_thread = threading.Thread(target=self.spout_thread)
        spout_thread.start()
        id = spout_thread.ident
        logging.info(spout_thread)

    def terminate(self):
        global id
        if self.ip == global_vars.MASTER:
            logging.info("start to stop")
            if not global_vars.result:
                self.stop_thread(id)
            self.sk.close()
            self.stop = True
            global_vars.stream_list.queue.clear()
            sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            logging.info("********terminate**********")
            message = {
                'type': 'stream_data',
                'send_ip': socket.gethostbyname(socket.gethostname()),
                'data': 'TERMINATE'
            }
            for member in global_vars.membership_list:
                ip_port = (member[0], 7000)
                sk.sendto(pickle.dumps(message), ip_port)
            sk.close()

    def is_ready(self):
        flag = True
        if len(global_vars.membership_list) < 2:
            flag = False
        return flag

    def read_stream(self):
        file_name = ""
        if global_vars.membership_list[0][5] == 0:
            logging.info("Please select application.")
        elif global_vars.membership_list[0][5] == "1":
            file_name = global_vars.file_name_one
        elif global_vars.membership_list[0][5] == "2":
            file_name = global_vars.file_name_two
        elif global_vars.membership_list[0][5] == "3":
            file_name = global_vars.file_name_three
        while file_name:
            f = open(file_name, "r")
            lines = f.readlines()
            if not lines:
                break
            for line in lines:
                global_vars.stream_list.put(line)
            self.file_length = len(lines)
            f.close()
            break

    def spout_thread(self):
        time.sleep(0.5)
        while not self.stop and not self.is_stop:
            target_ip =[]
            for member in global_vars.membership_list:
                if member[4] == global_vars.BOLT_FILTER:
                    target_ip.append(member[0])
            ip_length = len(target_ip)
            logging.info("spout out doing now")
            logging.info(target_ip)
            # only read one line
            num = self.file_length // ip_length
            logging.info(num)
            for ips in target_ip:
                ip_port = (ips, 7000)
                count = 0
                if ips != target_ip[-1]:
                    logging.info("SpoutWorker to the filter bolt: " + ips + " start")
                    while count < num:
                        logging.info('+++++++++++++++')
                        send_message = global_vars.stream_list.get()
                        message = {
                            'type': 'stream_data',
                            'send_ip': socket.gethostbyname(socket.gethostname()),
                            'target_ip':ips,
                            'data': send_message
                        }
                        logging.info(message)
                        self.sk.sendto(pickle.dumps(message), ip_port)
                        time.sleep(0.005)
                        count += 1
                    message = {
                        'type': 'stream_data',
                        'send_ip': socket.gethostbyname(socket.gethostname()),
                        'target_ip': ips,
                        'data': global_vars.Stream_Stop
                    }
                    self.sk.sendto(pickle.dumps(message), ip_port)
                    logging.info(message)

                else:
                    logging.info("SpoutWorker to the filter bolt: " + ips + " start")
                    remain = self.file_length - num * (ip_length - 1)
                    while count < remain:
                        logging.info('+++++++++++++++')
                        send_message = global_vars.stream_list.get()
                        message = {
                            'type': 'stream_data',
                            'send_ip': socket.gethostbyname(socket.gethostname()),
                            'target_ip': ips,
                            'data': send_message
                        }
                        logging.info(message)
                        self.sk.sendto(pickle.dumps(message), ip_port)
                        time.sleep(0.006)
                        count += 1
                    message = {
                        'type': 'stream_data',
                        'send_ip': socket.gethostbyname(socket.gethostname()),
                        'target_ip': ips,
                        'data': global_vars.Stream_Stop
                    }
                    self.sk.sendto(pickle.dumps(message), ip_port)
                    logging.info(message)

            break
        global_vars.stream_list.queue.clear()
        logging.info("Stream concurrent list is empty, all the message have been sent out.")
        self.sk.close()

    def listen_thread(self):
        sk2 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sk2.bind(('0.0.0.0', 5050))
        target_ip = []
        for member in global_vars.membership_list:
            if member[4] == global_vars.BOLT_FILTER:
                target_ip.append(member[0])
        listen_ip = target_ip[-1]
        while True:
            msg, address = sk2.recvfrom(32768)
            receive_msg = pickle.loads(msg)
            if receive_msg['type'] == 'ack':
                send_i = receive_msg['data']
                if send_i in self.line_total:
                    self.line_total.remove(send_i)
            break
        ip_port = (listen_ip, 7000)
        if len(self.line_total) != 0:
            for k in self.line_total:
                message = {
                    'type': 'stream_data',
                    'send_ip': socket.gethostbyname(socket.gethostname()),
                    'target_ip': listen_ip,
                    'data': self.lines[k],
                    'lines': k
                }

                sk2.sendto(pickle.dumps(message), ip_port)
                logging.info(message)
        message = {
            'type': 'stream_data',
            'send_ip': socket.gethostbyname(socket.gethostname()),
            'target_ip': listen_ip,
            'data': global_vars.Stream_Stop,
            'lines': 0
        }
        sk2.sendto(pickle.dumps(message), ip_port)
        logging.info(message)

    def _async_raise(self,tid, exctype):
        """raises the exception, performs cleanup if needed"""
        tid = ctypes.c_long(tid)
        if not inspect.isclass(exctype):
            exctype = type(exctype)
        res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
        if res == 0:
            raise ValueError("invalid thread id")
        elif res != 1:
            # """if it returns a number greater than one, you're in trouble,
            # and you should call it again with exc=NULL to revert the effect"""
            ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
            raise SystemError("PyThreadState_SetAsyncExc failed")

    def stop_thread(self,id):
        self._async_raise(id, SystemExit)


import matplotlib.pyplot as plt
import math

dt = 0.001
Cm = 1
VNa = 50
VK = -77
VL = -54.387
gNa = 120
gK = 36
gL = 0.3
v = -65
m = 0.053
h = 0.6
n = 0.318

alfa = 2
beta = 1
Esyn = 0
T = 0
r = 0
gsyn = 1
Isyn = 0

vres = []
mres = []
hres = []
nres = []
time = []
rres = []
Isynres = []

f = open("h-h-model.txt", "w")
f.write(f'{str(v)},{str(m)},{str(h)},{str(n)},0,{str(r)}')

for i in range(int(100/dt)):
    milisegundo = int(i*dt)
    if milisegundo == 30 or milisegundo == 40 or milisegundo == 50:
        T = 1
    else:
        T = 0

    vres.append(v)
    mres.append(m)
    hres.append(h)
    nres.append(n)
    time.append(i*dt)
    rres.append(r)
    Isynres.append(Isyn)

    aM = (0.1 * (-v-40)) / (math.exp((-v-40)/10) - 1)
    bM = 4 * math.exp((-v-65)/18)
    aH = 0.07 * math.exp((-v-65)/20)
    bH = 1 / (math.exp((-v-35)/10) + 1)
    aN = (0.01 * (-v-55)) / (math.exp((-v-55)/10) - 1)
    bN = 0.125 * math.exp((-v-65)/80)

    Isyn = gsyn * r * (v - Esyn)
    vNext = v + dt*(-gL*(v - VL) - gNa*(m**3)*h*(v - VNa) - gK*(n**4)*(v - VK) - Isyn)
    mNext = m + dt*(aM*(1-m) - bM*m)
    hNext = h + dt*(aH*(1-h) - bH*h)
    nNext = n + dt*(aN*(1-n) - bN*n)
    r = r + dt*((alfa*T) * (1-r) - (beta*r))

    v = vNext
    m = mNext
    h = hNext
    n = nNext

    f.write(f'{str(v)},{str(m)},{str(h)},{str(n)},{str(i*dt)},{str(r)}\n')
f.close()

fig, axes = plt.subplots(2,1)
axes[0].plot(time, vres, label='Potencial de membrana')
axes[0].plot(time, Isynres, label = 'Corriente sináptica')
axes[0].set_title('Potencial de membrana / t', fontsize=24, loc='left')
axes[0].legend(loc="upper right", fontsize=18)
axes[0].set_ylabel('Potencial (mV)', fontsize=18)
axes[0].set_xlabel('Tiempo (ms)', fontsize=18)

axes[1].plot(time, rres)
axes[1].set_title('r / t', fontsize=24, loc='left')
axes[1].set_ylabel('r', fontsize=24)
axes[1].set_xlabel('Tiempo (ms)', fontsize=18)
axes[1].set_ylim(ymin=0, ymax=1)

plt.show()


#!/usr/bin/env pnpython4
#
# Merge Response_t from ph5 files and create corrected Das_t files
# Only creates corrected Response_t and Das_t files.
#
# Steve Azevedo, Mar 2017
#

import argparse
import logging
import os
import re
from ph5.core import ph5api

PROG_VERSION = '2019.14'
LOGGER = logging.getLogger(__name__)

ALL_FAMILIES = ['A', 'B', 'C', 'D', 'E', 'F', 'G',
                'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q']

def get_args():
    global ARGS, P5

    parser = argparse.ArgumentParser()
    parser.usage = "v{0}: set_n_i_response\
    (Run from top level families directory.)".format(
        PROG_VERSION)

    parser.add_argument("-F", dest="families_directory", required=False,
                        help="Directory that holds the family directories."
                             "Absolute path.")
    parser.add_argument("-N", dest="first_n_i", required=False, type=int,
                        default=0,
                        help="The n_i of the first entry. Defaults to zero.")

    ARGS = parser.parse_args()

    if ARGS.families_directory is None:
        ARGS.families_directory = os.getcwd()

def dump_kefs():
    '''   Dump each family Response_t into a single kef file.
          Label start of each Response_t with the family name '## A' as example
    '''
    global ORIG_RESPS
    miniRE = re.compile(r"miniPH5_\d{5}.ph5")
    here = os.getcwd()
    first = True
    if not os.path.exists("RESPONSE_T_N_I"):
        os.mkdir("RESPONSE_T_N_I")
    ORIG_RESPS = os.path.join(here, "RESPONSE_T_N_I",
                              "Response_t_by_family.kef")
    LOGGER.info("Creating: {0}".format(ORIG_RESPS))
    with open(ORIG_RESPS, 'w+') as fh:
        for fam in ALL_FAMILIES:
            if os.path.exists(os.path.join(fam, "master.ph5")):
                files = os.listdir(os.path.join('.', fam))
                matches = [f for f in files if miniRE.match(f)]
                if len(matches) == 0:
                    continue
                if first:
                    fh.write("## {0}\n".format(here))
                    first = False
                command = "ph5tokef -n {0}/master.ph5 -R".format(fam)
                fh.write("## {0}\n".format(fam))
                pipeh = os.popen(command)
                if pipeh:
                    while True:
                        line = pipeh.readline()
                        if not line:
                            break
                        fh.write(line)

#
# First line at start of family kef: "## A" (as an example)
#

def parse_kef():
    '''   Parse catted response file created by dump_kefs to a map:
          MAP[family][index]['n_i_all':n, 'n_i_family':n,
          [Response_t_dictionary]]
          family => Family name, 'A' as an example
          n_i_all => The n_i after all the Response_t are catted together
          n_i_family => The original n_i
          Response_t_dictionary => A dictionary for the line from the
          Response_t, keys are
          column names
    '''
    global ARGS
    ret = {}
    row = None
    n_i_all = 0
    with open(ORIG_RESPS) as fh:
        while True:
            line = fh.readline()
            if not line:
                break
            if line[0] == '#':
                # Comment with family
                family = line[3:].strip()
                if family not in ret:
                    ret[family] = []
                if len(line) == 5 and line[1] == '#':
                    # Save row, Just finished last family so save last row
                    if row is not None:
                        row['n_i_family'] = len(ret[family])
                        ret[family].append(row)

                    row = {}

            # We start a new row in the table here
            elif line[0] == '/':
                # Save row if there is one
                if row != {}:
                    row['n_i_family'] = len(ret[family])
                    ret[family].append(row)

                row = {}
                row['n_i_all'] = n_i_all + ARGS.first_n_i
                row['ppath'] = line.strip()
                n_i_all += 1
            elif line[0] == '\t':
                line = line.strip()
                key, value = line.split('=')
                if key == 'n_i':
                    last_n_i = int(value)
                if 'kv' not in row:
                    row['kv'] = {}
                row['kv'][key] = value

        row['n_i_family'] = last_n_i + ARGS.first_n_i
        ret[family].append(row)
    return ret

def print_new_Response_t(n_i_map):
    '''   Print the final Response_t.kef
    '''
    CORRECTED_RESPS = os.path.join(
        ARGS.families_directory, "RESPONSE_T_N_I", "Response_t_cor.kef")
    LOGGER.info("Creating: {0}".format(CORRECTED_RESPS))
    with open(CORRECTED_RESPS, 'w+') as fh:
        families = sorted(n_i_map.keys())
        for family in families:
            fh.write("#  {0}\n".format(family))
            n = 0
            for element in n_i_map[family]:
                fh.write("#  {0}\n".format(n))
                n += 1
                kv = element['kv']
                kv['n_i'] = element['n_i_all']
                keys = sorted(kv.keys())
                fh.write(element['ppath'] + '\n')
                for k in keys:
                    fh.write("\t{0}={1}\n".format(k, kv[k]))

def print_new_Das_t(P5, n_i_map, family):
    '''   Print Das_t corrected for Response_t n_i
    '''
    P5.read_das_g_names()
    for das_g in P5.Das_g_names:
        das = P5.read_das_t(das_g)
        MAP = n_i_map[family]
        if not das or das not in P5.Das_t:
            LOGGER.warning("#***\tMissing: {0}".format(das))
            continue

        if not os.path.exists(os.path.join(
                ARGS.families_directory, "RESPONSE_T_N_I")):
            os.mkdir(os.path.join(ARGS.families_directory, "RESPONSE_T_N_I"))

        DAS_KEF = os.path.join(
            ARGS.families_directory, "RESPONSE_T_N_I",
            "Das_t_response_n_i_{0}.kef".format(das))
        LOGGER.info("Creating: {0}\n".format(DAS_KEF))
        with open(DAS_KEF, 'w+') as fh:
            fh.write("#   PH5VERSION: {0}\n".format(ph5api.PH5VERSION))
            keys = sorted(P5.Das_t[das]['keys'])
            i = 1
            for das_t in P5.Das_t[das]['rows']:
                try:
                    M = MAP[das_t['response_table_n_i']]
                    das_t['response_table_n_i'] = M['n_i_all']
                    fh.write("#   {0}\n".format(i))
                    i += 1
                    fh.write(
                        "/Experiment_g/Receivers_g/{0}/Das_t\n".format(das_g))
                    for k in keys:
                        fh.write("\t{0}={1}\n".format(k, das_t[k]))
                except IndexError:
                    sr = ph5api.fepoch(
                        das_t['sample_rate_i'],
                        das_t['sample_rate_multiplier_i'])
                    LOGGER.warning(
                        "#   Index out of range for DAS: {0}, sample rate: {1}"
                        .format(das, sr))
                    LOGGER.warning("#   Entry unchanged! Suspect data.")
                    fh.write(
                        "#   {0} response_table_n_i entry suspect!\n".format(
                            i))
                    i += 1
                    fh.write(
                        "/Experiment_g/Receivers_g/{0}/Das_t\n".format(das_g))
                    for k in keys:
                        fh.write("\t{0}={1}\n".format(k, das_t[k]))

def main():
    get_args()
    os.chdir(ARGS.families_directory)
    dump_kefs()
    try:
        n_i_map = parse_kef()
    except BaseException:
        LOGGER.error("Cannot create n_i map. "
                     "Make sure the directory is correct using -F flag")
    else:
        for family in ALL_FAMILIES:
            ph5 = os.path.join(ARGS.families_directory, family)
            try:
                P5 = ph5api.PH5(path=ph5, nickname='master.ph5')
            except Exception as e:
                LOGGER.warning(e.msg)
                continue

            print_new_Das_t(P5, n_i_map, family)
            P5.close()
        print_new_Response_t(n_i_map)

if __name__ == '__main__':
    main()


import time
import paho.mqtt.client as paho
from paho import mqtt

# setting callbacks for different events to see if it works, print the message etc.
def on_connect(client, userdata, flags, rc, properties=None):
    print("CONNACK received with code %s." % rc)

# print which topic was subscribed to
def on_subscribe(client, userdata, mid, granted_qos, properties=None):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))

# print message, useful for checking if it was successful
def on_message(client, userdata, msg):
    print("Received: ")
    print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))

client = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5)
client.on_connect = on_connect

#client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
# set username and password
#client.username_pw_set("awmrpk06", "1065287982aA")
# connect to HiveMQ Cloud on port 8883 (default for MQTT)
client.connect("127.0.0.1", 1883)
# setting callbacks, use separate functions like above for better visibility
client.on_subscribe = on_subscribe
client.on_message = on_message
# subscribe to all topics of encyclopedia by using the wildcard "#"
client.subscribe("encyclopedia/#")
# you can also use loop_start and loop_stop
client.loop_forever()


import os
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
from cassandra.query import SimpleStatement, BatchStatement

dir_path = os.path.dirname(os.path.realpath(__file__))
BUNDLE_PATH = os.path.join(dir_path, 'secure-connect-gesture-prediction.zip')

cloud_config = {
    'secure_connect_bundle': BUNDLE_PATH
}
auth_provider = PlainTextAuthProvider('UzJkZZyFwGSAxJCcKGLXtoEu',
                                      'ZntHG+Xa01.deSCmpuEGt1fRY_G+ptJgZZR9SluCpAk9Zz2Cqtkg2v7Q4FwpXi3-1qD7I9UMQzsNZsONamSju6f9fRGy4D8xWRJZJjvvGDYYaXiv1wmZxkqUk6W4bNhM')
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
session = cluster.connect()

def delete_data():
    query = f"""
            TRUNCATE gesture_prediction.gesture_prediction
            """
    session.execute(query)

def add_data(raw_df):
    delete_data()
    df = raw_df.copy()
    col = """id, time, channel1, channel2, channel3,
          channel4, channel5, channel6, channel7, channel8,
          class"""
    # for _, row in df.iterrows():
    #     query = f"""
    #     INSERT INTO gesture_prediction.gesture_prediction ({col})
    #     VALUES (uuid(), {int(row['time'])},{row['channel1']},{row['channel2']},
    #             {row['channel3']},{row['channel4']},{row['channel5']},
    #             {row['channel6']},{row['channel7']},{row['channel8']},{int(row['class'])})
    #     """
    #     session.execute(query)

    batch = BatchStatement()

    for _, row in df.iterrows():
        INSERT_STMT = f"""
            INSERT INTO gesture_prediction.gesture_prediction ({col})
            VALUES (uuid(), {int(row['time'])},{row['channel1']},{row['channel2']},
                    {row['channel3']},{row['channel4']},{row['channel5']},
                    {row['channel6']},{row['channel7']},{row['channel8']},{int(row['class'])})
            """
        batch.add(SimpleStatement(INSERT_STMT))

    session.execute(batch)

src/databaseconnection - not using/connect_database.py


import os
import argparse
import logging
import pickle
from tqdm import tqdm
from sklearn.cluster import KMeans
import torch

from symbolnet_utils import dataloaders, networks, memory, folder_tool, emnist_tools

def register_memory(folder, data_path, classes, **kwargs):
    """
    Creates the file 'memory.pkl' inside 'folder' and saves the feature representations of images from the classes
    specified by 'classes' in it. This memory file contains a pickled dictionary (let's call it memory_dict) whose keys
    are class names. For instance, if the class 'A' is specified in 'classes', then memory_dict['A'] contains the output
    features obtained when mapping 'num_mem' randomly selected images labeled 'A' through the network's feature_network
    (except when using K-means, i.e. when 'num_see' > 'num_mem', see below). These feature representations can then be
    compared, using the network's evaluator, to the features of images to be classified (see e.g. classification_test).

    Note that it is possible to memorize classes that were not used during training!

    REQUIRED PARAMETERS
        - folder: A folder containing the network state dictionary for which to register memory, as well as the
                  architecture file for that state dictionary. (Both the required files 'network_state_dict.pth'
                  and 'network_info.pkl' are created when running 'train.py'.)
        - data_path: Path where the EMNIST dataset is located. The data_root path must lead to a folder containing
                     a folder called EMNIST which itself contains the dataset. If the dataset is not there, then
                     it gets downloaded automatically.
        - classes: A string specifying which classes to use for training. Basic options are 'digits', 'uppercases'
                   and 'lowercases'. See symbolnet_utils.emnist_tools.EmnistClassLabelsSetup for a detailed explanation
                   of how to specify other sets of classes.
                   TYPE: string

    OPTIONAL PARAMETERS
        - device: Can be either torch.device('cpu') or torch.device('cuda').
                  TYPE: torch.device
                  DEFAULT: torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
        - emnist_split: Which split of the EMNIST dataset to use. Can be either 'byclass', 'bymerge', 'balanced'
                        or 'digits' (the 'letters' split is not supported).
                        TYPE: string
                        DEFAULT: 'byclass'
        - train: True means we use EMNIST training data, False means we use EMNIST test data.
                 TYPE: bool
                 DEFAULT: True
        - num_mem: Number of sets features to memorize for each class. See K-MEANS explanation below
                   TYPE: int
                   DEFAULT: 10
        - num_see: Number of images to look at for each at for each class. See K-MEANS explanation below
                   TYPE: int
                   DEFAULT: 10

    K-MEANS OPTION
        When 'num_mem' == 'num_see' (the default option), each set of features being memorized is obtained by mapping
        one randomly selected image from the dataset through the newtwork's feature_network module. When 'num_mem' <
        'num_see', 'num_see' images are mapped through the feature network for each class, and a K-means algorithm with
        'num_mem' centroids is performed on the resulting features. The 'num_mem' centroid features are then saved.
    """
    # -------------------------- SETTING UP --------------------------
    # Folder
    required_files = ['network_state_dict.pth', 'network_info.pkl']
    resume = folder_tool.check_folder(folder, required_files)
    if not resume:
        raise ValueError('...')

    # Device
    if 'device' in kwargs.keys():
        device = kwargs['device']
    else:
        device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

    # Dataloader - batch_size=None by default, so we load one element at a time.
    train = kwargs['train']        if ('train'        in kwargs) else True
    split = kwargs['emnist_split'] if ('emnist_split' in kwargs) else 'byclass'
    loader = dataloaders.EmnistSinglesLoader(data_path = data_path,
                                             split = split,
                                             classes_string = classes,
                                             inds_save_folder = folder,
                                             train=train,
                                             device = device)
    loader_it = iter(loader)
    classes = loader.classes
    class_labels = loader.class_labels
    train_or_test = 'train' if train else 'test'

    # Network
    with open(os.path.join(folder, 'network_info.pkl'), 'rb') as net_dict_file:
        net_dict = pickle.load(net_dict_file)
    arch      = net_dict['architecture']
    comp_mode = net_dict['compare_mode']
    blocks    = net_dict['blocks']
    arch   = getattr(networks, arch)
    blocks = [int(b) for b in blocks.split(',')]
    net = arch(blocks=blocks, compare_mode=comp_mode).to(device)
    net.load_state_dict(torch.load(os.path.join(folder, 'network_state_dict.pth'), map_location=device))

    if 'memorized_classes' in net_dict.keys():
        mem_classes = net_dict['memorized_classes'] + classes
    else:
        mem_classes = classes
    mem_classes = emnist_tools.sort_classes(mem_classes)
    net_dict['memorized_classes'] = mem_classes

    with open(os.path.join(folder, 'network_info.pkl'), 'wb') as net_dict_file:
            pickle.dump(net_dict, net_dict_file, protocol=pickle.HIGHEST_PROTOCOL)

    # Numbers of images
    num_mem = kwargs['num_mem'] if ('num_mem' in kwargs) else 10
    num_see = kwargs['num_see'] if ('num_see' in kwargs) else 10
    compute_kmeans = (num_see > num_mem)

    # Logging
    log_file = os.path.join(folder, 'log.txt')

    logging.basicConfig(level = 0)
    logger = logging.getLogger(__name__) # We use a single logger with 2 handlers.
    logger.propagate = False
    logger.setLevel(0)

    console_handler = logging.StreamHandler() # Writes to console
    console_handler.setFormatter(logging.Formatter('%(message)s'))
    console_handler.setLevel(1)
    logger.addHandler(console_handler)

    file_handler = logging.FileHandler(log_file) # Writes to log.txt
    file_handler.setFormatter(logging.Formatter('%(message)s'))
    file_handler.setLevel(2)
    logger.addHandler(file_handler)

    # Initial message
    message  = f"Registering memory of the following classes:\n{classes}\n"
    if compute_kmeans:
        message += f"For each class, looking at {num_see} images and memorizing {num_mem} elements through a K-means algorithm.\n"
        message += f"Images are taken from the '{split} {train_or_test}' EMNIST split."
    else:
        message += f"For each class, memorizing {num_mem} images taken at random from the '{split} {train_or_test}' EMNIST split."
    message +=  "\n...\n"
    logger.log(2, message)

    # -------------------------- FINDING IMAGES TO MEMORIZE --------------------------
    logger.log(1, "Finding images to memorize.\n")
    # Get examples from each class
    examples = {class_ : [] for class_ in classes}
    while any(len(mem_list) < num_see for mem_list in examples.values()):
        try:
            im, label = next(loader_it)
            class_ = classes[class_labels.index(label)]
            if len(examples[class_]) < num_see:
                examples[class_].append(im)
        except StopIteration:
            break
    for class_ in classes:
        examples[class_] = torch.stack(examples[class_], dim=0).to(device)

    # -------------------------- MEMORIZING --------------------------
    logger.log(1, "Memorizing elements from each class.\n")
    # Map examples to features, perform K-means if required, save in memory file.
    with torch.no_grad():
        for class_ in tqdm(classes):
            batch = examples[class_]
            features = net.feature_network(batch)
            if compute_kmeans:
                for feat_key in features.keys():
                    np_feats = features[feat_key].cpu().numpy()
                    shape = list(np_feats.shape)
                    np_feats = np_feats.reshape((shape[0], -1))
                    feat_kmeans = KMeans(n_clusters=num_mem).fit(np_feats).cluster_centers_
                    shape[0] = num_mem
                    feat_kmeans = feat_kmeans.reshape(shape)
                    feat_kmeans = torch.from_numpy(feat_kmeans)
                    features[feat_key] = feat_kmeans
            memory.register_features(folder, class_, features)

    # -------------------------- EPILOGUE --------------------------
    message  = "DONE"
    message += "\n\n\n\n----------------------\n\n\n"
    logger.log(2, message)

if __name__ == '__main__':
    parser = argparse.ArgumentParser('...')
    # General
    parser.add_argument('--folder',      type=str,  required=True, help="Folder to create or from which to resume")
    # Dataset
    parser.add_argument('--data_path',    type=str, required=True,     help="Where the data is.")
    parser.add_argument('--classes',      type=str, required=True,     help="String specifying which classes to use.")
    parser.add_argument('--emnist_split', type=str, default='byclass', help="Which split of the EMNIST dataset to use.")
    # Memorize
    parser.add_argument('--num_mem', type=int, default=10, help='Number of elements to store in memories for each class.')
    parser.add_argument('--num_see', type=int, default=10, help='Number of images from each class to see while memorizing.')

    args = parser.parse_args()
    register_memory(**args.__dict__)


#!/usr/bin/env python3

import sys
sys.path.append('../Lib/')

import torch
import torch.nn as nn

from transformers import get_linear_schedule_with_warmup
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score

import os, configparser, math, random
import datareader, tokenizer, utils

# deterministic determinism
torch.manual_seed(2020)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(2020)

class TransformerClassifier(nn.Module):
  """A transformative experience"""

  def __init__(self, num_classes=2 ):
    """We have some of the best constructors in the world"""

    super(TransformerClassifier, self).__init__()

    self.embedding = nn.Embedding(
      num_embeddings=cfg.getint('data', 'vocab_size'),
      embedding_dim=cfg.getint('model', 'emb_dim'))

    encoder_layer = nn.TransformerEncoderLayer(
      d_model=cfg.getint('model', 'emb_dim'),
      nhead=cfg.getint('model', 'num_heads'),
      dim_feedforward=cfg.getint('model', 'feedforw_dim'))

    self.trans_encoder = nn.TransformerEncoder(
      encoder_layer=encoder_layer,
      num_layers=cfg.getint('model', 'num_layers'))

    self.dropout = nn.Dropout(cfg.getfloat('model', 'dropout'))

    self.linear = nn.Linear(
      in_features=cfg.getint('model', 'emb_dim'),
      out_features=num_classes)

  def forward(self, texts, attention_mask):
    """Moving forward"""

    output = self.embedding(texts) # * sqrtn

    # encoder input: (seq_len, batch_size, emb_dim)
    # encoder output: (seq_len, batch_size, emb_dim)
    output = output.permute(1, 0, 2)
    output = self.trans_encoder(output, attention_mask)

    # extract CLS token only
    # output = output[0, :, :]

    # average pooling
    output = torch.mean(output, dim=0)

    output = self.dropout(output)
    output = self.linear(output)

    return output

def fit(model, train_loader, val_loader, weights, n_epochs):
  """Training routine"""

  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  model.to(device)

  weights = weights.to(device)
  cross_entropy_loss = torch.nn.CrossEntropyLoss(weights)

  optimizer = torch.optim.AdamW(
    model.parameters(),
    lr=cfg.getfloat('model', 'lr'))

  scheduler = get_linear_schedule_with_warmup(
    optimizer,
    num_warmup_steps=100,
    num_training_steps=1000)

  best_roc_auc = -1
  optimal_epochs = -1

  for epoch in range(1, n_epochs + 1):
    model.train()
    train_loss, num_train_steps = 0, 0

    for batch in train_loader:
      batch = tuple(t.to(device) for t in batch)
      batch_inputs, batch_mask, batch_labels = batch
      batch_mask = batch_mask.repeat(cfg.getint('model', 'num_heads'), 1, 1)
      optimizer.zero_grad()

      logits = model(batch_inputs, batch_mask)
      loss = cross_entropy_loss(logits, batch_labels)
      loss.backward()

      torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
      optimizer.step()
      scheduler.step()

      train_loss += loss.item()
      num_train_steps += 1

    av_loss = train_loss / num_train_steps
    val_loss, roc_auc = evaluate(model, val_loader, weights)
    print('ep: %d, steps: %d, tr loss: %.3f, val loss: %.3f, val roc: %.3f' % \
          (epoch, num_train_steps, av_loss, val_loss, roc_auc))

    if roc_auc > best_roc_auc:
      best_roc_auc = roc_auc
      optimal_epochs = epoch

  return best_roc_auc, optimal_epochs

def evaluate(model, data_loader, weights):
  """Evaluation routine"""

  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
  weights = weights.to(device)
  model.to(device)

  cross_entropy_loss = torch.nn.CrossEntropyLoss(weights)
  total_loss, num_steps = 0, 0

  model.eval()

  all_labels = []
  all_probs = []

  for batch in data_loader:
    batch = tuple(t.to(device) for t in batch)
    batch_inputs, batch_mask, batch_labels = batch
    batch_mask = batch_mask.repeat(cfg.getint('model', 'num_heads'), 1, 1)

    with torch.no_grad():
      logits = model(batch_inputs, batch_mask)
      loss = cross_entropy_loss(logits, batch_labels)

    batch_logits = logits.detach().to('cpu')
    batch_labels = batch_labels.to('cpu')
    batch_probs = torch.nn.functional.softmax(batch_logits, dim=1)[:, 1]

    all_labels.extend(batch_labels.tolist())
    all_probs.extend(batch_probs.tolist())

    total_loss += loss.item()
    num_steps += 1

  av_loss = total_loss / num_steps
  roc_auc = roc_auc_score(all_labels, all_probs)

  return av_loss, roc_auc

def main():
  """Fine-tune bert"""

  #
  # split train into train and validation and evaluate
  #

  tr_texts, tr_labels = datareader.DirDataReader.read(
    os.path.join(base, cfg.get('data', 'train')),
    {'no':0, 'yes':1})

  tr_texts, val_texts, tr_labels, val_labels = train_test_split(
    tr_texts, tr_labels, test_size=0.20, random_state=2020)

  tok = tokenizer.Tokenizer(cfg.getint('data', 'vocab_size'))
  tok.fit_on_texts(tr_texts)

  tr_texts = tok.texts_as_sets_to_seqs(tr_texts)
  val_texts = tok.texts_as_sets_to_seqs(val_texts)

  train_loader = utils.make_data_loader(
    tr_texts,
    tr_labels,
    cfg.getint('model', 'batch_size'),
    cfg.getint('data', 'max_len'),
    'train',
    utils.to_transformer_inputs)

  val_loader = utils.make_data_loader(
    val_texts,
    val_labels,
    cfg.getint('model', 'batch_size'),
    cfg.getint('data', 'max_len'),
    'dev',
    utils.to_transformer_inputs)

  print('loaded %d training and %d validation samples' % \
        (len(tr_texts), len(val_texts)))

  model = TransformerClassifier()

  label_counts = torch.bincount(torch.IntTensor(tr_labels))
  weights = len(tr_labels) / (2.0 * label_counts)

  best_roc, optimal_epochs = fit(
    model,
    train_loader,
    val_loader,
    weights,
    cfg.getint('model', 'num_epochs'))
  print('roc auc %.3f after %d epochs' % (best_roc, optimal_epochs))

  #
  # now retrain and evaluate on test
  #

  tr_texts, tr_labels = datareader.DirDataReader.read(
    os.path.join(base, cfg.get('data', 'train')),
    {'no':0, 'yes':1})

  test_texts, test_labels = datareader.DirDataReader.read(
    os.path.join(base, cfg.get('data', 'test')),
    {'no':0, 'yes':1})

  tok = tokenizer.Tokenizer(cfg.getint('data', 'vocab_size'))
  tok.fit_on_texts(tr_texts)

  tr_texts = tok.texts_as_sets_to_seqs(tr_texts)
  test_texts = tok.texts_as_sets_to_seqs(test_texts)

  train_loader = utils.make_data_loader(
    tr_texts,
    tr_labels,
    cfg.getint('model', 'batch_size'),
    cfg.getint('data', 'max_len'),
    'train',
    utils.to_transformer_inputs)

  test_loader = utils.make_data_loader(
    test_texts,
    test_labels,
    cfg.getint('model', 'batch_size'),
    cfg.getint('data', 'max_len'),
    'test',
    utils.to_transformer_inputs)

  print('loaded %d training and %d test samples' % \
        (len(tr_texts), len(test_texts)))

  model = TransformerClassifier()

  label_counts = torch.bincount(torch.IntTensor(tr_labels))
  weights = len(tr_labels) / (2.0 * label_counts)

  fit(model, train_loader, test_loader, weights, optimal_epochs)

if __name__ == "__main__":

  cfg = configparser.ConfigParser()
  cfg.read(sys.argv[1])
  base = os.environ['DATA_ROOT']

  main()


import pandas as pd
import os
import re

# detalles en el notebook

# carga de archivos

path = r'C:\Users\barce\OneDrive\Escritorio\HENRY\HENRY_Course\1- Data_Science\Proyectos\1- Individual\PI - 1\PI01_DATA_ENGINEERING-main\PI_01\Datasets\precios_semanas_20200419_20200426.xlsx'
df_1 = pd.read_excel(path, sheet_name= None)

df_1_h1 = df_1['precios_20200426_20200426']
df_1_h2 = df_1['precios_20200419_20200419']

path_2 = r'C:\Users\barce\OneDrive\Escritorio\HENRY\HENRY_Course\1- Data_Science\Proyectos\1- Individual\PI - 1\PI01_DATA_ENGINEERING-main\PI_01\Datasets\precios_semana_20200413.csv'
df_2 = pd.read_csv(path_2, encoding='utf-16-le', sep=',')

path_3 = r'C:\Users\barce\OneDrive\Escritorio\HENRY\HENRY_Course\1- Data_Science\Proyectos\1- Individual\PI - 1\PI01_DATA_ENGINEERING-main\PI_01\Datasets\precios_semana_20200503.json'
df_3 = pd.read_json(path_3)

path_5 = r'C:\Users\barce\OneDrive\Escritorio\HENRY\HENRY_Course\1- Data_Science\Proyectos\1- Individual\PI - 1\PI01_DATA_ENGINEERING-main\PI_01\Datasets\producto.parquet'
df_producto = pd.read_parquet(path_5, engine='pyarrow')

path_6 = r'C:\Users\barce\OneDrive\Escritorio\HENRY\HENRY_Course\1- Data_Science\Proyectos\1- Individual\PI - 1\PI01_DATA_ENGINEERING-main\PI_01\Datasets\sucursal.csv'
df_sucursal = pd.read_csv(path_6, sep=',')

# Eliminar duplicados

dicc = {'df_1_h1':df_1_h1, 'df_1_h2':df_1_h2, 'df_2':df_2,
         'df_3':df_3, 'df_sucursal':df_sucursal, 'df_producto':df_producto}

for key in dicc:
    dicc[key].drop_duplicates(inplace=True)

# Remplazar por 0 los valores faltantes

dicc = {'df_1_h1':df_1_h1, 'df_1_h2':df_1_h2, 'df_2':df_2,
         'df_3':df_3, 'df_sucursal':df_sucursal, 'df_producto':df_producto}
for key in dicc:
    for e in dicc[key]:
        dicc[key][e].fillna(0, inplace=True)

# Ordenar columnas

df_1_h1 = df_1_h1[['precio','producto_id','sucursal_id']]
df_1_h2 = df_1_h2[['precio','producto_id','sucursal_id']]

# Cambio de nombre de columna

df_producto = df_producto.rename(columns={'id':'producto_id'})

# Recucción de la cantidad de decimales de la columna precio

df_3.precio = df_3.precio.replace('',0) # Reemplazar str '' con 0, cambiar type

df_2.precio = df_2.precio.astype(float);
df_1_h1.precio = df_1_h1.precio.astype(float);
df_1_h2.precio = df_1_h2.precio.astype(float);
df_3.precio = df_3.precio.astype(float);

df_1_h1.producto_id.fillna(0,inplace=True) # quitar registro nulos de df_1_h1.producto_id

df_2.precio = df_2.precio.round(2)
df_1_h1.precio = df_1_h1.precio.round(2)
df_1_h2.precio = df_1_h2.precio.round(2)
df_3.precio = df_3.precio.round(2)

# Corección sucursal_id

# Función para normalizar los id

df_1_h2.sucursal_id = df_1_h2.sucursal_id.astype(str)
df_2.sucursal_id = df_2.sucursal_id.astype(str)

def mod_id_suc (x):
    if ' 00:00:00' in x:
        x = x.replace(' 00:00:00','')
        val_1 = x.split('-')[0]
        val_2 = x.split('-')[1]
        val_3 = x.split('-')[2]

        x = val_3 + '-' + val_2 + '-' + val_1
    return x

df_2.sucursal_id = df_2.sucursal_id.apply(mod_id_suc)
df_1_h2.sucursal_id = df_1_h2.sucursal_id.apply(mod_id_suc)

# Crear columnas con las fechas de semana
        # Extracción de fechas de los nombres de los archivos

        # Extraer el texto del cual se sacará la fecha
df_2_texto = os.path.split(path_2)[1]
df_3_texto = os.path.split(path_3)[1]

lista = list(df_1.keys())
df_1_h1_texto = lista[0]
df_1_h2_texto = lista[1]

        # Extraer la fecha del texto
df_2_fecha = [int(df_2_semana) for df_2_semana in re.findall(r'\d+', df_2_texto)][0]
df_3_fecha = [int(df_3_semana) for df_3_semana in re.findall(r'\d+', df_3_texto)][0]
df_1_h1_fecha = [int(df_2_semana) for df_2_semana in re.findall(r'\d+', df_1_h1_texto)][0]
df_1_h2_fecha = [int(df_2_semana) for df_2_semana in re.findall(r'\d+', df_1_h2_texto)][0]

df_2['fecha_semana'] = df_2_fecha
df_3['fecha_semana'] = df_3_fecha
df_1_h1['fecha_semana'] = df_1_h1_fecha
df_1_h2['fecha_semana'] = df_1_h2_fecha

# Exportar DataFrames a csv

# ubicar csv en programdata para poder acceder a ellos desde mysql
df_1_h1.to_csv(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\PI-01\precios_semanas_20200426_20200426_mod_h1.csv', index=False)
df_1_h2.to_csv(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\PI-01\precios_semanas_20200419_20200419_mod_h2.csv', index=False)
df_2.to_csv(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\PI-01\precios_semana_20200413_mod.csv', index=False)
df_3.to_csv(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\PI-01\precios_semana_20200503_mod.csv', index=False)
df_producto.to_csv(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\PI-01\producto_mod.csv', index=False)
df_sucursal.to_csv(r'C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\PI-01\sucursal_mod.csv', index=False)


"""
This dataset assumes that you have a frozen camera rig.
All the scenes are shot using this rig.
The numeration of cameras is consistent across the scenes.
Therefore, the background for each camera (if known) is the same across all the scenes.
Every scene has images taken from all the available cameras.

The dataset files are expected to have the following structure:

| root/
|---- KRT.txt
|---- pose.txt
|---- background_0.jpg
|---- background_1.jpg
|---- scene_0/
     |---- time_0/
          |---- image_0.jpg
          |---- depth_0.jpg
          |---- image_1.jpg
          |---- depth_1.jpg
     |---- time_1/
     |---- time_2/
|---- scene_1/
     |---- time_0/
     |---- time_1/
     |---- time_2/

The file `roles.yaml` should contain the ids of cameras, used to extract the latent variable, and to reconstruct.
Example:

```

- scene_0:
  - time_0:
    - condition_train: [5, 7, 200]
    - reconstruction_train: [0, 2, 4]
    - condition_vis: [100, 19]
    - reconstruction_vis: [0, 1, 3, 11, 15, 17]
  - time_1:
    - condition_train: [6, 8, 201]
    - reconstruction_train: [2, 4, 7]
    - condition_vis: [100, 19, 58]
    - reconstruction_vis: [0, 1,, 11, 15, 17]
- scene_1:
  - time_2:
    - ...
  - time_1:
    - ...
- ...

```
"""

__all__ = ['CameraRigDataset']

import itertools
import logging
import os
import random

import numpy as np
import torch
import torch.nn.functional as F
import torch.utils.data

from lib.utils.io import load_krt, load_pose, load_roles
from .multiview_dataset import MultiviewDataset

logger = logging.getLogger(__name__)
ROLES_PATH = 'roles.yaml'

class CameraRigDataset(MultiviewDataset):
    def __init__(self,
                 root,
                 mode,  # train | val | vis | ...
                 roles_path=None,
                 background_type=None,
                 condition_sampling_type=None,  # random | deterministic
                 condition_sampling_number=None,  # 3
                 condition_data_types=None,  # [image, depth]
                 data_types=('image',),  # [image, background, depth]
                 world_scale=1.,
                 subsample_type=None,
                 subsample_size=0,
                 image_size=(1024, 667),
                 resize_scale=1,
                 fix_intrisics_coef=1,  # TODO: rewrite dryice KRT and get rid of this redundant parameter
                 change_orientation=False,
                 img_ext='jpg',
                 crop_size=None):

        super(torch.utils.data.Dataset, self).__init__()

        self.root = root
        self.img_ext = img_ext
        self.mode = mode
        if roles_path is None:
            roles_path = ROLES_PATH
        if os.path.isabs(roles_path):
            self.roles_path = roles_path
        else:
            self.roles_path = os.path.join(self.root, roles_path)

        self.background_type = background_type
        self.subsample_type = self.parse_subsample_type(subsample_type)
        self.subsample_size = subsample_size

        self.condition_sampling_type = condition_sampling_type
        self.condition_sampling_number = condition_sampling_number
        self.condition_data_types = condition_data_types
        self.data_types = data_types

        # firstly, the image is cropped. Secondly, the picked crop is resized.
        self.image_size = image_size
        self.crop_size = crop_size
        self.resize_scale = resize_scale
        if self.crop_size is not None:
            self.image_size = self.crop_size
        self.image_size = (int(self.image_size[0] / self.resize_scale),
                           int(self.image_size[1] / self.resize_scale))

        transf_path = os.path.join(self.root, 'pose.txt')
        transf = load_pose(transf_path)
        krt_path = os.path.join(self.root, 'KRT.txt')
        self.krt = load_krt(krt_path,
                            image_rescale_ratio=(1 / resize_scale) * fix_intrisics_coef,
                            world_center=transf[:, -1:],
                            world_scale=world_scale,
                            change_orientation=change_orientation,
                            image_size=image_size,
                            center_crop=crop_size,
                            )

        for val in self.krt.values():
            val.update({"image_size": np.array(self.image_size)})

        self.camera_ids = sorted(self.krt.keys())

        scene_prefix = 'scene_'
        time_prefix = 'time_'

        self.scene_time_links = dict()
        self.scene_time_list = []
        self.cumulated_number_of_views = []
        roles = load_roles(self.roles_path)

        for dirpath, subdirnames, filenames in os.walk(self.root, followlinks=True):
            possible_scene, possible_time = dirpath.split(os.sep)[-2:]
            if possible_scene.startswith(scene_prefix) and possible_time.startswith(time_prefix):
                scene_id = possible_scene[len(scene_prefix):]
                time_id = possible_time[len(time_prefix):]

                data = dict()
                try:
                    roles_data = roles[possible_scene][possible_time]
                except KeyError:
                    continue

                self.scene_time_list.append((scene_id, time_id))

                if self.condition_sampling_type not in {'none', None}:
                    data['condition_cameras'] = roles_data[f'condition_{self.mode}']
                else:
                    data['condition_cameras'] = []
                data['reconstruction_cameras'] = roles_data[f'reconstruction_{self.mode}']
                data['dirpath'] = dirpath
                self.scene_time_links[(scene_id, time_id)] = data
                self.cumulated_number_of_views.append(len(data['reconstruction_cameras']))
                self._check_constistency(data)

        self.cumulated_number_of_views = np.cumsum(self.cumulated_number_of_views)

    @property
    def all_cameras_krt(self):
        krt = []
        for index in self.camera_ids:
            camera = self.krt[index]
            camera['camera_name'] = index
            krt.append(camera)

        return {f'frozen_camera_rig': krt}

    def _check_constistency(self, data):
        if len(self.camera_ids) < len(data['condition_cameras']) + len(data['reconstruction_cameras']):
            intersecting_cameras = list(set(data['condition_cameras']) & set(data['reconstruction_cameras']))
            logger.warning(f'Some cameras are used both for conditioning and reconstruction in the folder '
                           f'{data["dirpath"]} -- '
                           + ', '.join(intersecting_cameras)
                           )

        for cam_id, dtype in itertools.product(data['condition_cameras'],
                                               self.condition_data_types,
                                               ):
            if dtype == 'background':
                filepath = os.path.join(self.root, f'{dtype}_{cam_id}.{self.img_ext}')
            else:
                filepath = os.path.join(data['dirpath'], f'{dtype}_{cam_id}.{self.img_ext}')
            if not os.path.exists(filepath):
                logger.warning(f'The condition camera {cam_id} is specified, however file {filepath} is missing')
            if cam_id not in self.krt:
                logger.warning(f'The condition camera {cam_id} is specified, however it is missing in KRT.txt')

        for cam_id, dtype in itertools.product(data['reconstruction_cameras'],
                                               self.data_types,
                                               ):
            if dtype == 'background':
                filepath = os.path.join(self.root, f'{dtype}_{cam_id}.{self.img_ext}')
            else:
                filepath = os.path.join(data['dirpath'], f'{dtype}_{cam_id}.{self.img_ext}')
            if not os.path.exists(filepath):
                logger.warning(f'The reconstruction camera {cam_id} is specified, however file {filepath} is missing')
            if cam_id not in self.krt:
                logger.warning(f'The reconstruction camera {cam_id} is specified, however it is missing in KRT.txt')

    def __getitem__(self, idx):
        position = np.searchsorted(self.cumulated_number_of_views, idx + 1)
        scene_id, time_id = self.scene_time_list[position]
        data = self.scene_time_links[(scene_id, time_id)]

        result = {'scene_id': scene_id,
                  'time_id': time_id,
                  'scene_time': 'frozen_camera_rig',
                  }

        # condition images and cameras
        if self.condition_sampling_type in {None, 'none'}:
            condition_ids = None
        elif self.condition_sampling_type == 'deterministic':
            condition_ids = data['condition_cameras']
        else:
            condition_ids = random.sample(data['condition_cameras'], self.condition_sampling_number)

        if condition_ids is not None:
            condition_keys = ['condition_cameras_ids',
                              'condition_extrinsics',
                              'condition_intrinsics',
                              'condition_images_sizes'
                              ]
            condition_keys.extend([f'condition_{dtype}' for dtype in self.condition_data_types])
            for key in condition_keys:
                result[key] = []
            for cond_id in condition_ids:
                result['condition_cameras_ids'].append(self.camera_ids.index(cond_id))
                result['condition_extrinsics'].append(self.krt[cond_id]['extrin'])
                result['condition_intrinsics'].append(self.krt[cond_id]['intrin'])
                result['condition_images_sizes'].append(self.krt[cond_id]['image_size'])

                for dtype in self.condition_data_types:
                    filepath = os.path.join(data['dirpath'], f'{dtype}_{cond_id}.{self.img_ext}')
                    image, _ = self._read_image(filepath)
                    result[f'condition_{dtype}'].append(self._preprocess_condition_image(image))
            for key in condition_keys:
                result[key] = np.stack(result[key], axis=0)

        # reconstruction camera, images and coords
        if position > 0:
            reconstruction_id = idx - self.cumulated_number_of_views[position - 1]
        else:
            reconstruction_id = idx
        reconstruction_id = data['reconstruction_cameras'][reconstruction_id]

        result['camera_id'] = self.camera_ids.index(reconstruction_id)
        result['extrinsics'] = self.krt[reconstruction_id]['extrin']
        result['intrinsics'] = self.krt[reconstruction_id]['intrin']
        result['images_sizes'] = self.krt[reconstruction_id]['image_size']

        for dtype in self.data_types:
            if dtype == 'background':
                filepath = os.path.join(self.root, f'{dtype}_{reconstruction_id}.{self.img_ext}')
            else:
                filepath = os.path.join(data['dirpath'], f'{dtype}_{reconstruction_id}.{self.img_ext}')
            image, _ = self._read_image(filepath)
            result[dtype] = image

        if self.background_type == 'real':
            assert 'background' in result, "There are no real background. " \
                                           "Probably your forgot add background option in " \
                                           "general_dataset_constants/data_types field in your config."
        elif self.background_type == 'white':
            result['background'] = np.ones_like(result['image']) * 255
        elif self.background_type == 'black':
            result['background'] = np.zeros_like(result['image'])
        elif self.background_type == 'self':
            result['background'] = result['image']

        height, width = result['image'].shape[1:3]
        result['pixel_coords'], result['relative_coords'] = self._sample_pixel_grid(height,
                                                                                width,
                                                                                image=result.get('image'),
                                                                                background=result.get('background'),
                                                                                )
        if result.get('image') is not None and result['pixel_coords'].shape[:2] != result['image'].shape[1:3]:
            result['image'] = F.grid_sample(torch.from_numpy(result['image']).float().unsqueeze(0),
                                            torch.from_numpy(result['relative_coords']).float().unsqueeze(0),
                                            ).squeeze(0)
            if 'background' in result:
                result['background'] = F.grid_sample(torch.from_numpy(result['background']).float().unsqueeze(0),
                                                    torch.from_numpy(result['relative_coords']).float().unsqueeze(0),
                                                    ).squeeze(0)

        return result


import telebot #pip install pyTelegramBotAPI
import configure #Token in another python file

client = telebot.TeleBot(configure.config['token'])

@client.message_handler(content_types =  ['text'])
def get_text(message):
    if message.text.lower() == 'hello':
        client.send_message(message.chat.id,'hello unknown user')
    elif message.text.lower() == 'how are you':
        client.send_message(message.chat.id,'I am okay,and you 😁')

client.polling(none_stop=True,interval=0)


""" Main Datamaker Entrypoint"""
from __future__ import print_function
import argparse
from pathlib import Path

import datamaker.db as db

from multiprocessing import Pool

def _get_hist(pair_id, path, range):
    session = db.Session()
    pair = session.query(db.CurrencyPair).get(pair_id)
    return pair.download_historical_data(path, range)

def get_historical(path=Path(".")):
    """ Get historical"""
    parser = argparse.ArgumentParser(
        description="Datamaker CLI Tool",
        usage='''dm-hist [args]'''
    )
    parser.add_argument(
        "range", type=float, help="Range (in years) of data to get")
    args = parser.parse_args()

    session = db.Session()
    pairs = session.query(db.CurrencyPair).all()
    pool = Pool()
    for pair in pairs:
        pool.apply_async(_get_hist, args=(pair.id, path, args.range))
    pool.close()
    pool.join()

    for pair in pairs:
        print(len(pair.get_historical_database(path).ohlcv))
        pair.deduplicate_historical(path)
        print(len(pair.get_historical_database(path).ohlcv))

if __name__ == "__main__":
    get_historical()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('bp_cupid', '0024_praxen_aktualisiere_zeitraeume'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='platz',
            options={'ordering': ['zeitraum'], 'verbose_name': 'Platz', 'verbose_name_plural': 'Plätze'},
        ),
    ]


import json
import re
import requests

class InvalidInput(Exception):
    pass

class Product:
    def __init__(self, product_url: str):
        self.product_url = product_url

    def url_validation(self):
        try:
            if type(self.product_url) is str and type(int(self.product_url.split("/")[-2])):

                if "myntra.com" in re.findall('://www.([\w\-\.]+)', self.product_url):
                    pass

                else:
                    raise InvalidInput("Provided input is not Myntra's product link")

        except Exception as e:
            raise InvalidInput(e)

    def fetch_data(self):
        from Headers import fetched_header
        self.url_validation()

        product_code = self.product_url.split("/")[-2]

        url = f"https://www.myntra.com/gateway/v2/product/{product_code}"
        fetched_header = fetched_header()

        response = requests.request("GET", url, cookies=fetched_header['cookies'],
                                    headers=fetched_header['headers'], data=fetched_header['payload'])

        raw_data = json.loads(response.text)

        product_name = raw_data['style']['brand']['name']
        image = raw_data['style']['media']['albums'][0]['images'][0]['src']
        to_replace = [["($height)", "732"], ["($qualityPercentage)", "90"], ["($width)", "600"]]

        # Resizing  product image to save network bandwidth
        for _ in range(3):
            image = image.replace(to_replace[_][0], to_replace[_][1])

        # print(raw_data)

        discounted_price = "None"
        mrp = raw_data['style']['mrp']

        for i in raw_data['style']['sizes']:
            if "sizeSellerData" in i:
                for j in i['sizeSellerData']:
                    if "discountedPrice" in j:
                        discounted_price = j['discountedPrice']
                        break

        available_count = 0

        for total in raw_data['style']['sizes']:
            if "sizeSellerData" in total:
                for each in total['sizeSellerData']:
                    if "availableCount" in each:
                        available_count += each['availableCount']

        # If encountered a product with no rating, handling with exception
        try:
            average_rating = raw_data['style']['ratings']['averageRating']
        except TypeError:
            average_rating = "None"

        return json.dumps(
            {'product_name': product_name, "image": image, "discounted_Price": discounted_price, "MRP": mrp,
             "available_Count": available_count, "average_Rating": format(average_rating, '.2f'),
             'product_code': product_code, 'product_url': self.product_url}, indent=4)

def product(url: str):
    return Product(url).fetch_data()


from collections import defaultdict

def solution(s):
    str_dict = defaultdict(int)

    for e in s:
        str_dict[e] += 1

    lst = [0 for _ in range(1 + max(str_dict.values()))]

    for i, e in str_dict.items():
        lst[e] += 1

    lst.reverse()

    cnt = 0

    for i, e in enumerate(lst):
        if i == len(lst) - 1:
            continue
        if e > 1:
            over = lst[i] - 1
            lst[i + 1] += over
            cnt += over

    return cnt

print(solution("ceabaacb"))
print(solution("ccceeeaabbfdz"))


from rest_framework.serializers import (ModelSerializer, SlugRelatedField,
                                        SerializerMethodField)
from .models import PhotoAlbum, Image

class ImageSerializer(ModelSerializer):

    class Meta:
        model = Image
        exclude = ('id',)

class PhotoAlbumSerializer(ModelSerializer):
    image_data= SerializerMethodField()
    thumbnail_data = SerializerMethodField()
    thumbnail = SlugRelatedField(slug_field='uuid',
                                 queryset=Image.objects.all())

    class Meta:
        model = PhotoAlbum
        exclude = ('id',)

    def get_image_data(self, obj):
        image_urls = [{'src':image.image.url, 'width':image.image.width,
                       'height':image.image.height}
                      for image in obj.images.select_related()]
        return image_urls

    def get_thumbnail_data(self, obj):
        return {'uuid':obj.thumbnail.uuid,
                'src':obj.thumbnail.image.url,
                'width':obj.thumbnail.width,
                'height':obj.thumbnail.image.height
               }


s = [char for char in input()]
p = input()
result = 'No'
for i in range(len(s)):
    helper = []
    for j in range(len(p)):
        helper.append(s[j % len(s)])
    if ''.join(helper) == p:
        result = 'Yes'
    s.append(s[0])
    s.pop(0)
print(result)


from flask import request
from flask_restx import marshal_with, fields
from dependency_injector.wiring import Provide, inject
from wtforms import Form as BaseForm, fields as forms_fields, validators

from app.core.presentation.v1.helpers import AbstractResource, ResultEnum, ResultField, marshal_error_fields, jwt_required
from app.core.di.container import Container
from app.core.domain.search.entity import SearchFilter
from app.core.useCase.search import SearchUseCase

class Form(BaseForm):
    title = forms_fields.StringField('Title', [validators.Length(min=2)])
    year = forms_fields.IntegerField('Year')

class Search(AbstractResource):
    data = marshal_error_fields.copy()
    data.update({
        'items': fields.List(
            fields.Nested(
                {
                    'id': fields.Integer,
                    'titles': fields.List(
                        fields.Nested({
                            'language': fields.String(attribute='language.name'),
                            'value': fields.String
                        })
                    ),
                    'year': fields.Integer,
                    'rating': fields.Float
                },
                allow_null=True,
                skip_none=True
            )
        )
    })
    resource_fields = {
        'status': ResultField,
        'data': fields.Nested(data, skip_none=True)
    }

    @jwt_required
    @inject
    @marshal_with(fields=resource_fields, skip_none=True)
    def post(self, searchUseCase: SearchUseCase = Provide[Container.searchUseCase]):
        form = Form(data=request.get_json())
        if not form.validate():
            return self._send_form_error(form.errors, status=400)

        items = searchUseCase.get(
            SearchFilter(
                title=form.title.data,
                year=form.year.data
            )
        )

        return {
            'status': ResultEnum.success,
            'data': {'items': items}
        }


# Starter code for CS 165B HW4

"""
Implement the testing procedure here.

Inputs:
    Given the folder named "hw4_test" that is put in the same directory of your "predictio.py" file, like:
    - Main folder
        - "prediction.py"
        - folder named "hw4_test" (the exactly same as the uncompressed hw4_test folder in Piazza)
    Your "prediction.py" need to give the following required output.

Outputs:
    A file named "prediction.txt":
        * The prediction file must have 10000 lines because the testing dataset has 10000 testing images.
        * Each line is an integer prediction label (0 - 9) for the corresponding testing image.
        * The prediction results must follow the same order of the names of testing images (0.png – 9999.png).
    Notes:
        1. The teaching staff will run your "prediction.py" to obtain your "prediction.txt" after the competition ends.
        2. The output "prediction.txt" must be the same as the final version you submitted to the CodaLab,
        elsewise you will be given 0 score for your hw4.

**!!!!!!!!!!Important Notes!!!!!!!!!!**
    To open the folder "hw4_test" or load other related files,
    please use open('./necessary.file') instaed of open('some/randomly/local/directory/necessary.file').

    For instance, in the student Jupyter's local computer, he stores the source code like:
    - /Jupyter/Desktop/cs165B/hw4/prediction.py
    - /Jupyter/Desktop/cs165B/hw4/hw4_test
    If he use os.chdir('/Jupyter/Desktop/cs165B/hw4/hw4_test'), this will cause an IO error
    when the teaching staff run his code under other system environments.
    Instead, he should use os.chdir('./hw4_test').

    If you use your local directory, your code will report an IO error when the teaching staff run your code,
    which will cause 0 socre for your hw4.
"""
import os
import skimage
from skimage import data
from glob import glob
import numpy as np
from skimage.color import rgb2gray
from skimage import transform
import tensorflow as tf

def load_test_data(label_directory):
    images = []
    files = [os.path.join(label_directory, f)
             for f in os.listdir(label_directory)
             if f.endswith(".png")]
    files.sort()
    for f in files:
        images.append(skimage.data.imread(f))

    return images

def load_training_data(data_directory):
    directories = [d for d in os.listdir(data_directory)
                   if os.path.isdir(os.path.join(data_directory, d))]

    labels = []
    images = []
    directories.sort()
    for d in directories:
        label_directory = os.path.join(data_directory, d)
        files = [os.path.join(label_directory, f)
                 for f in os.listdir(label_directory)
                 if f.endswith(".png")]
        files.sort()
        for f in files:
            images.append(skimage.data.imread(f))
            labels.append(int(d))

    return images, labels

images, labels = load_training_data("./hw4_train")

images = np.array(images)
#images = rgb2gray(images)

x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
y = tf.placeholder(dtype = tf.int32, shape = [None])

images_flat = tf.contrib.layers.flatten(x)

logits = tf.contrib.layers.fully_connected(images_flat, 62, tf.nn.relu)

# Define a loss function
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y,
                                                                    logits = logits))
# Define an optimizer
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)

# Convert logits to label indexes
correct_pred = tf.argmax(logits, 1)

# Define an accuracy metric
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

tf.set_random_seed(1234)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

for i in range(201):
    _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x:images, y:labels})
    #if i%10 == 0:
     #   print(accuracy_val)

test_images = load_test_data("./hw4_test")

test_images = np.array(test_images)
predicted = sess.run([correct_pred], feed_dict={x:test_images})[0]

print(predicted)


#Takes a Summary_counts file from TargetScan and either a miRNA name or a seed sequence.  Returns list of that are targeted by that miRNA or seed sequence.  Optionally, takes a list of genes of interest and will make special note of those genes that are targeted.

#Usage: python miRNAtargetgenes.py --help

import sys
import os
import argparse

def geneswithsites(targetscanfile, genelist, queryseedsite, querymiRNA):
    genes_with_sites = []
    interesting_genes_with_sites = []
    interesting_genes_in_targetscan = 0
    genes_with_seedsites = []
    interesting_genes_with_seedsites = []
    genes_with_miRNAtargets = []
    interesting_genes_with_miRNAtargets = []
    targetscanfh = open(targetscanfile, 'r')
    targetscanlines = []
    genes_with_queryseedsite = []
    for line in targetscanfh:
        line = line.strip().split('\t')
        if line[0] != 'Transcript ID': #skip header line
            targetscanlines.append(line)
    targetscanfh.close()

    if genelist:
        genelistfh = open(genelist, 'r')
        genes_of_interest = []
        for line in genelistfh:
            line = line.strip()
            genes_of_interest.append(line)
        genelistfh.close()

        for gene in genes_of_interest:
            for line in targetscanlines:
                if gene == line[1]:
                    interesting_genes_in_targetscan +=1
                    break
        sys.stderr.write('Found {0} of {1} supplied genes in TargetScan file.\n'.format(interesting_genes_in_targetscan, len(genes_of_interest)))

    for line in targetscanlines:
        transcript = line[0]
        gene = line[1]
        seed = line[2]
        targetsites = int(line[4]) + int(line[5]) + int(line[6]) + int(line[7]) + int(line[8]) + int(line[9]) + int(line[10]) + int(line[11])
        miRNA = line[12]
        if queryseedsite:
            if queryseedsite == seed and targetsites > 0:
                genes_with_seedsites.append(gene)
                if genelist:
                    if gene in genes_of_interest:
                        interesting_genes_with_seedsites.append(gene)
        if querymiRNA:
            if querymiRNA == miRNA and targetsites > 0:
                genes_with_miRNAtargets.append(gene)
                if genelist:
                    if gene in genes_of_interest:
                        interesting_genes_with_miRNAtargets.append(gene)

    if queryseedsite:
        if genelist:
            #Remove duplicates. They could be hits on multiple transcripts of the same gene.
            interesting_genes_with_seedsites = list(set(interesting_genes_with_seedsites))
            genes_with_seedsites = list(set(genes_with_seedsites))
            return genes_with_seedsites, interesting_genes_with_seedsites
        else:
            genes_with_seedsites = list(set(genes_with_seedsites))
            return genes_with_seedsites, None # just easier to always return two things
    if querymiRNA:
        if genelist:
            interesting_genes_with_miRNAtargets = list(set(interesting_genes_with_miRNAtargets))
            genes_with_miRNAtargets = list(set(genes_with_miRNAtargets))
            return genes_with_miRNAtargets, interesting_genes_with_miRNAtargets
        else:
            genes_with_miRNAtargets = list(set(genes_with_miRNAtargets))
            return genes_with_miRNAtargets, None

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--targetscan', type = str, help = 'TargetScan summary counts file')
    parser.add_argument('--genelist', type = str, help = 'Optional. List of genes of particular interest. E.g. Localized genes')
    parser.add_argument('--output', type = str, help = 'Output file.')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--queryseedsite', type = str, help = 'Seed site to search for. Corresponds to 3rd field of TargetScan file.')
    group.add_argument('--querymiRNA', type = str, help = 'miRNA to search for.  Corresponds to 13th field of TargetScan file.')
    args = parser.parse_args()

    outfh = open(args.output, 'w')

    genes_with_hits, interesting_genes_with_hits = geneswithsites(args.targetscan, args.genelist, args.queryseedsite, args.querymiRNA)
    if args.genelist:
        outfh.write('***All genes with hits***\n\n')
        for gene in genes_with_hits:
            outfh.write(gene + '\n')
        outfh.write('\n***Interesting genes with hits***\n\n')
        for gene in interesting_genes_with_hits:
            outfh.write(gene + '\n')
    elif args.genelist == None:
        for gene in genes_with_hits:
            outfh.write(gene + '\n')
    outfh.close()


from typing import List
class Solution:
    def searchRange(self, nums: List[int], target: int) -> List[int]:
        left = 0
        right = len(nums)
        res = []
        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                right = mid
            elif nums[mid] < target:
                left = mid + 1
            elif nums[mid] > target:
                right = mid
        if left == len(nums) or nums[left] != target:
            res += [-1, -1]
            return res
        else:
            res.append(left)

        left = 0
        right = len(nums)
        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] == target:
                left = mid + 1
            elif nums[mid] < target:
                left = mid + 1
            elif nums[mid] > target:
                right = mid
        res.append(right - 1)
        return res

if __name__ == '__main__':
    solution = Solution()
    res = solution.searchRange([5,7,7,8,8,10],8)
    print(res)


from typing import NamedTuple, List, Iterable

from ..core import datetime_aware, Res, LazyLogger
from ..core.compat import removeprefix

logger = LazyLogger(__name__)

class Watched(NamedTuple):
    url: str
    title: str
    when: datetime_aware

    @property
    def eid(self) -> str:
        return f'{self.url}-{self.when.isoformat()}'

# todo define error policy?
# although it has one from google takeout module.. so not sure

def watched() -> Iterable[Res[Watched]]:
    try:
        from ..google.takeout.parser import events
        from google_takeout_parser.models import Activity
    except ModuleNotFoundError as ex:
        logger.exception(ex)
        from ..core.warnings import high
        high("Please set up my.google.takeout.parser module for better youtube support. Falling back to legacy implementation.")
        yield from _watched_legacy()
        return

    YOUTUBE_VIDEO_LINK = '://www.youtube.com/watch?v='

    # TODO would be nice to filter, e.g. it's kinda pointless to process Location events
    for e in events():
        if isinstance(e, Exception):
            yield e

        if not isinstance(e, Activity):
            continue

        url = e.titleUrl
        header = e.header
        title = e.title

        if url is None:
            continue

        if header in {'Image Search', 'Search', 'Chrome'}:
            # sometimes results in youtube links.. but definitely not watch history
            continue

        if header not in {'YouTube', 'youtube.com'}:
            # TODO hmm -- wonder if these would end up in dupes in takeout? would be nice to check
            # perhaps this would be easier once we have universal ids
            if YOUTUBE_VIDEO_LINK in url:
                # TODO maybe log in this case or something?
                pass
            continue

        if header == 'youtube.com' and title.startswith('Visited '):
            continue

        if title.startswith('Searched for') and url.startswith('https://www.youtube.com/results'):
            # search activity, don't need it here
            continue

        if title.startswith('Subscribed to') and url.startswith('https://www.youtube.com/channel/'):
            # todo might be interesting to process somewhere?
            continue

        # all titles contain it, so pointless to include 'Watched '
        # also compatible with legacy titles
        title = removeprefix(title, 'Watched ')

        if YOUTUBE_VIDEO_LINK not in url:
            if e.details == ['From Google Ads']:
                # weird, sometimes results in odd
                continue
            if title == 'Used YouTube' and e.products == ['Android']:
                continue

            yield RuntimeError(f'Unexpected url: {e}')
            continue

        yield Watched(
            url=url,
            title=title,
            when=e.time,
        )

from ..core import stat, Stats
def stats() -> Stats:
    return stat(watched)

### deprecated stuff (keep in my.media.youtube)

get_watched = watched

def _watched_legacy() -> Iterable[Watched]:
    from ..google.takeout.html import read_html
    from ..google.takeout.paths import get_last_takeout

    # todo looks like this one doesn't have retention? so enough to use the last
    path = 'Takeout/My Activity/YouTube/MyActivity.html'
    last = get_last_takeout(path=path)
    if last is None:
        return []

    watches: List[Watched] = []
    for dt, url, title in read_html(last, path):
        watches.append(Watched(url=url, title=title, when=dt))

    # todo hmm they already come sorted.. wonder if should just rely on it..
    return list(sorted(watches, key=lambda e: e.when))


#
#   Author: Klaus Niederberger
#   Release: Marco Winzker, Hochschule Bonn-Rhein-Sieg, 22.12.2022
#
# data-based normalization
# based on "Theory and Tools for the Conversion of Analog to Spiking Convolutional Neurlal Networks" by Rueckauer et al.

def data_normalization(max_activation):
    weight_factor = [1, 1]
    bias_factor = [1, 1]
    v_th = [1, 1]

    for i, value in enumerate(weight_factor):               # layer-wise adaption of weights
        weight_factor[i] = value * max_activation[i] / max_activation[i + 1]
    for i, value in enumerate(bias_factor):                 # layer-wise adaption of biases
        bias_factor[i] = value / max_activation[i + 1]

    return weight_factor, bias_factor, v_th


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="show_month_day",
    version="0.0.1",
    author="Tom",
    author_email="s099064146@gmail.com",
    description="Input string of starting year, month, date and ending year, month, date.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/s099064146/Practice",
    packages=setuptools.find_packages(),
    classifiers=(
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ),
)