#coding: utf8

# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    prediction_from_db.py                              :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: Zhenkun <zhenkun91@outlook.com>            +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2020/02/19 04:01:00 by Kay Zhou          #+#    #+#              #
#    Updated: 2020/12/25 15:12:58 by Zhenkun          ###   ########.fr        #
#                                                                              #
# **************************************************************************** #

from SQLite_handler import *
from tqdm import tqdm
import os

# 0 > JB; 1 > DT
US_states = ['NY', 'DC', 'IN', 'AR', 'WY', 'ME', 'TX', 'NH', 'CO', 'CA', 'IL',
             'WA', 'VA', 'FL', 'MA', 'OR', 'AZ', 'MT', 'MN', 'NE', 'TN', 'OH',
             'NJ', 'NV', 'KY', 'UT', 'NC', 'SC', 'PA', 'NM', 'KS', 'GA', 'MI',
             'WI', 'AK', 'MS', 'MD', 'LA', 'HI', 'MO', 'AL', 'CT', 'OK', 'IA',
             'WV', 'RI', 'SD', 'VT', 'ND', 'ID', 'DE']
# USERS = pd.read_csv("disk/users-face/2020-04-02.csv").set_index("uid")
# USERS_STATE = pd.read_csv("disk/users-location/2020-04-02.csv",
#                           usecols=["uid","state"],
#                           error_bad_lines=False).set_index("uid")
# USERS_STSTE_GENDER_AGE = USERS.join(USERS_STATE, how="inner")
# SET_USERS = set(USERS_STSTE_GENDER_AGE.index)
# SET_USERS = set(USERS_STATE.index)

def save_bots(out_name):
    set_users = set()
    with open(out_name, "w") as f:
        months = ["10", "09", "08", "07", "06"]
        for m in months:
            print(m)
            for line in open(f"data/2020{m}-tweets-prediction.txt"):
                d = line.strip().split(",")
                if d[3] != "None" and d[1] not in set_users:
                    f.write(d[1] + "\n")
                    set_users.add(d[1])

def load_bots(in_name):
    all_bots = set()
    for line in open(in_name):
        all_bots.add(line.strip())
    print("# of bots:", len(all_bots))
    return all_bots
# ALL_BOTS = load_bots("data/users-profile/20201010bots.txt")

def save_user_snapshot_json(in_names, model_version="v1", p=0.5):
    # global ALL_BOTS
    set_bots = set()
    for in_name in in_names:
        print("save_user_snapshot_json", in_name)
        dict_date_users = {}
        for line in tqdm(open(in_name, encoding="utf8"), ascii=True):
            d = line.strip().split(",")
            # 先不查重tweet_id
            uid = d[0]
            # if uid in ALL_BOTS or d[3] != "None":
            #     continue
            date = d[1]
            source = d[2]
            if source != "None" or uid in set_bots:
                set_bots.add(uid)
                continue
            proba = float(d[3])
            query = d[4].lower()
            # if not ("trump" in query or "biden" in query or "~" in query):
            #     continue
            # or include all queries

            if date not in dict_date_users:
                dict_date_users[date] = {}
            if uid not in dict_date_users[date]:
                dict_date_users[date][uid] = [0, 0]

            # 0 for Biden, 1 for Trump
            if proba < (1 - p):
                dict_date_users[date][uid][0] += 1
            elif proba >= p:
                dict_date_users[date][uid][1] += 1
            # else:
            #     dict_date_users[date][uid][2] += 1

        for date, dict_uid in dict_date_users.items():
            print("save", date)
            f_name = f"data/users-day-{model_version}-{p}/{date}.json"
            if os.path.exists(f_name):
                print(f_name, "已经存在。")
            else:
                json.dump(dict_uid, open(f_name, "w"))

def save_user_snapshot(sess, now):
    users = {}
    # for t in get_tweets(sess, now, now.add(days=1)):
    for t in get_tweets_proba(sess, now, now.add(days=1)):
        uid = t.user_id
        if uid not in users:
            users[uid] = [0, 0] # Biden and Trump
        users[uid][t.camp] += 1
    print("# of users:", len(users))
    with open(f"data/users-day-0.66/{now.to_date_string()}.csv", "w") as f:
        f.write("uid,0,1\n")
        for u, v in users.items():
            f.write(f"{u},{v[0]},{v[1]}\n")

def save_user_csv(sess, start, end):
    for dt in pendulum.period(start, end.add(days=-1)):
        print(dt)
        save_user_snapshot(sess, dt)

# def read_users_from_csv(in_name):
#     print("Reading users from csv ...", in_name)
#     users = pd.read_csv(in_name).set_index("uid").T.to_dict()
#     _users = {}
#     for u, v in users.items():
#         _users[u] = np.array([v["0"], v["1"]])
#     print("# of users:", len(_users))
#     return _users
def read_users_from_csv(in_name):
    print("Loading users from:", in_name)
    users = {}
    for line in tqdm(open(in_name)):
        if line.startswith("uid"):
            continue
        uid, v0, v1 = line.strip().split(",")
        if uid in all_bots:
            continue
        users[uid] = [int(v0), int(v1)] # 0 for Biden, 1 for Trump
    print("# of users:", len(users))
    return users

def read_users_from_json(in_name):
    print("Loading users from:", in_name)
    if os.path.exists(in_name):
        users = json.load(open(in_name))
    else:
        print("Not exist")
        users = {}
    print("# of users:", len(users))
    return users

# def read_users_from_csv_from_uids(in_name, set_uids):
#     print("Reading users from csv ...", in_name)
#     users = pd.read_csv(in_name).set_index("uid").T.to_dict()
#     _users = {}
#     for u, v in users.items():
#         if u in set_uids:
#             _users[u] = np.array([v["0"], v["1"]])
#     print("# of users:", len(_users))
#     return _users
def read_users_from_csv_from_uids(in_name, set_uids):
    print("Loading users from:", in_name)
    users = {}
    for line in open(in_name):
        if line.startswith("uid"):
            continue
        uid, v0, v1 = line.strip().split(",")
        if uid in set_uids:
            users[uid] = [int(v0), int(v1)]
    print("# of users:", len(users))
    return users

def union_users_from_dict(users_groups):
    all_users = {}
    for users in users_groups:
        for u, v in users.items():
            if u not in all_users:
                all_users[u] = v
            else:
                all_users[u][0] += v[0]
                all_users[u][1] += v[1]
    return all_users

def union_users_from_yesterday_and_today(yes_users, today_users):
    all_users = yes_users
    for u, v in today_users.items():
        if u not in all_users:
            all_users[u] = [0, 0]
        all_users[u][0] += v[0]
        all_users[u][1] += v[1]
    return all_users

def write_union_users_csv(union_users_dict, out_dir, dt):
    print("Saving ...", f"disk/{out_dir}/{dt}.csv")
    with open(f"disk/{out_dir}/{dt}.csv", "w") as f:
        f.write("uid,0,1\n")
        for u, v in union_users_dict.items():
            f.write(f"{u},{v[0]},{v[1]}\n")

def write_union_users_json(union_users_dict, out_dir, dt):
    print("Saving ...", f"data/{out_dir}/{dt}.csv")
    # set_users_county = set([str(json.loads(line.strip())["id"]) for line in open("data/County_users.lj")])
    # union_users_dict_county = {uid: v for uid, v in union_users_dict.items() if uid in set_users_county}
    with open(f"data/{out_dir}/{dt}.json", "w") as f:
        json.dump(union_users_dict, f)
        # json.dump(union_users_dict_county, f)

def write_union_users_csv_v2(union_users_dict, out_dir, dt):
    # 需要结合已有数据，减少数据保存量
    num2label = {
        0: "JB",
        1: "DT",
    }
    print("saving ...", f"disk/{out_dir}/{dt}.csv")
    rsts = []
    if union_users_dict:
        for u, v in union_users_dict.items():
            if u in SET_USERS:
                max_i = v.argmax()
                rsts.append({"uid": u, "Camp": num2label[max_i]})

        _users = pd.DataFrame(rsts).set_index("uid").join(
            USERS_STSTE_GENDER_AGE, how="inner")
        _users.to_csv(f"disk/{out_dir}/{dt}.csv")

def get_share_from_users_dict(users_dict):
    counts = {
        0: 0,  # Biden
        1: 0,  # Trump
        2: 0,  # Undecided
    }
    for _, v in users_dict.items():
        if v[0] > v[1]:
            counts[0] += 1
        elif v[0] < v[1]:
            counts[1] += 1
        elif v[0] == v[1]:
        # elif v[0] == v[1] and v[0] > 0:
            counts[2] += 1
    return counts

def get_share_from_csv(csv_name):
    users_dict = read_users_from_csv(csv_name)
    return get_share_from_users_dict(users_dict)

def calculate_window_share_size_1(start, end, save_csv=True):
    rsts = []
    for dt in pendulum.period(start, end):
        _u = read_users_from_csv(f"data/users-day/{dt.to_date_string()}.csv")
        rst = get_share_from_users_dict(_u)
        rst["dt"] = dt.to_date_string()
        print(rst)
        rsts.append(rst)

    if save_csv:
        rsts = pd.DataFrame(rsts).set_index("dt")
        rsts.to_csv(f"data/csv/results-1day-from-{start.to_date_string()}-to-{end.to_date_string()}.csv")

def calculate_window_share(start, end, win=14, model_version="v1", p=0.5, save_users=True):
    """VIP 1

    Args:
        start ([type]): [description]
        end ([type]): [description]
        win (int, optional): [description]. Defaults to 14.
        save_csv (bool, optional): [description]. Defaults to True.
    """
    rsts = []
    users_cache = {}

    for dt in pendulum.period(start, end):

        if dt == start:
            continue
        # print(dt)
        win_dts = pendulum.period(dt.add(days=-win), dt.add(days=-1))

        users_groups = []
        for win_dt in win_dts:
            win_dt_str = win_dt.to_date_string()
            if win_dt_str in users_cache:
                _u = users_cache[win_dt_str]
            else:
                # if win_dt_str < "2020-05-01":
                #     _u = read_users_from_json(f"data/users-day-onlyTB/{win_dt_str}.json")
                # else:
                #     _u = read_users_from_json(f"data/users-day/{win_dt_str}.json")
                if win_dt_str < "2020-04-01":
                    _u = read_users_from_json(f"data/users-day-v2-{p}/{win_dt_str}.json")
                else:
                    _u = read_users_from_json(f"data/users-day-{model_version}-{p}/{win_dt_str}.json")
                users_cache[win_dt_str] = _u
            users_groups.append(_u)

        users_cache.pop(dt.add(days=-win).to_date_string())

        union_users_dict = union_users_from_dict(users_groups)
        if save_users and dt.day_of_week == 1:
            # write_union_users_json(union_users_dict, f"users-{win}days", dt.to_date_string())
            # write_union_users_json(union_users_dict, f"users-{win}days-onlyTB", dt.to_date_string())
            write_union_users_json(union_users_dict, f"users-{win}days-{model_version}-{p}", dt.to_date_string())

        rst = get_share_from_users_dict(union_users_dict)
        rst["dt"] = dt.to_date_string()
        print(rst)
        rsts.append(rst)

    rsts = pd.DataFrame(rsts).set_index("dt")
    rsts = rsts.rename(columns={0: "Biden", 1: "Trump", 2: "Undecided"})
    rsts.to_csv(f"data/csv/{win}days-from-{start.to_date_string()}-to-{end.to_date_string()}-{model_version}-{p}.csv")

def calculate_cumulative_share(start, end, super_start_month="01", model_version="v1", p=0.5, save_users=False):
    """VIP 2

    Args:
        start ([type]): [description]
        end ([type]): [description]
        super_start_month (str, optional): [description]. Defaults to "01".
        save_users (bool, optional): [description]. Defaults to True.
        save_db (bool, optional): [description]. Defaults to False.

    Returns:
        [type]: [description]
    """
    # from super_start (include) to -1
    if super_start_month == "01":
        super_start = pendulum.datetime(2020, 1, 1)
    elif super_start_month == "06":
        super_start = pendulum.datetime(2020, 6, 1)
    else:
        super_start = "Unknown super_start_month"

    rsts = []
    # super_start = start
    yesterday_users = None

    for dt in pendulum.period(start, end):
        if dt <= super_start:
            print("Error: start <= super_start.")
            return -1

        elif dt == super_start.add(days=1):  # 从第二天开始
            super_start_string = super_start.to_date_string()
            if super_start_string < "2020-04-01":
                union_users_dict = read_users_from_json(f"data/users-day-v2-{p}/{super_start_string}.json") # 只有v2所有日期都有
            else:
                union_users_dict = read_users_from_json(f"data/users-day-{model_version}-{p}/{super_start_string}.json")
            # union_users_dict = read_users_from_json(f"data/users-day-onlyTB-{p}/{super_start.to_date_string()}.json")
            # if dt.to_date_string() < "2020-09-01":
            #     union_users_dict = read_users_from_json(f"data/users-day-{p}/{super_start.to_date_string()}.json")
            # else: qwer    er
            #     union_users_dict = read_users_from_json(f"data/users-day-v1-{p}/{super_start.to_date_string()}.json")
            print("Loading data on super_start ...", super_start_string)
            # write_union_users_json(union_users_dict, f"users-cumFrom{super_start_month}", dt.to_date_string())
            # write_union_users_json(union_users_dict, f"users-cumFrom{super_start_month}-onlyTB", dt.to_date_string())
            write_union_users_json(union_users_dict, f"users-cumFrom{super_start_month}-{model_version}-{p}", dt.to_date_string())

        else:
            # just from the cumulative yesterday
            # So I must have the yesterday's cumulative csv
            yes_str = dt.add(days=-1).to_date_string()

            if yesterday_users is None:
                print("Loading yesterday users' json at （载入初始数据）", dt.add(days=-1))
                yesterday_users = read_users_from_json(
                    # f"disk/users-cumFrom{super_start_month}/{dt.add(days=-1).to_date_string()}.json")
                    # f"disk/users-cumFrom{super_start_month}-onlyTB/{dt.add(days=-1).to_date_string()}.json")
                    f"disk/users-cumFrom{super_start_month}-{model_version}-{p}/{yes_str}.json")

            if yes_str < "2020-04-01":
                yes_users = read_users_from_json(f"data/users-day-v2-{p}/{yes_str}.json")
            else:
                yes_users = read_users_from_json(f"data/users-day-{model_version}-{p}/{yes_str}.json")
            # if yes_str < "2020-09-01":
            #     yes_users = read_users_from_json(f"data/users-day-{model_version}-{p}/{yes_str}.json")
            # else:
            #     yes_users = read_users_from_json(f"data/users-day-{model_version}-{p}/{yes_str}.json")
            union_users_dict = union_users_from_yesterday_and_today(yesterday_users, yes_users)
            yesterday_users = union_users_dict  # Today will be the yesterday.

            if (save_users and dt.day_of_week) == 1 or dt == end:
                write_union_users_json(union_users_dict, f"users-cumFrom{super_start_month}-{model_version}-{p}", dt.to_date_string())

        rst = get_share_from_users_dict(union_users_dict)
        rst["dt"] = dt.to_date_string()
        print(rst)
        rsts.append(rst)

    pd_rsts = pd.DataFrame(rsts).set_index("dt")
    pd_rsts.index = pd.to_datetime(pd_rsts.index)
    pd_rsts = pd_rsts.rename(columns={0: "Biden", 1: "Trump", 2: "Undecided"})
    pd_rsts.to_csv(f"data/csv/cumFrom{super_start_month}-from-{start.to_date_string()}-to-{end.to_date_string()}-{model_version}-{p}.csv")

def calculate_t0_share(start, super_end, save_csv=None):
    rsts = []

    # super_start = start
    next_users = None

    for i, dt in enumerate(pendulum.period(super_end, start)):
        print(i, dt)

        if dt == super_end:
            union_users_dict = read_users_from_csv(
                f"data/users-day_after_BT_m2_all/{super_end.add(days=-1).to_date_string()}.csv")
            write_union_users_csv(
                union_users_dict, f"users-To{super_end.to_date_string()}", "1")

        else:
            if next_users is None:
                print("Loading next users' csv.")
                next_users = read_users_from_csv(
                    f"disk/users-To{super_end.to_date_string()}/1.csv")

            today_users = read_users_from_csv(
                f"data/users-day_after_BT_m2_all/{dt.add(days=-1).to_date_string()}.csv")
            union_users_dict = union_users_from_yesterday_and_today(
                today_users, next_users)
            next_users = union_users_dict
            write_union_users_csv_v2(
                union_users_dict, f"users-To{super_end.to_date_string()}", str(i+1))

        rst = get_share_from_users_dict(union_users_dict)
        rst["dt"] = dt.to_date_string()
        print(rst)
        rsts.append(rst)

    rsts = pd.DataFrame(rsts).set_index("dt")

    if save_csv:
        rsts.to_csv(f"data/csv/results-To{super_end.to_date_string()}.csv")

    return rsts

def load_df_user_loc(dt):
    print("Loading df_user_loc ...")
    df_users = pd.read_csv(f"disk/users-location/{dt.to_date_string()}.csv",
                           usecols=["uid", "state"]).set_index("uid")
    return df_users

def predict_from_location_from_csv(csv_file, save_csv=None):
    df_user = load_df_user_loc("?")
    users_dict = read_users_from_csv(csv_file)
    rsts = []
    for _s in US_states:
        uid_in_s = set(df_user[df_user.state == _s].index)
        users_in_s = {u: v for u, v in users_dict.items() if u in uid_in_s}
        print(_s, len(uid_in_s), len(users_dict))
        # write_union_users_csv(users_dict, out_dir, _s + ".csv")
        rst = get_share_from_users_dict(users_in_s)
        rst["state"] = _s
        print(rst)
        rsts.append(rst)
    rsts = pd.DataFrame(rsts).set_index("state")

    if save_csv:
        rsts.to_csv(save_csv)

def read_located_users():
    users = []
    user_ids = set()
    for line in open("data/located_users.lj"):
        u = json.loads(line.strip())
        _uid = str(u["user_id"])
        if u["user_id"] not in user_ids:
            users.append({"uid": _uid, "state": u["State"]})
            user_ids.add(_uid)
    for line in open("data/located_users_Jan_March.lj"):
        u = json.loads(line.strip())
        _uid = str(u["user_id"])
        if u["user_id"] not in user_ids:
            users.append({"uid": _uid, "state": u["State"]})
            user_ids.add(_uid)
    return pd.DataFrame(users).set_index("uid")

def predict_from_location(start, end, in_dir, save_users=False):
    # df_user = load_df_user_loc(end)
    df_user = read_located_users()
    print(df_user["state"].value_counts())
    df_state_user = {}
    df_state_user["USA"] = set(df_user.index)
    for _s in US_states:
        uid_in_s = set(df_user[df_user.state == _s].index)
        df_state_user[_s] = uid_in_s

    rsts = []
    for dt in pendulum.period(start, end):
        if dt == start or dt.day_of_week != 1:
            continue
        print("Date >", dt)
        json_file = f"data/users-{in_dir}/{dt.to_date_string()}.json"
        users_dict = read_users_from_json(json_file)
        # country
        uid_in_s = df_state_user["USA"]
        users_in_s = {u: v for u, v in users_dict.items() if u in uid_in_s}
        print("USA", len(uid_in_s), len(users_in_s))  # 州，该州多少用户，命中多少用户
        rst = get_share_from_users_dict(users_in_s)
        rst["id"] = "USA:" + dt.to_date_string()
        rst["dt"] = dt.to_date_string()
        rst["state"] = "USA"
        print(rst)
        rsts.append(rst)

        if dt.day_of_week == 1 and save_users:
            write_union_users_json(users_dict, in_dir + "_loc", dt.to_date_string() + "-" + _s)

        # 选择每个洲的结果
        for _s in US_states:
            uid_in_s = df_state_user[_s]
            users_in_s = {u: v for u, v in users_dict.items() if u in uid_in_s}
            print(_s, len(uid_in_s), len(users_in_s))  # 州，该州多少用户，命中多少用户
            rst = get_share_from_users_dict(users_in_s)
            rst["id"] = _s + ":" + dt.to_date_string()
            rst["dt"] = dt.to_date_string()
            rst["state"] = _s
            print(rst)
            rsts.append(rst)

    rsts = pd.DataFrame(rsts).set_index("id")
    rsts.to_csv(f"data/csv/states-{in_dir}-from-{start.to_date_string()}-to-{end.to_date_string()}.csv")

    return rsts

def predict_t0_from_location(in_dir, state):
    df_user = load_df_user_loc()
    uid_in_s = set(df_user[df_user.state == state].index)

    rsts = []
    for i in range(1, 31):
        csv_file = f"disk/users-To{in_dir}/{i}.csv"
        users_dict = read_users_from_csv(csv_file)
        users_in_s = {u: v for u, v in users_dict.items() if u in uid_in_s}
        print(state, len(uid_in_s), len(users_dict))
        rst = get_share_from_users_dict(users_in_s)
        rst["days"] = i
        rst["state"] = state
        print(rst)
        rsts.append(rst)

    rsts = pd.DataFrame(rsts).set_index("days")
    rsts.to_csv(f"data/csv/results-t0-{state}.csv")

def predict_from_location_superStart(start, end, state):
    """
    each state have the super_start
    """
    import pathlib

    df_user = load_df_user_loc(end)
    uid_in_s = set(df_user[df_user.state == state].index)

    superStart_of_states = {
        "IA": pendulum.datetime(2020, 2, 1, tz="UTC"),  # IA
        "NH": pendulum.datetime(2020, 2, 3, tz="UTC"),  # NH
        # "NV": pendulum.datetime(2020, 1, 22, tz="UTC"), # NV
    }

    super_start = superStart_of_states[state]
    # super_start = start
    yesterday_users = None

    pathlib.Path(
        f"disk/users-From{super_start.to_date_string()}-{state}").mkdir(exist_ok=True)

    rsts = []
    for dt in pendulum.period(start, end):

        if dt <= super_start:  # start in the function is always the next day of super_start
            print("Error: start <= super_start of cumulative prediction.")
            return -1

        elif dt == super_start.add(days=1):
            union_users_dict = read_users_from_csv_from_uids(
                f"data/users-day/{super_start.to_date_string()}.csv", uid_in_s)
            write_union_users_csv(
                union_users_dict, f"users-From{super_start.to_date_string()}-{state}", dt.to_date_string())

        else:
            # just from the cumulative yesterday
            # So I must have the yesterday's cumulative csv
            if yesterday_users is None:
                print("Loading yesterday users' csv at", dt.add(days=-1))
                yesterday_users = read_users_from_csv_from_uids(
                    f"disk/users-From{super_start.to_date_string()}-{state}/{dt.add(days=-1).to_date_string()}.csv",
                    uid_in_s)
            today_users = read_users_from_csv_from_uids(
                f"data/users-day/{dt.add(days=-1).to_date_string()}.csv", uid_in_s)
            union_users_dict = union_users_from_yesterday_and_today(
                yesterday_users, today_users)
            yesterday_users = union_users_dict

        if dt == end:
            print("Writing cumulative users' csv at", dt)
            write_union_users_csv(
                union_users_dict, f"users-From{super_start.to_date_string()}-{state}", dt.to_date_string())

        rst = get_share_from_users_dict(union_users_dict)
        rst["dt"] = dt.to_date_string()
        rst["state"] = state
        print(rst)
        rsts.append(rst)

    rsts = pd.DataFrame(rsts).set_index("dt")
    rsts.to_csv(
        f"data/csv/results-From{super_start.to_date_string()}-{state}-from-{start.to_date_string()}-to-{end.to_date_string()}.csv")

def daily_prediction():
    end = pendulum.today(tz="UTC")  # not include this date
    start = pendulum.yesterday(tz="UTC")  # include this date

    # nation From01
    calculate_cumulative_share(start, end, super_start_month="01")

    # states 40days
    calculate_window_share(start, end, win=40)
    predict_from_location(start, end, out_dir="40days")

if __name__ == "__main__":
    # v1 > only Trump and Biden
    # v2 > Republicans and Democrats
    in_dir = "F:/US2020_data/classification"

    file_name_tweets_prediction = [
        f"{in_dir}/20200910-tweets-prediction-v1.txt",
        f"{in_dir}/202008-tweets-prediction-v1.txt",
        f"{in_dir}/202007-tweets-prediction-v1.txt",
        f"{in_dir}/202006-tweets-prediction-v1.txt",
        f"{in_dir}/202005-tweets-prediction-v1.txt",
        f"{in_dir}/202004-tweets-prediction-v1.txt",
    ]
    # 已经去掉bots
    # save_user_snapshot_json(file_name_tweets_prediction, model_version="v1", p=0.5)
    # save_user_snapshot_json(file_name_tweets_prediction, model_version="v1", p=0.66)

    file_name_tweets_prediction = [
        f"{in_dir}/20200910-tweets-prediction-v2.txt",
        f"{in_dir}/202008-tweets-prediction-v2.txt",
        f"{in_dir}/202007-tweets-prediction-v2.txt",
        f"{in_dir}/202006-tweets-prediction-v2.txt",
        f"{in_dir}/202005-tweets-prediction-v2.txt",
        f"{in_dir}/202004-tweets-prediction-v2.txt",
        f"{in_dir}/202003-tweets-prediction-v2.txt",
        f"{in_dir}/202002-tweets-prediction-v2.txt",
        f"{in_dir}/202001-tweets-prediction-v2.txt",
    ]
    # save_user_snapshot_json(file_name_tweets_prediction, model_version="v2", p=0.5)
    # save_user_snapshot_json(file_name_tweets_prediction, model_version="v2", p=0.66)

    # start = pendulum.datetime(2020, 1, 1, tz="UTC")
    # end = pendulum.datetime(2020, 6, 1, tz="UTC")
    # sess = get_session_2()
    # # -- to database --
    # # tweets_to_db(sess, start, end, clear=True)
    # # -- save users' snapshot --
    # save_user_csv(sess, start, end)
    # sess.close()

    # run it per day
    # daily_prediction()

    # -- window start --
    # 1 day
    # start = pendulum.datetime(2020, 1, 8, tz="UTC")
    # end = pendulum.datetime(2020, 7, 19, tz="UTC")
    # calculate_window_share_size_1(start, end, save_csv=True)

    # 7 days
    # start = pendulum.datetime(2020, 1, 8, tz="UTC")
    # end = pendulum.datetime(2020, 2, 26, tz="UTC")
    # calculate_window_share(start, end, win=7, save_csv=True)

    # 14 days
    start = pendulum.datetime(2020, 1, 14, tz="UTC")
    end = pendulum.datetime(2020, 10, 31, tz="UTC")
    calculate_window_share(start, end, win=14, model_version="v1", p=0.5)
    calculate_window_share(start, end, win=14, model_version="v1", p=0.66)

    # calculate_window_share(start, end, win=14, model_version="v2", p=0.5)
    # calculate_window_share(start, end, win=14, model_version="v2", p=0.66)
    # calculate_window_share(start, end, win=14, p=0.7)
    # -- window end --

    # -- cumulative start --
    start = pendulum.datetime(2020, 1, 2, tz="UTC")
    end = pendulum.datetime(2020, 10, 31, tz="UTC")
    calculate_cumulative_share(start, end, super_start_month="01", model_version="v1", p=0.5)
    calculate_cumulative_share(start, end, super_start_month="01", model_version="v1", p=0.66)

    # calculate_cumulative_share(start, end, super_start_month="01", model_version="v2", p=0.5)
    # calculate_cumulative_share(start, end, super_start_month="01", model_version="v2", p=0.66)
    # calculate_cumulative_share(start, end, super_start_month="01", p=0.7)
    # -- cumulative end --

    # for states
    # start = pendulum.datetime(2020, 1, 15, tz="UTC")
    # end = pendulum.datetime(2020, 10, 20, tz="UTC")
    # predict_from_location(start, end, in_dir="14days", save_users=True)

    # start = pendulum.datetime(2020, 1, 2, tz="UTC")
    # end = pendulum.datetime(2020, 10, 20, tz="UTC")
    # predict_from_location(start, end, in_dir="cumFrom01", save_users=False)

    # only-TB: 1~3月只有川普和拜登
    # start = pendulum.datetime(2020, 6, 2, tz="UTC")
    # end = pendulum.datetime(2020, 10, 10, tz="UTC")
    # predict_from_location(start, end, out_dir="cumFrom06", save_users=True)

    # t0
    # start = pendulum.datetime(2019, 9, 4, tz="UTC")
    # end = pendulum.datetime(2020, 3, 10, tz="UTC")
    # calculate_t0_share(start, end, save_csv=True)

    # start = pendulum.datetime(2020, 1, 11, tz="UTC")
    # end = pendulum.datetime(2020, 2, 11, tz="UTC")
    # calculate_t0_share(start, end, save_csv=True)

    # start = pendulum.datetime(2020, 1, 3, tz="UTC")
    # end = pendulum.datetime(2020, 2, 3, tz="UTC")
    # calculate_t0_share(start, end, save_csv=True)

    # t0 state
    # predict_t0_from_location("2020-02-03", "IA")
    # predict_t0_from_location("2020-02-11", "NH")
    # predict_t0_from_location("2020-02-22", "NV")

    # different initial dates for states
    # end = pendulum.datetime(2020, 3, 2, tz="UTC")

    # start = pendulum.datetime(2020, 2, 2, tz="UTC")
    # predict_from_location_superStart(start, end, "IA")

    # start = pendulum.datetime(2020, 2, 4, tz="UTC")
    # predict_from_location_superStart(start, end, "NH")


from faker import Faker
import json
import datetime
import random

class Person:
    def __init__(self):
        fake = Faker('es_ES')
        self.nombre = fake.first_name()
        self.apellido = fake.last_name()
        self.username = self.nombre[:3] + self.apellido[:3]
        self.email = fake.email()
        self.telefono = fake.phone_number()
        self.ciudad = fake.city()
        self.numcuenta = fake.bban()
        self.nacimiento = fake.date_of_birth(minimum_age=18,maximum_age=100)
        #self.historial = [Operation().get_item() for i in range(random.randint(0,10))]

    def get_item(self,i):
        p = {
            'id_persona': i,
            'nombre': self.nombre,
            'apellido': self.apellido,
            'username': self.username,
            'email': self.email,
            'telefono': self.telefono,
            'ciudad': self.ciudad,
            'numcuenta': self.numcuenta,
            'nacimiento': self.nacimiento.strftime('%m/%d/%Y')#,
            #'historial de ventas': self.historial
        }
        return (p)


"""
Puts crowdflower data in MySQL and downloads transformed images
"""

import os
from Python_code import sql_connect as mysql
import pandas as pd
from PIL import Image
from io import BytesIO
import requests
import platform

if platform.platform()[:5] == 'Linux':
    FILE_PATH = '/home/ec2-user/crowdflower_images/'
else:
    IMAGE_DIR = '/Volumes/NeuralNet/crowdflower_images/'

def download_image(url, image_id):
    response = requests.get(url)
    img = Image.open(BytesIO(response.content))
    img = img.resize((400, 400), Image.ANTIALIAS)
    filename = FILE_PATH + str(image_id) + '.jpg'
    img.save(filename)

def add_to_db(image_id, sentiment, unclear_sentiment, image_url):
    connection = mysql.connect()
    with connection.cursor() as cursor:
        sql = 'INSERT INTO Crowdflower ( ' \
              'image_id, sentiment, unclear_sentiment, image_url)' \
              'VALUES (%s, %s, %s, %s)'
        cursor.execute(sql, (image_id, sentiment, unclear_sentiment, image_url))
    connection.commit()
    connection.close()

curr_dir = os.getcwd()
os.chdir(curr_dir[:-18] + 'Data/test_data')

cf_images = pd.read_csv('Sentiment-Polarity-DFE.csv')

for row in range(len(cf_images)):
    image_id = int(cf_images.at[row,'_unit_id'])
    sentiment = 1 \
        if 'ositive' in \
           cf_images.at[row, 'which_of_these_sentiment_scores_does_the_above_image_fit_into_best'] \
        else -1
    unclear_sentiment = 0 \
        if cf_images.at[row, 'which_of_these_sentiment_scores_does_the_above_image_fit_into_best:confidence'] > .66 \
        else 1
    image_url = cf_images.at[row, 'imageurl']
    download_image(image_url, image_id)
    add_to_db(image_id, sentiment, unclear_sentiment, image_url)
    if row % 100 == 0:
        print(row)


import cv2
import mediapipe as mp

mpFaces = mp.solutions.face_detection
faces = mpFaces.FaceDetection()
mp_draw = mp.solutions.drawing_utils

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    frameRGB = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    results = faces.process(frame)

    if results.detections:
        for id, detection in enumerate(results.detections):
            # mp_draw.draw_detection(frame, detection)
            # print(detection.location_data.relative_bounding_box)
            boxC = detection.location_data.relative_bounding_box
            h, w, c = frame.shape
            box = int(boxC.xmin * w), int(boxC.ymin * h), int(boxC.width * w), int(boxC.height * h)
            cv2.rectangle(frame, box, (182, 24, 82), 1)
            cv2.putText(frame, f'{int(detection.score[0] * 100)}%', (box[0], box[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1,
                        (12, 112, 88), 2)
            x, y, w1, h1 = box
            x1, y1 = x + w1, y + h1
            cv2.line(frame, (x, y), (x + 30, y), (182, 24, 82), 4)
            cv2.line(frame, (x, y), (x, y + 30), (182, 24, 82), 4)
            cv2.line(frame, (x1, y), (x1 - 30, y), (182, 24, 82), 4)
            cv2.line(frame, (x1, y), (x1, y + 30), (182, 24, 82), 4)
            cv2.line(frame, (x, y1), (x + 30, y1), (182, 24, 82), 4)
            cv2.line(frame, (x, y1), (x, y1 - 30), (182, 24, 82), 4)
            cv2.line(frame, (x1, y1), (x1 - 30, y1), (182, 24, 82), 4)
            cv2.line(frame, (x1, y1), (x1, y1 - 30), (182, 24, 82), 4)

    cv2.imshow("image", frame)

    if cv2.waitKey(1) & 0xff is 27:
        break

cap.release()
cv2.destroyAllWindows()


import requests
import json
## calling API
saral_data=requests.get("http://saral.navgurukul.org/api/courses")
data=saral_data.json()
#### pushing data in json file
with open("saral_data.json","w") as f:
    json.dump(data,f,indent=4)

def meraki_course():
    ###  this loop for coures name and id
    serial_number=1
    for index_1 in data["availableCourses"]:
        print(serial_number,index_1["name"],":",index_1["id"])
        serial_number+=1
meraki_course()

#### user input for which course you like to do
user_choose_course=int(input("enter the course number: "))-1
print(data["availableCourses"][user_choose_course]["name"])
user=input("do you want to previous ").lower()
if user=="p":
    meraki_course()
###this link for parents data
parents_data=requests.get("http://saral.navgurukul.org/api/courses/"+data["availableCourses"][user_choose_course-1]["id"]+" "+"/exercises")
parents_Data1=parents_data.json()
## dump parents data in json file XXXX
with open("parents_data.json","w") as fl:
    json.dump(parents_Data1,fl,indent=2)

def saral_data():
#####parents(child)
    serialNum=1
    print("slu",parents_Data1["data"][1]["slug"])
    child_Data=parents_Data1["data"]
    for index_2 in child_Data:
        print(serialNum,index_2["name"])
        serialNum+=1
        if index_2["childExercises"]==[]:
            print(" ",index_2["slug"])
        else:
            serialNum2=1
            Child=index_2["childExercises"]
            for index_3 in Child:
                print("  ",serialNum2,index_3["name"])
                serialNum2+=1

# saral_data()
# slug=int(input("enter the number of parents:"))
# print(parents_Data1["data"][slug-1]["name"])

# user_choose_course=int(input("enter the course number: "))-1
# print(data["availableCourses"][user_choose_course]["name"])
# Number1=input("do you want to previous or next").lower()
# if Number1=="p":
#     print(" ")

# saral_data()

# slug_content=[]
# no=0
# list=[]
# for child in range(len(parents_Data1["data"])):
#     no=+1
#     serial_no=1
#     if parents_Data1["data"][child]["childExercises"]==list:
#         serial_no += 1

#         Serial_Number=1
#         for Question in range(len(parents_Data1["data"][slug-1]["childExercises"])):
#             if parents_Data1["data"][child]["childExercises"] == list:
#                 print("      ",Serial_Number,".",parents_Data1["data"][slug-1]["chilExercises"][Question]["name"])
#                 slug1 = parents_Data1["data"][slug-1]["chilExercises"][Question]["slug"]
#                 parent = parents_Data1["data"][slug-1]["chilExercises"][Question]["id"]
#                 slug2 = requests.get("http://saral.navgurukul.org/api/courses/"+parent+"/exercise/getBySlug?slug="+slug)

#                 slug3 = slug2.json()
#                 slug_content.append(slug3["content"])
#                 Serial_Number += 1

# user_choose_course=int(input("enter the course number: "))
# Data=parents_Data["data"][user_choose_course-1]["childExercises"]
# serial_Number=1
# for index_4 in Data:
#     print(" ",serial_Number,index_4["name"])
#     serial_Number+=1
# slug
# slugInput=int(input("which question you want to see"))-1
# slugId=Child[slugInput]["id"]
# childSlugName=Child[slugInput]["slug"]
# slug_data=requests.get("http://saral.navgurukul.org/api/courses/"+slugId+"/exercise/getBySlug?slug="+str(childSlugName))
# data5=slug_data.json()
# with open("slug_data","w") as File:
#     json.dump(data5,File,indent=4)
#     print(data5["content"])


from django.urls import path, include
from . import views

urlpatterns = [
    path('', views.leadership, name='leadership'),
    path('mgt_training', views.schoolMgt, name='mgt_training'),
    path('itAndAuto', views.itAndAuto, name='itAndAuto'),
    path('literacy', views.financialLiteracy, name='literacy'),
    path('capacity', views.capacityBuilding, name='capacity'),
]


from pypy.conftest import gettestobjspace
from pypy.objspace.std.test.test_typeobject import AppTestTypeObject

class AppTestMethodCaching(AppTestTypeObject):
    def setup_class(cls):
        cls.space = gettestobjspace(
            **{"objspace.std.withmethodcachecounter": True})

    def test_mix_classes(self):
        import __pypy__
        class A(object):
            def f(self):
                return 42
        class B(object):
            def f(self):
                return 43
        class C(object):
            def f(self):
                return 44
        l = [A(), B(), C()] * 10
        __pypy__.reset_method_cache_counter()
        for i, a in enumerate(l):
            assert a.f() == 42 + i % 3
        cache_counter = __pypy__.method_cache_counter("f")
        assert cache_counter[0] >= 15
        assert cache_counter[1] >= 3 # should be (27, 3)
        assert sum(cache_counter) == 30

    def test_class_that_cannot_be_cached(self):
        import __pypy__
        class X:
            pass
        class Y(object):
            pass
        class A(Y, X):
            def f(self):
                return 42

        class B(object):
            def f(self):
                return 43
        class C(object):
            def f(self):
                return 44
        l = [A(), B(), C()] * 10
        __pypy__.reset_method_cache_counter()
        for i, a in enumerate(l):
            assert a.f() == 42 + i % 3
        cache_counter = __pypy__.method_cache_counter("f")
        assert cache_counter[0] >= 9
        assert cache_counter[1] >= 2 # should be (18, 2)
        assert sum(cache_counter) == 20

    def test_change_methods(self):
        import __pypy__
        class A(object):
            def f(self):
                return 42
        l = [A()] * 10
        __pypy__.reset_method_cache_counter()
        for i, a in enumerate(l):
            assert a.f() == 42 + i
            A.f = eval("lambda self: %s" % (42 + i + 1, ))
        cache_counter = __pypy__.method_cache_counter("f")
        # the cache hits come from A.f = ..., which first does a lookup on A as
        # well
        assert cache_counter == (17, 3)

    def test_subclasses(self):
        import __pypy__
        class A(object):
            def f(self):
                return 42
        class B(object):
            def f(self):
                return 43
        class C(A):
            pass
        l = [A(), B(), C()] * 10
        __pypy__.reset_method_cache_counter()
        for i, a in enumerate(l):
            assert a.f() == 42 + (i % 3 == 1)
        cache_counter = __pypy__.method_cache_counter("f")
        assert cache_counter[0] >= 15
        assert cache_counter[1] >= 3 # should be (27, 3)
        assert sum(cache_counter) == 30

    def test_many_names(self):
        import __pypy__
        for j in range(20):
            class A(object):
                foo = 5
                bar = 6
                baz = 7
                xyz = 8
                stuff = 9
                a = 10
                foobar = 11

            a = A()
            names = [name for name in A.__dict__.keys()
                          if not name.startswith('_')]
            names.sort()
            names_repeated = names * 10
            result = []
            __pypy__.reset_method_cache_counter()
            for name in names_repeated:
                result.append(getattr(a, name))
            append_counter = __pypy__.method_cache_counter("append")
            names_counters = [__pypy__.method_cache_counter(name)
                              for name in names]
            try:
                assert append_counter[0] >= 10 * len(names) - 1
                for name, count in zip(names, names_counters):
                    assert count == (9, 1), str((name, count))
                break
            except AssertionError:
                pass
        else:
            raise

    def test_mutating_bases(self):
        class C(object):
            pass
        class C2(object):
            foo = 5
        class D(C):
            pass
        class E(D):
            pass
        d = D()
        e = E()
        D.__bases__ = (C2,)
        assert e.foo == 5

        class F(object):
            foo = 3
        D.__bases__ = (C, F)
        assert e.foo == 3

    def test_custom_metaclass(self):
        import __pypy__
        for j in range(20):
            class MetaA(type):
                def __getattribute__(self, x):
                    return 1
            def f(self):
                return 42
            A = type.__new__(MetaA, "A", (), {"f": f})
            l = [type.__getattribute__(A, "__new__")(A)] * 10
            __pypy__.reset_method_cache_counter()
            for i, a in enumerate(l):
                assert a.f() == 42
            cache_counter = __pypy__.method_cache_counter("f")
            assert sum(cache_counter) == 10
            if cache_counter == (9, 1):
                break
            #else the moon is misaligned, try again
        else:
            raise AssertionError("cache_counter = %r" % (cache_counter,))

    def test_mutate_class(self):
        import __pypy__
        class A(object):
            x = 1
            y = 2
        __pypy__.reset_method_cache_counter()
        a = A()
        for i in range(100):
            assert a.y == 2
            assert a.x == i + 1
            A.x += 1
        cache_counter = __pypy__.method_cache_counter("x")
        assert cache_counter[0] >= 350
        assert cache_counter[1] >= 1
        assert sum(cache_counter) == 400

        __pypy__.reset_method_cache_counter()
        a = A()
        for i in range(100):
            assert a.y == 2
            setattr(a, "a%s" % i, i)
        cache_counter = __pypy__.method_cache_counter("x")
        assert cache_counter[0] == 0 # 0 hits, because all the attributes are new

    def test_get_module_from_namedtuple(self):
        # this used to crash
        from collections import namedtuple
        assert namedtuple("a", "b").__module__


#!/usr/bin/env python
# author = 'ZZH'
# time = 2021/11/23
# project = 剑指offerII45
# Definition for a binary tree node.
from collections import deque

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def findBottomLeftValue(self, root: TreeNode) -> int:
        ret = 0
        nodes = deque()
        nodes.append(root)
        while nodes:
            size = len(nodes)
            for i in range(size):
                node = nodes.popleft()
                if i == 0:
                    ret = node.val
                if node.left:
                    nodes.append(node.left)
                if node.right:
                    nodes.append(node.right)
        return ret


import time
import rich

from minimax import minimax
import board
from legal_moves_list import all_legal_moves

def player_vs_computer(player_w, player_b, depth):
    current_depth = depth
    while True:
        if (board.main_board.who_to_move == 1 and player_w == 'computer') or (board.main_board.who_to_move == 0 and player_b == 'computer'):
            start_time = time.perf_counter()
            computer_move = minimax(board.main_board, board.main_board.who_to_move, (-9999, None), (9999, None), current_depth)
            end_time = time.perf_counter()

            print(f"Computer: {computer_move[1]}, calculated in {end_time - start_time} seconds. Evaluation = {computer_move[0]} at depth {current_depth}")
            board.main_board.move(computer_move[1])

        else:
            legal_move_found = False
            while legal_move_found == False:
                player_move = input_move(board.main_board.who_to_move)

                if player_move == None:
                    continue

                if player_move.startswith("depth="):
                    current_depth = int(player_move[6:])
                    print(f"Depth has successfully changed to {current_depth}")
                    continue

                board.main_board.move(player_move)
                legal_move_found = True

        rich.print(f"\nPosition: \n{pos_to_cli_board(board.main_board)} \n")

def input_move(player):
    player_move = input("Player: ")

    if player_move.startswith("depth="):
        return player_move

    for move in all_legal_moves(board.main_board, player):
        if move == player_move:
            return move

    print("That's not a legal move!")
    return None

def pos_to_cli_board(board):
    unicode_pieces = {
        "black rook": "♖",
        "black knight": "♘",
        "black bishop": "♗",
        "black king": "♔",
        "black queen": "♕",
        "black pawn": "♙",
        "white rook": "♜",
        "white knight": "♞",
        "white bishop": "♝",
        "white king": "♚",
        "white queen": "♛",
        "white pawn": "♟"
    }

    final_text = ""

    for y in reversed(range(8)):
        for x in range(8):
            bg_color = f"{'#f0d9b5' if (x + y) % 2 else '#b58863'}"
            broken_off = False
            for piece in board.pieces:
                if piece.x == x and piece.y == y:
                    char = unicode_pieces[f"white {piece.__class__.__name__.lower()}"]
                    piece_color = 'red' if piece.color == 1 else 'blue'
                    final_text += f"[{piece_color} on {bg_color}] {char} [/{piece_color} on {bg_color}]"
                    broken_off = True
                    break
            if not broken_off:
                final_text += f"[white on {bg_color}]   [/white on {bg_color}]"
        final_text += '\n'

    return final_text


import numpy as np
import serial, time, datetime
import threading
import traceback
import math
import pickle

class UWBManager():

    def __init__(self):

        self.serial_port_ = serial.Serial("/dev/ttyUSB0", 115200)
        self.tag_data = None
        self.active_flag_ = False

        self.serial_port_.flush()

    def updateSensorData(self):
        while self.active_flag_:
            if self.serial_port_.is_open:
                raw_data = self.serial_port_.readline()
                if len(raw_data) == 16:  # data length should be 16 bytes
                    self.tag_data = self.processRawData(raw_data)
                    # print("distace: {} distance: {} distance: {} ".format(self.tag_distance[0], self.tag_distance[1], self.tag_distance[2]))

    def processRawData(self, raw_data) -> list:
        distance_data = [0, 0, 0, 0]
        distance_data[0] = self.stitchup(raw_data[8], raw_data[7]) # anchor0 to tag
        distance_data[1] = self.stitchup(raw_data[10], raw_data[9]) # anchor1 to tag
        distance_data[2] = self.stitchup(raw_data[12], raw_data[11]) # anchor2 to tag
        if raw_data[4] == 0x0f:
            distance_data[3] = 0 # Master Tag ID
        else:
            distance_data[3] = raw_data[4] # Slave Tag ID
        return distance_data

    def stitchup(self, high_byte, low_byte):
        return float((high_byte * 256 + low_byte) / 100)

    def getUWBDistance(self) -> list:
        return self.tag_data

    def startFetchDistance(self):
        self.active_flag_ = True
        self.thread_update_data= threading.Thread(
            target=self.updateSensorData,
            daemon= True
        )
        self.thread_update_data.start()

    def closeUWBPort(self):
        self.active_flag_ = False
        self.serial_port_.close()
        if not self.serial_port_.is_open:
            print("UWB port closed complete")

class UWBLocalizationSystem():

    def __init__(self):
        self.uwb_manager_ = UWBManager()
        self.uwb_is_active_ = False

        self.tag_data = None
        self.anchor_pos_list = list() # max 4 anchor pos
        self.save_pos_x = list()
        self.save_pos_y = list()
        self.time_out_count = 0
        self.MAX_ANCHOR_NUM = 4
        self.TIME_OUT_COUNT = 200
        self.activateUWBManager()

    def activateUWBManager(self):
        self.uwb_manager_.startFetchDistance()
        self.uwb_is_active_ = True

    def caculateTagPosition(self):
        tag_data = self.uwb_manager_.getUWBDistance()
        if tag_data is None:
            return False
        if not any(tag_data) == 0:
            self.processMLE(tag_data)
        return True

    def processMLE(self, tag_dis):
        A = np.array([[2 * (self.anchor_pos_list[0][0] - self.anchor_pos_list[-1][0]),2 * (self.anchor_pos_list[0][1] - self.anchor_pos_list[-1][1])],
                      [2 * (self.anchor_pos_list[1][0] - self.anchor_pos_list[-1][0]),2 * (self.anchor_pos_list[1][1] - self.anchor_pos_list[-1][1])]])
        b = np.array([[pow(self.anchor_pos_list[0][0],2) - pow(self.anchor_pos_list[-1][0],2) + pow(self.anchor_pos_list[0][1],2) - pow(self.anchor_pos_list[-1][1],2) + pow(tag_dis[2],2) - pow(tag_dis[0],2)],
                      [pow(self.anchor_pos_list[1][0],2) - pow(self.anchor_pos_list[-1][0],2) + pow(self.anchor_pos_list[1][1],2) - pow(self.anchor_pos_list[-1][1],2) + pow(tag_dis[2],2) - pow(tag_dis[1],2)]])
        temp_X = np.dot(np.linalg.inv(np.dot(A.T,A)),A.T)
        X = np.dot(temp_X, b)
        self.tag_data =  [X.T[0,0], X.T[0,1], tag_dis[3]]
        print("tag X: {} Y: {} ID: {}".format(self.tag_data[0], self.tag_data[1], self.tag_data[2]))
        # For Data Collect
        # self.save_pos_x.append(self.tag_data[0])
        # self.save_pos_y.append(self.tag_data[1])

    def setAanchorPos(self, anchor_x, anchor_y):
        if len(self.anchor_pos_list) < self.MAX_ANCHOR_NUM:
            anchor_pos = [0, 0]
            anchor_pos[0] = anchor_x
            anchor_pos[1] = anchor_y
            self.anchor_pos_list.append(anchor_pos)
        else:
            print("Anchor pos num out of range")

    def getTagPosition(self):
        if self.tag_data is not None:
            return self.tag_data
        else:
            self.tag_data = [0, 0, 0]
            return self.tag_data

    def processLoop(self):
        while not self.stop_localize_thread_:
            connect_ok = self.caculateTagPosition()
            if not connect_ok:
                self.checkTimeOut()
            time.sleep(0.01)

    def checkTimeOut(self):
        self.time_out_count += 1
        if self.time_out_count == self.TIME_OUT_COUNT:
            self.closeSystem()
            print(" UWB Master Connection Lost ")

    def startLocalizeTag(self):
        if self.uwb_is_active_:
            self.setAanchorPos(0,0) # anchor 0
            self.setAanchorPos(-0.5, 3.65) # anchor 1
            self.setAanchorPos(-4.34, 1.13) # anchor 2
            self.stop_localize_thread_ = False
            self.track_data_thread = threading.Thread(
                target=self.processLoop,
                daemon=True
            )
            self.track_data_thread.start()
        else:
            print("UWB sensor is not activate")

    def saveData(self):
        fileName = datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")
        fileName = './data/' + fileName
        with open(fileName + ".x_posdata", 'wb+') as f:
            pickle.dump(self.save_pos_x,f)
        with open(fileName + ".y_posdata", 'wb+') as f:
            pickle.dump(self.save_pos_y,f)

    def closeSystem(self):
        self.stop_localize_thread_ = True
        self.uwb_manager_.closeUWBPort()

if __name__ == "__main__":

    manager = UWBLocalizationSystem()
    manager.startLocalizeTag()
    while True:
        try:
            cmd = input("CMD: ")
            if cmd == "q":
                manager.saveData()
                break
        except Exception as e:
            traceback.print_exc()
            break

    manager.closeSystem()


from flask.helpers import url_for
from werkzeug.utils import redirect
from . import auth
from flask import render_template, request, flash
from .forms import LoginForm, RegisterStudentForm, RegisterTeacherForm
from flask_login import login_user, current_user, logout_user, login_required

@auth.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()

    if request.method == 'POST':
        user = form.validateUser()
        if type(user) == str:
            flash(user)
        else:
            login_user(user)
            flash('Bienvenido '+current_user.user_name)
            return redirect(url_for('main.home'))

    return render_template('auth/login.html', form=form)

@auth.route('/register_student', methods=['GET', 'POST'])
def registerStudent():
    form = RegisterStudentForm()

    if request.method == 'POST':
        user_valid = form.validateIdentifier()
        if type(user_valid) == str:
            flash(user_valid)
            return redirect(url_for('auth.login'))
        elif user_valid == False:
            flash('Usuario ya existente.')
            return redirect(url_for('auth.register'))
        else:
            flash('Identificador no válido.')
            return redirect(url_for('auth.register'))

    return render_template('auth/registerStudent.html', form=form)

@auth.route('/register_teacher', methods=['GET', 'POST'])
def registerTeacher():
    form = RegisterTeacherForm()

    if request.method == 'POST':
        user_valid = form.registerUser()
        if type(user_valid) == str:
            flash(user_valid)
            return redirect(url_for('auth.login'))
        elif user_valid == False:
            flash('Usuario ya existente.')
            return redirect(url_for('auth.register'))

    return render_template('auth/registerTeacher.html', form=form)

@auth.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('auth.login'))


from PySide2 import QtCore, QtWidgets

# https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/kernel/qwidget.h#n873
QWIDGETSIZE_MAX = (1 << 24) - 1

class MyWidget(QtWidgets.QWidget):
    def __init__(self):
        super(MyWidget, self).__init__()
        self.m_deltaX = 0
        self.btn = QtWidgets.QPushButton(
            ">", checkable=True, clicked=self.closeOpenEditor
        )
        self.btn.setFixedSize(QtCore.QSize(25, 25))

        self.text1 = QtWidgets.QTextEdit()
        self.text1.setText("some sample text")

        self.text2 = QtWidgets.QTextEdit()

        layout_btn = QtWidgets.QVBoxLayout()
        layout_btn.addWidget(self.btn)

        lay = QtWidgets.QHBoxLayout(self)
        lay.addWidget(self.text1, 10)
        lay.addSpacing(15)
        lay.addLayout(layout_btn)
        lay.setSpacing(0)
        lay.addWidget(self.text2, 4)

        self.resize(800, 500)

        self.m_animation = QtCore.QPropertyAnimation(
            self.text2, b"maximumWidth", parent=self, duration=250
        )

    def closeOpenEditor(self):
        if self.btn.isChecked():
            self.text2.setMaximumWidth(self.text2.width())
            text2Start = int(self.text2.maximumWidth())
            self.m_deltaX = text2Start
            text2End = 3
            self.m_animation.setStartValue(text2Start)
            self.m_animation.setEndValue(text2End)
            self.btn.setText("<")
        else:
            text2Start = int(self.text2.maximumWidth())
            text2End = self.m_deltaX
            self.m_animation.setStartValue(text2Start)
            self.m_animation.setEndValue(text2End)
            self.btn.setText(">")

        self.m_animation.start()

    def resizeEvent(self, event: "QResizeEvent"):
        if not self.btn.isChecked():
            self.text2.setMaximumWidth(QWIDGETSIZE_MAX)

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MyWidget()
    w.show()
    sys.exit(app.exec_())


import matplotlib.pyplot as plt
import pandas as pd

nucs = ['Pu239', 'U235', 'U233']

fig = plt.figure()
for nuc in nucs:
    df = pd.read_csv('{}.txt'.format(nuc), skiprows=1, header=None,
                     sep=" ", names=['energy', 'xs'])
    plt.loglog(df['energy'], df['xs'], label=nuc, figure=fig)
plt.legend(loc=0)
plt.xlabel('Energy (eV)')
plt.ylabel('$\sigma(E)$ (barns)')
plt.title('Fission Cross Section')
plt.savefig('xs.pdf')


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

# In[1]:

import scipy
import numpy as np
from tabulate import tabulate
from scipy.optimize import linear_sum_assignment

# In[2]:

def group_values(calc_values):
    """Here, we pass the nd.array of total fields with
    their labels, index and total field value to group the values
    e.g calc_values = np.array([['$B_{\\mu}^{s}$', '1-1', '0.9694245004388192'],
                                ['$B_{\\mu}^{s}$', '2-1', '0.3696106159894605'],
                                ['$B_{\\mu}^{s}$', '2-2', '0.5802625057445634'],
                                ['$B_{\\mu}^{s}$', '2-3', '1.0119281898999035'],
                                ['$B_{\\mu}^{s}$', '3-1', '2.0119281898999035']]
                                )
        and returns a dictionary values based on their index
        {1: np.array([0.9694245]),
         2: np.array([0.36961062, 0.58026251, 1.01192819]),
         3: np.array([2.01192819])}

    Parameters
    ----------
    calc_values : numpy.ndarray
        array containing the label, index and value of field contribution
    Returns: dict
        A dictionary of group values
    """
    import copy
    def group(items):
        """group a list containing identical items
        into group  with their index
        """
        from itertools import groupby
        result = {
            key: [item[0] for item in group]
            for key, group in groupby(sorted(enumerate(items), key=lambda x: x[1]), lambda x: x[1])
        }
        return result

    calc_values = np.array(calc_values)
    if len(calc_values)==1:
        return {1: np.array([np.float_(calc_values[:,2])])}

    index = calc_values[:,1]
    index_pair = [(inx.split('-')[0], inx.split('-')[1]) for inx in index]
    index_single1 = []
    index_single2 = []
    for inx in index_pair:
        index_single1.append(inx[0])
        index_single2.append(inx[1])

    index_single1_ = list(np.int_(index_single1))
    calc_values_ = np.float_(calc_values[:,2])
    #print(calc_values_)
    group_indexes = group(index_single1_)
    group_indexes_index = list(group_indexes.values())

    calc_values_list = [[] for i in range(len(group_indexes))]
    calc_values_dict = {}
    dic = {}
    for items in group_indexes.items():
        dic[items[0]] = calc_values_[np.array(items[1])]
    return dic

# In[3]:

class MergeField:
    """This class is use to Merge/Collapse a set list of values that their
    difference is less than a threshold into unique set of values .
    """

    @staticmethod
    def index_of_items_from_lists(list_lists, list_items):
        """Find index of items in a lists

        Parameters
        ----------
        list_lists : list
            list of values
        list_items : list
            list of values to identify index from list_lists
        Returns
        -------
        list
        """
        return [list(list_lists).index(item) for item in list_items]

    @staticmethod
    def store_dict_to_file(data_object, filename, uuid_index):
        """To store a data object

        Parameters:
        -----------
        data_object : numpuy.ndarray
            A data objec to store in txt file (just for fun to make it editable)
        filenmae : str
            name of file to store data
        uuid_index : int
            index for each file
        """
        import pprint
        dic = data_object.copy()
        with open(filename, "w") as f:
            f.write(pprint.pformat(dic, indent=4))

    def __init__(
        self,
        total_fields,
        muon_sites,
        uuid_index = 1,
        threshold = 0.1,
        filename = None
    ):
        """
        Parameters
        ----------
        total_fields : list
            list of calculated field
        muon_sites : nd.array
            muon sites positions
        uuid_index : int
            calculation index, Default = 1
        threshold : float
        filename : str
            file name to save data object

        """
        self.total_fields = np.array(total_fields)
        self.muon_sites = muon_sites
        self.uuid_index = uuid_index
        self.threshold = threshold
        self.filename = filename

        if filename is None or filename == "":
            self.filename = "data"
        else:
            self.filename = filename

        if len(self.total_fields) == 1:
            self.index  = self.total_fields[:,1]
            self.labels = list(set(self.total_fields[:,0]))
            self.fields = [np.float_(self.total_fields[:,2])]
        else:
            self.index  = self.total_fields[:,1]
            self.labels = list(set(self.total_fields[:,0]))
            self.fields = list(np.float_(self.total_fields[:,2]))

        # The self.init_musite_index and self.init_musite_value
        # to keep note of the index of calculated field (not replicas)
        # and its value
        for i, ind in enumerate(self.index):
            split_ = ind.split('-')
            if split_[-1] == '1':
                self.init_musite_index = i
                #self.init_site_index.append(i)
        self.init_musite_value = self.fields[self.init_musite_index]
#         print('init_musite_index = {} and  init_musite_value = {}'.
#               format(self.init_musite_index,
#                      self.init_musite_value
#                     )
#              )
        self.length_of_data = len(self.fields)
        original_indexes = [i+1 for i in range(self.length_of_data)]

        self.merge_fields_average = {}
        self.merge_indexes_sublists = {}

        self.fields_distinct_dic = {}
        self.group_index_sublist_dict = {}

    def merge_fields(self):
        """ Perform the merging of values
        """
        if self.length_of_data == 1:
            self.merge_fields_average = {'1*':self.fields[0]}
            self.merge_indexes_sublists = {1:[0]}
            return self.merge_fields_average, self.merge_indexes_sublists

        fields_data = np.array(self.fields)
        idx0 = np.argsort(fields_data)
        fields = fields_data[idx0]
        idx = np.argsort(fields)
        diff = np.diff(fields)
        #print('idx0 = ', idx0, 'idx = ', idx)
        avg = fields[:-1]+np.diff(fields)/2
        merge = diff/avg < self.threshold
        # create a list of lists, put the first value of the source data in the first
        lists = [[fields[0]]]
        for i, x in enumerate(fields[1:]):
            # if the gap from the current item to the previous is more than the threshold
            # Note: the previous item is the last item in the last list
            # Note: the '> self.threshold' is the part you'd modify to make it stricter
            # or more relaxed
            if (x - lists[-1][-1]) / avg[i] > self.threshold:
                # then start a new list
                lists.append([])
            # add the current item to the last list in the list
            lists[-1].append(x)

        #print('lists = ', lists)

        sublists = []
        for items in lists:
            sublists.append(self.index_of_items_from_lists(fields_data, items))
        for i, val in enumerate(sublists):
            self.merge_indexes_sublists[i+1] = val

        for i, l in enumerate(lists):
            #print('values = ', l)
            string = str(i+1)
            if self.init_musite_value in l:
                string = string+'*'
            self.merge_fields_average[string] = np.average(l)

        return self.merge_fields_average, self.merge_indexes_sublists

    def summary(self):
        """ Display the summary of informations
        """
        muon_sites = np.array(self.muon_sites)
        fields_dict = self.merge_fields()[0]
        group_indexes = self.merge_fields()[1]

        filename = self.filename+'_merge_index_calc_'+str(self.uuid_index)+'.txt'
        print('... Saving complete merge index\'s for calculation #{} to a file : {}'.
              format(self.uuid_index,
                     filename
                    )
             )
        self.store_dict_to_file(group_indexes, filename, self.uuid_index)

        sites = []
        m_sites = []
        fields = []
        number_dict = {}
        multiplicity = []
        for items1, items2 in zip(group_indexes.items(), fields_dict.items()):
            mu_index = items1[1][0]
            if '*' in items2[0]:
                mu_index = self.init_musite_index
            number_dict[items1[0]] = len(items1[1])
            sites.append(items2[0])
            fields.append(items2[1])
            m_sites.append(muon_sites[mu_index])
            multiplicity.append(len(items1[1]))
        m_sites = np.around(np.array(m_sites), 6)
        tab = zip(sites, m_sites, fields, multiplicity)
        headers = ['##', 'POSITION (x,y,z)', 'NET FIELD (Tesla)', 'MULTIPLICITY ##']

        print('\n... Total number of {} equiv fields are merged to give {} distinct field ...\n'.
              format(self.length_of_data,
                     len(number_dict)
                    )
             )
#         print('\n... Each distinct field has nth terms as : {} ...\n'.format(number_dict))
        print(tabulate(tab, headers=headers, tablefmt="github"))
        print('\n\t[*] Means calculated muon site ...\n')

    def data_object(self):
        """Prepare the merge values into a form of input data of 'total_fields'
        """
        fields_dict = self.merge_fields()[0]
        data = []
        for i, items in enumerate(fields_dict.items()):
            data.append([str(self.labels[0]),  str(self.uuid_index)+'-'+str(items[0]),    items[1]])
        return data

# In[4]:

class HungarianError(Exception):
    pass

# Import numpy. Error if fails
try:
    import scipy
    import numpy as np
    from scipy.optimize import linear_sum_assignment
except ImportError:
    raise HungarianError("numpy or scipy not installed.")

class HungarianAlgorithm:
    """This class perfrom a Hungarian algorithm to assign experimental values
        to calculated values
    """

    @staticmethod
    def __cost_matrix(
        calculated_values,
        experimental_values
    ):

        """Calculate the cost and real matrix. The cost matrix is weight of task to
        assign each experimental values to any calculated values, which is absolute
        difference between experimental and calculated values. The real matrix
        is weight of calculated value only.

        Parameters
        ----------
        calculated_values : dict, list
            calculated value
        experimental_values : list
            experimental value

        Returns
        -------
        tuple
        cost_matrix : numpy.ndarray
            the cost matrix to optimized
        real matrix :  numpy.ndarray
            the real matrix of the calculated values
        """
        nrows = len(experimental_values)
        ncols = len(calculated_values)
        cost_matrix = np.zeros([nrows, ncols])
        real_matrix = np.zeros([nrows, ncols])
        for i, val in enumerate(experimental_values):
            cost_matrix[i] = [np.abs(val-c) for c in calculated_values]
            real_matrix[i] = [np.abs(0-c) for c in calculated_values]
        return cost_matrix, real_matrix

    def __init__(
        self,
        calculated_values=None,
        experimental_values=None
    ):
        """This class perfrom a Hungarian algorithm to assign experimental values
        to calculated values

        Parameters
        ----------
        calculated_values : dict, list
            calculated value
        experimental_values : list
            experimental value
        Returns
        -------
        """

        if calculated_values is not None and experimental_values is not None:
            exp_values = list(experimental_values)
            argsort = np.argsort(exp_values)
            self._exp_values = list(np.array(exp_values)[argsort])
            if isinstance(calculated_values, dict):
                #calc_keys = list(calculated_values.keys())
                self._dict = calculated_values.copy()
                data_type = 'dict'
                calc_values = list(calculated_values.values())
                calc_values = [item for sublist in calc_values for item in sublist]
            elif isinstance(calculated_values, list):
                data_type = 'list'
                calc_values = list(calculated_values)
                #calc_keys = [i+1 for i in range(len(calc_values))]
            elif isinstance(calculated_values, np.ndarray):
                data_type = 'list'
                calc_values = [item for sublist in calc_values for item in sublist]
                #calc_keys = [i+1 for i in range(len(calc_values))]
            # sort values
            argsort = np.argsort(calc_values)
            self._calc_values = list(np.array(calc_values)[argsort])
            self._data_type = data_type
            #self._calc_keys = calc_keys
            # Results from algorithm.
            self._results = []               # for hungarian results
            self._results2 = []               # return the exact results
            self._PotentialValues = []       # for hungarian results
            self._PotentialValues2 = []       # return the exact results
        else:
            self._calc_values = None
            self._exp_values = [0.0]

    def get_results(self):
        """Get results after calculation."""
        return self._results

    def get_potential_values(self):
        """Returns expected value after calculation."""
        #print('Hungarian algorithm gives...')
        if len(self._exp_values) > len(self._PotentialValues):
            print('ALL EXPERIMENTAL DATA CAN\'T BE ASSIGNED...')
        else:
            print('ALL EXPERIMENTAL DATA CAN BE ASSIGNED...')
        print('BY HUNGARIAN ALGORITHM TO GIVE...')
        match_data_object = {}
        for i, value in enumerate(self._PotentialValues):
            if self._data_type == 'dict':
                other_values, index = self.check_other_values_and_index(value=value)
                print('VALUE  #{} :: = {} in Tesla from calculation {}.'.
                      format(i+1,
                             value,
                             index
                            )
                     )
                print('\tCONTAINS {} SYMMETRY REPLICAS...'.format(len(other_values)))
                print('\tWITH VALUES = {} in Tesla.'.format(other_values))
            else:
                print('VALUE  #{} :: = {} Tesla.'.format(i+1, value))
            match_data_object[i+1] = value
        print('\nMATCH DATA OBJECT = {}\n'.format(match_data_object))
        return self._PotentialValues

    def check_other_values_and_index(self, value):
        """Found a value from a group
        """
        for key, val in self._dict.items():
            if value in val:
                return val, key

    def calculate(
        self,
        calculated_values=None,
        experimental_values=None
    ):
        """Perform the Hungarian algorithm.

        Parameters
        ----------
        calculated_values : dict, list
            calculated value
        experimental_values : list
            experimental value
        Returns
        -------
        """

        if calculated_values is None and self._calc_values is None:
            raise HungarianError("values is invalid or not given")
        elif calculated_values is not None:
            self.__init__(
                calculated_values=calculated_values,
                experimental_values=experimental_values
            )
        calc_values = self._calc_values
        exp_values = self._exp_values
        data_type = self._data_type
        cost_matrix_, real_matrix_ = self.__cost_matrix(calc_values, exp_values)

        # Hungarian scipy optimization
        matched_rows, matched_columns = linear_sum_assignment(cost_matrix_)

        # Save Results
        for result in zip(matched_rows, matched_columns):
            row, column = result
            rc = (int(row), int(column))
            self._results.append(rc)
            self._PotentialValues.append(real_matrix_[row, column])

# In[ ]:


# !/user/bin/env python3

# Created by Kevin Csiffary
# Date: Jan. 11, 2023
# This program is my game

import stage
import ugame
import time
import random
import supervisor

import constants

def splash_screen():
    # setup the display and frame rate
    game = stage.Stage(ugame.display, 60)

    # render an entire 160 * 128 image from 5 slices
    for i in range(5):
        slice_bmp = stage.Bank.from_bmp16(f"slice{i}.bmp")
        cur_slice = stage.Grid(slice_bmp, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y)
        for y in range(16):
            if y >= 8:
                c = 1
            else:
                c = 0
            # place the chunk at the correct posiiton
            cur_slice.tile((i * 2) + c, y - (8 * c), y)
        # add the chunk to the list of things to render
        game.layers += [cur_slice]

    # render all of the sprites
    game.render_block()

    while True:
        # wait 2 seconds
        time.sleep(2.0)
        # go to the menu scene
        menu_scene()

def menu_scene():
    image_bank_mt_background = stage.Bank.from_bmp16("pain_menu.bmp")

    # setup the text for the menu
    text = []
    text1 = stage.Text(width=40, height=20, font=None, palette=constants.RED_PALETTE, buffer=None)
    text1.move(59, 20)
    text1.text("Pain")
    text.append(text1)

    text5 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
    text5.move(15, 45)
    text5.text("Press A to Shoot")
    text.append(text5)

    text3 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
    text3.move(5, 65)
    text3.text("Move Left and Right")
    text.append(text3)

    text4 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
    text4.move(25, 75)
    text4.text("With the Left")
    text.append(text4)

    text6 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
    text6.move(13, 85)
    text6.text("and Right Buttons")
    text.append(text6)

    text2 = stage.Text(width=29, height=12, font=None, palette=constants.RED_PALETTE, buffer=None)
    text2.move(4, 110)
    text2.text("Press Start To Play")
    text.append(text2)

    # set the background to a custom sprite
    background = stage.Grid(image_bank_mt_background, 10, 8)

    # setup background music
    back_music = open("white_space.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # setup the display and frame rate
    game = stage.Stage(ugame.display, 60)

    # sets the layers of the sprites
    game.layers = text + [background]

    # render all of the sprites
    game.render_block()

    # play the background music
    sound.play(back_music)

    # variable for looping background musiic
    back_music_time = 0

    while True:
        # handle inputs
        keys = ugame.buttons.get_pressed()

        if keys & ugame.K_START:
            game_scene()

        ### looping music system ###
        # check if 838 frames have gone by since last time the music was played
        if back_music_time >= 838:
            # play the music again
            sound.play(back_music)
            # reset the music frame counter to 0
            back_music_time = 0
        else:
            # increment the music frame counter
            back_music_time += 1

        # only ticks every 1/60th of a second
        game.tick()

# for the main game scene
def game_scene():
    # for placing the enemys
    def show_enemy():
        for enemy_number in range(len(enemys)):
            if enemys[enemy_number].x < 0:
                enemys[enemy_number].move(random.randint(0 + constants.SPRITE_SIZE, constants.SCREEN_X - constants.SPRITE_SIZE), constants.OFF_TOP_SCREEN)
                break

    # initialize score
    score = 0

    # setup score text
    score_text = stage.Text(width=29, height=14)
    score_text.clear()
    score_text.cursor(0,0)
    score_text.move(1,1)
    score_text.text(f"Score: {score}")

    # set the image bank on the pybadge
    image_bank_background = stage.Bank.from_bmp16("pain_background.bmp")
    image_bank_sprites = stage.Bank.from_bmp16("pain_sprites.bmp")

    # initialize buttons
    a_button = constants.button_state["button_up"]
    b_button = constants.button_state["button_up"]
    start_button = constants.button_state["button_up"]
    select_button = constants.button_state["button_up"]

    # sets the size of the background grid to 10*8
    background = stage.Grid(image_bank_background, 10, 8)

    # sets the position of the 4 player sprites
    player1 = stage.Sprite(image_bank_sprites, 2, 67, 98)
    player2 = stage.Sprite(image_bank_sprites, 3, 67, 114)
    player3 = stage.Sprite(image_bank_sprites, 4, 83, 98)
    player4 = stage.Sprite(image_bank_sprites, 5, 83, 114)

    # initialize the enemies
    enemys = []
    for enemy_number in range(constants.TOTAL_NUMBER_OF_ENEMYS):
        a_single_enemy1 = stage.Sprite(image_bank_sprites, 7, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
        #a_single_enemy2 = stage.Sprite(image_bank_sprites, 8, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y - 16)
        enemys.append(a_single_enemy1)
        #enemys.append(a_single_enemy2)
    show_enemy()

    # setup the audio files
    hit_sound = open("hit_sound.wav", 'rb')
    pew_sound = open("throw.wav", 'rb')
    death_sound = open("death_sound.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)

    # unused variable for background music
    back_music_time = 0

    # sets player to all of the player sprites
    #player = [player1] + [player2] + [player3] + [player4]

    # initializes the knifes
    knifes = []
    for knife_number in range(constants.TOTAL_NUMBER_OF_KNIFES):
        a_single_knife = stage.Sprite(image_bank_sprites, 10, constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
        knifes.append(a_single_knife)

    # displays the background and sets the frame rate to 60
    game = stage.Stage(ugame.display, 60)

    # sets the layers of the sprites
    game.layers = [score_text] + enemys + knifes + [player1] + [player2] + [player3] + [player4] + [background]

    # render all of the sprites
    game.render_block()

    while True:
        # handle inputs
        keys = ugame.buttons.get_pressed()

        if keys & ugame.K_X:
            if b_button == constants.button_state["button_up"]:
                b_button = constants.button_state["button_just_pressed"]
            elif b_button == constants.button_state["button_just_pressed"]:
                b_button = constants.button_state["button_still_pressed"]
        else:
            if b_button == constants.button_state["button_still_pressed"]:
                b_button = constants.button_state["button_released"]
            else:
                b_button = constants.button_state["button_up"]
            pass
        if keys & ugame.K_O:
            if a_button == constants.button_state["button_up"]:
                a_button = constants.button_state["button_just_pressed"]
            elif a_button == constants.button_state["button_just_pressed"]:
                a_button = constants.button_state["button_still_pressed"]
        else:
            if a_button == constants.button_state["button_still_pressed"]:
                a_button = constants.button_state["button_released"]
            else:
                a_button = constants.button_state["button_up"]
            pass
        if keys & ugame.K_START:
            pass
        if keys & ugame.K_SELECT:
            pass
        if keys & ugame.K_RIGHT:
            # check if the player is off screen
            if (player3.x + 10) < 160:
                # move all of the player sprites right
                player1.move(player1.x + 1, player1.y)
                player2.move(player2.x + 1, player2.y)
                player3.move(player3.x + 1, player3.y)
                player4.move(player4.x + 1, player4.y)
            else:
                # dont move the player sprites
                player1.move(134, player1.y)
                player2.move(134, player2.y)
                player3.move(150, player3.y)
                player4.move(150, player4.y)
        if keys & ugame.K_LEFT:
            # check if the player is off screen
            if (player1.x + 6) > 0:
                # move all of the player sprites left
                player1.move(player1.x - 1, player1.y)
                player2.move(player2.x - 1, player2.y)
                player3.move(player3.x - 1, player3.y)
                player4.move(player4.x - 1, player4.y)
            else:
                # dont move the player sprites
                player1.move(-6, player1.y)
                player2.move(-6, player2.y)
                player3.move(10, player3.y)
                player4.move(10, player4.y)

        if keys & ugame.K_UP:
            pass
            #player1.move(player1.x, player1.y - 1)
            #player2.move(player2.x, player2.y - 1)
            #player3.move(player3.x, player3.y - 1)
            #player4.move(player4.x, player4.y - 1)
        if keys & ugame.K_DOWN:
            pass
            #player1.move(player1.x, player1.y + 1)
            #player2.move(player2.x, player2.y + 1)
            #player3.move(player3.x, player3.y + 1)
            #player4.move(player4.x, player4.y + 1)

        # throwing knifes
        if a_button == constants.button_state["button_just_pressed"]:
            for knife_number in range(len(knifes)):
                if knifes[knife_number].x < 0:
                    knifes[knife_number].move(player1.x + 8, player1.y)
                    sound.play(pew_sound)
                    break

        # move the knifes
        for knife_number in range(len(knifes)):
            if knifes[knife_number].x > 0:
                knifes[knife_number].move(knifes[knife_number].x, knifes[knife_number].y - constants.KNIFE_SPEED)
                if knifes[knife_number].y < constants.OFF_TOP_SCREEN:
                    knifes[knife_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)

        # check knifes for colisons
        for knife_number in range(len(knifes)):
            if knifes[knife_number].x > 0:
                for enemy_number in range(len(enemys)):
                    if enemys[enemy_number].x > 0:
                        if stage.collide(knifes[knife_number].x + 6, knifes[knife_number].y + 2, knifes[knife_number].x + 11, knifes[knife_number].y + 12, enemys[enemy_number].x + 1, enemys[enemy_number].y, enemys[enemy_number].x + 15, enemys[enemy_number].y + 15):
                            # hide the enemy and the knife
                            enemys[enemy_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
                            knifes[knife_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
                            # stop current audio and play the hit sound
                            sound.stop()
                            sound.play(hit_sound)
                            # spawn two more enemys
                            show_enemy()
                            show_enemy()
                            # increment score
                            score += 1
                            score_text.clear()
                            score_text.cursor(0,0)
                            score_text.move(1,1)
                            score_text.text(f"Score: {score}")

        # move the enemys
        for enemy_number in range(len(enemys)):
            if enemys[enemy_number].x > 0:
                enemys[enemy_number].move(enemys[enemy_number].x, enemys[enemy_number].y + constants.ENEMY_SPEED)
                # if the enemy reaches the bottom of the screen
                if enemys[enemy_number].y > constants.SCREEN_Y:
                    # hide the enemy
                    enemys[enemy_number].move(constants.OFF_SCREEN_X, constants.OFF_SCREEN_Y)
                    # show an enemy
                    show_enemy()
                    # decrement score
                    score -= 1
                    score_text.clear()
                    score_text.cursor(0,0)
                    score_text.move(1,1)
                    score_text.text(f"Score: {score}")

        # check if the enemy colides with the player
        for enemy_number in range(len(enemys)):
            if enemys[enemy_number].x > 0:
                if stage.collide(enemys[enemy_number].x + 6, enemys[enemy_number].y + 2, enemys[enemy_number].x + 11, enemys[enemy_number].y + 12, player1.x, player1.y, player1.x + 25, player1.y + 25):
                    # stop current sounds and play the death sound
                    sound.stop()
                    sound.play(death_sound)
                    # wait three seconds
                    time.sleep(3.0)
                    # load the game over scene
                    game_over_scene(score)

        # if back_music_time >= 838:
        #     sound.play(back_music)
        #     back_music_time = 0
        # else:
        #     back_music_time += 1

        # renders the sprites every frame
        game.render_sprites([player1] + [player2] + [player3] + [player4] + knifes + enemys)

        # only ticks every 1/60th of a second
        game.tick()

def game_over_scene(final_score):
    # initialize audio and stop it
    sound = ugame.audio
    sound.stop()

    # open up the sprite sheet
    image_bank_2 = stage.Bank.from_bmp16("pain_menu.bmp")

    # set the bacground to a custom strite
    background =  stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y)

    # set all of the text
    text = []
    text1 = stage.Text(width=29, height=14, font=None, palette=constants.RED_PALETTE, buffer=None)
    text1.move(24, 20)
    text1.text(f"Final Score: {final_score}")
    text.append(text1)

    text3 = stage.Text(width=29, height=14, font=None, palette=constants.RED_PALETTE, buffer=None)
    text3.move(43, 60)
    text3.text(f"GAME OVER")
    text.append(text3)

    text2 = stage.Text(width=29, height=14, font=None, palette=constants.RED_PALETTE, buffer=None)
    text2.move(32, 110)
    text2.text(f"PRESS SELECT")
    text.append(text2)

    # sets up the display and frame rate
    game = stage.Stage(ugame.display, constants.FPS)

    # set the layers to render the text and the background
    game.layers = text + [background]

    # render everything
    game.render_block()

    # opens then plays music
    music = open("rick.wav", 'rb')
    sound = ugame.audio
    sound.stop()
    sound.mute(False)
    sound.play(music)

    while True:
        keys = ugame.buttons.get_pressed()

        # check if the player presed select
        if keys & ugame.K_SELECT != 0:
            # if they did restart
            supervisor.reload()

            game.tick()

if __name__ == "__main__":
    splash_screen()


# This Python 3 environment comes with many helpful analytics libraries installed

# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python

# For example, here's several helpful packages to load in

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the "../input/" directory.

# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os

for dirname, _, filenames in os.walk('/kaggle/input'):

    for filename in filenames:

        print(os.path.join(dirname, filename))

# Any results you write to the current directory are saved as output.
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score

from sklearn.model_selection import train_test_split

from xgboost import XGBRegressor

from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
train_df = pd.read_csv("../input/covid19-global-forecasting-week-3/train.csv")

test_df = pd.read_csv("../input/covid19-global-forecasting-week-3/test.csv")

submission_df = pd.read_csv("../input/covid19-global-forecasting-week-3/submission.csv")
train_df['Province_State'].fillna('',inplace=True)

test_df['Province_State'].fillna('',inplace=True)
lb = LabelEncoder()

train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])

test_df['Country_Region'] = lb.transform(test_df['Country_Region'])

lb1 = LabelEncoder()

train_df['Province_State'] = lb.fit_transform(train_df['Province_State'])

test_df['Province_State'] = lb.transform(test_df['Province_State'])
def split_date(date):

    date = date.split('-')

    date[0] = int(date[0])

    if(date[1][0] == '0'):

        date[1] = int(date[1][1])

    else:

        date[1] = int(date[1])

    if(date[2][0] == '0'):

        date[2] = int(date[2][1])

    else:

        date[2] = int(date[2])

    return date

train_df.Date = train_df.Date.apply(split_date)

test_df.Date = test_df.Date.apply(split_date)
year = []

month = []

day = []

for i in train_df.Date:

    year.append(i[0])

    month.append(i[1])

    day.append(i[2])

train_df['Year'] = year

train_df['Month'] = month

train_df['Day'] = day

del train_df['Date']
year = []

month = []

day = []

for i in test_df.Date:

    year.append(i[0])

    month.append(i[1])

    day.append(i[2])

test_df['Year'] = year

test_df['Month'] = month

test_df['Day'] = day

del test_df['Date']

del train_df['Id']

del test_df['ForecastId']

train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)

train_df['Fatalities'] = train_df['Fatalities'].apply(int)

cases = train_df.ConfirmedCases

fatalities = train_df.Fatalities

del train_df['ConfirmedCases']

del train_df['Fatalities']
scaler = MinMaxScaler()

x_train = scaler.fit_transform(train_df.values)

x_test = scaler.transform(test_df.values)
rf = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)

rf.fit(x_train,cases)

cases_pred = rf.predict(x_test)

cases_pred = np.around(cases_pred,decimals = 0)

cases_pred

x_train_cas = []

for i in range(len(x_train)):

    x = list(x_train[i])

    x.append(cases[i])

    x_train_cas.append(x)

x_train_cas = np.array(x_train_cas)
rf = XGBRegressor(n_estimators = 1500 , random_state = 0 , max_depth = 15)

rf.fit(x_train_cas,fatalities)

x_test_cas = []

for i in range(len(x_test)):

    x = list(x_test[i])

    x.append(cases_pred[i])

    x_test_cas.append(x)

x_test_cas = np.array(x_test_cas)

fatalities_pred = rf.predict(x_test_cas)

fatalities_pred = np.around(fatalities_pred,decimals = 0)

fatalities_pred
submission_df['ConfirmedCases'] = cases_pred

submission_df['Fatalities'] = fatalities_pred
submission_df.to_csv("submission.csv" , index = False)


import adios2
import numpy as np
import h5py

varname = "L0101"

adios = adios2.ADIOS()
IO = adios.DeclareIO("reader")
IO.SetEngine("BP4")

#bpStream = IO.Open("KSTAR.bp", adios2.Mode.Read)
bpStream = IO.Open("/global/cscratch1/sd/rkube/KSTAR/kstar_streaming/KSTAR_018431.bp", adios2.Mode.Read)
df_h5 = h5py.File("/global/cscratch1/sd/rkube/KSTAR/kstar_streaming/018431/ECEI.018431.LFS.h5")

ds = 10000
res = np.zeros(10000, dtype=np.float64)

for step in range(bpStream.Steps()):
    print("***Step: {0:d}".format(step))
    bpStream.BeginStep()

    var = IO.InquireVariable("ECEI_{0:s}".format(varname))
    L2408 = bpStream.Get(var, res, adios2.Mode.Sync)
    bpStream.EndStep()

    h5data = df_h5["/ECEI/ECEI_{0:s}/Voltage".format(varname)][(step) * ds:(step + 1) * ds]
    #print(h5data)

    print(varname, "*** BP: min = {0:6f}, max = {1:6f}, mean = {2:6f}".format(res.min(), res.max(), res.mean()))
    print(varname, "*** H5: min = {0:6f}, max = {1:6f}, mean = {2:6f}".format(h5data.min(), h5data.max(), h5data.mean()))

    if(step > 10):
        break

df_h5.close()


import pandas as pd
import numpy as np
import torch as th
import torch.nn as nn
import torch.nn.functional as F
import dgl
from dgl import edge_subgraph
from dgl.nn.functional import edge_softmax
import dgl.nn as dglnn
import dgl.function as fn
from sklearn.preprocessing import LabelEncoder, label_binarize
from tqdm import tqdm

import evaluation

class graph_2_df:
    def __init__(self,train_g,test_g,test_idx):

        self.train_g=train_g
        self.test_g=test_g
        self.test_idx=test_idx

    def df_creation(self):
        usaanr_feat=[]
        for key, scheme in self.train_g.node_attr_schemes(ntype="usaanr").items():
            usaanr_feat.append(key)
        usaanr_feat=[x for x in usaanr_feat if x not in
                     ['usaanr','cmpyelig','ACTCORP','Segment','train_mask','val_mask','test_mask','label','_ID']]

        train_idx=th.arange(self.train_g.num_nodes()).squeeze()
        test_idx=th.from_numpy(self.test_idx).squeeze()

        train_label=self.train_g.nodes['usaanr'].data['label'][train_idx]
        test_label=self.test_g.nodes['usaanr'].data['label'][test_idx]

        label_train=train_label.squeeze().numpy()
        label_test=test_label.squeeze().numpy()

        df_train=pd.DataFrame()
        for i,col in enumerate(usaanr_feat):
            ndata=self.train_g.nodes['usaanr'].data[col].squeeze().numpy()
            df_train[col]=ndata
        df_train['target_variable']=label_train
        df_train['mask']=0

        df_test=pd.DataFrame()
        for i,col in enumerate(usaanr_feat):
            ndata=self.test_g.nodes['usaanr'].data[col].squeeze().numpy()
            df_test[col]=ndata[self.test_idx]
        df_test['target_variable']=label_test
        df_test['mask']=1

        df_all=df_train.append(df_test)

        class_le=LabelEncoder()

        for i in tqdm(range(len(df_all.columns)),position=0,leave=True):
            col=df_all.columns[i]
            if col not in ["target_variable","mask"]:
                df_all[col]=df_all[col].astype('str')
                df_all[col]=class_le.fit_transform(df_all[col])
                df_all[col]=df_all[col].astype('str')

        df_train=df_all[df_all["mask"]==0]
        df_test=df_all[df_all["mask"]==1]

        train_y=df_train['target_variable'].values
        num_classes=th.unique(th.from_numpy(train_y)).shape[0]
        train_classes_num, train_classes_weight = evaluation.get_class_count_weight(train_y,num_classes)

        test_y=df_test['target_variable'].values

        df_train.drop(['target_variable', 'mask'], axis=1,inplace=True)
        df_test.drop(['target_variable', 'mask'], axis=1,inplace=True)
        categorical_index=np.where(df_train.dtypes==object)[0]

        return df_train, df_test, train_y, test_y, categorical_index,train_classes_weight


from django.conf import settings
from django.urls import include, path
from drf_spectacular.views import SpectacularAPIView, SpectacularSwaggerView
from rest_framework.authtoken import views

urlpatterns = [
    path("token/", views.obtain_auth_token, name="token"),
    path("", include("user_register.urls")),
    path("", include("classroom_creation.urls")),
    path("", include("grade_register.urls")),
    path("", include("absence_register.urls")),
    path("student/", include("student_consulting.urls")),
    path("teacher/", include("teacher_consulting.urls")),
    path("schema/", SpectacularAPIView.as_view(), name="schema"),
    # Optional UI:
    path(
        "docs/",
        SpectacularSwaggerView.as_view(url_name="schema"),
        name="swagger-ui",
    ),
]

if settings.DEBUG is True:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns += staticfiles_urlpatterns()