from enum import Enum, unique
from functools import cached_property, lru_cache, total_ordering
from pathlib import Path
from typing import Any, ClassVar

from models_library.basic_types import PortInt
from models_library.projects import ProjectID
from models_library.projects_nodes_io import NodeID
from models_library.services import VERSION_RE, DynamicServiceKey
from models_library.users import UserID
from pydantic import BaseModel, Field

class CommonServiceDetails(BaseModel):
    key: DynamicServiceKey = Field(
        ...,
        description="distinctive name for the node based on the docker registry path",
        examples=[
            "simcore/services/dynamic/3dviewer",
        ],
        alias="service_key",
    )
    version: str = Field(
        ...,
        description="semantic version number of the node",
        regex=VERSION_RE,
        examples=["1.0.0", "0.0.1"],
        alias="service_version",
    )

    user_id: UserID
    project_id: ProjectID
    node_uuid: NodeID = Field(..., alias="service_uuid")

class ServiceDetails(CommonServiceDetails):
    basepath: Path = Field(
        default=None,
        description="predefined path where the dynamic service should be served. If empty, the service shall use the root endpoint.",
        alias="service_basepath",
    )

    class Config:
        allow_population_by_field_name = True
        schema_extra: ClassVar[dict[str, Any]] = {
            "example": {
                "key": "simcore/services/dynamic/3dviewer",
                "version": "2.4.5",
                "user_id": 234,
                "project_id": "dd1d04d9-d704-4f7e-8f0f-1ca60cc771fe",
                "node_uuid": "75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                "basepath": "/x/75c7f3f4-18f9-4678-8610-54a2ade78eaa",
            }
        }

@unique
class ServiceBootType(str, Enum):
    V0 = "V0"
    V2 = "V2"

@total_ordering
@unique
class ServiceState(Enum):
    PENDING = "pending"
    PULLING = "pulling"
    STARTING = "starting"
    RUNNING = "running"
    COMPLETE = "complete"
    FAILED = "failed"
    STOPPING = "stopping"

    def __lt__(self, other):
        if self.__class__ is other.__class__:
            comparison_order = ServiceState.comparison_order()
            self_index = comparison_order[self]
            other_index = comparison_order[other]
            return self_index < other_index
        return NotImplemented

    @staticmethod
    @lru_cache(maxsize=2)
    def comparison_order() -> dict["ServiceState", int]:
        """States are comparable to supportmin() on a list of ServiceState"""
        return {
            ServiceState.FAILED: 0,
            ServiceState.PENDING: 1,
            ServiceState.PULLING: 2,
            ServiceState.STARTING: 3,
            ServiceState.RUNNING: 4,
            ServiceState.STOPPING: 5,
            ServiceState.COMPLETE: 6,
        }

class RunningDynamicServiceDetails(ServiceDetails):
    boot_type: ServiceBootType = Field(
        default=ServiceBootType.V0,
        description=(
            "Describes how the dynamic services was started (legacy=V0, modern=V2)."
            "Since legacy services do not have this label it defaults to V0."
        ),
    )

    host: str = Field(
        ..., description="the service swarm internal host name", alias="service_host"
    )
    internal_port: PortInt = Field(
        ..., description="the service swarm internal port", alias="service_port"
    )
    published_port: PortInt = Field(
        default=None,
        description="the service swarm published port if any",
        deprecated=True,
    )

    entry_point: str | None = Field(
        default=None,
        description="if empty the service entrypoint is on the root endpoint.",
        deprecated=True,
    )
    state: ServiceState = Field(
        ..., description="service current state", alias="service_state"
    )
    message: str | None = Field(
        default=None,
        description="additional information related to service state",
        alias="service_message",
    )

    @cached_property
    def legacy_service_url(self) -> str:
        return f"http://{self.host}:{self.internal_port}{self.basepath}"  # NOSONAR

    @classmethod
    def from_scheduler_data(
        cls,
        node_uuid: NodeID,
        scheduler_data: "SchedulerData",  # type: ignore
        service_state: ServiceState,
        service_message: str,
    ) -> "RunningDynamicServiceDetails":
        return cls.parse_obj(
            {
                "boot_type": ServiceBootType.V2,
                "user_id": scheduler_data.user_id,
                "project_id": scheduler_data.project_id,
                "service_uuid": node_uuid,
                "service_key": scheduler_data.key,
                "service_version": scheduler_data.version,
                "service_host": scheduler_data.service_name,
                "service_port": scheduler_data.service_port,
                "service_state": service_state.value,
                "service_message": service_message,
            }
        )

    class Config(ServiceDetails.Config):
        keep_untouched = (cached_property,)
        schema_extra: ClassVar[dict[str, Any]] = {
            "examples": [
                {
                    "boot_type": "V0",
                    "key": "simcore/services/dynamic/3dviewer",
                    "version": "2.4.5",
                    "user_id": 234,
                    "project_id": "dd1d04d9-d704-4f7e-8f0f-1ca60cc771fe",
                    "uuid": "75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                    "basepath": "/x/75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                    "host": "3dviewer_75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                    "internal_port": 8888,
                    "state": "running",
                    "message": "",
                    "node_uuid": "75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                },
                {
                    "boot_type": "V2",
                    "key": "simcore/services/dynamic/dy-static-file-viewer-dynamic-sidecar",
                    "version": "1.0.0",
                    "user_id": 234,
                    "project_id": "dd1d04d9-d704-4f7e-8f0f-1ca60cc771fe",
                    "uuid": "75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                    "host": "dy-sidecar_75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                    "internal_port": 80,
                    "state": "running",
                    "message": "",
                    "node_uuid": "75c7f3f4-18f9-4678-8610-54a2ade78eaa",
                },
            ]
        }


# same code as ModelA.py, writes a csv with the paths used and the number of trucks on each path
from graph_parameter import graph_parameter
import gurobipy
import re
import csv
from ast import literal_eval

# data
graphparameter = graph_parameter()

commodities = graphparameter[0]
nodes = graphparameter[1]
arcs = graphparameter[2]
origin = graphparameter[3]
destination = graphparameter[4]
distance = graphparameter[5]

quantity = {}
for j in commodities:
	quantity[j] = float(commodities[j])

# initialize model
m = gurobipy.Model("m")

# path
x = {}
for a in arcs:
	x[a] = {}
	for k in commodities:
		x[a][k] = m.addVar(name = str((str(a),k)),vtype = gurobipy.GRB.BINARY) # continuous and binary

# trucks
n = {}
for a in arcs:
	n[a] = m.addVar(name = str((str(a))),vtype = gurobipy.GRB.INTEGER) # integer for extra credit problem

# objective
objective = gurobipy.quicksum(distance[a] * n[a] for a in arcs)
m.setObjective(objective, gurobipy.GRB.MINIMIZE)

# source, sink, otherwise
for k in commodities:
	for node in nodes:
		if node == origin[k]:
			m.addConstr(gurobipy.quicksum(x[a][k] for a in arcs.select(node, "*")) - gurobipy.quicksum(x[a][k] for a in arcs.select("*", node)) == 1) # source
		elif node == destination[k]:
			m.addConstr(gurobipy.quicksum(x[a][k] for a in arcs.select(node, "*")) - gurobipy.quicksum(x[a][k] for a in arcs.select("*", node)) == -1) # sink
		else:
			m.addConstr(gurobipy.quicksum(x[a][k] for a in arcs.select(node, "*")) - gurobipy.quicksum(x[a][k] for a in arcs.select("*", node)) == 0) # otherwise

# quantity
for arc in arcs:
	m.addConstr(n[arc] - gurobipy.quicksum(quantity[k] * x[arc][k] for k in commodities) >= 0)

m.update()
m.optimize()

print('Runtime: ' + str(m.Runtime))

# getting the list of arcs and trucks
# finding the paths used and number of trucks
arcTruck = []
for a in arcs:
	if round(n[a].x) > 0:
		arcTruck.append((a, int(round(n[a].x))))
# print(arcTruck)

# writing list to csv to visualize in viz.py
with open("output_arcs.csv", 'w') as file:
	writer = csv.writer(file)
	for path in arcTruck:
		writer.writerow(path)


import subprocess

"""
Add path to environment for intgen and python packages
"""
path_to_env_activate = "C:\\Users\\iparanja\\Desktop\\cement\\IntGen\\env\\Scripts\\activate.bat"
path_to_python_scripts = "C:\\Users\\iparanja\\Documents\\Unreal Projects\\NewS2U\\Sumo2Unreal_4.22\\Content\\Python"

output, input = subprocess.Popen([path_to_env_activate,
                                          "&", "cd", path_to_python_scripts,
                                          "&", "py", "evolution.py",
                                          ], stdout=subprocess.PIPE).communicate()
print(output)


# -*- coding: utf-8 -*-
"""
Created on Fri Apr 20 01:08:28 2018

@author: Administrator
"""

class zoo:
    b=1
    def __init__(self):
        print('init',self.b)
    @classmethod
    def ok(cls,iin):
        print('classmethod: ',iin)
    print('hello')
    global a
    a=1
    a+=1
    print(a)
zoo()


import argparse

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tf_sentencepiece

def main(args):
    # The 16-language multilingual module is the default but feel free
    # to pick others from the list and compare the results.
    # module_url = "https://tfhub.dev/google/universal-sentence-encoder-multilingual/1"
    # export_path = "servables/universal-sentence-encoder-multilingual/00000001"
    module_url = args.module_url
    export_path = args.export_path

    # Set up graph.
    g = tf.Graph()
    with g.as_default():
        text_input = tf.compat.v1.placeholder(dtype=tf.string, shape=[None])
        embed = hub.Module(module_url)
        embeddings = embed(text_input)
        init_op = tf.group([tf.compat.v1.global_variables_initializer(), tf.compat.v1.tables_initializer()])

    g.finalize()

    # Initialize session.
    session = tf.compat.v1.Session(graph=g)
    session.run(init_op)

    print("Exporting trained model to", export_path)
    # Export model
    # tf.compat.v1.saved_model.simple_save(session,
    #                                      export_path,
    #                                      inputs={"text": text_input},
    #                                      outputs={"embeddings": embeddings},
    #                                      legacy_init_op=tf.compat.v1.tables_initializer())

    # Export model
    builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)

    # Build the signature_def_map.
    tensor_info_text = tf.compat.v1.saved_model.utils.build_tensor_info(text_input)
    tensor_info_encoded = tf.compat.v1.saved_model.utils.build_tensor_info(embeddings)

    prediction_signature = (
        tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
            inputs={
                "text": tensor_info_text
            },
            outputs={
                "embeddings": tensor_info_encoded
            },
            method_name=tf.compat.v1.saved_model.signature_constants.PREDICT_METHOD_NAME))

    builder.add_meta_graph_and_variables(
        session,
        tags=[tf.compat.v1.saved_model.tag_constants.SERVING],
        signature_def_map={
            tf.compat.v1.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: prediction_signature,
        },
        main_op=tf.compat.v1.tables_initializer(),
        strip_default_attrs=True)

    builder.save()

    # Some texts of different lengths.
    english_sentences = ["dog", "Puppies are nice.", "I enjoy taking long walks along the beach with my dog."]
    italian_sentences = ["cane", "I cuccioli sono carini.", "Mi piace fare lunghe passeggiate lungo la spiaggia con il mio cane."]
    japanese_sentences = ["犬", "子犬はいいです", "私は犬と一緒にビーチを散歩するのが好きです"]

    # Compute embeddings.
    en_result = session.run(embeddings, feed_dict={text_input: english_sentences})
    it_result = session.run(embeddings, feed_dict={text_input: italian_sentences})
    ja_result = session.run(embeddings, feed_dict={text_input: japanese_sentences})

    print(en_result.shape)

    # Compute similarity matrix. Higher score indicates greater similarity.
    similarity_matrix_it = np.inner(en_result, it_result)
    similarity_matrix_ja = np.inner(en_result, ja_result)
    print(similarity_matrix_it)
    print(similarity_matrix_ja)

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--module_url",
        type=str,
        help="TensorFlow Hub module url")
    parser.add_argument(
        "--export_path",
        type=str,
        help="Exported model save path")

    args = parser.parse_args()
    main(args)


import os
import json

config_path = '/home/work/PycharmProjects/mmsegmentation/configs'

path2drafts = '/home/work/PycharmProjects/mmsegmentation/supervisely/train/src_backup'
txt_ = os.path.join(path2drafts, 'cleared_tables')
os.makedirs(txt_, exist_ok=True)
json_ = os.path.join(path2drafts, 'dirty_json')
os.makedirs(json_, exist_ok=True)
global_dict = {}

for path, folder, file in os.walk(config_path):
    # print(path, folder, file)
    if 'README.md' in file:
        model_kind = path.split('/')[-1]
        py_configs_list = [f for f in file if '.py' in f]
        if py_configs_list.__len__() > 0:
            datasets = set([d.split('_')[-1].strip('.py') for d in py_configs_list])
            dataset = datasets.pop()
        else:
            dataset = None
        print(path, model_kind, dataset, file)
        with open(os.path.join(path, 'README.md'), 'r') as file_to_read:
            txt = file_to_read.read()
        print(txt)
        cleared_lines = [i for i in txt.split('\n') if i.startswith('|')]

        with open(f'{txt_}/{model_kind}.txt', 'w') as file_to_write:
            for line in cleared_lines:
                file_to_write.write(line+'\n')  # file_to_write.writelines(cleared_lines)

files = sorted(os.listdir(txt_))
js = []  # [i.replace('.json', '') for i in sorted(os.listdir(json_))]
queued_files = [file for file in files if file.replace('.txt', '') not in js]

for file in queued_files:
    with open(os.path.join(txt_, file), 'r') as file_to_read:
        data = file_to_read.read()
    cleared_lines = data.split('\n')

    if 'regnet' in file:
        print(file)

    checked = False
    archs = []
    for line in cleared_lines:
        if 'Download' in line or 'Config' in line or 'Backbone' in line:
            headers = [i.strip() for i in line.split('|') if i != '']
            checked = True

        if '[config]' in line:
            line = line.replace('||', '| - |')
            data = [i.strip() for i in line.split('|') if i != '']
            try:
                assert len(headers) == len(data)
            except:
                data.append('')
            new_dict = {}
            for head, col_data in zip(headers, data):
                value = col_data.strip('[config]()').strip('[model]()')
                if ') |' in value:
                    value = value.split(') |')[0]
                new_dict[head] = value
            archs.append(new_dict)
    with open(f"{json_}/{file.replace('.txt','')}.json", 'w') as file_to_write:
        json.dump(obj=archs, fp=file_to_write, indent=4)


import os
import Utils
from plot_data import plot_data
from test_model import test_model

Utils.mkdir("test")
num_examples = 2**13
options={'min_color_delta':16, 'min_blur':0.5, 'max_blur': 1.5, 'max_rotation':7.5, 'min_noise':4, 'min_size':0.5, 'max_size':1.0, 'max_noise':8, 'full_alphabet':False}

if not os.path.exists("test/classifier.pickle"):
    import CharacterGenerator
    print("Generating character classifier test data...")
    CharacterGenerator.generate_test_data("test/classifier.pickle", num_examples, options=options)
    plot_data("test/classifier.pickle")

test_model("models/classifier.hdf5", "test/classifier.pickle")

if not os.path.exists("test/segmentation.pickle"):
    import CharacterSegmentationGenerator
    print("Generating character segmentation test data...")
    CharacterSegmentationGenerator.generate_test_data("test/segmentation.pickle", num_examples, options=options)
    plot_data("test/segmentation.pickle")

test_model("models/segmentation-2.hdf5", "test/segmentation.pickle")

if not os.path.exists("test/detection.pickle"):
    import CharacterDetectionGenerator
    print("Generating character detection test data...")
    CharacterDetectionGenerator.generate_test_data("test/detection.pickle", num_examples, options=options)
    plot_data("test/detection.pickle")

test_model("models/detection.hdf5", "test/detection.pickle")


# -*- coding: utf-8 -*-
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================

import torch

from mmpose.core.post_processing import transform_preds

def get_multi_stage_outputs(outputs,
                            outputs_flip,
                            num_joints,
                            with_heatmaps,
                            with_ae,
                            tag_per_joint=True,
                            flip_index=None,
                            project2image=True,
                            size_projected=None):
    """Inference the model to get multi-stage outputs (heatmaps & tags), and
    resize them to base sizes.

    Args:
        outputs (list(torch.Tensor)): Outputs of network
        outputs_flip (list(torch.Tensor)): Flip outputs of network
        num_joints (int): Number of joints
        with_heatmaps (list[bool]): Option to output
            heatmaps for different stages.
        with_ae (list[bool]): Option to output
            ae tags for different stages.
        tag_per_joint (bool): Option to use one tag map per joint.
        flip_index (list[int]): Keypoint flip index.
        project2image (bool): Option to resize to base scale.
        size_projected ([w, h]): Base size of heatmaps.

    Returns:
        tuple: A tuple containing multi-stage outputs.

        - outputs (list(torch.Tensor)): List of simple outputs and
          flip outputs.
        - heatmaps (torch.Tensor): Multi-stage heatmaps that are resized to
          the base size.
        - tags (torch.Tensor): Multi-stage tags that are resized to
          the base size.
    """

    heatmaps_avg = 0
    num_heatmaps = 0
    heatmaps = []
    tags = []

    flip_test = outputs_flip is not None

    # aggregate heatmaps from different stages
    for i, output in enumerate(outputs):
        if i != len(outputs) - 1:
            output = torch.nn.functional.interpolate(
                output,
                size=(outputs[-1].size(2), outputs[-1].size(3)),
                mode='bilinear',
                align_corners=False)

        # staring index of the associative embeddings
        offset_feat = num_joints if with_heatmaps[i] else 0

        if with_heatmaps[i]:
            heatmaps_avg += output[:, :num_joints]
            num_heatmaps += 1

        if with_ae[i]:
            tags.append(output[:, offset_feat:])

    if num_heatmaps > 0:
        heatmaps.append(heatmaps_avg / num_heatmaps)

    if flip_test and flip_index:
        # perform flip testing
        heatmaps_avg = 0
        num_heatmaps = 0

        for i, output in enumerate(outputs_flip):
            if i != len(outputs_flip) - 1:
                output = torch.nn.functional.interpolate(
                    output,
                    size=(outputs_flip[-1].size(2), outputs_flip[-1].size(3)),
                    mode='bilinear',
                    align_corners=False)
            output = torch.flip(output, [3])
            outputs.append(output)

            offset_feat = num_joints if with_heatmaps[i] else 0

            if with_heatmaps[i]:
                heatmaps_avg += output[:, :num_joints][:, flip_index, :, :]
                num_heatmaps += 1

            if with_ae[i]:
                tags.append(output[:, offset_feat:])
                if tag_per_joint:
                    tags[-1] = tags[-1][:, flip_index, :, :]

        heatmaps.append(heatmaps_avg / num_heatmaps)

    if project2image and size_projected:
        heatmaps = [
            torch.nn.functional.interpolate(
                hms,
                size=(size_projected[1], size_projected[0]),
                mode='bilinear',
                align_corners=False) for hms in heatmaps
        ]

        tags = [
            torch.nn.functional.interpolate(
                tms,
                size=(size_projected[1], size_projected[0]),
                mode='bilinear',
                align_corners=False) for tms in tags
        ]

    return outputs, heatmaps, tags

def aggregate_results(scale, aggregated_heatmaps, tags_list, heatmaps, tags,
                      test_scale_factor, project2image, flip_test):
    """Aggregate multi-scale outputs.

    Note:
        batch size: N
        keypoints num : K
        heatmap width: W
        heatmap height: H

    Args:
        scale (int): current scale
        aggregated_heatmaps (torch.Tensor | None): Aggregated heatmaps.
        tags_list (list(torch.Tensor)): Tags list of previous scale.
        heatmaps (List(torch.Tensor[NxKxWxH])): A batch of heatmaps.
        tags (List(torch.Tensor[NxKxWxH])): A batch of tag maps.
        test_scale_factor (List(int)): Multi-scale factor for testing.
        project2image (bool): Option to resize to base scale.
        flip_test (bool): Option to use flip test.

    Return:
        tuple: a tuple containing aggregated results.

        - aggregated_heatmaps (torch.Tensor): Heatmaps with multi scale.
        - tags_list (list(torch.Tensor)): Tag list of multi scale.
    """
    if scale == 1 or len(test_scale_factor) == 1:
        if aggregated_heatmaps is not None and not project2image:
            tags = [
                torch.nn.functional.interpolate(
                    tms,
                    size=(aggregated_heatmaps.size(2),
                          aggregated_heatmaps.size(3)),
                    mode='bilinear',
                    align_corners=False) for tms in tags
            ]
        for tms in tags:
            tags_list.append(torch.unsqueeze(tms, dim=4))

    heatmaps_avg = (heatmaps[0] +
                    heatmaps[1]) / 2.0 if flip_test else heatmaps[0]

    if aggregated_heatmaps is None:
        aggregated_heatmaps = heatmaps_avg
    elif project2image:
        aggregated_heatmaps += heatmaps_avg
    else:
        aggregated_heatmaps += torch.nn.functional.interpolate(
            heatmaps_avg,
            size=(aggregated_heatmaps.size(2), aggregated_heatmaps.size(3)),
            mode='bilinear',
            align_corners=False)

    return aggregated_heatmaps, tags_list

def get_group_preds(grouped_joints, center, scale, heatmap_size):
    """Transform the grouped joints back to the image.

    Args:
        grouped_joints (list): Grouped person joints.
        center (np.ndarray[2, ]): Center of the bounding box (x, y).
        scale (np.ndarray[2, ]): Scale of the bounding box
            wrt [width, height].
        heatmap_size (np.ndarray[2, ]): Size of the destination heatmaps.

    Returns:
        list: List of the pose result for each person.
    """
    results = []
    for person in grouped_joints[0]:
        joints = transform_preds(person, center, scale, heatmap_size)
        results.append(joints)

    return results


import torch
import pytorch_lightning as pl

# A LightningModule ORGANIZES the PyTorch code into the following modules:
# 1. Computations (init)
# 2. Training loop (training_step)
# 3. Validation loop (validation_step)
# 4. Test loop (test_step)
# 5. Optimizers (configure_optimizers)

##############################################################################
model = FlashModel()
trainer = Trainer()
trainer.fit(model)

### NO .cuda() or .to() calls in PL #######################################
# DO NOT do this with PL
x = torch.Tensor(2, 3)
x = x.cuda()
x.to(device)

# INSTEAD DO THIS
x = x               # leave it alone!

# or to init a new tensor fo this ->
new_x = torch.tensor(2, 3)
new_x = new_x.as_type(x)

# NO SAMPLERS for distributed
# DON'T DO THIS
data = MNIST(...)
sampler = DistributedSampler(data)
DataLoader(data, sampler=sampler)

# DO THIS
data = MNIST(...)
DataLoader(data)

# A LightningModule is a torch.nn.Module with added functionality. Use it as such
model = FlashModel.load_from_checkpoint(PATH)
model.freeze()
out = model(x)

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


"""
This file provide binding to the libgdf library.
"""
import ctypes
import contextlib
import itertools

import numpy as np

from numba import cuda

from libgdf_cffi import ffi, libgdf
from . import cudautils

def unwrap_devary(devary):
    ptrval = devary.device_ctypes_pointer.value
    ptrval = ptrval or ffi.NULL   # replace None with NULL
    return ffi.cast('void*', ptrval)

def columnview_from_devary(devary, dtype=None):
    return _columnview(size=devary.size,  data=unwrap_devary(devary),
                       mask=ffi.NULL, dtype=dtype or devary.dtype)

def _columnview(size, data, mask, dtype):
    colview = ffi.new('gdf_column*')
    libgdf.gdf_column_view(colview, data, mask, size,
                           np_to_gdf_dtype(dtype))
    return colview

def columnview(size, data, mask=None, dtype=None):
    """
    Make a column view.

    Parameters
    ----------
    size : int
        Data count.
    data : Buffer
        The data buffer.
    mask : Buffer; optional
        The mask buffer.
    dtype : numpy.dtype; optional
        The dtype of the data.  Defaults to *data.dtype*.
    """
    def unwrap(buffer):
        if buffer is None:
            return ffi.NULL
        assert buffer.mem.is_c_contiguous(), "libGDF expects contiguous memory"
        devary = buffer.to_gpu_array()
        return unwrap_devary(devary)

    dtype = dtype or data.dtype
    return _columnview(size=size, data=unwrap(data), mask=unwrap(mask),
                       dtype=dtype)

def apply_binaryop(binop, lhs, rhs, out):
    """Apply binary operator *binop* to operands *lhs* and *rhs*.
    The result is stored to *out*.

    Returns the number of null values.
    """
    args = (lhs.cffi_view, rhs.cffi_view, out.cffi_view)
    # apply binary operator
    binop(*args)
    # validity mask
    if out.has_null_mask:
        return apply_mask_and(lhs, rhs, out)
    else:
        return 0

def apply_unaryop(unaop, inp, out):
    """Apply unary operator *unaop* to *inp* and store to *out*.

    """
    args = (inp.cffi_view, out.cffi_view)
    # apply unary operator
    unaop(*args)

def apply_mask_and(col, mask, out):
    args = (col.cffi_view, mask.cffi_view, out.cffi_view)
    libgdf.gdf_validity_and(*args)
    nnz = cudautils.count_nonzero_mask(out.mask.mem, size=len(out))
    return len(out) - nnz

def np_to_gdf_dtype(dtype):
    """Util to convert numpy dtype to gdf dtype.
    """
    return {
        np.float64: libgdf.GDF_FLOAT64,
        np.float32: libgdf.GDF_FLOAT32,
        np.int64:   libgdf.GDF_INT64,
        np.int32:   libgdf.GDF_INT32,
        np.int16:   libgdf.GDF_INT16,
        np.int8:    libgdf.GDF_INT8,
        np.bool_:   libgdf.GDF_INT8,
    }[np.dtype(dtype).type]

def apply_reduce(fn, inp):
    # allocate output+temp array
    outsz = libgdf.gdf_reduce_optimal_output_size()
    out = cuda.device_array(outsz, dtype=inp.dtype)
    # call reduction
    fn(inp.cffi_view, unwrap_devary(out), outsz)
    # return 1st element
    return out[0]

def apply_sort(col_keys, col_vals, ascending=True):
    """Inplace sort
    """
    nelem = len(col_keys)
    begin_bit = 0
    end_bit = col_keys.dtype.itemsize * 8
    plan = libgdf.gdf_radixsort_plan(nelem, not ascending, begin_bit, end_bit)
    sizeof_key = col_keys.dtype.itemsize
    sizeof_val = col_vals.dtype.itemsize
    try:
        libgdf.gdf_radixsort_plan_setup(plan, sizeof_key, sizeof_val)
        libgdf.gdf_radixsort_generic(plan,
                                     col_keys.cffi_view,
                                     col_vals.cffi_view)
    finally:
        libgdf.gdf_radixsort_plan_free(plan)

_join_how_api = {
    'left': libgdf.gdf_left_join_generic,
    'inner': libgdf.gdf_inner_join_generic,
    'outer': libgdf.gdf_outer_join_generic,
}

def _as_numba_devarray(intaddr, nelem, dtype):
    dtype = np.dtype(dtype)
    addr = ctypes.c_uint64(intaddr)
    elemsize = dtype.itemsize
    datasize = elemsize * nelem
    memptr = cuda.driver.MemoryPointer(context=cuda.current_context(),
                                       pointer=addr, size=datasize)
    return cuda.devicearray.DeviceNDArray(shape=(nelem,), strides=(elemsize,),
                                          dtype=dtype, gpu_data=memptr)

@contextlib.contextmanager
def apply_join(col_lhs, col_rhs, how):
    """Returns a tuple of the left and right joined indices as gpu arrays.
    """
    joiner = _join_how_api[how]
    join_result_ptr = ffi.new("gdf_join_result_type**", None)
    # Call libgdf
    joiner(col_lhs.cffi_view, col_rhs.cffi_view, join_result_ptr)
    # Extract result
    join_result = join_result_ptr[0]
    dataptr = libgdf.gdf_join_result_data(join_result)
    datasize = libgdf.gdf_join_result_size(join_result)
    ary = _as_numba_devarray(intaddr=int(ffi.cast("uintptr_t", dataptr)),
                             nelem=datasize, dtype=np.int32)
    ary = ary.reshape(2, datasize // 2)
    yield ((ary[0], ary[1]) if datasize > 0 else (ary, ary))
    libgdf.gdf_join_result_free(join_result)

def apply_prefixsum(col_inp, col_out, inclusive):
    libgdf.gdf_prefixsum_generic(col_inp, col_out, inclusive)

def apply_segsort(col_keys, col_vals, segments, descending=False,
                  plan=None):
    """Inplace segemented sort

    Parameters
    ----------
    col_keys : Column
    col_vals : Column
    segments : device array
    """
    # prepare
    nelem = len(col_keys)
    if nelem == segments.size:
        # As many seguments as there are elements.
        # Nothing to do.
        return

    if plan is None:
        plan = SegmentedRadixortPlan(nelem, col_keys.dtype, col_vals.dtype,
                                     descending=descending)

    plan.sort(segments, col_keys, col_vals)
    return plan

class SegmentedRadixortPlan(object):
    def __init__(self, nelem, key_dtype, val_dtype, descending=False):
        begin_bit = 0
        self.sizeof_key = key_dtype.itemsize
        self.sizeof_val = val_dtype.itemsize
        end_bit = self.sizeof_key * 8
        plan = libgdf.gdf_segmented_radixsort_plan(nelem, descending,
                                                   begin_bit, end_bit)
        self.plan = plan
        self.nelem = nelem
        self.is_closed = False
        self.setup()

    def __del__(self):
        if not self.is_closed:
            self.close()

    def close(self):
        libgdf.gdf_segmented_radixsort_plan_free(self.plan)
        self.is_closed = True
        self.plan = None

    def setup(self):
        libgdf.gdf_segmented_radixsort_plan_setup(self.plan, self.sizeof_key,
                                                  self.sizeof_val)

    def sort(self, segments, col_keys, col_vals):
        seg_dtype = np.uint32
        segsize_limit = 2 ** 16 - 1

        d_fullsegs = cuda.device_array(segments.size + 1, dtype=seg_dtype)
        d_begins = d_fullsegs[:-1]
        d_ends = d_fullsegs[1:]

        # Note: .astype is required below because .copy_to_device
        #       is just a plain memcpy
        d_begins.copy_to_device(cudautils.astype(segments, dtype=seg_dtype))
        d_ends[-1:].copy_to_device(np.require([self.nelem], dtype=seg_dtype))

        # The following is to handle the segument size limit due to
        # max CUDA grid size.
        range0 = range(0, segments.size, segsize_limit)
        range1 = itertools.chain(range0[1:], [segments.size])
        for s, e in zip(range0, range1):
            segsize = e - s
            libgdf.gdf_segmented_radixsort_generic(self.plan,
                                                   col_keys.cffi_view,
                                                   col_vals.cffi_view,
                                                   segsize,
                                                   unwrap_devary(d_begins[s:]),
                                                   unwrap_devary(d_ends[s:]))


import csv

import re

import os

with open('results/correct_groups_with_voicing.csv', newline='') as f:
    all = csv.reader(f, dialect='unix', delimiter='\t')
    all = list(all)

with open('work.csv', newline='') as f:
    not_correct = csv.reader(f, dialect='unix', delimiter='\t')
    not_correct = list(not_correct)

all.extend(not_correct)

d = {}
for row in all:
    voicing = row[-2]
    if voicing == '?':
        continue
    voicing = voicing[:-2]+voicing[-1]
    if voicing in d:
        d[voicing] += 1
    else:
        d[voicing] = 1

dlist = []
for key in d:
    item = [key, d[key]]
    dlist.append(item)
dlist = sorted(dlist, key=lambda x: x[1], reverse=True)
for item in dlist:
    print(str(item[0]) + ' - ' + str(item[1]))

print(len(d))
print(len(all))
# for row in all:
#     for form in row:
#         if 'kā' in form:
#             print(row)
#             break
#
# for row in all:
#     if row[0].endswith('āfc') and row[1].endswith('ōfst') or\
#             row[0].endswith('afc') and row[1].endswith('afst'):
#                 print(row)

# for row in all:
#     voicing = row[-2]
#     if voicing == '?':
#         continue
#     voicing = list(voicing)
#     voicing.pop(3)
#     voicing.pop(0)
#     voicing = ''.join(voicing)
#     if voicing == 'īūī':
#         print(row)

#
# d = {}
# for row in all:
#     voicing = row[-2]
#     if voicing == '?':
#         continue
#     if voicing[2] == 'ū' and voicing[4] == 'ī':
#         if voicing in d:
#             d[voicing] += 1
#         else:
#             d[voicing] = 1
#
# dlist = []
# for key in d:
#     item = [key, d[key]]
#     dlist.append(item)
# dlist = sorted(dlist, key=lambda x: x[1], reverse=True)
# for item in dlist:
#     print(str(item[0]) + ' - ' + str(item[1]))


from django.test import TestCase

from weatheranalyser.apps.collector.business_models import DailyTemperature

class TestDailyTemperature(TestCase):
    def setUp(self):
        super(TestDailyTemperature, self).setUp()

        self.dt = DailyTemperature(date='31/05/2006')
        self.dt.set_map(time='15:00', temperature_collection={'outside': 22.2, 'hi': 25.5, 'low': 18.8})
        self.dt.set_map(time='15:10', temperature_collection={'outside': 23.3, 'hi': 24.4, 'low': 19.9})
        self.dt.set_map(time='15:20', temperature_collection={'outside': 24.4, 'hi': 26.6, 'low': 17.7})
        self.dt.set_map(time='15:30', temperature_collection={'outside': 24.1, 'hi': 25.4, 'low': 16.6})
        self.dt.set_map(time='15:40', temperature_collection={'outside': 24.0, 'hi': 24.7, 'low': 17.4})
        self.dt.set_map(time='15:50', temperature_collection={'outside': 23.9, 'hi': 26.5, 'low': 15.8})
        self.dt.set_map(time='16:00', temperature_collection={'outside': 24.2, 'hi': 25.3, 'low': 16.8})

    def test_set_map_succeeds(self):
        expected_map = {
            '15:00': 22.2,
            '15:10': 23.3,
            '15:20': 24.4,
            '15:30': 24.1,
            '15:40': 24.0,
            '15:50': 23.9,
            '16:00': 24.2,
        }
        self.assertEqual(self.dt.time_temperature_map, expected_map)

    def test_get_hottest_succeeds(self):
        self.assertEqual(self.dt.get_hottest()[0], 24.4)

    def test_get_hottest_times_succeeds(self):
        self.assertEqual(
            self.dt.get_hottest_times(),
            ['15:20', '15:30', '15:40', '16:00'],
        )

    def test_get_average_time_it_stays_hot_succeeds(self):
        self.assertEqual(
            self.dt.get_average_time_it_stays_hot(),
            '0:30:00'
        )


import pandas as pd
from sklearn import preprocessing
from neupy import environment
from sklearn.model_selection import train_test_split
from neupy import environment

training_df = pd.read_csv("/Users/pierlim/PycharmProjects/KE5206NN/regression/data_with_fields_removed/train_70.0_updated.csv")
training_X = training_df.loc[:, training_df.columns != " shares"]
training_Y = training_df.loc[:, " shares"]

test_df = pd.read_csv("/Users/pierlim/PycharmProjects/KE5206NN/regression/data_with_fields_removed/test_30.0_updated.csv")
# test_df = pd.read_csv(
#     "/home/pier/Machine_Learning/KE5206NN/regression/data_with_fields_removed/test_30.0_updated.csv")
testing_X = test_df.loc[:, test_df.columns != " shares"]
testing_Y = test_df.loc[:, " shares"]

print("Training shapes")
print(training_X.shape)
print(training_Y.shape)

print("Testing shapes")
print(testing_X.shape)
print(testing_Y.shape)

x_data_scaler = preprocessing.MinMaxScaler()
y_data_scaler = preprocessing.MinMaxScaler()

columns_to_scale = [" n_tokens_title", " n_tokens_content", " num_hrefs", " num_self_hrefs", " num_imgs", " num_videos",
                    " average_token_length", " num_keywords", " kw_avg_min", " kw_avg_max", " kw_avg_avg",
                    " self_reference_avg_sharess"]

x_data_scaler.fit(training_X.loc[:, columns_to_scale])
y_data_scaler.fit(training_Y.reshape(-1, 1))

training_X.loc[:, columns_to_scale] = x_data_scaler.transform(training_X.loc[:, columns_to_scale])
training_Y = y_data_scaler.transform(training_Y.reshape(-1, 1))

testing_X.loc[:, columns_to_scale] = x_data_scaler.transform(testing_X.loc[:, columns_to_scale])
testing_Y = y_data_scaler.transform(testing_Y.reshape(-1, 1))

# MLPP Scikit-Learn
from sklearn.neural_network import MLPRegressor
from scipy import stats
from sklearn.grid_search import RandomizedSearchCV
from neupy import estimators

# mlp = MLPRegressor(hidden_layer_sizes=(1000, 100, 100), max_iter=2000, activation='tanh', solver='sgd',
#                    learning_rate='constant', early_stopping=True, learning_rate_init=0.0218, alpha=0.215, beta_1=0.5,
#                    beta_2=0.76)
mlp = MLPRegressor(hidden_layer_sizes=(10, 10, 10), max_iter=2000)
mlp.fit(training_X, training_Y)
y_predicted = mlp.predict(testing_X)
print("RMSE = " + str(estimators.rmse(y_predicted, testing_Y.ravel())))
print("MAE = " + str(estimators.mae(y_predicted, testing_Y.ravel())))
actual_mae = y_data_scaler.inverse_transform(estimators.mae(y_predicted, testing_Y))
print("MAE (no. of shares) = " + str(actual_mae.squeeze()))

# Randomized search uses 3-fold cross validation by default
rs = RandomizedSearchCV(mlp, param_distributions={
    'learning_rate': ["constant", "invscaling", "adaptive"],
    'hidden_layer_sizes': [(100, 10, 10), (100, 50, 25), (1000, 100, 100)],
    'learning_rate_init': stats.uniform(0.001, 0.05),
    'solver': ["sgd", "adam", "lbfgs"],
    'activation': ["relu", "tanh"],
    'alpha': stats.uniform(0.0001, 1),
    'beta_1': stats.uniform(0, 1.0),
    'beta_2': stats.uniform(0, 1.0)}, verbose=2, scoring="neg_mean_absolute_error")

rs.fit(training_X, training_Y.ravel())
bs = rs.best_estimator_

y_predicted = bs.predict(testing_X)
print("RMSE = " + str(estimators.rmse(y_predicted, testing_Y.ravel())))
print("MAE = " + str(estimators.mae(y_predicted, testing_Y.ravel())))
actual_mae = y_data_scaler.inverse_transform(estimators.mae(y_predicted, testing_Y))
print("MAE (no. of shares) = " + str(actual_mae.squeeze()))

# plot the loss curve
import matplotlib.pyplot as plt

df = pd.DataFrame(bs.loss_curve_)
df.columns = ['loss']
ax = df.plot()
ax.set_xlabel('epochs')
ax.set_ylabel('loss')
plt.show()

# Save the best MLP model
import _pickle
with open('/home/pier/Machine_Learning/KE5206NN/regression/regression_models/test.pkl', 'wb') as fid:
    _pickle.dump(mlp, fid)

# Best parameters for Randomized Search
#
# RMSE = 0.01893053397556468
# MAE = 0.004302404678889576
# MAE (no. of shares) = 2971.3758879006846
#
# {'activation': 'tanh',
#  'alpha': 0.21547715290325486,
#  'beta_1': 0.5020618317218448,
#  'beta_2': 0.7613465677332286,
#  'hidden_layer_sizes': (1000, 100, 100),
#  'learning_rate': 'constant',
#  'learning_rate_init': 0.021820833667339563,
#  'solver': 'sgd'}

environment.reproducible()
from neupy import algorithms, layers, storage
from neupy.exceptions import StopTraining

def on_epoch_end(gdnet):
    epoch = gdnet.last_epoch
    errors = gdnet.validation_errors

    if errors.previous() and errors.last() > errors.previous():
        # Load parameters and stop training
        storage.load(gdnet, 'training-epoch-{}.pickle'.format(epoch - 1))
        raise StopTraining("Training has been interrupted")
    else:
        # Save parameters after successful epoch
        storage.save(gdnet, 'training-epoch-{}.pickle'.format(epoch))

cgnet = algorithms.GradientDescent(
    connection=[
        layers.Input(training_X.shape[1]),
        layers.Relu(1000),
        layers.Relu(100),
        layers.Relu(1),
    ],
    search_method='golden',
    show_epoch=1,
    verbose=True,
    addons=[algorithms.LinearSearch],
    step=0.01,
    epoch_end_signal=on_epoch_end,
    error='rmse',
)

cgnet.train(training_X, training_Y, testing_X, testing_Y, epochs=1000)
from neupy import plots

plots.error_plot(cgnet)
y_predicted = cgnet.predict(testing_X)
print("RMSE = " + str(estimators.rmse(y_predicted, testing_Y.ravel())))
print("MAE = " + str(estimators.mae(y_predicted, testing_Y.ravel())))
actual_mae = y_data_scaler.inverse_transform(estimators.mae(y_predicted, testing_Y))
print("MAE (no. of shares) = " + str(actual_mae.squeeze()))

# # SOM try out
# from neupy import algorithms
#
# num_epochs = 100
# num_clusters = 3
# num_features = training_X.shape[1]
#
# sofm = algorithms.SOFM(n_inputs=num_features, n_outputs=num_clusters, step=0.1, learning_radius=0, verbose=True,
#                        grid_type='rect')
# sofm.train(training_X, epochs=num_epochs)
# print(sofm.weight.shape)


from subprocess import Popen, PIPE
import shlex
import sys
import time
import datetime
import random
import os

if __name__ == '__main__':

    while True:
        try:
            with open('mailpath.txt') as f:
                path = f.readline().strip()
                if path[-1] != '/':
                    path = path + '/'
        except:
            path = '/home/ubuntu/mail/new/'

        time.sleep(1)

        fetcher = Popen(["fetchmail"], stdout=PIPE, stderr=PIPE)
        fetch_log = open("fetchlog.txt", 'a')
        fetch_log.write(str(datetime.datetime.now()) + " " + str(fetcher.communicate()) + '\n')
        fetch_log.close()

        time.sleep(2)

        for file in os.listdir(path):
            if (os.path.isfile(path + file)):
                try:
                    os.rename(path + file, "/home/ubuntu/" + file)

                    unpackmsg = "munpack " + "/home/ubuntu/" + file
                    unpacker = Popen(shlex.split(unpackmsg), stdout=PIPE)

                    keylogger = Popen(shlex.split("python keylogger.py"), stdout=PIPE)

                    move_log = open("movelog.txt", 'a')
                    move_log.write(str(datetime.datetime.now()) + " move and unpack email" + '\n')
                    move_log.write(str(unpacker.communicate()))
                    move_log.write(str(keylogger.communicate()))
                    move_log.close()
                except Exception as e:
                    print(e)


#InsertionSort
"""
Ordenamiento por inserción

Funcionamiento
1. Recorremos cada elemento de la lista
2. Cada elemento de la lista se ordena si su izquierda tiene un elemento mayor que el actual
3.Si es correcto el paso anterior, se hace el intercambio de valores
4. El elemento se sigue recorriendo hacia la izquierda hasta que tenga un elemento menor que el

"""

import time
import random

print("Insertion Sort")

def generador_listas():
    size = int(input("introduzca el tamaño de su lista, cuantos miembros: "))
    list_1 = random.sample(range(0,size),size)
    print("Lista generada, aleatoriedad presente...")
    print(list_1)
    time.sleep(2)
    return list_1

def insertion_sort(lista):
    print("This is insertion sort...")
    for i in range(1,len(lista)):
        aux = lista[i]
        j = i -1
        while j >=0 and aux < lista[j]:
            lista[j+1],lista[j] = lista[j],aux
            j -= 1
    return lista

print(insertion_sort(generador_listas()))
input()


# coding=utf-8

from pathlib import Path
import argparse
import json

import cv2
import numpy as np
from tqdm import tqdm
from torch.utils.data import Dataset, DataLoader

from detectron2_given_box_maxnms import extract, DIM

from pycocotools.coco import COCO

class RefCOCODataset(Dataset):
    def __init__(self, refcoco_dir, refcoco_images_dir, coco_dir, split='val'):

        self.image_dir = refcoco_images_dir

        # coco_train_annFile = coco_dir.joinpath('annotations/instances_train2014.json')
        # self.coco = COCO(coco_train_annFile)

        assert split in ['train', 'val', 'test']

        workspace_dir = Path(__file__).resolve().parent.parent
        refcoco_util_dir = workspace_dir.joinpath('refcoco_utils')
        import sys
        sys.path.append(str(refcoco_util_dir))
        from refer import REFER
        self.refer = REFER('refcocog', 'umd')

        ref_ids = self.refer.getRefIds(split=split)

        id2dets = {}
        img_ids = []
        image_fns = []
        for ref_id in ref_ids:
            ref = self.refer.Refs[ref_id]
            img_id = ref['image_id']

            if img_id not in img_ids:
                img_ids.append(img_id)

                fn_ann = ref['file_name']

                # COCO_train2014_000000419645_398406.jpg
                # COCO_train2014_000000419645.jpg

                suffix = fn_ann.split('.')[-1]

                fname = '_'.join(fn_ann.split('_')[:-1]) + '.' + suffix

                image_fns.append(fname)

                detections = self.refer.imgToAnns[img_id]

                id2dets[img_id] = detections

        self.image_ids = img_ids
        self.image_fns = image_fns
        self.id2dets = id2dets

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

    def __getitem__(self, idx):

        image_id = self.image_ids[idx]
        image_fn = self.image_fns[idx]
        image_path = self.image_dir.joinpath(image_fn)

        assert Path(image_path).exists(), image_path

        img = cv2.imread(str(image_path))

        H, W, C = img.shape

        dets = self.id2dets[image_id]
        # cat_names = [det['category_name'] for det in dets]

        boxes = []
        for i, region in enumerate([det['bbox'] for det in dets]):
            # (x1, y1, x2, y2)
            x, y, w, h = region[:4]
            x1, y1, x2, y2 = x, y, x+w, y+h

            # x1, y1, x2, y2 = region[:4]

            assert x2 <= W, (image_id, i, region)
            assert y2 <= H, (image_id, i, region)

            box = [x1, y1, x2, y2]
            boxes.append(box)

        boxes = np.array(boxes)

        return {
            'img_id': str(image_id),
            'img_fn': image_fn,
            'img': img,
            'boxes': boxes,
            # 'captions': cat_names
        }

def collate_fn(batch):
    img_ids = []
    imgs = []

    boxes = []

    captions = []

    for i, entry in enumerate(batch):
        img_ids.append(entry['img_id'])
        imgs.append(entry['img'])
        boxes.append(entry['boxes'])
        # captions.append(entry['captions'])

    batch_out = {}
    batch_out['img_ids'] = img_ids
    batch_out['imgs'] = imgs

    batch_out['boxes'] = boxes

    # batch_out['captions'] = captions

    return batch_out

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument('--batchsize', default=1, type=int, help='batch_size')
    parser.add_argument('--refcocoroot', type=str, default='/ssd-playpen/home/jmincho/workspace/datasets/RefCOCO/')
    parser.add_argument('--cocoroot', type=str, default='/ssd-playpen/home/jmincho/workspace/datasets/COCO/')
    parser.add_argument('--split', type=str, default='val', choices=['train', 'val', 'test'])

    args = parser.parse_args()

    refcoco_dir = Path(args.refcocoroot).resolve()
    refcocog_dir = refcoco_dir.joinpath('refcocog')
    coco_dir = Path(args.cocoroot).resolve()
    refcoco_images_dir = coco_dir.joinpath('images/train2014')
    dataset_name = 'RefCOCOg'

    out_dir = refcocog_dir.joinpath('features')
    if not out_dir.exists():
        out_dir.mkdir()

    dataset = RefCOCODataset(refcoco_dir, refcoco_images_dir, coco_dir, args.split)
    print('# Images:', len(dataset))

    dataloader = DataLoader(dataset, batch_size=args.batchsize,
                            shuffle=False, collate_fn=collate_fn, num_workers=4)

    output_fname = out_dir.joinpath(f'{args.split}_boxes_GT.h5')
    print('features will be saved at', output_fname)

    desc = f'{dataset_name}_given_boxes_({DIM})'

    extract(output_fname, dataloader, desc)


import os
import shutil

from populus.config import (
    load_default_config,
    write_config,
)
from populus.config.versions import (
    V1,
)

from populus.utils.six import configparser
from populus.utils.mappings import (
    set_nested_key,
    deep_merge_dicts,
)
from populus.utils.config import (
    get_json_config_file_path,
    check_if_json_config_file_exists,
)

INI_CONFIG_FILENAME = './populus.ini'

def get_ini_config_file_path(project_dir=None):
    if project_dir is None:
        project_dir = os.getcwd()

    ini_config_file_path = os.path.join(project_dir, INI_CONFIG_FILENAME)
    return ini_config_file_path

def check_if_ini_config_file_exists(project_dir=None):
    if project_dir is None:
        project_dir = os.getcwd()

    ini_config_file_path = get_ini_config_file_path(project_dir)
    return os.path.exists(ini_config_file_path)

PROVIDER_LOOKUP = {
    'mainnet': 'populus.chain.MainnetChain',
    'testnet': 'populus.chain.TestnetChain',
    'morden': 'populus.chain.TestnetChain',
    'ropsten': 'populus.chain.TestnetChain',
    'testrpc': 'populus.chain.TestRPCChain',
    'tester': 'populus.chain.TesterChain',
    'temp': 'populus.chain.TemporaryGethChain',
}

def translate_legacy_ini_config_file(ini_config_file_path):
    config = configparser.ConfigParser()
    config.read(ini_config_file_path)

    upgraded_config = {}

    for section in config.sections():
        if section == 'populus':
            for key, value in config.items('populus'):
                if key == 'contracts_dir':
                    set_nested_key(upgraded_config, 'compilation.contracts_dir', value)
                else:
                    set_nested_key(upgraded_config, 'populus.{0}'.format(key), value)
            continue
        elif section.startswith('chain:'):
            _, _, chain_name = section.partition(':')
            key_prefix = "chains.{0}".format(chain_name)

            if not config.options(section):
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'chain.class')),
                    PROVIDER_LOOKUP.get(chain_name, 'populus.chain.LocalGethChain'),
                )
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'web3.provider.class')),
                    'web3.providers.ipc.IPCProvider',
                )
                continue

            if config.has_option(section, 'is_external') and config.get(section, 'is_external'):
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'chain.class')),
                    'populus.chain.ExternalChain',
                )
            else:
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'chain.class')),
                    PROVIDER_LOOKUP.get(chain_name, 'populus.chain.LocalGethChain'),
                )

            if config.has_option(section, 'provider'):
                provider_class_path = config.get(section, 'provider')
                if provider_class_path == 'web3.providers.rpc.RPCProvider':
                    if config.has_option(section, 'rpc_host'):
                        rpc_host = config.get(section, 'rpc_host')
                    else:
                        rpc_host = 'localhost'
                    if config.has_option(section, 'rpc_port'):
                        rpc_port = config.get(section, 'rpc_port')
                    else:
                        rpc_port = '8545'
                    set_nested_key(
                        upgraded_config,
                        '.'.join((key_prefix, 'web3.provider.class')),
                        'web3.providers.rpc.HTTPProvider',
                    )
                    set_nested_key(
                        upgraded_config,
                        '.'.join((key_prefix, 'web3.provider.settings.endpoint_uri')),
                        'http://{0}:{1}'.format(rpc_host, rpc_port),
                    )
                else:
                    set_nested_key(
                        upgraded_config,
                        '.'.join((key_prefix, 'web3.provider.class')),
                        config.get(section, 'provider'),
                    )
            else:
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'web3.provider.class')),
                    'web3.providers.ipc.IPCProvider',
                )

            if config.has_option(section, 'ipc_path'):
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'web3.provider.settings.ipc_path')),
                    config.get(section, 'ipc_path'),
                )

            if config.has_option(section, 'default_account'):
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'web3.eth.default_account')),
                    config.get(section, 'default_account'),
                )
            elif config.has_option(section, 'deploy_from'):
                set_nested_key(
                    upgraded_config,
                    '.'.join((key_prefix, 'web3.eth.default_account')),
                    config.get(section, 'deploy_from'),
                )

    return upgraded_config

def upgrade_legacy_config_file(project_dir):
    has_ini_config = check_if_ini_config_file_exists(project_dir)
    has_json_config = check_if_json_config_file_exists(project_dir)

    if not has_ini_config:
        raise ValueError("No `populus.ini` file found")
    elif has_json_config:
        raise ValueError(
            "Cannot upgrade config file if there is already an existing "
            "`populus.json` file."
        )

    ini_config_file_path = get_ini_config_file_path(project_dir)
    json_config_file_path = get_json_config_file_path(project_dir)

    upgraded_config = translate_legacy_ini_config_file(ini_config_file_path)

    default_config = load_default_config(version=V1)
    config = deep_merge_dicts(default_config, upgraded_config)

    write_config(
        project_dir,
        config,
        json_config_file_path,
    )
    backup_ini_config_file_path = "{0}.bak".format(ini_config_file_path)
    shutil.move(ini_config_file_path, backup_ini_config_file_path)
    return backup_ini_config_file_path


str1 = 'mohamad sahebi sahebi is mohamad is is that sahebi that hamid hamed hasan hasan ali ali ali a a a a b b b b w w w w w'
ls1 = str1.split(" ")
dic = dict()
for st1 in  ls1:
    if st1 in dic:
        dic[st1] += 1
    else:
        dic[st1] =1
print(dic)


from mw import Timestamp
from nose.tools import eq_

from ..types import RevisionMetadata, UserInfo

def test_revision_metadata():
    doc = {
        "revid": 3456789,
        "parentid": 54678,
        "comment": "Wat?",
        "user": "EpochFail",
        "userid": 34567890,
        "timestamp": "2015-01-07T12:23:57Z",
        "page": {
            "pageid": 347,
            "title": "Hats",
            "ns": 0
        }
    }
    metadata = RevisionMetadata.from_doc(doc)

    eq_(metadata.rev_id, 3456789)
    eq_(metadata.parent_id, 54678)
    eq_(metadata.user_id, 34567890)
    eq_(metadata.user_text, "EpochFail")
    eq_(metadata.timestamp, Timestamp("2015-01-07T12:23:57Z"))
    eq_(metadata.comment,  "Wat?")
    eq_(metadata.page_id, 347)
    eq_(metadata.page_namespace, 0)
    eq_(metadata.page_title, "Hats")

    doc = {}
    metadata = RevisionMetadata.from_doc(doc)

    eq_(metadata.timestamp, None)

def test_user_info():
    doc = {
        "userid": 24278012,
        "name": "Hoardablehotsauce",
        "editcount": 5,
        "registration": "2015-02-28T22:25:37Z",
        "groups": [
            "*",
            "user"
        ],
        "implicitgroups": [
            "*",
            "user"
        ],
        "blockid": "5752570",
        "blockedby": "Cryptic",
        "blockedbyid": "295294",
        "blockedtimestamp": "2015-02-28T22:43:23Z",
        "blockreason": "{{uw-softerblock}} <!-- Promotional username, "
                       "soft block -->",
        "blockexpiry": "infinity",
        "gender": "unknown"
    }
    user_info = UserInfo.from_doc(doc)

    eq_(user_info.id, doc['userid'])
    eq_(user_info.name, doc['name'])
    eq_(user_info.editcount, doc['editcount'])
    eq_(user_info.registration, Timestamp(doc['registration']))
    eq_(user_info.groups, doc['groups'])
    eq_(user_info.implicitgroups, doc['implicitgroups'])
    eq_(user_info.gender, doc['gender'])
    eq_(user_info.blocked_by, doc['blockedby'])
    eq_(user_info.blocked_by_id, int(doc['blockedbyid']))
    eq_(user_info.blocked_timestamp, Timestamp(doc['blockedtimestamp']))
    eq_(user_info.block_reason, doc['blockreason'])
    eq_(user_info.block_expiry, doc['blockexpiry'])

    doc = {
        "userid": 24278012,
        "name": "Hoardablehotsauce",
        "editcount": 5,
        "groups": [
            "*",
            "user"
        ],
        "implicitgroups": [
            "*",
            "user"
        ]
    }
    user_info = UserInfo.from_doc(doc)
    eq_(user_info.registration, None)
    eq_(user_info.blocked_timestamp, None)


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

import time
import RPi.GPIO as GPIO

LED_PIN_SET = [12, 16, 18, 22, 36, 38]

PWM_PIN = 40
PWM_FREQ = 300

data = ""

def setLedpin():
    GPIO.setmode(GPIO.BOARD)
    for i in LED_PIN_SET:
        GPIO.setup(i, GPIO.OUT)
    print("Led pin set finish",LED_PIN_SET)

def ledDectoBin(decNum):
    binNum = '{:08b}'.format(decNum)
    for i in LED_PIN_SET:
        GPIO.output(i, decNum & 0x1)
        decNum = decNum >> 1

    print( "Led Binary :", binNum)
    time.sleep(0.1)

def ledBinleft(decNum):
    binNum = 0
    global LED_MOVE_UP
    for i in range(decNum):
        binNum = binNum * 2 + 1
    return binNum

setLedpin()

GPIO.setup(PWM_PIN, GPIO.OUT)
pwm = GPIO.PWM(PWM_PIN, PWM_FREQ)
pwm.start(0)
print("PWM pin set :", PWM_PIN, "frequency :", PWM_FREQ, type(pwm))

try:
    while True:

        for LED_CONT in range(0, 70, 10):
            pwm.ChangeDutyCycle(LED_CONT)
            time.sleep(0.01)
            ledDectoBin(ledBinleft(int(LED_CONT / 10)))

        for LED_CONT in range(60, 0, -10):
            pwm.ChangeDutyCycle(LED_CONT)
            time.sleep(0.01)
            ledDectoBin(ledBinleft(int(LED_CONT / 10)))

except KeyboardInterrupt:
    print("c")

finally:
    pwm.stop()
    GPIO.cleanup()


import textwrap
import itertools

def get_test_input() -> str:
    return textwrap.dedent("""\
    6,10
    0,14
    9,10
    0,3
    10,4
    4,11
    6,0
    6,12
    4,1
    0,13
    10,12
    3,4
    3,0
    8,4
    1,10
    2,14
    8,10
    9,0

    fold along y=7
    fold along x=5""")

def read_input(day_number, test=False):
    if test:
        return parse_input(get_test_input())
    else:
        filename = 'input/day{}.txt'.format(day_number)
        with open(filename, 'r') as file:
            return parse_input(file.read())

def parse_input(s: str):
    pts = set()
    folds = []
    for line in s.splitlines(keepends=False):
        if line.startswith('fold'):
            axis, val = line.split()[2].split('=')
            folds.append((axis, int(val)))
        elif line:
            pts.add(tuple(int(x) for x in line.split(',')))
    return pts, folds

def fold(pts, fold_axis, fold_place):
    new_pts = set()
    if fold_axis == 'y':
        for pt in pts:
            if pt[1] > fold_place:
                new_pts.add((pt[0], 2*fold_place - pt[1]))
            else:
                new_pts.add(pt)
    elif fold_axis == 'x':
        for pt in pts:
            if pt[0] > fold_place:
                new_pts.add((2*fold_place - pt[0], pt[1]))
            else:
                new_pts.add(pt)
    return new_pts

def print_pts(pts, w, h):
    print('\n'.join(
        ''.join('#' if (x, y) in pts else '.' for x in range(w)) for y in range(h)
    ))

def part_1(pts, folds):
    first_fold = folds[0]
    new_pts = fold(pts, first_fold[0], first_fold[1])
    print(f'Part 1: {len(new_pts)}')

def part_2(pts, folds):
    for f in folds:
        pts = fold(pts, f[0], f[1])
    w = max(p[0] for p in pts)
    h = max(p[1] for p in pts)
    print_pts(pts, w+1, h+1)

def main():
    pts, folds = read_input(day_number=13, test=False)
    part_1(pts, folds)
    part_2(pts, folds)

if __name__ == "__main__":
    main()


"""
VAE-GAN
  This implementation is based on the original implementation (https://github.com/andersbll/autoencoding_beyond_pixels).
  For details, please refer to the original paper (https://arxiv.org/pdf/1512.09300.pdf).
"""

import torch
from torch import nn

def initialize_weights(model):
  for m in model.modules():
    if isinstance(m, nn.Conv2d):
      m.weight.data.normal_(0, 0.02)
      if m.bias is not None:
        m.bias.data.zero_()
    elif isinstance(m, nn.ConvTranspose2d):
      m.weight.data.normal_(0, 0.02)
      if m.bias is not None:
        m.bias.data.zero_()
    elif isinstance(m, nn.Linear):
      m.weight.data.normal_(0, 0.02)
      if m.bias is not None:
        m.bias.data.zero_()

"""
Hyperparameters

  nc (int): channel size of image
  nz (int): size of latent vector (z)
"""
nc = 3
nz = 1000

class Encoder(nn.Module):
  """
  Encoder class

  Model:
    conv1: (nc, 64, 64) -> (64, 32, 32)
    conv2: (64, 32, 32) -> (128, 16, 16)
    conv3: (128, 16, 16) -> (256, 8, 8)
    fc4: (256*8*8) -> (2048)
    mu: (2048) -> (nz)
    logvar: (2048) -> (nz)
  """
  def __init__(self):
    super(Encoder, self).__init__()

    self.conv1 = nn.Sequential(
      nn.Conv2d(nc, 64, kernel_size=5, stride=2, padding=2, bias=False),
      nn.BatchNorm2d(64),
      nn.ReLU(inplace=True)
    )

    self.conv2 = nn.Sequential(
      nn.Conv2d(64, 128, kernel_size=5, stride=2, padding=2, bias=False),
      nn.BatchNorm2d(128),
      nn.ReLU(inplace=True)
    )

    self.conv3 = nn.Sequential(
      nn.Conv2d(128, 256, kernel_size=5, stride=2, padding=2, bias=False),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True)
    )

    self.fc4 = nn.Sequential(
      nn.Linear(256 * 8 * 8, 2048, bias=False),
      nn.BatchNorm1d(2048),
      nn.ReLU(inplace=True)
    )

    self.mu = nn.Linear(2048, nz, bias=False)

    self.logvar = nn.Linear(2048, nz, bias=False)

    initialize_weights(self)

  def forward(self, x):
    batch_size = x.size(0)

    x = self.conv1(x)
    x = self.conv2(x)
    x = self.conv3(x)
    x = x.view(batch_size, -1)
    x = self.fc4(x)
    mu = self.mu(x)
    logvar = self.logvar(x)
    return mu, logvar

class Decoder(nn.Module):
  """
  Decoder class

  Model:
    fc1: (nz) -> (256*8*8)
    deconv2: (256, 8, 8) -> (256, 16, 16)
    deconv3: (256, 16, 16) -> (128, 32, 32)
    deconv4: (128, 32, 32) -> (32, 64, 64)
    conv5: (32, 64, 64) -> (nc, 64, 64)
  """
  def __init__(self):
    super(Decoder, self).__init__()

    self.fc1 = nn.Sequential(
      nn.Linear(nz, 256 * 8 * 8, bias=False),
      nn.BatchNorm1d(256 * 8 * 8),
      nn.ReLU(inplace=True)
    )

    self.deconv2 = nn.Sequential(
      nn.ConvTranspose2d(256, 256, kernel_size=5, stride=2, padding=2, output_padding=1, bias=False),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True)
    )

    self.deconv3 = nn.Sequential(
      nn.ConvTranspose2d(256, 128, kernel_size=5, stride=2, padding=2, output_padding=1, bias=False),
      nn.BatchNorm2d(128),
      nn.ReLU(inplace=True)
    )

    self.deconv4 = nn.Sequential(
      nn.ConvTranspose2d(128, 32, kernel_size=5, stride=2, padding=2, output_padding=1, bias=False),
      nn.BatchNorm2d(32),
      nn.ReLU(inplace=True)
    )

    self.conv5 = nn.Sequential(
      nn.Conv2d(32, nc, kernel_size=5, stride=1, padding=2, bias=False),
      nn.Tanh()
    )

    initialize_weights(self)

  def forward(self, x):
    x = self.fc1(x)
    x = x.view(-1, 256, 8, 8)
    x = self.deconv2(x)
    x = self.deconv3(x)
    x = self.deconv4(x)
    x = self.conv5(x)
    return x

class Generator(nn.Module):
  """
  Generator class
  """
  def __init__(self):
    super(Generator, self).__init__()

    self.encoder = Encoder(nz)
    self.decoder = Decoder(nz)

  def reparameterize(self, mu, logvar):
    if self.training:
      std = logvar.mul(0.5).exp_()
      eps = torch.randn_like(std)
      return eps.mul(std).add_(mu)
    else:
      return mu

  def forward(self, x):
    mu, logvar = self.encoder(x)
    z = self.reparameterize(mu, logvar)
    xhat = self.decoder(z)
    return xhat, mu, logvar

  def generate(self, z):
    self.eval()
    samples = self.decoder(z)
    return samples

  def reconstruct(self, x):
    self.eval()
    mu, _ = self.encoder(x)
    xhat = self.decoder(mu)

    return xhat

class Discriminator(nn.Module):
  """
  Discriminator class

  Model:
    conv1: (nc, 64, 64) -> (32, 64, 64)
    conv2: (32, 64, 64) -> (128, 32, 32)
    conv3: (128, 32, 32) -> (256, 16, 16)
    conv4: (256, 16, 16) -> (256, 8, 8)
    fc5: (256*8*8) -> (512)
    fc6: (512) -> (1)
  """
  def __init__(self):
    super(Discriminator, self).__init__()

    self.conv1 = nn.Sequential(
      nn.Conv2d(nc, 32, kernel_size=5, stride=1, padding=2),
      nn.ReLU(inplace=True)
    )

    self.conv2 = nn.Sequential(
      nn.Conv2d(32, 128, kernel_size=5, stride=2, padding=2, bias=False),
      nn.BatchNorm2d(128),
      nn.ReLU(inplace=True)
    )

    self.conv3 = nn.Sequential(
      nn.Conv2d(128, 256, kernel_size=5, stride=2, padding=2, bias=False),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True)
    )

    self.conv4 = nn.Sequential(
      nn.Conv2d(256, 256, kernel_size=5, stride=2, padding=2, bias=False),
      nn.BatchNorm2d(256),
      nn.ReLU(inplace=True)
    )

    self.fc5 = nn.Sequential(
      nn.Linear(256 * 8 * 8, 512, bias=False),
      nn.BatchNorm1d(512),
      nn.ReLU(inplace=True)
    )

    self.fc6 = nn.Sequential(
      nn.Linear(512, 1),
    )

    initialize_weights(self)

  def forward(self, x):
    x = self.conv1(x)
    x = self.conv2(x)
    x = self.conv3(x)
    x = self.conv4(x)
    x = x.view(-1, 256 * 8 * 8)
    x = self.fc5(x)
    x = self.fc6(x)
    return x


import os
import sys
import imp
import numpy as np
import unittest
import tensorflow as tf
from tensorflow import keras
__path__ = [os.path.dirname(os.path.abspath(__file__))]
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from unittest import TestCase
from adapted_lm_algorithm import ParseArgs
from training_params_functions import ModelTrainingParameters

class ModelTrainingParametersTests(TestCase):

    def setUp(self):
        self.trainingModel = ModelTrainingParameters(3, [3, 2], 2)
        self.parseArgs = ParseArgs(3, [3, 2], 2)

    # Test the build network method
    def test_build_network(self):
        self.my_parser = [4, 3]
        self.trainingModel.hidden_layers = [4, 3]
        # self.args_new.my_parser.mlp_hid_structure
        self.trainingModel.input_features = 4
        self.trainingModel.num_classes = 2
        self.neurons_cnt, self.lst_shape, self.lst_sizes, self.hid_layer = self.trainingModel.build_network()
        # check return types as int (number of neurons), list(layers, shapes and sizes)
        self.assertEqual(type(self.neurons_cnt), int)
        self.assertEqual(type(self.lst_shape), list)
        self.assertEqual(type(self.lst_sizes), list)
        self.assertEqual(len(self.lst_shape), len(self.lst_sizes))

    # test build network using tensorflow keras
    def test_tf_keras_mlp_model(self):
        """testing the tensorflow keras build network function
        """
        self.trainingModel.input_features = keras.Input(shape=(3, ), name="Inputs")
        self.trainingModel.hidden_layers = [3, 4]
        self.trainingModel.classify_shapes = 3
        self.model_types = self.trainingModel.tf_keras_mlp_model(self.trainingModel.input_features)
        print(self.model_types)
        self.assertNotEqual(type(self.model_types), tf.Tensor)

    def test_loss_function_from_scratch(self):
        """Function to test entropy loss for the MLP model prior to training
        """
        # Initialize parameters
        set_pts, input_n, class_val = 100, 3, 2
        x1 = np.random.randn(set_pts, input_n)
        x2 = np.random.randn(set_pts, 2)

        # Return number of parameters and network structure
        n_neurons, shape_lst, sizes_lst, l_hidden = self.trainingModel.build_network()
        dict_params = {'choose_val': 1, 'wb_shapes': shape_lst, 'l_hidden': l_hidden, 'wb_sizes': sizes_lst, 'nclasses': class_val, 'n': input_n}

        # Initialize Glorot Normal
        self.initializer = tf.initializers.GlorotNormal(43)
        params_0 = tf.Variable(self.initializer([n_neurons], dtype=tf.float64))

        # Loss function
        loss_from_scratch = self.trainingModel.loss_function_from_scratch(dict_params, params_0, x1, x2)
        print(loss_from_scratch)

    def test_loss_function_keras(self):
        """Construct the loss function and arrange

        Args:
            model (Keras class): The keras model object class.

        """
        # build a keras loss using return values from build_network method.
        # Generate a toy data set
        self.set_points, self.row_try, self.n_row = 100, 10, 3
        x1 = np.random.randn(self.set_points, self.n_row)
        x2 = np.random.randn(self.set_points, 2)
        x3_predict = np.random.rand(self.row_try, self.n_row)
        self.input_features_dim = keras.Input(shape=(self.n_row, ), name="Inputs_test")
        self.keras_model = self.trainingModel.mlp_function_keras(self.trainingModel.input_features_model)
        self.history, self.prediction = self.trainingModel.loss_function_keras(self.keras_model, x1, x2, x3_predict)
        # self.assertEqual(self.model, 4)

# Define a class to pytest parse_arg values

class ArgParseTestCase(TestCase):

    def setUp(self):
        """A setup for argparse!"""
        self.args_class = ParseArgs(2, [4, 3], 3)

    def test_version(self):
        sys.argv[1:]=['-v']
        with self.assertRaises(SystemExit):
            self.args_class.parse_command_line_argument_function()
            # testing version message requires redirecting stdout
    # similarly for a misc_opts test

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


from math import pi
import tkinter as tk
from mech_calculator import *
"""
This program calculates the diameter and desired angles for a GT timing pulley.
To ease the design process in CAD software.
"""

def menuInterface():

    print("Welcome to a part calculator for your SolidWorks schematics.")
    print("Remember to enter the dimension in mm.")
    print("Select an option:")
    print("")
    print("1.- GT2Pulley")
    print("2.- Gears")
    print("")
    print("Write your option like in the menu.")
    return input()

if __name__ == "__main__":

    """
    if(menuInterface().lower() == "gt2pulley"):
        print(GT2Pulley())
    else:
        print("Nothing to do.")
    """

    # Create the window for the GUI
    app = tk.Tk()
    # Name the window app
    app.title("Mechanical Parts Calculator")
    # Set default size
    app.geometry('800x600')
    # Set the sacred rites
    label = tk.Label(app, text="Praise the machine").pack(side="bottom")
    # Frame inside the main window
    frame = tk.Frame(app)
    frame.pack(side="bottom")

    # Add scrollbar

    # Calculate button
    button = tk.Button(frame, text="Calculate", command= app.destroy)
    button.pack()

    # Run app
    app.mainloop()


# Project Euler #7: 10001st prime

# By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
# What is the Nth prime number?

import sys

# begin with a short list of primes
# as more primes are needed, add to the list to avoid recomputing primes
def add_primes(n, primes):
    temp = primes[-1] + 2
    while len(primes) < n:
        check = 0
        for i in primes:
            if temp % i == 0:
                check = 1
                break
        if check == 0:
            primes.append(temp)
        temp += 2

t = int(input().strip())
T = []
for a0 in range(t):
    T.append(int(input().strip()))

primes = [2,3]
for i in T:
    if i > len(primes):
        add_primes(i, primes)
    print(primes[i-1])


from asterisk.ami import AMIClient, AMIClientAdapter
import sensitive
import redis
import re, random, logging

logger = logging.getLogger(__name__)

class NumberFormatException(Exception):
  pass

class Backend:
  def __init__(self):
    self.client = None
    self.adapter = None
    self.redis = None

  def __enter__(self):
    client = AMIClient(address='asterisk', port=5038)
    client.login(username='asterisk',secret='asterisk')
    self.client = client
    self.adapter = AMIClientAdapter(self.client)
    self.client.add_event_listener(self._event_callback)
    self.redis = redis.StrictRedis(host='redis')
    return self

  def __exit__(self, *args):
    try:
      self.client.logoff()
      self.client.disconnect()
    finally:
      self.adapter = None
      self.client = None
      self.redis = None

  def _event_callback(self, event, source):
    try:
      if 'Channel' not in event or not re.match('^SIP/', event['Channel']):
        return

      if event.name == 'Hangup':
        self._hangup(event['CallerIDNum'])
      elif event.name == 'NewConnectedLine':
        self._connect(event['CallerIDNum'])

    except Exception:
      logger.exception('in AMI event handler')

  def _hangup(self, num):
    print('HANGUP: {}'.format(num))
    self.redis.delete('sotellme_connected_to:{}'.format(num))

  def _connect(self, num):
    print('CONNECT: {}'.format(num))
    self.redis.set('sotellme_connected_to:{}'.format(num), 'yes')

  def _do_call(self, conference_id, *numbers):
    futures = []
    for number in numbers:
      futures.append(self.adapter.Originate(
          Channel='LOCAL/{}@voipms-outbound'.format(number),
          Exten='10',
          Variable='SOTELLME_CONF_ID={}'.format(conference_id),
          Priority=1,
          Context='sotellme',
          CallerID='"SoTellMe" <{}>'.format(sensitive.dial_in_number),
          Async='yes',
        ))
    for future in futures:
      print('Originate result: {}'.format(future.response))

  def _gen_conference_id(self):
    return ''.join(random.choice('0123456789abcdef') for n in range(10))

  def verify_and_dial(self, numbers):
    if not all([ re.match('^(1[0-9]{10}|4443)$', number) for number in numbers ]):
      raise NumberFormatException()

    conference_id = self._gen_conference_id()
    self._do_call(conference_id, *numbers)
    return conference_id

  def kill_conference(self, conference_id):
    if not re.match('^[0-9a-f]*$', conference_id):
      raise NumberFormatException()
    return str(self.adapter.ConfbridgeKick(
        Conference=conference_id,
        Channel='all'
      ).response)


"""
Written by Albert"Anferensis"Ong

Controls the respawn of a character.
This script is activated when a character contacts the blast zones.
"""

from bge import logic

def main():

    cont = logic.getCurrentController()
    owner = cont.owner

    player_num = str(owner["player_num"])
    stock_num = str(owner["Stocks"])

    delay = owner.sensors["Respawn Delay"]
    collision = owner.sensors["Blast Zone"]

    if collision.positive:
        owner["Dead"] = True

    if owner["Dead"]:

        if owner["Stocks"] == 1:

            ko_sound = owner.actuators["KO"]
            ko_sound.startSound()

            overlay_objects = logic.getSceneList()[1].objects
            stock_name = "P" + player_num + " Stock" + stock_num
            overlay_objects[stock_name].visible = False

            scene = logic.getCurrentScene()
            stage_props = scene.objects["Stage Properties"]
            stage_props["Game Over"] = True

        elif delay.positive:

            owner["Stocks"] -= 1

            # Spawn locations are stored in the stage properties.
            objects = logic.getCurrentScene().objects
            stage_props = objects["Stage Properties"]

            # Spawn location differs based on the player number.
            spawn_x = "Spawn_" + player_num + "X"
            spawn_z = "Spawn_" + player_num + "Z"

            owner.position.x = stage_props[spawn_x]
            owner.position.z = stage_props[spawn_z]

            owner["Dead"] = False

        else:

            ko_sound = owner.actuators["KO"]
            ko_sound.startSound()

            # Makes the stock icon in the overlay invisible.
            overlay_objects = logic.getSceneList()[1].objects
            stock_name = "P" + player_num + " Stock" + stock_num
            overlay_objects[stock_name].visible = False

#==================================================

main()


import os
import shutil
import tempfile
import zipfile
from datetime import datetime

import numpy as np
import pandas as pd

unit_dic = {'61a3fa': 'Vandiver2', '61a52b': 'Blessing2', '61a579': 'Frink', '62fef0': 'Christensen',
            '62fef1': 'Miller', '62fef8': 'Rembold',
            '62fefd': 'Liang2', '62ff20': 'Gettum', '62ff25': 'Liang1', '63005c': 'McGurer', '630091': 'Russel',
            '630092': 'Mensa',
            '630093': 'Roller', '630094': 'Sotiri', '6300c7': 'Wenzlick', '630116': 'Lewis', '63011e': 'King',
            '6300c5': 'Hemphill', '6300c4': 'Finney',
            '63011d': 'Jones', '630198': 'Burns', '6309ec': 'Lee', '6300c8': 'Malespin', '63009b': 'Vandiver2',
            '62ff02': 'Blessing1', '6300cc': 'Vandiver1'}

def unzip_folder(path):
    print('Unzip the folder')
    file_list = os.listdir(path)
    # create a temporary dir for extracting and renaming all the zipped file
    temp_dir = tempfile.TemporaryDirectory(dir=path)
    temp_dir_name = temp_dir.name

    for file in file_list:
        name = os.path.splitext(file)[0]
        suffix = os.path.splitext(file)[1]
        # then zip
        if suffix == ".zip":
            with zipfile.ZipFile(os.path.join(path, file), 'r') as zip_ref:
                zip_ref.extractall(temp_dir_name)
                # rename
                os.rename(os.path.join(temp_dir_name, os.listdir(temp_dir_name)[0]), os.path.join(temp_dir_name, name))
                shutil.move(os.path.join(temp_dir_name, name), path)
    temp_dir.cleanup()

def select_files(path):
    print('Sort files')
    folders = []
    file_list = os.listdir(path)
    for key in unit_dic.keys():
        new_folder = PATH + '\\unit\\' + key
        if not os.path.exists(new_folder):
            os.mkdir(new_folder)
        for folder in file_list:
            folder_name = path + '\\' + folder
            if os.path.isdir(folder_name):
                for item in os.listdir(folder_name):
                    if key in item:
                        new_name = folder + item
                        os.rename(os.path.join(folder_name, item), os.path.join(folder_name, new_name))
                        shutil.move(os.path.join(folder_name, new_name), new_folder)
                        folders.append(new_folder)
    print(folders)
    return folders

def sensor_list(folder_path, files_list):
    sensor_list = []
    all_files = os.listdir(folder_path)
    for item in all_files:
        item_path = os.path.join(folder_path, item)
        for file_name in files_list:
            for root, dirs, files in os.walk(item_path):
                for file in files:
                    if file_name in file:
                        item_PATH = os.path.join(PATH, file_name, item_path.split('\\')[-1])
                        if not os.path.exists(item_PATH):
                            os.mkdir(item_PATH)
                        os.rename(os.path.join(root, file), os.path.join(root, root.split('\\')[-2] + file))
                        try:
                            shutil.copy(os.path.join(root, root.split('\\')[-2] + file), item_PATH)
                        except Exception as e:
                            print(e)

        sensor_list.append(os.path.join(PATH, file_name))
    return sensor_list

def data_preprocess(PATH, sensor_data, unit, variable, starting_date_time, ending_date_time, interval, week_day):
    item_info = pd.DataFrame()
    All_files = os.listdir(sensor_data)
    for file_path in All_files:
        # item_pd = item+'_pd'
        item_pd = pd.read_csv(os.path.join(sensor_data, file_path))
        item_pd["date (UTC)"] = item_pd["date (UTC)"].map(lambda x: x[:-14]) + ' ' + item_pd["date (UTC)"].map(
            lambda x: x[-13:-8])
        # convert your timestamps to datetime and then use matplotlib
        item_pd["date-format"] = item_pd["date (UTC)"].map(lambda x: datetime.strptime(x, "%Y-%m-%d %H:%M"))
        item_pd = item_pd[["date-format", "value"]]
        item_pd = item_pd.groupby(by='date-format', as_index=False).mean()
        try:
            item_pd['value'] = item_pd['value'].apply(lambda x: np.nan if np.isnan(x) else int(x))
        except Exception as e:
            print('Exception:', e)
        item_pd["date-format"] = item_pd["date-format"] - pd.Timedelta(hours=5)  # CDT = GMT-5, EDT=GMT-4
        item_pd = item_pd.dropna()
        item_pd = item_pd[(item_pd['date-format'] >= starting_date_time) & (item_pd['date-format'] <= ending_date_time)]
        item_pd.rename(columns={'value': variable}, inplace=True)
        # item_pd['date'] = item_pd['date-format'].map(lambda x: str(x).split(' ')[0])
        item_info = item_info.append(item_pd)
    item_info = item_info.sort_values('date-format')
    item_info.drop_duplicates(subset=['date-format'], keep='first', inplace=True)
    item_info = item_info.set_index('date-format').asfreq('1min', method='bfill')
    # item_info = item_info.fillna(method='bfill')
    item_info['Week_Day'] = item_info.index.map(lambda x: (pd.Timestamp(x.year, x.month, x.day, 0, 0, 0).weekday()) + 1)
    item_info["date"] = (item_info.index).map(lambda x: str(x).split(' ')[0])
    item_info["Time"] = (item_info.index).map(lambda x: str(x).split(' ')[1])
    item_info.reset_index(drop=True, inplace=True)
    # select specific Week Day/Week Days
    Week_item_info = item_info.loc[item_info['Week_Day'].isin([week_day])]
    if Week_item_info.empty:
        item_info = item_info.groupby(item_info[:(len(item_info) // interval) * interval].index // interval).agg(
            {'Time': 'first', 'date': 'first', variable: 'mean'})
        two_level_index_series = item_info.set_index(['date', 'Time'])[variable]
        new_intem_info = two_level_index_series.unstack()
        print('Done_Part1')
    else:
        Week_item_info = Week_item_info.groupby(
            Week_item_info[:(len(Week_item_info) // interval) * interval].index // interval).agg(
            {'Time': 'first', 'date': 'first', variable: 'mean'})
        two_level_index_series = Week_item_info.set_index(['date', 'Time'])[variable]
        new_intem_info = two_level_index_series.unstack()
        print('Week_Done_Part1')

    analysis_data = os.path.join(PATH, 'Analysis_' + variable)
    if not os.path.exists(analysis_data):
        os.mkdir(analysis_data)
    data_csv = unit + '_Per' + str(interval) + '_Week' + week_day + '_' + str(starting_date_time).split(' ')[0] + '_' + \
               str(ending_date_time).split(' ')[0]
    new_intem_info.to_csv(os.path.join(analysis_data, data_csv + '.csv'))
    print('Done_Path', analysis_data)

def upload_folder(path):
    for root, dirs, files in os.walk(path):
        temp_file = os.path.join(PATH, 'temp_file_RH')
        if not os.path.exists(temp_file):
            os.mkdir(temp_file)
        for file in files:
            shutil.copy(os.path.join(root, file), temp_file)
    print('Upload done')

if __name__ == '__main__':
    PATH = os.path.abspath(os.path.dirname(os.getcwd()))
    # unit = '61a52b'
    unit = '630094'
    interval = 15
    week_day = 'all'  # Monday=1, ..., Sunday =7, One week = 'all'
    starting_date_time = datetime.strptime('29.12.2020 00:00:00', "%d.%m.%Y %H:%M:%S")
    ending_date_time = datetime.strptime('05.02.2021 23:59:00', "%d.%m.%Y %H:%M:%S")
    files_list = ['WuhanIAQ_CO2', 'WuhanIAQ_PM1', 'WuhanIAQ_RH', 'WuhanIAQ_PM25', 'WuhanIAQ_VOC']
    # unzip folders
    # df_final = unzip_folder(PATH + '\\Raw_data')
    # select all files and save in per unit folder
    # folder_path = select_files(PATH + '\\Raw_data')
    # extract Wuhan Sensors files
    # sensor_list = sensor_list(os.path.join(PATH,'unit'),files_list)
    # data files merge into one CSV
    # data_preprocess(PATH,os.path.join(PATH,files_list[1],unit), unit,files_list[1].split('_')[1],starting_date_time,ending_date_time,interval,week_day)

    upload_folder(os.path.join(PATH, 'Raw_data', '630094_Rawdata_CO2'))
    temp_info = 'temp_file_CO2'
    temp_starting_date_time = datetime.strptime('28.2.2021 00:00:00', "%d.%m.%Y %H:%M:%S")
    temp_ending_date_time = datetime.strptime('22.08.2021 23:59:00', "%d.%m.%Y %H:%M:%S")
    data_preprocess(PATH, os.path.join(PATH, temp_info), unit, files_list[0].split('_')[1], temp_starting_date_time,
                    temp_ending_date_time, interval, week_day)


import os
import getch
import f_ruch
def zapis(plansza):
    print('Podaj nazwe pliku: ')
    nazwa=str('plansze/')+str(input())+str('.txt')
    plik = open(nazwa, 'w+')
    for i in plansza:
        plik.writelines(i)
        plik.write('\n')
    plik.close()
def zapytaj_plansza(plansza):
    print("Czy chcesz zapisać planszę ? (t/n)")
    odpowiedz = getch.getch()
    if(odpowiedz=='t'):
        os.system("cls")
        zapis(plansza)
        os.system("cls")
    elif(odpowiedz=='n'):
        os.system("cls")
    else:
        zapytaj_plansza(plansza)

def drukuj_plansze(plansza):
    for i in plansza:
        for j in i:
            print(j + ' ',end='')
        print()

def generujplansze(plansza,n,obiekty):
    for i in range(n):
        plansza.append([])  # generowanie wierszy
        for j in range(n):
            plansza[i].append([])  # generowanie kolumn
    for i in range(n):
        for j in range(n):
            if (i == 0 or i == (n - 1) or j == 0 or j == (n - 1)):
                plansza[j][i] = obiekty["sciana"]         #wypelnianie scian 'H'
            else:
                plansza[j][i] = obiekty["puste_pole"]         #wypelnianie wnetrza

def postawsciane(plansza,gy,gx):
    plansza[gy][gx]='H'

def debug_wyswietl(debug,ruch,gx,gy,swx,swy,strzalwrogkierunek,kierunkistrzalu,ruchy):
    if (debug == True):
        f_ruch.wyswietlruch(ruch,kierunkistrzalu,ruchy)
        print('gX: ' + str(gx) + ' gY: ' + str(gy))
        print('sX: ' + str(swx) + ' sY: ' + str(swy))

def czy_e_wcisniete(bufor_sciana,plansza,gx,gy,obiekty):
    if (bufor_sciana == True):
        plansza[gy][gx]=obiekty["sciana"]
    else:
        plansza[gy][gx]=obiekty["puste_pole"]

def ammo_wyswietl(ammo):
    print('Ammo | ' + str(ammo) + ' | 20 |')


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import Lasso
from sklearn.preprocessing import PolynomialFeatures
import sklearn.metrics as skmt
import seaborn as sns
import matplotlib.pyplot as plt

def read_file(filename):
    return pd.read_csv(filename)

def split_dataset(df):
    x = np.array(df.drop(columns='y')) if df.shape[1] == 2\
        else np.matrix(df.drop(columns='y'))
    y = np.array(df.y).reshape(-1,1) if df.shape[1] == 2\
        else np.matrix(df.y).reshape(-1,1)
    return x, y

def plot_regression(df, x, w, name = ''):
    sns.set()
    data = pd.DataFrame({'x': x[:,1], 'y': np.matmul(x, w).reshape(-1)})
    ax = sns.scatterplot(x='x', y='y', data=df, color='teal')
    ax = sns.lineplot(x='x', y='y', data=data, color='#e74c3c')
    ax.set_title(name)
    plt.show()

def print_r2_score(r2_score):
    print('Best R2-score: ' + str(r2_score))

def print_information(r2_score, alpha):
    print('Alpha: ' + str(alpha) + ', R2-score: ' + str(r2_score))

def print_information(dfname, degree, score, alpha, coeff):
    print('Information about ' + dfname + ', degree = ' + str(degree) + ':')
    print('Best R2-score: ' + str(score))
    print('Best alpha: ' + str(alpha))
    print('Best coeff: ' + str(coeff))

def add_dummy_value(x):
    x = np.concatenate((np.ones((x.shape[0], 1)), x), axis=1)
    return x

def lasso_lin_regression(df, dfname, alpha, degree=1, plot=True):
    x, y = split_dataset(df)
    x = add_dummy_value(x)
    x_train, x_test, y_train, y_test = \
        train_test_split(x, y, test_size=0.2)
    best_score = -1
    for al in alpha:
        model = Lasso(al)
        model.fit(x_train, y_train)
        #train_score = model.score(x_train, y_train)
        test_score = model.score(x_test, y_test)
        coeff_used = np.sum(model.coef_ != 0)
        if test_score > best_score:
            best_score = test_score
            best_alpha = al
            best_coeff = model.coef_
            number_best_coeff = coeff_used
    print_information(dfname, degree, best_score, best_alpha, number_best_coeff)
    if plot:
        plot_regression(df, x, best_coeff, name='Lasso regression for ' + dfname +\
                        ', degree = ' + str(degree))

def lasso_pol_regression(df, dfname, alpha, degree, plot=True):
    x, y = split_dataset(df)
    for degr in degree:
        poly = PolynomialFeatures(degree=degr)
        x_fit = poly.fit_transform(x)
        #x_fit = x_fit[:,1:]
        x_train, x_test, y_train, y_test = \
            train_test_split(x_fit, y, test_size=0.2)
        best_score = -1
        for al in alpha:
            model = Lasso(al, max_iter=10000000, fit_intercept=True)
            model.fit(x_train, y_train)
            #train_score = model.score(x_train, y_train)
            test_score = model.score(x_test, y_test)
            coeff_used = np.sum(model.coef_ != 0)
            if test_score > best_score:
                best_score = test_score
                best_alpha = al
                best_coeff = model.coef_
                number_best_coeff = coeff_used
        print_information(dfname, degr, best_score, best_alpha, number_best_coeff)
        if plot:
            plot_regression(df, x_fit, best_coeff, name='Lasso regression for ' + dfname +\
                        ', degree = ' + str(degr))

def run_lasso_regr_noisysine():
    df = read_file('noisysine.csv')
    alpha = np.arange(start=0.0001, stop=1.1, step=0.01)
    lasso_lin_regression(df, 'noisysine', alpha, plot=False)

def run_lasso_pol_regr_noisysine():
    df = read_file('noisysine.csv')
    degree = np.arange(start=2, stop=6)
    alpha = [0.000000001, 1e-10, 0.000001, 0.001, 0.00001, 0.01, 0.1, 2, 3, 5]
    lasso_pol_regression(df, 'noisysine', alpha, degree, plot=False)

def run_lasso_regr_hydrodynamics():
    df = read_file('hydrodynamics.csv')
    alpha = np.arange(start=0.0001, stop=1.1, step=0.01)
    lasso_lin_regression(df, 'hydrodynamics', alpha, plot=False)

def run_lasso_pol_regr_hydrodynamics():
    df = read_file('hydrodynamics.csv')
    degree = np.arange(start=2, stop=3)
    alpha = [0.000000001, 0.000001, 0.00001, 0, 0.1, 0.5, 1, 3]
    lasso_pol_regression(df, 'hydrodynamics', alpha, degree, plot=False)

#run_lasso_regr_noisysine()
#run_lasso_pol_regr_noisysine()
#run_lasso_regr_hydrodynamics()
#un_lasso_pol_regr_hydrodynamics()


"""
Create and write a TRC file

Note that you can also export a TRC file using Mocka
File > Export > Motion Analysis Corp. > TRC file

"""

__author__ = "Nathan Brantly, Alex Woodall"
__version__ = "2.0"
__license__ = "ABI"

import numpy as np
from numpy import matlib
import os
import tkinter as tk
from tkinter import filedialog

def write_trc(marker_labels: list, header_information: dict, frame_numbers: np.ndarray, marker_data: np.ndarray, full_file_name: str):
	'''
	Creates a TRC file with (name, directory, and file extension (should always be '.trc') specified
	in full_file_name)

	Inputs:
			marker_labels: a list of all the marker labels
			header_information: a dictionary of the header information required for the trc file
			frame_numbers: an array of all the frame numbers
			marker_data: an array of all the marker data to write to the trc file
			full_file_name: a string of the full path and file name for the new trc file

	'''

	# Get header information from the information dictionary
	frame_rate = header_information["CameraRate"]
	n_markers = int(header_information["NumMarkers"])
	num_frames = int(header_information["NumFrames"])
	units = header_information["Units"]

	# Get the number of columns to write for the data section of the trc file
	_, num_col = np.shape(marker_data)

	file_extention = os.path.splitext(full_file_name)[-1] # Get the extension of the filename

	# Check that the new file name is a trc file
	if file_extention != '.trc':
		full_file_name = input("Please enter a full file name with the extension '.trc'") # Request a different filename from the user

	# Open the new file for writing
	fid = open(full_file_name, "w+")

	# Get just the file name from the full file name
	file_name = full_file_name.split('\\')[-1]

	# Write the header information
	fid.write("PathFileType\t4\t(X/Y/Z)\t%s\n" % (file_name))
	fid.write("DataRate\tCameraRate\tNumFrames\tNumMarkers\tUnits\tOrigDataRate\tOrigDataStartFrame\tOrigNumFrames\n")
	fid.write("%f\t%f\t%d\t%d\t%s\t%f\t%d\t%d\n" % (frame_rate, frame_rate, num_frames, n_markers, units, frame_rate, frame_numbers[0], frame_numbers[-1]))
	fid.write("Frame#\tTime\t")

	for i in range(n_markers):
		fid.write("%s\t\t\t" % (marker_labels[i]))

	fid.write("\n\t\t")

	for i in range(n_markers):
		fid.write("X%i\tY%i\tZ%i\t" % (i+1, i+1, i+1))

	fid.write("\n\n")

	# Close the file
	fid.close()

	# Now append the data to the file now that the header has been written out

	# Open the file to append to
	fid = open(full_file_name, "a+")

	# Save the marker array to the file
	for kk in range(num_frames):
		for ll in range(num_col): # If an entry in column 1 (i.e., frame#), write format as integer
			if ll == 0:
				fid.write("%d\t" % (marker_data[kk, ll]))

			elif ll == 1: # If an entry in column 2 (i.e., 'time'), write format as compact format
				fid.write("%g\t" % (marker_data[kk,ll]))

			elif ll == num_col - 1:
				fid.write("%0.5f\n" % (marker_data[kk,ll])) # Carriage return after writing each line

			else: # Otherwise, write format as fixed-point notation with 5 dp
				fid.write("%0.5f\t" % (marker_data[kk,ll]))

	# Close the file
	fid.close()

	# Print new file name for the user
	print('New TRC location: %s\n' % (full_file_name))


import torch
import torch.nn as nn
import torch.nn.functional as F
from ray.rllib.models.modelv2 import ModelV2
from ray.rllib.models.torch.recurrent_net import RecurrentNetwork
from ray.rllib.utils.annotations import override

class SmallConvNet(nn.Module):
    """
    Small PyTorch CNN.
    """
    def __init__(self):
        super(SmallConvNet, self).__init__()
        self.conv1 = nn.Conv2d(4, 32, 3, 2, 1)
        self.conv2 = nn.Conv2d(32, 32, 3, 2, 1)
        self.conv3 = nn.Conv2d(32, 32, 3, 2, 1)
        self.conv4 = nn.Conv2d(32, 32, 3, 2, 1)

    def forward(self, x):
        x = F.leaky_relu(self.conv1(x))
        x = F.leaky_relu(self.conv2(x))
        x = F.leaky_relu(self.conv3(x))
        x = F.leaky_relu(self.conv4(x))
        x = x.reshape((-1, 3 * 3 * 32))
        return x

class SmallConvLSTMModel(RecurrentNetwork, nn.Module):
    """
    RLlib small CNN LSTM actor critic model.
    """
    def __init__(self, obs_space, action_space, num_outputs, model_config, name):
        RecurrentNetwork.__init__(self, obs_space, action_space, num_outputs, model_config, name)
        nn.Module.__init__(self)

        self.convnet = SmallConvNet()
        self.conv_features = None
        self.lstm = nn.LSTM(288, 256, batch_first=True)
        self.lstm.bias_ih_l0.data[256:256 * 2].fill_(1)
        self.lstm_features = None
        self.action_branch = nn.Linear(256, num_outputs)
        self.value_branch = nn.Linear(256, 1)

    @override(RecurrentNetwork)
    def forward(self, input_dict, state, seq_lens):
        x = input_dict['obs'].float().permute(0, 3, 1, 2)
        self.conv_features = self.convnet(x)

        input_dict["obs_flat"] = self.conv_features
        return super().forward(input_dict, state, seq_lens)

    @override(RecurrentNetwork)
    def forward_rnn(self, inputs, state, seq_lens):
        self.lstm_features, [h, c] = self.lstm(inputs,
            [torch.unsqueeze(state[0], 0),
             torch.unsqueeze(state[1], 0)])

        action_out = self.action_branch(self.lstm_features)
        return action_out, [torch.squeeze(h, 0), torch.squeeze(c, 0)]

    @override(ModelV2)
    def value_function(self):
        assert self.lstm_features is not None, "must call forward() first"
        value_out = torch.reshape(self.value_branch(self.lstm_features), [-1])
        return value_out

    @override(ModelV2)
    def get_initial_state(self):
        h = [
            self.action_branch.weight.new(1, 256).zero_().squeeze(0),
            self.action_branch.weight.new(1, 256).zero_().squeeze(0),
        ]
        return h