"""
Input pipeline (tf.dataset and input_fn) for GQN datasets.
Adapted from the implementation provided here:
https://github.com/deepmind/gqn-datasets/blob/acca9db6d9aa7cfa4c41ded45ccb96fecc9b272e/data_reader.py
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import os
import tensorflow as tf

# ---------- ad-hoc data structures ----------

DatasetInfo = collections.namedtuple(
    'DatasetInfo',
    ['basepath', 'train_size', 'test_size', 'frame_size', 'sequence_size']
)
Context = collections.namedtuple('Context', ['frames', 'cameras'])
Query = collections.namedtuple('Query', ['context', 'query_camera'])
TaskData = collections.namedtuple('TaskData', ['query', 'target'])

# ---------- dataset constants ----------

_DATASETS = dict(
    jaco=DatasetInfo(
        basepath='jaco',
        train_size=3600,
        test_size=400,
        frame_size=64,
        sequence_size=11),

    mazes=DatasetInfo(
        basepath='mazes',
        train_size=1080,
        test_size=120,
        frame_size=84,
        sequence_size=300),

    rooms_free_camera_with_object_rotations=DatasetInfo(
        basepath='rooms_free_camera_with_object_rotations',
        train_size=2034,
        test_size=226,
        frame_size=128,
        sequence_size=10),

    rooms_ring_camera=DatasetInfo(
        basepath='rooms_ring_camera',
        train_size=2160,
        test_size=240,
        frame_size=64,
        sequence_size=10),

    # super-small subset of rooms_ring for debugging purposes
    rooms_ring_camera_debug=DatasetInfo(
        basepath='rooms_ring_camera_debug',
        train_size=1,
        test_size=1,
        frame_size=64,
        sequence_size=10),

    rooms_free_camera_no_object_rotations=DatasetInfo(
        basepath='rooms_free_camera_no_object_rotations',
        train_size=2160,
        test_size=240,
        frame_size=64,
        sequence_size=10),

    shepard_metzler_5_parts=DatasetInfo(
        basepath='shepard_metzler_5_parts',
        train_size=900,
        test_size=100,
        frame_size=64,
        sequence_size=15),

    shepard_metzler_7_parts=DatasetInfo(
        basepath='shepard_metzler_7_parts',
        train_size=900,
        test_size=100,
        frame_size=64,
        sequence_size=15)
)
_NUM_CHANNELS = 3
_NUM_RAW_CAMERA_PARAMS = 5
_MODES = ('train', 'test')

# ---------- helper functions ----------

def _convert_frame_data(jpeg_data):
  decoded_frames = tf.image.decode_jpeg(jpeg_data)
  return tf.image.convert_image_dtype(decoded_frames, dtype=tf.float32)

def _get_dataset_files(dataset_info, mode, root):
  """Generates lists of files for a given dataset version."""
  basepath = dataset_info.basepath
  base = os.path.join(root, basepath, mode)
  if mode == 'train':
    num_files = dataset_info.train_size
  else:
    num_files = dataset_info.test_size
  length = len(str(num_files))
  template = '{:0%d}-of-{:0%d}.tfrecord' % (length, length)
  record_paths = [  # indexing runs from 1 to n
      os.path.join(base, template.format(i, num_files))
      for i in range(1, num_files + 1)]
  return record_paths

def _get_randomized_indices(context_size, dataset_info, seed):
  """Generates randomized indices into a sequence of a specific length."""
  example_size = context_size + 1
  indices = tf.range(0, dataset_info.sequence_size)
  indices = tf.random_shuffle(indices, seed=seed)
  indices = tf.slice(indices, begin=[0], size=[example_size])
  return indices

def _parse(raw_data, dataset_info):
  """Parses raw data from the tfrecord."""
  feature_map = {
      'frames': tf.FixedLenFeature(
          shape=dataset_info.sequence_size, dtype=tf.string),
      'cameras': tf.FixedLenFeature(
          shape=[dataset_info.sequence_size * _NUM_RAW_CAMERA_PARAMS],
          dtype=tf.float32)
  }
  # example = tf.parse_example(raw_data, feature_map)
  example = tf.parse_single_example(raw_data, feature_map)
  return example

def _preprocess(example, indices, context_size, custom_frame_size, dataset_info):
  """Preprocesses the parsed data."""
  # frames
  example_size = context_size + 1
  frames = tf.concat(example['frames'], axis=0)
  frames = tf.gather(frames, indices, axis=0)
  frames = tf.map_fn(
      _convert_frame_data, tf.reshape(frames, [-1]),
      dtype=tf.float32, back_prop=False)
  dataset_image_dimensions = tuple(
      [dataset_info.frame_size] * 2 + [_NUM_CHANNELS])
  frames = tf.reshape(
      frames, (example_size, ) + dataset_image_dimensions)
  if (custom_frame_size and
      custom_frame_size != dataset_info.frame_size):
    frames = tf.reshape(frames, dataset_image_dimensions)
    new_frame_dimensions = (custom_frame_size,) * 2 + (_NUM_CHANNELS,)
    frames = tf.image.resize_bilinear(
        frames, new_frame_dimensions[:2], align_corners=True)
    frames = tf.reshape(
        frames, (-1, example_size) + new_frame_dimensions)
  # cameras
  raw_pose_params = example['cameras']
  raw_pose_params = tf.reshape(
      raw_pose_params,
      [dataset_info.sequence_size, _NUM_RAW_CAMERA_PARAMS])
  raw_pose_params = tf.gather(raw_pose_params, indices, axis=0)
  pos = raw_pose_params[:, 0:3]
  yaw = raw_pose_params[:, 3:4]
  pitch = raw_pose_params[:, 4:5]
  cameras = tf.concat(
      [pos, tf.sin(yaw), tf.cos(yaw), tf.sin(pitch), tf.cos(pitch)], axis=-1)
  # return preprocessed tuple
  preprocessed_example = {}
  preprocessed_example['frames'] = frames
  preprocessed_example['cameras'] = cameras
  return preprocessed_example

def _prepare(preprocessed_example):
  """Prepares the preprocessed data into (feature, label) tuples."""
  # decompose
  frames = preprocessed_example['frames']
  cameras = preprocessed_example['cameras']
  # split data
  context_frames = frames[:-1]
  context_cameras = cameras[:-1]
  target = frames[-1]
  query_camera = cameras[-1]
  context = Context(cameras=context_cameras, frames=context_frames)
  query = Query(context=context, query_camera=query_camera)
  data = TaskData(query=query, target=target)
  return data, data.target

# ---------- input_fn ----------

def gqn_input_fn(
    dataset_name,
    root,
    mode,
    context_size,
    batch_size=1,
    num_epochs=1,
    # optionally reshape frames
    custom_frame_size=None,
    # queue params
    num_threads=4,
    buffer_size=256,
    seed=None):
  """
  Creates a tf.data.Dataset based op that returns data.
    Args:
      dataset_name: string, one of ['jaco', 'mazes', 'rooms_ring_camera',
          'rooms_free_camera_no_object_rotations',
          'rooms_free_camera_with_object_rotations', 'shepard_metzler_5_parts',
          'shepard_metzler_7_parts'].
      root: string, path to the root folder of the data.
      mode: one of tf.estimator.ModeKeys.
      context_size: integer, number of views to be used to assemble the context.
      batch_size: (optional) batch size, defaults to 1.
      num_epochs: (optional) number of times to go through the dataset,
          defaults to 1.
      custom_frame_size: (optional) integer, required size of the returned
          frames, defaults to None.
      num_threads: (optional) integer, number of threads used to read and parse
          the record files, defaults to 4.
      buffer_size: (optional) integer, capacity of the underlying prefetch or
          shuffle buffer, defaults to 256.
      seed: (optional) integer, seed for the random number generators used in
          the dataset.

    Returns:
      tf.data.dataset yielding tuples of the form (features, labels)
      shapes:
        features.query.context.cameras: [N, K, 7]
        features.query.context.frames: [N, K, H, W, 3]
        features.query.query_camera: [N, 7]
        features.target (same as labels): [N, H, W, 3]

    Raises:
      ValueError: if the required version does not exist; if the required mode
         is not supported; if the requested context_size is bigger than the
         maximum supported for the given dataset version.
  """

  # map estimator mode key to dataset internal mode strings
  if mode == tf.estimator.ModeKeys.TRAIN:
    str_mode = 'train'
  else:
    str_mode = 'test'
  # check validity of requested dataset and split
  if dataset_name not in _DATASETS:
    raise ValueError('Unrecognized dataset {} requested. Available datasets '
                      'are {}'.format(dataset_name, _DATASETS.keys()))
  if str_mode not in _MODES:
    raise ValueError('Unsupported mode {} requested. Supported modes '
                      'are {}'.format(str_mode, _MODES))
  # retrieve dataset parameters
  dataset_info = _DATASETS[dataset_name]
  if context_size >= dataset_info.sequence_size:
    raise ValueError(
        'Maximum support context size for dataset {} is {}, but '
        'was {}.'.format(
            dataset_name, dataset_info.sequence_size-1, context_size))
  # collect the paths to all tfrecord files
  record_paths = _get_dataset_files(dataset_info, str_mode, root)
  # create TFRecordDataset
  dataset = tf.data.TFRecordDataset(
      filenames=record_paths, num_parallel_reads=num_threads)
  # parse the data from tfrecords
  dataset = dataset.map(
      lambda raw_data: _parse(raw_data, dataset_info),
      num_parallel_calls=num_threads)
  # preprocess into context and target
  indices = _get_randomized_indices(context_size, dataset_info, seed)
  dataset = dataset.map(
      lambda example: _preprocess(example, indices, context_size, custom_frame_size, dataset_info),
      num_parallel_calls=num_threads)
  # parse into tuple expected by tf.estimator input_fn
  dataset = dataset.map(_prepare, num_parallel_calls=num_threads)
  # shuffle data
  if mode == tf.estimator.ModeKeys.TRAIN:
    dataset = dataset.shuffle(buffer_size=(buffer_size * batch_size), seed=seed)
  # set up batching
  dataset = dataset.repeat(num_epochs)
  dataset = dataset.batch(batch_size)
  dataset = dataset.prefetch(buffer_size)
  return dataset


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jan  9 14:52:17 2022

@author: elie
"""

#################### SCALING ####################

import os
os.chdir('/home/elie/Documents/Tecnico/2ND_PERIOD/DS/PROJECT/CODE/')

from pandas import read_csv, DataFrame, concat, unique
from pandas.plotting import register_matplotlib_converters
from matplotlib.pyplot import subplots, show, figure, savefig
from ds_charts import get_variable_types, multiple_line_chart, plot_evaluation_results
from sklearn.preprocessing import MinMaxScaler, StandardScaler
from numpy import ndarray
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

register_matplotlib_converters()

############################# GET DATA ###############################
path = "Data/"
file = path+"air_quality_tabular_without_na"
#file = path+"nyc_car_crash_without_na"
filename = file+".csv"
data = read_csv(filename, na_values='', parse_dates=True, infer_datetime_format=True)

# SPLIT DATA BASED ON TYPE OF VARIABLE
variable_types = get_variable_types(data)
numeric_vars = variable_types['Numeric']
symbolic_vars = variable_types['Symbolic']
boolean_vars = variable_types['Binary']

df_nr = data[numeric_vars]
df_sb = data[symbolic_vars]
df_bool = data[boolean_vars]

# remove symbolic values before computation : date, time, id
data = data.drop(symbolic_vars, axis=1)

############################# NORMALIZATION ###############################

# Z SCORE
transf = StandardScaler(with_mean=True, with_std=True, copy=True).fit(df_nr)
tmp = DataFrame(transf.transform(df_nr), index=data.index, columns= numeric_vars)
norm_data_zscore = concat([tmp, df_sb,  df_bool], axis=1)
#norm_data_zscore.to_csv(f'{file}_scaled_zscore.csv', index=False)
print(norm_data_zscore.describe())

norm_data_zscore = norm_data_zscore.drop(symbolic_vars, axis=1)

# MIN MAX SCALER
transf = MinMaxScaler(feature_range=(0, 1), copy=True).fit(df_nr)
tmp = DataFrame(transf.transform(df_nr), index=data.index, columns= numeric_vars)
norm_data_minmax = concat([tmp, df_sb,  df_bool], axis=1)
#norm_data_minmax.to_csv(f'{file}_scaled_minmax.csv', index=False)
print(norm_data_minmax.describe())

norm_data_minmax = norm_data_minmax.drop(symbolic_vars, axis=1)

# fig, axs = subplots(1, 3, figsize=(20,10),squeeze=False)
# axs[0, 0].set_title('Original data')
# data.boxplot(ax=axs[0, 0])
# axs[0, 1].set_title('Z-score normalization')
# norm_data_zscore.boxplot(ax=axs[0, 1])
# axs[0, 2].set_title('MinMax normalization')
# norm_data_minmax.boxplot(ax=axs[0, 2])
# show()

################################## KNN ##################################

nb_rows = norm_data_zscore.shape[0]
sample_pct = 0.33
norm_data_zscore = norm_data_zscore.sample(n=round(nb_rows*sample_pct), random_state=1)
norm_data_minmax = norm_data_minmax.sample(n=round(nb_rows*sample_pct), random_state=1)

potential_cols = ["ALARM"]
nvalues = [10, 15, 20, 25, 30, 35, 40, 45]
dist = ['manhattan', 'euclidean', 'chebyshev']
values = {}
best = (0, '')
last_best = 0

for c in potential_cols:
    target = c
    y = norm_data_zscore.pop(target).values
    X_train, X_test, y_train, y_test = train_test_split(norm_data_zscore, y, test_size=0.33, random_state=42)
    labels = unique(y_train)
    labels.sort()
    for d in dist:
        yvalues = []
        for n in nvalues:
            knn = KNeighborsClassifier(n_neighbors=n, metric=d)
            knn.fit(X_train, y_train)
            y_pred = knn.predict(X_test)
            current_accuracy = accuracy_score(y_test, y_pred)
            yvalues.append(current_accuracy)
            print("For column : "+str(c)+" Accuracy with n = "+str(n)+ " and distance : "+str(d)+" => "+str(current_accuracy))
            # if yvalues[-1] > last_best:
            #     best = (n, d)
            #     last_best = yvalues[-1]
        values[d] = yvalues
    figure()
    multiple_line_chart(nvalues, values, title='KNN variants', xlabel='n', ylabel='accuracy', percentage=True)
    #savefig('images/{file_tag}_knn_study.png')
    show()

# figure()
# multiple_line_chart(nvalues, values, title='KNN variants', xlabel='n', ylabel='accuracy', percentage=True)
# #savefig('images/{file_tag}_knn_study.png')
# show()
# print('Best results with %d neighbors and %s'%(best[0], best[1]))

# ###### CONFUSION MATRIX #######

clf = knn = KNeighborsClassifier(n_neighbors=6, metric="manhattan")
clf.fit(X_train, y_train)
train_pred = clf.predict(X_train)
test_pred = clf.predict(X_test)
plot_evaluation_results(labels, y_train, train_pred, y_test, test_pred)
#savefig('images/{file_tag}_knn_best.png')
show()

###############################   TEST  ###################################"

# # GET THE PREPROCESSED DATA WITHOUT NA
# path = "Data/"
# #file = path+"air_quality_tabular_without_na"
# file = path+"NYC_collisions_tabular"
# filename = file+".csv"
# data_bis = read_csv(filename, na_values='', parse_dates=True, infer_datetime_format=True)

# for col in data_bis.columns:
#     print("COL : "+str(col))
#     print("/////////////////")
#     print(data_bis[col].value_counts())
#     print("\n\n\n")


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

"""
Description: Deep Patch Learning Model
Author: wondervictor
"""

import math
import torch
import torch.nn as nn
import numpy as np
import layers
import basenet

class PatchHeadNetwork(nn.Module):

    def __init__(self, use_cuda, num_classes, use_relation=False):
        super(PatchHeadNetwork, self).__init__()

        self.roi_align = layers.ROIAlign(out_size=7, spatial_scale=0.0625)

        self.fc = nn.Sequential(
            nn.Linear(512*7*7, 4096),
            nn.LeakyReLU(negative_slope=0.02, inplace=True),
            nn.Dropout(0.5),
            nn.Linear(4096, 4096),
            nn.LeakyReLU(negative_slope=0.02, inplace=True),
            nn.Dropout(0.5)
        )

        self.patch_encoder = nn.Linear(4096, 256)
        self.cls_score1 = nn.Linear(256*8, num_classes)
        self.cls_score2 = nn.Linear(4096, num_classes)
        self.patch_pooling = layers.MaxPatchPooling(use_cuda)
        self.spm_pooling = layers.SPMMaxPooling()

        for m in self.modules():
            if isinstance(m, nn.Linear):
                m.weight.data.normal_(0.0, 0.01)
                m.bias.data.uniform_(-0.5, 0.5)

    def forward(self, features, shapes, rois):
        # N denotes the num_rois, B denotes the batchsize
        # features: B*C*H*W
        # rois:   N*5
        # shapes: B*2

        batch_size = features.size()[0]

        roi_output = self.roi_align(features, rois)
        # N*512*7*7

        num_rois = rois.size()[0]
        output_batch_id = np.zeros(num_rois, dtype=np.int32)
        for i in xrange(num_rois):
            batch_id = int(rois[i].data[0])
            output_batch_id[i] = batch_id

        patch_features = roi_output.view(-1, 512*7*7)
        # patch_features: N * (512*7*7)

        patch_features = self.fc(patch_features)
        # patch_features: N * 4096
        encoded_features = self.patch_encoder(patch_features)
        spm_features = self.spm_pooling(encoded_features, shapes, rois)
        spm_features = spm_features.view(batch_size, 256 * 8)
        cls_score1 = self.cls_score1(spm_features)
        cls_score2_features = self.cls_score2(patch_features)
        cls_score2 = self.patch_pooling(cls_score2_features, batch_size, output_batch_id)
        det_scores = cls_score2_features
        return cls_score1, cls_score2, det_scores

class DPL(nn.Module):

    def __init__(self, use_cuda, num_classes=20, enable_base_grad=False, base='vgg', pretrained=True, use_relation=False):
        super(DPL, self).__init__()
        if base == 'vgg':
            self.cnn = basenet.VGG16()
        elif base == 'resnet50':
            self.cnn = basenet.ResNet50(pretrained=True)
        elif base == 'resnet34':
            self.cnn = basenet.ResNet34(pretrained=True)
        if not enable_base_grad:
            print("Not Enable Base Model Gradient")
            for param in self.cnn.parameters():
                param.require_grad = False
        self.use_cuda = use_cuda
        self.head_network = PatchHeadNetwork(use_cuda=use_cuda, num_classes=num_classes, use_relation=use_relation)

    def freeze_bn(self):
        for layer in self.cnn.modules():
            if isinstance(layer, nn.BatchNorm2d):
                layer.eval()

    def forward(self, images, shapes, rois):
        features = self.cnn(images)
        cls_score1, cls_score2, det_scores = self.head_network(features, shapes, rois)
        return cls_score1, cls_score2, det_scores


import requests
import json
import logging
import sys
import json
import pandas as pd
from pathlib import Path
from requests_html import HTMLSession

def parse_and_download_files(servicetags_public, msftpublic_ips, officeworldwide_ips):
    # URL for Feeds
    azurepublic = "https://www.microsoft.com/en-us/download/confirmation.aspx?id=56519"
    msftpublic = "https://www.microsoft.com/en-us/download/confirmation.aspx?id=53602"
    officeworldwide = "https://endpoints.office.com/endpoints/worldwide?clientrequestid=b10c5ed1-bad1-445f-b386-b919946339a7"

    session = HTMLSession()
    azure_resp = session.get(azurepublic)
    links = azure_resp.html.links
    json_link = [link for link in links if ".json" in link]

    msft_resp = session.get(msftpublic)
    links = msft_resp.html.links
    csv_link = [link for link in links if ".csv" in link]

    # Download JSON link
    azure_json = requests.get(json_link[0])
    msft_csv = requests.get(csv_link[0], stream=True)
    o365_json = requests.get(officeworldwide, stream=True)
    # Write output file
    logging.info("Writing ServiceTags_Public.json file to output directory")
    with open(servicetags_public, "w") as f:
        json.dump(azure_json.json(), f, indent=4)
    logging.info("Writing MSFT_PublicIPs.csv file to output directory")
    with open(msftpublic_ips, "wb") as f:
        for line in msft_csv.iter_lines():
            f.write(line + "\n".encode())
    logging.info("Writing OfficeWorldWide-IPRanges.json file to output directory")
    with open(officeworldwide_ips, "w") as f:
        json.dump(o365_json.json(), f, indent=4)

def main():
    logging.basicConfig(
        stream=sys.stdout,
        level=logging.DEBUG,
        format="%(asctime)s:%(levelname)s: %(message)s",
    )

    curr_path = Path.cwd()
    out_path = curr_path / "master" / "PublicFeeds" / "MSFTIPRanges"
    try:
        out_path.mkdir(parents=True, exist_ok=False)
    except FileExistsError:
        logging.info("Folder is already present")
    else:
        logging.info(f"{out_path} Folder was created")

    servicetags_public = (
        curr_path
        / "master"
        / "PublicFeeds"
        / "MSFTIPRanges"
        / "ServiceTags_Public.json"
    )
    msftpublic_ips = (
        curr_path / "master" / "PublicFeeds" / "MSFTIPRanges" / "MSFT_PublicIPs.csv"
    )
    officeworldwide_ips = (
        curr_path
        / "master"
        / "PublicFeeds"
        / "MSFTIPRanges"
        / "OfficeWorldWide-IPRanges.json"
    )

    logging.info(f"Writing json file to output directory : {servicetags_public}")
    logging.info(f"Writing csv file to output directory : {msftpublic_ips}")
    logging.info(f"Writing json file to output directory : {officeworldwide_ips}")
    parse_and_download_files(servicetags_public, msftpublic_ips, officeworldwide_ips)

if __name__ == "__main__":
    main()


from heapq import heappush, heappop
n = int(input())
card = []
for _ in range(n):
    heappush(card,int(input()))
    # 들어오는 순서대로 정렬
print(card)
answer = 0
while card:
    print(heappop(card))
# while len(card) > 1:
#     a = heappop(card)
#     b = heappop(card)
#     heappush(card, a+b)
#     answer += a+b
print(answer)


import turtle
#Best practice is to place functions after an import

def square(len):
  for i in range(4):
    turtle.forward(len)
    turtle.left(90)

def rectangle(width, height):
  for i in range(2):
    turtle.forward(width)
    turtle.left(90)
    turtle.forward(height)
    turtle.left(90)

turtle.shape("turtle") # optional
turtle.speed(0) # optional
turtle.color("green")
turtle.pensize(3)

# square(90)

turtle.color("blue")
turtle.penup()
turtle.forward(100)
turtle.pendown()

#square(90)
# rectangle(90, 140)

# pritty pic

for i in range(19):
  square(90)
  turtle.right(19)
  rectangle(90, 140)

#screen to exit
turtle.Screen().exitonclick()


"""Bubble sort implementation: running time == O(n^2)"""
import copy

class BubbleSort:
    def __init__(self, items):
        self.items = items
        self.n = len(items)

    def run(self):
        A = copy.deepcopy(self.items)
        for i in range(self.n-1):
            for j in range(self.n-1, i, -1):
                if A[j] < A[j-1]:
                    temp = A[j]
                    A[j] = A[j-1]
                    A[j-1] = temp

        return A

if __name__=="__main__":
    num_list = [5, 2, 4, 6, 1, 3, 3, 8, 9, 11, 7, 0, 1, 4, 10, 12]
    bubble_sort = BubbleSort(items=num_list)
    print(bubble_sort.run())


import numpy as np
import matplotlib.pyplot as plt
# data I/O
filename = 'dataset.txt'
file = open(filename, 'r')
data = file.read()
# use set() to count the vacab size
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
print ('data has %d characters, %d unique.' % (data_size, vocab_size))

# dictionary to convert char to idx, idx to char
char_to_ix = { ch:i for i,ch in enumerate(chars) }
ix_to_char = { i:ch for i,ch in enumerate(chars) }

# hyperparameters
hidden_size = 50 # size of hidden layer of neurons
seq_length = 50 # number of steps to unroll the RNN for
learning_rate = 1e-1
Wxh = np.random.randn(hidden_size, vocab_size)*0.01 # input to hidden
Whh = np.random.randn(hidden_size, hidden_size)*0.01 # hidden to hidden
Why = np.random.randn(vocab_size, hidden_size)*0.01 # hidden to output
bh = np.zeros((hidden_size, 1)) # hidden bias
by = np.zeros((vocab_size, 1)) # output bias
def lossFun(inputs, targets, hprev):

  xs, hs, ys, ps = {}, {}, {}, {}
  ## record each hidden state of
  hs[-1] = np.copy(hprev)
  loss = 0
  # forward pass for each training data point
  for t in range(len(inputs)):
    xs[t] = np.zeros((vocab_size, 1)) # encode in 1-of-k representation
    xs[t][inputs[t]] = 1

    ## hidden state, using previous hidden state hs[t-1]
    hs[t] = np.tanh(np.dot(Wxh, xs[t]) + np.dot(Whh, hs[t-1]) + bh)
    ## unnormalized log probabilities for next chars
    ys[t] = np.dot(Why, hs[t]) + by
    ## probabilities for next chars, softmax
    ps[t] = np.exp(ys[t]) / np.sum(np.exp(ys[t]))
    ## softmax (cross-entropy loss)
    loss += -np.log(ps[t][targets[t], 0])

  # backward pass: compute gradients going backwards
  dWxh, dWhh, dWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)
  dbh, dby = np.zeros_like(bh), np.zeros_like(by)
  dhnext = np.zeros_like(hs[0])
  for t in reversed(range(len(inputs))):
    ## compute derivative of error w.r.t the output probabilites
    ## dE/dy[j] = y[j] - t[j]
    dy = np.copy(ps[t])
    dy[targets[t]] -= 1 # backprop into y

    ## output layer doesnot use activation function, so no need to compute the derivative of error with regard to the net input
    ## of output layer.
    ## then, we could directly compute the derivative of error with regard to the weight between hidden layer and output layer.
    ## dE/dy[j]*dy[j]/dWhy[j,k] = dE/dy[j] * h[k]
    dWhy += np.dot(dy, hs[t].T)
    dby += dy

    ## backprop into h
    ## derivative of error with regard to the output of hidden layer
    ## derivative of H, come from output layer y and also come from H(t+1), the next time H
    dh = np.dot(Why.T, dy) + dhnext
    ## backprop through tanh nonlinearity
    ## derivative of error with regard to the input of hidden layer
    ## dtanh(x)/dx = 1 - tanh(x) * tanh(x)
    dhraw = (1 - hs[t] * hs[t]) * dh
    dbh += dhraw

    ## derivative of the error with regard to the weight between input layer and hidden layer
    dWxh += np.dot(dhraw, xs[t].T)
    dWhh += np.dot(dhraw, hs[t-1].T)
    ## derivative of the error with regard to H(t+1)
    ## or derivative of the error of H(t-1) with regard to H(t)
    dhnext = np.dot(Whh.T, dhraw)

  for dparam in [dWxh, dWhh, dWhy, dbh, dby]:
    np.clip(dparam, -5, 5, out=dparam) # clip to mitigate exploding gradients

  return loss, dWxh, dWhh, dWhy, dbh, dby, hs[len(inputs)-1]

## given a hidden RNN state, and a input char id, predict the coming n chars
def sample(h, seed_ix, n):

  ## a one-hot vector
  x = np.zeros((vocab_size, 1))
  x[seed_ix] = 1

  ixes = []
  for t in range(n):
    ## self.h = np.tanh(np.dot(self.W_hh, self.h) + np.dot(self.W_xh, x))
    h = np.tanh(np.dot(Wxh, x) + np.dot(Whh, h) + bh)
    ## y = np.dot(self.W_hy, self.h)
    y = np.dot(Why, h) + by
    ## softmax
    p = np.exp(y) / np.sum(np.exp(y))
    ## sample according to probability distribution
    ix = np.random.choice(range(vocab_size), p=p.ravel())

    ## update input x
    ## use the new sampled result as last input, then predict next char again.
    x = np.zeros((vocab_size, 1))
    x[ix] = 1

    ixes.append(ix)

  return ixes

## iterator counter
n = 0
## data pointer
p = 0

mWxh, mWhh, mWhy = np.zeros_like(Wxh), np.zeros_like(Whh), np.zeros_like(Why)
mbh, mby = np.zeros_like(bh), np.zeros_like(by) # memory variables for Adagrad
smooth_loss = -np.log(1.0/vocab_size)*seq_length # loss at iteration 0
l=[]
sl=[]
## main loop
while True:
  # prepare inputs (we're sweeping from left to right in steps seq_length long)
  if p + seq_length + 1 >= len(data) or n == 0:
    # reset RNN memory
    ## hprev is the hiddden state of RNN
    hprev = np.zeros((hidden_size, 1))
    # go from start of data
    p = 0

  inputs = [char_to_ix[ch] for ch in data[p : p + seq_length]]
  targets = [char_to_ix[ch] for ch in data[p + 1 : p + seq_length + 1]]

  # sample from the model now and then
  if n % 1000 == 0:
    sample_ix = sample(hprev, inputs[0], 5000)
    txt = ''.join(ix_to_char[ix] for ix in sample_ix)
    print ('---- sample -----')
    print ('----\n %s \n----' % (txt, ))

  # forward seq_length characters through the net and fetch gradient
  loss, dWxh, dWhh, dWhy, dbh, dby, hprev = lossFun(inputs, targets, hprev)
  ## author using Adagrad(a kind of gradient descent)
  smooth_loss = smooth_loss * 0.999 + loss * 0.001
  if n % 1000 == 0:
    print ('iter %d, loss: %f' % (n, smooth_loss)) # print progress
    l.append(n)
    sl.append(smooth_loss)
  for param, dparam, mem in zip([Wxh, Whh, Why, bh, by],
                                [dWxh, dWhh, dWhy, dbh, dby],
                                [mWxh, mWhh, mWhy, mbh, mby]):
    mem += dparam * dparam
    param += -learning_rate * dparam / np.sqrt(mem + 1e-8)

  p += seq_length # move data pointer
  n += 1 # iteration counter

# gradient checking

from random import uniform
def gradCheck(inputs, target, hprev):
  global Wxh, Whh, Why, bh, by
  num_checks, delta = 10, 1e-5
  _, dWxh, dWhh, dWhy, dbh, dby, _ = lossFun(inputs, targets, hprev)
  for param,dparam,name in zip([Wxh, Whh, Why, bh, by], [dWxh, dWhh, dWhy, dbh, dby], ['Wxh', 'Whh', 'Why', 'bh', 'by']):
    s0 = dparam.shape
    s1 = param.shape
    if(s0 == s1):
        print('Error dims dont match: %s and %s.' % (s0, s1))
    print (name)
    for i in range(num_checks):
      ri = int(uniform(0,param.size))

      old_val = param.flat[ri]
      param.flat[ri] = old_val + delta
      cg0, _, _, _, _, _, _ = lossFun(inputs, targets, hprev)
      param.flat[ri] = old_val - delta
      cg1, _, _, _, _, _, _ = lossFun(inputs, targets, hprev)
      param.flat[ri] = old_val
      grad_analytic = dparam.flat[ri]
      grad_numerical = (cg0 - cg1) / ( 2 * delta )
      rel_error = abs(grad_analytic - grad_numerical) / abs(grad_numerical + grad_analytic)
      print ('%f, %f => %e ' % (grad_numerical, grad_analytic, rel_error))

gradCheck(inputs,targets,hprev)

plt.plot(loss,smoothloss,label='Epoch Loss')

plt.xlabel('Epochs')

plt.ylabel('Loss')

plt.title('EPOCH _LOSS PLOT')

plt.legend()

plt.show()


import torch
from torch import nn
import torch.nn.functional as F

from ssd.utils_ssd.box_utils import match, log_sum_exp

# evaluate conf_loss and loc_loss
class MultiBoxLoss(nn.Module):
    def __init__(self, cfg):
        super(MultiBoxLoss, self).__init__()
        self.num_classes = cfg.num_classes
        self.threshold = cfg.overlap_thresh
        self.negpos_ratio = cfg.neg_pos
        self.variance = cfg.variance

    def forward(self, preds, targets):
        loc_data, conf_data, priors = preds
        num = loc_data.size(0)
        num_priors = priors.size(0)
        # match priors (priors->nearest target)
        loc_t = torch.Tensor(num, num_priors, 4)
        conf_t = torch.LongTensor(num, num_priors)
        if loc_data.is_cuda:
            loc_t, conf_t = loc_t.cuda(), conf_t.cuda()
        for idx in range(num):
            truths = targets[idx][:, :-1]
            labels = targets[idx][:, -1]
            defaults = priors
            match(self.threshold, truths, defaults, self.variance, labels, loc_t, conf_t, idx)
        pos = conf_t > 0
        # location loss
        pos_idx = pos.unsqueeze(2).expand_as(loc_data)
        loc_p = loc_data[pos_idx].view(-1, 4)
        loc_t = loc_t[pos_idx].view(-1, 4)
        loss_l = F.smooth_l1_loss(loc_p, loc_t, size_average=False)

        # evaluate each priors's loss (the same as the paper)
        batch_conf = conf_data
        loss_c = (log_sum_exp(batch_conf) - batch_conf.gather(2, conf_t.unsqueeze(2))).squeeze(2)
        # hard negative mining: note: the batch size of each iteration is not the same
        # find the "max loss" background
        loss_c[pos] = 0  # filter out pos boxes
        _, loss_idx = loss_c.sort(1, descending=True)
        _, idx_rank = loss_idx.sort(1)
        num_pos = pos.long().sum(1, keepdim=True)
        num_neg = torch.clamp(self.negpos_ratio * num_pos, max=pos.size(1) - 1)  # size: [num, 1]
        neg = idx_rank < num_neg.expand_as(idx_rank)
        # confidence loss (pos:neg=1:3)
        pos_idx = pos.unsqueeze(2).expand_as(conf_data)
        neg_idx = neg.unsqueeze(2).expand_as(conf_data)
        conf_p = conf_data[(pos_idx + neg_idx).gt(0)].view(-1, self.num_classes)
        targets_weightd = conf_t[(pos + neg).gt(0)]
        loss_c = F.cross_entropy(conf_p, targets_weightd, size_average=False)

        return loss_l / num_pos.sum(), loss_c / num_pos.sum()


import os
import shutil
import subprocess
import time
import pylab
import imageio
import numpy as np
from tqdm import tqdm
from skimage.io import imread, imsave

def create_options(model, epoch):
    opts_test = {
        "loadSize": 512,
        "fineSize": 512,
        "how_many": 'all',
        "phase": 'test',
        "name": model,
        "checkpoints_dir": './checkpoints',
        "results_dir": '.temp_output',
        "which_epoch": str(epoch),
        "which_direction": 'AtoB',
        "resize_or_crop": '"scale_width"',
    }
    return opts_test

def create_bash_cmd_test(opts_test):
    """constructs bash command to run CycleGAN with the given settings"""
    cmd = []
    cmd.append("DATA_ROOT=.temp_input")
    for opt in opts_test.keys():
        cmd.append(opt + "=" + str(opts_test[opt]))
    cmd += ['th', 'test.lua']
    return(" ".join(cmd))

def check_correct_directory():
    """check if the script is being run from CycleGAN"""
    fpath = os.path.realpath(__file__)
    dirname = os.path.dirname(fpath).split('/')[-1]
    if not dirname == "CycleGAN":
        raise ValueError("Script should be run from CycleGAN base directory.")

def prep_directories():
    """ensures clean temporary directories for CycleGAN"""
    for dir_ in ['.temp_input', '.temp_output']:
        if os.path.exists(dir_):
            shutil.rmtree(dir_)
    for dir_ in ['testA', 'testB']:
        os.makedirs(os.path.join('.temp_input', dir_))
    os.makedirs(os.path.join('.temp_output'))

def grab_epochs(model):
    """
    given a model name or a folder path,
    returns an array of available epochs
    """
    if not os.path.isdir(model):
        model = os.path.join('checkpoints', model)
    assert os.path.isdir(model), model + " not a valid model"

    epochs = []
    for file in os.listdir(model):
        if file.split('.')[-1] == "t7":
            epochs.append(file.split('_')[0])
    epochs = [e for e in epochs if not e == 'latest']

    return list(set(epochs))

def test(img, opts):
    """
    performs a test inference on img, saves to a temp directory
    returns the stylized image
    """
    prep_directories()
    for dir_ in ['testA', 'testB']:
        imsave(os.path.join('.temp_input', dir_, 'img.png'), img)

    # run the bash command for test phase of CycleGAN
    cmd = create_bash_cmd_test(opts)

    start = time.time()
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
    process.wait()
    print("Stylizing complete. Time elapsed:", time.time() - start)

    # read image back into python
    path = os.path.join('.temp_output', opts['name'], str(opts['which_epoch']) + "_test", "images", "fake_B", "img.png")
    stylized_img = imread(path)
    return stylized_img

def stylize_image_all_epochs(img_path, output_dir, model):
    """
    processes an image with a model at all available epochs
    """
    imname = os.path.split(img_path)[1].split('.')[0]
    os.makedirs(output_dir, exist_ok=True)

    img = imread(img_path)
    available_epochs = grab_epochs(model)

    for epoch in tqdm(available_epochs):
        opts = create_options(model, epoch)
        stylized_img = test(img, opts)
        imsave(os.path.join(output_dir, imname + "-" + model + "-epoch-" + str(epoch)) + ".png", stylized_img)

def stylize_video(vid_path, out_path, model, epoch):
    """
    stylizes all frames of a video
    """
    video = imageio.get_reader(vid_path, 'mpeg')
    writer = imageio.get_writer(out_path, fps=30)
    opts = create_options(model, epoch)

    # TODO: don't hardcode 30fps downsampling
    for i, frame in enumerate(video):
        if i % 2 == 0:
            frame = test(np.array(frame), opts)
            writer.append_data(frame)
        if i % 10 == 0:
            print(i, "of", len(video), "frames done.")
        if i == len(video) - 10:  # TAKE THIS OUT AFTER DONE TESTING
            break
    writer.close()

def repeat_stylization(img_path, out_dir, n_iter, model, epoch):
    """
    Repeatedly applies a style to an image
    """
    fname = os.path.splitext(img_path)[0].split("/")[-1]
    img = imread(img_path)
    os.makedirs(out_dir, exist_ok=True)
    opts = create_options(model, epoch)

    for i in range(n_iter):
        img = test(img, opts)
        imsave(os.path.join(out_dir, fname + "-" + model + "-" + str(epoch) + "-iter" + i))

def stylize_dir_hacky(input_dir, output_dir):
    """
	this is horrible and last minute
    temporary function to perform stylization
    images: applies 3 available styles at 5 different epochs
    the bean - pop art at all epochs
    northwestern - cubism at all epochs
    video: applies 3 styles to each video
    """
    models = ['cubism_v2', 'impressionism', 'pop_art']
    epochs_img = [50, 100, 150, 200]
    files = os.listdir(input_dir)
    files = [f for f in files if not f[0] == "."]
    os.makedirs(output_dir)

    for file in files:
        filename = file.split(".")[0]
        output_subdir = os.path.join(output_dir, filename + "-stylized")
        os.makedirs(output_subdir, exist_ok=True)

        print("Stylizing", file, "\nSaving to", output_subdir)

        # Videos
        if ".mp4" in file:
            for model in models:
                print("Applying", model, "to", file, ", saving to")
                stylize_video(vid_path=os.path.join(input_dir, file),
                              out_path=os.path.join(output_subdir, file + '-' + model + '.mp4'),
                              model=model,
                              epoch=200)

        # Photos
        else:

            # Images, all epochs, all models
            if file in ['northwestern.jpeg', 'the_bean.jpeg']:
                output_subdir_all_epochs = os.path.join(output_dir, file.split(".")[0] + "-all-epochs")
                os.makedirs(output_subdir_all_epochs, exist_ok=True)
                for model in models:
                    print("Applying", model, "to", file, "all epochs", "\nSaving to", output_subdir_all_epochs)
                    # try:
                    stylize_image_all_epochs(img_path=os.path.join(input_dir, file),
                                             output_dir=output_subdir_all_epochs,
                                             model=model)
                    # except:
                    #     pass

            # Images, only certain styles
            for model in models:
                for epoch in epochs_img:
                    try:
                        img = imread(os.path.join(input_dir, file))
                        opts = create_options(model, epoch)
                        stylized_img = test(img, opts)
                        imsave(os.path.join(output_subdir, filename + "-" + model + "-epoch-" + epoch + ".png"), stylized_img)
                    except:
                        pass

def stylize_image_all_styles(img_path, models):
    pass

if __name__ == "__main__":
    stylize_dir_hacky("input_5-28-17", "output_5-28-17")


from __future__ import division, print_function, unicode_literals

import unittest

from math import pi
import numpy as np
from analysis import extract_z, extract_scattering

class SigmaTest(unittest.TestCase):
    def test_quad(self):
        T = 0.01
        w_n = pi * (2*np.arange(10)+1) * T
        ImSigma = w_n * (w_n - 10)
        z = extract_z(w_n, ImSigma)
        self.assertAlmostEqual(z, 1/11)
        gamma = extract_scattering(w_n, ImSigma)
        self.assertAlmostEqual(gamma, 0)

    def test_linear(self):
        T = 0.02
        w_n = pi * (2*np.arange(10)+1) * T
        ImSigma = -0.5 - w_n
        z = extract_z(w_n, ImSigma)
        self.assertAlmostEqual(z, 1/2)
        gamma = extract_scattering(w_n, ImSigma)
        self.assertAlmostEqual(gamma, 0.5)


def simplify_fraction(fraction):
    nominator = fraction[0]
    denominator = fraction[1]

    if denominator == 0:
        raise Exception("Division by zero is undefined")
    elif nominator == 0:
        return 0
    elif nominator > denominator:
        for index in range(2, denominator + 1):
            while (denominator % index == 0) and (nominator % index == 0):
                nominator /= index
                denominator /= index
    else:
        for index in range(2, nominator + 1):
            while (nominator % index == 0) and (denominator % index == 0):
                nominator /= index
                denominator /= index

    return (int(nominator), int(denominator))

def collect_fractions(fractions):
    for index in range(len(fractions)):
        if index == 0:
            nominator1 = fractions[index][0]
            denominator1 = fractions[index][1]
        elif denominator1 == 0:
            raise Exception("Division by zero is undefined")
        elif nominator1 == 0:
            return 0
        else:
            nominator2 = fractions[index][0]
            denominator2 = fractions[index][1]
            if denominator2 == 0:
                raise Exception("Division by zero is undefined")
            elif nominator2 == 0:
                return 0
            if denominator1 == denominator2:
                nominator1 += nominator2
            else:
                current_denominator1 = denominator1
                current_denominator2 = denominator2
                denominator1 *= denominator2
                nominator1 *= current_denominator2
                nominator2 *= current_denominator1
                nominator1 += nominator2
    return simplify_fraction((nominator1, denominator1))

def sort_fractions(fractions):
    result = []
    my_dict = {}
    for index in range(len(fractions)):
        nominator = fractions[index][0]
        denominator = fractions[index][1]
        my_dict.update({(nominator, denominator): nominator / denominator})
    sorted_by_value = sorted(my_dict.items(), key=lambda kv: kv[1])
    for sorted_index in sorted_by_value:
        result.append(sorted_index[0])
    return result


#!/usr/bin/env python3

import fnmatch
import os
import re
import ntpath
import sys
import argparse

def get_private_declare(content):
    priv_declared = []

    srch = re.compile('private.*')
    priv_srch_declared = srch.findall(content)
    priv_srch_declared = sorted(set(priv_srch_declared))

    priv_dec_str = ''.join(priv_srch_declared)

    srch = re.compile('(?<![_a-zA-Z0-9])(_[a-zA-Z0-9]*?)[ ,\}\]\)";]')
    priv_split = srch.findall(priv_dec_str)
    priv_split = sorted(set(priv_split))
    priv_declared += priv_split;

    srch = re.compile('params \[.*\]|PARAMS_[0-9].*|EXPLODE_[0-9]_PVT.*|DEFAULT_PARAM.*|KEY_PARAM.*|IGNORE_PRIVATE_WARNING.*')
    priv_srch_declared = srch.findall(content)
    priv_srch_declared = sorted(set(priv_srch_declared))

    priv_dec_str = ''.join(priv_srch_declared)

    srch = re.compile('(?<![_a-zA-Z0-9])(_[a-zA-Z0-9]*?)[ ,\}\]\)";]')
    priv_split = srch.findall(priv_dec_str)
    priv_split = sorted(set(priv_split))

    priv_declared += priv_split;

    srch = re.compile('(?i)[\s]*local[\s]+(_[\w\d]*)[\s]*=.*')
    priv_local = srch.findall(content)
    priv_local_declared = sorted(set(priv_local))

    priv_declared += priv_local_declared;

    return priv_declared

def check_privates(filepath):
    bad_count_file = 0
    def pushClosing(t):
        closingStack.append(closing.expr)
        closing << Literal( closingFor[t[0]] )

    def popClosing():
        closing << closingStack.pop()

    with open(filepath, 'r') as file:
        content = file.read()

        priv_use = []
        priv_use = []

        # Regex search privates
        srch = re.compile('(?<![_a-zA-Z0-9])(_[a-zA-Z0-9]*?)[ =,\^\-\+\/\*\%\}\]\)";]')
        priv_use = srch.findall(content)
        priv_use = sorted(set(priv_use))

        # Private declaration search
        priv_declared = get_private_declare(content)

        if '_this' in priv_declared: priv_declared.remove('_this')
        if '_this' in priv_use: priv_use.remove('_this')

        if '_x' in priv_declared: priv_declared.remove('_x')
        if '_x' in priv_use: priv_use.remove('_x')

        if '_forEachIndex' in priv_declared: priv_declared.remove('_forEachIndex')
        if '_forEachIndex' in priv_use: priv_use.remove('_forEachIndex')
        if '_foreachIndex' in priv_declared: priv_declared.remove('_foreachIndex')
        if '_foreachIndex' in priv_use: priv_use.remove('_foreachIndex')
        if '_foreachindex' in priv_declared: priv_declared.remove('_foreachindex')
        if '_foreachindex' in priv_use: priv_use.remove('_foreachindex')

        missing = []
        for s in priv_use:
            if s.lower() not in map(str.lower,priv_declared):
                if s.lower() not in map(str.lower,missing):
                    missing.append(s)

        if len(missing) > 0:
            print (filepath)

            private_output = 'private[';
            first = True
            for bad_priv in missing:
                if first:
                    first = False
                    private_output = private_output + '"' + bad_priv
                else:
                    private_output = private_output + '", "' + bad_priv

            private_output = private_output + '"];';
            print (private_output)

            for bad_priv in missing:
                print ('\t' + bad_priv)
                bad_count_file = bad_count_file + 1

    return bad_count_file

def main():

    print("#########################")
    print("# Search your Privates  #")
    print("#########################")

    sqf_list = []
    bad_count = 0

    parser = argparse.ArgumentParser()
    parser.add_argument('-m','--module', help='only search specified module addon folder', required=False, default=".")
    args = parser.parse_args()

    for root, dirnames, filenames in os.walk('../addons' + '/' + args.module):
      for filename in fnmatch.filter(filenames, '*.sqf'):
        sqf_list.append(os.path.join(root, filename))

    for filename in sqf_list:
        bad_count = bad_count + check_privates(filename)

    print ("Bad Count {0}".format(bad_count))

if __name__ == "__main__":
    main()


def get_customer(session, w_id, d_id, c_id):
    prepared = session.prepare(
        "SELECT c_first, c_middle, c_last FROM customer \
        WHERE c_w_id = ? AND c_d_id = ? and c_id = ?"
    )
    rows = session.execute(prepared.bind((w_id, d_id, c_id)))
    return None if not rows else rows[0]

def get_last_order(session, w_id, d_id, c_id):
    prepared = session.prepare(
        "SELECT o_id, o_entry_d, o_carrier_id FROM order_by_customer \
        WHERE o_w_id = ? AND o_d_id = ? AND o_c_id = ? LIMIT 1"
    )
    rows = session.execute(prepared.bind((w_id, d_id, c_id)))
    return None if not rows else rows[0]

def get_order_items(session, w_id, d_id, o_id):
    prepared = session.prepare(
        "SELECT ol_i_id, ol_supply_w_id, ol_quantity, ol_amount, ol_delivery_d \
        FROM order_line \
        WHERE ol_w_id = ? AND ol_d_id = ? AND ol_o_id = ?"
    )
    rows = session.execute(prepared.bind((w_id, d_id, int(o_id))))
    return None if not rows else rows

def order_status_xact(session, w_id, d_id, c_id):
    payload = {}

    payload['customer'] = get_customer(session, w_id, d_id, c_id)
    order = get_last_order(session, w_id, d_id, c_id)
    payload['order'] = order
    payload['items'] = None
    if order:
        payload['items'] = get_order_items(session, w_id, d_id, order.o_id)

    return payload


import threading
import tkinter as tk

from motor import Motor

motor = Motor()

thread_motor = threading.Thread(target=motor.inicia_motor, args=(True,))
thread_motor.daemon = True
thread_motor.start()

def update_value():
    # Função para atualizar o valor
    # Aqui você pode implementar a lógica desejada para atualizar o valor

    # Exemplo: Atualizar o valor somando 1
    global value
    value = motor.rotacoes
    value_label.config(text=str(f'{value:0.2f} RPM'))

    # Chama a função novamente após 100ms
    value_label.after(70, update_value)

def slider_callback(value):
    # Função de callback do slider
    # Exibe o valor selecionado pelo slider
    motor.amperes = int(value)

# Cria a janela principal
window = tk.Tk()
window.title("Atualização de Valor")

# Valor inicial
value = 0

# Cria o rótulo para exibir o valor
value_label = tk.Label(window, text=str(value), font=("Arial", 24), height=5, width=20)
value_label.pack(pady=20)

# Slider
slider = tk.Scale(window, from_=motor.amperes, to=100, orient=tk.HORIZONTAL, command=slider_callback)
slider.pack(pady=10)

# Botão "Desligar"
desligar_button = tk.Button(window, text="Desligar", command=motor.desliga_motor)
desligar_button.pack(pady=10)

# Chama a função de atualização inicialmente
update_value()

# Inicia o loop principal da janela
window.mainloop()


import os
from random import shuffle
import tensorflow as tf
import glob
from config import config

# all functions except init and create_iterators should be empty
class Preprocessing:
    def __init__(self):
        print('preprocessing instance creation started')

        self.dir_name = config['data_dir']
        self.input_len = config['input_len']
        #maybe add later noise augmentation

    def create_iterators(self):
        # get the filenames split into train test validation
        test_files = self.get_files_from_txt('testing_list.txt')
        val_files = self.get_files_from_txt('validation_list.txt')
        filenames = glob.glob(os.path.join(self.dir_name, '*/**.wav'), recursive=True)
        filenames = [filename for filename in filenames if 'background_noise' not in filename]
        train_files = list(set(filenames) - set(val_files) - set(test_files))
        shuffle(train_files)
        # get the commands and some prints
        self.commands = self.get_commands()
        self.num_classes = len(self.commands)
        print('len(train_data)', len(train_files))
        print('prelen(test_data)', len(test_files))
        print('len(val_data)', len(val_files))
        print('commands: ', self.commands)
        print('number of commands: ', len(self.commands))

        # make tf dataset object
        train_dataset = self.make_tf_dataset_from_list(train_files)
        val_dataset = self.make_tf_dataset_from_list(val_files, is_validation = True)
        test_dataset = self.make_tf_dataset_from_list(test_files)
        return train_dataset, val_dataset, test_dataset

    def get_files_from_txt(self, which_txt):
        assert which_txt == 'testing_list.txt' or which_txt == 'validation_list.txt', 'wrong argument'
        path = os.path.join(self.dir_name, which_txt)
        with open(path) as f:
            paths = f.readlines()
        paths = [os.path.join(self.dir_name, path[:len(path) - 1]) for path in paths]
        shuffle(paths)
        return paths

    def get_commands(self):
        dirs = glob.glob(os.path.join(self.dir_name, "*", ""))
        commands = [os.path.split(os.path.split(dir)[0])[1] for dir in dirs if 'background' not in dir]
        return commands

    @staticmethod
    def decode_audio(audio_binary):
        audio, _ = tf.audio.decode_wav(audio_binary)
        return tf.squeeze(audio, axis=-1)

    def get_label(self, file_path):
        parts = tf.strings.split(file_path, os.path.sep)
        label = parts[-2]
        label_id = tf.argmax(label == self.commands)
        label = tf.one_hot(label_id, self.num_classes)
        return label

    def make_tf_dataset_from_list(self, filenames_list, is_validation = False):
        files = tf.data.Dataset.from_tensor_slices(filenames_list)
        dataset = files.map(self.get_waveform_and_label, num_parallel_calls=tf.data.AUTOTUNE)
        dataset = dataset.map(self.pad_map_func, num_parallel_calls=tf.data.AUTOTUNE)
        dataset = dataset.shuffle(buffer_size=5000, reshuffle_each_iteration=True)
        if is_validation:
            dataset = dataset.repeat()
        dataset = dataset.batch(config['train_params']['batch_size']).prefetch(tf.data.AUTOTUNE)
        return dataset

    def get_waveform_and_label(self, file_path):
        label = self.get_label(file_path)
        audio_binary = tf.io.read_file(file_path)
        waveform = self.decode_audio(audio_binary)
        return waveform, label

    def pad_map_func(self, audio, label):
        return [self.add_paddings(audio), label]

    def add_paddings(self, wav):
        len_wav = len(wav)
        if len_wav < self.input_len:
            paddings = tf.zeros((self.input_len - len_wav))
            wav = tf.concat([wav, paddings], axis=0)
        return wav


from transformers import (
    BlenderbotSmallForConditionalGeneration,
    BlenderbotSmallTokenizer,
    BlenderbotForConditionalGeneration,
    BlenderbotTokenizer,
)
from transformers import GPT2LMHeadModel, GPT2Tokenizer
from transformers import AutoTokenizer, AutoModelForCausalLM
import sys

download_type = sys.argv[1]

if download_type == 'dialogpt':
    #------dialogpt samll------#
    model = GPT2LMHeadModel.from_pretrained("microsoft/DialoGPT-small")
    tokenizer = GPT2Tokenizer.from_pretrained("microsoft/DialoGPT-small")
    #------dialogpt medium------#
    model = GPT2LMHeadModel.from_pretrained("microsoft/DialoGPT-medium")
    tokenizer = GPT2Tokenizer.from_pretrained("microsoft/DialoGPT-medium")

    print("dialogpt is done!")

elif download_type == 'gptneo':
    #------gptneo small------#
    model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M")
    tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
    #------gptneo large------#
    #model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B")
    #tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B")

    print("gptneo is done!")

elif  download_type == 'blender':
    #------blender small------#
    model = BlenderbotSmallForConditionalGeneration.from_pretrained("facebook/blenderbot_small-90M")
    tokenizer = BlenderbotSmallTokenizer.from_pretrained("facebook/blenderbot_small-90M")
    #------blender medium------#
    model = BlenderbotForConditionalGeneration.from_pretrained("facebook/blenderbot-400M-distill")
    tokenizer = BlenderbotTokenizer.from_pretrained("facebook/blenderbot-400M-distill")

    print("blender is done!")


import discord
import requests

client = discord.Client()

tokenFile = open('secret.secret','r')
token = tokenFile.readline()

@client.event
async def on_message(msg):
    if msg.content.startswith('$$$$'):
        name = msg.content[4::]
        apiCall = 'https://na.whatismymmr.com/api/v1/summoner?name=' + name
        response = requests.get(apiCall)
        if(response.status_code == 200):
            data = response.json()
            Ammr = data['ARAM']['avg']
            Apct = data['ARAM']['percentile']
            Arank = data['ARAM']['closestRank']
            builtMsg = f'{name}\'s ARAM MMR: {Ammr}\n{Apct}th percentile, about {Arank}'
            await msg.channel.send(builtMsg)
        else:
            await msg.channel.send('RIP LMAO.')
##test push
client.run(token)


import csv
with open ("election_results.txt", "w+") as output:
    with open("election_data.csv" , "r") as csvfile:
        readCSV = csv.reader(csvfile)
        data = list(readCSV)
        row_count = len(data)

        print("Election Results:")
        print("-------------------------")
        print("Total Votes: " + str(row_count))
        print("-------------------------")

        cand = []
        for row in data:
            cand.append(row[2])

        Khan_count = cand.count("Khan")
        Khan_percent = (Khan_count / row_count)
        Khan_format = format(Khan_percent, ".3%")
        print("Khan: " + str(Khan_format) + " " +
        "(" + str(Khan_count) + ")")

        Correy_count = cand.count("Correy")
        Correy_percent = (Correy_count / row_count)
        Correy_format = format(Correy_percent, ".3%")
        print("Correy: " + str(Correy_format) + " " +
        "(" + str(Correy_count) + ")")

        Li_count = cand.count("Li")
        Li_percent = (Li_count / row_count)
        Li_format = format(Li_percent, ".3%")
        print("Li: " + str(Li_format) + " " +
        "(" + str(Li_count) + ")")

        O_Tooley_count = cand.count("O'Tooley")
        O_Tooley_percent = (O_Tooley_count / row_count)
        O_Tooley_format = format(O_Tooley_percent, ".3%")
        print("O'Tooley: " + str(O_Tooley_format) + " " +
        "(" + str(O_Tooley_count) + ")")

        print("-------------------------")
        print("Winner: Khan")
        print("-------------------------")

        output.write("""Election Results:
-------------------------
Total Votes: 20000
-------------------------
Khan: 62.810% (12562)
Correy: 19.940% (3988)
Li: 14.285% (2857)
O'Tooley: 2.960% (592)
-------------------------
Winner: Khan
-------------------------
Election Results:""")


import openpyxl

def get_exl(file,Sheet):
    exl = openpyxl.load_workbook(file)
    table = exl[Sheet]
    max_rows = table.max_row
    max_column = table.max_column
    # print(max_rows,max_column)
    data = []
    for row in range(1, max_rows):
        rowdata = []
        for column in range(3, max_column-1):
            rowdata.append(table.cell(row+1, column+1).value)
        data.append(rowdata)
    return data

if __name__ == '__main__':
    run = get_exl('../TestData/data/testdata.xlsx','查询终端设备')
    print(run)


"""
Exercises of the book "Think python"
2.10 Exercises
"""
import math
import datetime
import decimal

# Exercise 2
# Using the Python as a calculator

# 1. The volume of a sphere with radius r is 4/3 π r3. What is the volume of a sphere with radius 5?
radius = 5

print("The volume of a sphere: ", (4 / 3 * math.pi * radius**3))

# 2. Suppose the cover price of a book is $24.95, but bookstores get a 40% discount.
#   Shipping costs $3 for the first copy and 75 cents for each additional copy.
#   What is the total wholesale cost for 60 copies?
cover_price = decimal.Decimal('24.95')
discount = decimal.Decimal('0.4')
copies_amount = decimal.Decimal(60)

s_cost_first = decimal.Decimal(3)  # Shipping_cost for the first copy
s_cost_add = decimal.Decimal('0.75')  # Shipping_cost for each additional copy

# Total wholesale cost
sum = (
    (cover_price * (1 - discount)) * copies_amount
    + (s_cost_add * (copies_amount - 1))
    + s_cost_first
)
print("Total wholesale cost for 60 copies: ", round(sum, 2))

# 3. If I leave my house at 6:52 am and run 1 mile at an easy pace (8:15 per mile),
#   then 3 miles at tempo (7:12 per mile) and 1 mile at easy pace again,
#   what time do I get home for breakfast?

# Speed (minutes)
easy_pace = "08:15"
tempo = "07:12"

def time_to_seconds(time: str):
    """Function convert time from str format('07:12', MM:SS) to amount of seconds."""
    return (int(time[: time.find(":")]) * 60) + (int(time[time.find(":") + 1 :]))

# Convert time to seconds
easy_pace = time_to_seconds(easy_pace)
tempo = time_to_seconds(tempo)

# Start time of the run
start_time = datetime.datetime.strptime("6:52", "%H:%M")

# Calculate duration of the run in seconds (miles*time per mile)
whole_time = 2 * easy_pace + 3 * tempo

# Calculate time of the end of the run
home_time = (start_time + datetime.timedelta(seconds=whole_time)).time()

print("You will get home at: ", home_time)


from __future__ import (
    absolute_import,
    unicode_literals,
)
from application.utils.utils import (
    get_near_positions,
    remove_occupied_position,
    pick_random
)

from application.entity.point import Point
from application.entity.enemy_ship import EnemyShip

class EnemyBoard(object):
    def __init__(self, width, height, ships):
        self.width = width
        self.height = height
        self.valid_positions = []
        self.remain_positions = []
        self.fired_positions = []
        self.hit_positions = []
        self.remain_ship = []
        for x in range(width):
            for y in range(height):
                if (x + y) % 2 == 0:
                    self.valid_positions.append(Point(x, y))
                    self.remain_positions.append(Point(x, y))

        self.init_ships(ships)

    def init_ships(self, ships):
        for ship_data in ships:
            for index in range(int(ship_data['quantity'])):
                ship = EnemyShip(ship_data['type'])
                self.remain_ship.append(ship)

    def fire(self):
        fire_point = self.get_high_expect_positions()
        self.remain_positions.remove(fire_point)
        self.fired_positions.append(fire_point)
        return fire_point

    def get_high_expect_positions(self):
        high_expect_positions = []
        if self.hit_positions:
            for position in self.hit_positions:
                near_positions = get_near_positions(position)
                high_expect_positions = remove_occupied_position(near_positions, self.fired_positions)
                if high_expect_positions:
                    break
        else:
            return self.fire_random()
        if not high_expect_positions:
            return self.fire_random()
        return pick_random(high_expect_positions)

    def fire_random(self):
        fire_point = pick_random(self.remain_positions)
        self.remain_positions.remove(fire_point)
        self.fired_positions.append(fire_point)
        return fire_point


import pytest
from collections import Counter
from ottoscript.base import OttoBase, OttoContext
from ottoscript.datatypes import (Number,
                                  String,
                                  Var,
                                  Entity,
                                  List,
                                  Dict,
                                  Target,
                                  Area,
                                  Input)

@pytest.mark.asyncio
async def test_numeric():
    """Verify we correctly parse a number"""

    n = Number().parseString("15")[0]
    output = await n.eval()
    assert output == 15

@pytest.mark.asyncio
async def test_string():
    """Verify we correctly parse a string"""

    n = String().parseString("'foo'")[0]
    output = await n.eval()
    assert output == 'foo'

@pytest.mark.asyncio
async def test_var_no_fetch():
    """Verify we correctly parse a var"""

    n = Var().parseString("@foo")[0]
    assert n.name == '@foo'

@pytest.mark.asyncio
async def test_var_with_attributes():
    """Verify we correctly parse a var with attributes"""

    ctx = OttoContext()
    d_string = "(one=1, two=2)"
    e_string = "ship.crew"
    ctx.update_vars({'@foo_entity': Entity().parseString(e_string)[0]})
    ctx.update_vars({'@foo_dict': Dict().parseString(d_string)[0]})
    OttoBase.set_context(ctx)

    n = Var().parseString("@foo_entity:name")[0]
    r = await n.eval()
    assert r == "ship.crew"

    n = Var().parseString("@foo_entity:brightness")[0]
    r = await n.eval()
    assert r == 1

    r = n.fetch()
    assert r.name == "ship.crew.brightness"

    n = Var().parseString("@foo_entity:number")[0]
    r = await n.eval()
    assert r == 1

    n = Var().parseString("@foo_dict:one")[0]
    r = await n.eval()
    assert r == 1

    n = Var().parseString("@foo_dict:two")[0]
    r = await n.eval()
    assert r == 2

@pytest.mark.asyncio
async def test_entity():
    """Verify we correctly parse an entity"""
    test_list = [('ship.crew', 'ship.crew', 'ship.crew'),
                 ('ship.crew:uniform', 'ship.crew.uniform', 1)
                 ]

    for test in test_list:
        n = Entity().parseString(test[0])[0]
        assert n.name == test[1]
        assert await n.eval() == test[2]

@pytest.mark.asyncio
async def test_list():
    """Verify we correctly parse a list"""

    ctx = OttoContext()
    ctx.update_vars({'@foo': 'foostring'})
    OttoBase.set_context(ctx)

    string = "'test1', 27, ship.crew, @foo"
    expected = [String().parseString('"test1"')[0],
                Number().parseString('27')[0],
                Entity().parseString('ship.crew')[0],
                Var().parseString('@foo')[0]]

    n1 = List().parseString(string)[0]

    assert Counter([type(x) for x in n1.contents]) \
        == Counter([type(x) for x in expected])

    n2 = List().parseString(f"({string})")[0]
    assert Counter([type(x) for x in n2.contents]) \
        == Counter([type(x) for x in expected])

@pytest.mark.asyncio
async def test_list_single():
    """Verify we correctly parse a number"""

    string = "ship.crew"
    expected = list

    n1 = List().parseString(string)[0]

    assert type(n1.contents) == expected

@pytest.mark.asyncio
async def test_dictionary():
    """Verify we correctly parse a dictionary"""

    ctx = OttoContext()
    ctx.update_vars({'@foo': 'foostring'})
    OttoBase.set_context(ctx)

    string = "(first = 1, second = 'foo', third = ship.crew, fourth = @foo)"
    expected = {'first': 1,
                'second': 'foo',
                "third": 'ship.crew',
                "fourth": 'foostring'}

    n1 = Dict().parseString(string)[0]

    result = await n1.eval()
    assert result == expected

@pytest.mark.asyncio
async def test_target():

    ctx = OttoContext()
    area = Area().parseString('kitchen')[0]
    ctx.update_vars({'@area': area})

    arealist = List(Area()).parseString('kitchen, living_room')[0]
    ctx.update_vars({'@arealist': arealist})

    OttoBase.set_context(ctx)

    tests = [('ship.crew, ship.phasers',
              {'entity_id': ['ship.crew', 'ship.phasers'], 'area_id': []}
              ),

             ('AREA kitchen, living_room',
              {'area_id': ['kitchen', 'living_room'], 'entity_id': []}
              ),

             ('AREA @area',
              {'area_id': ['kitchen'], 'entity_id': []}
              ),

             ('AREA @arealist',
              {'area_id': ['kitchen', 'living_room'], 'entity_id': []}
              )
             ]

    for test in tests:
        n = Target().parseString(test[0])[0]
        result = await n.eval()
        assert result == test[1]

@pytest.mark.asyncio
async def test_input():

    ctx = OttoContext()
    ctx.update_vars({'@foostring': String().parseString("'foostring'")[0],
                     '@foonumber': Number().parseString("30.0")[0]})

    OttoBase.set_context(ctx)

    tests = [{"type": "text",
              "string": "'foostring'",
              "expected": "foostring"},

             {"type": "text",
              "string": "@foostring",
              "expected": "foostring"},

             {"type": "text",
              "string": "foo.string",
              "expected": "foo.string"},

             {"type": "numeric",
              "string": "15",
              "expected": 15.0},

             {"type": "numeric",
             "string": "@foonumber",
              "expected": 30.0},

             {"type": "numeric",
             "string": "foo.number:attr",
              "expected": 1.0},

             {"type": "any",
             "string": "'foostring'",
                      "expected": "foostring"},

             {"type": "any",
              "string": "@foostring",
              "expected": "foostring"},

             {"type": "any",
              "string": "foo.string",
              "expected": "foo.string"},

             {"type": "any",
              "string": "15",
              "expected": 15.0},

             {"type": "any",
              "string": "@foonumber",
              "expected": 30.0}

             ]

    for test in tests:
        n = Input(test["type"]).parseString(test["string"])[0]
        print(test["string"])
        result = await n.eval()
        assert result == test["expected"]


from rest_framework.serializers import CharField, ModelSerializer

from .models.base import CanadianCommonCv
from .models.employment import AcademicWorkExperience, Employment
from .models.personal_information import Identification, Email, Website
from .models.recognitions import AreaOfResearch
from .models.user_profile import UserProfile

class AreaOfResearchSerializer(ModelSerializer):
    class Meta:
        model = AreaOfResearch
        fields = [
            'area',
            'sector',
            'field'
        ]

class WebsiteSerializer(ModelSerializer):
    class Meta:
        model = Website
        fields = ['url']

class EmailSerializer(ModelSerializer):
    class Meta:
        model = Email
        fields = ['address']

class IdentificationSerializer(ModelSerializer):
    email = EmailSerializer(many=True, read_only=True, source='email_set')
    website = WebsiteSerializer(many=True, read_only=True, source="website_set")

    class Meta:
        model = Identification
        fields = [
            'email',
            'title',
            'website',
            'family_name',
            'first_name',
            'middle_name',
            'previous_family_name',
            'previous_first_name'
        ]

class AcademicWorkExperienceSerializer(ModelSerializer):
    class Meta:
        model = AcademicWorkExperience
        fields = [
            'department',
            'position_title'
        ]

class EmploymentSerializer(ModelSerializer):
    academic_work_experience = AcademicWorkExperienceSerializer(many=True, read_only=True)

    class Meta:
        model = Employment
        fields = ['academic_work_experience']

class UserProfileSerializer(ModelSerializer):
    research_description = CharField(source='research_interest', read_only=True)
    research_interests = AreaOfResearchSerializer(many=True, read_only=True, source='user_aor')

    class Meta:
        model = UserProfile
        fields = [
            'research_description',
            'research_interests'
        ]

class CanadianCommonCvSerializer(ModelSerializer):
    identification = IdentificationSerializer(read_only=True)
    employment = EmploymentSerializer(read_only=True)
    user_profile = UserProfileSerializer(read_only=True)

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['research_description'] = ret['user_profile']['research_description']
        ret['research_interests'] = ret['user_profile']['research_interests']
        ret.pop('user_profile')
        return ret

    class Meta:
        model = CanadianCommonCv
        fields = [
            'identification',
            'employment',
            'user_profile'
        ]


import wx
import sys
from datetime import date
import webbrowser

from pbalance_c import pbalance_c as balance_c
from pincome_c import pincome_c as income_c
from pportfolio_c import pportfolio_c as portfolio_c
from padddata_c import padddata_c as adddata_c
from praw_c import praw_c as raw_c
from puseradd_c import puseradd_c as useradd_c
from plugin_c import plugin_c

from datautils_c import datautils_c
from datacoll_c import datacoll_c

class gui_c(wx.Frame):
	def __init__(self,parent,program,version,about,owner):
		wx.Frame.__init__(self,parent,-1,program+' '+version,size=(900,650))
		self.CreateStatusBar()
		self.SetMinSize((640,480))
		self.pinfo = program
		self.vinfo = version
		self.ainfo = about
		self.owner = owner
		self.projectopen = False
		self.frametitle = program+' '+version
		# CALLBACK
		owner.registerupdcallback(self.updatecallback)
		# PLUGINS
		#self.plugins = [balance_c,income_c,useradd_c,addquotes_c,raw_c,adddata_c]
		self.plugins = [balance_c,income_c,portfolio_c,useradd_c,adddata_c,raw_c]
		self.datacoll = self.owner.datacoll
		# COLOURS
		#self.palette = DEF_COLORS
		#self.palette1 = DEF_COLORS_PROFIT
		# MENU BAR
		self.menubar = wx.MenuBar()
		# FILE
		self.menufile = wx.Menu()
		item = self.menufile.Append(gui_c.ID_NEWPROJ,'New project...','Create a new project')
		item = self.menufile.Append(gui_c.ID_OPEPROJ,'Open project...','Open an existing project')
		item = self.menufile.Append(gui_c.ID_SAVPROJ,'Save project','Save current project')
		self.menufile.AppendSeparator()
		item = self.menufile.Append(gui_c.ID_EXIT,'Exit','Exit program')
		#self.menufile.Enable(gui_c.ID_SAVPROJ,False)
		self.menubar.Append(self.menufile,'File')
		# VIEW
		self.menuview = wx.Menu()
		item = self.menuview.Append(gui_c.ID_DETAILS,'Details','Show a detailed textual view',kind=wx.ITEM_RADIO)
		item = self.menuview.Append(gui_c.ID_HISTORY,'Monthly history','Show a monthly historical textual view',kind=wx.ITEM_RADIO)
		item = self.menuview.Append(gui_c.ID_HISTORYY,'Yearly history','Show a yearly historical textual view',kind=wx.ITEM_RADIO)
		item = self.menuview.Append(gui_c.ID_GDETAILS,'Detailed plot','Show a detailed graphical view',kind=wx.ITEM_RADIO)
		item = self.menuview.Append(gui_c.ID_GHISTORY,'Monthly graph','Show a monthly historical graphical view',kind=wx.ITEM_RADIO)
		item = self.menuview.Append(gui_c.ID_GHISTORYY,'Yearly graph','Show a yearly historical graphical view',kind=wx.ITEM_RADIO)
		self.menubar.Append(self.menuview,'View')
		# PROJECT
		self.menuproj = wx.Menu()
		item = self.menuproj.Append(gui_c.ID_UPDPROJ,'Download online quotes','Update stock quotes and currency rates online')
		item = self.menuproj.Append(gui_c.ID_CALPROJ,'Re-calculate result','Re-calculate all matrices and data')
		#self.menuproj.Enable(gui_c.ID_UPDPROJ,False)
		#self.menuproj.Enable(gui_c.ID_CALPROJ,False)
		self.menubar.Append(self.menuproj,'Project')
		# HELP
		self.menuabout = wx.Menu()
		item = self.menuabout.Append(gui_c.ID_UPDATE,'Search for updates...','Online update search')
		item = self.menuabout.Append(gui_c.ID_USERGU,'User\'s Guide... (web)','Frugal\'s User\'s Guide')
		item = self.menuabout.Append(gui_c.ID_ABOUT,'About','About %s'%(self.pinfo))
		self.menubar.Append(self.menuabout,'About')
		# UPDATE
		#self.menuupdate = wx.Menu()
		#item = self.menuupdate.Append(gui_c.ID_UPDATE,'Show available update','Show available updates')
		#self.menubar.Append(self.menuupdate,'Update')
		# ADD MENU AND EVENTS
		self.SetMenuBar(self.menubar)
		#self.Bind(wx.EVT_MENU, self.OnExit, id=wx.ID_EXIT)
		#wx.EVT_MENU(self,gui_c.ID_NEWPROJ,self.new)
		self.Bind(wx.EVT_MENU, self.new, id=gui_c.ID_NEWPROJ)
		#wx.EVT_MENU(self,gui_c.ID_OPEPROJ,self.open)
		self.Bind(wx.EVT_MENU, self.open, id=gui_c.ID_OPEPROJ)
		#wx.EVT_MENU(self,gui_c.ID_SAVPROJ,self.save)
		self.Bind(wx.EVT_MENU, self.save, id=gui_c.ID_SAVPROJ)
		#wx.EVT_MENU(self,gui_c.ID_EXIT,self.quit)
		self.Bind(wx.EVT_MENU, self.quit, id=gui_c.ID_EXIT)
		#wx.EVT_MENU(self,gui_c.ID_UPDPROJ,self.updateproject)
		self.Bind(wx.EVT_MENU, self.updateproject, id=gui_c.ID_UPDPROJ)
		#wx.EVT_MENU(self,gui_c.ID_CALPROJ,self.calc)
		self.Bind(wx.EVT_MENU, self.calc, id=gui_c.ID_CALPROJ)
		#wx.EVT_MENU(self,gui_c.ID_USERGU,self.usersguide)
		self.Bind(wx.EVT_MENU, self.usersguide, id=gui_c.ID_USERGU)
		#wx.EVT_MENU(self,gui_c.ID_ABOUT,self.about)
		self.Bind(wx.EVT_MENU, self.about, id=gui_c.ID_ABOUT)
		#wx.EVT_MENU(self,gui_c.ID_UPDATE,self.showupdate)
		self.Bind(wx.EVT_MENU, self.showupdate, id=gui_c.ID_UPDATE)
		self.Bind(wx.EVT_CLOSE,self.closeprogram)
		# SPLITTER
		self.splitv = wx.SplitterWindow(self)
		# TREE
		self.tree = wx.TreeCtrl(self.splitv,-1)
		self.troot = self.tree.AddRoot('Frugal')
		self.tree.Expand(self.troot)
		self.tree.SelectItem(self.troot)
		self.tree.Bind(wx.EVT_TREE_SEL_CHANGED,self.treeevent)
		# PANEL
		lc = wx.ListCtrl(self.splitv,style=wx.LC_REPORT)
		# CONF SPLITTERS
		self.splitv.SplitVertically(self.tree,lc,200)
		self.Centre()
		if len(sys.argv) > 1:
			# assume path to project is first variable
			path = sys.argv[1]
			pd = wx.ProgressDialog('Opening project','Please wait - this might take a while...',100,self)
			pd.Update(10)
			self.openpath(path)
			pd.Destroy()

	def new(self,event):
		if self.projectopen == True:
			#TODO error message
			return
		dialog = wx.DirDialog(self,'Select new project directory','.',wx.DD_DEFAULT_STYLE)
		if dialog.ShowModal() == wx.ID_OK:
			path = dialog.GetPath()
			dialog.Destroy()
			dialog = wx.SingleChoiceDialog(self,'Choose project currency:','Currency',self.datacoll.ALLOWEDCURRENCIES)
			if dialog.ShowModal() == wx.ID_OK:
				currency = dialog.GetStringSelection()
			else:
				currency = 'EUR'
			dialog.Destroy()
			pd = wx.ProgressDialog('Create new project','Please wait... this might take a while.',100,self)
			if self.owner.new(path,currency) == False:
				pd.Destroy()
				self.errdlg('Error: Failed to create new project!')
				return
		else:
			dialog.Destroy()
			return
		if currency == '':
			currency = 'EUR'
		self.openpath(path,pd)
		pd.Destroy()

	def open(self,event):
		if self.projectopen == True:
			#TODO error message
			return
		dialog = wx.DirDialog(self,'Select project directory','.',wx.DD_DEFAULT_STYLE)
		if dialog.ShowModal() == wx.ID_OK:
			path = dialog.GetPath()
			pd = wx.ProgressDialog('Opening project','Please wait - this might take a while...',100,self)
			pd.Update(10)
			self.openpath(path,pd)
			pd.Destroy()
		dialog.Destroy()

	def openpath(self,path,pd=False):
		if self.owner.open(path,pd) == True:
			#self.menufile.Enable(gui_c.ID_NEWPROJ,False)
			#self.menufile.Enable(gui_c.ID_OPEPROJ,False)
			#self.menufile.Enable(gui_c.ID_SAVPROJ,True)
			#self.menuproj.Enable(gui_c.ID_UPDPROJ,True)
			#self.menuproj.Enable(gui_c.ID_CALPROJ,True)
			self.createtree()
			self.projectopen = True
		else:
			self.errdlg('Error: Failed to open project!')

	def save(self,event):
		if self.projectopen == False:
			#TODO error message
			return
		pd = wx.ProgressDialog('Saving project','Please wait - this might take a while...',100,self)
		pd.Update(10)
		rv = self.owner.save(pd)
		pd.Destroy()
		if rv == False:
			self.errdlg('Error: Failed to save project!')
			return
		self.unsaved_data = 0

	def closeprogram(self,event):
		if self.datacoll.unsaved == 1:
			msg = wx.MessageDialog(self,'The financial data has been modified.\nDo you want to save your changes before exiting?','Save data',wx.YES_NO|wx.YES_DEFAULT|wx.CANCEL|wx.ICON_QUESTION)
			rv = msg.ShowModal()
			msg.Destroy()
			if rv == wx.ID_YES:
				self.owner.datacoll.savefiles()
			if rv == wx.ID_CANCEL:
				return
		self.Destroy()

	def quit(self,event):
		self.Close()

	def updateproject(self,event):
		if self.projectopen == False:
			#TODO error message
			return
		pd = wx.ProgressDialog('Updating stock quotes and currency rates','Please wait - this might take a while...',100,self)
		pd.Update(10)
		if self.owner.downloadquotes(pd) == True:
			self.tree.SelectItem(self.troot)
		pd.Destroy()
		self.calc(0)

	def calc(self,event):
		if self.projectopen == False:
			#TODO error message
			return
		pd = wx.ProgressDialog('Calculating result','Please wait - this might take a while...',100,self)
		pd.Update(10)
		if self.owner.calc(pd) == True:
			self.tree.SelectItem(self.troot)
		pd.Destroy()

	def showupdate(self,event):
		updateavailable = self.owner.checkforupdate()
		if updateavailable == 1:
			msg = 'A new version of Frugal is available for download!\n\nPlease go to http://www.samoht.se/frugal/ and click Download!'
			self.SetTitle(self.frametitle+' (new version available for download)')
		elif updateavailable == -1:
			msg = 'Failed to read latest version! Please retry.'
		else:
			msg = 'No new version of Frugal available!'
		notice = wx.MessageDialog(self,msg,self.pinfo,wx.OK|wx.ICON_INFORMATION)
		notice.ShowModal()
		notice.Destroy()

	def updatecallback(self):
		# UPDATE
		#self.menuupdate = wx.Menu()
		#item = self.menuupdate.Append(gui_c.ID_UPDATE,'Show available update','Show available updates')
		#self.menubar.Append(self.menuupdate,'Update')
		self.SetTitle(self.frametitle+' (new version available for download)')

	def about(self,event):
		about = wx.MessageDialog(self,self.ainfo,self.pinfo,wx.OK|wx.ICON_INFORMATION)
		about.ShowModal()
		about.Destroy()

	def usersguide(self,event):
		webbrowser.open('http://www.samoht.se/frugal/usersguide.php')

	def createtree(self):
		for p in self.plugins:
			itm = self.tree.AppendItem(self.troot,p.NAME)
			for f in p.FUNCTIONS:
				self.tree.AppendItem(itm,f)
		self.tree.Expand(self.troot)

	def treeevent(self,event):
		itm = event.GetItem()
		text = self.tree.GetItemText(itm)
		selectedplugin = None
		attr = []
		if self.menuview.FindItemById(gui_c.ID_DETAILS).IsChecked():
			attr.append('details')
		elif self.menuview.FindItemById(gui_c.ID_HISTORY).IsChecked():
			attr.append('history')
		elif self.menuview.FindItemById(gui_c.ID_HISTORYY).IsChecked():
			attr.append('historyy')
		elif self.menuview.FindItemById(gui_c.ID_GDETAILS).IsChecked():
			attr.append('gdetails')
		elif self.menuview.FindItemById(gui_c.ID_GHISTORY).IsChecked():
			attr.append('ghistory')
		elif self.menuview.FindItemById(gui_c.ID_GHISTORYY).IsChecked():
			attr.append('ghistoryy')
		for p in self.plugins:
			if p.NAME == text:
				selectedplugin = p(self,self.splitv,func=p.NAME,attr=attr)
			for f in p.FUNCTIONS:
				if f == text:
					selectedplugin = p(self,self.splitv,func=f,attr=attr)
		if not selectedplugin == None:
			self.showpanel(selectedplugin)

	def showpanel(self,panel):
		old = self.splitv.GetWindow2()
		self.splitv.ReplaceWindow(old,panel)
		old.Destroy()

	def errdlg(self,text='',title='Error',style=wx.OK|wx.ICON_ERROR):
		return self.msgdlg(text,title,style)

	def msgdlg(self,text='',title='Information',style=wx.OK|wx.ICON_INFORMATION):
		msg = wx.MessageDialog(self,text,title,style)
		rv = msg.ShowModal()
		msg.Destroy()
		return rv

	ID_NEWPROJ = 1001
	ID_OPEPROJ = 1002
	ID_SAVPROJ = 1003
	ID_EXIT    = 1004
	ID_CALPROJ = 1005
	ID_ABOUT   = 1006
	ID_ADDTRAN = 1007
	ID_DETAILS = 1008
	ID_HISTORY = 1009
	ID_GDETAILS = 1010
	ID_GHISTORY = 1011
	ID_CLOPROJ = 1012
	ID_UPDATE = 1013
	ID_HISTORYY = 1014
	ID_GHISTORYY = 1015
	ID_UPDPROJ = 1016
	ID_USERGU = 1017

class appl_c(wx.App):
	def OnInit(self):
		return True

	def setup(self,program,version,about,owner):
		frame = gui_c(None,program,version,about,owner)
		frame.Show()


import glob
import PinshCmd
import BomHostField
from commonUtil import *

class ScpHostField(PinshCmd.PinshCmd):
    def __init__(self):
        PinshCmd.PinshCmd.__init__(self, name = "scpHostName")
        self.helpText = "<path>\tA path to a remote location. Follows pattern: <hostname>:/path/to/file"
        self.bomHostField = BomHostField.BomHostField()
        self.cmdOwner = 0
        self.pathCache = {}
        self.tokenDelimiter = ''

    def checkPossiblePathCache(self, hostName, checkPath):
        basePath = '/'.join(checkPath.split('/')[:-1])
        if not basePath in self.pathCache:
            p = mode.getBomConnection(hostName)
            p.freshen()
            remotePaths = p.checkPossiblePaths(basePath+'/')
            self.pathCache[basePath] = remotePaths
        returnValue = []
        for path in self.pathCache[basePath]:
            if checkPath in path:
                returnValue.append(hostName+":"+path)
        return returnValue

    def getPossibleHostNames(self, dest):
        if ':' in dest:
            target = dest.split(':')[0]
        else:
            target = dest
        possibleHostNames = self.bomHostField.preferredNames([target], 0)
        return possibleHostNames

    def preferredNames(self, tokens, index):
        dest = tokens[index]
        hostNames = self.getPossibleHostNames(dest)
        if ':' in dest:
            if len(hostNames) != 1:
                return []
            hostName = hostNames[0]
            path = dest.split(':')[1]
            if not path:
                return [hostName+':']
            return self.checkPossiblePathCache(hostName, path)
        else:
            if hostNames:
                return hostNames
            else:
                possibleLocalNames = glob.glob(dest+"*")
                if dest == '.':
                    possibleLocalNames.append('.')
                if os.path.isdir(dest):
                    possibleLocalNames.append(dest)
                return possibleLocalNames
        return []

    def match(self, tokens, index):
        possibleMatches = self.preferredNames(tokens, index)
        if not possibleMatches:
            return NO_MATCH, 1
        if len(possibleMatches) > 1:
            return PARTIAL, 1
        return COMPLETE, 1

if __name__ == "__main__":
    # A server must be enabled for the tests to work properly.
    from libTest import startTest, runTest, endTest
    hostField = ScpHostField()
    status = OK
    startTest()
    status = runTest(hostField.preferredNames, [["bigap:/usr/l"], 0], ['bigap:/usr/lib/', 'bigap:/usr/libexec/', 'bigap:/usr/local/'], status)
    status = runTest(hostField.preferredNames, [["biga"], 0], ['bigap'], status)
    status = runTest(hostField.preferredNames, [["/usr/l"], 0], ['/usr/lib', '/usr/lib64', '/usr/local'], status)
    status = runTest(hostField.preferredNames, [["/tmp/2.ovpn"], 0], ['/tmp/2.ovpn'], status)
    status = runTest(hostField.preferredNames, [["bigap:/tmp/"], 0], ['bigap:/tmp/sudoers', 'bigap:/tmp/'], status)
    #status = runTest(hostField.preferredNames, [["/tmp/"], 0], ['/tmp/'], status)
    endTest(status)


# n, m, k, p = [int(input()) for _ in range(4)]
# print(n - ((m + k) - p))
##############
#2
# ls = input().split()
# a = set(ls)
# print(len(ls) - len(a))
# res = set()
# for i in range(int(input())):
#     res.add(input())
# f = input()
# if f in res:
#     print('REPEAT')
# else:
#     print('OK')
################
# m = int(input())
# n = int(input())
# dom = {input() for _ in range(m)}
# for i in range(n):
#     if input() in dom:
#         print('YES')
#     else:
#         print('NO')
#############
# a = {int(i) for i in input().split()}
# b = {int(i) for i in input().split()}
# res = set(a.intersection(b))
# if len(res) == 0:
#     print('BAD DAY')
# else:
#     print(*sorted(res, reverse=True))
# myset1 = {int(i) for i in input().split()}
# myset2 = {int(i) for i in input().split()}
#
# f = myset2 - myset1
# flag = myset1.issubset(myset2)
# if len(f) != 0:
# #     print('NO')
# # else:
# #     if flag == True:
# #         print('YES')
# #     else:
# #         print('NO')
# m, n = int(input()), int(input())
# mat = {input() for _ in range(m)}
# info = {input() for _ in range(n)}
#
# res = set()
# res.update(mat - info)
# res.update(info - mat)
#
# if len(res) == 0:
#     print('NO')
# else:
#     print(len(res))
###########
# a, b = set(input().split()), set(input().split())
# a.update(b)
# print(*sorted(a))

#############
# m = int(input())
# n = int(input())
# all_shool = [input()  for _ in range(m+n)]
#
# a = {}
# for i in all_shool:
#     a.setdefault(i, all_shool.count(i))
#
# couny = 0
# for i in a.values():
#     if i == 1:
#         couny += 1
# if couny == 0:
#     print('NO')
# else:
#     print(couny)
# #########
# # res = [input() for _ in range(int(input()))]
# one = set(res[0])
# for i in res:
# #     one.intersection_update(i)
# # print(*sorted(one))
res = [{input() for _ in range(int(input()))} for _ in range(int(input()))]
one = set(res[0])
for i in res:
    one.intersection_update(i)
print(sorted(one), sep='\n')


import os
class InputProject():
    # data
    # Project path; file list;
    # id-file saving
    def __init__(self, inputPathList, extNameArr):
        self.fileList    = [] # projectId-file
        self.projectList = inputPathList

        if len(extNameArr) <= 0:
            print('extName configuration error. Please check config.json.')
            return False
        else:
            self.__extName = set()
            for i in extNameArr:
                self.__extName.add(i)

        for projectId in range(len(inputPathList)):
            if self.__checkPath(inputPathList[projectId]):
                self.fileList.append(self.__addFiles(inputPathList[projectId]))
            else:
                print("path:" + inputPathList[projectId] + "not found.")

    def __addFiles(self, inputPath):
        res = []
        fileWalking = os.walk(inputPath)
        tmp = ''
        for path, dir_list, file_list in fileWalking:
            for file_name in file_list:
                tmp = os.path.abspath(path + '/' + file_name)
                if self.__ifTargetFile(tmp):
                    res.append(tmp)
        return res

    def addFile(self, filePath):
        if self.__ifTargetFile(filePath):
            self.fileList.append(filePath)

    def __ifTargetFile(self, filePath):
        extStr = os.path.splitext(filePath)[1]
        return {extStr}.issubset(self.__extName)

    def __checkPath(self,pathStr):
        return os.path.exists(pathStr)

    def saveFileList(self, folderPath):
        fileListPath = folderPath + '/fileList.txt'
        file = open(fileListPath,'w')
        for projectId in range(len(self.fileList)):
            for filePath in self.fileList[projectId]:
                file.write(str(projectId) + ',' + filePath + '\n')
        print("File list created in file:" + fileListPath)
        file.close()


import numpy as np
import cv2

img = cv2.imread('/Users/macbookair/PycharmProjects/PR/homework2/unpro.jpg')
bg = cv2.imread('/Users/macbookair/PycharmProjects/PR/homework2/back2.png')#---->3750*2500
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (1250,25,2000,2325)
# rect = (1,1,l-2,w-2)
#1250 25 2400 2450

cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)

mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
mask2 = cv2.erode(mask2,None,iterations=2)
mask2 = cv2.dilate(mask2,None,iterations=1)
mask_inv = np.where((mask2==0),1,0).astype('uint8')

img1 = img*mask2[:,:,np.newaxis]
img2 = bg*mask_inv[:,:,np.newaxis]
dst=cv2.addWeighted(img1,1,img2,1,0)
cv2.imshow('output',dst)
cv2.imwrite('output.jpg',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()


import cv2
import numpy as np
from PIL import Image

# Load the image
img = cv2.imread('ParkingLot.jpg')
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply edge detection
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# Apply Hough line detection
lines = cv2.HoughLines(edges, rho=1, theta=np.pi/180, threshold=100)
# Cluster the detected lines
line_clusters = []
for line in lines:
    rho, theta = line[0]
    if len(line_clusters) == 0:
        line_clusters.append([(rho, theta)])
    else:
        found_cluster = False
        for cluster in line_clusters:
            if abs(cluster[0][0] - rho) < 50 and abs(cluster[0][1] - theta) < np.pi/36:
                cluster.append((rho, theta))
                found_cluster = True
                break
        if not found_cluster:
            line_clusters.append([(rho, theta)])
# Find intersection points
intersections = []
for cluster in line_clusters:
    for i in range(len(cluster)):
        rho1, theta1 = cluster[i]
        for j in range(i+1, len(cluster)):
            rho2, theta2 = cluster[j]
            A = np.array([[np.cos(theta1), np.sin(theta1)], [np.cos(theta2), np.sin(theta2)]])
            b = np.array([rho1, rho2])
            x, y = np.linalg.solve(A, b)
            if x >= 0 and x < img.shape[1] and y >= 0 and y < img.shape[0]:
                intersections.append((int(x), int(y)))
# Find parking space polygons
polygons = []
for i in range(len(intersections)):
    for j in range(i+1, len(intersections)):
        for k in range(j+1, len(intersections)):
            for l in range(k+1, len(intersections)):
                p1, p2, p3, p4 = intersections[i], intersections[j], intersections[k], intersections[l]
                sides = [cv2.norm(np.array(p1) - np.array(p2)),
                         cv2.norm(np.array(p2) - np.array(p3)),
                         cv2.norm(np.array(p3) - np.array(p4)),
                         cv2.norm(np.array(p4) - np.array(p1))]
                if all(side > 30 for side in sides):
                    area = cv2.contourArea(np.array([p1, p2, p3, p4]))
                    if area > 1000:
                        polygons.append([p1, p2, p3, p4])

# Draw the polygons on the image
for i, parking_space in enumerate(polygons):
    pts = np.array(parking_space, np.int32)
    pts = pts.reshape((-1, 1, 2))
    cv2.polylines(img, [pts], True, (0, 255, 0), thickness=2)

# Display the image with polygons drawn
cv2.imshow('Parking Lot with Polygons', img)
cv2.waitKey(0)
cv2.destroyAllWindows()