#!/usr/bin/env python3

"""
    Server side of Assignment 1 for ECE 4564.
    Handles:
    1. Receiving question from client.
    2. Speaking question.
    3. Sending a receiving from WolframAlpha API.
    4. Sending answer to client.
"""

import os
import sys
import argparse
import socket

from cryptography.fernet import Fernet
import wolframalpha
import hashlib
import pickle
from gtts import gTTS

from serverKeys import wolfram_alpha_appid

def checkpoint(message):
    """Prints [Checkpoint] <message>"""
    print("[Checkpoint] {}".format(message))

def encrypt_data(data, key):
    """Encrypt given data and returns key, ciphertest, and md5"""
    # Has to be the same key because the client only has the key it
    #     sent the data with
    cipher_suite = Fernet(key)
    # Defaults to utf-8
    bytedata = data.encode()
    # Encrypt WA Answer
    ciphertext = cipher_suite.encrypt(bytedata)

    # Determine the md5 of ciphertext
    temp = hashlib.md5()
    temp.update(ciphertext)
    md5 = temp.digest()

    return ciphertext, md5

def ask_wa(message):
    """Asks Wolfram Alpha messasge"""
    checkpoint("Sending question to Wolframalpha: {}".format(message))

    # Get answer from WA
    client = wolframalpha.Client(wolfram_alpha_appid)
    res = client.query(message)

    # Handle bad queries to WA
    try:
        answer = next(res.results).text
    except Exception as ex:
        answer = "Wolfram Alpha was unable to find an answer for {}. \
            Please try something else.".format(message)

    checkpoint("Received answer to Wolframalpha: {}".format(answer))

    return answer

def speak(message):
    """Speaks given message"""
    checkpoint("Speaking: {}".format(message))

    tts = gTTS(text=message, lang='en')
    tts.save("./saythis.mp3")
    os.system("mplayer ./saythis.mp3 > /dev/null 2>&1")

def decrypt_data(data):
    """Verify md5 and decrypt data"""
    # Data should be formatted as the tuple: (key, ciphertext, md5)

    # Extract key & decrypt data
    key = data[0]
    cipher_suite = Fernet(key)
    plaintext = cipher_suite.decrypt(data[1])

    # Get md5 of data
    temp = hashlib.md5()
    temp.update(data[1])
    md5 = temp.digest()

    return key, plaintext, md5

def accept_connections(socket, size):
    """Repeatedly accepts and handles new client connections"""
    while 1:
        client, address = socket.accept()
        client_addr = client.getpeername()[0]
        client_port = client.getpeername()[1]
        checkpoint("Accepted client connection from {} on port {}".format(
            client_addr, client_port))

        data = client.recv(size)
        if data:
            checkpoint("Received data: {}".format(data))

            # Decrypt data
            decryptedData = pickle.loads(data)
            recv_key, recv_plaintext, recv_md5 = decrypt_data(decryptedData)

            # Check if md5 is valid
            if decryptedData[2] == recv_md5:
                checkpoint("Checksum is VALID")
            else:
                checkpoint("Checksum is INVALID")

            checkpoint("Decrypt: Using Key: {} | Plaintext: {}"
                .format(recv_key, recv_plaintext, recv_md5))

            # Speak data
            speak(recv_plaintext.decode())

            # Ask WA
            answer = ask_wa(recv_plaintext.decode())

            # Encrypt response
            send_ciphertext, send_md5 = encrypt_data(answer, recv_key)
            checkpoint("Encrypt: Generated Key: {} | Ciphertext: {}"
                .format(recv_key, send_ciphertext))
            checkpoint("Generated MD5 Checksum: {}".format(send_md5))

            # Send response back to client
            dataTup = (recv_key, send_ciphertext, send_md5)
            send_data = pickle.dumps(dataTup)
            client.send(send_data)
            checkpoint("Sending data: {}".format(send_data))

            client.close()

def main():
    """Main function to manager the server"""
    # Parse command line args
    parser = argparse.ArgumentParser(description='Prossesses arguments')
    parser.add_argument('-p', help='Set the server port.')
    parser.add_argument('-b', help='Set the backlog size.')
    parser.add_argument('-z', help='Set the socket size')

    args = parser.parse_args()
    if args.p == None:
        print('Please set server port with the -p flag.')
        sys.exit(1)
    if args.b == None:
        print('Please set backlog size with the -b flag.')
        sys.exit(1)
    if args.z == None:
        print('Please set socket size with the -z flag.')
        sys.exit(1)

    host = '0.0.0.0'

    # Copy args into vars
    try:
        port =      int(args.p)
        backlog =   int(args.b)
        size =      int(args.z)
    except Exception as ex:
        print(ex)
        sys.exit(1)

    # Setup socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((host,port))
    checkpoint("Created socket at {} on port {}".format(host, port))

    # Start listening
    checkpoint("Listening for client connections")
    s.listen(backlog)

    # Start accepting connections
    accept_connections(s, size)

main()


#User function Template for python3

class Solution:

    #Function to return a list of integers denoting spiral traversal of matrix.
    def spirallyTraverse(self,matrix, r, c):
        # code here
        row,col = 0,0
        res = []
        while (row<r and col<c):
            for i in range(col,c):
                res.append(matrix[row][i])
            row+=1
            for i in range(row,r):
                res.append(matrix[i][c-1])
            c-=1
            if (row<r):
                for i in range(c-1,col-1,-1):
                    res.append(matrix[r-1][i])
                r-=1
            if (col<c):
                for i in range(r-1,row-1,-1):
                    res.append(matrix[i][col])
                col+=1
        return res

#{
 # Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        r,c = map(int, input().strip().split())
        values = list(map(int, input().strip().split()))
        k = 0
        matrix =[]
        for i in range(r):
            row=[]
            for j in range(c):
                row.append(values[k])
                k+=1
            matrix.append(row)
        obj = Solution()
        ans = obj.spirallyTraverse(matrix,r,c)
        for i in ans:
            print(i,end=" ")
        print()

# } Driver Code Ends

Spirally traversing a matrix - GFG/spirally-traversing-a-matrix.py


import xarray as xr
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import ticker
import datetime as datetime
from common import time_mean_to_orca

def add_slice(ax, ds, time, depth):

    ds = ds.isel(time_counter=time, deptht=depth)

    lev = np.linspace(-2,2,11)
    p = ax.pcolor(ds.nav_lon, ds.nav_lat, ds.vozocrtx, vmin=-0.4, vmax=0.4,
                  cmap=plt.cm.inferno)

    return p

def plot(orca_path, sochic_path, model, depth0, depth1):
    '''
    main plotting routine adding six time slices over two depths
    '''

    # intialise plots
    fig, axs = plt.subplots(2,6, figsize=(6.5, 2), dpi=300)
    plt.subplots_adjust(bottom=0.15, top=0.98, right=0.90, left=0.10,
                        wspace=0.1, hspace=0.1)

    # load data
    orca   = xr.open_dataset(orca_path, decode_cf=False)
    sochic = xr.open_dataset(sochic_path, decode_cf=True)

    # translate orca dates to cf
    orca.time_counter.attrs['units'] = 'seconds since 1900-01-01'
    orca.time_centered.attrs['units'] = 'seconds since 1900-01-01'
    orca = xr.decode_cf(orca)

    #sochic['time_counter'] = sochic.indexes['time_counter'].to_datetimeindex()

    #if model == 'orca':
    m = orca
    #if model == 'sochic':
    #    # align time steps
    #m = time_mean_to_orca(orca, sochic)

    # plot six time steps at depth0
    for i, ax in enumerate(axs[0]):
        print ('a', i)
        add_slice(ax, m, i, depth0)

    # plot six time steps at depth1
    for i, ax in enumerate(axs[1]):
        print ('b', i)
        p = add_slice(ax, m, i, depth1)

    pos = axs[0,1].get_position()
    cbar_ax = fig.add_axes([0.91, pos.y0, 0.02, pos.y1 - pos.y0])
    cbar = fig.colorbar(p, cax=cbar_ax)
    #cbar.locator = ticker.MaxNLocator(nbins=3)
    #cbar.update_ticks()
    #cbar.ax.text(4.5, 0.5, labels[i], fontsize=8, rotation=90,
    cbar.ax.text(4.5, 0.5, 'temperature', fontsize=8, rotation=90,
                 transform=cbar.ax.transAxes, va='center', ha='right')
    for ax in axs[0,:]:
        ax.set_xticks([])
    for ax in axs[0,1:]:
        ax.set_yticks([])
    for ax in axs[1,1:]:
        ax.set_yticks([])
    for ax in axs.flatten():
        ax.set_aspect('equal')

    plt.savefig(model + 'orca_u_orca_mean.png')

if __name__ == '__main__':
    model = 'EXP106'
    orca_path = '../processORCA12/DataOut/ORCA_PATCH_U.nc'
    #orca_path = '../processORCA12/DataOut/ORCA0083-N06_T_conform.nc'
    sochic_path = '../Output/EXP106/SOCHIC_PATCH_1h_20150106_20150118_grid_U.nc'
   #sochic_path = '../Output/EXP74/SOCHIC_PATCH_1h_20150106_20150130_grid_T.nc'
    #sochic_path = '../Output/EXP60/SOCHIC_PATCH_1h_20150101_20150121_grid_T.nc'

    #SOCHIC_PATCH_1h_20150101_20150130_grid_T.nc'
    #plot(orca_path, sochic_path, 'orca', depth0=0, depth1=20)
    plot(orca_path, sochic_path, model, depth0=0, depth1=20)


from moviepy.editor import VideoFileClip, concatenate_videoclips
from moviepy.video.fx.resize import resize
import os
from os.path import isfile, join
import random
import shutil
from collections import defaultdict

VideoFileClip.resize = resize

def extractAcc(filepath):
    try:
        s = filepath.split("/")[-1].split("-")
        acc = "-".join(s[1:(2+(len(s) - 4))])
        return acc
    except:
        return ""

# generateTimeRange converts float seconds to a range of form @MM:SS
def generateTimeRange(duration, clipDuration):
    preHour = int(duration / 60)
    preMin = int(duration % 60)
    preTime = str(preHour // 10) + str(preHour % 10) + ":" + str(preMin // 10) + str(preMin % 10)

    duration += clipDuration
    postHour = int(duration / 60)
    postMin = int(duration % 60)
    postTime = str(postHour // 10) + str(postHour % 10) + ":" + str(postMin // 10) + str(postMin % 10)

    #return "@" + preTime + " - " + "@" + postTime
    return "@" + preTime

# makeCompilation takes videos in a folder and creates a compilation with max length totalVidLength
def makeCompilation(path = "./",
                    introName = '',
                    outroName = '',
                    totalVidLength = 10*60,
                    maxClipLength = 20,
                    minClipLength = 5,
                    outputFile = "output.mp4"):

    allVideos = []
    seenLengths = defaultdict(list)
    totalLength = 0
    for fileName in os.listdir(path):

        filePath = join(path, fileName);
        if isfile(filePath) and fileName.endswith(".mp4"):
            print(fileName)
            if os.stat(filePath).st_size < 5000:
                continue

            # Destination path
            clip = VideoFileClip(filePath)
            clip = clip.resize(width=1920)
            clip = clip.resize(height=1080)
            duration = clip.duration
            print(duration)
            if duration <= maxClipLength and duration >= minClipLength:
                allVideos.append(clip)
                seenLengths[duration].append(fileName)
                totalLength += duration

    print("Total Length: " + str(totalLength))

    random.shuffle(allVideos)

    duration = 0
    # Add intro vid
    videos = []
    if introName != '':
        introVid = VideoFileClip("./" + introName)
        videos.append(introVid)
        duration += introVid.duration

    description = ""
    # Create videos
    for clip in allVideos:
        timeRange = generateTimeRange(duration, clip.duration)
        acc = extractAcc(clip.filename)
        description += timeRange + " : @" + acc + "\n"
        duration += clip.duration
        videos.append(clip)
        print(duration)
        if duration >= totalVidLength:
            # Just make one video
            break

    # Add outro vid
    if outroName != '':
        outroVid = VideoFileClip("./" + outroName)
        videos.append(outroVid)

    finalClip = concatenate_videoclips(videos, method="compose")

    audio_path = "/tmp/temoaudiofile.m4a"

    #print(description)
    # Create compilation
    finalClip.write_videofile(outputFile, threads=8, temp_audiofile=audio_path, remove_temp=True, codec="libx264", audio_codec="aac")

    return description

if __name__ == "__main__":
    makeCompilation(path = "/Users/nathanan/Documents/YOUTUBE/AutomatedChannel/Videos/Memes/",
                    introName = "intro_vid.mp4",
                    outroName = '',
                    totalVidLength = 10*60,
                    maxClipLength = 20,
                    outputFile = "outputseq.mp4")


from global_vars import grammar, nonterms, terms
from slr_parser import perform_action

def parse_input_string(start, error, parse_table):
    input_str = input('\nEnter Input String' +
                      '(Whitespaces is required in between lexemes): ')
    parse_str = (input_str + ' $').split()
    inp_ptr = 0
    stack = ['0']

    print(
        '\n+--------+----------------------------+----------------------------+----------------------------+'
    )
    print('|{:^8}|{:^28}|{:^28}|{:^28}|'.format('Step', 'Stack', 'Input',
                                                'Action'))
    print(
        '+--------+----------------------------+----------------------------+----------------------------+'
    )

    step = 1

    while True:
        curr_symbol = parse_str[inp_ptr]
        stack_top = int(stack[-1])
        stack_content = ''
        input_content = ''

        print('|{:^8}|'.format(step), end=' ')

        for i in stack:
            stack_content += i
        print('{:27}|'.format(stack_content), end=' ')
        i = inp_ptr

        while i < len(parse_str):
            input_content += parse_str[i]
            i += 1

        print('{:>26} | '.format(input_content), end=' ')

        step += 1
        action = perform_action(stack_top, curr_symbol, start, error,
                                parse_table)

        if '/' in action:
            print('{:^26}|'.format(action + '. Multiple conflicting actions.'))

            break

        if 's' in action:
            print('{:^26}|'.format(action))
            stack.append(curr_symbol)
            stack.append(action[1:])
            inp_ptr += 1
        elif 'r' in action:
            print('{:^26}|'.format(action))
            i = 0

            for head, prods in grammar.items():
                for prod in prods:
                    if i == int(action[1:]):
                        for _ in range(2 * len(prod)):
                            stack.pop()

                        state = stack[-1]
                        stack.append(head)
                        stack.append(
                            parse_table[int(state)][len(terms) +
                                                    nonterms.index(head)])
                    i += 1
        elif action == 'acc':
            print('{:^26}|'.format('Accepted'))

            break
        else:
            print('ERROR: Illegal symbol', curr_symbol, '|')

            break
    print(
        '+--------+----------------------------+----------------------------+----------------------------+'
    )


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

# In[28]:

# import important libraries
import pandas as pd
from matplotlib import dates
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb

# ## **Walmart**
# walmart is an American multinational retail corporation that operates a chain of hypermarkets, discount department stores, and grocery stores from the United states.

#

#
#

# ### Dataset Description
#
# This is the historical data that covers sales from 2010-02-05 to 2012-11-01, in the file Walmart_Store_sales. Within this file you will find the following fields:
#
# Store - the store number
#
# Date - the week of sales
#
# Weekly_Sales -  sales for the given store
#
# Holiday_Flag - whether the week is a special holiday week 1 – Holiday week 0 – Non-holiday week
#
# Temperature - Temperature on the day of sale
#
# Fuel_Price - Cost of fuel in the region
#
# CPI – Prevailing consumer price index
#
# Unemployment - Prevailing unemployment rate

# In[2]:

# import the dataset
data = pd.read_csv('Walmart_Store_sales.csv')

# In[3]:

data.head()

# ## Understanding Dataset

# In[4]:

data.info()

# In[56]:

data.max() #Finds the Maximum value in each column

# In[57]:

# checking for missing values
data.isnull().sum()

# ### Preparation Dataset

# In[32]:

# Convert date to datetime format and show dataset information
data['Date'] =  pd.to_datetime(data['Date'])
data.info()

# In[33]:

# Splitting Date and create new columns (Day, Month, and Year)
data["Day"]= pd.DatetimeIndex(data['Date']).day
data['Month'] = pd.DatetimeIndex(data['Date']).month
data['Year'] = pd.DatetimeIndex(data['Date']).year
data

# #### QUESTION 1 :- Which store has maximum sales in this dataset?

# In[6]:

data.loc[data['Weekly_Sales'] ==  data['Weekly_Sales'].max()]
# used to find the row meeting the specific condition,
# Here we are checking in column Weekly_Sales which row or store in particular is having the maximum Weekly_Sales.

# From above Figure we see that Store 14 has the maximum weekly sales.

# #### QUESTION 2 :- Which store has maximum standard deviation i.e., the sales vary a lot. Also, find out the coefficient of variance (C0V)

# In[7]:

#Here i am grouping by store and finding the standard deviation and mean of each store.
maxstd=pd.DataFrame(data.groupby('Store').agg({'Weekly_Sales':['std','mean']}))
#Just resetting the index.
maxstd = maxstd.reset_index()
#Now we know that CoV is std/ mean we are doing this for each store.
maxstd['CoV'] =(maxstd[('Weekly_Sales','std')]/maxstd[('Weekly_Sales','mean')]) *100
#finding the store with maximum standard deviation.
maxstd.loc[maxstd[('Weekly_Sales','std')]==maxstd[('Weekly_Sales','std')].max()]

# From above Figure we can conclude that sales in store 14 vary a lot

# #### QUESTION 3 :- Which store/s has good quarterly growth rate in Q3’2012.

# In[8]:

#Converting the data type of date column to dateTime
data['Date'] = pd.to_datetime(data['Date'])

#defining the start and end date of Q3 and Q2
Q3_date_from = pd.Timestamp(date(2012,7,1))
Q3_date_to = pd.Timestamp(date(2012,9,30))
Q2_date_from = pd.Timestamp(date(2012,4,1))
Q2_date_to = pd.Timestamp(date(2012,6,30))

#Collecting the data of Q3 and Q2 from original dataset.
Q2data=data[(data['Date'] > Q2_date_from) & (data['Date'] < Q2_date_to)]
Q3data=data[(data['Date'] > Q3_date_from) & (data['Date'] < Q3_date_to)]

#finding the sum weekly sales of each store in Q2
Q2 = pd.DataFrame(Q2data.groupby('Store')['Weekly_Sales'].sum())
Q2.reset_index(inplace=True)
Q2.rename(columns={'Weekly_Sales': 'Q2_Weekly_Sales'},inplace=True)

#finding the sum weekly sales of each store in Q2
Q3 = pd.DataFrame(Q3data.groupby('Store')['Weekly_Sales'].sum())
Q3.reset_index(inplace=True)
Q3.rename(columns={'Weekly_Sales': 'Q3_Weekly_Sales'},inplace=True)

#mergeing Q2 and Q3 data on Store as a common column
Q3_Growth= Q2.merge(Q3,how='inner',on='Store')

# In[9]:

#Calculating Growth rate of each Store and collecting it into a dataframe
Q3_Growth['Growth_Rate'] =(Q3_Growth['Q3_Weekly_Sales'] - Q3_Growth['Q2_Weekly_Sales'])/Q3_Growth['Q2_Weekly_Sales']
Q3_Growth['Growth_Rate']=round(Q3_Growth['Growth_Rate'],2)
Q3_Growth.sort_values('Growth_Rate',ascending=False).head(1)

# In[10]:

Q3_Growth.sort_values('Growth_Rate',ascending=False).tail(1)

# From above tables we can infer that Q3 growth rate is in losses .
# the Store 16 has the least loss of 3% compared the other stores and store 14 has highest loss of 18%.

# #### QUESTION 4:- Some holidays have a negative impact on sales. Find out holidays which have higher sales than the mean sales in non-holiday season for all stores together.

# In[13]:

#finding the mean sales of non holiday and holiday
data.groupby('Holiday_Flag')['Weekly_Sales'].mean()

# In[14]:

#marking the holiday dates
Christmas1 = pd.Timestamp(date(2010,12,31) )
Christmas2 = pd.Timestamp(date(2011,12,30) )
Christmas3 = pd.Timestamp(date(2012,12,28) )
Christmas4 = pd.Timestamp(date(2013,12,27) )

Thanksgiving1=pd.Timestamp(date(2010,11,26) )
Thanksgiving2=pd.Timestamp(date(2011,11,25) )
Thanksgiving3=pd.Timestamp(date(2012,11,23) )
Thanksgiving4=pd.Timestamp(date(2013,11,29) )

LabourDay1=pd.Timestamp(date(2010,2,10) )
LabourDay2=pd.Timestamp(date(2011,2,9) )
LabourDay3=pd.Timestamp(date(2012,2,7) )
LabourDay4=pd.Timestamp(date(2013,2,6) )

SuperBowl1=pd.Timestamp(date(2010,9,12) )
SuperBowl2=pd.Timestamp(date(2011,9,11) )
SuperBowl3=pd.Timestamp(date(2012,9,10) )
SuperBowl4=pd.Timestamp(date(2013,9,8) )

#Calculating the mean sales during the holidays
Christmas_mean_sales=data[(data['Date'] == Christmas1) | (data['Date'] == Christmas2) | (data['Date'] == Christmas3) | (data['Date'] == Christmas4)]
Thanksgiving_mean_sales=data[(data['Date'] == Thanksgiving1) | (data['Date'] == Thanksgiving2) | (data['Date'] == Thanksgiving3) | (data['Date'] == Thanksgiving4)]
LabourDay_mean_sales=data[(data['Date'] == LabourDay1) | (data['Date'] == LabourDay2) | (data['Date'] == LabourDay3) | (data['Date'] == LabourDay4)]
SuperBowl_mean_sales=data[(data['Date'] == SuperBowl1) | (data['Date'] == SuperBowl2) | (data['Date'] == SuperBowl3) | (data['Date'] == SuperBowl4)]
#
list_of_mean_sales = {'Christmas_mean_sales' : round(Christmas_mean_sales['Weekly_Sales'].mean(),2),
'Thanksgiving_mean_sales': round(Thanksgiving_mean_sales['Weekly_Sales'].mean(),2),
'LabourDay_mean_sales' : round(LabourDay_mean_sales['Weekly_Sales'].mean(),2),
'SuperBowl_mean_sales':round(SuperBowl_mean_sales['Weekly_Sales'].mean(),2),
'Non holiday weekly sales' : data[data['Holiday_Flag'] == 0 ]['Weekly_Sales'].mean()}
list_of_mean_sales

# From above Figure we can infer that the mean sales of thanks giving is more than the non holiday weekly sales .

# #### QUESTION 5 :-Provide a Monthly,Quaterly and Semester view of sales in units and give insights.

# In[68]:

#Monthly sales
monthly = data.groupby(pd.Grouper(key='Date', freq='1M')).sum()# groupby each 1 month
monthly=monthly.reset_index()
fig, ax = plt.subplots(figsize=(8,6))
X = monthly['Date']
Y = monthly['Weekly_Sales']
plt.plot(X,Y)
plt.title('Month Wise Sales')
plt.xlabel('Monthly')
plt.ylabel('Weekly_Sales')

# We can observe from the Monthly Sales Graph that highest sum of sales is recorded in between jan-2011 to march-2011.

# In[69]:

#Quaterly Sales
Quaterly = data.groupby(pd.Grouper(key='Date', freq='3M')).sum()
Quaterly = Quaterly.reset_index()
fig, ax = plt.subplots(figsize=(8,6))
X = Quaterly['Date']
Y = Quaterly['Weekly_Sales']
plt.plot(X,Y)
plt.title('Quaterly Wise Sales')
plt.xlabel('Quaterly')
plt.ylabel('Weekly_Sales')

# We can observe from the Quarterly Sales Graph that higest sum of sales is recorded in Q1 of 2011 and 2012.

# In[70]:

#Semester Sales
Semester = data.groupby(pd.Grouper(key='Date', freq='6M')).sum()
Semester = Semester.reset_index()
fig, ax = plt.subplots(figsize=(8,6))
X = Semester['Date']
Y = Semester['Weekly_Sales']
plt.plot(X,Y)
plt.title('Semester Wise Sales')
plt.xlabel('Semester')
plt.ylabel('Weekly_Sales')

# We can Observe from Semester Sales graph that at beginning of 1st sem of 2010 and 1st sem of 2013 sales are lowest.

# ### Build  prediction models to forecast demand

# In[72]:

# find outliers
fig, axs = plt.subplots(4,figsize=(6,18))
X = data[['Temperature','Fuel_Price','CPI','Unemployment']]
for i,column in enumerate(X):
    sb.boxplot(data[column], ax=axs[i])

# In[20]:

# drop the outliers
data_new = data[(data['Unemployment']<10) & (data['Unemployment']>4.5) & (data['Temperature']>10)]
data_new

# In[21]:

# check outliers
fig, axs = plt.subplots(4,figsize=(6,18))
X = data_new[['Temperature','Fuel_Price','CPI','Unemployment']]
for i,column in enumerate(X):
    sb.boxplot(data_new[column], ax=axs[i])

# ### Bulding the model

# In[22]:

# Import sklearn
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn import metrics
from sklearn.linear_model import LinearRegression

# In[50]:

# Select features and target
X = data_new[['Store','Fuel_Price','CPI','Unemployment']]
y = data_new['Weekly_Sales']

# Split data to train and test (0.80:0.20)
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2)

# In[52]:

# Linear Regression model
print('Linear Regression:')
print()
reg = LinearRegression()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
print('Accuracy:',reg.score(X_train, y_train)*100)

print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

sb.scatterplot(y_pred, y_test);

# In[54]:

# Random Forest Regressor
print('Random Forest Regressor:')
print()
rfr = RandomForestRegressor(n_estimators = 400,max_depth=15,n_jobs=5)
rfr.fit(X_train,y_train)
y_pred=rfr.predict(X_test)
print('Accuracy:',rfr.score(X_test, y_test)*100)

print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred)))

sb.scatterplot(y_pred, y_test);

# In[ ]:

# In[ ]:


import os
import argparse
import pandas as pd
from lxml import etree

NAMESPACE = 'http://openquake.org/xmlns/nrml/0.4'
GML_NAMESPACE = 'http://www.opengis.net/gml'
SERIALIZE_NS_MAP = {None: NAMESPACE, 'gml': GML_NAMESPACE}

def csv_to_xml(input_csv, output_xml):
    """
    Converts the CSV vulnerability model file to the NRML format
    """
    data = pd.io.parsers.read_csv(input_csv)
    grouped_by_set = data.groupby(['vuln_set_id','vuln_func_id'])
    vuln_model = {}
    for (vuln_set_id, vuln_func_id), group in grouped_by_set:
        if vuln_set_id not in vuln_model:
            vuln_model[vuln_set_id] = {}
            vuln_model[vuln_set_id]['asset_cat'] = group['asset_cat'].tolist()[0]
            vuln_model[vuln_set_id]['loss_cat'] = group['loss_cat'].tolist()[0]
            vuln_model[vuln_set_id]['imt'] = group['imt'].tolist()[0]
            vuln_model[vuln_set_id]['iml'] = group['iml'].tolist()
            vuln_model[vuln_set_id]['functions'] = []

        vuln_func = {}
        vuln_func['vuln_func_id'] = vuln_func_id
        vuln_func['distr'] = group['distr'].tolist()[0]
        vuln_func['mean_lr'] = group['mean_lr'].tolist()
        vuln_func['stddev_lr'] = group['stddev_lr'].tolist()

        vuln_model[vuln_set_id]['functions'].append(vuln_func)

    with open(output_xml, "w") as f:
        root = etree.Element('nrml', nsmap=SERIALIZE_NS_MAP)
        node_vm = etree.SubElement(root, "vulnerabilityModel")
        for vuln_set_id, vuln_func_set in vuln_model.iteritems():
            node_dvs = etree.SubElement(node_vm, "discreteVulnerabilitySet")
            node_dvs.set("vulnerabilitySetID", vuln_set_id)
            node_dvs.set("assetCategory", vuln_func_set['asset_cat'])
            node_dvs.set("lossCategory", vuln_func_set['loss_cat'])
            node_iml = etree.SubElement(node_dvs, "IML")
            node_iml.set("IMT", vuln_func_set['imt'])
            node_iml.text = " ".join(map(str, vuln_func_set['iml']))

            for vuln_func in vuln_func_set['functions']:
                node_dvf = etree.SubElement(node_dvs, "discreteVulnerability")
                node_dvf.set("vulnerabilityFunctionID", vuln_func['vuln_func_id'])
                node_dvf.set("probabilisticDistribution", vuln_func['distr'])
                node_lr = etree.SubElement(node_dvf, "lossRatio")
                node_lr.text = " ".join(map(str, vuln_func['mean_lr']))
                node_cv = etree.SubElement(node_dvf, "coefficientsVariation")
                node_cv.text = " ".join(map(str, vuln_func['stddev_lr']))

        f.write(etree.tostring(root, pretty_print=True, xml_declaration=True, encoding='UTF-8'))

def xml_to_csv (input_xml, output_csv):
    """
    Converts the XML vulnerability model file to the CSV format
    """
    print('This feature will be implemented in a future release.')

def set_up_arg_parser():
    """
    Can run as executable. To do so, set up the command line parser
    """

    description = ('Convert a Vulnerability Model from CSV to XML and '
                   'vice versa.\n\nTo convert from CSV to XML: '
                   '\npython vulnerability_model_converter.py '
                   '--input-csv-file PATH_TO_VULNERABILITY_MODEL_CSV_FILE '
                   '--output-xml-file PATH_TO_OUTPUT_XML_FILE'
                   '\n\nTo convert from XML to CSV type: '
                   '\npython vulnerability_model_converter.py '
                   '--input-xml-file PATH_TO_VULNERABILITY_MODEL_XML_FILE '
                   '--output-csv-file PATH_TO_OUTPUT_CSV_FILE')

    parser = argparse.ArgumentParser(description=description, formatter_class=argparse.RawTextHelpFormatter)
    flags = parser.add_argument_group('flag arguments')

    group_input = flags.add_mutually_exclusive_group(required=True)
    group_input.add_argument('--input-xml-file',
                       help='path to vulnerability model XML file',
                       default=None)
    group_input.add_argument('--input-csv-file',
                       help='path to vulnerability model CSV file',
                       default=None)

    group_output = flags.add_mutually_exclusive_group()
    group_output.add_argument('--output-xml-file',
                              help='path to output XML file',
                              default=None,
                              required=False)
    group_output.add_argument('--output-csv-file',
                              help='path to output CSV file',
                              default=None,
                              required=False)
    return parser

if __name__ == "__main__":

    parser = set_up_arg_parser()
    args = parser.parse_args()
    if args.input_csv_file:
        if args.output_xml_file:
            output_file = args.output_xml_file
        else:
            (filename, ext) = os.path.splitext(args.input_csv_file)
            output_file = filename + '.xml'
        csv_to_xml(args.input_csv_file, output_file)
    elif args.input_xml_file:
        if args.output_csv_file:
            output_file = args.output_csv_file
        else:
            (filename, ext) = os.path.splitext(args.input_xml_file)
            output_file = filename + '.csv'
        xml_to_csv(args.input_xml_file, output_file)
    else:
        parser.print_usage()


#!/usr/bin/env python

import json
import os
from argparse import ArgumentParser, RawTextHelpFormatter
from pathlib import Path
from textwrap import dedent

from Hive.utils.log_utils import (
    get_logger,
    add_verbosity_options_to_argparser,
    log_lvl_from_verbosity_args,
)

DESC = dedent(
    """
    Run nnDetection pre-processing, data analysis, and image data unpacking.
    The CL scripts called are  ``nndet_prep`` and ``nndet_unpack``, with the arguments extracted from the given configuration file.
    """  # noqa: E501
)
EPILOG = dedent(
    """
    Example call:
    ::
        {filename} --config-file /PATH/TO/CONFIG_FILE.json
    """.format(  # noqa: E501
        filename=Path(__file__).stem
    )
)

def get_arg_parser():
    pars = ArgumentParser(description=DESC, epilog=EPILOG, formatter_class=RawTextHelpFormatter)

    pars.add_argument(
        "--config-file",
        type=str,
        required=True,
        help="File path for the configuration dictionary, used to retrieve experiment variables (**Task_ID**, **results_folder**, ...) ",
        # noqa: E501
    )

    pars.add_argument(
        "--n-workers",
        type=str,
        default=None,
        help="Number of parallel processes used when pre-processing and unpacking the image data (Default: ``N_THREADS``)",
    )

    add_verbosity_options_to_argparser(pars)

    return pars

def main():
    parser = get_arg_parser()
    arguments, unknown_arguments = parser.parse_known_args()
    args = vars(arguments)

    logger = get_logger(  # NOQA: F841
        name=Path(__file__).name,
        level=log_lvl_from_verbosity_args(args),
    )
    config_file = args["config_file"]

    n_workers = "1"
    if args["n_workers"] is None:
        if "N_THREADS" in os.environ is not None:
            n_workers = str(os.environ["N_THREADS"])
    else:
        n_workers = str(args["n_workers"])

    with open(config_file) as json_file:
        data = json.load(json_file)

        arguments = [data["Task_ID"], "-np", n_workers, "-npp", n_workers]

        os.environ["det_data"] = data["base_folder"]
        os.environ["OMP_NUM_THREADS"] = "1"

        os.environ["nnUNet_def_n_proc"] = n_workers
        os.environ["det_models"] = data["results_folder"]
        arguments.extend(unknown_arguments)
        os.system("nndet_prep " + " ".join(arguments))
        os.system(
            "nndet_unpack {} {}".format(
                str(
                    Path(os.environ["det_data"]).joinpath(
                        "Task{}_{}".format(data["Task_ID"], data["Task_Name"]), "preprocessed", "D3V001_3d", "imagesTr"
                    )
                ),
                n_workers,
            )
        )

if __name__ == "__main__":
    main()


#!/usr/bin/env python3

##The scholar crawler v 1.0
# It's as elegant as a drunk elephant

import argparse
from scholarly import scholarly
import re
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from tqdm import tqdm

## Going to use object orientation to keep publications data organized
## Not going to make the whole script object oriented though

class Paper:
    def __init__(self, pub):
        self.pub = pub
        self.url = None
        self.priority = 0
        self.text = None
        self.year = None
        self.domain = None
        self.period = None
        self.citations = None
        self.minim_citations = None

    def read(self):
        """Read contents of a web page and closes the browser"""

        #Open chrome silently in the background

        chrome_options = Options()
        chrome_options.add_argument("--headless")

        #Read webpage data

        wd = webdriver.Chrome(options=chrome_options)
        wd.get(self.url)
        text = BeautifulSoup(wd.page_source,"lxml").get_text()
        wd.close()
        self.text = text

    def get_url(self):
        """Get publication url from publication data"""
        self.url = self.pub["pub_url"]

    def get_year(self):
        """Get publication year from the copyright"""
        self.year = int(self.pub["bib"]["pub_year"])

    def in_period(self, period):
        """Filter Paper by time period"""
        if "-" in period:
            period = period.split("-")
        else:
            period = period.split()

        if self.year:
            self.period = ((self.year > int(period[0])) and (self.year < int(period[1])))

    def keywords_filter(self, filter_words):
        """Assign a score to publication based on the presence of keywords"""
        if self.text:
            for word in filter_words:
                if re.findall(word.lower(), self.text):
                    self.priority = int(self.priority) + 1

    def filter_domains(self, domains):
        """Filter Paper by domain"""
        for domain in domains.split():
            if re.findall(domain.lower(), self.url):
                self.domain = re.findall(domain.lower(), self.url)[0]

    def get_citations(self):
        """get the number of citations from scholarly data"""
        if self.pub["num_citations"]:
            self.citations = int(self.pub["num_citations"])

    def filter_citations(self, citations):
        """Return if the number of the citations is igual or grater than the minimum"""
        if self.citations:
            self.minim_citations = (self.citations >= int(citations))

    def norm_priority(self):
        """Makes sure publication score is within 0 to 10 range"""
        if self.priority is not None:
            self.priority = int(self.priority) % 10
        else:
            self.priority = 0

def get_first_n_pubs(query, n):
    """Create a list of dictionaries with the first n publications data"""
    ## Must be done with next() since query is an iterator and
    ## isn't subscriptable
    return [next(query) for i in tqdm(range(0, n))]

def get_arguments():
    """Return the query filter parameters and output name from comandline arguments"""

    parser = argparse.ArgumentParser()

    parser.add_argument("-q", "--query", help="The query you want to search")
    parser.add_argument("-kw", "--keywords", help="The key words of interest")
    parser.add_argument("-n", "--number", help="Maximum number of papers in the output")
    parser.add_argument("-p", "--period", help="The time period to filter for (optional)")
    parser.add_argument("-d", "--domains", help="Filter by domain name (optional)")
    parser.add_argument("-c", "--citations", help="number of citations(optional)")
    parser.add_argument("-o", "--output", help="Output file name")

    return parser.parse_args()

def make_url_score_output(lst):

    """Converts the publications urls and scores into a string that can be written in the output file"""

    string = ""

    for paper in lst:

        string += f"{paper.url},  {paper.priority}\n\n"

    string = "#url, #score\n" + string

    return string

def write_output(string, filename):
    """Writes data into a file"""
    with open(filename, "w") as f:
        f.write(string)

def main():

    args = get_arguments()

    query = args.query #the query string
    keywords = args.keywords # keywords to refine the search
    n = int(args.number) #maximum number of publications to search
    filename = args.output #output file name
    period = args.period #time period to filter
    domains = args.domains #domain names to filter
    citations = args.citations #Minimum nuymber of citations

    ## Init a query object from scholarly
    search = scholarly.search_pubs(query)

    ## Iterates getting the first n publications data

    print("\nSearching Google scholar ... \n")

    pubs = get_first_n_pubs(search, n)

    papers = [] ## Publications which were treated will be from now on regarded as papers

    for pub in tqdm(pubs):
        try:
             papers.append(Paper(pub)) ## Initialize a Paper type object for every publication
        except Exception as e:
            raise e
        finally:
            pass

    print("\nCrawling .... \n")

    ##Load Paper data into list of Paper objects
    ##Read the page text

    for paper in tqdm(papers):
        try:
            paper.get_url() ## Get the publication url for a Paper object
            paper.read() ## Read the webpage content

            if paper.text:
                paper.keywords_filter(keywords) ##Assign a score to the publication
                paper.norm_priority()
            else:
                papers.remove(paper)
        except Exception as e:
            raise e
        finally:
            pass

    if period: ## If flag -p was used

        print("\nFiltering by period ... \n")

        for paper in papers:
            try:
                paper.get_year() #get publication year
                paper.in_period(period) # check if the publication year is within the time interval
            except Exception as e:
                raise e
            finally:
                pass

        #Filter the publications that are in the time interval set by the user
        papers = list(filter(lambda x: x.period, papers))

    if domains: ## If Domains is not None filter publications by domains

        print(f"\nFiltering by Domain ... \n")

        for paper in tqdm(papers):
            try:
                paper.filter_domains(domains) ##Checks if the publication url is a domain specified by the user
            except Exception as e:
                raise e
            finally:
                pass
        ## Filters the Papers based by if their url domain is in the user suplied input
        papers = list(filter(lambda x: x.domain, papers))

    if citations: ## if -c flag was used

        print(f"\nFiltering by Citations ... \n")

        for paper in tqdm(papers):
            try:
                paper.get_citations() #Get citations from publication data
                paper.filter_citations(citations) ## Checks if publication has minimum number of citations
            except Exception as e:
                raise e
            finally:
                pass
        ## Filters the publications by if they have the minimum number of citations
        papers = list(filter(lambda x: x.minim_citations , papers))

    #Sort the papers by their score
    papers.sort(key=lambda x: x.priority, reverse=True)

    #Make data into a string that can be written to the output file
    string = make_url_score_output(papers)

    #writes data to an output file
    write_output(string, filename)

    print("\nDone!")

if __name__ == '__main__':
    main()


import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.ops import rnn, rnn_cell
from data_pre import *
from lstm import *
from nltk import sent_tokenize, word_tokenize
from nltk.stem.snowball import SnowballStemmer as stemmer
import sys

def test_lstm(sent ,prediction):
	sent = np.array(sent_to_vec(sent[:-1], lexicon)).reshape((-1, n_chunks, chunk_size))
	with tf.Session() as sess:
		saver.restore(sess, 'model.ckpt')
		p = tf.argmax(prediction,1)
		pre = p.eval(feed_dict={x:sent}, session=sess)
	return pre

# sent = ['to', 'contribute', 'any', 'really', 'good', 'enigmas']
# sent = ['.--', 'Fancying', 'you', 'to', 'have', 'fathomed']
# print(gre[test_lstm(sent, predict)[0]])

input = "All children, except one, grow up. They soon know that they will grow\
up, and the way Wendy knew was this. One day when she was two years old\
she was playing in a garden, and she plucked another flower and ran with\
it to her mother. I suppose she must have looked rather delightful, for\
Mrs. Darling put her hand to her heart and cried, “Oh, why can’t you\
remain like this for ever!” This was all that passed between them on\
the subject, but henceforth Wendy knew that she must grow up. You always\
know after you are two. Two is the beginning of the end."

stem = stemmer("english")
def changeable():
	# read in the changeable word vocabulary
	return set(line.strip('\n') for line in open('synonym.txt'))

change = changeable()
def replacable(word):
	word = stem.stem(word)
	if word in change:
		return True
	else: return False

def complifier2():
	input = "Of course they lived at 14 [their house number on their street], and\
until Wendy came her mother was the chief one. She was a lovely lady,\
with a romantic mind and such a sweet mocking mouth. Her romantic\
mind was like the tiny boxes, one within the other, that come from the\
puzzling East, however many you discover there is always one more; and\
her sweet mocking mouth had one kiss on it that Wendy could never get,\
though there it was, perfectly conspicuous in the right-hand corner."
	sents = ""
	output = ""
	for line in input:
		sents += line.replace('\n',' ')
	sents = sent_tokenize(sents)
	for sent in sents:
		sent = word_tokenize(sent)
		for word in sent:
			if replacable(word):
				i = sent.index(word)
				sixgram = []
				count = 5
				while count > 0:
					if sent[i-count]:
						sixgram.append(sent[i-count])
					else: sixgram.append('un_k')
					count = count - 1
				sixgram.append(word)
				replaceword = gre[test_lstm(sixgram, predict)[0]]
				output += '
'+word+''+replaceword+'
'
					# add something to implement the tooltip
			else:
				# print(word)
				output = output + word + " "
				# add something to implement to tooltip
	return output

# def test_argu(sys.argv[1]):
# 	print(sys.argv[1])
# print(complifier2(sys.argv[1]))
output2 = complifier2()
# if __name__=='__main__':
# 	sys.exit(main(sys.argv[1]))


class DisplayData:
    def __init__(self, dataset, textbox):
        # display class data
        data_info_string = ('Dataset Name: ' + dataset.name +
                            '\n\n' + 'Number of Classes: ' + str(dataset.class_count) +
                            '\n\n' + 'Number of Attributes: ' + str(dataset.attribute_count) +
                            '\n\n' + 'Number of Samples: ' + str(dataset.sample_count))

        # loop through class names
        counter = 1
        for ele in dataset.class_names:
            data_info_string += ('\n\n' + 'Class ' + str(counter) + ': ' + str(ele) +
                                 '\n' + '--Count: ' + str(dataset.count_per_class[counter - 1]))
            counter += 1

        textbox.setText(data_info_string)


from collections import Counter

ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
class Card:
    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit
        self.value = ranks.index(rank)
    def __str__(self):
        return f'{self.rank} of {self.suit}'
    def __eq__(self, other):
        return self.rank == other.rank and self.suit == other.suit
    def __hash__(self):
        return hash((self.rank, self.suit))

def get_card_input(deck, existing_cards):
    card_input = input("Enter the card (e.g. 'AH' for Ace of Hearts): ")
    suit_input = card_input[-1].lower()
    rank_input = card_input[:-1].upper()
    suits = {'c': 'Clubs', 'd': 'Diamonds', 'h': 'Hearts', 's': 'Spades'}
    ranks = {'2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', '10': '10', 'J': 'Jack', 'Q': 'Queen', 'K': 'King', 'A': 'Ace'}
    if suit_input not in suits:
        print("Invalid suit input. Please enter a valid suit (c, d, h, s).")
        return None
    if rank_input not in ranks:
        print("Invalid rank input. Please enter a valid rank (2-10, J, Q, K, A).")
        return None
    suit_input = suits[suit_input]
    rank_input = ranks[rank_input]
    card_to_add = Card(rank_input, suit_input)
    if card_to_add in deck and card_to_add not in existing_cards:
        return card_to_add
    elif card_to_add in existing_cards:
        print(f"{card_to_add} already in the existing cards.")
        return None
    else:
        print(f"{card_to_add} not in the deck.")
        return None

def get_user_hand(deck):
    existing_cards = set()
    while len(existing_cards) < 2:
        card = get_card_input(deck, existing_cards)
        if card:
            existing_cards.add(card)
            deck.remove(card)
            print(f"{card} added to your hand.")
    return existing_cards

def get_river(deck):
    river = set()

    # Flop Input
    input_flop = input("Enter flop details (y/n) ?").lower()
    if input_flop == "y":
        while len(river) < 3:
            card = get_card_input(deck, river)
            if card:
                river.add(card)
                deck.remove(card)
                print(f"{card} added to the flop.")

        # Turn Input
        input_turn = input("Enter turn details (y/n) ?").lower()
        if input_turn == "y":
            while len(river) < 4:
                card = get_card_input(deck, river)
                if card:
                    river.add(card)
                    deck.remove(card)
                    print(f"{card} added to the turn.")

            # River Input
            input_river = input("Enter river details (y/n) ?").lower()
            if input_river == "y":
                while len(river) < 5:
                    card = get_card_input(deck, river)
                    if card:
                        river.add(card)
                        deck.remove(card)
                        print(f"{card} added to the river.")
    return river

def determine_hand_type(hand, river):
    hand_types = [royal_flush, straight_flush, four_of_a_kind, full_house, flush, straight, three_of_a_kind, two_pair, pair, high_card]
    for hand_type in hand_types:
        result = hand_type(hand, river)
        if result:
            hand_type_name = hand_type.__name__.replace("_", " ")
            print(f"\nHand type: {hand_type_name}")
            return result
    print("No hand detected")

"""
Helper Functions
"""
def value_count(N, hand, river= set([])):
    combined_hand = hand.union(river)
    ranks = ['2','3','4','5','6','7','8','9','10','Jack','Queen','King','Ace']
    value_count_list = []
    for rank in ranks:
        count = 0
        rank_cards = []
        for card in combined_hand:
            if card.rank == rank:
                count += 1
                rank_cards.append(card)
        if count == N:
            value_count_list.append(rank_cards)
    return value_count_list

def complete_hand(sig_hand, hand, river):
    combined_hand = hand.union(river)
    final_hand = sig_hand.copy()
    num_missing_cards = 5 - len(sig_hand)
    remaining_cards = combined_hand.difference(sig_hand)
    remaining_cards = sorted(remaining_cards, key=lambda card: card.value, reverse=True)
    final_hand.extend(remaining_cards[:num_missing_cards])
    return final_hand

"""
Hand type functions
"""
def high_card(hand, river=set([])):
    combined_hand = hand.union(river)
    sorted_hand = sorted(combined_hand, key=lambda card: card.value, reverse=True)
    final_hand = []
    final_hand.append(sorted_hand[0:5])
    return final_hand

def pair(hand, river=set([])):
    combined_hand = hand.union(river)
    rank_count = {}
    for card in combined_hand:
        if card.rank not in rank_count:
            rank_count[card.rank] = 1
        else:
            rank_count[card.rank] += 1
    final_list = []
    for rank, count in rank_count.items():
        if count >= 2:
            pair_cards = [card for card in combined_hand if card.rank == rank]
            other_cards = [card for card in combined_hand if card.rank != rank]
            other_cards = sorted(other_cards, key=lambda card: card.value, reverse=True)
            final_sort = pair_cards + other_cards[:3]
            if final_sort not in final_list:
                final_list.append(final_sort)
    return final_list

def two_pair(hand, river=set([])):
    combined_hand = hand.union(river)
    rank_count = {}
    for card in combined_hand:
        if card.rank not in rank_count:
            rank_count[card.rank] = 1
        else:
            rank_count[card.rank] += 1
    pair_list = []
    for rank, count in rank_count.items():
        if count >= 2:
            pair_cards = [card for card in combined_hand if card.rank == rank]
            other_cards = [card for card in combined_hand if card.rank != rank]
            pair_list.append(pair_cards)
    two_pair_list = []
    if len(pair_list) >= 2:
        for i in range(len(pair_list)):
            for j in range(i+1, len(pair_list)):
                two_pair_hand = pair_list[i][:2] + pair_list[j][:2]
                other_cards = [card for card in combined_hand if card not in two_pair_hand]
                other_cards = sorted(other_cards, key=lambda card: card.value, reverse=True)
                final_sort = two_pair_hand + [other_cards[0]]
                two_pair_list.append(final_sort)
    return two_pair_list

def three_of_a_kind(hand, river=set([])):
    three_of_a_kind_list = value_count(3, hand, river)
    for i in range(len(three_of_a_kind_list)):
        three_of_a_kind_list[i] = complete_hand(three_of_a_kind_list[i][:3], hand, river)
    return three_of_a_kind_list

def four_of_a_kind(hand, river=set([])):
    four_of_a_kind_list = value_count(4, hand, river)
    for i in range(len(four_of_a_kind_list)):
        four_of_a_kind_list[i] = complete_hand(four_of_a_kind_list[i][:4], hand, river)
    return four_of_a_kind_list

def flush(hand, river = set([])):
    combined_hand = hand.union(river)
    flush_list = []
    suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    for suit in suits:
        suit_cards = [card for card in combined_hand if card.suit == suit]
        if len(suit_cards) >= 5:
            flush_list.append(sorted(suit_cards, key=lambda card: card.value, reverse=True)[:5])
    return flush_list

def straight(hand, river = set([])):
    combined_hand = hand.union(river)
    straight_list = []
    ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace']
    unique_ranks_in_hand = {card.rank for card in combined_hand}

    for i in range(len(ranks) - 4):
        is_straight = all(ranks[j] in unique_ranks_in_hand for j in range(i, i + 5))
        if is_straight:
            straight_cards = [card for card in combined_hand if card.rank in ranks[i:i+5]]
            sorted_straight = sorted(straight_cards, key=lambda card: ranks.index(card.rank), reverse=True)[:5]
            straight_list.append(sorted_straight)

    # Special case: Straight with Ace as the low card ('5', '4', '3', '2', 'Ace')
    if all(rank in unique_ranks_in_hand for rank in ['2', '3', '4', '5', 'Ace']):
        straight_cards = [card for card in combined_hand if card.rank in ['2', '3', '4', '5', 'Ace']]
        sorted_straight = sorted(straight_cards, key=lambda card: ranks.index(card.rank), reverse=True)
        # Place the Ace at the end
        sorted_straight = sorted_straight[:-1] + [sorted_straight[-1]]
        straight_list.append(sorted_straight)

    return straight_list

def full_house(hand, river=set([])):
    combined_hand = hand.union(river)
    rank_count = {}
    for card in combined_hand:
        if card.rank not in rank_count:
            rank_count[card.rank] = 1
        else:
            rank_count[card.rank] += 1
    three_of_a_kind = None
    pair = None
    for rank, count in rank_count.items():
        if count == 3:
            three_of_a_kind = [card for card in combined_hand if card.rank == rank]
        elif count == 2:
            pair = [card for card in combined_hand if card.rank == rank]
    if three_of_a_kind is not None and pair is not None:
        full_house_cards = three_of_a_kind + pair
        return [sorted(full_house_cards, key=lambda card: card.value, reverse=True)[:5]]
    else:
        return []

def straight_flush(hand, river):
    combined_hand = hand.union(river)
    flush_list = flush(combined_hand)
    straight_flush_list = []
    for flush_hand in flush_list:
        straight_list = straight(set(flush_hand))
        for consec in straight_list:
            if len(set([card.suit for card in consec])) == 1:
                straight_flush_list.append(consec)
    return straight_flush_list

def royal_flush(hand, river):
    combined_hand = hand.union(river)
    royal_flush_cards = {Card('Ace', 'Hearts'), Card('King', 'Hearts'), Card('Queen', 'Hearts'), Card('Jack', 'Hearts'), Card('10', 'Hearts')}

    for card in royal_flush_cards:
        if card not in combined_hand:
            return []
    return [list(royal_flush_cards)]

def main():
    ranks = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
    suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
    deck = [Card(rank, suit) for suit in suits for rank in ranks]

    # User prompted to enter their card details
    print("User Hand Card Details\n")
    user_hand = get_user_hand(deck)

    # User prompted to enter the river's card details
    print("River Card Details\n")
    river = get_river(deck)

    # OUTPUT
    best_hand = determine_hand_type(user_hand, river)[0]
    print("Containing: ")
    for card in best_hand:
        print(f"  - {card}")
    print("\n")

if __name__ == "__main__":
    main()


from django.shortcuts import get_object_or_404
from rest_framework.response import Response
from rest_framework.throttling import UserRateThrottle, AnonRateThrottle
from rest_framework.views import APIView

from plane.models import Plane, Ticket
from .serializer import TicketSerializer, PlaneSerializer

class PlaneAPIView(APIView):
    throttle_classes = [AnonRateThrottle]

    def get(self, request, *args, **kwargs):
        pk = self.kwargs.get('pk')
        if pk is not None:
            # Logic for retrieving a specific reservation by pk
            try:
                plane = Plane.objects.get(pk=pk)
                serializer = PlaneSerializer(plane)
                return Response(serializer.data)
            except Plane.DoesNotExist:
                return Response(status=404)  # Return a not found response if the reservation doesn't exist
        else:
            # Logic for retrieving all reservations
            planes = Plane.objects.all()
            serializer = PlaneSerializer(planes, many=True)
            return Response(serializer.data)

class TicketAPIView(APIView):
    throttle_classes = [UserRateThrottle]

    def get(self, request, *args, **kwargs):
        pk = self.kwargs.get('pk')
        if pk is not None:
            # Logic for retrieving a specific reservation by pk
            try:
                ticket = Ticket.objects.get(pk=pk)
                serializer = TicketSerializer(ticket)
                return Response(serializer.data)
            except Ticket.DoesNotExist:
                return Response(status=404)  # Return a not found response if the reservation doesn't exist
        else:
            # Logic for retrieving all reservations
            reservations = Ticket.objects.all()
            serializer = TicketSerializer(reservations, many=True)
            return Response(serializer.data)

    def post(self, request):
        plane_id = request.data.get('plane_id')
        total = request.data.get('total')

        plane = get_object_or_404(Plane, pk=plane_id)
        if not plane.capacity != 0:
            return Response({'error': 'The plane is full.'}, status=400)

        reservation = Ticket(
            user=request.my_user,
            plane=plane,
            total=total
        )
        reservation.save()
        if plane.capacity - total < 0:
            return Response({'error': 'request is more that plane capacity.'}, status=400)
        plane.capacity -= total
        plane.save()

        serializer = TicketSerializer(reservation)

        return Response(serializer.data)

    def __delete__(self, request, pk):
        try:
            reservation = Ticket.objects.get(pk=pk)

            plane = reservation.plane
            plane.capacity += reservation.total
            plane.save()

            reservation.delete()
            return Response(status=204)  # Return a success response if deletion is successful
        except Ticket.DoesNotExist:
            return Response(status=404)  # Return a not found response if the instance doesn't exist


import datetime as dt
import json
from os import getenv
from typing import Callable

import requests
from flask import Flask, jsonify, request

# create your API token, and set it up in Postman collection as part of the Body section
API_TOKEN = getenv('API_TOKEN')
# you can get API keys for free here - https://www.weatherapi.com/my/
WEATHER_API_KEY = getenv('WEATHER_API_KEY')

app = Flask(__name__)

url_base_url = 'http://api.weatherapi.com/v1'

def get_current_weather(q: str, lang: str = None):
    """
    Return the current weather for the given parameters.

    `q` could be the following:
        * Latitude and Longitude (Decimal degree) e.g: q=48.8567,2.3508
        * city name e.g.: q=Paris

    :param q: Query parameter based on which data is sent back.
    :param lang: Optional. Returns 'condition:text' field in API in the desired language.
    :return: The current weather for the given parameters.
    """
    url_endpoint = 'current.json'
    url = f"{url_base_url}/{url_endpoint}"

    params = {
        'key': WEATHER_API_KEY,
        'q': q
    }
    if lang:
        params['lang'] = lang

    response = requests.request('GET', url, params=params)
    return json.loads(response.text)

def get_weather_forecast(q: str, days: int, lang: str = None):
    """
    Return the forecast weather for the given parameters.

    `q` could be the following:
        * Latitude and Longitude (Decimal degree) e.g: q=48.8567,2.3508
        * city name e.g.: q=Paris

    `days` parameter value ranges between 1 and 14.
        If no days parameter is provided then only today's weather is returned

    :param q: Query parameter based on which data is sent back.
    :param days: Number of days of forecast required.
    :param lang: Optional. Returns 'condition:text' field in API in the desired language.
    :return: The weather forecast for the given parameters.
    """
    url_endpoint = 'forecast.json'
    url = f"{url_base_url}/{url_endpoint}"

    params = {
        'key': WEATHER_API_KEY,
        'q': q,
        'days': days
    }
    if lang:
        params['lang'] = lang

    response = requests.request('GET', url, params=params)
    return json.loads(response.text)

def get_weather_history(q: str, dt: str, end_dt: str = None, lang: str = None):
    """
    Return the history weather for the given parameters.

    `q` could be the following:
        * Latitude and Longitude (Decimal degree) e.g: q=48.8567,2.3508
        * city name e.g.: q=Paris

    `dt` and `end_dt` should be on or after 1st Jan, 2010 in yyyy-MM-dd format (i.e. dt=2010-01-01)
    `end_dt` should be greater than 'dt' parameter
        and difference should not be more than 30 days between the two dates.
    If `end_dt` isn't provided, the history weather will be returned only for `dt` date.

    :param q: Query parameter based on which data is sent back.
    :param dt: Start date, from which history weather is returned
    :param end_dt: End date, to which history weather is returned
    :param lang: Optional. Returns 'condition:text' field in API in the desired language.
    :return: The current weather for the given parameters.
    """
    url_endpoint = 'history.json'
    url = f"{url_base_url}/{url_endpoint}"

    params = {
        'key': WEATHER_API_KEY,
        'q': q,
        'dt': dt
    }
    if end_dt:
        params['end_dt'] = end_dt
    if lang:
        params['lang'] = lang

    response = requests.request('GET', url, params=params)
    return json.loads(response.text)

class InvalidUsage(Exception):
    status_code = 400

    def __init__(self, message, status_code=None, payload=None):
        Exception.__init__(self)
        self.message = message
        if status_code is not None:
            self.status_code = status_code
        self.payload = payload

    @property
    def to_dict(self):
        rv = dict(self.payload or ())
        rv["message"] = self.message
        return rv

def authorize_and_validate_request(f: Callable):
    def wrapper():
        json_data = request.get_json()

        if json_data.get("token") is None:
            raise InvalidUsage("token is required", status_code=400)

        token = json_data.get("token")

        if token != API_TOKEN:
            raise InvalidUsage("wrong API token", status_code=403)

        if not json_data.get('weather'):
            raise InvalidUsage('weather params must be provided', status_code=400)

        return f()
    wrapper.__name__ = f.__name__
    return wrapper

def add_execution_time_params_to_response(f: Callable[[], dict]):
    def wrapper():
        start_dt = dt.datetime.now()
        response = f()
        end_dt = dt.datetime.now()
        execution_time_params = {
            "event_start_datetime": start_dt.isoformat(),
            "event_finished_datetime": end_dt.isoformat(),
            "event_duration": str(end_dt - start_dt),
        }
        response.update(execution_time_params)
        return response
    wrapper.__name__ = f.__name__
    return wrapper

@app.errorhandler(InvalidUsage)
def handle_invalid_usage(error):
    response = jsonify(error.to_dict)
    response.status_code = error.status_code
    return response

@app.route("/")
def home_page():
    return '

KMA L2: Python Saas. Weather

'

@app.route(
    '/content/api/v1/integration/weather/current',
    methods=["POST"],
)
@authorize_and_validate_request
@add_execution_time_params_to_response
def current_weather_endpoint():
    json_data: dict = request.get_json()

    weather_params = json_data.get('weather')
    weather = get_current_weather(weather_params.get('q'), weather_params.get('lang'))

    result = {
        "weather": weather
    }

    return result

@app.route(
    '/content/api/v1/integration/weather/forecast',
    methods=["POST"],
)
@authorize_and_validate_request
@add_execution_time_params_to_response
def weather_forecast_endpoint():
    json_data: dict = request.get_json()

    weather_params = json_data.get('weather')
    weather = get_weather_forecast(
        weather_params.get('q'), weather_params.get('days'),
        weather_params.get('lang')
    )

    result = {
        "weather": weather
    }

    return result

@app.route(
    '/content/api/v1/integration/weather/history',
    methods=["POST"],
)
@authorize_and_validate_request
@add_execution_time_params_to_response
def weather_history_endpoint():
    json_data: dict = request.get_json()

    weather_params: dict = json_data.get('weather')
    weather = get_weather_history(
        weather_params.get('q'),
        weather_params.get('dt'), weather_params.get('end_dt'),
        weather_params.get('lang')
    )

    result = {
        "weather": weather
    }

    return result


from __future__ import print_function
from __future__ import division
import argparse
import os
import random
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable;
import sys
sys.path.append('./model/')
from dataset import *
from invmodel import *
from utils import *
from Lutils import *
from ply import *
from cmap import colorcoord;
import torch.nn.functional as F
import sys
import os
import json
import time, datetime

sys.path.append("./ext/");
import dist_chamfer as ext;
distChamfer =  ext.chamferDist();

def eval_ae(net,cd_meter,inv_meter,pts):
    with torch.no_grad():
        points = Variable(pts);
        points = points.transpose(2,1).contiguous()
        points = points.cuda()
        pts_res,inv_err  = net(points)
        dist1, dist2 = distChamfer(points.transpose(2,1).contiguous(),pts_res);
        cd = (torch.mean(dist1)) + (torch.mean(dist2))
        cd_meter.update(cd.data);
        inv_meter.update(inv_err.data);
    return cd.data,inv_err.data;

def train_ae(net,optimizer,cd_meter,inv_meter,pts,opt):
    optimizer.zero_grad();
    points = Variable(pts);
    points = points.transpose(2,1).contiguous();
    points = points.cuda();
    pts_res,inv_err = network(points);
    inv_meter.update(inv_err.data[0]);
    dist1, dist2 = distChamfer(points.transpose(2,1).contiguous(),pts_res);
    cd = (torch.mean(dist1)) + (torch.mean(dist2));
    cd_meter.update(cd.data);
    loss = cd + opt.w*inv_err;
    #loss = inv_err;
    loss.backward();
    optimizer.step();
    return loss,cd,inv_err;

def eval_svr(net,cd_meter,inv_meter,pts,img):
    with torch.no_grad():
        img = Variable(img);
        img = img.cuda();
        points = Variable(pts);
        points = points.cuda();
        pts_res,inv_err = net(img);
        dist1, dist2 = distChamfer(points,pts_res);
        cd = (torch.mean(dist1)) + (torch.mean(dist2));
        cd_meter.update(cd.data);
        inv_meter.update(inv_err.data);
    return cd.data,inv_err.data;

def train_svr(net,optimizer,cd_meter,inv_meter,pts,img,opt):
    optimizer.zero_grad();
    img = Variable(img);
    img = img.cuda();
    points = Variable(pts);
    points = points.cuda();
    pts_res,inv_err = network(img);
    inv_meter.update(inv_err.data);
    dist1, dist2 = distChamfer(points,pts_res);
    cd = (torch.mean(dist1)) + (torch.mean(dist2));
    cd_meter.update(cd.data);
    loss = cd + opt.w*inv_err;
    loss.backward();
    optimizer.step();
    return loss,cd,inv_err;

def write_log(logname,val_cd,val_inv,dataset_test,train_cd=None,train_inv=None,epoch=None):
    log_dict = {};
    log_dict['val_cd'] = np.asscalar(val_cd.avg.cpu().numpy());
    log_dict['val_inv'] = np.asscalar(val_inv.avg.cpu().numpy());
    for item in dataset_test.cat:
        print(item,np.asscalar(dataset_test.perCatValueMeter[item].avg.cpu().numpy()))
        log_dict.update({item:np.asscalar(dataset_test.perCatValueMeter[item].avg.cpu().numpy())})
    if train_cd is not None:
        log_dict['train_cd'] = np.asscalar(train_cd.avg.cpu().numpy());
    if train_inv is not None:
        log_dict['train_inv'] = np.asscalar(train_inv.avg.cpu().numpy());
    if epoch is not None:
        log_dict['epoch'] = epoch;
    with open(logname, 'a') as f: #open and append
        f.write('json_stats: '+json.dumps(log_dict)+'\n');
    return;

bestnum = 10;
best_cd = np.zeros(bestnum);
best_all = np.zeros(bestnum);

def save_model(logname,dirname,net,opt,vcd,vall):
    global best_cd;
    global best_all;
    if opt.part == 'full':
        sdict = net.state_dict();
    elif opt.part == 'en':
        sdict = net.encoder.state_dict();
    elif opt.part == 'de':
        sdict = net.all_decoder.state_dict();
    cdname = dirname+os.sep+opt.mode+'_'+opt.part+'_cd';
    allname = dirname+os.sep+opt.mode+'_'+opt.part+'_all';
    if vcd < best_cd[-1]:
        best_cd[-1] = vcd;
        best_cd = np.sort(best_cd);
        bidx = np.searchsorted(best_cd,vcd);
        for idx in range(bestnum-2,bidx-1,-1):
            if os.path.exists(cdname+'_%d'%idx+'.pth'):
                if os.path.exists(cdname+'_%d'%(idx+1)+'.pth'):
                    os.remove(cdname+'_%d'%(idx+1)+'.pth');
                print('rename '+cdname+'_%d'%(idx)+'.pth'+' '+cdname+'_%d'%(idx+1)+'.pth');
                os.rename(cdname+'_%d'%(idx)+'.pth',cdname+'_%d'%(idx+1)+'.pth');
        print('saving model');
        torch.save(sdict,cdname+'_%d'%(bidx)+'.pth');
        with open(logname,'a') as f:
            f.write('best_cd:'+np.array2string(best_cd,precision=6,separator=',')+'\n');
    if vall < best_all[-1]:
        best_all[-1] = vall;
        best_all = np.sort(best_all);
        bidx = np.searchsorted(best_all,vall);
        for idx in range(bestnum-2,bidx-1,-1):
            if os.path.exists(allname+'_%d'%idx+'.pth'):
                if os.path.exists(allname+'_%d'%(idx+1)+'.pth'):
                    os.remove(allname+'_%d'%(idx+1)+'.pth');
                print('rename '+allname+'_%d'%(idx)+'.pth'+' '+allname+'_%d'%(idx+1)+'.pth');
                os.rename(allname+'_%d'%(idx)+'.pth',allname+'_%d'%(idx+1)+'.pth');
        print('saving model');
        torch.save(sdict,allname+'_%d'%(bidx)+'.pth');
        with open(logname,'a') as f:
            f.write('best_all:'+np.array2string(best_all,precision=6,separator=',')+'\n');

def load_model(path,net):
    bname = os.path.basename(path);
    if '_full_' in bname:
        net.load_state_dict(torch.load(path));
    elif '_en_' in bname:
        net.encoder.load_state_dict(torch.load(path));
    elif '_de_' in bname:
        net.all_decoder.load_state_dict(torch.load(path));
    else:
        net.load_state_dict(torch.load(path));

def view_color(y,c=None):
    if c is None:
        c = colorcoord(y);
    return pd.concat([pd.DataFrame(y),pd.DataFrame(c)],axis=1,ignore_index=True);

def view_ae(dirname,net,pts,index,cat,opt):
    points = Variable(pts, volatile=True);
    points = points.transpose(2,1).contiguous();
    points = points.cuda();
    grid = None;
    fidx = None;
    if opt.grid_dim == 3:
        grid,Li,Lw,fidx = sphere_grid(points.size()[0],opt.pts_num,'cot');
    elif opt.grid_dim == 2:
        grid,Li,Lw,fidx = patch_grid(points.size()[0],opt.pts_num,opt.grid_num);
    grid = Variable(grid,volatile=True);
    grid = grid.cuda();
    y,inv_err  = net(points,grid);
    y_inv = net.inv_y;
    ply_path = dirname+os.sep+'ply';
    if not os.path.exists(ply_path):
        os.mkdir(ply_path);
    T=np.dtype([("n",np.uint8),("i0",np.int32),('i1',np.int32),('i2',np.int32)]);
    face = np.zeros(shape=[fidx.shape[0]],dtype=T);
    for i in range(fidx.shape[0]):
        face[i] = (3,fidx[i,0],fidx[i,1],fidx[i,2]);
    y = y.cpu().data.numpy();
    inv_y = net.inv_y.cpu().data.numpy();
    grid = grid.transpose(2,1).contiguous().cpu().data.numpy();
    c = colorcoord(grid[0,...])
    write_ply(ply_path+os.sep+'%02d_%s_grid.ply'%(index,cat[0]),points = view_color(grid[0,...],c),faces=pd.DataFrame(face),color=True);
    for i in range(y.shape[0]):
        write_ply(ply_path+os.sep+'%02d_%02d_%s.ply'%(index,i,cat[0]),points = view_color(y[i,...],c),faces=pd.DataFrame(face),color=True);
        write_ply(ply_path+os.sep+'%02d_%02d_%s_inv.ply'%(index,i,cat[0]),points = view_color(inv_y[i,...],c),faces=pd.DataFrame(face),color=True);

def view_svr(dirname,net,img,index,cat,opt):
    img = Variable(img,volatile=True);
    img = img.cuda();
    grid = None;
    fidx = None;
    if opt.grid_dim == 3:
        grid,Li,Lw,fidx = sphere_grid(points.size()[0],opt.pts_num,'cot');
    elif opt.grid_dim == 2:
        grid,Li,Lw,fidx = patch_grid(points.size()[0],opt.pts_num,opt.grid_num);
    grid = Variable(grid,volatile=True);
    grid = grid.cuda();
    y,inv_err  = net(img,grid);
    ply_path = dirname+os.sep+'ply';
    if not os.path.exists(ply_path):
        os.mkdir(ply_path);
    T=np.dtype([("n",np.uint8),("i0",np.int32),('i1',np.int32),('i2',np.int32)]);
    face = np.zeros(shape=[fidx.shape[0]],dtype=T);
    for i in range(fidx.shape[0]):
        face[i] = (3,fidx[i,0],fidx[i,1],fidx[i,2]);
    y = y.cpu().data.numpy();
    inv_y = net.inv_y.cpu().data.numpy();
    grid = grid.transpose(2,1).contiguous().cpu().data.numpy();
    c = colorcoord(grid[0,...])
    write_ply(ply_path+os.sep+'%02d_%s_grid.ply'%(index,cat[0]),points = view_color(grid[0,...],c),faces=pd.DataFrame(face),color=True);
    for i in range(y.shape[0]):
        write_ply(ply_path+os.sep+'%02d_%02d_%s.ply'%(index,i,cat[0]),points = view_color(y[i,...],c),faces=pd.DataFrame(face),color=True);
        write_ply(ply_path+os.sep+'%02d_%02d_%s_inv.ply'%(index,i,cat[0]),points = view_color(inv_y[i,...],c),faces=pd.DataFrame(face),color=True);

parser = argparse.ArgumentParser()
parser.add_argument('--batchSize','-bs',type=int,default=32,help='input batch size')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=6)
parser.add_argument('--nepoch', type=int, default=400, help='number of epochs to train for')
parser.add_argument('--model','-m',type=str,default = '',help='model path');
parser.add_argument('--part','-p',type=str,default = 'full',help='save part');
parser.add_argument('--train_part','-tp',type=str,default = 'full',help='train part');
parser.add_argument('--mode','-md', type=str, default = 'AE',  help='mode');
parser.add_argument('--pts_num', type=int, default = 2500,  help='number of points')
parser.add_argument('--grid_num', type=int, default = 1,  help='number of grid')
parser.add_argument('--grid_dim', type=int, default = 3,  help='dim of grid')
parser.add_argument('--dir', type=str, default = '', help='dir')
parser.add_argument('--lr',type=float,default=0.001,help='set learning rate');
parser.add_argument('--weight_decay',type=float,default=1e-9,help='set weight decay');
parser.add_argument('--gpu','-gpu',type=str,default='0',help='set gpu');
parser.add_argument('--ply',type=int,default=0,help='write out ply');
parser.add_argument('--w','-w',type=float,default=1.0,help='train on all data');

opt = parser.parse_args();
if not 'CUDA_VISIBLE_DEVICES' in os.environ:
    os.environ['CUDA_VISIBLE_DEVICES'] = '';
if opt.gpu:
    os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpu;
print('visible gpu '+os.environ['CUDA_VISIBLE_DEVICES']);
now = datetime.datetime.now();
if opt.dir == '':
    save_path = 'InvAtlasNet'+now.isoformat();
else:
    save_path = opt.dir;
dir_name =  os.path.join('log',save_path);
if not os.path.exists(dir_name):
    os.mkdir(dir_name);
logname = os.path.join(dir_name,'log.txt');

opt.manualSeed = random.randint(1,10000) # fix seed
print("Random Seed:", opt.manualSeed);
random.seed(opt.manualSeed);
torch.manual_seed(opt.manualSeed);

#Create train/test dataloader on new views and test dataset on new models
SVR = False;
if opt.mode == 'SVR':
    SVR = True;

dataset = ShapeNet( SVR=SVR, normal = False, class_choice = None, train=True)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=opt.batchSize,
                                          shuffle=True, num_workers=int(opt.workers))

dataset_test = ShapeNet( SVR=SVR, normal = False, class_choice = None, train=False)
dataloader_test = torch.utils.data.DataLoader(dataset_test, batch_size=opt.batchSize,
                                          shuffle=False, num_workers=int(opt.workers))

print('training set',len(dataset.datapath));
print('testing set',len(dataset_test.datapath));

cudnn.benchmark = True;
len_dataset = len(dataset);

#create network
network = InvAtlasNet(num_points=opt.pts_num,grid_num=opt.grid_num,grid_dim=opt.grid_dim,mode=opt.mode);
network.cuda();
#=====================================================
with open(logname,'a') as f:
    for v in sys.argv:
        f.write(str(v)+' ');
    f.write('\n');
    f.write(str(network)+'\n');
#=====================================================
if opt.model != '':
    try:
        load_model(opt.model,network);
    except:
        print("Try partial");
        partial_restore(network,opt.model);
    print(" Previous weight loaded ");
#======================================================
val_cd = AverageValueMeter();
val_cd.reset();
val_inv = AverageValueMeter();
val_inv.reset();
#=====================================================
network.eval();
for i, data in enumerate(dataloader_test, 0):
    img, points, cat, _ , _ = data;
    if SVR:
        cd,inv = eval_svr(network,val_cd,val_inv,points,img);
    else:
        cd,inv = eval_ae(network,val_cd,val_inv,points);
    val_cd.update(cd);
    val_inv.update(inv);
    dataset_test.perCatValueMeter[cat[0]].update(cd);
    if bool(opt.ply):
        if SVR:
            view_svr(dir_name,network,img,i,cat,opt);
        else:
            view_ae(dir_name,network,points,i,cat,opt);
    print('[%d: %d/%d] val loss:  %f '%(0,i,len(dataset_test)/opt.batchSize,cd));
write_log(logname,val_cd,val_inv,dataset_test);
best_cd[...] = np.asscalar(val_cd.avg.cpu().numpy()); + 0.01;
best_all[...] = np.asscalar(val_cd.avg.cpu().numpy()) + np.asscalar(val_inv.avg.cpu().numpy()); + 0.01;
#=====================================================
optimizer = None;
if opt.train_part == 'en':
    optimizer = optim.Adam(network.encoder.parameters(),lr = opt.lr,weight_decay=opt.weight_decay);
elif opt.train_part == 'de':
    optimizer = optim.Adam(network.decoder.parameters(),lr = opt.lr,weight_decay=opt.weight_decay);
elif opt.train_part == 'full':
    optimizer = optim.Adam(network.parameters(),lr = opt.lr,weight_decay=opt.weight_decay);
elif opt.train_part == 'inv':
    optimizer = optim.Adam(network.inv_decoder.parameters(),lr = opt.lr,weight_decay=opt.weight_decay);
elif opt.train_part == 'deinv':
    param = [];
    param.extend(network.decoder.parameters());
    param.extend(network.inv_decoder.parameters());
    optimizer = optim.Adam(param,lr = opt.lr,weight_decay=opt.weight_decay);
elif opt.train_part == 'redeinv':
    network.decoder.apply(weights_init);
    network.inv_decoder.apply(weights_init);
    param = [];
    param.extend(network.decoder.parameters());
    param.extend(network.inv_decoder.parameters());
    optimizer = optim.Adam(param,lr = opt.lr,weight_decay=opt.weight_decay);
#=====================================================
trainloss_acc0 = 1e-9;
trainloss_accs = 0;
train_cd = AverageValueMeter();
train_inv = AverageValueMeter();
#=====================================================
nb = len(dataset)/opt.batchSize
for epoch in range(opt.nepoch):
    #TRAIN MODE
    train_cd.reset();
    train_inv.reset();
    network.train()
    for i, data in enumerate(dataloader, 0):
        img, points, cat, _ , _= data;
        if SVR:
            loss,cd,inv_err = train_svr(network,optimizer,train_cd,train_inv,points,img,opt);
        else:
            loss,cd,inv_err = train_ae(network,optimizer,train_cd,train_inv,points,opt);
        trainloss_accs = trainloss_accs * 0.99 + loss.data[0];
        trainloss_acc0 = trainloss_acc0 * 0.99 + 1;
        print('[%d: %d/%d] train loss:%f,%f,%f/%f' %(epoch,i,nb,cd.data[0],inv_err.data[0],loss.data[0],trainloss_accs/trainloss_acc0));
    #VALIDATION
    val_cd.reset();
    val_inv.reset();
    for item in dataset_test.cat:
        dataset_test.perCatValueMeter[item].reset();
#======================================================
    network.eval()
    for i, data in enumerate(dataloader_test, 0):
        img, points, cat, _, _ = data
        if SVR:
            cd,inv = eval_svr(network,val_cd,val_inv,points,img);
        else:
            cd,inv = eval_ae(network,val_cd,val_inv,points);
        val_cd.update(cd);
        val_inv.update(inv);
        dataset_test.perCatValueMeter[cat[0]].update(cd);
        print('[%d: %d/%d] val loss:%f ' %(epoch,i,len(dataset_test)/opt.batchSize,cd));
    write_log(logname,val_cd,val_inv,dataset_test,train_cd,train_inv,epoch);
#=======================================================
    save_model(logname,dir_name,network,opt,val_cd.avg,val_cd.avg+val_inv.avg);


import sys
from numbers import Real

from hypothesis import given

from shewchuk import Expansion
from tests.utils import skip_reference_counter_test
from . import strategies

@given(strategies.expansions)
def test_basic(expansion: Expansion) -> None:
    result = expansion.imag

    assert isinstance(result, int)

@given(strategies.expansions)
def test_zeroness(expansion: Expansion) -> None:
    result = expansion.imag

    assert result == 0

@skip_reference_counter_test
@given(strategies.expansions)
def test_reference_counter(expansion: Expansion) -> None:
    expansion_refcount_before = sys.getrefcount(expansion)

    result = expansion.imag

    expansion_refcount_after = sys.getrefcount(expansion)
    assert expansion_refcount_after == expansion_refcount_before


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

'''
Created on Mar 12, 2017

@author: Yusuke Kawatsu
'''

import numpy as np
from sklearn import datasets
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model.logistic import LogisticRegression
import matplotlib.pyplot as plt

def plot():
    # load samples.
    iris = datasets.load_iris()
    X = iris.data[:, [2, 3]]
    y = iris.target

    # split data for testing and training.
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    # standardize.
    sc = StandardScaler()
    sc.fit(X_train)
    X_train_std = sc.transform(X_train)
    X_test_std = sc.transform(X_test)

    # calc.
    weights, params = [], []
    for c in np.arange(-5, 5):
        lr = LogisticRegression(C=10**c, random_state=0)
        lr.fit(X_train_std, y_train)

        # classes --> features.
        # http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html
        weights.append(lr.coef_[1])
        params.append(10**c)

    # convert.
    weights = np.array(weights)

    # plot.
    plt.plot(params, weights[:, 0], label='petal length')
    plt.plot(params, weights[:, 1], label='petal width', linestyle='--')
    plt.ylabel('weight coefficient')
    plt.xlabel('C')
    plt.legend(loc='upper left')
    plt.xscale('log')

    plt.show()

if __name__ == '__main__':
    plot()


from random import *
G = "АУЕЫОЭЯИЮ"
S = "ЦКНГШЩЗХФВПРЛДЖЧСМТБ"
lout = [i+j for i in G for j in S] + [j+i for i in G for j in S] + [i for i in G]

N = int(input())

sout = ""
while len(sout) < N:
    sout += choice(lout)

print(sout)