# -- encoding:utf-8 --
import os
import argparse

import cv2
import numpy as np
from keras import backend as K
from keras.layers import Input, Activation, Conv2D, BatchNormalization, Lambda, MaxPooling2D,concatenate
from keras.models import Model
from keras.optimizers import SGD
from keras.callbacks import ModelCheckpoint, TensorBoard
import tensorflow as tf
import keras.backend.tensorflow_backend as KTF

def build_model(width, height, num_channels, classes):
    input_tensor = Input(name='the_input', shape=(width, height, num_channels), dtype='float32')
    x = input_tensor
    base_conv = 32

    for i in range(3):
        x = Conv2D(base_conv * (2 ** i), (3, 3), padding="same")(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        x = MaxPooling2D(pool_size=(2, 2))(x)
    x = Conv2D(256, (5, 5))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    conx1 = Conv2D(256, (7, 1), padding='same')(x)
    conx1 = BatchNormalization()(conx1)
    conx2 = Conv2D(256, (5, 1), padding='same')(x)
    conx2 = BatchNormalization()(conx2)
    conx3 = Conv2D(256, (3, 1), padding='same')(x)
    conx3 = BatchNormalization()(conx3)
    conx4 = Conv2D(256, (1, 1), padding='same')(x)
    conx4 = BatchNormalization()(conx4)
    x=concatenate([conx1,conx2,conx3,conx4],axis=-1)
    x = Conv2D(1024, (1, 1))(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(classes, (1, 1))(x)
    x = Activation('softmax')(x)
    model = Model(input_tensor, x, name='miniggnet')
    # y_pred = x
    # return input_tensor, y_pred
    return model


import requests
import json
import codecs
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from typing import List
from titlecase import titlecase

class GetData:
    """A class to get data from a specific local authority"""

    def __init__(self, authority_name=None, url='http://api.erg.kcl.ac.uk/AirQuality/'):
        self.authority_name = titlecase(authority_name)
        self.url = url

    def get_local_authority_data(self, url_suffix='Information/MonitoringLocalAuthority/GroupName=London/Json',
                                 get_id_only=False):
        r = requests.get(self.url + url_suffix)
        data = r.json()['LocalAuthorities']['LocalAuthority']
        if self.authority_name is None:
            pass
        elif self.authority_name is not None:
            for item in data:
                if item['@LocalAuthorityName'] == self.authority_name and get_id_only == False:
                    data = item
                elif item['@LocalAuthorityName'] == self.authority_name and get_id_only == True:
                    data = item['@LocalAuthorityCode']
        else:
            print('Please insert a valid name')
        return data

    def get_pollution_data(self, local_authority_id, years: List):
        pollution_df = pd.DataFrame({'species_code': [], 'species_desc': [],
                                     'objective_name': [], 'value': [],
                                     'site_name': [],
                                     'year': []})
        for year in years:
            url_suffix = '/Annual/MonitoringObjective/LocalAuthorityId={0}/Year={1}/Json'.format(
                str(local_authority_id),
                str(year))
            r = requests.get(self.url + url_suffix)
            json_data = json.loads(codecs.decode(r.content, 'utf-8-sig'))

            try:
                for item in json_data['SiteObjectives']['Site']:
                    for part in item['Objective']:
                        pollution_df = pollution_df.append({'species_code': part['@SpeciesCode'],
                                                            'species_desc': part['@SpeciesDescription'],
                                                            'objective_name': part['@ObjectiveName'],
                                                            'value': part['@Value'],
                                                            'site_name': item['@SiteName'],
                                                            'longitude': item['@Longitude'],
                                                            'latitude': item['@Latitude'],
                                                            'year': part['@Year']}, ignore_index=True)
            except TypeError:
                list_data = json_data['SiteObjectives']['Site']
                for item in list_data['Objective']:
                    pollution_df = pollution_df.append({'species_code': item['@SpeciesCode'],
                                                        'species_desc': item['@SpeciesDescription'],
                                                        'objective_name': item['@ObjectiveName'],
                                                        'value': item['@Value'],
                                                        'site_name': list_data['@SiteName'],
                                                        'longitude': list_data['@Longitude'],
                                                        'latitude': list_data['@Latitude'],
                                                        'year': item['@Year']}, ignore_index=True)
        return pollution_df

    def get_daily_pollution_stats(self, local_authority_ids: List):
        """Provides daily pollution stats based on area: need to provide local_authority_ids in the for of a List"""
        daily_pollution_df = pd.DataFrame({'species_code': [], 'site_name': [], 'air_quality_index': [],
                                           'air_quality_band': [], 'site_code': []})
        for item in local_authority_ids:
            url_suffix = '/Daily/MonitoringIndex/Latest/LocalAuthorityId={0}/Json'.format(str(item))
            r = requests.get(self.url + url_suffix)
            json_data = r.json()

            try:
                list_data = json_data['DailyAirQualityIndex']['LocalAuthority']['Site']
                for item in list_data:
                    if type(item['Species']) == list:
                        for part in item['Species']:
                            daily_pollution_df = daily_pollution_df.append({'species_code': part['@SpeciesCode'],
                                                                            'site_code': item['@SiteCode'],
                                                                            'air_quality_index': int(part[
                                                                                '@AirQualityIndex']),
                                                                            'air_quality_band': part['@AirQualityBand'],
                                                                            'site_name': item['@SiteName'],
                                                                            "retrieval_timestamp": pd.to_datetime(
                                                                                'today').strftime('%Y-%m-%d')
                                                                            },
                                                                           ignore_index=True)

                    elif type(item['Species']) == dict:
                        daily_pollution_df = daily_pollution_df.append({'species_code': item['Species']['@SpeciesCode'],
                                                                        'site_code': item['@SiteCode'],
                                                                        'air_quality_index': int(item['Species'][
                                                                                '@AirQualityIndex']),
                                                                        'air_quality_band': item['Species'][
                                                                            '@AirQualityBand'],
                                                                        'site_name': item['@SiteName'],
                                                                        "retrieval_timestamp": pd.to_datetime(
                                                                            'today').strftime('%Y-%m-%d')
                                                                        }, ignore_index=True)
            except TypeError:
                dict_data = json_data['DailyAirQualityIndex']['LocalAuthority']['Site']
                for item in dict_data['Species']:
                    daily_pollution_df = daily_pollution_df.append({'species_code': item['@SpeciesCode'],
                                                                    'site_code': dict_data['@SiteCode'],
                                                                    'air_quality_index': int(item['@AirQualityIndex']),
                                                                    'air_quality_band': item['@AirQualityBand'],
                                                                    'site_name': dict_data['@SiteName'],
                                                                    "retrieval_timestamp": pd.to_datetime(
                                                                        'today').strftime('%Y-%m-%d')},
                                                                   ignore_index=True)
        daily_pollution_df['borough'], daily_pollution_df['site_name'] = daily_pollution_df['site_name'].str.split(
            ' - ', 1).str
        return daily_pollution_df

    @staticmethod
    def plot_pollution_by_species(species, data_df: pd.DataFrame):
        df_species = data_df.loc[(data_df['species_code'] == species) & (data_df['objective_name'] == 'Capture Rate (%)')]
        df_species['value'] = df_species['value'].apply(pd.to_numeric, errors='coerce')
        plt.figure(figsize=(16, 6))
        g = sns.lineplot(x="year", y="value", hue="site_name", data=df_species, marker="o")


import configparser

class CheckWordsPipeline:
    def __init__(self, bad_title_file_path, domain_parking_file_path, maintenance_file_path, flash_file_path):
        config = configparser.ConfigParser()
        config.read('./config.ini')
        wordlists_config = config['wordlists']
        self.bad_title_file_path = wordlists_config['badtitle']
        self.domain_parking_file_path = wordlists_config['domainparking']
        self.maintenance_file_path = wordlists_config['maintainance']
        self.flash_file_path = wordlists_config['flash']
        with open(bad_title_file_path, "r") as f:
            self.bad_title_words = [word.strip() for word in f.readlines()]
        with open(domain_parking_file_path, "r") as f:
            self.domain_parking_words = [word.strip() for word in f.readlines()]
        with open(maintenance_file_path, "r") as f:
            self.maintenance_words = [word.strip() for word in f.readlines()]
        with open(flash_file_path, "r") as f:
            self.flash_words = [word.strip() for word in f.readlines()]

    def process_item(self, item, spider):
        content = item["content"]
        url = item["url"]

        results = {
            "badTitle": self.check_words(self.bad_title_words, item["title"], url, "title"),
            "Domainparking": self.check_words(self.domain_parking_words, content, url, "domainparking"),
            "Maintenance": self.check_words(self.maintenance_words, content, url, "maintenance"),
            "foundFlash": self.check_words(self.flash_words, content, url, "flash"),
        }

    def split_domain(domain):
        if domain.startswith("http://"):
            domain = domain[7:]
        elif domain.startswith("https://"):
            domain = domain[8:]

        # Extract the domain name
        return domain.split("/")[0]

    def check_words(self, words, content, url, mode):
        try:
            if mode == "domainparking":
                alfa_target = f'Die Domain "{split_domain(url)}" ist nicht verfügbar.'
                if alfa_target in content:
                    print(f"found: {url} - {mode} - {alfa_target}")
                    return True
                else:
                    for word in words:
                        if word in content:
                            print(f"found: {url} - {mode} - {word}")
                            return True
                    return False
            for word in words:
                if word in content:
                    print(f"found: {url} - {mode} - {word}")
                    return True
            return False
        except TypeError as e:
            pass


import pathlib
from setuptools import setup

# The directory containing this file
HERE = pathlib.Path(__file__).parent

# The text of the README file
README = (HERE / "README.md").read_text()

# This call to setup() does all the work
setup(
    name="pviewer",
    version="1.0",
    description="pviewer is a tool which will be used to find the conflicting policies for a AWS IAM user",
    long_description=README,
    long_description_content_type="text/markdown",
    url="https://github.com/ankitsaini2609/PViewer",
    author="Ankit Saini",
    author_email="ankitsaini2609@gmail.com",
    license="MIT",
    classifiers=[
        "License :: OSI Approved :: MIT License",
        "Programming Language :: Python :: 3",
    ],
    packages=["pviewer"],
    include_package_data=True,
    install_requires=["boto3", "graphviz", "pytest"],
    entry_points={
        "console_scripts": [
            "pviewer=pviewer.__main__:main",
        ]
    },
)


from django.urls import reverse
from django.db import models
from product.models import Product
from invoice.models import Invoice, InvoiceLine
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from main.models import NamingSeries as NS

ENTRY_STATUS = [
    ('draft', 'Draft'),
    ('submitted', 'Submitted'),
    ('cancelled', 'Cancelled'),
]
ENTRY_TYPE = [
    ('transfer', 'Transfer'),
    ('issue', 'Issue'),
    ('delivery', 'Delivery'),
    ('receipt', 'Receipt'),
]

class StockEntry(models.Model):
    code = models.CharField(max_length=10, null=True)
    date = models.DateField(null=True)
    type = models.CharField(choices=ENTRY_TYPE, default='', null=True, max_length=10)
    status = models.CharField(choices=ENTRY_STATUS, default='draft', null=True, max_length=10)
    total = models.DecimalField(default=0.0, decimal_places=2, max_digits=12)
    parent = models.ForeignKey(Invoice, related_name='ste_parent', on_delete=models.CASCADE, null=True)

    class Meta:
        ordering = ['-code']

    def __str__(self):
        return self.code

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        if not self.id:
            if self.type == 'transfer':
                self.code = NS.get_series(serie='TSTE')
            if self.type == 'issue':
                self.code = NS.get_series(serie='ISTE')
            if self.type == 'delivery':
                self.code = NS.get_series(serie='DSTE')
            if self.type == 'receipt':
                self.code = NS.get_series(serie='RSTE')

        # FIXME dont calculate total fom post_save
        # when ste create from invoice only from save method

        else:
            total = 0
            lines = StockEntryLine.objects.filter(parent=self.id)
            for line in lines:
                total += line.total
            self.total = total
        return super().save(force_insert, force_update, using, update_fields)

    @classmethod
    def submit_stock_entry(cls, id=None):
        if id:
            ste = cls.objects.get(pk=id)
            if StockEntryLine.submit_stock_lines(ste):
                ste.status = 'submitted'
                ste.save()

    @classmethod
    def cancel_stock_entry(cls, id=None):
        if id:
            ste = cls.objects.get(pk=id)
            ste.status = 'cancelled'
            ste.save()

    @classmethod
    def calculate_total(cls, id=None):
        if id:
            total = 0
            lines = StockEntryLine.objects.filter(parent=id)
            for line in lines:
                total += line.total
            ste = cls.objects.get(pk=id)
            ste.total = total
            ste.save()

class StockEntryLine(models.Model):
    item = models.ForeignKey(Product, related_name='str_line_item', on_delete=models.SET_NULL, null=True)
    parent = models.ForeignKey(StockEntry, related_name='ste_line_parent', on_delete=models.SET_NULL, null=True)
    qty = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
    price = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
    status = models.CharField(choices=ENTRY_STATUS, default='draft', null=True, max_length=10)
    total = models.DecimalField(default=0.0, decimal_places=2, max_digits=10)
    invo_line = models.ForeignKey(InvoiceLine, related_name='invo_line_parent', on_delete=models.SET_NULL, null=True, blank=True)

    def save(self, force_insert=False, force_update=False, using=None, update_fields=None):
        self.total = self.qty * self.price
        return super().save(force_insert, force_update, using, update_fields)

    @classmethod
    def submit_stock_lines(cls, parent=None):
        lines = cls.objects.all().filter(parent=parent.id)
        if lines:
            for line in lines:
                if line.item.type == 'stockable':
                    if line.invo_line:
                        line.invo_line.qty_stock = line.invo_line.qty_stock - line.qty
                        line.invo_line.save()
                        line.invo_line.update_qty_status(line.invo_line)
                    if parent.type == 'receipt':
                        if line.item.update_product_stock(line, type='in'):
                            line.status = 'submitted'
                            line.save()
                    if parent.type == 'delivery':
                        if line.item.update_product_stock(line, type='out'):
                            line.status = 'submitted'
                            line.save()
            return True
        else:
            return False

    @classmethod
    def cancel_stock_lines(cls, parent=None):
        lines = cls.objects.all().filter(parent=parent)
        if lines:
            for line in lines:
                line.status = 'cancelled'
                line.save()
            return True
        else:
            return False

@receiver(post_save, sender=StockEntryLine)
def save_calculate_ste_lines(sender, instance, **kwargs):
    # FIXME dont calculate when create from invoice
    instance.parent.calculate_total(instance.parent.id)

@receiver(post_delete, sender=StockEntryLine)
def delete_calculate_ste_lines(sender, instance, **kwargs):
    instance.parent.calculate_total(instance.parent.id)


from ..runner import Sequential
from ..utils import Registry, build_from_cfg

def build_model_from_cfg(cfg, registry, default_args=None):
    """Build a PyTorch model from config dict(s). Different from
    ``build_from_cfg``, if cfg is a list, a ``nn.Sequential`` will be built.

    Args:
        cfg (dict, list[dict]): The config of modules, is is either a config
            dict or a list of config dicts. If cfg is a list, a
            the built modules will be wrapped with ``nn.Sequential``.
        registry (:obj:`Registry`): A registry the module belongs to.
        default_args (dict, optional): Default arguments to build the module.
            Defaults to None.

    Returns:
        nn.Module: A built nn module.
    """
    if isinstance(cfg, list):
        modules = [
            build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg
        ]
        return Sequential(*modules)
    else:
        return build_from_cfg(cfg, registry, default_args)

MODELS = Registry('model', build_func=build_model_from_cfg)


import requests

# ------------ CONS ------------- #

MY_LAT = 54.372158
MY_LONG = 18.638306

# ------------ API connection --------------- #

parameters = {
    "lat": MY_LAT,
    "lng": MY_LONG,
}

response = requests.get(url="https://api.sunrise-sunset.org/json", params=parameters, verify=False)
response.raise_for_status()
data = response.json()

# ------------- Show Results -------------- #

print(f"Sunrise: {data['results']['sunrise']}")
print(f"Sunset: {data['results']['sunset']}")
print(f"Day Length: {data['results']['day_length']}")


"""
ASV benchmarks for irradiance.py
"""

import pandas as pd
import pvlib
from packaging.version import Version
from functools import partial

def set_weather_data(obj):
    obj.times = pd.date_range(start='20180601', freq='1min',
                              periods=14400)
    obj.poa = pd.Series(1000, index=obj.times)
    obj.tamb = pd.Series(20, index=obj.times)
    obj.wind_speed = pd.Series(2, index=obj.times)

class SAPM:

    def setup(self):
        set_weather_data(self)
        if Version(pvlib.__version__) >= Version('0.7.0'):
            kwargs = pvlib.temperature.TEMPERATURE_MODEL_PARAMETERS['sapm']
            kwargs = kwargs['open_rack_glass_glass']
            self.sapm_cell_wrapper = partial(pvlib.temperature.sapm_cell,
                                             **kwargs)
        else:
            sapm_celltemp = pvlib.pvsystem.sapm_celltemp

            def sapm_cell_wrapper(poa_global, temp_air, wind_speed):
                # just swap order; model params are provided by default
                return sapm_celltemp(poa_global, wind_speed, temp_air)
            self.sapm_cell_wrapper = sapm_cell_wrapper

    def time_sapm_cell(self):
        # use version-appropriate wrapper
        self.sapm_cell_wrapper(self.poa, self.tamb, self.wind_speed)

class Fuentes:

    def setup(self):
        if Version(pvlib.__version__) < Version('0.8.0'):
            raise NotImplementedError

        set_weather_data(self)

    def time_fuentes(self):
        pvlib.temperature.fuentes(self.poa, self.tamb, self.wind_speed,
                                  noct_installed=45)


import dataclasses
import numpy as np

from foundations import hparams
import models.base
from pruning import base
from pruning.mask import Mask

@dataclasses.dataclass
class PruningHparams(hparams.PruningHparams):
    pruning_fraction: float = 0.2
    pruning_layers_to_ignore: str = None

    _name = 'Hyperparameters for Sparse Global Pruning'
    _description = 'Hyperparameters that modify the way pruning occurs.'
    _pruning_fraction = 'The fraction of additional weights to prune from the network.'
    _layers_to_ignore = 'A comma-separated list of addititonal tensors that should not be pruned.'

class Strategy(base.Strategy):
    @staticmethod
    def get_pruning_hparams() -> type:
        return PruningHparams

    @staticmethod
    def prune(pruning_hparams: PruningHparams, trained_model: models.base.Model, current_mask: Mask = None):
        current_mask = Mask.ones_like(trained_model).numpy() if current_mask is None else current_mask.numpy()

        # Determine the number of weights that need to be pruned.
        number_of_remaining_weights = np.sum([np.sum(v) for v in current_mask.values()])
        number_of_weights_to_prune = np.ceil(
            pruning_hparams.pruning_fraction * number_of_remaining_weights).astype(int)

        # Determine which layers can be pruned.
        prunable_tensors = set(trained_model.prunable_layer_names)
        if pruning_hparams.pruning_layers_to_ignore:
            prunable_tensors -= set(pruning_hparams.pruning_layers_to_ignore.split(','))

        # Get the model weights.
        weights = {k: v.clone().cpu().detach().numpy()
                   for k, v in trained_model.state_dict().items()
                   if k in prunable_tensors}

        # Create a vector of all the unpruned weights in the model.
        weight_vector = np.concatenate([v[current_mask[k] == 1] for k, v in weights.items()])
        threshold = np.sort(np.abs(weight_vector))[number_of_weights_to_prune]

        new_mask = Mask({k: np.where(np.abs(v) > threshold, current_mask[k], np.zeros_like(v))
                         for k, v in weights.items()})
        for k in current_mask:
            if k not in new_mask:
                new_mask[k] = current_mask[k]

        return new_mask


import time
import datetime
import random
import json
from django.shortcuts import render
from api.models import temp_log
from api.serializers import tempSerializer
import datetime

date = '2022-03-28T22:40:41.730193+05:30'
date = date.split(".")[0]
datem = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
print(datem.day)

def index(request):
    context = {}
    context['temp'] = 0
    context['humidity'] = 0
    return render(request, 'home.html', context)

def linechart(request):
    """
    lineChart page
    """

    context = {}
    queryset = temp_log.objects.all().order_by('-id')
    ser = tempSerializer(queryset, many=True)
    first = ser.data[0]["time"]
    date = first.split(".")[0]
    f_date = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S').day

    arr = [['Time', 'Temperature']]

    for i in ser.data:
        date = i["time"].split(".")[0]
        datem = datetime.datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
        if f_date == datem.day:
            time = str(datem.hour) + ":"+str(datem.minute)
            arr.append([time, i["temp"]])

    context['temp'] = json.dumps(arr)
    print(context['temp'])

    return render(request, 'linechart.html', context)


"""
Implements Tokenizer() class for preprocessing and tokenizing raw-text and
Language() class for storing metrics about the language used.
"""

import re
import pickle
from tqdm import tqdm
from numpy import log, mean
from unidecode import unidecode
from collections import Counter
from flashtext import KeywordProcessor

import utils as utils

class Tokenizer(object):
    """ Stores all methods for working with text """
    # base methods
    def __init__(self, lower=True):
        assert isinstance(lower, bool), ('lower expected type bool, but found '
                                        f'type {type(lower)}.')
        self.lower = lower
        # low level attributes to be abstracted
        self.vocabSize  =   0
        self.freqDict   =   None
        self.idx        =   None
        self.reverseIdx =   None
        self.tokenizer  =   KeywordProcessor()
        # high level attribute to indicate initialization
        self.initialized = False
        # matches non-alphanumeric, space, or sentence-ending punctuation
        self.STRIP = re.compile(r'[^0-9a-zA-Z\t\n\s_.?!:;/<>*&^%$#@()"~`+-]')
        # matches sequence of tabs, newlines, spaces, underscores, and dashes
        self.SPACE = re.compile(r'[\t\n\s_.?!:;/<>*&^%$#@()"~`+-]+')

    def __str__(self):
        return (f'<Tokenizer Object: VOCAB_SIZE={self.vocabSize} | ' \
                f'LOWER={self.lower}>')

    # save/load methods
    def save(self, path):
        """ Saves Tokenizer() to folder at path """
        assert isinstance(path, str), ('path expected type str, but found '\
                                        f'type {type(path)}.')
        assert self.initialized, 'Tokenizer must be initialized before saving.'
        utils.safe_make_folder(path)
        # save neccessary attributes for lossless reconstruction
        utils.save_obj(self.freqDict, f'{path}/freqDict')
        utils.save_obj(self.idx, f'{path}/idx')
        utils.save_obj(self.tokenizer, f'{path}/tokenizer')
        with open(f'{path}/lower.sav', 'w+') as lowerFile:
            lowerStr = 't' if self.lower else 'f'
            lowerFile.write(lowerStr)
        return True

    def load(self, path):
        """ Loads Tokenizer() from folder at path """
        utils.path_exists(path)
        assert not self.initialized, ("Tokenizer file can't be loaded into an "\
                                        "initialized Tokenizer.")

        # load pickled objects
        self.freqDict = utils.load_obj(f'{path}/freqDict')
        self.idx = utils.load_obj(f'{path}/idx')
        self.tokenizer = utils.load_obj(f'{path}/tokenizer')
        # load lower bool
        with open(f'{path}/lower.sav', 'r') as lowerFile:
            lowerStr = lowerFile.read()
        self.lower = True if (lowerStr=='t') else False
        # extrapolate from loaded objects
        self.build_reverse_idx()
        self.vocabSize = len(self.freqDict)
        self.initialized = True
        return True

    # common file iterators
    def wiki_iterator(self, path='data/inData/wikiArticles.csv'):
        """ Iterates over wiki csv, yielding raw article text """
        with open(path, 'r') as wikiFile:
            for line in wikiFile:
                commaLoc = line.find(',')
                articleText = line[commaLoc+3:-3]
                yield articleText

    # methods for preprocessing text
    def to_lower(self, rawString):
        """ Lowercases string """
        return rawString.lower()

    def clean(self, rawString):
        """
        Cleans rawString by replacing spaceMatcher and tagMatcher with a single
        space, removing non-alpha chars, and lowercasing alpha chars
        """
        unicodedString = unidecode(rawString)
        # replace STRIP Matcher with ""
        cleanedString = re.sub(self.STRIP, "", rawString)
        # replace SPACE Matcher with " " and strip surround whitespace
        spacedString = re.sub(self.SPACE, " ", cleanedString)
        # lowercase the alpha chars that remain
        if self.lower:
            return self.to_lower(spacedString)
        else:
            return spacedString

    # methods for gathering language data
    def calc_tf_idf(self, termFreq, docFreq):
        """ Calcs scalar token freq score from tf and idf """
        # DEBUG: TF IDF SEEMS OFF
        return 1 + log(termFreq * docFreq)

    def freq_dict_from_file_iterator(self, iterator):
        """ Builds freq dict from file iterator. Updates vocabSize """
        # initialize counter to map tokens to raw number of occurences
        tokenCounts = Counter()
        # initialize counter to map tokens to number of docs they appear in
        tokenAppearances = Counter()
        # initialize variable to count total number of words used
        totalLength = 0
        # get base count of article num for tqdm
        articleCount = len([None for _ in iterator()])
        # iterate over wiki file
        for i, text in enumerate(tqdm(iterator(), total=articleCount)):
            # find tokens in text
            cleanText = self.clean(text)
            tokenList = cleanText.split()
            # count number of times each token appears
            currentCounts = Counter(tokenList)
            # get total length of text
            wordCount = len(tokenList)
            # add tokens counts to tokenCounts counter
            tokenCounts.update(currentCounts)
            # add single appearance for each token found
            tokenAppearances.update(set(currentCounts))
            # add number of words in current file to totalLength
            totalLength += wordCount
        # lambdas for calculating termFreq and docFreq
        calc_termFreq = lambda tokenCount : tokenCount / totalLength
        calc_docFreq = lambda tokenAppearance : log(float(i) / tokenAppearance)
        # use total num to norm tokenCounts and find frequency for each token
        freqDict = {token : self.calc_tf_idf(calc_termFreq(rawCount),
                            calc_docFreq(tokenAppearances[token]))
                    for token, rawCount in tokenCounts.items()}
        meanScore = mean([elt for elt in freqDict.values()])
        self.freqDict = freqDict
        self.vocabSize = len(freqDict)
        return True

    def filter_freq_dict(self, minFreq=0, maxFreq=1, tokenNum=50000):
        """
        Filters freq dict to tokenNum tokens between min and maxFreq. Updates
        vocab size in conjunction.
        """
        assert (tokenNum > 0), f'Filtering to tokenNum={tokenNum} would result'\
                                'in empty freqDict.'
        qualifies = lambda freq : (maxFreq > freq > minFreq)
        filteredFreqDict = {token : freq
                            for i, (token, freq)
                            in enumerate(self.freqDict.items())
                            if (qualifies(freq) and (i < tokenNum))}
        assert (filteredFreqDict != dict()), ('Filtering removed all elements '\
                                            'from freqDict. Try chaning min '\
                                            'or max frequency parameters.')
        self.freqDict = filteredFreqDict
        self.vocabSize = len(filteredFreqDict)
        return True

    # methods for tokenizer modification
    def build_tokenizer(self):
        """ Builds flashtext tokenizer from freq dict """
        assert (self.freqDict), f'freqDict must be built before tokenizer.'
        self.tokenizer.add_keywords_from_list(list(self.freqDict.keys()))
        return True

    # methods for idx modification
    def build_idx(self):
        assert (self.freqDict), f'freqDict must be built before idx.'
        self.idx = {word : i for i, word in enumerate(self.freqDict)}

    def build_reverse_idx(self):
        assert (self.idx), f'idx must be built before reverse idx.'
        self.reverseIdx = {i : word for word, i in self.idx.items()}

    # higher level initialization methods
    def language_from_wiki_file(self, minFreq, maxFreq, tokenNum):
        """
        Builds freqDict, vocabSize, tokenizer, idx, and reverse idx from wiki
        file. Takes tokenNum tokens between minFreq and maxFreq.
        """
        self.freq_dict_from_file_iterator(iterator=self.wiki_iterator)
        self.filter_freq_dict(minFreq, maxFreq, tokenNum)
        self.build_tokenizer()
        self.build_idx()
        self.build_reverse_idx()
        self.initialized = True
        return True

    # tokenization methods
    def clean_and_tokenize(self, text):
        """ Returns Counter() of recognized tokens in raw text """
        return Counter(self.tokenizer.extract_keywords(self.clean(text)))

    # mechanical token ranking in text
    def score_single_token(self, token, observedFreq):
        """ Scores a single token in text according to observed tf """
        freqDiff = self.calc_tf_idf(observedFreq, 1) - self.freqDict[token]
        if freqDiff <= 0:
            return None
        return round(freqDiff, 4)

    def single_mechanically_score_tokens(self, text):
        """
        Ranks token scores in text using freqDict and assuming no subtokens
        and converts token names to idx number
        """
        tokenCounts = self.clean_and_tokenize(text)
        wordNum = len(text.split())
        tokenScores =  {token : self.score_single_token(token,
                                                        (count/wordNum))
                        for token, count in tokenCounts.items()}
        return {self.idx[token] : score for token, score in tokenScores.items()
                if ((score != None) and (token in self.idx))}

    # def KNOWLEDGE_mechanically_score_tokens(self, text, maxChunkSize=5):
    #     """ Ranks tokenes according to freqDict and observed freq in text """
    #     # find counter of greedy tokens in text
    #     greedyTokens = self.clean_and_tokenize(test)
    #     subTokens = Counter()
    #     # iterate over greedy counter
    #     for greedyToken, greedyCount in greedyTokens.items():
    #         greedyWords = greedyToken.split()
    #         wordNum = len(greedyWords)
    #         # if multiple words in cur topToken, recursively look for sub tokens
    #         if wordNum > 1:
    #             # init chunk is 1 smaller than wordNum but capped at maxChunkSize
    #             chunkSize = min(maxChunkSize, (wordNum - 1))
    #             # iterate over greedy tokens, analyzing smaller chunks at a time
    #             while chunkSize > 0:
    #                 for i in range(wordNum):
    #                     chunkWords = greedyWords[i : i+chunkSize]
    #                     textChunk = ' '.join(chunkWords)
    #                     # if the chunk is a token in the tokenizer,
    #                     # add its count tokenCounts after norming by fraction
    #                     # of larger token and multiplying by larger token's count
    #                     # (becuase you're iterating over a set)
    #                     if textChunk in knowledgeProcessor:
    #                         subTokens.update({textChunk :
    #                                             (greedyCount * (len(chunkWords)
    #                                                             / wordNum))})
    #                 chunkSize -= 1
    #     greedyTokens.update(subTokens)
    #     return greedyTokens


from constants.constantsGeneral import ConstantsGeneral
from data.builder import builderCartData, builderEwallet
from data.dataGenerator import DataGenerator
from service.serviceNicepay import ServiceNicepay

class testEwallet:
    amt = 100
    itemCartData = {
        "img_url": "https://cdn.eraspace.com/pub/media/catalog/product/i/p/iphone_13_pro_max_silver_1_5.jpg",
        "goods_name": "iPhone13ProMax",
        "goods_detail": "1TB-White",
        "goods_amt": amt,
        "goods_quantity": "1"
    }

    bodyCartData = (
        builderCartData.BuildCartData()
        .setCount("1")
        .setItem(itemCartData)
        .build()
    )

    bodyEwallet = (
        builderEwallet.BuildEwallet()
        .setPayMethod(ConstantsGeneral.getPayMethodEWallet())
        .setMitraCd("DANA")
        .setUserIp(ConstantsGeneral.getUserIp())
        .setAmt(amt)
        .build()
    )

    response = ServiceNicepay.serviceRequest(DataGenerator.getTransactionBody(bodyEwallet.jsonEwallet(),
                                                                              bodyCartData.jsonCartData()))


#!/usr/bin/env python
# encoding: utf-8
'''
@author: mj
@contact: major3428@foxmail.com
@software: pycharm
@file: paint500v2_U_THDu.py
@time: 2018-12-26 上午 9:33
@desc:
'''

import matplotlib as mpl
import pandas as pd
from pandas import DataFrame
import numpy as np
from fbprophet import Prophet
import matplotlib.pyplot as plt
from sklearn import preprocessing
import matplotlib.mlab as mlab#拟合模块
from scipy import stats
from scipy import optimize

#读取
df = pd.read_csv('./data/huabang2_Nov.csv',index_col=0)
#index_col=0去掉索引
#更名
df.rename(columns={'采集时间': 'ds', 'A相电流': 'Ia', 'B相电流': 'Ib', 'C相电流': 'Ic',
                   'A相电流THD': 'Ia_THD', 'B相电流THD': 'Ib_THD', 'C相电流THD': 'Ic_THD',
                   'A相电压': 'Ua', 'B相电压': 'Ub', 'C相电压': 'Uc',
                   'A相电压THD': 'Ua_THD', 'B相电压THD': 'Ub_THD', 'C相电压THD': 'Uc_THD',
                   '总功率因数': 'PF', '三相无功功率': 'Q', '三相有功功率': 'P',
                   'A相电流H3': 'Ia_H3', 'A相电流H5': 'Ia_H5', 'A相电流H7': 'Ia_H7',
                   'A相电流H9': 'Ia_H9', 'A相电流H11': 'Ia_H11', 'A相电流H13': 'Ia_H13',
                   'A相电流H15': 'Ia_H15', 'A相电流H17': 'Ia_H17', 'A相电流H19': 'Ia_H19',
                   'A相电流H21': 'Ia_H21',
                   'B相电流H3': 'Ib_H3', 'B相电流H5': 'Ib_H5', 'B相电流H7': 'Ib_H7',
                   'B相电流H9': 'Ib_H9', 'B相电流H11': 'Ib_H11', 'B相电流H13': 'Ib_H13',
                   'B相电流H15': 'Ib_H15', 'B相电流H17': 'Ib_H17', 'B相电流H19': 'Ib_H19',
                   'B相电流H21': 'Ib_H21',
                   'C相电流H3': 'Ic_H3', 'C相电流H5': 'Ic_H5', 'C相电流H7': 'Ic_H7',
                   'C相电流H9': 'Ic_H9', 'C相电流H11': 'Ic_H11', 'C相电流H13': 'Ic_H13',
                   'C相电流H15': 'Ic_H15', 'C相电流H17': 'Ic_H17', 'C相电流H19': 'Ic_H19',
                   'C相电流H21': 'Ic_H21'
                   },inplace = True)

print(np.max([df.Ua_THD, df.Ub_THD, df.Uc_THD], axis=1))
mu = np.mean([df.Ua, df.Ub, df.Uc,
              df.Ua_THD, df.Ub_THD, df.Uc_THD], axis=1)
#mu = np.mean(df.Ua)
sigma = np.std([df.Ua, df.Ub, df.Uc,
                df.Ua_THD, df.Ub_THD, df.Uc_THD], axis=1)
freq = len(df.ds) #总样本频次
#print(freq)

#设置字体
mpl.rcParams['font.sans-serif'] = ['Microsoft Yahei']
mpl.rcParams['font.family']='sans-serif'#解决负号'-'显示为方块的问题
mpl.rcParams['axes.unicode_minus'] = False

#配置参数
gama = 0.90
beta = 0.85
QR = np.array([len(df.query('Ua_THD < 5')),len(df.query('Ub_THD < 5')),len(df.query('Uc_THD < 5'))])/freq
UR = 1-QR
qur = np.array([len(df.query('Ua < 235')), len(df.query('Ub < 235')), len(df.query('Uc < 235'))])/freq
unr = 1 - qur
values = [[qur[0], unr[0]], [qur[1], unr[1]], [qur[2], unr[2]]]
UTHDQR = np.array([len(df.query('Ua_THD < 5')), len(df.query('Ub_THD < 5')), len(df.query('Uc_THD < 5'))])/freq
UTHDUR = 1 - UTHDQR
vals = [[UTHDQR[0], UTHDUR[0]], [UTHDQR[1], UTHDUR[1]], [UTHDQR[2], UTHDUR[2]]]

#配置颜色
color_kinds = [['orange', 'goldenrod', ['khaki', 'goldenrod'], 'darkorange'],
               ['mediumseagreen', 'green', ['mediumseagreen', 'green'], 'lightseagreen'],
               ['orangered', 'red', ['lightcoral', 'orangered']], 'lightcoral']

thd_color = [['orange', 'darkorange', ['khaki', 'goldenrod']],
             ['mediumseagreen', 'lightseagreen', ['mediumseagreen', 'green']],
             ['orangered', 'lightcoral', ['lightcoral', 'orangered']]]

#面向对象
class Paint:
    def __init__(self):
        self.pos = 'upper left'

    def paintU(self, electricity, phase, num):
        '''
        画电压
        :param electricity: 电压数组
        :param phase: 相
        :param num: A:0 B:1 C:2
        :return:
        '''
        # 画布大小 长 宽 dpi分辨率 facecolor背景颜色
        fig1 = plt.figure(figsize=[10.5, 3.55], dpi=150, facecolor='gainsboro')
        plt.subplot2grid((1, 3), (0, 0), colspan=2)
        n_u, bins_u, patches_u = plt.hist(electricity, bins=17, alpha=0.6, rwidth=0.9, color=color_kinds[num][0],
                                          label='概率', density=False)

        # 显示数字标签 ha代表对齐方式
        for a, b in zip(bins_u, n_u):
            plt.text(a, b + 2, '%.1f%%' % (b / freq * 100), ha='left', va='bottom', fontsize=6)
        # 拟合U
        y_u = stats.norm.pdf(bins_u, mu[num], sigma[num])

        alpha = np.max(n_u, axis=0) / np.max(y_u, axis=0) * beta

        norm_u = plt.plot(bins_u, y_u * alpha, color=color_kinds[num][1], linestyle='--', label='正态曲线')
        # 画参考线
        if np.max(electricity, axis=0) > 235:
            plt.vlines(235, 0, np.max(n_u, axis=0) * gama, colors=color_kinds[num][3], linestyles='-.', label='国标上限 235V')

        plt.legend(fontsize=6, loc=self.pos)
        plt.xlabel(phase+'相电压(单位:V)', fontsize=9)
        plt.ylabel('发生次数', fontsize=9)
        #plt.title(phase+'相电压', fontsize=10)

        plt.subplot2grid((1, 3), (0, 2))
        wedges1, texts1, autotexts1 = plt.pie(values[num], autopct="%.1f%%", radius=1, pctdistance=0.85,
                                              colors=color_kinds[num][2], textprops={'color': 'w'},
                                              startangle=90, wedgeprops={'width': 0.3, 'edgecolor': 'w'},
                                              )
        plt.legend(wedges1, labels=['合格率 %.1f%%' % (qur[num] * 100), '不合格率 %.1f%%' % (unr[num] * 100)], fontsize=7,
                   title=phase+'相电压', loc='center')
        fig1.set_tight_layout(True)
        plt.show()

    def paintTHDu(self, electricity, phase, num):
        fig2 = plt.figure(figsize=[10.5, 3.55], dpi=150, facecolor='gainsboro')
        plt.subplot2grid((1, 3), (0, 0), colspan=2)
        n_thdu, bins_thdu, patches_thdu = plt.hist(electricity, bins=17, alpha=0.6, rwidth=0.9, color=thd_color[num][0],
                                                      label='概率', density=False)
        print(n_thdu)  # 检查

        # 显示数字标签 ha代表对齐方式
        for a, b in zip(bins_thdu, n_thdu):
            plt.text(a, b + 2, '%.1f%%' % (b / freq * 100), ha='left', va='bottom', fontsize=6)

        if np.max(electricity, axis=0) > 5:
            plt.vlines(5, 0, np.max(n_thdu, axis=0), colors=thd_color[num][1], linestyles='-.', label='上限 5%')

        plt.legend(fontsize=6, loc=self.pos)
        plt.xlabel(phase+'相电压THD(单位:%)', fontsize=9)
        plt.ylabel('发生次数', fontsize=9)

        plt.subplot2grid((1, 3), (0, 2))
        wedges2, texts2, autotexts2 = plt.pie(vals[num], autopct="%.1f%%", radius=1, pctdistance=0.85,
                                              colors=thd_color[num][2], textprops={'color': 'w'},
                                              startangle=90, wedgeprops={'width': 0.3, 'edgecolor': 'w'}
                                              )
        plt.legend(wedges2, labels=['合格率 %.1f%%' % (UTHDQR[num] * 100), '不合格率 %.1f%%' % (UTHDUR[num] * 100)], fontsize=7,
                   title=phase+'相电压谐波含有率', loc='center')
        #plt.title(phase+'相谐波电压含有率合格率', fontsize=9)
        fig2.set_tight_layout(True)
        plt.show()
p = Paint()
#p.paintU(df.Ub, 'B', 1)
#p.paintTHDu(df.Ub_THD, 'B', 1)


from pyspark.sql import SparkSession

if __name__ == "__main__":

    spark = SparkSession\
        .builder\
        .master("local[*]") \
        .appName("retail") \
        .getOrCreate()

    df = spark.read \
        .option("header", "true") \
        .csv("./data/online-retail-dataset.csv") \
        .repartition(2)

    result = df.selectExpr("instr(Description, 'GLASS') >= 1 as is_glass") \
        .groupBy("is_glass") \
        .count() \
        .collect()

    print(result)
    # sit here so you can look at the SparkUI
    # localhost:4040
    ip = input("Press return to end: ")

    spark.stop()


#!/usr/bin/env python3

import json
from copy import deepcopy

import pytest

from vyper.cli.vyper_json import _parse_args
from vyper.exceptions import JSONError

FOO_CODE = """
import contracts.bar as Bar

@external
def foo(a: address) -> bool:
    return Bar(a).bar(1)
"""

BAR_CODE = """
@external
def bar(a: uint256) -> bool:
    return True
"""

BAR_ABI = [
    {
        "name": "bar",
        "outputs": [{"type": "bool", "name": "out"}],
        "inputs": [{"type": "uint256", "name": "a"}],
        "stateMutability": "nonpayable",
        "type": "function",
    }
]

INPUT_JSON = {
    "language": "Vyper",
    "sources": {
        "contracts/foo.vy": {"content": FOO_CODE},
        "contracts/bar.vy": {"content": BAR_CODE},
    },
    "interfaces": {"contracts/ibar.json": {"abi": BAR_ABI}},
    "settings": {"outputSelection": {"*": ["*"]}},
}

def _no_errors(output_json):
    return "errors" not in output_json or not any(
        err["severity"] == "error" for err in output_json["errors"]
    )

def test_to_stdout(tmp_path, capfd):
    path = tmp_path.joinpath("input.json")
    with path.open("w") as fp:
        json.dump(INPUT_JSON, fp)
    _parse_args([path.absolute().as_posix()])
    out, _ = capfd.readouterr()
    output_json = json.loads(out)
    assert _no_errors(output_json), (INPUT_JSON, output_json)
    assert "contracts/foo.vy" in output_json["sources"]
    assert "contracts/bar.vy" in output_json["sources"]

def test_to_file(tmp_path):
    path = tmp_path.joinpath("input.json")
    with path.open("w") as fp:
        json.dump(INPUT_JSON, fp)
    output_path = tmp_path.joinpath("output.json")
    _parse_args([path.absolute().as_posix(), "-o", output_path.absolute().as_posix()])
    assert output_path.exists()
    with output_path.open() as fp:
        output_json = json.load(fp)
    assert _no_errors(output_json), (INPUT_JSON, output_json)
    assert "contracts/foo.vy" in output_json["sources"]
    assert "contracts/bar.vy" in output_json["sources"]

def test_pretty_json(tmp_path, capfd):
    path = tmp_path.joinpath("input.json")
    with path.open("w") as fp:
        json.dump(INPUT_JSON, fp)
    _parse_args([path.absolute().as_posix()])
    out1, _ = capfd.readouterr()
    _parse_args([path.absolute().as_posix(), "--pretty-json"])
    out2, _ = capfd.readouterr()
    assert len(out2) > len(out1)
    assert json.loads(out1) == json.loads(out2)

def test_traceback(tmp_path, capfd):
    path = tmp_path.joinpath("input.json")
    input_json = deepcopy(INPUT_JSON)
    del input_json["sources"]
    with path.open("w") as fp:
        json.dump(input_json, fp)
    _parse_args([path.absolute().as_posix()])
    out, _ = capfd.readouterr()
    output_json = json.loads(out)
    assert not _no_errors(output_json)
    with pytest.raises(JSONError):
        _parse_args([path.absolute().as_posix(), "--traceback"])


import dateparser
from PIL import Image
from django.test import TestCase
from numpy import asarray
from cameratrap.OCRUtils import OCRUtils

class OCRUtilTests(TestCase):

    def test_ocr_bushnell(self):
        img = Image.open(r'./cameratrap/testdata/bushnell.jpg')
        img_as_numpy = asarray(img)
        ocr = OCRUtils(image=img_as_numpy)
        dt = ocr.extractDateTime()
        dt_in_image = dateparser.parse('2022-05-04 16:08:36+00:00')
        self.assertEqual(dt.date(), dt_in_image.date())
        self.assertEqual(dt.time(), dt_in_image.time())
        print(dt)

    def test_ocr_deer(self):
        img = Image.open(r'./cameratrap/testdata/deer.jpg')
        img_as_numpy = asarray(img)
        ocr = OCRUtils(image=img_as_numpy)
        dt = ocr.extractDateTime()
        dt_in_image = dateparser.parse('2022-03-04 07:18:00+00:00')
        self.assertEqual(dt.date(), dt_in_image.date())
        self.assertEqual(dt.time(), dt_in_image.time())
        print(dt)


# -*- encoding: utf-8 -*-

##
from matplotlib import pyplot as plt
import matplotlib.style as mplstyle
import os

# 设置快速样式,加快绘图速度.并确保fast在最后
# https://blog.csdn.net/jeffery0207/article/details/81317000
mplstyle.use(['seaborn-deep', 'fast'])
# matplotlib绘图风格,参考下面链接. seaborn-deep效果不错
# https://blog.csdn.net/justisme/article/details/100543869

# 解决标签无法显示中文的问题
plt.rcParams['font.sans-serif'] = ['Helvetica']         # matlab字体
# plt.rcParams['font.sans-serif'] = ['Microsoft YaHei']   # 微软雅黑,可显示中文
plt.rcParams['axes.unicode_minus'] = False              # 用来正常显示负号

# matplotlib 查看所有可设置字体
# import matplotlib
# a = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist])
# print(a)

container = []     # 容器, 用于放置axes

STAGE = 'test'     # 'test' 'show' 'save', 默认'test'

HOLD = False       # holdon 全局设置, 仅使用一次

FIGwidth, FIGheight, FIGdpi = 8, 5.8, 300

# 全局设置
def stage(stage: str, figwidth=FIGwidth, figheight=FIGheight, figdpi=FIGdpi):
    # 设置阶段
    global STAGE, FIGwidth, FIGheight, FIGdpi
    if stage.lower() in ['test', 'show', 'save']:
        STAGE = stage
    else:
        raise ValueError('wrong value of `stage`')
    # 设置图像参数
    FIGwidth, FIGheight, FIGdpi = figwidth, figheight, figdpi
    # 清空容器
    container.clear()

def subplot(*args, stick=True):
    global HOLD; HOLD = True
    plt.subplot(*args, frameon=stick)
    if STAGE.lower() in ['show','save']:
        plt.gcf().set_figwidth(FIGwidth)
        plt.gcf().set_figheight(FIGheight)

def stem(*args, label=None, linewidth=0.5, line='-', marker=' ', holdon=False):
    # holdon和HOLD有一个为True就不新建图窗
    if not (holdon or HOLD): new()
    # 绘图
    if label is None or label == '':
        ax = plt.stem(*args, linefmt=line, markerfmt=marker,  basefmt=' ')
    else:
        ax = plt.stem(*args, linefmt=line, markerfmt=marker,  basefmt=' ', label=label)
        leg = plt.legend(loc='best', frameon=False, ncol=2)
        leg.legendHandles[0].set_linewidth(linewidth)       # 设置legend的线宽
        ax[1].set_linewidth(linewidth)    # 竖线 matplotlib.collections.LineCollection
        # ax[0].set_markersize(linewidth) # 标记 matplotlib.lines.Line2D
    container.append(ax)                  # stem的axes包含3个元素
    # 绘制格网
    plt.grid(b=True, linestyle='-.', linewidth=0.5)

def plot(*args, label=None, linewidth=0.75, holdon=False):
    # holdon和HOLD有一个为True就不新建图窗
    if not (holdon or HOLD): new()
    # 绘图
    if label is None or label == '':
        ax = plt.plot(*args, linewidth=linewidth)
    else:
        ax = plt.plot(*args, linewidth=linewidth, label=label)
        plt.legend(loc='best', frameon=False, ncol=2)
    container.extend(ax)
    # 绘制格网
    plt.grid(b=True, linestyle='-.', linewidth=0.5)

def set(xlim=None, ylim=None,
        title=None, titlesize=11.5, label=None,
        xlabel=None, ylabel=None, labelsize=11.5,
        show=False):
    # 如果仅仅用于测试, 则将字体调小
    # 若用于Word文档, 12号的字体很合适
    if STAGE.lower() == 'test':
        titlesize, labelsize = 10, 10
    # limit
    if   xlim is not None: plt.xlim(xlim[0], xlim[1])
    if   ylim is not None: plt.ylim(ylim[0], ylim[1])
    # title
    if  title is not None: plt.title(title,  fontsize=titlesize)
    # legend of current axes
    if  label is not None:
        container[-1].set_label(label)
        plt.gcf().axes[-1].legend(loc='best', frameon=False, ncol=2)
    # x/y label
    if xlabel is not None: plt.xlabel(xlabel, fontsize=labelsize)
    if ylabel is not None: plt.ylabel(ylabel, fontsize=labelsize)
    # show
    if show: showing()

# 仅设置axes.lines里的legend
def legend_lines(labels=None, show=False):
    lines_gca = plt.gca().lines
    for i, l in enumerate(lines_gca):
        if labels[i] == '': continue
        l.set_label(labels[i])
    plt.legend(loc='best', frameon=False, ncol=2)
    # show
    if show: showing()

# 设置container里每一个axes的Legend
def legend(labels=None, show=False):
    # 如果lables是str类型,将其转为list
    if isinstance(labels, str): labels = [labels]
    # 逐一设置label
    for i, c in enumerate(container):
        if labels[i] == '': continue
        c.set_label(labels[i])
    # 对于每个子图，都要调用.legend()方法
    for ax in plt.gcf().axes:
        ax.legend(loc='best', frameon=False, ncol=2)
    # show
    if show: showing()

def new():  # 清空容器, 创建新fig
    global HOLD; HOLD = False
    container.clear()
    # 创建图窗
    plt.figure()
    if STAGE.lower() in ['show','save']:
        plt.gcf().set_figwidth(FIGwidth)
        plt.gcf().set_figheight(FIGheight)
        if STAGE.lower() == 'save':
            plt.gcf().set_figdpi(FIGdpi)

def save(filename='tmp.svg', path=None):
    # 获取存储路径
    if path is None: path = os.getcwd() + '/'
    # 判断路径的末尾是否存在'/'或'\\',若不存在则添加'/'
    if path[-1] != '/' and path[-2:] != '\\':
        path = path + '/'
    # save 背景是透明的
    if STAGE.lower() == 'save':
        mplstyle.use(['seaborn-deep'])            # 取消fast
        plt.savefig( path + filename, transparent=True, dpi=FIGdpi)
        mplstyle.use(['seaborn-deep', 'fast'])    # fast仅用于展示
    else:
        plt.savefig( path + filename, transparent=True)

def showing():  # 清空容器, 展示fig
    container.clear()
    # if backen is not None:
    #     plt.switch_backend(backen)
    # else:
    #     backen = 'TkAgg'
    # # 判断启用的是否是webagg
    # if backen.lower() == 'webagg':
    #     import nest_asyncio
    #     nest_asyncio.apply()
    # show完不需关闭，程序自动向下执行
    plt.ion()
    # show完不需关闭，程序自动向下执行
    plt.show()


from rest_framework import status
from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet
from rest_framework.exceptions import APIException
from rest_framework.response import Response
from .serializers import DeliverySerializer, DeliveryDetailSerializer, DeliveryListSerializer, DeliveryUpdateSerializer
from ..canceled_order.serializers import CanceledOrderSerializer, CanceledOrderDetailSerializer
from ..order.serializers import OrderSerializer, OrderedProductSerializer

class DeliveryViewSet(ModelViewSet):
    queryset = DeliverySerializer.Meta.model.objects.all()
    lookup_field = 'order'

    def get_serializer_class(self):
        if self.action == 'list':
            return DeliveryListSerializer
        if self.action == 'retrieve':
            return DeliveryDetailSerializer
        if self.action in ['update', 'partial_update']:
            return DeliveryUpdateSerializer
        return DeliverySerializer

    def create(self, request, *args, **kwargs):
        serializer_class = self.get_serializer_class()
        serializer = serializer_class(data=request.data)
        if serializer.is_valid():
            order = OrderSerializer.Meta.model.objects.filter(id=request.data['order']).first()
            serializer.save()
            order.state = serializer.data['status']
            order.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def update(self, request, pk=None, *args, **kwargs):
        partial = self.kwargs.pop('partial', False)
        instance = self.get_object()
        serializer = self.get_serializer(instance, data=request.data, partial=partial)
        if serializer.is_valid():
            if 'status' in request.data:
                order = instance.order
                order.state = request.data.get('status')
                order.save()
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def partial_update(self, request, *args, **kwargs):
        self.kwargs['partial'] = True
        return self.update(request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        reason = request.data.get('reason', 'No especified')
        instance = self.get_object()
        order = instance.order
        data = {
            'reason': reason,
            'order': order.id
        }
        serializer = CanceledOrderSerializer(data=data)
        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        serializer.save()
        order.state = 'Cancelled'
        order.save()
        ordered = OrderedProductSerializer.Meta.model.objects.filter(order=order)
        for ordered_product in ordered:
            product = ordered_product.product
            product.quantity += ordered_product.quantity
            product.save()
        instance.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


import functions
import main

def functions_test():
    from transformers import BertTokenizer
    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased') # define pre-trained tokenizer
    sentence = "ok this is a TEST you'll have to watch."
    sentence = functions.preprocessW(sentence, 400, tokenizer)
    print(sentence)

if __name__ == "__main__":
    functions_test()


import cv2
import numpy
import numpy as np

import utils
# This is a sample Python script.

########
web_cam = False
image1 = './images/toma11_1.png' ##1200*901
image2 = './images/toma11_2.png' ##1200*901
image3 = './images/toma11_3.png' ##1200*901
#image3 = './images/toma12_2.jpg'
cap = cv2.VideoCapture(0)
cap.set(10, 160)
cap.set(3, 1920)
cap.set(4, 1080)
scale = 1
wP = 1500 * scale # el original fue 210 * scale
hP = 2250 * scale # 297 * scale
########

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

# set the lower and upper bounds for the green hue
lower_green = np.array([50, 100, 50])
upper_green = np.array([70, 255, 255])
lower_red = np.array([160, 100, 50])
upper_red = np.array([180, 255, 255])
lower_yel = np.array([15, 50, 0])
upper_yel = np.array([35, 255, 255])
lower_white = np.array([0, 0, 60])
upper_white = np.array([107, 111, 62])
lower_blue = np.array([100, 150, 0])
upper_blue = np.array([140, 255, 255])

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

def show_image():
    img1 = cv2.imread(image1)
    img2 = cv2.imread(image2)
    img3 = cv2.imread(image3)

    img1 = cv2.resize(img1, (0, 0), None, 0.5, 0.5)
    img2 = cv2.resize(img2, (0, 0), None, 0.5, 0.5)
    img3 = cv2.resize(img3, (0, 0), None, 0.5, 0.5)

    cv2.imshow('Original_1', img1)
    cv2.imshow('Original_2', img2)
    cv2.imshow('Original_3', img3)
    cv2.waitKey(0)

    # convert the BGR image to HSV colour space
    hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)

    ## create a mask for green colour using inRange function
    ymask = cv2.inRange(hsv, lower_yel, upper_yel)
    #rmask = cv2.inRange(hsv, lower_red, upper_red)
    ##mask = cv2.bitwise_or(ymask, rmask)
    mask = ymask

    #cv2.imshow('yell_mask', mask)
    res1 = cv2.bitwise_and(img1, img1, mask=mask)
    #cv2.imshow('Original_yel1', res1)

    hsv = cv2.cvtColor(img1, cv2.COLOR_BGR2HSV)
    ymask = cv2.inRange(hsv, lower_blue, upper_blue)
    #rmask = cv2.inRange(hsv, lower_red, upper_red)
    #mask = cv2.bitwise_or(ymask, rmask)
    mask = ymask
    res2 = cv2.bitwise_or(img1, img1, mask=mask)
    cv2.imshow('Original_blue', res2)
    cv2.waitKey(0)

    hsv = cv2.cvtColor(img3, cv2.COLOR_BGR2HSV)
    ymask = cv2.inRange(hsv, lower_yel, upper_yel)
    rmask = cv2.inRange(hsv, lower_red, upper_red)
    #mask = cv2.bitwise_or(ymask, rmask)
    mask = ymask
    #mask = cv2.inRange(hsv, lower_red, upper_red)
    res3 = cv2.bitwise_and(img3, img3, mask=mask)
    cv2.imshow('Original_yel3', res3)

    utils.getContours(res1, showCanny=True, draw=True)
    cv2.waitKey(0)
    utils.getContours(res2, showCanny=True, draw=True)
    cv2.waitKey(0)
    utils.getContours(res3, showCanny=True, draw=True)
    cv2.waitKey(0)

    cv2.destroyAllWindows()

def originalSizing():
    img = cv2.imread(image3)
    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)

    cv2.imshow('Original', img)

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    ymask = cv2.inRange(hsv, lower_yel, upper_yel)
    wmask = cv2.inRange(hsv, lower_white, upper_white)
    bmask = cv2.inRange(hsv, lower_blue, upper_blue)
    mask = cv2.bitwise_or(ymask, bmask)
    #mask = bmask
    #cv2.imshow('blue&yell_mask', mask)
    res1 = cv2.bitwise_and(img, img, mask=mask)
    cv2.imshow('Original_yel1', res1)
    cv2.waitKey(0)

    imgContours, conts = utils.getContours(res1, minArea=20000, filter=4, showCanny=True)
    if len(conts) != 0:
        biggest = conts[0][2]
        # print(biggest)
        imgWarp = utils.warpImg(img, biggest, wP, hP)
        imgContours2, conts2 = utils.getContours(imgWarp, showCanny=True,
                                                 minArea=2000, filter=4,
                                                 draw=True)
        #cv2.imshow('warp', imgWarp)
        #cv2.imshow('contours2', imgContours2)
        #cv2.waitKey(0)
        if len(conts) != 0:
            for obj in conts2:
                cv2.polylines(imgContours2, [obj[2]], True, (0, 255, 0), 2)
                nPoints = utils.reorder(obj[2])
                nW = round((utils.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale) / 10), 1)
                nH = round((utils.findDis(nPoints[0][0] // scale, nPoints[2][0] // scale) / 10), 1)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]),
                                (nPoints[1][0][0], nPoints[1][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                cv2.arrowedLine(imgContours2, (nPoints[0][0][0], nPoints[0][0][1]),
                                (nPoints[2][0][0], nPoints[2][0][1]),
                                (255, 0, 255), 3, 8, 0, 0.05)
                x, y, w, h = obj[3]
                cv2.putText(imgContours2, '{}cm'.format(nW), (x + 30, y - 10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
                cv2.putText(imgContours2, '{}cm'.format(nH), (x - 70, y + h // 2), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                            (255, 0, 255), 2)
        imgContours2 = cv2.resize(imgContours2, (0, 0), None, 0.5, 0.5)
        cv2.imshow('A4', imgContours2)

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    cv2.imshow('Original', img)
    cv2.waitKey(0)

def filter_image(img, lower_mask, upper_mask):
    if type(img) != numpy.ndarray:
        img = cv2.imread(img)
    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    fmask = cv2.inRange(hsv, lower_mask, upper_mask)
    res1 = cv2.bitwise_and(img, img, mask=fmask)
    return res1

def filter_image_or(img, lower_mask1, upper_mask1, lower_mask2, upper_mask2):
    if type(img) != numpy.ndarray:
        img = cv2.imread(img)

    img = cv2.resize(img, (0, 0), None, 0.5, 0.5)

    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask1 = cv2.inRange(hsv, lower_mask1, upper_mask1)
    mask2 = cv2.inRange(hsv, lower_mask2, upper_mask2)
    ormask = cv2.bitwise_or(mask1, mask2)
    res1 = cv2.bitwise_or(img, img, mask=ormask)
    return res1

def blue_image(image):
    return filter_image(image, lower_blue, upper_blue)

def yellow_image(image):
    return filter_image(image, lower_yel, upper_yel)

def blue_yellow_image(image):
    return filter_image_or(image, lower_blue, upper_blue, lower_yel, upper_yel)

def get_corners(image):
    img = cv2.imread(image)
    if img is None:
        print(image+" not found")
        return
    resizeFactor = 0.5
    resBlue = blue_image(image)
    #cv2.imshow('Original_blue', resBlue)
    resYell = yellow_image(image)
    #cv2.imshow('Original_yellow', resYell)
    img = cv2.resize(img, (0, 0), None, resizeFactor, resizeFactor)
    basePoints, _ = utils.getBaseContours(resBlue, minArea=20000, showCanny=False)

    #imgDoted = utils.mark_points(basePoints, img, len(basePoints))
    cv2.imshow('Original', img)
    cv2.waitKey(0)

    imgWrp = utils.warpImg(img, basePoints, wP, hP, 20)
    cv2.imshow('Original warped',  cv2.resize(imgWrp, (0, 0), None, resizeFactor, resizeFactor))
    cv2.waitKey(0)
    #imgWrp = cv2.resize(imgWrp, (0, 0), None, resizeFactor, resizeFactor)

    resYell = yellow_image(imgWrp)
    boxPoints, contsBox = utils.getBoxContours(resYell, minArea=20000, showCanny=False, draw=True)
    resBoth = blue_yellow_image(imgWrp)
    completeImg = utils.mark_points(boxPoints, resBoth, len(boxPoints))

    triPoints = utils.get3Points(boxPoints)

    pairOfPoints = utils.pairPoints(triPoints) #boxPoints

    for pointsPair in pairOfPoints:
        point1 = pointsPair[0]
        point2 = pointsPair[1]
        dist = round(utils.findDis(point1, point2) // resizeFactor // 10, 1)

#           ancho = round((utils.findDis(nPoints[0][0] // scale, nPoints[1][0] // scale) / 10), 1)
#           alto = round((utils.findDis(nPoints[0][0] // scale, nPoints[2][0] // scale) / 10), 1)
        cv2.arrowedLine(completeImg, (point1[0][0], point1[0][1]), (point2[0][0], point2[0][1]),
                        (255, 0, 255), 3, 8, 0, 0.05)
        cv2.putText(completeImg, '{}cm'.format(dist), (point1[0][0] + 30, point1[0][1] ), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1.5,
                    (255, 0, 255), 2)
#           cv2.arrowedLine(imgWrp, (nPoints[0][0][0], nPoints[0][0][1]), (nPoints[2][0][0], nPoints[2][0][1]),
#                           (255, 0, 255), 3, 8, 0, 0.05)
#           x, y, w, h = fig[3]
    cv2.imshow('Warped_doted', completeImg)
    cv2.waitKey(0)

def makeTriFiles(root, seq):
    trio = ["", "", ""]
    for i in range(3):
        file = f"{seq}_{i+1}.jpg" #= './images/toma12_2.jpg'
        trio[i] = root + file
    return trio

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    #show_image()
    #originalSizing()
    #get_corners(image3)
    trio = makeTriFiles('./images/toma', 11)
    for f in trio:
        get_corners(f)

# See PyCharm help at https://www.jetbrains.com/help/pycharm/


def download_file_from_google_drive(id, destination):
    '''
    Function Author: @turdus-merula, from https://stackoverflow.com/questions/25010369/wget-curl-large-file-from-google-drive/39225039#39225039

    Docstring Author: Harry Hawkes (GH: @hph220), Lehigh Class of 2020

    Purpose: A solution for sharing large files with your group or future users.

    Solution:
    1. Upload (compressed) files to Lehigh Google Drive. (A non-Lehigh google drive
    will work too, but has limitations on space.)

    2. Turn on sharing for the file by right clicking it and selecting "get shareable link",
    but you don't need to give edit access.

    3. Turn on sharing for users outside of Lehigh University, but you don't need to give
    edit access.

    4. Get the ID from the sharing link (found at the end of the sharing URL), you'll need to
    put that into your code.

    5. In your file that uses this data:

        import os
        from download_file_from_google_drive import download_file_from_google_drive

        # download the file to your computer if you haven't already

        filepath_to_download_to = 'my_big_file.csv' # replace with your preferred name/path
        if not os.path.exists(filepath_to_download_to)
            download_file_from_google_drive(<put the ID here>, filepath_to_download_to)

        # open it
        <open the file, exact method depends on file type>
    '''

    import requests

    def get_confirm_token(response):
        for key, value in response.cookies.items():
            if key.startswith('download_warning'):
                return value

        return None

    def save_response_content(response, destination):
        CHUNK_SIZE = 32768

        with open(destination, "wb") as f:
            for chunk in response.iter_content(CHUNK_SIZE):
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)

    URL = "https://docs.google.com/uc?export=download"

    session = requests.Session()

    response = session.get(URL, params = { 'id' : id }, stream = True)
    token = get_confirm_token(response)

    if token:
        params = { 'id' : id, 'confirm' : token }
        response = session.get(URL, params = params, stream = True)

    save_response_content(response, destination)

if __name__ == "__main__":
    import sys
    if len(sys.argv) is not 3:
        print("Usage: python google_drive.py drive_file_id destination_file_path")
    else:
        # TAKE ID FROM SHAREABLE LINK
        file_id = sys.argv[1]
        # DESTINATION FILE ON YOUR DISK
        destination = sys.argv[2]
        download_file_from_google_drive(file_id, destination)


#coding:utf-8

"""
ID:          index.alter-05
TITLE:       ALTER INDEX - INACTIVE FOREIGN KEY
DESCRIPTION:
FBTEST:      functional.index.alter.05
"""

import pytest
from firebird.qa import *

init_script = """CREATE TABLE pk( a INTEGER NOT NULL,
                CONSTRAINT pkindx PRIMARY KEY(a)
              );
commit;
CREATE TABLE fk( a INTEGER NOT NULL,
                 CONSTRAINT fkindx FOREIGN KEY(a) REFERENCES pk(a)
              );
commit;"""

db = db_factory(init=init_script)

test_script = """ALTER INDEX fkindx INACTIVE;"""

act = isql_act('db', test_script)

expected_stderr = """Statement failed, SQLSTATE = 27000
unsuccessful metadata update
-ALTER INDEX FKINDX failed
-action cancelled by trigger (2) to preserve data integrity
-Cannot deactivate index used by an integrity constraint
"""

@pytest.mark.version('>=3.0')
def test_1(act: Action):
    act.expected_stderr = expected_stderr
    act.execute()
    assert act.clean_stderr == act.clean_expected_stderr


# -*- coding: utf-8 -*-
"""
Created on Sun Mar 12 13:06:07 2017
@author: Mario
"""

########################################################################
#   Cropping an image: Function to crop an image
#
#
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# at your option) any later version.                                   #
#                                                                      #
# This program 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 General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with this program. If not, see <http://www.gnu.org/licenses/>. #
########################################################################

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

def crop(X):

    def scale(Y):
        A=np.array(Y)
        mx=np.amax(Y)
        mn=np.amin(Y)
        A=(Y-mn)/(mx-mn)
        return np.float32(A)

    image=np.zeros(X.shape+(3,),float)
    for i in range(3): image[:,:,i]=scale(X)

    cropping = False

    def ccrop(event, x, y, flags, param):
    	global coords, cropping
    	if event == cv2.EVENT_LBUTTONDOWN:
    		coords = [(x, y)]
    		cropping = True
    	elif event == cv2.EVENT_LBUTTONUP:
    		coords.append((x, y))
    		cropping = False
    		cv2.rectangle(image, coords[0], coords[1], (0, 0, 1), 2)
    		cv2.imshow("Patient", image)

    clone = image.copy()
    cv2.namedWindow("Patient")
    cv2.setMouseCallback("Patient", ccrop)

    while True:
    	cv2.imshow("Patient", image)
    	key = cv2.waitKey(1) & 0xFF
    	if key == ord("r"):
    		image = clone.copy()
    	elif key == ord("c"):
    		break

    [x0,y0]=coords[0]
    [xf,yf]=coords[1]

    if x0 > xf:
        x0,xf = xf,x0
    elif x0 == xf:
        x0, xf= -1
    if y0 > yf:
        y0,yf = yf,y0
    elif y0 == yf:
        y0, yf= -1

    if len(coords) == 2:
        	roi = clone[y0:yf,x0:xf,:]
        	cv2.imshow("Region of Interest", roi)
        	cv2.waitKey(0)

    cv2.destroyAllWindows()

    return ((x0,y0),(xf,yf))
"""data=dicom.read_file('C:/Users/Mario/Desktop/algoritmo/pato 1/ser003img00109.dcm')

roi,image= crop(data.pixel_array)

cv2.destroyAllWindows()
axes = np.zeros((1, 3), dtype=np.object)

d=np.where(image[:,:,2] == 255)
image[d[0],d[1],2]=1

fig=plt.figure(figsize=(12,6))
im=axes[0,0]=fig.add_subplot(1,3,1)
roiim=axes[0,1]=fig.add_subplot(1,3,2)
im.set_axis_off()
im.set_title('Imagen original',fontsize=10)
roiim.set_title('Regíon de interés',fontsize=10)
roiim.set_axis_off()
axes[0,0].imshow(image)
axes[0,1].imshow(roi,cmap='gray')
f=axes[0,2]=fig.add_subplot(1,3,3)
axes[0,2].hist(roi.ravel(),bins=150,color='blue')
f.set_title('Histograma de región de interés',fontsize=10)"""


import glob
import os
import pickle
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.patches import Rectangle
from blitzcurve.utils import setup_matplotlib_dark_background
import blitzcurve.utils as utils
import sys
import blitzcurve

def run_compare(data_dir, file_list="all", name_dict= None):
    """

    Parameters
    ----------
    data_dir : str
        Path to directory with the input text files.
    file_list : str, list
        List of files to analyse.
        E.g. ["10nM-FGC1-2min_aniso.txt", "10nM-FGC2-2min_aniso.txt"]
        Default is "all", which analyses all files in folder
    name_dict : dict
        Dictionary to rename long filenames for plotting.

    Returns
    -------

    Usage
    -----
    import blitzcurve
    data_dir = r"D:\data\20180229_TRdata"
    # fit
    blitzcurve.run_fit(data_dir, figs_to_plot=figs_to_plot, testing_mode=False)
    # compare
    name_dict = {"10nM-FGC1-2min_aniso.txt": "FGC1", "10nM-FGC2-2min_aniso.txt": "FGC2", "10nM-FGC3-2min_aniso.txt": "FGC3"}
    blitzcurve.run_compare(data_dir, name_dict=name_dict)
    """

    # create object with the compare file paths
    cfp = utils.CompareFilePaths(data_dir)

    setup_matplotlib_dark_background(plt)

    fc = utils.FlourescentColours()
    csv_files = glob.glob(os.path.join(data_dir, "*.txt"))

    # drop any files not in the list
    if file_list != "all":
        for csv in csv_files:
            if os.path.basename(csv) not in file_list:
                csv_files.remove(csv)

    summ_csv = os.path.join(data_dir, "summary", "fit_summary.csv")
    df = pd.read_csv(summ_csv, index_col=0)

    df["orig_names"] = df.index

    if name_dict is not None:
        # rename the index
        df.rename(index=name_dict, inplace=True)
    else:
        # no naming dict available
        # cut off the ".txt" in the filename before plotting.
        df.index = df.index.str[:-4]

    create_barcharts = True

    if create_barcharts:
        #########################################################################
        #                   Barchart maximum anisotropy                         #
        #########################################################################
        fig, ax = plt.subplots()
        width = 0.6
        df["r_max"].plot(kind="bar", ax=ax, color=fc.blue, width=width, label="segment 1")
        ax.set_ylabel(r"$r_{max}$", fontsize=16)
        # adjust margins around bars
        ax.set_xlim(-0.6, df.shape[0] - 0.4)
        ax.set_ylim(df["r_max"].min() - 0.005, df["r_max"].max() + 0.005)
        fig.tight_layout()
        fig.savefig(cfp.barchart_r_max)
        sys.stdout.write("\n{}".format(cfp.barchart_r_max))

        #########################################################################
        #                              Barchart r_inf                           #
        #########################################################################
        fig, ax = plt.subplots()
        width = 0.6
        df["r_inf"].plot(kind="bar", ax=ax, color=fc.blue, width=width, label="segment 1")
        ax.set_ylabel(r"$r_{inf}$", fontsize=16)
        # adjust margins around bars
        ax.set_xlim(-0.6, df.shape[0] - 0.4)
        ax.set_ylim(df["r_inf"].min() - 0.005, df["r_inf"].max() + 0.005)
        fig.tight_layout()
        fig.savefig(cfp.barchart_r_inf)
        sys.stdout.write("\n{}".format(cfp.barchart_r_inf))

        #########################################################################
        #               Barchart variable a, segments 1 and 2                   #
        #########################################################################

        #generate only dataframe with a_seg1 and a_seg2
        df_a_bars = pd.concat([df["a_seg1"], df["a_seg2"]], axis=1)
        df_a_bars.columns = ["a_seg1", "a_seg2"]

        fig, ax = plt.subplots()
        width = 0.8
        df_a_bars.plot(kind="bar", ax=ax, color=[fc.blue,fc.lemon], width=width, legend=False)
        #gets handles for the legend (also gets and labels - rudimentary)
        handles, labels = ax.get_legend_handles_labels()
        #brings segment b to secondary y axis
        ax2 = ax.twinx()
        for r in ax.patches[len(df_a_bars):]:
            r.set_transform(ax2.transData)
        #defines labels
        labels = ["segment 1", "segment 2"]

        ax.set_ylabel("variable a in segment 1", color=fc.blue)
        ax.tick_params("y", colors=fc.blue)
        ax2.set_ylabel("variable a in segment 2", color=fc.lemon)
        ax2.tick_params("y", colors=fc.lemon)
        # adjust margins around bars
        ax.set_xlim(-0.6, df.shape[0] - 0.4)
        ax.set_ylim(df["a_seg1"].min() - 0.005, df["a_seg1"].max() + 0.01)
        ax2.set_ylim(df["a_seg2"].min() - 0.005, df["a_seg2"].max() + 0.01)
        fig.legend(handles,labels,ncol=2, loc="upper center", bbox_to_anchor=(0, 0.85, 1.1, .102))

        fig.tight_layout()
        fig.savefig(cfp.barchart_variable_a_png)
        sys.stdout.write("\n{}".format(cfp.barchart_variable_a_png))

        #########################################################################
        #               Barchart variable b, segments 1 and 2                   #
        #########################################################################

        #generate only dataframe with b_seg1 and b_seg2
        df_b_bars = pd.concat([df["b_seg1"], df["b_seg2"]], axis=1)
        df_b_bars.columns = ["b_seg1", "b_seg2"]

        fig, ax = plt.subplots()
        width = 0.8
        df_b_bars.plot(kind="bar", ax=ax, color=[fc.blue, fc.lemon], width=width, legend=False)
        # gets handles for the legend (also gets and labels - rudimentary)
        handles, labels = ax.get_legend_handles_labels()
        # brings segment b to secondary y axis
        ax2 = ax.twinx()
        for r in ax.patches[len(df_b_bars):]:
            r.set_transform(ax2.transData)
        # defines labels
        labels = ["segment 1", "segment 2"]

        ax.set_ylabel("variable b in segment 1", color=fc.blue)
        ax.tick_params("y", colors=fc.blue)
        ax2.set_ylabel("variable b in segment 2", color=fc.lemon)
        ax2.tick_params("y", colors=fc.lemon)
        # adjust margins around bars
        ax.set_xlim(-0.6, df.shape[0] - 0.4)
        ax.set_ylim(df["b_seg1"].min() - 0.005, df["b_seg1"].max() + 0.02)
        ax2.set_ylim(df["b_seg2"].min() - 0.005, df["b_seg2"].max() + 0.02)
        fig.legend(handles,labels,ncol=2, loc="upper center", bbox_to_anchor=(0, 0.85, 1.1, .102))

        fig.tight_layout()
        fig.savefig(cfp.barchart_variable_b_png)
        sys.stdout.write("\n{}".format(cfp.barchart_variable_b_png))

        #########################################################################
        #               Barchart variable c, segments 1 and 2 (r_inf)           #
        #########################################################################
        plt.close("all")

        # generate only dataframe with c_seg1 and r_inf
        df_c_rinf_bars = pd.concat([df["c_seg1"], df["r_inf"]], axis=1)
        df_c_rinf_bars.columns = ["c_seg1", "r_inf"]

        fig, ax = plt.subplots()
        width = 0.8
        df_c_rinf_bars.plot(kind="bar", ax=ax, color=[fc.blue, fc.lemon], width=width, legend=False)
        # gets handles for the legend (also gets and labels - rudimentary)
        handles, labels = ax.get_legend_handles_labels()
        # brings segment b to secondary y axis
        ax2 = ax.twinx()
        for r in ax.patches[len(df_c_rinf_bars):]:
            r.set_transform(ax2.transData)
        # defines labels
        labels = ["segment 1", "segment 2"]

        ax.set_ylabel("variable c in segment 1", color=fc.blue)
        ax.tick_params("y", colors=fc.blue)
        ax2.set_ylabel("variable c (r_inf) in segment 2", color=fc.lemon)
        ax2.tick_params("y", colors=fc.lemon)
        # adjust margins around bars
        ax.set_xlim(-0.6, df.shape[0] - 0.4)
        ax.set_ylim(df["c_seg1"].min() - 0.005, df["c_seg1"].max() + 0.01)
        ax2.set_ylim(df["r_inf"].min() - 0.005, df["r_inf"].max() + 0.01)

        fig.legend(handles,labels,ncol=2, loc="upper center", bbox_to_anchor=(0, 0.85, 1.1, .102))
        fig.tight_layout()
        fig.savefig(cfp.barchart_variable_c_png)
        sys.stdout.write("\n{}".format(cfp.barchart_variable_c_png))

    fig_sg, ax_sg = plt.subplots()
    fig_seg1, ax_seg1 = plt.subplots()
    fig_seg2, ax_seg2 = plt.subplots()

    for csv in csv_files:
        # paths for the output files
        p = utils.FitFilePaths(data_dir, csv)
        with open(p.fitdata_pickle, "rb") as pic:
            #fd = blitzcurve.fit.OutputFitData()
            fd = pickle.load(pic)
            print("fd.filename", fd.filename)
            #for item in [fd.fit_savgol, fd.seg1_xfit, fd.seg1_yfit, fd.seg2_xfit, fd.seg2_yfit]:
            #    assert item is not None
            if name_dict is not None:
                label = name_dict[fd.filename] if fd.filename in name_dict else fd.filename
            else:
                label = fd.filename
            ax_sg.plot(fd.time, fd.y_fit_savgol, label=label)

            ax_seg1.plot(fd.time, fd.y_fit_savgol, color="0.3", zorder=0)
            ax_seg1.plot(fd.seg1_xfit, fd.seg1_yfit, label=label)

            ax_seg2.plot(fd.time, fd.y_fit_savgol, color="0.3", zorder=0)
            ax_seg2.plot(fd.seg2_xfit, fd.seg2_yfit, label=label)

    ax_sg.set_xlabel("time (ns)")
    ax_sg.set_ylabel("anisotropy, r")
    ax_sg.legend()
    fig_sg.tight_layout()
    fig_sg.savefig(cfp.linechart_savgol)

    ax_seg1.set_xlabel("time (ns)")
    ax_seg1.set_ylabel("anisotropy, r")
    ax_seg1.legend()
    fig_seg1.tight_layout()
    fig_seg1.savefig(cfp.linechart_seg1)

    ax_seg2.set_xlabel("time (ns)")
    ax_seg2.set_ylabel("anisotropy, r")
    ax_seg2.legend()
    fig_seg2.tight_layout()
    fig_seg2.savefig(cfp.linechart_seg2)


each_post="
\
                    <a href=\"{<link>}\" target=\"_blank\">\

\
                            {<title>}\

\

\
                            {<subtitle>}\

\
                    \

Posted on {<date>}
\

\

"

import feedparser

rss_url = "https://medium.com/feed/@americast"

feed = feedparser.parse( rss_url )

all_posts=""
for each_feed in feed["entries"]:
	link = each_feed["link"]
	date = each_feed["published"]
	title =  each_feed["title"]

	post_here=each_post.replace('{<link>}',link.decode('utf-8'))
	post_here=post_here.replace('{<date>}',date[:-12].decode('utf-8'))
	post_here=post_here.replace('{<title>}',title.decode('utf-8'))

	subtitle=raw_input("Enter subtitle for "+str(title)+ "or enter 0 to skip:  ")
	if subtitle=='0': continue

	post_here=post_here.replace('{<subtitle>}',subtitle.decode('utf-8'))
	all_posts+=post_here
f=open("index.html","r")
org_html=f.read()
all_posts=org_html.decode('utf-8').split("")[0]+"\n\n"+all_posts+org_html.decode('utf-8').split("")[1]
f.close()
f=open("index.html","w")
f.write(all_posts)


from msdi_io import *
import numpy as np
from skimage.feature import hog
from librosa.feature import mfcc
from os import listdir
import json, pickle

_msdi_path = 'msdi'
orientation_hog_img = 8
P=Q=3

def contructing_img_feature(img):
    hog_vector, _ = hog(img, orientations=orientation_hog_img, pixels_per_cell=(img.shape[0]//P, img.shape[1]//Q),
        cells_per_block=(1, 1), visualize=True, multichannel=True)
    return hog_vector

def contructing_tram_feature(tram):
    return np.sum(tram, axis=0)

def building_data():
    labels = get_label_list()
    msdi = get_msdi_dataframe(_msdi_path)
    with open('bag_of_words.json', 'r') as fp:
        bag_of_words = json.load(fp)
    dataset = []
    for index in range(len(msdi)):
        entry = msdi.loc[index] # index in the data file
        set = get_set(entry) # train/val/test
        track_id = get_track_id(entry) # track ID

        tram = load_mfcc(entry, _msdi_path) # load mfcc
        tram_feature = contructing_tram_feature(tram) # audio features

        img = load_img(entry, _msdi_path) # get image
        img_feature = contructing_img_feature(img) # img features

        entry_label = get_label(entry) # instance label

        # bag_of_words in the dict. I built it before

        info = {
            'id' : track_id,
            'instance_set' : set,
            'label' : labels.index(entry_label),
            'audio' : tram_feature,
            'image' : img,
            'hog' : img_feature,
            'bag_of_words': np.asarray(bag_of_words[track_id]) if track_id in bag_of_words.keys() else None
        }

        dataset.append(info)
    return dataset

#print(load_mfcc_dicts()["mfcc_A.npz"].keys())

dataset = building_data()
#filehandler = open("dataset.txt","wb")
#pickle.dump(dataset,filehandler)


import requests
import datetime
import pprint
from config import open_weather_token, code_to_smile

def getWeather(city):
    try:
        r = requests.get(
            f"https://api.openweathermap.org/data/2.5/forecast?q={city}&appid={open_weather_token}"
        )
        data = r.json()
        print(data)

        city = data["name"]
        curTemp = data["main"]["temp"]
        humidity = data["main"]["humidity"]
        pressure = data["main"]["pressure"]
        windSpeed = data["wind"]["speed"]
        sunriseTime = datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
        sunsetTime = datetime.datetime.fromtimestamp(data["sys"]["sunset"])
        lenthOfTheDay = sunsetTime - sunriseTime

        weatherDisc = data["weather"][0]["main"]
        if weatherDisc in code_to_smile:
            wd = code_to_smile[weatherDisc]

        print(f"***{datetime.datetime.now().strftime('%Y-%m-%d %H-%M')}***\n"
              f"{city} {wd}\nTemperature: {curTemp}°C\n"
              f"Humidity: {humidity}%\nPressure: {pressure} mmHg\n"
              f"Wind: {windSpeed} m\s\nSunrise: {sunriseTime}\n"
              f"Sunset: {sunsetTime}\nDaylight hours: {lenthOfTheDay}\n"
              f"Have a nice day!")

    except Exception as ex:
        print(ex)
        print("Check the name of the city")

def getDayWeather(city):
    try:
        r = requests.get(
            f"https://api.openweathermap.org/data/2.5/forecast?q={city}&appid={open_weather_token}"
        )
        data = r.json()
        print(data)

        city = data["name"]
        curTemp = data["main"]["temp"]
        feelsTemp = data[""]
        maxTemp = data[""]
        minTemp = data[""]
        humidity = data["main"]["humidity"]
        pressure = data["main"]["pressure"]
        windSpeed = data["wind"]["speed"]
        sunriseTime = datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
        sunsetTime = datetime.datetime.fromtimestamp(data["sys"]["sunset"])
        lenthOfTheDay = sunsetTime - sunriseTime

        weatherDisc = data["weather"][0]["main"]
        if weatherDisc in code_to_smile:
            wd = code_to_smile[weatherDisc]

        print(#f"***{datetime.datetime.now().strftime('%Y-%m-%d %H-%M')}***\n"
              f"{city}\n{curTemp}°C  {wd}\n\n"
              f"Feels like: {feelsTemp}\n"
              f"Max: {maxTemp} / min: {minTemp}\n\n"
              f"\U0001F4A7 Humidity: {humidity}%\n\U0001F321 Pressure: {pressure} mmHg\n"
              f"\U0001F32C Wind: {windSpeed} m\s\n\U0001F304 Sunrise: {sunriseTime.strftime('%H:%M')}\n"
              f"\U0001F307 Sunset: {sunsetTime.strftime('%H:%M')}\n\U0000231B Daylight hours: {lenthOfTheDay}\n\n"
              f"***Have a nice day!***")

    except Exception as ex:
        print(ex)
        print("Check the name of the city")

def getFiveDayWeather(city):
    try:
        r = requests.get(
            f"https://api.openweathermap.org/data/2.5/forecast?q={city}&appid={open_weather_token}"
        )
        data = r.json()
        print(data)

        city = data["name"]
        maxTemp1 = data[""]
        minTemp1 = data[""]
        weatherDisc1 = data["weather"][0]["main"]
        if weatherDisc1 in code_to_smile:
            wd1 = code_to_smile[weatherDisc1]
        maxTemp2 = data[""]
        minTemp2 = data[""]
        weatherDisc2 = data["weather"][0]["main"]
        if weatherDisc2 in code_to_smile:
            wd2 = code_to_smile[weatherDisc2]
        maxTemp3 = data[""]
        minTemp3 = data[""]
        weatherDisc3 = data["weather"][0]["main"]
        if weatherDisc3 in code_to_smile:
            wd3 = code_to_smile[weatherDisc3]
        maxTemp4 = data[""]
        minTemp4 = data[""]
        weatherDisc4 = data["weather"][0]["main"]
        if weatherDisc4 in code_to_smile:
            wd4 = code_to_smile[weatherDisc4]
        maxTemp5 = data[""]
        minTemp5 = data[""]
        weatherDisc5 = data["weather"][0]["main"]
        if weatherDisc5 in code_to_smile:
            wd5 = code_to_smile[weatherDisc5]

        print(f"{city}\n\n"
              f"Today {wd1}\n"
              f"{minTemp1}°C / {maxTemp1}°C\n\n"
              f"Tomorrow {wd2}\n"
              f"{minTemp2}°C / {maxTemp2}°C\n\n"
              f" {wd3}\n"
              f"{minTemp3}°C / {maxTemp3}°C\n\n"
              f" {wd4}\n"
              f"{minTemp4}°C / {maxTemp4}°C\n\n"
              f" {wd5}\n"
              f"{minTemp5}°C / {maxTemp5}°C\n\n"
              f"***Have a nice day!***")

    except Exception as ex:
        print(ex)
        print("Check the name of the city")

def test_weather(city):
    try:
        r = requests.get(
            f"https://api.openweathermap.org/data/2.5/forecast?q={city}&appid={open_weather_token}&units=metric"
        )
        data = r.json()
        print(data)

        date = []
        maxTemp = []
        minTemp = []
        weatherDisc = []
        wd = []

        for i in data['list']:
            print(i['dt_txt'], '{0:+3.0f}'.format(i['main']['temp']), i['weather'][0]['description'])

        for i in data['list']:
            maxTemp.append(i['main']['temp_max'])
            minTemp.append(i['main']['temp_min'])
            weatherDisc.append(i['weather'][0]['main'])

        print(maxTemp[0], maxTemp[1], maxTemp[2], maxTemp[3], maxTemp[4])
        print(minTemp[0], minTemp[1], minTemp[2], minTemp[3], minTemp[4])
        print(weatherDisc[0], weatherDisc[1], weatherDisc[2], weatherDisc[3], weatherDisc[4])

        for el in weatherDisc:
            if el in code_to_smile:
                wd.append(code_to_smile[el])

        for el in wd:
            print(el)

    except:
        print ("\U00002620 Check the name of the city \U00002620 ")

def test_tomorrow(city):
    try:
        r = requests.get(
            f"https://api.openweathermap.org/data/2.5/forecast?q={city}&appid={open_weather_token}&units=metric"
        )
        data = r.json()
        pprint.pprint(data)

        date = datetime.datetime.today() + datetime.timedelta(days=1)
        curTemp = '{0:+3.0f}'.format(data["list"][1]["main"]["temp"])
        feelsTemp = '{0:+3.0f}'.format(data["list"][1]["main"]["feels_like"])
        maxTemp = '{0:+3.0f}'.format(data["list"][1]["main"]["temp_max"])
        minTemp = '{0:+3.0f}'.format(data["list"][1]["main"]["temp_min"])
        windSpeed = data["list"][1]["wind"]["speed"]

        weatherDisc = data["list"][1]["weather"][0]["main"]
        if weatherDisc in code_to_smile:
            wd = (code_to_smile[weatherDisc])

        print(
            f"*Tomorrow*\n"
            f"({date})\n\n"
            f"{city}\n{curTemp}°C  {wd}\n"
            f"Feels like: {feelsTemp}\n\n"
            f"**Min / Max**\n"
            f"{minTemp} / {maxTemp}\n\n"
            f"\U0001F32C Wind: {windSpeed} m\s\n"
            f"***Have a nice day!***")

    except:
        print ("\U00002620 Check the name of the city \U00002620 ")

def main():
    city = input("Enter the city: ")
    test_tomorrow(city)

if __name__ == '__main__':
    main()


"""Runs a single episode to compare step durations across batch sizes.
"""

import ml_collections
import numpy as np
from configs.trial_config import trial_config

def get_config():
    config = ml_collections.ConfigDict()
    config.experiment_name = 'mlp_batch_sizes'
    config.trials = []
    for param in [64, 128, 256, 512, 1024]:
        trial = trial_config('MLP')
        trial.epochs = 1
        trial.batch_size = param
        trial.trial_name = f'batch_size_{param}'
        config.trials.append(trial)
    config.lock()
    return config


import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.crawler import Request
import os
import logging

class SerieASpidermatch(scrapy.Spider):
    name = "Serie A"
    start_urls = ["https://www.booking.com/"]

    def start_requests(self):
        for d in [URLS]:
            yield Request(d,callback=self.parse)

    def parse(self, response):
        card = response.xpath('//*[@id="outer-edito-content"]/div[3]/main/div[3]/div[2]/div[1]/div/div')
        for i in card:
            winner = i.css('div.TeamScore__name TeamScore__team--winner').get()
            looser = i.css('TeamScore__name TeamScore__team--looser').get()
            score = i.css('TeamScore__score TeamScore__score--ended').getall()
            dic = {
                'winner': winner , 'looser': looser , 'score': score
            }

            yield dic

# Define a filename
filename = "serie_A_scrap_matches.json"

# Remove previous file if it allready exist
if filename in os.listdir('src/'):
        os.remove('src/' + filename)

# Define the settings for the crawler process
process = CrawlerProcess(settings = {
    # simulate browser
    'USER_AGENT': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0',
    # display regular errors
    'LOG_LEVEL': logging.INFO,
    "FEEDS": {
        'src/' + filename : {"format": "json"},
    }
})

# Initiate crawler
process.crawl(SerieASpidermatch)
process.start()


import transformnet
import extractnet
import numpy as np
import tensorflow as tf
import PIL.Image as Image
import os
from config import *
import uuid
import shutil
import config

def traingen(dirpath):
    flist = os.listdir(dirpath)
    np.random.shuffle(flist)
    cursor = 0
    while cursor<len(flist):
        batchfn = flist[cursor:cursor+batchsize]
        cursor+=batchsize
        ims = []
        for fn in batchfn:
            im = Image.open(os.path.join(dirpath, fn))
            im = im.resize((224, 224), Image.ANTIALIAS)
            im = np.array(im) / 255.
            ims.append(im)
        yield np.array(ims)

sess = tf.Session()
# model1
input = tf.placeholder(dtype='float32', shape=(None, None, None, 3),name="input")
synthesizetensor = transformnet.transform(input)
predcontenttensor, predstyletensors = extractnet.extract(synthesizetensor,prefix="A")
# model2
contenttensor, styletensors = extractnet.extract(input,prefix="B")

styleim = Image.open(styleimg)
#styleim = styleim.resize((224, 224), Image.ANTIALIAS)
styleim = np.expand_dims(np.array(styleim) / 255., axis=0)
styles = sess.run(styletensors,feed_dict={input:styleim})
grams = []
for style in styles:
    style = np.squeeze(style)
    style = np.transpose(style,[2,0,1])
    channelnum = style.shape[0]
    style = np.resize(style,(channelnum,-1))
    styletranspose = np.transpose(style,[1,0])
    gram = np.matmul(style,styletranspose)/style.shape[1]
    grams.append(gram)
loss = tf.reduce_sum((predcontenttensor-contenttensor)**2)/tf.cast(tf.size(contenttensor[0]),dtype=tf.float32)
for predstyletensor,truthgram in zip(predstyletensors,grams):
    predstyletensor = tf.transpose(predstyletensor,[0,3,1,2])
    shape = tf.shape(predstyletensor)
    predstyletensor = tf.reshape(predstyletensor,shape=[shape[0],shape[1],-1])
    grampred = tf.matmul(predstyletensor,predstyletensor,transpose_b=True)/tf.cast(tf.shape(predstyletensor)[2],dtype=tf.float32)
    loss+=30*tf.reduce_sum((grampred-truthgram)**2)/truthgram.size/len(config.styletensors)
loss/=batchsize
trainop = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss)
saver = tf.train.Saver()
#sess.run(tf.global_variables_initializer())
saver.restore(sess,modelpath)

def train():
    for ep in range(epoch):
        testims = next(traingen(testpath))
        minerr = 999999999
        argminerr = -1
        errorrec = []
        for i, batchims in enumerate(traingen(trainpath)):
            _, losstrain = sess.run([trainop, loss], feed_dict={input: batchims})
            print(losstrain)
            if i % 5 == 0:
                synthesizeimgs, lossval = sess.run([synthesizetensor, loss], feed_dict={input: testims})
                errorrec.append(lossval)
                print("validate loss:"+str(lossval))
                if len(errorrec) - argminerr > 100:
                    return
                list(map(os.unlink, (os.path.join(savepath, f) for f in os.listdir(savepath))))
                for im in synthesizeimgs:
                    im = np.uint8(im*255)
                    Image.fromarray(im).save(os.path.join(savepath, str(uuid.uuid4()) + ".jpg"))
                if lossval < minerr:
                    minerr = lossval
                    argminerr = len(errorrec) - 1
                    saver.save(sess,modelpath)

train()

# tf.summary.scalar('cc',loss)
# merged = tf.summary.merge_all()
# train_writer = tf.summary.FileWriter(r"D:\Users\yl_gong\Desktop\log",
#                                       sess.graph)
# sess.run(tf.global_variables_initializer())
# im = Image.open(testimg)
# im = np.expand_dims(np.array(im)/255.,axis=0)
# summary = sess.run(merged,feed_dict={input:im})
# train_writer.add_summary(summary, 0)