import requests
import argparse
import json
from pathlib import Path
from tqdm import tqdm
from 臺灣言語工具.解析整理.拆文分析器 import 拆文分析器
from 臺灣言語工具.語音合成 import 台灣話口語講法

# AllenNLP 翻譯
AllenNLP_url = "http://127.0.0.1:8000/predict"

def translate(txt):
    # 華語
    #txt = "今天天氣真好"
    req_data = {"source": txt}
    req_headers = {'Content-Type': 'application/json'}
    rsp = requests.post(AllenNLP_url, headers=req_headers, json=req_data)
    # ['kin1', 'a2', 'jit8', 'thinn1', 'khi3', 'tsin1', 'ho2']
    translated_tokens = eval(rsp.content.decode())['predicted_tokens']

    # 'kin1 a2 jit8 thinn1 khi3 tsin1 ho2'
    pinyin = ' '.join(translated_tokens)
    return pinyin

if __name__ == '__main__':
    """example usage
    python translate.py sentence "今天天氣真好"
    python translate.py file test.txt
    """
    parser = argparse.ArgumentParser(description='翻譯')
    subparsers = parser.add_subparsers(
        help='help for subcommand', dest="subcommand")
    # === subcommand: sentence ===
    parser_sentence = subparsers.add_parser('sentence', help='翻譯句子')
    parser_sentence.add_argument('sentence', help='翻譯的句子')
    # === subcommand: file ===
    parser_file = subparsers.add_parser('file', help='翻譯文件')
    parser_file.add_argument('file_in', help='翻譯的文件')
    args = parser.parse_args()

    if args.subcommand == 'sentence':
        pinyin = translate(args.sentence)
        print(pinyin)

    elif args.subcommand == 'file':
        # === read file === #
        with open(args.file_in, 'r') as f:
            txt = f.readlines()

        # === translate === #
        result = []
        for i, t in enumerate(tqdm(txt)):
            data = {}
            text = t.strip()
            pinyin = translate(t)
            data["index"] = i
            data["華語"] = text
            data["臺語"] = pinyin
            result.append(data)
        # === save file === #
        with open(Path(args.file_in).with_suffix(".json"), 'w') as f:
            json.dump(result, f, ensure_ascii=False, indent=2)
    else:
        assert False, "unknown subcommand"


# https://leetcode.com/problems/keys-and-rooms
# 841. Keys and Rooms
from collections import deque
from typing import List

def canVisitAllRooms(rooms: List[List[int]]) -> bool:
    is_locked = [True] * len(rooms)
    q = deque()
    q.append(0)
    is_locked[0] = False
    while q:
        node = q.popleft()
        for child_node in rooms[node]:
            if is_locked[child_node] == True:
                is_locked[child_node] = False
                q.append(child_node)
    return sum(is_locked) == 0

print(canVisitAllRooms([[1, 3], [3, 0, 1], [2], [0]]))


import math
import numpy as np
from typing import List, Tuple

from pyavis.shared.util import Subject
from .signal import AudioSignal

class Track:
    """
    Class representing an audio track.
    """
    def __init__(self, label: str, sampling_rate: int):
        """
        Construct a new track.

        Parameters
        ----------
        label : str
            Name of the track
        sampling_rate : int
            Sampling rate of the track
        """
        self.label = label
        self.sampling_rate = sampling_rate
        self.signals: List[Tuple[int, AudioSignal]] = []

        self.signalAdded = Subject()
        self.signalRemoved = Subject()
        self.signalMoved = Subject()

    def __getitem__(self, index):
        """
        Accessing array elements through slicing.
            * slice, range and step slicing track[4:40:2]
                * Negative indexing and missing slice values use the tracks minimum length as basis.
                    * e.g. [-1:-500:-1] and [::1]
                * Use positive indexing if you want padding
            * Time slicing (unit in seconds) using dict track[{1:2.5}]
                * Indexing from 1s to 2.5s

        Parameters
        ----------
            index : slice or list or dict
                Slicing argument.

        Returns
        -------
        numpy.ndarray
            Array containing the signal values in the range. Values between signals are 0.
        """
        if isinstance(index, slice):
            idx = index
        elif isinstance(index, dict):
            for key, val in index.items():
                try:
                    start = int(key * self.sampling_rate)
                except TypeError:
                    start = None
                try:
                    stop = int(val * self.sampling_rate)
                except TypeError:
                    stop = None
            idx = slice(start, stop, 1)
        else:
            raise TypeError("Not valid indexing")

        return self._get_signal_values(idx)

    def _get_signal_values(self, slice: slice):
        start, stop = slice.start, slice.stop

        # Calculate indicies for negative slice values
        new_start, new_stop, step = slice.indices(self.get_minimum_length())
        if start is None or start < 0:
            start = new_start
        if stop is None or stop < 0:
            stop = new_stop

        if start > stop and step > 0:
            return []
        elif start < stop and step < 0:
            return []
        elif start > stop and step < 0:
            return self._handle_indexing(stop, start, step)
        elif start < stop and step > 0:
            return self._handle_indexing(start, stop, step)
        else:
            return []

    def _handle_indexing(self, lower, higher, step):
        direction = "forward" if step > 0 else "backward"

        # Adjust size of array based on step size
        array = np.zeros(int(math.ceil(abs(higher - lower) / abs(step))))

        for pos, sig in self.signals:

            signal_start = pos
            signal_end = pos + len(sig.signal())

            if lower >= signal_end or higher <= signal_start:
                continue

            if direction == "forward":
                # Signal starts outside of range and ends in range
                if signal_start <= lower and signal_end <= higher:
                    # Divide stop index by step to allow both padding and step at the same time
                    array[0:int(math.ceil((signal_end - lower) / step))] = sig.signal()[lower - signal_start::step]

                # Signal starts in range and ends outside of range
                elif signal_start >= lower and signal_start < higher and signal_end > higher:
                    # TODO: Is divide here necessary
                    array[signal_start - lower:] = sig.signal()[0:higher - signal_end:step]

                # Range completly inside signal
                elif signal_start <= lower and signal_end >= higher:
                    array[:] = sig.signal()[lower - signal_start:higher - signal_end:step]

                # Signal lies completly in the range
                else:
                    pos_1 = signal_start - lower

                    # Step could lead to a situation where we do not start at the beginning of the array
                    # Account for that by calculation of the offset
                    padding = pos_1
                    remainder = padding % abs(step)
                    offset = step - remainder if not remainder == 0 else 0

                    values = sig.signal()[offset::step]
                    start_pos = math.ceil(pos_1 / step)

                    array[start_pos:start_pos + len(values)] = values

            elif direction == "backward":
                # Signal starts outside of range and ends in range
                if signal_start <= lower and signal_end <= higher:
                    # We can start from outside the array, we need to calculate the offset with which we enter the array
                    padding = higher - signal_end
                    remainder = padding % abs(step)
                    offset = step + remainder

                    values = sig.signal()[offset:lower - signal_start:step]
                    if not len(values) == 0:
                        array[-len(values):] = values

                # Signal starts in range and ends outside of range
                elif signal_start >= lower and signal_start < higher and signal_end > higher:
                    # Since signal ends outsider of range, and were are going "backwards"
                    # -> Start at beginng of array
                    values = sig.signal()[higher - signal_end:lower-signal_start:step]
                    array[:len(values)] = values

                # Range completly inside signal
                elif signal_start <= lower and signal_end >= higher:
                    array[:] = sig.signal()[higher - signal_end:lower - signal_start:step]

                # Signal lies completly in the range
                else:
                    # We come from 'behind' -> possible offset necessary
                    padding = higher - signal_end # come from behind
                    remainder = padding % abs(step) # calc. what remains after enough steps
                    offset = step + remainder # padding is negative so -x + y => yields offset into array

                    values = sig.signal()[offset::step]

                    start = math.ceil(-signal_start / abs(step))
                    start = start if start != 0 else None
                    end = start - len(values) if start is not None else -len(values)

                    array[end:start] = values

        return array

    def get_index(self, signal: AudioSignal) -> int:
        """
        Get index of the signal

        Parameters
        ----------
        signal : Signal
            The signal

        Returns
        -------
        int
            Index of the signal
        """
        value = next(filter(lambda item: item[1] == signal, self.signals), None)
        return self.signals.index(value)

    def get_signal(self, signal: AudioSignal) -> Tuple[int, AudioSignal] | None:
        """
        Get the signal and its position

        Parameters
        ----------
        signal : Signal
            The signal

        Returns
        -------
        Tuple[int, AudioSignal] | None
            Tuple of position and audio signal, or None if signal not in track
        """
        return next(filter(lambda item: item[1] == signal, self.signals), None)

    def get_signal_at_position(self, pos: int) -> Tuple[int, AudioSignal] | None:
        """
        Get signal at the position

        Parameters
        ----------
        pos : int
            Position of the signal

        Returns
        -------
        Tuple[int, Signal] | None
            Returns a signal, if there is one at the position
        """
        value = next(filter(lambda item: pos >= item[0] and pos < item[0] + len(item[1].signal()), self.signals), None)
        return value

    def try_add(self, pos: int, signal: AudioSignal) -> bool:
        """
        Try adding the signal at the positioin position to the track.

        Parameters
        ----------
        pos : int
            Positon of the new signal
        signal : AudioSignal
            Signal to add

        Returns
        -------
        bool
            True, if the signal does not overlap
            False, if the signal does overlap with another signal
        """
        if signal.sampling_rate() != self.sampling_rate:
            raise ValueError("Sampling rate does not match")

        if self.can_add_at(pos, signal):
            self.signals.append((pos, signal))
            self.signalAdded.emit(self, (pos, signal))
            return True
        else:
            return False

    def can_add_at(self, pos: int, signal: AudioSignal) -> bool:
        """
        Check if a signal can be added at the position

        Parameters
        ----------
        pos : int
            Positon of the new signal in the track
        signal : AudioSignal
            Signal to add
        """
        start, end = pos, pos + len(signal.signal())
        for iter_idx, (iter_pos, iter_signal) in enumerate(self.signals):
            iter_start, iter_end = iter_pos, iter_pos + len(iter_signal.signal())
            if start > iter_start and start < iter_end:
                return False
            if end > iter_start and end < iter_end:
                return False
        return True

    def remove_signal(self, signal: AudioSignal):
        """
        Remove a signal.

        Parameters
        ----------
        signal : AudioSignal
            Signal to delete

        Raises
        ------
        ValueError
            Raises if signal could not be removed
        """
        (position, signal) = self.get_signal(signal)
        success = self.remove(position, signal)
        if not success:
            raise ValueError("Could not remove signal")

        self.signalRemoved.emit(self, (position, signal))

    def remove(self, pos: int, signal: AudioSignal):
        """
        Remove the signal at the position.

        Parameters
        ----------
        pos: int
            Position of the signal
        signal: AudioSignal
            AudioSignal to delete
        """
        self.signals.remove((pos, signal))
        self.signalRemoved.emit(self, (pos, signal))

    def try_move(self, pos: int, idx: int) -> bool:
        """
        Try moving the signal to the new position.

        Parameters
        ----------
        pos : int
            New positon of the signal in the track
        idx : int
            Index of the signal

        Returns
        -------
        bool
        """
        if self.can_move_to(pos, idx):
            value = list(self.signals[idx])
            value[0] = pos
            self.signals[idx] = tuple(value)
            return True
        else:
            return False

    def can_move_to(self, pos: int, signal: AudioSignal | int) -> bool:
        """
        Check if the signal can be moved to the position.

        Parameters
        ----------
        pos : int
            New positon of the signal in the track
        idx : int | AudioSignal
            Index of the signal

        Returns
        -------
        bool
        """
        if isinstance(signal, int):
            (sig_pos, signal) = self.signals[signal]
        elif isinstance(signal, AudioSignal):
            (sig_pos, signal) = self.get_signal(signal)

        start, end = pos, pos + len(signal.signal())
        for iter_idx, (iter_pos, iter_signal) in enumerate(self.signals):
            # Ignore current signal, since it will be moved.
            if sig_pos == iter_pos:
                continue
            iter_start, iter_end = iter_pos, iter_pos + len(iter_signal.signal())
            if start > iter_start and start < iter_end:
                return False
            if end > iter_start and end < iter_end:
                return False
        return True

    def get_minimum_length(self, type="samples") -> int | float:
        """
        Get the minimum length that the track occupies

        Parameter
        ---------
        type : str
            Return type (either "samples" or "seconds")

        Returns
        -------
        int | float
            Minimum length in samples or seconds
        """
        val = max(self.signals, key=lambda x: x[0])
        if type == "samples":
            return val[0] + len(val[1].signal())
        elif type == "seconds":
            return ( val[0] + len(val[1].signal()) ) / self.sampling_rate
        else:
            raise ValueError("Not a valid argument")


import time
import argparse
from torch.autograd import Variable
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
from utils import *
from tqdm import tqdm
from model import Net

# Settings
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--device', type=str, default='cuda:0')
    parser.add_argument('--parallel', type=bool, default=False)
    parser.add_argument('--num_workers', type=int, default=8)
    parser.add_argument("--angRes", type=int, default=9, help="angular resolution")
    parser.add_argument('--model_name', type=str, default='OACC-Net')
    parser.add_argument('--trainset_dir', type=str, default='./dataset/training/')
    parser.add_argument('--validset_dir', type=str, default='./dataset/validation/')
    parser.add_argument('--patchsize', type=int, default=48)
    parser.add_argument('--batch_size', type=int, default=16)
    parser.add_argument('--lr', type=float, default=1e-3, help='initial learning rate')
    parser.add_argument('--n_epochs', type=int, default=3500, help='number of epochs to train')
    parser.add_argument('--n_steps', type=int, default=3500, help='number of epochs to update learning rate')
    parser.add_argument('--gamma', type=float, default=0.5, help='learning rate decaying factor')
    parser.add_argument('--load_pretrain', type=bool, default=False)
    parser.add_argument('--model_path', type=str, default='./log/OACC-Net.pth.tar')

    return parser.parse_args()

def train(cfg):
    if cfg.parallel:
        cfg.device = 'cuda:0'
    net = Net(cfg.angRes)
    net.to(cfg.device)
    cudnn.benchmark = True
    epoch_state = 0

    if cfg.load_pretrain:
        if os.path.isfile(cfg.model_path):
            model = torch.load(cfg.model_path, map_location={'cuda:0': cfg.device})
            net.load_state_dict(model['state_dict'], strict=False)
        else:
            print("=> no model found at '{}'".format(cfg.load_model))

    if cfg.parallel:
        net = torch.nn.DataParallel(net, device_ids=[0, 1])

    criterion_Loss = torch.nn.L1Loss().to(cfg.device)
    optimizer = torch.optim.Adam([paras for paras in net.parameters() if paras.requires_grad == True], lr=cfg.lr)
    scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=cfg.n_steps, gamma=cfg.gamma)
    scheduler._step_count = epoch_state

    loss_list = []

    for idx_epoch in range(epoch_state, cfg.n_epochs):
        train_set = TrainSetLoader(cfg)
        train_loader = DataLoader(dataset=train_set, num_workers=cfg.num_workers, batch_size=cfg.batch_size, shuffle=True)
        loss_epoch = []
        for idx_iter, (data, dispGT) in tqdm(enumerate(train_loader), total=len(train_loader)):
            data, dispGT = data.to(cfg.device), dispGT.to(cfg.device)
            disp = net(data, dispGT)
            loss = criterion_Loss(disp, dispGT[:, 0, :, :].unsqueeze(1))
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            loss_epoch.append(loss.data.cpu())

        if idx_epoch % 1 == 0:
            loss_list.append(float(np.array(loss_epoch).mean()))
            print(time.ctime()[4:-5] + ' Epoch----%5d, loss---%f' % (idx_epoch + 1, float(np.array(loss_epoch).mean())))
            if cfg.parallel:
                save_ckpt({
                'epoch': idx_epoch + 1,
                'state_dict': net.module.state_dict(),
            }, save_path='./log/', filename=cfg.model_name + '.pth')
            else:
                save_ckpt({
                    'epoch': idx_epoch + 1,
                    'state_dict': net.state_dict(),
                }, save_path='./log/', filename=cfg.model_name + '.pth')
        if idx_epoch % 10 == 9:
            if cfg.parallel:
                save_ckpt({
                    'epoch': idx_epoch + 1,
                    'state_dict': net.module.state_dict(),
                }, save_path='./log/', filename=cfg.model_name + str(idx_epoch + 1) + '.pth')
            else:
                save_ckpt({
                'epoch': idx_epoch + 1,
                'state_dict': net.state_dict(),
            }, save_path='./log/', filename=cfg.model_name + str(idx_epoch + 1) + '.pth')

            valid(net, cfg, idx_epoch + 1)

        scheduler.step()

def valid(net, cfg, epoch):

    torch.no_grad()
    scene_list = ['boxes', 'cotton', 'dino', 'sideboard']
    angRes = cfg.angRes

    txtfile = open(cfg.model_name + '_MSE100.txt', 'a')
    txtfile.write('Epoch={}:\t'.format(epoch))
    txtfile.close()

    for scenes in scene_list:
        lf = np.zeros(shape=(9, 9, 512, 512, 3), dtype=int)
        for i in range(81):
            temp = imageio.imread(cfg.validset_dir + scenes + '/input_Cam0%.2d.png' % i)
            lf[i // 9, i - 9 * (i // 9), :, :, :] = temp
            del temp
        lf = np.mean((1 / 255) * lf.astype('float32'), axis=-1, keepdims=False)
        disp_gt = np.float32(
            read_pfm(cfg.validset_dir + scenes + '/gt_disp_lowres.pfm'))  # load groundtruth disparity map
        angBegin = (9 - angRes) // 2

        lf_angCrop = lf[angBegin:  angBegin + angRes, angBegin: angBegin + angRes, :, :]

        patchsize = 128
        stride = patchsize // 2
        mini_batch = 4

        data = torch.from_numpy(lf_angCrop)
        sub_lfs = LFdivide(data.unsqueeze(2), patchsize, stride)
        n1, n2, u, v, c, h, w = sub_lfs.shape
        sub_lfs = rearrange(sub_lfs, 'n1 n2 u v c h w -> (n1 n2) u v c h w')
        num_inference = (n1 * n2) // mini_batch
        with torch.no_grad():
            out_disp = []
            for idx_inference in range(num_inference):
                current_lfs = sub_lfs[idx_inference * mini_batch: (idx_inference + 1) * mini_batch, :, :, :, :, :]
                input_data = rearrange(current_lfs, 'b u v c h w -> b c (u h) (v w)')
                out_disp.append(net(input_data.to(cfg.device)))

            if (n1 * n2) % mini_batch:
                current_lfs = sub_lfs[(idx_inference + 1) * mini_batch:, :, :, :, :, :]
                input_data = rearrange(current_lfs, 'b u v c h w -> b c (u h) (v w)')
                out_disp.append(net(input_data.to(cfg.device)))

        out_disps = torch.cat(out_disp, dim=0)
        out_disps = rearrange(out_disps, '(n1 n2) c h w -> n1 n2 c h w', n1=n1, n2=n2)
        disp = LFintegrate(out_disps, patchsize, patchsize // 2)
        disp = disp[0: data.shape[2], 0: data.shape[3]]
        disp = np.float32(disp.data.cpu())

        mse100 = np.mean((disp[11:-11, 11:-11] - disp_gt[11:-11, 11:-11]) ** 2) * 100
        txtfile = open(cfg.model_name + '_MSE100.txt', 'a')
        txtfile.write('mse_{}={:3f}\t'.format(scenes, mse100))
        txtfile.close()

    txtfile = open(cfg.model_name + '_MSE100.txt', 'a')
    txtfile.write('\n')
    txtfile.close()

    return

def save_ckpt(state, save_path='./log', filename='checkpoint.pth.tar'):
    torch.save(state, os.path.join(save_path,filename))

if __name__ == '__main__':
    cfg = parse_args()
    train(cfg)


from pyspark.sql import SparkSession
import math
from pyspark.sql.types import StringType, DoubleType

spark = SparkSession.builder.getOrCreate()
ct_df = spark.read.parquet("/user/yz8759_nyu_edu/project/datasets/cell_tower_us_lte_clustered").na.fill(0)

ct_df.createOrReplaceTempView("cell_tower")

points_df = spark.read.option("header", True).option("inferSchema", True).csv("/user/yz8759_nyu_edu/project/datasets/stage2").drop("score").drop("n").drop("d").na.fill(0)
points_df.createOrReplaceTempView("points")

def add_score(lon1, lat1, lon2, lat2):
    if lon2 is None or lat2 is None:
        return 0
    distance = math.sqrt((lon1-lon2)**2 + (lat1-lat2)**2)
    return 1 / distance**2

spark.udf.register("add_score", add_score, DoubleType())

result_temp = spark.sql("""
                    SELECT points.lon AS points_lon, points.lat AS points_lat, SUM(add_score(points.lon, points.lat, cell_tower.lon, cell_tower.lat)) AS score
                    FROM points
                    LEFT JOIN cell_tower ON SQRT(POW(cell_tower.lon - points.lon, 2) + POW(cell_tower.lat - points.lat, 2)) < 0.4
                    GROUP BY points.lon, points.lat
                  """)
result_temp.createOrReplaceTempView("result_temp")

result = spark.sql("""
                    SELECT *
                    FROM result_temp
                    LEFT JOIN points ON result_temp.points_lon = points.lon AND result_temp.points_lat = points.lat
                   """)

result2 = result.drop("points_lon").drop("points_lat")

result2.write.format("parquet").mode("overwrite").save("/user/yz8759_nyu_edu/project/datasets/stage2-with-new-score")


# coding: utf-8

from dataclasses import make_dataclass
from learn_report.module.describe.type_hints import TABLE_DATA_TYPE, TABLE_MASK_TYPE

TableDesc = make_dataclass(
    'TableDesc',
    [
        ('data', TABLE_DATA_TYPE),
        ('mask', TABLE_MASK_TYPE)
    ],
    frozen=True
)


"""Setup script for psnapshot."""

from setuptools import setup, find_packages
from setuptools.command.test import test as TestCommand
import sys

class PyTestCommand(TestCommand):
    user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.pytest_args = []

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest

        errno = pytest.main(self.pytest_args)
        sys.exit(errno)

setup(
    name='psnapshot',
    version='0.1.1',
    packages=find_packages(),
    entry_points={'console_scripts': ['psnapshot = psnapshot.control:main']},
    #install_requires=install_requires,
    tests_require=[
        'pytest'
    ],
    cmdclass={'test': PyTestCommand},
    url='https://github.com/moltob/psnapshot',
    license='',
    author='Mike Pagel',
    author_email='mike@mpagel.de',
    description='Python implementation rsnapshot-like hardlink based backups.'
)


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

import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as mlp
from sklearn.linear_model import LinearRegression

data= pd.read_csv("cost_revenue_clean.csv")
des_data= data.describe()
data["val"]=str
data["val"][0]="o"
print(data)

des_data

X= DataFrame(data, columns=["production_budget_usd"])
y= DataFrame(data, columns=["worldwide_gross_usd"])

mlp.figure(figsize=(10,6))
mlp.scatter(X,y)

mlp.ylim(0,3000000000)
mlp.xlim(0,450000000)
mlp.show()

regression = LinearRegression()
regression.fit(X,y)
regression.intercept_
regression.coef_

mlp.figure(figsize=(10,6))
mlp.scatter(X,y, alpha= 0.3)
mlp.plot(X, regression.predict(X), color= "red", linewidth=3)

mlp.ylim(0,3000000000)
mlp.xlim(0,450000000)
mlp.show()

data= pd.read_csv("lsd_math_score_data.csv")
print(data)

LSD= data[["LSD_ppm"]]
score=data[["Avg_Math_Test_Score"]]
time= data[["Time_Delay_in_Minutes"]]
mlp.plot( time,LSD, color= "#986D8E", linewidth= 4)
mlp.title=("Tissue concentration over time")
mlp.xlabel= "PPM"
mlp.ylabel= "minutes"
mlp.style.use("classic")

rgrn= LinearRegression()
rgrn.fit(LSD,score)
coef=rgrn.coef_[0][0]
intercept=rgrn.intercept_[0]
rgrn.score(LSD,score)
mlp.style.use("fivethirtyeight")
mlp.scatter(LSD,score, alpha= 0.7, color= 'blue',s=100)
predicted_score= rgrn.predict(LSD)
mlp.plot(LSD,predicted_score)
mlp.show()

data["Test_subject"]= "J.Lopez"
print(data)

clean_data= data[["Avg_Math_Test_Score", "LSD_ppm"]]

type(clean_data)

X= data[["LSD_ppm"]]
type(X)

del data["Time_Delay_in_Minutes"]
print(data)

import math
print(math.pi)
math.e


from datetime import datetime, timedelta
from typing import Optional, Union

import jwt
from django.conf import settings
from django.http import HttpResponse

from .models import SubscriptionEmail

def generate_confirmation_token(email: str) -> str:
    payload = {
        'user_email': email,
        'exp': datetime.utcnow() + timedelta(minutes=30),
        'iat': datetime.utcnow()
    }

    # Generates a web token in json format with expiry of 30 minutes
    return jwt.encode(payload, settings.SECRET_KEY, algorithm='HS256')

def verify_token(token: str) -> Optional[Union[HttpResponse, SubscriptionEmail]]:
    try:
        # deocodes the the token generated for confirmation link
        payload = jwt.decode(
            token, settings.SECRET_KEY, algorithms=['HS256'])

    except jwt.ExpiredSignatureError:
        return HttpResponse("Link Expired!")

    # verifies the user from the decoded token
    user = SubscriptionEmail.objects.filter(
        email=payload.get('user_email')).first()

    return (user, None)

def send_confirmation_link(email: str, **kwargs) -> None:
    from django.core.mail import EmailMessage
    from django.template.loader import get_template

    token = generate_confirmation_token(email)
    template = get_template('newsletter/confirm.html')
    subject = 'Confirm Your Subscription'
    _from = 'noreply@djangonewsletter.com'
    context = {
        'email': email,
        'token': token,
        'protocol': kwargs['scheme'],
        'domain': kwargs['host']
    }

    # pass the domain and protocol context from request &
    # other required credentials while sending email
    content = template.render(context)
    msg = EmailMessage(subject, content, _from, to=[email])
    msg.send()

def send_already_exist_email(email: str) -> None:
    from django.core.mail import send_mail

    subject = 'Already Subscribed!'
    content = 'You are already our member!'
    _from = 'noreply@djangonewsletter.com'

    send_mail(subject, content, _from, [email], fail_silently=False)


import requests

getURL = 'http://127.0.0.1:5000/getData'
postURL = 'http://127.0.0.1:5000/addData'
flag = True

Payload = {
    "Contact": "",
    "Name": ""
}

print("Choose method:")
print("[1]GET")
print("[2]POST")

while flag == True:
    method = int(input("Choose appropriate number: "))

    if method == 1:
        flag = False
        response = requests.request("GET", getURL)
        print(response.text)
    elif method == 2:
        flag = False
        cont = input("Contact: ")
        name = input("Name: ")
        Payload["Contact"] = cont
        Payload["Name"] = name
        response = requests.request("POST", postURL, json=Payload)
        print(response.text)
    else:
        print("Error!")
        print("Choose again.")


from __future__ import unicode_literals
import simplejson as json
import os

from flask import render_template, request, current_app, \
    url_for, Response
from flask_babelex import gettext as _
from flask_security import login_required, current_user
from pgadmin.misc.bgprocess.processes import BatchProcess, IProcessDesc
from pgadmin.utils import PgAdminModule, get_storage_directory, html, \
    fs_short_path, document_dir
from pgadmin.utils.ajax import make_json_response, bad_request

from config import PG_DEFAULT_DRIVER
from pgadmin.model import Server

# set template path for sql scripts
MODULE_NAME = 'backup'
server_info = {}

class BackupModule(PgAdminModule):
    """
    class BackupModule(Object):

        It is a utility which inherits PgAdminModule
        class and define methods to load its own
        javascript file.
    """

    LABEL = _('Backup')

    def get_own_javascripts(self):
        """"
        Returns:
            list: js files used by this module
        """
        return [{
            'name': 'pgadmin.tools.backup',
            'path': url_for('backup.index') + 'backup',
            'when': None
        }]

    def show_system_objects(self):
        """
        return system preference objects
        """
        return self.pref_show_system_objects

    def get_exposed_url_endpoints(self):
        """
        Returns:
            list: URL endpoints for backup module
        """
        return ['backup.create_server_job', 'backup.create_object_job']

# Create blueprint for BackupModule class
blueprint = BackupModule(
    MODULE_NAME, __name__, static_url_path=''
)

class BACKUP(object):
    """
    Constants defined for Backup utilities
    """
    GLOBALS = 1
    SERVER = 2
    OBJECT = 3

class BackupMessage(IProcessDesc):
    """
    BackupMessage(IProcessDesc)

    Defines the message shown for the backup operation.
    """

    def __init__(self, _type, _sid, _bfile, *_args, **_kwargs):
        self.backup_type = _type
        self.sid = _sid
        self.bfile = _bfile
        self.database = _kwargs['database'] if 'database' in _kwargs else None
        self.cmd = ''

        def cmdArg(x):
            if x:
                x = x.replace('\\', '\\\\')
                x = x.replace('"', '\\"')
                x = x.replace('""', '\\"')
                return ' "' + x + '"'
            return ''

        for arg in _args:
            if arg and len(arg) >= 2 and arg[:2] == '--':
                self.cmd += ' ' + arg
            else:
                self.cmd += cmdArg(arg)

    def get_server_details(self):
        # Fetch the server details like hostname, port, roles etc
        s = Server.query.filter_by(
            id=self.sid, user_id=current_user.id
        ).first()

        from pgadmin.utils.driver import get_driver
        driver = get_driver(PG_DEFAULT_DRIVER)
        manager = driver.connection_manager(self.sid)

        host = manager.local_bind_host if manager.use_ssh_tunnel else s.host
        port = manager.local_bind_port if manager.use_ssh_tunnel else s.port

        return s.name, host, port

    @property
    def message(self):
        name, host, port = self.get_server_details()

        if self.backup_type == BACKUP.OBJECT:
            return _(
                "Backing up an object on the server '{0}' "
                "from database '{1}'..."
            ).format(
                "{0} ({1}:{2})".format(
                    name, host, port
                ),
                self.database
            )
        if self.backup_type == BACKUP.GLOBALS:
            return _("Backing up the global objects on "
                     "the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    name, host, port
                )
            )
        elif self.backup_type == BACKUP.SERVER:
            return _("Backing up the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    name, host, port
                )
            )
        else:
            # It should never reach here.
            return "Unknown Backup"

    def details(self, cmd, args):
        name, host, port = self.get_server_details()

        res = '
'

        if self.backup_type == BACKUP.OBJECT:
            res += _(
                "Backing up an object on the server '{0}' "
                "from database '{1}'..."
            ).format(
                "{0} ({1}:{2})".format(
                    html.safe_str(name),
                    html.safe_str(host),
                    html.safe_str(port),
                ),
                html.safe_str(self.database)
            )
        elif self.backup_type == BACKUP.GLOBALS:
            res += _("Backing up the global objects on "
                     "the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    html.safe_str(name),
                    html.safe_str(host),
                    html.safe_str(port)
                )
            )
        elif self.backup_type == BACKUP.SERVER:
            res += _("Backing up the server '{0}'...").format(
                "{0} ({1}:{2})".format(
                    html.safe_str(name),
                    html.safe_str(host),
                    html.safe_str(port)
                )
            )
        else:
            # It should never reach here.
            res += "Backup"

        res += '

'
        res += _("Running command:")
        res += '
'
        res += html.safe_str(cmd + self.cmd)
        res += '
'

        return res

@blueprint.route("/")
@login_required
def index():
    return bad_request(errormsg=_("This URL cannot be called directly."))

@blueprint.route("/backup.js")
@login_required
def script():
    """render own javascript"""
    return Response(
        response=render_template(
            "backup/js/backup.js", _=_
        ),
        status=200,
        mimetype="application/javascript"
    )

def filename_with_file_manager_path(_file):
    """
    Args:
        file: File name returned from client file manager

    Returns:
        Filename to use for backup with full path taken from preference
    """
    # Set file manager directory from preference
    storage_dir = get_storage_directory()

    if storage_dir:
        _file = os.path.join(storage_dir, _file.lstrip(u'/').lstrip(u'\\'))
    elif not os.path.isabs(_file):
        _file = os.path.join(document_dir(), _file)

    # Touch the file to get the short path of the file on windows.
    with open(_file, 'a'):
        pass

    return fs_short_path(_file)

@blueprint.route(
    '/job/<int:sid>', methods=['POST'], endpoint='create_server_job'
)
@login_required
def create_backup_job(sid):
    """
    Args:
        sid: Server ID

        Creates a new job for backup task (Backup Server/Globals)

    Returns:
        None
    """
    if request.form:
        # Convert ImmutableDict to dict
        data = dict(request.form)
        data = json.loads(data['data'][0], encoding='utf-8')
    else:
        data = json.loads(request.data, encoding='utf-8')

    try:
        backup_file = filename_with_file_manager_path(data['file'])
    except Exception as e:
        return bad_request(errormsg=str(e))

    # Fetch the server details like hostname, port, roles etc
    server = Server.query.filter_by(
        id=sid, user_id=current_user.id
    ).first()

    if server is None:
        return make_json_response(
            success=0,
            errormsg=_("Could not find the specified server.")
        )

    # To fetch MetaData for the server
    from pgadmin.utils.driver import get_driver
    driver = get_driver(PG_DEFAULT_DRIVER)
    manager = driver.connection_manager(server.id)
    conn = manager.connection()
    connected = conn.connected()

    if not connected:
        return make_json_response(
            success=0,
            errormsg=_("Please connect to the server first.")
        )

    utility = manager.utility('backup_server')

    args = [
        '--file',
        backup_file,
        '--host',
        manager.local_bind_host if manager.use_ssh_tunnel else server.host,
        '--port',
        str(manager.local_bind_port) if manager.use_ssh_tunnel
        else str(server.port),
        '--username',
        server.username,
        '--no-password',
        '--database',
        server.maintenance_db
    ]

    if 'role' in data and data['role']:
        args.append('--role')
        args.append(data['role'])
    if 'verbose' in data and data['verbose']:
        args.append('--verbose')
    if 'dqoute' in data and data['dqoute']:
        args.append('--quote-all-identifiers')
    if data['type'] == 'global':
        args.append('--globals-only')

    try:
        p = BatchProcess(
            desc=BackupMessage(
                BACKUP.SERVER if data['type'] != 'global' else BACKUP.GLOBALS,
                sid,
                data['file'].encode('utf-8') if hasattr(
                    data['file'], 'encode'
                ) else data['file'],
                *args
            ),
            cmd=utility, args=args
        )
        manager.export_password_env(p.id)
        # Check for connection timeout and if it is greater than 0 then
        # set the environment variable PGCONNECT_TIMEOUT.
        if manager.connect_timeout > 0:
            env = dict()
            env['PGCONNECT_TIMEOUT'] = str(manager.connect_timeout)
            p.set_env_variables(server, env=env)
        else:
            p.set_env_variables(server)

        p.start()
        jid = p.id
    except Exception as e:
        current_app.logger.exception(e)
        return make_json_response(
            status=410,
            success=0,
            errormsg=str(e)
        )
    # Return response
    return make_json_response(
        data={'job_id': jid, 'success': 1}
    )

@blueprint.route(
    '/job/<int:sid>/object', methods=['POST'], endpoint='create_object_job'
)
@login_required
def create_backup_objects_job(sid):
    """
    Args:
        sid: Server ID

        Creates a new job for backup task
        (Backup Database(s)/Schema(s)/Table(s))

    Returns:
        None
    """
    if request.form:
        # Convert ImmutableDict to dict
        data = dict(request.form)
        data = json.loads(data['data'][0], encoding='utf-8')
    else:
        data = json.loads(request.data, encoding='utf-8')

    # Remove ratio from data in case of empty string
    if 'ratio' in data and data['ratio'] == '':
        data.pop("ratio")

    try:
        backup_file = filename_with_file_manager_path(data['file'])
    except Exception as e:
        return bad_request(errormsg=str(e))

    # Fetch the server details like hostname, port, roles etc
    server = Server.query.filter_by(
        id=sid, user_id=current_user.id
    ).first()

    if server is None:
        return make_json_response(
            success=0,
            errormsg=_("Could not find the specified server.")
        )

    # To fetch MetaData for the server
    from pgadmin.utils.driver import get_driver
    driver = get_driver(PG_DEFAULT_DRIVER)
    manager = driver.connection_manager(server.id)
    conn = manager.connection()
    connected = conn.connected()

    if not connected:
        return make_json_response(
            success=0,
            errormsg=_("Please connect to the server first.")
        )

    utility = manager.utility('backup')
    args = [
        '--file',
        backup_file,
        '--host',
        manager.local_bind_host if manager.use_ssh_tunnel else server.host,
        '--port',
        str(manager.local_bind_port) if manager.use_ssh_tunnel
        else str(server.port),
        '--username',
        server.username,
        '--no-password'
    ]

    def set_param(key, param):
        if key in data and data[key]:
            args.append(param)

    def set_value(key, param, value):
        if key in data:
            if value:
                if value is True and data[key]:
                    args.append(param)
                    args.append(data[key])
                else:
                    args.append(param)
                    args.append(value)

    set_param('verbose', '--verbose')
    set_param('dqoute', '--quote-all-identifiers')
    set_value('role', '--role', True)
    if data['format'] is not None:
        if data['format'] == 'custom':
            args.extend(['--format=c'])

            set_param('blobs', '--blobs')
            set_value('ratio', '--compress', True)

        elif data['format'] == 'tar':
            args.extend(['--format=t'])

            set_param('blobs', '--blobs')

        elif data['format'] == 'plain':
            args.extend(['--format=p'])
            if 'only_data' in data and data['only_data']:
                args.append('--data-only')
                set_param('disable_trigger', '--disable-triggers')
            else:
                set_param('only_schema', '--schema-only')
                set_param('dns_owner', '--no-owner')
                set_param('include_create_database', '--create')
                set_param('include_drop_database', '--clean')
        elif data['format'] == 'directory':
            args.extend(['--format=d'])

    set_param('pre_data', '--section=pre-data')
    set_param('data', '--section=data')
    set_param('post_data', '--section=post-data')
    set_param('dns_privilege', '--no-privileges')
    set_param('dns_tablespace', '--no-tablespaces')
    set_param('dns_unlogged_tbl_data', '--no-unlogged-table-data')
    set_param('use_insert_commands', '--inserts')
    set_param('use_column_inserts', '--column-inserts')
    set_param('disable_quoting', '--disable-dollar-quoting')
    set_param('with_oids', '--oids')
    set_param('use_set_session_auth', '--use-set-session-authorization')

    set_value('encoding', '--encoding', True)
    set_value('no_of_jobs', '--jobs', True)

    for s in data['schemas']:
        args.extend(['--schema', s])

    for s, t in data['tables']:
        args.extend([
            '--table', driver.qtIdent(conn, s, t)
        ])

    args.append(data['database'])

    try:
        p = BatchProcess(
            desc=BackupMessage(
                BACKUP.OBJECT, sid,
                data['file'].encode('utf-8') if hasattr(
                    data['file'], 'encode'
                ) else data['file'],
                *args,
                database=data['database']
            ),
            cmd=utility, args=args
        )
        manager.export_password_env(p.id)
        # Check for connection timeout and if it is greater than 0 then
        # set the environment variable PGCONNECT_TIMEOUT.
        if manager.connect_timeout > 0:
            env = dict()
            env['PGCONNECT_TIMEOUT'] = str(manager.connect_timeout)
            p.set_env_variables(server, env=env)
        else:
            p.set_env_variables(server)

        p.start()
        jid = p.id
    except Exception as e:
        current_app.logger.exception(e)
        return make_json_response(
            status=410,
            success=0,
            errormsg=str(e)
        )

    # Return response
    return make_json_response(
        data={'job_id': jid, 'Success': 1}
    )


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

import os
import re
import sys
import logging
import argparse

from scipy.signal import find_peaks

LOG = logging.getLogger(__name__)

__version__ = "1.1.0"
__author__ = ("Xingguo Zhang",)
__email__ = "invicoun@foxmail.com"
__all__ = []

def read_depth(file):
    '''Read depth file'''

    for line in open(file, 'r'):
        line = line.strip()

        if not line or line.startswith("#"):
            continue

        line = line.split('\t')

        yield float(line[3]), float(line[4])

def stat_hist(data, bin=50):

    data = sorted(data)
    maxdata = int(max(data))
    mindata = int(min(data))
    step = int((maxdata-mindata)/bin)
    r = {}

    if step >=10:
        step = 10
    elif step < 1:
        step = 1
    else:
        pass
    for i in range(mindata, maxdata, step):
        for j in data:
            if j >= i and  j< (i+step):
                k = i+step
                if k not in r:
                    r[k] = 0
                r[k] += 1
            if j >= (i+step):
                break
            else:
                continue
    return r

def find_peak(data):

    x = list(data.keys())
    y = list(data.values())
    peaks, pros = find_peaks(y, prominence=10, width=4)

    xpeak = []
    ypeak = []
    for i in peaks:
        xpeak.append(x[i])
        ypeak.append(y[i])

    maxy = max(ypeak)

    peakx = []
    temp = 0
    for i in range(len(ypeak)):
        if ypeak[i] >= maxy/3:
            peakx.append(xpeak[i])

    return peakx

def find_peaked(file, bin=50):

    gcs = []
    depths = []
    for gc, depth in read_depth(file):
        gcs.append(gc)
        depths.append(depth)

    gcdict = stat_hist(gcs, bin)
    depthdict = stat_hist(depths, bin)
    try:
       gcpeak = find_peak(gcdict)
    except:
       gcpeak = [sum(gcs)/len(gcs)]

    try:
       depthpeak = find_peak(depthdict)
    except:
       depthpeak = [sum(depths)/len(depths)]

    if len(gcpeak) ==1 and len(depthpeak)==1:
        print("样本基因组GC-Depth无明显分离聚团现象，GC含量在{gc:.2f}%左右，\
样本的Depth在{depth:,.2f}X左右，样本无明显污染可以进行后续分析".format(
              gc=gcpeak[0], depth=depthpeak[0])
        )
    elif len(gcpeak) == 1:
        print("样本基因组GC-Depth有明显分离聚团现象，GC含量无明显分离现象，\
GC含量在{gc:.2f}%左右，样本Depth有明显分离，样本可能为高杂合".format(
               gc=gcpeak[0])
        )
    else:
        print("样本基因组GC-Depth有明显分离聚团现象，GC含量有明显分离现象，\
样本Depth有明显分离，样本可能含有外源的污染")

    return gcpeak, depthpeak

def add_hlep_args(parser):

    parser.add_argument("gc_depth", metavar="FILE", type=str,
        help="Input the genome GC depth.")
    parser.add_argument("-b","--bin", metavar="INT", type=int, default=50,
        help='Data segmentation bin, default=50.')

    return parser

def main():

    logging.basicConfig(
        stream=sys.stderr,
        level=logging.INFO,
        format="[%(levelname)s] %(message)s"
    )
    parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
    description='''
name:
    find_peak.py Discover the GC and deep peaks of the genome

attention:
    find_peak.py genome.fasta stat_gc_depth.tsv >gc_depth_describe.txt
version: %s
contact:  %s <%s>\
        ''' % (__version__, ' '.join(__author__), __email__))

    args = add_hlep_args(parser).parse_args()

    find_peaked(args.gc_depth, args.bin)

if __name__ == "__main__":

    main()


from flask import render_template, jsonify, request
import requests
from models.ebay_request_history_model import srcape_request_data
import re
from bs4 import BeautifulSoup
from threading import Thread

def ebay_scrape_data():
    if request.method == 'POST':
        location_val = request.form.getlist('location')
        condition_val = request.form.getlist('condition[]')
        buy_format_val = request.form.getlist('buy_format')
        min_val = request.form.get('min')
        max_val = request.form.get('max')
        item_sold_val = request.form.get('item_sold')
        search_box = request.form.get('searchtext')
        for location_val in location_val:
            location_val = location_val
        for buy_format_val in buy_format_val:
            buy_format_val = buy_format_val
        condition_list = []
        if len(condition_val) > 1:
            for condition_val in condition_val:
                condition_val = '%7C'+condition_val
                condition_list.append(condition_val)
            item = ''.join(str(item) for item in condition_list)
            url = f"https://www.ebay.com/sch/i.html?&_nkw={search_box}&_sacat=&_ipg=60rt=nc&LH_PrefLoc={location_val}&{buy_format_val}&rt=nc&_udlo={min_val}&_udhi={max_val}&LH_ItemCondition={item}&{item_sold_val}&rt=nc"

        else:
            for condition_val in condition_val:
                condition_val = condition_val
                url = f"https://www.ebay.com/sch/i.html?&_nkw={search_box}&_sacat=&_ipg=60rt=nc&LH_PrefLoc={location_val}&{buy_format_val}&rt=nc&_udlo={min_val}&_udhi={max_val}&LH_ItemCondition={condition_val}&{item_sold_val}&rt=nc"

        # print(search_box)
        # print(location_val, condition_val, buy_formate_val,
        #       min_val, max_val, item_sold_val)
        print(url)
        get_request = requests.get(url)
        get_request_text = get_request.text
        soup = BeautifulSoup(get_request_text, 'html.parser')
        results = soup.find_all('div', {'class': 's-item__wrapper clearfix'})
        data = []
        location = ""
        location_href = ""
        for item in results:
            title = item.find('div', {'class': 's-item__info clearfix'}
                              ).find('div', {'class': 's-item__title'}).find('span').text
            img = item.find(
                'div', {'class': 's-item__image-wrapper image-treatment'}).find('img')['src']
            condition = item.find('span', {'class': 'SECONDARY_INFO'}).text
            price = item.find('span', {'class': 's-item__price'}).text
            details_page_link = item.find(
                'a', {'class': 's-item__link'})['href']
            get_request_details = requests.get(details_page_link)
            soup_details = BeautifulSoup(
                get_request_details.text, 'html.parser')
            location_page_link = soup_details.find(
                'div', {'class': 'ux-seller-section__item--seller'})
            if location_page_link:
                location_href = location_page_link.find('a')['href']
                get_location_page = requests.get(f"{location_href}#tab1")
                soup_location = BeautifulSoup(
                    get_location_page.text, 'html.parser')
                section = soup_location.find(
                    'section', {'class': 'str-about-description__seller-info'})
                if section:
                    section_span = section.find(
                        'span', {'class': 'str-text-span BOLD'}).text
                    if section_span == "United States":
                        location = section_span

            if location == "United States":
                location = location

                print(location, title, details_page_link, location_href)
                data.append({
                    'title': title,
                    'img': img,
                    'condition': condition,
                    'price': price,
                    'location': location,
                })
            response = {
                'aaData': data
            }
            # print(response)
        return jsonify(response)

    return render_template('tables.html')

def ebay_scrape_request_history():
    if request.method == 'POST':
        location_url_val = request.form.getlist('location_url_val')
        location_text = request.form.getlist('location_text')
        exclude_location_val = request.form.get('exclude_location')
        condition_url_val = request.form.getlist('condition_url_val[]')
        condition_text = request.form.get('condition_text')
        buy_format_url = request.form.getlist('buy_format_url_val')
        buy_format_text = request.form.get('buy_format')
        min_price = request.form.get('min')
        max_price = request.form.get('max')
        sold_item_val = request.form.get('item_sold')
        search_text_val = request.form.get('searchtext')
        for location_url_val in location_url_val:
            location_url_val = location_url_val
        for condition_url_val in condition_url_val:
            condition_url_val = condition_url_val
        for buy_format_url in buy_format_url:
            buy_format_url = buy_format_url

        exclude_location_val = exclude_location_val.strip()

        srcape_request_data.put_request_history(
            request_name=search_text_val, location_url_val=location_url_val, location_text=location_text, exclude_location=exclude_location_val, condition_url_val=condition_url_val, buy_format_url_val=buy_format_url, buy_format_text=buy_format_text, min_price=min_price, max_price=max_price, sold_item=sold_item_val, condition_text=condition_text)

    return 'OK'

def get_ebay_request_history():
    data = srcape_request_data.get_request_history()
    return jsonify(data)

def request_history():
    return render_template('ebayscraphistory.html')


from flask import Flask, jsonify, request
from flask_migrate import Migrate
from models import db, Restaurant, RestaurantPizza, Pizzas

# Create instances of Pizzas, Restaurant, and RestaurantPizza
pizza2 = Pizzas(name="Normal", ingredients="Dough, Tomato, Cheese, Oil")
rest2 = Restaurant(name="Road Inn", address="123 Jogoo Road")
restaurant_pizza1 = RestaurantPizza(restaurant_id=1, pizza_id=1)
restaurant_pizza2 = RestaurantPizza(restaurant_id=1, pizza_id=2)

# Initialize the Flask app
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# Initialize the Flask-Migrate extension
migrate = Migrate(app, db)

# Initialize the SQLAlchemy database
db.init_app(app)

# Define the index route
@app.route('/')
def index():
    return "
Hello World
"

# Define a route to get all restaurants
@app.route('/restaurants')
def get_all_restaurants():
    restaurants = Restaurant.query.all()
    restaurant_list = []

    for restaurant in restaurants:
        restaurant_data = {
            'id': restaurant.id,
            'name': restaurant.name,
            'address': restaurant.address  # Added 'address' field
        }
        restaurant_list.append(restaurant_data)

    return jsonify(restaurant_list)

# Define a route to get all pizzas
@app.route('/pizzas')
def get_all_pizzas():
    pizzas = Pizzas.query.all()
    pizzas_list = []

    for pizza in pizzas:
        pizza_data = {
            'id': pizza.id,
            'name': pizza.name,
            'ingredients': pizza.ingredients
        }
        pizzas_list.append(pizza_data)

    return jsonify(pizzas_list)

# Define a route to add RestaurantPizza entries (associations between restaurants and pizzas)
@app.route('/restaurant_pizzas', methods=['POST'])
def add_RestaurantPizzas():
    new_RestaurantPizza = RestaurantPizza(
        restaurant_id=request.form.get("restaurant_id"),
        pizza_id=request.form.get("pizza_id")
    )
    db.session.add(new_RestaurantPizza)
    db.session.commit()

    # Retrieve the added pizza's information
    pizza = Pizzas.query.filter(Pizzas.id == request.form.get('pizza_id')).first()

    pizza_data = {
        'id': pizza.id,
        'name': pizza.name,
        'ingredients': pizza.ingredients
    }

    return jsonify(pizza_data), 201  # Return the pizza data and a 201 status code

# Define a route to get restaurant information by ID and optionally delete a restaurant
@app.route('/restaurants/<int:id>', methods=['GET', 'DELETE'])
def get_restaurant_by_id(id):
    restaurant = Restaurant.query.filter(Restaurant.id == id).first()

    if request.method == 'GET':
        if restaurant is None:
            return jsonify({"error": "Restaurant not found"}), 404

        restaurant_data = {
            'id': restaurant.id,
            'name': restaurant.name,
            'address': restaurant.address
        }

        RestaurantPizzas = RestaurantPizza.query.filter(RestaurantPizza.restaurant_id == id).all()
        pizza_list = []

        for restaurantPizza in RestaurantPizzas:
            pizza = Pizzas.query.filter(Pizzas.id == restaurantPizza.pizza_id).first()
            if pizza:
                pizza_data = {
                    'id': pizza.id,
                    'name': pizza.name,
                    'ingredients': pizza.ingredients,
                }
                pizza_list.append(pizza_data)

        restaurant_data['pizzas'] = pizza_list

        return jsonify(restaurant_data)

    elif request.method == 'DELETE':
        db.session.delete(restaurant)
        db.session.commit()

        response_body = {}
        response = jsonify(response_body), 200

        return response

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


"""Moira list utility functions"""
import os
import re
from collections import namedtuple

from django.conf import settings
from django.contrib.auth.models import User
from django.db import transaction
from mit_moira import Moira

from moira_lists.exceptions import MoiraException
from moira_lists.models import MoiraList

MoiraUser = namedtuple("MoiraUser", "username type")

def get_moira_client():
    """
    Gets a moira client.

    Returns:
        Moira: A moira client
    """

    _check_files_exist(
        [settings.MIT_WS_CERTIFICATE_FILE, settings.MIT_WS_PRIVATE_KEY_FILE]
    )
    try:
        return Moira(settings.MIT_WS_CERTIFICATE_FILE, settings.MIT_WS_PRIVATE_KEY_FILE)
    except Exception as exc:  # pylint: disable=broad-except
        raise MoiraException(
            "Something went wrong with creating a moira client"
        ) from exc

def _check_files_exist(paths):
    """Checks that files exist at given paths."""
    errors = []
    for path in paths:
        if not os.path.isfile(path):
            errors.append("File missing: expected path '{}'".format(path))
    if errors:
        raise RuntimeError("\n".join(errors))

def get_moira_user(user):
    """
    Return the most likely username & type (USER, STRING) for a user in moira lists based on email.
    If the email ends with 'mit.edu', assume kerberos id = email prefix
    Otherwise use the entire email address as the username.

    Args:
        user (django.contrib.auth.User): the Django user to return a Moira user for.

    Returns:
        MoiraUser: A namedtuple containing username and type
    """
    if re.search(r"(@|\.)mit.edu$", user.email):
        return MoiraUser(user.email.split("@")[0], "USER")
    return MoiraUser(user.email, "STRING")

def query_moira_lists(user):
    """
    Get a set of all moira lists (including nested lists) a user has access to, by querying the Moira service.

    Args:
        user (django.contrib.auth.User): the Django user.

    Returns:
        list_names(list of str): A list of names of moira lists which contain the user as a member.
    """
    moira_user = get_moira_user(user)
    moira = get_moira_client()
    try:
        list_infos = moira.user_list_membership(
            moira_user.username, moira_user.type, max_return_count=100_000
        )
        list_names = [
            list_info["listName"] for list_info in list_infos if list_info["listName"]
        ]
        return list_names
    except Exception as exc:  # pylint: disable=broad-except
        if "java.lang.NullPointerException" in str(exc):
            # User is not a member of any moira lists, so ignore exception and return empty list
            return []
        raise MoiraException(
            "Something went wrong with getting moira-lists for %s" % user.username
        ) from exc

def user_moira_lists(user):
    """
    Get a list of all the moira lists a user has access to

    Args:
        user (django.contrib.auth.User): the Django user.

    Returns:
        list_names(set): An set containing all known lists the user belongs to,
            including ancestors of nested lists.
    """
    if user.is_anonymous:
        return []
    list_names = set(query_moira_lists(user))
    return list_names

def moira_user_emails(member_list):
    """
    Transform a list of moira list members to emails.
    Assumes kerberos id => <kerberos_id>@mit.edu

    Args:
        member_list (list of str): List of members returned by Moira

    Returns:
        list of str: Member emails in list
    """
    return list(
        map(
            lambda member: member if "@" in member else f"{member}@mit.edu",
            filter(None, member_list),
        )
    )

@transaction.atomic
def update_user_moira_lists(user):
    """
    Add moira lists the user is a member of, remove any expired lists

    Args:
        user (User): user to update moira lists for
    """
    moira_lists = query_moira_lists(user)

    for list_name in moira_lists:
        moira_list, _ = MoiraList.objects.get_or_create(name=list_name)
        moira_list.users.add(user)

    user.moira_lists.set(MoiraList.objects.filter(name__in=moira_lists))

def get_list_members(moira_name):
    """
    Get all the members of a moira list

    Args:
        moira_name (str): Moira list name

    Returns:
        list of str: moira list members

    """
    moira_client = get_moira_client()
    members = moira_client.list_members(moira_name, type="USER")
    members.extend(moira_client.list_members(moira_name, type="STRING"))
    return members

@transaction.atomic
def update_moira_list_users(moira_list):
    """
    Update the users in a moira list

    Args:
        moira_list (MoiraList): the moira list
    """
    users = User.objects.filter(
        email__in=moira_user_emails(get_list_members(moira_list.name))
    )
    moira_list.users.set(users)

def is_public_list_editor(user):
    """
    Determine if a user can author public user lists & paths

    Args:
        user (User): a user

    Returns:
        boolean: True or False
    """
    return user and (
        user.is_superuser
        or user.is_staff
        or len(
            set(user.moira_lists.values_list("name", flat=True)).intersection(
                settings.STAFF_MOIRA_LISTS
            )
        )
        > 0
    )


from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='mainapp_index'),
    path('step1', views.step1, name='mainapp_step1'),
    path('report/<str:report_id>/', views.enviar_resultado),
    path('decision', views.decision, name='mainapp_decision'),
    path('exito/<str:report_id>/', views.mostrar_resultado),
    path('mostrar_reporte', views.mostrar_reporte, name='mainapp_mostrar'),
    path('volver', views.volver, name='mainapp_volver')
]


from config import CONFIG, SCRIPT_DIR, save as config_save
from sys import argv
from iface import getVFConfigs, getVFStates, findFreeVF, findVFByVMIDAndVLAN, VF, DEFAULT_MAC, DEFAULT_VLAN
from pve import getVMIDList, saveAllPVEConfigs

cmd = argv[1]
iface = CONFIG['interface']

if cmd == 'show':
    iflen = 10

    vfConfigs = getVFConfigs(iface)
    vfStates = getVFStates(iface)

    vfStateMap = {}

    for vfState in vfStates:
        vfStateMap[vfState.idx] = vfState

    print('  '.ljust(iflen) + '                          CONFIG | CURRENT                | STATE')
    print('PF'.ljust(iflen) + '  VF VMID VLAN MAC               | VLAN MAC               | VLAN MAC')
    for vf in getVFConfigs(iface):
        vfState = vfStateMap[vf.idx]
        vlanStr = 'GOOD'
        if vfState.vlan != vf.vlan:
            macStr = 'BAD '
        macStr = 'GOOD'
        if vfState.mac != vf.mac:
            macStr = 'BAD '
        print(vf.tabular(iflen) + ' | ' + vfState.tabular(iflen, True) + ' | ' + vlanStr + ' ' + macStr)

elif cmd == 'add':
    vmid = int(argv[2], 10)
    vlan = int(argv[3], 10)

    if findVFByVMIDAndVLAN(iface, vmid, vlan):
        raise Exception('Cannot have two interfaces with same VLAN on VM')

    vf = findFreeVF(iface)
    if not vf:
        raise Exception('No free interfaces found on {iface}')

    vf.vmid = vmid
    vf.vlan = vlan

    vf.syncConfig()
    vf.applyOS()
    vf.applyVM(vmid)
    config_save()

    print(f'Added interface {vf.getPHYName()} with MAC {vf.mac}')
elif cmd == 'rm':
    vmid = int(argv[2], 10)
    vlan = None
    if len(argv) > 3:
        vlan = int(argv[3], 10)
    vf = findVFByVMIDAndVLAN(iface, vmid, vlan)
    vf.vmid = None
    vf.vlan = 0

    vf.syncConfig()
    vf.applyOS()
    vf.applyVM(vmid)
    config_save()

    print(f'Removed interface {vf.getPHYName()}')
elif cmd == 'mv':
    vmid = int(argv[2], 10)
    oldVlan = None
    newVlan = None
    if len(argv) > 4:
        oldVlan = int(argv[3], 10)
        newVlan = int(argv[4], 10)
    else:
        newVlan = int(argv[3], 10)

    vf = findVFByVMIDAndVLAN(iface, vmid, oldVlan)
    vf.vlan = newVlan

    vf.syncConfig()
    vf.applyOS()
    vf.applyVM(vmid)
    config_save()

    print(f'Moved interface {vf.getPHYName()} to VLAN {newVlan}')
elif cmd == 'apply':
    applyType = None
    if len(argv) > 2:
        applyType = argv[2].upper()

    doPVE = applyType == 'PVE' or not applyType
    doOS = applyType == 'OS' or not applyType

    vmidList = []

    if doPVE:
        vmidList = getVMIDList()

    for vf in getVFConfigs(None):
        if doOS:
            vf.applyOS()

        if doPVE:
            for vmid in vmidList:
                vf.applyVM(vmid)
    if doOS:
        print('Applied config to OS!')
    if doPVE:
        print('Applied config to PVE!')
elif cmd == 'fixmacs':
    for vf in getVFConfigs(None):
        vf.mac = vf.mac.lower()
        vf.syncConfig()
    config_save()
    print('Lowercased all MACs')
elif cmd == 'fixorphans':
    vmidList = getVMIDList()
    for vf in getVFConfigs(None):
        if (vf.vmid == None and vf.vlan == DEFAULT_VLAN) or vf.vmid in vmidList:
            continue
        print(f'Orphan found: {vf.getPHYName()} points to VM {vf.vmid} and VLAN {vf.vlan}')
        vf.vmid = None
        vf.vlan = DEFAULT_VLAN
        vf.syncConfig()
        vf.applyOS()
    config_save()
else:
    print(f'Invalid command: {cmd}\n')
    f = open(f'{SCRIPT_DIR}/README', 'r')
    print(f.read())
    f.close()

saveAllPVEConfigs()


import torch
import torch.nn as nn

class TokenSplit(nn.Module):

    def __init__(self, embed_dim, expand=1):
        super().__init__()
        assert isinstance(embed_dim, int)

        self.expand = expand
        assert self.expand in [1, 2], 'expand not in [1, 2] can not be handled yet.'

        self.linear = nn.Sequential(
            nn.Linear(embed_dim, embed_dim * expand),  # C -> 2C
            nn.ReLU(inplace=True),
        )

    def forward(self, x):
        """
        Args:
            x: dim: N, B, C, where N is the number of tokens, B is the batch size,
            C is the channel dimension.
        Returns:

        """

        if self.expand == 1:
            z = self.linear(x)
            return z  # torch.split(z, C, dim=-1)
        else:
            C = x.shape[-1]
            z = self.linear(x)
            # z[:,:, :C], z[:,:, C:]
            # Splits the tensor into chunks. Each chunk is a view of the original tensor.
            return torch.split(z, C, dim=-1)

def get_valid_token_mask(x, mask=None):
    """
    Args:
        x: dim: (N, B, C), where N is the number of tokens, B is the batch size,
        mask: (B, N); 0, valid locations, 1, padding locations.
    Returns: bool()
    """
    N, B, C = x.shape
    if mask is None:
        valid_tokens = torch.ones(N, B).to(x.device).bool()
    else:
        valid_tokens = ~(mask.bool().permute(1, 0))  # (B, N) -> (N, B)
    return valid_tokens

class sMLP(nn.Module):
    # 1x1 conv for fg tokens.

    def __init__(self, embed_dim,):
        super().__init__()
        self.token_split_conv = TokenSplit(embed_dim=embed_dim, expand=1)

    def forward(self, x, mask, ksgt_targets):
        fg_gt = ksgt_targets['fg_gt']  # (N, B), e.g.,  torch.Size([756, 2])
        valid_tokens_float = get_valid_token_mask(x, mask).float()  # (N, B)
        gt_fg_token_mask = fg_gt.bool().float() * valid_tokens_float  # (N, B)

        N, B, C = x.shape
        x_fg = torch.zeros_like(x)
        for k in range(B):
            mask = gt_fg_token_mask[:, k].bool()
            x_k = x[:, k, :][mask]   # x: N, B, C
            x_k_new = self.token_split_conv(x_k)
            x_fg[mask, k, :] += x_k_new

        # Keep bg tokens and update fg tokens to x_fg
        x_new = x.clone() * (1 - gt_fg_token_mask).unsqueeze(-1).repeat(1, 1, x.shape[-1]) + x_fg

        output_dict = {'x': x_new, 'gt_fg_token_mask': gt_fg_token_mask, }

        return output_dict


import subprocess
from subprocess import PIPE
from .config import Config

class RapiProc(object):

    def __init__(self, executable):
        self.path = executable

    def run(self, config: Config):
        proc = subprocess.Popen([self.path, "--call-by-api"],
                                stdin=PIPE, stderr=PIPE, stdout=PIPE, encoding="utf-8")
        (out, err) = proc.communicate(config.toStr())
        return {
            "out": out,
            "err": err,
            "status": proc.returncode
        }


# n = len(nums)
# time: O(log n)
# space: O(1)

from typing import List

class Solution:
    def countNegatives(self, grid: List[List[int]]) -> int:

        count = 0
        col_num = len(grid[0])

        for col in grid:
            left = 0
            right = len(col) - 1

            while left <= right:
                mid = (right+left) // 2
                if col[mid] >= 0:
                    left = mid + 1
                else:
                    right = mid - 1

            count += col_num - left

        return count


import csv
import numpy as np
import os
import shutil
import sys
import time
import pandas as pd
from datetime import datetime

from STResNet import stresnet

from keras.metrics import mse
from keras.optimizers import Adam
from keras.callbacks import CSVLogger, EarlyStopping, ModelCheckpoint, LearningRateScheduler

def getXSYS(mode, dayInfo=False):
    '''
       Generate features and labels
       mode: {'train', 'test'}
       dayInfo: True/False
    '''
    '''
    dates = pd.date_range(START, END).strftime('%Y%m%d').tolist()
    startIndex, endIndex = dates.index(trainStartDate), dates.index(trainEndDate)
    trainData = allData[startIndex * DAYTIMESTEP : (endIndex + 1) * DAYTIMESTEP]
    '''
    interval_p, interval_t = 1, 7    # day interval for period/trend
    depends = [range(1, len_c + 1),
               [interval_p * DAYTIMESTEP * i for i in range(1, len_p + 1)],
               [interval_t * DAYTIMESTEP * i for i in range(1, len_t + 1)]]

    # mode
    if mode == 'train':
        data = np.load(dataPath + '/popTrainValidate.npy')
        start = max(len_c, interval_p * DAYTIMESTEP * len_p, interval_t * DAYTIMESTEP * len_t)
        end = data.shape[0]
        if dayInfo:
            day_info = np.genfromtxt(dataPath + '/dayInfo_onehot_%s_%s.csv' % (trainStartDate, trainEndDate),
                                     delimiter = ',', skip_header = 1)
            print('Read-in train dayInfo dim:', day_info.shape)
            day_info_inter = []
            for row in day_info:
                for i in range(30//5):    # dayInfo within every 30 min stays same
                    day_info_inter.append(row)
            #day_info_inter = np.array(day_info_inter)
            dayInfo_dim = day_info.shape[1]
            day_feature = np.array(day_info_inter)[start:end]
        else:
            dayInfo_dim = 0
            day_feature = None
    '''
    elif mode=='test':
        data = np.load(dataPath + '/popSpecial.npy')
        start = len_c
        end = data.shape[0]
        if dayInfo:
            day_info = np.genfromtxt(dataPath + '/dayInfo_onehot_%s_%s.csv' % (testStartDate, testEndDate),
                                     delimiter = ',', skip_header = 1)
            print('Read-in test dayInfo dim:', day_info.shape)
            day_info_intep = []
            for row in day_info:
                for i in range(30//5):    # dayInfo within every 30 min stays same
                    day_info_intep.append(row)
            day_info_intep = np.array(day_info_intep)
            dayInfo_dim = day_info.shape[1]
            day_feature = day_info_intep[start:end]
        else:
            dayInfo_dim = 0
            day_feature = None
    else:
        assert False, 'Invalid mode...'
    '''
    XC, XP, XT = [], [], []
    for i in range(start, end):
        x_c = [data[i - j] for j in depends[0]]
        x_p = [data[i - j] for j in depends[1]]
        x_t = [data[i - j] for j in depends[2]]
        if len_c > 0:
            XC.append(np.dstack(x_c))
        if len_p > 0:
            XP.append(np.dstack(x_p))
        if len_t > 0:
            XT.append(np.dstack(x_t))

    #XC, XP, XT = np.array(XC), np.array(XP), np.array(XT)
    #print(mode, 'X', XC.shape, XP.shape, XT.shape, day_feature.shape)
    #XS = [XC, XP, XT, day_feature] if day_feature is not None else [XC, XP, XT]
    XS = [x for x in [XC, XP, XT, day_feature] if x != []]
    print('train X')
    for item in XS:
        print(np.array(item).shape)
    YS = data[start:end]
    print(mode, 'Y', YS.shape)

    return XS, YS, dayInfo_dim

def getRealTest(data, dayInfo):
    if dayInfo:
        day_info = np.genfromtxt(dataPath + '/dayInfo_onehot_%s_%s.csv' % (testStartDate, testEndDate),
                                 delimiter = ',', skip_header = 1)
        day_info_inter = []
        for row in day_info:
            for i in range(30//5):
                day_info_inter.append(row)
        dayInfo_dim = day_info.shape[1]
        day_feature = np.array(day_info_inter)
        print('Test dayInfo dim', dayInfo_dim, day_feature.shape)
    XC, YS, DAY = [], [], []
    for i in range(data.shape[0] - TIMESTEP*2):
        x = data[i:i+TIMESTEP]
        y = data[i+TIMESTEP:i+TIMESTEP*2]
        day = day_feature[i+TIMESTEP:i+TIMESTEP*2]
        XC.append(x), YS.append(y), DAY.append(day)
    #XC = np.array(XC).swapaxes(4, 1)
    #XC = np.squeeze(XC)
    XS = [np.array(XC), np.array(DAY)]
    return XS, np.array(YS)

def getModel(modelName, residual_units, dayInfo_dim):
    if modelName == 'STResNet':
        # format input
        c_dim = (len_c, map_height, map_width, nb_channel) if len_c > 0 else None
        p_dim = (len_c, map_height, map_width, nb_channel) if len_p > 0 else None
        t_dim = (len_c, map_height, map_width, nb_channel) if len_t > 0 else None
        print('Input dimensions:', c_dim, p_dim, t_dim, dayInfo_dim)

        # call the model
        model = stresnet(c_dim = c_dim, p_dim = p_dim, t_dim = t_dim,
                         residual_units = residual_units, dayInfo_dim = dayInfo_dim)
        model.summary()
        # from keras.utils import plot_model
        # plot_model(model, to_file = modelPath+'/model.png', show_shapes = True)
        return model
    else:
        print("Model name can't be recognized...")
        return None

def trainModel(model, trainX, trainY):
    print('*' * 10, 'model training started ...', time.ctime(), '*' * 10)
    csv_logger = CSVLogger(logPath + '/epochLog.log')
    #early_stopper = EarlyStopping(monitor='val_loss', patience=10, verbose = 1, mode='auto')
    model_checkpointer = ModelCheckpoint(modelPath + '/temp' + '/modelBest.h5', monitor='val_loss', verbose=1, save_best_only=True, mode='min')

    #print('trainX dim:', trainX.shape, 'trainY dim:', trainY.shape)
    # train
    history = model.fit(trainX, trainY,
                        batch_size = BATCHSIZE,
                        epochs = EPOCHS,
                        validation_split = SPLIT,
                        callbacks = [LR, csv_logger, model_checkpointer],
                        # shuffle = True,
                        verbose = 1)
    model.save_weights(modelPath + '/temp' + '/trained_weights.h5', overwrite = True)
    print('*' * 10, 'model training ended ...', time.ctime(), '*' * 10)

def testModel(model, testX, testY):
    with open(logPath + '/prediction_scores.txt', 'a') as wf:
        wf.write(','.join(['*' * 10, 'model testing started ...', time.ctime(), '*' * 10]) + '\n')
        print('*' * 10, 'model testing started ...', time.ctime(), '*' * 10)

    assert os.path.exists(modelPath + '/temp' + '/modelBest.h5'), 'Model is not existing...'
    model.load_weights(modelPath + '/temp' + '/modelBest.h5')

    f = open(logPath + '/prediction_scores.txt', 'a')
    print('*' * 40)
    f.write('*'*20 + 'Hyper parameter' + '*'*20 + '\n')

    # sequence to sequence prediction
    YS_pred_list = []
    for i in range(TIMESTEP):
        # preprocess XS
        XC, DAY = testX[0], testX[1]
        if i > 0:
            XC = np.concatenate((XC[:, i:, :, :, :], YS_pred), axis = 1)
        else:
            pass
        XC = XC.swapaxes(1, 4)
        XC = np.squeeze(XC)
        DAY = np.squeeze(DAY[:, i:i+1, :])
        curXS = [XC, DAY]

        stepYS = model.predict(curXS, BATCHSIZE)
        stepYS = stepYS[:, np.newaxis, :, :, :]
        YS_pred_list.append(stepYS)
        YS_pred = np.concatenate(YS_pred_list, axis = 1)    # generate YS_pred

        stepYS_true = testY[:, i:i+1, :, :, :]
        print(stepYS.shape, stepYS_true.shape)
        assert stepYS.shape == stepYS_true.shape    # (N, 1, 80, 80, 1)
        MSE_cur_step = np.mean((stepYS - stepYS_true)**2)
        f.write("Model MSE for step %d, %f\n" % (i, MSE_cur_step))
        print("Model MSE for step", i, MSE_cur_step)

    # compute MSE for whole test set
    print(YS_pred.shape, testY.shape)
    assert YS_pred.shape == testY.shape    # (N, 6, 80, 80, 1)
    MSE_multisteps = np.mean((YS_pred - testY)**2)
    f.write("Model MSE for multisteps, %f\n" % MSE_multisteps)
    print('MSE for multisteps', MSE_multisteps)

    # compute rescaled MSE
    YS_pred = YS_pred * 500
    YS_true = testY * 500
    for i in range(TIMESTEP):
        stepYS_pred = YS_pred[:, i:i+1, :, :, :]
        stepYS_true = YS_true[:, i:i+1, :, :, :]
        MSE_cur_step = np.mean((stepYS_pred - stepYS_true)**2)
        f.write("Model rescaled(*500) MSE for step, %d, %f\n" % (i, MSE_cur_step))
        print("Model rescaled(*500) MSE for step", i, MSE_cur_step)
    MSE_multisteps = np.mean((YS_pred - YS_true) ** 2)
    f.write("Model rescaled(*500) MSE for multisteps, %f\n" % MSE_multisteps)
    print('MSE rescaled(*500) for multisteps', MSE_multisteps)

    f.close()

    '''
    # evaluate on test set
    scores = model.evaluate(testX, testY, BATCHSIZE *2, verbose = 1)
    predY = model.predict(testX, BATCHSIZE *2, verbose = 1)
    testY, predY = normal.denormalize(testY), normal.denormalize(predY)
    print('testY range:', testY.min(), testY.max())
    print('predY range:', predY.min(), predY.max())

    # Metrics
    MSE = scores[3]*((normal.dmax - normal.dmin)/2)**2

    model.save_weights(modelPath + '/temp' + '/tested_weights.h5', overwrite = True)

    print("Model scaled MSE", scores[0])
    print("Model rescaled MSE", MSE)
    with open(logPath + '/prediction_scores.txt', 'a') as wf:
        wf.write("Model scaled MSE, %.6f\n" % scores[0])
        wf.write("Model rescaled MSE, %.6f\n" % MSE)

    with open(logPath + '/prediction_scores.txt', 'a') as wf:
        wf.write(','.join(['*' * 10, 'model testing ended ...', time.ctime(), '*' * 10]) + '\n')
        print('*' * 10, 'model testing ended ...', time.ctime(), '*' * 10)
    '''

################# Parameter Settings #######################

dataPath = './E1_Earthquake'
START, END = '20110301', '20110311'
trainStartDate, trainEndDate = '20110301', '20110310'
testStartDate, testEndDate = '20110311', '20110311'

modelPath = './model'
logPath = './log'
modelName = 'STResNet'
dayInfo = True
DAYTIMESTEP = 24 * 12    # 5 min time interval
map_height, map_width = 80, 80
nb_channel = 1    # population density
len_c, len_p, len_t = 6, 0, 0    # seq of 6 closeness
nb_residual_units = 1

TIMESTEP = 6    # test sequence length
LOSS = 'mse'
OPTIMIZER = 'adam'
METRICS = [mse]
LR = LearningRateScheduler(lambda epoch: 0.0001)
BATCHSIZE = 4
EPOCHS = 200
SPLIT = 0.2

################## Parameter Settings #######################

def main():
    # reproducibility
    np.random.seed(100)
    import random
    random.seed(100)
    from tensorflow import set_random_seed
    set_random_seed(100)
    os.environ['PYTHONHASHSEED'] = '0'

    # set GPU session
    import tensorflow as tf
    session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
    # session_conf.gpu_options.per_process_gpu_memory_fraction = 0.5
    session_conf.gpu_options.allow_growth = True
    session_conf.gpu_options.visible_device_list = '7'
    tf.set_random_seed(100)
    session = tf.Session(graph=tf.get_default_graph(), config=session_conf)
    from keras import backend as K
    K.set_session(session)

    # set log path
    if not os.path.exists(logPath):
        print('Making a folder for log output...')
        os.makedirs(os.path.dirname(__file__) + logPath)

    # get features
    trainX, trainY, dayInfo_dim = getXSYS('train', dayInfo)
    #print('trainXY shapes:', trainX.shape, trainY.shape)
    testData = np.load(dataPath + '/popSpecial.npy')
    XS, YS = getRealTest(testData, dayInfo)
    print('Test data dims:', XS[0].shape, XS[1].shape, YS.shape)

    # get model
    model = getModel(modelName, nb_residual_units, dayInfo_dim)
    model.compile(loss = LOSS, optimizer = OPTIMIZER, metrics = METRICS)
    print('Model compiled...')

    # train model
    trainModel(model, trainX, trainY)

    # test model
    testModel(model, XS, YS)

if __name__ == '__main__':
    main()


from collections import deque
from sys import stdin
input = stdin.readline

def BFS() :
    visited = [0] * 101
    queue = deque([1])
    while queue :
        num = queue.popleft()
        for next_num in [*range(num + 1, num + 7)] :
            if next_num == 100 : return visited[num] + 1
            if next_num < 100 :
                if next_num in jump.keys() : next_num = jump[next_num]
                if not visited[next_num] :
                    visited[next_num] = visited[num] + 1
                    queue.append(next_num)
    return visited[100]

if __name__ == "__main__" :
    N, M = map(int, input().split())
    jump = dict()
    for _ in range(N + M) :
        x, y = map(int, input().split())
        jump[x] = y
    print(BFS())


import argparse
import json
import os
import subprocess

from google.cloud import storage
from google.oauth2 import service_account

parser = argparse.ArgumentParser()
parser.add_argument('--path', default='/tmp/token.json')

service_account_info = json.load(open('cloud_storage_credentials.json'))
credentials = service_account.Credentials.from_service_account_info(service_account_info)
client = storage.Client(
    credentials=credentials,
    project=credentials.project_id,
)

if __name__ == "__main__":
    args = parser.parse_args()
    print('local remove file path={}'.format(args.path))
    os.remove(args.path)

    bucket = client.bucket('sagawa-notice-line-bot')
    blob = bucket.blob('token.json')
    print('cloud storage remove file path=/{}/{}'.format(bucket, blob))
    blob.delete()

    subprocess.call('python main.py', shell=True)


import tvo
import numpy as np
import torch as to
from torch import Tensor

def init_W_data_mean(
    data: Tensor,
    H: int,
    std_factor: float = 0.25,
    dtype: to.dtype = to.float64,
    device: to.device = None,
) -> Tensor:
    """Initialize weights W based on noisy mean of the data points.

    :param data: Data set, is (N, D).
    :param H: Number of basis functions to be generated.
    :param std_factor: Scalar to control amount of standard deviation of additive noise
    :param dtype: dtype of output Tensor. Defaults to torch.float64.
    :param device: torch.device of output Tensor. Defaults to tvo.get_device().
    :returns: Weight matrix W with shape (D,H).
    """
    device_ = tvo.get_device() if device is None else device
    data_nanmean = to.from_numpy(np.nanmean(data.detach().cpu().numpy(), axis=0)).to(
        dtype=dtype, device=device_
    )
    var = init_sigma2_default(data, dtype, device_)
    return data_nanmean.repeat((H, 1)).t() + std_factor * to.sqrt(var) * to.randn(
        (len(data_nanmean), H), dtype=dtype, device=device_
    )

def init_sigma2_default(
    data: Tensor, dtype: to.dtype = to.float64, device: to.device = None
) -> Tensor:
    """Initialize scalar sigma parameter based on variance of the data points.

    :param data: Data set, is (N, D).
    :param dtype: dtype of output Tensor. Defaults to torch.float64.
    :param device: torch.device of output Tensor. Defaults to tvo.get_device().
    :returns: Scalar sigma parameter.

    Returns the mean of the variance in each dimension d=1,...,D.
    """
    _device = tvo.get_device() if device is None else device
    var = to.from_numpy(np.nanvar(data.detach().cpu().numpy(), axis=0)).to(
        device=_device, dtype=dtype
    )
    return to.mean(var) + to.tensor([0.001], device=_device, dtype=dtype)

def init_pies_default(
    H: int, crowdedness: float = 2.0, dtype: to.dtype = to.float64, device: to.device = None
):
    """Initialize pi parameter based on given crowdedness.

    :param H: Length of pi vector.
    :param crowdedness: Average crowdedness corresponding to sum of elements in vector pi.
    :param dtype: dtype of output Tensor. Defaults to torch.float64.
    :param device: torch.device of output Tensor. Defaults to tvo.get_device().
    :returns: Vector pi.
    """

    if device is None:
        device = tvo.get_device()
    return to.full((H,), fill_value=crowdedness / H, dtype=dtype, device=device)


import os, django
import re
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "nta_library.settings")
django.setup()
from books.models import Book, Borrower
from faker import Faker

fakegen = Faker()
def populate(N=5):
    rows = []
    for entry in range(N):
        fake_name = fakegen.name()
        fake_auther = fakegen.name()
        fake_type = fakegen.name()
        fake_isbn = fakegen.zipcode()
        fake_copies = fakegen.zipcode()
        fake_issue_date = fakegen.date()
        fake_date = fakegen.date()
        fake_out = fakegen.boolean()
        # New entry
        boooooks = Book.objects.get_or_create(name=fake_name,auther=fake_auther,type=fake_type,isbn = fake_isbn,copies=fake_copies,
        issue_date=fake_issue_date,out=fake_out  )[0]
        borrrrrowerrrr= Borrower.objects.get_or_create(borrower_book = boooooks, name= fake_name,
        department = fake_name, borrow_date = fake_date,return_date = fake_date, blacklisted = fake_out)[0]
        rows.append(boooooks)
    return rows

populate(5)


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

# In[1]:

import torch
from torch.utils.data import Dataset, DataLoader
from enum import Enum
import pandas as pd
from PIL import Image
import numpy as np
import torchvision.transforms as transforms
import matplotlib.pyplot as plt

# In[2]:

class Modes(Enum):
    train = 1
    test = 2

class ODIR5K(Dataset):
    def __init__(self, mode, augmentations):
        super(ODIR5K, self).__init__()
        self.mode = mode
        self.csv_path = f"/pytorch_fake{self.mode.name}.csv"
        self.csv = pd.read_csv(self.csv_path)
        self.aug = augmentations

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

    def __getitem__(self, index):
        csv_line = self.csv.iloc[index]
        img_path = csv_line['path']
        label = torch.tensor(int(csv_line['label']))

        image = Image.open(img_path).convert("RGB")

        if self.aug is not None:
            image = self.aug(image)

        return image, label

# In[6]:

if __name__ == "__main__":
    train_idx, val_idx = train_test_split(list(range(len(odir_train))), test_size=0.20, random_state=42)
    t = transforms.Compose([transforms.Resize((500, 500)),
                            transforms.ToTensor()])

    ds = ODIR5K(Modes.train, t, train_idx)
    dl = DataLoader(ds, batch_size = 4, shuffle=True)
    batch = next(iter(dl))

    for img in batch['image']:
#         print(b)
        img = np.transpose(img.numpy(), (1, 2, 0))
        plt.figure()
        plt.imshow(img)
        plt.show()

# In[ ]:


import argparse
from pathlib import Path
import numpy as np
from time import time as tm
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from saft.saft import SAF_File
from saft.saf_histo import check_histograms

fit_fn_msg = "log((S + B) / B) = p0 exp(-p1 * pT)"

def print_scipy_stats(sci_ps, sci_errs):
    """Print minimizing paraters. """
    print("*" * 40)
    print("Scipy minimizing function parameters")
    print("Fitting function:", fit_fn_msg)
    for i, (p, err) in enumerate(zip(sci_ps, sci_errs)):
        print(f"p{i:<24} = {p:>13.8f}  +/-   {err:<10.8f}")
    print("*" * 40)

def fit_f(x, p0, p1):
    """
    The fitting function.
    Parameters
    ----------
        - x: np.array, independent data
        - p0: float, origin intercept
        - p1: float, angular coefficient
    Returns
    -------
        - np.array, evaluated function points of shape=(nb data,)
    """
    return p0 + p1 * x

def f(x, p0, p1):
    """
    The function to be plotted.
    Parameters
    ----------
        - x: np.array, independent points of shape=(nb data,)
        - p0: float, origin intercept
        - p1: float, angular coefficient
    Returns
    -------
        - np.array, evaluated function points of shape=(nb data,)
    """
    return p0 + p1 * x

def main(dirname):
    """
    Parameters
    ----------
        - dirname: str, directory name with .saf files
    """
    # importing histograms
    sm_hist = SAF_File(dirname / "histos_sm.saf").get_from_title("pt")
    dm_hist = SAF_File(dirname / "histos_dm.saf").get_from_title("pt")
    check_histograms(sm_hist, dm_hist)

    # compute transverse momentum bin values
    cut = 50 if "50" in dirname.name else 200
    ptmin = sm_hist.xmin
    ptmax = sm_hist.xmax
    pts = sm_hist.bins[:-1]

    # compute the bulk distribution
    bulk_min = 50 if cut == 50 else 200
    bulk_max = 250 if cut == 50 else 400
    mask = np.logical_and(pts>=bulk_min, pts<bulk_max)

    bkg = sm_hist.data # backgroud
    sig = dm_hist.data # signal

    # compute double log of signal to background ratio r
    r = np.divide(sig + bkg, bkg, out=np.ones_like(bkg), where=bkg!=0)
    logr = np.log(r)
    log2r = np.log(logr, out=np.zeros_like(logr), where=logr!=0)

    # fit with SciPy
    sci_ps, sci_cov = curve_fit(fit_f, pts[mask], log2r[mask])
    sci_errs = np.sqrt(np.diag(sci_cov))
    print_scipy_stats(sci_ps, sci_errs)

    x = np.linspace(ptmin, ptmax, 1000)

    # plot the error bands on the fitted function
    ps = np.linspace(-1, 1, 10)

    fig = plt.figure(figsize=[8, 6], dpi=100)
    ax = fig.add_subplot()
    mean_f = f(x, sci_ps[0], sci_ps[1])
    lower_f = f(x, sci_ps[0] - sci_errs[0], sci_ps[1] - sci_errs[1])
    upper_f = f(x, sci_ps[0] + sci_errs[0], sci_ps[1] + sci_errs[1])
    msg1 = r"$R = %.4f \pm %.4f$" % (sci_ps[0], sci_errs[0])
    msg2 = r"$(k_1-k_2) = %.4f \pm %.4f$ GeV$^{-1}$" % (sci_ps[1], sci_errs[1])
    ax.scatter(
        pts,
        log2r,
        s=5,
        marker=".",
        label="mg5",
        color="blue",
    )
    ax.fill_between(x, lower_f, upper_f, color="green", alpha=0.5, label=msg1)
    ax.fill_between([], [], color="white", label=msg2)
    ax.plot(x, mean_f, color="red", lw=0.5)
    ax.set_xlim(bulk_min, bulk_max)
    ax.legend()
    ax.tick_params(which="both", direction="in", top=True, right=True)
    ax.minorticks_on()
    fig.suptitle(r"$p + p \rightarrow j + X_{inv} $,     $p_T > %d$ GeV"%cut, y=0.96)
    ax.set_title(r"Linear interpolation: y = R + $(k_1-k_2) \,p_T$")
    ax.set_ylabel(r"$\log (\log (r))$")
    ax.set_xlabel(r"$p_{T,j}$ [GeV]")
    plt.savefig(dirname/"log2r(pT).png", dpi=300, bbox_inches="tight")
    plt.close()

if __name__ == "__main__":
    start = tm()
    parser = argparse.ArgumentParser()
    parser.add_argument("dirname", type=Path, help="directory name with .saf files")
    args = parser.parse_args()
    main(args.dirname)
    print(f"Program done in {tm()-start}s")


from pathlib import Path

import pandas as pd
import pytest
from typer.testing import CliRunner

from ilids.commands.experiments import typer_app

runner = CliRunner(mix_stderr=False)

@pytest.mark.handcrafted_files(("actionclip_sequences.csv", "actionclip_sequences"))
@pytest.mark.ckpt_files((Path("actionclip") / "vit-b-16-8f.pt", "actionclip_ckpt"))
def test_actionclip(actionclip_sequences: Path, actionclip_ckpt: Path, tmp_path: Path):
    output_path = tmp_path / "output.pkl"

    input_df = pd.read_csv(actionclip_sequences)

    min_input_path = tmp_path / "min_input.csv"
    input_df.head(n=2).to_csv(min_input_path)

    result = runner.invoke(
        typer_app,
        [
            "actionclip",
            str(actionclip_ckpt),
            str(min_input_path),
            str(output_path),
        ],
    )

    assert result.exit_code == 0

    assert output_path.exists() and output_path.is_file()

    df = pd.read_pickle(output_path)

    assert len(df) == 2
    assert len(df.columns) == 512, df.columns


"""
124. Binary Tree Maximum Path Sum

https://leetcode.com/problems/binary-tree-maximum-path-sum/
"""
from typing import Optional

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:

    res = -(10**20)

    def maxPathSum(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        left = self.go(root.left)
        right = self.go(root.right)

        return max(left + right + root.val, root.val, self.res, root.val + left, root.val + right)

    def go(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0

        left = self.go(root.left)
        right = self.go(root.right)

        maxOne = max(root.val, left + root.val, right + root.val)
        maxAll = max(maxOne, left + right + root.val)

        self.res = max(self.res, maxAll)

        return maxOne

def main():
    s = Solution()
    print(s.maxPathSum(TreeNode(-10, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))))
    print(s.maxPathSum(TreeNode(2, TreeNode(-1))))

if __name__ == '__main__':
    raise(SystemExit(main()))


__author__ = 'rasheduzzaman'

import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, neighbors
from sklearn.metrics import roc_curve, auc
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import label_binarize
from sklearn.multiclass import OneVsRestClassifier
import pandas as pd
import sklearn.preprocessing as trans
from sklearn.lda import LDA
from sklearn.decomposition import PCA
from sklearn.qda import QDA

###############################################################################
# read data file
file = pd.read_csv('/home/rasheduzzaman/Courses/CS782/HAR_V1/v1.csv', header=None, sep=',')
df = pd.DataFrame(file)
data = df.convert_objects(convert_numeric=True)

# get data indexes
index = np.asarray(data.ix[:, 0:1].values)   # has column 0 and 1
classes = np.asarray(data.ix[:, 45].values)  # has column 45
data = np.asarray(data.ix[:, 2:44].values)   # has column 2 to 44

# actual working data
y = classes.astype(str) # classes
X = data.astype(float)  # data
X = np.nan_to_num(X)    # replace all nan to zero

# Normalize the data
X_norm = trans.StandardScaler().fit_transform(X)

###############################################################################
'''
# Perform PCA
pca = PCA(n_components=35)
X = pca.fit(X_norm).transform(X_norm)
print(np.sum(pca.explained_variance_ratio_))

'''
# Perform LDA
lda = LDA(n_components=35)
X = lda.fit(X_norm, y).transform(X_norm)

st = np.unique(y)  # class names

# Binarize the output
y = label_binarize(y, classes=st)
n_classes = y.shape[1]

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

# shuffle and split training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.3,
                                                    random_state=0)

# linear discriminent classifier
#clf = LDA(solver="svd", store_covariance=True)

# linear discriminent classifier
clf = QDA()

# k-nearest neighbor classifier
#clf = neighbors.KNeighborsClassifier(4)

# support vector machine
#clf = svm.SVC(kernel='rbf', gamma=0.4,  probability=True, random_state=0)

# Learn to predict each class against the other
classifier = OneVsRestClassifier(clf)
y_score = classifier.fit(X_train, y_train).predict_proba(X_test)

# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(n_classes):
    fpr[i], tpr[i], _ = roc_curve(y_test[:, i], y_score[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])

# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(y_test.ravel(), y_score.ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

# Plot ROC curve
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
         label='micro-average ROC curve (area = {0:0.2f})'
               ''.format(roc_auc["micro"]))
for i in range(n_classes):
    plt.plot(fpr[i], tpr[i], label='ROC curve of class {0} (area = {1:0.2f})'
                                   ''.format(st[i], roc_auc[i]))

plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic for multi-class')
plt.legend(loc="lower right")
plt.show()