import pickle
import pandas as pd
import numpy as np
from functools import cached_property
from dfs_tools_mlb import settings
from dfs_tools_mlb import config
from dfs_tools_mlb.utils.storage import pickle_path
from dfs_tools_mlb.utils.time import time_frames as tf
from dfs_tools_mlb.compile import teams
from dfs_tools_mlb.utils.pd import modify_team_name
from dfs_tools_mlb.utils.pd import sm_merge_single
import random

class FDSlateSingle:
    def __init__(
        self,
        entries_file=config.get_fd_file(),
        slate_number=1,
        lineups=150,
        stack_points_weight=2,
        stack_threshold=120,
        heavy_weight_stack=False,
        max_batting_order=7,
        h_fades=[],
    ):

        self.entry_csv = entries_file
        if not self.entry_csv:
            raise TypeError(
                "There are no fanduel entries files in specified DL_FOLDER, obtain one at fanduel.com/upcoming"
            )

        self.slate_number = slate_number
        self.lineups = lineups

        self.points_weight = stack_points_weight
        self.stack_threshold = stack_threshold

        self.heavy_weight_stack = heavy_weight_stack

        self.max_batting_order = max_batting_order
        self.h_fades = h_fades

    def entries_df(self, reset=False):
        df_file = pickle_path(
            name=f"lineup_entries_{tf.today}_{self.slate_number}",
            directory=settings.FD_DIR,
        )
        path = settings.FD_DIR.joinpath(df_file)
        if path.exists() and not reset:
            df = pd.read_pickle(path)
        else:
            cols = [
                "entry_id",
                "contest_id",
                "contest_name",
                "MVP - 2X Points",
                "STAR - 1.5X Points",
                "UTIL",
                "UTIL.1",
                "UTIL.2",
            ]
            csv_file = self.entry_csv
            with open(csv_file, "r") as f:
                df = pd.read_csv(f, usecols=cols)
            df = df[~df["entry_id"].isna()]
            df = df.astype({"entry_id": np.int64})
            with open(df_file, "wb") as f:
                pickle.dump(df, f)
        return df

    @cached_property
    def player_info_df(self):
        df_file = pickle_path(
            name=f"player_info_{tf.today}_{self.slate_number}",
            directory=settings.FD_DIR,
        )
        path = settings.FD_DIR.joinpath(df_file)
        if path.exists():
            df = pd.read_pickle(path)
        else:
            cols = [
                "Player ID + Player Name",
                "Id",
                "Position",
                "First Name",
                "Nickname",
                "Last Name",
                "FPPG",
                "Salary",
                "Game",
                "Team",
                "Opponent",
                "Injury Indicator",
                "Injury Details",
                "Roster Position",
            ]
            csv_file = self.entry_csv
            with open(csv_file, "r") as f:
                df = pd.read_csv(f, skiprows=lambda x: x < 6, usecols=cols)
            df.rename(
                columns={
                    "Id": "fd_id",
                    "Player ID + Player Name": "fd_id_name",
                    "Nickname": "name",
                    "Position": "fd_position",
                    "Roster Position": "fd_r_position",
                    "Injury Indicator": "fd_injury_i",
                    "Injury Details": "fd_injury_d",
                    "Opponent": "opp",
                    "First Name": "f_name",
                    "Last Name": "l_name",
                    "Salary": "fd_salary",
                },
                inplace=True,
            )

            df.columns = df.columns.str.lower()
            df = modify_team_name(df, columns=["team", "opp"])

            with open(df_file, "wb") as f:
                pickle.dump(df, f)
        return df

    @cached_property
    def active_teams(self):
        return self.player_info_df["team"].unique()

    @cached_property
    def default_stack_dict(self):
        team_dict = {}
        for team in self.active_teams:
            team_dict[team] = 0
        return team_dict

    def insert_lineup(self, idx, lineup):
        cols = ["MVP - 2X Points", "STAR - 1.5X Points", "UTIL", "UTIL.1", "UTIL.2"]
        df = self.entries_df()
        df.loc[idx, cols] = lineup
        file = pickle_path(
            name=f"lineup_entries_{tf.today}_{self.slate_number}",
            directory=settings.FD_DIR,
        )
        with open(file, "wb") as f:
            pickle.dump(df, f)
        return f"Inserted {lineup} at index {idx}."

    def finalize_entries(self):
        df = self.entries_df()
        csv = self.entry_csv
        df.rename(columns={"UTIL.1": "UTIL", "UTIL.2": "UTIL"}, inplace=True)
        df.to_csv(csv, index=False)
        return f"Stored lineups at {csv}"

    @cached_property
    def team_instances(self):
        instances = set()
        for team in teams.Team:
            if team.name in self.active_teams:
                instances.add(team)
        return instances

    def get_hitters(self):
        df_file = pickle_path(
            name=f"all_h_{tf.today}_{self.slate_number}", directory=settings.FD_DIR
        )
        path = settings.FD_DIR.joinpath(df_file)
        for team in self.team_instances:
            hitters = team.lineup_df()
            merge = self.player_info_df[(self.player_info_df["team"] == team.name)]
            hitters = sm_merge_single(hitters, merge, ratio=0.75, suffixes=("", "_fd"))
            hitters.drop_duplicates(subset="mlb_id", inplace=True)
            order_filter = hitters["order"] <= self.max_batting_order
            hitters_order = hitters[order_filter]
            team.salary = hitters_order["fd_salary"].sum() / len(hitters_order.index)
            cols = [
                "raw_points",
                "venue_points",
                "temp_points",
                "points",
                "salary",
                "sp_mu",
            ]
            points_file = pickle_path(
                name=f"team_points_{tf.today}_{self.slate_number}",
                directory=settings.FD_DIR,
            )
            points_path = settings.FD_DIR.joinpath(points_file)
            if points_path.exists():
                p_df = pd.read_pickle(points_path)
            else:
                p_df = pd.DataFrame(columns=cols)
            p_df.loc[team.name, cols] = [
                team.raw_points,
                team.venue_points,
                team.temp_points,
                team.points,
                team.salary,
                team.sp_mu,
            ]
            with open(points_file, "wb") as f:
                pickle.dump(p_df, f)
            if path.exists():
                df = pd.read_pickle(path)
                df.drop(index=df[df["team"] == team.name].index, inplace=True)
                df = pd.concat([df, hitters], ignore_index=True)
            else:
                df = hitters
            with open(df_file, "wb") as f:
                pickle.dump(df, f)

        return df

    def h_df(self):
        df_file = pickle_path(
            name=f"all_h_{tf.today}_{self.slate_number}", directory=settings.FD_DIR
        )
        path = settings.FD_DIR.joinpath(df_file)
        if not path.exists():
            df = self.get_hitters()
        else:
            df = pd.read_pickle(path)
        return df

    def points_df(self):
        file = pickle_path(
            name=f"team_points_{tf.today}_{self.slate_number}",
            directory=settings.FD_DIR,
        )
        path = settings.FD_DIR.joinpath(file)
        if not path.exists():
            self.get_hitters()
        df = pd.read_pickle(path).apply(pd.to_numeric)
        return df

    def stacks_df(self):
        lineups = self.lineups
        df = self.points_df()

        df["p_z"] = (
            (df["points"] - df["points"].mean()) / df["points"].std()
        ) * self.points_weight
        df["s_z"] = ((df["salary"] - df["salary"].mean()) / df["salary"].std()) * -(
            2 - self.points_weight
        )
        df["stacks"] = 1000
        increment = 0
        while df["stacks"].max() > self.stack_threshold:
            df_c = df.copy()
            df_c["z"] = ((df_c["p_z"] + df_c["s_z"]) / 2) + increment
            df_c = df_c[df_c["z"] > 0]
            lu_base = lineups / len(df_c.index)
            df_c["stacks"] = lu_base * df_c["z"]
            if df_c["stacks"].max() > self.stack_threshold:
                increment += 0.01
                continue
            diff = lineups - df_c["stacks"].sum()
            df_c["stacks"] = round(df_c["stacks"])
            while df_c["stacks"].sum() < lineups:
                if self.heavy_weight_stack:
                    df_c["stacks"] = df_c["stacks"] + np.ceil(
                        ((diff / len(df.index)) * df_c["z"])
                    )
                else:
                    df_c["stacks"] = df_c["stacks"] + np.ceil((diff / len(df.index)))
                df_c["stacks"] = round(df_c["stacks"])
            while df_c["stacks"].sum() > lineups:
                for idx in df_c.index:
                    if df_c["stacks"].sum() == lineups:
                        break
                    else:
                        df_c.loc[idx, "stacks"] -= 1
            if df_c["stacks"].max() > self.stack_threshold:
                increment += 0.01
                continue
            df = df_c

        i = df[df["stacks"] == 0].index
        df.drop(index=i, inplace=True)
        return df

    def h_counts(self):
        file = pickle_path(
            name=f"h_counts_{tf.today}_{self.slate_number}", directory=settings.FD_DIR
        )
        path = settings.FD_DIR.joinpath(file)
        if path.exists():
            df = pd.read_pickle(path)
            return df
        else:
            return "No lineups stored yet."

    def build_lineups(
        self,
        lus=150,
        index_track=0,
        max_lu_total=90,
        max_sal=35000,
        stack_sample=4,
        util_replace_filt=1000,
        variance=0,
        non_stack_max_order=5,
        custom_counts={},
        custom_stacks=None,
        below_avg_count=30,
        stack_expand_limit=30,
        exempt=[],
        full_stack_cutoff=50,
    ):
        max_order = self.max_batting_order
        # all hitters in slate
        h = self.h_df()
        # dropping faded, platoon, and low order (max_order) players.
        h_fade_filt = h["fd_id"].isin(self.h_fades)
        h_order_filt = h["order"] > max_order
        h_exempt_filt = ~h["fd_id"].isin(exempt)
        hfi = h[(h_fade_filt | h_order_filt) & h_exempt_filt].index
        h.drop(index=hfi, inplace=True)
        # count each players entries
        h["t_count"] = 0
        # count non_stack
        h["ns_count"] = 0
        h_count_df = h.copy()
        # risk_limit should always be >= below_avg_count
        h.loc[
            (h["exp_ps_sp_pa"] < h["exp_ps_sp_pa"].median()), "t_count"
        ] = below_avg_count
        exempt_filt = h["fd_id"].isin(exempt)
        h.loc[exempt_filt, "t_count"] = 0
        for k, v in custom_counts.items():
            h.loc[h["fd_id"] == k, "t_count"] = v
        # team: stacks to build
        if not custom_stacks:
            s = self.stacks_df()["stacks"].to_dict()
        else:
            s = custom_stacks
        # lineups to build
        sorted_lus = []
        while lus > 0:
            salary = 0
            # if lineup fails requirements, reset will be set to true.
            reset = False
            if lus > full_stack_cutoff:
                stack_size = 4
            else:
                stack_size = 3

            stacks = {k: v for k, v in s.items() if v > 0}
            stack = random.choice(list(stacks.keys()))
            remaining_stacks = stacks[stack]
            # lookup players on the team for the selected stack
            stack_df = h[h["team"] == stack]
            # stack_key = 'exp_ps_sp_pa'
            # non_stack_key = 'exp_ps_sp_pa'
            if remaining_stacks % 4 == 0:
                stack_key = "total_pitches"
            elif remaining_stacks % 3 == 0:
                stack_key = "fd_hr_weight"
            elif remaining_stacks % 2 == 0:
                stack_key = "points"
            else:
                stack_key = "exp_ps_sp_pa"
            if lus % 2 == 0:
                non_stack_key = "exp_ps_sp_pa"
            else:
                non_stack_key = "points"
            # filter the selected stack by stack_sample arg.
            if remaining_stacks > stack_expand_limit:
                highest = stack_df.loc[
                    stack_df[stack_key].nlargest(stack_sample + 1).index
                ]
            else:
                highest = stack_df.loc[stack_df[stack_key].nlargest(stack_sample).index]
            # array of fanduel ids of the selected hitters
            stack_ids = highest["fd_id"].values
            # initial empty lineup, ordered by fanduel structed and mapped by p_map
            lineup = [None, None, None, None, None]
            # insert current pitcher into lineup

            # try to create a 4-man stack that satifies position requirements 5 times, else 3-man stack.
            try:
                samp = random.sample(sorted(stack_ids), stack_size)
            except ValueError:
                samp = random.sample(sorted(stack_ids), stack_size - 1)
            stack_salary = h.loc[h["fd_id"].isin(samp), "fd_salary"].sum()
            salary += stack_salary
            rem_sal = max_sal - salary
            random.shuffle(stack_ids)

            for x, y in enumerate(samp):
                lineup[x] = y

            # filter out hitters on the team of the current stack, as they are already in the lineup.
            stack_filt = h["team"] != stack
            # filter out players hitting below specified lineup spot
            order_filt = (h["order"] <= non_stack_max_order) | exempt_filt
            # filter out players not on a team being stacked on slate and proj. points not in 90th percentile.
            max_stack = max(stacks.values())
            # variance default is 0
            count_filt = h["t_count"] < ((max_lu_total - max_stack) - variance)
            plat_filt = h["is_platoon"] != True
            for y, z in enumerate(lineup):
                if not z:
                    # filter out players already in lineup, lineup will change with each iteration
                    dupe_filt = ~h["fd_id"].isin(lineup)
                    # filter out players not eligible for the current position being filled.
                    # get the ammount of roster spots that need filling.
                    npl = len([idx for idx, spot in enumerate(lineup) if not spot])
                    # the avgerage salary remaining for each empty lineup spot
                    avg_sal = rem_sal / npl
                    # filter out players with a salary greater than the average avg_sal above
                    sal_filt = h["fd_salary"] <= avg_sal

                    try:
                        hitters = h[
                            stack_filt
                            & dupe_filt
                            & count_filt
                            & sal_filt
                            & order_filt
                            & plat_filt
                        ]
                        hitter = hitters.loc[hitters[non_stack_key].idxmax()]
                    except (KeyError, ValueError):
                        try:
                            hitters = h[
                                stack_filt
                                & dupe_filt
                                & count_filt
                                & sal_filt
                                & order_filt
                            ]
                            hitter = hitters.loc[hitters[non_stack_key].idxmax()]
                        except (KeyError, ValueError):
                            try:
                                hitters = h[
                                    stack_filt
                                    & dupe_filt
                                    & count_filt
                                    & sal_filt
                                    & plat_filt
                                ]
                                hitter = hitters.loc[hitters[non_stack_key].idxmax()]
                            except (KeyError, ValueError):
                                try:
                                    hitters = h[
                                        stack_filt
                                        & dupe_filt
                                        & count_filt
                                        & order_filt
                                        & plat_filt
                                    ]
                                    hitter = hitters.loc[
                                        hitters[non_stack_key].idxmax()
                                    ]
                                except:
                                    hitters = h[
                                        stack_filt
                                        & dupe_filt
                                        & sal_filt
                                        & order_filt
                                        & plat_filt
                                    ]
                                    hitter = hitters.loc[
                                        hitters[non_stack_key].idxmax()
                                    ]
                    salary += hitter["fd_salary"].item()
                    rem_sal = max_sal - salary
                    # if the selected hitter's salary put the lineup over the max. salary, try to find replacement.
                    if rem_sal < 0:
                        r_sal = hitter["fd_salary"].item()
                        try:
                            salary_df = hitters[
                                (hitters["fd_salary"] <= (r_sal + rem_sal))
                            ]
                            hitter = salary_df.loc[hitters[non_stack_key].idxmax()]
                            salary += hitter["fd_salary"]
                            salary -= r_sal
                            rem_sal = max_sal - salary
                        except (ValueError, KeyError):
                            try:
                                hitters = h[stack_filt & dupe_filt]
                                salary_df = hitters[
                                    (hitters["fd_salary"] <= (r_sal + rem_sal))
                                ]
                                hitter = salary_df.loc[hitters[non_stack_key].idxmax()]
                                salary += hitter["fd_salary"]
                                salary -= r_sal
                                rem_sal = max_sal - salary

                            except (KeyError, ValueError):
                                reset = True
                                print("resetting")
                                break

                    h_id = hitter["fd_id"]
                    lineup[y] = h_id
                    used_players = []
                    print(lineup)

            while not reset and lineup[0:2] + sorted(lineup[2:5]) in sorted_lus:
                mvp = lineup[0]
                all_star = lineup[1]
                potential_lu = lineup[0:2] + sorted(lineup[2:5])
                potential_lu[0] = all_star
                potential_lu[1] = mvp
                if potential_lu not in sorted_lus:
                    lineup = potential_lu
                    break
                try:
                    # redeclare use_teams each pass
                    h_df = h[h["fd_id"].isin(lineup)]
                    # append players already attempted to used_players and filter them out each loop
                    used_filt = ~h["fd_id"].isin(used_players)
                    dupe_filt = ~h["fd_id"].isin(lineup)
                    utility = h[h["fd_id"] == lineup[4]]
                    used_players.append(utility["fd_id"])
                    r_salary = utility["fd_salary"].item()
                    # only use players with a salary between the (util's salary - util_replace_filt) and maxiumum salary.
                    sal_filt = h["fd_salary"].between(
                        (r_salary - util_replace_filt), (rem_sal + r_salary)
                    )
                    # don't use players on team against current pitcher
                    hitters = h[
                        dupe_filt
                        & sal_filt
                        & used_filt
                        & count_filt
                        & stack_filt
                        & order_filt
                        & plat_filt
                    ]
                    hitter = hitters.loc[hitters[non_stack_key].idxmax()]
                    used_players.append(hitter["fd_id"])
                    salary += hitter["fd_salary"].item()
                    salary -= utility["fd_salary"].item()
                    rem_sal = max_sal - salary
                    lineup[4] = hitter["fd_id"]
                    h_df = h[h["fd_id"].isin(lineup)]
                # same as above, but no fade_filt and increasing minimum thresehold by 100.
                except (KeyError, ValueError):
                    try:
                        # redeclare use_teams each pass
                        h_df = h[h["fd_id"].isin(lineup)]
                        # append players already attempted to used_players and filter them out each loop
                        used_filt = ~h["fd_id"].isin(used_players)
                        dupe_filt = ~h["fd_id"].isin(lineup)
                        utility = h[h["fd_id"] == lineup[3]]
                        used_players.append(utility["fd_id"])
                        r_salary = utility["fd_salary"].item()
                        # only use players with a salary between the (util's salary - util_replace_filt) and maxiumum salary.
                        sal_filt = h["fd_salary"].between(
                            (r_salary - util_replace_filt), (rem_sal + r_salary)
                        )
                        # don't use players on team against current pitcher
                        hitters = h[
                            dupe_filt
                            & sal_filt
                            & used_filt
                            & count_filt
                            & stack_filt
                            & order_filt
                            & plat_filt
                        ]
                        hitter = hitters.loc[hitters[non_stack_key].idxmax()]
                        used_players.append(hitter["fd_id"])
                        salary += hitter["fd_salary"].item()
                        salary -= utility["fd_salary"].item()
                        rem_sal = max_sal - salary
                        lineup[3] = hitter["fd_id"]
                        h_df = h[h["fd_id"].isin(lineup)]
                    except (KeyError, ValueError):
                        reset = True
                        print("resetting")
                        break
            if reset == True:
                continue
            #!!the lineup meets all parameters at this point.!!
            # decrease lus arg, loop ends at 0.
            lus -= 1
            # append the new lineup to the sorted lus list for next loop.
            sorted_lus.append(lineup[0:2] + sorted(lineup[2:5]))
            # insert the lineup in the lineup df
            self.insert_lineup(index_track, lineup)
            # increase index for next lineup insertion.
            index_track += 1
            # decrease the current stack's value, so it won't be attempted once it reaches 0.
            s[stack] -= 1
            # keep track of total insertions and stack insertions, players exceeding max_lu_total will be dropped next loop.
            lu_filt = h["fd_id"].isin(lineup)
            h.loc[lu_filt, "t_count"] += 1
            h.loc[lu_filt & stack_filt, "ns_count"] += 1
            # print(lus)
            # print(index_track)
            print(salary)
            print(lus)
            # keep track of players counts, regardless if they're eventually dropped.
            h_count_df.loc[(h_count_df["fd_id"].isin(lineup)), "t_count"] += 1

        # dump the counts into pickled df for analysis
        h_count_file = pickle_path(
            name=f"h_counts_{tf.today}_{self.slate_number}", directory=settings.FD_DIR
        )

        with open(h_count_file, "wb") as f:
            pickle.dump(h_count_df, f)

        return sorted_lus


pessoas = list()
dados = dict()
media = 0

while True:

    dados['nome'] = input("\nNome: ").strip()

    while True:

        dados['sexo'] = input("Sexo: [M/F] ").strip().upper()

        if dados['sexo'] in ['M','F']: break

    dados['idade'] = int(input("Idade: "))
    media += dados['idade']

    pessoas.append(dados.copy())

    while True:

        op = input("\nQuer continuar: [S/N] ").strip().upper()

        if op in ['S','N']: break

    if op == 'N': break

media /= len(pessoas)

print('', '-=' * 30, '', sep='\n')

print(f'A) Ao todo temos {len(pessoas)} pessoas cadastradas.')
print(f'B) A média de idade é de {media} anos.')
print(f'C) As mulheres cadastradas foram ', end='')

for pessoa in pessoas:

    if pessoa['sexo'] == 'F':
        print(f'[{pessoa["nome"]}]', end=' ')

print(f'\nD) Lista das pessoas que estão acima da média:')

for pessoa in pessoas:

    if pessoa['idade'] >= media:
        print(f'\t nome = {pessoa["nome"]}; sexo = {pessoa["sexo"]}; idade = {pessoa["idade"]};')

print()


# read experiment data from excel file
from operator import itemgetter
import pandas as pd
import numpy as np

def readExpDataSorted(fileName, flagPercent, flagTrueStrainStress):
    """
    Parameters
    -----------------
    fileName:
        experimental data file name including directory information
    flagPercent:
        to check whether strain is measured in percent (%) form
        0: correct form, strain = 0.01,0.02, etc
        1: percent form, strain = 0.01 should actually be 0.01%.
            therefore, should * 0.01 to strain_exp list
    flagTrueStrainStress:
        controls whether convert exp data into true strain stress data
        0: not convert, this means maybe we dont need true strain stress
            data, or the exp data has been converted to true strain stress
        1: convert exp data to true strain stress data by the following
            equation:
            sigma_true = sigma_engineering * (1 + epsilon_engineering)
            epsilon_true = ln(1 + epsilon_engineering)

    Return:
    -----------------
    strain_exp and stress_exp lists sorted following strain_exp list
        in an ascending order

    strain_exp:
        strain values in a list form
    stress_exp:
        stress values in a list form
    """
    df = pd.read_excel(fileName)

    # check flagPercent
    if flagPercent == 1:
        strain_exp = df.values[:, 0] * 0.01
    else:
        strain_exp = df.values[:, 0]

    stress_exp = df.values[:, 1]

    # ###################################################################
    # sort it to avoid possible fatal errors
    # assemble into a 2d list
    strain_stress_exp = list(zip(strain_exp, stress_exp))
    # sort it
    strain_stress_exp.sort(key=itemgetter(0), reverse=False)
    # get 1st column in strain_stress_exp
    strain_exp = [i[0] for i in strain_stress_exp]
    # get 2nd column in strain_stress_exp
    stress_exp = [i[1] for i in strain_stress_exp]

    # convert to true strain stress data when flagTrueStrainStress == 1
    if flagTrueStrainStress == 1:
        stress_exp = [item * (1 + strain_exp[index]) for
                      index, item in enumerate(stress_exp)]
        strain_exp = [np.log(1 + i) for i in strain_exp]

    return strain_exp, stress_exp


def fibonacci(n,count):
    if(n==0):
      count[0] += 1
      return 0
    elif (n==1):
      count[1] += 1
      return 1
    else:
       return fibonacci(n-1,count) + fibonacci(n-2,count)

T = int(input())
T_list = []

for i in range(T):
  T_list.append(int(input()))

for i in range(T):
  count = [0,0]
  fibonacci(T_list[i],count)
  print(str(count[0]) + ' ' + str(count[1]))
# 파이썬은 list, dict, set와 같이 mutable object가 argument로 넘어가면
# object reference가 넘어가서 담고 있는 값을 바꿀 수 있다
# immutable object인 int, float, str, tuples 등은 단일 값이거나 static 속성
# 하지만 이렇게 재귀형식으로 푼다면 오류!!(시간제한)


from flask import Flask, render_template, request

from base64 import b64decode, b64encode
import cv2
import io
import json

import requests
import numpy as np

app = Flask(__name__)

def preprocess_image(image_file_path, max_width, max_height):
    """Preprocesses input images for AutoML Vision Edge models.

    Args:
        image_file_path: Path to a local image for the prediction request.
        max_width: The max width for preprocessed images. The max width is 640
            (1024) for AutoML Vision Image Classfication (Object Detection)
            models.
        max_height: The max width for preprocessed images. The max height is
            480 (1024) for AutoML Vision Image Classfication (Object
            Detetion) models.
    Returns:
        The preprocessed encoded image bytes.
    """
    # cv2 is used to read, resize and encode images.
    encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 85]
    # im = cv2.imread(image_file_path)
    # im=request.files.get('image')
    # im = cv2.imdecode(np.frombuffer(io_buf.getbuffer(), np.uint8), -1)
    im=image_file_path
    [height, width, _] = im.shape
    if height > max_height or width > max_width:
        ratio = max(height / float(max_width), width / float(max_height))
        new_height = int(height / ratio + 0.5)
        new_width = int(width / ratio + 0.5)
        resized_im = cv2.resize(
            im, (new_width, new_height), interpolation=cv2.INTER_AREA)
        _, processed_image = cv2.imencode('.jpg', resized_im, encode_param)
    else:
        _, processed_image = cv2.imencode('.jpg', im, encode_param)

    print("imagepreprocessed")
    return b64encode(processed_image).decode('utf-8')

def container_predict(image_file_path, image_key, port_number=8501):
    """Sends a prediction request to TFServing docker container REST API.

    Args:
        image_file_path: Path to a local image for the prediction request.
        image_key: Your chosen string key to identify the given image.
        port_number: The port number on your device to accept REST API calls.
    Returns:
        The response of the prediction request.
    """
    # AutoML Vision Edge models will preprocess the input images.
    # The max width and height for AutoML Vision Image Classification and
    # Object Detection models are 640*480 and 1024*1024 separately. The
    # example here is for Image Classification models.
    encoded_image = preprocess_image(
        image_file_path=image_file_path, max_width=640, max_height=480)

    # The example here only shows prediction with one image. You can extend it
    # to predict with a batch of images indicated by different keys, which can
    # make sure that the responses corresponding to the given image.
    # instances = {
    #         'instances': [
    #                 {'image_bytes': {'b64': str(encoded_image)},
    #                  'key': image_key}
    #         ]
    # }
    #
    # # This example shows sending requests in the same server that you start
    # # docker containers. If you would like to send requests to other servers,
    # # please change localhost to IP of other servers.
    # url = 'http://0.0.0.0:{}/v1/models/default:predict'.format(port_number)
    #
    # response = requests.post(url, data=json.dumps(instances))
    #
    # response1 = json.loads(response.content)

    payload = {
        'instances': [
            {'image_bytes': {'b64': str(encoded_image)},
             'key': image_key}
        ]
    }

    # This example shows sending requests in the same server that you start
    # docker containers. If you would like to send requests to other servers,
    # please change localhost to IP of other servers.
    url = 'http://0.0.0.0:{}/v1/models/default:predict'.format(port_number)
    url = 'https://dog-classifier-x2cqll44ga-ew.a.run.app/v1/models/default:predict'
    response = requests.post(url, data=json.dumps(payload))
    print("here")
    # print(response.content)
    # print(type(response.content))
    response1 = json.loads(response.content)

    x = response1["predictions"][0]["scores"]
    max_value = max(x)
    max_index = x.index(max_value)
    breed = response1["predictions"][0]["labels"][max_index]

    print(type(x))
    print(x)
    print(breed)
    return breed

# def predict_label(img_path):
#     i = image.load_img(img_path, target_size=(100,100))
# 	i = image.img_to_array(i)/255.0
# 	i = i.reshape(1, 100,100,3)
# 	p = model.predict_classes(i)
# 	return dic[p[0]]

# routes
@app.route("/", methods=['GET', 'POST'])
def main():
    return render_template("index.html")

@app.route("/about")
def about_page():
    return "Please subscribe  Artificial Intelligence Hub..!!!"

@app.route("/submit", methods = ['GET', 'POST'])
def get_output():
    if request.method == 'POST':
        img = request.files['my_image']
        imgr=img.read()
        img_b64 = b64encode(imgr).decode()
        img_src = 'data:{};base64,{}'.format(img.content_type, img_b64)
        npimg = np.fromstring(imgr, np.uint8)
        img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
        # p = container_predict(img, "1", 8501)

        # img_path = "tmp/" + img.filename
        # img.save(img_path)
        img_path=img
        print(type(img_path))

        p= container_predict(img_path,"1",8501)
        # p = predict_label(img_path)

    return render_template("index.html", prediction = p, img_path = img_src)

if __name__ =='__main__':
    #app.debug = True
    app.run(host='0.0.0.0', port=5000)


from __future__ import annotations

__all__ = [
    "BasePeriodicTimeSeriesGenerator",
    "is_periodic_timeseries_generator_config",
    "setup_periodic_timeseries_generator",
]

import logging
from abc import ABC, abstractmethod

from objectory import AbstractFactory
from objectory.utils import is_object_config
from redcat import BatchDict
from torch import Generator

from startorch.utils.format import str_target_object

logger = logging.getLogger(__name__)

class BasePeriodicTimeSeriesGenerator(ABC, metaclass=AbstractFactory):
    r"""Defines the base class to generate periodic time series.

    A child class has to implement the ``generate`` method.

    Example usage:

    .. code-block:: pycon

        Example usage:

    .. code-block:: pycon

        >>> from startorch.periodic.timeseries import Repeat
        >>> from startorch.timeseries import TimeSeries
        >>> from startorch.sequence import RandUniform
        >>> generator = Repeat(TimeSeries({"value": RandUniform(), "time": RandUniform()}))
        >>> generator
        RepeatPeriodicTimeSeriesGenerator(
          (generator): TimeSeriesGenerator(
              (value): RandUniformSequenceGenerator(low=0.0, high=1.0, feature_size=(1,))
              (time): RandUniformSequenceGenerator(low=0.0, high=1.0, feature_size=(1,))
            )
        )
        >>> generator.generate(seq_len=12, period=4, batch_size=4)
        BatchDict(
          (value): tensor([[...]], batch_dim=0, seq_dim=1)
          (time): tensor([[...]], batch_dim=0, seq_dim=1)
        )
    """

    @abstractmethod
    def generate(
        self, seq_len: int, period: int, batch_size: int = 1, rng: Generator | None = None
    ) -> BatchDict:
        r"""Generates a batch of periodic time series.

        All the time series in the batch have the same length.

        Args:
        ----
            seq_len (int): Specifies the sequence length.
            period (int): Specifies the period.
            batch_size (int, optional): Specifies the batch size.
                Default: ``1``
            rng (``torch.Generator`` or None, optional): Specifies
                an optional random number generator. Default: ``None``

        Returns:
        -------
            ``BatchDict``: A batch of periodic time series.

        Example usage:

        .. code-block:: pycon

            >>> from startorch.periodic.timeseries import Repeat
            >>> from startorch.timeseries import TimeSeries
            >>> from startorch.sequence import RandUniform
            >>> generator = Repeat(TimeSeries({"value": RandUniform(), "time": RandUniform()}))
            >>> generator.generate(seq_len=12, period=4, batch_size=4)
            BatchDict(
              (value): tensor([[...]], batch_dim=0, seq_dim=1)
              (time): tensor([[...]], batch_dim=0, seq_dim=1)
            )
        """

def is_periodic_timeseries_generator_config(config: dict) -> bool:
    r"""Indicates if the input configuration is a configuration for a
    ``BasePeriodicTimeSeriesGenerator``.

    This function only checks if the value of the key  ``_target_``
    is valid. It does not check the other values. If ``_target_``
    indicates a function, the returned type hint is used to check
    the class.

    Args:
    ----
        config (dict): Specifies the configuration to check.

    Returns:
    -------
        bool: ``True`` if the input configuration is a configuration
            for a ``BasePeriodicTimeSeriesGenerator`` object.

    Example usage:

    .. code-block:: pycon

        >>> from startorch.periodic.timeseries import is_periodic_timeseries_generator_config
        >>> is_periodic_timeseries_generator_config(
        ...     {
        ...         "_target_": "startorch.periodic.timeseries.Repeat",
        ...         "generator": {
        ...             "_target_": "startorch.timeseries.TimeSeries",
        ...             "sequences": {
        ...                 "value": {"_target_": "startorch.sequence.RandUniform"},
        ...                 "time": {"_target_": "startorch.sequence.RandUniform"},
        ...             },
        ...         },
        ...     }
        ... )
        True
    """
    return is_object_config(config, BasePeriodicTimeSeriesGenerator)

def setup_periodic_timeseries_generator(
    generator: BasePeriodicTimeSeriesGenerator | dict,
) -> BasePeriodicTimeSeriesGenerator:
    r"""Sets up a periodic time series generator.

    The time series generator is instantiated from its configuration by
    using the ``BasePeriodicTimeSeriesGenerator`` factory function.

    Args:
    ----
        generator (``BasePeriodicTimeSeriesGenerator`` or dict): Specifies a
            periodic time series generator or its configuration.

    Returns:
    -------
        ``BasePeriodicTimeSeriesGenerator``: A periodic time series generator.

    Example usage:

    .. code-block:: pycon

        >>> from startorch.periodic.timeseries import setup_periodic_timeseries_generator
        >>> setup_periodic_timeseries_generator(
        ...     {
        ...         "_target_": "startorch.periodic.timeseries.Repeat",
        ...         "generator": {
        ...             "_target_": "startorch.timeseries.TimeSeries",
        ...             "sequences": {
        ...                 "value": {"_target_": "startorch.sequence.RandUniform"},
        ...                 "time": {"_target_": "startorch.sequence.RandUniform"},
        ...             },
        ...         },
        ...     }
        ... )
        RepeatPeriodicTimeSeriesGenerator(
          (generator): TimeSeriesGenerator(
              (value): RandUniformSequenceGenerator(low=0.0, high=1.0, feature_size=(1,))
              (time): RandUniformSequenceGenerator(low=0.0, high=1.0, feature_size=(1,))
            )
        )
    """
    if isinstance(generator, dict):
        logger.info(
            "Initializing a periodic time series generator from its configuration... "
            f"{str_target_object(generator)}"
        )
        generator = BasePeriodicTimeSeriesGenerator.factory(**generator)
    if not isinstance(generator, BasePeriodicTimeSeriesGenerator):
        logger.warning(
            f"generator is not a `BasePeriodicTimeSeriesGenerator` (received: {type(generator)})"
        )
    return generator


from fastapi import FastAPI
from pydantic import BaseModel,Field
from typing import Optional
from uuid import UUID

app = FastAPI()

class Book(BaseModel):
    id:UUID
    title: str = Field(min_length=1) # Data validation
    author: str = Field(min_length=1,max_length=100)
    description: Optional[str] = Field(title="Description of the book",min_length = 1,max_length=100)
    rating : int = Field(gt=-1,lt=101)

Books = []

@app.get("/")
async def read_all_books():
    if len(Books)<=0:
        create_books_no_api()
    return Books

@app.post("/createbook")
async def create_book(book:Book):
    Books.append(book)
    return book

#How to add books to book list when BOOKS list is empty

def create_books_no_api():
    book1 = Book(id="7adfeeec-6a3f-437a-8d19-2743c4edb9f4",
    title = "Computers",
    author = "Charless",
    description= "Description1",
    rating= 90)
    book2 = Book(id="7adfeeec-6a3f-437a-8d19-2743c4edb9f5",
    title = "Maths",
    author = "Venkat",
    description= "Description1",
    rating= 99)
    book3 = Book(id="7adfeeec-6a3f-437a-8d19-2743c4edb9f6",
    title = "Python",
    author = "GVANR",
    description= "Description1",
    rating= 100)

    Books.append(book1)
    Books.append(book2)
    Books.append(book3)

# Get books by passing UUID
@app.get("/books/book_id")
async  def read_book_by_id(book_id:UUID):
    for x in Books:
        if x.id == book_id:
            return x
    return {"Message":"UUID not matched"}

# update book by passing id
@app.put("/{book_id}")
async  def update_book(book_id:UUID,book:Book):
    count =0
    for x in Books:
        count += 1
        if x.id == book_id:
            Books[count-1] = book
        return Books[count-1]

# Delete book
@app.delete("/{book_id}")
async def delete_book(book_id:UUID):
    counter =0
    for x in Books:
        counter +=1
        if x.id == book_id:
            del Books[counter -1]
            return f'Deleted book id is {x.id}'


import os
import subprocess
import pytest

from psyclone.tests.lfric_build import LFRicBuild
from psyclone.tests.utilities import Compile, CompileError

@pytest.fixture(name="enable_compilation")
def protect_infrastructure_path_fixture(monkeypatch):
    '''A pytest fixture that mocks testing with compilation enabled while
    ensuring that any pre-existing LFRicBuild._compilation_path is
    preserved.'''
    # Pretend that the infrastructure has already been built so that the
    # LFRicBuild constructor doesn't attempt to trigger it.
    monkeypatch.setattr(LFRicBuild, "_infrastructure_built", True)
    # Pretend that compilation testing is enabled.
    monkeypatch.setattr(Compile, "TEST_COMPILE", True)
    # If compilation testing is enabled then the infrastructure lib will
    # already have been built so we use monkeypatch to ensure its
    # location is automatically restored at the end of this test.
    monkeypatch.setattr(LFRicBuild, "_compilation_path", "/no/path")

def test_lf_build_get_infrastructure_flags(monkeypatch, tmpdir):
    '''
    Test the get_infrastructure_flags method.

    '''
    # Pretend that compilation testing is disabled.
    monkeypatch.setattr(Compile, "TEST_COMPILE", False)
    builder = LFRicBuild(tmpdir)
    flags = builder.get_infrastructure_flags()
    dir_list = []
    for idx, flag in enumerate(flags):
        if idx % 2 == 0:
            assert flag == '-I'
        else:
            # Just keep a list of the base directories
            dir_list.append(os.path.split(flag)[1])
    assert 'configuration' in dir_list
    assert 'function_space' in dir_list
    assert 'field' in dir_list

@pytest.mark.usefixtures("enable_compilation")
def test_lfric_build_compiler_flags(tmpdir, monkeypatch):
    '''
    Check that the compiler settings supplied to pytest are passed through
    when building the (stub) LFRic infrastructure.

    '''
    def fake_popen(arg_list, stdout=None, stderr=None):
        '''Mock implementation of Popen that just raises an Exception.'''
        raise OSError(f"arg_list = {arg_list}")

    # Monkeypatch subprocess.Popen so that it just raises an exception.
    monkeypatch.setattr(subprocess, "Popen", fake_popen)
    builder = LFRicBuild(tmpdir)
    # Set-up a custom compiler and flags.
    builder._f90 = "my_compiler"
    builder._f90flags = "-my -special -flags"
    # Finally, check that these get passed through to the call to Popen when
    # the infrastructure is built.
    with pytest.raises(CompileError) as err:
        builder._build_infrastructure()
    assert ("['make', 'F90=my_compiler', 'F90FLAGS=-my -special -flags', '-f'"
            in str(err.value))

@pytest.mark.usefixtures("enable_compilation")
def test_lfric_build_infrastructure(tmpdir, monkeypatch):
    '''
    Test the _build_infrastructure method when compilation appears to proceed
    (i.e. the Popen.subprocess() command completes without raising an
    exception). Test with a return status of both 0 (success) and 1 (fail).

    '''
    class Build():
        '''Mock object for use when monkeypatching Popen.'''
        RETURN_CODE = 1

        def __init__(self):
            self.returncode = Build.RETURN_CODE

        def __enter__(self):
            return self

        def __exit__(self, _1, _2, _3):
            return

        def communicate(self):
            '''
            :returns: fake stdout output.
            :rtype: Tuple[bytes, NoneType]
            '''
            return (bytes("fake_out", "utf-8"), None)

    def fake_popen(arg_list, stdout=None, stderr=None):
        '''Mock implementation of Popen that just returns a Build instance.'''
        # pylint: disable=unused-argument
        return Build()

    # Monkeypatch subprocess.Popen so that compilation appears to run but
    # returns a status of 1.
    monkeypatch.setattr(subprocess, "Popen", fake_popen)
    builder = LFRicBuild(tmpdir)
    with pytest.raises(CompileError) as err:
        builder._build_infrastructure()
    assert "Compile error: fake_out" in str(err.value)
    # Repeat but alter the Build class to mock a successful build (return
    # status of 0).
    monkeypatch.setattr(Build, "RETURN_CODE", 0)
    monkeypatch.setattr(LFRicBuild, "_infrastructure_built", False)
    builder._build_infrastructure()
    # Check that the '_infrastructure_built' flag is set after a successful
    # build.
    assert LFRicBuild._infrastructure_built is True


import os

from model import *
from data import test_generator
from utils import create_directory, get_subfiles
from show_annotated_images import save_images_and_masks, save_result

from dotenv import load_dotenv
load_dotenv()

# TEST
def get_predictions():
    create_directory(os.getenv('PREDICTION_IMAGES_PATH'))

    test_filenames = get_subfiles(os.getenv('PNG_IMAGES_PATH'))

    weight_file_path = os.getenv('MODELPATH')

    model = unet(pretrained_weights=weight_file_path)

    testgen = test_generator(os.getenv('PNG_IMAGES_PATH'))

    test_batch_size = len(test_filenames)

    print('\nStarting testing ...\n')
    print('Using model - {}\n'.format(weight_file_path))
    results = model.predict_generator(testgen, test_batch_size, verbose=1)
    print('DONE !')

    # save predictions - images and masks
    print('\nSaving test results - masks')
    save_result(os.getenv('PREDICTION_IMAGES_PATH'), results, test_filenames, flag_multi_class=False, num_class=2)
    print('DONE !')

    if os.getenv('SAVE_COMBINED') == 'TRUE':
        create_directory(os.getenv('COMBINED_IMAGES_PATH'))
        imagesdir = os.getenv('PNG_IMAGES_PATH')
        masksdir = os.getenv('PREDICTION_IMAGES_PATH')
        suffix = '_predict'

        print('\nSaving test results - images and masks combined')
        save_images_and_masks(imagesdir, masksdir, suffix, save=True)
        print('DONE !')


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

# Test spherical earth model functions and methods.

__all__ = ('Tests',)
__version__ = '17.08.10'

from testLatLon import Tests as _TestsLL
from testVectorial import Tests as _TestsV

from pygeodesy import F_D, F_DMS, lonDMS

class Tests(_TestsLL, _TestsV):

    def testSpherical(self, module):

        self.subtitle(module, 'Spherical')

        LatLon = module.LatLon

        p = LatLon(51.8853, 0.2545)
        self.test('isSpherical', p.isSpherical, True)
        self.test('isEllipsoidal', p.isEllipsoidal, False)

        q = LatLon(49.0034, 2.5735)
        self.test('isSpherical', q.isSpherical, True)
        self.test('isEllipsoidal', q.isEllipsoidal, False)

        i = p.intersection(108.55, q, 32.44)
        self.test('intersection', i.toStr(F_D),  '50.907608°N, 004.508575°E')  # 50.9076°N, 004.5086°E  # Trig
        self.test('intersection', i.toStr(F_DMS), '50°54′27.39″N, 004°30′30.87″E')
        self.test('intersection', isinstance(i, LatLon), True)

        REO = LatLon(42.600, -117.866)
        BKE = LatLon(44.840, -117.806)
        i = REO.intersection(51, BKE, 137)
        self.test('intersection', i.toStr(F_D), '43.5719°N, 116.188757°W')  # 43.572°N, 116.189°W
        self.test('intersection', i.toStr(F_DMS), '43°34′18.84″N, 116°11′19.53″W')
        self.test('intersection', isinstance(i, LatLon), True)

        p = LatLon(0, 0)
        self.test('maxLat0',  p.maxLat( 0), '90.0')
        self.test('maxLat1',  p.maxLat( 1), '89.0')
        self.test('maxLat90', p.maxLat(90),  '0.0')

        if hasattr(LatLon, 'crossingParallels'):
            ps = p.crossingParallels(LatLon(60, 30), 30)
            t = ', '.join(map(lonDMS, ps))
            self.test('crossingParallels', t, '009°35′38.65″E, 170°24′21.35″E')

        if hasattr(LatLon, 'isEnclosedBy'):
            p = LatLon(45.1, 1.1)

            b = LatLon(45, 1), LatLon(45, 2), LatLon(46, 2), LatLon(46, 1)
            for _ in self.testiter():
                self.test('isEnclosedBy', p.isEnclosedBy(b), True)

            b = LatLon(45, 1), LatLon(45, 3), LatLon(46, 2), LatLon(47, 3), LatLon(47, 1)
            for _ in self.testiter():
                try:
                    self.test('isEnclosedBy', p.isEnclosedBy(b), True)  # Nvector
                except ValueError as x:
                    t = ' '.join(str(x).split()[:3] + ['...)'])
                    self.test('isEnclosedBy', t, 'non-convex: (LatLon(45°00′00.0″N, 001°00′00.0″E), ...)')  # Trig

        p = LatLon(51.127, 1.338)
        q = LatLon(50.964, 1.853)
        b = p.rhumbBearingTo(q)
        self.test('rhumbBearingTo', b, 116.722, fmt='%.3f')  # 116.7

        d = p.rhumbDestination(40300, 116.7)
        self.test('rhumbDestination', d, '50.964155°N, 001.853°E')  # 50.9642°N, 001.8530°E
        self.test('rhumbDestination', isinstance(d, LatLon), True)

        d = p.rhumbDistanceTo(q)
        self.test('rhumbDistanceTo', d, 40307.8, fmt='%.1f')  # XXX 40310 ?

        m = p.rhumbMidpointTo(q)
        self.test('rhumbMidpointo', m, '51.0455°N, 001.595727°E')
        self.test('rhumbMidpointo', isinstance(m, LatLon), True)

        b = LatLon(45, 1), LatLon(45, 2), LatLon(46, 2), LatLon(46, 1)
        self.test('areaOf', module.areaOf(b), '8.6660587507e+09', fmt='%.10e')  # 8666058750.718977

        c = LatLon(0, 0), LatLon(1, 0), LatLon(0, 1)
        self.test('areaOf', module.areaOf(c), '6.18e+09', fmt='%.2e')

        if hasattr(module, 'isPoleEnclosedBy'):
            b = LatLon(85, 90), LatLon(85, 0), LatLon(85, -90), LatLon(85, -180)
            for _ in self.testiter():
                self.test('isPoleEnclosedBy', module.isPoleEnclosedBy(b), 'True')
            b = LatLon(85, 90), LatLon(85, 0), LatLon(85, -180)
            for _ in self.testiter():
                self.test('isPoleEnclosedBy', module.isPoleEnclosedBy(b), 'True', known=True)

if __name__ == '__main__':

    from pygeodesy import sphericalNvector as N, \
                          sphericalTrigonometry as T

    t = Tests(__file__, __version__)

    t.testLatLon(N, Sph=True)
    t.testSpherical(N)
    t.testVectorial(N)

    t.testLatLon(T, Sph=True)
    t.testSpherical(T)

    t.results()
    t.exit()


import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="darcyai",
    author="darcy.ai",
    author_email="info@darcy.ai",
    description="DarcyAI Library",
    keywords="darcy, darcyai",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/darcyai/darcyai",
    project_urls={
        "Documentation": "https://darcyai.github.io/darcyai/",
        "Bug Reports":
        "https://github.com/darcyai/darcyai/issues",
        "Source Code": "https://github.com/darcyai/darcyai"
    },
    include_package_data=True,
    package_data={
        "darcyai": [
            "src/darcyai/swagger/*",
            "src/darcyai/perceptor/coral/models/*",
            "src/darcyai/perceptor/cpu/models/*",
            "src/darcyai/perceptor/posenet_lib/*",
            "src/darcyai/input/bars.png",
        ]
    },
    exclude_package_data={
        "darcyai": [
            "src/examples/*"
        ]
    },
    package_dir={"": "src"},
    packages=setuptools.find_packages(where="src"),
    classifiers=[
        "Development Status :: 2 - Pre-Alpha",
        "Intended Audience :: Developers"
    ],
    python_requires=">=3.6.9",
    install_requires=[
        "pillow==9.0.1",
        "imutils==0.5.4",
        "pytest==6.2.5",
        "flask==2.0.2",
        "requests==2.26.0",
        "logging-json==0.2.1",
        "waitress==2.1.2",
    ]
)


import numpy as np
import json
import pandas as pd

def create_json(path, save_json_path):

    bad_files = [
    "test_132.jpg",
    "test_1346.jpg",
    "test_184.jpg",
    "test_1929.jpg",
    "test_2028.jpg",
    "test_22.jpg",
    "test_232.jpg",
    "test_2321.jpg",
    "test_2613.jpg",
    "test_2643.jpg",
    "test_274.jpg",
    "test_2878.jpg",
    "test_521.jpg",
    "test_853.jpg",
    "test_910.jpg",
    "test_923.jpg",
    "train_1239.jpg",
    "train_2376.jpg",
    "train_2903.jpg",
    "train_2986.jpg",
    "train_305.jpg",
    "train_3240.jpg",
    "train_340.jpg",
    "train_3556.jpg",
    "train_3560.jpg",
    "train_38.jpg",
    "train_3832.jpg",
    "train_4222.jpg",
    "train_5007.jpg",
    "train_5137.jpg",
    "train_5143.jpg",
    "train_5762.jpg",
    "train_5822.jpg",
    "train_6052.jpg",
    "train_6090.jpg",
    "train_6138.jpg",
    "train_6409.jpg",
    "train_6722.jpg",
    "train_6788.jpg",
    "train_737.jpg",
    "train_7576.jpg",
    "train_7622.jpg",
    "train_775.jpg",
    "train_7883.jpg",
    "train_789.jpg",
    "train_8020.jpg",
    "train_8146.jpg",
    "train_882.jpg",
    "train_903.jpg",
    "train_924.jpg",
    "val_147.jpg",
    "val_286.jpg",
    "val_296.jpg",
    "val_386.jpg"
    ]

    data = pd.read_csv(path, usecols=[0,1,2,3,4,5,6,7], names=['filename','xmin','ymin','xmax','ymax','class','width','height'], header=None)

    for n in bad_files:
        data = data.drop(data[data['filename'] == n].index)

    images = []
    categories = []
    annotations = []

    category = {}
    category["supercategory"] = 'none'
    category["id"] = 1
    category["name"] = 'None'
    categories.append(category)

    data['fileid'] = data['filename'].astype('category').cat.codes
    data['categoryid']= pd.Categorical(data['class'],ordered= True).codes
    data['categoryid'] = data['categoryid']+1
    data['annid'] = data.index

    def image(row):
        image = {}
        image["height"] = row.height
        image["width"] = row.width
        image["id"] = row.fileid
        image["file_name"] = row.filename
        return image

    def category(row):
        category = {}
        category["supercategory"] = 'None'
        category["id"] = row.categoryid
        category["name"] = row[2]
        return category

    def annotation(row):
        annotation = {}
        area = 100
        #area = (row.xmax -row.xmin)*(row.ymax - row.ymin)
        annotation["segmentation"] = []
        annotation["iscrowd"] = 0
        annotation["area"] = area
        annotation["image_id"] = row.fileid

        annotation["bbox"] = [row.xmin, row.ymin, row.xmax -row.xmin,row.ymax-row.ymin ]

        annotation["category_id"] = row.categoryid
        annotation["id"] = row.annid
        return annotation

    for row in data.itertuples():
        annotations.append(annotation(row))

    imagedf = data.drop_duplicates(subset=['fileid']).sort_values(by='fileid')
    for row in imagedf.itertuples():
        images.append(image(row))

    catdf = data.drop_duplicates(subset=['categoryid']).sort_values(by='categoryid')
    for row in catdf.itertuples():
        categories.append(category(row))

    data_coco = {}
    data_coco["images"] = images
    data_coco["categories"] = categories
    data_coco["annotations"] = annotations
    json.dump(data_coco, open(save_json_path, "w"), indent=4)


from unicodedata import normalize

from tool import valid_file, replace_words, invert_dict
from tool.argument import Argument

_argument = Argument("file", type=valid_file, meta="<file>")

def create_subparser(subparsers):
    command = "normalize"
    parser = subparsers.add_parser(command, help="Normalize string in xml file.")
    _argument.add_argument(parser)
    return command, _normal

def _normal(args):
    if args is None:
        file_path = _argument.ask_input()
    else:
        file_path = args.file
    with open(file_path, mode="r+", encoding="utf-8") as file:
        content = normal(file.read())
        file.seek(0)
        file.write(content)
        file.truncate()

def normal(source: str) -> str:
    content = replace_words(source, _before)
    content = normalize("NFKC", content)
    return replace_words(content, _after)

_before = {
    "～": "$wave%",
    "＆": "&"
}

_after = {
    **invert_dict(_before),
    "...": "…",
    "．．．": "…",
    "・・・": "…"
}


"""Lauches the acorn ui server and opens the browser to the correct page.
"""
import webbrowser
import os
from multiprocessing import Process
import time

def open_window():
    """Opens a new tab for the acorn notebook to be displayed in.
    """
    time.sleep(1)
    webbrowser.open('http://127.0.0.1:8000/',new=2,autoraise=True)

def launch_server():
    """Launches the django server at 127.0.0.1:8000
    """
    print(os.path.dirname(os.path.abspath(__file__)))
    cur_dir = os.getcwd()
    path = os.path.dirname(os.path.abspath(__file__))
    run = True
    os.chdir(path)
    os.system('python manage.py runserver --nostatic')
    os.chdir(cur_dir)

if __name__ == '__main__':
    Process(target=launch_server).start()
    Process(target=open_window).start()


import pandas
import matplotlib.pyplot as plt;
import seaborn as sns;

# load dataset
columns = ['day', 'hour', 'op', 'ftype', 'parent', 'fileSize',
           'p1', 'p2', 'p3', 'p4', 'filename',
           's1', 's2', 's3', 's4']
dataframe = pandas.read_csv("/home/anuradha/PycharmProjects/data/fyp/final/test-after-filtered.csv", header=None, names=columns)

print (dataframe.info())
print (dataframe.describe())
# print (dataframe.sample(5))
# print (dataframe.drop_duplicates().fileSize.value_counts())
# print (dataframe.drop_duplicates().filename.value_counts())
print (dataframe.drop_duplicates().s1.value_counts())
# print (dataframe.drop_duplicates().parent.value_counts())

# dataframe['op'].value_counts().plot(kind='bar', title='Training examples by operation types');
# dataframe['ftype'].value_counts().plot(kind='bar', title='Training examples by file types');
# dataframe['parent'].value_counts().plot(kind='bar', title='Training examples by parent folder');
# dataframe['hour'].value_counts().plot(kind='bar', title='Training examples by hour');
# dataframe['fileSize'].value_counts().plot(kind='bar', title='Training examples by file size');

# plt.show()

corr = dataframe.corr()
sns.heatmap(corr,
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values)
sns.plt.title("correlations among variables")
sns.plt.show()

#day of week, hour, operation type, file size have very weak correlations

#parent folder, file type has moderate correlation


import logging
import sys,os
import pandas as pd
from income.exception import CustomException
from income.utils import load_object
from income.predictor import ModelResolver
import sklearn

PREDICTION_DIR = "prediction"

class PredictPipeline:
    def __init__(self):
        pass

    def predict(self, features):
        try:
            os.makedirs(PREDICTION_DIR, exist_ok=True)
            logging.info(f"Creating model resolver object")
            model_resolver = ModelResolver(model_registry="saved_models")
            logging.info(f"Reading file :{features}")

            logging.info(f"Loading transformer to transform dataset")
            transformer = load_object(file_path=model_resolver.get_latest_transformer_path())
            model = load_object(file_path=model_resolver.get_latest_model_path())

            input_arr = transformer.transform(features)

            prediction = model.predict(input_arr)

            return prediction

        except Exception as e:
            raise CustomException(e, sys)

class CustomData:
    def __init__(self,
                 age: float,
                 workclass: str,
                 education_num:int,
                 marital_status: str,
                 occupation: str,
                 relationship: str,
                 race: str,
                 sex:str,
                 capital_gain:int,
                 capital_loss:int,
                 hours_per_week:int,
                 country:str):

        self.age = age

        self.workclass = workclass

        self.education_num = education_num

        self.marital_status = marital_status

        self.occupation = occupation

        self.relationship = relationship

        self.race = race

        self.sex = sex

        self.capital_gain = capital_gain

        self.capital_loss = capital_loss

        self.hours_per_week = hours_per_week

        self.country = country

    def get_data_as_data_frame(self):
        try:
            custom_data_input_dict = {
                "age": [self.age],
                "workclass": [self.workclass],
                "education-num": [self.education_num],
                "marital-status": [self.marital_status],
                "occupation": [self.occupation],
                "relationship": [self.relationship],
                "race": [self.race],
                "sex": [self.sex],
                "capital-gain": [self.capital_gain],
                "capital-loss": [self.capital_loss],
                "hours-per-week": [self.hours_per_week],
                "country": [self.country],

            }

            df = pd.DataFrame(custom_data_input_dict)
            # Remove leading and trailing spaces from all columns
            df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
            return df

        except Exception as e:
            raise CustomException(e, sys)


# https://classroom.udacity.com/nanodegrees/nd256/parts/f74fc064-524b-4ee8-8fb1-570b3c31a993/modules/9bbb9a6d-d848-4153-a2fc-25065ee8d42d/lessons/2bdc11fe-1acf-4363-8cac-d2a9c1d714a1/concepts/d5d4db5f-e7fc-4005-96b1-cb8b1c770fdc
# http://www.onlineconversion.com/leapyear.htm

def is_leap_year(year):
    leap = False
    if year % 4 == 0:
        if year % 100 == 0:
            leap = ((year % 400) == 0)
        else:
            leap = True
    return leap

def day_in_month(year, month):
    assert 1 <= month <= 12
    days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    if month == 2 and is_leap_year(year):
        return days[month - 1] + 1
    return days[month - 1]

def next_day(year, month, day):
    assert 1 <= month <= 12
    assert 1 <= day <= 31
    if day < day_in_month(year, month):
        return year, month, day + 1
    else:
        if month == 12:
            return year + 1, 1, 1
        else:
            return year, month + 1, 1

def is_date_before(year_before, month_before, day_before, year_after, month_after, day_after):
    if year_before > year_after:
        return False
    if year_before == year_after:
        if month_before > month_after:
            return False
        if month_before == month_after:
            return day_before < day_after
    return True

def days_between_dates(year1, month1, day1, year2, month2, day2):
    """Returns the number of days between year1/month1/day1
       and year2/month2/day2. Assumes inputs are valid dates
       in Gregorian calendar, and the first date is not after
       the second."""
    days = 0
    # date1 = (year1,month1,day1)
    assert not is_date_before(year2, month2, day2, year1, month1, day1)
    while is_date_before(year1, month1, day1, year2, month2, day2):
        year1, month1, day1 = next_day(year1, month1, day1)
        days += 1
    # YOUR CODE HERE!
    return days

def test_days_between_dates():
    test_cases = [((2012, 1, 1, 2012, 2, 28), 58),
                  ((2012, 1, 1, 2012, 3, 1), 60),
                  ((2011, 6, 30, 2012, 6, 30), 366),
                  ((2011, 1, 1, 2012, 8, 8), 585),
                  ((1900, 1, 1, 1999, 12, 31), 36523),
                  ((2013, 1, 1, 1999, 12, 31), "AssertionError")]

    for (args, answer) in test_cases:
        try:
            result = days_between_dates(*args)
            if result == answer and answer != "AssertionError":
                print("Test case passed!")
            else:
                print("**********************Test with input data:", args, "failed")

        except AssertionError:
            if answer == "AssertionError":
                print("Nice job! Test case {0} correctly raises AssertionError!\n".format(args))
            else:
                print("Check your work! Test case {0} should not raise AssertionError!\n".format(args))

test_days_between_dates()

# Nice job! Test case next_day(2012, 1, 1) is correct!
# Nice job! Test case next_day(2012, 4, 30) is correct!
# Nice job! Test case next_day(2012, 12, 1) is correct!
# Nice job! Test case next_day(1999, 12, 31) is correct!
# Nice job! Test case next_day(2012, 12, 31) is correct!
# Nice job! Test case days_between_dates(2012, 9, 30, 2012, 10, 30) is correct!
# Nice job! Test case days_between_dates(2012, 5, 15, 2012, 5, 17) is correct!
# Nice job! Test case days_between_dates(2012, 1, 1, 2013, 1, 1) is correct!

# Nice job! Test case next_day(2012, 1, 1) is correct!
# Nice job! Test case next_day(2012, 4, 30) is correct!
# Nice job! Test case next_day(2012, 12, 1) is correct!
# Nice job! Test case next_day(1999, 12, 31) is correct!
# Nice job! Test case next_day(2012, 12, 31) is correct!
# Nice job! Test case days_between_dates(2012, 9, 30, 2012, 10, 30) is correct!
# Nice job! Test case days_between_dates(2012, 2, 1, 2012, 3, 1) is correct!
# Nice job! Test case days_between_dates(2013, 1, 1, 1999, 12, 31) correctly raises AssertionError!
# Nice job! Test case days_between_dates(1991, 3, 1, 1991, 1, 3) correctly raises AssertionError!
# Nice job! Test case days_between_dates(2012, 1, 1, 2012, 2, 28) is correct!
# Nice job! Test case days_between_dates(2012, 1, 1, 2012, 3, 1) is correct!
# Nice job! Test case days_between_dates(2011, 6, 30, 2012, 6, 30) is correct!
# Nice job! Test case days_between_dates(2011, 1, 1, 2012, 8, 8) is correct!
# Nice job! Test case days_between_dates(1900, 1, 1, 1999, 12, 31) is correct!

# print days_between_dates(2013, 1, 24, 2013, 6, 29)
# print days_between_dates(1912, 12, 12, 2012, 12, 12)
# print days_between_dates(2013, 1, 1, 2012, 12, 20) # returns 0 - but it doesn't make sense so program defensevely

# print not is_date_before(2012, 9, 1, 2012, 9, 1)

# BEI-C_G_-AF-DHJK
# BEI
#

Udacity/1 - Introduction/Problem Solving/U-ProblemSolving-daysBetweenDates.py


import subprocess
from pathlib import Path

import pytest
from click.testing import CliRunner

from simplec import cli

def fixture_path(path):
    return str(Path(__file__).parent / path)

@pytest.mark.parametrize(
    "src,returncode",
    [
        ("fixtures/one.c", 1),
        ("fixtures/add.c", 8),
        ("fixtures/call.c", 45),
    ],
)
def test_compiler(tmpdir, src, returncode):
    runner = CliRunner()
    result = runner.invoke(cli.compiler, [fixture_path(src)])
    assert result.exit_code == 0

    asm_path = tmpdir / f"{Path(src).stem}.s"
    exe_path = tmpdir / Path(src).stem
    with open(asm_path, "w") as fp:
        fp.write(result.output)

    assert (
        subprocess.run(
            ["arm-linux-gnueabihf-gcc", str(asm_path), "-o", str(exe_path)]
        ).returncode
        == 0
    )
    result = subprocess.run(
        ["qemu-arm-static", "-L", "/usr/arm-linux-gnueabihf/", str(exe_path)],
        capture_output=True,
    )
    assert result.returncode == returncode


# 班上有 N 名学生。其中有些人是朋友，有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友，B 是 C 的朋友，那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈，是指所有朋友的集合。
#
# 给定一个 N * N 的矩阵 M，表示班级中学生之间的朋友关系。如果M[i][j] = 1，表示已知第 i 个和 j 个学生互为朋友关系，否则为不知道。你必须输出所有学生中的已知的朋友圈总数。
#
# 示例 1:
#
# 输入:
# [[1,1,0],
#  [1,1,0],
#  [0,0,1]]
# 输出: 2
# 说明：已知学生0和学生1互为朋友，他们在一个朋友圈。
# 第2个学生自己在一个朋友圈。所以返回2。
# 示例 2:
#
# 输入:
# [[1,1,0],
#  [1,1,1],
#  [0,1,1]]
# 输出: 1
# 说明：已知学生0和学生1互为朋友，学生1和学生2互为朋友，所以学生0和学生2也是朋友，所以他们三个在一个朋友圈，返回1。

# 他这个是矩阵，是行列，横竖看：
# [[1,1,0],代表0和自己要“连着”，和1连着。
#  [1,1,0],代表1和自己要“连着”，和0连着。
#  [0,0,1]],代表2和自己要“连着”

#最直觉的方式是DFS，和岛屿一样
#可以直接套岛屿，一样的数据结构
#与岛屿的不同之处在于，可以旋转，因为这个数组传递的是关系。可以旋转的是x，y不行。
# 也不对，不是岛屿问题，第一个点可以直接和第三个点相连，数组上看，没有相邻。所以本质上不是一个靠相邻就能解决的问题。
class Solution:
    def DFS(self,M,visited,i):
        visited[i] = 1
        for j in range(len(M[0])):
            if M[i][j] and not visited[j]:
                self.DFS(M,visited,j)

    def findCircleNum(self, M):
        """
        :type M: List[List[int]]
        :rtype: int
        """
        cnt = 0
        visited = [0  for i in range(len(M))]
        # print(visited)
        for i in range(len(M)):
            if M[i] and not visited[i]:
                # print('cnt++')
                cnt += 1
                self.DFS(M,visited,i)
                # print(visited)

        return cnt

nums = \
    [[1,1,0],
 [1,1,0],
 [0,0,1]]
nums = \
[[1,0,0,1],
 [0,1,1,0],
 [0,1,1,1],
 [1,0,1,1]]
sol = Solution()
print('ret:',sol.findCircleNum(nums))

# #并查集的本质就是重定向，本来都是独立的个体，通过无数个重定向，集中到一起。
# 但是这个操作有没有顺序影响？A指向B，B指向C，A还能指向C吗？
#
# 另外，这是数组实现，按理说可以dict或者set来实现。

class DisjointSet(object):
    def __init__(self,n):
        self.id = [i for i in range(n)]
        print(self.id)
    def find(self,p):
        return self.id[p]

    def union(self,p,q):
        pid = self.find(p)
        qid = self.find(q)
        if pid == qid:
            return;
        for i in range(len(self.id)):#这里，不止一个，以前A指向B，现在A和B的值都是pid，所以A和B都指向qid。
            if self.id[i] == pid:
                self.id[i] = qid
        print('id is ',self.id)

#上边这是基本结构，但是要做这道题，要用变形才会快，因为你不能费力合并半天，还要费时间去两两比较。给加一个counter，合并了就减少counter,其实仍然不够快
#这样，把原题拿来构建并查集，最后的count就是结果
class DisjointSet(object):
    def __init__(self,n):
        self.id = [i for i in range(n)]
        self.size = [1 for i in range(n)]
        self.count = n

    def find(self,p):
        while p != self.id[p]:
            self.id[p] = self.id[self.id[p]]#其实路径压缩根本不用管位置，路径压缩确实就是跳一步，但是因为“终点”是自己指向自己，你跳也不怕跳出问题
            p = self.id[p]
        return p

    def union(self,p,q):
        i = self.find(p)
        j = self.find(q)
        if i == j:
            return;
        if self.size[i] < self.size[j]:#保持平衡，i小，则i指向j
            self.id[i] = j
            self.size[j] += self.size[i]
        else:
            self.id[j] = i
            self.size[i] += self.size[j]
        self.count-=1

class Solution:
    def DFS(self,M,visited,i):
        visited[i] = 1
        for j in range(len(M[0])):
            if M[i][j] and not visited[j]:
                self.DFS(M,visited,j)

    def findCircleNum(self, M):
        d_set = DisjointSet(len(M))
        for i in range(len(M)):
            for j in range(i+1,len(M[0])):#小优化，这是双向关系，所以j不用遍历n，从i+1开始
                if M[i][j]:
                    d_set.union(i,j)

        return d_set.count

d = DisjointSet(7)
d.union(0,5)
print(d.id)
# print(d.find(0))
print(d.id)
print(d.size)
d.union(2,4)
print(d.id)
print(d.size)
d.union(0,4)
print(d.id)
print(d.size)

sol = Solution()
print('ret:',sol.findCircleNum(nums))


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

# In[1]:

import json

# In[30]:

# modelPath = '../output/testBigramModel.json'
modelPath = '../output/bigramModel.json'
UomodelPath = '../output/UObigramModel.json'
limit = 6
#https://stackoverflow.com/questions/35624064/sorting-dictionary-descending-in-python

# In[35]:

def findNext(query,collection):
    # Let's assume the query is of form "abc cde "
    mPath = modelPath
    if collection == "UofO catalog":
        mPath = UomodelPath
    with open(mPath,'r') as f:
        file = json.load(f)
        previousWord = query.split()[-1]
        d = file[previousWord]
        sortedProb = {k: v for k, v in sorted(d.items(), key=lambda item: item[1],reverse=True)}
        return [query + x for x in list(sortedProb)[0:limit]]

# In[19]:

#list(newf)[0:3]

# In[37]:

# print(findNext("U.S. would ")
# print(findNext("boo ")


import enum
import re
from dataclasses import dataclass
from typing import Dict, Optional, Tuple

from yarl import URL

from ._rewrite import rewrite_module
from ._url_utils import _check_uri, _check_uri_str

@rewrite_module
class TagOption(enum.Enum):
    ALLOW = enum.auto()
    DENY = enum.auto()
    DEFAULT = enum.auto()

@rewrite_module
@dataclass(frozen=True)
class RemoteImage:
    name: str
    tag: Optional[str] = None
    registry: Optional[str] = None
    cluster_name: Optional[str] = None
    org_name: Optional[str] = None
    project_name: Optional[str] = None

    @property
    def _is_in_neuro_registry(self) -> bool:
        return bool(self.registry and self.cluster_name and self.project_name)

    @classmethod
    def new_neuro_image(
        cls,
        name: str,
        registry: str,
        *,
        cluster_name: str,
        org_name: Optional[str],
        project_name: str,
        tag: Optional[str] = None,
    ) -> "RemoteImage":
        return RemoteImage(
            name=name,
            tag=tag,
            registry=registry,
            cluster_name=cluster_name,
            org_name=org_name,
            project_name=project_name,
        )

    @classmethod
    def new_external_image(
        cls, name: str, registry: Optional[str] = None, *, tag: Optional[str] = None
    ) -> "RemoteImage":
        return RemoteImage(name=name, tag=tag, registry=registry)

    def __post_init__(self) -> None:
        if self.registry:
            if self.project_name:
                if not self.cluster_name:
                    raise ValueError("required cluster name")
            else:
                if self.cluster_name:
                    raise ValueError("required project")
        else:
            if self.project_name or self.cluster_name:
                raise ValueError("required registry")

    def as_docker_url(self, with_scheme: bool = False) -> str:
        if self._is_in_neuro_registry:
            if self.org_name:
                prefix = f"{self.registry}/{self.org_name}/{self.project_name}/"
            else:
                prefix = f"{self.registry}/{self.project_name}/"
            prefix = "https://" + prefix if with_scheme else prefix
        else:
            prefix = ""
        suffix = f":{self.tag}" if self.tag else ""
        return f"{prefix}{self.name}{suffix}"

    def __str__(self) -> str:
        result = self.name
        if self.tag:
            result = f"{result}:{self.tag}"
        if self._is_in_neuro_registry:
            assert self.cluster_name is not None
            base = ""
            if self.org_name:
                base = f"/{self.org_name}"
            result = str(
                URL.build(
                    scheme="image",
                    host=self.cluster_name,
                    path=f"{base}/{self.project_name}/{result}",
                )
            )
        return result

    def __rich__(self) -> str:
        return str(self)

@rewrite_module
@dataclass(frozen=True)
class LocalImage:
    name: str
    tag: Optional[str] = None

    def __str__(self) -> str:
        post = f":{self.tag}" if self.tag else ""
        return self.name + post

    def __rich__(self) -> str:
        return str(self)

class _ImageNameParser:
    def __init__(
        self,
        default_cluster: str,
        default_org: Optional[str],
        default_project: str,
        registry_urls: Dict[str, URL],
    ):
        self._default_cluster = default_cluster
        self._default_org_name = default_org
        self._default_project_name = default_project
        self._registries = {}
        for cluster_name, registry_url in registry_urls.items():
            if not registry_url.host:
                raise ValueError(
                    f"Empty hostname in registry URL '{registry_url}': "
                    f"please consider updating configuration"
                )
            self._registries[cluster_name] = _get_url_authority(registry_url)

    def parse_as_local_image(self, image: str) -> LocalImage:
        try:
            self._validate_image_name(image)
            return self._parse_as_local_image(image)
        except ValueError as e:
            raise ValueError(f"Invalid local image '{image}': {e}") from e

    def parse_as_neuro_image(
        self, image: str, *, tag_option: TagOption = TagOption.DEFAULT
    ) -> RemoteImage:
        try:
            self._validate_image_name(image)
            tag: Optional[str]
            if tag_option == TagOption.DEFAULT:
                tag = "latest"
            else:
                if tag_option == TagOption.DENY and self.has_tag(image):
                    raise ValueError("tag is not allowed")
                tag = None
            return self._parse_as_neuro_image(image, default_tag=tag)
        except ValueError as e:
            raise ValueError(f"Invalid remote image '{image}': {e}") from e

    def parse_remote(
        self, value: str, *, tag_option: TagOption = TagOption.DEFAULT
    ) -> RemoteImage:
        if value.startswith("image:") or self._find_by_registry(value):
            return self.parse_as_neuro_image(value, tag_option=tag_option)

        img = self.parse_as_local_image(value)
        name = img.name
        registry = None
        if ":" in name:
            msg = "here name must contain slash(es). checked by _split_image_name()"
            assert "/" in name, msg
            registry, name = name.split("/", 1)

        return RemoteImage.new_external_image(name=name, tag=img.tag, registry=registry)

    def convert_to_neuro_image(self, image: LocalImage) -> RemoteImage:
        cluster_name = self._default_cluster
        org_name = self._default_org_name
        project_name = self._default_project_name
        name = image.name
        res = self._find_by_registry(name)
        if res:
            cluster_name, path = res
            if path:
                project_name, _, name = path.partition("/")
                if not name:
                    project_name = self._default_project_name
                    name = path

        return RemoteImage.new_neuro_image(
            name=name,
            tag=image.tag,
            cluster_name=cluster_name,
            org_name=org_name,
            project_name=project_name,
            registry=self._registries[cluster_name],
        )

    def convert_to_local_image(self, image: RemoteImage) -> LocalImage:
        return LocalImage(name=image.name, tag=image.tag)

    def has_tag(self, image: str) -> bool:
        prefix = "image:"
        if image.startswith(prefix):
            url = URL(image)
            image = url.path
            if image.startswith("/"):
                image = image[1:]
        _, tag = self._split_image_name(image)
        return bool(tag)

    def _validate_image_name(self, image: str) -> None:
        if not image:
            raise ValueError("empty image name")
        if image.startswith("-"):
            raise ValueError("image cannot start with dash")
        if image == "image:latest":
            raise ValueError(
                "ambiguous value: valid as both local and remote image name"
            )

    def _parse_as_local_image(self, image: str) -> LocalImage:
        if image.startswith("image:"):
            raise ValueError("scheme 'image://' is not allowed for local images")
        name, tag = self._split_image_name(image, "latest")
        return LocalImage(name=name, tag=tag)

    def _parse_as_neuro_image(
        self, image: str, default_tag: Optional[str]
    ) -> RemoteImage:
        if image.startswith("image:"):
            # Check string representation to detect also trailing "?" and "#".
            _check_uri_str(image, "image")
            url = URL(image)
            if not url.scheme and url.path.startswith("image:"):
                prefix = ""
                if self._default_org_name:
                    prefix = f"/{self._default_org_name}"
                url = URL.build(
                    scheme="image",
                    host=self._default_cluster,
                    path=(
                        f"{prefix}/{self._default_project_name}"
                        f"/{url.path[len('image:') :]}"
                    ),
                )
        else:
            res = self._find_by_registry(image)
            if not res:
                raise ValueError("scheme 'image:' is required for remote images")
            cluster_name, path = res
            url = URL(f"image://{cluster_name}/{path}")

        if not url.path or url.path == "/":
            raise ValueError("no image name specified")
        _check_uri(url)

        name, tag = self._split_image_name(url.path.lstrip("/"), default_tag)
        if url.host is None:
            # This is short url, either image:name or image:/project/name
            cluster_name = self._default_cluster
            org_name = self._default_org_name
        else:
            cluster_name = url.host
            org_name = None
        if url.path.startswith("/"):
            project_name, _, name = name.partition("/")
            if project_name == self._default_org_name and url.host:
                # Long form with explicit org name (image://cluster/org/project/image)
                org_name = project_name
                project_name, _, name = name.partition("/")
            if not name:
                raise ValueError("no image name specified")
        else:
            project_name = self._default_project_name
        if cluster_name not in self._registries:
            tip = "Please logout and login again."
            raise RuntimeError(
                f"Cluster {cluster_name} doesn't exist in "
                f"a list of available clusters "
                f"{list(self._registries)}. {tip}"
            )
        return RemoteImage.new_neuro_image(
            name=name,
            tag=tag,
            registry=self._registries[cluster_name],
            cluster_name=cluster_name,
            org_name=org_name,
            project_name=project_name,
        )

    def _find_by_registry(self, image: str) -> Optional[Tuple[str, str]]:
        for cluster_name, registry in self._registries.items():
            if image.startswith(f"{registry}/"):
                path = image[len(registry) :].lstrip("/")
                return cluster_name, path
        return None

    def _split_image_name(
        self, image: str, default_tag: Optional[str] = None
    ) -> Tuple[str, Optional[str]]:
        if image.endswith(":") or image.startswith(":"):
            # case `ubuntu:`, `:latest`
            raise ValueError("empty name or empty tag")
        colon_count = image.count(":")
        if colon_count == 0:
            # case `ubuntu`
            name, tag = image, default_tag
        elif colon_count == 1:
            # case `ubuntu:latest`
            name, tag = image.split(":")
            if "/" in tag:
                # case `localhost:5000/ubuntu`
                name, tag = image, default_tag
        elif colon_count == 2:
            # case `localhost:9000/owner/ubuntu:latest`
            if "/" not in image:
                # case `localhost:9000:latest`
                raise ValueError("too many tags")
            name, tag = image.rsplit(":", 1)
        else:
            raise ValueError("too many tags")
        if "/" in name:
            _, name_no_repo = name.split("/", 1)
        else:
            name_no_repo = name
        if not name_no_repo:
            raise ValueError("no image name specified")
        if not re.fullmatch(
            r"(?:[a-z0-9]+(?:[._-][a-z0-9]+)*/)*[a-z0-9]+(?:[._-][a-z0-9]+)*",
            name_no_repo,
        ):
            raise ValueError(
                "invalid image name. Docker specifies it to be the following:\n"
                "Name components may contain lowercase letters, digits and "
                "separators. A separator is defined as a period, one or two "
                "underscores, or one or more dashes. A name component may not "
                "start or end with a separator."
            )
        if tag:
            if len(tag) > 128:
                raise ValueError("tag is to long")
            if not re.fullmatch(r"[a-zA-Z0-9_]+[a-zA-Z0-9_.-]*", tag):
                raise ValueError(
                    "invalid tag. Docker specifies it to be the following:\n"
                    "A tag name must be valid ASCII and may contain lowercase "
                    "and uppercase letters, digits, underscores, periods and "
                    "dashes. A tag name may not start with a period or a dash "
                    "and may contain a maximum of 128 characters."
                )
        return name, tag

@rewrite_module
@dataclass(frozen=True)
class Tag:
    name: str
    size: Optional[int] = None

def _get_url_authority(url: URL) -> str:
    assert url.host is not None
    port = url.explicit_port  # type: ignore
    suffix = f":{port}" if port is not None else ""
    return url.host + suffix


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = Axes3D(fig)

x = np.arange(0, 200)
y = np.arange(0, 100)
x, y = np.meshgrid(x, y)
z = np.random.randint(0, 200, size=(100, 200))%3
print(z.shape)

# ax.scatter(x, y, z, c='r', marker='.', s=50, label='')
ax.plot_surface(x, y, z,label='')
plt.show()


#! /usr/bin/env python3
# -*- coding: utf-8 -*-

__author__ = "Cchen Zhang"

import time, threading, multiprocessing

balance = 0
lock = threading.Lock()
threadpool = []

def loop():
    print('thread %s is running' % threading.current_thread().name)
    n = 0
    while n < 5:
        n = n + 1
        print('thread %s >>> %s' % (threading.current_thread().name, n))
        time.sleep(1)
    print('thread %s end' % threading.current_thread().name)

def dead_loop():
    n = 0
    while True:
        n = n ^ 1

def change_value(n):
    global balance
    balance = balance + n
    balance = balance - n

def run_thread(n):
    for i in range(1000):
        lock.acquire()
        try:
            change_value(n)
        finally:
            lock.release()

def test_multiThread():
    print('thread %s is running' % threading.current_thread().name)
    t1 = threading.Thread(target=run_thread, name='t1', args=(5,))
    t2 = threading.Thread(target=run_thread, name='t2', args=(-8,))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    print(balance)
    print('thread %s is end' % threading.current_thread().name)

def test_thread_cpu():
    n = multiprocessing.cpu_count()
    print(n)
    for i in range(n):
        t = threading.Thread(target=dead_loop())
        print('start new thread')
        threadpool.append(t)
        t.start()

def main():
    # test_multiThread()
    test_thread_cpu()
    time.sleep(10)
    for t in threadpool:
        t.terminate()

if __name__ == '__main__':
    main()


import sqlite3

con = sqlite3.connect(r"e:\classroom\python\hr.db")
cur = con.cursor()

cur.execute("select * from jobs order by minsal desc")
jobs = cur.fetchall()
for id, title, minsal in jobs:
    print(f"{id:3d} {title:30s} {minsal:6d}")

con.close()


#! /usr/bin/env python

import sys
import os
import gzip
from glob import glob
import re

def get_tax_lookup(summary_file):
    # Dictionary to lookup taxonomy ID
    _tax_lookup = {}
    lines = (l.strip('\n').split('\t') for l in open(summary_file,'rU'))
    header = lines.next()
    for l in lines:
        asm_id = l[header.index('ftp_path')].split('/')[-1]
        ## asm_id = '%s_%s' % (l[header.index('# assembly_accession')], '_'.join(l[header.index('asm_name')].split()))
        _tax_lookup[asm_id] = l[header.index('taxid')]
    return _tax_lookup

def get_gi_lookup(genbank_file):
    _gi_lookup = {}
    gblines = (l.strip() for l in gzip.open(genbank_file, 'rb') if l.startswith('VERSION'))
    for gbline in gblines:
        x,acc,gi = gbline.split()
        _gi_lookup[acc] = gi.split(':')[1]
    return _gi_lookup

def get_taxid_from_genbank(genbank_file):
    ''' Just get the first taxid in genbank file '''
    taxidRE = re.compile('^/db_xref="taxon:(\d+)"')
    gblines = (l.strip() for l in gzip.open(genbank_file, 'rb'))
    for l in gblines:
        m = taxidRE.search(l)
        if m:
            return m.group(1)
    return None

#
# for rec in split_gb_records(genbank_file):
#         cur_taxid = None
#         cur_acc   = None
#         for l in rec:
#             if l.startswith('VERSION'):
#                 x,acc,gi = l.split()
#                 cur_acc = acc
#             else:
#                 m = taxidRE.search(l)
#                 if m:
#                     cur_taxid = m.group(1)
#                     break
#         print '%s\t%s' % (cur_acc, cur_taxid)
#
#

def main(args):
    if args.sumfile == '':
        tax_lookup = get_tax_lookup('%s.assembly_summary.txt' % args.tlevel)
    else:
        tax_lookup = get_tax_lookup(args.sumfile)

    file_index = 1
    outh = open('%s.%02d.fna' % (args.prefix, file_index), 'w')
    file_char = 0

    fasta_files = glob('%s/%s/*/latest_assembly_versions/*/*_genomic.fna.gz' % (args.db,args.tlevel))
    for ff in fasta_files:
        # Assembly ID
        asm_id = ff.split('/')[-1].split('_genomic')[0]
        ### Find the taxonomy ID
        if asm_id in tax_lookup:
            # Assembly ID has an exact match in the assembly summary file
            ti = tax_lookup[asm_id]
        else:
            # Check if Assembly ID prefix matches to assembly summary file
            asm_prefix = asm_id.split('.')[0]
            # print >>sys.stderr, 'Using alternate lookup for %s' % asm_prefix
            prematch = [k for k in tax_lookup.keys() if k.startswith(asm_prefix)]
            if len(prematch)==1:
                ti = tax_lookup[prematch[0]]
            else:
                # Check in genbank file for taxonomy ID
                # print >>sys.stderr, 'Looking for taxid in genbank for %s'  % asm_prefix
                gb_fn = '%s.gbff.gz' % '.'.join(ff.split('.')[:-2])
                assert os.path.exists(gb_fn), "GFF file %s does not exist" % gb_fn
                gb_val = get_taxid_from_genbank(gb_fn)
                if gb_val is not None:
                    ti = gb_val
                    # print >>sys.stderr, 'Found taxid %s for %s' % (gb_val, asm_id)
                else:
                    # Give up, set taxon ID to assembly prefix
                    print >>sys.stderr, 'Assembly ID %s is not found, assigning %s' % (asm_id, asm_prefix)
                    ti = asm_prefix

        # Mapping from accession to gi
        gf = '%s.gbff.gz' % '.'.join(ff.split('.')[:-2])
        assert os.path.exists(gf), "GFF file %s does not exist" % gf
        gi_lookup = get_gi_lookup(gf)

        buffer = []
        buffer_char = 0
        for l in gzip.open(ff, 'rb'):
            if l.startswith('>'):
                acc = l.split()[0].strip('>')
                gi = gi_lookup[acc]
                # print >>args.outfile, '>ti|%s|gi|%s|ref|%s| %s' % (ti, gi, acc, ' '.join(l.split()[1:]))
                headerline = '>ti|%s|gi|%s|ref|%s| %s' % (ti, gi, acc, ' '.join(l.split()[1:]))
                if (file_char + buffer_char) > args.maxchar:
                    print >>sys.stderr, '%s.%02d.fna has %d characters' % (args.prefix, file_index, file_char)
                    # create a new file
                    outh.close()
                    file_index += 1
                    outh = open('%s.%02d.fna' % (args.prefix, file_index), 'w')
                    file_char = 0

                # Write buffer to file
                if len(buffer):
                    print >>outh, '\n'.join(buffer)
                    file_char += buffer_char
                # Create new buffer
                buffer = [ headerline ]
                buffer_char = 0
            else:
                #print >>args.outfile, l.strip('\n')
                buffer.append(l.strip('\n'))
                buffer_char += (len(l) - 1)

        # Clear buffer for file
        if (file_char + buffer_char) > args.maxchar:
            print >>sys.stderr, '%s.%02d.fna has %d characters' % (args.prefix, file_index, file_char)
            # create a new file
            outh.close()
            file_index += 1
            outh = open('%s.%02d.fna' % (args.prefix, file_index), 'w')
            file_char = 0
        # Write buffer to file
        print >>outh, '\n'.join(buffer)
        file_char += buffer_char

    print >>sys.stderr, '%s.%02d.fna has %d characters' % (args.prefix, file_index, file_char)
    outh.close()

if __name__=='__main__':
    import argparse
    parser = argparse.ArgumentParser(description='Get sequences with taxonomy id appended')
    parser.add_argument('--db', help="Database", default="refseq")
    parser.add_argument('--sumfile', help="Summary file", default="")
    parser.add_argument('--maxchar', type=float,  help="Max characters in file", default=4e9)
    parser.add_argument('tlevel', help="Taxonomy level to process")
    parser.add_argument('prefix', help="Output prefix")
    main(parser.parse_args())


#!/usr/bin/env python3

import sys
import random

def cmdlinearg(name, default=None):
    for arg in sys.argv:
        if arg.startswith(name + "="):
            return arg.split("=")[1]
    assert default is not None, name
    return default

random.seed(int(cmdlinearg('seed', sys.argv[-1])))
n = int(cmdlinearg('n'))
m = int(cmdlinearg('m'))
k = int(cmdlinearg('k'))

edge_set = set()
edges = []

for i in range(1, n):
    root = random.randrange(0, i)
    edges.append((root, i))
    edge_set.add((root, i))
    edge_set.add((i, root))

for i in range(0, m-n+1):
    i = random.randrange(0, n)
    j = random.randrange(0, n)
    while i == j or (i,j) in edge_set:
        i = random.randrange(0, n)
        j = random.randrange(0, n)
    edges.append((i, j))
    edge_set.add((i, j))
    edge_set.add((j, i))

print(n,m,k)
for (i,j) in edges:
    print(i+1, j+1)


import logging

def returns_notes_logger():
    """
    this method is used to create and configure logger
    :return: returns configured logger
    """
    log_file_format = '%(asctime)s %(message)s {%(pathname)s:%(lineno)d}'
    notes_logger = logging.getLogger("another")
    notes_log_handler = logging.FileHandler('notes_error_files.log', mode='w')
    notes_log_handler.setLevel(logging.DEBUG)
    notes_log_handler.setFormatter(logging.Formatter(log_file_format))
    notes_logger.addHandler(notes_log_handler)
    return notes_logger

notes_log = returns_notes_logger()