import argparse, sys

def main(args):

    with open(f'job_script_{args.exp_name}.job', 'w') as out_file:
        out_file.write('#!/bin/bash\n')
        out_file.write(f'#SBATCH --job-name={args.exp_name}\n')
        out_file.write('#SBATCH --account=project_2001403\n')
        out_file.write('#SBATCH --partition=gpu\n')
        out_file.write(f'#SBATCH --time=10:00:00\n')
        out_file.write('#SBATCH --ntasks=1\n')
        out_file.write('#SBATCH --cpus-per-task=10\n')
        out_file.write('#SBATCH --mem-per-cpu=8000\n')
        out_file.write(f'#SBATCH --gres=gpu:v100:1\n')
        out_file.write('#SBATCH --mail-user=ilmari.kylliainen@helsinki.fi\n')
        out_file.write(f'#SBATCH --output={args.exp_name}.log\n')
        out_file.write('#SBATCH --mail-type=BEGIN,END,FAIL\n\n')
        out_file.write('module purge\n\n')
        out_file.write('module load pytorch/1.9\n\n')
        out_file.write(f'srun python ../output_model_predictions.py ../../models/{args.model} ../../../datasets/{args.data_file} --batch_size {args.batch_size} --out_file {args.exp_name}_predictions.json')

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Generate SLURM job scripts for QA model evaluation')

    parser.add_argument('model', metavar='model_dir', help='Model to use for creating the predictions.')
    parser.add_argument('data_file', metavar='data.json', help='Data to use for creating the predictions.')
    parser.add_argument("--exp_name", required=True, help="Name for experiment and output model")
    parser.add_argument("--batch_size", required=False, type=int, choices=[8, 16, 32], default=16,
                        help="Batch size. Choose 8, 16, or 32")

    if len(sys.argv) == 1:
        parser.print_help()
        sys.exit(1)

    args = parser.parse_args()

    main(args)


# -*- coding: utf-8 -*-
"""
Created on Sat Jun 13 18:13:21 2020

@author: FFernandes
"""

# Libbraries importing section
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import pandas as pd
import json
import dash_table
from dash_table.Format import Format, Group, Scheme, Symbol
import datetime as dt
import numpy as np

'''
Source of Population Info: https://www.ibge.gov.br/estatisticas/sociais/populacao/9103-estimativas-de-populacao.html?edicao=25272&t=downloads
Source of Maps: https://www.ibge.gov.br/geociencias/organizacao-do-territorio/15774-malhas.html?=&t=downloads
Source of COVID data: https://brasil.io/dataset/covid19/caso?format=csv
'''

#Reading data section

## Reading last update info
readfile = open('last_update.txt', 'r')
last_update = readfile.readlines()
last_update_message = 'Dados atualizados em: ' + last_update[0]
readfile.close()

## Reading maps  #Those maps were prepared before construction of these program
with open('Maps/brasil_1.json', encoding='utf-8') as geofile1:
    jdataBra_states = json.load(geofile1)

with open('Maps/brasil_2.json', encoding='utf-8') as geofile2:
    jdataBra_rgintermed = json.load(geofile2)

with open('Maps/brasil_3.json', encoding='utf-8') as geofile3:
    jdataBra_rgimed = json.load(geofile3)

with open('Maps/brasil_4.json', encoding='utf-8') as geofile4:
    jdataBra_municipios = json.load(geofile4)

## Reading info from the hard drive previously downloaded and manipulated file

estados = pd.read_csv('Tables/uf.csv', encoding = 'latin1', dtype = 'object')
rgint = pd.read_csv('Tables/rgintermed.csv', encoding = 'latin1', dtype = 'object')
rgi = pd.read_csv('Tables/rgi.csv', encoding = 'latin1', dtype = 'object')
cidades = pd.read_csv('Tables/municipios.csv', encoding = 'latin1', dtype = 'object')
cidades['localidade'] = cidades['nome_mun'] + ' / ' + cidades['sigla']

tdf = pd.read_csv('Tables/tdf.csv',
                  parse_dates = ['data'],
                  dtype = {'total_casos': 'int64',
                           'total_obitos': 'int64',
                           'novos_casos': 'int64',
                           'novos_obitos': 'int64',
                           'cod_rgi': 'object',
                           'cod_rgint': 'object',
                           'cod_uf': 'object'},
                           encoding = 'latin1',
                           compression = 'gzip')
tdf = tdf.drop(axis = 1, labels = ['Unnamed: 0'])

tdf_rgi = pd.read_csv('Tables/tdf_rgi.csv',
                  parse_dates = ['data'],
                  dtype = {'total_casos': 'int64',
                           'total_obitos': 'int64',
                           'novos_casos': 'int64',
                           'novos_obitos': 'int64',
                           'cod_rgi': 'object',
                           'cod_rgint': 'object',
                           'cod_uf': 'object'},
                           encoding = 'latin1',
                           compression = 'gzip')
tdf_rgi = tdf_rgi.drop(axis = 1, labels = ['Unnamed: 0'])

tdf_rgint = pd.read_csv('Tables/tdf_rgint.csv',
                  parse_dates = ['data'],
                  dtype = {'total_casos': 'int64',
                           'total_obitos': 'int64',
                           'novos_casos': 'int64',
                           'novos_obitos': 'int64',
                           'cod_rgint': 'object',
                           'cod_uf': 'object'},
                           encoding = 'latin1',
                           compression = 'gzip')
tdf_rgint = tdf_rgint.drop(axis = 1, labels = ['Unnamed: 0'])

tdf_estados = pd.read_csv('Tables/tdf_estados.csv',
                  parse_dates = ['data'],
                  dtype = {'total_casos': 'int64',
                           'total_obitos': 'int64',
                           'novos_casos': 'int64',
                           'novos_obitos': 'int64'},
                           encoding = 'latin1',
                           compression = 'gzip')
tdf_estados = tdf_estados.drop(axis = 1, labels = ['Unnamed: 0'])

### Keeping only the updated info
df_hoje = tdf[tdf['atualizado']].copy()

df_estados_hoje = tdf_estados[tdf_estados['data'] == tdf_estados['data'].max()].copy()
df_rgint_hoje = tdf_rgint[tdf_rgint['data'] == tdf_rgint['data'].max()].copy()
df_rgi_hoje = tdf_rgi[tdf_rgi['data'] == tdf_rgi['data'].max()].copy()

'''
NOTA:
Os dados de município carregam apenas as informações mais atuais de cada município.
Os dados agrupados de regiões e estados consideram a soma dos dados mais atuais de cada município que os compõem.
Isso significa que possivelmente os dados dessas regiões estarão um pouco desatualizadas.
'''

##### including a column to make identification easier
df_hoje['name'] = df_hoje['nome_mun'] + ' / ' + df_hoje['sigla']
df_rgi_hoje['name'] = df_rgi_hoje['nome_rgi'] + ' / ' + df_rgi_hoje['sigla']
df_rgint_hoje['name'] = df_rgint_hoje['nome_rgint'] + ' / ' + df_rgint_hoje['sigla']
df_estados_hoje['name'] = df_estados_hoje['estado']

### Summing up total numbers
total_cases = df_estados_hoje['total_casos'].sum()
total_deaths = df_estados_hoje['total_obitos'].sum()

### Creating the locations for each level of geo organization
locations_states = df_estados_hoje['sigla']
locations_rgintermed = df_rgint_hoje['cod_rgint']
locations_rgi =df_rgi_hoje['cod_rgi']
locations_mun = df_hoje['codigo_ibge']

### Creating options for the filters
opcoes_estados = estados.sort_values(by = 'estado')[['estado', 'sigla']].rename(columns = {'estado': 'label', 'sigla': 'value'}).to_dict(orient = 'records')

#Creating the core of the dashboard
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

server = app.server #Disable it when in test / development mode. Enable it to Production mode
app.layout = html.Div([
        #dcc.Store(id="store_data"), #later understand why this is useful

        html.Div(html.H1('Painel COVID-19 nas localidades brasileiras')),

        html.Div(html.Label(last_update_message)),

        html.Div([
                html.Div(
                    [html.Label('Total de casos confirmados no Brasil'),
                     html.H1(total_cases)
                    ]),

                html.Div(
                    [html.Label('Total de óbitos confirmados no Brasil'),
                     html.H1(total_deaths)
                    ]),
                    ],
            id="header",
            className="row flex-display",
            style={"margin-bottom": "25px"}), #closing Div with overall scores

        html.Div([
            html.Div(html.H3('Configure o mapa:')),
            html.Div([html.Div('Nível de organização do território:'),
                    dcc.Dropdown(id = 'dropdown_geo_level',
                                   options = [
                                           {'label': 'Estados', 'value': 'states'},
                                           {'label': 'Regiões Geográficas Intermediárias', 'value': 'rgintermed'},
                                           {'label': 'Regiões Geográficas Imediatas', 'value': 'rgi'},
                                           {'label': 'Municípios', 'value': 'cities'}
                                           ],
                                   value = 'states',
                                   multi = False)]),

            html.Div([html.Div('Variável de interesse:'),
                    dcc.Dropdown(id = 'dropdown_info',
                                   options = [
                                           {'label': 'A - Total de casos', 'value': 'total_casos'},
                                           {'label': 'B - Total de óbitos', 'value': 'total_obitos'},
                                           {'label': 'C - Novos casos', 'value': 'novos_casos'},
                                           {'label': 'D - Novos óbitos', 'value': 'novos_obitos'},
                                           {'label': 'E - Média móvel dos novos casos - 7 dias', 'value': 'mm_7dias_novos_casos'},
                                           {'label': 'F - Média móvel dos novos óbitos - 7 dias', 'value': 'mm_7dias_novos_obitos'},
                                           {'label': 'G - Total de casos por 100.000 habitantes', 'value': 'total_casos%'},
                                           {'label': 'H - Total de óbitos por 100.000 habitantes', 'value': 'total_obitos%'},
                                           {'label': 'I - Novos casos por 100.000 habitantes', 'value': 'novos_casos%'},
                                           {'label': 'J - Novos óbitos por 100.000 habitantes', 'value': 'novos_obitos%'},
                                           {'label': 'K - Média móvel dos novos casos - 7 dias - por 100.000 habitantes', 'value': 'mm_7dias_novos_casos%'},
                                           {'label': 'L - Média móvel dos novos óbitos - 7 dias - por 100.000 habitantes', 'value': 'mm_7dias_novos_obitos%'},
                                           {'label': 'M - Variação percentual da média móvel dos novos casos - 7 dias', 'value': 'var_casos_7dias'},
                                           {'label': 'N - Variação percentual da média móvel dos novos óbitos - 7 dias', 'value': 'var_obitos_7dias'}],
                                   value = 'total_casos',
                                   multi = False)]), ### closing Div-Div-Dropdown

            html.Div(html.H3('E aplique filtros:')),
            html.Div([
                    html.Div('Estado:'),
                    dcc.Dropdown(id = 'dropdown_state',
                                   options = opcoes_estados,
                                   value = 'all',
                                   searchable = True,
                                   clearable = True,
                                   placeholder = 'Todos os Estados',
                                   multi = True),

                    html.Div('Região Geográfica Intermediária:'),
                    dcc.Dropdown(id = 'dropdown_rgint',
                                   value = 'all',
                                   searchable = True,
                                   clearable = True,
                                   placeholder = 'Todas as Regiões Geográficas Intermediárias',
                                   multi = True),

                    html.Div('Região Geográfica Imediata:'),
                    dcc.Dropdown(id = 'dropdown_rgi',
                                   value = 'all',
                                   searchable = True,
                                   clearable = True,
                                   placeholder = 'Todas as Regiões Geográficas Imediatas',
                                   multi = True),

                    html.Div('Município:'),
                    dcc.Dropdown(id = 'dropdown_city',
                                   value = 'all',
                                   searchable = True,
                                   clearable = True,
                                   placeholder = 'Todos os Municípios',
                                   multi = True)

                                 ]),

        ]),  ### closing configuring and filtering section.

        html.Div(dcc.Graph(id = 'main_map')), ### closing Div-Graph - Map

        html.Div(dcc.Graph(id = 'main_lineplot')), ### closing Div-Graph - Line plot

        html.Div([html.H6('Legenda:'),
                  html.Div('A - Total de casos'),
                  html.Div('B - Total de óbitos'),
                  html.Div('C - Novos casos'),
                  html.Div('D - Novos óbitos'),
                  html.Div('E - Média móvel dos novos casos - 7 dias'),
                  html.Div('F - Média móvel dos novos óbitos - 7 dias'),
                  html.Div('G - Total de casos por 100.000 habitantes'),
                  html.Div('H - Total de óbitos por 100.000 habitantes'),
                  html.Div('I - Novos casos por 100.000 habitantes'),
                  html.Div('J - Novos óbitos por 100.000 habitantes'),
                  html.Div('K - Média móvel dos novos casos - 7 dias - por 100.000 habitantes'),
                  html.Div('L - Média móvel dos novos óbitos - 7 dias - por 100.000 habitantes'),
                  html.Div('M - Variação percentual da média móvel dos novos casos - 7 dias'),
                  html.Div('N - Variação percentual da média móvel dos novos óbitos - 7 dias')
                                    ]),

        html.Div(dash_table.DataTable(id='table',
                                      export_format = 'csv',
                                      style_cell={'textAlign': 'left'},
                                      style_data_conditional=[
                                                {
                                                'if': {'row_index': 'odd'},
                                                'backgroundColor': 'rgb(248, 248, 248)'
                                                },
                                                {
                                                    'if': {'column_id': 'pop'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'total_casos'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'total_obitos'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'novos_casos'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'novos_obitos'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'mm_7dias_novos_casos'},
                                                    'textAlign': 'right'
                                                },
                                                 {
                                                    'if': {'column_id': 'mm_7dias_novos_obitos'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'total_casos%'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'total_obitos%'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'novos_casos%'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'novos_obitos%'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'mm_7dias_novos_casos%'},
                                                    'textAlign': 'right'
                                                },
                                                {
                                                    'if': {'column_id': 'mm_7dias_novos_obitos%'},
                                                    'textAlign': 'right'
                                                }
                                              ],
                                      style_header= [
                                              {
                                              'backgroundColor': 'rgb(230, 230, 230)',
                                              'fontWeight': 'bold'
                                              },
                                              {
                                               'if': {'column_id': 'pop'},
                                               'textAlign': 'right'
                                                }
                                              ]
                                      )),
        html.Div(dcc.Markdown(
                            '''
                              Os dados reportados neste painel foram obtidos na plataforma Brasil.IO, repositório de dados públicos disponibilizados em formato acessível. Conheça e apoie esta iniciativa.

                              https://brasil.io/home/

                              Os mapas foram obtidos no site do IBGE em formato shapefile e simplificados usando a biblioteca Geopandas.
                              '''
                              ) #closing Markdown
        ) ### closing Div-Markdown
]) #closing main html.Div

# Callbacks - here is where the magic happens
@app.callback([Output('main_map', 'figure'),
               Output('dropdown_rgint', 'options'),
               Output('dropdown_rgint', 'disabled'),
               Output('dropdown_rgi', 'options'),
               Output('dropdown_rgi', 'disabled'),
               Output('dropdown_city', 'options'),
               Output('dropdown_city', 'disabled'),
               Output('table', 'data'),
               Output('table', 'columns'),
               Output('main_lineplot', 'figure')],
              [Input('dropdown_info', 'value'),
               Input('dropdown_geo_level', 'value'),
               Input('dropdown_state', 'value'),
               Input('dropdown_rgint', 'value'),
               Input('dropdown_rgi', 'value'),
               Input('dropdown_city', 'value')])
def update_figure(info, geo_level, filtro_estados, filtro_rgint, filtro_rgi, filtro_cidades):
     ## Managing filters:
    if filtro_estados == 'all':
        estados_selecionados = estados['sigla']
    elif filtro_estados == []:
        estados_selecionados = estados['sigla']
    elif filtro_estados == '':
        estados_selecionados = estados['sigla']
    else:
        estados_selecionados = filtro_estados

    if filtro_rgint == 'all':
        rgint_selecionadas = rgint['cod_rgint']
    elif filtro_rgint == []:
        rgint_selecionadas = rgint['cod_rgint']
    elif filtro_rgint == '':
        rgint_selecionadas = rgint['cod_rgint']
    else:
        rgint_selecionadas = filtro_rgint

    if filtro_rgi == 'all':
        rgi_selecionadas = rgi['cod_rgi']
    elif filtro_rgi == []:
        rgi_selecionadas = rgi['cod_rgi']
    elif filtro_rgi == '':
        rgi_selecionadas = rgi['cod_rgi']
    else:
        rgi_selecionadas = filtro_rgi

    if filtro_cidades == 'all':
        cidades_selecionadas = cidades['CD_GEOCODI']
    elif filtro_cidades == []:
        cidades_selecionadas = cidades['CD_GEOCODI']
    elif filtro_cidades == '':
        cidades_selecionadas = cidades['CD_GEOCODI']
    else:
        cidades_selecionadas = filtro_cidades

    ### according to the geo_level, we'll choose the right DataFrame
    if geo_level == 'states':
        df = df_estados_hoje
        locations = locations_states
        geojson = jdataBra_states
        featureidkey = 'properties.SIGLA_UF'
        key = 'sigla'
        time_df = tdf_estados
        rgint_disabled = True
        rgi_disabled = True
        city_disabled = True
        ## Applying filters
        filtered_df = df[df['sigla'].isin(estados_selecionados)]

    elif geo_level == 'rgintermed':
        df = df_rgint_hoje
        locations = locations_rgintermed
        geojson = jdataBra_rgintermed
        featureidkey='properties.CD_RGINT'
        key = 'cod_rgint'
        time_df = tdf_rgint
        rgint_disabled = False
        rgi_disabled = True
        city_disabled = True
        ## Applying filters
        filtered_df = df[(df['sigla'].isin(estados_selecionados)) & (df['cod_rgint'].isin(rgint_selecionadas))]

    elif geo_level == 'rgi':
        df = df_rgi_hoje
        locations = locations_rgi
        geojson = jdataBra_rgimed
        featureidkey = featureidkey='properties.CD_RGI'
        key = 'cod_rgi'
        time_df = tdf_rgi
        rgint_disabled = False
        rgi_disabled = False
        city_disabled = True
        ## Applying filters
        filtered_df = df[(df['sigla'].isin(estados_selecionados)) &
                         (df['cod_rgint'].isin(rgint_selecionadas)) &
                         (df['cod_rgi'].isin(rgi_selecionadas))]

    else: #else select cities
        df = df_hoje
        locations = locations_mun
        geojson = jdataBra_municipios
        featureidkey = featureidkey='properties.CD_MUN'
        key = 'codigo_ibge'
        time_df = tdf
        rgint_disabled = False
        rgi_disabled = False
        city_disabled = False
        ## Applying filters
        filtered_df = df[(df['sigla'].isin(estados_selecionados)) &
                         (df['cod_rgint'].isin(rgint_selecionadas)) &
                         (df['cod_rgi'].isin(rgi_selecionadas)) &
                         (df['codigo_ibge'].isin(cidades_selecionadas))]

    ## arranging the dataframe to be shown
    blank_df = pd.DataFrame(index = locations)
    df_to_show = blank_df.merge(filtered_df, left_index = True, right_on = key, how = 'left').fillna(0)

    if info in ['var_casos_7dias', 'var_obitos_7dias']:
        color_sequence = 'Picnic'
        zauto = True

    else:
        color_sequence = 'RdPu'
        zauto = False

    ## Creating the map figure
    figure_map = {'data': [go.Choroplethmapbox(geojson = geojson,
                                           locations = locations,
                                           z = df_to_show[info],
                                           featureidkey = featureidkey,
                                           text = df_to_show['name'],
                                           zauto = zauto,
                                           zmid = 0,
                                           colorscale = color_sequence
                                           )],
                  'layout': go.Layout(title = 'Mapa da COVID-19 no Brasil',
                                      hovermode = 'closest',
                                      width = 1500,
                                      height = 1000,
                                      mapbox={'zoom': 3,
                                              'style': 'carto-positron',
                                              'center': {"lat": -15, "lon": -60}})
                                   } #closing figure

    table_data=filtered_df.sort_values(by = info, ascending = False).to_dict('records')

    formatlocale_ptBR_int = Format(
            scheme = Scheme.fixed,
            precision = 0,
            group = Group.yes,
            groups = 3,
            group_delimiter = '.',
            decimal_delimiter = ',',
            symbol = Symbol.no,
            symbol_prefix = u'R$')

    formatlocale_ptBR_float = Format(
            scheme = Scheme.fixed,
            precision = 1,
            group = Group.yes,
            groups = 3,
            group_delimiter = '.',
            decimal_delimiter = ',',
            symbol = Symbol.no,
            symbol_prefix = u'R$')

    formatlocale_ptBR_percent = Format(
            scheme = Scheme.fixed,
            precision = 1,
            group = Group.yes,
            groups = 3,
            group_delimiter = '.',
            decimal_delimiter = ',',
            symbol = Symbol.no,
            symbol_prefix = u'R$')

    ### Organizing the format of the table
    if geo_level == 'states':
        columns=[{'id': 'estado', 'name': 'Estado', 'type': 'text'},
                 {'id': 'sigla', 'name': 'Sigla', 'type': 'text'},
                 {'id': 'pop', 'name': 'População', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_casos', 'name': 'A', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_obitos', 'name': 'B', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_casos', 'name': 'C', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_obitos', 'name': 'D', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'mm_7dias_novos_casos', 'name': 'E', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos', 'name': 'F', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_casos%', 'name': 'G', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_obitos%', 'name': 'H', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_casos%', 'name': 'I', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_obitos%', 'name': 'J', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_casos%', 'name': 'K', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos%', 'name': 'L', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_casos_7dias', 'name': 'M', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_obitos_7dias', 'name': 'N', 'type': 'numeric', 'format': formatlocale_ptBR_float}
                 ]
    elif geo_level == 'rgintermed':
        columns=[{'id': 'nome_rgint', 'name': 'Região Intermediária', 'type': 'text'},
                 {'id': 'estado', 'name': 'Estado', 'type': 'text'},
                 {'id': 'pop', 'name': 'População', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_casos', 'name': 'A', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_obitos', 'name': 'B', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_casos', 'name': 'C', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_obitos', 'name': 'D', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'mm_7dias_novos_casos', 'name': 'E', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos', 'name': 'F', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_casos%', 'name': 'G', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_obitos%', 'name': 'H', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_casos%', 'name': 'I', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_obitos%', 'name': 'J', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_casos%', 'name': 'K', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos%', 'name': 'L', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_casos_7dias', 'name': 'M', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_obitos_7dias', 'name': 'N', 'type': 'numeric', 'format': formatlocale_ptBR_float}
                 ]
    elif geo_level == 'rgi':
        columns=[{'id': 'nome_rgi', 'name': 'Região Imediata', 'type': 'text'},
                 {'id': 'nome_rgint', 'name': 'Região Intermediária', 'type': 'text'},
                 {'id': 'estado', 'name': 'Estado', 'type': 'text'},
                 {'id': 'pop', 'name': 'População', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_casos', 'name': 'A', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_obitos', 'name': 'B', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_casos', 'name': 'C', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_obitos', 'name': 'D', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'mm_7dias_novos_casos', 'name': 'E', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos', 'name': 'F', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_casos%', 'name': 'G', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_obitos%', 'name': 'H', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_casos%', 'name': 'I', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_obitos%', 'name': 'J', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_casos%', 'name': 'K', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos%', 'name': 'L', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_casos_7dias', 'name': 'M', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_obitos_7dias', 'name': 'N', 'type': 'numeric', 'format': formatlocale_ptBR_float}
                 ]
    else: #municipios
        columns=[{'id': 'nome_mun', 'name': 'Município', 'type': 'text'},
                 {'id': 'nome_rgi', 'name': 'Região Imediata', 'type': 'text'},
                 {'id': 'nome_rgint', 'name': 'Região Intermediária', 'type': 'text'},
                 {'id': 'sigla', 'name': 'Estado', 'type': 'text'},
                 {'id': 'pop', 'name': 'População', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_casos', 'name': 'A', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'total_obitos', 'name': 'B', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_casos', 'name': 'C', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'novos_obitos', 'name': 'D', 'type': 'numeric', 'format': formatlocale_ptBR_int},
                 {'id': 'mm_7dias_novos_casos', 'name': 'E', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos', 'name': 'F', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_casos%', 'name': 'G', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'total_obitos%', 'name': 'H', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_casos%', 'name': 'I', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'novos_obitos%', 'name': 'J', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_casos%', 'name': 'K', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'mm_7dias_novos_obitos%', 'name': 'L', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_casos_7dias', 'name': 'M', 'type': 'numeric', 'format': formatlocale_ptBR_float},
                 {'id': 'var_obitos_7dias', 'name': 'N', 'type': 'numeric', 'format': formatlocale_ptBR_float}
                 ]

    ## creating the line plot
    top_places = filtered_df.sort_values(by = info, ascending = False).head(5)
    data = []
    for i, place in top_places.iterrows():
        trace = go.Scatter(x = time_df[time_df[key] == place[key]]['data'],
                           y = time_df[time_df[key] == place[key]][info],
                           mode = 'lines+markers',
                           name = place['name'])
        data.append(trace)

    figure_line = go.Figure(data=data,
                       layout=go.Layout(title = 'Curva de evolução da COVID nas localidades selecionadas'))

    figure_line = figure_line.update_xaxes(rangeslider_visible=True,
                                 range = ['2020-02-25', time_df['data'].max()],
                                 rangeselector=dict(buttons=list([
                                         dict(count=7, label="7d", step="day", stepmode="backward"),
                                         dict(count=14, label="14d", step="day", stepmode="backward"),
                                         dict(count=1, label="1m", step="month", stepmode="backward"),
                                         dict(count=6, label="6m", step="month", stepmode="backward"),
                                         dict(count=1, label="YTD", step="year", stepmode="todate"),
                                         dict(step="all")])))

    figure_line.update_layout(
            autosize=True,
            #width=1500,
            #height=1200,
            margin=dict(
                l=50,
                r=50,
                b=100,
                t=100,
                pad=4),
                    paper_bgcolor="LightSteelBlue")

    ## Managing dropdown options
    ####opcoes_estados = estados.sort_values(by = 'estado')[['estado', 'sigla']].rename(columns = {'estado': 'label', 'sigla': 'value'}).to_dict(orient = 'records')
    rgint_options = rgint[rgint['sigla'].isin(estados_selecionados)].sort_values(by = ['estado', 'nome_rgint'])[['nome_rgint', 'cod_rgint']].rename(columns = {'nome_rgint': 'label', 'cod_rgint': 'value'}).to_dict(orient = 'records')

    rgi_options = rgi[(rgi['sigla'].isin(estados_selecionados)) &
                      (rgi['cod_rgint'].isin(rgint_selecionadas))].sort_values(by = ['estado', 'nome_rgi'])[['nome_rgi', 'cod_rgi']].rename(columns = {'nome_rgi': 'label', 'cod_rgi': 'value'}).to_dict(orient = 'records')

    city_options = cidades[(cidades['sigla'].isin(estados_selecionados)) &
                           (cidades['cod_rgint'].isin(rgint_selecionadas)) &
                           (cidades['cod_rgi'].isin(rgi_selecionadas))].sort_values(by = ['localidade'])[['localidade', 'CD_GEOCODI']].rename(columns = {'localidade': 'label', 'CD_GEOCODI': 'value'}).to_dict(orient = 'records')

    return figure_map, rgint_options, rgint_disabled, rgi_options, rgi_disabled, city_options, city_disabled, table_data, columns, figure_line

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


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

from django.db import models, migrations
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('rtiapp', '0031_auto_20151228_2331'),
    ]

    operations = [
        migrations.CreateModel(
            name='Message',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('message_text', models.TextField()),
                ('read_status', models.BooleanField(default=False)),
                ('message_date', models.DateTimeField(auto_now_add=True)),
                ('receiver', models.ForeignKey(related_name='receiver', to=settings.AUTH_USER_MODEL, null=True)),
                ('sender', models.ForeignKey(related_name='sender', to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
    ]


from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import matplotlib.pyplot as plt

from tensorflow import keras

y = np.load('labels/00e5671e594a6fe621c3605fcc5a0e4466ba6478.npy')
x = np.rot90(np.load('spectro/00e5671e594a6fe621c3605fcc5a0e4466ba6478.npy'))
x = x[0:y.shape[0]]
x = np.expand_dims(x, axis=2)

print('x.shape', x.shape)
print('y.shape', y.shape)

model = keras.Sequential()
model.add(keras.layers.LSTM(128))
model.add(keras.layers.Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history = model.fit(x, y, epochs=250, shuffle=False)
print(model.summary())
# score = model.evaluate(x_test, y_test)
# print(score)

plt.plot(history.history['accuracy'])
plt.plot(history.history['loss'])
plt.title('onset by neural network')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['accuracy', 'loss'], loc='upper left')
plt.show()


import io
import struct
from typing import Iterable

from antlir.btrfs_diff.parse_send_stream import (
    AttributeKind,
    check_magic,
    check_version,
    CommandKind,
    file_unpack,
    parse_send_stream,
    read_attribute,
    read_command,
)
from antlir.btrfs_diff.send_stream import SendStreamItem
from antlir.btrfs_diff.tests.demo_sendstreams import gold_demo_sendstreams
from antlir.btrfs_diff.tests.demo_sendstreams_expected import (
    get_filtered_and_expected_items,
)

from antlir.tests.common import AntlirTestCase

def _parse_stream_bytes(s: bytes) -> Iterable[SendStreamItem]:
    return parse_send_stream(io.BytesIO(s))

class ParseSendStreamTestCase(AntlirTestCase):
    def test_verify_gold_parse(self) -> None:
        stream_dict = gold_demo_sendstreams()
        filtered_items, expected_items = get_filtered_and_expected_items(
            items=[
                *_parse_stream_bytes(stream_dict["create_ops"]["sendstream"]),
                *_parse_stream_bytes(stream_dict["mutate_ops"]["sendstream"]),
            ],
            build_start_time=stream_dict["create_ops"]["build_start_time"],
            build_end_time=stream_dict["mutate_ops"]["build_end_time"],
            dump_mode=False,
        )
        self.assertEqual(filtered_items, expected_items)

    def test_errors(self) -> None:
        with self.assertRaisesRegex(RuntimeError, "Magic b'xxx', not "):
            check_magic(io.BytesIO(b"xxx"))
        with self.assertRaisesRegex(RuntimeError, "but expected version 1"):
            check_version(io.BytesIO(b"abcd"), expected=1)
        with self.assertRaisesRegex(RuntimeError, "Not enough bytes"):
            file_unpack("<Q", io.BytesIO(b""))

        cmd_header_2_attrs = struct.pack(
            "<IHI",
            2 * (2 + 2 + 3),  # length excluding this header
            CommandKind.MKFILE.value,
            0,  # crc32c
        )

        with self.assertRaisesRegex(RuntimeError, "CommandHead.* got 0 bytes"):
            read_command(io.BytesIO(cmd_header_2_attrs))

        with self.assertRaisesRegex(RuntimeError, "AttributeH.* got 0 bytes"):
            read_attribute(
                io.BytesIO(
                    struct.pack(
                        "<HH",
                        AttributeKind.PATH.value,
                        # length excluding this header -- error: we write no
                        # data!
                        3,
                    )
                )
            )

        with self.assertRaisesRegex(RuntimeError, "\\.PATH occurred twice"):
            read_command(
                io.BytesIO(
                    cmd_header_2_attrs
                    + struct.pack(
                        "<" + "HH3s" * 2,  # 2 attributes
                        AttributeKind.PATH.value,
                        3,  # length excluding this header
                        b"cat",
                        AttributeKind.PATH.value,
                        3,  # length excluding this header
                        b"dog",
                    )
                )
            )


# coding: utf-8

"""
    Bacalhau API

    This page is the reference of the Bacalhau REST API. Project docs are available at https://docs.bacalhau.org/. Find more information about Bacalhau at https://github.com/bacalhau-project/bacalhau.  # noqa: E501

    OpenAPI spec version: ${PYPI_VERSION}
    Contact: team@bacalhau.org
    Generated by: https://github.com/swagger-api/swagger-codegen.git
"""

import pprint
import re  # noqa: F401

import six

class ResourceUsageData(object):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """

    """
    Attributes:
      swagger_types (dict): The key is attribute name
                            and the value is attribute type.
      attribute_map (dict): The key is attribute name
                            and the value is json key in definition.
    """
    swagger_types = {"cpu": "float", "disk": "int", "gpu": "int", "memory": "int"}

    attribute_map = {"cpu": "CPU", "disk": "Disk", "gpu": "GPU", "memory": "Memory"}

    def __init__(self, cpu=None, disk=None, gpu=None, memory=None):  # noqa: E501
        """ResourceUsageData - a model defined in Swagger"""  # noqa: E501
        self._cpu = None
        self._disk = None
        self._gpu = None
        self._memory = None
        self.discriminator = None
        if cpu is not None:
            self.cpu = cpu
        if disk is not None:
            self.disk = disk
        if gpu is not None:
            self.gpu = gpu
        if memory is not None:
            self.memory = memory

    @property
    def cpu(self):
        """Gets the cpu of this ResourceUsageData.  # noqa: E501

        cpu units  # noqa: E501

        :return: The cpu of this ResourceUsageData.  # noqa: E501
        :rtype: float
        """
        return self._cpu

    @cpu.setter
    def cpu(self, cpu):
        """Sets the cpu of this ResourceUsageData.

        cpu units  # noqa: E501

        :param cpu: The cpu of this ResourceUsageData.  # noqa: E501
        :type: float
        """

        self._cpu = cpu

    @property
    def disk(self):
        """Gets the disk of this ResourceUsageData.  # noqa: E501

        bytes  # noqa: E501

        :return: The disk of this ResourceUsageData.  # noqa: E501
        :rtype: int
        """
        return self._disk

    @disk.setter
    def disk(self, disk):
        """Sets the disk of this ResourceUsageData.

        bytes  # noqa: E501

        :param disk: The disk of this ResourceUsageData.  # noqa: E501
        :type: int
        """

        self._disk = disk

    @property
    def gpu(self):
        """Gets the gpu of this ResourceUsageData.  # noqa: E501

        :return: The gpu of this ResourceUsageData.  # noqa: E501
        :rtype: int
        """
        return self._gpu

    @gpu.setter
    def gpu(self, gpu):
        """Sets the gpu of this ResourceUsageData.

        :param gpu: The gpu of this ResourceUsageData.  # noqa: E501
        :type: int
        """

        self._gpu = gpu

    @property
    def memory(self):
        """Gets the memory of this ResourceUsageData.  # noqa: E501

        bytes  # noqa: E501

        :return: The memory of this ResourceUsageData.  # noqa: E501
        :rtype: int
        """
        return self._memory

    @memory.setter
    def memory(self, memory):
        """Sets the memory of this ResourceUsageData.

        bytes  # noqa: E501

        :param memory: The memory of this ResourceUsageData.  # noqa: E501
        :type: int
        """

        self._memory = memory

    def to_dict(self):
        """Returns the model properties as a dict"""
        result = {}

        for attr, _ in six.iteritems(self.swagger_types):
            value = getattr(self, attr)
            if isinstance(value, list):
                result[attr] = list(
                    map(lambda x: x.to_dict() if hasattr(x, "to_dict") else x, value)
                )
            elif hasattr(value, "to_dict"):
                result[attr] = value.to_dict()
            elif isinstance(value, dict):
                result[attr] = dict(
                    map(
                        lambda item: (item[0], item[1].to_dict())
                        if hasattr(item[1], "to_dict")
                        else item,
                        value.items(),
                    )
                )
            else:
                result[attr] = value
        if issubclass(ResourceUsageData, dict):
            for key, value in self.items():
                result[key] = value

        return result

    def to_str(self):
        """Returns the string representation of the model"""
        return pprint.pformat(self.to_dict())

    def __repr__(self):
        """For `print` and `pprint`"""
        return self.to_str()

    def __eq__(self, other):
        """Returns true if both objects are equal"""
        if not isinstance(other, ResourceUsageData):
            return False

        return self.__dict__ == other.__dict__

    def __ne__(self, other):
        """Returns true if both objects are not equal"""
        return not self == other


import os
import numpy as np
import tensorflow as tf
import math
from tensorflow.examples.tutorials.mnist import input_data as mnist_data
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
from skimage import io
from .resizeimageutil import img_pad
import pickle
from sklearn.preprocessing import OneHotEncoder
import zipfile

#STEP 2 - Architecture Selection
def model(x,numclass, logits=False, training=False):
    with tf.variable_scope('conv0'):
        z = tf.layers.conv2d(x, filters=32, kernel_size=[2, 2], padding='same', activation=tf.nn.relu)
        z = tf.layers.max_pooling2d(z, pool_size=[2, 2], strides=2)
    with tf.variable_scope('conv1'):
        z = tf.layers.conv2d(z, filters=64, kernel_size=[3, 3], padding='same', activation=tf.nn.relu)
        z = tf.layers.max_pooling2d(z, pool_size=[2, 2], strides=2)
    with tf.variable_scope('flatten'):
        shape = z.get_shape().as_list()
        z = tf.reshape(z, [-1, np.prod(shape[1:])])
    logits_ = tf.layers.dense(z, units=numclass, name='logits')
    y = tf.nn.softmax(logits_, name='ybar')
    if logits:
        return y, logits_
    return y

def jsma(model,numclass, x, y=None, epochs=1, eps=1.0, k=1, clip_min=0.0, clip_max=1.0, score_fn=lambda t, o: t * tf.abs(o)):
    n = tf.shape(x)[0]
    target = tf.cond(tf.equal(0, tf.rank(y)), lambda: tf.zeros([n], dtype=tf.int32) + y, lambda: y)
    target = tf.stack((tf.range(n), target), axis=1) # 2xn
    if isinstance(epochs, float):
        tmp = tf.to_float(tf.size(x[0])) * epochs
        epochs = tf.to_int32(tf.floor(tmp))
    return _jsma_impl(model,numclass, x, target, epochs=epochs, eps=eps, clip_min=clip_min, clip_max=clip_max, score_fn=score_fn)

def _prod(iterable):
    ret = 1
    for x in iterable:
        ret *= x
    return ret

def _jsma_impl(model,numclass, x, yind, epochs, eps, clip_min, clip_max, score_fn):
    def cond(i, xadv):
        return tf.less(i, epochs)
    def body(i, xadv):
        ybar = model(xadv,numclass)
        dy_dx = tf.gradients(ybar, xadv)[0]
        yt = tf.gather_nd(ybar, yind)
        dt_dx = tf.gradients(yt, xadv)[0]
        do_dx = dy_dx - dt_dx
        c0 = tf.logical_or(eps < 0, xadv < clip_max)
        c1 = tf.logical_or(eps > 0, xadv > clip_min)
        cond = tf.reduce_all([dt_dx >= 0, do_dx <= 0, c0, c1], axis=0)
        cond = tf.to_float(cond)
        score = cond * score_fn(dt_dx, do_dx)
        shape = score.get_shape().as_list()
        dim = _prod(shape[1:])
        score = tf.reshape(score, [-1, dim])
        ind = tf.argmax(score, axis=1)
        dx = tf.one_hot(ind, dim, on_value=eps, off_value=0.0)
        dx = tf.reshape(dx, [-1] + shape[1:])
        xadv = tf.stop_gradient(xadv + dx)
        xadv = tf.clip_by_value(xadv, clip_min, clip_max)
        return i+1, xadv
    #STEP 3 - Substitute Dataset Labeling
    _, xadv = tf.while_loop(cond, body, (0, tf.identity(x)), back_prop=False, name='_jsma_batch')
    return xadv

class Environment():
    pass

# CLASS ENVIRONMENT DEFINITION, BEFORE RUNNING MAIN

def evaluate(sess, ambiente, X_data, y_data, batch_size=128):
    print('\nValutazione')
    n_sample = X_data.shape[0]
    n_batch = int((n_sample+batch_size-1) / batch_size)
    loss, acc = 0, 0
    for batch in range(n_batch):
        print(' batch {0}/{1}'.format(batch + 1, n_batch), end='\r')
        start = batch * batch_size
        end = min(n_sample, start + batch_size)
        cnt = end - start
        batch_loss, batch_acc = sess.run([ambiente.loss, ambiente.acc],feed_dict={ambiente.x: X_data[start:end], ambiente.y: y_data[start:end]})
        loss += batch_loss * cnt
        acc += batch_acc * cnt
    loss /= n_sample
    acc /= n_sample
    print(' loss: {0:.4f} acc: {1:.4f}'.format(loss, acc))
    return loss, acc

#STEP 4 - Substitute DNN F Training
def training(sess, ambiente, X_data, y_data, X_valid=None, y_valid=None, epochs=1, load=False, shuffle=True, batch_size=128, name='model'):
    if load:
        if not hasattr(ambiente, 'saver'):
            return print('\nError')
        return ambiente.saver.restore(sess, 'model/{}'.format(name))
    n_sample = X_data.shape[0]
    n_batch = int((n_sample+batch_size-1) / batch_size)
    for epoch in range(epochs):
        print('\nEpoch {0}/{1}'.format(epoch + 1, epochs))
        if shuffle:
            print('\nShuffling data')
            ind = np.arange(n_sample)
            np.random.shuffle(ind)
            X_data = X_data[ind]
            y_data = y_data[ind]
        for batch in range(n_batch):
            print(' batch {0}/{1}'.format(batch + 1, n_batch), end='\r')
            start = batch * batch_size
            end = min(n_sample, start + batch_size)
            sess.run(ambiente.train_op, feed_dict={ambiente.x: X_data[start:end], ambiente.y: y_data[start:end], ambiente.training: True})
        if X_valid is not None:
            evaluate(sess, ambiente, X_valid, y_valid)
    # if hasattr(ambiente, 'saver'):
    #     os.makedirs('model', exist_ok=True)
    #     ambiente.saver.save(sess, 'model/{}'.format(name))

#STEP 5 - Jacobian-Based Dataset Augmentation
def make_jsma(sess, numclass,ambiente, X_data, epochs=0.2, eps=1.0, batch_size=128):
    print('\nInizio JSMA')
    n_sample = X_data.shape[0]
    n_batch = int((n_sample + batch_size - 1) / batch_size)
    X_adv = np.empty_like(X_data)
    for batch in range(n_batch):
        print(' batch {0}/{1}'.format(batch + 1, n_batch), end='\r')
        start = batch * batch_size
        end = min(n_sample, start + batch_size)
        feed_dict = {ambiente.x: X_data[start:end], ambiente.target: np.random.choice(numclass),ambiente.adv_epochs: epochs,ambiente.adv_eps: eps}
        adv = sess.run(ambiente.x_jsma, feed_dict=feed_dict)
        X_adv[start:end] = adv
    print()
    return X_adv

def mainjsma(fileParent1):
#STEP 1 - Substitute Training Dataset Collection
    tf.reset_default_graph()
    old_v = tf.logging.get_verbosity()
    tf.logging.set_verbosity(tf.logging.ERROR)

    f = zipfile.ZipFile(os.path.join("uploadfile", fileParent1), 'r')  # 压缩文件位置
    fileParent = fileParent1.split(".")[0]
    if not os.path.exists("extract/" + fileParent):
        os.makedirs("extract/" + fileParent)
    for file in f.namelist():
        f.extract(file, "extract/" + fileParent + "/")  # 解压位置
    f.close()
    wh = 0
    # import imageio
    x_train = []
    y_train = []
    # pathname=r"D:\downloads\chromefile\fgsmWebTotal\fgsmWeb\extract"
    pathname = "extract/" + fileParent
    filenamel = []
    if os.path.exists(pathname):
        filelist = os.listdir(pathname)
        numclass = len(filelist)
        for f in filelist:
            subf = os.path.join(pathname, f)
            if os.path.isdir(subf):
                filelist1 = os.listdir(subf)
                for f1 in filelist1:
                    if f1.endswith(".bmp") or f1.endswith(".png") or f1.endswith(".jpg") or f1.endswith(".ppm"):
                        filepath = os.path.join(subf, f1)
                        # print()
                        img = io.imread(filepath)
                        if wh == 0:
                            wh = img.shape[0]
                        x_train.append(img_pad(img, wh))
                        y_train.append(int(f))
                        filenamel.append(f + "/" + f1)
    x_train = np.array(x_train)
    input_shape = x_train.shape[1:]
    y_train = np.array(y_train)

    onehotencode=OneHotEncoder()
    y_train=onehotencode.fit_transform(y_train.reshape(-1,1))
    # print(y_train)
    y_train=(y_train).toarray()
    # print(y_train)

    tf.logging.set_verbosity(old_v)

    print('\nInizializzazione grafo')
    ambiente =  Environment()

    with tf.variable_scope('model'):
        ambiente.x = tf.placeholder(tf.float32, (None, wh,wh, 3),name='x')
        ambiente.y = tf.placeholder(tf.float32, (None, numclass), name='y')
        ambiente.training = tf.placeholder_with_default(False, (), name='mode')
        ambiente.ybar, logits = model(ambiente.x,numclass, logits=True, training=ambiente.training)
        with tf.variable_scope('acc'):
            count = tf.equal(tf.argmax(ambiente.y, axis=1), tf.argmax(ambiente.ybar, axis=1))
            ambiente.acc = tf.reduce_mean(tf.cast(count, tf.float32), name='acc')
        with tf.variable_scope('loss'):
            xent = tf.nn.softmax_cross_entropy_with_logits_v2(labels=ambiente.y, logits=logits)
            ambiente.loss = tf.reduce_mean(xent, name='loss')
        with tf.variable_scope('train_op'):
            optimizer = tf.train.AdamOptimizer()
            ambiente.train_op = optimizer.minimize(ambiente.loss)
        ambiente.saver = tf.train.Saver()

    with tf.variable_scope('model', reuse=True):
        ambiente.target = tf.placeholder(tf.int32, (), name='target')
        ambiente.adv_epochs = tf.placeholder_with_default(20, shape=(), name='epochs')
        ambiente.adv_eps = tf.placeholder_with_default(0.2, shape=(), name='eps')
        ambiente.x_jsma = jsma(model, numclass,ambiente.x, ambiente.target, eps=ambiente.adv_eps, epochs=ambiente.adv_epochs)
        sess = tf.InteractiveSession()
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        saver=tf.train.Saver(max_to_keep=1)
# runs training and evaluating
# STEP 4

    training(sess, ambiente, x_train, y_train, epochs=2)

    # if os.path.exists("ckpt/fgsm.ckpt"):
    #     print("loaded")
    #     saver.restore(sess, "ckpt/fgsm.ckpt")
    #
    # saver.save(sess, 'ckpt_jsma/jsma.ckpt')

    X_adv = make_jsma(sess, numclass,ambiente, x_train/255.0, epochs=40, eps=0.8)

    pathsave = r"download"
    # if not os.path.exists(pathsave+"/extract"):
    #     os.makedirs(pathsave+"/extract")
    tmp = ''
    for i, X_advP in enumerate(X_adv):

        if not os.path.exists(pathsave + "/" + fileParent + "/" + filenamel[i].split("/")[0]):
            os.makedirs(pathsave + "/" + fileParent + "/" + filenamel[i].split("/")[0])
        if len(tmp) == 0:
            tmp = "static" + "/" + filenamel[i].split("/")[-1].split(".")[0] + ".png"
            print(tmp)
            io.imsave(tmp, (X_advP * 255).astype(int))
        io.imsave(os.path.join(pathsave + "/" + fileParent, filenamel[i]), (X_advP * 255).astype(int))
    evaluate(sess, ambiente, x_train, y_train)
    evaluate(sess, ambiente, X_adv, y_train)
    return tmp
if __name__ == "__main__":
    mainjsma("data.zip")


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import datetime

import isodate

from django.utils import timezone

from crashstats.supersearch.form_fields import split_on_operator

DEFAULT_RANGE_DAYS = 7

def get_date_boundaries(params):
    """Return the date boundaries in a set of parameters.

    Return a tuple with 2 datetime objects, the first one is the lower bound
    date and the second one is the upper bound date.
    """
    default_date_range = datetime.timedelta(days=DEFAULT_RANGE_DAYS)

    start_date = None
    end_date = None

    if not params.get("date"):
        end_date = timezone.now()
        start_date = end_date - default_date_range
    else:
        for param in params["date"]:
            d = isodate.parse_datetime(split_on_operator(param)[1]).replace(
                tzinfo=timezone.utc
            )

            if param.startswith("<") and (not end_date or end_date < d):
                end_date = d
            if param.startswith(">") and (not start_date or start_date > d):
                start_date = d

        if not end_date:
            end_date = timezone.now()

        if not start_date:
            start_date = end_date - default_date_range

    return (start_date, end_date)


import pandas as pd
import numpy as np
from sklearn.externals import joblib

from main import main
from Core.classific import cv, my_test
from setting import VEC_TRAIN_PATH, VEC_TEST_PATH, VEC_LARGE_PATH

def vec():
	data = pd.read_csv(VEC_TRAIN_PATH)
	print("训练集规模：", data.shape)
	print("训练集标签分布：\n", pd.value_counts(data.get("62")))
	test = pd.read_csv(VEC_TEST_PATH)
	print("测试集规模：", test.shape)
	print("测试集标签分布：\n", pd.value_counts(test.get("62")))
	target = "62"
	# 去掉ID和属性列
	x_columns = [x for x in data.columns if x not in [target]]
	t_columns = [x for x in test.columns if x not in [target]]
	data_x = np.array(data[x_columns])
	data_y = np.array(data[target])
	test_x = np.array(test[t_columns])
	test_y = np.array(test[target])
	clf = joblib.load("core/models/rf_62_vec.m")
	print("平衡数据集随机森林模型交叉验证评估结果：")
	cv(data_x, data_y, clf)
	print("-----------------------------------")

	print("平衡数据集随机森林模型测试集评估结果：")
	my_test(test_x, test_y, clf)
	print("-----------------------------------")

	large = pd.read_csv(VEC_LARGE_PATH)
	print("完全不平衡数据集规模：", large.shape)
	print("完全不平衡数据集标签分布：\n", pd.value_counts(large.get("62")))
	# 去掉ID和属性列
	l_columns = [x for x in large.columns if x not in [target]]
	large_x = np.array(large[l_columns])
	large_y = np.array(large[target])
	my_test(large_x, large_y, clf)

def predict():
	with open("./input_test.txt", "rb") as f:
		input_data = [data.decode().strip() for data in f.readlines()]
	input_data.append("")
	data = input_data
	rf = joblib.load("./core/models/rf_62_vec.m")
	vec_proba, desc = main(data, "rf")
	print("特征向量概率向量：", vec_proba)
	if vec_proba[0] < 0.6:
		print("\n ----> 预判为假！！")
	else:
		print("\n ----> 预判为真！！")

if __name__ == '__main__':
	print("开始测试特征向量分类模型性能：")
	vec()
	print("开始测试对输入数据识别：")
	predict()


import os

import matplotlib.pyplot as plt
import numpy as np

def intermediate_output_to_fig(z, img_ctr, label, predicted, confidence):
    n_columns = 4
    n_rows = 3
    f, ax = plt.subplots(n_rows, n_columns)
    f.tight_layout()
    for i, channel_o in enumerate(z):
        c_img = channel_o * 1
        r, c = divmod(i, n_columns)
        ax[r][c].set_title('Channel ' + str(i))
        # ax[r][c].xticks([])
        # ax[r][c].yticks([])
        low = np.quantile(c_img, 0.01)
        high = np.quantile(c_img, 0.99)
        im = ax[r][c].imshow(c_img, vmin=low, vmax=high, interpolation=None)
        f.colorbar(im, ax=ax[r][c])
    if not os.path.exists(f"bottleneck_output/7b-3ch/{label}"):
        os.mkdir(f"bottleneck_output/7b-3ch/{label}")
    f.savefig(f"bottleneck_output/7b-3ch/{label}/{predicted}{img_ctr}_conf={confidence}_3.png")
    plt.close()


import tensorflow as tf
import tensorflow_probability as tfp

ds = tfp.distributions

class SeqVAE(object):

    def __init__(self, hparams, encoder, decoder_theta, decoder_beta, discriminator):
        self._hparams = hparams

        self.encoder = encoder
        self.decoder_theta = decoder_theta
        self.decoder_beta = decoder_beta
        self.discriminator = discriminator

        # z's prior distribution
        self.z_p = ds.MultivariateNormalDiag(
            loc=[0.] * self._hparams.z_size,
            scale_diag=[1.] * self._hparams.z_size)

        # global step
        self._global_step = tf.train.get_or_create_global_step()

    def compute_loss(self, target_sequence, sequence_len):
        # encoder ==> kl divergence
        z_q = self.encoder.encode(target_sequence, sequence_len)
        z = z_q.sample()
        # compute formula:https://zhuanlan.zhihu.com/p/22464760
        self._kl_div = ds.kl_divergence(z_q, self.z_p)
        # <Improving variational inference with inverse auto regressive flow>, NIPS 2016
        # Bits to exclude from KL loss per dimension.
        free_nats = self._hparams.free_bits * tf.math.log(2.0)
        self.kl_cost = tf.reduce_mean(tf.maximum(self._kl_div - free_nats, 0))
        # kl divergence scalar
        self.kl_bits_scalar = tf.summary.scalar("vae-pg/kl_bits", tf.reduce_mean(self._kl_div / tf.math.log(2.0)))
        self.kl_loss_scalar = tf.summary.scalar("vae-pg/kl_loss", self.kl_cost)

        # decoder & discriminator ==> rewards
        hparams = self._hparams
        kl_loss_beta = (1.0 - tf.pow(hparams.beta_decay_rate, tf.to_float(self._global_step))) * hparams.max_beta
        tf.summary.scalar("vae-pg/kl_loss_beta", kl_loss_beta)
        step = hparams.compute_rewards_step
        max_seq_len = hparams.max_seq_len
        batch_size = hparams.batch_size
        rollout_num = hparams.rollout_num
        self.pg_loss = 0

        _, output_sm = self.decoder_theta.generate_part(z, target_sequence, max_seq_len)

        # accuracy
        truth = tf.argmax(target_sequence, axis=2)
        predictions = tf.argmax(output_sm, axis=2)
        self.train_accuracy_real = tf.reduce_sum(tf.cast(tf.equal(truth, predictions), tf.float32)) \
                                   / (1.0 * batch_size * max_seq_len)
        tf.summary.scalar("vae-pg/train_accuracy_real", self.train_accuracy_real)

        rewards = []
        for k in range(rollout_num):
            count = 0
            for i in range(step, max_seq_len, step):
                gen_part_output, _ = self.decoder_beta.generate_part(z, target_sequence, i)
                scores_sm, _ = self.discriminator.discriminate(gen_part_output)
                reward = tf.squeeze(scores_sm[:, :1], 1)
                if k == 0:
                    rewards.append(reward)
                else:
                    rewards[count] += reward
                count += 1

            # the last token reward
            scores_sm, _ = self.discriminator.discriminate(target_sequence)
            reward = tf.squeeze(scores_sm[:, :1], 1)
            if k == 0:
                rewards.append(reward)
            else:
                rewards[count] += reward

        rewards = [reward / (1.0 * rollout_num) for reward in rewards]
        rewards = tf.transpose(tf.convert_to_tensor(rewards), perm=[1, 0])  # batch_size * (max_seq_len // step)

        # make log(output_sm) not get nan
        output_sm = tf.clip_by_value(output_sm, 1e-20, 1.0)
        log_signal = tf.reduce_sum(target_sequence * tf.log(output_sm), axis=2)  # batch_size * max_seq_len
        log_sum = []
        for i in range(max_seq_len // step):
            log_sum.append(tf.reduce_sum(log_signal[:, i * step:(i + 1) * step], axis=1))
        log_sum = tf.transpose(tf.convert_to_tensor(log_sum), perm=[1, 0])

        pg_loss = tf.reduce_sum(log_sum * rewards, axis=1)  # batch_size
        self.pg_loss = - tf.reduce_mean(pg_loss)
        tf.summary.scalar("vae-pg/pg_loss", self.pg_loss)

        # VAE loss
        # <Generating sentences from a continuous space>, CONLL 2016
        self.vae_loss = kl_loss_beta * self.kl_cost + self.pg_loss
        tf.summary.scalar("vae-pg/vae_loss", self.vae_loss)

        return self.vae_loss

    def discriminator_loss(self, inputs, labels, dropout_keep_prob=1.0):
        _, self.dis_loss = self.discriminator.discriminate(inputs, labels, dropout_keep_prob)
        return self.dis_loss

    def generate(self, z):
        return self.decoder_theta.generate(z)

    def update_decoder_beta(self):
        return self.decoder_beta.update(self.decoder_theta.collect_all_variables())


# -*- coding: utf-8 -*-
# @Time    : 2018/11/15 20:29
# @Author  : MengnanChen
# @FileName: feeder_mlp.py
# @Software: PyCharm

import linecache

import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import pandas as pd

from models import hparams

class CachedLineList:
    def __init__(self, fname):
        self._fname = fname

    def __getitem__(self, x):
        if type(x) is slice:
            return [linecache.getline(self._fname, n + 1)
                    for n in range(x.start, x.stop, x.step)]
        else:
            return linecache.getline(self._fname, x + 1)

    def __getslice__(self, beg, end):
        return self[beg:end:1]

class Feeder(object):
    def __init__(self, data_path, sess):
        super(Feeder, self).__init__()
        self._data_path = data_path

        data = pd.read_csv(self._data_path)
        label_name = 'emolabel'
        data[label_name] = LabelEncoder().fit_transform(data[label_name])
        feature_column_names = [x for x in data.columns if x != 'emolabel']
        test_size = hparams.test_batch_size if hparams.test_batch_size is not None \
            else hparams.test_batch_size_percent
        X_train, X_test, y_train, y_test = train_test_split(data[feature_column_names].values,
                                                            data['emolabel'].values,
                                                            test_size=test_size)

        self.num_batch = len(X_train) // hparams.batch_size
        self.eval_num_batch = len(X_test) // hparams.batch_size

        self._placeholder = (
            tf.placeholder(tf.float32, shape=(None, hparams.n_features), name='input_x'),
            tf.placeholder(tf.int32, shape=(None,), name='input_y')
        )

        with tf.device('/cpu:0'):
            dataset = tf.data.Dataset.from_tensor_slices(self._placeholder)
            dataset = dataset.batch(hparams.batch_size).shuffle(buffer_size=1000).repeat()
            iterator = dataset.make_initializable_iterator()
            sess.run(iterator.initializer, feed_dict=dict(zip(self._placeholder, (X_train, y_train))))
            self.next_element = iterator.get_next()

            eval_dataset = tf.data.Dataset.from_tensor_slices(self._placeholder)
            eval_dataset = eval_dataset.batch(hparams.batch_size).repeat()
            eval_iterator = eval_dataset.make_initializable_iterator()
            sess.run(eval_iterator.initializer, feed_dict=dict(zip(self._placeholder, (X_test, y_test))))
            self.eval_next_element = eval_iterator.get_next()


# -*- coding: utf-8 -*-

import urllib
import pandas
import csv
import ast
import datetime
import requests
import sched
import time
import json
import sys
from bs4 import BeautifulSoup

titles = ['庫存', '股票代碼', '股票名稱', '產業類別', \
        '高價(2.5%)', '合理價(5%)', '低價(10%)', \
        '殖利率(%)', '當盤成交價', \
        '現金股利', '股票股利', '盈餘配股', '公積配股', \
        '成立時間', '上市(櫃)時間', \
        '營業毛利率', '營業利益率', '稅前淨利率', '資產報酬率', '股東權益報酬率', '每股淨值', \
        '前4季盈餘', '前3季盈餘', '前2季盈餘', '前1季盈餘', \
        '前4年盈餘', '前3年盈餘', '前2年盈餘', '前1年盈餘', \
        '個股資訊', '持股比例',
        ]

def check_float(var, multiple):
    try:
        return round(float(var) * multiple, 1)
    except ValueError:
        return 0;

def price(id):
    url = 'https://tw.stock.yahoo.com/q/q?s='+id
    try:
        page = urllib.request.urlopen(url).read()
        soup = BeautifulSoup(page.decode('cp950','ignore').encode('utf-8'), features="lxml")
        table = soup.find_all(text='成交')[0].parent.parent.parent
        stock_name = table.select('tr')[1].select('td')[0].text
        price = table.select('tr')[1].select('td')[2].text
        #print(stock_name.strip('加到投資組合'))
        #print('成交價 :',price)
        return price
    except:
        print('股票代碼錯誤或查無此代碼!!')
        return 0

def percentage(stock_price, cash_share):
    return round(float(cash_share) * 100.0/float(stock_price), 2)

def profile(id, had):
    url = 'https://tw.stock.yahoo.com/d/s/company_'+id+'.html'
    resp = urllib.request.urlopen(url)
    html = resp.read()
    soup = BeautifulSoup(html.decode('cp950','ignore').encode('utf-8'), features="lxml")
    try:
        name = soup.title.string.split('-')[0]
        table1 = soup.findAll(text='基 本 資 料')[0].parent.parent.parent
        table2 = soup.findAll(text='營業毛利率')[0].parent.parent.parent
        category = table1.select('tr')[1].select('td')[1].text.strip()
        cashshare = table1.select('tr')[1].select('td')[3].text.strip("元")
        stockshare = table1.select('tr')[2].select('td')[3].text.strip("元")
        earnshare = table1.select('tr')[3].select('td')[3].text.strip("元")
        remainshare = table1.select('tr')[4].select('td')[3].text.strip("元")
        setupDate = table1.select('tr')[2].select('td')[1].text.strip()
        onboardDate = table1.select('tr')[3].select('td')[1].text.strip()
        grossprofit = table2.select('tr')[1].select('td')[1].text.strip()
        netprofit = table2.select('tr')[2].select('td')[1].text.strip()
        taxprofit = table2.select('tr')[3].select('td')[1].text.strip()
        rate = table2.select('tr')[4].select('td')[1].text.strip()
        bq4 = table2.select('tr')[1].select('td')[3].text.strip().strip("元")
        bq3 = table2.select('tr')[2].select('td')[3].text.strip().strip("元")
        bq2 = table2.select('tr')[3].select('td')[3].text.strip().strip("元")
        bq1 = table2.select('tr')[4].select('td')[3].text.strip().strip("元")
        earn = table2.select('tr')[5].select('td')[1].text.strip()
        netvalue = table2.select('tr')[5].select('td')[2].text.strip("每股淨值:").strip().strip("元")
        by4 = table2.select('tr')[1].select('td')[5].text.strip().strip("元")
        by3 = table2.select('tr')[2].select('td')[5].text.strip().strip("元")
        by2 = table2.select('tr')[3].select('td')[5].text.strip().strip("元")
        by1 = table2.select('tr')[4].select('td')[5].text.strip().strip("元")
        current_price = price(id)
        result = list([had, id, name, category , \
                check_float(cashshare, 40), check_float(cashshare, 20), check_float(cashshare, 10), \
                percentage(current_price, cashshare), current_price, \
                cashshare, stockshare, earnshare, remainshare, \
                setupDate, onboardDate, \
                grossprofit, netprofit, taxprofit, rate, earn, netvalue, \
                bq4, bq3, bq2, bq1, \
                by4, by3, by2, by1, \
                'https://tw.stock.yahoo.com/d/s/company_'+id+'.html', \
                'https://fubon-ebrokerdj.fbs.com.tw/z/zc/zck/zck_'+id+'.djhtm', \
                ])
    except:
        result = [id, name, 'access fail']
    #print('result=',result)
    return result

def parse_and_save(candidate, name):
    candidate_list = [titles]
    for row in candidate:
        result = profile(str(row[0]), row[1])
        print(result)
        candidate_list.append(result)

    #save result
    f = open(name+".csv","w+")
    w = csv.writer(f, lineterminator='\n')
    w.writerows(candidate_list)
    f.close()


#!/usr/bin/env python

from setuptools import setup

def reqs_from_file(filename):
    with open(filename) as f:
        lineiter = (line.rstrip() for line in f if not line.startswith("#"))
        return list(filter(None, lineiter))

setup(
    name='rewrite_cassandra_yaml',
    version='0.0.3',
    description='Script to rewrite a cassandra yaml file to allow remote access',
    author='Hugh Brown',
    author_email='hughdbrown@yahoo.com',

    # Required packages
    install_requires=reqs_from_file('requirements.txt'),
    # tests_require=reqs_from_file('test-requirements.txt'),

    # Main packages
    packages=[
        'src',
    ],

    zip_safe=False,

    scripts=[
        'bin/rewrite-cassandra-yaml',
    ],
    entry_points={
        'console_scripts': [
            'rewrite-cassandra-yaml = src.rewrite_cassandra_yaml:main',
        ],
    },
)


import pygame

import time

import sys

pygame.init()
pygame.font.init()

window_width = 720
window_height = 480
window = pygame.display.set_mode([window_width, window_height])

startbg = pygame.image.load('start_screenbg.png').convert()

clock = pygame.time.Clock()

def image_load(filename):
    image = pygame.image.load(filename).convert_alpha()
    image = pygame.transform.scale(image, (20, 30))
    return image

def loop(window, cycles, state):
    if state == 0:
        window.blit(startbg, (0,0))
        flame_sprite.update()
        flame_sprite.draw(window)
        if cycles < 1900:
            state = 0
        else:
            state = 1
    if state == 1:
        if cycles < 2600:
            window.blit(startbg, (0, 0))
            state = 1
        else:
            state = 2
    elif state == 2:
        window.blit(startbg, (0, 0))
        if cycles < 3900:
            text_print(window, 'Hey, good job', 80)
            state = 2
        else:
            state = 3
    elif state == 3:
        window.blit(startbg, (0, 0))
        if cycles < 4900:
            text_print(window, "You didn't blow up", 80)
            state = 3
        else:
            state = 4
    elif state == 4:
        window.blit(startbg, (0, 0))
        if cycles < 5900:
            text_print(window, "Try to drive better next time", 50)
            state = 4
        else:
            state = 5
    cycles += 1
    return state, cycles

def text_print(window, text, size):
    font = pygame.font.SysFont('haettenschweiler', size)
    textsurface = font.render(text, False, (255, 255, 255))
    window.blit(textsurface, ((window_width/2) - (textsurface.get_rect().width/2), (window_height/2) - (textsurface.get_rect().height/2)))

class Flames(pygame.sprite.Sprite):
    """
    #Creates moving flames during intro screen
    """
    def __init__(self):
        # Constructor function
        pygame.sprite.Sprite.__init__(self)

        # Flames animation list
        self.animation = []

         # Index shit
        self.index = 0

        # Loading all flame images
        self.animation.append(image_load('flame1.png'))
        self.animation.append(image_load('flame2.png'))
        self.animation.append(image_load('flame3.png'))
        self.animation.append(image_load('flame4.png'))
        self.animation.append(image_load('flame5.png'))
        self.animation.append(image_load('flame6.png'))
        self.animation.append(image_load('flame7.png'))

        # Rect and pos
        self.image = self.animation[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = (407, 275)

    def update(self):

        self.index += 1
        if self.index >= len(self.animation):
            self.index = 0
        self.image = self.animation[self.index]

class Flames2(pygame.sprite.Sprite):
    """
    #Creates moving flames during intro screen
    """
    def __init__(self):
        # Constructor function
        pygame.sprite.Sprite.__init__(self)

        # Flames animation list
        self.animation = []

         # Index shit
        self.index = 0

        # Loading all flame images
        self.animation.append(image_load('flame1.png'))
        self.animation.append(image_load('flame2.png'))
        self.animation.append(image_load('flame3.png'))
        self.animation.append(image_load('flame4.png'))
        self.animation.append(image_load('flame5.png'))
        self.animation.append(image_load('flame6.png'))
        self.animation.append(image_load('flame7.png'))

        # Rect and pos
        self.image = self.animation[self.index]
        self.rect = self.image.get_rect()
        self.rect.center = (347, 201)

    def update(self):

        self.index += 1
        if self.index >= len(self.animation):
            self.index = 0
        self.image = self.animation[self.index]

# Fire shit
flame_sprite = pygame.sprite.Group()
flames = Flames()
flame_sprite.add(flames)
flames2 = Flames2()
flame_sprite.add(flames2)
flame_sprite.update()

def run(window, bg):
    state = 0
    window.blit(bg, (0, 0))

    cycles = 0

    while state < 5:
        state, cycles = loop(window, cycles, state)
        pygame.display.flip()
    if state == 5:
        pygame.quit()


from pyspark.sql import SparkSession
from pyspark.sql.functions import *
from pyspark.sql.types import *

spark = SparkSession.builder.appName("Covid ETL").getOrCreate()

covid_data = spark.read.csv('gs://[bucket-name]/data/*.csv',
                            sep=';', header='true', inferSchema='true')

# #### Processing data ####

# Removing hour from "data" column

covid_data = covid_data.withColumn("data", split(col("data"), " ").getItem(0))

# Preparing total cases data to transformation
covid_data = covid_data.withColumn("casosAcumulado",
                                   covid_data['casosAcumulado'].cast(FloatType()))

# Data is updated to December 2021

data = "2021-12-31"  # date = 2021 December 31th
regiao = "Brasil"  # region columns with "Brasil" value

brasil_covid = covid_data.where((covid_data.regiao == regiao) &
                                (covid_data.data == data))

# #### Creating views ####

# View for the recovered and under monitoring, respectively

recuperados_brasil = brasil_covid\
    .select(brasil_covid["Recuperadosnovos"].alias("Casos_Recuperados"),
            brasil_covid["emAcompanhamentoNovos"].alias("Em_Acompanhamento"))

# View of accumulated cases, new cases and incidence per 100 thousand people, respectively

casos_brasil = brasil_covid\
    .select(brasil_covid["casosAcumulado"].alias("Casos_Acumulados"),
            brasil_covid["casosNovos"].alias("Casos_Novos"),
            (round(brasil_covid["casosAcumulado"] /
                   brasil_covid["populacaoTCU2019"]*100000, 1))
            .alias("Incidencia"))

# View for deaths, fatality and mortality rate per 100 thousand people, respectively

obitos_brasil = brasil_covid\
    .select(brasil_covid["obitosAcumulado"].alias("Obitos_acumulados"),
            brasil_covid["obitosNovos"].alias("Obitos_novos"),
            (round(brasil_covid["obitosAcumulado"] /
                   brasil_covid["casosAcumulado"]*100, 1)).alias("Letalidade"),
            (round(brasil_covid["obitosAcumulado"] /
                   brasil_covid["populacaoTCU2019"]*100000, 1)).alias("Mortalidade"))

# #### Uploading views to BigQuery dataset tables ####

recuperados_brasil.write \
  .format("bigquery") \
  .option("writeMethod", "direct",) \
  .mode("overwrite") \
  .save("covid_views.recuperados_brasil")

casos_brasil.write \
  .format("bigquery") \
  .option("writeMethod", "direct") \
  .mode("overwrite") \
  .save("covid_views.casos_brasil")

obitos_brasil.write \
  .format("bigquery") \
  .option("writeMethod", "direct") \
  .mode("overwrite") \
  .save("covid_views.obitos_brasil")


from functools import partial
import sys
from PyQt5.QtWidgets import (
    QApplication,
    QMainWindow,
    qApp,
    QGraphicsScene,
    QListWidget,
    QHBoxLayout,
    QWidget,
    QMessageBox,
    QInputDialog,
    QAction,
    QFileDialog,
)
from canvas import MyCanvas
from PyQt5.QtGui import QImage,QPainter,QColor

class MainWindow(QMainWindow):
    """
    主窗口类
    """
    def __init__(self):
        super().__init__()
        self.item_cnt = 0

        # 使用QListWidget来记录已有的图元，并用于选择图元。注：这是图元选择的简单实现方法，更好的实现是在画布中直接用鼠标选择图元
        self.list_widget = QListWidget(self)
        self.list_widget.setMinimumWidth(200)

        # 使用QGraphicsView作为画布
        self.scene = QGraphicsScene(self)
        h,w = 600,600
        self.scene.setSceneRect(0, 0, h, w)
        self.canvas:MyCanvas = MyCanvas(self.scene, self)
        self.canvas.setFixedSize(h, w)
        self.canvas.main_window = self
        self.canvas.list_widget = self.list_widget
        self.modified = False
        self.file_name = ''
        # 设置菜单栏
        menubar = self.menuBar()
        file_menu = menubar.addMenu('文件')
        file_menu.addAction('设置画笔').triggered.connect(self.canvas.setColor)
        file_menu.addAction('重置画布').triggered.connect(self.resetCanvas)
        file_menu.addAction('调整画布大小').triggered.connect(partial(self.resetCanvas,True))
        file_menu.addAction('保存画布').triggered.connect(self.save_slot)
        file_menu.addAction('加载画布').triggered.connect(self.load_slot)
        file_menu.addAction('导出画布').triggered.connect(self.export_slot)
        file_menu.addAction('退出').triggered.connect(self.quit_slot)
        draw_menu = menubar.addMenu('DRAW')
        line_menu = draw_menu.addMenu('Line')
        line_menu.addAction('Naive')
        line_menu.addAction('DDA')
        line_menu.addAction('Bresenham')
        polygon_menu = draw_menu.addMenu('Polygon')
        polygon_menu.addAction('DDA')
        polygon_menu.addAction('Bresenham')
        draw_menu.addAction('Ellipse')
        curve_menu = draw_menu.addMenu('Curve')
        curve_menu.addAction('Bezier')
        curve_menu.addAction('B-spline')
        edit_menu = menubar.addMenu('Edit')
        edit_menu.addAction('translate')
        edit_menu.addAction('rotate')
        edit_menu.addAction('scale')
        clip_menu = edit_menu.addMenu('clip')
        clip_menu.addAction('Cohen-Sutherland')
        clip_menu.addAction('Liang-Barsky')
        history_menu = menubar.addMenu('历史记录')
        self.undo_action = history_menu.addAction('撤销')
        self.undo_action.triggered.connect(self.canvas.op_record.undo)
        self.redo_action = history_menu.addAction('重做')
        self.redo_action.triggered.connect(self.canvas.op_record.redo)
        self.delete_action = history_menu.addAction('删除')
        self.delete_action.triggered.connect(self.canvas.deleteItem)
        self.canvas.actionChanged.connect(self.updateMenu)
        self.updateMenu()
        # 连接信号和file_menu.children()槽函数
        for func_menu in [draw_menu,edit_menu]:
            for menu in func_menu.children():
                if isinstance(menu,QAction):
                    type = menu.text()
                    menu.triggered.connect(partial(self.draw_slot,type,''))
                    continue
                type = menu.title()
                for action in menu.children():
                    algr = action.text()
                    action.triggered.connect(partial(self.draw_slot,type,algr))
        self.list_widget.currentTextChanged.connect(self.canvas.selectionChanged)
        # 设置主窗口的布局
        self.hbox_layout = QHBoxLayout()
        self.hbox_layout.addWidget(self.canvas)
        self.hbox_layout.addWidget(self.list_widget, stretch=1)
        self.central_widget = QWidget()
        self.central_widget.setLayout(self.hbox_layout)
        self.setCentralWidget(self.central_widget)
        self.statusBar().showMessage('freenom')
        self.resize(h, w)
        self.setWindowTitle('CG Demo')
        self.canvas.start("freenom","")

    def get_id(self,inc=False):
        if inc:
            self.item_cnt += 1
        return str(self.item_cnt)

    def resetCanvas(self,resize=False):
        h,w = None,None
        if resize:
            h = QInputDialog.getInt(self, '请输入', '长度', 800, 200, 1500)[0]
            w = QInputDialog.getInt(self, '请输入', '宽度', 800, 200, 900)[0]
            self.scene.setSceneRect(0, 0, h, w)
        self.list_widget.clearSelection()
        self.list_widget.clear()
        self.item_cnt = 0
        self.file_name = ''
        self.canvas.reset(h,w)

    def draw_slot(self,type:str,algr:str):
        type = type.lower()
        if self.canvas.start(type,algr):
            self.statusBar().showMessage(type+'-'+algr)

    def quit_slot(self):
        if self.modified:
            ret = QMessageBox.question(self, '退出 - 是否保存', '是否保存当前草稿？', QMessageBox.Yes | QMessageBox.No | QMessageBox.Cancel, QMessageBox.Yes)
            if ret == QMessageBox.Cancel:
                return
            if ret == QMessageBox.Yes:
                self.save_slot()
        qApp.quit()

    def export_slot(self):
        file_name = QFileDialog.getSaveFileName(self,caption="导出画布", filter="bmp;;png;;jpg")
        if file_name[0] != '':
            scene = self.canvas.scene()
            scene.clearSelection()
            image = QImage(scene.sceneRect().size().toSize(), QImage.Format.Format_ARGB32)
            image.fill(QColor(255,255,255))
            painter = QPainter(image)
            scene.render(painter)
            image.save(file_name[0]+'.'+file_name[1])
            painter.end()

    def save_slot(self):
        if self.file_name == '':
            file_name = QFileDialog.getSaveFileName(self,caption="保存画布", filter="canvas")
            if file_name[0] == '':
                return
            self.file_name = file_name[0] + '.' + file_name[1]
        self.canvas.saveToFile(self.file_name)

    def load_slot(self):
        file_name = QFileDialog.getOpenFileName(self,caption="加载画布", filter="画布(*.canvas)")
        if file_name[0] != '':
            self.file_name = file_name[0]
        self.canvas.loadFromFile(self.file_name)

    def closeEvent(self, e) -> None:
        self.quit_slot()
        return super().closeEvent(e)

    def updateMenu(self):
        self.undo_action.setEnabled(self.canvas.op_record.canUndo())
        self.redo_action.setEnabled(self.canvas.op_record.canRedo())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())


from django.shortcuts import render, redirect
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.views.generic import View, ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Document, Useful
from .forms import DocumentForm
from django.http import HttpResponseRedirect
from django.utils import timezone
from django.urls import reverse
from django.db.models import Q
# Create your views here.
User = get_user_model

class DocumentList(ListView):
    model = Document
    template_name = 'studymaterial/document_list.html'
    context_object_name = 'documents'
    paginate_by = 10

    def get_queryset(self):
        category = self.request.GET.get('category')
        q        = self.request.GET.get('q')

        if category == None:
            category = 'ALL'

        if category != 'ALL' and q:
            return Document.objects.filter( Q(name__icontains=q) | Q(uploaded_by__username__icontains=q) | Q(description__icontains=q), uploaded_at__lte=timezone.now(), category = category).order_by('-uploaded_at')
        elif category != 'ALL':
            return Document.objects.filter(uploaded_at__lte=timezone.now(), category = category).order_by('-uploaded_at')
        elif q:
            return Document.objects.filter( Q(name__icontains=q) | Q(uploaded_by__username__icontains=q) | Q(description__icontains=q), uploaded_at__lte=timezone.now()).order_by('-uploaded_at')
        else:
            return Document.objects.filter(uploaded_at__lte=timezone.now()).order_by('-uploaded_at')

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['categories'] = Document.CATEGORY
        return context
@login_required
def upload_document(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            doc = form.save(commit=False)
            doc.uploaded_by = request.user
            doc.save()
            # Always return an HttpResponseRedirect after successfully dealing
            # with POST data. This prevents data from being posted twice if a
            # user hits the Back button.
            return HttpResponseRedirect(reverse('studymaterial:document_list'))
    else:
        form = DocumentForm()
    return render(request, 'studymaterial/upload_form.html', {'form':form})

@login_required
def found_useful(request, slug):
    document = Document.objects.get(slug=slug)
    useful_object, created = Useful.objects.get_or_create(person=request.user, document=document)
    return redirect('studymaterial:document_list')

@login_required
def unlike(request, slug):
    document = Document.objects.get(slug=slug)
    Useful.objects.filter(document=document, person=request.user).delete()
    return redirect('studymaterial:document_list')