'''
This script creates a dataframe of all data collected by this project
about the National Park Service Parks/Units. As of May 1, 2019, the NPS
manages 419 official units and 170 Related Areas. This project deals
only with the 419 official units.

1) Read the data available from the api. This source provides the four
   character code that uniquely identifies each park, the list of state
   codes the park is in, and the latitude and longitiude of the park.
2) Create the master dataframe by merging the api data with the list of
   official park units available on nps.gov. This source provides the
   official list of units, the park name, and park designation.
3) Read the established date for each National Park, available on a
   Wikipedia page, and merge with the master dataframe.
4) Read the park size data, available from nps.gov as a report, and
   merge with the master dataframe.
5) Read the park visitor use statistics, available from nps.gov as a
   report, and merge with the master dataframe.

Required Libraries
------------------
pandas, SequenceMatcher

Dependencies
------------
1) Run the script, nps_get_park_sites_web.py, to create the file,
   'nps_park_sites_web.xlsx'.
2) Run the script, nps_get_park_sites_api.py, to create the file,
   'nps_park_sites_api.xlsx'.
3) Run the script, nps_get_wikipedia_data.py, to create the file,
   'wikipedia_date_established.csv'.
4) Download the most recent acreage report from the nps website at:
   https://www.nps.gov/subjects/lwcf/acreagereports.htm Calendar
   Year Reports, Year = 2018. Place this file in the '_acreage_data'
   directory of this project.
5) Run the script, nps_read_visitor_data.py to create the file:
   annual_visitors_by_park_1904_2018.xlsx.
'''

import pandas as pd
from difflib import SequenceMatcher

def strip_park_name(park_name):
    '''
    Park names for the same park may be slightly different between
    sources, making matching them difficult. This function strips
    some designations from the park name to improve the chance of
    matching.

    Parameters
    ----------
    park_name : str
        Park name to strip of designations.

    Returns
    -------
    park_name : str
        Stripped park name.
    '''

    park_name = (park_name.replace("National Monument & Preserve", "")
                          .replace("National Park & Preserve", "")
                          .replace("National and State Parks", "")
                          .replace("National Monument", "")
                          .replace("National Park", "")
                          .replace("National Preserve", "")
                          .replace("NATIONAL PRESERVE", ""))
    if park_name.endswith('NP'):
        park_name = park_name.replace(" NP", "")

    return park_name.rstrip()

def read_park_sites_api():
    '''
    Read the list of park sites and associated data pulled from the
    NPS API and saved in an Excel file by the nps_get_park_sites_api.py
    script into a dataframe.

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

    Returns
    -------
    df : pandas DataFrame
        Dataframe of park site data.
    '''

    filename = '_reference_data/nps_park_sites_api.xlsx'
    df = pd.read_excel(filename, header=0)

    # Exclude park codes that are not in the National Park System list
    # of 419 Units/Parks.
    exclude_park_codes = ['afam', 'alka', 'anch', 'alca', 'aleu', 'amme',
                          'anac', 'armo', 'attr', 'auca', 'balt', 'bawa',
                          'blrn', 'cali', 'crha', 'came', 'cahi', 'cajo',
                          'chva', 'cbpo', 'cbgn', 'cwdw', 'coal', 'colt',
                          'xrds', 'dabe', 'dele', 'elte', 'elca', 'elis',
                          'erie', 'esse', 'fati', 'fodu', 'fofo', 'glec',
                          'glde', 'grfa', 'grsp', 'guge', 'haha', 'jame',
                          'hurv', 'inup', 'iafl', 'iatr', 'blac', 'jthg',
                          'juba', 'keaq', 'klse', 'lecl', 'lode', 'loea',
                          'maac', 'mide', 'migu', 'mihi', 'mopi', 'auto',
                          'mush', 'avia', 'npnh', 'neen', 'pine', 'nifa',
                          'noco', 'oire', 'okci', 'olsp', 'oreg', 'ovvi',
                          'oxhi', 'para', 'poex', 'prsf', 'rist', 'roca',
                          'safe', 'scrv', 'semo', 'shvb', 'soca', 'stsp',
                          'tecw', 'qush', 'thco', 'tosy', 'trte', 'waro',
                          'whee', 'wing', 'york']
    df = df[~(df.park_code.isin(exclude_park_codes))]

    # Replace certain park names so that they will be matched correctly
    # with the park names in the official list of 419.
    df['park_name'].replace(
        {"Ford's Theatre":"Ford's Theatre National Historic Site",
        "Pennsylvania Avenue":"Pennsylvania Avenue National Historic Site",
        "Arlington House, The Robert  E. Lee Memorial":"Arlington House",
        "President's Park \(White House\)":"White House",
        "Great Egg Harbor River":
            "Great Egg Harbor National Scenic and Recreational River",
        "Lower Delaware National Wild and Scenic River":
            "Delaware National Scenic River"},
        regex=True, inplace=True)

    df['park_name_stripped'] = df.park_name.apply(
                               lambda x: strip_park_name(x))

    df = df.sort_values(by=['park_name'])

    return df[['park_code', 'park_name', 'park_name_stripped',
               'states', 'lat', 'long']]

def read_park_sites_web(df_api):
    '''
    Read the list of 419 units/parks managed by the National Park
    Service from nps.gov and saved in an Excel file by the
    nps_get_park_sites_web.py script into a dataframe.

    Parameters
    ----------
    df_api : pandas DataFrame
        Dataframe of data from api in which to lookup park code.

    Returns
    -------
    df : pandas DataFrame
        Dataframe of park site data.
    '''

    filename = '_reference_data/nps_park_sites_web.xlsx'
    df = pd.read_excel(filename, header=0)

    # MLK Historic Park park name is ambiguous and makes park name
    # matching impossible, replace with more specific name.
    df.loc[df.park_name == "Martin Luther King", 'park_name'] = "Martin Luther King National Historic Park"

    # Find the park code for each park in the df_master dataframe by
    # matching it to the park in the df_api dataframe.
    df['park_name_stripped'] = df.park_name.apply(
                               lambda x: strip_park_name(x))
    df['park_code'] = df.park_name_stripped.apply(
                      lambda x: lookup_park_code(x, df_api))

    return df

def lookup_park_code(park_name, df_lookup):
    '''
    Each park is assigned a four character park code that uniquely
    identifies it. The park code for each park is available from the
    NPS API. This function accepts a park name and returns the park code
    after looking in up in the API dataframe.

    Parameters
    ----------
    park_name : str
        Park name to lookup the code for.
    df_lookup : pandas DataFrame
        Dataframe of park names and codes.

    Returns
    -------
    park_code : str
        Park code that matches the park name.
    '''

    df = df_lookup

    # Use SequenceMatcher to find the best park name match.
    df['name_match'] = df['park_name_stripped'].apply(
                       lambda x: SequenceMatcher(None, x.lower(),
                       park_name.lower()).ratio())
    park_code = df.loc[df['name_match'].idxmax()].park_code

    # Although Kings Canyon NP and Sequoia NP are separate parks, they
    # are managed together and share the same park code.
    if park_name.lower().find('kings canyon') > -1: park_code = 'seki'
    if park_name.lower().find('sequoia') > -1: park_code = 'seki'

    # Fort Caroline National Memorial is a part of the Timucuan
    # Ecological and Hitoric Preserve (FL). Vistor date and acreage for
    # both should be combined.
    if park_name.lower().find('caroline') > -1: park_code = 'timu'

    # Lake Chelan NRA and Ross Lake NRA are both part of the North
    # Cascades National Park Service Complex. Visitor data and acreage
    # for all three areas should be combined.
    if park_name.lower().find('chelan') > -1: park_code = 'noca'
    if park_name.lower().find('ross lake') > -1: park_code = 'noca'

    # The John D. Rockefeller, Jr. Memorial Parkway is in Grand Teton
    # National Park (WY) and does not have its own park code. Visitor
    # data and acreage should not be combined.
    if park_name.lower().find('john d. rockefeller') > -1: park_code = 'xxx1'

    # World War II Valor in the Pacific National Monument split into
    # three parks in 3/2019 - Pearl Harbor National Memorial (HI)
    # (maintains the valr code), Aleutian Islands World War II National
    # Monument (AK), and Tule Lake National Monument (CA)(tule). Tule
    # Lake is on the list of official park units, Aleutian Islands is
    # under 'Related Areas' and not on the official list.
    if park_name.lower().find('valor') > -1: park_code = 'valr'

    # The National World War I Memorial is a part of the National Mall
    # and Memorial Parks (DC).
    if park_name.lower() == "world war i memorial": park_code = 'nama'
    if park_name.lower().startswith("world war i "): park_code = 'nama'

    return park_code

def read_wikipedia_date_established(df_api):
    '''
    This function reads the park name and date established from the
    Excel file created by the nps_get_wikipedia_data.py script, looks up
    the correct park code in the paramter dataframe and returns a
    dataframe containing the park code and date established. The date
    established is currently only available for National Parks.

    Parameters
    ----------
    df_api : pandas DataFrame
        Dataframe for park code lookup.

    Returns
    -------
    df : pandas DataFrame
        Dataframe of park code and date established.
    '''

    # Read date established from file.
    filename = '_reference_data/wikipedia_date_established.csv'
    df = pd.read_csv(filename, header=0)
    df.date_established = pd.to_datetime(df.date_established)

    # Lookup the correct park code for the park name.
    df['park_name_stripped'] = df.park_name.apply(
                               lambda x: strip_park_name(x))
    df['park_code'] = df.park_name_stripped.apply(
                      lambda x: lookup_park_code(x, df_api))

    # Remove Seqoia and Kings Canyon NPs from the dataframe. They share
    # the same park code but have different established dates which will
    # be assigned after merge.
    df = df[df.park_code != 'seki']

    return df[['park_code', 'date_established']]

def read_wikipedia_list_of_presidents():
    '''
    This function reads president name and start and end term dates
    from the Excel file created by the nps_get_wikipedia_data.py script,
    and adds them to a dataframe.

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

    Returns
    -------
    df : pandas DataFrame
        Dataframe of president name and term dates.
    '''

    filename = '_reference_data/wikipedia_list_of_presidents.csv'
    df_pres = pd.read_csv(filename, header=0)
    df_pres.start_date = pd.to_datetime(df_pres.start_date)
    df_pres.end_date = pd.to_datetime(df_pres.end_date)

    return df_pres

def assign_president(date, df_pres):
    '''
    This function finds the president in office on the parameter date.

    Parameters
    ----------
    date : datetime
        Date for which to find president in office for.
    df_pres : pandas DataFrame
        Dataframe of president term start and end dates.

    Returns
    -------
    president_name : str
        President name.
    president_end_date : datetime
        End date of president's term.
    '''

    if pd.isnull(date):
        president_name = ''
        president_end_date = ''
    else:
        president_row = (df_pres[(df_pres.start_date <= date)
                         & (df_pres.end_date > date)])
        president_name = president_row.president.tolist()[0]
        president_end_date = pd.to_datetime(president_row.end_date.tolist()[0])
        if president_name == 'Grover Cleveland':
            president_end_date = pd.to_datetime('1889-03-04')

    return [president_name, president_end_date]

def read_acreage_data(df_api):
    '''
    This function reads the park size data from a report downloaded from
    nps.gov, looks up the correct park code for each park in the
    parameter dataframe and returns a dataframe containing the park code
    and park size in acres.

    Parameters
    ----------
    df_api : pandas DataFrame
        Dataframe for park code lookup.

    Returns
    -------
    df : pandas DataFrame
        Dataframe of park code and park size.
    '''

    filename = '_acreage_data/NPS-Acreage-12-31-2018.xlsx'
    df = pd.read_excel(filename, skiprows=1)
    df = df[pd.notnull(df['State'])]
    df = df.rename({'Gross Area Acres': 'gross_area_acres',
                    'Area Name': 'park_name'}, axis='columns')

    # Make some park name replacements to make matching the park name
    # to the df_api dataframe to find the park code work correctly.
    df['park_name'].replace(
        {"C & O":"Chesapeake & Ohio",
         "FDR":"Franklin Delano Roosevelt",
         "FRED-SPOTS":"Fredericksburg & Spotsylvania",
         "FT SUMTER":"Fort Sumter and Fort Moultrie",
         "JDROCKEFELLER":"John D. Rockefeller",
         "NATIONAL MALL":"National Mall and Memorial Parks",
         "OCMULGEE":"Ocmulgee Mounds",
         "RECONSTRUCTION ERA NM":"RECONSTRUCTION ERA NHP",
         "SALT RVR BAY NHP":
             "Salt River Bay National Historical Park and Ecological Preserve",
         "T ROOSEVELT":"Theodore Roosevelt",
         "WWII":"World War II",
         " NHP":" National Historical Park",
         " NHS":" National Historical Site",
         " NMP":" National Military Park",
         " NRA":" National Recreation Area",
         " NSR":" National Scenic River",
         " NS RIVERWAYS":" National Scenic Riverways",
         " NS TRAIL":" National Scenic Trail",
         " NS":" National Seashore",
         " RVR ":" River "},
        regex=True, inplace=True)

    df['park_name_stripped'] = df.park_name.apply(
                                   lambda x: strip_park_name(x))

    # Lookup the correct park code for the park name.
    df['park_code'] = df.park_name_stripped.apply(
                      lambda x: lookup_park_code(x, df_api))

    # Sum acreage data for parks with the same park code.
    df = df.groupby(['park_code'], as_index=False).sum()

    # Add square miles and square meters columns for reporting.
    df = df[['park_code', 'gross_area_acres']]
    df['gross_area_square_miles'] = df.gross_area_acres * 0.0015625
    df['gross_area_square_meters'] = df.gross_area_acres * 4046.86

    return df

def read_visitor_data(df_api):
    '''
    This function reads the park visitor data from a report downloaded
    from nps.gov, and saved in an Excel file by the
    nps_read_visitor_data.py script, into a dataframe. It then looks up
    the correct park code for each park in the parameter dataframe and
    returns a dataframe containing the park code and park visits per
    year for the years 1904 - 2018.

    Parameters
    ----------
    df_api : pandas DataFrame
        Dataframe for park code lookup.

    Returns
    -------
    df : pandas DataFrame
        Dataframe of park code and visits per year.
    '''

    filename = '_visitor_data/annual_visitors_by_park_1904_2018.xlsx'
    df = pd.read_excel(filename)

    # Exclude certain parks not in the list of 419.
    exclude_list = ["John F. Kennedy Center For Pa", "National Visitor Center",
                    "Klondike Gold Rush NHP Seattle", "Oklahoma City NMEM"]
    df = df[~(df.park_name.isin(exclude_list))]

    # Make some park name replacements to make matching the park name
    # to the df_api dataframe to find the park code work correctly.
    df['park_name'].replace(
        {"Fort Sumter":"Fort Sumter and Fort Moultrie",
         "Longfellow":"Longfellow House Washington's Headquarters",
         "Ocmulgee":"Ocmulgee Mounds",
         "President's Park":"President's Park (White House)",
         " EHP":"Ecological & Historic Preserve",
         " NHP":" National Historical Park",
         " NHS":" National Historical Site",
         " NMEM":" National Memorial",
         " NMP":" National Military Park",
         " NRA":" National Recreation Area",
         " NSR":" National Scenic River",
         " NS":" National Seashore"},
        regex=True, inplace=True)

    df['park_name_stripped'] = df.park_name.apply(
                               lambda x: strip_park_name(x))

    # Lookup the correct park code for the park name.
    df['park_code'] = df.park_name_stripped.apply(
                      lambda x: lookup_park_code(x, df_api))

    df.drop(columns=['park_name', 'park_name_stripped'], inplace=True)

    # Sum visitor data for parks with the same park code.
    df = df.groupby(['park_code'], as_index=False).sum()

    return df

def print_debug(df1_name, df1, df2_name, df2):
    '''
    Print some debug information.
    '''

    print("**** DEBUG: {} and {} ****".format(df1_name, df2_name))
    print("-- {}: {}".format(df1_name, df1.shape))
    print("-- {}: {}\n".format(df2_name, df2.shape))
    print("-- {} dupes: ".format(df1_name))
    df1_dupes = (df1[df1.duplicated(['park_code'], keep=False)]
                .sort_values(by=['park_code']))
    print(df1_dupes,"\n")
    print("-- {} dupes: ".format(df2_name))
    df2_dupes = (df2[df2.duplicated(['park_code'], keep=False)]
                .sort_values(by=['park_code']))
    print(df2_dupes,"\n")
    print("-- Park codes in {}, but not in {}:".format(df1_name, df2_name))
    df1_not_in_df2 = set(sorted((x for x in list(df1.park_code)
                     if x not in list(df2.park_code))))
    print(df1_not_in_df2)
    print("Length: {}\n".format(len(df1_not_in_df2)))
    print("-- Park codes in {}, not in {}:".format(df2_name, df1_name))
    df2_not_in_df1 = set(sorted((x for x in list(df2.park_code)
                     if x not in list(df1.park_code))))
    print(df2_not_in_df1)
    print("Length: {}\n".format(len(df2_not_in_df1)))

def main():
    pd.set_option('display.max_rows', 1000)
    debug = False

    # Read the NPS API data from file into a dataframe.
    df_api = read_park_sites_api()

    # Read the nps.gov National Park System Unit/Park list data from
    # file into a dataframe and merge it with the nps api dataframe.
    df_master = read_park_sites_web(df_api)
    if debug: print_debug('df_master', df_master, 'df_api', df_api)
    df_master = pd.merge(df_master[['park_name', 'park_code', 'designation']],
                         df_api[['park_code', 'states', 'lat', 'long']],
                         how='left', on='park_code')

    # Read the Wikipedia national park date established data from file
    # into a dataframe and merge it with the master dataframe.
    #df_estab = read_wikipedia_date_established(df_api)
    #df_master = pd.merge(df_master, df_estab, how='left', on='park_code')

    # Read manually created Excel file to get park dates.
    df_dates = get_park_dates()

    # Kings Canyon and Sequoia National Parks share the same park code
    # but were established on separate dates. Assign these dates.
    df_master.loc[df_master.park_name == "Kings Canyon National Park",
                  'date_established'] = pd.to_datetime('1890-10-01')
    df_master.loc[df_master.park_name == "Sequoia National Park",
                  'date_established'] = pd.to_datetime('1890-09-25')

    df_pres = read_wikipedia_list_of_presidents()
    df_master[['president', 'president_end_date']] = df_master.apply(
        lambda row: pd.Series(assign_president(row.date_established, df_pres)),
        axis=1
    )

    # Read the NPS Acreage report data from file into a dataframe and
    # merge it with the master dataframe.
    df_acreage = read_acreage_data(df_api)
    if debug: print_debug('df_master', df_master, 'df_acreage', df_acreage)
    df_master = pd.merge(df_master, df_acreage, how='left', on='park_code')

    # Read the NPS Visitor Use Statistics report from file into a
    # dataframe and merge it with the master dataframe.
    df_visitor = read_visitor_data(df_api)
    if debug: print_debug('df_master', df_master, 'df_visitor', df_visitor)
    df_master = pd.merge(df_master, df_visitor, how='left', on='park_code')

    # Sort and save the master dataframe to an Excel file.
    df_master = df_master.sort_values(by=['park_name']).reset_index(drop=True)
    df_master.to_excel('nps_parks_master_df.xlsx')

if __name__ == '__main__':
    main()


import pandas as pd
import numpy as np
from scipy import stats
import librosa as lr
from scipy.signal import filtfilt, argrelmax, find_peaks, correlate

def autocorrelation(array, samplerate=220, max_len_autocorr=220):
    try:
        autocorr = correlate(array, array)
        autocorr = autocorr[autocorr.shape[0]//2:]
        peaks = find_peaks(autocorr[:max_len_autocorr])
        if len(peaks[0]) == 0:
            peakpos = max_len_autocorr
            peakval = 1
        else:
            peakpos = peaks[0][0]
            peakval = autocorr[peaks[0][0]] / autocorr[0]
        return {'autocorr_peak_position': peakpos / samplerate,
                'autocorr_peak_value_normalized': peakval,
                'autocorr_ZCR': np.sum(lr.core.zero_crossings(autocorr[:max_len_autocorr])) / max_len_autocorr}
    except ValueError as e:
        return {'autocorr_peak_position': max_len_autocorr / samplerate,
                'autocorr_peak_value_normalized': 1,
                'autocorr_ZCR': 0}

def get_features(array, samplerate=220, max_len_autocorr=220):
    argmin = np.argmin(array)
    argmax = np.argmax(array)
    std = np.std(array)
    rms = np.sqrt(np.mean(array**2))
    acr = autocorrelation(array)
    zcr = np.sum(lr.core.zero_crossings(array)) / array.shape[0]
    return [array[argmin], array[argmax], np.mean(array), std,
            np.percentile(array, 10), np.percentile(array, 25),
            np.percentile(array, 50), np.percentile(array, 75), np.percentile(array, 90),
            stats.skew(array), stats.kurtosis(array),
            rms, rms / std,
            array[argmax] / array[argmin],
            acr['autocorr_peak_position'],
            acr['autocorr_peak_value_normalized'],
            acr['autocorr_ZCR']]

def get_features_with_derivative(array, samplerate=220, max_len_autocorr=220):
	feat_basic = get_features(array, samplerate, max_len_autocorr)
	feat_deriv = get_features(np.gradient(array), samplerate, max_len_autocorr)
	return feat_basic + feat_deriv


from pwn import *
import sys
from Crypto.Util.number import long_to_bytes

def init():
    p = remote(sys.argv[1], sys.argv[2])
    return Exploit(p), p

class Exploit:
    def __init__(self, p: process):
        self.p = p

    def start(self):
        p = self.p
        p.recvuntil(b"here we go:\n")

def main():
    x, p = init()
    x.start()
    for _ in range(100):
        val = p.recvline().decode()
        print(val)
        if val.startswith("0x"):
            a = long_to_bytes(eval(val))
            p.sendline(a)
        elif val.startswith("0b"):
            a = long_to_bytes(eval(val))
            p.sendline(a)
        else:
            a = base64.b64decode(val)
            p.sendline(a)
        print(a)
    p.interactive()

main()


import matplotlib.pyplot as plt
import datetime

time = []
aqi = []
filename = str(datetime.date.today())
file = open("AQI/AQI"+filename+".txt", "r+")
line = file.readline()
while line:
    time.append(int(line[0:2]))
    aqi.append(int(line[3:6]))
    line = file.readline()
file.close()
plt.bar(time, aqi, width=0.25)
plt.xticks(time)
plt.xlim()
plt.ylim(0, 300)
for x, y in zip(time, aqi):
    plt.text(x, y, '%d' % y, ha='center', va='bottom')
plt.savefig("AQI/AQI"+filename)


import os
import datetime
import re
from API import API
from CeleryPy import log
from CeleryPy import set_user_env
from Botstate import get_bot_state
import json

class MyFarmware():

    def get_input_env(self):
        prefix = self.farmwarename.lower().replace('-','_')

        self.input_title = os.environ.get(prefix+"_title", '-')
        self.input_tool = os.environ.get(prefix+"_tool", 'BACKUP_USERENV')
        self.input_debug = int(os.environ.get(prefix+"_debug", 1))

        if self.input_debug >= 1:
            log('title: {}'.format(self.input_title), message_type='debug', title=self.farmwarename)
            log('tool: {}'.format(self.input_tool), message_type='debug', title=self.farmwarename)
            log('debug: {}'.format(self.input_debug), message_type='debug', title=self.farmwarename)

    def __init__(self,farmwarename):
        self.farmwarename = farmwarename
        self.get_input_env()
        self.api = API(self)
        self.points = []

    def check_celerypy(self,ret):
        try:
            status_code = ret.status_code
        except:
            status_code = -1
        try:
            text = ret.text[:100]
        except expression as identifier:
            text = ret
        if status_code == -1 or status_code == 200:
            if self.input_debug >= 1: log("{} -> {}".format(status_code,text), message_type='debug', title=self.farmwarename + ' check_celerypy')
        else:
            log("{} -> {}".format(status_code,text), message_type='error', title=self.farmwarename + ' check_celerypy')
            raise

    def get_toolslot(self, points, tools, tool_name='', pointer_type='ToolSlot'):
        for t in tools:
            if t['name'].lower() == tool_name.lower():
                for p in points:
                    if p['pointer_type'].lower() == pointer_type.lower():
                        if p['tool_id'] == t['id']:
                            return p.copy()

        return

    def backup_bot_state(self):
        self.botstate = get_bot_state()
        if self.input_debug >= 1: log('botstate: {}'.format(self.botstate), message_type='debug', title=self.farmwarename)
        self.toolslot = self.get_toolslot(
            points = self.api.api_get('points'),
            tools = self.api.api_get('tools'),
            tool_name = self.input_tool)
        if self.input_debug >= 1: log('toolslot Before: {}'.format(self.toolslot), message_type='debug', title=self.farmwarename)
        self.toolslot['meta'] = self.botstate["user_env"]
        if self.input_debug < 2 :
            endpoint = 'points/{}'.format(self.toolslot['id'])
            self.api.api_put(endpoint=endpoint, data=self.toolslot)
        self.toolslot = self.get_toolslot(
            points = self.api.api_get('points'),
            tools = self.api.api_get('tools'),
            tool_name = self.input_tool)
        if self.input_debug >= 1: log('toolslot After: {}'.format(self.toolslot), message_type='debug', title=self.farmwarename)

    def restore_bot_state(self):
        self.botstate = get_bot_state()
        if self.input_debug >= 1: log('botstate Before: {}'.format(self.botstate), message_type='debug', title=self.farmwarename)
        self.toolslot = self.get_toolslot(
            points = self.api.api_get('points'),
            tools = self.api.api_get('tools'),
            tool_name = self.input_tool)
        if self.input_debug >= 1: log('toolslot: {}'.format(self.toolslot), message_type='debug', title=self.farmwarename)
        for e in self.toolslot['meta']:
            if self.input_debug >= 1: log('set_user_env: {} => {}'.format(e,self.toolslot['meta'][e]), message_type='debug', title=self.farmwarename)
            if self.input_debug < 2 : self.check_celerypy(set_user_env(e, self.toolslot['meta'][e]))

    def run(self):
        if "restore" in self.farmwarename.lower() : self.restore_bot_state()
        if "backup" in self.farmwarename.lower() : self.backup_bot_state()


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

from setuptools import setup, find_packages
import sys

# add back later
if sys.version_info[0] != 3:
    # sys.exit("Only Python 3 is supported")
    print("WARNING: Only Python 3 is supported")

# with open('README.md') as readme_file:
#     readme = readme_file.read()

requirements = [
    "numpy",
    "pandas",
    "scipy",
    "scikit-learn>=0.18",
    "matplotlib",
    # "tensorflow", # - not per-se required
    # "glmnet",
    "keras>=2.0.4,<=2.2.4",
    'descartes',
    'shapely',
    'gtfparse>=1.0.7'
]

test_requirements = [
    "pytest",
    'hyperopt',
]

setup(
    name='concise',
    version='0.6.9',
    description="CONCISE (COnvolutional Neural for CIS-regulatory Elements)",
    # long_description=readme,
    author="Žiga Avsec",
    author_email='avsec@in.tum.de',
    url='https://github.com/gagneurlab/concise',
    packages=find_packages(),
    package_data={'concise.resources': ['attract_metadata.txt', 'attract_pwm.txt',
                                        'encode_motifs.txt.gz',
                                        'HOCOMOCOv10_pcms_HUMAN_mono.txt'],
                  'concise.resources.RNAplfold': ["H_RNAplfold", "I_RNAplfold", "M_RNAplfold", "E_RNAplfold"]},
    include_package_data=True,
    setup_requires=['numpy'],
    install_requires=requirements,
    # dependency_links=dependency_links,
    license="MIT license",
    zip_safe=False,
    keywords=["computational biology", "bioinformatics", "genomics",
              "deep learning", "tensorflow", ],
    extras_require={
        'tensorflow': ['tensorflow>=1.0'],
        'tensorflow with gpu': ['tensorflow-gpu>=1.0']},
    classifiers=[
        # classifiers
        # default
        'Topic :: Scientific/Engineering :: Bio-Informatics',
        'Topic :: Scientific/Engineering :: Artificial Intelligence',
        'Development Status :: 2 - Pre-Alpha',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: MIT License',
        'Natural Language :: English',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.5',
    ],
    test_suite='tests',
    tests_require=test_requirements
)


from ultralytics import YOLO
import cv2
import cvzone
import math
import uuid

cap = cv2.VideoCapture('../videos/cars.mp4')
# cap = cv2.VideoCapture('../videos/cars_karam.mp4')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
# Generate a random UUID as the filename
out_file = f"output_{uuid.uuid4()}.mp4"
# Define the codec and create a VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
# out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))
# Create the VideoWriter object with the new output filename
out = cv2.VideoWriter(out_file, fourcc, fps, (width, height))
model = YOLO("../Yolo-Weights/yolov8n.pt")
'''
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", "traffic light",
              "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
              "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
              "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
              "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
              "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa",
              "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard",
              "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
              "teddy bear", "hair drier", "toothbrush"]
'''
with open("../Yolo-Weights/coco.names", "r") as f:
    classNames = f.read().splitlines()
while True:
    success, img = cap.read()
    if not success:
        break
    results = model(img, stream=True)
    for r in results:
        boxes = r.boxes
        for box in boxes:
            x1, y1, x2, y2 = box.xyxy[0]
            x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
            w, h = x2 - x1, y2 - y1
            cvzone.cornerRect(img, (x1, y1, w, h))
            conf = math.ceil((box.conf[0] * 100)) / 100
            cls = int(box.cls[0])
            cvzone.putTextRect(img, f'{classNames[cls]} {conf}', (max(0, x1), max(35, y1)), scale=1, thickness=1)
    out.write(img)  # Write the frame to the output video
    # img_display = cv2.resizeWindow(img)
    cv2.imshow("Image",img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# Release everything
cap.release()
out.release()
cv2.destroyAllWindows()


import cv2
import numpy as np
import matplotlib.pyplot as plt
import random
# from skimage import io

img = cv2.imread("3.jpg")

def imshowx(img):
    size = plt.rcParams["figure.figsize"]
    size[0] = 12
    size[1] = 4
    plt.rcParams["figure.figsize"] = size

    plt.axis("off")
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.show()

def imshowgray(img):
    plt.axis("off")
    plt.imshow(img, plt.get_cmap("gray"))
    plt.show()

#imshowx(outimg)
def nothing(x):
    pass
def create_Trackbar():
    cv2.namedWindow("lower")
    cv2.namedWindow("upper")
    cv2.createTrackbar('lowH','lower',0,360,nothing)
    cv2.createTrackbar('highH','upper',360,360,nothing)

    cv2.createTrackbar('lowS','lower',0,255,nothing)
    cv2.createTrackbar('highS','upper',255,255,nothing)

    cv2.createTrackbar('lowV','lower',0,255,nothing)
    cv2.createTrackbar('highV','upper',255,255,nothing)

def get_threshold():
    ilowH = cv2.getTrackbarPos('lowH', 'lower')
    ihighH = cv2.getTrackbarPos('highH', 'upper')
    ilowS = cv2.getTrackbarPos('lowS', 'lower')
    ihighS = cv2.getTrackbarPos('highS', 'upper')
    ilowV = cv2.getTrackbarPos('lowV', 'lower')
    ihighV = cv2.getTrackbarPos('highV', 'upper')
    return np.array([ilowH,ilowS,ilowV]),np.array([ihighH,ihighS,ihighV])

#create_Trackbar()

# while(1):
#     hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#     cv2.imshow("hsv", hsv)
#     lower1, upper1 = get_threshold()
#     lower2, upper2 = get_threshold()
#     mask_1 = cv2.inRange(hsv, lower1,upper1) # lower range blue
#     mask_2 = cv2.inRange(hsv, lower2, upper2) # upper range blue
#     mask_r = cv2.bitwise_or(mask_1,mask_2)
#     cv2.imshow("mask1", mask_1)
#     cv2.imshow("mask2", mask_2)
#     cv2.imshow("mask", mask_r)
#     if cv2.waitKey(1) & 0xFF == ord('q'):
#         break

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower1, upper1 = np.array([78,102,43]), np.array([125,255,255])
lower2, upper2 = np.array([74,105,103]), np.array([182,213,195])
mask_1 = cv2.inRange(hsv, lower1,upper1) # lower range red
imshowgray(mask_1)
mask_2 = cv2.inRange(hsv, lower2, upper2) # upper range blue
imshowgray(mask_2)

mask_r = cv2.bitwise_or(mask_1,mask_2)
imshowgray(mask_r)
blur = cv2.GaussianBlur(mask_r, (9,9), 0)
edge_img = cv2.Canny(blur, 200, 255)
#imshowgray(edge_img)

img2 = img.copy()
# find contours in edge image: object found should be white on black backgroud
# function have three argument: src image, contour retrieval mode, contour approximation method
im ,cnts,_ = cv2.findContours(edge_img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img2,cnts, -1, (0,255,0), 2)
imshowx(img2)

img2 = img.copy()
for cnt in cnts:
    area = cv2.contourArea(cnt) # define area contour
    if area < 100:
         continue
    # elip = cv2.fitEllipse(cnt)
    # cv2.ellipse(img2, elip, (0, 255, 0), 2)
    x,y,w,h = cv2.boundingRect(cnt)
    cv2.rectangle(img2,(x,y),(x+w,y+h),(0,255,0),3)

imshowx(img2)


import sys

input = sys.stdin.readline

# 10103 주사위 게임
# 100점에서 시작하여 주사위를 던져 작은 눈이 나온쪽이 큰쪽 눈만큼 점수를 잃을 때
# 게임이 끝난 후 두 플레이어의 점수를 구하는 문제
def sol10103():
    answer = [100, 100]
    for _ in range(int(input())):
        u, v = map(int, input().split())
        if u < v:
            answer[0] -= v
        elif u > v:
            answer[1] -= u
    return '\n'.join(map(str, answer))


import csv
import matplotlib.pyplot as plt
from sat_to_act import sat_to_act

labels = None
dataset = None

college_name = 'Illinois'

with open('./data/College Data - ' + college_name + '.csv') as f:
    data = csv.reader(f)
    # labels = data[0]
    # dataset = data[1:]
    dataset = []
    i = 0
    for d in data:
        if i != 0:
            dataset.append(d)
        i += 1
    i = 0
    while i < len(dataset):
        d = dataset[i]
        if not dataset[i][1] in ['Accepted', 'Denied', 'Deferred', 'Waitlisted']:
            dataset.remove(d)
            i-=1
        elif max(sat_to_act(d[6]) if d[6].isdigit() else 0, int(d[7]) if d[7].isdigit() else 0) == 0:
            dataset.remove(d)
            i-=1
        i+=1
    plt.xlabel("ACT")
    plt.ylabel("GPA")
    plt.title(college_name + " Scattergram")
    plt.scatter([max(sat_to_act(d[6]) if d[6].isdigit() else 0, int(d[7]) if d[7].isdigit() else 0) for d in dataset], [float(d[5]) for d in dataset], marker='x', c=['green' if d[1] in ['Accepted', 'Deferred'] else 'red' if d[1] in ['Denied', 'Waitlisted'] else 'yellow' for d in dataset])
    plt.show()


from sys import stdin

def calculateMedianOfSortedArray(array):
    array = sorted(array)
    arrayLength = len(array)

    #If N is an odd number  -> median = X(n+1)/2
    if arrayLength % 2 != 0:
        median = array[int((arrayLength-1)/2)]
    #If N is an even number -> median = (X(n/2)+ X((n/2)+1))/2
    else:
        auxMidLow = int((arrayLength/2)-1)
        auxMidHig = int(arrayLength/2)
        median = (array[auxMidLow] + array[auxMidHig]) / 2

    return median

#arrayLength = int(stdin.readline())
#array = stdin.readline().split()
#arrayLength = 10
#array   = "3 7 8 5 12 14 21 15 18 14".split()
arrayLength = 9
array   ="3 7 8 5 12 14 21 13 18".split()

array = list(map(int, array))
array = sorted(array)
print(array)

median = 0.0
q1 = 0
q2 = 0
q3 = 0

middle = 0
lowerHalf = []
upperHalf = []

if arrayLength % 2 != 0:
    middle = int((arrayLength-1)/2)
    q2 = array[int(middle)]
    upperHalf = array[(middle+1):arrayLength]
else:
    middle = int(arrayLength/2)
    q2 = int(calculateMedianOfSortedArray(array))
    upperHalf = array[middle:arrayLength]

middle = int(middle)

lowerHalf = array[0:middle]

print('low ',lowerHalf)
print('up  ',upperHalf)

q1 = int(calculateMedianOfSortedArray(lowerHalf))
q3 = int(calculateMedianOfSortedArray(upperHalf))

print(q1)
print(q2)
print(q3)


# Exercise 3-09: Evaluate the RF regressor

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error as MSE

SEED = 2

df = pd.read_csv('input/bikes.csv')

y = df['cnt']
X = df.drop(['cnt'], axis=1)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=SEED)

rf = RandomForestRegressor(n_estimators=25, random_state=SEED)
rf.fit(X_train, y_train)

# Predict the test set labels
y_pred = rf.predict(X_test)

# Evaluate the test set RMSE
rmse_test = MSE(y_test, y_pred)**(1/2)

# Print rmse_test
print('Test set RMSE of rf: {:.2f}'.format(rmse_test))


# To find the third largest number from the list

# an empty list "a"
a = []

# how many elements user want to append
n = int(input("Enter the number of elements :)"))

# iterating the loop to n + 1
for i in range(1, n + 1):
    # asking the user to input a number
    b = int(input("Enter the number :)"))
    # adding each number to end of the list
    a.append(b)

# results
print("Third largest element is :)", a[n-3])


import bluepyopt as bpop
import bluepyopt.deapext.algorithms as algo
import pickle
import time
import numpy as np
from datetime import datetime
import argparse
import sys
import argparse
import time
import textwrap
import os
import cProfile
import shutil
import glob
from mpi4py import MPI
import multiprocessing
# set up environment variables for MPI
import logging

comm = MPI.COMM_WORLD
global_rank = comm.Get_rank()
size = comm.Get_size()
import hoc_evaluator as hoc_ev

import logging.config
# logging.config.dictConfig({
#     'version': 1,
#     'disable_existing_loggers': True,
# })
filename = "runTimeLogs/runTime.log"

import logging.handlers
import os

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# formatter = logging.Formatter(fmt="%(asctime)s %(levelname)s: %(message)s",
#                           datefmt="%Y-%m-%d - %H:%M:%S")

if global_rank == 0:
    # your logging setup
    should_roll_over = os.path.isfile(filename)
    fh = logging.handlers.RotatingFileHandler(filename, mode='w', backupCount=15, delay=True)
    if should_roll_over:  # log already exists, roll over!
        print("######### ROLL OVER #######")
        fh.doRollover()

if global_rank != 0:
    fh = logging.handlers.RotatingFileHandler(filename, mode='w', backupCount=15, delay=True)

comm.barrier()
fh.setLevel(logging.DEBUG)
# fh.setFormatter(formatter)
logger.addHandler(fh)
logger.propagate = False

# logging.config.dictConfig({ 'version': 1, 'filename':filename, 'level': logging.DEBUG})#, 'disable_existing_loggers': True })
# logging.basicConfig(filename=filename, level=logging.DEBUG)
logger.info("absolute start : " + str(time.time()) + " from rank" + str(global_rank))

gen_counter = 0
best_indvs = []
cp_freq = 1
old_update = algo._update_history_and_hof

def get_parser():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description='L5PC example',
        epilog=textwrap.dedent('''\
The folling environment variables are considered:
    L5PCBENCHMARK_USEIPYP: if set, will use ipyparallel
    IPYTHON_PROFILE: if set, used as the path to the ipython profile
    BLUEPYOPT_SEED: The seed used for initial randomization
        '''))
    parser.add_argument('--start', action="store_true")
    parser.add_argument('--continu', action="store_true")
    parser.add_argument('--checkpoint', required=False, default=None,
                        help='Checkpoint pickle to avoid recalculation')
    parser.add_argument('--offspring_size', type=int, required=False, default=2,
                        help='number of individuals in offspring')
    parser.add_argument('--max_ngen', type=int, required=False, default=2,
                        help='maximum number of generations')
    parser.add_argument('--n_stims', type=int, required=False, default=1,
                        help='number of stims to optimize over')
    parser.add_argument('--n_sfs', type=int, required=False, default=0,
                        help='number of score functions to use')
    parser.add_argument('--n_cpus', type=int, required=False, default=0,
                        help='number of cpu cores to use')
    parser.add_argument('--sf_module', type=str, required=False, default='efel',
                        help='number of cpu cores to use')
    parser.add_argument('--responses', required=False, default=None,
                        help='Response pickle file to avoid recalculation')
    parser.add_argument('--analyse', action="store_true")
    parser.add_argument('--compile', action="store_true")
    parser.add_argument('--hocanalyse', action="store_true")
    parser.add_argument('--seed', type=int, default=42,
                        help='Seed to use for optimization')
    parser.add_argument('--ipyparallel', action="store_true", default=False,
                        help='Use ipyparallel')
    parser.add_argument(
        '--diversity',
        help='plot the diversity of parameters from checkpoint pickle file')
    parser.add_argument('-v', '--verbose', action='count', dest='verbose',
                        default=0, help='-v for INFO, -vv for DEBUG')

    return parser

def my_update(halloffame, history, population):
    global gen_counter, cp_freq
    old_update(halloffame, history, population)
    best_indvs.append(halloffame[0])
    gen_counter = gen_counter+1
    print("Current generation: ", gen_counter)

    if gen_counter%cp_freq == 0:
        fn = '.pkl'
        save_logs(fn, best_indvs, population)

def save_logs(fn, best_indvs, population):
    output = open("./best_indv_logs/best_indvs_gen_"+str(gen_counter)+fn, 'wb')
    pickle.dump(best_indvs, output)
    output.close()

def my_record_stats(stats, logbook, gen, population, invalid_count):
    '''Update the statistics with the new population'''
    record = stats.compile(population) if stats is not None else {}
    logbook.record(gen=gen, nevals=invalid_count, **record)
    logbook = comm.bcast(logbook, root=0)
    if global_rank == 0:
        print('log: ', logbook, '\n')
        output = open("log.pkl", 'wb')
        pickle.dump(logbook, output)
        output.close()

def main():
    args = get_parser().parse_args()
    algo._update_history_and_hof = my_update
    algo._record_stats = my_record_stats

    evaluator = hoc_ev.hoc_evaluator(args.offspring_size,logger)
    seed = os.getenv('BLUEPYOPT_SEED', args.seed)
    opt = bpop.optimisations.DEAPOptimisation(
        evaluator=evaluator,
        #map_function=map_function,
        seed=seed,
        eta=20,
        mutpb=0.3,
        cxpb=0.7)
    pop, hof, log, hst = opt.run(max_ngen=args.max_ngen,
        offspring_size=args.offspring_size,
        continue_cp=args.continu,
        cp_filename=args.checkpoint,
        cp_frequency=1)

if __name__ == '__main__':

    main()
#     if global_rank == 0:
#         p.kill()
#         kill(p.pid)
    if global_rank == 1:
        logger.info("absolute end : " + str(time.time()))


# Assignment Question 1
# creating a method and calling it 'sorting'
def sorting(*coding):
    #word = coding
    if 'java' in coding:
        for letter in 'java':
            print(letter)

    else:
        print("There is no 'java' in sorting method")
        print('\n')

sorting('python', 'java')
print('\n')
sorting('python', 'java', 'Go')
print('\n')
sorting('python', 'js', 'c++')

# Question 2
# Method for tax calculation

def tax_calculation(gross_salary, tax = 0.22):
    global salary
    salary = gross_salary * (1-tax)
    print(salary)
def salary_limit_tester(net_salary):
    if net_salary > 5800:
        print("The net_salary is above 5800 and value is: " + str(net_salary))
    else:
        print("The net_salary is below 5800 and value is: " + str(net_salary))
tax_calculation(5000, 0.2)
salary_limit_tester(salary)
tax_calculation(6000,0.22)
salary_limit_tester(salary)
tax_calculation(10000)
salary_limit_tester(salary)


import os
import sqlite3
from datetime import datetime, timedelta, timezone

import pytest

from whylogs.api.store.query import DatasetIdQuery, DateQuery
from whylogs.api.store.sqlite_store import SQLiteStore
from whylogs.core import DatasetProfile, DatasetProfileView

class TestSQLiteStore:
    @classmethod
    def setup_class(cls):
        os.environ["SQLITE_STORE_LOCATION"] = "test_database.db"
        os.environ["MERGE_PROFILE_PERIOD_HOURS"] = "1"

    @classmethod
    def teardown_class(cls):
        del os.environ["MERGE_PROFILE_PERIOD_HOURS"]
        del os.environ["SQLITE_STORE_LOCATION"]
        os.remove("test_database.db")

    @pytest.fixture
    def store(self, profile_view):
        with SQLiteStore() as store:
            store.write(profile_view=profile_view, dataset_id="my_profile")
            yield store

    def test_sqlite_list(self, store):
        assert store.list() == ["my_profile"]

    def test_sqlite_get_by_name(self, store):
        name_query = DatasetIdQuery(dataset_id="my_profile")
        profile = store.get(query=name_query)
        assert isinstance(profile, DatasetProfileView)

    def test_sqlite_get_by_date_range(self, store):
        now = datetime.now(timezone.utc)
        one_week_ago = now - timedelta(days=7)
        date_query = DateQuery(dataset_id="my_profile", start_date=one_week_ago, end_date=now)
        profile = store.get(query=date_query)
        assert isinstance(profile, DatasetProfileView)

    def test_sqlite_get_by_start_date_only(self, store):
        now = datetime.now(timezone.utc)
        date_query = DateQuery(dataset_id="my_profile", start_date=now)
        profile = store.get(query=date_query)
        assert isinstance(profile, DatasetProfileView)

    def test_gets_an_empty_profile_if_does_not_exist(self, store):
        name_query = DatasetIdQuery(dataset_id="doesnt_exist")
        expected_profile = store.get(query=name_query)
        assert isinstance(expected_profile, DatasetProfileView)
        assert expected_profile.get_columns() == {}

    def test_write_to_sqlite(self, store, profile_view):
        store.write(profile_view=profile_view, dataset_id="test_profile")

        assert len(store.list()) > 0
        assert "test_profile" in store.list()

    def test_check_if_profile_exists(self, store, profile_view):
        dataset_id = "test_profile_exists"
        empty = store._check_if_profile_exists(profile_view=profile_view, dataset_id=dataset_id)

        assert empty.get_columns() == {}

        store.write(profile_view=profile_view, dataset_id=dataset_id)

        not_empty = store._check_if_profile_exists(profile_view=profile_view, dataset_id=dataset_id)
        assert not_empty.get_columns() != {}

    def test_merge_if_profile_already_exists(self, store, profile_view):
        store.write(profile_view=profile_view, dataset_id="merged_profile")
        store.write(profile_view=profile_view, dataset_id="merged_profile")

        assert "merged_profile" in store.list()
        query = DatasetIdQuery(dataset_id="merged_profile")

        actual_profile = store.get(query=query)
        expected_profile = profile_view.merge(profile_view)

        assert actual_profile.get_columns().keys() == expected_profile.get_columns().keys()
        actual_counts = actual_profile.get_column("legs").get_metric("counts").n.value
        expected_counts = expected_profile.get_column("legs").get_metric("counts").n.value

        assert actual_counts == expected_counts

    def test_larger_timestamps_wont_merge_profiles(self):
        with SQLiteStore() as store:
            ts1 = datetime.now(timezone.utc)
            ts2 = datetime.now(timezone.utc) + timedelta(hours=10 * int(os.environ["MERGE_PROFILE_PERIOD_HOURS"]))

            view1 = DatasetProfile(creation_timestamp=ts1).view()
            view2 = DatasetProfile(creation_timestamp=ts2).view()

            store.write(profile_view=view1, dataset_id="test_not_merge")
            store.write(profile_view=view2, dataset_id="test_not_merge")

        con = sqlite3.connect(os.environ["SQLITE_STORE_LOCATION"])
        cur = con.cursor()
        sql_query = "SELECT * FROM profile_store WHERE id = 'test_not_merge'"
        result = cur.execute(sql_query).fetchall()
        assert len(result) == 2


from EmotionModel import EmotionModel
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import numpy as np
import time
import torch.optim as optim
import torch
from dataset import get_data_deap,get_data_seed
from config import get_config

def train(Model, source_loader, target_loader):
    correct = 0
    train_correct=0.
    start_time = time.time()
    for (source_data, source_label),(target_data, target_label) in zip(source_loader,target_loader):
        optimizer.zero_grad()
        data_s, label_s = source_data[0].cuda(), source_label.cuda()
        data_t, label_t = target_data.cuda(), target_label.cuda()
        data=torch.cat([data_s,data_t],0)
        label=torch.cat([label_s,label_t],0)
        Model.fit_one_step(data,label,True)
        loss=Model.pred_loss+0.1*Model.walker_loss+Model.visit_loss+Model.domain_loss+Model.l2_loss
        loss.backward(retain_graph=False)
        optimizer.step()
        e_output = torch.argmax(Model.target_logits, dim=1)
        correct += torch.eq(e_output,Model.target_y.argmax(dim=1)).float().sum().item()
        train_correct+=Model.acc_nums.item()
    test_acc = round(correct / (len(target_loader.dataset)) * 100,3)
    train_acc = round(train_correct/ (len(source_loader.dataset)) * 100, 3)
    use_time = time.time() - start_time
    min = int(use_time // 60)
    sum = int(use_time % 60)
    print(f"Train Epoch: [{epoch}/{config.EPOCH}], Test Acc: {test_acc}, Train Acc: {train_acc} "
          f"Walker loss: {Model.walker_loss.item()}, Pred loss: {Model.pred_loss.item()}, Visit loss: {Model.visit_loss.item()}")
    return test_acc

if __name__ == '__main__':
    torch.cuda.empty_cache()
    total_log = []
    config=get_config()
    if config.DATA.DATASET == "deap":
        total = 32
    elif config.DATA.DATASET == "seed":
        total = 15
    else:
        raise NotImplementedError("not improve this dataset")
    for exp in range(1):
        exp = exp + 1
        subject_ACC = []
        for i in range(total):
            print('*' * 20+f'target subject: {i}'+'*'*20)
            index = np.arange(0, total, 1).tolist()
            del index[i]
            if config.DATA.DATASET == "deap":
                sample_path = config.DATA.DEAP_DATA_PATH
                source_loader, target_loader = get_data_deap(sample_path=sample_path,
                                                             index=index,
                                                             i=i,
                                                             num_classes=config.MODEL.DEAP.NUM_CLASSES,
                                                             batchsize=config.MODEL.DEAP.BATCH_SIZE)
                Model = EmotionModel(config.MODEL.DEAP.OUT*config.MODEL.DEAP.TIME_DIM,config.MODEL.DEAP.NUM_CLASSES).cuda()
                optimizer = optim.Adam(
                    # [{'params': Model.linear1.weight, 'weight_decay': 0.001},
                    #  {'params': Model.linear2.weight, 'weight_decay': 0.001},
                    #  {'params': Model.linear3.weight, 'weight_decay': 0.001},
                    #  {'params': Model.linear1.bias},
                    #  {'params': Model.linear2.bias},
                    #  {'params': Model.linear3.bias},]
                    Model.parameters(), lr=2*0.0001
                )

            elif config.DATA.DATASET == "seed":
                sample_path = config.DATA.SEED_DATA_PATH
                source_loader, target_loader = get_data_seed(sample_path=sample_path,
                                                             index=index,
                                                             i=i,
                                                             num_classes=config.MODEL.SEED.NUM_CLASSES,
                                                             batchsize=config.MODEL.SEED.BATCH_SIZE)
                Model = EmotionModel(config.MODEL.SEED.OUT * config.MODEL.SEED.TIME_DIM, config.MODEL.SEED.NUM_CLASSES).cuda()
                optimizer = optim.Adam(
                    # [{'params': Model.linear1.weight, 'weight_decay': 0.001},
                    #  {'params': Model.linear2.weight, 'weight_decay': 0.001},
                    #  {'params': Model.linear3.weight, 'weight_decay': 0.001},
                    #  {'params': Model.linear1.bias},
                    #  {'params': Model.linear2.bias},
                    #  {'params': Model.linear3.bias},]
                    Model.parameters(), lr=2*0.0001
                )

            else:
                raise NotImplementedError("not improve this dataset")
            total_test_acc=[]
            for epoch in range(1, config.EPOCH + 1):
                p = float(i) / epoch
                l = 2. / (1. + np.exp(-10. * p)) - 1
                lr = 0.0045 / (1. + 15 * p) ** 0.75
                Model.alpha=l
                if config.MODEL.IF_TURN_LR:
                    for param_group in optimizer.param_groups:
                        param_group['lr'] = lr
                test_acc=train(Model, source_loader, target_loader)
                total_test_acc.append(test_acc)
            subject_ACC.append(total_test_acc)
    for i in range(config.EPOCH):
        acc=np.array([subject_ACC[j][i] for j in range(total)]).mean()
        print(f"EPOCH {i} ,ACC {acc}")


"""
Radial kernels convolutions
===========================

This benchmark compares the performances of KeOps versus Numpy and PyTorch on various radial
kernels convolutions. Namely it computes:

.. math::

   a_i = \sum_{j=1}^N f\Big(\\frac{\|x_i-y_j\|}{\sigma}\Big) b_j, \quad \\text{ for all } i=1,\cdots,M

where :math:`f` is a Gauss or Cauchy or Laplace or inverse multiquadric kernel. See e.g. `wikipedia  <https://en.wikipedia.org/wiki/Radial_basis_function>`_

"""

#####################################################################
# Setup
# -----
# Standard imports:

import numpy as np
import timeit
import matplotlib
from matplotlib import pyplot as plt
from pykeops.numpy.utils import np_kernel
from pykeops.torch.utils import torch_kernel
from pykeops.torch import Vi, Vj, Pm

######################################################################
# Benchmark specifications:
#

M = 10000  # Number of points x_i
N = 10000  # Number of points y_j
D = 3  # Dimension of the x_i's and y_j's
E = 3  # Dimension of the b_j's
REPEAT = 10  # Number of loops per test

dtype = "float32"

######################################################################
# Create some random input data:
#

x = np.random.randn(M, D).astype(dtype)  # Target points
y = np.random.randn(N, D).astype(dtype)  # Source points
b = np.random.randn(N, E).astype(dtype)  # Source signal
sigma = np.array([2.4]).astype(dtype)  # Kernel radius

######################################################################
# And load it as PyTorch variables:
#

try:
    import torch

    use_cuda = torch.cuda.is_available()
    device = "cuda" if use_cuda else "cpu"
    torchtype = torch.float32 if dtype == "float32" else torch.float64

    xc = torch.tensor(x, dtype=torchtype, device=device)
    yc = torch.tensor(y, dtype=torchtype, device=device)
    bc = torch.tensor(b, dtype=torchtype, device=device)
    sigmac = torch.tensor(sigma, dtype=torchtype, device=device)

except:
    pass

####################################################################
# Convolution Benchmarks
# ----------------------
#
# We loop over four different kernels:
#

kernel_to_test = ["gaussian", "laplacian", "cauchy", "inverse_multiquadric"]
kernels = {
    "gaussian": lambda xc, yc, sigmac: (
        -Pm(1 / sigmac**2) * Vi(xc).sqdist(Vj(yc))
    ).exp(),
    "laplacian": lambda xc, yc, sigmac: (
        -(Pm(1 / sigmac**2) * Vi(xc).sqdist(Vj(yc))).sqrt()
    ).exp(),
    "cauchy": lambda xc, yc, sigmac: (
        1 + Pm(1 / sigmac**2) * Vi(xc).sqdist(Vj(yc))
    ).power(-1),
    "inverse_multiquadric": lambda xc, yc, sigmac: (
        1 + Pm(1 / sigmac**2) * Vi(xc).sqdist(Vj(yc))
    )
    .sqrt()
    .power(-1),
}

#####################################################################
# With four backends: Numpy, vanilla PyTorch, Generic KeOps reductions
# and a specific, handmade legacy CUDA code for kernel convolutions:
#

speed_numpy = {i: np.nan for i in kernel_to_test}
speed_pytorch = {i: np.nan for i in kernel_to_test}
speed_pykeops = {i: np.nan for i in kernel_to_test}

print("Timings for {}x{} convolutions:".format(M, N))

for k in kernel_to_test:
    print("kernel: " + k)

    # Pure numpy
    g_numpy = np.matmul(np_kernel(x, y, sigma, kernel=k), b)
    speed_numpy[k] = timeit.repeat(
        "gnumpy = np.matmul( np_kernel(x, y, sigma, kernel=k), b)",
        globals=globals(),
        repeat=5,
        number=1,
    )
    print("Time for NumPy:               {:.4f}s".format(np.median(speed_numpy[k])))

    # Vanilla pytorch (with cuda if available, and cpu otherwise)
    try:
        g_pytorch = torch_kernel(xc, yc, sigmac, kernel=k) @ bc
        torch.cuda.synchronize()
        speed_pytorch[k] = (
            np.array(
                timeit.repeat(
                    "torch_kernel(xc, yc, sigmac, kernel=k) @ bc; torch.cuda.synchronize()",
                    globals=globals(),
                    repeat=REPEAT,
                    number=4,
                )
            )
            / 4
        )

        print(
            "Time for PyTorch:             {:.4f}s".format(np.median(speed_pytorch[k])),
            end="",
        )
        print(
            "   (absolute error:       ",
            np.max(np.abs(g_pytorch.cpu().numpy() - g_numpy)),
            ")",
        )
    except:
        print("Time for PyTorch:             Not Done")

    # Keops: LazyTensors implementation (with cuda if available)
    try:
        g_pykeops = (kernels[k](xc, yc, sigmac) @ bc).cpu()
        torch.cuda.synchronize()
        speed_pykeops[k] = (
            np.array(
                timeit.repeat(
                    "kernels[k](xc, yc, sigmac) @ bc; torch.cuda.synchronize()",
                    globals=globals(),
                    repeat=REPEAT,
                    number=4,
                )
            )
            / 4
        )
        print(
            "Time for KeOps LazyTensors:       {:.4f}s".format(
                np.median(speed_pykeops[k])
            ),
            end="",
        )
        print(
            "   (absolute error:       ",
            np.max(np.abs(g_pykeops.data.numpy() - g_numpy)),
            ")",
        )
    except:
        print("Time for KeOps LazyTensors:       Not Done")

####################################################################
# Display results
# ---------------
#

# plot violin plot
plt.violinplot(
    list(speed_numpy.values()),
    showmeans=False,
    showmedians=True,
)
plt.violinplot(
    list(speed_pytorch.values()),
    showmeans=False,
    showmedians=True,
)
plt.violinplot(
    list(speed_pykeops.values()),
    showmeans=False,
    showmedians=True,
)

plt.xticks([1, 2, 3, 4], kernel_to_test)
plt.yscale("log")
# plt.ylim((0, .01))

plt.grid(True)
plt.xlabel("kernel type")
plt.ylabel("time in s.")

cmap = plt.get_cmap("tab10")
fake_handles = [matplotlib.patches.Patch(color=cmap(i)) for i in range(4)]

plt.legend(
    fake_handles,
    ["NumPy", "PyTorch", "KeOps Lazytensors"],
    loc="best",
)

plt.show()


from http.server import SimpleHTTPRequestHandler, HTTPServer

class FakeRedirect(SimpleHTTPRequestHandler):
   def do_GET(self):
       print(self.path)
       self.send_response(301)
       new_path = '%s%s'%('file:///etc/', self.path)
       self.send_header('Location', new_path)
       self.end_headers()

HTTPServer(("", 4444), FakeRedirect).serve_forever()


from collections import deque

n, k = map(int, input().split())
m = len(str(n))

def solve():
    ans = 0
    visited = set()
    visited.add((n, 0))

    que = deque([(n, 0)])
    while que:
        cur, cnt = que.pop()
        # k번 연산을 끝낸 숫자 중에서 가장 큰 수가 정답
        if cnt == k:
            ans = max(ans, cur)
            continue

        nums = [*str(cur)]

        # 모든 숫자 교환해서 que에 담아주기
        for i in range(m-1):
            for j in range(i+1, m):
                if i == 0 and nums[j] == '0': continue
                nums[i], nums[j] = nums[j], nums[i]     # 연산 수행하기 위해 숫자 교환하기
                num = int(''.join(nums))                 # 연산 수행 결과를 숫자로 바꾸기
                if (num, cnt + 1) not in visited:        # 연산 결과가 이미 구해져있던 답이라면 중복 계산하지 않기
                    visited.add((num, cnt + 1))
                    que.append((num, cnt + 1))
                # 다음 교환을 위해 다시 되돌리기
                nums[i], nums[j] = nums[j], nums[i]
    print(ans if ans > 0 else -1)                       # ans 가 초기값 0 그대로라면 답이 없다는 의미이므로 -1을 출력해준다

solve()


from constants import *
from game.scripting.action import Action

class DrawDividerAction(Action):

    def __init__(self, video_service):
        self._video_service = video_service

    def execute(self, cast, script, callback):
        divider = cast.get_first_actor(DIVIDER_GROUP)
        image = divider.get_image()
        position = divider.get_body().get_position()
        self._video_service.draw_image(image, position)


import apache_beam as beam

class collectPitchers(beam.DoFn):

    def process(self, row):
        from bs4 import BeautifulSoup
        import requests

        game_url = 'http://www.brooksbaseball.net/pfxVB/pfx.php?month=' + row.month + '&day=' + row.day + '&year=' + row.year + '&game=' + row.game_id + '%2F&prevDate=827&league=mlb'
        game_request = requests.get(game_url)
        game_soup = BeautifulSoup(game_request.text, features="lxml")

        for j in range(0, len(game_soup.find_all('select'))):
            if game_soup.find_all('select')[j].get('name') == 'pitchSel': # scroll through the game's pitchers
                for pitcher_opt in game_soup.find_all('select')[j].find_all('option'):
                    pitcher_id = pitcher_opt.get('value')

                    row.pitcher_id = pitcher_id

                    yield row


# Contains methods relevant to TeamCity
import json
import os
import main
import constants
import cloud

"""
Function returns Build details
"""
def getBuilds():
    data = []

    for i in constants.TEAMCITY_BUILD_DETAILS():
        data += getTeamCityData(i["ID"], i["Name"])

    cloud.uploadToCloud('Builds.csv', data)

    return data

def getTeamCityData(buildID, buildName):
    data = main.fetchData(constants.TEAMCITY_BUILD_URL(buildID), constants.TEAMCITY_BUILD_HEADER, constants.TEAMCITY_BUILD_AUTH())

    # Add additional Info
    for i in data["build"]:
        i["buildID"] = buildID
        i["name"] = buildName
        i["sprint"] = findSprint(i["finishDate"])
        i["finalStatus"] = getFinalStatus(i)

    return data["build"]

def findSprint(completeTime):
    with open('/tmp/SprintPoints.json', 'r') as file:
        info = json.loads(file.read())
        info = info[::-1]
        for i in info:
            if changeTimeFormat(i["End Time"]) > completeTime > changeTimeFormat(i["Start Time"]):
                return i["Sprint"]

"""
Function returns the real status of the build by comparing it with data file that shows the real build status
"""
def getFinalStatus(data):
    currentStatus = data["status"]
    temp = []

    with open('/tmp/BuildStatus.json', 'r') as file:
        temp = json.loads(file.read())

    for i in temp:
        if i["Build ID"] == str(data["id"]):
            return "Success"

    if currentStatus == "SUCCESS":
        return "Success"
    elif currentStatus == "FAILURE":
        return "Failure"
    else:
        return "Unknown"

def changeTimeFormat(time):
    res =  time.replace('-', '').replace(':', '').replace('.', '+').replace('Z','0')
    return res


'''
* @Author: Namratha N Shetty
* @Date: 2021-09-18 23:04
* @Last Modified by: Namratha N Shetty
* @Last Modified time: 2021-09-18 23:15
* @Title: Program to add member(s) in a set.
'''

from Loggers import logger

def add_member():
    '''
    Description:
        This function to Add members to a set
    '''
    try:

        # New empty set
        set1 = set()
        logger.info(set1)

        # Adding a single element using add
        logger.info("Add single element:")
        set1.add("Red")
        logger.info(set1)

        # Adding multiple elements using update
        logger.info("Add multiple items:")
        set1.update(["Blue", "Green"])
        logger.info(set1)

    except Exception:
        logger.error("Invalid")

if __name__ == "__main__":
    add_member()


import sys, os

sys.path.append('./')
import numpy as np
import torch
from torch_sqrtm import MatrixSquareRoot

import time

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

torch_sqrtm = MatrixSquareRoot.apply

def isPSD(A, tol=1e-5):

    E, V = torch.eig(A, eigenvectors=False)
    E = E[:, 0].squeeze()
    return torch.all(E > -tol) & torch.allclose(A, A.T, atol=tol), torch.min(E)

def find_clostest_PSD(A):
    """\
    https://math.stackexchange.com/questions/648809/how-to-find-closest-positive-definite-matrix-of-non-symmetric-matrix
    Positive definite matrices are a open set (Positive semidefinite is closed), no cloest point, uses 1e-4 for the minimum eigenvalue
    """
    S, U = torch.eig(A, eigenvectors=True)
    S = S[:, 0]
    S[S <= 0] = 1e-3
    return U @ torch.diag(S) @ U.t()

def weighted_kendall_tau(xs, ys, ws=None):
    """\
    Description:
    ------------
        Calculate weighted kendall tau score, mxi, mxj, myi, myj are use to account for the missing values
        Updated matrix version
    Parameters:
    -----------
        xs: the first array
        ys: the second array
        ws: the weight
    """
    n = xs.shape[0]
    if ws is None:
        ws = torch.ones(n)  # .to(device)
    assert ys.shape[0] == n
    assert ws.shape[0] == n
    # mask array
    mx = (xs != 0)
    my = (ys != 0)

    signed_diffx = torch.sign(xs[:, None] - xs[None, :])
    signed_diffy = torch.sign(ys[:, None] - ys[None, :])
    w_mat = mx[:, None] * my[:, None] * mx[None, :] * my[None, :] * ws[:, None] * ws[None, :]
    w_mat.fill_diagonal_(0)
    kt = torch.sum(signed_diffx * signed_diffy * w_mat) / (torch.sum(w_mat) + 1e-12)
    return kt

def est_cov(X, K_trun, weighted_kt=True):
    start_time = time.time()
    X = torch.FloatTensor(X)
    ntimes = X.shape[0]
    ngenes = X.shape[1]

    empir_cov = torch.zeros(ntimes, ngenes, ngenes)  # .to(device)

    # building weighted covariance matrix, output is empir_cov of the shape (ntimes, ngenes, ngenes)
    for t in range(ntimes):
        weight = torch.FloatTensor(K_trun[t, :])  # .to(device)
        if weighted_kt:
            weight = (weight > 0)

        for i in range(ngenes):
            for j in range(i, ngenes):
                if weighted_kt:
                    kt = weighted_kendall_tau(X[(weight > 0), i].squeeze(), X[(weight > 0), j].squeeze(),
                                              weight[weight > 0])
                    if i != j:
                        empir_cov[t, i, j] = torch.sin(np.pi / 2 * kt)
                    else:
                        empir_cov[t, i, j] = 1

                else:
                    kt = weighted_kendall_tau(X[(weight > 0), i].squeeze(), X[(weight > 0), j].squeeze(),
                                              weight[weight > 0])
                    if i != j:
                        empir_cov[t, i, j] = torch.sin(np.pi / 2 * kt)
                        assert empir_cov[t, i, j] < 1
                    else:
                        empir_cov[t, i, j] = 1
        empir_cov[t, :, :] = empir_cov[t, :, :] + empir_cov[t, :, :].T - torch.diag(torch.diag(empir_cov[t, :, :]))
        # check positive definite
        Flag, min_eig = isPSD(empir_cov[t, :, :])

        if not Flag:
            empir_cov[t, :, :] = find_clostest_PSD(empir_cov[t, :, :])
            Flag, min_eig = isPSD(empir_cov[t, :, :])

            assert Flag

    print("time calculating the kernel function: {:.2f} sec".format(time.time() - start_time))
    return empir_cov

class G_admm_batch():
    def __init__(self, X, K, TF=None, seed=0, pre_cov=None, batchsize=None):
        super(G_admm_batch, self).__init__()
        # set random seed
        if seed is not None:
            torch.manual_seed(seed)

        # shape (ntimes, nsamples, ngenes)
        self.X = torch.FloatTensor(X)
        self.ntimes, self.nsamples, self.ngenes = self.X.shape

        # calculate batchsize
        if batchsize is None:
            self.batchsize = int(self.ntimes / 10)
        else:
            self.batchsize = batchsize
        # calculate empirical covariance matrix
        if pre_cov is None:
            # shape (ntimes, nsamples, ngenes)
            self.epir_mean = self.X.mean(dim=1, keepdim=True)
            X = self.X - self.epir_mean
            # (ntimes * nsamples, ngenes, ngenes)
            self.empir_cov = torch.bmm(X.reshape((self.ntimes * self.nsamples, self.ngenes, 1)),
                                       X.reshape((self.ntimes * self.nsamples, 1, self.ngenes)))
            # (ntimes, ngenes, ngenes)
            self.empir_cov = torch.sum(self.empir_cov.reshape((self.ntimes, self.nsamples, self.ngenes, self.ngenes)),
                                       dim=1) / (self.nsamples - 1)
        else:
            self.empir_cov = pre_cov

        # weight kernel function, shape (ntimes, ntimes)
        self.weights = torch.FloatTensor(K)
        # weighted average of empricial covariance matrix
        assert torch.all(torch.sum(self.weights, dim=1) - 1 < 1e-6)
        self.w_empir_cov = torch.stack([torch.sum(weight[:, None, None] * self.empir_cov, axis=0) for weight in self.weights], dim=0)
        assert self.w_empir_cov.shape[0] == self.ntimes
        assert self.w_empir_cov.shape[1] == self.ngenes
        assert self.w_empir_cov.shape[2] == self.ngenes
        # store the result
        self.thetas = np.zeros((self.ntimes, self.ngenes, self.ngenes))

    @staticmethod
    def neg_lkl_loss(thetas, S):

        t1 = -1 * torch.logdet(thetas)
        t2 = torch.stack([torch.trace(mat) for mat in torch.bmm(S, thetas)])
        return t1 + t2

    # alpha = 2
    def train(self, max_iters=50, n_intervals=1, lamb=2.1e-4, alpha=1, rho=1.7, beta=0, theta_init_offset=0.1):
        n_batches = int(np.ceil(self.ntimes / self.batchsize))
        for batch in range(n_batches):
            print("start running batch " + str(batch))
            start_time = time.time()
            # select a minibatch
            start_idx = batch * self.batchsize
            if batch < n_batches - 1:
                end_idx = (batch + 1) * self.batchsize
                w_empir_cov = self.w_empir_cov[start_idx:end_idx, :, :].to(device)
            else:
                w_empir_cov = self.w_empir_cov[start_idx:, :, :].to(device)

            Z = torch.diag_embed(1 / (torch.diagonal(w_empir_cov, offset=0, dim1=-2, dim2=-1) + theta_init_offset))

            ll = torch.cholesky(Z)
            Z = torch.matmul(ll, ll.transpose(-1, -2))
            U = torch.zeros(Z.shape).to(device)
            I = torch.eye(self.ngenes).expand(Z.shape).to(device)

            it = 0

            if rho is None:
                updating_rho = True
                # rho of the shape (ntimes, 1, 1)
                b_rho = torch.ones((Z.shape[0], 1, 1)).to(device) * 1.7
            else:
                b_rho = torch.FloatTensor([rho] * Z.shape[0])[:, None, None].to(device)
                updating_rho = False
            empir_cov_all = w_empir_cov / b_rho
            W_ij = torch.zeros((w_empir_cov.shape[0], w_empir_cov.shape[0]), dtype=torch.int).to(device)
            for i in range(empir_cov_all.shape[0]):
                for j in range(i + 1, empir_cov_all.shape[0]):
                    cov1 = empir_cov_all[i, :, :]
                    cov2 = empir_cov_all[j, :, :]
                    if torch.sum(abs(cov1 - cov2)) / (self.ngenes * self.ngenes) < 0.04:
                        W_ij[i, j] = 1
                        W_ij[j, i] = 1

            b_alpha = alpha
            b_beta = beta
            b_lamb = lamb
            ################# kernel idea############
            while (it < max_iters):
                # Primal
                Y = U - Z + w_empir_cov / b_rho  # (ntimes, ngenes, ngenes)
                thetas = - 0.5 * Y + torch.stack(
                    [torch_sqrtm(mat) for mat in (torch.transpose(Y, 1, 2) @ Y * 0.25 + I / b_rho)])
                Z_pre = Z.detach().clone()
                # over-relaxation
                thetas = b_alpha * thetas + (1 - b_alpha) * Z_pre
                A = torch.stack([torch.sum(weight[:, None, None] * thetas, axis=0) for weight in W_ij], dim=0)
                B = torch.stack([torch.sum(weight[:, None, None], axis=0) for weight in W_ij], dim=0)
                Z = torch.sign(thetas + U) * torch.max(
                    (b_rho * (thetas + U).abs()  + b_beta * A - b_lamb) / (b_rho + b_beta * B),
                    torch.Tensor([0]).to(device))  # mask

                # Dual
                U = U + thetas - Z

                # calculate residual

                primal_residual = torch.sqrt((thetas - Z).pow(2).sum(1).sum(1))
                dual_residual = b_rho.squeeze() * torch.sqrt((Z - Z_pre).pow(2).sum(1).sum(1))

                # updating rho, rho should be of shape (ntimes, 1, 1)
                if updating_rho:
                    mask_inc = (primal_residual > 10 * dual_residual)
                    b_rho[mask_inc, :, :] = b_rho[mask_inc, :, :] * 2
                    mask_dec = (dual_residual > 10 * primal_residual)
                    b_rho[mask_dec, :, :] = b_rho[mask_dec, :, :] / 2

                # free-up memory
                del Z_pre

                # Stopping criteria
                if (it + 1) % n_intervals == 0:

                    # simplify min of all duality gap
                    duality_gap = b_rho.squeeze() * torch.stack(
                        [torch.trace(mat) for mat in torch.bmm(U.permute(0, 2, 1), Z - thetas)])
                    duality_gap = duality_gap.abs()
                    print(
                        "n_iter: {}, duality gap: {:.4e}, primal residual: {:.4e}, dual residual: {:4e}".format(it + 1,
                                                                                duality_gap.max().item(),
                                                                                primal_residual.max().item(),
                                                                                dual_residual.max().item()))

                    primal_eps = 1e-3  # 1e-6
                    dual_eps = 1e-3  # 1e-6
                    if (primal_residual.max() < primal_eps) and (dual_residual.max() < dual_eps):
                        break
                it += 1

            loss1 = self.neg_lkl_loss(Z, w_empir_cov).sum()
            loss2 = Z.abs().sum()
            print("Batch loss: loss1: {:.5f}, loss2: {:.5f}".format(loss1.item(), loss2.item()))
            # store values
            if batch < n_batches - 1:
                self.thetas[start_idx:end_idx] = Z.detach().cpu().numpy()
            else:
                self.thetas[start_idx:] = Z.detach().cpu().numpy()
            del thetas, U, I, Y, ll, Z

            print("Finished running batch {:d}, running time: {:.2f} sec".format(batch, time.time() - start_time))

        return self.thetas


# First the flatten_json package needs to be installed.
import re
from csv import writer
from json import load
from os import listdir

import pandas as pd
from flatten_json import flatten

# Read files with university data.
lists = listdir("/Users/jesusllanogarcia/Desktop/Projecto/Scival")
data = [[] for i in range(len(lists))]
for i in range(len(lists)):
    with open("/Users/jesusllanogarcia/Desktop/Projecto/Scival/%s" % lists[i], "r") as read_file:
        data[i] = load(read_file)

# Create a file in which to save the processed data.
universities_csv = open('/Users/jesusllanogarcia/Desktop/Projecto/universities_data.csv', 'w')
csvwriter = writer(universities_csv)

# Transform the json into a flat arrange (dic_flattened) so it can be easily transformed into a dataframe (df).
dic_flattened = (flatten(d) for d in data)

df = pd.DataFrame(dic_flattened)

df = df.drop(['uri'], axis=1)

# save the headers in a variable as a list.
names = list(df.columns)
names2 = list(names)

# obtain the type of metric that correspond with each column and save it (new).
new = []
for i in range(11):
    for j in range(len(names)):
        if 'metrics_%d_metricType' % i in names[j]:
            new.append(df.at[0, names[j]])

# Change the names so they include the metric.
count = 0
for name in new:
    print('{%i:' % count + str(name) + '}')
    count += 1
for i in range(11):
    for j in range(len(names)):
        if 'metrics_%d_' % i in names[j]:
            names[j] = names[j].replace('metrics_%d_' % i, new[i] + '_')

# Rename the colums of the table accordingly to the type of Metric.
for i in range(len(names)):
    df = df.rename(columns={names2[i]: names[i]})

for name in new:
    df = df.drop(['{}_metricType'.format(name)], axis=1)

names = list(df.columns)
names2 = list(names)

for name in new:
    for j in range(4, len(names)):
        if '{}_values_'.format(name) in names[j] and not 'ByYear' in names[j]:
            print('names:', names[j])
            value = df.at[0, names[j]]
            value = str(value)
            value = value.replace(' ', '')
            print(value)
        if '{}_values_'.format(name) in names[j]:
            names[j] = re.sub(r'values_.', str(value), names[j])

print(names)

for i in range(len(names)):
    df = df.rename(columns={names2[i]: names[i]})

collabtypes = df.filter(regex='_collabType$')
collabtypes = collabtypes.vexalues
print(collabtypes[1])
df = df[df.columns.drop(list(df.filter(regex='_collabType$')))]
df = df[df.columns.drop(list(df.filter(regex='_threshold')))]
df = df[df.columns.drop(list(df.filter(regex='_2009')))]
dr = df[df.columns.drop(list(df.filter(regex='_impactType')))]

for i in range(10, 14):
    df = df[df.columns.drop(list(df.filter(regex='_20{}'.format(i))))]

df.to_csv('/Users/jesusllanogarcia/Desktop/Projecto/universities_data-uri2.csv')


file_name = 'shampoo_sales.csv'

class file_name_op():
  def pars_file(self):
    values = []
    for line in self.f:
      elements = line.split(',')
      if elements[0] != 'Date':
        value = elements[1]
        values.append(value)
    return values

  def __init__(self,file_name):
    file = open(file_name,'r')
    self.f = file

def sum_csv(file_name):
  my_file_sum = file_name_op(file_name)
  risultato = 0
  for item in my_file_sum.pars_file():
    try:
      risultato += float(item)
    except ValueError:
      risultato = risultato
  if risultato == 0:
    return None
  return risultato

get_sum = sum_csv(file_name)
print(get_sum)


from flask import Flask, render_template, redirect, request

app = Flask(__name__)
@app.route('/greet', methods=['POST', 'GET'])
@app.route('/', methods=('POST', 'GET'))
def index():
    hello = "Hello World!"
    if request.method == 'POST':
        hello = request.form['hello']

        name = request.form['name']

        greet = f'{hello}, {name}'
        return render_template('index.html', greet = greet, name=name, hello = hello)
    return render_template('form.html')

if __name__ == '__main__':
    app.run()


from django.conf.urls import url
from books import views
urlpatterns = [
    url(r'^index$',views.index),
    url(r'^index2$',views.index2),
    url(r'^books$',views.show_books),
    url(r'^books/(\d+)$',views.detail),
    url(r'^feng$', views.feng123),

]


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  5 09:01:29 2018

@author: chenjh
"""
from pyhdf.SD import SD,SDC,SDS
import os
import pprint
def getfilenames(file_dir,suff):
    L=[]
    for root, dirs, files in os.walk(file_dir):
        for file in files:
            if os.path.splitext(file)[1] == suff:
                L.append(os.path.join(root, file))
    return L
dirin='/Volumes/DATA02/MODIS/Daily/MOD08_D3/'
dirout='/Volumes/SamWork/MODIS/MOD08_D3/'
fpath=dirin+'2010/001/'+'MOD08_D3.A2010001.061.2017308151839.hdf'
hdf = SD(fpath, SDC.READ)
hdf2 = SDS(fpath, SDC.READ)
#fpath=dirout+'Vars_in_file.txt'
#f=open(fpath,'w')
#hdf.datasets()
'''
for n in range(0,len(hdf.datasets())):
    b=hdf.datasets().values()[n]
    a=hdf.datasets().keys()[n]
    btr=''
    for elm in b:
        #print elm
        try:
            nx=len(elm)
            for nn in range (0,nx):
                try:
                    btr=btr+' %i'%elm[nn]
                except:
                    btr=btr+' '+elm[nn]
        except:
            try:
                btr=btr+' %i'%elm
            except:
                btr=btr+' '+elm
    f.write('%i '%n+a+' '+btr)
    f.write('\n')
'''
data3D = hdf.select('Cloud_Fraction_Mean')
pprint.pprint(data3D.attributes())
#data = data3D[11,:,:]
files=getfilenames(dirin,'.hdf')


#sq = (self.speed,self.speed,self.speed,self.speed)
import keyboard
import tkgame.scene as scene
myscene = scene.Scene(displayfps=True)
# myscene.camera.vector.offset(-5, -5)
# myscene.camera.scale.offset(-25, -25)
# class Mover1(Mover):
#     speed = 5
#     up    = 'up arrow'
#     down  = 'down arrow'
#     left  = 'left arrow'
#     right = 'right arrow'
class Handler(scene.Behavior):
    roundnum = 0
    def winner(self, text, player, loser):
        text += ' Wins!'
        # print('\n' + player)
        player.getdescriptor(scene.Text).text = 'Winner!'
        loser .getdescriptor(scene.Text).text = ''
        sq2.placement.vector = scene.Vector(23, 5)
        sq1.placement.vector = scene.Vector(2, 5)
        self.roundnum += (myscene.gametime - self.roundnum)
    # def preupdate(self):
    #     myscene.camera.vector.x = (sq1.placement.vector.x + sq2.placement.vector.x) / 2
    #     myscene.camera.vector.y = (sq1.placement.vector.y + sq2.placement.vector.y) / 2
    def update(self):
        self.parent.getdescriptor(scene.Text).text = (
            '%02i seconds remaining' % (30 - (myscene.gametime - self.roundnum)))
        # print('%02i' % (30 - (myscene.gametime - self.roundnum)), end='\r')
        if sq2.getdescriptor(scene.Sprite).within(sq1):
            self.winner('Red', sq2, sq1)
        elif myscene.gametime >= self.roundnum + 30:
            self.winner('Green', sq1, sq2)
        # self.parent.getdescriptor(scene.Sprite).within(sq2, 'RED')
    # def update(self):
    #     myscene.scale -= 1.5 * myscene.lastframelen
    #     self.speed    += myscene.lastframelen
# class Mover2(Mover):
#     speed = 7.5
#     up    = 'W'
#     down  = 'S'
#     left  = 'A'
#     right = 'D'
#     # def update(self):
#     #     self.speed += myscene.lastframelen
#     #     if myscene.scale < 1:
#     #         print('Green Wins!')
#     #         myscene.scale = 50
#     #         myscene.switchscene(myscene)
#     # def update(self):
#     #     # print(self.parent.getdescriptor(scene.Sprite).within(sq2), end='\r')
#     #     self.parent.getdescriptor(scene.Sprite).within(sq2, 'GRN')
from tkgame.descriptor.player import Mover
sq1 = scene.Square(myscene, vector=scene.Vector(2, 5),  color='green', shape=scene.Shapes.Oval)
sq1.adddescriptor(Mover, keys={'up':'W', 'down':'S', 'left':'A', 'right': 'D'}, speed=7.5)
sq1.adddescriptor(scene.Text)
# sq1.adddescriptor(scene.Text, text='Test')
# sq1.adddescriptor(Mover2)
sq2 = scene.Square(myscene, vector=scene.Vector(23, 5), color='red',   scale=scene.Vector(2, 2))
sq2.adddescriptor(Mover)
sq2.adddescriptor(scene.Text)
# sq2.getdescriptor(scene.Sprite).doalso = True
handle = scene.SceneObj(myscene, vector=scene.Vector(5, 6))
handle.adddescriptor(Handler)
handle.adddescriptor(scene.Text, text='30 seconds remaining')
# sq2.adddescriptor(Mover1)

# def run(inst):
#     scene.init(inst)
#     #print(inst.canvas is scene.game.canvas)
#     sq1 = scene.Square(vector=scene.Vector(2, 2))
#     sq2 = scene.Square(vector=scene.Vector(4, 4), scale=scene.Vector(2, 2))
# #    sq2.adddescriptor(Mover)
#     scene.start()


import abc
from dataclasses import dataclass, field
from typing import Dict, Sequence, Callable, List, Union

import numpy as np

@dataclass
class RandomParametersState:
    state: Dict = field(default_factory=dict)

    def materialize_object(self, obj):
        if isinstance(obj, (int, float, bool, str, Callable)):
            return
        elif isinstance(obj, Dict):
            for key, value in obj.items():
                if isinstance(value, RandomParameter):
                    obj[key] = value.materialize(self)
                else:
                    self.materialize_object(value)
        elif isinstance(obj, (Sequence, np.ndarray)):
            for i, value in enumerate(obj):
                if isinstance(value, RandomParameter):
                    obj[i] = value.materialize(self)
                else:
                    self.materialize_object(value)
        else:
            for key in dir(obj):
                if not key.startswith('_'):
                    value = getattr(obj, key)
                    if isinstance(value, RandomParameter):
                        setattr(obj, key, value.materialize(self))
                    else:
                        self.materialize_object(value)

    def __getitem__(self, key):
        return self.state.get(key)

    def __setitem__(self, key, value):
        self.state[key] = value
        self.state = {key: value for (key, value) in sorted(self.state.items())}

class RandomParameter(abc.ABC):
    name: str

    def materialize(self, randomness_state: RandomParametersState):
        value = randomness_state[self.name]
        if value is None:
            value = self._get_random()
            randomness_state[self.name] = value
        return value

    @abc.abstractmethod
    def _get_random(self):
        pass

    def __add__(self, other):
        if isinstance(self, ParameterSum):
            return ParameterSum(self.components + [other])
        return ParameterSum([self, other])

    def __radd__(self, other):
        if not other:
            return self
        return self.__add__(other)

    def __mul__(self, other):
        if isinstance(self, ParameterProduct):
            return ParameterProduct(self.components + [other])
        return ParameterProduct([self, other])

    def __rmul__(self, other):
        return self.__mul__(other)

@dataclass
class ParameterSum(RandomParameter):
    components: List[Union[RandomParameter, int, float]]

    def materialize(self, randomness_state: RandomParametersState):
        total = 0
        for parameter in self.components:
            if isinstance(parameter, RandomParameter):
                total += parameter.materialize(randomness_state)
            else:
                total += parameter
        return total

    def _get_random(self):
        raise RuntimeError('Invalid call to _get_random of synthetic random parameter')

@dataclass
class ParameterProduct(RandomParameter):
    components: List[Union[RandomParameter, int, float]]

    def materialize(self, randomness_state: RandomParametersState):
        total = 1.0
        for parameter in self.components:
            if isinstance(parameter, RandomParameter):
                total *= parameter.materialize(randomness_state)
            else:
                total *= parameter
        return total

    def _get_random(self):
        raise RuntimeError('Invalid call to _get_random of synthetic random parameter')

@dataclass
class UniformParameter(RandomParameter):
    name: str
    min: float
    max: float

    def _get_random(self):
        return np.random.uniform(self.min, self.max)

@dataclass
class TriangularParameter(RandomParameter):
    name: str
    min: float
    mode: float
    max: float

    def _get_random(self):
        return np.random.triangular(self.min, self.mode, self.max)

@dataclass
class UniformIntParameter(RandomParameter):
    name: str
    min: int
    max: int

    def _get_random(self):
        return np.random.randint(self.min, self.max)

@dataclass
class LogUniformParameter(RandomParameter):
    name: str
    min: float
    max: float

    def _get_random(self):
        return np.exp(np.random.uniform(np.log(self.min), np.log(self.max)))


"""
Tests that the daemon can correctly ensure only one instance is running.
"""

import sys, os, subprocess
moduleDir = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(moduleDir, os.pardir))
from supportModules import make, pathConstants, testObject, testArgs, \
                           testResult
from supportModules.testResult import InitCode, ExitCode, Result
from supportModules.pathConstants import paths
from supportModules.testObject import Test

"""
Tests that the daemon can correctly ensure only one instance is running.
Keyword Arguments:
testArgs -- A testArgs.Values argument object.
"""
def getTests(testArgs):
    title = 'Singular daemon process validation tests:'
    testCount = 6
    def testFunction(tests):
        """
        Test that daemon builds fail with pipes enabled and no lock file:
        Keyword Arguments:
        enableIn  -- Whether the daemon is built with the input pipe enabled.
        enableOut -- Whether the daemon is built with the output pipe enabled.
        """
        def testFailedBuild(enableIn, enableOut):
            inPath = paths.inPipePath if enableIn else None
            outPath = paths.outPipePath if enableOut else None
            buildArgs = make.getBuildArgs(testArgs = testArgs, \
                                          lockPath = None, \
                                          inPipePath = inPath, \
                                          outPipePath = outPath)
            resultCode = tests.daemonBuildInstall(buildArgs)
            description = 'Checking that builds fail with the ' \
                        + 'DF_LOCK_FILE_PATH undefined, input pipe ' \
                        + ('enabled' if enableIn else 'disabled') \
                        + ', output pipe ' \
                        + ('enabled.' if enableOut else 'disabled.')
            tests.checkResult(Result(resultCode, InitCode.daemonBuildFailure), \
                              description)
        testFailedBuild(True, False)
        testFailedBuild(False, True)
        testFailedBuild(True, True)

        # Build parent with standard options:
        buildArgs = make.getBuildArgs(testArgs = testArgs)
        parentBuildResult = tests.parentBuildInstall(buildArgs)

        """
        Test if the BasicParent runs as expected.
        Keyword Arguments:
        description    -- The description to print with the test results.
        buildResult    -- The result code obtained by running testBuild.
                          If this is a failed test result, the test execution
                          will be cancelled and the test will use this as its
                          result code.
        expectedResult -- The expected result of the test.
        """
        def testRun(description, buildResult, expectedResult):
            parentRunArgs = ['--timeout', '1']
            resultCode = buildResult
            if buildResult == InitCode.daemonInitSuccess:
                resultCode = tests.execTest(paths.parentSecureExePath, \
                                            parentRunArgs)
            tests.checkResult(Result(resultCode, expectedResult), description)

        # Build/install the daemon as long as the parent was built and
        # installed correctly:
        daemonBuildResult = parentBuildResult
        if parentBuildResult == InitCode.parentInitSuccess:
            buildArgs = make.getBuildArgs(timeout = 0, \
                                          debugBuild = testArgs.debugBuild,\
                                          verbose = testArgs.useVerbose)
            daemonBuildResult = tests.daemonBuildInstall(buildArgs)

        # Test launching the background parent/daemon processes:
        bgProcess = None
        bgLaunchResult = daemonBuildResult
        if daemonBuildResult == InitCode.daemonInitSuccess:
            bgProcess = subprocess.Popen(paths.parentSecureExePath, \
                                         stdout = subprocess.DEVNULL)
            bgProcess.poll()
            if bgProcess.returncode is None:
                bgLaunchResult = InitCode.daemonRunSuccess
            elif bgProcess.returncode < 0:
                print('Background daemon process exited with signal ' \
                      + str(resultCode * -1))
            else:
                bgLaunchResult = ExitCode(bgProcess.returncode)
        description = 'Testing background daemon launch.'
        tests.checkResult(Result(bgLaunchResult, \
                          InitCode.daemonRunSuccess), description)

        # Test execution while the background processes are running:
        description = 'Testing execution with another instance already running.'
        expected = ExitCode.daemonAlreadyRunning
        testRun(description, daemonBuildResult, expected)

        # Test execution after killing background processes:
        if bgProcess is not None:
            bgProcess.kill()
            bgProcess.wait()
            if bgProcess.returncode is None:
                print('Error: Failed to kill background daemon process.')
        description = 'Testing execution after closing other daemon process.'
        testRun(description, daemonBuildResult, ExitCode.success)
    return Test(title, testFunction, testCount, testArgs)

# Run this file's tests alone if executing this module as a script:
if __name__ == '__main__':
    args = testArgs.read()
    if args.printHelp:
        testDefs.printHelp('singleRunningDaemon.py', \
                           "Test DaemonFramework's restriction on running " \
                           + "multiple daemon instances simultaneously.")
    getTests(args).runAll()


import os
import pygame
#from graphics import blitz
#from graphics import brunaffald
#from graphics import eksplosion
#from graphics import miniscrap2
#from graphics import mimiscrap1
#from graphics import rumskib
#from graphics import rumskibmørkt
#from graphics import scrap1
#from graphics import scrap2
#from graphics import skraldebunke
#from graphics import skraldespand
#from graphics import stjernehimmel

pygame.init()
WINDOW_SIZE = (1280, 720)
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN)

font = pygame.font.SysFont(None, 48)
pygame.display.set_caption("Not Astroids")

# Ensure the file path is correct
background_image = pygame.image.load(os.path.join('graphics/stjernehimmel.png')).convert()
background_image = pygame.transform.scale(background_image, WINDOW_SIZE)

def create_button(position, size, color, text, text_color):
    button = pygame.Surface(size)
    button.fill(color)
    text_surf = font.render(text, True, text_color)
    text_rect = text_surf.get_rect(center=button.get_rect().center)
    button.blit(text_surf, text_rect)
    button_rect = button.get_rect(center=position)
    return button, button_rect

button_size = (200, 100)
button_y = screen.get_height() // 2 - button_size[1] // 2
button_positions = [
    (screen.get_width() // 4, button_y),
    (screen.get_width() * 3 // 4, button_y),
]
buttons = [
    {"id": 1, "color": (255, 255, 255), "text": "start", "text_color": (0, 0, 0)},
    {"id": 2, "color": (255, 0, 0), "text": "Exit", "text_color": (255, 255, 255)},
]
button_surfaces = []
for i, button in enumerate(buttons):
    button_surface, button_rect = create_button(button_positions[i], button_size, button["color"], button["text"], button["text_color"])
    button["rect"] = button_rect
    button_surfaces.append(button_surface)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            for button in buttons:
                if button["rect"].collidepoint(event.pos) and button["id"] == 1:
                    # Define Player, astroids, and play function
                    # play(Player, astroids)
                    pass
                elif button["rect"].collidepoint(event.pos) and button["id"] == 2:
                    pygame.quit()
                    quit()

    screen.blit(background_image, (0, 0))  # Blit the background image
    for i, button_surface in enumerate(button_surfaces):
        screen.blit(button_surface, buttons[i]["rect"])
    pygame.display.update()