import streamlit as st
from graph import box,histogram,pie
from dataframe import df

def score():
    s_names = ('S1','S2','S3','S4','S5','S6','S7','S8','S9','S10','GPA')
    s = st.radio('Điểm từng session',s_names,horizontal=True)
    if s == 'S1':
        box(df,'CLASS-GROUP','S1','GENDER')
        histogram(df,'S1',None,'PYTHON-CLASS')

    if s == 'S2':
        box(df,'CLASS-GROUP','S2','GENDER')
        histogram(df,'S2',None,'PYTHON-CLASS')

    if s == 'S3':
        box(df,'CLASS-GROUP','S3','GENDER')
        histogram(df,'S3',None,'PYTHON-CLASS')
    if s == 'S4':
        box(df,'CLASS-GROUP','S4','GENDER')
        histogram(df,'S4',None,'PYTHON-CLASS')

    if s == 'S5':
        box(df,'CLASS-GROUP','S5','GENDER')
        histogram(df,'S5',None,'PYTHON-CLASS')

    if s == 'S6':
        box(df,'CLASS-GROUP','S6','GENDER')
        histogram(df,'S6',None,'PYTHON-CLASS')

    if s == 'S7':
        box(df,'CLASS-GROUP','S7','GENDER')
        histogram(df,'S7',None,'PYTHON-CLASS')

    if s == 'S8':
        box(df,'CLASS-GROUP','S8','GENDER')
        histogram(df,'S8',None,'PYTHON-CLASS')

    if s == 'S9':
        box(df,'CLASS-GROUP','S9','GENDER')
        histogram(df,'S9',None,'PYTHON-CLASS')

    if s == 'S10':
        box(df,'CLASS-GROUP','S10','GENDER')
        histogram(df,'S10',None,'PYTHON-CLASS')

    if s == 'GPA':
        box(df,'CLASS-GROUP','GPA',None)
        st.success('Các lớp khối tự nhiên học tốt hơn lớp khối xã hội')
        st.success('Lớp Chuyên Tin học giỏi nhất')
        box(df,'PYTHON-CLASS','GPA',None)
        st.success('Lớp 114 học tốt hơn lớp 115')
        histogram(df,'GPA',None,'GENDER')
        st.success('Nam sinh học tốt hơn nữ sinh')
        pie(df[df['GPA'] >= 6],'CLASS-GROUP','Tỉ lệ đậu lớp MC')
def graph():
    a_names = ('PHÂN BỐ HS NAM - NỮ','PHÂN BỐ HS LỚP SÁNG - CHIỀU','PHÂN BỐ HS CHUYÊN - THƯỜNG','PHÂN BỐ HS CÁC KHỐI')
    a = st.radio('Loại biểu đồ',a_names,horizontal=True)
    if a=='PHÂN BỐ HS NAM - NỮ':
        pie(df, df["GENDER"],"PHÂN BỐ HS NAM - NỮ")
        st.write('Nam hứng thú với AI hơn')
    if a=='PHÂN BỐ HS LỚP SÁNG - CHIỀU':
        pie(df, df["PYTHON-CLASS"],"PHÂN BỐ HS LỚP SÁNG CHIỀU")
        st.write('Học sinh lớp sáng chiều phân bố khá đồng đều')
    if a=='PHÂN BỐ HS CHUYÊN - THƯỜNG':
        pie(df, df["CLASS-GROUP"],"PHÂN BỐ HS CHUYÊN - THƯỜNG")
        st.write('Học sinh lớp chuyên có hứng thú với AI hơn')
    if a=='PHÂN BỐ HS CÁC KHỐI':
        pie(df, df["GRADE"],"PHÂN BỐ HS CÁC KHỐI")
        st.write("Học sinh lớp 12 ít có số lượng đăng kí học AI ít nhất do bận ôn thi THPTQG")


# flake8: noqa

import pyarrow.feather as feather
import pandas as pd
import json
import numpy as np
import pyarrow as pa
import pyarrow.parquet as pq
from pandas.util.testing import rands
import gc
import time

def get_timing(f, niter):
    start = time.clock_gettime(time.CLOCK_REALTIME)
    for i in range(niter):
        f()
    result = (time.clock_gettime(time.CLOCK_REALTIME) - start) / niter
    return result

class Benchmarker:

    def __init__(self, file_info):
        self.base = file_info['base']
        (self.csv_path,
         self.sep,
         self.header) = unpack(file_info['source'], 'path', 'sep', 'header')

        self.parquet_unc_path = '{}_uncompressed.parquet'.format(self.base)
        self.parquet_snappy_path = '{}_snappy.parquet'.format(self.base)
        self.feather_unc_path = '{}_uncompressed.feather'.format(self.base)
        self.feather_lz4_path = '{}_lz4.feather'.format(self.base)
        self.feather_zstd_path = '{}_zstd.feather'.format(self.base)

    def bench_read(self, niter=5):
        cases = [
            ('parquet (UNC)', 'arrow Table',
             lambda: pq.read_table(self.parquet_unc_path, memory_map=False)),
            ('parquet (UNC)', 'pandas',
             lambda: (pq.read_table(self.parquet_unc_path, memory_map=False)
                      .to_pandas())),
            ('parquet (SNAPPY)', 'arrow Table',
             lambda: pq.read_table(self.parquet_snappy_path,
                                   memory_map=False)),
            ('parquet (SNAPPY)', 'pandas',
             lambda: (pq.read_table(self.parquet_snappy_path, memory_map=False)
                      .to_pandas())),
            ('feather V2 (UNC)', 'pandas',
             lambda: feather.read_feather(self.feather_unc_path,
                                          memory_map=False)),
            ('feather V2 (LZ4)', 'pandas',
             lambda: feather.read_feather(self.feather_lz4_path,
                                          memory_map=False)),
            ('feather V2 (ZSTD)', 'pandas',
             lambda: feather.read_feather(self.feather_zstd_path,
                                          memory_map=False)),
            ('feather V2 (UNC)', 'arrow Table',
             lambda: feather.read_table(self.feather_unc_path,
                                        memory_map=False)),
            ('feather V2 (LZ4)', 'arrow Table',
             lambda: feather.read_table(self.feather_lz4_path,
                                        memory_map=False)),
            ('feather V2 (ZSTD)', 'arrow Table',
             lambda: feather.read_table(self.feather_zstd_path,
                                        memory_map=False)),
        ]

        return self._bench_cases(cases, niter)

    def bench_write(self, niter=2):
        print("Reading text file: {}".format(self.csv_path))
        df = pd.read_csv(self.csv_path, sep=self.sep, header=self.header,
                         low_memory=False)
        if self.header is None:
            df.columns = ['f{}'.format(i) for i in range(len(df.columns))]

        def _get_table(df):
            return (pa.Table.from_pandas(df, preserve_index=False)
                    .replace_schema_metadata(None))

        t = _get_table(df)

        cases = [
            ('parquet (UNC)', 'arrow Table',
             lambda: pq.write_table(t, self.parquet_unc_path,
                                    compression='NONE')),
            ('parquet (UNC)', 'pandas',
             lambda: pq.write_table(_get_table(df), self.parquet_unc_path,
                                    compression='NONE')),
            ('parquet (SNAPPY)', 'arrow Table',
             lambda: pq.write_table(t, self.parquet_snappy_path)),
            ('parquet (SNAPPY)', 'pandas',
             lambda: pq.write_table(_get_table(df), self.parquet_snappy_path)),
            ('feather V2 (UNC)', 'pandas',
             lambda: feather.write_feather(df, self.feather_unc_path,
                                           compression='uncompressed')),
            ('feather V2 (UNC)', 'arrow Table',
             lambda: feather.write_feather(t, self.feather_unc_path,
                                           compression='uncompressed')),
            ('feather V2 (LZ4)', 'pandas',
             lambda: feather.write_feather(df, self.feather_lz4_path,
                                           compression='lz4')),
            ('feather V2 (LZ4)', 'arrow Table',
             lambda: feather.write_feather(t, self.feather_lz4_path,
                                           compression='lz4')),
            ('feather V2 (ZSTD)', 'pandas',
             lambda: feather.write_feather(df, self.feather_zstd_path,
                                           compression='zstd')),
            ('feather V2 (ZSTD)', 'arrow Table',
             lambda: feather.write_feather(t, self.feather_zstd_path,
                                           compression='zstd'))
        ]

        return self._bench_cases(cases, niter)

    def _bench_cases(self, cases, niter):
        results = []
        for name, output_type, f in cases:
            print(name)
            result = (name, output_type, get_timing(f, niter))
            print(result)
            results.append(result)
        return pd.DataFrame.from_records(results,
                                         columns=['expr', 'output_type',
                                                  'mean'])

def unpack(d, *fields):
    return (d[f] for f in fields)

files = {
    'fanniemae': {
        'base': '2016Q4',
        'source': {
            'path': 'data/2016Q4.csv',
            'sep': '|',
            'header': None
        }
    },
    'nyctaxi': {
        'base': 'yellow_tripdata_2010-01',
        'source': {
            'path': 'data/yellow_tripdata_2010-01.csv',
            'sep': ',',
            'header': 0
        }
    }
}

def run_benchmarks(num_threads, what='read'):
    pa.set_cpu_count(num_threads)

    all_results = []
    for name, info in files.items():
        benchmarker = Benchmarker(info)
        if what == 'read':
            print("Benchmarking reads")
            file_results = benchmarker.bench_read()
        elif what == 'write':
            print("Benchmarking writes")
            file_results = benchmarker.bench_write()
        else:
            raise ValueError(what)
        file_results['dataset'] = name
        all_results.append(file_results)

    print(all_results)
    return pd.concat(all_results, ignore_index=True)

# for i in range(5):
#     pq.read_table('yellow_tripdata_2010-01.parquet').to_pandas()

# write_files(files)

num_threads_cases = [1, 4]

for nthreads in num_threads_cases:
    write_results = run_benchmarks(nthreads, what='write')
    write_results.to_csv('py_write_results_{}.csv'.format(nthreads))

    read_results = run_benchmarks(nthreads, what='read')
    read_results.to_csv('py_read_results_{}.csv'.format(nthreads))

# for nthreads in num_threads_cases:
#     run_benchmarks(nthreads)

# ('pyarrow.parquet', 1.5470361709594727)
# ('pyarrow.parquet-pandas', 2.925654172897339)
# ('pyarrow.feather', 1.6384665012359618)


import numpy as np
import torch
import util
import nnet
import tqdm
import os

def extract_gnn_feature(args):
    # get loader
    train_loader, dev_loader = util.get_visualize_loader(args)

    # create model
    model = nnet.get_model(args)
    nnet.print_summary(model)
    augment_model = nnet.Augmentation(args)

    # load model
    # path = '/home/thanh/gnn-mvdr/ckpt/baseline/checkpoints/baseline.ckpt'
    path = '/home/thanh/gnn-mvdr/ckpt-09082021/baseline/checkpoints/baseline.ckpt'
    checkpoint = torch.load(path)
    epoch = checkpoint['epoch']
    model.load_state_dict(checkpoint['model_state_dict'])
    model.eval()

    # reset output folder
    z_in_folder  = '../feature/z_in'
    z_out_folder = '../feature/z_out'
    for folder in [z_in_folder, z_out_folder]:
        if os.path.exists(folder):
            os.system(f'rm -r {folder}')
        os.makedirs(folder)

    # generate z
    idx = 0
    with torch.no_grad():
        with tqdm.tqdm(dev_loader, unit="it") as pbar:
            pbar.set_description(f'Epoch {epoch}')
            for batch_idx, batch in enumerate(pbar):
                # extract data
                cleans, noises, rirs, rirs_idx = batch
                # augmenta data
                inputs, cleans, noises, clean_reverbs, noise_reverbs = augment_model(cleans, noises, rirs)
                # encode
                z_in, z_out  = model.encode(inputs)
                # z_in, z_out = model.encode(clean_reverbs)
                # z_in, z_out = model.encode(noise_reverbs)
                z_in        = z_in.detach().cpu().numpy()
                z_out       = z_out.detach().cpu().numpy()
                # save data
                batch_size  = rirs_idx.shape[0]
                for i in range(batch_size):
                    idx += 1

                    mdict = {
                        'feature': z_in[i],
                        'label': rirs_idx[i],
                    }
                    path = os.path.join(z_in_folder, f'{idx}.npz')
                    np.savez(path, **mdict)

                    mdict = {
                        'feature': z_out[i],
                        'label': rirs_idx[i],
                    }
                    path = os.path.join(z_out_folder, f'{idx}.npz')
                    np.savez(path, **mdict)


import logging
import datetime

from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import (
    AbstractRequestHandler,
    AbstractRequestInterceptor,
    AbstractResponseInterceptor)
import ask_sdk_core.utils as ask_utils
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import Response

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

class TellTimeHandler(AbstractRequestHandler):
    """Handler for Skill Launch and TellTime Intent."""
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return (ask_utils.is_request_type("LaunchRequest")(handler_input) or
                ask_utils.is_intent_name("TellTimeIntent")(handler_input))

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        logger.info("In TellTimeHandler")

        current_time = datetime.datetime.utcnow().time()
        hour = current_time.strftime('%I').strip('0')
        minute = current_time.strftime('%M')
        ampm = current_time.strftime('%p')

        return (
            handler_input.response_builder
            .speak(f"Hello! It is currently {hour}:{minute} {ampm} U.T.C.")
            .response
        )

class RequestLogger(AbstractRequestInterceptor):
    """Log all Alexa requests."""
    def process(self, handler_input):
        # type: (HandlerInput) -> None
        logger.debug("Alexa Request: {}".format(
            handler_input.request_envelope.request))

class ResponseLogger(AbstractResponseInterceptor):
    """Log all Alexa responses."""
    def process(self, handler_input, response):
        # type: (HandlerInput, Response) -> None
        logger.debug("Alexa Response: {}".format(response))

sb = SkillBuilder()

sb.add_request_handler(TellTimeHandler())

sb.add_global_request_interceptor(RequestLogger())
sb.add_global_response_interceptor(ResponseLogger())

handler = sb.lambda_handler()


from arts import logo
print(logo,"\n")
print("================================================================", '\n')

lances = {}
lances_encerrados = False

def procurar_maior_aposta(apostas_registro):
    maior_aposta = 0
    for apostador in apostas_registro:
        aposta_valor = apostas_registro[apostador]
        if aposta_valor > maior_aposta:
            maior_aposta = aposta_valor
            vencedor = apostador
    print(f"O vencedor é {vencedor} com a aposta de ${maior_aposta}")

while not lances_encerrados:
    name = input('Qual seu nome?: ')
    valor = float(input('Qual teu lance?: $'))
    lances[name] = valor
    continuar = input('Deseja continuar apostando? "Sim" ou "Nao"\n').lower()
    if continuar == 'nao':
        lances_encerrados = True
        procurar_maior_aposta(lances)

Days 1-9 (Beginner)/Day 9 - Dictionaries, Nesting and the Secret Auction/day_9_project.py


"""
A person wants to determine the most expensive computer keyboard and USB drive that
can be purchased with a given budget. Given price lists for keyboards and USB drives and a budget,
find the cost to buy them. If it is not possible to buy both items, return -1.

Example
b = 60
keyboards = [40,50,60]
drives = [5,8,12]

The person can buy a 40 keyboard + 12 drive =  52, or a 40 keyboard + 8 drive =  58.
Choose the latter as the more expensive option and return 58.

Function Description

Complete the getMoneySpent function in the editor below.

Parameter(s):
        int keyboards[n]: the keyboard prices
        int drives[m]: the drive prices
        int b: the budget

Returns
    int: the maximum that can be spent, or -1 if it is not possible to buy both items
"""

budget1 = 10
keyboards1 = [3,1]
drives1 = [5,2,8]

budget2 = 5
keyboards2 = [4]
drives2 = [5]

budget3 = 60
keyboards3 = [40,50,60]
drives3 = [5,8,12]

def Solution(b,k,d):
    if min(k)+min(d) > b:
        return -1
    maxx = 0
    for i in k:
        for j in d:
            if i+j > maxx and i+j <= b:
                maxx = i+j
    return maxx

val = Solution(budget1, keyboards1, drives1)
print(val)

val = Solution(budget2, keyboards2, drives2)
print(val)

val = Solution(budget3, keyboards3, drives3)
print(val)


import ast
import string
import pandas as pd
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
from nltk import stem
from nltk.tokenize import word_tokenize
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer, TfidfTransformer

from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, r2_score
from sklearn.ensemble import VotingRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR

pd.set_option('display.width', 400)
pd.set_option('display.max_columns', 10)

def clean_text(text):
    text = text.lower()

    # Lancaster is strictest and PorterStemmer is least strict.
    # Snowball is in the middle, so we use Snowball here for stemming text.
    stemmer = stem.SnowballStemmer('english')
    stop_words = set(stopwords.words('english'))

    word_tokens = word_tokenize(text)
    not_punctuation = [word for word in word_tokens if word not in string.punctuation]
    cleaned = [word for word in not_punctuation if word not in stop_words]
    cleaned_stem = [stemmer.stem(word) for word in cleaned]
    cleaned_join = " ".join(cleaned_stem)

    return cleaned_join

# read file
filename = "movies.csv"
data = pd.read_csv(filename, sep=',', converters={'genres': ast.literal_eval})

"""

**************** CLEANING AND ETL ****************

"""

data = data[data['genres'].apply(len).gt(0)]  # exclude observations with no genres recorded
data = data[data['overview'].notna()]  # exclude observations with no overview recorded
data = data.copy().reset_index(drop=True)

''' EXTRACT GENRES '''

movie_genres = data[['original_title', 'overview', 'genres', 'vote_average']]
movie_genres = movie_genres.copy()
genres = pd.read_csv("genres_collection.csv", sep=',')
sorted_unique_genres = genres['genre'].tolist()

movie_genres['genres'] = movie_genres['genres'].apply(lambda x: [i[1] for i in x])
movie_genres['genres'] = movie_genres['genres'].apply(", ".join)

# increase the weight for the genres by 3 times compared to the movie overview
movie_genres['overview_genres'] = movie_genres['overview'] + movie_genres['genres'] + movie_genres['genres'] + movie_genres['genres']

''' PROCESS TEXT '''

movie_genres['overview'] = movie_genres['overview'].astype(str)
# movie_genres['overview'] = movie_genres['overview'].apply(clean_text)
tfidfvectorizer = TfidfVectorizer(analyzer=clean_text, stop_words='english')
overview_bow = tfidfvectorizer.fit_transform(movie_genres['overview'])
# print(overview_bow)
# print(type(overview_bow))
# overview_bow = overview_bow.todense()
# print(type(overview_bow))

"""

**************** ANALYZING ****************

"""

''' Build a ML model to predict movie rating based on overview and genres '''
X_train, X_valid, y_train, y_valid = train_test_split(overview_bow, movie_genres['vote_average'])

print("KNeighbors Regressor:")
model_kneighbors = KNeighborsRegressor(15)
model_kneighbors.fit(X_train, y_train)
print(model_kneighbors.score(X_train, y_train))
print(model_kneighbors.score(X_valid, y_valid))
y_pred = model_kneighbors.predict(X_valid)
print(r2_score(y_valid, y_pred))
print("")

print("Random Forest Regressor:")
model_forest = RandomForestRegressor(1000, max_depth=5, min_samples_leaf=100, random_state=50)
model_forest.fit(X_train, y_train)
print(model_forest.score(X_train, y_train))
print(model_forest.score(X_valid, y_valid))
y_pred = model_kneighbors.predict(X_valid)
print(r2_score(y_valid, y_pred))
print("")

# https://machinelearningmastery.com/configure-gradient-boosting-algorithm/
print("Gradient Boost Regressor:")
model_gradient_boost = GradientBoostingRegressor(n_estimators=500, max_depth=4, min_samples_leaf=6)
model_gradient_boost.fit(X_train, y_train)
print(model_gradient_boost.score(X_train, y_train))
print(model_gradient_boost.score(X_valid, y_valid))
y_pred = model_kneighbors.predict(X_valid)
print(r2_score(y_valid, y_pred))
print("")

print("Support Vector Machine Regressor:")
model_svr = SVR(kernel='rbf')
model_svr.fit(X_train, y_train)
print(model_svr.score(X_train, y_train))
print(model_svr.score(X_valid, y_valid))
y_pred = model_kneighbors.predict(X_valid)
print(r2_score(y_valid, y_pred))
print("")

print("Neural Network Regressor:")
model_neural = MLPRegressor(hidden_layer_sizes=(5, 4), activation='logistic', solver='lbfgs', max_iter=36000)
model_neural.fit(X_train, y_train)
print(model_neural.score(X_train, y_train))
print(model_neural.score(X_valid, y_valid))
y_pred = model_kneighbors.predict(X_valid)
print(r2_score(y_valid, y_pred))
print("")

print("Voting Regressor:")
model_voting = VotingRegressor([
                            ('neighbors', KNeighborsRegressor(50)),
                            ('forest', RandomForestRegressor(n_estimators=100, min_samples_leaf=20)),
                            ('svr', SVR(kernel='rbf')),
                            ('neural', MLPRegressor(hidden_layer_sizes=(4, 5), activation='logistic', solver='lbfgs', max_iter=50000))
                            ]
)

model_voting.fit(X_train, y_train)
print(model_voting.score(X_train, y_train))
print(model_voting.score(X_valid, y_valid))
y_pred = model_kneighbors.predict(X_valid)
print(r2_score(y_valid, y_pred))
print("")


import sys
sys.stdin = open("input.txt","rt")

N = int(input())
seq=[list(map(int,input().split())) for _ in range(N)]

seq.sort(reverse=True)

max_height=[0]*(N)
#head_weight=[0]*(N)
#head_width=[0]*(N)

max_height[0] = seq[0][1]
#head_weight[0] = seq[0][2]
#head_width[0] = seq[0][0]

#print(seq)
for i in range(1,N):
    max_h = 0
#    head_w = 0
#    head_wi= 0
#    print(i)
    for j in range(0,i):
#        print(j,i)
#        print(seq[j],seq[i])
#        print(max_h)
        if seq[i][2] < seq[j][2] and max_h < max_height[j]:
            max_h = max_height[j]
            #head_w = head_weight[j]
            #head_wi = head_width[j]

    max_height[i] = max_h + seq[i][1]
    #head_weight[i] = head_w + seq[i][2]
    #head_width[i] = head_wi + seq[i][0]

#print(max_height)
print(max(max_height))

Chapter8/6. 가장 높은 탑 쌓기(LIS 응용)/SH_cht8_6.py


import numpy as np
import seaborn as sns
import shap
from matplotlib import pyplot as plt
from sklearn.calibration import CalibrationDisplay
from sklearn.metrics import auc, precision_recall_curve, average_precision_score, plot_roc_curve, plot_precision_recall_curve, \
    ConfusionMatrixDisplay, PrecisionRecallDisplay
from sweetviz import compare, analyze
import os.path as pth
import warnings
#001C7F, #D62728, #017517, #8C0900, #7600A1, #B8860B, #FF7F0E
dpi = 300
colors = ['#001C7F', '#D62728', '#017517', '#8C0900', '#7600A1', '#B8860B', '#FF7F0E']

model_name_replacements = {
    'DecisionTreeClassifier': 'Decision tree',
    'LogisticRegression': 'Logistic regression',
    'LinearSVC': 'Linear SVM',
    'GradientBoostingClassifier': 'Gradient boosting machine',
    'RandomForestClassifier': 'Random forest',
    'SVC': 'SVM',
    'MLPClassifier': 'Neural network',
}

def boxplot(out_dir, data, metric_name, y_label, ymin=0, ymax=1):
    """Prints boxplot for CV Splits and additionally plots test set value

    Parameters
    ----------
    out_dir : str
        Base output directory
    data : dict
        Metric data in the form {model_name: (list of val split results, test split result)}
    metric_name : str
        The name of the metric
    y_label : str
        The name of the predicted endpoint
    ymin : int, default=0
        min of y axis (usually 0)
    ymax : int, default=0
        max of y axis (usually 1)
    """

    fig = plt.figure()
    fig.suptitle(f'{metric_name} for all models predicting {y_label}')
    ax = fig.add_subplot(111)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

    model_names = list(data.keys())
    # Plot val boxplot
    val_data = list(map(lambda x: x[0], data.values()))
    plt.boxplot(val_data)

    # Plot test single data point
    test_data = list(map(lambda x: x[1], data.values()))
    plt.scatter(range(1, len(model_names) + 1), test_data, marker='o', color='blue')

    # Format axes etc
    ax.set_xticklabels([model_name_replacements.get(model_name, model_name) for model_name in model_names], rotation=45, ha='right')
    ax.set_ylim(ymin, ymax)
    ax.set_ylabel(metric_name)
    plt.tight_layout()
    plt.savefig(f'{out_dir}/{y_label.replace(" ", "_")}/all_models_{metric_name}', dpi=dpi)
    plt.close()

def plot_coefficients(out_dir, coefs, feature_names, model_name, label_name, top_features=None):
    # filter out 0 values
    filter_mask = (abs(coefs) > 1e-3)
    coefs = coefs[filter_mask]
    feature_names = feature_names[filter_mask]
    if top_features:
        top_coef_indixes = np.argsort(abs(coefs))[-top_features:]
        coefs = coefs[top_coef_indixes]
        feature_names = feature_names[top_coef_indixes]
    # sort coefficients
    sort_mask = np.argsort(coefs)
    coefs = coefs[sort_mask]
    feature_names = feature_names[sort_mask]
    # plot
    plt.figure(figsize=(30, 10))
    plt.suptitle(f'Feature importance - {model_name} predicting {label_name}')
    colors = ['red' if c < 0 else 'blue' for c in coefs]
    plt.bar(np.arange(len(coefs)), coefs, color=colors)
    plt.xticks(np.arange(len(coefs)), feature_names, rotation=60, ha='right')
    plt.tight_layout()
    plt.savefig(f'{out_dir}/{label_name.replace(" ", "_")}/test/{model_name}_feature_importance', dpi=dpi)
    plt.close()

def plot_summary_roc(all_model_metrics, out_dir, label_col, dataset_partition='val', title=None, legend=False, value_in_legend=True):
    """
    Plot ROC curves for all models in one figure.
    Parameters
    ----------
    all_model_metrics:
    out_dir: str
    label_col:
    dataset_partition: str
        'val' or 'test', depending on which curves to plot (for CV or test performance)
    title: Include title in plot.
    legend: Include legend in plot.
    """
    if dataset_partition == 'val':
        ds_index = 0
    else:
        ds_index = 1

    plt.rcParams['font.family'] = "Arial"
    plt.style.use('seaborn-notebook')
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.set_aspect('equal')
    if title:
        fig.suptitle(f"{title} {label_col}")
    ax.plot([0, 1], [0, 1], linestyle='--', lw=2, color='grey', label='Chance', alpha=.8)

    x_linspace = np.linspace(0, 1, 101)
    roc_prc_data = {model_name: entry[2] for model_name, entry in all_model_metrics.items()}
    i = 0
    for model_name, val_and_test_curves_data in roc_prc_data.items():
        if model_name == 'DummyClassifier':
            continue
        if value_in_legend:
            label = '{} (AUROC = {:.2f})'.format(model_name_replacements.get(model_name, model_name),
                                                 np.mean(all_model_metrics[model_name][ds_index]['roc_auc']))
        else:
            label = model_name_replacements.get(model_name, model_name)
        ax.plot(x_linspace, val_and_test_curves_data[ds_index][0],
                label=label,
                lw=2, alpha=.8, color=colors[i])
        i += 1
    ax.set(xlim=[-0.05, 1.05], ylim=[-0.05, 1.05])
    if legend:
        ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
    plt.xlabel('False Positive Rate', fontsize=18)
    plt.ylabel('True Positive Rate', fontsize=18)
    ax.tick_params(axis='both', which='major', labelsize=12)
    ax.tick_params(axis='both', which='minor', labelsize=12)
    plt.savefig(f'{out_dir}/{label_col}/all_models_{dataset_partition}_roc_curves'.replace(' ', '_'),
                bbox_inches='tight', dpi=dpi)
    plt.close()

def plot_summary_prc(all_model_metrics, out_dir, label_col, y, dataset_partition='val', title=None, legend=False, value_in_legend=True):
    """
    Plot PRC curves for all models in one figure.
    Parameters
    ----------
    all_model_metrics:
    out_dir: str
    label_col:
    y:
    dataset_partition: str
        'val' or 'test', depending on which curves to plot (for CV or test performance)
    title: Include title in plot.
    legend: Include legend in plot.
    """
    if dataset_partition == 'val':
        ds_index = 0
    else:
        ds_index = 1

    plt.rcParams['font.family'] = "Arial"
    plt.style.use('seaborn-notebook')
    fig, ax = plt.subplots(figsize=(10, 5))
    ax.set_aspect('equal')
    if title:
        fig.suptitle(f"{title} {label_col}")
    ax.plot([0, 1], [(len(y[y == 1]) / len(y)), (len(y[y == 1]) / len(y))], linestyle='--', lw=2, color='grey',
            label='Chance', alpha=.8)
    roc_prc_data = {model_name: entry[2] for model_name, entry in all_model_metrics.items()}
    i = 0
    for model_name, val_and_test_curves_data in roc_prc_data.items():
        if model_name == 'DummyClassifier':
            continue
        if value_in_legend:
            label = '{} (AUPRC = {:.2f})'.format(model_name_replacements.get(model_name, model_name),
                                                 np.mean(all_model_metrics[model_name][ds_index]['prc_auc']))
        else:
            label = model_name_replacements.get(model_name, model_name)
        ax.plot(val_and_test_curves_data[ds_index][2], val_and_test_curves_data[ds_index][1],
                label=label,
                lw=2, alpha=.8, color=colors[i])
        i += 1
    ax.set(xlim=[-0.05, 1.05], ylim=[-0.05, 1.05])
    plt.xlabel('Recall', fontsize=18)
    plt.ylabel('Precision', fontsize=18)
    ax.tick_params(axis='both', which='major', labelsize=12)
    ax.tick_params(axis='both', which='minor', labelsize=12)
    if legend:
        ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
    plt.savefig(f'{out_dir}/{label_col}/all_models_{dataset_partition}_prc_curves'.replace(' ', '_'),
                bbox_inches='tight', dpi=dpi)
    plt.close()

def plot_summary_roc_pr(all_model_metrics, out_dir, label_col, y):
    """
    Plot both ROC and PR curves for all models in one figure.
    Parameters
    ----------
    all_model_metrics:
    out_dir: str
    label_col:
    y:
    """
    # Plot mean val & test ROC and PR Curve for all models
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
    ax1.set_aspect('equal')
    ax2.set_aspect('equal')
    fig.suptitle(f'Predicting {label_col} ROC & PR Curves')
    ax1.plot([0, 1], [0, 1], linestyle='--', lw=2, color='grey',
             label='Chance', alpha=.8)
    ax2.plot([0, 1], [(len(y[y == 1]) / len(y)), (len(y[y == 1]) / len(y))], linestyle='--', lw=2, color='grey',
             label='Chance', alpha=.8)
    x_linspace = np.linspace(0, 1, 101)
    roc_prc_data = {model_name: entry[2] for model_name, entry in all_model_metrics.items()}
    for model_name, (val_curves_data, test_curves_data) in roc_prc_data.items():
        if model_name == 'DummyClassifier':
            continue
        ax1.plot(x_linspace, val_curves_data[0],
                 label='{} (AUROC = {:.2f})'.format(model_name_replacements.get(model_name, model_name),
                                                    np.mean(all_model_metrics[model_name][0]['roc_auc'])),
                 lw=1, alpha=.8)
        ax2.plot(val_curves_data[2], val_curves_data[1],
                 label='{} (AUPRC = {:.2f})'.format(model_name_replacements.get(model_name, model_name),
                                                    np.mean(all_model_metrics[model_name][0]['prc_auc'])),
                 lw=1, alpha=.8)
    ax1.set(xlim=[-0.05, 1.05], ylim=[-0.05, 1.05],
            title='ROC')
    ax1.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
    ax2.set(xlim=[-0.05, 1.05], ylim=[0.0, 1.05],
            title='Precision-Recall')
    ax2.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))
    plt.savefig(f'{out_dir}/{label_col}/all_models_roc_prc_curves'.replace(' ', '_'),
                bbox_inches='tight', dpi=dpi)
    plt.close()

def plot_val_mean_roc(ax, tprs, aucs, x_linspace):
    ax.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
            label='Chance', alpha=.8)

    mean_tpr = np.mean(tprs, axis=0)
    mean_tpr[-1] = 1.0
    mean_auc = auc(x_linspace, mean_tpr)
    std_auc = np.std(aucs)
    ax.plot(x_linspace, mean_tpr, color='b',
            label=r'Mean ROC (AUROC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
            lw=2, alpha=.8)

    std_tpr = np.std(tprs, axis=0)
    tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
    tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
    ax.fill_between(x_linspace, tprs_lower, tprs_upper, color='grey', alpha=.2,
                    label=r'$\pm$ 1 std. dev.')

    ax.set(xlim=[-0.05, 1.05], ylim=[-0.05, 1.05],
           title='ROC')
    ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))

    return mean_tpr

def plot_val_mean_prec_rec(ax, ys_real, ys_proba, pos_class_fraction):
    ax.plot([0, 1], [pos_class_fraction, pos_class_fraction], linestyle='--', lw=2, color='r',
            label='Chance', alpha=.8)

    overall_precision, overall_recall, _ = precision_recall_curve(ys_real, ys_proba)
    overall_prc_auc = auc(overall_recall, overall_precision)
    overall_prc_ap = average_precision_score(ys_real, ys_proba)
    ax.plot(overall_recall, overall_precision, color='b',
            label=r'Overall Prec_Rec (AP = %0.2f, AUC = %.2f)' % (overall_prc_ap, overall_prc_auc),
            lw=2, alpha=.8)

    ax.set(xlim=[-0.05, 1.05], ylim=[0.0, 1.05],
           title='Precision-Recall')

    ax.legend(loc="upper center", bbox_to_anchor=(0.5, -0.15))

    return overall_precision, overall_recall, overall_prc_auc

def data_exploration_comparison(train_data, test_data, endpoint, file_path):
    exploration_report = compare([train_data, "Training data"], [test_data, "External Test Data"], endpoint)
    exploration_report.show_html(filepath=pth.join(file_path, f"dataset_comparison_{endpoint}.html"),
                                 open_browser=False,
                                 layout='widescreen',
                                 scale=None)

def data_exploration(data, endpoint, file_path):
    exploration_report = analyze([data, "Source Data"], endpoint)
    exploration_report.show_html(filepath=pth.join(file_path, f"dataset_exploration_{endpoint}.html"),
                                 open_browser=False,
                                 layout='widescreen',
                                 scale=None)

def plot_roc_pr_curve(X_test, y_test, endpoint, model, model_name, out_dir):
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
    ax1.set_aspect('equal')
    ax2.set_aspect('equal')
    ax1.set(xlim=[-0.05, 1.05], ylim=[0.0, 1.05])
    ax2.set(xlim=[-0.05, 1.05], ylim=[0.0, 1.05])
    fig.suptitle(f'{model_name} predicting {endpoint}')
    # ROC
    roc_plot = plot_roc_curve(model, X_test, y_test,
                              name='ROC curve', lw=1, ax=ax1)
    ax2.set(xlim=[-0.05, 1.05], ylim=[0.0, 1.05])
    prc_plot = PrecisionRecallDisplay.from_estimator(model, X_test, y_test,
                                                     name='PR curve', lw=1, ax=ax2)
    plt.savefig(f'{out_dir}/{endpoint}/test/{model_name}_roc_prc_curves'.replace(' ', '_'), bbox_inches='tight', dpi=dpi)

    return roc_plot, prc_plot

def plot_confusion_matrix(label, confusion_matrix, model_name, out_dir, phase):
    cm_fig, ax = plt.subplots()
    cm_fig.suptitle(f'{model_name} predicting {label}')
    disp = ConfusionMatrixDisplay(confusion_matrix=confusion_matrix,
                                  display_labels=[0, 1])
    disp.plot(include_values=True, cmap='Blues', ax=ax,
              xticks_rotation='horizontal', values_format='d')
    plt.savefig(f'{out_dir}/{label}/{phase}/{model_name}_cm'.replace(' ', '_'))
    plt.close()

def plot_shap_values(X_test, X_train, y_train, cv, model, model_name, out_dir, select_features):
    def get_shap_values(test_data, train_data):
        if select_features:
            train_data = model.named_steps['selector'].transform(train_data)
            test_data = model.named_steps['selector'].transform(test_data)

        # Get SHAP values function
        if model_name == 'LogisticRegression':
            explainer = shap.LinearExplainer(model.named_steps['model'], train_data)
        elif model_name in ['DecisionTreeClassifier', 'RandomForestClassifier', 'GradientBoostingClassifier']:
            explainer = shap.TreeExplainer(model.named_steps['model'], train_data)
        else:
            if hasattr(model, "predict_proba"):
                f = lambda x: model.predict_proba(x)[:, 1]
                explainer = shap.KernelExplainer(f, train_data)
            else:
                explainer = shap.KernelExplainer(model.decision_function, train_data)
        return explainer.shap_values(test_data)

    with warnings.catch_warnings():
        warnings.filterwarnings("ignore")
        # Test SHAP values
        model.fit(X_train, y_train)
        test_shap_values = get_shap_values(X_test, X_train)
        shap.summary_plot(test_shap_values, X_test, show=False)
        plt.tight_layout()
        plt.savefig(f'{out_dir}/{y_train.name}/test/{model_name}_SHAP.png'.replace(' ', '_'), dpi=dpi)
        plt.close()

        shap.summary_plot(test_shap_values, X_test, plot_type='bar', show=False)
        plt.tight_layout()
        plt.savefig(f'{out_dir}/{y_train.name}/test/{model_name}_SHAP_bars.png'.replace(' ', '_'), dpi=dpi)
        plt.close()

        # CV SHAP values
        shap_values_list = []
        test_ixs = []
        for train_ix, test_ix in cv.split(X_train, y_train):
            test_ixs.append(test_ix)

            X_tr = X_train.iloc[train_ix]
            y_tr = y_train.iloc[train_ix]
            X_te = X_train.iloc[test_ix]
            model.fit(X_tr, y_tr)
            shap_values = get_shap_values(X_te, X_tr)
            for shap_value in shap_values:
                shap_values_list.append(shap_value)

        new_index = [ix for ix_test_fold in test_ixs for ix in ix_test_fold]
        shap.summary_plot(np.array(shap_values_list), X_train.reindex(new_index), show=False)
        plt.tight_layout()
        plt.savefig(f'{out_dir}/{y_train.name}/val/{model_name}_SHAP.png'.replace(' ', '_'), dpi=dpi)
        plt.close()

        shap.summary_plot(np.array(shap_values_list), X_train.reindex(new_index), plot_type='bar', show=False)
        plt.tight_layout()
        plt.savefig(f'{out_dir}/{y_train.name}/val/{model_name}_SHAP_bars.png'.replace(' ', '_'), dpi=dpi)
        plt.close()

def plot_calibration_curves(X_test, y_test, endpoint, model, model_name, out_dir):
    sns.set(style='darkgrid', context='talk', palette='rainbow')
    fig, ax = plt.subplots()
    fig.suptitle(f'{model_name} predicting {endpoint}')
    viz = CalibrationDisplay.from_estimator(model, X_test, y_test, n_bins=10, name=model_name, ax=ax)
    plt.tight_layout()
    plt.savefig(f'{out_dir}/{endpoint}/test/{model_name}_calibration'.replace(' ', '_'), dpi=dpi)
    plt.close()
    sns.reset_orig()


# 1. Задайте список, состоящий из произвольных чисел,
# количество задаёт пользователь.
# Напишите программу, которая найдёт сумму элементов списка,
# стоящих на нечётных позициях(не индексах).

from random import sample

def create_list(count, range_from, range_to):
    if count >= 0 and 0 <= range_from < range_to:
        new_list = sample(range(range_from, range_to + 1), count)
        return new_list
    else:
        print("Заданы неверные параметры")

def sum_odd_pos_elements(list_in):
    sum = 0
    for i in range(0, len(list_in), 2):
        sum += list_in[i]
    return sum

num_list = create_list(int(input("Задайте количество элементов в списке:\n")), int(input("Задайте начало диапазона случайных чисел для элементов в списке:\n")), int(input("Задайте конец диапазона случайных чисел для элементов в списке:\n")))
print(f"Созданный список:\n{num_list}")
print(f"Сумма элементов на нечётных позициях списка:\n{sum_odd_pos_elements(num_list)}")


import os
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets
from utils.cli import get_argument
from utils.model import NN
from utils.make_dataset import LoadData, get_json

def training(
    model,
    criterion,
    optimizer,
    featureloader,
    labelloader,
    epochs=15,
    print_every=40
):
    epoch_loss = 0
    steps = 0

    for epoch in range(epochs):
        for features, labels in zip(featureloader, labelloader):
            steps += 1
            features = features.type(torch.FloatTensor)
            labels = labels.type(torch.FloatTensor)
            labels.resize_(labels.shape[0], 1)

            output = model.forward(features)
            loss = criterion(output, labels)

            optimizer.zero_grad()
            loss.backward()
            optimizer.step()

            epoch_loss += loss.item()

            if steps % print_every == 0:
                print('Epoch: {}/{}..'.format(epoch, epochs),
                      'Training loss: {:.3f}'.format(epoch_loss/print_every))

                epoch_loss = 0

def save_model(
    model,
    epochs,
    optimizer,
    scaler,
    filename='checkpoint.pth',
    save_dir='reports'
):
    checkpoint = {
        'model': model,
        'model_state': model.state_dict(),
        'epochs': epochs,
        'optim': optimizer,
        'optim_state': optimizer.state_dict(),
        'scaler': scaler
    }
    torch.save(checkpoint, os.path.join(save_dir, filename))
    print('[INFO] model is saved!')

if __name__ == '__main__':
    argument = get_argument()
    feature_name = get_json('references/col_to_feat.json')
    train = LoadData(argument.data, names=feature_name, sep='\s+')
    scaled_train, scaler = train.standardize()
    # convert dataset into tensor
    featureset = torch.from_numpy(scaled_train[:, 2:])
    labelset = torch.from_numpy(train.target)
    # define data loader
    featureloader = torch.utils.data.DataLoader(featureset, batch_size=32)
    labelloader = torch.utils.data.DataLoader(labelset, batch_size=32)
    # define the model
    model = NN(hidden_sizes=argument.hidden_units, drop_p=argument.drop_p)
    # define loss function and optimizer
    criterion = nn.MSELoss()
    optimizer = optim.RMSprop(model.parameters(), lr=argument.learning_rate)
    # train the model
    training(
        model, criterion, optimizer,
        featureloader, labelloader,
        epochs=argument.epochs
    )
    # save the trained model
    save_model(
        model, argument.epochs, optimizer, scaler,
        filename=argument.save_name, save_dir=argument.save_dir
    )


from collections import deque
import sys

n, m = map(int, sys.stdin.readline().split()) # n: 가로 크기/ m: 세로 크기

field = []
# field = [list(sys.stdin.readline().strip()) for _ in range(m)]
for i in range(m):
    field.append(list(input()))

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

result_W = 0
result_B = 0

def solution(x, y, color):
    cnt = 0

    queue = deque()
    queue.append((x, y))

    field[x][y] == 0

    while queue:
        x, y = queue.popleft()

        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]

            if nx < 0 or ny < 0 or nx >= m or ny >= n:
                continue

            if field[nx][ny] != 0 and field[nx][ny] == color:
                queue.append((nx, ny))
                field[nx][ny] = 0
                cnt += 1

    return (1 if cnt==0 else cnt)

for i in range(m):
    for j in range(n):
        if field[i][j] != 0:
            if field[i][j] == 'W':
                result_W += solution(i, j, 'W') ** 2

            elif field[i][j] == 'B':
                result_B += solution(i, j, 'B') ** 2

print(result_W, result_B)


# -*- coding:utf-8 -*-
# Time : 2019/10/05 上午 9:29
# Author : 御承扬
# e-mail:2923616405@qq.com
# project:  PyQt5
# File : CallWeatherWin.py
# @software: PyCharm

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from 综合应用.getCityWeather.WeatherWin import Ui_Form
import requests

class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.cityCode = None
        self.ui = Ui_Form()
        self.ui.setupUi(self)

    def queryWeather(self):
        print("* queryWeather")
        cityName = self.ui.weatherComboBox.currentText()
        self.transCityName(cityName)
        req = requests.get('http://weather.com.cn/data/sk/' + self.cityCode + '.html')
        req.encoding = 'utf-8'
        print(req.json())
        msg1 = '时间：%s' % req.json()['weatherinfo']['time'] + '\n'
        msg2 = '城市：%s' % req.json()['weatherinfo']['city'] + '\n'
        msg3 = '城市代码：%s' % req.json()['weatherinfo']['cityid'] + '\n'
        msg4 = '风向：%s' % req.json()['weatherinfo']['WD'] + '\n'
        msg5 = '温度：%s' % req.json()['weatherinfo']['temp'] + "度\n"
        msg6 = '风力：%s' % req.json()['weatherinfo']['WS'] + '\n'
        msg7 = '湿度：%s' % req.json()['weatherinfo']['SD'] + '\n'
        msg8 = '气压：%s' % req.json()['weatherinfo']['AP']
        result = msg1 + msg2 + msg3 + msg4 + msg5 + msg6 + msg7 + msg8
        self.ui.resultText.setText(result)

    def transCityName(self, cityName):
        if cityName == '陆丰':
            self.cityCode = '101282103'
        elif cityName == '北京':
            self.cityCode = '101010100'
        elif cityName == '天津':
            self.cityCode = '101030100'
        elif cityName == '上海':
            self.cityCode = '101020100'

    def clearResult(self):
        self.ui.resultText.clear()

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


#!/usr/bin/python

''' A script that tests the one_angle.py module.
    Run by: nosetests -v test_one_angle.py
'''

import one_angle as oa
import two_angle as ta
import radtran as rt
import numpy as np
from scipy.integrate import *
import pickle
import pdb

# loading the quadrrature set for calculations
N = 4
f = open('quad_dict.dat')
quadr_dict = pickle.load(f)
f.close()
quadr = quadr_dict[str(N)]
gauss_wt = quadr[:,2] / 8.
views = np.array(zip(quadr[:,0],quadr[:,1]))

def test_gl():
  '''A function to test to integration of the leaf angle
  distribution function gl() between 0 and pi/2 is unity.
  '''
  arch = ['p','e','s','m','x','u']
  g = []
  for a in arch:
    g.append(quad(rt.gl, 0., np.pi/2., args=(a))[0])
  np.testing.assert_almost_equal(g, 1.0, decimal=6)
  #return g

def test_one_angle_fluxes():
  '''A function that tests one_angle.py flux calculation.
  see table in Myneni 1988b for test data.
  '''
  test = oa.rt_layers(Tol=1.e-06,Iter=200,K=40,N=16,\
      Lc=4.,refl=0.175,trans=0.175,refl_s=1.e-16,I0=1.,\
      sun0=120.,arch='s')
  test.solve()
  c,s,a = test.Scalar_flux()
  val = np.array([c,s,a])
  truth = np.array([0.0988, 0.0383,\
      0.8628])
  #return (val, truth)
  np.testing.assert_almost_equal(val, truth, decimal=2)

def test_two_angle_fluxes():
  '''A function that tests two_angle.py flux calculation.
  see table in Myneni p.95 for test data.
  '''
  test = ta.rt_layers(Tol=1.e-06,Iter=200,K=20,N=4,\
      Lc=4.,refl=0.1,trans=0.1,refl_s=0.1,sun0_zen=180.,\
      F=np.pi,sun0_azi=0.0,arch='s',Beta=1.0)
  test.solve()
  c,s,a = test.Scalar_flux()
  val = np.array([c,s,a])
  truth = np.array([0.0356, 0.1360, 0.8284])
  #return (val, truth)
  np.testing.assert_almost_equal(val, truth, decimal=2)

def test_G2():
  '''A function to test the G-projection function for integration
  to 0.5 for the two angle case'''
  truth = 0.5
  arch = ['p','e','s','m','x','u']
  vals = []
  for a in arch:
    Gg = []
    for v in views:
      Gg.append(rt.G(v[0], a))
    val = np.sum(np.multiply(Gg, gauss_wt))
    vals.append(val)
  #return (vals, truth)
  np.testing.assert_almost_equal(vals, truth, decimal=2)

def test_dot():
  '''A function to test the dot product function used to
  calculate the cosine between 2 spherical angles.
  The dot product is used in the calculation of the
  Big_psi2 function.
  '''
  sun = (180. * np.pi / 180., 45. * np.pi / 180.)
  view = (90. * np.pi / 180., 45. * np.pi / 180.)
  truth = np.cos(np.pi/2)
  val = rt.dot(sun, view)
  #return (val, truth)
  np.testing.assert_almost_equal(val, truth, decimal=6)

def test_Big_psi2():
  '''A function to test the radtran.py Big_psi2 function against a
  dot integrated function using scipy integration solution based
  on Myneni 1988c eq(13).
  '''
  leaf_ze = 82. * np.pi / 180.
  view_ze = 25. * np.pi / 180.
  view_az = 63. * np.pi / 180.
  view = (view_ze, view_az)
  sun_ze = 74. * np.pi / 180.
  sun_az = 29. * np.pi / 180.
  sun = (sun_ze, sun_az)
  val = rt.Big_psi2(view, sun, leaf_ze)
  val = val[0] - val[1]
  def integ(leaf_az, leaf_ze, view_ze, view_az, sun_ze, sun_az):
    view = (view_ze, view_az)
    sun = (sun_ze, sun_az)
    dots = np.array([])
    for la in leaf_az:
      leaf = (leaf_ze, la)
      sun = (sun_ze, sun_az)
      view = (view_ze, view_az)
      dots = np.append(dots, rt.dot(leaf, sun) * rt.dot(leaf, view))
    return dots
  truth = fixed_quad(integ, 0, 2.*np.pi, args=(leaf_ze, view_ze,\
      view_az, sun_ze, sun_az))[0] / np.pi / 2.
  #return (val, truth)
  #pdb.set_trace()
  np.testing.assert_almost_equal(val, truth, decimal=3)

def test_Gamma2():
  '''A function that tests the radtran.py Gamma2 phase function
  for integration to albedo * G(sun) over the complete sphere.
  Based on Myneni 1988c eq(2).
  '''
  sun = (180./180. * np.pi, 45./180. * np.pi)
  arch = 'u'
  refl = 0.5
  trans = 0.5
  albedo = refl + trans
  truth = albedo * rt.G(sun[0],arch)
  Gam = []
  for v in views:
    Gam.append(rt.Gamma2(v, sun, arch, refl, trans))
  val = np.sum(np.multiply(Gam, gauss_wt)) * 16. / np.pi
  # the *16/pi is not part of the original text but makes the
  # code agree with the test. one would think that *4*pi would
  # be the factor to use.
  #return (val, truth)
  np.testing.assert_almost_equal(val, truth, decimal=2)

def test_Gamma_Gamma2():
  '''A function that tests the one and two angle Gamma functions
  against eachother. Gamma2 is intergrated over the azimuth and
  divided by 2*pi.
  '''
  view_ze = 25. * np.pi / 180.
  view_az = 63. * np.pi / 180.
  view = (view_ze, view_az)
  sun_ze = 74. * np.pi / 180.
  sun_az = 29. * np.pi / 180.
  sun = (sun_ze, sun_az)
  arch = 'u'
  refl = 0.5
  trans = 0.5
  g1 = rt.Gamma(view_ze, sun_ze, arch, refl, trans)
  def integ(view_az, view_ze, sun_az, sun_ze, arch, refl, trans):
    g = rt.Gamma2((view_ze, view_az), (sun_ze, sun_az), arch, refl,\
        trans)
    return g
  g2 = quad(integ, 0., 2.*np.pi, args=(view_ze, sun_az, sun_ze, arch,\
      refl, trans), limit=10, points=(np.pi/2.,))[0] / 2. / np.pi
  #return (g1, g2)
  np.testing.assert_almost_equal(g1, g2, decimal=2)

def test_Psi2_psi():
  '''A function that test the 2 angle Big_psi functions against
  each other. Based on Myneni 1988c (19).
  '''
  view_zen = 40.*np.pi/180.
  sun_zen = 15.*np.pi/180.
  sun_azi = 60.*np.pi/180.
  leaf_zen = 20.*np.pi/180.
  arch = 'u'
  refl = .5
  trans = .5
  #truth = rt.Big_psi(view_zen, sun_zen, leaf_zen, 't')
  truth = trans * np.cos(view_zen) * np.cos(sun_zen) * \
      np.cos(leaf_zen)**2.
  def fun(view_azi, view_zen, sun_azi, sun_zen, leaf_zen,\
      refl, trans):
    view = (view_zen, view_azi)
    sun = (sun_zen, sun_azi)
    f = rt.Big_psi2(view, sun, leaf_zen)
    f = f[0]*trans + f[1]*refl
    return f
  val = quad(fun, 0., 2.*np.pi, args=(view_zen, sun_azi, \
      sun_zen, leaf_zen, refl, trans))[0] / np.pi / 2.
  #return (val, truth)
  np.testing.assert_almost_equal(val, truth, decimal=2)

def test_P2():
  '''A function that tests the radtran.py P2 phase function for
  integration to unity over the complete sphere.
  '''
  sun = (0. * np.pi / 180. , 45.) # zenith, azimuth in radians
  arch = 'u'
  refl = 0.5
  trans = 0.5
  truth = 1.0
  p = []
  for v in views:
    p.append(rt.P2(v, sun, arch, refl, trans))
  p = np.sum(np.multiply(p, gauss_wt))
  # in the original text the term above needs to be divided by
  # 4 / pi. This does not work though for some reason.....
  #return (p, truth)
  np.testing.assert_almost_equal(p, truth, decimal=1)


import torch
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

taskids = 80
MCs = 10

df = pd.DataFrame(columns=["sigma_pert", "method", "mc",
                           "FDtrue_train", "FDhat_train", "Aq_f_train", "Aq_f_z_train", "Ap_f_train", "Ap_f_z_train",
                           "FDtrue_test", "FDhat_test", "Aq_f_test", "Aq_f_z_test", "Ap_f_test", "Ap_f_z_test"])

baseline_df = pd.DataFrame(columns=["sigma_pert", "method", "FDhat_train", "FDhat_test"])

for taskid in range(taskids):
    for mc in range(1,MCs+1):
        try:
            result = torch.load('taskid{}/result{}.pt'.format(taskid, mc), map_location=torch.device('cpu'))
            df.loc[len(df.index)] = list(result.values())
            baseline_df.loc[len(baseline_df.index)] = [result['sigma_pert'], 'truth', result['FDtrue_train'], result['FDtrue_test']]
        except:
            print('missing')

# just look at a subset of the results
methods_list = [
    ['nn_True_300_1.0', 'nn_True_30_1.0', 'nn_True_300_10.0', 'nn_True_30_10.0', 'sq_nn_True_300_1.0', 'sq_nn_True_30_1.0', 'sq_nn_True_300_10.0', 'sq_nn_True_30_10.0'],
    ['sq_nn_True_300_10.0', 'sq_nn_True_30_10.0', 'nn_True_30_10.0'],
    ['nn_False_300_1.0', 'nn_False_30_1.0', 'nn_False_300_10.0', 'nn_False_30_10.0', 'sq_nn_False_300_1.0', 'sq_nn_False_30_1.0', 'sq_nn_False_300_10.0', 'sq_nn_False_30_10.0'],
    ['sq_nn_False_300_10.0', 'sq_nn_False_30_10.0', 'nn_False_30_10.0'],
]

for methods in methods_list:

    temp = df.loc[df['method'].isin(methods)]

    i = 1
    for metric in ['FDhat_train', 'FDhat_test']:

        true = baseline_df.groupby(['sigma_pert', 'method'])[metric].mean()
        df2 = temp.groupby(['sigma_pert', 'method'])[metric].mean()
        df3 = pd.concat([true.unstack(), df2.unstack()], axis=1)

        print(df3)
        plt.subplot(1, 2, i)
        df3.plot(legend=False, ax=plt.gca(), yerr=temp.groupby(['sigma_pert', 'method'])[metric].std().unstack())
        plt.title('{}'.format(metric))
        i += 1

    # ax = plt.gca()
    # ax.set_aspect('equal')
    plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
    plt.tight_layout()
    plt.show()


# -*- coding: utf-8 -*-
# @Author  : Zhirong Tang
# @Time    : 2022/1/10 8:51 AM

from typing import List

class Solution:
    def minPathSum(self, grid: List[List[int]]):
        # method-1: DP
        # dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
        m, n = len(grid), len(grid[0])
        dp = [[0] * n for _ in range(m)]
        # boundary
        dp[0][0] = grid[0][0]
        for i in range(1, m):
            dp[i][0] = dp[i - 1][0] + grid[i][0]
        for j in range(1, n):
            dp[0][j] = dp[0][j - 1] + grid[0][j]

        for i in range(1, m):
            for j in range(1, n):
                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]
        return dp[m - 1][n - 1]

if __name__ == '__main__':
    obj = Solution()
    # grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]
    # grid = [[1, 2, 3], [4, 5, 6]]
    grid = [[1,3,1],[1,5,1],[4,2,1]]
    print(obj.minPathSum(grid))


#!/usr/bin/env python

from Bio import SeqIO
from collections import defaultdict
import re

def call_LOF(seq_record, peptide_length):
    peptide = str(seq_record.seq.ungap('-').translate()).split('*')[0]
    if len(peptide) < 0.8*peptide_length:
        return "LOF"
    else:
        return "full_length"

mtr_mosaic_dictionary = defaultdict(lambda: defaultdict(lambda: "NA"))

genes = ["mtrR", "mtrC", "mtrD", "mtrE"]
peptide_lengths = {"mtrR":211, "mtrC":413, "mtrD":1068, "mtrE":468}

for g in genes:
    for rec in SeqIO.parse(f"blast_results/{g}_gc.fa", "fasta"):
        rec.id = re.split('_\d+_length', rec.id)[0]
        lof = call_LOF(rec, peptide_lengths[g])
        if lof == "LOF":
            mtr_mosaic_dictionary[rec.id][g] = "LOF"
        else:
            mtr_mosaic_dictionary[rec.id][g] = "GC_allele"
    for rec in SeqIO.parse(f"blast_results/{g}_gc_mosaics.fa", "fasta"):
        rec.id = re.split('_\d+_length', rec.id)[0]
        lof = call_LOF(rec, peptide_lengths[g])
        if lof == "LOF":
            mtr_mosaic_dictionary[rec.id][g] = "LOF"
        else:
            mtr_mosaic_dictionary[rec.id][g] = "mosaic"
    for rec in SeqIO.parse(f"blast_results/{g}_gc_lengthOutliers.fa", "fasta"):
        rec.id = re.split('_\d+_length', rec.id)[0]
        lof = call_LOF(rec, peptide_lengths[g])
        if lof == "LOF":
            mtr_mosaic_dictionary[rec.id][g] = "LOF"
        else:
            mtr_mosaic_dictionary[rec.id][g] = "lengthOutlier"

mtr_promoter_dictionary = {}

for i,rec in enumerate(SeqIO.parse("blast_results/mtrPromoter_gc.fa", "fasta")):
    rec.seq = rec.seq.ungap('-')
    rec.id = re.split('_\d+_length', rec.id)[0]
    if "TAAAAAAG" in rec.seq and "CTTTTTA" in rec.seq and "AACCT" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "WT"
    elif "AATCT" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "Tpos120"
    elif "CTTTTA" in rec.seq or "CTTTTC" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "Adel_13bp_invertedRepeat"
    elif "TAAAAAAAAG" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "TTins_13bp_invertedRepeat"
    elif "TAAAAAAAG" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "Tins_13bp_invertedRepeat"
    elif "TAAAAAG" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "Tdel_13bp_invertedRepeat"
    elif "CTTTGTA" in rec.seq:
        mtr_promoter_dictionary[rec.id] = "AtoC_13bp_invertedRepeat"
    else:
        print(rec.id)

with open("resistance/mtr_alleles.tsv", "w") as outfile:
    outfile.write("wgs_id\tmtrPromoter\tmtrR\tmtrC\tmtrD\tmtrE\n")
    for sample in mtr_mosaic_dictionary:
        mtr_alleles = mtr_mosaic_dictionary[sample]
        if "mtrR" not in mtr_alleles:
            mtr_alleles["mtrR"] = "blast_hit_missing_possible_mosaic"
        alleles = [mtr_alleles[mtr_region] for mtr_region in genes]
        allele_string = "\t".join(alleles)
        if sample not in mtr_promoter_dictionary:
            mtr_promoter_dictionary[sample] = "divergent_promoter"
        outfile.write(f"{sample}\t{mtr_promoter_dictionary[sample]}\t{allele_string}\n")


#Singleton as class

class Singleton(object):
    _instance = None
    def __new__(self):
        if not self._instance:
            self._instance = super(Singleton, self).__new__(self)
        return self._instance

x = Singleton();
z = Singleton()
x.y = 20
x.r = 55
print(z.y)
z.y = 30
print(x.y)
print(z.r)
z.r = 66
print(x.r)


"""
    列表助手模块
"""

class ListHelper:
    """
        列表助手类
    """

    @staticmethod
    def find_all(list_target,func_condition):
        """
            通用的查找某个条件的所有元素方法
        :param list_target: 需要查找的列表
        :param func_condition: 需要查找的条件,函数类型
                函数名(参数) --> bool
        :return: 需要查找的元素,生成器类型.
        """
        for item in list_target:
            if func_condition(item):
                yield item

class ListHelper1:
    """
        列表助手类
    """
    @staticmethod
    def find_single(list_target, fun_condition):
        """
              通用的查找某个条件下元素方法
          :param list_target: 需要查找的列表
          :param func_condition: 需要查找的条件,函数类型
                  函数名(参数) --> bool
          :return: 需要查找的元素,生成器类型.
        """
        for item in list_target:
            if fun_condition(item):
                return item

    @staticmethod
    def find_lambda(list_target, fun_codition):
        """
            通用的查找某个条件下所有元素的数量方法
        :param list_target: 需要查找的列表
        :param func_condition: 需要查找的条件,函数类型
                函数名(参数) --> bool
        :return: 需要查找的元素,生成器类型.
        """
        count = 0
        for item in list_target:
            if fun_codition(item):
                count += 1
        return count

    @staticmethod
    def find_emeny(list_target, fun_condition):
        for item in list_target:
            if fun_condition(item):
                return True


import os
import json

class User:
    """
    This is a class for interact with user files only
    (ONLY SHOULD BE USED FOR DIRS)
    """
    def __init__(self, userid,dir):
        self.userid = str(userid)
        self.userDir = f"{dir}/U#{userid}"
        self.userRecipesDir = self.userDir + "/RecipesInfo"

    def createUserDirs(self):
        try:
            # Creation of user root dir
            os.mkdir(self.userDir)
            # Creation of recipes dir
            os.mkdir(self.userRecipesDir)
        except OSError:
            return False

    def createUserCategories(self):
        tags = ["Sopa", "Vegana", "Gourmet", "Postres", "Casero", "Tipica"]
        regions = ["latam","asia","norteA","europa","africa","oceania"]
        categoryFile = self.userDir+"/category.json"

        # Writes the category JSON File for a New User
        with open(categoryFile, "w") as categories:
            categoriesDict = {}
            for region in regions:
                categoriesDict[region] = {"timesVisited": 0}
                for tag in tags:
                    categoriesDict[region][tag] = 0

            json.dump(categoriesDict,categories,indent=4)

    def createUserChefs(self):
        chefsDir = self.userDir + "/chefs.json"
        with open(chefsDir, "w") as chefs:
            json.dump({}, chefs)

    def createUserRecipes(self):
        recommendedNotViewed = self.userRecipesDir + "/rnv.json"
        viewedRecipes = self.userRecipesDir + "/vw.json"

        with open(recommendedNotViewed,"w") as rnv, open(viewedRecipes, "w") as vw:
            json.dump({},rnv)
            json.dump({
                "avg_secs": 300,
                "last_sum": 0,
                "max": 0,
                "num": 0,
                "video_ids": []
            },vw,indent=4)

    def getUserDir(self):
        return os.path.exists(self.userDir)

    def initUser(self):
        if self.getUserDir() is False:
            self.createUserDirs()
            self.createUserChefs()
            self.createUserCategories()
            self.createUserRecipes()
            return True
        else:
            return False


"""

.. function:: rowidvt(query:None)

Returns the query input result adding rowid number of the result row.

:Returned table schema:
    Same as input query schema with addition of rowid column.

    - *rowid* int
        Input *query* result rowid.

Examples::

    >>> table1('''
    ... James   10	2
    ... Mark    7	3
    ... Lila    74	1
    ... ''')
    >>> sql("rowidvt select * from table1")
    rowid | a     | b  | c
    ----------------------
    1     | James | 10 | 2
    2     | Mark  | 7  | 3
    3     | Lila  | 74 | 1
    >>> sql("rowidvt select * from table1 order by c")
    rowid | a     | b  | c
    ----------------------
    1     | Lila  | 74 | 1
    2     | James | 10 | 2
    3     | Mark  | 7  | 3

    Note the difference with rowid table column.

    >>> sql("select rowid,* from table1 order by c")
    rowid | a     | b  | c
    ----------------------
    3     | Lila  | 74 | 1
    1     | James | 10 | 2
    2     | Mark  | 7  | 3
"""
import functions

import vtbase

### Classic stream iterator
registered = True

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass

    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass

    return False

class keep_numeric(vtbase.VT):

    def VTiter(self, *parsedArgs, **envars):
        largs, dictargs = self.full_parse(parsedArgs)

        self.nonames = True
        self.names = []
        self.types = []

        if 'query' not in dictargs:
            raise functions.OperatorError(__name__.rsplit('.')[-1], "No query argument ")
        query = dictargs['query']

        cur = envars['db'].cursor()
        c = cur.execute(query)
        schema = cur.getdescriptionsafe()
        schema1 = []

        first_row = c.next()
        first_tuple = []
        j = 0
        for i in first_row:
            if is_number(i):
                schema1.append(schema[j])
                first_tuple.append(i)
            j += 1

        yield tuple(schema1)
        yield tuple(first_tuple)

        for row in c:
            tmp_row = []
            j = 0
            for col in row:
                if schema[j] in schema1:
                    tmp_row.append(col)
                j += 1
            yield tmp_row

def Source():
    return vtbase.VTGenerator(keep_numeric)

if not ('.' in __name__):
    """
    This is needed to be able to test the function, put it at the end of every
    new function you create
    """
    import sys
    from functions import *

    testfunction()
    if __name__ == "__main__":
        reload(sys)
        sys.setdefaultencoding('utf-8')
        import doctest

        doctest.testmod()


#Python program to calculate perimeter and area of a rectangle
#stores user input
length = float(input('Enter the length:'))

width = float(input('Enter the width:'))

#outputs user input
print('A rectangle with a length of: %.1f \nAnd a width of: %.1f' %(length,width))
# calculates perimeter and area
per = 2*(length + width)
area = length * width
#outputs calculated results
print('Has a perimeter of: %.1f \nAnd an area of:%.1f' %(per,area))


import cv2 as cv

img = cv.imread("miss.jpg")

# 访问和修改像素值
px = img[100, 100]
print(px)
# 仅仅访问蓝色
blue = img[100, 100, 0]
print(blue)

# 修改像素值
img[100, 100] = [0, 0,0]

# 以上方法访问修改像素值太慢了

# 优化方案
red = img.item(10, 10, 2)
print(red)
img.itemset((10, 10, 2), 255)

# 图像的属性包含了行数 列数 通道数  图像数据类型   像素值
print(img.shape) # 返回的是行数 列数 通道数
print("image size 像素总数", img.size)
print(img.dtype)

# 图像数据感兴趣ROI

ball = img[280:340, 330: 390]
# print(ball)

print("==="* 40)
# 拆分和合并图像通道
b,g,r =  cv.split(img)

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
BLUE = [255,0,0]
img1 = cv.imread('opencv-logo.png')
replicate = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REPLICATE)
reflect = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT)
reflect101 = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_REFLECT_101)
wrap = cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_WRAP)
constant= cv.copyMakeBorder(img1,10,10,10,10,cv.BORDER_CONSTANT,value=BLUE)
plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.show()


'''
@ author: Amal Joseph Varghese
@ email: amaljova@gmail.com
@ github: https://github.com/amaljova
@ date:
'''

import pandas as pd

def getMissingSlices(df: pd.DataFrame):
    present = set(df.InstanceNumber)
    expected = set(range(1, int(df.InstanceNumber.max()) +1 ))
    absent = list(expected - present)
    absent.sort()
    return absent


import work_with_user

def main():
    """
    MAIN function!
    """
    flag = True
    print("\nHello!\n"
          'This is 8-puzzle python script')
    while flag:
        print('This script has 4-modes to work\n'
              '[1] -> from CLI to CLI\n'
              '[2] -> from CLI to FILE\n'
              '[3] -> from FILE to CLI\n'
              '[4] -> from FILE to FILE\n'
              '[5] -> play GUI version\n')
        try:
            mode = int(input('Input mode: '))
        except TypeError:
            raise TypeError("Error Input")
        if mode > 6 or mode < 1:
            raise Exception("Mode should be 1, 2, 3, 4 or 5")
        if mode == 1:
            work_with_user.cli_to_cli()
        if mode == 2:
            work_with_user.cli_to_file()
        if mode == 3:
            work_with_user.file_to_cli()
        if mode == 4:
            work_with_user.file_to_file()
        if mode == 5:
            work_with_user.GUI_with_tips()

        again = input("\nwant to continue ?\n"
                      "y/n: ")
        if again == 'y':
            flag = True
        if again == 'n':
            flag = False

if __name__ == "__main__":
    main()


import os
import time
import uuid
import yaml
import boto3

def before_all(context):
    context.uuid = uuid.uuid1().hex
    context.project_code = "sceptre-integration-tests-{0}".format(
        context.uuid
    )

    sts = boto3.client("sts")
    account_number = sts.get_caller_identity()['Account']
    context.bucket_name = "sceptre-integration-tests-templates-{}".format(
        account_number
    )

    context.sceptre_dir = os.path.join(
        os.getcwd(), "integration-tests", "sceptre-project"
    )
    update_config(context)
    context.cloudformation = boto3.resource('cloudformation')
    context.client = boto3.client("cloudformation")

    config_path = os.path.join(
        context.sceptre_dir, "config", "9/B" + ".yaml"
    )

    with open(config_path, "r") as file:
        file_data = file.read()

    file_data = file_data.replace("{project_code}", context.project_code)

    with open(config_path, "w") as file:
        file.write(file_data)

def before_scenario(context, scenario):
    os.environ.pop("AWS_REGION", None)
    os.environ.pop("AWS_CONFIG_FILE", None)
    context.error = None
    context.response = None
    context.output = None

def update_config(context):
    config_path = os.path.join(
        context.sceptre_dir, "config", "config.yaml"
    )
    with open(config_path) as config_file:
        stack_group_config = yaml.safe_load(config_file)

    stack_group_config["template_bucket_name"] = context.bucket_name
    stack_group_config["project_code"] = context.project_code

    with open(config_path, 'w') as config_file:
        yaml.safe_dump(
            stack_group_config, config_file, default_flow_style=False
        )

def after_all(context):
    response = context.client.describe_stacks()
    for stack in response["Stacks"]:
        if stack["StackName"].startswith(context.project_code):
            context.client.delete_stack(
                StackName=stack["StackName"]
            )
            time.sleep(2)
    context.project_code = "sceptre-integration-tests"
    context.bucket_name = "sceptre-integration-tests-templates"
    update_config(context)


# Sub-string divisibility
# Myyura

# The number, 1406357289, is a 0 to 9 pandigital number because it is
# made up of each of the digits 0 to 9 in some order, but it also has
# a rather interesting sub-string divisibility property.

# Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note the following:

# d2d3d4=406 is divisible by 2
# d3d4d5=063 is divisible by 3
# d4d5d6=635 is divisible by 5
# d5d6d7=357 is divisible by 7
# d6d7d8=572 is divisible by 11
# d7d8d9=728 is divisible by 13
# d8d9d10=289 is divisible by 17
# Find the sum of all 0 to 9 pandigital numbers with this property.

def next_permutation(s):
    s_copy = list(s)
    k = -1
    for i in range(len(s_copy) - 1):
        if s_copy[i] < s_copy[i + 1]:
            k = i

    if k == -1:
        return s_copy[::-1]

    j = -1
    for i in range(k + 1, len(s_copy)):
        if s_copy[i] > s_copy[k]:
            j = i

    s_copy[k], s_copy[j] = s_copy[j], s_copy[k]
    return s_copy[0:k + 1] + s_copy[len(s_copy) - 1:k:-1]

def sum_substr_divisibility():
    n = list("1023456789")
    primes = [2, 3, 5, 7, 11, 13, 17]
    sum = 0
    while n != list("0123456789"):
        flag = True
        for i in range(1, 8):
            if int("".join(n[i:i + 3])) % primes[i - 1] != 0:
                flag = False
                break
        if flag:
            sum += int("".join(n))

        n = next_permutation(n)

    return sum

print(sum_substr_divisibility())


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

# # **Proyecto final Data Science - Coderhouse - Juan Ignacio Szurlewicz**

# ## Título: Wheather prediction in Seattle

# El objetivo del presente proyecto es obtener mediante un set de datos de la ciudad de Seattle y un algoritmo de clasificación, un modelo que nos permita pronosticar de manera categórica el clima futuro, contemplando un conjunto de variables observadas.Título: Wheather prediction on Seattle

# El set de datos empleado se encuentra en http://kaggle.com, en el siguiente link :
# https://www.kaggle.com/datasets/ananthr1/weather-prediction

# # Data Acquisition

# Para la realización de la etapa de Data Acquisition se procedió a la búsqueda de un dataset que a travéz de variables claves, nos permita llegar a nuestra variable target propuesta. En este sentido, nos encontramos con el dataset de variables climatologicas de la ciudad de Seattle. El mismo, se encontraba en formato .CSV, es decir, de texto en columnas, clasificandose como un tipo de dato estructurado.

# Para la lectura del archivo utilizamos la libreria pandas que, a travéz del comando read_csv nos permite la carga de este tipo de archivos planos.

# In[355]:

import pandas as pd
import datetime as dt

# In[356]:

_df=pd.read_csv(r"C:\Users\Novix\Downloads\seattle-weather.csv")

# In[357]:

_df.head()

# Así, pudimos completar la lectura de nuestro dataset y a través del comando Head obtener los primeros 5 valores del conjunto de datos y observar, cuáles son nuestras variables consideradas. El siguiente paso es ir hacia el Data Wrangling, mediante el cuál haremos un descubrimiento mayor del conjunto de datos, su limpieza, estructuración y validación.

# # Data Wrangling

# La etapa del datawrangling consiste en la manipulación de los datos obtenidos en la etapa previa, de forma tal que sean más amigables y adecuados para su trabajo y para el logro del objetivo final. Dentro de esta etapa, iremos abarcando las distintas tareas del descubrimiento, estructuración, limpieza, enriquecimiento, validación y publicación.

# Comenzamos con la lectura del dataframe:

# In[358]:

_df.head() #Aquí identificamos a primera vista las variables que componen el set de datos

# In[359]:

_df.shape #Vemos el tamaño de nuestro dataset

# In[360]:

_df.isna().sum() #Analizamos si tenemos algun missing value en el set de datos

# In[361]:

_df.describe().T  #Obtenemos una visión general de las variables, con sus principales estadísticas descriptivas

# In[362]:

_df.dtypes #Aquí podemos ver los tipos de datos, vamos a pasar la variables date a datetime. El resto están bien asignadas

# In[363]:

_df['date'] = pd.to_datetime(_df['date'])

# In[364]:

_df.dtypes #Quedaron los tipos corregidos

# # Exploratory Data Analysis

# El exploratory data analysis consiste en una etapa que tiene como finalidad la exploración del conjunto de variables, de forma univariada como así tambien de forma bivariada y multivariada. Debemos comprender que representa cada una de ellas, si existen casos atípicos que puedan influir la consistencia de nuestro modelo antes de aplicar el mismo. Se deben preparar los datos para que los mismos representen de la forma más fiel el eje del problema en cuestión. Para ello, se realizan analisis gráficos en una primera instancia y luego analisis de medidas descriptivas. Si es necesario, se deben decidir cursos de acción para solucionar problemas como la presencia de missing values y valores outliers.

# In[365]:

#Importamos las librerias necesarias
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.ticker import FormatStrFormatter
from plotly.subplots import make_subplots
import plotly.graph_objects as go
from seaborn import boxplot
from seaborn import violinplot
import numpy as np

# ### Análisis univariado de las variables consideradas

# #### 1) Wheater

# In[366]:

_df.weather.describe()

# In[367]:

sns.countplot(x=_df['weather'],palette='Spectral')

# In[368]:

frec = _df["weather"].value_counts()
Wheater_freq=pd.DataFrame(frec)
Wheater_freq=Wheater_freq.rename(columns={'weather':'Frecuencia Absoluta'})
Frec_abs_val = Wheater_freq["Frecuencia Absoluta"].values
acum = []
valor_acum = 0
for i in Frec_abs_val:
    valor_acum = valor_acum + i
    acum.append(valor_acum)

Wheater_freq["frec_abs_acum"] = acum
Wheater_freq["frec_rel_%"] = round(100 * Wheater_freq["Frecuencia Absoluta"]/len(_df.weather),4)
Frec_rel_val = Wheater_freq["frec_rel_%"].values
acum = []
valor_acum = 0
for i in Frec_rel_val:
    valor_acum = valor_acum + i
    acum.append(valor_acum)

Wheater_freq["frec_rel_%_acum"] = acum
Wheater_freq

# Por medio de la tabla de frecuencias podemos ver que entre la condición climática "lluvia" y "soleado" se presenta en casi 90% de los casos.

# #### 2) Precipitation

# Esta variable se encuentra en pulgadas

# In[369]:

_df.precipitation.describe().T

# In[370]:

boxplot(x=_df.precipitation, orient="Vertical")

# Como podemos observar, encontramos valores outliers superiores, pero lo atribuimos a la naturaleza de la variable precipitación, ya que se trata de un fenómeno natural que puede presentar eventos disruptivos que no siguen una distribución normal.

# In[371]:

sns.histplot(data=_df.precipitation,kde=True)
plt.xlim(0,50)

# Podemos ver una clara asimetria hacia la derecha

# In[372]:

frec = _df["precipitation"].value_counts()
precipitation_freq=pd.DataFrame(frec)
precipitation_freq=precipitation_freq.rename(columns={'precipitation':'Frecuencia Absoluta'})
Frec_abs_val = precipitation_freq["Frecuencia Absoluta"].values
acum = []
valor_acum = 0
for i in Frec_abs_val:
    valor_acum = valor_acum + i
    acum.append(valor_acum)

precipitation_freq["frec_abs_acum"] = acum
precipitation_freq["frec_rel_%"] = round(100 * precipitation_freq["Frecuencia Absoluta"]/len(_df.precipitation),4)
Frec_rel_val = precipitation_freq["frec_rel_%"].values
acum = []
valor_acum = 0
for i in Frec_rel_val:
    valor_acum = valor_acum + i
    acum.append(valor_acum)

precipitation_freq["frec_rel_%_acum"] = acum
precipitation_freq.sort_values(by='Frecuencia Absoluta',ascending=False)

# In[373]:

precipitation_freq.head()  #Los primeros 5 valores en la distribución de frecuencia absoluta

# In[374]:

precipitation_freq.tail()   #Los últimos 5 valores en la distribución de frecuencia absoluta

# ### 3) Wind

# Esta variable se encuentra en millas por hora Mph

# In[375]:

_df.wind.describe()

# In[376]:

sns.histplot(data=_df.wind,kde=True)
plt.xlim(0,10)

# En este caso podemos ver que la distribución es prácticamente normal no estandar

# In[377]:

violinplot(x=_df.wind, data=_df , orient="Verical")

# Aquí podemos visualizar mejor la distribución de los datos, dónde el ancho de la figura indica la frecuencia de ocurrencia absoluta de los valores.

# ### 3) Temp_Max

# Esta variable se encuentra en grados celcius C°

# In[378]:

_df.temp_max.describe()

# In[379]:

sns.histplot(data=_df.temp_max,kde=True)
plt.xlim(0,36)

# En este caso tambien, la distribución es prácticamente normal, media 16.43 y desviación estándar +- 7.35, si observamos las estadísticas descriptivas, el 25% de la distribución se da en 10.6 (16.43-7.35=9) y el 75% es en 22.2 (16.43+7.35=23.78)

# ### 3) Temp_Min

# Esta variable se encuentra en grados celcius C°

# In[380]:

_df.temp_min.describe()

# In[381]:

sns.histplot(data=_df.temp_min,kde=True)
plt.xlim(0,18.3)

# Aquí la distribución ya no parece la de una normal. La temperatura mínima observada se encuentra distribuida de manera más uniforme

# In[382]:

violinplot(x=_df.temp_min, data=_df , orient="Verical")

# La densidad que representa la figura es más amplia respecto al eje x, con lo cuál, para cada temperatura mínima existe un número similar de ocurrencias

# ### Análisis bivariado de las variables consideradas

# In[383]:

plt.rcParams['figure.figsize'] = (10,8)

sns.heatmap(_df.corr(), annot = True, cmap = 'Wistia')
plt.title('Mapa de correlaciones', fontsize = 20)
plt.show()

# Analizamos mediante el mapa de correlaciones la relación lineal entre las variables cuantitativas bajo análisis.

# ### 1) Weather vs Wind

# In[384]:

sns.scatterplot(x="weather", y="wind", data=_df)
plt.title('Relacion entre Sex and target', fontsize = 20, fontweight = 30)

# Podemos ver que en el caso de la llovizna, el viento es débil. Luego, para el caso de las lluvias, la distribución es mas uniforme. Por otro lado, en el caso del clima soleado, suele darse con mayor concentración para velocidades de viento menores. Por último, para el caso de la nieve y la niebla, hay algunos puntos pero la relación es débil. La niebla es más probable con poco viento.

# In[385]:

_df.groupby('weather')[['wind']].mean()  #Medias dentro de cada tipo de clima

# ### 2) Weather vs Precipitation

# In[386]:

sns.scatterplot(x="weather", y="precipitation", data=_df)
plt.title('Relacion entre Sex and target', fontsize = 20, fontweight = 30)

# Aquí podemos observar que las precipitaciones sólo se dan en el clima de lluvia y nieve. No se detectan precipitaciones en climas de neblina o llovizna.

# In[387]:

_df.groupby('weather')[['precipitation']].mean()  #Medias dentro de cada tipo de clima

# ### 3) Weather vs Temp_min

# In[388]:

sns.scatterplot(x="weather", y="temp_min", data=_df)
plt.title('Relacion entre Sex and target', fontsize = 20, fontweight = 30)

# Podemos observar de forma gráfica que los climas de llovizna, lluvia y niebla con compatibles con temperaturas mínimas mas elevadas. En el caso del clima soleado se presente para todas las temperaturas mínimas observadas y en el caso de nieve, para casos de temperaturas mínimas bajas.

# In[389]:

_df.groupby('weather')[['temp_min']].mean()  #Medias dentro de cada tipo de clima, reforzamos el análisis gráfico.v

# ### 3) Weather vs Temp_max

# In[390]:

sns.scatterplot(x="weather", y="temp_max", data=_df)
plt.title('Relacion entre Sex and target', fontsize = 20, fontweight = 30)

# Podemos observar de forma gráfica que los climas de llovizna, lluvia y niebla con compatibles con temperaturas máximas mas elevadas. En el caso del clima soleado se presente para todas las temperaturas mínimas observadas y en el caso de nieve, para casos de temperaturas mínimas bajas.

# In[391]:

_df.groupby('weather')[['temp_max']].mean()  #Medias dentro de cada tipo de clima

# ### Análisis multivariado de las variables consideradas

# In[392]:

sns.FacetGrid(_df,hue = 'weather' , height = 5).map(plt.scatter,'temp_min','temp_max').add_legend();
plt.show()

# In[393]:

sns.FacetGrid(_df,hue = 'weather' , height = 5).map(plt.scatter,'precipitation','wind').add_legend();
plt.show()

# In[394]:

sns.FacetGrid(_df,hue = 'weather' , height = 5).map(plt.scatter,'wind','temp_max').add_legend();
plt.show()

# In[395]:

sns.FacetGrid(_df,hue = 'weather' , height = 5).map(plt.scatter,'wind','temp_min').add_legend();
plt.show()

# In[396]:

sns.FacetGrid(_df,hue = 'weather' , height = 5).map(plt.scatter,'precipitation','temp_min').add_legend();
plt.show()

# In[397]:

sns.FacetGrid(_df,hue = 'weather' , height = 5).map(plt.scatter,'precipitation','temp_max').add_legend();
plt.show()

# Podemos observar que existe sólo un claro patrón en el primer caso, de temperatura min vs temperatura máx. En el resto de los casos, no hay un patrón.

# In[398]:

plt.figure(dpi=120)
sns.pairplot(_df)
plt.show()

# Aquí podemos observar lo mismo que antes y sacar las mismas conclusiones, sin discriminar por tipo de clima.

# In[399]:

_df.groupby('weather')[['precipitation','wind','temp_min','temp_max']].mean().T

# Mediante la tabla descriptiva podemos analizar que:
#
# - **precipitation**: se presenta sólo en el clima de lluvia y nive, con una media de mm mayor en el último estado.
# - **wind**: se presenta en todos los estados, con mayor velocidad durante la nieve y climas soleados.
# - **temp_min**: se presenta en todos los estados
# - **temp_max**: se presenta en todos los estados

# In[400]:

_df['date']=pd.to_datetime(_df['date'])

# In[401]:

_df['date'] =_df['date'].dt.strftime('%m')

# In[402]:

_df.groupby('date')[['precipitation','wind','temp_min','temp_max']].mean().T

# Mediante dicha tabla podemos analizar el mes, tomando un promedio de los años analizados, con mayor media de cada variable. Entonces, Noviembre es el mes en el cuál mas lluvia hay, Diciembre es el mes con mas vientos, las temperaturas mínimas son mayores en Enero y las máximas en Agosto.

# ## Objetivo

# El objetivo principal del presente proyecto es obtener mediante un algoritmo de machine learning, un modelo que a travéz de los inputs analizados nos permita pronosticar el clima en la ciudad de Seattle. Nuestra variable target es entonces la variable "Weather" y las variables explicativas son las precipitaciones en pulgadas, la velocidad del viento en millas por hora y la temperatura mínima y máxima en grados celcius.

# In[ ]:


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import StratifiedKFold

#http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedKFold.html
#https://mclguide.readthedocs.io/en/latest/sklearn/cv.html

df = pd.read_csv(
    filepath_or_buffer='https://archive.ics.uci.edu/ml/machine-learning-databases/adult/adult.data',
    header=None,
    sep=',')

df.columns=['Idade', 'Emprego', 'Peso final', 'Educação', 'Anos de Estudo', 'Estado Civil', 'Ocupação', 'Relacionamento', 'Raça', 'Sexo', 'Ganhos de Capital', 'Perdas de Capital', 'Horas de trabalho por semana', 'País de Nascimento', 'classe']
df.dropna(how="all", inplace=True)

df2=df.drop(columns=['Emprego', 'Educação', 'Estado Civil', 'Ocupação', 'Relacionamento', 'Raça', 'Sexo', 'País de Nascimento'])

atributos=df2[['Idade', 'Peso final', 'Anos de Estudo', 'Ganhos de Capital', 'Perdas de Capital', 'Horas de trabalho por semana']]
classe=df2[['classe']]
X, y = atributos.values, classe.values

cv = StratifiedKFold(n_splits=3, shuffle=True)
cv.get_n_splits(X, y)

print(classe.size)
print(cv)

for train_index, test_index in cv.split(X, y):
	#print("TRAIN:", train_index, "TEST:", test_index)
	print("%s %s" % (train_index.size,test_index.size))


tp = ('zero','um','dois','tres','quatro',
      'cinco','seis','sete','oito','nove',
      'dez','onze','doze','treze','quatorze',
      'quinze','dezesseis','dezessete',
      'dezoito','dezenove','vinte')

while True:

    n = int(input('digite um numero entre 0 e 20:'))

    if 0 <= n <= 20:
        break
    print('Valor invalido, tente novamente.',end ='')

print (f'o numero que você digitou foi {tp[n]}')


# this script lets you write a file to drive

# drefine the directory and file name to write the file into
imageLocation=u'D:\\writetest.txt'

# create the file
file = open(imageLocation,'w')

# write some text into it
file.write('Hello, this works!')

# close the file
file.close()

print("file stored")


from tkinter import *
from PIL import ImageTk,Image
import requests
import yfinance as yf

def stk_img(ticker):
	stck=yf.Ticker(ticker)
	img_src=stck.info['logo_url']
	response = requests.get(img_src)
	file=open('stockimg.png','wb')
	file.write(response.content)
	file.close()
def comma(arg):
   i=0
   r=0
   end_r=""
   result=str(arg).strip()
   result=list(result)

   for x in result:

	   if r==-3 :

	    	result.insert(i, ",")

	   elif r < -3:
	    	r=0
	   i-=1
	   r-=1

   for y in result:
	   end_r+= y
   end_r += "$"
   return end_r
def discounted_cashf(exp_cf, perc, yr):
	    translation=""
	    perc=perc/100
	    result= int(exp_cf/(1+ perc)**yr)
	    return result
def investment(whole, percentage, time, contribution=0):
	contribution=int(contribution)
	whole=int(whole)
	time=int(time)
	percentage=float(percentage)
	percentage=percentage/100
	for x in range(time):
		whole=whole*(1+percentage)
		whole+=contribution
	return comma(int(whole))
class stck_db:
	def add(dta):
		data=open('log.txt','a')
		data.write(f"{dta.upper()} ")
		data.close()
	def r():
	    data=open('log.txt','r')
	    print(data.readlines())
	    data.close()
	def clear_all():
	    data=open('log.txt','w+')
	    data.write('')
	    data.close()
	data=open('log.txt','r')
	data_=''
	for x in data:
	    data_+=x
	data.close()
	current_data=data_.split()
def figures(answer):
	    counter=0
	    for x in answer:
	        if x==',':
	           counter+=1
	    if counter==1:
	       figure=f"{answer} Thousand"
	    elif counter==2:
	       figure=f"{answer} Million"
	    elif counter==3:
	       figure=f"{answer} Billion"
	    elif counter==4:
	       figure=f"{answer} Trillion"
	    return figure


import unittest

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class NewVisitorTest(unittest.TestCase):
    def setUp(self):
        self.browser = webdriver.PhantomJS()
        self.browser.implicitly_wait(2)

    def tearDown(self):
        self.browser.quit()

    def test_user_can_enter_url_and_get_snapshot(self):
        # Jane navigties to http://scrot.io/ in her web browser.
        self.browser.get('http://localhost:8000/')

        # She is presented with an input that will allow her to enter any url.
        self.assertIn('Scrot.io', self.browser.title)

        # She enters the URL http://google.com/ and hits enter
        input = self.browser.find_element_by_id('id_url')
        input.send_keys('http://google.com/')
        input.send_keys(Keys.ENTER)

        # Soon enough, she is redirected to a page that displays a
        # screenshot of the URL she requested.
        body = self.browser.find_element_by_tag_name('body')
        images = body.find_elements_by_tag_name('img')
        self.assertIn('full.png', images[0].get_attribute('src'))

if __name__ == '__main__':
    unittest.main()


"""JWT module.

"""
import datetime
import json
from time import time
from uuid import uuid4

from authlib.jose import jwt

from .config import JWT_ALGORITHM, JWT_ISSUER, JWT_SHARED_SECRET

def get_bytes_secret(hex_encoded_secret: str = str(JWT_SHARED_SECRET)) -> bytes:
    """Get bytes for configured hexadecimal encoded secret.

    """
    return bytes.fromhex(hex_encoded_secret)

def issue_jwt_for_user_and_date(
    user: str,
    date: datetime.date,
    issuer: str = str(JWT_ISSUER),
    timestamp: int = int(time()),
    secret: bytes = get_bytes_secret(),
) -> bytes:
    """Issue JWT for given user and date.

    """
    header = {"alg": JWT_ALGORITHM}
    payload_claim = json.dumps({"user": user, "date": date.isoformat()})
    payload = {
        "iss": issuer,
        "iat": timestamp,
        "jti": uuid4().hex,
        "payload": payload_claim,
    }
    return bytes(jwt.encode(header, payload, secret))


def swap_case(s):
    n=''
    for i in s:
        if i.isupper():
            n+=i.lower()
        else :
            n+=i.upper()
    return s.swapcase()

if __name__ == '__main__':
    s = input()
    result = swap_case(s)
    print(result)


from pymongo import MongoClient
from similarity import get_similarity

client = MongoClient()
db = client.mydb
content = db.content
similar_to=[]
temp = 0

def similarity_matcher(final_article,url):
    flag=0
    if content.count()==0:
        print("hello")
    else:
        for i in content.find():
            db_url=str(i['url'])
            article=str(i['article'])
            post_id=str(i["_id"])

            if db_url != url:
                sim_value=get_similarity(final_article,article)
                print (sim_value)
                if sim_value>=.3:
                    similar_to.append(post_id)
                    flag=1
            else:
                return 1

def insertion(category,url,final_article4):
    temp_2 = similarity_matcher(final_article4,url)

    if(temp_2==1):
        print ("Article already exists in database")
    else:
        post = {"category": category,
                "url": url,
                "article": final_article4,
                "similar to": similar_to}

        content.insert_one(post)