import logging
import importlib
import numpy as np
import sys
import typing
import time
import copy
from d3m.primitive_interfaces.supervised_learning import SupervisedLearnerPrimitiveBase
from d3m.primitive_interfaces.base import CallResult
from d3m.metadata import hyperparams, params, base as metadata_base
from d3m import container

# lazy init
# import keras
# from tensorflow.keras.engine.sequential import Sequential

from . import config

# Input should from dataframe_to_tensor primitive
Inputs = container.DataFrame
Outputs = container.DataFrame  # results

class LSTMHyperparams(hyperparams.Hyperparams):
    LSTM_units = hyperparams.UniformInt(
        default=2048,
        lower=1,
        upper=pow(2, 31),
        description="Positive integer, dimensionality of the output space of LSTM model",
        semantic_types=["https://metadata.datadrivendiscovery.org/types/ControlParameter", ]
    )
    verbose = hyperparams.UniformInt(
        default=0,
        lower=0,
        upper=3,
        description="Verbosity mode. 0 = silent, 1 = progress bar, 2 = one line per epoch.",
        semantic_types=["https://metadata.datadrivendiscovery.org/types/ControlParameter", ]
    )
    batch_size = hyperparams.UniformInt(
        default=32,
        lower=1,
        upper=10000,
        description="The batch size for RNN training",
        semantic_types=["https://metadata.datadrivendiscovery.org/types/ControlParameter", ]
    )
    epochs = hyperparams.UniformInt(
        default=1000,
        lower=1,
        upper=sys.maxsize,
        description="epochs to do on fit process",
        semantic_types=["http://schema.org/Boolean", "https://metadata.datadrivendiscovery.org/types/ControlParameter", ]
    )
    shuffle = hyperparams.Hyperparameter[bool](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=True,
        description='Shuffle minibatches in each epoch of training (fit).'
    )
    loss_threshold = hyperparams.Hyperparameter[float](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=1e-5,
        description='Threshold of loss value to early stop training (fit).'
    )
    weight_decay = hyperparams.Hyperparameter[float](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=1e-6,
        description='Weight decay (L2 regularization) used during training (fit).'
    )
    learning_rate = hyperparams.Hyperparameter[float](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=1e-5,
        description='Learning rate used during training (fit).'
    )
    dropout_rate = hyperparams.Hyperparameter[float](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=0.5,
        description='Learning rate used during training (fit).'
    )
    validate_data_percent = hyperparams.Hyperparameter[float](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=0.2,
        description='The perncentage of the data used for validation purpose during train(fit).'
    )
    optimizer_type = hyperparams.Enumeration[str](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        values=['adam', 'sgd'],
        default='adam',
        description='Type of optimizer used during training (fit).'
    )
    momentum = hyperparams.Hyperparameter[float](
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        default=0.9,
        description='Momentum used during training (fit), only for optimizer_type sgd.'
    )

class Params(params.Params):
    target_column_name: str
    class_name_to_number: typing.Dict[str, int]
    keras_model: typing.Dict  # tensorflow.keras.engine.Sequential, cannot use because of lazy init
    feature_shape: typing.List[int]
    input_feature_column_name: str

class LSTM(SupervisedLearnerPrimitiveBase[Inputs, Outputs, Params, LSTMHyperparams]):
    """
    video classification primitive that use lstm RNN network
    """

    __author__ = 'USC ISI'
    metadata = hyperparams.base.PrimitiveMetadata({
        'id': 'dsbox-featurizer-video-classification-lstm',
        'version': config.VERSION,
        'name': "DSBox Video Classification LSTM",
        'description': 'Find the corresponding object position from given images(tensors)',
        'python_path': 'd3m.primitives.classification.lstm.DSBOX',
        'primitive_family': "CLASSIFICATION",
        'algorithm_types': ["DEEP_NEURAL_NETWORK"],
        'keywords': ['image', 'featurization', 'lstm'],
        'source': {
            'name': config.D3M_PERFORMER_TEAM,
            "contact": config.D3M_CONTACT,
            'uris': [config.REPOSITORY]
        },
        # The same path the primitive is registered with entry points in setup.py.
        'installation': [config.INSTALLATION],
        # Choose these from a controlled vocabulary in the schema. If anything is missing which would
        # best describe the primitive, make a merge request.

        # A metafeature about preconditions required for this primitive to operate well.
        'precondition': [],
        'hyperparms_to_tune': []
    })

    def __init__(self, *, hyperparams: LSTMHyperparams, volumes: typing.Union[typing.Dict[str, str], None] = None) -> None:
        super().__init__(hyperparams=hyperparams, volumes=volumes)
        self.hyperparams = hyperparams

        # All other attributes must be private with leading underscore
        self._has_finished = False
        self._iterations_done = 0
        self._inited = False
        self._model = None  # this used to store the real of the model
        self._training_inputs = None
        self._training_outputs = None
        self._epochs = self.hyperparams['epochs']
        self._batch_size = self.hyperparams['batch_size']
        self._loss_threshold = self.hyperparams['loss_threshold']
        self._validate_data_percent = self.hyperparams['validate_data_percent']
        self._shuffle = self.hyperparams['shuffle']
        self._verbose_mode = self.hyperparams['verbose']
        self._learning_rate = self.hyperparams['learning_rate']
        self._weight_decay = self.hyperparams['weight_decay']
        self._LSTM_units = self.hyperparams['LSTM_units']
        self._dropout_rate = self.hyperparams['dropout_rate']
        self._optimizer_type = self.hyperparams['optimizer_type']
        self._momentum = self.hyperparams['momentum']
        self._metrics = []
        self._class_name_to_number = {}
        self._input_feature_column_name = ""
        self.logger = logging.getLogger(__name__)

    def get_params(self) -> Params:
        param = Params(
            keras_model=self._model.get_config(),
            class_name_to_number=self._class_name_to_number,
            target_column_name=self._target_column_name,
            feature_shape=self._feature_shape,
            input_feature_column_name=self._input_feature_column_name
        )
        return param

    def set_params(self, *, params: Params) -> None:
        from tensorflow.keras import Sequential
        # self._model = self._lazy_init_lstm()
        config_lstm = params['keras_model']
        self._model = Sequential.from_config(config_lstm)
        self._class_name_to_number = params["class_name_to_number"]
        self._target_column_name = params["target_column_name"]
        self._feature_shape = params["feature_shape"]
        self._input_feature_column_name = params["input_feature_column_name"]

    def set_training_data(self, *, inputs: Inputs, outputs: Outputs) -> None:

        self._training_inputs = copy.copy(inputs)
        self._training_outputs = copy.copy(outputs)

        if self._training_inputs.shape[0] < self._training_outputs.shape[0]:
            if 'd3mIndex' in self._training_inputs.columns:
                inputs_index_with_contents = self._training_inputs['d3mIndex'].astype(int).tolist()
            else:
                self.logger.warning("No d3mIndex found in input training dataset!")
                inputs_index_with_contents = list(range(self._training_inputs.shape[0]))
            self._training_outputs = self._training_outputs.iloc[inputs_index_with_contents, :]
        elif self._training_inputs.shape[0] > self._training_outputs.shape[0]:
            raise ValueError("The length of inputs is larger than outputs which is impossible.")

        # TODO: maybe use a better way to find the feature input columns
        first_content_index = 0
        input_column_names = []
        input_column_numbers = []
        for i, each_column in enumerate(self._training_inputs.columns):
            while isinstance(self._training_inputs[each_column][first_content_index], type(None)):
                first_content_index += 1
            if type(self._training_inputs[each_column][first_content_index]) is np.ndarray and len(
                    self._training_inputs[each_column][first_content_index].shape) == 2:
                input_column_names.append(each_column)
                input_column_numbers.append(i)
        if len(input_column_names) < 1:
            raise ValueError("No extract feature attribute from input detected!")
        if len(input_column_names) > 1:
            self.logger.warning("More that 1 feature attribute detected! Will only use first one")

        useless_train_rows = []
        input_column_number = input_column_numbers[0]
        for i in range(self._training_inputs.shape[0]):
            if self._training_inputs.iloc[i, input_column_number] is None:
                useless_train_rows.append(i)

        self._training_inputs = self._training_inputs.drop(useless_train_rows)
        self._training_outputs = self._training_outputs.drop(useless_train_rows)

        self.logger.info("following rows are dropped beacuse the training data is None.")
        self.logger.info(str(useless_train_rows))

        self._input_feature_column_name = input_column_names[0]
        self._features = self._training_inputs[self._input_feature_column_name]
        self._training_size = self._training_inputs.shape[0]
        target_column_names = self._training_outputs.columns.tolist()
        if 'd3mIndex' in target_column_names:
            target_column_names.remove('d3mIndex')
        if len(target_column_names) < 1:
            raise ValueError("No target attribute from output detected!")
        if len(target_column_names) > 1:
            self.logger.warning("Multiple target detected! Will only use first one")
        self._target_column_name = target_column_names[0]
        class_names = set(self._training_outputs[self._target_column_name].tolist())
        self._number_of_classes = len(class_names)
        self._feature_shape = list(self._features[first_content_index].shape)
        self._features_amount = self._training_inputs.shape[0]
        self._training_inputs_ndarry = np.empty((self._features_amount, self._feature_shape[0], self._feature_shape[1]))
        for i, each in enumerate(self._features):
            self._training_inputs_ndarry[i] = each
        self._training_ouputs_ndarry = np.zeros((self._features_amount, self._number_of_classes))
        # make a reference from name to number
        for i, each in enumerate(class_names):
            self._class_name_to_number[each] = i
        # update this reference to self._training_ouputs_ndarry
        for i, each in enumerate(self._training_outputs[self._target_column_name]):
            self._training_ouputs_ndarry[i, self._class_name_to_number[each]] = 1
        self._fitted = False

    def fit(self, *, timeout: float = None, iterations: int = None) -> CallResult[None]:
        """
            If using the pre-training model, here we will use this model to detect what inside the bounding boxes from training dataset
            Then, count the number of the objects deteced in each box, we will treat only the objects amount number larger than the threshold
            to be the target that we need to detect in the test part.
        """
        if self._training_inputs is None:
            raise ValueError('Missing training(fitting) data.')

        if timeout is None:
            timeout = np.inf
        if iterations is None:
            iterations = self._epochs
        # if self._minibatch_size > self._training_size:
        #     self._minibatch_size = self._training_size
        # initialze the model if not model loaded
        if not self._model:
            self._model = self._lazy_init_lstm()
        self._iterations_done = 0
        start = time.time()
        self._has_finished = False
        if self._shuffle:
            indices = np.random.permutation(self._training_inputs_ndarry.shape[0])
        else:
            indices = list(range(self._training_inputs_ndarry.shape[0]))
        number_of_training_data = int((1 - self._validate_data_percent) * self._training_size)
        self.logger.info(str(number_of_training_data) + " of the " + str(
            self._training_size) + " input data will be used to trained. The remainede will be used for validation.")
        training_idx, test_idx = indices[:number_of_training_data], indices[number_of_training_data:]
        training_x, test_x = self._training_inputs_ndarry[training_idx, :], self._training_inputs_ndarry[test_idx, :]
        training_y, test_y = self._training_ouputs_ndarry[training_idx, :], self._training_ouputs_ndarry[test_idx, :]

        # repeat fit until interation down or epoch_loss less than threshold
        while time.time() < start + timeout and self._iterations_done < iterations:
            self.logger.info("Start fit on iteration " + str(self._iterations_done))

            result = self._model.fit(training_x, training_y,
                                     # result = self._model.fit(self._training_inputs_ndarry, self._training_ouputs_ndarry,
                                     batch_size=self._batch_size,
                                     # validation_split = self._validate_data_percent,
                                     verbose=self._verbose_mode,
                                     validation_data=(test_x, test_y),
                                     shuffle=self._shuffle,
                                     initial_epoch=self._iterations_done,
                                     epochs=1 + self._iterations_done)
            # result available for these values
            # train_loss = result.history['loss']
            # test_loss   = result.history['val_loss']
            # train_acc  = result.history['acc']
            # test_acc    = result.history['val_acc']
            epoch_loss = result.history['val_loss'][-1]
            self._iterations_done += 1
            if epoch_loss < self._loss_threshold:
                self._has_finished = True
                self.logger.info("The model is well fitted during " + str(self._iterations_done) + "interation. No more needed.")
                return CallResult(None)

        self.logger.info("The model fitting finished with" + str(self._iterations_done) + "interation.")
        self.logger.info("The final result is:")
        try:
            self.logger.info(str(result.history))
            self.logger.info("train_loss      = " + str(result.history['loss']))
            self.logger.info("train_acc       = " + str(result.history['acc']))
            self.logger.info("validation_loss = " + str(result.history['val_loss']))
            self.logger.info("validation_acc  = " + str(result.history['val_acc']))
        except:
            pass
        return CallResult(None)

    def produce(self, *, inputs: Inputs, timeout: float = None, iterations: int = None) -> CallResult[Outputs]:
        """
            Use YOLO to detect the objects in the input dataframe
            The function will read the images if the input is a dataframe with image names
            The detected output depends on the input training dataset's ojbects
        """
        if not self._model:
            raise ValueError('The model not fitted or loaded!')

        produce_input_feature = inputs[self._input_feature_column_name]
        result_length = inputs.shape[0]
        produce_input_ndarry = np.empty((result_length, self._feature_shape[0], self._feature_shape[1]))
        for i, each in enumerate(produce_input_feature):
            produce_input_ndarry[i] = each

        prediction = self._model.predict(produce_input_ndarry)
        results = []
        number_to_class_name = dict((v, k) for k, v in self._class_name_to_number.items())
        for each_predict in prediction:
            label = np.argmax(each_predict)
            results.append(number_to_class_name[label])
        output_dataframe = container.DataFrame(inputs['d3mIndex'])
        extracted_feature_dataframe = container.DataFrame({self._target_column_name: results}, generate_metadata=False)
        output_dataframe[self._target_column_name] = extracted_feature_dataframe

        # add metadata
        for each_column in range(2, output_dataframe.shape[1]):
            metadata_selector = (metadata_base.ALL_ELEMENTS, each_column)
            metadata_each_column = {'semantic_types': ('https://metadata.datadrivendiscovery.org/types/PredictedTarget',)}
            output_dataframe.metadata = output_dataframe.metadata.update(metadata=metadata_each_column,
                                                                         selector=metadata_selector)

        self._has_finished = True
        self._iterations_done = True
        return CallResult(output_dataframe, self._has_finished)

    def _lazy_init_lstm(self):  # -> "tensorflow.keras.models"
        """
            a lazy init function which initialize the LSTM model only when the primitive's fit/ produce method is called
        """
        keras_models = importlib.import_module('tensorflow.keras.models')
        keras_layers = importlib.import_module('tensorflow.keras.layers')
        keras_optimizers = importlib.import_module('tensorflow.keras.optimizers')
        model = keras_models.Sequential()
        # TODO: following parameters could also be hyperparameters for tuning
        model.add(keras_layers.LSTM(self._LSTM_units, return_sequences=False,
                                    input_shape=self._feature_shape,
                                    dropout=self._dropout_rate))
        model.add(keras_layers.Dense(512, activation='relu'))
        model.add(keras_layers.Dropout(self._dropout_rate))
        model.add(keras_layers.Dense(self._number_of_classes, activation='softmax'))
        self._metrics = ['accuracy']
        if self._number_of_classes >= 10:
            self._metrics.append('top_k_categorical_accuracy')
            # Now compile the network
        if self._optimizer_type == 'adam':
            optimizer = keras_optimizers.Adam(lr=self._learning_rate, decay=self._weight_decay)
        elif self._optimizer_type == 'sgd':
            optimizer = keras_optimizers.SGD(lr=self._learning_rate, decay=self._weight_decay, mementum=self._momentum)
        else:
            raise ValueError('Unsupported optimizer_type: {}. Available options: adam, sgd'.format(self._optimizer_type))

        model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=self._metrics)
        return model

'''
    def __getstate__(self) -> typing.Dict:
        """
        This method is used by the pickler as the state of object.
        The object can be recovered through this state uniquely.
        Returns:
            state: Dict
                dictionary of important attributes of the object
        """
        # print("[INFO] Get state called")

        state = self.__dict__  # get attribute dictionary

        # add the fitted_primitives
        state['fitted_pipe'] = self.runtime.steps_state
        state['pipeline'] = self.pipeline.to_json_structure()
        state['log_dir'] = self.log_dir
        state['id'] = self.id
        del state['runtime']  # remove runtime entry

        return state

    def __getstate__(self):
        model_str = ""
        with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
            tensorflow.keras.models.save_model(self, fd.name, overwrite=True)
            model_str = fd.read()
        d = { 'model_str': model_str }
        return d

    def __setstate__(self, state):
        with tempfile.NamedTemporaryFile(suffix='.hdf5', delete=True) as fd:
            fd.write(state['model_str'])
            fd.flush()
            model = tensorflow.keras.models.load_model(fd.name)
        self.__dict__ = model.__dict__

    def __setstate__(self, state: typing.Dict) -> None:
        """
        This method is used for unpickling the object. It takes a dictionary
        of saved state of object and restores the object to that state.
        Args:
            state: typing.Dict
                dictionary of the objects picklable state
        Returns:
        """

        # print("[INFO] Set state called!")

        fitted = state['fitted_pipe']
        del state['fitted_pipe']

        structure = state['pipeline']
        state['pipeline'] = Pipeline.from_json_structure(structure)

        run = Runtime(state['pipeline'], fitted_pipeline_id=state['id'],
                      volumes_dir=FittedPipeline.static_volume_dir, log_dir=state['log_dir'])
        run.steps_state = fitted

        state['runtime'] = run

        self.__dict__ = state
'''


from .base import Renderer

class DoxygenTypeSubRenderer(Renderer):

    def render(self):

        nodelist = []

        # Process all the compound children
        for compound in self.data_object.get_compound():
            context = self.context.create_child_context(compound)
            compound_renderer = self.renderer_factory.create_renderer(context)
            nodelist.extend(compound_renderer.render())

        return nodelist

# Used below in CompoundTypeSubRenderer and in RefTypeSubRenderer in compound.py so we have split it
# out in a helper function. This feels fairly ugly due to the number of arguments. A forced
# refactoring for the sake of refactoring rather than a beautiful reuse of code.
def render_compound(
    name,
    kind,
    parent_context,
    rendered_data,
    renderer_factory,
    node_factory,
    domain_target,
    doxygen_target,
    document
    ):

    # Build targets for linking
    signode = node_factory.desc_signature()
    signode.extend(domain_target)
    signode.extend(doxygen_target)

    file_data = parent_context.node_stack[0]
    new_context = parent_context.create_child_context(file_data.compounddef)

    # Check if there is template information and format it as desired
    if file_data.compounddef.templateparamlist:
        context = new_context.create_child_context(file_data.compounddef.templateparamlist)
        renderer = renderer_factory.create_renderer(context)
        template_nodes = [node_factory.Text("template <")]
        template_nodes.extend(renderer.render())
        template_nodes.append(node_factory.Text(">"))
        signode.append(node_factory.line("", *template_nodes))

    # Set up the title and a reference for it (refid)
    signode.append(node_factory.emphasis(text=kind))
    signode.append(node_factory.Text(" "))
    signode.append(node_factory.desc_name(text=name))

    contentnode = node_factory.desc_content()

    if file_data.compounddef.includes:
        for include in file_data.compounddef.includes:
            context = new_context.create_child_context(include)
            renderer = renderer_factory.create_renderer(context)
            contentnode.extend(renderer.render())

    contentnode.extend(rendered_data)

    node = node_factory.desc()
    node.document = document
    node['objtype'] = kind
    node.append(signode)
    node.append(contentnode)

    return [node]

class CompoundTypeSubRenderer(Renderer):

    def __init__(self, compound_parser, *args):
        Renderer.__init__(self, *args)

        self.compound_parser = compound_parser

    def create_doxygen_target(self):
        """Can be overridden to create a target node which uses the doxygen refid information
        which can be used for creating links between internal doxygen elements.

        The default implementation should suffice most of the time.
        """

        refid = "%s%s" % (self.project_info.name(), self.data_object.refid)
        return self.target_handler.create_target(refid)

    def create_domain_target(self):
        """Should be overridden to create a target node which uses the Sphinx domain information so
        that it can be linked to from Sphinx domain roles like cpp:func:`myFunc`

        Returns a list so that if there is no domain active then we simply return an empty list
        instead of some kind of special null node value"""

        return []

    def render(self):

        # Read in the corresponding xml file and process
        file_data = self.compound_parser.parse(self.data_object.refid)

        context = self.context.create_child_context(file_data)
        data_renderer = self.renderer_factory.create_renderer(context)

        # Defer to function for details
        return render_compound(
                self.data_object.name,
                self.data_object.kind,
                context,
                data_renderer.render(),
                self.renderer_factory,
                self.node_factory,
                self.create_domain_target(),
                self.create_doxygen_target(),
                self.state.document
                )

class ClassCompoundTypeSubRenderer(CompoundTypeSubRenderer):

    def create_domain_target(self):

        return self.domain_handler.create_class_target(self.data_object)


# coding=utf-8
# Date: 15/4/10'
# Email: wangjian2254@icloud.com
import json
from django.conf import settings
from django.db.models import Q
from Need_Server.settings import CACHES_JPUSH_ALIAS_TIME_OUT

from util import PROJECT_ID_FLAG_OR_FILEGROUP_ID_ALIAS, PROJECT_POWER_TIMELINE, PROJECT_USER_REALPOWERS

__author__ = u'王健'

from django.core.cache import cache

def get_alias_by_project_id_flag(project_id, flag, file_group_id=None):
    """
    通过缓存查找alias
    by：尚宗凯 at：2015-04-09
    修复bug
    by：尚宗凯 at：2015-04-09
    优化缓存结果的非空判断
    by:王健 at:2015-05-21
    修复逻辑bug
    by：尚宗凯 at：2015-05-21
    修改获取alias逻辑
    by：尚宗凯 at：2015-05-31
    """
    cache_key = PROJECT_ID_FLAG_OR_FILEGROUP_ID_ALIAS % (project_id, flag)
    alias = cache.get(cache_key)
    if alias is None:
        if file_group_id is None:
            from util.cache_handle import query_project_filegroup_data_
            result = query_project_filegroup_data_(project_id)
            file_group_id = result['flags2'][flag]
        alias = []
        users = []
        from util.cache_handle import get_project_user_group_use_cache
        project_user_group = get_project_user_group_use_cache(project_id)
        for user_list in project_user_group.values():
            users.extend(user_list)
        users = list(set(users))
        for user_id in users:
            powers = get_real_power_by_project_user(project_id,user_id)
            if int(file_group_id)*100 in powers:
                alias.append(user_id)
    cache.set(cache_key, alias, CACHES_JPUSH_ALIAS_TIME_OUT)
    return alias

# def get_alias_by_project_id_filegroup_id(project_id, filegroup_id):
#     """
#     根据project_id filegroup_id获取alias
#     by：王健 at：2015-05-07
#     优化缓存结果的非空判断
#     by:王健 at:2015-05-21
#     修改控制校验
#     by:王健 at:2015-05-21
#     """
#     from needserver.models import Group
#
#     cache_key = PROJECT_ID_FLAG_OR_FILEGROUP_ID_ALIAS % (project_id, filegroup_id)
#     alias = cache.get(cache_key)
#     if alias is None:
#         project_powers = get_cache_project_power_timeline(project_id)[0]
#         flag = project_powers['pks'][str(filegroup_id)]
#         alias = Group.get_user_id_by_group_ids(get_power_group(flag, project_powers))
#         cache.set(cache_key, alias, CACHES_JPUSH_ALIAS_TIME_OUT)
#     return alias

def get_cache_project_power_timeline(project_id):
    """
    获取缓存中的project_power_timeline
    by：尚宗凯 at：2015-04-09
    修改返回值，兼容 权限 观察者模块
    by：王健 at：2015-04-10
    修改极光推送方式
    by：尚宗凯 at：2015-05-07
    优化缓存结果的非空判断
    by:王健 at:2015-05-21
    修改逻辑错误
    by：尚宗凯 at：2015-05-21
    """
    from needserver.models import Group, FileGroup

    cache_key_project = PROJECT_POWER_TIMELINE % project_id
    project_powers = cache.get(cache_key_project)
    project_powers_flag = False

    if project_powers is None:
        project_powers_flag = True
        flag_dict = {}
        flag_pk_dict = {}
        flag_pk_dict = {}
        powers_dict = {}
        power_timeline = 0
        project_powers = {'flag': flag_dict, 'powers': powers_dict, 'pks': flag_pk_dict}
        for file_group in FileGroup.objects.filter(Q(project_id=project_id) | Q(project=None)):
            if file_group.father_id:
                flag_dict[file_group.flag] = file_group.father_id
            else:
                flag_dict[file_group.flag] = file_group.pk
            flag_pk_dict[str(file_group.pk)] = file_group.flag
        for f in flag_dict.keys():
            for k in flag_pk_dict.keys():
                if flag_pk_dict[k] == f:
                    flag_pk_dict[k] = flag_pk_dict[str(flag_dict[f])]

        for x in Group.objects.filter(project_id=project_id).exclude(type='root').values_list('powers', 'timeline',
                                                                                'id'):
            if x[0] and isinstance(x[0], (str, unicode)):
                powers = json.loads(x[0])
                powers_dict[str(x[2])] = powers
            if power_timeline < int(x[1]):
                power_timeline = int(x[1])
        project_powers['timeline'] = power_timeline
        cache.set(cache_key_project, project_powers, settings.CACHES_TIMEOUT)
    return project_powers, project_powers_flag

def get_power_group(flag, project_powers):
    """
    获取当前flag有权限的group
    by:尚宗凯 at：2015-04-09
    修改获取group方式
    by：王健 at：2015-05-07
    """
    from needserver.models import FileGroup

    power = project_powers['flag'].get(flag) * 100
    grps = []
    for p in project_powers['powers']:
        if power in project_powers['powers'][p]:
            grps.append(p)
    return grps

def get_real_power_by_project_user(project_id, user_id):
    """
    通过project_id user_id返回用户的真实权限
    by: 尚宗凯 at：2015-05-20
    """
    powers = cache.get(PROJECT_USER_REALPOWERS % (project_id, user_id))
    if powers is None:
        from needserver.models import Person
        powers = Person.objects.get(user_id=user_id, project_id=project_id).real_powers()
        cache.set(PROJECT_USER_REALPOWERS % (project_id, user_id), powers, settings.CACHES_TIMEOUT)
    return powers

def whether_user_have_power_by_flag(user_id, project_id, flags_or_flag, all_flag_dict=None):
    """
    通过user_id project_id flag判断是否具有权限
    by：尚宗凯 at：2015-05-20
    针对示例项目对powers做特殊处理
    by：尚宗凯 at：2015-05-21
    使用外部参数，避免一次 cache操作
    by:王健 at:2015-05-28
    """
    if int(project_id) == settings.SHOW_PROJECT_ID:
        powers = []
    else:
        powers = get_real_power_by_project_user(project_id, user_id)
    from util.cache_handle import query_project_filegroup_data_
    if all_flag_dict is None:
        all_flag_dict = query_project_filegroup_data_(project_id)
    if isinstance(flags_or_flag,(str,unicode)):
        file_group_id = all_flag_dict['flags2'][all_flag_dict['flag_father'][flags_or_flag]]
        if int(file_group_id)*100 in powers:
            return True
        else:
            return False
    else:
        result = {}
        for flag in flags_or_flag:
            file_group_id = all_flag_dict['flags2'][all_flag_dict['flag_father'][flag]]
            if int(file_group_id)*100 in powers:
                result[flag] = True
            else:
                result[flag] = False
        return result

def all_flag_user_have_power_by_flag(user_id, project_id, flags_or_flag, all_flag_dict=None):
    """
    根据flag 获取 具有权限的所有flag
    :param user_id:
    :param project_id:
    :param flags_or_flag:
    :param all_flag_dict:
    :return:
    """
    if all_flag_dict is None:
        from util.cache_handle import query_project_filegroup_data_
        all_flag_dict = query_project_filegroup_data_(project_id)
    if not flags_or_flag:
        flags_or_flag = all_flag_dict['flags2'].keys()
    m = whether_user_have_power_by_flag(user_id, project_id, flags_or_flag, all_flag_dict)
    tmp = []
    for k in m:
        if m[k]:
            if not all_flag_dict["flag_child"][k]:
                tmp.append(k)
            else:
                tmp.extend(all_flag_dict["flag_child"][k])
    all_have_power_flag = list(set(tmp))
    return all_have_power_flag


import pandas_datareader.data as web
import pandas as pd
import numpy as np
import os
import time

def pull_data_from_av(tickers, data_type, start_date, end_date):
	data = pd.DataFrame()
	for idx,ticker in enumerate(tickers):
		ticker = ticker.replace(" ","")
		if idx == 5:
			time.sleep(60)
		quote = web.DataReader(ticker, data_type, start_date, end_date,  api_key=os.getenv('ALPHAVANTAGE_API_KEY'))
		# since alpha vantage blocks requests
		quote['ticker'] = ticker
		data = pd.concat([data, quote])
	return data

def __add_ticker_names_to_df(tickers, data_frame):
	list_of_columns = ['returns','stdev','sharpe'] + sorted(tickers)
	data_frame = pd.DataFrame(data_frame.T,columns=list_of_columns)
	return data_frame

def simulation_without_active_management(table, num_of_portfolios, tickers):
	num_of_tickers = len(tickers)
	table.head()
	#calculate montly returns
	returns_montly = table.pct_change()
	returns_annual = returns_montly.mean()*12

	# get daily and covariance of returns of the stock
	cov_montly = returns_montly.cov()
	cov_annual = cov_montly * 12

	#Create empty dataframe
	portfolios = np.zeros((4+len(tickers)-1, num_of_portfolios))

	#Run simulations of portfolios with random weights
	for portfolio in range(num_of_portfolios):
		weights = weights = np.random.random(num_of_tickers)
		weights /= np.sum(weights)
		returns = np.dot(weights, returns_annual)
		volativity = np.sqrt(np.dot(weights.T, np.dot(cov_annual, weights)))
		portfolios[0, portfolio] = returns
		portfolios[1, portfolio] = volativity
		portfolios[2, portfolio] = returns/volativity
		for weight in range(len(weights)):
			portfolios[weight+3, portfolio] = weights[weight]

	portfolios = __add_ticker_names_to_df(tickers, portfolios)
	return portfolios

def clean_table(data):
	#Add date field for easier handling and discard data which is not needed
	data['date'] = data.index
	data = data.filter(['date', 'ticker', 'adjusted close'], axis=1)

	#Clean table to easy to read format
	clean = data.set_index('date')
	table = clean.pivot(columns='ticker')
	table = table.replace(0, np.NaN).fillna(method='ffill')
	return table


# coding: UTF-8
'''''''''''''''''''''''''''''''''''''''''''''''''''''
   file name: alexnet.py
   create time: 2018年09月5日
   author: wenqi Tang

'''''''''''''''''''''''''''''''''''''''''''''''''''''
# based on Frederik Kratzert's alexNet with tensorflow
import tensorflow as tf
import numpy as np
from functools import reduce

# define different layer functions
# we usually don't do convolution and pooling on batch and channel
def maxPoolLayer(x, ksize,strides=[1,1], name='None', padding = "SAME"):
    """max-pooling"""
    return tf.nn.max_pool(x, ksize =[1]+ ksize+[1],
                          strides = [1] +strides+[1], padding = padding, name = name)

def fcLayer(x, input_size, output_size, reluFlag, name):
    """fully-connect"""
    with tf.variable_scope(name) as scope:
        w = tf.get_variable("w", shape = [input_size, output_size], dtype = "float")
        b = tf.get_variable("b", [output_size], dtype = "float")
        out = tf.nn.xw_plus_b(x, w, b, name = scope.name)
        if reluFlag:
            return tf.nn.relu(out)
        else:
            return out

def convLayer(x, ksize, strides,out_channel, name, padding = "SAME"):
    """convolution"""
    in_channel = int(x.get_shape()[-1])

    conv = lambda a, b: tf.nn.conv2d(a, b, strides = [1] +strides +[ 1], padding = padding)

    with tf.variable_scope(name) as scope:
        w = tf.get_variable("w", shape = ksize+[in_channel,out_channel])
        b = tf.get_variable("b", shape = [out_channel])
        out_put = conv(x,w)
        # print mergeFeatureMap.shape
        out = tf.nn.bias_add(out_put, b)
        return tf.nn.relu(out, name = scope.name)

class alexNet(object):
    """alexNet model"""
    def __init__(self, x, classNum, skip=None, modelPath = "alexnet"):
        self.X = x
        self.CLASSNUM = classNum
        self.SKIP = skip
        self.MODELPATH = modelPath
        self.training = True
        #build CNN
        self.buildCNN()

    def buildCNN(self):
        """build model"""
        conv1 = convLayer(self.X, [5, 5], [1, 1], 128, "conv1", "SAME")
        pool1 = maxPoolLayer(conv1,[3, 3],[ 1,1], "pool1", "SAME")

        norm_pool1=tf.layers.batch_normalization(pool1,training=self.training)
        conv2 = convLayer(norm_pool1, [5, 5], [1, 1], 64, "conv2",'SAME')
        pool2 = maxPoolLayer(conv2,[3, 3], [2, 2], "pool2", "SAME")

        norm_pool2=tf.layers.batch_normalization(pool2,training=self.training)
        conv3 = convLayer(norm_pool2, [5, 5], [1, 1], 64, "conv3",'VALID')
        pool3 = maxPoolLayer(conv3, [3, 3], [2, 2], "pool3", "VALID")

        norm_pool3=tf.layers.batch_normalization(pool3,training=self.training)
        conv3 = convLayer(norm_pool2, [3, 3], [1, 1], 64, "conv3",'VALID')
        pool3 = maxPoolLayer(conv3, [3, 3], [2, 2], "pool3", "VALID")

        shapes = pool3.get_shape().as_list()[1:]
        mul = reduce(lambda x,y:x * y,shapes)

        reshape = tf.reshape(pool3,[-1,mul])
        dim = reshape.get_shape()[1].value

        norm_reshape=tf.layers.batch_normalization(reshape,training=self.training)
        fc1 = fcLayer(norm_reshape, dim, 512, reluFlag=True, name = "fc4")

        norm_fc1=tf.layers.batch_normalization(fc1,training=self.training)
        fc2 = fcLayer(norm_fc1, 512, 128, reluFlag=True,name =  "fc5")

        norm_fc2=tf.layers.batch_normalization(fc2,training=self.training)
        self.fc3 = fcLayer(norm_fc2, 128, self.CLASSNUM, reluFlag=True,name =  "fc6")

    # def loadModel(self, sess):
    #     """load model"""
    #     wDict = np.load(self.MODELPATH, encoding = "bytes").item()
    #     #for layers in model
    #     for name in wDict:
    #         if name not in self.SKIP:
    #             with tf.variable_scope(name, reuse = True):
    #                 for p in wDict[name]:
    #                     if len(p.shape) == 1:
    #                         #bias
    #                         sess.run(tf.get_variable('b', trainable = False).assign(p))
    #                     else:
    #                         #weights
    #                         sess.run(tf.get_variable('w', trainable = False).assign(p))


import nltk
import urllib2
from BeautifulSoup import BeautifulSoup
import sys

seedurl_1 = 'http://www.poemhunter.com/p/t/l.asp?p='
seedurl_2='&l=Top500'

urllist = []
page = 0

for i in range(1,10):
    seedurl = seedurl_1 + str(i) + seedurl_2
    soup = BeautifulSoup(urllib2.urlopen(seedurl))
    f = open('poets_list.txt', 'w')
    poem_stats_file = open('poem_stats.txt','w')
    poets_grid = soup.findAll('ol', attrs={'class':'poets-grid cols-2'})
    for ol in poets_grid:
        poets = ol.findAll('li')
        for poet in poets:
            link = poet.find('a')
            if link:
                url = 'http://www.poemhunter.com' + link['href'] + 'stats/'
                urllist.append(url)
                #f.write(url)
                #f.write('\n')
for links in urllist:
    f.write(links + "\n")

file_final = open('dataset.txt', 'w')
log_file = open('logfile.txt', 'w')
with open('poets_list.txt') as poets:
    for line in poets:
        poet = line[26:line.find('/stats/')].replace('-',' ')
        full_poem_link = ""
        try:
            soup1 = BeautifulSoup(urllib2.urlopen(line))
            for p in soup1.findAll('table', attrs={'class': 'stats ratings'})[0].findAll('td', attrs={'class' : 'title'}):
                href = str(p.find('a')['href'])
                poem_link = line.rstrip() + href[href.find('?'):]
                if 'poem=' not in poem_link:
                    continue

                rating = p.findNextSiblings(attrs={'class': 'rating'})[0].renderContents().strip()
                num_rating = p.findNextSiblings(attrs={'class': 'num-rating'})[0].renderContents().strip()
                my_poem_list = p.findNextSiblings(attrs={'class': 'my-poem-list'})[0].renderContents().strip()

                soup2 = BeautifulSoup(urllib2.urlopen(poem_link))
                full_poem_link = 'http://www.poemhunter.com' + str(soup2.find('div', attrs = {'class': 'box brown'}).find('a')['href'])

                soup3 = BeautifulSoup(urllib2.urlopen(full_poem_link))

                try:
                    #poem_text = soup3.find('div', attrs = {'class': 'KonaBody'}).find('p').renderContents().lstrip().replace('Anonymous submission.','').replace('\n')
                    poem_text = soup3.find('div', attrs = {'class': 'KonaBody'}).find('p').renderContents().lstrip().replace('Anonymous submission.','').replace('\n',' ').rstrip('
').rstrip().rstrip('

')
                except:
                    log_file.write(full_poem_link + "\t" + str(sys.exc_info()[0]) + "\n")

                file_final.write(poet + "\t" + poem_text + "\t" + rating + "\t" + num_rating + "\t" + my_poem_list +"\n")

        except:
            log_file.write(full_poem_link + "\t" + str(sys.exc_info()[0]) + "\n")
            #pass


#!/usr/bin/env python3

"""
A script which follows align_two_sequence_files.py and iterates through all
directories looking for files ending with the *align.txt in the name. Pulls
the best %ID found for each particular identity and generates a CSV file with
just two columns, one for the locus name and one for the percent identity.

Input:
    1. Path to a directory to iterate through to check alignment stats in.
    2. Name of the output CSV to generate.

Output:
    1. A two-column CSV file with locus_name,percent_id

Usage:
    assess_all_alignments.py -i /path/to/align/dir -o name_of_out.csv

Author:
    James Matsumura
"""

import argparse
from collections import defaultdict
import os
import re

def main():

    parser = argparse.ArgumentParser(description='Script to assess alignment results from align_two_sequence_files.py.')
    parser.add_argument('-i', type=str, required=True, help='Path of the base alignment result directory from align_two_sequence_files.py.')
    parser.add_argument('-o', type=str, required=True, help='Name of the CSV file to generate.')
    args = parser.parse_args()

    best_ids = defaultdict(lambda : 0) # map the best ID to each individual locus

    for path,subdirs,files in os.walk(args.i):
        for name in files:
            if name.endswith('align.txt'):

                file_path = "{}/{}".format(path,name)
                locus = file_path.split('/')[-2]

                with open(file_path,'r') as alignment:
                    content = alignment.read()
                    percent_id = float(re.search(r'Identity:\s+\d+/\d+\s\(\s*(\d+\.\d)%\)\n',content).group(1))
                    if percent_id > best_ids[locus]:
                        best_ids[locus] = percent_id

    with open(args.o,'w') as outfile:

        outfile.write("locus_name,percent_id\n")

        for k in sorted(best_ids):
            outfile.write("{},{}\n".format(k,best_ids[k]))

if __name__ == '__main__':
    main()


# -*- coding: utf-8 -*-
# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.4.1
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd
students = pd.read_csv('stud_math.csv')
def quantiles_info(columns):
    median = columns.median()
    IQR = columns.quantile(0.75) - columns.quantile(0.25)
    perc25 = columns.quantile(0.25)
    perc75 = columns.quantile(0.75)
    minimum = columns.min()
    maximum = columns.max()
    print('Минимальное значение: {},'.format(minimum),'\nМаксимальное значение: {},'.format(maximum),
          "\nГраницы выбросов: [{f}, {l}],".format(f=perc25 - 1.5*IQR, l=perc75 + 1.5*IQR),'\n25-й перцентиль: {},'.format(perc25), '75-й перцентиль: {},'.format(perc75)
          , "IQR: {}, ".format(IQR),)
    if minimum < (perc25 - 1.5*IQR) | maximum > (perc75 + 1.5*IQR):
        print ('Есть выбросы')

def missing_values_table(df):
    # Total missing values
    mis_val = df.isnull().sum()

    # Percentage of missing values
    mis_val_percent = 100 * df.isnull().sum() / len(df)

    # Make a table with the results
    mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)

    # Rename the columns
    mis_val_table_ren_columns = mis_val_table.rename(
    columns = {0 : 'Отсутствующие значения', 1 : '% от общего кол-ва '})

    # Sort the table by percentage of missing descending
    mis_val_table_ren_columns = mis_val_table_ren_columns[mis_val_table_ren_columns.iloc[:,1] != 0].sort_values('% от общего кол-ва ', ascending=False).round(1)

    # Print some summary information
    print ("В датафрейме содержится " + str(df.shape[1]) + " колонок.\n" "В " + str(mis_val_table_ren_columns.shape[0]) +" колонках есть отсутствующие значения.")

    # Return the dataframe with missing information
    return mis_val_table_ren_columns

def replace_nan_with_most_frequent(title):
    most_frequent = students[title].value_counts().index[0]
    students[title] = students[title].apply(lambda x: most_frequent if pd.isna(x) else x)

def replace_nan_with_value(title, value):
    students[title] = students[title].apply(lambda x: value if pd.isna(x) else x)

def replace_na_with_median():
    nan_columns = list(students.columns[students.isnull().sum() > 0])
    if nan_columns:
        for nan_column in nan_columns:
            students[nan_column+'_isNAN'] = pd.isna(students[nan_column]).astype('uint8')
        students.fillna(data.median(), inplace=True)

def get_stat_dif(column):
    cols = students.loc[:, column].value_counts().index
    combinations_all = list(combinations(cols, 2))
    for comb in combinations_all:
        if ttest_ind(students.loc[students.loc[:, column] == comb[0], 'score'],
                        students.loc[students.loc[:, column] == comb[1], 'score']).pvalue \
            <= 0.05/len(combinations_all): # Учли поправку Бонферони
            print('Найдены статистически значимые различия для колонки', column)
            break

def get_boxplot(column):
    fig, ax = plt.subplots(figsize = (14, 4))
    sns.boxplot(x=column, y='score', data=students, ax=ax)
    plt.xticks(rotation=45)
    ax.set_title('Boxplot for ' + column)
    plt.show()
# -


def get_res(N, arr, aim):
    dic = {i: aim - i for i in arr}
    mid = int(aim // 2)
    # res = []
    f = False
    for i in range(1, mid + 1):
        if i in arr and aim - i in dic:
            # res.append([i, aim - i])
            f = True
            print(i, aim - i)
    # if res:
    #     for i in res:
    #         # print(" ".join([str(j) for j in i]))
    #         print(i[0], i[1])
    if not f:
        print("NO")

if __name__ == "__main__":
    N = int(input())
    arr = [int(i) for i in input().split()]
    aim = int(input())
    get_res(N, arr, aim)

# numCup = int(input())
# doseB = list(map(int, input().split()))
# doseNeed = int(input())
# doseB.sort()
# res_all = []
# start = 0
# end = numCup-1
# while start < end:
#     a = doseB[start]
#     b = doseB[end]
#     if a+b == doseNeed:
#         res_all.append([a,b])
#     if a+b > doseNeed:
#         end -= 1
#     else:
#         start +=1
# if len(res_all) == 0:
#     print('NO')
# else:
#     for item in res_all:
#         print('%s %s'%(item[0],item[1]))


"""
Serializers allow complex data such as querysets and model instances to be converted to
native Python datatypes that can then be easily rendered into JSON, XML or other content types.
"""

from rest_framework.serializers import ModelSerializer
from .models import Article

class ArticleSerializer(ModelSerializer):
    '''By using a ModelSerializer, we get by default create and update methods for free.
       Also, a set of default validators are automatically populated.'''

    class Meta:
        model  = Article
        fields = ('id',
                 'title',
                 'body',
                 'author_id'
                 )


from django.conf.urls import url
from . import views
from django.views.generic import TemplateView

urlpatterns = [
    url(r'search/$', views.search, name="search"),
    url(r'^categorias/(?P<nombre_categoria>[A-Za-z\-]+)/$', views.categoria_view, name="categorias"),
    url(r'^articulo/(?P<nombre_producto>[A-Za-z\-]+)/$', views.producto_details, name="producto_details"),
    url(r'^cesta/$', views.ver_cesta, name="ver_cesta"),
    url(r'^cesta/agregar/(?P<id_producto>[\d]+)/$', views.agregar_producto, name="agregar_producto"),
    url(r'^cesta/remover_producto/$', views.remover_de_cesta, name="remover_de_cesta"),
    url(r'^checkout/$', views.checkout, name='checkout'),
    url(r'^fin/$', TemplateView.as_view(template_name='tienda/fin.html'), name='fin'),
    url(r'^cuenta/$', views.cuenta, name="cuenta")
]


"""
Performs Complex Scaling on the Hamiltonian of a Hydrogen Atom. Confirms the Aguilar–Balslev–Combes Theorem
"""

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

#Grid Parameters
theta = np.pi/4.0
g = np.linspace(0,20,501)[1:]
r   = j3DR.Grid(g, theta)
r_0 = j3DR.Grid(g, 0.0)

#System Parameters
ne      = 1
nstates = 20

w = 3.5
vext       = -1./ r.x
vext_theta = -1./r.complex
#vext  =  (1/2 * r.x**2 - w) * np.exp(-0.1 * r.x**2)

eig = j3DR.CSEigenSolver(r, nstates)

energy,       psi,       density        =  j3DR.scf(ne, nstates, r_0, vext,       eig)
energy_theta, psi_theta, density_theta  =  j3DR.scf(ne, nstates, r ,  vext_theta, eig)

print("Total Energy", energy[0])

#Plot external potential and density
plt.figure(figsize=(12, 3), dpi=300)
plt.plot(r.x, vext,      label="Unrotated $V_{ext}$")
plt.plot(r.x, vext_theta, label="Electron Density" )
plt.legend()
plt.savefig("External_Potentials.png", dpi=300)

plt.figure(figsize=(12, 3), dpi=300)
plt.plot(r.x, density,      label="Unrotated Density")
plt.plot(r.x, density_theta, label="Rotated Density" )
plt.legend()
plt.savefig("Densities.png", dpi=300)

if np.isclose(energy[0], energy_theta[0].real, atol=1e-3):
    print("The act of complex scaling leaves bound energies unchanged!")


from flask import Flask, render_template, request

import json
import urllib.request
import gzip
import datetime

app = Flask(__name__)

@app.route('/', defaults={'date': None})
@app.route('/<date>')
def show_scoreboard(date):
    if date is None:
        date = datetime.datetime.today()
    else:
        date = datetime.datetime.strptime(date, '%Y%m%d')

    dates = [date + datetime.timedelta(days=i) for i in range(-2, 3)]
    dates = [date.strftime('%Y%m%d') for date in dates]
    current_date = date.strftime('%Y%m%d')
    date = date.strftime('%Y%m%d')

    json = get_scoreboard_json(date)

    return render_template('scoreboard.html', dates=dates, games=json['games'])

@app.route('/boxscore/<date>/<gameid>', defaults={'layout': None})
@app.route('/boxscore/<date>/<gameid>/<layout>')
def show_boxscore(date, gameid, layout):
    boxscore = get_boxscore_json(date, gameid)

    if 'stats' not in boxscore:
        return 'Game has not started'

    players_json = get_players_json(boxscore['basicGameData']['seasonYear'])

    for player in boxscore['stats']['activePlayers']:
        for record in players_json['league']['standard']:
            if record['personId'] == player['personId']:
                player['playerName'] = record['firstName'] + ' ' + record['lastName']
                player['pos'] = record['pos']

    if layout is None and 'Android' in request.headers['User-Agent']:
        layout = 'compact'
    elif layout is None:
        layout = 'full'

    return render_template('boxscore.html', boxscore=boxscore, layout=layout)

@app.route('/standings')
def show_standings():
    date = datetime.datetime.today().strftime('%Y%m%d')

    standings = get_standings_json(date)
    teams = get_teams_json(standings['league']['standard']['seasonYear'])
    teams = {team['teamId']: team['tricode'] for team in teams['league']['standard']}

    return render_template('standings.html', standings=standings, teams=teams)

@app.route('/schedule/<year>/<teamid>')
def show_schedule(year, teamid):
    schedule = get_schedule_json(year, teamid)
    teams = get_teams_json(year)

    return render_template('schedule.html', schedule=schedule, teamid=teamid, teams=teams)

def get_boxscore_json(date, gameid):
    url = 'https://data.nba.net/prod/v1/{}/{}_boxscore.json'.format(date, gameid)
    return request_and_decode(url)

def get_players_json(year):
    url = 'http://data.nba.net/data/10s/prod/v1/{}/players.json'.format(year)
    return request_and_decode(url)

def get_teams_json(year):
    url = 'http://data.nba.net/data/10s/prod/v1/{}/teams.json'.format(year)
    return request_and_decode(url)

def get_scoreboard_json(date):
    url = 'http://data.nba.net/data/10s/prod/v1/{}/scoreboard.json'.format(date)
    return request_and_decode(url)

def get_standings_json(date):
    url = 'http://data.nba.net/data/10s/prod/v1/{}/standings_conference.json'.format(date)
    return request_and_decode(url)

def get_schedule_json(year, teamid):
    url = 'http://data.nba.net/data/10s/prod/v1/{}/teams/{}/schedule.json'.format(year, teamid)
    return request_and_decode(url)

def request_and_decode(url):
    headers = {
        "Accept-Encoding": "gzip, deflate",
        "Accept-Language": "en-US",
        "Accept": "*/*",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",
        "Referer": "http://stats.nba.com/scores/",
        "Connection": "keep-alive",
        "Cache-Control": "no-cache",
        "Origin": "http://stats.nba.com",
    }

    req = urllib.request.Request(url, headers=headers)
    resp_bytes = gzip.decompress(urllib.request.urlopen(req).read())
    return json.loads(resp_bytes.decode('utf-8'))


import sys
import os
import data as dt
from timeit import default_timer as timer
import net as net
import numpy as np
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch
import smtplib
import math

gpu = True
if gpu:
    torch.cuda.init()

def write_all(data, path, file, measure, type):
    if path != '':
        element = os.listdir(path)
    else:
        element = ['plot']
    if 'plot' not in element:
        os.system('mkdir plot')
    if measure == 'loss':
        fout = open('plot/'+file, 'w')
        fout.write('LOSS FUNCTION\n')
        for i in range(len(data)):
            fout.write(str(data[i])+" ")
        fout.write('\n')
        fout.close()
    elif measure == 'accuracy':
        fout = open('plot/'+file, 'a')
        if type == 'val':
            fout.write('ACCURACY VALIDATION\n')
        elif type == 'test':
            fout.write('ACCURACY TEST\n')
        fout.write(str(data)+" ")
        fout.write('\n')
        fout.close()
    elif measure == 'error':
        fout = open('plot/'+file, 'a')
        if type == 'val':
            fout.write('ERROR VALIDATION\n')
        elif type == 'test':
            fout.write('ERROR TEST\n')
        for j in range(len(data)):
            fout.write(str(data[j])+" ")
            fout.write('\n')
        fout.write('\n')
        fout.close()

def save_model(model, path):
    torch.save(model.state_dict(), path)

def load_model(path_model, input_size, output_size, gpu):
    model = net.Network(input_size, output_size, gpu)
    model.load_state_dict(torch.load(path_model))
    return model

def read_params(path):
    verbose = True
    gpu = True
    path = './sorted/unfix/'
    f = 't0Bnet.txt'
    m = 't0Bnet.pt'
    params = sys.argv
    if len(params) > 1:
        for i in range(len(params)):
            if params[i] == "--path":
                if len(params) > i+1:
                    path = params[i+1]
                else:
                    usage(True)
                    sys.exit()
            elif params[i] == "--v":
                verbose = True
            elif params[i] == "--help" or params[i] == "-h":
                usage(False)
                sys.exit()
            elif params[i] == "--gpu":
                gpu = True
    file = params[0].split('.')
    if file[1] == 'py':
        name = file[0].split('/')
        f = name[-1]+".txt"
        m = name[-1]+".pt"
    return path, verbose, f, m, gpu

def main(sourcep):
    # read all arguments
    path, verbose, file, modelf, gpu = read_params(sourcep)
    if verbose:
        print('###\tStart reading data')
    start = timer()
    train, validation, test = dt.read_data(path)
    end = timer()
    if verbose:
        print('###\tTime for reading data: %.2f sec' % (end - start))
    return train, validation, test, verbose, path, file, modelf, gpu

# order to execute
path = './sorted/unfix/'
path_model = './plot/'
train, validation, test, verbose, path, file, modelf, gpu = main(path)
gpu = True
training = True
n_epochs = len(train)
n_iters = train[0].n_item
input_size = 400
output_size = 1
lr = 0.005
cycle = 5

if training:
    if verbose:
        print('###\tCreate the net')

    # create the net
    network = net.Network(input_size, output_size, gpu)
    if gpu:
        network.cuda()
    criterion = nn.L1Loss()
    optimizer = optim.SGD(network.parameters(), lr)

    if verbose:
        print('###\tStart training')
        start = timer()

    losses = np.zeros(n_epochs)
    hid = network.initHidden()
    for k in range(cycle):
        for epoch in range(n_epochs):
            input = []
            target = []
            for i in range(int(input_size/2)):
                if i < train[epoch].n_item:
                    input.append(train[epoch].items[i][0])
                    input.append(train[epoch].items[i][1])
                else:
                    input.append(2)
                    input.append(-1)
            if gpu:
                inputs = torch.cuda.FloatTensor([input])
            else:
                inputs = torch.Tensor([input])
            target = [float(train[epoch].z_opt/train[epoch].ub_dan)]
            if gpu:
                targets = torch.cuda.FloatTensor([target])
            else:
                targets = torch.Tensor([target])
            outputs = network(inputs)  # for NN
            optimizer.zero_grad()
            loss = criterion(outputs, targets)

            loss.backward(retain_graph=True)
            optimizer.step()
            losses[epoch] += loss.item()

    if verbose:
        end = timer()
        print('###\tTime for training net: %.2f sec' % (end - start))

    if verbose:
        print('###\tStart saving model')
        start = timer()

    save_model(network, modelf)

    if verbose:
        end = timer()
        print('###\tTime for save model: %.2f sec' % (end - start))

    write_all(losses, '', file, 'loss', '')
else:
    if verbose:
        print('###\tStart loading model')
        start = timer()

    network = load_model(path_model+modelf, input_size, output_size, gpu)
    if gpu:
        network.cuda()

    if verbose:
        end = timer()
        print('###\tTime for load model: %.2f sec' % (end - start))

if verbose:
    print('###\tStart validation')
    start = timer()

network.eval()
n_epochs = len(validation)
error = [0, 0, 0]
accuracy = 0
n_iters = validation[0].n_item
for epoch in range(n_epochs):
    input = []
    targets = []
    for i in range(int(input_size/2)):
            if i < validation[epoch].n_item:
                input.append(validation[epoch].items[i][0])
                input.append(validation[epoch].items[i][1])
            else:
                input.append(0)
                input.append(0)
    if gpu:
        inputs = torch.cuda.FloatTensor([input])
    else:
        inputs = torch.Tensor([input])
    target = [float(validation[epoch].z_opt/validation[epoch].ub_dan)]
    if gpu:
        targets = torch.cuda.FloatTensor([target])
    else:
        targets = torch.Tensor([target])
    outputs = network(inputs)  # for NN
    if (outputs[0]-targets[0])/targets[0] < error[0]:
        error[0] = float((outputs[0]-targets[0])/targets[0])
    elif (outputs[0]-targets[0])/targets[0] > error[1]:
        error[1] = float((outputs[0]-targets[0])/targets[0])
    error[2] = error[2] + abs(float((outputs[0]-targets[0])/targets[0]))
    if outputs[0] == targets[0]:
        accuracy = accuracy + 1
accuracy = accuracy/len(validation)
error[2] = error[2]/len(validation)
if verbose:
    end = timer()
    print('###\tTime for validation net: %.2f sec' % (end - start))

write_all(accuracy, '', file, 'accuracy', 'val')
write_all(error, '', file, 'error', 'val')

if verbose:
    print('###\tStart testing')
    start = timer()

network.eval()
n_epochs = len(test)
error = [0, 0, 0]
accuracy = 0
n_iters = test[0].n_item
for epoch in range(n_epochs):
    input = []
    targets = []
    for i in range(int(input_size/2)):
            if i < test[epoch].n_item:
                input.append(test[epoch].items[i][0])
                input.append(test[epoch].items[i][1])
            else:
                input.append(2)
                input.append(-1)
    if gpu:
        inputs = torch.cuda.FloatTensor([input])
    else:
        inputs = torch.Tensor([input])
    target = [float(test[epoch].z_opt/test[epoch].ub_dan)]
    if gpu:
        targets = torch.cuda.FloatTensor([target])
    else:
        targets = torch.Tensor([target])
    outputs = network(inputs)  # for NN
    if (outputs[0]-targets[0])/targets[0] < error[0]:
        error[0] = float((outputs[0]-targets[0])/targets[0])
    elif (outputs[0]-targets[0])/targets[0] > error[1]:
        error[1] = float((outputs[0]-targets[0])/targets[0])
    error[2] = error[2] + abs(float((outputs[0]-targets[0])/targets[0]))
    if outputs[0] == targets[0]:
        accuracy = accuracy + 1
accuracy = accuracy/len(test)
error[2] = error[2]/len(test)

if verbose:
    end = timer()
    print('###\tTime for testing net: %.2f sec' % (end - start))

write_all(accuracy, '', file, 'accuracy', 'test')
write_all(error, '', file, 'error', 'test')


from typing import Dict, List, Set, Tuple

def reallocate(memory_allocation: List[int]) -> None:
    """
    Computes the next reallocation cycle by round-robin redistributing the first, most full memory bank to all memory
    banks.

    :param memory_allocation: Tuple[int, ...] Current memory allocation state
    """
    bank_to_reallocate = memory_allocation.index(max(memory_allocation))
    blocks_to_reallocate = memory_allocation[bank_to_reallocate]
    memory_allocation[bank_to_reallocate] = 0
    for _ in range(blocks_to_reallocate):
        bank_to_reallocate = (bank_to_reallocate + 1) % len(memory_allocation)
        memory_allocation[bank_to_reallocate] += 1

already_seen_configurations: Set[Tuple[int, ...]] = set()

with open('memory_banks.txt') as input_file_desc:
    bank_allocation = [int(blocks) for blocks in input_file_desc.readline().strip().split()]

while tuple(bank_allocation) not in already_seen_configurations:
    already_seen_configurations.add(tuple(bank_allocation))
    reallocate(bank_allocation)

print(len(already_seen_configurations))

# Part Two

past_configurations: Dict[Tuple[int, ...], int] = {}

with open('memory_banks.txt') as input_file_desc:
    bank_allocation = [int(blocks) for blocks in input_file_desc.readline().strip().split()]

step = 0

while past_configurations.get(tuple(bank_allocation)) is None:
    past_configurations[tuple(bank_allocation)] = step
    reallocate(bank_allocation)
    step += 1

print(step - past_configurations[tuple(bank_allocation)])


import textwrap

import clippers

def test_choice_prompt(capsys):
    # Given a prompt and list of choices the user should see the prompt
    # followed by a list of choices with a check box beside each choice.
    prompt = "Which house do you belong to?"
    choices = ["Gryffindor", "Slytherin", "Ravenclaw", "Hufflepuff"]
    expected_out = textwrap.dedent(
        """\
        Which house do you belong to?
        [x] Gryffindor
        [ ] Slytherin
        [ ] Ravenclaw
        [ ] Hufflepuff"""
    )
    clippers.choice_prompt(prompt, choices)
    captured = capsys.readouterr()
    assert captured.out == expected_out


class Morph:

    def __init__(self, surface, base, pos, pos1):
        self.surface = surface
        self.base = base
        self.pos = pos
        self.pos1 = pos1

    def is_end_of_sentence(self) -> bool: return self.pos1 == '句点'

    def __str__(self) -> str: return 'surface: {}, base: {}, pos: {}, pos1: {}'.format(self.surface, self.base, self.pos, self.pos1)

def make_morph_list(analyzed_file_name: str) -> list:

    sentences = []
    sentence = []

    with open(analyzed_file_name, encoding='utf-8') as input_file:
        for line in input_file:
            line_list = line.split()
            if (line_list[0] == '*') | (line_list[0] == 'EOS'):
                pass
            else:
                line_list = line_list[0].split(',') + line_list[1].split(',')
                # この時点でline_list
                # ['始め', '名詞', '副詞可能', '*', '*', '*', '*', '始め', 'ハジメ', 'ハジメ']
                _morph = Morph(surface=line_list[0], base=line_list[7], pos=line_list[1], pos1=line_list[2])

                sentence.append(_morph)

                if _morph.is_end_of_sentence():
                    sentences.append(sentence)
                    sentence = []

    return sentences

morphed_sentences = make_morph_list('neko.txt.cabocha')

# 3文目の形態素列を表示
for morph in morphed_sentences[2]:
    print(str(morph))


from unittest import TestCase
from gitlint.git import GitContext

import os

class BaseTestCase(TestCase):
    # In case of assert failures, print the full error message
    maxDiff = None

    @staticmethod
    def get_sample_path(filename=""):
        samples_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "samples")
        return os.path.join(samples_dir, filename)

    @staticmethod
    def get_sample(filename=""):
        sample_path = BaseTestCase.get_sample_path(filename)
        sample = open(sample_path).read()
        return sample

    @staticmethod
    def gitcontext(commit_msg_str, changed_files=None):
        """ Utility method to easily create gitcontext objects based on a given commit msg string and set of
        changed files"""
        gitcontext = GitContext()
        gitcontext.set_commit_msg(commit_msg_str)
        if changed_files:
            gitcontext.changed_files = changed_files
        else:
            gitcontext.changed_files = []
        return gitcontext


''' Life takes place on a two dimensional grid made up various tiles. At any
point of time the tile can either be alive or dead. Each tile has 8 other tiles
surrounding it :
1 2 3
4 x 5
6 7 8
The rules whether a cell can be alive or dead in the next time step depends on the
following:
1)If the cell is dead:
Birth: if exactly three of its neighbours are alive, the cell will become alive at the next step.
2)If the cell is already alive
Survival: if the cell has two or three live neighbours, the cell remains alive.
Otherwise, the cell will die
3)Death by loneliness: if the cell has only zero or one live neighbours,
the cell will become dead at the next step.
4)Death by overcrowding: if the cell alive and has more than three live neighbours,
the cell also dies.'''

''' A turtle-based version of Conway's Game of Life.
An empty board will be displayed, and the following commands are available:
E : Erase the board
R : Fill the board randomly
S : Step for a single generation
C : Update continuously until a key is struck
Q : Quit
Cursor keys :  Move the cursor around the board
Space or Enter : Toggle the contents of the cursor's position
'''

import sys
import turtle
import random

cell_size=10

class GameBoard():
    '''creates a game tile
    Attributes:
    xsize & ysize as the horizonal and vertical size of the game board
    state: set containing (x,y) coordinates for live cells

    Methods:
    display(update_board)----display the state of the board on the screen
    erase()---clear the entire board
    makeRandom()---fill the board randomly
    set(x,y)--- set a given tive to live tile; doesn't refresh the board
    toggle(x,y)---change the given cell to live to dead; or vice versa;
    also refreshes the screen'''

    def _init_(self,xsize,ysize):
        '''creates a gameboard instance
        scr---curses screen object to use for display
        char---character used to render live cells default('*')
        '''
        self.state=set()
        self.xsize=xsize
        self.ysize=ysize

    def is_acceptable(self,x,y):
        '''Returns true if x,y coordinates are acceptable for the board'''
        return(0<=x<self.xsize) and (0<=y<self.ysize)

    def set(self,x,y):
        '''set a tile to a live state'''
        if not self.is_acceptable(x,y):
            raise ValueError("coordinates{},{} out of range 0..{}, 0..{}".format(x, y, self.xsize, self.ysize))

        key = (x,y)
        self.state.add(key)

    def makeRandom(self):
        '''Fill the board with a random pattern'''
        self.erase()
        for i in range(0,self.xsize):
            for j in range(0,self.ysize):
                if random.random()>0.5:#Return the next random floating point number in the range [0.0, 1.0]
                    self.set(i,j)

    def toggle(self,x,y):
        '''Toggle a cell's state between live and dead'''
        if not self.is_acceptable(x,y):
            raise ValueError("coordinates {},{} out of range 0..{},0..{}".format(x,y,self.xsize,self.ysize))
        key=(x,y)
        if key in self.state:
            self.state.remove(key)
        else:
            self.state.add(key)

    def erase(self):
        '''clear the entire gameboard'''
        self.state.clear()

    def step(self):
        '''compute one generation with updating the display'''
        d=set()
        for i in range(self.xsize):
            x_range=range(max(0,i-1),min(self.xsize,i+2))
            for j in range(self.ysize):
                s=0
                live=((i,j) in self.state)
                for yp in range(max(0,j-1),min(self.state,j+2)):
                    for xp in x_range:
                        if (xp,yp)in self.state:
                            s+=1
                s-=live
                if s==3:
                    d.add((i,j))
                elif s==2 and live:
                    d.add((i,j))
                elif live:
                    pass
        self.state=d

    def Draw(self,x,y):
        '''update the cell (x,y) on the display i.e the new generation'''
        turtle.penup()
        key=(x,y)
        if key in self.state:
            turtle.setpos(x*cell_size,y*cell_size)
            turtle.color('black')
            turtle.pendown()
            turtle.setheading(0)
            turtle.begin_fill()
            for i in range(4):
                turtle.forward(cell_size-1)
                turtle.left(90)
            turtle.end_fill()

    def display(self):
        '''Draw the whole Board'''
        turtle.clear()
        for i in range(self.xsize):
            for j in range(self.ysize):
                self.draw(i,j)
        turtle.update()

    def display_help_window(self):
        ''' this is used to show the help dialogue'''
        from turtle import TK
        root=TK.tk()
        frame=TK.Frame()
        canvas=TK.Canvas(root,width=300,height=200,bg='white')
        canvas.pack()
        help_screen=turtle.TurtleScreen(canvas)
        help_t=turtle.RawTurtle(help_screen)
        help_t.penup()
        help_t.hideturtle()
        help_t.speed('fastest')

        width,height=help_screen.screensize()
        line_height=20
        y=height//2-30
        for s in ("Click on cells to make them alive or dead.",
                  "Keyboard commands:",
                  " E)rase the board",
                  " R)andom fill",
                  " S)tep once or",
                  " C)ontinuously -- use 'S' to resume stepping",
                  " Q)uit"):
             help_t.setpos(-(width/2),y)
             help_t.write(s,font=('sans serif',14,'normal'))
             y-=line_height

    def main():

        scr = turtle.Screen()
        turtle.mode('standard')
        xsize= scr.screensize()
        ysize = scr.screensize()

        turtle.setworldcoordinates(0, 0, xsize, ysize)
        board=GameBoard(xsize//cell_size,1+ysize//cell_size)
        board. display_help_window()

        turtle.hideturtle()
        turtle.speed('fastest')
        turtle.tracer(0,0)
        turtle.penup()

        def toggle(x,y):
            cell_x=x//cell_size
            cell_y=y//cell_size
            if board.is_acceptable(cell_x,cell_y):
                board.toggle(cell_x,cell_y)
                board.display()

        turtle.onscreenclick(turtle.listen)
        turtle.onscreenclick(toggle)

        board.makeRandom()
        board.display()

        def erase():
            '''used to designate the'e' key to erase the gameboard'''
            board.erase()
            board.display()
        turtle.onkey(erase,'e')

        def makeRandom():
            '''used to designate the'r' key to randomly fill the gameboard'''
            board.makeRandom()
            board.display()
        turtle.onkey(makeRandom,'r')

        turtle.onkey(sys.exit,'q')

        #continuous=False
        def step_once():
            #nonlocal continuous
            continuous=False
            perform_step()

        def step_continous():
            #nonlocal continuous
            continuous=True
            perform_step()

        def perform_step():
            board.step()
            board.display()
            if continuous:
                turtle.ontimer(perform_step,25)

        turtle.onkey(step_once,'s')
        turtle.onkey(step_continous,'c')

        turtle.listen()
        turtle.mainloop()

    #if __name__ == '__main__':
        #main()

Sim lab sem 6/finals/Process Simulation Lab Sem 6/game_of_life_python.py


import numpy as np

vierkant = np.array([5, 0, 0,
                   0, 0, 4,
                   0, 0, 6])

x1 = vierkant[0]
x2 = vierkant[1]
x3 = vierkant[2]
x4 = vierkant[3]
x5 = vierkant[4]
x6 = vierkant[5]
x7 = vierkant[6]
x8 = vierkant[7]
x9 = vierkant[8]
t = -1

regels = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
laatste_regel = [3*4]

Matrix = []
for x in regels:
    rij = []
    result = 0
    for count in range(9):
        if count in x:
            if vierkant[count] != 0:
                rij.append(0)
            else:
                rij.append(1)
        else:
            rij.append(0)
    rij.append(t)
    Matrix.append(rij)

print(Matrix)

vector_b = []
for x in regels:
    waarde = []
    for getal in x:
        waarde.append(vierkant[getal])
    vector_b.append(-1*sum(waarde))
print(vector_b)

# print(np.shape(Matrix)[0])
# # adding the last row manual
# last_row = [0 for x in range(len(vierkant))]
# last_row.append(-1)
# if vierkant[4] !=0:
#     last_row[4] = 0
#     vector_b.append(0 - (3 * vierkant[4]))
# else:
#     last_row[4] = 3
#     vector_b.append(0)
# Matrix.append(last_row)

# convert the lists into numpy arrays for processing
matrix_A = np.array(Matrix)
# vector_b = np.transpose(np.array([vector_b]))
vector_b = np.array(vector_b)

# print the lists
print("Matrix A:")
print(matrix_A)
print("\nDe vector b van de resultaat:")
print(vector_b)

def delete_column(chosen_matrix):
    """Checks for an empty column and deletes one"""

    chosen_matrix = list(chosen_matrix)
    col_number = 0

    #
    for col in range(0, len(chosen_matrix[0])):
        is_zero_column = True
        for row in range(0, len(chosen_matrix)):
            if chosen_matrix[row][col_number] != 0:
                is_zero_column = False
                break
        # the column is a zero-column
        if is_zero_column:
            chosen_matrix = np.array(chosen_matrix)
            chosen_col = [x for x in range(0, len(chosen_matrix) + 1) if x != col_number]
            return chosen_matrix[:, chosen_col], col_number

        # search the next column
        col_number += 1

# printing the shape of the matrix
print("Shape of the matrix before erase:\n{0}\n".format(np.shape(matrix_A)))

# erase a column and return the new values into their respective variables
changed_matrix = delete_column(matrix_A)
matrix_B = changed_matrix[0]
deleted_col = changed_matrix[1]
print("Shape of matrix after erase:\n{0}".format(np.shape(matrix_B)))
# calculate the pseudo-inverse of the matrix
pseudo_inverse = np.linalg.pinv(matrix_B)

# take the dot product of the pseudo inverse and the result vector
solve_vector = np.dot(pseudo_inverse, vector_b)
solve_vector = list(solve_vector)

# add the deleted column back
solve_vector.insert(deleted_col, 0)

# round all the numbers to .1 accuracy
for x in range(0, len(solve_vector)):
    solve_vector[x] = round(solve_vector[x], 2)

# save the total sum number
total_number = solve_vector.pop()

# reshape the vector as a n x n matrix
solve_vector = np.reshape(solve_vector, (3, 3))

check_square = list([[5, 0, 0,],[0, 0, 4],[0, 0, 6]])
solved_square = []
for row in range(0, len(check_square)):
    for num in range(0, len(check_square[0])):
        if check_square[row][num] > 0:
            solved_square.append(check_square[row][num])
        else:
            solved_square.append(solve_vector[row][num])

# reshape the square and print all the info
solved_square = np.reshape(solved_square, (3, 3))
print("The solved square:\n{0}\n".format(solved_square))
print("The side sum is: {0}".format(total_number))


"""
Few functions for first program

Created by akaish at 21.03.2019
"""

def hello():
    username = input("What's your name, friend? :")
    print('Hello, {0}!'.format(username))

A_LESS_THEN_B = 1
B_LESS_THEN_A = 2
A_AND_B_EQUAL = 3

def compare_two_integers(a, b):
    if a > b :
        return B_LESS_THEN_A
    elif a < b :
        return A_LESS_THEN_B
    else :
        return A_AND_B_EQUAL

def lambda_test() :
    print("Lambda test: ")
    print("Print operators defined in for_import.lambda_test()")
    print("Lambda set to L_TEST variable (L_TEST = lambda : lambda_test()) ")
    print("And passed to another function as parameter")

L_TEST = lambda : lambda_test()

def lambda_executor_function(lambda_function) :
    lambda_function()


'''
E)
This case fail to represent the posterior because the trace never
leaves the area around 0.
The reason is the small proposal SD and the fact that there is
a boundary between the modes of the prior distribution.

F)
Same issue. Change starting point of the Metropolis algorithm
to 0.99 to see the same behavior from the other side of the
multi-modal prior distribution.
'''
import itertools

from scipy import stats
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

def acf(trace, lag):
    '''
    Autocorrelation function, based on Pearson correlation
    coefficient.
    '''
    correlation, _ = stats.stats.pearsonr(trace[:-lag], trace[lag:])
    return correlation

def ess(trace):
    '''
    Effective sample size, as defined in Kruschke's "Doing
    Bayesian Data Analysis", 2nd edition, p. 184.
    '''
    significant_autocorrelations = []
    for lag in itertools.count(start=1):
        autocorrelation = acf(trace, lag)
        significant_autocorrelations.append(autocorrelation)
        if autocorrelation < 0.05:
            break
    N = len(trace)
    denominator = 1 + 2 * sum(significant_autocorrelations)
    return N / denominator

def acceptance_ratio(trace, min_diff=0.0001):
    diffs = np.array(trace[1:]) - np.array(trace[:-1])
    abs_diff = np.abs(diffs)
    not_accepted_steps = abs_diff < min_diff
    return 1 - sum(not_accepted_steps) / (len(trace) - 1)

# Specify the data, to be used in the likelihood function
data = [0, 1, 1]

def likelihood(theta, data):
    '''Bernoulli likelihood function'''
    if theta > 1 or theta < 0:
        return 0
    z = sum(data)
    N = len(data)
    return theta ** z * (1 - theta) ** (N - z)

# Define the prior density function
def prior(theta):
    return (np.cos(4 * np.pi * theta) + 1) ** 2 / 1.5

# Define the relative probability of the target distribution,
# as a function of vector theta. For our application, this
# target distribution is the unnormalized posterior distribution.
def target_relative_probability(theta, data):
    return likelihood(theta, data) * prior(theta)

trace_length = 50000  # The number of jumps to try
burn_in = int(.0 * trace_length)

def metropolis(target_prob, proposal_sd, starting_point, trace_length):
    '''
    Run a simple Metrolopis MCMC algorithm with normal deviated
    proposal.
    '''
    trace = [starting_point]
    while len(trace) < trace_length:
        current_position = trace[-1]
        proposed_jump = np.random.normal(0, proposal_sd)
        proposed_position = current_position + proposed_jump
        current_prob = target_prob(current_position, data)
        proposed_prob = target_prob(proposed_position, data)
        acceptance_prob = min(1, proposed_prob / current_prob)
        if np.random.uniform() < acceptance_prob:
            next_position = proposed_position
        else:
            next_position = current_position
        trace.append(next_position)
    return trace

# Specify standard deviation of proposal distribution
proposal_sd = [0.02, 0.2, 2][0]
trace = metropolis(target_relative_probability, proposal_sd,
                   starting_point=.99, trace_length=trace_length)
# Extract the post-burn-in portion of the trace
accepted_trace = trace[burn_in:]
effective_size = ess(accepted_trace)
acc_ratio = acceptance_ratio(accepted_trace)

plt.subplot(3, 1, 1)
title_template = 'Prpsl.SD == {}; Eff.Sz == {:.1f}'
plt.title(title_template.format(proposal_sd, effective_size))
sns.distplot(accepted_trace)
plt.xlabel('theta')

plt.subplot(3, 1, 2)
plt.title('End of chain')
plt.plot(trace[-100:], range(100))
plt.text(.01, 80, 'Acceptance ratio == {:.3f}'.format(acc_ratio))
plt.xlim([0, 1])
plt.gca().set_yticklabels([])
plt.xlabel('theta')

plt.subplot(3, 1, 3)
plt.title('Beginning of chain')
plt.plot(trace[:100], range(100))
plt.xlim([0, 1])
plt.gca().set_yticklabels([])
plt.tight_layout()
plt.xlabel('theta')

plt.show()


# coding: utf-8

from django.db.models import BooleanField
from django.forms import ModelForm, RadioSelect
from crispy_forms.helper import FormHelper

class InventoryForm(ModelForm):
    """
    Abstract base class for inventory form.
    """
    def __init__(self, *args, **kwargs):
        read_only = False
        if 'read_only' in kwargs:
            read_only = kwargs.pop('read_only')
        ModelForm.__init__(self, *args, **kwargs)
        self.helper = FormHelper(self)
        self.helper.form_tag = False
        for f in self.fields:
            if isinstance(self.Meta.model._meta.get_field(f), BooleanField):
                self.fields[f].widget = RadioSelect()
        if read_only:
            # Read only form - Code from django snippets:
            # "https://djangosnippets.org/snippets/3040/"#
            from django.utils.translation import ugettext as _
            from django.forms.widgets import Select
            for f in self.fields:
                self.fields[f].label = _(self.fields[f].label)
                if isinstance(self.fields[f].widget, Select):
                    self.fields[f].widget.attrs['disabled'] = 'disabled'
                else:
                    self.fields[f].widget.attrs['readonly'] = 'readonly'


import pandas as pd
from mrjob.job import MRJob
from mrjob.step import MRStep
import csv

# def rst_category(rst_city):
#     rst['categories'] = rst['categories'].apply(\
#         lambda x: x.strip('[').strip(']'))
#     rst['categories'] = rst['categories'].apply(lambda x: x.split(','))

class MRCategorize(MRJob):
    def mapper(self, _, line):
        reader = csv.reader([line])
        l = next(reader)
        bid = l[3] # Business ID
        cate = l[34].strip('[').strip(']') # Categories
        cate = cate.split(',')

        yield bid, cate

    def combiner(self, bid, cate):
        cate_ListOfList = list(cate)
        for cate_list in cate_ListOfList:
            for c in cate_list:
                yield c, bid

    def reducer(self, c, bid):

        yield c, list(bid)

if __name__ == '__main__':
    MRCategorize.run()
# Import csv file: cs123_restaurants.csv
# python3 category_restaurant.py  --jobconf mapreduce.jobreduces=1 restaurant.csv


from django.conf.urls import url, include
from rest_framework_extensions.routers import ExtendedSimpleRouter

from .views import (
    UserViewSetWithEmailLookup,
    GroupViewSet,
)

router = ExtendedSimpleRouter()

# main routesconfiguration
(
    router.register(r'users', UserViewSetWithEmailLookup)
            .register(r'groups', GroupViewSet, 'users-group', parents_query_lookups=['user_groups__email'])
)

urlpatterns = [
    url(r'^', include(router.urls)),
]


from math import sqrt
import random

def is_prime(n):
    value = int(sqrt(n))
    for i in range(2, value + 1, 1):
        if n % i == 0:
            return False
    return True

def multiplicative_inverse(e, phi):
    s1, s2 = 1, 0
    t1, t2 = 0, 1
    while phi != 0:
        q = e // phi
        r = e % phi
        e, phi = phi, r
        s = s1 - q * s2
        s1, s2 = s2, s
        t = t1 - q * t2
        t1, t2 = t2, t
    return s1 % s2

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

def generate_keypair(p, q):
    if not (is_prime(p) and is_prime(q)):
        raise ValueError('Both numbers must be prime.')
    elif p == q:
        raise ValueError('p and q cannot be equal')

    # n = pq
    n = p * q

    # phi = (p-1)(q-1)
    phi = (p - 1) * (q - 1)

    # Choose an integer e such that e and phi(n) are coprime
    e = random.randrange(1, phi)

    # Use Euclid's Algorithm to verify that e and phi(n) are comprime
    g = gcd(e, phi)
    while g != 1:
        e = random.randrange(1, phi)
        g = gcd(e, phi)

    # Use Extended Euclid's Algorithm to generate the private key
    d = multiplicative_inverse(e, phi)
    # Return public and private keypair
    # Public key is (e, n) and private key is (d, n)
    return (e, n), (d, n)


import sys,argparse
import os,glob
import numpy as np
import pandas as pd
#from GenomeData import *

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
matplotlib.rcParams['font.size']=12
matplotlib.rcParams["font.sans-serif"] = ["Arial", "Liberation Sans", "Bitstream Vera Sans"]
matplotlib.rcParams["font.family"] = "sans-serif"
#import seaborn as sns
#sns.set(font_scale=2)
#sns.set_style("whitegrid", {'axes.grid' : False})

#import re,bisect
#plus = re.compile('\+')
#minus = re.compile('\-')

def scatter_plot(df,marked_index,figname,mark_col):

    pylist = df.index
    plt.figure(figsize=(2.6,2.6))
    for index in df.index:
        xp = list(df.index).index(index)
        values = -1*np.log10(df.loc[index,mark_col])#;print(values)
        plt.scatter(xp,values,color='k',s=6)

    max_p = -1*np.log10(df.iloc[0,-1])
    values_reset = max_p*1.05
    # top 3 and TFs in shared top 10
    sorted_marker_ids = np.append(df.index[:10],df.index[:20].intersection(marked_index)[:5]);print(sorted_marker_ids)
    sorted_marker_pos = sorted(set([list(df.index).index(marker) for marker in  sorted_marker_ids]))
    x_pos_m=0
    for marker_id in sorted_marker_pos:
        xp = marker_id #;print(xp,df)
        values = -1*np.log10(df.loc[df.index[marker_id],mark_col])#;print(values)
        plt.scatter(xp,values,color='r',s=15)
        # ==== mark the index label, not overlap with each other
        values_reset = min(values,values_reset-max_p*0.095)#;print(values,values_reset)
        # ==== mark the index and plot the arrow separately
        if df.index[marker_id] in marked_index:
            plt.text(xp+120+x_pos_m,values_reset,df.index[marker_id],color='red')
        else:
            plt.text(xp+120+x_pos_m,values_reset,df.index[marker_id])

        # ==== plt.arrow(x,y,dx,dy)
        plt.arrow(xp+120+x_pos_m,values_reset+max_p*0.03,-90-x_pos_m,values-values_reset-max_p*0.03,\
                  length_includes_head = True,head_width=max_p*0.02,head_length=35,fc='k',ec='k')
        x_pos_m = x_pos_m + 30

    title = os.path.basename(figname).split('_bart3d_results.pdf')[0]
    title = title.split('_')
    plt.title('{}\n{}'.format(' '.join(title[:4]),' '.join(title[4:][::-1])),fontsize=12)
    plt.xlabel('TR Rank')
    plt.ylabel('-log$_{{10}}$ $p$-value')
    plt.axes().set_xticks([0,len(df.index)])
    plt.axes().set_xticklabels([1,len(df.index)],rotation=0, ha='center',fontsize=13,color='k')
    plt.savefig(figname,bbox_inches='tight',pad_inches=0.1,transparent=True,dpi=600)
    plt.close()

def main_plot(infile,outdir):

    marked_index = []
    basename = os.path.basename(infile).split('.txt')[0]
    df = pd.read_csv(infile,index_col=0,sep='\t')
    mark_col = df.columns[-1]
    df = df.sort_values(by=[mark_col],ascending=True)
    figname = outdir+os.sep+basename+'.pdf'
    scatter_plot(df,marked_index,figname,mark_col)

def main(indir,outdir):

    os.makedirs(outdir,exist_ok=True)

    infiles = glob.glob('{}/*bart*_results.txt'.format(indir))
    for infile in infiles[:]:
        main_plot(infile,outdir)

if __name__ == '__main__':

    parser = argparse.ArgumentParser()
#     parser.add_argument('-i', '--infile', action = 'store', type = str,dest = 'infile', help = 'input file of', metavar = '<file>')
#     parser.add_argument('-o','--outfile', action = 'store', type = str,dest = 'outfile', help = 'outfile of', metavar = '<file>')
    parser.add_argument('-i', '--indir', action = 'store', type = str,dest = 'indir', help = 'input dir of ', metavar = '<dir>')
    parser.add_argument('-o','--outdir', action = 'store', type = str,dest = 'outdir', help = 'outdir of ,default: current dir', metavar = '<dir>',default='rank_dot_figs')
    #parser.add_argument('-s','--species', action = 'store', type = str,dest = 'species', help = 'species used to choose correct chromosome, e.g., hg38 or mm10', metavar = '<str>',required=True)

    args = parser.parse_args()
    if(len(sys.argv))<0:
        parser.print_help()
        sys.exit(1)

    main(args.indir,args.outdir)


from PIL import Image
import numpy as np
import cv2
import sys
from werkzeug import secure_filename
import sqlite3
from contextlib import closing
from handle_db import db_save_image
import os

db_name = 'database.db'
OUTPUT_FOLDER = './outputs'

def face_rect_out(image_path, cascade_path, output_path):

    image = cv2.imread(image_path)
    image_shape = (image.shape[1],image.shape[0])
    image_center = (int(image.shape[1]/2),int(image.shape[0]/2))
    angle = 0

    mtx = cv2.getRotationMatrix2D(image_center, angle, 1)
    image_rot = cv2.warpAffine(image, mtx, image_shape)
    image_gray = cv2.cvtColor(image_rot, cv2.COLOR_BGR2GRAY)

    cascade = cv2.CascadeClassifier(cascade_path)
    facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=2, minSize=(50, 50))

    trimmed_image_url = []
    # 検出した場合
    if len(facerect) > 0:
        color = (255, 0, 255)
        #検出した顔を囲む矩形の作成
        rect_data = []
        trimmed_images = []
        original_img_name = image_path.split('/')[-1].split('.')[0]
        for i, rect in enumerate(facerect):
            xmin = rect[0]
            ymin = rect[1]
            xmax = rect[0]+rect[2]
            ymax = rect[1]+rect[3]
            trimmed = image[ymin:ymax, xmin:xmax, :]
            trimmed_images.append(trimmed)
            url = os.path.join(OUTPUT_FOLDER, original_img_name+'_{}.jpg'.format(i))
            trimmed_image_url.append(url)
            cv2.imwrite(url, trimmed)

        for rect in facerect:
            cv2.rectangle(image, tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]), color, thickness=2)
            rect_data.append((image_path.split('/')[-1], int(xmin), int(ymin), int(xmin-xmax), int(ymin-ymax)))

        #認識結果の保存
        db_save_image(rect_data)

    #認識結果の保存
    cv2.imwrite(output_path, image)
    return trimmed_image_url

if __name__ == "__main__":
    #image_path = "test0.jpg"
    cascade_path = "haarcascade_frontalface_default.xml"
    output_path = "output.jpg"
    face_rect_out(sys.argv[1], cascade_path, output_path)