from miniMax import *
from tree_parser import *
from alphaBeta import *

#filename = sys.argv[1]
#print ("hello world! " + filename)
#data_list = parse_data_as_list(filename)

data_tree = GameTree()
#data_tree.build_tree(data_list)
#data_tree.armarArbol(1,3)
data_tree.armarArbol([99,0,0,0,0,0],[99,99,0,0, 99,0],3)

mini=MiniMax(data_tree)
print("MiniMax")
bestMove=mini.minimax(mini.root)

print()
print("alpha-beta")
alpha=AlphaBeta(data_tree)
alpha.alpha_beta_search(alpha.root)


"""Zero-field splitting.

See::

    Spin decontamination for magnetic dipolar coupling calculations:
    Application to high-spin molecules and solid-state spin qubits

    Timur Biktagirov, Wolf Gero Schmidt, and Uwe Gerstmann

    Phys. Rev. Research 2, 022024(R) – Published 30 April 2020

"""
from math import pi
from typing import List, Tuple, Dict

import numpy as np
from ase.units import Bohr, Ha, _c, _e, _hplanck

from my_gpaw.calculator import GPAW
from my_gpaw.grid_descriptor import GridDescriptor
from my_gpaw.typing import Array1D, Array2D, Array4D
from my_gpaw.hyperfine import alpha  # fine-structure constant: ~ 1 / 137
from my_gpaw.setup import Setup
from my_gpaw.pw.lfc import PWLFC
from my_gpaw.pw.descriptor import PWDescriptor
from my_gpaw.mpi import serial_comm

def zfs(calc: GPAW,
        method: int = 1) -> Array2D:
    """Zero-field splitting.

    Calculate magnetic dipole coupling tensor in eV.
    """
    (kpt1, kpt2), = calc.wfs.kpt_qs  # spin-polarized and gamma only

    nocc1 = (kpt1.f_n > 0.5).sum()
    nocc2 = (kpt2.f_n > 0.5).sum()

    assert nocc1 == nocc2 + 2, (nocc1, nocc2)

    if method == 1:
        wf1 = WaveFunctions.from_calc(calc, 0, nocc1 - 2, nocc1)
        wf12 = [wf1]
    else:
        wf1 = WaveFunctions.from_calc(calc, 0, 0, nocc1)
        wf2 = WaveFunctions.from_calc(calc, 1, 0, nocc2)
        wf12 = [wf1, wf2]

    D_vv = np.zeros((3, 3))

    if calc.world.rank == 0:
        compensation_charge = create_compensation_charge(wf1.setups,
                                                         wf1.pd,
                                                         calc.spos_ac)
        for wfa in wf12:
            for wfb in wf12:
                d_vv = zfs1(wfa, wfb, compensation_charge)
                D_vv += d_vv

    calc.world.broadcast(D_vv, 0)

    return D_vv

class WaveFunctions:
    def __init__(self,
                 psit_nR: Array4D,
                 P_ani: Dict[int, Array2D],
                 spin: int,
                 setups: List[Setup],
                 gd: GridDescriptor = None,
                 pd: PWDescriptor = None):
        """Container for wave function in real-space and projections."""

        self.pd = pd or PWDescriptor(ecut=None, gd=gd)
        self.psit_nR = psit_nR
        self.P_ani = P_ani
        self.spin = spin
        self.setups = setups

    @staticmethod
    def from_calc(calc: GPAW, spin: int, n1: int, n2: int) -> 'WaveFunctions':
        """Create WaveFunctions object GPAW calculation."""
        kpt = calc.wfs.kpt_qs[0][spin]
        gd = calc.wfs.gd.new_descriptor(pbc_c=np.ones(3, bool),
                                        comm=serial_comm)
        psit_nR = gd.empty(n2 - n1)
        for band, psit_R in enumerate(psit_nR):
            psit_R[:] = calc.get_pseudo_wave_function(
                band + n1,
                spin=spin) * Bohr**1.5

        return WaveFunctions(psit_nR,
                             kpt.projections.as_dict_on_master(n1, n2),
                             spin,
                             calc.setups,
                             gd=gd)

    def __len__(self) -> int:
        return len(self.psit_nR)

def create_compensation_charge(setups: List[Setup],
                               pd: PWDescriptor,
                               spos_ac: Array2D) -> PWLFC:
    compensation_charge = PWLFC([data.ghat_l for data in setups], pd)
    compensation_charge.set_positions(spos_ac)
    return compensation_charge

def zfs1(wf1: WaveFunctions,
         wf2: WaveFunctions,
         compensation_charge: PWLFC) -> Array2D:
    """Compute dipole coupling."""
    pd = wf1.pd
    setups = wf1.setups
    N2 = len(wf2)

    G_G = pd.G2_qG[0]**0.5
    G_G[0] = 1.0
    G_Gv = pd.get_reciprocal_vectors(add_q=False) / G_G[:, np.newaxis]

    n_sG = pd.zeros(2)
    for n_G, wf in zip(n_sG, [wf1, wf2]):
        D_aii = {}
        Q_aL = {}
        for a, P_ni in wf.P_ani.items():
            D_ii = np.einsum('ni, nj -> ij', P_ni, P_ni)
            D_aii[a] = D_ii
            Q_aL[a] = np.einsum('ij, ijL -> L', D_ii, setups[a].Delta_iiL)

        for psit_R in wf.psit_nR:
            n_G += pd.fft(psit_R**2)

        compensation_charge.add(n_G, Q_aL)

    nn_G = (n_sG[0] * n_sG[1].conj()).real
    D_vv = zfs2(pd, G_Gv, nn_G)

    n_nG = pd.empty(N2)
    for n1, psit1_R in enumerate(wf1.psit_nR):
        D_anii = {}
        Q_anL = {}
        for a, P1_ni in wf1.P_ani.items():
            D_nii = np.einsum('i, nj -> nij', P1_ni[n1], wf2.P_ani[a])
            D_anii[a] = D_nii
            Q_anL[a] = np.einsum('nij, ijL -> nL',
                                 D_nii, setups[a].Delta_iiL)

        for n_G, psit2_R in zip(n_nG, wf2.psit_nR):
            n_G[:] = pd.fft(psit1_R * psit2_R)

        compensation_charge.add(n_nG, Q_anL)

        nn_G = (n_nG * n_nG.conj()).sum(axis=0).real
        D_vv -= zfs2(pd, G_Gv, nn_G)

    D_vv -= np.trace(D_vv) / 3 * np.eye(3)  # should be traceless

    sign = 1.0 if wf1.spin == wf2.spin else -1.0

    return sign * alpha**2 * pi * D_vv * Ha

def zfs2(pd: PWDescriptor,
         G_Gv: Array2D,
         nn_G: Array1D) -> Array2D:
    """Integral."""
    D_vv = np.einsum('gv, gw, g -> vw', G_Gv, G_Gv, nn_G)
    D_vv *= 2 * pd.gd.dv / pd.gd.N_c.prod()
    return D_vv

def convert_tensor(D_vv: Array2D,
                   unit: str = 'eV') -> Tuple[float, float, Array1D, Array2D]:
    """Convert 3x3 tensor to D, E and easy axis.

    Input tensor must be in eV and the result can be returned in
    eV, μeV, MHz or 1/cm acording to the value uf *unit*
    (must be one of "eV", "ueV", "MHz", "1/cm").

    >>> D_vv = np.diag([1, 2, 3])
    >>> D, E, axis, _ = convert_tensor(D_vv)
    >>> D
    4.5
    >>> E
    0.5
    >>> axis
    array([0., 0., 1.])
    """
    if unit == 'ueV':
        scale = 1e6
    elif unit == 'MHz':
        scale = _e / _hplanck * 1e-6
    elif unit == '1/cm':
        scale = _e / _hplanck / _c / 100
    elif unit == 'eV':
        scale = 1.0
    else:
        raise ValueError(f'Unknown unit: {unit}')

    (e1, e2, e3), U = np.linalg.eigh(D_vv * scale)

    if abs(e1) > abs(e3):
        D = 1.5 * e1
        E = 0.5 * (e2 - e3)
        axis = U[:, 0]
    else:
        D = 1.5 * e3
        E = 0.5 * (e2 - e1)
        axis = U[:, 2]

    return D, E, axis, D_vv * scale

def main(argv: List[str] = None) -> Array2D:
    """CLI interface."""
    import argparse

    parser = argparse.ArgumentParser(
        prog='python3 -m gpaw.zero_field_splitting',
        description='...')
    add = parser.add_argument
    add('file', metavar='input-file',
        help='GPW-file with wave functions.')
    add('-u', '--unit', default='ueV', choices=['ueV', 'MHz', '1/cm'],
        help='Unit.  Must be "ueV" (micro-eV, default), "MHz" or "1/cm".')
    add('-m', '--method', type=int, default=1)

    args = parser.parse_intermixed_args(argv)

    calc = GPAW(args.file)

    D_vv = zfs(calc, args.method)
    D, E, axis, D_vv = convert_tensor(D_vv, args.unit)

    unit = args.unit
    if unit == 'ueV':
        unit = 'μeV'

    print('D_ij = (' +
          ',\n        '.join('(' + ', '.join(f'{d:10.3f}' for d in D_v) + ')'
                             for D_v in D_vv) +
          ') ', unit)
    print('i, j = x, y, z')
    print()
    print(f'D = {D:.3f} {unit}')
    print(f'E = {E:.3f} {unit}')
    x, y, z = axis
    print(f'axis = ({x:.3f}, {y:.3f}, {z:.3f})')

    return D_vv

if __name__ == '__main__':
    main()


from core_operations.models import US_COUNTRY_CODE, NUMBER_OF_DAYS_IN_A_YEAR, LIST_OF_STATES_IN_US
from core_operations.models import FormattedPhoneNumberField, YearsOfWorkField
from django.db import models
import re
from datetime import date
from datetime import datetime
# from internal_users.models import InternalUser
from django.utils import timezone
from faker import Faker

fake = Faker()

# added on 2023-06-03. common operational models, functions shall be defined in core_operations app.

UNASSIGNED = 'Unassigned'
VISITOR = 'visitor only'
SERVICE_FRONT = 'service advisor'
SERVICE_GARAGE = 'service technican'
TALENT_MANAGEMENT = 'talent management'
ACCOUNTING = 'Accounting'
CODING_MASTERS = 'code masters'
CYBER_SECURITY = 'cyber security'
TRAINEE = 'Trainee'
LEGAL = 'legal'

DEPARTMENTS = ((UNASSIGNED, 'your deparment has not been assigned yet.'),
               (VISITOR, 'visitor group'),
               (SERVICE_FRONT, 'service advisor group'),
               (SERVICE_GARAGE, 'service technican group'),
               (TALENT_MANAGEMENT, 'talent management group'),
               (LEGAL, 'legal group'),
               (TRAINEE, 'trainee group'),
               (CODING_MASTERS, 'code master group'),
               (CYBER_SECURITY, 'cyber security group'),
               )

PAY_TYPE_UNASSIGNED = 0
PAY_TYPE_HOURLY = 1
PAY_TYPE_SALARY = 2
PAY_TYPE_BONUS = 3
PAY_TYPE_INTERNSHIP = 4
PAY_TYPE_OTHER1 = 5
PAY_TYPE_OTHER2 = 6
PAY_TYPES = ((PAY_TYPE_UNASSIGNED, 'unassigned pay type. Must be assigned before the first work day.'),
             (PAY_TYPE_HOURLY, 'hourly'),
             (PAY_TYPE_SALARY, 'salaried'),
             (PAY_TYPE_INTERNSHIP, 'internship'),
             (PAY_TYPE_BONUS, 'bonus pay'),
             (PAY_TYPE_OTHER1, 'pay type other-1'),
             (PAY_TYPE_OTHER2, 'pay type other-2'),
             )

# 2023-05-23 add pay frequency choices
# Weekly – 52 paychecks per year.
# Biweekly – 26 paychecks per year.
# Semi-monthly – 24 paychecks per year.
# Monthly – 12 paychecks per year.
PAY_FREQUENCY_UNDEFINED = 0
PAY_FREQUENCY_DAILY = 1
PAY_FREQUENCY_WEEKLY = 2
PAY_FREQUENCY_BIWEEKLY = 3
PAY_FREQUENCY_SEMIMONTHLY = 4
PAY_FREQUENCY_MONTHLY = 5
PAY_FREQUENCY_SEMIANNUALLY = 6
PAY_FREQUENCY_ANNUALLY = 7
PAY_FREQUENCY_RESERVE1 = 8
PAY_FREQUENCY_RESERVE2 = 9
PAY_FREQUENCY_LIST = ((PAY_FREQUENCY_UNDEFINED, 'pay frequency not defined'),
                      (PAY_FREQUENCY_DAILY, 'daily'),
                      (PAY_FREQUENCY_WEEKLY, 'weekly'),
                      (PAY_FREQUENCY_BIWEEKLY, 'bi-weekly'),
                      (PAY_FREQUENCY_SEMIMONTHLY, 'semi-monthly'),
                      (PAY_FREQUENCY_MONTHLY, 'monthly'),
                      (PAY_FREQUENCY_SEMIANNUALLY, 'monthly'),
                      (PAY_FREQUENCY_ANNUALLY, 'monthly'),
                      (PAY_FREQUENCY_RESERVE1,
                       'reserved pay frequency 1; not used yet'),
                      (PAY_FREQUENCY_RESERVE2,
                       'reserved pay frequency 2; not used yet'),
                      )

class TalentsModel(models.Model):
    talent_id = models.BigAutoField(primary_key=True)
    talent_employee_id = models.IntegerField(unique=True)
    talent_first_name = models.CharField(
        max_length=50, null=False, verbose_name="Legal First Name (as in driver license (DL) or passport)")
    talent_last_name = models.CharField(
        max_length=50, null=False, verbose_name="Legal Last Name (as in driver license (DL) or passport)")
    talent_middle_name = models.CharField(
        max_length=50, null=True, blank=True, verbose_name="Middle Name")
    talent_preferred_name = models.CharField(
        max_length=50, null=True, blank=True, verbose_name="Preferred Name")
    talent_email = models.EmailField(max_length=50, blank=True, null=True)
    # this custom field works with a glitch as of 2023-06-03.
    talent_phone_number_primary = FormattedPhoneNumberField()
    talent_phone_number_primary_digits_only = models.CharField(
        max_length=20, null=True, blank=True)
    talent_phone_number_alternates_01 = FormattedPhoneNumberField(null=True)
    talent_phone_number_alternates_02 = FormattedPhoneNumberField(null=True)
    talent_emergency_contact = models.CharField(
        max_length=200, null=True, blank=True)
    talent_date_of_birth = models.DateField(
        verbose_name='Date of Birth (DOB)', null=True)
    talent_ssn = models.CharField(max_length=15,
                                  verbose_name='SSN or Tax ID (TIN)', null=True)

    talent_physical_address_01 = models.CharField(
        verbose_name='street address 01', max_length=100)
    talent_physical_address_02 = models.CharField(
        verbose_name='street address 02 (apt numbers, unit #, etc.)', max_length=100, blank=True, null=True)
    talent_physical_address_city = models.CharField(max_length=50, null=True)
    talent_physical_address_state = models.CharField(max_length=2, null=True)
    talent_physical_address_zip_code = models.CharField(
        max_length=10, null=True)
    talent_physical_address_country = models.CharField(verbose_name='Country',
                                                       max_length=50, default='US')
    talent_mailing_address_is_the_same_physical_address = models.BooleanField(
        default=True)
    talent_mailing_address_01 = models.CharField(
        verbose_name='mailing address 01', max_length=100)
    talent_mailing_address_02 = models.CharField(
        verbose_name=' mailing address 02 (apt numbers, unit #, etc)', max_length=100, blank=True, null=True)
    talent_mailing_address_city = models.CharField(max_length=50, null=True)
    talent_mailing_address_state = models.CharField(max_length=2, null=True)
    talent_mailing_address_zip_code = models.CharField(max_length=10)
    talent_mailing_address_country = models.CharField(
        max_length=50, default='US')
    talent_education_level = models.CharField(max_length=100, default='None')
    talent_certifications = models.CharField(
        max_length=500, null=True, blank=True)

    talent_hire_date = models.DateTimeField(blank=True, null=True)
    talent_department = models.CharField(max_length=50,
                                         choices=DEPARTMENTS,
                                         default=UNASSIGNED)
    talent_supervisor = models.ForeignKey(
        'self', on_delete=models.SET_NULL, null=True, blank=True)
    talent_work_start_date = models.DateTimeField(blank=True, null=True)
    talent_pay_type = models.PositiveSmallIntegerField(default=PAY_TYPE_UNASSIGNED,
                                                       choices=PAY_TYPES)
    talent_pay_rate = models.DecimalField(
        max_digits=10, decimal_places=2, default=0.00)
    talent_pay_frequency = models.PositiveSmallIntegerField(choices=PAY_FREQUENCY_LIST,
                                                            default=PAY_FREQUENCY_UNDEFINED)
    talent_previous_department = models.CharField(max_length=50,
                                                  choices=DEPARTMENTS,
                                                  default=UNASSIGNED,
                                                  )
    talent_discharge_date = models.DateTimeField(null=True, blank=True)
    talent_years_of_work = YearsOfWorkField(null=True, blank=True)
    talent_HR_remarks_json = models.TextField(null=True, blank=True)
    talent_incident_record_json = models.TextField(null=True, blank=True)

    # added on 2023-06-02 to store the future talent_digital_files
    talent_digital_file_storage_path_01 = models.CharField(
        max_length=2000, null=True, blank=True)
    talent_digital_file_storage_path_02 = models.CharField(
        max_length=2000, null=True, blank=True)

    talent_is_active = models.BooleanField(default=True)
    talent_created_at = models.DateTimeField(auto_now_add=True)
    talent_last_udpated_at = models.DateTimeField(auto_now=True)
    # talent_created_by_user = models.ForeignKey(
    #     InternalUser, null=True, on_delete=models.SET_NULL)

    @property
    def talent_full_name(self):
        return f"{self.talent_first_name} {self.talent_last_name} {self.talent_middle_name}"

    @property
    def talent_full_physical_address(self):
        addr_fields = [self.talent_physical_address_01, self.talent_physical_address_02, self.talent_physical_address_city, self.talent_physical_address_state.upper(),
                       self.talent_physical_address_zip_code]

        full_address = " ".join(
            [field for field in addr_fields if field is not None]).strip()
        if len(full_address) != 0:
            full_address = full_address + " " + self.talent_physical_address_country
        else:
            full_address = full_address
        return full_address

    @property
    def talent_full_mailing_address(self):
        addr_fields = [self.talent_mailing_address_01, self.talent_mailing_address_02, self.talent_mailing_address_city, self.talent_mailing_address_state,
                       self.talent_mailing_address_zip_code]
        full_address = " ".join(
            [field for field in addr_fields if field is not None]).strip()

        # if the first 5 fields are empty; do not add the country in the end, return none instead.
        if len(full_address) != 0:
            full_address = full_address + " " + self.talent_mailing_address_country
        else:
            full_address = full_address
        return full_address

    def __init__(self, *args, **kwargs):
        super(TalentsModel, self).__init__(*args, **kwargs)
        self._initial_state = {field: getattr(
            self, field) for field in self.fields_to_track()}

    @classmethod
    def fields_to_track(cls):
        return ['talent_pay_rate', 'talent_pay_frequency', 'talent_date_of_birth', 'talent_email', 'talent_phone_number_primary']

    def get_changed_fields(self):
        changed_fields = {}
        for field in self.fields_to_track():
            if self._initial_state[field] != getattr(self, field):
                changed_fields[field] = self._initial_state[field]
        return changed_fields

    def save(self, *args, **kwargs):
        # if not self.pk:
        # Only set the talent_created_by_user_id if this is a new instance
        # self.talent_created_by_user_id = request.user.id
        # super(TalentsModel, self).save(*args, **kwargs)

        # creating a employee_id that is different from the talent_id that is used in the database.
        # employee ID start from 1024
        if not self.talent_employee_id:
            last_talent_employee = TalentsModel.objects.order_by(
                '-talent_employee_id').first()
            if last_talent_employee:
                self.talent_employee_id = last_talent_employee.talent_employee_id + 2
            else:
                self.talent_employee_id = 1024

        elif self.talent_employee_id and self.pk:
            self.talent_employee_id = self.talent_employee_id

        super(TalentsModel, self).save(*args, **kwargs)

    def __str__(self):
        return f"{self.talent_first_name} {self.talent_last_name} {self.talent_middle_name}"

    class Meta:
        db_table = 'talent_managment'
        ordering = ['-talent_id']

class TalentDocuments(models.Model):
    document_id = models.BigAutoField(primary_key=True)
    talent = models.ForeignKey(
        TalentsModel, on_delete=models.SET_NULL, null=True)
    talent_employment_docs = models.FileField(
        upload_to='2023_talent_employment_docs')
    talent_uploaded_photos = models.ImageField(upload_to='photos')
    uploaded_date = models.DateTimeField(default=timezone.now)
    document_is_active = models.BooleanField(default=True)

    class Meta:

        db_table = 'talent_documents'
        ordering = ['-talent_id']

class TalentAudit(models.Model):
    talent_audit_id = models.BigAutoField(primary_key=True)
    # Changed from OneToOneField to ForeignKey to allow multiple audit records per talent
    talent = models.ForeignKey(TalentsModel, on_delete=models.CASCADE)
    created_by = models.ForeignKey(
        'internal_users.InternalUser', related_name="created_audits", swappable=True, on_delete=models.SET_NULL, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    field_changed = models.CharField(max_length=50, null=True, blank=True)
    old_value = models.CharField(max_length=255, null=True, blank=True)
    new_value = models.CharField(max_length=255, null=True, blank=True)

    class Meta:

        db_table = 'talent_audit'
        ordering = ['-talent_audit_id']


from pydantic import BaseSettings, BaseModel

from pathlib import Path

from .languages import WIKIPEDIA_LANGS

import toml

class Settings(BaseSettings):
    status: str = "unknown"
    logging_chat: int = None

    db_path: Path = Path("jdanbot.db")
    music_path: Path = Path("media/music")

    admin_notes: list[str]

    bot_owners: list[int] = [795449748, 0]

    class Tokens(BaseModel):
        bot_token: str

    class Schedule(BaseModel):
        delay_seconds: int = 20

        katz_bots: bool = False

    class Egg(BaseModel):
        commands: list[str]
        audio: Path

    tokens: Tokens
    schedule: Schedule = Schedule()
    eggs: list[Egg]

with open("settings.toml") as file:
    settings_file = toml.loads(file.read())

with open(".secrets.toml") as file:
    secrets_file = toml.loads(file.read())

settings = Settings.parse_obj(settings_file | secrets_file)

BASE_DIR = Path(__file__).parent.parent.parent
LOCALES_DIR = BASE_DIR / "locales"

WIKIPEDIA_SHORTCUTS = {
    "ru": ["w"],
    "en": ["v"],
    "uk": ["wua", "wikiua"]
}

WIKI_COMMANDS = []

for lang in WIKIPEDIA_LANGS:
    WIKI_COMMANDS.extend([f"wiki{lang}", f"w{lang}"])

for lang in WIKIPEDIA_SHORTCUTS:
    WIKI_COMMANDS.extend(WIKIPEDIA_SHORTCUTS[lang])


import time
from unittest import skip
from qiskit.providers.jobstatus import JobStatus

from qiskit_ibm_runtime.exceptions import RuntimeJobTimeoutError

from ..unit.mock.proxy_server import MockProxyServer, use_proxies
from ..ibm_test_case import IBMIntegrationJobTestCase
from ..decorators import run_integration_test
from ..utils import cancel_job_safe, wait_for_status

class TestIntegrationResults(IBMIntegrationJobTestCase):
    """Integration tests for result callbacks."""

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_result_callback(self, service):
        """Test result callback."""

        def result_callback(job_id, result):
            nonlocal final_it
            if "iteration" in result:
                final_it = result["iteration"]
            nonlocal callback_err
            if job_id != job.job_id():
                callback_err.append(f"Unexpected job ID: {job_id}")
            if "interim_results" in result and result["interim_results"] != int_res:
                callback_err.append(f"Unexpected interim result: {result}")

        int_res = "foo"
        final_it = 0
        callback_err = []
        iterations = 3
        job = self._run_program(
            service,
            backend="ibmq_qasm_simulator",
            interim_results=int_res,
            callback=result_callback,
        )
        job.wait_for_final_state()
        self.assertEqual(iterations - 1, final_it)
        self.assertFalse(callback_err)
        self.assertIsNotNone(job._ws_client._server_close_code)

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_result_callback_with_job_result(self, service):
        """Test result callback along with job result."""

        def result_callback(job_id, result):
            nonlocal count
            count = count + 1
            nonlocal final_it
            if "iteration" in result:
                final_it = result["iteration"]
            nonlocal callback_err
            if job_id != job.job_id():
                callback_err.append(f"Unexpected job ID: {job_id}")
            if "interim_results" in result and result["interim_results"] != int_res:
                callback_err.append(f"Unexpected interim result: {result}")

        int_res = "foo"
        count = 0
        final_it = 0
        callback_err = []
        iterations = 3
        job = self._run_program(
            service,
            backend="ibmq_qasm_simulator",
            interim_results=int_res,
            callback=result_callback,
        )
        job.result()
        self.assertEqual(iterations - 1, final_it)
        self.assertEqual(iterations + 1, count)
        self.assertFalse(callback_err)
        self.assertIsNotNone(job._ws_client._server_close_code)

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_stream_results(self, service):
        """Test stream_results method."""

        def result_callback(job_id, result):
            nonlocal final_it
            if "iteration" in result:
                final_it = result["iteration"]
            nonlocal callback_err
            if job_id != job.job_id():
                callback_err.append(f"Unexpected job ID: {job_id}")
            if "interim_results" in result and result["interim_results"] != int_res:
                callback_err.append(f"Unexpected interim result: {result}")

        int_res = "bar"
        final_it = 0
        callback_err = []
        iterations = 3
        job = self._run_program(
            service,
            backend="ibmq_qasm_simulator",
            interim_results=int_res,
        )
        job.stream_results(result_callback)
        job.wait_for_final_state()
        self.assertEqual(iterations - 1, final_it)
        self.assertFalse(callback_err)
        self.assertIsNotNone(job._ws_client._server_close_code)

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_stream_results_done(self, service):
        """Test streaming results after job is done."""

        def result_callback(job_id, result):
            # pylint: disable=unused-argument
            nonlocal called_back_count
            called_back_count += 1

        called_back_count = 0
        job = self._run_program(
            service,
            backend="ibmq_qasm_simulator",
            interim_results="foobar",
        )
        job.wait_for_final_state()
        job._status = JobStatus.RUNNING  # Allow stream_results()
        job.stream_results(result_callback)
        time.sleep(2)
        # Callback is expected twice because both interim and final results are returned
        self.assertEqual(2, called_back_count)
        self.assertIsNotNone(job._ws_client._server_close_code)

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_retrieve_interim_results(self, service):
        """Test retrieving interim results with API endpoint"""
        job = self._run_program(service)
        job.wait_for_final_state()
        interim_results = job.interim_results()
        self.assertIn("iteration", interim_results[0])
        self.assertIn("counts", interim_results[0])

    @run_integration_test
    def test_result_timeout(self, service):
        """Test job result timeout"""
        job = self._run_program(service)
        with self.assertRaises(RuntimeJobTimeoutError):
            job.result(0.1)

    @run_integration_test
    def test_wait_for_final_state_timeout(self, service):
        """Test job wait_for_final_state timeout"""
        job = self._run_program(service)
        with self.assertRaises(RuntimeJobTimeoutError):
            job.wait_for_final_state(0.1)

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_callback_error(self, service):
        """Test error in callback method."""

        def result_callback(job_id, result):
            # pylint: disable=unused-argument
            if "iteration" in result and result["iteration"] == 0:
                raise ValueError("Kaboom!")
            nonlocal final_it
            if "iteration" in result:
                final_it = result["iteration"]

        final_it = 0
        iterations = 10
        inputs = {"iterations": iterations, "sleep_per_iteration": 3}
        with self.assertLogs("qiskit_ibm_runtime", level="WARNING") as err_cm:
            job = self._run_program(
                service,
                backend="ibmq_qasm_simulator",
                inputs=inputs,
                interim_results="foo",
                callback=result_callback,
            )
            job.wait_for_final_state()

        self.assertIn("Kaboom", ", ".join(err_cm.output))
        self.assertEqual(iterations - 1, final_it)
        self.assertIsNotNone(job._ws_client._server_close_code)

    @run_integration_test
    def test_callback_cancel_job(self, service):
        """Test canceling a running job while streaming results."""

        def result_callback(job_id, result):
            # pylint: disable=unused-argument
            nonlocal final_it
            if "iteration" in result:
                final_it = result["iteration"]

        final_it = 0
        iterations = 5
        sub_tests = [JobStatus.QUEUED, JobStatus.RUNNING]

        for status in sub_tests:
            with self.subTest(status=status):
                if status == JobStatus.QUEUED:
                    _ = self._run_program(service)

                job = self._run_program(
                    service=service,
                    interim_results="foo",
                    callback=result_callback,
                )
                wait_for_status(job, status)
                if not cancel_job_safe(job, self.log):
                    return
                time.sleep(3)  # Wait for cleanup
                self.assertIsNotNone(job._ws_client._server_close_code)
                self.assertLess(final_it, iterations)

    @skip("skip until qiskit-ibm-runtime #933 is fixed")
    @run_integration_test
    def test_websocket_proxy(self, service):
        """Test connecting to websocket via proxy."""

        def result_callback(job_id, result):  # pylint: disable=unused-argument
            nonlocal callback_called
            callback_called = True

        MockProxyServer(self, self.log).start()
        callback_called = False

        with use_proxies(service, MockProxyServer.VALID_PROXIES):
            job = self._run_program(
                service,
                backend="ibmq_qasm_simulator",
                callback=result_callback,
            )
            job.wait_for_final_state()

        self.assertTrue(callback_called)

    @run_integration_test
    def test_websocket_proxy_invalid_port(self, service):
        """Test connecting to websocket via invalid proxy port."""

        def result_callback(job_id, result):  # pylint: disable=unused-argument
            nonlocal callback_called
            callback_called = True

        callback_called = False
        invalid_proxy = {
            "https": "http://{}:{}".format(
                MockProxyServer.PROXY_IP_ADDRESS, MockProxyServer.INVALID_PROXY_PORT
            )
        }
        # TODO - verify WebsocketError in output log. For some reason self.assertLogs
        # doesn't always work even when the error is clearly logged.
        with use_proxies(service, invalid_proxy):
            job = self._run_program(service, callback=result_callback)
            job.wait_for_final_state()
        self.assertFalse(callback_called)


def get_employees():
    db1 = {}
    key = 1
    while key == 1:
        name = input("Введите имя рабочего:\n")
        salary = int(input(f"Введите зарплату, которую получает {name}:\n"))
        db = {name: salary}
        db1.update(db)
        exit_ = input("Продолжить?:\n")
        if exit_ == "да":
            key = 1
        else:
            key = 0
    return print(db1)


from sklearn import datasets
from sklearn.impute import SimpleImputer
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np

# Multiclass Classification Datasets

def load_mnist():
    mnist = datasets.load_digits(as_frame=True)
    mnist_X, mnist_y = mnist.data, mnist.target

    X_train, X_test, y_train, y_test = train_test_split(mnist_X, mnist_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_forest_covertypes():
    forest = datasets.fetch_covtype(as_frame=True)
    forest_X, forest_y = forest.data, forest.target
    forest_X = forest_X[:15000]
    forest_y = forest_y[:15000]

    X_train, X_test, y_train, y_test = train_test_split(forest_X, forest_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_kepler_exoplanets():
    kepler = pd.read_csv('data/kepler_exoplanet.csv')
    not_X_columns = ['rowid', 'kepid', 'kepoi_name', 'kepler_name', 'koi_score', 'koi_pdisposition', 'koi_disposition', 'koi_teq_err1', 'koi_teq_err2', 'koi_tce_delivname']

    kepler_X = kepler.drop(not_X_columns, axis=1)
    kepler_y = kepler['koi_pdisposition']

    imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
    kepler_X_array = imputer.fit_transform(kepler_X)
    kepler_X = pd.DataFrame(kepler_X_array, columns=kepler_X.columns)

    X_train, X_test, y_train, y_test = train_test_split(kepler_X, kepler_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

# Regression Datasets

def load_cali_housing():
    california = datasets.fetch_california_housing(as_frame=True)
    cali_X, cali_y = california.data, california.target

    X_train, X_test, y_train, y_test = train_test_split(cali_X, cali_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_melbourne_housing():
    melbourne = pd.read_csv('data/melbourne_housing_data.csv')

    not_X_columns = ['Address', 'Price', 'SellerG', 'Date']
    melbourne_X = melbourne.drop(not_X_columns, axis=1)
    melbourne_y = melbourne['Price']

    # Convert all features to integers
    categorical_cols = ['Suburb', 'Type', 'Method', 'CouncilArea', 'Regionname']
    for col_name in categorical_cols:
        melbourne_X[col_name] = pd.Categorical(melbourne_X[col_name]).codes

    # Impute missing values
    imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
    melbourne_X_array = imputer.fit_transform(melbourne_X)
    melbourne_X = pd.DataFrame(melbourne_X_array, columns=melbourne_X.columns)

    X_train, X_test, y_train, y_test = train_test_split(melbourne_X, melbourne_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_world_happiness():
    happiness = pd.read_csv('data/world-happiness-report-2021.csv')

    X_columns = ['Regional indicator', 'Logged GDP per capita', 'Social support', 'Healthy life expectancy', 'Freedom to make life choices', 'Generosity', 'Perceptions of corruption']
    happiness_X = happiness[X_columns]
    happiness_y = happiness['Ladder score']

    happiness_X['Regional indicator'] = pd.Categorical(happiness_X['Regional indicator']).codes

    X_train, X_test, y_train, y_test = train_test_split(happiness_X, happiness_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

# Binary Classification Datasets

def load_heart_attack():
    heart_attack = pd.read_csv('data/heart-attack.csv')

    heart_X = heart_attack.drop('output', axis=1)
    heart_y = heart_attack['output']

    X_train, X_test, y_train, y_test = train_test_split(heart_X, heart_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_stroke():
    stroke = pd.read_csv('data/healthcare-dataset-stroke-data.csv')

    not_X_columns = ['id', 'stroke']
    stroke_X = stroke.drop(not_X_columns, axis=1)
    stroke_y = stroke['stroke']

    # Convert all features to integers
    categorical_cols = ['gender', 'ever_married', 'work_type', 'Residence_type', 'smoking_status']
    for col_name in categorical_cols:
        stroke_X[col_name] = pd.Categorical(stroke_X[col_name]).codes

    # Impute missing values
    imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
    stroke_X_array = imputer.fit_transform(stroke_X)
    stroke_X = pd.DataFrame(stroke_X_array, columns=stroke_X.columns)

    X_train, X_test, y_train, y_test = train_test_split(stroke_X, stroke_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_telecom():
    telecom = pd.read_csv('data/telecom_users.csv')

    not_X_columns = ['Unnamed: 0', 'customerID', 'TotalCharges', 'Churn']
    telecom_X = telecom.drop(not_X_columns, axis=1)
    telecom_y = pd.Categorical(telecom['Churn']).codes

    # Convert all features to integers
    not_categorical_cols = ['SeniorCitizen', 'tenure', 'MonthlyCharges']
    for col_name in telecom_X.columns:
        if col_name not in not_categorical_cols:
            telecom_X[col_name] = pd.Categorical(telecom_X[col_name]).codes

    # Impute missing values
    imputer = SimpleImputer(missing_values=np.nan, strategy='most_frequent')
    telecom_X_array = imputer.fit_transform(telecom_X)
    telecom_X = pd.DataFrame(telecom_X_array, columns=telecom_X.columns)

    X_train, X_test, y_train, y_test = train_test_split(telecom_X, telecom_y, test_size=0.2, random_state=42)
    return X_train, X_test, y_train, y_test

def load_multiclass():
    all_data = {}

    all_data['mnist'] = load_mnist()
    all_data['forest_covertypes'] = load_forest_covertypes()
    all_data['kepler_exoplanets'] = load_kepler_exoplanets()

    return all_data

def load_regression():
    all_data = {}

    all_data['california_housing'] = load_cali_housing()
    all_data['melbourne_housing'] = load_melbourne_housing()
    all_data['world_happiness'] = load_world_happiness()

    return all_data

def load_binary():
    all_data = {}

    all_data['heart_attack'] = load_heart_attack()
    all_data['stroke'] = load_stroke()
    all_data['telecom'] = load_telecom()

    return all_data


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 18 11:28:15 2017

@author: vitorhadad
"""

import torch
from torch import nn, cuda
from torch.autograd import Variable
import torch.nn.functional as F
from torch import optim
import numpy as np
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence

class RNN(nn.Module):

    def __init__(self,
                 input_size,
                 hidden_size,
                 num_layers=1,
                 bidirectional = False,
                 class_weights = [1, 10]):

        super(RNN, self).__init__()

        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.num_directions = 2 if bidirectional else 1
        self.seq_len = 1
        self.num_classes = 2
        self.class_weights = class_weights
        self.loss_fn = nn.CrossEntropyLoss(reduce = False,
                        weight = torch.FloatTensor(self.class_weights))

        self.count_loss_fn = nn.MSELoss()

        self.rnn = nn.LSTM(input_size,
                           hidden_size,
                           num_layers,
                           bidirectional = bidirectional)

        self.logit_layer = nn.Linear(hidden_size, self.num_classes)

        self.count_layer = nn.Linear(hidden_size, 1)

        self.c0 = nn.Parameter(torch.randn(num_layers * self.num_directions,
                              1,
                              hidden_size), requires_grad = True)

        self.h0 = nn.Parameter(torch.randn(num_layers * self.num_directions,
                              1,
                              hidden_size), requires_grad = True)

        self.optim = optim.Adam(self.parameters(), lr=0.001)

    def forward(self, inputs, lens = None):

        if lens is None:
            lens = inputs.any(2).sum(0)

        inputs = Variable(torch.FloatTensor(inputs),
                          requires_grad = False)

        order = np.flip(np.argsort(lens), 0).astype(int)
        order_r = torch.LongTensor(order[order])

        seq = pack_padded_sequence(inputs[:,order,:], lens[order])
        this_batch_size = seq.batch_sizes[0]
        initial_state = (self.c0.repeat(1, this_batch_size, 1),
                         self.h0.repeat(1, this_batch_size, 1))

        outputs, staten = self.rnn(seq, initial_state)
        outputs, lens = pad_packed_sequence(outputs)
        outputs = outputs[:, :, :self.hidden_size] +\
                  outputs[:, :, self.hidden_size:]

        prelogits = outputs[:,order_r,:].transpose(1,0)

        logits = self.logit_layer(prelogits)

        precounts = prelogits.sum(1).squeeze()

        counts = self.count_layer(precounts)
        return logits, counts

    def run(self, inputs, true_outputs, lens = None):

        if lens is None:
            lens = inputs.any(2).sum(0)

        ylogits, ycount = self.forward(inputs, lens)
        ytruth = Variable(torch.LongTensor(true_outputs), requires_grad = False)

        logit_loss = 0
        for i,l in enumerate(lens):
            l = lens[i]
            yh = ylogits[i,:l]
            yt = ytruth[i,:l].view(-1)
            try:
                logit_loss += self.loss_fn(yh, yt).mean()
            except RuntimeError as e:
                print(e)
        logit_loss /= batch_size

        count_loss = self.count_loss_fn(ycount, ytruth.sum(1).float())

        loss = logit_loss + count_loss

        self.optim.zero_grad()
        loss.backward()
        self.optim.step()

        return (logit_loss.data.numpy()[0],
                count_loss.data.numpy()[0],
                ylogits, ycount.data.numpy())

    def __str__(self):
        return "RNN_{}-{}"\
                .format(self.hidden_size,
                        self.num_layers)

#%%

if __name__ == "__main__":

    from matching.utils.data_utils import open_file, confusion
    from sys import argv, platform

    if platform == "darwin":
        argv.extend(["abo", 1, 100, .5, np.random.randint(1e8)])

    #if len(argv) > 1:
    print("Creating new RNN")
    env_type = argv[1]
    num_layers = int(argv[2])
    hidden_size = int(argv[3])
    c = float(argv[4])
    s = str(argv[5])
    input_size = {"abo":24, "optn":294}
    net = RNN(input_size=input_size[env_type],
          hidden_size=hidden_size,
          num_layers=num_layers,
          bidirectional=True,
          class_weights = [1,100*c])

    batch_size = 32
    open_every = 10
    save_every = 500
    log_every = 10

    name = "{}-{}_{}".format(
            str(net),
            env_type,
            s)

#%%
    for i in range(10000000):

        if i % open_every == 0:
            X, Y, GN = open_file(env_type = env_type, open_GN = True, open_A = False)
            SS = np.concatenate([X, GN], 2).transpose((1,0,2))
            n = SS.shape[1]

        idx = np.random.choice(n, size=batch_size)
        inputs = SS[:,idx,:]
        ytrue = Y[idx]

        lens = inputs.any(2).sum(0)

        avg_ones = np.hstack([Y[k,:l,0] for k,l in zip(idx, lens)]).mean()

        if avg_ones > 0:
            w = c*1/avg_ones

        net.loss_fn = nn.CrossEntropyLoss(reduce = False,
                        weight = torch.FloatTensor([1, w]))

        lloss,closs, ylogits, ycount = net.run(inputs, ytrue, lens)

        cacc = np.mean(ycount.round() == ytrue.sum(1))
        tp, tn, fp, fn = confusion(ylogits, ytrue, lens)
        tpr = tp/(tp+fn)
        tnr = tn/(tn+fp)
        lacc = (tp + tn)/(tp+fp+tn+fn)

        if tpr < .1:
            c *= 1.05
        if tnr < .1:
            c *= .95

        msg = "{:1.4f},{:1.4f},{:1.4f},"\
                "{:1.4f},{:1.4f},{:1.4f},{:1.4f}"\
                .format(lloss,
                        closs,
                    tpr, # True positive rate
                    tnr, # True negative rate
                    lacc, # Logits accuracy
                    cacc, # Count accuracy
                    w)
        if i % log_every == 0:
            print(msg)
            if platform == "linux":
                with open("results/" + name + ".txt", "a") as f:
                    print(msg, file = f)

        if platform == "linux" and i % save_every == 0:
            torch.save(net, "results/" + name)


import pygame
import Snake, Apple
import os

os.environ['SDL_VIDEO_CENTERED'] = '1'

pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()
pygame.display.set_caption("Sssssnake")

# 10x10 segments
winwidth = 200
winheight = 240
win = pygame.display.set_mode((winwidth, winheight))

# segment - 20x20px
segsize = 20
snake = Snake.Snake(0, 20, segsize, segsize-2, segsize-2)
apple = Apple.Apple(segsize//2, winwidth, winheight, segsize, snake.segments)

# font
font = pygame.font.SysFont("monospace", 15)

# sounds
eatsound = pygame.mixer.Sound('sounds/eat sound.wav')
losesound = pygame.mixer.Sound('sounds/lose sound.wav')
music = pygame.mixer.music.load('sounds/bg music.mp3')
pygame.mixer.music.play(-1)

def lost():
    pygame.mixer.music.stop()
    losesound.play()
    global win, running, snake, score
    gameover = font.render("GAME OVER :(", 1, (255, 255, 255))
    playagain = font.render("Play again?", 1, (255, 255, 255))
    yorn = font.render("(y)   (n)", 1, (255, 255, 255))
    win.blit(gameover, (winwidth//2 - 55, winheight//2 - 35))
    win.blit(playagain, (winwidth//2 - 52, winheight//2 - 15))
    win.blit(yorn, (winwidth//2 - 40, winheight//2))
    pygame.display.update()
    pygame.event.clear()
    while True:
        event = pygame.event.wait()
        if event.type == pygame.QUIT:
            running = False
            break
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_y:
                snake = Snake.Snake(0, 20, segsize, segsize-2, segsize-2)
                apple.forbidden = snake.segments
                score = 0
                pygame.mixer.music.play(-1)
                break
            if event.key == pygame.K_n:
                running = False
                break

# mainloop
running = True
score = 0
while running:
    pygame.time.delay(200)
    keypressed = False
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN and not keypressed:
            if event.key == pygame.K_UP and snake.direction != (1, 0):
                snake.direction = (-1, 0)
                keypressed = True
            elif event.key == pygame.K_DOWN and snake.direction != (-1, 0):
                snake.direction = (1, 0)
                keypressed = True
            elif event.key == pygame.K_LEFT and snake.direction != (0, 1):
                snake.direction = (0, -1)
                keypressed = True
            elif event.key == pygame.K_RIGHT and snake.direction != (0, -1):
                snake.direction = (0, 1)
                keypressed = True

    # calculating new position
    tempx = snake.x + snake.vel * snake.direction[1]
    tempy = snake.y + snake.vel * snake.direction[0]
    if 0 <= tempx <= winwidth - snake.segwidth and segsize <= tempy <= winheight - segsize - snake.segheigth:
        snake.x = tempx
        snake.y = tempy
    else:
        #collision with borders
        lost()
        continue

    snake.move()

    # collision with snake
    if snake.segments.count((snake.x, snake.y)) > 1:
        lost()
        continue

    win.fill((0, 0, 0))
    # collision with apple
    if snake.x <= apple.x <= snake.x + segsize and snake.y <= apple.y <= snake.y + segsize:
        eatsound.play()
        snake.addsegment()
        snake.draw(win)
        apple.setposition()
        score += 1
    else:
        snake.draw(win)

    label = font.render("Score {}".format(score), 1, (255, 255, 255))
    win.blit(label, (winwidth - 70, 0))
    apple.draw(win)
    pygame.draw.rect(win, (255, 255, 0), (1, 20, winwidth - 2, winheight - 2*segsize), 1)
    pygame.display.update()

pygame.quit()


# pip install uszipcode
import pandas as pd
from uszipcode import SearchEngine
search = SearchEngine(simple_zipcode=False)

def add_coordinates(df):
    '''Input: a pandas dataframe which includes a 'Zip' field representing a
       US zip code.
       Output: dataframe with 'lat' and 'lng' fields added which represent
       latitude and longitude

    '''
    df['lat'] = df['Zip'].apply(lambda x: pd.Series(search.by_zipcode(x).lat))
    df['lng'] = df['Zip'].apply(lambda x: pd.Series(search.by_zipcode(x).lng))

    df['county'] = df['Zip'].apply(lambda x: pd.Series(search.by_zipcode(x).county))

def add_fips(df):
    '''Input: a pandas dataframe which includes a 'Zip' field representing a
       US zip code.
       Output: dataframe with 'lat' and 'lng' fields added which represent
       latitude and longitude

    '''

    colorado_fips = ['08001', '08003', '08005', '08007', '08009', '08011', '08013', '08014', '08015', '08017', '08019',
                    '08021', '08023', '08025', '08027', '08029', '08031', '08033', '08035', '08037', '08039', '08041',
                    '08043', '08045', '08047', '08049', '08051', '08053', '08055', '08057', '08059', '08061', '08063',
                    '08065', '08067', '08069', '08071', '08073', '08075', '08077', '08079', '08081', '08083', '08085',
                    '08087', '08089', '08091', '08093', '08095', '08097', '08099', '08101', '08103', '08105', '08107',
                    '08109', '08111', '08113', '08115', '08117', '08119', '08121', '08123', '08125']

    colorado_counties = ['Adams County', 'Alamosa County', 'Arapahoe County', 'Archuleta County', 'Baca County', 'Bent County', 'Boulder County', 'Broomfield County',
                         'Chaffee County', 'Cheyenne County', 'Clear Creek County', 'Conejos County', 'Costilla County', 'Crowley County', 'Custer County',
                         'Delta County', 'Denver County', 'Dolores County', 'Douglas County', 'Eagle County', 'Elbert County', 'El Paso County', 'Fremont County',
                         'Garfield County', 'Gilpin County', 'Grand County', 'Gunnison County', 'Hinsdale County', 'Huerfano County', 'Jackson County', 'Jefferson County',
                         'Kiowa County', 'Kit Carson County', 'Lake County', 'La Plata County', 'Larimer County', 'Las Animas County',  'Lincoln County', 'Logan County',
                         'Mesa County', 'Mineral County', 'Moffat County', 'Montezuma County', 'Montrose County', 'Morgan County', 'Otero County', 'Ouray County',
                         'Park County', 'Phillips County', 'Pitkin County', 'Prowers County',  'Pueblo County', 'Rio Blanco County', 'Rio Grande County','Routt County',
                         'Saguache County', 'San Juan County', 'San Miguel County', 'Sedgwick County', 'Summit County', 'Teller County', 'Washington County',
                         'Weld County', 'Yuma County']

    counties_fips_dict = dict(zip(colorado_counties, colorado_fips))

    df['county'] = df['Zip'].apply(lambda x: pd.Series(search.by_zipcode(x).county))
    col_only_df = df[df['county'].isin(colorado_counties)]
    col_only_df['fip'] = col_only_df['county'].apply(lambda x: counties_fips_dict[x])
    return col_only_df


import numpy as np
import tensorflow as tf
from mpl_toolkits import mplot3d
from d2l import tensorflow as d2l

def f(x):
    return x ** 2

def f_grad(x):
    return 2 * x

def gd(eta, f_grad):
    x = 10.0
    results = [x]
    for i in range(10):
        x -= eta * f_grad(x)
        results.append(float(x))
    print(f'Epoch 10, x: {x:f}')
    return results

def show_trace(results, f):
    n = max(abs(min(results)), abs(max(results)))
    f_line = tf.range(-n, n, 0.01)
    d2l.set_figsize()
    d2l.plot([f_line, results], [[f(x) for x in f_line], [
        f(x) for x in results]], 'x', 'f(x)', fmts=['-', '-o'])

def train_2d(trainer, steps=20):
    x1, x2, s1, s2 = -5, -2, 0, 0
    results = [(x1, x2)]
    for i in range(steps):
        x1, x2, s1, s2 = trainer(x1, x2, s1, s2)
        results.append((x1, x2))
    return results

def show_trace_2d(f, results):
    """Show the trace of 2D variables during optimization."""
    d2l.set_figsize()
    d2l.plt.plot(*zip(*results), '-o', color='#ff7f0e')
    x1, x2 = np.meshgrid(np.arange(-5.5, 1.0, 0.1),
                         np.arange(-3.0, 1.0, 0.1))
    d2l.plt.contour(x1, x2, f(x1, x2), colors='#1f77b4')
    d2l.plt.xlabel('x1')
    d2l.plt.ylabel('x2')

f = lambda x1, x2: x1 ** 2 + 2 * x2 ** 2
gradf = lambda x1, x2: (2 * x1, 4 * x2)

def gd(x1, x2, s1, s2):
    (g1, g2) = gradf(x1, x2)
    return (x1 - eta * g1, x2 - eta * g2, 0, 0)

if __name__ == "__main__":
    # results = gd(0.3, f_grad)
    # show_trace(results, f)
    eta = 0.1
    results = train_2d(gd)
    print(results)


import os

import mujoco_py
import numpy as np
from mujoco_py import functions

from learn_seq.utils.general import get_mujoco_model_path

# object indicator in mujoco
MJ_SITE_OBJ = 6  # `site` objec
MJ_BODY_OBJ = 1  # `body` object
MJ_GEOM_OBJ = 5  # `geom` object
# geom types
MJ_CYLINDER = 5
MJ_BOX = 6
MJ_MESH = 7

def load_model(xml_name="round_pih.xml"):
    """Load a model from `mujoco/franka_pih`

    :param type xml_name: Description of parameter `xml_name`.
    :param type primitive: Description of parameter `primitive`.
    :return: Description of returned object.
    :rtype: type

    """
    model_path = get_mujoco_model_path()
    xml_path = os.path.join(model_path, xml_name)
    model = mujoco_py.load_model_from_path(xml_path)
    sim = mujoco_py.MjSim(model)

    return sim

def attach_viewer(sim):
    return mujoco_py.MjViewer(sim)

def set_state(sim, qpos, qvel):
    assert qpos.shape == (sim.model.nq, ) and qvel.shape == (sim.model.nv, )
    old_state = sim.get_state()
    new_state = mujoco_py.MjSimState(old_state.time, qpos, qvel, old_state.act,
                                     old_state.udd_state)
    sim.set_state(new_state)
    sim.forward()

def get_contact_force(mj_model, mj_data, body_name, frame_pos, frame_quat):
    """Get the force acting on a body, with respect to a frame.
    Note that mj_rnePostConstraint should be called before this function
    to update the simulator state.

    :param str body_name: Body name in mujoco xml model.
    :return: force:torque format.
    :rtype: np.array(6)

    """
    bodyId = mujoco_py.functions.mj_name2id(mj_model, MJ_BODY_OBJ, body_name)
    force_com = mj_data.cfrc_ext[bodyId, :]
    # contact force frame
    # orientation is aligned with world frame
    qf = np.array([1, 0, 0, 0.])
    # position of origin in the world frame
    body_rootid = mj_model.body_rootid[bodyId]
    pf = mj_data.subtree_com[body_rootid, :]

    # inverse com frame
    pf_inv, qf_inv = np.zeros(3), np.zeros(4)
    functions.mju_negPose(pf_inv, qf_inv, pf, qf)
    # T^com_target
    p_ct, q_ct = np.zeros(3), np.zeros(4)
    functions.mju_mulPose(p_ct, q_ct, pf_inv, qf_inv, frame_pos, frame_quat)
    # q_ct -> mat
    mat_ct = np.zeros(9)
    functions.mju_quat2Mat(mat_ct, q_ct)

    # transform to desired frame
    trn_force = force_com.copy()
    functions.mju_transformSpatial(trn_force, force_com, 1, p_ct, np.zeros(3),
                                   mat_ct)

    # reverse order to get force:torque format
    return np.concatenate((trn_force[3:], trn_force[:3]))

def get_geom_pose(model, geom_name):
    """Return the geom pose (relative to parent body).

    :param mujoco_py.MjModel model:
    :param str geom_name:
    :return: position, quaternion
    :rtype: tuple(np.array(3), np.array(4))
    """
    geom_id = functions.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    pos = model.geom_pos[geom_id, :]
    quat = model.geom_quat[geom_id, :]
    return pos, quat

def get_geom_size(model, geom_name):
    """Return the geom size.

    :param mujoco_py.MjModel model:
    :param str geom_name:
    :return: (radius, half-length, _) for cylinder geom, and
             (X half-size; Y half-size; Z half-size) for box geom
    :rtype: np.array(3)
    """
    geom_id = functions.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    if model.geom_type[geom_id] == MJ_BOX or model.geom_type[
            geom_id] == MJ_CYLINDER:
        return model.geom_size[geom_id, :]
    else:
        return None

def get_geom_friction(model, geom_name):
    geom_id = functions.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    return model.geom_friction[geom_id, :]

def get_body_mass(model, body_name):
    body_id = functions.mj_name2id(model, MJ_BODY_OBJ, body_name)
    return model.body_mass[body_id]

def get_body_pose(model, body_name):
    body_id = functions.mj_name2id(model, MJ_BODY_OBJ, body_name)
    return model.body_pos[body_id], model.body_quat[body_id]

def get_mesh_vertex_pos(model, geom_name):
    geom_id = functions.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    assert model.geom_type[geom_id] == MJ_MESH
    mesh_id = model.geom_dataid[geom_id]
    first_vertex_id = model.mesh_vertadr[mesh_id]
    no_vertex = model.mesh_vertnum[mesh_id]
    vertex_pos = model.mesh_vert[first_vertex_id:first_vertex_id + no_vertex]
    return vertex_pos

def set_geom_size(model, geom_name, size):
    geom_id = functions.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    model.geom_size[geom_id, :] = size

def set_body_mass(model, body_name, mass):
    body_id = functions.mj_name2id(model, MJ_BODY_OBJ, body_name)
    model.body_mass[body_id] = mass

def set_geom_friction(model, geom_name, friction):
    geom_id = functions.mj_name2id(model, MJ_GEOM_OBJ, geom_name)
    model.geom_friction[geom_id, :] = friction

def set_body_pose(model, body_name, pos, quat):
    body_id = functions.mj_name2id(model, MJ_BODY_OBJ, body_name)
    model.body_pos[body_id, :] = pos
    model.body_quat[body_id, :] = quat

# -------- GEOMETRY TOOLs
def quat_error(q1, q2):
    """Compute the rotation vector (expressed in the base frame), that if follow
        in a unit time, will transform a body with orientation `q1` to
        orientation `q2`

    :param list/np.ndarray q1: Description of parameter `q1`.
    :param list/np.ndarray q2: Description of parameter `q2`.
    :return: a 3D rotation vector
    :rtype: np.ndarray

    """
    if isinstance(q1, list):
        q1 = np.array(q1)

    if isinstance(q2, list):
        q2 = np.array(q2)

    dtype = q1.dtype
    neg_q1 = np.zeros(4, dtype=dtype)
    err_rot_quat = np.zeros(4, dtype=dtype)
    err_rot = np.zeros(3, dtype=dtype)

    if q1.dot(q2) < 0:
        q1 = -q1

    functions.mju_negQuat(neg_q1, q1)
    functions.mju_mulQuat(err_rot_quat, q2, neg_q1)
    functions.mju_quat2Vel(err_rot, err_rot_quat, 1)
    return err_rot

def quat2mat(q):
    """Tranform a quaternion to rotation amtrix.

    :param type q: Description of parameter `q`.
    :return: 3x3 rotation matrix
    :rtype: np.array
    """
    mat = np.zeros(9)
    functions.mju_quat2Mat(mat, q)
    return mat.reshape((3, 3))

def pose_transform(p1, q1, p21, q21):
    """Coordinate transformation between 2 frames

    :param np.ndarray p1: position in frame 1
    :param np.ndarray q1: orientation (quaternion) in frame 1
    :param np.ndarray p21: relative position between frame 1 and 2
    :param np.ndarray q21: relative orientation between frame 1 and 2
    :return: position and orientation in frame 2
    :rtype: type

    """
    # quat to rotation matrix
    R21 = quat2mat(q21)

    p2 = p21 + R21.dot(p1)
    q2 = np.zeros_like(q1)
    functions.mju_mulQuat(q2, q21, q1)  # q2 = q21*q1
    return p2, q2

def integrate_quat(q, r, dt):
    """Integrate quaternion by a fixed angular velocity over the duration dt.

    :param np.array(4) q: quaternion.
    :param np.array(3) r: angular velocity.
    :param float dt: duration.
    :return: result quaternion.
    :rtype: np.array(4)
    """
    qres = np.zeros(4)
    qe = np.zeros(4)
    r = r * dt
    angle = np.linalg.norm(r)
    if angle < 1e-9:
        # if angle too small then return current q
        return q.copy()
    axis = r / angle
    functions.mju_axisAngle2Quat(qe, axis, angle)
    functions.mju_mulQuat(qres, qe, q)
    return qres

def transform_spatial(v1, q21):
    """Coordinate transformation of a spatial vector. The spatial vector can be either
    twist (linear + angular velocity) or wrench (force + torque)

    :param type v1: Spatial vector in frame 1
    :param type q21: transformation matrix (in terms of quaternion)
    :return: Description of returned object.
    :rtype: type
    """
    R21 = quat2mat(q21)
    R = np.block([[R21, np.zeros((3, 3))], [np.zeros((3, 3)), R21]])
    return R.dot(v1)

def similarity_transform(A1, q21):
    """Similarity transformation of a matrix from frame 1 to frame 2
            A2 = R21 * A1 * R12

    :param np.array((3, 3)) A1: 3x3 matrix.
    :param np.array(4) q21: quaternion representation.
    :return: 3x3 matrix
    :rtype: np.array

    """
    R21 = quat2mat(q21)
    return R21.dot(A1.dot(R21.T))

# NOTE: there are infinite rotation vector solutions for a particular
# orientation, the `ref` is to find the closest solution to a reference.
# Is there another minimal representation that could avoid this?
def quat2vec(q, ref=None):
    """Transform quaternion representation to rotation vector representation"""
    r = np.zeros(3)
    scale = 1
    mujoco_py.functions.mju_quat2Vel(r, q, scale)
    if ref is not None:
        if r.dot(ref) < 0:
            angle = np.linalg.norm(r)
            r = r / angle
            angle = angle - 2 * np.pi
            r = r * angle
    return r

def inverse_frame(p, q):
    pi, qi = np.zeros(3), np.zeros(4)
    functions.mju_negPose(pi, qi, p, q)
    return pi, qi

def mat2quat(R):
    R = R.flatten()
    q = np.zeros(4)
    mujoco_py.functions.mju_mat2Quat(q, R)
    return q

def mul_quat(q1, q2):
    q = np.zeros(4)
    mujoco_py.functions.mju_mulQuat(q, q1, q2)
    return q


import pygame
import jumpingHorses.constants as Constants
from .constants import BLACK_PIECE, WHITE_PIECE, MOVE_COLOR, MOVE_RADIUS, SQUARE_SIZE, WIDTH, HEIGHT, LETTER_GAP_SIZE, OUTLINE_SIZE
from .board import Board
from menu.main_menu import MainMenu
import menu.gameState as GameState

class GameMaster:
    #inicializē
    def __init__(self, surface):
        self._init()
        self.surface = surface
    #atjaunina to, kas redzams ekrānā
    def update(self):
        self.board.draw(self.surface)
        if self.selectedPiece != None:
            self.draw_valid_moves(self.valid_moves)
    #sāk no jauna spēli
    def reset(self):
        self._init()
    #notīra esošo stāvokli
    def _init(self):
        self.selectedPiece = None
        self.turn = WHITE_PIECE
        self.board = Board()
        self.valid_moves = {}
    #pārbauda, vai var izvēlēties
    def select(self, pos):
        if LETTER_GAP_SIZE < pos[0] < WIDTH+LETTER_GAP_SIZE and pos[1] < HEIGHT:
            row, col = self.get_row_col_from_mouse(pos)
            if self.selectedPiece:#ja kaut kas jau ir izvēlēts
                result = self._move(row, col)#tad to pabīda, ja ir legāls gājiens
                self.selectedPiece = None
                if not result:#ja neidzodas pabīdīt(izvēlas neiespējamu gājienu, vai kaut ko citu), tad selecto pa jaunam
                    self.selectedPiece = None
                    self.select(pos)
                    return True
            #ja kaut kas tiek izvēlēts pirmo reizi, vai tika izvēlēts kaut kas, kas nebija iespējams gājiens
            piece = self.board.get_piece(row, col)
            if piece != 0 and piece.color == self.turn:
                self.selectedPiece = piece
                self.valid_moves = self.board.get_valid_moves(piece)
                return True
        self.selectedPiece = None
        return False
    #pārvieto kauliņu
    def _move (self, row, col):
        piece = self.board.get_piece(row,col)
        if self.selectedPiece and piece == 0 and (row,col) in self.valid_moves:
            self.board.move(self.selectedPiece, row, col)
            self.change_turn()
        else:
            return False

        return True
    #samaina gājienu
    def change_turn(self):
        self.valid_moves = {}
        if self.turn == BLACK_PIECE:
            self.turn = WHITE_PIECE
        else:
            self.turn = BLACK_PIECE

    #uzzīmē legālos gājienus
    def draw_valid_moves(self, moves):
        pygame.draw.circle(self.surface, (0,255,0), (self.selectedPiece.x, self.selectedPiece.y), self.selectedPiece.radius+OUTLINE_SIZE, 5)
        for move in moves:
            row, col = move
            pygame.draw.circle(self.surface, MOVE_COLOR, (col*SQUARE_SIZE + SQUARE_SIZE//2, row*SQUARE_SIZE + SQUARE_SIZE//2) , MOVE_RADIUS)

    #atgriež rindu un kolonnu atkarībā no peles pozīcijas
    def get_row_col_from_mouse(self, pos):
        x, y = pos
        row = y // SQUARE_SIZE
        col = (x - LETTER_GAP_SIZE) // SQUARE_SIZE
        return row, col
    #dators veic gājienu
    def ai_move(self, board):
        self.board = board
        self.change_turn()
    #pārbauda, vai ir uzvarētājs
    def check_winner(self):
        if self.board.winner() == Constants.starting_player[1]:
            GameState.currentState = GameState.State.win
        elif self.board.winner() == Constants.starting_player[2]:
            GameState.currentState = GameState.State.lost

    def get_board(self):
        #print("get_board called")
        #self.board.print_board()
        return self.board


# -*- coding:utf-8 -*-
# 这道题对于Python来说似乎没有意义
# 在Python中字符串类型是不可变类型，即不可以在原处修改
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        s = s.replace(' ', '%20')
        return s
if __name__=='__main__':
    s = Solution()
    string = "we are happy"
    print(s.replaceSpace(string))


from absl.testing import absltest
from absl.testing import parameterized
import more_itertools
import tensorflow as tf
from uncertainty_baselines.datasets import datasets
import data_preprocessor  # local file import from experimental.language_structure.vrnn
import data_utils  # local file import from experimental.language_structure.vrnn
import utils  # local file import from experimental.language_structure.vrnn

INPUT_ID_NAME = data_preprocessor.INPUT_ID_NAME
INPUT_MASK_NAME = data_preprocessor.INPUT_MASK_NAME
DIAL_TURN_ID_NAME = data_preprocessor.DIAL_TURN_ID_NAME

class DataPreprocessorTest(parameterized.TestCase):

  def setUp(self):
    super().setUp()
    self.batch_size = 2

  def create_data_preprocessor(self, max_seq_length, **kwargs):
    del max_seq_length  # unused
    return data_preprocessor.DataPreprocessor(**kwargs)

  def load_dataset(self, dataset_name):
    dataset_builder = datasets.get(
        dataset_name, split='test', add_dialog_turn_id=True)
    return dataset_builder.load(batch_size=self.batch_size).prefetch(1)

  @parameterized.named_parameters(('multiwoz_synth', 'multiwoz_synth'),
                                  ('simdial', 'simdial'),
                                  ('sgd_synth', 'sgd_synth'))
  def test_output_shape(self, dataset_name):
    dataset = self.load_dataset(dataset_name)
    dialog_length = data_utils.get_dataset_max_dialog_length(dataset_name)
    seq_length = data_utils.get_dataset_max_seq_length(dataset_name)
    num_states = data_utils.get_dataset_num_latent_states(dataset_name)

    preprocessor = self.create_data_preprocessor(
        seq_length, num_states=num_states)
    dataset = dataset.map(preprocessor.create_feature_and_label)
    (input_1, input_2, label, label_mask, initial_state, initial_sample,
     domain_label) = more_itertools.first(dataset)

    for inputs in [input_1, input_2]:
      for key in [INPUT_ID_NAME, INPUT_MASK_NAME]:
        self.assertEqual([self.batch_size, dialog_length, seq_length],
                         inputs[key].shape.as_list())

    for inputs in [label, label_mask, domain_label]:
      self.assertEqual([self.batch_size, dialog_length], inputs.shape.as_list())

    for inputs in [initial_state, initial_sample]:
      self.assertEqual([self.batch_size, num_states], inputs.shape.as_list())

  @parameterized.named_parameters(('multiwoz_synth', 'multiwoz_synth'),
                                  ('simdial', 'simdial'),
                                  ('sgd_synth', 'sgd_synth'))
  def test_label_mask_by_dialog_turn_ids(self, dataset_name):
    dataset = self.load_dataset(dataset_name)
    inputs = more_itertools.first(dataset)
    dialog_turn_id_indices = [(0, 2), (1, 3), (1, 5)]
    dialog_turn_ids = tf.gather_nd(inputs[DIAL_TURN_ID_NAME],
                                   dialog_turn_id_indices)
    seq_length = data_utils.get_dataset_max_seq_length(dataset_name)
    num_states = data_utils.get_dataset_num_latent_states(dataset_name)

    preprocessor = self.create_data_preprocessor(
        seq_length,
        num_states=num_states,
        labeled_dialog_turn_ids=dialog_turn_ids)
    dataset = dataset.map(preprocessor.create_feature_and_label)
    (_, _, _, label_mask, _, _, _) = more_itertools.first(dataset)

    for i, row in enumerate(label_mask.numpy()):
      for j, val in enumerate(row):
        if (i, j) in dialog_turn_id_indices:
          self.assertEqual(val, 1)
        else:
          self.assertEqual(val, 0)

class BertDataPreprocessorTest(DataPreprocessorTest):

  def create_data_preprocessor(self, max_seq_length, **kwargs):
    preprocess_tfhub_url = 'https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3'
    bert_preprocess_model = utils.BertPreprocessor(preprocess_tfhub_url,
                                                   max_seq_length)
    return data_preprocessor.BertDataPreprocessor(bert_preprocess_model,
                                                  **kwargs)

if __name__ == '__main__':
  absltest.main()


import collections
class Solution:
    def findLucky(self, arr) :
        count = collections.Counter(arr)
        max_count = -1
        for key in count :
            if key == count[key]:
                max_count = max(max_count, key)
        return max_count

s = Solution()
print(s.findLucky([4,2,3]))

'''
using dictionary : takes longer

sorting
[2,3,4] ??

counter method

>>> array = [1,2,2,3,3,3]
>>> Counter(array)
Counter({3: 3, 2: 2, 1: 1})
>>> c = Counter(array)
>>> c.keys()
[1, 2, 3]
>>> c.values()
[1, 2, 3]
>>> c[3]
3
'''


# 1. UPDATE VALUES IN DICTIONARIES AND LISTS
x = [ [5,2,3], [10,8,9] ]
students = [
     {'first_name':  'Michael', 'last_name' : 'Jordan'},
     {'first_name' : 'John', 'last_name' : 'Rosales'}
]
sports_directory = {
    'basketball' : ['Kobe', 'Jordan', 'James', 'Curry'],
    'soccer' : ['Messi', 'Ronaldo', 'Rooney']
}
z = [ {'x': 10, 'y': 20} ]

def update_values(x, students, sports_directory, z):
    # CHANGE THE VALUE 10 IN X TO BE 15
    x[1][0] = 15
    #CHANGE LAST NAME OF THE FIRST STUDENT
    students[0]['last_name'] = 'Bryant'

    sports_directory['soccer'][0] = 'Andres'

    z[0]['y'] = 30

update_values(x, students, sports_directory, z)

#2 ITERATE THROUGH A LIST OF DICTIONARIES
students = [
         {'first_name':  'Michael', 'last_name' : 'Jordan'},
         {'first_name' : 'John', 'last_name' : 'Rosales'},
         {'first_name' : 'Mark', 'last_name' : 'Guillen'},
         {'first_name' : 'KB', 'last_name' : 'Tonel'}
    ]
def iterate_dictionary(student):
# LOOP THROUGH THE LIST
    print_string = ""
    for i in range(len(student)):
    # PRINT THE KEYS AND VALUES
        for key in student[i]:
            print_string += f"{key} - {student[i][key]}"
    print(print_string)
    print_string = ""
iterate_dictionary(students)

#3 GET VALUES FROM A LIST OF DICTIONARIES
def iterate_dictionary2(key_value, student):
    for i in range(len(student)):
        if key_value not in student[i]:
            print("Key is not in dictionary")
        else:
            print(student[i][key_value])

iterate_dictionary2('first_name', students)
iterate_dictionary2('last_name', students)

#4
dojo = {
   'locations': ['San Jose', 'Seattle', 'Dallas', 'Chicago', 'Tulsa', 'DC', 'Burbank'],
   'instructors': ['Michael', 'Amy', 'Eduardo', 'Josh', 'Graham', 'Patrick', 'Minh', 'Devon']
}

def print_info(some_dict):
    #LOOP THROUGH THE DICTIONARY
    for key in some_dict:
        #PRINT LENGTH OF THE LIST AND THE KEY
        print(f"{len(some_dict[key])} {key.upper()}")
        #LOOP TO PRINT EACH ITEM IN THE LIST
        for i in range(len(some_dict[key])):
            print(some_dict[key][i])
print_info(dojo)


import typing
from datetime import datetime

from discord import Activity, ActivityType, Embed, Game, HTTPException, Status, Member, User, TextChannel, VoiceChannel, Role, Invite, Game, Emoji, PartialEmoji, Colour
from discord.ext import commands

from discordbot.botmodules import serverdata, audio
from discordbot.config import EXTENSIONFOLDER, EXTENSIONS, ALL_PREFIXES, MAIN_PREFIXES, DEBUG
from discordbot.utils import chunks
from discordbot.errors import ErrorMessage

from rich.traceback import install as install_traceback
install_traceback()
from rich.pretty import install as install_pretty
install_pretty()

#

CONVERTERS = {
    Member: commands.MemberConverter,
    User: commands.UserConverter,
    TextChannel: commands.TextChannelConverter,
    VoiceChannel: commands.VoiceChannelConverter,
    Role: commands.RoleConverter,
    Invite: commands.InviteConverter,
    Game: commands.GameConverter,
    Emoji: commands.EmojiConverter,
    PartialEmoji: commands.PartialEmojiConverter,
    Colour: commands.ColourConverter
}

# Own classes

class MyContext(commands.Context):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.database = serverdata.DjangoConnection(self.author, self.guild)
        self.audio = audio.AudioManager(self)

        if self.guild is not None:
            self.data = serverdata.Server.getServer(self.guild.id)

    async def sendEmbed(self, title: str, *args, receiver=None, message: str = "", description: str = "", fields: list = [], **kwargs):
        if len(description) > 2048:
            desc = list(chunks(description, 2042))
            for i in range(len(desc)):
                if i == 0:
                    await (receiver or self).send(message, embed=self.getEmbed(f"{title} ({i+1}/{len(desc)})", *args, description=desc[i]+" [...]", fields=fields, **kwargs))
                elif i == len(desc)-1:
                    return await (receiver or self).send(embed=self.getEmbed(f"{title} ({i+1}/{len(desc)})", *args, description=desc[i], **kwargs))
                else:
                    await (receiver or self).send(embed=self.getEmbed(f"{title} ({i+1}/{len(desc)})", *args, description=desc[i]+" [...]", **kwargs))
        elif len(fields) > 25:
            flds = list(chunks(fields, 25))
            for i in range(len(flds)):
                if i == 0:
                    await (receiver or self).send(message, embed=self.getEmbed(f"{title} ({i+1}/{len(flds)})", *args, description=description, fields=flds[i], **kwargs))
                elif i == len(flds)-1:
                    return await (receiver or self).send(embed=self.getEmbed(f"{title} ({i+1}/{len(flds)})", *args, fields=flds[i], **kwargs))
                else:
                    await (receiver or self).send(embed=self.getEmbed(f"{title} ({i+1}/{len(flds)})", *args, fields=flds[i], **kwargs))
        else:
            return await (receiver or self).send(message, embed=self.getEmbed(title=title, *args, description=description, fields=fields, **kwargs))

    def getEmbed(self, title:str, description:str="", color:int=0x000000, fields:list=[], inline=True, thumbnailurl:str=None, authorurl:str="", authorname:str=None, footertext:str="Angefordert von USER", footerurl:str="AVATARURL", timestamp=False):
        EMBED = Embed(title=title[:256], description=description[:2048], color=color or getattr(self.cog, "color", 0x000000))
        EMBED.set_footer(text=footertext.replace("USER", str(self.author.name+"#"+self.author.discriminator))[:2048], icon_url=footerurl.replace("AVATARURL", str(self.author.avatar_url)))

        if timestamp:
            EMBED.timestamp = datetime.utcnow() if timestamp is True else timestamp
        for field in fields[:25]:
            EMBED.add_field(name=field[0][:256], value=(field[1][:1018]+" [...]" if len(field[1]) > 1024 else field[1]), inline=bool(field[2] if len(field) > 2 else inline))
        if thumbnailurl:
            EMBED.set_thumbnail(url=thumbnailurl.strip())
        if authorname:
            if authorurl and ("https://" in authorurl or "http://" in authorurl):
                EMBED.set_author(name=authorname[:256], url=authorurl.strip())
            else:
                EMBED.set_author(name=authorname[:256])
        return EMBED

    async def tick(self, value=True):
        emoji = '\N{WHITE HEAVY CHECK MARK}' if value else '\N{CROSS MARK}'
        try:
            await self.message.add_reaction(emoji)
        except HTTPException:
            pass

    async def send_help(self):
        await self.invoke(self.bot.get_command("help"), self.invoked_with)

    async def invoke_as(self, member, command, *args):
        _command = command.replace("_", " ")
        cmd = self.bot.get_command(_command)
        if cmd is None:
            raise ErrorMessage(f"Der Befehl `{ _command }` wurde nicht gefunden! \nPS: Benutze im Command bitte kein Prefix! Für Subcommands, benutze command_subcommand.")
        self.message.content = self.prefix+_command+self.message.content.split(command)[1]
        self.message.author = member
        self.author = member
        self.database = type(self.database)(self.author, self.guild)
        annotations = cmd.callback.__annotations__
        annotations.pop("return", None)
        arguments = list(args)
        for i, cls in enumerate(annotations.values()):
            if len(arguments) > i:
                if cls in CONVERTERS:
                    arguments[i] = await CONVERTERS[cls]().convert(self, arguments[i])
                else:
                    arguments[i] = cls(arguments[i])
        await self.invoke(cmd, *arguments)

class MyBot(commands.Bot):
    def __init__(self, **kwargs):
        super().__init__(self.get_command_prefix, **kwargs)

    def get_command_prefix(self, client, message):
        if message.guild:
            prefixes = MAIN_PREFIXES
        else:
            prefixes = ALL_PREFIXES
        return commands.when_mentioned_or(*prefixes)(client, message)

    async def get_context(self, message, *, cls=MyContext):
        return await super().get_context(message, cls=cls)

    def getEmbed(self, title: str, description: str = "", color: int = 0x000000, fields: list = [], inline=True, thumbnailurl: str = None, authorurl: str = "", authorname: str = None, footertext: str = None, footerurl: str = None, timestamp=False):
        EMBED = Embed(title=title[:256], description=description[:2048], color=color)
        if footertext:
            if footerurl:
                EMBED.set_footer(text=footertext[:2048], icon_url=footerurl)
            else:
                EMBED.set_footer(text=footertext[:2048])

        if timestamp:
            EMBED.timestamp = datetime.utcnow() if timestamp is True else timestamp
        for field in fields:
            EMBED.add_field(name=field[0][:256], value=(field[1][:1018]+" [...]" if len(field[1]) > 1024 else field[1]), inline=bool(
                field[2] if len(field) > 2 else inline))
        if thumbnailurl:
            EMBED.set_thumbnail(url=thumbnailurl.strip())
        if authorname:
            if authorurl and ("https://" in authorurl or "http://" in authorurl):
                EMBED.set_author(name=authorname[:256], url=authorurl.strip())
            else:
                EMBED.set_author(name=authorname[:256])
        return EMBED

# create Bot

bot = MyBot(
    description='Das ist eine Beschreibung!',
    case_insensitive=True,
    activity=Activity(type=ActivityType.listening, name=(MAIN_PREFIXES[0] if MAIN_PREFIXES else "/")+"help"),
    status=Status.idle,
    help_command=None,
    strip_after_prefix=True,
)

@bot.before_invoke
async def before_invoke(ctx):
    await ctx.trigger_typing()

# Events

from discordbot.botevents import setup

setup(bot)

# Start

def run(TOKEN):
    print("[Bot] - Starting with DEBUG="+str(DEBUG))
    bot.run(TOKEN, bot=True, reconnect=True)

if __name__ == "__main__":
    print("[Bot] - You must run this bot via your manage.py file: python3.8 manage.py run-discorbot")


from flask import Flask
from bitrix24 import *
import pycep_correios

app = Flask(__name__)

@app.route('/<id>', methods=['POST','GET'])

def cep(id):
    bx24 = Bitrix24('https://megaisencoes.bitrix24.com.br/rest/XXXX/XXXXXXX/')
    dealId = id

    chamada= bx24.callMethod("crm.deal.get", id=dealId)
    cep1=chamada.get('UF_CRM_5DF0204B5D798')
    contactId = chamada.get('CONTACT_ID')
    complemento = chamada.get('UF_CRM_5DF0204B50C64')
    numero= chamada.get('UF_CRM_5DF0204B42F73')
    cpf = chamada.get('UF_CRM_5DF0204BA9076')
    rg = chamada.get('UF_CRM_5DF0204BB3CD4')
    cep = chr_remove(cep1, ' -.,')

    endereco = pycep_correios.get_address_from_cep(cep)
    bairro = endereco['bairro']
    cidade = endereco['cidade']
    rua = endereco['logradouro']
    uf = endereco['uf']
    cep = endereco['cep']

    cep2 = cep[0:5]
    zona = getZona(int(cep2))

    bx24.callMethod("crm.deal.update", id=dealId, fields={'UF_CRM_1606240753':zona,'UF_CRM_1606228463':uf, 'UF_CRM_5DF0204B93074':bairro,'UF_CRM_5E18F32827B32':rua,'UF_CRM_5DF0204B68A91':cidade,'UF_CRM_5DF0204B5D798':cep})
    bx24.callMethod("crm.contact.update", id=contactId, fields={'UF_CRM_5DE6A1384D99D':complemento,'UF_CRM_5DE6A1384016A':numero,'UF_CRM_1575396704':rg,'UF_CRM_1575396694':cpf,'UF_CRM_1606395844':uf, 'UF_CRM_5DE6A139AD7B0':bairro,'UF_CRM_5E2F1DAA04C4B':rua,'UF_CRM_5DE6A13867AD5':cidade,'UF_CRM_5DE6A1385B8FC':cep})
    return '
Endereço preenchido
'
def chr_remove(old, to_remove):
    new_string = old
    for x in to_remove:
        new_string = new_string.replace(x, '')
    return new_string

def getZona(cep):
    if cep > 1000 and cep < 1599:
        return "Centro"
    elif cep > 2000 and cep < 2999:
        return "Zona Norte"
    elif cep > 3000 and cep < 3999 or (cep > 8000 and cep < 8499):
        return "Zona Leste"
    elif cep > 4000 and cep < 4999:
        return "Zona Sul"
    elif cep > 5000 and cep < 5899:
        return "Zona Oeste"
    else:
        return "Cep não pertence a cidade de São Paulo"

if __name__ == '__main__':
    app.run(debug=True)


#!/usr/bin/env python
# coding: utf-8

# In[63]:

from PIL import Image, ImageDraw
import numpy as np
import math
import imageio
from copy import deepcopy
import cv2

def to_integral_image(img_arr):
    """
    Calculates the integral image based on this instance's original image data.
    """
    row_sum = np.zeros(img_arr.shape)
    # we need an additional column and row of padding zeros
    integral_image_arr = np.zeros((img_arr.shape[0] + 1, img_arr.shape[1] + 1))
    for x in range(img_arr.shape[1]):
        for y in range(img_arr.shape[0]):
            row_sum[y, x] = row_sum[y-1, x] + img_arr[y, x]
            integral_image_arr[y+1, x+1] = integral_image_arr[y+1, x-1+1] + row_sum[y, x]
    return integral_image_arr

def sum_region(integral_img_arr, top_left, bottom_right):
    """
    Calculates the sum in the rectangle specified by the given tuples.
    """
    top_left=(top_left[0]-1,top_left[1]-1)
    top_right = (bottom_right[0], top_left[1])
    bottom_left = (top_left[0], bottom_right[1])
    return integral_img_arr[bottom_right] - integral_img_arr[top_right] - integral_img_arr[bottom_left] + integral_img_arr[top_left]

class HaarFeature(object):
    """
    Class representing a haar-like feature.
    """

    def __init__(self, feature_type, top_left, bottom_right, threshold, polarity, error, weight, flag):
        """
        Creates a new haar-like feature with relevant attributes.
        """
        self.type = feature_type
        self.top_left = top_left
        self.bottom_right = bottom_right
        self.width = bottom_right[0]-top_left[0]
        self.height = bottom_right[1]-top_left[1]
        self.threshold = threshold
        self.polarity = polarity
        self.error=error
        self.weight=weight
        self.flag=flag

    def get_score(self, int_img):
        """
        Get score for given integral image array.
        """
        score = 0
        if self.type == (1,2):
            first = sum_region(int_img, self.top_left, (self.top_left[0] + self.width, int(self.top_left[1] + self.height / 2)))
            second = sum_region(int_img, (self.top_left[0], int(self.top_left[1] + self.height / 2)), self.bottom_right)
            score = first - second
        elif self.type == (2,1):
            first = sum_region(int_img, self.top_left, (int(self.top_left[0] + self.width / 2), self.top_left[1] + self.height))
            second = sum_region(int_img, (int(self.top_left[0] + self.width / 2), self.top_left[1]), self.bottom_right)
            score = first - second
        return score

    def get_vote(self, int_img):
        """
        Get vote of this feature for given integral image, the vote is 1 or -1.
        """
        score = self.get_score(int_img)
        return 1 if self.polarity * (score-self.threshold) >= 0 else -1

#helper function to sum positive numbers in an array
def sum_positive(array):
    s=0
    l=len(array)
    for i in range(l):
        if array[i]>0:
            s=s+array[i]
    return s
#helper function to sum negative numbers in an array
def sum_negative(array):
    s=0
    l=len(array)
    for i in range(l):
        if array[i]<0:
            s=s+array[i]
    return s

#given an array of lables and weights of each image (label), find the threshold for this weaker learner
def find_threshold(array, weights):
    index=1
    p=1
    l=len(array)
    output_error=1
    temp=np.multiply(array,weights)
    Lp=0
    Ln=0
    Rp=sum_positive(temp)
    Rn=sum_negative(temp)
    #try every index
    for i in range(1,l):
        t=temp[i]
        if t>0:
            Lp=Lp+t
            Rp=Rp-t
        else:
            Ln=Ln+t
            Rn=Rn-t
        error=min(Lp+abs(Rn),abs(Ln)+Rp)
        if error < output_error:
            output_error=error
            index = i
            if Lp+abs(Rn) < Rp+abs(Ln):
                p=1
            else:
                p=-1
    #return the best polarity, the index of the image (whose score will be the threshold),
    #and the error of this weak learner
    return (index,p,output_error)

def learn(features, images, labels, weights):
    """
    This is the mean funtion we use, every time we feed the images, labels, features,
    and weigts of the images, it will output the current best weaklearner and set the
    parameters.
    """
    # select classifiers
    lenf = len(features)
    leni = len(labels)
    fi=0
    min_error=1
    for i in range(lenf):
        temp=np.zeros((leni,3))
        for j in range(leni):
            img=images[j]
            x=features[i].get_score(img)
            y=labels[j]
            temp[j][0]=x
            temp[j][1]=y
            temp[j][2]=weights[j]
        temp=temp[temp[:,0].argsort()]
        #get the labels and weights we need to find the threshold for this feature
        tup=find_threshold(temp[:,1],temp[:,2])
        index=tup[0]
        features[i].threshold=temp[index][0]
        features[i].polarity=tup[1]
        error=tup[2]
        #to record the best feature
        if (error < min_error) and (features[i].flag==0):
            min_error=error
            fi=i

    # already find the best feature, update its parameters
    # flag indicates whether this feature has already been picked before
    features[fi].flag=1
    features[fi].error=min_error
    # find the weight of this chosen weak learner
    if min_error>0:
        z=2*(min_error*(1-min_error)) ** (1/2)
        a = 0.5 * np.log((1 - min_error) / min_error)
    else:
        a=2
        z=0
    features[fi].weight=a
    # update the weights of the data (images)
    if z!=0:
        for i in range(leni):
            vote=features[i].get_vote(images[i])
            weights[i]=weights[i]*math.exp(-a*labels[i]*vote)/z
    # normalize the weights
    s=np.sum(weights)
    weights=weights/s
    return (fi,weights)

# This generates all the features we need
all_features=[]
for i in range(1,65,3):
    for j in range(1,65,3):
        m1=min(i+16,65)
        m2=min(j+16,65)
        for k1 in range(i,m1,4):
            for h1 in range(j+3,m2,4):
                f = HaarFeature((1,2),(i,j),(k1,h1),0,1,0,0,0)
                all_features.append(f)
        for k2 in range(i+3,m1,4):
            for h2 in range(j,m2,4):
                f = HaarFeature((2,1),(i,j),(k2,h2),0,1,0,0,0)
                all_features.append(f)

# given a classifier, this function outputs whether an image is a face or not according to this classifier
def test(img, classifiers, threshold=3):
    l=len(classifiers)
    s=0
    for i in range(l):
        s=s+classifiers[i].weight*classifiers[i].get_vote(img)
    if s>=threshold:
        return (1,s)
    return (-1,s)

# load the images
images=[]
labels=[]
for i in range (1000):
    j=i
    j=str(j)
    im = imageio.imread('Downloads/faces/face'+j+'.jpg')
    im=np.array(im).mean(axis=2)
    im=to_integral_image(im)
    images.append(im)
    labels.append(1)
for i in range (1000):
    j=i
    j=str(j)
    im = imageio.imread('Downloads/background/'+j+'.jpg')
    im=np.array(im).mean(axis=2)
    im=to_integral_image(im)
    images.append(im)
    labels.append(-1)

cascade=[]
thres=[]
iter=0
for j in range(6):
    classifiers=[]
    for i in range(5):
        l=len(images)
        weights=np.full(l,1/l)
        t = learn(all_features, images, labels, weights)
        p=t[0]
        weights=t[1]
        classifiers.append(all_features[p])
    l=len(images)
    mini=0
    # set the capital Theta to make sure we correctly classify all faces
    for i in range(l):
        t=test(images[i],classifiers)
        if labels[i]==1:
            if t[1]<mini:
                mini=t[1]
    thres.append(deepcopy(mini))
    cascade.append(deepcopy(classifiers))
    # elimiate the images we correctly classify as backgrounds
    images_temp=[]
    labels_temp=[]
    for i in range(l):
        t=test(images[i],classifiers,mini)
        if (labels[i]!=t[0]) or (labels[i]==1):
            images_temp.append(images[i])
            labels_temp.append(labels[i])
    images=deepcopy(images_temp)
    labels=deepcopy(labels_temp)
    # deal with the situation is before we use up all 5 classifiers, the error has already been 0
    iter=iter+1
    if len(images)<2:
        break

def get_v(cascade, int_img):
        """
        helper funtion to avoid overlapping of red patches detecting faces
        """
        l=len(cascade)
        s=0
        for i in range(l):
            score = cascade[i].get_score(int_img)
            ab=abs(cascade[i].polarity * (score-cascade[i].threshold))
            s=s+cascade[i].weight*ab
        return s

#slide our cascade to detect faces in the test image
coor=np.zeros((1280,1600))
im1=Image.open('Downloads/test_img.jpg')
#add chanels to the black&white test image, so we can draw "red sqaures" on it
im1=cv2.cvtColor(np.array(im1),cv2.COLOR_GRAY2RGB)
img=Image.fromarray(im1, 'RGB')
im2=Image.open('Downloads/test_img.jpg')
imay=np.array(im2)
draw=ImageDraw.Draw(img)
# determine where we shoud place our pateches
for i in range(0,1216,4):
    for j in range(0,1536,4):
        y=to_integral_image(imay[i:i+64,j:j+64])
        flag=0
        for k in range(iter):
            t=test(y,cascade[k])
            if t[0]==1:
                flag=flag+1
        if (flag>=6):
            v=0
            for k in range(iter):
                v=v+get_v(cascade[k],y)
            coor[i,j]=v
# avoid overlapping
for i in range(0,1216,4):
    for j in range(0,1536,4):
        ff=1
        for z1 in range(-7,8):
            for z2 in range(-7,8):
                if coor[i,j]<coor[i-4*z1,j-4*z2]:
                    ff=0
        if (ff==1) and coor[i,j]>0:
            draw.line([(j,i),(j,i+64)],fill='red')
            draw.line([(j,i),(j+64,i)],fill='red')
            draw.line([(j+64,i),(j+64,i+64)],fill='red')
            draw.line([(j,i+64),(j+64,i+64)],fill='red')
img.save('Downloads/result4.jpg')


import numpy as np

def compute_errors(gt, pred):
    """Computation of error metrics between predicted and ground truth depths
        Taken from Beyond Image to Depth Github repository
    """
    # Select only the values that are greater than zero
    mask = gt > 0
    pred = pred[mask]
    gt = gt[mask]

    thresh = np.maximum((gt / pred), (pred / gt))
    a1 = (thresh < 1.25     ).mean()
    a2 = (thresh < 1.25 ** 2).mean()
    a3 = (thresh < 1.25 ** 3).mean()

    rmse = (gt - pred) ** 2
    rmse = np.sqrt(rmse.mean())
    if rmse != rmse:
        rmse = 0.0
    if a1 != a1:
        a1=0.0
    if a2 != a2:
        a2=0.0
    if a3 != a3:
        a3=0.0

    abs_rel = np.mean(np.abs(gt - pred) / gt)
    log_10 = (np.abs(np.log10(gt)-np.log10(pred))).mean()
    mae = (np.abs(gt-pred)).mean()
    if abs_rel != abs_rel:
        abs_rel=0.0
    if log_10 != log_10:
        log_10=0.0
    if mae != mae:
        mae=0.0

    return abs_rel, rmse, a1, a2, a3, log_10, mae


import os

import anndata

import scanpy as sc

from matplotlib import rcParams

import sccross

rcParams["figure.figsize"] = (4, 4)

PATH = "s01_preprocessing"
os.makedirs(PATH, exist_ok=True)

rna = anndata.read_h5ad("Saunders-2018.h5ad")

met = anndata.read_h5ad("Luo-2017.h5ad")

atac = anndata.read_h5ad("10x-ATAC-Brain5k.h5ad")

rna.layers["raw_count"] = rna.X.copy()
sc.pp.normalize_total(rna)
sc.pp.log1p(rna)
sc.pp.scale(rna, max_value=10)
sc.tl.pca(rna, n_comps=100, use_highly_variable=True, svd_solver="auto")

rna.X = rna.layers["raw_count"]
del rna.layers["raw_count"]

sc.pp.neighbors(rna, n_pcs=100, metric="cosine")
sc.tl.umap(rna)

rna.obs["cell_type"].cat.set_categories([
    "Layer2/3", "Layer5a", "Layer5", "Layer5b", "Layer6",
    "Claustrum", "CGE", "MGE"
], inplace=True)

met.X = met.layers["norm"].copy()
sc.pp.log1p(met)
sc.pp.scale(met, max_value=10)
sc.tl.pca(met, n_comps=100, use_highly_variable=True, svd_solver="auto")

met.X = met.layers["norm"]
del met.layers["norm"]

sc.pp.neighbors(met, n_pcs=100, metric="cosine")
sc.tl.umap(met)

met.obs["cell_type"].cat.set_categories([
    "mL2/3", "mL4", "mL5-1", "mDL-1", "mDL-2", "mL5-2",
    "mL6-1", "mL6-2", "mDL-3", "mIn-1", "mVip",
    "mNdnf-1", "mNdnf-2", "mPv", "mSst-1", "mSst-2"
], inplace=True)

sccross.data.lsi(atac, n_components=100, use_highly_variable=False, n_iter=15)

sc.pp.neighbors(atac, n_pcs=100, use_rep="X_lsi", metric="cosine")
sc.tl.umap(atac)

atac.obs["cell_type"].cat.set_categories([
    "L2/3 IT", "L4", "L5 IT", "L6 IT", "L5 PT",
    "NP", "L6 CT", "Vip", "Pvalb", "Sst"
], inplace=True)

fig = sc.pl.umap(atac, color="cell_type", title="scATAC-seq cell type", return_fig=True)
fig.savefig(f"{PATH}/atac_ct.pdf")

atac2rna = sccross.data.geneActivity(atac)

rna.write("rna_preprocessed.h5ad", compression="gzip")
met.write("met_preprocessed.h5ad", compression="gzip")
atac.write("atac_preprocessed.h5ad", compression="gzip")
atac2rna.write("atac2rna.h5ad", compression="gzip")


# Given files of user data scrapped from Yelp on different dates
# Merge the files so that each row represents one user
# and his/her elite status on each date
# For example, Jan 4: Not Elite, Jan 5: Not Elite, Jan 6: Elite
# Find on what date the user's status changed to Elite
import csv
import copy
def combineCityEliteFiles(in_filename1, in_filename2, out_filename, newDate):
    with open(in_filename1, 'rU') as in_f1,\
    open(in_filename2, 'rU') as in_f2, open(out_filename, 'wb') as out_f:
        dataReader1 = csv.reader(in_f1)
        dataReader2 = csv.reader(in_f2) #errorFile higher priority
        originalList = list(dataReader1)
        errorList = list(dataReader2)
        dataWriter = csv.writer(out_f)
        indexUserURL = -5
        indexEliteFlag = -6
        errorDict = {}
        errorLineDict = {}
        for errorLine in errorList:
            userURL = errorLine[indexUserURL]
            eliteFlag = errorLine[indexEliteFlag]
            errorDict[userURL] = eliteFlag
            errorLineDict[userURL] = errorLine

        copyErrorDict = copy.deepcopy(errorDict)

        for line1 in originalList:
            userURL = line1[indexUserURL]
            newEliteFlag = errorDict.get(userURL, None)
            if "property" in line1[indexUserURL]:
                dataWriter.writerow([newDate] + line1)
                continue
            if newEliteFlag == None:
                eliteFlagMissing = "NoData"
                dataWriter.writerow([eliteFlagMissing]+line1)
            else:
                dataWriter.writerow([newEliteFlag]+ line1)
                copyErrorDict.pop(userURL, None)

        if len(copyErrorDict)!=0:
            numberOfEntries = len(originalList[0])
            numberOfOthers = 5
            numberOfEliteDataMissing = numberOfEntries-numberOfOthers
            for errorUserURL in copyErrorDict.keys():
                errorLine = errorLineDict[errorUserURL]
                errorEliteFlag = errorDict[errorUserURL]
                if "property" in errorLine[indexUserURL]:
                    continue
                newLine = [errorEliteFlag]+['Missing']*numberOfEliteDataMissing+\
                      errorLine[-5:]
                dataWriter.writerow(newLine)
    return

def main():
    combineCityEliteFiles("mergeJan91011Jan12.csv","Jan8/EliteFlag/cityLabelCombinedEliteFlagJan 8 - Tampa to Cleveland.csv", "mergeJan891011Jan12.csv","Jan8")
    combineCityEliteFiles("mergeJan891011Jan12.csv","Jan5/EliteFlag/cityLabelCombinedEliteFlagJan 5 - Tampa to Cleveland.csv", "mergeJan5891011Jan12.csv","Jan5")
    combineCityEliteFiles("mergeJan5891011Jan12.csv","Jan4/EliteFlag/cityLabelCombinedEliteFlagJan 4 - Tampa to Cleveland.csv", "mergeJan45891011Jan12.csv","Jan4")

if __name__ == "__main__":
    main()


import pytest

@pytest.mark.asyncio
async def test_healthcheck(test_client_rest):
    response = await test_client_rest.get("http://test/healthcheck")

    assert response.status_code == 200
    data = response.json()
    assert data == {"status": "ok"}


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import numpy as np
import math

if __name__ == '__main__':
    SZ = 64  # taille de l'image
    SP = int(SZ / 2)  # Coordonnee max/min
    im = np.zeros((SZ, SZ), np.uint8)  # Image comme un tableau
    all_i = range(-SP, SP)
    all_j = range(-SP, SP)

    # fp periode sur une image
    fp = 4.0  # 1.0;2.0;4.0;8.0;16.0
    fr = fp / SZ  # frequence reduite
    for i in all_i:
        for j in all_j:
            im[i, j] = 128 + 128 * math.sin(2 * 3.14 * fr * i)

    plt.figure(1)
    plt.clf()
    plt.imshow(im, cmap=plt.cm.gray)

    IG, JG = np.meshgrid(all_i, all_j)
    fig = plt.figure(2)
    plt.clf()
    ax = Axes3D(fig)
    ax.plot_surface(IG, JG, im, rstride=1, cstride=1, cmap=cm.jet)
    plt.show()