###############################################
# Author: Aniruddha Gokhale
# Updated by: Patrick Muradaz
# Vanderbilt University
# Purpose: Subscriber application for PAs
# Semester: Spring 2023
###############################################
#
# The key steps for the subscriber application are
# (1) parse command line and configure application level parameters
# (2) obtain the subscriber middleware object and configure it.
# (3) As in the publisher, register ourselves with the discovery service
# (4) since we are a subscriber, we need to ask the discovery service to
#     let us know of each publisher that publishes the topic of interest to
#     us. Then our middleware object will connect its SUB socket to all these
#     publishers for the Direct strategy else connect just to the broker.
# (5) Subscriber will always be in an event loop waiting for some matching
#     publication to show up. We also compute the latency for dissemination and
#     store all these time series data in some database for later analytics.
#
# Import statements
import sys, os, time, argparse, configparser, logging, random
sys.path.append(os.getcwd())
from Apps.Common.common import handle_exception, dump
from Apps.Common.topic_selector import TopicSelector
from Apps.Subscriber.middleware import SubscriberMW

"""SubscriberAppln class"""
class SubscriberAppln():

  """constructor"""
  def __init__(self, logger):
    self.logger = logger      # internal logger for print statements
    self.name = None          # our name (some unique name)
    self.addr = None          # our advertised IP address
    self.port = None          # port num where we are going to publish our topics
    self.topiclist = None     # the different topics that we subscribe to
    self.lookup = None        # one of the diff ways we do lookup
    self.dissemination = None # direct or via broker
    self.mw_obj = None        # handle to the underlying Middleware object

  """configure/initialize"""
  def configure(self, args):
    try:
      self.logger.debug("SubscriberAppln::configure")
      # initialize our variables
      self.name = args.name
      self.port = args.port
      self.addr = args.addr
      # Now, get the configuration object
      config = configparser.ConfigParser()
      config.read(args.config)
      self.lookup = config["Discovery"]["Strategy"]
      self.dissemination = config["Dissemination"]["Strategy"]
      # Now get our topic list of interest
      ts = TopicSelector()
      # self.topiclist = ts.interest()
      self.topiclist = ["light", "humidity"]
      # Now setup up our underlying middleware object
      self.mw_obj = SubscriberMW(self.logger)
      self.mw_obj.configure(args)
      self.logger.info("Subscriber app configured.")
      dump(self.logger, "SubscriberAppln", self.addr, self.port,
           name=self.name, topiclist=self.topiclist)
    except Exception as e: handle_exception(e)

  """driver program"""
  def driver(self):
    try:
      self.logger.debug("SubscriberAppln::driver")
      # Use middleware to register us with the discovery service
      self.logger.info("Registering app with discovery service.")
      result = self.mw_obj.register(self.name, self.topiclist)
      self.logger.debug(f"SubscriberAppln::driver - result: {str(result)}")
      self.logger.info("Subscriber app registered.")
      # Keep checking with the discovery service if we are ready to go
      self.logger.info("Waiting for all ready from discovery service.")
      while(not self.mw_obj.is_ready()): time.sleep(0.5)  # sleep between calls
      self.logger.info("All sub and pub apps registered and ready.")
      time.sleep(random.uniform(0.0, 1.0)) # sleep for a bit to not overwhelm system
      # Now, find the publishers that match our topics
      self.logger.info("Locating all publishers for our topics.")
      pubs = self.mw_obj.locate_pubs(self.topiclist)
      # Finally, subscribe and listen to the publishers
      self.logger.info("Subscribing to relevant publishers.")
      self.mw_obj.sub_to_pubs(pubs, self.topiclist)
    except Exception as e: handle_exception(e)

"""Parse command line arguments"""
def parseCmdLineArgs():
  # instantiate a ArgumentParser object
  parser = argparse.ArgumentParser(description="Subscriber Application")
  # Now specify all the optional arguments we support
  parser.add_argument(
    "-n", "--name", default="sub",
    help="Some name assigned to us. Keep it unique per subscriber."
  )
  parser.add_argument(
    "-a", "--addr", default="localhost",
    help="IP addr of this subscriber to advertise (default: localhost)"
  )
  parser.add_argument(
    "-p", "--port", default="5566",
    help="Port number on which our underlying subscriber ZMQ service runs, " +
      "default=5566"
  )
  parser.add_argument(
    "-d", "--discovery", default="localhost:5555",
     help="IP Addr:Port combo for the discovery service, " +
      "default localhost:5555"
  )
  parser.add_argument(
    "-c", "--config", default="Apps/Common/config.ini",
    help="configuration file (default: Apps/Common/config.ini)"
  )
  parser.add_argument(
    "-l", "--loglevel", type=int, default=logging.INFO,
    choices=[
      logging.DEBUG,logging.INFO,logging.WARNING,
      logging.ERROR,logging.CRITICAL
    ],
    help="logging level, choices 10,20,30,40,50: default 20=logging.INFO"
  )
  return parser.parse_args()

"""Main program"""
def main():
  try:
    # obtain a system wide logger and initialize it to debug to begin with
    logging.debug("Main - acquire a child logger to log messages in")
    logger = logging.getLogger("SubAppln")
    # first parse the arguments
    logger.debug("Main: parse command line arguments")
    args = parseCmdLineArgs()
    # reset the log level to as specified
    logger.debug("Main: resetting log level to {}".format(args.loglevel))
    logger.setLevel(args.loglevel)
    logger.debug("Main: effective log level is {}".format(logger.getEffectiveLevel()))
    # Obtain a subscriber application
    logger.debug("Main: obtain the object")
    sub_app = SubscriberAppln(logger) # get the object
    sub_app.configure(args)           # configure the object
    sub_app.driver()                  # invoke the object driver
  except Exception as e: handle_exception(e)

"""Main entry point"""
if __name__ == "__main__":
  # set underlying default logging capabilities
  logging.basicConfig(level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  main()


#!/usr/bin/env -S python -W ignore

# anonymize.py

# Standard library imports
import json
import uuid
import random
import argparse

# Third-party imports
from faker import Faker

# Local application/library imports
# ...

fake = Faker()

def anonymize_data(data, anonymization_rules):
    """Recursively anonymize specific fields in the given data."""
    if isinstance(data, dict):
        return {key: anonymize_value(key, value, anonymization_rules) for key, value in data.items()}

    if isinstance(data, list):
        return [anonymize_data(item, anonymization_rules) for item in data]

    return data

def anonymize_value(key, value, anonymization_rules):
    """Anonymize individual value based on the key using provided faker functions."""
    if key in anonymization_rules:
        faker_function = anonymization_rules[key]
        return faker_function()

    if isinstance(value, (dict, list)):
        return anonymize_data(value, anonymization_rules)

    return value

def anonymize_json_file(input_file, output_file, anonymization_rules):
    """Read JSON from file, anonymize it, and write to another file."""
    with open(input_file, "r", encoding="utf-8") as file:
        data = json.load(file)

    anonymized_data = anonymize_data(data, anonymization_rules)

    with open(output_file, "w", encoding="utf-8") as file:
        json.dump(anonymized_data, file, indent=4)

def main():
    """
    Anonymizes the JSON data in the specified file.

    Args:
        file (str): The JSON file to be anonymized.

    Returns:
        None
    """
    parser = argparse.ArgumentParser(description="Anonymize JSON data.")
    parser.add_argument("file", help="The JSON file to be anonymized")

    args = parser.parse_args()

    input_file = args.file
    output_file = f"{input_file}.anon.json"

    anonymization_rules = {
        "created": lambda: fake.date_time_this_month().isoformat(" "),
        "updated": lambda: fake.date_time_this_month().isoformat(" "),
        "name": fake.name,
        "display_name": fake.name,
        "city": fake.city,
        "description": lambda: fake.text(max_nb_chars=80),
        "second_line": lambda: fake.text(max_nb_chars=20),
        "public_nick_name": fake.name,
        "id": lambda: random.randrange(100000, 999999),
        "monetary_account_id": lambda: random.randrange(100000, 999999),
        "iban": fake.iban,
        "url": fake.url,
        "target_url": fake.url,
        "uuid": lambda: str(uuid.uuid4()),
        "anchor_uuid": lambda: str(uuid.uuid4()),
        "attachment_public_uuid": lambda: str(uuid.uuid4()),
        "latitude": lambda: float(fake.latitude()),
        "longitude": lambda: float(fake.longitude()),
        "altitude": lambda: float(random.uniform(0, 1) * 100),
    }

    anonymize_json_file(input_file, output_file, anonymization_rules)
    print(f"Anonymized file saved as: {output_file}")

if __name__ == "__main__":
    main()


'''
26. Faça um programa que leia dois vetores de 10 elementos.
Crie um vetor que seja a intersecção entre os q2 vetores anteriores, ou seja,
que contêm apenas os números que estão em ambos os vetores. Não deve conter números repetidos.
'''
a = input("Digite 10 números de alunos separados por espaço: ").split(" ")
b = input("Digite as alturas dos 10 alunos separadas por espaço: ").split(" ")

if len(a) != 10 and len(b) != 10:
    print("Precisa digitar 10 itens para cada")
    exit(1)

intersec = []

for i in a:
    for j in b:
        if j == i:
            intersec.append(i)

print("Intersecção = ", intersec)


import re
import requests

from django.http import Http404

from rest_framework import serializers

from finder.models import State, City, Address
from address_finder.settings import CORREIOS_API

class AddressSerializer(serializers.BaseSerializer):

    def to_internal_value(self, data):
        cep = data.get('cep')

        if not cep:
            raise serializers.ValidationError({
                'cep': 'This field is required.'
            })

        if not re.search('^([0-9]{8})$', cep):
            raise serializers.ValidationError({
                'cep': 'Invalid Cep.'
            })

        return {
            'cep': cep
        }

    def to_representation(self, obj):
        return {
            'id': obj.id,
            'address': obj.address,
            'district': obj.district,
            'complement': obj.complement,
            'city': obj.city.name,
            'cep': obj.cep
        }

    def save(self, validated_data):
        url = '{0}/{1}/json'.format(CORREIOS_API.get('url'),
                                    validated_data.data.get('cep'))
        response = requests.get(url)
        data_response = response.json()

        if response.status_code == 200 and not data_response.get('erro'):
            state = State.objects.create(slug=data_response.get('uf'))
            city = City.objects.create(name=data_response.get('localidade'),
                                       state=state)
            return Address.objects.create(address=data_response.get('logradouro'),
                                          complement=data_response.get('complemento'),
                                          district=data_response.get('bairro'),
                                          cep=data_response.get('cep'),
                                          city=city)

        raise Http404

    def validate(self, data):
        """
        Check if the data contains only numbers.
        """
        if re.search('^[0-9]+$', data):
            raise serializers.ValidationError("Only numbers are accepted")
        return data


class Solution:
    def prefixesDivBy5(self, nums: List[int]) -> List[bool]:
        ans = []
        k = ''
        for i in nums:
            k = k + str(i)
            if int(k,2) % 5 == 0:
                ans.append(True)
            else:
                ans.append(False)
        return ans


# https://leetcode.com/problems/maximize-score-after-n-operations/
from functools import cache
from typing import *
from math import gcd
import bisect

class Solution:
    def maxScoreOLD(self, nums: List[int]) -> int:
        n = len(nums) // 2
        gcds = []
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                g = (gcd(nums[i], nums[j]), i, j)
                bisect.insort(gcds, g)

        d = {}

        def myFilter(local_gcds, ind):
            ele = set(local_gcds[ind][1:])

            def test(x):
                if x[1] in ele or x[2] in ele:
                    return False
                return True

            return list(filter(test, local_gcds))

        def DB(local_gcds, local_n) -> int:
            if not local_n:
                return 0
            if (tuple(local_gcds), local_n) in d:
                return d[(tuple(local_gcds), local_n)]
            if len(local_gcds) == 1:
                d[(tuple(local_gcds), local_n)] = local_gcds[0][0] * local_n
                return d[(tuple(local_gcds), local_n)]
            ans = DB(myFilter(local_gcds[:], -1), local_n - 1) + local_n * local_gcds[-1][0]
            local_i = len(local_gcds) - 2
            while local_i:
                t1 = DB(myFilter(local_gcds[:], local_i), local_n - 1) + local_n * local_gcds[local_i][0]
                ans = t1 if t1 > ans else ans
                local_i -= 1
            d[(tuple(local_gcds), local_n)] = ans
            return ans

        return DB(gcds, n)

    def maxScore(self, nums: List[int]) -> int:
        @cache
        def DB(local_nums, n):
            if not local_nums:
                return 0
            ans = 0
            for i in range(len(local_nums)):
                for j in range(i + 1, len(local_nums)):
                    new = local_nums[:i] + local_nums[i + 1:j] + local_nums[j + 1:]
                    ans = max(ans, n * gcd(local_nums[i], local_nums[j]) + DB(tuple(new), n - 1))
            return ans

        return DB(tuple(nums), len(nums) // 2)

S = Solution()
# X = S.maxScore([6, 3, 4, 3, 5, 3, 5, 10, 10, 3, 1, 7, 3, 3])
X = S.maxScore(
    [109497, 983516, 698308, 409009, 310455, 528595, 524079, 18036, 341150, 641864, 913962, 421869, 943382, 295019])
# Y = S.maxScore1(
#     [109497, 983516, 698308, 409009, 310455, 528595, 524079, 18036, 341150, 641864, 913962, 421869, 943382, 295019])
print(X)


"""
Advent of Code 2022, Day 1!
Alastair Cottier
December 1, 2022

"""

text_file = open('advent_day_1.txt', 'r')
lines = text_file.readlines()
cals = 0
elf_totals = []
top3 = 0

for line in lines:
    line.strip()
    if line != '\n':  # still in current elf's bag, add cals
        cals += int(line)
    else:  #new elf, add pprevious elf's total to list
        elf_totals.append(cals)
        cals = 0  #reset cals forr new elf

elf_totals.sort()  #ascending order
length = len(elf_totals)
top3_list = elf_totals[length-3:] #splice list for last 3 elfs totals // new list top3_list

for item in top3_list:  #sum top3_list
    top3 += int(item)

print(max(elf_totals))  #highest cals
print(top3)  #to 3 highest cals


# -*- coding: utf-8 -*-
"""
Created on Thu Jan 12 20:19:25 2023

@author: linus
"""

import os
import time
import csv
from Hasse import check_ORG_LOR,create_Hasse
from Analysis import Analysis
from reactionnetwork import create_closures, SBML_to_RN
from LP_compartments import get_min_compartments,get_MCs

def calculate_os(analyze):
    orgs=0
    for i in range(len(analyze.DOs)):
        if is_DO_O(analyze,i):
            orgs+=1
    return(orgs)

def is_DO_O(analyze,i):

    for rea in analyze.reaction_network.reactions:
        if rea.defined_name not in analyze.SOR_map[analyze.DOs[i]][-1]:
            if set(rea.listOfReactants).issubset(analyze.DOs[i]):
                return(False)
    return(True)

def iterate_over_database(path="C:/Users/linus/python/nice_BM2/", start_index=0,end_index=3000, exelname=False):

    header = ['ID',"name",'number_species', 'number_reactions', 'number_SORs',\
              'number_SOR_O', "number_SOR_DO_only","number_DO","number_Os",\
                  "max_MRC","comp2","comp3","comp4","comp5","comp6","timer_MRC","reaction_orders",\
                  "Timer_ERC","Timer_LP","Timer_LP_DO1","number_constr","time_complete"]
    more_timers=False
    if more_timers:
        header = ['ID',"name",'number_species', 'number_reactions', 'number_SORs',\
              'number_SOR_O', "number_SOR_DO_only","number_DO","number_Os",\
                  "max_MRC","comp2","comp3","comp4","comp5","comp6","timer_MRC","reaction_orders",\
                  "Timer_ERC","Timer_LP","Timer_LP_DO1","number_constr","timer_all1","timer_all2","timer_all3","timer_all4"]
    if exelname:
        exel_insert=str(exelname)
    else:
        exel_insert="output_table"
    if not os.path.exists("test-output/"):
        os.makedirs("test-output/")

    global safe_object
    with os.scandir(path) as sbml_folder, open('test-output/' + exel_insert + '.csv', 'w', newline='',encoding="utf-8") as exel:
        writer_csv = csv.writer(exel, delimiter=';')
        writer_csv2 = csv.DictWriter(exel, header,delimiter=';')
        writer_csv.writerow(header)

        current_index=-1
        dead_entries=[560,595,909,908,1061]
        dead_entries=[]
        #only_entries=[496,497,255,560,595,909,908,1061]
        #only_entries=[496,497,255,1061]
        for entry in sbml_folder:
            #skips files before reaching start_index
            if start_index-1 >current_index:
                current_index+=1
                continue
            for i in dead_entries:
                if entry.name.endswith(str(i)+".xml"):
                    continue
            run=0
            #for i in only_entries:
            #    if entry.name.endswith(str(i)+".xml"):
            #        run=1
            #        break
            #if not run:
            #    continue

            if end_index <=current_index:
                break
            current_index+=1

            #check for file integrity
            if  ".orgml" in entry.name:
                continue

            if (entry.name.endswith(".sbml") or entry.name.endswith(".xml")) and entry.is_file():
                    #job_queueueue.append([entry.path,current_index])
                    result_line=calculate_RN([entry.path,current_index])
                    writer_csv2.writerow(result_line)
                    exel.flush()

def calculate_RN(input_list):
    more_timers=False
    entry,current_index=input_list
    print(current_index)
    RN=SBML_to_RN(str(entry))
    numbers_array=[0,0,0,0,0,0,0,0,0]
    for ele in RN.reactions:
        numbers=len(ele.listOfReactants)
        try:
            numbers_array[numbers]+=1
        except IndexError:
            numbers_array=numbers
            break

    RN.replace_inflow_by_selfreplication()
    current_SBML=Analysis(RN)
    #call of analysis
    global database_dict
    if current_SBML.reaction_network is None:
        print(entry)
        return()

    #insert basic data
    outputdict={}
    outputdict["reaction_orders"]=numbers_array
    timer_all=time.time()

    outputdict['ID']=os.path.basename(entry)
    outputdict['name']=current_SBML.name

    #calculate SORs
    current_SBML.get_ERCs()
    current_SBML.all_SORs()
    SOR_o=0
    for ele in current_SBML.SORs:
        if check_ORG_LOR(ele,current_SBML.reaction_network):
            SOR_o+=1

    #insert data of SORs
    outputdict['number_SORs']=len(current_SBML.SORs)
    outputdict['number_SOR_O']=SOR_o
    outputdict['number_SOR_DO_only']=len(current_SBML.SORs)-SOR_o

    if hasattr(current_SBML,"abort_run"):
        outputdict["termination"]="ERC timeout"
        return(outputdict)

    if len(current_SBML.reaction_network.reactions)==0:
        return outputdict

    outputdict['number_species']=len(current_SBML.reaction_network.species)
    outputdict['number_reactions']=len(current_SBML.reaction_network.reactions)
    outputdict["number_constr"]=current_SBML.metadata["number_constr"]

    outputdict["Timer_ERC"]=str(current_SBML.metadata["Time_ERC"]).replace('.',',')
    outputdict["Timer_LP"]=str(current_SBML.metadata["Timer_LP"]).replace('.',',')

    start_MRC=time.time()
    cancel_mrc=False
    get_comp=True
    compartment_2=0
    compartment_3=0
    compartment_4=0
    compartment_5=0
    compartment_6=0
    maxnumbercomp=0
    timer1=str(time.time()-timer_all).replace('.',',')

    if get_comp==True:
        for i in range(len(current_SBML.SORs)):
            if not check_ORG_LOR(current_SBML.SORs[i],current_SBML.reaction_network):
                if current_SBML.SORs[i][0]:
                    compartments=[]
                    try:
                        solution_gc=get_MCs(current_SBML.reaction_network,current_SBML.SORs[i])
                        maxnumbercomp=max(maxnumbercomp,len(solution_gc))
                        compartments=get_min_compartments(current_SBML.reaction_network, solution_gc, current_SBML.SORs[i])
                    except MemoryError:
                        cancel_mrc=True
                        break

                    #compartments=get_min_compartments(current_SBML.reaction_network, solution_gc, current_SBML.SORs[i])
                else: compartments=[]
                #if len(compartments)>2:
                 #   compartment_counter.append([i,len(compartments)])
                if len(compartments)==2:
                    compartment_2+=1
                if len(compartments)==3:
                    compartment_3+=1
                if len(compartments)==4:
                    compartment_4+=1
                if len(compartments)==5:
                    compartment_5+=1
                if len(compartments)>5:
                    compartment_6+=1

                if int(time.time()-start_MRC)>300:
                    cancel_mrc=True
                    print("cANCELA")
                    break
        if cancel_mrc:
            outputdict["timer_MRC"]="timeout"
        else:
            outputdict["timer_MRC"]=str(time.time()-start_MRC).replace('.',',')

        outputdict["max_MRC"]=maxnumbercomp
        outputdict["comp2"]=compartment_2
        outputdict["comp3"]=compartment_3
        outputdict["comp4"]=compartment_4
        outputdict["comp5"]=compartment_5
        outputdict["comp6"]=compartment_6
    else:
    #outputdict["compartment_lengths"]=compartment_counter
        outputdict["max_MRC"]=""
        outputdict["comp2"]=""
        outputdict["comp3"]=""
        outputdict["comp4"]=""
        outputdict["comp5"]=""
        outputdict["comp6"]=""
        outputdict["timer_MRC"]=""
    timer2=str(time.time()-timer_all).replace('.',',')
    #outputdict["Timer_LP_DO2"]=str(time.process_time()-start_DO2_time).replace('.',',')

    current_SBML.all_DOs()
    timer3=str(time.time()-timer_all).replace('.',',')
    outputdict["Timer_LP_DO1"]=str(current_SBML.metadata["Timer_LP"]).replace('.',',')

    Os=calculate_os(current_SBML)

    outputdict["number_Os"]=Os
    outputdict["number_DO"]=len(current_SBML.DOs)

    #if hasattr(current_SBML,"SORs_wrong"):
     #   outputdict['termination']=current_SBML.SORs_wrong
      #  timeout_counter=1

    if more_timers:

        outputdict["timer_all1"]=timer1

        outputdict["timer_all2"]=timer2

        outputdict["timer_all3"]=timer3
        outputdict["timer_all4"]=str(time.time()-timer_all).replace('.',',')
    else:
        outputdict["time_complete"]=str(time.time()-timer_all).replace('.',',')

    return outputdict

def get_species_closure(loR,list_of_SORs):
    o_set=set()

    for SOR in list_of_SORs:
        if check_ORG_LOR(SOR[0],loR):
            species_set=set()
            for reaction in loR:
                if reaction.defined_name in SOR[0]:
                    species_set.update(reaction.listOfReactants)
                    species_set.update(reaction.listOfProducts)
            species_set_frozen=frozenset(species_set)
            o_set.add(species_set_frozen)
    return(o_set)

def get_species_closure2(loR,list_of_SORs):
    SOR_species=[]

    for SOR in list_of_SORs:
        species_set=set()
        for reaction in loR:
            if reaction.defined_name in SOR[0]:
                species_set.update(reaction.listOfReactants)
                species_set.update(reaction.listOfProducts)
        species_set_frozen=frozenset(species_set)
        SOR_species.append(species_set_frozen)
    return(SOR_species)

def calculate_number_DO(listofSpecies,SORs,LOR):
    species_min_SOR=set()
    for rea in SORs[0][0]:
        for reaction in LOR:
            if reaction.defined_name==rea:
                species_min_SOR.add(frozenset(set(reaction.listOfProducts).union(set(reaction.listOfReactants))))
    free_spec=set()
    for spec in listofSpecies:
        if spec not in species_min_SOR:
            free_spec.add(spec)
    #get ERC of species_existence
    ERC_dict1= create_closures(LOR, species=free_spec, Timer_in_sec=120)
    dict1,dict2=create_Hasse(SORs,LOR, analyze_only=True)
    #get SORs, where species can exist

    species_of_SORs=get_species_closure2(LOR,SORs)

    new_map={}
    combi_spec=0
    combi_spec=[]
    print(ERC_dict1)
    #für jede abh. spez
    for ele in ERC_dict1:
        if len(ERC_dict1[ele].reactions)!=0:
            print("stuff")
            print(ele)
            print(type(ERC_dict1[ele].reactions[0]))
            print(ERC_dict1[ele].reactions)
            #für jeden vertex
            for solution_index in range(len(SORs)):
                print("vertex")
                print(dict1[solution_index])
                #wenn rea1 grün
                if ERC_dict1[ele].reactions[0].defined_name in dict1[solution_index]:
                    if ele not in new_map:
                        new_map[ele]=[species_of_SORs[solution_index]]
                    else:
                        new_map[ele].append(species_of_SORs[solution_index])
                    print()
        else:
            combi_spec.append(ele)
    print(new_map)
    print(combi_spec)
    number_DOs=0
    number_DOs+=2**len(combi_spec)
    for ele in new_map:
        combi_sec_ele=combi_spec
        for opportunity in new_map[ele]:
            for species in opportunity:
                if species in combi_spec:
                    combi_sec_ele-=1
            number_DOs+=2**len(combi_spec)


import threading
import sys
import time

#https://ithelp.ithome.com.tw/articles/10254439
def socket1():
    # for i in range(1,6):
    lock_s2.acquire()
    print('socket-server1')
    lock_s1.release()
    time.sleep(0.1)

def socket2():
    # for i in range(1,6):
    lock_s1.acquire()
    print('socket-server2')
    lock_s2.release()
    time.sleep(0.1)

if __name__ == "__main__":
    lock_s1 = threading.Lock()
    lock_s2 = threading.Lock()

    t1 = threading.Thread(None,socket1())
    t2 = threading.Thread(None,socket2())
    lock_s1.acquire()
    t1.start()
    t2.start()
    t1.join()


# Importing the relevant modules
import matplotlib.pyplot as plt

# Plotting Graphs in Python

# Very basic plot - wee want something better!
#x = [1, 3, 5, 10] # This is what we are plotting
#plt.plot(x)
#plt.show() # This will show the plot we are plotting

# Plotting x and y against each other
#y = [7, 12, 21, 22]
#.plot(x, y)
#plt.show()

# Let's plot a lovely looking plot

# line 1 - Points
x = [3, 9, 14]
y = [2, 7, 30]
# Plotting x and y
plt.plot(x, y, c="red", linewidth=2, label="Line 1", marker="o")

# Line 2 - Points
x2 = [1, 15, 18]
y2 = [0, 3, 12]
# Plotting x2 and y2
plt.plot(x2, y2, c="green", linewidth=0.5, label="2", linestyle="dashed",
         marker="s", markerfacecolor="orange", markersize=10)

# Label the axes and give the plot a title
plt.xlabel("X-axis")
plt.xlabel("Y-axis")
plt.title("Two lines!")

# Limit of the axes
plt.ylim(1, 10)
plt.xlim(0, 30)

# Show the legend on the plot
plt.legend()

# Get Python to show the plot
plt.show()


a = int(input())
b = int(input())
c = int(input())
if a == 0 :
    print('Đây không phải PT bậc 2')
d = b**2 - 4*a*c
if d<0:
    print('PTVN')
elif d == 0:
    print('PT có nghiệm kép',-b/(2*a))
else:
    print('PT có 2 nghiệm x1=', (-b+d**0.5)/(2*a), 'x2=',(-b-d**0.5)/(2*a))


from datetime import datetime, timedelta
from airflow.decorators import dag
from tasks.ktc import (
    ktc_web_scraper,
    data_validation,
    ktc_player_load,
    surrogate_key_formatting,
)

dag_owner = "dynasty_superflex_db"

@dag(
    default_args={
        "owner": dag_owner,
        "depends_on_past": False,
        "email": ["grayson.stream@gmail.com"],
        "email_on_failure": False,
        "email_on_retry": False,
        "retries": 1,
        "retry_delay": timedelta(minutes=5),
    },
    description="Web Scaper pulling in KTC Player data to build Superflex Power Rankings",
    schedule_interval="30 * * * *",
    start_date=datetime(2022, 6, 8),
    catchup=False,
    tags=["scraper", "database"],
)
def ktc_player_task_api():

    player_data = ktc_web_scraper()
    player_validation = data_validation(player_data)
    player_load = ktc_player_load(player_validation)
    surrogate_key_formatting(player_load)

ktc_player_dag = ktc_player_task_api()


import json

import aio_pika

async def listen_to_mq(loop, app):
    connection = await aio_pika.connect_robust(
        "amqp://guest:guest@127.0.0.1/",
        loop=loop
    )

    async with connection:
        queue_name = "test_queue"
        channel = await connection.channel()
        queue = await channel.declare_queue(queue_name, auto_delete=True)

        async with queue.iterator() as queue_iter:
            async for message in queue_iter:
                async with message.process():
                    mq_data = message.body.decode()
                    data = json.loads(mq_data)

                    for ws_data in app['websockets']:
                        if ws_data.get('id') == str(data.get('id')):
                            await ws_data.get('ws').send_str(str(data.get('msg')))


from typing import Dict, List
from assetuniverse import AssetUniverse
import numpy as np
import scipy.optimize as opt
import pandas as pd

class Subportfolio(object):
    def __init__(self, params: Dict, au: AssetUniverse, assets: pd.DataFrame, id: int, total_ids: int, rebalance_dates: pd.DatetimeIndex):
        self.lookback = params[0]
        self.momentum_metric = params[1]
        self.subportfolio_threshold = params[2]
        self.subportfolio_min_keep = params[3]
        self.max_ind_allocation = params[4]

        self.au = au
        self.rebalance_dates = rebalance_dates

        self.target_weights = self.set_target_weights()

        # Print status
        if id % 10 == 0 and id > 0:
            print(f'Finished subportfolio: {id} of {total_ids}')

    def set_target_weights(self):
        """Set the target weights on all rebalance dates
        """
        tickers = self.au.tickers(include_cash=False, include_borrow_rate=False)
        target_weights = self.au.prices(tickers=tickers).copy(deep=True)*0     # Reset to 0
        earliest_rebalance = self.au.prices(tickers=tickers).index[self.lookback + 1]
        for date_window_end in self.rebalance_dates:
            if date_window_end > earliest_rebalance:
                date_window_start = date_window_end - np.timedelta64(int(7.4*self.lookback/5), 'D')
                best_assets = self.sort_by_metric(date_window_start, date_window_end)
                target_weights.loc[date_window_end, :] = self.optimize_target_weights(
                    best_assets,
                    date_window_start,
                    date_window_end
                )

        return target_weights

    def optimize_target_weights(self, assets: List, date_window_start, date_window_end):
        """Calculate the optimal target weights given the history of returns

        Parameters
        ----------
        returns : pd.DataFrame
            History of returns
        """
        # covmatrix = self.au.returns().loc[date_window_start:date_window_end, assets].cov().values
        covmatrix = self.au.covariance_matrix(assets, date_window_start, date_window_end)
        cons = [
            {'type': 'eq', 'fun': lambda x: np.sum(x) - 1.0},           # sum of weights must match 1.0
            ]

        result = opt.minimize(
            fun=self._expected_variance,
            x0=np.ones(len(assets))/len(assets),
            args=covmatrix,
            method='SLSQP',
            constraints=cons,
            bounds=opt.Bounds(0, self.max_ind_allocation),
            tol=1e-13,
            options={'maxiter': 1000, 'disp': False}
        )
        optimized_weights = result.x.transpose()

        tickers = self.au.tickers(include_cash=False, include_borrow_rate=False)
        weights = pd.Series(
            data=np.zeros(len(tickers)),
            index=tickers
            )
        weights.loc[assets] = optimized_weights
        return weights

    def _expected_variance(self, weights, covmatrix):
        return np.matmul(weights, np.matmul(covmatrix, weights.T))

    def sort_by_metric(self, date_window_start, date_window_end) -> List:
        tickers = self.au.tickers(include_cash=False, include_borrow_rate=False)
        metric, ascending = self.momentum_metric(
            self.au.prices(tickers=tickers, start=date_window_start, end=date_window_end),
            self.au.returns(tickers=tickers, start=date_window_start, end=date_window_end),
            self.lookback
            )
        metric = metric.sort_values(ascending=ascending)
        num_keep = int(np.ceil(max(self.subportfolio_threshold*len(tickers), self.subportfolio_min_keep)))
        return metric[-num_keep:].index.to_list()

class FixedAllocationSubportfolio(Subportfolio):
    def __init__(self, percentage_allocations: List, au: AssetUniverse, assets: pd.DataFrame, rebalance_dates: pd.DatetimeIndex):
        self.percentage_allocations = percentage_allocations
        self.au = au
        self.rebalance_dates = rebalance_dates
        self.target_weights = self.set_target_weights()

    def set_target_weights(self):
        tickers = self.au.tickers(include_cash=False, include_borrow_rate=False)
        target_weights = self.au.prices(tickers=tickers).copy(deep=True)*0     # Reset to 0
        for date_window_end in self.rebalance_dates:
            target_weights.loc[date_window_end, :] = self.percentage_allocations

        return target_weights


import numpy as np
from PIL import Image
import pickle
import LogisticRegression
from scipy.special import expit

def mean_std(X):
    return (X - np.mean(X)) / np.std(X)

def binary_predict(image_loc, theta_loc):
    np_im = convert_image(image_loc)
    theta = pickle.load(open(theta_loc, 'rb'))
    prediction = LogisticRegression.predict(np_im, theta)
    print("Output:", str(prediction))
    if (prediction >= 0.5):
        print("Predicted Value:", str(1))
    else:
        print("Predicted Value:", str(0))

    return prediction

def multiclass_predict(image_loc):
    np_im = convert_image(image_loc)
    predictions = np.zeros([10, 1])
    for numClass in range(10):
        theta = np.load("Multiclass_weights6k 10I/class" + str(numClass) + "weights.npy")
        predictions[numClass] = LogisticRegression.predict(np_im, theta)
    print(predictions)
    print("Prediction:" + str(np.where(predictions == max(predictions))[0]))

def multiclass_predict_optim(image_loc, weights_loc):
    np_im = convert_image(image_loc)
    predictions = np.zeros([10, 1])
    theta = np.load(weights_loc)
    print("Theta shape: " + str(theta.shape))
    print("Image shape: " + str(np_im.shape))
    predictions = LogisticRegression.predict(np_im, theta)
    print(predictions)
    largest = 0
    for i in range(predictions.shape[1]):
        if predictions[0, i] > predictions[0, largest]:
            largest = i
    print("Predicted value:" + str(largest))
    return largest

def convert_image(image_loc):
    image = Image.open(image_loc).convert('L')
    np_im = np.array(image)
    np_im = np.reshape(np_im, (1, np_im.shape[1] * np_im.shape[1]))
    np_im = mean_std(np_im)
    return np_im


#!/usr/bin/env python
"""Common fixtures for functional serving tests"""
import pytest
from falcon import testing

from orion.serving.webapi import WebApi
from orion.testing import OrionState

@pytest.fixture()
def ephemeral_storage():
    storage = {"type": "legacy", "database": {"type": "EphemeralDB"}}
    with OrionState(storage=storage) as cfg:
        yield cfg

@pytest.fixture()
def client(ephemeral_storage):
    """Mock the falcon.API instance for testing with an in memory database"""
    yield testing.TestClient(WebApi(ephemeral_storage.storage, {}))

@pytest.fixture()
def client_with_frontends_uri(ephemeral_storage):
    """Mock the falcon.API instance for testing with custom frontend_uri"""
    yield testing.TestClient(
        WebApi(
            ephemeral_storage.storage,
            {
                "storage": ephemeral_storage.storage,
                "frontends_uri": ["http://123.456", "http://example.com"],
            },
        )
    )


import numpy as np
import scipy.integrate as intgr
import matplotlib.pyplot as plt

heaviside = lambda x: 0.5 * (np.sign(x) + 1)
gap_function = lambda a, l: lambda x: heaviside(x-a) * heaviside(a + l - x)

normal_concetrated_force_gen = lambda N, x0 = 0: lambda x: (-1) * N * heaviside(x - x0)

disturbed_force_gen = lambda f, l, x0 = 0: lambda x: f(x - x0) * gap_function(x0, l)(x)
normal_disturbed_force_gen = lambda f: lambda x:(-1) * intgr.quad(f, 0, x)[0] #use with "disturbed_force_gen function"
overall_disturbed_force = lambda f, l: intgr.quad(lambda x: intgr, 0, l)[0] #use with original d.force function

enlongation_gen = lambda N, E, A: lambda x: intgr.quad(lambda x: N(x)/E(x)/A(x), 0, x)[0]
tense_gen = lambda N, A: lambda x: N(x)/A(x)
#_gen functions used to produce final functions

#use if defined allowed enlongation
reaction_force_left= lambda w_max, length, E, A: (-1) * w_max * intgr.quad(lambda x: w_max*E*A(x)/x)[0]

def function_addition(a, b):
    return lambda x: a(x) + b(x)

def width_function_gen(A, L):
    #a_f = list()
    a_f = list([lambda x: A[0]*gap_function(0, L[0])(x)])
    a_f_gen = lambda j: lambda x: a_f[j-1](x) + A[j]*gap_function(L[j-1], L[j] - L[j - 1])(x)
    for i in range(1, len(A)):
        a_f.append(a_f_gen(i))
    return a_f[len(A) - 1]

def diagram(f, l,r, name = '', d = -1):
    if d == -1:
        d = 1000
    x = np.arange(l, r, abs(float(l - r))/d)
    y = map(f, x)
    ymx = max(y)
    ymn = min(y)
    #Настроим сетку
    plt.rc('text', usetex=False)
    plt.plot(x, y, color='green')
    plt.axhline(xmax = r)
    plt.axvline(ymax = ymx)
    plt.grid()
    plt.axis([l, r, ymn, ymx*1.5],'tight')
    #Создадим название графика
    plt.title(name)
    plt.show()

def diag_list(f, l, r, d = 1000):
    '''Функция строит график массива функций f в промежутке от l до r c количеством разбиений равным d'''
    x_ar = np.arange(l, r, abs(float(l - r))/d)
    y_ar = map(lambda a: map(a, x_ar), f)

    for i in range(len(y_ar)):
        y_t = y_ar[i]
        ymx = max(y_t)
        ymn = min(y_t)
        d = ymx - ymn
        plt.axhline(xmax = r, color = 'black')
        plt.axvline(ymax = ymx, color = 'black')
        plt.grid()
        plt.subplot(len(y_ar), 1, i+1)
        plt.plot(x_ar, y_t)
        plt.axis([l, r, ymn - d*0.01, ymx + d*0.1])

    plt.show()

def arr_red(iterble):
    cpr = list(iterble)
    for i in range(len(iterble)):
        iterble[i] = reduce(lambda a, b: a + b, cpr[:i + 1])


import os
import sys
from django.core.exceptions import ImproperlyConfigured
import yaml
from contracts import contract

@contract
def get_env_variable(var_name, required=True, default=None, as_yaml=True, warning=None):
    """Get the environment variable, process and return, or raise exception.

    :type var_name: string
    :type required: bool
    :type default: string|bool|None|int
    :type as_yaml: bool
    :type warning: string|None

    :rtype: string|bool|seq|int|None

    """

    if default is not None:
        required = False

    try:
        answer = os.environ[var_name]
        if as_yaml:
            answer = yaml.safe_load(answer)
        return answer

    except KeyError:
        if warning:
            sys.stdout.write("WARNING: {} not present in the environment\n".format(var_name))
            sys.stdout.write(warning + "\n")

        if required:
            raise ImproperlyConfigured("{} is required".format(var_name))
        else:
            return default


import datetime
from subredditsCall import datastore
from subredditsCall import reddit

def timer(time, id):
    hour = time/3600
    found_time = False

    while(not(found_time)):
        now = datetime.datetime.now()
        todays_hour = now.replace(hour=time, minute=0, second=0)
        almost_hour = now.replace(hour=time,minute=0,second=1)
        if(now == todays_hour) or (now == almost_hour):
            f = datastore.returnData(id)[1]
            return reddit.pick_sub(f)


from typing import List

from fastapi import APIRouter, Depends

from app.core.security import get_request_user_id, is_consumer
from app.models.comment import CommentCreate, CommentRead
from app.services.comment import CommentService

router = APIRouter(tags=['comment'])

@router.get("",
            response_model=List[CommentRead]
            )
async def get_story_comments(
        story_id: int,
        comment_service: CommentService = Depends(CommentService)
):
    """
    Given a research story id, returns a list of all comments under that story:
    """
    return comment_service.get_story_comments(story_id)

@router.post("",
             response_model=CommentRead,
             dependencies=[Depends(is_consumer)]
             )
async def add_comment(
        comment: CommentCreate,
        comment_service: CommentService = Depends(CommentService),
        current_user_id: int = Depends(get_request_user_id)
):
    """
    Posts a comment under a research and if successful the id of the new comment
    """
    return comment_service.add_comment(comment, current_user_id)

@router.get("/like",
            response_model=bool,
            dependencies=[Depends(is_consumer)]
            )
async def get_comment_like(
        comment_id: int,
        comment_service: CommentService = Depends(CommentService),
        current_user_id: int = Depends(get_request_user_id)
):
    """
    Returns true/false on whether the current user has liked a comment
    """
    return comment_service.get_comment_like(comment_id, current_user_id)

@router.get("/likes",
            response_model=int
            )
async def get_num_likes(
        comment_id: int,
        comment_service: CommentService = Depends(CommentService),
):
    """
    Returns the number of likes on a comment
    """
    return comment_service.get_num_likes(comment_id)

@router.put("/like",
            response_model=None,
            dependencies=[Depends(is_consumer)]
            )
async def set_comment_like(
        comment_id: int,
        liked: bool,
        comment_service: CommentService = Depends(CommentService),
        current_user_id: int = Depends(get_request_user_id)
):
    """
    Sets a comment to either liked (true) or not liked (false) by the current user
    """
    comment_service.set_comment_like(current_user_id, comment_id, liked)


import requests
from bs4 import BeautifulSoup
import csv

# Define the base URL and headers
base_url = "https://www.amazon.in/s?k=bags&crid=2M096C61O4MLT&qid=1653308124&sprefix=ba%2Caps%2C283&ref=sr_pg_1"
headers = {
    "User-Agent": "use your user agent to bypass"
}

# Part 1: Scraping Product Listings
with open("amazon_products.csv", "w", newline="", encoding="utf-8") as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow(["Product URL", "Product Name", "Product Price", "Rating", "Number of Reviews"])

    for page_number in range(1, 21):  # Scraping 20 pages
        params = {
            "k": "bags",
            "crid": "2M096C61O4MLT",
            "qid": "1653308124",
            "sprefix": "ba,aps,283",
            "ref": f"sr_pg_{page_number}",
        }
        response = requests.get(base_url, headers=headers, params=params)

        soup = BeautifulSoup(response.content, "html.parser")
        products = soup.find_all("div", class_="a-link-normal s-underline-text s-underline-link-text s-link-style a-text-normal")

        for product in products:
            product_url = product.find("a", class_="a-link-normal").get("href")
            product_name = product.find("span", class_="a-text-normal").text
            product_price = product.find("span", class_="a-price-whole").text
            rating = product.find("span", class_="a-icon-alt").text
            num_reviews = product.find("span", class_="a-size-base").text.split()[0]

            csv_writer.writerow([product_url, product_name, product_price, rating, num_reviews])

print(response.content)

# Part 2: Scraping Additional Product Information
product_urls = []
with open("amazon_products.csv", "r", encoding="utf-8") as csv_file:
    csv_reader = csv.reader(csv_file)
    next(csv_reader)  # Skip the header row
    for row in csv_reader:
        product_urls.append(row[0])  # Product URL is in the first column

with open("amazon_products_additional.csv", "w", newline="", encoding="utf-8") as csv_file_additional:
    csv_writer_additional = csv.writer(csv_file_additional)
    csv_writer_additional.writerow(["Product URL", "Description", "ASIN", "Product Description", "Manufacturer"])

    for product_url in product_urls:
        response = requests.get(product_url, headers=headers)

        soup = BeautifulSoup(response.content, "html.parser")

        # Extract information from the product page and add to the CSV
        description = soup.find("meta", {"name": "description"}).get("content", "")
        asin = soup.find("th", text="ASIN").find_next_sibling("td").text.strip()
        product_desc = soup.find("div", {"id": "productDescription"}).text.strip()
        manufacturer = soup.find("a", {"id": "bylineInfo"}).text.strip()

        csv_writer_additional.writerow([product_url, description, asin, product_desc, manufacturer])


"""
Classe qui ajoute de nouvelles fonctionnalitées et qui hérite de la classe Quoridor
"""

import turtle as tl
import quoridor as qr

class QuoridorX(qr.Quoridor):
    """
    Classe qui permet d'afficher l'interface graphique du jeu.
    """
    def __init__(self, joueurs, murs=None):
        """
        Initialise la classe à l'aide de la classe-parent.
        """
        super().__init__(joueurs, murs=None)

    def afficher(self):
        """
        méthode qui affiche le jeu
        """
        posj1 = self.posj1
        posj2 = self.posj2
        fond = tl.Turtle()
        fen = tl.Screen()
        fen.title("Quoridor.py")
        fen.setup(width=800, height=800)
        fond.pencolor('black')
        fen.delay(0)
        fond.ht()
        fond.fillcolor('midnight blue')
        fond.penup()
        fond.setpos(-325, -325)
        fond.pendown()
        fond.begin_fill()
        fond.forward(650)
        fond.left(90)
        fond.forward(650)
        fond.left(90)
        fond.forward(650)
        fond.left(90)
        fond.forward(650)
        fond.end_fill()
        car = tl.Turtle()
        car.pencolor('black')
        car.pensize(2)
        car.ht()
        car.fillcolor('dark orange')
        for x in range(9):
            for i in range(9):
                car.penup()
                car.setpos(-325+((i*60)+(i+1)*11), -325+((x*60)+(x+1)*11))
                car.pendown()
                car.begin_fill()
                car.forward(60)
                car.left(90), car.forward(60)
                car.left(90), car.forward(60)
                car.left(90), car.forward(60)
                car.left(90)
                car.end_fill()
        j1 = tl.Turtle()
        j1.shape("circle")
        j1.shapesize(2)
        j1.color('red')
        j1.penup()
        j1.setpos(-325+(((posj1[0]-1)*60)+((posj1[0]-1)+1)*11)+30,\
            -300+(((posj1[1]-1)*60)+((posj1[1]-1)+1)*11)+5)
        j2 = tl.Turtle()
        j2.shape('circle')
        j2.shapesize(2)
        j2.color('green')
        j2.penup()
        j2.setpos(-325+(((posj2[0]-1)*60)+((posj2[0]-1)+1)*11)+30,\
            -300+(((posj2[1]-1)*60)+((posj2[1]-1)+1)*11)+5)
        mh = tl.Turtle()
        mh.ht()
        mh.pencolor('black')
        mh.fillcolor('white')
        for i in self.état['murs']['horizontaux']:
            mh.penup()
            mh.setpos(-325+(((i[0]-1)*60)+((i[0]-1)+1)*11), -325+(((i[1]-1)*60)+((i[1]-1)+1)*11)-8)
            mh.pendown()
            mh.begin_fill()
            mh.forward(131)
            mh.left(90), mh.forward(5)
            mh.left(90), mh.forward(131)
            mh.left(90), mh.forward(5)
            mh.left(90)
            mh.end_fill()
        mv = tl.Turtle()
        mv.ht()
        mv.pencolor('black')
        mv.fillcolor('white')
        for v in self.état['murs']['verticaux']:
            mv.penup()
            mv.setpos(-325+(((v[0]-1)*60)+((v[0]-1)+1)*11)-8, -325+(((v[1]-1)*60)+((v[1]-1)+1)*11))
            mv.pendown()
            mv.begin_fill()
            mv.forward(5)
            mv.left(90), mv.forward(131)
            mv.left(90), mv.forward(5)
            mv.left(90), mv.forward(131)
            mv.left(90)
            mv.end_fill()
        mh._update()
        mv._update()
        fond._update()
        car._update()
        j1._update()
        j2._update()


import scrapy
from time import time
import random
from hashlib import md5

class YoudaoSpider(scrapy.Spider):
    name = 'youdao'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule',]
    def start_requests(self):
        # 获取查询单词
        word = input('请输入你要查询的单词:')
        # 获取当前时间戳
        lts = str(int(time() * 1000))
        # 获取salt
        salt = lts + str(random.randint(0, 9))
        # 获取sign
        sign = "fanyideskweb" + word + salt + "Y2FYu%TNSbMCxc3t2u^XT"
        md = md5()  # 实例化——得到MD5加密对象
        md.update(sign.encode())  # 生成加密数据
        sign = md.hexdigest()
        post_data = {
            'i': word,
            'from': 'AUTO',
            'to': 'AUTO',
            'smartresult': 'dict',
            'client': 'fanyideskweb',
            'salt': salt,
            'sign': sign,
            'lts': lts,
            'bv': '679a714475741baa9769e4725c532bb7',
            'doctype': 'json',
            'version': '2.1',
            'keyfrom': 'fanyi.web',
            'action': 'FY_BY_REALTlME',
        }
        # 将cookie转成字典类型
        cookies = 'OUTFOX_SEARCH_USER_ID=1370777476@2409:8920:400:46ac:a171:61b:7868:1c32; OUTFOX_SEARCH_USER_ID_NCOO=1092215250.8540661; UM_distinctid=179d34a1f154d-057d5ca47388f3-6701b35-144000-179d34a1f161e3; JSESSIONID=aaap-S9l5WclRfOMVkgQx; ___rl__test__cookies='+lts
        cookies = {i.split('=')[0]: i.split('=')[1] for i in cookies.split('; ')}
        print(post_data)
        # 手动发送post请求
        yield scrapy.FormRequest(url = self.start_urls[0],
                                 headers={
            'Host': 'fanyi.youdao.com',
            'Origin': 'https://fanyi.youdao.com',
            'Referer': 'https://fanyi.youdao.com/',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
        },
                                 formdata=post_data,
                                 callback=self.parse,
                                 cookies = cookies)
    def parse(self, response):
        print(response,response.text)
        #print(response,response.request.headers,response.request.method,response.text)


from CuentaBancaria import CuentaBancaria

cuentaA = CuentaBancaria(0.15, 30000)
cuentaB = CuentaBancaria(0.05, 1000)

cuentaA.deposito(5000).deposito(3000).deposito(100000).retiro(40000).generar_interes().mostar_info_cuenta()

cuentaB.deposito(87000).deposito(60000).retiro(10000).retiro(20000).retiro(15000).retiro(50000).generar_interes().mostar_info_cuenta()

CuentaBancaria.cuenta()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jun  1 15:24:11 2018

@author: max
Attention this script alters your data:
It will either drop all nan and 0 values from your data (nandrop=true),
or replaces them by 0 and tries to fill single timepoint 0 values by the
average of the two adjacent values

"""

import pandas as pd
import numpy as np
import scipy.stats as ss
import matplotlib.pyplot as plt
import os
import glob
import re
import seaborn as sns
sns.set(color_codes=True)
from statsmodels.stats.multicomp import pairwise_tukeyhsd
plt.ioff()
#%% inputs
identifier='Farred_nuclei'
Si_Well={'B2':'ARGHEF1', 'B3':'DOCK10', 'B4': 'FGD2', 'B5':'Ctrl'}
timeinterval=15
#set to true if you want all NaNs and 0s dropped
nandrop=False
#set true if you want to use the robust version of z score calculation
robust=False

#%% loading and formatting data
length_l=[]
branch_l=[]
neurites_l=[]
wellnames=[]
#sns.set(font_scale=3) #enable for figureprinting
#plt.figure(figsize=(20,15)) #enable for figureprinting
#for filepath in glob.glob('/home/mheydasch/myimaging/N1E115/SiRNA/SiRNA_11/4days/adjusted/analyzed/csv/*{}'.format(identifier)): #enable on server
#defining the patterns of the well and fov descriptors
wellpattern=re.compile('_[A-Z][0-9]_')
fovpattern=re.compile('[0-9]{4}_' + identifier)
for filepath in glob.glob('/Users/max/Desktop/Office/Phd/Data/N1E_115/SiRNA/SiRNA_21/csv/*{}'.format(identifier)):

    file = os.path.join(filepath, 'TotalNeuritesLength.csv')
    filename = vars()['filepath'].split('/')[-1]
    try:
        temp=pd.read_csv(file)
        rows, columns = temp.shape
        timer=0
        temp=temp.dropna(axis=0, how='all', subset=[x for x in list(temp) if x.startswith('cell ')])
        for x in range(len(temp)):
            temp.at[x, 'time']=timer
            timer+=15 #Adjust for respective time interval
        well=re.search(wellpattern, file).group().strip('_')
        temp['well']=well
        fov = re.search(fovpattern, file).group().strip('_' + identifier)
        temp['fov']=fov
        temp=temp.drop(['frame'], axis=1)
        length_l.append(temp) #appends current dataframe to list length_l
    except:
        print('{} could not be found'.format(file))
        next
    #totalneuritesbranches
    branchfile = os.path.join(filepath, 'TotalNeuritesBranches.csv')
    try:
        temp=pd.read_csv(branchfile)
        rows, columns = temp.shape
        timer=0
        temp=temp.dropna(axis=0, how='all', subset=[x for x in list(temp) if x.startswith('cell ')])
        for x in range(len(temp)):
            temp.at[x, 'time']=timer
            timer+=timeinterval #Adjust for respective time interval
        well=re.search(wellpattern, file).group().strip('_')
        temp['well']=well
        pattern=re.compile('[0-9]{4}_' + identifier)
        fov = re.search(fovpattern, file).group().strip('_' + identifier)
        temp['fov']=fov
        temp=temp.drop(['frame'], axis=1)
        branch_l.append(temp)
    except:
        print('{} could not be found'.format(branchfile))
        next
    #NumberOfNeurites
    neuritesfile = os.path.join(filepath, 'NumberOfNeurites.csv')
    try:
        temp=pd.read_csv(neuritesfile)
        rows, columns = temp.shape
        timer=0
        temp=temp.dropna(axis=0, how='all', subset=[x for x in list(temp) if x.startswith('cell ')])
        for x in range(len(temp)):
            temp.at[x, 'time']=timer
            timer+=15 #Adjust for respective time interval
        well=re.search(wellpattern, file).group().strip('_')
        temp['well']=well
        pattern=re.compile('[0-9]{4}_' + identifier)
        fov = re.search(fovpattern, file).group().strip('_' + identifier)
        temp['fov']=fov
        temp=temp.drop(['frame'], axis=1)
        neurites_l.append(temp)
    except:
        print('{} could not be found'.format(neuritesfile))
        next
#total length
allwells = pd.concat(length_l)
allwells= pd.melt(allwells, id_vars=['well', 'fov', 'time'])
allwells['unique']=allwells['well']+allwells['fov']+allwells['variable']

#total branches
allbranch = pd.concat(branch_l)
allbranch= pd.melt(allbranch, id_vars=['well', 'fov', 'time'])
allbranch['unique']=allbranch['well']+allbranch['fov']+allbranch['variable']

#number of neurites
allneurites = pd.concat(neurites_l)
allneurites= pd.melt(allneurites, id_vars=['well', 'fov', 'time'])
allneurites['unique']=allneurites['well']+allneurites['fov']+allneurites['variable']

#allwells=allwells.dropna()
#allwells['value']=np.log(allwells.value)
#well_mask=allwells['well']=='A3' #creating boolean mask
#timepoint_mask=(allwells['time']==465)

normality=[]

#%% selecting data and checking for normal distribution per well
wellnames=allwells['well'].unique() #extracting unique well names
length_l=[]
branch_l=[]
neurites_l=[]

df=pd.DataFrame({'well':[], 'n':[],'mean':[], 'STDV':[], 'normal':[],'Test':[], 'F_value':[], 'P_value':[]},)
rowindex=0
#sns.set(font_scale=3) #enable for figureprinting
#plt.figure(figsize=(1, 1)) #enable for figureprinting

#%% Decide: this one drops all NAs and all 0 values
if nandrop == True:
    for i in wellnames:
        time_wellmask=(allwells['well']==i)  #creating bolean mask
        selection =allwells[time_wellmask] #applying bolean mask to select the appropriate wells
        cleansed=selection.dropna() #removing all lines where no value could be obtained, this could probably already be implemented at the "allwells" level
        cleansed=cleansed[cleansed.value !=0] #activate if you want to ignore zero values
        #graph=sns.distplot(cleansed['value'], hist=True, label=i)
        #data selection for branches
        time_branchmask=(allbranch['well']==i)  #creating bolean mask
        selection_branch =allbranch[time_branchmask] #applying bolean mask to select the appropriate wells
        cleansed_branch=selection_branch.dropna() #removing all lines where no value could be obtained, this could probably already be implemented at the "allwells" level
        cleansed_branch=cleansed_branch[cleansed_branch.value !=0]
        #data selection for neurites
        time_neuritemask=(allneurites['well']==i)  #creating bolean mask
        selection_neurites =allneurites[time_neuritemask] #applying bolean mask to select the appropriate wells
        cleansed_neurites=selection_neurites.dropna() #removing all lines where no value could be obtained, this could probably already be implemented at the "allwells" level
        cleansed_neurites=cleansed_neurites[cleansed_neurites.value !=0]

#%% Decide: This one will replace all Nas by 0
if nandrop == False:
    for i in wellnames:
        time_wellmask=(allwells['well']==i)  #creating bolean mask
        selection =allwells[time_wellmask] #applying bolean mask to select the appropriate wells
        cleansed=selection.fillna(0) #filling all NA values
        #graph=sns.distplot(cleansed['value'], hist=True, label=i)
        #data selection for branches
        time_branchmask=(allbranch['well']==i)  #creating bolean mask
        selection_branch =allbranch[time_branchmask] #applying bolean mask to select the appropriate wells
        cleansed_branch=selection_branch.fillna(0) #removing all lines where no value could be obtained, this could probably already be implemented at the "allwells" level
        #data selection for neurites
        time_neuritemask=(allneurites['well']==i)  #creating bolean mask
        selection_neurites =allneurites[time_neuritemask] #applying bolean mask to select the appropriate wells
        cleansed_neurites=selection_neurites.fillna(0) #removing all lines where no value could be obtained, this could probably already be implemented at the "allwells" level

    #%% test for normality
        k2, p = ss.normaltest(cleansed['value'])
    #%% #writing results of test to dataframe
        print('p =',p)
        df.loc[rowindex, 'well']=i
        df.loc[rowindex, 'n']=len(cleansed)
        df.loc[rowindex, 'mean']=cleansed['value'].mean()
        df.loc[rowindex, 'STDV']=cleansed['value'].std()
        print('{} has {} observations'.format(i, len(cleansed)))
        print('The mean distance traveled of {} is {}'.format(i, cleansed['value'].mean()))
        print('Standard deviation of traveled distance of {} is {}'.format(i, cleansed['value'].std()))

        if p < 0.05 :
            print('The data of well {} is not normally distributed'.format(i))
            df.loc[len(df)-1, 'normal']='No'
            norm=0

        else:
            print('The data of well {} is normally distributed'.format(i))
            df.loc[len(df)-1, 'normal']='Normal'
            norm=1
        length_l.append(cleansed) #appending the data of the current well to a list
        neurites_l.append(cleansed_neurites)
        branch_l.append(cleansed_branch)
        rowindex+=1
        normality.append(norm) #create a list of normality for each condition 1=true 0=false
relevant_data=pd.concat(length_l) #creating a dataframe from the list
relevant_neurites=pd.concat(neurites_l)
relevant_branch=pd.concat(branch_l)
relevant_data.rename(index=str, columns={'value' :'length'}, inplace=True)
relevant_neurites.rename(index=str, columns={'value' :'n_neurites'}, inplace=True)
relevant_branch.rename(index=str, columns={'value' :'n_branch'}, inplace=True)
wellnames= relevant_data['well'].unique() #reassigning wellnames incase one disappeared after cleansing
#%% merging data frames for n_neurites, n_branch and length
merged=relevant_data.merge(relevant_neurites[['unique', 'n_neurites', 'time']], on=['unique', 'time'])
merged=merged.merge(relevant_branch[['unique', 'n_branch', 'time']], on=['unique', 'time'])
#%%correcting for minor segmentation errors
#sets the parameters
parameters=['length', 'n_neurites', 'n_branch']
#loops through each unique cell
for cell in merged.groupby('unique'):
    indices=[]
#for each parameter, for each row, if the value is 0 it will be replaced by the average of it's two
#adjacent values
    for measurement in parameters:
       #for each element from the 0st to the last in the length of the group
        for i in range(0, len(cell[1])):
            if i < (len(cell[1])-1):
                i_plus=i+1
            else:
                i_plus=i
            if i >0:
                i_minus=i-1
            else:
                i_minus=i
                #if the current value of the measurement is 0, but the two adjacent ones are not
            if cell[1][measurement][cell[1][measurement].index[i]]==0 and cell[1][measurement][cell[1][measurement].index[i_minus]]!=0 and cell[1][measurement][cell[1][measurement].index[i_plus]]!=0:
                new_value=(cell[1][measurement][cell[1][measurement].index[i_minus]]+cell[1][measurement][cell[1][measurement].index[i_plus]])/2
                #the current value in the dataframe (not the grouped object) will be replaced
                #by the mean of the two adjacent values
                merged.loc[[cell[1][measurement].index[i]],[measurement]]=new_value

#%%creating new measurements based on the three loaded measurements
merged['length/complexity']=merged['length']/merged['n_branch']
merged['length/neurites']=merged['length']/merged['n_neurites']
merged['corrected_length']=merged['length/complexity']/merged['n_neurites']
merged['well']=merged['well'].map(Si_Well)
merged['branch/length']=merged['n_branch']/merged['length']
#replaces infinite numbers by nans
merged=merged.replace([np.inf, -np.inf], np.nan)
#replaces nans by 0
merged=merged.fillna(0)
wellnames=merged['well'].unique()

#%% ANOVA test uncomment if needed
# =============================================================================
# threshold=sum(normality)/len(normality) #relative normality
# samples = [condition[1] for condition in merged.groupby('well')['length']]
# if  threshold >= 1: #check if data is normal to decide for a test
#  f_val, p_val = ss.f_oneway(*samples)
#  df.loc[:, 'Test']= 'ANOVA'
#
# else:
#     f_val, p_val = ss.kruskal(*samples)
#     df.loc[:, 'Test']= 'Kruskal'
# print('F value: {:.3f}, p value: {:.3f}'.format(f_val, p_val))
# print('F value: {:.3f}, p value: {:.3f}'.format(f_val, p_val))
# df.loc[1, 'F_value']= f_val
# df.loc[1, 'P_value']= p_val
# =============================================================================
#%% Tukey test
# =============================================================================
# for name, grouped_df in relevant_data.groupby('well'):
#     print('Well {}'.format(name), pairwise_tukeyhsd(merged['length'], merged['well']))
# =============================================================================
#%% creating a normalized dataset to compare data from multiple analysis
averaged=pd.DataFrame({'time': [], 'well':[], 'length':[], 'n_neurites' :[], 'n_branch':[], 'length/complexity':[], 'length/neurites':[], 'corrected_length': []})
grouped_df=merged.groupby(['time', 'well'])

#%% z-score calculation
outputpattern=re.compile('SiRNA_[0-9]+')
outputidentifier=vars()['filepath'].split('/')
outputidentifier=list(filter(outputpattern.search, outputidentifier))
#%%
#either computes the zscore using the MAD (robust==True), or the standard deviation(robust==False)
def MAD_robust(x):
    if robust==True:
        med=np.median(x)
        dif=[np.abs(i-med) for i in x]
        return np.median(dif)
    else:
        return np.std(x)
def Mean_robust(x):
    if robust==True:
        return np.median(x)
    else:
        return np.mean(x)

#creating the number of observations(cells) per condition
cell_observations={}
for group in merged.groupby(['time','well']):
    temp={group[1]['well'].unique()[0]:len(group[1]['unique'].unique())}
    cell_observations.update(temp)

#groups merged by well and time, and calculates the median for each parameter
mean=merged.groupby(['well', 'time'], as_index=False).agg({'length': [MAD_robust, Mean_robust], 'n_neurites':[MAD_robust, Mean_robust],'n_branch':[MAD_robust, Mean_robust],'length/complexity':[MAD_robust, Mean_robust], 'length/neurites':[MAD_robust, Mean_robust], 'corrected_length':[MAD_robust, Mean_robust], 'branch/length': [MAD_robust, Mean_robust]})
#groups merged together by time, without the control and calculates the MAD and median for each parameter. All conditions combined
CTRL_MAD=merged[merged['well']=='Ctrl'].groupby(('time'), as_index=False).agg({'length': [MAD_robust, Mean_robust], 'n_neurites':[MAD_robust, Mean_robust],'n_branch':[MAD_robust, Mean_robust],'length/complexity':[MAD_robust, Mean_robust], 'length/neurites':[MAD_robust, Mean_robust], 'corrected_length':[MAD_robust, Mean_robust], 'branch/length': [MAD_robust, Mean_robust] })

#creates more reasonable names for the columns
mean.columns = ["_".join(x) for x in mean.columns.ravel()]
CTRL_MAD.columns = ["_".join(x) for x in CTRL_MAD.columns.ravel()]
#removes 'Ctrl' from the wellnames
wellnames_2=wellnames[wellnames!='Ctrl']
#initiates a list to store the zscore dataframes
zscore=[]
#creates a list of all parameters
parameters=list(merged.columns.values)
#creates a dictionary of unwanted parameters
unwanted={'well', 'time', 'fov', 'variable', 'unique'}
#creates parameters as everythin that is not in the unwanted dictionary
parameters=[e for e in parameters if e not in unwanted]
#initates an empty list for stripped parameters
stripped_parameters=[]
#to be able to use the parameters from the list for both, mean and MAD we need to strip
#'_mean' from them
# =============================================================================
# for i in parameters:
#     if i.endswith('Mean_robust'):
#         temp=i[:-len('Mean_robust')]
#         stripped_parameters.append(temp)
# =============================================================================
unique_ids=[]
for cell_id in merged['unique'].unique():
    cell_ids=cell_id
    unique_ids.append(cell_ids)
number_of_cells=len(unique_ids)
loop_iteration=1
#%%
#groups dataframe by time and loops through all timepoints
for timepoint in merged.groupby(['time'], as_index=False):
    loop_iteration=1
    print('computing timepoint ', timepoint[0])
#loops through the individual cells
    for cell_id in unique_ids:
         well=merged['well'][merged['unique']==cell_id].values[0]
         loop_iteration+=1
         print('computing timepoint ', timepoint[0])
         print('computing cell ', loop_iteration, 'of ', number_of_cells)
#loops through the parameters
         for parameter in parameters:
             #creates a dataframe with the wellname, the timepoint timepoint[0], the unique_cell_ID
             # the experiment name,and the zscore for the values
             #timepoint[1] where well==1 and parameter equals item
             #MAD of control
             #calculates the zscore by using the value from the test condition (dataframe mean) and subtracting
             #from that the mean of the control. this is then divided by the standard deviation of the control
             temp=pd.DataFrame([[well, timepoint[0], parameter,
                                 (timepoint[1][parameter][timepoint[1]['unique']==cell_id].values[0]
     -CTRL_MAD[parameter+'_Mean_robust'][CTRL_MAD['time_']==timepoint[0]].values[0])/(
        CTRL_MAD[CTRL_MAD['time_']==timepoint[0]][parameter+'_MAD_robust'].values[0]), outputidentifier[0], outputidentifier[0]+'_'+cell_id ]],
        columns=['well', 'time', 'variable', 'value', 'experiment', 'cell_id' ])

             zscore.append(temp)
print('concatonating zscore df')
zscore1=pd.concat(zscore, axis=0, join='outer', ignore_index=True)
print('Figureprintin...')

#%% boxplot length
#plt.ylim(0, 1000)
# =============================================================================
# median=merged.groupby('well')['length'].median().values
# sns.boxplot(x=merged['well'], y=merged['length'])
# ax= sns.stripplot(x=merged['well'], y=merged['length'], color='orange', jitter=0.2, size=2.5)
# sns.set_style('ticks') #removes background and adds ticks
# sns.despine() #removes top and right
# counts=x=merged['well'].value_counts()
# nobs = [str(x) for x in counts.tolist()]
# nobs = ["n: " + i for i in nobs]
# pos = range(len(nobs)) # add number of observations (currently added to wrong box)
# for tick,label in zip(pos,ax.get_xticklabels()):
#     ax.text(pos[tick], median[tick] + median[tick]*0.2, nobs[tick],
#     horizontalalignment='center',  color='black', weight='semibold')
#
# fig1=ax.get_figure()
# fig1.set_size_inches(12, 8)
# plt.show()
# plt.close()
# =============================================================================
#%%boxplot length/complexity
#plt.ylim(0, 40)
# =============================================================================
# median=merged.groupby('well')['length/complexity'].median().values
# sns.boxplot(x=merged['well'], y=merged['length/complexity'])
# ax= sns.stripplot(x=merged['well'], y=merged['length/complexity'], color='orange', jitter=0.2, size=2.5)
# sns.set_style('ticks') #removes background and adds ticks
# sns.despine() #removes top and right
# counts=x=merged['well'].value_counts()
# nobs = [str(x) for x in counts.tolist()]
# nobs = ["n: " + i for i in nobs]
# pos = range(len(nobs)) # add number of observations (currently added to wrong box)
# for tick,label in zip(pos,ax.get_xticklabels()):
#     ax.text(pos[tick], median[tick] + median[tick]*0.2, nobs[tick],
#     horizontalalignment='center',  color='black', weight='semibold')
#
# fig2=ax.get_figure()
# fig2.set_size_inches(12, 8)
# plt.show()
# plt.close()
# =============================================================================
#%%boxplot corrected_length
# =============================================================================
# plt.ylim(0, 10)
# median=merged.groupby('well')['corrected_length'].median().values
# sns.boxplot(x=merged['well'], y=merged['corrected_length'])
# ax= sns.stripplot(x=merged['well'], y=merged['corrected_length'], color='orange', jitter=0.2, size=2.5)
# sns.set_style('ticks') #removes background and adds ticks
# sns.despine() #removes top and right
# counts=x=merged['well'].value_counts()
# nobs = [str(x) for x in counts.tolist()]
# nobs = ["n: " + i for i in nobs]
# pos = range(len(nobs)) # add number of observations (currently added to wrong box)
# for tick,label in zip(pos,ax.get_xticklabels()):
#     ax.text(pos[tick], median[tick] + median[tick]*0.2, nobs[tick],
#     horizontalalignment='center',  color='black', weight='semibold')
#
# fig3=ax.get_figure()
# fig3.set_size_inches(12, 8)
# plt.show()
# =============================================================================
#%%boxplot length/neurite
# =============================================================================
# plt.ylim(0, 2000)
# median=merged.groupby('well')['length/neurites'].median().values
# sns.boxplot(x=merged['well'], y=merged['length/neurites'])
# ax= sns.stripplot(x=merged['well'], y=merged['length/neurites'], color='orange', jitter=0.2, size=2.5)
# sns.set_style('ticks') #removes background and adds ticks
# sns.despine() #removes top and right
# counts=x=merged['well'].value_counts()
# nobs = [str(x) for x in counts.tolist()]
# nobs = ["n: " + i for i in nobs]
# pos = range(len(nobs)) # add number of observations (currently added to wrong box)
# for tick,label in zip(pos,ax.get_xticklabels()):
#     ax.text(pos[tick], median[tick] + median[tick]*0.2, nobs[tick],
#     horizontalalignment='center',  color='black', weight='semibold')
#
# fig4=ax.get_figure()
# fig4.set_size_inches(12, 8)
# plt.show()
# =============================================================================
#%%boxplot number of neurites
#plt.ylim(0, 30)
# =============================================================================
# median=merged.groupby('well')['n_neurites'].median().values
# sns.boxplot(x=merged['well'], y=merged['n_neurites'])
# ax= sns.stripplot(x=merged['well'], y=merged['n_neurites'], color='orange', jitter=0.2, size=2.5)
# sns.set_style('ticks') #removes background and adds ticks
# sns.despine() #removes top and right
# counts=x=merged['well'].value_counts()
# nobs = [str(x) for x in counts.tolist()]
# nobs = ["n: " + i for i in nobs]
# pos = range(len(nobs)) # add number of observations (currently added to wrong box)
# for tick,label in zip(pos,ax.get_xticklabels()):
#     ax.text(pos[tick], median[tick] + median[tick]*0.2, nobs[tick],
#     horizontalalignment='center',  color='black', weight='semibold')
#
# fig5=ax.get_figure()
# fig5.set_size_inches(12, 8)
# plt.show()
# =============================================================================
#%%# neurite length at t=0
#plt.ylim(0, 30)
# =============================================================================
# median=merged.loc[merged['time']==0].groupby('well')['length'].median().values
# sns.boxplot(x=merged['well'], y=merged['length'].loc[merged['time']==0])
# ax= sns.stripplot(x=merged['well'], y=merged['length'], color='orange', jitter=0.2, size=2.5)
# sns.set_style('ticks') #removes background and adds ticks
# sns.despine() #removes top and right
# counts=x=merged['well'].value_counts()
# nobs = [str(x) for x in counts.tolist()]
# nobs = ["n: " + i for i in nobs]
# pos = range(len(nobs)) # add number of observations (currently added to wrong box)
# for tick,label in zip(pos,ax.get_xticklabels()):
#     ax.text(pos[tick], median[tick] + median[tick]*0.2, nobs[tick],
#     horizontalalignment='center',  color='black', weight='semibold')
# ax.set_title('time=0')
# fig1=ax.get_figure()
# fig1.set_size_inches(12, 8)
# plt.show()
# plt.close()
# =============================================================================
#%%timeseries neurite length
ax=sns.lineplot(x='time', y='length', data=merged, hue='well')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles[1:], labels=labels[1:])
legend=ax.get_legend()
#creates an empty dictionary
neurite_observations={}
#groups the dataframe by time and well, to get from each well, for one timepoint
#the amount of neurites, adding well and neurites to the dictionary
for group in merged.groupby(['time','well']):
    temp={group[1]['well'].unique()[0]:group[1]['n_neurites'].sum()}
    neurite_observations.update(temp)
#legend.texts[0].set_text(' ')
#creates '' at the list new labels as number 1, as this is represents the legend title
new_labels=['']
#loops through all wellnames
for well in wellnames:
    count=0
    #for element in the legend, the counter will increase, and if the element contains
    #the wellname, a list will be created with the wellname +
    #the number of observations for that well
    for name in legend.texts:
        count+=1
        #string conversion is required, because the 'Text' object cannot otherwise be
        #searched
        if well in str(name):
            #the counter already increased by one before the condition, so
            #it needs to be decreased again to adress the correct element
            count-=1
            #ax.legend.texts[count].set_text('{} n: {}'.format(well, counts[well]))
            #print('legend.texts: ',legend.texts[count])
            temp=('{}; n: {}'.format(well, neurite_observations[well]))
            new_labels.append(temp)
            break
#updates the labels with the new list of labels
ax.legend(handles=handles, labels=new_labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
art6=[]
art6.append(ax.legend)
plt.show()
fig6=ax.get_figure()
plt.close()
#%%timeseries neuritelengt/number of neurites

ax=sns.lineplot(x='time', y='length/neurites', data=merged, hue='well')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles[1:], labels=labels[1:])
legend=ax.get_legend()

#legend.texts[0].set_text(' ')
new_labels=['']
for well in wellnames:
    count=0
    for name in legend.texts:
        count+=1
        if well in str(name):
            count-=1
            #ax.legend.texts[count].set_text('{} n: {}'.format(well, counts[well]))

            temp=('{}; n: {}'.format(well, cell_observations[well]))
            new_labels.append(temp)
            break
#edits the legend, uses the just created new labels list, for labels, places the legend
            #outside of the graph
ax.legend(handles=handles, labels=new_labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
#creates a list for additional artists, so the legend will be saved as well.
art7=[]
art7.append(ax.legend)
#ax.legend()
plt.show()
fig7=ax.get_figure()
plt.close()
#%% timeseries neurite lenght/ number of neurites/ number of branches
ax=sns.lineplot(x='time', y='corrected_length', data=merged, hue='well')
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles=handles[1:], labels=labels[1:])
legend=ax.get_legend()
#legend.texts[0].set_text(' ')
new_labels=['']
for well in wellnames:
    count=0
    #print('well: ', well)
    for name in legend.texts:
        #print('name: ', name)
        count+=1
        #print('count: ', count)
        if well in str(name):
            count-=1
            #ax.legend.texts[count].set_text('{} n: {}'.format(well, counts[well]))
            #print('legend.texts: ',legend.texts[count])
            #print(count)
            temp=('{}; n: {}'.format(well, cell_observations[well]))
            new_labels.append(temp)
            break

ax.legend(handles=handles, labels=new_labels, bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
art8=[]
art8.append(ax.legend)
plt.show()
fig8=ax.get_figure()

#%% Individual tracks
# =============================================================================
# plt.close()
# ax=sns.lineplot(x='time', y='length', data=merged, hue='well', size='unique')
# ax.get_legend().set_visible(False)
# ax.legend_.remove()
# Fig_it=ax.get_figure()
# Fig_it.set_size_inches(20, 20)
# plt.show()
# =============================================================================
#%% Saving files
#outputidentifier=vars()['filepath'].split('/')[-3]
print(outputidentifier[0])
path=vars()['filepath'].split('/')
print (filepath)
path=os.path.join(path[1], path[2], path[3], path[4], path[5], path[6], path[7], path[8], path[9])
path='/'+ path + '/'
print(path)

#%% data to save
#df.to_csv('{}{}_results.csv'.format(filepath, outputidentifier[0]))
#%% Boxlplots
# =============================================================================
# fig1.savefig('{}{}_neurite_length'.format(path, outputidentifier[0]))
# fig2.savefig('{}{}_neurite_length_p_complexity'.format(path, outputidentifier[0]))
# fig3.savefig('{}{}_corrected_length'.format(path, outputidentifier[0]))
# fig4.savefig('{}{}_length_p_neurite'.format(path, outputidentifier[0]))
# fig5.savefig('{}{}_neurite_number'.format(path, outputidentifier[0]))
# =============================================================================
#%% Timeseries
fig6.savefig('{}{}_length_over_time.png'.format(path, outputidentifier[0]), additional_artists=art6, bbox_inches='tight')
fig7.savefig('{}{}_length_p_neurite_over_time.png'.format(path, outputidentifier[0]), additional_artists=art7, bbox_inches='tight')
fig8.savefig('{}{}_corrected_length.png'.format(path, outputidentifier[0]), additional_artists=art8, bbox_inches='tight')
#%%
zscore1.to_csv('{}{}_z_scores.csv'.format(path, outputidentifier[0]))


import xml.dom.minidom as DOM
from xml.dom.minidom import Node
from datetime import datetime
import dateutil.parser
from pytz import timezone
import bisect

class TrackPoint:
    def __getattr__(self, key):
        if key in ['lat', 'latitude']:
            return float(self._dom_node.getAttribute('lat'))
        elif key in ['lon', 'longitude']:
            return float(self._dom_node.getAttribute('lon'))
        elif key in ['elevation', 'ele']:
            return self._get_elevation()
        elif key == 'time':
            return self._get_time()
        else:
            return super(TrackPoint, self).__getattr__(key)

    def __setattr__(self, key, value):
        if key in ['lat', 'latitude']:
            self._dom_node.setAttribute('lat', ('%0.6f' % value))
        elif key in ['lon', 'longitude']:
            self._dom_node.setAttribute('lon', ('%0.6f' % value))
        elif key in ['elevation', 'ele']:
            self._set_elevation(value)
        elif key == 'time':
            self._set_time(value)
        else:
            super(TrackPoint, self).__setattr__(key, value)

    def _ensure_tag(self, dom, tagname, default_value=''):
        candidates = self._dom_node.getElementsByTagName(tagname)
        if len(candidates) == 0:
            node = dom.createElement(tagname)
            node.appendChild(dom.createTextNode(default_value))
            self._dom_node.appendChild(node)
            return node
        else:
            node = candidates[0]
            if node.hasChildNodes():
                if node.firstChild.nodeType == Node.TEXT_NODE:
                    default_value = node.firstChild.nodeValue
                    if node.firstChild == node.lastChild:
                        return node
                while node.hasChildNodes():
                    node.removeChild(node.firstChild)
            node.appendChild(dom.createTextNode(default_value))
            return node

    def _get_elevation(self):
        str_value = self._dom_ele.firstChild.nodeValue
        if str_value == '':
            return None
        else:
            return float(str_value)
    def _set_elevation(self, elevation):
        if elevation is None:
            self._dom_ele.firstChild.nodeValue = ''
        else:
            self._dom_ele.firstChild.nodeValue = ('%0.2f' % elevation)

    def _get_time(self):
        str_value = self._dom_time.firstChild.nodeValue
        if str_value == '':
            return None
        else:
            return dateutil.parser.parse(str_value)
    def _set_time(self, dt):
        if dt is None:
            self._dom_time.firstChild.nodeValue = ''
        else:
            self._dom_time.firstChild.nodeValue = dt.isoformat()

    def get_gps_triple(self):
        return (self.lat, self.lon, self.ele)

    def apiformat(self):
        return '%0.6f,%0.6f' % (self.lat, self.lon)

    def __lt__(self, other):
        return self.time < other.time if isinstance(other, TrackPoint) else self.time < other
    def __gt__(self, other):
        return self.time > other.time if isinstance(other, TrackPoint) else self.time > other
    def __ge__(self, other):
        return self.time >= other.time if isinstance(other, TrackPoint) else self.time >= other
    def __le__(self, other):
        return self.time <= other.time if isinstance(other, TrackPoint) else self.time <= other

    def __init__(self, dom, trackpt_dom):
        self._dom_node = trackpt_dom
        self._dom_ele = self._ensure_tag(dom, 'ele')
        self._dom_time = self._ensure_tag(dom, 'time')

class TrackPoints:

    def __getitem__(self, key):
        return self._locations.__getitem__(key)
    def __len__(self):
        return len(self._locations)
    def __iter__(self):
        return self._locations.__iter__()
    def __reversed__(self):
        return self._locations.__reversed__()
    def __contains__(self, item):
        return self._locations.__contains__(item)

    def __init__(self, dom):
        self._locations = []
        for trkpt in dom.getElementsByTagName('trkpt'):
            self._locations.append(TrackPoint(dom, trkpt))

class GeotagQuery:
    def _find_le_idx(self, time):
        i = bisect.bisect_right(self._locations, time)
        return i - 1 if i else None

    def __call__(self, time):
        if time.tzinfo is None or time.tzinfo.utcoffset(time) is None:
            time = timezone('Europe/Rome').localize(time)

        if time < self._locations[0].time or time > self._locations[-1].time:
            return None
        le_idx = self._find_le_idx(time)
        if not le_idx:
            return None
        if self._locations[le_idx].time == time:
            return self._locations[le_idx].get_gps_triple()

        # interpolate linearly
        time0 = self._locations[le_idx].time
        time1 = self._locations[le_idx + 1].time
        lat0 = self._locations[le_idx].lat
        lat1 = self._locations[le_idx + 1].lat
        lon0 = self._locations[le_idx].lon
        lon1 = self._locations[le_idx + 1].lon

        alpha = (time - time0).total_seconds() / (time1 - time0).total_seconds()
        lat = (1. - alpha) * lat0 + alpha * lat1
        lon = (1. - alpha) * lon0 + alpha * lon1
        return (lat, lon, None)

    def __init__(self, track):
        self._locations = [pt for pt in track if pt.time is not None]
        self._locations.sort()


q=int(input())
a=int(input())
b=int(input())
c=int(input())

da = 35 - a
db = 100 - b
dc = 10 - c
t = 0
while q >0:
    num = min(da, db, dc)
    a += num
    b += num
    c += num
    if num == da:
        q = q - (3 * num) + 30
        a = 0
    elif num==db:
        q = q - (3 * num) + 60
        b = 0
    elif num==dc:
        q = q - (3 * num) + 9
        c = 0
    if q <= 0:
        t = t + q1
        break
    da = 35- a
    db = 100- b
    dc = 10- c
    t += num * 3
    q1 = q
print('Matha plays ' + str(t)+' times before going broke.')


#! /usr/bin/python

# ---------- IMPORTS ----------

from rtd_board import RTD_Board
import os
import time

# ---------- GLOBAL VARS ----------

CALIB_FILE = "calibration.csv"
rtd = RTD_Board(1,0x08)
rtd.initialize(32, 9, 0.0039083, -0.0000005775)

# ---------- FUNCTIONS ----------

def getChannel():
    channel = raw_input("Enter the channel to calibrate:\n")
    return int(channel)

def getUserTemp():
    value = raw_input("Enter the current calibration temperature ( q to quit ):\n")
    if value == 'q' or value == 'Q':
        exit(0)
    return float(value)

def writeTempDelta(channel, userTemp):
    if os.path.exists(CALIB_FILE) == False:
        fd = open(CALIB_FILE, mode='w')
        fd.write("Channel,SensorTemperature,UserTemperature,Difference\n")
        fd.close()
    fd = open(CALIB_FILE, mode='a')
    sensorTemp = rtd.getTemperatureC(channel)
    diff = sensorTemp - userTemp
    fd.write(str(channel)+","+str(sensorTemp)+","+str(userTemp)+","+str(diff)+"\n")
    print(str(channel)+","+str(sensorTemp)+","+str(userTemp)+","+str(diff)+"\n")
    fd.close()

# ---------- MAIN ROUTINE HERE ----------

print("Board calibration, please enter the channel you want to calibrate")
channel = getChannel()

while True:
    print("Selected channel : "+str(channel))
    temp = getUserTemp()
    writeTempDelta(channel, temp)

exit(0)


from __future__ import print_function
from __future__ import absolute_import
from proteus import *
from proteus.default_n import *
from proteus import defaults
defaults.reset_default_n()
try:
    from .stokesDrivenCavity_2d_p import *
except:
    from stokesDrivenCavity_2d_p import *

#######################################################

# context variables
nLevels = ct.nLevels
numeric_scheme = ct.numeric_scheme
useWeakBoundaryConditions = ct.useWeakBoundaryConditions
solveIteratively = ct.solveIteratively
solveInParallel = ct.solveInParallel

# TODO - this parallel flag needs work

#######################################################

#steady-state so no time integration
timeIntegration = NoIntegration
#number of output timesteps
nDTout = 1

######################################################
#            specify the numerical scheme
if numeric_scheme=="TH":
    femSpaces = {0:C0_AffineLinearOnSimplexWithNodalBasis,
                 1:C0_AffineQuadraticOnSimplexWithNodalBasis,
                 2:C0_AffineQuadraticOnSimplexWithNodalBasis}
elif numeric_scheme=="C0P1C0P1":
    femSpaces = {0:C0_AffineLinearOnSimplexWithNodalBasis,
                1:C0_AffineLinearOnSimplexWithNodalBasis,
                2:C0_AffineLinearOnSimplexWithNodalBasis}
    subgridError = StokesASGS_velocity_pressure(coefficients,nd)
elif numeric_scheme=="C0Q1C0Q1":
   femSpaces = {0:C0_AffineLinearOnCubeWithNodalBasis,
                1:C0_AffineLinearOnCubeWithNodalBasis,
                2:C0_AffineLinearOnCubeWithNodalBasis}
   subgridError = StokesASGS_velocity_pressure(coefficients,nd)
elif numeric_scheme=="THQuads":
    Q2 = LagrangeCubeFactory(2)
    femSpaces = {0:C0_AffineLinearOnCubeWithNodalBasis,
                 1:Q2,
                 2:Q2}
else:
    print('INVALID FINITE ELEMENT SELECTED')
#######################################################

######################################################
#           Mesh and Quadrature Options

if (numeric_scheme=="C0Q1C0Q1" or numeric_scheme=="THQuads"):
    # use a quadrilateral grid
    quad = True
    # numerical quadrature choice
    elementQuadrature = CubeGaussQuadrature(nd,2)
    elementBoundaryQuadrature = CubeGaussQuadrature(nd-1,1)
else:
    # use a simplex grid
    elementQuadrature = SimplexGaussQuadrature(nd,4)
    elementBoundaryQuadrature = SimplexGaussQuadrature(nd-1,4)
    # if unstructured would need triangleOptions flag to be set
    he = 0.05
    triangleOptions = "VApq30Dena%8.8f" % he

#####################################################

#no shock capturing
shockCapturing = None

#nonlinear solver choices
multilevelNonlinearSolver  = Newton
levelNonlinearSolver = Newton
#linear problem so force 1 iteration allowed
maxNonlinearIts = 2
maxLineSearches = 1
fullNewtonFlag = True
#absolute nonlinear solver residual tolerance
nl_atol_res = 1.0e-8
#relative nonlinear solver convergence tolerance as a function of h
#(i.e., tighten relative convergence test as we refine)
tolFac = 0.0

#matrix type
matrix = SparseMatrix

if solveInParallel:
    multilevelLinearSolver = KSP_petsc4py
    #for petsc do things like
    #"-ksp_type cg -pc_type asm -pc_asm_type basic -ksp_atol  1.0e-10 -ksp_rtol 1.0e-10 -ksp_monitor_draw" or
    #-pc_type lu -pc_factor_mat_solver_package
    #can also set -pc_asm_overlap 2 with default asm type (restrict)
    levelLinearSolver = KSP_petsc4py#
    #for petsc do things like
    #"-ksp_type cg -pc_type asm -pc_asm_type basic -ksp_atol  1.0e-10 -ksp_rtol 1.0e-10 -ksp_monitor_draw" or
    #-pc_type lu -pc_factor_mat_solver_package
    #can also set -pc_asm_overlap 2 with default asm type (restrict)
    #levelLinearSolver = PETSc#
    #pick number of layers to use in overlap
    nLayersOfOverlapForParallel = 0
    #type of partition
    parallelPartitioningType = MeshParallelPartitioningTypes.node
    #parallelPartitioningType = MeshParallelPartitioningTypes.element
    #have to have a numerical flux in parallel
    numericalFluxType = Stokes_Advection_DiagonalUpwind_Diffusion_SIPG_exterior
    #for true residual test
    linearSolverConvergenceTest = 'r-true'
    #to allow multiple models to set different ksp options
    #linear_solver_options_prefix = 'poisson_'
    linearSmoother = SimpleNavierStokes2D
else:
    if solveIteratively:
        multilevelLinearSolver = KSP_petsc4py
        levelLinearSolver = KSP_petsc4py
    else:
        multilevelLinearSolver = LU
        levelLinearSolver = LU
    if useWeakBoundaryConditions:
        numericalFluxType =  Stokes_Advection_DiagonalUpwind_Diffusion_SIPG_exterior
    else:
        pass

#linear solver relative convergence test
linTolFac = 0.0
#linear solver absolute convergence test
l_atol_res = 1.0e-10

#linearSmoother=SimpleNavierStokes3D
#linearSmoother=NavierStokes3D_Qp
linearSmoother = petsc_LU
from petsc4py import PETSc
OptDB=PETSc.Options()
OptDB.clear()
OptDB.setValue("ksp_type","fgmres")
OptDB.setValue("ksp_atol",1e-20)
OptDB.setValue("ksp_atol",1e-12)
OptDB.setValue("pc_type","fieldsplit")
OptDB.setValue("pc_fieldsplit_type","schur")
OptDB.setValue("pc_fieldsplit_schur_fact_type","upper")
OptDB.setValue("fieldsplit_velocity_ksp_type","preonly")
OptDB.setValue("fieldsplit_velocity_pc_type","lu")
OptDB.setValue("fieldsplit_pressure_ksp_type","preonly")


from pyspark.sql import SparkSession

spark = SparkSession.builder.master("local[2]").appName("windowsFunction").getOrCreate()

simpleData = (("James", "Sales", 3000),
              ("Michael", "Sales", 4600),
              ("Robert", "Sales", 4100),
              ("Maria", "Finance", 3000),
              ("James", "Sales", 3000),
              ("Scott", "Finance", 3300),
              ("Jen", "Finance", 3900),
              ("Jeff", "Marketing", 3000),
              ("Kumar", "Marketing", 2000),
              ("Saif", "Sales", 4100)
              )

columns = ["employee_name", "department", "salary"]

df = spark.createDataFrame(data=simpleData, schema=columns)

# df.printSchema()
# df.show(truncate=False)

from pyspark.sql.window import Window
from pyspark.sql.functions import row_number, rank, dense_rank, percent_rank, ntile, cume_dist, lag, lead

windowSpec = Window.partitionBy("department").orderBy("salary")
# row_number
# df.withColumn("row_number", row_number().over(windowSpec)).show(truncate=False)

# rank
# df.withColumn("Rank", rank().over(windowSpec)).show(truncate=False)

# dense Rank
# df.withColumn("dense_rank",dense_rank().over(windowSpec)).show(truncate=False)

# percent_rank
# df.withColumn("percent_rank", percent_rank().over(windowSpec)).show(truncate=False)

# ntile
# df.withColumn("ntile",ntile(1).over(windowSpec)).show(truncate=False)

# cume_dist
# df.withColumn("cume_dist",cume_dist().over(windowSpec)).show(truncate=False)

# lag
# df.withColumn("lag",lag("salary",1).over(windowSpec)).show(truncate=False)

# lead
# df.withColumn("lead",  lead("salary", 1).over(windowSpec)).show(truncate=False)

windowsAgg = Window.partitionBy("department")
from pyspark.sql.functions import col, avg, max, min, row_number, sum

df.withColumn("row", row_number().over(windowSpec))\
    .withColumn("avg", avg(col("salary")).over(windowsAgg)) \
    .withColumn("sum", sum(col("salary")).over(windowsAgg)) \
    .withColumn("min", min(col("salary")).over(windowsAgg)) \
    .withColumn("max", max(col("salary")).over(windowsAgg)) \
    .where(col("row") == 1)\
    .show()


"""
Provides a command line interface to the DuMBSwitcher
"""

import dumb_switcher.controller as controller
from dumb_switcher.controller import WALLPAPER_PATH
import argparse
import sys
import dumb_switcher.DuMBSwitcher as ds

def main():
    """
    Parse command line arguments and call the associated controller functions.
    :return:
    """
    parser = argparse.ArgumentParser(description="Create a spanned wallpaper out of two images to fit on two monitors.")

    parser.add_argument("--left_wallpaper", help="The wallpaper to display on the left monitor")
    parser.add_argument("--right_wallpaper", help="The wallpaper to display on the right monitor")
    parser.add_argument("--slideshow_dir", help="The directory to get wallpapers from for a slideshow")
    parser.add_argument("--switch_both", action="store_true", help="Switch both monitors at the same time")
    parser.add_argument("--slideshow_duration", help="The amount of time before switching a wallpaper.")
    parser.add_argument("--enable_lock_screen", action="store_true",
                        help="Show the current wallpaper on the lock screen as well.")
    parser.add_argument("--stop_slideshow", action="store_true", help="Stop playing the slideshow on startup.")
    parser.add_argument("--start_slideshow", action="store_true",
                        help="Start the slideshow that is currently set to play.")
    parser.add_argument("--version", action="store_true", help="Display version info.")

    args = parser.parse_args()

    if args.version:
        print("DuMBSwitcher Version " + controller.get_version())
        exit(0)

    # Display help if there are not enough arguments
    if len(sys.argv) == 1:
        parser.print_help()

    # If the left and right wallpaper are specified, create the new wallpaper and set it
    if args.left_wallpaper and args.right_wallpaper:
        controller.set_wallpaper_once(args.left_wallpaper, args.right_wallpaper)

    # If the user wants the image on their lock screen, set the lock screen
    if args.enable_lock_screen:
        controller.set_lock_screen()

    # If the user wants to stop the slideshow, remove it from the startup programs
    if args.stop_slideshow:
        controller.create_or_remove_startup_process_for_slideshow(None, 0, False, remove=True)

    switch_both = False

    if args.switch_both:
        switch_both = True

    # If a slideshow directory was provided, set up a slideshow
    if args.slideshow_dir:

        if args.slideshow_duration:
            controller.create_or_remove_startup_process_for_slideshow(args.slideshow_dir, args.slideshow_duration,
                                                                      switch_both)
    # If the user wants to start the slideshow, start it
    if args.start_slideshow:

        if not args.slideshow_dir or not args.slideshow_duration:
            print("Error: You must specify a slideshow directory and duration")
        else:

            ds.run(WALLPAPER_PATH, slideshow=True, slideshow_duration=args.slideshow_duration,
                   switch_both_monitors=switch_both, slideshow_dir=args.slideshow_dir)

if __name__ == "__main__":
    main()