from typing import List, Tuple
import util
import random
import copy

def neighborhood_search(data: Tuple[List[int], List[int], List[int]]):

    (items, bags) = util.process_data(data)

    items.sort(key=lambda x: x.benefit, reverse=True)
    bags.sort(key=lambda x: x.capacity, reverse=False)

    # Greedy search
    for item in items:
        for bag in bags:
            if bag.capacity_left >= item.weight:
                bag.add_item(item)
                break

    util.sync(bags, items)
    best_solution = [item.bag for item in items]
    best_total_profit = util.get_total_profit_of_bags(bags)
    best_bags = copy.deepcopy(bags)
    best_items = copy.deepcopy(items)

    # Neighborhood search
    local_optima_flag = False
    while not local_optima_flag:
        continue_flag = False
        item_to_change = util.get_best_leftover(items)
        if item_to_change == None:
            print("Neighborhood search: No item leftover. \
                Best solution has been found.")
            return best_total_profit

        last_bags = copy.deepcopy(bags)
        last_items = copy.deepcopy(items)
        for i in range(len(bags)):
            bag = bags[i]
            next_bag = bags[0 if i == len(bags) - 1 else i + 1]
            rotate = False
            # Select two items in two bags to rotate
            for item_1 in bag.items:
                if rotate:
                    break
                for item_2 in next_bag.items:
                    if rotate:
                        break
                    if bag.capacity_left + item_1.weight >= item_to_change.weight \
                        and next_bag.capacity_left + item_2.weight >= item_1.weight:
                        rotate = True
                        temp_item = item_2
                        next_bag.remove_item(item_2)
                        next_bag.add_item(item_1)
                        bag.remove_item(item_1)
                        bag.add_item(item_to_change)
                        item_to_change = temp_item

            util.sync(bags, items)
            for item in items:
                if item.bag == -1:
                    for bag in bags:
                        if bag.capacity_left >= item.weight:
                            bag.add_item(item)
                            break

            util.sync(bags, items)
            current_solution = [item.bag for item in items]
            current_total_profit = util.get_total_profit_of_bags(bags)
            print(f"i: {i}", end=" ")
            print(f"rotate: {rotate}", end=" ")
            print(f"current_total_profit: {current_total_profit}")

            # Update best solution
            if current_total_profit > best_total_profit:
                best_solution = current_solution
                best_total_profit = current_total_profit
                best_bags = copy.deepcopy(bags)
                best_items = copy.deepcopy(items)
                print(f"update best_total_profit: {best_total_profit}")
                continue_flag = True

            # Reset bags and items
            bags = copy.deepcopy(last_bags)
            items = copy.deepcopy(last_items)
            util.sync(bags, items)
            item_to_change = util.get_best_leftover(items)

        print(f"continue: {continue_flag}")
        # Enter next neighborhood starting point
        if continue_flag:
            bags = copy.deepcopy(best_bags)
            items = copy.deepcopy(best_items)
        else:
            local_optima_flag = True
    print("Neighborhood search done.")
    print()
    return best_total_profit


import boto3
from opensearchpy import OpenSearch, RequestsHttpConnection,AWSV4SignerAuth
import csv

# Reference: https://docs.aws.amazon.com/opensearch-service/latest/developerguide/search-example.html
# Reference: https://docs.aws.amazon.com/lambda/latest/dg/python-package.html

region = 'us-east-1'
credentials = boto3.Session().get_credentials()
auth = AWSV4SignerAuth(credentials, region)

host = 'search-restaurants-6vnek5tpsoommj6acyiww2vdue.us-east-1.es.amazonaws.com'
index = 'restaurants'
url = host + '/' + index + '/restaurant'

# Lambda execution starts here
def lambda_handler(event, context):
    es = OpenSearch(
        hosts=[{'host': host, 'port': 443}],
        use_ssl=True,
        verify_certs=True,
        http_auth=auth,
        connection_class=RequestsHttpConnection
    )
    # Elasticsearch 6.x requires an explicit Content-Type header
    headers = { "Content-Type": "application/json" }

    with open('restaurant.csv', newline='') as file:
        reader = csv.reader(file)
        restaurants = list(reader)
        restaurants = restaurants[1:]
    for r in restaurants:
        body = {
            'RestaurantID': r[0],
            'Cuisine': r[3]
        }
        es.index(index="restaurants", id=r[0], body=body)


import jax
import jax.numpy as jnp

def xy_grid(grid_points, x_max=1, y_max=1):
    """Createes a grid of size (grid_points**2, 2) in the range of [-x_max, x_max] x [-y_max, y_max]"""
    x_grid_space = jnp.linspace(-x_max, x_max, num=grid_points)
    y_grid_space = jnp.linspace(-x_max, y_max, num=grid_points)
    X, Y = jnp.meshgrid(x_grid_space, y_grid_space, indexing="ij")
    xy_grid = jnp.column_stack((X.flatten(), Y.flatten()))
    return xy_grid

def create_dataset_sampler(initial_value_function, num_states):
    # num_states := in global state space including time before apply a transform_fn
    # for example for dubins3d it would be 4  (t x y theta)

    def dataset_sampler(key, dataset_state):
        coords_key, time_key = jax.random.split(key)
        # subtract 1 since we're going to generate a seperate time column
        coords = jax.random.uniform(
            coords_key, (dataset_state.batch_size, num_states - 1), minval=-1, maxval=1
        )

        if dataset_state.counter < dataset_state.pretrain_end:
            time = jnp.ones((dataset_state.batch_size, 1)) * dataset_state.t_min
        else:
            time = (
                dataset_state.t_min
                + jax.random.uniform(
                    time_key,
                    (dataset_state.batch_size, 1),
                    minval=0,
                    maxval=(dataset_state.t_max - dataset_state.t_min)
                    * jnp.minimum(
                        (dataset_state.counter / dataset_state.counter_end), 1.0
                    ),  # lerp time up to t_max
                )
                # / t_max  # ensure all inputs are in [-1, 1]
            )

        time = time.at[-dataset_state.samples_at_t_min :].set(dataset_state.t_min)
        normalized_tcoords = jnp.concatenate((time, coords), axis=-1)  # (b, [t c])
        boundary_values = initial_value_function(normalized_tcoords)

        dirichlet_mask = (time == dataset_state.t_min).flatten()

        dataset_state = dataset_state.replace(counter=dataset_state.counter + 1)

        return (
            dataset_state,
            normalized_tcoords,
            {
                "source_boundary_values": boundary_values,
                "dirichlet_mask": dirichlet_mask,
            },
        )

    return dataset_sampler


from collections import defaultdict

class ID:
    def __init__(self, num, str):
        self.num = num
        self.str = str

inp = [ (i.split('\n'))[0] for i in open("AOC_19.txt").readlines()]
idx = inp.index('split')
inp_2 = inp[idx+1:]
inp = inp[0:idx]
print(inp)
print(inp_2)
#inp = inp[0:inp.find("split")]
#inp2 = inp[inp.find("split"):]
inp = [i.split('|') for i in inp]
sub_array= []
not_processed = []#stores keys that are yet to be processed (initially those that are not just a character)
adj_list = defaultdict(list)#creates an adjacency list structure for the input
pot_strings = defaultdict(list) #stores potential strings that can be formed from a combination of given rules
def if_solve(case):
    for elem in case:
        if(elem in not_processed):
            return False
    return True
def solve_it(I_D,case):
    pos_strings = []
    ret_arr = []
    for elem in case:
        tmp_arr = pot_strings[elem]
        pos_strings.append(tmp_arr)
    #print(pos_strings)
    #max number of dependencies seems to be 3
    if(len(pos_strings)==3):
        for j in pos_strings[0]:
            for k in pos_strings[1]:
                for l in pos_strings[2]:
                    if( str(j)+str(k)+str(l) not in pot_strings[I_D]):
                        pot_strings[I_D].append(j+k+l)
    elif(len(pos_strings)==2):
        print(I_D)
        print(pos_strings[0])
        print(pos_strings[1])
        for j in pos_strings[0]:
            for k in pos_strings[1]:
                print("TWO DEPENDENCIES: " + str(j) + " " + str(k) )
                if( (j+k) not in pot_strings[I_D]):
                    pot_strings[I_D].append( (j+k) )
    elif(len(pos_strings)==1):
        for e in pos_strings:
            pot_strings[I_D].append(e)
def is_number(string):
    for char in string:
        if(not char.isdigit() ):
            return False
    return True
def parse_input():
    for j in inp:
        k = j[0]
        tmp_k = k
        sub = k[k.find(':')+1:]
        enter = []
        cur_idx = 0
        sub=sub.split()
        print(sub)
        for elem in sub:
            if( not is_number(elem) ):
                continue
            enter.append( int(elem) )
        if(len(enter)>0):
            adj_list[int(tmp_k[0:tmp_k.find(':')])].append(enter)
        if(len(adj_list[int(tmp_k[0:tmp_k.find(':')])])==0 ):
            if(int(tmp_k[0:tmp_k.find(':')])==97):
                pot_strings[int(tmp_k[0:tmp_k.find(':')])].append(tmp_k[5])
            else:
                pot_strings[int(tmp_k[0:tmp_k.find(':')])].append(tmp_k[4])
            s = int(tmp_k[0:tmp_k.find(':')])
            sub_array.append(s)
        if(len(j)==1):
            continue
        else:
            for idx in range(1,len(j)):
                enter2 = []
                cur_idx = 0
                j[idx] = j[idx].split()
                for elem in j[idx]:
                    if(not is_number(elem) ):
                        continue
                    enter2.append( int(elem) )
                adj_list[int(j[0][0:j[0].find(':')])].append(enter2)
def solve_part_one():
    for a,b in adj_list.items():
        if(a not in sub_array):
            not_processed.append(a)
        else:
            print("VALID: " + str(a))
    #print(not_processed)

    while(len(not_processed)>0):
        print(pot_strings.items())
        print('__________________________________')
        removal = []
        for j in range(len(not_processed)):
            amt = 0
            ls = adj_list[not_processed[j]]
            for lis in ls:
                if(if_solve(lis)):
                    solve_it(not_processed[j], lis)
                    amt += 1
            if(amt == len(adj_list[not_processed[j]])):
                removal.append(not_processed[j])
        for r in removal:
            not_processed.remove(r)

parse_input()

#print(adj_list.items())
solve_part_one()

set_strings = pot_strings[0]
print(set_strings)
ans = 0
for s in set_strings:
    if(s in inp_2):
        ans += 1
print(ans)


import argparse
import os

parser = argparse.ArgumentParser(description='Convert VCF from delly to browser extendable format')
parser.add_argument('-i','--input', dest='input', help='Input delly vcf')
parser.add_argument('-s','--samplename', dest='samplename', help='Samplename')
parser.add_argument('-o','--outdir', dest='outdir', help='Outdir')
args = parser.parse_args()

'''
Script converts the vcf-file into bed and bedpe formats :
1. Deletions => bed
2. Inversion => bed
3. Tandem duplication => bed
4. Translocation => bedpe
'''

# Output
out_del = open(os.path.join(args.outdir, args.samplename + "_deletions.bed"), "w")
out_inv = open(os.path.join(args.outdir, args.samplename + "_inversions.bed"), "w")
out_dup = open(os.path.join(args.outdir, args.samplename + "_dup.bed"), "w")
out_bnd = open(os.path.join(args.outdir, args.samplename + "_translocations.bedpe"), "w")

# Iterate over input lines
with open(args.input) as infile:
    for ln in infile:
        if ln.startswith("#"):
            if ln.startswith("#CHROM"):
                cols = ln.split("\t")
                chrom_idx = cols.index("#CHROM")
                pos_idx = cols.index("POS")
                id_idx = cols.index("ID")
                ref_idx = cols.index("REF")
                alt_idx = cols.index("ALT")
                qual_idx = cols.index("QUAL")
                filter_idx = cols.index("FILTER")
                info_idx = cols.index("INFO")
        else:
            # Split col
            cols = ln.split("\t")

            # Find if sv site location is PRECISE or not
            sv_site_location = cols[info_idx].split(";")[0]
            precise = "Yes" if sv_site_location == "PRECISE" else "No"

            # Parse info field as dictionary
            info_dc = dict([tuple(item.split("=")) for item in cols[info_idx].split(";")[1:]])

            # Get SV type
            svtype = info_dc["SVTYPE"]

            # Construct the BEDPE fields
            chrom1 = cols[chrom_idx]
            start1 = str(int(cols[pos_idx]) - 1)

            if svtype == "BND":

                # End
                end1 = cols[pos_idx]

                # Chromosome 2
                chrom2 = info_dc["CHR2"]

                # Start 2
                start2 = str(int(info_dc["POS2"]) - 1)

                # End 2
                end2 = info_dc["POS2"]

            else:

                end1 = info_dc["END"]

            name = cols[id_idx]
            score = "."
            strand1 = "."
            strand2 = "."

            # Custom info column
            filter_status = cols[filter_idx]

            # Extract mapping qualities
            mapq = info_dc["MAPQ"]

            # Paired-end signature induced connection type
            CT = info_dc["CT"]

            if "PE" in info_dc.keys():
                pe = info_dc["PE"]
            else:
                pe = "0"

            if "SR" in info_dc.keys():
                sr = info_dc["SR"]
            else:
                sr = "0"

            if "SRMAPQ" in info_dc.keys():
                srmapq = info_dc["SRMAPQ"]
            else:
                srmapq = "NA"

            # Info string
            info = f'FILTER={filter_status},PRECISE={precise},MAPQ={mapq},SRMAPQ={srmapq},CT={CT},PE={pe},SR={sr}'

            if svtype == "BND":
                # IF SV is a translocation produce bedpe
                out_bnd.write("\t".join([chrom1, start1, end1,
                                                 chrom2, start2, end2,
                                                 name, score, strand1,
                                                 strand2, svtype, info]) + "\n")
            elif svtype == "DEL":
                # If SV is a deletion produce bed
                out_del.write("\t".join([chrom1, start1, end1,
                                                 name, score, strand1,
                                                 svtype, info]) + "\n")

            elif svtype == "DUP":
                # If SV is duplication produce bed
                out_dup.write("\t".join([chrom1, start1, end1,
                                                 name, score, strand1,
                                                 svtype, info]) + "\n")
            else:
                # If SV is an inversion produce bed
                out_inv.write("\t".join([chrom1, start1, end1,
                                                 name, score, strand1,
                                                 svtype, info]) + "\n")
out_del.close()
out_inv.close()
out_dup.close()
out_bnd.close()


import numpy as np
from scipy import signal, butter, filtfilt

data_in = np.zeros((37, 2000))
fs = 500

eeg_data = data_in[np.array([3, 4, 12, 13, 14, 21, 28, 29]), :]

eeg_data = signal.resample(eeg_data, int(eeg_data.shape[1] / 10), axis=1)  # Resample along axis 1
fs = 50

def butter_highpass(cutoff, fs, order=2):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='high', analog=False)
    return b, a

b, a = butter_highpass(1, fs, order=2)
eeg_data = filtfilt(b, a, eeg_data, axis=1)

EEG_prevwin = eeg_data[:, 0:99]
EEG = eeg_data[:, 100:199]

numchans = EEG.shape[0]
points = 0
for chan_num in selected_channels:
    # Previous window
    data_prevwin = EEG_prevwin[chan_num-1, :]
    t = np.arange(1, len(data_prevwin) + 1) / fs

    # Generate sinusoids at 4 Hz and 7 Hz
    sinusoid4Hz = np.sin(2 * np.pi * 4 * t)
    sinusoid7Hz = np.sin(2 * np.pi * 7 * t)

    # Compute the cross-correlation
    cross_corr_4Hz_prevwin = np.correlate(data_prevwin, sinusoid4Hz, mode='full')
    cross_corr_7Hz_prevwin = np.correlate(data_prevwin, sinusoid7Hz, mode='full')

    # Current window
    data = EEG[chan_num-1, :]
    t = np.arange(1, len(data) + 1) / fs

    # Generate sinusoids at 4 Hz and 7 Hz
    sinusoid4Hz = np.sin(2 * np.pi * 4 * t)
    sinusoid7Hz = np.sin(2 * np.pi * 7 * t)

    # Compute the cross-correlation
    cross_corr_4Hz = np.correlate(data, sinusoid4Hz, mode='full')
    cross_corr_7Hz = np.correlate(data, sinusoid7Hz, mode='full')

    if cross_corr_4Hz > cross_corr_4Hz_prevwin:
        if cross_corr_7Hz < cross_corr_7Hz_prevwin:
            points = points+1
    else:
        if cross_corr_4Hz < cross_corr_4Hz_prevwin:
            if cross_corr_7Hz > cross_corr_7Hz_prevwin:
                points = points-1

if points>0:
    output = 'left'
elif points<0:
    output = 'right'
else:
    output = 'stay'


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

import errno
import os
import re
import sys
import zlib
from time import strftime

import PyPDF2

import dirarchpdf
import extrac

carp = input("¿En qué carpeta desea hacer la búsqueda? ")

rdf = input("Buscar en todo [1] o en un rango de fechas [2]: ")

if rdf == "2":
    fecha_init = input("Fecha inicial: ")
    fecha_fina = input("Fecha final: ")
    busqueda = dirarchpdf.Dir(carp, fecha_init, fecha_fina)
else:
    busqueda = dirarchpdf.Dir(carp)

pdb = input("Ingrese su búsqueda: ")

rutas = dirarchpdf.Dir.buspdfall(busqueda)

print("Se realizará la búsqueda en {} archivos: ".format(len(rutas)))

try:
    os.makedirs("resultados")
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

try:
    resultado = open("resultados/{}.txt".format(pdb), "a")
except OSError as e:
    if e.errno != errno.EEXIST:
        raise

print("Buscando archivos pdf en el directorio indicado...")

resultado.write(strftime("%d %b %Y %H:%M"))

for nomarc in rutas:
    nomarch = nomarc.lstrip()
    open(nomarch, 'rb')
    print("Leyendo: " + nomarch)
    mipdf = open(nomarch, 'rb')
    buscar = re.compile(pdb, flags=re.IGNORECASE)
    try:
        caja_busq = extrac.pdfbusq(mipdf, buscar)
        if len(caja_busq) > 1:
            resultado.write(
                "\n Se encontraron {} resultados en {} \n {}".format(len(caja_busq) - 1, nomarch, str(caja_busq)))
            print(caja_busq)
        else:
            print("Sin resultados")
    except (KeyError, PyPDF2.utils.PdfReadError, NotImplementedError, zlib.error, ValueError):
        error_log = open("error_log.txt", "a")
        error_log.write(
            "\n# [{}] No se pudo leer el archivo {}. Error: {} \n".format(strftime("%d %b %Y %H:%M"), nomarch,
                                                                          sys.exc_info()))
        error_log.close()
        pass

resultado.close()

print("Finalizó la búsqueda :)")
print("El archivo está guardado en {}".format(os.getcwd()))


from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from django.db import models

# Create your models here.

class UserDetails(models.Model):
    user_id = models.ForeignKey(User,null=True, on_delete=models.DO_NOTHING)
    wallet = models.CharField(max_length=100, null=True)

class Node(models.Model):
    user_id = models.ForeignKey(User, null=True, on_delete=models.DO_NOTHING)
    parent_folder_id = models.IntegerField(null=True)
    folder_id = models.BigIntegerField(null=True)
    is_folder = models.BigIntegerField(null=True)
    name = models.CharField(max_length=100, null=True)
    location =  models.CharField(max_length=200, null=True)
    type = models.CharField(max_length=50, null=True)
    last_modified = models.DateField(null=True)


# !/usr/bin/env python3

# encoding: utf-8
import requests
import json
import sys

host = "127.0.0.1"
port = 8500

# consul中微服务名称
serviceNames = ""

def get_service():
    url = 'http://' + host + ':' + str(port) + '/v1/agent/services'
    data = requests.get(url).json()
    jsonStr = json.dumps(data)
    print('json ' + jsonStr)
    keys = []
    for k in data:
        serviceName = data[k]['Service']
        if serviceName in serviceNames:
            print(data[k]['Service'])
            print(data[k]['ID'])
            keys.append(k)
    return keys

def del_service(keys):
    url = 'http://' + host + ':' + str(port) + '/v1/agent/service/deregister/'
    for sid in keys:
        requests.put(url + sid)
        print('删除 ' + sid)

# python3 main.py 127.0.0.1 application-yierbao-sweb
if __name__ == '__main__':
    if len(sys.argv) < 3:
        print("请输入：python3 ", sys.argv[0], "[ip] [service-name1,service-name2...]")
    else:
        host = sys.argv[1]
        serviceNames = sys.argv[2]
        keys = get_service()
        del_service(keys)


import asyncio

import pytest
import requests
from binance import AsyncClient, Client
from ftx import FtxClient

@pytest.fixture
def event_loop():
    loop = asyncio.get_event_loop()
    yield loop
    loop.close()

def test_response_binance(event_loop):
    client = AsyncClient()
    response = event_loop.run_until_complete(
        client.get_order_book(symbol="BTCUSDT")
    )
    assert "bids" in response
    assert "asks" in response

def test_response_ftx():
    client = FtxClient()
    response = client.get_orderbook("BTC/USDT", 1)
    assert "bids" in response
    assert "asks" in response

def test_response_glm_btc():
    client = FtxClient()
    try:
        client.get_orderbook("GLM/BTC", 1)
        assert False
    except Exception as ex:
        pass

    client = Client()
    try:
        client.get_order_book(symbol="GLMBTC")
    except Exception as ex:
        print(ex)
        assert False


from typing import List
'''
In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order
of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the
given words are sorted lexicographicaly in this alien language.

Example 1:
Input: words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
Output: true
Explanation: As 'h' comes before 'l' in this language, then the sequence is sorted.

Example 2:
Input: words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
Output: false
Explanation: As 'd' comes after 'l' in this language, then words[0] > words[1], hence the sequence is unsorted.

Example 3:
Input: words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
Output: false
Explanation: The first three characters "app" match, and the second string is shorter (in size.) According to
lexicographical rules "apple" > "app", because 'l' > '∅', where '∅' is defined as the blank character which is less
than any other character (More info).

Constraints:
    1 <= words.length <= 100
    1 <= words[i].length <= 20
    order.length == 26
    All characters in words[i] and order are English lowercase letters.
'''

class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:

        # Make a hash table to lookup the order
        order_hashtable = {letter: index for index, letter in enumerate(order)}

        # xrange is really fast apparently so use it where you can
        # range is inclusive but we don't want to use the last position since we are comparing to the
        # next word in every step
        for i in range(len(words) - 1):
            word1 = words[i]
            word2 = words[i + 1]

            # An important thing to realize is that you only need to look for the first difference,
            # that is the point where you can determine sorted-ness and stop
            for letter_position in range(min(len(word1), len(word2))):
                if word1[letter_position] != word2[letter_position]:
                    if order_hashtable[word1[letter_position]] > order_hashtable[word2[letter_position]]:
                        return False
                    break
            else:
                if len(word1) > len(word2):
                    return False
        return True

mySolution = Solution()
mySolution.isAlienSorted()


import csv
import math
import random
import copy
from statistics import mean
from operator import add
import time

########

def randomNewPos():
    centre = [53.958365,-1.080271]
    radius = 0.00810135
    pos = [0,0]
    while ((math.pow(pos[0] - centre[0],2) + math.pow(pos[1] - centre[1],2)) > math.pow(radius,2)):
        pos = [random.uniform(53.9493635,53.9673665),random.uniform(-1.0892725,-1.0712695)]
    return pos

#

def distance(p1,p2):
    return(math.sqrt(math.pow(p1[0]-p2[0],2)+math.pow(p1[1]-p2[1],2)))

#

def distributor_fuction(distances,totalToDistribute):
    final_distribution =  []
    ind_weights = []
    dists_total = 0

    for i in range(len(distances)):
        dists_total = dists_total + (1/math.exp(distances[i]))

    for i in range(len(distances)):
        ind_weights.append((1/math.exp(distances[i])/dists_total))
        final_distribution.append(totalToDistribute * ind_weights[i])

    return final_distribution

########

#list of location names/positions:

genLocationsDict = {"Parliament Street": [53.958901,-1.081244],
                    "Coney Street": [53.959327,-1.084198],
                    "Stonegate": [53.960815,-1.083520],
                    "Micklegate": [53.957305,-1.086630],
                    "Church Street": [53.959973,-1.081135],
                    "Parliament Street at M&S": [53.958372,-1.080276]}

####

#initializations:

start = time.time()

bin_count = 10
bin_positions = [[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0],[0,0]]
for i in range(bin_count):
            bin_positions[i] = randomNewPos()

with open('inputdata.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)[1::]

for i in range(len(data)):
    # data preprocessing
    if (data[i][2] == ''):
        data[i][2] = 0
    else:
        data[i][2] = int(data[i][2])

    day = copy.copy(data[i][0][0:2])
    month = copy.copy(data[i][0][3:5])
    year = copy.copy(data[i][0][6:10])
    data[i][0] = year + "/" + month + "/" + day + data[i][0][10::]

timesteps = list(dict.fromkeys([i[0] for i in data]))
timesteps.sort()

####

#primary loop:

with open('output.csv', 'w', newline='') as dataout:
    outwriter = csv.writer(dataout, delimiter=',',quotechar='"', quoting=csv.QUOTE_MINIMAL)
    outwriter.writerow(["Bin ID", "Time", "Bin Position Y", "Bin Position X", "Received"])

    for t in timesteps:
        temp_gens = []
        gen_footfall_proportions = []
        gen_footfall_final_values = []
        bin_fills = [0] * bin_count

        for i in data:
            if i[0] == t: temp_gens.append(i)

        gens_total = sum([j[2] for j in temp_gens])
        avg_footfall = mean([j[2] for j in temp_gens]) #estimate the average footfall within city centre

        for i in range(len(temp_gens)):
            gen_footfall_proportions.append(temp_gens[i][2]/gens_total)
            gen_footfall_final_values.append(avg_footfall * gen_footfall_proportions[i])

        for i in range(bin_count):
            if (random.randint(0,99) < 5):
                bin_positions[i] = randomNewPos()

        for i in range(len(temp_gens)):
            #for each generating point
            bin_gen_distances = []

            for j in range(bin_count):
                #for each bin
                bin_gen_distances.append((distance(genLocationsDict[temp_gens[i][1]],bin_positions[j]))*200)

            bin_fills = list(map(add,distributor_fuction(bin_gen_distances,gen_footfall_final_values[i]),bin_fills))

        for k in range(bin_count):
            outwriter.writerow([k,t,bin_positions[k][0],bin_positions[k][1],(round(bin_fills[k]))])

end = time.time()

total_time = end - start

print("Complete. Time elapsed: " + str(total_time) + "s")
input("Press any key to continue...")


#!/usr/bin/env python3

import bs4
import os
import re

XML_PATH   : str = 'arq.xml'
HTML_DIR   : str = 'out/'
HTML_PATH  : str = 'index.html'
PATTERN    : re.Pattern = re.compile(r'(.+)\.xml$')
HTML_BEGIN : str = '''
<!DOCTYPE html>

        <meta charset="utf-8" />
'''
HTML_END   : str = '''

'''

def write_html(path : str, parent_tag : bs4.Tag):

    with open(path, 'w', encoding='utf-8') as hfh:

        hfh.write(HTML_BEGIN)
        hfh.write(f"\t\t<title>{path}</title>\n")
        hfh.write(f"\t\n\n")
        hfh.write('\t\n\n')

        for child in parent_tag.descendants:
            if child.string and child.name:
                hfh.write(f"\t\t
{child.name}: {child.string}
\n")

        hfh.write(HTML_END)

def main():

    def make_tuple(x : str) -> tuple[str, str]:
        basename : str = PATTERN.search(x)[1]
        return (f"{HTML_DIR}{x}", f"{HTML_DIR}{basename}.html")

    filenames : list[str] = map(
        make_tuple,
        filter(
            lambda x: PATTERN.search(x),
            os.listdir(HTML_DIR)
        )
    )

    for fn_tuple in filenames:

        xml_tree : bs4.BeautifulSoup

        with open(fn_tuple[0], 'r', encoding='utf-8') as xfh:

            xml_tree = bs4.BeautifulSoup(xfh, 'lxml-xml')

        parent_tag : bs4.Tag = xml_tree.ARQELEM

        write_html(fn_tuple[1], parent_tag)

if __name__ == '__main__':
    main()


from argparse import Namespace
from typing import Any, Dict, Optional, Union

from pytorch_lightning.core.lightning import LightningModule
from pytorch_lightning.loggers.base import LightningLoggerBase, rank_zero_experiment
from pytorch_lightning.utilities import _module_available
from pytorch_lightning.utilities.distributed import rank_zero_only, rank_zero_warn

_TESTTUBE_AVAILABLE = _module_available("test_tube")

if _TESTTUBE_AVAILABLE:
    from test_tube import Experiment
else:
    Experiment = None

class TestTubeLogger(LightningLoggerBase):
    r"""
    Log to local file system in `TensorBoard <https://www.tensorflow.org/tensorboard>`_ format
    but using a nicer folder structure (see `full docs <https://williamfalcon.github.io/test-tube>`_).

    Install it with pip:

    .. code-block:: bash

        pip install test_tube

    .. code-block:: python

        from pytorch_lightning import Trainer
        from pytorch_lightning.loggers import TestTubeLogger
        logger = TestTubeLogger("tt_logs", name="my_exp_name")
        trainer = Trainer(logger=logger)

    Use the logger anywhere in your :class:`~pytorch_lightning.core.lightning.LightningModule` as follows:

    .. code-block:: python

        from pytorch_lightning import LightningModule
        class LitModel(LightningModule):
            def training_step(self, batch, batch_idx):
                # example
                self.logger.experiment.whatever_method_summary_writer_supports(...)

            def any_lightning_module_function_or_hook(self):
                self.logger.experiment.add_histogram(...)

    Args:
        save_dir: Save directory
        name: Experiment name. Defaults to ``'default'``.
        description: A short snippet about this experiment
        debug: If ``True``, it doesn't log anything.
        version: Experiment version. If version is not specified the logger inspects the save
            directory for existing versions, then automatically assigns the next available version.
        create_git_tag: If ``True`` creates a git tag to save the code used in this experiment.
        log_graph: Adds the computational graph to tensorboard. This requires that
            the user has defined the `self.example_input_array` attribute in their
            model.
        prefix: A string to put at the beginning of metric keys.
    """

    __test__ = False
    LOGGER_JOIN_CHAR = '-'

    def __init__(
        self,
        save_dir: str,
        name: str = "default",
        description: Optional[str] = None,
        debug: bool = False,
        version: Optional[int] = None,
        create_git_tag: bool = False,
        log_graph: bool = False,
        prefix: str = '',
    ):
        if Experiment is None:
            raise ImportError('You want to use `test_tube` logger which is not installed yet,'
                              ' install it with `pip install test-tube`.')
        super().__init__()
        self._save_dir = save_dir
        self._name = name
        self.description = description
        self.debug = debug
        self._version = version
        self.create_git_tag = create_git_tag
        self._log_graph = log_graph
        self._prefix = prefix
        self._experiment = None

    @property
    @rank_zero_experiment
    def experiment(self) -> Experiment:
        r"""

        Actual TestTube object. To use TestTube features in your
        :class:`~pytorch_lightning.core.lightning.LightningModule` do the following.

        Example::

            self.logger.experiment.some_test_tube_function()

        """
        if self._experiment is not None:
            return self._experiment

        self._experiment = Experiment(
            save_dir=self.save_dir,
            name=self._name,
            debug=self.debug,
            version=self.version,
            description=self.description,
            create_git_tag=self.create_git_tag,
            rank=rank_zero_only.rank,
        )
        return self._experiment

    @rank_zero_only
    def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
        # TODO: HACK figure out where this is being set to true
        self.experiment.debug = self.debug
        params = self._convert_params(params)
        params = self._flatten_dict(params)
        self.experiment.argparse(Namespace(**params))

    @rank_zero_only
    def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> None:
        # TODO: HACK figure out where this is being set to true
        metrics = self._add_prefix(metrics)
        self.experiment.debug = self.debug
        self.experiment.log(metrics, global_step=step)

    @rank_zero_only
    def log_graph(self, model: LightningModule, input_array=None):
        if self._log_graph:
            if input_array is None:
                input_array = model.example_input_array

            if input_array is not None:
                self.experiment.add_graph(
                    model,
                    model.transfer_batch_to_device(
                        model.example_input_array, model.device)
                )
            else:
                rank_zero_warn('Could not log computational graph since the'
                               ' `model.example_input_array` attribute is not set'
                               ' or `input_array` was not given',
                               UserWarning)

    @rank_zero_only
    def save(self) -> None:
        super().save()
        # TODO: HACK figure out where this is being set to true
        self.experiment.debug = self.debug
        self.experiment.save()

    @rank_zero_only
    def finalize(self, status: str) -> None:
        super().finalize(status)
        # TODO: HACK figure out where this is being set to true
        self.experiment.debug = self.debug
        self.save()
        self.close()

    @rank_zero_only
    def close(self) -> None:
        super().save()
        # TODO: HACK figure out where this is being set to true
        self.experiment.debug = self.debug
        if not self.debug:
            exp = self.experiment
            exp.close()

    @property
    def save_dir(self) -> Optional[str]:
        return self._save_dir

    @property
    def name(self) -> str:
        if self._experiment is None:
            return self._name
        else:
            return self.experiment.name

    @property
    def version(self) -> int:
        if self._experiment is None:
            return self._version
        else:
            return self.experiment.version

    # Test tube experiments are not pickleable, so we need to override a few
    # methods to get DDP working. See
    # https://docs.python.org/3/library/pickle.html#handling-stateful-objects
    # for more info.
    def __getstate__(self) -> Dict[Any, Any]:
        state = self.__dict__.copy()
        state["_experiment"] = self.experiment.get_meta_copy()
        return state

    def __setstate__(self, state: Dict[Any, Any]):
        self._experiment = state["_experiment"].get_non_ddp_exp()
        del state["_experiment"]
        self.__dict__.update(state)


import torch.nn as nn
import torch
import os

from efficientnet_pytorch import EfficientNet
from torch.optim import lr_scheduler
from torchvision import transforms

from EfficientNet.attention import ChannelAttention, SpatialAttention
from Utils.loader import CustomDataset
from Utils.trainer import run_epochs_for_loop, Trainer

class EfficientNetWithAttention(nn.Module):

    def __init__(self, num_classes: int = 200):
        super(EfficientNetWithAttention, self).__init__()
        self.eff_model = EfficientNet.from_pretrained("efficientnet-b7")
        self._avg_pooling = nn.AdaptiveAvgPool2d(output_size=1)
        self._dropout = nn.Dropout(p=0.5, inplace=False)

        self.fc = nn.Linear(in_features=2560, out_features=num_classes, bias=True)
        self.ca_head = ChannelAttention(64)
        self.sa = SpatialAttention()
        self.ca_tail = ChannelAttention(2560)

    def forward(self, x):
        x = self.eff_model.extract_features(x)
        # 最后一层加入 Attention 机制
        x = self.ca_tail(x) * x
        x = self.sa(x) * x
        x = self._avg_pooling(x)
        if self.eff_model._global_params.include_top:
            x = x.flatten(start_dim=1)
            x = self._dropout(x)
            x = self.fc(x)
        return x

if __name__ == '__main__':
    data_dir = "../AI研习社_鸟类识别比赛数据集"
    selection = ["train_set", "val_set"]
    train_labels = os.path.join(data_dir, "train_pname_to_index.csv")
    valid_labels = os.path.join(data_dir, "val_pname_to_index.csv")
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                     std=[0.229, 0.224, 0.225])
    formats = {
        'train_set': [
            transforms.Resize(456),
            transforms.RandomHorizontalFlip(),
            transforms.RandomRotation(5),
            transforms.RandomCrop(456),
            transforms.ToTensor(),
            normalize,
        ],
        'val_set': [
            transforms.Resize(456),
            transforms.CenterCrop(456),
            transforms.ToTensor(),
            normalize,
        ],
    }
    data_label_paths = {
        "train_set": train_labels,
        "val_set": valid_labels,
    }
    data_sets = {}
    for one in selection:
        data_sets[one] = CustomDataset(os.path.join(data_dir, one),
                                       data_label_paths[one],
                                       transforms.Compose(formats[one]))
    # windows操作系统不支持 python 的多进程操作。
    # 而神经网络用到多进程的地方在数据集加载上，
    # 所以将 DataLoader 中的参数 num_workers 设置为 0即可
    data_loader = {
        "train":
            torch.utils.data.DataLoader(
                data_sets["train_set"],
                batch_size=2,
                shuffle=True,
                num_workers=0
            ),

        "valid":
            torch.utils.data.DataLoader(
                data_sets["val_set"],
                batch_size=5,
                shuffle=False,
                num_workers=0
        ),
    }
    model = EfficientNetWithAttention()
    criterion = nn.CrossEntropyLoss()
    model.to(device)
    criterion.to(device)

    params = filter(lambda p: p.requires_grad, model.parameters())
    optimizer = torch.optim.SGD(params, lr=0.003, momentum=0.9)

    # Decay LR by a factor of 0.1 every 7 epochs
    exp_lr_scheduler = lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)

    trainer = Trainer(model, criterion, optimizer, device)
    run_epochs_for_loop(trainer, 1,
                        data_loader["train"],
                        data_loader["valid"],
                        True,
                        exp_lr_scheduler)


import torch
from torch import nn

from einops import rearrange, repeat
from einops.layers.torch import Rearrange

import torch
import torch.nn as nn
import torch.nn.functional as F

class DoubleConv(nn.Module):
    """(convolution => [BN] => ReLU) * 2"""

    def __init__(self, in_channels, out_channels, mid_channels=None):
        super().__init__()
        if not mid_channels:
            mid_channels = out_channels
        self.double_conv = nn.Sequential(
            nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
            nn.BatchNorm2d(mid_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
            nn.BatchNorm2d(out_channels),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        return self.double_conv(x)

class Down(nn.Module):
    """Downscaling with maxpool then double conv"""

    def __init__(self, in_channels, out_channels):
        super().__init__()
        self.maxpool_conv = nn.Sequential(
            nn.MaxPool2d(2),
            DoubleConv(in_channels, out_channels)
        )

    def forward(self, x):
        return self.maxpool_conv(x)

class Up(nn.Module):
    """Upscaling then double conv"""

    def __init__(self, in_channels, out_channels, bilinear=True):
        super().__init__()

        # if bilinear, use the normal convolutions to reduce the number of channels
        if bilinear:
            self.up = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
            self.conv = DoubleConv(in_channels, out_channels, in_channels // 2)
        else:
            self.up = nn.ConvTranspose2d(in_channels , in_channels // 2, kernel_size=2, stride=2)
            self.conv = DoubleConv(in_channels, out_channels)

    def forward(self, x1, x2):
        x1 = self.up(x1)
        # input is CHW
        diffY = x2.size()[2] - x1.size()[2]
        diffX = x2.size()[3] - x1.size()[3]

        x1 = F.pad(x1, [diffX // 2, diffX - diffX // 2,
                        diffY // 2, diffY - diffY // 2])
        x = torch.cat([x2, x1], dim=1)
        return self.conv(x)

class OutConv(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(OutConv, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1)

    def forward(self, x):
        return self.conv(x)

# helpers

def pair(t):
    return t if isinstance(t, tuple) else (t, t)

# classes

class PreNorm(nn.Module):
    def __init__(self, dim, fn):
        super().__init__()
        self.norm = nn.LayerNorm(dim)
        self.fn = fn
    def forward(self, x, **kwargs):
        return self.fn(self.norm(x), **kwargs)

class FeedForward(nn.Module):
    def __init__(self, dim, hidden_dim, dropout = 0.):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(dim, hidden_dim),
            nn.GELU(),
            nn.Dropout(dropout),
            nn.Linear(hidden_dim, dim),
            nn.Dropout(dropout)
        )
    def forward(self, x):
        return self.net(x)

class Attention(nn.Module):
    def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0.):
        super().__init__()
        inner_dim = dim_head *  heads
        project_out = not (heads == 1 and dim_head == dim)

        self.heads = heads
        self.scale = dim_head ** -0.5

        self.attend = nn.Softmax(dim = -1)
        self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)

        self.to_out = nn.Sequential(
            nn.Linear(inner_dim, dim),
            nn.Dropout(dropout)
        ) if project_out else nn.Identity()

    def forward(self, x):
        qkv = self.to_qkv(x).chunk(3, dim = -1)
        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = self.heads), qkv)

        dots = torch.matmul(q, k.transpose(-1, -2)) * self.scale

        attn = self.attend(dots)

        out = torch.matmul(attn, v)
        out = rearrange(out, 'b h n d -> b n (h d)')
        return self.to_out(out)

class Transformer(nn.Module):
    def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout = 0.):
        super().__init__()
        self.layers = nn.ModuleList([])
        for _ in range(depth):
            self.layers.append(nn.ModuleList([
                PreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout)),
                PreNorm(dim, FeedForward(dim, mlp_dim, dropout = dropout))
            ]))
    def forward(self, x):
        for attn, ff in self.layers:
            x = attn(x) + x
            x = ff(x) + x
        return x

class ViT(nn.Module):
    def __init__(self, *, image_size, patch_size, dim, depth, heads, mlp_dim, pool = 'cls', channels = 512, dim_head = 64, dropout = 0., emb_dropout = 0.):
        super().__init__()
        image_height, image_width = pair(image_size)
        patch_height, patch_width = pair(patch_size)

        assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.'

        num_patches = (image_height // patch_height) * (image_width // patch_width)
        patch_dim = channels * patch_height * patch_width
        assert pool in {'cls', 'mean'}, 'pool type must be either cls (cls token) or mean (mean pooling)'

        self.to_patch_embedding = nn.Sequential(
            Rearrange('b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = patch_height, p2 = patch_width),
            nn.Linear(patch_dim, dim),
        )

        self.pos_embedding = nn.Parameter(torch.randn(1, num_patches + 1, dim))
        self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
        self.dropout = nn.Dropout(emb_dropout)

        self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim, dropout)

        self.pool = pool
        self.to_latent = nn.Identity()

    def forward(self, img):
        x = self.to_patch_embedding(img)
        b, n, _ = x.shape
        cls_tokens = repeat(self.cls_token, '() n d -> b n d', b = b)
        x = torch.cat((cls_tokens, x), dim=1)
        x += self.pos_embedding[:, :(n + 1)]
        x = self.dropout(x)
        x = self.transformer(x)
        x = x.mean(dim = 1) if self.pool == 'mean' else x[:, 0]
        x = self.to_latent(x)
        return x

class UNet(nn.Module):
    def __init__(self, n_channels, n_classes, bilinear=True):
        super(UNet, self).__init__()
        self.n_channels = n_channels
        self.n_classes = n_classes
        self.bilinear = bilinear

        self.inc = DoubleConv(n_channels, 64)
        self.down1 = Down(64, 128)
        self.down2 = Down(128, 256)
        self.down3 = Down(256, 512)
        factor = 2 if bilinear else 1
        self.down4 = Down(512, 1024 // factor)

        self.vit = ViT(image_size = 32,patch_size = 8,dim = 2048, depth = 2, heads = 16,mlp_dim = 12,channels = 512) #dim%head=0
        self.vit_conv = nn.Conv2d(32,512,kernel_size = 1,padding = 0) #to increase the number of channels
        self.vit_linear = nn.Linear(64,1024)

        self.up1 = Up(1024, 512 // factor, bilinear)
        self.up2 = Up(512, 256 // factor, bilinear)
        self.up3 = Up(256, 128 // factor, bilinear)
        self.up4 = Up(128, 64, bilinear)
        self.outc = OutConv(64, n_classes)

    def forward(self, x):
        x1 = self.inc(x)
        x2 = self.down1(x1)
        x3 = self.down2(x2)
        x4 = self.down3(x3)
        x5 = self.down4(x4)

        #applying Vision Transformer
        x6 = self.vit(x5) #[4,2048]
        x6 = torch.reshape(x6,(-1,32,8,8)) #[4,32,8,8]
        x7 = self.vit_conv(x6) #[4,512,8,8]
        x8 = self.vit_linear(torch.reshape(x7,(-1,512,64))) #[4,512,1024]
        x9 = torch.reshape(x8,(-1,512,32,32))

        x = self.up1(x9, x4)
        x = self.up2(x, x3)
        x = self.up3(x, x2)
        x = self.up4(x, x1)
        logits = self.outc(x)
        return logits

# Use as below
# device = torch.device('cuda') if torch.cuda.is_available else torch.device('cpu')
# model = UNet(n_channels=3, n_classes=1).to(device)
# model.load_state_dict(torch.load('./segmenation_best_model.pth'))
# model.eval()


#! usr/bin/env python
# -*- coding:utf-8 -*-
# created by zhaoguanhua 2017/9/25
# AtmosphericCorrection for Landsat8

import glob
import os
import sys
import tarfile
import re
import gdal
import numpy
from Py6S import *
from osgeo import gdal
import pdb
import shutil
import argparse
from .base import MeanDEM

def parse_arguments(argv):
    parser = argparse.ArgumentParser()

    parser.add_argument('--Input_dir',type=str,help='Input dir',default=None)
    parser.add_argument('--Output_dir',type=str,help='Output dir',default=None)

    return parser.parse_args(argv)

# 逐波段辐射定标
def RadiometricCalibration(BandId):
    # LandSat8 TM辐射定标参数
    global data2,ImgRasterData
    parameter_OLI = numpy.zeros((9,2))

    #计算辐射亮度参数
    parameter_OLI[0,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_1.+',data2)[0]).split("=")[1])
    parameter_OLI[1,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_2.+',data2)).split("=")[1])
    parameter_OLI[2,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_3.+',data2)).split("=")[1])
    parameter_OLI[3,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_4.+',data2)).split("=")[1])
    parameter_OLI[4,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_5.+',data2)).split("=")[1])
    parameter_OLI[5,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_6.+',data2)).split("=")[1])
    parameter_OLI[6,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_7.+',data2)).split("=")[1])
    parameter_OLI[7,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_8.+',data2)).split("=")[1])
    parameter_OLI[8,0] = float(''.join(re.findall('RADIANCE_MULT_BAND_9.+',data2)).split("=")[1])

    parameter_OLI[0,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_1.+',data2)[0]).split("=")[1])
    parameter_OLI[1,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_2.+',data2)).split("=")[1])
    parameter_OLI[2,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_3.+',data2)).split("=")[1])
    parameter_OLI[3,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_4.+',data2)).split("=")[1])
    parameter_OLI[4,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_5.+',data2)).split("=")[1])
    parameter_OLI[5,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_6.+',data2)).split("=")[1])
    parameter_OLI[6,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_7.+',data2)).split("=")[1])
    parameter_OLI[7,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_8.+',data2)).split("=")[1])
    parameter_OLI[8,1] = float(''.join(re.findall('RADIANCE_ADD_BAND_9.+',data2)).split("=")[1])

    Gain = parameter_OLI[int(BandId) - 1,0]
    Bias = parameter_OLI[int(BandId) - 1,1]

    RaCal = numpy.where(ImgRasterData>0 ,Gain * ImgRasterData + Bias,-9999)
    return (RaCal)

# 6s大气校正
def AtmosphericCorrection(BandId):
    global data
    # 6S模型
    s = SixS()

    s.geometry = Geometry.User()
    s.geometry.solar_z = 90-float(''.join(re.findall('SUN_ELEVATION.+',data2)).split("=")[1])
    s.geometry.solar_a = float(''.join(re.findall('SUN_AZIMUTH.+',data2)).split("=")[1])
    s.geometry.view_z = 0
    s.geometry.view_a = 0

    # 日期
    Dateparm = ''.join(re.findall('DATE_ACQUIRED.+',data2)).split("=")
    Date = Dateparm[1].split('-')

    s.geometry.month = int(Date[1])
    s.geometry.day = int(Date[2])

    # 中心经纬度
    point1lat = float(''.join(re.findall('CORNER_UL_LAT_PRODUCT.+',data2)).split("=")[1])
    point1lon = float(''.join(re.findall('CORNER_UL_LON_PRODUCT.+',data2)).split("=")[1])
    point2lat = float(''.join(re.findall('CORNER_UR_LAT_PRODUCT.+',data2)).split("=")[1])
    point2lon = float(''.join(re.findall('CORNER_UR_LON_PRODUCT.+',data2)).split("=")[1])
    point3lat = float(''.join(re.findall('CORNER_LL_LAT_PRODUCT.+',data2)).split("=")[1])
    point3lon = float(''.join(re.findall('CORNER_LL_LON_PRODUCT.+',data2)).split("=")[1])
    point4lat = float(''.join(re.findall('CORNER_LR_LAT_PRODUCT.+',data2)).split("=")[1])
    point4lon = float(''.join(re.findall('CORNER_LR_LON_PRODUCT.+',data2)).split("=")[1])

    sLongitude = (point1lon + point2lon + point3lon + point4lon) / 4
    sLatitude = (point1lat + point2lat + point3lat + point4lat) / 4

    # 大气模式类型
    if sLatitude > -15 and sLatitude <= 15:
        s.atmos_profile = AtmosProfile.PredefinedType(AtmosProfile.Tropical)

    if sLatitude > 15 and sLatitude <= 45:
        if s.geometry.month > 4 and s.geometry.month <= 9:
            s.atmos_profile = AtmosProfile.PredefinedType(AtmosProfile.MidlatitudeSummer)
        else:
            s.atmos_profile = AtmosProfile.PredefinedType(AtmosProfile.MidlatitudeWinter)

    if sLatitude > 45 and sLatitude <= 60:
        if s.geometry.month > 4 and s.geometry.month <= 9:
            s.atmos_profile = AtmosProfile.PredefinedType(AtmosProfile.SubarcticSummer)
        else:
            s.atmos_profile = AtmosProfile.PredefinedType(AtmosProfile.SubarcticWinter)

    # 气溶胶类型大陆
    s.aero_profile = AtmosProfile.PredefinedType(AeroProfile.Continental)

    # 目标地物？？？？？？
    s.ground_reflectance = GroundReflectance.HomogeneousLambertian(0.36)

    # 550nm气溶胶光学厚度,根据日期从MODIS处获取。
    #s.visibility=40.0
    s.aot550 = 0.14497

    # 通过研究去区的范围去求DEM高度。
    pointUL = dict()
    pointDR = dict()
    pointUL["lat"] = point1lat
    pointUL["lon"] = point1lon
    pointDR["lat"] = point4lat
    pointDR["lon"] = point2lon
    meanDEM = (MeanDEM(pointUL, pointDR)) * 0.001

    # 研究区海拔、卫星传感器轨道高度
    s.altitudes = Altitudes()
    s.altitudes.set_target_custom_altitude(meanDEM)
    s.altitudes.set_sensor_satellite_level()

    # 校正波段（根据波段名称）
    if BandId == '1':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B1)

    elif BandId == '2':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B2)

    elif BandId == '3':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B3)

    elif BandId == '4':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B4)

    elif BandId == '5':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B5)

    elif BandId == '6':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B6)

    elif BandId == '7':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B7)

    elif BandId == '8':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B8)

    elif BandId == '9':
        s.wavelength = Wavelength(PredefinedWavelengths.LANDSAT_OLI_B9)

    # 下垫面非均一、朗伯体
    s.atmos_corr = AtmosCorr.AtmosCorrLambertianFromReflectance(-0.1)

    # 运行6s大气模型
    s.run()

    xa = s.outputs.coef_xa
    xb = s.outputs.coef_xb
    xc = s.outputs.coef_xc
    x = s.outputs.values
    return (xa, xb, xc)

if __name__ == '__main__':

    #输入数据路径
    RootInputPath = parse_arguments(sys.argv[1:]).Input_dir
    RootOutName = parse_arguments(sys.argv[2:]).Output_dir

    #创建日志文件
    LogFile = open(os.path.join(RootOutName,'log.txt'),'w')

    for root,dirs,RSFiles in os.walk(RootInputPath):

        #判断是否进入最底层
        if len(dirs)==0:
            #根据输入输出路径建立生成新文件的路径
            RootInputPathList = RootInputPath.split(os.path.sep)
            RootList = root.split(os.path.sep)
            StartList = len(RootInputPathList)
            EndList = len(RootList)
            outname = RootOutName
            for i in range(StartList,EndList):
                if os.path.exists(os.path.join(outname,RootList[i]))==False:
                    os.makedirs(os.path.join(outname,RootList[i]))
                    outname=os.path.join(outname,RootList[i])
                else:
                    outname=os.path.join(outname,RootList[i])

            MeteDatas = glob.glob(os.path.join(root,'*MTL.txt'))

            for MeteData in MeteDatas:
                pass

            f = open(MeteData)
            data = f.readlines()
            data2 =' '.join(data)

            shutil.copyfile(MeteData,os.path.join(outname,os.path.basename(MeteData)))

            if len(os.path.basename(MeteData))<10:

                RSbands = glob.glob(os.path.join(root,"B0[1-8].tiff"))
            else:
                RSbands = glob.glob(os.path.join(root,"*B[1-8].TIF"))
            print('影像'+root+'开始大气校正')
            print(RSbands)
            for tifFile in RSbands:

                BandId = (os.path.basename(tifFile).split('.')[0])[-1]

                #捕捉打开数据出错异常
                try:
                    IDataSet = gdal.Open(tifFile)
                except Exception as e:
                    print("文件%S打开失败" % tifFile)
                    LogFile.write('\n'+tifFile+'数据打开失败')

                if IDataSet == None:
                    LogFile.write('\n'+tifFile+'数据集读取为空')
                    continue
                else:
                    #获取行列号
                    cols = IDataSet.RasterXSize
                    rows = IDataSet.RasterYSize
                    ImgBand = IDataSet.GetRasterBand(1)
                    ImgRasterData = ImgBand.ReadAsArray(0, 0, cols, rows)

                    if ImgRasterData is None:
                        LogFile.write('\n'+tifFile+'栅格数据为空')
                        continue
                    else:
                        #设置输出文件路径
                        outFilename=os.path.join(outname,os.path.basename(tifFile))

                        #如果文件存在就跳过，进行下一波段操作
                        if os.path.isfile(outFilename):
                            print("%s已经完成" % outFilename)
                            continue
                        else:
                            # #辐射校正
                            RaCalRaster = RadiometricCalibration(BandId)
                            #大气校正
                            a, b, c = AtmosphericCorrection(BandId)
                            y = numpy.where(RaCalRaster!=-9999,a * RaCalRaster - b,-9999)
                            atc = numpy.where(y!=-9999,(y / (1 + y * c))*10000,-9999)

                            driver = IDataSet.GetDriver()
                            #输出栅格数据集
                            outDataset = driver.Create(outFilename, cols, rows, 1, gdal.GDT_Int16)

                            # 设置投影信息，与原数据一样
                            geoTransform = IDataSet.GetGeoTransform()
                            outDataset.SetGeoTransform(geoTransform)
                            proj = IDataSet.GetProjection()
                            outDataset.SetProjection(proj)

                            outband = outDataset.GetRasterBand(1)
                            outband.SetNoDataValue(-9999)
                            outband.WriteArray(atc, 0, 0)
                print('第%s波段计算完成'%BandId)
            # print(root+'计算完成')
            print('\n')
    #关闭日志文件
    LogFile.close()


import sys
from collections import deque

n,m,k = map(int,sys.stdin.readline().split())

graph = [[0 for _ in range(m)] for _ in range(n)]

for _ in range(k):
    a,b = map(int,sys.stdin.readline().split())
    graph[a-1][b-1] = 1

dx = [-1,1,0,0]
dy = [0,0,1,-1]

visit = [[0 for _ in range(m)] for _ in range(n)]

def bfs(i,j):
    queue = deque()
    queue.append([i,j])
    visit[i][j] = 1
    sum = 1 # 넓이
    while queue:
        x,y = queue.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if 0 <= nx < n and 0 <= ny < m and visit[nx][ny] == 0:
                if graph[nx][ny] == 1:
                    visit[nx][ny] = 1
                    queue.append([nx,ny])
                    sum += 1
    return sum

result = 0 # 최대값 저장하는 변수
for i in range(n):
    for j in range(m):
        if graph[i][j]==1 and visit[i][j] == 0:
            result = max(result,bfs(i,j)) # 최대값
print(result)


import dash
from dash import no_update
from dash.dependencies import Input,Output,State
import dash_bootstrap_components as dbc
import dash_html_components as html
import dash_core_components as dcc

from utilities import fetchilarity_score

app = dash.Dash(__name__,external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server

app.layout = html.Div([
    dbc.Container(
        [
            dbc.Row(
                dbc.Col(
                    dcc.Markdown(
'''
# Fetchilarity

### Enter two bits of text to determine how similar they are!

Made with love by Russell.

'''
                    ),
                ),
                style=dict(textAlign='center')

            ),

            dbc.Row(
                [
                    # enter text1
                    dbc.Col(
                        [
                            dbc.Textarea(placeholder='Enter the first bit of text',id='text1'),
                        ],
                        width=6
                    ),
                    # enter text2
                    dbc.Col(
                        [
                            dbc.Textarea(placeholder='Enter the second bit of text',id='text2'),
                        ],
                        width=6
                    )
                ]
            ),
            html.Br(),

            dbc.Row(
                [
                    dbc.Col(
                        dbc.Button('SUBMIT',id='submit',color='primary',size='lg',block=True,n_clicks=0)
                    ),
                ],
            ),
            html.Br(),

            dbc.Row(
                [
                    dbc.Col(
                        [
                            html.Div(id='output',style=dict(textAlign='center'))
                        ]
                    )
                ]
            )
        ]
    )
])

@app.callback(
    Output('output','children'),
    [Input('submit','n_clicks')],
    [State('text1','value'),
     State('text2','value')]
)
def get_score(n_clicks,text1,text2):
    if n_clicks==0:
        return no_update

    try:
        score = fetchilarity_score(text1,text2,mode='api')
    except BaseException as e:
        print(text1,text2)
        print(e)
        return 'something went terribly wrong with that input. Please try again.'

    return dbc.Card(
        [
            html.H3('Fetchilarity Score',className='card-title'),
            html.H1(str(round(score,4)))
        ],
        body=True
    )

if __name__ == "__main__":
    app.run_server(
        port=8050,
        debug=True
    )


from collections import Counter
from typing import List

class Solution:
    def isPossibleDivide(self, nums: List[int], k: int) -> bool:
        nums.sort()
        left = Counter(nums)

        for n in nums:
            if left[n] == 0:
                continue
            for i in range(n, n+k):
                if left[i] == 0:
                    return False
                left[i] -= 1
        return True


from utils import *
from itertools import combinations
from math import comb, floor, ceil
import functools

def is_leaf(a):
    return isinstance(a, list) and all(isinstance(x, int) for x in a)

def infix(root):
    result = []
    def sub(root):
        if isinstance(root[0], int):
            result.append((root, 0))
        else:
            sub(root[0])
        if isinstance(root[1], int):
            result.append((root, 1))
        else:
            sub(root[1])
    sub(root)
    return result

def explode(a):
    inf = infix(a)
    def sub2(root, lvl):
        for i in [0, 1]:
            if lvl >= 3 and is_leaf(root[i]):
                tmp = root[i]
                root[i] = 0
                pos = next(i for i in range(len(inf)) if id(inf[i][0]) == id(tmp))
                if pos > 0:
                    l, p = inf[pos-1]
                    l[p] += tmp[0]
                if pos < len(inf)-2:
                    l, p = inf[pos+2]
                    l[p] += tmp[1]
                return True
            elif isinstance(root[i], list) and sub2(root[i], lvl+1):
                return True
        return False
    exploded = sub2(a, 0)
    return exploded

def split(a):
    def sub(root):
        if isinstance(root[0], int) and root[0] >= 10:
            root[0] = [floor(root[0] / 2), ceil(root[0] / 2)]
            return True
        elif isinstance(root[0], list) and sub(root[0]):
            return True
        elif isinstance(root[1], int) and root[1] >= 10:
            root[1] = [floor(root[1] / 2), ceil(root[1] / 2)]
            return True
        elif isinstance(root[1], list) and sub(root[1]):
            return True
        return False
    return sub(a)

def reduce(a):
    reduced = explode(a) or split(a)
    while reduced:
        reduced = explode(a) or split(a)

def add(a, b):
    tmp = [eval(repr(a)), eval(repr(b))]
    reduce(tmp)
    return tmp

def magnitude(a):
    if isinstance(a, int):
        return a
    return 3*magnitude(a[0]) + 2*magnitude(a[1])

lines = [eval(line) for line in readlines('18_input.txt')]
s = functools.reduce(add, lines)
print('Star 1:', magnitude(s))
print('Star 2:', max(max(magnitude(add(a, b)), magnitude(add(b, a))) for a, b in combinations(lines, 2)))


# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import sqlite3

class ProductLinkScraperPipeline:

    def __init__(self) :
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect("product.db")
        self.corr = self.conn.cursor()

    def create_table(self):
        self.corr.execute('''DROP TABLE IF EXISTS PRODUCTLINK''')
        self.corr.execute('''CREATE TABLE ProductLink (pnum integer primary key,
                                          title text,
                                          link text) ''')

    def process_item(self, item, spider):
        self.store_item(item)
        return item

    def store_item(self,item):
        self.corr.execute('''INSERT INTO PRODUCTLINK(title,link) VALUES (?,?)''',(item["title"],item["link"]))
        self.conn.commit()

class ProductDescScraperPipeline:

    def __init__(self) :
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect("product.db")
        self.corr = self.conn.cursor()

    def create_table(self):
        self.corr.execute('''DROP TABLE IF EXISTS PRODUCTDESC''')
        self.corr.execute('''create table productdesc (
            pid integer primary key,
            pnum integer not null,
            price integer,
            discount integer,
            rating real,
            n_ratings integer,
            n_reviews integer,
            foreign key (pnum) references productlink (pnum)
            ); ''')

        self.corr.execute('''DROP TABLE IF EXISTS PRODUCTSPECS''')
        self.corr.execute('''
            create table productspecs (
            pid integer primary key,
            pnum integer not null,
            brand text,
            Model_Number text,
            Type text,
            Suitable_For text,
            Processor_Brand text,
            Processor_Name text,
            SSD_Capacity_GB integer,
            HDD_Capacity_GB  integer,
            RAM_GB integer,
            Graphic_Processor text,
            Operating_System text,
            Screen_Size_cm real,
            Screen_Resolution text,
            foreign key (pnum) references productlink (pnum)
            ); ''')

    def process_item(self, item, spider):
        self.store_item(item)
        return item

    def store_item(self,item):

        price = int(item["price"][1:].replace(",","")) if item["price"] != "NULL" else 'NULL'
        discount = int(item["discount"].split("%")[0]) if item["discount"] != "NULL" else 'NULL'
        rating = float(item["rating"]) if item["rating"] != "NULL" else 'NULL'

        self.corr.execute('''INSERT INTO
        PRODUCTDESC(pnum,price,discount,rating,n_ratings,n_reviews)
              VALUES (?,?,?,?,?,?)''',(item["pnum"],price,discount,rating,item['n_ratings'],item['n_reviews']))

        self.conn.commit()

        brand = item["brand"].split(" ")[0] if item["brand"] != 'NULL' else 'NULL'

        ssd = item['SSD_Capacity'].split(" ") if item.get('SSD_Capacity',0) != 0 else 0

        if ssd !=0 and ssd[1] =='GB':
            SSD_Capacity_GB = int(ssd[0])
        elif ssd !=0 and ssd[1] =='TB':
            SSD_Capacity_GB = int(ssd[0])*1000
        else:
            SSD_Capacity_GB = 0

        hdd = item['HDD_Capacity'].split(" ") if item.get('HDD_Capacity',0) != 0 else 0

        if hdd !=0 and hdd[1] =='GB':
            HDD_Capacity_GB = int(hdd[0])
        elif hdd !=0 and hdd[1] =='TB':
            HDD_Capacity_GB = int(hdd[0])*1000
        else:
            HDD_Capacity_GB = 0

        RAM_GB = int(item['RAM'].split(" ")[0]) if item.get('RAM','NULL') != "NULL" else "NULL"
        ss = item['Screen_Size'].split(" ") if item.get('Screen_Size','NULL') != "NULL" else "NULL"
        if ss !='NULL' and ss[1] =='cm':
            Screen_Size_cm = float(ss[0])
        elif ss !='NULL' and ss[1] =='inch':
            Screen_Size_cm = float(ss[0])*2.54
        else:
            Screen_Size_cm = 'NULL'

        self.corr.execute('''INSERT INTO
        PRODUCTSPECS(pnum,brand,Model_Number,Type,Suitable_For,Processor_Brand,Processor_Name,SSD_Capacity_GB,
        HDD_Capacity_GB,RAM_GB,Graphic_Processor,Operating_System,Screen_Size_cm,Screen_Resolution)
        VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)''',
        (item["pnum"],brand,item.get('Model_Number','NULL'),item.get('Type','NULL'),item.get('Suitable_For','NULL'),item.get('Processor_Brand','NULL'),
        item.get('Processor_Name','NULL'),SSD_Capacity_GB,HDD_Capacity_GB,RAM_GB,item.get('Graphic_Processor','NULL'),item.get('Operating_System','NULL'),
        Screen_Size_cm,item.get('Screen_Resolution','NULL')))

        self.conn.commit()

class ProductReviewScraperPipeline:

    def __init__(self) :
        self.create_connection()
        self.create_table()

    def create_connection(self):
        self.conn = sqlite3.connect("product.db")
        self.corr = self.conn.cursor()

    def create_table(self):

        self.corr.execute('''DROP TABLE IF EXISTS PRODUCTREVIEWS''')
        self.corr.execute('''
                          create table ProductReviews (
                                        rid integer primary key,
                                        pnum integer not null,
                                        review_rating integer,
                                        Timestamp DATETIME DEFAULT CURRENT_TIMESTAMP not null,
                                        review_date text,
                                        location text,
                                        review_title text,
                                        review text,
                                        foreign key (pnum) references productlink (pnum)
                                        );
                          ''')

    def process_item(self, item, spider):
        self.store_item(item)
        return item

    def store_item(self,item):

        if item["location"][:2] == ", ":
            item["location"] = item["location"][2:]

        self.corr.execute('''INSERT INTO PRODUCTREVIEWS(pnum,review_rating,review_date,location,review_title,review)
             VALUES (?,?,?,?,?,?)''',(item["pnum"],item["review_rating"],item["review_date"],item["location"],item["review_title"],item["review"]))
        self.conn.commit()


#!/usr/bin/python

import string
import argparse
import random
import unidecode
import re

import load as Loader
from dictionaries import *
import onomono

from propernoun import *
from onomono import *
import unicodedata

from nltk.util import ngrams
import nltk
import nltk.tag

from textstat.textstat import textstat

from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.feature_extraction.text import *
import sklearn

from collections import Counter
from sets import Set

import ujson

#import hmm_model as markov

#NUM_GRAMS = 5
#MIN_PHRASE = 7
NUM_FICS = 10
OUTFILE="out.txt"

parser = argparse.ArgumentParser(description='Analyze scraped data.')
parser.add_argument('directory', metavar='dir',
                   help='directory to process')

args = parser.parse_args()
data = Loader.load_data_partial(args.directory,NUM_FICS)

print('The nltk version is {}.'.format(nltk.__version__))
print('The scikit-learn version is {}.'.format(sklearn.__version__))

def clean_line(line):
      cleanline = re.compile('[\\\\]')
      line = unidecode.unidecode(line)
      line = cleanline.sub('',line)
      return line

def proc_line(line):
   #
   whitespace = re.compile("[\s]+")
   punc_pfx = re.compile("^[^A-Za-z]+")
   punc_sfx = re.compile("[^A-Za-z]+$")
   alpha = re.compile("[A-Za-z]+((-|')[A-Za-z])?[A-Za-z]*")
   hyphen = re.compile("[-]+")
   poss = re.compile("'s")
   tokens = whitespace.split(line)
   nline = ""
   for tok in tokens:
      if len(tok) == 0:
         continue;

      pfx = punc_pfx.search(tok)
      sfx = punc_sfx.search(tok)
      word = alpha.match(tok)
      suff = ""
      pref = ""

      if word == None:
         nline += tok + " "
         continue;

      word = word.group(0)

      if pfx != None:
         pref = pfx.group(0)

      if sfx != None:
         suff = sfx.group(0)

      if "'s" in word:
         suff = "'s" + suff
         word = poss.split(word)[0]

      success,sfx = recog_sfx(word)

      if success:
         nline += pref+sfx+suff
         continue;

      if "-" in word:
         if is_proper_noun(word) and is_template(word):
               nname = get_template(word);
               nline += pref+nname+suff+" "
               continue;

         subwords = hyphen.split(word)
         all_words = True
         for subword in subwords:
            if defined_in_dict(subword) == False:
               all_words = False

         if all_words == True:
            nline += pref + word.lower() + suff + " ";
         else:
            if is_proper_noun(word) and not is_template(word):
               nname = add_template(word)
               nline += pref+nname+suff+" "

            elif is_proper_noun(word) and is_template(word):
               nname = get_template(word);
               nline += pref+nname+suff+" "

            else:
               nline += pref+ word + suff+" "

      else:
         if is_proper_noun(word) and is_template(word):
               nname = get_template(word);
               nline += pref+nname+suff+" "
               continue;

         if defined_in_dict(word):
            nline += tok + " "
         else:
            if is_proper_noun(word) and not is_template(word):
               nname = add_template(word)
               nline += pref+nname+suff+" "

            elif is_proper_noun(word) and is_template(word):
               nname = get_template(word);
               nline += pref+nname+suff+" "

            else:
               print("unknown",pref,word,suff)
               nline += pref+ word + suff+" "

   return nline;

def fic2text(ident,master):
   textsegs = Loader.get_field(data['fics'],ident,'fic')
   rtags = Loader.get_field(data['base'],ident,'tags')
   rtext = ""
   #tngms = []
   ttoks = Set([])
   atoks = []

   rtext = ""
   for line in textsegs:
      line = clean_line(line)
      line = proc_line(line)
      rtext += line

   #for i in range(0,len(frags)):
   #   if is_punc(frags[i]):
   #      continue
   #   print(frags[i])

   #master = markov.train([rtext],NUM_GRAMS,split_callback=split_line,master_dict=master)

   return rtext

print("==== Loaded. Getting Data.... =====")
ids = Loader.get_primaries(data['fics']);
vdata = []
vwords = Set([])
master = None

i=1;
ofh = open("out.txt","w")
for ident in ids:
   print(">> process "+str(ident)+" : # "+str(i))
   text=fic2text(ident,master)
   ofh.write(text)
   i+=1
   #qwords,qdata = fic2text(i)
   #vdata += qdata
   #vwords.update(qwords)

ofh.close()
print("==== Writing to File ======")
dump_template("names.templ")
#
print("==== Finished ======")

#print("===========")
#print("===== Finished Processing Ngrams ===")
#vstat = Counter(vdata)
#for k in vstat:
#   print(k,vstat[k])

#nwords = len(vwords)
#print("number of words:",nwords)

#HiddenMarkovModelTagger(nwords,)


from math import factorial
import numpy as np
import attr
from scipy import ndimage, signal, optimize

_TOPHAT_FACTOR = 0.01 #025   #05
_MEDWINSIZE = 299
_MEDMMSIZE = 1991

def b(txt):
    """ return a binary string aka bytes """
    return txt.encode('UTF-8')

def smooth_signal( raw_signal ):
    """ smooth signal using savitzky_golay algorithm """
    return savitzky_golay( raw_signal, 11, 7 )

def correct_baseline( signal ):
    """ use tophat morphological transform to correct for baseline """

    return ndimage.white_tophat(signal, None,
                np.repeat([1], int(round(signal.size * _TOPHAT_FACTOR)))
            )

@attr.s
class NormalizedTrace(object):
    signal = attr.ib()
    baseline = attr.ib()
    mma = attr.ib()
    mmb = attr.ib()

def func_mm(x, a, b):
    """ Michaelis Menten kinetics equation -
        this has a nice property of passing initial value and plateau
    """
    return a*x/(b+x)

def normalize_baseline( raw ):
    """ return mean, median, sd and smooth signal """

    median_line = signal.medfilt(raw, [_MEDWINSIZE])
    baseline = signal.savgol_filter( median_line, _MEDWINSIZE, 7)
    corrected_baseline = raw - baseline
    np.maximum(corrected_baseline, 0, out=corrected_baseline)
    smooth = correct_baseline( signal.savgol_filter(corrected_baseline, 11, 7) )

    # perform michaelis-menten equation for baseline assessment
    mm_line = signal.medfilt(raw, [ _MEDMMSIZE ])
    xx = np.linspace(0, len(raw) )
    try:
        popt, pcov = optimize.curve_fit(func_mm, xx, mm_line)
    except:
        # michaelis menten are not appropriate for this scale
        popt = pcov = [0, 0]

    return NormalizedTrace( signal=smooth, baseline = baseline, mma = popt[0], mmb = popt[1] )

def search_peaks( signal, cwt_widths, min_snr ):
    """ returns [ (peak, height, area) ], ... ] """

    # find all peaks by cwt-based algorithm
    indices = find_peaks_cwt( signal, cwt_widths, min_snr=min_snr )

    if not indices:
        return []

    # find absolute heights

    raw_peaks = []
    for idx in indices:
        for i in range(3, -1, -1):
            try:
                height, index = max( [ (signal[i], i) for i in range(idx-3, idx+3) ] )
            except IndexError:
                continue
            break
        if height < params.min_height:
            continue
        if index < 0:
            continue
        raw_peaks.append( (index, height) )

    if not raw_peaks:
        return []

    # calculate area
    peaks = []
    for (peak, height) in raw_peaks:
        area, brtime, ertime = calculate_area( signal, peak, 5e-2 )
        peaks.append( (peak, height, area, brtime, ertime) )

    return peaks

def calculate_area(y, t, threshold):
    """ return (area, begin_rtime, end_rtime)
    """

    # right area
    data = y[t:]
    r_area, ertime, r_shared = half_area(data, threshold)

    # left area
    data = y[:t+1][::-1]
    l_area, brtime, l_shared= half_area(data, threshold)

    return ( l_area + r_area - y[t], t - brtime, ertime + t )

def half_area(y, threshold):
    """ return (area, ertime)
    """

    winsize = 3
    threshold = threshold/2
    shared = False
    area = y[0]
    edge = float(np.sum(y[0:winsize]))/winsize
    old_edge = 2 * edge

    index = 1
    limit = len(y)

    while edge > area * threshold and edge < old_edge and index < limit:
        old_edge = edge
        area += y[index]
        edge = float(np.sum(y[index:index+winsize]))/winsize
        index += 1
    if edge >= old_edge:
        shared = True
    index -= 1

    return area, index, shared

@attr.s
class TraceChannel(object):
    dye_name = attr.ib()
    dye_wavelength = attr.ib()
    raw_channel = attr.ib()
    smooth_channel = attr.ib()
    median = attr.ib()
    mean = attr.ib()
    sd = attr.ib()
    max_height = attr.ib()
    min_height = attr.ib()

def separate_channels( trace ):
    # return a list of [ 'dye name', dye_wavelength, numpy_array, numpy_smooth_baseline ]

    from fatools.lib.fautil.traceio import WAVELENGTH

    results = []
    for (idx, data_idx) in [ (1,1), (2,2), (3,3), (4,4), (5,105) ]:
        try:
            dye_name = trace.get_data(b('DyeN%d' % idx)).decode('UTF-8')
            #dye_wavelength = trace.get_data(b('DyeW%d' % idx))

            # below is to workaround on some strange dye names
            if dye_name == '6FAM': dye_name = '6-FAM'
            elif dye_name == 'PAT': dye_name = 'PET'
            elif dye_name == 'Bn Joda': dye_name = 'LIZ'

            try:
                dye_wavelength = trace.get_data(b('DyeW%d' % idx))
            except KeyError:
                dye_wavelength = WAVELENGTH[dye_name]

            raw_channel = np.array( trace.get_data(b('DATA%d' % data_idx)) )
            nt = normalize_baseline( raw_channel )

            results.append( TraceChannel(dye_name, dye_wavelength, raw_channel,
                            nt.signal, np.median(nt.baseline), np.mean(nt.baseline),
                            np.std(nt.baseline), np.max(nt.baseline), np.min(nt.baseline))
            )
        except KeyError:
            pass

    return results

def savitzky_golay(y, window_size, order, deriv=0, rate=1):
    r"""Smooth (and optionally differentiate) data with a Savitzky-Golay filter.
    The Savitzky-Golay filter removes high frequency noise from data.
    It has the advantage of preserving the original shape and
    features of the signal better than other types of filtering
    approaches, such as moving averages techniques.
    Parameters
    ----------
    y : array_like, shape (N,)
        the values of the time history of the signal.
    window_size : int
        the length of the window. Must be an odd integer number.
    order : int
        the order of the polynomial used in the filtering.
        Must be less then `window_size` - 1.
    deriv: int
        the order of the derivative to compute (default = 0 means only smoothing)
    Returns
    -------
    ys : ndarray, shape (N)
        the smoothed signal (or it's n-th derivative).
    Notes
    -----
    The Savitzky-Golay is a type of low-pass filter, particularly
    suited for smoothing noisy data. The main idea behind this
    approach is to make for each point a least-square fit with a
    polynomial of high order over a odd-sized window centered at
    the point.
    Examples
    --------
    t = np.linspace(-4, 4, 500)
    y = np.exp( -t**2 ) + np.random.normal(0, 0.05, t.shape)
    ysg = savitzky_golay(y, window_size=31, order=4)
    import matplotlib.pyplot as plt
    plt.plot(t, y, label='Noisy signal')
    plt.plot(t, np.exp(-t**2), 'k', lw=1.5, label='Original signal')
    plt.plot(t, ysg, 'r', label='Filtered signal')
    plt.legend()
    plt.show()
    References
    ----------
    .. [1] A. Savitzky, M. J. E. Golay, Smoothing and Differentiation of
       Data by Simplified Least Squares Procedures. Analytical
       Chemistry, 1964, 36 (8), pp 1627-1639.
    .. [2] Numerical Recipes 3rd Edition: The Art of Scientific Computing
       W.H. Press, S.A. Teukolsky, W.T. Vetterling, B.P. Flannery
       Cambridge University Press ISBN-13: 9780521880688
    """
    import numpy as np
    from math import factorial

    try:
        window_size = np.abs(np.int(window_size))
        order = np.abs(np.int(order))
    except ValueError as msg:
        raise ValueError("window_size and order have to be of type int")
    if window_size % 2 != 1 or window_size < 1:
        raise TypeError("window_size size must be a positive odd number")
    if window_size < order + 2:
        raise TypeError("window_size is too small for the polynomials order")
    order_range = range(order+1)
    half_window = (window_size -1) // 2
    # precompute coefficients
    b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
    m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
    # pad the signal at the extremes with
    # values taken from the signal itself
    firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
    lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
    y = np.concatenate((firstvals, y, lastvals))
    return np.convolve( m[::-1], y, mode='valid')

def smooth(x,window_len=11,window='hanning'):
    """smooth the data using a window with requested size.

    This method is based on the convolution of a scaled window with the signal.
    The signal is prepared by introducing reflected copies of the signal
    (with the window size) in both ends so that transient parts are minimized
    in the begining and end part of the output signal.

    input:
        x: the input signal
        window_len: the dimension of the smoothing window; should be an odd integer
        window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
            flat window will produce a moving average smoothing.

    output:
        the smoothed signal

    example:

    t=linspace(-2,2,0.1)
    x=sin(t)+randn(len(t))*0.1
    y=smooth(x)

    see also:

    numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
    scipy.signal.lfilter

    TODO: the window parameter could be the window itself if an array instead of a string
    NOTE: length(output) != length(input), to correct this: return y[(window_len/2-1):-(window_len/2)] instead of just y.
    """

    if x.ndim != 1:
        raise ValueError( "smooth only accepts 1 dimension arrays." )

    if x.size < window_len:
        raise ValueError( "Input vector needs to be bigger than window size." )

    if window_len<3:
        return x

    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
        raise ValueError( "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'")

    s=np.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]
    #print(len(s))
    if window == 'flat': #moving average
        w=np.ones(window_len,'d')
    else:
        w=eval('np.'+window+'(window_len)')

    y=np.convolve(w/w.sum(),s,mode='valid')
    return y


import cv2
import argparse

# img = cv2.imread("./logo.png")
# cv2.imshow("LOGO", img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

parse = argparse.ArgumentParser()
parse.add_argument("path_image", help="path to input the image")

args = parse.parse_args()

img = cv2.imread(args.path_image)
cv2.imshow("LOGO", img)

args_dict = vars(parse.parse_args())  # {"path_image" : "./logo.png"}
img2 = cv2.imread(args_dict["path_image"])
cv2.imshow("logo_two", img2)
cv2.waitKey(0)

cv2.destroyAllWindows()


#
# Author: KEEN LITTLE
#
# Date: 29 March 2021
#
# Assignment Step 214, Python Course
#   The Tech Academy Software Development Bootcamp
#
# Purpose: to practice with python3 and sqlite3, showing how SQL queries can be
#   written and automated with Python.
#
################################################################################

import sqlite3

fileList = ('information.docx', 'hello.txt', 'myImage.png', \
            'myMovie.mpg', 'World.txt', 'data.pdf', 'myPhoto.jpg')
            # Here are the file names that we're pulling into the database.

def createTable():
    conn = sqlite3.connect('pysql.db')
    with conn:
        cur = conn.cursor() # Creating a database with two fields:
        cur.execute("CREATE TABLE IF NOT EXISTS tbl_textFiles( \
            ID INTEGER PRIMARY KEY AUTOINCREMENT, \
            col_fileName VARCHAR(50) \
            )") # We have a primary key and a varchar (string) for the names.
        conn.commit()
    conn.close()

"""
txtFileList = ()

def sortFiles(): # I'm just gonna grab the files I want
    for i in fileList:
	if i.endswith('.txt'):
	    txtFileList.append(i) # And dump 'em into a new list! Easier.
""" # Better idea: sort this with an if statement when I populate the table

def filesToTable():
    conn = sqlite3.connect('pysql.db')
    for item in fileList: # Iterating through the tuple,
        if item.endswith('.txt'): # Here we grab only the .txt files
            with conn:
                cur = conn.cursor()
                cur.execute("INSERT INTO tbl_textFiles (col_fileName) VALUES (?)", (item,))
                print(item)
                conn.commit()
    conn.close()

if __name__ == "__main__":

    createTable()
#    sortFiles()
    filesToTable()


""" Importamos el modelo del archivo en que lo definimos. """
from model_robots import StorageModel

""" matplotlib lo usaremos crear una animación de cada uno de los pasos
    del modelo. """
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.animation as animation
plt.rcParams["animation.html"] = "jshtml"
matplotlib.rcParams['animation.embed_limit'] = 2**128

""" Importamos los siguientes paquetes para el mejor manejo de valores
    numéricos."""
import numpy as np
import pandas as pd

""" Definimos otros paquetes que vamos a usar para medir el tiempo de
    ejecución de nuestro algoritmo. """
import time
import datetime

M = int(input("dame el numero de filas "))

N = int(input("deme el numero de columnas "))

numAgents = int(input("Dame el numero de Agentes "))

numBoxes = int(input("Dame el numero de Cajas "))

MAXTIME = int(input("Tiempo máximo de ejecución en segundos "))

""" Registramos el tiempo de inicio y ejecutamos la simulación. """
start_time = time.time()
model = StorageModel(M, N,numAgents,numBoxes)

frames =-1
while datetime.timedelta(seconds=(time.time() - start_time)< MAXTIME):
    frames+=1
    if model.unsortedBoxes==0:
        break
    model.step()

print('Tiempo de ejecución:', str(datetime.timedelta(seconds=(time.time() - start_time))))
print('Número de movimientos realizados por todos los agentes', model.agentMovements)

""" Obtenemos la información que almacenó el colector, este nos entregará un
    DataFrame de pandas que contiene toda la información. """
all_grid = model.datacollector.get_model_vars_dataframe()

# Graficamos la información usando `matplotlib`
fig, axs = plt.subplots(figsize=(7,7))
axs.set_xticks([])
axs.set_yticks([])
patch = plt.imshow(all_grid.iloc[0][0])

def animate(i):
    patch.set_data(all_grid.iloc[i][0])

anim = animation.FuncAnimation(fig, animate, frames=frames,interval=1)
plt.show()