import pandas as pd
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
import seaborn as sns
#%% Reading the obtained results for different representation and sampling methods
undersampling= pd.read_csv('Pred_Infos_undersampling.csv')
oversampling= pd.read_csv('Pred_Info_oversampling.csv')
physical= pd.read_csv('Pred_Info_oversampling_physical.csv')
ph_undersampling= pd.read_csv('Pred_Info_undersampling_physical.csv')

physical_over=pd.DataFrame()
physical_over['Score']=pd.to_numeric(physical.iloc[4,1::2])
physical_over['Embedding'] = 'Physical_Features'
physical_over['Sampling']= 'R-Over-sampling'

physical_smote= pd.DataFrame()
physical_smote['Score']=pd.to_numeric(physical.iloc[4,2::2])
physical_smote['Embedding'] = 'Physical_Features'
physical_smote['Sampling']= 'SMOTE'

physical_under=pd.DataFrame()
physical_under['Score']=pd.to_numeric(ph_undersampling.iloc[4,1:])
physical_under['Embedding'] = 'Physical_Features'
physical_under['Sampling']=  ' Undersampling'
#%%
undersampling_oh= list(undersampling.iloc[4,1::3])
undersampling_unirep=list(undersampling.iloc[4,2::3])
undersampling_esm= list(undersampling.iloc[4,3::3])

oversampling_ro_oh=list(oversampling.iloc[4,1::6])
oversampling_ro_unirep=list(oversampling.iloc[4,2::6])
oversampling_ro_esm= list(oversampling.iloc[4,3::6])

oversampling_smo_oh=list(oversampling.iloc[4,4::6])
oversampling_smo_unirep=list(oversampling.iloc[4,5::6])
oversampling_smo_esm= list(oversampling.iloc[4,6::6])

#%% plot 1 making comparison between each embedding performance for undersampling vs oversampling

OH_under= pd.DataFrame()
OH_under['Score']= pd.to_numeric(undersampling.iloc[4,1::3])
OH_under['Embedding'] = 'OneHot'
OH_under['Sampling']= 'Under-sampling'
OH_under_n= OH_under[(np.abs(stats.zscore(OH_under['Score'])) < 3)|(np.abs(stats.zscore(OH_under['Score'])) > -3)]

OH_over= pd.DataFrame()
OH_over['Score']= pd.to_numeric(oversampling.iloc[4,1::6])
OH_over['Embedding'] = 'OneHot'
OH_over['Sampling']= 'R-Over-sampling'
OH_over_n= OH_over[(np.abs(stats.zscore(OH_over['Score'])) < 3)|(np.abs(stats.zscore(OH_over['Score'])) >-3)]

OH_smote= pd.DataFrame()
OH_smote['Score']= pd.to_numeric(oversampling.iloc[4,4::6])
OH_smote['Embedding'] = 'OneHot'
OH_smote['Sampling']= 'SMOTE'
OH_smote_n= OH_smote[(np.abs(stats.zscore(OH_smote['Score'])) < 3)|(np.abs(stats.zscore(OH_smote['Score'])) >-3)]

Unirep_under= pd.DataFrame()
Unirep_under['Score']= pd.to_numeric(undersampling.iloc[4,2::3])
Unirep_under['Embedding'] = 'UniRep'
Unirep_under['Sampling']= 'Under-sampling'
Unirep_under_n= Unirep_under[(np.abs(stats.zscore(Unirep_under['Score'])) < 3)|(np.abs(stats.zscore(Unirep_under['Score'])) > -3)]

Unirep_over= pd.DataFrame()
Unirep_over['Score']= pd.to_numeric(oversampling.iloc[4,2::6])
Unirep_over['Embedding'] = 'UniRep'
Unirep_over['Sampling']= 'R-Over-sampling'
Unirep_over_n= Unirep_over[(np.abs(stats.zscore(Unirep_over['Score'])) < 3)|(np.abs(stats.zscore(Unirep_over['Score']))>-3)]

Unirep_smote= pd.DataFrame()
Unirep_smote['Score']= pd.to_numeric(oversampling.iloc[4,5::6])
Unirep_smote['Embedding'] = 'UniRep'
Unirep_smote['Sampling']= 'SMOTE'
Unirep_smote_n= Unirep_smote[(np.abs(stats.zscore(Unirep_smote['Score'])) < 3)|(np.abs(stats.zscore(Unirep_smote['Score'])) >-3)]

ESM_under= pd.DataFrame()
ESM_under['Score']= pd.to_numeric(undersampling.iloc[4,3::3])
ESM_under['Embedding'] = 'ESM'
ESM_under['Sampling']= 'Under-sampling'
ESM_under_n= ESM_under[(np.abs(stats.zscore(ESM_under['Score'])) < 3)|(np.abs(stats.zscore(ESM_under['Score'])) >-3)]

ESM_over= pd.DataFrame()
ESM_over['Score']= pd.to_numeric(oversampling.iloc[4,3::6])
ESM_over['Embedding'] = 'ESM'
ESM_over['Sampling']= 'R-Over-sampling'
ESM_over=ESM_over.drop(index='RandomOverSampler()UniRep_Embedding6')
ESM_over_n= ESM_over[(np.abs(stats.zscore(ESM_over['Score'])) < 3)|(np.abs(stats.zscore(ESM_over['Score'])) > -3)]

ESM_smote= pd.DataFrame()
ESM_smote['Score']= pd.to_numeric(oversampling.iloc[4,6::6])
ESM_smote['Embedding'] = 'ESM'
ESM_smote['Sampling']= 'SMOTE'
ESM_smote_n= ESM_smote[(np.abs(stats.zscore(ESM_smote['Score'])) < 3)| (np.abs(stats.zscore(ESM_smote['Score'])) >-3)]

physical_over=pd.DataFrame()
physical_over['Score']=pd.to_numeric(physical.iloc[4,1::2])
physical_over['Embedding'] = 'Physical_Features'
physical_over['Sampling']= 'R-Over-sampling'

physical_smote= pd.DataFrame()
physical_smote['Score']=pd.to_numeric(physical.iloc[4,2::2])
physical_smote['Embedding'] = 'Physical_Features'
physical_smote['Sampling']= 'SMOTE'
#%%
df_all_n= pd.concat([OH_under_n, OH_over_n, OH_smote_n, Unirep_under_n, Unirep_over_n, Unirep_smote_n, ESM_under_n, ESM_over_n, ESM_smote_n])
df_all= pd.concat([OH_under, OH_over, OH_smote, Unirep_under, Unirep_over, Unirep_smote, ESM_under, ESM_over, ESM_smote, physical_smote, physical_over])
df_all_nn= df_all[(np.abs(stats.zscore(df_all['Score'])) < 3)| (np.abs(stats.zscore(df_all['Score'])) >-3)]

df_alll= pd.concat([ OH_over, Unirep_over, ESM_under,  physical_smote])

#%% Sampling vs sampling

OH_under_vs_over= stats.ttest_ind(OH_under['Score'], OH_over['Score'])
OH_under_vs_smote= stats.ttest_ind(OH_under['Score'], OH_smote['Score'])
OH_over_vs_smote= stats.ttest_ind(OH_over['Score'], OH_smote['Score'])

Unirep_under_vs_over= stats.ttest_ind(Unirep_under['Score'], Unirep_over['Score'])
Unirep_under_vs_smote= stats.ttest_ind(Unirep_under['Score'], Unirep_smote['Score'])
Unirep_over_vs_smote= stats.ttest_ind(Unirep_over['Score'], Unirep_smote['Score'])

ESM_under_vs_over= stats.ttest_ind(ESM_under['Score'], ESM_over['Score'])
ESM_under_vs_smote= stats.ttest_ind(ESM_under['Score'], ESM_smote['Score'])
ESM_over_vs_smote= stats.ttest_ind(ESM_over['Score'], ESM_smote['Score'])

#%% T test Representation vs representation given same sampling method

Under_OH_Unirep= stats.ttest_ind(OH_under['Score'], Unirep_under['Score'])
Under_OH_ESM=stats.ttest_ind(OH_under['Score'], ESM_under['Score'])
Under_ESM_Unirep=stats.ttest_ind(ESM_under['Score'], Unirep_under['Score'])

Over_OH_Unirep= stats.ttest_ind(OH_over['Score'], Unirep_over['Score'])
Over_OH_ESM=stats.ttest_ind(OH_over['Score'], ESM_over['Score'])
Over_ESM_Unirep=stats.ttest_ind(ESM_over['Score'], Unirep_over['Score'])

smote_OH_Unirep= stats.ttest_ind(OH_smote['Score'], Unirep_smote['Score'])
smote_OH_ESM=stats.ttest_ind(OH_smote['Score'], ESM_smote['Score'])
smote_ESM_Unirep=stats.ttest_ind(ESM_smote['Score'], Unirep_smote['Score'])


import pytest
from selenium.webdriver import FirefoxProfile
from seleniumrequests import Firefox

def pytest_addoption(parser):
    parser.addoption("--browser", default="chrome", help="Browsers : Chrome or Firefox")
    parser.addoption("--env", default="qa", help="Environments : qa or uat")

@pytest.fixture
def env(request):
    return request.config.getoption("--env")

@pytest.fixture
def browser(request):
    return request.config.getoption("--browser")

@pytest.fixture
def driver_init(request, browser):
    from selenium.webdriver import Chrome
    from selenium.webdriver.chrome.options import Options

    # if browser(request) == "chrome":
    if browser == "chrome":
        options = Options()
        options.add_argument("--incognito")
        # options.add_argument('--headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        request.instance.driver = Chrome(executable_path="D:/github/automationpractice/fe/chromedriver.exe", options=options)
        request.instance.driver.maximize_window()
        request.addfinalizer(request.instance.driver.quit)

    # if browser(request) == "firefox":
    if browser == "firefox":
        fp = FirefoxProfile()
        request.instance.driver = Firefox(executable_path="D:/github/automationpractice/fe/geckodriver.exe", firefox_profile=fp)
        request.addfinalizer(request.instance.driver.quit)

@pytest.fixture
def chrome_init():
    from selenium.webdriver import Chrome
    from selenium.webdriver.chrome.options import Options
    chrome_opt = Options()
    chrome_opt.add_argument("--incognito")
    chrome_opt.add_argument("user-agent=qaauto")
    driver = Chrome(executable_path="../chromedriver.exe", options=chrome_opt)
    yield driver
    driver.quit()

@pytest.fixture
def firefox_init(request):
    from selenium.webdriver import Firefox
    from selenium.webdriver import FirefoxProfile
    fp = FirefoxProfile()
    request.instance.driver = Firefox(executable_path=r'/usr/local/bin/geckodriver', firefox_profile=fp)
    request.addfinalizer(request.instance.driver.quit)


import json

import torch
from tensorboardX import SummaryWriter

from trainer import train

def main():
    # Load the pipeline configuration file
    config_path = "config.json"
    with open(config_path, "r", encoding="utf8") as f:
        config = json.load(f)

    writer = SummaryWriter()
    use_gpu = config["use_gpu"] and torch.cuda.is_available()
    device = torch.device("cuda" if use_gpu else "cpu")

    train(config, writer, device)

if __name__ == "__main__":
    main()


from collections import namedtuple

import pytest

from tests.profiler import Profiler
from tests.helpers import read_from_file
from data_structures.week1_basic_data_structures.\
    II_tree_height.tree_height import main

func_to_test = main

Setup = namedtuple("Setup", ["correct_result", "params"])
setups = [
    Setup(5, dict(parents=[3, -1, 3, 1, 1, 1, 5, 6, 7])),
    Setup(3, dict(parents=[3, -1, 3, 1, 3, 3, 3, 3])),
    Setup(10**5 + 1, dict(parents=[-1, *list(range(10**5))])),
    Setup(4, dict(parents=[-1, *list(range(3))]))
    ]

@pytest.fixture(autouse=True)
def setup_functions():
    def _prepare_setup(setup):
        return {
            "correct_result": setup[0],
            "profiler_instance": Profiler(
                func=func_to_test, **setup[1])
        }

    yield [_prepare_setup(setup) for setup in setups]


#!/usr/bin/env python3
# https://thingsmatic.com/2017/03/02/influxdb-and-grafana-for-sensor-time-series/
import paho.mqtt.client as mqtt
import datetime
import time
import json
from influxdb import InfluxDBClient
from config import *

def on_connect(client, userdata, flags, rc):
    print("Connected with result code " + str(rc))
    client.subscribe("fronius/#")

def on_message(client, userdata, msg):
    print("Received a message on topic: " + msg.topic)
    # Use utc as timestamp
    receiveTime = datetime.datetime.utcnow()

    m_decode=str(msg.payload.decode("utf-8","ignore"))
#    print ("\ndata m_decode-type ",type(m_decode))
    if m_decode != 'TimeOut':   # To avoid the connector to chrash on timeouts on the publisher
      if m_decode[0]=='[':
        message = json.loads(m_decode)
#        print("Message: ",message)
        dbclient.write_points(message)
#        print("Finished writing to InfluxDB")
      else:
        print ("No DATA!!!!!!!!!!!!!!!")   # This seems to happen on start.
    else:
      print('>>>>>>>>  TIMEOUT!!!!!  <<<<<<<<<<<')
print("start")

# Set up a client for InfluxDB
dbclient = InfluxDBClient(INFLUXDB_HOST, 8086, 'grafana', 'Mulan2010', 'fronius')
print("dbclient created")

# Initialize the MQTT client that should connect to the Mosquitto broker
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

# Wait for connect
connOK = False
while(connOK is False):
    try:
        client.connect(BROKER_HOST, BROKER_PORT, 60)
        connOK = True
    except Exception:
        connOK = False
    time.sleep(2)

print("mqtt connection established")

# Blocking loop to the Mosquitto broker
client.loop_forever()


import torch
import numpy as np
from typing import List

from nomeroff_net.tools.mcm import (modelhub, get_device_torch)

# download and append to path yolo repo
info = modelhub.download_repo_for_model("yolov5")
repo_path = info["repo_path"]

class Detector(object):
    """

    """
    @classmethod
    def get_classname(cls: object) -> str:
        return cls.__name__

    def __init__(self, numberplate_classes=None) -> None:
        self.model = None
        self.numberplate_classes = ["numberplate"]
        if numberplate_classes is not None:
            self.numberplate_classes = numberplate_classes
        self.device = get_device_torch()

    def load_model(self, weights: str, device: str = '') -> None:
        device = device or self.device
        model = torch.hub.load(repo_path, 'custom', path=weights, source="local")
        model.to(device)
        if device != 'cpu':  # half precision only supported on CUDA
            model.half()  # to FP16

        self.model = model
        self.device = device

    def load(self, path_to_model: str = "latest") -> None:
        if path_to_model == "latest":
            model_info = modelhub.download_model_by_name("yolov5")
            path_to_model = model_info["path"]
            self.numberplate_classes = model_info.get("classes", self.numberplate_classes)
        elif path_to_model.startswith("http"):
            model_info = modelhub.download_model_by_url(path_to_model, self.get_classname(), "numberplate_options")
            path_to_model = model_info["path"]
        elif path_to_model.startswith("modelhub://"):
            path_to_model = path_to_model.split("modelhub://")[1]
            model_info = modelhub.download_model_by_name(path_to_model)
            self.numberplate_classes = model_info.get("classes", self.numberplate_classes)
            path_to_model = model_info["path"]
        self.load_model(path_to_model)

    @torch.no_grad()
    def predict(self, imgs: List[np.ndarray], min_accuracy: float = 0.5) -> np.ndarray:
        model_outputs = self.model(imgs)
        model_outputs = [[[item["xmin"], item["ymin"], item["xmax"], item["ymax"], item["confidence"], item["class"]]
                         for item in img_item.to_dict(orient="records")
                         if item["confidence"] > min_accuracy]
                         for img_item in model_outputs.pandas().xyxy]
        return np.array(model_outputs)


import isbnlib
import csv
import pandas as pd
from ConvertToExcel import createExcelSheet,librarikaData
import xlsxwriter
from collections import defaultdict,OrderedDict
from ConvertExcelToCSV import Excel2CSV

df = pd.read_csv('ISBNNumbers.csv')
saved_column = df.ISBN
listOfBookDicts=[]
dd = defaultdict(list)
listAuthors=[]
finalFeatureDict=OrderedDict()
def CreateBookByISBN():
    try:
        i=0
        for isbn in saved_column.values.tolist():
            if type(isbn) is str:
                book = isbnlib.meta(isbn)
                listOfBookDicts.append(book)

        for d in listOfBookDicts: # you can list as many input dicts as you want here
            if d is not None:
                for key, value in d.items():
                    dd[key].append(value)

        for k,v in dd.items():
            for each in v:
                if(len(each)>1) and type(each)==list:
                    dd[k].remove(each)
                    dd[k].insert(i,';'.join(each) )
                    i=i+1
                elif type(each)==list:
                    dd[k].remove(each)
                    dd[k].insert(i,''.join(each))
                    i=i+1

        createOrderDict(dd,librarikaData)

        createExcelSheet(finalFeatureDict)
        Excel2CSV("BookListExcelColumn.xlsx","Sheet1")
    except Exception as ex:
        print(ex)
        pass
def createOrderDict(dd,librarikaData):
     for  specs in librarikaData :
            if specs not in dd.keys():
                finalFeatureDict[specs]=[]
            else:
                finalFeatureDict[specs]=dd[specs]

     for key in list(dd.keys()):
         if key=='ISBN-13':
            finalFeatureDict['ISBN13']=dd[key]
         if  key not in librarikaData :
            finalFeatureDict.pop(key, None)

CreateBookByISBN()


import math
import matplotlib.pyplot as plt

dataset = [
    [i * (2 / 5) for i in range(6)],
    [math.atan(i * (2 / 5)) for i in range(6)]
]

class SplineStruct:
    def __init__(self, a, b, c, d, x):
        self.a = a
        self.b = b
        self.c = c
        self.d = d
        self.x = x

def spline_create(x, y, n):
    splines = [SplineStruct(0, 0, 0, 0, 0) for _ in range(0, n)]
    for i in range(0, n):
        splines[i].x = x[i]
        splines[i].a = y[i]

    splines[0].c = splines[n - 1].c = 0.0

    alpha = [0.0 for _ in range(0, n - 1)]
    beta = [0.0 for _ in range(0, n - 1)]

    for i in range(1, n - 1):
        hi = x[i] - x[i - 1]
        hi1 = x[i + 1] - x[i]
        A = hi
        C = 2.0 * (hi + hi1)
        B = hi1
        F = 6.0 * ((y[i + 1] - y[i]) / hi1 - (y[i] - y[i - 1]) / hi)
        z = (A * alpha[i - 1] + C)
        alpha[i] = -B / z
        beta[i] = (F - A * beta[i - 1]) / z

    for i in range(n - 2, 0, -1):
        splines[i].c = alpha[i] * splines[i + 1].c + beta[i]

    for i in range(n - 1, 0, -1):
        hi = x[i] - x[i - 1]
        splines[i].d = (splines[i].c - splines[i - 1].c) / hi
        splines[i].b = hi * (2.0 * splines[i].c + splines[i - 1].c) / 6.0 + (y[i] - y[i - 1]) / hi
    return splines

def interpolate(splines, x):
    if not splines:
        return None

    n = len(splines)
    s = SplineStruct(0, 0, 0, 0, 0)

    if x <= splines[0].x:
        s = splines[0]
    elif x >= splines[n - 1].x:
        s = splines[n - 1]
    else:
        i = 0
        j = n - 1
        while i + 1 < j:
            k = i + (j - i) // 2
            if x <= splines[k].x:
                j = k
            else:
                i = k
        s = splines[j]

    dx = x - s.x
    return s.a + (s.b + (s.c / 2.0 + s.d * dx / 6.0) * dx) * dx

spline = spline_create(dataset[0], dataset[1], 6)

print("Сплайн в точке x = 0.75: ", interpolate(spline, 0.75))
print("Значение функции tg(x) в точке x = 0.75: ", math.atan(0.75))
print("Погрешность: {:0.9f}".format(abs(interpolate(spline, 0.75) - math.atan(0.75))))

splinesx = []
for i in dataset[0]:
    splinesx.append(interpolate(spline, i))

plt.plot(dataset[0], dataset[1])
plt.scatter(dataset[0], splinesx)
plt.show()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Make a existing local or ephemeral hidden service available over TLS for non-Tor users
"""

import os
import sys
import time
import logging
import argparse
import subprocess
from multiprocessing import Process
import ssl

from flask import Flask, send_from_directory
from stem.control import Controller

handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(fmt="%(asctime)s [%(levelname)s]: %(message)s"))

logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)

app = Flask(__name__)

def make_directory(path):
    if not os.path.exists(path):
        os.makedirs(path)

@app.route("/")
def index():
    return "It works"

@app.route("/<path:filename>")
def serve_challenge_response(filename):
    """Serve files from the web root directory"""
    return send_from_directory(app.config["web_root"], filename)

def request_cert(domain, web_root, data_dir, testing=False):
    """Run cerbot in background to issue a new cert"""
    cmd = [
        "certbot", "certonly", "--webroot", "-d", domain, "--register-unsafely-without-email",
        "--work-dir", data_dir, "--config-dir", data_dir, "--logs-dir", data_dir, "--agree-tos",
        "-w", web_root, "--keep",
    ]
    if testing:
        cmd.append("--test-cert")

    logger.debug("Running: {}".format(" ".join(cmd)))
    return subprocess.check_output(cmd, stderr=subprocess.STDOUT)

def create_server_thread(*args, **kwargs):
    return Process(target=app.run, kwargs=kwargs)

def server_with_letsencrypt_keys(port, cert_path):
    context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
    context.load_cert_chain(os.path.join(cert_path, "fullchain.pem"),
                            os.path.join(cert_path, "privkey.pem"))
    return create_server_thread(port=port, ssl_context=context)

def start_ephemeral_onion(ports):
    """
    Start ephemeral onion address and wait for publication.

    The ports argument should be a dictionary mapping public to local ports
    """
    with Controller.from_port() as controller:
        controller.authenticate()
        return controller.create_ephemeral_hidden_service(ports, await_publication=True,
                                                          detached=True)

def parse_cmd_args():
    """Parses and returns command line arguments."""
    parser = argparse.ArgumentParser(description="%s start a Flask web server and request"
                                     "a LetsEncrypt cert for your onion address." % sys.argv[0])

    parser.add_argument("--onion_address", type=str, default=None,
                        help="Local onion service address. If not provided an ephemeral onion "
                        "service will be started.")

    parser.add_argument("--proxy-host", type=str, default="oniongate.com",
                        help="Service running the OnionGateway TLS proxy (default: '%(default)s').")

    parser.add_argument("--http-port", type=int, default=8080,
                        help="Local destination port for HTTP request to the Tor "
                        "onion service (default: '%(default)s').")

    parser.add_argument("--tls-port", "-p", type=int, default=8443,
                        help="Local destination port for TLS connections to the Tor "
                        "onion service (default: '%(default)s').")

    parser.add_argument("--letsencrypt-data-dir", "-d", type=str, default="letsencrypt_data",
                        help="Directory to store LetsEncrypt keys, logs and other data "
                        "(default: '%(default)s').")

    parser.add_argument("--web-root", "-w", type=str, default="webroot",
                        help="Directory to store LetsEncrypt challenges (default: '%(default)s').")

    parser.add_argument("--test-run", action="store_true",
                        help="Use the LetsEncrypt staging server when requesting the cert.")

    # parser.add_argument("--challenge-path", type=str, default="/.well-known/acme-challenge/",
    #                     help="The path where LetsEncrypt challenges are served "
    #                     "(default: '%(default)s').")

    return parser.parse_args()

def main():
    args = parse_cmd_args()

    make_directory(args.web_root)
    app.config["web_root"] = args.web_root

    # Start an ephemeral onion if no onion address is provided.
    if not args.onion_address:
        logger.info("Starting ephemeral onion service, this may take a minute...")
        response = start_ephemeral_onion({80: args.http_port, 443: args.tls_port})
        onion_address = response.service_id
        logger.info("Started ephemeral onion service {}".format(onion_address))

    else:
        onion_address = args.onion_address

    if onion_address.endswith(".onion"):
        onion_address = onion_address[:-6]

    # Determine public domain from onion address and proxy host
    domain = ".".join([onion_address, args.proxy_host])

    # Start a local Flask server to serve the LetsEncrypt challenge response
    # The `adhoc` ssl_context mode creates a temporary self-signed cert and private key
    logger.info("Starting Flask server with self-signed certs for domain {}".format(domain))
    server = create_server_thread(port=args.tls_port, ssl_context="adhoc")
    server.start()
    time.sleep(5)

    try:
        logger.info("Starting LetsEncrypt cert request")
        certbot_response = request_cert(domain, args.web_root, args.letsencrypt_data_dir,
                                        args.test_run)
        logger.debug("Certbot output:\n{}".format(certbot_response.decode("utf-8")))

    except subprocess.CalledProcessError as e:
        logger.warning("LetsEncrypt cert issuance failed:\n{}".format(e.output.decode("utf-8")))
        server.terminate()
        sys.exit(1)

    # Stop running Flask server and restart with the new certs
    logger.info("Successfully got a LetsEncrypt cert for https://{}".format(domain))
    server.terminate()
    server.join()

    logger.info("Starting Flask server with the LetsEncrypt certificate")
    cert_path = os.path.join(args.letsencrypt_data_dir, "live", domain)
    server = server_with_letsencrypt_keys(port=args.tls_port, cert_path=cert_path)
    server.daemon = True
    server.start()

    # Start the HTTP server for direct connections to the onion address
    app.run(port=args.http_port)

if __name__ == "__main__":
    main()


from pylab import figure, axes, pie, title, savefig
import matplotlib.pyplot as plt

#그래프 그리기
def draw_graph(x, y):
    plt.axhline(0, color='black', linewidth = 0.2)
    plt.plot(x, y, color = 'blue')
    plt.xlabel('x')
    plt.ylabel('Moment')
    plt.title('BMD')

def generate():
	xs = []
	bmds = []
	with open("data.txt", "r") as file:
		for line in file :
			(x, sfd, bmd) = map(float, line.strip().split(", "))
			xs.append(x)
			bmds.append(bmd)
	draw_graph(xs, bmds)
	savefig('output/bmd.pdf')

generate()


# -*- coding: utf-8 -*-
"""
Created on Sun Oct 23 10:50:50 2022

@author: ChrisZeThird

Special thanks to ConfusedReptile#6830 on discord for their help on the animation setup.
"""
import numpy as np

import matplotlib.pyplot as plt
from matplotlib.widgets import Button
import matplotlib.animation as animation

""" Rules of the Game of Life """

def update(grid):

    # Copy grid since we require 8 neighbors for calculation and we go cell by cell
    newGrid = grid.copy()
    N,_ = np.shape(grid)
    for i in range(N):
        for j in range(N):
            total = grid[i%N, (j-1)%N] + grid[i%N, (j+1)%N] + grid[(i-1)%N, j%N] + grid[(i+1)%N, j%N] + grid[(i-1)%N, (j-1)%N] + grid[(i-1)%N, (j+1)%N] + grid[(i+1)%N, (j-1)%N] + grid[(i+1)%N, (j+1)%N]

            # Applies Conway's rules
            if grid[i, j]  == 1:
                if (total < 2) or (total > 3):
                    newGrid[i, j] = 0
            else:
                if total == 3:
                    newGrid[i, j] = 1

    # Updated data
    global data
    data = newGrid
    # Updated image
    global img
    img.set_data(data)

""" Setting initial distribution """

# Size of the grid
N = 30

# Make an empty data set
data = np.zeros((N, N))

""" Figure setup """

global fig
fig = plt.figure()
global ax
ax = fig.subplots()
plt.subplots_adjust(right = 0.25)

# Draw a grid layout to see the cell more clearly
for x in range(N + 1):
    ax.axhline(x, lw=2, color='w', zorder=5)
    ax.axvline(x, lw=2, color='w', zorder=5)

# Turn off the axis labels
ax.axis('off')
plt.tight_layout(pad=4)

# Plot the initial empty distribution
global img
img = ax.imshow(data, cmap='gray', extent=[0, N, 0, N], vmin=0, vmax=1)

""" Buttons positioning """

# Reset button placement
axes_exit = plt.axes([0.755, 0.2, 0.1, 0.075])
exit_button = Button(axes_exit, 'Exit',color='lightcoral', hovercolor='firebrick')

# Reset grid button placement
axes_reset = plt.axes([0.7, 0.4, 0.1, 0.075])
reset_button = Button(axes_reset, 'Reset',color='sandybrown', hovercolor='orangered')

# Random distribution button placement
axes_random = plt.axes([0.81, 0.4, 0.1, 0.075])
random_button = Button(axes_random, 'Randomize', color='lightblue', hovercolor='darkblue')

# Start animation button placement
axes_start = plt.axes([0.7, 0.6, 0.1, 0.075])
start_button = Button(axes_start, 'Start',color='palegreen', hovercolor='lime')

# Pause animation button placement
axes_pause = plt.axes([0.81, 0.6, 0.1, 0.075])
pause_button = Button(axes_pause, 'Pause',color='navajowhite', hovercolor='gold')

""" Defining button press events """

# Allow the user to select cells to setup the initial configuration
def turn_on(event):
    gx = event.xdata # x coordinate of the mouse
    gy = event.ydata # y coordinate of the mouse

    # mouse coordinate (x,y) correspond to array indexes [i,j] with i = N-1 - y and j = x
    i = N - 1 - int(gy)
    j = int(gx)
    if data[i,j] == 0:
        data[i,j] = 1 # on click, turns cell on
    else:
        data[i,j] = 0 # on click, turns cell off if it was on already
    global img
    img.set_data(data) # update the imshow

    fig.canvas.draw_idle()

# Lets the user exit the figure
def escape(event):
    plt.close()

# On click, reset the plot with all zeros array
def reset(event):
    global data
    data = np.zeros((N,N)) # reset the array to all zeros

    global img
    img.set_data(data)
    fig.canvas.draw_idle()

# Start the animation of the game
def _update(frame):
    update(data)

def start_anim(event):
    global ani
    ani = animation.FuncAnimation(fig, _update, interval=200, save_count=50)
    # ani.save('animation3.gif')

def pause_anim(event):
    global ani
    ani.pause()

def random_distribution(event):
    global data
    data = np.random.randint(2, size=(N,N))

    global img
    img.set_data(data)
    fig.canvas.draw_idle()

""" Associate buttons with respective function """

# Connect buttons to functions
start_button.on_clicked(start_anim) # start the animation on click
pause_button.on_clicked(pause_anim)

reset_button.on_clicked(reset)
random_button.on_clicked(random_distribution)

exit_button.on_clicked(escape)

# Connect buttons to figure
fig.canvas.mpl_connect('button_press_event', turn_on) # connect cells management to figure


from os import getenv
import requests
import random
from googletrans import Translator

class assistir_filme():
    token = getenv(key="CHAVE_API_FILMES")
    dict_genero = {"terror":27, "acao":28, "comedia":35, "drama":18, "ficcao_cientifica":878,
               "fantasia":14, "familia":10751, "aventura":12, "misterio":9648, "suspense":53,
               "crime":80, "cinemaTv":10770, "romance":10749}

    def numero_aleatorio(self, limite_range=501):
        numero_aleatorio = random.randrange(1, limite_range)
        return numero_aleatorio

    def chunks(self, lista):
        for i in range(0, len(lista), 4):
            yield lista[i:i + 4]

    def lista_aleatoria(self, lista):
        lista = list(self.chunks(lista))
        count_list = len(lista)
        numero = random.randrange(0, count_list +1)
        try:
            list_aleatoria = lista[numero]
        except:
            numero = numero - 1
            list_aleatoria = lista[numero]
        return list_aleatoria

    def requisicao(self, url):
        requisicao = requests.get(url)
        json = requisicao.json()
        return json

    def listar_filmes_rate_genero(self, rate, genero):
        while True:
            lista_filmes = []
            genero_id = self.dict_genero[f"{genero}"]
            url_geral = "https://api.themoviedb.org/3/discover/movie?" \
                        f"api_key={self.token}&" \
                        "include_adult=false&" \
                        "include_video=false&" \
                        f"page={self.numero_aleatorio()}&" \
                        f"vote_average.gte={rate}&" \
                        f"with_genres={genero_id}"
            filmes = self.requisicao(url_geral)
            filmes = filmes['results']
            if not filmes:
                continue
            else:
                for movie in filmes:
                    id = movie['id']
                    lista_filmes.append(id)
                return lista_filmes

    def listar_filmes_genero(self, genero):
        while True:
            lista_filmes = []
            genero_id = self.dict_genero[f"{genero}"]
            url_geral = "https://api.themoviedb.org/3/discover/movie?" \
                        f"api_key={self.token}&" \
                        "include_adult=false&" \
                        "include_video=false&" \
                        f"page={self.numero_aleatorio()}&" \
                        f"with_genres={genero_id}"
            filmes = self.requisicao(url_geral)
            filmes = filmes['results']
            if not filmes:
                continue
            else:
                for movie in filmes:
                    id = movie['id']
                    lista_filmes.append(id)
                return lista_filmes

    def listar_filmes_rate(self, rate, max_rate=10):
        contador_de_tentativas = 0
        while True:
            if rate >=0 and rate <=4:
                max_rate = 4
            if rate >= 5 and rate <= 7:
                max_rate = 7
            if rate >= 8 and rate <= 10:
                max_rate = 10

            lista_filmes = []
            url_geral = "https://api.themoviedb.org/3/discover/movie?" \
                        f"api_key={self.token}&" \
                        "include_adult=false&" \
                        "include_video=false&" \
                        f"page={self.numero_aleatorio()}&" \
                        f"vote_average.gte={rate}&" \
                        f"vote_average.lte={max_rate}"
            filmes = self.requisicao(url_geral)
            filmes = filmes['results']
            if not filmes:
                contador_de_tentativas += 1
                if contador_de_tentativas == 20:
                    url_geral = f'https://api.themoviedb.org/3/discover/movie?' \
                                f'api_key={self.token}&' \
                                f'include_adult=false&' \
                                f'include_video=false&page={self.numero_aleatorio(limite_range=max_rate)}&' \
                                f'vote_average.gte={rate}&' \
                                f'vote_average.lte={max_rate}'
                    filmes = self.requisicao(url_geral)
                    filmes = filmes['results']
                    if not filmes:
                        continue
                    for movie in filmes:
                        id = movie['id']
                        lista_filmes.append(id)
                    return lista_filmes
                continue
            else:
                for movie in filmes:
                    id = movie['id']
                    lista_filmes.append(id)
                return lista_filmes

    def listar_filmes(self):
        while True:
            lista_filmes = []
            url_geral = "https://api.themoviedb.org/3/discover/movie?" \
                        f"api_key={self.token}&" \
                        "include_adult=false&" \
                        "include_video=false&" \
                        f"page={self.numero_aleatorio()}"
            filmes = self.requisicao(url_geral)
            filmes = filmes['results']
            if not filmes:
                continue
            else:
                for movie in filmes:
                    id = movie['id']
                    lista_filmes.append(id)
                return lista_filmes

    def rodar(self, lista_filmes):
        i = True
        while i:
            try:
                count_movies = len(lista_filmes)
                random_id = random.randrange(0, count_movies + 1)
                id_filme = lista_filmes[random_id]

                url_filme = f"https://api.themoviedb.org/3/movie/{id_filme}?" \
                            f"api_key={self.token}"
                filme = self.requisicao(url_filme)
                imagem = filme['poster_path']
                id = filme['id']
                url_id = 'https://www.themoviedb.org/movie/'+str(id)
                nome = filme['original_title']
                sinopse = filme['overview']
                translator = Translator()
                sinopse = translator.translate(text=sinopse, dest='pt')
                nome_pt = translator.translate(text=nome, dest='pt')
                if not sinopse:
                    sinopse = "Não encontrado"
                votos = filme['vote_average']
                saida = f'****** SEU FILME É ******\nNome: {nome_pt.text} ({nome})\nSinopse: {sinopse.text}\nMédia de votos: {votos}'
                i = False
                return [saida, url_id, imagem]
            except Exception as e:
                print("Algo deu errado, finalizando !")


__all__ = ["boxplot"]

from typing import Union, List, Tuple

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

from .utils import is_rgb
from .utils import is_series
from .utils import is_dataframe
from .utils import process_axes
from .utils import create_subplots
from .utils import make_cols_from_cmap

def boxplot(
        data:Union[np.ndarray, List[np.ndarray]],
        line_color:Union[str, List[str]] = None,
        line_width = None,
        fill_color:Union[str, List[str]] = None,
        labels:Union[str, List[str]] = None,
        share_axes:bool = True,
        figsize:tuple = None,
        ax:plt.Axes = None,
        ax_kws:dict = None,
        show:bool = True,
        **box_kws,
)->Tuple[Union[plt.Axes, List[plt.Axes]], Union[List[dict], dict]]:
    """
    Draws the box and whiker plot

    parameters
    ----------
    data :
        array like (list, numpy array, pandas dataframe/series) or list of
        array likes. If list of array likes, the length of arrays in the list
        can be different.
    line_color :
        name of color/colors/cmap for lines/boundaries of box
    line_width :
        width of the box lines.
    fill_color :
        name of color/colors/cmap to fill the boxes. It can be any valid
         matplotlib color or cmap.
    labels : str/list (default=None)
        used for ticklabels of x-axes
    share_axes : bool (default=True)
        whether to draw all the histograms on one axes or not
    figsize : tuple (default=None)
        figure size as tuple (width, height)
    ax : plt.Axes, optional (default=None)
        matploltib axes on which to draw the plot
    ax_kws : dict (default=None)
        keyword arguments of :py:func:`easy_mpl.utils.process_axes`
    show : bool (default=show)
        whether to show the plot or not
    **box_kws :
        any additional keyword argument for :obj:`matplotlib.axes.Axes.boxplot`

    Returns
    -------
    tuple
        a tuple of two
            - plt.Axes or list of :obj:`matplotlib.axes`
            - a dictionary or list of dictionaries which consists of boxes,
              medians, whiskers, fliers

    Examples
    ---------
    >>> from easy_mpl import boxplot
    >>> boxplot(np.random.random((100, 5)))
    we can also provide arrays of different lengths
    >>> boxplot([np.random.random(100), np.random.random(90)])
    the color can be given as either color name or colormap
    >>> boxplot(np.random.random((100, 3)), fill_color=['pink', 'lightblue', 'lightgreen'])
    >>> boxplot(np.random.random((100, 3)), fill_color="viridis")

    See :ref:`sphx_glr_auto_examples_boxplot.py` for more examples

    """

    if ax is None:
        ax = plt.gca()

    _box_kws = {}

    data, labels = _unpack_data(data, labels, share_axes)

    if share_axes:
        nplots = 1
    else:
        nplots = len(labels)

    if box_kws is None:
        box_kws = dict()

    _box_kws.update(box_kws)

    f, axes = create_subplots(nplots, ax=ax, figsize=figsize)

    if isinstance(axes, np.ndarray):
        axes = axes.flatten().tolist()
    elif isinstance(axes, plt.Axes):
        axes = [axes]

    nboxes = len(labels)
    if len(labels)==1:
        nboxes = len(labels[0])
    fill_colors = _unpack_colors(fill_color, nboxes, share_axes)
    line_colors = _unpack_colors(line_color, nboxes, share_axes)
    line_widths = _unpack_linewidth(line_width, nboxes, share_axes)

    box_outs = []
    for (idx, name), x, ax in zip(enumerate(labels), data, axes):

        # in version 3.2.. giving DataFrame to ax.boxplot makes boxes for each row
        # in version 3.3.. giving DataFrame to ax.boxplot tries to make boxp for first row (columns)
        if is_dataframe(x) and matplotlib.__version__ < "3.3.0":
            x = x.values

        box_out = ax.boxplot(x, **_box_kws)
        box_outs.append(box_out)

        _set_box_props(fill_colors[idx], line_colors[idx],
                       line_widths[idx], box_out)

        _set_ticklabels(ax, share_axes, name, _box_kws)

        if ax_kws:
            process_axes(ax, **ax_kws)

    if show:
        plt.show()

    if len(box_outs)==1:
        box_outs = box_outs[0]

    if len(axes)==1:
        axes = axes[0]

    return axes, box_outs

def _set_ticklabels(ax, share_axes, name, box_kws):

    if name is not None:
        kws = dict()

        if share_axes:
            if box_kws.get('vert', True):
                ax.set_xticklabels(name)
            else:
                ax.set_yticklabels(name)
        else:
            if isinstance(name, (str, int)):
                if box_kws.get('vert', True):
                    ax.set_xticklabels([name])
                else:
                    ax.set_yticklabels([name], rotation=90, va='center')
            elif isinstance(name, list):
                if box_kws.get('vert', True):
                    ax.set_xticklabels(name)
                else:
                    ax.set_yticklabels(name, rotation=90, va='center')

        if share_axes and len(name) > 7:
            kws['rotation'] = 90
            ax.xaxis.set_tick_params(rotation=90)

    return

def _unpack_linewidth(line_width, nboxes, share_axes):
    if isinstance(line_width, (float, int)):
        line_widths = [[line_width] for _ in range(nboxes)]
    elif line_width is None:
        line_widths = [[None] for _ in range(nboxes)]

    if share_axes:
       line_widths = [[line_width[0] for line_width in line_widths]]

    return line_widths

def _unpack_colors(color, nboxes, share_axes)->list:
    if isinstance(color, str):
        if color in plt.colormaps():
            colors = make_cols_from_cmap(color, nboxes)
            colors = [[color] for color in colors]
        else:
            colors = [[color] for _ in range(nboxes)]
    elif is_rgb(color):
        colors = [[color] for _ in range(nboxes)]
    elif hasattr(color, '__len__'):
        assert len(color) == nboxes, f"{len(color)} colors for {nboxes} boxes?"
        colors = [[clr] for clr in color]
    elif color is None:
        colors = [[None] for _ in range(nboxes)]
    else:
        raise ValueError(f"{color} is not recognized as valid color")

    if share_axes:
       colors = [[color[0] for color in colors]]

    return colors

def _set_box_props(fill_color:list,
                   line_color:list,
                   line_width:list,
                   box_out):

    for idx, patch in enumerate(box_out['boxes']):
        if hasattr(patch, 'set_facecolor'):
            if fill_color[idx] is not None:
                patch.set_facecolor(fill_color[idx])
        elif hasattr(patch, 'set_markerfacecolor'):
            if fill_color[idx] is not None:
                patch.set_markerfacecolor(fill_color[idx])

        if hasattr(patch, 'set_color') and line_color[idx] is not None:
            patch.set_color(line_color[idx])

        if hasattr(patch, 'set_linewidth') and line_width[idx] is not None:
            patch.set_linewidth(line_width[idx])

    return

def _unpack_data(x, labels, share_axes:bool)->Tuple[list, list]:

    if isinstance(x, np.ndarray):
        if len(x) == x.size:
            X = [x]
            names = [[None]]
        else:
            if share_axes:
                X = [x]
                names = [[f"{i}" for i in range(x.shape[1])]]
            else:
                X = [x[:, i] for i in range(x.shape[1])]
                names = [f"{i}" for i in range(x.shape[1])]

    elif is_dataframe(x):
        if share_axes:
            names = [x.columns.tolist()]
            X = [x]
        else:
            X = []
            for col in x.columns:
                X.append(x[col].values)
            names = x.columns.tolist()

    elif is_series(x):
        X = [x.values]
        names = [[x.name]]

    elif isinstance(x, (list, tuple)) and isinstance(x[0], (list, tuple, np.ndarray)):
        assert all([len(array)==array.size for array in x]), f"""
        All arrays must be one dimensional."""
        X = [np.array(x_).reshape(-1,) for x_ in x]
        names = [None] * len(X)
        if share_axes:
            X = [X]
            names = [names]

    elif isinstance(x, (list, tuple)) and is_series(x[0]):  # list of series
        if share_axes:
            X = [x]
        else:
            X = x
        names = [x_.name for x_ in x]

    elif isinstance(x, (list, tuple)) and not is_dataframe(x[0]):
        X = [x]
        names = [None]
    else:
        raise ValueError(f"unrecognized type of x {type(x)}")

    if labels is not None:
        if isinstance(labels, str):
            labels = [labels]
        if share_axes:
            labels = [labels]
        #assert len(labels) == len(names), f"{len(names)} does not match data"
        names = labels

    return X, names


from flask import Flask, render_template, request
from pprint import pprint
import json

app = Flask(__name__)

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0

@app.route("/", methods=["GET", "POST"])
def home():
	services = None

	if request.method == "POST":
		data = request.form.items()
		services = []

		for items in zip(*[iter(data)] * 3):
			infos = []
			for item in items:
				infos.append(item[1])
			services.append(infos)

	with open("config/prestations.json", "r") as f:
		data = json.load(f)

		if services != None:
			if len(services) != 0:
				export = []

				for service in services:
					export.append({
						"name" : service[1],
						"price" : service[0],
						"description" : service[2]
					})

				with open("config/export.json", "w") as outfile:
					json.dump({"services" : export}, outfile, indent=4, ensure_ascii=False)

		return render_template("pages/home.html", quotes=data)

@app.errorhandler(404)
def page_not_found(error):
	return render_template("pages/404.html"), 404

if __name__ == "__main__":
	app.run(debug=True, port=80)


# -*- coding: UTF-8 -*-
# @Project: Progress
# @File: Net
# @Author: Henry Ng
# @Date: 2021/12/08 15:02

import os
from tensorflow.keras.preprocessing import image

import numpy as np
from tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau
from tensorflow.keras.models import Model
import tensorflow as tf
from config import Config
import sys
sys.path.append('/')
from pypeline.Validation import Validation
from pypeline.VGG import VGG
from pypeline.XCeption import XCeption
from pypeline.Resnet import Resnet
from Debug import Debug

class Net(object):
    def __init__(self, k: int, frozen_layer: int = Config.frozen_layer_vgg16, net_name: str = Config.name_vgg16):
        self.K = k
        self.image_width = 300
        self.image_height = 300
        self.nb_epoch = 100
        self.batch_size = 32
        self.frozen_layer = frozen_layer
        self.label = {}
        self.net_name = net_name

    @staticmethod
    def save_history(history, result_file):
        loss = history.history['loss']
        acc = history.history['accuracy']
        # val_loss = history.history['val_loss']
        val_acc = history.history['val_accuracy']
        nb_epoch = len(acc)

        with open(result_file, "w") as fp:
            fp.write("epoch\tloss\tacc\tval_loss\tval_acc\n")
            for i in range(nb_epoch):
                # fp.write("%d\t%f\t%f\t%f\t%f\n" % (i, loss[min(i, len(loss) - 1)], acc[min(i, len(acc) - 1)], val_loss[min(i, len(val_loss) - 1)], val_acc[min(i, len(val_acc) - 1)]))
                fp.write("%d\t%f\t%f\t%f\n" % (i, loss[min(i, len(loss) - 1)], acc[min(i, len(acc) - 1)], val_acc[min(i, len(val_acc) - 1)]))

    def create_model(self) -> tf.keras.models.Model:
        shape = (self.image_width, self.image_height, 3)
        model = Model()
        if Config.name_vgg16 == self.net_name:
            return VGG(shape=shape, frozen_layer=self.frozen_layer).model()
        elif Config.name_resnet == self.net_name:
            return Resnet(shape=shape, frozen_layer=self.frozen_layer).model()
        elif Config.name_xception == self.net_name:
            return XCeption(shape=shape, frozen_layer=self.frozen_layer).model()
        return model

    def generate_train(self, batch_size: int = 32, target_size: (int, int) = (256, 256)):
        head_data_path = Config.path(Config.slic_result_path, self.K, Config.name_train)

        train_datagen = image.ImageDataGenerator(rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
        train_generator = train_datagen.flow_from_directory(
            head_data_path,
            target_size=target_size,
            batch_size=batch_size,
            class_mode='binary')
        self.label = train_generator.class_indices  # {'fake': 0, 'real': 1}
        return train_generator

    def generate_validation(self, batch_size: int = 32, target_size: (int, int) = (256, 256)):
        head_data_path = Config.path(Config.slic_result_path, self.K, Config.name_validation)
        real_path = Config.path(head_data_path, Config.name_real)
        fake_path = Config.path(head_data_path, Config.name_fake)
        real_list = Config.get_child_folder(real_path)  # 存储真图片的文件夹 每张图片一个文件夹
        fake_list = Config.get_child_folder(fake_path)
        image_list = real_list + fake_list  # 所有图片的子文件夹
        boundary = len(real_list)
        max_len = len(image_list)

        steps = 0
        finish_flag = False
        while True:
            x_list = []  # 存储图片的数组，每次返回后清零 （len/batch, 20, 300, 300, 3）
            y_list = []
            for i in range(steps, min(steps + batch_size, max_len)):
                tmp_list = []  # （20, 300, 300, 3)
                for pic in Config.get_image_file_list(image_list[i]):  # 遍历每一个子文件夹（20）
                    tmp_list.append(np.array(image.load_img(
                        path=pic,
                        target_size=target_size
                    )))
                x_list.append(np.array(tmp_list))  # （20, 300, 300, 3)
                y_list.append(self.label[Config.name_real] if i < boundary else self.label[Config.name_fake])  # 1

            steps += batch_size
            if steps >= max_len:
                steps = 0
                finish_flag = True
            # (n, 20, 300, 300, 3)  (n, 1)
            yield np.array(x_list), np.array(y_list), finish_flag
            finish_flag = False

    def fit(self, model: tf.keras.models.Model) -> tf.keras.models.Model:
        check = Config.path_exist(Config.path(Config.checkpoint_path, str(self.K), self.net_name))
        checkpoint_path = check + '/cp-{epoch:04d}.ckpt'
        checkpoint_dir = os.path.dirname(checkpoint_path)

        cp_callback = ModelCheckpoint(
            filepath=checkpoint_path,
            monitor='accuracy',
            save_best_only=True,
            model='auto',
            save_weights_only=True,
            save_freq=1
        )
        reduce_lr = ReduceLROnPlateau(
            monitor='accuracy',
            factor=0.1,
            patience=2,
        )
        train = self.generate_train(
                batch_size=self.batch_size,
                target_size=(self.image_width, self.image_height)
            )
        validation = Validation(
                    validation_gen=self.generate_validation(
                        batch_size=self.batch_size,
                        target_size=(self.image_width, self.image_height)
                    ),
                    label=self.label,  # {'fake': 0, 'real': 1}
                    status='{} slice with {} Net'.format(self.K, self.net_name)
                )
        initial_epoch = 0
        if os.path.exists(check):
            latest = tf.train.latest_checkpoint(checkpoint_dir)
            if latest:
                model.load_weights(latest)
                tmp_len = len(checkpoint_dir) + 4  # 路径长度
                initial_epoch = int(latest[tmp_len:tmp_len + 4]) - 1
        model.fit_generator(
            generator=train,
            epochs=self.nb_epoch,
            # steps_per_epoch=nb_train_samples,
            # validation_data=validation_generator,
            # validation_steps=nb_validation_samples
            callbacks=[
                cp_callback,
                reduce_lr,
                validation
            ],
            initial_epoch=initial_epoch
        )
        return model

    def save(self, model: tf.keras.models.Model):
        model.save_weights(Config.path_exist(Config.model_path) + '{}_{}_fine-tuning.h5'.format(self.K, self.net_name))
        self.save_history(model.history, Config.path_exist(Config.history_path) + '{}_{}_history.txt'.format(self.K, self.net_name))

    def run(self):
        if not os.path.isfile(Config.path_exist(Config.model_path) + '{}_{}_fine-tuning.h5'.format(self.K, self.net_name)):
            Debug.info("Net_Processing: {} net is running with {} slices".format(self.net_name, self.K))
            model = self.create_model()
            self.save(self.fit(model))
        else:
            Debug.info('{} net exists, skipping...'.format(self.net_name))


'''
    given a folder with images of triangles, squares, and circles of any size,
    resize images and write out pixel values as a csv
'''

from __future__ import print_function
from PIL import Image
import cv2
import numpy as np
import os, sys

SHAPE_LABELS = {'square': 0, 'circle': 1, 'triangle': 2}
dataFolder = 'data'

# Resize all the images in the data folder and put them in a new sub-directory
def batch_resize(folders, w, h, color = 0, edges = False):
    for folder in folders:
        path = dataFolder + '/' + folder + '/'
        dirs = os.listdir( path )
        resized_img_path = dataFolder + '/' + folder + '_resized/'
        if not os.path.exists(resized_img_path):
            os.makedirs(resized_img_path)
        for item in dirs:
            if os.path.isfile(path + item):
                f, e = os.path.splitext(item)
                print(e)
                # make sure there only te right file types are used
                if (e == '.png' or e == '.jpg'):
                    # create a PIL Image with the file
                    ##im = Image.open(path + item)
                    im = cv2.imread(path+item, color)
                    # resize
                    ##imResize = im.resize((w,h), Image.ANTIALIAS)
                    imResize = cv2.resize(im, (w,h), interpolation = cv2.INTER_AREA)
                    if(edges == True):
                        imResize = make_edge_img(imResize)
                    # save
                    filename = os.path.basename(path + item)
                    filename, extension = os.path.splitext(filename)
                    ##imResize.save(resized_img_path + filename + '_resized.png', 'png')
                    cv2.imwrite(resized_img_path + filename + '_resized.png', imResize)

# creates a file with all the pixels of each image written out with ' ' delimiter
# images parameter is an array of PIL Images

def createDataset(images):
    if not os.path.exists('train/'):
        os.makedirs('train/')

    random_train = np.arange(len(images))

    np.random.shuffle(random_train)

    # write out files for images
    with open('train/images_train.csv', 'w+') as a, open('train/images_test.csv', 'w+') as b:
        for index in range(len(images)):
            image_index = random_train[index]
            image = images[image_index]
            pixels = list(image[1].getdata())
            pixels = list(map((lambda x: x/255), pixels))
            pixels_string = ','.join(map(str, pixels))
            classification = SHAPE_LABELS[image[0]]
            if (index < len(images)*9/10):
                a.write(pixels_string + '\n')
            else:
                b.write(pixels_string + '\n')

    # write out files for labels
    with open('train/labels_train.csv', 'w+') as a, open('train/labels_test.csv', 'w+') as b:
        for index in range(len(images)):
            image_index = random_train[index]
            image = images[image_index]
            classification = SHAPE_LABELS[image[0]]
            if (index < len(images)*9/10):
                a.write(str(classification) + '\n')
            else:
                b.write(str(classification) + '\n')

# make a PIL Image instance for each image and return a list of images
def createImages(folders):
    images = []
    for folder in folders:
        path = dataFolder + '/' + folder + '_resized/'
        if os.path.exists(path):
            for item in os.listdir(path):
                new_image = Image.open(path + item)
                new_image = new_image.convert('L')
                images = images + [(folder, new_image)]
    return images

def make_edge_img(image):
    edges = cv2.Canny(image,100,200)
    return edges

if __name__ == '__main__':
    # create a list of file names where the images are stored
    folders = ['square', 'circle', 'triangle']

    import argparse
    parser = argparse.ArgumentParser(
        description='resize the datasets in data folder, and create new training and testing files'
    )
    parser.add_argument('width', type=int, help='set the width you would like the dataset to be resized to')
    parser.add_argument('height', type=int, help='set the height you would like the dataset to be resized to')
    args = parser.parse_args()

    # create new directories of images (width, height) for the given directories of images
    batch_resize(folders, args.width, args.height, color = 0, edges=False)

    # find all resized data and make a train folder
    createDataset(createImages(folders))


from django.shortcuts import render_to_response
import numpy as np
import matplotlib.pyplot as plt, mpld3
import mpld3
from pywordcloud import pywordcloud

def home(request):
    import matplotlib.pyplot as plt, mpld3

    # Graph
    fig = plt.figure()
    x = [4, 12, 14, 4, 22, 18]
    y = [3, 10, 16, 3, 20, 17]
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16], mec='w', mew=5, ms=20)
    graph1 = mpld3.fig_to_html(fig)

    # Graph 2
    fig = plt.figure()
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
    graph2 = mpld3.fig_to_html(fig)

    # Histogram
    fig = plt.figure()
    ax = fig.add_subplot(111, axisbg='#EEEEEE')
    ax.grid(color='white', linestyle='solid')

    x = np.random.normal(size=1000)
    ax.hist(x, 30, histtype='stepfilled', fc='lightblue', alpha=0.5);
    histogram = mpld3.fig_to_html(fig)

    return render_to_response('election_prediction_site/index.html', {'graph1': graph1,
                                                                      'graph2': graph2,
                                                                      'histogram': histogram})

class PlotFactory:

    def get_graph(self, x, y):
        fig = plt.figure()

        plt.plot(x, y, mec='w', mew=5, ms=20)
        figure = mpld3.fig_to_html(fig)
        return figure

    def get_wordcloud(self, corpus, using='pywordcloud'):
        #mask = imread("stormtrooper_mask.png")
        #wc = WordCloud(max_words=1000, mask=mask, margin=10,
        #       random_state=1).generate(corpus)
        #return wc.to_html()

        return pywordcloud.create(corpus,
            outfile="wordcloud.html",
            uppercase=False,
            showfreq=True,
            frequency=100,
            removepunct = False,
            minfont = 1.5,
            maxfont = 6,
            hovercolor="green",
            showborder=False,
            fontfamily='calibri',
            width="1000px",
            height="400px",
            #colorlist = ["red","blue","yellow","black","green"]
        )

    def get_histogram(self, x):
        fig = plt.figure()


### 使用time库
import time

### datetime字符串转换为时间戳
# datetime类型字符串
datetime = "2020-03-11 19:26:13"
# 先将str转换为时间元组
# time.strptime(str,fmt='...')根据fmt的格式把一个时间字符串str解析为时间元组。
timetuple = time.strptime(datetime, "%Y-%m-%d %H:%M:%S") # time.struct_time(tm_year=2020, tm_mon=3, tm_mday=11, tm_hour=19, tm_min=26, tm_sec=13, tm_wday=2, tm_yday=71, tm_isdst=-1) <class 'time.struct_time'>
# 将时间元组转换成时间戳
# time.mktime()接受时间元组并返回时间戳（1970纪元后经过的浮点秒数）
timestamp = time.mktime(timetuple) # 1583925973.0 <class 'float'>

### 时间戳转换为datetime字符串
# float类型时间戳，小数点前10位
timestamp = time.time() # 1584278483.256262 <class 'float'>
# 时间戳转换为时间元组
# time.localtime([secs])接收时间戳（1970纪元后经过的浮点秒数），返回当地时间下的时间元组t
timetuple = time.localtime(timestamp) # time.struct_time(tm_year=2020, tm_mon=3, tm_mday=15, tm_hour=21, tm_min=21, tm_sec=23, tm_wday=6, tm_yday=75, tm_isdst=0) <class 'time.struct_time'>
# 时间元组转为datetime字符串
# time.strftime(fmt[,tupletime])接收时间元组，返回以可读字符串表示的当地时间，格式由fmt决定。
datetime = time.strftime("%Y-%m-%d %H:%M:%S", timetuple) # 2020-03-15 21:21:23 <class 'str'>

import datetime
### 使用datetime库显示datetime字符串
# 时间戳转为datetime字符串格式
timeStamp = time.time() # 1584278312.2667465 <class 'float'>
dateArray = datetime.datetime.fromtimestamp(timeStamp) # 2020-03-15 21:18:32.266747 <class 'datetime.datetime'>
# datetime.datetime.strftime()方法接收日期格式返回该格式字符串
otherStyleTime = dateArray.strftime("%Y-%m-%d %H:%M:%S") # 2020-03-15 21:18:32 <class 'str'>

# datetime获取当前时间，转为字符串格式
now = datetime.datetime.now() # 2020-03-15 20:58:00.147842 <class 'datetime.datetime'>
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S") # 2020-03-15 20:58:00 <class 'str'>

### python中时间日期格式化符号：
# %y 两位数的年份表示（00-99）
# %Y 四位数的年份表示（000-9999）
# %m 月份（01-12）
# %d 月内中的一天（0-31）
# %H 24小时制小时数（0-23）
# %I 12小时制小时数（01-12）
# %M 分钟数（00=59）
# %S 秒（00-59）
# %a 本地简化星期名称
# %A 本地完整星期名称
# %b 本地简化的月份名称
# %B 本地完整的月份名称
# %c 本地相应的日期表示和时间表示
# %j 年内的一天（001-366）
# %p 本地A.M.或P.M.的等价符
# %U 一年中的星期数（00-53）星期天为星期的开始
# %w 星期（0-6），星期天为星期的开始
# %W 一年中的星期数（00-53）星期一为星期的开始
# %x 本地相应的日期表示
# %X 本地相应的时间表示
# %Z 当前时区的名称
# %% %号本身

### struct_time时间元组格式
# tm_year: 公历年
# tm_mon: 月，1-12
# tm_mday: 日，1-31
# tm_hour: 时，0-23
# tm_min: 分，0-59
# tm_sec: 秒，0-61 (60或61 是闰秒)
# tm_wday: 星期，0-6 (0是周一)
# tm_yday: 一年中的第几天，1-366
# tm_isdst: 是否为夏令时，值有：1(夏令时)、0(不是夏令时)、-1(未知)，默认-1


from itertools import (chain,
                       combinations)

import pytest
from hypothesis import given
from robust.linear import segment_contains

from bentley_ottmann.core.linear import (SegmentsRelationship,
                                         segments_intersections,
                                         segments_relationship)
from bentley_ottmann.hints import Contour
from bentley_ottmann.planar import edges_intersect
from tests.utils import (contour_to_segments,
                         reverse_point_coordinates)
from . import strategies

@given(strategies.contours)
def test_basic(contour: Contour) -> None:
    result = edges_intersect(contour)

    assert isinstance(result, bool)

@given(strategies.triangular_contours)
def test_base_case(contour: Contour) -> None:
    result = edges_intersect(contour)

    left_vertex, mid_vertex, right_vertex = sorted(contour)
    assert result is segment_contains((left_vertex, right_vertex), mid_vertex)

@given(strategies.non_triangular_contours)
def test_step(contour: Contour) -> None:
    first_vertex, *rest_vertices = contour

    result = edges_intersect(rest_vertices)
    next_result = edges_intersect(contour)

    first_edge, last_edge = ((first_vertex, rest_vertices[0]),
                             (rest_vertices[-1], first_vertex))
    rest_edges = contour_to_segments(rest_vertices)
    assert (next_result
            is (result
                and len(rest_vertices) > 2
                and (any(segments_intersections(rest_edges[index],
                                                rest_edges[other_index])
                         for index in range(len(rest_edges) - 1)
                         for other_index in chain(
                            range(index - 1),
                            range(index + 2, len(rest_edges) - 1)))
                     or any(segments_relationship(edge, other_edge)
                            is SegmentsRelationship.OVERLAP
                            for edge, other_edge in combinations(
                                    rest_edges[:-1], 2)))
                or any(segments_intersections(first_edge, edge)
                       for edge in rest_edges[1:-1])
                or any(segments_intersections(last_edge, edge)
                       for edge in rest_edges[:-2])
                or len(rest_vertices) > 1
                and (segments_relationship(first_edge, rest_edges[0])
                     is SegmentsRelationship.OVERLAP
                     or segments_relationship(first_edge, last_edge)
                     is SegmentsRelationship.OVERLAP
                     or segments_relationship(last_edge, rest_edges[0])
                     is SegmentsRelationship.OVERLAP)))

@given(strategies.contours)
def test_reversed(contour: Contour) -> None:
    result = edges_intersect(contour)

    assert result is edges_intersect(contour[::-1])

@given(strategies.contours)
def test_reversed_coordinates(contour: Contour) -> None:
    result = edges_intersect(contour)

    assert result is edges_intersect([reverse_point_coordinates(vertex)
                                      for vertex in contour])

@given(strategies.degenerate_contours)
def test_degenerate_contour(contour: Contour) -> None:
    with pytest.raises(ValueError):
        edges_intersect(contour)