# import eventlet
# eventlet.monkey_patch()

from flask import Flask, request, jsonify, render_template
from flask_socketio import SocketIO, emit
from flask_cors import CORS
import time
import pandas as pd
import numpy as np
from math import sqrt
import heapq
from copy import deepcopy
import json

app = Flask(__name__, static_url_path='', static_folder='front-end/build')
app.config['SECRET_KEY'] = 'secret!'
CORS(app, resources={r"/*": {"origins": "*"}})
socketio = SocketIO(app, cors_allowed_origins="*")

@app.route("/http-call")
def http_call():
    """return JSON with string data as the value"""
    data = {'data': 'This text was fetched using an HTTP call to server on render'}
    return jsonify(data)

@app.route("/")
def index():
    return app.send_static_file('index.html')

@socketio.on("connect")
def connected():
    """event listener when client connects to the server"""
    print(request.sid)
    print("client has connected")
    emit("connect", {"data": f"id: {request.sid} is connected"})

# @socketio.on('dimension')
# def get_dimensions(data):
#     print("data for dimensions from the front end: ",str(data))

@socketio.on('data')
def handle_message(data):
    """event listener when client types a message"""
    print("data from the front end: ", (data))
    wjdata = json.loads(data)
    print("data from the front end wjdata: ", (wjdata))
    # emit("data",{'data':data,'id':request.sid},broadcast=True)
    # recursively call and emit data
    # for i in range(20):
    #     socketio.sleep(0.5)
    #     emit("data",{'data':i,'id':request.sid},broadcast=False)
    #     print(i)

    def create_grid(p, dim):
        '''

    :param p: probability with which a node is blocked
    :param dim: dimension of the matrix
    :return: a 2d list denoting grid where 1 = traversable node, 0 = non traversable

    This function generates a random grid by taking into account probability 'p' and dimension 'dim'
    '''

    # initialise a grid with all 0. 0 = cell blocked, 1 =  cell unblocked
        grid = [[0 for i in range(dim)] for j in range(dim)]

        # Loop over inputted dimension
        for i in range(dim):
            for j in range(dim):
                actual_prob = np.random.random_sample()  # generating a random number
                # if the generated random number > p, assign it 1 (meaning it is
                if actual_prob > p:
                    grid[i][j] = 1  # traversable.
                else:
                    grid[i][j] = 0

        grid[0][0] = 1  # start node and end node is always traversable.
        grid[dim - 1][dim - 1] = 1
        return grid

    class Node:
        '''
        A node class that stores 5 things for a node - position, parent, g(n), h(n), f(n)
        '''

        def __init__(self, parent=None, position=None):
            '''
            This function initalises a node by setting parent , position and heuristic values as 0
            :param parent: parent of the current code
            :param position: position of the current code
            '''
            self.parent = parent
            self.position = position
            self.g = 0
            self.f = 0
            self.h = 0

        def __eq__(self, node):
            '''
            This function is overload for == operator in python. It is used to compare if two nodes are equal.

            :param node: node to compare with
            :return: 1 if two self and node is equal, otherwise 0

            '''
            if (self.position[0] == node.position[0] and self.position[1] == node.position[1]):
                return True
            else:
                return False

        def __lt__(self, other):
            '''
            This function is overload for < operator in python. It is used to compare if one node is less than other.

            :param other: node to compare with
            :return: 1 if self's f value is less than other's f value, otherwise 0

            '''
            return self.f < other.f

    def generate_children(grid, knowledge_grid, fringe, visited_list, current, all_moves, end_node, is_gridknown):
        '''
        This function uses a grid (be it grid or knowledge) and generates all valid children of the current node.

        :param grid: the original actual grid
        :param knowledge_grid: the knowledge grid
        :param fringe: list of nodes in the priority queue
        :param visited_list: a dictionary of nodes already visited
        :param current: current node in the queue
        :param all_moves: array of all valid moves
        :param end_node: end position/node in the grid
        :param is_gridknown: parameter to switch between grid and knowledge grid
        :return: array of relevant children
        '''
        current_x, current_y = current.position
        relevant_children = []
        dim = len(grid)

        for a_move in all_moves:  # looping over all valid moves
            child_x = current_x + a_move[0]
            child_y = current_y + a_move[1]
            # condition to check if node is in within
            if child_x > dim-1 or child_x < 0 or child_y > dim-1 or child_y < 0:
                # boundaries of the grid
                continue
            # initalising children node with current
            children_node = Node(current, (child_x, child_y))
            # as parent and child_x, child_y as position

            if (is_gridknown == "No"):  # isgridknown checks whether to we have grid
                # loaded in the memory, if not we use knowledge
                # grid
                grid_for_pathcalculation = knowledge_grid
            else:
                grid_for_pathcalculation = grid
            # condition to check is current node
            if (grid_for_pathcalculation[child_x][child_y] != 0) and (visited_list.get(children_node.position) != "Added"):
                # is not blocked and current node is
                # not in visited list

                # assigining current g = g(parent) + 1
                children_node.g = current.g + 1
                children_node.h = abs(children_node.position[0] - end_node.position[0]) + abs(  # using manhattan distance as our heuristic
                    children_node.position[1] - end_node.position[1])

                children_node.f = children_node.g + \
                    children_node.h  # f(n) = g(n) + f(n)
                relevant_children.append(children_node)
        return relevant_children

    def search(grid, fringe, knowledge_grid, start_position, end_position, is_gridknown):
        '''

        :param grid: the original actual grid
        :param fringe: list of all processed nodes
        :param knowledge_grid: the knowledge grid
        :param start_position: start position in grid
        :param end_position: end position in grid
        :param is_gridknown: parameter to switch between grid and knowledge grid
        :return: the path from start node to end node
        '''
        startNode = Node(None, start_position)
        endNode = Node(None, end_position)

        fringe = []
        visited_nodes = {}
        already_fringed = {}  # a hashmap to keep track of fringed nodes and its lowest cost
        already_fringed[startNode.position] = startNode.f
        # pushing start node in fringe
        heapq.heappush(fringe, (startNode.f, startNode))
        all_moves = [[1, 0],  # defined all moves -
                     [0, 1],  # [1,0] - move right
                     [-1, 0],  # [0,1] - move down
                     [0, -1]]  # [0,-1] - move up
        # [-1,0] - move left

        path = []
        while fringe:  # while fringe is not empty
            current = heapq.heappop(fringe)  # popping node from fringe
            current = current[1]
            # assigning current node to visited
            visited_nodes[current.position] = "Added"

            if current.position == endNode.position:

                i = current
                while (i is not None):  # traversing path if current=goal to get the path from start to goal
                    path.append(i.position)
                    i = i.parent

                return "Solvable", path
            children = generate_children(  # otherwise generate children
                grid, knowledge_grid, fringe, visited_nodes, current, all_moves, endNode, is_gridknown)
            if children:
                for node in children:
                    if node.position in already_fringed:  # checking if the children is already fringed,
                        # if yes update and push the moinimum cost one
                        if already_fringed[node.position] > node.f:
                            # otherwise ignore the child
                            already_fringed[node.position] = node.f
                            heapq.heappush(fringe, (node.f, node))
                    else:
                        # if the child is not already fringed, push it
                        heapq.heappush(fringe, (node.f, node))
                        # to priority queue and assign in the hashmap
                        already_fringed[node.position] = node.f

        return "Unsolvable", path

    fringe = []
    dim = int(wjdata["dim"])
    is_gridknown = "No"
    density = float(wjdata["density"])

    # create a grid with entered density and dim values.
    grid = create_grid(density, dim)

    # assuming unblocked for all cells
    # intialise knowledge grid to all 1's
    knowledge_grid = [[1 for _ in range(dim)] for _ in range(dim)]
    im = None
    pltGrid = deepcopy(grid)
    pltGrid[0][0] = 3
    prevPos = [0, 0]
    start = (0, 0)
    end = (dim-1, dim-1)
    all_moves = [[1, 0],
                 [0, 1],
                 [-1, 0],
                 [0, -1]]
    for a_move in all_moves:
        child_x = start[0] + a_move[0]
        child_y = start[1] + a_move[1]
        if (child_x > dim-1 or child_x < 0 or child_y > dim-1 or child_y < 0):
            continue
        else:
            if (grid[child_x][child_y] == 0):
                # update the knowledge grid with field of view
                knowledge_grid[child_x][child_y] = 0
    ll, path = search(grid, fringe, knowledge_grid, start, end, is_gridknown)
    final_path = []
    if (ll != "Unsolvable" and is_gridknown == "No"):
        while (len(path) > 1 and ll != "Unsolvable"):
            count = 0
            flag = 0

            # traverse the path obtained from search function to see if blocked cell exists or not.
            # If blocked cell exists, run search function again to calculate the path
            #  Continue in this while loop -1) either path returned is 0 that means nothing left in fringe and no path to reach goal 2) or path exists to reach goal

            for i in path[::-1]:
                count += 1
                for a_move in all_moves:
                    child_x = i[0] + a_move[0]
                    child_y = i[1] + a_move[1]
                    if (child_x > dim-1 or child_x < 0 or child_y > dim-1 or child_y < 0):
                        continue
                    else:
                        if (grid[child_x][child_y] == 0):
                            knowledge_grid[child_x][child_y] = 0
                final_path.append((i[0], i[1]))

                if (grid[i[0]][i[1]] == 0):
                    pltGrid[i[0]][i[1]] = 4  # blocked in grid
                    final_path.pop()
                    knowledge_grid[i[0]][i[1]] = 0  # updating knowledge_grid
                    new_start_position = path[path.index(
                        i)+1][0], path[path.index(i)+1][1]
                    ll, path = search(grid, fringe, knowledge_grid,
                                      new_start_position, end, is_gridknown)
                    finalresult = ll
                    break

                pltGrid[prevPos[0]][prevPos[1]] = 2
                pltGrid[i[0]][i[1]] = 3
                prevPos = [i[0], i[1]]
                socketio.sleep(0.5)
                # print("pltGrid", pltGrid)
                emit("data", {'data': pltGrid,
                     'id': request.sid, 'status': "Solving..."}, broadcast=False)

                if (count == len(path)):
                    emit("data", {'data': pltGrid,
                                  'id': request.sid, 'status': "Reached the destination!"}, broadcast=False)
                    print("Solved")
                    flag = 1
                    break
            if (flag == 1):

                return final_path, knowledge_grid
                break
        if (ll == "Unsolvable"):

            emit("data", {'data': pltGrid, 'status': "Unsolvable"
                          }, broadcast=False)
            print("Unsolvable")
            # return [], knowledge_grid
        if (flag != 1):
            print("finalresult", finalresult)
            # return [], knowledge_grid

    elif (is_gridknown == "Yes"):
        print(ll)
        print("path", path)

    else:
        emit("data", {'data': pltGrid, 'status': "Unsolvable"
                      }, broadcast=False)
        print("Unsolvable")
        for (i, j) in final_path:
            grid[i][j] = 2

        # return [], knowledge_grid

@socketio.on("disconnect")
def disconnected():
    """event listener when client disconnects to the server"""
    print("user disconnected")
    emit("disconnect", f"user {request.sid} disconnected", broadcast=True)

if __name__ == '__main__':
    socketio.run(app, debug=True, port=5001)


import csv
with open('dataset.csv','r') as f:
	reader = csv.reader(f)
	header_row = next(reader)
	ip=[]
	tp=[]
	for row in reader:
#		print(row[0])
		ip.append(row[0])
		tp.append(row[1])
f.close()

nip=[]
ntp=[]
for i in ip:
	if i not in nip:
		nip.append(i)
		count =ip.index(i)
		ntp.append(tp[count])
	elif i in nip:
		count1 = ip.index(i)
		count2 = nip.index(i)
		if float(tp[count1]) >float(ntp[count2]):
			ntp[count2] = tp[count1]
for i in range(len(nip)):
	with open('newdataset.csv','a+') as f:
		f.write(nip[i] + ','+ ntp[i] + '\n')


import argparse
import cv2
import minerl
import numpy as np
import plotly.express as px
import random
import streamlit as st
from pathlib import Path

import time

@st.cache(suppress_st_warning=True, allow_output_mutation=True, max_entries=1)
def get_timeseries_actions_fig(actions_wide, action_labels, rewards):
    st.warning("Cache miss: `get_timeseries_actions_fig` ran")
    fig = px.imshow(
        actions_wide,
        x = list(range(actions_wide.shape[1])),
        y = action_labels,
        height=300,
    )
    fig.update_traces(dict(
        showscale=False,
        coloraxis=None,
        colorscale=[(0, "#FFF"), (1, "#3f51b5")],
        ), selector={'type':'heatmap'})
    fig.update_layout(
        margin=dict(l=0, r=20, t=20, b=0),
    )
    return fig

class App:
    def __init__(self):
        # self.data_frames = []
        pass

    @st.cache(suppress_st_warning=True, allow_output_mutation=True, max_entries=1)
    def get_trajectory_names(self, data_dir: Path):
        st.warning("Cache miss: `get_trajectory_names` ran")
        traj_dirs = sorted([x for x in data_dir.glob("*/*") if x.is_dir()])
        traj_names = [str(Path(x.parent.stem) / x.stem) for x in traj_dirs]
        return traj_names

    @st.cache(suppress_st_warning=True, allow_output_mutation=True, max_entries=1)
    def load_data(self, data_dir, env_name, stream_name):
        st.warning("Cache miss: `load_data` ran")
        minerl_data = minerl.data.make(env_name, data_dir=data_dir)
        data_frames = list(minerl_data.load_data(stream_name, include_metadata=True))
        return data_frames

    def get_timeseries_reward(self, data_frames):
        rewards = [float(frame[2]) for frame in data_frames]
        return rewards

    def get_timeseries_actions(self, data_frames):
        action_labels = sorted([key for key in data_frames[0][1].keys()])
        actions_timeseries_wide = []
        for key in action_labels:
            action_sample = data_frames[0][1][key]
            if type(action_sample) == np.ndarray:
                actions_timeseries_wide.append([(1 if np.any(frame[1][key]) else 0) for frame in data_frames])
            elif type(action_sample) == np.str_:
                actions_timeseries_wide.append([(0 if frame[1][key] == "none" else 1) for frame in data_frames])
            elif type(action_sample) == np.int64:
                actions_timeseries_wide.append([float(frame[1][key]) for frame in data_frames])
            else:
                raise Exception(f"Action type not supported! `{action_sample}` of type `{type(action_sample)}`")
        return np.array(actions_timeseries_wide), action_labels

    def run(self, data_dir):
        st.set_page_config(page_title="MineRL Trajectory Viewer", page_icon=None, layout='wide')
        st.title('MineRL Trajectory Viewer')

        col1, col2, col3, col4 = st.columns([6,2,2,2])

        with col1:
            data_dir = Path(data_dir)
            st.write(f"Data dir: `{data_dir}`")

            # Select trajectory
            traj_names = self.get_trajectory_names(data_dir)
            option = st.selectbox(
                'Select a trajectory:',
                traj_names)
            chosen_path = data_dir / option

            env_name = str(Path(chosen_path).parent.stem)
            stream_name = Path(chosen_path).stem
            data_frames = self.load_data(data_dir, env_name, stream_name)

            # TODO: Display the video!
            # video_frames = np.stack([frame[0]["pov"].transpose(2,0,1) for frame in self.data_frames])
            # video_frames = np.stack([frame[0]["pov"] for frame in self.data_frames])

            # st.write(video_frames.shape)
            # tmp_vid_path = "vid.avi"
            # fourcc = cv2.VideoWriter_fourcc(*'MJPG')
            # video = cv2.VideoWriter(tmp_vid_path, fourcc, 20.0, video_frames[0].shape[:2])
            # for image in video_frames:
            #     video.write(image)
            # video.release()
            # print("Video saved to", tmp_vid_path)
            # st.video(tmp_vid_path)

            # # Display GIF / video
            # st.write("## Playback")
            # video_path = str(Path(chosen_path) / "recording_.mp4")
            # st.video(video_frames)
            # See Streamlit issue:
            # https://github.com/streamlit/streamlit/pull/1583

            # Select current frame
            max_frame = len(data_frames) - 1
            frame_idx = st.slider("Select frame:", 0, max_frame, 0)
            current_frame = data_frames[frame_idx]

            state, action, reward, next_state, done, meta = current_frame

            # Aggregate plots
            actions_wide, action_labels = self.get_timeseries_actions(data_frames)
            rewards = self.get_timeseries_reward(data_frames)
            fig = get_timeseries_actions_fig(actions_wide, action_labels, rewards)
            st.write("### Actions over time")
            st.plotly_chart(fig, use_container_width=True)
            st.write("### Rewards over time")
            st.area_chart(rewards, height=100)

        with col2:
            st.write("### Actions")
            st.write(action)
            st.write("### Reward")
            st.write(f"`{reward}`")
            st.write("### Done")
            st.write(done)
            st.write("### Metadata")
            st.write(meta)

        with col3:
            st.write("### State")
            st.image(state["pov"], use_column_width=True, caption="Current State POV")
            for key, val in state.items():
                if key == "pov":
                    continue
                st.write(f"#### {key}")
                st.write(val)

        with col4:
            st.write("### Next State")
            st.image(next_state["pov"], use_column_width=True, caption="Next State POV")
            for key, val in next_state.items():
                if key == "pov":
                    continue
                st.write(f"#### {key}")
                st.write(val)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Visualize MineRL trajectories')
    parser.add_argument("-d", "--data-dir", required=True,
                        help="Root directory containing trajectory data. Default: %(default)s")
    options = parser.parse_args()

    app = App()
    app.run(options.data_dir)


# Title: 감소하는 수
# Link: https://www.acmicpc.net/problem/1038
import itertools
import sys

sys.setrecursionlimit(10 ** 6)

def read_list_int():
    return list(map(int, sys.stdin.readline().strip().split(' ')))

def read_single_int():
    return int(sys.stdin.readline().strip())

def add_to_one(d_number):
    if d_number == 9876543210:
        return -1
    r_str = []
    d_str = list(reversed(str(d_number)))
    carry = 0
    num = '1' + '0'*(len(d_str)-1)
    for i, d in enumerate(d_str):
        if i+1 == len(d_str):
            r = int(d) + int(num[i]) + carry
            if r >= 10:
                r_str.append(str(len(r_str)))
                r_str.append(str(len(r_str)))
            else:
                r_str.append(str(r))
        else:
            r = int(d) + int(num[i]) + carry
            if r < int(d_str[i+1]):
                r_str.append(str(r))
                carry = 0
            else:
                carry = 1
                if len(r_str) == 0:
                    r_str.append('0')
                else:
                    r_str.append(str(int(r_str[-1])+1))
    return int(''.join(reversed(r_str)))

def get_nth_number(N):
    d_number = 0
    for _ in range(N):
        d_number = add_to_one(d_number)
        if d_number == -1:
            return -1

    return d_number

def get_nth_decreasing_numbers(N):
    decreasing_numbers = []
    for n in range(1, 11):
        decreasing_numbers += sorted(map(lambda l: int(''.join(l)), itertools.combinations('9876543210', n)))
        if len(decreasing_numbers) > N:
            return decreasing_numbers[N]
    return -1

if __name__ == '__main__':
    N = read_single_int()
    # print(get_nth_number(N))
    print(get_nth_decreasing_numbers(N))


#Number guessing program on the command line.
#Guess the number (between 1 and 10) and see if you are right!

#Written by Caleb Phillips (LebbyFoxx).
#Monday 28th September 2020.

import random

#Function that randomises number.
def numGuess():
    #Variable 'tries' is set to 5 (and will be reduced if the answer is wrong).
    global tries
    tries = 5
    #Set number to a random value between 1 and 10.
    global number
    number = random.randint(1,10)
    print ("I am thinking of a number from 1 to 10.")
    print ("Can you guess it?")

#Function that restarts game.
def restart():
    #Ask player to input a key to restart game.
    #Anything should do (except for 'tab' and stuff like that? See what works).
    reset = input ("Press a key to restart: ")
    tries = 5
    #Call the numGuess game function again.
    numGuess()

#Main loop.
numGuess()
running = True
while running:

    #Checking to see if the player has any 'tries' left.
    while tries > 0:
        #Checking to see if input was an integer.
        try:
            numIn = int(input("Number>>> "))
            #If the number is guess right then the player wins!
            if numIn == number:
                print ("You guessed it! :)")
                restart()

            elif numIn < number:
                print ("Higher.")
                #If they get it wrong then 'tries' gets reduced by 1.
                tries -=1

            elif numIn > number:
                print ("Lower.")
                #Same goes for picking a lower number.
                tries -=1

        #If not an integer then tell user to input one.
        #Luckily there is no penalty for a non-number answer.
        except:
            print ("Please input a number.")

    #When 'tries' has been depleted it is game over and the game restarts.
    print ("Game over!")
    restart()


#!/usr/bin/env python

"""
This script creates an injection and a .ini file to run LALInference

(C) Archisman Ghosh, Abhirup Ghosh, 2015-09-20
"""

import sys, lal, os, commands, numpy as np
sys.path.insert(1, os.path.join(os.path.expanduser('~'), 'src/lalsuite/lalinference/python'))
import imrtestgr as tgr
import nr_fits as nr
import optparse as op
import chirptimes as ct
import time
from time import sleep
import glob

os.system('. ${HOME}/.profile')
home = os.getenv('HOME')
user = commands.getoutput('whoami')
lal_prefix = os.getenv('LAL_PREFIX')
psd_path = '%s/share/lalsimulation'%lal_prefix
glue_location = os.getenv('GLUE_LOCATION')
pylal_location = os.getenv('PYLAL_LOCATION')

parser = op.OptionParser()
parser.add_option('--inj-no', type='int', dest='inj', help='injection number')
parser.add_option('--fcut', type='float', dest='fcut', help='fcut')
parser.add_option('--seglen', type='int', dest='seglen', help='seglen')
(options, args) = parser.parse_args()
inj = options.inj
fcut = options.fcut
seglen = options.seglen

inj_file = '../injections/waveform_systematics/waveform_systematics_investigations_SEOBNRv4_ROMpseudoFourPN_m_10_80_z_0_0p5_UCTV'
out_folder = '/home/abhirup/Documents/Work/testGR_IR/runs/waveform_systematics/20170427_SEOBNRv4_ROM_inj_SEOBNRv4_ROM_rec_H1L1V1_flow15Hz/injection_%04d'%inj

# ### RECOVERY ###

approximant = 'SEOBNRv4_ROMpseudoFourPN' # recovery waveform
ligo_psd = os.path.join(psd_path, 'LIGO-T0900288-v3-ZERO_DET_high_P.txt')
virgo_psd = os.path.join(psd_path, 'LIGO-P1200087-v18-AdV_DESIGN.txt')

def write_pipeline(out_folder, dataseed, flow=15, fhigh=1023., nlive=1024, create_dag=True):

    randomseed = np.random.randint(32768)

    os.system('mkdir -p %s'%(out_folder))
    os.system('cp %s.xml %s/injection.xml'%(inj_file, out_folder))

    ofile = open(os.path.join(out_folder, 'pipeconfig.ini'), 'w')

    ofile.write("[analysis]\n")
    ofile.write("ifos=['H1','L1','V1']\n")
    ofile.write("engine=lalinferencenest\n")
    ofile.write("nparallel=4\n")
    ofile.write("coherence-test=False\n")
    ofile.write("accounting_group=ligo.prod.o2.cbc.pe.lalinference\n")
    ofile.write("upload-to-gracedb=False\n")
    ofile.write("dataseed=%d\n"%dataseed) # ###FIXME###
    ofile.write("\n")

    projectdir = out_folder.replace('%s/Documents/Work/'%home, '')

    ofile.write("[paths]\n")
    ofile.write("webdir=%s/public_html/%s\n"%(home, projectdir))
    ofile.write("basedir=%s/public_html/%s\n"%(home, projectdir))
    ofile.write("baseurl=https://dumpty.alice.icts.res.in/~%s/%s\n"%(user, projectdir))
    ofile.write("\n")

    ofile.write("[input]\n")
    ofile.write("max-psd-length=1024\n")
    ofile.write("padding=16\n")
    ofile.write("timeslides=false\n")
    ofile.write("ignore-science-segments=True\n")
    ofile.write("events=%d\n"%(int(inj-1)))
    ofile.write("\n")

    ofile.write("[datafind]\n")
    ofile.write("types={'H1':'LALSimAdLIGO','L1':'LALSimAdLIGO','V1':'LALSimAdVirgo'}\n")
    ofile.write("\n")

    ofile.write("[data]\n")
    ofile.write("channels={'H1':'LALSimAdLIGO','L1':'LALSimAdLIGO','V1':'LALSimAdVirgo'}\n")
    ofile.write("\n")

    ofile.write("[condor]\n")
    ofile.write("mpirun=/bin/true\n")
    ofile.write("datafind=%s/bin/gw_data_find\n"%(glue_location))
    ofile.write("gracedb=/bin/true\n")
    ofile.write("ligolw_print=%s/bin/ligolw_print\n"%(glue_location))
    ofile.write("segfind=%s/bin/ligolw_segment_query\n"%(glue_location))
    ofile.write("lalinferencenest=%s/bin/lalinference_nest\n"%(lal_prefix))
    ofile.write("lalinferencemcmc=%s/bin/lalinference_mcmc\n"%(lal_prefix))
    ofile.write("mergescript=%s/bin/lalapps_nest2pos\n"%(lal_prefix))
    ofile.write("resultspage=%s/bin/cbcBayesPostProc.py\n"%(pylal_location))
    ofile.write("ppanalysis=%s/bin/cbcBayesPPAnalysis.py\n"%(pylal_location))
    ofile.write("pos_to_sim_inspiral=%s/bin/cbcBayesPosToSimInspiral.py\n"%(pylal_location))
    ofile.write("coherencetest=%s/bin/lalapps_coherence_test\n"%(lal_prefix))
    ofile.write("lalinferencedatadump=False\n")
    ofile.write("skyarea=/usr/bin/run_sky_area\n")
    ofile.write("\n")

    ofile.write("[engine]\n")
    ofile.write("approx=%s\n"%(approximant))
    ofile.write("seglen=%d\n"%(seglen))
    ofile.write("nlive=%d\n"%(nlive))
    ofile.write("srate=2048\n")
    ofile.write("distance-min=1\n")
    ofile.write("distance-max=20000\n")
    ofile.write("comp-min=3.0\n")
    ofile.write("comp-max=300.0\n")
    ofile.write("a_spin1-max=0.99\n")
    ofile.write("a_spin2-max=0.99\n")
    ofile.write("amporder=-1\n") ##FIXME##
    ofile.write("resume=\n")
    ofile.write("progress=\n")
    ofile.write("H1-asd=%s\n"%(ligo_psd))
    ofile.write("L1-asd=%s\n"%(ligo_psd))
    ofile.write("V1-asd=%s\n"%(virgo_psd))
    ofile.write("randomseed=%d\n"%(randomseed))
    ofile.write("\n")

    ofile.write("[lalinference]\n")
    ofile.write("fake-cache={'H1':'interp:%s','L1':'interp:%s','V1':'interp:%s'}\n"%(ligo_psd, ligo_psd, virgo_psd))
    #ofile.write("fake-cache={'H1':'LALSimAdLIGO','L1':'LALSimAdLIGO','V1':'LALSimAdVirgo'}\n")
    ofile.write("flow={'H1':%.2f,'L1':%.2f,'V1':%.2f}\n"%(flow, flow, flow))
    ofile.write("fhigh={'H1':%.2f,'L1':%.2f,'V1':%.2f}\n"%(fhigh, fhigh, fhigh))
    ofile.write("\n")

    ofile.write("[merge]\n")
    ofile.write("\n")

    ofile.write("[resultspage]\n")
    ofile.write("skyres=0.5\n")
    ofile.write("\n")

    ofile.close()

    if create_dag:
      os.system('%s/bin/lalinference_pipe -I %s/injection.xml -r %s -p %s %s/pipeconfig.ini'%(lal_prefix, out_folder, out_folder, out_folder, out_folder))
      dag_folder = os.path.join(out_folder, 'lalinferencenest', approximant)
      dag_file = glob.glob(os.path.join(dag_folder, 'lalinference_*.dag'))[0]
      print('condor_submit_dag %s'%(dag_file))
      os.system('condor_submit_dag %s'%(dag_file))

# Main
if __name__ == '__main__':

    imr_folder = '%s/IMR'%(out_folder)
    insp_folder = '%s/inspiral'%(out_folder)
    post_insp_folder = '%s/post-inspiral'%(out_folder)

    insp_fhigh = ring_flow = fcut
    dataseed = np.random.randint(1,1000)

    write_pipeline(imr_folder, dataseed)
    write_pipeline(insp_folder, dataseed, fhigh=insp_fhigh)
    write_pipeline(post_insp_folder, dataseed, flow=ring_flow)


from user_access import data_read, data_write

class User:
    def __init__(self, *args, **kwargs):
        self.__dict__.update(kwargs)

    @classmethod
    def create_user(cls, *args, **kwargs):
        user = cls(*args, **kwargs)
        user.save()
        return user

    def save(self):
        users_data = data_read('./user.json')
        self.id = len(users_data) + 1
        users_data.append(self.__dict__)
        data_write(users_data, './user.json')

    @classmethod
    def initial_users(cls):

        pass

    @classmethod
    def find_user(cls, field_name, field_value):
        users_data = data_read ('./user.json')
        found_user =[]
        for user in users_data:
            if user[field_name] == field_value:
                found_user.append(cls(**user))
        return found_user if found_user else None


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

def plantilla_ahorcado( palabra, lista_letras, lista_errores):
    """ plantilla que crea a partir de una palabra y dos listas con aciertos y errores
    la figura del juego del ahorcado """

    persona = ("O", "|", "/", "\\","/", "\\")
    horca = [" ", " ", " ", " ", " ", " "]

    for i in range(len(lista_errores)):
        horca[i] = persona[i]

    print(
"""
      +---+
      |   |
      {0}   |
     {2}{1}{3}  |
     {4} {5}  |
          |
    =========""".format(horca[0], horca[1], horca[2], horca[3], horca[4], horca[5]).center(40))
    print("Palabra:", end=" ")
    for letra in palabra:
        if letra not in lista_letras:
            print ("_", end=" ")
        else:
            print(letra, end="")

    print()


def checkMagazine(magazine, note):
    mag = {}
    match = 'Yes'
    for word in magazine:
        if word in mag:
            mag[word] += 1
        else:
            mag[word] = 1
    for word2 in note:
        if word2 in mag:
            mag[word2] -= 1
            if mag[word2] < 0:
                match = 'No'
        else:
            match = 'No'
    print(match)


import pymol
from pymol import cmd

class PymolRun:
    @staticmethod
    def main(queue):
        print('### LAUCH PYMOL')
        pymol.finish_launching()
        cmd.fetch('1d86')

        while True:
            # {'cmd': [0, 1, 3]}
            # If the queue is empty, queue.get() will block until the queue has data
            cmd_dict = queue.get()
            if cmd_dict.get('translate'):
                cmd.translate(cmd_dict['translate'])
            elif cmd_dict.get('rotate'):
                cmd.rotate('y', cmd_dict['rotate'])
            elif cmd_dict.get('save'):
                cmd.save(cmd_dict['save'])
            elif cmd_dict.get('rotate180right'):
                cmd.rotate("y", cmd_dict['rotate180right'])
            elif cmd_dict.get('rotate180left'):
                cmd.rotate("y", cmd_dict['rotate180left'])
            elif cmd_dict.get('rotateup'):
                cmd.rotate("x", cmd_dict['rotateup'])
            elif cmd_dict.get('rotatedown'):
                cmd.rotate("x", cmd_dict['rotatedown'])


#!/usr/bin/env python3
"""Contains function sum_list"""
from typing import Union, List

def sum_mixed_list(mxd_list: List[Union[int, float]]) -> float:
    """Calculates the sum of floats in the given list"""
    sum: float = 0.00
    for num in mxd_list:
        sum += num

    return sum


class Solution:
    def findMin(self, nums: List[int]) -> int:
        if len(nums) == 1 or nums[0] < nums[-1]:
            return nums[0]

        l, r = 0, len(nums) - 1

        while l <= r:
            mid = l + ((r - l) >> 1)

            if nums[mid - 1] > nums[mid]:
                return nums[mid]

            # search on the right side, because smaller elements are in the right side
            if nums[mid] > nums[r]:
                l = mid + 1
            else:
                # search the minimum in the left side
                r = mid - 1


import os, sys
import gym
import torch
import cv2
import numpy as np
import random
import time
import schedule
sys.path.append("./")
from network.backbone import BasicNet
from policy_optimizer import Optimizer
from network.dqn import DQN
from utils.dataloader import tnx2batch, batch4net
from utils.atari_wrapper import wrap_rainbow
from utils.trace import PriorTrace
from policy_optimizer import Optimizer
import time

torch.backends.cudnn.benchmark = True

"""
Transition = {"state": np.array, "action": int, "next_state": np.array, "reward": float, "done": logical}
"""

class BasicWorker():
    def __init__(self, env_name="PongNoFrameskip-v4", save_interval=1000, max_steps=100000,
        phase="train", db=None, db_write=None, suffix="default"):
        assert phase in ["train", "valid", "test"], "phase can only be train/test/valid"
        self.phase = phase
        if self.phase == "train":
            self.env = wrap_rainbow(gym.make(env_name), swap=True, phase="train")
        else:
            self.env = wrap_rainbow(gym.make(env_name), swap=True, phase="test")
        self.env_name = env_name
        self.save_interval = save_interval
        self.max_steps = max_steps
        self.db = db
        if db_write:
            assert "db" in db_write.__code__.co_varnames and "data" in db_write.__code__.co_varnames
        self.db_write = db_write
        self.ob = self.reset()
        self.info = {}
        self.cache = []
        self.save_count = 0
        self.video_path = "./video/{}/{}".format(env_name, suffix)
        self.sche = schedule.Sched()
        print("{}\tOb Space: {}\tActions: {}".format(self.env_name, self._shape(), self._na()))

    def reset(self):
        """return ob"""
        return self.env.reset()

    def step(self, a):
        next_ob, rw, done, info = self.env.step(a)
        return next_ob, rw, done, info

    def _simulate_with_train(self):
        self.ob = self.reset()
        done, episod_len, episod_rw, episod_real_rw = False, 0, 0, 0
        while not done and episod_len < self.max_steps:
            a = self._action()
            next_ob, rw, done, info = self.step(a)
            self.cache.append({"state": self.ob, "action": a, "next_state": next_ob, "reward": rw, "done": done})
            self.ob = next_ob
            episod_len += 1
            self.save_count += 1
            episod_rw += rw
            episod_real_rw += info["reward"]
            if self.save_count % self.save_interval == 0:
                if len(self.sche):
                    self.sche.wait()
                self.sche.add(None, self.db_write, db=self.db, data=self.cache)
                self.cache.clear()
                self.save_count = 0
        self.info["episod_rw"] = episod_rw
        self.info["episod_real_rw"] = episod_real_rw
        self.info["episod_len"] = episod_len
        if "total_env_steps" in self.info:
            self.info["total_env_steps"] += episod_len
        else:
            self.info["total_env_steps"] = episod_len
        return self.info

    def _simulate_with_test(self, episod=30):
        self.ob = self.reset()
        true_ob = self.env.render(mode="rgb_array")
        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        if not os.path.exists(self.video_path):
            os.makedirs(self.video_path)
        out = cv2.VideoWriter(os.path.join(self.video_path, "video-{}.avi".format(time.strftime("%Y-%m-%d_%H:%M:%S", time.localtime()))),
            fourcc, 25.0, (true_ob.shape[1], true_ob.shape[0]))
        acc_rw, done = 0, False
        count, acc_len = 0, 0
        while count < episod and acc_len < episod*self.max_steps:
            episod_len, done = 0, False
            self.ob = self.reset()
            while not done and episod_len < self.max_steps:
                a = self._action(eps=0.05)
                self.ob, rw, done, _ = self.step(a)
                acc_rw += rw
                episod_len += 1
                acc_len += 1
                true_ob = self.env.render(mode="rgb_array")
                out.write(true_ob)
            count += 1
        out.release()
        self.ob = self.reset()
        return acc_rw / count

    def __iter__(self):
        return self

    def __next__(self):
        if self.phase == "train":
            return self._simulate_with_train()
        elif self.phase == "valid":
            return self._simulate_with_test(episod=30)
        else:
            return self._simulate_with_test(episod=200)

    def _action(self, eps=None):
        return self.env.action_space.sample()

    def _shape(self):
        return self.ob.shape

    def _na(self):
        return self.env.action_space.n

    def update(self):
        raise NotImplementedError

    def load(self):
        raise NotImplementedError

class DQN_Worker(BasicWorker):
    def __init__(self, env_name="PongNoFrameskip-v4", arch=DQN, backbone=BasicNet, cuda=True,
                save_interval=1000, max_steps=100000, phase="train", db=None, db_write=None,
                suffix="default", **kwargs):
        super(DQN_Worker, self).__init__(env_name, save_interval, max_steps, phase, db, db_write, suffix)
        self.shape = self._shape()
        self.na = self._na()
        self.alg = arch(self.shape, self.na, backbone).eval()
        self.alg.cuda() if cuda == True else None
        self.cuda = cuda
        self.eps = 0

    def _action(self, eps=None):
        eps = self.eps if eps is None else eps
        if random.random() < eps:
            a = self.env.action_space.sample()
        else:
            with torch.no_grad():
                ob = torch.from_numpy(self.ob).cuda().float() if self.cuda else torch.from_numpy(self.ob).float()
                a = self.alg.action(ob).item()
        return a

    def update(self, state_dict=None, eps=None):
        if state_dict is not None:
            self.alg.load_state_dict(state_dict["policy"])
            # if self.write_prior:
            #     self.target.load_state_dict(state_dict["target"])
        if eps is not None:
            self.eps = eps

    def load(self, path):
        self.alg.load_state_dict(torch.load(path))

class PriorDQN_Worker(DQN_Worker):
    """
    todo discount
    """
    def __init__(self, env_name="PongNoFrameskip-v4", arch=DQN, backbone=BasicNet, cuda=True,
                save_interval=1000, max_steps=100000, phase="train", db=None, db_write=None,
                suffix="default", **kwargs):
        super(PriorDQN_Worker, self).__init__(env_name, arch, backbone, cuda, save_interval, max_steps,
                                                phase, db,db_write,suffix)
        self.trace = PriorTrace(discount=0.99)

    def _action(self, eps=None):
        eps = self.eps if eps is None else eps
        with torch.no_grad():
            ob = torch.from_numpy(self.ob).cuda().float() if self.cuda else torch.from_numpy(self.ob).float()
            v, a = self.alg.value_action(ob)
        if random.random() < eps:
            a = self.env.action_space.sample()
        else:
            a = a.item()
        self.trace.add_value(v)
        self.trace.add_action(a)
        return a

    def _simulate_with_train(self):
        self.ob = self.reset()
        done, episod_len, episod_rw, episod_real_rw = False, 0, 0, 0
        while not done and episod_len < self.max_steps:
            a = self._action()
            next_ob, rw, done, info = self.step(a)
            self.trace.add_reward(rw)
            self.trace.add_done(done)
            self.cache.append({"state": self.ob, "action": a, "next_state": next_ob, "reward": rw, "done": done})
            self.ob = next_ob
            episod_len += 1
            self.save_count += 1
            episod_rw += rw
            episod_real_rw += info["reward"]
            if self.save_count % self.save_interval == 0:
                self._action()
                if len(self.sche):
                    self.sche.wait()
                prior = self.trace.prior()
                assert len(prior) == len(self.cache), "prior len {} not match cache len {}".format(len(prior), len(self.cache))
                self.sche.add(None, self.db_write, db=self.db, data=self.cache, prior=prior)
                self.cache.clear()
                self.save_count = 0
                self.trace.reinit()
        self.info["episod_rw"] = episod_rw
        self.info["episod_real_rw"] = episod_real_rw
        self.info["episod_len"] = episod_len
        if "total_env_steps" in self.info:
            self.info["total_env_steps"] += episod_len
        else:
            self.info["total_env_steps"] = episod_len
        return self.info


from collections import deque
from contextlib import contextmanager
from string import Template
from textwrap import dedent

import dominate
from dominate.tags import *
from dominate.util import text
import arcade.examples
import pathlib
import math
import demos.lib02
import demos.movement

def main():
    with open('reveal/reveal.js-3.6.0/index.html.template',
              encoding='utf-8') as f:
        tmpl = Template(f.read())

    output = tmpl.safe_substitute(
        slides='\n'.join(str(s) for s in slides)
    )
    print(output)
    with open('reveal/reveal.js-3.6.0/indexgen.html', 'w', encoding='utf-8') as f:
        f.write(output)

    print('Total slides: ', len(slides))

slides = []

def add_slide(f):
    @section
    def inner(*args, **kwargs):
        f(*args, **kwargs)

    slides.append(inner())

def add_slide_plain(f):
    @section
    def inner(*args, **kwargs):
        f(*args, **kwargs)

    slides.append(inner())

def add_slide50(f):
    @section(style='top: 50px')
    def inner(*args, **kwargs):
        f(*args, **kwargs)

    slides.append(inner())

def code_block(filename, lines=None, size='0.4em', highlights=None):
    code_path = pathlib.Path('demos') / filename
    with pre(style=f'font-size: {size}'):
        with code(cls='hljs python', data_trim='true',
                  style='max-height: unset',
                  data_noescape='true'
                  ):
            with open(code_path) as f:
                data = f.readlines()
            if lines:
                data = data[slice(lines[0] - 1, lines[1] - 0)]
            content = ''.join(data)
            if highlights:
                c = content
                for s in highlights:
                    a, b, c = c.partition(s)
                    text(a)
                    mark(b)
                text(c)
            else:
                text(''.join(data))

@add_slide
def slide_title():
    h1('multiplayer 2D gaming')
    h2('with python-arcade')
    with p(style='font-size: 0.7em'):
        text('@caleb_hattingh ● ')
        # a('github.com/cjrh', href='github.com/cjrh')
        a('github.com/cjrh/pyconau2018-arcade2Dmultiplayer', href='github.com/cjrh/pyconau2018-arcade2Dmultiplayer')

@add_slide
def slide_goals():
    h2('Goals')
    with p():
        text('Show you how to build a simple multiplayer game!')

@add_slide
def slide_goals():
    h2('Goals')
    with p():
        s('Show you how to build a simple multiplayer game!')
    div(style='margin-top: 30px;')
    p('Show you the building blocks')

@add_slide
def slide_about_me():
    h2('about me')
    with ul():
        with li():
            with p():
                text('Network automation at ')
                img(src='/img/pccwglobal.png', height=42, style='vertical-align:middle;')
        # li('"Network Automation" at PCCW Global')
        li("Books and videos at O'Reilly Safari:")
    with div():
        img(src='/img/cythoncover.jpg', height=250)
        img(src='/img/20libscover.png', height=250)
        with div(style='display: inline-block'):
            p('April 2018!', style='font-size: 0.6em; margin: 0')
            img(src='/img/aiocover.png', height=250)
    a('https://www.safaribooksonline.com/search/?query=caleb%20hattingh',
      href='https://www.safaribooksonline.com/search/?query=caleb%20hattingh',
      style='font-size: 0.6em;')

@add_slide
def slide_python_arcade():
    h2('Python-Arcade')
    with p():
        text('created by Paul Craven ●')
        a('http://arcade.academy', href='http://arcade.academy')
    with div(style='display: flex; margin: auto'):
        img(data_src='img/penguin.png', width=48, height=48)
        with pre():
            with code(cls='hljs bash', data_trim=True,
                      contenteditable=True):
                text('''\
                    (venv) $ pip install arcade
                ''')
    with div(style='display: flex; margin: auto'):
        img(data_src='img/windows_icon.png', width=48, height=48)
        with pre():
            with code(cls='hljs cmd', data_trim=True,
                      contenteditable=True):
                text('''\
                    (venv) C:\mygame> pip install arcade
                ''')

@add_slide
def slide_arcade_docs():
    img(src='/img/arcade_doc_screenshot.png')

@add_slide
def slide_why_arcade():
    h2('Why Python-Arcade?')
    with ul():
        li('Easy to install')
        li('OpenGL (via Pyglet)')
        li('Modern (Python 3 only, type annotations)')
        with li():
            code('(0, 0)')
            text(' is at the bottom-left')
        li('Very clean, simple API')
        with li(cls='fragment'):
            strong('Examples 🎁🎁🎁')

@add_slide
def slide_lots_of_examples():
    with h2():
        u('lots')
        text(' of examples')

    examples_path = pathlib.Path(arcade.examples.__file__)
    examples_dir = examples_path.parent
    examples = deque(sorted(
        f.name for f in filter(
            lambda x: x.suffix == '.py' and x.name != '__init__.py',
            list(examples_dir.iterdir()))
    ))

    columns = 3
    column_height = math.ceil(len(examples) / columns)

    with div(cls='container', style='font-size: 0.3em;'):
        for i in range(columns):
            with div(cls='col'):
                for j in range(column_height):
                    if examples:
                        p(examples.popleft())

@contextmanager
def code_bullet(btn_text='', cmd=''):
    with li():
        with pre():
            with code(cls='hljs bash', data_trim='true',
                      contenteditable='true', data_noescape='true'):
                yield
        if btn_text:
            button(
                btn_text,
                cls='runprogram',
                cmd=cmd)

@add_slide
def slide_run_examples():
    h2('lots of examples')
    exnames = ['bouncing_ball', 'sprite_collect_coins',
               'sprite_move_keyboard']
    with ul():
        for example in exnames:
            cmd = f'arcade.examples.{example}'
            with code_bullet(btn_text=example, cmd=cmd):
                text('(venv) $ python -m arcade.examples.')
                mark(example)

@add_slide
def slide_get_started_intro():
    h2('Intro: Python-Arcade')

@add_slide50
def slide_getting_started():
    h2('Getting started')
    code_block('getting_started.py',
               highlights=[
                   'def update',
                   'def on_draw',
                   'def on_key_press',
                   'def on_key_release',
               ])
    button('getting_started.py', cls='runprogram', cmd='demos.getting_started')

@add_slide50
def slide_getting_started():
    h2('Getting started')
    code_block('getting_started.py',
               highlights=[
                   'self.keys_pressed',
                   'apply_movement',
                   'self.keys_pressed.keys[key] = True',
                   'self.keys_pressed.keys[key] = False',
               ])
    button('getting_started.py', cls='runprogram', cmd='demos.getting_started')

@add_slide
def slide_keyspressed():
    h2('"Movement" utils')
    code_block('movement.py',
               size='0.5em',
               highlights=[
                   'current_position + delta_position * speed * dt'
               ])

@add_slide
def slide_tip_vector():
    h2('🎁tip #1: use a vector class')
    with p():
        text('Use the one in ')
        strong('pymunk')

    with pre():
        with code(cls='hljs python', data_trim='true', contenteditable='true'):
            text(dedent('''\
                >>> from pymunk.vec2d import Vec2d
                >>> pixel = Vec2d(3, 4)
                >>> pixel.x
                3
                >>> pixel.y
                4
                >>>
                >>> pixel + 2 * pixel
                Vec2d(9, 12)
                >>> pixel.length
                5.0
                >>> pixel.length = 1
                >>> pixel
                Vec2d(0.6, 0.8)
            '''))

@add_slide
def slide_keyspressed():
    h2('"Movement" utils')
    import inspect
    # code_text = inspect.getsource(demos.movement)
    code_block('movement.py',
               size='0.5em',
               highlights=['.normalized()'])
    # with pre(style='font-size: 0.4em'):
    #     with code(cls='hljs python', data_trim='true',
    #               style='max-height: unset'):
    #         text(code_text)
    button('Normalize your movement vector!',
           cls='runprogram', cmd='demos.getting_started_norm')

# @add_slide
def slide_lag_compensation():
    p(dedent('''\
Good discussion about lag compensation

https://www.reddit.com/r/Overwatch/comments/3u5kfg/everything_you_need_to_know_about_tick_rate/
https://en.wikipedia.org/wiki/Netcode
https://www.pcgamer.com/netcode-explained/

Some python code here:

https://www.gamedev.net/forums/topic/652377-network-tick-rates/

Book on safari:
https://www.safaribooksonline.com/library/view/fundamentals-of-network/9781584505570/ch01.html
    '''))

@add_slide_plain
def slide_shall():
    return section(data_background_image='/img/shallplaygame.jpg')

@add_slide_plain
def slide_fortnite():
    return section(data_background_image='/img/fortnite3.jpg')

@add_slide_plain
def slide_sc2():
    return section(data_background_image='/img/starcraft2.jpg')

@add_slide_plain
def slide_awesomenauts():
    return section(data_background_image='/img/awesomenautsplay.jpg')

@add_slide
def slide_network_models():
    h2('Network models')
    with section():
        with ol():
            with li(cls='fragment'):
                strong('Client-server: ')
                text('clients only "capture inputs"')
                with ul(style='font-size: 0.7em;'):
                    examples = ['Fortnite', 'Quake', 'Unreal Tournament', 'Overwatch']
                    for ex in examples:
                        li(ex)
            with li(cls='fragment'):
                strong('Peer-to-peer (lockstep): ')
                text('synced sim on each client')
                with ul(style='font-size: 0.7em;'):
                    examples = ['Command & Conquer', 'Age of Empires',
                                'StarCraft', 'Supreme Commander 2']
                    for ex in examples:
                        li(ex)
            with li(cls='fragment'):
                strong('Peer-to-peer: ')
                text('each client calculates self')
                with ul(style='font-size: 0.7em;'):
                    examples = ['Awesomenauts']
                    for ex in examples:
                        li(ex)

    with section():
        links = [
            a('What every programmer needs to know about game networking - Glenn Fiedler',
              href='https://gafferongames.com/post/what_every_programmer_needs_to_know_about_game_networking/'),
            a('Core network structures for games - Joost van Dongen',
              href='http://joostdevblog.blogspot.com/2014/09/core-network-structures-for-games.html'),
            a('Source Multiplayer Networking - Valve',
              href='https://developer.valvesoftware.com/wiki/Source_Multiplayer_Networking'),
        ]

        p('References', style='font-size: 0.6em')
        for link in links:
            p(link, style='font-size: 0.6em')

@add_slide
def slide_big_picture():
    h2('Client-server: The Big Picture')
    img(src='/img/server-based-network.svg', width=500)

@add_slide
def slide_basic_networking():
    h2('Communication')
    with ol():
        li('Player inputs: client 🠊 server')
        li('Game state: client 🠈 server')
        li('TCP versus UDP')

@add_slide
def player_inputs():
    h2('1. Player input state')
    p('send client 🠊 server')
    with ul():
        with li():
            b('inputs ')
            text('not "speed" or "position"')
        li('Use dataclasses:')

    code_block('player_event.py', highlights=['class PlayerEvent'])

@add_slide
def game_state():
    h2('2. Game state')
    p('send server 🠊 client')
    code_block('game_state.py', highlights=[
        'x: float = 0',
        'y: float = 0',
        'class GameState'
    ])

@add_slide
def slide_tcp_udp():
    h2('3. TCP vs UDP')
    with ul():
        with li(cls='fragment'):
            text('Problem: TCP is ')
            strong('too ')
            text('reliable')
            with ul():
                li('Dropped packets causes latency (bad!)')
                li("Sometimes packet loss is ok")
        with li(cls='fragment'):
            text('UDP chosen for ')
            u('control')
            text(' (not merely speed)')
            with ul():
                li('Can choose when to allow packet loss...')
                li("BUT: it's much more work")
    # TODO: maybe an image showing how one lost packet causes a bunch of
    # others to wait.

@add_slide
def slide_zmq_excuse():
    with p():
        text("No time for UDP!")
    img(src='/img/greenninja.jpg')
    with p():
        text("Instead, we'll just show TCP (and ZeroMQ) for simplicity.")

@add_slide
def slide_zmq():
    h2('Brief intro to ZeroMQ')
    p('Thin abstraction over TCP ● "magic" sockets')
    with ul():
        with li(cls='fragment'):
            text('Handling player inputs')
            with ul():
                with li():
                    b('PUSH + PULL ')
                    text('sockets')
                li('all clients push to a single server')

        with li(cls='fragment'):
            text('Handling game state')
            with ul():
                with li():
                    b('PUB + SUB ')
                    text('sockets')
                li('Server pushes to all clients')

@add_slide
def slide_zmq_demo():
    h2('ZMQ client & server')
    with div(cls='container'):
        with div(cls='col', style='padding: 10px'):
            h3('Client (player)')
            with pre():
                with code(cls='hljs python', data_trim='true', contenteditable='true'):
                    text(dedent('''\
                        from asyncio import run, Queue
                        import zmq
                        from zmq.asyncio import Context, Socket

                        async def zmq_push(q: Queue):
                            ctx = Context()
                            sock = ctx.socket(zmq.PUSH)
                            sock.connect('127.0.0.1', 9999)
                            while True:
                                payload: Dict = await q.get()
                                await sock.send_json(payload)

                            ctx.destroy()
                    '''))
        with div(cls='col', style='padding: 10px'):
            h3('Server')
            with pre():
                with code(cls='hljs python', data_trim='true', contenteditable='true'):
                    text(dedent('''\
                        from asyncio import run, Queue
                        import zmq
                        from zmq.asyncio import Context, Socket

                        async def zmq_pull(q: Queue):
                            ctx = Context()
                            sock = ctx.socket(zmq.PULL)
                            sock.bind('127.0.0.1', 9999)
                            while True:
                                payload = await sock.recv_json()
                                await q.put()

                            ctx.destroy()
                    '''))

@add_slide
def slide_strategy():
    h2('Client-server: Design')

    def lip(text=None):
        if text:
            return li(text, cls='fragment', style='font-size: 0.7em')
        else:
            return li(cls='fragment', style='font-size: 0.7em')

    with div(cls='container'):
        with div(cls='col', style='padding: 10px'):
            h3('Client')
            with ol():
                lip('client connects to server')
                with lip():
                    strong('Task A: ')
                    text('send player input (keyboard, mouse) '
                         'e.g. 30 Hz')
                with lip():
                    strong('Task B: ')
                    text('receive game state (position, health) '
                         'from server')
                with lip():
                    strong('Task C: ')
                    text('draw game state on screen')
        with div(cls='col', style='padding: 10px'):
            h3('Server')
            with ol():
                with lip():
                    strong('Task A: ')
                    text('accept client connections')
                with lip():
                    strong('Task B: ')
                    with ol():
                        li('receive player input')
                        li('update game state')
                with lip():
                    strong('Task C: ')
                    text('send game state to clients, e.g. 15 Hz')

    p('Each of the internal tasks runs independently.',
      cls='fragment')

@add_slide
def begin_with_server():
    h4("Let's begin with the server")
    p("(It's easier)")

@add_slide
def server_code_main():
    h3("Server code - main (1/2)")
    code_block('server03.py', lines=[44, 67], size='0.5em',
               highlights=[
                   'Task A',
                   'task_B',
                   'task_C'
               ])

@add_slide
def server_code_tasks():
    h3("Server code - task detail (2/2)")
    code_block('server03.py', lines=[15, 42], highlights=['TASK B', 'TASK C'])

# Client code
@add_slide
def client_code1():
    h2('Client code needs TWO loops!')
    with ul():
        li('python-arcade: game loop')
        li('asyncio: IO loop')
        with li():
            b('Cannot ')
            text('run both loops in same thread')

    p('Least-effort solution: run asyncio loop in a thread')
    # mention - because of asyncio, only need 1 extra thread, whereas
    # blocking sockets would need more.

@add_slide
def client_code_whole():
    h3('Client code - main (1/3)')
    code_block('client03.py', size='0.5em', lines=[131, 146],
               highlights=['iomain', 'MyGame']
               )

@add_slide
def client_code_gross():
    img(src='/img/gross.gif')

@add_slide
def client_code_io():
    h3('Client code - iomain thread (2/3)')
    # should also use the "empty" version here
    # rename window.t to window.time_since_state_update
    code_block('client03-empty.py', size='0.4em', lines=[61, 92],
               highlights=['iomain', 'window.player_input',
                           'window.game_state.from_json',
                           'window.player.position'])

@add_slide
def client_code_game():
    h3('Client code - game object (3/3)')
    # Need a version without any prediction!
    # And then later also need a version WITH prediction.
    code_block('client03-empty.py', size='0.4em', lines=[27, 60],
               highlights=['MyGame', 'pass'])

@add_slide
def client_code_interp():
    h2('Success demo!')

    button('server', cls='runprogram', cmd='demos.server02')
    button('client', cls='runprogram', cmd='demos.client02b')

@add_slide
def end_slide01():
    p('End*')

@add_slide
def client_not_success():
    h5("(Not success - it's laggy)!")

@add_slide
def client_code_prediction():
    h2('Sprite interpolation')
    img(src='/img/prevlatestupdate.svg', width='80%')

@add_slide
def slide_client_interpolation():
    h3('Client-side extrapolation')
    with p():
        text('Need to understand ')
        u('motion')
        text(', i.e., speed')
    with script(type='math/tex; mode=display'):
        text(dedent(r'''
            v_x = \frac{x_1 - x_0}{t_1 - t_0} \qquad v_y = \frac{y_1 - y_0}{t_1 - t_0}
        '''))
    with p(cls='fragment'):
        text('This describes the past—what about the future?')
    with div(cls='fragment'):
        with script(cls='fragment', type='math/tex; mode=display'):
            text(dedent(r'''
                v_x = \frac{\color{red}{x_2} - x_1}
                {\color{red}{t_2} - t_1} \qquad v_y =
                \frac{\color{red}{y_2} - y_1}{\color{red}{t_2} - t_1}
            '''))

@add_slide
def slide_client_interpolation_2():
    h3('Client-side extrapolation')
    with script(type='math/text; mode=display'):
        text(dedent(r'''
            v_x = \frac{\color{red}{x_2} - x_1}
            {\color{red}{t_2} - t_1} \qquad v_y =
            \frac{\color{red}{y_2} - y_1}{\color{red}{t_2} - t_1}
        '''))
    with p(cls='fragment'):
        text('Make the predicted values explicit:')
    with div(cls='fragment'):
        with script(cls='fragment', type='math/tex; mode=display'):
            text(dedent(r'''
                \color{red}{x_2} = v_x \times \Delta t + x_1
                \qquad
                \color{red}{y_2} = v_y \times \Delta t + y_1
            '''))

@add_slide
def slide_client_interpolation_3():
    h3('Client-side extrapolation')
    with ul():
        li('Store last 2 server updates')
        li('Calculate predicted future update (extrapolation)')
        li('DRAW interpolated positions between "now" and the predicted '
           'future position.')

@add_slide
def slide_client_interp_codeA():
    h3('Client-side extrapolation')
    code_block('client02d.py', size='0.5em',
               lines=[145, 155],
               highlights=[
                   'window.position_buffer.append',
                   'window.t',
                   'window.player_position_snapshot'
               ])

@add_slide
def slide_client_interp_codeB():
    h3('Client-side extrapolation')
    code_block('client02d.py', size='0.5em',
               lines=[89, 108],
               highlights=[
                   'predicted_position',
                   'x = (self.t - 0) / (t1 - t0)',
               ])

@add_slide
def client_code_demo_4_updates_per_second():
    h2('Success demo attempt #2!')

    p('Prediction + interpolation (2 Hz server updates)')
    button('server', cls='runprogram', cmd='demos.server02c')
    button('client', cls='runprogram', cmd='demos.client02c')

    p('Prediction + interpolation (10 Hz server updates)')
    button('server', cls='runprogram', cmd='demos.server02')
    button('client', cls='runprogram', cmd='demos.client02')

@add_slide
def fin():
    p('The end!')

if __name__ == '__main__':
    main()


__all__ = ["load", "save", "load_single_particle_model", "load_multi_particle_model"]

import os
import numpy as np
import tensorflow as tf
import deeptrack as dt
from tqdm import tqdm
import glob
import pandas as pd

def load(filename: str):
    *_, ext = filename.split(os.path.extsep)

    if os.path.isdir(filename):
        return load_dir(filename)

    if ext == "py":
        return load_python(filename)

    return load_video(filename)

def load_python(filename):
    pass

def load_video(filename):
    import cv2

    cap = cv2.VideoCapture(filename)

    n_frames, width, height = (
        int(cap.get(cv2.CAP_PROP_FRAME_COUNT)),
        int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)),
    )

    frames = np.empty((n_frames, width, height, 3))

    print("Reading video...")
    for t in tqdm(range(n_frames)):
        _, frames[t] = cap.read()

    return frames

def load_dir(filename):
    npys = glob.glob(os.path.join(filename, "*.npy"))
    csvs = glob.glob(os.path.join(filename, "*.csv"))

    npy_files = [np.load(f) for f in npys]
    csv_files = [pd.read_csv(f) for f in csvs]

    npy_files = np.stack(npy_files, axis=0)

    csv_files = pd.concat(csv_files)
    # csv_files = np.stack(csv_files, axis=0)

    npy_files = np.reshape(npy_files, (-1, *npy_files.shape[-3:]))

    return npy_files, csv_files

def load_image(filename):
    pass

def save(model, args):

    _out_path = f"checkpoints/{args.prefix}{os.path.split(args.filename)[-1]}"
    out_path = _out_path
    idx = 0
    while os.path.exists(out_path):
        idx += 1
        out_path = f"{_out_path}_{idx}"
    out_path = os.path.normcase(out_path)
    os.makedirs(out_path, exist_ok=True)

    model.save(out_path)

def load_single_particle_model(path):
    return tf.keras.models.load_model(
        path,
        custom_objects={
            "AutoTrackerModel": dt.models.autotrack.AutoTracker.AutoTrackerModel
        },
    )

def load_multi_particle_model(path):
    return tf.keras.models.load_model(
        path,
        custom_objects={
            "AutoTrackerModel": dt.models.autotrack.AutoTracker.AutoTrackerModel
        },
    )


import numpy as np
from pbcore.io import BamReader, IndexedBamReader, IndexedFastaReader, AlignmentSet
import pickle
import sys
import itertools
import pandas as pd
import gzip
import pysam
from array import array
from tqdm import tqdm, trange

# Kinetics tools can be found https://github.com/PacificBiosciences/kineticsTools
# and can be installed in to a conda environment using setup.py
#from kineticsTools.KineticWorker import KineticWorkerProcess
#from kineticsTools.ResultWriter import KineticsWriter
from kineticsTools.ipdModel import IpdModel, GbmContextModel
#from kineticsTools import ReferenceUtils, loader
from kineticsTools.sharedArray import SharedArray

bamIn = pysam.AlignmentFile(sys.argv[1], check_sq=False)
bamOut = pysam.AlignmentFile(sys.argv[2], "wb", header=bamIn.header)

def snippetFunc(refId, pre, post):
    """
    Return a function that returns a snippet of the reference sequence around a given position
    """

    refArray = refId.getNumpyWrapper()

    def f(tplPos, tplStrand):
        """Closure for returning a reference snippet. The reference is padded with N's for bases falling outside the extents of the reference"""
        # skip over the padding
        tplPos += pad

        # Forward strand
        if tplStrand == 0:
            slc = refArray[(tplPos - pre):(tplPos + 1 + post)]
            slc = np.right_shift(slc, 4)
            return "".join(c for c in seqMapNp[slc])

        # Reverse strand
        else:
            slc = refArray[(tplPos + pre):(tplPos - post - 1):-1]
            slc = np.right_shift(slc, 4)
            return "".join(c for c in seqMapComplementNp[slc])

    return f

def _makeFramepoints():
    B = 2
    t = 6
    T = 2**t

    framepoints = []
    next = 0
    for i in range(256//T):
        grain = B**i
        nextOnes = next + grain * np.arange(0, T)
        next = nextOnes[-1] + grain
        framepoints = framepoints + list(nextOnes)
    return np.array(framepoints, dtype=np.uint16)

def _makeLookup(framepoints):
    # (frame -> code) involves some kind of rounding
    # basic round-to-nearest
    frameToCode = np.empty(shape=max(framepoints)+1, dtype=int)
    for i, (fl, fu) in enumerate(zip(framepoints, framepoints[1:])):
        if (fu > fl + 1):
            m = (fl + fu)//2
            for f in range(fl, m):
                frameToCode[f] = i
                frameToCode[f] = i + 1
        else:
            frameToCode[fl] = i
    # Extra entry for last:
    frameToCode[fu] = i + 1
    return frameToCode, fu

_framepoints = _makeFramepoints()
_frameToCode, _maxFramepoint = _makeLookup(_framepoints)

def framesToCode(nframes):
    nframes = np.minimum(_maxFramepoint, nframes)
    return _frameToCode[nframes]

def codeToFrames(code):
    return _framepoints[code]

byte = np.dtype('byte')
uint8 = np.dtype('uint8')

# Map for ascii encoded bases to integers 0-3 -- will be used to define a 24-bit lookup code
# for fetching predicted IPDs from the kinetic LUT.

# We start everything at 0, so anything will map to 'A' unless it appears
# in this table
lutCodeMap = np.zeros(256, dtype=uint8)
maps = {'a': 0, 'A': 0, 'c': 1, 'C': 1, 'g': 2, 'G': 2, 't': 3, 'T': 3}
for k in maps:
    lutCodeMap[ord(k)] = maps[k]
lutReverseMap = {0: 'A', 1: 'C', 2: 'G', 3: 'T'}

seqCodeMap = np.ones(256, dtype=uint8) * 4
for k in maps:
    seqCodeMap[ord(k)] = maps[k]
seqMap = {0: 'A', 1: 'C', 2: 'G', 3: 'T', 4: 'N'}
seqMapNp = np.array(['A', 'C', 'G', 'T', 'N'])

seqMapComplement = {0: 'T', 1: 'G', 2: 'C', 3: 'A', 4: 'N'}
seqMapComplementNp = np.array(['T', 'G', 'C', 'A', 'N'])

# Base letters for modification calling
# 'H' : m6A, 'I' : m5C, 'J' : m4C, 'K' : m5C/TET
baseToCode = {'N': 0, 'A': 0, 'C': 1, 'G': 2,
              'T': 3, 'H': 4, 'I': 5, 'J': 6, 'K': 7}
baseToCanonicalCode = {'N': 0, 'A': 0, 'C': 1,
                       'G': 2, 'T': 3, 'H': 0, 'I': 1, 'J': 1, 'K': 1}

codeToBase = dict([(y, x) for (x, y) in baseToCode.items()])

pre = 10
post = 4
pad = 30
base4 = 4 ** np.array(range(pre + post + 1))
refDict = {}
refLengthDict = {}

refid = 0

lutPath = "./kineticsTools/resources/SP3-C3.npz.gz"

with gzip.open(lutPath, "rb") as npz_in:
    gbmModelData = np.load(npz_in, allow_pickle=True)
    gbmModel = GbmContextModel(gbmModelData, -1)

for read in tqdm(bamIn, desc='Reads'):
    rawSeq = read.seq
    refSeq = np.frombuffer(rawSeq.encode("utf-8"), dtype=byte)

    # Store the reference length

    length = len(rawSeq)

    # Make a shared array
    sa = SharedArray(dtype='B', shape=len(rawSeq) + pad * 2)
    saWrap = sa.getNumpyWrapper()

    # Lut Codes convert Ns to As so that we don't put Ns into the Gbm Model
    # Seq Codes leaves Ns as Ns for getting reference snippets out
    innerLutCodes = lutCodeMap[refSeq]
    innerSeqCodes = seqCodeMap[refSeq]
    innerCodes = np.bitwise_or(innerLutCodes, np.left_shift(innerSeqCodes, 4))

    saWrap[pad:(len(rawSeq) + pad)] = innerCodes

    # Padding codes -- the lut array is padded with 0s the sequence
    # array is padded with N's (4)
    outerCodes = np.left_shift(np.ones(pad, dtype=uint8) * 4, 4)
    saWrap[0:pad] = outerCodes
    saWrap[(len(rawSeq) + pad):(len(rawSeq) + 2 * pad)] = outerCodes

    snipFunction = snippetFunc(sa, post, pre)
    sites = range(0,length)
    contexts = [snipFunction(sites[x], 1) for x in sites]

    control = gbmModel.getPredictions(contexts)
    ip = read.get_tag('ip')
    ipFrames = codeToFrames(read.get_tag('ip'))
    ipFramesNorm = ipFrames / np.mean(ipFrames)
    ipr = ipFramesNorm / control

    read.set_tag('in', array('f', ipFramesNorm))
    read.set_tag('ir', array('f', ipr))
    bamOut.write(read)

bamOut.close()


import sys

if sys.version_info.major == 2:
    import httplib
    from base64 import decodestring as base64_decode
    from StringIO import StringIO as StringLikeIO
else:
    from http import client as httplib
    from base64 import decodebytes as base64_decode
    from io import BytesIO as StringLikeIO

import base64
import json
import numpy as np
import os
import random
import requests
import six
import skimage.io
import skimage.transform
import warnings

from collections import Mapping
from nose.tools import assert_equal
from nose.tools import assert_in
from nose.tools import assert_is_instance
from nose.tools import assert_true
from nose.tools import assert_tuple_equal
from numpy.testing import assert_array_almost_equal
from parameterized import parameterized
from unittest import TestSuite

def bounded_repr(obj, limit=100, trim=20):
    obj_repr = repr(obj)

    if len(obj_repr) > limit:
        obj_repr = '{0}...<truncated>...{1}'.format(
            obj_repr[:trim],
            obj_repr[-trim:])

    return obj_repr

def assert_response_expectations(
    response, expected_status_codes, ok_key, expected_ok_value):

    expected_status_codes = set(expected_status_codes)

    assert_in(response.status_code, expected_status_codes,
        msg='got invalid status code HTTP {0} from endpoint'.format(
            response.status_code))

    json_data = response.json()

    assert_in(ok_key, json_data,
        msg='expected "{0}" key to be present in response JSON data'.format(
            ok_key))

    assert_equal(json_data[ok_key], expected_ok_value,
        msg='expected "{0}" key to be {1}, got {2} (type={3})'.format(
            ok_key,
            expected_ok_value,
            repr(json_data[ok_key]),
            type(json_data[ok_key]).__name__))

    return json_data

def assert_follows_schema(schema, data, name):

    if isinstance(schema, type):
        assert_is_instance(data, schema,
            msg=('expected `{name}` to be of type {expected_type}, got '
                 '{actual_type} [{repr}]').format(
                    name=name,
                    expected_type=schema,
                    actual_type=type(data).__name__,
                    repr=bounded_repr(data)))

    elif isinstance(schema, tuple):
        assert_true(isinstance(data, schema),
            msg=('expected `{name}` to be one of types {expected_types}, got '
                 '{actual_type} [{repr}]').format(
                    name=name,
                    expected_types=json.dumps([t.__name__ for t in schema]),
                    actual_type=type(data).__name__,
                    repr=bounded_repr(data)))

    elif isinstance(schema, Mapping):
        for schema_key, schema_value in six.iteritems(schema):
            assert_follows_schema(
                schema_value,
                data[schema_key], '{name}.{key}'.format(
                    name=name, key=schema_key))

    elif isinstance(schema, list):

        if len(schema) != 1:
            raise ValueError(
                ('schema assertions for lists are expected to contain only 1 '
                 'element, but {num_elements} elements were provided').format(
                    num_elements=len(schema)))

        for i, element in enumerate(data):
            assert_follows_schema(
                schema[0],
                element,
                '{name}.#{num}'.format(name=name, num=i))

    else:
        raise ValueError(
            ('got unsupported schema definition, got type {schema_type} '
             'instead').format(schema_type=type(schema).__name__))

class TestSegmenter(TestSuite):

    def setUp(self):
        host = os.environ.get('SEGMENTER_TEST_HOST', 'http://localhost')
        port = os.environ.get('SEGMENTER_TEST_PORT', 80)
        self.max_size = os.environ.get('SEGMENTER_TEST_MAX_SIZE', 1080)

        endpoint = '{host}:{port}'.format(host=host, port=port)
        response = requests.get(endpoint, timeout=3)

        if response.status_code != httplib.OK:
            raise RuntimeError(
                'got HTTP {0} from microservice at {1}'.format(
                    response.status_code,
                    endpoint))

        before_filename = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'data', 'before.png'))
        if not os.path.isfile(before_filename):
            raise RuntimeError(
                'could not locate "before" test image [{0}]'.format(
                    before_filename))

        after_filename = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'data', 'after.npy'))
        if not os.path.isfile(after_filename):
            raise RuntimeError(
                'could not locate "after" test image [{0}]'.format(
                    after_filename))

        self.endpoint = endpoint
        self.before_filename = before_filename
        self.after_filename = after_filename

    def test_ping_response(self):
        response = requests.get(self.endpoint)

        assert_equal(response.status_code, httplib.OK,
            msg='expected HTTP {0}, got HTTP {1}'.format(
                httplib.OK, response.status_code))

    def test_inference_with_single_image(self):
        before_image = skimage.io.imread(self.before_filename)
        with open(self.before_filename, 'rb') as fp:
            base64_before_image = base64.b64encode(fp.read()).decode('ascii')

        response = requests.post(
            self.endpoint,
            data={
                'images': json.dumps([base64_before_image]),
            })

        json_data = assert_response_expectations(
            response,
            expected_status_codes=[httplib.OK],
            ok_key='ok',
            expected_ok_value=True)

        expected_schema = {
            'ok': bool,
            'masks': six.string_types,
        }
        assert_follows_schema(expected_schema, json_data, 'response')

        returned_mask_string = json.loads(json_data['masks'])
        stringlike_buffers = [
            StringLikeIO(base64_decode(base64_response.encode('ascii')))
                for base64_response in returned_mask_string]
        returned_masks = [skimage.io.imread(b) for b in stringlike_buffers]

        assert_equal(len(returned_masks), 1,
            msg='expected only 1 mask in the response')

        returned_mask = returned_masks[0]

        after_mask = np.load(self.after_filename)
        assert_array_almost_equal(after_mask, returned_mask,
            err_msg='segmentation mask is incorrect')

    def test_inference_with_multiple_images(self):
        num_images = random.randint(2, 5)

        before_image = skimage.io.imread(self.before_filename)
        with open(self.before_filename, 'rb') as fp:
            base64_before_image = base64.b64encode(fp.read()).decode('ascii')

        response = requests.post(
            self.endpoint,
            data={
                'images': json.dumps(
                    [base64_before_image for _ in range(num_images)]),
            })

        json_data = assert_response_expectations(
            response,
            expected_status_codes=[httplib.OK],
            ok_key='ok',
            expected_ok_value=True)

        expected_schema = {
            'ok': bool,
            'masks': six.string_types,
        }
        assert_follows_schema(expected_schema, json_data, 'response')

        returned_mask_string = json.loads(json_data['masks'])
        stringlike_buffers = [
            StringLikeIO(base64_decode(base64_response.encode('ascii')))
                for base64_response in returned_mask_string]
        returned_masks = [skimage.io.imread(b) for b in stringlike_buffers]

        assert_equal(len(returned_masks), num_images,
            msg='expected {0} masks in the response, got {1}'.format(
                num_images, len(returned_masks)))

        after_mask = np.load(self.after_filename)

        for returned_mask in returned_masks:

            assert_array_almost_equal(
                after_mask,
                returned_mask,
                err_msg='segmentation mask is incorrect')

    def test_inference_with_oversized_image(self):
        before_image = skimage.io.imread(self.before_filename)

        # catch skimage's warnings
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', category=UserWarning)

            before_image = skimage.transform.resize(
                before_image,
                (int(1.1 * self.max_size), int(1.1 * self.max_size)))

            stringlike_buffer = StringLikeIO()
            skimage.io.imsave(stringlike_buffer, before_image)

        base64_before_image = base64.b64encode(
            stringlike_buffer.getvalue()).decode('ascii')

        response = requests.post(
            self.endpoint,
            data={
                'images': json.dumps([base64_before_image]),
            })

        json_data = assert_response_expectations(
            response,
            expected_status_codes=[httplib.OK],
            ok_key='ok',
            expected_ok_value=True)

        expected_schema = {
            'ok': bool,
            'masks': six.string_types,
        }
        assert_follows_schema(expected_schema, json_data, 'response')

        returned_mask_string = json.loads(json_data['masks'])
        stringlike_buffers = [
            StringLikeIO(base64_decode(base64_response.encode('ascii')))
                for base64_response in returned_mask_string]
        returned_masks = [skimage.io.imread(b) for b in stringlike_buffers]

        assert_equal(len(returned_masks), 1,
            msg='expected only 1 mask in the response')

        returned_mask = returned_masks[0]

        assert_tuple_equal(returned_mask.shape, before_image.shape[:2],
            msg='expected mask shape to be {0}, got {1}'.format(
                before_image.shape[:2], returned_mask.shape))

    @parameterized.expand([
        [
            'mising_images',
            {},
        ],
        [
            'no_images',
            {
                'images': json.dumps([]),
            },
        ],
    ])
    def test_inference_with_missing_parameters(self, name, payload):
        response = requests.post(self.endpoint, data=payload)

        json_data = assert_response_expectations(
            response,
            expected_status_codes=[httplib.BAD_REQUEST],
            ok_key='ok',
            expected_ok_value=False)

        expected_schema = {
            'ok': bool,
            'error_message': six.string_types,
        }
        assert_follows_schema(expected_schema, json_data, 'response')


import requests
from get_access_token_by_refresh_token import get_access_token

def get_modules():
    url = 'https://www.zohoapis.in/crm/v2/settings/modules'
    access_token = get_access_token()

    headers = {
        'Authorization': 'Zoho-oauthtoken {}'.format(access_token)
    }

    response = requests.get(url=url, headers=headers)

    if response is not None:
        print("HTTP Status Code : " + str(response.status_code))

        print(response.json())

get_modules()


import flet as ft
import views as views_handle

def main(page: ft.Page):

    #defining the fonts
    page.fonts = {
        "SF Pro": "https://raw.githubusercontent.com/google/fonts/master/ofl/sfprodisplay/SFProDisplay-Bold.ttf",
    }

    #defining the window size
    page.window_min_width = 425
    page.window_width = 425
    page.window_min_height = 820
    page.window_height = 820

    page.title = "Project 334 Appication"
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    #page.scroll = ft.ScrollMode.HIDDEN
    page.padding = 0
    page.bgcolor = "#ddf7f1"

    page.theme = ft.Theme(font_family="SF Pro")
    page.theme_mode = ft.ThemeMode.LIGHT

    def route_change(route):
        print(page.route)
        # CLEAR ALL PAGE
        page.views.clear()
        page.views.append(
            views_handle.views(page)[page.route],
        )
        page.update()

    page.on_route_change = route_change
    page.go('/')

ft.app(target = main)


#길이제한 해주는 프로그램 GPT3 최대 2048개의 토큰 처리 가능
import pandas as pd
from transformers import GPT2Tokenizer

def trim_to_combined_token_limit(csv_file, max_tokens=2048):
    tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
    df = pd.read_csv(csv_file)

    for idx, row in df.iterrows():
        # convert 'nan' or empty inputs to ""
        row['Title'] = row['Title'] if row['Title'] and str(row['Title']).lower() != 'nan' else ""
        row['Content'] = row['Content'] if row['Content'] and str(row['Content']).lower() != 'nan' else ""
        row['Comments'] = row['Comments'] if row['Comments'] and str(row['Comments']).lower() != 'nan' else ""

        tokenized_title = tokenizer.encode(row['Title'], truncation=False)
        tokenized_content = tokenizer.encode(row['Content'], truncation=False)
        tokenized_comments = tokenizer.encode(row['Comments'], truncation=False)

        # If the combined tokens exceed the limit, trim the 'content' from the end
        # and the 'comments' from the beginning until they fit
        while len(tokenized_title) + len(tokenized_content) + len(tokenized_comments) > max_tokens:
            if len(tokenized_content) > len(tokenized_comments):
                tokenized_content = tokenized_content[:-1]
            else:
                tokenized_comments = tokenized_comments[1:]

        df.loc[idx, 'Title'] = tokenizer.decode(tokenized_title)
        df.loc[idx, 'Content'] = tokenizer.decode(tokenized_content)
        df.loc[idx, 'Comments'] = tokenizer.decode(tokenized_comments)

    df.to_csv('trimmed_dataset.csv', index=False)

trim_to_combined_token_limit('everytime_hotarticle_merged.csv')


#问题011：有一对兔子，从出生后第3个月起每个月都生一对兔子，小兔子长到第三个月后每个月又生一对兔子。假如兔子都不死。问每个月的兔子总数为多少？
#【思路分析】这其实就是斐波拉契数列的由来，可以思考斐波拉契数列的打印过程
'''
实际解决这题时，按照事实的逻辑顺序来解题即可，即：
第一个月有一对小兔子，
第二个月一对小兔子长成了一对大兔子
第三个月，一对大兔子生出了一对小兔子，原本的一对大兔子都不死
每个月的过程是：
大兔子每月生小兔子
原先的小兔子长成2月兔（下月生育）
大兔子不死
'''
smallrabi=1
grownrabi=0
oldrabi=0
for month in range(1,12):
    oldrabi=oldrabi+grownrabi
    grownrabi=smallrabi
    smallrabi=oldrabi
    print("第%2d 个月，有%4d 对小兔子,%4d 对大兔子，共%4d 对兔子" %(month+1,smallrabi,grownrabi,smallrabi+grownrabi+oldrabi))


from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save

from datetime import datetime

##
## begin util functies
##
def nvl(value,default_value) :
  return default_value if value is None else value

##
## einde util functies
##

class AukArtikelRankVw(models.Model):
    artikel_id = models.IntegerField()
    artikel_titel = models.CharField(max_length=200)
    krant = models.TextField()
    cat_id = models.IntegerField()
    cat_volgorde = models.IntegerField()
    categorie = models.CharField(max_length=200)
    pubdate = models.DateTimeField()
    pubdate_yyyymmdd = models.TextField()
    krant_weight = models.DecimalField(decimal_places=6,max_digits=10)
    calculated_weight = models.DecimalField(decimal_places=6,max_digits=10)
    cloud_weight = models.DecimalField(decimal_places=6,max_digits=10)
    total_weight = models.DecimalField(decimal_places=6,max_digits=10)
    rank = models.DecimalField(decimal_places=6,max_digits=10)
#    class Meta:
#        abstract = True

class AukArtikel(models.Model):
    uniek = models.CharField(max_length=200, unique=True)
    titel = models.CharField(max_length=200)
    pubdate = models.DateTimeField()
    rssfeeddescription = models.CharField(max_length=4000)
    url = models.CharField(max_length=1000)
    body = models.CharField(max_length=64000)
    body_clean = models.CharField(max_length=64000)
#    body_stemmed = models.CharField(max_length=64000)
    class Meta:
        db_table = u'auk_artikel'

    def __unicode__(self):
        return u'%s' % (self.uniek)

#Alles van de krant
class Krant(models.Model):
#    id = models.IntegerField(primary_key=True)
    naam = models.CharField(unique=True, max_length=100)
    url = models.CharField(max_length=1000)
    class Meta:
        db_table = u'auk_krant'

    def __unicode__(self):
        return u'%s' % (self.naam)

class Rubriek(models.Model):
    krt = models.ForeignKey(Krant)
    naam = models.CharField(max_length=100)
    url = models.CharField(unique=True,max_length=1000)
    actief = models.CharField(max_length=1)
    class Meta:
        db_table = u'auk_rubriek'

    def __unicode__(self):
        return u'%s' % (self.naam)

class Categorie(models.Model):
#    id = models.IntegerField(primary_key=True)
    volgorde = models.IntegerField()
    naam = models.CharField(unique=True,max_length=100)
    cloud = models.CharField(max_length=4000,null=True,blank=True)
    cloud_factor = models.DecimalField(default=1,max_digits=4, decimal_places=2)
    aging_factor = models.DecimalField(default=1,max_digits=4, decimal_places=2)
    class Meta:
        db_table = u'auk_categorie'

    def __unicode__(self):
        return u'%s' % (self.naam)

class GebruikerCategorie(models.Model):
#    id = models.IntegerField(primary_key=True)
    usr = models.ForeignKey(User)
    cat = models.ForeignKey(Categorie)
    volgorde = models.IntegerField()
    aantal_artikelen = models.IntegerField()
    class Meta:
        db_table = u'auk_gebruiker_categorie'
        unique_together = ("usr","cat")

#
# Intersectie tussen rubriek en categorie
#

class AukRubriekCategorie(models.Model):
    rbk = models.ForeignKey(Rubriek)
    cat = models.ForeignKey(Categorie)
    class Meta:
        db_table = u'auk_rubriek_categorie'
        unique_together = ("rbk","cat")

#
# Intersectie tussen artikel en categorie
#

class AukArtikelCategorie(models.Model):
    art               = models.ForeignKey(AukArtikel)
    cat               = models.ForeignKey(Categorie)
    krant_weight      = models.FloatField(null=True)
    calculated_weight = models.FloatField(null=True)
    cloud_weight      = models.FloatField(null=True)
    total_weight      = models.FloatField(null=True)
    rank              = models.IntegerField(null=True)

    class Meta:
        db_table = u'auk_artikel_categorie'
        unique_together = ("art","cat")

#
# Intersectie tussen artikel en rubriek
#

class AukArtikelPublicatie(models.Model):
    art = models.ForeignKey(AukArtikel)
    rbk = models.ForeignKey(Rubriek)

    class Meta:
        db_table = u'auk_artikel_publicatie'
        unique_together = ("art", "rbk")

#
# Search and Rank tabellen
#
class SarIgnorewordlist(models.Model):
    word = models.CharField(max_length=4000)
    class Meta:
        db_table = u'sar_ignorewordlist'

class SarWordlist(models.Model):
    word = models.CharField(unique=True, max_length=4000)
    class Meta:
        db_table = u'sar_wordlist'

class SarWordlocation(models.Model):
    art = models.ForeignKey(AukArtikel)
    wrd = models.ForeignKey(SarWordlist)
    location = models.IntegerField()
    class Meta:
        db_table = u'sar_wordlocation'
        unique_together = ("art", "wrd", "location")

#
#
# Logging om hierna learning te bewerkstelligen
#
class LogAction(models.Model):
     usr = models.ForeignKey(User,null=True)
     art = models.ForeignKey(AukArtikel,null=True)
     session_key       = models.CharField(max_length=40)
     first_action_time = models.DateTimeField(default=datetime.now)
     last_action_time  = models.DateTimeField()
     action            = models.CharField(max_length=100)
     spent_time        = models.IntegerField(default=0)
     unique_together = ("usr","art","session_key")

class Vote(models.Model) :
     usr   = models.ForeignKey(User)
     art   = models.ForeignKey(AukArtikel)
     score = models.IntegerField()
     unique_together = ("usr","art")


import numpy as np

# gradient descent는 tensorflow에 이미 구현이 되어있다.
# 확률적 선형 뉴런 분석기는 각 훈련 샘플에 대해서 조금씩 가중치를 업데이트 한다.
class LogisticRegressionGD(object):
    """경사 하강법을 이용한 Logistic regression 분류기

    매개변수
    ------------
    eta : float
      학습률 (0.0과 1.0 사이)
    n_iter : int
      훈련 데이터셋 반복 횟수
    random_state : int
      가중치 무작위 초기화를 위한 난수 생성기 시드

    속성
    -----------
    w_ : 1d-array
      학습된 가중치
    cost_ : list
      에포크마다 누적된 비용 함수의 제곱합

    """
    def __init__(self, eta=0.05, n_iter=100, random_state=None):
        self.eta = eta
        self.n_iter = n_iter
        self.random_state = random_state

    def fit(self, X, y):
        """훈련 데이터 학습

                매개변수
                ----------
                X : {array-like}, shape = [n_samples, n_features]
                  n_samples개의 샘플과 n_features개의 특성으로 이루어진 훈련 데이터
                y : array-like, shape = [n_samples]
                  타깃값

                반환값
                -------
                self : object

                """

        rgen = np.random.RandomState(self.random_state)
        self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])
        self.cost_ = []

        for i in range(self.n_iter):
            net_input = self.net_input(X)
            output = self.activation(net_input)
            errors = (y - output)
            self.w_[1:] += self.eta * X.T.dot(errors)
            self.w_[0] += self.eta * errors.sum()

            # 오차 제곱합 대신 로지스틱 비용을 계산, 아달린과는 다름
            cost = (-y.dot(np.log(output)) - ((1 - y).dot(np.log(1 - output))))
            self.cost_.append(cost)
        return self

    def net_input(self, X):
        """최종 입력 계산"""
        return np.dot(X, self.w_[1:]) + self.w_[0]

    # 단순 항등 함수 (단일층 시녕망을 통해 정보의 흐름을 표현 하기 위한 함수)
    def activation(self, z):
        """로지스틱 활성화 계산"""
        return 1. / (1. + np.exp(-np.clip(z, -250, 250)))

    def predict(self, X):
        """단위 계산 함수를 사용하여 클래스 테이블을 반환합니다."""
        return np.where(self.activation(self.net_input(X)) >= 0.5, 1, 0)
        # 아래 방식으로도 리턴이 가능하다/
        # return np.where(self.net_input(X) >= 0.0, 1, 0)


# coding: utf-8
from pycparser import c_parser, c_ast
from cflags_loader import read_compiler_dflags_lib, get_cpp_dflags
from c_preprocessing import preprocess

def parse_text(text):
    parser = c_parser.CParser()
    ast = parser.parse(text, filename="<none>")

    return ast

def get_ast(grouprep, filename, include_paths, cpp_flags_file):
    dflags = read_compiler_dflags_lib(cpp_flags_file)

    grouprep_dflags = get_cpp_dflags(grouprep, dflags)
    text = preprocess(filename, grouprep_dflags, include_paths)
    return parse_text(text)

def get_asts_for_all_files(grouprep, include_paths, source_files, cpp_flags_file):
    return dict(
        print(f"Processing {filename}")
        or (filename, get_ast(grouprep, filename, include_paths, cpp_flags_file))
        for filename in source_files
    )

def ast_rec_iterator(node, tag="", level=0):
    yield tag, node
    if hasattr(node, "children"):
        for tag, child in node.children():
            yield from ast_rec_iterator(child, tag, level + 1)

def find_function_definitions(ast):
    # Function definitions should be unique.
    decl_list = [(node.decl.name, node.decl.storage, node)
        for _, node in ast_rec_iterator(ast)
        if isinstance(node, c_ast.FuncDef)]

    names =  [ name for name,storage,node in decl_list ]
    for name in set(names):
        count = names.count(name)
        assert count == 1, f"Function {name} is declared {count} times!"
    return decl_list

def find_function_call_names(ast):

    fcalls = set(
        node.name.name
        for _, node in ast_rec_iterator(ast)
        if isinstance(node, c_ast.FuncCall)
    )

    ids =  set(
        node.name
        for _, node in ast_rec_iterator(ast)
        if isinstance(node, c_ast.ID)
    )

    return set.union(ids,fcalls)


# test_dbmanager.py

import unittest
import os
from binance import enums as k_binance

from src.pp_order import Order, OrderStatus
from polaris_old.pp_dbmanager import DBManager

TEST_DATABASE = 'test.db'

class TestDBManager(unittest.TestCase):
    def setUp(self) -> None:
        try:
            os.remove(TEST_DATABASE)
        except IOError as e:
            print(e)
        print('cwd: ', os.getcwd())

        self.dbm = DBManager(db_name=TEST_DATABASE, order_tables=['orders'])

        self.order = Order(
            session_id='S_20210501_2008',
            order_id='ORDER_ID',
            pt_id='PT_ID',
            k_side=k_binance.SIDE_BUY,
            price=50_000.0,
            amount=1.0,
        )
        self.order_2 = Order(
            session_id='S_20210501_2008',
            order_id='OR_000001',
            pt_id='PT_000001',
            k_side=k_binance.SIDE_SELL,
            price=60_000.88,
            amount=1.0876548765,
            uid='0123456789abcdef'
        )

    def test_get_table_creation_query(self):
        print(self.dbm.get_table_creation_query(table='orders'))

    def test_add_order(self):
        table = 'orders'
        self.dbm.add_order(table=table, order=self.order)
        c = self.dbm.conn.cursor()
        rows = c.execute(f'SELECT * FROM {table};').fetchall()
        self.assertEqual(1, len(rows))
        row = rows[0]
        self.assertEqual(OrderStatus.MONITOR.name, row[8])

    def test_delete_order(self):
        table = 'orders'
        self.dbm.add_order(table=table, order=self.order)
        self.dbm.add_order(table=table, order=self.order_2)
        self.dbm.delete_order(table=table, order=self.order)
        c = self.dbm.conn.cursor()
        rows = c.execute(f'SELECT * FROM {table};').fetchall()
        self.assertEqual(1, len(rows))
        self.assertEqual('0123456789abcdef', rows[0][0])

    def tearDown(self) -> None:
        self.dbm.conn.close()