## Brian Blaylock
## April 23, 2021

"""
==================================
HRRR-B Extension: xarray accessors
==================================

Extend the xarray capabilities with a custom accessor.
http://xarray.pydata.org/en/stable/internals.html#extending-xarray

"""
import warnings

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import xarray as xr
from paint.radar import cm_reflectivity
from paint.radar2 import cm_reflectivity
from paint.standard2 import cm_dpt, cm_pcp, cm_rh, cm_tmp, cm_wind
from toolbox.cartopy_tools_OLD import common_features, pc

@xr.register_dataset_accessor("hrrrb")
class hrrrb_accessor:
    def __init__(self, xarray_obj):
        self._obj = xarray_obj
        self._center = None

    @property
    def center(self):
        """Return the geographic center point of this dataset."""
        if self._center is None:
            # we can use a cache on our accessor objects, because accessors
            # themselves are cached on instances that access them.
            lon = self._obj.latitude
            lat = self._obj.longitude
            self._center = (float(lon.mean()), float(lat.mean()))
        return self._center

    def plot(self, ax=None, common_features_kw={}, **kwargs):
        """Plot data on a map."""
        ds = self._obj

        _level_units = dict(
            adiabaticCondensation="adiabatic condensation",
            atmosphere="atmosphere",
            atmosphereSingleLayer="atmosphere single layer",
            boundaryLayerCloudLayer="boundary layer cloud layer",
            cloudBase="cloud base",
            cloudCeiling="cloud ceiling",
            cloudTop="cloud top",
            depthBelowLand="m",
            equilibrium="equilibrium",
            heightAboveGround="m",
            heightAboveGroundLayer="m",
            highCloudLayer="high cloud layer",
            highestTroposphericFreezing="highest tropospheric freezing",
            isobaricInhPa="hPa",
            isobaricLayer="hPa",
            isothermZero="0 C",
            isothermal="K",
            level="m",
            lowCloudLayer="low cloud layer",
            meanSea="MSLP",
            middleCloudLayer="middle cloud layer",
            nominalTop="nominal top",
            pressureFromGroundLayer="Pa",
            sigma="sigma",
            sigmaLayer="sigmaLayer",
            surface="surface",
        )

        for var in ds.data_vars:
            print("cfgrib variable:", var)
            print("GRIB_cfName", ds[var].GRIB_cfName)
            print("GRIB_cfVarName", ds[var].GRIB_cfVarName)
            print("GRIB_name", ds[var].GRIB_name)
            print("GRIB_units", ds[var].GRIB_units)
            print("GRIB_typeOfLevel", ds[var].GRIB_typeOfLevel)
            print()
            ds[var].attrs["units"] = (
                ds[var]
                .attrs["units"]
                .replace("**-1", "$^{-1}$")
                .replace("**-2", "$^{-2}$")
            )

            dpi = common_features_kw.pop("dpi", 150)
            figsize = common_features_kw.pop("figsize", [10, 5])
            fig, ax = plt.subplots(
                1, 1, subplot_kw=dict(projection=ds.crs), dpi=dpi, figsize=figsize
            )

            default = dict(scale="50m", ax=ax, crs=ds.crs, STATES=True, BORDERS=True)
            common_features_kw = {**default, **common_features_kw}
            common_features(**common_features_kw)

            title = ""
            kwargs = {}
            cbar_kwargs = dict(pad=0.01)

            if ds[var].GRIB_cfVarName in ["d2m", "dpt"]:
                ds[var].attrs["GRIB_cfName"] = "dew_point_temperature"

            if ds[var].GRIB_cfName == "air_temperature":
                kwargs = {**cm_tmp().cmap_kwargs, **kwargs}
                cbar_kwargs = {**cm_tmp().cbar_kwargs, **cbar_kwargs}
                if ds[var].GRIB_units == "K":
                    ds[var] -= 273.15
                    ds[var].attrs["GRIB_units"] = "C"
                    ds[var].attrs["units"] = "C"

            elif ds[var].GRIB_cfName == "dew_point_temperature":
                kwargs = {**cm_dpt().cmap_kwargs, **kwargs}
                cbar_kwargs = {**cm_dpt().cbar_kwargs, **cbar_kwargs}
                if ds[var].GRIB_units == "K":
                    ds[var] -= 273.15
                    ds[var].attrs["GRIB_units"] = "C"
                    ds[var].attrs["units"] = "C"

            elif ds[var].GRIB_name == "Total Precipitation":
                title = "-".join(
                    [f"F{int(i):02d}" for i in ds[var].GRIB_stepRange.split("-")]
                )
                ds[var] = ds[var].where(ds[var] != 0)
                kwargs = {**cm_pcp().cmap_kwargs, **kwargs}
                cbar_kwargs = {**cm_pcp().cbar_kwargs, **cbar_kwargs}

            elif ds[var].GRIB_name == "Maximum/Composite radar reflectivity":
                ds[var] = ds[var].where(ds[var] >= 0)
                cbar_kwargs = {**cm_reflectivity().cbar_kwargs, **cbar_kwargs}
                kwargs = {**cm_reflectivity().cmap_kwargs, **kwargs}

            elif ds[var].GRIB_cfName == "relative_humidity":
                cbar_kwargs = {**cm_rh().cbar_kwargs, **cbar_kwargs}
                kwargs = {**cm_rh().cmap_kwargs, **kwargs}

            else:
                cbar_kwargs = {
                    **dict(
                        label=f"{ds[var].GRIB_parameterName.strip().title()} ({ds[var].units})"
                    ),
                    **cbar_kwargs,
                }

            p = ax.pcolormesh(
                ds.longitude, ds.latitude, ds[var], transform=pc, **kwargs
            )
            plt.colorbar(p, ax=ax, **cbar_kwargs)

            VALID = pd.to_datetime(ds.valid_time.data).strftime("%H:%M UTC %d %b %Y")
            RUN = pd.to_datetime(ds.time.data).strftime("%H:%M UTC %d %b %Y")
            FXX = f"F{pd.to_timedelta(ds.step.data).total_seconds()/3600:02.0f}"

            level_type = ds[var].GRIB_typeOfLevel
            if level_type in _level_units:
                level_units = _level_units[level_type]
            else:
                level_units = "unknown"

            if level_units.lower() in ["surface", "atmosphere"]:
                level = f"{title} {level_units}"
            else:
                level = f"{ds[var][level_type].data:g} {level_units}"

            ax.set_title(
                f"Run: {RUN} {FXX}",
                loc="left",
                fontfamily="monospace",
                fontsize="x-small",
            )
            ax.set_title(f"HRRR {level}", loc="center", fontweight="semibold")
            ax.set_title(
                f"Valid: {VALID}",
                loc="right",
                fontfamily="monospace",
                fontsize="x-small",
            )

            # Set extent (could do this more efficiently by storing the data elsewhere)
            new = ds.crs.transform_points(pc, ds.longitude.data, ds.latitude.data)
            LONS = new[:, :, 0]
            LATS = new[:, :, 1]
            ax.set_extent([LONS.min(), LONS.max(), LATS.min(), LATS.max()], crs=ds.crs)

        return ax


#!/usr/bin/python3
import os
import subprocess

import datetime
from flask import Flask, render_template, request, redirect, url_for, flash, make_response
from flask.ext.sqlalchemy import SQLAlchemy
import requests, re
import socket

from haproxy import launch_haproxy

app = Flask(__name__)
with open("secret.txt") as f:
    app.secret_key = f.read()
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///user.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
my_ip = s.getsockname()[0]
s.close()

class Port(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.now() - datetime.timedelta(minutes=3))
    port = db.Column(db.Integer, unique=True)

    def __init__(self, port):
        self.port = port

class ActionByIPAdrr(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    timestamp = db.Column(db.DateTime, default=datetime.datetime.now())
    ip = db.Column(db.String, unique=True)

    def __init__(self, ip):
        self.ip = ip

def is_valid_ip(ip: str) -> bool:
    try:
        socket.inet_aton(ip)
        return True
    except:
        return False

def is_valid_port(port: str) -> bool:
    try:
        p = int(port)
        return 0 < p < 65536
    except:
        return False

def is_http_header(header: str) -> bool:
    m = re.match(r'\w{3,20} \w{3,200}', header)
    return m is not None

def is_valid_ssl_cert(ssl_cert: str) -> bool:
    p = subprocess.Popen(["openssl", "x509", "-text", "-noout"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    _ = p.communicate(input=ssl_cert.encode())
    return p.returncode == 0

def is_valid_ssl_key(ssl_key):
    p = subprocess.Popen(["openssl", "rsa", "-check"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    _ = p.communicate(input=ssl_key.encode())
    return p.returncode == 0

def port_is_used(port):
    return os.system("docker ps | grep '0.0.0.0:" + str(port) + "->" + str(port) + "/tcp'") == 0

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template('index.html')
    else:
        try:
            url_recaptcha = "https://www.google.com/recaptcha/api/siteverify"
            data_recaptcha = {
                "secret": "XXX",
                "response": request.form["g-recaptcha-response"]
            }
            ip1 = request.form["ip_backend1"]
            ip2 = request.form["ip_backend2"]
            port1 = request.form["port_backend1"]
            port2 = request.form["port_backend2"]
            ssl_cert = request.form["ssl_cert"]
            ssl_key = request.form["ssl_key"]
            http_headers = request.form["http_headers"]

            ip_client = request.remote_addr
            if ip_client != "134.214.182.12":
                action = ActionByIPAdrr.query.filter_by(ip=ip_client).first()
                if action and action.timestamp > datetime.datetime.now() - datetime.timedelta(seconds=20):
                    flash("Sorry you can only launch an instance every 20s.")
                    return redirect("/")
                elif action:
                    action.timestamp = datetime.datetime.now()
                else:
                    action = ActionByIPAdrr(ip=ip_client)
                    db.session.add(action)
            if http_headers:
                http_headers = http_headers.split(";")
                for http_header in http_headers:
                    if http_header and not is_http_header(http_header):
                        flash("Invalid HTTP header")
                        return redirect('/')
            if (ssl_key and not ssl_cert) or (ssl_cert and not ssl_key):
                flash("If you want to use SSL, you need to specify both SSL private key and SSL certificate.")
                return redirect("/")
            if ssl_cert and not is_valid_ssl_cert(ssl_cert):
                flash("SSL Cert not valid")
                return redirect('/')
            if ssl_key and not is_valid_ssl_key(ssl_key):
                flash("SSL Private Key not valid")
                return redirect('/')
            if ssl_key:
                ssl = (ssl_cert, ssl_key)
            else:
                ssl = None
            if not is_valid_ip(ip1) or not is_valid_ip(ip2) or not is_valid_port(port1) or not is_valid_port(port2):
                flash("Invalid IP/PORT")
                return redirect("/")
            try:
                pswd = request.args["pswd"]
            except:
                pswd = ""
            if pswd != "zTOkNv6AIFFTFuZLKFRPTdiTCwHIpvqtBlUuY4103lS2Xf8nnX":
                r = requests.post(url_recaptcha, data=data_recaptcha)
                resp_data = r.json()
                if not resp_data.get("success", False):
                    flash("Invalid captcha")
                    return redirect("/")

            port = Port.query.filter(Port.timestamp < datetime.datetime.now() - datetime.timedelta(minutes=2, seconds=30)).first()
            if not port or port_is_used(port.port):
                flash("Sorry I couldn't find a free port, wait a bit please.")
                return redirect("/")
            port.timestamp = datetime.datetime.now()
            db.session.commit()
            launch_haproxy.launch(port.port, http_headers, ip1 + ":" + port1, ip2 + ":" + port2, ssl)
            http = "http"
            if ssl:
                http += "s"
            flash("The load balancer has been successfully launched. It will be available for 2 minutes at: " + http + "://" + my_ip + ":" + str(port.port))
        except Exception as e:
            print(e)
            flash("Error launching your proxy. Please try again.")
        return redirect('/')

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


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('forum', '0007_proxyforumconversationattachment_proxyforumconversationpost_proxyforumconversationtopic_proxyforumco'),
    ]

    operations = [
        migrations.AlterField(
            model_name='forum',
            name='condominium',
            field=models.ForeignKey(null=True, blank=True, verbose_name='Condominium', to='system.Condominium'),
        ),
    ]


# Time:O(S)，其中 S 为所有可行解的长度之和。比较松的上界是O(n*2^n). n个位置考虑选还是不选。尽管有排序，nlogn可以忽略
# Space：O(target) 最坏情况递归target层

'''
聪明的剪枝：
1. 先对数组排序
2. 每次回溯只保留小于残差的数字组成列表，因为排序过，所以visited和索引不变
3. 回溯时限定begin，为了不超过len(candidate)，回溯时送入的begin=i，但是i会在最开始校验visited时被抛弃
'''

'''
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.

Each number in candidates may only be used once in the combination.

Note: The solution set must not contain duplicate combinations.

Example 1:

Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Example 2:

Input: candidates = [2,5,2,1,2], target = 5
Output:
[
[1,2,2],
[5]
]

Constraints:

1 <= candidates.length <= 100
1 <= candidates[i] <= 50
1 <= target <= 30

'''

class Solution(object):
    def combinationSum2(self, candidates, target):
        """
        :type candidates: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        visited = [False for _ in range(len(candidates))]
        res = list()
        candidates.sort()
        self.backtrack(res, [], candidates, target, visited,0)
        return res
    def backtrack(self, res, path, candidates, residue, visited, start):
        if residue == 0:
            res.append(path)
            return
        if residue < 0:
            return
        candidates = [x for x in candidates if x <= residue]
        for i in range(start, len(candidates)):
            if visited[i]:
                continue
            if i>0 and candidates[i-1] == candidates[i] and not visited[i-1]:
                continue
            visited[i] = True
            self.backtrack(res, path + [candidates[i]], candidates, residue - candidates[i], visited, i)
            visited[i] = False


import argparse
import logging

def get_args():
    """
    Parse arguments
    """
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--std_dir",
        type=str,
        required=True,
        help="The directory of standardised and typed files.",
    )
    parser.add_argument(
        "--final_dir",
        type=str,
        required=True,
        help="The directory of final derived files.",
    )
    return parser.parse_args()

def init_logger(module_name: str):
    logging.basicConfig(
        format="%(asctime)s - %(name)s:%(lineno)d - %(levelname)s - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S",
    )
    logger = logging.getLogger(module_name)
    logger.setLevel(logging.INFO)

    return logger


import os
from logging import info
from pathlib import Path
from typing import Tuple
from zipfile import ZipFile

import pandas as pd
import requests
import torch
from torch.utils.data import Dataset

from nn_repair.utils.checksums import sha256sum

class CMAPSS(Dataset):
    """
    The `Turbofan Engine Degradation Dataset (C-MAPSS)
    <https://www.nasa.gov/content/prognostics-center-of-excellence-data-set-repository>`_
    [SaxenaGoebel2008]_.

    Specifically, the FD001 dataset from the C-MAPSS dataset.
    The time series data is sliced into windows of a certain length to allow
    processing with a CNN.
    Each window corresponds to one data point in the resulting training or test set.
    The windows are overlapping with a stride of 1.

    The features of this data set are 2D tensors with shape `20, window_size`.
    The 20 channels are the setting and sensor features of the dataset with
    setting_3, sensor_1, sensor_18, and sensor_19 removed as they're constant
    in the dataset.
    The window size is set in the initializer.

    The targets are the remaining useful file (RUL) clipped to a value of 150, following
    [MathworksRULPred]_.

    .. [SaxenaGoebel2008] A. Saxena and K. Goebel (2008).
       "Turbofan Engine Degradation Simulation Data Set", NASA Prognostics
       Data Repository, NASA Ames Research Center, Moffett Field, CA

    .. [MathworksRULPred] https://de.mathworks.com/help/releases/R2021a/predmaint/ug/remaining-useful-life-estimation-using-convolutional-neural-network.html#mw_rtc_RULEstimationUsingCNNExample_98C35430

    """
    zip_url = "https://phm-datasets.s3.amazonaws.com/NASA/6.+Turbofan+Engine+Degradation+Simulation+Data+Set.zip"
    zip_checksum = "c9c5dec12a945a82e8bb4446589d7fb3cc057b5e5d81fa1a12e25ee9912ad3b2"
    subset = "FD001"
    train_inputs_file = "train_inputs.pyt"
    train_targets_file = "train_targets.pyt"
    test_inputs_file = "test_inputs.pyt"
    test_targets_file = "test_targets.pyt"

    def __init__(self, root: str, train: bool = True, window_size: int = 40, download: bool = False):
        """
        Loads the `Turbofan Engine Degradation Dataset (C-MAPSS)
        <https://www.nasa.gov/content/prognostics-center-of-excellence-data-set-repository>`_
        [SaxenaGoebel2008]_.

        :param root: The root directory where the C-MAPSS folder is placed or
          is to be downloaded to if download is set to True.
        :param train: Whether to get the training set or test set of
          the dataset.
        :param window_size: The size of the window into which the time series data
         is split.
         This is the last dimension
        :param download: Whether to download the C-MAPSS dataset from
          https://www.nasa.gov/content/prognostics-center-of-excellence-data-set-repository
          if it isn't present in the root directory.
        """
        self.files_dir = Path(root, f"C-MAPSS_window_{window_size}")
        self.window_size = window_size
        if not self.files_dir.exists():
            if not download:
                raise RuntimeError("Dataset not found. "
                                   "Download it by passing download=True.")
            os.mkdir(self.files_dir)
            self._download()

        if train:
            inputs_file = self.train_inputs_file
            targets_file = self.train_targets_file
        else:
            inputs_file = self.test_inputs_file
            targets_file = self.test_targets_file
        self.data = torch.load(Path(self.files_dir, inputs_file))
        self.targets = torch.load(Path(self.files_dir, targets_file))

    def _download(self):
        info("Downloading C-MAPSS dataset file...")
        # the dataset is in a zip file in a zip file
        zip1_path = Path(self.files_dir, "Turbofan.zip")
        temp_extracted_path = Path(
            self.files_dir, "6. Turbofan Engine Degradation Simulation Data Set"
        )
        zip2_path = Path(temp_extracted_path, "CMAPSSData.zip")

        result = requests.get(self.zip_url)
        with open(zip1_path, "xb") as file:
            file.write(result.content)

        zip1_checksum = sha256sum(zip1_path)
        if zip1_checksum != zip1_checksum:
            raise RuntimeError(
                f"Downloaded dataset file has different checksum than "
                f"expected: {zip1_path}. "
                f"Expected sha256 checksum: {zip1_checksum}"
            )

        info("Extracting data...")
        with ZipFile(zip1_path) as zip_obj:
            zip_obj.extractall(self.files_dir)

        with ZipFile(zip2_path) as zip_obj:
            with zip_obj.open(f"train_{self.subset}.txt") as file:
                train_raw = pd.read_csv(file, sep=" ", header=None)
            with zip_obj.open(f"test_{self.subset}.txt") as file:
                test_raw = pd.read_csv(file, sep=" ", header=None)
            with zip_obj.open(f"RUL_{self.subset}.txt") as file:
                test_targets_raw = pd.read_csv(file, sep=" ", header=None)

        zip1_path.unlink()
        zip2_path.unlink()
        temp_extracted_path.rmdir()

        info("Preprocessing data...")
        # the train and test files contain two training spaces that lead
        # to two extra columns
        train_raw.drop([26, 27], axis=1, inplace=True)
        test_raw.drop([26, 27], axis=1, inplace=True)
        # the RUL file has only one training space
        test_targets_raw.drop([1], axis=1, inplace=True)

        # split the data by ids => each result is a time series
        train_time_series = train_raw.groupby(0)  # column 0 is the ID
        test_time_series = test_raw.groupby(0)

        # compute the number of cycles until failure for the training set
        # column 1 is the cycle number
        train_failure_cycle = {id_: df[1].max() for id_, df in train_time_series}

        # 0, 1 are id and cycle number
        # the remaining ones are constant in the dataset (zero variance)
        cols_to_drop = [0, 1, 4, 5, 9, 14, 20, 22, 23]

        # make windows of the training data
        train_inputs = []
        train_targets = []
        for id_, df in train_time_series:
            for i in range(len(df) - self.window_size + 1):
                window = df.iloc[i:i+self.window_size]
                # predict the remaining useful life at the end of the
                # time series
                target = train_failure_cycle[id_] - window[1].max()
                # drop id, cycle number and constant features
                window = window.drop(cols_to_drop, axis=1)
                train_inputs.append(torch.as_tensor(window.to_numpy(), dtype=torch.float))
                train_targets.append(target)
        train_inputs = torch.stack(train_inputs)
        train_targets = torch.tensor(train_targets, dtype=torch.int)

        test_inputs = []
        test_targets = []
        for id_, df in test_time_series:
            final_target = test_targets_raw.iloc[id_ - 1]  # ids start from 1
            if len(df) < self.window_size:
                data = df.drop(cols_to_drop, axis=1).to_numpy()
                window = torch.full((self.window_size, data.shape[1]), fill_value=0.0)
                window[:len(df), :] = torch.as_tensor(data)
                test_inputs.append(window)
                test_targets.append(final_target)
            else:
                final_target += df[1].max()  # RUL at the start of the time series
                for i in range(len(df) - self.window_size + 1):
                    window = df.iloc[i:i+self.window_size]
                    # predict the remaining useful life at the end of the
                    # time series
                    target = final_target - window[1].max()
                    window = window.drop(cols_to_drop, axis=1)
                    test_inputs.append(torch.as_tensor(window.to_numpy(), dtype=torch.float))
                    test_targets.append(target)
        test_inputs = torch.stack(test_inputs)
        test_targets = torch.tensor(test_targets, dtype=torch.int)

        # make channel x window_size tensors
        train_inputs.transpose_(-2, -1)
        test_inputs.transpose_(-2, -1)

        # clip RULs to 150
        train_targets.clip_(max=150)
        test_targets.clip_(max=150)

        torch.save(train_inputs, Path(self.files_dir, self.train_inputs_file))
        torch.save(train_targets, Path(self.files_dir, self.train_targets_file))
        torch.save(test_inputs, Path(self.files_dir, self.test_inputs_file))
        torch.save(test_targets, Path(self.files_dir, self.test_targets_file))
        info("Download finished.")

    def __getitem__(self, index: int) -> Tuple[torch.Tensor, torch.Tensor]:
        return self.data[index], self.targets[index]

    def __len__(self) -> int:
        return len(self.targets)


import numpy as np

def precisionk(actual: list, recommended: list):
    """
    Computes the number of relevant results among the top k recommended items

    Parameters
    ----------
    actual: list
        A list of ground truth items
        example: [X, Y, Z]
    recommended: list
        A list of ground truth items (all possible relevant items)
        example: [x, y, z]

    Returns
    ----------
        precision at k
    """
    relevantResults = set(actual) & set(recommended)
    assert 0 <= len(
        relevantResults), f"The number of relevant results is not true (currently: {len(relevantResults)})"
    return 1.0 * len(relevantResults) / len(recommended)

def recallk(actual: list, recommended: list):
    """
    The number of relevant results among the top k recommended items divided by the total number of relevant items

    Parameters
    ----------
    actual: list
        A list of ground truth items (all possible relevant items)
        example: [X, Y, Z]
    recommended: list
        A list of items recommended by the system
        example: [x, y, z]

    Returns
    ----------
        recall at k
    """
    relevantResults = set(actual) & set(recommended)
    assert 0 <= len(
        relevantResults), f"The number of relevant results is not true (currently: {len(relevantResults)})"
    return 1.0 * len(relevantResults) / len(actual)

def mapk(actual: list, predicted: list, k: int = 10):
    """
    Computes mean Average Precision at k (mAPk) for a set of recommended items

    Parameters
    ----------
    actual: list
        A list of ground truth items (order doesn't matter)
        example: [X, Y, Z]
    predicted: list
        A list of predicted items, recommended by the system (order matters)
        example: [x, y, z]
    k: integer, optional (default to 10)
        The number of elements of predicted to consider in the calculation

    Returns
    ----------
    score:
        The mean Average Precision at k (mAPk)
    """
    score = 0.0
    numberOfHits = 0.0
    for i, p in enumerate(predicted):
        if p in actual and p not in predicted[:i]:
            numberOfHits += 1.0
            score += numberOfHits / (i+1.0)
    if not actual:
        return 0.0
    score = score / min(len(actual), k)
    return score

def dcg(scores: list):
    """
    Computes the Discounted Cumulative Gain (DCG) for a list of scores

    Parameters
    ----------
    scores: list
        A list of scores

    Returns
    ----------
    dcg: float
        The Discounted Cumulative Gain (DCG)
    """
    return np.sum(np.divide(np.power(2, scores) - 1, np.log(np.arange(scores.shape[0], dtype=np.float32) + 2)),
                  dtype=np.float32)

def ndcgk(actual: list, predicted: list, relevance=None, at=None):
    """
    Calculates the implicit version of Normalized Discounted Cumulative Gain (NDCG) for top k items in the ranked output

    Parameters
    ----------
    actual: list
        A list of ground truth items
        example: [X, Y, Z]
    predicted: list
        A list of predicted items, recommended by the system
        example: [x, y, z]
    relevance: list, optional (default to None)
        A list of relevance scores for the items in the ground truth
    at: any, optional (default to None)
        The number of items to consider in the calculation

    Returns
    ----------
    ndcg:
        Normalized DCG score

    Metric Defintion
    ----------
    Jarvelin, K., & Kekalainen, J. (2002). Cumulated gain-based evaluation of IR techniques.
    ACM Transactions on Information Systems (TOIS), 20(4), 422-446.
    """
    # Convert list to numpy array
    actual, predicted = np.asarray(list(actual)), np.asarray(list(predicted))
    # Check the relevance value
    if relevance == None:
        relevance = np.ones_like(actual)
    assert len(relevance) == actual.shape[0]
    # Creating a dictionary associating itemId to its relevance
    item2rel = {it: r for it, r in zip(actual, relevance)}
    # Creates array of length "at" with the relevance associated to the item in that position
    rankScores = np.asarray([item2rel.get(it, 0.0)
                            for it in predicted[:at]], dtype=np.float32)
    # IDCG has all relevances to 1, up to the number of items in the test set
    idcg = dcg(np.sort(relevance)[::-1])
    # Calculating rank-DCG, DCG uses the relevance of the recommended items
    rdcg = dcg(rankScores)
    if rdcg == 0.0:
        return 0.0
    # Return items
    return round(rdcg / idcg, 4)


#!/usr/bin/env python
# Based on astropy affiliated package template's setup.py
# Licensed under a 3-clause BSD style license - see LICENSE.md
from __future__ import print_function

import glob
import os
import sys
import imp
import ast

__minimum_python_version__ = "3.5"

# Enforce Python version check - this is the same check as in __init__.py but
# this one has to happen before importing ah_bootstrap.
if sys.version_info < tuple((int(val) for val in __minimum_python_version__.split('.'))):
    sys.stderr.write("ERROR: poppy requires Python {} or later\n".format(__minimum_python_version__))
    sys.exit(1)

try:
    import numpy
except ImportError:
    print("""
WARNING: NumPy was not found! setup.py will attempt to install it if asked, but
\tyou may experience issues. Try installing NumPy first, separately.
\tSee https://github.com/numpy/numpy/issues/2434 for details.
""")

import ah_bootstrap
from setuptools import setup

#A dirty hack to get around some early import/configurations ambiguities
if sys.version_info[0] >= 3:
    import builtins
else:
    import __builtin__ as builtins
builtins._ASTROPY_SETUP_ = True

from astropy_helpers.setup_helpers import (
    register_commands, adjust_compiler, get_debug_option, get_package_info)
from astropy_helpers.git_helpers import get_git_devstr
from astropy_helpers.version_helpers import generate_version_py

# Get some values from the setup.cfg
try:
    from ConfigParser import ConfigParser
except ImportError:
    from configparser import ConfigParser

conf = ConfigParser()
conf.read(['setup.cfg'])
metadata = dict(conf.items('metadata'))

PACKAGENAME = metadata.get('package_name', 'packagename')
DESCRIPTION = metadata.get('description', 'unknown')
AUTHOR = metadata.get('author', '')
AUTHOR_EMAIL = metadata.get('author_email', '')
LICENSE = metadata.get('license', 'unknown')
URL = metadata.get('url', 'http://astropy.org')

# Get the long description from the package's docstring
_, module_path, _ = imp.find_module(PACKAGENAME)
with open(os.path.join(module_path, '__init__.py')) as f:
    module_ast = ast.parse(f.read())
LONG_DESCRIPTION = ast.get_docstring(module_ast)

# Store the package name in a built-in variable so it's easy
# to get from other parts of the setup infrastructure
builtins._ASTROPY_PACKAGE_NAME_ = PACKAGENAME

# VERSION should be PEP386 compatible (http://www.python.org/dev/peps/pep-0386)
VERSION = '0.9.0'

# Indicates if this version is a release version
RELEASE = 'dev' not in VERSION

if not RELEASE:
    VERSION += get_git_devstr(False)

# Populate the dict of setup command overrides; this should be done before
# invoking any other functionality from distutils since it can potentially
# modify distutils' behavior.
cmdclassd = register_commands(PACKAGENAME, VERSION, RELEASE)

# Adjust the compiler in case the default on this platform is to use a
# broken one.
adjust_compiler(PACKAGENAME)

# Freeze build information in version.py
generate_version_py(PACKAGENAME, VERSION, RELEASE,
                    get_debug_option(PACKAGENAME))

# Treat everything in scripts except README.rst as a script to be installed
scripts = [fname for fname in glob.glob(os.path.join('scripts', '*'))
           if os.path.basename(fname) != 'README.rst']

# Get configuration information from all of the various subpackages.
# See the docstring for setup_helpers.update_package_files for more
# details.
package_info = get_package_info()

# Add the project-global data
package_info['package_data'].setdefault(PACKAGENAME, [])
package_info['package_data'][PACKAGENAME].append('data/*')

# Include all .c files, recursively, including those generated by
# Cython, since we can not do this in MANIFEST.in with a "dynamic"
# directory name.
c_files = []
for root, dirs, files in os.walk(PACKAGENAME):
    for filename in files:
        if filename.endswith('.c'):
            c_files.append(
                os.path.join(
                    os.path.relpath(root, PACKAGENAME), filename))
package_info['package_data'][PACKAGENAME].extend(c_files)

install_requires_packages = [
      'numpy>=1.13.0',
      'scipy>=1.0.0',
      'matplotlib>=2.0.0',
      'astropy>=3.0.0',
]

setup(name=PACKAGENAME,
      version=VERSION,
      description=DESCRIPTION,
      scripts=scripts,
      python_requires='>=' + __minimum_python_version__,
      install_requires=install_requires_packages,
      provides=[PACKAGENAME],
      author=AUTHOR,
      author_email=AUTHOR_EMAIL,
      license=LICENSE,
      url=URL,
      long_description=LONG_DESCRIPTION,
      cmdclass=cmdclassd,
      zip_safe=False,
      **package_info
)


from django.shortcuts import render,redirect
from .cleaner import SparkApp
import os
from .forms import FileUploadForm
from .models import FileModel
from django.conf import settings
from django.http import HttpResponse
import multiprocessing

def mainPage(request):
    latest_file = None
    form=FileUploadForm()

    if request.method == 'POST':
        form=FileUploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()

        latest_file=FileModel.objects.order_by('id').first()
        #file_path = os.path.join(settings.MEDIA_ROOT, latest_file.file.name)
        #result=SparkApp(file_path).cleanAny()

    return render(request,'main/form.html',{'form':form,'dataframe':latest_file,'file':latest_file})

def listFiles(request):
    files=FileModel.objects.all()
    context={'files':files}
    return render(request,'main/list.html',context)

def viewPage(request,pk):
    file=FileModel.objects.get(id=pk)
    file_path=os.path.join(settings.MEDIA_ROOT,file.file.name)
    print(file.mode)
    machine=SparkApp(file_path)
    if file.mode == 'ALL':
        result=machine.cleanAll()
        schema=machine.recommendSchema()

    else:
        result=machine.cleanAny()
        schema=machine.recommendSchema()

    print('schema is saved! --------------')

    context={'result':result,'schema':schema,'file':file}
    return render(request,'main/live.html',context)

def deletePage(request,pk):
    file=FileModel.objects.get(id=pk)
    file.delete()
    return redirect('listPage')

def downloadPage(request,pk):
    file=FileModel.objects.get(id=pk)
    file_path=os.path.join(settings.MEDIA_ROOT,file.file.name)
    if request.method == 'GET':
        file_path = file.file.path
        with open(file_path, 'rb') as file:
        # Set the appropriate content-type and headers for the response
            response = HttpResponse(file.read(), content_type='application/octet-stream')
            response['Content-Disposition'] = f'attachment; filename="{os.path.basename(file_path)}"'

        return response


import numpy as np
import sys

def scan(file):
    data = []
    with open(file) as file_in:
        for line in file_in:
            data.append(line)
    return data

def erase_spaces_and_comments(data):
    data_fix = []
    for i in range(len(data)):
        if len(data[i]) == 0:
            continue
        last = len(data[i])
        for j in range(len(data[i])):
            if data[i][j][0] == '#':
                last = j
        if last == 0:
            continue
        data_fix.append(data[i][:last])
    return data_fix

def write_answer_to_file(answer,file):
    with open(file, 'w') as answer_file:
        for s in answer:
            answer_file.write(s + '\n')

state = 'state.txt'
state_fix = 'state_fix.txt'
creatures = 'creatures.conf'
creatures_fix = 'creatures_fix.conf'

data_state = erase_spaces_and_comments([s.split() for s in scan(state)])
date_creatures = erase_spaces_and_comments([s.split() for s in scan(creatures)])

data_str = []
data_str.append(
    str(data_state[0][1])
    + ' ' +  str(data_state[0][3])
    + ' ' + str(len(data_state) - 1))

for i in range(1,len(data_state)):
    for j in range(len(data_state[i])):
        data_str.append(data_state[i][j])

print(data_str)
write_answer_to_file(data_str, state_fix)

d = dict()
for s in date_creatures:
    d[s[0]] = s[2]

L = ['pollutionBound', 'divisionBound', 'vConsumeFeed',
'vConsumeStuff1', 'vConsumeStuff2', 'vProduceStuff1', 'vProduceStuff2']
data_str = [d[l] for l in L]
print(data_str)

write_answer_to_file(data_str, creatures_fix)


import collections

class Solution:

    def solve_shortest_point2point_path(self, graph, point2point_shortest_path, istart, iend):
        if istart == iend:
            return 0
        if len(graph[istart]) == 0:
            return 12
        if iend in graph[istart]:
            return 1

        shortest_path_len = 12
        for interal in graph[istart]:
            interal_shortest_path_len = 1 + self.solve_shortest_point2point_path(graph, point2point_shortest_path, interal, iend)
            if shortest_path_len > interal_shortest_path_len:
                shortest_path_len = interal_shortest_path_len
        return shortest_path_len

    def shortestPathLength1(self, graph):
        """
        :type graph: List[List[int]]
        :rtype: int
        """

        # first get point to point, the shortest path length
        point2point_shortest_path = [[12 for x in range(0, len(graph))] for y in range(0, len(graph))]
        for istart in range(0, len(graph)):
            point2point_shortest_path[istart][istart] = 0
            for iend in graph[istart]:
                point2point_shortest_path[istart][iend] = 1

        for istart in range(0, len(graph)):
            for iend in range(0, len(graph)):
                point2point_shortest_path[istart][iend] = \
                    self.solve_shortest_point2point_path(graph, point2point_shortest_path, istart, iend)

        print(point2point_shortest_path)

    def shortestPathLength1(self, graph):
        N = len(graph)
        queue = collections.deque((1 << x, x) for x in xrange(N))
        dist = collections.defaultdict(lambda: N * N)
        for x in xrange(N): dist[1 << x, x] = 0

        while queue:
            cover, head = queue.popleft()
            d = dist[cover, head]
            if cover == 2 ** N - 1: return d
            for child in graph[head]:
                cover2 = cover | (1 << child)
                if d + 1 < dist[cover2, child]:
                    dist[cover2, child] = d + 1
                    queue.append((cover2, child))

    def shortestPathLength(self, graph):
        N = len(graph)
        dist = [[float('inf')] * N for i in xrange(1 << N)]
        for x in xrange(N):
            dist[1 << x][x] = 0

        for cover in xrange(1 << N):
            repeat = True
            while repeat:
                repeat = False
                for head, d in enumerate(dist[cover]):
                    for nei in graph[head]:
                        cover2 = cover | (1 << nei)
                        if d + 1 < dist[cover2][nei]:
                            dist[cover2][nei] = d + 1
                            if cover == cover2:
                                repeat = True

        return min(dist[2 ** N - 1])

if __name__ == '__main__':
    sol = Solution()
    sol.shortestPathLength([[1],[0,2,4],[1,3,4],[2],[1,2]])


import math

from components.frustum import Frustum
from components.vectors import Vector3D
from components.polygons import Mesh, Triangle, Quad

from components.utils import clamp_float
from copy import deepcopy

class Camera:
    def __init__(self, width: int, height: int) -> None:
        self.frustum = Frustum(width, height)
        self.yaw = 0.0
        self.pitch = 0.0
        self.camera_position = Vector3D(-1000.0, 25.0, 500.0)
        self.camera_target = Vector3D(0.0, 0.0, 0.0)
        self.side_direction = Vector3D(1.0, 0.0, 0.0)
        self.up_direction = Vector3D(0.0, 1.0, 0.0)
        self.look_direction = Vector3D(0.0, 0.0, 1.0)

        self.previous_pointer = (width / 2.0, height / 2.0)
        self.enable_frustum_clipping = True

        self.save()

    def save(self):
        self.dict = deepcopy(self.__dict__)

    def toggle_frustum_clipping(self):
        self.enable_frustum_clipping = not self.enable_frustum_clipping
        print("FRUSTUM CLIPPING:", self.enable_frustum_clipping)

    def apply_view_transform(self, position: Vector3D) -> Vector3D:
        look_dir = self.look_direction
        side_dir = self.side_direction
        up_dir = self.up_direction

        point = position.subtract_vector(self.camera_position)
        x = point.dot_product(side_dir)
        y = point.dot_product(up_dir)
        z = point.dot_product(look_dir)

        translated_point = Vector3D(x, y, z)
        return translated_point

    def ndc_to_screen_coordinates(self, position: Vector3D) -> Vector3D:
        half_width = self.frustum.width / 2.0
        half_height = self.frustum.height / 2.0

        x = (position.x + 1.0) * half_width
        y = (1.0 - position.y) * half_height

        screen_coordinates = Vector3D(x, y, position.z)
        return screen_coordinates

    def calculate_perspective_projection(self, position: Vector3D) -> Vector3D:
        width = self.frustum.width
        height = self.frustum.height
        fov_degrees = self.frustum.fov
        zn = self.frustum.near_plane
        zf = self.frustum.far_plane

        xi = position.x
        yi = position.y
        zi = position.z

        aspect_ratio = width / height
        fov_rad = math.tan(math.radians(fov_degrees / 2))

        xo = xi * (1 / (fov_rad * aspect_ratio))
        yo = yi * (1 / (fov_rad))
        zo = zi * -((zf - zn) / (zn - zf)) + ((2 * zf * zn) / (zn - zf))

        if zi != 0.0:
            xo /= -zi
            yo /= -zi
            zo /= -zi

        vo = Vector3D(xo, yo, zo)
        return vo

    def apply_projection_polygons(self, mesh: Mesh) -> Mesh:
        mesh = deepcopy(mesh)
        self.apply_direction_adjustment()

        for polygon in mesh.polygons:
            vertices = list(polygon.shape.vertices)
            for idx, vertex in enumerate(vertices):
                vertex = self.apply_view_transform(vertex)
                vertices[idx] = vertex
            polygon.shape.vertices = tuple(vertices)

        if self.enable_frustum_clipping:
            self.frustum.frustum_clip(mesh)

        for polygon in mesh.polygons:
            vertices = list(polygon.shape.vertices)
            for idx, vertex in enumerate(vertices):
                vertex = self.calculate_perspective_projection(vertex)
                vertex = self.ndc_to_screen_coordinates(vertex)
                vertices[idx] = vertex
            polygon.shape.vertices = tuple(vertices)
        return mesh

    def filter_polygons_outside_frustum(self, mesh: Mesh):
        polygons = []

        for polygon in mesh.polygons:
            if isinstance(polygon.shape, Triangle):
                v1 = polygon.shape.vertices[0]
                v2 = polygon.shape.vertices[1]
                v3 = polygon.shape.vertices[2]

                v1 = self.apply_view_transform(v1)
                v2 = self.apply_view_transform(v2)
                v3 = self.apply_view_transform(v3)

                v1_in = self.frustum.is_point_in_frustum(v1)
                v2_in = self.frustum.is_point_in_frustum(v2)
                v3_in = self.frustum.is_point_in_frustum(v3)
                in_frustums = [v1_in, v2_in, v3_in]
                if any(in_frustums):
                    polygons.append(polygon)

            elif isinstance(polygon.shape, Quad):
                v1 = polygon.shape.vertices[0]
                v2 = polygon.shape.vertices[1]
                v3 = polygon.shape.vertices[2]
                v4 = polygon.shape.vertices[3]

                v1 = self.apply_view_transform(v1)
                v2 = self.apply_view_transform(v2)
                v3 = self.apply_view_transform(v3)
                v4 = self.apply_view_transform(v4)

                v1_in = self.frustum.is_point_in_frustum(v1)
                v2_in = self.frustum.is_point_in_frustum(v2)
                v3_in = self.frustum.is_point_in_frustum(v3)
                v4_in = self.frustum.is_point_in_frustum(v4)
                in_frustums = [v1_in, v2_in, v3_in, v4_in]
                if any(in_frustums):
                    polygons.append(polygon)
        mesh.polygons = polygons
        return mesh

    def handle_mouse_movement(self, x: float, y: float) -> None:
        sens_x = 0.3
        sens_y = 0.1

        dx = x - self.previous_pointer[0]
        dy = y - self.previous_pointer[1]
        self.previous_pointer = (x, y)

        self.yaw += dx * sens_x
        self.pitch += dy * -sens_y

        if self.pitch > 89.0:
            self.pitch = 89.0
        elif self.pitch < -89.0:
            self.pitch = -89.0
        self.apply_mouse_movement()

    def apply_direction_adjustment(self):
        self.look_direction = self.camera_target.subtract_vector(self.camera_position)
        self.look_direction = self.look_direction.normalize()
        self.side_direction = self.look_direction.cross_product(self.up_direction)
        self.side_direction = self.side_direction.normalize()

        yaw_rad = math.radians(self.yaw)
        pitch_rad = math.radians(self.pitch - 90.0)
        up_x = math.cos(yaw_rad) * math.cos(pitch_rad)
        up_y = math.sin(pitch_rad)
        up_z = math.sin(yaw_rad) * math.cos(pitch_rad)
        self.up_direction = Vector3D(up_x, up_y, up_z).normalize()

    def apply_mouse_movement(self):
        yaw_rad = math.radians(self.yaw)
        pitch_rad = math.radians(self.pitch)

        direction_x = math.cos(yaw_rad) * math.cos(pitch_rad)
        direction_y = math.sin(pitch_rad)
        direction_z = math.sin(yaw_rad) * math.cos(pitch_rad)

        direction = Vector3D(direction_x, direction_y, direction_z)
        self.camera_target = self.camera_position.add_vector(direction)
        self.apply_direction_adjustment()

    def increment_position_x(self, increment: float):
        side_vector = self.side_direction.multiply(-increment)
        side_vector.y = 0
        self.camera_position = self.camera_position.add_vector(side_vector)
        self.camera_target = self.camera_position.add_vector(self.look_direction)

    def increment_position_y(self, increment: float):
        up_vector = self.up_direction.multiply(increment)
        self.camera_position = self.camera_position.add_vector(up_vector)
        self.camera_target = self.camera_position.add_vector(self.look_direction)

    def increment_position_z(self, increment: float):
        look_vector = self.look_direction.multiply(increment)
        look_vector.y = 0
        self.camera_position = self.camera_position.add_vector(look_vector)
        self.camera_target = self.camera_position.add_vector(self.look_direction)

    def increment_planes(self, increment: float):
        near_plane = self.frustum.near_plane
        far_plane = self.frustum.far_plane

        if (near_plane + increment) > 0.1:
            near_plane += increment
            far_plane += increment
            self.frustum.near_plane = clamp_float(near_plane, 0.1, float("inf"))
            self.frustum.far_plane = clamp_float(far_plane, near_plane, float("inf"))

    def increment_target_x(self, increment: float):
        self.camera_target.x += increment

    def increment_target_y(self, increment: float):
        self.camera_target.y += increment

    def increment_target_z(self, increment: float):
        self.camera_target.z += increment

    def reset(self):
        class_dict = deepcopy(self.dict)

        for variable, value in class_dict.items():
            self.__setattr__(variable, value)


# @author Metro
# @time 2021/11/24
import os.path

import gym
from agents.pdqn import P_DQN
from utilities.memory import ReplayBuffer
from utilities.utilities import *
from utilities.route_generator import generate_routefile

class Train_and_Evaluate(object):

    def __init__(self, config):
        # Environment
        generate_routefile(seed=config.seed, demand=config.demand)
        self.env = gym.make(config.environment)

        # Agent
        self.agent = P_DQN(config, self.env)

        # Memory
        self.replay_memory_size = config.hyperparameters['replay_memory_size']
        self.batch_size = config.hyperparameters['batch_size']
        self.updates_per_step = config.hyperparameters['updates_per_step']
        self.memory = ReplayBuffer(self.replay_memory_size)

        self.total_steps = 0
        self.total_updates = 0

        self.save_freq = config.save_freq
        self.file_to_save = config.file_to_save
        self.maximum_episodes = config.hyperparameters['maximum_episodes']

        self.train = config.train
        self.evaluate = config.evaluate
        self.evaluate_internal = config.evaluate_internal

        self.agent_to_color_dictionary = config.agent_to_color_dictionary
        self.standard_deviation_results = config.standard_deviation_results

        self.colors = ['red', 'blue', 'green', 'orange', 'yellow', 'purple']
        self.color_idx = 0

        self.rolling_score_window = config.rolling_score_window
        self.runs_per_agent = config.runs_per_agent
        self.agent_name = config.agent_name
        self.ceil = config.ceil

        # Training Loop

    def train_agent(self):
        """

        :return:
        """

        rolling_scores_for_diff_runs = []
        file_to_save_actor = os.path.join(self.file_to_save, 'actor/')
        file_to_save_actor_param = os.path.join(self.file_to_save, 'actor_param/')
        file_to_save_runs = os.path.join(self.file_to_save, 'runs_1/')
        file_to_save_rolling_scores = os.path.join(self.file_to_save, 'rolling_scores/')
        os.makedirs(file_to_save_actor, exist_ok=True)
        os.makedirs(file_to_save_actor_param, exist_ok=True)
        os.makedirs(file_to_save_runs, exist_ok=True)
        os.makedirs(file_to_save_rolling_scores, exist_ok=True)

        for run in range(self.runs_per_agent):
            game_full_episodes_scores = []
            game_full_episodes_rolling_scores = []

            for i_episode in range(self.maximum_episodes):

                if self.save_freq > 0 and i_episode % self.save_freq == 0:
                    actor_path = os.path.join(file_to_save_actor, 'episode{}'.format(i_episode))
                    actor_param_path = os.path.join(file_to_save_actor_param, 'episode{}'.format(i_episode))
                    self.agent.save_models(actor_path, actor_param_path)

                episode_score = []
                episode_steps = 0
                done = 0
                state = self.env.reset()  # n_steps

                while not done:
                    if len(self.memory) > self.batch_size:
                        action, action_params = self.agent.select_action(state, self.train)
                        if self.ceil:
                            action_params = np.ceil(action_params).squeeze(0)

                        action_for_env = [action, int(action_params[action])]

                        for i in range(self.updates_per_step):
                            self.agent.update(self.memory)
                            self.total_updates += 1
                    else:
                        action_params = np.random.randint(low=10, high=31, size=8)
                        action = np.random.randint(7, size=1)[0]
                        action_for_env = [action, action_params[action]]

                    next_state, reward, done, info = self.env.step(action_for_env)
                    print(reward)

                    episode_steps += 1
                    episode_score.append(info)

                    self.total_steps += 1
                    self.memory.push(state, action, action_params, reward, next_state, done)

                    state = next_state

                episode_score_so_far = np.mean(episode_score)
                game_full_episodes_scores.append(episode_score_so_far)
                game_full_episodes_rolling_scores.append(
                    np.mean(game_full_episodes_scores[-1 * self.rolling_score_window:]))

                print("Episode: {}, total steps:{}, episode steps:{}, scores:{}".format(
                    i_episode, self.total_steps, episode_steps, episode_score_so_far))

                self.env.close()
                file_path_for_pic = os.path.join(file_to_save_runs, 'episode{}_run{}.jpg'.format(i_episode, run))
                visualize_results_per_run(agent_results=game_full_episodes_scores,
                                          agent_name=self.agent_name,
                                          save_freq=1,
                                          file_path_for_pic=file_path_for_pic)
                rolling_scores_for_diff_runs.append(game_full_episodes_rolling_scores)

        file_path_for_pic = os.path.join(file_to_save_rolling_scores, 'rolling_scores.jpg')
        visualize_overall_agent_results(agent_results=rolling_scores_for_diff_runs,
                                        agent_name=self.agent_name,
                                        show_mean_and_std_range=True,
                                        agent_to_color_dictionary=self.agent_to_color_dictionary,
                                        standard_deviation_results=1,
                                        file_path_for_pic=file_path_for_pic
                                        )


import math

forcas=[]
n = int(input())

for i in range(n):
    pi = int(input())
    forcas.append(pi)

forcas.sort()
menor_d=forcas[0]
for i in range(len(forcas)-1):
    d=forcas[i+1] - forcas[i]
    if d < menor_d:
        menor_d = d
print(menor_d)

'''
import sys
import math

t=0
n = int(input())
for e,i in enumerate(input().split()):
    if (int(i)==0) or (e==0):
        t = int(i)
    elif (abs(int(i))-ref)<0:
        t=int(i)
    ref=abs(t)
print(t)

'''

'''ultimo=len(forcas)-1
menor_d=0

for i in range(len(forcas)-1):
    diferenca = forcas[ultimo]-forcas[ultimo-1]
    if i == 0:
        menor_d=diferenca
    elif diferenca<menor_d:
        menor_d=diferenca
    ultimo = ultimo-1
print(menor_d)'''


"""
整数
N
 が
9
 の倍数であることと、
N
 を十進法で表したときの各桁の数の和が
9
 の倍数であることは同値です。

N
 が
9
 の倍数であるか判定してください。

"""
# -*- coding: utf-8 -*-

import time
input_val = input()

#1
start = time.time()

a = list(map(int, input_val))
sum_val = sum(a)
if sum_val % 9 == 0:
    print("Yes")
else:
    print("No")

elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")

start = time.time()
if int(input_val) % 9 == 0:
    print("Yes")
else:
    print("No")

elapsed_time = time.time() - start
print("elapsed_time:{0}".format(elapsed_time) + "[sec]")


from django.urls import path
from .views import login_view, ConversationListCreateAPIView, ConversationRetrieveUpdateDestroyAPIView, ChatMessageListAPIView, ChatMessageDestroyAPIView
from .views.document_view import DocumentRetrieveUpdateDestroyAPIView, DocumentListCreateAPIView

urlpatterns = [
    path('auth/login', login_view, name='login'),
    path('conversations', ConversationListCreateAPIView.as_view(), name='conversation_list_create'),
    path('conversations/<int:pk>', ConversationRetrieveUpdateDestroyAPIView.as_view(), name='conversation_retrieve_update_destroy'),
    path('conversations/<int:conversation_id>/messages', ChatMessageListAPIView.as_view(), name='chat_message_list'),
    path('conversations/<int:conversation_id>/messages/<int:pk>', ChatMessageDestroyAPIView.as_view(), name='chat_message_delete'),
    path('documents', DocumentListCreateAPIView.as_view(), name='document-list-create'),
    path('documents/<int:pk>', DocumentRetrieveUpdateDestroyAPIView.as_view(), name='document-retrieve-update-destroy'),
]


import numpy as np

def skew(v):
    return np.float32([[0, -v[2], v[1]],
                       [v[2], 0, -v[0]],
                       [-v[1], v[0], 0]])

class HTransf(object):
    """
            Class for representing and manipulating homogeneous transformation matrices in SE(3)
            constructor args: (a) angle, (k) axis, (t) translation,
            alternativly use the from_matrix() or from_vecs() methods to create a class object from an existing ndarray represeinting
            a homogeneous transformation matrix in SE(3)
    """

    def __init__(self, a=0, k=np.array([1, 1, 1]), t=np.array([0, 0, 0])):
        self.angle = a
        self.axis = (k/np.linalg.norm(k)).reshape(3, 1)
        self.skew_axis = skew(self.axis)
        self.t = t
        self.rot_matrix = np.identity(
            3) + np.sin(a)*self.skew_axis + (1-np.cos(a))*(self.skew_axis@self.skew_axis)
        self.matrix = np.zeros((4, 4))
        self.matrix[0:3, 0:3] = self.rot_matrix
        self.matrix[0:3, 3] = t.reshape(3)
        self.matrix[3, :] = np.array([0, 0, 0, 1])

    @classmethod
    def from_matrix(cls, m):
        '''allows construction from  honogeneous transformation matrix'''
        if m.shape == (3, 3):
            t = np.array([0, 0, 0])
        else:
            t = np.array([m[0][3], m[1][3], m[2][3]])
        trR = m[0][0]+m[1][1]+m[2][2]
        if(trR > 3.0):
            trR = 3.0
        if(trR < -1):
            trR = -1.0
        if(np.isclose(trR, -1.0, rtol=1e-4)):  # trR==-1):
            a = np.pi
            trR = -1.0
            if not np.isclose(m[2, 2], -1.0, rtol=1e-3):
                k = 1.0/(np.sqrt(2.0*(1.0+m[2][2]))) * \
                    np.array([m[0][2], m[1][2], 1.0+m[2][2]])
            elif not np.isclose(m[1, 1], -1.0, rtol=1e-3):
                k = 1.0/(np.sqrt(2.0*(1.0+m[1][1]))) * \
                    np.array([m[0][1], 1.0+m[1][1], m[2][1]])
            else:
                k = 1.0/(np.sqrt(2.0*(1.0+m[0][0]))) * \
                    np.array([1.0+m[0][0], m[1][0], m[2][0]])
        else:
            a = np.arccos((trR-1)/2)
            if(np.isclose(a, 0.0, rtol=1e-2)):
                a = 0.0
                return cls(t=t)
            # print('regner ut k2')
            k = (1.0/(2.0*np.sin(a))) * \
                (np.array([m[2][1]-m[1][2], m[0][2]-m[2][0], m[1][0]-m[0][1]]))
        return cls(a, k, t)

    @classmethod
    def from_vecs(cls, r=np.array([0, 0, 0]), t=np.array([0, 0, 0])):
        a = np.linalg.norm(r)
        k = r / a
        return cls(a, k, t)

    @classmethod
    def from_kuka_pose(cls, x, y, z, a, b, c):
        '''derivation for zyx euler angles
        found in modern robotics appendix B.1'''
        Ca = np.cos(a)
        Cb = np.cos(b)
        Cc = np.cos(c)
        Sa = np.sin(a)
        Sb = np.sin(b)
        Sc = np.sin(c)
        m = np.array([[Ca*Cb, Ca*Sb*Sc-Sa*Cc, Ca*Sb*Cc+Sa*Sc, x],
                      [Sa*Cb, Sa*Sb*Sc+Ca*Cc, Sa*Sb*Cc-Ca*Sc, y],
                      [-Sb, Cb*Sc, Cb*Cc, z],
                      [0, 0, 0, 1]])
        return cls.from_matrix(m)

    @classmethod
    def from_quat_pose(cls, p):
        # Q = [q1, q2, q3, q4] is a unit quaternion q1 is the scalar component,
        # and q2, q3, q4 are the vector components
        # using formulas on p. 54 in "Robotics, modelling and control" by Siciliano et al.
        # a = np.arccos(q1)/2
        # k = np.sin(a/2) * np.array(q2, q3, q4)

        x, y, z = p[0], p[1], p[2]
        q1, q2, q3, q4 = p[3], p[4], p[5], p[6]

        T = np.array([[2*(q1**2+q2**2)-1, 2*(q2*q3-q1*q4), 2*(q2*q4+q1*q3), x],
                      [2*(q2*q3+q1*q4), 2*(q1**2+q3**2)-1, 2*(q3*q4-q1*q2), y],
                      [2*(q2*q4-q1*q3), 2*(q3*q4+q1*q2), 2*(q1**2+q4**2)-1, z],
                      [0, 0, 0, 1]])
        return cls.from_matrix(T)

    def __str__(self):
        # defines what is printed when invocing the print() method on the object
        return "{}\n".format(self.matrix)

    def __repr__(self):
        return 'HTransf object'

    def __matmul__(self, other):
        # overloads the matrix multiplication operator '@'
        if isinstance(other, np.ndarray):
            if other.shape == (3,):
                return self.rot_matrix @ other
            return self.matrix @ other
        return HTransf.from_matrix(self.matrix @ other.matrix)

    def __add__(self, other):
        # note that for addition and subptraction the result is not in SE(3)
        if isinstance(other, np.ndarray):
            return self.matrix+other
        return self.matrix+other.matrix

    def __sub__(self, other):
        if isinstance(other, np.ndarray):
            return self.matrix-other
        return self.matrix-other.matrix

    def to_kuka_pose(self):
        m = self.rot_matrix
        beta = np.arctan2(-m[2][0], np.sqrt(m[0][0]**2+m[1][0]**2))
        alpha = np.arctan2(m[1][0], m[0][0])
        gamma = np.arctan2(m[2][1], m[2][2])
        return np.rad2deg(alpha), np.rad2deg(beta), np.rad2deg(gamma)

    def quat_pose(self):
        q1 = np.cos(self.angle / 2)
        Q = np.sin(self.angle / 2) * self.axis

        return np.array([self.t[0], self.t[1], self.t[2], q1, Q[0], Q[1], Q[2]])

    def to_mm(self):
        transmm = self.t*1000
        angle, axis = self.angle_axis()
        return HTransf(angle, axis, transmm)

    def tr(self):
        return np.trace(self.matrix)

    def tr_rot(self):
        return np.trace(self.rot_matrix)

    def angle_axis(self):
        '''
        returns the angle-axis representation of the rotation matrix
        '''
        return (self.angle, self.axis)

    def rvec(self):
        a, k = self.angle_axis()
        return a*k

    def get_translation(self):
        return self.t

    def inv(self):
        '''returns a HTransf object representing the inverse
        Homogeneous transformation matrix
        of the calling object'''
        return HTransf.from_matrix(np.linalg.inv(self.matrix))

    def get_matrix(self):
        return self.matrix

    def confirm_SO3(self):
        '''
        returns true if the rotation is member of the SO(3) group, false otherwise.
        '''
        det = np.linalg.det(self.rot_matrix)
        rotTransp = self.rot_matrix.T
        rotInv = np.linalg.inv(self.rot_matrix)
        if (np.isclose(det, 1.0, atol=1e-3) and np.isclose(np.sum(rotTransp-rotInv), 0.0, atol=1e-3)):
            return True
        else:
            return False

if __name__ == "__main__":
    T = HTransf(np.pi/3, np.array([1, 0, 0]), np.array([1, 2, 3]))
    print(T)
    print(T.quat_pose())
    H = HTransf.from_quat_pose(T.quat_pose())
    print(H)


import traceback # for error handling
import configparser
import time
import datetime
from bs4 import BeautifulSoup
from selenium import webdriver # for scraping
from selenium.webdriver.chrome.options import Options
import subprocess # for executing bash

path = "settings.ini"

def executeCommand(command):
  error = ""
  output = ""
  print("Executing: " + command)
  try:
    output = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT)
    output = output.decode('utf-8')
  except Exception as e:
    error = "ERROR: " + str(e.returncode) + "  " + str(e) + "\n"
    output = repr(e)  # to get the output even when error
  #print("Output: " + output)
  return output, error

while 1<2:

    config = configparser.ConfigParser()
    config.read(path)

    for section in config.sections():
        address = config.get(section, "address")
        print("###### Kill older processes of chromedriver and xvfb")
        output, error = executeCommand('kill -kill $(ps -eaf | grep -i chrome | grep -v grep | tr -s " " | cut -d " " -f 2)')
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument("user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36")
        driver = webdriver.Chrome(chrome_options=chrome_options)

        try:
            print ('https://charts.bogged.finance/?token=' + address)
            driver.get('https://charts.bogged.finance/?token=' + address)  # known url using cloudflare's "under attack mode"
            print ("Sleeping 10 sec for page to load")
            time.sleep (10)
            #driver.save_screenshot('poocoin.png')
            content = driver.page_source
            #print ("CONTENT:")
            #print (content)
            #print ("End content")
            soup = BeautifulSoup(content, features="html.parser")
            try:
              divMarketCap = soup.findAll('div', attrs={'class':'items-center lg:h-16 h-10 lg:bg-transparent bg-white dark:bg-transparent flex flex-row justify-center text-center lg:text-left lg:justify-start space-x-3 lg:pt-0 lg:my-1.5 my-0 sm:space-x-6 w-full lg:w-auto'})
              for span in divMarketCap[0].findAll('span'):
                  if "Marketcap" in str(span) and "$" in str(span):
                      marketcap = span.findAll('h4')[0].text.strip()
            except:
              marketcap = 0

            try:
              price = "$" + str(soup.findAll('title')).split("$")[1].split(" ")[0]
            except:
              price = 0

            try:
              divsIndex = soup.findAll('div', attrs={'flex flex-row space-x-2 items-center'})
              for divIndex in divsIndex:
                for h4 in divIndex.findAll('h4'):
                    if "%" in str(h4):
                      index = h4.text.strip().replace("%", "")
            except:
              index = 0

            print(datetime.datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"))
            string = 'Market Cap= '+marketcap+'   Price = '+ price+'   Index = '+ index
            print(string)
            config.set(section, "datetime", datetime.datetime.now().strftime("%d-%b-%Y (%H:%M:%S.%f)"))
            config.set(section, "price", price)
            config.set(section, "marketcap", marketcap)
            config.set(section, "index", index)
            with open(path, "w") as config_file:
                config.write(config_file)
            driver.quit()
        except Exception as e:
            print ("Something wrong, killing chrome")
            print ("Error: {}".format(e))
            tracebackError = traceback.format_exc()
            print (tracebackError)
            driver.quit()

    print ("Sleeping 60")
    time.sleep(60)


import numpy as np
from typing_extensions import ParamSpecArgs
from typing import Union

from prio_replay.maze import LinearTrack, OpenField
from prio_replay.parameters import Parameters
from mazemdp.toolbox import sample_categorical
from prio_replay.q_learning import get_action

"""==============================================================================================================="""

def create_plan_exp( m : Union[LinearTrack,OpenField], params : Parameters ) :
    """ Creates a matrix of tuple (state, action, reward, next_state) based on the previous experiences

        Arguments
        ----------
            m -- Union[LinearTrack,OpenField] from maze.py : class with the maze and the agent
            params -- Parameters from parameters.py : class with the settings of the current simulation

        Returns
        ----------
            planExp -- matrix ((state X action) X 4) : memory of last reward and next state obtained of each tuple (state, action)
    """

    """
    create a matrix with shape :
     [
       [ [ s[0] a[0] last_r[0,0] last_stp1[0,0] ] ]
       [ [ s[1] a[0] last_r[1,0] last_stp1[1,0] ] ]
       [ [ s[2] a[0] last_r[2,0] last_stp1[2,0] ] ]
                     ....
       [ [ s[0] a[1] last_r[0,1] last_stp1[0,1] ] ]
       [ [ s[1] a[1] last_r[1,1] last_stp1[1,1] ] ]
       [ [ s[2] a[1] last_r[2,1] last_stp1[2,1] ] ]
                  ..........
     ]
    """
    planExp = np.empty( (0,4) )

    for a in range(m.action_space.n) :
        for s in range(m.nb_states) :
            exp = np.array( [ s, a, m.exp_LastR[s,a], m.exp_LastStp1[s,a] ] )
            planExp = np.append( planExp, [exp], axis=0 )

    if params.remove_samestate : # remove all experiences that goes to the same states (i.e walls)
        planExp = planExp[planExp[:, 0] != planExp[:, 3]]

    planExp = planExp[ np.invert( np.isnan(planExp).any(axis=1) ) ] # remove all experiences with NaNs in it , we dont need this theoretically

    return planExp

"""==============================================================================================================="""

def expand_plan_exp(planning_backups, planExp, m : Union[OpenField,LinearTrack], params) :

    seqStart = np.argwhere(planning_backups[:, 4] == 1)[-1][0]
    seqSoFar = planning_backups[seqStart:, 0:4]

    sn = int( seqSoFar[-1, 3] )  # Final state reached in the last planning st...

    if params.onlineVSoffline == "online" : # agent is awake
        an = get_action(sn,m,params)

    else : # agent is asleep
        probs = np.zeros( np.size(m.Q[sn]) )
        probs[ m.Q[sn] == max( m.Q[sn] ) ] =  1 / ( sum(m.Q[sn]) == max(m.Q[sn]) )
        an = sample_categorical(probs)

    snp1 = m.exp_LastStp1[sn,an]
    rn = m.exp_LastR[sn,an]

    step_isNaN = np.isnan( m.exp_LastStp1[sn,an] ) or np.isnan( m.exp_LastR[sn,an] )
    step_isRepeated = np.isin( snp1 , [ seqSoFar[:, 0], seqSoFar[:, 3] ] )

    if (not step_isNaN) and (params.allowLoops or (not step_isRepeated)) :
        expanded_exp = np.array( [sn, an, rn, snp1] )
        seqUpdated = np.append( seqSoFar, [expanded_exp], axis=0 )
        planExp.append(seqUpdated)

"""==============================================================================================================="""

def update_planning_backups(planning_backups, plan_exp_arr_max) :

    if planning_backups.shape[0] > 0:
        planning_backups = np.vstack([planning_backups, np.append(plan_exp_arr_max, plan_exp_arr_max.shape[0])])
    elif planning_backups.shape[0] == 0:
        planning_backups = np.append(plan_exp_arr_max,plan_exp_arr_max.shape[0]).reshape(1, planning_backups.shape[1])
    else:
        err_msg = 'planning_backups does not have the correct shape. It is {} but should have a length equal to 1 or 2, e.g. (5,) or (2, 5)'.format(planning_backups.shape)
        raise ValueError(err_msg)

    return planning_backups

"""==============================================================================================================="""


"""Fake booking status data access object"""
from typing import Optional

from bookshop_app.models.booking_status import BookingStatus
from bookshop_app.models.booking_status import BookingStatusModel
from bookshop_app.schemas.booking_status import BookingStatusSchema
from bookshop_test_framework.mocks.dao.base import BaseFakeDAO

booking_status_schema = BookingStatusSchema()

class FakeBookingStatusDAO(BaseFakeDAO):
    """Data Access Object that uses class attribute for storing data"""

    _data: list[BookingStatusModel] = [
        *[BookingStatusModel(id=index + 1, name=status) for index, status in enumerate(BookingStatus)]
    ]

    @classmethod
    def get_by_id(cls, booking_status_id: int) -> Optional[BookingStatusModel]:
        """Get booking status by id"""
        for booking_status in cls._data:
            if booking_status.id == booking_status_id:
                return booking_status
        return None

    @classmethod
    def get_all(cls) -> list[BookingStatusModel]:
        """Return all booking statuses"""
        return cls._data


#!/usr/bin/env python3.7

def format_float(num):
    if '.' in num:
        whole, decimal = num.split('.')
        return ','.join([whole, decimal[:3]])
    else:
        return num

if __name__ == '__main__':
    while True:
        try:
            num = input('give me a float')
            float(num)
        except ValueError:
            print("Value is not a number")
            continue
        output = format_float(num)
        print(output)


import logging
from datetime import datetime, timedelta
from enum import Enum
from typing import Generic, TypeVar, Optional

from vocode.streaming.models.model import TypedModel

class VoiceActivityDetectorType(str, Enum):
    BASE = "base_voice_activity_detector"
    WEB_RTC = "web_rtc_voice_activity_detector"
    SILERO = "silero_voice_activity_detector"
    LIB_ROSA = "librosa_voice_activity_detector"

class BaseVoiceActivityDetectorConfig(TypedModel, type=VoiceActivityDetectorType.BASE.value):
    frame_rate: int = 16000
    min_activity_duration: timedelta = timedelta(milliseconds=400)
    min_mute_duration: timedelta = timedelta(seconds=3)
    speach_ratio: float = .8

VoiceActivityDetectorConfigType = TypeVar("VoiceActivityDetectorConfigType", bound=BaseVoiceActivityDetectorConfig)

class BaseVoiceActivityDetector(Generic[VoiceActivityDetectorConfigType]):
    def __init__(self, config: VoiceActivityDetectorConfigType, logger: Optional[logging.Logger] = None):
        self.logger: logging.Logger = logger or logging.getLogger(__name__)
        self.config = config
        self.speach_start_timestamp = None
        self.activity_state = {True: 0, False: 0}
        self.is_speaking = False
        self.is_interrupted = False

    def is_voice_active(self, frame: bytes) -> bool:
        raise NotImplementedError

    def get_config(self) -> BaseVoiceActivityDetectorConfig:
        return self.config

    def should_interrupt(self, frame: bytes) -> bool:
        now = datetime.now()
        is_voice_active = self.is_voice_active(frame)

        self.activity_state[is_voice_active] += 1

        if is_voice_active and self.speach_start_timestamp is None:
            self.activity_state = {True: 0, False: 0}
            self.speach_start_timestamp = now

        if self.speach_start_timestamp is None:
            return False

        if now - self.speach_start_timestamp > self.config.min_mute_duration and not is_voice_active:
            self.speach_start_timestamp = None
            self.is_interrupted = False
            return False

        if (now - self.speach_start_timestamp) > self.config.min_activity_duration:
            speach_ratio = self.activity_state[True] / (self.activity_state[True] + self.activity_state[False])
            if speach_ratio > self.config.speach_ratio:
                if self.is_interrupted:
                    return False
                self.is_interrupted = True
                return True
            else:
                self.is_interrupted = False
                self.speach_start_timestamp = None


class Solution:
    def search(self, nums: List[int], target: int) -> int:
#======== <Solution 1> ========#
        return nums.index(target) if target in nums else -1

# Reference: https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/14435/Clever-idea-making-it-simple
#======== <Solution 2> ========#
        l, r = 0, len(nums) - 1
        while l < r:
            mid = (l + r) // 2
            if nums[0] <= nums[mid]:  # nums[mid] is on the left side
                if nums[0] <= target:  # target is on the left side
                    if nums[mid] < target:
                        l = mid + 1
                    else:
                        r = mid
                else:  # target is on the right side
                    l = mid + 1
            else:  # nums[mid] is on the right side
                if target < nums[0]:  # target is on the right side
                    if nums[mid] < target:
                        l = mid + 1
                    else:
                        r = mid
                else:  # target is on the left side
                    r = mid
        return l if nums[l] == target else -1

#======== <Solution 3> ========#
        l, r = 0, len(nums)
        while l < r:
            mid = (l + r) // 2
            # nums[mid] is on the left side and target is on the right side
            if target < nums[0] <= nums[mid]:
                l = mid + 1
            # target is on the left side and nums[mid] is on the right side
            elif nums[mid] < nums[0] <= target:
                r = mid
            # Both nums[mid] and target are on the same side
            elif nums[mid] < target:
                l = mid + 1
            elif target < nums[mid]:
                r = mid
            # nums[mid] == target
            else:
                return mid
        return -1

#======== <Solution 4> ========#
        l, r = 0, len(nums)
        while l < r:
            mid = (l + r) // 2
            if (nums[mid] < nums[0]) == (target < nums[0]):  # Both nums[mid] and target are on the same side
                num = nums[mid]
            elif target < nums[0]:  # nums[mid] is on the left side and target is on the right side
                num = float('-inf')
            else:  # target is on the left side and nums[mid] is on the right side
                num = float('inf')

            if num < target:
                l = mid + 1
            elif target < num:
                r = mid
            else:
                return mid
        return -1


import datetime

from django import forms
from django.core.exceptions import ValidationError

from .models import *

class TextName(forms.Form):
    your_name = forms.CharField(
        label="Твое имя",
        help_text="Attention!",
        initial=datetime.date.today,
        max_length=100,
        widget=forms.Textarea,
        required=False,
        error_messages={"required": "Egghead, what did you do?"}
    )

class ReviewForm(forms.ModelForm):
    class Meta:
        model = Reviews
        fields = ["Text", "Name", "Email"]

# class Comment(forms.ModelForm):
#     # slug = CharField(validators=[validate_Age])
#     class Meta:
#         model = DirectorsActors
#         fields = ["Name", "Age", "Description", "Image"]
        # widgets = {
        #     "Name": forms.Textarea(attrs={"cols": 10, "rows": 10}),
        # }
        # labels = {
        #     "Name":_("Имя быстро"),
        #     "Age":_("Возраст какой а?"),
        # }
        #
        # help_texts={
        #     "Name": _("Имя нужно в латинице")
        # }

    #
    # def clean_name(self):
    #     name = self.cleaned_data["Name"]
    #     if len(name)>6:
    #         raise ValidationError("Ты шо правила не читал?")
    #     return name


import numpy as np

def perspective_fov(fov, aspect_ratio, near_plane, far_plane):
	num = 1.0 / np.tan(fov / 2.0)
	num9 = num / aspect_ratio
	return np.array([
		[num9, 0.0, 0.0, 0.0],
		[0.0, num, 0.0, 0.0],
		[0.0, 0.0, far_plane / (near_plane - far_plane), -1.0],
		[0.0, 0.0, (near_plane * far_plane) / (near_plane - far_plane), 0.0]
	])

def look_at(camera_position, camera_target, up_vector):
	vector = camera_position - camera_target
	vector = vector / np.linalg.norm(vector)

	vector2 = np.cross(up_vector, vector)
	vector2 = vector2 / np.linalg.norm(vector2)

	vector3 = np.cross(vector, vector2)
	return np.array([
		[vector2[0], vector3[0], vector[0], 0.0],
		[vector2[1], vector3[1], vector[1], 0.0],
		[vector2[2], vector3[2], vector[2], 0.0],
		[-np.dot(vector2, camera_position), -np.dot(vector3, camera_position), np.dot(vector, camera_position), 1.0]
	])


from constants import addGenePlasmidsAureus

def errAbort(msg):
    " print err msg and exit "
    raise Exception(msg)
    sys.exit(0)  # cgi must not exit with 1

def revComp(seq):
    " rev-comp a dna sequence with UIPAC characters "
    revTbl = {'A' : 'T', 'C' : 'G', 'G' : 'C', 'T' : 'A', 'N' : 'N' , 'M' : 'K', 'K' : 'M',
        "R" : "Y" , "Y":"R" , "g":"c", "a":"t", "c":"g","t":"a", "n":"n"}
    newSeq = []
    for c in reversed(seq):
        newSeq.append(revTbl[c])
    return "".join(newSeq)

def makeTempFile(prefix, suffix):
    " return a temporary file that is deleted upon exit, unless DEBUG is set "
    if DEBUG:
        fname = join("/tmp", prefix+suffix)
        fh = open(fname, "w")
    else:
        fh = tempfile.NamedTemporaryFile(prefix="primer3In", suffix=".txt")
    return fh

def setupPamInfo(pam):
    " modify a few globals based on the current pam "
    if pam=="TTTN":
        GUIDELEN = 23
        cpf1Mode = True
        addGenePlasmids = 'None'
    elif pam=="NNGRRT":
        addGenePlasmids = addGenePlasmidsAureus
        GUIDELEN = 21
        cpf1Mode = False
    else:
        GUIDELEN = 20
        cpf1Mode = False
        addGenePlasmids = 'None'
    return GUIDELEN,cpf1Mode,addGenePlasmids


from edge import Edge
from node import Node

class Graph:
    def __init__(self,  edges=None, nodes=None):
        self.edges = [] if edges is None else edges
        self.nodes = [] if nodes is None else nodes
        self.predecessors = {}
        self.successors = {}

    def add_node(self, node):
        if not isinstance(node, Node):
            raise TypeError("node is not an instance of Node class")
        if self.search_node(node.identity) is None:
            self.nodes.append(node)

    def add_edge(self, edge):
        if not isinstance(edge, Edge):
            raise TypeError("edge is not an instance of Edge class")
        if self.search_edge(edge) is None:
            self.edges.append(edge)

    def search_node(self, identity):
        node = [n for n in self.nodes if n.identity == identity]
        if node:
            return node[0]
        return None

    def search_edge(self, edge):

        edges = [e for e in self.edges if (
                e.from_node == edge.from_node
                and e.to_node == edge.to_node
                and e.type_label == edge.type_label)]

        if edges:
            return edges[0]
        return None

    def update_resource(self, resource_data):
        name = resource_data['name']
        length_name = len(name.split("/"))
        ancestors = resource_data['ancestors'][1:]  # exclude self
        first_ancestor = None
        if ancestors:
            first_ancestor = ancestors[0]  # first parent
        identity = "/".join(name.split("/")[length_name - 2:])
        asset_type = resource_data['asset_type'].split("/")[-1]
        if first_ancestor:  # maintain direct successors of first parent
            node_resource_first_ancestor = Node(asset_type, first_ancestor)
            self.add_node(node_resource_first_ancestor)
            if node_resource_first_ancestor.identity not in self.successors:
                self.successors[node_resource_first_ancestor.identity] = []
            self.successors[node_resource_first_ancestor.identity].append(identity)
        node_resource = Node(asset_type, identity)
        self.add_node(node_resource)
        self.predecessors[node_resource.identity] = ancestors
        bindings_data = resource_data['iam_policy']['bindings']
        for binding_data in bindings_data:
            type_label = binding_data['role'].split("/")[-1]
            members = binding_data['members']
            for member in members:
                node_member = Node("Member", member)
                self.add_node(node_member)
                edge = Edge(node_resource, node_member, type_label)
                self.add_edge(edge)


#!/bin/env python3
n = int(input())
str = list(input())
for i in range(n):
    team = []
    for j in range(len(str)-1):
        if str[j] == str[j+1]:
            team.append(j)
    if team == []:
        break
    team.reverse()
    for j in team:
        str.pop(j)
print(n - len(str))


from p2pd.test_init import *
from p2pd import IPRange, Bind, Route, Interface
from p2pd.net import BLACK_HOLE_IPS, ip_norm, DUEL_STACK, IP6, IP4
from p2pd.net import NIC_BIND, EXT_BIND, TCP, socket_factory

class TestBind(unittest.IsolatedAsyncioTestCase):
    async def test_bind(self):
        i = await Interface().start_local()
        af = i.stack if i.stack != DUEL_STACK else IP4
        b = Bind(i, af)

    async def test_ip_val_v6_bind_types(self):
        i = await Interface().start_local()
        try:
            # Test global tuples set.
            ip = ip_norm("2402:1f00:8101:83f::1")
            b = Bind(i, IP6, ips=ip)
            await b.bind()
            self.assertEqual(b.ext_bind, ip)
            self.assertEqual(ip_norm(b.bind_tup(flag=EXT_BIND)[0]), ip)
            self.assertEqual(ip_norm(b.ext_bind), ip)

            # Test link local tuples set.
            ip = i.route(IP6).nic()
            b = Bind(i, IP6, ips=ip)
            await b.bind()
            self.assertEqual(b.nic_bind, ip)
            self.assertEqual(ip_norm(b.bind_tup(flag=NIC_BIND)[0]), ip)
            self.assertEqual(ip_norm(b.nic_bind), ip)
            s = await socket_factory(b)
            self.assertTrue(isinstance(s, socket.socket))
            s.close()
        except:
            return

    async def test_ip_val_v4_bind_types(self):
        i = await Interface().start_local()

        # Test nic bind occurs.
        tests = ["192.168.0.1", "8.8.8.8"]
        for ip in tests:
            b = Bind(i, IP4, ips=ip)
            self.assertEqual(b.nic_bind, ip)
            await b.bind()
            self.assertEqual(b.bind_tup(flag=NIC_BIND)[0], ip)

    async def test_route_v4_bind_types(self):
        i = await Interface().start_local()
        r = i.route(IP4)
        b = await r.bind()
        tup = b.bind_tup(flag=NIC_BIND)
        self.assertTrue(tup[0])

    async def test_route_v6_bind_types(self):
        i = await Interface().start_local()

        try:
            af = IP6
            r = await i.route_test(af)

            # Patch EXT to return a pub IP without interface being resolved.
            r.ext = lambda: "2402:1f00:8101:083f:0000:0000:0000:0001"
            b = await r.bind()
            tup = b.bind_tup(flag=EXT_BIND)
            self.assertTrue(tup[0])
        except:
            return

    # TODO: netifaces is pulling invalid net masks for some IPs?
    async def test_bind_assumptions(self):
        ip = "139.99.209.1"
        #socket.socket(IP4, TCP)

    async def test_bind_start_v4_all_addr(self):
        af = IP4
        try:
            i = await Interface(af).start_local()
        except:
            # If not supported.
            return

        route = await i.route(af).bind(13453, "*")
        bind_tup = ("0.0.0.0", 13453)
        expected_tups = {
            EXT_BIND: bind_tup,
            NIC_BIND: bind_tup
        }
        self.assertEqual(route._bind_tups, expected_tups)
        s = await socket_factory(route)
        self.assertTrue(s is not None)
        if s is not None:
            s.close()

    async def test_bind_start_v6_all_addr(self):
        i = await Interface().start_local()
        try:
            af = IP6
            route = await i.route(af).bind(13453, "*")
            bind_tup = (
                "::",
                13453,

                # NIC no and scope ID stuff from getaddrinfo.
                route._bind_tups[EXT_BIND][2],
                route._bind_tups[EXT_BIND][3]
            )
            expected_tups = {
                EXT_BIND: bind_tup,
                NIC_BIND: bind_tup
            }
            self.assertEqual(route._bind_tups, expected_tups)
            s = await socket_factory(route)
            self.assertTrue(s is not None)
            if s is not None:
                s.close()
        except:
            return

# TODO loopback tests.

if __name__ == '__main__':
    main()


"""

Copyright (C) 2021 Hoichan JUNG <hoichanjung@korea.ac.kr> - All Rights Reserved

"""

import time
from tqdm import tqdm
from copy import deepcopy

import torch
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torchvision.transforms as transforms
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.optim.lr_scheduler import ReduceLROnPlateau, CosineAnnealingWarmRestarts
from warmup_scheduler import GradualWarmupScheduler
from IQA_pytorch import SSIM
from ptflops import get_model_complexity_info

from config import getConfig
from DatasetGenerator import DatasetGenerator
from models import *
from utils import *
from losses import *

cfg = getConfig()

class Trainer():
    def train(pathDirData, noiseType, nnArchitecture, batchSize, trMaxEpoch, imgtransResize, model_name, logger, run):

        # -------------------- SETTINGS: NETWORK ARCHITECTURE
        if nnArchitecture == 'UNET':
            model = Denoising_UNet(in_channels=1, n_classes=1, padding=True).cuda()
        elif nnArchitecture == 'REDNET':
            model = REDNet20(in_channels=1, num_layers=6).cuda()
        elif nnArchitecture == 'UNET_REDNET':
            model = UNet_REDNet().cuda()
        elif nnArchitecture == 'VDSR':
            model = VDSR().cuda()
        elif nnArchitecture == 'HINET':
            model = HINet(in_chn = 1).cuda()
        elif nnArchitecture == 'MPRNET':
            model = MPRNet(in_c = 1, out_c=1).cuda()
        elif nnArchitecture == 'UFORMER':
            model = Uformer(img_size=imgtransResize, in_chans=1, token_mlp='ffn').cuda()
        elif nnArchitecture == 'RESTORMER':
            model = Restormer(inp_channels=1, out_channels=1, LayerNorm_type='BiasFree').cuda()

        macs, params = get_model_complexity_info(model, (1, imgtransResize, imgtransResize), as_strings=True,
                                                print_per_layer_stat=False, verbose=True)
        print('MACs(G): ', macs)
        print('Params: ', params)

        logger.info(f'MACs(G): {macs}')
        logger.info(f'Params: {params}')

        run['MACs(G)'] = macs
        run['Params'] = params

        model = torch.nn.DataParallel(model).cuda()
        pathModel = f'results/{model_name}/model.pth.tar'

        # -------------------- SETTINGS: DATA TRANSFORMS
        transformList = []
        # transformList.append(transforms.RandomHorizontalFlip())
        # transformList.append(transforms.RandomVerticalFlip())
        # transformList.append(transforms.RandomAffine((-20, 20)))
        # transformList.append(transforms.RandomRotation(90))
        transformList.append(transforms.Resize(imgtransResize))
        transformList.append(transforms.ToTensor())
        # transformList.append(transforms.Normalize([0.5], [0.5]))
        transform = transforms.Compose(transformList)

        # -------------------- SETTINGS: DATASET BUILDERS
        datasetTrain = DatasetGenerator(pathDirData, dataset='train', noise_type=noiseType, transform=transform, logger=logger)
        datasetValid = DatasetGenerator(pathDirData, dataset='valid', noise_type=noiseType, transform=transform, logger=logger)

        dataLoaderTrain = DataLoader(dataset = datasetTrain, batch_size = batchSize , shuffle = True, pin_memory = True)
        dataLoaderValid = DataLoader(dataset = datasetValid, batch_size = batchSize , shuffle = False, pin_memory = True)
        run['Batch'] = batchSize
        # -------------------- SETTINGS: OPTIMIZER & SCHEDULER
        if cfg.op == 'adam':
            optimizer = optim.Adam(model.parameters(), lr=cfg.lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=1e-5)

        if cfg.scheduler == 'ReduceLROnPlateau':
            scheduler = ReduceLROnPlateau(optimizer, factor=0.1, patience=cfg.patience, mode='min')
        elif cfg.scheduler == 'CosineAnnealingWarmRestarts':
            warmup_epochs = 3
            scheduler_cosine = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, trMaxEpoch-warmup_epochs, eta_min=1e-8)
            scheduler = GradualWarmupScheduler(optimizer, multiplier=1, total_epoch=warmup_epochs, after_scheduler=scheduler_cosine)

        # -------------------- SETTINGS: Loss Function
        # criterion_mse = nn.MSELoss()
        criterion_char = CharbonnierLoss()
        criterion_edge = EdgeLoss()
        # criterion_psnr = PSNRLoss()

        # -------------------- TRAIN / VALIDATION
        lossMIN = 10000

        for epochID in range(trMaxEpoch):
            timestampTime = time.strftime("%H%M%S")
            timestampDate = time.strftime("%d%m%Y")
            timestampSTART = timestampDate + '-' + timestampTime
            print('current lr : {:.7f}'.format(optimizer.param_groups[0]['lr']))

            if nnArchitecture == 'UNET' or nnArchitecture == 'REDNET' or nnArchitecture == 'UNET_REDNET'\
                or nnArchitecture == 'VDSR' or nnArchitecture == 'UFORMER' or nnArchitecture == 'RESTORMER':
                epochTrainUNET(model, dataLoaderTrain, optimizer, criterion_char, criterion_edge)
                lossVal, losstensor = epochValidUNET(model, dataLoaderValid, criterion_char, criterion_edge, epochID, model_name)

            elif nnArchitecture == 'HINET' or nnArchitecture == 'MPRNET':
                epochTrainHINET(model, dataLoaderTrain, optimizer, criterion_char, criterion_edge)
                lossVal, losstensor = epochValidHINET(model, dataLoaderValid, criterion_char, criterion_edge, epochID, model_name)

            timestampTime = time.strftime("%H%M%S")
            timestampDate = time.strftime("%d%m%Y")
            timestampEND = timestampDate + '-' + timestampTime

            scheduler.step(losstensor.data)

            if lossVal < lossMIN:
                lossMIN = lossVal

                torch.save({'epoch': epochID + 1, 'state_dict': model.state_dict(), 'best_loss': lossMIN,
                            'optimizer': optimizer.state_dict()}, pathModel)
                print('Epoch [' + str(epochID + 1) + '] [save] [' + timestampEND + '] loss= ' + str(lossVal))
                logger.info('Epoch [' + str(epochID + 1) + '] [save] [' + timestampEND + '] loss= ' + str(lossVal))
                run['Valid loss'].log(lossVal)

            else:
                print('Epoch [' + str(epochID + 1) + '] [----] [' + timestampEND + '] loss= ' + str(lossVal))
                logger.info('Epoch [' + str(epochID + 1) + '] [----] [' + timestampEND + '] loss= ' + str(lossVal))

    def test(pathDirData, noiseType, nnArchitecture, batchSize, imgtransResize, model_name, logger, run):
        cudnn.benchmark = True

        # -------------------- SETTINGS: NETWORK ARCHITECTURE
        if nnArchitecture == 'UNET':
            model = Denoising_UNet(in_channels=1, n_classes=1, padding=True).cuda()
        elif nnArchitecture == 'REDNET':
            model = REDNet20(in_channels=1, num_layers=6).cuda()
        elif nnArchitecture == 'UNET_REDNET':
            model = UNet_REDNet().cuda()
        elif nnArchitecture == 'VDSR':
            model = VDSR().cuda()
        elif nnArchitecture == 'HINET':
            model = HINet(in_chn = 1).cuda()
        elif nnArchitecture == 'MPRNET':
            model = MPRNet(in_c = 1, out_c=1).cuda()
        elif nnArchitecture == 'UFORMER':
            model = Uformer(img_size=imgtransResize, in_chans=1, token_mlp='ffn').cuda()
        elif nnArchitecture == 'RESTORMER':
            model = Restormer(inp_channels=1, out_channels=1, LayerNorm_type='BiasFree').cuda()

        model = torch.nn.DataParallel(model).cuda()
        pathModel = f'results/{model_name}/model.pth.tar'
        load_pretrained(model, pathModel)

        # -------------------- SETTINGS: DATA TRANSFORMS
        transformList = []
        transformList.append(transforms.Resize(imgtransResize))
        transformList.append(transforms.ToTensor())
        # transformList.append(transforms.Normalize([0.5], [0.5]))
        transform = transforms.Compose(transformList)

        # -------------------- SETTINGS: DATASET BUILDERS
        datasetTest = DatasetGenerator(pathDirData, dataset = 'test', noise_type = noiseType, transform = transform, logger=logger)
        dataLoaderTest = DataLoader(dataset = datasetTest, batch_size = batchSize , shuffle = False, pin_memory = True)

        # -------------------- TEST
        with torch.no_grad():

            model.eval()

            label = torch.FloatTensor().cuda()
            noise = torch.FloatTensor().cuda()
            pred = torch.FloatTensor().cuda()

            img_number = 0
            for batchID, data in enumerate(tqdm(dataLoaderTest)):

                img_og, img_noise = data['original'].cuda(), data['noise'].cuda()
                img_output = model(img_noise)

                label = torch.cat((label, img_og), 0)
                noise = torch.cat((noise, img_noise), 0)

                if nnArchitecture == 'UNET' or nnArchitecture == 'REDNET' or nnArchitecture == 'UNET_REDNET'\
                    or nnArchitecture == 'VDSR' or nnArchitecture == 'UFORMER' or nnArchitecture == 'RESTORMER':
                    pred = torch.cat((pred, img_output), 0)
                elif nnArchitecture == 'HINET':
                    pred = torch.cat((pred, img_output[1]), 0)
                elif nnArchitecture == 'MPRNET':
                    pred = torch.cat((pred, img_output[0]), 0)

                for imgID in range(img_og.shape[0]):

                    save_image(img_og[imgID], f'./results/{model_name}/original/{img_number}.png')
                    save_image(img_noise[imgID], f'./results/{model_name}/noise/{img_number}.png')

                    if nnArchitecture == 'UNET' or nnArchitecture == 'REDNET' or nnArchitecture == 'UNET_REDNET'\
                        or nnArchitecture == 'VDSR' or nnArchitecture == 'UFORMER' or nnArchitecture == 'RESTORMER':
                        save_image(img_output[imgID], f'./results/{model_name}/output/{img_number}.png')
                    elif nnArchitecture == 'HINET':
                        save_image(img_output[1][imgID], f'./results/{model_name}/output/{img_number}.png')
                    elif nnArchitecture == 'MPRNET':
                        save_image(img_output[0][imgID], f'./results/{model_name}/output/{img_number}.png')

                    img_number += 1

            noisePSNR = torchPSNR(label, noise)
            predPSNR = torchPSNR(label, pred)
            print(f"Noise PSNR : {noisePSNR: .2f}")
            print(f"Pred PSNR : {predPSNR: .2f}")

            logger.info(f"Noise PSNR : {noisePSNR: .2f}")
            logger.info(f"Pred PSNR : {predPSNR: .2f}")

            run['Noise PSNR'] = f"{noisePSNR: .2f}"
            run['Pred PSNR'] = f"{predPSNR: .2f}"

            ssim = SSIM()
            noiseSSIM = torch.mean(ssim(label, noise, as_loss=False))
            predSSIM = torch.mean(ssim(label, pred, as_loss=False))

            print(f"Noise SSIM : {noiseSSIM: .3f}")
            print(f"Pred SSIM : {predSSIM: .3f}")

            logger.info(f"Noise SSIM : {noiseSSIM: .3f}")
            logger.info(f"Pred SSIM : {predSSIM: .3f}")

            run['Noise SSIM'] = f"{noiseSSIM: .3f}"
            run['Pred SSIM'] = f"{predSSIM: .3f}"

            save_results(label, noise, pred, f'./results/{model_name}/results.csv')