import cv2
import numpy as np
from sklearn.utils import shuffle
from keras.models import Sequential
from keras.layers.core import Dense

def load_model():
    model = Sequential()

    model.add(Dense(256, input_dim=16384, activation='relu'))
    model.add(Dense(64, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(16, activation="relu"))
    model.add(Dense(3, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adadelta',
                  metrics=['accuracy'])
    model.load_weights("64-soft-50-CNN18.hdf5")
    return model

def get_features(potentials):
    hog = cv2.HOGDescriptor()

    pots = np.array([np.array(hog.computeGradient(
        potential)).flatten() for potential in potentials])

    print(pots[0].shape)
    return pots

def classify_DNN(pot_signs):
    model = load_model()
    print(pot_signs[0].shape)
    features = get_features(pot_signs)
    print(features[0].shape)

    predictions = model.predict_classes(features, batch_size=len(pot_signs))
    return predictions


class Solution:
    def largestRectangleArea(self, heights: List[int]) -> int:
        stack = []
        maxArea = 0

        for i, h in enumerate(heights):
            idx = i
            while stack and stack[-1][-1] > h:
                prev_i, prev_h = stack.pop()
                maxArea = max((i - prev_i)*prev_h, maxArea)
                idx = prev_i
            stack.append((idx, h))

        while stack:
            prev_i, prev_h = stack.pop()
            maxArea = max((len(heights) - prev_i)*prev_h, maxArea)

        return maxArea


from sys import stdin

readline = stdin.readline
N = int(readline())
p = [tuple(map(int, readline().split())) for _ in [0] * N]

def calc_cost(pre, to):
    return abs(to[0] - pre[0]) + abs(to[1] - pre[1]) + max(0, to[2] - pre[2])

def has_bit(n, i):
    return n & (1 << i) > 0

ALL = 1 << N
dp = [[1 << 60] * N for _ in [0] * ALL]

dp[1][0] = 0

for n in range(ALL):
    for i in range(N):
        for j in range(N):
            if has_bit(n, j) or i == j:
                continue
            dp[n | (1 << j)][j] = min(
                dp[n | (1 << j)][j], dp[n][i] + calc_cost(p[i], p[j])
            )

ans = 1 << 60
for i in range(N):
    ans = min(ans, dp[-1][i] + calc_cost(p[i], p[0]))

print(ans)


# -*- mode: python ; coding: utf-8 -*-
# import sys ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)

from PyInstaller.utils.hooks import collect_dynamic_libs
from PyInstaller.utils.hooks import collect_data_files
from PyInstaller.utils.hooks import copy_metadata
# from PyInstaller.utils.hooks import collect_all

datas = []
datas += copy_metadata('tqdm')
datas += copy_metadata('regex')
datas += copy_metadata('requests')
datas += copy_metadata('packaging')
datas += copy_metadata('filelock')
datas += copy_metadata('numpy')
datas += copy_metadata('tokenizers')
datas += copy_metadata('torch')
datas += copy_metadata('transformers')
datas += copy_metadata('accelerate')
datas += copy_metadata('sentencepiece')
datas += copy_metadata('pyarrow')

datas += [('resources', 'resources')]

datas += collect_data_files('torch', include_py_files=True, includes=['**/*.py'])
datas += collect_data_files('pyarrow', include_py_files=True, includes=['**/*.py'])
datas += collect_data_files('sentencepiece', include_py_files=True, includes=['**/*.py'])
datas += collect_data_files('transformers', include_py_files=True, includes=['**/*.py'])
datas += collect_data_files('accelerate', include_py_files=True, includes=['**/*.py'])
datas += collect_data_files('datasets', include_py_files=True, includes=['**/*.py'])

binaries = []
binaries += collect_dynamic_libs('sentencepiece')
binaries += collect_dynamic_libs('pyarrow')

hiddenimports = []
hiddenimports += 'sentencepiece'
hiddenimports += 'pyarrow'

block_cipher = None

a = Analysis(
    ['entry.py'],
    pathex=[],
    binaries=binaries,
    datas=datas,
    hiddenimports=hiddenimports,
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='backend',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.zipfiles,
    a.datas,
    strip=False,
    upx=True,
    upx_exclude=[],
    name='backend',
)


import glob
import os
import os.path as osp

from setuptools import find_packages, setup
from torch.utils.cpp_extension import BuildExtension, CUDAExtension

this_dir = osp.dirname(osp.abspath(__file__))
_ext_src_root = osp.join("pointnet2_ops", "_ext-src")
_ext_sources = glob.glob(osp.join(_ext_src_root, "src", "*.cpp")) + glob.glob(osp.join(_ext_src_root, "src", "*.cu"))
# _ext_headers = glob.glob(osp.join(_ext_src_root, "include", "*"))

try:
    from pointnet2_ops._version import __version__
except ImportError:
    __version__ = "unknown-version"

cuda_arch_list = os.environ.get("TORCH_CUDA_ARCH_LIST")
if cuda_arch_list is None:
    os.environ["TORCH_CUDA_ARCH_LIST"] = "5.0+PTX;6.0;6.1;6.2;7.0;7.2;7.5;8.0;8.6;8.9+PTX"

long_description = ''
try:
    with open('README.md', 'r', encoding='utf-8') as f:
        long_description = f.read()
except FileNotFoundError:
    pass

setup(
    name="pointnet2_ops",
    version=__version__,
    description="CUDA Operations for PointNet2",
    long_description=long_description,
    long_description_content_type="text/markdown",
    author="Erik Wijmans",
    url="https://github.com/erikwijmans/Pointnet2_PyTorch/tree/master/pointnet2_ops_lib",
    license="Unlicense",
    packages=find_packages(),
    install_requires=["torch>=1.4"],
    ext_modules=[
        CUDAExtension(
            name="pointnet2_ops._ext",
            sources=_ext_sources,
            extra_compile_args={
                "cxx": ["-O3"],
                "nvcc": ["-O3", "-Xfatbin", "-compress-all"],
            },
            include_dirs=[osp.join(this_dir, _ext_src_root, "include")],
        )
    ],
    cmdclass={"build_ext": BuildExtension},
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: The Unlicense",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.6',
)


import csv
import logging
import os
import sys
from time import sleep

from lxml import etree, html
from lxml.etree import Element
import requests

requests.packages.urllib3.disable_warnings()

log_file = 'cities.log'
logging.basicConfig(filename=log_file, level=logging.INFO)

metros = [
    {
        'state_code': 'ct',
        'metro': 'Bridgeport-Stamford-Norwalk',
        'msa_id': '14860',
    },
    {
        'state_code': 'ct',
        'metro': 'New Haven-Milford',
        'msa_id': '35300',
    },
    {
        'state_code': 'me',
        'metro': 'Portland-South Portland',
        'msa_id': '38860',
    },
    {
        'state_code': 'md',
        'metro': 'Baltimore-Columbia-Towson',
        'msa_id': '12580',
    },
    {
        'state_code': 'ma',
        'metro': 'Boston-Cambridge-Newton',
        'msa_id': '14460',
    },
    {
        'state_code': 'ma',
        'metro': 'Worcester',
        'msa_id': '49340',
    },
    {
        'state_code': 'nj',
        'metro': 'New York-Newark-Jersey City',
        'msa_id': '35620',
    },
    {
        'state_code': 'nj',
        'metro': 'Philadelphia-Camden-Wilmington',
        'msa_id': '37980',
    },
    {
        'state_code': 'ny',
        'metro': 'New York-Newark-Jersey City',
        'msa_id': '35620',
    },
    {
        'state_code': 'Providence-Warwick',
        'metro': 'Bridgeport',
        'msa_id': '39300',
    },
]

csv_columns = [
    'State',
    'City',
    # Best Places
    # Summary
    'Population',
    'Median Income',
    'Median Home Price',
    'Comfort Index (Climate)',
    'County',
    'Metro Area',
    'Zip Codes',
    'Primary Zip Code',
    'Pros',
    'Cons',
    # Jobs
    'Income per Capita',
    'Unemployment Rate',
    'Recent Job Growth',
    'Future Job Growth',
    'You Should Know',
    # Crime
    'Violent Crime',
    'Property Crime',
    # Climate
    'Rain',
    'Snow',
    'Sunny Days',
    'Precipitation Days',
    'Summer High',
    'Winter Low',
    # Economy
    'Sales Tax Rate',
    'Income Tax Rate',
    # Health
    'Health Cost Index',
    'Water Quality Index',
    'Superfund Index',
    'Air Quality Index',
    # People
    'Population Density',
    'White Percent',
    'Black Percent',
    'Asian Percent',
    'Hispanic Percent',
    'DINK Percent',
    # Politics
    'Political Climate',
    'VoteWord'
    # Housing
    'Home Appreciation 10y',  # ???
    'Home Appreciation',
    'Average Home Age',
    'Renting Rate',
    'Property Tax Rate',
    # Transportation
    'One-Way Commute',
    'Mass Transit Rate',

    # Niche
    # Summary
    'Overall Niche Grade',
    'Public Schools Grade',
    'Housing Grade',
    'Families Grade',
    'Jobs Grade',
    'Cost of Living Grade',
    'Outdoor Activities Grade',
    'Crime Grade',
    'Nightlife Grade',
    'Diversity Grade',
    'Weather Grade',
    'Health Grade',
    'Commute Grade',
    # Real Estate
    'Area Feel',
    'Own Percent',
    # Crime
    #
    # I'm ignoring this section
    #
    # Residents
    'Higher Degree Percent',
    # Working
    'Median Household Income',
    'Best Places Rank',

    # Area Vibes
    # Summary
    'Livability',
    # Locals' Thoughts
    'Family Friendly',
    'Public Transit Accessible',
    'Walkable Groceries',

]

best_places_city_base_url = 'https://www.bestplaces.net'

best_places_city_url =                      '//div[@class="col-md-4"]/ul/li/a'
best_places_city_state =                    '//b[text()="State:"]/following-sibling::u[1]/a'
best_places_city_population =               '//u[contains(text(),"Population")]/../../following-sibling::p[1]'
best_places_city_med_income =               '//u[contains(text(),"Median Income")]/../../following-sibling::p[1]'
best_places_city_med_age =                  '//u[contains(text(),"Median Age")]/../../following-sibling::p[1]'
best_places_city_unempl_rate =              '//u[contains(text(),"Unemployment Rate")]/../../following-sibling::p[1]'
best_places_city_med_home =                 '//u[contains(text(),"Median Home Price")]/../../following-sibling::p[1]'
best_places_city_comfort =                  '//u[contains(text(),"Comfort Index (Climate)")]/../../following-sibling::p[1]'
best_places_city_county =                   '//b[text()="County:"]/following-sibling::u[1]/a'
best_places_city_primary_zip_code =         '//b[text()="Zip Codes:"]/../a[1]/u'
best_places_city_cost_of_living =           '//b[text()="Cost of Living:"]/following-sibling::text()[1]'
best_places_jobs_future_jobs =              '//u[text()="Future Job Growth"]/../../following-sibling::td[1]'
best_places_health_cost_index =             '//h6[text()="HEALTH COST INDEX"]/following-sibling::div[1]'
best_places_health_water_quality_index =    '//h6[text()="WATER QUALITY INDEX"]/following-sibling::div[1]'
best_places_health_superfund_index =        '//h6[text()="SUPERFUND INDEX"]/following-sibling::div[1]'
best_places_health_air_quality_index =      '//h6[text()="AIR QUALITY INDEX"]/following-sibling::div[1]'
best_places_voting_vote_word =              '//h5[text()="VoteWord™"]/following-sibling::h5[1]'

area_vibes_livibility_total =               '//img[contains(@alt, "livability")]/following-sibling::em[1]'

def setup():
    for metro_area in metros:
        filename = f'output/{metro_area.get("metro")}.csv'
        if not os.path.isfile(filename):
            with open(file=filename,mode='w+') as state_csv:
                state_csv.write('')

def tear_down():
    logging.info('Exiting')

def get_page(url: str) -> etree:
    logging.info(f'Requesting page at {url}')
    response = requests.get(url=url, verify=False, allow_redirects=True)
    return html.fromstring(response.content)

def parse_city(metro: dict, city_element: etree.Element):
    city_link: str = city_element.attrib['href'].replace('..', best_places_city_base_url)
    city_name: str = city_element[0].text
    logging.info(f'Getting information about {city_name}')
    city = {}

    # Get city information
    # Best Places
    # Summary Page + inputs
    city_page: etree = get_page(city_link.replace('..', best_places_city_base_url))

    city['state']                   = str(city_page.xpath(best_places_city_state)[0].text).strip()
    city['state_code']              = str.upper(metro.get('state_code'))
    city['metro']                   = metro.get('metro')
    city['metro_id']                = metro.get('msa_id')
    city['city']                    = city_name
    city['county']                  = str(city_page.xpath(best_places_city_county)[0].text).replace('County','').strip()
    try:
        city['zip_code']            = str(city_page.xpath(best_places_city_primary_zip_code)[0].text)
    except IndexError as e:
        logging.error('Zip Code not available')
        city['zip_code']            = 'None'
    city['population']              = str(city_page.xpath(best_places_city_population)[0].text)
    city['unemployment']            = str(city_page.xpath(best_places_city_unempl_rate)[0].text)
    city['median_income']           = str(city_page.xpath(best_places_city_med_income)[0].text)
    city['median_home_price']       = str(city_page.xpath(best_places_city_med_home)[0].text)
    city['median_age']              = str(city_page.xpath(best_places_city_med_age)[0].text)
    city['summer_comfort_index']    = str.split(city_page.xpath(best_places_city_comfort)[0].text, ' / ')[0]
    city['winter_comfort_index']    = str.split(city_page.xpath(best_places_city_comfort)[0].text, ' / ')[1]
    city['cost_of_living']          = str(city_page.xpath(best_places_city_cost_of_living)[0]).strip()

    sleep(1)

    # Jobs Page
    city_jobs_page: etree = get_page(url=f'https://www.bestplaces.net/jobs/city/{city["state"]}/{city["city"]}')

    city['future_job_growth']   = str(city_jobs_page.xpath(best_places_jobs_future_jobs)[0].text)

    sleep(1)

    # Health Page
    city_health_page: etree = get_page(url=f'https://www.bestplaces.net/health/city/{city["state"]}/{city["city"]}')

    city['health_cost_index']   = str(city_health_page.xpath(best_places_health_cost_index)[0].text).strip()
    city['water_quality_index'] = str(city_health_page.xpath(best_places_health_water_quality_index)[0].text).strip()
    city['superfund_index']     = str(city_health_page.xpath(best_places_health_superfund_index)[0].text).strip()
    city['air_quality_index']   = str(city_health_page.xpath(best_places_health_air_quality_index)[0].text).strip()

    sleep(1)

    # Voting Page
    city_voting_page: etree = get_page(url=f'https://www.bestplaces.net/voting/city/{city["state"]}/{city["city"]}')

    city['voteword']    = str.split(city_voting_page.xpath(best_places_voting_vote_word)[0].text, ':')[1].strip()

    # Area Vibes
    area_vibes_page: etree = get_page(url=f'https://www.areavibes.com/{city["city"]}-{city["state_code"]}')

    try:
        city['livability_score']    = str(area_vibes_page.xpath(area_vibes_livibility_total)[0].text)
    except IndexError as e:
        logging.error('City not found on Area Vibes')
        city['livability_score']    = 'None'

    return city

def save_city(city: dict):
    filename = f'output/{city.get("metro")}.csv'
    with open(file=filename, mode='a') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=[key for key in city], delimiter=',', quotechar='\"', quoting=csv.QUOTE_NONNUMERIC, lineterminator='\n')
        if os.stat(filename).st_size == 0:
            writer.writeheader()
        writer.writerow(city)

def parse_and_save_city(metro: dict, city_element: etree.Element):
    # If city already in file, move on to next one
    filename = f'output/{metro.get("metro")}.csv'
    city_name: str = city_element[0].text

    with open(file=filename, mode='r') as csvfile:
        reader = csv.DictReader(csvfile, delimiter=',')
        logging.info(f'Searching for {city_name} in {filename}')
        for row in reader:
            if city_name == row['city']:
                logging.warning(f'{city_name} found in {filename}. Skipping.')
                return None
    logging.info(f'{city_name} not found in {filename}. Saving.')
    city = parse_city(metro=metro, city_element=city_element)
    save_city(city=city)

def main():
    for metro in metros:
        best_places_url = f'https://www.bestplaces.net/find/state.aspx?state={metro.get("state_code")}&msa={metro.get("msa_id")}'
        logging.info(f'Gathering information for {metro.get("metro")}')
        logging.info(f'Best places URL: {best_places_url}')

        metro_page = get_page(best_places_url)
        metro_cities = metro_page.xpath(best_places_city_url)

        for city in metro_cities:
            parse_and_save_city(metro=metro, city_element=city)
            sleep(5)

        sleep(5)

setup()

main()

tear_down()


# -*- coding: utf-8 -*-
import scipy as sp
from scipy import linalg
import InOut as Io
import GMesh as Gm
import os
import semiLagrangean as sl

cwd = os.getcwd()

arquivo = "cavity"

malha = Gm.GMesh("mesh/"+arquivo+".msh")
x = malha.X
y = malha.Y
ien = malha.IEN
nodes = len(x)
num_ele = len(ien)

dt = 0.01
tempo = 3000
Re = 400

p_lagrange = 0.
p_smooth = 0.5
p_wave = 0.5

# ---------------------------------------
# Wz, Psi e velocidade inicial
# ---------------------------------------

Psi_new = sp.zeros(nodes, dtype="float64")
Wz_new = sp.zeros(nodes, dtype="float64")
vx = sp.zeros(nodes, dtype="float64")
vy = sp.zeros(nodes, dtype="float64")

# ---------------------------------------
# Montagem de matrizes
# ---------------------------------------

def fem_matrix(_x, _y, _numele, _numnode, _ien):
    k_local = sp.zeros((3, 3), dtype="float64")
    m_local = sp.array([[2, 1, 1], [1, 2, 1], [1, 1, 2]], dtype="float64")
    gx_local = sp.zeros((3, 3), dtype="float64")
    gy_local = sp.zeros((3, 3), dtype="float64")
    a = sp.zeros(3, dtype="float64")
    b = sp.zeros(3, dtype="float64")
    c = sp.zeros(3, dtype="float64")
    yy = sp.zeros(3, dtype="float64")
    xx = sp.zeros(3, dtype="float64")
    k_global = sp.zeros((_numnode, _numnode), dtype="float64")
    m_global = sp.zeros((_numnode, _numnode), dtype="float64")
    gx_global = sp.zeros((_numnode, _numnode), dtype="float64")
    gy_global = sp.zeros((_numnode, _numnode), dtype="float64")

    for elem in range(_numele):
        for i in range(3):
            xx[i] = _x[_ien[elem, i]]
            yy[i] = _y[_ien[elem, i]]

        a[0] = xx[0] * yy[1] - xx[1] * yy[0]
        a[1] = xx[2] * yy[0] - xx[0] * yy[2]
        a[2] = xx[1] * yy[2] - xx[2] * yy[1]
        Area = (a[0] + a[1] + a[2]) / 2.

        b[0] = yy[1] - yy[2]
        b[1] = yy[2] - yy[0]
        b[2] = yy[0] - yy[1]
        c[0] = xx[2] - xx[1]
        c[1] = xx[0] - xx[2]
        c[2] = xx[1] - xx[0]

        for i in range(3):
            for j in range(3):
                k_local[i, j] = (b[i] * b[j] + c[i] * c[j]) / (4 * Area)
                gx_local[i,j] = b[j] * (1/6.)
                gy_local[i,j] = c[j] * (1/6.)

        for i_local in range(3):
            i_global = _ien[elem, i_local]
            for j_local in range(3):
                j_global = _ien[elem, j_local]
                k_global[i_global, j_global] += k_local[i_local, j_local]
                m_global[i_global, j_global] += m_local[i_local, j_local]* (Area/12.)
                gx_global[i_global, j_global] += gx_local[i_local, j_local]
                gy_global[i_global, j_global] += gy_local[i_local, j_local]

    return  k_global, m_global, gx_global, gy_global

K, M, Gx, Gy = fem_matrix(x, y, num_ele, nodes, ien)

Boundary = sp.zeros(nodes)
lista = []
for i in range(nodes):
    if x[i] == 0.0 or y[i] == 0.0 or y[i] == 1.0 or x[i] == 1.0:
        Boundary[i] = i
    else:
        lista.append(i)

Boundary = sp.delete(Boundary, lista, axis=0)
num_bc = len(Boundary)

# ---------------------------------------
# Condições de contorno e Inicial
# ---------------------------------------

bc_omega = sp.zeros(nodes)
for i in Boundary:
    j = int(i)
    vy[j] = 0.0
    if y[j] == 1.0:
        vx[j] = 1.0
    else:
        vx[j] = 0.0

Minv = sp.linalg.inv(M)
Wz_old = sp.dot(Minv, (sp.dot(Gx, vy) - sp.dot(Gy, vx)))

K_psi = sp.copy(K)
ccpsi = sp.zeros(nodes)

for i in range(num_bc):
    index = int(Boundary[i])
    value = 0.0
    if y[index] == 0.0 or y[index] == 1.0:
        for j in range(nodes):
            if j != index:
                K_psi[index, j] = 0
                K_psi[j, index] = 0
            else:
                K_psi[index, j] = 1

F_psi = sp.dot(M, Wz_old)
for i in range(num_bc):
    index = int(Boundary[i])
    F_psi[index] = 0

Psi_old = sp.linalg.solve(K_psi, F_psi)

sp.random.seed(1)

# ----------------------------------------------------------
# ---------------------- Loop No Tempo ------------------------

neighbour_ele, neighbour_nodes = sl.neighbourElements2(nodes, ien)

for t in range(0, tempo-1):
    print("Solving System " + str((float(t)/(tempo-1))*100) + "%")

    vx_smooth, vy_smooth = Gm.smoothMesh(neighbour_nodes, malha, x, y, dt)
    vx_wave = sp.random.rand(nodes) * 0.2*sp.cos(2*sp.pi*5*t*dt)
    vy_wave = sp.random.rand(nodes) * 0.2*sp.cos(2*sp.pi*5*t*dt)

    vxAle = p_lagrange * vx + p_smooth * vx_smooth + p_wave * vx_wave
    vyAle = p_lagrange * vy + p_smooth * vy_smooth + p_wave * vy_wave

    for i in range(num_bc):
        index = int(Boundary[i])
        vy[index] = 0.0
        vxAle[index] = 0
        vyAle[index] = 0
        if y[index] == max(y):
            vx[index] = 1.0
        else:
            vx[index] = 0.0

    vx_sl = vx - vxAle
    vy_sl = vy - vyAle
    #Wz_dep = sl.Linear2D(nodes, neighbour, ien, x, y, vx, vy, dt, Wz_old)
    Wz_dep = sl.Linear2D(nodes, neighbour_ele, ien, x, y, vx_sl, vy_sl, dt, Wz_old)

    # Solução de Wz e Psi
    x = x + vxAle * dt
    y = y + vyAle * dt
    K, M, Gx, Gy = fem_matrix(x, y, num_ele, nodes, ien)

    Minv = linalg.inv(M)

    # B.C. Vorticidade
    Wcc = sp.dot(Minv, (sp.dot(Gx, vy) - sp.dot(Gy, vx)))
    ccomega = sp.zeros(nodes)
    #Wcc = sp.copy(bc_omega)

    LHS = M / dt + K / Re
    LHS_omega = sp.copy(LHS)

    for i in range(num_bc):
        index = int(Boundary[i])
        value = Wcc[index]
        for j in range(nodes):
            ccomega[j] -= value * LHS[j, index]
            if j != index:
                LHS_omega[index, j] = 0
                LHS_omega[j, index] = 0
            else:
                LHS_omega[index, j] = 1

    F_omega = sp.dot(M / dt, Wz_dep) + ccomega / Re

    for i in range(num_bc):
        index = int(Boundary[i])
        F_omega[index] = Wcc[index]

    Wz_new = sp.linalg.solve(LHS_omega, F_omega)

    K_psi = sp.copy(K)

    for i in range(num_bc):
        index = int(Boundary[i])
        for j in range(nodes):
            if j != index:
                K_psi[index, j] = 0
                K_psi[j, index] = 0
            else:
                K_psi[index, j] = 1

    F_psi = sp.dot(M, Wz_new)
    for i in range(num_bc):
        index = int(Boundary[i])
        F_psi[index] = 0

    Psi_new = sp.linalg.solve(K_psi, F_psi)

    # Salvar VTK
    vtk = Io.InOut(x, y, ien, len(x), len(ien), Psi_old, Wz_old, Wz_dep,
                    None, None, vx, vy, vx_sl, vy_sl)
    vtk.saveVTK(cwd+"/results", arquivo + str(t+1))

    Psi_old = sp.copy(Psi_new)
    Wz_old = sp.copy(Wz_new)

    # Calculo de Vx e Vy
    vx = sp.dot(Minv, sp.dot(Gy, Psi_new))
    vy = -1.0 * sp.dot(Minv, sp.dot(Gx, Psi_new))

#----------------- Fim de Loop -------------------
#-------------------------------------------------


from jax import grad
from jax import numpy as jnp

# Define the softmax model
def old_softmax(
    x,
):  # TOREM: This one should not be working. But alas it give better accuracy
    """Compute softmax values for each sets of scores in x."""
    e_x = jnp.exp(x - jnp.max(x))
    return e_x / e_x.sum(axis=0)

def softmax(x):  # TEST: It should be this way
    """Compute softmax values for each sets of scores in x."""
    # e_x = jnp.exp(x)
    e_x = jnp.exp(x - jnp.max(x))  # CHECK: shold i use this one?
    sum = jnp.expand_dims(e_x.sum(axis=1), 1)
    return e_x / sum

# Define the loss function
def loss_fn(W: jnp.ndarray, b: jnp.ndarray, x: jnp.ndarray, y: jnp.ndarray):
    dot = jnp.dot(x, W)
    non_scalar = -jnp.log(
        softmax(dot + b)[jnp.arange(dot.shape[0]), y]
    )  # This is wrong
    return jnp.mean(non_scalar)  # + 0.1 * jnp.sum(W**2)

# Define the gradient function
grad_loss_W = grad(loss_fn, argnums=0)
grad_loss_b = grad(loss_fn, argnums=1)

# Define the optimizer
def sgd(W, b, x, y, lr=1e-2):
    # Backpop
    newW = W - lr * grad_loss_W(W, b, x, y)
    newB = b - lr * grad_loss_b(W, b, x, y)
    return newW, newB


x = eval(input("请输入整数x:"))
y = eval(input("请输入整数y:"))
if x < y:
    x, y = y, x

while True:
    r = x % y
    if r == 0:
        break
    x = y
    y = r

print("GCD(x,y) =", y)


#!/usr/bin/python
from numpy import *
import operator
import numpy as np
def file2matrix(filename):
  fr = open(filename)
  numberOfLines = len(fr.readlines())
  data = zeros((numberOfLines,2))
  fr = open(filename, 'r')
  index = 0

  output = open(filename, 'r') # See the r
  listFromLine = output.readlines()
  while index < len(listFromLine):
    line = listFromLine[index].split(',')
    data[index,:] = line
    index+=1

  return data

def computeCost(X, y, theta):
  J=0
  # ----Write your code here ----
  m = X.shape[0]
  J = 1 / float(2 * m) * np.sum(
    (np.dot(X, theta) - y) ** 2)
  return J

def gradientDescent(X, y, theta, alpha, num_iters):
  m = len(y) # number of training examples
  n = X.shape[1]
  J_history = zeros((num_iters, 1))
  for k in range(num_iters):
    J_history[k] = computeCost(X, y, theta)
    #---- Write your code here ----
    delta = np.zeros((n, 1))
    for i in range(m):
      hyp_xi = np.dot(theta.transpose(), X[i, :].transpose())
      delta += ( (hyp_xi - y[i]) * X[i, :].transpose() ).reshape(n, 1)
    theta -= alpha / float(m) * delta
  return theta, J_history

#read from file
data = file2matrix('ex1data1.txt')

m = len(data)
data = np.insert(np.array(data),0,np.ones(m),axis=1) # Add a column of ones to x
y = data[:,1]
theta = zeros((2, 1)) # initialize fitting parameters
# Some gradient descent settings
X = data[:,0:2]
y = data[:,2].reshape(m, 1)
#print (X)
#print (y)
iterations = 1500
alpha = 0.01

# compute and display initial cost
J = computeCost(X, y, theta)
print ('Cost value is '+str(J))
#run gradient descent
theta, J_history = gradientDescent(X, y, theta, alpha, iterations)
#print (theta)
predict1 = np.dot([1, 5], theta)
predict2 = np.dot([1, 25],theta)

# code for visualizing your data
import matplotlib.pyplot as plt
import pylab
plt.scatter(X[:,1],y,color='red', marker='x')
plt.ylabel('Profit in $10,000s');
plt.xlabel('Population of City in 10,000s');
plt.plot([5,25],[predict1,predict2],color='blue',linewidth=2.0)
plt.show()

print(predict1,predict2)
iterations = 1500;
alpha = 0.01;


def find_set(x):
    while x != rep[x]:
        x = rep[x]
    return x

def union(x, y):
    rep[find_set(y)] = find_set(x)
    rep[y] = find_set(x)

T = int(input())
for test_case in range(1, T + 1):
    V, E = map(int, input().split())
    rep = [i for i in range(V+1)]
    graph = []
    for _ in range(E):
        u, v, w = map(int, input().split())
        graph.append((u, v, w))

    graph.sort(key=lambda x: x[2])

    s = 0
    cnt = 0

    for u, v, w in graph:
        if find_set(u) != find_set(v):
            cnt += 1
            s += w
            union(u, v)
            if cnt == V:
                break

    print(f'#{test_case}', s)


from .base import *

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '4o5h94sf8#+e_$a7*szdfcyf!uhf2er)0kg#qp3r*nac$%pp6q'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

STATICFILES_DIRS = [
    "ui/static",
]


import numpy as np
from matplotlib import pyplot as plt

def Brownian1D(t0: int = 0, t1: int = 1, N: int = 10000, nums: int = 1, mean:float = 0.0):
    timeSpace = np.linspace(t0, t1, num=N, endpoint=True, dtype=np.float64)

    dt = timeSpace[1] - timeSpace[0]

    mean = np.array(mean, dtype=np.float64)
    mean = mean.repeat(nums)
    mean = np.expand_dims(mean, 0)

    stdDev = np.sqrt([dt] * nums)

    dB = stdDev * np.random.normal(size=(N - 1, nums))

    B = np.cumsum(dB, axis=0, dtype=np.float64) + mean

    B = np.concatenate((mean, B), 0)

    plt.plot(timeSpace, B)
    plt.xlabel("Time - t")
    plt.ylabel("Space - x")
    plt.title("Brownian Motion 1-D")
    plt.show()

    return B

if __name__ == "__main__":
    Brownian1D(0, 1, 10000, 4, 12)


import tensorflow as tf
import math, numpy as np
import matplotlib.pyplot as plt

def plot_data(centroids, data, n_samples):
    colour = plt.cm.rainbow(np.linspace(0,1,len(centroids)))
    for i, centroid in enumerate(centroids):
        samples = data[i*n_samples:(i+1)*n_samples]
        plt.scatter(samples[:,0], samples[:,1], c=colour[i], s=1)
        plt.plot(centroid[0], centroid[1], markersize=10, marker="x", color='k', mew=5)
        plt.plot(centroid[0], centroid[1], markersize=5, marker="x", color='m', mew=2)

def all_distances(a, b):
    diff = tf.squared_difference(tf.expand_dims(a, 0), tf.expand_dims(b,1))
    return tf.reduce_sum(diff, axis=2)

class Kmeans(object):

    def __init__(self, data, n_clusters):
        self.n_data, self.n_dim = data.shape
        self.n_clusters = n_clusters
        self.data = data
        self.v_data = tf.Variable(data)
        self.n_samples = self.n_data//self.n_clusters

    def run(self):
        tf.global_variables_initializer().run()
        initial_centroids = self.find_initial_centroids(self.n_clusters).eval()
        curr_centroids = tf.Variable(initial_centroids)
        nearest_indices = self.assign_to_nearest(curr_centroids)
        updated_centroids = self.update_centroids(nearest_indices)
        # Begin main algorithm
        tf.global_variables_initializer().run()
        c = initial_centroids
        for i in range(10):
            c2 = curr_centroids.assign(updated_centroids).eval()
            if np.allclose(c,c2): break
            c=c2
        return c2

    def find_initial_centroids(self, k):
        r_index = tf.random_uniform([1], 0, self.n_data, dtype=tf.int32)
        r = tf.expand_dims(self.v_data[tf.squeeze(r_index)], dim=1)
        initial_centroids = []
        for i in range(k):
            dist = all_distances(self.v_data, r)
            farthest_index = tf.argmax(tf.reduce_min(dist, axis=0), 0)
            farthest_point = self.v_data[tf.to_int32(farthest_index)]
            initial_centroids.append(farthest_point)
            r = tf.stack(initial_centroids)
        return r

    def choose_random_centroids(self):
        n_samples = tf.shape(v_data)[0]
        random_indices = tf.random_shuffle(tf.range(0, n_samples))
        centroid_indices = random_indices[:self.n_clusters]
        return tf.gather(self.v_data, centroid_indices)

    def assign_to_nearest(self, centroids):
        return tf.argmin(all_distances(self.v_data, centroids), 0)

    def update_centroids(self, nearest_indices):
        partitions = tf.dynamic_partition(self.v_data, tf.to_int32(nearest_indices), self.n_clusters)
        return tf.concat([tf.expand_dims(tf.reduce_mean(partition, 0), 0)
                                      for partition in partitions], 0)


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

import numpy
import scipy
import sympy

from ..helpers import untangle, fsd, pm
from .. import nsphere

class Stroud1967b(object):
    """
    A.H. Stroud,
    Some Seventh Degree Integration Formulas for Symmetric Regions,
    SIAM J. Numer. Anal., 4(1), 37–44. (8 pages),
    <https://doi.org/10.1137/0704004>.
    """

    def __init__(self, index, n, symbolic=False):
        sqrt = sympy.sqrt if symbolic else numpy.sqrt
        pi = sympy.pi if symbolic else numpy.pi
        frac = sympy.Rational if symbolic else lambda x, y: x / y
        gamma = sympy.gamma if symbolic else scipy.special.gamma

        self.dim = n
        self.degree = 7

        if index in ["2a", "2b"]:
            # the points/weights are complex for n >= 9; one could permit that
            if index == "2a":
                assert n in [2, 3, 4, 6, 7]
                p_m = +1
            else:
                assert index == "2b"
                assert n in [3, 4]
                p_m = -1

            p_m = +1 if index == "2a" else -1

            sqrt38n = sqrt(3 * (8 - n))

            r2 = (3 * (8 - n) - p_m * (n - 2) * sqrt38n) / 2 / (5 - n)
            s2 = (3 * n - p_m * 2 * sqrt38n) / 2 / (3 * n - 8)
            t2 = (6 + p_m * sqrt38n) / 2
            B = (8 - n) / r2 ** 3 / 8
            C = 1 / s2 ** 3 / 2 ** (n + 3)
            D = 1 / t2 ** 3 / 16
            A = 1 - 2 * n * B - 2 ** n * C - 2 * n * (n - 1) * D

            r = sqrt(r2)
            s = sqrt(s2)
            t = sqrt(t2)

            data = [
                (A, numpy.full((1, n), 0)),
                (B, fsd(n, (r, 1))),
                (C, pm(n, s)),
                (D, fsd(n, (t, 2))),
            ]

            self.points, self.weights = untangle(data)
            self.weights *= sqrt(pi) ** n
        else:
            assert index == "4"
            assert n >= 3

            sqrt2n2 = sqrt(2 * (n + 2))
            r1, r2 = [sqrt((n + 2 - p_m * sqrt2n2) / 2) for p_m in [+1, -1]]
            g = gamma(frac(n, 2))
            A1, A2 = [(n + 2 + p_m * sqrt2n2) / 4 / (n + 2) * g for p_m in [+1, -1]]

            s = nsphere.Stroud1967(n, symbolic=symbolic)

            self.points = numpy.concatenate([r1 * s.points, r2 * s.points])

            self.weights = numpy.concatenate([A1 * s.weights, A2 * s.weights])

        return


import json
import os
import uuid
import pandas as pd
import numpy as np

from tqdm import tqdm
from matchms.importing import load_from_mgf
import pymzml
from pyteomics import mzxml, mzml

import logging
logger = logging.getLogger('msql_fileloading')

def _determine_cache_filename_prefix(input_filename, cache_dir=None, cache_file=None):
    """
    This function determines the cache filename prefix before the extensions

    Args:
        input_filename ([type]): [description]
        cache_dir ([type], optional): [description]. Defaults to None.
        cache_file ([type], optional): [description]. Defaults to None.

    Raises:
        Exception: [description]

    Returns:
        [type]: [description]
    """
    if cache_dir is not None and cache_file is not None:
        raise Exception("Cannot specify both cache_dir and cache_file")

    if cache_dir is not None:
        # Calculate the file size of the input_filename
        input_file_size = os.stat(input_filename).st_size

        # Use a hashing that takes into account the file size and filename
        namespace = uuid.UUID('6ba7b810-9dad-11d1-80b4-00c04fd430c8')
        hashed_identifier = str(uuid.uuid3(namespace, "{}:{}".format(input_filename, input_file_size))).replace("-", "")

        hash_folder = hashed_identifier[:2]

        cache_filename = os.path.join(cache_dir, hash_folder, hashed_identifier)
    elif cache_file is not None:
        # Here we assume that the cache_file is a full path but without the extensions
        cache_filename = cache_file
    else:
        cache_filename = input_filename

    return cache_filename

def _determine_feather_cache_filename(input_filename, cache_dir=None, cache_file=None):
    """
    This function determines the feather cache filename

    Args:
        input_filename ([type]): [description]
        cache_dir ([type], optional): [description]. Defaults to None.
        cache_file ([type], optional): [description]. Defaults to None.

    Returns:
        [type]: [description]
    """
    cache_filename = _determine_cache_filename_prefix(input_filename, cache_dir=cache_dir, cache_file=cache_file)
    ms1_cache_filename = cache_filename + "_ms1.msql.feather"
    ms2_cache_filename = cache_filename + "_ms2.msql.feather"

    return ms1_cache_filename, ms2_cache_filename

def load_data(input_filename, cache=None, cache_dir=None, cache_file=None):
    """
    Loading data generically

    Args:
        input_filename ([type]): [description]
        cache (string, optional): [description]. Defaults to None. The different types are feather
        cache_dir (string, optional): [If we use the cache, we can specify a folder and utilize our own naming]. Defaults to None.
        cache_file (string, optional): [If we use the cache, we can also specify the file, this is mutually exclusive to the folder]. Defaults to None.

    Returns:
        [type]: [description]
    """
    if cache is not None:
        if cache == "feather":
            ms1_filename, ms2_filename = _determine_feather_cache_filename(input_filename, cache_dir=cache_dir, cache_file=cache_file)

            if os.path.exists(ms1_filename) or os.path.exists(ms2_filename):
                try:
                    ms1_df = pd.read_feather(ms1_filename)
                except:
                    ms1_df = pd.DataFrame()
                try:
                    ms2_df = pd.read_feather(ms2_filename)
                except:
                    ms2_df = pd.DataFrame()

                return ms1_df, ms2_df

    # Actually loading
    if input_filename[-5:].lower() == ".mzml":
        #ms1_df, ms2_df = _load_data_mzML(input_filename)
        #ms1_df, ms2_df = _load_data_mzML2(input_filename) # Faster version using pymzML
        ms1_df, ms2_df = _load_data_mzML_pyteomics(input_filename) # Faster version using pyteomics

    elif input_filename[-6:].lower() == ".mzxml":
        ms1_df, ms2_df = _load_data_mzXML(input_filename)

    elif input_filename[-5:] == ".json":
        ms1_df, ms2_df = _load_data_gnps_json(input_filename)

    elif input_filename[-4:].lower() == ".mgf":
        ms1_df, ms2_df = _load_data_mgf(input_filename)

    elif input_filename[-4:].lower() == ".txt" or input_filename[-4:].lower() == ".dat":
        ms1_df, ms2_df = _load_data_txt(input_filename)

    else:
        print("Cannot Load File Extension")
        raise Exception("File Format Not Supported")

    # Saving Cache
    if cache is not None:
        if cache == "feather":
            ms1_filename, ms2_filename = _determine_feather_cache_filename(input_filename, cache_dir=cache_dir, cache_file=cache_file)

            # lets make sure the folder exists for the filenames
            if not os.path.exists(os.path.dirname(ms1_filename)):
                os.makedirs(os.path.dirname(ms1_filename))

            if not (os.path.exists(ms1_filename) or os.path.exists(ms2_filename)):
                try:
                    ms1_df.to_feather(ms1_filename)
                except:
                    pass

                try:
                    ms2_df.to_feather(ms2_filename)
                except:
                    pass

    return ms1_df, ms2_df

def _load_data_mgf(input_filename):
    file = load_from_mgf(input_filename)

    ms2mz_list = []
    for i, spectrum in enumerate(file):
        if len(spectrum.peaks.mz) == 0:
            continue

        mz_list = list(spectrum.peaks.mz)
        i_list = list(spectrum.peaks.intensities)
        i_max = max(i_list)
        i_sum = sum(i_list)

        for i in range(len(mz_list)):
            if i_list[i] == 0:
                continue

            peak_dict = {}
            peak_dict["i"] = i_list[i]
            peak_dict["i_norm"] = i_list[i] / i_max
            peak_dict["i_tic_norm"] = i_list[i] / i_sum
            peak_dict["mz"] = mz_list[i]

            # Handling malformed mgf files
            try:
                peak_dict["scan"] = spectrum.metadata["scans"]
            except:
                peak_dict["scan"] = i + 1
            try:
                peak_dict["rt"] = float(spectrum.metadata["rtinseconds"]) / 60
            except:
                peak_dict["rt"] = 0
            try:
                peak_dict["precmz"] = float(spectrum.metadata["pepmass"][0])
            except:
                peak_dict["precmz"] = 0

            peak_dict["ms1scan"] = 0
            peak_dict["charge"] = 1 # TODO: Add Charge Correctly here
            peak_dict["polarity"] = 1 # TODO: Add Polarity Correctly here

            ms2mz_list.append(peak_dict)

    # Turning into pandas data frames
    ms1_df = pd.DataFrame([peak_dict])
    ms2_df = pd.DataFrame(ms2mz_list)

    return ms1_df, ms2_df

def _load_data_gnps_json(input_filename):
    all_spectra = json.loads(open(input_filename).read())

    ms1_df_list = []
    ms2_df_list = []

    for spectrum in tqdm(all_spectra):
        # Skipping spectra bigger than 1MB of peaks
        if len(spectrum["peaks_json"]) > 1000000:
            continue

        peaks = json.loads(spectrum["peaks_json"])
        peaks = [peak for peak in peaks if peak[1] > 0]
        if len(peaks) == 0:
            continue
        i_max = max([peak[1] for peak in peaks])
        i_sum = sum([peak[1] for peak in peaks])
        if i_max == 0:
            continue

        ms2mz_list = []

        for peak in peaks:
            peak_dict = {}
            peak_dict["i"] = peak[1]
            peak_dict["i_norm"] = peak[1] / i_max
            peak_dict["i_tic_norm"] = peak[1] / i_sum
            peak_dict["mz"] = peak[0]
            peak_dict["scan"] = spectrum["spectrum_id"]
            peak_dict["rt"] = 0
            peak_dict["precmz"] = float(spectrum["Precursor_MZ"])
            peak_dict["ms1scan"] = 0
            peak_dict["charge"] = 1 # TODO: Add Charge Correctly here
            peak_dict["polarity"] = 1 # TODO: Add Polarity Correctly here

            ms2mz_list.append(peak_dict)

        # Turning into pandas data frames
        if len(ms2mz_list) > 0:
            ms2_df = pd.DataFrame(ms2mz_list)
            ms2_df_list.append(ms2_df)

            ms1_df = pd.DataFrame([peak_dict])
            ms1_df_list.append(ms1_df)

    # Merging
    ms1_df = pd.concat(ms1_df_list).reset_index()
    ms2_df = pd.concat(ms2_df_list).reset_index()

    return ms1_df, ms2_df

def _load_data_mzXML(input_filename):
    ms1mz_list = []
    ms2mz_list = []
    previous_ms1_scan = 0

    with mzxml.read(input_filename) as reader:
        for spectrum in tqdm(reader):
            if len(spectrum["intensity array"]) == 0:
                continue

            if not "m/z array" in spectrum:
                # This is not a mass spectrum
                continue

            mz_list = list(spectrum["m/z array"])
            i_list = list(spectrum["intensity array"])
            i_max = max(i_list)
            i_sum = sum(i_list)

            mslevel = spectrum["msLevel"]
            if mslevel == 1:
                for i in range(len(mz_list)):
                    peak_dict = {}
                    peak_dict["i"] = i_list[i]
                    peak_dict["i_norm"] = i_list[i] / i_max
                    peak_dict["i_tic_norm"] = i_list[i] / i_sum
                    peak_dict["mz"] = mz_list[i]
                    peak_dict["scan"] = spectrum["id"]
                    peak_dict["rt"] = spectrum["retentionTime"]
                    peak_dict["polarity"] = _determine_scan_polarity_mzXML(spectrum)

                    ms1mz_list.append(peak_dict)

                    previous_ms1_scan = spectrum["id"]

            if mslevel == 2:
                msn_mz = spectrum["precursorMz"][0]["precursorMz"]
                msn_charge = 0

                if "precursorCharge" in spectrum["precursorMz"][0]:
                    msn_charge = spectrum["precursorMz"][0]["precursorCharge"]

                for i in range(len(mz_list)):
                    peak_dict = {}
                    peak_dict["i"] = i_list[i]
                    peak_dict["i_norm"] = i_list[i] / i_max
                    peak_dict["i_tic_norm"] = i_list[i] / i_sum
                    peak_dict["mz"] = mz_list[i]
                    peak_dict["scan"] = spectrum["id"]
                    peak_dict["rt"] = spectrum["retentionTime"]
                    peak_dict["precmz"] = msn_mz
                    peak_dict["ms1scan"] = previous_ms1_scan
                    peak_dict["charge"] = msn_charge
                    peak_dict["polarity"] = _determine_scan_polarity_mzXML(spectrum)

                    ms2mz_list.append(peak_dict)

    # Turning into pandas data frames
    ms1_df = pd.DataFrame(ms1mz_list)
    ms2_df = pd.DataFrame(ms2mz_list)

    return ms1_df, ms2_df

def _determine_scan_polarity_mzML(spec):
    """
    Gets an enum for positive and negative polarity, for pymzml

    Args:
        spec ([type]): [description]

    Returns:
        [type]: [description]
    """
    polarity = 0
    negative_polarity = spec["negative scan"]
    if negative_polarity is True:
        polarity = 2
    positive_polarity = spec["positive scan"]
    if positive_polarity is True:
        polarity = 1

    return polarity

def _determine_scan_polarity_pyteomics_mzML(spec):
    """
    Gets an enum for positive and negative polarity, for pyteomics

    Args:
        spec ([type]): [description]

    Returns:
        [type]: [description]
    """
    polarity = 0

    if "negative scan" in spec:
        polarity = 2
    if "positive scan" in spec:
        polarity = 1

    return polarity

def _determine_scan_polarity_mzXML(spec):
    polarity = 0
    if spec["polarity"] == "+":
        polarity = 1
    if spec["polarity"] == "-":
        polarity = 2
    return polarity

def _load_data_mzML_pyteomics(input_filename):
    """
    This is a loading operation using pyteomics to help with loading mzML files with ion mobility

    Args:
        input_filename ([type]): [description]
    """

    previous_ms1_scan = 0

    # MS1
    all_mz = []
    all_rt = []
    all_polarity = []
    all_i = []
    all_i_norm = []
    all_i_tic_norm = []
    all_scan = []

    # MS2
    all_msn_mz = []
    all_msn_rt = []
    all_msn_polarity = []
    all_msn_i = []
    all_msn_i_norm = []
    all_msn_i_tic_norm = []
    all_msn_scan = []
    all_msn_precmz = []
    all_msn_ms1scan = []
    all_msn_charge = []
    all_msn_mobility = []

    with mzml.read(input_filename) as reader:
        for spectrum in tqdm(reader):
            if len(spectrum["intensity array"]) == 0:
                continue

            # Getting the RT
            try:
                rt = spectrum["scanList"]["scan"][0]["scan start time"]
            except:
                rt = 0

            # Correcting the unit
            try:
                if spectrum["scanList"]["scan"][0]["scan start time"].unit_info == "second":
                    rt = rt / 60
            except:
                pass

            scan = int(spectrum["id"].replace("scanId=", "").split("scan=")[-1])

            if not "m/z array" in spectrum:
                # This is not a mass spectrum
                continue

            mz = spectrum["m/z array"]
            intensity = spectrum["intensity array"]
            i_max = max(intensity)
            i_sum = sum(intensity)

            # If there is no ms level, its likely an UV/VIS spectrum and we can skip
            if not "ms level" in spectrum:
                continue

            mslevel = spectrum["ms level"]
            if mslevel == 1:
                all_mz += list(mz)
                all_i += list(intensity)
                all_i_norm += list(intensity / i_max)
                all_i_tic_norm += list(intensity / i_sum)
                all_rt += len(mz) * [rt]
                all_scan += len(mz) * [scan]
                all_polarity += len(mz) * [_determine_scan_polarity_pyteomics_mzML(spectrum)]

                previous_ms1_scan = scan

            if mslevel == 2:
                msn_mz = spectrum["precursorList"]["precursor"][0]["selectedIonList"]["selectedIon"][0]["selected ion m/z"]
                msn_charge = 0

                if "charge state" in spectrum["precursorList"]["precursor"][0]["selectedIonList"]["selectedIon"][0]:
                    msn_charge = int(spectrum["precursorList"]["precursor"][0]["selectedIonList"]["selectedIon"][0]["charge state"])

                all_msn_mz += list(mz)
                all_msn_i += list(intensity)
                all_msn_i_norm += list(intensity / i_max)
                all_msn_i_tic_norm += list(intensity / i_sum)
                all_msn_rt += len(mz) * [rt]
                all_msn_scan += len(mz) * [scan]
                all_msn_polarity += len(mz) * [_determine_scan_polarity_pyteomics_mzML(spectrum)]
                all_msn_precmz += len(mz) * [msn_mz]
                all_msn_ms1scan += len(mz) * [previous_ms1_scan]
                all_msn_charge += len(mz) * [msn_charge]

                if "product ion mobility" in spectrum["precursorList"]["precursor"][0]["selectedIonList"]["selectedIon"][0]:
                    mobility = spectrum["precursorList"]["precursor"][0]["selectedIonList"]["selectedIon"][0]["product ion mobility"]
                    all_msn_mobility += len(mz) * [mobility]

    ms1_df = pd.DataFrame()
    if len(all_mz) > 0:
        ms1_df['i'] = all_i
        ms1_df['i_norm'] = all_i_norm
        ms1_df['i_tic_norm'] = all_i_tic_norm
        ms1_df['mz'] = all_mz
        ms1_df['scan'] = all_scan
        ms1_df['rt'] = all_rt
        ms1_df['polarity'] = all_polarity

    ms2_df = pd.DataFrame()
    if len(all_msn_mz) > 0:
        ms2_df['i'] = all_msn_i
        ms2_df['i_norm'] = all_msn_i_norm
        ms2_df['i_tic_norm'] = all_msn_i_tic_norm
        ms2_df['mz'] = all_msn_mz
        ms2_df['scan'] = all_msn_scan
        ms2_df['rt'] = all_msn_rt
        ms2_df["polarity"] = all_msn_polarity
        ms2_df["precmz"] = all_msn_precmz
        ms2_df["ms1scan"] = all_msn_ms1scan
        ms2_df["charge"] = all_msn_charge

        if len(all_msn_mobility) == len(all_msn_i):
            ms2_df["mobility"] = all_msn_mobility

    return ms1_df, ms2_df

def _load_data_mzML2(input_filename):
    """This is a faster loading version, but a bit more memory intensive

    Args:
        input_filename ([type]): [description]

    Returns:
        [type]: [description]
    """

    MS_precisions = {
        1: 5e-6,
        2: 20e-6,
        3: 20e-6,
        4: 20e-6,
        5: 20e-6,
        6: 20e-6,
        7: 20e-6,
    }
    run = pymzml.run.Reader(input_filename, MS_precisions=MS_precisions)

    previous_ms1_scan = 0

    # MS1
    all_mz = []
    all_rt = []
    all_polarity = []
    all_i = []
    all_i_norm = []
    all_i_tic_norm = []
    all_scan = []

    # MS2
    all_msn_mz = []
    all_msn_rt = []
    all_msn_polarity = []
    all_msn_i = []
    all_msn_i_norm = []
    all_msn_i_tic_norm = []
    all_msn_scan = []
    all_msn_precmz = []
    all_msn_ms1scan = []
    all_msn_charge = []

    for i, spec in tqdm(enumerate(run)):
        # Getting RT
        rt = spec.scan_time_in_minutes()

        # Getting peaks
        peaks = spec.peaks("raw")

        # Filtering out zero rows
        peaks = peaks[~np.any(peaks < 1.0, axis=1)]

        if spec.ms_level == 2:
            if len(peaks) > 1000:
                # Sorting by intensity
                peaks = peaks[peaks[:,1].argsort()]

                # Getting top 1000
                peaks = peaks[-1000:]

        if len(peaks) == 0:
            continue

        mz, intensity = zip(*peaks)

        i_max = max(intensity)
        i_sum = sum(intensity)

        if spec.ms_level == 1:
            all_mz += list(mz)
            all_i += list(intensity)
            all_i_norm += list(intensity / i_max)
            all_i_tic_norm += list(intensity / i_sum)
            all_rt += len(mz) * [rt]
            all_scan += len(mz) * [spec.ID]
            all_polarity += len(mz) * [_determine_scan_polarity_mzML(spec)]

            previous_ms1_scan = spec.ID

        if spec.ms_level == 2:
            msn_mz = spec.selected_precursors[0]["mz"]
            charge = 0
            if "charge" in spec.selected_precursors[0]:
                charge = spec.selected_precursors[0]["charge"]

            all_msn_mz += list(mz)
            all_msn_i += list(intensity)
            all_msn_i_norm += list(intensity / i_max)
            all_msn_i_tic_norm += list(intensity / i_sum)
            all_msn_rt += len(mz) * [rt]
            all_msn_scan += len(mz) * [spec.ID]
            all_msn_polarity += len(mz) * [_determine_scan_polarity_mzML(spec)]
            all_msn_precmz += len(mz) * [msn_mz]
            all_msn_ms1scan += len(mz) * [previous_ms1_scan]
            all_msn_charge += len(mz) * [charge]

    ms1_df = pd.DataFrame()
    if len(all_mz) > 0:
        ms1_df['i'] = all_i
        ms1_df['i_norm'] = all_i_norm
        ms1_df['i_tic_norm'] = all_i_tic_norm
        ms1_df['mz'] = all_mz
        ms1_df['scan'] = all_scan
        ms1_df['rt'] = all_rt
        ms1_df['polarity'] = all_polarity

    ms2_df = pd.DataFrame()
    if len(all_msn_mz) > 0:
        ms2_df['i'] = all_msn_i
        ms2_df['i_norm'] = all_msn_i_norm
        ms2_df['i_tic_norm'] = all_msn_i_tic_norm
        ms2_df['mz'] = all_msn_mz
        ms2_df['scan'] = all_msn_scan
        ms2_df['rt'] = all_msn_rt
        ms2_df["polarity"] = all_msn_polarity
        ms2_df["precmz"] = all_msn_precmz
        ms2_df["ms1scan"] = all_msn_ms1scan
        ms2_df["charge"] = all_msn_charge

    return ms1_df, ms2_df

def _load_data_mzML(input_filename):
    MS_precisions = {
        1: 5e-6,
        2: 20e-6,
        3: 20e-6,
        4: 20e-6,
        5: 20e-6,
        6: 20e-6,
        7: 20e-6,
    }
    run = pymzml.run.Reader(input_filename, MS_precisions=MS_precisions)

    ms1_df_list = []
    ms2_df_list = []
    previous_ms1_scan = 0

    for i, spec in tqdm(enumerate(run)):
        ms1_df = pd.DataFrame()
        ms2_df = pd.DataFrame()

        # Getting RT
        rt = spec.scan_time_in_minutes()

        # Getting peaks
        peaks = spec.peaks("raw")

        # Filtering out zero rows
        peaks = peaks[~np.any(peaks < 1.0, axis=1)]

        # Sorting by intensity
        peaks = peaks[peaks[:, 1].argsort()]

        if spec.ms_level == 2:
            # Getting top 1000
            peaks = peaks[-1000:]

        if len(peaks) == 0:
            continue

        mz, intensity = zip(*peaks)

        i_max = max(intensity)
        i_sum = sum(intensity)

        if spec.ms_level == 1:
            ms1_df['i'] = intensity
            ms1_df['i_norm'] = intensity / i_max
            ms1_df['i_tic_norm'] = intensity / i_sum
            ms1_df['mz'] = mz
            ms1_df['scan'] = spec.ID
            ms1_df['rt'] = rt
            ms1_df['polarity'] = _determine_scan_polarity_mzML(spec)

            previous_ms1_scan = spec.ID

        if spec.ms_level == 2:
            msn_mz = spec.selected_precursors[0]["mz"]
            charge = 0
            if "charge" in spec.selected_precursors[0]:
                charge = spec.selected_precursors[0]["charge"]

            ms2_df['i'] = intensity
            ms2_df['i_norm'] = intensity / i_max
            ms2_df['i_tic_norm'] = intensity / i_sum
            ms2_df['mz'] = mz
            ms2_df['scan'] = spec.ID
            ms2_df['rt'] = rt
            ms2_df["polarity"] = _determine_scan_polarity_mzML(spec)
            ms2_df["precmz"] = msn_mz
            ms2_df["ms1scan"] = previous_ms1_scan
            ms2_df["charge"] = charge

        # Turning into pandas data frames
        if len(ms1_df) > 0:
            ms1_df_list.append(ms1_df)

        if len(ms2_df) > 0:
            ms2_df_list.append(ms2_df)

    if len(ms1_df_list) > 0:
        ms1_df = pd.concat(ms1_df_list).reset_index()
    else:
        ms1_df = pd.DataFrame()

    if len(ms2_df_list) > 0:
        ms2_df = pd.concat(ms2_df_list).reset_index()
    else:
        ms2_df = pd.DataFrame()

    return ms1_df, ms2_df

def _load_data_txt(input_filename):
    # We are assuming whitespace separated columns, first is mz, second is intensity, and will be marked as MS1
    mz_list = []
    i_list = []
    for line in open(input_filename):
        cleaned_line = line.rstrip()
        if len(cleaned_line) == 0:
            continue
        mz, i = cleaned_line.split()

        mz_list.append(float(mz))
        i_list.append(float(i))

    ms1_df = pd.DataFrame()
    ms1_df['mz'] = mz_list
    ms1_df['i'] = i_list
    ms1_df['i_norm'] = ms1_df['i'] / max(ms1_df['i'])
    ms1_df['i_tic_norm'] = ms1_df['i'] / sum(ms1_df['i'])
    ms1_df['scan'] = 1
    ms1_df['rt'] = 0
    ms1_df['polarity'] = "Positive"

    print(ms1_df)

    return ms1_df, pd.DataFrame()


""" Script to pull data from the binance API of the previous years trades
	Writes to ../data/Symbol_Data.csv The binance API data

	as well as ./input.csv traning data for the Neural Network

"""

import time, datetime, csv, json, requests, numpy as np

import matplotlib.pyplot as plt

def main():
	symbols = ['BTCUSDT', 'ETHBTC', 'EOSBTC', 'LTCBTC', 'BCCBTC', 'XRPBTC', 'XLMBTC']
	data = get_binance_data(symbols)

	for k, v in data.items():
		print(k, len(v))

	#Write the raw data to disk
	for s in symbols:
		with open('data/' + s + '_previous_year_data.csv', 'w') as csvfile:
			writer = csv.writer(csvfile)
			writer.writerow(['open_time', 'open', 'high', 'low', 'close', 'volume', 'close_time', 'quote_asset_volume', 'number_of_trades', 'taker_buy_base_asset_volume', 'taker_buy_quote_asset_volume'])
			for line in data[s]:
				writer.writerow(line[:-1])

	#generate data for NN
	""" Data vector: [ts, weekday, hour number, (open, close, log return, volume) for each currency]
	"""
	timestamps = None

	for k, v in data.items():
		current_timestamps = set()
		for hour in v:
			current_timestamps.add(hour[0])
		if timestamps is None:
			timestamps = current_timestamps
		else:
			timestamps = timestamps.intersection(current_timestamps)

	print(len(timestamps))
	log_returns = []

	for k, v in data.items():
		data[k] = [hour for hour in v if hour[0] in timestamps]

	with open('input.csv', 'w') as csvfile:
		writer = csv.writer(csvfile)
		header = ['timestamp', 'day', 'hour']
		currency_titles = ['log_return', 'open', 'close', 'volume']
		for symbol in symbols:
			header += [symbol + '_' + title for title in currency_titles]
		writer.writerow(header)
		for i in range(len(data[symbols[0]])):
			ts = data[symbols[0]][i][0]

			#only record the timestamp if all data is available
			if ts not in timestamps:
				continue

			row = [int(ts), datetime.datetime.fromtimestamp(int(ts / 1000)).strftime('%w'), int(datetime.datetime.fromtimestamp(int(ts / 1000)).strftime('%H'))]
			for symbol, d in data.items():
				row += [np.log(d[i][4] / d[i][1]), d[i][1], d[i][4] , d[i][3]]
				log_returns.append(np.log(d[i][4] / d[i][1]))
			writer.writerow(row)

	plt.hist(log_returns, 50)
	plt.show()

def get_binance_data(symbols):
	""" Queries the API to get the previous year of trades

	Arguments:
	--------------
		symbols: list of symbols traded on the binance exchange

	Returns:
	-------------
		dict of arrays with the previous years' data (9000 hours) in chronological order
	example response
		[
		  [
		    1499040000000,      // Open time
		    "0.01634790",       // Open
		    "0.80000000",       // High
		    "0.01575800",       // Low
		    "0.01577100",       // Close
		    "148976.11427815",  // Volume
		    1499644799999,      // Close time
		    "2434.19055334",    // Quote asset volume
		    308,                // Number of trades
		    "1756.87402397",    // Taker buy base asset volume
		    "28.46694368",      // Taker buy quote asset volume
		    "17928899.62484339" // Ignore.
		  ]
		]
	"""

	today = time.mktime(datetime.date.today().timetuple())
	#365 * 24 = 8760, so look for the previous 9000 candles
	data = {s: [] for s in symbols}

	for i in range(9, 0, -1):
		print(i)
		start = today * 1000 - (i + 1) * 1000 * 1000 * 60 * 60
		end = today * 1000 - i * 1000 * 1000 * 60 * 60

		for symbol in symbols:
			r = requests.get('https://api.binance.com/api/v1/klines', params={'symbol': symbol, 'interval': '1h', 'startTime': int(start), 'endTime': int(end), 'limit': 1000})
			if r.status_code != 200:
				print(r, '\n' , r.text)
			else:
				data[symbol] += [[float(x) for x in hour] for hour in json.loads(r.text)]

	return data

if __name__ == '__main__':
	main()


# Import necessary libraries
import pygame
import random

# Initialiaze pygame module
pygame.init()

# Screen properties
SCREEN_WIDTH = 945
SCREEN_HEIGHT = 600
display_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
display_caption = pygame.display.set_caption("Catch Me !")

# Set game values
clown_velocity = 3
clown_acceleration = 0.25
score = 0
player_lives = 5

clown_dx = random.choice([-1, 1])
clown_dy = random.choice([-1, 1])

# Set FPS and clock
FPS = 60
clock = pygame.time.Clock()

# Set colors
BLUE = (1, 179, 209)
YELLOW = (248, 231, 28)

# Set font
font = pygame.font.Font("Franxurter.ttf", 32)

system_text = font.render("CATCH ME !", True, BLUE, YELLOW)
system_text_rect = system_text.get_rect()
system_text_rect.topleft = (10, 10)

score_text = font.render("SCORE: " + str(score), True, YELLOW, BLUE)
score_text_rect = score_text.get_rect()
score_text_rect.topright = (SCREEN_WIDTH - 20, 10)

lives_text = font.render("LIVES: " + str(player_lives), True, YELLOW, BLUE)
lives_text_rect = lives_text.get_rect()
lives_text_rect.topright = (SCREEN_WIDTH - 20, 50)

gameover_text = font.render("GAME OVER", True, BLUE, YELLOW)
gameover_text_rect = gameover_text.get_rect()
gameover_text_rect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)

play_again_text = font.render("CLICK ANYWHERE TO PLAY AGAIN", True, YELLOW, BLUE)
play_again_text_rect = play_again_text.get_rect()
play_again_text_rect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2 + 64)

# Game images
# Clown
clown_image = pygame.image.load("clown.png")
clown_rect = clown_image.get_rect()
clown_rect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)

# Background
background_image = pygame.image.load("background.png")
background_image_rect = background_image.get_rect()
background_image_rect.topleft = (0, 0)

# Set sound and music
# Sounds
click_sound = pygame.mixer.Sound("click_sound.wav")
miss_sound = pygame.mixer.Sound("miss_sound.wav")

# Music
background_music = pygame.mixer.music.load("bgr_music.wav")
pygame.mixer.music.play(-1, 0.0)

# Logical variables
running = True
position_x = True
position_y = True

# Main game loop
while running == True:

    # Check for user input
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # Check for clicks
        if event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x = event.pos[0]
            mouse_y = event.pos[1]

            # Check if the clown it's clicked
            if pygame.Rect.collidepoint(clown_rect, mouse_x, mouse_y):
                click_sound.play()
                clown_velocity += clown_acceleration
                score += 1

                # Change clown's direction
                previous_dx = clown_dx
                previous_dy = clown_dy

                # Randomize clown's direction
                while (previous_dx == clown_dx and previous_dy == clown_dy):
                    clown_dx = random.choice([-1, 1])
                    clown_dy = random.choice([-1, 1])

            # Clown it isn't clicked
            else:
                miss_sound.play()
                clown_velocity += clown_acceleration
                player_lives -= 1

    # Move the clown
    clown_rect.x += clown_dx*clown_velocity
    clown_rect.y += clown_dy*clown_velocity

    # Bounce the clown off the edges of the display
    if clown_rect.left <= 0 or clown_rect.right >= SCREEN_WIDTH:
        clown_dx = -1 * clown_dx
    if clown_rect.top <= 0 or clown_rect.bottom >= SCREEN_HEIGHT:
        clown_dy = -1 * clown_dy

    # Update the HUD
    score_text = font.render("SCORE: " + str(score), True, YELLOW, BLUE)
    lives_text = font.render("LIVES: " + str(player_lives), True, YELLOW, BLUE)

    # Check for game over
    if player_lives == 0:
        display_surface.blit(gameover_text, gameover_text_rect)
        display_surface.blit(play_again_text, play_again_text_rect)
        pygame.display.update()

        # Pause the game until the player clicks. Then continue or exit the game
        pygame.mixer.music.stop()
        is_paused = True
        while is_paused:
            for event in pygame.event.get():

                # The player wants to play again
                if event.type == pygame.MOUSEBUTTONDOWN:
                    score = 0
                    player_lives = 5
                    clown_velocity = 3
                    clown_rect.center = (SCREEN_WIDTH/2, SCREEN_HEIGHT/2)
                    clown_dx = random.choice([-1, 1])
                    clown_dy = random.choice([-1, 1])
                    pygame.mixer.music.play(-1, 0.0)
                    is_paused = False

                # The player wants to quit
                if event.type == pygame.QUIT:
                    is_paused = False
                    running = False

    # Blit the background
    display_surface.blit(background_image, background_image_rect)

    # Blit HUD (Head Up Display)
    display_surface.blit(score_text, score_text_rect)
    display_surface.blit(lives_text, lives_text_rect)
    display_surface.blit(system_text, system_text_rect)

    # Blit the clown
    display_surface.blit(clown_image, clown_rect)

    # Update the screen
    pygame.display.update()

    # Tick clock
    clock.tick(FPS)

# End the game
pygame.quit()


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

# class BasicBlock(nn.Module):
#     expansion = 1

#     def __init__(self, in_planes, planes, stride=1):
#         super(BasicBlock, self).__init__()
#         self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
#         self.bn1 = nn.BatchNorm2d(planes)
#         self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=1, bias=False)
#         self.bn2 = nn.BatchNorm2d(planes)

#         self.shortcut = nn.Sequential()
#         if stride != 1 or in_planes != self.expansion*planes:
#             self.shortcut = nn.Sequential(
#                 nn.Conv2d(in_planes, self.expansion*planes, kernel_size=1, stride=stride, bias=False),
#                 nn.BatchNorm2d(self.expansion*planes)
#             )

#     def forward(self, x):
#         out = F.relu(self.bn1(self.conv1(x)))
#         out = self.bn2(self.conv2(out))
#         out += self.shortcut(x)
#         out = F.relu(out)
#         return out

class SWD_CNN(nn.Module):
    def __init__(self, hidden_channel_list=[6, 16], hidden_dims=[120, 84], output_dim=10,
                input_channels=3):
        super(SWD_CNN, self).__init__()
        self.conv1 = nn.Conv2d(input_channels, hidden_channel_list[0], 5)
        self.relu = nn.ReLU()
        self.pool = nn.MaxPool2d(2, 2)

        # middle_hidden_channel_list = hidden_channel_list[1:-1]
        layers = []
        for i in range(len(hidden_channel_list) - 2):
            index = i+1
            layers.append(nn.Conv2d(hidden_channel_list[index-1], hidden_channel_list[index],
                                    kernel_size=3, stride=1,
                                    padding=1, bias=False))
            layers.append(nn.ReLU())
        self.hidden_layers = nn.Sequential(*layers)

        self.conv_last = nn.Conv2d(hidden_channel_list[-2], hidden_channel_list[-1], 5)

        # self.fc1 = nn.Linear(input_dim, hidden_dims[0])
        # self.fc2 = nn.Linear(hidden_dims[0], hidden_dims[1])
        # self.fc3 = nn.Linear(hidden_dims[1], output_dim)
        self.linear_input_dim = hidden_channel_list[-1] * 5 * 5
        self.linear = nn.Linear(self.linear_input_dim, output_dim)

    def forward(self, x):
        #out = self.conv1(x)
        #out = self.relu(out)
        #out = self.pool(out)
        #out = self.conv2(out)
        #out = self.relu(out)
        #out = self.pool(out)
        #out = out.view(-1, 16 * 5 * 5)

        x = self.pool(self.relu(self.conv1(x)))
        x = self.hidden_layers(x)
        x = self.pool(self.relu(self.conv_last(x)))
        x = x.view(-1, self.linear_input_dim)

        # x = self.relu(self.fc1(x))
        # x = self.relu(self.fc2(x))
        # x = self.fc3(x)

        x = self.linear(x)

        return x

def build_SWD_CNN(model_name="swdcnn2", hidden_dims=[120, 84], output_dim=10,
                input_channels=3):
    if model_name == "swdcnn2":
        hidden_channel_list=[6, 16]
    elif model_name == "swdcnn3":
        hidden_channel_list=[6, 8, 16]
    elif model_name == "swdcnn4":
        hidden_channel_list=[6, 8, 8, 16]
    elif model_name == "swdcnn5":
        hidden_channel_list=[6, 8, 8, 8, 16]
    elif model_name == "swdcnn6":
        hidden_channel_list=[6, 8, 8, 8, 8, 16]
    elif model_name == "swdcnn7":
        hidden_channel_list=[6, 8, 8, 8, 8, 8, 16]
    elif model_name == "swdcnn8":
        hidden_channel_list=[6, 8, 8, 8, 8, 8, 8, 16]
    else:
        raise NotImplementedError

    model = SWD_CNN(hidden_channel_list, hidden_dims, output_dim, input_channels)

    return model


import os
import random
import subprocess
import tempfile
import time
from functools import partial
from typing import Any, Dict

import cv2
import imageio
import numpy as np
import torch
import torchvision.transforms as T
from mvextractor.videocap import VideoCap
from PIL import Image

import modelscope.models.multi_modal.videocomposer.data as data
from modelscope.metainfo import Pipelines
from modelscope.models.multi_modal.videocomposer.data.transforms import (
    CenterCropV3, random_resize)
from modelscope.models.multi_modal.videocomposer.ops.random_mask import (
    make_irregular_mask, make_rectangle_mask, make_uncrop)
from modelscope.models.multi_modal.videocomposer.utils.utils import rand_name
from modelscope.outputs import OutputKeys
from modelscope.pipelines.base import Input, Pipeline
from modelscope.pipelines.builder import PIPELINES
from modelscope.utils.constant import Tasks
from modelscope.utils.device import device_placement
from modelscope.utils.logger import get_logger

logger = get_logger()

@PIPELINES.register_module(
    Tasks.text_to_video_synthesis, module_name=Pipelines.videocomposer)
class VideoComposerPipeline(Pipeline):
    r""" Video Composer Pipeline.

    Examples:

    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks
    >>> pipe = pipeline(
            task=Tasks.text_to_video_synthesis,
            model='buptwq/videocomposer',
            model_revision='v1.0.1')
    >>> inputs = {'Video:FILE': 'path/input_video.mp4',
                  'Image:FILE': 'path/input_image.png',
                  'text': 'the text description'}
    >>> output = pipe(inputs)
    """

    def __init__(self, model: str, **kwargs):
        """
        use `model` to create a videocomposer pipeline for prediction
        Args:
            model: model id on modelscope hub.
        """
        super().__init__(model=model)
        self.log_dir = kwargs.pop('log_dir', './video_outputs')
        if not os.path.exists(self.log_dir):
            os.makedirs(self.log_dir)
        self.feature_framerate = kwargs.pop('feature_framerate', 4)
        self.frame_lens = kwargs.pop('frame_lens', [
            16,
            16,
            16,
            16,
        ])
        self.feature_framerates = kwargs.pop('feature_framerates', [
            4,
        ])
        self.batch_sizes = kwargs.pop('batch_sizes', {
            '1': 1,
            '4': 1,
            '8': 1,
            '16': 1,
        })
        l1 = len(self.frame_lens)
        l2 = len(self.feature_framerates)
        self.max_frames = self.frame_lens[0 % (l1 * l2) // l2]
        self.batch_size = self.batch_sizes[str(self.max_frames)]
        self.resolution = kwargs.pop('resolution', 256)
        self.image_resolution = kwargs.pop('image_resolution', 256)
        self.mean = kwargs.pop('mean', [0.5, 0.5, 0.5])
        self.std = kwargs.pop('std', [0.5, 0.5, 0.5])
        self.vit_image_size = kwargs.pop('vit_image_size', 224)
        self.vit_mean = kwargs.pop('vit_mean',
                                   [0.48145466, 0.4578275, 0.40821073])
        self.vit_std = kwargs.pop('vit_std',
                                  [0.26862954, 0.26130258, 0.27577711])
        self.misc_size = kwargs.pop('kwargs.pop', 384)
        self.visual_mv = kwargs.pop('visual_mv', False)
        self.max_words = kwargs.pop('max_words', 1000)
        self.mvs_visual = kwargs.pop('mvs_visual', False)

        self.infer_trans = data.Compose([
            data.CenterCropV2(size=self.resolution),
            data.ToTensor(),
            data.Normalize(mean=self.mean, std=self.std)
        ])

        self.misc_transforms = data.Compose([
            T.Lambda(partial(random_resize, size=self.misc_size)),
            data.CenterCropV2(self.misc_size),
            data.ToTensor()
        ])

        self.mv_transforms = data.Compose(
            [T.Resize(size=self.resolution),
             T.CenterCrop(self.resolution)])

        self.vit_transforms = T.Compose([
            CenterCropV3(self.vit_image_size),
            T.ToTensor(),
            T.Normalize(mean=self.vit_mean, std=self.vit_std)
        ])

    def preprocess(self, input: Input) -> Dict[str, Any]:
        video_key = input['Video:FILE']
        cap_txt = input['text']
        style_image = input['Image:FILE']

        total_frames = None

        feature_framerate = self.feature_framerate
        if os.path.exists(video_key):
            try:
                ref_frame, vit_image, video_data, misc_data, mv_data = self.video_data_preprocess(
                    video_key, self.feature_framerate, total_frames,
                    self.mvs_visual)
            except Exception as e:
                logger.info(
                    '{} get frames failed... with error: {}'.format(
                        video_key, e),
                    flush=True)

                ref_frame = torch.zeros(3, self.vit_image_size,
                                        self.vit_image_size)
                video_data = torch.zeros(self.max_frames, 3,
                                         self.image_resolution,
                                         self.image_resolution)
                misc_data = torch.zeros(self.max_frames, 3, self.misc_size,
                                        self.misc_size)

                mv_data = torch.zeros(self.max_frames, 2,
                                      self.image_resolution,
                                      self.image_resolution)
        else:
            logger.info(
                'The video path does not exist or no video dir provided!')
            ref_frame = torch.zeros(3, self.vit_image_size,
                                    self.vit_image_size)
            _ = torch.zeros(3, self.vit_image_size, self.vit_image_size)
            video_data = torch.zeros(self.max_frames, 3, self.image_resolution,
                                     self.image_resolution)
            misc_data = torch.zeros(self.max_frames, 3, self.misc_size,
                                    self.misc_size)
            mv_data = torch.zeros(self.max_frames, 2, self.image_resolution,
                                  self.image_resolution)

        # inpainting mask
        p = random.random()
        if p < 0.7:
            mask = make_irregular_mask(512, 512)
        elif p < 0.9:
            mask = make_rectangle_mask(512, 512)
        else:
            mask = make_uncrop(512, 512)
        mask = torch.from_numpy(
            cv2.resize(
                mask, (self.misc_size, self.misc_size),
                interpolation=cv2.INTER_NEAREST)).unsqueeze(0).float()

        mask = mask.unsqueeze(0).repeat_interleave(
            repeats=self.max_frames, dim=0)
        video_input = {
            'ref_frame': ref_frame.unsqueeze(0),
            'cap_txt': cap_txt,
            'video_data': video_data.unsqueeze(0),
            'misc_data': misc_data.unsqueeze(0),
            'feature_framerate': feature_framerate,
            'mask': mask.unsqueeze(0),
            'mv_data': mv_data.unsqueeze(0),
            'style_image': style_image
        }
        return video_input

    def forward(self, input: Dict[str, Any]) -> Dict[str, Any]:
        return self.model(input)

    def postprocess(self, inputs: Dict[str, Any],
                    **post_params) -> Dict[str, Any]:
        output_video_path = post_params.get('output_video', None)
        temp_video_file = False
        if output_video_path is not None:
            output_video_path = tempfile.NamedTemporaryFile(suffix='.gif').name
            temp_video_file = True

        if temp_video_file:
            return {OutputKeys.OUTPUT_VIDEO: inputs['video_path']}
        else:
            return {OutputKeys.OUTPUT_VIDEO: inputs['video']}

    def video_data_preprocess(self, video_key, feature_framerate, total_frames,
                              visual_mv):

        filename = video_key
        for _ in range(5):
            try:
                frame_types, frames, mvs, mvs_visual = self.extract_motion_vectors(
                    input_video=filename,
                    fps=feature_framerate,
                    visual_mv=visual_mv)
                break
            except Exception as e:
                logger.error(
                    '{} read video frames and motion vectors failed with error: {}'
                    .format(video_key, e),
                    flush=True)

        total_frames = len(frame_types)
        start_indexs = np.where((np.array(frame_types) == 'I') & (
            total_frames - np.arange(total_frames) >= self.max_frames))[0]
        start_index = np.random.choice(start_indexs)
        indices = np.arange(start_index, start_index + self.max_frames)

        # note frames are in BGR mode, need to trans to RGB mode
        frames = [Image.fromarray(frames[i][:, :, ::-1]) for i in indices]
        mvs = [torch.from_numpy(mvs[i].transpose((2, 0, 1))) for i in indices]
        mvs = torch.stack(mvs)

        if visual_mv:
            images = [(mvs_visual[i][:, :, ::-1]).astype('uint8')
                      for i in indices]
            path = self.log_dir + '/visual_mv/' + video_key.split(
                '/')[-1] + '.gif'
            if not os.path.exists(self.log_dir + '/visual_mv/'):
                os.makedirs(self.log_dir + '/visual_mv/', exist_ok=True)
            logger.info('save motion vectors visualization to :', path)
            imageio.mimwrite(path, images, fps=8)

        have_frames = len(frames) > 0
        middle_indix = int(len(frames) / 2)
        if have_frames:
            ref_frame = frames[middle_indix]
            vit_image = self.vit_transforms(ref_frame)
            misc_imgs_np = self.misc_transforms[:2](frames)
            misc_imgs = self.misc_transforms[2:](misc_imgs_np)
            frames = self.infer_trans(frames)
            mvs = self.mv_transforms(mvs)
        else:
            vit_image = torch.zeros(3, self.vit_image_size,
                                    self.vit_image_size)

        video_data = torch.zeros(self.max_frames, 3, self.image_resolution,
                                 self.image_resolution)
        mv_data = torch.zeros(self.max_frames, 2, self.image_resolution,
                              self.image_resolution)
        misc_data = torch.zeros(self.max_frames, 3, self.misc_size,
                                self.misc_size)
        if have_frames:
            video_data[:len(frames), ...] = frames
            misc_data[:len(frames), ...] = misc_imgs
            mv_data[:len(frames), ...] = mvs

        ref_frame = vit_image

        del frames
        del misc_imgs
        del mvs

        return ref_frame, vit_image, video_data, misc_data, mv_data

    def extract_motion_vectors(self,
                               input_video,
                               fps=4,
                               dump=False,
                               verbose=False,
                               visual_mv=False):

        if dump:
            now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')
            for child in ['frames', 'motion_vectors']:
                os.makedirs(os.path.join(f'out-{now}', child), exist_ok=True)
        temp = rand_name()
        tmp_video = os.path.join(
            input_video.split('/')[0], f'{temp}' + input_video.split('/')[-1])
        videocapture = cv2.VideoCapture(input_video)
        frames_num = videocapture.get(cv2.CAP_PROP_FRAME_COUNT)
        fps_video = videocapture.get(cv2.CAP_PROP_FPS)
        # check if enough frames
        if frames_num / fps_video * fps > 16:
            fps = max(fps, 1)
        else:
            fps = int(16 / (frames_num / fps_video)) + 1
        ffmpeg_cmd = f'ffmpeg -threads 8 -loglevel error -i {input_video} -filter:v \
                        fps={fps} -c:v mpeg4 -f rawvideo {tmp_video}'

        if os.path.exists(tmp_video):
            os.remove(tmp_video)

        subprocess.run(args=ffmpeg_cmd, shell=True, timeout=120)

        cap = VideoCap()
        # open the video file
        ret = cap.open(tmp_video)
        if not ret:
            raise RuntimeError(f'Could not open {tmp_video}')

        step = 0
        times = []

        frame_types = []
        frames = []
        mvs = []
        mvs_visual = []
        # continuously read and display video frames and motion vectors
        while True:
            if verbose:
                logger.info('Frame: ', step, end=' ')

            tstart = time.perf_counter()

            # read next video frame and corresponding motion vectors
            ret, frame, motion_vectors, frame_type, timestamp = cap.read()

            tend = time.perf_counter()
            telapsed = tend - tstart
            times.append(telapsed)

            # if there is an error reading the frame
            if not ret:
                if verbose:
                    logger.warning('No frame read. Stopping.')
                break

            frame_save = np.zeros(frame.copy().shape, dtype=np.uint8)
            if visual_mv:
                frame_save = draw_motion_vectors(frame_save, motion_vectors)

            # store motion vectors, frames, etc. in output directory
            dump = False
            if frame.shape[1] >= frame.shape[0]:
                w_half = (frame.shape[1] - frame.shape[0]) // 2
                if dump:
                    cv2.imwrite(
                        os.path.join('./mv_visual/', f'frame-{step}.jpg'),
                        frame_save[:, w_half:-w_half])
                mvs_visual.append(frame_save[:, w_half:-w_half])
            else:
                h_half = (frame.shape[0] - frame.shape[1]) // 2
                if dump:
                    cv2.imwrite(
                        os.path.join('./mv_visual/', f'frame-{step}.jpg'),
                        frame_save[h_half:-h_half, :])
                mvs_visual.append(frame_save[h_half:-h_half, :])

            h, w = frame.shape[:2]
            mv = np.zeros((h, w, 2))
            position = motion_vectors[:, 5:7].clip((0, 0), (w - 1, h - 1))
            mv[position[:, 1],
               position[:,
                        0]] = motion_vectors[:, 0:
                                             1] * motion_vectors[:, 7:
                                                                 9] / motion_vectors[:,
                                                                                9:]

            step += 1
            frame_types.append(frame_type)
            frames.append(frame)
            mvs.append(mv)
        if verbose:
            logger.info('average dt: ', np.mean(times))
        cap.release()

        if os.path.exists(tmp_video):
            os.remove(tmp_video)

        return frame_types, frames, mvs, mvs_visual


#!/usr/local/bin/python3

import sys
# insert at 1, 0 is the script path (or '' in REPL)
sys.path.insert(1, '../lib')

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn import metrics

iris = load_iris()
# create X (features) and y (response)
X = iris.data
y = iris.target
# use train/test split with different random_state values
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=4)

# check classification accuracy of KNN with K=5
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
print(metrics.accuracy_score(y_test, y_pred))

X_new = [[3, 5, 4, 2], [5, 4, 3, 2]]
p=knn.predict(X_new)
print(p)

import pickle
with open('data/model.dat','wb') as file:
    pickle.dump(knn,file)


"""
Importació de dades a la base de dades
"""

import pandas as pd
import os
from dades import connexio

dir_path = os.path.dirname(os.path.realpath(__file__))
ct = connexio()

# ----------- Buidar taules --------------

# Eliminació taula Professor
with ct.cursor() as cursor:
    cursor.execute("DELETE FROM Professor;")
    ct.commit()

# Eliminació taula Horari
with ct.cursor() as cursor:
    cursor.execute("DELETE FROM Horari;")
    ct.commit()

# ----------- Taula Professor -------------

profes = pd.read_csv(dir_path + '/../files/professors_bd.csv', sep=",")
profes.fillna('', inplace=True)

for i in profes.index:
    Dni = profes.loc[i,'Dni']
    Nom = profes.loc[i,'Nom']
    Cognom = profes.loc[i, 'Cognom']
    CodiHorari = str(profes.loc[i,'CodiHorari'])
    CodiBarres = str(profes.loc[i,'CodiBarres'])
    Departament = profes.loc[i,'Departament']

    insert = "INSERT INTO Professor (Dni, Nom, Cognom, CodiHorari, CodiBarres, Departament, Actiu, Substitut) " \
             "VALUES ('" + Dni + "', '" + Nom + "', '" + Cognom + "', " + CodiHorari + ", " \
             + CodiBarres + ", '" + Departament + "', 1, 'no');"

    with ct.cursor() as cursor:
        cursor.execute(insert)
        ct.commit()

# ---------- Taula Horari -----------

horari = pd.read_csv(dir_path + '/../files/horari_bd.csv', sep=",", dtype=str)
horari = horari.fillna('')

for i in horari.index:
    Dia = horari.loc[i,'Dia']
    Hora = horari.loc[i,'Hora']
    Assignatura = horari.loc[i, 'Assignatura']
    CodiHorari = horari.loc[i,'CodiHorari']
    Aula = horari.loc[i,'Aula']
    Grup = horari.loc[i,'Grup']

    insert = "INSERT INTO Horari (Dia, Hora, Assignatura, CodiHorari, Aula, Grup) " \
             "VALUES (" + Dia + ", " + Hora + ", '" + Assignatura + "', " \
             + CodiHorari + ", '" + Aula + "', '" + Grup + "');"

    with ct.cursor() as cursor:
        cursor.execute(insert)
        ct.commit()

ct.close()


import json

from flask import abort, jsonify, request, Response
from flask.views import MethodView
from werkzeug.datastructures import MultiDict

import config

from finance import utils, db
from finance.forms.transaction import TransactionForm, TransactionsImportForm
from finance.models.transaction import Transaction
from finance.stats import STATS
from finance.trx_import import TransactionsImport

class TransactionAPI(MethodView):
    """Transaction Views"""

    decorators = [
        utils.requires_auth,
        utils.crossdomain(
            origin='*',
            headers=config.HEADERS_ALLOWED
        ),
    ]

    def get(self, transaction_id):
        if transaction_id is None:
            # return a list of transactions
            with STATS.all_transactions.time():
                # return a list of accounts
                res = [trx.jsonify() for trx in Transaction.query.all()]
                STATS.success += 1
                return Response(json.dumps(res), mimetype='application/json')
        else:
            # expose transaction
            with STATS.get_transaction.time():
                # expose a single account
                trx = Transaction.query.get(transaction_id)
                if trx is None:
                    STATS.notfound += 1
                    return abort(404)
                STATS.success += 1
                return jsonify(trx.jsonify())

    def post(self):
        with STATS.add_transaction.time():
            # create a new trasnsaction
            form = TransactionForm(request.data)
            if form.validate():
                trx = Transaction(
                    form.debit.account,
                    form.credit.account,
                    form.amount.data,
                    form.summary.data,
                    form.date.data,
                    form.description.data
                )
                db.session.add(trx)
                db.session.commit()
                STATS.success += 1
                return jsonify({
                    'message': 'Successfully added Transaction',
                    'transaction_id': trx.transaction_id
                })
            STATS.validation += 1
            resp = jsonify({"errors": form.errors})
            resp.status_code = 400
            return resp

    def delete(self, transaction_id):
        with STATS.delete_transaction.time():
            # delete a single transaction
            trx = Transaction.query.get(transaction_id)
            if trx is None:
                STATS.notfound += 1
                return abort(404)
            db.session.delete(trx)
            db.session.commit()
            STATS.success += 1
            return jsonify({"message": "Successfully deleted transaction"})

    def put(self, transaction_id):
        with STATS.update_transaction.time():
            # update a single transaction
            trx = Transaction.query.get(transaction_id)
            if trx is None:
                STATS.notfound += 1
                return abort(404)
            form = TransactionForm(request.data)
            if form.validate():
                trx = Transaction.query.get(transaction_id)
                trx.account_debit_id = form.debit.data
                trx.account_credit_id = form.credit.data
                trx.amount = form.amount.data
                trx.summary_id = form.summary.data
                trx.date = form.date.data
                trx.description = form.description.data
                db.session.add(trx)
                db.session.commit()
                STATS.success += 1
                return jsonify({
                    'message': 'Successfully updated Transaction'
                })
            STATS.validation += 1
            resp = jsonify({'errors': form.errors})
            resp.status_code = 400
            return resp

@utils.requires_auth
@utils.crossdomain(origin='*', headers=config.HEADERS_ALLOWED)
def transactions_import():
    """Import transactions"""
    with STATS.upload_transactions.time():
        form = TransactionsImportForm(MultiDict(request.data))
        if form.validate():
            importer = TransactionsImport(form.account.data,
                                          form.transactions_file.data)
            transaction_list = []
            for trx in importer.parse_file():
                transaction_list.append(trx.jsonify())
            STATS.success += 1
            return jsonify(trx)
        resp = jsonify(form.errors)
        resp.status_code = 400
        return resp


# Crie um programa que leia dois valores e mostre um menu na tela
# 1 - somar
# 2 - multiplicar
# 3 - maior
# 4 - novos números
# 5 - sair do programa

# Seu programa deverá realizar a operação soliciada em cada caso.

valor1 = int(input("Digite o primeiro valor: "))
valor2 = int(input("Digite o segundo valor: "))

opcao = 0

while opcao != 5:
    print(
        """Selecione a opção desejada:
    1 - somar
    2 - multiplicar
    3 - maior
    4 - novos números
    5 - sair do programa"""
    )

    opcao = int(input("Digite a opção desejada: "))

    if opcao == 1:
        soma = valor1 + valor2
        print(f"A soma dos valores é {soma}")
    elif opcao == 2:
        multiplicacao = valor1 * valor2
        print(f"A multiplicação dos valores é {multiplicacao}")
    elif opcao == 3:
        if valor1 > valor2:
            print(f"O maior número é {valor1}")
        else:
            print(f"O maior número é {valor2}")
    elif opcao == 4:
        print("Digite os novos valores!")
        valor1 = int(input("Digite o primeiro valor: "))
        valor2 = int(input("Digite o segundo valor: "))
    elif opcao == 5:
        print("Até logo")
    else:
        print("Opção inválida")


import sys
input = sys.stdin.readline

n = int(input())
all = []

for i in range(n):
    temp = list(map(int, input().split()))
    all.append(temp)

for i, a in enumerate(all):
    if i == 0:
        continue

    for j, v in enumerate(a):
        if j == 0:
            all[i][j] += all[i-1][0]
        elif j == len(a)-1:
            all[i][j] += all[i-1][-1]

        else:
            all[i][j] += max(all[i-1][j-1], all[i-1][j])

print(max(all[i]))


import folium
import pandas

df = pandas.read_csv("data/Volcanoes-USA.txt")

avgLAT = df['LAT'].mean()
avgLON = df['LON'].mean()

map_1 = folium.Map(location=[avgLAT, avgLON],zoom_start=4,tiles='Stamen Toner')

def marker_color(elev):
    if elev in range(0,1000):
        col = 'green'
    elif elev in range(1000,3000):
        col = 'orange'
    else:
        col ='red'
    return col

volcano_fg = folium.FeatureGroup(name="Volcano Locations")

for lat, lon, name, elev in zip(df['LAT'], df['LON'], df['NAME'], df['ELEV']):

    folium.Marker([lat, lon],popup = name, icon = folium.Icon(color = marker_color(elev))).add_to(volcano_fg)

volcano_fg.add_to(map_1)

map_1.add_child(folium.LayerControl())

map_1.save('test1.html')


import logging
import os
import tempfile

from subprocess import run
from . import log, rmdir

__all__ = ["mount"]

L = logging.getLogger(__name__)

@log
def mount(path, offset):
    """Mount a Linux Logical Volume Manager (LVM) volume system using
    `libvslvm`.

    See also:
    https://github.com/libyal/libvslvm/wiki/Mounting

    Args:
        path (str): Path to the RAW image file containing LVM volume system.
        offset (str|int): Value in bytes where the LVM volume system starts.

    Returns:
        Path to the directory containing volumes as a virtual file named
        `lvm1`, `lvm2`, etc.
    """
    mp = tempfile.mkdtemp()
    cmd = ["vslvmmount", "-o", str(offset), path, mp]
    try:
        p = run(cmd, capture_output=True, check=False, text=True)
    except Exception as e:
        L.error("failed to execute command %s: %r", cmd, e)
        rmdir(mp)
        return ""

    out, err = p.stdout.strip(), p.stderr.strip()
    if p.returncode or not os.path.ismount(mp):
        L.error("retcode: %d, stdout: %s, stderr: %s", p.returncode, out, err)
        rmdir(mp)
        return ""

    return mp


#! /usr/bin/env python

import numpy as np
import math as Math

def rad(d):
    return d * Math.pi /180.0

###  return hdistance unit is miles ################
def compute_hdistance(lon1,lat1,lon2,lat2):
    lon1 = float(lon1)
    lat1 = float(lat1)
    lon2 = float(lon2)
    lat2 = float(lat2)

    radLat1 = rad(lat1)
    radLat2 = rad(lat2)
    a = radLat1 - radLat2
    b = rad(lon1) - rad(lon2)
    s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
        Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b/2),2)))
    s = s * 6378.137
    s = round(s * 10000) / 10
    return s

### return odistance ############################
def compute_odistance(lon1,lat1,lon2,lat2):
    return Math.sqrt((lon1 - lon2)*(lon1 - lon2) + (lat1 - lat2)*(lat1 - lat2))