def solution(priorities, location):
    cnt = 1
    while 1:
        if priorities[0] == max(priorities):
            priorities.pop(0)
            if location == 0:
                return cnt
            else:
                cnt += 1
        else:
            priorities.append(priorities.pop(0))
        location += -1 if location else len(priorities)-1


import collections

import arviz as az
import matplotlib.colors
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.collections import LineCollection
from matplotlib.colors import LinearSegmentedColormap

# Create our own custom color maps to transition from black to the color of interest
cmap_C0 = LinearSegmentedColormap.from_list("blue", ["k", "C0"])
cmap_C1 = LinearSegmentedColormap.from_list("orange", ["k", "C1"])

def plot_cline(ax, x, y, dates, lw=1.0, primary=True):
    """
    Plot the orbit shaded by phase.

    Args:
        ax: matplotlib axes to plot onto
        x: the matplotlib x values (i.e., the Y astro coordinates)
        y: the matplotlib y values (i.e., the X astro coordinates)
        dates: the dates corresponding to each (x,y) value
        lw: the linewidth of the orbit
        primary: if True, plot in blue, else plot orange.

    Returns:
        None

    Example

            plot_cline(ax_orbit, Y_A, X_A, dates, primary=True)
            plot_cline(ax_orbit, Y_B, X_B, dates, primary=False)

    """

    cmap = cmap_C0 if primary else cmap_C1
    points = np.array([x, y]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)

    lc = LineCollection(
        segments, cmap=cmap, norm=plt.Normalize(np.min(dates), np.max(dates))
    )
    lc.set_array(dates)
    lc.set_linewidth(lw)
    ax.add_collection(lc)

def plot_nodes(ax, Omega, radius):
    """
    Plot the line of nodes on the sky plot. The ascending node half is red, while the
    descending node half is blue.

    Args:
        ax: the matplotlib axes to plot onto
        Omega: the position angle of the ascending node (in radians)
        radius: how far away from the center to draw the line

    Returns:
        None
    """

    # calculate the slope of the line from Omega

    x_a = radius * np.sin(Omega)
    y_a = radius * np.cos(Omega)

    x_d = radius * np.sin(Omega + np.pi)
    y_d = radius * np.cos(Omega + np.pi)

    # plot in two segments
    ax.plot([0, x_a], [0, y_a], color="r")
    ax.plot([0, x_d], [0, y_d], color="b")

def plot_errorbar(ax, thetas, rhos, theta_errs, rho_errs, **kwargs):
    """
    Custom routine for plotting polar error bars onto cartesian plane
    All kwargs sent to plot. Thetas are in radians.
    """

    thetas = np.atleast_1d(thetas)
    rhos = np.atleast_1d(rhos)
    theta_errs = np.atleast_1d(theta_errs)
    rho_errs = np.atleast_1d(rho_errs)

    assert len(thetas) == len(rhos) == len(theta_errs) == len(rho_errs)

    for theta, rho, theta_err, rho_err in zip(thetas, rhos, theta_errs, rho_errs):

        # In this notation, x has been flipped.
        # x = rho * np.sin(theta)
        # y = rho * np.cos(theta)

        # Calculate two lines, one for theta_err, one for rho_err

        # theta error
        # theta error needs a small correction in the rho direction in order to go right through the data point
        delta = rho * (1 / np.cos(theta_err) - 1)
        xs = (rho + delta) * np.sin(np.array([theta - theta_err, theta + theta_err]))
        ys = (rho + delta) * np.cos(np.array([theta - theta_err, theta + theta_err]))
        ax.plot(xs, ys, **kwargs)

        # rho error
        xs = np.sin(theta) * np.array([(rho - rho_err), (rho + rho_err)])
        ys = np.cos(theta) * np.array([(rho - rho_err), (rho + rho_err)])
        ax.plot(xs, ys, **kwargs)

def efficient_trace(trace, var_names, figstem, max_panel=6):
    """
    Wrap the arviz call for trace to speed things up by splitting into smaller figures bundled as a scrollable PDF.
    """

    N = len(var_names)
    n = 0
    i = 0
    while n <= N:
        print("Plotting {:} of {:}".format(n, N))
        plot_vars = var_names[n : n + max_panel]

        az.plot_trace(trace, var_names=plot_vars)
        plt.savefig(figstem.format(i))
        plt.close("all")
        n += max_panel
        i += 1

def efficient_autocorr(trace, var_names, figstem, max_panel=6):
    """
    Wrap the arviz call for correlation to speed things up by splitting into smaller figures bundled as a scrollable PDF.
    """

    N = len(var_names)
    n = 0
    i = 0
    while n <= N:
        plot_vars = var_names[n : n + max_panel]

        az.plot_autocorr(trace, var_names=plot_vars)
        plt.savefig(figstem.format(i))
        plt.close("all")
        n += max_panel
        i += 1


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import tempfile
import base64

from fuel.datasets import IndexableDataset
from fuel.streams import DataStream

from dictlearn.data import (
    LanguageModellingData, ExtractiveQAData, RandomSpanScheme)
from dictlearn.vocab import Vocabulary
from dictlearn.util import vec2str

from tests.util import TEST_TEXT, TEST_SQUAD_BASE64_HDF5_DATA

def test_languge_modelling_data():
    temp_dir = tempfile.mkdtemp()
    train_path = os.path.join(temp_dir, "train.txt")
    with open(train_path, 'w') as dst:
        print(TEST_TEXT, file=dst)

    data = LanguageModellingData(temp_dir, 'standard')

    # test without batches
    stream = data.get_stream('train')
    it = stream.get_epoch_iterator()
    example = next(it)
    # skip one
    example = next(it)
    assert len(example) == 1
    assert len(example[0]) == 4
    assert example[0][0][:5].tolist() == map(ord, '<bos>')
    assert example[0][1][:5].tolist() == [ord('d'), ord('e'), ord('f'), 0, 0]
    assert example[0][2][:5].tolist() == [ord('d'), ord('e'), ord('f'), 0, 0]
    assert example[0][3][:5].tolist() == [ord('x'), ord('y'), ord('z'), 0, 0]

    # test with batches
    stream = data.get_stream('train', batch_size=2)
    it = stream.get_epoch_iterator()
    example = next(it)
    # skip one
    example = next(it)
    assert len(example) == 2
    assert example[0].shape == (2, 4, 100)
    assert example[1].shape == (2, 4)
    assert example[0][1, 2, :5].tolist() == [ord('d'), ord('e'), ord('f'), 0, 0]
    assert example[1].tolist() == [[1., 1., 0., 0.], [1., 1., 1., 1.]]

    os.remove(train_path)
    os.rmdir(temp_dir)

def test_squad_data():
    temp_dir = tempfile.mkdtemp()
    train_path = os.path.join(temp_dir, 'train.h5')
    with open(train_path, 'wb') as dst:
        print(base64.b64decode(TEST_SQUAD_BASE64_HDF5_DATA), file=dst)

    data = ExtractiveQAData(path=temp_dir, layout='squad')
    stream = data.get_stream('train', batch_size=3, shuffle=True, seed=3)
    assert set(stream.sources) == set(['contexts', 'contexts_mask',
                                       'questions', 'questions_mask',
                                       'answer_begins',
                                       'answer_ends'])
    batch = next(stream.get_epoch_iterator(as_dict=True))
    assert batch['contexts'].ndim == 2
    assert batch['contexts_mask'].ndim == 2
    assert batch['questions'].ndim == 2
    assert batch['questions_mask'].ndim == 2
    assert batch['answer_begins'].tolist() == [45, 78, 117]
    assert batch['answer_ends'].tolist() == [46, 80, 118]

    longest = batch['contexts_mask'].sum(axis=1).argmax()
    assert batch['contexts'][longest][-1] == data.vocab.eos

def test_random_span_scheme():
    scheme = RandomSpanScheme(10000, 100, 1)
    req_it = scheme.get_request_iterator()
    assert next(req_it) == slice(235, 335, None)

    dataset = IndexableDataset(['abc', 'def', 'xyz', 'ter'])
    scheme = RandomSpanScheme(4, 2, 1)
    stream = DataStream(dataset, iteration_scheme=scheme)
    it = stream.get_epoch_iterator()
    assert next(it) == (['def', 'xyz'],)


"""
citations
"""

import logging

import pickleshare
import requests
from pathlib import Path

log = logging.getLogger(__name__)

# silence request logging
logging.getLogger("requests.packages.urllib3.connectionpool").setLevel(
    logging.WARNING)

def get_cache(cache_dir):
    # The reason to use pickleshare is that it uses a separate pickle file
    # for each key (i.e. doi), so if the process crashes, at least most of the
    # looked up citations/metadata is saved to file
    cache_path = Path(cache_dir)
    cache_path.mkdir(parents=True, exist_ok=True)
    log.info('reading cached data from ' + cache_dir)
    cache = pickleshare.PickleShareDB(cache_dir)
    return cache

def get_citation(doi, cache,
                 style='chicago-fullnote-bibliography',
                 strip_doi=True, online=True):
    """
    Get formatted citation string for DOI from CrossRef

    Parameters
    ----------
    doi
    cache
    style
    strip_doi
    online

    Returns
    -------

    """
    try:
        return cache[doi]
    except KeyError:
        pass

    if not online:
        log.warning('skipping online lookup of citation for DOI {}'.format(doi))
        return ''

    headers = {'Accept': 'text/bibliography; style={}'.format(style)}
    attempts = 10
    response = None

    for i in range(attempts):
        response = requests.get('http://dx.doi.org/{}'.format(doi), headers=headers)
        if response.ok:
            break
    else:
        if response:
            log.error('request for formated citation of {} returned {}: {}'.format(
                doi, response.status_code, response.reason))
        else:
            log.error('request for formated citation of {} failed'.format(doi))
        return ''

    log.info('request for formated citation of {} succeeded'.format(doi))

    citation = response.content.decode('utf-8')

    if strip_doi:
        # TODO 3: won't work for other styles
        citation = citation.split(' doi:')[0]

    cache[doi] = citation
    return citation

def get_all_metadata(doi, cache, online=True):
    """
    Get metadata for DOI, including metadata from ISSN
    """
    metadata = get_doi_metadata(doi, cache, online)
    issn = metadata['ISSN']
    if issn:
        issn_metadata = get_issn_metadata(issn, cache, online)
        metadata.update(issn_metadata)
    return metadata

def get_doi_metadata(doi, cache, online=True):
    """
    Get metadata for DOI
    """
    doi_metadata = request_doi_metadata(doi, cache, online=online)
    metadata = {'doi': doi}

    for key in 'title', 'publisher':
        try:
            metadata[key] = doi_metadata[key]
        except KeyError:
            metadata[key] = None
            log.warning('no {} found for DOI {}'.format(key, doi))

    try:
        metadata['ISSN'] = doi_metadata['ISSN'][0]
    except (KeyError, IndexError):
        metadata['ISSN'] = None
        log.warning('no ISSN found for DOI {}'.format(doi))

    try:
        metadata['journal'] = doi_metadata['container-title']
    except KeyError:
        metadata['journal'] = None
        log.warning('no journal (container-title) found for DOI {}'.format(doi))

    # example fragment:
    # 'published-online': {'date-parts': [[2009, 8, 30]]},
    # 'published-print': {'date-parts': [[1998, 1, 22]]}
    published = (doi_metadata.get('published-online') or
                 doi_metadata.get('published-print') or
                 doi_metadata.get('issued'))

    try:
        parts = published['date-parts'][0]
    except (TypeError, KeyError, IndexError):
        parts = []

    try:
        metadata['year'] = parts[0]
    except IndexError:
        metadata['year'] = None
        log.warning('no publication date found for DOI {}'.format(doi))

    try:
        metadata['month'] = parts[1]
    except IndexError:
        metadata['month'] = None

    try:
        metadata['day'] = parts[2]
    except IndexError:
        metadata['day'] = None

    return metadata

def request_doi_metadata(doi, cache, attempts=10, online=True):
    """
    Request metadata for DOI from CrossRef
    """
    try:
        return cache[doi]
    except KeyError:
        pass

    if not online:
        log.warning('skipping online lookup for DOI {}'.format(doi))
        return {}

    headers = {'Accept': 'application/vnd.citationstyles.csl+json'}
    response = None

    for i in range(attempts):
        response = requests.get('http://dx.doi.org/{}'.format(doi),
                                headers=headers)
        if response.ok:
            break
    else:
        if response:
            log.error('request for metadata of DOI {} returned {}: {}'.format(
                doi, response.status_code, response.reason))
        else:
            log.error('request for metadata of DOI {} failed'.format(doi))
        return {}

    log.info('request for metadata of DOI {} succeeded'.format(doi))
    metadata = response.json()
    cache[doi] = metadata
    return metadata

def get_issn_metadata(issn, cache, online=True):
    """
    Get metadata for ISSN
    """
    issn_metadata = request_issn_metadata(issn, cache, online=online)
    metadata = {}

    try:
        metadata['journal'] = issn_metadata['title']
    except KeyError:
        pass

    try:
        metadata['publisher'] = issn_metadata['publisher']
    except KeyError:
        pass

    return metadata

def request_issn_metadata(issn, cache, attempts=10, online=True):
    """
    Request metadata for ISSN from CrossRef
    """
    try:
        return cache[issn]
    except KeyError:
        pass

    if not online:
        log.warning('skipping online lookup for ISSN {}'.format(issn))
        return {}

    response = None

    for i in range(attempts):
        response = requests.get(
            'http://api.crossref.org/journals/{}'.format(issn))
        if response.ok:
            break
    else:
        if response:
            log.error('request for metadata of ISSN {} returned {}: {}'.format(
                issn, response.status_code, response.reason))
        else:
            log.error('request for metadata of ISSN {} failed'.format(issn))

        return {}

    log.info('request for metadata of ISSN {} succeeded'.format(issn))
    message = response.json()['message']
    cache[issn] = message
    return message

def clean_metadata_cache(cache_dir):
    """
    Remove records with None values from metadata cache

    This means that on the next call to add_metadata(),
    new metadata will be requested for the removed records.
    """
    log.info('cleaning cached metadata from ' + cache_dir)
    cache = pickleshare.PickleShareDB(cache_dir)
    to_delete = []

    for key in cache.keys():
        func = get_doi_metadata if '/' in key else get_issn_metadata
        metadata = func(key, cache)
        if None in metadata.values():
            to_delete.append(key)

    for key in to_delete:
        log.info('removing incomplete cached metadata for key {}'.format(key))
        del cache[key]


import os
import tkinter as tk
from tkinter import filedialog
from tkinter import ttk
import time

def delete_files_with_extension(folder_path, extension, callback):
    file_count = 0
    total_size = 0

    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith(extension):
                file_path = os.path.join(root, file)
                file_size = os.path.getsize(file_path)
                total_size += file_size
                file_count += 1
                os.remove(file_path)
                callback(file_count, total_size)

def browse_folder():
    folder_path = filedialog.askdirectory()
    folder_entry.delete(0, tk.END)
    folder_entry.insert(tk.END, folder_path)

def delete_files():
    folder_path = folder_entry.get()
    extension = extension_entry.get()

    progress_label.config(text="Deleting files...")
    delete_button.config(state=tk.DISABLED)

    def update_progress(file_count, total_size):
        progress_label.config(text=f"Files deleted: {file_count}\nSpace recovered: {total_size} bytes")
        progress_bar["value"] = file_count

    start_time = time.time()
    delete_files_with_extension(folder_path, extension, update_progress)
    end_time = time.time()

    elapsed_time = end_time - start_time
    progress_label.config(text=f"Operation completed in {elapsed_time:.2f} seconds.")
    delete_button.config(state=tk.NORMAL)

# Create the main window
window = tk.Tk()
window.title("File Deletion")
window.geometry("400x300")

# Folder path label and entry
folder_label = tk.Label(window, text="Folder Path:")
folder_label.pack()

folder_entry = tk.Entry(window, width=40)
folder_entry.pack()

browse_button = tk.Button(window, text="Browse", command=browse_folder)
browse_button.pack()

# Extension label and entry
extension_label = tk.Label(window, text="File Extension:")
extension_label.pack()

extension_entry = tk.Entry(window, width=10)
extension_entry.pack()

# Delete button
delete_button = tk.Button(window, text="Delete Files", command=delete_files)
delete_button.pack()

# Progress label and progress bar
progress_label = tk.Label(window, text="")
progress_label.pack()

progress_bar = ttk.Progressbar(window, mode="determinate")
progress_bar.pack()

# Start the main loop
window.mainloop()


import pandas as pd
import numpy as np
import lightgbm as lgb
import matplotlib.pyplot as plt
import category_encoders as ce
import optuna  # ハイパーパラメータチューニング自動化ライブラリ
from tqdm import tqdm
from optuna.integration import lightgbm_tuner
from python_file.kaggle.common import common_funcs as cf
from sklearn.model_selection import train_test_split, StratifiedKFold
from sklearn.metrics import accuracy_score
from sklearn.feature_selection import RFECV, RFE
from python_file.kaggle.predict_winner.common import Common

DATA_DIR = "src/sample_data/Kaggle/predict_winner"
TRAIN_PATH = f"{DATA_DIR}/train_data.csv"
TEST_PATH = f"{DATA_DIR}/test_data.csv"
BUKI_PATH = f"{DATA_DIR}/weapon.csv"
REMOVAL_COLS = ["lobby", "game-ver"]

WEAPON_MAP = {
    "heroblaster_replica": "hotblaster",
    "herobrush_replica": "hokusai",
    "herocharger_replica": "splatcharger",
    "heromaneuver_replica": "maneuver",
    "heroroller_replica": "splatroller",
    "heroshelter_replica": "parashelter",
    "heroshooter_replica": "sshooter",
    "heroslosher_replica": "bucketslosher",
    "herospinner_replica": "splatspinner",
    "octoshooter_replica": "sshooter",
}

RANK_MAP = {
    "c-": 1,
    "c": 2,
    "c+": 3,
    "b-": 4,
    "b": 5,
    "b+": 6,
    "a-": 7,
    "a": 8,
    "a+": 9,
    "s-": 10,
    "s": 11,
    "s+": 12,
    "x": 13,
}

cm = Common()

def get_best_params(train_x, train_y):
    tr_x, val_x, tr_y, val_y = train_test_split(
        train_x, train_y, test_size=0.2, random_state=1
    )
    tr_set = lgb.Dataset(tr_x, tr_y)
    val_set = lgb.Dataset(val_x, val_y, reference=tr_set)
    best_params = {}
    params = {
        "objective": "binary",
        "metric": "binary_logloss",
        "boosting_type": "gbdt",
    }
    best_params = {}
    tuning_history = []
    gbm = lightgbm_tuner.train(
        params,
        tr_set,
        valid_sets=val_set,
        num_boost_round=1000,
        early_stopping_rounds=50,
        verbose_eval=10,
        best_params=best_params,
        tuning_history=tuning_history,
    )
    return best_params

def objective(X, y, trial):
    """最適化する目的関数"""
    tr_x, val_x, tr_y, val_y = train_test_split(X, y, test_size=0.2, random_state=1)
    gbm = lgb.LGBMClassifier(
        objective="binary",
        boosting_type="gbdt",
        n_jobs=4,
        n_estimators=1000,
        early_stopping_rounds=100,
        learning_rate=0.01,
    )
    # RFE で取り出す特徴量の数を最適化する
    n_features_to_select = (
        trial.suggest_int("n_features_to_select", 100, len(list(tr_x.columns))),
    )
    rfe = RFE(estimator=gbm, n_features_to_select=n_features_to_select)
    rfe.fit(tr_x, tr_y)
    selected_cols = list(tr_x.columns[rfe.support_])

    tr_x_selected = tr_x.loc[:, selected_cols]
    val_x_selected = val_x.loc[:, selected_cols]
    gbm.fit(
        tr_x_selected,
        tr_y,
        eval_set=[(val_x_selected, val_y)],
        early_stopping_rounds=20,
    )
    y_pred = gbm.predict(val_x_selected)
    accuracy = accuracy_score(val_y, y_pred)
    return accuracy

def train(train_x, train_y, kfold, best_params=None):
    params = {
        "objective": "binary",
        "boosting_type": "gbdt",
        "metric": {"binary_logloss"},
        "num_leaves": 50,
        "min_data_in_leaf": 100,
        "learning_rate": 0.1,
        "feature_fraction": 0.7,
    }
    models = []
    for i, (tr_idx, val_idx) in enumerate(kfold.split(train_x, train_y)):
        tr_x = train_x.iloc[tr_idx].reset_index(drop=True)
        tr_y = train_y.iloc[tr_idx].reset_index(drop=True)
        val_x = train_x.iloc[val_idx].reset_index(drop=True)
        val_y = train_y.iloc[val_idx].reset_index(drop=True)

        tr_set = lgb.Dataset(tr_x, tr_y)
        val_set = lgb.Dataset(val_x, val_y, reference=tr_set)

        evals_result = {}
        model = lgb.train(
            params=params,
            train_set=tr_set,
            valid_sets=[val_set, tr_set],
            num_boost_round=10000,
            early_stopping_rounds=100,
            verbose_eval=1,
            evals_result=evals_result,
            feval=accuracy,
        )

        importance = pd.DataFrame(
            model.feature_importance(), index=train_x.columns, columns=["importance"]
        ).sort_values("importance", ascending=[False])

        # print(f"######################importance#####################")
        # print(importance.head(50))

        # 検証結果の描画
        fig = lgb.plot_metric(evals_result)
        plt.savefig(f"{DATA_DIR}/learning_curve_{i+1}.png")

        models.append(model)

    return models

def accuracy(preds, data, threshold=0.5):
    """精度 (Accuracy) を計算する関数"""
    weight = data.get_weight()
    true_label = data.get_label()
    pred_label = (preds > threshold).astype(int)
    acc = np.average(true_label == pred_label, weights=weight)
    return "accuracy", acc, True

def predict(model, test_x, threshold):
    y_pred = model.predict(test_x, num_iteration=model.best_iteration)
    pred = [0 if i < threshold else 1 for i in y_pred]
    return pd.Series(pred)

def run_all():
    train_raw_data = pd.read_csv(TRAIN_PATH)
    test_raw_data = pd.read_csv(TEST_PATH)
    buki_raw_data = pd.read_csv(BUKI_PATH)

    train_raw_data = train_raw_data.replace(WEAPON_MAP)
    test_raw_data = test_raw_data.replace(WEAPON_MAP)
    buki_raw_data = buki_raw_data.replace(WEAPON_MAP)

    test_raw_data["y"] = 0
    train_raw_data["usage"] = 0  # for train
    test_raw_data["usage"] = 1  # for test

    X = cm.make_input_output(train_raw_data, with_y=False)
    train_data = pd.concat([train_raw_data, X], axis=1)

    # 武器ごとの勝率計算
    win_rate_df = []
    for buki in buki_raw_data.key.unique():
        # print(buki, win_rate(buki))
        rate, count = cm.win_rate(buki, train_data)
        win_rate_df.append([buki, rate, count])
    win_rate_df = pd.DataFrame(win_rate_df, columns=["buki", "win_rate", "count"])
    win_rate_df = win_rate_df.sort_values(by="win_rate", ascending=False)
    buki_ja_dict = buki_raw_data[["key", "reskin"]].set_index("key").to_dict()["reskin"]
    win_rate_df.buki = win_rate_df.buki.map(buki_ja_dict)
    win_rate_dict = (
        win_rate_df.reset_index(drop=True)
        .loc[:, ["buki", "win_rate"]]
        .set_index("buki")
        .to_dict()
    )[
        "win_rate"
    ]  # 全体の勝率dict(武器名: 勝率)

    # モード毎の武器勝率計算
    win_rate_mode_df = pd.DataFrame()
    for mode in train_data["mode"].unique():
        win_rate_df = []
        train_mode = train_data[train_data["mode"] == mode]
        for buki in buki_raw_data.key.unique():
            # print(buki, win_rate(buki))
            rate, count = cm.win_rate(buki, train_mode)
            win_rate_df.append([buki, rate, count])
        win_rate_df = pd.DataFrame(
            win_rate_df, columns=[mode + "_buki", mode + "_win_rate", mode + "_count"]
        )
        win_rate_df = win_rate_df.sort_values(by=mode + "_win_rate", ascending=False)
        buki_ja_dict = (
            buki_raw_data[["key", "reskin"]].set_index("key").to_dict()["reskin"]
        )
        win_rate_df[mode + "_buki"] = win_rate_df[mode + "_buki"].map(buki_ja_dict)
        win_rate_df = win_rate_df.reset_index(drop=True)
        win_rate_mode_df = pd.concat([win_rate_mode_df, win_rate_df], axis=1)

    # 武器と射程距離の関係分類計算
    buki_range_distance_dict = (
        buki_raw_data.loc[:, ["category2", "key"]]
        .set_index("key")
        .to_dict()["category2"]
    )

    train_data, test_data = cm.make_feature(train_raw_data, test_raw_data)
    raw_data = pd.concat([train_data, test_data], axis=0).reset_index(drop=True)
    raw_data = raw_data.fillna(raw_data.mode().iloc[0])

    data = cm.add_count_range_distance(raw_data, buki_range_distance_dict)
    # data = cm.calc_weapons_win_rate_avg(data, win_rate_dict)
    # data = cm.calc_weapons_win_rate_avg_per_mode(data, win_rate_mode_df)

    categorical_columns = [x for x in data.columns if data[x].dtype == "object"]

    ce_oe = ce.OrdinalEncoder(cols=categorical_columns, handle_unknown="impute")
    encorded_data = ce_oe.fit_transform(data)

    train_data = (
        encorded_data[encorded_data["usage"] == 0]
        .drop(["usage"], axis=1)
        .reset_index(drop=True)
    )
    test_data = (
        encorded_data[encorded_data["usage"] == 1]
        .drop(["usage"], axis=1)
        .reset_index(drop=True)
    )

    train_y = train_data.loc[:, "y"]
    train_x = train_data.drop(["y"], axis=1)
    test_x = test_data.drop(["y"], axis=1)

    ids = test_x.loc[:, "id"]

    # # 学習用のハイパラをチューニング
    # best_params = get_best_params(train_x, train_y)

    # 学習
    n_splits = 5
    kfold = StratifiedKFold(n_splits=n_splits, shuffle=True, random_state=0)
    models = train(train_x, train_y, kfold)

    # 評価
    threshold = 0.5
    y_preds = []
    for i, model in enumerate(models):
        y_pred = predict(model, test_x, threshold)
        y_preds.append(y_pred)

    # 提出用ファイル成型
    winner_pred = pd.concat(y_preds, axis=1).mode(axis=1).rename(columns={0: "y"})
    submission = pd.concat([ids, winner_pred], axis=1)

    print(submission)
    submission.to_csv(f"{DATA_DIR}/submission18.csv", index=False)

if __name__ == "__main__":
    run_all()


import logging
import os
import unittest
import fudge
from google.appengine.ext import db
from crauth import users, models, permissions

os.environ['SERVER_NAME'] = 'localhost'
os.environ['SERVER_PORT'] = '8000'
os.environ['USER_EMAIL'] = 'test@example.com'
os.environ['USER_IS_ADMIN'] = '1'

FAKE_DOMAIN = 'fakedomain.com'
FAKE_EMAIL = 'fake@%s' % FAKE_DOMAIN

class PermissionsTestCase(unittest.TestCase):
    def setUp(self):
        if os.environ.has_key(users._ENVIRON_EMAIL):
            del os.environ[users._ENVIRON_EMAIL]
        if os.environ.has_key(users._ENVIRON_DOMAIN):
            del os.environ[users._ENVIRON_DOMAIN]
        domain = models.AppsDomain.get_or_insert(
            key_name=FAKE_DOMAIN,
            domain=FAKE_DOMAIN,
        )
        db.delete(models.UserPermissions.all().fetch(100))
        db.delete(models.Role.all().fetch(100))

    def set_testing_user(self):
        users._set_testing_user(FAKE_EMAIL, FAKE_DOMAIN)
        return users.get_current_user()

    def fake_is_admin(self, is_admin=True):
        fake_is_admin = fudge.Fake('users.User.is_admin',
                                   callable=True)
        fake_is_admin.returns(is_admin)
        return fudge.patched_context(users.User, 'is_admin',
                                     fake_is_admin)

    def testFakeIsAdmin(self):
        self.set_testing_user()
        with self.fake_is_admin(True):
            self.assertTrue(users.is_current_user_admin())
        with self.fake_is_admin(False):
            self.assertFalse(users.is_current_user_admin())

    def testHasPerm(self):
        user = self.set_testing_user()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            permissions=['test_perm1', 'test_perm2'],
        )
        permissions.put()
        with self.fake_is_admin(False):
            self.assertTrue(user.has_perms(['test_perm1', 'test_perm2']))

    def testHasPerms(self):
        user = self.set_testing_user()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            permissions=['test_perm'],
        )
        permissions.put()
        with self.fake_is_admin(False):
            self.assertTrue(user.has_perm('test_perm'))

    def testDoesntHavePerm(self):
        user = self.set_testing_user()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            permissions=['some_perm'],
        )
        permissions.put()
        with self.fake_is_admin(False):
            self.assertFalse(user.has_perm('some_other_perm'))

    def testAdminHasAllPerms(self):
        user = self.set_testing_user()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            permissions=['some_perm'],
        )
        permissions.put()
        with self.fake_is_admin(True):
            self.assertTrue(user.has_perm('some_perm'))
            self.assertTrue(user.has_perm('some_other_perm'))
            self.assertTrue(user.has_perms(['some_perm', 'some_other_perm']))

    def testDoesntHaveAllPerms(self):
        user = self.set_testing_user()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            permissions=['some_perm'],
        )
        permissions.put()
        with self.fake_is_admin(False):
            self.assertFalse(user.has_perms(['some_perm', 'some_other_perm']))

    def testHasAdminPerm(self):
        user = self.set_testing_user()
        with self.fake_is_admin(True):
            for perm in permissions.ADMIN_PERMS:
                self.assertTrue(user.has_perm(perm))
            self.assertTrue(user.has_perms(permissions.ADMIN_PERMS))

    def testDoesntHaveAdminPerm(self):
        user = self.set_testing_user()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            permissions=['admin'],
        )
        permissions.put()
        with self.fake_is_admin(False):
            self.assertFalse(user.has_perm('admin'))

    def testRoleForDomain(self):
        domain_1 = models.AppsDomain(domain='domain1')
        domain_1.put()
        domain_2 = models.AppsDomain(domain='domain2')
        domain_2.put()
        role = models.Role(
            name='Test Role',
            domain=domain_1,
        )
        role.put()
        role = models.Role(
            name='Test Role',
            domain=domain_2,
        )
        role.put()
        self.assertEqual(len(models.Role.for_domain(domain_1).fetch(2)), 1)

    def testHasPermThroughRole(self):
        user = self.set_testing_user()
        role = models.Role(
            name='Test Role',
            domain=user.domain(),
            permissions=['some_perm'],
        )
        role.put()
        permissions = models.UserPermissions(
            key_name=user._email,
            user_email=user._email,
            roles=[role.key()],
        )
        permissions.put()
        with self.fake_is_admin(False):
            self.assertTrue(user.has_perm('some_perm'))


import asyncio
from time import time
from typing import Any, Awaitable, Callable
from unittest import mock
from uuid import uuid4

import pytest_asyncio
from aiokafka import AIOKafkaProducer
from httpx import AsyncClient
from motor.core import AgnosticClient
from motor.motor_asyncio import AsyncIOMotorClient
from src.common.authorization import JwtClaims, JwtUserSchema
from src.common.databases import get_kafka_producer, get_mongodb
from src.main import app
from src.settings.app import get_app_settings

settings = get_app_settings()

GetRequestType = Callable[
    [str, dict[str, Any]],
    Awaitable[tuple[dict, int]],
]

@pytest_asyncio.fixture(scope="session")
def event_loop():
    policy = asyncio.get_event_loop_policy()
    loop = policy.new_event_loop()
    yield loop
    loop.close()

@pytest_asyncio.fixture(scope="function")
async def db_session():
    mongodb = AsyncIOMotorClient(settings.mongo.dsn)
    producer = mock.AsyncMock()

    def get_test_mongodb() -> AgnosticClient:
        if mongodb is None:
            raise RuntimeError("MongoDB client has not been defined.")

        return mongodb

    def get_test_kafka_producer() -> AIOKafkaProducer:
        if producer is None:
            raise RuntimeError("Kafka producer has not been defined.")

        return producer

    app.dependency_overrides[get_mongodb] = get_test_mongodb
    app.dependency_overrides[get_kafka_producer] = get_test_kafka_producer

    yield mongodb

    collections = await mongodb[settings.mongo.db_name].list_collection_names()
    for collection in collections:
        await mongodb[settings.mongo.db_name].drop_collection(collection)

@pytest_asyncio.fixture(scope="function")
async def client():
    base_url = f"http://{settings.service.host}:{settings.service.port}/api/v1"
    async with AsyncClient(app=app, base_url=base_url) as client:
        yield client

@pytest_asyncio.fixture(scope="session", autouse=True)
async def mock_fast_api_limiter_init():
    """Mock FastAPILimiter.init() method to prevent 429 status code during tests."""
    with mock.patch(
        "fastapi_limiter.depends.FastAPILimiter", new_callable=mock.AsyncMock
    ):
        yield

@pytest_asyncio.fixture(scope="session", autouse=True)
async def mock_jwt():
    jwt_user = JwtUserSchema(
        id=str(uuid4()),
        permissions=["test"],
    )
    jwt_claim = JwtClaims(
        user=jwt_user,
        iat=1,
        access_jti="test",
        refresh_jti="test",
        type="test",
        exp=int(time()) + 60,
    )

    with mock.patch("src.common.authorization.decode_token") as mock_jwt:
        mock_jwt.return_value = jwt_claim
        yield jwt_claim


import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

A = np.array([[1,0.1,0.1],
              [0.1,1,0.9],
              [0.1,0.9,1]])

def plot_mu(mus,*args, triag = False, **kwargs):
    mus = mus/np.sum(mus, axis = 0)
    x = mus[1] + mus[-1]/2
    y = mus[-1]
    if triag:
        x = np.append(x, x[0])
        y = np.append(y, y[0])
    plt.plot(x,y, *args, **kwargs)
    return x,y

fig = plt.figure()
ax = plt.gca()
ax.set_xticks([])
ax.set_yticks([])

ax.set_frame_on(False)
plot_mu(np.eye(3), 'k', triag = True)
x,y = plot_mu(A, 'ko')

coex = Polygon(np.array([x,y]).T, closed = True,
               alpha = 0.5, color = "g")
ax.add_patch(coex)

rc = 1/len(A)*(np.sum(A/np.linalg.norm(A, axis = 1), axis = 0))
plot_mu(rc, 'ro')

itera = 5000
mu1 = np.linspace(0,1,itera)
mu2 = np.linspace(0,1-mu1,itera)
mu1 = np.repeat([mu1], itera, axis = 0)
mu3 = 1 - mu1 - mu2
mu = np.array([mu1, mu2, mu3])
good = np.abs(np.einsum("i, inm->nm", rc, mu)/np.linalg.norm(mu, axis = 0)-0.75)<1e-4

plot_mu(mu[:,good], ',b')
mu_theta = mu[:,good]
pos_equi = np.all(np.linalg.solve(A, mu_theta)>0, axis = 0)
plot_mu(mu[:,good][:,pos_equi], ',', color = "orange")

ax.text(0,-0.05,"$\mu_1$", ha = "center")
ax.text(1,-0.05,"$\mu_2$", ha = "center")
ax.text(0.5,1+0.05,"$\mu_3$", ha = "center")

fig.savefig("Figure_S7.pdf")


#!/usr/bin/python3

# Note: it could be further optimized by not performing whole string comparisons,
# but instead compare the first character

file = open("input.txt", "r")

hor = 0
depth = 0

while True:
    line = file.readline()
    if(line == ""):
        break

    command, x = line.split(" ")
    value = int(x)
    if command == 'up':
        depth -= value
    elif command == 'down':
        depth += value
    elif command == 'forward':
        hor += value

print(hor * depth)


def solution(n, arr1, arr2):
    answer = []
    array1=[]
    for i in range(n):
        temp1=list(str(bin(arr1[i])))
        del temp1[0]
        del temp1[0]
        while len(temp1)<n:
            temp1.insert(0,"0")
        temp2 = list(str(bin(arr2[i])))
        del temp2[0]
        del temp2[0]
        while len(temp2) < n:
            temp2.insert(0, "0")
        temp3=""
        for j in range(n):
            if temp1[j]=="0" and temp2[j]=="0":
                temp3+=" "
            else:
                temp3+="#"
        answer.append(temp3)

    return answer

프로그래머스/lv1/17681. ［1차］ 비밀지도/［1차］ 비밀지도.py

［1차］ 비밀지도.py


from django.urls import path, include
from rest_framework import routers

from .views import StoryViewSet, EpisodeViewSet

router = routers.DefaultRouter()

router.register(r'episodes', EpisodeViewSet, basename='episodes')
router.register(r'(?P<story_id>[0-9]+)/episode', EpisodeViewSet, basename='story_episodes')
router.register(r'', StoryViewSet, basename='stories')

urlpatterns = [
    path('', include(router.urls)),
]


"""
PyTorch GAN Zoo - unittests for FC-networks
Author: Vahid Mirjalili
"""

from ganzoo.misc import ops
from ganzoo.nn_modules import fc_nets
from ganzoo.constants import data_constants
from ganzoo.constants import names
from ganzoo.constants import defaults

def test_fc_networks():

    image_shape = data_constants.IMAGE_SHAPES[names.NAMESTR_MNIST]
    image_dim, image_channels = image_shape[0], image_shape[2]

    classes = [
        (fc_nets.FCSmall_Generator, fc_nets.FCSmall_Discriminator),
        (fc_nets.FCSkipConnect_Generator, fc_nets.FCSkipConnect_Discriminator),
        (fc_nets.FCLarge_Generator, fc_nets.FCLarge_Discriminator)
    ]
    for gen_class, disc_class in classes:
        generator = gen_class(
            num_z_units=10,
            num_hidden_units=10,
            output_image_dim=image_dim,
            output_image_channels=image_channels,
            p_drop=0.5)

        input_size = image_dim * image_dim * image_channels
        discriminator = disc_class(
            input_feature_dim=input_size,
            num_hidden_units=10,
            p_drop=0.5,
            activation=defaults.DISC_ACTIVATIONS['vanilla'])

        # test with uniform z
        sample_z = ops.get_latent_sampler(
            z_dim=10,
            z_distribution=names.NAMESTR_UNIFORM,
            make_4d=False)
        z = sample_z(batch_size=2)

        fake = generator(z)
        assert fake.shape == (2, 1, image_dim, image_dim)

        d_fake = discriminator(fake)
        assert d_fake.shape == (2, 1)

        # test with normal z
        sample_z = ops.get_latent_sampler(
            z_dim=10,
            z_distribution=names.NAMESTR_UNIFORM,
            make_4d=False)
        z = sample_z(batch_size=2)

        fake = generator(z)
        assert fake.shape == (2, 1, image_dim, image_dim)

        d_fake = discriminator(fake)
        assert d_fake.shape == (2, 1)


import smbus
import time

# MPU-6050 register addresses
PWR_MGMT_1 = 0x6B
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F
GYRO_XOUT_H = 0x43
GYRO_YOUT_H = 0x45
GYRO_ZOUT_H = 0x47

# Initialize I2C bus and MPU-6050
bus = smbus.SMBus(1)
bus.write_byte_data(0x68, PWR_MGMT_1, 0)

# Function to read 16-bit word from I2C device
def read_word_2c(addr):
    high = bus.read_byte_data(0x68, addr)
    low = bus.read_byte_data(0x68, addr + 1)
    val = (high << 8) + low

    if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val

# Read and print accelerometer and gyroscope data
while True:
    accel_x = read_word_2c(ACCEL_XOUT_H)
    accel_y = read_word_2c(ACCEL_YOUT_H)
    accel_z = read_word_2c(ACCEL_ZOUT_H)
    gyro_x = read_word_2c(GYRO_XOUT_H)
    gyro_y = read_word_2c(GYRO_YOUT_H)
    gyro_z = read_word_2c(GYRO_ZOUT_H)

    print("accelerometer (x,y,z): ({},{},{})".format(accel_x, accel_y, accel_z))
    print("gyroscope (x,y,z): ({},{},{})".format(gyro_x, gyro_y, gyro_z))

    time.sleep(1.5)


# imported packages
import pandas as pd
from inoutput import *
from naiveBayes import *
from freq import *
import matplotlib.pyplot as plt
import matplotlib.colors as mcol

# main method
def main():
    sep = '='*100+"\n"   # separator

    # get user input of filename
    # read the file using csv
    data = read_csv_file(get_user_input())

    # ================================================================================================================ #

    # Task 1: Extract data and build model
    print('==========\nTask 1\n==========')
    # extract raw title from data
    raw_title_2018 = get_raw_title(data, '2018')

    # clean the raw title data
    clean_words_2018, removed_words_2018 = clean_raw_data(raw_title_2018)

    # get the vocabulary 2018 words and size
    vocabulary_2018 = sorted(set(clean_words_2018))
    vocabulary_size = len(vocabulary_2018)

    # extract class raw title data
    story_title_raw, ask_title_raw, show_title_raw, poll_title_raw = get_raw_class_title(data)

    # clean raw class title data
    clean_story_words, _ = clean_raw_data(story_title_raw)
    clean_ask_words, _ = clean_raw_data(ask_title_raw)
    clean_show_words, _ = clean_raw_data(show_title_raw)
    clean_poll_words, _ = clean_raw_data(poll_title_raw)

    # write the vocabulary to vocabulary.txt
    write_to_file('vocabulary.txt', vocabulary_2018)

    # write the removed words to removed_word.txt
    write_to_file('removed_word.txt', removed_words_2018)
    print('Done cleaning data...')
    print(sep)

    # words frequency
    print('Computing word frequencies...')
    story_freq_dict = get_word_freq_dict(clean_story_words)
    ask_freq_dict = get_word_freq_dict(clean_ask_words)
    show_freq_dict = get_word_freq_dict(clean_show_words)
    poll_freq_dict = get_word_freq_dict(clean_poll_words)
    print('Done computing word frequencies')
    print(sep)

    # Probability smoothing (delta = 0.5)
    # get count of classes
    story_count, ask_count, show_count, poll_count, total_count = get_post_type_count(data)

    # probability of each class
    p_story = float(story_count / total_count)
    p_ask = float(ask_count / total_count)
    p_show = float(show_count / total_count)
    p_poll = float(poll_count / total_count)

    # total number of words in each class
    story_words_count = len(story_freq_dict)
    ask_words_count = len(ask_freq_dict)
    show_words_count = len(show_freq_dict)
    poll_words_count = len(poll_freq_dict)

    delta = 0.5  # smooth word frequency from vocabulary_2018
    # create a dictionary of words and frequency with smoothing
    words_freq_dict = {w: {'story': story_freq_dict.get(w, 0) + delta, 'ask_hn': ask_freq_dict.get(w, 0) + delta,
                           'show_hn': show_freq_dict.get(w, 0) + delta, 'poll': poll_freq_dict.get(w, 0) + delta}
                       for w in vocabulary_2018}

    # create a data frame from words freq dictionary
    pd.set_option("display.max_rows", None)  # print all dataframe rows
    words_freq_prob_df = pd.DataFrame.from_dict(words_freq_dict, orient='index',
                                                columns=['story', 'ask_hn', 'show_hn', 'poll'])

    # compute the smoothed probability (delta = 0.5)
    words_freq_prob_df, story_prob = get_word_prob(words_freq_prob_df, 'story', 'story_prob', story_words_count,
                                                   vocabulary_size, delta=delta)
    words_freq_prob_df, ask_prob = get_word_prob(words_freq_prob_df, 'ask_hn', 'ask_prob', ask_words_count,
                                                 vocabulary_size, delta=delta)
    words_freq_prob_df, show_prob = get_word_prob(words_freq_prob_df, 'show_hn', 'show_prob', show_words_count,
                                                  vocabulary_size, delta=delta)
    words_freq_prob_df, poll_prob = get_word_prob(words_freq_prob_df, 'poll', 'poll_prob', poll_words_count,
                                                  vocabulary_size, delta=delta)
    print('Done computing smoothed probabilities')
    print(sep)
    print(words_freq_prob_df)
    print(sep)

    # create dictionary of words: prob for each class
    story_word_prob = dict(list(zip(words_freq_prob_df.index, story_prob)))
    ask_word_prob = dict(list(zip(words_freq_prob_df.index, ask_prob)))
    show_word_prob = dict(list(zip(words_freq_prob_df.index, show_prob)))
    poll_word_prob = dict(list(zip(words_freq_prob_df.index, poll_prob)))

    # create the model-2018.txt file
    create_model_file('model-2018.txt', words_freq_prob_df, story_prob, ask_prob, show_prob, poll_prob)
    print(sep)

    # ================================================================================================================ #

    # Task 2: Use ML Classifier to test data set
    print('==========\nTask 2\n==========')
    # get the testing data titles
    testing_2019, testing_titles = get_testing_title(data)

    clean_testing_titles = []   # titles only
    actual_labels = []         # actual labels only
    for t, l in testing_2019:
        clean_testing_titles.append(clean_testing_data(t))  # clean titles
        actual_labels.append(l)

    # get scores for the each title and each class type
    story_scores = get_score(clean_testing_titles, p_story, story_word_prob, story_words_count, vocabulary_size, 'story')
    ask_scores = get_score(clean_testing_titles, p_ask, ask_word_prob, ask_words_count, vocabulary_size, 'ask_hn')
    show_scores = get_score(clean_testing_titles, p_show, show_word_prob, show_words_count, vocabulary_size, 'show_hn')
    poll_scores = get_score(clean_testing_titles, p_poll, poll_word_prob, poll_words_count, vocabulary_size, 'poll')

    # create dictionary of title score
    title_score_dict = {i: {'title': clean_testing_titles[i], 'story': story_scores[i], 'ask_hn': ask_scores[i],
                            'show_hn': show_scores[i], 'poll': poll_scores[i], 'actual_label': actual_labels[i]}
                        for i in range(len(clean_testing_titles))}

    # create a data frame from title score dictionary
    testing_score_df = pd.DataFrame.from_dict(title_score_dict, orient='index',
                                              columns=['title', 'story', 'ask_hn', 'show_hn', 'poll', 'actual_label'])

    # compute max scores
    max_scores = compute_max_scores(testing_score_df)

    # set classified label
    classify_label = set_classify_label(testing_score_df, max_scores)

    # check if classification is right of wrong
    classify_result = check_classification(testing_score_df)
    print(testing_score_df)
    print(sep)

    # compute the number of right and wrong
    right_count, wrong_count = get_number_right_wrong(testing_score_df)
    print(sep)

    # create baseline-result.txt file
    create_baseline('baseline-result.txt', testing_score_df, testing_titles, classify_label,
                    story_scores, ask_scores, show_scores, poll_scores,
                    actual_labels, classify_result)
    print(sep)

    # ================================================================================================================ #

    # Task 3: Experiments with the classifier
    print('==========\nTask 3\n==========')

    # Experiment 1: Stop-word Filtering
    print('----------\nExperiment 1\n----------')
    stopwords_file = './resource/stopwords.txt'
    stopword_model = 'stopword-model.txt'
    stopword_result = 'stopword-result.txt'

    # Experiment 1 Task 1
    # read stopwords file and store words in list
    stopwords_list = read_file(stopwords_file)

    # filter stopwords from vocabulary
    e1_vocabulary = [w for w in vocabulary_2018 if w not in stopwords_list]
    e1_vocabulary_size = len(e1_vocabulary)

    # create a dictionary of words and frequency with smoothing from experiment1_vocabulary
    e1_words_freq_dict = {w: {'story': story_freq_dict.get(w, 0) + delta, 'ask_hn': ask_freq_dict.get(w, 0) + delta,
                              'show_hn': show_freq_dict.get(w, 0) + delta, 'poll': poll_freq_dict.get(w, 0) + delta}
                          for w in e1_vocabulary}

    # create a data frame from experiment1 words freq dictionary
    e1_words_freq_dict_df = pd.DataFrame.from_dict(e1_words_freq_dict, orient='index',
                                                   columns=['story', 'ask_hn', 'show_hn', 'poll'])

    # compute the smoothed probability (delta = 0.5)
    e1_words_freq_dict_df, e1_story_prob = get_word_prob(e1_words_freq_dict_df, 'story', 'story_prob',
                                                         story_words_count, e1_vocabulary_size, delta=delta)
    e1_words_freq_dict_df, e1_ask_prob = get_word_prob(e1_words_freq_dict_df, 'ask_hn', 'ask_prob',
                                                       ask_words_count, e1_vocabulary_size, delta=delta)
    e1_words_freq_dict_df, e1_show_prob = get_word_prob(e1_words_freq_dict_df, 'show_hn', 'show_prob',
                                                        show_words_count, e1_vocabulary_size, delta=delta)
    e1_words_freq_dict_df, e1_poll_prob = get_word_prob(e1_words_freq_dict_df, 'poll', 'poll_prob',
                                                        poll_words_count, e1_vocabulary_size, delta=delta)

    # create dictionary of words: prob for each class
    e1_story_word_prob = dict(list(zip(e1_words_freq_dict_df.index, e1_story_prob)))
    e1_ask_word_prob = dict(list(zip(e1_words_freq_dict_df.index, e1_ask_prob)))
    e1_show_word_prob = dict(list(zip(e1_words_freq_dict_df.index, e1_show_prob)))
    e1_poll_word_prob = dict(list(zip(e1_words_freq_dict_df.index, e1_poll_prob)))

    # create the stopword model
    create_model_file(stopword_model, e1_words_freq_dict_df, e1_story_prob, e1_ask_prob,
                      e1_show_prob, e1_poll_prob)
    print(sep)

    # Experiment 1 Task 2
    # get scores for the each title and each class type
    e1_story_scores = get_score(clean_testing_titles, p_story, e1_story_word_prob,
                                story_words_count, e1_vocabulary_size, 'story')
    e1_ask_scores = get_score(clean_testing_titles, p_ask, e1_ask_word_prob,
                              ask_words_count, e1_vocabulary_size, 'ask_hn')
    e1_show_scores = get_score(clean_testing_titles, p_show, e1_show_word_prob,
                               show_words_count, e1_vocabulary_size, 'show_hn')
    e1_poll_scores = get_score(clean_testing_titles, p_poll, e1_poll_word_prob,
                               poll_words_count, e1_vocabulary_size, 'poll')

    # create dictionary of title score
    e1_title_score_dict = {i: {'title': clean_testing_titles[i], 'story': e1_story_scores[i],
                               'ask_hn': e1_ask_scores[i], 'show_hn': e1_show_scores[i], 'poll': e1_poll_scores[i],
                               'actual_label': actual_labels[i]} for i in range(len(clean_testing_titles))}

    # create a data frame from title score dictionary
    e1_testing_score_df = pd.DataFrame.from_dict(e1_title_score_dict, orient='index',
                                                 columns=['title', 'story', 'ask_hn', 'show_hn', 'poll', 'actual_label'])

    # compute e1 max scores
    e1_max_scores = compute_max_scores(e1_testing_score_df)

    # set e1 classified label
    e1_classify_label = set_classify_label(e1_testing_score_df, e1_max_scores)

    # check if e1 classification is right of wrong
    e1_classify_result = check_classification(e1_testing_score_df)

    # compute the number of right and wrong
    e1_right_count, wrong_count = get_number_right_wrong(e1_testing_score_df)
    print(sep)

    # create stopword-result file
    create_baseline(stopword_result, e1_testing_score_df, testing_titles, e1_classify_label,
                    e1_story_scores, e1_ask_scores, e1_show_scores, e1_poll_scores,
                    actual_labels, e1_classify_result)
    print(sep)

    # ---------------------------------------------------------------------------------------------------------------- #
    # Experiment 2: Word Length Filtering
    print('----------\nExperiment 2\n----------')

    wordlength_model = 'wordlength-model.txt'
    wordlength_result = 'wordlength-result.txt'

    # Experiment 2 Task 1
    # remove words with length <= 2 and >=9 from vocabulary
    e2_vocabulary = [w for w in vocabulary_2018 if 2 < len(w) < 9]
    e2_vocabulary_size = len(e2_vocabulary)

    # create a dictionary of words and frequency with smoothing from experiment1_vocabulary
    e2_words_freq_dict = {w: {'story': story_freq_dict.get(w, 0) + delta, 'ask_hn': ask_freq_dict.get(w, 0) + delta,
                              'show_hn': show_freq_dict.get(w, 0) + delta, 'poll': poll_freq_dict.get(w, 0) + delta}
                          for w in e2_vocabulary}

    # create a data frame from experiment1 words freq dictionary
    e2_words_freq_dict_df = pd.DataFrame.from_dict(e2_words_freq_dict, orient='index',
                                                   columns=['story', 'ask_hn', 'show_hn', 'poll'])

    # compute the smoothed probability (delta = 0.5)
    e2_words_freq_dict_df, e2_story_prob = get_word_prob(e2_words_freq_dict_df, 'story', 'story_prob',
                                                         story_words_count, e2_vocabulary_size, delta=delta)
    e2_words_freq_dict_df, e2_ask_prob = get_word_prob(e2_words_freq_dict_df, 'ask_hn', 'ask_prob',
                                                       ask_words_count, e2_vocabulary_size, delta=delta)
    e2_words_freq_dict_df, e2_show_prob = get_word_prob(e2_words_freq_dict_df, 'show_hn', 'show_prob',
                                                        show_words_count, e2_vocabulary_size, delta=delta)
    e2_words_freq_dict_df, e2_poll_prob = get_word_prob(e2_words_freq_dict_df, 'poll', 'poll_prob',
                                                        poll_words_count, e2_vocabulary_size, delta=delta)

    # create dictionary of words: prob for each class
    e2_story_word_prob = dict(list(zip(e2_words_freq_dict_df.index, e2_story_prob)))
    e2_ask_word_prob = dict(list(zip(e2_words_freq_dict_df.index, e2_ask_prob)))
    e2_show_word_prob = dict(list(zip(e2_words_freq_dict_df.index, e2_show_prob)))
    e2_poll_word_prob = dict(list(zip(e2_words_freq_dict_df.index, e2_poll_prob)))

    # create the wordlength_model
    create_model_file(wordlength_model, e2_words_freq_dict_df, e2_story_prob, e2_ask_prob,
                      e2_show_prob, e2_poll_prob)
    print(sep)

    # Experiment 2 Task 2
    # get scores for the each title and each class type
    e2_story_scores = get_score(clean_testing_titles, p_story, e2_story_word_prob,
                                story_words_count, e2_vocabulary_size, 'story')
    e2_ask_scores = get_score(clean_testing_titles, p_ask, e2_ask_word_prob,
                              ask_words_count, e2_vocabulary_size, 'ask_hn')
    e2_show_scores = get_score(clean_testing_titles, p_show, e2_show_word_prob,
                               show_words_count, e2_vocabulary_size, 'show_hn')
    e2_poll_scores = get_score(clean_testing_titles, p_poll, e2_poll_word_prob,
                               poll_words_count, e2_vocabulary_size, 'poll')

    # create dictionary of title score
    e2_title_score_dict = {i: {'title': clean_testing_titles[i], 'story': e2_story_scores[i],
                               'ask_hn': e2_ask_scores[i], 'show_hn': e2_show_scores[i], 'poll': e2_poll_scores[i],
                               'actual_label': actual_labels[i]} for i in range(len(clean_testing_titles))}

    # create a data frame from title score dictionary
    e2_testing_score_df = pd.DataFrame.from_dict(e2_title_score_dict, orient='index',
                                                 columns=['title', 'story', 'ask_hn', 'show_hn', 'poll', 'actual_label'])

    # compute e2 max scores
    e2_max_scores = compute_max_scores(e2_testing_score_df)

    # set e2 classified label
    e2_classify_label = set_classify_label(e2_testing_score_df, e2_max_scores)

    # check if e2 classification is right of wrong
    e2_classify_result = check_classification(e2_testing_score_df)

    # compute the number of right and wrong
    e2_right_count, e2_wrong_count = get_number_right_wrong(e2_testing_score_df)
    print(sep)

    # create wordlength_result file
    create_baseline(wordlength_result, e2_testing_score_df, testing_titles, e2_classify_label,
                    e2_story_scores, e2_ask_scores, e2_show_scores, e2_poll_scores,
                    actual_labels, e2_classify_result)
    print(sep)

    # ---------------------------------------------------------------------------------------------------------------- #
    # Experiment 3: Infrequent Word Filtering (unfinished)
    # print('----------\nExperiment 3\n----------')

    # remove freq = 1
    story_no1_freq_dict, ask_no1_freq_dict, show_no1_freq_dict, poll_no1_freq_dict \
        = remove_freq_1(story_freq_dict, ask_freq_dict, show_freq_dict, poll_freq_dict)

    # create a dictionary of words and frequency with smoothing
    words_no1_freq_dict = {w: {'story': story_no1_freq_dict.get(w, 0) + delta,
                               'ask_hn': ask_no1_freq_dict.get(w, 0) + delta,
                               'show_hn': show_no1_freq_dict.get(w, 0) + delta,
                               'poll': poll_no1_freq_dict.get(w, 0) + delta} for w in vocabulary_2018}
    words_no1_freq_dict_df = pd.DataFrame.from_dict(words_no1_freq_dict, orient='index',
                                                    columns=['story', 'ask_hn', 'show_hn', 'poll'])
    # print("Removed freq = 1")
    # print(words_no1_freq_dict_df)
    # print(sep)

    # remove freq <= 5
    story_no5_freq_dict, ask_no5_freq_dict, show_no5_freq_dict, poll_no5_freq_dict \
        = remove_freq_lte_5(story_freq_dict, ask_freq_dict, show_freq_dict, poll_freq_dict)

    # create a dictionary of words and frequency with smoothing
    words_no5_freq_dict = {w: {'story': story_no5_freq_dict.get(w, 0) + delta,
                               'ask_hn': ask_no5_freq_dict.get(w, 0) + delta,
                               'show_hn': show_no5_freq_dict.get(w, 0) + delta,
                               'poll': poll_no5_freq_dict.get(w, 0) + delta} for w in vocabulary_2018}
    words_no5_freq_dict_df = pd.DataFrame.from_dict(words_no5_freq_dict, orient='index',
                                                    columns=['story', 'ask_hn', 'show_hn', 'poll'])
    # print("Removed freq <=5")
    # print(words_no5_freq_dict_df)
    # print(sep)

    # remove freq <= 10
    story_no10_freq_dict, ask_no10_freq_dict, show_no10_freq_dict, poll_no10_freq_dict \
        = remove_freq_lte_10(story_freq_dict, ask_freq_dict, show_freq_dict, poll_freq_dict)

    # create a dictionary of words and frequency with smoothing
    words_no10_freq_dict = {w: {'story': story_no10_freq_dict.get(w, 0) + delta,
                               'ask_hn': ask_no10_freq_dict.get(w, 0) + delta,
                               'show_hn': show_no10_freq_dict.get(w, 0) + delta,
                               'poll': poll_no10_freq_dict.get(w, 0) + delta} for w in vocabulary_2018}
    words_no10_freq_dict_df = pd.DataFrame.from_dict(words_no10_freq_dict, orient='index',
                                                     columns=['story', 'ask_hn', 'show_hn', 'poll'])
    # print("Removed freq <= 10")
    # print(words_no10_freq_dict_df)
    # print(sep)

    # remove freq <= 15
    story_no15_freq_dict, ask_no15_freq_dict, show_no15_freq_dict, poll_no15_freq_dict \
        = remove_freq_lte_15(story_freq_dict, ask_freq_dict, show_freq_dict, poll_freq_dict)

    # create a dictionary of words and frequency with smoothing
    words_no15_freq_dict = {w: {'story': story_no15_freq_dict.get(w, 0) + delta,
                               'ask_hn': ask_no15_freq_dict.get(w, 0) + delta,
                               'show_hn': show_no15_freq_dict.get(w, 0) + delta,
                               'poll': poll_no5_freq_dict.get(w, 0) + delta} for w in vocabulary_2018}
    words_no15_freq_dict_df = pd.DataFrame.from_dict(words_no15_freq_dict, orient='index',
                                                     columns=['story', 'ask_hn', 'show_hn', 'poll'])
    # print("Removed freq <= 15")
    # print(words_no15_freq_dict_df)
    # print(sep)

    # remove freq <= 20
    story_no20_freq_dict, ask_no20_freq_dict, show_no20_freq_dict, poll_no20_freq_dict \
        = remove_freq_lte_20(story_freq_dict, ask_freq_dict, show_freq_dict, poll_freq_dict)

    # create a dictionary of words and frequency with smoothing
    words_no20_freq_dict = {w: {'story': story_no20_freq_dict.get(w, 0) + delta,
                               'ask_hn': ask_no20_freq_dict.get(w, 0) + delta,
                               'show_hn': show_no20_freq_dict.get(w, 0) + delta,
                               'poll': poll_no20_freq_dict.get(w, 0) + delta} for w in vocabulary_2018}
    words_no20_freq_dict_df = pd.DataFrame.from_dict(words_no20_freq_dict, orient='index',
                                                     columns=['story', 'ask_hn', 'show_hn', 'poll'])
    # print("Removed freq <= 20")
    # print(words_no20_freq_dict_df)
    # print(sep)

# end of main
if __name__ == '__main__':
    main()


import sys,tweepy,csv,re
from textblob import TextBlob
import matplotlib.pyplot as plt

class SentimentAnalysis:

    def __init__(self):
        self.tweets = []
        self.tweetText = []

    def DownloadData(self):
        # authenticating
        consumerKey = ""
        consumerSecret = ""
        accessToken = ""
        accessTokenSecret = ""
        auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
        auth.set_access_token(accessToken, accessTokenSecret)
        api = tweepy.API(auth)

        # input for term to be searched and how many tweets to search
        searchTerm = "#scotrail"
        NoOfTerms = 100

        # searching for tweets
        self.tweets = tweepy.Cursor(api.search, q=searchTerm, lang = "en").items(NoOfTerms)

        # creating some variables to store info
        polarity = 0
        positive = 0
        negative = 0
        neutral = 0

        # iterating through tweets fetched
        for tweet in self.tweets:
            #Append to temp so that we can store in csv later. I use encode UTF-8
            self.tweetText.append(self.cleanTweet(tweet.text).encode('utf-8'))
            # print (tweet.text.translate(non_bmp_map))    #print tweet's text
            analysis = TextBlob(tweet.text)
            # print(analysis.sentiment)  # print tweet's polarity
            polarity += analysis.sentiment.polarity  # adding up polarities to find the average later

            if (analysis.sentiment.polarity == 0.00): # adding reaction of how people are reacting to find average later
                neutral += 1
            elif (analysis.sentiment.polarity > 0.00):
                positive += 1
            elif (analysis.sentiment.polarity < 0.00):
                negative += 1

        # finding average of how people are reacting
        positive = self.percentage(positive, NoOfTerms)
        negative = self.percentage(negative, NoOfTerms)
        neutral = self.percentage(neutral, NoOfTerms)

        # finding average reaction
        polarity = polarity / NoOfTerms

    with open('Tweet_Sentiments.csv', 'w') as scorefile:
        scoreFileWriter = csv.writer(scorefile)
        # printing out data
        print("How people are reacting to " + searchTerm + " by analyzing " + str(NoOfTerms) + " tweets.")
        print()
        print("General Report: ")
        scoreFileWriter.writerow([tweet.text, analysis.sentiment.polarity])

        if (polarity == 0):
            print("Neutral")
        elif (polarity > 0.00):
            print("Positive")
        elif (polarity < 0.00):
            print("Negative")

        print()
        print("Detailed Report: ")
        print(str(positive) + "% people were positive")
        print(str(negative) + "% people were negative")
        print(str(neutral) + "% people were neutral")

        self.plotPieChart(positive, negative, neutral, searchTerm, NoOfTerms)

    def cleanTweet(self, tweet):
        # Remove Links, Special Characters etc from tweet
        return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t]) | (\w +:\ / \ / \S +)", " ", tweet).split())

    # function to calculate percentage
    def percentage(self, part, whole):
        temp = 100 * float(part) / float(whole)
        return format(temp, '.2f')

    def plotPieChart(self, positive, negative, neutral, searchTerm, noOfSearchTerms):
        labels = ['Positive [' + str(positive) + '%]', 'Neutral [' + str(neutral) + '%]', 'Negative [' + str(negative) + '%]']
        sizes = [positive, neutral, negative]
        colors = ['yellowgreen','gold', 'red',]
        patches, texts = plt.pie(sizes, colors=colors, startangle=90)
        plt.legend(patches, labels, loc="best")
        plt.title('How people are reacting to ' + searchTerm + ' by analyzing ' + str(noOfSearchTerms) + ' Tweets.')
        plt.axis('equal')
        plt.tight_layout()
        plt.show()

if __name__== "__main__":
    sa = SentimentAnalysis()
    sa.DownloadData()


import math
import random
import os
from datetime import datetime

from tornado.web import RequestHandler

from wx.basic import Basic
from common.redis_conn import redis

class UploadHandler(RequestHandler):
    def get(self):
        accessToken = Basic().get_access_token()
        return self.render('upload.html', accessToken=accessToken)

    def post(self):
        files = self.request.files.get('img', None)
        if files:
            if not files:
                return self.write('上传图片不能为空')

            upload_path = os.path.join(os.path.dirname(__file__), 'upload')
            if not os.path.exists(upload_path):
                os.mkdir(upload_path)

            for file in files:
                if file['content_type'] not in ['image/png', 'image/jpeg']:
                    return self.write('上传图片格式有误')

                try:
                    name = datetime.strftime(datetime.now(), '%Y%m%d%H%M%S') + str(math.floor(random.random() * 10))
                    sName = name + '.' + file['filename'].split('.')[1]
                    with open(os.path.join(upload_path, sName), 'wb') as up:
                        up.write(file['body'])

                    accessToken = Basic().get_access_token()
                    cmd = 'curl -F media=@upload/%s "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=%s"' % (
                        sName, accessToken)
                    wxName = json.loads(os.popen(cmd).read())['url']
                    fName = file['filename']

                    redis.hmset('wx:' + name, {'sname': sName, 'wxname': wxName, 'fname': fName})
                except Exception:
                    print(file['filename'] + '文件上传失败')

            return self.write('图片上传成功')


import astropy.units as u
import gala.dynamics as gd
import gala.integrate as gi
import numpy as np
from scipy.optimize import minimize

from .actions_o2gf import get_o2gf_aaf
from .actions_staeckel import get_staeckel_aaf
from .config import vcirc
from .potentials import galpy_potentials, potentials

def _same_actions_objfunc_staeckel(p, pos, vy, potential_name, match_actions):
    vx, vz = p
    w0 = gd.PhaseSpacePosition(pos=pos, vel=[vx, vy, vz] * u.km / u.s)

    o = potentials[potential_name].integrate_orbit(
        w0, dt=1.0 * u.Myr, t1=0, t2=1 * u.Gyr, Integrator=gi.DOPRI853Integrator
    )
    aaf = get_staeckel_aaf(o[::2], galpy_potentials[potential_name])
    actions = aaf["actions"].mean(axis=-1)

    _unit = (u.km / u.s * u.kpc) ** 2
    val = (
        (actions[0] - match_actions[0]) ** 2 + (actions[2] - match_actions[2]) ** 2
    ).to_value(_unit)

    return val

def _same_actions_objfunc_sanders(p, pos, vy, potential_name, match_actions):
    vx, vz = p
    w0 = gd.PhaseSpacePosition(pos=pos, vel=[vx, vy, vz] * u.km / u.s)
    aaf = get_o2gf_aaf(potentials[potential_name], w0, N_max=8)
    actions = aaf["actions"]

    _unit = (u.km / u.s * u.kpc) ** 2
    val = (
        (actions[0] - match_actions[0]) ** 2 + (actions[2] - match_actions[2]) ** 2
    ).to_value(_unit)

    return val

def get_w0s_with_same_actions(fiducial_w0, vy=None, staeckel=False):
    if staeckel:
        _same_actions_objfunc = _same_actions_objfunc_staeckel
    else:
        _same_actions_objfunc = _same_actions_objfunc_sanders

    if vy is None:
        # Default: set to circular velocity
        vy = vcirc

    # First, determine actions for the input orbit in the
    # fiducial potential model. These will be the target action values
    fiducial_actions = []
    for n in range(fiducial_w0.shape[0]):
        if staeckel:
            o = potentials["1.0"].integrate_orbit(
                fiducial_w0[n], dt=0.5, t1=0, t2=2 * u.Gyr
            )  # MAGIC NUMBERS
            fiducial_actions.append(
                get_staeckel_aaf(o, galpy_potentials["1.0"])["actions"]
            )
        else:
            fiducial_actions.append(
                get_o2gf_aaf(potentials["1.0"], fiducial_w0[n], N_max=8)["actions"]
            )

    fiducial_actions = u.Quantity(fiducial_actions).to(u.km / u.s * u.kpc)

    if staeckel:
        fiducial_actions = np.mean(fiducial_actions, axis=-1)

    w0s = {}
    for name in potentials:
        if name == "1.0":
            w0s[name] = fiducial_w0
            continue

        w0s[name] = []
        for n in range(fiducial_w0.shape[0]):
            res = minimize(
                _same_actions_objfunc,
                x0=fiducial_w0.v_xyz.value[[0, 2], n],
                args=(
                    fiducial_w0.pos[n],
                    vy.to_value(u.km / u.s),
                    name,
                    fiducial_actions[n],
                ),
                method="nelder-mead",
                options=dict(maxfev=64),
            )

            if res.fun > 1e-3:
                print(f"{name}, {n}: func val = {res.fun} -- " "Failed to converge")

            w0s[name].append(
                gd.PhaseSpacePosition(
                    pos=fiducial_w0.pos[n],
                    vel=[res.x[0], vy.to_value(u.km / u.s), res.x[1]] * u.km / u.s,
                )
            )

        w0s[name] = gd.combine(w0s[name])

    return w0s


# -*- python -*-

load("@drake//tools/workspace:github.bzl", "github_archive")

def googlebenchmark_repository(
        name,
        mirrors = None):
    github_archive(
        name = name,
        repository = "google/benchmark",
        commit = "v1.5.5",
        sha256 = "3bff5f237c317ddfd8d5a9b96b3eede7c0802e799db520d38ce756a2a46a18a0",  # noqa
        mirrors = mirrors,
    )


import logging
import os
import traceback
from pathlib import Path

import click

from covid_world_scraper import Runner
from covid_world_scraper.constants import (
    DEFAULT_CACHE_DIR,
    DEFAULT_LOG_FILE,
)
from covid_world_scraper.alerts import SlackAlertManager

@click.command(
    help="3-letter country codes for one or more countries. Multiple abbreviations can be "
)
@click.argument('countries', nargs=-1)
@click.option(
    '-a',
    '--all',
    is_flag=True,
    help="Scrape all available countries"
)
@click.option(
    '--alert',
    is_flag=True,
    help="Send scraper status alerts to Slack."
)
@click.option(
    '--cache-dir',
    default=DEFAULT_CACHE_DIR,
    show_default=DEFAULT_CACHE_DIR,
    help="Location to store scraped data files."
)
@click.option(
    '-l',
    '--list-scrapers',
    is_flag=True,
    help="List countries available to scrape."
)
@click.option(
    '--log-file',
    default=DEFAULT_LOG_FILE,
    show_default=DEFAULT_LOG_FILE,
    help="Scraper log file location."
)
@click.option(
    '--headless/--with-browser',
    default=True,
    show_default="--headless",
    help="Enable/disable headless mode for Selenium-based browsers."
)
def cli(countries, all, alert, cache_dir,
        list_scrapers, log_file, headless):
    """Scrape data for one or more countries."""
    # Ensure cache directory exists
    Path(cache_dir).mkdir(parents=True, exist_ok=True)
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(name)-12s - %(message)s',
        datefmt='%m-%d %H:%M',
        filename=log_file,
        filemode='a'
    )
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    formatter = logging.Formatter('%(name)-12s - %(message)s')
    console.setFormatter(formatter)
    logging.getLogger('').addHandler(console)
    logger = logging.getLogger(__name__)

    # Set up alert manager
    alert_manager=None
    if alert:
        try:
            api_key = os.environ['COVID_WORLD_SLACK_API_KEY']
            channel = os.environ['COVID_WORLD_SLACK_CHANNEL']
            alert_msg = "Slack alerts will be sent to #{}.".format(channel)
            alert_manager = SlackAlertManager(api_key, channel)
        except KeyError:
            alert_msg = "WARNING - Slack alerts will not be sent.\n" + \
                "Please ensure you've configured the below environment variables:\n" + \
                "COVID_WORLD_SLACK_API_KEY=YOUR_API_KEY\n" + \
                "COVID_WORLD_SLACK_CHANNEL=channel-name\n\n"
        finally:
            logger.warning(alert_msg)

    runner = Runner(alert_manager=alert_manager)

    if list_scrapers:
        click.echo('Available country scrapers:')
        for country in runner.list_countries():
            msg = '- {}'.format(country)
            click.echo(msg)
    else:
        kwargs = {
            'cache_dir': cache_dir,
            'headless_status': headless,
            'filter': countries,
        }
        try:
            runner.run(**kwargs)
            if alert and alert_manager:
                runner.send_alerts()
        except Exception as e:
            traceback_str = ''.join(traceback.format_tb(e.__traceback__))
            logger.error("ERROR: A fatal error occurred while running scrapers or sending alerts!!!")
            logger.error(traceback_str)


#!/usr/bin/python3
import json
import time
import sys
import socket
import urllib.request

with open('/etc/energy-monitoring-with-graphite/clairvoyance/settings.json') as json_data:
    d = json.load(json_data)
    remote_server = d['remoteServer']
    remote_port = d['remotePort']
    addresses = d['addresses']
    metrics = d['metrics']
    paths = d['paths']
    interval = d['interval']
    server = d['server']
    port = d['port']

t_prev = ''
while True:
    t_start = time.time()
    t = ''
    timestamp_prev = -1
    for a in addresses:
        f = urllib.parse.urlencode({"formula": str(",".join(metrics))})
        a = urllib.parse.urlencode({"address": a})
        url = "http://" + remote_server + ":" + str(remote_port) + "/forecast?" + f + "&" + a
        print("url: " + url)
        try:
            j = json.loads(urllib.request.urlopen(url).read())
        except:
            print('clairvoyance: could not connect to server')
            continue

        #print("anser: " + str(json.dumps(j, indent=2)))
        n = 0
        timestamp = j["time"]
        for m in metrics:
            a_ = a.replace(".", "_").replace(" ", "_").replace("address=","").replace("+", "_").replace("%2C", "").replace("%5C", "")
            p = paths[n] + "." + a_

            value = ""
            t_value = 0
            try:
                data = j[m]
                for d in data:
                    if timestamp < d["time"]:
                        value = d["value"]
                        t_value = d["time"]
                        break
            except:
                print('clairvoyance: got an error')
                pass

            v = str(value)

            if p != '' and t_value != 0:
                t = t + p + ' ' + v + ' ' + str(int(t_value)) + '\n'

            n = n + 1

    if t != '' and t != t_prev:
        try:
            print("Sending " + str(t))
            s = socket.socket()
            s.connect((server, port))
            s.send(t.encode('ascii'))
            s.close()
            t_prev = t
        except:
            print(time.ctime(t_start) + ' clairvoyance: Error: Could not send message.')

    t_now = time.time()
    if (t_now - t_start) < interval:
        time.sleep(interval - (t_now - t_start))


"""
Module to create map.
"""

from random import uniform
from math import sqrt, sin, cos, radians, asin
from collections import Counter

import folium
from geopy.exc import GeocoderTimedOut, GeocoderUnavailable, GeocoderServiceError
from geopy.extra.rate_limiter import RateLimiter
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="movie-near")
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)

def distance(point1: tuple[float, float], point2: tuple[float, float]) -> float:
    """
    Function calculates distance between two points on a map.
    This function have only to estimate distance.

    point1: first point
    point2: second point
    >>> distance((0, 0), (0, 0))
    0.0
    >>> distance((0, 0), (0, 1))
    111194.92664455874
    """
    if not point1 or not point2:
        return float("inf")

    lat1, lon1 = point1
    lat2, lon2 = point2

    lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])

    dlon = lon2 - lon1
    dlat = lat2 - lat1

    num = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
    return asin(sqrt(num)) * 2 * 6371e3

def get_coordinates(location: str) -> tuple[float, float]:
    """
    Function returns coodrinated of location by address name.

    location: location name
    >>> list(map(int, get_coordinates("Ternopil, Ukraine")))
    [49, 25]
    >>> list(map(int, get_coordinates("Kiev, Ukraine")))
    [50, 30]
    >>> get_coordinates("")
    """
    try:
        coords = geolocator.geocode(location)
    except (GeocoderUnavailable, GeocoderTimedOut, GeocoderServiceError) as error:
        print(f"Error {error.strerror} in: {location}")
        coords = None

    return (coords.latitude, coords.longitude) if coords else None

def unificate_points(points: list[tuple[float, float]]) -> list[tuple[float, float]]:
    """
    Function unificate points with same coordinates.
    Also functuion removes points with None coordinates.

    points: list of points
    >>> len(unificate_points([(0, 0), None])) == 1
    True
    """
    distribution = 0.005
    unificated = []
    counts = Counter(points)

    for point in points:
        if not point:
            continue

        if counts[point] != 1:
            delta_x = uniform(-distribution, distribution)
            delta_y = uniform(-distribution, distribution)
            point = (point[0] + delta_x, point[1] + delta_y)

        unificated.append(point)

    return unificated

def nearest_points(
    origin: tuple[float, float],
    locations: list[str],
    n_first: int) -> list[tuple[float, float]]:
    """
    Function returns n_first nearest points to origin.

    origin: origin point
    locations: list of locations
    n_first: number of nearest points

    >>> pnt = nearest_points((49.553802, 25.594092),\
    ["Paris, France", "Kiev, Ukraine"], 1)[0]
    >>> list(map(int, pnt)) == [50, 30]
    True
    """
    locations = [get_coordinates(location) for location in locations]
    locations = unificate_points(locations)
    locations.sort(key=lambda location: distance(origin, location))

    return locations[:min(n_first, len(locations))]

def create_map(
    dest: str,
    user_coords: tuple[float, float],
    points: list[tuple[int, int]]) -> None:
    """
    Function writes map in .html file.

    dest: path to map file
    user_coords: user coordinates
    points: list of points to show on map

    >>> create_map("", (49.553802, 25.594092), [(50.4501, 30.5234)])
    Error: invalid destination path
    """
    if not isinstance(dest, str) or len(dest) == 0:
        print("Error: invalid destination path")
        return

    wmap = folium.Map(location=list(user_coords), zoom_start=10)

    main_group = folium.FeatureGroup(name="World map")
    markers_group = folium.FeatureGroup(name="Markers")
    ragius_group = folium.FeatureGroup(name="Radius")

    # draw radius from user to farthest point
    ragius_group.add_child(folium.Circle(
        location=[user_coords[0], user_coords[1]],
        radius=distance(user_coords, points[-1]) + 1e5,
        color="green",
        fill=True,
        fill_color="green"
    ))

    # add user marker
    markers_group.add_child(folium.Marker(
        location=[user_coords[0], user_coords[1]],
        icon=folium.Icon(color="red")
    ))

    # add films markers
    for lat, lon in points:
        markers_group.add_child(folium.Marker(location=[lat, lon], icon=folium.Icon()))

    wmap.add_child(ragius_group)
    wmap.add_child(main_group)
    wmap.add_child(markers_group)

    wmap.save(dest)

if __name__ == "__main__":
    import doctest
    print(doctest.testmod())


#MultiWindowHandle on Yatra.Com
#code by sunil savale
#date- 20-09-2021

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as ec
import time

#Launch the Browser
class Windows():
    def MultiWindows(self):
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install())
        driver.maximize_window()

        #Navigate the URL
        driver.get("https://www.yatra.com/")

        # get the parent window ids
        parent_window = driver.current_window_handle
        print(parent_window)
        time.sleep(5)

        # Click on Airline Web it will give us multiple airline
        window_handle = driver.find_element(By.XPATH, ".//img[contains(@src,'/2020/Jul/5b10fea20065899f659cec0ecee16bca.jpg')]").click()
        all_handles = driver.window_handles

        # get all the Window handles in the form of ids
        print(all_handles)

        #Switch to window and click on particular web element which is inspect
        for handle in all_handles:
            if handle != parent_window:
                driver.switch_to.window(handle)
                time.sleep(3)
                driver.find_element(By.XPATH, ".//a[@href='https://www.goair.in/plan-my-trip/web-check-in']").click()
                # wait = WebDriverWait(driver, 15)
                # input = wait.until(ec.visibility_of_element_located((By.XPATH, ".//input[@id='LastName']")))
                # input.click()
                # input.send_keys("Patil")
                driver.close()
                break
                #switch to parent window , back to the main window

        time.sleep(5)
        driver.switch_to.window(parent_window)
        driver.find_element(By.XPATH, ".//img[contains(@src,'/2020/Jul/5b10fea20065899f659cec0ecee16bca.jpg')]").click()

switch = Windows()
switch.MultiWindows()


import time
from datetime import timedelta

from flask import current_app
from sentry_sdk import capture_message

from twitter_bastion.celery import celery_app
from twitter_bastion.extensions import db
from twitter_bastion.models import Tweet
from twitter_bastion.services import (
    get_tweet_count_percent_difference_for_subsequent_time_windows,
)

@celery_app.task
def archive_old_tweets():
    max_unarchived_tweets = current_app.config['MAX_UNARCHIVED_TWEETS']

    for record in db.graph.run(
        'MATCH (t:Tweet) '
        'WHERE NOT t:Archived '
        'RETURN t '
        'ORDER BY t.created_at DESC '
        'SKIP $max_unarchived_tweets',
        max_unarchived_tweets=max_unarchived_tweets
    ):
        archivable_tweet = Tweet()
        archivable_tweet.id = record['t']['id']
        archivable_tweet.archived = True
        db.graph.push(archivable_tweet)

@celery_app.task
def monitor_tweet_count_variation():
    time_window_length_in_minutes = (
        current_app.config['TIME_WINDOW_LENGTH_IN_MINUTES']
    )
    time_window_interval_in_minutes = (
        current_app.config['TIME_WINDOW_INTERVAL_IN_MINUTES']
    )
    warning_threshold_difference = (
        current_app.config['WARNING_THRESHOLD_DIFFERENCE']
    )
    # while True:
    difference = (
        get_tweet_count_percent_difference_for_subsequent_time_windows(
            time_window_length_in_minutes, time_window_interval_in_minutes
        )
    )
    if difference >= warning_threshold_difference:
        capture_message(
            f'Increase by {difference}% tweets since last time window.'
        )
    elif difference <= -warning_threshold_difference:
        capture_message(
            f'Reduction by {difference}% tweets since last time window.'
        )


import re
from ufwi_conf.backend.output_parser.errors import NoMatch

class Field(object):
    def __init__(self, regex, is_multiline):
        self.regex = re.compile(regex)
        self.is_multiline = is_multiline

class OutputStructure(object):
    WHITESPACE = re.compile(r'^\s*$')
    def __init__(self, title_regex, errors={}):
        """
        In case of nested OutputStructures, only define errors at top level
        """
        self.title_regex = title_regex
        self.title_re = re.compile(title_regex)
        self.errors = errors
        self.fields = {}
        self.sections = frozenset()

    def addField(self, name, regex, multiline=False):
        self.fields[name] = Field(regex, multiline)

    def matchError(self, line, arg):
        for regex in self.errors.keys():
            if re.match(regex, line):
                raise self.errors[regex](arg)

    def matchTitle(self, line):
        if self.title_re.search(line):
            return True
        return False

    @staticmethod
    def matchWhiteSpace(line):
        if OutputStructure.WHITESPACE.search(line):
            return True
        return False

    def getField(self, line):
        for name, field in self.fields.iteritems():
            match = field.regex.search(line)
            if match:
                #errors here mean no regex groups was defined in the regex
                return name, match.groups()[0], field.is_multiline
        raise NoMatch("No name matched", line)


"""Test frostfree class"""
import time
import unittest
from datetime import datetime
from threading import Thread
from typing import Optional

from src.localStorage.persistence.persistence import Persistence
from src.shared.enum.state import State
from src.zone.dto.info_frostfree import InfoFrostfree
from src.zone.frostfree import Frostfree
from src.zone.zone import Zone
from test.helpers.patchs.config_patch import ConfigPatch
from test.helpers.patchs.persistence_patch import PersistencePatch
from test.helpers.patchs.zone_patch import ZonePatch

ZONE1 = "zone1"

class TestFrostFree(unittest.TestCase):

    def setUp(self):
        ZonePatch.start_patch(self)
        PersistencePatch.start_patch(self)
        ConfigPatch.start_patch(self)

        self.thread = None
        self.zone: Optional[Zone] = None
        self.frostfree: Optional[Frostfree] = None

    def tearDown(self) -> None:
        ZonePatch.stop_patch(self)
        if self.frostfree:
            self.frostfree.stop_loop()
        if self.zone:
            self.zone.stop_loop()
        if self.thread:
            self.thread.join()

        PersistencePatch.stop_patch(self)
        ConfigPatch.stop_patch(self)

    def start_thread(self):
        self.thread = Thread(target=self.run_frostfree())
        self.thread.start()
        time.sleep(0.5)

    def run_frostfree(self):
        self.zone = Zone(1)
        time.sleep(0.5)
        self.frostfree = Frostfree([self.zone])

    def test_restore(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp() + 120))

        self.start_thread()

        self.assertNotEqual(self.frostfree.end_date, None)
        self.assertEqual(self.zone.current_state, State.FROSTFREE)

    def test_restore_end_date_expired(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp()))

        self.start_thread()

        self.assertEqual(self.frostfree.end_date, None)
        self.assertNotEqual(self.zone.current_state, State.FROSTFREE)

    def test_can_stopped(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp() + 120))

        self.start_thread()
        self.frostfree.stop()

        self.assertEqual(self.frostfree.end_date, None)
        self.assertNotEqual(self.zone.current_state, State.FROSTFREE)

    def test_get_data(self):
        Persistence().set_frostfree_end_date(datetime.fromtimestamp(datetime.now().timestamp() + 120))
        self.start_thread()
        info_frostfree = InfoFrostfree(self.frostfree.end_date)
        result = self.frostfree.get_data()

        self.assertEqual(info_frostfree, result)

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


from collections import defaultdict

n, m = map(int, input().split())

edges = []
parent = [i for i in range(n)]

for _ in range(m):
    a, b, c = map(int, input().split())
    edges.append((a-1, b-1, c))

def find(i):
    while i != parent[parent[i]]:
        parent[i] = parent[parent[i]]
        i = parent[i]
    return i

def union(x, y):
    p_x = find(x)
    p_y = find(y)
    parent[p_y] = p_x

def is_connected(x, y):
    p_x = find(x)
    p_y = find(y)
    return p_x == p_y

# build MST
tree = defaultdict(list)

edges.sort(key=lambda x: x[2])
for a, b, c in edges:
    if not is_connected(a, b):
        union(a, b)
        tree[a].append((b, c))
        tree[b].append((a, c))

ans = [0]*(2*m)

# Run DFS to count the number of times an edge is used
# as weights of all edges is different, hence each weight maps to a particular children

def dfs(src, p=-1):
    total = 1
    for v, c in tree[src]:
        if v != p:
            children = dfs(v, src)

            # children => nodes right to edge, n - children => nodes left to edge
            ans[c] += (n - children)*children

            total += children
    return total

dfs(0)

res = 0
for i in range(len(ans)):
    res += ans[i] * (1 << i)

print(str(bin(res))[2:])

Problem Solving/Algorithms/Graph Theory/Roads in HackerLand.py


import numpy as np
import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_validate

fruits = np.load('fruits_300.npy')
fruits_2d = fruits.reshape(-1, 100*100)
print(fruits.shape)

apple = fruits[0:100].reshape(-1, 100*100)
pineapple = fruits[100:200].reshape(-1, 100*100)
banana = fruits[200:300].reshape(-1, 100*100)

print(apple.shape)

# 샘플 평균의 히스토그램 - 픽셀값 분석
plt.hist(np.mean(apple, axis=1), alpha=0.8)
plt.hist(np.mean(pineapple, axis=1), alpha=0.8)
plt.hist(np.mean(banana, axis=1), alpha=0.8)
plt.legend(['apple', 'pineapple', 'banana'])
plt.show()

# 픽셀 평균의 히스토그램
fig, axs = plt.subplots(1, 3, figsize=(20, 5))
axs[0].bar(range(10000), np.mean(apple, axis=0))
axs[1].bar(range(10000), np.mean(pineapple, axis=0))
axs[2].bar(range(10000), np.mean(banana, axis=0))
plt.show()

# 평균 이미지 그리기
apple_mean = np.mean(apple, axis=0).reshape(100, 100)
pineapple_mean = np.mean(pineapple, axis=0).reshape(100, 100)
banana_mean = np.mean(banana, axis=0).reshape(100, 100)

fig, axs = plt.subplots(1, 3, figsize=(20, 5))
axs[0].imshow(apple_mean, cmap='gray_r')
axs[1].imshow(pineapple_mean, cmap='gray_r')
axs[2].imshow(banana_mean, cmap='gray_r')
plt.show()

# 평균과 가까운 사진 고르기
abs_diff = np.abs(fruits - apple_mean)
abs_mean = np.mean(abs_diff, axis=(1, 2))
print(abs_mean.shape)

apple_index = np.argsort(abs_mean)[:100]
fig, axs = plt.subplots(10, 10, figsize=(10, 10))
for i in range(10):
	for j in range(10):
		axs[i, j].imshow(fruits[apple_index[i*10 + j]], cmap='gray_r')
		axs[i, j].axis('off')
plt.show()

km = KMeans(n_clusters=3, random_state=42)
km.fit(fruits_2d)

print(km.labels_)

print(np.unique(km.labels_, return_counts=True))

def draw_fruits(arr, ratio=1):
	n = len(arr)	# n은 샘플 개수
	# 한 줄에 10개씩 이미지를 그립니다. 샘플 개수를 10으로 나누어 전체 행 개수를 계산
	rows = int(np.ceil(n / 10))
	# 행이 1개이면 열 개수는 샘플 개수. 그렇지 않으면 10개
	cols = n if rows < 2 else 10
	fig, axs = plt.subplots(rows, cols, figsize=(cols*ratio, rows*ratio), squeeze=False)

	for i in range(rows):
		for j in range(cols):
			if i*10 + j < n:
				axs[i, j].imshow(arr[i*10 + j], cmap='gray_r')
			axs[i, j].axis('off')
	plt.show()

draw_fruits(fruits[km.labels_==0])

draw_fruits(km.cluster_centers_.reshape(-1, 100, 100), ratio=3)

print(km.transform(fruits_2d[100:101]))

print(km.predict(fruits_2d[100:101]))

draw_fruits(fruits[100:101])

print(km.n_iter_)

# 최적의 K 찾기(클러스의 수 정하기)
inertia = []				# interia : 군집 내 샘플들과 군집 중심점의 거리 차이 --> 값이 적다는 것은 중심점 주변에 밀집해 있다는 뜻
for k in range(2, 7):
	km = KMeans(n_clusters=k, random_state=42)
	km.fit(fruits_2d)
	inertia.append(km.inertia_)

plt.plot(range(2, 7), inertia)
plt.xlabel('k')
plt.ylabel('inertia')
plt.show()

pca = PCA(n_components=50)
pca.fit(fruits_2d)

print(pca.components_.shape)

draw_fruits(pca.components_.reshape(-1, 100, 100))

print(fruits_2d.shape)

fruits_pca = pca.transform(fruits_2d)
print(fruits_pca.shape)

# 재구성
fruits_inverse = pca.inverse_transform(fruits_pca)
print(fruits_inverse.shape)

fruits_reconstruct = fruits_inverse.reshape(-1, 100, 100)

# 설명된 분산
print(np.sum(pca.explained_variance_ratio_))

plt.plot(pca.explained_variance_ratio_)

# 분류기와 함께 사용하기
lr = LogisticRegression()
target = np.array([0] * 100 + [1] * 100 + [2] * 100)

scores = cross_validate(lr, fruits_2d, target)
print(np.mean(scores['test_score']))
print(np.mean(scores['fit_time']))

scores = cross_validate(lr, fruits_pca, target)
print(np.mean(scores['test_score']))
print(np.mean(scores['fit_time']))

pca = PCA(n_components=0.5)
pca.fit(fruits_2d)
print(pca.n_components_)

fruits_pca = pca.transform(fruits_2d)
print(fruits_pca.shape)

scores = cross_validate(lr, fruits_pca, target)
print(np.mean(scores['test_score']))
print(np.mean(scores['fit_time']))

# 군집과 함께 사용하기
km = KMeans(n_clusters=3, random_state=42)
km.fit(fruits_pca)

print(np.unique(km.labels_, return_counts=True))

# 시각화
for label in range(0, 3):
	data = fruits_pca[km.labels_ == label]
	plt.scatter(data[:, 0], data[:, 1])

plt.legend(['apple', 'banana', 'pineapple'])
plt.show()


from django.conf.urls import url
from . import controllers

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^order-add', controllers.orderAdd),
    url(r'^department-add', controllers.departmentAdd),
    url(r'^orderer-add', controllers.ordererAdd),
    url(r'^product-add', controllers.productAdd),
    url(r'^fill-db-from-json', controllers.fillDataBaseFromJSON),

    url(r'^order-update', controllers.orderUpdate),
    url(r'^department-update', controllers.departmentUpdate),
    url(r'^product-update', controllers.productUpdate),

    url(r'^order-del', controllers.orderDelete),
    url(r'^department-del', controllers.departmentDelete),
    url(r'^orderer-del', controllers.ordererDelete),
    url(r'^product-del', controllers.productDelete),

    url(r'^orders', controllers.orders),
    url(r'^orderers', controllers.orderers),
    url(r'^products', controllers.products),
    url(r'^departments', controllers.departments),
    url(r'^$', controllers.index)
]


from tensorflow.keras.models import save_model
from tensorflow.keras.callbacks import EarlyStopping
from sklearn.model_selection import train_test_split
from helpers.ml import (extract_features, create_preprocess_pipeline, build_model,
                        split_dataframe_on_columns, transform_rnn_sequences,
                        create_target_scaler)
from helpers.database import InfluxdbQuery
from config import HOME
import pandas as pd
import joblib
import os

class MLTools():

    lookback = 48
    lookahead = 24
    binary_dir = os.path.join(HOME, 'btc_guru', 'bin')
    preprocess_pipeline_path = os.path.join(binary_dir, 'preprocess_pipeline.pkl')
    target_scaler_path = os.path.join(binary_dir, 'target_scaler.pkl')
    rnn_model_path = os.path.join(binary_dir, 'rnn_model.h5')

    @staticmethod
    def train_model() -> None:
        target_scaler = create_target_scaler()
        preprocess_pipeline = create_preprocess_pipeline()
        input_data = InfluxdbQuery(dict(fields='open,high,low,close,volume', result_limit=100000)).query()
        input_data = input_data.sort_values(by='time', ascending=True).set_index('time')
        input_data_features = extract_features(input_data, lookahead=MLTools.lookahead)
        input_data_features = input_data_features.pipe(lambda df: df[~pd.isna(df['target'])])
        dataframe_x, dataframe_y = split_dataframe_on_columns(input_data_features, ['target'])
        X_train, X_val, y_train, y_val = train_test_split(dataframe_x.values, dataframe_y.values,
                                                          test_size=0.03, random_state=42, shuffle=False)

        X_train = preprocess_pipeline.fit_transform(X_train)
        X_val = preprocess_pipeline.transform(X_val)

        y_train = target_scaler.fit_transform(y_train)
        y_val = target_scaler.transform(y_val)

        X_train, y_train = transform_rnn_sequences(X_train, y_train, lookback=MLTools.lookback,
            lookahead=MLTools.lookahead)
        X_val, y_val = transform_rnn_sequences(X_val, y_val, lookback=MLTools.lookback,
            lookahead=MLTools.lookahead)

        joblib.dump(preprocess_pipeline, MLTools.preprocess_pipeline_path)
        joblib.dump(target_scaler, MLTools.target_scaler_path)

        rnn_model = build_model(input_shape=X_train.shape[1:], output_neurons=y_train.shape[1])
        rnn_model.fit(X_train, y_train,
                      validation_data=(X_val, y_val),
                      epochs=30,
                      batch_size=32,
                      callbacks=[EarlyStopping(restore_best_weights=True, patience=2, monitor='val_loss')])

        save_model(rnn_model, MLTools.rnn_model_path)