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

# Copyright (c) 2020, Juan B Cabral
# License: BSD-3-Clause
#   Full Text: https://github.com/carpyncho/carpyncho-py/blob/master/LICENSE

# =============================================================================
# DOCS
# =============================================================================

"""Python client for Carpyncho VVV dataset collection.

This code access as a Pandas DataFrame all the data of the web version of
Carpyncho https://carpyncho.github.io/.

"""

__all__ = ["Carpyncho", "CARPYNCHOPY_DATA_PATH"]

__version__ = "0.0.5"

# =============================================================================
# IMPORTS
# =============================================================================

import os
import io
import bz2
import pathlib
import typing as t
import inspect
import hashlib
import functools
import urllib
import json

import attr

import diskcache as dcache

import tqdm

import humanize

import requests

import clize

import pandas as pd

# =============================================================================
# CONSTANTS
# =============================================================================

VERSION = __version__

#: Location of the entire dataset index.
CARPYNCHO_INDEX_URL = "https://raw.githubusercontent.com/carpyncho/carpyncho-py/master/data/index.json"  # noqa

#: Google drive location.
DRIVE_URL = "https://docs.google.com/uc?export=download"

#: Where carpyncho gonna store the entire data.
CARPYNCHOPY_DATA_PATH = pathlib.Path(
    os.path.expanduser(os.path.join('~', 'carpyncho_py_data')))

#: Chunk size when the library are download the big files of Carpyncho.
CHUNK_SIZE = 32768

#: Maximun cache size (10TB)
DEFAULT_CACHE_SIZE_LIMIT = int(1e10)

#: The location of the cache catabase and files.
DEFAULT_CACHE_DIR = CARPYNCHOPY_DATA_PATH / "_cache_"

# =============================================================================
# CACHE ORCHESTRATION
# =============================================================================

def from_cache(
    cache, tag, function, cache_expire,
    force=False, *args, **kwargs
):
    """Simplify cache orchestration.

    Parameters
    ----------
    tag: str
        Normally every function call the cache with their own tag.
        We sugest "module.function" or "module.Class.function"

    function: callable
        The function to be cached

    force: bool (default=False)
        If the vale of the cache must be ignored and re-execute the
        function.

    cache_expire: bool or None
        Time in seconds to expire the function call

    args and kwargs:
        All the parameters needed to execute the function.

    Returns
    -------
    The result of calling the function or the cached version of the same value.

    """
    # start the cache orchestration
    key = dcache.core.args_to_key(
        base=("carpyncho", tag), args=args, kwargs=kwargs, typed=False)

    with cache as c:
        c.expire()

        value = (
            dcache.core.ENOVAL if force else
            c.get(key, default=dcache.core.ENOVAL, retry=True))

        if value is dcache.core.ENOVAL:
            value = function(**kwargs)
            c.set(
                key, value, expire=cache_expire,
                tag=f"carpyncho.{tag}", retry=True)

    return value

# =============================================================================
# CLIENT
# =============================================================================

@attr.s(hash=False, frozen=True)
class Carpyncho:
    """Client to access the *Carpyncho VVV dataset collection*.

    This code access as a Pandas Dataframe all the data of the web version of
    Carpyncho. https://carpyncho.github.io/.

    Parameters
    ----------
    cache : ``diskcache.Cache``, ``diskcache.Fanout``,
        or ``None`` (default: ``None``)
        Any instance of ``diskcache.Cache``, ``diskcache.Fanout`` or
        ``None`` (Default). If it's ``None`` a ``diskcache.Cache``
        istance is created with the parameter
        ``directory = carpyncho.DEFAULT_CACHE_DIR``.
        More information: http://www.grantjenks.com/docs/diskcache
    cache_expire : ``float`` or None (default=``None``)
        Seconds until item expires (default ``None``, no expiry)
        More information: http://www.grantjenks.com/docs/diskcache

    """

    #: Local cache of the carpyncho database.
    cache: t.Union[dcache.Cache, dcache.FanoutCache] = attr.ib()

    #: Default timout of the catalog-cache.
    #: Try to always set to None (default), the catalogs are big and mostly
    #: never change.
    cache_expire: float = attr.ib(default=None, repr=False)

    #: Location of the carpyncho index (usefull for development)
    index_url: str = attr.ib(default=CARPYNCHO_INDEX_URL)

    # =========================================================================
    # ATTRS ORCHESTRATION
    # =========================================================================

    @cache.default
    def _cache_default(self):
        return dcache.Cache(
            directory=DEFAULT_CACHE_DIR, size_limit=DEFAULT_CACHE_SIZE_LIMIT)

    # =========================================================================
    # UTILITIES FOR CHECK THE REMOTE DATA
    # =========================================================================

    def retrieve_index(self, reset):
        """Access the remote index of the Carpyncho-Dataset.

        The index is stored internally for 1 hr.

        Parameters
        ----------
        reset: bool
            If its True the entire cache is ignored and a new index is
            donwloaded and cached.

        Returns
        -------
        dict with the index structure.

        """
        def get_json_data(url):
            parsed = urllib.parse.urlparse(url)
            if parsed.scheme in ("http", "https", "ftp"):
                response = requests.get(
                    url, headers={'Cache-Control': 'no-cache'})
                return response.json()
            with open(url) as fp:
                return json.load(fp)

        return from_cache(
            cache=self.cache,
            tag="get_index",
            function=get_json_data,
            cache_expire=3600,
            force=reset,
            url=self.index_url)

    @property
    def index_(self):
        """Structure of the Carpyncho dataset information as a Python-dict."""
        return self.retrieve_index(reset=False)

    def list_tiles(self):
        """Retrieve available tiles with catalogs as a tuple of str."""
        index = self.index_
        return tuple(k for k in index.keys() if not k.startswith("_"))

    def list_catalogs(self, tile):
        """Retrieve the available catalogs for a given tile.

        Parameters
        ----------
        tile: str
            The name of the tile to retrieve the catalogs.

        Returns
        -------
        tuple of str:
            The names of available catalogs in the given tile.

        Raises
        ------
        ValueError:
            If the tile is not found.

        """
        index = self.index_
        if tile not in index:
            raise ValueError(f"Tile {tile} not found")
        return tuple(index[tile])

    def has_catalog(self, tile, catalog):
        """Check if a given catalog and tile exists.

        Parameters
        ----------
        tile: str
            The name of the tile.
        catalog:
            The name of the catalog.

        Returns
        -------
        bool:
            True if the convination tile+catalog exists.

        """
        cat = self.index_.get(tile, {}).get(catalog)
        return bool(cat)

    def catalog_info(self, tile, catalog):
        """Retrieve the information about a given catalog.

        Parameters
        ----------
        tile: str
            The name of the tile.
        catalog:
            The name of the catalog.

        Returns
        -------
        dict:
            The entire information of the given catalog file. This include
            drive-id, md5 checksum, size in bytes, number of total records,
            etc.

        Raises
        ------
        ValueError:
            If the tile or the catalog is not found.

        """
        index = self.index_

        if tile not in index:
            raise ValueError(f"Tile {tile} not found")

        tile = index[tile]
        if catalog not in tile:
            raise ValueError(
                f"Catalog {catalog} for tile {tile} not found")

        return tile[catalog]

    # =========================================================================
    # THE DOWNLOAD PART
    # =========================================================================

    def _grive_download(self, tile, catalog, driveid, size, md5sum):

        # https://stackoverflow.com/a/39225272
        # https://stackoverflow.com/a/27508615

        # prepare the parameters and download the token
        params = {'id': driveid}
        session = requests.Session()
        response = session.get(
            DRIVE_URL, params=params, stream=True,
            headers={'Cache-Control': 'no-cache'})

        # retrieve the token from gdrive page
        token = None
        for key, value in response.cookies.items():
            if key.startswith('download_warning'):
                token = value
                break

        # if we have token add to the parameters
        if token:
            params['confirm'] = token

        # make the real deal request
        response = session.get(
            DRIVE_URL, params=params, stream=True,
            headers={'Cache-Control': 'no-cache'})

        # progress bar
        pbar = tqdm.tqdm(
            total=size, initial=0, unit='B',
            unit_scale=True, desc=f"{tile}-{catalog}")

        # the file is a bz2 file, we are going to decompress and store
        # the raw parquet data into a BytesIO
        decompressor = bz2.BZ2Decompressor()
        parquet_stream = io.BytesIO()

        # ademas necesitamos fijarnos que el md5 este ok
        file_hash = hashlib.md5()

        # retrive all the data one chunk at the time
        for chunk in response.iter_content(CHUNK_SIZE):
            if not chunk:
                break
            parquet_stream.write(decompressor.decompress(chunk))
            file_hash.update(chunk)
            pbar.update(CHUNK_SIZE)

        # stop the progress bar
        pbar.close()

        # check if the file was download correctly
        if file_hash.hexdigest() != md5sum:
            raise IOError(
                f"'{tile}-{catalog}' incorrect download.\n"
                f"expected: {md5sum}\n"
                f"caclulated: {file_hash.hexdigest()}")

        # read the entire stream into a dataframe
        df = pd.read_parquet(parquet_stream)
        return df

    def get_catalog(self, tile, catalog, force=False):
        """Retrieve a catalog from the carpyncho dataset.

        Parameters
        ----------
        tile: str
            The name of the tile.
        catalog:
            The name of the catalog.
        force: bool (default=False)
            If its True, the cached version of the catalog is ignored and
            redownloaded. Try to always set force to False.

        Returns
        -------
        pandas.DataFrame:
            The columns of the DataFrame changes between the different catalog.

        Raises
        ------
        ValueError:
            If the tile or the catalog is not found.
        IOError:
            If the checksum not match.

        """
        info = self.catalog_info(tile, catalog)
        driveid, size = info["driveid"], info["size"]
        md5sum = info["md5sum"].split()[0].strip().lower()

        df = from_cache(
            cache=self.cache,
            tag="get_catalog",
            function=self._grive_download,
            cache_expire=None,
            force=force,

            # params to _gdrive_download
            tile=tile, catalog=catalog,
            driveid=driveid, size=size, md5sum=md5sum)

        return df

# =============================================================================
# CLI
# =============================================================================

@attr.s(hash=False, frozen=True)
class CLI:
    """Carpyncho console client.

    Explore and download the entire https://carpyncho.github.io/
    catalogs from your command line.

    """

    footnotes = "\n".join([
        "This software is under the BSD 3-Clause License.",
        "Copyright (c) 2020, Juan Cabral.",
        "For bug reporting or other instructions please check:"
        " https://github.com/carpyncho/carpyncho-py"])

    #: Carpyncho client.
    client = attr.ib()

    def get_commands(self):
        methods = {}
        for k in dir(self):
            if k.startswith("_"):
                continue
            v = getattr(self, k)
            if inspect.ismethod(v) and k != "get_commands":
                methods[k] = v
        return methods

    def version(self):
        """Print Carpyncho version."""
        print(VERSION)

    def list_tiles(self):
        """Show available tiles."""
        for tile in self.client.list_tiles():
            print(f"- {tile}")

    def list_catalogs(self, tile):
        """Show the available catalogs for a given tile.

        tile:
            The name of the tile to retrieve the catalogs.

        """
        print(f"Tile {tile}")
        for catalog in self.client.list_catalogs(tile=tile):
            print(f"    - {catalog}")

    def has_catalog(self, tile, catalog):
        """Check if a given catalog and tile exists.

        tile:

        catalog:
            The name of the catalog.

        """
        has = "" if self.client.has_catalog(tile, catalog) else "NO "
        print(f"Catalog '{catalog}' or tile '{tile}': {has}exists")

    def catalog_info(self, tile, catalog):
        """Retrieve the information about a given catalog.

        tile:
            The name of the tile.

        catalog:
            The name of the catalog.

        """
        FORMATTERS = {
            "size": functools.partial(humanize.naturalsize, binary=True),
            "records": humanize.intcomma
        }

        print(f"Catalog {tile}-{catalog}")
        for k, v in self.client.catalog_info(tile, catalog).items():
            fmt = FORMATTERS.get(k, str)
            print(f"    - {k}: {fmt(v)}")

    def download_catalog(self, tile, catalog, *, force=False, out):
        """Retrives a catalog from th Carpyncho dataset collection.

        tile:
            The name of the tile.

        catalog:
            The name of the catalog.

        out:
            Path to store the catalog. The extension of the file detemines
            the format. Options are ".xlsx" (Excel), ".csv",
            ".pkl" (Python pickle) and ".parquet".

        force:
            Force to ignore the cached value and redownload the catalog.
            Try to always set force to False.

        """
        PARSERS = {
            ".xlsx": pd.DataFrame.to_excel,
            ".csv": pd.DataFrame.to_csv,
            ".pkl": pd.DataFrame.to_pickle,
            ".parquet": pd.DataFrame.to_parquet}

        df = self.client.get_catalog(tile, catalog)

        ext = os.path.splitext(out)[-1].lower()
        if ext not in PARSERS:
            raise clize.UserError(f"format '{ext}' not recognized")

        print(f"Writing {out}...")
        parser = PARSERS[ext]
        parser(df, out)

def main():
    """Run the carpyncho CLI interface."""
    cli = CLI(client=Carpyncho())
    commands = tuple(cli.get_commands().values())
    clize.run(
        *commands,
        description=cli.__doc__,
        footnotes=cli.footnotes)

if __name__ == "__main__":
    main()


# %load q01_plot_deliveries_by_team/build.py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

ipl_df = pd.read_csv('data/ipl_dataset.csv', index_col=None)

# Solution
def plot_deliveries_by_team():

    plt.bar(ipl_df['batting_team'].unique(), ipl_df['batting_team'].value_counts())

    plt.title('Number of Deliveries of each team')
    plt.xlabel('Batting Team')
    plt.ylabel('Counts of Deliveries')

    plt.xticks(ipl_df['batting_team'].unique(), rotation=90)

    plt.yticks(ipl_df['batting_team'].value_counts())

    plt.show()

plot_deliveries_by_team()


from pylab import *
from matplotlib import pyplot
import math
import numpy

g = 9.8
S0m=4.1e-4

v_=float(input('please input the inital velocity :'))
theta_=float(input('please input the firing angle :'))
v_wind=float(input('please input the velocity of the wind :'))
w0=float(input('please input the angular velocity :'))

class initial_state:
    def __init__(self, _v = 0, v_wind = 0, _theta = 0):
        _rad = _theta*math.pi/180
        self.vx = _v * math.cos(_rad)
        self.vy = _v * math.sin(_rad)
        self.v_wind = v_wind
        self.vz =0

class flight_state:
    def __init__(self, _x = 0, _y = 0, _z = 0, _vx = 0, _vy = 0, _vz = 0, _t = 0):
        self.x = _x
        self.y = _y
        self.z = _z
        self.vx = _vx
        self.vy = _vy
        self.vz = _vz
        self.t = _t

class cannon1:
    def __init__(self, _fs = flight_state(0, 0, 0, 0, 0), _dt = 0.1):
        self.cannon_flight_state = []
        self.cannon_flight_state.append(_fs)
        self.dt = _dt

    def next_state1(self, current_state):
        v = sqrt((current_state.vx-v_wind) * (current_state.vx-v_wind) + current_state.vy * current_state.vy)
        b2m =0.0039+0.0058/(1+math.exp((v-35)/5))
        pp0 = (1 - 6.5e-3 * current_state.y / 288.15) ** (2.5)
        next_x = current_state.x + current_state.vx * self.dt
        next_vx = current_state.vx - pp0 * b2m * v * (current_state.vx-v_wind) * self.dt
        next_y = current_state.y + current_state.vy * self.dt
        next_vy = current_state.vy - g * self.dt - pp0 * b2m * v * current_state.vy * self.dt
        next_z = current_state.z + current_state.vz * self.dt
        next_vz = current_state.vz
        return flight_state(next_x, next_y,next_z, next_vx, next_vy, next_vz,current_state.t + self.dt)

    def shoot1(self):
        while not(self.cannon_flight_state[-1].y < 0):
            self.cannon_flight_state.append(self.next_state1(self.cannon_flight_state[-1]))

        r = - self.cannon_flight_state[-2].y / self.cannon_flight_state[-1].y
        self.cannon_flight_state[-1].x = (self.cannon_flight_state[-2].x + r * self.cannon_flight_state[-1].x) / (r + 1)
        self.cannon_flight_state[-1].y = 0

    def show_trajectory1(self):
        global x1
        global y1
        global z1
        x1 = []
        y1 = []
        z1 = []
        for fs in self.cannon_flight_state:
            x1.append(fs.x)
            y1.append(fs.y)
            z1.append(fs.z)

class cannon2(cannon1):
    def __init__(self, _fs = flight_state(0, 0, 0, 0, 0,0,0), _dt = 0.1):
        self.cannon_flight_state2 = []
        self.cannon_flight_state2.append(_fs)
        self.dt = _dt

    def next_state2(self, current_state):
        global g, b2m ,S0m
        v = sqrt((current_state.vx-v_wind) * (current_state.vx-v_wind) + current_state.vy * current_state.vy)
        b2m =0.0039+0.0058/(1+math.exp((v-35)/5))
        pp0 = (1 - 6.5e-3 * current_state.y / 288.15) ** (2.5)
        next_x = current_state.x + current_state.vx * self.dt
        next_vx = current_state.vx - pp0 * b2m * v * (current_state.vx-v_wind) * self.dt-S0m* (current_state.vz) *w0* self.dt
        next_y = current_state.y + current_state.vy * self.dt
        next_vy = current_state.vy - g * self.dt - pp0 * b2m * v * current_state.vy * self.dt
        next_z = current_state.z + current_state.vz * self.dt
        next_vz = current_state.vz + S0m* (current_state.vx-v_wind) *w0* self.dt
        return flight_state(next_x, next_y,next_z, next_vx, next_vy, next_vz,current_state.t + self.dt)

    def shoot2(self):
        while not(self.cannon_flight_state2[-1].y < 0):
            self.cannon_flight_state2.append(self.next_state2(self.cannon_flight_state2[-1]))

        r = - self.cannon_flight_state2[-2].y / self.cannon_flight_state2[-1].y
        self.cannon_flight_state2[-1].x = (self.cannon_flight_state2[-2].x + r * self.cannon_flight_state2[-1].x) / (r + 1)
        self.cannon_flight_state2[-1].y = 0

    def show_trajectory2(self):
        global x2
        global y2
        global z2
        x2 = []
        y2 = []
        z2 = []
        for fs in self.cannon_flight_state2:
            x2.append(fs.x)
            y2.append(fs.y)
            z2.append(fs.z)

class cannon3(cannon1):
    def __init__(self, _fs = flight_state(0, 0, 0, 0, 0,0,0), _dt = 0.1):
        self.cannon_flight_state3 = []
        self.cannon_flight_state3.append(_fs)
        self.dt = _dt

    def next_state3(self, current_state):
        global g, b2m ,S0m
        v = sqrt((current_state.vx-v_wind) * (current_state.vx-v_wind) + current_state.vy * current_state.vy)
        b2m =0.0039+0.0058/(1+math.exp((v-35)/5))
        pp0 = (1 - 6.5e-3 * current_state.y / 288.15) ** (2.5)
        next_x = current_state.x + current_state.vx * self.dt
        next_vx = current_state.vx - pp0 * b2m * v * (current_state.vx-v_wind) * self.dt+S0m* (current_state.vy) *w0* self.dt
        next_y = current_state.y + current_state.vy * self.dt
        next_vy = current_state.vy - g * self.dt - pp0 * b2m * v * current_state.vy * self.dt+S0m* (current_state.vx-v_wind) *w0* self.dt
        next_z = current_state.z + current_state.vz * self.dt
        next_vz = current_state.vz
        return flight_state(next_x, next_y,next_z, next_vx, next_vy, next_vz,current_state.t + self.dt)

    def shoot3(self):
        while not(self.cannon_flight_state3[-1].y < 0):
            self.cannon_flight_state3.append(self.next_state3(self.cannon_flight_state3[-1]))

        r = - self.cannon_flight_state3[-2].y / self.cannon_flight_state3[-1].y
        self.cannon_flight_state3[-1].x = (self.cannon_flight_state3[-2].x + r * self.cannon_flight_state3[-1].x) / (r + 1)
        self.cannon_flight_state3[-1].y = 0

    def show_trajectory3(self):
        global x3
        global y3
        global z3
        x3 = []
        y3 = []
        z3 = []
        for fs in self.cannon_flight_state3:
            x3.append(fs.x)
            y3.append(fs.y)
            z3.append(fs.z)

    def show_out1(self):
        matplotlib.pyplot.figure(figsize=(8,6))
        matplotlib.pyplot.plot(x1,y1,label="without the effect of backspin",color="red",linewidth=3,linestyle='--')
        matplotlib.pyplot.plot(x2,y2,label="with the effect of backspin(w in -y direction) ",color="blue",linewidth=3,linestyle='--')
        matplotlib.pyplot.plot(x3,y3,label="with the effect of backspin(w in -z direction) ",color="green",linewidth=3,linestyle='--')
        matplotlib.pyplot.xlabel("x(m)")
        matplotlib.pyplot.title("adiabatic drag baseball with the wind in the front view")
        matplotlib.pyplot.ylabel("y(m)")
        matplotlib.pyplot.legend(loc='best')
        matplotlib.pyplot.show()

    def show_out2(self):
        matplotlib.pyplot.figure(figsize=(8,6))
        matplotlib.pyplot.plot(x1,z1,label="without the effect of backspin",color="red",linewidth=3,linestyle='--')
        matplotlib.pyplot.plot(x2,z2,label="with the effect of backspin(w in -y direction)",color="blue",linewidth=3,linestyle='--')
        matplotlib.pyplot.plot(x3,z3,label="with the effect of backspin(w in -z direction)",color="green",linewidth=3,linestyle='--')
        matplotlib.pyplot.xlabel("x(m)")
        matplotlib.pyplot.title("adiabatic drag baseball with the wind in the vertical view")
        matplotlib.pyplot.ylabel("z(m)")
        matplotlib.pyplot.legend(loc='best')
        matplotlib.pyplot.show()

#给定初速度大小和角度
a = initial_state(v_,v_wind,theta_)
b = cannon1(flight_state(0, 500, 0,a.vx, a.vy,a.vz, 0), _dt = 0.1)
b.shoot1()
b.show_trajectory1()
c = cannon2(flight_state(0, 500, 0,a.vx, a.vy,a.vz, 0), _dt = 0.1)
c.shoot2()
c.show_trajectory2()
d = cannon3(flight_state(0, 500, 0,a.vx, a.vy,a.vz, 0), _dt = 0.1)
d.shoot3()
d.show_trajectory3()
d.show_out1()
d.show_out2()
show()

print("①the maximun range(x) withput the effect of backspin is ")
print(x1[-1])
print("②the maximun range(x) with the effect of backspin(w in -y direction) is ")
print(x2[-1])
print("③the maximun range(x) with the effect of backspin(w in -z direction) is ")
print(x3[-1])
print("④the horizontal displace(z) with the effect of backspin(w in -y direction) is ")
print(z2[-1])


import math
import random
from collections import namedtuple

import torch
import torch.nn as nn
import torch.nn.functional as func
import torch.optim as optim
from kaggle_environments import make
from kaggle_environments.envs.halite.helpers import ShipAction, Board
from torch.utils.tensorboard import SummaryWriter

ship_actions = [
    ShipAction.NORTH, ShipAction.EAST, ShipAction.SOUTH,
    ShipAction.WEST
]
BATCH_SIZE = 4
GAMMA = 0.999
EPS_START = 0.9
EPS_END = 0.05
EPS_DECAY = 200
TARGET_UPDATE = 10
WINDOW_SIZE = 9

Transitions = namedtuple(
    'Transitions',
    ['state', 'action', 'next_state', 'reward']
)
writer = SummaryWriter("./runs/halite_trial_1")

class Encoder:
    def __init__(self):
        self.data = {}

    def fit(self, x):
        for idx, item in enumerate(x):
            one_hot = torch.zeros(len(x), dtype=torch.int64)
            one_hot[idx] = 1
            self.data[item] = one_hot

    def transform(self, item):
        if isinstance(item, str):
            return self.data[getattr(ShipAction, item)]
        return self.data[item]

encoder = Encoder()
encoder.fit(ship_actions)

class ReplayMemory:
    def __init__(self, capacity):
        self.capacity = capacity
        self.memory = []
        self.position = 0

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

    def push(self, state, action, next_state, reward):
        if len(self) < self.capacity:
            self.memory.append(None)
        self.memory[self.position] = Transitions(
            state=state, action=action, next_state=next_state,
            reward=reward
        )
        self.position += 1

    def sample(self, batch_size):
        return random.sample(self.memory, batch_size)

class DeepQNetwork(nn.Module):
    def __init__(self, num_actions):
        super(DeepQNetwork, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, kernel_size=5)
        self.head = nn.Linear(54, num_actions)

    def forward(self, x):
        x = self.conv1(x)
        return self.head(x.view(x.size(0), -1))

def get_state(halites, ships, yards, size):
    halites = torch.tensor(halites).reshape(size, size)
    array_shape = WINDOW_SIZE * 2 + 1
    ship_pos = torch.zeros((array_shape, array_shape))
    yard_pos = torch.zeros((array_shape, array_shape))
    halites_pos = torch.zeros((array_shape, array_shape))

    for ship_id, co_ord in ships.items():
        x1, y1 = co_ord
        for x in range(WINDOW_SIZE, -WINDOW_SIZE - 1, -1):
            for y in range(-WINDOW_SIZE, WINDOW_SIZE + 1):
                x_ = x + x1 if 0 < x + x1 < 21 else (x + x1) % 21
                y_ = y + y1 if 0 < y + y1 < 21 else (y + y1) % 21
                halites_pos[x + WINDOW_SIZE - 1][y + WINDOW_SIZE - 1] = halites[x_][y_]

        for ship_id2, co_ord2 in ships.items():
            x1_dash, y1_dash = co_ord2
            ship_x = x1 - x1_dash + WINDOW_SIZE
            ship_y = y1 - y1_dash + WINDOW_SIZE
            ship_pos[ship_x][ship_y] = 1

        for yard, yard_co_ord in yards.items():
            x1_dash, y1_dash = yard_co_ord
            yard_x = x1 - x1_dash + WINDOW_SIZE
            yard_y = y1 - y1_dash + WINDOW_SIZE
            yard_pos[yard_x][yard_y] = 1

        state = torch.stack([halites_pos, ship_pos, yard_pos])
        x, y, z = state.size()
        return state.reshape(1, x, y, z)

def agent(obs, config):
    size = config.size
    board = Board(obs, config)
    me = board.current_player

    ship_states = {}
    yard_states = {}
    exp_part = math.exp(config.steps / EPS_DECAY)
    eps_threshold = EPS_END + (EPS_START - EPS_END) * exp_part

    sample = random.random()
    for yard in me.shipyards:
        yard_states[yard.id] = yard.position

    for ship in me.ships:
        ship_states[ship.id] = ship.position

    for ship in me.ships:
        if sample > eps_threshold:
            print("Predicting move")
            current_state = get_state(obs['halite'], ship_states, yard_states, size)
            move = policy_net(current_state).argmax().item()
            ship.next_action = ship_actions[move]
        else:
            print("random move")
            ship.next_action = random.choice(ship_actions)

    return me.next_actions, ship_states, yard_states

memory = ReplayMemory(1000)
policy_net = DeepQNetwork(len(ship_actions))
target_net = DeepQNetwork(len(ship_actions))
target_net.load_state_dict(policy_net.state_dict())
target_net.eval()

optimizer = optim.RMSprop(policy_net.parameters())

def optimize_network():
    if len(memory) > BATCH_SIZE:
        batch = memory.sample(BATCH_SIZE)
        batch = Transitions(*zip(*batch))

        state_batch = torch.stack(batch.state)
        action_batch = torch.stack(batch.action).argmax(1).reshape(-1, 1)
        reward_batch = torch.tensor(batch.reward)
        next_states = torch.stack(batch.next_state)

        state_action_values = policy_net(
            state_batch).gather(1, action_batch)
        next_state_values = target_net(next_states).max(1)[0].detach()
        expected_state_action_values = (next_state_values * GAMMA) + reward_batch

        loss = func.smooth_l1_loss(state_action_values,
                                   expected_state_action_values.unsqueeze(1))
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        return loss

def train_loop():
    env = make("halite", debug=True)
    trainer = env.train([None])
    size = env.configuration.size
    step = 0

    for idx in range(1):
        observation = trainer.reset()
        ship_states = {}
        while not env.done:
            env.configuration.steps = step
            my_action, ships, yards = agent(observation,
                                            env.configuration)
            observation = trainer.step(my_action)[0]
            halites = observation["halite"]
            halites = torch.tensor(halites).reshape(size, size)
            array_shape = WINDOW_SIZE * 2 + 1
            ship_pos = torch.zeros((array_shape, array_shape))
            yard_pos = torch.zeros((array_shape, array_shape))
            halites_pos = torch.zeros((array_shape, array_shape))
            for ship_id, co_ord in ships.items():
                x1, y1 = co_ord
                for x in range(WINDOW_SIZE, -WINDOW_SIZE - 1, -1):
                    for y in range(-WINDOW_SIZE, WINDOW_SIZE + 1):
                        x_ = x + x1 if 0 < x + x1 < 21 else (x + x1) % 21
                        y_ = y + y1 if 0 < y + y1 < 21 else (y + y1) % 21
                        halites_pos[x + WINDOW_SIZE - 1][y + WINDOW_SIZE - 1] = halites[x_][y_]

                for ship_id2, co_ord2 in ships.items():
                    x1_dash, y1_dash = co_ord2
                    ship_x = x1 - x1_dash + WINDOW_SIZE
                    ship_y = y1 - y1_dash + WINDOW_SIZE
                    ship_pos[ship_x][ship_y] = 1

                for yard, yard_co_ord in yards.items():
                    x1_dash, y1_dash = yard_co_ord
                    yard_x = x1 - x1_dash + WINDOW_SIZE
                    yard_y = y1 - y1_dash + WINDOW_SIZE
                    yard_pos[yard_x][yard_y] = 1

                state = torch.stack([halites_pos, ship_pos, yard_pos])
                if ship_id in ship_states and len(my_action) > 0:
                    prev_state = ship_states[ship_id]
                    ship_states[ship_id] = state
                    action = encoder.transform(my_action[ship_id])
                    player = observation['player']
                    reward = observation['players'][player][0]
                    memory.push(
                        prev_state, action, state, reward
                    )
                else:
                    ship_states[ship_id] = state
            step += 1
            loss = optimize_network()
            if loss is not None:
                writer.add_scalar(
                    "training_loss",
                    loss,
                    step
                )

if __name__ == '__main__':
    train_loop()


from django.urls import path, include
from . import views

#Pattern starts with /books
urlpatterns = [
    path('', views.books),
    path('/add', views.addBooksPage),
    path('/create', views.createBook),
    path('/<int:bookID>', views.bookPage),
    path('/<int:bookID>/addReview', views.addReview),
    path('/<int:bookID>/<int:reviewID>/delete', views.deleteReview),
    path('/users/<int:userID>', views.users)
]


# --------------------------------------------------------------------------------
# Imports.
# --------------------------------------------------------------------------------

import os
import sys
import codecs

# --------------------------------------------------------------------------------
# Globals.
# --------------------------------------------------------------------------------

IGNORE = ["the", "and"]
MINSIZE = 3
ENCODING = "latin-1"

# --------------------------------------------------------------------------------
# Segmenter class.
# --------------------------------------------------------------------------------

class Segmenter(object):

    # instance variables
    __1grams = None
    __2grams = None

    def __init__(self, fpath_1grams, fpath_2grams=None):
        loader = Loader()
        self.__1grams = loader.load_ngrams(fpath_1grams, 1, min_ngram_size=MINSIZE, ignore=IGNORE)
        if fpath_2grams:
            self.__2grams = loader.load_ngrams(fpath_2grams, 2, min_ngram_size=MINSIZE, ignore=IGNORE)

    def prob(self, *words):
        """
        Get the probability of the specified n-grams.
        Raises ValueError if you pass in an illegal number of n-grams.
        :rtype : float
        """
        if len(words) == 1:
            word = words[0]
            if word in self.__1grams:
                return self.__1grams[word]
            else: return 0.0

        elif len(words) == 2:
            word1 = words[0]
            word2 = words[1]
            if word1 in self.__2grams and word2 in self.__2grams[word1]:
                return self.__2grams[word1][word2]
            else: return 0.0

        else:
            raise ValueError("Invalid number of arguments passed.")

    def segment(self, string):
        """
        Attempt to segment the given string into its English word components.
        Return the string with its components delimited by '-'
        :rtype : str
        """
        segmentation = self.__slice(string, len(string)-1)
        if self.__2grams:
            return self.__combine(segmentation)
        else:
            return segmentation

    def __slice(self, string, degree):

        # base case
        if degree < MINSIZE: return string

        # keep track of the most probable substring in this word. the first character
        # of this string will be at string[pivot].
        pr_string = self.prob(string)
        pr_pivot = 2.0
        pivot = -1

        # find all strings of length degree in this string and check their likelihood
        # record the most probable string in the word, which will start at the value of pivot.
        for i in range(0, len(string)-degree+1):
            substr = string[i:i+degree]
            if substr in self.__1grams:
                pr_substr = self.prob(substr)
                if (pivot == -1 and pr_substr > pr_string) or (pivot > -1 and pr_substr > pr_pivot):
                    pr_pivot = pr_substr
                    pivot = i

        # if no pivot was found, repeat with a lesser degree
        if pivot == -1: return self.__slice(string, degree-1)

        # if there's a pivot, split string into three and repeat on each string
        # concatenate each recursive answer with "-" to delimit segments
        else:
            left = string[:pivot]
            mid = string[pivot:pivot+degree]
            right = string[pivot+degree:]
            lseg = self.__slice(left, len(left)-1)
            mseg = self.__slice(mid, len(mid)-1)
            rseg = self.__slice(right, len(right)-1)
            return "-".join([lseg,mseg,rseg]).lstrip("-").rstrip("-")

    def __combine(self, segmentation):

        segments = segmentation.split("-")
        length = len(segments)
        if length < 2: return segmentation

        # keep merging segments of length 1
        while True:

            # keep track of best merge
            indxToMerge = -1
            bestMergeProb = 0.0

            for i in range(length-1):

                seg1 = segments[i]
                seg2 = segments[i+1]
                if len(seg1) > MINSIZE and len(seg2) > MINSIZE: continue
                merger = seg1 + seg2
                if merger not in self.__1grams: continue
                prob = self.prob(merger)
                if prob > bestMergeProb:
                    indxToMerge = i
                    bestMergeProb = prob

            if indxToMerge == -1:
                return segmentation
            else:
                merger = segments[indxToMerge] + segments[indxToMerge+1]
                segments = segments[:indxToMerge] + [merger] + segments[indxToMerge+2:]
                return "-".join(segments)

# --------------------------------------------------------------------------------
# IO.
# --------------------------------------------------------------------------------

# --------------------------------------------------------------------------------
# Functions.
# --------------------------------------------------------------------------------

class Loader(object):

    def load_ngrams(self, fname, degree, min_ngram_size=None, ignore=[]):
        words = dict()
        count = 0
        with codecs.open(fname, "r", ENCODING) as f:

            for line in f:

                # ngram
                line = line.split("\t")
                ngram = line[0]
                freq = int(line[1])

                # split ngram into its tokens
                # check validity
                ngrams = ngram.split(" ")
                if len(ngrams) != degree: continue
                if min_ngram_size:
                    for n in ngrams:
                        if len(n) < min_ngram_size: continue
                        if n in ignore: continue

                # add to words
                curr_dict = words
                for ngram in ngrams[:-1]:
                    if ngram not in curr_dict:
                        curr_dict[ngram] = dict()
                    curr_dict = curr_dict[ngram]

                # add to the curr_dict
                ngram = ngrams[-1]
                if ngram in curr_dict: curr_dict[ngram] = curr_dict[ngram] + freq
                else: curr_dict[ngram] = freq
                count += freq

            # normalise everything
            self.recursive_norm(words, count)

        return words

    def recursive_norm(self, words, count):
        for key in words:
            if isinstance(words[key], int):
                words[key] = words[key] / count
            else:
                self.recursive_norm(words[key], count)

# --------------------------------------------------------------------------------
# Main.
# --------------------------------------------------------------------------------

def main():

    # check arguments have been supplied
    if len(sys.argv) == 1:
        print("Error: no arguments supplied.")
        print("Usage: segmenter.py arg1 arg2 ... ")
        print("Where arg1, arg2, ... are the strings you want to segment.")
        sys.exit(0)

    # check existence of 1grams
    if not os.path.isfile("1grams.txt"):
        print("Error: could not find 1grams.txt.")
        print("Exiting....")
        sys.exit(1)
    ngrams1 = "1grams.txt"

    # check existence of 2grams
    ngrams2 = "2grams.txt"
    if not os.path.isfile(ngrams2):
        print("Could not find 2grams file")
        print("Segmenter will run without use of 2grams")
        ngrams2 = None

    # make segmenter, display output
    seg = Segmenter(ngrams1, fpath_2grams=ngrams2)
    for word in sys.argv[1:]:
        print(word + "     ---->     ", end="")
        segmentation = seg.segment(word)
        print(segmentation)

if __name__ == "__main__": main()


import json, sys
from collections import OrderedDict
import re

from PyQt5 import QtWidgets as QtW

def addHLine():
    toto = QtW.QFrame()
    toto.setFrameShape(QtW.QFrame.HLine)
    toto.setFrameShadow(QtW.QFrame.Sunken)
    return toto

def addVLine():
    toto = QtW.QFrame()
    toto.setFrameShape(QtW.QFrame.VLine)
    toto.setFrameShadow(QtW.QFrame.Sunken)
    return toto

##############################

def load_game_data(required_data_type = None):
    game_data = OrderedDict(json.load(open("game_data.json"), object_pairs_hook=OrderedDict))

    if required_data_type:
        return game_data[required_data_type]
    else:
        return game_data

def init_exception_hook():
    # Back up the reference to the exceptionhook
    sys._excepthook = sys.excepthook

    def exception_hook(exctype, value, traceback):
        # Print the error and traceback
        print(exctype, value, traceback)
        # Call the normal Exception hook after
        sys._excepthook(exctype, value, traceback)
        sys.exit(1)

    # Set the exception hook to our wrapping function
    sys.excepthook = exception_hook

##############################

# TODO: FIGURE OUT THIS PARSER

class OpAndToken(object):
    lbp = 10
    op = 'and'
    def led(self, parser, left):
        right = parser.expression(self.lbp)
        operands = []
        for operand in left, right:
            # flatten out nested operands of the same type
            if isinstance(operand, dict) and self.op in operand:
                operands.extend(operand[self.op])
            else:
                operands.append(operand)
        return {self.op: operands}

class OpOrToken(OpAndToken):
    lbp = 20
    op = 'or'

class LiteralToken(object):
    def __init__(self, value):
        self.value = value
    def nud(self):
        return self.value

class EndToken(object):
    lbp = 0

class Parser(object):
    operators = {'AND': OpAndToken, 'OR': OpOrToken}
    token_pat = re.compile("\s+(AND|OR)\s+")

    def __init__(self, program):
        self.program = program
        self.tokens = self.tokenizer()
        self.token = next(self.tokens)

    def expression(self, rbp=0):
        t = self.token
        self.token = next(self.tokens)
        left = t.nud()
        while rbp < self.token.lbp:
            t = self.token
            self.token = next(self.tokens)
            left = t.led(self, left)
        return left

    def tokenizer(self):
        for tok in self.token_pat.split(self.program):
            if tok in self.operators:
                yield self.operators[tok]()
            else:
                yield LiteralToken(tok)
        yield EndToken()

    def parse(self):
        return self.expression()

###
##############################

def parse_req(req):
    parsed_req = Parser(req).parse()
    if isinstance(parsed_req, dict):
        return parsed_req
    else:
        return {"and": [parsed_req]}

def debug(sentences):
    for sentence in sentences:
        print(json.dumps(sentence, indent=4))


import math

def square_of_a_rectangle(first_side, second_side):
    """Function that calculates the square of a rectangle."""

    return math.prod([first_side, second_side])

def square_of_a_triangle(first_side, second_side, third_side):
    """Function that calculates the square of a triangle."""

    p = sum([first_side, second_side, third_side]) / 2
    S = math.sqrt(math.prod([p, p - first_side, p - second_side, p - third_side]))
    return S

def square_of_a_circle(radius):
    """Function that calculates the square of a circle."""

    return math.prod([math.pi, pow(radius, 2)])

def message(text):
    """Print text."""

    print(text)

while True:
    the_entered_number_option = input(
        '\n' + 'What you want to calculate?:'
        '\n\t' + 'Square of a rectangle, enter number: "1"'
        '\n\t' + 'Square of triangle, enter number: "2"'
        '\n\t' + 'Square of circle, enter number: "3"'
        '\n\t' + 'If you want exit, enter number: "0"'
        '\n\t' + 'Enter: '
    )

    if the_entered_number_option == "1":
        first_side_of_the_rectangle, second_side_of_the_rectangle = (
            float(input("Enter the size of the first side of the rectangle: ")),
            float(input("Enter the size of the second side of the rectangle: "))
        )
        calculation_result = square_of_a_rectangle(
            first_side_of_the_rectangle,
            second_side_of_the_rectangle
        )
        calculation_result = "Square of a rectangle: " + str(calculation_result)

    elif the_entered_number_option == "2":
        first_side_of_the_triangle, second_side_of_the_triangle, third_side_of_the_triangle = (
            float(input("Enter the size of the first side of the triangle: ")),
            float(input("Enter the size of the second side of the triangle: ")),
            float(input("Enter the size of the third side of the triangle: "))
        )
        calculation_result = square_of_a_triangle(
            first_side_of_the_triangle,
            second_side_of_the_triangle,
            third_side_of_the_triangle
        )
        calculation_result = "Square of a triangle: " + str(calculation_result)

    elif the_entered_number_option == "3":
        radius_of_a_circle = float(input("Enter the radius of a circle: "))
        calculation_result = square_of_a_circle(radius_of_a_circle)
        calculation_result = "Square of a circle: " + str(calculation_result)

    elif the_entered_number_option == "0":
        message("=======================\n" + "Exit.")
        break

    else:
        calculation_result = "=======================\n" + "Make your choice!"

    message(calculation_result)


import pandas as pd
import numpy as np
import json

# df = pd.read_csv('../Data/HouseholdQuestions_Cities_Districts_040119_1300.csv',encoding='cp1252')
df = pd.read_csv('../Data/2020_Modyfied.csv')

jsonFile = []

# read file
try:
    with open('./JSON/2019/GeneralTableSubpopulations2019.json', 'r') as myfile:
        data=myfile.read()

    jsonData = json.loads(data)

except(FileNotFoundError):
    print("[OLD JSON FILE NOT FOUND, GENERATING NEW ONE]")
    jsonData = []

year = input("Input Year: ")
model = 'backend.GeneralTableSubpopulations' + year
data = []
count = len(jsonData)

year = int(year)

print("[CREATING NEW JSON FILE]")

class NpEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(NpEncoder, self).default(obj)

raceCategory = [('Asian','Asian'),('AmericanIndian','AmericanIndian'),('Black','Black'),('White','White'),('Multiple','Multiple'),('NativeHawaiian','NativeHawaiian'),('DoesntKnow','NotSure'), 'Total']
raceSubpopulation = ['Asian', 'American Indian', 'Black', 'White', 'Multiple Races', 'Native Hawaiian', 'Unknown Race', 'Total']

ethnicityCategory = [("Yes", "Hispanic"), ("No", "NonHispanic"), ("DoesntKnow", "NotSure"),'Total']
ethnicitySubpopulation = ["Hispanic", "NonHispanic", "Unknown Ethnicity", 'Total']

genderCategory = [('Male','Male'), ('Female','Female'), ('MTF', 'FTM'), ('GenderNonConforming','GenderNonConforming'), ('DoesntKnow','NotSure'), 'Total']
genderSubpopulation = ['Male', 'Female', 'Transgender', 'Gender Non-Conforming', 'Unknown Gender', 'Total']

ageCategory = [('adult', 'Over25'), ('youth', 'Under24'), ('child', 'Under18'), ('DoesntKnow','NotSure'), 'Total']
ageSubpopulation = ['Adults (>24)', 'Youth (18-24)', 'Children (<18)', 'Unknown Age', 'Total']

subpopulationCategory = ['Yes', 'No' , 'DoesntKnow']
veteransSubpopulation = ['Veteran Yes', 'Veteran No']
substanceAbuseSubpopulation = ['Substance Abuse', 'No Substance Abuse', 'Unknown Substance Abuse']
ptsdSubpopulation = ['PTSD', 'No PTSD', 'Unknown PTSD']
mentalHealthSubpopulation = ['Mental Health Conditions', 'No Mental Health Conditions', 'Unknown Mental Health Conditions']
physicalDisabilitySubpopulation = ['Physical Disability', 'No Physical Disability', 'Unknown Physical Disability']
developmentDisabilitySubpopulation = ['Developmental Disability', 'No Developmental Disability', 'Unknown Developmental Disability']
brainInjurySubpopulation =  ['Brain Injury', 'No Brain Injury', 'Unknown Brain Injury']
domesticViolenceSubpopulation = ['Victim of Domestic Violence', 'Not Victim of Domestic Violence','Unknown Victim of Domestic Violence']
aidsOrhivSubpopulation = ['AIDS or HIV', 'No AIDS or HIV' , 'Unknown AIDS or HIV']
jailYesCategory = ['Probation', 'Parole', 'CompletedSentence', 'DoesntKnow']
jail90DaysSubpopulation = ['Jail Release 90 Days: Probation', 'Jail Release 90 Days: Parole', 'Jail Release 90 Days: Completed Sentence', 'Jail Release 90 Days: (Unspecified)']
jail12MonthsSubpopulation = ['Jail Release 12 Months: Probation', 'Jail Release 12 Months: Parole','Jail Release 12 Months: Completed Sentence', 'Jail Release 12 Months: (Unspecified)']
jailCategory = ['No', 'DoesntKnow']
jailNoSubpopulation = ['No Jail', 'Unknown Jail']

livingSituationCategory = ['Woods', 'Vehicle','UnderBridge', 'Street', 'Park', 'Other','Bus','AbandonedBuilding']
livingSituationSubpopulation = ['Woods', 'Vehicle','UnderBridge', 'Street', 'Park', 'Other','Bus','AbandonedBuilding']

def get_race_count(in_df, value, subpopulation,year):

    if subpopulation == 'Total':
        interview = in_df.loc[lambda df:  (df['Household Survey Type'] == 'Interview'), ['Race']].shape[0]
        observation = in_df.loc[lambda df: (df['Household Survey Type'] == 'Observation'), ['Race Observed']].shape[0]
    else:
        interview =  in_df.loc[lambda df: (df['Race'] == value[0]), :].shape[0]
        observation = in_df.loc[lambda df: (df['Race Observed'] == value[1]), :].shape[0]

    return {"category": "Race", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_ethnicity_count(in_df, value, subpopulation,year):
    if subpopulation == 'Total':
        interview = in_df.loc[lambda df: (df['Household Survey Type'] == 'Interview'), ['Ethnicity']].shape[0]
        observation = in_df.loc[lambda df: (df['Household Survey Type'] == 'Observation'), ['Hispanic Observed']].shape[0]
    else:
        interview =  in_df.loc[lambda df: ((df['Ethnicity'] == value[0]) ), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Hispanic Observed'] == value[1])), :].shape[0]

    return {"category": "Ethnicity", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_gender_count(in_df, value, subpopulation,year):
    if subpopulation == 'Total':
        interview = in_df.loc[lambda df: (df['Household Survey Type'] == 'Interview'), ['Gender']].shape[0]
        observation = in_df.loc[lambda df: (df['Household Survey Type'] == 'Observation'), ['Gender Observed']].shape[0]

    elif value[0] == 'MTF':
        interview =  in_df.loc[lambda df: ((df['Gender'] == value[0]) ), :].shape[0] + in_df.loc[lambda df: ((df['Gender'] == value[1])), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Gender Observed'] == value[0]) ), :].shape[0] + in_df.loc[lambda df: ((df['Gender Observed'] == value[1])), :].shape[0]
    else:
        interview =  in_df.loc[lambda df: ((df['Gender'] == value[0])), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Gender Observed'] == value[1])), :].shape[0]

    return {"category": "Gender", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_age_count(in_df,value, subpopulation, year):
    if subpopulation == 'Total':
        interview = in_df.loc[lambda df: (df['Household Survey Type'] == 'Interview'), ['Age As Of Today']].shape[0]
        observation = in_df.loc[lambda df: (df['Household Survey Type'] == 'Observation'), ['Age Observed']].shape[0]

    elif value[0] == 'child':
        interview =  in_df.loc[lambda df: ((df['Age As Of Today'] < 18) ), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Age Observed'] == value[1])), :].shape[0]
    elif value[0] == 'youth':
        interview =  in_df.loc[lambda df: (((df['Age As Of Today'] < 25) & (df['Age As Of Today'] >= 18)) ), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Age Observed'] == value[1])), :].shape[0]
    elif value[0] == 'adult':
        interview =  in_df.loc[lambda df: ((df['Age As Of Today'] >=25)), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Age Observed'] == value[1])), :].shape[0]
    elif value[0] == 'DoesntKnow':
        interview =  in_df.loc[lambda df: ((df['Age As Of Today'] == value[0])), :].shape[0]
        observation = in_df.loc[lambda df: ((df['Age Observed'] == value[1])), :].shape[0]

    return {"category": "Age", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_Veterans(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['United States Armed Forces'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_ChronicHomeless(in_df,year):

    ChronicallyHomelessHouseholds = in_df.loc[lambda df:\
       ((df['Household Survey Type'] == 'Interview') & (df['Chronically Homeless Status'] == 1))\
        #    | ((df['Household Survey Type'] == 'Observation') & (df['Chronically Homeless Status'] == 1))\
            , ['ParentGlobalID']].drop_duplicates(subset='ParentGlobalID')

    total_persons = in_df.loc[lambda df:\
        ((df['Household Survey Type'] == 'Interview'))\
            # | ((df['Household Survey Type'] == 'Observation'))
                , ['ParentGlobalID']]['ParentGlobalID']\
                    .isin(ChronicallyHomelessHouseholds['ParentGlobalID']).sum()

    interview = total_persons
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": 'Chronically Homeless', "total": interview + observation, "year": year}

def get_Total_NotChronicHomeless(in_df,year):
    totalCount = in_df.loc[lambda df: (df['Household Survey Type'] == 'Interview') , :].shape[0]

    ChronicallyHomelessHouseholds = in_df.loc[lambda df:\
       ((df['Household Survey Type'] == 'Interview') & (df['Chronically Homeless Status'] == 1))\
        #    | ((df['Household Survey Type'] == 'Observation') & (df['Chronically Homeless Status'] == 1))\
            , ['ParentGlobalID']].drop_duplicates(subset='ParentGlobalID')

    total_persons = in_df.loc[lambda df:\
        ((df['Household Survey Type'] == 'Interview'))\
            # | ((df['Household Survey Type'] == 'Observation'))
                , ['ParentGlobalID']]['ParentGlobalID']\
                    .isin(ChronicallyHomelessHouseholds['ParentGlobalID']).sum()

    interview = totalCount - total_persons
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": 'Not Chronically Homeless', "total": interview + observation, "year": year}

def get_Total_SubstanceAbuse(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Substance Abuse'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_PTSD(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['PTSD'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_MentalHealthCondition(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Mental Health Issue'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_PhysicalDisability(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Physical Disability'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_DevelopmentDisability(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Developmental Disability'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_BrainInjury (in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Brain Injury'] == value) ), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_DomesticViolence (in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Domestic Violence Victim'] == value) ), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_AidsOrHiv (in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['HIV/AIDS'] == value) ), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_Jail90Days (in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Jail Or Prison'] == 'YesNinetyDays')  & (df['Probation Or Parole'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_Jail12Months (in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Jail Or Prison'] == 'YesTwelveMonths')  & (df['Probation Or Parole'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_NonJailCount (in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['Jail Or Prison'] == value)), :].shape[0]
    observation = 0

    return {"category": "Subpopulations", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_Households_OnlyAdults(in_df,year):
    total_Adults = in_df.loc[lambda df:\
         ((df['Household Survey Type'] == 'Interview') & (df['Age As Of Today'] >= 18))\
                ,['ParentGlobalID']]\
                    .drop_duplicates(subset='ParentGlobalID')

    total_children = in_df.loc[lambda df:\
        ((df['Household Survey Type'] == 'Interview') & (df['Age As Of Today'] < 18))\
               ,['ParentGlobalID']]\
                   .drop_duplicates(subset='ParentGlobalID')

    set_diff_df = total_Adults.merge(total_children, indicator=True, how="left")[lambda x: x._merge=='left_only'].drop('_merge',1).drop_duplicates()

    interview = set_diff_df.shape[0]
    observation = 0

    return {"category": "Households", "interview": interview ,"observation": observation, "subpopulation": 'Adults Only', "total": interview + observation, "year": year}

def get_Total_Households_OnlyChildren(in_df,year):
    total_children = in_df.loc[lambda df:\
    ((df['Household Survey Type'] == 'Interview') & (df['Age As Of Today'] < 18)  )\
            ,['ParentGlobalID']]\
                .drop_duplicates(subset='ParentGlobalID')

    total_Adults = in_df.loc[lambda df:\
         ((df['Household Survey Type'] == 'Interview') & (df['Age As Of Today'] >= 18)  )\
                ,['ParentGlobalID']]\
                    .drop_duplicates(subset='ParentGlobalID')
    set_diff_df = total_children.merge(total_Adults, indicator=True, how="left")[lambda x: x._merge=='left_only'].drop('_merge',1).drop_duplicates()

    interview = set_diff_df.shape[0]
    observation = 0

    return {"category": "Households", "interview": interview ,"observation": observation, "subpopulation": 'Children Only', "total": interview + observation, "year": year}

def get_Total_Households_AdultsandChildren(in_df, year):
    total_adults = in_df.loc[lambda df:\
         ((df['Household Survey Type'] == 'Interview') & (df['Age As Of Today'] >= 18) )\
                ,['ParentGlobalID']]\
                    .drop_duplicates(subset='ParentGlobalID')

    total_children = in_df.loc[lambda df:\
        ((df['Household Survey Type'] == 'Interview') & (df['Age As Of Today'] < 18) )\
               ,['ParentGlobalID']]\
                   .drop_duplicates(subset='ParentGlobalID')

    intersected_df = pd.merge(total_adults, total_children, how='inner').drop_duplicates(subset='ParentGlobalID')

    interview = intersected_df.shape[0]
    observation = 0

    return {"category": "Households", "interview": interview ,"observation": observation, "subpopulation": 'Adults and Children', "total": interview + observation, "year": year}

def get_Total_LivingSituation(in_df, value, subpopulation, year):
    interview =  in_df.loc[lambda df: ((df['P_Living_Situation'] == value)), :].shape[0]
    observation = 0

    return {"category": "Living Situation", "interview": interview ,"observation": observation, "subpopulation": subpopulation, "total": interview + observation, "year": year}

def get_Total_Households(in_df,year):
    interview =  in_df.loc[lambda df: (df['Household Survey Type'] == 'Interview'), :].shape[0]
    observation =  in_df.loc[lambda df: (df['Household Survey Type'] == 'Observation'), :].shape[0]

    return {"category": "Households", "interview": interview ,"observation": observation, "subpopulation": 'Total', "total": interview + observation, "year": year}

for i in range(len(raceSubpopulation)):
            data.append({
            "fields":  get_race_count(df, raceCategory[i],raceSubpopulation[i],year)
            })

for i in range(len(ethnicitySubpopulation)):
            data.append({
            "fields":  get_ethnicity_count(df, ethnicityCategory[i],ethnicitySubpopulation[i],year)
            })

for i in range(len(genderSubpopulation)):
            data.append({
            "fields":  get_gender_count(df, genderCategory[i],genderSubpopulation[i],year)
            })

for i in range(len(ageSubpopulation)):
            data.append({
            "fields":  get_age_count(df, ageCategory[i],ageSubpopulation[i],year)
            })

for i in range(len(veteransSubpopulation)):
            data.append({
            "fields":  get_Total_Veterans(df, subpopulationCategory[i],veteransSubpopulation[i],year)
            })

for i in range(len(substanceAbuseSubpopulation)):
            data.append({
            "fields":  get_Total_SubstanceAbuse(df, subpopulationCategory[i],substanceAbuseSubpopulation[i],year)
            })

for i in range(len(ptsdSubpopulation)):
            data.append({
            "fields":  get_Total_PTSD(df, subpopulationCategory[i],ptsdSubpopulation[i],year)
            })

for i in range(len(mentalHealthSubpopulation)):
            data.append({
            "fields":  get_Total_MentalHealthCondition(df, subpopulationCategory[i],mentalHealthSubpopulation[i],year)
            })

for i in range(len(physicalDisabilitySubpopulation)):
            data.append({
            "fields":  get_Total_PhysicalDisability(df, subpopulationCategory[i],physicalDisabilitySubpopulation[i],year)
            })

for i in range(len(developmentDisabilitySubpopulation)):
            data.append({
            "fields":  get_Total_DevelopmentDisability(df, subpopulationCategory[i],developmentDisabilitySubpopulation[i],year)
            })

for i in range(len(brainInjurySubpopulation)):
            data.append({
            "fields":  get_Total_BrainInjury(df, subpopulationCategory[i],brainInjurySubpopulation[i],year)
            })

for i in range(len(domesticViolenceSubpopulation)):
            data.append({
            "fields":  get_Total_DomesticViolence(df, subpopulationCategory[i],domesticViolenceSubpopulation[i],year)
            })

for i in range(len(aidsOrhivSubpopulation)):
            data.append({
            "fields":  get_Total_AidsOrHiv(df, subpopulationCategory[i],aidsOrhivSubpopulation[i],year)
            })

for i in range(len(jail90DaysSubpopulation)):
            data.append({
            "fields":  get_Total_Jail90Days(df, jailYesCategory[i],jail90DaysSubpopulation[i],year)
            })

for i in range(len(jail12MonthsSubpopulation)):
            data.append({
            "fields":  get_Total_Jail12Months(df, jailYesCategory[i],jail12MonthsSubpopulation[i],year)
            })

for i in range(len(jailNoSubpopulation)):
            data.append({
            "fields":  get_Total_NonJailCount(df, jailCategory[i],jailNoSubpopulation[i],year)
            })

for i in range(len(livingSituationSubpopulation)):
            data.append({
            "fields":  get_Total_LivingSituation(df, livingSituationCategory[i],livingSituationSubpopulation[i],year)
            })

data.append({
        "fields":  get_Total_Households(df,year)
        })

data.append({
        "fields":  get_Total_Households_OnlyAdults(df,year)
        })

data.append({
        "fields":  get_Total_Households_OnlyChildren(df,year)
        })

data.append({
        "fields":  get_Total_Households_AdultsandChildren(df,year)
        })

data.append({
        "fields":  get_Total_ChronicHomeless(df,year)
        })

data.append({
        "fields":  get_Total_NotChronicHomeless(df,year)
        })

for x in jsonData:
    x['model'] = model
    x['fields']['year'] = 2019

jsonFields = [ x["fields"] for x in jsonData]

for d in data:
    if d['fields'] not in jsonFields:
        print("not found: ", d['fields'])
        count +=1
        d["pk"] = count
        d["model"] = model
        jsonData.append(d)

out = open('./JSON/2020/GeneralTableSubpopulations.json','w')

out.write (json.dumps(jsonData, cls=NpEncoder, indent=4))
out.close()

print("[FINISHED CREATING JSON FILE]")


"""
n = 6, k = 4
keep the first 1, 2 from 1, 2, 3, 4, 5, 6
reverse insert the remaining numbers by dividing into two parts: [3, 4] [5, 6] => [6, 3, 5, 4]
combine together: [1, 2, 6, 3, 5, 4]
"""
class Solution(object):
    def constructArray(self, n, k):
        """
        :type n: int
        :type k: int
        :rtype: List[int]
        """
        result = range(1, n+1)[0: n-k]
        temp = range(1, n+1)[n-k:]
        firstHalf, secondHalf = temp[0: (len(temp) / 2)], temp[(len(temp) / 2):][::-1]
        for i in range(k):
            if i % 2 == 0:
                result.append(secondHalf[i/2])
            else:
                result.append(firstHalf[i/2])
            i += 1
        return result


from numpy import column_stack
from pandas import DataFrame
from pylogix import PLC
from datetime import datetime
import pandas as pd
import time

comm1 = PLC()
comm1.Micro800 = True
comm1.IPAddress = '192.168.0.119'

comm2 = PLC()
comm2.Micro800 = True
comm2.IPAddress = '192.168.0.113'

comm3 = PLC()
comm3.Micro800 = True
comm3.IPAddress = '192.168.0.116'

FirstTime = True

def column_values(comm, string: str) -> str:
    """
    It returns with specific column values

    :param comm: communication medium
    :param string: Real values
    :return: value of column for a timestamp in str data type
    """
    T = comm.Read(string)
    T_data = str(T).split(' ')
    return T_data[1]

Real = list("Real" + str(i + 1) for i in range(15))  # generates a list for values Real1 to Real15

def live_data() -> DataFrame:
    """
    It provides with live data for the instance it is called!

    :return: Dataframe for a specific Timestamp for which the function is called
    """
    try:

        T = list(column_values(comm=comm1, string=real) for real in Real)

        # DATA POLLING FROM PLC 2 - 192.168.0.113

        A = list(column_values(comm=comm2, string=real) for real in Real)

        # DATA POLLING FROM PLC 2 - 192.168.0.116

        X = list(column_values(comm=comm3, string=real) for real in Real)

        S = tuple(T + A + X)
        S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15, S21, S22, S23, S24, S25, S26, S27, S28, S29, S30, S31, S32, S33, S34, S35, S41, S42, S43, S44, S45, S46, S47, S48, S49, S50, S51, S52, S53, S54, S55 = S
        current_time = datetime.now()
        Timestamp = current_time.strftime("%Y%m%d%H%M%S")
        Data = {
            "Timestamp": Timestamp,
            "S1": S1,
            "S2": S2,
            "S3": S3,
            "S4": S4,
            "S5": S5,
            "S6": S6,
            "S7": S7,
            "S8": S8,
            "S9": S9,
            "S10": S10,
            "S11": S11,
            "S12": S12,
            "S13": S13,
            "S14": S14,
            "S15": S15,
            "S21": S21,
            "S22": S22,
            "S23": S23,
            "S24": S24,
            "S25": S25,
            "S26": S26,
            "S27": S27,
            "S28": S28,
            "S29": S29,
            "S30": S30,
            "S31": S31,
            "S32": S32,
            "S33": S33,
            "S34": S34,
            "S35": S35,
            "S41": S41,
            "S42": S42,
            "S43": S43,
            "S44": S44,
            "S45": S45,
            "S46": S46,
            "S47": S47,
            "S48": S48,
            "S49": S49,
            "S50": S50,
            "S51": S51,
            "S52": S52,
            "S53": S53,
            "S54": S54,
            "S55": S55,
        }
        df = pd.DataFrame(Data, index=[0])
        if type(df) is None:
            time.sleep(10)
            ds = live_data()
            return ds
        else:
            return df
    except ConnectionError:
        time.sleep(10)
        print('Connection Error caught')


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 29 14:10:06 2019

@author: Ikhtiyor
"""

from operations import  fully_conneted, relu, conv, max_pool
from operations import local_response_normalization, dropout
import numpy as np

import tensorflow as tf
def network_AlexNet(x,label_dim =2, reuse=False, drop_rate=0.3):
    with tf.variable_scope("network", reuse=reuse):
        # Layer 1.
        # [Input] ==> 100x91x1
        # --> 100x91x1 ==> [Convolution: size=(11x11x1)x96, strides=4, padding=valid] ==> 23x21x96
        # --> 23x21x96 ==> [ReLU] ==> 23x21x96
        # --> 23x21x96 ==> [Local Response Normalization] ==> 55x55x96
        # --> 23x21x96 ==> [Max-Pool: size=3x3, strides=2, padding=valid] ==> 11x10x96
        # --> [Output] ==>  11x10x96

        layer1 = conv(x, 96, kernel=11, stride=4, padding='VALID', name='layer1_conv')
        layer1 = relu(layer1)
        layer1 = local_response_normalization(input=layer1,name='layer1_lrn')
        layer1 = max_pool(layer1, name='layer1_mxpool')
        print(np.shape(layer1))

         # Layer 2.
        # [Input] ==> 11x10x96
        # --> 11x10x96 ==> [Convolution: size=(5x5x96)x256, strides=1, padding=same] ==> 11x10x256
        # --> 11x10x256 ==> [ReLU] ==> 11x10x256
        # --> 11x10x256 ==> [Local Response Normalization] ==> 11x10x256
        # --> 11x10x256 ==> [Max-Pool: size=3x3, strides=2, padding=valid] ==> 5x4x256
        # --> [Output] ==>  5x4x256

        layer2 = conv(layer1, 256, kernel=5, stride=1, padding='SAME', name='layer2_conv')
        layer2 = relu(layer2)
        layer2 = local_response_normalization(input=layer2, name='layer2_lrn')
        layer2 = max_pool(layer2,name='layer2_mxpool')
        print(np.shape(layer2))

        # Layer 3.
        # [Input] ==> 5x4x256
        # --> 5x4x256 ==> [Convolution: size=(3x3x256)x384, strides=1, padding=same] ==> 5x4x384
        # --> 5x4x384 ==> [ReLU] ==> 5x4x384
        # --> [Output] ==>   5x4x384

        layer3 = conv(layer2, 384, kernel=3, stride=1, padding='SAME',name='layer3_conv')
        layer3 = relu(layer3)
        print(np.shape(layer3))

        # Layer 4.
        # [Input] ==> 5x5x384
        # --> 5x4x384 ==> [Convolution: size=(3x3x384)x384, strides=1, padding=same] ==> 5x5x384
        # --> 5x4x384 ==> [ReLU] ==> 5x4x384
        # --> [Output] ==>   5x4x384

        layer4 = conv(layer3, 384, kernel=3, stride=1, padding='SAME',name='layer4_conv')
        layer4 = relu(layer4)
        print(np.shape(layer4))

        # Layer 5.
        # [Input] ==> 5x4x384
        # --> 5x4x384 ==> [Convolution: size=(3x3x384)x256, strides=1, padding=same] ==> 5x4x256
        # --> 5x4x256 ==> [ReLU] ==> 5x4x256
        # --> 5x4x256 ==> [Max-Pool: size=3x3, strides=2, padding=valid] ==> 2x1x256
        # --> [Output] ==>   2x1x256

        layer5 = conv(layer4, 256, kernel=3, stride=1, padding='SAME',name='layer5_conv')
        layer5 = relu(layer5)
        layer5 = max_pool(layer5,name='layer5_mxpool')
        print(np.shape(layer5))

        # Layer 6.
        # [Input] ==> 2x1x256 = 512
        # --> 512 ==> [Fully Connected: neurons=512] ==> 512
        # --> 512 ==> [ReLU] ==> 512
        # --> 512 ==> [Dropout] ==> 512
        # --> [Output] ==> 512
        layer6 = fully_conneted(layer5, 512,name='layer6_fc')
        layer6 = relu(layer6)
        layer6 = dropout(layer6, drop_rate,name='layer6_drp')

        # Layer 7.
        # [Input] ==>  512
        # --> 512 ==> [Fully Connected: neurons=1024] ==> 512
        # --> 512 ==> [ReLU] ==> 512
        # --> 512 ==> [Dropout] ==> 512
        # --> [Output] ==> 1024
        layer7 = fully_conneted(layer6, 512, name='layer7_fc')
        layer7 = relu(layer7)
        layer7 = dropout(layer7, drop_rate,name='layer7_drp')

        # Layer 8.
        # [Input] ==>  512
        # --> 512 ==> [Fully Connected: neurons=2] ==> 2
        # --> [Output] ==> 2
        layer8_logits = fully_conneted(layer7, label_dim, name='logits')

        return layer8_logits


from Exercise1.b_tree import BTree
if __name__=="__main__":
    print("="*20," BTREE ", "="*20)
    btree = BTree()
    inp = input("What do you want to do?\n 1) add\n 2) delete\n 3) search\n 4) print\n 5) exit\n")
    inp = int(inp)
    while not inp == 5:
        if inp == 1:
            k = input("Insert key to add: ")
            v = input("Insert value for key k: ")
            btree.add(k,v)
        elif inp == 2:
            k = int(input("Insert key to delete: "))
            btree.delete(k)
        elif inp == 3:
            k = int(input("Insert key to search: "))
            btree.search_key(k)
        elif inp == 4:
            btree.print_tree()

        inp = int(input("What do you want to do?\n 1) add\n 2) delete\n 3) search\n 4) print\n 5) exit\n"))


from random import choice

class RandomWalk:
    """Класс для генерирования случайных блужданий."""

    def __init__(self, num_points=2500):
        """Инициализирует атрибуты блуждания."""
        self.num_points = num_points

        # Все блужданий начинаются с точки (0, 0)
        self.x_values = [0]
        self.y_values = [0]

    def will_walk(self):
        """Вычесляет все точки блужданий."""

        # Шаги генерируются до достижения нужной длины.
        while len(self.x_values) < self.num_points:
            def get_step():
                direction = choice([1, -1])
                distance = choice(range(2))
                step = direction * distance
                return step
            x_step = get_step()
            y_step = get_step()

            # Откланение нулевых перемещений.
            if x_step == 0 and y_step == 0:
                continue

            # Вычисление слудующих значений x и y.
            next_x = self.x_values[-1] + x_step
            next_y = self.y_values[-1] + y_step

            self.x_values.append(next_x)
            self.y_values.append(next_y)


from django.shortcuts import render
from manager.models import *
from django import http
from django.views import View
import json, requests
from django.db.models import Max
from django.db import transaction
from django.db import IntegrityError
from django.db.models import Sum, Count, Max, Min, Avg

class order_type(View):
    # 历史所有参数修改的请求
    def get(self, request):
        all_past_log = order_choice_log.objects.all()
        all_log = []
        for past_log in all_past_log:
            past_log = {
                'choice_id':past_log.choice_id,
                'add_order_type':past_log.add_order_type,
                'nudge_order_type':past_log.nudge_order_type,
                'param':past_log.param,
                'create_time':past_log.create_time.strftime('%Y%m%d %H:%M:%S')
            }
            all_log.append(past_log)
        #print(all_log)

        return http.JsonResponse({'all_log':all_log})

    # 新加的修改参数的请求
    def post(self, request):
        try:
            dict_data = json.loads(request.body, strict = False)
            order_choice_log.objects.create(add_order_type = dict_data['add_order_type'],
                                        nudge_order_type = dict_data['nudge_order_type'],
                                        param = dict_data['param'])
            return http.JsonResponse({"edit_status": 1})
        except Exception:
            return http.JsonResponse({"edit_status": 0})

## 查看起始界面最新的order_choice
def order_type_new(request):
    past_log = order_choice_log.objects.all().last()
    past_log = {
            'choice_id':past_log.choice_id,
            'add_order_type':past_log.add_order_type,
            'nudge_order_type':past_log.nudge_order_type,
            'param':past_log.param,
            'create_time':past_log.create_time.strftime('%Y%m%d %H:%M:%S')
        }
    print(past_log)
    return http.JsonResponse({"all_log":past_log})


import csv
import os
from datetime import datetime

def append(dc,path):
    """
    dc={'date':'xxx','f1':'xx','f2':'xxx'}
    """
    try:
        path_dir=os.path.dirname(path)
        os.makedirs(path_dir)
    except Exception as e:
        print(e)

    head=[key for key in dc.keys()]
    head.sort()

    row=[dc[key] for key in head]

    head=['datetime']+head
    now=datetime.now()
    row=[now.strftime('%Y-%m-%d %H:%M:%S')]+row

    has_header=False
    if os.path.exists(path):
        with open(path,'rb') as f:
            if f.readline():
                has_header=True

    with open(path,'w+b') as f:
        writer = csv.writer(f)
        if not has_header:
            writer.writerow(head)
        writer.writerow(row)


import urllib
import oauth2 as oauth
from twitter import Api
from urlparse import parse_qsl

from zope.component import getUtility
from zope.publisher.browser import BrowserView

from plone.registry.interfaces import IRegistry
from Products.PluggableAuthService.interfaces.plugins import IExtractionPlugin
from Products.statusmessages.interfaces import IStatusMessage
from collective.beaker.interfaces import ISession

from cs.auth.twitter import TWMessageFactory as _
from cs.auth.twitter.plugin import SessionKeys
from cs.auth.twitter.interfaces import ICSTwitterPlugin

TWITTER_REQUEST_TOKEN_URL = 'https://api.twitter.com/oauth/request_token'
TWITTER_ACCESS_TOKEN_URL = 'https://api.twitter.com/oauth/access_token'
TWITTER_AUTH_URL = 'https://api.twitter.com/oauth/authorize'

class AuthorizationTokenKeys:
    """Constants used to look up authorization keys
    """

    oauth_token              = "cs.auth.twitter.oauth_token"
    oauth_token_secret       = "cs.auth.twitter.oauth_token_secret"
    oauth_callback_confirmed = "cs.auth.twitter.oauth_callback_confirmed"

class TwitterLogin(BrowserView):
    """This view implements the Twitter OAuth login protocol.

    The user may access the view via a link in an action or elsewhere.

    We ask twitter an authentication token, save it in session and redirect
    the user with this token to Twitter.

    The 2nd step of the process is handled by the next BrowserView

    """

    def __call__(self):
        registry = getUtility(IRegistry)
        TWITTER_CONSUMER_KEY = registry.get('cs.auth.twitter.controlpanel.ITwitterLoginSettings.twitter_consumer_key').encode()
        TWITTER_CONSUMER_SECRET = registry.get('cs.auth.twitter.controlpanel.ITwitterLoginSettings.twitter_consumer_secret').encode()

        # Create an Oauth Consumer
        oauth_consumer = oauth.Consumer(key=TWITTER_CONSUMER_KEY,
                                        secret=TWITTER_CONSUMER_SECRET)

        oauth_client = oauth.Client(oauth_consumer)

        # Set the callback URL. Be sure that callback urls are allowed in Twitter
        # App configuration. Do not leave blank the field of the callback url
        # when creating the app, otherwise this login method *will not work*.
        return_args = ''
        if self.request.get('came_from', None) is not None:
            return_args = {'came_from': self.request.get('came_from')}
            return_args = '?' + urllib.urlencode(return_args)

        args = {
                'oauth_callback' : self.context.absolute_url() +
                                   '/@@twitter-login-verify' +
                                    return_args,
            }
        body = urllib.urlencode(args)
        resp, content = oauth_client.request(TWITTER_REQUEST_TOKEN_URL, 'POST', body=body)

        if resp.get('status', '999') != '200':
            IStatusMessage(self.request).add(_(u"Error getting the authorization token from Twitter. Try again please"), type="error")
            self.request.response.redirect(self.context.absolute_url())
            return u""
        else:
            # The request was successful, so save the token in the session
            # and redirect the user to Twitter
            request_token = dict(parse_qsl(content))
            session = ISession(self.request)
            session[AuthorizationTokenKeys.oauth_token] = request_token['oauth_token']
            session[AuthorizationTokenKeys.oauth_token_secret] = request_token['oauth_token_secret']
            session[AuthorizationTokenKeys.oauth_callback_confirmed] = request_token['oauth_callback_confirmed']
            session.save()

            args = {
                'oauth_token' : request_token['oauth_token'],
            }

            self.request.response.redirect(
                    "%s?%s" % (TWITTER_AUTH_URL, urllib.urlencode(args),)
                )

class TwitterLoginVerify(BrowserView):
    """
        This BrowserView handles the 2nd step of the Oauth authentication with
        Twitter.

        It checks the authentication token saved in the 1st step against Twitter
        and if it's successful saves everything in the session so that Plone's
        Authentication and Credential extraction plugin
    """
    def __call__(self):
        registry = getUtility(IRegistry)
        TWITTER_CONSUMER_KEY = registry.get('cs.auth.twitter.controlpanel.ITwitterLoginSettings.twitter_consumer_key').encode()
        TWITTER_CONSUMER_SECRET = registry.get('cs.auth.twitter.controlpanel.ITwitterLoginSettings.twitter_consumer_secret').encode()

        session = ISession(self.request)
        token = oauth.Token(session[AuthorizationTokenKeys.oauth_token],
                            session[AuthorizationTokenKeys.oauth_token_secret],
                            )
        consumer = oauth.Consumer(key=TWITTER_CONSUMER_KEY,
                                 secret=TWITTER_CONSUMER_SECRET)
        client = oauth.Client(consumer, token)
        resp, content = client.request(TWITTER_ACCESS_TOKEN_URL, 'GET')
        if resp.get('status', '999') != '200':
            IStatusMessage(self.request).add(_(u"Error authenticating with Twitter. Please try again."), type="error")
            self.request.response.redirect(self.context.absolute_url())
            return u""

        access_token = dict(parse_qsl(content))
        # Save the data in the session so that the extraction plugin can
        # authenticate the user to Plone
        session = ISession(self.request)
        session[SessionKeys.user_id]            = str(access_token['user_id'])
        session[SessionKeys.screen_name]        = access_token['screen_name']
        session[SessionKeys.oauth_token]        = access_token['oauth_token']
        session[SessionKeys.oauth_token_secret] = access_token['oauth_token_secret']

        api = Api(consumer_key=TWITTER_CONSUMER_KEY,
                  consumer_secret=TWITTER_CONSUMER_SECRET,
                  access_token_key=session[AuthorizationTokenKeys.oauth_token],
                  access_token_secret=session[AuthorizationTokenKeys.oauth_token_secret])

        us = api.GetUser(str(access_token['user_id']))
        session[SessionKeys.name] = us.name
        session[SessionKeys.profile_image_url] = us.profile_image_url
        session[SessionKeys.description] = us.description
        session[SessionKeys.location] = us.location
        session.save()

        # Add user data into our plugin storage:
        acl = self.context.acl_users
        acl_plugins = acl.plugins
        ids = acl_plugins.listPluginIds(IExtractionPlugin)
        for id in ids:
            plugin = getattr(acl_plugins, id)
            if ICSTwitterPlugin.providedBy(plugin):
                if plugin._storage.get(session[SessionKeys.user_id], None) is None:
                    user_data = {'screen_name': session[SessionKeys.screen_name],
                                 'fullname': session[SessionKeys.name],
                                 'profile_image_url': session[SessionKeys.profile_image_url],
                                 'description': session[SessionKeys.description],
                                 'location': session[SessionKeys.location]
                                     }
                    plugin._storage[session[SessionKeys.user_id]] = user_data

        IStatusMessage(self.request).add(_(u"Welcome. You are now logged in."), type="info")

        return_args = ''
        if self.request.get('came_from', None) is not None:
            return_args = {'came_from': self.request.get('came_from')}
            return_args = '?' + urllib.urlencode(return_args)

        self.request.response.redirect(self.context.absolute_url() + '/logged_in' + return_args)


"""
# __init__,__new__的区别
"""
# class Test:
#     def __init__(self):
#         print("init的方法：",self)
#         print(id(self))
#
#     def __new__(cls, *args, **kwargs):
#         print("new的id：",id(cls))
#         print("new的方法：",super(Test,cls).__new__(cls))
#         return super(Test,cls).__new__(cls)
#
#
# if __name__ == '__main__':
#     print("init的id",id(Test()))

class Singleton:
    instance = None  # 初始化实例为NULL

    def __new__(cls, xx, yy):

        if cls.instance is None:  # 如果实例为空则实例化
            cls.instance = super().__new__(cls)
        return cls.instance

    def __init__(self, xx, yy):
        self.xx = xx
        self.yy = yy

if __name__ == "__main__":
    obj1 = Singleton(1, 2)
    obj2 = Singleton(2, 1) # 最后赋值xx为2，因为单例实例出来的对象是全局通用唯一的，所以xx=2
    print(obj1.xx,obj2.xx)
    print(obj1 is obj2)
    # print(obj1.xx)


"""Pull good var positions from exac.
   These are used in the enrichment.
"""
import argparse, csv

def goodPos(row):
    # kaviar filter
    if row['KaviarMafLs'] != 'KAV:' and row['KaviarMafLs'].strip():
        if max([float(x) for x in row['KaviarMafLs'].split(':')[1].split(',')]) > float(.01):
            return False

    if row['KaviarSourceLs']:
        for ss in row['KaviarSourceLs'].split(','):
            sources = ss.split('|')
            goodSources = [x for x in sources if not x[0:2] in ('NA','GS') and not 'Wellderly' in x and x not in ('ISB_founders-Nge3','Inova_CGI_founders-Nge3')]
            if not goodSources:
                return False

    # complete genomics control filter
    if row['CGcontrol'].strip():
        if row['CGcontrol'] != '??':
            return False

    return True

def limit(f, fout):
    reader = csv.DictReader(f, delimiter='\t')
    for row in reader:
        if goodPos(row):
            print(row['chrom'] + ':' + row['pos'], file=fout)

def main(args):
    with open(args.exacFile) as f, open(args.outFile, 'w') as fout:
        limit(f, fout)

if __name__ == "__main__":
    desc = 'get pos not in kaviar or cg'
    parser = argparse.ArgumentParser(description=desc)
    argLs = ('exacFile', 'outFile',)
    for param in argLs:
        parser.add_argument(param)
    args = parser.parse_args()
    main(args)


from contrast.klsh.klsh import KernelLSH
import numpy as np

def standard(data, L, cc):
    klsh = KernelLSH(nbits=L, kernel='cosine',
                     epsilon=0.5)  # 几种kernel都要试一下, 结果差异很大
    klsh.fit(data.point_set)
    nbrs = klsh.query(data.point_set[data.query_indices], k=cc)  # k控制返回索引数量
    precision = np.empty(len(data.query_indices))
    for i in range(len(data.query_indices)):
        result = nbrs[i]
        right_num = len(set(result).intersection(set(data.result_indices[i])))
        precision[i] = right_num / data.result_indices.shape[1]
    ap = np.average(precision)
    print("KLSH:", ap)


from django.contrib import admin

from ..models import ExportTransaction

from ..actions import export_as_csv_action, export_tx_to_csv_action

class ExportTransactionAdmin (admin.ModelAdmin):

    date_hierarchy = 'created'
    list_display = ('export_uuid', 'timestamp', 'render', 'status', 'app_label', 'object_name', 'export_change_type', 'exported', 'received', 'received_datetime', 'created',)
    list_filter = ('status', 'exported', 'received', 'app_label', 'object_name', 'export_change_type', 'received_datetime', 'created')
    search_fields = ('export_uuid', 'tx_pk', 'tx')

    def get_actions(self, request):
        actions = super(ExportTransactionAdmin, self).get_actions(request)
        actions['export_as_csv_action'] = (
            export_as_csv_action(),
            'export_as_csv_action',
            "Export to CSV"
            )
        actions['export_tx_to_csv_action'] = (
            export_tx_to_csv_action(),
            'export_tx_to_csv_action',
            "Export transaction in selected objects to CSV"
            )
        return actions

admin.site.register(ExportTransaction, ExportTransactionAdmin)


import argparse
import configparser
import logging
import os
import sys

from sync import sync

DEFAULT_CONFIG_PATH = "config.ini"

def ReadConfig(filename):
    """Reads and returns the ConfigParser instance."""
    config = configparser.RawConfigParser()
    config.read(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), filename))
    return config

def CommandSync(config, args):
    """Cleanup and do syncing of products."""
    sync.DoCleanupProcedure(config)
    sync.DoSyncProcedure(config)

def CommandReauthenticate(config, args):
    """Refresh authentication token."""
    DoLazadaResetAccessToken(config, args.token)

def CommandCleanup(config, args):
    """Cleanup dangling data."""
    sync.DoCleanupProcedure(config)

def CommandCheckConfig(config, args):
    """Check if auth config is still working."""
    logging.info(config.sections())
    oauth2_service = sync.Oauth2Service()
    with oauth2_service:
        lazada_oauth2_dict = oauth2_service.GetOauth2Tokens(sync._SYSTEM_LAZADA)
        logging.info(lazada_oauth2_dict)

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)

    # Command name to function mapping.
    COMMAND_LOOKUP = {
        "sync": CommandSync,
        "lzreauth": CommandReauthenticate,
        "cleanup": CommandCleanup,
        "chkconfig": CommandCheckConfig,
    }

    # Setup argument parser.
    parser = argparse.ArgumentParser(
        description="Opencart-Lazada-Shopee syncing script.")
    parser.add_argument(
        "mode",
        choices=COMMAND_LOOKUP.keys(),
        action="store",
        help="OCLZSH syncing script mode",
    )
    parser.add_argument(
        "--config",
        action="store",
        default=os.getenv("CONFIG_PATH", DEFAULT_CONFIG_PATH),
        help="path of the config file",
    )
    parser.add_argument(
        "--token",
        action="store",
        default="",
        help="token to use when reauthenticating to Lazada",
    )
    args = parser.parse_args(sys.argv[1:])

    # Setup config path.
    config = ReadConfig(args.config)

    # Invoke action based on matching mode.
    COMMAND_LOOKUP[args.mode](config, args)


import os, sys, argparse

import numpy as np
import torch

from ppo.a2c_ppo_acktr.envs import VecPyTorch, make_vec_envs
from ppo.a2c_ppo_acktr.utils import get_render_func, get_vec_normalize

# Workaround to unpickle old model files
import ppo.a2c_ppo_acktr
sys.modules['a2c_ppo_acktr'] = ppo.a2c_ppo_acktr
sys.path.append('a2c_ppo_acktr')

parser = argparse.ArgumentParser(description='RL')
parser.add_argument('--seed', type=int, default=1,
                    help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10,
                    help='log interval, one log per n updates (default: 10)')
parser.add_argument('--env-name', default='ScratchItchJaco-v0',
                    help='environment to train on (default: ScratchItchJaco-v0)')
parser.add_argument('--load-dir', default='./trained_models/ppo/',
                    help='directory to save agent logs (default: ./trained_models/ppo/)')
parser.add_argument('--add-timestep', action='store_true', default=False,
                    help='add timestep to observations')
parser.add_argument('--non-det', action='store_true', default=False,
                    help='whether to use a non-deterministic policy')
args = parser.parse_args()

args.det = not args.non_det

env = make_vec_envs(args.env_name, args.seed + 1000, 1, None, None,
                    args.add_timestep, device='cpu', allow_early_resets=False)

# Determine the observation lengths for the robot and human, respectively
obs = env.reset()
action = torch.tensor([env.action_space.sample()])
_, _, _, info = env.step(action)
dual_robots = 'dual_robots' in info[0]
if dual_robots:
    obs_robot_len = info[0]['obs_robot_len'] // 2
    action_robot_len = info[0]['action_robot_len'] // 2
    obs_robot1 = obs[:, :obs_robot_len]
    obs_robot2 = obs[:, obs_robot_len:]
    if len(obs_robot1[0]) != obs_robot_len or len(obs_robot2[0]) != obs_robot_len:
        print('robot 1 obs shape:', len(obs_robot1[0]), 'obs space robot shape:', (obs_robot_len,))
        print('robot 2 obs shape:', len(obs_robot2[0]), 'obs space robot shape:', (obs_robot_len,))
        exit()

env = make_vec_envs(args.env_name, args.seed + 1000, 1, None, None,
                    args.add_timestep, device='cpu', allow_early_resets=False)

# Get a render function
render_func = get_render_func(env)

# We need to use the same statistics for normalization as used in training
if dual_robots:
    actor_critic_robot1, actor_critic_robot2, ob_rms = torch.load(os.path.join(args.load_dir, args.env_name + ".pt"))
else:
    actor_critic, ob_rms = torch.load(os.path.join(args.load_dir, args.env_name + ".pt"))

vec_norm = get_vec_normalize(env)
if vec_norm is not None:
    vec_norm.eval()
    vec_norm.ob_rms = ob_rms

if dual_robots:
    recurrent_hidden_states_robot1 = torch.zeros(1, actor_critic_robot1.recurrent_hidden_state_size)
    recurrent_hidden_states_robot2 = torch.zeros(1, actor_critic_robot2.recurrent_hidden_state_size)
else:
    recurrent_hidden_states = torch.zeros(1, actor_critic.recurrent_hidden_state_size)
masks = torch.zeros(1, 1)

if render_func is not None:
    render_func('human')

obs = env.reset()
if dual_robots:
    obs_robot1 = obs[:, :obs_robot_len]
    obs_robot2 = obs[:, obs_robot_len:]

if args.env_name.find('Bullet') > -1:
    import pybullet as p

    torsoId = -1
    for i in range(p.getNumBodies()):
        if (p.getBodyInfo(i)[0].decode() == "torso"):
            torsoId = i

while True:
    with torch.no_grad():
        if dual_robots:
            value_robot1, action_robot1, _, recurrent_hidden_states_robot1 = actor_critic_robot1.act(
                obs_robot1, recurrent_hidden_states_robot1, masks, deterministic=args.det)
            value_robot2, action_robot2, _, recurrent_hidden_states_robot2 = actor_critic_robot2.act(
                obs_robot2, recurrent_hidden_states_robot2, masks, deterministic=args.det)
        else:
            value, action, _, recurrent_hidden_states = actor_critic.act(
                obs, recurrent_hidden_states, masks, deterministic=args.det)

    # Obser reward and next obs
    if dual_robots:
        action = torch.cat((action_robot1, action_robot2), dim=-1)
        obs, reward, done, infos = env.step(action)
        obs_robot1 = obs[:, :obs_robot_len]
        obs_robot2 = obs[:, obs_robot_len:]
    else:
        obs, reward, done, _ = env.step(action)

    masks.fill_(0.0 if done else 1.0)

    if args.env_name.find('Bullet') > -1:
        if torsoId > -1:
            distance = 5
            yaw = 0
            humanPos, humanOrn = p.getBasePositionAndOrientation(torsoId)
            p.resetDebugVisualizerCamera(distance, yaw, -20, humanPos)

    if render_func is not None:
        render_func('human')