import asyncio
import json
import os

from slack_sdk.web.async_client import AsyncWebClient

if os.path.exists("claude.json"):
    with open("claude.json") as f:
        try:
            claude_config = json.load(f)
        except json.JSONDecodeError:
            claude_config = {}
else:
    claude_config = {}

class Chatbot:
    def __init__(
            self,
            slack_user_token=claude_config.get("slackUserToken"),
            slack_channel_id=claude_config.get("slackChannelId"),
            claude_member_id=claude_config.get("claudeMemberId"),
            proxy=None,
    ):
        self.client = AsyncWebClient(token=slack_user_token, proxy=proxy)
        self.slack_channel_id = slack_channel_id
        self.claude_member_id = claude_member_id

    async def ask_stream(self, message):
        if len(message) < 3000:  # Slack truncates message at ~3000 characters
            response = await self.client.chat_postMessage(channel=self.slack_channel_id, text=message)
            thread_ts = response["ts"]
        else:
            response = await self.client.chat_postMessage(channel=self.slack_channel_id, text=message[:3000])
            thread_ts = response["ts"]
            await self.client.chat_postMessage(
                channel=self.slack_channel_id,
                text=message[3000:],
                thread_ts=thread_ts,
            )

        await self.client.chat_postMessage(
            channel=self.slack_channel_id,
            text=f'<@{self.claude_member_id}> [assistant](#message)',
            thread_ts=thread_ts,
        )

        while True:
            await asyncio.sleep(1)
            replies_response = await self.client.conversations_replies(channel=self.slack_channel_id, ts=thread_ts)
            all_replies = replies_response["messages"]
            for reply in all_replies:
                if reply["user"] == self.claude_member_id:
                    break
            else:
                continue

            if reply["text"].endswith("_Typing…_"):
                yield reply["text"][:-11]
            else:
                yield reply["text"]
                break


import pandas as pd
import numpy as np
import os
from datetime import datetime, date
from tqdm import tqdm

tqdm.pandas()

if os.name =='nt':
    df_path = r"D:\Coding_pratice\_Data\GalaxyEducation\ICK"
else:
    df_path = '/Users/admin/_Work/Data/icanKID'

# df_User = pd.read_csv(
#     os.path.join(df_path,'df_User.csv')
#     )
df_User = pd.read_csv(
    os.path.join(df_path,'df_User_Child_DeviceBrand.csv')
    )
df_Engagement = pd.read_csv(
    os.path.join(df_path, 'df_Engagement.csv')
    )
df_Engagement_updated = pd.read_csv(
os.path.join(df_path, 'df_Engagement_updated.csv')
    )
df_Payment = pd.read_csv(
    os.path.join(df_path, 'df_Payment.csv')
    )
df_fake_user = pd.read_csv(
    os.path.join(df_path, 'df_fake_paid_users.csv')
    )

# Discard fake user
df_User = df_User[~df_User['UserID'].isin(df_fake_user['UserID'])]
df_Engagement = df_Engagement[~df_Engagement['UserID'].isin(df_fake_user['UserID'])]
df_Payment = df_Payment[~df_Payment['UserID'].isin(df_fake_user['UserID'])]

# Get user's Age
df_User['monthBday'] = pd.to_datetime(df_User.ChildBday).dt.month
df_User['Age'] = (pd.to_datetime(date.today()) - pd.to_datetime(df_User.ChildBday)).astype('<m8[Y]').astype('int')

def day_of_week_func(x):
    """
        Input: Normal date

        Return: Day in week
    """
    # Why don't need .dt if apply to Series
    # https://stackoverflow.com/questions/62803633/timestamp-object-has-no-attribute-dt#_=_
    x = pd.to_datetime(x).dayofweek
    day_of_week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    dict_dayofweek = {i:v for i,v in enumerate(day_of_week)}

    return dict_dayofweek[x]

def slope_function(x, y):
    # x: Timeseries
    # y: Datapoint
    n = len(x)
    x_sum = np.sum(x)
    y_sum = np.sum(y)
    xy_sum = np.sum(x*y)
    x2_sum = np.sum(x**2)

    denominator = n * x2_sum - x_sum**2
    if denominator == 0:
        return 0

    else:
        return (n * xy_sum - x_sum * y_sum) / (n * x2_sum - x_sum**2)

def slope_function_2(x, y):
    x_mean = np.mean(x)
    y_mean = np.mean(y)
    sx = np.std(x, ddof=1)
    sy = np.std(y, ddof=1)
    denominator = (len(x) * sx * sy)

    if denominator == 0:
        return 0
    else:
        r = np.sum((x - x_mean) * (y - y_mean)) / (len(x) * sx * sy)
        m = r * (sy / sx)
        return m

def accumulate_func(Series_values:pd.Series, take_abs:int):
    if take_abs:
        accumulate = lambda x,y: np.abs(x-y)
    else:
        accumulate = lambda x,y: x-y

    return ([
        accumulate(Series_values.iloc[i], Series_values.iloc[i-1]) for i in range(1, len(Series_values))
        ] if len(Series_values) > 1 else [0])

def interest_rate(Engagement, userID):
    usedDay_column = 'usedDays'
    if usedDay_column in Engagement.columns:
        pass
    else:
        usedDay_column = 'nth_day_from_registration'

    temp = Engagement[Engagement.UserID == userID].sort_values(by=usedDay_column)
    UserID_df = temp[['UserID']].head(1).reset_index(drop=True)
    temp['interest'] = temp['UsageTime'] * temp['Records']

    temp_check = temp.groupby('ActionType')['interest'].agg(
        total_interest=lambda x: sum(x),
        average_interest=lambda x: np.mean(x),
        # accumulate_inc_avg=lambda x: np.mean([
        #     x.iloc[i] - x.iloc[i-1] if (i != 0) and (x.iloc[i] - x.iloc[i-1]) > 0 else 0 for i in range(1, len(x))]),
        # accumulate_dec=lambda x: sum([
        #     x.iloc[i] - x.iloc[i-1] if (i != 0) and (x.iloc[i] - x.iloc[i-1]) < 0 else 0 for i in range(len(x))]),
        # total_accumulate_avg=lambda x: np.mean(accumulate_func(x, True)),
        accumulate=lambda x: np.sum(accumulate_func(x, False)),

    ).reset_index()

    grouped = temp.groupby('ActionType')
    # temp_check_2 = grouped.agg(
    #     Slope=pd.NamedAgg(
    #         column='interest',
    #         aggfunc=lambda x: slope_function(
    #             grouped[usedDay_column].get_group(x.name),
    #             x
    # ))).reset_index()

    temp_check_3 = grouped.agg(
        Slope_2=pd.NamedAgg(
            column='interest',
            aggfunc=lambda x: slope_function_2(
                grouped[usedDay_column].get_group(x.name),
                x
    ))).reset_index()

    # temp_check = pd.merge(temp_check, temp_check_2, on='ActionType')
    temp_check = pd.merge(temp_check, temp_check_3, on='ActionType')

    # To tell if the recent activity increase or decrease throughout the span of 't' days
    # Value range between [-1,1], with 0 as no trend (only 1 day of active)
    temp_check.reset_index(inplace=True, drop=True)
    temp_check['Slope_norm'] = temp_check['Slope_2'] / temp_check['average_interest']
    # display(temp_check)

    temp_check.drop(
        columns=['Slope_2'],
        inplace=True)

    most_active = temp_check.iloc[[temp_check['total_interest'].idxmax()]].reset_index(drop=True)
    most_accumulate = temp_check.iloc[[temp_check['accumulate'].idxmax()]].reset_index(drop=True)

    most_active.fillna(0, inplace=True)
    most_active.drop(columns=['accumulate','average_interest'], inplace=True)
    most_accumulate.fillna(0, inplace=True)
    most_accumulate.drop(columns=['total_interest', 'average_interest'], inplace=True)

    most_active.rename(columns={
        "ActionType": "MostInterest",
        "total_interest": "TotalInterestRatio",
        "Slope_norm": "InterestTrend"
    },inplace=True)

    most_accumulate.rename(columns={
        "ActionType": "MostAccumulate",
        "accumulate": "TotalAccuRatio",
        "Slope_norm": "AccuTrend"
    }, inplace=True)
    total_interest = temp_check['total_interest'].sum()
    total_accumulate = np.sum(np.abs(temp_check['accumulate']))

    if total_interest == 0:
        most_active['TotalInterestRatio'] = 0
    else:
        most_active['TotalInterestRatio'] = most_active['TotalInterestRatio'] / total_interest

    if total_accumulate == 0:
        most_accumulate['TotalAccuRatio'] = 0
    else:
        most_accumulate['TotalAccuRatio'] = most_accumulate['TotalAccuRatio'] / total_accumulate

    return pd.concat([UserID_df, most_active, most_accumulate], axis=1)

def create_dataframe(dataframes):
    df_User, df_Engagement, df_Payment = dataframes

    # Rebrand rarely used brand
    rep_brand = df_User['DeviceBrand'].value_counts()[
    df_User['DeviceBrand'].value_counts() < df_User['DeviceBrand'].value_counts().mean()].index.values
    df_User['DeviceBrand'] = df_User['DeviceBrand'].apply(lambda x: 'Other brand' if x in rep_brand else x)

    device_brand_df = pd.get_dummies(
            df_User[['UserID', 'DeviceBrand']],
            columns=['DeviceBrand'], prefix= '', prefix_sep=''
        )

    # Get total child
    print("Counting total children")
    total_child = df_User.groupby(by='UserID')[['ChildID']].nunique().reset_index()
    total_child.rename(
        columns={
            'ChildID':'TotalChild'
        }, inplace=True
    )

    # Get number of female and male children for each user
    print("Counting number of boy and girl seperate")
    no_fe_n_ma = df_User.pivot_table(index='UserID', columns='ChildGender', values='ChildID', aggfunc='count').reset_index()
    no_fe_n_ma.rename(
        columns={
            'female':'No.Female',
            'male': 'No.Male'
        }, inplace=True
    )
    no_fe_n_ma.fillna(0, inplace=True)

    # Get median age children
    print("Get median children age")
    age_child = df_User.groupby(by='UserID')[['Age']].median().reset_index()
    age_child.rename(
        columns={
            'Age':'MedianChildAge'
        }
    )

    # Rename certain columns to match with general database
    # df_Engagement['lastuse_byhour'] = df_Engagement['lastuse'].apply(lambda x: pd.to_datetime(x).strftime("%H:%M:%S"))
    df_Engagement.rename(
        columns={
            'open_time': 'Opentime',
            'payment_screen_time': 'PaymentScreenTime'
        }, inplace=True
    )

    # Acquire dataframe Engagement by condition
    # df_Engagement = df_Engagement[df_Engagement.nth_day_from_registration <=7]
    # df_Engagement = df_Engagement[condition]

    """
        Idea fromm: https://stackoverflow.com/questions/47360510/pandas-groupby-and-aggregation-output-should-include-all-the-original-columns-i
    """
    print("Obtain Users event base on condition")
    df_merge = df_Engagement.groupby(by=['UserID', 'EventDate', 'ActionType'], as_index=False).progress_apply(
        lambda x : x.sum() if ((x.dtypes=='int64') | (x.dtypes=='float64')).any() else x.head(1)
        )
    print('df_merge: \n',df_merge)
    # Retain labels and important features

    labels_df = df_merge[['UserID', 'cutoffDay', 'lateUser', 'Label']]

    # Create base df for merge
    print("Create base column for merge")
    based_df = df_Engagement.groupby('UserID', as_index=False).progress_apply(
        lambda x: x.head(1))[['UserID', 'UserJoinedDate']].reset_index(drop=True)
    print("Unique user: ", based_df.shape[0])

    # Total of Action/Events
    print("Get total Open time/Usage Time/Record/Payment screen time")
    total_merge = df_merge.groupby(by='UserID')[['Opentime', 'UsageTime', 'Records', 'PaymentScreenTime']].progress_apply(sum).reset_index()
    total_merge.rename(
        columns={'Opentime':'TotalOpentime', 'UsageTime':'TotalUsageTime',
                'Records':'TotalRecords', 'PaymentScreenTime': 'TotalPayscreentime'}, inplace=True
    )

    # Sum activity merge
    print('Get Open time/Usage Time/Record/Payment screen time base on Action type')
    activity_merge = pd.pivot_table(df_merge,
                                index='UserID',
                                columns='ActionType',
                                values=['Opentime', 'UsageTime', 'Records','PaymentScreenTime'],
                                aggfunc=np.sum)

    activity_merge.columns = ["".join((i,j.title())) for i,j in activity_merge.columns]
    activity_merge.reset_index(inplace=True)
    activity_merge.fillna(0, inplace=True)

    print("Get interest...")
    # map_func = np.vectorize(lambda x: interest_rate(df_Engagement, x))
    # interest_df = map_func(based_df['UserID'])
    interest_df = [interest_rate(df_Engagement, n) for n in tqdm(based_df['UserID'])]
    interest_df = pd.concat(interest_df, axis=0, ignore_index=True)

    print(interest_df)
    merge_dfs = [
        total_child, no_fe_n_ma, age_child, total_merge, activity_merge, interest_df, device_brand_df, labels_df
        ]

    print("Star merging....")
    for df in merge_dfs:
        df.reset_index(inplace=True, drop=True)
        based_df = based_df.merge(df, on='UserID', how='left')
        based_df.drop_duplicates(subset=['UserID'], keep='first', inplace=True, ignore_index=True)

    print("Total user with compress events: ", based_df.shape)
    return based_df

def getEvent_before_cutoff(df_events, cutOffDay=3):
    days_from_regis = df_events.groupby('UserID')[['nth_day_from_registration']].min().reset_index()
    days_from_regis.rename(
        columns={
            "nth_day_from_registration": "MinFirstDay"
        },inplace=True
    )
    days_from_regis['cutoffDay'] = days_from_regis.MinFirstDay + cutOffDay
    days_from_regis['lateUser'] = (days_from_regis['MinFirstDay'] > 0).astype(int)

    Engagement = df_events.merge(days_from_regis, on='UserID',how='left')
    Engagement = Engagement[
        (Engagement['cutoffDay'] - Engagement['nth_day_from_registration']) > 0
        ].reset_index(drop=True)
    Engagement['usedDays'] = Engagement['nth_day_from_registration'] - Engagement['MinFirstDay']
    return Engagement

def setLabel(df_events, df_Payment, markDay=30):
    df_events = df_events.merge(df_Payment[['UserID','gapday']], how='left', on='UserID')
    """
        If label = True -> User paid before markDay
        else -> Free user
    """
    df_events['Label'] = ((df_events['gapday'] - df_events['MinFirstDay']) <= markDay).astype(int)
    print("Total events with labels: ", df_events.shape)
    return df_events

Engagements = getEvent_before_cutoff(df_Engagement, cutOffDay=3)
Engagements = setLabel(Engagements, df_Payment, markDay=30)
list_dataframes = [
    df_User,
    Engagements,
    df_Payment
]
condition_df = create_dataframe(list_dataframes)
condition_df
print(condition_df)

save_path = os.path.join(
    df_path, "Dataset"
    )
os.makedirs(
    save_path, exist_ok=True
)
condition_df.to_csv(
    os.path.join(save_path, "3days_cutoff.csv"), index=False
)


from datetime import datetime
import datetime as dt

from activity.models import EatRainbowActivity
from master.models import EatRainbowMaster

def get_previous_date(date_str):
    """

    :param date_str: date in yyyy-mm-dd formate
    :return:  date in yyyy-mm-dd formate
    """
    date_object = datetime.strptime(date_str, "%Y-%m-%d")
    day = dt.timedelta(days=1)
    reduce_date = date_object - day
    return reduce_date.strftime("%Y-%m-%d")

def get_difference_from_current_date(date_str):
    date_object = datetime.strptime(date_str, "%Y-%m-%d")
    date = datetime.now() - date_object
    return date.days

def user_first_login(user, eat_date):
    """

    this methods takes user and date object
    return eat_activity object matched with master first sequence

    """
    master = EatRainbowMaster.objects.get(sequence=1)
    eat_rainbow_object = EatRainbowActivity.objects.create(user=user, date=eat_date, master=master,
                                                               red_serving=master.red_serving,
                                                               cream_serving=master.cream_serving,
                                                               yellow_serving=master.yellow_serving,
                                                               kiwi_serving=master.kiwi_serving,
                                                               blue_serving=master.blue_serving,
                                                               green_serving=master.green_serving)
    eat_rainbow_object.enjoy_regular.set(master.enjoy_regular.all(), clear=True)
    eat_rainbow_object.eat_more.set(master.eat_more.all(), clear=True)
    eat_rainbow_object.eat_less.set(master.eat_less.all(), clear=True)
    eat_rainbow_object.eat_avoid.set(master.eat_avoid.all(), clear=True)
    eat_rainbow_object.tag.set(master.tag.all(), clear=True)
    eat_rainbow_object.save()
    return eat_rainbow_object


import pprint

import click

from . import constants, iapi, utils

@click.group()
@click.option("--verbose/--quiet", "-v/-q", default=False)
def main(verbose: bool):
    """main entry point.
    """
    if verbose:
        iapi.set_verbose_mode()

# ref.
# https://click.palletsprojects.com/en/8.1.x/options/#multi-value-options
# https://click.palletsprojects.com/en/8.1.x/utils/#pager-support
@main.command()
@click.option("--datadir", "-d", default='.')
@click.option("--output", "-o", default=constants.DATABASE_FILEPATH)
@click.option(
    "--zip-filenames", "-Z", nargs=2,
    default=constants.ZIPCODE_ZIP_FILENAMES
)
@click.option(
    "--csv-filenames", "-C", nargs=2,
    default=constants.ZIPCODE_CSV_FILENAMES
)
def initdb(
    datadir: str, output: str,
    zip_filenames: tuple[str, str],
    csv_filenames: tuple[str, str],
):
    """
    Prase csv files extracted from zip files, save resutl data as a db file.
    """
    iapi.initdb(
        datadir, output,
        zip_filenames=zip_filenames, csv_filenames=csv_filenames
    )

@main.command()
@click.option("--db-path", "-d", default=constants.DATABASE_FILEPATH)
@click.argument("zipcode")
def search(db_path: str, zipcode: str):
    """
    Search address info from rhe database file by given zip code.
    """
    res = iapi.search_by_zipcode(zipcode, db_path, limit=0)
    fmt = pprint.pformat

    if len(res) > utils.get_term_lines():
        click.echo_via_pager(fmt(r) for r in res)
    else:
        for zipd in res:
            click.echo(fmt(zipd))


"""
Script to convert angular units
"""

import argparse as ap
from datetime import datetime
from astropy import units as u
from astropy.coordinates import (
    Angle,
    EarthLocation,
    )
from astropy.time import Time

SITE_LATITUDE = 28.761935*u.deg
SITE_LONGITUDE = -17.877591*u.deg
SITE_ELEVATION = 2348*u.m

SITE_LOCATION = EarthLocation(lat = SITE_LATITUDE,
                              lon = SITE_LONGITUDE,
                              height = SITE_ELEVATION)

def argParse():
    """
    Argument parser settings

    Parameters
    ----------
    None

    Returns
    -------
    args : array-like
        Array of command line arguments

    Raises
    ------
    None
    """

    parser = ap.ArgumentParser()

    parser.add_argument('angle',
                        help='angle to be converted',
                        type=str)

    parser.add_argument('--rad2deg',
                        help='radians to degrees',
                        action='store_true')

    parser.add_argument('--deg2rad',
                        help='degrees to radians',
                        action='store_true')

    parser.add_argument('--dec2sexagesimal',
                        help='declination in degrees to sexagesimal',
                        action='store_true')

    parser.add_argument('--dec2deg',
                        help='declination in sexagesimal to degrees',
                        action='store_true')

    parser.add_argument('--ra2sexagesimal',
                        help='right ascension in degrees to sexagesimal',
                        action='store_true')

    parser.add_argument('--ra2deg',
                        help='right ascension in sexagesimal to degrees',
                        action='store_true')

    parser.add_argument('--ra2ha',
                        help='right ascension to current hour angle',
                        action='store_true')

    parser.add_argument('--ha2ra',
                        help='hour angle to current right ascension',
                        action='store_true')

    return parser.parse_args()

def rad2deg(angle):
    """
    convert radians to degrees
    """
    angle_rad = Angle(angle, u.rad)

    return angle_rad.deg

def deg2rad(angle):
    """
    convert degrees to radians
    """
    angle_deg = Angle(angle, u.deg)

    return angle_deg.rad

def dec2sexagesimal(angle):
    """
    convert declination in degrees to sexagesimal format
    """
    angle_deg = Angle(angle, u.deg)

    return angle_deg.to_string()

def dec2deg(angle):
    """
    convert declination in sexagesimal format to degrees
    """
    angle_deg = Angle(angle, u.deg)

    return angle_deg

def ra2sexagesimal(angle):
    """
    convert a right ascension in degrees to sexagesimal format
    """
    angle_deg = Angle(angle, u.deg)
    angle_hourangle = Angle(angle.hourangle, u.hourangle)

    return angle_hourangle.to_string(), angle_hourangle

def ra2deg(angle):
    """
    convert a right ascension in sexagesimal format to degrees
    """
    angle_sexagesimal = Angle(angle, u.hourangle)

    return angle_sexagesimal.deg

def ra2ha(ra):
    """
    convert right ascension to hour angle at the current time
    """
    ra = Angle(ra, u.hourangle)

    utc_now = Time(datetime.utcnow(), scale='utc', location=SITE_LOCATION)
    lst = utc_now.sidereal_time('apparent')

    return (lst - ra).wrap_at(12*u.hourangle)

def ha2ra(ha):
    """
    convert hour angle to right ascension at the current time
    """
    ha = Angle(ha, u.hourangle)

    utc_now = Time(datetime.utcnow(), scale='utc', location=SITE_LOCATION)
    lst = utc_now.sidereal_time('apparent')

    return (lst - ha).wrap_at(24*u.hourangle)

def main(args):
    """
    Carry out requested conversion
    """

    if args.rad2deg:
        print('Angle in degrees: ', rad2deg(args.angle))

    elif args.deg2rad:
        print('Angle in radians: ', deg2rad(args.angle))

    elif args.dec2sexagesimal:
        print('Declination in sexagesimal format: ', dec2sexagesimal(args.angle))

    elif args.dec2deg:
        print('Declination in degrees: ', dec2deg(args.angle))

    elif args.ra2sexagesimal:
        print('Right ascension in sexagesimal format: ', ra2sexagesimal(args.angle))

    elif args.ra2deg:
        print('Right ascension in degrees: ', ra2deg(args.angle))

    elif args.ra2ha:
        print('Hour angle: ', ra2ha(args.angle))

    elif args.ha2ra:
        print('Right ascension: ', ha2ra(args.angle))

    return None

if __name__ == "__main__":

    args = argParse()

    main(args)


from datetime import datetime
import time
now = datetime.now()

i = 0
while i < 10:
    i+=1
    time.sleep(1)
    stampTime = datetime.now()
    print(datetime.now())

# returns (minutes, seconds)
diff = stampTime - now
print(now)
print(stampTime)
print(diff)
print(diff.total_seconds())
minutes = divmod(diff.total_seconds(), 60)
print('Total difference in minutes: ', minutes[0], 'minutes', minutes[1], 'seconds')


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

# In[ ]:

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

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

# Input data files are available in the read-only "../../../input/imnikhilanand_heart-attack-prediction/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk("../../../input/imnikhilanand_heart-attack-prediction"):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All"
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session

# In[ ]:

d=pd.read_csv("../../../input/imnikhilanand_heart-attack-prediction/data.csv")

# In[ ]:

d.columns

# In[ ]:

d.columns=['age', 'sex', 'cp', 'bp', 'chol', 'fbs', 'restecg', 'thalach', 'exang', 'oldpeak', 'slope', 'ca', 'thal','num']

# In[ ]:

import matplotlib.pyplot as plt
d.head()

# In[ ]:

# In[ ]:

import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn import metrics

# In[ ]:

# In[ ]:

d["chol"].replace({'?': 0},inplace=True)
#d["age"].replace({'?': 0},inplace=True)
d["bp"].replace({'?': 0},inplace=True)
#d["sex"].replace({'?': 0},inplace=True)
#d["cp"].replace({'?': 0},inplace=True)
#d["num"].replace({'?': 0},inplace=True)

# In[ ]:

x=np.array(d.iloc[:,1:5])
y=np.array(d[['num']])
X_train, X_test, y_train, y_test = train_test_split( x, y, test_size=0.2, random_state=4)
LR = LogisticRegression().fit(X_train,y_train)

# In[ ]:

yhat = LR.predict(X_test)
yhat

# In[ ]:

yhat_prob = LR.predict_proba(x)
yhat_prob

# In[ ]:

accuracy = metrics.accuracy_score(y_test, yhat)
accuracy_percentage = 100 * accuracy
accuracy_percentage

# In[ ]:

from sklearn.metrics import jaccard_similarity_score
jaccard_similarity_score(y_test, yhat)

# In[ ]:

from sklearn.metrics import classification_report, confusion_matrix
import itertools
def plot_confusion_matrix(cm, classes,
                          normalize=False,
                          title='Confusion matrix',
                          cmap=plt.cm.Blues):
    """ This function prints and plots the confusion matrix. Normalization can be applied by setting `normalize=True`. """
    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    print(cm)

    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    print()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    fmt = '.2f' if normalize else 'd'
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, format(cm[i, j], fmt), horizontalalignment="center", color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
print(confusion_matrix(y_test, yhat, labels=[1,0]))

# In[ ]:

# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test, yhat, labels=[1,0])
np.set_printoptions(precision=2)

# Plot non-normalized confusion matrix
plt.figure()
plot_confusion_matrix(cnf_matrix, classes=['num=1','num=0'],normalize= False,  title='Confusion matrix')

# In[ ]:

print (classification_report(y_test, yhat))

# In[ ]:

print(len(d['chol']),len(yhat_prob[:,0]))

# In[ ]:

plt.scatter(d['age'],yhat_prob[:,0], s=10)
print()

# In[ ]:


"""empty message

Revision ID: 26c949a8de36
Revises: 20f51de281fe
Create Date: 2022-12-02 12:13:27.533902

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '26c949a8de36'
down_revision = '20f51de281fe'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('patients', schema=None) as batch_op:
        batch_op.add_column(sa.Column('patientid', sa.Integer(), nullable=False))
        batch_op.drop_column('id')

    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.drop_column('hiredate')

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('user', schema=None) as batch_op:
        batch_op.add_column(sa.Column('hiredate', sa.DATE(), nullable=True))

    with op.batch_alter_table('patients', schema=None) as batch_op:
        batch_op.add_column(sa.Column('id', sa.INTEGER(), nullable=False))
        batch_op.drop_column('patientid')

    # ### end Alembic commands ###


from time import sleep
from kafka import KafkaProducer
from json import dumps

producer = KafkaProducer(
    value_serializer=lambda m: dumps(m).encode(),
    bootstrap_servers=['localhost']
)

for i in range(3):
    producer.send('quickstart-events', value='009')
    sleep(0.001)


from Settings.InitParameters import InitParameters
import pygame

class Engine:
    '''
    Initializes PyGame
    '''
    def __init__(self):
        pygame.init()
        pygame.display.set_caption("Snake")
        self.window = pygame.display.set_mode((InitParameters.RESOLUTION, InitParameters.RESOLUTION))
        self.clock = pygame.time.Clock()

    '''
    Handles all drawing on screen
    '''
    def redraw(self, surface, objects):
        pygame.time.delay(100)
        self.clock.tick(10)
        surface.fill((0, 0, 0))
        for obj in objects:
            obj.draw(surface)
        self._draw_grid(surface)

        pygame.display.update()

    '''
    Draws grid on pygame window
    '''
    def _draw_grid(self, surface):
        x, y = 0, 0
        for l in range(InitParameters.RESOLUTION):
            x+=InitParameters.GRID_DISTANCE
            y+=InitParameters.GRID_DISTANCE

            pygame.draw.line(surface, (255,255,255), (x,0), (x, InitParameters.RESOLUTION))
            pygame.draw.line(surface, (255,255,255), (0, y), (InitParameters.RESOLUTION, y))


import matplotlib.pyplot as plt
x1 = [1,2,3,4,5,6]
y1 = [2,4,6,18,12,12]

x2 = [2,6,8]
y2 = [6,4,9]

plt.plot(x2,y2,linestyle=":")
plt.plot(x1,y1)
plt.title("test",fontsize=20, color="red")
plt.xlabel("x-axis")

plt.show()


from functools import wraps
import operator

def err(f):
    @wraps(f)
    def wrapper(*a,**kw):
        try:
            return f(*a,**kw)
        except Exception as e:
            return DotDict()
    return wrapper

class DotDict(dict):
    __getattr__ = dict.__getitem__
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__
    def __init__(self, dct={}):
        for key, value in dct.items():
            if hasattr(value, 'keys'):
                value = DotDict(value)
            self[key] = value
    def __missing__(self, key):
        self[key] = DotDict()
        return self[key]

def dotset(obj, keys, value):
    attrs = keys.split('.')
    target = obj
    if '.' in keys:
        target = dotget(obj, '.'.join(attrs[:-1]))
    setattr(target, attrs[-1], value)

def dotget(obj, attr):
    return reduce(getattr, attr.split('.'), obj)

def compare_object_values(a, b, attrs, cmp=lambda x,y: x==y, cmp_value=lambda x: x, flat=True, allow_empty_values=False):
    ac = DotDict()
    bc = DotDict()
    changes = DotDict()
    for key in attrs:
        setattr(ac, key, dotget(a, key))
        setattr(bc, key, dotget(b, key))
    for key in attrs:
        if not cmp(dotget(ac, key), dotget(bc, key)):
            value = dotget(ac, cmp_value(key))
            if not allow_empty_values and not value:
                continue
            if flat:
                setattr(changes, key, dotget(ac, key))
            else:
                dotset(changes, key, dotget(ac, key))
    return changes


from .models import Article, Comment
from slugify import slugify
from django import forms
from djrichtextfield.widgets import RichTextWidget

class ArticleForm(forms.ModelForm):
    """ Form to create an article """
    title = forms.CharField(max_length=200)

    def save(self, commit=True):
        instance = super().save(commit=False)
        # Set the slug value from the title
        instance.slug = slugify(instance.title)
        if commit:
            instance.save()
        return instance

    class Meta:
        model = Article
        fields = ['title', 'featured_image', 'image_alt', 'excerpt', 'content']

        contents = forms.CharField(widget=RichTextWidget())

        widget = {
            "excerpt": forms.Textarea(attrs={"rows": 5}),
        }

        labels = {
            "title": "Article Title",
            "featured_image": "Article Image",
            "image_alt": "Describe Image",
            "excerpt": "Excerpt",
            "content": "Content",
        }

class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('body',)


from django.contrib import admin
from django.urls import path
from . import views

app_name='website'
urlpatterns = [
    # all users
    path('' , views.homePage , name='home-page'),
    path('caravans/' , views.caravanList , name = 'all-caravans'),
    path('caravan/<caravan_id>' , views.show_details_of_caravans , name='caravan-detailes'),
    path('booking/<caravan_id>',views.bookCaravan ,name='booking'),
    path('showBook/', views.showUserbook , name='showBook'),
    path('abutus/' , views.aboutUs , name='abouUs'),
    path('abutus/' , views.aboutUs , name='abouUs'),
    #admin Only
    path('addCaravanAdmin/' , views.addCaravanAdmin , name='addcaravanadmin'),
    path('editcaravan/<caravan_id>',views.updateCaravan ,name='update'),
    path('delete/<caravan_id>',views.deleteCaravan, name='delete'),
    path('showAllPending/' , views.showNeedConfirmationCaravans,name='PendingCaravans'),
    path('showOnePending/<caravan_id>', views.showSelctedCarvanDetailsByAdminToConfirm , name='pendingCaravan'),
    path("confirmCaravan/<caravan_id>" , views.confirmCaravan , name='confirmation'),
    path("rejectCaravan/<caravan_id>" , views.rejectCaravan , name='reject'),
    path('adminManagAllCravanBook/<caravan_id>',views.adminManagAllCravanBook , name='adminManageBook'),
    path('deleteBookingByAdmin/<book_id>',views.adminDeleteBooking , name='deleteBookingAdmin'),
    path('updateBookingByAdmin/<book_id>',views.adminUpdateingBook , name='updatingBooking'),
    path('notreadyCaravan/',views.caravanNotReadyAdmin , name='notreadyCaravan'),
    #investor user
    path('addCaravanUser/' , views.addCaravanuser , name='addcaravanuser'),
    path('showCaravanStatusUser' , views.showCaravanStatusUser , name='showCaravanStatusUser'),
    path('showUserCaravanStatusBooking/<carvan_id>' , views.showUserCaravanIsBookStatus , name='showUserCaravanIsBookStatus'),

]


import logging
import regex

from logrec.dataprep.model.chars import MultilineCommentStart, MultilineCommentEnd, OneLineCommentStart, \
    NewLine, Backslash, Quote
from logrec.dataprep.model.containers import MultilineComment, OneLineComment, StringLiteral, \
    ProcessableTokenContainer
from logrec.dataprep.model.numeric import Number, D, F, L, DecimalPoint, HexStart, E
from logrec.dataprep.model.placeholders import placeholders
from logrec.dataprep.model.word import ParseableToken

logger = logging.getLogger(__name__)

START_MULTILINE_COMMENT = MultilineCommentStart()
END_MULTILINE_COMMENT = MultilineCommentEnd()

START_ONE_LINE_COMMENT = OneLineCommentStart()
NEW_LINE = NewLine()

QUOTE = Quote()
BACKSLASH = Backslash()

tabs = ["\t" + str(i) for i in range(11)]

multiline_comments_tokens = ["/*", "*/"]

two_character_tokens = [
    "==",
    "!=",
    "**",
    "//",
    "++",
    "--",
    "+=",
    "-=",
    "/=",
    "*=",
    "%=",
    "<=",
    ">=",
    "^=",
    "&=",
    "|=",
    ">>",
    "<<",
    "&&",
    "||"
]

one_character_tokens = [
    "+",
    "*",
    "!",
    "/",
    ">",
    "<",
    "="
]

two_char_verbose = [
    "\t", "\n"
]

newlines_and_tabs_reprs = [
    '\\t', '\\n'
]

one_char_verbose = [
    "{", "}", "[", "]", ",", ".", "-", "?", ":", "(", ")", ";", '"', "&",
    "|", "\\", "@", "#", "$", "'", "~", "%", "^", "\\"
]

delimiters_to_drop = "[\[\] ,.\-?:\n\t(){};\"&|_#\\\@$]"
delimiters_to_drop_verbose = " " #TODO according to the new philosophy we shoulnt drop anything

special_tokens = tabs + multiline_comments_tokens + two_character_tokens + one_character_tokens + two_char_verbose + one_char_verbose + newlines_and_tabs_reprs

key_words = [
"abstract",
"continue",
"for",
"new",
"switch",
"assert",
"default",
"package",
"synchronized",
"boolean",
"do",
"if",
"private",
"this",
"break",
"double",
"implements",
"protected",
"throw",
"byte",
"else",
"import",
"public",
"throws",
"case",
"enum",
"instanceof",
"return",
"transient",
"catch",
"extends",
"int",
"short",
"try",
"char",
"final",
"interface",
"static",
"void",
"class",
"finally",
"long",
"strictfp",
"volatile",
"float",
"native",
"super",
"while",
"true",
"false",
"null"
]

NUMBER_REGEX = '-?(?:0x[0-9a-fA-F]+[lL]?|[0-9]+[lL]?|(?:[0-9]*\.[0-9]+|[0-9]+(?:\.[0-9]*)?)(?:[eE][-+]?[0-9]+)?[fFdD]?)'

def is_number(s):
    return regex.fullmatch(NUMBER_REGEX, s)

def find_all_comment_string_literal_symbols(token_list):
    res = []
    for ind in range(len(token_list)):
        if token_list[ind] == QUOTE:
            i = ind - 1
            while i >= 0 and token_list[i] == BACKSLASH:
                i -= 1
            if (ind - i) % 2 == 1 and (ind == 0 or token_list[ind-1] != "'" or ind +1 >= len(token_list) or token_list[ind+1] != "'"):
                res.append((ind, token_list[ind]))
        elif token_list[ind] in [START_MULTILINE_COMMENT, END_MULTILINE_COMMENT, START_ONE_LINE_COMMENT, NEW_LINE]:
            res.append((ind, token_list[ind]))
    return res

def replace_segments(token_list, segments_to_remove):
    new_token_list = []
    curr_ind = 0
    for begin, end, clazz in segments_to_remove:
        new_token_list.extend(token_list[curr_ind:begin])
        new_token_list.append(clazz(token_list[begin+1:end]))
        curr_ind = end if clazz == OneLineComment else end + 1
    if curr_ind < len(token_list):
        new_token_list.extend(token_list[curr_ind:])
    return new_token_list

def process_comments_and_str_literals(token_list, context):
    comment_string_literal_symbols_locations = find_all_comment_string_literal_symbols(token_list)
    active_symbol, active_symbol_index = None, -1
    segments_to_remove = []

    for index, symbol in comment_string_literal_symbols_locations:
        if active_symbol is None:
            if symbol == END_MULTILINE_COMMENT:
                logger.warning(
                    f"{repr(' '.join(list(map(lambda t: str(t),token_list[index-100:index+1]))))}, index: {index}")
                return token_list
            elif symbol == NEW_LINE:
                pass
            else:
                active_symbol, active_symbol_index = symbol, index
        elif active_symbol == QUOTE:
            if symbol == NEW_LINE:
                logger.warning(
                    f"{repr(' '.join(list(map(lambda t: str(t),token_list[index-100:index+1]))))}, index: {index}")
                return token_list
            elif symbol == QUOTE:
                segments_to_remove.append((active_symbol_index, index, StringLiteral))
                active_symbol, active_symbol_index = None, -1
        elif active_symbol == START_ONE_LINE_COMMENT:
            if symbol == NEW_LINE:
                segments_to_remove.append((active_symbol_index, index, OneLineComment))
                active_symbol, active_symbol_index = None, -1
        elif active_symbol == START_MULTILINE_COMMENT:
            if symbol == END_MULTILINE_COMMENT:
                segments_to_remove.append((active_symbol_index, index, MultilineComment))
                active_symbol, active_symbol_index = None, -1
        else:
            raise AssertionError(f"Unknown symbol: {active_symbol}")
    token_list = replace_segments(token_list, segments_to_remove)
    return token_list

def replace(token_list, start, end, new_symbol):
    len_before_replacement = len(token_list)
    string_literal_content = token_list[start+1:end]
    del (token_list[start: end if new_symbol == OneLineComment else end+1])
    token_list.insert(start, new_symbol(string_literal_content))
    return len_before_replacement - len(token_list)

def strip_off_identifiers(token_list, context):
    identifiers_to_ignore = context['identifiers_to_ignore']
    non_identifiers = set(
        key_words + multiline_comments_tokens + two_character_tokens + one_character_tokens + one_char_verbose + two_char_verbose + \
        list(placeholders.values()) + tabs + identifiers_to_ignore)

    result = []
    for token in token_list:
        if not is_number(token) and token not in non_identifiers:
            result.append(placeholders['identifier'])
        else:
            result.append(token)
    return result

def process_number_literal(possible_number):
    if is_number(possible_number) and possible_number not in tabs:
        parts_of_number = []
        if possible_number.startswith('-'):
            parts_of_number.append('-')
            possible_number = possible_number[1:]
        if possible_number.startswith("0x"):
            parts_of_number.append(HexStart())
            possible_number = possible_number[2:]
            hex = True
        else:
            hex = False
        for ch in possible_number:
            if ch == '.':
                parts_of_number.append(DecimalPoint())
            elif ch == 'l' or ch == 'L':
                parts_of_number.append(L())
            elif (ch == 'f' or ch == 'F') and not hex:
                parts_of_number.append(F())
            elif (ch == 'd' or ch == 'D') and not hex:
                parts_of_number.append(D())
            elif (ch == 'e' or ch == 'E') and not hex:
                parts_of_number.append(E())
            else:
                parts_of_number.append(ch)
        return Number(parts_of_number)
    else:
        return ParseableToken(possible_number)

def process_numeric_literals(token_list, context):
    res = []
    for token in token_list:
        if isinstance(token, ParseableToken):
            numbers_separated = list(
                filter(None, regex.split(
                    f'(?:^|(?<=[^[:lower:][:upper:][:digit:]_]))({NUMBER_REGEX})(?![[:lower:][:upper:][:digit:]_.]|$)',
                    str(token))))
            for possible_number in numbers_separated:
               res.append(process_number_literal(possible_number))
        elif isinstance(token, ProcessableTokenContainer):
            for subtoken in token.get_subtokens():
                res.extend(process_numeric_literals(subtoken))
        else:
            res.append(token)
    return res


from django.urls import path
from .views import Home, Search, view_article

urlpatterns = [

    path('article/<int: article_id>', view_article, name='view-article'),
    path('', Home.as_view(), name='home'),
    path('search/', Search.as_view(), name='search'),

]


from urllib import parse
from markupsafe import Markup

def varencode(s):
    """
    URL encode string after first replacing spaces with underscores.
    :param s: String to url encode.
    :type s: basestring
    :return: URL-encoded string.
    """
    if type(s) == 'Markup':
        s = s.unescape()
    s = s.replace(' ', '_')
    s = s.encode('utf8')
    s = parse.quote_plus(s)
    return Markup(s)

def varunencode(s):
    """
    Unencode string url-encoded with varencode() to return it to its original readable state.
    :param s: String to unencode.
    :type s: basestring
    :return: Unencoded string.
    """
    s = s.replace('_', ' ')
    return parse.unquote(s)


import torch
import torchaudio
import matplotlib.pyplot as plt
from function.play_signal import play_signal
from function.get_noise import get_noise
from dataset import AudioDataset
import numpy as np
from model_demo import Denoiser
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader

#IMPORT SIGNAL

file_path = "examples\data_dir\MIDI-Unprocessed_01_R1_2006_01-09_ORIG_MID--AUDIO_01_R1_2006_02_Track02_wav.wav"
signal, sample_rate = torchaudio.load(file_path)

#-------------------------------------------------

#ADD NOISE

SNR = 20
noise = get_noise(signal,SNR)

for i in range(1):
    if(i ==0):
        signal_noise = signal + noise

    signal_noise = signal_noise + noise

#-------------------------------------------------------

#DENOISER

denoiser_model = Denoiser()

#denoiser_model = torch.load("trained_model/trained_model.pht")
#denoiser_model.eval()

n_fft = 512

spectrogram = torchaudio.transforms.Spectrogram(n_fft=n_fft)(signal)

noisy_spectrogram = torchaudio.transforms.Spectrogram(n_fft=n_fft)(signal_noise)

# Trénovanie autoencodéra pre úpravu spektrogramov
criterion = nn.MSELoss()
optimizer = optim.Adam(denoiser_model.parameters(), lr=0.001)

# Uchováva stratu počas trénovania
losses = []

num_epochs = 10

for epoch in range(num_epochs):
    # Predikcia a výpočet chyby
    output_spectrogram = denoiser_model(noisy_spectrogram)
    loss = criterion(output_spectrogram, spectrogram)

    # Spätná propagácia a aktualizácia váh
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    losses.append(loss.item())

    print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')

# Predikcia denoised spektrogramu pomocou natrénovaného denoisera
with torch.no_grad():
    denoised_spectrogram = denoiser_model(noisy_spectrogram)

# Vizualizácia pôvodného, zašumeného a denoised spektrogramu
plt.figure(figsize=(18, 12))

plt.subplot(3, 1, 1)
plt.imshow(torch.log(spectrogram[0] + 1e-9).numpy(), aspect='auto', origin='lower')
plt.title('Original Spectrogram')

plt.subplot(3, 1, 2)
plt.imshow(torch.log(noisy_spectrogram[0] + 1e-9).numpy(), aspect='auto', origin='lower')
plt.title('Noisy Spectrogram')

plt.subplot(3, 1, 3)
plt.imshow(torch.log(denoised_spectrogram[0] + 1e-9).numpy(), aspect='auto', origin='lower')
plt.title('Denoised Spectrogram')

plt.show()

#------------------------------------------------------------------------

#rekonštruovanie signálu - in progress problém že do modelu hádžem realne čísla a výstup su tiež realne čísla, takže to neviem transformovať späť

""""
reconstructed_signal = torch.istft(denoised_spectrogram,n_fft)

signal = signal.numpy()
plt.subplot(2,1,1)
plt.plot(signal[0])
plt.title('Original signal')

reconstructed_signal =reconstructed_signal.numpy()
plt.subplot(2,1,2)
plt.plot(reconstructed_signal[0])
plt.title('Reconstructed signal')

plt.show()
"""

#rekonštrukcia signálu s použitím funkcie torchaudio.transforms

reconstructed_signal = torchaudio.transforms.GriffinLim(n_fft=n_fft)(denoised_spectrogram)
signal_1 = signal.numpy()
plt.subplot(2,1,1)
plt.plot(signal_1[0])
plt.title('Original signal')

signal_new_1 = reconstructed_signal.numpy()
plt.subplot(2,1,2)
plt.plot(signal_new_1[0])
plt.title('transformed signal')

plt.show()


# -*- coding: utf-8 -*-
from flask import (
    Blueprint,
    current_app,
    flash,
    redirect,
    render_template,
    request,
    url_for,
    abort,
    jsonify,
    current_app,
    g
)
import requests

from eternity_backend_server.extensions import csrf_protect
from eternity_backend_server.blueprints.datamin.datamin import bishijieclient
from eternity_backend_server.extensions import db
from eternity_backend_server.blueprints.datamin.models import DATAMINFLAG, DATAMIN
from eternity_backend_server.blueprints.datamin.etherscanclient import etherscanclient
from eternity_backend_server.extensions import db
from eternity_backend_server.settings import ETHERSCAN_APIKEY, ETHERSCAN_TOKEN

datamin_bp = Blueprint("datamin", __name__)

@csrf_protect.exempt
@datamin_bp.route("/news/get", methods=["GET"])
def get_view():
    """获取新闻时讯的数据内容

       @@@
       ### description
       > finished
       ### args
       None

       ### return
       status code: **200**
       ```json

        [
          {
            "content": "A新闻的内容",
            "look_count_flase": "看空12",
            "look_count_true": "看多23",
            "title": "A新闻的标题"
          },
          {
            "content": "B新闻的内容",
            "look_count_flase": "看空25",
            "look_count_true": "看多12",
            "title": "B新闻的标题"
          },
          {
            "content": "C新闻的内容",
            "look_count_flase": "看空33",
            "look_count_true": "看多12",
            "title": "C新闻的标题"
          },
        ]
       ```
       @@@
       """
    datamin = DATAMIN.query.filter().first()
    return jsonify(datamin.datalist)

@csrf_protect.exempt
@datamin_bp.route("/news/send_heartbeat", methods=["GET"])
def send_heartbeat():
    """更新新闻时讯数据库信息

       @@@
       ### description
       > finished
       ### args
       None

       ### return
       status code: **200**
       已经更新过了
       ```json
        {
            "result": "is update",
            "message": [
              {
                "content": "A新闻的内容",
                "look_count_flase": "看空12",
                "look_count_true": "看多23",
                "title": "A新闻的标题"
              },
              {
                "content": "B新闻的内容",
                "look_count_flase": "看空25",
                "look_count_true": "看多12",
                "title": "B新闻的标题"
              },
              {
                "content": "C新闻的内容",
                "look_count_flase": "看空33",
                "look_count_true": "看多12",
                "title": "C新闻的标题"
              },
            ],
            "flag": 1
        }

       status code: **200**
       现在更新成功。
       ```json
        {
            "result": "is update",
            "message": [
              {
                "content": "A新闻的内容",
                "look_count_flase": "看空12",
                "look_count_true": "看多23",
                "title": "A新闻的标题"
              },
              {
                "content": "B新闻的内容",
                "look_count_flase": "看空25",
                "look_count_true": "看多12",
                "title": "B新闻的标题"
              },
              {
                "content": "C新闻的内容",
                "look_count_flase": "看空33",
                "look_count_true": "看多12",
                "title": "C新闻的标题"
              },
            ],
            "flag": 1
        }

       ```
       @@@
       """
    dataminflag = DATAMINFLAG.query.filter(DATAMINFLAG.id==1).first()
    if dataminflag.flag == 1:
        datamin = DATAMIN.query.filter(DATAMIN.id==1).first()
        return jsonify({
            "result": "is update",
            "message": datamin.datalist,
            "flag": dataminflag.flag
        })
    dataminflag.flag = 1
    result = bishijieclient()
    datamin = DATAMIN.query.filter(DATAMIN.id==1).first()
    datamin.type = result.get("info")
    datamin.datalist = result.get("info_list")
    db.session.commit()
    return jsonify({
        "result": "success update",
        "message": datamin.datalist,
        "flag": dataminflag.flag
    })

@csrf_protect.exempt
@datamin_bp.route("/eth/contractaddress", methods=["GET"])
def contract_address():
    """获取合约状态

       @@@
       ### description
       > finished
       ### args
       None

       ### return
       status code: **200**
       ```json
       {
          "message": "OK",
          "result": "0",
          "status": "1"
        }
       ```
       @@@
    """
    ethclient = etherscanclient(ETHERSCAN_TOKEN, ETHERSCAN_APIKEY)
    result = ethclient.contractaddress()
    return jsonify(result)

@csrf_protect.exempt
@datamin_bp.route("/eth/tokencontractaddress", methods=["GET"])
def token_contract_address():
    """获取tokenbalance状态

       @@@
       ### description
       > finished
       ### args
       None

       ### return
       status code: **200**
       ```json
       {
          "message": "OK",
          "result": "0",
          "status": "1"
        }
       ```
       @@@
    """
    ethclient = etherscanclient(ETHERSCAN_TOKEN, ETHERSCAN_APIKEY)
    result = ethclient.tokencontractaddress()
    return jsonify(result)

@csrf_protect.exempt
@datamin_bp.route("/eth/token_info", methods=["GET"])
def token_info():
    """获取token合约的信息

       @@@
       ### description
       > finished
       ### args
       None

       ### return
       status code: **200**
       ```json
        {
          "info_list": [
            {
              "name": "Price",
              "value": "$0.0772 @ 0.000036 Eth (-1.41%)"
            },
            {
              "name": "Fully_Diluted_Market_Cap",
              "value": "$77,244,000.00"
            },
            {
              "name": "Max_Total_Supply",
              "value": "1,000,000,000"
            },
            {
              "name": "Holders",
              "value": "5,098 ( -0.059%)"
            },
            {
              "name": "Volume_24H",
              "value": "$1,794,841.00"
            },
            {
              "name": "Market_Capitalization",
              "value": "$5,299,380.00"
            },
            {
              "name": "Circulating_Supply",
              "value": "68,605,322.00 ARES"
            }
          ],
          "info_name": "basic information",
          "status": true
        }
       ```
       @@@
    """
    ethclient = etherscanclient(ETHERSCAN_TOKEN, ETHERSCAN_APIKEY)
    result = ethclient.token_info()
    return jsonify(result)

@csrf_protect.exempt
@datamin_bp.route("/eth/token_transfers", methods=["GET"])
def token_transfers():
    """获取tokenbalance状态

       @@@
       ### description
       > error api, todo
       ### args
       None

       ### return
       status code: **200**
       ```json
        {}
       ```
       @@@
    """
    ethclient = etherscanclient(ETHERSCAN_TOKEN, ETHERSCAN_APIKEY)
    result = ethclient.token_transfers()
    return jsonify(result)


import time
import numpy as np
import pybullet as pb
import pybullet_data

class my_robot:
    def __init__(self,base_position_global,final_pos,final_vel,final_acc,c_m,c_d,c_k):
        #all configuration and loading here
        pb.connect(pb.GUI)
        pb.setAdditionalSearchPath(pybullet_data.getDataPath())
        pb.setGravity(0, 0, -9.8)
        pb.loadURDF("plane.urdf")
        path = r"D:\Ivlabs\Manipulator-robotics\urdfs of manipulators\git-repo-cloned\urdf\ur5.urdf"
        self.robotID = pb.loadURDF(path,base_position_global,useFixedBase=True)
        self.endPostition=final_pos #orientation and position both
        self.endVelocity=final_vel
        self.endAcceleration=final_acc
        self.desired_M = c_m*np.eye(6)
        self.desired_D = c_d*np.eye(6)
        self.desired_K = c_k*np.eye(6)
        self.num_joints = pb.getNumJoints(self.robotID)
        self.revolutes=[]
        for i in range(self.num_joints):
            temp = pb.getJointInfo(self.robotID,i)
            if(temp[2]==0):
                self.revolutes.append(i)
    def robotJointstates(self):
        #returns the positions,velocities and forces on all joints as lists
        jointState_temp = pb.getJointStates(self.robotID,self.revolutes)
        jointPositions = [i[0] for i in jointState_temp]
        jointVelocities = [i[1] for i in jointState_temp]
        jointForces = [i[2] for i in jointState_temp]
        return np.array(jointPositions),np.array(jointVelocities),np.array(jointForces)
    def dynamicMatrices(self,joint_pos,joint_vel):
        #returns M,C and G of the dynamic torque equation
        gravity_matrix = np.array(pb.calculateInverseDynamics(self.robotID,joint_pos,[0]*len(self.revolutes),[0]*len(self.revolutes)))
        coriolis_matrix = np.array(pb.calculateInverseDynamics(self.robotID,joint_pos,joint_vel,[0]*len(self.revolutes)))-gravity_matrix
        mass_matrix = np.array(pb.calculateMassMatrix(self.robotID,joint_pos))
        return mass_matrix,coriolis_matrix,gravity_matrix
    def jacobian_and_inverse(self):
        #returns jv and jomega concatenated and their inverse
        jointState_temp = pb.getJointStates(self.robotID,self.revolutes)
        jointPositions = [i[0] for i in jointState_temp]
        jointVelocities = [i[1] for i in jointState_temp]
        pos,vel = jointPositions,jointVelocities
        jv,jo = pb.calculateJacobian(self.robotID,7,[0,0,0],pos,vel,[0,0,0,0,0,0])
        jacobian = np.concatenate((jo,jv),axis=0)
        return jacobian,np.linalg.pinv(jacobian)
    def initialse_robot_joints(self):
        #removes all forces from joints
        zeroVect = np.zeros(6)
        pb.setJointMotorControlArray(self.robotID,self.revolutes,pb.VELOCITY_CONTROL,forces=zeroVect)
#class definition done

arm = my_robot(base_position_global=[0,0,0],final_pos=np.array([0,0,0,0.35,0.6,1.25]),final_vel=[0,0,0,0,0,0],final_acc=[0,0,0],c_m=0.001,c_d=0.02,c_k=0.05)
arm.initialse_robot_joints()

end_pos_e=0
end_vel_e=0
forcex = pb.addUserDebugParameter("force x",0,0.1,0)
forcey = pb.addUserDebugParameter("force y",0,0.1,0)
forcez = pb.addUserDebugParameter("force z",0,0.1,0)

to_run = int(input("Enter 1 to start the simulation: "))
if(to_run):
    while True:
        fx = pb.readUserDebugParameter(forcex)
        fy = pb.readUserDebugParameter(forcey)
        fz = pb.readUserDebugParameter(forcez)
        F_external = np.array([[0],[0],[0],[-fx],[-fy],[-fz]])
        pb.applyExternalForce(arm.robotID, 7, [fx, fy, fz], [0, 0, 0], pb.LINK_FRAME)
        J_g,J_inv = arm.jacobian_and_inverse()
        pos_current,vel_current,_ = arm.robotJointstates()
        end_pos = np.array(pb.getLinkState(arm.robotID,7)[0])
        end_pos = np.pad(end_pos,(3,0),'constant',constant_values=(0,0))
        end_vel = np.dot(J_g,vel_current)
        end_vel_e = arm.endVelocity-end_vel
        end_pos_e=arm.endPostition-end_pos
        m_matrix,c_matrix,g_matrix = arm.dynamicMatrices(list(pos_current),list(vel_current))
        M_x = np.dot(np.dot(np.transpose(J_inv),m_matrix),J_inv)
        temp = np.dot((np.dot(np.linalg.inv(arm.desired_M),M_x)-np.eye(6)),F_external)
        F = np.dot(np.dot(np.linalg.inv(arm.desired_M),M_x),(np.dot(arm.desired_D,end_vel_e)+np.dot(arm.desired_K,end_pos_e))) + np.squeeze(temp)
        torque = g_matrix+np.dot(np.transpose(J_g),F)
        pb.setJointMotorControlArray(arm.robotID, arm.revolutes, pb.TORQUE_CONTROL, forces=list(torque))
        pb.stepSimulation()
        time.sleep(1 / 100)


from __future__ import print_function
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as init
from torch.autograd import Variable
from util import xavier_uniform

class NeuralFingerprint(nn.Module):
	def __init__(self, paras):
		super(NeuralFingerprint, self).__init__()
		self.paras = paras
		self.embedding = nn.Linear(paras.num_feat, paras.F)
		self.hidden = {i: nn.Linear(paras.F, paras.F) for i in range(paras.R)}
		self.output_fp = {i: nn.Linear(paras.F, paras.L) for i in range(paras.R)}
		self.output_cl = nn.Linear(paras.L, paras.num_class)

	def weight_init(self):
		xavier_uniform(self.embedding)
		for _, hidden in self.hidden:
			init.xavier_normal(hidden)
		for _, output in self.output_fp:
			init.xavier_normal(output)
		init.xavier_normal(self.output_cl)

	def forward(self, graph):
		f = Variable(torch.FloatTensor(self.paras.L).zero_(), requires_grad=True)
		embedding = self.embedding(Variable(torch.from_numpy(graph.node_feature), requires_grad=False))
		for L in range(self.paras.R):
			tmp_embedding = []
			# graph.nodes must be a sorted list
			for node in graph.nodes:
				v = embedding[node]
				for neighbour in graph.neighbours[node]:
					v = torch.add(v, embedding[neighbour])
				r = nn.ELU()(self.hidden[L](v))
				tmp_embedding.append(r)
				i = F.softmax(self.output_fp[L](r))
				f = torch.add(f, i)
			embedding = torch.stack(tmp_embedding, 0)
		return F.log_softmax(self.output_cl(f))


import re
from music21 import note
import numpy as np

data = ['A4', 'F4', 'E.G']
# data = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B' ]

keyboard = np.zeros(88)

for d in data:
    n = d.split(".")
    print('Dlugosc', len(n))
    for m in n:
        f = note.Note(m)
        # print(m, int("0x" + f.pitch.pitchClassString, 0), f.octave if f.octave else 0)
        # print(m, int("0x" + f.pitch.pitchClassString, 0), f.octave if f.octave else 0)
        octave = int(f.octave if f.octave else 0)
        mantise = int("0x" + f.pitch.pitchClassString, 0)
        # number = mantise + 12 * octave
        # print(m, number)
        keyboard[mantise] = 1
    print(keyboard)
    keyboard = np.zeros(88)


import aiohttp
import asyncio
from functools import reduce
from PLATER.services.config import config

class BLHelper:
    def __init__(self, bl_url=config.get('bl_url')):
        self.bl_url = bl_url

    @staticmethod
    async def make_request(url):
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                if response.status == 200:
                    return await response.json()
                else:
                    return None

    async def get_most_specific_concept(self, concept_list: list) -> list:
        """
        Given a list of concepts find the most specific set of concepts.
        """
        tasks = []
        for concept in concept_list:
            parent_url = f"{self.bl_url}/bl/{concept}/ancestors"
            tasks.append(BLHelper.make_request(parent_url))
        response = await asyncio.gather(*tasks, return_exceptions=False)
        parents = list(reduce(lambda acc, value: acc + value, filter(lambda x: x, response), []))
        return list(filter(lambda x: x not in parents, concept_list))


#!/usr/bin/env python3

import redis
import json
from collections import defaultdict
from datetime import datetime
import re
from uuid import uuid4

def nano_valid_address(string):
    p = re.compile('^(nano|xrb)_[13]{1}[13456789abcdefghijkmnopqrstuwxyz]{59}$')
    return p.match(string)

def dpow_address(string):
    return string == "nano_1dpowzkw9u6annz4z48aixw6oegeqicpozaajtcnjom3tqa3nwrkgsk6twj7" or string == "xrb_1dpowzkw9u6annz4z48aixw6oegeqicpozaajtcnjom3tqa3nwrkgsk6twj7"

r = redis.StrictRedis(host="localhost", port=6379)

clients = r.smembers("clients")
clients = {c.decode("utf-8") for c in clients}

snapshot = defaultdict(lambda: {"precache": 0, "ondemand": 0, "id": str(uuid4())})
payouts = defaultdict(lambda: {"precache": 0, "ondemand": 0, "id": str(uuid4())})

for client in clients:
    if not nano_valid_address(client):
        print("!Skipping client '{}' as it is an invalid Nano account!\n\n".format(client))
        continue
    if dpow_address(client):
        print("!Skipping '{}' as it is the dpow account!\n\n".format(client))
        continue
    client_info = r.hgetall(f"client:{client}")
    if not client_info:
        continue
    print(client, client_info)

    for work in ('precache', 'ondemand'):
        work_enc = str.encode(work, "utf-8")
        if work_enc in client_info:
            amount = int(client_info[work_enc])
            snapshot_enc = str.encode("snapshot_"+work, "utf-8")
            snapshot_amount = int(client_info.get(snapshot_enc, 0))
            payout_amount = amount - snapshot_amount
            if payout_amount < 50:
                print(f"Skipping {work}, only did {payout_amount} since last snapshot.")
                continue
            snapshot[client][work] += snapshot_amount
            payouts[client][work] += payout_amount
            r.hset(f"client:{client}", snapshot_enc.decode("utf-8"), amount)
    print("\n")

now = f"{datetime.now():%Y-%m-%d_%H-%M-%S}"

payouts = dict(payouts)
with open(f"payouts_{now}.json", "w") as f:
    json.dump(payouts, f, indent=4)

snapshot = dict(snapshot)
with open(f"snapshot_{now}.json", "w") as f:
    json.dump(snapshot, f, indent=4)

print(f"Updated DB and wrote to files with timestamp {now}")


from django.shortcuts import render, redirect
from django.http import HttpResponse
from .forms import RecipeForm, IngredientForm
from django.views.generic import DetailView
from .models import Recipe, Ingredient, Association_Ingredient_Recipes
import re

def index(request):
    recipes = Recipe.objects.all()
    return render(request, 'books/index.html', {'recipes':recipes})

class RecipesDetailView(DetailView):
    model = Recipe
    template_name = 'books/details_view.html'
    context_object_name = 'recipes'

def create(request):
    error = ''
    if request.method == 'POST':
        form = RecipeForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('recipe_list')
        else:
            error = 'Incorrect form'

    form = RecipeForm()
    data = {
        'form': form,
        'error': error,
    }
    return render(request, 'books/create.html', data)
# Create your views here.

def all_recipes(request):
    if request.method == "POST":
        searched_dishes = request.POST['recipe_or_name'].lower()
        searched_ingredients = re.split("[\s,\\+]*[\\+,\s,\\,]",request.POST['ingredients'].lower())
        searched_objects = Recipe.objects.all()
        searched = 0
        if searched_dishes:
            searched_objects = Recipe.objects.filter(name__contains= searched_dishes)
        searched = len(searched_ingredients)

        for word in searched_ingredients:
            if word == '':
                continue
            searched_objects = searched_objects.filter(ingredients__name__contains=word)
    else:
        searched_dishes = ""
        searched_ingredients = [""]
        searched_objects = Recipe.objects.all()
        searched = 1

    return render(request, 'books/recipes.html', {'recipe_list':searched_objects,
                                                  'searched_dishes':searched_dishes,
                                                  'searched_ingredients': searched_ingredients,
                                                  'searched':searched,
                                                  })


import logging
import os

from aiohttp import ClientError, ClientResponseError
from asyncpg import exceptions
from discord import File, Embed
from discord.ext import commands
from discord.ext.commands import (
    BadArgument,
    NoPrivateMessage,
    CommandError,
    Context,
)
from django.conf import settings

import validators

from dangobot.core.bot import command_handler, DangoBot
from dangobot.core.plugin import Cog
from dangobot.core.helpers import download_file, FileTooLarge

from .models import file_path
from .data import ParsedCommand
from .repository import CommandRepository

logger = logging.getLogger(__name__)

class Commands(Cog):
    """A plugin for configuring custom user-made bot commands."""

    @command_handler
    async def handle_command(self, ctx: Context) -> bool:
        """
        The command handler.

        Handles all incoming command invocations, and checks if there's
        a custom command defined with the given name. If it exists, its
        contents are pulled from the database, and sent to the target channel.

        Arguments:
            message (string): the incoming message
        """
        if not ctx.guild:  # we don't want commands to work in DMs
            return False

        if not ctx.invoked_with:
            return False

        command = await CommandRepository().find_by_trigger(
            ctx.invoked_with, ctx.guild
        )

        if command:
            await self.send_response(ctx, command)
            return True

        return False

    async def send_response(self, ctx: Context, command) -> None:
        """Sends a response for a given custom command database record."""
        params = {"content": command["response"]}

        if command["file"] == "":
            await ctx.send(**params)
        else:
            # TODO: actual asynchronous file read
            with open(
                os.path.join(settings.MEDIA_ROOT, command["file"]), "rb"
            ) as file:
                params["file"] = File(file, command["original_file_name"])
                await ctx.send(**params)

    async def parse_command(self, ctx: Context, *args) -> ParsedCommand:
        """
        Parse the command syntax used for the :func:`add` and :func:`edit`
        functions.

        The format of the command is as follows:
        * the first word is the command trigger,
        * rest of the string is treated as the command response, until the last
          word starts with ``attachment=``,
        * if so, then the last word specifies a file attachment that will be a
          part of the command.

        The attachment is provided as an ``attachment=<url>`` parameter at the
        end of the command. The file under the URL is then downloaded and saved
        in the media location.

        This means that the command response can have a length of zero if you
        only specify the trigger and the attachment.

        Keep in mind that if there is an attachment provided as a part of the
        message (as in: uploaded in Discord, not as as an URL posted in the
        message body), then it will take precedence over the attachment posted
        in the command.

        Returns a four-element tuple where:
        * the first element is a command trigger,
        * the second element is the command response,
        * the third element is a path to an eventual attachment (relative to
          the media directory), if there's no attachment then it is an empty
          string,
        * the fourth element is the orignal file name of the attachment, or
          an empty string if there's no attachment.
        """
        if len(args) < 2 and len(ctx.message.attachments) < 1:
            raise BadArgument("No message content specified!")

        url = ""
        path_relative = ""
        filename = ""

        if args[-1].startswith("attachment="):
            url = args[-1].replace("attachment=", "", 1)

            if not validators.url(url):
                raise commands.BadArgument(
                    message="The provided URL is incorrect!"
                )

            args = args[:-1]

        if len(ctx.message.attachments) > 0:
            url = ctx.message.attachments[0].url

        if url != "":
            filename = url.split("/")[-1]
            # relative path is being saved to the database
            path_relative = file_path(ctx, filename)
            path_absolute = f"{settings.MEDIA_ROOT}/{path_relative}"

            try:
                await download_file(self.bot.http_session, url, path_absolute)
            except ClientError as exc:
                if isinstance(exc, ClientResponseError) and exc.status == 404:
                    raise CommandError(
                        "The requested file was not found!"
                    ) from exc

                logger.error(
                    "An error occured while downloading file %s to %s.",
                    url,
                    path_absolute,
                    exc_info=True,
                )

                raise exc
            except FileTooLarge as exc:
                raise CommandError(str(exc)) from exc

        trigger = args[0]
        response = " ".join(args[1:])

        return ParsedCommand(trigger, response, path_relative, filename)

    @commands.group(name="commands", invoke_without_command=True)
    async def cmds(self, ctx):
        """
        Management of user-defined commands.
        """
        await ctx.send_help("commands")

    @cmds.command(usage="<trigger> (<response>) (attachment=url)")
    @commands.has_permissions(administrator=True)
    async def add(self, ctx: Context, *args):
        """
        Add a new command.

        Parameters:

          * trigger: the string that will trigger executing this command.
          * response: the string that will be sent by this command.\
            Can be empty.
          * url: URL to a file that will be sent by this command.\
            Can be omitted.

        Executing the command without specifying either RESPONSE or ATTACHMENT
        will result in an error. You can also upload an attachment to Discord
        instead of specifying the URL.
        """
        if ctx.guild is None:
            raise NoPrivateMessage("This command cannot be used in a DM")

        command = await self.parse_command(ctx, *args)

        try:
            await CommandRepository().add_to_guild(ctx.guild, command)
        except exceptions.UniqueViolationError as exc:
            raise BadArgument(
                f"Command `{ctx.args[-1]}` already exists!"
            ) from exc

        await ctx.send(f"Command `{command.trigger}` added successfully!")

    @cmds.command()
    async def list(self, ctx: Context):
        """
        List all commands defined in the server.
        """
        if ctx.guild is None:
            raise NoPrivateMessage("This command cannot be used in a DM")

        command_list = await CommandRepository().find_all_from_guild(ctx.guild)

        embed = Embed()
        embed.title = "Available custom commands:"
        embed.description = "\n".join(
            map(
                lambda c: f"{ctx.prefix}{c['trigger']}",
                command_list,
            )
        )

        await ctx.send(embed=embed)

    @cmds.command()
    @commands.has_permissions(administrator=True)
    async def delete(self, ctx: Context, trigger: str):
        """
        Deletes a command from the server.
        """
        if ctx.guild is None:
            raise NoPrivateMessage("This command cannot be used in a DM")

        deleted = await CommandRepository().delete_from_guild(
            trigger, ctx.guild
        )

        if deleted:
            message = "Command `{}` deleted successfully!"
        else:
            message = "Command `{}` does not exist!"

        await ctx.send(content=message.format(trigger))

    @cmds.command(usage="<trigger> (<response>) (attachment=url)")
    @commands.has_permissions(administrator=True)
    async def edit(self, ctx: Context, *args):
        """
        Edit an existing command.

        Has the same parameters as the add command.
        """
        if ctx.guild is None:
            raise NoPrivateMessage("This command cannot be used in a DM")

        command = await self.parse_command(ctx, *args)

        updated = await CommandRepository().update_in_guild(ctx.guild, command)

        if updated:
            message = "Command `{}` updated successfully!"
        else:
            message = "Command `{}` does not exist!"

        await ctx.send(content=message.format(command.trigger))

async def setup(bot: DangoBot):  # pylint: disable=missing-function-docstring
    await bot.add_cog(Commands(bot))


import numpy as np
import sys,os
import cv2
caffe_root = '/home/wxx/develop/caffe-ssd/'
sys.path.insert(0, caffe_root + 'python')
import caffe
import shutil
import time

caffe.set_mode_gpu()

net_file= '../example/MobileNetSSD_deploy.prototxt'
caffe_model='../snapshot/r/mobilenet_iter_28000.caffemodel'
test_dir = "images"

if not os.path.exists(caffe_model):
    print(caffe_model + " does not exist")
    exit()
if not os.path.exists(net_file):
    print(net_file + " does not exist")
    exit()
net = caffe.Net(net_file,caffe_model,caffe.TEST)

CLASSES = ('background','person','test')

def preprocess(src):
    img = cv2.resize(src, (300,300))
    img = img - 127.5
    img = img * 0.007843
    return img

def postprocess(img, out):
    h = img.shape[0]
    w = img.shape[1]
    box = out['detection_out'][0,0,:,3:7] * np.array([w, h, w, h])

    cls = out['detection_out'][0,0,:,1]
    conf = out['detection_out'][0,0,:,2]
    return (box.astype(np.int32), conf, cls)

def detect(origimg):
    result = 0
    img = preprocess(origimg)

    img = img.astype(np.float32)
    img = img.transpose((2, 0, 1))

    net.blobs['data'].data[...] = img
    out = net.forward()
    start_time = time.time()
    box, conf, cls = postprocess(origimg, out)
    # if len(box) <= 0:
    #     return -5
    for i in range(len(box)):
        p1 = (box[i][0], box[i][1])
        p2 = (box[i][2], box[i][3])
        print (p1)
        print (p2)
        # if box[i][0] <=0 or box[i][1] <= 0 or box[i][2] <= 0 or box[i][3] <= 0:
        #     return -4
        # site =
        if int(cls[i]) == 1:
            cv2.rectangle(origimg, p1, p2, (0,255,255))
        elif int(cls[i]) > 1:
            break
        else:
            cv2.rectangle(origimg, p1, p2, (255,0,0))
        p3 = (max(p1[0], 15), max(p1[1], 15))
        title = "%s:%.2f:%d" % (CLASSES[int(cls[i])], conf[i],i)
        cv2.putText(origimg, title, p3, cv2.FONT_ITALIC, 0.6, (255, 0, 0), 1)
        if int(cls[i]) == 1:
            result = 1

    cv2.imshow("SSD", origimg)
    k = cv2.waitKey(1) & 0xff

    # k = cv2.waitKey(0) & 0xff
        #Exit if ESC pressed
    if k == 27 : result = -1
    return result

######## video  ##############
if __name__ == '__main__':
    cap = cv2.VideoCapture('/home/wxx/develop/demo4/video/walk2/test39.mp4')
    while(1):
        ret, img = cap.read()
        detect(img)

######## img  ##############
# if __name__ == '__main__':
#     img = cv2.imread("/home/wxx/testimg/02563.jpg")
#     witch = detect(img)

######## photo_test ##############
# if __name__ == '__main__':
#     imgpath = '/home/wxx/testimg/'
#     witch = 0
#     for i1 in range(0,9):
#         if witch == -1:
#             break
#         for i2 in range(0,9):
#             if witch == -1:
#                 break
#             for i3 in range(0,9):
#                 if witch == -1:
#                     break
#                 for i4 in range(0,9):
#                     imgname = '0' + str(i1) + str(i2) + str(i3) + str(i4) + '.jpg'
#                     imgfile = imgpath + imgname
#                     img = cv2.imread(imgfile)
#                     try:
#                         img.shape
#                         witch = detect(img)
#                     except:
#                         print('fail to read '+imgfile)
#                         continue

######## photo_result  ##############
# if __name__ == '__main__':
#     imgpath = '/home/wxx/img/person_data/JPEGImages/'
#     witch = 0
#     width = 1080
#     height = 720
#     sz = (width, height)
#     fps = 1
#     fourcc = cv2.VideoWriter_fourcc(*'XVID')
#     vout = cv2.VideoWriter('output.avi', fourcc, 20.0, sz, True)
#     back_img = np.zeros((720, 1080, 3), dtype=np.uint8)
#     # vout.open()
#     for i1 in range(0,9):
#         if witch == -1:
#             break
#         for i2 in range(0,9):
#             if witch == -1:
#                 break
#             for i3 in range(0,9):
#                 if witch == -1:
#                     break
#                 for i4 in range(0,9):
#                     imgname = '0' + str(i1) + str(i2) + str(i3) + str(i4) + '.jpg'
#                     imgfile = imgpath + imgname
#                     img = cv2.imread(imgfile)
#                     try:
#                         img.shape
#                         witch = detect(img)
#                         # back_img.fill(0)
#                         # for w in range(img.shape[1]):
#                         #     for h in range(img.shape[0]):
#                         #         back_img[h, w] = img[h, w]
#                         # vout.write(back_img)
#                         # pirnt('running')
#                         cv2.imwrite('result/'+imgname, img)
#                     except:
#                         print('fail to read '+imgfile)
#                         continue
#     vout.release()

######## copy  ##############
# if __name__ == '__main__':
#     imgpath = '/media/wxx/系统公用/add_data/'
#     witch = 0
#     print ('ok')
#     for i1 in range(0,9):
#         if witch == -1:
#             break
#         for i2 in range(0,9):
#             if witch == -1:
#                 break
#             for i3 in range(0,9):
#                 if witch == -1:
#                     break
#                 for i4 in range(0,9):
#                     imgname = '0' + str(i1) + str(i2) + str(i3) + str(i4) + '.jpg'

#                     imgfile = imgpath + imgname
#                     img = cv2.imread(imgfile)
#                     try:
#                         img.shape
#                         witch = detect(img)
#                         if witch == -4 or witch == -5:
#                             newname = '/home/wxx/testimg/' + imgname
#                             shutil.copyfile(imgfile,newname)
#                         elif witch == -1:
#                             break
#                     except:
#                         print('fail to read '+imgfile)
#                         continue


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

def isLoggedIn(driver):
    driver.get("http://www.evanfalkenstine.net/")
    time.sleep(1)
    header = driver.find_element_by_class_name("top-menu")
    if  header.text.find("Logout") == -1: # does not contain logout
        res = False
    else:
        res = True
    time.sleep(1)
    return res

def logIn(driver):
    if isLoggedIn(driver):
        return True

    driver.get("http://www.evanfalkenstine.net/login")
    userEmail = driver.find_element_by_id("identifierId")
    userEmail.send_keys("")
    driver.find_element_by_id("identifierNext").click()

    userPW = driver.find_element_by_id("password")
    userPW = userPW.find_element_by_name("password")
    userPW.send_keys("")
    userPW.send_keys(Keys.RETURN)
    try:
        element = WebDriverWait(driver, 10).until(
            EC.title_is("Item Catalog")
        )
    finally:
        time.sleep(1)
        return isLoggedIn(driver)

def addCategory(driver, category_name):
    if isLoggedIn(driver) == False:
        return False
    driver.get("http://www.evanfalkenstine.net/item_catalog/categories/add")
    inputName = driver.find_element_by_id("catName")
    inputName.send_keys(category_name)
    time.sleep(1)
    inputName.submit()
    time.sleep(1)
    return True

def categoryExists(driver, category_name):
    driver.get("http://www.evanfalkenstine.net/item_catalog/")
    all_a_tags = driver.find_elements_by_tag_name("a")
    for anchor in all_a_tags:
        if anchor.text == category_name:
            time.sleep(1)
            return True
    time.sleep(1)
    return False

def deleteCategory(driver, category_name):
    if isLoggedIn(driver) == False:
        time.sleep(1)
        return False

    driver.get("http://www.evanfalkenstine.net/item_catalog/")
    all_a_tags = driver.find_elements_by_tag_name("a")
    for anchor in all_a_tags:
        if anchor.text == category_name:
            collapse_name = anchor.get_attribute("href")[45:]
            anchor.click()
            break

    card_body = driver.find_element_by_id(collapse_name)
    links = card_body.find_elements_by_tag_name("a")
    for link in links:
        if link.text == "Delete":
            link.click()
            break
    time.sleep(1)
    driver.find_element_by_tag_name("input").click()
    time.sleep(1)
    return True

if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.implicitly_wait(5)
    example_name = "Job Interview Supplies"

    if logIn(driver):
       print("Successfully Logged In")
    else:
       print("Login Failed")

    if categoryExists(driver, example_name):
        print("Category Found")
    else:
        print("Category Not Found")

    if addCategory(driver, example_name):
        print("Category Added")
    else:
        print("Failed to Add Category")

    if categoryExists(driver, example_name):
        print("Category Found")
    else:
        print("Category Not Found")

    if deleteCategory(driver, example_name):
        print("Category Deleted")
    else:
        print("Failed to Delete Category")

    if categoryExists(driver, example_name):
        print("Category Found")
    else:
        print("Category Not Found")


import FetchService
from pathlib import Path

'''
This class enables the download and handle of COVID-19 epidemiological data provides by the german RKI Institut (www.RKI.de)
All data is gathereed into a single csv file, which makes the file very large (> 1.8million columns)

RKI.fetch allows the download of the whole file, saved in a seperated folder (./data/rki)

'''

def fetch():
    url = "https://www.arcgis.com/sharing/rest/content/items/f10774f1c63e40168479a1feb6c7ca74/data"
    directory = Path("./data/rki/")
    print("this will need a while...")
    FetchService.save_csv(directory, url)
    print("Finished!")


import pytesseract
import time
import os
import pyautogui

from utils import settings, functions, scrape, exporter

import datetime

# Flags (And defaults)
f_export = "xlsx"

try:
	# Focus and maximize the NOX window
	# Somewhat buggy since NOX does not use native Windows forms
	if functions.focusnox() == False:
		print("Could not detect Nox... Aborting!")
		exit(1)
	time.sleep(1)
	functions.click(settings.points['focus'])

	# Confirm we are on the right screen
	print("Checking if we are on the \"Nearby\" screen...")
	if functions.confirmpixels(settings.pixelgroups['nearby']) == False:
		print("Not on correct screen inside Nox... Aborting!")
		exit(1)
	else:
		print("Seems OK, proceeding...")

	# Extract current coordinates
	print("Looking for current GPS coordinates...")
	coords = functions.getGPSCoords()
	if coords == False:
		print("Could not extract GPS location... Aborting!")
		exit(1)
	else:
		print("Found location: {}".format(coords))
		time.sleep(1)

	# Close GPS window
	functions.click(settings.points['close'])
	time.sleep(2)

	# I didn't put my tesseract in my PATH properly, too lazy to fix :)
	# If you one of those people as well, feel free to use this in from your config
	if len(settings.settings['tesseract']) > 0:
		pytesseract.pytesseract.tesseract_cmd = settings.settings['tesseract']

	# Align screen for start
	functions.firstalign()

	# See if we have to unfold user list (> 5 users) and settle out
	scrape.expandLists()
	time.sleep(1)

	# Runtimeeeee!
	raw = scrape.scrapeEntries()

	# Export
	# Should actually make this into a proper jumptable but whatever
	filename = "results/result_{}".format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))

	# XLSX
	if f_export == "xlsx":
		filename += ".xlsx"
		exporter.exportDistancesXLSX(raw, coords, filename=filename)

	# JSON
	elif f_export == "json":
		filename += ".json"
		exporter.exportDistancesJSON(raw, coords, filename=filename)

# Using the PyAutoGUI failsafe
except pyautogui.FailSafeException:
	print("FAILSAFE TRIGGERED, ABORTING!")
	exit(2)

# Using the PyAutoGUI failsafe
except KeyboardInterrupt:
	print("User interrupt, aborting!")
	exit(1)

# Tests I want to save untill I am ready to use them
exit()

# Countdown and move spoofed GPS to coords
for i in range(3, 0, -1):
	print("{}...".format(i))
	time.sleep(1)

for k in settings.coords.keys():
	functions.setGeo(settings.coords[k])
	time.sleep(1)


from lxml import html
import requests
import pyfiglet
from prettytable import PrettyTable

try:
    f = pyfiglet.Figlet(font='larry3d')

    page = requests.get("https://www.theguardian.com/football/premierleague/table")
    tree = html.fromstring(page.content)

    clubs = tree.xpath("//a[@class='team-name__long']/text()")
    points = tree.xpath("//tr/td[10]/b[1]/text()")
    goal_difference = tree.xpath("//td[9]/text()")
    goals_for = tree.xpath("//td[7]/text()")
    goals_against = tree.xpath("//td[8]/text()")

    x = PrettyTable()

    column_names = ["Club", "Points", "Goal Difference", "Goals for", "Goals against"]

    x.add_column(column_names[0], clubs)
    x.add_column(column_names[1], points)
    x.add_column(column_names[2], goal_difference)
    x.add_column(column_names[3], goals_for)
    x.add_column(column_names[4], goals_against)

    print(f.renderText('Premier league'))

    print(x)
    print("")
    print(
        "The data provided is gathered from the Guardian website, https://www.theguardian.com/football/premierleague/table")
except:
    print("An error occured")


import random
from nas_lib.nas_201_api.genotypes import Structure as CellStructure
from copy import deepcopy
import numpy as np
from nas_lib.data.cell_nasbench201 import Cell
import torch
import pickle
from nas_lib.configs import nas_bench_201_converted_base_path

NUM_VERTICES = 8
OPS = ['input', 'none', 'skip_connect', 'nor_conv_1x1', 'nor_conv_3x3', 'avg_pool_3x3', 'isolate', 'output']
OPS_GRAPH = ['input', 'skip_connect', 'nor_conv_1x1', 'nor_conv_3x3', 'avg_pool_3x3', 'isolate', 'output']
OPS_LIST = ['avg_pool_3x3', 'nor_conv_1x1', 'nor_conv_3x3', 'none', 'skip_connect']
OP_SPOTS = 6

class NASBench201:
    def __init__(self, dataset):
        self.nas_bench = None
        self.max_nodes = 4
        self.op_names = ['none', 'skip_connect', 'nor_conv_1x1', 'nor_conv_3x3', 'avg_pool_3x3']
        self.op_names_alphaX = ['none', 'skip_connect', 'nor_conv_1x1', 'nor_conv_3x3', 'avg_pool_3x3', 'term']
        del self.nas_bench
        pkl_path = nas_bench_201_converted_base_path % dataset
        with open(pkl_path, 'rb') as f:
            self.total_archs = pickle.load(f)
            self.total_keys = pickle.load(f)

    def random_arch(self):
        genotypes = []
        for i in range(1, self.max_nodes):
            xlist = []
            for j in range(i):
                node_str = '{:}<-{:}'.format(i, j)
                op_name = random.choice(self.op_names)
                xlist.append((op_name, j))
            genotypes.append(tuple(xlist))
        return CellStructure(genotypes)

    def mutate(self, parent_arch):
        while True:
            child_arch = deepcopy(parent_arch)
            node_id = random.randint(0, len(child_arch.nodes) - 1)
            node_info = list(child_arch.nodes[node_id])
            snode_id = random.randint(0, len(node_info) - 1)
            xop = random.choice(self.op_names)
            while xop == node_info[snode_id][0]:
                xop = random.choice(self.op_names)
            node_info[snode_id] = (xop, node_info[snode_id][1])
            child_arch.nodes[node_id] = tuple(node_info)
            str_arch = child_arch.tostr()
            if str_arch in self.total_keys:
                break
        return child_arch, self.total_archs[str_arch]

    def get_candidates(self,
                       data,
                       num=100,
                       allow_isomorphisms=False,
                       patience_factor=5,
                       num_best_arches=10,
                       return_dist=False):
        """
        Creates a set of candidate architectures with mutated and/or random architectures
        """
        # test for isomorphisms using a hash map of path indices
        candidates = []
        dic = {}
        dist_list = []
        nums_list = []
        mutated_archs_list = []
        for d in data:
            arch = {'matrix': d[0][0], 'ops': d[0][1]}
            path_indices = Cell(**arch).get_path_indices()
            dic[path_indices] = 1

        best_arches = [CellStructure.str2structure(arch[6])
                       for arch in sorted(data, key=lambda i: i[4])[:num_best_arches * patience_factor]]
        best_arches_full = [arch for arch in sorted(data, key=lambda i: i[4])[:num_best_arches * patience_factor]]
        # stop when candidates is size num
        # use patience_factor instead of a while loop to avoid long or infinite runtime
        for idx, arch in enumerate(best_arches):
            if len(candidates) >= num:
                break
            nums = 0
            for i in range(num):
                _, mutated = self.mutate(arch)
                path_indices = Cell(matrix=mutated[0][0], ops=mutated[0][1]).get_path_indices()

                if allow_isomorphisms or path_indices not in dic:
                    dic[path_indices] = 1
                    candidates.append(mutated)
                    dist = adj_distance(arch.tostr(), mutated[6])
                    dist_list.append(dist)
                    nums += 1
            nums_list.append(nums)
            mutated_archs_list.append(best_arches_full[idx])
        if return_dist:
            return candidates[:num], dist_list[:num], 0, nums_list, mutated_archs_list
        else:
            return candidates[:num]

    def generate_random_dataset(self, num=10,
                                allow_isomorphisms=False,
                                deterministic_loss=False):
        data = []
        dic = {}
        key_list = []
        while True:
            k = random.sample(self.total_keys, 1)
            key_list.append(k[0])
            arch = self.total_archs[k[0]]
            cell_arch = Cell(matrix=arch[0][0], ops=arch[0][1], isolate_node_idxs=[])
            path_encoding1 = cell_arch.encode_paths()
            path_encoding2 = cell_arch.encode_cell2()
            path_indices = cell_arch.get_path_indices()
            if allow_isomorphisms or path_indices not in dic:
                dic[path_indices] = 1
                data.append(
                    (
                        arch[0],
                        arch[1],
                        arch[2],
                        path_encoding1,
                        arch[4],
                        arch[5],
                        arch[6],
                        path_encoding2
                    )
                )
            if len(data) == num:
                break
        return data

    def nasbench2graph2(self, data):
        matrix, ops = data[0], data[1]
        node_feature = torch.zeros(NUM_VERTICES, 8)
        edges = int(np.sum(matrix))
        edge_idx = torch.zeros(2, edges)
        counter = 0
        for i in range(NUM_VERTICES):
            idx = OPS.index(ops[i])
            node_feature[i, idx] = 1
            for j in range(NUM_VERTICES):
                if matrix[i, j] == 1:
                    edge_idx[0, counter] = i
                    edge_idx[1, counter] = j
                    counter += 1
        return edge_idx, node_feature

    def get_arch_list(self,
                      aux_file_path,
                      distance=None,
                      iteridx=0,
                      num_top_arches=5,
                      max_edits=20,
                      num_repeats=5,
                      random_encoding='adj',
                      verbose=0):
        # Method used for gp_bayesopt

        # load the list of architectures chosen by bayesopt so far
        base_arch_list = pickle.load(open(aux_file_path, 'rb'))
        top_arches = [archtuple[0] for archtuple in base_arch_list[:num_top_arches]]
        if verbose:
            top_5_loss = [archtuple[1][0] for archtuple in base_arch_list[:min(5, len(base_arch_list))]]
            print('top 5 val losses {}'.format(top_5_loss))

        # perturb the best k architectures
        dic = {}
        for archtuple in base_arch_list:
            arch = {'matrix': self.total_archs[archtuple[0]['string']][0][0],
                    'ops': self.total_archs[archtuple[0]['string']][0][1]}
            path_indices = Cell(**arch).get_path_indices()
            dic[path_indices] = 1

        new_arch_list = []
        for arch in top_arches:
            for edits in range(1, max_edits):
                for _ in range(num_repeats):
                    #perturbation = Cell(**arch).perturb(self.nasbench, edits)
                    perturbation = self.mutate_edit(arch['string'], mutation_rate=edits)
                    perturbation_arch = self.total_archs[perturbation][0]
                    path_indices = Cell(matrix=perturbation_arch[0], ops=perturbation_arch[1]).get_path_indices()
                    if path_indices not in dic:
                        dic[path_indices] = 1
                        new_arch_list.append({'string': perturbation})

        # make sure new_arch_list is not empty
        while len(new_arch_list) == 0:
            for _ in range(100):
                random_arch_str = self.random_arch().tostr()
                random_arch = self.total_archs[random_arch_str][0]
                path_indices = Cell(matrix=random_arch[0], ops=random_arch[1]).get_path_indices()
                if path_indices not in dic:
                    dic[path_indices] = 1
                    new_arch_list.append({'string': random_arch_str})

        return new_arch_list

    def mutate_edit(self, arch, mutation_rate=1.0):
        """
        code modified from project https://github.com/naszilla/naszilla
        """
        ops = get_op_list(arch)
        new_ops = []
        # keeping mutation_prob consistent with nasbench_101
        mutation_prob = mutation_rate / (OP_SPOTS - 2)

        for i, op in enumerate(ops):
            if random.random() < mutation_prob:
                available = [o for o in OPS_LIST if o != op]
                new_ops.append(random.choice(available))
            else:
                new_ops.append(op)
        return get_string_from_ops(new_ops)

    @classmethod
    def generate_distance_matrix(cls, arches_1, arches_2, distance):
        matrix = np.zeros([len(arches_1), len(arches_2)])
        for i, arch_1 in enumerate(arches_1):
            for j, arch_2 in enumerate(arches_2):
                if distance == 'adj':
                    matrix[i][j] = adj_distance(arch_1['string'], arch_2['string'])
                elif distance == 'nasbot':
                    matrix[i][j] = nasbot_distance(arch_1['string'], arch_2['string'])
                else:
                    raise ValueError(f'Distance {distance} does not support at present!')
        return matrix

def get_op_list(arch):
    """
    code modified from project https://github.com/naszilla/naszilla
    """
    # given a string, get the list of operations
    tokens = arch.split('|')
    ops = [t.split('~')[0] for i,t in enumerate(tokens) if i not in [0,2,5,9]]
    return ops

def get_string_from_ops(ops):
    """
    code modified from project https://github.com/naszilla/naszilla
    """
    # given a list of operations, get the string
    strings = ['|']
    nodes = [0, 0, 1, 0, 1, 2]
    for i, op in enumerate(ops):
        strings.append(op+'~{}|'.format(nodes[i]))
        if i < len(nodes) - 1 and nodes[i+1] == 0:
            strings.append('+|')
    return ''.join(strings)

def adj_distance(cell_1, cell_2):
    cell_1_ops = get_op_list(cell_1)
    cell_2_ops = get_op_list(cell_2)
    return np.sum([1 for i in range(len(cell_1_ops)) if cell_1_ops[i] != cell_2_ops[i]])

def nasbot_distance(cell_1, cell_2):
    # distance based on optimal transport between row sums, column sums, and ops

    cell_1_ops = get_op_list(cell_1)
    cell_2_ops = get_op_list(cell_2)

    cell_1_counts = [cell_1_ops.count(op) for op in OPS]
    cell_2_counts = [cell_2_ops.count(op) for op in OPS]
    ops_dist = np.sum(np.abs(np.subtract(cell_1_counts, cell_2_counts)))

    return ops_dist + adj_distance(cell_1, cell_2)

if __name__ == '__main__':
    search_space = NASBench201(dataset='cifar10-valid')
    # search_space = NASBench201(dataset='cifar100')
    # search_space = NASBench201(dataset='ImageNet16-120')
    total_val_list = []
    total_test_list = []
    for key in search_space.total_keys:
        total_val_list.append(search_space.total_archs[key][4])
        total_test_list.append(search_space.total_archs[key][5])
    total_val = np.array(total_val_list)
    total_test = np.array(total_test_list)
    idx = np.argmin(total_val)
    print(total_val[idx], total_test[idx], min(total_test_list))


from django.urls import path
from Accounts import views
app_name= 'accounts'

urlpatterns = [
    path('user_login/',views.userLogin,name='userLogin'),
    path('user_logout/',views.userLogout,name="userLogout"),
    path('user_profile/',views.userProfile,name="userProfile"),
    path('register/',views.RegisterUser,name='registerUser'),
    path('user_update/<pk>/',views.UserUpdate,name='updateProfile'),
]