from fastapi import APIRouter, HTTPException, Request
from utils.model import *
from services.camera_service import camera_service
from services.server_service import server_service
import requests
import threading

router = APIRouter(prefix="/camera")

@router.get("/{server_name}")
async def get_camera(server_name: str=None):
    server = server_service.get_by_server_name(server_name)
    records = camera_service.get_camera_by_server(server['server_id'])

    if server is not None:

        server['cameras'] = records if records is not None else []
        del server['_id']
        return {
            "data": server,
            "msg": "success",
        }
    else:
        return {
            "data": {},
            "msg": "fail"
        }

@router.get("/")
async def get_all_camera():
    servers = server_service.get_all()

    if servers is not None:
        for server in servers:

            cameras = camera_service.get_camera_by_server(server['server_id'])
            path = "http://{0}:8005/stream-manage/output/motion-detections-{1}"
            for camera in cameras:
                camera['stream_url'] = path.format(server['ip'], camera['camera_id'])

            server['cameras'] = cameras if cameras is not None else []
            del server['id']

        return {
            "data": list(servers),
            "msg": "success",

        }
    else:
        return {
            "data": {},
            "msg": "fail"
        }

@router.post("", response_model=Reponse[CameraResponse])
async def add_camera_api(camera: Camera):
    try:

        result = camera_service.add_camera(camera)

        def add_to_streaming():
            server = server_service.get_by_id(camera.server_id)
            server_name = server['server_name']
            root_url = f'http://{server_name}:8005/stream-manage/camera'
            requests.post(root_url, json=camera.dict())

        background_thread = threading.Thread(target=add_to_streaming)
        background_thread.start()

        return {"data": result}
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=str(e)
        )

@router.delete("/{camera_id}", response_model=Reponse[object])
async def delete_camera(camera_id: str):
    try:
        camera = camera_service.get_by_id(camera_id)
        result = camera_service.delete_camera(camera_id)

        def delete_to_streaming():
            server = server_service.get_by_id(camera['server_id'])
            server_name = server['server_name']
            root_url = f'http://{server_name}:8005/stream-manage/camera/{camera_id}'
            requests.delete(root_url)

        background_thread = threading.Thread(target=delete_to_streaming)
        background_thread.start()

        return {"data": result}
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=str(e)
        )

@router.put("", response_model=Reponse[CameraResponse])
async def update_camera(camera: Camera):
    try:
        result = camera_service.update_camera(camera)

        def refesh_streaming():
            server = server_service.get_by_id(camera.server_id)
            server_name = server['server_name']
            root_url = f'http://{server_name}:8005/stream-manage/camera/refresh'
            requests.get(root_url)

        background_thread = threading.Thread(target=refesh_streaming)
        background_thread.start()

        return {"data": result}
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=str(e)
        )

@router.put("/update-date", response_model=Reponse[CameraResponse])
async def update_camera_date(model: RangeDate):
    try:
        result = camera_service.get_by_id(model.camera_id)
        if result is None:
            raise HTTPException(
            status_code=400,
            detail='Camera ID does not existed'
        )
        print(result)
        result['start_time'] = model.start_time
        result['end_time'] = model.end_time
        result = camera_service.update_camera(Camera(**result))

        return {"data": result}
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=str(e)
        )

@router.put("/update-status", response_model=Reponse[CameraResponse])
async def update_camera_status(camera: Camera):
    try:
        result = camera_service.get_by_id(camera.camera_id)
        result['camera_status'] = camera.camera_status
        result = camera_service.update_camera(Camera(**result))

        return {"data": result}
    except Exception as e:
        raise HTTPException(
            status_code=400,
            detail=str(e)
        )


import pandas as pd
import numpy as np
from typing import List
from loguru import logger
from meche_copilot.utils.num_tokens_from_string import num_tokens_from_string

def combine_dataframe_chunks(dfs: List[pd.DataFrame]) -> pd.DataFrame:
    if all(df.shape[1] == dfs[0].shape[1] for df in dfs):
        return pd.concat(dfs, axis=0, ignore_index=True)
    elif all(df.shape[0] == dfs[0].shape[0] for df in dfs):
        return pd.concat(dfs, axis=1)
    else:
        raise ValueError("Chunks do not have consistent shape for concatenation.")

def chunk_dataframe(df: pd.DataFrame, axis=1, num_chunks=None, pct_list=None, max_tokens=None, **kwargs) -> List[pd.DataFrame]:
    """Chunk a dataframe into a list of dataframes using number of chunks xor pct of data in each chunk xor max_tokens in each chunk"""

    if axis not in [0, 1]:
        raise ValueError("axis should be either 0 (rows) or 1 (columns).")

    if sum([num_chunks is not None, pct_list is not None, max_tokens is not None]) != 1:
        raise ValueError(f"Exactly one of num_chunks, pct_list, or max_tokes must be specified. Got {num_chunks}, {pct_list}, {max_tokens}")

    # if using percents, they should not add up to greater than 100
    if pct_list:
        if sum(pct_list) > 100:
            raise ValueError("Sum of pct_list should be 100% or less.")
        num_chunks = len(pct_list) + 1
        pct_list.append(100 - sum(pct_list))

    # if using num_chunks (or pct_list), shouldnt be greater than items in axis
    if num_chunks:
      if axis == 0 and num_chunks > df.shape[0]:
          raise ValueError("Number of chunks should not be greater than number of rows.")
      if axis == 1 and num_chunks > df.shape[1]:
          raise ValueError("Number of chunks should not be greater than number of columns.")

    chunks = []
    if num_chunks and not pct_list: # split into num_chunks along axis
        logger.debug(f"Splitting df into {num_chunks} chunks along axis {axis}.")
        split_func = np.array_split
        chunks = split_func(df, num_chunks, axis=axis)
    elif pct_list: # split into fractions along axis
        logger.debug(f"Splitting df into {len(pct_list)} chunks along axis {axis} with pct_list {pct_list}.")
        fractions = [pct / 100 for pct in pct_list]
        if axis == 0: # split rows into fractions
            start_idx = 0
            for frac in fractions:
                end_idx = start_idx + int(frac * df.shape[0])
                chunks.append(df.iloc[start_idx:end_idx])
                start_idx = end_idx
        else: # split columns into fractions
            start_idx = 0
            for frac in fractions:
                end_idx = start_idx + int(frac * df.shape[1])
                chunks.append(df.iloc[:, start_idx:end_idx])
                start_idx = end_idx
    else: # split using max_tokens
        logger.debug(f"Splitting df along axis {axis} with max_tokens {max_tokens} per chunk.")
        encoding_name = kwargs.get("encoding_name", "gpt-4")
        start_idx = 0
        prev_tokens = None # To keep track of the previous token size
        while start_idx < df.shape[0] if axis == 0 else start_idx < df.shape[1]:
            for i in range(start_idx, df.shape[0] if axis == 0 else df.shape[1]): # iterate over rows/cols until max_tokens is reached, then append that chunk
                csv_string = df.iloc[start_idx:i+1].to_csv() if axis == 0 else df.iloc[:, start_idx:i+1].to_csv()
                tokens = num_tokens_from_string(csv_string, encoding_name)
                if tokens > max_tokens:
                    # Print the previous token size, not the updated token size
                    logger.debug(f"Adding chunk with shape {df.iloc[start_idx:i].shape if axis == 0 else df.iloc[:, start_idx:i].shape} and prev num tokens {prev_tokens}.")
                    chunks.append(df.iloc[start_idx:i] if axis == 0 else df.iloc[:, start_idx:i])
                    start_idx = i  + 1 # update start_idx
                    break
                prev_tokens = tokens # Save the previous token size
            else: # if loop completes without breaking (i.e., all remaining data fits within max_tokens)
                chunks.append(df.iloc[start_idx:] if axis == 0 else df.iloc[:, start_idx:])
                break
        logger.debug(f"Split df into {len(chunks)} chunks")

    return chunks


# -*- coding: utf-8 -*-
import re
import json
import time
import scrapy
import requests
import itertools
from lxml import etree
from hashlib import md5

from overseaSpider.items import ShopItem, SkuAttributesItem, SkuItem
from overseaSpider.util.scriptdetection import detection_main
from overseaSpider.util.utils import isLinux

website = 'samys'

class SamysSpider(scrapy.Spider):
    name = website
    # allowed_domains = ['samys.com']
    start_urls = ['https://www.samys.com/']

    @classmethod
    def update_settings(cls, settings):
        custom_debug_settings = getattr(cls, 'custom_debug_settings' if getattr(cls, 'is_debug',
                                                                                False) else 'custom_settings', None)
        system = isLinux()
        if not system:
            # 如果不是服务器, 则修改相关配置
            custom_debug_settings["HTTPCACHE_ENABLED"] = False
            custom_debug_settings["MONGODB_SERVER"] = "127.0.0.1"
        settings.setdict(custom_debug_settings or {}, priority='spider')

    def __init__(self, **kwargs):
        super(SamysSpider, self).__init__(**kwargs)
        self.counts = 0
        setattr(self, 'author', "无穹")

    is_debug = True
    custom_debug_settings = {
        'MONGODB_COLLECTION': website,
        'CONCURRENT_REQUESTS': 4,
        'DOWNLOAD_DELAY': 1,
        'LOG_LEVEL': 'DEBUG',
        'COOKIES_ENABLED': True,
        # 'HTTPCACHE_EXPIRATION_SECS': 14 * 24 * 60 * 60, # 秒
        'DOWNLOADER_MIDDLEWARES': {
            # 'overseaSpider.middlewares.PhantomjsUpdateCookieMiddleware': 543,
            # 'overseaSpider.middlewares.OverseaspiderProxyMiddleware': 400,
            'overseaSpider.middlewares.OverseaspiderUserAgentMiddleware': 100,
        },
        'ITEM_PIPELINES': {
            'overseaSpider.pipelines.OverseaspiderPipeline': 300,
        },
    }

    def filter_html_label(self, text):  # 洗description标签函数
        label_pattern = [r'(<!--[\s\S]*?-->)', r'<script>.*?</script>', r'<style>.*?</style>', r'<[^>]+>']
        for pattern in label_pattern:
            labels = re.findall(pattern, text, re.S)
            for label in labels:
                text = text.replace(label, '')
        text = text.replace('\n', '').replace('\r', '').replace('\t', '').replace('  ', '').strip()
        return text

    def filter_text(self, input_text):
        filter_list = [u'\x85', u'\xa0', u'\u1680', u'\u180e', u'\u2000-', u'\u200a',
                       u'\u2028', u'\u2029', u'\u202f', u'\u205f', u'\u3000', u'\xA0', u'\u180E',
                       u'\u200A', u'\u202F', u'\u205F']
        for index in filter_list:
            input_text = input_text.replace(index, "").strip()
        return input_text

    def parse(self, response):
        """获取全部分类"""
        category_url = ['https://www.samys.com/c/Photography/1/113.html',
                        'https://www.samys.com/c/Video/1/235.html',
                        'https://www.samys.com/c/Studio--Lighting/1/360.html',
                        'https://www.samys.com/c/Electronics/1/421.html',
                        'https://www.samys.com/c/Smartphone/1/830.html',
                        'https://www.samys.com/c/Pro-Cinema--Audio/2/794.html']
        for i in category_url:
            yield scrapy.Request(
                url=i,
                callback=self.parse_list,
                meta={"flag": 0}
            )

    def parse_list(self, response):
        """商品列表页"""
        detail_url = response.xpath("//div[@itemprop='name']/a/@href").getall()
        if detail_url:
            for i in detail_url:
                yield scrapy.Request(
                    url='https://www.samys.com'+i,
                    callback=self.parse_detail
                )
            if response.meta.get("flag") == 0:
                next_url = response.url + '?start=37'
                yield scrapy.Request(
                    url=next_url,
                    callback=self.parse_list,
                    meta={"flag": 1, "start": 37, "url": response.url}
                )
            else:
                start = response.meta.get("start") + 36
                next_url = response.url + '?start=' + str(start)
                yield scrapy.Request(
                    url=next_url,
                    callback=self.parse_list,
                    meta={"flag": 1, "start": start, "url": response.meta.get("url")}
                )
        else:
            category_url = response.xpath("//div[@class='category-container']/div/a/@href").getall()
            for i in category_url:
                yield scrapy.Request(
                    url='https://www.samys.com' + i,
                    callback=self.parse_list,
                    meta={"flag": 0}
                )

    def parse_detail(self, response):
        """详情页"""
        items = ShopItem()
        items["url"] = response.url
        items["name"] = response.xpath('//meta[@property="og:title"]/@content').get()
        cat_temp = response.xpath("//ul[@class='breadcrumbs floatContainer']//a//text()").getall()
        items["detail_cat"] = '/'.join(cat_temp)
        items["cat"] = cat_temp[-1]
        des_temp=response.xpath('//span[@itemprop="description"]//text()').getall()
        items["description"] = self.filter_text(self.filter_html_label(''.join(des_temp)))
        items["source"] = 'samys.com'
        items["brand"] = response.xpath('//meta[@itemprop="brand"]/@content').get()
        image_temp=response.xpath("//ul[@class='slider-detail']/li/a/img/@src").getall()[:1]+response.xpath("//ul[@class='slider-detail']/li/a/img/@data-post-load-image").getall()
        if not image_temp:
            image_temp = response.xpath("//div[@class='swiper-slide false']/img/@src").getall()
        image=[]
        for i in image_temp:
            image.append('https://www.samys.com'+i)
        items["images"] = image
        items["current_price"] = response.xpath("//meta[@itemprop='price']/@content").get()
        items["original_price"] = items["current_price"]
        items["measurements"] = ["Weight: None", "Height: None", "Length: None", "Depth: None"]

        items["sku_list"] =[]
        status_list = list()
        status_list.append(items["url"])
        status_list.append(items["original_price"])
        status_list.append(items["current_price"])
        status_list = [i for i in status_list if i]
        status = "-".join(status_list)
        items["id"] = md5(status.encode("utf8")).hexdigest()

        items["lastCrawlTime"] = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        items["created"] = int(time.time())
        items["updated"] = int(time.time())
        items['is_deleted'] = 0
        # detection_main(items=items, website=website, num=20, skulist=True, skulist_attributes=True)
        # print(items)
        yield items


###############################################################################
# make park model
###############################################################################
import cantera as ct
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt

import rmgpy
from rmgpy.data.thermo import ThermoDatabase
from rmgpy.data.kinetics import KineticsDatabase
from rmgpy.molecule import Molecule
from rmgpy.species import Species
from rmgpy.reaction import Reaction
import inspect
import copy
from rmgpy.kinetics.surface import SurfaceArrhenius
from rmgpy.kinetics.surface import StickingCoefficient
from rmgpy.quantity import ScalarQuantity

import rmgpy.chemkin as Chemkin
from cantera import ck2cti

###############################################################################
# useful functions
###############################################################################
def get_thermo(spec_str):
    '''
    takes a string input and returns a species object with complete thermo
    this may already exist in RMG.
    '''
    spec = Species()
    spec.from_smiles(spec_str)
    est_thermo = thermo_database.get_thermo_data(spec,metal_to_scale_to="Cu111")
    spec.thermo = est_thermo
    return spec

def get_gas_phase_precurs(spec):
    '''
    adapted from ThermoDatabase method:
    get_thermo_data_for_surface_species()
    gets a Species object corresponding to the gas phase precursor for
    a given surface species
    does NOT apply adsorption correction!
    '''
    dummy_molecules = spec.molecule[0].get_desorbed_molecules()

    for mol in dummy_molecules:
        mol.clear_labeled_atoms()
    if len(dummy_molecules) == 0:
        raise RuntimeError(f"Cannot get thermo for gas-phase molecule")

    # if len(molecule) > 1, it will assume all resonance structures have already been
    #generated when it tries to generate them, so evaluate each configuration separately
    # and pick the lowest energy one by H298 value

    gas_phase_species_from_libraries = []
    gas_phase_species_estimates = []
    for dummy_molecule in dummy_molecules:
        dummy_species = Species()
        dummy_species.molecule = [dummy_molecule]
        dummy_species.generate_resonance_structures()
        dummy_species.thermo = thermo_database.get_thermo_data(dummy_species)
        if dummy_species.thermo.label:
            gas_phase_species_from_libraries.append(dummy_species)
        else:
            gas_phase_species_estimates.append(dummy_species)

    # define the comparison function to find the lowest energy
    def lowest_energy(species):
        if hasattr(species.thermo, 'H298'):
            print(species.thermo.H298.value_si)
            return species.thermo.H298.value_si
        else:
            print(species.thermo.get_enthalpy(298.0))
            return species.thermo.get_enthalpy(298.0)

    if gas_phase_species_from_libraries:
        species = min(gas_phase_species_from_libraries, key=lowest_energy)
    else:
        species = min(gas_phase_species_estimates, key=lowest_energy)

    thermo = species.thermo

    return species

def update_thermo(spec, name, be1, be2):
    '''
    updates species thermo given an input for binding energy.
    input species object (spec)
    park name as string (name)
    two floats for the original binding
    energy (be1) and the "correct" binding energy (be2)
    '''
    spec_new = copy.deepcopy(spec)
    ev_2_kj = 9.6e4
    be_diff = (be_dict[name] - be_dict_park[name])*9.6e4
    new_h298 = spec.thermo.H298.value_si - be_diff
    spec_new.thermo.H298.value_si = new_h298
    print(name, id(spec_new.thermo.H298.value_si), id(spec.thermo.H298.value_si))
    print(name, spec_new.thermo.H298.value_si, spec.thermo.H298.value_si, be_diff)
    return spec_new

def make_reaction(reactants, products, rxn_str, A, Ea, stick = False,):
    '''
    make a rmgpy reaction object.
    takes a list of the species objects for
    the reactants and products.
    takes a string for the reaction string
    if Stick is true, A-factor is the sticking coefficient
    '''
    if stick:
        kinetics = StickingCoefficient(
            A=A,
            n=0.0,
            Ea=Ea,
            T0=(1.0, "K"),
            Tmin=None,
            Tmax=None,
            Pmin=None,
            Pmax=None,
            coverage_dependence=None,
            comment=''
        )
    else:
        kinetics = SurfaceArrhenius(
            A=A,
            n=0.0,
            Ea=Ea,
            T0=(1.0, "K"),
            Tmin=None,
            Tmax=None,
            Pmin=None,
            Pmax=None,
            coverage_dependence=None,
            comment=''
        )

    # use the rmgpy reaction object
    rxn = Reaction(
        index=-1,
        label=rxn_str,
        reactants=reactants,
        products=products,
        specific_collider=None,
        kinetics=kinetics,
        network_kinetics=None,
        reversible=True,
        transition_state=None,
        duplicate=False,
        degeneracy=1,
        pairs=None,
        allow_pdep_route=False,
        elementary_high_p=False,
        allow_max_rate_violation=False,
        rank=None,
        comment='',
        is_forward=None,
        )

    return rxn

def convert_to_nasa(spec):
    thermo = spec.thermo
    thermo_nasa = thermo.to_nasa(298, 1500, 1000)
    spec.thermo = thermo_nasa

###############################################################################
# initiialize things
###############################################################################

# quick check that we are using the correct rmgpy and version

print('using rmgpy at: ',inspect.getfile(rmgpy))
print('using rmgpy version: ', rmgpy.__version__)

# save rmgpy and db directory. db is assumed to be in the same
# folder as RMG-Py
rmg_py_path = inspect.getfile(rmgpy).split("rmgpy")[0]
rmg_db_path = rmg_py_path.split("RMG-Py")[0] + "RMG-database/"

# import data
# set absolute location, using './' in jupyter performs differently
# in vscode
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
park_xl_file =os.path.join(__location__,'park_thermo_and_rates.xlsx')
BE_sheet='Binding Energies'
rxn_sheet = 'reactions'
be_df = pd.read_excel(park_xl_file, sheet_name=BE_sheet, engine='openpyxl')
rxn_df = pd.read_excel(park_xl_file, sheet_name=rxn_sheet, engine='openpyxl')

# output files
chemkin_gas_file = os.path.join(__location__, 'park_gas.inp')
chemkin_surface_file = os.path.join(__location__ + '/park_surf.inp') # why do we need a / for surface?
cantera_file = os.path.join(__location__,'park_mech.cti')

###############################################################################
# Constants/values
###############################################################################
site_density_mol_cm = 2.943e-09
site_density_si = site_density_mol_cm * 1e4
site_density_object = ScalarQuantity(site_density_si, 'mol/m^2')

###############################################################################
# get thermo for all species in RMG model. adjust BEs per the sheet values
###############################################################################
db_input_path = rmg_db_path + 'input/'

# load the thermo database
library_path = db_input_path + 'thermo/'
thermo_libraries = [
    'surfaceThermoPt111',

]
thermo_database = ThermoDatabase()
thermo_database.load(
    library_path,
    libraries=thermo_libraries,
    depository=False,
    surface=True
    )

# load the kinetics database
kin_libraries_dir = db_input_path + "kinetics/libraries/Surface/"
kin_fam_dir = db_input_path + "kinetics/families/"

kinetics_libraries = [
    'CPOX_Pt/Deutschmann2006_adjusted',
]

kinetics_families = ['surface']

kinetics_database = KineticsDatabase()
kinetics_database.load_recommended_families(kin_fam_dir  + 'recommended.py')
kinetics_database.load_families(
    path=kin_fam_dir,
    families=kinetics_families,
)

kinetics_database.load_libraries(
    kin_libraries_dir,
    libraries=kinetics_libraries
)

# get binding energies
# need a dictionary translating species names to smiles
# need a dictionary translating species names to smiles
spec_smiles_dict = {
    'CO*':'O=C=[*]',
    'CO2*':'O=C=O.[*]',
    'H*':'[H]*',
    'H2O*':'O.[*]',
    'CH3OH*':'CO.[*]',
    'O*':'O=[*]',
    'OH*':'O[*]',
    'HCO*':'O=C*',
#     'HCOO**':'O=CO[*][*]', #formate, bidentate
    'HCOO**':'O=CO[*].[*]', # formate, bidentate, plus extra X
    'H2CO2*':'[*]OCO[*]',
    'COOH*':'O=C(O)[*]',
    'CH2O*':'C=O.[*]',
    'CH3O*':'CO[*]',
    'CH3O2*':'OCO[*]',
    '*':'[*]',
}

# also need a dict of gas phase species to get be's from
# key is surface species, value is Gas phase precursor
# either from RMGs estimate or if it's explicitly known,
# just the gas phase version (e.g. 'CO2*': 'CO2')
gas_pre_dict = {
    'CO*':'[C-]#[O+]',
    'CO2*':'O=C=O',
    'H*':'[H]',
    'H2O*':'O',
    'CH3OH*':'CO',
    'O*':'[O]',
    'OH*':'[OH]',
    'HCO*':'[CH]=O',
    'HCOO**':'[O]C=O', #formate, bidentate
    'H2CO2*':'[O]C[O]',
    'COOH*':'O=[C]O',
    'CH2O*':'C=O',
    'CH3O*':'C[O]',
    'CH3O2*':'[O]CO',
    '*':'[*]',
}

# all of the gas phase species in the model
gas_smiles_dict = {
    'CO':'[C-]#[O+]',
    'CO2':'O=C=O',
    'H2O':'O',
    'CH3OH':'CO',
    'CH2O':'C=O',
    'H2':'[H][H]',
}

# construct a dictionary of binding energies
be_dict = {}
for label in spec_smiles_dict.keys():
    surf_spec = get_thermo(spec_smiles_dict[label])
    gas_spec = get_thermo(gas_pre_dict[label])

    surf_h298 = surf_spec.thermo.get_enthalpy(298)
    gas_h298 = gas_spec.thermo.get_enthalpy(298)

    be_dict[label] = (surf_h298 - gas_h298)/9.6e4

species_dict = {}
for spec_name in be_df['Species']:
    smiles = spec_smiles_dict[spec_name.strip()]
    spec = get_thermo(smiles)
    spec.label = spec_name
    species_dict[spec_name.strip()] = spec

# # manually add surface site to species_dict
# species_dict['*'] = get_thermo(spec_smiles_dict['*'])

gas_species_dict = {}
for spec_name in gas_smiles_dict.keys():
    smiles = gas_smiles_dict[spec_name.strip()]
    spec = get_thermo(smiles)
    spec.label = spec_name
    gas_species_dict[spec_name.strip()] = spec

# make binding energy dictionary from park data
be_dict_park = {}
for i in range(len(be_df)):
    species = be_df['Species'][i].strip()
    be_park = be_df["BE"][i]
    be_dict_park[species] = be_park

# update thermo to be closer to bark BE values
new_thermo_spec_dict = {}
for name, spec in species_dict.items():
    spec_new = update_thermo(
        spec,
        name,
        be_dict[name],
        be_dict_park[name],
    )
    new_thermo_spec_dict[name] = spec_new

# combine gas and surface species dicts
combined_species_dict = {**new_thermo_spec_dict, **gas_species_dict}

# now that we've solidified the thermo, convert to nasa so chemkin conversion
# is a little easier
for spec in combined_species_dict.values():
    convert_to_nasa(spec)

# pull the information for rea ctants, products,
# and arrhenius prefactors for the equations below
rxn_spec_dict = {}
rxn_dict = {}
rxn_dict_coeff = {}
rxn_list = {}
for index, row in rxn_df.iterrows():
    rxn_raw = row['eqtn']
    rxn = rxn_raw.strip()
    reactants, products = rxn.split("<=>")
    reac_spl = reactants.split("+")
    prod_spl = products.split("+")

    # retain to list with stoichiometric coeff
    # just in case we need it
    reac_spl_coeff = reac_spl
    prod_spl_coeff = prod_spl

    # expand split reactant/product string so
    # reactants with "2" as prefix become two
    # separate strings
    # e.g. 2OH --> OH, OH
    for reac in reac_spl:
        if reac.startswith("2"):
            reac_dup = reac.replace("2","")
            reac_spl.remove(reac)
            reac_spl.extend([reac_dup]*2)

    for prod in prod_spl:
        if prod.startswith("2"):
            prod_dup = prod.replace("2","")
            prod_spl.remove(prod)
            prod_spl.extend([prod_dup]*2)

    rxn_dict[rxn] = [reac_spl, prod_spl]
    rxn_dict_coeff[rxn] = [reac_spl_coeff, prod_spl_coeff]

    if row['Af'] == 'N/A' and row['stick']:
        # if no rate info and sticking coefficient
        A = 1.0 # units of mol/m^2/s
    elif row['Af'] != 'N/A' and row['stick']:
        # if we supply a sticking coefficient
        A = float(row['Af'])
    else:
        # we are making a concession here. rates that do
        # not have an A-factor or Ea specified are quasi-
        # equilibrated, so I am setting the A-factor to the
        # highest value (1e22 1/s) in the mechanism, and
        # making it barrierless (Ea=0 eV)

        if len(reac_spl) > 1:
            A = (float(row['Af'] / site_density_si), 'm^2/(mol*s)') # units of mol/m^2/s
        else:
            A = (float(row['Af'] / site_density_si), 's^-1') # units of mol/m^2/s

    Ea = (float(row['Ef (eV)'] * 9.6e4), 'J/mol') # units of J/mol

    rxn_spec_dict[rxn] = [
        [combined_species_dict[reac] for reac in reac_spl],
        [combined_species_dict[prod] for prod in prod_spl],
    ]

    rxn_obj = make_reaction(
        rxn_spec_dict[rxn][0],
        rxn_spec_dict[rxn][1],
        rxn,
        A,
        Ea,
        stick = row['stick'],
    )
    rxn_list[rxn] = rxn_obj

# finally, make inputs into lists for chemkin file write
chemkin_specs = []
for spec in combined_species_dict.values():
    chemkin_specs.append(spec)

chemkin_rxns = []
for rxn in rxn_list.values():
    chemkin_rxns.append(rxn)

# write chemkin file
# make inputs into lists for chemkin file write
chemkin_specs = []
for spec in gas_species_dict.values():
    chemkin_specs.append(spec)

chemkin_rxns = []

Chemkin.save_chemkin_file(
    chemkin_gas_file,
    chemkin_specs,
    chemkin_rxns,
    verbose=True,
    check_for_duplicates=True,
    )

# make inputs into lists for chemkin file write
chemkin_specs = []
for spec in new_thermo_spec_dict.values():
    chemkin_specs.append(spec)

chemkin_rxns = []
for rxn in rxn_list.values():
    chemkin_rxns.append(rxn)

Chemkin.save_chemkin_surface_file(
    chemkin_surface_file,
    chemkin_specs,
    chemkin_rxns,
    verbose=True,
    check_for_duplicates=True,
    surface_site_density=site_density_object,
    )

parser = ck2cti.Parser()
parser.convertMech(
    chemkin_gas_file,
    outName=cantera_file,
    quiet=True,
    permissive=True,
    surfaceFile=chemkin_surface_file
)

# test that model works by attempting to load it
gas = ct.Solution(cantera_file, "gas")
surf = ct.Interface(cantera_file,"surface1", [gas])


import sqlite3

connection = sqlite3.connect('databasePeças.db')
c = connection.cursor()

def CREATE():
    # PEÇA #
    c.execute('CREATE TABLE IF NOT EXISTS PECA (\
        `codigo` VARCHAR(5) NOT NULL,\
        `nomeSingular` VARCHAR(25) NOT NULL,\
        `nomePlural` VARCHAR(25) NOT NULL,\
        `genero` VARCHAR(1) NOT NULL,\
        `preco` VARCHAR(8),\
        PRIMARY KEY(`codigo`));')
    connection.commit()
    connection.close()
CREATE()

Scripts Banco de Dados/criar_database_Peça.py


"""clean up unused tables

Create Date: 2022-05-02 17:19:09.910095

"""
from alembic import op

# revision identifiers, used by Alembic.
revision = "20220502_171903"
down_revision = "20220425_225456"
branch_labels = None
depends_on = None

def upgrade():
    op.drop_table("region_types", schema="aspen")
    op.drop_table("align_read_workflows", schema="aspen")
    op.drop_table("call_consensus_workflows", schema="aspen")
    op.drop_table("sequencing_reads_collections", schema="aspen")
    op.drop_table("sequencing_instrument_types", schema="aspen")
    op.drop_table("filter_read_workflows", schema="aspen")
    op.drop_table("host_filtered_sequencing_reads_collections", schema="aspen")
    op.drop_table("sequencing_protocol_types", schema="aspen")
    op.drop_table("bams", schema="aspen")
    op.drop_table("called_pathogen_genomes", schema="aspen")

    # Drop dummy data tied to our enums, relevant for dev environments
    op.execute(
        "DELETE FROM aspen.entities WHERE entity_type IN ('SEQUENCING_READS', 'BAM', 'CALLED_PATHOGEN_GENOME', 'HOST_FILTERED_SEQUENCE_READS')"
    )

    op.enum_delete(
        "entity_types",
        [
            "CALLED_PATHOGEN_GENOME",
            "BAM",
            "SEQUENCING_READS",
            "HOST_FILTERED_SEQUENCE_READS",
        ],
        schema="aspen",
    )
    op.enum_delete(
        "workflow_types",
        ["CALL_CONSENSUS", "ALIGN_READ", "FILTER_READ"],
        schema="aspen",
    )

def downgrade():
    raise NotImplementedError("don't downgrade")


import json
import logging
logging.basicConfig(level=logging.DEBUG)
import argparse
import uuid

import emission.storage.decorations.user_queries as esdu
import emission.net.ext_service.push.notify_usage as pnu
import emission.net.ext_service.push.query.dispatch as pqd
import emission.core.wrapper.user as ecwu
import emission.core.get_database as edb

def get_uuid_list_for_platform(platform):
    query_fn = pqd.get_query_fn("platform")
    return query_fn({"platform": platform})

def get_upgrade_push_spec(platform):
    android_url = "https://play.google.com/store/apps/details?id=gov.nrel.cims.openpath"
    ios_url = "https://apps.apple.com/us/app/nrel-openpath/id1628058068"
    if platform == "android":
        platform_url = android_url
    elif platform == "ios":
        platform_url = ios_url
    else:
        raise InvalidArgumentException("Found unknown platform %s, expected 'android' or 'ios'" % platform)
    push_spec = {
        "alert_type": "website",
        "title": "Your version of the NREL OpenPATH app may have errors",
        "message": "Please upgrade to the most recent version",
        "image": "icon",
        "spec": {
            "url": platform_url
        }
    }
    return push_spec

def needs_version_update(uuid, target_version):
    curr_profile = edb.get_profile_db().find_one({"user_id": uuid})
    logging.debug("Read profile %s for user %s" % (curr_profile, uuid))
    if curr_profile is None:
        logging.error("Could not find profile for %s" % uuid)
        return False
    elif curr_profile["client_app_version"] == target_version:
        logging.debug("%s is already at version %s" % (uuid, curr_profile["client_app_version"]))
        return False
    else:
        logging.debug("%s is at version %s, needs update to %s" % (uuid, curr_profile["client_app_version"], target_version))
        return True

def push_upgrade_message_for_platform(platform, cli_args):
    logging.info("About to push to %s" % platform)
    uuid_list = get_uuid_list_for_platform(platform)
    logging.info("UUID list for %s = %s" % (platform, uuid_list))
    if cli_args.target_version:
        filtered_uuid_list = [uuid for uuid in uuid_list if needs_version_update(uuid, cli_args.target_version)]
        logging.info("After filtering for %s, uuid_list is %s" % (cli_args.target_version, filtered_uuid_list))
    else:
        filtered_uuid_list = uuid_list
        logging.info("No target version specified, not filtering list")

    spec = get_upgrade_push_spec(platform)
    if cli_args.dry_run:
        logging.info("dry run, skipping actual push")
    else:
        response = pnu.send_visible_notification_to_users(filtered_uuid_list,
                                                            spec["title"],
                                                            spec["message"],
                                                            spec,
                                                            dev = cli_args.dev)
        pnu.display_response(response)

def runTests():
    try:
        edb.get_profile_db().insert_one({"user_id": "v4", "client_app_version": "1.0.4"})
        edb.get_profile_db().insert_one({"user_id": "v5", "client_app_version": "1.0.5"})
        edb.get_profile_db().insert_one({"user_id": "v6", "client_app_version": "1.0.6"})
        assert needs_version_update("v4", "1.0.6")
        assert needs_version_update("v5", "1.0.6")
        assert not needs_version_update("v6", "1.0.6")
        assert not needs_version_update("unknown", "1.0.6")
    finally:
        logging.debug("About to delete all entries from the profile")
        edb.get_profile_db().delete_many({"user_id": "v4"})
        edb.get_profile_db().delete_many({"user_id": "v5"})
        edb.get_profile_db().delete_many({"user_id": "v6"})

if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog="prompt_upgrade_to_latest")

    # until we figure out a way to add unit tests for scripts
    parser.add_argument("--test", action="store_true", default=False,
        help="Do everything except actually push the survey")
    parser.add_argument("-n", "--dry-run", action="store_true", default=False,
        help="Do everything except actually push the survey")
    parser.add_argument("-t", "--target-version",
        help="Only push to people who have not upgraded to this version")
    parser.add_argument("-d", "--dev", action="store_true", default=False)

    args = parser.parse_args()

    if args.test:
        runTests()
    else:
        push_upgrade_message_for_platform("android", args)
        push_upgrade_message_for_platform("ios", args)


#!/usr/bin/env python3

import click
import sys
from pathlib import Path
from RecBlast.RecBlast import RecSearch
import RecBlast.WarningsExceptions as RBWE

def deduce_searchtype(query_type, db_type, search_algorithm):
    # a bit of cleaning
    query_type = query_type.lower()
    db_type = db_type.lower()
    search_algorithm = search_algorithm.lower()

    if "blast" in search_algorithm:
        if query_type == "dna":
            if db_type == "prot":
                return "blastx"
            elif db_type == "dna":
                return "blastn"
            else:
                raise Exception("Unknown search database type! Allowed options are 'dna' or 'prot'")
        elif query_type == "prot":
            if db_type == "prot":
                return "blastp"
            elif db_type == "dna":
                return "tblastn"
            else:
                raise Exception("Unknown search database type! Allowed options are 'dna' or 'prot'")
        else:
                raise Exception("Unknown search sequence type! Allowed options are 'dna' or 'prot'")
    if "blat" in search_algorithm:
        if query_type == "dna":
            if db_type == "prot":
                return "blatx"
            elif db_type == "dna":
                return "blat"
            else:
                raise Exception("Unknown search database type! Allowed options are 'dna' or 'prot'")
        elif query_type == "prot":
            if db_type == "prot":
                return "blatp"
            elif db_type == "dna":
                return "tblat"
            else:
                raise Exception("Unknown search database type! Allowed options are 'dna' or 'prot'")
        else:
                raise Exception("Unknown search sequence type! Allowed options are 'dna' or 'prot'")
    else:
        raise RBWE.SearchEngineNotImplementedError("This search engine hasn't been implemented yet! Only BLAT and BLAST have been implemented!")

@click.command()
@click.option("-q", "--query-file", type=click.Path(exists=True))
@click.option("--query-file-type", type=str, default="fasta")
@click.option("-p", "--max-processes", type=int, default=40)
@click.option("-fp", "--forward-port")
@click.option("-rp", "--reverse-port")
@click.option("-fs", "--forward-species", type=str)
@click.option("-ft", "--forward-twobit", type=click.Path(exists=False))
@click.option("-rs", "--reverse-species", type=str)
@click.option("-rt", "--reverse-twobit", type=click.Path(exists=False))
@click.option("-ps", "--perc-score", type=str, default= "0.1")
@click.option("-pi", "--perc-identity", type=str, default = "0.5")
@click.option("-pq", "--perc-query-span", type=str, default = "0.5")
@click.option("--query_type", type=str, default = "prot")
@click.option("--reverse_type", type=str, default = "dna")
@click.option("--forward_algo", type=str, default = "blat")
@click.option("--reverse_algo", type=str, default = "blat")
@click.option("--reverse_db_type", type=str, default = "dna")
@click.option("--forward_db_type", type=str, default = "dna")
@click.option("--annotation_lookup_tsv", type=str, default = "")
@click.option("--output-root", type=str, default="./output")
@click.option('-v', '--verbose', count=True)
def __main__(query_file, forward_port, forward_species, forward_twobit,
             reverse_port, reverse_species, reverse_twobit,
             query_type, forward_db_type, forward_algo,
             reverse_type, reverse_db_type, reverse_algo,
             perc_score, perc_identity, perc_query_span, query_file_type, max_processes,
             annotation_lookup_tsv, output_root, verbose):
    perc_score = float(perc_score)
    perc_identity = float(perc_identity)
    perc_query_span = float(perc_query_span)

    forward_twobit = Path(forward_twobit)
    reverse_twobit = Path(reverse_twobit)

    print(forward_twobit, reverse_twobit, output_root, perc_identity, perc_score, perc_query_span, query_file, sep="\n", file=sys.stderr)
    output_location = Path(output_root, forward_twobit.stem)
    print(output_location, file=sys.stderr)

    f_search_type = deduce_searchtype(query_type, forward_db_type, forward_algo)
    r_search_type = deduce_searchtype(reverse_type, reverse_db_type, reverse_algo)

    recblast = RecSearch(target_species=forward_species, query_species=reverse_species,
                         forward_search_type=f_search_type, reverse_search_type=r_search_type,
                         sequence_source="twobit", verbose=verbose)
    recblast.max_processes = max_processes
    recblast.set_queries(query_file,
                         infile_type=query_file_type)
    recblast.forward_search_settings['database_port'] = {forward_species: forward_port}
    recblast.forward_search_settings['database'] = {forward_species: str(forward_twobit.name)}
    recblast.forward_search_settings['database_path'] = str(forward_twobit.parent)
    recblast.forward_search_criteria = dict(perc_score=perc_score,
                                            perc_ident=perc_identity,
                                            perc_query_span=perc_query_span)
    recblast.sequence_source_settings['database'] = {forward_species: str(forward_twobit.name)}
    recblast.sequence_source_settings['database_path'] = str(forward_twobit.parent)
    recblast.memory_saver_level = 1
    recblast.reverse_search_settings['database'] = {reverse_species: str(reverse_twobit.name)}
    recblast.reverse_search_settings['database_path'] = str(reverse_twobit.parent)
    recblast.reverse_search_settings['database_port'] = {reverse_species: reverse_port}
    if annotation_lookup_tsv:
        recblast.set_translation_annotation_parameters(method="table", key_value_order=False,
                                                    tsv_location=annotation_lookup_tsv)
    else:
        recblast.set_translation_annotation_parameters(method=False)
    recblast(run_name="{0}-pcScore{1}_pcIdent{2}_pcQuerySpan{3}_reverse-{4}".format(Path(query_file).stem,
                                                                                perc_score,
                                                                                perc_identity,
                                                                                perc_query_span,
                                                                                reverse_twobit.stem),
             output_type="bed-complete",
             output_location=output_location)

if __name__ == "__main__":
    __main__()

exit()


from codecs import open
from os import path
import re
from setuptools import setup, find_packages

dot = path.abspath(path.dirname(__file__))

# get the dependencies and installs
with open(path.join(dot, 'requirements.txt'), encoding='utf-8') as f:
    all_reqs = f.read().split('\n')

install_requires = [x.strip() for x in all_reqs if 'git+' not in x]
dependency_links = [x.strip().replace('git+', '') for x in all_reqs if
                    x.startswith('git+')]

# parse the version file
ver_content = open("cloudy/_version.py", "rt").read()
ver_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", ver_content, re.M)
if ver_match:
    version = ver_match.group(1)
else:
    raise RuntimeError("Unable to find version string")

setup(
    name='cloudy',
    version=version,
    description='opinionated & personal screenshot handler',
    long_description=(
        'Watches a directory for file changes, uploads them to a remote,'
        'generates a link, shortens it and dumps it into the clipboard.'
    ),
    license='BSD',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Programming Language :: Python :: 3',
    ],
    entry_points={
        'console_scripts': ['cloudy=cloudy.cloudy:cli'],
    },
    keywords='',
    packages=find_packages(exclude=['docs', 'tests*']),
    include_package_data=True,
    install_requires=install_requires,
    dependency_links=dependency_links,
)


import pandas as pd
import tekore as tk
from config import CLIENT_ID, CLIENT_SECRET

class SpotifyData:

    def get_one_song_data(self, query):
        token = tk.request_client_token(CLIENT_ID, CLIENT_SECRET)
        spotify = tk.Spotify(token)
        searched_track = spotify.search(query, types=('track',), market='pl')
        artist_id = searched_track[0].items[0].artists[0].id
        id = searched_track[0].items[0].id
        af = spotify.track_audio_features(id)
        output = [
            [af.danceability,
             af.energy,
             af.loudness,
             af.acousticness,
             af.instrumentalness,
             af.liveness,
             af.speechiness,
             af.valence]
        ]
        print("Znaleziono:", searched_track[0].items[0].artists[0].name, "-", searched_track[0].items[0].name)
        print("Gatunek:", spotify.artist(artist_id).genres[0])
        return pd.DataFrame(output, columns=['danceability', 'energy', 'loudness', 'acousticness', 'instrumentalness',
                                             'liveness', 'speechiness', 'valence'])

    def get_data(self, genres):
        genres_names = genres
        token = tk.request_client_token(CLIENT_ID, CLIENT_SECRET)
        spotify = tk.Spotify(token)
        output = pd.DataFrame(
            columns=['genre', 'danceability', 'energy', 'loudness', 'acousticness', 'instrumentalness',
                     'liveness', 'speechiness', 'valence'])
        for genre in genres_names:
            print('now: ', genre)
            tracks_id = []
            tracks_af = []

            try:
                searched_playlists = spotify.search(genre, types=('playlist',), market='pl', limit=50, offset=0)
                playlist_id = None
                for i in range(100):
                    if searched_playlists[0].items[i].tracks.total >= 100:
                        playlist_id = searched_playlists[0].items[i].id
                        break
                    elif i == 100:
                        playlist_id = searched_playlists[0].items[0].id

                playlist = spotify.playlist(playlist_id)
                playlist_tracks = playlist.tracks.items

                for i in range(100):
                    tracks_id.append(playlist_tracks[i].track.id)

                afs = spotify.tracks_audio_features(track_ids=tracks_id)
                print(len(afs))
                for af in afs:
                    tracks_af.append(
                        [genre, af.danceability, af.energy, af.loudness, af.acousticness, af.instrumentalness,
                         af.liveness,
                         af.speechiness, af.valence])
                x = pd.DataFrame(tracks_af,
                                 columns=['genre', 'danceability', 'energy', 'loudness', 'acousticness',
                                          'instrumentalness',
                                          'liveness', 'speechiness', 'valence'])
                output = pd.concat([output, x])
            except AttributeError:
                print('tekore attribute error')
                continue
            except IndexError:
                print('playlist index error')
                continue
            except TypeError:
                print('audio features type error')
                continue

        return output


import matplotlib.pyplot as plt
import numpy as np

plt.rcParams["text.usetex"] = True

LEGEND_FONTSIZE = 20
TICK_LABEL_FONTSIZE = 20
AXIS_LABEL_FONTSIZE = 20
TITLE_FONTSIZE = 20
CHART_SIZE = [10, 6]
LONG_CHART_SIZE = [10, 10]

def do_nothing_Rt_plot(Rt_dict, fname=None, ps=True):
    fig, ax = plt.subplots(1, 1, figsize=CHART_SIZE)
    ax.set_xlabel("Time (days)", fontsize=AXIS_LABEL_FONTSIZE)
    ax.set_ylabel(r"$\mathcal{R}_t$", fontsize=AXIS_LABEL_FONTSIZE)

    for R0 in Rt_dict:
        t_arr = Rt_dict[R0]["t"]
        Rt_arr = Rt_dict[R0]["Rt"]

        ax.plot(t_arr, Rt_arr, label=f"{R0:.1f}")

    ax.legend(
        loc="best",
        title=r"$\mathcal{R}_0$",
        fontsize=LEGEND_FONTSIZE,
        title_fontsize=TITLE_FONTSIZE,
    )
    ax.tick_params(axis="both", which="major", labelsize=TICK_LABEL_FONTSIZE)

    if not (fname is None):
        fig.savefig(fname)

    if ps:
        plt.show()

def do_nothing_hospital_plot(region_dict, fname=None, ps=True):
    fig, ax = plt.subplots(2, 2, sharex=True, sharey=True, figsize=CHART_SIZE)
    R0s = [k for k in region_dict]

    R0 = R0s[0]
    hospital_do_nothing_plot(ax[0, 0], region_dict[R0], R0, xlabel=False)

    plt.gca().set_prop_cycle(None)
    R0 = R0s[1]
    hospital_do_nothing_plot(ax[0, 1], region_dict[R0], R0, xlabel=False)

    plt.gca().set_prop_cycle(None)
    R0 = R0s[2]
    hospital_do_nothing_plot(ax[1, 0], region_dict[R0], R0)

    plt.gca().set_prop_cycle(None)
    R0 = R0s[3]
    hospital_do_nothing_plot(ax[1, 1], region_dict[R0], R0)

    fig.suptitle(r"Beds Occupied $(\%N)$", fontsize=TITLE_FONTSIZE)

    if not (fname is None):
        fig.savefig(fname)

    if ps:
        plt.show()

def hospital_do_nothing_plot(ax, Hdict, R0, xlabel=True):
    for region in Hdict:
        pN = Hdict[region]["pN"]
        ax.plot(Hdict[region]["t"], Hdict[region]["H"] * pN, label=region)

    if xlabel:
        ax.set_xlabel("Time (days)", fontsize=AXIS_LABEL_FONTSIZE)

    ax.legend(
        loc="best",
        title="Region",
        fontsize=LEGEND_FONTSIZE,
        title_fontsize=TITLE_FONTSIZE,
    )
    ax.tick_params(axis="both", which="major", labelsize=TICK_LABEL_FONTSIZE)
    ax.set_title(rf"$\mathcal{{R}}_0$ = {R0:.1f}", fontsize=TITLE_FONTSIZE)

def do_nothing_deaths_plot(region_dict, region_abm_dict, fname=None, ps=True):
    fig, ax = plt.subplots(1, 1, figsize=CHART_SIZE)

    for region in region_dict:
        (line,) = ax.plot(
            region_dict[region]["R0"],
            region_dict[region]["D"],
            label=region,
        )
        yerr = [
            np.array(region_abm_dict[region]["D_mean"])
            - np.array(region_abm_dict[region]["D_lb"]),
            np.array(region_abm_dict[region]["D_ub"])
            - np.array(region_abm_dict[region]["D_mean"]),
        ]
        ax.errorbar(
            region_abm_dict[region]["R0"],
            region_abm_dict[region]["D_mean"],
            yerr,
            label=f"PR: {region}",
            fmt=".",
            c=line.get_color(),
            capsize=5,
            # c="k",
        )

    ax.set_xlabel(r"$\mathcal{R}_0$", fontsize=AXIS_LABEL_FONTSIZE)
    ax.set_ylabel(r"Dead individuals $(\%N)$", fontsize=AXIS_LABEL_FONTSIZE)
    ax.legend(
        loc="best",
        title="Region",
        fontsize=LEGEND_FONTSIZE,
        title_fontsize=TITLE_FONTSIZE,
    )
    ax.tick_params(axis="both", which="major", labelsize=TICK_LABEL_FONTSIZE)

    if not (fname is None):
        fig.savefig(fname)

    if ps:
        plt.show()


import numpy as np
import json

def dump_to_file(arrays, filename):
  arrays_for_dump = {}
  for key, array in arrays.items():
    if isinstance(array, np.ndarray):
      arrays_for_dump[key] = array.tolist()
    else:
      arrays_for_dump[key] = array
      if isinstance(array, dict):
        try:
          for k,v in array.items():
            arrays_for_dump[key][k] = v.tolist()
        except:
          pass
  with open(filename, 'w') as handle:
    json.dump(arrays_for_dump, handle, indent=2)

def load_from_file(filename):
  with open(filename, 'r') as handle:
    arrays_for_dump = json.load(handle)
  arrays = {}
  for key, array in arrays_for_dump.items():
    if isinstance(array, list):
      arrays[key] = np.asarray(array)
    elif isinstance(array, dict):
      try:
        arrays[key] = {int(k):np.asarray(v) for k,v in array.items()}
      except:
        arrays[key] = array
    else:
      arrays[key] = array
  return arrays


# pylint: disable=W0613
from flask import request
from injector import inject

from app import app
from app.regali_app.list.application.use_cases import (
    get_gift_list,
    get_gift_lists,
    delete_gift_list,
    create_gift_list,
    delete_gift_list_element,
    create_gift_list_element
)
from app.regali_app.shared.infrastructure.routes.authentication import token_required

@inject
@app.route('/giftlists', methods=['POST'])
@token_required
def post_giftlist(
    current_user,
    use_case: create_gift_list.UseCase,
    request_data_transformer: create_gift_list.RequestDataTransformer
):
    return use_case.execute(
        request_data_transformer.transform(
            current_user.id,
            request
        )
    )

@inject
@app.route('/giftlists/<reference>', methods=['GET'])
@token_required
def get_giftlist(current_user, use_case: get_gift_list.UseCase, reference):
    giftlists = use_case.execute(get_gift_list.Request(reference))

    return giftlists

@inject
@app.route('/giftlists', methods=['GET'])
@token_required
def get_giftlists(current_user, use_case: get_gift_lists.UseCase):
    giftlists = use_case.execute()

    return giftlists

@inject
@app.route('/giftlists/<reference>', methods=['DELETE'])
@token_required
def delete_giftlists(current_user, use_case: delete_gift_list.UseCase, reference):
    use_case.execute(delete_gift_list.Request(reference))

    return {
        'message': 'List Deleted'
    }

@inject
@app.route('/giftlists/<reference>/elements', methods=['POST'])
@token_required
def post_giftlist_element(current_user, use_case: create_gift_list_element.UseCase, reference):
    return use_case.execute(
        create_gift_list_element.Request(reference, request.json['url'])
    )

@inject
@app.route('/giftlists/<list_reference>/elements/<element_reference>', methods=['DELETE'])
@token_required
def delete_giftlist_element(
    current_user,
    use_case: delete_gift_list_element.UseCase,
    list_reference,
    element_reference
):
    use_case.execute(
        delete_gift_list_element.Request(
            list_reference,
            element_reference
        )
    )

    return {
        'message': 'List Element Deleted'
    }


import math
from typing import List
import os
from app.resources.logger import logger

def get_block_entropy(block: bytes, block_size: int) -> float:
    # start counters
    counters = {byte: 0 for byte in range(2 ** 8)}
    for byte in block:
        counters[byte] += 1
    # calculate probabilities for each byte
    probabilities = [counter / block_size for counter in counters.values()]
    # final sum
    entropy = -sum(
        probability * math.log2(probability)
        for probability in probabilities if probability > 0
        )
    return entropy

def get_file_entropy(file_name: str, block_size: int) -> List[float]:
    entropy_detail = []
    f = open(file_name, "rb")
    block = f.read(block_size)
    # get entropy for each block
    while block:
        entropy = get_block_entropy(block, block_size)
        entropy_detail.append(float(f'{entropy:.2f}'))
        block = f.read(block_size)
    f.close()
    return entropy_detail

def get_entropy_summary(entropy_detail: List[float]) -> dict:
    low_entropy_blocks = len([x for x in entropy_detail if x < 2])
    high_entropy_blocks = len([x for x in entropy_detail if x > 7])
    entropy_summary = {
        "low_entropy_blocks": low_entropy_blocks,
        "high_entryopy_blocks": high_entropy_blocks
    }
    return entropy_summary

def delete_saved_file(file_name: str) -> None:
    if os.path.exists(file_name):
        os.remove(file_name)

def generate_entropy_report(file_name: str, block_size: int) -> dict:
    try:
        entropy_detail = get_file_entropy(file_name, block_size)
        entropy_summary = get_entropy_summary(entropy_detail)
        response = {
            "entropyDetail": entropy_detail,
            "summary": entropy_summary
        }
        delete_saved_file(file_name)
        return response
    except Exception as e:
        logger.error(f"Exception raised: {e} , deleting saved file.")
        delete_saved_file(file_name)
        raise e


"""module used to create a matrix containing the distances between the postcodes that specify the locations of the
 specified geographies"""

import os
import pandas as pd
from math import radians, sin, cos, sqrt, atan2
import csv
import datetime as dt

def calc_dist(inlat1, inlat2, inlong1, inlong2):
    """simple function that returns a distance between points given lat and longs of points.
    Takes curvature of earth into account"""
    R = 6373.0

    lat1 = radians(inlat1)
    lon1 = radians(inlong1)
    lat2 = radians(inlat2)
    lon2 = radians(inlong2)

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))

    return R * c

def produce_distance_matrix(input_data, geog, lat, long):
    """ produces a matrix of distance between lat and logs for given level of geography. Produces separate files
    due to potential for a very large matrix for lower levels of geography. If joined would take the form:

       A B C
     A 0 2 4
     B 2 0 5
     C 4 5 0

    Where A, B and C represent the codes of the geographies.

    Requires:

    input_data - csv file of lat and longs of each item (e.g. lsoa, la, postcode etc)
    geog - the name of the column with the codes
    lat - name of the latitude column
    long - name of the longitude column
    """

    start_time = dt.datetime.now()
    print('Started at: ', start_time)

    # load lat and long data
    fields = [geog, lat, long]
    info_df = pd.read_csv(input_data, usecols=fields)

    # create lists from dataframe - faster to access than dataframes
    name_list = list(info_df[geog])
    lat_list = list(info_df[lat])
    long_list = list(info_df[long])

    # for each record in the input
    for i in range(0, len(info_df)):

        current = name_list[i]
        current_lat = lat_list[i]
        current_long = long_list[i]

        temp_out = []  # list to store  distance results

        # calculate distances to all other records in input
        for j in range(0, len(info_df)):
            temp_name = name_list[j]
            temp_lat = lat_list[j]
            temp_long = long_list[j]

            dist = calc_dist(current_lat, temp_lat, current_long,  temp_long)
            # investigate if putting zero distances to NaN makes finding min easier...

            # create lists of names and associated distances
            temp_out.append([temp_name, dist])

        # report on progress based on current records processed
        if i > 0 and i % 1000 == 0:
            time_now = dt.datetime.now()
            time_left = ((time_now - start_time).seconds/(i/len(name_list))) - (time_now - start_time).seconds
            finish_time = time_now + dt.timedelta(seconds=time_left)

            print('Row ', i, 'reached. Projected finish time is: ', finish_time)

        temp_out = [['lsoa11cd', current]] + temp_out

        out_file = os.path.join(os.getcwd(), 'lsoa distances', current + ".csv")
        # write to output file
        with open(out_file, 'w') as myfile:
            for row in temp_out:
                wr = csv.writer(myfile)
                wr.writerow(row)

# location of input data
os.chdir(os.path.join(os.getcwd(), 'raw_inputs'))
# input data to include
produce_distance_matrix('LSOA_L&L.csv', 'LSOA11CD', 'LATITUDE', 'LONGITUDE')


import threading
import socket
import json
import termcolor
import time
import numpy as np
import pandas as pd
host = '127.0.0.1'#server ip
port = 5555 #server port
#Create a new socket using the given address family, socket type and protocol number.
#ipv4 family
#TCP (SOCK_STREAM) is a connection-based protocol. The connection is established
#and the two parties have a conversation until the connection is terminated by one of the parties or by a network error.
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#	Allows a socket to bind to an address and port already in use.
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server.bind((host, port)) #binidng port ip with server
server.listen()
clients = []
aliases = []

def specific_cast(message):
    """
    Forward mesages to client for whom message is meant by checking message header for sender and receiver.
    """
    receiver=message[2] #read message list 0 index
    index = aliases.index(receiver)
    client=clients[index]
    reliable_send(client,message)

# Function to handle clients'connections
def users_send():
    """
    Read username column from db and return
    """
    df=pd.read_csv('users.csv')
    new=df['username'].to_numpy().tolist()
    return new
def reliable_recv(target):
    """
    Receive as long as there is something to receive, can receive more than 1024 bytes
    """
    data = ''
    while True:
        try:
            #rstrip removes spaces at end
            data = data + target.recv(1024).decode().rstrip()
            return json.loads(data)
        except ValueError:
            continue

def reliable_send(target, data):
    """
    Reliable send, json object encoded as string

    """
    #json.dumps() takes in a json object and returns a string.

    jsondata = json.dumps(data)
    target.send(jsondata.encode())

def handle_client(client):
    """
    Works in different thread,
    handle every client connects with server
    checks header to call specific function
    """
    while True:
        try:
            message = reliable_recv(client)
            if str(message[0])=='messaging' and str(message[2])=='ALL':
                #messageing meant between specific clients
                broadcast(message)
            elif str(message[0])=='messaging':
                #if not broadcast
                specific_cast(message)

        except:
            index = clients.index(client)
            clients.remove(client)
            client.close()
            alias = aliases[index]
            aliases.remove(alias)
            #remove client if disconnected from list
            break
# Main function to receive the clients connection

def auth(name,password):
    """
    Loading users from db and comparing with fed name and password and returns true if authenticated
    """
    #checks for password and username parameters
    df=pd.read_csv('users.csv')
    row=df.loc[df['username']==name]
    isAuth=False
    row=np.array(row)
    if row.size:
        isAuth=row[0][1]==password
    return isAuth

def broadcast(message):
    """
    Broadcast messages ato all clients
    """
    for client in clients:
        reliable_send(client,message)
def receive():
    """
    Once User connects it, checks user credentials with db and send auth=true Response to clients, then client get authenticated.
    It also asks for clients name and then calls sender and receiver threads.
    """
    while True:
        print(termcolor.colored('[+] Server is Running! Waiting For The Incoming Connections ...', 'green'))
        client, address = server.accept()
        isAuth=False
        while not isAuth:
            data=reliable_recv(client)
            time.sleep(0.05)
            if data[0]=='auth':
                isAuth=auth(data[1],data[2])
                users_list=users_send()
                time.sleep(0.050)
                reliable_send(client,['auth_res',isAuth,users_list])
                time.sleep(0.050)
        time.sleep(2)
        print(termcolor.colored(str(address) + ' has connected!', 'green'))
        reliable_send(client,'alias?')
        alias = reliable_recv(client)
        aliases.append(alias)
        clients.append(client)
        print(f'The name of new client is {alias}')
        reliable_send(client,'you are now connected!')
        thread = threading.Thread(target=handle_client, args=(client,))
        thread.start()

receive()


from django.contrib.auth.models import User
from django.http import HttpResponseRedirect
from django.views.generic import FormView
from atelier.models import Profile
from django.views import generic
from atelier.forms import ProfileRegisterForm, ProfileChangeForm
from django.urls import reverse_lazy
from atelier.views.base_view import AtelierFilterObjectsPreMixin, BaseListView, TailorPermissionPreMixin, \
    BaseDetailView, BaseDeleteView, BaseUpdateView

class ProfileDetailView(TailorPermissionPreMixin, AtelierFilterObjectsPreMixin, BaseDetailView):
    model = Profile
    fields = '__all__'

class ProfileListView(AtelierFilterObjectsPreMixin, TailorPermissionPreMixin, BaseListView):
    model = Profile

class ProfileCreateView(TailorPermissionPreMixin, FormView):
    template_name = 'atelier/create_form.html'
    form_class = ProfileRegisterForm

    def get_initial(self):
        """
        Returns the initial data to use for atelier form field.
        """
        initial = super().get_initial()
        initial['atelier'] = self.request.user.profile.atelier
        return initial

    def get_success_url(self):
        return reverse_lazy('atelier:profile_list')

    def form_valid(self, form):
        # The default implementation for form_valid() simply redirects to the success_url.
        user = User.objects.create(
            email=form.cleaned_data['email'],
            username=form.cleaned_data['username'],
        )
        user.set_password(form.cleaned_data['password2'])
        user.save()
        Profile.objects.create(
            user=user,
            atelier=self.request.user.profile.atelier,
            is_tailor=form.cleaned_data['is_tailor'],
            created_by=self.request.user,
            last_updated_by=self.request.user,
        )
        return super().form_valid(form)

class ProfileChangeView(AtelierFilterObjectsPreMixin, TailorPermissionPreMixin, BaseUpdateView):
    model = Profile
    template_name = 'atelier/create_form.html'
    form_class = ProfileChangeForm

    def get_success_url(self):
        return reverse_lazy('atelier:profile_list')

    def get_profile_object(self):
        profile_id = self.kwargs.get('pk')
        return Profile.objects.get(id=profile_id)

    def get_initial(self):
        data = {
            'email': self.get_profile_object().user.email,
            'is_tailor': self.get_profile_object().is_tailor
        }
        return data

    def form_valid(self, form):
        # The default implementation for form_valid() simply redirects to the success_url.
        profile = self.get_profile_object()
        profile.is_tailor = form.cleaned_data['is_tailor']
        profile.user.email = form.cleaned_data['email']
        profile.last_updated_by = self.request.user
        profile.full_clean()
        profile.save()
        profile.user.save()
        return super().form_valid(form)

class ProfileDeleteView(TailorPermissionPreMixin, AtelierFilterObjectsPreMixin, BaseDeleteView):
    model = Profile
    success_url = reverse_lazy('atelier:profile_list')
    template_name = 'atelier/delete_form.html'

    def get_user_object(self):
        profile_id = self.kwargs.get('pk')
        profile = Profile.objects.get(pk=profile_id)
        return profile.user

    def delete(self, request, *args, **kwargs):
        """
        Overriding the delete() method to delete User instances, and according Profile instance will be deleted too.
        """
        self.object = self.get_user_object()
        success_url = self.get_success_url()
        self.object.delete()
        return HttpResponseRedirect(success_url)

    def get_success_url(self):
        return reverse_lazy('atelier:profile_list')


import sys
import gammalib
from utils import *
import numpy as np
import matplotlib.pyplot as plt

# first input is XML file name
models = gammalib.GModels(sys.argv[1])
# second and third input are minimum and maximum energy in TeV
emin = float(sys.argv[2])
emax = float(sys.argv[3])

lons, lats, radii, fluxes, names = dist_from_gammalib(models, emin=emin,emax=emax)

# binning
logs_min = int(np.floor(np.log10(np.min(fluxes))))
logs_max = int(np.ceil(np.log10(np.max(fluxes))))
nbins = 10 * (logs_max - logs_min)
bins_lognlogs = np.logspace(logs_min, logs_max, nbins)

fig1 = plt.figure('LogNLogS')
ax1 = plt.subplot()
ax1.set_xscale('log')
ax1.set_yscale('log')
ax1.set_xlabel("Flux {}-{} TeV (Crab units)".format(emin,emax), fontsize=14)
ax1.set_ylabel('Number of sources (> Flux)', fontsize=14)
format_ax(ax1)

ax1.hist(fluxes, bins=bins_lognlogs, density=False, histtype='step', cumulative=-1)

try:
    if sys.argv[4] == 'check_flux':
        for s, flux in enumerate(fluxes):
            if flux > 1.:
                msg = "Source {} has flux of {} Crab".format(names[s],flux)
                print(msg)
except:
    pass

plt.show()


import matplotlib.pyplot as plt
import math

def pre_processing(filename):
    file = open(filename)
    lines = file.readlines()
    cities = [[]]
    n = int(lines[0])
    for l in lines[1:]:
        cities.append(list(map(float, l.split())))
    adj_mat = [[None] * (n+1)  for i in range(n+1)]

    x = [float(i[0]) for i in cities[1:]]
    y = [float(i[1]) for i in cities[1:]]
    plt.scatter(x, y)
    plt.show()

    for i in range(1, n+1):
        for j in range(1, n+1):
            if adj_mat[j][i]:
                adj_mat[j][i] = adj_mat[i][j]
            else:
                c1 = cities[i]
                c2 = cities[j]
                adj_mat[j][i] = math.sqrt((c1[0] - c2[0])**2 + (c1[1] - c2[1])**2)
    return adj_mat, n

def get_bin(n, bits):
    if n == 0:
        return [0]
    res = list()
    digit = n-1
    cur = 1 << digit
    while cur < 1 << bits:
        for i in get_bin(n-1, digit):
            res.append(cur|i)
        digit += 1
        cur = 1 << digit
    return res

def get_sets(n):
    sets = dict()
    for i in range(0, n + 1):
        #print(i)
        sets[i] = get_bin(i, n)
    return sets

#sets: dict
#keys: subproblem size
#value: binary set
def get_ele(set_hash):
    set_hash >>= 1
    k = 2
    res = set()
    while set_hash:
        if set_hash & 1:
            res.add(k)
        k += 1
        set_hash >>= 1
    return res

def TSP(n,adj_mat):

    sets = get_sets(n)
    A = {}
    #deal with base case
    for i in range(1<<n):
        A[i,1] = float("inf")
    A[1,1] = 0

    for m in range(2, n+1):
        print('m=', m)
        for subset in sets[m]:
            if subset & 1:
                elements = get_ele(subset)
                for j in elements:
                    min_ = float("inf")
                    for k in range(1, n+1):
                        if k!= j:
                            if (subset ^ 1<<j-1,k) in A:
                                min_ = min(min_, A[subset ^ 1<<j-1, k] + adj_mat[k][j])
                            A[subset,j] = min_
    return A
if __name__ == '__main__':
    n, adj_mat = pre_processing('tsp.txt')
    A = TSP(n,adj_mat)
    res = float("inf")
    for j in range(2, n+1):
        res = min(res, A[(1<<n)-1, j] + adj_mat[j][1])
    print(res)


import os
import logging
import shutil
import json

from packratAgent.Collection import Collection
from packratAgent.LocalRepoManager import LocalRepoManager, hashFile

MODULE_OWNER = 'packrat'

class AnsibleGalaxyManager( LocalRepoManager ):
  def __init__( self, *args, **kargs ):
    super().__init__( *args, **kargs )
    self.entry_map = {}

  def filePaths( self, filename, distro, distro_version, arch ):
    ( namespace, name, version ) = filename.split( '-' )
    version = version.strip( '.tar.gz' )

    name_path = os.path.join( self.repo_dir, 'api', 'collections', namespace, name, 'versions' )

    return [
             os.path.join( name_path, 'index.html' ),
             os.path.join( name_path, version, 'index.html' ),
             os.path.join( self.repo_dir, 'downloads', namespace, filename )
           ]

  def metadataFiles( self ):
    return [ os.path.join( self.repo_dir, 'api', 'index.html' ) ]

  def addEntry( self, type, filename, distro, distro_version, arch ):
    if type != 'galaxy':
      logging.warning( 'ansiblegalaxy: New entry not a ansible, skipping...' )
      return

    logging.debug( 'ansiblegalaxy: Got Entry for package: "%s"', filename )

    ( namespace, name, version ) = filename.split( '-' )
    version = version.strip( '.tar.gz' )

    if namespace not in self.entry_map:
      self.entry_map[ namespace ] = {}
    if name not in self.entry_map[ namespace ]:
      self.entry_map[ namespace ][ name ] = {}

    dir_path = os.path.join( self.repo_dir, 'downloads', namespace )
    file_path = os.path.join( dir_path, filename )

    size = os.path.getsize( file_path )
    ( _, sha256, _ ) = hashFile( file_path )

    collection = Collection( file_path )

    self.entry_map[ namespace ][ name ][ filename ] = ( version, size, sha256, collection.metadata )

  def removeEntry( self, filename, distro, distro_version, arch ):
    ( namespace, name, version ) = filename.split( '-' )
    version = version.strip( '.tar.gz' )

    try:
      del self.entry_map[ namespace ][ name ][ filename ]
      if not self.entry_map[ namespace ][ name ]:
        del self.entry_map[ namespace ][ name ]
      if not self.entry_map[ namespace ]:
        del self.entry_map[ namespace ]

    except KeyError:
      logging.warning( 'ansiblegalaxy: unable to remove entry "%s", ignored.', filename )

  def loadFile( self, filename, temp_file, distro, distro_version, arch ):
    ( namespace, name, _ ) = filename.split( '-' )

    dir_path = os.path.join( self.repo_dir, 'downloads', namespace )
    if not os.path.exists( dir_path ):
        os.makedirs( dir_path )

    file_path = os.path.join( dir_path, filename )
    shutil.move( temp_file, file_path )

  def writeMetadata( self ):
    api_path = os.path.join( self.repo_dir, 'api' )
    if not os.path.exists( api_path ):
      os.makedirs( api_path )

    api_metadata_map = {
                          'description': '{0} - {1}'.format( self.mirror_description, self.repo_description ),
                          'available_versions': { 'v2': '' }
                        }

    open( os.path.join( api_path, 'index.html' ), 'w' ).write( json.dumps( api_metadata_map ) )

    collections_path = os.path.join( api_path, 'collections' )
    if not os.path.exists( collections_path ):
      os.makedirs( collections_path )

    for namespace in self.entry_map.keys():
      for name in self.entry_map[ namespace ].keys():
        versions_path = os.path.join( collections_path, namespace, name, 'versions' )
        if not os.path.exists( versions_path ):
          os.makedirs( versions_path )

        result_list = []
        for filename, entry in self.entry_map[ namespace ][ name ].items():
          result_list.append( { 'version': entry[0] } )
          entry_map = {
                        'version': entry[0],
                        'namespace': { 'name': namespace },
                        'collection': { 'name': name },
                        'download_url': '{0}/downloads/{1}/{2}'.format( self.repo_url, namespace, filename ),
                        'artifact': { 'size': entry[1], 'sha256': entry[2] },
                        'metadata': entry[3]
                      }
          entry_path = os.path.join( versions_path, entry[0] )
          if not os.path.exists( entry_path ):
            os.makedirs( entry_path )

          open( os.path.join( entry_path, 'index.html' ), 'w' ).write( json.dumps( entry_map ) )

        version_map = {
                        'count': len( result_list ),
                        'next': None,
                        'previous': None,
                        'results': result_list
                      }

        logging.debug( 'ansiblegalaxy: writing version index for "%s"', namespace )
        open( os.path.join( versions_path, 'index.html' ), 'w' ).write( json.dumps( version_map ) )


import argparse
import pygame as pg

def create_circle(size, circle_color, background_color, filename):
  # margin = int(0.1 * size)
  margin = 0
  screen = pg.display.set_mode((2 * size + margin, 2 * size + margin))
  screen.fill(background_color)
  x = screen.get_width() // 2
  y = screen.get_height() // 2
  pg.draw.circle(screen, circle_color, (x, y), size)
  pg.image.save(screen, filename)

def create_cell(genome):
  colors = {'red': (255,0,0), 'green': (0,255,0), 'blue': (0,0,255), 'darkBlue': (0,0,128),
            'white': (255,255,255), 'black': (0,0,0), 'pink': (255,200,200)}
  if genome == '00':
    screen = pg.display.set_mode((20, 20))
    screen.fill(colors['black'])
    pg.image.save(screen, 'images/' + genome + '.png')
  elif genome == '01':
    screen = pg.display.set_mode((40, 20))
    screen.fill(colors['white'])
    pg.draw.rect(screen, colors['black'], (0, 0, 20, 20))
    pg.draw.polygon(screen, colors['black'], [(20, 0), (20, 20), (40, 10)])
    pg.image.save(screen, 'images/' + genome + '.png')
  elif genome == '10':
    screen = pg.display.set_mode((60, 20))
    screen.fill(colors['white'])
    pg.draw.polygon(screen, colors['black'], [(20, 0), (40, 0), (40, 20), (20, 20)])
    pg.draw.polygon(screen, colors['black'], [(0, 10), (20, 0), (20, 20)])
    pg.draw.polygon(screen, colors['black'], [(40, 0), (40, 20), (60, 10)])
    pg.image.save(screen, 'images/' + genome + '.png')
  else:
    screen = pg.display.set_mode((60, 40))
    screen.fill(colors['white'])
    pg.draw.polygon(screen, colors['black'], [(20, 0), (40, 0), (40, 20), (20, 20)])
    pg.draw.polygon(screen, colors['black'], [(0, 10), (20, 0), (20, 20)])
    pg.draw.polygon(screen, colors['black'], [(40, 0), (40, 20), (60, 10)])
    pg.draw.polygon(screen, colors['black'], [(20, 20), (40, 20), (30, 40)])
    pg.image.save(screen, 'images/' + genome + '.png')

def create_triangle(colors):
  screen = pg.display.set_mode((20, 20))
  screen.fill(colors['white'])
  # pg.draw.polygon(screen, colors['black'], [(0, 10), (20, 0), (20, 20)])
  pg.draw.polygon(screen, colors['black'], [(0, 0), (0, 20), (20, 10)])
  pg.image.save(screen, 'images/triangle.png')

if __name__ == '__main__':
  colors = {'red': (255,0,0), 'green': (0,255,0), 'blue': (0,0,255), 'darkBlue': (0,0,128),
            'white': (255,255,255), 'black': (0,0,0), 'pink': (255,200,200)}

  # create_circle(30, colors['black'], colors['white'], 'images/black_circle_30.png')
  # create_triangle(colors)
  create_cell('11')


import nltk
from nltk.tokenize import sent_tokenize
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('gutenberg')

from tqdm import tqdm
import string
from collections import Counter
from flair.models import SequenceTagger
from flair.data import Sentence

book = nltk.corpus.gutenberg.raw('carroll-alice.txt')
chapters = book.split('CHAPTER')

summary = ''
print(f"NER MODEL")
# For each chapter, run NER
for i in range(1, len(chapters)):

    temp = chapters[i]
    title = temp.split('\n')[0]
    chapters[i] = chapters[i].replace('\n', ' ')
    chapters[i] = chapters[i].replace('\r', ' ')
    chapters[i] = chapters[i].replace('\'', ' ')
    sent = sent_tokenize(chapters[i])

    # Flair named entity recognition model
    tagger = SequenceTagger.load('ner')

    # Get all the characters names and locations
    characters = []
    locations = []
    for line in tqdm(sent):
        sentence = Sentence(line)

        tagger.predict(sentence)
        for entity in sentence.get_spans('ner'):

            # If person, add to characters list
            if entity.get_label("ner").value == 'PER':
                characters.append(entity.text)

            # If location, add to location list
            elif entity.get_label("ner").value == 'LOC':
                locations.append(entity.text)

    # Remove any punctuation within the names
    names = []
    for name in characters:
        names.append(name.translate(str.maketrans('', '', string.punctuation)))

    # List characters by the frequency with which they are mentioned
    result = [item for items, c in Counter(characters).most_common() for item in [items] * c]

    common = []
    main_freq = []

    # Manually remove words that are not character names from our list
    not_names = ['Well', 'Ive', 'Five', 'Theyre', 'Dont', 'Wow', 'Ill', 'Miss', 'Hush', 'Yes', ]

    for n, c in Counter(names).most_common():
        if  n not in not_names:
            main_freq.append((n, c))
            common.append(n)

    summary += f"Chapter{title}:\n    Character List: {common}\n    Locations: {list(set(locations))}\n"
    summary += "---------------------------------------------\n"

with open('charactersLocations.txt', 'w') as f:
    f.write(summary)


from tempfile import NamedTemporaryFile
from typing import Any, Dict, List, Tuple

from parlai.core.image_featurizers import ImageLoader
from parlai.core.message import Message
from parlai.core.worlds import validate
from parlai.crowdsourcing.tasks.model_chat.utils import Compatibility, get_image_src
from parlai.crowdsourcing.tasks.model_chat.worlds import (
    BaseModelChatWorld,
    get_bot_worker,
)

class ModelImageChatWorld(BaseModelChatWorld):
    """
    A chat world in which an image is shown to the worker and bot at the beginning.
    """

    def __init__(self, opt, agent, bot, image_idx: int, image_act: Message):
        super().__init__(opt, agent=agent, bot=bot)

        self.image_stack = opt['image_stack']
        self.image_idx = image_idx
        self.image_act = image_act

        # Get a stringified version of the image to show the user
        orig_image = self.image_act['image']
        self.image_src = get_image_src(image=orig_image)

        # Get a featurized version of the image to show the bot
        with NamedTemporaryFile(suffix='.jpg') as f:
            orig_image.save(f)
            image_loader = ImageLoader(self.bot.model_agent.opt)
            self.image_act.force_set('image', image_loader.load(f.name))

    def _run_initial_turn(self) -> None:
        """
        Show the image to the human and bot, and show the bot's response to the human.
        """

        system_id = 'SYSTEM'
        system_agent_idx = None

        # Show the image to the human
        image_act_for_human = {
            'episode_done': False,
            'id': system_id,
            'text': f"""Welcome! You'll now have a conversation with your partner.

<-- FIRST, YOUR PARTNER WILL SAY SOMETHING ABOUT THIS IMAGE TO YOUR LEFT.

Be sure to talk about this image a little bit before discussing other things!
""",
            'task_data': {'image_src': self.image_src},
            'agent_idx': system_agent_idx,
        }
        self.agent.observe(validate(image_act_for_human))

        # Show the image to the bot
        image_act = {
            **self.image_act,
            'episode_done': False,
            'id': system_id,
            'agent_idx': system_agent_idx,
        }
        self.bot.observe(validate(image_act))
        del image_act['image']
        # Don't save the image features to disk

        # Have the bot respond
        bot_first_act_raw = self.bot.act()
        bot_first_act_raw = Message(
            Compatibility.maybe_fix_act(bot_first_act_raw)
        ).json_safe_payload()
        bot_first_act_raw['id'] = self.bot.agent_id
        self.agent.observe(validate(bot_first_act_raw))
        bot_first_act = {
            'episode_done': False,
            'id': bot_first_act_raw['id'],
            'text': bot_first_act_raw['text'],
            'agent_idx': 1,
        }

        # Record lines of dialogue
        self.dialog.append(image_act)
        self.dialog.append(bot_first_act)

    def _postprocess_acts(self, acts: List[dict], agent_idx: int):
        """
        Show the bot the image again on every turn.
        """
        if agent_idx == 0:
            # Add the image to every human act, seen by the bot. Also adds in any other
            # image-related fields needed by the model
            for key, value in self.image_act.items():
                if key not in ['episode_done', 'id', 'text', 'agent_idx']:
                    acts[agent_idx][key] = value

    def get_final_chat_data(self) -> Dict[str, Any]:
        """
        Add image-specific fields to the final chat data.
        """
        data = super().get_final_chat_data()
        data['image_idx'] = self.image_idx
        return data

    def _prepare_acceptability_checking(self) -> Tuple[List[str], List[str]]:
        """
        Apply acceptability checking params specific to image-chat conversation.

        The conversation starts with an image, so the human shouldn't be starting their
        first message with "Hi", etc.
        """
        human_messages, violation_types = super()._prepare_acceptability_checking()
        violation_types.append('penalize_greetings')
        return human_messages, violation_types

    def shutdown(self):

        if not self.chat_done:
            # If the HIT was not completed, remove this worker from the stack
            worker = self.agent.mephisto_agent.get_worker().db_id
            self.image_stack.remove_worker_from_stack(
                worker=worker, stack_idx=self.image_idx
            )

        self.agent.shutdown()

def make_world(opt, agents):

    # We are showing an image to the worker and bot, so grab the image path and other
    # context info
    image_idx, model_name, no_more_work = opt['image_stack'].get_next_image(
        agents[0].mephisto_agent.get_worker().db_id
    )
    full_image_context = opt['image_contexts'][image_idx]
    if no_more_work:
        # There are no more HITs for this worker to do, so give them a qualification
        agents[0].mephisto_agent.get_worker().grant_qualification(
            qualification_name=opt['block_qualification'], value=1
        )

    # Get a bot agent
    bot_worker = get_bot_worker(opt=opt, model_name=model_name)

    return ModelImageChatWorld(
        opt=opt,
        agent=agents[0],
        bot=bot_worker,
        image_idx=image_idx,
        image_act=full_image_context['image_act'],
    )

def get_world_params():
    return {"agent_count": 1}


import argparse
import shutil
import numpy as np
from matplotlib import image,pyplot
import os
import cv2
import json

parser = argparse.ArgumentParser()
parser.add_argument('--MaskedImageFolder', type=str)
parser.add_argument('--FullImageFolder', type=str)
args = parser.parse_args()

pathX = os.path.join(args.MaskedImageFolder, "X/")
pathY = os.path.join(args.MaskedImageFolder, "Y/")

savepathX =  os.path.join(args.FullImageFolder, "X/")
savepathY =  os.path.join(args.FullImageFolder, "Y/")

flist = os.path.join(args.FullImageFolder, "imagefiles.flist")
jsonfile = os.path.join(args.MaskedImageFolder, "maskdata.json")
shutil.copy(jsonfile, args.FullImageFolder)

filesX = sorted(os.listdir(pathX))

# flist file creation#
if not os.path.exists(flist):
    os.mknod(flist)

fo = open(flist,"w")
with open(jsonfile, encoding='utf-8') as jfile:
    mask_info = json.loads(jfile.read())
    for fileX in filesX:
        imgX = cv2.imread(os.path.join(pathX,fileX), -1) #masked image
        imgY = cv2.imread(os.path.join(pathY, fileX), -1) #mask
        if imgX.ndim == 3:
            origIm = imgX + imgY
            mask = np.zeros_like(imgX[:, :, 0])
            for i in range(len(mask_info[fileX])):
                x, y, size = mask_info[fileX][i]
                mask[x:x+size, y:y+size] = 1
            # Save original image and the corresponding mask
            cv2.imwrite(savepathX + fileX, origIm)
            pyplot.imsave(savepathY + fileX, mask, cmap='gray')
            # Write the file paths to flist file
            fo.write("%s\n" % (savepathX + fileX))
        elif imgX.ndim == 2:
            if imgY.ndim == 3:
                imgY = imgY[:,:,0]
            origIm = imgX + imgY
            mask = np.zeros_like(imgX)
            for i in range(len(mask_info[fileX])):
                x, y, size = mask_info[fileX][i]
                mask[x:x + size, y:y + size] = 1
            # Save original image and the corresponding mask
            pyplot.imsave(savepathX + fileX, origIm, cmap='gray')
            pyplot.imsave(savepathY + fileX, mask, cmap='gray')
            # Write the file paths to flist file
            fo.write("%s\n" % (savepathX + fileX))


import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import time

MIN_MATCH_COUNT = 10

img_sample = cv.cvtColor(cv.imread("./img/dataset/9.png",cv.IMREAD_COLOR),cv.COLOR_BGR2GRAY)

img_q = cv.cvtColor(cv.imread("./img/query/3.png",cv.IMREAD_COLOR),cv.COLOR_BGR2GRAY)

sift = cv.SIFT_create()

keypoints_1, descriptors_1 = sift.detectAndCompute(img_q, None)
keypoints_2, descriptors_2 = sift.detectAndCompute(img_sample, None)

outImage_1 = cv.drawKeypoints(img_q, keypoints_1,None)
outImage_2 = cv.drawKeypoints(img_sample, keypoints_2,None)

print(len(keypoints_1))
print(len(keypoints_2))

#cv.imwrite('image.jpg', outImage_1)
#cv.waitKey(0)

# BFMatcher
def BFMatcher(descript_1,descript_2):
    bf = cv.BFMatcher()
    matches = bf.knnMatch(descript_1,descript_2,k=2)
    return matches

# FLANNMatcher
def FLANNMatcher(descript_1,descript_2):
    FLANN_INDEX_KDTREE = 1
    index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
    search_params = dict(checks = 50)
    flann = cv.FlannBasedMatcher(index_params, search_params)
    matches = flann.knnMatch(descript_1,descript_2,k=2)
    return matches

start = time.time()
matches = BFMatcher(descriptors_1,descriptors_2)
#matches = FLANNMatcher(descriptors_1,descriptors_2)
end = time.time()
print("time cost: ",end-start)

# ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append(m)
# cv.drawMatchesKnn expects list of lists as matches.
print("match pairs: ", len(good))
# img4 = cv.drawMatchesKnn(img_q,keypoints_1,img_sample,keypoints_2,good,None,flags=cv.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
# plt.imshow(img4),plt.show()

if len(good)>MIN_MATCH_COUNT:
    src_pts = np.float32([ keypoints_1[m.queryIdx].pt for m in good ]).reshape(-1,1,2)
    dst_pts = np.float32([ keypoints_2[m.trainIdx].pt for m in good ]).reshape(-1,1,2)
    M, mask = cv.findHomography(src_pts, dst_pts, cv.RANSAC,5.0)
    matchesMask = mask.ravel().tolist()
    h,w = img_q.shape
    pts = np.float32([ [0,0],[0,h-1],[w-1,h-1],[w-1,0] ]).reshape(-1,1,2)
    dst = cv.perspectiveTransform(pts,M)
    #box_color = (0,0,255)
    sample_draw = cv.merge((img_sample.copy(),img_sample.copy(),img_sample.copy()))
    img_sample_detected = cv.polylines(sample_draw,[np.int32(dst)],True,(255,0,0),5, cv.LINE_AA)
else:
    print( "Not enough matches are found - {}/{}".format(len(good), MIN_MATCH_COUNT) )
    matchesMask = None
query_draw = cv.merge((img_q.copy(),img_q.copy(),img_q.copy()))
draw_params = dict(matchColor = (0,255,0), # draw matches in green color
                   singlePointColor = None,
                   matchesMask = matchesMask, # draw only inliers
                   flags = 2)
img3 = cv.drawMatches(query_draw,keypoints_1,img_sample_detected,keypoints_2,good,None,**draw_params)
plt.imshow(img3, 'gray'),plt.show()


import argparse
import os
import subprocess
import sys
from pathlib import Path

_SWIFT_PATH = Path(__file__).resolve().parents[1]

_KNOWN_SCRIPT_PATHS = [
    _SWIFT_PATH / "benchmark/scripts/Benchmark_Driver",
    _SWIFT_PATH / "benchmark/scripts/Benchmark_DTrace.in",
    _SWIFT_PATH / "benchmark/scripts/Benchmark_GuardMalloc.in",
    _SWIFT_PATH / "benchmark/scripts/Benchmark_QuickCheck.in",
    _SWIFT_PATH / "benchmark/scripts/Benchmark_RuntimeLeaksRunner.in",
    _SWIFT_PATH / "benchmark/scripts/run_smoke_bench",
    _SWIFT_PATH / "docs/scripts/ns-html2rst",
    _SWIFT_PATH / "test/Driver/Inputs/fake-toolchain/ld",
    _SWIFT_PATH / "utils/80+-check",
    _SWIFT_PATH / "utils/backtrace-check",
    _SWIFT_PATH / "utils/build-script",
    _SWIFT_PATH / "utils/check-incremental",
    _SWIFT_PATH / "utils/coverage/coverage-build-db",
    _SWIFT_PATH / "utils/coverage/coverage-generate-data",
    _SWIFT_PATH / "utils/coverage/coverage-query-db",
    _SWIFT_PATH / "utils/coverage/coverage-touch-tests",
    _SWIFT_PATH / "utils/dev-scripts/blockifyasm",
    _SWIFT_PATH / "utils/dev-scripts/split-cmdline",
    _SWIFT_PATH / "utils/gyb",
    _SWIFT_PATH / "utils/line-directive",
    _SWIFT_PATH / "utils/PathSanitizingFileCheck",
    _SWIFT_PATH / "utils/recursive-lipo",
    _SWIFT_PATH / "utils/round-trip-syntax-test",
    _SWIFT_PATH / "utils/rth",
    _SWIFT_PATH / "utils/run-test",
    _SWIFT_PATH / "utils/scale-test",
    _SWIFT_PATH / "utils/submit-benchmark-results",
    _SWIFT_PATH / "utils/swift_build_support/tests/mock-distcc",
    _SWIFT_PATH / "utils/symbolicate-linux-fatal",
    _SWIFT_PATH / "utils/update-checkout",
    _SWIFT_PATH / "utils/viewcfg",
]

_INSTALL_BLACK_MESSAGE = """\
The black Python package is required for formatting, but it was not found on
your system.

You can install it using:

    python3 -m pip install black

For more help, see https://black.readthedocs.io.
"""

def _get_python_sources():
    """Returns a list of path objects for all known Python sources in the Swift
    project.
    """

    return list(_SWIFT_PATH.rglob("*.py")) + _KNOWN_SCRIPT_PATHS

def _is_package_installed(name):
    """Runs the pip command to check if a package is installed.
    """

    command = [
        sys.executable,
        "-m",
        "pip",
        "show",
        "--quiet",
        name,
    ]

    with open(os.devnull, "w") as devnull:
        status = subprocess.call(command, stderr=devnull)

    return not status

def parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument(
        "paths",
        type=Path,
        metavar="PATH",
        nargs="*",
        help="Source path to format.",
    )

    parser.add_argument(
        "--check",
        action="store_true",
        help="Don't write the files back, just return the status.",
    )

    parser.add_argument(
        "-v",
        "--verbose",
        action="store_true",
        help="Emit messages to stderr about files that were not changed.",
    )

    parser.add_argument(
        "--diff",
        action="store_true",
        help="Don't write the files back, just output a diff for each file on stdout.",
    )

    parser.add_argument(
        "-S",
        "--skip-string-normalization",
        action="store_true",
        help="Don't normalize string quotes or prefixes.",
    )

    return parser.parse_args()

def main():
    args = parse_args()

    if not _is_package_installed("black"):
        print(_INSTALL_BLACK_MESSAGE)
        return 1

    command = [
        sys.executable,
        "-m",
        "black",
        "--target-version",
        "py38",
    ]

    if args.check:
        command.append("--check")
    if args.verbose:
        command.append("--verbose")
    if args.diff:
        command.append("--diff")
    if args.skip_string_normalization:
        command.append("--skip-string-normalization")

    requested_paths = [path.resolve() for path in args.paths]

    # Narrow down the set of paths to format to only those paths which are either
    # included in the set of requested paths or are subpaths of the requested paths.
    format_paths = {
        known_path
        for path in requested_paths
        for known_path in _get_python_sources()
        if path == known_path or path in known_path.parents
    }

    # Add requested paths that exists, but aren't included in the format set.
    for path in requested_paths:
        if path not in format_paths and path.exists():
            format_paths.add(path)

    command += sorted([str(path) for path in format_paths])

    return subprocess.call(command)

if __name__ == "__main__":
    sys.exit(main())


"""
10:05 시작

"""

import sys
import copy
from collections import deque

def func():
    N, L, R = map(int, sys.stdin.readline().split())
    land = []

    for i in range(N):
        land.append(list(map(int, sys.stdin.readline().split())))

    if N == 1:
        return 0

    time = 0

    while time < 2000:  # 2000일 이상은 주어지지 않음
        time += 1

        visited = [[False for i in range(N)] for j in range(N)]  # BFS로 인구 이동이 일어나는 곳을 체크하기 위함

        tmp_lands = copy.deepcopy(land)
        lands_cnt = 0  # 인구 이동에 포함된 나라의 수

        plus_x = [1, -1, 0, 0]
        plus_y = [0, 0, 1, -1]

        for i in range(N):  # 인구 이동을 할 나라가 있는지 탐색
            for j in range(N):
                if visited[i][j]: continue

                # BFS로 열려 있는 국경선끼리 인구 공유

                lands_cnt += 1

                people_sum = land[i][j]  # 국경선이 인접한 나라의 인구의 합
                country_num = 1  # 국경선이 인접한 나라의 개수
                countries = [(i, j)]

                dq = deque()
                dq.append((i, j))
                visited[i][j] = True

                # BFS를 하며 인접한 국가 탐색
                while dq:
                    tup = dq.popleft()
                    y = tup[0]
                    x = tup[1]

                    for k in range(4):
                        nx = x + plus_x[k]
                        ny = y + plus_y[k]

                        if 0 <= nx < N and 0 <= ny < N and not visited[ny][nx] and L <= abs(land[y][x] - land[ny][nx]) <= R:
                            visited[ny][nx] = True
                            dq.append((ny, nx))

                            people_sum += land[ny][nx]
                            country_num += 1
                            countries.append((ny, nx))

                if len(countries) == N * N:  # 모든 나라를 방문할 수 있는 경우
                    return time

                for country in countries:  # 나라 인구의 합의 평균 값을 대입
                    tmp_lands[country[0]][country[1]] = people_sum // country_num

        if lands_cnt == N * N:  # 모든 나라가 인접하지 않은 경우
            return time - 1

        land = copy.deepcopy(tmp_lands)

    if time >= 2001:
        return 0

print(func())