from __future__ import division

import os
import sys
import math

ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(ROOT, '..'))

from pygooglechart import Chart
from pygooglechart import SimpleLineChart
from pygooglechart import XYLineChart
from pygooglechart import SparkLineChart
from pygooglechart import Axis

import settings
import helper

def simple_random():
    chart = SimpleLineChart(settings.width, settings.height, y_range=(0, 100))
    chart.add_data(helper.random_data())
    chart.download('line-simple-random.png')

def xy_random():
    chart = XYLineChart(settings.width, settings.height,
                        x_range=(0, 100), y_range=(0, 100))
    chart.add_data(helper.random_data())
    chart.add_data(helper.random_data())
    chart.download('line-xy-random.png')

def xy_rect():
    chart = XYLineChart(settings.width, settings.height,
                        x_range=(0, 100), y_range=(0, 100))
    chart.add_data([10, 90, 90, 10, 10])
    chart.add_data([10, 10, 90, 90, 10])
    chart.download('line-xy-rect.png')

def xy_circle():
    chart = XYLineChart(settings.width, settings.height,
                        x_range=(0, 100), y_range=(0, 100))
    steps = 40
    xradius = 25
    yradius = 45
    xmid = 50
    ymid = 50
    xlist = []
    ylist = []
    for angle in range(0, steps + 1):
        angle = float(angle) / steps * math.pi * 2
        xlist.append(math.cos(angle) * xradius + xmid)
        ylist.append(math.sin(angle) * yradius + ymid)
    chart.add_data(xlist)
    chart.add_data(ylist)
    chart.download('line-xy-circle.png')

def sparklines():
    chart = SparkLineChart(settings.width, settings.height)
    chart.add_data(helper.random_data())
    chart.download('line-sparkline.png')

def fill():

    # Set the vertical range from 0 to 50
    max_y = 50
    chart = SimpleLineChart(200, 125, y_range=[0, max_y])

    # First value is the highest Y value. Two of them are needed to be
    # plottable.
    chart.add_data([max_y] * 2)

    # 3 sets of real data
    chart.add_data([28, 30, 31, 33, 35, 36, 42, 48, 43, 37, 32, 24, 28])
    chart.add_data([16, 18, 18, 21, 23, 23, 29, 36, 31, 25, 20, 12, 17])
    chart.add_data([7, 9, 9, 12, 14, 14, 20, 27, 21, 15, 10, 3, 7])

    # Last value is the lowest in the Y axis.
    chart.add_data([0] * 2)

    # Black lines
    chart.set_colours(['000000'] * 5)

    # Filled colours
    # from the top to the first real data
    chart.add_fill_range('76A4FB', 0, 1)

    # Between the 3 data values
    chart.add_fill_range('224499', 1, 2)
    chart.add_fill_range('FF0000', 2, 3)

    # from the last real data to the
    chart.add_fill_range('80C65A', 3, 4)

    # Some axis data
    chart.set_axis_labels(Axis.LEFT, ['', max_y / 2, max_y])
    chart.set_axis_labels(Axis.BOTTOM, ['Sep', 'Oct', 'Nov', 'Dec'])

    chart.download('line-fill.png')

def stripes():

    # Set the vertical range from 0 to 100
    max_y = 100

    # Chart size of 200x125 pixels and specifying the range for the Y axis
    chart = SimpleLineChart(200, 125, y_range=[0, max_y])

    # Add the chart data
    data = [
        32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37,
        37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55,
        55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32
    ]
    chart.add_data(data)

    # Set the line colour to blue
    chart.set_colours(['0000FF'])

    # Set the vertical stripes
    chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2)

    # Set the horizontal dotted lines
    chart.set_grid(0, 25, 5, 5)

    # The Y axis labels contains 0 to 100 skipping every 25, but remove the
    # first number because it's obvious and gets in the way of the first X
    # label.
    left_axis = list(range(0, max_y + 1, 25))
    left_axis[0] = ''
    chart.set_axis_labels(Axis.LEFT, left_axis)

    # X axis labels
    chart.set_axis_labels(Axis.BOTTOM, \
        ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])

    chart.download('line-stripes.png')

def main():
    simple_random()
    xy_random()
    xy_rect()
    xy_circle()
    sparklines()
    fill()
    stripes()

if __name__ == '__main__':
    main()


import logging
from functools import wraps

from flask import request

from GeoPredictor.errors import GeostoreNotFound, error
from GeoPredictor.services import GeostoreService

def remove_keys(keys, dictionary):
    for key in keys:
        try:
            del dictionary[key]
        except KeyError:
            pass
        except Exception as err:
            return error(status=502, detail=f'{err}')
    return dictionary

def sanitize_parameters(func):
    """Sets any queryparams in the kwargs"""

    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            logging.debug(f'[middleware] [sanitizer] args: {args}')
            logging.debug(f'[middleware] [sanitizer] kargs: {kwargs}')

            myargs = {**kwargs, **request.args, **request.args.get('payload', {})}
            logging.debug(f'[middleware] [sanitizer] User_args: {myargs}')
            # Exclude params like loggedUser here

            sanitized_args = remove_keys(['loggedUser'], myargs)
            kwargs['params'] = sanitized_args
        except GeostoreNotFound:
            return error(status=404, detail='body params not found')
        except Exception as err:
            return error(status=502, detail=f'{err}')

        return func(*args, **kwargs)

    return wrapper

def parse_payload(func):
    """Get payload data"""

    @wraps(func)
    def wrapper(*args, **kwargs):
        kwargs["payload"] = request.args.get('payload', {'payload': None})
        return func(*args, **kwargs)

    return wrapper

def get_geo_by_hash(func):
    """Get geostore data"""

    @wraps(func)
    def wrapper(*args, **kwargs):

        try:
            if 'geostore' in kwargs["sanitized_params"].keys():
                geostore = kwargs["sanitized_params"]["geostore"]
                logging.info(f'[middleware]: {geostore}')
                geojson = GeostoreService.get(geostore)
                kwargs["sanitized_params"]["geojson"] = geojson
        except GeostoreNotFound:
            return error(status=404, detail='Geostore not found')
        except Exception as err:
            return error(status=502, detail=f'{err}')

        return func(*args, **kwargs)

    return wrapper


import os
import json
import time
import copy
import importlib
import argparse
import scipy.misc as misc
import numpy as np
import skimage.measure as measure
from collections import OrderedDict
from PIL import Image

import torch
import torch.nn as nn
from torch.autograd import Variable
from dataset import TestDataset

def psnr(im1, im2):
    def im2double(im):
        min_val, max_val = 0, 255
        out = (im.astype(np.float64)-min_val) / (max_val-min_val)
        return out

    im1 = im2double(im1)
    im2 = im2double(im2)
    psnr = measure.compare_psnr(im1, im2, data_range=1)
    return psnr

def generate_single(net, lr, hr, chunk, stage, cfg):
    scale_diff = cfg.scale_diff
    h, w = lr.size()[1:]
    h_chunk, w_chunk = int(h/chunk), int(w/chunk)
    h_chop, w_chop   = h_chunk + cfg.shave, w_chunk + cfg.shave

    lr_patch = torch.FloatTensor(chunk**2, 3, h_chop, w_chop)
    for i in range(chunk):
        for j in range(chunk):
            h_from, h_to = i*h_chunk, (i+1)*h_chunk+cfg.shave
            w_from, w_to = j*w_chunk, (j+1)*w_chunk+cfg.shave
            if (i+1) == chunk: h_from, h_to = h-h_chop, h
            if (j+1) == chunk: w_from, w_to = w-w_chop, w
            lr_patch[i+j*chunk].copy_(lr[:, h_from:h_to, w_from:w_to])
    lr_patch = Variable(lr_patch, volatile=True).cuda()

    h, h_chunk, h_chop = h*scale_diff, h_chunk*scale_diff, h_chop*scale_diff
    w, w_chunk, w_chop = w*scale_diff, w_chunk*scale_diff, w_chop*scale_diff

    sr = np.empty((chunk**2, h_chop, w_chop, 3), dtype=np.uint8)
    for i, patch in enumerate(lr_patch):
        out = net(patch.unsqueeze(0), stage).data[0]
        out = out.cpu().mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()
        sr[i] = out

    result = np.empty((h, w, 3), dtype=np.uint8)
    for i in range(chunk):
        for j in range(chunk):
            h_from, h_to = 0, h_chunk
            w_from, w_to = 0, w_chunk
            hh_from, hh_to = i*h_chunk, (i+1)*h_chunk
            ww_from, ww_to = j*w_chunk, (j+1)*w_chunk

            if (i+1) == chunk:
                h_from, h_to = -h_chunk-(h-(i+1)*h_chunk), None
                hh_from, hh_to = i*h_chunk, None
            if (j+1) == chunk:
                w_from, w_to = -w_chunk-(w-(j+1)*w_chunk), None
                ww_from, ww_to = j*w_chunk, None

            result[hh_from:hh_to, ww_from:ww_to, :] = \
                copy.deepcopy(sr[i+j*chunk, h_from:h_to, w_from:w_to, :])
    return result

def recover_origin(image, index):
    angle = [0, 3, 2, 1]
    image = np.rot90(image, angle[index%4]).copy()
    if index > 3:
        image = np.fliplr(image).copy()

    return image

def evaluate(net, dataset, chunk, stage, cfg):
    net.eval()
    scale_diff = cfg.scale_diff
    mean_psnr, mean_runtime = 0, 0
    for step, (lr_ensemble, hr, name) in enumerate(dataset):
        hr = hr.cpu().mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()

        h, w = lr_ensemble[0].size()[1:]
        sr_ensemble = np.zeros((len(lr_ensemble), h*scale_diff, w*scale_diff, 3))

        t1 = time.time()
        for i, lr in enumerate(lr_ensemble):
            tmp_image = generate_single(net, lr, hr, chunk, stage, cfg)
            sr_ensemble[i] = recover_origin(tmp_image, i)

        sr = np.mean(sr_ensemble, axis=0)
        sr = np.clip(sr, 0, 255)

        t2 = time.time()

        # match resolution when stage is less then two
        if int(cfg.scales[0]/scale_diff) > 1:
            sr = misc.imresize(sr, cfg.scales[0]/scale_diff)

        # crop HR to match SR
        hr = hr[:sr.shape[0], :sr.shape[1]]
        bnd = int(cfg.scales[0]/cfg.scales[-1]) + 6
        hr = hr[bnd:-bnd, bnd:-bnd]
        sr = sr[bnd:-bnd, bnd:-bnd]

        mean_psnr += psnr(hr, sr) / len(dataset)
        mean_runtime += (t2-t1) / len(dataset)

    return mean_runtime, mean_psnr

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str)

    parser.add_argument("--dirname", type=str)
    parser.add_argument("--data_from", type=str)
    parser.add_argument("--data_to", type=str)

    parser.add_argument("--ckpt_path", type=str)
    parser.add_argument("--shave", type=int, default=20)

    parser.add_argument("--chunk", type=int)
    parser.add_argument("--stage", type=int)

    return parser.parse_args()

def main(cfg):
    cfg.scales = list()
    cfg.scales.append([int(s) for s in cfg.data_from if s.isdigit()][-1])
    if "HR" in cfg.data_to:
        cfg.scales.append(1)
    else:
        cfg.scales.append([int(s) for s in cfg.data_to if s.isdigit()][-1])
    cfg.scale_diff = 8

    module = importlib.import_module("model.{}".format(cfg.model))
    net = module.Net()
    print(json.dumps(vars(cfg), indent=4, sort_keys=True))

    state_dict = torch.load(cfg.ckpt_path)
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k
        # name = k[7:] # remove "module."
        new_state_dict[name] = v

    net.load_state_dict(new_state_dict)
    net.cuda()

    dataset = TestDataset(cfg.dirname,
                          cfg.scale_diff,
                          cfg.data_from,
                          cfg.data_to, self_ensemble=True)

    mean_runtime, mean_psnr = evaluate(net, dataset, cfg.chunk, cfg.stage, cfg)
    print("Mean runtime: {:.3f}s mean PSNR: {:.3f}".format(mean_runtime, mean_psnr))

if __name__ == "__main__":
    cfg = parse_args()
    main(cfg)


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os

#read data
def readData(filename):
    return pd.read_csv(filename)

def iterateData():
    dfdict = {}
    filedict = {}
    for directory in os.listdir("./data/"):
        dflst = []
        filelst = []
        for filename in os.listdir("./data/"+directory):
            print(f"Reading {filename} ")
            dflst.append(readData("./data/"+directory+"/"+filename))
            filelst.append(filename)
        dfdict[directory] = dflst
        filedict[directory] = filelst
    return dfdict, filedict

def meanDict(df, filename, slicedf=None, example=False):
    if not example:
        if slicedf:
            df = df.loc[df['time'] < slicedf]
        if list(df.keys()) == ['time', ' Feed Pressure [bara]','time.1', ' Permeate Pressure [bara]', 'time.2',' Retentate Pressure [bara]', 'time.3', ' O_2  Permeate [%]', 'time.4', ' O_2 Retentate [%]', 'time.5', ' Flow Meter [l/min]','time.6', ' Flow Controller Range (0-50 SLPM) [l/min]']:
            d = {
                'filename': filename,
                'Feed Pressure [Pa]': df[' Feed Pressure [bara]'].mean() * 10**5,
                'Permeate Pressure [Pa]': df[' Permeate Pressure [bara]'].mean() * 10**5,
                'Retentate Pressure [Pa]': df[' Retentate Pressure [bara]'].mean() * 10**5,
                'O_2  Permeate': df[' O_2  Permeate [%]'].mean()/100,
                'O_2 Retentate': df[' O_2 Retentate [%]'].mean()/100,
                'Permeate flow [m^3/s]': df[' Flow Meter [l/min]'].mean() * (10**(-3)/60),
                'Retentate flow [m^3/s]': df[' Flow Controller Range (0-50 SLPM) [l/min]'].mean() * (10**(-3)/60),
                }
            return d
        else:
            print("ERROR invalid keys in csv file")
            return {'filename': filename}
    else:
        return {
                'filename': "testfile",
                'Feed Pressure [Pa]': 4.7 * 10**5,
                'Permeate Pressure [Pa]': 0.9 * 10**5,
                'Retentate Pressure [Pa]': 4.6 * 10**5,
                'O_2  Permeate': 40.7/100,
                'O_2 Retentate': 16/100,
                'Permeate flow [m^3/s]': 4.1 * (10**(-3)/1),#permeate flow
                'Retentate flow [m^3/s]': 21.3 * (10**(-3)/1),#rententate flow
                }

#formulas

def massBalance(V_r, V_p):
    return V_r + V_p

def yN(yO):
    return 1-yO

def componentBalanceYf(V_f,V_p, V_r,y_r_c,y_p_c):
    return (V_p*y_p_c+V_r*y_r_c)/V_f

def cutRate(V_p, V_f):
    return V_p/V_f

def pressureRatio(p_f, p_p):
    return p_f/p_p

def recovery(y_rp_c,V_rp, y_f_c, V_f):
    return (y_rp_c * V_rp)/(y_f_c * V_f)

def example():
    d = meanDict(None,None,example=True)
    d['Feed flow [m^3/s]'] = massBalance(d['Retentate flow [m^3/s]'], d['Permeate flow [m^3/s]'])
    d['N_2  Permeate'] = yN(d['O_2  Permeate'])
    d['N_2  Retentate'] = yN(d['O_2 Retentate'])
    d['O_2  Feed'] = componentBalanceYf(d['Feed flow [m^3/s]'],d['Permeate flow [m^3/s]'], d['Retentate flow [m^3/s]'],d['O_2 Retentate'],d['O_2  Permeate'])
    d['N_2  Feed'] = componentBalanceYf(d['Feed flow [m^3/s]'],d['Permeate flow [m^3/s]'], d['Retentate flow [m^3/s]'],d['N_2  Retentate'],d['N_2  Permeate'])
    d['Cut rate'] = cutRate(d['Permeate flow [m^3/s]'], d['Feed flow [m^3/s]'])
    d['Pressure ratio'] = pressureRatio(d['Feed Pressure [Pa]'], d['Permeate Pressure [Pa]'])
    d['Recovery O_2'] = recovery(d['O_2  Permeate'],d['Permeate flow [m^3/s]'], d['O_2  Feed'], d['Feed flow [m^3/s]'])
    d['Recovery N_2'] = recovery(d['N_2  Retentate'],d['Retentate flow [m^3/s]'], d['N_2  Feed'], d['Feed flow [m^3/s]'])
    print()
    for key,value in d.items():
        print(key, "    ", value)

def addToDict(d):
    d['Feed flow [m^3/s]'] = massBalance(d['Retentate flow [m^3/s]'], d['Permeate flow [m^3/s]'])
    d['N_2  Permeate'] = yN(d['O_2  Permeate'])
    d['N_2  Retentate'] = yN(d['O_2 Retentate'])
    d['O_2  Feed'] = componentBalanceYf(d['Feed flow [m^3/s]'],d['Permeate flow [m^3/s]'], d['Retentate flow [m^3/s]'],d['O_2 Retentate'],d['O_2  Permeate'])
    d['N_2  Feed'] = componentBalanceYf(d['Feed flow [m^3/s]'],d['Permeate flow [m^3/s]'], d['Retentate flow [m^3/s]'],d['N_2  Retentate'],d['N_2  Permeate'])
    d['Cut rate'] = cutRate(d['Permeate flow [m^3/s]'], d['Feed flow [m^3/s]'])
    d['Pressure ratio'] = pressureRatio(d['Feed Pressure [Pa]'], d['Permeate Pressure [Pa]'])
    d['Recovery O_2'] = recovery(d['O_2  Permeate'],d['Permeate flow [m^3/s]'], d['O_2  Feed'], d['Feed flow [m^3/s]'])
    d['Recovery N_2'] = recovery(d['N_2  Retentate'],d['Retentate flow [m^3/s]'], d['N_2  Feed'], d['Feed flow [m^3/s]'])
    return d

def meanDataLst(dlist):
    lstDict = {}
    for key in dlist[0].keys():
        lstDict[key] = []
    for d in dlist:
        for key in d:
            lstDict[key].append(d[key])
    return lstDict

def plot(plotKeys,lstlistDict):
    color = [[(255/255,0/255,0/255),(170/255,0/255,0/255)],[(0/255,255/255,0/255),(0/255,170/255,0/255)],[(0/255,0/255,255/255),(0/255,0/255,170/255)],[(255/255,255/255,0/255),(170/255,170/255,0/255)],[(0/255,255/255,255/255),(0/255,170/255,170/255)]]
    for d in plotKeys:
        for i in range(len(d["y"])):
            if d["type"] == "plot":
                for j, listDict in enumerate(lstlistDict):
                    parallel = listDict["filename"][0].replace("_1.csv", "").replace("1.csv", "").replace("_", " ")
                    if "single" in parallel:
                        plt.scatter(listDict[d["x"][0]], listDict[d["y"][i]], label=d["label"][i]+" "+parallel, color=color[j][i])
                    else:
                        plt.plot(listDict[d["x"][0]], listDict[d["y"][i]], label=d["label"][i]+" "+parallel, color=color[j][i])
            elif d["type"] == "scatter":
                for listDict in lstlistDict:
                    parallel = listDict["filename"][0].replace("_1.csv", "").replace("1.csv", "").replace("_", " ")
                    plt.scatter(listDict[d["x"][0]], listDict[d["y"][i]], label=d["label"][i]+" "+parallel, color=color[j][i])
        plt.xlabel(d["axis"][0])
        plt.ylabel(d["axis"][1])
        plt.legend()
        plt.grid()
        plt.show()

def plotLstKeys():
    #{"x": [""], "y":[""]}
    return [
        {"x": ["Pressure ratio"], "y":["O_2  Permeate"],"type": "plot", "label": [r"O$_2$  Permeate"], "axis": [r"Pressure ratio",r"O$_2$  Permeate (purity)"]},
        {"x": ["Pressure ratio"], "y":["N_2  Retentate"],"type": "plot", "label": [r"N$_2$  Retentate"], "axis": [r"Pressure ratio",r"N$_2$  Retentate (purity)"]},
        {"x": ["Pressure ratio"], "y":["Recovery O_2", "Recovery N_2"],"type": "plot", "label": [r"Recovery O$_2$", r"Recovery N$_2$"], "axis": [r"Pressure ratio",r"Recovery"]},
        {"x": ["Cut rate"], "y":["O_2  Permeate"],"type": "plot", "label": [r"O$_2$  Permeate"], "axis":  [r"Cut rate",r"O$_2$  Permeate (purity)"]},
        {"x": ["Cut rate"], "y":["N_2  Retentate"],"type": "plot", "label": [r"N$_2$ Retentate"], "axis":  [r"Cut rate",r"N$_2$ Retentate (purity)"]},
        {"x": ["Cut rate"], "y":["Recovery O_2", "Recovery N_2"], "type": "plot", "label": [r"Recovery O$_2$", r"Recovery N$_2$"], "axis": [r"Cut rate",r"Recovery"]},
        {"x": ["O_2  Permeate"], "y":["Recovery O_2"], "type": "plot", "label": [r"Recovery O$_2$"], "axis": [r"O$_2$ Permeate (purity)",r"Recovery O$_2$"]},
        {"x": ["N_2  Retentate"], "y":["Recovery N_2"], "type": "plot", "label": [r"Recovery N$_2$"], "axis": [r"N$_2$ Retentate (purity)",r"Recovery N$_2$"]}
        ]

def createDataDict():
    dDict = {}
    lstlistDict = []
    dfdict, filedict = iterateData()
    #print(filedict)
    for key, dflist in dfdict.items():
        dlst = []

        for count, df in enumerate(dflist):
            d = meanDict(df,filedict[key][count])
            d = addToDict(d)
            dlst.append(d)
        #for i in dlst:
            #print(i)
        dDict[key] = dlst
        lstlistDict.append(meanDataLst(dlst))
    return lstlistDict

def scatterParallels(listDictSerie):
    for key, value in listDictSerie.items():
        if type(value[0]) == np.float64:
            print(key, value, "mean: ",round(np.mean(value),5), "std: ", round(np.std(value),5))
        else:
            print(key, value)
    return

def main():
    lstlistDict = createDataDict()
    listDictSingle={}
    listDictSerie ={}
    listDictParallel3 ={}
    listDictParallel4 ={}
    for i, d in enumerate(lstlistDict):
        if "single" in d["filename"][0]:
            #listDictSingle = lstlistDict.pop(i)
            listDictSingle = lstlistDict[i]
        elif "serie" in d["filename"][0]:
            listDictSerie = lstlistDict[i]
        elif "parallel_4" in d["filename"][0]:
            listDictParallel4 = lstlistDict[i]
        elif "parallel_3" in d["filename"][0]:
            listDictParallel3 = lstlistDict[i]
    scatterParallels(listDictSingle)
    #print(lstlistDict)

    plotKeys = plotLstKeys()
    plot(plotKeys,lstlistDict)

    #example()

main()


import websocket, json
from typing import Optional
import threading
import time

class Coinbase(threading.Thread):

    def __init__(
        self,
        url: Optional[str] = 'wss://ws-feed.exchange.coinbase.com',
        exchange: Optional[str] = 'Coinbase',
        orderBook: Optional[dict] = dict(),
        lock = None
    ):
        super().__init__()
        self.url = url
        self.exchange = exchange
        self.orderBook = orderBook[exchange]
        self.lock = lock
        self.ws = websocket.WebSocketApp(self.url, on_open = self.on_open, on_message=self.on_message)

    def run(self):
        while True:
            self.ws.run_forever()

    def on_open(self,ws):
        """
            Definition: sets subscribe data
        """

        # print("Connection establish with Coinbase")

        subscribe = {
            "type":"subscribe",
            "channels": [
                {
                    "name":"ticker",
                    "product_ids": [
                        "BTC-USD",
                        "ETH-USD"
                    ]
                }
            ]
        }

        self.ws.send(json.dumps(subscribe))

    def on_message(self,ws,message):
        json_message = json.loads(message)
        # print(json_message)
        with self.lock:

            if json_message['product_id'] == 'BTC-USD':

                self.orderBook['BTC-USDT']['bid'] = json_message["best_bid"]
                self.orderBook['BTC-USDT']['ask'] = json_message["best_ask"]
            else:
                self.orderBook['ETH-USDT']['bid'] = json_message["best_bid"]
                self.orderBook['ETH-USDT']['ask'] = json_message["best_ask"]

class Binance(threading.Thread):

    def __init__(
        self,
        url: Optional[str] = 'wss://stream.binance.com:9443/ws',
        exchange: Optional[str] = 'Binance',
        orderBook: Optional[dict] = dict(),
        lock = None
    ):
        super().__init__()
        self.url = url
        self.exchange = exchange
        self.orderBook = orderBook[exchange]
        self.lock = lock
        self.ws =  websocket.WebSocketApp(self.url, on_open = self.on_open, on_message=self.on_message)

    def run(self):

        while True:
            self.ws.run_forever()

    def on_open(self,ws):

        # print("Connection established with Binance")

        subscribe = {
                "method":"SUBSCRIBE",
                "params": [
                        "btcusdt@bookTicker",
                        "ethusdt@bookTicker"
                    ],
                "id": 1
            }

        self.ws.send(json.dumps(subscribe))

    def on_message(self, ws, message):
        json_message = json.loads(message)

        with self.lock:

            if json_message['s'] == "BTCUSDT":

                self.orderBook["BTC-USDT"]['bid'] = json_message["b"]
                self.orderBook["BTC-USDT"]['ask'] = json_message["a"]

            elif json_message['s'] == "ETHUSDT":
                self.orderBook["ETH-USDT"]['bid'] = json_message["b"]
                self.orderBook["ETH-USDT"]['ask'] = json_message["a"]


f=open('data','r')
lines=f.readlines()
f.close()
accum=[]
for i in lines:
   j=i.split('\n')[0]  # first get rid of the '\n'
   k=j.split(',')      # now split on the comma
   r=[]
   for x in k:
      r = r + [int(x)]
   accum = accum + [r] # accumulate

def sortKey(x):
    return -x[2]
def genSortKey(col, up):
    def key(x):
        if up:
            return x[col]
        else:
            return -x[col]
    return key

sortKey = genSortKey(2,True)

print(sorted(accum, key=sortKey))


from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('sms', '0010_update_sqlmobilebackend_couch_id'),
    ]

    operations = [
        migrations.AlterField(
            model_name='sqlmobilebackend',
            name='inbound_api_key',
            field=models.CharField(unique=True, max_length=126, db_index=True),
            preserve_default=True,
        ),
    ]


# 메모리초과 방지 dfs
from sys import setrecursionlimit as st
st(10 ** 6)

def dfs(x):
    global tree
    visited[x] = 1
    for new_x in arr[x]:
        if not visited[new_x]:
            if connected[x][new_x]:
                tree = False
            else:
                connected[x][new_x] = 1
                connected[new_x][x] = 1
            dfs(x)
tc = 0
while True:
    n, m = map(int, input().split())
    if not n and not m:
        break
    tc += 1
    arr = [[] * (n+1) for _ in range(n+1)]
    for _ in range(m):
        a, b = map(int, input().split())
        arr[a].append(b)
        arr[b].append(a)
    cnt = 0
    connected = [[0] * (n + 1) for _ in range(n+1)]
    visited = [0] * (n+1)
    for i in range(1, n+1):
        tree = True
        if not visited[i]:
            dfs(i)
            if tree:
                cnt += 1

    if not cnt:
        print(f'Case {tc}: No trees.')
    else:
        if cnt == 1:
            print(f'Case {tc}: There is one tree.')
        else:
            print(f'Case {tc}: A forest of {cnt} trees.')


import os
import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info("#### I am logging Lambda versioning")

    request_body = json.loads(event.get('body'))

    name = request_body.get('name')

    if name == 'Joker':
        raise ValueError('Bad guys should not be greeted')

    return {
        "statusCode": 200,
        "body": json.dumps({
            "greeting": "Hello and wellcome again " + name,
        }),
    }


from __future__ import annotations

import warnings
from typing import Any

import numpy as np
import openslide
import PIL.Image
import pyvips

from dlup import UnsupportedSlideError
from dlup.backends.common import AbstractSlideBackend, numpy_to_pil
from dlup.types import PathLike
from dlup.utils.image import check_if_mpp_is_valid

def open_slide(filename: PathLike) -> "PyVipsSlide":
    """
    Read slide with pyvips.

    Parameters
    ----------
    filename : PathLike
        Path to image.
    """
    return PyVipsSlide(filename)

class PyVipsSlide(AbstractSlideBackend):
    """
    Backend for pyvips.
    """

    def __init__(self, filename: PathLike) -> None:
        """
        Parameters
        ----------
        filename : PathLike
            Path to image.
        """
        super().__init__(filename)
        # You can have pyvips figure out the reader
        self._images = []
        self._images.append(pyvips.Image.new_from_file(str(filename)))

        self._loader = self._images[0].get("vips-loader")

        self.__slide_bounds = (0, 0), (0, 0)

        if self._loader == "tiffload":
            self._read_as_tiff(filename)
        elif self._loader == "openslideload":
            self._read_as_openslide(filename)
        else:
            raise NotImplementedError(f"Loader {self._loader} is not implemented.")

        self._regions = [pyvips.Region.new(image) for image in self._images]

    def _read_as_tiff(self, path: PathLike) -> None:
        """
        Read all other pages except the first using the tiff backend of pyvips.

        Parameters
        ----------
        path : PathLike
        """
        self._level_count = int(self._images[0].get_value("n-pages"))
        for level in range(1, self._level_count):
            self._images.append(pyvips.Image.tiffload(str(path), page=level))

        # Each tiff page has a resolution
        unit_dict = {"cm": 1000, "centimeter": 1000}
        self._downsamples.append(1.0)
        for idx, image in enumerate(self._images):
            mpp_x = unit_dict.get(image.get("resolution-unit"), 0) / float(image.get("xres"))
            mpp_y = unit_dict.get(image.get("resolution-unit"), 0) / float(image.get("yres"))
            check_if_mpp_is_valid(mpp_x, mpp_y)

            self._spacings.append((mpp_x, mpp_y))
            if idx >= 1:
                downsample = mpp_x / self._spacings[0][0]
                self._downsamples.append(downsample)
            self._shapes.append((image.get("width"), image.get("height")))

        self.__slide_bounds = (0, 0), self._shapes[0]

    def _read_as_openslide(self, path: PathLike):
        """
        Read all other pages except the first using the openslide backend of pyvips.

        Parameters
        ----------
        path : PathLike
        """
        self._level_count = int(self._images[0].get("openslide.level-count"))
        for level in range(1, self._level_count):
            self._images.append(pyvips.Image.openslideload(str(path), level=level))

        for idx, image in enumerate(self._images):
            openslide_shape = (
                int(image.get(f"openslide.level[{idx}].width")),
                int(image.get(f"openslide.level[{idx}].height")),
            )
            pyvips_shape = (image.width, image.height)
            if not openslide_shape == pyvips_shape:
                raise UnsupportedSlideError(
                    f"Reading {path} failed as openslide metadata reports different shapes than pyvips. "
                    f"Got {openslide_shape} and {pyvips_shape}."
                )

            self._shapes.append(pyvips_shape)

        for idx, image in enumerate(self._images):
            self._downsamples.append(float(image.get(f"openslide.level[{idx}].downsample")))

        mpp_x, mpp_y = None, None
        available_fields = self._images[0].get_fields()
        if "openslide.mpp-x" in available_fields and "openslide.mpp-y" in available_fields:
            mpp_x = float(self._images[0].get("openslide.mpp-x"))
            mpp_y = float(self._images[0].get("openslide.mpp-y"))

        if mpp_x is not None and mpp_y is not None:
            check_if_mpp_is_valid(mpp_x, mpp_y)
            self._spacings = [(np.array([mpp_x, mpp_y]) * downsample).tolist() for downsample in self._downsamples]
        else:
            warnings.warn(
                f"{path} does not have a parseable spacings property. You can overwrite it with `.mpp = (mpp_x, mpp_y)."
            )

        if "openslide.bounds-x" in available_fields and "openslide.bounds-y" in available_fields:
            offset = int(self._images[0].get("openslide.bounds-x")), int(self._images[0].get("openslide.bounds-y"))
        else:
            offset = (0, 0)

        if "openslide.bounds-width" in available_fields and "openslide.bounds-height" in available_fields:
            size = int(self._images[0].get("openslide.bounds-width")), int(
                self._images[0].get("openslide.bounds-height")
            )
        else:
            size = self._shapes[0]

        self.__slide_bounds = offset, size

    @property
    def slide_bounds(self) -> tuple[tuple[int, int], tuple[int, int]]:
        """Returns the bounds of the slide as ((x,y), (width, height)). These can be smaller than the image itself."""
        return self.__slide_bounds

    @property
    def spacing(self) -> tuple[float, float] | None:
        if not self._spacings:
            return None
        return self._spacings[0]

    @spacing.setter
    def spacing(self, value: tuple[float, float]) -> None:
        if not isinstance(value, tuple) and len(value) != 2:
            raise ValueError("`.spacing` has to be of the form (mpp_x, mpp_y).")

        mpp_x, mpp_y = value
        check_if_mpp_is_valid(mpp_x, mpp_y)
        self._spacings = [(np.array([mpp_x, mpp_y]) * downsample).tolist() for downsample in self._downsamples]

    @property
    def properties(self):
        """Metadata about the image as given by pyvips,
        which includes openslide tags in case openslide is the selected reader."""

        keys = self._images[0].get_fields()
        return {key: self._images[0].get_value(key) for key in keys}

    @property
    def magnification(self) -> float | None:
        """Returns the objective power at which the WSI was sampled."""
        if self._loader == "openslideloader":
            return float(self._images[0].properties[openslide.PROPERTY_NAME_OBJECTIVE_POWER])
        else:
            return None

    @property
    def vendor(self) -> str | None:
        """Returns the scanner vendor."""
        if self._loader == "openslideloader":
            return str(self._images[0].properties[openslide.PROPERTY_NAME_VENDOR])
        return None

    @property
    def associated_images(self) -> dict[str, PIL.Image.Image]:
        """Images associated with this whole-slide image."""
        if not self._loader == "openslideload":
            return {}
        # TODO: Fix this
        # associated_images = (_.strip() for _ in self.properties["slide-associated-images"].split(","))
        raise NotImplementedError

    def set_cache(self, cache) -> None:
        raise NotImplementedError

    def read_region(self, coordinates: tuple[Any, ...], level: int, size: tuple[Any, ...]) -> PIL.Image.Image:
        """
        Return the best level for displaying the given image level.

        Parameters
        ----------
        coordinates : tuple
            Coordinates of the region in level 0.
        level : int
            Level of the image pyramid.
        size : tuple
            Size of the region to be extracted.

        Returns
        -------
        PIL.Image
            The requested region.
        """
        image = self._regions[level]
        ratio = self._downsamples[level]
        x, y = coordinates
        height, width = size

        region = np.asarray(image.fetch(int(x // ratio), int(y // ratio), int(height), int(width))).reshape(
            int(width), int(height), -1
        )

        return numpy_to_pil(region)

    def close(self) -> None:
        """Close the underlying slide"""
        del self._regions
        del self._images


from django.shortcuts import render
from django.views.generic import View
from infoapp.utils import is_json
from infoapp.mixins import HttpResponseMixin,SerializeMixin,GetObjectById
import json
from infoapp.models import Student
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from infoapp.forms import StudentForm

@method_decorator(csrf_exempt,name='dispatch')
class StudentCRUD(HttpResponseMixin,SerializeMixin,GetObjectById,View):
    def get(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'please provide valid json data'}),status=400)
        p_data=json.loads(data)
        id=p_data.get('id',None)
        if id is not None:
            std=self.get_object_by_id(id)
            if std is None:
                return self.render_http_response(json.dumps({'msg':'not matched record found with given id'}),status=400)
            json_data=self.serialize([std,])
            return self.render_http_response(json_data)
        qs=Student.objects.all()
        json_data=self.serialize(qs)
        return self.render_http_response(json_data)
    def post(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'please provide valid json data'}),status=400)
        std_data=json.loads(data)
        form=StudentForm(std_data)
        if form.is_valid():
            form.save(commit=True)
            return self.render_http_response(json.dumps({'msg':'Resource created successfully'}))
        if form.errors:
            json_data=json.dumps(form.errors)
            return self.render_http_response(json_data,status=400)
    def put(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'please provide valid json data'}),status=400)
        provided_data=json.loads(data)
        id=provided_data.get('id',None)
        if id is None:
            return self.render_http_response(json.dumps({'msg':'To perfrom updation id is mandatory'}),status=400)
        std=self.get_object_by_id(id)
        if std is None:
                return self.render_http_response(json.dumps({'msg':'No matched record found with the given id'}),status=400)
        original_date={
        'name':std.name,
        'rollno':std.rollno,
        'marks':std.marks,
        'location':std.location,
        }
        original_date.update(provided_data)
        form=StudentForm(original_date,instance=std)
        if form.is_valid():
            form.save(commit=True)
            return self.render_http_response(json.dumps({'msg':'Resources updated successfully'}))
        if form.errors:
            json_data=json.dumps(form.errors)
            return self.render_http_response(json_data,status=400)
    def delete(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'Please provide valid json data'}),status=400)
        p_data=json.loads(data)
        id=p_data.get('id',None)
        if id is None:
            return self.render_http_response(json.dumps({'msg':'To deletion id is mandatory please provide'}),status=400)
        std=self.get_object_by_id(id)
        if std is None:
            return self.render_http_response(json.dumps({'msg':'No matched record found with the given id'}),status=400)
        status,deleted_item=std.delete()
        if status==1:
            json_data=json.dumps({'msg':'Resource Deleted successfully'})
            return self.render_http_response(json_data)
        json_data=json.dumps({'msg':'unable to delte.. plzz try again'})
        return self.render_http_response(json_data,status=500)


user_input = int(input('Ввеите к-ство секунд: '))

hours = 0
minutes = 0
seconds = 0
days = user_input // 86400
# Вычисляем остачу секунд в дне от введного количества секунд
day_remainder = user_input % 86400
if day_remainder > 0:
    hours = day_remainder // 3600
    hours_remainder = day_remainder % 3600

    if hours_remainder > 0:
        minutes = hours_remainder // 60
        minutes_remainder = hours_remainder % 60
        seconds = minutes_remainder

print(f'{user_input} секунд = Дней:{days} Часов:{hours} Минут:{minutes} Cекунд:{seconds}')


from celery.result import AsyncResult

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from worker import start_task
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class RequestBody(BaseModel):
    prompt: str
    speaker_index: int
    output_file_name: str

@app.post("/api/generate", status_code=201)
def run_task(request_body: RequestBody):
    task = start_task.delay(
        request_body.prompt, request_body.speaker_index, request_body.output_file_name)
    return JSONResponse({"task_id": task.id})

@app.get("/api/tasks/{task_id}")
def get_status(task_id):
    task_result = AsyncResult(task_id)
    if task_result.failed():
        return JSONResponse({
            "task_id": task_id,
            "task_status": task_result.state,
            "exception": task_result.result.args[0]
        })
    result = {
        "task_id": task_id,
        "task_status": task_result.state,
        "task_result": task_result.result
    }
    return JSONResponse(result)


import unittest
from enum import Enum
from random import shuffle
from copy import deepcopy
from itertools import count

from zombiaki_karty import *
from zombiaki_plansza import *

#################################################################################
class Actions(Enum):
	Noop = 0
	PlaceCard = 1
	MoveCards = 2
	RemoveCards = 3
	DiscardCard = 4
	TakeCards = 5

class Action:
	def __init__(self, action, args=None, position=None):
		self.action = action
		self.args = args
		self.position = position

""" Przebieg gry:
1 runda
	zombiaki
		sprzątanie
		przesuwanie kota
		przesuwanie psa
		przesuwanie zombie
		dobieranie
		wyrzucanie
		zagranie kart
2 runda
	Ludzie
		sprzątanie
		przesuwanie
		dobieranie
		wyrzucanie
		zagranie kart
2 runda
	zombiaki
		...
	ludzie
		...
3 runda """

class Phase(Enum):
	RemoveExpiredCards=0
	CatMovement=1   #skipped if no cat in play
	DogMovement=2   #skipped if no dog in play
	OtherCardMovement=3
	TakeNewCards=4
	DiscardCard=5
	Play1stCard=6
	Play2ndCard=7
	Play3rdCard=8
	NumberOfPhases=9

	@staticmethod
	def next(currentPhase, currentPlayer, useCat, useDog):
		vn = currentPhase.value + 1
		#need two if's as both can be skipped
		if vn==Phase.CatMovement.value\
			and (not useCat or currentPlayer == Player.Humans):
			vn += 1
		if vn==Phase.DogMovement.value\
			and (not useDog or currentPlayer == Player.Humans):
			vn += 1
		nextRonud=False
		if vn==Phase.NumberOfPhases.value:
			vn = Phase.RemoveExpiredCards
			nextRonud=True
		return Phase(vn), nextRonud

class GameState:
	@classmethod
	def init(cls):
		cls.PhaseToLegalMoves = { Phase.RemoveExpiredCards  : cls.getLegalMoves_in_removeExpiredCards,
								  Phase.CatMovement         : cls.getLegalMoves_in_CatMovement,
								  Phase.DogMovement         : cls.getLegalMoves_in_DogMovement,
								  Phase.OtherCardMovement   : cls.getLegalMoves_in_Movement,
								  Phase.TakeNewCards        : cls.getLegalMoves_in_takeNewCards,
								  Phase.DiscardCard         : cls.getLegalMoves_in_DiscardCard,
								  Phase.Play1stCard         : cls.getLegalMoves_in_PlayCard,
								  Phase.Play2ndCard         : cls.getLegalMoves_in_PlayCard,
								  Phase.Play3rdCard         : cls.getLegalMoves_in_PlayCard }
		cls.PhaseToExec       = { Phase.RemoveExpiredCards  : cls.execMove_in_removeExpiredCards,
								  Phase.CatMovement         : cls.execMove_in_Movement,
								  Phase.DogMovement         : cls.execMove_in_Movement,
								  Phase.OtherCardMovement   : cls.execMove_in_Movement,
								  Phase.TakeNewCards        : cls.execMove_in_takeNewCards,
								  Phase.DiscardCard         : cls.execMove_in_DiscardCard,
								  Phase.Play1stCard         : cls.execMove_in_PlayCard,
								  Phase.Play2ndCard         : cls.execMove_in_PlayCard,
								  Phase.Play3rdCard         : cls.execMove_in_PlayCard }
	@staticmethod
	def createRandom():
		zombiesDeck = list(deepcopy(ZombiesCards))
		shuffle(zombiesDeck)
		humansDeck  = list(deepcopy(HumansCards))
		shuffle(humansDeck)
		return GameState.create(zombiesDeck, humansDeck)

	@staticmethod
	def create(zombiesDeck, humansDeck):
		gs = GameState()
		gs.board = Board()
		gs.isDogInPlay = False
		gs.isCatInPlay = False
		gs.cemetary = []
		gs.barricade = []
		gs.thrash = []
		gs.zombiesDeck = zombiesDeck
		gs.humansDeck = humansDeck
		gs.player = Player.Zombies
		gs.phase = Phase.TakeNewCards
		gs.roundNo = 1
		gs.deck = gs.zombiesDeck
		gs.hand = gs.cemetary
		return gs

	def nextPhase(self):
		self.phase,nextPlayer = Phase.next(self.phase, self.player, self.isCatInPlay, self.isDogInPlay)
		if nextPlayer:
			self.roundNo += 1
			self.player = Turn.next(self.player)
			if self.player == Player.Zombies:
				self.deck = self.zombiesDeck
				self.hand = self.cemetary
			else:
				self.deck =  self.humansDeck
				self.hand = self.barricade

	def getLegalMoves(self, player):
		if player != self.player: return [Action(Actions.Noop)]
		return GameState.PhaseToLegalMoves[self.phase](self, player)

	def getNextState(self, player, move):
		ns = deepcopy(self)
		GameState.PhaseToExec[ns.phase](ns, player,move)
		ns.nextPhase()
		return ns

	def getLegalMoves_in_removeExpiredCards(self, player):
		expired = self.board.findExpiredCards(player, self.roundNo)
		return [ Action(Actions.RemoveCards,expired) ]

	def getLegalMoves_in_takeNewCards(self, player):
		numCardsToTake = min( 4 - len(self.hand), len(self.deck) )
		return [ Action(Actions.TakeCards,numCardsToTake) ]

	def getLegalMoves_in_CatMovement(self, player):
		kot = self.board.findCardByType(Kot)
		assert(kot is not None)
		positions = kot.getLegalPositionsAfterMove(self.board)
		return [ Action(Actions.MoveCards, kot, pos)  for pos in positions ]

	def getLegalMoves_in_DogMovement(self, player):
		pies = self.board.findCardByType(Pies)
		assert(pies is not None)
		positions = pies.getLegalPositionsAfterMove(self.board)
		return [ Action(Actions.MoveCards, pies, pos)  for pos in positions ]

	def getLegalMoves_in_Movement(self, player):
		return [ Action(Actions.MoveCards, moveable) ]

	def getLegalMoves_in_DiscardCard(self, player):
		assert(self.hand is not None)
		return [ Action(Actions.DiscardCard, card) for card in self.hand ]

	def getLegalMoves_in_PlayCard(self, player):
		Mv = [ Action(Action.Noop) ]
		for card in self.hand:
			ValidPositions = self.board.getCardValidPositions(card)
			Mv.extend( [ Action(Actions.PlaceCard, c, vp) for vp in ValidPositions] )
		return Mv

	def execMove_in_removeExpiredCards(self, player, move):
		assert(move.action == Actions.RemoveCards)
		Bc = [self.board.removeCard(pos) for pos in move.args]
		for bc in Bc:
			self.thrash.extend( bd.cards )

	def execMove_in_Movement(self, player, move):
		assert(move.action == Actions.MoveCards)
		for pos in move.args:
			self.board.moveCard(pos)

	def execMove_in_takeNewCards(self, player, move):
		self.hand.extend( self.deck[0 : move.args] )
		del self.deck[0 : move.args]

	def execMove_in_DiscardCard(self, player, move):
		 self.hand.remove(move.args)
		 self.thrash.append(move.args)

	def execMove_in_PlayCard(self, player, move):
		pass

class CardTests(unittest.TestCase):
	def test_BulletProofZombie(self):
		z = BulletProofZombie(3)
		self.assertEqual( z.owner, Player.Zombies )
		self.assertTrue( z.isZombie )
		self.assertTrue( z.isMovable )

	def test_Kot(self):
		z = Kot()
		self.assertEqual( z.owner, Player.Zombies )
		self.assertTrue( z.isZombie )
		self.assertTrue( z.isMovable )
		self.assertEqual(z.wounds, 1)

	def test_Czlowiek(self):
		z = Czlowiek()
		self.assertEqual( z.owner, Player.Zombies )
		self.assertFalse( z.canAttachTo( Kot() ))
		self.assertFalse( z.canAttachTo( Pies() ))
		self.assertTrue( z.canAttachTo( BulletProofZombie(3) ))

	def test_compare_zombie_cards(self):
		b1 = BulletProofZombie(3)
		b2 = BulletProofZombie(3)
		self.assertEqual(b1,b2)

	def test_kot_initial_position(self):
		b = Board.create()
		kot =Kot()
		b.placeCard( Zombie(2), Position(0,0), 1 )
		validPositions = kot.getLegalInitialPositions(b)
		expvp = [ Position(0,1), Position(0,2) ]
		for p in validPositions:self.assertIn( p, expvp )
		for p in expvp:	 		self.assertIn( p, validPositions )

	def test_kot_movement(self):
		b = Board.create()
		kot = Kot()
		b.placeCard( kot, 		Position(0,1), 1 )
		b.placeCard( Zombie(2), Position(0,0), 1 )
		b.placeCard( Zombie(2), Position(1,1), 1 )
		b.placeCard( Zapora(), 	Position(4,0), 1 )
		b.placeCard( Zapora(), 	Position(4,1), 1 )
		b.placeCard( Zapora(), 	Position(4,2), 1 )
		vp = list(kot.getLegalPositionsAfterMove(b))
		exp_vp = [ Position(0,0), Position(0,1), Position(0,2), Position(1,1), Position(2,1) ]
		for p in vp: 		self.assertTrue( p in exp_vp )
		for p in exp_vp:	self.assertTrue( p in vp )

class TurnsTests(unittest.TestCase):
	def test1(self):
		p = Phase.RemoveExpiredCards
		pn,nextPlayer = Phase.next(p, Player.Zombies, False,False)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)
		p = Phase.Play3rdCard
		pn,nextPlayer = Phase.next(p, Player.Zombies, False,False)
		self.assertEqual(pn, Phase.RemoveExpiredCards)
		self.assertTrue(nextPlayer)

	def test2(self):
		p = Phase.RemoveExpiredCards
		pn,nextPlayer = Phase.next(p,Player.Zombies, True,False)
		self.assertEqual(pn, Phase.CatMovement)
		self.assertFalse(nextPlayer)

		pn,nextPlayer = Phase.next(pn,Player.Zombies, True,False)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)

		p = Phase.CatMovement
		pn,nextPlayer = Phase.next(p,Player.Zombies, True,True)
		self.assertEqual(pn, Phase.DogMovement)
		self.assertFalse(nextPlayer)
		pn,nextPlayer = Phase.next(pn,Player.Zombies, False, False)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)

	def test3(self):
		p = Phase.RemoveExpiredCards
		pn,nextPlayer = Phase.next(p,Player.Humans, True,True)
		self.assertEqual(pn, Phase.OtherCardMovement)
		self.assertFalse(nextPlayer)

class BoardTests(unittest.TestCase):
	def test1(self):
		c1 = Kot()
		c2 = Kot()
		c3 = Kot()
		b = Board.create()
		b.placeCard( c1, Position(0,0), 1)
		b.placeCard( c2, Position(1,1), 1)
		b.placeCard( c3, Position(2,2), 2)
		e = b.findExpiredCards(1)
		self.assertEqual(e, [])
		e = b.findExpiredCards(2)
		self.assertEqual(e, [])
		e = b.findExpiredCards(3)
		self.assertEqual(e, [Position(0,0), Position(1,1)])
		e = b.findExpiredCards(4)
		self.assertEqual(e, [Position(0,0), Position(1,1), Position(2,2)])
		c = b.removeCard(Position(2,2))
		self.assertEqual(c.cards[0],c3)
		c = b.removeCard(Position(1,1))
		self.assertEqual(c.cards[0],c2)
		c = b.removeCard(Position(0,0))
		self.assertEqual(c.cards[0],c1)
		e = b.findExpiredCards(4)
		self.assertEqual(e, [])

	def test2(self):
		b = Board.create()
		b.placeCard(Kot(), Position(4,2), 1)
		c = b.findCardByType(Kot)
		self.assertTrue(c is not None)
		self.assertEqual(type(c),Kot)

		b.removeCard(Position(4,2))
		c = b.findCardByType(Kot)
		self.assertTrue(c is None)

class GameStateTests(unittest.TestCase):
	@classmethod
	def setUpClass(cls):
		GameState.init()

	def test1(self):
		#1st Zombie action after create
		s = GameState.createRandom()
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual( len(Mv), 1 )
		self.assertEqual( Mv[0].action, Actions.TakeCards )
		self.assertEqual( Mv[0].args, 4 )

	def test2(self):
		#1st human action after create
		s = GameState.createRandom()
		Mv = s.getLegalMoves(Player.Humans)
		self.assertEqual( len(Mv), 1 )
		self.assertEqual( Mv[0].action, Actions.Noop )

	def test3(self):
		#TakeCard action
		s = GameState.createRandom()
		self.assertEqual(s.hand,[])
		nc = len(s.deck)
		tod = s.deck[0:4]
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual(Mv[0].action, Actions.TakeCards)
		ns = s.getNextState(Player.Zombies, Mv[0])
		#check if s does not changes
		self.assertEqual(s.phase, Phase.TakeNewCards)
		self.assertEqual(s.hand, [])
		self.assertEqual(len(s.deck), nc)
		#check ifns is next phase
		self.assertEqual(ns.hand, tod)
		self.assertEqual(len(ns.deck), nc-4)
		self.assertEqual(ns.phase, Phase.DiscardCard)

	def test4(self):
		#DiscardCard action
		s = GameState.createRandom()
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual(Mv[0].action, Actions.TakeCards)
		s = s.getNextState(Player.Zombies, Mv[0])
		Mv = s.getLegalMoves(Player.Zombies)
		self.assertEqual(len(Mv),4)
		for m,i in zip(Mv,count(0)):
			self.assertEqual( m.action, Actions.DiscardCard )
			self.assertEqual( m.args, s.hand[i] )
		ns = s.getNextState(Player.Zombies, Mv[0])
		self.assertTrue( Mv[0].args not in ns.hand )
		self.assertTrue( Mv[0].args in ns.thrash )
		for i in range(1,4):
			c = Mv[i].args
			self.assertTrue( c in ns.hand )
			self.assertTrue( c not in ns.thrash )
		self.assertEqual(ns.phase, Phase.Play1stCard)

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


import os
import sys

import pygame

from level_builder.level_builder import LevelBuilder
from routes.route import Route
from routes.singleplayer import Singleplayer
from utils.button import Button

class LevelBuilderMenu(Route):

    def __init__(self):
        self.color = (200, 200, 200)
        self.menu_button = Button("MENU", 15, (10, 10, 60, 30), (0, 200, 0), (0, 150, 0))
        self.add_button = Button("ADD NEW", 30, (300, 10, 400, 80), (0, 200, 0), (0, 150, 0))
        self.is_add_active = True
        self.level_buttons = []
        self.del_buttons = []
        filenames = sorted(os.listdir("boards/builder_boards"))
        for i, name in enumerate(filenames):
            self.level_buttons.append(
                Button(name.upper()[:-4], 30, (300, 130 + i * 100, 400, 80), (0, 0, 200), (0, 0, 150))
            )
            self.del_buttons.append(
                Button("X", 30, (750, 150 + i * 100, 50, 40), (200, 0, 0), (150, 0, 0))
            )

    def draw(self, win: pygame.Surface) -> None:
        win.fill(self.color)
        self.menu_button.draw(win)
        self.add_button.draw(win)
        for button in self.level_buttons:
            button.draw(win)
        for del_button in self.del_buttons:
            del_button.draw(win)

    def update_state(self) -> 'Route':
        if len(self.level_buttons) >= 5:
            self.is_add_active = False
            self.add_button.color = (100, 100, 100)
            self.add_button.hover_color = (70, 70, 70)
        else:
            self.is_add_active = True
            self.add_button.color = (0, 200, 0)
            self.add_button.hover_color = (0, 150, 0)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
            if self.menu_button.is_mouse(event):
                from routes.menu import Menu
                return Menu((1000, 700))
            if self.add_button.is_mouse(event) and self.is_add_active:
                maxi = 0
                for button in self.level_buttons:
                    s = button.text.split()
                    maxi = max(int(s[1]), maxi)
                return LevelBuilder(maxi + 1)
            for button in self.level_buttons:
                if button.is_mouse(event):
                    return Singleplayer(button.text.lower())
            for i, del_button in enumerate(self.del_buttons):
                if del_button.is_mouse(event):
                    self.del_level(i, del_button)
        return self

    def del_level(self, i: int, del_butt: Button):
        self.del_level_file(self.level_buttons[i].text.lower())
        self.del_buttons.remove(del_butt)
        del self.level_buttons[i]
        for i, button in enumerate(self.level_buttons):
            if button.rect.y > del_butt.rect.y:
                button.move_y(-100)
                self.del_buttons[i].move_y(-100)

    @staticmethod
    def del_level_file(text: str):
        os.remove(f"boards/builder_boards/{text}.txt")


'''
This script sends multimple frames
This example was found at:
https://www.openlighting.org/ola/developer-documentation/python-api/

'''
import array
from ola.ClientWrapper import CLientWrapper

wrapper = None
i = 0
TICK_INTERVAL = 100 #100 ms

def DmxSent(state):
    if not state.Succeeded():
        wrapper.Stop()

def SendFMXFrame():
    #Schedule a function call in 100ms
    #We do this first in case the frame computation takes a long time.
    wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)

    #Here is where we compute our frames
    data = array.array('B')
    global i
    data.append(i % 255)
    i += 1

    #Now send that frame
    wrapper.Client().SendDmx(1, data, DmxSent)

wrapper = ClientWrapper()
wrapper.AddEvent(TICK_INTERVAL, SendDMXFrame)
wrappper.Run()


import sys

names = list(sys.stdin.readline().rstrip())
name_cnt = [0 for _ in range(26)]
for name in names:
  name_cnt[ord(name) - 65] += 1

odd = 0
odd_answer = ""
answer = ""
for i in range(26):
  if name_cnt[i] % 2 == 1:
    odd += 1
    odd_answer += chr(i + 65)
  answer += chr(i + 65) * (name_cnt[i] // 2)

if odd > 1:
  print("I'm Sorry Hansoo")
else:
  print(answer + odd_answer + answer[::-1])

백준/Silver/1213. 팰린드롬 만들기/팰린드롬 만들기.py


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from deal import Deal
from tuangou import Tuangou

class Tongcheng(Tuangou):

    def __init__(self):
        #cityID=2 means beijing
        api = "http://open.t.58.com/api/products?city=bj"
        n = "58tuan"
        chname = '58同城团'
        Tuangou.__init__(self, api=api, n=n, chname=chname)

    def parseDeals(self):

        assert self.json

        items = self.json['urlset']['url']

        for item in items:

            url = item['loc']
            item = item['data']['display']

            d = {
                'title': item['title'],
                'price': item['price'],
                'value': item['value'],
                'cate': item['category'],
                'subcate': item['tag'],
                'url': url,
                'time': item["startTime"],
            }

            if self.timestamp > int(d['time']):
                continue

            deal = Deal().create_item(d)
            if len(deal.title) * len(deal.cate) * len(deal.subcate) == 0:
                continue

            self.deals.append(deal)

if __name__ == '__main__':
    Tongcheng().start()


# ##### BEGIN GPL LICENSE BLOCK #####
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2
#  of the License, or (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software Foundation,
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####

import bpy
import parser
import mathutils
from mathutils import Vector
from bpy.props import StringProperty, BoolProperty, EnumProperty
from sverchok.node_tree import SverchCustomTreeNode, StringsSocket, VerticesSocket, \
    MatrixSocket
from sverchok.data_structure import (updateNode, Vector_generate, SvSetSocketAnyType,
                            SvGetSocketAnyType, match_short, match_long_repeat)

class SvRayCastNode(bpy.types.Node, SverchCustomTreeNode):
    ''' RayCast Object '''
    bl_idname = 'SvRayCastNode'
    bl_label = 'raycast'
    bl_icon = 'OUTLINER_OB_EMPTY'

    modes = [
        ("Object",   "object_space",   "", 1),
        ("World",   "world_space",   "", 2),
    ]

    Itermodes = [
        ("match_short",   "match_short",   "", 1),
        ("match_long_repeat",   "match_long_repeat",  "", 2),
    ]

    Modes = EnumProperty(name="Raycast modes", description="Raycast modes",
                         default="Object", items=modes, update=updateNode)
    Iteration = EnumProperty(name="iteration modes",
                             description="Iteration modes",
                             default="match_short", items=Itermodes,
                             update=updateNode)
    formula = StringProperty(name='formula',
                             description='name of object to operate on \
                             ("object_space" mode only)', default='Cube',
                             update=updateNode)

    def draw_buttons(self, context, layout):
        if self.Modes == 'Object':
            layout.prop(self, "formula", text="")
        row = layout.row(align=True)
        layout.prop(self, "Modes", "Raycast modes")
        layout.prop(self, "Iteration", "Iteration modes")

    def sv_init(self, context):
        self.inputs.new('VerticesSocket', 'start', 'start')
        self.inputs.new('VerticesSocket', 'end', 'end')
        self.outputs.new('VerticesSocket', "HitP", "HitP")
        self.outputs.new('VerticesSocket', "HitNorm", "HitNorm")
        self.outputs.new('StringsSocket', "INDEX/Succes", "INDEX/Succes")

    def process(self):
        start_links = self.inputs['start'].links
        if not (start_links and (type(start_links[0].from_socket) ==
                VerticesSocket)):
            return

        end_links = self.inputs['end'].links
        if not (end_links and (type(end_links[0].from_socket) ==
                VerticesSocket)):
            return

        SSSAT = SvSetSocketAnyType
        bcsrc = bpy.context.scene.ray_cast
        outputs = self.outputs
        out = []
        OutLoc = []
        OutNorm = []
        INDSucc = []
        OutMatrix = []
        ObjectID = []

        st = Vector_generate(SvGetSocketAnyType(self, self.inputs['start']))
        en = Vector_generate(SvGetSocketAnyType(self, self.inputs['end']))
        start = [Vector(x) for x in st[0]]
        end = [Vector(x) for x in en[0]]
        if self.Iteration == 'match_short':
            temp = match_short([start, end])
            start, end = temp[0], temp[1]
        if self.Iteration == 'match_long_repeat':
            temp = match_long_repeat([start, end])
            start, end = temp[0], temp[1]

        if self.Modes == 'Object' and (self.formula in bpy.data.objects):

            if 'data.object' in outputs:
                outputs.remove(outputs['data.object'])

            if 'hited object matrix' in outputs:
                outputs.remove(outputs['hited object matrix'])

            obj = bpy.data.objects[self.formula]
            i = 0
            while i < len(end):
                out.append(obj.ray_cast(start[i], end[i]))
                i = i+1
            for i in out:
                OutNorm.append(i[1][:])
                INDSucc.append(i[2])
                OutLoc.append(i[0][:])

        if self.Modes == 'World':

            if 'data.object' not in outputs:
                outputs.new("VerticesSocket", "data.object")
            if 'hited object matrix' not in outputs:
                outputs.new("MatrixSocket", "hited object matrix")

            for i, last in enumerate(end):
                src = bcsrc(start[i], last)
                OutLoc.append(src[3][:])
                OutNorm.append(src[4][:])
                INDSucc.append(src[0])
                OutMatrix.append(src[2][:])
                OutMatrix = [[a[:], b[:], c[:], d[:]] for a, b, c, d in
                             OutMatrix]
                ObjectID.append(src[1])

        if outputs['HitP'].links:
            SSSAT(self, 'HitP', [OutLoc])
        if outputs['HitNorm'].links:
            SSSAT(self, 'HitNorm', [OutNorm])
        if outputs['INDEX/Succes'].links:
            SSSAT(self, 'INDEX/Succes', [INDSucc])
        if 'hited object matrix' in outputs:
                if outputs['hited object matrix'].links:
                    SSSAT(self, 'hited object matrix', OutMatrix)
        if 'data.object' in outputs:
                if outputs['data.object'].links:
                    SSSAT(self, 'data.object', [ObjectID])

    def update_socket(self, context):
        self.update()

def register():
    bpy.utils.register_class(SvRayCastNode)

def unregister():
    bpy.utils.unregister_class(SvRayCastNode)


# File to Determine occurrences of Corporate Group Term Sets.
# Will follow overall flow (and likely borrow logic) from stakeholder group term sets.

# The core logic for each file:
#   * Load same files as stakeholders
#   * Break into sentences
#   * For each sentence:
#     - Save occurrence of "we"
#     - Save occurrence of "company"
#     - Save occurrence of Ticker
#     - If it is a per-company term, test for following before adding:
#        ~ Does it overlap at all with a product NER tag
#        ~ Does "financ" or "foundation" follow within a couple of words
#        ~ Does company follow within a couple of words
#   * Make format comparable with stakeholders
#   * Just get it working and then we can refine terms.

import stanza # for nlp
import sys # to detect OS platform
import os # to navigate file system and load files
import csv # for working with CSV files
from datetime import datetime  # for console-logging

CorpTickerList = ['A', 'ABC', 'ABT', 'ADBE', 'ADI', 'ADM', 'ADP', 'ADSK', 'AEE', 'AEP', 'AES', 'AFL', 'AIG', 'AIV',
                  'ALL', 'AMAT', 'ANTM', 'APA', 'APD', 'AVY', 'AXP', 'AZO', 'BA', 'BAC', 'BAX', 'LB', 'BDX', 'BEN',
                  'BFB', 'BIIB', 'BK', 'BLL', 'BMY', 'BSX', 'C', 'CAG', 'CAH', 'CAT', 'CCL', 'CI', 'CINF', 'CL',
                  'CLX', 'CMA', 'CMCSA', 'CMI', 'CMS', 'CNP', 'COF', 'COP', 'COST', 'CPB', 'CSCO', 'CSX', 'CTAS',
                  'CTXS', 'CVS', 'CVX', 'D', 'DE', 'DGX', 'DHR', 'DIS', 'DOV', 'DRI', 'DTE', 'DUK', 'DVN', 'EA',
                  'EBAY', 'ECL', 'ED', 'EFX', 'EIX', 'EMN', 'EMR', 'EOG', 'EQR', 'ETFC', 'ETR', 'EXC', 'F', 'FCX',
                  'FDX', 'FE', 'FISV', 'FITB', 'GD', 'GE', 'GILD', 'GIS', 'GLW', 'GPC', 'GPS', 'GS', 'GWW', 'HAL',
                  'HAS', 'HBAN', 'HD', 'HES', 'HIG', 'HOG', 'HON', 'HPQ', 'HRB', 'HUM', 'IBM', 'IFF', 'INTC', 'INTU',
                  'IP', 'IPG', 'ITW', 'JNJ', 'JPM', 'JWN', 'K', 'KEY', 'KLAC', 'KMB', 'KO', 'KR', 'KSS', 'L', 'LEG',
                  'LH', 'LLY', 'LMT', 'LNC', 'LOW', 'LUMN', 'LUV', 'M', 'MAR', 'MAS', 'MCD', 'MCK', 'MCO', 'MET', 'MKC',
                  'MMC', 'MMM', 'MO', 'MRK', 'MRO', 'MS', 'MSFT', 'MSI', 'MTB', 'NEE', 'NEM', 'NI', 'NKE', 'NOC',
                  'NSC', 'NTAP', 'NTRS', 'NUE', 'NVDA', 'NWL', 'OMC', 'OXY', 'PAYX', 'PCAR', 'PEG', 'PEP', 'PFE',
                  'PFG', 'PG', 'PGR', 'PH', 'PHM', 'PKI', 'PLD', 'PNC', 'PNW', 'PPG', 'PPL', 'PRU', 'QCOM', 'RF',
                  'RHI', 'ROK', 'RTX', 'SBUX', 'SCHW', 'SEE', 'SHW', 'SLB', 'SNA', 'SO', 'SPG', 'SPGI', 'SRE', 'STT',
                  'SWK', 'SYK', 'SYY', 'T', 'TGT', 'TIF', 'TJX', 'TMO', 'TROW', 'TRV', 'TXN', 'TXT', 'UNH', 'UNM',
                  'UNP', 'UPS', 'USB', 'VLO', 'VMC', 'VZ', 'WAT', 'WFC', 'WHR', 'WM', 'WMB', 'WMT', 'WY', 'XEL',
                  'XLNX', 'XOM', 'XRX', 'YUM', 'ZBH', 'ZION']

BaseCorpTermsList = ["we ", "We ", " company"]
CorpTermsDic = {}
MatchList = []
FailList = []

start_time = datetime.now()
print (sys.platform, start_time)

if sys.platform.startswith('darwin'):
    onedrive_base_dir = '/Users/moranmarkd/Library/CloudStorage/OneDrive-Personal'
elif sys.platform.startswith('linux'):
    onedrive_base_dir = '/home/mark/OneDrive'
else:
    exit_string = "sys.platform=" + sys.platform + " not a valid choice."
    exit(exit_string)

# Directory where letters are stored
#BaseLetterDir = onedrive_base_dir + '/Academics/Illinois/Dissertation/Paper1.1/Letters/09-CL-txt-test/'
BaseLetterDir = onedrive_base_dir + '/Academics/Illinois/Dissertation/Paper1.1/Letters/09-CL-txt/'

# Build dictionary of custom terms per ticker.
CorpTermsFileName = onedrive_base_dir + '/Academics/Illinois/Dissertation/Paper1.3/Config/CorporateGroupTermSet.csv'
with open(CorpTermsFileName, encoding='utf-8') as corp_terms_file:
    corp_terms_csv = csv.reader(corp_terms_file)
    for row in corp_terms_csv:
        CorpTermsDic[row[0]] = row
    for tkr in CorpTickerList:
        if CorpTermsDic[tkr][-1] == '':
            CorpTermsDic[tkr].pop()
        CorpTermsDic[tkr] = BaseCorpTermsList + CorpTermsDic[tkr][1:]
        print (tkr, CorpTermsDic[tkr])

# out_file for results. Just append.
out_file = onedrive_base_dir  + '/Academics/Illinois/Dissertation/Paper1.3/Out/CorpTerm_List_v1.csv'

# See if log file exist. If it does, load its content into a list. If not, create a new empty one.
log_file = onedrive_base_dir + '/Academics/Illinois/Dissertation/Paper1.3/Logs/CorpTerm_Log_v1.txt'
dfl = []  # done files list
if os.path.exists(log_file):  # Log file exists
    fl = open(log_file, 'r', encoding='latin1')
    done_files_list = fl.readlines()
    for i in done_files_list:
        dfl.append(i.strip('\n'))
    print(dfl)
else:  # No log file...make a new one.
    fl = open(log_file, 'w')
    fl.write('Files Complete\n') # Do I really need that line?
    fl.close()

# Build List of Letters to work through
LettersList = []
fnl = os.listdir(BaseLetterDir)
for fn in fnl:
    if fn.lower().endswith('txt'):
        LettersList.append(fn)

print (LettersList)

# Check for outfile. Build header if it does not exist.
out_list = []
out_header = ['Letter_File_Name', 'Sentence_Token_Count', 'Sent_ID', 'Total_Letter_Sentences', 'Specific_Term',
              'Ticker', 'Sentence_Text']
if not os.path.exists(out_file):
    fo = open(out_file, "w")
    fo_csv = csv.writer(fo)
    fo_csv.writerow(out_header)
    fo.close()

# Load up Stanza and turn documents into sentences.
stanza.download('en')
nlp = stanza.Pipeline(lang='en', processors='tokenize,ner')

# Functions
def exception_test(co_term, test_str, sent_text, test_range=15):
    except_flag = True
#    print ("Hello from here:", co_term, test_str, sent_text, test_range)
    if test_str in sent_text:
#        print ("Testing...")
        if 0 < sent_text.find(test_str) - sent.text.find(co) < test_range:
#            print("Don't count:", test_str, sent_text.find(test_str) - sent_text.find(co_term))
            except_flag = False
#            print ('Match ==> False')
#        print (except_flag)
    return except_flag

# Just some accounting variables
match_count = 0
fail_count = 0
total_count = 0

# Main loop. Based on FindStakeholderSentences1.2xl.py

j = 0
for ltr in LettersList:
    tkr = ltr.split('-')[0]
    if ltr in dfl:
        print(ltr, "in", dfl)
    else:
        print(ltr, "not in", dfl)
        dfl.append(ltr)
        j = j + 1
        full_infile_path = BaseLetterDir + ltr

        inputfile = open(full_infile_path, mode='r', encoding='utf-8', errors='ignore')
        text1 = inputfile.read()
#        text2 = text1.replace("’", "'")
#        if text1 != text2:
#            print ("Apostrophe changes made.")
#        print (text1)
        print(j, '--------------------', datetime.now(), ltr, tkr, '-----------------------')
        doc = nlp(text1)
        for i, sent in enumerate(doc.sentences):
            print ("----------", i, "----------")
            print (i, sent.text)
            # From here, start recursing through and finding matches.
            for co in CorpTermsDic[tkr]:
                match_flag = False
#                print (co, CorpTermsDic[tkr].index(co))
                if co in sent.text and "company's" not in sent.text and "company’s" not in sent.text and len(sent.words) >= 6:
                    print (co, "is a tentative match:", i, sent.text.find(co), sent.text)
                    match_flag = True
                    # Okay...if I have a match, I need to work through the exceptions.
                    if CorpTermsDic[tkr].index(co) > 2:
                        # Test for strings that would dis-qualify the use of the corp_term
                        match_flag = exception_test(co, 'Financ', sent.text)
                        match_flag = exception_test(co, 'Foundation', sent.text)
                        match_flag = exception_test(co, 'Credit', sent.text)
                        if tkr != 'MCO':
                            match_flag = exception_test(co, "'s", sent.text)
                            match_flag = exception_test(co, "’s", sent.text)
                        # Now the logic to test if there is a product NER.
#                        print(*[f'entity: {ent.text}\ttype: {ent.type}' for ent in sent.ents], sep='\n')
                        for ent in sent.ents:
#                            print (ent.type, ent.text)
                            if ent.type == 'PRODUCT':
                                print ("product:", ent.type, ent.text)
                                if ent.start_char - sent.text.find(co) < 15:
                                    print ("Product reference. Don't count:", ent.start_char - sent.text.find(co))
                                    match_flag = False
                    if match_flag == True:
                        match_count = match_count + 1
                        total_count = total_count + 1
                        print ("Confirmed match:", ltr, i, co, sent.text.find(co))
                        MatchList.append([i, total_count, match_count, sent.text, co, match_flag])
                    else:
                        fail_count = fail_count + 1
                        total_count = total_count + 1
                        print ("No match:", ltr, i, co)
                        FailList.append([i, total_count, match_count, sent.text, co, match_flag])
                    print(i, total_count, match_count, fail_count, sent.text, co, match_flag)
                    print(ltr, ':', len(sent.tokens), ';', i, '/', len(doc.sentences), '.', co, '/', co, '(', tkr, ')',
                          ':', sent.text)
                    out_list.append([ltr, len(sent.tokens), i, len(doc.sentences), co, tkr, sent.text])
            print(i, j, ltr, tkr, datetime.now(), match_count, fail_count, total_count)

        fo = open(out_file, "a")
        fo_csv = csv.writer(fo)
        fo_csv.writerows(out_list)
        fo.close()
        out_list = []
# Append file name to log file
        fl = open(log_file, 'a')
        fl.write(ltr)
        fl.write('\n')
        fl.close()

print('Matches:')
for r in MatchList:
    print(r)

print('No Matches:')
for r in FailList:
    print(r)

#print (out_list)
#print (len(out_list))
end_time = datetime.now()
print ('elapsed time:', end_time - start_time)


import numpy as np

####################### FUNCTIONS #####################

def importdata(jobid, timecompression):
    global Wpt, xFieldCentres, FieldCentres, FieldSeparation, FieldSizes, SystemsMatch, MeanChange, TRin, Currentiteration, Weightiteration
    Wpt = np.load('../../RetinotopicMapsData/%s/Weightmatrix.npy' % ('{0:04}'.format(jobid)))
    xFieldCentres = np.load('../../RetinotopicMapsData/%s/xFieldCentres.npy' % ('{0:04}'.format(jobid)))
    TRin = np.load('../../RetinotopicMapsData/%s/PrimaryTR.npy' % ('{0:04}'.format(jobid)))

    FieldCentres = np.zeros(
        [2, int(len(Wpt[:, 0, 0, 0, 0]) / timecompression), len(Wpt[0, :, 0, 0, 0]), len(Wpt[0, 0, :, 0, 0])])
    FieldSizes = np.zeros(
        [int(len(Wpt[:, 0, 0, 0, 0]) / timecompression), len(Wpt[0, :, 0, 0, 0]), len(Wpt[0, 0, :, 0, 0])])

    Currentiteration = -1
    Weightiteration = -1

def updatetimepoint(timecompression):
    global Currentiteration, Weightiteration
    Currentiteration += 1
    Weightiteration += timecompression

def UpdateFieldCentres():
    synapses = np.array(np.nonzero(Wpt[Weightiteration, :, :, :, :]))

    totaldim1 = np.zeros([len(Wpt[0, :, 0, 0, 0]), len(Wpt[0, 0, :, 0, 0])])
    totaldim2 = np.zeros([len(Wpt[0, :, 0, 0, 0]), len(Wpt[0, 0, :, 0, 0])])
    weightsumdim1 = np.zeros([len(Wpt[0, :, 0, 0, 0]), len(Wpt[0, 0, :, 0, 0])])
    weightsumdim2 = np.zeros([len(Wpt[0, :, 0, 0, 0]), len(Wpt[0, 0, :, 0, 0])])

    for synapse in range(int(len(synapses[0, :]))):
        tdim1 = synapses[0, synapse]
        tdim2 = synapses[1, synapse]
        rdim1 = synapses[2, synapse]
        rdim2 = synapses[3, synapse]
        weightsumdim1[tdim1, tdim2] += Wpt[Weightiteration, tdim1, tdim2, rdim1, rdim2]
        weightsumdim2[tdim1, tdim2] += Wpt[Weightiteration, tdim1, tdim2, rdim1, rdim2]
        totaldim1[tdim1, tdim2] += rdim1 * Wpt[Weightiteration, tdim1, tdim2, rdim1, rdim2]
        totaldim2[tdim1, tdim2] += rdim2 * Wpt[Weightiteration, tdim1, tdim2, rdim1, rdim2]

    FieldCentres[0, Currentiteration, :, :] = totaldim1 / weightsumdim1
    FieldCentres[1, Currentiteration, :, :] = totaldim2 / weightsumdim2
    FieldCentres[:, Currentiteration, :, :] = np.nan_to_num(FieldCentres[:, Currentiteration, :, :])

def UpdateFieldSizes():
    for tdim1 in range(len(Wpt[0, :, 0, 0, 0])):
        for tdim2 in range(len(Wpt[0, 0, :, 0, 0])):
            area = 0
            synapses = np.array(np.nonzero(Wpt[Weightiteration, tdim1, tdim2, :, :]))
            for dim1 in np.unique(synapses[0, :]):
                area += max(synapses[1, synapses[0, :] == dim1]) - min(synapses[1, synapses[0, :] == dim1])

            for dim2 in np.unique(synapses[1, :]):
                area += max(synapses[0, synapses[1, :] == dim2]) - min(synapses[0, synapses[1, :] == dim2])

            diameter = 2 * np.sqrt(area / (2 * np.pi))
            FieldSizes[Currentiteration, tdim1, tdim2] = diameter

def savedata(JobID, Timecompression):
    np.save('../../RetinotopicMapsData/%s/FieldCentres' % ('{0:04}'.format(JobID)), FieldCentres)
    np.save('../../RetinotopicMapsData/%s/FieldSizes' % ('{0:04}'.format(JobID)), FieldSizes)
    np.save('../../RetinotopicMapsData/%s/SecondaryTR' % ('{0:04}'.format(JobID)), TRin * Timecompression)


"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='alix',
    version='0.5',
    description='Alix the tiny micro services for Python',
    long_description=long_description,
    url='https://github.com/boudartjj/alix',
    author='Jean-Jacques Boudart',
    author_email='jean-jacques@boudart.eu',
    license='MIT',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3.6',
    ],
    keywords='microservices framework',
    packages=['alix'],
    install_requires=['redis'],
)


import json
from functools import partial
from pathlib import Path

import numpy as np
import pytest

from tests.algorithm_validation_tests.helpers import get_test_params

assert_allclose = partial(np.testing.assert_allclose, rtol=1e-2, atol=1e-2)
fit_exp = Path(__file__).parent / "expected" / "naive_bayes_gauss_fit.json"
pred_exp = Path(__file__).parent / "expected" / "naive_bayes_gauss_predict.json"
cv_inputs = Path(__file__).parent / "expected" / "naive_bayes_gauss_cv.json"

def get_test_inputs(file):
    with file.open() as f:
        params = json.load(f)["test_cases"]
    return [p["input"] for p in params]

class TestGaussianNB:
    @pytest.mark.parametrize("test_input, expected", get_test_params(fit_exp))
    def test_fit__class_count(self, test_input, expected, get_algorithm_result):
        result = get_algorithm_result("test_nb_gaussian_fit", test_input)
        assert result["class_count"] == expected["class_count"]

    @pytest.mark.parametrize("test_input, expected", get_test_params(fit_exp))
    def test_fit__theta(self, test_input, expected, get_algorithm_result):
        result = get_algorithm_result("test_nb_gaussian_fit", test_input)
        assert_allclose(result["theta"], expected["theta"])

    @pytest.mark.parametrize("test_input, expected", get_test_params(fit_exp))
    def test_fit__var(self, test_input, expected, get_algorithm_result):
        result = get_algorithm_result("test_nb_gaussian_fit", test_input)
        assert_allclose(result["var"], expected["var"])

    @pytest.mark.parametrize("test_input, expected", get_test_params(pred_exp))
    def test_predict(self, test_input, expected, get_algorithm_result):
        result = get_algorithm_result("test_nb_gaussian_predict", test_input)
        assert result["predictions"] == expected["predictions"]

    @pytest.mark.parametrize("test_input", get_test_inputs(cv_inputs))
    def test_cv__confusion_matrix(self, test_input, get_algorithm_result):
        result = get_algorithm_result("naive_bayes_gaussian_cv", test_input)
        confmat = np.array(result["confusion_matrix"]["data"])
        assert (0 <= confmat).all()

    @pytest.mark.parametrize("test_input", get_test_inputs(cv_inputs))
    def test_cv__accuracy(self, test_input, get_algorithm_result):
        result = get_algorithm_result("naive_bayes_gaussian_cv", test_input)
        accuracy = result["classification_summary"]["accuracy"]
        self._assert_all_between_0_and_1(accuracy)

    @pytest.mark.parametrize("test_input", get_test_inputs(cv_inputs))
    def test_cv__precision(self, test_input, get_algorithm_result):
        result = get_algorithm_result("naive_bayes_gaussian_cv", test_input)
        precision = result["classification_summary"]["precision"]
        self._assert_all_between_0_and_1(precision)

    @pytest.mark.parametrize("test_input", get_test_inputs(cv_inputs))
    def test_cv__recall(self, test_input, get_algorithm_result):
        result = get_algorithm_result("naive_bayes_gaussian_cv", test_input)
        recall = result["classification_summary"]["recall"]
        self._assert_all_between_0_and_1(recall)

    @pytest.mark.parametrize("test_input", get_test_inputs(cv_inputs))
    def test_cv__fscore(self, test_input, get_algorithm_result):
        result = get_algorithm_result("naive_bayes_gaussian_cv", test_input)
        fscore = result["classification_summary"]["fscore"]
        self._assert_all_between_0_and_1(fscore)

    @staticmethod
    def _assert_all_between_0_and_1(quantity):
        for key1, val1 in quantity.items():
            for key2, val2 in val1.items():
                msg = f"{key1}:{key2} is out of bounds"
                assert 0 <= val2 <= 1, msg


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 10 22:02:23 2020

@author: arti
"""

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

titanic = pd.read_csv('titanic.csv')

sns.set_style('darkgrid')

fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)

sns.distplot(titanic['fare'], ax=ax1)
sns.distplot(titanic['fare'], hist=False, ax=ax2)
sns.distplot(titanic['fare'], kde=False, ax=ax3)

ax1.set_title('titanic fare - hist/ked')
ax2.set_title('titanic fare - ked')
ax3.set_title('titanic fare - hist')

plt.show()


from enum import Enum, auto
import torch
from typing import Tuple
import warnings

class TensorLayout(Enum):
    Conv = auto()
    FC = auto()

class DataType(Enum):
    Spike = auto()
    Dense = auto()

class MetaTensor:
    def __init__(self,
                 tensor: torch.Tensor,
                 tensor_layout: TensorLayout,
                 data_type: DataType):
        self._data = tensor
        self._tensor_layout = tensor_layout
        self._data_type = data_type

        if self.hasFCLayout():
            assert tensor.shape[2:4] == (1, 1)

    def getTensor(self):
        return self._data.detach()

    def getMeanNumSpikesPerNeuron(self):
        """
        :return: For each sample in the batch, the average number of spikes per neuron in the given time.
                 Returns None if data does not contain spikes.
        """
        if not self.isSpikeType():
            warnings.warn("Data does not contain spikes. Returning None.", Warning)
            return None
        if self.hasConvLayout():
            return torch.mean(self._data.sum(4), dim=(1, 2, 3)).detach()
        assert self.hasFCLayout()
        return torch.mean(self._data.squeeze().sum(2), dim=1).detach()

    def getSpikeCounts(self):
        """
        :return: For each sample in the batch, return the number of spikes, number of neurons and number of timesteps.
                 Returns None if data does not contain spikes.
        """
        output = dict()
        data = self._data.detach()
        if not self.isSpikeType():
            warnings.warn("Data does not contain spikes. Returning None.", Warning)
            return None
        if self.hasConvLayout():
            num_spikes = torch.sum(data, dim=(1, 2, 3, 4))
            num_neurons = data.shape[1] * data.shape[2] * data.shape[3] * data.shape[4]
            num_steps = data.shape[4]
        else:
            assert self.hasFCLayout()
            data = data.squeeze()  # (batch, neurons, time)
            num_spikes = torch.sum(data, dim=(1, 2)).detach()
            num_neurons = data.shape[1]
            num_steps = data.shape[2]
        output['num_spikes'] = num_spikes
        output['num_neurons'] = num_neurons
        output['num_steps'] = num_steps

        num_spikes_per_neuron = torch.sum(data, dim=-1).long()
        is_spiking = num_spikes_per_neuron >= 1
        num_steps_per_spike_per_neuron = num_steps / num_spikes_per_neuron[is_spiking].float()

        fraction_spiking = torch.sum(is_spiking).float() / num_spikes_per_neuron.numel()

        # steps/(spikes/neuron) as flattened vector over the whole batch:
        output['fraction_spiking'] = fraction_spiking.item()
        # spikes/neuron as flattened vector over the whole batch (only includes spiking neurons):
        output['spikes_per_neuron'] = num_spikes_per_neuron[is_spiking]
        # steps/(spikes/neuron) as flattened vector over the whole batch (only includes spiking neurons):
        output['steps_in_batch'] = num_steps_per_spike_per_neuron

        return output

    def isSpikeType(self):
        return self._data_type == DataType.Spike

    def isDenseType(self):
        return self._data_type == DataType.Dense

    def hasConvLayout(self):
        return self._tensor_layout == TensorLayout.Conv

    def hasFCLayout(self):
        return self._tensor_layout == TensorLayout.FC

class SpikeModule(torch.nn.Module):
    _input_key = 'input'
    _output_key = 'output'

    def __init__(self):
        super().__init__()
        self._data = dict()

    def getMetaTensorDict(self):
        return self._data

    def addMetaTensor(self, key: str, value: MetaTensor):
        assert not key == self._output_key, 'Use addOutputMetaTensor function instead'
        self._data[key] = value

    def addInputMetaTensor(self, value: MetaTensor):
        assert value.isSpikeType()
        assert value.hasConvLayout(), 'Does not have to be but is reasonable for the moment'
        self._data[self._input_key] = value

    def addOutputMetaTensor(self, value: MetaTensor):
        assert value.isDenseType()
        assert value.hasFCLayout(), 'Does not have to be but is reasonable for the moment'
        self._data[self._output_key] = value

def getNeuronConfig(type: str='SRMALPHA',
                    theta: float=10.,
                    tauSr: float=1.,
                    tauRef: float=1.,
                    scaleRef: float=2.,
                    tauRho: float=0.3,  # Was set to 0.2 previously (e.g. for fullRes run)
                    scaleRho: float=1.):
    """
    :param type:     neuron type
    :param theta:    neuron threshold
    :param tauSr:    neuron time constant
    :param tauRef:   neuron refractory time constant
    :param scaleRef: neuron refractory response scaling (relative to theta)
    :param tauRho:   spike function derivative time constant (relative to theta)
    :param scaleRho: spike function derivative scale factor
    :return: dictionary
    """
    return {
        'type': type,
        'theta': theta,
        'tauSr': tauSr,
        'tauRef': tauRef,
        'scaleRef': scaleRef,
        'tauRho': tauRho,
        'scaleRho': scaleRho,
    }


def gradingStudents(grades):
    new_grades = []
    for i in grades:
        if i < 38 or i%5 ==0:
            new_grades.append(i)
        else:
            k = i%5
            k = 5-k
            if k >= 3:
                new_grades.append(i)
            else:
                t = i + k
                new_grades.append(t)
    return new_grades

if __name__ == '__main__':

    grades_count = int(input().strip())
    grades = []
    for _ in range(grades_count):
        grades_item = int(input().strip())
        grades.append(grades_item)
    result = gradingStudents(grades)
    print(result)


#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Developed by Shangchen Zhou <shangchenzhou@gmail.com>
import os
import utils.network_utils as net_utils
from losses.losses import *
from losses.metrics import *

from time import time

def mkdir(path):
    if not os.path.isdir(path):
        mkdir(os.path.split(path)[0])
    else:
        return
    os.mkdir(path)

def val(cfg, epoch_idx, val_data_loader, net, val_writer):
    Init_Epoch = epoch_idx

    n_batches = len(val_data_loader)

    # Batch average meterics
    batch_time = net_utils.AverageMeter()
    data_time = net_utils.AverageMeter()
    PSNRs = net_utils.AverageMeter()

    batch_end_time = time()

    for batch_idx, (_, img_lr_s, img_lr, img_hr) in enumerate(val_data_loader):
        data_time.update(time() - batch_end_time)
        # Switch models to validation mode
        net.eval()

        with torch.no_grad():
            # Get data from data loader
            img_lr_s, img_lr, img_hr = [net_utils.var_or_cuda(img) for img in [img_lr_s, img_lr, img_hr]]

            if cfg.TEST.CHOP:
                img_out = net_utils.Chop(model=net)(img_lr_s, img_lr)
            else:
                img_out = net(img_lr_s, img_lr)

            if batch_idx < cfg.VAL.VISUALIZATION_NUM:
                if epoch_idx == Init_Epoch:
                    img_lr_cpu = img_lr[0].cpu()/255.
                    img_hr_cpu = img_hr[0].cpu()/255.
                    val_writer.add_image(cfg.CONST.NAME+'/IMG_LR' + str(batch_idx + 1), img_lr_cpu, epoch_idx + 1)
                    val_writer.add_image(cfg.CONST.NAME+'/IMG_HR' + str(batch_idx + 1), img_hr_cpu, epoch_idx + 1)

                img_out_cpu = img_out[0].cpu().clamp(0,cfg.DATA.RANGE)/255.
                val_writer.add_image(cfg.CONST.NAME+'/IMG_OUT'+str(batch_idx+1), img_out_cpu, epoch_idx+1)

            img_out = net_utils.tensor2img(img_out[0], min_max = [0, cfg.DATA.RANGE])
            img_hr  = net_utils.tensor2img(img_hr[0], min_max = [0, cfg.DATA.RANGE])

            img_out, img_hr = net_utils.crop_border([img_out, img_hr], cfg.CONST.SCALE)

            img_out_y = net_utils.bgr2ycbcr(img_out / 255., only_y=True)
            img_hr_y = net_utils.bgr2ycbcr(img_hr / 255., only_y=True)

            PSNR = calculate_psnr(img_out_y * 255, img_hr_y * 255)
            PSNRs.update(PSNR, cfg.CONST.VAL_BATCH_SIZE)

            batch_time.update(time() - batch_end_time)
            batch_end_time = time()

            if (batch_idx + 1) % cfg.VAL.PRINT_FREQ == 0:
                print('[VAL] [Epoch {0}/{1}][Batch {2}/{3}]\t BT {4}\t DT {5}\t PSNR {6}'
                    .format(epoch_idx + 1, cfg.TRAIN.NUM_EPOCHES, batch_idx + 1, n_batches, batch_time, data_time, PSNRs))

    # Add validation results to TensorBoard
    val_writer.add_scalar(cfg.CONST.NAME+'/PSNR_VAL', PSNRs.avg, epoch_idx + 1)

    print('============================ RESULTS ===========================')
    print('[VAL] Average_PSNR: ' + str(PSNRs.avg))
    print('[VAL] [Epoch {0}] BatchTime_avg {1} DataTime_avg {2} PSNR_avg {3}\n'
          .format(epoch_idx+1, batch_time.avg, data_time.avg, PSNRs.avg))
    return PSNRs.avg


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

"""This example script illustrates how to make a custom likelihood
service in Python.
"""
from __future__ import print_function

import os

import icecube.icetray
import icecube.dataclasses
import icecube.dataio
import icecube.gulliver
import icecube.lilliput.segments
import icecube.phys_services
from I3Tray import I3Tray

class GaussianLiklihood(icecube.gulliver.I3EventLogLikelihood):
    """Example of a simple Likelihood calculation in Python

    The likelihood is a Gaussian with a fixed error.

    """
    def __init__(self, pulses, error):
        super(GaussianLiklihood, self).__init__()
        self.pulse_name = pulses
        self.error = error

    def GetName(self):
        return self.pulse_name

    def SetGeometry(self, geo):
        self.geometry = geo.omgeo

    def SetEvent(self, frame):
        self.pulses = icecube.dataclasses.I3RecoPulseSeriesMap.from_frame(
            frame, self.pulse_name)

    def GetLogLikelihood(self, hypo):
        part = hypo.particle
        L = 0.

        for omkey, series in self.pulses.items():
            pos = self.geometry[omkey].position
            time = series[0].time

            tres = icecube.phys_services.I3Calculator.time_residual(
                part, pos, time)

            L -= (tres / self.error)**2

        return L

    def GetMultiplicity(self):
        return len(self.pulses)

    def GetDiagnostics(self, x):
        return None

filelist = [
    os.path.join(os.environ["I3_TESTDATA"], "GCD", "GeoCalibDetectorStatus_IC86.55697_corrected_V2.i3.gz"),
    os.path.join(os.environ["I3_TESTDATA"], "sim", "Level3_nugen_numu_IC86.2012.011069.000000_20events.i3.bz2")
    ]

pulses = "TWSRTHVInIcePulses"

tray = I3Tray()

tray.Add("I3Reader", FileNameList=filelist)

tray.context["GaussianLiklihood"] = GaussianLiklihood(
    pulses, 15.*icecube.icetray.I3Units.ns)

icecube.lilliput.segments.add_simple_track_parametrization_service(tray)

tray.AddService("I3GSLSimplexFactory", "GSLSimplex")

tray.AddService(
    "I3BasicSeedServiceFactory", "seed",
    InputReadout=pulses,
    FirstGuesses=["LineFit_TWHV"],
    TimeShiftType="TFirst")

tray.Add(
    "I3SimpleFitter",
    OutputName="SPEFitSingle_TWHV_NL",
    SeedService="seed",
    Parametrization="default_simpletrack",
    LogLikelihood="GaussianLiklihood",
    Minimizer="GSLSimplex")

def print_result(frame):
    mctruth = icecube.dataclasses.get_most_energetic_track(frame["I3MCTree"])
    reco = frame["SPEFitSingle_TWHV_NL"]
    print()
    for x in "zenith", "azimuth":
        a = getattr(mctruth.dir, x) / icecube.icetray.I3Units.degree
        b = getattr(reco.dir, x) / icecube.icetray.I3Units.degree
        print("%15s %8.3f %8.3f %8.3f" % (x, a, b, abs(a - b)))

tray.AddModule(print_result)
tray.Execute()


#!/usr/bin/env python

#using built-in function print in python2
from __future__ import print_function

"""
 15-619 Cloud Computing
 Project1.2 Elastic MapReduce
 File: filter_reducer.py
 Name: Huacong Cai
 AndrewID: hcai
 Date: Sep, 13, 2014
"""
""" Reducer

    Aggregate the pageviews from hourly views to daily views from mapper,
    and calculate the total pageviews for each page, print the pages whose
    total views > 100000 with detailed info

    """

import sys

#aggregate page views by date
viewsForDate = dict()

for line in sys.stdin:
    line = line.strip()
    page, date, views = line.split('\t')

    try:
        views = int(views)
    except ValueError:
        continue

    #create new dict item for this date
    if date not in viewsForDate:
        viewsForDate[date] = dict()

    if page in viewsForDate[date]:
        viewsForDate[date][page] += views
    else: #new page record
        viewsForDate[date][page] = views

#aggregate page views by month
totalViews = dict()

for date in viewsForDate:
    for page in viewsForDate[date]:
        if page in totalViews:
            totalViews[page] += viewsForDate[date][page]
        else: #new page record
            totalViews[page] = viewsForDate[date][page]

#print pages info whose views > 10000
for page in totalViews:
    if totalViews[page] > 100000:
        print("%d\t%s\t" %(totalViews[page], page), end='')

        for date in xrange(20140701, 20140732): #print record for each date in ascending order
            try:
                dateStr = str(date)
                print("<%s:%d>\t" %(dateStr, viewsForDate[dateStr][page]), end='')
            except KeyError: #this date do not have record of this page
                continue
        print()