from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import numpy as np
from Orange.data import ContinuousVariable, Domain, Table, Variable
from Orange.misc.utils.embedder_utils import EmbedderCache
from Orange.util import dummy_callback

from orangecontrib.imageanalytics.local_embedder import LocalEmbedder
from orangecontrib.imageanalytics.server_embedder import ServerEmbedder
from orangecontrib.imageanalytics.squeezenet_model import SqueezenetModel
from orangecontrib.imageanalytics.utils.image_utils import extract_paths

MODELS = {
    "inception-v3": {
        "name": "Inception v3",
        "description": "Google's Inception v3 model trained on ImageNet.",
        "target_image_size": (299, 299),
        "layers": ["penultimate"],
        "order": 0,
        # batch size tell how many images we send in parallel, this number is
        # high for inception since it has many workers, but other embedders
        # send less images since bottleneck are workers, this way we avoid
        # ReadTimeout because of images waiting in a queue at the server
        "batch_size": 500,
    },
    "painters": {
        "name": "Painters",
        "description": "A model trained to predict painters from artwork\nimages.",
        "target_image_size": (256, 256),
        "layers": ["penultimate"],
        "order": 4,
        "batch_size": 500,
    },
    "deeploc": {
        "name": "DeepLoc",
        "description": "A model trained to analyze yeast cell images.",
        "target_image_size": (64, 64),
        "layers": ["penultimate"],
        "order": 5,
        "batch_size": 500,
    },
    "vgg16": {
        "name": "VGG-16",
        "description": "16-layer image recognition model trained on\nImageNet.",
        "target_image_size": (224, 224),
        "layers": ["penultimate"],
        "order": 2,
        "batch_size": 500,
    },
    "vgg19": {
        "name": "VGG-19",
        "description": "19-layer image recognition model trained on\nImageNet.",
        "target_image_size": (224, 224),
        "layers": ["penultimate"],
        "order": 3,
        "batch_size": 500,
    },
    "openface": {
        "name": "openface",
        "description": "Face recognition model trained on FaceScrub and\n"
        "CASIA-WebFace datasets.",
        "target_image_size": (256, 256),
        "layers": ["penultimate"],
        "order": 6,
        "batch_size": 500,
    },
    "squeezenet": {
        "name": "SqueezeNet",
        "description": "Deep model for image recognition that achieves \n"
        "AlexNet-level accuracy on ImageNet with \n"
        "50x fewer parameters.",
        "target_image_size": (227, 227),
        "layers": ["penultimate"],
        "order": 1,
        "is_local": True,
        "batch_size": 16,
        "model": SqueezenetModel,
    },
}

class ImageEmbedder:
    """
    Client side functionality for accessing a remote image embedding backend.

    Attributes
    ----------
    model
        Name of the model, must be one from MODELS dictionary
    server_url
        The url of the server with embedding backend.

    Examples
    --------
    >>> import Orange
    >>> from orangecontrib.imageanalytics.image_embedder import ImageEmbedder

    >>> # embedding from list of paths
    >>> image_file_paths = ['image001.jpg', 'image001.jpg']
    >>> with ImageEmbedder(model='model_name') as emb:
    ...    embeddings = emb(image_file_paths)

    >>> # embedding from orange tabl
    >>> table = Orange.data.Table('Table_with_image_path.csv')
    >>> with ImageEmbedder(model='model_name') as emb:
    ...    embeddings = emb(table, col="image_path_column")
    """

    _embedder = None

    def __init__(
        self,
        model: str = "inception-v3",
        server_url: str = "https://api.garaza.io/",
    ):
        self.server_url = server_url
        self.model = model
        self._model_settings = self._get_model_settings_confidently()

    def is_local_embedder(self) -> bool:
        """
        Tells whether selected embedder is local or not.
        """
        return self._model_settings.get("is_local", False)

    def _get_model_settings_confidently(self) -> Dict[str, Any]:
        """
        Returns the dictionary with model settings

        Returns
        -------
        The dictionary with model settings
        """
        if self.model not in MODELS.keys():
            model_error = "'{:s}' is not a valid model, should be one of: {:s}"
            available_models = ", ".join(MODELS.keys())
            raise ValueError(model_error.format(self.model, available_models))
        return MODELS[self.model]

    def _init_embedder(self) -> None:
        """
        Init local or server embedder.
        """
        if self.is_local_embedder():
            self._embedder = LocalEmbedder(self.model, self._model_settings)
        else:
            self._embedder = ServerEmbedder(
                self.model,
                self._model_settings["batch_size"],
                self.server_url,
                "image",
                self._model_settings["target_image_size"]
            )

    def __call__(
        self,
        data: Union[Table, List[str], np.array],
        col: Optional[Union[str, Variable]] = None,
        callback: Optional[Callable] = dummy_callback,
    ) -> Union[Tuple[Table, Table, int], List[List[float]]]:
        """
        Embedd images.

        Parameters
        ----------
        data
            Data contains the path to images (locally or online). It can be
            Orange data table or list/array. When data table on input col
            parameter must define which column in the table contains images.
        col
            The column with images in Orange data table. It is not required
            when data are list or array.
        callback
            Optional callback - function that is called for every embedded
            image and is used to report the progress.

        Returns
        -------
        Embedded images. When data is Table it returns tuple with two tables:
        1) original table with embedded images appended to it, 2) table with
        skipped images, 3) number of skipped images.
        When data is array/list it returns the list of list with embeddings,
        each image is represented with vector of numbers.
        """
        assert data is not None
        assert isinstance(data, (np.ndarray, list, Table))
        self._init_embedder()
        if isinstance(data, Table):
            assert col is not None, "Please provide a column for image path"
            # if table on input tables on output
            return self.from_table(data, col=col, callback=callback)
        elif isinstance(data, (np.ndarray, list)):
            # if array-like on input array-like on output
            return self._embedder.embedd_data(data, callback=callback)

    def from_table(
        self,
        data: Table,
        col: Union[str, Variable] = "image",
        callback: Callable = None,
    ) -> Tuple[Table, Table, int]:
        """
        Calls embedding when data are provided as a Orange Table.

        Parameters
        ----------
        data
            Data table with image paths
        col
            The column with image paths
        callback
            Optional callback - function that is called for every embedded
            image and is used to report the progress.
        """
        file_paths = extract_paths(data, data.domain[col])
        embeddings_ = self._embedder.embedd_data(file_paths, callback=callback)
        return ImageEmbedder.prepare_output_data(data, embeddings_)

    def __enter__(self) -> "ImageEmbedder":
        return self

    def __exit__(self, _, __, ___) -> None:
        pass

    def __del__(self) -> None:
        self.__exit__(None, None, None)

    @staticmethod
    def construct_output_data_table(
            embedded_images: Table, embeddings_: np.ndarray
    ) -> Table:
        """
        Join the orange table with embeddings.

        Parameters
        ----------
        embedded_images
            Table with images that were successfully embedded
        embeddings_
            Embeddings for images from table

        Returns
        -------
        Table with added embeddings to data.
        """
        new_attributes = [
            ContinuousVariable("n{:d}".format(d)) for d in range(embeddings_.shape[1])
        ]
        # prevent embeddings to be shown in long drop-downs in e.g. scatterplot
        for a in new_attributes:
            a.attributes["hidden"] = True

        domain_new = Domain(
            list(embedded_images.domain.attributes) + new_attributes,
            embedded_images.domain.class_vars,
            embedded_images.domain.metas,
        )
        table = embedded_images.transform(domain_new)
        with table.unlocked(table.X):  # writing to fresh part, can be unlocked
            table[:, new_attributes] = embeddings_

        return table

    @staticmethod
    def prepare_output_data(
        input_data: Table, embeddings_: List[List[float]]
    ) -> Tuple[Table, Table, int]:
        """
        Prepare output data when data table on input.

        Parameters
        ----------
        input_data
            The table with original data that are joined with embeddings
        embeddings_
            List with embeddings

        Returns
        -------
        Tuple where first parameter is table with embedded images, the second
        table with skipped images and third the number of skipped images.
        """
        skipped_images_bool = [x is None or len(x) == 0 for x in embeddings_]

        if np.any(skipped_images_bool):
            skipped_images = input_data[skipped_images_bool].copy()
            skipped_images.name = "Skipped images"
            num_skipped = len(skipped_images)
        else:
            num_skipped = 0
            skipped_images = None

        embedded_images_bool = np.logical_not(skipped_images_bool)

        if np.any(embedded_images_bool):
            embedded_images = input_data[embedded_images_bool]

            embeddings_ = [
                e for e, b in zip(embeddings_, embedded_images_bool) if b
            ]
            embeddings_ = np.vstack(embeddings_)

            embedded_images = ImageEmbedder.construct_output_data_table(
                embedded_images, embeddings_
            )
            embedded_images.ids = input_data.ids[embedded_images_bool]
            embedded_images.name = "Embedded images"
        else:
            embedded_images = None

        return embedded_images, skipped_images, num_skipped

    def clear_cache(self) -> None:
        """
        Function clear cache for the selected embedder. If embedder is loaded
        cache is cleaned from its dict otherwise we load cache and clean it
        from file.
        """
        if self._embedder:
            # embedder is loaded so we clean its cache
            self._embedder.clear_cache()
        else:
            # embedder is not initialized yet - clear it cache from file
            cache = EmbedderCache(self.model)
            cache.clear_cache()

if __name__ == "__main__":
    image_file_paths = ["tests/test_images/example_image_0.jpg"]
    # with ImageEmbedder(model='inception-v3') as embedder:
    with ImageEmbedder(model="squeezenet") as embedder:
        embedder.clear_cache()
        print(embedder(image_file_paths))


import os
import json
class Config:
    @staticmethod
    def getConfig(name: str):
        fname = name
        if not os.path.exists(fname):
            return None
        with open(fname, "r") as fp:
            raw = fp.read()
        return json.loads(raw)
    @staticmethod
    def saveConfig(name: str, conf: dict):
        fname = name
        dirname = os.path.dirname(fname)
        if not os.path.exists(dirname):
            os.mkdir(dirname)
        with open(fname, "w") as fp:
            fp.write(json.dumps(conf))

    @staticmethod
    def getValue(confname: str, key: str):
        conf = Config.getConfig(confname)
        if conf is None:
            return None
        if key in conf:
            return conf[key]
        return None
    @staticmethod
    def setValue(confname: str, key: str, val):
        conf = Config.getConfig(confname)
        if conf is None:
            return
        conf[key] = val
        fname = confname
        with open(fname, "w") as fp:
            fp.write(json.dumps(conf))


from tensorboard.backend.event_processing import event_accumulator
import matplotlib.pyplot as plt
from os import listdir
from os.path import isfile, join
import math
import numpy as np
import sys
import tuneConfigurations
import warnings
from ray.tune import Analysis

plt.rcParams['figure.dpi'] = 200

def plot(configs, sets='test', save=False, colorsFirst=False, title="", limits=None):
    keyLoss = 'loss'

    lineStyles = ['solid', 'dashed', 'dotted', 'dashdot']
    lineColors = ['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9']

    if not type(configs) is list:
        configs = [configs]

    if not type(sets) is list:
        sets = [sets]

    if len(configs)*len(sets) > len(lineStyles)*len(lineColors):
        raise ValueError("Too many curves to plot, {} of max {}.".format(len(configs)*len(sets), len(lineStyles)*len(lineColors)))

    fig = plt.figure(figsize=(8,9))
    #gs = fig.add_gridspec(2,1)
    #axLoss = fig.add_subplot(gs[0, 0])
    #axAcc = fig.add_subplot(gs[1, 0])
    axLoss = fig.add_axes([0.07, 0.53, 0.6, 0.42])
    axAcc = fig.add_axes([0.07, 0.05, 0.6, 0.42])
    #fig, (axLoss, axAcc) = plt.subplots(2)
    metrics = []
    i = 0
    for set in sets:
        for config in configs:
            myConfig = getattr(sys.modules['tuneConfigurations'], config)
            metrics.append(myConfig.trackMetric)

            #use tune to pick best in run
            analysis = Analysis(join("tuneOutput", myConfig.path))
            mode = ("max" if myConfig.bestSign == '>' else "min")
            #print("best hyperparameters for {}: {}".format(config, analysis.get_best_config(metric=myConfig.bestKey, mode=mode)))
            tunePath = analysis.get_best_logdir(metric=myConfig.bestKey, mode=mode)
            expPath = join(tunePath,'files', 'tensorBoard')
            keyAcc = "{}_{}".format(metrics[-1], metrics[-1])

            # metrics.append(getattr(sys.modules['configurations'], config).trackMetric)
            # keyAcc = "{}_{}".format(metrics[-1], metrics[-1])
            # expPath = join('files', getattr(sys.modules['configurations'], config).path, 'tensorBoard')
            try:
                keys = [f for f in listdir(expPath) if not isfile(join(expPath, f))]
            except FileNotFoundError:
                warnings.warn("Configuration {} not present. Skipping.".format(config))
                continue

            points = {}
            for k in keys:
                eventPathPart = join(expPath, k, set)
                for runPath in sorted([f for f in listdir(eventPathPart) if isfile(join(eventPathPart, f))]):
                    eventPath = join(eventPathPart, runPath)
                    ea = event_accumulator.EventAccumulator(eventPath)
                    ea.Reload()
                    if not k in points:
                        points[k] = [[v.step for v in ea.Scalars(k)], [v.value for v in ea.Scalars(k)]]
                    else:
                        points[k][0].extend([v.step for v in ea.Scalars(k)])
                        points[k][1].extend([v.value for v in ea.Scalars(k)])

            if limits is None:
                valuesLoss = points[keyLoss]
                valuesAcc = points[keyAcc]
            else:
                valuesLoss = [points[keyLoss][i][limits[0]:limits[1]] for i in [0,1]]
                valuesAcc = [points[keyAcc][i][limits[0]:limits[1]] for i in [0,1]]

            linesLoss = axLoss.plot(valuesLoss[0], valuesLoss[1], label="{} {}".format(config, set))
            linesAcc = axAcc.plot(valuesAcc[0], valuesAcc[1], label="{} {}".format(config, set))
            if colorsFirst:
                linesLoss[0].set_color(lineColors[i%len(lineColors)])
                linesLoss[0].set_linestyle(lineStyles[(i//len(lineColors))%len(lineStyles)])
                linesAcc[0].set_color(lineColors[i%len(lineColors)])
                linesAcc[0].set_linestyle(lineStyles[(i//len(lineColors))%len(lineStyles)])
            else:
                linesLoss[0].set_linestyle(lineStyles[i%len(lineStyles)])
                linesLoss[0].set_color(lineColors[(i//len(lineStyles))%len(lineColors)])
                linesAcc[0].set_linestyle(lineStyles[i%len(lineStyles)])
                linesAcc[0].set_color(lineColors[(i//len(lineStyles))%len(lineColors)])

            i += 1

    # axLoss.legend(loc='upper left', bbox_to_anchor=(1, 1),
    #          ncol=math.ceil(len(configs)/20), fancybox=True, shadow=True)
    #axLoss.set_title(title)
    axLoss.set_xlabel("Epoch")
    axLoss.set_ylabel("Loss")

    # axLoss.set_xticks(np.arange(0, round(axLoss.get_xlim()[1])+10, 10))
    # axLoss.set_xticks(np.arange(round(axLoss.get_xlim()[0]), round(axLoss.get_xlim()[1])+1, 1), minor=True)
    # axLoss.set_yticks(np.arange(0, round(axLoss.get_ylim()[1])+0.05, 0.05))
    # axLoss.set_yticks(np.arange(0, round(axLoss.get_ylim()[1])+0.01, 0.01), minor=True)
    axLoss.grid(which='both')
    axLoss.grid(which='minor', alpha=0.2)
    axLoss.grid(which='major', alpha=0.5)

    # axAcc.legend(loc='upper left', bbox_to_anchor=(1, 1),
    #           ncol=math.ceil(len(configs)/20), fancybox=True, shadow=True)
    #axAcc.set_title(title)
    axAcc.set_xlabel("Epoch")
    axAcc.set_ylabel("Metric ({})".format(list(dict.fromkeys(metrics))))
    #axAcc.set_ylim(0.49,1.)

    #axAcc.set_xticks(np.arange(0, round(axAcc.get_xlim()[1])+10, 10))
    #axAcc.set_xticks(np.arange(round(axAcc.get_xlim()[0]), round(axAcc.get_xlim()[1])+1, 1), minor=True)
    axAcc.set_yticks(np.arange(0.5, 1.01, 0.1))
    axAcc.set_yticks(np.arange(0.49, 1.01, 0.01), minor=True)
    axAcc.grid(which='both')
    axAcc.grid(which='minor', alpha=0.2)
    axAcc.grid(which='major', alpha=0.5)

    fig.suptitle(title)

    handles, labels = axAcc.get_legend_handles_labels()
    fig.legend(handles, labels, bbox_to_anchor=(0.68, 0.95), loc=2, borderaxespad=0.)
    # fig.legend(handles, labels, loc='upper left', bbox_to_anchor=(1, 1),
    #              bbox_transform = plt.gcf().transFigure,
    #              ncol=math.ceil(len(configs)/20), fancybox=True, shadow=True)

    # plt.legend( handles, labels, loc = 'upper left', bbox_to_anchor = (0.9,-0.1,2,2),
    #         bbox_transform = plt.gcf().transFigure )

    # plt.figlegend(handles, labels, loc='upper center', bbox_to_anchor=(0.5, 0), bbox_transform=plt.gcf().transFigure)

    # fig.subplots_adjust(wspace=2, hspace=2,left=0,top=2,right=2,bottom=0)

    #fig.tight_layout()

    #fig.subplots_adjust(right=2)

    fig.show()

    if save:
        fig.savefig('img/plot.eps')#, bbox_inches = 'tight')#, pad_inches = 0)

    plt.close()

def printMetrics(configs, printAllConfigs=False):
    sets = ['train', 'valid', 'test']

    if not type(configs) is list:
        configs = [configs]

    bestConfig = None
    points = {}
    for config in configs:
        myConfig = getattr(sys.modules['tuneConfigurations'], config)
        metric = myConfig.trackMetric

        try:
            #use tune to pick best in run
            analysis = Analysis(join("tuneOutput", myConfig.path))
        except ValueError:
            warnings.warn("Configuration {} not present. Skipping.".format(config))
            continue
        mode = ("max" if myConfig.bestSign == '>' else "min")
        print("best hyperparameters for {}: {}".format(config, analysis.get_best_config(metric=myConfig.bestKey, mode=mode)))
        tunePath = analysis.get_best_logdir(metric=myConfig.bestKey, mode=mode)
        expPath = join(tunePath,'files', 'tensorBoard')
        keyAcc = "{}_{}".format(metric, metric)
        try:
            keys = [f for f in listdir(expPath) if not isfile(join(expPath, f))]
        except FileNotFoundError:
            warnings.warn("Configuration {} not present. Skipping.".format(config))
            continue

        points[config] = {}
        for set in sets:
            points[config][set] = {}
            for k in keys:
                eventPathPart = join(expPath, k, set)
                for runPath in sorted([f for f in listdir(eventPathPart) if isfile(join(eventPathPart, f))]):
                    eventPath = join(eventPathPart, runPath)
                    ea = event_accumulator.EventAccumulator(eventPath)
                    ea.Reload()
                    if not k in points[config][set]:
                        points[config][set][k] = [[v.step for v in ea.Scalars(k)], [v.value for v in ea.Scalars(k)]]
                    else:
                        points[config][set][k][0].extend([v.step for v in ea.Scalars(k)])
                        points[config][set][k][1].extend([v.value for v in ea.Scalars(k)])

        bestSign = myConfig.bestSign
        if bestSign == '>':
            bestI = np.argmax(points[config]['valid'][keyAcc][1]) #point where better metric
        else:
            bestI = np.argmin(points[config]['valid'][keyAcc][1]) #point where better metric

        thisConfig = {
            'name': config,
            'epoch': bestI+1,
            'train': points[config]['train'][keyAcc][1][bestI],
            'valid': points[config]['valid'][keyAcc][1][bestI],
            'test': points[config]['test'][keyAcc][1][bestI],
        }
        if printAllConfigs:
            print("{} (epoch {}):\ttrain {:.3};\tvalid {:.3};\ttest {:.3}".format(thisConfig['name'], thisConfig['epoch'], thisConfig['train'], thisConfig['valid'], thisConfig['test']))

        if bestConfig is None or (bestSign == '>' and thisConfig['valid']>bestConfig['valid']) or (bestSign == '<' and thisConfig['valid']<bestConfig['valid']):
            bestConfig = thisConfig

    if not bestConfig is None:
        print("BEST ==== {} (epoch {}):\ttrain {:.3};\tvalid {:.3};\ttest {:.3}".format(bestConfig['name'], bestConfig['epoch'], bestConfig['train'], bestConfig['valid'], bestConfig['test']))


import cv2
import os
import numpy as np
import multiprocessing
from concurrent.futures import ThreadPoolExecutor

def apply_clahe(image):
    lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
    l, a, b = cv2.split(lab)
    clahe = cv2.createCLAHE(clipLimit=4.0, tileGridSize=(8, 8))
    cl = clahe.apply(l)
    merged = cv2.merge([cl, a, b])
    result = cv2.cvtColor(merged, cv2.COLOR_LAB2BGR)
    return result

def gamma_correction(image: np.ndarray, gamma):
    gamma_corrected = np.power(image/255.0, gamma)
    gamma_corrected = gamma_corrected*255.0
    gamma_corrected = gamma_corrected.astype(np.uint8)
    return gamma_corrected

root_dir = "Queensland Dataset CE42/"
classes = ["BCC/", "IEC/", "SCC/"]
images_list = []
io_files = []

for idx, obj in enumerate(classes):
    image_file = os.listdir(root_dir + obj + "Images/")

    for _, image in enumerate(image_file):
        img = root_dir + obj + "Images/" + image
        images_list.append(img)

num_cores = multiprocessing.cpu_count()
print(f"Number of cores: {num_cores}" )

# Preprocessing the image with CLAHE and Gamma-correction
def preprocess_image(image_path):
    image = cv2.imread(image_path)
    clahe_img = apply_clahe(image)
    gamma_img = gamma_correction(clahe_img, 3)
    resize_img = cv2.resize(gamma_img, (256, 256))
    return resize_img

# Processing image in parallel using ThreadPoolExecutor
def process_image_parallel(image):
    return preprocess_image(image)

batch_size = 50
preprocessed_image = []
with ThreadPoolExecutor(max_workers=4) as executor:
    for i in range(0, len(images_list), batch_size):
        batch_images = images_list[i : i + batch_size]

        # Using ThreadPoolExecutor.map to to preprocess images in parallel
        preprocess_batch = list(executor.map(process_image_parallel, batch_images))
        preprocessed_image.extend(preprocess_batch)

for idx, obj in enumerate(classes):
    mask_files = os.listdir(root_dir + obj + "Masks/")
    for idx, mask in enumerate(mask_files):
      mask = cv2.imread(root_dir + obj + "Masks/" + mask)
      r_mask = cv2.resize(mask, (256, 256))

      io_files.append(cv2.hconcat([preprocessed_image[idx], r_mask]))

np.save("Preprocessed_data", np.array(io_files))
cv2.imshow("Image + mask: ", io_files[0])
cv2.waitKey(0)


import copy
from SPARQLWrapper import SPARQLWrapper, JSON
from slot_recognition import *
from SPARQL_generation import *

class QueryManager:
    def __init__(self, verbose=False):
        self.__conn = SPARQLWrapper(ENDPOINT_URL)
        self.__verbose = verbose

    def set_verbose(self, verbose):
        if type(verbose) == bool:
            self.__verbose = verbose

    def ask(self, ques):
        query_success = True

        sentence = ques.replace('“', '"').replace('”', '"')

        recognition_result = SlotRecognizer.recognize(sentence)

        # 无法理解问题的情况处理
        if recognition_result is None:
            result_value = None
            result_reply = '对不起，我无法理解您的问题，请换种说法问我吧！'
            query_success = False
            if self.__verbose:
                print('*' * 13)
                print('答案内容：', result_value)
                print('答案回复：', result_reply)
                print('*' * 13)
            query_note = {'success': query_success,
                          'question': ques,
                          'result': result_value,
                          'reply': result_reply, }
            return query_note

        tid = recognition_result[0]
        arguments = recognition_result[1]
        template = templates[tid]
        temp_type = template['type']

        sparql = SparqlGenerator.generate_sparql_from_recognition_result(recognition_result)

        query_result = self.query(sparql)
        result_list = self.parse_result(query_result, temp_type)
        result_value = '、'.join(result_list)
        result_arguments = copy.deepcopy(arguments)
        result_arguments['value'] = result_value

        result_reply = None
        # 没有结果的处理
        if (temp_type == 'select' and result_value == '') or \
            (temp_type == 'count' and result_value == '0'):  # 继续跟踪查询是不是事件名等输错
            checks = template['checks']
            checks_note = []
            for check in checks:
                check_template = checks_templates[check]
                check_temp_type = check_template['type']
                check_sparql = SparqlGenerator.generate_sparql_from_check_template(check_template, arguments)
                check_query_result = self.query(check_sparql)
                check_result_list = self.parse_result(check_query_result, check_temp_type)
                check_result_value = '、'.join(check_result_list)
                if (check_temp_type == 'count' and check_result_value == '0') or \
                    (check_temp_type == 'select' and check_result_value == ''):
                    note = check_template['if_none_reply'].format(**arguments)
                    checks_note.append(note)
            checks_reply = '，'.join(checks_note)
            if checks_reply != '':
                checks_reply += '，'
                result_reply = '对不起，没有查询到结果。' + checks_reply + '请检查您的提问是否有误。'
                query_success = False
            else:
                result_reply = template['none_reply'].format(**arguments)
                query_success = True
        else:
            result_reply = template['reply'].format(**result_arguments)

        if self.__verbose:
            print('*' * 13)
            print('问题：', ques)
            print('模板ID：', tid)
            print('槽识别结果：', arguments)
            print('SPARQL：\n', sparql)
            print('请求返回结果：', query_result)
            print('答案内容：', result_value)
            print('答案回复：', result_reply)
            print('*' * 13)

        query_note = {'success': query_success,
                      'question': ques,
                      'template_id': tid,
                      'arguments': arguments,
                      'SPARQL': sparql,
                      'result': result_value,
                      'reply': result_reply, }

        return query_note

    def query(self, sparql, format=JSON):
        self.__conn.setQuery(sparql)
        self.__conn.setReturnFormat(format)
        query_result = self.__conn.query().convert()
        return query_result

    @staticmethod
    def parse_result(query_result, type):
        bindings = query_result['results']['bindings']
        result_list = []
        key_val = 'x'
        if type == 'count':
            key_val = 'callret-0'
        for item in bindings:
            value = item[key_val]['value']
            if type == 'select':
                index = value.find('#')
                value = value[index + 1:]
            result_list.append(value)
        return result_list


import FeatureProject
from sklearn.linear_model import LogisticRegression
import os
import ROCX
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report, confusion_matrix
import pandas as pd
from sklearn.decomposition import PCA
import time
import numpy as np
'''进行模型的保存和加载'''
import joblib
import csv

def n_components_analysis(n, x_train, y_train, x_val, y_val,result_file,resultPath,classifier,splitFile):  #

    start = time.time()
    pca = PCA(n_components=n)
    result_file.write("特征降维，传递参数为{}\n".format(n))
    # print("特征降维，传递参数为{}".format(n))
    pca.fit(x_train)

    x_train_pca = pca.transform(x_train)
    x_val_pca = pca.transform(x_val)

    result_file.write("开始进行SVM训练")
    # print("开始进行SVM训练")
    ss = LogisticRegression(class_weight='balanced', penalty='l1', solver='liblinear')
    ss.fit(x_train_pca, y_train)
    ## 使用测试集进行预测概率
    y_pred_prob = ss.fit(x_train_pca, y_train).predict_proba(x_val_pca)
    y_pred = ss.fit(x_train_pca, y_train).predict(x_val_pca)
    # str1 = 'classification_report:\n', classification_report(y_val, y_pred,digits=3)
    # result_file.writelines(str1)
    # str1 = 'confusion_matrix:\n',confusion_matrix(y_val, y_pred)
    # result_file.writelines(str1)
    """
    主成分分析的模型结果也需要进行存储
    """
    pd.DataFrame(y_pred_prob).to_csv(resultPath + '/pred_proba.csv')
    pd.DataFrame(y_pred).to_csv(resultPath + '/pred.csv')
    df1 = pd.read_csv(splitFile + '/y_test.csv')
    df2 = pd.read_csv(resultPath + '/pred.csv', index_col='Unnamed: 0')
    df3 = pd.read_csv(resultPath + '/pred_proba.csv', index_col='Unnamed: 0')
    df4 = pd.concat(objs=[df1, df2, df3], axis=1)
    if classifier == 2:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2']
    if classifier == 3:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2', 'pred_prob3']
    df4.to_csv(resultPath + '/' + str(n) + 'result_compare.csv')

    '''将交叉验证结果写入文件'''
    scores = cross_val_score(ss, x_train_pca, y_train, cv=5, scoring='accuracy')
    result_file.write(str('\n训练集交叉验证结果：\n'))
    result_file.writelines(str(scores))
    '''将模型测试评分结果写入文件'''
    score = ss.score(x_val_pca, y_val)  # 多分类单看一个score不恰当，应该看单独的
    result_file.write(str('\n模型测试评分结果：'))
    result_file.writelines(str(score))

    '''将分类报告写入文件'''
    clf_rep = classification_report(y_val, y_pred, digits=3)
    result_file.write(str('\n分类报告：\n'))
    result_file.write(clf_rep)
    '''将混淆矩阵写入文件'''
    cfu_mx = confusion_matrix(y_val, y_pred)
    result_file.write(str('混淆矩阵：\n'))
    result_file.writelines(str(cfu_mx))
    result_file.write('\n-----------------------------------------------------------------------------\n')

    #     ## 绘制roc曲线
    picName = '选择' + str(n) + '的主成分ClassifierAuc.png'
    if classifier == 3:
        ROCX.three_auc_report(n, classifier,y_val,y_pred_prob,resultPath+'/ROC', picName=picName)
    else:
        ROCX.auc_report(n, y_val, y_pred_prob[:, 1], resultPath+'/ROC', picName=picName)

'''
函数参数说明：
classifer：用于说明是进行二分类还是进行三分类
splitFile：存放测试集、训练集的文件路径
resultPath：用于保存结果的文件路径,比如SVM就写到 ...... /result/SVM
'''

def logistic_reg(classifer,splitFile,resultPath, tp):

    if os.path.exists(resultPath) == False:  # 如果存放结果的目标路径不存在，则进行创建
        os.makedirs(resultPath)

    X_train = pd.read_csv(splitFile + '/X_train.csv').iloc[:, 2:].values
    X_test = pd.read_csv(splitFile + '/X_test.csv').iloc[:, 2:].values
    y_train = pd.read_csv(splitFile + '/y_train.csv').iloc[:, 1].values
    y_test = pd.read_csv(splitFile + '/y_test.csv').iloc[:, 1].values

    estimator = LogisticRegression(class_weight='balanced', penalty='l1', solver='liblinear')
    estimator.fit(X_train, y_train)

    y_pred_prob = estimator.predict_proba(X_test)
    y_pred = estimator.predict(X_test)

    '''训练集同样当成测试集输入并且进行测试'''
    train_pred_prob = estimator.predict_proba(X_train)
    train_pred = estimator.predict(X_train)

    pd.DataFrame(train_pred_prob).to_csv(resultPath + '/train_pred_proba.csv')
    pd.DataFrame(train_pred).to_csv(resultPath + '/train_pred.csv')
    df1 = pd.read_csv(splitFile + '/y_train.csv')
    df2 = pd.read_csv(resultPath + '/train_pred.csv', index_col='Unnamed: 0')
    df3 = pd.read_csv(resultPath + '/train_pred_proba.csv', index_col='Unnamed: 0')
    df4 = pd.concat(objs=[df1, df2, df3], axis=1)
    if classifer == 2:
        df4.columns = ['SubjID', 'Group', 'pred', 'train_pred_prob1', 'train_pred_prob2']
    if classifer == 3:
        df4.columns = ['SubjID', 'Group', 'pred', 'train_pred_prob1', 'train_pred_prob2', 'train_pred_prob3']
    df4.to_csv(resultPath + '/train_result_compare.csv')

    """
            将预测结果、预测结果概率存入/result/logisticRegression/result_compare.csv文件
    """
    pd.DataFrame(y_pred_prob).to_csv(resultPath + '/pred_proba.csv')
    pd.DataFrame(y_pred).to_csv(resultPath + '/pred.csv')
    df1 = pd.read_csv(splitFile + '/y_test.csv')
    df2 = pd.read_csv(resultPath + '/pred.csv', index_col='Unnamed: 0')
    df3 = pd.read_csv(resultPath + '/pred_proba.csv', index_col='Unnamed: 0')
    df4 = pd.concat(objs=[df1, df2, df3], axis=1)
    if classifer == 2:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2']
    if classifer == 3:
        df4.columns = ['SubjID', 'Group', 'pred', 'pred_prob1', 'pred_prob2', 'pred_prob3']
    df4.to_csv(resultPath + '/result_compare.csv')

    """
            权重保存
    """
    weights = estimator.coef_.tolist()[0]
    with open(splitFile + '/X_train.csv', 'r') as f:
        reader = csv.reader(f)
        feature = next(reader)
        feature = feature[2:]
    # print('feature:', feature)
    # print('weights:', weights)
    dataframe = pd.DataFrame({'feature': feature, 'weight': weights})
    # print('path:', resultPath + '/Logitic_weights.csv')
    dataframe.to_csv(resultPath + '/Logitic_weights.csv', index=False, sep=',')

    """
            模型得分
    """
    '''打开文件'''
    proba_result = open(resultPath + '/model_score.txt', mode='w')

    '''将模型得出的权重系数写入到文件中'''
    # proba_result.write('逻辑回归得到的权重系数：' + estimator.)
    '''将交叉验证结果写入文件'''
    scores = cross_val_score(estimator, X_train, y_train, cv=5, scoring='accuracy')
    proba_result.write(str('训练集交叉验证结果：\n'))
    proba_result.writelines(str(scores))
    '''将模型测试评分结果写入文件'''
    score = estimator.score(X_test, y_test)  # 多分类单看一个score不恰当，应该看单独的
    proba_result.write('测试集：\n')
    proba_result.write(str('\n模型测试评分结果：'))
    proba_result.writelines(str(score))

    y_pred = estimator.predict(X_test)
    '''将分类报告写入文件'''
    clf_rep = classification_report(y_test, y_pred, digits=6)
    proba_result.write(str('\n分类报告：\n'))
    proba_result.write(clf_rep)
    '''将混淆矩阵写入文件'''
    cfu_mx = confusion_matrix(y_test, y_pred)
    proba_result.write(str('混淆矩阵：\n'))
    proba_result.writelines(str(cfu_mx))
    '''关闭文件'''

    '''训练集评分保存至文件'''
    score = estimator.score(X_train, y_train)  # 多分类单看一个score不恰当，应该看单独的
    proba_result.write(str('\n测试集模型测试评分结果：'))
    proba_result.writelines(str(score))

    y_pred = estimator.predict(X_train)
    '''将分类报告写入文件'''
    clf_rep = classification_report(y_train, y_pred, digits=6)
    proba_result.write(str('\n分类报告：\n'))
    proba_result.write(clf_rep)
    '''将混淆矩阵写入文件'''
    cfu_mx = confusion_matrix(y_train, y_pred)
    proba_result.write(str('混淆矩阵：\n'))
    proba_result.writelines(str(cfu_mx))
    '''关闭文件'''
    proba_result.close()

    if classifer == 3:
        # ROCX.three_auc_report(1, classifer, y_test, y_pred_prob, resultPath+'/ROC', picName='logisticRegression.png', tp=tp)
        joblib.dump(estimator, resultPath + '/model.pkl')
    # else:
    #     ROCX.auc_report(1, y_test, y_pred_prob[:,1], resultPath+'/ROC', picName='logisticRegression.png')
    #     joblib.dump(estimator, resultPath + '/model.pkl')
    #

    """
    PCA
    """
    # if os.path.exists(resultPath+'/PCA') == False:
    #     os.makedirs(resultPath+'/PCA')
    #
    # result_file = open(resultPath + '/PCA' + '/result.txt', mode='w')
    # n_s = np.linspace(0.6, 0.8, num=5)
    #
    # accuracy = []
    # for n in n_s:
    #     tmp = n_components_analysis(n, X_train, y_train, X_test,
    #                                 y_test, result_file, resultPath + '/PCA',
    #                                 classifier=classifer,splitFile=splitFile)  # 使用原始数据直接进行主成分分析，8-2分
    #
    #     accuracy.append(tmp)
    # # acc = 'total accurcy:\n', accuracy
    # # result_file.writelines(acc)
    # result_file.close()
    return


# coding: utf-8

# In[1]:

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

# In[16]:

import pandas as pd
path_to_netatmo_coords_df = (r'X:\hiwi\ElHachem\Prof_Bardossy\Extremes'
                             r'\NetAtmo_BW'
                             r'\rain_bw_1hour'
                             r'\netatmo_bw_1hour_coords.csv')
df_c = pd.read_csv(path_to_netatmo_coords_df, sep=';', index_col=0)

plt.ioff()
fig = plt.figure(figsize=(15, 15), dpi=200)

ax = fig.add_subplot(111)

path_to_shpfile = (
    r"X:\exchange\ElHachem\Netatmo\Landesgrenze_ETRS89\Landesgrenze_10000_ETRS89_lon_lat.shp")
shp_de = shapefile.Reader(path_to_shpfile)
# read and plot shapefile (BW or Germany) should be lon lat
for shape_ in shp_de.shapeRecords():
    lon = [i[0] for i in shape_.shape.points[:][::-1]]
    lat = [i[1] for i in shape_.shape.points[:][::-1]]

    ax.scatter(lon, lat, marker='.', c='lightgrey',
               alpha=0.25, s=1)

df_coords = pd.read_csv(
    r"X:\hiwi\ElHachem\Prof_Bardossy\Extremes\DWD_coords_BW.csv", sep=',', index_col=0)

ax.scatter(df_c[' lon'], df_c[' lat'], c='b', alpha=0.85,
           marker='o', s=25, label='Netatmo stations')
ax.scatter(df_coords['lon'], df_coords['lat'], c='r', alpha=0.85,
           s=25,  marker='d', label='DWD stations')
plt.axis('equal')
plt.grid(alpha=.05)
plt.legend(loc=0, fontsize=12)
plt.xlabel('Longitude', fontsize=10)
plt.ylabel('Latitude', fontsize=10)
plt.xticks(fontsize=10)
plt.yticks(fontsize=10)
plt.tight_layout()
plt.savefig(r"X:\hiwi\ElHachem\Prof_Bardossy\Extremes\stations.png",
            frameon=True, papertype='a4',
            bbox_inches='tight', pad_inches=.2)

# In[7]:

plt.savefig(r"X:\hiwi\ElHachem\Prof_Bardossy\Extremes\stations.png",
            frameon=True, papertype='a4',
            bbox_inches='tight', pad_inches=.2)


from src.datamodules.common.generic_datamodule import GenericDatamodule
from src.utils.hydra import instantiate_delayed
import os
from torchvision.utils import save_image
import torch
from src.utils.audio import save_mp3_to_tensor

class AudioDataModule(GenericDatamodule):
    def __init__(
        self,
        batch_size=64,
        num_workers: int = 0,
        pin_memory: bool = False,
        train_ratio=0.85,
        val_ratio=0.15,
        sr=44100,
        interval_length=20,
        extensions=[],
        loader_type="torch",
        transform=None,
        preparers=None,
        train_datasets=None,
        test_datasets=None,
        images_preparers=None,
        images_dir="",
        torch_preparers=None,
        torch_dir="",
        audio_dir="",
        device="gpu",
    ):
        super().__init__(
            batch_size,
            num_workers,
            pin_memory,
            train_ratio,
            val_ratio,
            train_datasets,
            test_datasets,
        )
        self.preparers = preparers if preparers is not None else []
        self.images_preparers = images_preparers if images_preparers is not None else []
        self.images_dir = images_dir
        self.torch_preparers = torch_preparers if torch_preparers is not None else []
        self.torch_dir = torch_dir
        self.audio_dir = audio_dir
        self.sr = sr
        self.loader_type = loader_type
        self.device = "cuda" if device == "gpu" else device

    def prepare_data(self):
        print("Audio data module prepare start...")
        for preparer in self.preparers.values():
            preparer.prepare()
        print("Audio data module prepare finished.")

    def create_audio_tensors(self):
        if not os.path.exists(self.torch_dir):
            os.mkdir(self.torch_dir)

        files_num = 0
        for (
            root,
            dirs,
            files,
        ) in os.walk(self.audio_dir):
            destination_dir = root.replace(self.audio_dir, self.torch_dir)
            if not os.path.exists(destination_dir):
                os.makedirs(destination_dir)

            for file in files:
                if file.endswith(".mp3") or file.endswith(".wav"):
                    source_path = os.path.join(root, file)
                    destination_path = os.path.join(destination_dir, file).replace(
                        ".mp3", ".pt"
                    )
                    save_mp3_to_tensor(
                        source_path,
                        destination_path,
                        self.sr,
                        self.loader_type,
                        self.device,
                    )

    def create_spectrograms(self):
        if not os.path.exists(self.images_dir):
            os.mkdir(self.images_dir)

        datasets = [
            *self.train_datasets_configs,
            *self.test_datasets_configs,
        ]
        for image_preparer in self.images_preparers.values():
            dataset_images_dir = image_preparer.images_dir

            if not os.path.exists(dataset_images_dir):
                os.makedirs(dataset_images_dir, exist_ok=True)

            dataset_name = image_preparer.dataset_name
            dataset_config = next(filter(lambda d: d["name"] == dataset_name, datasets))
            dataset = instantiate_delayed(dataset_config)
            idx_to_class = {v: k for k, v in dataset.class_to_idx.items()}
            for index, entry in enumerate(dataset):
                if entry is None or entry[0] is None:
                    continue

                sample, label = entry
                image = sample[0]
                key_dir = idx_to_class[label]
                filename = os.path.basename(dataset.samples[index][0][:-4]) + ".png"
                full_dir = os.path.join(dataset_images_dir, key_dir)
                full_path = os.path.join(full_dir, filename)

                if not os.path.exists(full_dir):
                    os.mkdir(full_dir)

                save_image(image, full_path)


"""
Hi, here's your problem today. This problem was recently asked by Microsoft:

Given the root of a binary tree, print its level-order traversal. For example:

  1
 / \
2   3
   / \
  4   5

The following tree should output 1, 2, 3, 4, 5.

class Node:
  def __init__(self, val, left=None, right=None):
    self.val = val
    self.left = left
    self.right = right

def print_level_order(root):
  # Fill this in.

root = Node(1, Node(2), Node(3, Node(4), Node(5)))
print_level_order(root)
# 1 2 3 4 5
"""

from collections import deque

class Node:
    def __init__(self, val, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def print_level_order(root):
    queue = deque([root])
    order = []
    while len(queue) > 0:
        node = queue.pop()
        order.append(node.val)
        if node.left:
            queue.appendleft(node.left)
        if node.right:
            queue.appendleft(node.right)

    print(" ".join([str(x) for x in order]))

root = Node(1, Node(2), Node(3, Node(4), Node(5)))
print_level_order(root)
# 1 2 3 4 5


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from dao.character_dao import CharacterDao
from dao.weapon_dao import WeaponDao

class Genshin:

    character_dao = None
    weapon_dao = None
    __instance = None

    @staticmethod
    def get_instance():
        """Static method access"""
        if Genshin.__instance is None:
            Genshin.__instance = Genshin()
        return Genshin.__instance

    def __init__(self, connection_url="sqlite:///genshin-data.db"):
        engine = create_engine(connection_url, echo=True)
        Session = sessionmaker(bind=engine)
        self.__db_session = Session()

    def get_character_dao(self):
        """Get character dao."""
        if self.character_dao is None:
            self.character_dao = CharacterDao(session=self.__db_session)
        return self.character_dao

    def get_weapon_dao(self):
        """Get weapon dao."""
        if self.weapon_dao is None:
            self.weapon_dao = WeaponDao(session=self.__db_session)
        return self.weapon_dao

    def close_db(self):
        """Close all the database"""
        self.__db_session.close()


import decimal
import re

import lxml.html

class Base(object):
    fetched = False

    def __init__(self, mal_id, mal):
        self.mal_id = mal_id
        self.mal = mal

    def _get_url(self):
        return self.base_url % self.mal_id

    def fetch(self):
        if not self.fetched:
            return self.mal._fetch(self)

    def parse(self, html):
        # Ignoring errors here because MAL allows users to use their own encodings
        # Without testing, probably allows the users to store pictures from their latest vacation as a review
        # Anyways, anything we need is (hopefully) in utf-8
        tree = lxml.html.fromstring(html.decode('utf-8', errors='ignore'))

        schema = tree.xpath('//div[@id="contentWrapper"]')[0]

        self.title = schema.xpath('.//span[@itemprop="name"]/text()')[0].strip()
        synopsis = schema.xpath('.//span[@itemprop="description"]/text()')
        if synopsis:
            self.synopsis = synopsis[0].strip()
        else:
            self.synopsis = ''
        self.cover = schema.xpath('.//img[@itemprop="image"]')[0]
        if 'data-src' in self.cover.attrib:
            self.cover = self.cover.attrib['data-src']
        else:
            self.cover = self.cover.attrib['src']

        self.info = info = {}
        self.alternative_titles = alternative_titles = {}
        self.statistics = statistics = {}
        self.related = related = {}
        self.reviews = []

        def duration2int(x):
            runtime = 0
            hours = re.findall(r'(\d+) hr', x)
            minutes = re.findall(r'(\d+) min', x)
            if hours:
                try:
                    runtime += int(hours[0])*60
                except ValueError:
                    pass

            if minutes:
                try:
                    runtime += int(minutes[0])
                except ValueError:
                    pass

            return runtime

        def num2int(x):
            try:
                return int(x.replace(',', ''))
            except ValueError:
                return None

        def num2dec(x):
            try:
                return decimal.Decimal(x)
            except decimal.InvalidOperation:
                return None

        strip2int = lambda x: x != 'N/A' and int(x.strip('#')) or None

        loop_elements = [
            ('Alternative Titles', True, [], alternative_titles, {}),
            ('Information', False, ['Producers', 'Genres', 'Authors', 'Serialization', 'Licensors', 'Studios'], info, {'Episodes': num2int, 'Duration': duration2int, 'Volumes': num2int, 'Chapters': num2int}),
            ('Statistics', False, [], statistics, {'Favorites': num2int, 'Members': num2int, 'Popularity': strip2int, 'Ranked': strip2int}),
        ]

        for block, splitlist, linklist, save_target, postprocess in loop_elements:
            for el in tree.xpath('//h2[text()="%s"]/following-sibling::*' % block):
                if el.tag != 'div' or not el.xpath('span') or ':' not in el.xpath('span/text()')[0]:
                    break

                text = ''.join(el.xpath('text()')).strip()
                info_type = el.xpath('span/text()')[0].strip(':')
                if info_type in linklist:
                    save_target[info_type] = []

                    if 'None found' not in text:
                        for a in el.xpath('a'):
                            save_target[info_type].append({
                                'id': int(re.findall('\d+', a.attrib['href'])[-1]),
                                'name': a.text
                            })
                elif info_type == 'Type':
                    save_target[info_type] = str(sorted(el.xpath('.//text()'), key=lambda x:len(x))[-1])
                elif info_type == 'Premiered':
                    premiered = el.xpath('./a/text()')[0].split(' ')
                    if premiered:
                        year = premiered[1]
                        try:
                            year = int(premiered[1])
                        except ValueError:
                            pass

                        save_target[info_type] = {
                            'season': premiered[0],
                            'year': year,
                        }
                else:
                    save_target[info_type] = text.strip()
                    if splitlist:
                        save_target[info_type] = map(lambda x:x.strip(), save_target[info_type].split(','))
                    elif info_type in postprocess:
                        save_target[info_type] = postprocess[info_type](save_target[info_type])

        score_box = tree.xpath('//div[./span[text()="Score:"]]/span')

        votes = tree.xpath('//span[@itemprop="ratingCount"]/text()')
        if votes:
            statistics['Votes'] = votes[0]
        else:
            statistics['Votes'] = score_box[2].xpath('./text()')[0]

        if 'Votes' in statistics:
            statistics['Votes'] = int(statistics['Votes'].replace(',', ''))

        score = tree.xpath('//span[@itemprop="ratingValue"]/text()')
        if score:
            statistics['Score'] = score[0]
        else:
            statistics['Score'] = score_box[1].xpath('./text()')[0]

        if 'Score' in statistics:
            statistics['Score'] = num2dec(statistics['Score'])

        found_h2 = False
        tags = iter(filter(lambda x:x, map(lambda x:x.strip(': ,'), tree.xpath('//h2[starts-with(text(), "Related ")]/../text()'))))
        current_tag = None

        for el in tree.xpath('//table[@class="anime_detail_related_anime"]/tr'):
            name, relationships = el.xpath('./td')
            name = name.text.strip(':')
            related[name] = []
            for r in relationships.xpath('./a'):
                url = r.attrib['href'].split('/')
                tag_type = url[1]
                tag_id = url[2]
                related[name].append({'type': tag_type, 'id': int(tag_id)})

        self.mal._handle_related(self)

        for review in tree.xpath('//h2[contains(text(), "Reviews")]/following-sibling::*//div[contains(@class, "borderLight")]'):
            rating = int(review.xpath('.//a[text()="Overall Rating"]/../text()')[0].strip(': '))
            review = ''.join(review.xpath('following-sibling::div/text()')).strip() + '\n'.join(review.xpath('following-sibling::div/span/text()')).strip()
            review = review.replace('\n\n', '\n')

            self.reviews.append({
                'rating': rating,
                'review': review
            })

        self.fetched = True


from django import forms
from .models import Post
from django.core.validators import FileExtensionValidator

class PostForm(forms.ModelForm):
    thumbnail = forms.FileField(required=False, widget=forms.ClearableFileInput(attrs={'class': 'input'}))
    video = forms.FileField(required=False, validators=[FileExtensionValidator(allowed_extensions=['MOV','avi','mp4','webm','mkv'])], widget=forms.ClearableFileInput(attrs={'class': 'input'}))

    class Meta:
        model = Post
        fields = ['title', 'intro', 'body', 'contribution_amount', 'thumbnail', 'video']
        widgets = {
            'title': forms.TextInput(attrs={'class': 'input', 'placeholder': 'The title of your post', 'required': True}),
            'intro': forms.TextInput(attrs={'class': 'input', 'placeholder': 'The intro of your post', 'required': True}),
            'body': forms.Textarea(attrs={'class': 'textarea', 'placeholder': 'Include details about your cause'}),
            'contribution_amount': forms.TextInput(attrs={'class': 'input', 'placeholder': 'Contribution amount'}),
        }
        validators = {
            'video': FileExtensionValidator(allowed_extensions=['MOV','avi','mp4','webm','mkv'])
        }


import os
import time

from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit, disconnect

from collections import deque

app = Flask(__name__)

# socket-io configure
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
socketio = SocketIO(app)

# in-memory data
USERS = {}
CHANNELS = {"general": deque([], maxlen=100)}

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

@socketio.on('connect')
def connection():
    print("new user connected")

@socketio.on('userdata')
def user_data(data):
    if 'username' in data:
        USERS[data['username']] = request.sid

@socketio.on('new channel')
def new_channel(data):
    if data['name'] in CHANNELS:
        return False
    else:
        CHANNELS[data['name']] = deque(maxlen=100)
        emit('new channel', { "name" : data['name']}, broadcast=True)

@socketio.on('new msg')
def new_msg(data):

    if 'channel' in data:
        data['created_at'] = int(time.time())
        CHANNELS[data['channel']].append(data)
        emit('msg', data, broadcast=True)

@socketio.on('get channels')
def get_channels():
    emit('channels', list(CHANNELS.keys()))

@socketio.on('get msgs')
def get_msgs(data):
    if 'name' in data:
        emit('msgs', list(CHANNELS[data['name']]))

if __name__ == "__main__":
    socketio.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 5000)))


import pygame
import config
from CentipedeComponent import CentipedeComponent
from Direction import Direction
from threading import Timer
from PendingMovement import PendingMovement

def backwards(dir):
    if dir == Direction.up: return Direction.down
    elif dir == Direction.right: return Direction.left
    elif dir == Direction.down: return Direction.up
    elif dir == Direction.left: return Direction.left

class Centipede():

    def __init__(self, controller, initAmount = 10, initX = 20 + (5 * 10), initY = 20, initMove = 0, newG = pygame.sprite.Group()):

        self.centipede_group = newG
        self.moveAmount = initMove
        self.facingRight = True
        self.speed = 2
        self.controller = None

        self.pendingMovements = []

        print("initAmount: " + str(initAmount))
        self.controller = controller
        self.headX = initX
        self.headY = initY
        if initAmount > 0:
            for x in range(0, initAmount):
                self.centipede_group.add(CentipedeComponent(self))
            if initX > 0:
           	    self.initPos()

    def update(self, screen, background, collideWith, bullets):
        self.updatePos(collideWith, bullets)
        self.centipede_group.draw(screen)

    def divide(self, at):
        try:
            origLen = len(self.centipede_group)
            if origLen == 0:
                return
            atX = self.centipede_group.sprites()[at].rect.x
            atY = self.centipede_group.sprites()[at].rect.y
            rem = 0
            newG = pygame.sprite.Group()
            while len(self.centipede_group) >= at and len(self.centipede_group) >= 1:
                obj = self.centipede_group.sprites()[len(self.centipede_group.sprites()) - 1]
                rem += 1
                if rem != 0:
                    newG.add(obj)
                self.centipede_group.remove(obj)
            if len(self.centipede_group) <= 1:
                self.controller.centipedes.remove(self)
            if len(newG) <= 2:
                return
            temp = Centipede(self.controller, 0, -1, -1, self.moveAmount, newG)
            for x in temp.centipede_group:
                x.parent = temp
                x.direction = backwards(x.direction)
                x.updateDirection()
            self.controller.centipedes.append(temp)
        except Exception as e:
            print(e.message)

    def initPos(self):
        count = 0
        for x in self.centipede_group:
            x.rect.x = self.headX + (20 * count)
            x.rect.y = self.headY + 20
            count += 1

    def updatePos(self, collideWith, bullets):
        count = 0
        direction = None
        newX = 0
        newY = 0
        trail = self.centipede_group.sprites()[len(self.centipede_group.sprites()) - 1]
        trail.move(self.headX, self.headY, self.speed, collideWith, bullets, count, self.centipede_group)
        direction = trail.direction
        newX = trail.rect.x
        newY = trail.rect.y
        self.pendingMovements.append(PendingMovement((7 / 2) * self.speed, self.centipede_group, newX, newY, collideWith, bullets, trail.direction))
        for m in self.pendingMovements:
            if m.tick():
                self.pendingMovements.remove(m)


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

"""
.. _log

Basic stdout log classes: Error, Verbose and Warning.

**Content**
"""

# *credits*:      `gjacopo <jacopo.grazzini@ec.europa.eu>`_
# *since*:        Fri May  8 15:21:31 2020

#%% Settings

import os, sys, warnings
import inspect
import functools
import six

DEFVERBOSE          = False # True

REDUCE_ANSWER       = False # ! used for testing purpose: do not change !
EXCLUSIVE_ARGUMENTS = False # ! used for settings: do not change !

#%% Core functions/classes

#==============================================================================
# Class Warning
#==============================================================================

class Warnings(Warning):
    """Dummy class for warnings in this package.

        >>> Warnings(warnmsg, expr=None)

    Arguments
    ---------
    warnmsg : str
        warning message to display.

    Keyword arguments
    -----------------
    expr : str
        input expression in which the warning occurs; default: :data:`expr` is
        :data:`None`.

    Example
    -------

        >>> Warnings('This is a very interesting warning');
            Warnings: ! This is a very interesting warning !
    """
    def __init__(self, msg='', **kwargs):
        self.msg = msg
        expr = kwargs.pop('expr',None)
        if expr is not None:    self.expr = expr
        else:                   self.expr = ''
        # warnings.warn(self.msg)
        print(self)
    def __repr__(self):             return self.msg
    def __str__(self):
        #return repr(self.msg)
        return (
                "! %s%s%s !" %
                (self.msg,
                 ' ' if self.msg and self.expr else '',
                 self.expr
                 )
            )

#==============================================================================
# Class Verbose
#==============================================================================

class Verbose(object):
    """Dummy class for verbose printing mode in this package.

        >>> Verbose(msg, verb=True, expr=None)

    Arguments
    ---------
    msg : str
        verbose message to display.

    Keyword arguments
    -----------------
    verb : bool
        flag set to :data:`True` when the string :literal:`[verbose] -` is added
        in front of each verbose message displayed.
    expr : str
        input expression in which the verbose mode is called; default: :data:`expr` is
        :data:`None`.

    Example
    -------

        >>> Verbose('The more we talk, we less we do...', verb=True);
            [verbose] - The more we talk, we less we do...
    """
    def __init__(self, msg='', **kwargs):
        expr = kwargs.pop('expr','')
        verb = kwargs.pop('verb', DEFVERBOSE)
        self.msg = msg
        if verb is True:
            print('\n! [verbose] - %s !' % self.msg)
        if expr is not None:    self.expr = expr
    #def __repr__(self):
    #    return self.msg
    def __str__(self):
        return repr(self.msg)

#==============================================================================
# Class Error
#==============================================================================

class Error(Exception):
    """Dummy class for exception raising in this package.

        >>> raise Error(msg, type=None, code=None, expr='')

    Arguments
    ---------
    errmsg : str
        message -- explanation of the error.

    Keyword arguments
    -----------------
    type : object
        error type; when :data:`errtype` is left to :data:`None`, the system tries
        to retrieve automatically the error type using :data:`sys.exc_info()`.
    code : (float,int)
        error code; default: :data:`errcode` is :data:`None`.
    expr : str
        input expression in which the error occurred; default: :data:`expr` is
        :data:`None`.

    Example
    -------

        >>> try:
                assert False
            except:
                raise Error('It is False')
            Traceback ...
            ...
            Error: !!! AssertionError: It is False !!!
    """

    def __init__(self, msg='',  **kwargs):
        self.msg = msg
        typ = kwargs.pop('type',None)
        code = kwargs.pop('code',None)
        expr = kwargs.pop('expr','')
        if expr is not None:                self.expr = expr
        else:                               self.expr = ''
        if typ is None:
            try:            typ = sys.exc_info()[0]
            except:         pass
        if inspect.isclass(typ):            self.type = typ.__name__
        elif isinstance(typ, (int,float)):  self.type = str(typ)
        else:                               self.type = typ
        if code is not None:                self.code = str(code)
        else:                               self.code = ''
        # super(Error,self).__init__(self, msg)

    def __str__(self):
        # return repr(self.msg)
        str_ = ("%s%s%s%s%s%s%s" %
                (self.type or '',
                 ' ' if self.type and self.code else '',
                 self.code or '',
                 ': ' if (self.type or self.code) and (self.msg or self.expr) else '',
                 self.msg or '',
                 ' ' if self.msg and self.expr else '',
                 self.expr or '' #[' ' + self.expr if self.expr else '']
                 )
                )
        return ( "%s%s%s" %
                ('' if str_.startswith('!!!') else '!!! ',
                 str_,
                 '' if str_.endswith('!!!') else ' !!!'
                 )
                )

#==============================================================================
# Method deprecated
#==============================================================================

def deprecated(reason, run=True):
    """This is a decorator which can be used to mark functions as deprecated.

        >>> new = deprecated(reason)

    Arguments
    ---------
    reason : str
        optional string explaining the deprecation.

    Keywords arguments
    ------------------
    run : bool
        set to run the function/method/... despite being deprecated; default:
        :data:`False` and the decorated method/function/... is not run.

    Examples
    --------
    The deprecated function can be used to decorate different objects:

        >>> @deprecated("use another function")
        ... def old_function(x, y):
        ...     return x + y
        >>> old_function(1, 2)
            __main__:1: DeprecationWarning: Call to deprecated function old_function (use another function).
            3
        >>> class SomeClass(object):
        ... @deprecated("use another method", run=False)
        ... def old_method(self, x, y):
        ...     return x + y
        >>> SomeClass().old_method(1, 2)
            __main__:1: DeprecationWarning: Call to deprecated function old_method (use another method).
        >>> @deprecated("use another class")
        ... class OldClass(object):
        ...     pass
        >>> OldClass()
            __main__:1: DeprecationWarning: Call to deprecated class OldClass (use another class).
            <__main__.OldClass at 0x311e410f0>

    Note
    ----
    It will result in a warning being emitted when the function is used and when
    a :data:`reason` is passed.
    """
    # see https://stackoverflow.com/questions/2536307/decorators-in-the-python-standard-lib-deprecated-specifically
    if isinstance(reason, six.string_types): # happyType.isstring(reason):
        def decorator(func1):
            if inspect.isclass(func1):
                fmt1 = "Call to deprecated class {name} ({reason})."
            else:
                fmt1 = "Call to deprecated function {name} ({reason})."
            @functools.wraps(func1)
            def new_func1(*args, **kwargs):
                warnings.simplefilter('always', DeprecationWarning)
                warnings.warn(
                    fmt1.format(name=func1.__name__, reason=reason),
                    category=DeprecationWarning,
                    stacklevel=2
                )
                warnings.simplefilter('default', DeprecationWarning)
                if run is True:
                    return func1(*args, **kwargs)
            return new_func1
        return decorator
    elif inspect.isclass(reason) or inspect.isfunction(reason):
        func2 = reason
        if inspect.isclass(func2):
            fmt2 = "Call to deprecated class {name}."
        else:
            fmt2 = "Call to deprecated function {name}."
        @functools.wraps(func2)
        def new_func2(*args, **kwargs):
            warnings.simplefilter('always', DeprecationWarning)
            warnings.warn(
                fmt2.format(name=func2.__name__),
                category=DeprecationWarning,
                stacklevel=2
            )
            warnings.simplefilter('default', DeprecationWarning)
            if run is True:
                return func2(*args, **kwargs)
        return new_func2
    else:
        raise Error('wrong type for input reason - %s not supported' % repr(type(reason)))


# -*- coding: utf-8 -*-
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from ncar.items import NcarItem

class GetsomeSpider(CrawlSpider):
    global li
    li = list()
    name = 'getsome'
    allowed_domains = ['ncar.cc']
    start_urls = ['http://bbs.ncar.cc/forum.php?mod=forumdisplay&fid=129']

    rules = (
        Rule(LinkExtractor(restrict_xpaths="//div[@class='xbs xbs_4 block move-span']//li/a"), callback='parse_item', follow=True),
    )

    def parse_start_url(self,response):
        for sel in response.xpath("//div[@class='xbs xbs_4 block move-span']//li/a"):
            item = NcarItem()
            item['name'] = sel.xpath("text()").extract()
            item['url'] = sel.xpath("@href").extract()
            item['links'] = dict()
            li.append(item)
#        print li

    def parse_item(self, response):
        for l in li:
#            print response.url.find(l['url'][0]),l['url'][0],response.url,type(l['url']),type(response.url.find(l['url'][0]))
            #if(l['url']==response.url):
            if(response.url.find(l['url'][0])!=-1):
                for sel in response.xpath("//td[@class='t_f']/div[3]/a"):
                    l['links'][sel.xpath("text()").extract()[0]]=sel.xpath("@href").extract()[0]
                yield l


from django.contrib.auth.models import User
from django.http import HttpResponse
from django.shortcuts import redirect, render

from .HospitalDBConnect import *

def home(request):
    ''' the home for hosptial admins '''

    appt_count = view_appt_count()
    doc_count = view_doc_count()
    room_count = view_room_count()

    appt_count = appt_count[0]['count(PatientID)']
    doc_count = doc_count[0]['count(DocID)']
    room_count = room_count[0]['count(RoomNumber)']

    context = {
        "appt_count": appt_count,
        'doc_count': doc_count,
        'room_count': room_count
    }
    # handle get only
    return HttpResponse(render(request, 'Hosptial/home.html', context))

def search(request):
    ''' search for firstname, lastname, email '''

    # handle post
    if request.method == 'POST':

        # get data
        first_name = request.POST.get("firstName", "")
        last_name = request.POST.get("lastName", "")

        matching_patients = view_person_search_count(first_name, last_name)
        context = {'patients': matching_patients}

        # reorganize data and pass as context
        return HttpResponse(render(request, 'Hosptial/search.html', context))

    else:
        return HttpResponse(render(request, 'Hosptial/search.html'))

def appointments(request):
    ''' handles appointments '''

    # get all appointments
    all_appointments = view_Appointments()
    context = {'appts': all_appointments}

    return HttpResponse(render(request, 'Hosptial/appointments.html', context))

def profile(request, patient_id):
    ''' shows patient profile '''

    # get the data for that id
    patient_profile = view_history(patient_id)
    context = {"profile": patient_profile}

    return HttpResponse(render(request, 'Hosptial/profile.html', context))

def treatment(request, patient_id):
    ''' Handles treatment stuff '''

    # handle post, redirect to patient profile
    if request.method == 'POST':

        doc_id = request.POST.get("doctorName", "")
        aliment = request.POST.get("aliment", "")
        pre_date = request.POST.get("pdate", "")
        expected = request.POST.get("expected", "")
        warnings = request.POST.get("warnings", "")

        InsertTreatment(aliment, str(pre_date), str(expected), str(warnings))
        return redirect('/hosptial/profile/' + patient_id)

    else:
        # get profile data for side display
        patient_profile = view_history(patient_id)

        # get all doctors
        all_doctors = view_Doctors()

        # set context
        context = {"profile": patient_profile[-1], "doctors": all_doctors}

        return HttpResponse(
            render(request, 'Hosptial/create_treatment.html', context))

def update_appointment(request, patient_id, doc_id):
    ''' handles appointments update '''

    # handle get
    if request.method == 'POST':
        prefered_doctor = request.POST.get("preferedDoctor", "")
        prefered_date = request.POST.get("preferedDate", "")
        reason = request.POST.get("reason", "")

        Update_Appointment(
            int(patient_id), int(doc_id.strip('/')), int(prefered_doctor),
            str(prefered_date), reason)
        return redirect('/hosptial/appointments/')

    else:

        # get old apt data and pass it as context

        # get all doctors
        all_doctors = view_Doctors()
        context = {
            'doctors': all_doctors,
            'PatientID': patient_id,
            'DocID': doc_id
        }
        return HttpResponse(
            render(request, 'Hosptial/update_appt.html', context))

def patients(request):
    ''' shows all patients and leads to profile '''
    # get data
    all_patients = view_Patients()
    context = {'patients': all_patients}

    return HttpResponse(render(request, 'Hosptial/patients.html', context))

def doctors(request):
    ''' shows all doctors and their info '''

    # get all the data
    all_doctors = view_Doctors()
    context = {'doctors': all_doctors}
    return HttpResponse(render(request, 'Hosptial/doctors.html', context))

def nurses(request):
    ''' shows all doctors and their info '''

    # get all the data
    all_nurses = view_Nurses()
    context = {'nurses': all_nurses}
    return HttpResponse(render(request, 'Hosptial/nurses.html', context))

def employees(request):
    ''' shows all doctors and their info '''

    # get all the data
    all_employees = view_Employees()
    context = {'employees': all_employees}
    return HttpResponse(render(request, 'Hosptial/employees.html', context))

def rooms(request):
    ''' shows info about rooms: handles get only '''

    # get all data and set context
    all_rooms = view_Rooms()
    context = {'rooms': all_rooms}

    return HttpResponse(render(request, 'Hosptial/rooms.html', context))

def departments(request):
    ''' shows dep info: handles get only '''
    # get data
    all_departments = view_Departments()

    context = {'departments': all_departments}

    return HttpResponse(render(request, 'Hosptial/departments.html', context))

def bills(request):
    ''' just shows all the bills '''

    # get all the data
    all_bills = view_Bills()
    context = {'bills': all_bills}
    # print(context)

    return HttpResponse(render(request, 'Hosptial/bills.html', context))

def bills_more(request, billNumber):
    ''' shows the user profile '''
    all_bills = view_Bill_more(billNumber)
    # print (all_bills)
    context = {'bill': all_bills[0]}
    print(context)
    return HttpResponse(render(request, 'Hosptial/bills_more.html', context))

def create_bill(request, patient_id):
    ''' handles the view and creation of bills '''

    if request.method == 'POST':
        due_date = request.POST.get("dueDate", "")
        re_date = request.POST.get("reDate", "")
        amount = request.POST.get("amount", "")
        description = request.POST.get("description", "")

        InsertBill(
            int(patient_id), str(re_date), int(amount), str(description),
            str(due_date))

        return redirect('/hosptial/bills')

    context = {'PatientID': patient_id}
    return HttpResponse(render(request, 'Hosptial/create_bill.html', context))

def delete_bill(request, bill_num):
    ''' deletes a bill given the bill_id '''

    # call the delete function
    DeleteBill(bill_num)

    return redirect('/hosptial/bills')


from __future__ import print_function
import argparse
import os
import random
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torch.utils.data
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from models import *
from my_utils import *
import matplotlib.pyplot as plt

def train_and_validation(opt,dataloader):

    #=== parse opt =============
    device = torch.device("cuda:0" if opt.cuda else "cpu")
    ngpu = int(opt.ngpu)

    nz = int(opt.nz)
    ngf = int(opt.ngf)
    ndf = int(opt.ndf)
    nc = 3 #num channel
    imagesize = opt.imageSize
    #load checkpoint by inline code
    #opt.netG = './saves/lfw/netG_epoch_12.pth'
    #opt.netD = ''
    #============================

    #====== model and optimizer =========
    netG = Generator(ngpu,nz = nz, ngf = ngf, nc = nc , imagesize = imagesize).to(device)
    netG.apply(weights_init)
    if opt.netG != '':
        netG.load_state_dict(torch.load(opt.netG))
    print(netG)

    netD = Discriminator(ngpu,nc = nc , ndf= ndf,imagesize = imagesize).to(device)
    netD.apply(weights_init)
    if opt.netD != '':
        netD.load_state_dict(torch.load(opt.netD))
    print(netD)

    criterion = nn.BCELoss()
    optimizer_netD = torch.optim.Adam(netD.parameters(),lr = opt.lr,betas = (opt.beta1,0.9999))
    optimizer_netG = torch.optim.Adam(netG.parameters(),lr = opt.lr, betas = (opt.beta1,0.9999))

    fixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device)

    for epo in range(opt.nepo):
        for itr, (data,label) in enumerate(dataloader):
            #=================================
            # (1) update Discriminator :
            #  maximize log D(x) + log (1 - D(G(z))
            #  = minimize -1* (log D(x) + log( 1- D(G(z)))
            #=================================
            optimizer_netD.zero_grad()
            real_data_cpu = data
            real_data = data.to(device)

            label = torch.full((opt.batchSize,), 1, device=device)
            output = netD(real_data)
            loss_Real = criterion(output,label)
            loss_Real.backward()

            label = label.fill_(0)
            noise = torch.randn((opt.batchSize,nz,1,1),device = device)
            fake = netG(noise)
            output_fake = netD(fake.detach())
            loss_Fake = criterion(output_fake,label)
            loss_Fake.backward()
            optimizer_netD.step()

            loss_D = (loss_Real + loss_Fake).item()
            # =================================
            # (1) update Generator :
            #  maximize log (D(G(z))
            #  = minimize -1* (log( D(G(z)))
            # =================================
            optimizer_netG.zero_grad()
            noise = torch.randn((opt.batchSize,nz,1,1,), device = device)
            fake = netG(noise)
            output_fake = netD(fake)
            label = label.fill_(1)

            loss1 = -1 * criterion(output_fake, torch.full((opt.batchSize,), 0, device=device))
            loss2 = criterion(output_fake, label.fill_(1))
            if(torch.abs(loss1) < torch.abs(loss2)):
                loss = loss2
                print(round(loss1.item(),4), round(loss2.item(),4))
            else:
                loss = loss1
                print(round(loss1.item(), 4), round(loss2.item(), 4))
            #loss = criterion(output_fake,label.fill_(0))
            loss.backward()
            optimizer_netG.step()

            loss_G = loss.item()
            print("itr:",itr ,"loss_D : ", round(loss_D,4), "loss_G : ", round(loss_G,4) , "D(fake) :", round(torch.mean(output_fake).detach().item(),4))

            if itr% 50  == 0:
                ''' for display
                imshow_with_tensor(vutils.make_grid(fake.detach()).cpu())
                if not os.path.isdir(os.path.join(opt.outf)):
                    os.makedirs(os.path.join(opt.outf), exist_ok=True)
                vutils.save_image(fake.detach(),'%s/fake_samples_epoch_%03d.png' % (opt.outf, epo))
                '''
                if not os.path.isdir(os.path.join(opt.outf)):
                    os.makedirs(os.path.join(opt.outf), exist_ok=True)
                vutils.save_image(real_data_cpu, '%s/real_samples.png' % opt.outf, normalize=True)

                fake = netG(fixed_noise)
                if not os.path.isdir(os.path.join(opt.outf)):
                    os.makedirs(os.path.join(opt.outf), exist_ok=True)
                vutils.save_image(fake.detach(), '%s/fake_samples_epoch_%03d.png' % (opt.outf, epo),
                                  normalize=True)

                # do checkpointing
        torch.save(netG.state_dict(), '%s/netG_epoch_%d.pth' % (opt.outf, epo))
        torch.save(netD.state_dict(), '%s/netD_epoch_%d.pth' % (opt.outf, epo))


""" Python3: Save single page of pdf as a new pdf file """
import PyPDF2
# Initialize input pdf file
in_pdf = open(r'/path/to/input.pdf', 'rb')
pdf_reader = PyPDF2.PdfFileReader(in_pdf)   # Reader element
pdf_writer = PyPDF2.PdfFileWriter()         # Writer element
pdf_writer.addPage(pdf_reader.getPage(n))   # Choose n-th page number
# Initialize output pdf file
out_pdf = open(r'/path/to/output.pdf', 'wb')
pdf_writer.write(out_pdf)
# Close I/O pdf files
out_pdf.close()
in_pdf.close()


import xml.etree.ElementTree as ET
import json

# Parse the KML file
tree = ET.parse('tests/places.kml')

# Get the root element
root = tree.getroot()

# Find all Placemark elements
placemarks = root.findall('.//{http://www.opengis.net/kml/2.2}Placemark')

# Loop through the placemarks and print the name and coordinates
data = []

for placemark in placemarks:
    name = placemark.find(
        './/{http://www.opengis.net/kml/2.2}name').text.strip()
    coordinates = placemark.find(
        './/{http://www.opengis.net/kml/2.2}coordinates').text.strip()

    temp = {"name": name, "coordinates": [float(x) for x in coordinates.split(",")[:2]]}
    data.append(temp)

with open("tests/print.json","w") as f: json.dump(data,f)


import itertools

def solution(users, emoticons):
    answer = [0, 0]
    sale = [10, 20, 30, 40]
    sale_rate = list(itertools.product(sale, repeat=len(emoticons)))
    for i in sale_rate:
        plus = 0
        earn_money = 0
        for user in users:
            rate, money = user
            tmp = 0
            for idx, e_rate in enumerate(i):
                if rate <= e_rate:
                    tmp += int(emoticons[idx] * (100-e_rate) / 100)
            if money <= tmp:
                plus += 1
            else:
                earn_money += tmp
        if answer[0] < plus:
            answer[0] = plus
            answer[1] = earn_money
        elif answer[0] == plus:
            if answer[1] < earn_money:
                answer[1] = earn_money
        else:
            continue
    print(answer)
    return answer

이모티콘 할인행사.py


import numpy as np
from PIL import Image
from tqdm import tqdm
from modules.Zest.Zest_Network import Zest_Network

class Zest_ImageProcessing(Zest_Network):

    square_size_HQ = 32
    square_size_LQ = 16

    def __init__(self, x, y) -> None:
        super().__init__(x, y)

    def process_image(self, image_path = None, image_pillow_obj = None):

        image_to_process = None
        image_to_output  = None

        # Prepare Image Object
        if image_path is not None:
            image_to_process = Image.open(image_path)
        elif image_pillow_obj is not None:
            if isinstance(image_pillow_obj, Image):
                image_to_process = image_pillow_obj
            else:
                # No Pillow Object
                return None
        else:
            # Nothing to work on
            return None

        # Start Processing Image

        # Crop image to fix the 16 x 16 squares
        image_to_process = image_to_process.crop(
            (
                0,
                0,
                (image_to_process.size[0] // 16) * 16,
                (image_to_process.size[1] // 16) * 16
            )
        )

        # Create a blank image to output the pixels
        image_to_output = Image.new(
            'RGB',
            (
                image_to_process.size[0] * 2,
                image_to_process.size[1] * 2
            )
        )

        for img_x in range(0, image_to_process.size[0], self.square_size_LQ):
            for img_y in range(0, image_to_process.size[1], self.square_size_LQ):
                # Convert Square Area to an readable array for the Network
                cache_square_LQ = []

                for square_x in range(img_x, (img_x) + 16):
                    for square_y in range(img_y, (img_y) + 16):
                        for color_value in image_to_process.getpixel((square_x, square_y)):
                            cache_square_LQ.append(color_value / 255)

                self.feed_forward(cache_square_LQ)
                # print(self.output.sum() / self.output.size, self.output.max())
                for pixel_color in range(0, len(self.output), 3):
                    # Recalculate where to put the pixel
                    pixel_y = ((pixel_color / 3) // self.square_size_HQ)
                    pixel_x = (pixel_color // 3) - (pixel_y * self.square_size_HQ)

                    # print(pixel_color, pixel_x, pixel_y, len(self.output), image_to_output.size, self.output[pixel_color])

                    image_to_output.putpixel(
                        (int(img_x * 2 + pixel_x), int(img_y * 2 + pixel_y)),
                        (
                            int(self.output[pixel_color]     * 255),
                            int(self.output[pixel_color + 1] * 255),
                            int(self.output[pixel_color + 2] * 255)
                        )
                    )
                # image_to_output.save('D:\\#Data\\shiro-output\\steps\\' + str(img_x) + '-' + str(img_y) + '.jpg')
        # input('hi')
        return image_to_output


from __future__ import print_function

import asyncio
import random
import threading
import time
import numpy as np
import websockets
from websocket import create_connection
from robot_dqnagent import DQNAgent
from robot_arena import Arena

class MSGWorker (threading.Thread):

  def __init__(self):
    self.coords = [100, 100]
    state_size = 2
    action_size = 5
    self.currentAction = ""
    self.agent = DQNAgent(state_size, action_size)
    self.agent.load('trekker_5000_refactored.h5')
    threading.Thread.__init__(self)
    self.connected = set()

  def run(self):
    while True:
      time.sleep(0.001)

  def act(self,x,y):

      state = np.array([x,y])
      #arena.drawRobot(state)

      #arena.setPos(np.array_str(state))

      mops= np.reshape(state, [1, 2])
      arena.drawRobot(state)

      action = self.agent.act_execute(mops)
      if action == 0 and self.currentAction != 0:  # up
          print("up")
          self.currentAction = 0
          ws = create_connection("ws://192.168.178.61:8080")
          ws.send("camup")
          ws.close()

      if action == 1 and self.currentAction != 1:  # down
          print("down")
          self.currentAction = 1
          ws = create_connection("ws://192.168.178.61:8080")
          ws.send("camdown")
          ws.close()

      if action == 2 and self.currentAction != 2:  # left
           print("left")
           self.currentAction = 2
           ws = create_connection("ws://192.168.178.61:8080")
           ws.send("left,1")
           ws.close()
      if action == 3 and self.currentAction != 3:  # right
           print("right")
           self.currentAction = 3
           ws = create_connection("ws://192.168.178.61:8080")
           ws.send("right,1")
           ws.close()

  @asyncio.coroutine
  def handler(self, websocket, path):
    self.connected.add(websocket)
    try:
      name = yield from websocket.recv()
      commaindex = name.find(",")
      commandlength = len(name)
      direction = name[0:commaindex]
      self.speed = name[commaindex+1:commandlength]

      self.act(direction,self.speed)

      #print(direction+','+self.speed)

      # here are the coordinates coming -> handled to the message worker !

    except websockets.exceptions.ConnectionClosed:
      pass
    finally:
      self.connected.remove(websocket)

  def sendData(self, data):
    for websocket in self.connected.copy():
      #print("Sending data: %s" % data)
      coro =yield from websocket.send(data)
      future = asyncio.run_coroutine_threadsafe(coro, loop)

if __name__ == "__main__":
  print('AI Server')
  msgWorker = MSGWorker()
  arena = Arena()

  try:

    msgWorker.start()
    ws_server = websockets.serve(msgWorker.handler, '192.168.178.67', 8080)
    loop = asyncio.get_event_loop()
    loop.run_until_complete(ws_server)
    loop.run_forever()
  except KeyboardInterrupt:
    stopFlag = True
    #TODO: close ws server and loop correctely
    print("Exiting program...")


import math
from PyQt5.QtWidgets import QGraphicsView, QGraphicsLineItem, QApplication, QMenu, QAction
from PyQt5.QtGui import QColor, QBrush, QPen
from PyQt5.QtCore import pyqtSlot, QLineF, QRectF, QPoint, QPointF, Qt
from pyqtgraph import GraphicsLayoutWidget, PlotItem, ViewBox, GraphicsItem, GraphicsView, PlotDataItem, TextItem, ButtonItem
from orbit import Orbit, BPM
from bpm_line_item import BPMLineItem
from magnet_view import MagnetView
from PyQt5.QtCore import QTimer

class OrbitView(GraphicsLayoutWidget):
    def __init__(self, orbit=None, axis="X", use_sector_ticks=True, parent=None, ymin=-1.0, ymax=1.0, name=None, label=None, units=None, draw_timer=None, magnet_list=None):
        super(OrbitView, self).__init__(parent=parent)
        axis = axis.lower()
        if axis not in ["x", "y", "tmit"]:
            raise Exception("Axis must be 'x', 'y', or 'tmit'")
        self.axis = axis
        self.use_sector_ticks = use_sector_ticks
        self.sector_ticks = [[],[]]
        self.ci.layout.setSpacing(0.0)
        self.plotLabel = self.addLabel(text=name, row=0, col=0, rowspan=3, angle=-90)
        self.up_magnet_view = MagnetView(magnet_list=orbit, direction="up")
        self.up_magnet_view.hideAxis('left')
        self.up_magnet_view.hideAxis('bottom')
        self.ci.layout.setRowStretchFactor(0,3)
        self.plotItem = self.addPlot(name=name, row=0, col=1)
        self.ci.layout.setRowStretchFactor(1,0)
        self.down_magnet_view = MagnetView(magnet_list=orbit, direction="down")
        self.down_magnet_view.hideAxis('left')
        self.up_magnet_view.setXLink(self.plotItem)
        self.down_magnet_view.setXLink(self.plotItem)
        self.ci.layout.setRowStretchFactor(2,0)
        self.show_magnet_buttons = False
        if axis != "tmit" and magnet_list is not None:
            self.show_magnet_views(True)
        self.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate) # Greatly improves drawing performance.
        self.plotItem.setMouseEnabled(y=False)
        #Customize the right-click menu.
        self.plotItem.setMenuEnabled(enableMenu=False, enableViewBoxMenu=None)
        reset_view_range = QAction("Reset View Range", self.plotItem.vb.menu)
        reset_view_range.triggered.connect(self.reset_range)
        self.plotItem.vb.scene().contextMenu = []
        existing_menu_actions = self.plotItem.vb.menu.actions()
        self.plotItem.vb.menu.insertAction(existing_menu_actions[0], reset_view_range)
        for action in existing_menu_actions:
            if str(action.text()) == "View All":
                self.plotItem.vb.menu.removeAction(action)
        self.plotItem.showGrid(y=True)
        #self.plotItem.getAxis('left').setStyle(tickTextWidth=60)
        #self.plotItem.getAxis('left').setStyle(autoExpandTextSpace=False)
        #if label is not None:
        #   self.plotItem.getAxis('left').enableAutoSIPrefix(enable=False)
        #   self.plotItem.getAxis('left').setLabel(text=label, units=units)
        self.bpm_brush = QBrush(QColor(0,255,0))
        self.energy_bpm_brush = QBrush(QColor(100,200,255))
        self.ymin = ymin #Y axis goes from self.ymin to self.ymax by default.
        self.ymax = ymax
        self.yminlimit = 10.0*ymin #This is the limit on the Y axis range.
        self.ymaxlimit = 10.0*ymax #This is the upper limit on the Y axis range.
        self.plotItem.setLimits(minYRange=0.04, maxYRange=abs(self.ymaxlimit - self.yminlimit))
        self.axis_pen = QPen(QBrush(QColor(255,255,255)), 0)
        self.axis_pen.setCapStyle(Qt.FlatCap)
        self.bpm_pen = QPen(self.bpm_brush, 2)
        self.bpm_pen.setCosmetic(True)
        self.bpm_pen.setCapStyle(Qt.FlatCap)
        self.no_beam_brush = QBrush(QColor(0,255,0,45))
        self.no_beam_pen = QPen(self.no_beam_brush, 2)
        self.no_beam_pen.setCosmetic(True)
        self.no_beam_pen.setCapStyle(Qt.FlatCap)
        self.energy_bpm_pen = QPen(self.energy_bpm_brush, 2)
        self.energy_bpm_pen.setCosmetic(True)
        self.energy_bpm_pen.setCapStyle(Qt.FlatCap)
        self.fit_brush = QBrush(QColor(255,255,255,255))
        self.fit_pen = QPen(self.fit_brush, 0)
        self.fit_pen.setCosmetic(True)
        self.fit_pen.setCapStyle(Qt.FlatCap)
        self.axis_line = QGraphicsLineItem(0.0,0.0,1.0,0.0)
        self.axis_line.setPen(self.axis_pen)
        self.plotItem.addItem(self.axis_line, ignoreBounds=True)
        self.lines = {}
        self.orbit = None
        self.needs_initial_range = True
        self.set_draw_timer(draw_timer)
        self._display_fit = False
        self.fit_data_item = None
        self.fit_options = {}
        if orbit is not None:
            self.set_orbit(orbit)

    def make_right_click_menu(self):
        menu = QMenu(self)
        return menu

    def display_fit(self, enabled=True):
        if enabled and self.fit_data_item is None:
            self.fit_data_item = PlotDataItem(pen=self.fit_pen)
            self.plotItem.addItem(self.fit_data_item)
        elif not enabled:
            self.plotItem.removeItem(self.fit_data_item)
            self.fit_data_item = None
        self._display_fit = enabled

    def set_draw_timer(self, new_timer, start=False):
        try:
            self.draw_timer.timeout.disconnect(self.redraw_bpms)
        except:
            pass
        if new_timer is None:
            new_timer = QTimer(self)
            new_timer.setInterval(int(1000/60))
        self.draw_timer = new_timer
        self.draw_timer.timeout.connect(self.redraw_bpms)
        if start:
            self.draw_timer.start()

    def set_orbit(self, orbit, reset_range=True):
        if self.orbit == orbit:
            return
        old_range = None
        old_zmax = None
        old_zmin = None
        if self.orbit is not None:
            old_range = self.plotItem.viewRect()
            old_zmax = self.orbit.zmax()
            old_zmin = self.orbit.zmin()
        self.clear_orbit()
        self.orbit = orbit
        extent = self.orbit.zmax() - self.orbit.zmin()
        self.plotItem.setLimits(xMin=self.orbit.zmin()-(0.02*extent), xMax=self.orbit.zmax()+(0.02*extent))
        self.plotItem.enableAutoRange(enable=False)
        self.axis_line.setLine(self.orbit.zmin(),0.0,self.orbit.zmax(),0.0)
        for bpm in self.orbit:
            line = BPMLineItem(bpm)
            self.lines[bpm.name] = line
            self.set_pen_for_bpm(bpm)
            self.plotItem.addItem(self.lines[bpm.name])
        if self.use_sector_ticks and (old_zmax != orbit.zmax() or old_zmin != orbit.zmin()):
            self.sector_ticks = [[],[]]
            self.sector_ticks[0] = self.orbit.sector_locations()
            unit_nums = [name.split(":")[-1] for name in self.orbit.names()]
            self.sector_ticks[1] = zip(self.orbit.z_vals(), unit_nums)
            self.plotItem.getAxis('bottom').setTicks(self.sector_ticks)
            self.plotItem.getAxis('bottom').setStyle(textFillLimits=[(0,0.72)])
            self.plotItem.showGrid(x=True)
        if reset_range or self.needs_initial_range:
            self.reset_range()
            self.needs_initial_range = False
        else:
            self.plotItem.setRange(old_range, padding=0.0, update=False)
        self.draw_timer.start()

    def show_magnet_views(self, enabled):
        if enabled == self.show_magnet_buttons:
            return
        self.show_magnet_buttons = enabled
        if enabled:
            self.addItem(self.up_magnet_view, row=1, col=1)
            self.addItem(self.down_magnet_view, row=2, col=1)
            self.up_magnet_view.setXLink(self.plotItem)
            self.down_magnet_view.setXLink(self.plotItem)
        else:
            self.removeItem(self.up_magnet_view)
            self.removeItem(self.down_magnet_view)

    def set_magnet_list(self, magnet_list):
        self.up_magnet_view.set_magnets(magnet_list, reset_range=False)
        self.down_magnet_view.set_magnets(magnet_list, reset_range=False)

    @pyqtSlot(bool)
    def reset_range(self, checked=False):
        self.plotItem.enableAutoRange(axis=ViewBox.XAxis)
        self.plotItem.setYRange(self.ymin, self.ymax)

    def wheelEvent(self, event):
        if event.modifiers() == Qt.ShiftModifier:
            numPixels = event.pixelDelta()
            numDegrees = event.angleDelta()
            if not numPixels.isNull():
                s = (1.005) ** (numPixels.y())
            else:
                s = (1.005) ** (numDegrees.y() * (-1.0/8.0))
            self.plotItem.vb.scaleBy(y=s)
        else:
            super(OrbitView, self).wheelEvent(event)

    def clear_orbit(self):
        self.draw_timer.stop()
        auto_range_x_enabled = self.plotItem.vb.state['autoRange'][0]
        auto_range_y_enabled = self.plotItem.vb.state['autoRange'][1]
        self.plotItem.enableAutoRange(enable=False)
        if self.orbit is None:
            return
        for bpm in self.orbit:
            self.plotItem.removeItem(self.lines[bpm.name])
        self.plotItem.enableAutoRange(x=auto_range_x_enabled, y=auto_range_y_enabled)
        self.lines = {}

    @pyqtSlot()
    def redraw_bpms(self):
        for bpm in self.orbit:
            self.set_pen_for_bpm(bpm)
            self.lines[bpm.name].setLine(bpm.z,0.0,bpm.z,bpm[self.axis])
        self.update_fit()

    def set_pen_for_bpm(self, bpm):
        if bpm.severity(self.axis) != 0:
            self.lines[bpm.name].setPen(self.no_beam_pen)
        else:
            if bpm.is_energy_bpm:
                self.lines[bpm.name].setPen(self.energy_bpm_pen)
            else:
                self.lines[bpm.name].setPen(self.bpm_pen)

    def update_fit(self):
        if not self._display_fit:
            return
        if self.orbit.fit_data is None:
            if self.fit_data_item is not None:
                self.fit_data_item.hide()
            return
        fit_data = None
        if self.axis == 'x':
            fit_data = self.orbit.fit_data['xpos']
        elif self.axis == 'y':
            fit_data = self.orbit.fit_data['ypos']
        self.fit_data_item.show()
        self.fit_data_item.setData(x=self.orbit.fit_data['zs'], y=fit_data)

    @pyqtSlot()
    def stop(self):
        self.draw_timer.stop()

    @pyqtSlot()
    def start(self):
        if self.orbit is not None:
            self.draw_timer.start()

    def setXLink(self, view):
        return self.plotItem.setXLink(view.plotItem)

    def setYLink(self, view):
        return self.plotItem.setYLink(view.plotItem)