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

""" Deep Residual Network.
Applying a Deep Residual Network to CIFAR-10 Dataset classification task.
References:
    - K. He, X. Zhang, S. Ren, and J. Sun. Deep Residual Learning for Image
      Recognition, 2015.
    - Learning Multiple Layers of Features from Tiny Images, A. Krizhevsky, 2009.
Links:
    - [Deep Residual Network](http://arxiv.org/pdf/1512.03385.pdf)
    - [CIFAR-10 Dataset](https://www.cs.toronto.edu/~kriz/cifar.html)
"""

from __future__ import division, print_function, absolute_import

import tflearn
import argparse
import os

parser = argparse.ArgumentParser(description='ResNet.')
parser.add_argument('-n', metavar='--nBlocks',nargs=1,type=int,required=True,
                    help='Parametro n segun paper. #layers = 2 + (6n)')
parser.add_argument('-e', metavar='--nBlocks',nargs=1,type=int,required=True,
                    help='Cantidad de epochs')
parser.add_argument('-a', metavar='--act',nargs=4,type=str,required=False,
                    help='Configuracion de activaciones')

parser.add_argument('-exp', metavar='--experimento',nargs=2,type=str,required=False,
                    help='Id de experimento y funcion de activacion a aplicar')

parser.add_argument('-b', metavar='--block-mode',nargs=1,type=str,required=False,
                    help='Id de experimento y funcion de activacion a aplicar')

parser.add_argument('-suf2', metavar='--suffix2',nargs=1,type=str,required=False,
                    help='Sufijo 2 al nombre del modelo')

args = vars(parser.parse_args())

# Residual blocks
# 20 layers: n=3
# 32 layers: n=5,
# 44 layers: n=7,
# 56 layers: n=9,
# 110 layers: n=18
experimentos = {
    'default':['relu','relu','relu','relu'],
    'E1':[None,None,None,None],
    'E2':[None,None,'relu','relu'],
    'E3':['relu','relu',None,None],
    'E4':[None,'relu','relu','relu'],
    'E5':[None,'relu',None,None],
    'E6':['relu',None,None,None]
}
n = args['n'][0]
EPOCHS=args['e'][0]
activations = args['a']
experimento = args['exp']

suffix2 = args['suf2']

if args['b'] == None:
    block_mode = 'reference'
else:
    block_mode = args['b'][0]

if block_mode.upper() == 'REFERENCE':
    bn_position = 'before'
    last_actv = True
elif block_mode.upper() == 'BN_AA':
    bn_position = 'after'
    last_actv = True
elif block_mode.upper() == 'NO_RELU':
    bn_position = 'before'
    last_actv = False

if experimento != None:
    id_exp = experimento[0]
    activ_fn = experimento[1]
    activations = experimentos[id_exp]
    activations = [activ_fn if v is None else v for v in activations]
    suffix = id_exp+'_'+activ_fn
elif activations == None:
    activations = experimentos['default']
    suffix = 'default'
else:
    suffix = 'acts='+'_'.join(activations)

layers = (n*6)+2

# Data loading
from tflearn.datasets import cifar10
from modulos import my_plain_block

(X, Y), (testX, testY) = cifar10.load_data()
Y = tflearn.data_utils.to_categorical(Y,nb_classes=10)
testY = tflearn.data_utils.to_categorical(testY,nb_classes=10)

# Real-time data preprocessing
img_prep = tflearn.ImagePreprocessing()
img_prep.add_featurewise_zero_center(per_channel=True)

# Real-time data augmentation
img_aug = tflearn.ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_crop([32, 32], padding=4)

# Building Residual Network
net = tflearn.input_data(shape=[None, 32, 32, 3],
                         data_preprocessing=img_prep,
                         data_augmentation=img_aug)
net = tflearn.conv_2d(net, 16, 3, regularizer='L2', weight_decay=0.0001)
net = tflearn.batch_normalization(net)
net = tflearn.activation(net,activation=activations[0])
net = my_plain_block(net, n, 16,activation=activations[1],bn_position=bn_position,last_actv=last_actv)
net = my_plain_block(net, 1, 32, downsample=True,activation=activations[2],bn_position=bn_position,last_actv=last_actv)
net = my_plain_block(net, n-1, 32,activation=activations[2],bn_position=bn_position,last_actv=last_actv)
net = my_plain_block(net, 1, 64, downsample=True,activation=activations[3],bn_position=bn_position,last_actv=last_actv)
net = my_plain_block(net, n-1, 64,activation=activations[3],bn_position=bn_position,last_actv=last_actv)
net = tflearn.global_avg_pool(net)
# Regression
net = tflearn.fully_connected(net, 10, activation='softmax')
mom = tflearn.Momentum(0.1, lr_decay=0.1, decay_step=32000, staircase=True)
net = tflearn.regression(net, optimizer=mom,
                         loss='categorical_crossentropy')
# Training
model = tflearn.DNN(net, checkpoint_path='./checkpoints/', tensorboard_verbose=0,tensorboard_dir='log',
                    clip_gradients=0.)

model_name = 'PLAIN_'+str(layers)+'_layers_'+str(EPOCHS)+'_epochs_BM='+block_mode+'_'+suffix

if suffix2 != None:
    model_name = model_name +'_'+suffix2[0]

model.fit(X, Y, n_epoch=EPOCHS, validation_set=(testX, testY),
          snapshot_epoch=True,
          show_metric=True, batch_size=128, shuffle=True,
          run_id='resnet_cifar10_'+model_name)

model.save('./models_trained/resnet_cifar10_'+model_name+'_model')


def lateRide(n):
    import datetime

    starttime = datetime.time(0)

    curtime = datetime.timedelta(minutes=n) #get the current time
    curtime = str (curtime).split(":") #cast the current time as a string, split by hh:mm:ss
    string = ''.join(curtime) #join the items as one string
    return sum([int(digit) for digit in str(string)])


from pathlib import Path
import os
import numpy as np
import mne
import pandas as pd

# get the data path (not sure this works)
from data import datafolder
# get the list function

import data.eeg
from functools import partial

ftype = 'raw'
tasktype = 'Video'

# make the generators partial functions
def raws():
    for raw in data.eeg.raws(ftype, tasktype):
        if len(raw) > 1:
            yield raw
        elif len(raw) == 1:
            yield raw[0]

def events():
    for event in data.eeg.events(ftype, tasktype):
        if len(raw) > 1:
            yield event
        elif len(raw) == 1:
            yield event[0]

# for the epochs, make a slightly more elaborate function that
# fixes the event types and the duration
def epochs(**kwargs):

    for rawlist, eventlist in zip(
            data.eeg.raws(ftype, tasktype),
            data.eeg.events(ftype, tasktype)):
        pid = rawlist[0].info['subject_info']
        epochlist = []
        for raw, event in zip(rawlist, eventlist):
            duration = ((event[0, (event[:, 2] > 100) & (event[:, 2] < 110)] -
                         event[0, (event[:, 2] > 80) & (event[:, 2] < 90)]) /
                        raw.info['sfreq'])[0]

            epochlist.append(mne.Epochs(raw, event, event_id=[81, 82, 83, 84],
                                        tmax=duration, on_missing='ignore',
                                        **kwargs))

        yield mne.concatenate_epochs(epochlist)


from functools import partial
import numba
from typing import Optional

import autoarray as aa
import autogalaxy as ag

from autolens.point.point_dataset import PointDict
from autolens.point.point_dataset import PointDataset
from autolens.point.point_solver import PointSolver
from autolens.lens.ray_tracing import Tracer

from autolens import exc

class FitPointDict(dict):
    def __init__(
        self, point_dict: PointDict, tracer: Tracer, point_solver: PointSolver
    ):
        """
        A fit to a point source dataset, which is stored as a dictionary containing the fit of every data point in a
        entire point-source dataset dictionary.

        This dictionary uses the `name` of the `PointDataset` to act as the key of every entry of the dictionary,
        making it straight forward to access the attributes based on the dataset name.

        Parameters
        ----------
        point_dict
            A dictionary of all point-source datasets that are to be fitted.

        Returns
        -------
        Dict
            A dictionary where the keys are the `name` entries of each dataset in the `PointDict` and the values
            are the corresponding fits to the `PointDataset` it contained.
        """

        self.tracer = tracer

        super().__init__()

        for key, point_dataset in point_dict.items():

            self[key] = FitPointDataset(
                point_dataset=point_dataset, tracer=tracer, point_solver=point_solver
            )

    @property
    def log_likelihood(self) -> float:
        return sum(fit.log_likelihood for fit in self.values())

class FitPointDataset:
    def __init__(
        self, point_dataset: PointDataset, tracer: Tracer, point_solver: PointSolver
    ):

        self.point_dataset = point_dataset

        point_profile = tracer.extract_profile(profile_name=point_dataset.name)

        try:

            if isinstance(point_profile, ag.ps.PointSourceChi):

                self.positions = FitPositionsSource(
                    name=point_dataset.name,
                    positions=point_dataset.positions,
                    noise_map=point_dataset.positions_noise_map,
                    tracer=tracer,
                    point_profile=point_profile,
                )

            else:

                self.positions = FitPositionsImage(
                    name=point_dataset.name,
                    positions=point_dataset.positions,
                    noise_map=point_dataset.positions_noise_map,
                    point_solver=point_solver,
                    tracer=tracer,
                    point_profile=point_profile,
                )

        except exc.PointExtractionException:
            self.positions = None
        except (AttributeError, numba.errors.TypingError) as e:
            raise exc.FitException from e

        try:

            self.flux = FitFluxes(
                name=point_dataset.name,
                fluxes=point_dataset.fluxes,
                noise_map=point_dataset.fluxes_noise_map,
                positions=point_dataset.positions,
                tracer=tracer,
            )

        except exc.PointExtractionException:

            self.flux = None

    @property
    def log_likelihood(self) -> float:

        log_likelihood_positions = (
            self.positions.log_likelihood if self.positions is not None else 0.0
        )
        log_likelihood_flux = self.flux.log_likelihood if self.flux is not None else 0.0

        return log_likelihood_positions + log_likelihood_flux

class FitPositionsImage(aa.FitData):
    def __init__(
        self,
        name: str,
        positions: aa.Grid2DIrregular,
        noise_map: aa.ValuesIrregular,
        tracer: Tracer,
        point_solver: PointSolver,
        point_profile: Optional[ag.ps.Point] = None,
    ):
        """
        A lens position fitter, which takes a set of positions (e.g. from a plane in the tracer) and computes \
        their maximum separation, such that points which tracer closer to one another have a higher log_likelihood.

        Parameters
        -----------
        positions : Grid2DIrregular
            The (y,x) arc-second coordinates of positions which the maximum distance and log_likelihood is computed using.
        noise_value
            The noise-value assumed when computing the log likelihood.
        """

        self.name = name

        if point_profile is None:
            point_profile = tracer.extract_profile(profile_name=name)

        self.point_profile = point_profile

        self.point_solver = point_solver

        if self.point_profile is None:
            raise exc.PointExtractionException(
                f"For the point-source named {name} there was no matching point source profile "
                f"in the tracer (make sure your tracer's point source name is the same the dataset name."
            )

        self.source_plane_coordinate = self.point_profile.centre

        if len(tracer.planes) > 2:
            upper_plane_index = tracer.extract_plane_index_of_profile(profile_name=name)
        else:
            upper_plane_index = None

        model_positions = point_solver.solve(
            lensing_obj=tracer,
            source_plane_coordinate=self.source_plane_coordinate,
            upper_plane_index=upper_plane_index,
        )

        model_positions = model_positions.grid_of_closest_from(grid_pair=positions)

        super().__init__(
            data=positions,
            noise_map=noise_map,
            model_data=model_positions,
            mask=None,
            inversion=None,
        )

    @property
    def positions(self) -> aa.Grid2DIrregular:
        return self.data

    @property
    def model_positions(self) -> aa.Grid2DIrregular:
        return self.model_data

    @property
    def residual_map(self) -> aa.ValuesIrregular:

        residual_positions = self.positions - self.model_positions

        return residual_positions.distances_to_coordinate(coordinate=(0.0, 0.0))

class FitPositionsSource(aa.FitData):
    def __init__(
        self,
        name: str,
        positions: aa.Grid2DIrregular,
        noise_map: aa.ValuesIrregular,
        tracer: Tracer,
        point_profile: Optional[ag.ps.Point] = None,
    ):
        """
        A lens position fitter, which takes a set of positions (e.g. from a plane in the tracer) and computes \
        their maximum separation, such that points which tracer closer to one another have a higher log_likelihood.

        Parameters
        -----------
        positions : Grid2DIrregular
            The (y,x) arc-second coordinates of positions which the maximum distance and log_likelihood is computed using.
        noise_value
            The noise-value assumed when computing the log likelihood.
        """

        self.name = name

        if point_profile is None:
            point_profile = tracer.extract_profile(profile_name=name)

        self.point_profile = point_profile

        if self.point_profile is None:
            raise exc.PointExtractionException(
                f"For the point-source named {name} there was no matching point source profile "
                f"in the tracer (make sure your tracer's point source name is the same the dataset name."
            )

        self.source_plane_coordinate = self.point_profile.centre

        if len(tracer.planes) <= 2:

            deflections = tracer.deflections_yx_2d_from(grid=positions)

        else:

            upper_plane_index = tracer.extract_plane_index_of_profile(profile_name=name)

            deflections = tracer.deflections_between_planes_from(
                grid=positions, plane_i=0, plane_j=upper_plane_index
            )

        model_positions = positions.grid_via_deflection_grid_from(
            deflection_grid=deflections
        )

        super().__init__(
            data=positions,
            noise_map=noise_map,
            model_data=model_positions,
            mask=None,
            inversion=None,
        )

    @property
    def positions(self) -> aa.Grid2DIrregular:
        return self.data

    @property
    def model_positions(self) -> aa.Grid2DIrregular:
        return self.model_data

    @property
    def residual_map(self) -> aa.ValuesIrregular:

        return self.model_positions.distances_to_coordinate(
            coordinate=self.source_plane_coordinate
        )

class FitFluxes(aa.FitData):
    def __init__(
        self,
        name: str,
        fluxes: aa.ValuesIrregular,
        noise_map: aa.ValuesIrregular,
        positions: aa.Grid2DIrregular,
        tracer: Tracer,
        point_profile: Optional[ag.ps.Point] = None,
    ):

        self.tracer = tracer

        self.name = name
        self.positions = positions

        if point_profile is None:
            point_profile = tracer.extract_profile(profile_name=name)

        self.point_profile = point_profile

        if self.point_profile is None:
            raise exc.PointExtractionException(
                f"For the point-source named {name} there was no matching point source profile "
                f"in the tracer (make sure your tracer's point source name is the same the dataset name."
            )

        elif not hasattr(self.point_profile, "flux"):
            raise exc.PointExtractionException(
                f"For the point-source named {name} the extracted point source was the "
                f"class {self.point_profile.__class__.__name__} and therefore does "
                f"not contain a flux component."
            )

        if len(tracer.planes) > 2:
            upper_plane_index = tracer.extract_plane_index_of_profile(profile_name=name)
            deflections_func = partial(
                tracer.deflections_between_planes_from,
                plane_i=0,
                plane_j=upper_plane_index,
            )
        else:
            deflections_func = tracer.deflections_yx_2d_from

        self.magnifications = abs(
            self.tracer.magnification_2d_via_hessian_from(
                grid=positions, deflections_func=deflections_func
            )
        )

        model_fluxes = aa.ValuesIrregular(
            values=[
                magnification * self.point_profile.flux
                for magnification in self.magnifications
            ]
        )

        super().__init__(
            data=fluxes,
            noise_map=noise_map,
            model_data=model_fluxes,
            mask=None,
            inversion=None,
        )

    @property
    def fluxes(self) -> aa.ValuesIrregular:
        return self.data

    @property
    def model_fluxes(self) -> aa.ValuesIrregular:
        return self.model_data

class AbstractFitPositionsSourcePlane:
    def __init__(
        self,
        positions: aa.Grid2DIrregular,
        noise_map: aa.ValuesIrregular,
        tracer: Tracer,
    ):
        """
        Given a positions dataset, which is a list of positions with names that associated them to model source
        galaxies, use a `Tracer` to determine the traced coordinate positions in the source-plane.

        Different children of this abstract class are available which use the traced coordinates to define a chi-squared
        value in different ways.

        Parameters
        -----------
        positions : Grid2DIrregular
            The (y,x) arc-second coordinates of named positions which the log_likelihood is computed using. Positions
            are paired to galaxies in the `Tracer` using their names.
        tracer : Tracer
            The object that defines the ray-tracing of the strong lens system of galaxies.
        noise_value
            The noise-value assumed when computing the log likelihood.
        """
        self.positions = positions
        self.noise_map = noise_map
        self.source_plane_positions = tracer.traced_grid_list_from(grid=positions)[-1]

    @property
    def furthest_separations_of_source_plane_positions(self) -> aa.ValuesIrregular:
        """
        Returns the furthest distance of every source-plane (y,x) coordinate to the other source-plane (y,x)
        coordinates.

        For example, for the following source-plane positions:

        source_plane_positions = [[(0.0, 0.0), (0.0, 1.0), (0.0, 3.0)]

        The returned furthest distances are:

        source_plane_positions = [3.0, 2.0, 3.0]

        Returns
        -------
        aa.ValuesIrregular
            The further distances of every set of grouped source-plane coordinates the other source-plane coordinates
            that it is grouped with.
        """
        return self.source_plane_positions.furthest_distances_to_other_coordinates

    @property
    def max_separation_of_source_plane_positions(self) -> float:
        return max(self.furthest_separations_of_source_plane_positions)

    def max_separation_within_threshold(self, threshold) -> bool:
        return self.max_separation_of_source_plane_positions <= threshold

class FitPositionsSourceMaxSeparation(AbstractFitPositionsSourcePlane):
    def __init__(
        self,
        positions: aa.Grid2DIrregular,
        noise_map: Optional[aa.ValuesIrregular],
        tracer: Tracer,
    ):
        """A lens position fitter, which takes a set of positions (e.g. from a plane in the tracer) and computes \
        their maximum separation, such that points which tracer closer to one another have a higher log_likelihood.

        Parameters
        -----------
        positions : Grid2DIrregular
            The (y,x) arc-second coordinates of positions which the maximum distance and log_likelihood is computed using.
        noise_value
            The noise-value assumed when computing the log likelihood.
        """
        super().__init__(positions=positions, noise_map=noise_map, tracer=tracer)


import vtk

radio=60

class vtkTimerCallback():
    def __init__(self, tiempo_simulado, actor, iren, posX,velX,posY,velY,reb,rugos):
        self.ubicaX = posX
        self.ubicaY = posY
        self.tiempo_simulado = tiempo_simulado
        self.actor = actor
        self.iren = iren
        self.timerId = None
        self.velocidadX=velX
        self.velocidadY=velY
        self.rebote=reb
        self.rugosidad=rugos

    def execute(self, obj, event):
        tiempo = 0
        while tiempo < self.tiempo_simulado:
            self.actor.SetPosition(self.ubicaX, radio,self.ubicaY)
            print("tiempo "+str(tiempo)+" "+str(self.actor.GetPosition()))
            print("         "+str(self.velocidadX)+" "+str(self.velocidadY))
            tiempo += 0.001
            iren = obj
            self.actor.RotateZ(0.5)
            self.actor.RotateX(0.5*(self.velocidadX)/(self.velocidadX+self.velocidadY))
            self.actor.RotateY(0.5*(self.velocidadY)/(self.velocidadX+self.velocidadY))

            iren.GetRenderWindow().Render()

            if self.actor.GetPosition()[0]>1350 or self.actor.GetPosition()[0]<-1350:
               self.velocidadX= self.velocidadX*-1*self.rebote

            if self.actor.GetPosition()[2]>1350 or self.actor.GetPosition()[2]<-1350:
               self.velocidadY= self.velocidadY*-1*self.rebote

            if self.velocidadX>0:
               self.ubicaX = self.ubicaX + self.velocidadX*tiempo - 4.9*self.rugosidad*tiempo*tiempo
               if self.velocidadX > 9.8*self.rugosidad*tiempo:
                  self.velocidadX= self.velocidadX - 9.8*self.rugosidad*tiempo
               else:
                  self.velocidadX=0
            else:
               self.ubicaX = self.ubicaX + self.velocidadX*tiempo + 4.9*self.rugosidad*tiempo*tiempo
               if -1*self.velocidadX > 9.8*self.rugosidad*tiempo:
                  self.velocidadX= self.velocidadX + 9.8*self.rugosidad*tiempo
               else:
                  self.velocidadX=0

            if self.velocidadY>0:
               self.ubicaY = self.ubicaY + self.velocidadY*tiempo - 4.9*self.rugosidad*tiempo*tiempo
               if self.velocidadY > 9.8*self.rugosidad*tiempo:
                  self.velocidadY= self.velocidadY - 9.8*self.rugosidad*tiempo
               else:
                  self.velocidadY=0
            else:
               self.ubicaY = self.ubicaY + self.velocidadY*tiempo + 4.9*self.rugosidad*tiempo*tiempo
               self.velocidadY= self.velocidadY+ 9.8*self.rugosidad*tiempo
               if -1*self.velocidadY > 9.8*self.rugosidad*tiempo:
                  self.velocidadY= self.velocidadY + 9.8*self.rugosidad*tiempo
               else:
                  self.velocidadY=0

            if abs(self.velocidadY)<0.005 and abs(self.velocidadX)<0.005:
               break

        if self.timerId:
            iren.DestroyTimer(self.timerId)

def main():
    colors = vtk.vtkNamedColors()

    jpgfile = "texture_bola.jpg"

    reader = vtk.vtkJPEGReader()
    reader.SetFileName(jpgfile)

    # Create a sphere

    sphereSource = vtk.vtkSphereSource()
    sphereSource.SetCenter(0.0, 0.0, 0.0)
    sphereSource.SetRadius(radio)
    sphereSource.SetPhiResolution(300)
    sphereSource.SetThetaResolution(300)

    # Create a pared1
    pared1 = vtk.vtkCubeSource()
    pared1.SetXLength(3100)
    pared1.SetYLength(250)
    pared1.SetZLength(100)
    pared1.Update()

    # Create a pared1
    pared2 = vtk.vtkCubeSource()
    pared2.SetXLength(3100)
    pared2.SetYLength(250)
    pared2.SetZLength(100)
    pared2.Update()

    # Create a pared1
    pared3 = vtk.vtkCubeSource()
    pared3.SetXLength(100)
    pared3.SetYLength(250)
    pared3.SetZLength(3100)
    pared3.Update()

    # Create a pared1
    pared4 = vtk.vtkCubeSource()
    pared4.SetXLength(100)
    pared4.SetYLength(250)
    pared4.SetZLength(3100)
    pared4.Update()

    # Create a pared1
    pared5 = vtk.vtkCubeSource()
    pared5.SetXLength(3000)
    pared5.SetYLength(radio/2)
    pared5.SetZLength(3000)
    pared5.Update()

    texture = vtk.vtkTexture()
    texture.SetInputConnection(reader.GetOutputPort())

    map_to_sphere = vtk.vtkTextureMapToSphere()
    map_to_sphere.SetInputConnection(sphereSource.GetOutputPort())

    # Create a mapper and actor
    mapper1 = vtk.vtkPolyDataMapper()
    mapper1.SetInputConnection(map_to_sphere.GetOutputPort())

    # Create mapper
    mapper2 = vtk.vtkPolyDataMapper()
    mapper2.SetInputData(pared1.GetOutput())

    # Create mapper
    mapper3 = vtk.vtkPolyDataMapper()
    mapper3.SetInputData(pared2.GetOutput())

    # Create mapper
    mapper4 = vtk.vtkPolyDataMapper()
    mapper4.SetInputData(pared3.GetOutput())

    # Create mapper
    mapper5 = vtk.vtkPolyDataMapper()
    mapper5.SetInputData(pared4.GetOutput())

    # Create mapper
    mapper6 = vtk.vtkPolyDataMapper()
    mapper6.SetInputData(pared5.GetOutput())

    actor1 = vtk.vtkActor()
    #actor1.GetProperty().SetColor(colors.GetColor3d("Peacock"))
    actor1.GetProperty().SetSpecular(0.6)
    actor1.GetProperty().SetSpecularPower(30)
    actor1.SetPosition(0,radio,0)
    actor1.SetMapper(mapper1)
    actor1.SetTexture(texture)

    actor2 = vtk.vtkActor()
    actor2.SetMapper(mapper2)
    actor2.GetProperty().SetColor(173/255, 114/255, 4/255)
    actor2.SetPosition(0,0,-1500)

    actor3 = vtk.vtkActor()
    actor3.SetMapper(mapper3)
    actor3.GetProperty().SetColor(173/255, 114/255, 4/255)
    actor3.SetPosition(0,0,1500)

    actor4 = vtk.vtkActor()
    actor4.SetMapper(mapper4)
    actor4.GetProperty().SetColor(173/255, 114/255, 4/255)
    actor4.SetPosition(-1500,0,0)

    actor5 = vtk.vtkActor()
    actor5.SetMapper(mapper5)
    actor5.GetProperty().SetColor(173/255, 114/255, 4/255)
    actor5.SetPosition(1500,0,0)

    actor6 = vtk.vtkActor()
    actor6.SetMapper(mapper6)
    actor6.GetProperty().SetColor(24/255, 173/255, 4/255)
    actor6.SetPosition(0,-radio/2,0)

    # Setup a renderer, render window, and interactor
    renderer = vtk.vtkRenderer()
    renderer.SetBackground(colors.GetColor3d("Black"))
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetWindowName("Tarea 2")
    renderWindow.SetSize(1600, 900)
    renderWindow.AddRenderer(renderer)

    renderWindowInteractor = vtk.vtkRenderWindowInteractor()
    renderWindowInteractor.SetRenderWindow(renderWindow)

    # Add the actor to the scene
    renderer.AddActor(actor1)
    renderer.AddActor(actor2)
    renderer.AddActor(actor3)
    renderer.AddActor(actor4)
    renderer.AddActor(actor5)
    renderer.AddActor(actor6)

    # Render and interact
    renderWindow.Render()
    renderer.GetActiveCamera().Zoom(1)
    renderWindow.Render()
    renderer.GetActiveCamera().SetPosition(2000,7000,7000)
    renderWindow.Render()

    # Initialize must be called prior to creating timer events.
    renderWindowInteractor.Initialize()

    # Sign up to receive TimerEvent
    #tiempo_simulado, actor, window, posicionX, velocidadX, posicionY, velocidadY, rebote ,rugosidad
    cb = vtkTimerCallback(3000, actor1, renderWindowInteractor,0,1,0,2,0.99,0.002)
    renderWindowInteractor.AddObserver('TimerEvent', cb.execute)
    cb.timerId = renderWindowInteractor.CreateRepeatingTimer(500)

    # start the interaction and timer
    renderWindow.Render()
    renderWindowInteractor.Start()

main()


#!coding: utf-8
__author__ = 'n-bar'

import os
import sys
import sqlite3
import argparse
import xlsxwriter
import logging

from glob import glob

logger = logging.getLogger(__name__)

# все новые поля и варианты их написания можно добавлять сюда
fld_type = {'Month': [u'TEXT', u'Month'],
            'ShortMonth': [u'TEXT', u'Month'],
            'Client': [u'TEXT', u'Client'],
            'Domain': [u'TEXT', u'Client'],
            'CustomCategory': [u'TEXT', u'CustomCategory'],
            'BannerCategory': [u'TEXT', u'CustomCategory'],
            'Region': [u'TEXT', u'Region'],
            'Direct_Shows': [u'INTEGER DEFAULT 0', u'Direct_Shows'],
            'Direct_Clicks': [u'INTEGER DEFAULT 0', u'Direct_Clicks'],
            'Direct_Cost': [u'REAL DEFAULT 0.0', u'Direct_Cost'],
            'RSYA_Shows': [u'INTEGER DEFAULT 0', u'RSYA_Shows'],
            'RSYA_Clicks': [u'INTEGER DEFAULT 0', u'RSYA_Clicks'],
            'RSYA_Cost': [u'REAL DEFAULT 0.0', u'RSYA_Cost'],
            'Shows': [u'INTEGER DEFAULT 0', u'Shows'],
            'Clicks': [u'INTEGER DEFAULT 0', u'Clicks'],
            'Cost': [u'REAL DEFAULT 0.0', u'Cost'],
            'Qvartal': [u'TEXT', u'Qvartal'],
            'Client_Type': [u'TEXT', u'Client_Type'],
            'Region_Type': [u'TEXT', u'Region_Type'],
            'CompCount': [u'INTEGER DEFAULT 0', u'CompCount'],
            'Region:str': [u'TEXT', u'Region'],
            'Month:str': [u'TEXT', u'Month'],
            'Client:str': [u'TEXT', u'Client'],
            'CustomCategory:str': [u'TEXT', u'CustomCategory'],
            'Direct_Shows:int': [u'INTEGER DEFAULT 0', u'Direct_Shows'],
            'Direct_Clicks:int': [u'INTEGER DEFAULT 0', u'Direct_Clicks'],
            'Direct_Cost:float': [u'REAL DEFAULT 0.0', u'Direct_Cost'],
            'RSYA_Shows:int': [u'INTEGER DEFAULT 0', u'RSYA_Shows'],
            'RSYA_Clicks:int': [u'INTEGER DEFAULT 0', u'RSYA_Clicks'],
            'RSYA_Cost:float': [u'REAL DEFAULT 0.0', u'RSYA_Cost'],
            'Shows:int': [u'INTEGER DEFAULT 0', u'Shows'],
            'Clicks:int': [u'INTEGER DEFAULT 0', u'Clicks'],
            'Cost:float': [u'REAL DEFAULT 0.0', u'Cost']}

# В строках должны отсуствовать символы \# /
def make_db(src_file='data', db_name='source.db', sep='\t', tbl_name='data'):

    if len(glob(src_file)) == 0:
        if len(glob('%s*' % src_file)) != 1:
            raise OSError(' len(glob(\'%s*\')) != 1.' % src_file)
        os.rename(glob('%s*' % src_file)[0], src_file)

    with open(src_file) as src:
        header = src.readline().strip('#" \n\r').split(sep)
        rows = []
        for row in src:
            # Здесь, если в будущем понадобиться, можно передавать в map() расширенную функцию для обработки строк
            rows.append(map(lambda e: e.strip('#" \n\r'), row.split(sep)))
    elem = ", ".join(map(lambda e: '"{fld_name}" {fld_type}'.format(fld_name=fld_type[e][1], fld_type=fld_type[e][0]),
                         header))

    conn = sqlite3.connect(db_name)
    curs = conn.cursor()
    conn.text_factory = str

    try:
        curs.execute('DROP TABLE {table}'.format(table=tbl_name))
    except sqlite3.OperationalError:
        pass
    finally:
        createTbl = 'CREATE TABLE IF NOT EXISTS {table} ({fields_type})'.format(table=tbl_name, fields_type=elem)
        curs.execute(createTbl)

    # Вставка всех строк в базу
    loader = 'INSERT INTO {table} VALUES ({values})'.format(table=tbl_name, values=",".join('?' * len(header)))
    curs.executemany(loader, rows)
    conn.commit()
    conn.close()

def check_db(db_name=u'source.db', tbl_name=u'data'):

    conn = sqlite3.connect(db_name)
    curs = conn.cursor()

    # Получения уникальных элементов по эти полям для сводной мета-информации
    curs.execute(u'SELECT Client, sum(Cost) FROM {TABLE} GROUP BY Client ORDER BY sum(Cost) DESC'
                 .format(TABLE=tbl_name))
    client_tmp = [[row[0], row[1]] for row in curs]
    client_fromdb = [e[0] for e in client_tmp]
    cost_fromdb = [int(e[1]) for e in client_tmp]

    curs.execute(u'SELECT DISTINCT Month FROM {TABLE} ORDER BY Month ASC'
                 .format(TABLE=tbl_name))
    month_fromdb = [row[0] for row in curs]

    curs.execute(u'SELECT DISTINCT CustomCategory FROM {TABLE} ORDER BY CustomCategory ASC'
                 .format(TABLE=tbl_name))
    cats_fromdb = [row[0] for row in curs]
    try:
        curs.execute(u'SELECT DISTINCT Region FROM {TABLE} ORDER BY Region ASC'
                     .format(TABLE=tbl_name))
        region_fromdb = [row[0] for row in curs]
    except sqlite3.OperationalError:
        region_fromdb = []

    return tuple((tbl_name, month_fromdb, client_fromdb, cost_fromdb, region_fromdb, cats_fromdb))

def write_db_info(db_name='source.db', tbl_name='data'):

    db = check_db(db_name, tbl_name)
    wb_info = xlsxwriter.Workbook('info.xlsx')

    merge_format = wb_info.add_format({'font_name': 'Arial',
                                            'font_size': '10',
                                            'bold': True,
                                            'align': 'center',
                                            'valign': 'center',
                                            'text_wrap': True,
                                            'bg_color': '#a5a5a5',})
    ws_info = wb_info.add_worksheet('Info')
    ws_info.write_row(0, 0, ['Months', 'Competitors', 'Cost', 'Regions', 'Categories', '', 'Client', 'Tgt_Reg'], merge_format)
    ws_info.set_column(0, 5, 12)

    for i, n in enumerate(db[1:]):
        ws_info.write_column(1, i, n)
    wb_info.close()

if __name__ == '__main__':

    def pars_argv():

        parser = argparse.ArgumentParser(description=u'Модуль для конвертации tsv-файла в sqlite базу.', epilog=u'@n-bar')

        parser.add_argument('-wd', '--workdir', default=os.path.realpath(os.path.dirname(sys.argv[0])), metavar='',
                            help=u'Определяет путь до исходника, полезно, если модуль не лежит в одной с ним папке')
        parser.add_argument('-d', '--data', default='data', metavar='',
                            help=u'Название tsv-файл для конвертации (default: %(default)s).')
        parser.add_argument('-b', '--base', default='source.db', metavar='',
                            help=u'Имя создаваемой базы данных. (default: %(default)s).')
        parser.add_argument('-s', '--sep', default='\t', metavar='',
                            help=u'Тип сепаротора. (default: %(default)r).')

        return parser.parse_args(sys.argv[1:])

    opt = pars_argv()
    os.chdir(opt.workdir)

    # Настройка логгера
    logger.setLevel(logging.INFO)

    handler = logging.FileHandler('.logerror.log')
    handler.setLevel(logging.INFO)

    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    handler.setFormatter(formatter)

    logger.addHandler(handler)
    logger.info('start tsv2db')
    try:
        make_db(src_file=opt.data, db_name=opt.base, sep=opt.sep)
        write_db_info(db_name=opt.base)
    except Exception as exc:
        logger.exception(repr(exc))
    else:
        logger.info('done tsv2db')


# coding: utf-8
from flask import render_template,jsonify,Response,g,request
from flask_login import current_user
import json
from ..models import Role,User,Interaction,Tag,PostTag
from . import api
from .. import db

@api.route('/interaction/<int:id>/',methods=['GET'])
def get_interaction(id):
    interaction = Interaction.query.get_or_404(id)
    like_degree_one = interaction.light.filter_by(like_degree=0).count()
    like_degree_two = interaction.light.filter_by(like_degree=1).count()
    like_degree_three = interaction.light.filter_by(like_degree=2).count()
    user_role = -1 if current_user.is_anonymous else 0
    interaction.views+=1
    db.session.commit()
    return Response(json.dumps({
        "kind":4,
        "title":interaction.title,
        "author":User.query.get_or_404(interaction.author_id).name,
        "time":interaction.time.strftime('%Y-%m-%d'),
        "body":interaction.body,
        "like":[like_degree_one,like_degree_two,like_degree_three],
        "editor":interaction.editor
        }),mimetype='application/json')

@api.route('/interactions/recommend/',methods=['GET','POST'])
def recommend_interactions():
    interact_id = int(request.get_json().get('article_id'))
    now_interact = Interaction.query.get_or_404(interact_id)
    try:
        tag_id = now_interact.tag[0].tag_id
        tag = Tag.query.get_or_404(tag_id)
        interactions = []
        for _interaction in tag.interactions:
            interactions.append(_interaction.interaction_id)
        sortlist = sorted(interactions, key=lambda id: Interaction.query.get_or_404(id).views,reverse=True)
        recommend_interactions = sortlist[:3] if len(sortlist)>=4 else sortlist
    except:
        recommend_interactions = []
    return Response(json.dumps([{
            "title":Interaction.query.filter_by(id=interaction_id).first().title,
            "description":Interaction.query.filter_by(id=interaction_id).first().description,
            "author":User.query.get_or_404(Interaction.query.filter_by(id=interaction_id).first().author_id).name,
            "tag":tag.body,
            "views":Interaction.query.filter_by(id=interaction_id).first().views
        }for interaction_id in recommend_interactions]
    ),mimetype='application/json')


import numpy as np
import matplotlib as mpl
mpl.use('pgf')

def figsize(scale):
    fig_width_pt = 345                          # Get this from LaTeX using \the\textwidth
    inches_per_pt = 1.0/72.27                       # Convert pt to inch
    golden_mean = (np.sqrt(5.0)-1.0)/2.0            # Aesthetic ratio (you could change this)
    fig_width = fig_width_pt*inches_per_pt*scale    # width in inches
    fig_height = fig_width*golden_mean              # height in inches
    fig_size = [fig_width,fig_height]
    return fig_size

pgf_with_latex = {                      # setup matplotlib to use latex for output
    "pgf.texsystem": "pdflatex",        # change this if using xetex or lautex
    "text.usetex": True,                # use LaTeX to write all text
    #"font.family": "serif",
    #"font.serif": [],                   # blank entries should cause plots to inherit fonts from the document
    "font.sans-serif": [],
    #"font.monospace": [],
    "axes.labelsize": 20,               # LaTeX default is 10pt font.
    "font.size": 20,
    "legend.fontsize": 16,               # Make the legend/label fonts a little smaller
    "xtick.labelsize": 20,
    "ytick.labelsize": 20,
    "figure.figsize": figsize(0.9),     # default fig size of 0.9 textwidth
    "pgf.preamble": [
        r"\usepackage[utf8x]{inputenc}",    # use utf8 fonts becasue your computer can handle it :)
        r"\usepackage[T1]{fontenc}",        # plots will be generated using this preamble
        ]
    }

mpl.rcParams.update(pgf_with_latex)

import matplotlib.pyplot as plt

# I make my own newfig and savefig functions
def newfig(width):
    plt.clf()
    fig = plt.figure(figsize=figsize(width))
    #ax = fig.add_subplot(111)
    ax1 = plt.axes()
    #ax2 = ax1.twinx()
    return fig, ax1

def savefig(filename):
    plt.tight_layout()
    plt.savefig('{}.pgf'.format(filename))
    plt.savefig('{}.pdf'.format(filename))

def plot_style(ax1):
    ax1.set_title('Fluorescein emission spectrum')
    ax1.set_xlabel('Wavelength \ ($nm$)')
    ax1.set_ylabel('Emission \ ($8$-$bit$)', color = 'k')
    ax1.tick_params('y', colors = 'k')
    ax1.spines['left'].set_color('k')
    #ax2.set_ylabel('$Temperature \ (^{O}C$)', color = 'g')
    #ax2.tick_params('y', colors = 'g')
    #ax2.spines['right'].set_color('g')
    #ax1.minorticks_on()
    #ax1.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75')
    #ax1.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75')
    #ax1.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75')
    #ax1.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75')
    #ax1.set_ylim(10.008, 10.011)
    #ax1.set_ylim(18375, 18440)
    #ax1.set_xlim(0, 2500)
    ax1.ticklabel_format(style='plain')
    #ax2.ticklabel_format(style='plain')

# Simple plot
#fig, ax1, ax2  = newfig(1)
fig, ax1 = newfig(1.8)
plot_style(ax1)

wavelength, amplitude = np.genfromtxt('Last.csv', delimiter = ',', unpack=True)
wavelength2, amplitude2, other = np.genfromtxt('SecondLast.csv', skip_header=6, delimiter = ',', unpack=True)
wavelength3, amplitude3, other = np.genfromtxt('SecondFromBottom.csv', skip_header=6, delimiter = ',', unpack=True)
wavelength4, amplitude4, other = np.genfromtxt('0.01.csv', skip_header=6, delimiter = ',', unpack=True)
wavelength5, amplitude5, other = np.genfromtxt('Top.csv', skip_header=6, delimiter = ',', unpack=True)

ax1.get_yaxis().get_major_formatter().set_useOffset(False)
#ax2.get_yaxis().get_major_formatter().set_useOffset(False)

ax1.plot(wavelength4, amplitude4, 'b.', label = '0.01 $mol \ L^{-1}$')
ax1.plot(wavelength5, amplitude5, 'g1', label = '0.001 $mol \ L^{-1}$')
ax1.plot(wavelength3, amplitude3, 'r+', label = '0.0001 $mol \ L^{-1}$')
ax1.plot(wavelength2, amplitude2, 'k2', label = '0.00001 $mol \ L^{-1}$')

legend = ax1.legend(loc='best', markerscale=2)
savefig('/home/iain/Desktop/Github/SpectroscoPi/Poster/EmissionGraph')


import logging

from .encoder import get_encoder, extract_zip
from os.path import join

ENCODER_PATH = "facedecoder/temp/encoders"
IMAGE_PATH = "facedecoder/temp/image"
ZIP_PATH = "facedecoder/temp/zip"
SLEEP_TIME = 2
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger(__name__)

log.setLevel(logging.DEBUG)

# ловим сообщение что нужно обновлять из очередени что нужно обновлять

# в очереди по ключу будет сообщение насчет обновления данных(в значение будет идентификатор названия папки где лежит
# zip архив)

def run_model_updater(tech_info_about_model: tuple):

    token, model_uid = tech_info_about_model
    log.debug("Start create model - %r", model_uid)
    log.debug("Extract zip - %r", model_uid)

    # извлекаем содержимое архива в папку
    status: bool = extract_zip(join(ZIP_PATH, model_uid), IMAGE_PATH)
    if status:
        log.debug("Done extract zip - %r", model_uid)
        log.debug("create encoders - %r", model_uid)

        result: dict = get_encoder(join(IMAGE_PATH, model_uid), join(ENCODER_PATH, model_uid))

        log.debug("done encoders - %r", model_uid)
        return result
    else:
        return {"faces_encoders": [], "faces_mapping": [], "status": status}


'''
Created on Mar 29, 2017

provides basic utilities for candidate rankings

@author: meike.zehlike
'''

import resource

def countProtected(ranking):
    result = 0
    for candidate in ranking:
        if candidate.isProtected:
            result += 1
    return result

def normalizeQualifications(ranking):
    """normalizes candidate qualifications to be within [0, 1]"""
    # find highest qualification of candidate
    qualifications = [ranking[i].qualification for i in range(len(ranking))]
    highest = max(qualifications)
    for candidate in ranking:
        candidate.originalQualification = candidate.originalQualification / highest
        candidate.qualification = candidate.qualification / highest

def setMemoryLimit(maxMem):
    if maxMem is None:
        maxMem = -1
    try:
        resource.setrlimit(resource.RLIMIT_MEMLOCK, (1, maxMem))
        return True
    except ValueError:
        return False

class Switch(object):
    def __init__(self, value):
        self.value = value
        self.fall = False

    def __iter__(self):
        """Return the match method once, then stop"""
        yield self.match
        raise StopIteration

    def match(self, *args):
        """Indicate whether or not to enter a case suite"""
        if self.fall or not args:
            return True
        elif self.value in args:  # changed for v1.5, see below
            self.fall = True
            return True
        else:
            return False


import pandas as pd
import nltk
from nltk.corpus import stopwords
from nltk import word_tokenize
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import NMF
from sklearn.preprocessing import Normalizer
from sklearn.pipeline import make_pipeline

nltk.download("stopwords")
nltk.download("punkt")
nltk.download("wordnet")
stop_words = set(stopwords.words('english'))

def clean_text(text):
  tokens = word_tokenize(text)
  tokens_transformed = [lemmatizer.lemmatize(t.lower()) for t in tokens if t not in stop_words]
  s = ""
  for t in tokens_transformed:
    s = s + t + " "
  return s

def make_df():
    df_original = pd.read_excel("imdb_movie_summary.xlsx")
    df = df_original.copy()
    df["transformed_summary"] = df["summary"].apply(clean_text)

    tfidf = TfidfVectorizer()

    model = NMF(n_components = 100)

    scaler = Normalizer()

    pipeline = make_pipeline(tfidf, model)

    df_transformed = pd.DataFrame(pipeline.fit_transform(df.transformed_summary), index = df.titles)

    genre_master = ["drama", "comedy", "romance", "horror", "action", "sci-fi", "sport", "fantasy", "crime", "music", "war", "biography", "thriller", "mystery", "family", "animation", "adventure", "musical", "history", "western", "film-noir" ]
    genre_dict = {"drama": 1, "comedy": 2, "romance": 3, "horror": 4, "action": 5, "sci-fi": 6, "sport":7, "fantasy": 8, "crime":  9, "music": 10, "war": 11, "biography": 12, "thriller": 13, "mystery": 14, "family": 15, "animation": 16, "adventure": 17, "musical": 18, "history": 19, "western": 20, "film-noir": 21}
    full = []
    for g in df.genre:
      g = g.lower()
      ind = [genre_dict.get(x) for x in g.split(", ")]
      temp = []
      for i in range(1, len(genre_master) + 1):
        if i in ind:
          temp.append(1)
        else:
          temp.append(0)
      full.append(temp)

      df_genre = pd.DataFrame(full, columns = genre_master, index = df.titles)
      df_combined = pd.DataFrame(scaler.fit_transform(pd.concat([df_transformed, df_genre], axis = 1)), index = df.titles)

    return df_combined
df = make_df()
df.to_excel("df_transformed.xlsx")


from __future__ import print_function

from math import log10

import numpy
import h5py

import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from model import NetARCNN

import argparse

from datetime import datetime

logfile = "log/log_ARCNN_" + str(datetime.now()) + ".txt"
train_data_path = "dataset/train_data.h5"
test_data_path = "dataset/test_data.h5"
checkpoint_path = "checkpoint_ARCNN/"

parser = argparse.ArgumentParser(description="Pytorch Deblocking Example")
parser.add_argument("--batchSize", type=int, default=100, help="training batch size")
parser.add_argument("--testBatchSize", type=int, default=30, help="testing batch size")
parser.add_argument("--nEpochs", type=int, default=30, help="number of epochs to train for")
parser.add_argument("--lr", type=float, default=0.0001, help="Learning Rate")
# Notice: it is "action" for cuda options, not type
parser.add_argument("--cuda", action="store_true", help="use cuda?") # what is "store true"?
# parser.add_argument("--threads", type=int, default=4, help="number of threads for data loader to use")
parser.add_argument("--seed",type=int, default=123, help="random seed to use.  Default = 123")
# parser.add_argument("--logfile",type=str, default=logfile, help="name of log file for training")
opt = parser.parse_args()
print(opt)

print("===> Building logfile")
output = open(logfile,'a+')
# output = open("log_"+str(datetime.now())+".txt")
# output = open("train_result.txt")
output.write("Model: ARCNN\nbatchSize: {}\ntestBatchSize: {}\nnEpochs: {}\nlearningRate: {}\n\nEpoch |PSNR before |PSNR after".format(opt.batchSize, opt.testBatchSize, opt.nEpochs, opt.lr))
output.close()

cuda = opt.cuda
if cuda and not torch.cuda.is_available():
    raise Exception("No GPU found, please run without --cuda")

torch.manual_seed(opt.seed)
if cuda:
    torch.cuda.manual_seed(opt.seed)

# print("===> Loading datasets")

print("===> Building model")
model = NetARCNN()
# model.load_state_dict(torch.load(checkpoint_path+".pkl"))

criterion = nn.MSELoss()

if cuda:
    model = model.cuda()
    criterion = criterion.cuda()

optimizer = optim.Adam(model.parameters(), lr=opt.lr)

def train(epoch, train_data, batch_size):

    epoch_loss = 0
    iter_num = 10
    for i in range(iter_num):

        # !! iter_num is only for testing and need to be updated later

        ran = numpy.sort(numpy.random.choice(train_data['data'].shape[0], batch_size, replace=False))
        batch_data = Variable(torch.from_numpy(train_data['data'][ran,:,:,:].astype(numpy.float32)/255.0))
        batch_label = Variable(torch.from_numpy(train_data['label'][ran,:,:,:].astype(numpy.float32)/255.0))
        if cuda:
            batch_data = batch_data.cuda()
            batch_label = batch_label.cuda()

        optimizer.zero_grad()
        # Error: argument 0 is not a Variable ?
        # Maybe it is because the order of the batch_data? currently [4*32*32*1]
        # the correct order is [4*1*32*32] and I have already fixed it,
        # but the bug is still exist
        # print(batch_data)
        loss = criterion(model(batch_data), batch_label)
        epoch_loss += loss.data[0]
        loss.backward()
        optimizer.step()

        print("===> Epoch[{}]({}/{}): Loss: {:.6f}".format(epoch, i, iter_num, loss.data[0]))

    print("===> Epoch {} Complete: Avg. Loss: {:.6f}".format(epoch, epoch_loss / iter_num))

def test(epoch, test_data, test_batch_size):
    avg_psnr1 = 0
    avg_psnr2 = 0
    iter_num = 5
    for i in range(iter_num):

        # !! iter_num is only for testing and need to be updated later

        ran2 = numpy.sort(numpy.random.choice(test_data['data'].shape[0],test_batch_size, replace=False))
        batch_data = Variable(torch.from_numpy(test_data['data'][ran2,:,:,:].astype(numpy.float32)/255.0))
        batch_label = Variable(torch.from_numpy(test_data['label'][ran2,:,:,:].astype(numpy.float32)/255.0))
        if cuda:
            batch_data = batch_data.cuda()
            batch_label = batch_label.cuda()

        prediction = model(batch_data)

        mse1 = criterion(batch_data, batch_label)
        mse2 = criterion(prediction, batch_label)
        psnr1 = 10 * log10(1 / mse1.data[0])
        psnr2 = 10 * log10(1 / mse2.data[0])

        avg_psnr1 += psnr1
        avg_psnr2 += psnr2
    print("===> Before: Avg. PSNR: {:.5f} dB; after: Avg. PSNR: {: .5f} dB".format((avg_psnr1 / iter_num), (avg_psnr2 / iter_num)))
    # print the result to a file so as to track the tendency
    # output = open('train_result.txt','a+')
    output = open(logfile, 'a+')
    output.write("{} {: .5f} {: .5f}\n".format(epoch, (avg_psnr1/iter_num), (avg_psnr2/iter_num)))
    output.close()

def checkpoint(epoch):
    # model_out_path = "model_epoch_{}.pth".format(epoch) # is that all right?
    # torch.save(model, checkpoint_path+model_out_path)
    torch.save(model.state_dict(), checkpoint_path+("checkpoint_ARCNN_epoch_{}.pkl".format(epoch))) # use this line to save parameters only
    print("Checkpoint saved to {}".format(checkpoint_path))

# can I open 2 files at one time?
# A: Sure

train_data = h5py.File(train_data_path, "r")
test_data = h5py.File(test_data_path, "r")

for epoch in range(1, opt.nEpochs + 1):
    train(epoch, train_data, opt.batchSize)
    test(epoch, test_data, opt.testBatchSize)
    checkpoint(epoch)


#!/usr/bin/env python

import copy
import logging

from hc.monitor import Monitor

logger = logging.getLogger("Monitor.{}".format(__name__))

class zmq_send(Monitor):

    def __init__(self, config, **args):
        Monitor.__init__(self, config, **args)

        self.metrics = self.getconfig("metrics", default=[])
        self.savevar = self.getconfig("save", default=False)
        self.timeout = self.getconfig("timeout", default=2)
        self.ignore_missing = self.getconfig("ignore_missing", default=True)

    def search(self, items):

        if isinstance(items, dict):

            if 'query' in items:
                status, results = self.getvars(self.build_query(items), first=False)
                if not status:
                    raise Exception('Missing value')

                if len(results) == 1:
                    return results[0]['values']
                else:
                    data = {}
                    for res in results:
                        # FIXME: it takes only the last path part into account
                        data[res['path'][-1]] = res['values']
                    return data
            elif 'value' in items:
                return items['value']
            else:
                for key, value in items.items():
                    items[key] = self.search(value)
        elif isinstance(items, list):
            for idx, item in enumerate(items):
                items[idx] = self.search(item)
        else:
            return self.getconfig(items, search_cfg=False)

        return items

    def build_query(self, keys, res=False, op_type='append'):

        if isinstance(keys, list):
            query = {'query': keys}
        else:
            query = keys

        if 'servers' in query:
            if isinstance(query['servers'], dict):
                ressrv, query['servers'] = self.getvars(query['servers'], default=[])

        try:
            if query['ns'] == 'localhost':
                query['ns'] = self.namespace
        except KeyError:
            pass

        if not res:
            return query

        if 'query' in query:
            try:
                query['query'][1][1] = res
            except(IndexError):
                logger.debug('Operation: {}'.format(op_type))
                if op_type == 'extend':
                    query['query'][1].extend(res)
                else:
                    query['query'][1].append(res)

        return query

    def get_from(self, keys):
        logger.debug('Getting data from: {}'.format(keys))
        if type(keys['from']) not in [list, dict]:
            return keys['from']

        return self.search(keys['from'])

    def send_to(self, name, keys, results, op_type='append'):

        logger.debug('Data to: {}'.format(keys['to']))

        data = {'failed': set(), 'ok': set()}

        try:
            onchange = keys['to']['onchange']
        except KeyError:
            onchange = False

        if onchange:
            resstatus, old_res = self.getvars(['DR', [[self.modulename, name, 'from']]])
            if not resstatus:
                logger.debug('No old data available.')
                old_res = None
            elif old_res == results:
                logger.debug('Results are the same.')
                return None

        query = self.build_query(keys['to'], res=results, op_type=op_type)
        resstatus, back_res = self.getvars(query, raw=True)

        for srv, res in back_res:
            if not res:
                data['failed'].add(srv)
            elif res['status']:
                data['ok'].add(srv)

        return data

    def resolve_when(self, keys):

        try:
            conditions = keys['when']
        except KeyError:
            return False

        for cond in conditions:
            logger.debug("When condition: {}".format(cond))
            rsstat, results = self.getvars(cond)
            logger.debug("When Status: {} Result: {}".format(rsstat, results))

            if not rsstat:
                logger.debug("Condition not found: not executing: {}".format(cond))
                return True

            if not results['resolved'][0]:
                logger.debug("Condition not met: not executing: {}".format(cond))
                return True

        return

    def runit(self):

        status = 0
        errs = ''

        data = {}
        for metric in self.metrics:
            name = list(metric.keys())[0]
            keys = copy.deepcopy(metric[name])

            if self.resolve_when(keys):
                continue

            try:
                results = self.get_from(keys)
            # FIXME: create a special type of exception
            except Exception as err:
                logger.debug(err)
                if self.ignore_missing:
                    continue
                raise(err)

            data[name] = {'from': {}, 'to': {}}
            if self.savevar:
                data[name]['from'] = results

            op_type = keys.get('op_type', 'append')
            data[name]['to'] = self.send_to(name, keys, results, op_type=op_type)

        return status, data, {}, errs


import cv2
import matplotlib.pyplot as plt
import numpy as np

def salt(img, n):
    for k in range(n):
        i = int(np.random.random() * img.shape[1]);
        j = int(np.random.random() * img.shape[0]);
        if img.ndim == 2:
            img[j,i] = 255
        elif img.ndim == 3:
            img[j,i,0]= 255
            img[j,i,1]= 255
            img[j,i,2]= 255
    return img

img = cv2.imread(r"D:\Python_all\pythonholder\practice_project\character_recognition\test_image\test\test03\mid_grabcut_result.jpg",0)
# img = cv2.resize(img,(384,216))
#高斯滤波
lbimg=cv2.GaussianBlur(img,(3,3),1.8)
#中值滤波
img = salt(img, 500)
middle = cv2.medianBlur(img, 5)
# cv2.imwrite(r"D:\Python_all\pythonholder\practice_project\character_recognition\test_image\test\test03\ middle.jpg", middle, params=None)
img = cv2.resize(img,(680,480))
lbimg = cv2.resize(lbimg,(680,480))

middle = cv2.resize(middle,(680,480))
cv2.imshow('src',img)
cv2.imshow('dst',middle)
cv2.waitKey()
cv2.destroyAllWindows()


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

"""
作者：Ted
日期：2017-07-13
说明：

包裹 class
货物 class
Uld class

"""
import simpy
import pandas as pd

from collections import namedtuple, defaultdict
from src.utils import PackageRecord, PipelineRecord, TruckRecord
from src.controllers import PathGenerator
import logging

__all__ = ["Package", "Truck", "Uld", "SmallBag", "SmallPackage", "Pipeline", "PipelineRes", "BasePipeline"]

# init path generator
path_generator = PathGenerator()

class Package:
    """包裹"""
    def __init__(self,
                 env: simpy.Environment,
                 attr: pd.Series,):

        # 包裹的所有信息都在 attr
        self.attr = attr
        # id
        self.item_id = self.attr["parcel_id"]
        # env
        self.env = env
        # data store
        self.machine_data = []
        self.pipeline_data = []
        # path_generator
        self.path_generator = path_generator.path_generator
        # paths
        self.planned_path = None
        self.path = None
        # next pipeline_id
        self.next_pipeline = None

    # use in unload machine
    def set_path(self, package_start):

        dest_code = self.attr["dest_zone_code"]
        dest_type = self.attr["dest_type"]
        parcel_type = self.attr["parcel_type"]
        sorter_type = "reload" if parcel_type == "parcel" else "small_sort"

        path = path_generator.path_generator(package_start, dest_code, sorter_type, dest_type)

        self.planned_path = tuple(path)
        self.path = list(self.planned_path)
        self.next_pipeline = self.planned_path[:2]

    def insert_data(self, record: namedtuple):
        # print out data
        if isinstance(record, PackageRecord):
            self.machine_data.append(record)

            logging.debug(msg=f"Package: {record.package_id} , action: {record.action}"
                             f", equipment: {record.equipment_id}, timestamp: {record.time_stamp}")

        elif isinstance(record, PipelineRecord):
            self.pipeline_data.append(record)

            logging.debug(msg=f"Package: {record.package_id} , action: {record.action}"
                              f", pipeline: {record.pipeline_id}, timestamp: {record.time_stamp}")

        else:
            raise ValueError("Wrong type of record")

    def pop_mark(self):
        """删去第一个节点, 返回下一个 pipeline id: (now_loc, next_loc)"""
        self.path.pop(0)
        if len(self.path) >= 2:
            self.next_pipeline = tuple(self.path[0: 2])
        # 当 package 去到 reload（终分拣）， 终分拣的队列 id 只有一个值
        elif len(self.path) == 1:
            self.next_pipeline = self.path[-1]
        else:
            raise ValueError('The path have been empty!')
        # remove the now_loc
        # 改变下一个 pipeline id

    def __str__(self):
        display_dct = dict(self.attr)
        return f"<package attr:{dict(display_dct)}, path: {self.planned_path}>"

class SmallPackage(Package):
    """小件包裹"""
    def __str__(self):
        display_dct = dict(self.attr)
        return f"<SmallBag attr:{dict(display_dct)}, path: {self.planned_path}>"

class SmallBag(Package):
    """小件包"""
    def __init__(self, env: simpy.Environment,
                 attr: pd.Series,
                 small_packages: list):

        super(SmallBag, self).__init__(env, attr,)

        # 存储小件包裹
        self.store = small_packages
        assert self._all_are_small_packages(), "SmallBag store SmallPackage only !!"
        self.store_size = len(self.store)

    def _all_are_small_packages(self):
        packages_bool = [isinstance(small_package, SmallPackage) for small_package in self.store]
        return all(packages_bool)

    def __str__(self):
        display_dct = dict(self.attr)
        return f"<SmallBag attr:{dict(display_dct)}, path: {self.planned_path}, store_size:{self.store_size}>"

class Truck:
    """货车"""
    def __init__(self, env: simpy.Environment, item_id: str, come_time: int, truck_type: str, packages: list):
        """
        :param truck_id: self explain
        :param come_time: self explain
        :param packages: a data frame contain all packages
        """
        self.item_id = item_id
        self.come_time = come_time
        self.store = packages
        assert self._all_are_packages(), "Truck store Package only !!"
        self.store_size = len(self.store)
        self.truck_type = truck_type
        self.env = env
        self.truck_data = []

    def _all_are_packages(self):
        packages_bool = [isinstance(package, Package) for package in self.store]
        return all(packages_bool)

    def insert_data(self, record: namedtuple):

        if isinstance(record, TruckRecord):
            self.truck_data.append(record)
        else:
            raise ValueError("Wrong type of record")

    def __str__(self):
        return f"<truck_id: {self.item_id}, come_time: {self.come_time}, store_size:{self.store_size}>"

class Uld(Truck):
    """航空箱"""
    pass

class BasePipeline:

    def __init__(self, env: simpy.Environment, pipeline_id: str, equipment_id: str, machine_type: str, ):

        self.env = env
        self.pipeline_id = pipeline_id
        self.equipment_id = equipment_id
        self.queue_id = pipeline_id
        self.machine_type = machine_type
        self.queue = simpy.Store(env)

    def get(self):
        return self.queue.get()

    def put(self, item):

        item.insert_data(
            PipelineRecord(
                pipeline_id=':'.join(self.pipeline_id),
                queue_id=self.queue_id,
                package_id=item.item_id,
                time_stamp=self.env.now,
                action="start", ))

        self.queue.put(item)

class Pipeline:

    """传送带"""

    def __init__(self,
                 env: simpy.Environment,
                 delay_time: float,
                 pipeline_id: tuple,
                 queue_id: str,
                 machine_type: str,
                 ):

        self.env = env
        self.delay = delay_time
        self.queue = simpy.Store(env)
        self.pipeline_id = pipeline_id
        self.queue_id = queue_id
        self.machine_type = machine_type
        self.equipment_id = self.pipeline_id[1]  # in Pipeline the equipment_id is equipment after this pipeline

    def latency(self, item: Package):
        """模拟传送时间"""

        # pipeline start server
        item.insert_data(
            PipelineRecord(
                pipeline_id=':'.join(self.pipeline_id),
                queue_id=self.queue_id,
                package_id=item.item_id,
                time_stamp=self.env.now,
                action="start", ))

        yield self.env.timeout(self.delay)
        # cutting path
        item.pop_mark()

        # package wait for next process
        item.insert_data(
            PackageRecord(
                equipment_id=self.equipment_id,
                package_id=item.item_id,
                time_stamp=self.env.now,
                action="wait", ))

        # pipeline end server
        item.insert_data(
            PipelineRecord(
                pipeline_id=':'.join(self.pipeline_id),
                queue_id=self.queue_id,
                package_id=item.item_id,
                time_stamp=self.env.now,
                action="end", ))

        self.queue.put(item)

    def put(self, item: Package):
        self.env.process(self.latency(item))

    def get(self):
        return self.queue.get()

    def __str__(self):
        return f"<Pipeline: {self.pipeline_id}, delay: {self.delay}>"

class PipelineRes(Pipeline):

    def __init__(self,
                 env: simpy.Environment,
                 resource_dict: defaultdict,
                 equipment_resource_dict: dict,
                 delay_time: float,
                 pipeline_id: tuple,
                 queue_id: str,
                 machine_type: str,
                 ):

        super(PipelineRes, self).__init__(env,
                                          delay_time,
                                          pipeline_id,
                                          queue_id,
                                          machine_type,)

        self.equipment_last = self.pipeline_id[0]  # in PipelineRes the equipment_id is equipment before this pipeline
        self.equipment_next = self.pipeline_id[1]  # in PipelineRes the equipment_id is equipment before this pipeline
        self.resource_id = equipment_resource_dict[self.equipment_last]
        self.resource = resource_dict[self.resource_id]["resource"]

    def latency(self, item: Package):

        with self.resource.request() as req:
            """模拟传送时间"""

            yield req

            # pipeline start server
            item.insert_data(
                PipelineRecord(
                    pipeline_id=':'.join(self.pipeline_id),
                    queue_id=self.queue_id,
                    package_id=item.item_id,
                    time_stamp=self.env.now,
                    action="start", ))

            yield self.env.timeout(self.delay)

            # package start for process
            item.insert_data(
                PackageRecord(
                    equipment_id=self.equipment_next,
                    package_id=item.item_id,
                    time_stamp=self.env.now,
                    action="wait", ))

            # cutting path, change item next_pipeline
            item.pop_mark()

            # pipeline end server
            item.insert_data(
                PipelineRecord(
                    pipeline_id=':'.join(self.pipeline_id),
                    queue_id=self.queue_id,
                    package_id=item.item_id,
                    time_stamp=self.env.now,
                    action="end", ))

            self.queue.put(item)

if __name__ == '__main__':
    pass


import pyodbc
# from accounts.models import User

con = pyodbc.connect('Trusted_Connection=yes', driver = '{SQL Server}',server = 'DESKTOP-6O0KO5B\SQLEXPRESS', database ='sanpham')
cursor = con.cursor()

def listIdSp():
    query = "select id from danhsach"
    try:
        cursor.execute(query)
    except Exception:
        return False
    data = []
    for row in cursor.fetchall():
        data.append(int(row.id))
    return data
def codeAnalysis():
    listId = listIdSp()
    data = []
    for i in listId:
        query = "select COUNT(*) as code_count from dinhdanh where id_sp =" + str(i)
        try:
            cursor.execute(query)
        except Exception:
            return False
        for row in cursor.fetchall():
            data.append(int(row.code_count))
    return data
def getDataDanhSach():              # lấy thông tin về các sản phẩm đang tồn tại
    query = "select * from danhsach"
    try:
        cursor.execute(query)
    except Exception:
        return False
    data = []
    for row in cursor.fetchall():
        data.append(row)
    return data
# from ..app.utils import getDataDanhSach
# data = getDataDanhSach()

# for i in data:
#     print('\"' + i[1] + '\"')
def history():
    query = "select CAST(ngay_sinh as DATE),DATEPART(hour,ngay_sinh) as gio,sum(num) as sum_day from lichsu " \
            "group by CAST(ngay_sinh as date) ,DATEPART(hour,ngay_sinh) " \
            "order by CAST(ngay_sinh as date),DATEPART(hour,ngay_sinh)"
    try:
        cursor.execute(query)
    except Exception:
        return False
    data = []
    for row in cursor.fetchall():
        data.append(row)
    return data
# print(history())


# Django modules
from django.shortcuts import render_to_response
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.template import RequestContext
from django.db import transaction
from django.views.decorators.csrf import csrf_protect

# From Python

# Our modules
from infra.forms.message_type import SearchMessageQueueForm, ActionMessageQueueFormSet
from infra.models import MessageQueue
from infra.custom.build_query_filter import build_query_filter
import infra.custom.custom_json as custom_json

# This program's constants

# Decorators to ensure that user is logged in and
# is a staff member
@login_required
@staff_member_required
@csrf_protect
# Have to fix all exception before enabling commit manually.
# When turned on, will hide any exception behind its TransactionManagementError
# so remark off when debugging...
@transaction.commit_manually
def my_messages(request, delivery_method='Q'):
    """

    Display Messages for logged in User and allow
    action on message.

    Delivery Method : A=Alerts, Q=Queued Messages

    queryset_criteria : Because we want to save previous query
    criteria as json in formset so that we can redisplay the rows
    which were updated. However filter() args cannot be simply be
    treated like a dict, so we borrowed code from django snippets
    that allow us to express the query criteria as a list which
    can be converted to Q objects.
    See infra/custom/build_query_filter.py

    Uses template infra/my_messages.html

    """
    # Initialise Error Messages
    error_messages = []

    # Post happens when user clicks on enters data and press submit button
    if request.method == 'POST':

        search_form = SearchMessageQueueForm(request.POST)
        # User pressed search button
        if request.POST.has_key('search'):
            if search_form.is_valid():
                # Reset queryset criteria, we and together all
                queryset_criteria = ['and', ['exact', 'recipient', request.user.id]]

                if search_form.cleaned_data['delivery_method']:
                    queryset_criteria.append(['exact', 'message_type__delivery_method', search_form.cleaned_data['delivery_method']])
                else:
                    queryset_criteria.append(['exact', 'message_type__delivery_method', delivery_method])
                if search_form.cleaned_data['required_action']:
                    queryset_criteria.append(['exact', 'message_type__required_action', search_form.cleaned_data['required_action']])
                if search_form.cleaned_data['external_delivery_method']:
                    queryset_criteria.append(['exact', 'message_type__external_delivery_method', search_form.cleaned_data['external_delivery_method']])
                if search_form.cleaned_data['sender_name']:
                    queryset_criteria.append(['exact', 'sender_name', search_form.cleaned_data['sender_name']])
                if search_form.cleaned_data['sent_on']:
                    queryset_criteria.append(['gte', 'sent_on', search_form.cleaned_data['sent_on']])
                if search_form.cleaned_data.has_key('acted_flag'):
                    queryset_criteria.append(['isnull', 'action_on', (not search_form.cleaned_data['acted_flag'])])

                my_messages = MessageQueue.objects.filter(build_query_filter(queryset_criteria))
                message_formset = ActionMessageQueueFormSet(queryset=my_messages)
        # User press post to save updates to messages
        elif request.POST.has_key('post'):
            # Retrieve previous queryset criteria
            queryset_criteria = custom_json.loads(request.POST.get('queryset_criteria'))
            # Instantiate formset from Post request and with previous queryset
            my_messages = MessageQueue.objects.filter(build_query_filter(queryset_criteria))
            message_formset = ActionMessageQueueFormSet(request.POST, queryset=my_messages)
            # Save updates to messages
            if message_formset.is_valid():
                saved_messages = message_formset.save()
                transaction.commit()
                # Inform user how many saved
                error_messages.append(_("%d Message(s) has been updated") % (len(saved_messages),))
                # Reset formset (to show updated messages)
                message_formset = ActionMessageQueueFormSet(queryset=my_messages)
            # else redisplay formset with errors

    else:
        # GET Method (1st time this page is called), default our hidden entity
        search_form = SearchMessageQueueForm()
        # Default is show unread messages only
        queryset_criteria = ['and', ['exact', 'recipient', request.user.id],
                ['exact', 'message_type__delivery_method', delivery_method],
                ['isnull', 'action_on', True]]

        my_messages = MessageQueue.objects.filter(build_query_filter(queryset_criteria))
        message_formset = ActionMessageQueueFormSet(queryset=my_messages)

    # Note: Paginator does not work with formsets

    # Even reads need to be committed
    transaction.commit()
    # Display page to user. We have to commit again, possibly because render to response will
    # dirty trx buffer again
    with transaction.commit_on_success(): return render_to_response('infra/my_messages.html', {'search_form': search_form,
        'message_formset': message_formset, 'queryset_criteria': custom_json.dumps(queryset_criteria),
        'error_messages': error_messages, 'media': search_form.media,
        }, context_instance=RequestContext(request))


import shutil, os, sys
from fenx.resources import icon_process
from PyQt5.QtWidgets import QApplication
app = QApplication([])

png = os.path.join(os.path.dirname(icon_process.__file__), 'icons', 'fenx.png')
png_i = os.path.join(os.path.dirname(icon_process.__file__), 'icons', 'fenx_install.png')

if not os.path.exists(png):
    sys.exit(1)

icon_process.convert_to_ico(png_i)
ficon = icon_process.convert_to_ico(png)

tray = os.path.join(os.path.dirname(png), 'tray.png')
trayw = os.path.join(os.path.dirname(png), 'tray_wait.png')
shutil.copy2(png, tray)
icon_process.color_correct_hsv(png, trayw, v=0.5)


# Modules
import datetime as dt
from typing import List
import matt
# ------------------------------------------------------------------------------------------------------------------------------------------------------
# User Input
arguments = {
    "BranchIds"   : {"type" : List[int], "names" : ["i", "bids", "branchids"], "default" : [],              "info" : "List of branches to assign surveys for."},
    "ClosureDate" : {"type" : dt.date,   "names" : ["c", "cl", "closuredate"], "default" : dt.date.today(), "info" : "Date at which the branches will close."},
    "Logging"     : {"type" : bool,      "names" : ["l", "log", "logging"],    "default" : True,            "info" : R"Log the SQL statments executed by this script in 'E:\Oneoffs'."},
    "Autocommit"  : {"type" : bool,      "names" : ["a", "ac", "autocommit"],  "default" : False,           "info" : "Automatically commit all transactions in this script instead of requesting user permission."}
}
args = matt.args.IOHandler(arguments=arguments, program_desc="Script to make new entries in the branch closures table.")
branch_ids, closure_date, logging, autocommit = args["BranchIds"], args["ClosureDate"], args["Logging"], args["Autocommit"]
# ------------------------------------------------------------------------------------------------------------------------------------------------------
# Setup
alch = matt.sql.Alchemy(schemas=["base"])
s, tables = alch.session, alch.reflection.classes
Closures = tables.branch_closures
alch.initialize_log("BranchClosures", logdir=matt.lbg.one_offs_path, active=logging)
# ------------------------------------------------------------------------------------------------------------------------------------------------------
# Execution
extant = [rec[0] for rec in s.query(Closures.branch_id).filter(Closures.branch_id.in_(branch_ids)).all()]
if extant:
    raise ValueError(f"Duplicate branch ids {[branch for branch in branch_ids if branch in extant]} not allowed.")

new = [Closures(branch_id=branch, closure_date=closure_date, processed=None, created_date=alch.func.now(), last_modified_date=alch.func.now()) for branch in branch_ids]
alch.insert_statement_with_safeguards(new, select_from=Closures, autocommit=autocommit)


from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin

from .base import BitLinkBaseView

class BitLinkCreateView(BitLinkBaseView, LoginRequiredMixin, CreateView):
    template_name = 'create.html'
    fields = [
        'original_url',
    ]
    success_url = '/mypage/'

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(BitLinkCreateView, self).form_valid(form)

    def get_context_data(self, **kwargs):
        context = super(BitLinkCreateView, self).get_context_data(**kwargs)
        context['site_name'] = 'Create BitLink'
        return context


from django import forms
from .models import Club
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _

# Form for adding a new Club
class ClubForm(forms.Form):
	name = forms.CharField(label='Club Name', max_length=64,required=True)
	website = forms.CharField(label='Website', max_length=128,required=False)

	class Meta:
		model = Club
		fields = {'name' , 'email' , 'website' , 'meeting_times' , 'president' , 'treasurer' , 'icc_rep'}

	def save(self, commit=True):
		club = Club()
		club.name = self.cleaned_data['name']
		club.website = self.cleaned_data['website']

		if commit:
			club.save()

		return club

# Form for editing the a Club that already exists
class EditClubForm(forms.Form):
	id = forms.IntegerField(required=True)
	name = forms.CharField(label='Club Name', max_length=64,required=True)
	website = forms.URLField(label='Website', max_length=128,required=False)
	email = forms.EmailField(label='Email', max_length=256,required=False)
	meeting_times = forms.CharField(label='Meeting Times', max_length=256,required=False)

	class Meta:
		model = Club
		fields = {'id', 'name' , 'email' , 'website' , 'meeting_times'}

	def save(self, commit=True, user_id = None):
		club = Club.objects.filter(id=self.cleaned_data['id'])
		if len(club) == 1:
			club = club[0]
		# check if the person updating the club actually has access
		if user_id in {club.president, club.treasurer, club.icc_rep}:
			club.name = self.cleaned_data['name']
			club.website = self.cleaned_data['website']
			club.email = self.cleaned_data['email']
			club.meeting_times = self.cleaned_data['meeting_times']

			if commit:
				club.save()
		else:
			raise ValidationError(message = "User ID " + str(user_id) + " doesn't have permission to edit Club ID " + str(club.id) )

		return club


## plot_binned_radialPSD.py
##=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## DTM 10/12/2012
##=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

#import modules
import sys
import numpy as np, matplotlib.pyplot as plt
from matplotlib import rcParams

def make_plots(file_id):

    # Set up fonts for plots
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['arial']
    rcParams['font.size'] = 8
    rcParams['legend.numpoints'] = 1
    OutputFigureFormat='png'
    #########################
    #                       #
    #   READ IN THE DATA    #
    #                       #
    #########################

    # open file
    #FileName1 = 'test_binned_radialPSD.txt'
    FileName1 = file_id+'_binned_radialPSD.txt'
    #OutputFigureName = 'radial_PSD'
    #OutputFigureFormat = 'eps'
    f1 = open(FileName1,'r')  # open file
    lines = f1.readlines()   # read in the data
    no_lines = len(lines)   # get the number of lines (=number of data)
    # data variables
    # data variables
    radial_frequency_binned = np.zeros(no_lines-1)
    PSD_binned = np.zeros(no_lines-1)
    wavelength_binned = np.zeros(no_lines-1)
    R_sq = np.zeros(no_lines-1)
    Beta = np.zeros(no_lines-1)
    Background = np.zeros(no_lines-1)
    CI95 = np.zeros(no_lines-1)
    CI99 = np.zeros(no_lines-1)

    for i in range (1,no_lines-1):
        line = lines[i].strip().split(" ")
        #print line
        radial_frequency_binned[i] = float(line[0])
        wavelength_binned[i] = float(line[1])
        PSD_binned[i] = float(line[2])
        R_sq[i] = float(line[3])
        Beta[i] = float(line[4])
        Background[i] = float(line[5])
        CI95[i] = float(line[6])
        CI99[i] = float(line[7])
    f1.close()

    #FileName2 = 'test_radialPSD.txt'
    FileName2 = file_id+'_radialPSD.txt'
    f2 = open(FileName2,'r')  # open file
    lines = f2.readlines()   # read in the data
    no_lines = len(lines)   # get the number of lines (=number of data)
    # data variables
    #radial_frequency = np.zeros(no_lines-1)
    #wavelength = np.zeros(no_lines-1)
    #amplitude = np.zeros(no_lines-1)
    #norm_amplitude = np.zeros(no_lines-1)
    rf = []
    wl = []
    amp = []
    na = []

    for i in range (1,no_lines-1):
        line = lines[i].strip().split(" ")
        #print line
        #if( float(line[1]) > 100):
        rf.append(float(line[0]))
        wl.append(float(line[1]))
        amp.append(float(line[2]))
        na.append(float(line[3]))

    f2.close()

    radial_frequency = np.asarray(rf)
    wavelength = np.asarray(wl)
    amplitude = np.asarray(amp)
    norm_amplitude = np.asarray(na)

    rollover_freq = radial_frequency_binned[Beta==max(Beta)]
    #########################
    #                       #
    #   MAKE PSD plot       #
    #                       #
    #########################

    plt.figure(1, facecolor='white',figsize=[4,8])
    ax1 = plt.subplot(3,1,1)
    ax1.axvline(x=rollover_freq,linestyle='dashed',color='black')
    #ax1.vlines(rollover_freq,10**-9,10**2,linestyles='dashed')
    ax1.plot(radial_frequency, amplitude,".", alpha = 0.10, markeredgewidth = 0, color = '0.0')
    ax1.plot(radial_frequency_binned, PSD_binned,"o",color='white', markeredgecolor="red")
    ax1.plot(radial_frequency_binned, Background,"-b")
    ax1.set_yscale('log')
    ax1.set_xscale('log')
    ax1.set_xlim(xmax=0.3)
    ax1.grid(True)
    xmin,xmax = ax1.get_xlim()
    ax1.annotate('a', xy=(0.95,0.85), xycoords='axes fraction',backgroundcolor='white',horizontalalignment='right', verticalalignment='bottom', fontsize=rcParams['font.size']+2)
    ax1.set_ylabel('DFT mean-squared amplitude (m$^2$)')

    ax2 = plt.subplot(3,1,2)
    ax2.axvline(x=rollover_freq,linestyle='dashed',color='black')
    series1 = ax2.plot(radial_frequency_binned[Beta != -9999], R_sq[Beta != -9999],"o", color="blue", label = 'R$^2$')
    ax2.set_ylabel('R$^2$')
    ax2.set_xscale('log')
    ax2.set_xlim(xmax=1)
    ax2.set_ylim(ymin=0.9*np.min(R_sq[Beta  > 0]),ymax=1)
    ax2.xaxis.grid(True)
    ax2.annotate('b', xy=(0.95,0.85), xycoords='axes fraction',backgroundcolor='white',horizontalalignment='right', verticalalignment='bottom', fontsize=rcParams['font.size']+2)

    ax3 = plt.subplot(3,1,2).twinx()
    series2 = ax3.plot(radial_frequency_binned[Beta != -9999], Beta[Beta != -9999],"d",color='white', markeredgecolor="red", label = 'Beta')
    ax3.set_ylabel('Beta')
    ax3.set_xscale('log')
    ax3.set_ylim(ymin=0.8*np.min(Beta[Beta > 0]),ymax=1.25*np.max(Beta[Beta > 0]))
    ax3.set_xlim(xmin,xmax)
    series = series1+series2
    labels = [l.get_label() for l in series]
    ax2.legend(series, labels, loc=3)

    ax4 = plt.subplot(3,1,3)
    ax4.axhspan(0, np.max(CI99), alpha = 0.2)
    ax4.axhspan(0, np.max(CI95), alpha = 0.2)
    ax4.plot(radial_frequency, norm_amplitude,".", alpha = 0.75, markeredgewidth = 0, color = '0.0')
    ax4.set_ylabel('Normalised Power')
    ax4.set_xlabel('Radial Frequency (m$^{-1}$)')
    ax4.set_xscale('log')
    ax4.set_xlim(xmin,xmax)
    ax4.grid(True)
    ax4.annotate('c', xy=(0.95,0.85), xycoords='axes fraction',backgroundcolor='white',horizontalalignment='right', verticalalignment='bottom', fontsize=rcParams['font.size']+2)
    plt.tight_layout()
    plt.subplots_adjust(right=0.85)
    #plt.show()
    plt.savefig(file_id+'_spectrum.' + OutputFigureFormat, format=OutputFigureFormat)

if __name__ == "__main__":
    #file_id = "/home/s1143956/LSD_Datastore/LH_paper_dems/gm_30m_dem"
    make_plots(sys.argv[1])