"""
Authored by: Adam Hollow - DevOps Technician, Devices.
For: South Ayrshire Council

"""

import xml.etree.ElementTree as ET
import os
from os import path
import win32api
import win32net
import adConnect
import win32con
import datetime

filePath = r'\\PATH_TO_STORAGE_FOLDER\inf_' + adConnect.full_name + '.xml'

def initial():
    if path.exists(filePath):
        pass
    else:
        root = ET.Element("Launch")
        b = ET.SubElement(root, "Count")
        c = ET.SubElement(b, "Number")
        d = ET.SubElement(b, "Date")
        e = ET.SubElement(b, "Flag")
        e.text = "False"
        c.text = "0"
        d.text = str(datetime.datetime.now())[:10]
        tree = ET.ElementTree(root)
        tree.write(filePath)

def checkDate():
    tree = ET.parse(filePath)
    root = tree.getroot()
    recDate = root[0][1].text
    nowDate = str(datetime.datetime.now())[:10]
    Dates = [recDate, nowDate]
    x = 0
    for Date in Dates:
        dateParseDate = [Date[:4], Date[5:7], Date[8:10]]
        dateParsedDate = (dateParseDate[0] + dateParseDate[1] + dateParseDate[2])
        Dates[x] = datetime.date(int(dateParsedDate[0:4]), int(dateParsedDate[4:6]), int(dateParsedDate[6:8]))
        x += 1
    datedelta = Dates[1] - Dates[0]
    if datedelta.days < 180:
        return False
    else:
        return True

def checkFlag():
    tree = ET.parse(filePath)
    root = tree.getroot()
    flag = root[0][2].text
    if flag == 'True':
        return True
    if flag == 'False':
        return False

def checkCount():
    tree = ET.parse(filePath)
    root = tree.getroot()
    num = int(root[0][0].text)
    if num > 9:
        return True
    else:
        return False

def closedApp():
    tree = ET.parse(filePath)
    root = tree.getroot()
    root[0][0].text = str(int(root[0][0].text) + 1)
    tree.write(filePath)

def completed():
    tree = ET.parse(filePath)
    root = tree.getroot()
    root[0][0].text = 0
    tree.write(filePath)

def updateInfo():
    tree = ET.parse(filePath)
    root = tree.getroot()
    root[0][1].text = str(datetime.datetime.now())[:10]
    root[0][2].text = 'True'
    tree.write(filePath)


"""
https://www.acmicpc.net/problem/3067
"""

from sys import stdin
from typing import List

input = stdin.readline

t = int(input())

answers = [0] * t

def count_coins(n: int, coins: List[int], m: int):
    dp = [0] * (m + 1)

    for coin in coins:
        if coin > m:
            continue

        dp[coin] += 1

        for i in range(coin + 1, m + 1):
            dp[i] = dp[i] + dp[i-coin]

    return dp[-1]

for i in range(t):
    n = int(input())
    coins = list(map(int, input().split()))
    m = int(input())
    answers[i] = count_coins(n, coins, m)

print("\n".join(map(str, answers)))


# coding: utf-8

# # Introduction
#
# In this problem you will be analysing time series data. Specifically, the data that you will be working with has been collecting via Pittsburgh's TrueTime system which is available publicly here http://truetime.portauthority.org/bustime/login.jsp. If you're interested, you can request an API key and collect the data yourself (the process is just submitting a form), however we've already collected some smaller subset of the available data for the purposes of this assignment.

# ## Part 1 TrueTime dataset
#
# The bus data has been collected by querying the TrueTime API every minute. Each time, we make two requests:
#
# 1. We request vehicle information for every bus running on the 61A, 61B, 61C, and 61D bus routes.
# 2. We request all available time predictions for the CMU / Morewood bus stop in both outbound and inbound directions.
#
# The results are given as XML, which are consequently parsed and stored within a sqlite database with two tables, one for vehicles and one for predictions. The table for the vehicles is organized in the following manner.
#
# | | **vehicles**             |
# |----------|-------------|
# | vid      | vehicle identifier |
# | tmstmp | date and time of the last positional update of the vehicle |
# | lat | latitude position of the vehicle in decimal degrees |
# | lon | longitude position of the vehicle in decimal degrees |
# | hdg | heading of vehicle as a 360 degree value (0 is north, 90 is east, 180 is south, and 270 is west |
# | pid | pattern ID of trip currently being executed |
# | rt | route that is currently being execute |
# | des | destination of the current trip |
# | pdist | linear distance (feet) vehicle has traveled into the current pattern |
# |  spd | speed as reported from the vehicle in miles per hour |
# | tablockid | TA's version of the scheduled block identifier for work currently behind performed |
# | tatripid | TA's version of the scheduled trip identifier for the vehicle's current trip |
#
# The table for the predictions is organized in the following manner
#
# | | **predictions** |
# |---|---|
# | tmstmp | date and time the prediction was generated |
# | typ | type of prediction (A for arrival, D for a departure) |
# | stpnm | display name of the stop for which this prediction was generated |
# | stpid | unique identifier representing the stop for which this prediction was generated |
# | vid | unique ID of the vehicle for which this prediction was generated |
# | dstp | linear distance (feet) left to be traveled by the vehicle before it reaches the stop for which this prediction was generated |
# | rt | route for which this prediction was generated |
# | rtdd | language-specific route designator meant for display |
# | rtdir | direction of travel of the route associated with this prediction |
# | des | final destination of the vehicle associated with this prediction |
# | prdtm | predicted date and time of a vehicle's arrival or departure to the stop associated with this prediction |
# | dly | true if the vehicle is delayed, only present if the vehicle that generated this prediction is delayed |
# | tablockid | TA's version of the scheduled block identifier for work currently behind performed |
# | tatripid | TA's version of the scheduled trip identifier for the vehicle's current trip |
#

# First you will need to read in the data. We have dumped the raw form of the data into a sqlite database, which you can read directly into a pandas dataframe.
#
# Since this data has not been processed at all by the course staff, you will need to fix and canonicalize a few things.
#
# ### Specification
#
# 1. Sometimes the TrueTime API returns a bogus result that has all the attributes but empty strings for all the values. You should inspect the data for clearly useless entries and remove all offending rows.
#
# 2. If you check the datatype of each column, you'll notice that most columns are stored as objects. However, some of these columns are in fact integers or floats, and if you wish to run numerical functions on them (like with numpy) you'll need to convert the columns to the correct type. Note that strings show up as objects. This is because the underlying implementation of Pandas uses numpy arrays, which need fixed-size entries, so they store pointers to strings. Your dataframe datatypes should match the following order and types (your datatypes may be 32bit instead of 64bit depending on your platform):
#
#    ```python
#    >>> vdf.dtypes
#    vid                   int64
#    tmstmp       datetime64[ns]
#    lat                 float64
#    lon                 float64
#    hdg                   int64
#    pid                   int64
#    rt                   object
#    des                  object
#    pdist                 int64
#    spd                   int64
#    tablockid            object
#    tatripid              int64
#    dtype: object
#
#    >>> pdf.dtypes
#    tmstmp       datetime64[ns]
#    typ                  object
#    stpnm                object
#    stpid                 int64
#    vid                   int64
#    dstp                  int64
#    rt                   object
#    rtdd                 object
#    rtdir                object
#    des                  object
#    prdtm        datetime64[ns]
#    dly                    bool
#    tablockid            object
#    tatripid              int64
#    dtype: object
#    ```
#
# 3. As you may have noticed from the above data types, you should convert all timestamps to Pandas datetime objects.

# In[65]:

import pandas as pd
import sqlite3

# In[215]:

def load_data1(fname):
    con = sqlite3.connect(fname)
    cur = con.cursor()
    vdf = pd.read_sql('SELECT * FROM vehicles', con=con)
    pdf = pd.read_sql('SELECT * FROM predictions', con=con)
    vdf = vdf[vdf['tmstmp']!='']
    vdf[['vid','hdg','pid','pdist','spd','tatripid']] = vdf[['vid','hdg','pid','pdist','spd','tatripid']].astype(int)
    vdf[['lat','lon']] = vdf[['lat','lon']].apply(pd.to_numeric)
    vdf['tmstmp'] =  pd.to_datetime(vdf['tmstmp'])
    pdf = pdf[pdf['tmstmp']!='']
    pdf[['stpid','vid','dstp','tatripid']] = pdf[['stpid','vid','dstp','tatripid']].astype(int)
    pdf['tmstmp'] =  pd.to_datetime(pdf['tmstmp'])
    pdf['prdtm'] =  pd.to_datetime(pdf['prdtm'])
    pdf['dly'] = pdf['dly'].astype('bool')
    return vdf, pdf

def load_data(fname):
    conn = sqlite3.connect(fname)
    vehicles = pd.read_sql_query('select * from vehicles', conn)
    predictions = pd.read_sql_query('select * from predictions', conn)
    conn.close()

    vehicles = vehicles[vehicles['vid'] != '']
    predictions = predictions[predictions['vid'] != '']

    vehicles['tmstmp'] = pd.to_datetime(vehicles['tmstmp'],infer_datetime_format=True)
    predictions['tmstmp'] = pd.to_datetime(predictions['tmstmp'],infer_datetime_format=True)
    predictions['prdtm'] = pd.to_datetime(predictions['prdtm'],infer_datetime_format=True)

    vehicles[['vid', 'lat', 'lon', 'hdg', 'pid', 'pdist', 'spd', 'tatripid']] = (
        vehicles[['vid', 'lat', 'lon', 'hdg', 'pid', 'pdist', 'spd', 'tatripid']].apply(pd.to_numeric)
    )
    predictions[['stpid', 'vid', 'dstp', 'tatripid']] = predictions[['stpid', 'vid', 'dstp', 'tatripid']].apply(pd.to_numeric)

    predictions['dly'] = predictions['dly'].replace({'True': True, '': False})
    return vehicles, predictions
# In[216]:

# ## Part 2 Splitting Trips
#
# For this assignment, we will focus on the vehicles dataframe and come back to the predictions later. The next thing we will do is take the dataframe of vehicles and and split it into individual trips. Specifically, a trip is the sequence of rows corresponding to a single bus, typically at one minute intervals, from the start of its route to the end of its route. We will represent each trip as an individual dataframe, and create a list of trip dataframes to represent all the trips.
#
# ### Specification
# 1. All entries in a trip should belong to a single route, destination, pattern, and vehicle.
#
# 2. The entries in a trip should have (not strictly) monotonically increasing timestamps and distance traveled.
#
# 3. Each trip should be of maximal size. I.e. you should sort first by time, and secondarily by pdist, and use a drop in a pdist as an indication that a new trip has started.
#
# 3. Each trip should have the timestamp set as the index, named `tmstmp`

# In[217]:

def split_trips1(df):
    nl = []
    df = df.sort_values(['rt','des','pid','vid','tmstmp','pdist'])
    df = df.reset_index(drop=True)
    si = -1
    ei = -1
    for i in range(len(df)):
        if i == 0:
            si = 0
            ei = 0
        if i!=0 and i!=len(df)-1 and df.iloc[i-1,8]<=df.iloc[i,8] and df.iloc[i-1,0]==df.iloc[i,0]:
            ei += 1
        elif i!=0 and i!=len(df)-1 and (df.iloc[i-1,8]>df.iloc[i,8] or df.iloc[i-1,0]!=df.iloc[i,0]):
            nl.append(df.iloc[si:ei+1,:])
            ei+=1
            si=ei
        elif i == len(df)-1:
            nl.append(df.iloc[si:,:])
    for i in range(len(nl)):
        nl[i].set_index('tmstmp')
    return nl

def split_trips(df):
    def split(trip):
        if not increasing(trip.tmstmp) or not increasing(trip.pdist):
            trip = trip.sort_values(['tmstmp','pdist'], ascending=[True, True])
        indices = [0]
        i = 1
        while i < len(trip):
            if trip['pdist'].iloc[i] < trip['pdist'].iloc[i-1]:
                indices.append(i)
            i+=1
        indices.append(i)
        return [trip[a:b].set_index('tmstmp') for (a,b) in zip(indices[:-1], indices[1:])]
    def increasing(L):
        return all(x<=y for x,y in zip(L[:-1],L[1:]))

    trips = []
    for vid in df['vid'].unique():
        df0 = df[df['vid']==vid]
        for pid in df0['pid'].unique():
            df1 = df0[df0['pid']==pid]
            trips += split(df1)
    return trips

# You should verify that your code meets the specification above. Use this cell to write tests that check the validity of your resulting output. For example,you should test that the total number of datapoints in the list of dataframes is the same as the number of datapoints in the original dataframe.

# ## Part 3 Sliding Averages
#
# Let's compute a basic statistic for time series / sequential data, which is the sliding average. Sliding averages are typically used to smooth out short-term fluctuations to see the long-term patterns.
#
# While it would be fairly simple to directly construct a list of all the sliding averages from the existing dataset, in reality, new TrueTime bus data is constantly becoming available. We want to avoid recomputing all the sliding averages every time a new data point comes in. Thus, instead of storing an unbounded list of datapoints, we will construct a class which does constant time updates as new data comes in.
#
# ### Specifications
# 1. Your function should not use more than O(k) memory, where k is the number of elements to average.
# 2. Each update should do O(1) work.
# 3. We will use a centered sliding average: we will average the k values both before and after the center point, averaging a total of 2k+1 elements. Note that k=0 will just return the stream without any averaging.
# 4. Since the average depends on both past and future elements, the `update` function will not be able to output anything useful for the first k elements. You should output `None` during these iterations. We suggest you signify the end of the stream by calling `update(None)` k times, during which you should output the last k sliding averages.
# 4. When at the beginning or end of a list, just compute the average of elements that exist.
# 5. As usual, you should test the correctness of your code. You can do this in the same cell or make a new cell.
#
# Note: you may find the `collections.deque` data structure to be helpful.
#
# Example:
# ```python
# >>> compute_sliding_averages(pd.Series([1,2,3,4,5]),1)
# pdf.Series([1.5, 2.0, 3.0, 4.0, 4.5])
# ```

# In[273]:

from collections import deque

class SlidingAverage:
    def __init__(self,k):
        """ Initializes a sliding average calculator which keeps track of the average of the last k seen elements.

        Args:
            k (int): the number of elements to average (the half-width of the sliding average window)
        """

        self.k = k
        self.window = deque()
        for i in range(2*k+1):
            self.window.append(None)
        self.sumbetween = 0
        self.counter = 0

    def update(self,x):
        """ Computes the sliding average after having seen element x

        Args:
            x (float): the next element in the stream to view

        Returns:
            (float): the new sliding average after having seen element x, if it can be calculated
        """

        if self.counter < self.k and x!=None:
            self.sumbetween += x
            self.counter+=1
            self.window.popleft()
            self.window.append(x)
            return None

        elif self.counter < 2*self.k+1 and x!=None:
            self.sumbetween += x
            self.counter+=1
            self.window.popleft()
            self.window.append(x)
            return self.sumbetween/self.counter

        elif self.counter == 2*self.k+1 and x!=None:
            pop = self.window.popleft()
            self.sumbetween = self.sumbetween-pop+x
            self.window.append(x)
            return self.sumbetween/self.counter

        elif self.counter <= 2*self.k+1 and x == None:
            pop = self.window.popleft()
            if pop != None:
                self.sumbetween = self.sumbetween-pop
                self.counter = self.counter-1
            return self.sumbetween/self.counter

def compute_sliding_averages(s, k):
    """ Computes the sliding averages for a given Pandas series using the SlidingAverage class.

    Args:
        s (pd.Series): a Pandas series for which the sliding average needs to be calculated
        k (int): the half-width of the sliding average window

    Returns:
        (pd.Series): a Pandas series of the sliding averages

    """
    a = pd.Series([])
    l = len(s)
    sa = SlidingAverage(k)

    count = 0
    for num in s:
        new_s = sa.update(num)
        if new_s != None:
            count+=1
            a = a.set_value(count-1, new_s)
            if count==l:
                return a
    for i in range(k):
        if count==l:
            return a
        new_s = sa.update(None)
        count+=1
        a = a.set_value(count-1, new_s)

#         print(a)
    return a

# #     sa = SlidingAverage(k)
#     a = pd.Series(sa.update(num) for num in s)
#     count = len(a)
#     for i in range(k):
#         new_s = sa.update(None)
#         if new_s != None:
#             count+=1
#             a = a.set_value(count-1, new_s)
#     return a

# In[271]:

from collections import deque

class SlidingAverage:
    def __init__(self,k):
        """ Initializes a sliding average calculator which keeps track of the average of the last k seen elements.

        Args:
            k (int): the number of elements to average (the half-width of the sliding average window)
        """
        self.k = k
        self.window = deque()
#         self.flag = 0
        self.sumbetween = 0
        self.counter = 0
        self.cnone = 0

    def update(self,x):
        """ Computes the sliding average after having seen element x

        Args:
            x (float): the next element in the stream to view

        Returns:
            (float): the new sliding average after having seen element x, if it can be calculated
        """
        if self.counter >= self.k and self.cnone <= self.k:
            if self.counter == 2*self.k+1:
                if x!=None:
                    pop = self.window.popleft()
                    self.window.append(x)
                    self.sumbetween=self.sumbetween-pop+x
                    return self.sumbetween/self.counter
                elif x==None:
                    pop = self.window.popleft()
                    self.sumbetween=self.sumbetween-pop
                    self.counter-=1
                    self.cnone+=1
                    return self.sumbetween/self.counter
            else:
                if x!=None:
                    self.window.append(x)
                    self.counter+=1
                    self.sumbetween=self.sumbetween+x
                    return self.sumbetween/self.counter
                else:
                    if self.cnone+self.counter==2*self.k+1:
                        pop = self.window.popleft()
                        self.cnone+=1
                        self.counter-=1
                        self.sumbetween -= pop
                        return self.sumbetween/self.counter
                    else:
                        self.cnone+=1
                        return self.sumbetween/self.counter
        else:
            if x!=None:
                self.window.append(x)
                self.counter+=1
                self.sumbetween=self.sumbetween+x
                return None
            else:
                self.cnone+=1
                return None

def compute_sliding_averages(s, k):
    """ Computes the sliding averages for a given Pandas series using the SlidingAverage class.

    Args:
        s (pd.Series): a Pandas series for which the sliding average needs to be calculated
        k (int): the half-width of the sliding average window

    Returns:
        (pd.Series): a Pandas series of the sliding averages

    """
    a = pd.Series([])
    l = len(s)
    sa = SlidingAverage(k)

    count = 0
    for num in s:
        if count==l:
            return a
        new_s = sa.update(num)
        if new_s != None:
            count+=1
            a = a.set_value(count-1, new_s)

    for i in range(k):
        if count == l:
            return a
        new_s = sa.update(None)
        count+=1
        a = a.set_value(count-1, new_s)
#         print(a)
    return a

#     sa = SlidingAverage(k)
#     a = pd.Series(sa.update(num) for num in s)
#     count = len(a)
#     for i in range(k):
#         new_s = sa.update(None)
#         if new_s != None:
#             count+=1
#             a = a.set_value(count-1, new_s)
#     return a

# Test your code!

# ## Part 4 Time Series Visualizations
#
# Time series data is typically displayed as signals over time. For example, this could be the speed of the bus over time, or the number of minutes behind or ahead of schedule a bus is.

# In[206]:

import matplotlib
# Use svg backend for better quality
# AUTOLAB_IGNORE_START
matplotlib.use("svg")
# AUTOLAB_IGNORE_STOP
import matplotlib.pyplot as plt
# AUTOLAB_IGNORE_START
get_ipython().magic('matplotlib inline')
plt.style.use('ggplot')
matplotlib.rcParams['figure.figsize'] = (10.0, 5.0) # you should adjust this to fit your screen
# AUTOLAB_IGNORE_STOP

# As the first example, you'll plot the speed of the bus as a function of time. Here, we'll overlay multiple routes on a single plot. Can you determine the direction of the bus (to or away from downtown) from the signal?
#
# ### Specification:
# 1. Plot the sliding average speed of each bus, using a new line for each bus.
# 2. Return a list of the resulting `Line2D` objects plot. The order of the line objects should correspond with the order of the trips.
# 3. Do not call `plt.show()` inside the function. Autolab will not X out of any plotted images.
# 4. We want you to plot the bus as a function of time, and not of datetime (these are different python types) which is the index of your dataframe. You can get the time with df.index.time.

# In[214]:

def plot_trip(trips, k):
    """ Plots the sliding average speed as a function of time

    Args:
        trip (list): list of trip DataFrames to plot
        k (int): the half-width of the sliding average window
    """
#     b = []

#     trips.index = trips.index.time
#     return plt.plot(trips[0],trips.loc["spd"])

# Play around with these values. Can you differentiate the buses going towards downtown from the buses going away from downtown?

# AUTOLAB_IGNORE_STOP

# We can also gain information from overall trends from averaging many data points. In the following function, you will plot the average speed of all buses at regular time intervals throughout the day.
#
# ### Specification
# 1. You should group the rows of the dataframe by taking the timestamp modulo t and ignoring the day/month/year (since the data was collected at 1 minute intervals, this means that t=1 corresponds to averaging one entry per day recorded).
# 2. Return the PathCollection object of your plot. For example, if you create the plot using the matplotlib command `scatter(...)`, return the result of this function call.
# 3. Do not call `plt.show()` inside the function. Autolab will not X out of any plotted images.

# In[213]:

import datetime

def plot_avg_spd(df, t):
    """ Plot the average speed of all recorded buses within t minute intervals
    Args:
        df (pd.DataFrame): dataframe of bus data
        t (int): the granularity of each time period (in minutes) for which an average is speed is calculated
    """
    pass

# AUTOLAB_IGNORE_START

# AUTOLAB_IGNORE_STOP

Time Series Analysis/PA Port Authority Data - Regression Modeling/time_series.py


#!/usr/bin/env python3

import socket
import time
import argh

@argh.arg('port', type=int)
def wait_port(port):
    s = socket.socket()
    while True:
        try:
            s.connect(('localhost', port))
            break
        except ConnectionRefusedError:
            time.sleep(0.1)

if __name__ == '__main__':
    argh.dispatch_command(wait_port)


######## Main code ###############
# Python program with Paralleization

import os
import concurrent.futures
import multiprocessing as mp
import time as time
from multiprocessing import Process, Manager

# Step 1: Init multiprocessing.Pool()
pool = mp.Pool(mp.cpu_count())
print ('Number of cores',mp.cpu_count())

start=time.perf_counter()

def Search(text,pattern):
  M = len(pattern)
  N = len(text)
  #print(pattern)
  #print(text)
  # build lps[] to hold the longest prefix and suffix.
  # patterns values
  lps = [0]*M
  j = 0 # index for pattern[]

  # calculate lps[] array
  computeLPSArray(pattern, M, lps)

  i = 0 # index for text[]

  while i < N:
    if pattern[j] == text[i]:
      i += 1
      j += 1

    if j == M:
      print ("Found pattern at index", str(i-j))
      j = lps[j-1]

    # mismatch after j matches
    elif i < N and pattern[j] != text[i]:
      #Avoid matching the characters lps[0..lps[j-1]], as they will match anyway.
      if j != 0:
        j = lps[j-1]
      else:
        i += 1

def computeLPSArray(pattern, m, lps):
  len = 0 # previous longest prefix-suffix length

  lps[0] # lps[0] is always 0
  i = 1

  # lps[i] is calculated in the loop for i = 1 to m-1.
  while i < m:
    if pattern[i]== pattern[len]:
      len += 1
      lps[i] = len
      i += 1
    else:
      if len != 0:
        len = lps[len-1]

        # Also, note that we do not increment i here
      else:
        lps[i] = 0
        i += 1

################ Code to retrieve text and pattern #######
print(os.getcwd())
path = os.getcwd()

inputfile_path = os.path.join(path, "Python_Master.txt")
with open(inputfile_path) as masterFile:
    number_of_loop = 0
    for line in masterFile:

        # reading each word
        character = line.split()
        # Now open respective text file and pattern file and capture their content
        text_file_name = "textfile" + character[0]
        pattern_file_name = "pattern" + character[1]
        pattern_file_path = os.path.join(path, "pattern", pattern_file_name + "." + 'txt')
        text_file_path = os.path.join(path, "textfile", text_file_name + "." + 'txt')

        #print("Round is: ",number_of_loop)
        with open(pattern_file_path) as patternFile:
            for line in patternFile:
                pattern = line
        with open(text_file_path) as textFile:
            for line in textFile:
                text = line
        number_of_loop = number_of_loop + 1
        Search(text,pattern)
data=[]
# Step 2: `pool.apply` the `Search()`
results = [pool.apply(Search, args=(text,pattern)) for row in data]
# Step 3: Don't forget to close
pool.close()

'''
#We can do Paralleization using below method also
#method 2: Paralleization

 with concurrent.futures.ProcessPoolExecutor() as executer:
results=executer.map(Search,text,pattern)
 '''

finish=time.perf_counter()
print(f'Finished in {round(finish-start,5)} second(s)')


import sys, os
import timeit
from functions import *

root_path = os.getcwd() # get path to directory
input_path = sys.argv[1] # get first arg (must be relative path to input file)
output_path = "/data/output"

N = None
D = None
seq = None
res = 0

def read():
    global root_path, input_path, N, D, seq
    in_file = open(root_path + input_path, 'r')
    N, D, seq = read_file(in_file)
    in_file.close()

def number_from(file):
    num = ""
    s = file.read(1)
    if s == "": # EOF
        return None
    while s != " " and s != "\n":   # read by symbol
        num += s
        s = file.read(1)
    return int(num)

# добавил оптимизации, работу с файлом
# Занятие 5 10.3 связный поиск в линейных списках
def process():
    global N, D, seq, res

    # with open(root_path + input_path, 'r') as in_file:
    #     line = in_file.readline().split()
    #     N, D = int(line[0]), int(line[1])

    #     curr_ind = 0
    #     next_ind = 1
    #     curr_num = number_from(in_file) + D # read first number
    #     curr_pos = in_file.tell() # save next number pos
    #     next_pos = curr_pos
    #     is_last_iter = False

    #     while curr_ind < N:
    #         seq_flag = False
    #         curr_count = 0
    #         in_file.seek(next_pos)

    #         for i in range(next_ind, N):
    #             tmp_pos = in_file.tell()
    #             num = number_from(in_file) # read number

    #             if curr_num < num:
    #                 if not seq_flag:
    #                     next_ind = i
    #                     next_pos = tmp_pos
    #                 break
    #             elif curr_num == num:
    #                 if not seq_flag:
    #                     next_ind = i
    #                     next_pos = tmp_pos # hold on pos on first seq number
    #                     seq_flag = True
    #                 curr_count += 1
    #             else:
    #                 if i == N-1:
    #                     is_last_iter = True # curr_num > num and i == N-1; no more possible

    #         curr_ind += 1
    #         if curr_ind < N:
    #             # optimisation for sequenses
    #             prev_num = curr_num
    #             while curr_num is not None:
    #                 res += curr_count
    #                 in_file.seek(curr_pos)
    #                 curr_num = number_from(in_file)
    #                 if curr_num is not None:
    #                     curr_num += D
    #                     curr_pos = in_file.tell()
    #                     if curr_num != prev_num:
    #                         break

    #         # just optimisation
    #         if is_last_iter:
    #             break

    curr_ind = 0
    next_ind = 1
    curr_num = seq[curr_ind] + D
    prev_num = curr_num
    is_last_iter = False

    while curr_ind < N:
        seq_flag = False
        curr_count = 0

        for i in range(next_ind, N):
            if curr_num < seq[i]:
                if not seq_flag:
                    next_ind = i
                break
            elif curr_num == seq[i]:
                if not seq_flag:
                    next_ind = i
                    seq_flag = True
                curr_count += 1
            else:
                if i == N-1:
                    is_last_iter = True # curr_num > num and i == N-1; no more possible

        if curr_ind < N:
            # optimisation for sequenses
            prev_num = curr_num
            while curr_ind < N:
                res += curr_count
                curr_ind += 1
                if curr_ind == N:
                    break
                curr_num = seq[curr_ind] + D
                if curr_num != prev_num:
                    break

        # just optimisation
        if is_last_iter:
            break

def output():
    global root_path, output_path, res
    out_file = open(root_path + output_path, 'w')
    out_file.write("{0}".format(res))
    out_file.close()

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

read_time = timeit.timeit(read, number=1)
process_time = timeit.timeit(process, number=1)
output_time = timeit.timeit(output, number=1)

print("Reading time: {0}".format(read_time))
print("Processing time: {0}".format(process_time))
print("Output time: {0}".format(output_time))
print("Total time: {0}".format(read_time + process_time + output_time))

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


import heapq

class Solution:
    def minimumCost(self, start: List[int], target: List[int], specialRoads: List[List[int]]) -> int:
        specialRoads = [[a, b, c, d, x] for a, b, c, d, x in specialRoads if x < abs(a - c) + abs(b - d)]
        dist = {(start[0], start[1]): 0}
        heap = [(0, start[0], start[1])]
        while len(heap) > 0:
            currdist, x, y = heapq.heappop(heap)
            for a, b, c, d, cost in specialRoads:
                if dist.get((c, d), float('inf')) > currdist + abs(x - a) + abs(y - b) + cost:
                    dist[(c, d)] = currdist + abs(x - a) + abs(y - b) + cost
                    heapq.heappush(heap, (dist[(c, d)], c, d))
        res = abs(target[0] - start[0]) + abs(target[1] - start[1])
        for a, b, c, d, cost in specialRoads:
            res = min(res, dist.get((c, d), float('inf')) + abs(target[0] - c) + abs(target[1] - d))
        return res


"""Color classification [colorful colorization, https://richzhang.github.io/colorization/]"""
import os
import torch
from torch import nn
from torch.utils.data import DataLoader, Dataset
from torch import optim
from PIL import Image
import torchvision
from tqdm import tqdm
import kornia

device = "cuda"
LATENT_DIM = 100
IMAGE_SIZE = 128
BATCH_SIZE = 16

class Down(nn.Module):
    def __init__(self, in_channels, out_channels):
        super().__init__()
        self.fw = nn.Sequential(
            nn.Conv2d(in_channels=in_channels,
                      out_channels=out_channels,
                      kernel_size=(5,5),
                      padding=2),
            nn.BatchNorm2d(num_features=out_channels),
            nn.GELU(),
            nn.Conv2d(in_channels=out_channels,
                      out_channels=out_channels,
                      kernel_size=(5,5),
                      stride=(2,2),
                      padding=2),
            nn.BatchNorm2d(num_features=out_channels),
            nn.LeakyReLU(0.2)
          )

    def forward(self, x):
        return self.fw(x)

class Up(nn.Module):
    """Strided up convolution, resolution is doubled"""
    def __init__(self, in_channels, out_channels, activation=nn.LeakyReLU(0.2)):
        super().__init__()
        self.fw = nn.Sequential(
          nn.ConvTranspose2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=4, stride=2, padding=1),
          nn.BatchNorm2d(num_features=out_channels),
          nn.LeakyReLU(0.2),
          nn.Conv2d(in_channels=out_channels,
                    out_channels=out_channels,
                    kernel_size=(5,5),
                    padding=2),
          nn.BatchNorm2d(num_features=out_channels),
          activation,
        )

    def forward(self, x):
        return self.fw(x)

class Colorizer(nn.Module):
    """The colorizer"""
    def __init__(self):
        super().__init__()
        self.encoder = nn.Sequential(
          Down(   1,   8),
          Down(   8,  32),
          Down(  32, 128),
          Down( 128, 512),
        )
        self.decoder = nn.Sequential(
          Up( 512, 128),
          Up( 128,  32),
          Up(  32,  16),
          Up(  16,  400, nn.LogSoftmax(dim=1)),
        )

    def forward(self, x):
        x = self.encoder(x)
        return self.decoder(x).view(-1,400,IMAGE_SIZE*IMAGE_SIZE)

class ImageDataset(Dataset):
  def __init__(self, path):
    self.path = path
    self.imgs = os.listdir(path)

    self.transform = torchvision.transforms.Compose([
        torchvision.transforms.Resize((IMAGE_SIZE,IMAGE_SIZE),
          interpolation=torchvision.transforms.InterpolationMode.BILINEAR),
        torchvision.transforms.Lambda(lambda x: x.type(torch.float)),
        torchvision.transforms.Normalize(128, 128),
        # Avoid 100% black or white as that will create ugly artifacts during color space transformation
        torchvision.transforms.Lambda(lambda x: (x + 1.0) / 2.0),
        torchvision.transforms.Lambda(lambda x: 0.1 + 0.8*x)
    ])

  def __len__(self):
    return len(self.imgs)

  def __getitem__(self, idx):
    img = self.transform(torchvision.io.read_image(os.path.join(self.path, self.imgs[idx])))

    yuv = kornia.color.yuv.rgb_to_yuv(img)

    u = torch.clip(torch.floor((yuv[1,:,:] + 0.5) * 20.0), 0, 19)
    v = torch.clip(torch.round((yuv[2,:,:] + 0.5) * 20.0), 0, 19)
    color_bin = (u * 20 + v)

    return img, yuv[0, :, :].view(1,IMAGE_SIZE,IMAGE_SIZE)*2-1, color_bin.view(1, IMAGE_SIZE, IMAGE_SIZE)

def recover_uv(color_bin):
  u = torch.div(color_bin, 20, rounding_mode='floor') / 20.0 - 0.5
  v = torch.remainder(color_bin, 20) / 20.0 - 0.5
  return u, v

dataloader = DataLoader(ImageDataset("./train/dogs"), batch_size=BATCH_SIZE, shuffle=True)
MODEL_PATH = "colorful.pt"

class Trainer:
    """ The training class for the colorizer network"""
    def __init__(self):
        self.model = Colorizer().to(device)
        self.optimizer = torch.optim.Adam(self.model.parameters(), lr=0.0001)

        self.start_epoch = 0
        try:
            checkpoint = torch.load(MODEL_PATH)

            self.model.\
              load_state_dict(checkpoint['model_state_dict'])
            self.optimizer.\
              load_state_dict(checkpoint['optimizer_state_dict'])

            self.start_epoch = checkpoint['epoch']
        except:
            print("Could not load model from disk, starting from scratch")

        self.calculate_loss_weights()

    def save_images(self, epoch, original_sample, bw_sample, estimated_bins):
        """Generate some images and save them to disk for review"""

        bw_sample=(bw_sample[:8,:,:].view(8,1,IMAGE_SIZE, IMAGE_SIZE)+1)/2

        u, v = recover_uv(estimated_bins)
        print(estimated_bins.size())
        reconstructed_sample=torch.cat([bw_sample, u[:8,:,:], v[:8,:,:]], dim=1)
        reconstructed_sample=kornia.color.yuv.yuv_to_rgb(reconstructed_sample)

        bw_sample=torch.concat([bw_sample, bw_sample, bw_sample], dim=1)

        img = torch.concat([original_sample[:8,:,:], bw_sample, reconstructed_sample], dim=0)

        grid = torchvision.utils.make_grid(img, nrow=8)
        im = torchvision.transforms.ToPILImage()(grid)
        im.save("colorful_epoch_{}.png".format(epoch))

    def calculate_loss_weights(self):
        try:
          cp = torch.load("colorful_weights.pt")
          self.weights = cp["weights"].to(device)
          return
        except:
          print("Could not load color bin weights, re-calculating")

        total = torch.zeros(400)
        # Calculate the histogram of all color bins
        for original, bw, color_bin in tqdm(dataloader):
          hist, bins = torch.histogram(color_bin, bins=400, range=(0, 400))
          total += hist

        # Turn into a distribution
        dist = total / torch.sum(total)

        # Smooth with a gaussian kernel (compare original paper)
        dist = dist.view(1, 20, 20)
        width=9
        sigma=5
        distance = torch.arange(
            -(width // 2), width // 2 + 1, dtype=torch.float, device="cpu"
        )
        gaussian = torch.exp(
            -(distance[:, None] ** 2 + distance[None] ** 2) / (2 * sigma ** 2)
        )
        gaussian /= gaussian.sum()
        kernel = gaussian[None, None].expand(1, -1, -1, -1)
        dist = torch.nn.functional.conv2d(dist, kernel, padding=width//2, groups=1)
        dist = (dist.view(1,1,400) + (1/400)) / 2
        dist = dist.view(400)
        weights = 1.0 / dist
        weights /= weights.sum()

        print(weights)
        torch.save({
          "weights": weights
          }, "colorful_weights.pt")

        self.weights = weights.to(device)

    def train(self):
        """Train some epochs"""
        loss_function = torch.nn.NLLLoss(weight=self.weights)

        for epoch in range(self.start_epoch+1, 25000):
            bar = tqdm(dataloader)
            total_loss = 0
            cnt = 0
            for original_sample, bw_sample, color_bins in bar:
              bw_sample = bw_sample.to(device)
              color_bins = color_bins.to(device).view(-1,IMAGE_SIZE*IMAGE_SIZE).type(torch.long)
              self.optimizer.zero_grad()
              x = self.model(bw_sample)
              loss = loss_function(x, color_bins)
              loss.backward()
              total_loss += loss.item()
              cnt += 1
              self.optimizer.step()
              bar.set_description("epoch {}, loss {:.8f}".format(epoch, total_loss / cnt*1000))

            original_sample, bw_sample, _ = next(iter(dataloader))
            x = self.model(bw_sample.to(device)).view(-1, 400, IMAGE_SIZE, IMAGE_SIZE)
            x = torch.argmax(x, dim=1).view(-1,1, IMAGE_SIZE, IMAGE_SIZE)
            self.save_images(epoch, original_sample.to("cpu"), bw_sample.to("cpu"), x.to("cpu"))

            torch.save({
                      'epoch': epoch,
                      'model_state_dict':
                        self.model.state_dict(),
                      'optimizer_state_dict':
                        self.optimizer.state_dict(),
                      }, MODEL_PATH)

trainer = Trainer()
trainer.train()


#!/usr/bin/env python
# update web page with links to log files

import os
import re

# function to update web
def updateWeb (directory):

    # grab head and tail from index.html
    webfile = os.path.join(directory, 'index.html')
    f = open(webfile)
    webhead = ''
    webtail = ''
    head = True
    tail = False
    for line in f.readlines():
        if re.search(r'
', line):
            tail = True
        if head:
            webhead += line
        if tail:
            webtail += line
        if re.search(r'
', line):
            head = False
    f.close()

    # re-write the head
    f = open(webfile, 'wt')
    f.write(webhead)

    # write list of log files
    files = [file for file in os.listdir(directory) if file.startswith('log_') and
             file.endswith('.txt')]
    for file in files:
        f.write('
' + grab_date(file) + ' log file
\n')

    # re-write the tail
    f.write(webtail)

def grab_date (filename):
    return re.findall(r'\d+-\d+', filename)[0]

if __name__ == '__main__':
    updateWeb('/mnt/sd/arduino/www')


import pygame
import math

pygame.init()

FPS = 24
SIZE = width, height = 640, 480
SCREEN = pygame.display.set_mode(SIZE)
BULLETS = []
CLOCK = pygame.time.Clock()

class GameObject:
    def __init__(
        self,
        path_to_img,
        width=32,
        height=45,
        speed_x=0,
        speed_y=0,
        angle=0,
        x=0,
        y=0
    ):

        # we keep the original surface to compensate rotation
        # lost of image quality
        print(path_to_img)
        self.orig_surface = pygame.image.load(path_to_img)
        self.orig_surface = pygame.transform.scale(
            self.orig_surface, (width, height)
        )

        self.surface = self.orig_surface.copy()
        self.rect = self.surface.get_rect()

        self.rect.center = (x, y)

        # perform rotation without loosing image quality
        orig_center = self.rect.center
        self.surface = pygame.transform.rotate(self.surface, angle)
        self.rect = self.surface.get_rect(center=orig_center)

        # setting the initial values
        self.speed_x = speed_x
        self.speed_y = speed_y
        self.angle = 0
        self.projection_x = 1
        self.projection_y = 0

    def _perform_move(self):
        self.rect = self.rect.move([self.speed_x, self.speed_y])

    def update(self):
        print('angle: {}'.format(self.angle))
        print('projection_x: {}'.format(self.projection_x))
        print('projection_y: {}'.format(self.projection_y))
        print('speed_x: {}'.format(self.speed_x))
        print('speed_y: {}'.format(self.speed_y))
        print('\n')
        self._perform_move()

class Rocket(GameObject):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.jet = Jet(
            'assets/jet.png',
            width=20, height=30,
            angle=self.angle,
            x=self.rect.center[0],
            y=self.rect.bottom
        )
        self.is_accelerating = False

    def _perform_move(self):
        super()._perform_move()
        self.jet.rect = self.jet.rect.move([self.speed_x, self.speed_y])

    def _accelerate(self, val):
        self.speed_x += val * self.projection_x
        self.speed_y += val * self.projection_y

    @property
    def angle(self):
        return self._angle

    @angle.setter
    def angle(self, val):
        if abs(val) == 360:
            self._angle = 0

        else:
            self._angle = val

        if not val % 90:
            self.surface = pygame.transform.rotate(self.orig_surface, val)

    @property
    def rads(self):
        return self.angle * math.pi / 180

    def accept_direction(self, direction):
        if direction == 'right':
            self._accelerate(0)

            self.angle -= 10

            # perform rotation without loosing image quality
            orig_center = self.rect.center
            self.surface = pygame.transform.rotate(self.surface, -10)
            self.rect = self.surface.get_rect(center=orig_center)

        if direction == 'left':
            self._accelerate(0)

            self.angle += 10

            # perform rotation without loosing image quality
            orig_center = self.rect.center
            self.surface = pygame.transform.rotate(self.surface, 10)
            self.rect = self.surface.get_rect(center=orig_center)

        if direction == 'up':
            self._accelerate(1)

            if not self.is_accelerating:
                self.is_accelerating = True

        self.projection_x = -math.sin(self.rads)
        self.projection_y = -math.cos(self.rads)

    def fire(self):
        bullet_speed = 15

        bullet = Bullet(
            'assets/bullet.png',
            width=30, height=20,
            speed_x=self.projection_x * bullet_speed + self.speed_x,
            speed_y=self.projection_y * bullet_speed + self.speed_y,
            angle=self.angle - 90,
            x=self.rect.center[0],
            y=self.rect.center[1]
        )

        BULLETS.append(bullet)

class Bullet(GameObject):
    def exist(self):
        pass

class Jet(GameObject):
    def exist(self):
        pass

rocket = Rocket('assets/rocket.png', angle=-90)

while True:
    for event in pygame.event.get():
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                rocket.is_accelerating = False

    keys = pygame.key.get_pressed()

    if keys[pygame.K_RIGHT]:
        rocket.accept_direction('right')

    if keys[pygame.K_LEFT]:
        rocket.accept_direction('left')

    if keys[pygame.K_UP]:
        rocket.accept_direction('up')

    if keys[pygame.K_SPACE]:
        rocket.fire()

    rocket.update()

    CLOCK.tick(FPS)

    SCREEN.fill((0, 0, 0))
    SCREEN.blit(rocket.surface, rocket.rect)

    if rocket.is_accelerating:
        SCREEN.blit(rocket.jet.surface, rocket.jet.rect)

    for bullet in BULLETS:
        SCREEN.blit(bullet.surface, bullet.rect)
        bullet.update()

    pygame.display.flip()
    pygame.event.pump()


import pygame
from pygame.locals import (
    RLEACCEL,
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_ESCAPE,
    KEYDOWN,
    QUIT,
)

class Player(pygame.sprite.Sprite):
    def __init__(self):
        super(Player, self).__init__()
        player_img = pygame.image.load("player-imgs/0_Citizen_Idle_000.png").convert()
        self.image = pygame.transform.scale(player_img, (50, 75))
        self.rect = self.image.get_rect()

    def get_rect(self):
        return self.rect

    def detect_collision(self, ball):
        return self.rect.colliderect(ball.rect)


import numpy as np
import numpy as xp

import six
from six import __init__

def loc2bbox(src_bbox, loc):
    """Decode bounding boxes from bounding box offsets and scales.

    Given bounding box offsets and scales computed by
    :meth:`bbox2loc`, this function decodes the representation to
    coordinates in 2D image coordinates.

    Given scales and offsets :math:`t_y, t_x, t_h, t_w` and a bounding
    box whose center is :math:`(y, x) = p_y, p_x` and size :math:`p_h, p_w`,
    the decoded bounding box's center :math:`\\hat{g}_y`, :math:`\\hat{g}_x`
    and size :math:`\\hat{g}_h`, :math:`\\hat{g}_w` are calculated
    by the following formulas.

    * :math:`\\hat{g}_y = p_h t_y + p_y`
    * :math:`\\hat{g}_x = p_w t_x + p_x`
    * :math:`\\hat{g}_h = p_h \\exp(t_h)`
    * :math:`\\hat{g}_w = p_w \\exp(t_w)`

    The decoding formulas are used in works such as R-CNN [#]_.

    The output is same type as the type of the inputs.

    .. [#] Ross Girshick, Jeff Donahue, Trevor Darrell, Jitendra Malik. \
    Rich feature hierarchies for accurate object detection and semantic \
    segmentation. CVPR 2014.

    Args:
        src_bbox (array): A coordinates of bounding boxes.
            Its shape is :math:`(R, 4)`. These coordinates are
            :math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`.
        loc (array): An array with offsets and scales.
            The shapes of :obj:`src_bbox` and :obj:`loc` should be same.
            This contains values :math:`t_y, t_x, t_h, t_w`.

    Returns:
        array:
        Decoded bounding box coordinates. Its shape is :math:`(R, 4)`. \
        The second axis contains four values \
        :math:`\\hat{g}_{ymin}, \\hat{g}_{xmin},
        \\hat{g}_{ymax}, \\hat{g}_{xmax}`.

    """

    if src_bbox.shape[0] == 0:
        return xp.zeros((0, 4), dtype=loc.dtype)

    src_bbox = src_bbox.astype(src_bbox.dtype, copy=False)

    src_height = src_bbox[:, 2] - src_bbox[:, 0]
    src_width = src_bbox[:, 3] - src_bbox[:, 1]
    src_ctr_y = src_bbox[:, 0] + 0.5 * src_height
    src_ctr_x = src_bbox[:, 1] + 0.5 * src_width

    dy = loc[:, 0::4]
    dx = loc[:, 1::4]
    dh = loc[:, 2::4]
    dw = loc[:, 3::4]

    ctr_y = dy * src_height[:, xp.newaxis] + src_ctr_y[:, xp.newaxis]
    ctr_x = dx * src_width[:, xp.newaxis] + src_ctr_x[:, xp.newaxis]
    h = xp.exp(dh) * src_height[:, xp.newaxis]
    w = xp.exp(dw) * src_width[:, xp.newaxis]

    dst_bbox = xp.zeros(loc.shape, dtype=loc.dtype)
    dst_bbox[:, 0::4] = ctr_y - 0.5 * h
    dst_bbox[:, 1::4] = ctr_x - 0.5 * w
    dst_bbox[:, 2::4] = ctr_y + 0.5 * h
    dst_bbox[:, 3::4] = ctr_x + 0.5 * w

    return dst_bbox

def bbox2loc(src_bbox, dst_bbox):
    """Encodes the source and the destination bounding boxes to "loc".

    Given bounding boxes, this function computes offsets and scales
    to match the source bounding boxes to the target bounding boxes.
    Mathematcially, given a bounding box whose center is
    :math:`(y, x) = p_y, p_x` and
    size :math:`p_h, p_w` and the target bounding box whose center is
    :math:`g_y, g_x` and size :math:`g_h, g_w`, the offsets and scales
    :math:`t_y, t_x, t_h, t_w` can be computed by the following formulas.

    * :math:`t_y = \\frac{(g_y - p_y)} {p_h}`
    * :math:`t_x = \\frac{(g_x - p_x)} {p_w}`
    * :math:`t_h = \\log(\\frac{g_h} {p_h})`
    * :math:`t_w = \\log(\\frac{g_w} {p_w})`

    The output is same type as the type of the inputs.
    The encoding formulas are used in works such as R-CNN [#]_.

    .. [#] Ross Girshick, Jeff Donahue, Trevor Darrell, Jitendra Malik. \
    Rich feature hierarchies for accurate object detection and semantic \
    segmentation. CVPR 2014.

    Args:
        src_bbox (array): An image coordinate array whose shape is
            :math:`(R, 4)`. :math:`R` is the number of bounding boxes.
            These coordinates are
            :math:`p_{ymin}, p_{xmin}, p_{ymax}, p_{xmax}`.
        dst_bbox (array): An image coordinate array whose shape is
            :math:`(R, 4)`.
            These coordinates are
            :math:`g_{ymin}, g_{xmin}, g_{ymax}, g_{xmax}`.

    Returns:
        array:
        Bounding box offsets and scales from :obj:`src_bbox` \
        to :obj:`dst_bbox`. \
        This has shape :math:`(R, 4)`.
        The second axis contains four values :math:`t_y, t_x, t_h, t_w`.

    """

    height = src_bbox[:, 2] - src_bbox[:, 0]
    width = src_bbox[:, 3] - src_bbox[:, 1]
    ctr_y = src_bbox[:, 0] + 0.5 * height
    ctr_x = src_bbox[:, 1] + 0.5 * width

    base_height = dst_bbox[:, 2] - dst_bbox[:, 0]
    base_width = dst_bbox[:, 3] - dst_bbox[:, 1]
    base_ctr_y = dst_bbox[:, 0] + 0.5 * base_height
    base_ctr_x = dst_bbox[:, 1] + 0.5 * base_width

    eps = xp.finfo(height.dtype).eps
    height = xp.maximum(height, eps)
    width = xp.maximum(width, eps)

    dy = (base_ctr_y - ctr_y) / height
    dx = (base_ctr_x - ctr_x) / width
    dh = xp.log(base_height / height)
    dw = xp.log(base_width / width)

    loc = xp.vstack((dy, dx, dh, dw)).transpose()
    return loc

def bbox_iou(bbox_a, bbox_b):
    """Calculate the Intersection of Unions (IoUs) between bounding boxes.

    IoU is calculated as a ratio of area of the intersection
    and area of the union.

    This function accepts both :obj:`numpy.ndarray` and :obj:`cupy.ndarray` as
    inputs. Please note that both :obj:`bbox_a` and :obj:`bbox_b` need to be
    same type.
    The output is same type as the type of the inputs.

    Args:
        bbox_a (array): An array whose shape is :math:`(N, 4)`.
            :math:`N` is the number of bounding boxes.
            The dtype should be :obj:`numpy.float32`.
        bbox_b (array): An array similar to :obj:`bbox_a`,
            whose shape is :math:`(K, 4)`.
            The dtype should be :obj:`numpy.float32`.

    Returns:
        array:
        An array whose shape is :math:`(N, K)`. \
        An element at index :math:`(n, k)` contains IoUs between \
        :math:`n` th bounding box in :obj:`bbox_a` and :math:`k` th bounding \
        box in :obj:`bbox_b`.

    """
    if bbox_a.shape[1] != 4 or bbox_b.shape[1] != 4:
        raise IndexError

    # top left
    tl = xp.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
    # bottom right
    br = xp.minimum(bbox_a[:, None, 2:], bbox_b[:, 2:])

    area_i = xp.prod(br - tl, axis=2) * (tl < br).all(axis=2)
    area_a = xp.prod(bbox_a[:, 2:] - bbox_a[:, :2], axis=1)
    area_b = xp.prod(bbox_b[:, 2:] - bbox_b[:, :2], axis=1)
    return area_i / (area_a[:, None] + area_b - area_i)

def __test():
    pass

if __name__ == '__main__':
    __test()

def generate_anchor_base(base_size=16, ratios=[0.5, 1, 2],
                         anchor_scales=[8, 16, 32]):
    """Generate anchor base windows by enumerating aspect ratio and scales.

    Generate anchors that are scaled and modified to the given aspect ratios.
    Area of a scaled anchor is preserved when modifying to the given aspect
    ratio.

    :obj:`R = len(ratios) * len(anchor_scales)` anchors are generated by this
    function.
    The :obj:`i * len(anchor_scales) + j` th anchor corresponds to an anchor
    generated by :obj:`ratios[i]` and :obj:`anchor_scales[j]`.

    For example, if the scale is :math:`8` and the ratio is :math:`0.25`,
    the width and the height of the base window will be stretched by :math:`8`.
    For modifying the anchor to the given aspect ratio,
    the height is halved and the width is doubled.

    Args:
        base_size (number): The width and the height of the reference window.
        ratios (list of floats): This is ratios of width to height of
            the anchors.
        anchor_scales (list of numbers): This is areas of anchors.
            Those areas will be the product of the square of an element in
            :obj:`anchor_scales` and the original area of the reference
            window.

    Returns:
        ~numpy.ndarray:
        An array of shape :math:`(R, 4)`.
        Each element is a set of coordinates of a bounding box.
        The second axis corresponds to
        :math:`(y_{min}, x_{min}, y_{max}, x_{max})` of a bounding box.

    """
    py = base_size / 2.
    px = base_size / 2.

    anchor_base = np.zeros((len(ratios) * len(anchor_scales), 4),
                           dtype=np.float32)
    for i in six.moves.range(len(ratios)):
        for j in six.moves.range(len(anchor_scales)):
            h = base_size * anchor_scales[j] * np.sqrt(ratios[i])
            w = base_size * anchor_scales[j] * np.sqrt(1. / ratios[i])

            index = i * len(anchor_scales) + j
            anchor_base[index, 0] = py - h / 2.
            anchor_base[index, 1] = px - w / 2.
            anchor_base[index, 2] = py + h / 2.
            anchor_base[index, 3] = px + w / 2.
    return anchor_base


#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy import MetaData, Column, Table, PrimaryKeyConstraint
from sqlalchemy import String, DateTime, Float
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import datetime

Base = declarative_base()
metadata = MetaData()
engine = create_engine('postgresql:///siki', echo=False)

class pdata(Base):
    __table__ = Table('pdata', Base.metadata, autoload=True,
        autoload_with=engine)

session = sessionmaker()
session.configure(bind=engine)

# session and ORM
s = session()
# html
print ("""<!DOCTYPE html>

    <title>Python postgres table to  html</title>

""")
for row in s.query(pdata).filter(pdata.id.in_(['1', '2'])).order_by(pdata.id).all():
    print ("
%s	%.3f	%.3f	%s
" % (row.id, row.easting, row.northing, row.d.strftime('%Y-%m-%d %H:%m')))
print ("""

""")
s.close()


import sys

n = int(sys.stdin.readline())
money_unit = [0.25, 0.1, 0.05, 0.01]
answer = ''

for i in range(n):
    money = int(sys.stdin.readline()) / 100
    for unit in money_unit:
        for j in range(1, 101):
            if (unit * j) == money:
                answer += str(j) + " "
                money = round(money - (unit * (j)), 2)
                break
            elif (unit * j) >= money:
                answer += str(j - 1) + " "
                money = round(money - (unit * (j - 1)), 2)
                break

    print(answer)
    answer = ''

백준/Bronze/2720. 세탁소 사장 동혁/세탁소 사장 동혁.py

세탁소 사장 동혁.py


import os
import discord

# Login token for discord bot.
TOKEN = os.getenv('BROKE_PHI_BOT_TOKEN')

intents = discord.Intents.default()
intents.message_content = True

# Create the connection to discord.
client = discord.Client(intents=intents)

# Events are asynchronous and code is executed as a callback.
# Called when bot has finished logging in and setting things up.
@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

# Called when bot has recieved a message.
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('$hello'):
        await message.channel.send('Hello!')

# Run the bot with login token.
client.run(TOKEN)


## BOILERPLATE ##
import sys
from PySide import QtGui, QtCore
if QtCore.QCoreApplication.instance() is None:
    app = QtGui.QApplication(sys.argv)
    import qt4reactor
    qt4reactor.install()
## BOILERPLATE ##
from twisted.internet.defer import inlineCallbacks
from steppermotorclient import ChunkedStepperMotorClient
from goto import MIN, MAX, PRECISION, SLIDER, POI, GotoWidget
from qtutils.label import LabelWidget
from qtutils.qled import LEDWidget
from operator import index
from sitz import compose
from ab.abclient import getProtocol
from functools import partial
from qtutils.qled import LEDWidget

PARAMS = {
    MIN:-999999,
    MAX:999999,
    PRECISION:0,
    SLIDER:200,
    POI:{'none':0}
}

RATE_MIN = 1
RATE_MAX = 1000

class StepperMotorWidget(QtGui.QWidget):
    def __init__(self,protocol):
        QtGui.QWidget.__init__(self)
        self.setLayout(QtGui.QHBoxLayout())
        def onConnectionLost(reason):
            if reactor.running:
                QtGui.QMessageBox.information(self,'connection lost','connect to server terminated. program quitting.')
                self.close()
                reactor.stop()
                protocol.__class__.connectionLost(protocol,reason)
        protocol.connectionLost = onConnectionLost

        self.gotoWids = {}
        self.enbButtons = {}
        self.sms = {}
        #self.LEDs = {}

        @inlineCallbacks
        def onInit():
            config = yield protocol.sendCommand('get-configuration')
            self.show()

            #sort the list of dgNames based on guiOrder key in config
            sorted_sms = list()
            for smName in config.keys():
                sorted_sms.append((smName,config[smName]['guiOrder'],config[smName]))
            sorted_sms = sorted(sorted_sms, key=lambda x:x[1])

            #create a vertical layout for each sm (thisLayout), when done add to the full horizontal layout (self.layout())
            for id, guiOrder, config in sorted_sms:
                thisLayout = QtGui.QVBoxLayout()

                '''
                # add a ready LED indicator
                self.LEDs[id].toggle()
                self.LEDs[id] = LEDWidget(initialState=False)
                thisLayout.addLayout(self.LEDs[id])
                '''

                # establish the stepper motor protocol
                sm = ChunkedStepperMotorClient(protocol,id)
                self.sms[id] = sm

                #create a goto widget for this steppermotor
                PARAMS[POI] = config['pts_of_int']
                gotoWidget = GotoWidget(PARAMS)
                self.gotoWids[sm.id] = gotoWidget
                thisLayout.addWidget(gotoWidget)
                @inlineCallbacks
                def onGotoRequested(sm,payload):
                    position, deferred = payload
                    yield sm.setPosition(int(position))
                    deferred.callback(None)
                gotoWidget.gotoRequested.connect(partial(onGotoRequested,sm))
                #gotoWidget.spin.editingFinished.connect(partial(onGotoRequested,sm))
                sm.addListener(sm.POSITION,gotoWidget.setPosition)
                def onUpdateRequested(stepperMotor,gw):
                    stepperMotor.getPosition().addCallback(gw.setPosition)
                gotoWidget.updateRequested.connect(partial(onUpdateRequested,sm,gotoWidget))
                gotoWidget.cancelRequested.connect(sm.cancel)
                position = yield sm.getPosition()
                gotoWidget.setPosition(position)

                '''
                #create an enable toggle button for this sm
                enableButton = QtGui.QPushButton('enable', self)
                self.enbButtons[sm.id] = enableButton
                enableButton.clicked.connect(sm.toggleStatus)
                #handle external application toggling this sm
                def adjustText(sm,status):
                    if status == 'enabled':
                        self.enbButtons[sm.id].setText('disable')
                        self.gotoWids[sm.id].setEnabled(True)
                    elif status == 'disabled':
                        self.enbButtons[sm.id].setText('enable')
                        self.gotoWids[sm.id].setEnabled(False)
                sm.addListener(sm.ENABLE,partial(adjustText,sm))

                #disable enable button if this sm doesn't have that functionality
                if config['enable_channel'] == None: enableButton.setEnabled(False)
                else: gotoWidget.setEnabled(False)
                thisLayout.addWidget(enableButton)
                '''

                #create a spinbox to control the step rate for this sm
                rate = yield sm.getStepRate()
                rateSpin = QtGui.QSpinBox()
                thisLayout.addWidget(LabelWidget('rate',rateSpin))
                rateSpin.editingFinished.connect(
                    compose(
                        sm.setStepRate,
                        rateSpin.value
                    )
                )
                rateSpin.setRange(
                    RATE_MIN if rate > RATE_MIN else rate,
                    RATE_MAX if rate < RATE_MAX else rate
                )
                rateSpin.setValue(rate)
                sm.addListener(sm.RATE,rateSpin.setValue)

                #add this steppermotor panel to the gui
                self.layout().addWidget(
                    LabelWidget(
                        config['name'],
                        thisLayout
                    )
                )
        onInit()

    def closeEvent(self, event):
        event.accept()
        quit()

@inlineCallbacks
def main(container):
    # check if debugging / testing
    import sys
    DEBUG = len(sys.argv) > 1 and sys.argv[1] == 'debug'

    #from sitz import STEPPER_MOTOR_SERVER, TEST_STEPPER_MOTOR_SERVER
    from config.serverURLs import STEPPER_MOTOR_SERVER, TEST_STEPPER_MOTOR_SERVER
    from ab.abclient import getProtocol
    protocol = yield getProtocol(
        TEST_STEPPER_MOTOR_SERVER
        if DEBUG else
        STEPPER_MOTOR_SERVER
    )
    widget = StepperMotorWidget(protocol)
    container.append(widget)
    widget.setWindowTitle('%s stepper motor gui' % ('debug' if DEBUG else 'real'))

if __name__ == '__main__':
    from twisted.internet import reactor
    container = []
    main(container)
    reactor.run()


"""
Plots results from the removal experiment.
"""
import os
import argparse

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import sem

def set_size(width, fraction=1, subplots=(1, 1)):
    """
    Set figure dimensions to avoid scaling in LaTeX.
    """
    golden_ratio = 1.618
    height = (width / golden_ratio) * (subplots[0] / subplots[1])
    return width, height

def get_results(args):
    """
    Return results.
    """

    res_dir = os.path.join(args.in_dir, args.dataset, args.tree_type, args.tree_kernel)
    assert os.path.exists(res_dir)

    res = {}
    for i in args.rs:
        res[i] = np.load(os.path.join(res_dir, 'rs{}'.format(i), 'results.npy'),
                         allow_pickle=True)[()]
    return res

def get_mean(res, name='aucs'):
    """
    Return mean array from each result dictionary.
    """

    vals = []
    for k in res.keys():
        vals.append(res[k][name])

    vals_arr = np.vstack(vals)
    vals_mean = np.mean(vals_arr, axis=0)
    vals_sem = sem(vals_arr, axis=0)

    return vals_mean, vals_sem

def main(args):

    # matplotlib settings
    plt.rc('font', family='serif')
    plt.rc('xtick', labelsize=15)
    plt.rc('ytick', labelsize=15)
    plt.rc('axes', labelsize=16)
    plt.rc('axes', titlesize=16)
    plt.rc('legend', fontsize=18)
    plt.rc('legend', title_fontsize=11)
    plt.rc('lines', linewidth=1)
    plt.rc('lines', markersize=6)

    # inches
    width = 5.5  # Neurips 2020
    width, height = set_size(width=width * 1.25, fraction=1, subplots=(1, 1))
    fig, ax = plt.subplots(1, 1, figsize=(width, height / 1.5))

    res = get_results(args)
    n_removed, _ = get_mean(res, name='n_remove')
    score_mean, score_sem = get_mean(res, name=args.metric)
    original_score, _ = get_mean(res, name='original_{}'.format(args.metric))

    # plot results
    ax.errorbar(n_removed, score_mean, yerr=score_sem, fmt='-o', color='green')
    ax.axhline(original_score, linestyle='--', color='k')
    ax.set_xlabel('# train samples removed')
    ax.set_ylabel('Test {}'.format(args.metric.upper()))
    ax.tick_params(axis='both', which='major')

    os.makedirs(args.out_dir, exist_ok=True)

    plt.tight_layout()
    plt.savefig(os.path.join(args.out_dir, 'plot.{}'.format(args.ext)))

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Feature representation extractions for tree ensembles',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('--dataset', type=str, default='nc17_mfc18')
    parser.add_argument('--in_dir', type=str, default='output/removal/', help='input directory.')
    parser.add_argument('--out_dir', type=str, default='output/plots/removal/', help='output directory.')

    parser.add_argument('--tree_type', type=str, default='lgb', help='tree ensemble.')
    parser.add_argument('--tree_kernel', type=str, default='leaf_output', help='tree kernel.')

    parser.add_argument('--rs', type=int, nargs='+', default=list(range(1, 21)), help='random state.')
    parser.add_argument('--metric', type=str, default='auc', help='predictive metric.')
    parser.add_argument('--ext', type=str, default='pdf', help='output image format.')
    args = parser.parse_args()
    main(args)

class Args:

    dataset = 'nc17_mfc18'
    in_dir = 'output/removal/'
    out_dir = 'output/plots/removal/'

    tree_type = 'lgb'
    tree_kernel = 'leaf_output'

    rs = list(range(1, 21))
    metric = 'auc'
    ext = 'pdf'


import numpy as np
from scipy.special import expit

class NaiveBayesClassifier():
    def __init__(self, k=1):
        # Variables to store model parameters
        self.k = k
        self.PC = None
        self.PAC = None
        self.trained = False

    def train(self, X, y):
        self.trained = True
        # We will store both the probabilities of the labels and attribute probabilities given the label in dictionaries
        y_unique=np.unique(y)
        self.PAC = dict.fromkeys(y_unique)
        self.PC = dict.fromkeys(y_unique)
        N = len(y)

        for c in self.PC:
            # Calculate PC as the count of a given label divided by the total of instances and create the PA list of length
            # equal to the number of attributes.
            nc = np.count_nonzero(y == c)
            self.PC[c] = nc / N
            PA = np.empty(len(X[0])).tolist()

            for i in range(len(PA)):
                # Get possible values an attribute can take and create an empty dictionary at index i of the PA list.
                A = np.unique(X[:, i])
                PA[i] = dict.fromkeys(A)

                for a in A:
                    # Count the number of values with a given value 'a' and label 'c' using np.logical_and()
                    # Assign the dictionary values in the PA list using the formula with Laplace's smoothing.
                    Pac = np.count_nonzero(np.logical_and(y == c, X[:, i] == a))
                    PA[i][a] = (Pac + self.k) / (nc + self.k * len(A))

            # Store the result in the PAC class dictionary with the corresponding label.
            self.PAC[c] = PA

    def classify_prob(self, example):
        if not self.trained:
            raise ClassifierNotTrained("The model has not been trained")

        # Create an empty list for probabilities. Check if the input example is a 1-dimensional list or a numpy array
        # to ensure consistent results.
        probabilities = []

        if len(example.shape) == 1:
            example = np.array([example])

        # Iterate through all instances in the example and assign a dictionary with the labels from PC.
        for row in example:
            probability = dict.fromkeys(np.unique(list(self.PC.keys())))

            # Iterate through the labels, where 's' is a sum (since we're taking logarithms) and retrieve the values
            # stored in PC and PAC.
            for c in self.PC:
                s = np.log(self.PC[c])

                for i in row:
                    s += np.log(self.PAC[c][np.where(np.array(row) == i)[0][0]][i])

                probability[c] = np.exp(s)

            # Assign the sum values to the probability dictionary with label 'c'. Once all label values are iterated,
            # append them to the list and return.
            probabilities.append(probability)

        return probabilities

    def classify(self, example):
        # Create a list for class predictions. First, execute the class function that returns a list of probabilities
        # for each label.
        class_predictions = []
        probabilities = self.classify_prob(example)

        for probability in probabilities:
            # Take the maximum value from the corresponding dictionary values for each instance and return the
            # corresponding labels.
            class_predictions.append(max(probability, key=probability.get))

        return class_predictions

class ClassifierNotTrained(Exception):
    pass

class LogisticRegressionMiniBatch:
    # Constructor that stores the variable values throughout the class
    def __init__(self, classes=[0, 1], normalization=False, rate=0.1, rate_decay=False, batch_size=64):
        self.classes = classes
        self.normalization = normalization
        self.rate = rate
        self.rate0 = rate
        self.rate_decay = rate_decay
        self.batch_size = batch_size
        self.mean = None
        self.std = None
        self.weights = None

    # Create a function to calculate gradient descent
    def __gradient_descent(self, X, y, n_epochs):
        # Assign the number of features and instances using the shape function
        n_samples, n_features = X.shape

        # For each epoch, obtain a random batch. Perform the dot product of weights w*x using .dot and
        # apply the sigmoid function from scipy. Update the weights with the result and calculate the new rate.
        for i in range(n_epochs):
            indices = np.random.choice(n_samples, self.batch_size, replace=False)
            X_batch = X[indices]
            y_batch = y[indices]

            y_prediction = expit(self.weights.T.dot(X_batch.T))
            gradients = X_batch.T.dot(y_batch - y_prediction)
            self.weights += self.rate * gradients

        if self.rate_decay:
            self.rate = self.rate0 * (1 / (1 + i))

    def train(self, X, y, n_epochs, reset_weights=False, initial_weights=None):
        # Normalize the data and store the mean and standard deviation to apply them to the prediction set
        if self.normalization:
            self.mean = np.mean(X, axis=0)
            self.std = np.std(X, axis=0)
            X = (X - self.mean) / self.std

        # Add a new column to the data that corresponds to w_0 (bias)
        X = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)

        if reset_weights or self.weights is None:
            self.weights = np.random.uniform(-1, 1, size=X.shape[1])
        else:
            self.weights = initial_weights

        # Call the gradient descent function
        self.__gradient_descent(X, y, n_epochs)

    def classify_prob(self, example):
        probabilities = []
        if len(example.shape) == 1:
            example = np.array([example])
        if self.normalization:
            example = (example - self.mean) / self.std
        for row in example:
            # Normalize using the stored mean and standard deviation, and add the column with 1s (w_0) by reshaping
            # the data from the example.
            row = row.reshape(1, -1)
            row = np.concatenate((np.ones((row.shape[0], 1)), row), axis=1)
            row = row.reshape(-1)
            probabilities.append(expit(row.dot(self.weights)))
        # Add the probabilities to the list as the sigmoid of the dot product h*x and return
        return probabilities

    def classify(self, example):
        # Call the classify_prob function to obtain the list with sigmoid values,
        # and distinguish between one classification or another based on a threshold of > 0.5
        probabilities = self.classify_prob(example)
        return [self.classes[1] if p > 0.5 else self.classes[0] for p in probabilities]

class RL_OvR:
    # Variables stored for use in the class
    def __init__(self, classes, rate=0.1, normalization=False, rate_decay=False, batch_size=64):
        self.classes = classes
        self.normalization = normalization
        self.rate = rate
        self.rate_decay = rate_decay
        self.batch_size = batch_size
        self.classifiers = None

    def train(self, X, y, n_epochs):
        # Create an empty list that will contain as many classifiers as there are classes
        self.classifiers = []
        # Iterate over all labels, assigning 1 to the matching value and 0 to the rest,
        # creating binary classification where we can use LogisticRegressionMiniBatch
        for label in self.classes:
            binary_classes = np.where(y == label, 1, 0)
            classifier = LogisticRegressionMiniBatch(rate=self.rate, rate_decay=self.rate_decay, normalization=self.normalization, batch_size=self.batch_size)
            classifier.train(X, binary_classes, n_epochs)
            self.classifiers.append(classifier)

    def classify(self, example):
        probabilities, predictions = [], []
        # In this case, it won't be necessary to distinguish the size of the example since it is already handled
        # by the logistic classifiers by default.
        # Apply all the previously declared classifiers to the example data
        for classifier in self.classifiers:
            probabilities.append(np.array(classifier.classify_prob(example)))
        # Create an ndarray where each row contains the values returned by the sigmoid functions
        probabilities = np.column_stack([array for array in probabilities])
        # For each row, find the maximum value and return the corresponding class for that value
        for probability in probabilities:
            predictions.append(self.classes[np.argmax(probability)])
        return predictions

#Simple function that can be used to calculate the performance of any of the previous models
def performance(model, X, y):
    hit, miss= 0, 0
    classification=model.clasifica(X)
    for row in range(0,len(X)):
        if(classification[row]==y[row]):
           hit+=1
        else:
           miss+=1
    return hit/(hit+miss)


arrBerat = []
bMin = 0
bMax = 0
def hitungMinMax(arrBerat):
    global bMin, bMax
    # Definisikan Proses Mencari Berat Maximum Dan Minimum
    arrBerat.sort()
    bMin = arrBerat[0]
    bMax = arrBerat[-1]
    return bMin, bMax
def rerata(arrBerat):
    total = 0
    # Definisikan Proses Mencari Rerata Dari Total Berat
    for i in arrBerat:
        total += i
    rerata = total/len(arrBerat)
    # Return Hasil Rerata
    return rerata

print('Masukkan Banyak Data Berat Balita :', end=" ")
n = int(input())

for i in range(n):
    print(f'Masukkan Berat Balita Ke-{i+1} :', end=' ')
    # Inisialisasi Input Data Berat
    berat = float(input())
    # Masukkan Data Berat Ke Array (arrBerat)
    arrBerat.append(berat)

# Panggil procedur hitungMinMax(arrBerat)
hitungMinMax(arrBerat)

# Print Data Minimum, Maximum, dan Rerata Berat
print("Berat balita minimum: {:.2f} kg".format(bMin))
print("Berat balita maksimum: {:.2f} kg".format(bMax))
print("Rerata berat balita: {:.2f} kg".format(rerata(arrBerat)))


import torch
import os
import torch.nn as nn
import torch.optim as optim

import torchvision
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torchvision.datasets import DatasetFolder
from torchvision.datasets import ImageFolder
from torchvision.transforms import transforms
from torchvision.utils import save_image
from torch.optim.lr_scheduler import StepLR
import torch.nn.init as init

import math

import torch.nn.functional as F

import itertools

import numpy as np

from tensorboardX import SummaryWriter

batch_size = 100
n_epoch = 50
z_dim = 128
train_path = 'data_aligned_resize/train/'
test_path = 'data_aligned_resize/val/'
writer = SummaryWriter()

USE_CUDA = True
use_cuda = USE_CUDA and torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

#To Flatten the shape
class View(nn.Module):
    def __init__(self, size):
        super(View, self).__init__()
        self.size = size

    def forward(self, tensor):
        return tensor.view(self.size)

#Load training data
train_dataset = ImageFolder(
        root=train_path,
        transform=torchvision.transforms.ToTensor()
    )
train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        num_workers=0,
        shuffle=True
    )

#Load testing data
test_dataset = ImageFolder(
        root=test_path,
        transform=torchvision.transforms.ToTensor()
    )
test_loader = DataLoader(
        test_dataset,
        batch_size=batch_size,
        num_workers=0,
        shuffle=True
    )

#Initialize the weight
def kaiming_init(m):
    if isinstance(m, (nn.Linear, nn.Conv2d)):
        init.kaiming_normal(m.weight)
        if m.bias is not None:
            m.bias.data.fill_(0)
    elif isinstance(m, (nn.BatchNorm1d, nn.BatchNorm2d)):
        m.weight.data.fill_(1)
        if m.bias is not None:
            m.bias.data.fill_(0)

#Initialize AE model and optimizer
class AE(nn.Module):
    def __init__(self, z_dim=10, nc=3):
        super(AE, self).__init__()
        self.z_dim = z_dim
        self.nc = nc
        self.encoder = nn.Sequential(
            nn.Conv2d(nc, 128, 4, 2, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(True),
            nn.Conv2d(128, 256, 4, 2, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(True),
            nn.Conv2d(256, 512, 4, 2, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(True),
            nn.Conv2d(512, 1024, 4, 2, 1, bias=False),
            nn.BatchNorm2d(1024),
            nn.ReLU(True),
            View((-1, 1024*4*4)),
            nn.Linear(1024*4*4, z_dim)
        )
        self.decoder = nn.Sequential(
            nn.Linear(z_dim, 1024*8*8),
            View((-1, 1024, 8, 8)),
            nn.ConvTranspose2d(1024, 512, 4, 2, 1, bias=False),
            nn.BatchNorm2d(512),
            nn.ReLU(True),
            nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
            nn.BatchNorm2d(256),
            nn.ReLU(True),
            nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
            nn.BatchNorm2d(128),
            nn.ReLU(True),
            nn.ConvTranspose2d(128, nc, 1),
        )
        self.weight_init()

    def weight_init(self):
        for block in self._modules:
            for m in self._modules[block]:
                kaiming_init(m)

    def forward(self, x):
        z = self._encode(x)
        x_recon = self._decode(z)

        return x_recon, z

    def _encode(self, x):
        return self.encoder(x)

    def _decode(self, z):
        return self.decoder(z)

def cuda(tensor, uses_cuda):
    return tensor.cuda() if uses_cuda else tensor

AE_net = AE(z_dim=128, nc=3)

if torch.cuda.is_available():
    AE_net = AE_net.cuda()

optim_AE = optim.Adam(AE_net.parameters(), lr = 1e-03, betas=(0.5, 0.999))
loss_fn = nn.MSELoss()

#Train

AE_net.train()

for epoch in range(n_epoch):
    for batch_idx, datax in enumerate(train_loader, 0):

        data,_ = datax

        if torch.cuda.is_available():
            data = data.cuda()

        data = Variable(data)

        recons_x, code = AE_net(data)

        optim_AE.zero_grad()
        loss_ae = loss_fn(recons_x, data)
        loss_ae.backward()
        optim_AE.step()

        print("Train Epoch: {} [{}/{} ({:.0f}%)] \tAE_Loss: {:.6f}".format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss_ae.item()))

        n_epochs = epoch*len(train_loader)+batch_idx
        writer.add_scalar('ae_loss', loss_ae.data.cpu().numpy(), n_epochs)

        #Show the image result
        if batch_idx % 5 == 0:
            test_iter = iter(test_loader)
            test_data = next(test_iter)

            code = AE_net._encode(Variable(test_data[0]).cuda())

            gen = AE_net._decode(code).cpu().view(batch_size, 3, 64, 64)
            save_image(gen.data, 'img_result_ae/ae_reconst_%d.png' % (epoch + 1), nrow=10)

    #Save the weight
    torch.save(AE_net.state_dict(), 'saved_model_ae/AE.pth')


#########################################################################
# This script is used to calculate moisture source contribution
# fraction from different source regions to Southeast Asia (SEA)
# in different months and seasons
#
# by Harry Li (2018.06)
# refers to Ellen Dyer's code
#########################################################################

from matplotlib.patches import Polygon
from netCDF4 import MFDataset
from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap, addcyclic, shiftgrid
from pylab import *
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import sys
import collections
import os
import glob
import string
from math import *
import numpy as np
import numpy.ma as ma
import matplotlib
matplotlib.use("Agg")

def drawMapRect(lats, lons, m, color):
    x, y = m(lons, lats)
    xy = list(zip(x, y))
    # print(xy)
    poly = Polygon(xy, edgecolor='black', fill=False, linewidth=1.)
    plt.gca().add_patch(poly)
    plt.text(x[0], y[0]+5, 'SEA', color='black', fontsize=5, weight='bold')

# first entry for each tag is latitude 1, second entry is latitude 2, third entry is land or ocean (land=1, ocean=0), fourth entry is tag number
lats = collections.OrderedDict([
    ('Southern Indian Ocean', [-40, -30, 0, 5]),
    ('Southern Coast Indian Ocean', [-30, -10, 0, 6]),
    ('South Western Indian Ocean', [-30, -10, 0, 7]),
    ('South Central Indian Ocean', [-30, -10, 0, 8]),
    ('South East Indian Ocean', [-30, -10, 0, 9]),
    ('Eastern Indian Ocean', [-10, 10, 0, 10]),
    ('Equatorial Coastal Indian Ocean', [-10, 10, 0, 11]),
    ('Equatorial Central Indian Ocean', [-10, 10, 0, 12]),
    ('Equatorial East Indian Ocean', [-10, 10, 0, 13]),
    ('Arabian Sea', [10, 30, 0, 14]),
    ('Bay of Bengal', [10, 30, 0, 15]),
    ('Mediterranean Sea', [30, 50, 0, 16]),
    ('Persian Gulf', [10, 30, 0, 17]),
    ('Europe', [40, 70, 1, 18]),
    ('North Asia', [50, 80, 1, 19]),
    ('Indian Sub Continent', [10, 30, 1, 20]),
    ('Southeast Asia subregion', [10, 30, 1, 21]),
    ('East coast China Ocean', [10, 40, 0, 22]),
    ('East China land', [20, 50, 1, 23]),
    ('Mid Asia and Western China', [30, 50, 1, 24]),
    ('Indonesia Ocean', [-10, 10, 0, 25]),
    ('Indonesia Land', [-10, 10, 1, 26]),
    ('Southern Japanese Ocean', [10, 30, 0, 27]),
    ('Japanese Ocean', [30, 60, 0, 28]),
    ('Australia', [-40, -10, 1, 29]),
    ('Northwest Subtropical Pacific_W', [10, 30, 0, 30]),
    ('Northwest Subtropical Pacific_E', [10, 30, 0, 31]),
    ('West Equatorial Pacific_W', [-10, 10, 0, 32]),
    ('West Equatorial Pacific_E', [-10, 10, 0, 33]),
    ('North Pacific', [30, 60, 0, 34]),
    ('East Equatorial Pacific', [-10, 10, 0, 35]),
    ('Northeast Subtropical Pacific', [10, 30, 0, 36]),
    ('Southeast Subtropical Pacific', [-30, -10, 0, 37]),
    ('Southwest Subtropical Pacific', [-30, -10, 0, 38]),
    ('East Pacific', [-30, 30, 0, 39]),
    ('Southern Pacific', [-60, -30, 0, 40])])

print('total region numbers(lats):')
lats_l = list(lats.keys())
print(len(lats_l))
print('************************************')

lons = collections.OrderedDict([
    ('Southern Indian Ocean', [30, 110, 0, 5]),
    ('Southern Coast Indian Ocean', [30, 50, 0, 6]),
    ('South Western Indian Ocean', [50, 70, 0, 7]),
    ('South Central Indian Ocean', [70, 90, 0, 8]),
    ('South East Indian Ocean', [90, 110, 0, 9]),
    ('Eastern Indian Ocean', [90, 110, 0, 10]),
    ('Equatorial Coastal Indian Ocean', [30, 50, 0, 11]),
    ('Equatorial Central Indian Ocean', [50, 70, 0, 12]),
    ('Equatorial East Indian Ocean', [70, 90, 0, 13]),
    ('Arabian Sea', [50, 80, 0, 14]), ('Bay of Bengal', [80, 100, 0, 15]),
    ('Mediterranean Sea', [350, 40, 0, 16]),
    ('Persian Gulf', [30, 50, 0, 17]),
    ('Europe', [350, 50, 1, 18]),
    ('North Asia', [50, 130, 1, 19]),
    ('Indian Sub Continent', [70, 90, 1, 20]),
    ('Southeast Asia subregion', [90, 110, 1, 21]),
    ('East coast China Ocean', [110, 130, 0, 22]),
    ('East China land', [110, 130, 1, 23]),
    ('Mid Asia and Western China', [50, 110, 1, 24]),
    ('Indonesia Ocean', [110, 150, 0, 25]),
    ('Indonesia Land', [110, 150, 1, 26]),
    ('Southern Japanese Ocean', [130, 150, 0, 27]),
    ('Japanese Ocean', [130, 150, 0, 28]),
    ('Australia', [110, 150, 1, 29]),
    ('Northwest Subtropical Pacific_W', [150, 170, 0, 30]),
    ('Northwest Subtropical Pacific_E', [170, 190, 0, 31]),
    ('West Equatorial Pacific_W', [150, 170, 0, 32]),
    ('West Equatorial Pacific_E', [170, 190, 0, 33]),
    ('North Pacific', [150, 230, 0, 34]),
    ('East Equatorial Pacific', [190, 230, 0, 35]),
    ('Northeast Subtropical Pacific', [190, 230, 0, 36]),
    ('Southeast Subtropical Pacific', [190, 230, 0, 37]),
    ('Southwest Subtropical Pacific', [150, 190, 0, 38]),
    ('East Pacific', [230, 270, 0, 39]),
    ('Southern Pacific', [150, 270, 0, 40])])

print('total region numbers(lons):')
lons_l = list(lons.keys())
print(len(lons_l))
print('************************************')

tagnum = len(lats_l)  # all tag numbers

print('all tag regions:')
for i in np.arange(0, len(lons_l)):
    print(str(i+5)+': '+lons_l[i])
print('************************************')

# define file directory
filedir = "/scratch/d/dylan/harryli/gpcdata/icam5_iclm4_v12/archive/SEA_wt_1920today/atm/hist/"
outdir = '/scratch/d/dylan/harryli/gpcdata/Analysis/icam5/SEA_wt_1920today/climatology/watertag/'

# read lats lons, land fraction
filename = filedir+"SEA_wt_1920today.cam.h0.1980-01.nc"
nvarFile = Dataset(filename)
LON = nvarFile.variables['lon'][:]
lat = nvarFile.variables['lat'][:]
LNDFRC = nvarFile.variables['LANDFRAC'][0, :, :]
lndfrc, lon = shiftgrid(180., LNDFRC[:, :], LON, start=False)

# define month array
months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '01', '02']
mons = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]) - 1
numMon = len(mons)

# Region of Interest
las = [10, 10, 20, 20]
los = [100, 110, 110, 100]

# find region boundary indice
la_drc_1 = np.argmin(np.abs(lat - las[0]))
la_drc_2 = np.argmin(np.abs(lat - las[2]))
lo_drc_1 = np.argmin(np.abs(LON - los[0]))
lo_drc_2 = np.argmin(np.abs(LON - los[2]))

print('region of interest boundary indices:')
print('lats: '+str(la_drc_1)+','+str(la_drc_2))
print('lons: '+str(lo_drc_1)+','+str(lo_drc_2))
print('************************************')

# define water tags
SOURCES = ['Convective rain', 'Large-scale rain', 'Convective snow', 'Large-scale sow']
sourceL = ['PRECRC_', 'PRECRL_', 'PRECSC_', 'PRECSL_']
sourceS = ['r', 'R', 's', 'S']

# define years
year1 = 1980
year2 = 2005

# set plot months and seasons
monplt = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
monnam = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
monind = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
seaplt = ['DJF', 'MAM', 'JJA', 'SON']
seaind = np.array([[12, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]])

##############################################################################
# plot for each month
##############################################################################

# create global map fpr plotting
POLY = np.zeros((len(lat), len(LON)))
for count in range(0, tagnum):
    wtag_num = count + 5
    la1 = np.argmin(np.abs(lat - lats[lats_l[count]][0]))
    la2 = np.argmin(np.abs(lat - lats[lats_l[count]][1]))
    lo1 = np.argmin(np.abs(LON - lons[lons_l[count]][0]))
    lo2 = np.argmin(np.abs(LON - lons[lons_l[count]][1]))

    for la in range(la1, la2):
        if (lo2 > lo1):
            for lo in range(lo1, lo2):
                if np.abs(LNDFRC[la, lo] - lats[lats_l[count]][2]) < 0.5:
                    POLY[la, lo] = lons[lons_l[count]][3]
        else:
            for lo in range(lo1, len(LON)):
                if np.abs(LNDFRC[la, lo] - lats[lats_l[count]][2]) < 0.5:
                    POLY[la, lo] = lons[lons_l[count]][3]
            for lo in range(0, lo2):
                if np.abs(LNDFRC[la, lo] - lats[lats_l[count]][2]) < 0.5:
                    POLY[la, lo] = lons[lons_l[count]][3]

poly, lon = shiftgrid(180., POLY[:, :], LON, start=False)

plt.clf()
m = Basemap(projection='cyl', llcrnrlat=-70, urcrnrlat=80, llcrnrlon=10, urcrnrlon=290)
parallels = np.arange(90, -90, -20.0)
meridians = np.arange(-10, 310, 40.0)
m.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=5, linewidth=0.3)
m.drawmeridians(meridians, labels=[0, 0, 0, 1], fontsize=5, linewidth=0.3)
m.drawcoastlines(linewidth=0.6)

x, y = np.meshgrid(LON[:], lat[:])
csm = m.pcolormesh(x, y, POLY[:, :], cmap=cm.nipy_spectral, edgecolor='face', linewidth=0, rasterized=True, vmax=40., vmin=5., alpha=0.7)
# csm = m.pcolormesh(x, y, POLY[:, :], cmap=cm.Spectral, vmax=40., vmin=1.)
# csc = m.contour(x, y, POLY[:, :], colors='black', levels=np.arange(4, 41, 1), linewidths=0.5, interpolation='none')
# cbar = m.colorbar(csm, shrink=0.5)
drawMapRect(las, los, m, 'white')
# plt.show()
plt.savefig(outdir+'icam_wt_SEA_36tag_regions_wtLandMask_pynew_10N_20N.png', bbox_inches='tight', dpi=600)
plt.suptitle('36 tagged regions', fontsize=7)
plt.savefig(outdir+'icam_wt_SEA_36tag_regions_wtLandMask_pynew_10N_20N.pdf', bbox_inches='tight', dpi=600)


fruit_list={
    "apple": 5,
    "banana": 8,
    "grapes": 5,
    "watermelon": 10
}

cust_ans = input("Which fruit are you looking for? ").lower()

if(cust_ans in fruit_list):
  print("Fruit is in stock")
else:
    print("Fruit is not in stock ")


import unittest
import ticketsviewer

'''
Ticket 1 is a sample ticket provided by Zendesk and we will be using that to test our fetchTicket(id) method.

The init() method makes a request to the API to check how many tickets are in the account and exits gracefully if the API returns anything other than a status code of 200

The menu() method facilitates the choices for the user.

    * UI is pretty clear and has rigorous test from within to ensure an error-free environment.
    * The currentPage variable (which keeps track of which page is being viewed) doesn't get updated unless a vaible input is inserted by the user which significantly reduces the margin for error.
    * If a user requests a specific page or ticket, extra tests are conducted to ensure that the ticket ID and page number is a valid number.

displayTicket(ticket) - displays the ticket parameter in great detail. Returns a null if ticket is null.
displayTickets(tickets) - displays the first 25 of the tickets or the total number of tickets, whichever is lesser. Returns null if tickets is null.

'''

class TestTicketsviewer (unittest.TestCase):

    def test_fetchTicket(self):
        ticket = ticketsviewer.fetchTicket(1)
        self.assertEqual(ticket['ticket']['subject'], 'Sample ticket: Meet the ticket')
        self.assertEqual(ticket['ticket']['id'], 1)

    def test_fetchPage(self):
        tickets = ticketsviewer.fetchPage(1)
        self.assertEqual(len(tickets), 25)
        self.assertEqual(tickets[0] ['id'], 1)
        tickets = ticketsviewer.fetchPage(4)
        self.assertEqual(tickets[0]['id'], 76)

    def test_displayTicket(self):
        self.assertEqual(ticketsviewer.displayTicket(None), -1)
        self.assertEqual(ticketsviewer.displayTicket(ticketsviewer.fetchTicket(1)['ticket']), None)

    def test_displayTickets(self):
        tickets = ticketsviewer.fetchPage(1)
        self.assertEqual(ticketsviewer.displayTickets(tickets), None)

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


def nibu(n,g):
    from collections import deque
    q = deque([(0,0)])
    color = [-1]*n
    color[0] = 0
    while q:
        x,c = q.popleft()
        for y in g[x]:
            if color[y]==c:
                return False,[]
            if color[y]==-1:
                color[y]=1-c
                q.append((y,1-c))
    return True,color


import string

import apperror

class IdentifierError(apperror.ApplicationError):
    def __init__(self, idString):
        self.str="'%s' is not a valid identifier"

# keyword strings belong here.

# the self instance
Self="self"

# I/O name specifiers
In="in"
Out="out"
# subnet inputs/outputs
SubIn="sub_in"
SubOut="sub_out"
# explicitly named external inputs/outputs
ExtIn="ext_in"
ExtOut="ext_out"

# The message objects
Msg="msg"
MsgError="error"
MsgWarning="warning"
MsgLog="log"

# instance separator
InstSep=':'
ArraySepStart='['
ArraySepEnd=']'

# subtype separator
SubTypeSep='.'

# module separator
ModSep='::'

# the set of keywords that are not allowed for identifiers
keywords=set([ Self, In, Out, SubIn, SubOut, ExtIn, ExtOut ])

# the identifier allowed characters
allowedIdFirstChars = set(string.ascii_letters)
allowedIdChars = string.ascii_letters + string.digits + '_'
idTransTable = string.maketrans('-', '_')
idEmptyTransTable = string.maketrans('','')

def validIdentifier(idString):
    """Check whether a string is a valid identifier.
       Throws an IdentifierError if it is not a valid identifier,
       returns a backward-compatibility-fixed string"""
    global keywords
    global allowedIdFirstChars
    global allowedIdChars
    global idTransTable
    global idEmptyTransTable
    # the first MUST be a letter
    if not idString[0] in allowedIdFirstChars:
        raise IdentifierError(idString)
    # now fix the ID for backward compatibility
    idString=idString.translate(idTransTable)
    # and check the string for non-allowed characters, or whether it is a
    # keyword
    if ( not idString.translate(idEmptyTransTable, allowedIdChars)
         or idString in keywords):
        raise IdentifierError(idString)
    return idString

# an additional function for backward-compatibility: all IDs should have
# underscores, not dashes, and we force dashes to be underscores.
def fixID(idString):
    """an additional function for backward-compatibility: all IDs should have
       underscores, not dashes, and we force dashes to be underscores."""
    return idString.replace('-', '_')