"""
l2gen.py must be run with at least 2 MPI processes, as it utilizes a master-worker approach.
Example usage:
    mpirun --machinefile machinefile.txt python3 -u -m mpi4py l2gen.py
    mpirun -n 120 python3 -u -m mpi4py l2gen.py -p /mn/stornext/d22/cmbco/comap/params/param_co7_apr22_v2.txt
    To get Numpy to respect number of threads (per MPI thread):
    OMP_NUM_THREADS=20 OPENBLAS_NUM_THREADS=20 MKL_NUM_THREADS=20 mpirun -n 10 python3 -u -m mpi4py l2gen.py
the -m mpi4py is necessary for MPI to properly handle errors. See https://mpi4py.readthedocs.io/en/stable/mpi4py.run.html and https://stackoverflow.com/questions/49868333/fail-fast-with-mpi4py.
"""
import time
import numpy as np
import logging
import datetime
import os
import psutil
import random
from mpi4py import MPI
from tqdm import tqdm
import sys
from l2gen_l2class import level2_file
import l2gen_filters
from tools.read_runlist import read_runlist
# from l2gen_filters import Tsys_calc, Normalize_Gain, Decimation, Pointing_Template_Subtraction, Masking, Polynomial_filter, Frequency_filter, PCA_filter, PCA_feed_filter, Calibration

import warnings
warnings.filterwarnings("ignore", message="divide by zero encountered in double_scalars")
warnings.filterwarnings("ignore", message="invalid value encountered in double_scalars")
warnings.filterwarnings("ignore", message="invalid value encountered in true_divide")
warnings.filterwarnings("ignore", message="divide by zero encountered in true_divide")
warnings.filterwarnings("ignore", message="Degrees of freedom <= 0 for slice.")
warnings.filterwarnings("ignore", message="Mean of empty slice")

class Terminal_print:
    def __init__(self, filter_list, Nscans, Nranks):
        self.Nscans = Nscans
        self.N_finished_scans = 0
        self.accumulated_acceptrate = np.zeros((19, 4))
        self.accumulated_feeds = np.zeros((19))
        self.accumulated_filter_runtime = {}
        self.accumulated_filter_runtime["l1_read"] = 0
        for filter in filter_list:
            self.accumulated_filter_runtime[filter.name] = 0
        self.accumulated_filter_runtime["l2_write"] = 0
        self.total_filter_runtime = 1e-10
        self.rank_progress = ["x"]*Nranks
        self.t0 = time.time()
        self.pt0 = time.process_time()
        self.rewrite_lines = 0

    def get_color(self, value):
        if value > 85:
            return "\033[96m"
        elif value > 70:
            return "\033[94m"
        elif value > 50:
            return "\033[93m"
        else:
            return "\033[91m"

    def scan_done_update(self, info):
        self.N_finished_scans += 1

        self.accumulated_feeds[info["feeds"]-1] += 1
        self.accumulated_acceptrate[info["feeds"]-1] += info["acceptrate"]

        self.total_filter_runtime = 0.0
        for filter in info["filter_runtimes"].keys():
            self.accumulated_filter_runtime[filter] += info["filter_runtimes"][filter]
            self.total_filter_runtime += self.accumulated_filter_runtime[filter]

    def scan_progress_update(self, info):
        self.rank_progress[info["rank"]] = info["status"]

    def rewrite_terminal(self):
        Nfeeds = 19
        Nsb = 4
        feeds = np.arange(1, 20)

        printstring = ""
        printstring += " " + "_"*107 + "\n"
        asdf = tqdm.format_meter(self.N_finished_scans, self.Nscans, elapsed=time.time()-self.t0, ncols=105)
        printstring += f"{'______Total progress______':<105}\n"
        printstring += f"{asdf}\n"
        printstring += f"{'':<105}\n"
        printstring += f"{'______Average acceptrate by feed and sidebands______':<105s}\n"
        printstring += f"       all"
        for ifeed in range(Nfeeds):
            printstring += f"{feeds[ifeed]:5d}"
        acc = np.sum(self.accumulated_acceptrate)/np.sum(Nsb*self.accumulated_feeds)*100
        printstring += f"\nall  {self.get_color(acc)}{acc:4.0f}%\033[0m"
        for ifeed in range(Nfeeds):
            acc = np.sum(self.accumulated_acceptrate[ifeed])/(Nsb*self.accumulated_feeds[ifeed])*100
            printstring += f"{self.get_color(acc)}{acc:4.0f}%\033[0m"
        for isb in range(Nsb):
            acc = np.sum(self.accumulated_acceptrate[:,isb])/np.sum(self.accumulated_feeds)*100
            printstring += f"\n  {isb}  {self.get_color(acc)}{acc:4.0f}%\033[0m"
            for ifeed in range(Nfeeds):
                acc = np.sum(self.accumulated_acceptrate[ifeed,isb])/self.accumulated_feeds[ifeed]*100
                printstring += f"{self.get_color(acc)}{acc:4.0f}%\033[0m"
        printstring += "\n"
        printstring += f"{'':<105}\n"
        printstring += f"{'______Runtime summary______':<105s}\n"
        for filter in self.accumulated_filter_runtime.keys():
            tempstring = f"{filter:12s}: {self.accumulated_filter_runtime[filter]/60.0:8.1f}m  ({100*self.accumulated_filter_runtime[filter]/self.total_filter_runtime:4.1f} %)"
            printstring += f"{tempstring:<105s}\n"

        printstring += f"{'':<105}\n"
        printstring += f"{'______Rank report______':<105s}\n"
        rankstring = "".join(self.rank_progress)
        for i in range(100):
            if len(rankstring) > 100*i:
                printstring += f"{rankstring[i*100:(i+1)*100]:<105s}"
                printstring += "\n"
            else:
                break

        printstring = printstring.replace("\n", " |\n| ")
        printstring = printstring.replace("|", "", 1)
        printstring += "_"*105 + " |\n"

        for i in range(self.rewrite_lines):
            sys.stdout.write("\x1b[1A\x1b[2K")
        print(printstring)
        lines = printstring.count("\n")
        self.rewrite_lines = lines + 1

class l2gen_runner:
    def __init__(self, omp_num_threads=2):
        self.comm = MPI.COMM_WORLD
        self.Nranks = self.comm.Get_size()
        self.rank = self.comm.Get_rank()
        self.node_name = MPI.Get_processor_name()
        if self.rank == 0:
            print("\n######## Initializing l2gen ########")

        self.omp_num_threads = omp_num_threads
        self.read_params()
        self.configure_logging()
        self.configure_filters()
        self.read_runlist()
        if self.rank == 0:
            print("######## Done initializing l2gen ########\n")

    def run(self):
        self.comm.Barrier()
        Nscans = len(self.runlist)
        if Nscans == 0:
            if self.rank == 0:
                print(f"No unprocessed scans in runlist. Exiting.")
            return

        WORK_TAG = 1
        DIE_TAG = 2
        PROGRESS_TAG = 8
        DONE_TAG = 9

        ##### Master #####
        if self.rank == 0:
            if self.params.print_progress_bar:
                term = Terminal_print(self.filter_list, Nscans, self.Nranks)
                term.rewrite_terminal()

            proc_order = np.arange(1, self.Nranks)
            np.random.shuffle(proc_order)
            self.tasks_done = 0
            self.tasks_started = 0
            for irank in range(self.Nranks-1):
                self.comm.send(self.tasks_started, dest=proc_order[irank], tag=WORK_TAG)
                self.tasks_started += 1
                if self.tasks_started == Nscans:  # If we have more processes than tasks, kill the rest, and break the task-assigment loop.
                    for iirank in range(irank, self.Nranks-1):
                        self.comm.send(-1, dest=proc_order[iirank], tag=DIE_TAG)
                    break
                if self.params.distributed_starting:
                    time.sleep(min(600/self.Nranks, 15))  # Spawn ranks randomly over 10 minutes, or 15 seconds per rank, whichever is faster.
                time.sleep(0.01)

            while self.tasks_started < Nscans:
                status = MPI.Status()
                info = self.comm.recv(source=MPI.ANY_SOURCE, status=status)
                if status.Get_tag() == DONE_TAG:
                    if self.params.print_progress_bar:
                        term.scan_done_update(info)
                        term.rewrite_terminal()
                    self.tasks_done += 1
                    workerID = status.Get_source()
                    self.comm.send(self.tasks_started, dest=workerID, tag=WORK_TAG)
                    self.tasks_started += 1
                    time.sleep(0.01)
                elif status.Get_tag() == PROGRESS_TAG:
                    if self.params.print_progress_bar:
                        term.scan_progress_update(info)
                        term.rewrite_terminal()
                else:
                    raise ValueError("Unknown tag recieved.")

            while self.tasks_done < Nscans:
                status = MPI.Status()
                info = self.comm.recv(source=MPI.ANY_SOURCE, status=status)
                if status.Get_tag() == DONE_TAG:
                    if self.params.print_progress_bar:
                        term.scan_done_update(info)
                        term.rewrite_terminal()
                    self.tasks_done += 1
                    workerID = status.Get_source()
                    self.comm.send(-1, dest=workerID, tag=DIE_TAG)
                    time.sleep(0.01)
                elif status.Get_tag() == PROGRESS_TAG:
                    if self.params.print_progress_bar:
                        term.scan_progress_update(info)
                        term.rewrite_terminal()
                else:
                    raise ValueError("Unknown tag recieved.")

        ##### Workers #####
        else:
            while True:
                status = MPI.Status()
                iscan = self.comm.recv(source=0, tag=MPI.ANY_TAG, status=status)
                if status.Get_tag() == DIE_TAG:
                    break
                # print(f"[{self.rank}] >>> Starting scan {self.runlist[iscan][0]} ({iscan+1}/{Nscans})...")
                logging.info(f"[{self.rank}] >>> Starting scan {self.runlist[iscan][0]} ({iscan+1}/{Nscans})..."); t0 = time.time(); pt0 = time.process_time()
                l2 = l2gen(self.runlist[iscan], self.filter_list, self.params, omp_num_threads=self.omp_num_threads)
                l2.run()
                dt = time.time() - t0; pdt = time.process_time() - pt0
                # print(f"[{self.rank}] >>> Fishinsed scan {self.runlist[iscan][0]} ({iscan+1:}/{Nscans}) in {dt/60.0:.1f} minutes. Acceptrate: {np.mean(l2.l2file.acceptrate)*100:.1f}%")
                logging.info(f"[{self.rank}] >>> Fishinsed scan {self.runlist[iscan][0]} ({iscan+1:}/{Nscans}) in {dt/60.0:.1f} minutes. Acceptrate: {np.mean(l2.l2file.acceptrate)*100:.1f}%")

                return_dict = {}
                return_dict["filter_runtimes"] = l2.filter_runtimes
                return_dict["acceptrate"] = l2.l2file.acceptrate
                return_dict["feeds"] = l2.l2file.feeds
                self.comm.send(return_dict, dest=0, tag=DONE_TAG)

    def read_params(self):
        self.params = 0
        if self.rank == 0:
            from l2gen_argparser import parser
            self.params = parser.parse_args()
            if not self.params.runlist:
                raise ValueError("A runlist must be specified in parameter file or terminal.")
            print(f"Filters included: {self.params.filters}")
        self.params = self.comm.bcast(self.params, root=0)

    def configure_logging(self):
        runID = 0
        if self.rank == 0:
            runID = str(datetime.datetime.now())[2:].replace(" ", "-").replace(":", "-")
        runID = self.comm.bcast(runID, root=0)

        self.params.runID = int(runID.replace("-", "").replace(".", ""))
        logfilepath = os.path.join(self.params.log_dir, f"l2gen-{runID}.log")
        logging.basicConfig(filename=logfilepath, filemode="a", format="%(levelname)s %(asctime)s - %(message)s", level=logging.DEBUG)
        if self.rank == 0:
            logging.info(f"Log initialized for runID {runID}")
            print(f"Log initialized for runID {runID}")

    def configure_filters(self):
        self.filter_list = []
        if self.rank == 0:
            for filter_str in self.params.filters:
                filter = getattr(l2gen_filters, filter_str)
                self.filter_list.append(filter)
        self.filter_list = self.comm.bcast(self.filter_list, root=0)

    def read_runlist(self):
        self.runlist = []
        if self.rank == 0:
            self.runlist = read_runlist(self.params, ignore_existing=True)

        self.runlist = self.comm.bcast(self.runlist, root=0)

class l2gen:
    def __init__(self, scan_info, filter_list, params, omp_num_threads):
        self.comm = MPI.COMM_WORLD
        self.Nranks = self.comm.Get_size()
        self.rank = self.comm.Get_rank()
        self.l2file = level2_file(*scan_info, filter_list, params)
        self.filter_list = filter_list
        self.omp_num_threads = omp_num_threads
        self.params = params
        self.verbose = self.params.verbose
        self.filter_names = [filter.name for filter in filter_list]
        self.filter_runtimes = {}
        self.filter_processtimes = {}

    def run(self):
        logging.debug(f"[{self.rank}] Reading level1 data...")
        info = {}
        info["rank"] = self.rank
        info["status"] = "r"
        self.comm.send(info, dest=0, tag=8)
        t0 = time.time(); pt0 = time.process_time()
        self.l2file.load_level1_data()
        t1 = time.time(); pt1 = time.time()
        self.filter_runtimes["l1_read"] = t1 - t0
        self.filter_processtimes["l1_read"] = pt1 - pt0
        logging.debug(f"[{self.rank}] Finished l1 file read in {t1-t0:.1f} s. Process time: {pt1-pt0:.1f} s.")

        if self.params.write_inter_files:
            logging.debug(f"[{self.rank}] Writing pre-filtered data to file...")
            self.l2file.write_level2_data(name_extension="_0")
        for i in range(len(self.filter_list)):
            filter = self.filter_list[i](self.params, omp_num_threads=self.omp_num_threads)
            if self.params.print_progress_bar:
                info = {}
                info["rank"] = self.rank
                info["status"] = filter.name[0]
                self.comm.send(info, dest=0, tag=8)
            logging.debug(f"[{self.rank}] [{filter.name}] Starting {filter.name_long}...")
            t0 = time.time(); pt0 = time.process_time()
            filter.run(self.l2file)
            t1 = time.time(); pt1 = time.process_time()
            logging.debug(f"[{self.rank}] [{filter.name}] Finished {filter.name_long} in {t1-t0:.1f} s. Process time: {pt1-pt0:.1f} s.")
            if self.params.print_progress_bar:
                self.filter_runtimes[self.filter_names[i]] = t1 - t0
                self.filter_processtimes[self.filter_names[i]] = pt1 - pt0
            if self.params.write_inter_files:
                logging.debug(f"[{self.rank}] [{filter.name}] Writing result of {filter.name_long} to file...")
                self.l2file.write_level2_data(name_extension=f"_{str(i+1)}_{filter.name}")
            del(filter)

        logging.debug(f"[{self.rank}] Writing level2 file...")
        t0 = time.time()
        self.l2file.write_level2_data()
        t1 = time.time()
        self.filter_runtimes["l2_write"] = t1 - t0
        self.filter_processtimes["l2_write"] = pt1 - pt0
        logging.debug(f"[{self.rank}] Finished l2 file write.")

if __name__ == "__main__":
    if "OMP_NUM_THREADS" in os.environ:
        omp_num_threads = int(os.environ["OMP_NUM_THREADS"])
    else:
        omp_num_threads = 1
    l2r = l2gen_runner(omp_num_threads=omp_num_threads)
    l2r.run()


a=input("enter a string: ")
b=input('enter string to check: ')
c=a+b

if len(a)!=len(b):
    print('rotation not possible')
else:
    if a in c:
        print('rotation possible')
    else:
        print('no')


import pylab as pl
import numpy as np

def MRF(I, J, eta = 1.5, zeta=1.0):
    ind =np.arange(np.shape(I)[0])
    np.random.shuffle(ind)
    orderx = ind.copy()
    np.random.shuffle(ind)

    for i in orderx:
        for j in ind:
            oldJ = J[i,j]
            J[i,j] = 1
            patch = 0
            for k in range(-1,1):
                for l in range(-1,1):
                    patch += J[i,j] * J[i+k,j+l]
            energya = -eta*np.sum(I*J) - zeta*patch
            J[i,j] = -1 # J[i,j] = -1
            patch = 0
            for k in range(-1,1):
                for l in range(-1,1):
                    patch += J[i,j] * J[i+k,j+l]
            energyb = -eta*np.sum(I*J) - zeta*patch
            if energya<energyb:
                J[i,j] = 1
            else:
                J[i,j] = -1 # J[i,j] = -1
    return J

# ignore region
# aa is data number.

I = dt[aa,:,:].astype(int)

# np.mean(I)
# I = full_to_mask(dt[0,:,:], mask1)['value']
N = np.shape(I)[0]
# I = I[:,:,0]
# I = np.where(I<0.1,-1,1)

pl.title('Original Image')
pl.imshow(I)
# pl.imshow( mask_to_full(I, np.where(template.flatten() == 1)[0], template) )

np.median(noise)
noise = np.random.rand(N,N)
J = I.copy()
ind = np.where(noise < 1 )
J[ind] = -J[ind]
# pl.figure()
pl.title('Noisy image')
pl.imshow(J)
# pl.imshow(mask_to_full(J, np.where(template.flatten() == 1)[0], template))


import argparse

from Loader import Loader
from Model import *
import tensorflow as tf

def parse_args():
    '''
    python Run.py --epochs 100 --batch_size 256 --lr 0.01 --dropout_rate 0.5
    '''
    parser = argparse.ArgumentParser(description="GNN")
    parser.add_argument("--epochs", default=100, type=int)
    parser.add_argument("--batch_size", default=256, type=int)
    parser.add_argument("--lr", default=0.01, type=float)
    parser.add_argument("--dropout_rate", default=0.5, type=float)

    return parser.parse_args()

class Run:
    def __init__(self):
        self.loader = Loader()

    def run_experiment(self, x_train, y_train, args):
        # Compile the model.
        self.gnn_model.compile(
            optimizer=keras.optimizers.Adam(args.lr),
            loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
            metrics=[keras.metrics.SparseCategoricalAccuracy(name="acc")],
        )
        # Create an early stopping callback.
        early_stopping = keras.callbacks.EarlyStopping(
            monitor="val_acc", patience=50, restore_best_weights=True
        )
        # Fit the model.
        history = self.gnn_model.fit(
            x=x_train,
            y=y_train,
            epochs=args.epochs,
            batch_size=args.batch_size,
            validation_split=0.15,
            callbacks=[early_stopping],
        )

        return history

    def run(self,args):
        citations, papers, x_train, x_test, y_train, y_test, self.train_data, self.test_data = self.loader.load_dataset()
        feature_names = self.loader.feature_names
        num_classes = self.loader.num_classes
        graph_info = self.loader.graph_info

        self.gnn_model = GNNNodeClassifier(
            graph_info=graph_info,
            num_classes=num_classes,
            hidden_units=[32,32],
            dropout_rate=args.dropout_rate,
            name="gnn_model",
        )

        # print("GNN output shape:", gnn_model([1, 10, 100]))
        # gnn_model.summary()

        x_train = self.train_data.paper_id.to_numpy()
        history = self.run_experiment(x_train, y_train, args)

    def test(self):
        citations, papers, x_train, x_test, y_train, y_test, self.train_data, self.test_data = self.loader.load_dataset()
        _, test_accuracy = self.gnn_model.evaluate(x=x_test, y=y_test, verbose=0)
        print(f"Test accuracy: {round(test_accuracy * 100, 2)}%")

if __name__=="__main__":

    args=parse_args()
    run = Run()
    run.run(args)

    loader = run.loader
    gnn_model = run.gnn_model

    class_values = loader.class_values
    node_features=loader.node_features
    edges = loader.edges

    # 샘플 데이터
    sample_data = run.test_data.paper_id.iloc[:5]
    def display_class_probabilities(probabilities):
        for instance_idx, probs in enumerate(probabilities):
            print()
            print(f"Instance {instance_idx + 1}:")
            for class_idx, prob in enumerate(probs):
                print(f"- {class_values[class_idx]}: {round(prob * 100, 2)}%")

    logits = gnn_model.predict(tf.constant(sample_data))
    probabilities = keras.activations.softmax(tf.convert_to_tensor(logits)).numpy()
    print("sample test data : ",tf.constant(sample_data))
    display_class_probabilities(probabilities)


import os
import pickle
from pathlib import Path
import pandas as pd

from ml.data import process_data
from ml.model import compute_model_metrics, inference

def slice_metrics(
        model,
        encoder,
        lb,
        data,
        slice_feature,
        categorical_features=[]):
    """
    Output the performance of the model on slices of the data
    Inputs
    ------
    model : Machine learning model
        Trained machine learning model.
    encoder : sklearn.preprocessing._encoders.OneHotEncoder
        Trained sklearn OneHotEncoder, only used if training=False.
    lb : sklearn.preprocessing._label.LabelBinarizer
        Trained sklearn LabelBinarizer, only used if training=False.
    data : pd.DataFrame
        Dataframe containing the features and label.
    slice_feature: str
        Name of the feature used to make slices (categorical features)
    categorical_features: list[str]
        List containing the names of the categorical features (default=[])

    Returns
    -------
    None
    """

    X, y, encoder, lb = process_data(
        data, categorical_features=categorical_features, label="salary", training=False, encoder=encoder, lb=lb)
    preds = inference(model, X)

    with open('slice_output.txt', 'w') as f:
        for slice_value in data[slice_feature].unique():
            slice_index = data.index[data[slice_feature] == slice_value]

            f.write(str(slice_feature)+' = '+str(slice_value)+'\n')
            f.write('data size:{}\n'.format(len(slice_index)))
            f.write('precision: {}, recall: {}, fbeta: {}\n'.format(
                *compute_model_metrics(y[slice_index], preds[slice_index])
            )
            )
            f.write('--------------------------------------------------------------------------------------------'+'\n')


from operator import truediv
import RPi.GPIO as GPIO

pwmLED1 = 26 # Pulse width modulation
led2 = 13
btnPin = 2

GPIO.setwarnings(False)

# Pin setup:
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering
GPIO.setup(led2, GPIO.OUT)
pwm = GPIO.PWM(pwmLED1, 1) # Initialise la led a X hertz par seconde
GPIO.setup(pwmLED1, GPIO.OUT)

# Setup Initial
GPIO.output(led2, GPIO.LOW)
try:
    while True:
        GPIO.output(led2, GPIO.HIGH)
        time.sleep(0.1)
        GPIO.output(led2, GPIO.HIGH)
        time.sleep(0.1)

except KeyboardInterrupt: # Si CTRL-C exit
    GPIO.cleanup() # Réinitialise les pins


import numpy as np
from math import pi
import math
import cmath
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import csv
import matplotlib.pyplot as plt
from torch.utils.data import Dataset,DataLoader
from torchvision import transforms,utils

## GPU加速 ##
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")  # 只能单GPU运行
if(torch.cuda.is_available()):
    print("Use GPU")
else:
    print("Use CPU")

## （一）数据载入  ##
data = np.load("30_Leg_DATA_input.npy")

## （二）数据自动切割对齐标签  ##
b = np.ones(10000)     #真样本标签10000
c = np.zeros(10000)    #假样本标签10000
label = np.concatenate((b,c))  #设置标签20000
#print(label.size)

#切分训练集、验证集、测试集，比例为8:1:1  #
def data_splite(data,label,train_rate,test_rate,eval_rate,Select_fun = 'Train'):
    size = int(data.size/2)
    label_size = int(label.size/2)
    if (Select_fun == 'Train'):
        select_data = np.concatenate((data[0:int(size*train_rate)],data[size:int(size*(1+train_rate))]))
        select_label = np.concatenate((label[0:int(label_size*train_rate)],label[label_size:int(label_size*(1+train_rate))]))
    elif (Select_fun == 'Test'):
        select_data = np.concatenate((data[int(size*train_rate):int(size*(train_rate+test_rate))],data[int(size*(1+train_rate)):int(size*(1+train_rate+test_rate))]))
        select_label = np.concatenate((label[int(label_size*train_rate):int(label_size*(train_rate+test_rate))],label[int(label_size*(1+train_rate)):int(label_size*(1+train_rate+test_rate))]))
    elif (Select_fun == 'Eval'):
        select_data = np.concatenate((data[int(size*(1 - eval_rate)):size],data[int(size*(2 - eval_rate)):2*size]))
        select_label = np.concatenate((label[int(label_size*(1 - eval_rate)):label_size],label[int(label_size*(2 - eval_rate)):2*label_size]))
    return select_data,select_label

class MyDataset(Dataset):
    def __init__(self,data,label,transform = None): #传入信号的生成数据，标签自己生成
        #存入的数据中标签部分为传入的data
        super(MyDataset,self).__init__()
        self.data = data
        self.label = label
        self.transform = transform
        #self.target_transform = target_transform

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

    def __getitem__(self, index):
        signal = self.data[800*index:800*index+800]
        target = self.label[index]
        return signal,target

train_data,train_label = data_splite(data,label,train_rate = 0.8,eval_rate = 0.1,test_rate = 0.1,Select_fun = 'Train')
test_data,test_label = data_splite(data,label,train_rate = 0.8,eval_rate = 0.1,test_rate = 0.1,Select_fun = 'Test')
eval_data,eval_label = data_splite(data,label,train_rate = 0.8,eval_rate = 0.1,test_rate = 0.1,Select_fun = 'Eval')
#dataset
train_dataset = MyDataset(data = train_data,label = train_label,transform = transforms.Compose([transforms.Resize(4,100),transforms.ToTensor()]))
test_dataset = MyDataset(data = test_data,label = test_label,transform = transforms.Compose([transforms.Resize(4,100),transforms.ToTensor()]))
eval_dataset = MyDataset(data = eval_data,label = eval_label,transform = transforms.Compose([transforms.Resize(4,100),transforms.ToTensor()]))
#dataloader
batch_size = 100
train_loader = DataLoader(dataset = train_dataset, batch_size = batch_size, shuffle=True,num_workers = 8)
test_loader = DataLoader(dataset = test_dataset, batch_size = batch_size, shuffle=True,num_workers = 8)
eval_loader = DataLoader(dataset = eval_dataset, batch_size = batch_size, shuffle=True,num_workers = 8)

#testloader_size = int(test_label.size)
#print("test_loader_size:",int(test_label.size))

## （三）合法接收机分类器的网络架构 ##
class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(800, 200)
        self.fc2 = nn.Linear(200, 100)
        self.dropout = torch.nn.Dropout(p=0.6)
        # self.fc3 = nn.Linear(100, 100)
        self.fc4 = nn.Linear(100,1)

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.dropout(x)
        # x = F.relu(self.fc3(x))
        #x = F.softmax(self.fc4(x),dim=0)
        x = F.sigmoid(self.fc4(x))
        return x
net = Net().to(device)

## （四） 训练网络  ##
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)

#net.load_state_dict(torch.load("0.953Leg_receiver_net.pkl", map_location=device))

num_epoch = 15
loss = 0
criterion = nn.BCELoss()  #交叉熵函数
for epoch in range(num_epoch):
    batch_num = 0
    net.train()
    for signal,target in train_loader:
        batch_num = batch_num + 1
        #input = torch.tensor(signal, dtype=torch.float32).to(device)
        input = signal.float().to(device)
        target = target.to(device)
        out = net(input)
        loss = criterion(out,target)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        if(batch_num)%160 == 0:
            print("Epoch[{}/{}],loss:{:.6f}".format(epoch+1,num_epoch,loss.data))
    #print("batch_num:",batch_num)
    #查看验证集的acc变换情况
    net.eval()
    eval_correct = 0
    for signal,target in eval_loader:
        input = signal.float().to(device)
        target = torch.FloatTensor(target).to(device)
        out = net(input)
        pred = (out>0.5).float()
        eval_correct += pred.eq(target.view_as(pred)).sum().item()
    print("Epoch[{}/{}],eval_acc = {:.4f}".format(epoch+1,num_epoch,eval_correct/len(eval_loader.dataset)))
test_correct = 0
for signal,target in test_loader:
    input = signal.float().to(device)
    target = torch.FloatTensor(target).to(device)
    out = net(input)
    pred = (out>0.5).float()
    test_correct += pred.eq(target.view_as(pred)).sum().item()
print("test_acc ={:.4f}".format(test_correct/len(test_loader.dataset)))

corrected = test_correct/len(test_loader.dataset)
if corrected > 0.9 or corrected <0.85:
    torch.save(net.state_dict(), '{}Leg_receiver_net.pkl'.format(corrected))


# vim: sw=4 sts=4 et fileencoding=utf8 nomod

r'''Data model - contextual data.
'''

from sixx.world import World
from sixx.input import InputError
from sixx.node import *
from sixx.links import *

__all__ = ['Data', 'Has_context',
           'Data_factory_place', 'Data_factory_nocontext',
           'Data_factory_context',
          ]

class Data(Node):

    r'''An atom of contextual data consists of a Unicode ID string, and a value
    string.  These, combined with the 'context' node to which the data is
    linked, uniquely identifies the atom.'''

    def __init__(self, context, id, value, timestamp=None):
        assert isinstance(id, str)
        assert isinstance(value, str)
        super(Data, self).__init__()
        self.id = id
        self.value = value
        Has_context(self, context, timestamp=timestamp)

    def context(self):
        return next(self.nodes(outgoing & is_link(Has_context)))

    def key(self):
        return self.context(), self.id

    def __str__(self):
        return ' = '.join((self.id, self.value))

class Has_context(Link):

    r'''Connect a Data node to its context node.  Exactly one of these per Data
    node.
    '''

    def __init__(self, data, context, timestamp=None):
        from sixx.links import Is_in
        from sixx.person import Person
        from sixx.family import Family
        from sixx.org import Organisation
        assert isinstance(data, Data)
        assert isinstance(context, (Association, Is_in, Person, Family, Organisation))
        super(Has_context, self).__init__(data, context, timestamp=timestamp)
        self.data = data
        self.context = context

class Data_factory_place(object):

    r'''A Data factory that makes data nodes with a context of a given place.
    '''

    def __init__(self, place):
        self._place = place

    def __call__(self, part, who, id, value):
        from sixx.links import Is_in
        if isinstance(who, Link):
            raise InputError('place-contextual data in a non-place context',
                             line=value)
        if (hasattr(part, 'place') and part.place is not None and
            part.place != self._place):
            raise InputError('invalid data place %s - should be %s' %
                             (part.place, self._place), line=value)
        context = who.link(is_in_place(self._place))
        if not context:
            context = Is_in(who, self._place)
        return Data(context, id, value, timestamp=part.updated)

class Data_factory_nocontext(object):

    r'''A Data factory that makes data nodes with no context.
    '''

    def __call__(self, part, who, id, value):
        if isinstance(who, Link):
            raise InputError('non-contextual data in a context',
                             line=value)
        if hasattr(part, 'place') and part.place is not None:
            raise InputError('non-contextual data in a place context',
                             line=value)
        return Data(who, id, value, timestamp=part.updated)

class Data_factory_context(object):

    r'''The default Data factory that makes data nodes with whatever context it
    is told to use.
    '''

    def __call__(self, part, context, id, value):
        from sixx.links import Is_in
        if not isinstance(context, Link):
            place = part.place or context.only_place()
            if not place:
                raise InputError('data context (place) unknown',
                                 line=value)
            is_in = context.link(is_in_place(place))
            if not is_in:
                is_in = Is_in(context, place)
            context = is_in
        return Data(context, id, value, timestamp=part.updated)


friends = ['zhengke','keke','guaiwan','doudou','jiji','segun','banzhang']
# jiji 无法参加聚会，但是xiaoshizi可以来
friends[4] = 'xiaoshizi'
#还想邀请两位女士，分别添加到列表的开始和末尾
friends.insert(0,'yuehua')
friends.append('gcyryr')
for i in range(len(friends)-2):
    friend = friends.pop()
    print('sorry',friend+'由于位置有限，不能和你共进晚餐了')
print('{}我们依旧可以共进晚餐'.format(friends[0]))
print('{}我们依旧可以共进晚餐'.format(friends[1]))
print(friends)
del friends[0]
del friends[0]
print(friends)


import queue
import numpy as np
from collections import deque

def write_sol(out, num):
    f = open(f'output_{num}.txt', 'w')
    f.write(f'{out}\n')
    f.close()

def read_input():
    f = open("input.txt", "r")
    data = f.read().splitlines()
    f.close()
    return data

def cal_x(start, steps):
    slowest = max(start - steps, 0)
    return ((start * (start + 1)) - (slowest * (slowest + 1))) // 2

def cal_y(start, steps):
    velo_dist = steps * start
    gravity = (steps * (steps - 1)) // 2
    return velo_dist - gravity

def binsearch_xmin(param, steps):
    left = 0
    right = param
    while left != right:
        start = (left + right) // 2
        if cal_x(start, steps) < param:
            left = start + 1
        else:
            right = start
    return left

def binsearch_ymin(param, steps):
    # assuming param negative
    left = param
    right = steps
    while left != right:
        start = (left + right) // 2
        if cal_y(start, steps) < param:
            left = start + 1
        else:
            right = start
    return left

def binsearch_xmax(param, steps):
    left = 0
    right = param
    while left != right:
        start = (left + right + 1) // 2
        if cal_x(start, steps) > param:
            right = start - 1
        else:
            left = start
    return left

def binsearch_ymax(param, steps):
    # assuming param negative
    left = param
    right = steps
    while left != right:
        start = (left + right + 1) // 2
        if cal_y(start, steps) > param:
            right = start - 1
        else:
            left = start
    return left

def psbl_for_steps(steps, target_area):
    x_min = binsearch_xmin(target_area[0][0], steps)
    x_max = binsearch_xmax(target_area[0][1], steps)
    y_min = binsearch_ymin(target_area[1][0], steps)
    y_max = binsearch_ymax(target_area[1][1], steps)
    if cal_y(y_max+1, steps) <= target_area[1][1] or cal_y(y_min-1, steps) >= target_area[1][0] \
            or cal_x(x_max+1, steps) <= target_area[0][1] or cal_x(x_min-1, steps) >= target_area[0][0]:
        pass
    if cal_y(y_max, steps) > target_area[1][1] or cal_y(y_min, steps) < target_area[1][0] \
            or cal_x(x_max, steps) > target_area[0][1] or cal_x(x_min, steps) < target_area[0][0]:
        pass
    if x_min <= x_max and y_min <= y_max:
        return [(i, j) for i in range(x_min, x_max+1) for j in range(y_min, y_max+ 1)]
    else:
        return []

def task17():
    data = read_input()[0]
    ## data = 'target area: x=20..30, y=-10..-5'
    data = data[15:].split(', y=')
    target_area = [[int(s) for s in line.split('..')] for line in data]
    print(target_area)

    max_y = 109
    max_up = 109 * 110 // 2
    print(max_up)
    write_sol(max_up, 1)

    psbl_starts = []
    for steps in range(1, 250):
        psbl_starts += psbl_for_steps(steps, target_area)

    psbl_starts = set(psbl_starts)
    print(len(psbl_starts))
    write_sol(len(psbl_starts), 2)

task17()


from prac_08.silver_service_taxi import SilverServiceTaxi
from prac_08.taxi import Taxi

Menu = """q)uit, c)hoose taxi, d)rive"""

def main():
    """Write a program using SilverServiceTaxi and Taxi classes.
     Allow users choose type of taxi and how far they want to drive,
    Display the trip cost and add it to their bill when user choose to quit."""
    total_fare = 0
    taxis = [Taxi("Prius", 100), SilverServiceTaxi("Limo", 100, 2), SilverServiceTaxi("Hummer", 200, 4)]

    print("Let's drive")
    print(Menu)
    choice = input(">>> ").lower()
    while choice != "q":
        if choice == "c":
            print("Taxis available:")
            display_taxis(taxis)
            taxi_choice = int(input("Choose taxi: "))
            current_taxi = taxis[taxi_choice]
        elif choice == "d":
            current_taxi.start_fare()
            driving_distance = float(input("Drive how far? "))
            current_taxi.drive(driving_distance)
            trip_fare = current_taxi.get_fare()
            print("Your {} trip cost you ${:.2f}".format(current_taxi.name, trip_fare))
            total_fare += trip_fare
        else:
            print("Invalid option")
        print("Bill to date: ${:.2f}".format(total_fare))
        print(Menu)
        choice = input(">>> ").lower()
    print("Total trip cost: ${:.2f}".format(total_fare))
    print("Taxis are now:")
    display_taxis(taxis)

def display_taxis(taxis):
    """Display list of taxis."""
    for i, taxi in enumerate(taxis):
        print("{} - {}".format(i, taxi))

main()


from workers.data_scraper.scraper_dormitory.scraping_default_usage import Scraper as ABCScraper
from workers.data_scraper.scraper_dormitory.scraper_tools.tools import *
from .parser import *

# 채널 이름 : 시청자미디어재단

#HTTP Request
'''
    @post list
    method : GET
    url_0 = https://kcmf.or.kr/cms/board/board_list.php?page={page_count}&btype=cms_notice&menuIdx=19
    header :
        1. Accept-Language: ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6
'''
'''
    @post info
    method : GET
    url :
        self.post_url + href
    header :
        None
'''
sleep_sec = 1
is_update = True

class Scraper(ABCScraper):
    def __init__(self, session):
        super().__init__(session)
        self.channel_name = '시청자미디어재단'
        self.post_board_name = '공지사항'
        self.post_url = 'https://kcmf.or.kr/cms/board'

    def scraping_process(self, channel_code, channel_url, dev, full_channel_code):
        super().scraping_process(channel_code, channel_url, dev, full_channel_code)
        self.additional_key_value.append(("Accept-Language", "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6"))
        self.session = set_headers(self.session, self.additional_key_value, is_update)
        self.page_count = 1
        while True :
            self.channel_url = self.channel_url_frame.format(self.page_count)
            self.post_list_scraping()
            if self.scraping_target :
                self.target_contents_scraping()
                self.collect_data()
                self.mongo.reflect_scraped_data(self.collected_data_list)
                self.page_count += 1
            else :
                break

    def post_list_scraping(self):
        super().post_list_scraping(post_list_parsing_process, 'get', sleep_sec)

    def target_contents_scraping(self):
        super().target_contents_scraping(post_content_parsing_process, sleep_sec)


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

class Solution(object):
    def largestValues(self, root):
        return self.findValueMostElement(root)

    def findValueMostElement(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        if not root:
            return []
        rows = self.getRows(root)
        return [max(x) for x in rows]

    def getRows(self, root):
        ans = []
        temp = [root]
        while temp:
            cur = temp[:]
            temp = []
            for x in cur:
                if x.left:
                    temp += x.left,
                if x.right:
                    temp += x.right,
            ans += [x.val for x in cur],
        return ans


"""
This script transforms the tree data within the LiDAR dataset. It divides the tree data into Minecraft chunks,
and performs a horizontal mean shift clustering algorithm with vertical strata analysis to determine the tree
trunk locations. The tree trunk locations are then saved both into the world and to an array, where we'll use the trunks
and the corresponding leaves to cluster the leaves and place branches outwards from the trunks.
"""

import time

import amulet
import numpy as np
from amulet import Block
from amulet.utils import block_coords_to_chunk_coords
from amulet_nbt import StringTag
from sklearn.cluster import MeanShift, DBSCAN
from tqdm import tqdm

import scripts.helpers

MIN_BIN_FREQUENCY = 4
TRUNK_BLOCK = Block("minecraft", "spruce_log")
LEAVES_BLOCK = Block("minecraft", "spruce_leaves", {"persistent": StringTag("true")})
TALL_VEGETATION_LABEL = 5

def transform_dataset(ds, start_time):
    """
    Transforms the given dataset by filtering out non-tree elements, dividing into chunks,
    and calling the chunk processor on each chunk.
    :param ds: the LiDAR dataset to transform
    :param start_time: the start time of the program
    :return: None
    """
    level = amulet.load_level(scripts.helpers.WORLD_DIRECTORY)
    max_x, max_z, min_x, min_z, x, y, z = scripts.helpers.preprocess_dataset(ds, TALL_VEGETATION_LABEL)

    trunk_block_universal, _, _ = level.translation_manager.get_version("java", (1, 19, 4)).block.to_universal(
        TRUNK_BLOCK)
    trunk_block_id = level.block_palette.get_add_block(trunk_block_universal)
    leaves_block_universal, _, _ = level.translation_manager.get_version("java", (1, 19, 4)).block.to_universal(
        LEAVES_BLOCK)
    leaves_block_id = level.block_palette.get_add_block(leaves_block_universal)

    print("Finished preprocessing. Starting chunk processing.", time.time() - start_time)

    for ix in tqdm(range(min_x.astype(int), max_x.astype(int), 16)):
        for iz in range(min_z.astype(int), max_z.astype(int), 16):
            chunk_indices = np.where((x >= ix) & (x < ix + 16) & (z >= iz) & (z < iz + 16))
            cx, cz = block_coords_to_chunk_coords(ix, iz)
            if len(chunk_indices[0]) == 0:
                continue
            chunk = level.get_chunk(cx, cz, "minecraft:overworld")
            chunk = handle_chunk(chunk, x[chunk_indices], y[chunk_indices], z[chunk_indices], trunk_block_id,
                                 leaves_block_id)
            level.put_chunk(chunk, "minecraft:overworld")
    print("Time taken: " + str(time.time() - start_time))
    level.save()
    level.close()
    print("Saved: " + str(time.time() - start_time))

def handle_chunk(chunk, x, y, z, trunk_block_id, leaves_block_id):
    """
    Handles the given chunk by performing the horizontal mean shift clustering algorithm with vertical strata analysis
    to determine the tree trunk locations. The tree trunk locations are then saved both into the world and to an array,
    where we'll use the trunks and the corresponding leaves to cluster the leaves and place branches outwards from the
    trunks.
    :param chunk: the chunk to handle
    :param x: the x data within the chunk
    :param y: the y data within the chunk
    :param z: the z data within the chunk
    :param trunk_block_id: the block ID of the trunk block
    :param leaves_block_id: the block ID of the leaves block
    :return: the chunk with the tree trunks, branches, and leaves placed
    """

    # First, we need to do some data pre-processing. Right now, the level object has the Minecraft world data,
    # and that is effectively the DEM of the surface. As a result, we need to convert the height of each point into
    # an above-ground height; i.e. subtracting the current height of the surface at that point from the height of the
    # point.
    cx, cz = chunk.cx, chunk.cz
    original_y = y
    ground_heights = np.zeros((16, 16))
    for ix in range(16):
        for iz in range(16):
            indices = np.where((x >= cx * 16 + ix) & (x < cx * 16 + ix + 1) & (z >= cz * 16 + iz) & (
                    z < cz * 16 + iz + 1))
            column = chunk.blocks[ix, scripts.helpers.MIN_HEIGHT:scripts.helpers.MAX_HEIGHT, iz]
            column = np.array(column).flatten()
            non_air_indices = np.where(column != 0)
            if len(non_air_indices[0]) == 0:
                return chunk
            dem_height = np.max(non_air_indices) + scripts.helpers.MIN_HEIGHT
            y[indices] -= dem_height
            ground_heights[ix, iz] = dem_height
    # We should remove outliers now. Any point with a height of less than or equal to 2 is too close to the ground to be
    # useful to us. This does impact hedges a bit - as those are classified as trees for some reason - but that is only
    # an issue off-campus.
    delete_indices = np.where(y <= 2)
    x, y, z = np.delete(x, delete_indices), np.delete(y, delete_indices), np.delete(z, delete_indices)
    original_y = np.delete(original_y, delete_indices)
    if len(x) == 0 or len(y) == 0 or len(z) == 0:
        return chunk

    # The study we're basing this off of also removed points with an above-ground height greater than three standard
    # deviations above the mean. We don't need to do the same, as the LiDAR dataset we're working with has sufficient
    # denoising already done.

    # Now the horizontal mean shift clustering means that instead of providing the heights of the points, we need to
    # just provide the x and z axes. The MeanShift algorithm itself performs the binning and density estimation.

    chunk = place_leaves(chunk, cx, cz, ground_heights, leaves_block_id, x, y, z)
    try:
        ms = MeanShift(bin_seeding=True, min_bin_freq=MIN_BIN_FREQUENCY, cluster_all=False, max_iter=127, n_jobs=6)
        ms.fit(np.array([x, z]).T)
        ms_labels = ms.labels_
        cluster_centers = ms.cluster_centers_
    except ValueError:
        # We should only perform mean shift if the number of points is enough to actually perform the algorithm.
        # Otherwise, we will just return the chunk with the leaves placed.
        return chunk

    # Now we need to do the vertical strata analysis.
    crown_clusters, non_ground_points, tree_cluster_centers, tree_clusters = vertical_strata_analysis(cluster_centers,
                                                                                ms_labels,
                                                                                x, y, z)

    # Now we need to assign the crown clusters to the nearest tree cluster
    # If there are no tree clusters then we can just skip this step
    if len(tree_clusters) > 0:
        for cluster in crown_clusters:
            cluster_indices = np.where(ms_labels == cluster)
            cluster_x, cluster_y, cluster_z = non_ground_points[cluster]
            for point in np.array([cluster_x, cluster_y, cluster_z]).T:
                # find the nearest tree cluster
                comparison_point = np.array([point[0], point[2]])
                nearest_cluster = tree_clusters[
                    np.argmin(np.linalg.norm(np.array(tree_cluster_centers) - comparison_point, axis=1))]
                # assign the point to that cluster
                ms_labels[cluster_indices] = nearest_cluster
        # re-calculating the cluster centers and calculating the height of each tree
        cluster_centers = []
        cluster_heights = []
        for cluster in np.unique(ms_labels):
            cluster_indices = np.where(ms_labels == cluster)
            cluster_x, cluster_y, cluster_z = x[cluster_indices], y[cluster_indices], z[cluster_indices]
            cluster_centers.append(np.array([np.average(cluster_x), np.average(cluster_z)]))
            cluster_heights.append(np.max(cluster_y))
        cluster_centers = np.array(cluster_centers)
        cluster_heights = np.array(cluster_heights)

        # let's place the trunks into the world
        for cluster_center, cluster_height in zip(cluster_centers, cluster_heights):
            ix, iz = int(cluster_center[0] - cx * 16), int(cluster_center[1] - cz * 16)
            dem_height = ground_heights[ix, iz].astype(int)
            column = chunk.blocks[ix, scripts.helpers.MIN_HEIGHT:scripts.helpers.MAX_HEIGHT, iz]
            column = np.array(column).flatten()
            leaf_block_indices = np.where(column == leaves_block_id)
            if len(leaf_block_indices[0]) > 0:
                max_leaf_height = np.max(leaf_block_indices) + scripts.helpers.MIN_HEIGHT
                chunk.blocks[ix, int(dem_height):max_leaf_height, iz] = trunk_block_id
        # Now it's time to create tree branches. The best way to do this is via DBSCAN. This will be done per tree,
        # so we'll need to iterate over each tree cluster and call a helper function to do the DBSCAN on it and
        # parameterize the lines. That function will return a tuple of vertices for each branch line - One point on
        # the tree trunk, and the cluster center.
        chunk = create_branches(x, original_y, z, ms_labels, cluster_centers, cluster_heights, chunk, trunk_block_id,
                                leaves_block_id, ground_heights)

    return chunk

def place_leaves(chunk, cx, cz, ground_heights, leaves_block_id, x, y, z):
    for x_point, y_point, z_point in zip(x, y, z):
        if y_point <= 2:
            continue
        ix, iz = int(x_point - cx * 16), int(z_point - cz * 16)
        chunk.blocks[ix, int(y_point + ground_heights[ix, iz]), iz] = leaves_block_id
    chunk.changed = True
    return chunk

def create_branches(x, y, z, ms_labels, cluster_centers, cluster_heights, chunk, trunk_block_id, leaves_block_id,
                    dem_heights):
    """
    This function creates the branches for each tree in the chunk through a combination of DBSCAN and constrained
    optimization.
    :param x: array of the LiDAR x coordinates in the chunk
    :param y: array of the LiDAR y coordinates in the chunk
    :param z: array of the LiDAR z coordinates in the chunk
    :param ms_labels: array of labels for each point corresponding to the cluster it belongs to
    :param cluster_centers: centers of each tree trunk (x and z coordinates)
    :param cluster_heights: heights of each tree trunk
    :param chunk: the chunk object that we are editing
    :param trunk_block_id: the block id of the trunk
    :param leaves_block_id: the block id of the leaves
    :param dem_heights: the heights of the DEM at each point in the chunk
    :return: The chunk object with the branches added.
    """
    cx, cz = chunk.cx, chunk.cz
    for cluster, cluster_center, cluster_height in zip(np.unique(ms_labels), cluster_centers, cluster_heights):
        cluster_indices = np.where(ms_labels == cluster)
        cluster_x, cluster_y, cluster_z = x[cluster_indices], y[cluster_indices], z[cluster_indices]
        # now we need to perform DBSCAN on the cluster.
        min_samples = 7  # this is the minimum number of points required to form a branch. We can change this later.
        epsilon = 2
        if len(cluster_x) > min_samples:
            # we want to perform a 3d DBSCAN on the cluster
            cluster_points = np.array([cluster_x, cluster_y, cluster_z]).T
            db = DBSCAN(eps=epsilon, min_samples=min_samples, metric='euclidean', n_jobs=6).fit(cluster_points)
            labels = db.labels_
            core_samples = db.core_sample_indices_
            # Now for every unique label that's not -1, we want to get the core points that correspond to it
            # Those core points are what we'll use to calculate the cluster center.
            core_sample_labels = labels[core_samples]
            tree_height = dem_heights[int(cluster_center[0] - cx * 16), int(cluster_center[1] - cz * 16)]
            for label in np.unique(core_sample_labels):
                centroid = np.average(cluster_points[np.where(core_sample_labels == label)], axis=0)
                points_in_cluster = cluster_points[np.where(labels == label)]  # these are the points that we want to
                # use in our constrained optimization
                # We will use a more simplified version of the optimization problem given we will eventually be
                # truncating to Minecraft blocks anyway. This will save a lot of time.
                # As such we just need to draw a line between the centroid and the trunk (with the height on the trunk
                # being the variable to optimize)
                distances = []
                for height in np.arange(centroid[2] - 2, centroid[2] + 2, 1):  # Even more constrained for speed
                    # the first vertex is just the centroid itself
                    # the second vertex is the trunk point
                    trunk_point = np.array([cluster_center[0], height, cluster_center[1]])
                    # now it's time to calculate the distance between all the points in the cluster and the line
                    # between the trunk point and the centroid
                    distance = np.linalg.norm(np.cross(points_in_cluster - trunk_point, points_in_cluster - centroid),
                                              axis=1) / np.linalg.norm(centroid - trunk_point)
                    distances.append(np.sum(distance))
                best_height = np.argmin(distances)
                offset_x, offset_z = -cx * 16, -cz * 16
                # The max/min bounding below is resultant from debugging out-of-bounds errors. Not sure if they're
                # necessary anymore.
                cluster_center_x = max(0, min(int(cluster_center[0] + offset_x), 15))
                cluster_center_z = max(0, min(int(cluster_center[1] + offset_z), 15))
                centroid_x = max(0, min(int(centroid[0] + offset_x), 15))
                centroid_z = max(0, min(int(centroid[2] + offset_z), 15))
                branch_blocks = scripts.helpers.bresenham_3d(cluster_center_x, int(best_height + tree_height),
                                                             cluster_center_z, centroid_x,
                                                             int(centroid[1] + tree_height), centroid_z)
                if len(branch_blocks) > 7:  # Removing long branches due to the inherent errors in selecting a maximum
                    # window size for the horizontal mean shift clustering: crown clusters are assigned to the closest
                    # tree *in the same chunk* which may not be the actual closest tree.
                    continue
                for block in branch_blocks:
                    column = chunk.blocks[block[0], scripts.helpers.MIN_HEIGHT:scripts.helpers.MAX_HEIGHT, block[2]]
                    column = np.array(column).flatten()
                    leaf_block_indices = np.where(column == leaves_block_id)
                    if len(leaf_block_indices[0]) > 0:  # if there exists a leaf block in the column, we're under a tree
                        # and it makes sense to place a branch. Otherwise, it was an error, and we shouldn't place one.
                        # Again a resultant error from large constraints placed on the horizontal mean shift clustering.
                        chunk.blocks[block[0], block[1], block[2]] = trunk_block_id
                chunk.changed = True

    return chunk

def vertical_strata_analysis(cluster_centers, meanshift_labels, x, y, z):
    """
    This function performs the vertical strata analysis on the clusters to determine which clusters are crown clusters
    :param cluster_centers: mean shift cluster centers
    :param meanshift_labels: array of labels for each point
    :param x: Array of x coordinates in the chunk
    :param y: Array of y coordinates in the chunk (height)
    :param z: Array of z coordinates in the chunk
    :return: crown_clusters (list of cluster indices), non_ground_points (list of arrays of non-ground points),
    tree_cluster_centers (list of tree cluster centers), tree_clusters (list of tree cluster indices)
    """
    non_ground_points = []
    crown_clusters = []
    crown_cluster_centers = []
    tree_clusters = []
    tree_cluster_centers = []
    for cluster in np.unique(meanshift_labels):
        # if the label is -1, continue
        if cluster == -1:
            continue
        cluster_indices = np.where(meanshift_labels == cluster)
        cluster_x, cluster_y, cluster_z = x[cluster_indices], y[cluster_indices], z[cluster_indices]
        vertical_gap_y = np.max(cluster_y) * 0.3  # this 0.3 is based on the study
        non_ground_indices = np.where(cluster_y > vertical_gap_y)
        # we can ignore the ground residuals now. We just want to keep the crown and stem points
        non_ground_points.append(
            np.array([cluster_x[non_ground_indices], cluster_y[non_ground_indices], cluster_z[non_ground_indices]]))

        # Now we need to determine if this is a crown cluster or a tree cluster. To do this we analyze the vertical
        # length ratio of the cluster.
        vlr = (np.max(cluster_y) - np.min(cluster_y)) / np.max(cluster_y)
        # A high VLR cluster is a tree cluster, a low VLR cluster is a crown cluster
        cutoff = 0.62  # Modified cutoff value from the study to try and get more tree clusters given the fact that we
        # a. are taking into account less data points, and b. are limiting the meanshift to a per-chunk basis
        if vlr < cutoff:
            crown_clusters.append(cluster)  # crown cluster. We need to assign these points to the nearest tree
            # cluster later.
            crown_cluster_centers.append(cluster_centers[cluster])
        else:
            tree_clusters.append(cluster)
            tree_cluster_centers.append(cluster_centers[cluster])

    return crown_clusters, non_ground_points, tree_cluster_centers, tree_clusters

if __name__ == "__main__":
    finished_datasets = ["480000_5454000.las", "480000_5455000.las", "480000_5456000.las", "480000_5457000.las",
                         "481000_5454000.las", "481000_5455000.las", "481000_5456000.las", "481000_5457000.las",
                         "481000_5458000.las", "482000_5454000.las", "482000_5455000.las", "482000_5456000.las",
                         "482000_5457000.las", "482000_5458000.las", "483000_5454000.las", "483000_5455000.las",
                         "483000_5456000.las", "483000_5457000.las"]
    scripts.helpers.dataset_iterator(transform_dataset, finished_datasets)


import logging

class OutStream:

    INFO = 1
    WARNING = 2
    DEBUG = 3
    ERROR = 4
    CRITICAL = 5

    def __init__(self,module_name_str, log_filename_str, screen_out_bool = True):
        self.screen_out=screen_out_bool
        self.log_fn=log_filename_str
        self.logger=logging.getLogger(module_name_str)
        self.logger.setLevel(logging.DEBUG)
        formatter= logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
        logfile_h=logging.FileHandler(log_filename_str)
        logfile_h.setFormatter(formatter)
        self.logger.addHandler(logfile_h)
        if screen_out_bool:
            console_h = logging.StreamHandler()
            console_h.setFormatter(formatter)
            self.logger.addHandler(console_h)

    def outs(self, str_to_output,type=INFO):
        if type==OutStream.INFO:
            self.logger.info(str_to_output)
        elif type==OutStream.WARNING:
            self.logger.warning(str_to_output)
        elif type==OutStream.DEBUG:
            self.logger.debug(str_to_output)
        elif type==OutStream.ERROR:
            self.logger.error(str_to_output)
        elif type==OutStream.CRITICAL:
            self.logger.critical(str_to_output)


class Solution:
    # TC: O(n)
    # SC: O(1)
    def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
        if not intervals:
            return True

        intervals = sorted(intervals, key = lambda x: (x[0], x[1]))
        prevEnd = intervals[0][1]

        for idx in range(1, len(intervals)):
            start, end = intervals[idx]

            if start < prevEnd:
                return False
            else:
                prevEnd = end

        return True


from torch import nn as  nn
from torchvision import models
from torchsummary import summary
import torch

class Network(nn.Module):
    def __init__(self,num_class):
        super(Network,self).__init__()
        self.model = models.resnet101(pretrained=True)
        num_ftrs = self.model.fc.in_features
        self.model.fc = nn.Linear(num_ftrs,num_class)
    def forward(self,x):
        x = self.model(x)
        return x

if __name__ == "__main__":
    device = "cuda" if torch.cuda.is_available() else "cpu"
    net = Network(4).to(device)
    # input =  torch.rand((3,256,256),dtype=torch.float32).to(device)
    summary(net,input_size=(3,256,256))


from commands.bot_status import find_chat, find_command
from commands import functions
from commands.define import commands, prefix

def get_command_func(message_content):
    msg_chunks = [msg_chunk.lower() for msg_chunk in message_content.split()]
    for comm_func, comm_v in commands.items():
        for comm_msg in comm_v['message']:
            comm_msg = prefix + comm_msg
            try:
                msg_idx = msg_chunks.index(comm_msg)
            except ValueError:
                continue
            param_len = comm_v['parameters']
            if param_len == -1:
                parameters = msg_chunks[msg_idx + 1:]
            else:
                parameters = msg_chunks[msg_idx + 1: msg_idx + param_len + 1]
            if len(parameters) < param_len or len(parameters) == 0 and param_len == -1:
                return {
                    'function': functions.bot_help,
                    'parameters': [],
                }
            return {
                'function': getattr(functions, comm_func),
                'parameters': parameters,
            }
    return {
        'function': None,
        'parameters': [],
    }

async def execute_command(client, message, logger, msg_id):
    if message.author.bot:
        return None
    comm_func = get_command_func(message.content)
    find_chat(message.channel)
    if comm_func['function'] is None:
        logger.debug('<msg_id> Cannot found function'.format(msg_id=msg_id))
        return None
    else:
        find_command(message.channel)
        logger.debug('<msg_id> Get command function: function: {func}, parameters: {param}'.format(
            msg_id=msg_id,
            func=comm_func['function'],
            param=comm_func['parameters']
        ))
    return await comm_func['function'](client, message, comm_func['parameters'], logger, msg_id)


qty = [6,2,5,5,4,5,6,3,7,6]

T = int(input())
list=[]

for i in range(T):
    length = input()
    total = 0
    for j in length:
        x = int(j)
        total = total + int(qty[x])
    if total%2 != 0:
        number = (total-3)//2
        finalnumber = "7" + "1"*number
    else:
        number = total//2
        finalnumber = "1"*number
    list.append(finalnumber)

for a in range(0,len(list)):
	print (list[a])


import superdesk
from superdesk.errors import AlreadyExistsError

registered_search_providers = {}
allowed_search_providers = []

def init_app(app):
    # Must be imported here as the registered_search_providers is referenced by the below modules
    from .resource import SearchProviderResource
    from .service import SearchProviderService

    endpoint_name = 'search_providers'
    search_provider_service = SearchProviderService(endpoint_name, backend=superdesk.get_backend())
    SearchProviderResource(endpoint_name, app=app, service=search_provider_service)

    superdesk.privilege(name='search_providers', label='Manage Search Providers',
                        description='User can manage search providers.')

def register_search_provider(name, fetch_endpoint):
    """
    Registers a Search Provider with the given name and fetch_endpoint. Both have to be unique and if not raises
    AlreadyExistsError. The fetch_endpoint is used by clients to fetch the article from the Search Provider.

    :param name: Search Provider Name
    :type name: str
    :param fetch_endpoint: relative url to /api
    :type fetch_endpoint: str
    :raises: AlreadyExistsError - if a search has been registered with either name or fetch_endpoint.
    """

    if name in registered_search_providers:
        raise AlreadyExistsError("A Search Provider with name: {} already exists".format(name))

    if fetch_endpoint in registered_search_providers.values():
        raise AlreadyExistsError("A Search Provider for the fetch endpoint: {} exists with name: {}"
                                 .format(fetch_endpoint, registered_search_providers[name]))

    registered_search_providers[name] = fetch_endpoint
    allowed_search_providers.append(name)


# -*- coding: utf-8 -*-
"""
Created on Tue Oct 30 14:29:41 2018

@author: alex
"""

import numpy as np
import matplotlib.pyplot as plt
#%% CREA UNA MATRIZ 5*7
mat_completa = np.random.randint(100, size = (10, 10))
fil_mat = mat_completa.shape[0]
col_mat = mat_completa.shape[1]
#print("matriz completa \n",mat_completa)
plt.matshow(mat_completa)
plt.show()
#print("filas de la matrix",fil_mat)
#print("cols de la matrix",col_mat)
###############################################################################CREA UNA MATRIZ 3X3 CON CEROS
mascara = np.zeros((3, 3))
mat_filtrada = np.zeros((fil_mat, col_mat))
#print(mascara)

#%%
##################################################################################################FILTRO DE MEDIA_PONDERADA
print("#########################################################################################FILTRO DE MEDIA PONDERADA")

for i in range(fil_mat):
    #print("paso1")
    for j in range(col_mat):
        #print("paso2")
        if (i != 0) and (i != fil_mat-1) and (j != 0) and (j != col_mat-1):
            #print("paso3")
            mascara[0][0] = mat_completa[i-1][j-1]
            #print("esta es la mascara con el dato de la posicion 0,0",mascara[0][0])
            mascara[0][1] = mat_completa[i-1][j]
            mascara[0][2] = mat_completa[i-1][j+1]
            mascara[1][0] = mat_completa[i][j-1]
            mascara[1][1] = mat_completa[i][j] * 2
            mascara[1][2] = mat_completa[i][j+1]
            mascara[2][0] = mat_completa[i+1][j-1]
            mascara[2][1] = mat_completa[i+1][j]
            mascara[2][2] = mat_completa[i+1][j+1]
            #print (mascara)
            promedio = (mascara[0][0] + mascara[0][1] + mascara[0][2] + mascara[1][0] +
            mascara[1][1] + mascara[1][2] + mascara[2][0] + mascara[2][1] + mascara[2][2])/9
            #print ("este es el promedio.......", promedio)
            mat_filtrada[i][j] = int(promedio)
print (mat_filtrada)
plt.matshow(mat_filtrada)
plt.show()

"""
#%%
####################################################################################FILTRO DE MEDIA

print("#########################################################################################FILTRO DE MEDIA")
for i in range(fil_mat):
    for j in range(col_mat):
        if (i != 0) and (i != fil_mat-1) and (j != 0) and (j != col_mat-1):
            mascara[0][0] = mat_completa[i-1][j-1]
            mascara[0][1] = mat_completa[i-1][j]
            mascara[0][2] = mat_completa[i-1][j+1]
            mascara[1][0] = mat_completa[i][j-1]
            mascara[1][1] = mat_completa[i][j]
            mascara[1][2] = mat_completa[i][j+1]
            mascara[2][0] = mat_completa[i+1][j-1]
            mascara[2][1] = mat_completa[i+1][j]
            mascara[2][2] = mat_completa[i+1][j+1]
           # print ("MASCARA \n",mascara)
            promedio = (mascara[0][0] + mascara[0][1] + mascara[0][2] + mascara[1][0] +
            mascara[1][1] + mascara[1][2] + mascara[2][0] + mascara[2][1] + mascara[2][2])/9
            #print ("este es el promedio.......", promedio)
            mat_filtrada[i][j] = int(promedio)
print ("ESTA ES LA MATRIZ FILTRADA \n",mat_filtrada)
plt.matshow(mat_filtrada)
plt.show()
"""


import pandas as pd
import datetime.datetime as dt
import shutil
import os

'''
Module for wrapping BCNRM software with Python
'''

class BCNRM:
    def __init__(self, weather, yr, mo, d, age, sex, breed1, breed2, clim, wpath="..//weather-repo"):
        self.wpath = wpath
        self.weather = weather
        self.yr = yr
        self.mo = mo
        self.d = d
        self.age = age
        self.sex = sex
        self.breed1 = breed1
        self.breed2 = breed2
        self.clim = clim

    def beef_fast_high(self):
        '''
        The inputs described are for:
            adg = 3.02
            corn = 11.7
            dgs = 4.5
            hay = 1.8
            breed = 1
        Need to change for each different t_lookup.csv files and different inputs
        '''

        temps = pd.read_csv('t_lookup_high.csv')
        doy = dt(self.yr, self.mo, self.d).strftime("%y%j")
        day = dt(self.yr, self.mo, self.d).strftime("%j")
        yrchar = doy[:2]
        wthname = self.weather + yrchar + self.clim + '5'

        path = os.path.join(self.wpath, 'RCP%s_5/%s.WTH' % (self.clim, wthname))
        shutil.copy(path, '.')
        file = open("%s.WTH" % (wthname))
        wth = file.readlines()[3:-1]
        file.close()
        wth = [x.split() for x in wth]
        df = pd.DataFrame(wth)
        df.columns = df.iloc[0]
        df = df.drop(0)
        df['TMIN'] = [float(x) for x in df['TMIN']]
        df['TMAX'] = [float(x) for x in df['TMAX']]
        df['TAVE'] = (df['TMIN'] + df['TMAX']) / 2
        df['MONTH'] = [dt.strptime(x, '%y%j').strftime('%m/%d/%y')[:2] for x in df['@DATE']]
        weather = df.groupby(['MONTH'])
        wave = weather['TAVE'].agg(['mean', 'count'])
        start = int(day)
        day = int(day)
        month = self.mo

        gain = 3.02
        methane = 109.267
        excreteN = 67.3
        corn_total = 0
        dgs_total = 0
        drink = 0
        gain_total = 0
        excreteN_total = 0
        methane_total = 0
        w = 900

        while w < 1250:
            t = wave.loc["%02d" % (month), 'mean']
            t = round(t)
            days = wave.loc["%02d" % (month), 'count']
            drink += float(temps.loc[temps['temp'] == t, 'water'])
            w += gain * days
            corn_total += 11.7 * days
            dgs_total += 4.5 * days
            gain_total += gain * days
            excreteN_total += excreteN * days
            methane_total += methane * days
            month += 1
            print("Weight is %f; Month is %d" % (w, month))
        finishing_time = month - self.mo
        os.remove('%s.WTH' % (wthname))

        out = {'Total Gain': gain_total, 'Total Corn': corn_total, 'Total DGS': dgs_total,
               'Finishing Time': finishing_time, 'Total Methane': methane_total, 'Total N': excreteN_total,
               'Water Drank': drink}
        return out


from constants import PRINTER_MODE_HEADER, PRINTER_MODE_INFO, PRINTER_MODE_WARNING, VARIABLES, UNITS

def printer(message: str, mode: int = 0) -> None:
    """
    Format and print the message.
    :param message: the message to print and format
    :param mode: how to format the message
    :return: None
    """
    if is_in(mode, PRINTER_MODE_HEADER):
        print(20 * "=")

    if is_in(mode, PRINTER_MODE_INFO):
        print("[+]", end=" ")
    elif is_in(mode, PRINTER_MODE_WARNING):
        print("[!]", end=" ")

    print(message)

    if is_in(mode, PRINTER_MODE_HEADER):
        print(20 * "=")

def is_in(settings: int, constants: int) -> bool:
    """
    If the bit(s) "constants" is/are on in "settings".
    :param settings: the holder of bits to test
    :param constants: the bit(s) to test
    :return: if the bit(s) is/are "settings"
    """
    return settings & constants == constants

def replace_variables(text: str) -> str:
    """
    Replace variables of a text defined by constants.py#VARIABLES.
    :param text: the text that hold or not variables
    :return: the text replaced
    """
    for v in VARIABLES:
        text = text.replace(v, VARIABLES[v])

    return text

def get_size(size: str) -> tuple:
    """
    Get what represent in bytes a string like 1Gb...
    :param size: the size with units
    :return: the size without units and the string unit
    """
    size = size.lower()
    for u in UNITS:
        if size.endswith(u):
            try:
                v = int(size[:-len(u)] if len(u) > 0 else size)
                return v * UNITS[u], size[-len(u):] if len(u) > 0 else size
            except ValueError:
                pass

    raise ValueError(f"Can't understand the unit: {size}")


class Node:
    def __init__(self) -> None:
        self.data = ""
        self.leftPtr = 0
        self.rightPtr = 0

class Tree:
    def __init__(self, size) -> None:
        self.container = [Node() for i in range(size)]
        self.size = size
        self.nullPtr = -1
        self.freePtr = 0
        self.rootPtr = self.nullPtr

    def initialiseTree(self):
        self.freePtr = 0
        self.rootPtr = self.nullPtr
        for i in range(self.size - 1):
            self.container[i].leftPtr = i + 1
        self.container[self.size - 1].leftPtr = self.nullPtr

    def insertNode(self, newItem):
        if self.freePtr != self.nullPtr:
            newNodePtr = self.freePtr
            self.freePtr = self.container[newNodePtr].leftPtr
            self.container[newNodePtr].data = newItem
            self.container[newNodePtr].leftPtr = self.nullPtr
            self.container[newNodePtr].rightPtr = self.nullPtr

            if self.rootPtr == self.nullPtr:
                self.rootPtr = newNodePtr
            else:
                thisNodePtr = self.rootPtr
                prevNodePtr = self.nullPtr
                turnedLeft = False
                while thisNodePtr != self.nullPtr:
                    prevNodePtr = thisNodePtr
                    if self.container[thisNodePtr].data > newItem:
                        turnedLeft = True
                        thisNodePtr = self.container[thisNodePtr].leftPtr
                    else:
                        turnedLeft = False
                        thisNodePtr = self.container[thisNodePtr].rightPtr
                if turnedLeft == True:
                    self.container[prevNodePtr].leftPtr = newNodePtr
                else:
                    self.container[prevNodePtr].rightPtr = newNodePtr
    def findElement(self, itemToFind):
        node = -1
        for i in range(self.size):
            if self.container[i].data == itemToFind:
                node = i
                break
        return node

    def displayContainer(self):
        print(f"FreePtr: {self.freePtr} | RootPrt: {self.rootPtr}")
        print("------------------------------------")
        for i in range(self.size):
            print(f"Node: {i} | Left Pointer: {self.container[i].leftPtr} | Data: {self.container[i].data} | Right Pointer: {self.container[i].rightPtr}")
        print("-----------------------------------")

t1 = Tree(size=5)
t1.initialiseTree()

t1.insertNode("B")
t1.insertNode("C")
t1.insertNode("A")
t1.insertNode("E")
t1.insertNode("D")

t1.displayContainer()

print("Searching for A ..")
res = t1.findElement("A")
if res == -1:
    print("Item not found")
else:
    print(f"Item in node {res}")


# solution for the 'contacts' problem
# @see: https://www.hackerrank.com/challenges/contacts/problem
# based on https://www.hackerrank.com/challenges/contacts/editorial

from functools import reduce
from typing import Dict

class Node:
    def __init__(self) -> None:
        self.end = False
        self.count = 0
        self.children: Dict[str, Node] = {}

class ContactList:
    def __init__(self) -> None:
        self.root = Node()

    def add(self, name: str):
        if not name:
            return

        current_node: Node = self.root
        for letter in name:
            if not letter in current_node.children:
                current_node.children[letter] = Node()
            current_node = current_node.children[letter]
            current_node.count += 1

    def find(self, prefix: str):
        if not prefix:
            return 0

        current_node = self.root

        current_node = reduce(
            lambda current_node, letter: current_node and current_node.children.get(letter), prefix, current_node)

        if current_node is None:
            # there is no path that encodes this whole prefix
            return 0
        else:
            # the number of nodes bellow this node is the time it was traversed
            # **considering that there is no repeated "add" operations**
            return current_node.count

contact_list = ContactList()
contact_list.add("ed")
contact_list.add("eddie")
contact_list.add("eddie")
contact_list.add("edward")
contact_list.add("edwina")

assert(contact_list.find("ed") == 5)
assert(contact_list.find("ew") == 0)


#!/usr/bin/env python
#import everything
import rospy
import math
import numpy as np
from racecar import racecar
from sensor_msgs.msg import LaserScan
from ackermann_msgs.msg import AckermannDriveStamped
import time

class potentialField:
	def __init__(self):
		self.scanResult=rospy.Subscriber("/scan", LaserScan, self.callback)
		self.car=racecar()

	def callback(self,msg):
		magnitudes = []
		x = []
		y = []
		sumx = 0
		sumy = 0
		pspeed = 1.0
		pangle = 1
		specialX = 15
		for i in range(1080):
			magnitudes.append(-0.1/(msg.ranges[i]**2))
			x.append(magnitudes[i]*math.cos(math.radians(i*0.25-135)))
			y.append(magnitudes[i]*math.sin(math.radians(i*0.25-135)))
			sumx = sumx + x[i]
			sumy = sumy + y[i]
		sumx = sumx + specialX
		speed = pspeed * math.sqrt(sumx**2 + sumy**2) * np.sign(sumx)
		angle = pangle * math.atan2(sumy,sumx) * np.sign(sumx)
		self.car.drive(0.2*speed, angle)

if __name__ == "__main__":
	rospy.init_node("potentialField")
	potential = potentialField()
	rospy.spin()


import logging

from rest_framework import serializers

from sme_ptrf_apps.utils.update_instance_from_dict import update_instance_from_dict
from .rateio_despesa_serializer import RateioDespesaSerializer
from .tipo_documento_serializer import TipoDocumentoSerializer, TipoDocumentoListSerializer
from .tipo_transacao_serializer import TipoTransacaoSerializer
from ..serializers.rateio_despesa_serializer import RateioDespesaCreateSerializer
from ...models import Despesa
from ....core.api.serializers.associacao_serializer import AssociacaoSerializer
from ....core.models import Associacao

log = logging.getLogger(__name__)

class DespesaSerializer(serializers.ModelSerializer):
    associacao = AssociacaoSerializer()
    tipo_documento = TipoDocumentoSerializer()
    tipo_transacao = TipoTransacaoSerializer()
    rateios = RateioDespesaSerializer(many=True)

    class Meta:
        model = Despesa
        fields = '__all__'

class DespesaCreateSerializer(serializers.ModelSerializer):
    associacao = serializers.SlugRelatedField(
        slug_field='uuid',
        required=False,
        queryset=Associacao.objects.all()
    )
    rateios = RateioDespesaCreateSerializer(many=True, required=False)

    def create(self, validated_data):
        rateios = validated_data.pop('rateios')

        # if not rateios:
        #     msgError = "Pelo menos uma linha de rateio deve ser enviada!"
        #     log.info(msgError)
        #     raise ValidationError(msgError)

        despesa = Despesa.objects.create(**validated_data)
        log.info("Criando despesa com uuid: {}".format(despesa.uuid))

        rateios_lista = []
        for rateio in rateios:
            rateio_object = RateioDespesaCreateSerializer().create(rateio)
            rateios_lista.append(rateio_object)
        despesa.rateios.set(rateios_lista)
        log.info("Despesa {}, Rateios: {}".format(despesa.uuid, rateios_lista))

        log.info("Criação de despesa finalizada!")

        return despesa

    def update(self, instance, validated_data):
        rateios_json = validated_data.pop('rateios')
        instance.rateios.all().delete()

        rateios_lista = []
        for rateio_json in rateios_json:
            rateios_object = RateioDespesaCreateSerializer(
            ).create(rateio_json)
            rateios_lista.append(rateios_object)

        update_instance_from_dict(instance, validated_data)
        instance.rateios.set(rateios_lista)
        instance.save()

        return instance

    class Meta:
        model = Despesa
        exclude = ('id',)

class DespesaListSerializer(serializers.ModelSerializer):
    associacao = serializers.SlugRelatedField(
        slug_field='uuid',
        required=False,
        queryset=Associacao.objects.all()
    )

    tipo_documento = TipoDocumentoListSerializer()

    class Meta:
        model = Despesa
        fields = ('uuid', 'associacao', 'numero_documento', 'tipo_documento', 'data_documento', 'cpf_cnpj_fornecedor',
                  'nome_fornecedor', 'valor_total', 'valor_ptrf')


import csv
from datetime import datetime

from plotly.graph_objs import Scattergeo, Layout
from plotly import offline

filename = "data/world_fires_1_day.csv"
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    for index, column_header in enumerate(header_row):
        print(index, column_header)

    # Get latitudes, longitudes, brightness and dates from this file.
    lats, longs, brightnesses, dates = [], [], [], []
    hover_texts = []
    for row in reader:
        lats.append(row[0])
        longs.append(row[1])
        brightnesses.append(float(row[2]))
        date = datetime.strptime(row[5], '%Y-%m-%d')
        label = f"{date.strftime('%m/%d/%y')} - {float(row[2])}"

        hover_texts.append(label)

# Mapping the fires.
data = [{
    "type": "scattergeo",
    "lat": lats,
    "lon": longs,
    "text": hover_texts,
    "marker": {
        "color": brightnesses,
        "colorscale": "YlOrRd",
        "reversescale": True,
        "colorbar": {"title": "Brightness"},
    },
}]

my_layout = Layout(title="World Fires - Sep 22-23, 2018")

fig = {"data": data, "layout": my_layout}
offline.plot(fig, filename="global_fires.html")


#!/usr/bin/env python3
"""Provides a function to create a layer for a neural network"""
# pylint: disable=invalid-name

import tensorflow as tf

def create_layer(prev, n, activation):
    """
    Creates a layer for a neural network
    Arguments:
        prev: the tensor output of the previous layer
        n: the number of nodes in the layer to create
        activation: the activation function
    Returns:
        the tensor output of the layer
    """
    init = tf.contrib.layers.variance_scaling_initializer(mode='FAN_AVG')
    layer = tf.layers.Dense(
        units=n, activation=activation, kernel_initializer=init, name='layer')
    return layer(prev)


# 117308KB / 436ms (PyPy3)
import sys
input = sys.stdin.readline

# 1. 미세먼지를 확산시키고
# 2. 공기청정기를 작동시킴

R, C, T = map(int, input().split())
array = []
dusts1 = []
dusts2 = []
firstCleaner = -1
secondCleaner = -1
for r in range(R):
    array.append(list(map(int, input().split())))
    for c in range(C):
        if array[r][c]==-1:
            if firstCleaner==-1:
                firstCleaner = r
            else:
                secondCleaner = r

dr = [0, 0, 1, -1] # 좌하우상 (반시계순)
dc = [1, -1, 0, 0]
for t in range(T):
    # 1. 미세먼지 확산
    tempArray = [array[i][:] for i in range(R)]
    for r in range(R):
        for c in range(C):
            if array[r][c] < 5: continue
            for v in range(4):
                nextR = r + dr[v]
                nextC = c + dc[v]
                if 0<=nextR<R and 0<=nextC<C and array[nextR][nextC]>=0:
                    tempArray[nextR][nextC] += array[r][c]//5
                    tempArray[r][c] -= array[r][c]//5
    array = tempArray
    # 2. 공기청정기 작동 - 반시계방향
    array[firstCleaner-1][0] = 0
    # 하
    for r in range(firstCleaner-1, -1, -1):
        if array[r][0] > 0:
            array[r+1][0] = array[r][0]
            array[r][0] = 0
    # 좌
    for c in range(1, C):
        if array[0][c] > 0:
            array[0][c-1] = array[0][c]
            array[0][c] = 0
    # 상
    for r in range(1, firstCleaner+1):
        if array[r][C-1] > 0:
            array[r-1][C-1] = array[r][C-1]
            array[r][C-1] = 0
    # 우
    for c in range(C-2, 0, -1): #C-1~1
        if array[firstCleaner][c] > 0:
            array[firstCleaner][c+1] = array[firstCleaner][c]
            array[firstCleaner][c] = 0

    # 2. 공기청정기 작동 - 시계방향
    array[secondCleaner+1][0] = 0
    # 상
    for r in range(secondCleaner+2, R):
        if array[r][0] > 0:
            array[r-1][0] = array[r][0]
            array[r][0] = 0
    # 좌
    for c in range(1, C):
        if array[R-1][c] > 0:
            array[R-1][c-1] = array[R-1][c]
            array[R-1][c] = 0
    # 하
    for r in range(R-2, secondCleaner-1, -1):
        if array[r][C-1] > 0:
            array[r+1][C-1] = array[r][C-1]
            array[r][C-1] = 0
    # 우
    for c in range(C-2, 0, -1):
        if array[secondCleaner][c] > 0:
            array[secondCleaner][c+1] = array[secondCleaner][c]
            array[secondCleaner][c] = 0
    # print("=================")
    # print(f"{t}일 째")
    # for i in range(R):
    #     print(array[i])
    # print("=================")
ans = sum(map(sum, array))+2
print(ans)


import speech_recognition, librosa, os
from pydub import AudioSegment

def divide(divider, audio=None):
    list_durations = []
    audio_duration = librosa.get_duration(filename=audio)

    if audio_duration > 240:
        while audio_duration > 0:
            list_durations.append(audio_duration)
            audio_duration -= divider

        list_durations.append(0)
        list_durations.reverse()

    else:
        list_durations = [0, audio_duration]

    return list_durations

def remove_file(filename):
    if os.path.exists(filename):
        os.remove(filename)

def recognition(offset, duration, audio=None, language="en-US"):
    try:
        if ".mp3" in audio:
            sound = AudioSegment.from_mp3(audio)
            sound.export("audio.wav", format="wav")
        r = speech_recognition.Recognizer()

        if audio != None:
            audio_file = speech_recognition.AudioFile(audio)
            with audio_file as source:
                r.adjust_for_ambient_noise(source)
                audio = r.record(source, offset=offset, duration=duration)
                try:
                    text = r.recognize_google(audio, language=language)
                    return {"text": text, "processed": True}
                except:
                    return {"text": "...", "processed": True}

    except Exception as e:
        return f"Error {e}"


import os
import sys
import signal

from lib.config import Config
from lib.debug import Debug

def Init():
    '''
    Check the Python version.
    '''
    if sys.version_info[0] < 3:
        print("This program requires Python 3.x")
        sys.exit(1)

    if Config.clearOnStart:
        os.system('cls' if os.name == 'nt' else 'clear')

def DefineHandler():
    signal.signal(signal.SIGINT, handler)

def handler(signum, frame):
    print('\nAborted...')
    exit(1)

def PrintFullLine(char = '=', length = None):
    '''
    Print a line of characters of a given length.

    Parameters
    ----------
    char : str
        The character to use.
    length : int
        The length of the line.
        None means use the current terminal width.
    '''
    if length is None:
        length = os.get_terminal_size().columns
    print(char * length)

def getStringBetween(text, firstWord, lastWord, keepAroundWords = True):
    '''
    Get the string between two words.

    Parameters
    ----------
    text : str
        Le texte dans lequel on cherche
    firstWord : str
        Le mot qui débute la chaine
    lastWord : str
        Le mot qui termine la chaine

    Returns
    -------
    dict|None
        pos: position of the first word in the text
        length: length of the string between the first and last word
        content: the string between the first and last word

        None if the words are not found.
    '''

    firstPos = text.find(firstWord)
    if firstPos == -1:
        return None

    lastPos = firstPos + text[firstPos+len(firstWord):].find(lastWord)
    if lastPos == -1:
        return None

    output = {}
    if keepAroundWords:
        output['pos'] = firstPos
        output['length'] = lastPos - firstPos + len(firstWord) + len(lastWord)
        output['content'] = text[firstPos:lastPos + len(firstWord) + len(lastWord)]
    else:
        output['pos'] = firstPos + len(firstWord)
        output['length'] = lastPos - firstPos
        output['content'] = text[firstPos + len(firstWord):lastPos + len(firstWord)]
    return output

def GetFileContent(filename: str, cleanContent: bool = True):
    '''
    Get the content of a file.

    Parameters
    ----------
    filename : str
        The name of the file.

    Returns
    -------
    str
        The content of the file or None if failed.
    '''

    # Check if file is a valid svg file
    filepath = os.path.join(Config.dirRaw, filename)
    if not filepath.endswith(".svg"): return None
    if not os.path.isfile(filepath): return None
    Debug.Info('Processing file "' + filename + '"')

    # Get file content
    try:
        file = open(filepath, 'rb')
        fileContent = file.read()
        fileContent = fileContent.decode('utf8')
        file.close()
    except Exception as e:
        Debug.Error('SVG not read')
        Debug.Log(e)
        return None

    if cleanContent:
        # Remove useless lines
        removeChars = [ '\n', '\r', '\t' ]
        for char in removeChars:
            fileContent = fileContent.replace(char, '')

        # Remove comments
        commentsInfo = ''
        while commentsInfo != None:
            commentsInfo = getStringBetween(fileContent, '<!--', '-->')
            if commentsInfo != None:
                fileContent = fileContent.replace(commentsInfo['content'], '', 1)

    if fileContent.startswith('<?'):
        # Remove XML declaration
        fileContent = fileContent[fileContent.find('>')+1:]

    return fileContent

def SaveFileContent(filename: str, fileContent: str):
    '''
    Save the content of a file.

    Parameters
    ----------
    filename : str
        The path of the file.
    fileContent : str
        The content of the file.
    '''

    filename = '.'.join(filename.split('.')[:-1])
    filename += '.js' if Config.convertToRN else '.svg'
    filepath = os.path.join(Config.dirSvg, filename)
    try:
        file = open(filepath, 'wb')
        file.write(fileContent.encode('utf8'))
        file.close()
        Debug.Info('New SVG saved')
    except Exception as e:
        Debug.Error('SVG not saved')
        Debug.Log(e)


"""
A module that provides functions for manipulating bitmasks and data quality (DQ) arrays.

:Authors: Mihai Cara (contact: help@stsci.edu)

:License: :doc:`../LICENSE`

"""

import numpy as np

__version__ = '0.9.3'
__vdate__ = '29-March-2015'
__author__ = 'Mihai Cara'

def interpret_bits_value(val):
    """
    Converts input bits value from string to a single integer value or None.
    If a comma- or '+'-separated set of values are provided, they are summed.

    .. note::
        In order to flip the bits of the final result (after summation),
        for input of `str` type, prepend '~' to the input string. '~' must
        be prepended to the *entire string* and not to each bit flag!

    Parameters
    ----------
    val : int, str, None
        An integer bit mask or flag, `None`, or a comma- or '+'-separated
        string list of integer bit values. If `val` is a `str` and if
        it is prepended with '~', then the output bit mask will have its
        bits flipped (compared to simple sum of input val).

    Returns
    -------
    bitmask : int or None
        Returns and integer bit mask formed from the input bit value
        or `None` if input `val` parameter is `None` or an empty string.
        If input string value was prepended with '~', then returned
        value will have its bits flipped (inverse mask).

    Examples
    --------
        >>> "{0:016b}".format(0xFFFF & interpret_bit_flags(28))
        '0000000000011100'
        >>> "{0:016b}".format(0xFFFF & interpret_bit_flags('4,8,16'))
        '0000000000011100'
        >>> "{0:016b}".format(0xFFFF & interpret_bit_flags('~4,8,16'))
        '1111111111100011'
        >>> "{0:016b}".format(0xFFFF & interpret_bit_flags('~(4+8+16)'))
        '1111111111100011'

    """
    if isinstance(val, int) or val is None:
        return val

    else:
        val = str(val).strip()

        if val.startswith('~'):
            flip_bits = True
            val = val[1:].lstrip()
        else:
            flip_bits = False

        if val.startswith('('):
            if val.endswith(')'):
                val = val[1:-1].strip()
            else:
                raise ValueError('Unbalanced parantheses or incorrect syntax.')

        if ',' in val:
            valspl = val.split(',')
            bitmask = 0
            for v in valspl:
                bitmask += int(v)

        elif '+' in val:
            valspl = val.split('+')
            bitmask = 0
            for v in valspl:
                bitmask += int(v)

        elif val.upper() in ['', 'NONE', 'INDEF']:
            return None

        else:
            bitmask = int(val)

        if flip_bits:
            bitmask = ~bitmask

    return bitmask

def bitmask2mask(bitmask, ignore_bits, good_mask_value=1, dtype=np.uint8):
    """
    bitmask2mask(bitmask, ignore_bits, good_pix_value=1, dtype=numpy.uint8)
    Interprets an array of bit flags and converts it to a "binary" mask array.
    This function is particularly useful to convert data quality arrays to
    binary masks.

    Parameters
    ----------
    bitmask : numpy.ndarray
        An array of bit flags. Values different from zero are interpreted as
        "bad" values and values equal to zero are considered as "good" values.
        However, see `ignore_bits` parameter on how to ignore some bits
        in the `bitmask` array.

    ignore_bits : int, str, None
        An integer bit mask, `None`, or a comma- or '+'-separated
        string list of integer bit values that indicate what bits in the
        input `bitmask` should be *ignored* (i.e., zeroed). If `ignore_bits`
        is a `str` and if it is prepended with '~', then the meaning
        of `ignore_bits` parameters will be reversed: now it will be
        interpreted as a list of bits to be *used* (or *not ignored*) when
        deciding what elements of the input `bitmask` array are "bad".

        The `ignore_bits` parameter is the integer sum of all of the bit
        values from the input `bitmask` array that should be considered
        "good" when creating the output binary mask. For example, if
        values in the `bitmask` array can be combinations
        of 1, 2, 4, and 8 flags and one wants to consider that
        values having *only* bit flags 2 and/or 4 as being "good",
        then `ignore_bits` should be set to 2+4=6. Then a `bitmask` element
        having values 2,4, or 6 will be considered "good", while an
        element with a value, e.g., 1+2=3, 4+8=12, etc. will be interpreted
        as "bad".

        Alternatively, one can enter a comma- or '+'-separated list
        of integer bit flags that should be added to obtain the
        final "good" bits. For example, both ``4,8`` and ``4+8``
        are equivalent to setting `ignore_bits` to 12.

        See :py:func:`interpret_bits_value` for examples.

        | Setting `ignore_bits` to `None` effectively will interpret
          all `bitmask` elements as "good" regardless of their value.

        | Setting `ignore_bits` to 0 effectively will assume that all
          non-zero elements in the input `bitmask` array are to be
          interpreted as "bad".

        | In order to reverse the meaning of the `ignore_bits`
          parameter from indicating bits in the values of `bitmask`
          elements that should be ignored when deciding which elements
          are "good" (these are the elements that are zero after ignoring
          `ignore_bits`), to indicating the bits should be used
          exclusively in deciding whether a `bitmask` element is "good",
          prepend '~' to the string value. For example, in order to use
          **only** (or **exclusively**) flags 4 and 8 (2nd and 3rd bits)
          in the values of the input `bitmask` array when deciding whether
          or not that element is "good", set `ignore_bits` to ``~4+8``,
          or ``~4,8 To obtain the same effect with an `int` input value
          (except for 0), enter -(4+8+1)=-9. Following this convention,
          a `ignore_bits` string value of ``'~0'`` would be equivalent to
          setting ``ignore_bits=None``.

    good_mask_value : int, bool (Default = 1)
        This parameter is used to derive the values that will be assigned to
        the elements in the output `mask` array that correspond to the "good"
        flags (that are 0 after zeroing bits specified by `ignore_bits`)
        in the input `bitmask` array. When `good_mask_value` is non-zero or
        `True` then values in the output mask array corresponding to "good"
        bit flags in `bitmask` will be 1 (or `True` if `dtype` is `bool`) and
        values of corresponding to "bad" flags will be 0. When
        `good_mask_value` is zero or `False` then values in the output mask
        array corresponding to "good" bit flags in `bitmask` will be 0
        (or `False` if `dtype` is `bool`) and values of corresponding
        to "bad" flags will be 1.

    dtype : data-type (Default = numpy.uint8)
        The desired data-type for the output binary mask array.

    Returns
    -------
    mask : numpy.ndarray
        Returns an array whose elements can have two possible values,
        e.g., 1 or 0 (or `True` or `False` if `dtype` is `bool`) according to
        values of to the input `bitmask` elements, `ignore_bits` parameter,
        and the `good_mask_value` parameter.

    Examples
    --------
        >>> from stsci.tools import bitmask
        >>> dqbits = np.asarray([[0,0,1,2,0,8,12,0],[10,4,0,0,0,16,6,0]])
        >>> bitmask.bitmask2mask(dqbits, ignore_bits=0, dtype=int)
        array([[1, 1, 0, 0, 1, 0, 0, 1],
               [0, 0, 1, 1, 1, 0, 0, 1]])
        >>> bitmask.bitmask2mask(dqbits, ignore_bits=0, dtype=bool)
        array([[True,  True, False, False,  True, False, False,  True],
               [False, False,  True,  True,  True, False, False,  True]], dtype=bool)
        >>> bitmask.bitmask2mask(dqbits, ignore_bits=6, good_pix_value=0, dtype=int)
        array([[0, 0, 1, 0, 0, 1, 1, 0],
               [1, 0, 0, 0, 0, 1, 0, 0]])
        >>> bitmask.bitmask2mask(dqbits, ignore_bits=~6, good_pix_value=0, dtype=int)
        array([[0, 0, 0, 1, 0, 0, 1, 0],
               [1, 1, 0, 0, 0, 0, 1, 0]])
        >>> bitmask.bitmask2mask(dqbits, ignore_bits='~(2+4)', good_pix_value=0, dtype=int)
        array([[0, 0, 0, 1, 0, 0, 1, 0],
               [1, 1, 0, 0, 0, 0, 1, 0]])

    """

    ignore_bits = interpret_bits_value(ignore_bits)

    if good_mask_value:
        mask = np.ones_like(bitmask, dtype=dtype)
        if ignore_bits is None:
            return mask
        bad_mask_value = 0

    else:
        mask = np.zeros_like(bitmask, dtype=dtype)
        if ignore_bits is None:
            return mask
        bad_mask_value = 1

    mask[np.bitwise_and(bitmask, ~ignore_bits) > 0] = bad_mask_value

    return mask