# -*- coding: utf-8 -*-
"""
fl_defs

2023.may  mlabru  referências aos diretórios alterados. Compatibilidade com GORmet
2021.may  mlabru  initial version (Linux/Python)
"""
# < imports >----------------------------------------------------------------------------------

# python library
import logging

# < defines >----------------------------------------------------------------------------------

# logging level
# DI_LOG_LEVEL = logging.WARNING
DI_LOG_LEVEL = logging.DEBUG

# GMT is ahead of us
DI_DIFF_GMT = 3

# radius of earth in kilometers. Use 3956 for miles
DI_RADIUS = 6371

# ft -> m
DF_FT2M = 0.3048

# kt -> m/s
DF_KT2MPS = 0.514444444

# m/s -> kt
DF_MPS2KT = 1.943844492

# maximum visibility (CAVOK)
DI_VIS_CAVOK = 10000

# weahter state messages
DDCT_WEATHER = {"DZ":     "Drizzle.",
                "-DZ":    "Light drizzle.",
                "+DZ":    "Heavy drizzle.",
                "RA":     "Rain.",
                "-RA":    "Light rain.",
                "+RA":    "Heavy rain.",
                "SN":     "Snow.",
                "-SN":    "Light snow.",
                "+SN":    "Heavy snow.",
                "SG":     "Snow grains.",
                "-SG":    "Light snow grains.",
                "+SG":    "Heavy snow grains.",
                "PL":     "Ice pellets.",
                "-PL":    "Light Ice pellets.",
                "+PL":    "Heavy Ice pellets.",
                "GS":     "Samll hail.",
                "-GS":    "Light Samll hail.",
                "+GS":    "Heavy Samll hail.",
                "GR":     "Hail.",
                "-GR":    "Light hail.",
                "+GR":    "Heavy hail.",
                "RASN":   "Rain and snow.",
                "-RASN":  "Light rain and snow.",
                "+RASN":  "Heavy rain and snow.",
                "SNRA":   "Snow and rain.",
                "-SNRA":  "Light snow and rain.",
                "+SNRA":  "Heavy snow and rain.",
                "SHSN":   "Snow showers.",
                "-SHSN":  "Light snow showers.",
                "+SHSN":  "Heavy snow showers.",
                "SHRA":   "Rain showers.",
                "-SHRA":  "Light rain showers.",
                "+SHRA":  "Heavy rain showers.",
                "SHGR":   "Hail showers.",
                "-SHGR":  "Light hail showers.",
                "+SHGR":  "Heavy hail showers.",
                "FZRA":   "Freezing rain.",
                "-FZRA":  "Light freezing rain.",
                "+FZRA":  "Heavy freezing rain.",
                "FZDZ":   "Freezing drizzle.",
                "-FZDZ":  "Light freezing drizzle.",
                "+FZDZ":  "Heavy freezing drizzle.",
                "TSRA":   "Thunderstorm with rain.",
                "-TSRA":  "Light thunderstorm with rain.",
                "+TSRA":  "Heavy thunderstorm with rain.",
                "TSGR":   "Thunderstorm with hail.",
                "-TSGR":  "Light thunderstorm with hail.",
                "+TSGR":  "Heavy thunderstorm with hail.",
                "TSGS:":  "Thunderstorm with small hail.",
                "-TSGS:": "Light thunderstorm with small hail.",
                "+TSGS:": "Heavy thunderstorm with small hail.",
                "TSSN:":  "Thunderstorm with snow.",
                "-TSSN:": "Light thunderstorm with snow.",
                "+TSSN:": "Heavy thunderstorm with snow.",
                "DS:":    "Duststorm.",
                "-DS:":   "Light duststorm.",
                "+DS:":   "Heavy duststorm.",
                "SS:":    "Sandstorm.",
                "-SS:":   "Light sandstorm.",
                "+SS:":   "Heavy sandstorm.",
                "FG:":    "Fog.",
                "FZFG:":  "Freezing fog.",
                "VCFG:":  "Fog in vicinity.",
                "MIFG:":  "Shallow fog.",
                "PRFG:":  "Aerodrome partially covered by fog.",
                "BCFG:":  "Fog patches.",
                "BR:":    "Mist.",
                "HZ:":    "Haze.",
                "FU:":    "Smoke.",
                "DRSN:":  "Low drifting snow.",
                "DRSA:":  "Low drifting sand.",
                "DRDU:":  "Low drifting dust.",
                "DU:":    "Dust.",
                "BLSN:":  "Blowing snow.",
                "BLDU:":  "Blowing dust.",
                "SQ:":    "Squall.",
                "IC:":    "Ice crystals.",
                "TS:":    "Thunderstorm.",
                "VCTS:":  "Thunderstorm in vicinity.",
                "VA:":    "Volcanic ash."}

# < the end >----------------------------------------------------------------------------------


## 1. Introduction ##

import csv

f = open('world_bank_gdp.csv')
gdp = list(csv.reader(f))
gdp = gdp[1:]

ireland = gdp[57][1:]

ireland_int = []

for item in ireland:
    ireland_int.append(int(item))

## 2. Writing List Comprehensions ##

ireland = gdp[57][1:]

# LOOP VERSION
#
# ireland_int = []
# for i in ireland:
#     ireland_int.append(int(i))

ireland_int = [int(i) for i in ireland]

## 3. Common Applications of List Comprehensions ##

jamaica = gdp[62][1:]

jamaica_mil = [int(jam)/1000000 for jam in jamaica]
years = [str(year) for year in range(1970,2018)]
gdp_1970 = [row[1] for row in gdp]

## 4. Nested List Comprehensions ##

def convert(item):
    try:
        value = round(int(item)/1000000)
    except:
        value = item
    return value

gdp = [[convert(i) for i in row] for row in gdp]

## 5. Enumerating Lists ##

china = gdp[22][1:]
germany = gdp[31][1:]
cn_pct_of_de = []

for i, item_1 in enumerate(china):
    item_2 = germany[i]
    cn_pct_of_de.append(item_1/item_2)

## 6. Combining Lists with the Zip Function ##

india = gdp[56][1:]
australia = gdp[4][1:]

ind_pct_of_au = [one/two for one,two in zip(india,australia)]

## 7. The Any and All Functions ##

gdp_clean = [row for row in gdp if all(row)]
print(len(gdp),len(gdp_clean))

## 8. Passing Functions as Arguments ##

def key_func(list_arg):
    return list_arg[1]

gdp_sorted_1970 = sorted(gdp_clean, key = key_func, reverse=True)

top_5_1970 = [row[0] for row in gdp_sorted_1970[:5]]

## 9. Lambda Functions ##

# def multiply(a, b):
#    return a * b

multiply = lambda a, b: a * b

## 10. Using Lambda Functions to Analyze Lists of Lists ##

gdp_sorted_2016 = sorted(gdp_clean, key = lambda row: row[-2], reverse = True)
top_5_2016 = [row[0] for row in gdp_sorted_2016[:5]]

## 11. Dictionary Comprehensions ##

gdp_dict = {i[0]:i[1:] for i in gdp_clean}
bangladesh = gdp_dict["Bangladesh"]

## 12. Challenge: Combining Techniques ##

def n_biggest_increase(start_yr, end_yr, n=5):
    countries = [row[0] for row in gdp]
    start = [row[start_yr - 1969] for row in gdp]
    end = [row[end_yr - 1969] for row in gdp]

    cse = [[country, start[i], end[i]] for i, country in enumerate(countries) if country and start[i] and end[i]]

    increases = [[c, e / s] for c, s, e in cse]

    ordered = sorted(increases, key=lambda row: row[1], reverse=True)

    top_n = ordered[:n]

    return top_n

top_5_1980_to_85 = n_biggest_increase(1980, 1985)
print(top_5_1980_to_85)
top_5_1970s = n_biggest_increase(1970, 1980)

Python Introduction/Python for Data Science Intermediate/Code/Python Tips and Tricks-355.py


"""Functions to build data pipelines for the Kaggle NLP with Disaster Tweets challenge."""

import csv
from typing import Callable, Iterable, Optional
import torch
import torchtext.data
from torchdata.datapipes.map import MapDataPipe, SequenceWrapper
from torchdata.datapipes.iter import IterDataPipe

def load_csv_dataset(filepath: str) -> list[tuple[int, str, Optional[int]]]:
    """Load a dataset file for the Kaggle NLP with Disaster Tweets challenge.
    Result will be a list of tuples, each representing a sample and containing the sample id (int),
    tweet text (str) and label (int or None).
    A 1 (0) label indicates that the tweet is (not) about a disaster event.
    When loading the challenge test set, the labels will be None.
    """
    with open(filepath, mode='r', newline='', encoding="utf8") as file:
        return _parse_csv(file)

def _parse_csv(f: Iterable[str]):
    data = []
    reader = csv.DictReader(f)
    for row in reader:
        # row is a dict with keys id, keyword, location, text, target (not for test dataset)
        text = row["text"]

        label=None
        if "target" in row:
            label = int(row["target"])

        sample_id = int(row["id"])

        data.append((sample_id, text, label))

    return data

def tokenize_dataset(data: list[tuple[int, str, Optional[int]]], tokenize: Callable) -> list[tuple[int, list[int], Optional[int]]]:
    """Tokenize the text of each sample of a dataset.
    Since the dataset for the Kaggle challenge is small, we can just tokenize all
    sentences once at the beginning.
    """
    return [(id, tokenize(text), label) for id, text, label in data]

class BasicTokenizer:
    """BasicTokenizer loads its token dictionary from a file and uses the basic_english tokenizer
    from the torchtext.data package. This will lowercase the text and split it into words.
    Note however that e.g. fastText word embeddings are case-sensitive. There are separate
    embeddings for "and" and "And", so some information will be lost.
    """
    def __init__(self, token_dict_file: str) -> None:
        self._token_dict = torch.load(token_dict_file)
        self._tokenizer = torchtext.data.get_tokenizer("basic_english")

    def tokenize(self, text: str | list[str], unknown_index=1) -> list[int] | list[list[int]]:
        """Split text into tokens and map them to token indices.
        Supports single elements (str) or batches (list[str]).
        """
        if isinstance(text, list):
            return [self._tokenize_single(x, unknown_index=unknown_index) for x in text]
        else:
            return self._tokenize_single(text, unknown_index=unknown_index)

    def _tokenize_single(self, text: str, unknown_index=1) -> list[int]:
        return [self._token_dict.get(y, unknown_index) for y in self._tokenizer(text)]

def build_pipeline(data, transform: Optional[Callable], batch_size: Optional[int], shuffle=True) -> IterDataPipe | MapDataPipe:
    """Returns a pipeline that applies the given transform function to each element/batch.
    If batch_size is None, no batching is performed.
    """
    pipeline = SequenceWrapper(data)
    if shuffle:
        pipeline = pipeline.shuffle()
    if batch_size is not None:
        pipeline = pipeline.batch(batch_size)
    if transform is not None:
        pipeline = pipeline.map(transform)
    return pipeline

def default_transform(samples, seq_len=None, padding_index=0, train=True):
    """Transform the given sample or batch of samples to tensors.
    It is assumed that text is already tokenized. The list of token indices is padded to
    the given length (max length of the batch if seq_len is None).
    If train is true returns tokens and labels, otherwise sample ids and tokens.
    """
    if isinstance(samples, list):
        sample_id, tokens, label = [x[0] for x in samples], [x[1] for x in samples], [x[2] for x in samples]
        if seq_len is None:
            seq_len = max(len(x) for x in tokens)
        tokens = [to_length(x, seq_len, padding_index) for x in tokens]
    else:
        sample_id, tokens, label = samples
        if seq_len is not None:
            tokens = to_length(tokens, seq_len, padding_index)

    if train:
        return torch.tensor(tokens), torch.tensor(label)
    else:
        return sample_id, torch.tensor(tokens)

def to_length(seq: list[int], length: int, padding_index) -> list[int]:
    """Pad or truncate sequence to given length."""
    if len(seq) > length:
        return seq[:length]
    if len(seq) < length:
        seq.extend([padding_index for i in range(length-len(seq))])
    return seq


import os
import shutil
import zipfile

import urllib.parse
import urllib.request

import torch
import torch.utils.data
from dataset import *
import pickle
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import numpy as np
import csv
from collections import Counter, defaultdict
import pandas as pd

def prepare_bash():
    cohort_size = pickle.load(open(r'../ipreprocess/save_cohort_all/cohorts_size.pkl', 'rb'))
    fo = open('run_bash.cmd', 'w')  # 'a'
    for k, v in cohort_size.items():
        if v >= 200:
            name = k.split('.')[0]
            cmd = "python main.py --data_dir ../ipreprocess/save_cohort_all/  --controlled_drug random " \
                  "--pickles_dir  pickles --treated_drug_file {} " \
                  "--save_model_filename  save/save_model_test/{}.pt --random_seed 99 " \
                  "--outputs_lstm log/log_LSTM_test/{}.csv " \
                  "--outputs_lr  log/log_LR_test/{}.csv " \
                  "--save_db  save/save_db_test/{}.db \n".format(name,name,name,name,name) # *((name,)*5)
            fo.write(cmd)

    fo.close()

def prepare_bash_MLP():
    cohort_size = pickle.load(open(r'../ipreprocess/output/save_cohort_all_loose/cohorts_size.pkl', 'rb'))
    fo = open('run_bash_MLP.cmd', 'w')  # 'a'

    name_cnt = sorted(cohort_size.items(), key=lambda x: x[1], reverse=True)
    for x in name_cnt:
        k, v = x
    # for k, v in cohort_size.items():
        if v >= 500:
            name = k.split('.')[0]
            cmd = "python main.py --data_dir ../ipreprocess/output/save_cohort_all_loose/  --controlled_drug random " \
                  "--run_model MLP --treated_drug_file {} " \
                  "--save_model_filename  save/save_model_test/{}.pt " \
                  "--random_seed 1 " \
                  "--drug_coding rxnorm  " \
                  "--med_code_topk 150\n".format(name, name)  # *((name,)*5)
            fo.write(cmd)

    fo.close()

def prepare_bash_matter_arising():
    # --data_dir.. / ipreprocess / save_cohort_all / --controlled_drug random - -pickles_dir pickles  --treated_drug_file
    # 83367 - -save_model_filename
    # save / save_model_test / 83367.
    # pt  --random_seed
    # 99 - -outputs_lstm
    # log / log_LSTM_test / 83367.
    # csv - -outputs_lr
    # log / log_LR_test / 83367.
    # csv - -save_db
    # save / save_db_test / 83367.
    # db
    #
    n = 50
    fo = open('run_bash_83367.cmd', 'w')  # 'a'
    for i in range(n):
        cmd = "python main.py --data_dir ../ipreprocess/save_cohort_all/  --controlled_drug random " \
                  "--pickles_dir  pickles --treated_drug_file 83367 " \
                  "--save_model_filename  save/save_model_test/83367_{}.pt --random_seed {} " \
                  "--outputs_lstm log/log_LSTM_test/83367_{}.csv " \
                  "--outputs_lr  log/log_LR_test/83367_{}.csv " \
                  "--save_db  save/save_db_test/83367_{}.db \n".format(i, i, i, i, i)  # *((name,)*5)
        fo.write(cmd)

    fo.close()

def analyse_cohorts(patients_threshold=200):
    cohort_size = pickle.load(open(r'../ipreprocess/save_cohort_all/cohorts_size.pkl', 'rb'))
    cohorts = []
    for k, v in cohort_size.items():
        if v >= patients_threshold:
            name = k.split('.')[0]
            c = pickle.load(open(r'../ipreprocess/save_cohort_all/' + k, 'rb'))
            cohorts.append(c)

    return cohorts

def combine_results_old():
    df = pd.read_csv(r'../ipreprocess/save_cohort_all/cohort_all_name_size_positive_le200_results.csv')
    df['ATE_original'] = np.nan
    df['ATE_LR'] = np.nan
    df['LR_uf_ratio'] = np.nan
    df['ATE_LSTM'] = np.nan
    df['ATE_LSTM'] = np.nan
    df['LSTM_uf_ratio'] = np.nan

    for index, row in df.iterrows():
        drug = int(row['cohort_name'].split('.')[0])
        print(drug, flush=True)
        lstm = pd.read_csv(r'log/log_LSTM_test/{}.csv'.format(drug), index_col='drug')
        ATE_original = lstm.loc[drug, 'ATE_original']
        ATE_LSTM = lstm.loc[drug, 'ATE_weighted']

        lr = pd.read_csv(r'log/log_LR_test/{}.csv'.format(drug), index_col='drug')
        ATE_LR = lr.loc[drug, 'ATE_weighted']
        df.loc[index, 'ATE_original'] = ATE_original
        df.loc[index, 'ATE_LSTM'] = ATE_LSTM
        df.loc[index, 'ATE_LR'] = ATE_LR

        df.loc[index, 'LSTM_uf_ratio'] = lstm.loc[drug, 'n_unbalanced_feature_w']/lstm.loc[drug, 'n_feature']
        df.loc[index, 'LR_uf_ratio'] = lr.loc[drug,'n_unbalanced_feature_w'] / lr.loc[drug,'n_feature']

    df.to_csv(r'../ipreprocess/save_cohort_all/cohort_all_name_size_positive_le200_results_combined_withunbalanceratio.csv')
    print('Done')

def combine_results():
    # cohort_all_name_size_positive_le200_results.csv
    df = pd.read_csv(r'../ipreprocess/save_cohort_all/cohort_all_name_size_positive.csv')
    add_col = [
        'n_user','n_nonuser','max_unbalanced_original','max_unbalanced_weighted',
        'n_unbalanced_feature', 'n_unbalanced_feature_w','n_feature', 'UncorrectedEstimator_EY1_val',
        'UncorrectedEstimator_EY0_val', 'ATE_original','IPWEstimator_EY1_val','IPWEstimator_EY0_val','ATE_weighted']
    lr_col = ['max_unbalanced_original', 'max_unbalanced_weighted', 'n_unbalanced_feature', 'n_unbalanced_feature_w',
              'n_feature', 'IPWEstimator_EY1_val', 'IPWEstimator_EY0_val', 'ATE_weighted']
    for c in add_col:
        df[c] = np.nan
    for c in lr_col:
        df['LR_'+ c] = np.nan
    df['LSTM_UnbalFeat_ratio'] = np.nan
    df['LR_UnbalFeat_ratio'] = np.nan

    for index, row in df.iterrows():
        drug = int(row['cohort_name'].split('.')[0])
        print(drug, flush=True)
        if os.path.exists(r'log/log_LSTM_test/{}.csv'.format(drug)):
            lstm = pd.read_csv(r'log/log_LSTM_test/{}.csv'.format(drug), index_col='drug')
            for c in add_col:
                df.loc[index, c] = lstm.loc[drug, c]
            lr = pd.read_csv(r'log/log_LR_test/{}.csv'.format(drug), index_col='drug')
            for c in lr_col:
                df.loc[index, 'LR_'+c] = lr.loc[drug, c]

            df.loc[index, 'LSTM_UnbalFeat_ratio'] = lstm.loc[drug, 'n_unbalanced_feature_w']/lstm.loc[drug, 'n_feature']
            df.loc[index, 'LR_UnbalFeat_ratio'] = lr.loc[drug, 'n_unbalanced_feature_w'] / lr.loc[drug, 'n_feature']

    df.to_csv(r'../ipreprocess/save_cohort_all/cohort_all_name_size_positive_with_ALL_ATE_RESUTLS.csv')
    print('Done')

def analyse_MLP_results():
    cohort_size = pickle.load(open(r'../ipreprocess/save_cohort_all_mediate/cohorts_size.pkl', 'rb'))
    name_cnt = sorted(cohort_size.items(), key=lambda x:x[1], reverse=True)
    train = val = test = all = None
    for x in name_cnt:
        k, v = x
        if v >= 100:
            name = k.split('.')[0]
            # c = pickle.load(open(r'../ipreprocess/save_cohort_all/' + k, 'rb'))
            # cohorts.append(c)
            df = pd.read_csv('save/save_model_test_mediate/{}.pt_results_MLP_all.csv'.format(name), index_col=0)

            if train is not None:
                train = train.append(df.loc['train',:])
            else:
                train = df.loc[['train'],:]

            if val is not None:
                val = val.append(df.loc[['val'],:])
            else:
                val = df.loc[['val'],:]

            if test is not None:
                test = test.append(df.loc[['test'],:])
            else:
                test = df.loc[['test'],:]

            if all is not None:
                all = all.append(df.loc[['all'],:])
            else:
                all = df.loc[['all'],:]

    with pd.ExcelWriter('save/save_model_test_mediate/MLP_all_results.xlsx') as f:
        train.to_excel(f, sheet_name='train')
        val.to_excel(f, sheet_name='val')
        test.to_excel(f, sheet_name='test')
        all.to_excel(f, sheet_name='all')
    # train.to_csv('save/save_model_test/MLP_train_results.csv')
    # val.to_csv('save/save_model_test/MLP_val_results.csv')
    # test.to_csv('save/save_model_test/MLP_test_results.csv')
    # all.to_csv('save/save_model_test/MLP_all_results.csv')

if __name__ == '__main__':
#     # prepare_bash()
#     # cohorts = analyse_cohorts(patients_threshold=200)
#     # combine_results()
#     # prepare_bash_matter_arising()
#     # prepare_bash_MLP()
#     # analyse_MLP_results()
#     # rxnorm_name, df = load_latest_rxnorm_info()
#     # with open(r'pickles/rxnorm_label_mapping.pkl', 'rb') as f:
#     #     rxnorm_name2 = pickle.load(f)
#
#     # build atc to rxnorm mapping!
#     # df, ar = map_rxnorm_and_atc()
#     load_rxnorm_to_atc()
    print()


import gensim
from gensim.models.doc2vec import LabeledSentence
from gensim.models import Doc2Vec
import re
from nltk.tokenize import RegexpTokenizer
from stop_words import get_stop_words
from nltk.stem.porter import PorterStemmer
import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import seaborn
import time

start_time = time.time()
count = 0
lablist = []
for i in range(len(abstract_tot)): #abstract_tot is the list of all the scraped abstracts
    lablist.append(count)
    count+=1
tokenizer  = RegexpTokenizer(r'\w+')
en_stop = get_stop_words('en')
p_stemmer = PorterStemmer()

texts = []
taggeddoc = []
for index, i in enumerate(abstract_tot):
    raw = i.lower()
    tokens = tokenizer.tokenize(raw)
    stopped_tokens = [j for j in tokens if not j in en_stop]
    number_tokens = [re.sub(r'[\d]', ' ', k) for k in stopped_tokens]
    number_tokens = ' '.join(number_tokens).split()
    stemmed_tokens = [p_stemmer.stem(p) for p in number_tokens]
    texts.append(number_tokens)
    #td = TaggedDocument(gensim.utils.to_unicode(str.encode(' '.join(stemmed_tokens))).split(),str(index))
    #taggeddoc.append(td)

LabeledSentence = gensim.models.doc2vec.LabeledSentence
class LabeledLineSentence(object):
    def __init__(self, doc_list, labels_list):
       self.labels_list = labels_list
       self.doc_list = doc_list
    def __iter__(self):
        for idx, doc in enumerate(self.doc_list):
            #yield LabeledSentence(words=doc.split(),tags=[self.labels_list[idx]])
            yield LabeledSentence(words=doc,tags=[self.labels_list[idx]])

#it = LabeledLineSentence(claimcalc_tot, lablist)
it = LabeledLineSentence(texts, lablist)

#model = gensim.models.Doc2Vec(size=300, window=10, min_count=5, workers=11,alpha=0.025, min_alpha=0.025) # use fixed learning rate
model = gensim.models.Doc2Vec(dm = 1, size=100, min_count=10, workers=11,alpha=0.025, min_alpha=0.025) # use fixed learning rate
model.build_vocab(it)
for epoch in range(10):
    print("epoch is: " + str(epoch))
    model.train(it)
    model.alpha -= 0.002 # decrease the learning rate
    model.min_alpha = model.alpha # fix the learning rate
    model.train(it)

end_time = time.time()
print("time taken is" + str(end_time-start_time))

model.save('trained.model')
model.save_word2vec_format('trained.word2vec')

#plot word2vec example
w2v = gensim.models.Doc2Vec.load_word2vec_format('trained.word2vec')
words_np = []
words_label = []
for word in w2v.vocab.keys():
    words_np.append(w2v[word])
    words_label.append(word)
print('Added %s words. Shape %s'%(len(words_np),np.shape(words_np)))

pca = PCA(n_components=2)
pca.fit(words_np)
reduced= pca.transform(words_np)
plt.figure(figsize=(10, 10))

for index,vec in enumerate(reduced):
        # print ('%s %s'%(words_label[index],vec))
        if index <100:
            x,y=vec[0],vec[1]
            plt.scatter(x,y)
            plt.annotate(words_label[index],xy=(x,y), fontsize=14)
plt.axis([0,.2,-.5,.5])
plt.show()


import time
from functools import wraps

def time_this(func):
    @wraps(func)  # 保留原始函数的元数据
    def wrapper(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs)
        use_time = time.time() - start
        print('[%s] use time:%s' % (func.__name__, use_time))
        return result

    return wrapper

@time_this
def sum_(n):
    total = 0
    for i in range(n):
        total += i
    return total

sum_(100000000)
print(sum_.__name__)


import argparse
from datetime import datetime as dt
import json
from typing import Dict

"""
Potential additions:
    Profile specific clients
    Check for burst traffic (time-series analysis)
    Check for number of different paths requested (may be able to detect scanning activity)
"""

def parse_timestamp(ts_string: str, ts_format: str):
    """
    Parse a given string with the given datetime format and return the corresponding Unix epoch timestamp.
    :param ts_string: string containing a datetime object of some kind
    :param ts_format: format string presumably matching the format of ts_string
    :return: number of seconds since epoch
    """
    return dt.strptime(ts_string, ts_format).timestamp()

def profile_apache_logs(input_file: str) -> Dict:
    """
    Create a log profile from a parsed Apache access log file.
    :param input_file: Apache access log file (JSON format)
    :return: dictionary with profiled statistics
    """
    apache_ts_format = '%d/%b/%Y:%H:%M:%S %z'
    with open(input_file, 'r') as f:
        logs = json.load(f)
    baseline = {'requests': {'all': {'total': 0}}, 'methods_breakdown': {}}
    first_datetime = None
    last_datetime = None
    for log in logs:
        if not first_datetime:
            first_datetime = log['datetime']
        baseline['requests']['all']['total'] += 1
        method = log['request_method'].lower()
        if method not in baseline['methods_breakdown']:
            baseline['methods_breakdown'][method] = 0
        baseline['methods_breakdown'][method] += 1
        last_datetime = log['datetime']

    earliest_timestamp = parse_timestamp(first_datetime, apache_ts_format)
    latest_timestamp = parse_timestamp(last_datetime, apache_ts_format)
    baseline['first_timestamp'] = earliest_timestamp
    baseline['last_timestamp'] = latest_timestamp
    baseline['seconds_elapsed'] = latest_timestamp - earliest_timestamp
    baseline['minutes_elapsed'] = baseline['seconds_elapsed'] / 60
    baseline['requests']['all']['avg_per_sec'] = baseline['requests']['all']['total'] / baseline['seconds_elapsed']
    baseline['requests']['all']['avg_per_min'] = baseline['requests']['all']['total'] / baseline['minutes_elapsed']

    for k, v in baseline['methods_breakdown'].items():
        baseline['requests'][k] = {'total': v}
        baseline['requests'][k]['avg_per_sec'] = v / baseline['seconds_elapsed']
        baseline['requests'][k]['avg_per_min'] = v / baseline['minutes_elapsed']

    del baseline['methods_breakdown']
    return baseline

def main():
    parser = argparse.ArgumentParser(
        description='This application calculates a log profile from a provided parsed log file.')
    parser.add_argument('-i', '--input', required=True, help='Parsed log file (JSON format) to read from')
    parser.add_argument('-l', '--log-format', required=True, choices=['apache'], help='Type of log to parse')
    parser.add_argument('-o', '--output', help='Output file to write to')
    args = parser.parse_args()

    input_file = args.input
    log_format = args.log_format
    output = args.output

    profile_function = globals()['profile_{}_logs'.format(log_format)]
    profile = profile_function(input_file)

    if output:
        with open(output, 'w') as of:
            json.dump(profile, of, indent=2)
    else:
        print(json.dumps(profile, indent=2))

if __name__ == '__main__':
    main()


'''
Cracking the Coding Interview
Problem Number 1.8
Problem Name: Zero Matrix
Problem Description: Write an algorithm such that if an element in an MxN matrix is 0, its entire row and
column are set to O.
'''
m,n=map(int,input().split())
mat=[]
zero=[]
for i in range(m):
    l=list(map(int,input().split()))
    mat.append(l.copy())
for i in range(m):
    for j in range(n):
        if mat[i][j]==0:
            zero.append([i,j])
for k in zero:
    r,c=k[0],k[1]
    for i in range(n):
        mat[r][i]=0
    for i in range(m):
        mat[i][c]=0
for i in mat:
    for j in i:
        print(j,end=' ')
    print()


"""
Linear time sorting
"""

def countingSort(A: list) -> list:
    min_A, max_A = A[0], A[0]
    for a in A:
        if a>max_A:
            max_A = a
        if a<min_A:
            min_A = a
    H = {k: 0 for k in range(min_A, max_A+1)}
    for a in A:
        H[a]+=1
    for k in range(min_A+1, max_A+1):
        H[k]+=H[k-1]
    B = [None for _ in range(len(A))]
    for a in A:
        B[H[a]-1] = a
        H[a]-=1
    return B

print(countingSort([1, 4, 1, 2, 7, 5, 2]))


"""Load a layout in Blender."""

import json
from pathlib import Path
from pprint import pformat
from typing import Dict, Optional

import bpy

from openpype.pipeline import (
    discover_loader_plugins,
    remove_container,
    load_container,
    get_representation_path,
    loaders_from_representation,
    AVALON_CONTAINER_ID,
)
from openpype.hosts.blender.api.pipeline import (
    AVALON_INSTANCES,
    AVALON_CONTAINERS,
    AVALON_PROPERTY,
)
from openpype.hosts.blender.api import plugin

class JsonLayoutLoader(plugin.AssetLoader):
    """Load layout published from Unreal."""

    families = ["layout"]
    representations = ["json"]

    label = "Load Layout"
    icon = "code-fork"
    color = "orange"

    animation_creator_name = "CreateAnimation"

    def _remove(self, asset_group):
        objects = list(asset_group.children)

        for obj in objects:
            remove_container(obj.get(AVALON_PROPERTY))

    def _remove_animation_instances(self, asset_group):
        instances = bpy.data.collections.get(AVALON_INSTANCES)
        if instances:
            for obj in list(asset_group.children):
                anim_collection = instances.children.get(
                    obj.name + "_animation")
                if anim_collection:
                    bpy.data.collections.remove(anim_collection)

    def _get_loader(self, loaders, family):
        name = ""
        if family == 'rig':
            name = "BlendRigLoader"
        elif family == 'model':
            name = "BlendModelLoader"

        if name == "":
            return None

        for loader in loaders:
            if loader.__name__ == name:
                return loader

        return None

    def _process(self, libpath, asset, asset_group, actions):
        plugin.deselect_all()

        with open(libpath, "r") as fp:
            data = json.load(fp)

        all_loaders = discover_loader_plugins()

        for element in data:
            reference = element.get('reference')
            family = element.get('family')

            loaders = loaders_from_representation(all_loaders, reference)
            loader = self._get_loader(loaders, family)

            if not loader:
                continue

            instance_name = element.get('instance_name')

            action = None

            if actions:
                action = actions.get(instance_name, None)

            options = {
                'parent': asset_group,
                'transform': element.get('transform'),
                'action': action,
                'create_animation': True if family == 'rig' else False,
                'animation_asset': asset
            }

            if element.get('animation'):
                options['animation_file'] = str(Path(libpath).with_suffix(
                    '')) + "." + element.get('animation')

            # This should return the loaded asset, but the load call will be
            # added to the queue to run in the Blender main thread, so
            # at this time it will not return anything. The assets will be
            # loaded in the next Blender cycle, so we use the options to
            # set the transform, parent and assign the action, if there is one.
            load_container(
                loader,
                reference,
                namespace=instance_name,
                options=options
            )

        # Camera creation when loading a layout is not necessary for now,
        # but the code is worth keeping in case we need it in the future.
        # # Create the camera asset and the camera instance
        # creator_plugin = get_legacy_creator_by_name("CreateCamera")
        # if not creator_plugin:
        #     raise ValueError("Creator plugin \"CreateCamera\" was "
        #                      "not found.")

        # legacy_create(
        #     creator_plugin,
        #     name="camera",
        #     # name=f"{unique_number}_{subset}_animation",
        #     asset=asset,
        #     options={"useSelection": False}
        #     # data={"dependencies": str(context["representation"]["_id"])}
        # )

    def process_asset(self,
                      context: dict,
                      name: str,
                      namespace: Optional[str] = None,
                      options: Optional[Dict] = None):
        """
        Arguments:
            name: Use pre-defined name
            namespace: Use pre-defined namespace
            context: Full parenthood of representation to load
            options: Additional settings dictionary
        """
        libpath = self.filepath_from_context(context)
        asset = context["asset"]["name"]
        subset = context["subset"]["name"]

        asset_name = plugin.asset_name(asset, subset)
        unique_number = plugin.get_unique_number(asset, subset)
        group_name = plugin.asset_name(asset, subset, unique_number)
        namespace = namespace or f"{asset}_{unique_number}"

        avalon_container = bpy.data.collections.get(AVALON_CONTAINERS)
        if not avalon_container:
            avalon_container = bpy.data.collections.new(name=AVALON_CONTAINERS)
            bpy.context.scene.collection.children.link(avalon_container)

        asset_group = bpy.data.objects.new(group_name, object_data=None)
        asset_group.empty_display_type = 'SINGLE_ARROW'
        avalon_container.objects.link(asset_group)

        self._process(libpath, asset, asset_group, None)

        bpy.context.scene.collection.objects.link(asset_group)

        asset_group[AVALON_PROPERTY] = {
            "schema": "openpype:container-2.0",
            "id": AVALON_CONTAINER_ID,
            "name": name,
            "namespace": namespace or '',
            "loader": str(self.__class__.__name__),
            "representation": str(context["representation"]["_id"]),
            "libpath": libpath,
            "asset_name": asset_name,
            "parent": str(context["representation"]["parent"]),
            "family": context["representation"]["context"]["family"],
            "objectName": group_name
        }

        self[:] = asset_group.children
        return asset_group.children

    def exec_update(self, container: Dict, representation: Dict):
        """Update the loaded asset.

        This will remove all objects of the current collection, load the new
        ones and add them to the collection.
        If the objects of the collection are used in another collection they
        will not be removed, only unlinked. Normally this should not be the
        case though.
        """
        object_name = container["objectName"]
        asset_group = bpy.data.objects.get(object_name)
        libpath = Path(get_representation_path(representation))
        extension = libpath.suffix.lower()

        self.log.info(
            "Container: %s\nRepresentation: %s",
            pformat(container, indent=2),
            pformat(representation, indent=2),
        )

        assert asset_group, (
            f"The asset is not loaded: {container['objectName']}"
        )
        assert libpath, (
            "No existing library file found for {container['objectName']}"
        )
        assert libpath.is_file(), (
            f"The file doesn't exist: {libpath}"
        )
        assert extension in plugin.VALID_EXTENSIONS, (
            f"Unsupported file: {libpath}"
        )

        metadata = asset_group.get(AVALON_PROPERTY)
        group_libpath = metadata["libpath"]

        normalized_group_libpath = (
            str(Path(bpy.path.abspath(group_libpath)).resolve())
        )
        normalized_libpath = (
            str(Path(bpy.path.abspath(str(libpath))).resolve())
        )
        self.log.debug(
            "normalized_group_libpath:\n  %s\nnormalized_libpath:\n  %s",
            normalized_group_libpath,
            normalized_libpath,
        )
        if normalized_group_libpath == normalized_libpath:
            self.log.info("Library already loaded, not updating...")
            return

        actions = {}

        for obj in asset_group.children:
            obj_meta = obj.get(AVALON_PROPERTY)
            if obj_meta.get('family') == 'rig':
                rig = None
                for child in obj.children:
                    if child.type == 'ARMATURE':
                        rig = child
                        break
                if not rig:
                    raise Exception("No armature in the rig asset group.")
                if rig.animation_data and rig.animation_data.action:
                    namespace = obj_meta.get('namespace')
                    actions[namespace] = rig.animation_data.action

        mat = asset_group.matrix_basis.copy()

        self._remove_animation_instances(asset_group)

        self._remove(asset_group)

        self._process(str(libpath), asset_group, actions)

        asset_group.matrix_basis = mat

        metadata["libpath"] = str(libpath)
        metadata["representation"] = str(representation["_id"])

    def exec_remove(self, container: Dict) -> bool:
        """Remove an existing container from a Blender scene.

        Arguments:
            container (openpype:container-1.0): Container to remove,
                from `host.ls()`.

        Returns:
            bool: Whether the container was deleted.
        """
        object_name = container["objectName"]
        asset_group = bpy.data.objects.get(object_name)

        if not asset_group:
            return False

        self._remove_animation_instances(asset_group)

        self._remove(asset_group)

        bpy.data.objects.remove(asset_group)

        return True


import os
from django.urls.conf import path
from files.models import FilesAdmin
from django.shortcuts import redirect, render
from django.conf import settings
from django.http import Http404,HttpResponse, response
from .form import FilesAdminForm

# Create your views here.

def home(request):
    context={'file' : FilesAdmin.objects.all()}
    return render(request,'files/index.html',context)

def uploadfile(request):
    forms = FilesAdminForm()
    if request.method == 'POST':
        forms = FilesAdminForm(request.POST,request.FILES)
        if forms.is_valid():
            forms.save()
            context = {'Upload':"Success"}
            return render(request,'files/upload.html',context)
        else:
            form = FilesAdminForm()
    return render(request,'files/upload.html',context = {'form':forms})

def download(request,path):
    file_path=os.path.join(settings.MEDIA_ROOT,path)
    if os.path.exists(file_path):
        with open(file_path,'rb')as fh:
            response = HttpResponse(fh.read(),content_type="application/adminupload")
            response['Content-Disposition']='inline;filename='+os.path.basename(file_path)
            return response

        raise Http404


import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules

df = pd.read_csv("./bank-data.csv")
del df["id"]
df["income"] = pd.cut(df["income"],10)
dataset = []
for index, row in df.iterrows():
    row = [col+"="+str(row[col]) for col in list(df)]
    dataset.append(row)

te = TransactionEncoder()
te_ary = te.fit(dataset).transform(dataset)
df = pd.DataFrame(te_ary, columns=te.columns_)
frequent_itemsets = apriori(df, min_support=0.3, use_colnames=True)
print("Frequent Itemset:\n")
print(frequent_itemsets)

rules_confidence = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.7)
print("\nConfidence 0.7:\n")
print(rules_confidence)

rules_support = association_rules(frequent_itemsets, metric="support", min_threshold=0.4)
print("\nSupport 0.4:\n")
print(rules_support)

rules_lift = association_rules(frequent_itemsets, metric="lift", min_threshold=1)
print("\nLift 1:\n")
print(rules_lift)


import win32gui
import os
import time
global current_time

current_time = 0

def winEnumHandler( hwnd, ctx ):
    global current_time
    if win32gui.IsWindowVisible( hwnd ) or True:
        #print ( hex( hwnd ), win32gui.GetWindowText( hwnd ) )
        title = win32gui.GetWindowText( hwnd )
        if "Express Scribe" in title:
            current_time = title.split(" ")[0]
            hours = int(float(current_time.split(":")[0]))
            mins = int(float(current_time.split(":")[1]))
            secs = int(float(current_time.split(":")[2]))
            tot_secs = hours * 60 * 60 + mins * 60 + secs
            current_time = tot_secs

win32gui.EnumWindows( winEnumHandler, None )

if not os.path.isfile('starttime.txt'):
    file1 = open('starttime.txt','w')
    file1.write(str(current_time) + "\n")
    file1.close()
file1 = open('starttime.txt', 'r')
lines = file1.readlines()
file1.close()
start_time = lines[0].replace("\n","")
file2 = open('time_spent.txt', 'r')
lines = file2.readlines()
file2.close()
time_spent = int(float(lines[0].replace("\n","")))
init_time_spent = time_spent
start_audio_time = int(float(start_time))
start_time = time.time()
last_updated = time.time()
while True:
    if time.time() - last_updated >= 1.5:
        # update

        last_updated = time.time()
        time_spent = init_time_spent + (time.time() - start_time)
        time_spent = int(time_spent)
        file1 = open('time_spent.txt', 'w')
        file1.write(str(time_spent) + "\n")
        file1.close()
        last_current_time = current_time
        win32gui.EnumWindows(winEnumHandler, None)
        audio_done_secs = current_time - start_audio_time
        time_spent_hours = int(time_spent / 60 / 60)
        time_spent_mins = int(time_spent / 60) - time_spent_hours * 60 * 60
        time_spent_secs = time_spent - time_spent_mins * 60
        audio_done_mins = int(audio_done_secs / 60)
        audio_done_secs_show = audio_done_secs - int(audio_done_secs / 60) * 60
        amount_earned = 0.50 * (audio_done_secs / 60)
        pay_rate_per_sec = amount_earned / time_spent
        pay_rate = pay_rate_per_sec * 60 * 60
        pay_rate = round(pay_rate,2)

        # estimate time remaining
        audio_secs_per_sec_spent = audio_done_secs / time_spent
        audio_mins_per_min_spent = (audio_done_secs * 60) / (time_spent * 60)
        file_length_mins = 87
        file_remaining_mins = file_length_mins - (current_time / 60)
        time_remaining_mins = 60
        if audio_mins_per_min_spent != 0: time_remaining_mins = file_remaining_mins / audio_mins_per_min_spent
        time_remaining_hrs_show = int(time_remaining_mins / 60)
        time_remaining_mins_show = int(time_remaining_mins) - time_remaining_hrs_show * 60
        time_remaining_secs = (time_remaining_mins * 60)
        time_remaining_secs_show = time_remaining_secs - int(time_remaining_mins) * 60
        time_remaining_secs_show = int(float(time_remaining_secs_show))
        time_remaining_mins_show = int(float(time_remaining_mins_show))
        percentage = current_time * 100 / (file_length_mins * 60)
        percentage = round(percentage, 1)
        os.system("cls")
        print("Time spent: " + str(time_spent_hours) + " hrs " + str(time_spent_mins) + " mins " + str(time_spent_secs) + " secs")
        print("Audio done: " + str(audio_done_mins) + " mins " + str(audio_done_secs_show) + " secs")
        print("Time remaining: " + str(time_remaining_hrs_show) + " hrs " + str(time_remaining_mins_show) + " mins " + str(time_remaining_secs_show) + " secs (" + str(percentage) + "%)")
        print("Pay rate: £" + str(pay_rate) + "/hour")


'''
Linear complexity    O(n)

example:
    finding the index of an element in a non sorted list

'''

nums = [76, 34, 93, 25, 14, 99, 38]

def show(lst, element):
    for i in lst:
        if i == element:
            return lst.index(i)

print(show(nums, 25))


#encoding: utf-8

import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

from random import randint

def gdata(fname_randall,trainf,devf,testf):
	word=set()
	td=[]
	seld=[]
	total=0
	with open(fname_randall) as frd:
		for line in frd:
			tmp=line.strip()
			if tmp:
				tmp=tmp.decode("utf-8")
				wl=tmp.split("  ")
				at=False
				for wu in wl:
					if not wu in word:
						if not at:
							at=True
						word.add(wu)
				if at:
					td.append(tmp)
				else:
					seld.append(tmp)
				total+=1
	ts=int(total*0.1)
	ds=int(total*0.1)
	tl=[]
	while len(tl)<ts:
		ind=randint(0,len(seld)-1)
		tl.append(seld[ind])
		del seld[ind]
	dd=[]
	while len(dd)<ds:
		ind=randint(0,len(seld)-1)
		dd.append(seld[ind])
		del seld[ind]
	td.extend(seld)
	wrtl(trainf,td)
	wrtl(devf,dd)
	wrtl(testf,tl)

def wrtl(fname,lwt):
	with open(fname,"w") as fwrt:
		fwrt.write("\n".join(lwt))

def d2l(dicin):
	rs=[]
	for k,v in dicin.iteritems():
		rs.append(k+"	"+str(v))
	return rs

def main():
	gdata("full.txt","train.txt","dev.txt","test.txt")

if __name__=="__main__":
	main()


from django import forms
from django.forms import SelectDateWidget
from .models import PotentialStu, CounsellingInfo

class PotentialForm(forms.ModelForm):

    class Meta:
        model = PotentialStu
        fields = ('name', 'agent', 'dob', 'gender', 'phone', 'email', 'parent_phone',
                  'parent_email', 'city', 'state', 'school', 'grade', 'gpa', 'toefl')
        widgets = {
            'dob': SelectDateWidget(years=range(1990, 2016)),
        }

class CounsellingForm(forms.ModelForm):

    class Meta:
        model = CounsellingInfo
        fields = ('semester', 'apply_grade', 'school1', 'school2', 'school3',
                  'note', 'counsellor', 'date', 'status', 'fee', 'pay_date',)
        widgets = {
            'date': SelectDateWidget(years=range(2013, 2020)),
            'pay_date': SelectDateWidget(years=range(2013, 2020)),
        }

class ConfirmForm(forms.Form):
    CONFIRM_CHOICES = (
        ('yes', 'Yes'),
        ('no', 'No'),
    )

    confirm = forms.ChoiceField(choices=CONFIRM_CHOICES, widget=forms.RadioSelect())


from project_euler import utils

def concatenated_products(base):
    concatenated_product = ''
    n = 1

    while len(concatenated_product) < 10:
        concatenated_product += str(n * base)
        n += 1
        yield int(concatenated_product)

def brute_force_solution():
    """
    Compute the concatenated product for every number up to some limit and find the large product
    which is also pandigital.

    Because n > 1 and the max 1 to 9 pandigital number can only be 9 digits, the upper limit on
    the search must be a 4 digit number. A five digit number concatenated with the product of
    itself and 2 cannot produce a result that is only 9 digits.
    """
    return max(
        product
        for i in range(1, 10000)
        for product in concatenated_products(i)
        if utils.is_pandigital(product)
    )

SOLUTIONS = [
    brute_force_solution,
]


from PIL import Image
import numpy as np
import json
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
import matplotlib.pyplot as plt
import argparse

def load_checkpoint(filename):

    parser=argparse.ArgumentParser()

    parser.add_argument('-i','--image_dir', help='Path to image', default='flowers/train/1/image_06734.jpg')
    parser.add_argument('-c','--check_dir', help='Directory to checkpoint', default="")
    parser.add_argument('-t','--top_k', help='Choose number of top K', type=int, default='5')
    parser.add_argument('-f','--category_name', help='The mapping full file name for converting categories to real      names', default='cat_to_name.json')
    parser.add_argument('-g','--gpu', help='Use GPU for training gpu / cpu', default='gpu')

    args=parser.parse_args()
    print(args)

    if args.gpu == 'gpu':
        device = 'cuda'
    else:
        device = 'cpu'
    device = torch.device(device)
    checkpoint=torch.load(args.check_dir+filename)

    model=checkpoint['model']
    model.to(device)
    model.load_state_dict (checkpoint['state_dict'])
    optimizer= checkpoint['optimizer']
    epochs=checkpoint['epochs']
    model.to('cpu')

    return model, epochs, optimizer, args

model, epochs, optimizer, args=load_checkpoint('checkpoints.pth')

with open(args.category_name, 'r') as f:
    cat_to_name = json.load(f)

def process_image(image):

    image = Image.open(image)
    image= image.resize((224, 224))
    #box=(224, 224, 224, 224)
    #image= image.crop(box)

    image = np.array(image)

    image = np.divide(image,255)
    mean= np.array([0.485, 0.456, 0.406])
    std= np.array([0.229, 0.224, 0.225])

    image= (image - mean)/ std

    image = image.transpose((2 ,0 ,1))
    image= torch.from_numpy(image)
    model.to('cpu')

    return image

def predict(image_path, model, topk):
    model.eval()
    #model.float()
    # TODO: Implement the code to predict the class from an image file
    image = process_image(image_path)

    image = image.type(torch.FloatTensor)
    image=torch.unsqueeze(image,dim=0)

    #print(image.shape)
    logps = model.forward(image)
    ps= torch.exp(logps)
    probs, classes = ps.topk(topk, dim=1)

    return probs, classes

file_path = args.image_dir

probs, classes = predict(file_path, model, args.top_k)

print(probs)
print(classes)

probs_np=probs.detach().numpy()
max_index = np.argmax(probs_np)
max_probability = probs[max_index]
label = classes[0][max_index]
label_np=label.numpy()
classes_np=classes.numpy()

fig = plt.figure(figsize=(6,6))
ax1 = plt.subplot2grid((15,9), (0,0), colspan=9, rowspan=9)
ax2 = plt.subplot2grid((15,9), (9,2), colspan=5, rowspan=5)
#print (z)
image = Image.open(file_path)
ax1.axis('off')
ax1.set_title(cat_to_name[str(label_np+1)])
ax1.imshow(image)

labels = []
for i in classes_np[0]:
    labels.append(cat_to_name[str(i+1)])

y_pos = np.arange(args.top_k)
ax2.set_yticks(y_pos)
ax2.set_yticklabels(labels)
ax2.set_xlabel('Probability')
ax2.invert_yaxis()
ax2.barh(y_pos, probs_np[0], xerr=0, align='center', color='blue')

plt.show()


from django.contrib import messages
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required

from .forms import ProfileForm

@login_required
def view(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST, instance=request.user.profile)

        if form.is_valid():
            form.save()

            messages.success(request, "Your settings were saved successfully.")

            return redirect('profile:view')

    else:
        form = ProfileForm(instance=request.user.profile)

    return render(request, 'profile/view.html', {
        'form': form,
    })


"""Constants used in tests."""

FIRST_TRAINING = {
    "id": 1,
    "trainer_id": "Ju6JXm1S8rVQfyC18mqL418JdgE2",
    "title": "The first training.",
    "description": "This is the first training.",
    "type": "Cardio",
    "difficulty": "Easy",
    "rating": 4,
    "blocked": False,
    "exercises": [
        {
            "name": "Walk",
            "unit": "metre",
            "type": "Cardio",
            "count": 30,
            "series": 1,
        },
        {
            "name": "Run",
            "unit": "metre",
            "type": "Cardio",
            "count": 30,
            "series": 1,
        },
        {
            "name": "Jumping jacks",
            "type": "Cardio",
            "count": 15,
            "series": 3,
        },
    ]
}

TOMATO_TRAINING = {
    "id": 2,
    "trainer_id": "tomato",
    "title": "The tomato training.",
    "description": "This is the tomato training.",
    "type": "Leg",
    "difficulty": "Medium",
    "rating": 0,
    "blocked": False,
    "exercises": [
        {
            "name": "Squat",
            "type": "Leg",
            "count": 15,
            "series": 3,
        },
    ]
}

TO_BLOCK_TRAINING = {
    "id": 3,
    "trainer_id": "naughty_trainer",
    "title": "The training to be blocked.",
    "description": "This is going to be blocked and unblocked.",
    "type": "Arm",
    "difficulty": "Hard",
    "rating": 0,
    "blocked": False,
    "exercises": [
        {
            "name": "Hammer curl",
            "type": "Arm",
            "count": 15,
            "series": 3,
        },
    ]
}

TO_EDIT_TRAINING = {
    "id": 4,
    "trainer_id": "indecisive_trainer",
    "title": "This training will be modified, trainer is indecisive.",
    "description": "This is going to change.",
    "type": "Arm",
    "difficulty": "Hard",
    "rating": 0,
    "blocked": False,
    "exercises": [
        {
            "name": "Hammer curl",
            "type": "Arm",
            "count": 15,
            "series": 3,
        },
    ]
}

EMPTY_RESPONSE_WITH_PAGINATION = {
    "items": [],
    "offset": 0,
    "limit": 10,
    "count": 0,
}

TRAINING_TO_BE_CREATED = {
    "trainer_id": "Ju6JXm1S8rVQf7C18mqL418JdgE2",
    "title": "A training created with POST.",
    "description": "A training created with POST.",
    "type": "Arm",
    "difficulty": "Medium",
    "exercises": [
        {
            "name": "Hammer curl",
            "type": "Arm",
            "count": 15,
            "series": 3
        },
        {
            "name": "Arnold press",
            "type": "Arm",
            "count": 10,
            "series": 4
        }
    ]
}

EXPECTED_TRAINING_TYPES = {
    "items": [
        "Cardio",
        "Leg",
        "Arm",
        "Chest",
        "Back",
        "Abdomen",
    ]
}

EXPECTED_EXERCISES = {
    "items": [
        {"name": "Walk", "type": "Cardio", "unit": "metre"},
        {"name": "Walk", "type": "Cardio", "unit": "second"},
        {"name": "Run", "type": "Cardio", "unit": "metre"},
        {"name": "Run", "type": "Cardio", "unit": "second"},
        {"name": "Jumping jacks", "type": "Cardio"},
        {"name": "Squat", "type": "Leg"},
        {"name": "Lunge", "type": "Leg"},
        {"name": "Deadlift", "type": "Leg"},
        {"name": "Bicep curl", "type": "Arm"},
        {"name": "Hammer curl", "type": "Arm"},
        {"name": "Tricep dips", "type": "Arm"},
        {"name": "Close grip push up", "type": "Arm"},
        {"name": "Push-down", "type": "Arm"},
        {"name": "Lateral raise", "type": "Arm"},
        {"name": "Front raise", "type": "Arm"},
        {"name": "Arnold press", "type": "Arm"},
        {"name": "Push up", "type": "Chest"},
        {"name": "Bench press", "type": "Chest"},
        {"name": "Fly", "type": "Chest"},
        {"name": "Pull-down", "type": "Back"},
        {"name": "Pull-up", "type": "Back"},
        {"name": "Bent-over row", "type": "Back"},
        {"name": "Crunch", "type": "Abdomen"},
        {"name": "Bicycle crunch", "type": "Abdomen"},
        {"name": "Plank", "type": "Abdomen"},
    ]
}


# File for the variance threshold feature selection algorithm
from sklearn.feature_selection import VarianceThreshold

# The function which will be called
def get_features(raw_data, raw_data_ids):
    '''
    Perform feature selection using variance threshold
    Removes features without any variance
    '''

    # Returns columns that meet the amount of variance
    sel = VarianceThreshold()
    sel.fit(raw_data)

     # Set aside correct columns
    return_columns = []
    index = 0
    for feature in sel.get_support():
        if feature:
            return_columns.append(index)
        index += 1

    # return
    return return_columns


#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Auther : Jianghao
# Date : 2017/11/4

v1 = '姓名:%s'
aa = '姜昊1111'

new_v1 = v1%aa
print(new_v1)

new_v2 = v1%'姜昊2222'
print(new_v2)


name_scores = []
scores = set()
for _ in range(int(raw_input())):
    name = raw_input()
    score = float(raw_input())
    name_scores.append([name, score])
    scores.add(score)

scores.remove(min(scores))
second_min = min(scores)
for (name, score) in sorted(name_scores, key=lambda x : x[0]):
    if score == second_min:
        print(name)