#!/usr/bin/python
import numpy as np
import os
import json
import argparse

try:
    import bnnbench
except:
    import sys

    sys.path.append(os.path.expandvars('$BNNBENCHPATH'))

import bnnbench.utils.data_utils
from bnnbench.models import model_types
from bnnbench.config import globalConfig
from bnnbench import _log as bnnbench_logger
from bnnbench.utils.attrDict import AttrDict
import bnnbench.utils.universal_utils as utils

config_top_level_keys = utils.config_top_level_keys

def handle_cli() -> argparse.Namespace:

    print("Handling command line arguments.")
    parser = argparse.ArgumentParser(add_help=True,
                                     description="Generate a JSON file containing default values for conducting an "
                                                 "experiment using one of the supported model types. This utility is "
                                                 "intended to be used for auto-generating a sample of all configurable "
                                                 "settings of a configuration file for various use cases.")
    parser.add_argument('-m', '--model', type=str, default='mlp',
                        help='Case-insensitive string indicating the type of model to generate a config file for. '
                             'Valid options are: ["mlp", "mcdropout", "mcbatchnorm", "ensemble", "dngo"]')
    parser.add_argument('-f', '--filename', type=str, default='',
                        help='Optional name to store the generated config file as. Default is '
                             'sample_{model}_config.json. A directory path and default filename may be specified by '
                             'appending a "/" at the very end. Unless an absolute filepath is given, all paths are '
                             'assumed to be relative to the current working directory.')
    parser.add_argument('-o', '--otype', type=str, default="dataset",
                        help='Type of objective to be used. Can be either "toy_1d" for 1D toy functions or "dataset" '
                             'for locally stored datasets.')

    return parser.parse_args()

def generate_config(model: str, filename: str = None, otype: str = "dataset"):

    mtype = str.lower(model)
    if mtype not in model_types:
        raise RuntimeError("Unknown/unsupported model type specified. Must be one of: %s" % str(model_types.keys()))

    filepath = utils.standard_pathcheck(filename)
    if not os.path.isabs(filepath):
        filepath = os.path.join(os.getcwd(), filepath)

    if os.path.isdir(filepath):
        # The absolute path leads to a directory name, not a filename
        filepath = os.path.join(filepath, f"sample_{model}_config.json")

    mparams = model_types[mtype]().model_params._asdict()
    eparams = globalConfig.to_cli()

    utils.make_model_params_json_compatible(mparams)
    mparams['model_name'] = None   # Leave empty to allow random names to be generated.

    def json_objective(otype):
        otype = str.lower(otype)
        if otype == 'toy_1d':
            return "Infinity GO Problem 04"
        elif otype == "dataset":
            return {
                "type": "dataset",
                "name": "boston"
            }
        else:
            raise RuntimeError("Unknown objective '%s' of type %s" % (otype, type(otype)))

    jdict = {
        config_top_level_keys.obj_func: json_objective(otype),
        config_top_level_keys.mparams: mparams,
        config_top_level_keys.eparams: eparams
    }

    with open(filepath, 'w') as fp:
        try:
            json.dump(jdict, fp, indent=4)
        except TypeError as e:
            print("Could not write configuration file for config:\n%s" % jdict)

if __name__ == '__main__':
    generate_config()


from dataclasses import dataclass
import sys
from pathlib import Path

import pickle
import numpy as np
import skimage
from skimage.color import rgb2hsv
from skimage.morphology import binary_opening, binary_closing
from skimage.measure import label, regionprops, EllipseModel
from scipy.optimize import minimize
from joblib import Memory, Parallel, delayed

memory = Memory(cachedir=str(Path(__file__).parent / "cache"), verbose=0)

@memory.cache
def load_image(path: Path) -> np.ndarray:
    return skimage.io.imread(str(path))

@memory.cache
def extract_marker_coord(
    image: np.ndarray, min_th: float = 0.62, max_th: float = 0.68
) -> np.ndarray:
    hue_img = rgb2hsv(image)[:, :, 0]
    marker_candidates_mask = (min_th < hue_img) & (hue_img < max_th)
    marker_candidates_mask = binary_opening(
        marker_candidates_mask, footprint=skimage.morphology.disk(3)
    )
    marker_candidates_mask = binary_closing(
        marker_candidates_mask, footprint=skimage.morphology.disk(3)
    )
    marker_candidates_label = label(marker_candidates_mask)
    marker_candidates_prop = max(
        regionprops(marker_candidates_label), key=lambda x: x.area
    )
    return np.array(marker_candidates_prop.centroid)

@memory.cache
def extract_marker_coord_task(path: Path) -> np.ndarray:
    return extract_marker_coord(load_image(path))

def extract_marker_coords(root: Path) -> np.ndarray:
    marker_coords = Parallel(n_jobs=-1)(
        delayed(extract_marker_coord_task)(path) for path in sorted(root.glob("*.png"))
    )
    return np.stack(marker_coords)

@dataclass
class FOPTDParams:
    K: float = 1
    tau: float = 1
    tau_d: float = 0

def project_to_circle(marker_coords: np.ndarray) -> np.ndarray:
    ellipse = EllipseModel()
    ellipse.estimate(marker_coords)
    [xc, yc, a, b, theta] = ellipse.params
    A = np.array([[a, -b, xc], [b, a, yc], [0, 0, 1]])
    X = np.hstack((marker_coords, np.ones((marker_coords.shape[0], 1))))
    P = np.linalg.solve(A, X.T).T
    return P[:, :2]

def calc_step_response(coords: np.ndarray) -> np.ndarray:
    coords_on_unit_circle = coords / np.repeat(
        np.linalg.norm(coords, axis=1).reshape(-1, 1), 2, axis=1
    )
    forward_coord = coords_on_unit_circle[2:, 0] + 1j * coords_on_unit_circle[2:, 1]
    backward_coord = coords_on_unit_circle[:-2, 0] - 1j * coords_on_unit_circle[:-2, 1]
    delta_angles = np.angle(forward_coord * backward_coord) / 2.0

    max_idx = np.argmax(delta_angles)

    min_res_th = 0.004
    beg_idx = max(0, max_idx - np.argmax(delta_angles[max_idx:0:-1] < min_res_th) - 30)

    stop_th = 0.5 * delta_angles[max_idx]
    sustain_length = max(0, np.argmax(delta_angles[max_idx:] < stop_th) - 150)
    end_idx = max_idx + sustain_length
    return delta_angles[beg_idx:end_idx]

def foptd(t, K=1, tau=1, tau_d=0):
    tau_d = max(0, tau_d)
    tau = max(0, tau)
    return np.array(
        [K * (1 - np.exp(-(t - tau_d) / tau)) if t >= tau_d else 0 for t in t]
    )

def estimate_foptd_params(
    step_response: np.ndarray, input_amplitude: float, interval_sec: float
) -> FOPTDParams:
    def err(X, t, y):
        K, tau, tau_d = X
        z = foptd(t, K, tau, tau_d)
        iae = sum(abs(z - y)) * (max(t) - min(t)) / len(t)
        return iae

    X = [0.10 / input_amplitude, 10 * interval_sec, 30 * interval_sec]
    ts = np.array(range(len(step_response))) * interval_sec
    ys = step_response / input_amplitude
    K, tau, tau_d = minimize(err, X, args=(ts, ys)).x

    return FOPTDParams(K, tau, tau_d)

def estimate_foptd_task(root: Path, input_amplitude: int, fps: int) -> FOPTDParams:
    marker_coords = extract_marker_coords(root)
    projected_coords = project_to_circle(marker_coords)
    step_response = calc_step_response(projected_coords)

    interval_sec = 1.0 / fps
    return estimate_foptd_params(step_response, float(input_amplitude), interval_sec)

def main(root: Path, output: Path) -> None:
    fps = 240

    result = {}
    for capture_root in root.iterdir():
        if not capture_root.is_dir():
            continue
        input_amplitude = int(capture_root.name)
        result[input_amplitude] = estimate_foptd_task(
            capture_root, input_amplitude=input_amplitude, fps=fps
        )

    avg_k = sum([v.K for v in result.values()]) / len(result)
    avg_tau = sum([v.tau for v in result.values()]) / len(result)
    print(f"avg_k={avg_k}, avg_tau={avg_tau}")

    with open(output, "wb") as f:
        pickle.dump(result, f)

if __name__ == "__main__":
    try:
        root = Path(sys.argv[1])
        output = Path(sys.argv[2])
    except IndexError:
        sys.exit("Usage: from_images.py <root> <output.pickle>")
    main(root, output)


import pandas as pd
from owlready2 import *

# variables
units_file = 'Data/units_result.jl'
pp_file = 'Data/property_mapping_result.csv'
col_df_units = ['ProductLink',
                'Source',
                'StartTime',
                'EndTime',
                'DownloadTime',
                'Organization',
                'Name',
                'ProductCategory',
                'StructuredData',
                'Properties']
col_df_pp_uv = ['RequestID',
                'MatchPosition',
                'TypeofMatch',
                'Source',
                'ProductCategory',
                'ProductClass',
                'SpecificationName',
                'SpecificationUnit',
                'Example',
                'MatchedPropertyName',
                'MatchedPropertyID',
                'MatchedPropertyType',
                'MatchedPropertyUnit',
                'Description',
                'Ontology',
                'Score',
                'Scorer',
                'UserValidation']

# Functions
def update_vocabulary(onto, gr, df_pp_uv):
    for row in df_pp_uv.itertuples():
        p_name = row.MatchedPropertyID.replace(" ", "_")
        with onto:
            if onto[p_name]:
                onto_property = onto[p_name]
            elif 'DatatypeProperty' in row.MatchedPropertyType:
                onto_property = types.new_class(p_name, (DataProperty, gr.datatypeProductOrServiceProperty))
            elif 'ObjectProperty' in row.MatchedPropertyType:
                onto_property = types.new_class(p_name, (ObjectProperty, gr.quantitativeProductOrServiceProperty))

        SpecificationUnit = row.SpecificationUnit if not pd.isna(row.SpecificationUnit) else None

        update_property(onto, gr, onto_property, row.SpecificationName, SpecificationUnit, row.ProductClass)

def update_property(onto, gr, p, specification_name, specification_unit, taxonomic_class):
    target_class = [c for c in onto[taxonomic_class].subclasses() if '-gen' in c.name][0]
    #print(p, target_class)
    # update label or alternative label
    if not p.label:
        p.label.append(specification_name)
    elif specification_name not in p.label:
        if not p.altLabel or specification_name not in p.altLabel:
            p.altLabel.append(specification_name)

    # update unit
    if specification_unit and specification_unit not in p.UnitOfMeasurementName:
        p.UnitOfMeasurementName.append(specification_unit)

    # update domain
    if p.domain:
        if isinstance(p.domain[0], ThingClass):
            if target_class != p.domain[0]:
                p.domain = [Or([p.domain[0], target_class])]
        else:
            try:
                l_cls = p.domain[0].Classes
                if target_class not in l_cls:
                    l_cls.append(target_class)
                    p.domain = [Or(l_cls)]
            except:
                #print('domain could not be updated for %s' % p)
                pass
    else:
        p.domain.append(target_class)

    # update range (only object property quantitativeValueFloat)
    if owl.ObjectProperty in p.is_a:
        if not p.range: p.range = gr.QuantitativeValueFloat

def generate_instances(onto, gr, mbse, schemaorg, skos, df_units_r, df_pp_uv):
    for product in df_units_r.itertuples():
        taxonomic_class = product.ProductClass
        product_link = product.ProductLink
        name = product.Name
        organization_name = product.Organization
        category = product.ProductCategory
        timestamp = product.StartTime
        dics = [product.StructuredData.items(), product.Properties.items()]

        specs = set()
        target_class = [c for c in onto[taxonomic_class].subclasses() if '-gen' in c.name][0]

        someItem = gr.SomeItems(label=[name])
        onto.ECLASSType[someItem] = [target_class]
        gr['category'][someItem] = [category]

        offering = gr.Offering(includes=[someItem])
        mbse.describes[offering] = [someItem]

        organization = None
        for org in onto.search(iri='http://purl.org/goodrelations/v1#businessentity*'):
            if organization_name == org.label[0] or organization_name in org.altLabel:
                organization = org
                break
        if not organization:
            organization = gr.BusinessEntity(label=[organization_name])

        gr['offers'][organization].append(offering)

        ic = mbse.information_concretization(concretizes=[offering],
                                             timestamp=[timestamp],
                                             submitted_by=[mbse.Miguel],
                                             expressed_in=[mbse.HTML],
                                             authored_by=[organization])

        wp = None
        for webpage in onto.search(iri='http://david.org/informationmodel.owl#webpage*'):
            if product_link == webpage.label[0]:
                wp = webpage
                break
        if not wp:
            wp = mbse.Webpage(label=product_link)
        wp.captures.append(ic)

        for d in dics:
            for pair in d:
                # properties where unit was not well read represent a problem here!
                spec_name = pair[0]
                spec_value = pair[1]['value']
                spec_unit = pair[1]['unit']
                spec_value_orig = pair[1]['original_value']

                mask1 = df_pp_uv['SpecificationName'] == spec_name
                mask2 = df_pp_uv['SpecificationUnit'] == spec_unit if spec_unit else pd.isna(
                    df_pp_uv['SpecificationUnit'])
                mapping = df_pp_uv.loc[mask1 & mask2, ['SpecificationName', 'SpecificationUnit',
                                                       'MatchedPropertyID', 'Ontology']].drop_duplicates()

                # if the unit was droped in the mapping phase, read the spec_name without unit
                if mapping.empty:
                    mask2 = pd.isna(df_pp_uv['SpecificationUnit'])
                    mapping = df_pp_uv.loc[mask1 & mask2, ['SpecificationName', 'SpecificationUnit',
                                                           'MatchedPropertyID', 'Ontology']].drop_duplicates()

                mapping = mapping.reset_index(drop=True)

                if len(mapping.index) == 1:
                    p_name = mapping['MatchedPropertyID'][0].replace(" ", "_")
                    p_unit = spec_unit
                    p_value = spec_value
                    p_value_orig = spec_value_orig
                    # print(mapping['Ontology'][0], p_name, p_unit, p_value, p_value_orig)

                    if mapping['Ontology'][0] == 'GR':
                        property_instance_gr(gr, p_name, p_unit, p_value_orig, someItem, offering)

                    elif mapping['Ontology'][0] == 'ECLASS':
                        property_instance(onto, gr, p_name, p_unit, p_value, p_value_orig, someItem)

                    # special case, SKOS are annotation properties
                    elif mapping['Ontology'][0] == 'SKOS':
                        skos[p_name][someItem].append(p_name)

                    # special case, only for availability property
                    elif mapping['Ontology'][0] == 'SCHEMA':
                        property_instance_schema(schemaorg, p_name, p_unit, p_value_orig, someItem, offering)

                    if p_name not in specs:
                        specs.add(spec_name)
                    else:
                        pass
                        # print('property repeated for product ' + product.productLink)
                else:
                    pass
                    # print('not found', pair)

def property_instance(onto, gr, p_name, p_unit, p_value, p_value_orig, someItem):
    # object property
    if ObjectProperty in onto[p_name].is_a:

        if p_value and p_unit:
            if not isinstance(p_value, list):
                qi = gr.QuantitativeValue()
                onto[p_name][someItem].append(qi)
                gr.hasValue[qi].append(p_value)
                gr.hasUnitOfMeasurement[qi] = [p_unit]
                onto.hasOriginalValue[qi].append(p_value_orig)

            elif len(p_value) == 2:
                qi = gr.QuantitativeValue()
                onto[p_name][someItem].append(qi)
                gr.hasMaxValue[qi].append(max(p_value))
                gr.hasMinValue[qi].append(min(p_value))
                gr.hasUnitOfMeasurement[qi] = [p_unit]
                onto.hasOriginalValue[qi].append(p_value_orig)

            else:
                for v in p_value:
                    qi = gr.QuantitativeValue()
                    onto[p_name][someItem].append(qi)
                    gr.hasValue[qi].append(v)
                    gr.hasUnitOfMeasurement[qi] = [p_unit]
                    onto.hasOriginalValue[qi].append(p_value_orig)
        else:
            qi = gr.QuantitativeValue()
            onto[p_name][someItem].append(qi)
            onto.hasOriginalValue[qi].append(p_value_orig)

    # data property
    elif p_value_orig:
        onto[p_name][someItem] = [p_value_orig]
    else:
        print("property %s could not be generated" % p_name)

def property_instance_gr(gr, p_name, p_unit, p_value, someItem, offering):
    # print('gr', p_name, p_unit, p_value, someItem)
    objectproperty_product = {}
    objectproperty_offering = {'hasEligibleQuantity': gr.QuantitativeValue}
    dataproperty_offering = {'hasCurrency': {'o': gr.PriceSpecification, 'p': gr.hasPriceSpecification},
                             'hasCurrencyValue': {'o': gr.PriceSpecification, 'p': gr.hasPriceSpecification}}

    # object property
    if ObjectProperty in gr[p_name].is_a:
        # specific case for manufacturer
        if p_name == 'hasManufacturer':
            organization = None
            for org in onto.search(iri='http://purl.org/goodrelations/v1#businessentity*'):
                if p_value == org.label[0] or p_value in org.altLabel:
                    organization = org
                    break
            if not organization:
                organization = gr.BusinessEntity(label=[p_value])
            gr[p_name][someItem] = [organization]
        elif p_name == 'hasBrand':
            brand = None
            for br in onto.search(iri='http://purl.org/goodrelations/v1#brand*'):
                if p_value == br.label[0] or p_value in br.altLabel:
                    brand = br
                    break
            if not brand:
                brand = gr.Brand(label=[p_value])
            gr[p_name][someItem] = [brand]
        else:
            object_product = objectproperty_product.get(p_name)
            object_offering = objectproperty_offering.get(p_name)
            if object_product:
                qi = object_product(label=p_value)
                gr[p_name][someItem] = [qi]

            elif object_offering:
                qi = object_offering(label=p_value)
                gr[p_name][offering] = [qi]

            else:
                print('property %s not created, update gr specific object dictionaries' % p_name)

    # data property
    else:
        obj_offering = dataproperty_offering.get(p_name)
        if obj_offering:
            if obj_offering.get('p')[offering]:
                specific_object_offering = obj_offering.get('p')[offering][0]
            else:
                specific_object_offering = obj_offering.get('o')()
                obj_offering.get('p')[offering] = [specific_object_offering]

            gr[p_name][specific_object_offering] = [p_value]

        else:
            gr[p_name][someItem] = [p_value]

def property_instance_schema(schemaorg, p_name, p_unit, p_value, someItem, offering):
    specific_objects_offering = {}

    # object property
    if ObjectProperty in schemaorg[p_name].is_a:
        if p_name == 'Availability':
            availability = None
            for av in schemaorg.ItemAvailability.instances():
                if p_value.split('/')[-1] == av.label[0]:
                    availability = av
                    break
            if not availability:
                availability = schemaorg.ItemAvailability(p_value.split('/')[-1], label=[p_value.split('/')[-1]])
            schemaorg[p_name][offering] = [availability]
        else:
            specific_object_offering = specific_objects_offering.get(p_name)
            if specific_object_offering:
                qi = specific_object_offering(label=p_value)
                schemaorg[p_name][offering] = [qi]
            else:
                print('property %s not created, update schema specific object dictionaries' % p_name)
    else:
        schemaorg[p_name][someItem] = [p_value]

def population(onto):
    print('Getting Namespaces...')
    gr = onto.get_namespace("http://purl.org/goodrelations/v1#")
    schemaorg = onto.get_namespace("http://schema.org/")
    mbse = onto.get_namespace("http://david.org/informationmodel.owl#")
    skos = onto.get_namespace("http://www.w3.org/2004/02/skos/core#")

    print('Reading Units Parsing Result...')
    df_units = pd.read_json(units_file)
    df_units = df_units.set_axis(col_df_units, axis='columns')

    print('Reading Property Mapping Result...')
    df_pp = pd.read_csv(pp_file, delimiter=";")
    if not set(col_df_pp_uv).issubset(df_pp.columns):
        print('The columns of the property mapping result file where modified')
        exit()

    print('Updating Vocabulary...')
    df_pp_uv = df_pp.loc[~pd.isna(df_pp['UserValidation']) & ((df_pp['Ontology'] == 'ECLASS') |
                                                              (pd.isna(df_pp['Ontology'])))].drop_duplicates(
        subset='RequestID')
    update_vocabulary(onto, gr, df_pp_uv)

    print('Generating Instances...')
    right = df_pp[['Source', 'ProductCategory', 'ProductClass']].drop_duplicates()
    df_pp_uv = df_pp.loc[~pd.isna(df_pp['UserValidation'])].drop_duplicates(subset='RequestID')
    df_units_r = pd.merge(df_units, right, on=['Source', 'ProductCategory'])
    generate_instances(onto, gr, mbse, schemaorg, skos, df_units_r, df_pp_uv)

    print('Saving World...')
    MyWorld.save()

if __name__ == '__main__':
    print('Loading Ontology...')
    MyWorld = World(filename="PCS/eclass11.sqlite3")
    onto = MyWorld.get_ontology("http://www.intelligent-match.de/eco11/").load()
    population(onto)


from django.test import TestCase
from django.urls import reverse

from rest_framework.views import status

from res.strings import APIEndpointPaths as Path, APIMethods, APIKeys
from res.numbers import APILimits
from api.tests import BaseViewTestCase as BTC

class BaseViewTestCase(TestCase):

    def setUp(self):
        self.non_existant_shrtzy = 'doesnotexist'
        self.invalid_shrtzy = 'a%ygb'
        self.invalid_shrtzy_2 = 'a'+'b'*APILimits.MAX_SHRTZY_LEN
        self.valid_url = 'https://www.google.com/'

        self.valid_shrtzy = BTC().make_a_request(
            method=APIMethods.POST,
            data=BTC.url_data(self.valid_url)
        ).data[APIKeys.SHRTZY]

class SHRTZYRedirectPageAcceptanceTest(BaseViewTestCase):

    def verify_response(self, response, key, expected_key_value, expected_status_code):
        self.assertEqual(expected_status_code, response.status_code)
        self.assertIsNotNone(response.data)
        self.assertEqual(expected_key_value, response.data[key])

    # ACCEPTANCE TEST - 1.1
    def test_if_shrtzy_exist_always_redirect(self):
        response = self.client.get(
            reverse(Path.SHRTZY_REDIRECT, kwargs={'shrtzy':self.valid_shrtzy})
        )

        self.assertEqual(response.status_code, status.HTTP_302_FOUND)

    # ACCEPTANCE TEST - 1.2
    def test_if_shrtzy_is_invalid_always_return_404_not_found(self):
        response = self.client.get(
            reverse(Path.SHRTZY_REDIRECT, kwargs={'shrtzy':self.invalid_shrtzy})
        )

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

        response = self.client.get(
            reverse(Path.SHRTZY_REDIRECT, kwargs={'shrtzy':self.invalid_shrtzy_2})
        )

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

    # ACCEPTANCE TEST - 1.3
    def test_if_shrtzy_does_not_exist_always_return_404_not_found(self):
        response = self.client.get(
            reverse(Path.SHRTZY_REDIRECT, kwargs={'shrtzy':self.non_existant_shrtzy})
        )

        self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)


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

import geoip2.database
from prettytable import PrettyTable

x = PrettyTable([u"序号", u"IP地址", u"国家", u"省份", u"地市"])
x.align[u"序号"] = "1"
x.padding_width = 1  # One space between column edges and contents (default)
# x.add_row(["Adelaide", 1295, 1158259, 600.5])

f = open(r'SourceIP.txt')
targets = f.readlines()
f.close()
reader = geoip2.database.Reader('GeoLite2-City.mmdb')
count = 1
NoGuangxi = []
for target in targets:
    response = reader.city(target.strip())
    city = response.city.name
    if city=='Nanning':
        city = '南宁'
    info = str(count) + ' : ' + target.strip() + '\t''\t''\t' + str(response.country.names['zh-CN']) + '\t''\t''\t' + \
           str(response.subdivisions.most_specific.names["zh-CN"]) + '\t''\t''\t' + str(response.city.name)

    if "广西壮族自治区" in info:
        # print(info)
        x.add_row([str(count),str(target.strip()), str(response.country.names['zh-CN']),
                  str(response.subdivisions.most_specific.names["zh-CN"]), str(city)])
        count = count + 1
print(x)
#     else:
#         NoGuangxi.append(info)
#     # count = count + 1
#
# for i in NoGuangxi:
#     # print(i)
#     count = count + 1

# print("地区：{}({})".format(response.continent.names["es"], response.continent.names["zh-CN"]))
#
# print("国家：{}({}) ，简称:{}".format(response.country.name,response.country.names["zh-CN"],response.country.iso_code))

# print("洲／省：{}({})".format(response.subdivisions.most_specific.name,response.subdivisions.most_specific.names["zh-CN"]))
#
# print("城市：{}({})".format(response.city.name, response.city.names["zh-CN"]))
#
# print("经度：{}，纬度{}".format(response.location.longitude,response.location.latitude))
#
# print("时区：{}".format(response.location.time_zone))
# print("邮编:{}".format(response.postal.code))


from django.conf.urls import patterns, url, include
#from django.views.generic import TemplateView, DetailView, ListView, CreateView, UpdateView, DeleteView
from reports.views import *

urlpatterns = patterns('',
    url(r'^(?P<pk>\d+)/$', ReportingView.as_view(), name='reporting'),
    url(r'^(?P<pk>\d+)/project_report/$', ProjectInfoReportView.as_view(), name='project_report'),
    url(r'^(?P<pk>\d+)/generate/$', 'reports.views.projectInfoReportView', name='proj_report'),
    # url(r'^(?P<pk>\d+)/mapping_report/$', '', name='mapping_report'),
    # url(r'^(?P<pk>\d+)/carbon_report/$', '', name='carbon_stocks_report'),
    )


from threading import Thread
from socket import socket, AF_INET, SOCK_STREAM
import exceptions as e
from collections import namedtuple
from os import getcwd, path
from time import asctime

DEFAULT_HEADERS = {'Connection': 'close'}  # Я ожидал, что их будет больше

CONTENT_TYPES = {
    '.jpg': 'image/jpeg',
    '.png': 'image/png',
    '.gif': 'image/gif',
    '.html': 'text/html',
    '.json': 'application/json'
}

class Request:
    def __init__(self, method, target, proto, headers, file):
        self.method = method
        self.target = target
        self.proto = proto
        self.headers = headers
        self.file = file

class Response:
    def __init__(self, status, reason, headers=None, body=None):
        self.status = status
        self.reason = reason
        self.headers = headers
        self.body = body

class HTTPServer:
    def __init__(self, host: str, port: int, server_name: str = 'Default HTTP server'):
        self.__host = host
        self.__port = port
        self.__server_name = server_name
        self.__path = getcwd()

    def serve_forever(self):
        self.sock = socket(AF_INET, SOCK_STREAM)

        try:
            self.sock.bind((self.__host, self.__port))
            self.sock.listen()
            print('Serving...')
            while True:
                conn, addr = self.sock.accept()
                print(f'Connected {addr}')
                # Каждого клиента направляем в отдельный поток
                Thread(target=self.handle_client, args=(conn,)).start()
        finally:
            self.sock.close()

    def handle_client(self, conn: socket):
        try:
            while True:
                # Парсим реквест
                req = self.parse_request(conn)
                print(req.method, req.target, req.proto)
                # Создаем для него нормальный респонз
                resp = self.handle_request(req)
                # И шлем его клиенту
                self.send_response(conn, resp)
        except Exception as e:
            print(f'{e.__class__.__name__}: {e}')
        finally:
            conn.close()

    def parse_request(self, conn: socket) -> Request:
        # Все чтение сокета делал через файлы, так удобнее читать построчно
        file = conn.makefile('rb')
        data = file.readline(1024*64)

        # Нагуглил, что хедеры обязаны быть в этой кодировке
        line = data.decode('iso-8859-1')
        line = line.strip().split()
        if len(line) != 3:  # Ожидаем метод, цель и протокол
            raise e.BadRequest('Request first line have to be 3 parts')
        method, target, proto = line

        # Перенаправляем / на /index.html
        if target == '/':
            target = '/index.html'

        # Других скорее всего и не будет, но на всякий
        if proto != 'HTTP/1.1':
            raise e.UnexpectedProto('Unexpected HTTP version')

        headers = self.parse_headers(file)
        host = headers.get('Host')
        if not host or host.strip() not in [self.__host, f'{self.__host}:{self.__port}']:
            raise e.BadRequest('Not found')
        return Request(method, target, proto, headers, file)

    def parse_headers(self, file) -> dict:
        """
        Читаем и сплитим строку, пока не встретим пустую,
        полученные строки кидаем в словарь
        """
        headers = {}
        while True:
            line = file.readline()

            if line in [b'\r\n', b'\n', b'']:
                return headers
            line = line.decode('iso-8859-1')
            k, v = line.strip('\r\n').split(':', 1)
            headers[k] = v

    def handle_request(self, req):
        not_found = False
        # Путь к искомому файлу записывается как `/файл`, из-за этого os.path.join
        # некорректно отрабатывает, приходится отрубать ему передний слеш
        path_ = path.join(self.__path, req.target[1:])
        if not path.exists(path_):
            # Файла нет - получаем 404
            path_ = path.join(self.__path, '404.html')
            not_found = True
        with open(path.join(path_), 'rb') as f:
            data = f.read()
        for k, v in CONTENT_TYPES.items():
            if req.target.endswith(k):
                content_type = v
                break
        else:
            content_type = 'text/html'
        return Response(404 if not_found else 200, 'Not found' if not_found else 'OK', {**DEFAULT_HEADERS, 'Date': asctime(), 'Content-length': len(data), 'Server': self.__host, 'Content-type': content_type}, data)

    def send_response(self, conn: socket, resp):
        # Опять же пишем в файл
        file = conn.makefile('wb')
        # Записываем первую строку
        file.write(f'HTTP/1.1 {resp.status} {resp.reason}\r\n'.encode('iso-8859-1'))

        # Все хедеры
        if resp.headers:
            for k, v in resp.headers.items():
                file.write(f'{k}: {v}\r\n'.encode('iso-8859-1'))

        # и разделитель
        file.write(b'\r\n')

        if resp.body:
            file.write(resp.body)
        file.flush()
        file.close()

    def send_error(self, conn: socket, err):
        pass  # TODO: implement me!

if __name__ == "__main__":
    host = input('Host (optional):') or 'localhost'
    port = input('Port (optional): ') or 80
    port = int(port)
    name = input('Server name (optional): ')
    server = HTTPServer(host, port, name)
    try:
        server.serve_forever()
    finally:
        print('Goodbye!')


import asyncio, arpeggio, discord, hjson, os, pprint, re, sys
from ithkuil.morphology.words import Factory
from ithkuil.morphology.exceptions import AnalysisException

SUB = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
SUP = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")

with open('lexicon.hjson') as f: lexicon = hjson.load(f)
with open('suffixes.hjson') as f: suffixes = hjson.load(f)
with open('biases.hjson') as f: biases = hjson.load(f)

def normalize(s):
    s = s.replace('\N{left single quotation mark}', "'")
    s = s.replace('\N{right single quotation mark}', "'")
    s = s.replace('\N{left double quotation mark}', '"')
    s = s.replace('\N{right double quotation mark}', '"')
    s = s.replace('\N{modifier letter acute accent}', '\N{acute accent}')
    s = s.replace('\N{modifier letter grave accent}', '\N{grave accent}')
    s = s.replace('\N{modifier letter small h}', 'h')
    s = re.sub('ẍ|ẋ|x̌', 'xh', s)
    s = re.sub('ḑ|đ|ḏ', 'dh', s)
    s = re.sub('ı', 'i', s)
    return s

def fix_parens(s):
    return s.replace(') (', ', ')

def lexicon_lookup(root, designation, stem_and_pattern):
    d = 0 if designation == 'IFL' else 1
    p = int(stem_and_pattern[1]) - 1
    s = int(stem_and_pattern[3]) - 1
    return lexicon_lookup_(root, d, p, s)

def lexicon_lookup_(root, d, p, s):
    print('lexicon_lookup_', root, d, p, s)
    dps = ''
    if (d, p, s) != (0, 0, 0):
        dps = ('-%s-P%dS%d' % (['IFL', 'FML'][d], p + 1, s + 1)).translate(SUB)

    root = normalize(root)
    if root not in lexicon:
        return "%s%s" % (root, dps)
    entry = lexicon[root]
    if isinstance(entry, str):
        return "%s%s" % (entry, dps)

    if isinstance(entry[0], str):
        command = entry[0]
        if command.startswith('@'):
            # Template
            res = lexicon_lookup_(command, d, p, s)
            if command == '@kh' and '/' in entry[1]:
                res = res.replace('@ relation (dominant)', entry[1].split('/')[0].strip())
                res = res.replace('@ relation (passive)', entry[1].split('/')[1].strip())
            elif len(entry) > 2:
                for i in range(1, len(entry)):
                    res = res.replace('@' + str(i), entry[i])
                return res
            return res.replace('@', entry[1])
        elif command.startswith('+'):
            # Addendum
            res = lexicon_lookup_(command[1:], d, p, s)
            res = fix_parens(res + ' (' + entry[1] + ')')
            return res
        else:
            raise ValueError('Invalid command: ' + command)

    x = entry[d]
    if isinstance(x, str):
        res = lexicon_lookup_(root, 0, p, s)
        res = fix_parens(res + ' (' + x + ')')
        return res

    x = entry[d][p]
    if isinstance(x, str):
        res = lexicon_lookup_(root, d, 0, s)
        res = fix_parens(res + ' (' + x + ')')
        return res

    return entry[d][p][s]

defaults = 'UNI DEL CSL NRM M EXS OBL STA UNFRAMED MNO FAC CTX PRC ASR PPS CNF NEU'.split()

def nice_level(deg, typ):
    return '= > < OPT MIN SPL IFR ≥ ≤'.split()[deg-1] + {1: 'ᵣ', 2: 'ₐ', 3: '₃'}[typ]

def nice_suffix(s, full_names=False, t3_adjunct=False):
    if t3_adjunct:
        return nice_gloss(s['v3c_adjunct'], full_names)

    code = s['code']
    typ = int(s['degree'][5])
    deg = int(s['degree'][-1])
    if code == 'LVL':
        return nice_level(deg, typ)

    fill_at = None
    if re.match('MD..', code):
        fill_at = suffixes[code]
        code = 'MD'

    typs = {1: '', 2: '₂', 3: '₃'}[typ]
    if code in suffixes:
        result = suffixes[code][deg-1].join('‘’') + typs
        if fill_at: result = result.replace('@', fill_at)
        return result

    degs = str(deg).translate(SUP)
    return code + typs + degs

def nice_code(key, full_names=False):
    code = key['code']
    if code[:3] in biases and full_names:
         return '*(%s)*' % (biases[code[:3]]['+' in code])
    elif code.startswith('CMP'):
        p = {
            '1': 'previously less',
            '2': 'previously more',
            '3': 'still less',
            '4': 'still more',
            '5': 'now less',
            '6': 'now more',
            '7': 'previously equal',
            '8': 'previous level unknown'
        }[code[3]]
        q = {
            'A': 'but still low',
            'B': 'but now high',
            'C': 'now also high',
        }[code[4]]
        if 'now' in p: q = q.replace('now ', '')
        return '%s[%s; %s]' % (code, p, q)
    levels = 'EQU SUR DFC OPT MIN SPL IFR SPQ SBE'.split()
    if code[:3] in levels:
        return nice_level(levels.index(code[:3]) + 1, 'ra'.index(code[3:]) + 1)
    return key['name'].lower() if full_names else key['code']

def nice_gloss(word, full_names=False):
    word = normalize(word.lower().strip('.,?!'))

    try:
        parse = Factory.parseWord(word)
        desc = parse.fullDescription()
        descType = desc['type']

        if desc['type'] == 'Bias adjunct':
            code = desc['Bias']['code']
            return nice_code(desc['Bias'], full_names)

        if desc['type'] == 'Personal adjunct':
            tags = []
            for k in desc['categories']:
                if k in desc and desc[k]['code'] not in defaults:
                    tags.append(nice_code(desc[k], full_names))
            return '-'.join(tags)

        if desc['type'] != 'Formative':
            tags = []
            for k in desc['categories']:
                if k in desc and desc[k]['code'] not in defaults:
                    tags.append(nice_code(desc[k], full_names))
            return desc['type'] + ': ' + '-'.join(tags)

        tags = []
        root = desc.pop('Root')
        designation = desc.pop('Designation')['code']
        stem_and_pattern = desc.pop('Stem and Pattern', {'code': 'P1S1'})['code']
        lex = lexicon_lookup(root, designation, stem_and_pattern)

        ir = None
        if 'Incorporated root' in desc:
            i_root = desc.pop('Incorporated root')
            i_designation = desc.pop('Designation (inc)')['code']
            i_stem_and_pattern = desc.pop('Stem and Pattern (inc)', {'code': 'P1S1'})['code']
            i_lex = lexicon_lookup(i_root, i_designation, i_stem_and_pattern)
            inc_tags = []
            for k in desc['categories']:
                if k.endswith('(inc)') and k in desc:
                    key = desc.pop(k)
                    if key['code'] not in defaults:
                        inc_tags.append(nice_code(key, full_names))
            ir = '-'.join(["*%s*" % i_lex] + inc_tags)

        for k in desc['categories']:
            if k in desc and desc[k]['code'] not in defaults:
                tags.append(nice_code(desc[k], full_names))
        res = '-'.join(["*%s*" % lex] + tags)
        if ir:
            res += '-[%s]' % ir
        if 'suffixes' in desc:
            t3_adjunct = all(s['degree'][5] == '3' for s in desc['suffixes'])
            res += ' + ' + ' + '.join(nice_suffix(s, full_names, t3_adjunct) for s in desc['suffixes'])

        return res
    except arpeggio.NoMatch as e:
        return "Couldn't parse."
    except AnalysisException as e:
        return str(e)

client = discord.Client()

def bot_result(message_content):
    cmd, *words = message_content.split()
    full_names = 'full' in cmd

    if len(words) == 1:
        return nice_gloss(words[0], full_names)
    else:
        return '\n'.join(['**__Gloss:__**'] + ['**%s**: %s' % (word.lower().strip('.,?!'), nice_gloss(word, full_names)) for word in words])

@client.event
async def on_ready():
    print('Running Discord bot:', client.user)

@client.event
async def on_message(message):
    say = lambda s: message.channel.send(s)
    if message.author == client.user: return
    if message.content.startswith('!gloss'):
        await say(bot_result(message.content))

if __name__ == '__main__':
    if sys.argv[1:]:
        print(bot_result(' '.join(sys.argv[1:])))
    else:
        client.run(os.getenv('ULAMTON_TOKEN'))


import pandas as pd
data = pd.read_csv('assembly_pondi_detail.csv')

data_1 = data[(data['rank']==1)]
data_2 = data[(data['rank']==2)]

data_1 = data_1.set_index(['YEAR','AC_NAME'])
data_2 = data_2.set_index(['YEAR','AC_NAME'])

data_1['margin'] = data_1['VOTES'] - data_2['VOTES']

data_1.reset_index().to_csv('testing.csv',index=False)


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 17 08:00:21 2020

@author: 2007006
"""
import numpy as np
import copy
import time

class xray():

    def __init__(self):
#        self.A = np.fromfile('C:\\Users\\2007006\\Desktop\\HS_TemporalNoise.raw', dtype='int16', sep="")
#        self.A = np.fromfile('C:\\Users\\2007006\\Desktop\\N8reverse_30frame.raw', dtype='int16', sep="")
        self.A = np.fromfile('C:\\Users\\2007006\\Desktop\\Input\\HOS9B57684-05-VGH12-Vcom-6-VGL-2\\TemporalNoise_W3072H3072F30.raw', dtype='int16', sep="")
#        self.A = np.fromfile('C:\\Users\\2007006\\Desktop\\Input\\HOS9257676-17-VGH12-Vcom-6-VGL-2\\TemporalNoise_W3072H3072F30.raw', dtype='int16', sep="")
        self.ALL_median = np.median(self.A)
        self.std = np.std(self.A)
#========= get the pic_length of pic ==============
        self.raw_length = int((np.size(self.A))/9437184)

#========= read the current numb of picture (avoid to use too many memory)==============
    def read_arr(self,numb):
        arr =np.zeros((3072,3072))
        temp =self.A[0+numb*9437184:9437184+numb*9437184]
        arr = np.reshape(temp,(3072,3072))
        return arr
#=========  1280*1280 translate to one line ==============
    def arr_to_row(self,arr):
        out_arr =[]
        for i in range(3072):
            for j in range(3072):
                out_arr.append((arr[i,j]))
        return out_arr

#========= save the one line data to file==============

    def saveto_raw(self,out_arr,filename):
        out_arr = np.asarray(out_arr, dtype=np.int16)
        path = 'C:\\Users\\2007006\\Desktop\\'+ filename+'.raw'
        out_arr.astype('int16').tofile(path)

    def oneframe3072(self,arr):
        pic1=arr
# =====================減去中位數並且mark defect===============
        pic2 = copy.deepcopy(pic1)
        pic3 = copy.deepcopy(pic1)
        pic3 = pic3 -self.ALL_median
        pic3=np.where(pic3>(3*self.std),666,pic3)
        pic3=np.where(pic3<(-3*self.std),666,pic3)
# =====================處理data line noise==================
        aa=np.zeros((3072,3072))
        for j in range(384):
            y1=0+8*j
            y2=8+8*j
            tt =pic3[:,y1:y2]
            aa[:,y1:y2] = np.mean(tt[np.where(tt != 666)])
            print((np.mean(tt[np.where(tt != 666)])))
        pic2[np.where(pic3 != 666)] -= aa[np.where(pic3 != 666)]

# ==========================================================
        pic2[np.where(pic3 != 666)] -=self.ALL_median
        N = 256
        devide = int(3072/N)
# ==========================================================
        for i in range (3072):
            for j in range(devide):
                ind = 0+j*N
                ind1 = N+j*N
#                x = np.where((pic2[i,ind:ind1]<3*std))
#                x = np.where((pic2[i,ind:ind1]>-3*std))
                temp_pic2 =pic2[i,ind:ind1]
                temp = np.median(temp_pic2[:])
                temp_pic2[np.where(temp_pic2 != 666)]  -= temp
#                pic2[i,ind:ind1] -= temp
                pic2[i,ind:ind1] =temp_pic2
        pic2[np.where(pic3 != 666)] += self.ALL_median
        pic2 = np.where(pic2<0,0,pic2)
        return pic2

    def reverse_oneframe3072(self,arr): #反轉處理先後順序(肇驛建議)
        pic1=arr
# =====================減去中位數並且mark defect===============
        pic2 = copy.deepcopy(pic1)
        pic3 = copy.deepcopy(pic1)
        pic3 = pic3 -self.ALL_median
        pic3=np.where(pic3>(3*self.std),666,pic3)
        pic3=np.where(pic3<(-3*self.std),666,pic3)
## ==========================================================
        pic2[np.where(pic3 != 666)] -=self.ALL_median
        N = 256
        devide = int(3072/N)
## ==========================================================
        for i in range (3072):
            for j in range(devide):
                ind = 0+j*N
                ind1 = N+j*N
#                x = np.where((pic2[i,ind:ind1]<3*std))
#                x = np.where((pic2[i,ind:ind1]>-3*std))
                temp_pic2 =pic2[i,ind:ind1]
                temp = np.median(temp_pic2[:])
                temp_pic2[np.where(temp_pic2 != 666)]  -= temp
#                pic2[i,ind:ind1] -= temp
                pic2[i,ind:ind1] =temp_pic2

# =====================處理data line noise==================
        aa=np.zeros((3072,3072))
        for j in range(384):
            y1=0+8*j
            y2=8+8*j
            tt =pic3[:,y1:y2]
            aa[:,y1:y2] = np.mean(tt[np.where(tt != 666)])
    #        print((np.mean(tt[np.where(tt != 666)])))
        pic2[np.where(pic3 != 666)] -= aa[np.where(pic3 != 666)]
        pic2[np.where(pic3 != 666)] += self.ALL_median
        pic2 = np.where(pic2<0,0,pic2)
        return pic2

if __name__ == '__main__':

    time_start=time.time()
    A = xray()
    narry = np.array([])

    for i in range(A.raw_length):
#    for i in range(1):

#        pic2 = A.oneframe3072(A.arr[i])
        pic = A.read_arr(i)
        pic=pic.astype(float)
        pic2 = A.oneframe3072(pic)

        onelinearr = A.arr_to_row(pic2)

        narry =np.append(narry,onelinearr)

    A.saveto_raw(narry,'outfilename')

    del onelinearr,narry

    time_end=time.time()
    process_time =np.round((time_end-time_start),2)
    print(process_time)


_base_ = [
    '../_base_/models/resnet50_cifar.py',
    '../_base_/datasets/cifar100_bs16.py',
    '../_base_/default_runtime.py'
]

model = dict(head=dict(num_classes=100))

optimizer = dict(type='SGD', lr=0.1, momentum=0.9, weight_decay=0.0005)

#optimizer
optimizer_config = dict(grad_clip=None)
#learning policy
runner = dict(type='EpochBasedRunner', max_epochs=2)
lr_config = dict(policy='CosineAnnealing', min_lr=0)


import torch
import torch.nn.functional as F
from torch import nn

from my_common.my_helper import is_positive_int
from util.constants import Activation, TaskType, LossType, Regularization

def last_dims_tuple(xx, k=1):
    assert torch.is_tensor(xx)
    return tuple(range(k, xx.ndim))

def get_act_func(act):
    """
    Return the corresponding torch activation function given its name
    """
    assert isinstance(act, Activation)
    if act == Activation.ReLU:
        return F.relu  # Used in RP-Paper and original Set Transformer
    elif act == Activation.Tanh:
        return torch.tanh  # torch.functional.tanh is deprecated
    elif act == Activation.Sigmoid:
        return torch.sigmoid
    else:
        raise NotImplementedError(f"Haven't yet implemented models with {act.name} activation.")

def get_loss_func(task_type, loss_type=None):
    """
    Return the loss function. If loss type is given, return the corresponding loss function;
    otherwise return the default loss function associated with the given task type.
    """
    assert isinstance(task_type, TaskType)

    if loss_type is not None:
        assert isinstance(loss_type, LossType)
        if loss_type == LossType.mse:
            assert task_type == TaskType.regression
            return nn.MSELoss(reduction='none')
        elif loss_type == LossType.mae:
            assert task_type == TaskType.regression
            return nn.L1Loss(reduction='none')
        else:
            raise NotImplementedError(f"Haven't yet implemented models with {loss_type.name} loss.")
    else:
        if task_type == TaskType.binary_classification:
            return nn.BCEWithLogitsLoss(reduction='none')
        elif task_type in (TaskType.multi_classification, TaskType.node_classification):
            return nn.CrossEntropyLoss(reduction='none')
        elif task_type == TaskType.regression:
            return nn.MSELoss(reduction='none')
        else:
            raise NotImplementedError(f"Haven't yet implemented models for {task_type.name} task.")

class Two_Phase_Regularization_Scheduler:

    def __init__(self, cfg):
        assert isinstance(cfg.pull_down, bool)
        assert is_positive_int(cfg.milestone)
        assert isinstance(cfg.regularization, Regularization)

        self.pull_down = cfg.pull_down
        self.milestone = cfg.milestone
        self.regularization = cfg.regularization

    def step(self, model, epoch):
        if self.pull_down:
            if epoch >= self.milestone and model.regularizer.regularization != Regularization.none:
                model.regularizer.regularization = Regularization.none
        elif epoch <= self.milestone:  # when epoch > self.milestone, model.regularization is turned on by default
            if epoch == self.milestone:
                model.regularizer.regularization = self.regularization
            else:
                model.regularizer.regularization = Regularization.none


from flask import Flask, render_template, request, jsonify
import os
import sys
from random import randint
import requests, json

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/parse',methods=['POST', 'GET'] )
def extract():
    text=str(request.form.get('value1'))
    payload = json.dumps({"sender": "Rasa","message": text})

    headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    response = requests.request("POST", url="http://localhost:5005/webhooks/rest/webhook", headers=headers, data=payload)

    response=response.json()
    resp=[]

    for i in range(len(response)):
        try:
            resp.append(response[i]['text'])
            print(result)
        except:
            continue
    result=resp

    return render_template('index.html', result=result,text=text)
if __name__ == "__main__":
    app.run(debug=True)


from django.shortcuts import render, redirect
from .models import User

# Create your views here.

def index(request):
    context = {
        'users': User.objects.all()
    }
    return render(request, 'index.html', context)

def add_user(request):
    if request.method == "POST":
        first_name=request.POST['first_name']
        last_name=request.POST['last_name']
        email_address=request.POST['email_address']
        age=request.POST['age']
        User.objects.create(first_name=first_name, last_name=last_name, email_address=email_address, age=age)
    return redirect('/')

def delete_user(request, id):
    print('USER TO DELETE: ', id)
    user_to_delete = User.objects.get(id=id)
    if user_to_delete:
        user_to_delete.delete()
    return redirect('/')


# csv

import csv

def campo(t,c):
	r=[]
	campos=t[0]
	c=campos.index(c)
	for i in t:
		r.append(i[c])
	return r

a=[]
with open('ieo.txt', 'r') as csvfile:
    r = csv.reader(csvfile, delimiter=';')
    for i in r:
    	a.append(i)

b=[a[0]]
for i in a:
	if i[0]=='JULIO CESAR SAR VAZQUEZ':
		b.append(i)

print(len(b),'datos')
#print(b[0])

tallas=campo(b,'TALLA')
especies=campo(b,'ESP_CAT')

#print(especies)
#print(tallas)

c=[];
for i in range(len(especies)):
	c.append({
		'especie':especies[i],
		'talla':tallas[i],
		})
print(c)


#
# Copyright (c) 2012 by Pawel Tomulik
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE

env = Environment()
Export(['env'])

VariantDir('build/doc', 'doc', duplicate = 0)
SConscript('build/doc/SConscript')

# Run particular example
for n in range(1,2):
    exN = 'ex%d' % n
    env.AlwaysBuild(env.Alias(exN))
    if exN in COMMAND_LINE_TARGETS:
        env.SConscript('examples/%s/SConscript' % exN)

env.AlwaysBuild(env.Alias('unittest'))
if 'unittest' in COMMAND_LINE_TARGETS:
    import sys
    python = env.Detect('python')
    if python:
        path = ':'.join(sys.path)
        #unittestflags = "-v"
        unittestflags = ""
        discoverflags = "-p 'test*.py'"
        testcom = 'PYTHONPATH=%s %s -m unittest discover %s %s' \
                % (path, python, unittestflags, discoverflags)
        env.Execute(testcom, "Running unit tests")


from django.conf.urls import url, include

from . import views

urlpatterns = [
    url(r'^index/', views.index, name='index'),
    url(r'^process_word', views.process_word, name='process'),
    url(r'^feature/', views.feature, name='features'),
    url(r'^login/', views.login, name='login'),
    url(r'^$', views.registration, name='registration'),
]


import asyncio
import functools
from abc import abstractmethod, ABCMeta
import aioredis

import pydatacoll.utils.logger as my_logger
from pydatacoll.utils.func_container import ParamFunctionContainer

logger = my_logger.get_logger('BaseModule')

class BaseModule(ParamFunctionContainer, metaclass=ABCMeta):
    def __init__(self, io_loop: asyncio.AbstractEventLoop = None,
                 redis_pool: aioredis.RedisPool = None):
        super().__init__()
        self.io_loop = io_loop or asyncio.get_event_loop()
        self.redis_pool = redis_pool
        self._redis_pool = self.redis_pool
        if self.redis_pool is None:
            self.redis_pool = self._redis_pool = self.io_loop.run_until_complete(
                    functools.partial(
                            aioredis.create_pool, ('localhost', 6379), db=1, minsize=5, maxsize=10, encoding='utf-8')())
        self.initialized = False
        self.sub_client = None
        self.sub_channels = list()
        self.channel_router = dict()
        self._register_channel()
        # logger.info('plugin %s initialized', type(self).__name__)

    def _register_channel(self):
        for fun_name, args in self.module_arg_dict.items():
            if 'channel' not in args:
                raise Exception("wrong param_function prototype, need param: 'channel'")
            self.channel_router[args['channel']] = getattr(self, fun_name)

    async def install(self):
        try:
            self.sub_client = await self.redis_pool.acquire()
            self.sub_channels = await self.sub_client.psubscribe(*[a['channel'] for a in self.module_arg_dict.values()])
            for channel in self.sub_channels:
                asyncio.ensure_future(self._msg_reader(channel))
            await self.start()
            self.initialized = True
            logger.info('plugin %s installed', type(self).__name__)
        except Exception as e:
            logger.error('plugin %s install failed: %s', type(self).__name__, repr(e), exc_info=True)

    async def uninstall(self):
        try:
            await self.stop()
            await self.sub_client.punsubscribe(*[a['channel'] for a in self.module_arg_dict.values()])
            self.redis_pool.release(self.sub_client)
            await self._redis_pool.clear()
            self.initialized = False
            logger.info('plugin %s uninstalled', type(self).__name__)
        except Exception as e:
            logger.error('plugin %s uninstall failed: %s', type(self).__name__, repr(e), exc_info=True)

    async def _msg_reader(self, ch):
        while await ch.wait_message():
            real_channel, msg = await ch.get_json()
            channel = ch.name.decode()
            # logger.debug("%s channel[%s] Got Message:%s", type(self).__name__, channel, msg)
            self.io_loop.create_task(self.channel_router[channel](real_channel, msg))
        logger.debug('%s quit msg_reader!', type(self).__name__)

    @abstractmethod
    async def start(self):
        pass

    @abstractmethod
    async def stop(self):
        pass


# Link:
# IsDone: 0
def BinarySearchIterative(numbersList, value):
    low = 0
    high = len(numbersList) - 1
    while low <= high:
        mid = (low + high) // 2
        if numbersList[mid] > value: high = mid - 1
        elif numbersList[mid] < value: low = mid + 1
        else: return mid
    return -1

A = [534, 246, 933, 127, 277, 321, 454, 565, 220]
print(BinarySearchIterative(A, 277))


import os
import sys
import json
import plotly.io as pio

args = []
if len(sys.argv) == 2 :
    args = sys.argv[1].split()
elif len(sys.argv) > 1 :
    args = sys.argv

root = os.getcwd()
dirIn = os.path.join(root, 'test', 'image', 'mocks')
dirOut = os.path.join(root, 'build', 'test_images')

if '=' in args :
    args = args[args.index('=') + 1:]
    dirOut = os.path.join(root, 'test', 'image', 'baselines')
    print('output to', dirOut)

pio.templates.default = 'none'
pio.kaleido.scope.plotlyjs = os.path.join(root, 'build', 'plotly.js')
# TODO: specify local mathjax and plotly-geo-assets files?

_credentials = open(os.path.join(root, 'build', 'credentials.json'), 'r')
pio.kaleido.scope.mapbox_access_token = json.load(_credentials)['MAPBOX_ACCESS_TOKEN']
_credentials.close()

ALL_MOCKS = [os.path.splitext(a)[0] for a in os.listdir(dirIn) if a.endswith('.json')]
ALL_MOCKS.sort()

if len(args) > 0 :
    allNames = [a for a in args if a in ALL_MOCKS]
else :
    allNames = ALL_MOCKS

# gl2d pointcloud and other non-regl gl2d mock(s)
# must be tested in certain order to work on CircleCI;
#
# gl-shader appears to conflict with regl.
# We suspect that the lone gl context on CircleCI is
# having issues with dealing with the two different
# program binding algorithm.
#
# The problem will be solved by switching all our
# WebGL-based trace types to regl.
#
# More info here:
# https://github.com/plotly/plotly.js/pull/1037

LAST = [
    'gl2d_pointcloud-basic',
    'gl2d_heatmapgl',
    'gl2d_heatmapgl_discrete'
]

HAD = [item in allNames for item in LAST]

allNames = [a for a in allNames if a not in LAST]

allNames += [item for item, had_item in zip(LAST, HAD) if had_item]

# unable to generate baselines for the following mocks
blacklist = [
    'mapbox_density0-legend',
    'mapbox_osm-style'
]
allNames = [a for a in allNames if a not in blacklist]

if len(allNames) == 0 :
    print('error: Nothing to create!')
    sys.exit(1)

failed = []
for name in allNames :
    print(name)

    created = False

    MAX_RETRY = 2 # 1 means retry once
    for attempt in range(0, MAX_RETRY + 1) :
        with open(os.path.join(dirIn, name + '.json'), 'r') as _in :
            fig = json.load(_in)

            width = 700
            height = 500
            if 'layout' in fig :
                layout = fig['layout']
                if 'autosize' not in layout or layout['autosize'] != True :
                    if 'width' in layout :
                        width = layout['width']
                    if 'height' in layout :
                        height = layout['height']

            try :
                pio.write_image(
                    fig=fig,
                    file=os.path.join(dirOut, name + '.png'),
                    width=width,
                    height=height,
                    validate=False
                )
                created = True
            except Exception as e :
                print(e)
                if attempt < MAX_RETRY :
                    print('retry', attempt + 1, '/', MAX_RETRY)
                else :
                    failed.append(name)

        if(created) : break

if len(failed) > 0 :
    print('Failed at :')
    print(failed)
    sys.exit(1)


import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator
from SparseContext import SparseContext
import warnings
warnings.warn("deprecated", DeprecationWarning)

class LazyRuleClassifier(BaseEstimator):

    def __init__(self, criterion="gini",
                 min_rule_supp=2, max_rule_size=4, num_rules=5):
        self.criterion = criterion
        self.max_rule_size = max_rule_size
        self.min_rule_supp = min_rule_supp
        self.closed_intents = {}
        self.num_rules = num_rules

    def get_params(self, deep=True):
        return BaseEstimator.get_params(self, deep=deep)

    def set_params(self, **params):
        return BaseEstimator.set_params(self, **params)

    def fit(self, train_df, y):
        self.train_df, self.y = train_df, y

        self.attr_names = train_df.columns
        self.train_df.columns = train_df.columns
        self.train_context = SparseContext(from_dataframe=True, dataframe=pd.concat([train_df, y], axis=1))
        print(self.train_context.table[:10])

    def readable_rule_set(self, best_closed_itemsets):
        return '\n'.join([','.join([self.attr_names[attr - 1] for attr in itemset]) + ' -> ' + str(properties['class']) + \
                          ' (' + str(round(properties['criterion'], 3)) + ')'
                          for itemset, properties in best_closed_itemsets.items()])

    def predict(self, test_df):
        predictions = []
        self.test_df = test_df
        self.test_context = SparseContext(from_dataframe=True, dataframe=test_df)
        print(self.test_context.table)
        for i, test_obj in self.test_df.iterrows():

            test_attributes = np.where(np.array(test_obj) == 1)[0]
            # proj_cxt = self.train_context.project(test_attributes)

            # print(test_attributes)

            # proj_cxt = [(ix, desc.intersection(set(test_attributes)), target) for (ix, desc, target) in self.train_context.table]
            proj_cxt = self.train_context.project(test_attributes)
            # print(proj_cxt.table)

            # best_rules = proj_cxt.top_n_rules_fcbo(min_abs_supp=self.min_rule_supp, max_rule_size=self.max_rule_size,
            #                                        num_rules=self.num_rules)
            best_rules = proj_cxt.top_n_rules_inclose(min_abs_supp=self.min_rule_supp, max_rule_size=self.max_rule_size,
                                                   num_rules=self.num_rules)
            print(i)
            # print(self.readable_rule_set(best_rules))
            votes = [properties['class'] for properties
                     in best_rules.values()]

            pred = max(set(votes), key=votes.count)
            predictions.append(pred)
        return np.array(predictions)

if __name__ == '__main__':
    # load a toy example
    train_df = pd.read_csv('../input/toy_fruits_train.csv', index_col=0)
    test_df = pd.read_csv('../input/toy_fruits_test.csv', index_col=0)
    y = train_df['class']
    train_df.drop(['class'], axis=1, inplace=True)

    # add a negation for each attribute
    # for col in train_df.columns:
    #     train_df['not_' + col] = 1 - train_df[col]
    # for col in test_df.columns:
    #     test_df['not_' + col] = 1 - test_df[col]

    #
    print(train_df)
    print(test_df)

    clf = LazyRuleClassifier(criterion="gini",
                 min_rule_supp=1, max_rule_size=10, num_rules=5)

    clf.fit(train_df, y)

    print(clf.train_context.table)
    print("Predictions", clf.predict(test_df))


import math

N, D = map(int, input().split())
table = [[int(i) for i in input().split()] for _ in range(N)]

ans = 0

for i in table:
    if math.sqrt(i[0] ** 2 + i[1] ** 2) <= D:
        ans += 1

print(ans)


import sqlite3
import sys
import re

class Key:
    def getKey(self,mj):
        # コマンドラインより検索ワード取得
        print("please write your ID or name(English) >")
        rpas = sys.stdin.readline()
        pas = rpas.rstrip("\n")
        idnum = "nodata"

        # 検索ワード1文字目が数字ならTrue
        a = re.match(r'\d+', pas)
        if a:
            sql = "select * from keyset where id == " + pas
        else:
            sql = "select * from keyset where name == \"" + pas + "\""

        # 同フォルダ内のdbkey.dbのDBを展開
        con = sqlite3.connect("dbkey.db", isolation_level=None)

        # table:keysetのhitした内容取得
        c = con.cursor()
        c.execute(sql)
        for row1 in c:
            idnum = row1[0]
            name = row1[1]
            left_ltx = row1[2]
            left_lty = row1[3]
            left_rbx = row1[4]
            left_rby = row1[5]
            right_ltx = row1[6]
            right_lty = row1[7]
            right_rbx = row1[8]
            right_rby = row1[9]
            pose = row1[10]

        if idnum == "nodata":
            print("something wrong.")
            return (False,'none','none')
        else:
            mj.set_lock_position(left_ltx,
                                     left_lty,
                                     left_rbx,
                                     left_rby,
                                     right_ltx,
                                     right_lty,
                                     right_rbx,
                                     right_rby)
            return (True,pose,name)


# Escrever o codigo aqui
salaAtual = 1
contadorDeTentativas = 0
perdeu = False

def Escolhercaminho():
  global salaAtual
  global contadorDeTentativas
  global perdeu

  contadorDeTentativas += 1

while (salaAtual < 9):
  Escolhercaminho()
else:
  print("Voce conseguiu sair da dungeon com apenas {} tentativas".format(contadorDeTentativas))


# uncompyle6 version 3.7.4
# Python bytecode 3.7 (3394)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build\bdist.win-amd64\egg\xl_tensorflow\metrics\rafaelpadilla\Evaluator.py
# Compiled at: 2020-04-17 23:16:16
# Size of source mod 2**32: 23775 bytes
import os, sys
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
from .BoundingBox import *
from .BoundingBoxes import *
from .utils import *
from xl_tool.augmentation.image.general.annonation import get_bndbox

class Evaluator:

    def GetPascalVOCMetrics(self, boundingboxes, IOUThreshold=0.5, method=MethodAveragePrecision.EveryPointInterpolation):
        """Get the metrics used by the VOC Pascal 2012 challenge.
        Get
        Args:
            boundingboxes: Object of the class BoundingBoxes representing ground truth and detected
            bounding boxes;
            IOUThreshold: IOU threshold indicating which detections will be considered TP or FP
            (default value = 0.5);
            method (default = EveryPointInterpolation): It can be calculated as the implementation
            in the official PASCAL VOC toolkit (EveryPointInterpolation), or applying the 11-point
            interpolatio as described in the paper "The PASCAL Visual Object Classes(VOC) Challenge"
            or EveryPointInterpolation"  (ElevenPointInterpolation);
        Returns:
            A list of dictionaries. Each dictionary contains information and metrics of each class.
            The keys of each dictionary are:
            dict['class']: class representing the current dictionary;
            dict['precision']: array with the precision values;
            dict['recall']: array with the recall values;
            dict['AP']: average precision;
            dict['interpolated precision']: interpolated precision values;
            dict['interpolated recall']: interpolated recall values;
            dict['total positives']: total number of ground truth positives;
            dict['total TP']: total number of True Positive detections;
            dict['total FP']: total number of False Negative detections;
        """
        ret = []
        groundTruths = []
        detections = []
        classes = []
        for bb in boundingboxes.getBoundingBoxes():
            if bb.getBBType() == BBType.GroundTruth:
                groundTruths.append([
                 bb.getImageName(),
                 bb.getClassId(), 1,
                 bb.getAbsoluteBoundingBox(BBFormat.XYX2Y2)])
            else:
                detections.append([
                 bb.getImageName(),
                 bb.getClassId(),
                 bb.getConfidence(),
                 bb.getAbsoluteBoundingBox(BBFormat.XYX2Y2)])
            if bb.getClassId() not in classes:
                classes.append(bb.getClassId())

        classes = sorted(classes)
        for c in classes:
            dects = []
            [dects.append(d) for d in detections if d[1] == c]
            gts = []
            [gts.append(g) for g in groundTruths if g[1] == c]
            npos = len(gts)
            dects = sorted(dects, key=(lambda conf: conf[2]), reverse=True)
            TP = np.zeros(len(dects))
            FP = np.zeros(len(dects))
            det = Counter([cc[0] for cc in gts])
            for key, val in det.items():
                det[key] = np.zeros(val)

            for d in range(len(dects)):
                gt = [gt for gt in gts if gt[0] == dects[d][0]]
                iouMax = sys.float_info.min
                for j in range(len(gt)):
                    iou = Evaluator.iou(dects[d][3], gt[j][3])
                    if iou > iouMax:
                        iouMax = iou
                        jmax = j

                if iouMax >= IOUThreshold:
                    if det[dects[d][0]][jmax] == 0:
                        TP[d] = 1
                        det[dects[d][0]][jmax] = 1
                    else:
                        FP[d] = 1
                else:
                    FP[d] = 1

            acc_FP = np.cumsum(FP)
            acc_TP = np.cumsum(TP)
            rec = acc_TP / npos + 1e-08
            prec = np.divide(acc_TP, acc_FP + acc_TP + 1e-08)
            if method == MethodAveragePrecision.EveryPointInterpolation:
                ap, mpre, mrec, ii = Evaluator.CalculateAveragePrecision(rec, prec)
            else:
                ap, mpre, mrec, _ = Evaluator.ElevenPointInterpolatedAP(rec, prec)
            r = {'class':c,
             'precision':prec,
             'recall':rec,
             'AP':ap,
             'interpolated precision':mpre,
             'interpolated recall':mrec,
             'total positives':npos,
             'total TP':np.sum(TP),
             'total FP':np.sum(FP)}
            ret.append(r)

        return ret

    def PlotPrecisionRecallCurve(self, boundingBoxes, IOUThreshold=0.5, method=MethodAveragePrecision.EveryPointInterpolation, showAP=False, showInterpolatedPrecision=False, savePath=None, showGraphic=True):
        """PlotPrecisionRecallCurve
        Plot the Precision x Recall curve for a given class.
        Args:
            boundingBoxes: Object of the class BoundingBoxes representing ground truth and detected
            bounding boxes;
            IOUThreshold (optional): IOU threshold indicating which detections will be considered
            TP or FP (default value = 0.5);
            method (default = EveryPointInterpolation): It can be calculated as the implementation
            in the official PASCAL VOC toolkit (EveryPointInterpolation), or applying the 11-point
            interpolatio as described in the paper "The PASCAL Visual Object Classes(VOC) Challenge"
            or EveryPointInterpolation"  (ElevenPointInterpolation).
            showAP (optional): if True, the average precision value will be shown in the title of
            the graph (default = False);
            showInterpolatedPrecision (optional): if True, it will show in the plot the interpolated
             precision (default = False);
            savePath (optional): if informed, the plot will be saved as an image in this path
            (ex: /home/mywork/ap.png) (default = None);
            showGraphic (optional): if True, the plot will be shown (default = True)
        Returns:
            A list of dictionaries. Each dictionary contains information and metrics of each class.
            The keys of each dictionary are:
            dict['class']: class representing the current dictionary;
            dict['precision']: array with the precision values;
            dict['recall']: array with the recall values;
            dict['AP']: average precision;
            dict['interpolated precision']: interpolated precision values;
            dict['interpolated recall']: interpolated recall values;
            dict['total positives']: total number of ground truth positives;
            dict['total TP']: total number of True Positive detections;
            dict['total FP']: total number of False Negative detections;
        """
        results = self.GetPascalVOCMetrics(boundingBoxes, IOUThreshold, method)
        result = None
        for result in results:
            if result is None:
                raise IOError('Error: Class %d could not be found.' % classId)
            classId = result['class']
            precision = result['precision']
            recall = result['recall']
            average_precision = result['AP']
            mpre = result['interpolated precision']
            mrec = result['interpolated recall']
            npos = result['total positives']
            total_tp = result['total TP']
            total_fp = result['total FP']
            plt.close()
            if showInterpolatedPrecision:
                if method == MethodAveragePrecision.EveryPointInterpolation:
                    plt.plot(mrec, mpre, '--r', label='Interpolated precision (every point)')
                else:
                    if method == MethodAveragePrecision.ElevenPointInterpolation:
                        nrec = []
                        nprec = []
                        for idx in range(len(mrec)):
                            r = mrec[idx]
                            if r not in nrec:
                                idxEq = np.argwhere(mrec == r)
                                nrec.append(r)
                                nprec.append(max([mpre[int(id)] for id in idxEq]))

                        plt.plot(nrec, nprec, 'or', label='11-point interpolated precision')
                    else:
                        plt.plot(recall, precision, label='Precision')
                        plt.xlabel('recall')
                        plt.ylabel('precision')
                        if showAP:
                            ap_str = '{0:.2f}%'.format(average_precision * 100)
                            plt.title('Precision x Recall curve \nClass: %s, AP: %s' % (str(classId), ap_str))
                        else:
                            plt.title('Precision x Recall curve \nClass: %s' % str(classId))
                    plt.legend(shadow=True)
                    plt.grid()
                    if savePath is not None:
                        plt.savefig(os.path.join(savePath, classId + '.png'))
                if showGraphic is True:
                    plt.show()
                    plt.pause(0.05)

        return results

    @staticmethod
    def CalculateAveragePrecision(rec, prec):
        mrec = []
        mrec.append(0)
        [mrec.append(e) for e in rec]
        mrec.append(1)
        mpre = []
        mpre.append(0)
        [mpre.append(e) for e in prec]
        mpre.append(0)
        for i in range(len(mpre) - 1, 0, -1):
            mpre[i - 1] = max(mpre[(i - 1)], mpre[i])

        ii = []
        for i in range(len(mrec) - 1):
            if mrec[1:][i] != mrec[0:-1][i]:
                ii.append(i + 1)

        ap = 0
        for i in ii:
            ap = ap + np.sum((mrec[i] - mrec[(i - 1)]) * mpre[i])

        return [ap, mpre[0:len(mpre) - 1], mrec[0:len(mpre) - 1], ii]

    @staticmethod
    def ElevenPointInterpolatedAP(rec, prec):
        mrec = []
        [mrec.append(e) for e in rec]
        mpre = []
        [mpre.append(e) for e in prec]
        recallValues = np.linspace(0, 1, 11)
        recallValues = list(recallValues[::-1])
        rhoInterp = []
        recallValid = []
        for r in recallValues:
            argGreaterRecalls = np.argwhere(mrec[:] >= r)
            pmax = 0
            if argGreaterRecalls.size != 0:
                pmax = max(mpre[argGreaterRecalls.min():])
            recallValid.append(r)
            rhoInterp.append(pmax)

        ap = sum(rhoInterp) / 11
        rvals = []
        rvals.append(recallValid[0])
        [rvals.append(e) for e in recallValid]
        rvals.append(0)
        pvals = []
        pvals.append(0)
        [pvals.append(e) for e in rhoInterp]
        pvals.append(0)
        cc = []
        for i in range(len(rvals)):
            p = (rvals[i], pvals[(i - 1)])
            if p not in cc:
                cc.append(p)
            p = (
             rvals[i], pvals[i])
            if p not in cc:
                cc.append(p)

        recallValues = [i[0] for i in cc]
        rhoInterp = [i[1] for i in cc]
        return [ap, rhoInterp, recallValues, None]

    @staticmethod
    def _getAllIOUs(reference, detections):
        ret = []
        bbReference = reference.getAbsoluteBoundingBox(BBFormat.XYX2Y2)
        for d in detections:
            bb = d.getAbsoluteBoundingBox(BBFormat.XYX2Y2)
            iou = Evaluator.iou(bbReference, bb)
            ret.append((iou, reference, d))

        return sorted(ret, key=(lambda i: i[0]), reverse=True)

    @staticmethod
    def iou(boxA, boxB):
        if Evaluator._boxesIntersect(boxA, boxB) is False:
            return 0
        interArea = Evaluator._getIntersectionArea(boxA, boxB)
        union = Evaluator._getUnionAreas(boxA, boxB, interArea=interArea)
        iou = interArea / union
        assert iou >= 0
        return iou

    @staticmethod
    def _boxesIntersect(boxA, boxB):
        if boxA[0] > boxB[2]:
            return False
        if boxB[0] > boxA[2]:
            return False
        if boxA[3] < boxB[1]:
            return False
        if boxA[1] > boxB[3]:
            return False
        return True

    @staticmethod
    def _getIntersectionArea(boxA, boxB):
        xA = max(boxA[0], boxB[0])
        yA = max(boxA[1], boxB[1])
        xB = min(boxA[2], boxB[2])
        yB = min(boxA[3], boxB[3])
        return (xB - xA + 1) * (yB - yA + 1)

    @staticmethod
    def _getUnionAreas(boxA, boxB, interArea=None):
        area_A = Evaluator._getArea(boxA)
        area_B = Evaluator._getArea(boxB)
        if interArea is None:
            interArea = Evaluator._getIntersectionArea(boxA, boxB)
        return float(area_A + area_B - interArea)

    @staticmethod
    def _getArea(box):
        return (box[2] - box[0] + 1) * (box[3] - box[1] + 1)

def map_raf_from_lists(detections, ground_truths, iou_threshold=0.5, box_format='xywh', method='interpolation'):
    """
    Args:
        detections:[[image_id,[[label,confidence,x,y,w,h],[label,confidence,x,y,w,h]]],]
        ground_truths:[[image_id,[[label,x,y,w,h],[label,x,y,w,h]]],]
        iou_threshold:

    Returns:

    """
    box_format = BBFormat.XYWH if box_format == 'xywh' else BBFormat.XYX2Y2
    method = MethodAveragePrecision.EveryPointInterpolation if method == 'interpolation' else MethodAveragePrecision.ElevenPointInterpolation
    allBoundingBoxes = BoundingBoxes()
    for boxes in ground_truths:
        image_id = boxes[0]
        for box in boxes[1]:
            idClass = box[0]
            x = float(box[1])
            y = float(box[2])
            w = float(box[3])
            h = float(box[4])
            bb = BoundingBox(image_id,
              idClass,
              x,
              y,
              w,
              h,
              (CoordinatesType.Absolute),
              (416, 416), (BBType.GroundTruth),
              format=box_format)
            allBoundingBoxes.addBoundingBox(bb)

    for boxes in detections:
        image_id = boxes[0]
        for box in boxes[1]:
            idClass = box[0]
            confidence = float(box[1])
            x = float(box[2])
            y = float(box[3])
            w = float(box[4])
            h = float(box[5])
            bb = BoundingBox(image_id,
              idClass,
              x,
              y,
              w,
              h,
              (CoordinatesType.Absolute),
              (416, 416), (BBType.Detected),
              confidence,
              format=box_format)
            allBoundingBoxes.addBoundingBox(bb)

    evaluator = Evaluator()
    metricsPerClasses = evaluator.GetPascalVOCMetrics(allBoundingBoxes, IOUThreshold=iou_threshold,
      method=method)
    acc_AP = 0
    validClasses = 0
    for metricsPerClass in metricsPerClasses:
        cl = metricsPerClass['class']
        ap = metricsPerClass['AP']
        precision = metricsPerClass['precision']
        recall = metricsPerClass['recall']
        totalPositives = metricsPerClass['total positives']
        total_TP = metricsPerClass['total TP']
        total_FP = metricsPerClass['total FP']
        if totalPositives > 0:
            validClasses = validClasses + 1
            acc_AP = acc_AP + ap
            prec = ['%.2f' % p for p in precision]
            rec = ['%.2f' % r for r in recall]
            ap_str = '{0:.2f}%'.format(ap * 100)
            print('AP: %s (%s)' % (ap_str, cl))

    mAP = acc_AP / validClasses
    print(validClasses)
    mAP_str = '{0:.2f}%'.format(mAP * 100)
    print('mAP: %s' % mAP_str)
    return (mAP, metricsPerClasses)

def voc2ratxt(xml_file, box_format='xywh'):
    """"""
    boxes = get_bndbox(xml_file)
    image_id = os.path.basename(xml_file).split('.')[0]
    boxes = [[box['annonation_class'], *box['coordinates']] for box in boxes]
    if box_format == 'xywh':
        for box in boxes:
            x, y = box[1:3]
            w, h = box[2] - box[0], box[3] - box[1]
            box[1:] = [x, y, w, h]

    return [
     image_id, boxes]


'''Write a function toWeirdCase (weirdcase in Ruby) that accepts a string, and returns the same string with all even indexed characters
in each word upper cased, and all odd indexed characters in each word lower cased. The indexing just explained is zero based, so the
zero-ith index is even, therefore that character should be upper cased.

The passed in string will only consist of alphabetical characters and spaces(' '). Spaces will only be present if there are multiple
words. Words will be separated by a single space(' ').

Examples:
to_weird_case('String'); # => returns 'StRiNg'
to_weird_case('Weird string case') # => returns 'WeIrD StRiNg CaSe'
'''

def to_weird_case(string):
    words = string.split()
    case = []
    for i in words:
        temp = []
        for x in range(len(i)):
            if x == 0: temp.append(i[x].upper())
            elif x % 2 == 0: temp.append(i[x].upper())
            else: temp.append(i[x].lower())
        case.append(''.join(temp))
    return ' '.join(case)


'''
Created on Aug 10, 2018

@author: Jeff Krause
'''

import sys
import argparse
import subprocess
import logging

from datetime import datetime

DEFAULT_LOG_FILE = "C:/Users/Jeff Krause/logs/runTradeSimulatorLog." + datetime.today().strftime("%Y-%m-%d")
PYTHON_EXE = "C:/Windows/py "
PROGRAM = "\"C:/Users/Jeff Krause/scripts/tradeSimulator.py\""
MATURITY_OPTION = " -m "
LENGTH_OPTION = " -l "
AUCTION_WINDOW_OPTION = " -a "
STARTING_DATE_OPTION = " -s "
SLIDING_WINDOW_OPTION = " -w "
DATA_STARTING_DATE_OPTION = " -d "
CSV_OPTION = " -c "

def main():
    parser = argparse.ArgumentParser()
    parser._action_groups.pop()
    required = parser.add_argument_group('required_args')
    optional = parser.add_argument_group('optional_args')
    required.add_argument("-m", "--maturity_list", dest='maturity', nargs = '*', help="list of maturities in years", required=True)
    required.add_argument("-s", "--starting_date", dest='starting_date', nargs = '*', help="list of starting dates for price load: YYYY-MM-DD")
    required.add_argument("-l", "--length_list", dest='length', type=int, nargs = '*', help="list of length in months of trade sim", required=True)
    required.add_argument("-a", "--auction_days_window_list", dest='auction_days_window', type = int, nargs = '*', help="list of number of days before/after auction", required=True)
    optional.add_argument("-d", "--data_starting_date", dest='data_starting_date', help="Do not use data PRIOR to this date in sim")
    optional.add_argument("-w", "--sliding_window", dest='sliding_window', nargs = '*', help="list of sliding simulation window in months")
    optional.add_argument("-c", "--csv_file", action="store_true", help="create a csv file with results to import to Excel")

    args = parser.parse_args()

    maturity_list = ''
    for m in args.maturity:
        maturity_list += m + '_'
    maturity_list = maturity_list.rstrip('_')
    log_file_name = DEFAULT_LOG_FILE + '_' + maturity_list

    logging.basicConfig(filename = log_file_name, filemode='w', level=logging.INFO)

    if args.sliding_window:
        if len(args.sliding_window) % 2 != 0:
            print("-w option must contain size/interval pairs!")
            sys.exit(-1)
        i = 0
        # Optional arg, so form a list of pairs
        sw_list = []
        for sw_item in args.sliding_window:
            if i % 2 == 0:
                sw_pair = sw_item + ' '
            else:
                sw_pair += sw_item
                sw_list.append(sw_pair)
            i += 1

        sw_option = SLIDING_WINDOW_OPTION
    else:
        sw_list = ['']
        sw_option = ''

    for maturity in args.maturity:
        for starting_date in args.starting_date:
            for length in args.length:
                for auction_days_window in args.auction_days_window:
                    for sliding_window in sw_list:

                        program = PYTHON_EXE + PROGRAM + \
                            MATURITY_OPTION + maturity + \
                            STARTING_DATE_OPTION + starting_date + \
                            LENGTH_OPTION + str(length) + \
                            AUCTION_WINDOW_OPTION + str(auction_days_window) + \
                            sw_option + sliding_window

                        if args.data_starting_date:
                            program += DATA_STARTING_DATE_OPTION + args.data_starting_date
                        if args.csv_file:
                            program += CSV_OPTION

                        print("Running: {}".format(program))
                        logging.info("Running: " + program)
                        subprocess.run(program)

if __name__ == "__main__":
    main()


from mpi4py import MPI
import veloxchem as vlx
import sys
import numpy as np
import random
import copy
import L2
import L3

# Dirac delta fn
def drc(i, j):
	if (i == j):
		return 1
	else:
		return 0

# AO to MO basis transformation
def ao2mo(mo, A):
	return np.linalg.multi_dot([mo.T, A, mo])

# Test system setup
molecule_string = """
    O 0 0 0
    H 1.0 0.7 0
    H 1.0 -0.7 0"""

basis_set_label = 'STO-3G'

scf_settings = {'conv_thresh': 1.0e-6}
method_settings = {}

comm = MPI.COMM_WORLD
ostream = vlx.OutputStream(sys.stdout)

molecule = vlx.Molecule.read_str(molecule_string, units='angs')
basis = vlx.MolecularBasis.read(molecule, basis_set_label)

ostream.print_block(molecule.get_string())
ostream.print_block(basis.get_string('Atomic Basis', molecule))
ostream.flush()

# SCF calculation

# Do SCF again with non-custom routine
scfdrv = vlx.ScfRestrictedDriver(comm, ostream)
scfdrv.update_settings(scf_settings, method_settings)
scfdrv.compute(molecule, basis)

MOs = scfdrv.scf_tensors['C']
F = scfdrv.scf_tensors['F'][0]

# AO integrals (pq|rs)
n_ao = MOs.shape[0]
pqrs = np.zeros((n_ao, n_ao, n_ao, n_ao))

eri_drv = vlx.ElectronRepulsionIntegralsDriver(comm)
eri_drv.compute_in_mem(molecule, basis, pqrs)

# MO integrals (tu|vw), chemists' notation
tqrs = np.einsum('pqrs,pt->tqrs', pqrs, MOs, optimize=True)
del pqrs
turs = np.einsum('tqrs,qu->turs', tqrs, MOs, optimize=True)
del tqrs
tuvs = np.einsum('turs,rv->tuvs', turs, MOs, optimize=True)
del turs
tuvw = np.einsum('tuvs,sw->tuvw', tuvs, MOs, optimize=True)
del tuvs

H00 = scfdrv.old_energy

# Setup for linear response to get RHS for integrals
ref_freqs = [0.14]
ref_freqs_str = [str(x) for x in ref_freqs]

# Doing calculation with custom routine, stops at RHS
lr_solver = vlx.LinearResponseSolver(comm, ostream)
lr_solver.update_settings(
{
'frequencies': ','.join(ref_freqs_str),
}, method_settings)

# My RHS
RHS = lr_solver.get_prop_grad('dipole', 'xyz', molecule,
                                   basis, scfdrv.scf_tensors)

# Transition integrals
V0n_x = RHS[0][:RHS[0].shape[0]//2]
V0n_y = RHS[1][:RHS[1].shape[0]//2]
V0n_z = RHS[2][:RHS[2].shape[0]//2]

# "Proper" RHS
RHS_0 = RHS[0]
RHS_1 = RHS[1]
RHS_2 = RHS[2]

# Reference damped response data
damp_param = 0.005

cpp_solver_ref = vlx.ComplexResponse(comm, ostream)
cpp_solver_ref.update_settings(
{
'frequencies': ','.join(ref_freqs_str),
'damping': damp_param}, method_settings)
cpp_ref_dict = cpp_solver_ref.compute(molecule, basis,
                               scfdrv.scf_tensors)

# MO 2-electron integrals (curr. only used to get n_occ, n_virt)
moints_drv = vlx.MOIntegralsDriver(comm, ostream)
ovov = moints_drv.compute_in_mem(molecule, basis, scfdrv.mol_orbs, "OVOV")

n_occ = ovov.shape[0]
n_virt = ovov.shape[1]
n_ex = n_occ * n_virt

# Dipole moment integrals
dipole_drv = vlx.ElectricDipoleIntegralsDriver(comm)
dipole_mats = dipole_drv.compute(molecule, basis)

dipole_x = dipole_mats.x_to_numpy()
dipole_y = dipole_mats.y_to_numpy()
dipole_z = dipole_mats.z_to_numpy()

np.set_printoptions(precision=5, suppress=True, linewidth=132)

dipole_x_a = np.einsum('pq,pt->tq', dipole_x, MOs, optimize=True)
dipole_x_ab = np.einsum('tq,qu->tu', dipole_x_a, MOs, optimize=True)

dipole_y_a = np.einsum('pq,pt->tq', dipole_y, MOs, optimize=True)
dipole_y_ab = np.einsum('tq,qu->tu', dipole_y_a, MOs, optimize=True)

dipole_z_a = np.einsum('pq,pt->tq', dipole_z, MOs, optimize=True)
dipole_z_ab = np.einsum('tq,qu->tu', dipole_z_a, MOs, optimize=True)

V_mn_x = np.zeros((n_ex, n_ex))
V_mn_y = np.zeros((n_ex, n_ex))
V_mn_z = np.zeros((n_ex, n_ex))

# Ground-state dipole moment components
V_00_x = 2.0 * sum([dipole_x_ab[i,i] for i in range(n_occ)])
V_00_y = 2.0 * sum([dipole_y_ab[i,i] for i in range(n_occ)])
V_00_z = 2.0 * sum([dipole_z_ab[i,i] for i in range(n_occ)])

# Vm|n integrals
for i in range(n_occ):
	for s in range(n_virt):

		for j in range(n_occ):
			for t in range(n_virt):

				V_mn_x[i*n_virt + s, j*n_virt + t] += 2.0*V_00_x * drc(s,t) * drc(i,j) + 2.0 * dipole_x_ab[n_occ + t, n_occ + s] * drc(i,j) - 2.0 * dipole_x_ab[i, j]*drc(s,t)

				V_mn_y[i*n_virt + s, j*n_virt + t] += 2.0*V_00_y * drc(s,t) * drc(i,j) + 2.0 * dipole_y_ab[n_occ + t, n_occ + s] * drc(i,j) - 2.0 * dipole_y_ab[i, j]*drc(s,t)

				V_mn_z[i*n_virt + s, j*n_virt + t] += 2.0*V_00_z * drc(s,t) * drc(i,j) + 2.0 * dipole_z_ab[n_occ + t, n_occ + s] * drc(i,j) - 2.0 * dipole_z_ab[i, j]*drc(s,t)

F_mo = ao2mo(MOs, F)
g = tuvw

w = ref_freqs[0]

# E[2] and S[2] matrices
E2 = np.zeros((2 * n_ex, 2 * n_ex))
S2 = np.zeros((2 * n_ex, 2 * n_ex))

r = 0
c = 0
k = 0

# Size includes both m0, 0n elements and mn, nm elements
E2 = L2.L2_f(n_occ, n_virt, F_mo, g)
S2 = np.eye(n_ex*(n_ex + 2), dtype=complex)

RHS = np.zeros((n_ex*(n_ex + 2)), dtype=complex)
RHS[:2*n_ex] = 1.0j*RHS_0

# mu in L-space basis (one application of Q_mn) - NOT EXACTLY SAME AS RHS
M = np.zeros((n_ex*(n_ex + 2)), dtype=complex)
M[:n_ex] = RHS_0[:n_ex]
M[n_ex:2*n_ex] = RHS_0[:n_ex]

X = np.linalg.solve(E2 - 2.*(w + 1.0j * damp_param)*S2, RHS)

rsp = 1.0j*np.dot(M, X)

print('Linear response:', rsp)
print('Difference from reference: ', cpp_ref_dict['response_functions'][('x', 'x', ref_freqs[0])] - rsp)

K_1 = copy.deepcopy(X)

# Now constructing RHS for 2nd order

# V[2] and contraction with K

# Making first without any coefficient, to be handled later
# I assume that integrals of type Vmn|0 are zero

V2 = np.zeros((n_ex*(n_ex + 2)), dtype=complex)

for m in range(n_ex):

	for p in range(n_ex):

		V2[m] += 2.0 *(-1.0 * V_mn_x[m, p] + 2.0 * V_00_x * drc(m,p)) * K_1[p]

for n in range(n_ex):

	for q in range(n_ex):

		V2[n + n_ex] += 2.0 * (1.0*V_mn_x[q, n] - 2.0 * V_00_x * drc(n,q)) * K_1[n_ex + q]

# (no mn contribution to this V[2])

# G[3] and contractions with K

# Determinant-pair damping constants (currently zero)
Gamma = np.zeros((n_ex + 1, n_ex + 1), dtype=complex)

gamma_seed = damp_param

for m in range(n_ex + 1):
	for n in range(n_ex + 1):

		if m == 0:
			Gamma[m,n] = gamma_seed

		elif n == 0:
			Gamma[m,n] = gamma_seed

		else:
			Gamma[m,n] = 0.5*2.0*gamma_seed

G3 = np.zeros((n_ex*(n_ex + 2)), dtype=complex)

for m in range(n_ex):
	for n in range(n_ex):
		G3[2*n_ex + m*n_ex + n] = (Gamma[m + 1,n + 1] - Gamma[m + 1,0] - Gamma[0,n + 1]) * K_1[m] * K_1[n_ex + n]

# From general expression expansion coefficient
G3 *= -0.5

# E[3] and contractions with K

# Coefficients for E operators not fully settled, other factors I think are handled
# I do for now no regard to "non-integral E operators", review later
# For conjugate Hmn|p integrals Hm|np I do Hpn|m: Can be reviewed later whether it should be H_np|m (maybe they are symmetric)

K_for_L3 = np.zeros((2, 2*n_ex), dtype=complex)
K_for_L3[0,:] = K_1[:2*n_ex]
K_for_L3[1,:] = K_1[:2*n_ex]

L3_arr = L3.L3_f(n_occ, n_virt, H00, F_mo, g, K_for_L3)

# Setting up final projection vector
# Going by an assumed E operator form of this, revisit this point later
V_p = np.zeros((n_ex*(n_ex + 2)), dtype=complex)
V_p[:n_ex] = RHS_0[:n_ex]
V_p[n_ex:2*n_ex] = RHS_0[:n_ex]

for m in range(n_ex):
	for n in range(n_ex):
		V_p[2*n_ex + m*n_ex + n] = -1.0*V_mn_x[n,m]

# Left-hand side of rsp eqn.
# Here 2*w for SHG rsp, adapt this later
LHS = E2 - 2.0*(2.0*w + 1.0j* damp_param)*S2

# Second part: Contraction with K1*K1 part of rho(2) where the resulting state pair was not |0,0>>

rsp_Q2 = 0.0

for m in range(n_ex):
	for n in range(n_ex):
		rsp_Q2 -= 2.0*(V_mn_x[n,m] - 2.0*V_00_x*drc(m,n)) * K_1[m] * K_1[n_ex + n]

E2inv = np.linalg.inv(LHS)

NaT3_L = np.zeros(2*n_ex+ n_ex*n_ex, dtype=complex)

for m in range(2*n_ex + n_ex*n_ex):
	for p in range(2*n_ex+ n_ex*n_ex):
		NaT3_L[p] += E2inv[p,m] *1.0j* L3_arr[m]

rsp_QT3_L = np.dot(V_p, NaT3_L)

NaX2 = np.zeros(2*n_ex+ n_ex*n_ex, dtype=complex)

for m in range(2*n_ex + n_ex*n_ex):
	for p in range(2*n_ex+ n_ex*n_ex):
		NaX2[p] += E2inv[p,m] * V2[m]

rsp_QNa = np.dot(V_p, NaX2)

rsp_Q_all_test = rsp_Q2 + rsp_QNa *1.0j + rsp_QT3_L *1.0j

print('')
print('Quadratic response function', rsp_Q_all_test)
print('')
print('Individual contributions:')
print('')
print('NaX2 with factor', rsp_QNa *1.0j)
print('NxA2 with factor', rsp_Q2)
print('NaT3 with factor', rsp_QT3_L *1.0j)

NaG3 = np.zeros(2*n_ex+ n_ex*n_ex, dtype=complex)

for m in range(2*n_ex + n_ex*n_ex):

	for p in range(2*n_ex+ n_ex*n_ex):

		NaG3[p] += E2inv[p,m] *1.0j* G3[m]

rsp_G3 = np.dot(V_p, NaG3)
print('')
print('NaG3 contribution (not counted in above response function)', rsp_G3)


import rclpy
from rclpy.node import Node
from rclpy.action import ActionClient
from interfaces2.action import SquareSpiral
from action_msgs.msg import GoalStatus
import time

class Turtle_Action_Client(Node):

    def __init__(self):
        super().__init__('turtle_action_client')
        self.turtle_action = ActionClient(self, SquareSpiral, 'Spiral64')

    def send_goal(self, loop_length):
        self.get_logger().info('Waiting for action server...')
        self.turtle_action.wait_for_server()

        goal_action = SquareSpiral.Goal()
        goal_action.loop_length = loop_length

        self.goal_future =  self.turtle_action.send_goal_async(
           goal_action,
           feedback_callback= self.feedback_callback)

        self.goal_future.add_done_callback(self.goal_callback)

    def goal_callback(self, future):
        goal_handle = future.result()
        if not goal_handle.accepted:
            print('Goal was rejected')
            return
        self.get_logger().info('Goal was accepted, will proceed...')

        self.result_future = goal_handle.get_result_async()
        self.result_future.add_done_callback(self.result_callback)

    def feedback_callback(self, feedback_action):
        feedback = feedback_action.feedback
        self.get_logger().info('Received feedback: {0}'.format(feedback.current_spiral))

    def result_callback(self, future):
        result = future.result().result
        status = future.result().status
        if status == GoalStatus.STATUS_SUCCEEDED:
            print('Goal succeeded! Total number of spirals:', result.total)
        else:
            print('Goal could not be reached')

        # Shutdown after receiving a result
        rclpy.shutdown()

def main():
    rclpy.init()
    action_client = Turtle_Action_Client()

    action_client.send_goal(61)
    rclpy.spin(action_client)

if __name__ == '__main__':
    main()


from tensorflow.keras.preprocessing.image import ImageDataGenerator

'''
图片生成器tf.keras.preprocessing.image.ImageDataGenerator
用到的参数：
- rescale：输入一个整数，通常为1/255，由于图像像素都是0~255的整数，rescale可以
           让所有像素统一乘上一个数值，如果是1/255，像素会被转化为0~1之间的数。

从目录读取图片tf.keras.preprocessing.image.ImageDataGenerator.flow_from_directory
用到的参数：
- directory：图片存放路径。
- target_size：图片宽高缩放到指定的大小，默认(256, 256)。
- batch_size：每次读取的图片数，默认32。
- class_mode：类别格式，默认'categorical'。
              如果是'sparse'：类别['paper', 'rock', 'scissors'] ——> [0, 1, 2]
              如果是'categorical'：类别['paper', 'rock', 'scissors'] ——> [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
              如果是'input'：类别['paper', 'rock', 'scissors']保持不变
              如果是None：不返回标签。
'''

def train_val_generator(data_dir, target_size, batch_size, class_mode=None, subset='training'):
    train_val_datagen = ImageDataGenerator(# rotation_range=10, # rotation
        # width_shift_range=0.2, # horizontal shift
        # height_shift_range=0.2, # vertical shift
        zoom_range=0.2, # zoom
        horizontal_flip=True, # horizontal flip
        # brightness_range=[0.2,1.2],
        rescale=1.0/255.0,
        validation_split=0.1)
    return train_val_datagen.flow_from_directory(
        directory=data_dir,
        target_size=target_size,
        batch_size=batch_size,
        class_mode=class_mode,
        subset=subset)

def test_generator(data_dir, target_size, batch_size, class_mode=None):
    test_datagen = ImageDataGenerator(rescale=1./255.)
    return test_datagen.flow_from_directory(
        directory=data_dir,
        target_size=target_size,
        batch_size=batch_size,
        class_mode=class_mode)

def pred_generator(data_dir, target_size, batch_size, class_mode=None):
    pred_datagen = ImageDataGenerator(rescale=1./255.)
    return pred_datagen.flow_from_directory(
        directory=data_dir,
        target_size=target_size,
        batch_size=batch_size,
        class_mode=class_mode)