import abc
import numpy as np
import scipy.ndimage.filters

from skimage.io import imread
from skimage import img_as_ubyte

from skimage.segmentation import slic
from cv2 import VideoWriter, VideoWriter_fourcc
from utilities import flow_to_middlebury, read_flownet2_flow, labels_to_falsecolor, max_width_height_bbox
from skimage.color import rgb2gray

class Volume:
    def __init__(self, data):
        self.data = data
        self.num_frames = data.shape[2]
        self.current_frame = 0

    def __iter__(self):
        return self

    def __next__(self):
        if self.current_frame >= self.num_frames:
            self.current_frame = 0
            raise StopIteration
        else:
            self.current_frame += 1
            return self.frame(self.current_frame - 1)

    @classmethod
    def arrange(cls, rows, cols, volume_list):
        """ useful to arrange before printing volume
            volume_list  list of Volumes.
            if 'e' will be substituted with a black volume
        """
        for i in volume_list:
            if i != 'e':
                tmp = i.data
                break

        height, width, t = tmp.shape[0], tmp.shape[1], tmp.shape[2]
        out = np.zeros(shape=(height*rows, width*cols, t, 3), dtype=np.ubyte)

        curr_col = 0
        curr_row = 0
        for vol in volume_list:
            if vol=='e':
                pass
            else:
                ystart = curr_row*height
                xstart = curr_col*width
                out[ystart:ystart+height, xstart:xstart+width] = vol.data

            curr_col += 1
            if curr_col % (cols) == 0:
                curr_row += 1
                curr_col = 0
        return ImageVolume(out)

    @abc.abstractmethod
    def rgb(self):
        return

    #TODO: super-pfusch fix.. the classtypes???
    def grayscale(self):
        tmp = np.zeros((self.data.shape[0],self.data.shape[1],self.data.shape[2]))
        rgb = self.rgb()
        for t,img in enumerate(self):
            tmp[:,:,t] = rgb2gray(rgb.data[:,:,t])
        return type(self)(tmp)

    def to_video(self, path):
        if not path.endswith(".avi"):
            path += ".avi"

        xdim, ydim, _, _ = self.rgb().data.shape
        vwrite = VideoWriter(path, VideoWriter_fourcc('M','J','P','G'), 30, (ydim, xdim))

        for img in self.rgb():
            vwrite.write(img[...,::-1])

        print("Video written to: " + str(path))
        vwrite.release()
        return

    def frame(self, t):
        return self.data[:,:,t]

    def insert_into(self, dst, rois):
        target_width, target_height = max_width_height_bbox(rois)

        padded = np.pad(dst.data.copy(), [(target_height, target_height),
                         (target_width , target_width),
                         (0,0), (0,0)], mode='mean')

        for idx, img in enumerate(self):
            roi = rois[idx]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            padded[ystart:ystart+target_height,xstart:xstart+target_width, idx] = img.copy()

        return type(self)(padded[target_height:-target_height, target_width:-target_width,:,:])

    def crop(self, rois, padmode='mean'):
        """ calulates the largest bounding box from rois
            creates a subvolume from them
        """
        target_width, target_height = max_width_height_bbox(rois)
        tdim = self.data.shape[2]

        bbox_vol = np.zeros((target_height, target_width)+(self.data.shape[2:]), self.data.dtype)

        # Pad image so we cannot index out of bounds with boundingbox
        padding_tuple = [(target_height, target_height),
                         (target_width , target_width),
                         (0,0),(0,0)]

        if self.data.ndim == 3:
            padding_tuple = padding_tuple[:-1]

        padded  = np.pad(self.data.copy(), padding_tuple, mode=padmode)

        for t in range(tdim):
            roi = rois[t]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            bbox_vol[:,:,t] = padded[ystart:ystart+target_height,
                                     xstart:xstart+target_width,t]
        return type(self)(bbox_vol)

    def project_onto(self, dst, rois):
        if len(np.unique(self.data)) == 1:
            print("NOTHING TO BACKPROJECT")
            return dst

        if self.data.ndim != dst.data.ndim:
            raise ValueError("Dimensions don't match!")

        if self.data.shape[-1] != dst.data.shape[-1]:
            raise ValueError("Depths don't match!")

        target_width, target_height = max_width_height_bbox(rois)

        padding_tuple = [(target_height, target_height),
                         (target_width , target_width),
                         (0,0), (0,0)]

        if self.data.ndim == 3: # todo make generic...
            padding_tuple = padding_tuple[:-1]

        padded = np.pad(dst.data.copy(), padding_tuple, mode='constant')

        for idx, img in enumerate(self):
            roi = rois[idx]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            padded[ystart:ystart+target_height,xstart:xstart+target_width, idx] = img.copy()

        unpadded = padded[target_height:-target_height, target_width:-target_width,...]
        return type(dst)(unpadded)

    def slic(self, n_segments=100, compactness=10.0, max_iter=10, sigma=0,
             spacing=None, multichannel=True, convert2lab=None,
             enforce_connectivity=True, min_size_factor=0.5,
             max_size_factor=3, slic_zero=False):

        tmp = slic(self.data, n_segments,compactness,max_iter, sigma, spacing, multichannel,
           convert2lab,enforce_connectivity, min_size_factor, max_size_factor, slic_zero)
        return LabelVolume(tmp+1)

    def framewise_slic(self, n_segments=100, compactness=10.0, max_iter=10, sigma=0,
             spacing=None, multichannel=True, convert2lab=None,
             enforce_connectivity=True, min_size_factor=0.5,
             max_size_factor=3, slic_zero=False):

        self.data.shape[0],
        tmp = np.zeros(self.data.shape[:3])
        for t, img in enumerate(self):
            lbls = slic(img, n_segments,compactness,max_iter, sigma, spacing, multichannel,
                        convert2lab,enforce_connectivity, min_size_factor, max_size_factor, slic_zero)
            tmp[...,t] = lbls
        return LabelVolume(tmp+1)

    def gaussian(self, sigma):
        if self.data.ndim != len(sigma):
          raise ValueError("sigma should be %d tuple" % self.data.ndim)
        return type(self)(scipy.ndimage.filters.gaussian_filter(self.data.copy(), sigma))

class ImageVolume(Volume):
    def __init__(self, data):
        Volume.__init__(self,data)

    def rgb(self):
        return self

    @classmethod
    def from_filelist(cls, files):
        xdim, ydim, cdim = imread(files[0]).shape
        tdim = len(files)

        if cdim != 3:
            raise ValueError("Depth must be 3(r,g,b)")

        volume = np.zeros((xdim, ydim, tdim, cdim), dtype=np.ubyte)

        for t, file in enumerate(files):
            volume[:,:,t,:] = img_as_ubyte(imread(file))
        return cls(volume)

class FlowVolume(Volume):
    def __init__(self, data):
        Volume.__init__(self, data)

    def rgb(self, step=0, normalize=True):
        return ImageVolume(flow_to_middlebury(self.data, step, normalize))

    @classmethod
    def from_filelist(cls, files):
        xdim, ydim, cdim = read_flownet2_flow(files[0]).shape
        tdim = len(files)

        if cdim != 2:
            raise ValueError("Depth must be 2(xflow, yflow)")

        volume = np.zeros((xdim, ydim, tdim, cdim))

        for t, file in enumerate(files):
            volume[:,:,t,:] = read_flownet2_flow(file)
        return cls(volume)

class LabelVolume(Volume):
    def __init__(self, data):
        Volume.__init__(self, data)

    def rgb(self, colormap='viridis'):
        return ImageVolume(labels_to_falsecolor(self.data, colormap))

    @classmethod
    def from_filelist(cls, files):
        xdim, ydim = imread(files[0]).shape
        tdim = len(files)

        volume = np.zeros((xdim, ydim, tdim), dtype=np.ubyte)

        for t, file in enumerate(files):
            volume[:,:,t,:] = img_as_ubyte(file)
        return cls(volume)

    def select(self, labels):
        tmp = np.zeros(self.data.shape, self.data.dtype)
        for lbl in labels:
            tmp += (self.data==lbl)*lbl
        return LabelVolume(tmp)

    def project_onto_imagevol(self, dst, rois, background=0):

        if len(np.unique(self.data)) == 1:
            print("NOTHING TO BACKPROJECT")
            return dst

        target_width, target_height = max_width_height_bbox(rois)
        ydim, xdim, tdim, _ =dst.data.shape

        empty = np.zeros((ydim, xdim, tdim), dst.data.dtype)

        padded = np.pad(empty, [(target_height, target_height),
                         (target_width , target_width),
                         (0,0)], mode='constant')

        for idx, img in enumerate(self):
            roi = rois[idx]
            x1,y1,x2,y2 = roi[1],roi[0], roi[3],roi[2]

            bbox_width = int(np.ceil((x2-x1)/2)*2)
            bbox_height= int(np.ceil((y2-y1)/2)*2)

            dwidth  = int(target_width - bbox_width)
            dheight = int(target_height - bbox_height)

            xstart = int(x1 - dwidth/2) + target_width
            ystart = int(y1 - dheight/2) + target_height

            padded[ystart:ystart+target_height,xstart:xstart+target_width, idx] = img.copy()

        masked = padded!=background
        masked = masked[target_height:-target_height, target_width:-target_width]
        labelvol = padded[target_height:-target_height, target_width:-target_width]

        imagevol = dst.data.copy()
        imagevol[masked] = labels_to_falsecolor(labelvol[masked])

        return ImageVolume(imagevol)


# -*- coding: utf-8 -*-
"""
Created on Wed Nov 20 23:50:21 2019

@author: Cristian
"""

import bonobo
import requests
import csv
import io
import pandas as pd

API_URL = 'https://mindicador.cl/api/dolar/2019'
CSV_URL = "https://raw.githubusercontent.com/duberc/python/master/Datos%20hist%C3%B3ricos%20S%26P%20CLX%20IPSA.csv"

def get_csvFile():
    content=requests.get(CSV_URL).content
    return io.StringIO(content.decode('utf-8'))

def extract_JsonData():
    Valor_dolar = pd.DataFrame.from_dict(requests.get(API_URL).json().get('serie'))
    yield Valor_dolar

#
def extract_CSVData():
    Bolsa_valores=  pd.read_csv(get_csvFile(), sep=';')
    yield Bolsa_valores
    #yield from csv.reader(get_csvFile(),delimiter=';')

def date_format1(*args):
    Valor_dolar = args[0]
    Valor_dolar["fecha"]= Valor_dolar["fecha"].str.slice(0,10)
    Valor_dolar["FechaCorregido"]= pd.to_datetime(Valor_dolar["fecha"], dayfirst = True)
    yield Valor_dolar
    #Bolsa_valores["FechaCorregido"]= pd.to_datetime(Bolsa_valores["Fecha"],dayfirst = True)

def date_format2(*args):
    Bolsa_valores = args[0]
    #Valor_dolar["fecha"]= Valor_dolar["fecha"].str.slice(0,10)
    #Valor_dolar["FechaCorregido"]= pd.to_datetime(Valor_dolar["fecha"], dayfirst = True)
    Bolsa_valores["FechaCorregido"]= pd.to_datetime(Bolsa_valores["Fecha"],dayfirst = True)
    yield Bolsa_valores

def imprimir(*args):
    print(*args)

def get_graph(**options):
    graph = bonobo.Graph()
    graph.add_chain(
        extract_JsonData,
        date_format1,
        imprimir
    )
    graph.add_chain(
        extract_CSVData,
        date_format2,
        imprimir
    )

    return graph

def get_services(**options):
    return {}

if __name__ == '__main__':
    parser = bonobo.get_argument_parser()
    with bonobo.parse_args(parser) as options:
        bonobo.run(get_graph(**options), services=get_services(**options))


import os
import sys

import subprocess
from subprocess import PIPE, STDOUT, Popen

import settings

def get_board_path(prjctName, prjctPath):

    # cmd = 'cd ' + settings.escape_string(prjctPath) + ' && fossil rev-parse --show-toplevel'
    cmd = "todo"

    stdout, stderr = settings.run_cmd(cmd)
    scm_root = stdout

    # cmd = 'cd ' + settings.escape_string(scm_root) + ' && fossil ls-tree -r --name-only HEAD | grep -m 1 ' + prjctName
    cmd = "todo"

    stdout, stderr = settings.run_cmd(cmd)

    return settings.escape_string(stdout)

def get_boards(diff1, diff2, prjctName, kicad_project_path, prjctPath):

    '''Given two Fossil artifacts, write out two kicad_pcb files to their respective
    directories (named after the artifacts). Returns the date and time of both commits'''

    artifact1 = diff1[:6]
    artifact2 = diff2[:6]

    cmd = 'cd ' + settings.escape_string(prjctPath) + ' && fossil diff --brief -r ' + \
        artifact1 + ' --to ' + artifact2 + ' | grep .kicad_pcb'

    print("")
    print("Getting Boards")
    print(cmd)

    stdout, stderr = settings.run_cmd(cmd)
    changed = stdout

    if changed == '':
        print("\nThere is no difference in .kicad_pcb file in selected commits")
        sys.exit()

    outputDir1 = prjctPath + settings.plotDir + '/' + artifact1
    outputDir2 = prjctPath + settings.plotDir + '/' + artifact2

    if not os.path.exists(outputDir1):
        os.makedirs(outputDir1)

    if not os.path.exists(outputDir2):
        os.makedirs(outputDir2)

    print("")
    print("Setting output paths")
    print(outputDir1)
    print(outputDir2)

    fossilArtifact1 = 'cd ' + settings.escape_string(prjctPath) + ' && fossil cat ' + settings.escape_string(prjctPath) + prjctName + \
        ' -r ' + artifact1 + ' > ' + outputDir1 + '/' + prjctName

    fossilArtifact2 = 'cd ' + settings.escape_string(prjctPath) + ' && fossil cat ' + settings.escape_string(prjctPath) + prjctName + \
        ' -r ' + artifact2 + ' > ' + outputDir2 + '/' + prjctName

    print("")
    print("Setting artifact paths")
    print(fossilArtifact1)
    print(fossilArtifact2)

    fossilDateTime1 = 'cd ' + settings.escape_string(prjctPath) + ' && fossil info ' + artifact1
    fossilDateTime2 = 'cd ' + settings.escape_string(prjctPath) + ' && fossil info ' + artifact2

    print("")
    print("Checking datetime")
    print(fossilDateTime1)
    print(fossilDateTime2)

    stdout, stderr = settings.run_cmd(fossilArtifact1)
    dateTime, _ = settings.run_cmd(fossilDateTime1)
    uuid, _, _, _, _, _, _, _, _, artifactRef, dateDiff1, timeDiff1, *junk1 = dateTime.split(" ")

    stdout, stderr = settings.run_cmd(fossilArtifact2)
    dateTime, _ = settings.run_cmd(fossilDateTime2)
    uuid, _, _, _, _, _, _, _, _, artifactRef, dateDiff2, timeDiff2, *junk2 = dateTime.split(" ")

    dateTime = dateDiff1 + " " + timeDiff1 + " " + dateDiff2 + " " + timeDiff2

    return artifact1, artifact2, dateTime

def get_artefacts(prjctPath, board_file):
    '''Returns list of artifacts from a directory'''

    cmd = 'cd {prjctPath} && fossil finfo -b {board_file}'.format(prjctPath=prjctPath, board_file=board_file)

    print("")
    print("Getting artifacts")
    print(cmd)

    stdout, stderr = settings.run_cmd(cmd)
    artifacts = [a.replace(' ', ' | ', 4) for a in stdout.splitlines()]

    return artifacts

def get_kicad_project_path(prjctPath):
    '''Returns the root folder of the repository'''

    cmd = "cd {prjctPath} && fossil status ".format(
        prjctPath=settings.escape_string(prjctPath))

    stdout, _ = settings.run_cmd(cmd)
    repo_root_path = stdout.split()[3]

    kicad_project_path = os.path.relpath(prjctPath, repo_root_path)

    return repo_root_path, kicad_project_path


class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        ans = []
        marked = [False] * len(nums)
        self.dfs(nums, [], marked, ans)
        return ans

    def dfs(self, nums, path, marked, ans):
        if len(path) == len(nums):
            ans.append(list(path))
            return

        for i, n in enumerate(nums):
            if marked[i]:
                continue
            marked[i] = True
            path.append(n)
            self.dfs(nums, path, marked, ans)
            path.pop()
            marked[i] = False


from lxml import etree

class MenuList():
    def __init__(self):
        super().__init__()
        self.menuXML = '../docs/menu.xml'
        try:
            self.root = etree.parse(self.menuXML).getroot()
        except:
            self.__initList()
            print('no menu was found, an empty menu is created')

    def __initList(self):
        self.root = etree.Element("menu")
        tree = etree.ElementTree(self.root)
        tree.write(self.menuXML, xml_declaration=True, encoding='utf-8')

    def printMenu(self):
        for sort in self.root.findall("sort"):
            print(sort.attrib['name'])
            for dish in sort.findall("dish"):
                print(' |-->', dish.attrib['name'])

    def insert(self, name, sort, ingrediants):
        dishSort = self.root.find("sort[@name='" + sort + "']")
        if dishSort is None:
            dishSort = etree.SubElement(self.root, 'sort', name=sort)

        dish = etree.SubElement(dishSort, 'dish', name=name)
        for k, v in ingrediants.items():
            temp = etree.SubElement(dish, "ingrediant", name=k)
            temp.text = v

        tree = etree.ElementTree(self.root)
        tree.write(self.menuXML, xml_declaration=True, encoding='utf-8')

if __name__ == '__main__':
    menu = MenuList()
    menu.printMenu()
    # name = '测试'
    # sort = '种类2'
    # ingrediants = {'原料1':'1', '原料2':'1'}
    # menu.insert(name, sort, ingrediants)


from .tp_data_reader import get_dataset
from .vocabulary import Vocabulary
from .example import Example
from .models.attacker import *
from .dataset import PrDataset, AttackDataset

from collections import defaultdict
import torch.nn as nn
import torch
from torch import optim
from torch.utils.data import DataLoader
import sys
from tqdm import tqdm
import numpy as np
from sklearn.metrics import f1_score

import pytorch_influence_functions as pif
from pyvacy import optim, analysis, sampling
from torch.utils.data import DataLoader, TensorDataset

def extract_vocabulary(dataset, add_symbols=None):
    freqs = defaultdict(int)
    for example in dataset:
        s = example.get_sentence()
        for token in s:
            freqs[token] += 1
    if add_symbols is not None:
        for s in add_symbols:
            freqs[s] += 1000
    return Vocabulary(freqs)

def get_classifier_labels(dataset):
    return set([data.get_label() for data in dataset])

def get_aux_labels(examples):
    labels = set()
    for ex in examples:
        for l in ex.get_aux_labels():
            labels.add(l)
    return labels

def _to_device(*tensors, device: str):
    return tuple(tensor.to(device) for tensor in tensors)

class PrModel:
    def __init__(self, args, vocabulary: Vocabulary, classifier_output_size: int, adversary_output_size: int) -> None:
        self.args = args
        self.device = args.device

        self.vocabulary = vocabulary

        # classifier
        self.main_classifier = MainClassifier(
            alphabet_size=vocabulary.size_chars(), vocab_size=vocabulary.size_words(),
            output_size=classifier_output_size, args=args
            ).to(self.device)
        self.adversary_classifier = AdversaryClassifier(
            self.main_classifier.hidden_size,
            output_size=adversary_output_size, args=args
            ).to(self.device)

        # adversarial training
        self.discriminator = AdversaryClassifier(
            self.main_classifier.hidden_size,
            output_size=adversary_output_size, args=args
            ).to(self.device)

        if self.args.atraining:
            self.a_optimizer = optim.Adam(self.discriminator.parameters(), lr=args.learning_rate)

    def get_input(self, example: Example, adversarial=False):
        return self.vocabulary.code_sentence_cw(example.get_sentence(), adversarial=adversarial)

    def discriminator_train(self, hidden_state, target):
        # change device
        hidden_state, fake_labels = _to_device(hidden_state, target, device=self.device)

        fake_labels = ~target
        fake_loss = self.discriminator.get_loss(hidden_state, fake_labels)
        # fake_loss.backward()

        # self.a_optimizer.step()
        # self.a_optimizer.zero_grad()

        return fake_loss#.item()

    def evaluate_main(self, dataset):
        self.main_classifier.eval()
        device = self.device

        loss = 0
        acc = 0
        tot = 0#len(dataset)
        with torch.no_grad():
            for i, (input_vec, aux, target) in enumerate(dataset):
                input_vec = input_vec.to(device)
                target = target.to(device)
                l, predicts = self.main_classifier.get_loss_prediction(input_vec, target)
                loss += l.item()
                for p, t in zip(predicts, target):
                    tot += 1
                    if p.item() == t.item():
                        acc += 1
#                 print(acc, tot)
        return (loss / tot), round(acc / tot  * 100, 3)

    def train_main(self, train, dev):

        l2_norm_clip = 1.0
        noise_multiplier = 1.1
        minibatch_size = self.args.batch_size
        microbatch_size = 1
        iterations = self.args.iterations
        delta = 1e-5

        lr = self.args.learning_rate
        batch_size = self.args.batch_size
        device = self.device
        output_size = self.adversary_classifier.output_size

        train_dataset = PrDataset(train, self.vocabulary, self.args.seq_len, aux_size=output_size)
        val_dataset = PrDataset(dev, self.vocabulary, self.args.seq_len, aux_size=output_size)

        if self.args.is_add_gradient_noise:
            optimizer = optim.DPAdam(
                l2_norm_clip=l2_norm_clip,
                noise_multiplier=noise_multiplier,
                minibatch_size=minibatch_size,
                microbatch_size=microbatch_size,
                params=self.main_classifier.parameters(),
                lr=lr)
            minibatch_loader, microbatch_loader = sampling.get_data_loaders(minibatch_size, microbatch_size, iterations)
            print('Achieves ({}, {})-DP'.format(analysis.epsilon(len(train_dataset), minibatch_size, noise_multiplier,
                    iterations, delta,), delta, ))
            train_loader = minibatch_loader(train_dataset)
            val_loader = minibatch_loader(val_dataset)
        else:
            optimizer = optim.Adam(self.main_classifier.parameters(), lr=lr)
            train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
            val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True, num_workers=0)

        # epoch 0
        l, acc = self.evaluate_main(val_loader)
        print(f"[epoch=0] loss: {l}, acc: {acc}%")

        best_val_loss = 1000
        best_model = None
        for i in range(self.args.iterations):
            self.main_classifier.train()
            train_loss = 0
            train_acc = 0
            train_tot = 0
            for _i, (input_vec, aux, target) in enumerate(tqdm(train_loader)):
                optimizer.zero_grad()

                if self.args.is_add_gradient_noise:
                    for X_microbatch, y_microbatch  in microbatch_loader(TensorDataset(input_vec, target)):
                        optimizer.zero_microbatch_grad()
                        X_microbatch = X_microbatch.to(device)
                        y_microbatch = y_microbatch.to(device)
                        loss, predicts = self.main_classifier.get_loss_prediction(X_microbatch, y_microbatch)
                        loss.backward()
                        optimizer.microbatch_step()
                        train_loss += loss.item()
                        train_tot += 1
                        if predicts[0].item() == y_microbatch[0].item():
                            train_acc += 1
                    optimizer.step()
                else:
                    input_vec = input_vec.to(device)
                    target = target.to(device)
                    loss, predicts = self.main_classifier.get_loss_prediction(input_vec, target)
                    train_loss += loss.item()
                    if self.args.is_add_loss_noise:
                        loss = loss + self.add_loss_noise() #add noise before backward
                    if self.args.atraining:
                        loss += self.discriminator_train(self.main_classifier.get_lstm_embed(input_vec), aux)
                    loss.backward()
                    for p, t in zip(predicts, target):
                        train_tot += 1
                        if predicts[0].item() == target[0].item():
                            train_acc += 1
                    optimizer.step()

            # if self.args.ptraining:
            #     self.privacy_train(example, train)

            # if self.args.atraining:
            #     discriminator_loss += self.discriminator_train(example)

            # if self.args.generator:
            #     generator_loss += self.generator_train(example)

            train_acc = round(train_acc / train_tot  * 100, 3)
            print(f"[train epoch={i+1}] loss: {train_loss}, acc: {train_acc}%")
            l, acc = self.evaluate_main(val_loader)
            print(f"[val epoch={i+1}] loss: {l}, acc: {acc}%")

            if l < best_val_loss:
                best_val_loss = l
                best_model = self.main_classifier
                print('[best_model updated]')
        self.main_classifier = best_model
        l, acc = self.evaluate_main(val_loader)
        print(f"[val epoch=final] loss: {l}, acc: {acc}%")

    def evaluate_adversarial(self, dataset):
        self.adversary_classifier.eval()
        self.main_classifier.eval()
        device = self.device
        loss = 0
        gender_acc = 0
        age_acc = 0
        tot = 0#len(dataset)
        with torch.no_grad():
            for i, (input_vec, target) in enumerate(dataset):
                input_vec = input_vec.to(device)
                target = target.to(device)
                hidden_state = self.main_classifier.get_lstm_embed(input_vec)
                l, predicts = self.adversary_classifier.get_loss_prediction(hidden_state, target)
                loss += l.item()
                for p, t in zip(predicts, target):
                    tot += 1
                    if p[0].item() == t[0].item():
                        gender_acc += 1
                    if p[1].item() == t[1].item():
                        age_acc += 1
        return loss / tot, round(gender_acc / tot * 100, 3) , round(age_acc / tot * 100, 3)

    def train_adversarial(self, train, dev):
        lr = self.args.learning_rate
        batch_size = self.args.batch_size
        device = self.device
        output_size =  self.adversary_classifier.output_size
        seq_len = self.args.seq_len

        train_dataset = AttackDataset(train, self.vocabulary, seq_len, output_size)
        val_dataset = AttackDataset(dev, self.vocabulary, seq_len, output_size)
        train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=0)
        val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=True, num_workers=0)

        optimizer = optim.Adam(self.adversary_classifier.parameters(), lr=lr)

        # epoch 0
        l, gender_acc, age_acc = self.evaluate_adversarial(val_loader)
        print(f"[epoch=0] loss: {l}, gender acc: {gender_acc}%, age acc: {age_acc}%")

        best_val_loss = 1000
        best_model = None
        self.main_classifier.eval()
        for i in range(self.args.iterations):
            self.adversary_classifier.train()

            train_loss = 0
            train_gender_acc = 0
            train_age_acc = 0
            train_tot = 0

            for _i, (input_vec, target) in enumerate(tqdm(train_loader)):
                input_vec = input_vec.to(device)
                target = target.to(device)
                hidden_state = self.main_classifier.get_lstm_embed(input_vec)
                hidden_state = hidden_state.detach()
                loss, predicts = self.adversary_classifier.get_loss_prediction(hidden_state, target)
                loss.backward()
                optimizer.step()
                optimizer.zero_grad()

                train_loss += loss.item()
                for p, t in zip(predicts, target):
                    train_tot += 1
                    if p[0].item() == t[0].item():
                        train_gender_acc += 1
                    if p[1].item() == t[1].item():
                        train_age_acc += 1
                # if self.args.ptraining:
                #     self.privacy_train(example, train)

                # if self.args.atraining:
                #     discriminator_loss += self.discriminator_train(example)

                # if self.args.generator:
                #     generator_loss += self.generator_train(example)
            train_gender_acc = round(train_gender_acc / train_tot  * 100, 3)
            train_age_acc = round(train_age_acc / train_tot  * 100, 3)
            print(f"[train epoch={i+1}] loss: {train_loss}, gender acc: {train_gender_acc}%, age acc: {train_age_acc}%")
            l, gender_acc, age_acc = self.evaluate_adversarial(val_loader)
            print(f"[val epoch={i+1}] loss: {l}, gender acc: {gender_acc}%, age acc: {age_acc}%")

            if l < best_val_loss:
                best_val_loss = l
                best_model = self.adversary_classifier
                print('[best_model updated]')

        self.adversary_classifier = best_model
        l, gender_acc, age_acc = self.evaluate_adversarial(val_loader)
        print(f"[val epoch=final] loss: {l}, gender acc: {gender_acc}%, age acc: {age_acc}%")

    def evaluate_influence_sample(self, train, test):
        train_dataset = PrDataset(train, self.vocabulary, self.args.seq_len, return_aux=False)
        test_dataset = PrDataset(test, self.vocabulary, self.args.seq_len, return_aux=False)
        train_dataloader = DataLoader(train_dataset, batch_size=self.args.batch_size)
        test_dataloader = DataLoader(test_dataset, batch_size=self.args.batch_size)

        config = pif.get_default_config()
        self.main_classifier = self.main_classifier.cpu()

        config['gpu'] = -1
        config['damp'] = 0.01
        config['scale'] = 1
        config['outdir'] = "main_classifier_outdir"
        print("config: ", config)
        pif.calc_all_grad_then_test(config, self.main_classifier, train_dataloader, test_dataloader)
        config['outdir'] = "adversarial_outdir"
        pif.calc_all_grad_then_test(config, self.adversary_classifier, train_dataloader, test_dataloader)

    def add_gradient_noise(self):
        for p in self.main_classifier.parameters():
            noise = np.random.laplace(loc=0, scale=1, size = p.grad.shape)
            p.grad += torch.from_numpy(noise).to(self.device)

    def add_loss_noise(self):
        noise = np.random.laplace(loc=0, scale=1, size = 1)[0]
#         loss += noise
        return noise

def main(args):
    args.device_num = args.device
    device = torch.device(f'cuda:{args.device}' if args.device != 'cpu' else 'cpu')
    args.device = device
    torch.manual_seed(0)
    get_data = {"tp_fr": lambda : get_dataset("fr"),
                "tp_de": lambda : get_dataset("de"),
                "tp_dk": lambda : get_dataset("dk"),
                "tp_us": lambda : get_dataset("us"),
                "tp_uk": lambda : get_dataset("uk")
                }

    print("loading data...")
    train, dev, test = get_data[args.dataset]()

    print("building vocabulary...")
    symbols = ["<g={}>".format(i) for i in ["F", "M"]] + ["<a={}>".format(i) for i in ["U", "O"]]
    vocabulary = extract_vocabulary(train, add_symbols=symbols)

    # output size
    classifier_output_size: int = len(get_classifier_labels(train))
    adversary_output_size: int = len(get_aux_labels(train))

    mod = PrModel(args, vocabulary, classifier_output_size, adversary_output_size)

    mod.train_main(train, dev)
    mod.train_adversarial(train, dev)
    if args.is_influence_sample:
        mod.evaluate_influence_sample(train, test)

if __name__ == "__main__":
    import argparse
    import random
    import numpy as np
    import os
    random.seed(10)
    np.random.seed(10)
    torch.manual_seed(0)

    usage = """Implements the privacy evaluation protocol described in the article.

(i) Trains a classifier to predict text labels (topic, sentiment)
(ii) Generate a dataset with the hidden
  representations of each text {r(x), z} with:
    * z: binary private variables
    * x: text
    * r(x): vector representation of text
(iii) Trains the attacker to predict z from x and evaluates privacy
"""

    parser = argparse.ArgumentParser(description = usage, formatter_class=argparse.RawTextHelpFormatter)

    parser.add_argument("dataset", default="tp_fr", choices=["tp_fr", "tp_de", "tp_dk", "tp_us", "tp_uk", "bl"], help="Dataset. tp=trustpilot, bl=blog")

    parser.add_argument("--learning-rate", "-b", type=float, default=1e-3, help="Learning rate")
    parser.add_argument("--batch-size", type=int, default=256, help="Batch size")
    parser.add_argument("--iterations", "-i", type=int, default=10, help="Number of training iterations")

    parser.add_argument("--seq_len", "-sl", type=int, default=75, help="Length of word sequence")
    parser.add_argument("--char_seq_len", "-csl", type=int, default=150, help="Length of character sequence")

    # define model parameters
    parser.add_argument("--char-embed-dim","-c", type=int, default=50, help="Dimension of char embeddings")
    parser.add_argument("--char-hidden-dim","-C", type=int, default=50, help="Dimension of char lstm")
    parser.add_argument("--word-embed-dim","-w", type=int, default=50, help="Dimension of word embeddings")
    parser.add_argument("--word-hidden-dim","-W", type=int, default=50, help="Dimension of word lstm")

    parser.add_argument("--fc-dim","-l", type=int, default=50, help="Dimension of hidden layers")

    parser.add_argument("--device", "-d", type=str, default='cpu', help="Training device")

    parser.add_argument("--atraining", action="store_true", help="Adversarial classification defense (multidetasking)")
    parser.add_argument("--ptraining", action="store_true", help="Declustering defense")

    parser.add_argument("--is-add-loss-noise", action="store_true", help="Add noise to loss, [default=false]")
    parser.add_argument("--is-add-gradient-noise", action="store_true", help="Add noise to gradient, [default=false]")
    parser.add_argument("--is-influence-sample", "-if", action="store_true", help="Evaluate influence, [default=false]")
    parser.add_argument("--use-char-lstm", action="store_true", help="Use a character LSTM, [default=false]")

    args = parser.parse_args()

    main(args)

#=====dead kitten======#
#         else:
#
# #             optimizer = optim.AdamW(self.main_classifier.parameters(), lr=lr)

#             # epoch 0
#             l, acc = self.evaluate_main(val_loader)
#             print(f"[epoch=0] loss: {l}, acc: {acc}%")

#             for i in range(self.args.iterations):
#                 self.main_classifier.train()
#                 train_loss = 0
#                 train_acc = 0
#                 train_tot = 0
#                 for _i, (input_vec, target) in enumerate(tqdm(train_loader)):
#                     input_vec = input_vec.to(device)
#                     target = target.to(device)
#                     loss, predicts = self.main_classifier.get_loss_prediction(input_vec, target)
#                     train_loss += loss.item()

#                     if self.args.is_add_loss_noise:
#                         loss = loss + self.add_loss_noise() #add noise before backward

#                     loss.backward()
# #                     if self.args.is_add_gradient_noise:
# #                         self.add_gradient_noise() #add noise after gradient is calculated.
#                     for p, t in zip(predicts, target):
#                         train_tot += 1
#                         if predicts[0].item() == target[0].item():
#                             train_acc += 1
#                     optimizer.step()
#                     optimizer.zero_grad()

#                 # if self.args.ptraining:
#                 #     self.privacy_train(example, train)

#                 # if self.args.atraining:
#                 #     discriminator_loss += self.discriminator_train(example)

#                 # if self.args.generator:
#                 #     generator_loss += self.generator_train(example)
#                 train_acc = round(train_acc / train_tot  * 100, 3)
#                 print(f"[train epoch={i+1}] loss: {train_loss}, acc: {train_acc}%")

#                 l, acc = self.evaluate_main(val_loader)
#                 print(f"[val epoch={i+1}] loss: {l}, acc: {acc}%")


import math
vCasa = float(input("Digite o preço da casa que pretende financiar: "))
salario = float(input("Digite o valor do seu salário: "))
fina = int(input("digite a quantidade de meses que deseja financiar: "))
minimo = salario *30 / 100

def main():

    vParcela = float(vCasa / fina)

    if vParcela > minimo:
        print("Empréstimo negado")

    elif vParcela <= minimo:

        print ("Aprovado")

main()


# test_stk_vlass_pipeline_imaging.py
#
# Copyright (C) 2018
# Associated Universities, Inc. Washington DC, USA.
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
# License for more details.
#
# [https://open-jira.nrao.edu/browse/CAS-12427]
#
# Based on the requirements listed here:
#
#
##########################################################################

'''
Datasets :
---------------
VLASS 1.1 :
    - New/Old ACUs : Pointing offset between two sets of antennas is about 45 arcsec (upto 1 arcmin)
    - Pointing nods between integrations : 1.5 arcmin between integrations.
VLASS 1.2 :
     - Pointing nods between integrations : 1.5 arcmin between integrations

For both observation epochs : 3C286  :
               - 1  pointing, 2 integrations
               - 5 pointings, 5x2=10 integrations

TODO : Prune the pointing table.
             Get VLASS 1.2 dataset of 3c286

Algorithm Options :
-------------------------

New parameter :  pointingsigdev : [ bin width,  mean shift width ]

For VLASS data, [300, 300] will treat all antennas together, and no time-dependent pointing.
        - This will be similar to do-pointing=False except that the time-step will be every 300 sec.

For VLASS 1.1, a default of [ 30 arcsec,  30 arcsec ] is good.  Factor of 2 slower than [300,300]
For VLASS 1.2,  we can do [ 300arcsec, 30 arcsec ] to treat all antennas together, but to keep the time-dependence corrections.

If you sub-select antennas for new or old ACUs, then pointingsigdev applies to the selected antennas only. So, if selecting only new ACUs from VLASS 1.1 data, can set [ 300.0, 30.0 ].

'''

fieldnames = {'18952':'1447214-161911',
              '18953':'1447338-161911',
              '18954':'1447463-161911',
              '18955':'1447587-161911',
              '18956':'1448111-161911',
              '18957':'1448236-161911',
              '18958':'1448360-161911',
              '18959':'1448484-161911',
              '18960':'1449009-161911'}

fieldcenters = {'18952':'J2000 14:47:21.451021 -16.19.12.00000',
                '18953':'J2000 14:47:33.886250 -16.19.12.00000',
                '18954':'J2000 14:47:46.321479 -16.19.12.00000',
                '18955':'J2000 14:47:58.756708 -16.19.12.00000',
                '18956':'J2000 14:48:11.191937 -16.19.12.00000',
                '18957':'J2000 14:48:23.627167 -16.19.12.00000',
                '18958':'J2000 14:48:36.062396 -16.19.12.00000',
                '18959':'J2000 14:48:48.497625 -16.19.12.00000',
                '18960':'J2000 14:49:00.932854 -16.19.12.00000',
                '18961':'J2000 14:49:13.368083 -16.19.12.00000'}

alltimes = {'18952': {'one':'<13:34:14.7',  'two':'>13:34:14.7'} ,
            '18953': {'one':'<13:34:15.6',  'two':'>13:34:15.6'} ,
            '18954': {'one':'<13:34:16.5',  'two':'>13:34:16.5'} ,
            '18955': {'one':'<13:34:17.4',  'two':'>13:34:17.4'} ,
            '18956': {'one':'<13:34:18.3',  'two':'>13:34:18.3'} ,
            '18957': {'one':'<13:34:19.2',  'two':'>13:34:19.2'} ,
            '18958': {'one':'<13:34:20.1',  'two':'>13:34:20.1'} ,
            '18959': {'one':'<13:34:21.0',  'two':'>13:34:21.0'} ,
            '18960': {'one':'<13:34:21.9',  'two':'>13:34:21.9'} }

pixlocs = {'18953':[3025,4757,0,0],
               '18954':[3621,4758,0,0],
               '18955':[4218,4758,0,0],
               '18956':[4815,4758,0,0],
               '18957':[5411,4758,0,0],
               '18958':[6008,4758,0,0],
               '18959':[6604,4758,0,0],
               '18960':[7201,4757,0,0] }

##########################################################################
### NOTES

## pointingsigdev parameter needs to now show a warning for the default of usepointing=False !!!

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

# Imports #
import os
import subprocess
import glob
import time
import sys
import shutil
import unittest
import inspect
import pprint
import numpy
import pylab as pl

CASA6 = False

try:
    from casatools import ctsys
    from casatools import image as iatool
    from casatasks import tclean, casalog
    from casatasks.private.parallel.parallel_task_helper import ParallelTaskHelper
    CASA6 = True

    # CASA6 doesn't need default
    def default(atask):
        pass

    ctsys_resolve = ctsys.resolve
except ImportError:
    from __main__ import default
    from tasks import *
    from taskinit import *
    from parallel.parallel_task_helper import ParallelTaskHelper

    def ctsys_resolve(apath):
        dataPath = os.path.join(os.environ['CASAPATH'].split()[0],'data')
        return os.path.join(dataPath,apath)

from casatestutils.imagerhelpers import TestHelpers
th = TestHelpers()
from casatestutils import generate_weblog
from casatestutils import add_to_dict
from casatestutils import stats_dict

# Path to data
#data_path = '/lustre/rurvashi/StakeholderTests/VLASS/DATA_FOR_CAS12427/'
data_path = ctsys_resolve('stakeholders/vlass/')

##############################################################
############# Test settings #######################
## CFCAche settings. Default oversampling=20. Set to 2 to make the CFCache smaller, at the cost of accuracy
#os.environ['ATerm_CONVSIZE'] = '512'
#os.environ['ATerm_OVERSAMPLING'] = '2'

#cfcache_path  = data_path+'CFCACHE_OS2' # Made with above os.environ settings and wprojplanes=64
cfcache_path  = data_path+'CFCACHE'  # Made with DEFAULT os.environ settings and wprojplanes=16

## Number of iterations. This will test major and minor cycles and get better numerical results. (About 3 major cycles each)
#niter=100
## ( Set to niter=0 for a faster run and approximate results. It will still test for code changes.
niter=0

## A flag to run the tclean commands (True) as well as make the reports.
## False : Just remake the reports from existing outputs (False, useful for debugging the test scripts).
do_row=True

#(1)# Image all the fields in the MS to generate the full 'rowplot' plots and show full trends.
#fields = ['18954','18955','18956','18957','18958','18959']
#valcheck = 3  # index of field id 18957. Test for pass/fail on numbers from one field only.

#(2)# For a faster test run, do only one field.  No plots will be made, but the same pass/fail tests/checks apply.
fields = ['18957']
valcheck = 0  # index of field id 18957

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

# create baseclass and different setups for each dataset

## Base Test class with Utility functions
class test_base(unittest.TestCase):

    # Separate functions here, for special-case tests that need their own MS.
    def prepData(self,msname=""):
        self.dataset1 = 'vlass1.1_rowtest_6fields.ms' #'PcorrVis_field18954.ms'
        self.fieldname1 = fieldnames['18954']
        self.timerange1two = alltimes['18954']['two']

        self.dataset2 =  'vlass1.1_rowtest_6fields.ms' #'PcorrVis_field18957.ms'
        self.fieldname2 = fieldnames['18957']

    def getNameDoc(self):
        '''Run this function inside a test case'''
        testname=inspect.stack()[1][3]
        print("Test name  : " + testname)
        doc = eval('self.'+testname + '.__doc__')
        print("Doc : " +  doc)
        return testname, doc

    def image_stats(self,img,suf):
        self._myia.open(img+suf)
        rms = self._myia.statistics()['rms'][0]  # reads RMS value for image
        int_flux = self._myia.statistics()['sum'][0]  # reads integrated
                                                      # flux value for image
        major = self._myia.commonbeam()['major']['value']
        minor = self._myia.commonbeam()['minor']['value']
        pa = self._myia.commonbeam()['pa']['value']
        self._myia.close()

        return rms, int_flux, major, minor, pa

    def image_metrics(self, img, loc):
        # Read intensity, alpha and PB at source location
        intval = th.get_pix(img+'.image.tt0', loc)
        spxval = th.get_pix(img+'.alpha', loc)
        pbval = th.get_pix(img+'.pb.tt0', loc)

        # PB-correction
        if pbval>0.0:
            pbcor_intval = intval/pbval
        else:
            pbcor_intval=0.0 # null value

        return pbcor_intval, spxval, pbval

    def image_list(self,img,mode):

        imlist0 = [img+'.alpha', img+'.alpha.error',\
                   img+'.psf.tt0', img+'.psf.tt1', img+'.psf.tt2', \
                   img+'.residual.tt0', img+'.residual.tt1', \
                   img+'.pb.tt0', \
                   img+'.sumwt.tt0', img+'.sumwt.tt1', img+'.sumwt.tt2']

        imlist1 = [img+'.model.tt0', img+'.model.tt1', \
                   img+'.image.tt0', img+'.image.tt1']

        if mode == 'niterg0':
            return imlist0 + imlist1
        else:
            return imlist0

    def make_png(self, imname, out):
        self._myia.open(imname)
        pix = self._myia.getchunk()[:,:,0,0]
        self._myia.close()

        pl.imsave(fname=out, arr=numpy.transpose(pix), cmap='jet',origin='lower')

    def make_rowplot(self, fields, intvals, spxvals, pbvals, fname):
        pl.ioff()
        pl.clf()
        pl.figure(figsize=(8,6))
        fids=[]
        for ff in fields:
            fids.append(eval(ff))
        pl.plot( fids, intvals , 'o-', label='Sky Intensity' )
        pl.plot( fids, spxvals,'o-', label='Sky Alpha' )
        pl.plot( fids, pbvals,'o-', label='PB gain' )
        pl.hlines( y= 1.38 ,xmin=fids[0],xmax=fids[len(fids)-1],color='m')
        pl.hlines( y=-0.46 ,xmin=fids[0],xmax=fids[len(fids)-1],color='c')
        pl.axis([fids[0],fids[len(fids)-1],-2.5,2.5])
        pl.vlines(18956.7,-2.5,2.5,color='k',linestyle='--')
        pl.legend(loc='lower left')

        pl.savefig(fname)

##############################################
test_dict = {}
####    Tests     ####
class Test_vlass_1p1_row(test_base):

    @classmethod
    def setUpClass(cls):
        '''A class method called before tests in an individual class run'''
        # MS used for all tests of this class.
        cls.msfile = 'vlass1.1_rowtest_6fields.ms'
        os.symlink(data_path+cls.msfile, cls.msfile)

    def setUp(self):
        self.img = 'im_'
        self._myia = iatool()
        self.epsilon = 0.1 # sets epsilon as a percentage (10%)

        self.newACU = 'ea01, ea02, ea08, ea14, ea17, ea21'
        ##antenna = '0,1,6,12,15,19'  ## new ACU ants for this test dataset.
        ##antenna = '1,2,7,8,14,16,17,21,27,28' # Full list of ants with new ACUs (Summer 2018)
        self.oldACU = '!'+self.newACU

        # To use subdir in the output image names in some tests (CAS-10937)
        self.img_subdir = 'refimager_tst_subdir'
        self.parallel = False
        if ParallelTaskHelper.isMPIEnabled():
            self.parallel = True

    def tearDown(self):
        generate_weblog("tclean_VLASS_pipeline",test_dict)
        self._myia.done()
        # Delete the images created by each test case
        # Are they called tst* or im_test*
        os.system('rm -rf '+self.img+'*')
        os.system('rm -rf '+self.img_subdir)

    @classmethod
    def tearDownClass(cls):
        '''A class method called after tests in an individual class have run'''
        # Unlink the MS at the end of the class
        os.unlink(cls.msfile)

    @stats_dict(test_dict)
    def test_vlass_1p1_row_pcorr0(self):
        '''
        ---------------------------------------------------------------------------------------------------------------------------------------------
        VLASS 1.1 : No Pointing Corrections. MS field phasecenter is treated as the pointing center for all antennas.
                           Image each OTF field separately (to make a scan plot)
                           Use mtmfs with conjbeams True and test pb-corrected intensity and spectral index.

        Test that the intensity and spectral index are wrong (pick known wrong values).
        The plot should show a slope in intensity and spectral index arising from the average pointing offset
        in the majority of the antennas (ie. 20 old ACU ants) between the true pointing and the recorded phase center.

        This is the state of CASA 5.6 for usepointing=False
        ---------------------------------------------------------------------------------------------------------------------------------------------
        '''
        testname, testdoc = self.getNameDoc()

        intvals=[]
        spxvals=[]
        pbvals=[]

        #cfcache_oversampling1='CFCACHE_OS1'

        for field in fields:
            file_name = self.img + testname+'_'+field
            img = os.getcwd()+'/'+file_name
            #msname = data_path + 'PcorrVis_field'+field+'.ms'
#            msname = data_path+'vlass1.1_rowtest_6fields.ms'
            msname = self.msfile

            if do_row==True:
                os.system('rm -rf '+img+'.*')
                tclean(vis=msname, \
                       imagename=img, spw='2~17',antenna='', \
                       timerange='', \
                       field=fieldnames[field] ,uvrange='<12.0km',\
                       cell='0.3arcsec', imsize=10000, phasecenter="J2000 14:48:31.561 -16.18.56.120", \
                       specmode='mfs',nterms=2, reffreq='3.0GHz',\
                       deconvolver='mtmfs',weighting='uniform',robust=1.0,\
                       gridder='awproject', cfcache=cfcache_path,wbawp=True,\
                       pblimit=0.02,psterm=False, conjbeams=True,\
                       wprojplanes=16,niter=niter,datacolumn='data',\
                       mosweight=False, usepointing=False,
                       pointingoffsetsigdev=[300.0,300.0],\
                       parallel=self.parallel)

            intval, spxval, pbval = self.image_metrics(img=img, loc=[5792,4705,0,0])

            intvals.append(intval)
            spxvals.append(spxval)
            pbvals.append(pbval)

        if niter==0:   ## Approximate - initial residual image
            truth_int = 1.19
            truth_spx = -0.78
            truth_pb = 0.92
        else:   ## A bit more accurate after a few major cycles
            truth_int = 1.19
            truth_spx = -0.78
            truth_pb = 0.92

        out, report1 = th.check_val(  intvals[valcheck], truth_int, valname = 'PBCor Intensity', epsilon = truth_int*self.epsilon)
        out, report2 = th.check_val(  spxvals[valcheck], truth_spx, valname = 'Spectral Index', epsilon = self.epsilon)
        out, report3 = th.check_val(  pbvals[valcheck], truth_pb, valname = 'PB gain', epsilon = truth_pb*self.epsilon)
        report = report1 + report2 + report3
        report = report + 'Test values for field '+ fields[valcheck] + ' with name ' + fieldnames[fields[valcheck]] + '\n'

        test_dict[testname]['Test Description'] = testdoc
        test_dict[testname]['self.parallel'] = self.parallel
        test_dict[testname]['Report'] = report

        if len(fields)>1:
            self.make_rowplot(fields, intvals, spxvals, pbvals, testname+'_rowplot.png')
            test_dict[testname]['Figures'] = [testname+'_rowplot.png']

        add_to_dict(self, output = test_dict, dataset = msname)

        self.assertTrue(th.check_final(pstr = report), msg = report)

    @stats_dict(test_dict)
    def test_vlass_1p1_row_pcorr1_oneint(self):
        '''
        ---------------------------------------------------------------------------------------------------------------------------------------------
        VLASS 1.1 :  Corrections for antenna-groups (new/old ACUs). psd = [30,300]
                           Image each OTF field separately (to make a scan plot)
                           Use mtmfs with conjbeams True and test pb-corrected intensity and spectral index.
                           Image only one integration (first one per scan).

        Test that the intensity and spectral index are correct.
        This tests the antenna clustering using just one timestep of data (i.e. no time-dependent errors)

        [ If the second timestep is included (or chosen) the slope re-appears
           because with psf=[x,300], the pointing offset is read only from the first timestep in a VLASS scan. ]
        ---------------------------------------------------------------------------------------------------------------------------------------------
        '''
        testname, testdoc = self.getNameDoc()

#        fields = ['18954','18955','18956','18957','18958','18959']

        intvals=[]
        spxvals=[]
        pbvals=[]

        #cfcache_oversampling1='CFCACHE_OS1'

        for field in fields:
            file_name = self.img + testname+'_'+field
            img = os.getcwd()+'/'+file_name
            #msname = data_path + 'PcorrVis_field'+field+'.ms'
#            msname = data_path+'vlass1.1_rowtest_6fields.ms'
            msname = self.msfile

            if do_row==True:
                os.system('rm -rf '+img+'.*')
                tclean(vis=msname, \
                       imagename=img, spw='2~17',antenna='', \
                       timerange=alltimes[field]['one'], \
                       field=fieldnames[field] ,uvrange='<12.0km',\
                       cell='0.3arcsec', imsize=10000, phasecenter="J2000 14:48:31.561 -16.18.56.120", \
                       specmode='mfs',nterms=2, reffreq='3.0GHz',\
                       deconvolver='mtmfs',weighting='uniform',robust=1.0,\
                       gridder='awproject', cfcache=cfcache_path,wbawp=True,\
                       pblimit=0.02,psterm=False, conjbeams=True,\
                       wprojplanes=16,niter=niter,datacolumn='data',\
                       mosweight=False, usepointing=True,
                       pointingoffsetsigdev=[30.0, 300.0],\
                       parallel=self.parallel)

            intval, spxval, pbval = self.image_metrics(img=img, loc=[5792,4705,0,0])

            intvals.append(intval)
            spxvals.append(spxval)
            pbvals.append(pbval)

        if niter==0:   ## Approximate - initial residual image
            truth_int = 1.38
            truth_spx = -0.6
            truth_pb = 0.93
        else:   ## A bit more accurate after a few major cycles
            truth_int = 1.38
            truth_spx = -0.6
            truth_pb = 0.93

        out, report1 = th.check_val(  intvals[valcheck], truth_int, valname = 'PBCor Intensity', epsilon = truth_int*self.epsilon)
        out, report2 = th.check_val(  spxvals[valcheck], truth_spx, valname = 'Spectral Index', epsilon = self.epsilon)
        out, report3 = th.check_val(  pbvals[valcheck], truth_pb, valname = 'PB gain', epsilon = truth_pb*self.epsilon)

        report = report1 + report2 + report3
        report = report + 'Test values for field '+ fields[valcheck] + ' with name ' + fieldnames[fields[valcheck]] + '\n'

        test_dict[testname]['Test Description'] = testdoc
        test_dict[testname]['self.parallel'] = self.parallel
        test_dict[testname]['Report'] = report

        if len(fields)>1:
            self.make_rowplot(fields, intvals, spxvals, pbvals, testname+'_rowplot.png')
            test_dict[testname]['Figures'] = [testname+'_rowplot.png']

        add_to_dict(self, output = test_dict, dataset = msname)

        self.assertTrue(th.check_final(pstr = report), msg = report)

    @stats_dict(test_dict)
    @unittest.skipIf(True, "Skip the test until partitioning-related differences are removed")
    def test_vlass_1p1_row_pcorr1_twoint(self):
        '''
        ---------------------------------------------------------------------------------------------------------------------------------------------
       VLASS 1.1 :  Corrections for antenna-groups (new/old ACUs). psd = [30,300]
                            Image each OTF field separately (to make a scan plot)
                           Use mtmfs with conjbeams True and test pb-corrected intensity and spectral index.
                           Image both integrations in each scan.

        Test that the intensity and spectral index are wrong. Antenna clustering is applied, but time dependence it not.

        If the second timestep is included (or chosen) the slope re-appears
        because with psf=[x,300], the pointing offset is read only from the first timestep in a VLASS scan.

        This is a redundant test.  It need not be included in stakeholder verification tests, but is useful to illustrate
        an effect.
        ---------------------------------------------------------------------------------------------------------------------------------------------
        '''
        testname, testdoc = self.getNameDoc()

#        fields = ['18954','18955','18956','18957','18958','18959']

        intvals=[]
        spxvals=[]
        pbvals=[]

        #cfcache_oversampling1='CFCACHE_OS1'

        for field in fields:
            file_name = self.img + testname+'_'+field
            img = os.getcwd()+'/'+file_name
            #msname = data_path + 'PcorrVis_field'+field+'.ms'
#            msname = data_path+'vlass1.1_rowtest_6fields.ms'
            msname = self.msfile

            if do_row==True:
                os.system('rm -rf '+img+'.*')
                tclean(vis=msname, \
                       imagename=img, spw='2~17',antenna='', \
                       timerange='', \
                       field=fieldnames[field] ,uvrange='<12.0km',\
                       cell='0.3arcsec', imsize=10000, phasecenter="J2000 14:48:31.561 -16.18.56.120", \
                       specmode='mfs',nterms=2, reffreq='3.0GHz',\
                       deconvolver='mtmfs',weighting='uniform',robust=1.0,\
                       gridder='awproject', cfcache=cfcache_path,wbawp=True,\
                       pblimit=0.02,psterm=False, conjbeams=True,\
                       wprojplanes=16,niter=niter,datacolumn='data',\
                       mosweight=False, usepointing=True,
                       pointingoffsetsigdev=[30.0, 300.0],\
                       parallel=self.parallel)

            intval, spxval, pbval = self.image_metrics(img=img, loc=[5792,4705,0,0])

            intvals.append(intval)
            spxvals.append(spxval)
            pbvals.append(pbval)

        if niter==0:   ## Approximate - initial residual image
            truth_int = 1.17
            truth_spx = -0.77
            truth_pb = 0.93
        else:   ## A bit more accurate after a few major cycles
            truth_int = 1.17
            truth_spx = -0.77
            truth_pb = 0.93

        out, report1 = th.check_val(  intvals[valcheck], truth_int, valname = 'PBCor Intensity', epsilon = truth_int*self.epsilon)
        out, report2 = th.check_val(  spxvals[valcheck], truth_spx, valname = 'Spectral Index', epsilon = self.epsilon)
        out, report3 = th.check_val(  pbvals[valcheck], truth_pb, valname = 'PB gain', epsilon = truth_pb*self.epsilon)
        report = report1 + report2 + report3

        report = report + 'Test values for field '+ fields[valcheck] + ' with name ' + fieldnames[fields[valcheck]] + '\n'

        test_dict[testname]['Test Description'] = testdoc
        test_dict[testname]['self.parallel'] = self.parallel
        test_dict[testname]['Report'] = report

        if len(fields)>1:
            self.make_rowplot(fields, intvals, spxvals, pbvals, testname+'_rowplot.png')
            test_dict[testname]['Figures'] = [testname+'_rowplot.png']

        add_to_dict(self, output = test_dict, dataset = msname)

        self.assertTrue(th.check_final(pstr = report), msg = report)

    @stats_dict(test_dict)
    def test_vlass_1p1_row_pcorr1and2_twoint(self):
        '''
        ---------------------------------------------------------------------------------------------------------------------------------------------
        VLASS 1.1 :  Corrections for antenna-groups (new/old ACUs). and time-dependent nodding :psd = [30,30]
                           Image each OTF field separately (to make a scan plot)
                           Use mtmfs with conjbeams True and test pb-corrected intensity and spectral index.
                           Use both integrations per scan

        Test that the intensity and spectral index are correct.

        This is the setting most optimal for VLASS.  Perhaps the stakeholder test needs just this one run.
        ---------------------------------------------------------------------------------------------------------------------------------------------
        '''
        testname, testdoc = self.getNameDoc()

#        fields = ['18954','18955','18956','18957','18958','18959']

        intvals=[]
        spxvals=[]
        pbvals=[]

        #cfcache_oversampling1='CFCACHE_OS1'

        for field in fields:
            file_name = self.img + testname+'_'+field
            img = os.getcwd()+'/'+file_name
            #msname = data_path + 'PcorrVis_field'+field+'.ms'
#            msname = data_path+'vlass1.1_rowtest_6fields.ms'
            msname = self.msfile

            if do_row==True:
                os.system('rm -rf '+img+'.*')
                tclean(vis=msname, \
                       imagename=img, spw='2~17',antenna='', \
                       timerange='', \
                       field=fieldnames[field] ,uvrange='<12.0km',\
                       cell='0.3arcsec', imsize=10000, phasecenter="J2000 14:48:31.561 -16.18.56.120", \
                       specmode='mfs',nterms=2, reffreq='3.0GHz',\
                       deconvolver='mtmfs',weighting='uniform',robust=1.0,\
                       gridder='awproject', cfcache=cfcache_path,wbawp=True,\
                       pblimit=0.02,psterm=False, conjbeams=True,\
                       wprojplanes=16,niter=niter,datacolumn='data',\
                       mosweight=False, usepointing=True,
                       pointingoffsetsigdev=[30.0, 30.0],\
                       parallel=self.parallel)

            intval, spxval, pbval = self.image_metrics(img=img, loc=[5792,4705,0,0])

            intvals.append(intval)
            spxvals.append(spxval)
            pbvals.append(pbval)

        if niter==0:   ## Approximate - initial residual image
            truth_int = 1.32
            truth_spx = -0.66
            truth_pb = 0.84
        else:   ## A bit more accurate after a few major cycles
            truth_int = 1.32
            truth_spx = -0.58
            truth_pb = 0.84

        out, report1 = th.check_val(  intvals[valcheck], truth_int, valname = 'PBCor Intensity', epsilon = truth_int*self.epsilon)
        out, report2 = th.check_val(  spxvals[valcheck], truth_spx, valname = 'Spectral Index', epsilon = self.epsilon)
        out, report3 = th.check_val(  pbvals[valcheck], truth_pb, valname = 'PB gain', epsilon = truth_pb*self.epsilon)

        report = report1 + report2 + report3
        report = report + 'Test values for field '+ fields[valcheck] + ' with name ' + fieldnames[fields[valcheck]] + '\n'

        test_dict[testname]['Test Description'] = testdoc
        test_dict[testname]['self.parallel'] = self.parallel
        test_dict[testname]['Report'] = report

        if len(fields)>1:
            self.make_rowplot(fields, intvals, spxvals, pbvals, testname+'_rowplot.png')
            test_dict[testname]['Figures'] = [testname+'_rowplot.png']

        add_to_dict(self, output = test_dict, dataset = msname)

        self.assertTrue(th.check_final(pstr = report), msg = report)

    @stats_dict(test_dict)
    def test_vlass_1p1_jointmosaic_pcorr1and2(self):
        '''
        ---------------------------------------------------------------------------------------------------------------------------------------------
        VLASS 1.1 : With both types of pointing correction ( antenna clusters and time-dependent nodding )
                                        : posigdev=[30,30]
                           Use two scans/fields and all antennas together to test the field mosaic pointing offset.
                           Use mtmfs with conjbeams True and test pb-corrected intensity and spectral index.

        Test that the intensity and spectral index are correct.

        This is the setting most optimal for VLASS.
        ---------------------------------------------------------------------------------------------------------------------------------------------
        '''
        testname, testdoc = self.getNameDoc()

#        file_name = 'im_'+testname
        file_name = self.img + testname
        self.prepData()

        img = os.getcwd()+'/'+file_name
#        msname = data_path+'vlass1.1_rowtest_6fields.ms'
        msname = self.msfile

        if do_row==True:
            os.system('rm -rf '+img+'.*')
            tclean(vis=msname, \
                   imagename=img, spw='2~17',antenna='', \
                   timerange='', \
                   field='1~4' ,uvrange='<12.0km',\
                   cell='0.3arcsec', imsize=10000, phasecenter="J2000 14:48:31.561 -16.18.56.120", \
                   specmode='mfs',nterms=2, reffreq='3.0GHz',\
                   deconvolver='mtmfs',weighting='uniform',robust=1.0,\
                   gridder='awproject', cfcache=cfcache_path,wbawp=True,\
                   pblimit=0.02,psterm=False, conjbeams=True,\
                   wprojplanes=16,niter=niter,datacolumn='data',\
                   mosweight=False, usepointing=True,
                   pointingoffsetsigdev=[30.0, 30.0],\
                   parallel=self.parallel)

        intval, spxval, pbval = self.image_metrics(img=img, loc=[5792,4705,0,0])

        if niter==0:   ## Approximate - initial residual image
            truth_int = 1.3
            truth_spx = -0.67
            truth_pb = 0.94
        else:   ## A bit more accurate after a few major cycles
            truth_int = 1.3
            truth_spx = -0.5
            truth_pb = 0.94

        #report0 = th.check_imexist( self.image_list( img, 'niter0') )
        out, report1 = th.check_val(  intval, truth_int, valname = 'allants : PBCor Intensity', epsilon = truth_int*self.epsilon)
        out, report2 = th.check_val(  spxval, truth_spx, valname = 'allants : Spectral Index', epsilon = self.epsilon)
        out, report3 = th.check_val(  pbval, truth_pb, valname = 'allants : PB gain', epsilon = truth_pb*self.epsilon)

        report = report1 + report2 +report3

        test_dict[testname]['Test Description'] = testdoc

        test_dict[testname]['self.parallel'] = self.parallel
        test_dict[testname]['Report'] = report

        self.make_png(img+'.pb.tt0', out = img+'.pb.tt0.png')
        test_dict[testname]['Figures'] = [img+'.pb.tt0.png']

        add_to_dict(self, output = test_dict, dataset = self.dataset1)

        self.assertTrue(th.check_final(pstr = report), msg = report)

def suite():
     return [Test_vlass_1p1_row] #[Test_tclean_ALMA]

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


# -*- coding: utf-8 -*-
import json

from django.shortcuts import render, get_object_or_404, redirect
from django.db import connections
from django.db.models import Count, Sum, Q, F
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.utils import timezone

from .models import Candidates, Terms, Intent, Intent_Likes
from .forms import IntentForm
from commontag.views import paginate

def intents(request, election_year):
    ref = {
        'create_at': 'id',
        'likes': 'likes'
    }
    order_by = ref.get(request.GET.get('order_by'), 'likes')
    intents = Intent.objects.filter(election_year=election_year).order_by('-%s' % order_by)
    intents = paginate(request, intents)
    return render(request, 'candidates/intents.html', {'intents': intents, 'election_year': election_year})

def districts(request, election_year, county):
    districts = Terms.objects.filter(election_year=election_year, county=county)\
                             .values('constituency', 'district')\
                             .annotate(candidates=Count('id'))\
                             .order_by('constituency')
    return render(request, 'candidates/districts.html', {'election_year': election_year, 'county': county, 'districts': districts})

def district(request, election_year, county, constituency):
    candidates = Terms.objects.filter(election_year=election_year, county=county, constituency=constituency).order_by('-votes')
    return render(request, 'candidates/district.html', {'election_year': election_year, 'county': county, 'district': candidates[0].district, 'candidates': candidates})

def intent_home(request):
    return render(request, 'candidates/intent_home.html', )

def intent_upsert(request):
    election_year = '2018'
    if not request.user.is_authenticated:
        return redirect(reverse('candidates:intent_home'))
    try:
        instance = Intent.objects.get(user=request.user, election_year=election_year)
    except:
        instance = None
    if request.method == 'GET':
        form = IntentForm(instance=instance)
        form.fields['name'].initial = request.user.last_name + request.user.first_name
    if request.method == 'POST':
        form = IntentForm(request.POST, instance=instance)
        if form.has_changed() and form.is_valid():
            intent = form.save(commit=False)
            intent.user = request.user
            intent.status = 'intent_apply'
            intent.save()
            c = connections['default'].cursor()
            history = request.POST.copy()
            history.pop('csrfmiddlewaretoken', None)
            history['modify_at'] = timezone.now().strftime('%Y-%m-%d %H:%M:%S')
            c.execute('''
                UPDATE candidates_intent
                SET history = (%s::jsonb || COALESCE(history, '[]'::jsonb))
                WHERE user_id = %s AND election_year = %s
            ''', [json.dumps([history]), request.user.id, election_year])
        return redirect(reverse('candidates:intent_detail', kwargs={'intent_id': instance.uid if instance else intent.uid}))
    return render(request, 'candidates/intent_upsert.html', {'form': form})

def intent_detail(request, intent_id):
    intent = get_object_or_404(Intent.objects.select_related('user'), uid=intent_id)
    if request.user.is_authenticated:
        user_liked = Intent_Likes.objects.filter(intent_id=intent_id, user=request.user).exists()
        if request.method == 'POST':
            if request.POST.get('decision') == 'upvote' and not user_liked:
                Intent_Likes.objects.create(intent_id=intent_id, user=request.user)
                intent.likes += + 1
                intent.save(update_fields=["likes"])
                user_liked = True
            elif request.POST.get('decision') == 'downvote' and user_liked:
                Intent_Likes.objects.filter(intent_id=intent_id, user=request.user).delete()
                intent.likes -= 1
                intent.save(update_fields=["likes"])
                user_liked = False
    return render(request, 'candidates/intent_detail.html', {'intent': intent, 'user_liked': request.user.is_authenticated and user_liked})

def pc(request, candidate_id, election_year):
    candidate = get_object_or_404(Terms.objects, election_year=election_year, candidate_id=candidate_id)
    return render(request, 'candidates/pc.html', {'candidate': candidate})


from shader import *
import re

blockRegex = re.compile(r"(\w*?)\s* (\[[^\].]*\])? \s* (?:\-\> \s* (\(.*?\))?)? \s*\< (.*?) \>\s*?", re.VERBOSE | re.DOTALL)

def parseBlock( blockMatch ):
    label = blockMatch.group(1)

    version = re.sub( r'\[|\]', r'', str(blockMatch.group(2)) )
    if version == "None":
        version = None

    output = re.sub( r'\(|\)', r'', str(blockMatch.group(3)) )
    if output == "None":
        output = None

    body = blockMatch.group(4)

    if label != None and body != None:
        body = re.findall('..*?\n', body)
        return Block(label, version, output, body)

    return None

def parseShaderFromFile( path ):
    f = open(path)
    shader = parseShader(f.read())
    f.close()
    return shader

def parseShader( text ):
    if not text:
        return None

    lines = [l.split('//')[0].strip() for l in text.split('\n') if len(l) > 0]
    lines = [l for l in lines if len(l)>0]
    blocks = {}
    block = None

    for blockCode in [l for l in re.finditer(blockRegex, '\n'.join(lines))]:
        block = parseBlock(blockCode)
        if block != None:
            blocks.update(block.list())

    if all(x in blocks for x in ["uniforms","attributes","vertex","fragment"]):
        return Shader(blocks.get("uniforms"),blocks.get("attributes"), blocks.get("vertex_output"),blocks.get("vertex"), blocks.get("fragment"))
    return None


"""
Implementation of Weather-Umbrella HMM domain from AIMA3.

BE3M33UI - Artificial Intelligence course, FEE CTU in Prague
"""

from hmm import HMM

# State R
R_domain = {'+rain', '-rain'}
# Observation U
U_domain = {'+umb', '-umb'}

# Transition model
default_T = {
    '-rain':
        {'-rain': 0.7,
         '+rain': 0.3},
    '+rain':
        {'-rain': 0.3,
         '+rain': 0.7}
}

# Sensor (emission) model
default_E = {
    '-rain':
        {'+umb': 0.2,
         '-umb': 0.8},
    '+rain':
        {'+umb': 0.9,
         '-umb': 0.1}
}

class WeatherHMM(HMM):

    def __init__(self, X_domain=None, E_domain=None, trans_model=None, emission_model=None):
        self.X_domain = X_domain if X_domain else R_domain
        self.E_domain = E_domain if E_domain else U_domain
        self.T = trans_model if trans_model else default_T
        self.E = emission_model if emission_model else default_E

    def get_states(self):
        return self.X_domain

    def get_observations(self):
        return self.E_domain

    def pt(self, cur_state, next_state):
        """Return the probability of transition from current to next state"""
        return self.T[cur_state][next_state]

    def pe(self, state, obs):
        """Return the probability of transition from current to next state"""
        return self.E[state][obs]


"""
截面收益计算
输入x和y均为一列的数组形式, 且已处理过nan值
"""
import numpy as np
import pandas as pd
from cpa.utils import bar

def IC(x, y):
    """Pearson 相关系数"""
    x_ = x - x.mean(axis=0)
    y_ = y - y.mean(axis=0)
    xy = x_ * y_
    x2 = x_ ** 2
    y2 = y_ ** 2
    return xy.sum(axis=0) / np.sqrt(x2.sum(axis=0) * y2.sum(axis=0))

def RKIC(x, y):
    """Spearman 相关系数"""
    xNew = np.argsort(np.argsort(x))
    yNew = np.argsort(np.argsort(y))
    x_ = xNew - xNew.mean(axis=0)
    y_ = yNew - yNew.mean(axis=0)
    xy = x_ * y_
    x2 = x_ ** 2
    y2 = y_ ** 2
    return xy.sum(axis=0) / np.sqrt(x2.sum(axis=0) * y2.sum(axis=0))

def BETA(x, y):
    """单因子回归斜率"""
    x_ = x - x.mean(axis=0)
    y_ = y - y.mean(axis=0)
    xy = x_ * y_
    x2 = x_ ** 2
    return xy.sum(axis=0) / x2.sum(axis=0)

#暂时不用GPIC这个函数
def GPIC(x, y, groupNum):
    """分n组后的相关系数"""
    xRank = np.argsort(np.argsort(x))  # 计算序号:值越大序号越大
    xQuantile = xRank / len(x)  # 计算分位数
    groupIDs = np.array(range(1, groupNum + 1))  # 设置1-groupNum的组号
    groupMeanY = np.zeros([groupNum])  # 设置groupNum大小的空间存放每组平均收益
    xGroupRank = np.ceil(xQuantile * groupNum)  # 确定每个x值分别属于哪一组
    for dumi in range(1, groupNum + 1):
        groupIdx = xGroupRank == dumi  # 提取dumi组信息
        groupMeanY[dumi - 1] = np.sum(groupIdx * y, axis=0) / np.sum(groupIdx, axis=0)  # 计算该组平均收益率
    return IC(groupIDs, groupMeanY)

def TBDF(lastFactor, thisReturn, cut):
    """top平均收益 - bottom平均收益"""
    lastFactorRank = np.argsort(np.argsort(lastFactor))  # 计算序号:值越大序号越大
    lastFactorQuantile = lastFactorRank / len(lastFactor)  # 计算分位数
    topIdx = lastFactorQuantile >= 1 - cut  # 筛选出top组
    topRet = (thisReturn * topIdx).mean(axis=0) / topIdx.mean(axis=0)  # 计算top组平均收益
    botIdx = lastFactorQuantile < cut  # 筛选出bottom组
    botRet = (thisReturn * botIdx).mean(axis=0) / botIdx.mean(axis=0)  # 计算bottom组平均收益
    tbdf = topRet - botRet
    return tbdf

#暂时不用TURN这个函数
def TURN(lastTop, thisTop):
    turn = 1 - len(set(lastTop) & set(thisTop)) / len(lastTop)
    return turn

def ICGrouping(lastFactor, thisReturn, nGroup):
    '''
    :param lastFactor: 上期因子值
    :param thisReturn: 本期收益
    :param nGroup: 分组数
    :return:  分组计算各组的IC值
    '''
    lastFactorRank = np.argsort(np.argsort(lastFactor))  # 计算序号:值越大序号越大
    lastFactorQuantile = lastFactorRank / len(lastFactor)  # 计算分位数
    colCount = lastFactor.__len__()
    amountOfGroup = colCount // nGroup  # 计算每组股票数
    GroupingIC=[]
    lastFactor=pd.Series(lastFactor)
    thisReturn=pd.Series(thisReturn)
    for i in range(nGroup):
        groupIdx = ((colCount - i * amountOfGroup) > lastFactorRank) & (
                lastFactorRank >= (colCount - (i + 1) * amountOfGroup))  # 先筛选出相应的分组的股票，列表的值为TRUE和FALSE

        segFactor=lastFactor.iloc[groupIdx]
        segReturn=thisReturn.iloc[groupIdx]     #分别选出该组的 前一天因子值 和 当前的收益
        GroupingIC.append(IC(segFactor,segReturn))      #调用IC函数，计算该组的IC值
    return GroupingIC

def RankICGrouping(lastFactor, thisReturn, nGroup):
    '''
    :param lastFactor: 上期因子值
    :param thisReturn: 本期收益
    :param nGroup: 分组数
    :return: 分别计算各组的RankIC值
    '''
    lastFactorRank = np.argsort(np.argsort(lastFactor))  # 计算序号:值越大序号越大
    lastFactorQuantile = lastFactorRank / len(lastFactor)  # 计算分位数
    colCount = lastFactor.__len__()
    amountOfGroup = colCount // nGroup  # 计算每组股票数
    GroupingRankIC=[]
    lastFactor=pd.Series(lastFactor)
    thisReturn=pd.Series(thisReturn)
    for i in range(nGroup):
        groupIdx = ((colCount - i * amountOfGroup) > lastFactorRank) & (
                lastFactorRank >= (colCount - (i + 1) * amountOfGroup))  # 先筛选出相应的分组的股票，列表的值为TRUE和FALSE
        segFactor=lastFactor.iloc[groupIdx]
        segReturn=thisReturn.iloc[groupIdx]           #分别选出该组的 前一天因子值 和 当前的收益
        GroupingRankIC.append(RKIC(segFactor,segReturn))    #调用 RKIC函数，计算该组的RankIC
    return GroupingRankIC

def TurnGrouping(lastGrouping,thisGrouping, nGroup):
    '''
    :param lastGrouping: 之前持仓的股票
    :param thisGrouping: 目前根据因子计算，需要持仓的股票
    :param nGroup: 分组数量
    :return: 返回一个列表，元素是各组的当期换手率，第0个元素是top组
    '''
    turn=[]
    for i in range(nGroup):
        last = lastGrouping[i]
        this = thisGrouping[i]
        #换手率计算分两部分：
        #   1.上期持有，这期继续持有的票， 但是可能会增加持仓比例
        #   2.上期没有，这期要买入的票
        turnTmp = len(set(last) & set(this))*max((1/len(this)-1/len(last)),0) + \
                  1 - len(set(last) & set(this)) / len(last)
        turn.append(turnTmp)
    return turn

def Grouping(lastFactor, nGroup):
    '''
    :param lastFactor: 上期的因子值
    :param nGroup: 分组数
    :return: 根据传入的因子打分结果，按照得分高低，把股票分组，返回分组列表。列表的每个元素也是一个列表，对应该组别的True false
            返回的TRUE FALSE 列表的长度为，去除了空值后的长度
    '''
    lastFactorRank = np.argsort(np.argsort(lastFactor))  # 计算序号:值越大序号越大
    lastFactorQuantile = lastFactorRank / len(lastFactor)  # 计算分位数
    colCount = lastFactor.__len__()
    amountOfGroup = colCount // nGroup  # 计算每组股票数
    GroupingResult = []
    lastFactor = pd.Series(lastFactor)
    for i in range(nGroup):
        groupIdx = ((colCount - i * amountOfGroup) > lastFactorRank) & (
                lastFactorRank >= (colCount - (i + 1) * amountOfGroup))  # 先筛选出相应的分组的股票，列表的值为TRUE和FALSE
                                                                            # 分别选出该组的 前一天因子值 和 当前的收益
        GroupingResult.append(groupIdx)  # 把每一组的选出的股票记录（TRUE FALSE 列表）
    return GroupingResult

def adjustShape(GroupingResult,length,notNanLocate):
    '''
    :param GroupingResult: 由Grouping函数返回的列表
    :param length:  需要填充到的最终长度
    :param notNanLocate: 非空元素对应的位置
    :return: 返回一个列表，元素为子列表，每个子列表的长度填充为length
    '''
    adjusted=[]
    for i in range(len(GroupingResult)):
        wholeList=[False]*length
        for j in range(len(notNanLocate)):
            wholeList[notNanLocate[j]]=GroupingResult[i][j] #对于每个子列表，把GroupingResult中的TRUE、FALSE 按照notNanLocate填充进去
        adjusted.append(wholeList)
    return adjusted

def GroupRet(lastFactor, thisReturn, nGroup):
    '''
    :param lastFactor:
    :param thisReturn:
    :param nGroup:
    :return: 每组收益的list。从左数第一组为top组
    '''
    lastFactorRank = np.argsort(np.argsort(lastFactor))  # 计算序号:值越大序号越大
    lastFactorQuantile = lastFactorRank / len(lastFactor)  # 计算分位数
    colCount = lastFactor.__len__()
    amountOfGroup = colCount // nGroup  # 计算每组股票数
    retRaw = []
    for i in range(nGroup):
        groupIdx = ((colCount - i * amountOfGroup) > lastFactorRank) & (
                    lastFactorRank >= (colCount - (i + 1) * amountOfGroup))  # 先筛选出该组的股票
        groupRet = (thisReturn * groupIdx).sum(axis=0) / groupIdx.sum(axis=0)  # 再计算平均收益平均收益
        retRaw.append(groupRet)
    return retRaw

"""下面6个函数是对计算好的panel进行简单运算的函数"""
def RET(df):
    '''
    :param df:
    :return: 返回各组累计收益
    '''
    ret = df.sum(axis=0)
    ret.name = 'Ret'
    return ret

def MeanIC(df):
    '''
    :param df:
    :return: 返回各组平均IC
    '''
    IC_=df.mean(axis=0)
    IC_.name='IC'
    return IC_

def MeanRankIC(df):
    '''
    :param df:
    :return:返回各组平均RankIC
    '''
    IC_=df.mean(axis=0)
    IC_.name='RankIC'
    return IC_

def MeanTurn(df):
    '''
    :param df:
    :return: 返回每组平均换手率
    '''
    Turn_=df.iloc[1:].mean(axis=0)   #去除第一行，因为之前为了保持panel形状一致，在0时刻的换手率设为100%。 因此求平均时去除。
    Turn_.name='Turn'
    return Turn_

def SumCost(df):
    '''
    :param df:
    :return: 返回每组的累计交易成本
    '''
    Cost_ = df.sum(axis=0)
    Cost_.name = 'Cost'
    return Cost_

def MeanNumber(df):
    '''
    :param df:
    :return: 返回每组平均的持仓资产数量
    '''
    number_=df.mean(axis=0)
    number_.name='Num'
    return number_

def SHARP(df, frequency):
    if frequency == bar.Frequency.MINUTE:
        period = 252 * 4 * 60
    elif frequency == bar.Frequency.MINUTE5:
        period = 252 * 4 * 12
    elif frequency == bar.Frequency.MINUTE30:
        period = 252 * 4 * 2
    elif frequency == bar.Frequency.HOUR:
        period = 252 * 4
    elif frequency == bar.Frequency.HOUR2:
        period = 252 * 2
    elif frequency == bar.Frequency.DAY:
        period = 252
    else:
        raise ValueError('必须输入计算频率')

    # 注意是否要乘波动率
    excessEarning = df + int(1) - 1.035 ** (1 / period)
    sharpRaw = excessEarning.mean() / excessEarning.std()
    sharpRaw = sharpRaw * period ** 0.5
    sharpRaw.name = 'Sharp'
    return sharpRaw

def maxDrawDown(df):
    tempData1 = (df + 1).cumprod().expanding().max()  # 从开始到当期净值最高点
    tempData2 = (df + 1).cumprod() / tempData1  # 从开始到现在每一期的最大回撤
    maxdd = tempData2.min() - 1
    maxdd.name = 'MaxDD'
    return maxdd


# Витя работает недалеко от одной из станций кольцевой линии Московского метро,
# а живет рядом с другой станцией той же линии.
# Требуется выяснить, мимо какого наименьшего количества промежуточных станций необходимо проехать Вите по кольцу,
# чтобы добраться с работы домой.

def read() -> (int, int, int):
    with open('input.txt', 'r') as fin:
        return map(int, fin.readline().split())

if __name__ == '__main__':
    N, i, j = read()
    res = min(
        abs(i - j) - 1,  # по часовой
        abs(i + N - j) - 1,  # против, j слева от N
        abs(j + N - i) - 1  # против, i слева от N
    )
    print(res)


import logging

from airflow.exceptions import AirflowException
from airflow.utils.decorators import apply_defaults

from plugins.bioinf_mixin.operators import BioinfMixinOperator

class GatkOperator(BioinfMixinOperator):
    """Execute GATK task.

    Warning:
        If you are using GenotypeConcordance, pass input_files like this [eval, comp].

    Arguments:
        jar (str): Full path to GATK jar
        analysis_type (str): GATK -T parameter
        ram (int): GB of RAM to allocate
        gatk_params (str): Advanced GATK parameters
        intervals (str): GATK -L parameter
        reference (str): Full path to genome reference
        input_files: Full path to input files. Can pass in list or comma delimited string if GATK tool accepts mutiple files
        output (str): Full path to write output
        run_with_qsub (bool): Run as qsub job?
    """

    ui_color = '#F47321'
    ui_fgcolor = '#FFFFFF'

    @apply_defaults
    def __init__(self, jar, analysis_type, reference, input_files, output, ram=4,
                 gatk_params=None, intervals=None, run_with_qsub=True, *args, **kwargs):
        super(GatkOperator, self).__init__(*args, **kwargs)
        self.jar = jar
        self.analysis_type = analysis_type
        self.reference = reference
        self.input_files = input_files
        self.output = output
        self.ram = ram
        self.gatk_params = gatk_params
        self.intervals = intervals
        self.run_with_qsub = run_with_qsub

        if type(input_files) == str:
            self.input_files = input_files.strip().split(',')

    def prepare_command(self):
        """Build GATK command line."""

        command = 'java -Xmx{0}g -jar {1} -T {2} -R {3}'.format(self.ram, self.jar, self.analysis_type, self.reference)

        if self.gatk_params:
            command += ' {0}'.format(self.gatk_params)

        if self.intervals:
            command += ' -L {0}'.format(self.intervals)

        if self.input_files[0].endswith(('.vcf', '.vcf.gz')):
            gatk_input = '-V {0}'.format(' -V '.join(self.input_files))
        elif self.input_files[0].endswith(('.bam', '.cram')):
            gatk_input = '-I {0}'.format(' -I '.join(self.input_files))
        else:
            logging.error('Unrecognized file type: {0}'.format(self.input_files[0]))
            raise AirflowException('Unrecognized file type: {0}'.format(self.input_files[0]))

        # GATK GenotypeConcordance requires special input
        if self.analysis_type == 'GenotypeConcordance':
            if len(self.input_files) != 2:
                logging.error('-T {0} accepts only 2 input files!'.format(self.analysis_type))
                raise AirflowException('-T {0} accepts only 2 files!'.format(self.analysis_type))
            gatk_input = '-eval {0} -comp {1}'.format(self.input_files[0], self.input_files[1])

        command += ' {0} -o {1}'.format(gatk_input, self.output)

        return command


import json, codecs, os
from tqdm import tqdm
import numpy as np

# 标签路径
labelme_path = "/home/data1/competition/data/tianzhi2/CASIA-Ship/train/mask"  # 原始labelme标注数据路径
saved_path = "/home/data1/competition/data/tianzhi2/CASIA-Ship/train/Annotations"  # 保存路径
os.makedirs(saved_path, exist_ok=True)

# 获取待处理文件
files = os.listdir(labelme_path)
files = list(filter(lambda x: x.endswith('json'), files))
files = [i.split("/")[-1].split(".json")[0] for i in files]

# 读取标注信息并写入 xml
for json_file_ in tqdm(files):
    json_filename = os.path.join(labelme_path, json_file_ + ".json")
    json_file = json.load(open(json_filename, "r", encoding="utf-8"))
    height, width, channels = json_file['imageHeight'], json_file['imageWidth'], 3
    with codecs.open(os.path.join(saved_path, json_file_ + ".xml"), "w", "utf-8") as xml:
        xml.write('<annotation>\n')
        xml.write('\t<folder>' + 'VOC' + '</folder>\n')
        xml.write('\t<filename>' + json_file_ + ".jpg" + '</filename>\n')
        xml.write('\t<size>\n')
        xml.write('\t\t<width>' + str(width) + '</width>\n')
        xml.write('\t\t<height>' + str(height) + '</height>\n')
        xml.write('\t\t<depth>' + str(channels) + '</depth>\n')
        xml.write('\t</size>\n')
        xml.write('\t\t<segmented>0</segmented>\n')
        for multi in json_file["shapes"]:
            points = np.array(multi["points"])
            xmin = min(points[:, 0])
            xmax = max(points[:, 0])
            ymin = min(points[:, 1])
            ymax = max(points[:, 1])
            label = multi["label"]
            if xmax <= xmin:
                pass
            elif ymax <= ymin:
                pass
            else:
                xml.write('\t<object>\n')
                xml.write('\t\t<name>' + label + '</name>\n')
                xml.write('\t\t<pose>Unspecified</pose>\n')
                xml.write('\t\t<truncated>0</truncated>\n')
                xml.write('\t\t<difficult>0</difficult>\n')
                xml.write('\t\t<bndbox>\n')
                xml.write('\t\t\t<xmin>' + str(int(round(xmin))) + '</xmin>\n')
                xml.write('\t\t\t<ymin>' + str(int(round(ymin))) + '</ymin>\n')
                xml.write('\t\t\t<xmax>' + str(int(round(xmax))) + '</xmax>\n')
                xml.write('\t\t\t<ymax>' + str(int(round(ymax))) + '</ymax>\n')
                xml.write('\t\t</bndbox>\n')
                xml.write('\t</object>\n')

        xml.write('</annotation>')


# -*- coding: utf-8 -*-
from osv import osv
from osv import fields
from tools.translate import _
import time
import pooler

class res_partner(osv.osv):
	_name = 'res.partner'
	_inherit = 'res.partner'
	_columns = {
		'name': fields.char('Name', size=256, required=True, select=True),
		'rfc': fields.char('RFC', size=18),
		'lada_telefono': fields.char('Lada Telefonica', size=5),
		'email2': fields.char('2do. Email', size=240),
		'email3': fields.char('3er. Email', size=240),
		'numero_exterior': fields.char('Numero exterior', size=30),
		'numero_interior': fields.char('Numero interior', size=30),
		'type': fields.selection([('default', 'Default'), ('invoice', 'Invoice'),
		   ('obra', 'Obra'), ('oficinas_centrales', 'Oficinas Centrales'),
		   ('delivery', 'Shipping'), ('contact', 'Contact'),
		   ('other', 'Other')], 'Address Type',
            help="Used to select automatically the right address according to the context in sales and purchases documents."),
	}


import random
import actor
import pygame
import weapon

KEYS_DICT = {pygame.K_UP:'up',pygame.K_DOWN:'down',pygame.K_LEFT:'left',pygame.K_RIGHT:'right'}

def spawn_ammo(timer):
    if timer == random.randint(0,100):
        print ('ammo spawn')
        weapon.Ammo(random.randint(100,800),random.randint(100,700))
        weapon.FirstAidBox(random.randint(20,500),random.randint(20,500))

def spawn_enemies(n):
    for i in range(n):
        actor.EActor(random.randint(100,500),random.randint(100,500),random.choice(actor.ENEMY_LIST),random.choice(weapon.WEAPON_LIST))

def has_beaten_level():
    """Switches to a new scene once you have beaten the level.
    Beating the level consists of killing all enemies"""
    if len(actor.enemy_sprites) <=0:
        return True