from rect import Rect

class PackNode(object):
    """
    Creates an area which can recursively pack smaller areas into itself.
    """
    def __init__(self, xywh):
        self.rect = Rect(xywh)

    def __repr__(self):
        return "<%s %s>" % (self.__class__.__name__, self.rect)

    def insert(self, xywh):
        """
        Insert an rect into the current rect. Returns a new Node representing
        the new rect.
        Returns None if no space is available for the new rect.
        """
        if hasattr(self, 'children'):
            for child in self.children:
                r = child.insert(xywh)
                if r is not None:
                    return r
            return None

        rect = Rect(xywh)
        if self.rect.fits(rect):
            a = PackNode((self.rect.left+rect.width, self.rect.bottom, self.rect.width-rect.width, rect.height))
            b = PackNode((self.rect.left, self.rect.bottom+rect.height, self.rect.width, self.rect.height-rect.height))
            self.children = [a,b]
            return PackNode((self.rect.left, self.rect.bottom, rect.width, rect.height))

def pack(big_rect, rects, padding=1):
    """
    Packs a list of rects (or w,h pairs) into a larger containing rect,
    with optional padding. Returns list of new rects.
    Raises ValueError if big_rect is too small.
    """
    input_order = []
    rects = [ Rect(i) for i in rects]

    #sort into optimal insert order
    for r in rects:
        input_order.append((-r.height, -r.width,  r))
    input_order.sort()

    #pack em in
    rmap = {}
    tree = PackNode(big_rect)
    for h,w, rect in input_order:
        uv = tree.insert((rect.width+padding,rect.height+padding))
        if uv is None: raise ValueError('Pack size too small.')
        uv.rect.width -= padding
        uv.rect.height -= padding
        uv.rect.name = rect.name
        rmap[rect] = uv.rect

    #generate coords
    coords = []
    for r in rects:
        coords.append(rmap[r])
    return coords


from base64 import encodebytes
from flask import Flask, request, jsonify, make_response,send_file
from werkzeug.serving import WSGIRequestHandler
from datasetCreation import createDatasetFolder
from preprocessingEmbedding import preprocess
from trainingFaceML import training
import Video
import threading
# class StoppableThread(threading.Thread):
#     """Thread class with a stop() method. The thread itself has to check
#     regularly for the stopped() condition."""
#
#     def __init__(self,  *args, **kwargs):
#         super(StoppableThread, self).__init__(*args, **kwargs)
#         self._stop_event = threading.Event()
#
#     def stop(self):
#         self._stop_event.set()
#
#     def stopped(self):
#         return self._stop_event.is_set()
#
#     def run(self):
#         Video.captureFrames()
class myThread2 (threading.Thread):
   def __init__(self):
      threading.Thread.__init__(self)
   def stop(self):
       Video.stop()
   def run(self):
       Video.captureFrames()
Thread2 = myThread2()
# Thread2 = StoppableThread()
WSGIRequestHandler.protocol_version = "HTTP/1.1"
app = Flask(__name__)
isDatasetCreated=False
app.config['FLASK_ENV'] = 'development'
import io
from PIL import Image
@app.route('/captureFrames', methods=['POST'])
def captureFrame():
    if not request.json:
        return "not found", 400
    else:
        global Thread2
        if 'start' in request.json:

           Video.start()
           Thread2.start()
           return jsonify({'status':'started'})
        elif 'stop' in request.json:
            Video.stop()
            Thread2 = myThread2()
            return jsonify({'status': 'stopped'})

@app.route('/createDataset', methods=['POST'])
def createDataset():
    if not request.json:
        return "not found", 400
    else:
        if 'name' in request.json:

            name = request.json['name']
            createDatasetFolder(name)
            Thread = myThread()
            Thread.start()
            filePath='dataset/'+name+'/'
            result = [filePath+'00000.png',filePath+'00019.png',filePath+'00039.png',
                          filePath+'00059.png',filePath+'00079.png',filePath+'00099.png',
                          filePath+'00119.png',filePath+'00139.png',filePath+'00159.png',
                          filePath+'00179.png']
            encoded_imges = []
            for image_path in result:
             encoded_imges.append(get_response_image(image_path))
            return jsonify({'result': encoded_imges,'status':'done'})

def get_response_image(image_path):
    pil_img = Image.open(image_path, mode='r') # reads the PIL image
    byte_arr = io.BytesIO()
    pil_img.save(byte_arr, format='PNG') # convert the PIL image to byte array
    encoded_img = encodebytes(byte_arr.getvalue()).decode('ascii') # encode as base64
    return encoded_img

class myThread (threading.Thread):
   def __init__(self):
      threading.Thread.__init__(self)

   def run(self):
       preprocess()
       training()
       print('done')


from __future__ import absolute_import

import mock
from st2common.runners.base import ActionRunner
from st2tests.base import RunnerTestCase
from winrm_runner import winrm_ps_command_runner
from winrm_runner.winrm_base import WinRmBaseRunner

class WinRmPsCommandRunnerTestCase(RunnerTestCase):
    def setUp(self):
        super(WinRmPsCommandRunnerTestCase, self).setUpClass()
        self._runner = winrm_ps_command_runner.get_runner()

    def test_init(self):
        runner = winrm_ps_command_runner.WinRmPsCommandRunner("abcdef")
        self.assertIsInstance(runner, WinRmBaseRunner)
        self.assertIsInstance(runner, ActionRunner)
        self.assertEqual(runner.runner_id, "abcdef")

    @mock.patch("winrm_runner.winrm_ps_command_runner.WinRmPsCommandRunner.run_ps")
    def test_run(self, mock_run_ps):
        mock_run_ps.return_value = "expected"

        self._runner.runner_parameters = {"cmd": "Get-ADUser stanley"}
        result = self._runner.run({})

        self.assertEqual(result, "expected")
        mock_run_ps.assert_called_with("Get-ADUser stanley")


from config import *
import requests
import re
from bs4 import BeautifulSoup
import os
import time
import sys
def mkd(ml):
    if os.path.exists(ml) == False:
        try:
            os.mkdir(ml)
        except:
            print("文件夹创建失败,重新创建")
            os.mkdir(ml)
    else:
        print("该文件夹已存在,跳过")
def get_manhua_list(url):
    try:
        req1 = requests.get(url, headers=HEADERS)
        manhua_html = req1.text
        soup = BeautifulSoup(manhua_html, 'lxml')
        ml = soup.select('#Head1 title')
        ml = ml[0].get_text()
        ml = ml.replace(" ", "")
        ml = ml.replace("\n", "")
        ml = ml.replace("\r", "")
        ml = ml.lstrip()
        ml = os.getcwd() + "/" + ml
        print("创建文件夹:", ml)
        mkd(ml)
        print("文件夹创建成功")
        os.chdir(ml)
        print("进入文件夹:", ml)
        manhua_list_url = soup.select('.l_s')
        return manhua_list_url
    except:
        print('失败:重新获取漫画目录请求链接')
        get_manhua_list(url)
def get_list_url(manhua_list_url):
    try:
        for i in reversed(manhua_list_url):
            list_url_title = i['title']
            list_url_href = i['href']
            list_url_href = 'http://www.hhimm.com' + list_url_href

            yield  {
                'list_url_title' : list_url_title,
                'list_url_href' : list_url_href
            }
    except:
        print('失败:重新获取完整的目录链接')
        get_list_url(manhua_list_url)
def manhua_get_page(list_url_href):
    try:
        req2 = requests.get(list_url_href, headers=HEADERS)
        manhua_image_html = req2.text
        soup = BeautifulSoup(manhua_image_html, 'lxml')
        for i in soup.select('.cH1 b'):
            manhua_total_page = (i.get_text())[-2:]
            return manhua_total_page
    except:
        print('失败:重新获取漫画章节的图片数量')
        manhua_get_page(list_url_href)
def get_image_url(list_url_href_page):
    try:
        req3 = requests.get(list_url_href_page, headers=HEADERS)
        get_image_html = req3.text
        soup = BeautifulSoup(get_image_html, 'lxml')
        image_source = soup.select('#hdDomain')
        source = img_url_analysis(image_source)
        res = re.compile(r'<img id=".*" name="(\w*)".*value="\w*"')
        ff = re.findall(res,get_image_html)
        s = img_url_analysis2(ff)
        img_url = source+s

        return img_url
    except:
        print('失败:重新获取漫画图片的链接')
        get_image_url(list_url_href_page)

def img_url_analysis(image_source):
    try:
        for i in image_source:
            source = i['value']
            arrDS = source.split('|')
            return arrDS[0]
    except:
        img_url_analysis(image_source)
def img_url_analysis2(sss):
    try:
        s = sss[0]
        x = s[(len(s) - 1):]
        w = "abcdefghijklmnopqrstuvwxyz"
        xi = w.index(x) + 1
        sk = s[len(s) - xi - 12 : len(s) - xi - 1]
        s = s[0 : len(s) - xi - 12]
        k = sk[0 : len(sk) - 1]
        f = sk[(len(sk) - 1):]
        for i in range(len(k)):
            s = s.replace(k[i: i + 1], str(i))
        ss = s.split(f)
        s = ""
        for i in range(len(ss)):
            iii = int (ss[i])
            s = s + chr(iii)
        return s
    except:
        img_url_analysis2(sss)
def down(img_url, zz):
    try:
#        proxies = 'http://' + get_proxy()
#        proxy = {'http': proxies}
#        print("使用代理ip:", proxy)
        print("发送图片下载请求")
        req4 = requests.get(img_url, headers=HEADERS , timeout=10)
        print("图片开始下载")
        if not os.path.exists(zz):
            with open(zz, 'wb') as f:
                print('下载图片',zz)
                f.write(req4.content)
        print("图片下载成功")
    except:
        print('下载超时:重新下载图片文件')
        down(img_url, zz)
def main():
    rrr = sys.argv[1]
    manhua_list_url = get_manhua_list(rrr)
    cc = get_list_url(manhua_list_url)
    for i in cc:
        list_url_href = i['list_url_href']
        list_url_title = i['list_url_title']
        total_page = manhua_get_page(list_url_href)
        print("总共:", total_page, "页")
        list_url_href = re.sub(r'/\d+.html', '/PAGE.html', list_url_href)
        print('获取的章节链接为:', list_url_href)
        if os.path.exists(list_url_title) == False:
            os.mkdir(list_url_title)
        print("创建漫画文件夹", list_url_title)
        for i in range(1,int(total_page)+1):
            dd = '/'+str(i)+'.html'
            print('第', i ,"页")
            list_url_href_page = re.sub(r'/PAGE.html', dd, list_url_href) + '&d=0'
#            print(list_url_href_page)
            img_url = get_image_url(list_url_href_page)
            print('图片链接为:', img_url)
            zz = list_url_title + '/' + str(i) + '.jpg'
            time.sleep(2.7)
            down(img_url, zz)
if __name__ == '__main__':
    main()
    print("此漫画全部下载完成!!!!")


from numpy import polymul
from control import tf, tf2ss
from controls.frequency_response import first_order_tf, second_order_tf

zeta_sp = 0.6
omega_sp = 2.77
zeta_ph = 0.1177
omega_ph = 0.0752
a_theta = -11.33
t_theta_1 = 110.011
t_theta_2 = 2.336
k_theta = -3

zeta_dr = 0.0625
omega_dr = 3.47
t_spiral = 226.757
t_roll = 1.188

def den_long():
    poly_sp = second_order_tf(zeta_sp, omega_sp)
    poly_ph = second_order_tf(zeta_ph, omega_ph)
    den = polymul(poly_ph, poly_sp)
    return den

def den_lat_dir():
    poly_dr = second_order_tf(zeta_dr, omega_dr)
    poly_sp = first_order_tf(t_roll)
    poly_rl = first_order_tf(t_spiral)
    den = polymul(poly_rl, polymul(poly_sp, poly_dr))
    return den

def num_theta():
    poly_t_1 = first_order_tf(t_theta_1)
    poly_t_2 = first_order_tf(t_theta_2)
    num = polymul(polymul(poly_t_1, poly_t_2), a_theta*k_theta)
    return num

def sim_setup_theta():
    num = num_theta()
    den = den_long()
    sys = tf2ss(tf(num, den))
    n_axis = 1
    task = [5, 1, 2]
    return sys, n_axis, task


import numpy as np
from XRayCrystalDatabase import util

############################################################
#         Define crystals
############################################################
class Diamond:
    def __init__(self):
        # Define properties for the unit cell
        self.a = 3.567 / 1e10  # um
        self.b = self.a
        self.c = self.a
        self.volume = self.a * self.b * self.c / 4.

        # Atom positions
        self.atom_arrangement = [["C", np.array([0., 0., 0.])],
                                 ["C", np.array([0.25, 0.25, 0.25]) * self.a]]

class Silicon:
    def __init__(self):
        # Define properties for the unit cell
        self.a = 5.43095 / 1e10
        self.b = 5.43095 / 1e10
        self.c = 5.43095 / 1e10
        self.volume = self.a * self.b * self.c / 4.

        # Atom positions
        self.atom_arrangement = [["Si", np.array([0., 0., 0.])],
                                 ["Si", np.array([0.25, 0.25, 0.25]) * self.a]]

class Germanium:
    def __init__(self):
        # Define properties for the unit cell
        self.a = 5.658 / 1e10
        self.b = 5.658 / 1e10
        self.c = 5.658 / 1e10
        self.volume = self.a * self.b * self.c / 4.

        # Atom positions
        self.atom_arrangement = [["Ge", np.array([0., 0., 0.])],
                                 ["Ge", np.array([0.25, 0.25, 0.25]) * self.a]]

############################################################
#      Define functions to analyze the crystals
############################################################
def get_recirpocal_lattice(crystal_type, miller_idx):
    """
    Convert the miller index to the corresponding reciprocal lattice

    :param miller_idx:
    :param crystal_type:
    :return:
    """
    return np.array([miller_idx[0] / crystal_type.a,
                     miller_idx[1] / crystal_type.b,
                     miller_idx[2] / crystal_type.c]) * 2. * np.pi

def get_atomic_plane_distance(crystal_type, miller_idx):
    """

    :param miller_idx:
    :param crystal_type:
    :return:
    """
    distance = 1. / np.sqrt((miller_idx[0] / crystal_type.a) ** 2 +
                            (miller_idx[1] / crystal_type.b) ** 2 +
                            (miller_idx[2] / crystal_type.c) ** 2)
    return distance

def get_chi0_and_chih(crystal_type, xray_energy_kev, miller_idx, temp=293.):
    """
    Get the chi0 and chih value for the specific wave length an the reciprocal lattice.

    :param crystal_type:
    :param miller_idx:
    :param xray_energy_kev:
    :param temp: The temperature in K.
    :return:
    """
    # Get the atomic plane distance
    distance = get_atomic_plane_distance(crystal_type=crystal_type, miller_idx=miller_idx)
    print("The atomic layer distance is {:.2f} A.".format(distance * 1e10))

    # Get reciprocal lattice
    reciprocal_lattice = get_recirpocal_lattice(crystal_type=crystal_type, miller_idx=miller_idx)

    # Find the atom number in the unit cell of this atom
    atom_num = len(crystal_type.atom_arrangement)

    # Create holders for different parameters
    f0_holder = []  # Form factor for wave vector 0
    f_holder = []  # Form factor for wave length lambda
    fp_holder = []  # Real part of the anormalous dispersion
    fpp_holder = []  # Imaginary part of the anormalous dispersion
    sigma_d_holder = []  # Dipole photoelectric cross-section
    db_holder = []  # The Debye-Waller factor
    phase_holder = []  # The Phase for different atoms

    # Loop through the atoms in this unit cell
    for atom_idx in range(atom_num):
        # Find the atom
        atom_type = crystal_type.atom_arrangement[atom_idx][0]
        atom_position = crystal_type.atom_arrangement[atom_idx][1]

        ####################################################
        #   Step 1 Get Debye-Waller factor
        ####################################################
        # Get the Debye-Waller factor
        db_factor = util.get_debye_waller_factor(s=0.5 / distance,
                                                 atom_type=atom_type,
                                                 temp=temp)
        db_holder.append(db_factor)

        ####################################################
        #   Step 2 Get phase
        ####################################################
        # Get the phase
        phase_holder.append(np.exp(1.j * np.dot(reciprocal_lattice, atom_position)))

        ####################################################
        #   Step 3 Get f0, f, fp, fpp and sigma D
        ####################################################
        # Get distance in A for the fitting of the form factor f
        distance_a = distance * 1e10
        f0, f, fp, fpp, sigma_d = util.get_f0_f_fp_fpp_and_sigma_d(atom_type=atom_type,
                                                                   energy_kev=xray_energy_kev,
                                                                   s=0.5 / distance_a)
        f0_holder.append(f0)
        f_holder.append(f)
        fp_holder.append(fp)
        fpp_holder.append(fpp)
        sigma_d_holder.append(sigma_d)

    sanity_check = [f0_holder,
                    f_holder,
                    fp_holder,
                    fpp_holder,
                    sigma_d_holder,
                    db_holder,
                    ]

    ####################################################
    #   Calculate the chi0
    ####################################################
    # Get wave length
    wavelength = 2. * np.pi / util.kev_to_wave_number(energy_kev=xray_energy_kev)
    # print("Volume", crystal_type.volume)

    chi0r = 0.
    for idx in range(atom_num):
        chi0r += f0_holder[idx] + fp_holder[idx]
    chi0r *= - (wavelength ** 2) * util.r0 / np.pi / crystal_type.volume

    chi0i = 0.
    for idx in range(atom_num):
        chi0i += sigma_d_holder[idx]
    chi0i *= - wavelength / (2 * np.pi * crystal_type.volume)

    chi0 = chi0r + 1.j * chi0i

    ####################################################
    #   Calculate the chi0
    ####################################################
    # Get wave length
    wavelength = 2. * np.pi / util.kev_to_wave_number(energy_kev=xray_energy_kev)

    chihr = 0.
    for idx in range(atom_num):
        chihr += (f_holder[idx] + fp_holder[idx]) * phase_holder[idx] * db_holder[idx]
    chihr *= - (wavelength ** 2) * util.r0 / np.pi / crystal_type.volume

    chihi = 0.
    for idx in range(atom_num):
        chihi += sigma_d_holder[idx] * db_holder[idx] * phase_holder[idx]
    chihi *= - wavelength / (2 * np.pi * crystal_type.volume)

    chih_sigma = np.abs(chihr) + 1.j * np.abs(chihi)
    # chih_sigma = chihr - 1.j * chihi

    ####################################################
    #   Calculate chih for pi polarization
    ####################################################
    cos2bragg = 1 - 2 * ((wavelength / (2. * distance)) ** 2)
    chih_pi = chih_sigma * cos2bragg

    return chi0, chih_sigma, chih_pi, sanity_check


class State(object):
    """
    State comprise of the followings:
    board size, initialize state, heuristic value (manhattan) and g value (distance from init state)
    """
    def __init__(self, board_size, state, parent=None, move=None):
        self.board_size = board_size
        self.state = state.split('-')
        self.parent = parent
        self.move = move
        self.time_stamp = 0
        self.cost = 1
        self.h = 0
        self.g = 0

    def change_state(self, zero_state, mid_state, nodes):
        """
        return the new state after swap the zero node with other node
        :param zero_state: comprise of zero_row and zero_col
        :param mid_state: comprise of mid_row and mid_col
        :param nodes: all nodes in the problem
        :return: new state after replacing the nodes
        """
        (zero_row, zero_col) = zero_state
        (swap_row, swap_col) = mid_state
        temp_node = nodes[swap_row * self.board_size + swap_col]
        nodes[swap_row * self.board_size + swap_col] = '0'
        nodes[zero_row * self.board_size + zero_col] = temp_node
        new_state = '-'.join(nodes)
        return new_state

    def get_all_children(self):
        """
        create a list of all potential states of a certain node
        :return: list of states
        """
        all_children = list()
        zero_index = self.state.index('0')
        zero_row = int(zero_index / self.board_size)
        zero_col = zero_index % self.board_size
        # move up
        if zero_row != self.board_size - 1:
            new_state = self.change_state((zero_row, zero_col), (zero_row + 1, zero_col), list(self.state))
            all_children.append(State(self.board_size, new_state, self, 'U'))
        # move down
        if zero_row != 0:
            new_state = self.change_state((zero_row, zero_col), (zero_row - 1, zero_col), list(self.state))
            all_children.append(State(self.board_size, new_state, self, 'D'))
        # move left
        if zero_col != self.board_size - 1:
            new_state = self.change_state((zero_row, zero_col), (zero_row, zero_col + 1), list(self.state))
            all_children.append(State(self.board_size, new_state, self, 'L'))
        # move right
        if zero_col != 0:
            new_state = self.change_state((zero_row, zero_col), (zero_row, zero_col - 1), list(self.state))
            all_children.append(State(self.board_size, new_state, self, 'R'))
        return all_children

    def is_equal(self, state):
        """
        compare between 2 states by their state member
        :param state
        :return: true if both states are equal, else return false
        """
        return self.state == state.state

    def heuristic(self):
        """
        calculate manhattan's distance for this state to the goal state
        :return: value of the heuristic function for h value
        """
        h_value = 0
        # run through all nodes accept 0
        for index, item in enumerate(self.state):
            node_value = int(item)
            if node_value != 0:
                # current values
                cur_row = int(index / self.board_size)
                cur_col = index % self.board_size
                # goal values
                goal_row = int((node_value-1) / self.board_size)
                goal_col = (node_value-1) % self.board_size
                h_value += abs(goal_row - cur_row) + abs(goal_col - cur_col)
        return h_value

    def __lt__(self, other):
        """
        compare two nodes by their g value and h value
        :param other: the node to compare with
        :return: bigger node
        """
        # if value of f is equal return by time_stamp value
        if (self.g + self.h) == (other.g + other.h):
            return self.time_stamp < other.time_stamp
        return (self.g + self.h) < (other.g + other.h)


import argparse
import logging
import os
import sys
from typing import Any
from typing import List
from typing import Optional
from typing import Union

import numpy as np
import torch
from torch.nn.parallel import data_parallel

from funasr.build_utils.build_model_from_file import build_model_from_file
from funasr.build_utils.build_streaming_iterator import build_streaming_iterator
from funasr.datasets.preprocessor import LMPreprocessor
from funasr.fileio.datadir_writer import DatadirWriter
from funasr.torch_utils.device_funcs import to_device
from funasr.torch_utils.forward_adaptor import ForwardAdaptor
from funasr.torch_utils.set_all_random_seed import set_all_random_seed
from funasr.utils import config_argparse
from funasr.utils.cli_utils import get_commandline_args
from funasr.utils.types import float_or_none
from funasr.utils.types import str2bool
from funasr.utils.types import str2triple_str
from funasr.utils.types import str_or_none

def inference_lm(
        batch_size: int,
        dtype: str,
        ngpu: int,
        seed: int,
        num_workers: int,
        log_level: Union[int, str],
        key_file: Optional[str],
        train_config: Optional[str],
        model_file: Optional[str],
        log_base: Optional[float] = 10,
        allow_variable_data_keys: bool = False,
        split_with_space: Optional[bool] = False,
        seg_dict_file: Optional[str] = None,
        output_dir: Optional[str] = None,
        param_dict: dict = None,
        **kwargs,
):
    ncpu = kwargs.get("ncpu", 1)
    torch.set_num_threads(ncpu)

    if ngpu >= 1 and torch.cuda.is_available():
        device = "cuda"
    else:
        device = "cpu"

    # 1. Set random-seed
    set_all_random_seed(seed)

    # 2. Build Model
    model, train_args = build_model_from_file(
        train_config, model_file, None, device, "lm")
    wrapped_model = ForwardAdaptor(model, "nll")
    wrapped_model.to(dtype=getattr(torch, dtype)).to(device=device).eval()
    logging.info(f"Model:\n{model}")

    preprocessor = LMPreprocessor(
        train=False,
        token_type=train_args.token_type,
        token_list=train_args.token_list,
        bpemodel=train_args.bpemodel,
        text_cleaner=train_args.cleaner,
        g2p_type=train_args.g2p,
        text_name="text",
        non_linguistic_symbols=train_args.non_linguistic_symbols,
        split_with_space=split_with_space,
        seg_dict_file=seg_dict_file
    )

    def _forward(
            data_path_and_name_and_type,
            raw_inputs: Union[List[Any], bytes, str] = None,
            output_dir_v2: Optional[str] = None,
            param_dict: dict = None,
    ):
        results = []
        output_path = output_dir_v2 if output_dir_v2 is not None else output_dir
        if output_path is not None:
            writer = DatadirWriter(output_path)
        else:
            writer = None

        if raw_inputs != None:
            line = raw_inputs.strip()
            key = "lm demo"
            if line == "":
                item = {'key': key, 'value': ""}
                results.append(item)
                return results
            batch = {}
            batch['text'] = line
            if preprocessor != None:
                batch = preprocessor(key, batch)

            #  Force data-precision
            for name in batch:
                value = batch[name]
                if not isinstance(value, np.ndarray):
                    raise RuntimeError(
                        f"All values must be converted to np.ndarray object "
                        f'by preprocessing, but "{name}" is still {type(value)}.'
                    )
                # Cast to desired type
                if value.dtype.kind == "f":
                    value = value.astype("float32")
                elif value.dtype.kind == "i":
                    value = value.astype("long")
                else:
                    raise NotImplementedError(f"Not supported dtype: {value.dtype}")
                batch[name] = value

            batch["text_lengths"] = torch.from_numpy(
                np.array([len(batch["text"])], dtype='int32'))
            batch["text"] = np.expand_dims(batch["text"], axis=0)

            with torch.no_grad():
                batch = to_device(batch, device)
                if ngpu <= 1:
                    nll, lengths = wrapped_model(**batch)
                else:
                    nll, lengths = data_parallel(
                        wrapped_model, (), range(ngpu), module_kwargs=batch
                    )
                ## compute ppl
                ppl_out_batch = ""
                ids2tokens = preprocessor.token_id_converter.ids2tokens
                for sent_ids, sent_nll in zip(batch['text'], nll):
                    pre_word = "<s>"
                    cur_word = None
                    sent_lst = ids2tokens(sent_ids) + ['</s>']
                    ppl_out = " ".join(sent_lst) + "\n"
                    for word, word_nll in zip(sent_lst, sent_nll):
                        cur_word = word
                        word_nll = -word_nll.cpu()
                        if log_base is None:
                            word_prob = np.exp(word_nll)
                        else:
                            word_prob = log_base ** (word_nll / np.log(log_base))
                        ppl_out += '    p( {cur} | {pre} ) = {prob} [ {word_nll} ]\n'.format(
                            cur=cur_word,
                            pre=pre_word,
                            prob=round(word_prob.item(), 8),
                            word_nll=round(word_nll.item(), 8)
                        )
                        pre_word = cur_word

                    sent_nll_mean = sent_nll.mean().cpu().numpy()
                    sent_nll_sum = sent_nll.sum().cpu().numpy()
                    if log_base is None:
                        sent_ppl = np.exp(sent_nll_mean)
                    else:
                        sent_ppl = log_base ** (sent_nll_mean / np.log(log_base))
                    ppl_out += 'logprob= {sent_nll} ppl= {sent_ppl}\n\n'.format(
                        sent_nll=round(-sent_nll_sum.item(), 4),
                        sent_ppl=round(sent_ppl.item(), 4)
                    )
                    ppl_out_batch += ppl_out
                    item = {'key': key, 'value': ppl_out}
                    if writer is not None:
                        writer["ppl"][key + ":\n"] = ppl_out
                    results.append(item)

            return results

        # 3. Build data-iterator
        loader = build_streaming_iterator(
            task_name="lm",
            preprocess_args=train_args,
            data_path_and_name_and_type=data_path_and_name_and_type,
            dtype=dtype,
            batch_size=batch_size,
            key_file=key_file,
            preprocess_fn=preprocessor,
            num_workers=num_workers,
        )

        # 4. Start for-loop
        total_nll = 0.0
        total_ntokens = 0
        ppl_out_all = ""
        for keys, batch in loader:
            assert isinstance(batch, dict), type(batch)
            assert all(isinstance(s, str) for s in keys), keys
            _bs = len(next(iter(batch.values())))
            assert len(keys) == _bs, f"{len(keys)} != {_bs}"

            ppl_out_batch = ""
            with torch.no_grad():
                batch = to_device(batch, device)
                if ngpu <= 1:
                    # NOTE(kamo): data_parallel also should work with ngpu=1,
                    # but for debuggability it's better to keep this block.
                    nll, lengths = wrapped_model(**batch)
                else:
                    nll, lengths = data_parallel(
                        wrapped_model, (), range(ngpu), module_kwargs=batch
                    )
                ## print ppl
                ids2tokens = preprocessor.token_id_converter.ids2tokens
                for key, sent_ids, sent_nll in zip(keys, batch['text'], nll):
                    pre_word = "<s>"
                    cur_word = None
                    sent_lst = ids2tokens(sent_ids) + ['</s>']
                    ppl_out = " ".join(sent_lst) + "\n"
                    for word, word_nll in zip(sent_lst, sent_nll):
                        cur_word = word
                        word_nll = -word_nll.cpu()
                        if log_base is None:
                            word_prob = np.exp(word_nll)
                        else:
                            word_prob = log_base ** (word_nll / np.log(log_base))
                        ppl_out += '    p( {cur} | {pre} ) = {prob} [ {word_nll} ]\n'.format(
                            cur=cur_word,
                            pre=pre_word,
                            prob=round(word_prob.item(), 8),
                            word_nll=round(word_nll.item(), 8)
                        )
                        pre_word = cur_word

                    sent_nll_mean = sent_nll.mean().cpu().numpy()
                    sent_nll_sum = sent_nll.sum().cpu().numpy()
                    if log_base is None:
                        sent_ppl = np.exp(sent_nll_mean)
                    else:
                        sent_ppl = log_base ** (sent_nll_mean / np.log(log_base))
                    ppl_out += 'logprob= {sent_nll} ppl= {sent_ppl}\n\n'.format(
                        sent_nll=round(-sent_nll_sum.item(), 4),
                        sent_ppl=round(sent_ppl.item(), 4)
                    )
                    ppl_out_batch += ppl_out
                    utt2nll = round(-sent_nll_sum.item(), 5)
                    item = {'key': key, 'value': ppl_out}
                    if writer is not None:
                        writer["ppl"][key + ":\n"] = ppl_out
                        writer["utt2nll"][key] = str(utt2nll)
                    results.append(item)

            ppl_out_all += ppl_out_batch

            assert _bs == len(nll) == len(lengths), (_bs, len(nll), len(lengths))
            # nll: (B, L) -> (B,)
            nll = nll.detach().cpu().numpy().sum(1)
            # lengths: (B,)
            lengths = lengths.detach().cpu().numpy()
            total_nll += nll.sum()
            total_ntokens += lengths.sum()

        if log_base is None:
            ppl = np.exp(total_nll / total_ntokens)
        else:
            ppl = log_base ** (total_nll / total_ntokens / np.log(log_base))

        avg_ppl = 'logprob= {total_nll} ppl= {total_ppl}\n'.format(
            total_nll=round(-total_nll.item(), 4),
            total_ppl=round(ppl.item(), 4)
        )
        item = {'key': 'AVG PPL', 'value': avg_ppl}
        ppl_out_all += avg_ppl
        if writer is not None:
            writer["ppl"]["AVG PPL : "] = avg_ppl
        results.append(item)

        return results

    return _forward

def inference_launch(mode, **kwargs):
    if mode == "transformer":
        return inference_lm(**kwargs)
    else:
        logging.info("Unknown decoding mode: {}".format(mode))
        return None

def get_parser():
    parser = config_argparse.ArgumentParser(
        description="Calc perplexity",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )

    parser.add_argument(
        "--log_level",
        type=lambda x: x.upper(),
        default="INFO",
        choices=("CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "NOTSET"),
        help="The verbose level of logging",
    )
    parser.add_argument("--output_dir", type=str, required=True)
    parser.add_argument("--gpuid_list", type=str, required=True)
    parser.add_argument(
        "--ngpu",
        type=int,
        default=0,
        help="The number of gpus. 0 indicates CPU mode",
    )
    parser.add_argument("--seed", type=int, default=0, help="Random seed")
    parser.add_argument("--njob", type=int, default=1, help="Random seed")
    parser.add_argument(
        "--dtype",
        default="float32",
        choices=["float16", "float32", "float64"],
        help="Data type",
    )
    parser.add_argument(
        "--num_workers",
        type=int,
        default=1,
        help="The number of workers used for DataLoader",
    )
    parser.add_argument(
        "--batch_size",
        type=int,
        default=1,
        help="The batch size for inference",
    )
    parser.add_argument(
        "--log_base",
        type=float_or_none,
        default=10,
        help="The base of logarithm for Perplexity. "
             "If None, napier's constant is used.",
        required=False
    )

    group = parser.add_argument_group("Input data related")
    group.add_argument(
        "--data_path_and_name_and_type",
        type=str2triple_str,
        action="append",
        required=False
    )
    group.add_argument(
        "--raw_inputs",
        type=str,
        required=False
    )
    group.add_argument("--key_file", type=str_or_none)
    group.add_argument("--allow_variable_data_keys", type=str2bool, default=False)

    group.add_argument("--split_with_space", type=str2bool, default=False)
    group.add_argument("--seg_dict_file", type=str_or_none)

    group = parser.add_argument_group("The model configuration related")
    group.add_argument("--train_config", type=str)
    group.add_argument("--model_file", type=str)
    group.add_argument("--mode", type=str, default="lm")
    return parser

def main(cmd=None):
    print(get_commandline_args(), file=sys.stderr)
    parser = get_parser()
    args = parser.parse_args(cmd)
    kwargs = vars(args)
    kwargs.pop("config", None)

    # set logging messages
    logging.basicConfig(
        level=args.log_level,
        format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
    )
    logging.info("Decoding args: {}".format(kwargs))

    # gpu setting
    if args.ngpu > 0:
        jobid = int(args.output_dir.split(".")[-1])
        gpuid = args.gpuid_list.split(",")[(jobid - 1) // args.njob]
        os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
        os.environ["CUDA_VISIBLE_DEVICES"] = gpuid

    kwargs.pop("gpuid_list", None)
    kwargs.pop("njob", None)
    inference_pipeline = inference_launch(**kwargs)
    return inference_pipeline(kwargs["data_path_and_name_and_type"])

if __name__ == "__main__":
    main()


import numpy as np
import csv
import sys

all_peaks = set()
num_motifs = 0
motif_in = sys.argv[1]

b = True
with open("peak_motifs.bed", 'w') as out:
    with open(motif_in) as fd:
        rd = csv.reader(fd, delimiter="\t")
        for row in rd:
            if b:
                b = False
                continue
            if float(row[8]) <= 0.01:
                all_peaks.add(row[2])
                motif = row[2].split("_")
                motif[2] = str(int(motif[1]) + int(row[4]))
                motif[1] = str(int(motif[1]) + int(row[3]))
                motif[5] = row[5]
                if motif[5] == '+':
                    motif[8] = '0,0,200'
                else:
                    motif[8] = '200,0,0'
                # motif.append(row[9])
                out.write("\t".join(motif) + "\n")
                num_motifs += 1

print("Number of motifs found: ", num_motifs)
print("Distinct peaks with motifs: ", len(all_peaks))
print("Peaks without any motifs: ", 1469 - len(all_peaks))


#!/usr/bin/env python

import subprocess

from flask import Flask, request

__DEFAULT_MESSAGE = 'moo'

__EXE = {
    'say': 'cowsay',
    'think': 'cowthink'
}

__COWS = [
    'beavis.zen',
    'bong',
    'bud-frogs',
    'bunny',
    'cheese',
    'cower',
    'daemon',
    'default',
    'dragon-and-cow',
    'dragon',
    'elephant-in-snake',
    'elephant',
    'eyes',
    'flaming-sheep',
    'ghostbusters',
    'head-in',
    'hellokitty',
    'kiss',
    'kitty',
    'koala',
    'kosh',
    'luke-koala',
    'mech-and',
    'meow',
    'milk',
    'moofasa',
    'moose',
    'mutilated',
    'ren',
    'satanic',
    'sheep',
    'skeleton',
    'small',
    'sodomized',
    'stegosaurus',
    'stimpy',
    'supermilker',
    'surgery',
    'telebears',
    'three-eyes',
    'turkey',
    'turtle',
    'tux',
    'udder',
    'vader-koala',
    'vader',
    'www'
]

__ERROR = 'I\'m sorry, what?\n'

def get_cow(message, verb='say', cowfile='default'):
    if not verb in __EXE:
        raise('Unsupported action')
    if not cowfile in __COWS:
        raise('Unsupported cow')
    p = subprocess.Popen([__EXE[verb], '-f', cowfile, message],
        shell=False, stdout=subprocess.PIPE)
    return p.communicate()[0]

app = Flask(__name__)

@app.route('/cow/<verb>', methods=['GET'])
def cow(verb='say'):
    try:
        message = request.args.get('message', __DEFAULT_MESSAGE)
        if len(message) == 0:
            message = __DEFAULT_MESSAGE
        cowfile = request.args.get('cowfile', 'default')
        return get_cow(message, verb, cowfile)
    except:
        return __ERROR

@app.route('/cows', methods=['GET'])
def list_cows():
    return '%s\n' % '\n'.join(__COWS)

if __name__ == '__main__':
    app.run()


import math
import random
import matplotlib.pyplot as plt
import numpy as np

points =  [(1.0,1.0),(1.0,2.0),(2.0,2.0),(2.0,3.0),(4.0,4.0),(4.0,5.0),(5.0,5.0),(5.0,4.0)]

def getInitialCentroids(centroids):
    for i in range(2):
        centroids.append(random.choice(points))
    print(centroids)
    return centroids

def euclidean(centroid, points):

    ls = []
    print("points: ", points)
    for point in points:
        print(centroid,point)
        t1 = (point[0]-centroid[0])*(point[0]-centroid[0])
        t2 =  (point[1]-centroid[1])*(point[1]-centroid[1])
        print(t1,t2)
        sum1 =  t1+t2
        print(sum1)
        ans =  math.sqrt(sum1)
        ls.append(ans)

    print(ls)
    return ls

centroids = []
# getInitialCentroids(centroids)
centroids = [(1,1),(2,3)]
# ans  = euclidean(centroids[0],points)

def pointsCloserToCentroid(dist1,dist2):
    ls = []
    for i in range(len(dist1)):
        dist_cent_1 = dist1[i]
        dist_cent_2 =  dist2[i]
        if dist_cent_1 < dist_cent_2:
            ls.append(0)
        else:
            ls.append(1)
    print(ls)
    return ls

def getClusters(ls,points):
    cluster1 = []
    cluster2 = []

    for i in range(len(ls)):
        if(ls[i]==0):
            cluster1.append(points[i])
        else:
            cluster2.append(points[i])

    clusters = []
    clusters.append(cluster1)
    clusters.append(cluster2)

    return clusters

dist1 =  euclidean(centroids[0],points)
dist2 =  euclidean(centroids[1],points)
ls = pointsCloserToCentroid(dist1,dist2)
clusters = getClusters(ls,points) # now we have initial clusters

def getCentroids(cluster1,cluster2):
    sum_x = 0
    sum_y = 0
    for point in cluster1:
        sum_x =  sum_x + point[0]
        sum_y =  sum_y + point[1]
    centroid1 = ((sum_x/len(cluster1)) , (sum_y/len(cluster1)))

    sum_x = 0
    sum_y = 0
    for point in cluster2:
        sum_x =  sum_x + point[0]
        sum_y =  sum_y + point[1]
    centroid2 = ((sum_x/len(cluster2)) , (sum_y/len(cluster2)))

    centroids = []
    centroids.append(centroid1)
    centroids.append(centroid2)
    return centroids

for i in range(0,2):
    centroids = getCentroids(clusters[0],clusters[1])
    print(centroids)
    dist1 =  euclidean(centroids[0],points)
    dist2 =  euclidean(centroids[1],points)
    ls = pointsCloserToCentroid(dist1,dist2)
    clusters = getClusters(ls,points)
    print("clusters  :",clusters)


"""
This file contains methods that are needed in all apps.

Author: Michel Peltriaux
Organization: Spatial data infrastructure Rheinland-Pfalz, Germany
Contact: michel.peltriaux@vermkv.rlp.de
Created on: 22.01.19
"""
import hashlib
import urllib
import requests

from collections import OrderedDict
from copy import copy
from Geoportal.settings import DEBUG, INTERNAL_SSL
from searchCatalogue.utils.url_conf import URL_SESSION_WRAPPER, URL_BASE_LOCALHOST
from useroperations.models import Navigation
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
from urllib import parse

def get_navigation_items():
    """ Returns the navigation items from the database

    Returns:
        dict: Contains upper level pagenames with associated lists of subpagenames
    """
    navigation = Navigation.objects.all().order_by('position')
    navigation_dict = OrderedDict()
    navigation = copy(navigation)
    for upper_item in navigation:
        if upper_item.parent is None:
            navigation_dict[upper_item.name] = {
                "parent": upper_item,
                "children": []
            }

    for lower_item in navigation:
        if lower_item.parent is not None:
            navigation_dict[lower_item.parent.name]["children"].append(lower_item)

    return navigation_dict

def write_gml_to_session(session_id: str, lat_lon: dict):
    """ Writes gml data to session

    Args:
        session_id: The php session id
        lat_lon: A dict containing the x and y min and max values
    Returns:
        Nothing
    """
    minx = str(lat_lon.get("minx", -1))
    miny = str(lat_lon.get("miny", -1))
    maxx = str(lat_lon.get("maxx", -1))
    maxy = str(lat_lon.get("maxy", -1))

    post_content = '<FeatureCollection xmlns:gml="http://www.opengis.net/gml"><boundedBy><Box srsName="EPSG:4326">'
    post_content += "<coordinates>" + minx + "," + miny + " " + maxx
    post_content += "," + maxy + "</coordinates></Box>"
    post_content += '</boundedBy><featureMember><gemeinde><title>BBOX</title><the_geom><MultiPolygon srsName="EPSG:'
    post_content += "4326" + '"><polygonMember><Polygon><outerBoundaryIs><LinearRing><coordinates>'
    post_content += minx + "," + miny + " " + maxx + ","
    post_content += miny + " " + maxx + "," + maxy + " "
    post_content += minx + "," + maxy + " " + minx + "," + miny
    post_content += "</coordinates></LinearRing></outerBoundaryIs></Polygon></polygonMember></MultiPolygon></the_geom></gemeinde></featureMember></FeatureCollection>"

    data = {
        "value": post_content
    }

    uri = URL_BASE_LOCALHOST + URL_SESSION_WRAPPER + "?sessionId=" + session_id + "&operation=set&key=GML&" + urllib.parse.urlencode(data)

    response = requests.post(url=uri, data=post_content, verify=INTERNAL_SSL)
    i = 0

def execute_threads(thread_list):
    """ Executes a list of threads

    Args:
        thread_list (list): A list of threads
    Returns: nothing
    """
    for thread in thread_list:
        thread.start()
    for thread in thread_list:
        thread.join()

def print_debug(string: str):
    """ Print only if we are in dev mode!

    Args:
        string:
    Returns:
    """
    if DEBUG:
        print(string)

def resolve_boolean_value(val: str):
    """ Resolve a string which represents a boolean value

    Args:
        val: The value
    Returns:
         True, False or None
    """
    val = val.upper()
    if val == "TRUE":
        return True
    elif val == "FALSE":
        return False
    else:
        return None

def sha256(_input: str):
    """ Creates a sha256 hash from the input

    Args:
        _input (str): A string
    Returns:
         A sha256 hash string
    """
    m = hashlib.sha256()
    m.update(_input.encode("UTF-8"))
    return m.hexdigest()


from flask import Flask, request, jsonify
import click

from game import Game

app = Flask(__name__)
game = Game()

@app.route('/join', methods=['GET','POST'])
def join():
    click.echo('New player joined')

    json = request.get_json()
    players = game.add_player(json['id'], json['name'])
    return jsonify({'player_count': len(players)})

@app.route('/start', methods=['POST'])
def start():
    click.echo('Starting game')

    special_roles = request.get_json()
    players = game.assign_roles(special_roles)
    serialized_players = [player.serialize() for player in players]
    click.echo(serialized_players)
    return jsonify(serialized_players)

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')


from typing import Dict, List, Tuple
import io
import snappy
import os
import rlp

from ethereum_rlp import Body, Header

freezerHeadersTable = "headers"
freezerHashesTable = "hashes"
freezerBodiesTable = "bodies"
freezerReceiptsTable = "receipts"
freezerDifficultiesTable = "diffs"

freezerTableSize = 2 * 1000 * 1000 * 1000
indexEntrySize = 6

class IndexEntry:
    """
        filenum: 2 byte integer
        offset: 4 byte integer
    """

    __slots__ = ("filenum", "offset")

    def __init__(self, b: bytes):
        """ Deserializes binary b into a rawIndex object.  """
        self.filenum = int.from_bytes(b[:2], "big")
        self.offset = int.from_bytes(b[2:6], "big")

    def append(self, b: bytes) -> bytes:
        """ Append indexEntry to bytes """
        offset = len(b)
        return b + self.filenum.to_bytes(2, "big") + self.offset.to_bytes(4, "big")

    def __repr__(self) -> str:
        return f"IndexEntry(filenum={self.filenum} offset={self.offset})"

def bounds(start: IndexEntry, end: IndexEntry) -> Tuple[int, int, int]:
    """ Bounds returns the start and end offsets and the file number of where to read
        the data item marked by the two index entries. The two entries are assumed to be sequential
    """
    if start.filenum != end.filenum:
        # If a piece of data 'crosses' a data-file,
        # it's actually in one piece on the second data-file.
        # We return a zero-indexEntry for the second file as start
        return 0, end.offset, end.filenum
    return start.offset, end.offset, end.filenum

class FreezerTable:
    items: int = 0  # number of items stored in the table

    noCompression: bool
    readonly: bool
    maxFileSize: int = 100 * 1000 * 1000 * 1000
    name: str
    path: str

    head: str = ""  # File descriptor for the data head of the table
    files: Dict[int, str]  # open files
    headId: int = 0  # number of the currently active head file
    tailId: int = 0  # number of the earliest file
    index: str  # file descriptor for the index entry file of the table

    itemOffset: int = 0  # Offset (number of discarded items)

    headBytes: int = 0

    def __init__(self, path: str, name: str, disableSnappy: bool, readonly: bool):
        noCompression = disableSnappy
        if not os.path.isdir(path):
            raise Exception(f"path not found {path}")

        idxName: str
        if noCompression:
            idxName = f"{name}.ridx"
        else:
            idxName = f"{name}.cidx"

        offsets: io.TextIOWrapper
        if readonly:
            offsets = path + "/" + idxName

        self.files = {}
        self.index = offsets
        self.name = name
        self.path = path
        self.noCompression = noCompression
        self.readonly = readonly
        self.repair()

    def repair(self) -> None:
        # Ensure the index i s a multiple of indexEntrySize bytes
        if os.stat(self.index).st_size % indexEntrySize:
            raise Exception(
                f"index is not a multiple of indexEntry Size {indexEntrySize}"
            )

        # Get the file index size
        offsetsSize = os.stat(self.index).st_size

        # Read the index zero to determine which file is the earliest and what item offset to use
        buffer: bytes
        with open(self.index, "rb") as f:
            buffer = f.read(indexEntrySize)
            print(buffer)
        firstIndex = IndexEntry(buffer)
        print("first Index:", firstIndex)
        self.tailId = firstIndex.filenum
        self.itemOffset = firstIndex.offset

        with open(self.index, "rb") as f:
            f.seek(offsetsSize - indexEntrySize)
            buffer = f.read(indexEntrySize)

        lastIndex = IndexEntry(buffer)
        print("last Index:", lastIndex)
        self.head = self.openFile(lastIndex.filenum)
        contentSize = os.stat(self.head).st_size
        if contentSize is None:
            raise Exception("file could not be statted")

        self.headBytes = contentSize
        self.items = self.itemOffset + int(offsetsSize / indexEntrySize - 1)
        self.headId = lastIndex.filenum
        for i in range(lastIndex.filenum):
            self.openFile(i)

    def openFile(self, num: int) -> str:
        """ Doesn't actually open a file, but constructs the correct filename and fills the table
            TODO: change the function name once we are done with the code port
        """
        if num not in self.files:
            name: str
            if self.noCompression:
                name = self.path + "/" + f"{self.name}.%04d.rdat" % num
            else:
                name = self.path + "/" + f"{self.name}.%04d.cdat" % num
            self.files[num] = name
        return self.files[num]

    def Retrieve(self, item: int) -> bytes:
        items = self.RetrieveItems(item, 1, 0)
        return items[0]

    def RetrieveItems(self, start: int, count: int, maxBytes: int) -> List[bytes]:
        diskData, sizes = self.retrieveItems(start, count, maxBytes)
        output = []
        offset = 0  # offset for reading
        outputSize = 0  # size of uncompressed data

        # Slice up the data and decompress
        for (i, diskSize) in enumerate(sizes):
            item = diskData[offset : offset + diskSize]
            offset += diskSize
            decompressedSize = diskSize
            data: bytes
            # check the length first
            if not self.noCompression:
                data = snappy.decompress(item)
                decompressedSize = len(data)
                # decompressedSize = snappy.DecodeLen(item)
            if i > 0 and (outputSize + decompressedSize) > maxBytes:
                break
            if not self.noCompression:
                output.append(data)
            else:
                output.append(item)
            outputSize += decompressedSize

        return output

    def retrieveItems(
        self, start: int, count: int, maxBytes: int
    ) -> Tuple[bytes, List[int]]:
        """" Reads up to 'count' items from the table. Reads at least one itme, but otherwise avoids
        reading more than maxBytes bytes. Return the (potentially compressed) data, and the sizes
        """
        # Ensure the table is initialized
        if self.index is None or self.head is None:
            raise Exception("table not open")

        itemCount = self.items
        # Ensure the start is written, not deleted from the tail, and that the
        # caller actually wants something
        if itemCount <= start or self.itemOffset > start or count == 0:
            raise Exception("item out of bounds")
        if start + count > itemCount:
            count = itemCount - start

        output: bytearray = bytearray(b"")
        outputSize = 0

        def readData(fileId: int, start: int, length: int, output: bytearray) -> None:
            dataFile = self.files[fileId]
            if dataFile is None:
                raise Exception("missing data file %d", fileId)
            with open(dataFile, "rb") as f:
                f.seek(start)
                output.extend(f.read(length))

        indices = self.getIndices(start, count)

        sizes = []
        totalSize = 0
        readStart = indices[0].offset
        unreadSize = 0

        for (i, firstIndex) in enumerate(indices[:-1]):
            secondIndex = indices[i + 1]
            # Determine the size of the item
            (offset1, offset2, _) = bounds(firstIndex, secondIndex)
            size = int(offset2 - offset1)
            # Crossing a file boundary?
            if secondIndex.filenum != firstIndex.filenum:
                print("boundary indices:", indices)
                # If we have unread data in the first file, we need to do that read now
                if unreadSize > 0:
                    readData(firstIndex.filenum, readStart, unreadSize, output)
                    outputSize += unreadSize
                    unreadSize = 0
                readStart = 0

            if i > 0 and (totalSize + size) > maxBytes:
                # About to break out due ot byte limit being exceeded. We don't
                # read this last item, but we need to do the deferred reads now.
                if unreadSize > 0:
                    readData(secondIndex.filenum, readStart, unreadSize, output)
                    outputSize += unreadSize
                break

            # Defer the read for later
            unreadSize += size
            totalSize += size
            sizes.append(size)
            if i == len(indices) - 2 or totalSize > maxBytes:
                readData(secondIndex.filenum, readStart, unreadSize, output)
                outputSize += unreadSize

        return (bytes(output[:outputSize]), sizes)

    def getIndices(self, _from: int, count: int) -> List[IndexEntry]:
        """ Returns the index entries for the given from-item, covering 'count' items.
        N.B.: The actual number of returned indices for N items will always be N+1 (unless
        an error is returned).
        OBS: This method assumes that the caller has already verified (and/or trimmed) the
        range so that the items are within bounds. If this method is used to read out of bounds,
        it will raise an exception.
        """
        # Apply the table-offset
        _from = _from - self.itemOffset
        # For reading N items, we need N+1 indices
        buffer: bytes
        with open(self.index, "rb") as f:
            f.seek(_from * indexEntrySize)
            buffer = f.read((count + 1) * indexEntrySize)

        offset = 0
        indices = []
        for _ in range(_from, _from + count + 1):
            index = IndexEntry(buffer[offset:])
            offset += indexEntrySize
            indices.append(index)

        if _from == 0:
            # Special case if we're reading the first item in the freezer. We assume that the
            # First item always starts from zero. This means we can use the first item metadata
            # to carry information about the 'global' offset, for the delection-case
            indices[0].offset = 0
            indices[0].filenum = indices[1].filenum

        return indices

class FreezerHashesTable(FreezerTable):
    def __init__(self, ancient_chaindata_path: str):
        super(FreezerHashesTable, self).__init__(
            ancient_chaindata_path, freezerHashesTable, True, True
        )

    def get_hash_by_height(self, height: int) -> bytes:
        return self.Retrieve(height)

class FreezerBodiesTable(FreezerTable):
    def __init__(self, ancient_chaindata_path: str):
        super(FreezerBodiesTable, self).__init__(
            ancient_chaindata_path, freezerBodiesTable, False, True
        )

    def get_body_by_height(self, height: int) -> Body:
        return rlp.decode(self.Retrieve(height), Body)

class FreezerHeadersTable(FreezerTable):
    def __init__(self, ancient_chaindata_path: str):
        super(FreezerHeadersTable, self).__init__(
            ancient_chaindata_path, freezerHeadersTable, False, True
        )

    def get_header_by_height(self, height: int) -> Header:
        return rlp.decode(self.Retrieve(height), Header)


import cv2
import os
import sys
from os import path
from threading import Thread

import numpy as np

from .network import network_table

FORCE_RECORD = 'record' in sys.argv

outputs = None

network_table.putBoolean('Record', False)

def send(feed_img, ir_img, color_img, depth_img):
    global outputs

    image = feed_img
    if network_table.getBoolean('Record', False) or FORCE_RECORD:
        if not outputs:
            four_cc = cv2.VideoWriter_fourcc(*'XVID')
            n = 0
            while not path.isdir('data/capture-{}'.format(n)):
                n += 1
            out_path = 'data/capture-{}'.format(n)
            print('Recording to directory {}'.format(out_path))
            os.mkdir(out_path)

            feed_out = cv2.VideoWriter('{}/feed.avi'.format(out_path))
            ir_out = cv2.VideoWriter('{}/ir.avi'.format(out_path))
            color_out = cv2.VideoWriter('{}/color.avi'.format(out_path))
            depth_out = cv2.VideoWriter('{}/depth.avi'.format(out_path))
            outputs = (feed_out, ir_out, color_out, depth_out)
        else:
            feed_out, ir_out, color_out, depth_out = outputs
            feed_out.write(feed_img)
            ir_out.write(ir_img)
            color_out.write(color_img)
            depth_out.write(depth_img)
    elif outputs != None:
        release()

def release():
    print('Recording stopped')
    if outputs:
        for out in outputs:
            out.release()


import django.contrib.auth.models
from django.shortcuts import render, get_object_or_404
import django.forms as forms
from django.forms.utils import ValidationError
from django.contrib.auth.decorators import login_required
import django.urls
from django.utils.translation import gettext_lazy as _, gettext, ngettext
import django.utils.timezone
from django.conf import settings
from jb_common.utils.base import help_link, send_email, get_really_full_name, format_enumeration
from jb_common.models import Topic
import samples.utils.views as utils
from samples.utils import sample_names
from samples import permissions
from samples.models import Sample, SampleClaim
from samples.views.claim import ReviewerForm
from institute import models

# FixMe: This module contains a lot of code duplication from
# ``samples.views.claim``, which is not bad in itself (for this, it's too
# little), but many translations are doubled.

class SamplesForm(forms.Form):
    samples = forms.CharField(label=_("Samples"), help_text=_("Comma-separated"), widget=forms.widgets.Textarea)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["samples"].widget.attrs.update({"cols": 30, "rows": 5})

    def clean_samples(self):
        sample_names = self.cleaned_data["samples"].split(",")
        valid_names = []
        invalid_names = []
        for name in sample_names:
            name = name.strip()
            if name:
                if sample_names.sample_name_format(name) == "old":
                    if name in valid_names:
                        raise ValidationError(_("The name %(name)s appears more than once."), params={"name": name},
                                              code="invalid")
                    valid_names.append(name)
                else:
                    invalid_names.append(name)
        if invalid_names:
            error_message = ngettext(
                "The name %(invalid_names)s is not valid.", "The names %(invalid_names)s are not valid.",
                len(invalid_names))
            raise ValidationError(error_message, params={"invalid_names": format_enumeration(invalid_names)}, code="invalid")
        if not valid_names:
            raise ValidationError(self.fields["samples"].error_messages["required"], code="required")
        existing_names = [name for name in valid_names if sample_names.does_sample_exist(name)]
        if existing_names:
            # This opens a small security hole because people can find out that
            # confidential samples are existing.  However, such samples mostly
            # have non-oldstyle names anyway.
            error_message = ngettext(
                "The name %(existing_names)s is already existing.", "The names %(existing_names)s are already existing.",
                len(existing_names))
            raise ValidationError(error_message, params={"existing_names": format_enumeration(existing_names)},
                                  code="duplicate")
        return valid_names

class SubstrateForm(forms.ModelForm):

    class Meta:
        model = models.Substrate
        fields = ("material", "comments")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["comments"].widget.attrs.update({"cols": 30, "rows": 5})

    def clean(self):
        cleaned_data = super().clean()
        if "material" in cleaned_data and "comments" in cleaned_data:
            if cleaned_data["material"] == "custom" and not cleaned_data["comments"]:
                self.add_error("comments", ValidationError(_("For a custom substrate, you must give substrate comments."),
                                                           code="invalid"))
        return cleaned_data

@help_link("demo.html#the-actual-claim")
@login_required
def add_oldstyle(request, username):
    """View for adding a new claim to old-style sample names.  This is a nice
    example of a view of the app “samples” which is *extended* in the institute
    app.  The template – in this case,
    :file:`institute/templates/samples/list_claims.html` – overrides and extends the
    default one, and adds a link to a URL listed in institute's URLconf and pointing
    to this view function.

    The important step is the template.  This is the hook for your extensions.
    You override the template from “samples” by creating a file called the same
    in :file:`institute/templates/samples/`.  Because ``DIRS`` and ``loaders``
    are defined as recommended in :doc:`/programming/settings`, it shadows its
    counterpart.  By giving the full path, you can still access the original.
    Thus, you may start your template with

    ::

        {% extends "samples/templates/samples/list_claims.html" %}

    in order to extend it.

    The `username` parameter of this view function is actually superfluous
    because it must be the currently logged-in user anyway.  But this way, we
    don't get into trouble if a user happens to be called ``"add"``.
    Additionally, the URLs become RESTful.

    :param request: the current HTTP Request object
    :param username: the name of the user whose claim this will be; it must be
        the currently logged-in user

    :type request: HttpRequest
    :type username: str

    :return:
      the HTTP response object

    :rtype: HttpResponse
    """
    _ = gettext
    user = get_object_or_404(django.contrib.auth.models.User, username=username)
    if user != request.user:
        raise permissions.PermissionError(request.user, _("You are not allowed to add a claim in another user's name."))
    if request.method == "POST":
        samples_form = SamplesForm(request.POST)
        substrate_form = SubstrateForm(request.POST)
        reviewer_form = ReviewerForm(request.POST)
        if samples_form.is_valid() and substrate_form.is_valid() and reviewer_form.is_valid():
            reviewer = reviewer_form.cleaned_data["reviewer"]
            claim = SampleClaim(requester=user, reviewer=reviewer)
            claim.save()
            _ = lambda x: x
            send_email(_("Sample request from {requester}"),
                       _("""Hello {reviewer},

{requester} wants to become the new “currently responsible person”
of one or more samples.  Please visit

    {url}

for reviewing this request.  If you don't want or cannot approve
the request, please contact {requester} directly and ask him or her
to withdraw the request.

JuliaBase.
"""), reviewer, {"reviewer": get_really_full_name(reviewer), "requester": get_really_full_name(user),
                 "url": request.build_absolute_uri(django.urls.reverse("samples:show_claim", kwargs={"claim_id": claim.pk}))})
            _ = gettext
            samples = []
            nobody = django.contrib.auth.models.User.objects.get(username="nobody")
            legacy = Topic.objects.get(name="Legacy")
            now = django.utils.timezone.now()
            material, substrate_comments = substrate_form.cleaned_data["material"], substrate_form.cleaned_data["comments"]
            for name in samples_form.cleaned_data["samples"]:
                substrate = models.Substrate(operator=nobody, timestamp=now, material=material, comments=substrate_comments)
                substrate.save()
                sample = Sample(name=name, current_location="unknown", currently_responsible_person=nobody, topic=legacy)
                sample.save()
                sample.processes.add(substrate)
                samples.append(sample)
            claim.samples = samples
            return utils.successful_response(request,
                                             _("Sample claim {id_} was successfully submitted.").format(id_=claim.pk),
                                             "samples:show_claim", kwargs={"claim_id": claim.pk})
    else:
        samples_form = SamplesForm()
        substrate_form = SubstrateForm()
        reviewer_form = ReviewerForm()
    return render(request, "samples/add_claim_oldstyle.html", {"title": _("Assert claim"), "samples": samples_form,
                                                               "substrate": substrate_form, "reviewer": reviewer_form})

_ = gettext


from __future__ import division
import cPickle as pickle
import yaml
import os

import numpy as np
import matplotlib.pyplot as plt
import yaml
from pnr.data.utils import (
    pictorialize_team, pictorialize_fast, make_3teams_11players,
    make_reference, scale_last_dim, make_reference_pnr, pictorialize_fast_pnr
)
from pnr.data.constant import game_dir

class ExtractorException(Exception):
    pass

class OneHotException(Exception):
    pass

class BaseExtractor(object):
    """base class for sequence extraction
        Input: a truncated Event
        Output: classifier model input

    Simplest possible parametrization, a collapsed image of the full court
    Options (all 3 can be used in conjunction):
        -d0flip
        -d1flip
        -jitter (x,y)
    returns a 3 channel image of (ball, offense, defense)
            -> resolves possession: which side of the court it's one
    """

    def __init__(self, f_config):
        self.augment = True
        if type(f_config) == str:
            self.config = yaml.load(open(f_config, 'rb'))['extractor_config']
        else:
            self.config = f_config['extractor_config']

    def extract_raw(self, event, dont_resolve_basket=False):
        """
        """
        ##
        moments = event.moments
        off_is_home = event.is_home_possession(moments[len(moments) // 2])
        ball, offense, defense = [[]], [[], [], [], [], []], [[], [], [], [], []]
        for moment in moments:
            ball[0].append([moment.ball.x, moment.ball.y])
            off_id, def_id = 0, 0
            for player_idx, player in enumerate(moment.players):
                if dont_resolve_basket:
                    if player_idx < 5:
                        offense[off_id].append([player.x, player.y])
                        off_id += 1
                    else:
                        defense[def_id].append([player.x, player.y])
                        def_id += 1
                else:
                    if (player.team.id == event.home_team_id) == off_is_home:  # offense
                        offense[off_id].append([player.x, player.y])
                        off_id += 1
                    else:  # defense
                        defense[def_id].append([player.x, player.y])
                        def_id += 1
        if (
                len(ball) == 0 or
                (
                        not (
                            (len(np.array(ball).shape) == 3 and len(np.array(offense).shape) == 3
                            and len(np.array(defense).shape) == 3)
                            and (
                                np.array(ball).shape[1] == np.array(offense).shape[1]
                                and np.array(offense).shape[1] == np.array(defense).shape[1]
                            )
                        )
                )
        ):
            raise ExtractorException()

        return [ball, offense, defense]

    def extract_raw_pnr(self, event, one_hot=False):
        """
        """
        ##
        moments = event.moments
        off_is_home = event.is_home
        ball, ball_player, ball_defense_player, screen_player, screen_defense_player = [], [], [], [], []
        ball_player_vector, ball_defense_player_vector, screen_player_vector, screen_defense_player_vector = None, None, None, None

        try:
            for moment in moments:
                ball.append([moment.ball.x, moment.ball.y])
                for player_idx, player in enumerate(moment.players):
                    if player_idx == 0:
                        ball_player.append([player.x, player.y])
                    elif player_idx == 1:
                        screen_player.append([player.x, player.y])
                    elif player_idx == 2:
                        ball_defense_player.append([player.x, player.y])
                    elif player_idx == 3:
                        screen_defense_player.append([player.x, player.y])
        except Exception:
            raise ExtractorException

        if one_hot:
            for player_idx, player in enumerate(moment.players):
                if player_idx == 0:
                    ball_player_vector = player.one_hot_vector
                elif player_idx == 1:
                    screen_player_vector = player.one_hot_vector
                elif player_idx == 2:
                    ball_defense_player_vector = player.one_hot_vector
                elif player_idx == 3:
                    screen_defense_player_vector = player.one_hot_vector

        if(
            len(ball) == 0 or
            (
                not (
                    len(np.array(ball).shape) == 2 and
                    len(np.array(ball_player).shape) == 2 and
                    len(np.array(ball_defense_player).shape) == 2 and
                    len(np.array(screen_player).shape) == 2 and
                    len(np.array(screen_defense_player).shape) == 2
                )
            )
        ):
            raise ExtractorException()
        if not one_hot:
            return [ball, ball_player, screen_player, ball_defense_player, screen_defense_player]
        else:
            if (
                not (
                    ball_player_vector is not None and
                    screen_player_vector is not None and
                    ball_defense_player_vector is not None and
                    screen_defense_player_vector is not None
                )
            ):
                raise OneHotException()

            return [
                ball,
                ball_player,
                screen_player,
                ball_defense_player,
                screen_defense_player
            ], [
                ball_player_vector,
                screen_player_vector,
                ball_defense_player_vector,
                screen_defense_player_vector
            ]

    def extract(self, event):
        sample_rate = 1
        Y_RANGE = 100
        X_RANGE = 50
        x = self.extract_raw(event)
        ctxy = []
        if self.augment and np.sum(self.config['jitter']) > 0:
            d0_jit = (np.random.rand() * 2 - 1) * self.config['jitter'][0]
            d1_jit = (np.random.rand() * 2 - 1) * self.config['jitter'][1]
            jit = np.array([d0_jit, d1_jit])
            jit = jit.reshape(1, 2).repeat(len(x[0][0]), axis=0)
            for team in x:
                for player in team:
                    try:
                        player = np.array(player) + jit
                    except ValueError:  # bad sequence where not all players have the same number of moments
                        raise ExtractorException()

        for play_sequence in x:
            try:
                team_matrix = np.concatenate(play_sequence, 1)
            except ValueError:
                raise ExtractorException()

            tm = pictorialize_team(team_matrix, sample_rate=sample_rate,
                                   Y_RANGE=Y_RANGE, X_RANGE=X_RANGE)

            ctxy.append(tm)
        ctxy = np.array(ctxy)
        if len(ctxy.shape) == 1:  # different teams have different length
            raise ExtractorException()
        # compress the time dimension
        if 'video' in self.config and self.config['video']:
            if self.augment and self.config['d0flip'] and np.random.rand > .5:
                ctxy = ctxy[:, :, ::-1]
            if self.augment and self.config['d1flip'] and np.random.rand > .5:
                ctxy = ctxy[:, :, :, ::-1]
            return ctxy
        else:
            cxy = ctxy.sum(1)
            cxy[cxy > 1] = 1
            if self.augment and self.config['d0flip'] and np.random.rand > .5:
                cxy = cxy[:, ::-1]
            if self.augment and self.config['d1flip'] and np.random.rand > .5:
                cxy = cxy[:, :, ::-1]
            return cxy

    def extract_batch(self, events_arr, input_is_sequence=False, dont_resolve_basket=False):
        sample_rate = 1
        Y_RANGE = 100
        X_RANGE = 50
        if input_is_sequence:
            sequences = events_arr
        else:
            if self.config['pnr']:
                sequences = np.array([
                        self.extract_raw_pnr(e) for e in events_arr
                ])
            else:
                sequences = np.array([
                    make_3teams_11players(
                        self.extract_raw(e, dont_resolve_basket=dont_resolve_basket)
                    ) for e in events_arr
                ])
        # time crop (+jitter) , spatial crop
        if 'version' in self.config and self.config['version'] >= 2:
            if self.augment:
                t_jit = np.min([self.config['tfa_jitter_radius'], sequences.shape[2] / 2 - self.config['tfr']])
                t_jit = (2 * t_jit * np.random.rand()).round().astype('int32') - t_jit
            else:
                t_jit = 0
            tfa = int(sequences.shape[2] / 2 + t_jit)
            sequences = sequences[:, :, tfa - self.config['tfr']:tfa + self.config['tfr']]
            if 'crop' in self.config and self.config['crop'] != '':
                reference = make_reference(sequences, self.config['crop_size'], self.config['crop'])
                sequences = sequences - reference
                Y_RANGE = self.config['crop_size'][0] + 2
                X_RANGE = self.config['crop_size'][1] + 2
        # spatial jitter
        if self.augment and np.sum(self.config['jitter']) > 0:
            d0_jit = (np.random.rand() * 2 - 1) * self.config['jitter'][0]
            d1_jit = (np.random.rand() * 2 - 1) * self.config['jitter'][1]
            # hacky: can delete after -- temporary for malformed data (i.e.
            # missing player)
            try:
                sequences[:, :, :, 0] += d0_jit
            except:
                raise ExtractorException()
            sequences[:, :, :, 1] += d1_jit
        ##
        bctxy = pictorialize_fast(sequences, sample_rate, Y_RANGE, X_RANGE)

        # if cropped, shave off the extra padding
        if ('version' in self.config and self.config['version'] >= 2 and 'crop' in self.config):
            bctxy = bctxy[:, :, :, 1:-1, 1:-1]
        # compress the time dimension
        if 'video' in self.config and self.config['video']:
            if self.augment and self.config['d0flip'] and np.random.rand > .5:
                bctxy = bctxy[:, :, :, ::-1]
            if self.augment and self.config['d1flip'] and np.random.rand > .5:
                bctxy = bctxy[:, :, :, :, ::-1]
            return bctxy
        else:
            bcxy = bctxy.sum(2)
            bcxy[bcxy > 1] = 1
            if self.augment and self.config['d0flip'] and np.random.rand > .5:
                bcxy = bcxy[:, :, ::-1]
            if self.augment and self.config['d1flip'] and np.random.rand > .5:
                bcxy = bcxy[:, :, :, ::-1]

            # debugging
            # for img in bcxy:
            #     for ind, player in enumerate(img):
            #         plt.subplot(2, 3, ind + 1)
            #         plt.imshow(player)
            #     plt.show()
            #     plt.close()

            return bcxy


#!/usr/bin/python3
import click
import shutil
import os
from m2tools.extension import Extension
from m2tools import package, patcher
import tempfile
import json

import datetime

def patch_meta(meta):
    """
    Update composer.json with extra information
    :param meta:
    :return:
    """
    env_support_email = os.getenv('COMPOSER_SUPPORT_EMAIL') or 'support@example.com'
    env_support_docs = os.getenv('COMPOSER_SUPPORT_DOCS') or 'http://docs.example.com'

    s = getattr(meta, 'support', {})
    s.update({
        'email': env_support_email,
        'docs': env_support_docs
    })
    meta.support = s
    return meta

def make_signature():
    d = datetime.datetime.now()
    env_signature = os.getenv('SOURCES_SIGNATURE') or 'Copyright {date.year}. All rights reserved.\nSee LICENSE.txt for license details.'
    return env_signature.format(date=d)

def make_license():

    return "See {url}".format(url=(os.getenv("LICENSE_URL") or "https://example.com/license.txt"))

@click.group()
@click.pass_context
@click.option('--output', envvar="OUTPUT", default="/results", required=True, type=click.Path())
def cli(ctx, output):
    ctx.obj = {
        'OUTPUT': output
    }

@cli.command()
@click.option('--type', default="marketplace", type=click.Choice(["marketplace"]))
@click.argument('path')
@click.argument('output', envvar="OUTPUT", required=True, type=click.Path())
def pack(type, path, output):
    """
    Create marketplace package
    """
    with Extension(path) as e, tempfile.TemporaryDirectory() as tmpdir:

        module_name = e.meta.name.split('/')[1]
        dest_dir = os.path.join(tmpdir, module_name)
        shutil.copytree(e.path, dest_dir)

        with Extension(dest_dir) as patched_ext:
            patched_ext.init_from_path(dest_dir)
            patched_ext.meta = patch_meta(e.meta)

            # Patch composer.json
            with open(os.path.join(patched_ext.path, 'composer.json'), 'w') as cjs:
                json.dump(dict(patched_ext.meta), cjs, indent=4)

            _sign = make_signature()

            p = patcher.Patcher(patched_ext.path)
            p.patch_files(lambda x: patcher.PhpCode(x).sign(_sign),
                                match=lambda x: x.endswith('.php') or x.endswith('.phtml'))
            p.patch_files(lambda x: patcher.JsCode(x).sign(_sign), match=lambda x: x.endswith('.js'))
            p.patch_files(lambda x: patcher.XmlCode(x).sign(_sign), match=lambda x: x.endswith('.xml'))

            with open(os.path.join(patched_ext.path, 'LICENSE.txt'), 'w') as lf:
                lf.write(make_license())

            package.marketplace(patched_ext, output)

@cli.command()
@click.argument('path', type=click.Path(exists=True))
@click.argument('output', envvar="OUTPUT", required=False, type=click.File('w'))
@click.option('--format', default="{meta.name}-{meta.version}", help="Output format")
def info(path, output, format):
    """
    Read extension info from path or zip
    """
    with Extension(path) as e:
        info = format.format(meta=e.meta, path=e.path).replace('\\n', '\n')
    if output:
        output.write(info)
        return
    click.echo(info)

if __name__ == '__main__':
    cli()


#program to transform from teme frame to ECI frame using skyfield library

from skyfield.api import EarthSatellite
from skyfield.api import load, wgs84
from sgp4.api import Satrec
from sgp4.api import days2mdhms
from pprint import pprint
import numpy as np
import matplotlib.pyplot as plt
np.set_printoptions(precision=5)
plt.style.use('seaborn')

file1 = open('zarya.txt', 'r')
total_count = 0
s_count = 0
t_count = 0
list_s = []
list_t = []
au = 1.496e+8
day = 86400
km_sc = au/day

while total_count<24:
	# Get next line from file
	line = file1.readline()
	list_s.append(line.strip())
	s_count +=1
	line = file1.readline()
	list_t.append(line.strip())
	t_count +=1
	total_count += 2

	# if line is empty
	# end of file is reached
	if not line:
		break
		print("Line no line")

file1.close()

def norm_v(v):
	return np.sqrt(v[0]**2 + v[1]**2 + v[2]**2)

def cross(a, b):
	cross_vector = []
	v1 = a[1]*b[2] - a[2]*b[1]
	v2 = -(a[0]*b[2] - a[2]*b[0])
	v3 = a[0]*b[1] - a[1]*b[0]

	cross_vector.append(v1)
	cross_vector.append(v2)
	cross_vector.append(v3)

	return cross_vector

position_teme= []
velocity_teme = []

position_eci = []
velocity_eci = []

position_ric= []
velocity_ric = []

error_pos = []
error_vel =[]

time = []

for i in range(s_count):
	s = list_s[i]
	t = list_t[i]

	ts = load.timescale()
	satellite = EarthSatellite(s, t, 'ISS (ZARYA)')
	satellite_ =  Satrec.twoline2rv(s, t)

	print ("..................................")
	print(satellite)
	#print(satellite.epoch.tt)
	#print(satellite_.epochyr)

	jd_ = satellite_.jdsatepoch
	a = satellite_.epochdays
	d = int(a)
	fr_ = a - d
	#print(jd_)
	#print(fr_)
	e, r, v = satellite_.sgp4(jd_, fr_)
	time.append(a)

	#print(..)
	#t = satellite.epoch.tt
	#geocentric = satellite.at(t)
	#print(geocentric.position.km)

	month, day, hour, minute, second = days2mdhms(satellite_.epochyr, satellite_.epochdays)
	year = 0
	if satellite_.epochyr>1999:
		year = 2000 + satellite_.epochyr
	else:
		year = 1900 + satellite_.epochyr
	t = ts.utc(year, month, day, hour, minute, int(second))
	#print(year)
	#print(month)
	#print(day)
	#print(hour)
	#print(minute)
	#print(second)
	geocentric = satellite.at(t)
	#pprint(vars(geocentric))
	#print(r)
	r_ = geocentric.position.km
	v_ = geocentric.velocity
	# v_ *= k
	#print(r_)
	#pprint(r_[0])
	#print(v)
	#pprint(vars(v_))
	#print(v_)
	#print(v_.au_per_d[0]*km_sc)

	#print(v_.info)

	reci = np.array([r_[0], r_[1], r_[2]])
	veci = np.array([v_.au_per_d[0]*km_sc, v_.au_per_d[1]*km_sc, v_.au_per_d[2]*km_sc])

	print("position in TEME: " + str(r))
	print("velocity in TEME: " + str(v))
	pos_t = norm_v(r)
	vel_t = norm_v(v)
	position_teme.append(pos_t)
	velocity_teme.append(vel_t)

	print("position in ECI: " + str(reci))
	print("veclocity in ECI: " + str(veci))
	pos_e = norm_v(reci)
	vel_e = norm_v(veci)
	position_eci.append(pos_e)
	velocity_eci.append(vel_e)

	r_u = reci/norm_v(reci)
	r_w = cross(reci, veci)/norm_v(cross(reci, veci))
	r_v = cross(r_w, r_u)/norm_v(cross(r_w, r_u))

	r_u = r_u[..., None]
	r_w = r_w[..., None]
	r_v = r_v[..., None]

	T = np.zeros((3,3))
	T[0][0] = r_u[0]
	T[1][0] = r_u[1]
	T[2][0] = r_u[2]

	T[0][1] = r_v[0]
	T[1][1] = r_v[1]
	T[2][1] = r_v[2]

	T[0][2] = r_w[0]
	T[1][2] = r_w[1]
	T[2][2] = r_w[2]

	r_ric = np.dot(T, reci)
	v_ric = np.dot(T, veci)

	print("positon in RIC: " + str(r_ric))
	print("velocity in RIC: " + str(v_ric))
	pos_r = norm_v(r_ric)
	vel_r = norm_v(v_ric)
	position_ric.append(pos_r)
	velocity_ric.append(vel_r)

	err_p = r_ric - reci
	err_v = v_ric - veci
	error_pos.append(err_p)
	error_vel.append(err_v)
	#print(T)

colors=['red', 'blue', 'green']

fig1, (ax1) = plt.subplots(nrows=1, ncols=1)
fig2, (ax2) = plt.subplots(nrows=1, ncols=1)
fig3, (ax3) = plt.subplots(nrows=1, ncols=1)
fig4, (ax4) = plt.subplots(nrows=1, ncols=1)

ax1.plot(time, position_teme, label='TEME' ,color=colors[0], linestyle='--', marker='o')
ax1.plot(time, position_eci, label='ECI' ,color=colors[1], linestyle='--', marker='o')
ax1.plot(time, position_ric, label='RIC' ,color=colors[2], linestyle='--', marker='o')
ax2.plot(time,  velocity_teme, label='TEME' ,color=colors[0], linestyle='--', marker='o')
ax2.plot(time,  velocity_eci, label='ECI' ,color=colors[1], linestyle='--', marker='o')
ax2.plot(time,  velocity_ric, label='RIC' ,color=colors[2], linestyle='--', marker='o')

ax3.plot(time, error_pos, linestyle='-', marker='o')
ax4.plot(time, error_vel, linestyle='-', marker='o')

ax1.legend()
ax1.set_xlabel('epoch_day')
ax1.set_ylabel('position')

ax2.legend()
ax2.set_xlabel('epoch_day')
ax2.set_ylabel('velocity')

ax3.set_xlabel('epoch_day')
ax3.set_ylabel('err_pos')

ax4.set_xlabel('epoch_day')
ax4.set_xlabel('err_vel')
plt.tight_layout()

plt.show()


import numpy as np

from kivy.app import App
from kivy.graphics import Color
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.graphics.vertex_instructions import Line

class Graph(GridLayout):
    def __init__(self, **kwargs):
        super(Graph, self).__init__()
        self.cols = 2
        self.rows = 2

        self.interface = kwargs.get('interface')
        self.scale_factor = 10
        # print(kwargs)
        # self.data_points = kwargs.get('data_points')
        self.data_points = [100, 100, 200, 200, 300, 300]
        # self.draw_graph()

        self.graph = GridLayout(size_hint=(0.92, 0.9))
        self.y_axis = GridLayout(cols=1,
                                 size_hint=(0.08, 0.9))
        self.x_axis = GridLayout(rows=1,
                                 size_hint=(0.92, 0.1))

        self.origin = BoxLayout(size_hint=(0.08, 0.1))
        self.origin.add_widget(Label(text='Origin',
                                     halign='center',
                                     valign='center',
                                     size_hint=(1, 1)))

        self.add_widget(self.y_axis)
        self.add_widget(self.graph)
        self.add_widget(self.origin)
        self.add_widget(self.x_axis)

    def shift_data_points_to_local(self):
        new_dp = []

        for i in range(0, len(self.data_points), 2):
            new_x = self.data_points[i] + self.graph.pos[0]
            new_y = self.data_points[i + 1] + self.graph.pos[1]
            new_dp.append(new_x)
            new_dp.append(new_y)

        self.data_points = new_dp

    def draw_axes(self):
        x = self.graph.pos[0]
        y = self.graph.pos[1]

        with self.graph.canvas:
            Color(0, 0, 1, 1)
            # y-axis
            Line(points=[x, y, x, y + (self.height - 120)], width=1)
            # x-axis
            Line(points=[x, y, x + (self.width - 200), y], width=1)

    def draw_graph(self):
        # self.graph.canvas.clear()
        self.draw_axes()
        self.shift_data_points_to_local()
        # self.scale_dp()

        with self.graph.canvas:
            Color(1, 0, 0, 1)
            Line(points=self.data_points, width=1)

    # def scale_dp(self):
    #     for i in range(len(self.data_points)):
    #         self.data_points[i] *= self.scale_factor
    #         self.data_points[i] += self.offset[0]
    #         self.data_points[i + 1] += self.offset[1]

class app(App):
    def build(self):
        return Graph()

if __name__ == '__main__':
    app().run()


class CharTree:
  '''
  A class for storing character sequences that are easy to search.

  For example, if we wanted to know if a given text string started with
  one of the following character sequences:

  abcd
  abdd
  abde
  abde
  abff

  We could use the Python strings `find` method. However, this would require
  us to call find up to 5 times. If the text to be searched does not start with 'a',
  then we would not need to look any further.

  So a CharTree is a way of storing character sequences that makes it very easy to
  determine if a given sequence is *not* in the set.
  '''

  def __init__(self,prefix:str=""):
    self.prefix = prefix
    self.is_leaf = False
    self.chars = dict()

  def add_char(self,c):
    '''
    Add a character to this tree's children.

    If the child already exists, does nothing.
    '''
    if c not in self.chars:
      self.chars[c] = CharTree(self.prefix+c)

  def add_sequence(self,s:str) -> None:
    '''
    Add a sequence of characters to the tree.
    '''

    if type(s) is not str:
      raise "Currently, only adding character sequences with a string is supported"

    if len(s) == 0:
      self.is_leaf = True
      return

    self.add_char(s[0])
    self[s[0]].add_sequence(s[1:])

  def __getitem__(self,c):
    if len(c) < 1:
      return None

    if len(c) > 1:
      next = self[c[0]]
      if next is None:
        return next

      return self[c[0]][c[1:]]
    else:
      if c in self.chars:
        return self.chars[c]
      else:
        return None

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

  def get_prefix(self):
    return self.prefix

  def get_string(self,c=None):
    if c is None:
      return self.get_prefix()
    return self.get_prefix() + c

  def match(self,text:str) -> str:
    '''
    Check if any character sequence in the tree matches text.
    '''
    if type(text) is not str:
      raise "Currently, only matching character sequencess against strings is supported."

    if len(text) < 1 or text[0] not in self.chars:
      if self.is_leaf:
        return self.get_string()
      else:
        return None

    # if we are here, then we know that the first char in text is match
    # check if the next char matches
    # if it does, return it
    # if it doesn't, return our text
    return self.chars[text[0]].match(text[1:])