text:
import pandas as pd  # type: ignore
import numpy as np  # type: ignore
import copy
import typing

from d3m import container
from d3m.primitive_interfaces.unsupervised_learning import UnsupervisedLearnerPrimitiveBase
from d3m.metadata import hyperparams, params
from d3m.container import DataFrame as d3m_DataFrame
from d3m.primitive_interfaces.base import CallResult
from common_primitives import utils
from d3m.metadata import base as mbase
from . import config

Input = container.DataFrame
Output = container.DataFrame

class Params(params.Params):
    mapping: typing.Dict
    all_columns: typing.Set[str]
    empty_columns: typing.List[object]
    textmapping: typing.Dict
    requirement: typing.Dict
    cat_columns: typing.List[object]
    cat_col_index: typing.List[object]

class UEncHyperparameter(hyperparams.Hyperparams):
    text2int = hyperparams.UniformBool(
        default=False,
        description='Whether to convert everything to numerical. For text columns, each row may get converted into a column',
        semantic_types=['http://schema.org/Boolean',
                        'https://metadata.datadrivendiscovery.org/types/ControlParameter'])

    use_columns = hyperparams.Set(
        elements=hyperparams.Hyperparameter[int](-1),
        default=(),
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        description="A set of column indices to force primitive to operate on. If any specified column cannot be parsed, it is skipped.",
    )
    exclude_columns = hyperparams.Set(
        elements=hyperparams.Hyperparameter[int](-1),
        default=(),
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        description="A set of column indices to not operate on. Applicable only if \"use_columns\" is not provided.",
    )
    return_result = hyperparams.Enumeration(
        values=['append', 'replace', 'new'],
        default='replace',
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        description="Should parsed columns be appended, should they replace original columns, or should only parsed columns be returned? This hyperparam is ignored if use_semantic_types is set to false.",
    )
    use_semantic_types = hyperparams.UniformBool(
        default=False,
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        description="Controls whether semantic_types metadata will be used for filtering columns in input dataframe. Setting this to false makes the code ignore return_result and will produce only the output dataframe"
    )
    add_index_columns = hyperparams.UniformBool(
        default=True,
        semantic_types=['https://metadata.datadrivendiscovery.org/types/ControlParameter'],
        description="Also include primary index columns if input data has them. Applicable only if \"return_result\" is set to \"new\".",
    )

## reference: https://github.com/scikit-learn/scikit-learn/issues/8136
class Label_encoder(object):
    def __init__(self):
        self.class_index = None

    def fit_pd(self, df, cols=[]):
        '''
        fit all columns in the df or specific list.
        generate a dict:
        {feature1:{label1:1,label2:2}, feature2:{label1:1,label2:2}...}
        '''
        if len(cols) == 0:
            cols = df.columns
        self.class_index = {}
        for f in cols:
            uf = df[f].unique()
            self.class_index[f] = {}
            index = 1
            for item in uf:
                self.class_index[f][item] = index
                index += 1

    def transform_pd(self,df,cols=[]):
        '''
        transform all columns in the df or specific list from lable to index, return and update dataframe.
        '''
        newdf = copy.deepcopy(df)
        if len(cols) == 0:
            cols = df.columns
        for f in cols:
            if f in self.class_index:
                newdf[f] = df[f].apply(lambda d: self.__update_label(f,d))
        return newdf

    def get_params(self):
        return self.class_index

    def set_params(self, textmapping):
        self.class_index = textmapping

    def __update_label(self,f,x):
        '''
        update the label to index, if not found in the dict, add and update the dict.
        '''
        try:
            return self.class_index[f][x]
        except:
            self.class_index[f][x] = max(self.class_index[f].values())+1
            return self.class_index[f][x]

class UnaryEncoder(UnsupervisedLearnerPrimitiveBase[Input, Output, Params, UEncHyperparameter]):
    """
    A primitive which converts the numerical attributes to multi-column attributes.
    Each new column value would be 1 if the original value is larger than this column's name value
    Otherwise the new column value would be 0
    This encoder only operate when the amount of the numerical data is less than 12, otherwise the column would keep unchanged.
    """
    metadata = hyperparams.base.PrimitiveMetadata({
        "id": "DSBox-unary-encoder",
        "version": config.VERSION,
        "name": "DSBox Unary Data Encoder",
        "description": "Encode using unary code for orinal data",
        "python_path": "d3m.primitives.data_preprocessing.UnaryEncoder.DSBOX",
        "primitive_family": "DATA_PREPROCESSING",
        "algorithm_types": [ "ENCODE_ONE_HOT" ],
        "source": {
            "name": config.D3M_PERFORMER_TEAM,
            "contact": config.D3M_CONTACT,
            "uris": [ config.REPOSITORY ]
            },
        ### Automatically generated
        # "primitive_code"
        # "original_python_path"
        # "schema"
        # "structural_type"
        ### Optional
        "keywords": [ "preprocessing",  "encoding"],
        "installation": [ config.INSTALLATION ],
        #"location_uris": [],
        #"precondition": [],
        #"effects": [],
        #"hyperparms_to_tune": []
        })

    def __repr__(self):
        return "%s(%r)" % ('UnaryEncoder', self.__dict__)

    def __init__(self, *, hyperparams: UEncHyperparameter) -> None:
        super().__init__(hyperparams=hyperparams)

        self.hyperparams = hyperparams
        self._text2int = hyperparams['text2int']
        self._textmapping: typing.Dict = dict()
        self._mapping: typing.Dict = dict()
        self._all_columns: typing.Set = set()
        self._empty_columns: typing.List[object] = []
        self._cat_col_index: typing.List[object] = []
        self._cat_columns: typing.List[object] = []
        self._training_inputs = None
        self._fitted = False
        self._col_index = None
        self._requirement: typing.Dict = dict()

    def get_params(self) -> Params:

        # Hack to work around pytypes bug. Covert numpy int64 to int.
        for key in self._mapping.keys():
            self._mapping[key] = [np.nan if np.isnan(x) else int(x) for x in self._mapping[key]]

        param = Params(mapping = self._mapping,
                       all_columns = self._all_columns,
                       empty_columns = self._empty_columns,
                       textmapping = self._textmapping,
                       requirement = self._requirement,
                       cat_columns = self._cat_columns,
                       cat_col_index = self._cat_col_index
                       )
        return param

    def set_params(self, *, params: Params) -> None:
        self._textmapping = params['textmapping']
        self._mapping = params['mapping']
        self._all_columns = params['all_columns']
        self._empty_columns = params['empty_columns']
        self._requirement = params['requirement']
        self._cat_columns = params['cat_columns']
        self._cat_col_index = params['cat_col_index']
        self._fitted = True

    def set_training_data(self, *, inputs: Input) -> None:
        self._training_inputs = inputs
        self._fitted = False

    def fit(self, *, timeout: float = None, iterations: int = None) -> CallResult[None]:
        """
        Need training data from set_training_data first.
        The encoder would record specified columns to encode and column values to
        unary encode later in the produce step.
        """
        if self._fitted:
            return

        if self._training_inputs is None:
            raise ValueError('Missing training(fitting) data.')

        data = self._training_inputs.copy()
        all_attributes = utils.list_columns_with_semantic_types(metadata=data.metadata, semantic_types=[
            "https://metadata.datadrivendiscovery.org/types/Attribute"])

        # Remove columns with all empty values, structural type str
        numeric = utils.list_columns_with_semantic_types(
            data.metadata, ['http://schema.org/Integer', 'http://schema.org/Float'])
        numeric = [x for x in numeric if x in all_attributes]
        for element in numeric:
            if data.metadata.query((mbase.ALL_ELEMENTS, element)).get('structural_type', ())==str:
                if pd.isnull(pd.to_numeric(data.iloc[:,element], errors='coerce')).sum() == data.shape[0]:
                    self._empty_columns.append(element)

        # Remove columns with all empty values, structural numeric
        is_empty = pd.isnull(data).sum(axis=0) == data.shape[0]
        for i in all_attributes:
            if is_empty.iloc[i]:
                self._empty_columns.append(i)
        self._empty_columns = list(set(self._empty_columns))
        self._empty_columns.reverse()
        self._empty_columns = container.List(self._empty_columns)
        data = utils.remove_columns(data, self._empty_columns)
        # print('fit', data.shape)

        categorical_attributes = utils.list_columns_with_semantic_types(
            metadata=data.metadata,
            semantic_types=[
                "https://metadata.datadrivendiscovery.org/types/OrdinalData",
                "https://metadata.datadrivendiscovery.org/types/CategoricalData"
                ]
            )
        all_attributes = utils.list_columns_with_semantic_types(
            metadata=data.metadata,
            semantic_types=["https://metadata.datadrivendiscovery.org/types/Attribute"]
            )
        self._cat_col_index = container.List(set(all_attributes).intersection(numeric))
        self._cat_columns = container.List(data.columns[self._cat_col_index].tolist())
        #import pdb
        #pdb.set_trace()
        numerical_values = data.iloc[:, self._cat_col_index].apply(
            lambda col: pd.to_numeric(col, errors='coerce'))

        self._all_columns = set(data.columns)

        # mapping
        idict = {}
        for name in self._cat_columns:
            col = numerical_values[name]
            idict[name] = sorted(col.unique())
        self._mapping = idict

        if self._text2int:
            texts = data.drop(self._mapping.keys(),axis=1)
            texts = texts.select_dtypes(include=[object])
            le = Label_encoder()
            le.fit_pd(texts)
            self._textmapping = le.get_params()

        # determine whether to run unary encoder on the given column or not
        data_enc = data.iloc[:, self._cat_col_index].apply(lambda col: pd.to_numeric(col, errors='coerce'))
        for column_name in data_enc:
            col = data_enc[column_name]
            col.is_copy = False
            # only apply unary encoder when the amount of the numerical data is less than 12
            if col.unique().shape[0] < 13:
                self._requirement[column_name] = True
            else:
                self._requirement[column_name] = False

        self._fitted = True

        return CallResult(None, has_finished=True, iterations_done=1)

    def __encode_column(self, col):
        unary = pd.DataFrame(col)
        for v in self._mapping[col.name]:
            unary[col.name+"_"+str(v)] = (col >= v).astype(int)
        return unary.drop(col.name, axis=1)

    def produce(self, *, inputs: Input, timeout: float = None, iterations: int = None) -> CallResult[Output]:
        """
        Convert and output the input data into unary encoded format,
        using the trained (fitted) encoder.
        Value unseen in training_inputs would be rounded to nearest value in training_inputs.
        Missing(NaN) cells in a column one-hot encoded would give
        out a row of all-ZERO columns for the target column.
        """
        #if self._target_columns == []:
        #    return CallResult(inputs, True, 1)
        if not self._fitted:
            raise ValueError('Encoder model not fitted. Use fit()')

        # Return if there is nothing to encode
        if len(self._cat_columns)==0:
            return CallResult(inputs, True, 1)

        if isinstance(inputs, pd.DataFrame):
            data = inputs.copy()
        else:
            data = inputs[0].copy()
        data = utils.remove_columns(data, self._empty_columns)
        set_columns = set(data.columns)

        if set_columns != self._all_columns:
            raise ValueError('Columns(features) fed at produce() differ from fitted data.')

        # core part: encode the unary columns
        data_enc = data.iloc[:, self._cat_col_index].apply(lambda col: pd.to_numeric(col, errors='coerce'))
        data_else = data.drop(self._mapping.keys(),axis=1)
        res = []
        for column_name in data_enc:
            col = data_enc[column_name]
            col.is_copy = False
            # only apply unary encoder when the amount of the numerical data is less than 12
            if self._requirement[column_name]:
                chg_v = lambda x: min(self._mapping[col.name], key=lambda a:abs(a-x)) if x is not None else x
                # only encode the values which is not null
                col[col.notnull()] = col[col.notnull()].apply(chg_v)
                encoded = self.__encode_column(col)
                res.append(encoded)
            else:
                res.append(col)

        if self._text2int:
            texts = data_else.select_dtypes([object])
            le = Label_encoder()
            le.set_params(self._textmapping)
            data_else[texts.columns] = le.transform_pd(texts)
        # transfer the encoded results to dataFrame
        encoded = d3m_DataFrame(pd.concat(res, axis=1))

        # update metadata for existing columns
        for index in range(len(encoded.columns)):
            old_metadata = dict(encoded.metadata.query((mbase.ALL_ELEMENTS, index)))
            old_metadata["structural_type"] = int
            old_metadata["semantic_types"] = (
                'http://schema.org/Integer', 'https://metadata.datadrivendiscovery.org/types/Attribute')
            encoded.metadata = encoded.metadata.update((mbase.ALL_ELEMENTS, index), old_metadata)
        # after extracting the traget columns, remove these columns from dataFrame
        data_else = utils.remove_columns(data, self._cat_col_index)
        result = utils.horizontal_concat(data_else, encoded)

        return CallResult(result, True, 1)

    @classmethod
    def _get_columns_to_fit(cls, inputs: Input, hyperparams: UEncHyperparameter):
        if not hyperparams['use_semantic_types']:
            return inputs, list(range(len(inputs.columns)))

        inputs_metadata = inputs.metadata

        def can_produce_column(column_index: int) -> bool:
            return cls._can_produce_column(inputs_metadata, column_index, hyperparams)

        columns_to_produce, columns_not_to_produce = common_utils.get_columns_to_use(inputs_metadata,
                                                                             use_columns=hyperparams['use_columns'],
                                                                             exclude_columns=hyperparams['exclude_columns'],
                                                                             can_use_column=can_produce_column)
        return inputs.iloc[:, columns_to_produce], columns_to_produce

    @classmethod
    def _can_produce_column(cls, inputs_metadata: mbase.DataMetadata, column_index: int, hyperparams: UEncHyperparameter) -> bool:
        column_metadata = inputs_metadata.query((mbase.ALL_ELEMENTS, column_index))

        semantic_types = column_metadata.get('semantic_types', [])
        if len(semantic_types) == 0:
            cls.logger.warning("No semantic types found in column metadata")
            return False
        if "https://metadata.datadrivendiscovery.org/types/Attribute" in semantic_types:
            return True

        return False


text:
from __future__ import print_function
import unittest

import nifty
import nifty.graph
import numpy
import random

class TestLiftedMulticutSolver(unittest.TestCase):

    def generateGrid(self, gridSize):
        def nid(x, y):
            return x*gridSize[1] + y
        G = nifty.graph.UndirectedGraph
        g =  G(gridSize[0] * gridSize[1])
        for x in range(gridSize[0]):
            for y in range(gridSize[1]):

                u = nid(x,y)

                if x + 1 < gridSize[0]:

                    v = nid(x+1, y)
                    g.insertEdge(u, v)

                if y + 1 < gridSize[1]:

                    v = nid(x, y+1)
                    g.insertEdge(u, v)

        return g, nid

    def gridLiftedModel(self, gridSize = [3,2], bfsRadius=2, weightRange = [-1,1]):

        g,nid = self.generateGrid(gridSize)
        obj = nifty.graph.lifted_multicut.liftedMulticutObjective(g)
        graph = obj.graph
        liftedGraph = obj.liftedGraph

        # this should  add edges
        obj.insertLiftedEdgesBfs(bfsRadius)
        postEdges = liftedGraph.numberOfEdges

        for edge in liftedGraph.edges():
            u,v = liftedGraph.uv(edge)
            w = random.uniform(weightRange[0],weightRange[1])
            obj.setCost(u, v, w)

        return obj,nid

    def testLiftedMulticutGreedyAdditive(self):

        for x in range(5):
            obj,nid = self.gridLiftedModel(gridSize=[40,40] , bfsRadius=4, weightRange=[-1,1])

            solverFactory = obj.liftedMulticutGreedyAdditiveFactory()
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            argN = solver.optimize()

            solverFactory = obj.liftedMulticutAndresGreedyAdditiveFactory()
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            argA = solver.optimize()

            self.assertAlmostEqual(obj.evalNodeLabels(argA), obj.evalNodeLabels(argN))

    def testLiftedMulticutKernighanLinSimple(self):

        G = nifty.graph.UndirectedGraph
        g = G(5);
        g.insertEdge(0, 1) # 0
        g.insertEdge(0, 3) # 1
        g.insertEdge(1, 2) # 2
        g.insertEdge(1, 4) #
        g.insertEdge(3, 4) # 4

        obj = nifty.graph.lifted_multicut.liftedMulticutObjective(g)
        lg = obj.liftedGraph

        obj.setCost(0, 1, 10.0)
        obj.setCost(0, 2, -1.0)
        obj.setCost(0, 3, -1.0)
        obj.setCost(0, 4, -1.0)
        obj.setCost(1, 2, 10.0)
        obj.setCost(1, 3, -1.0)
        obj.setCost(1, 4,  4.0)
        obj.setCost(2, 3, -1.0)
        obj.setCost(2, 4, -1.0)
        obj.setCost(3, 4, 10.0)

        arg = numpy.array([1,2,3,4,5])
        solverFactory = obj.liftedMulticutKernighanLinFactory()
        solver = solverFactory.create(obj)
        visitor = obj.verboseVisitor(100)

        arg2 = solver.optimize(visitor,arg)

        self.assertEqual(arg2[0] != arg2[1],  bool(0))
        self.assertEqual(arg2[0] != arg2[2],  bool(0))
        self.assertEqual(arg2[0] != arg2[3],  bool(1))
        self.assertEqual(arg2[0] != arg2[4],  bool(1))
        self.assertEqual(arg2[1] != arg2[2],  bool(0))
        self.assertEqual(arg2[1] != arg2[3],  bool(1))
        self.assertEqual(arg2[1] != arg2[4],  bool(1))
        self.assertEqual(arg2[2] != arg2[3],  bool(1))
        self.assertEqual(arg2[2] != arg2[4],  bool(1))
        self.assertEqual(arg2[3] != arg2[4],  bool(0))

    def testLiftedMulticutKernighanLin(self):

        gridSize = [5, 5]
        for x in range(4):
            obj,nid = self.gridLiftedModel(gridSize=gridSize , bfsRadius=2, weightRange=[-3,1])

            solverFactory = obj.liftedMulticutGreedyAdditiveFactory()
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            arg = solver.optimize()
            argGC = arg.copy()
            egc = obj.evalNodeLabels(argGC)

            #arg = numpy.arange(gridSize[0]*gridSize[1])
            solverFactory = obj.liftedMulticutKernighanLinFactory()
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            arg2 = solver.optimize(argGC.copy())
            ekl = obj.evalNodeLabels(arg2)

            if False:
                solverFactory = obj.liftedMulticutAndresKernighanLinFactory()
                solver = solverFactory.create(obj)
                visitor = obj.verboseVisitor(100)
                arg3 = solver.optimize(argGC.copy())
                eakl = obj.evalNodeLabels(arg3)

                self.assertLessEqual(ekl, egc)
                self.assertAlmostEqual(ekl, eakl)

        for x in range(4):
            obj,nid = self.gridLiftedModel(gridSize=gridSize , bfsRadius=2, weightRange=[-3,1])

            arg = numpy.arange(gridSize[0]*gridSize[1])

            solverFactory = obj.liftedMulticutKernighanLinFactory()
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            arg2 = solver.optimize(arg.copy())
            ekl = obj.evalNodeLabels(arg2)

            if False:
                solverFactory = obj.liftedMulticutAndresKernighanLinFactory()
                solver = solverFactory.create(obj)
                visitor = obj.verboseVisitor(100)
                arg3 = solver.optimize(arg.copy())
                eakl = obj.evalNodeLabels(arg3)

                self.assertAlmostEqual(ekl, eakl)

                # solver with andres

    def testLiftedMulticutSolverFm(self):
        random.seed(0)
        for x in range(1):
            obj,nid = self.gridLiftedModel(gridSize=[40,40] , bfsRadius=4, weightRange=[-1,1])

            pgen = obj.watershedProposalGenerator(sigma=1.0,seedingStrategie='SEED_FROM_LOCAL',
                numberOfSeeds=0.1)

            print(x)

            solverFactory = obj.fusionMoveBasedFactory()
            solverFactory = obj.fusionMoveBasedFactory(proposalGenerator=pgen, numberOfIterations=100,
                stopIfNoImprovement=10)
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            argN = solver.optimize(visitor)

    def implTestLiftedMulticutIlpBfsGrid(self, ilpSolver, gridSize=[4,4], bfsRadius=4, weightRange=[-2,1], verbose=0, relativeGap=0.00001):

        obj,nid = self.gridLiftedModel(gridSize=gridSize , bfsRadius=bfsRadius, weightRange=weightRange)
        solverFactory = obj.liftedMulticutIlpFactory(ilpSolver=ilpSolver, relativeGap=relativeGap)
        solver = solverFactory.create(obj)
        if verbose > 0:
            visitor = obj.verboseVisitor(100)
            arg = solver.optimize(visitor=visitor)
        else:
            arg = solver.optimize()

    def testLiftedMulticutIlpBfsGrid(self):

        if nifty.Configuration.WITH_GLPK:
            for x in range(5):
                self.implTestLiftedMulticutIlpBfsGrid(ilpSolver='glpk', gridSize=[3,3],
                                                      relativeGap=0.3,
                                                      weightRange=(-2,1))
        if nifty.Configuration.WITH_CPLEX:
            for x in range(5):
                self.implTestLiftedMulticutIlpBfsGrid(ilpSolver='cplex', gridSize=[3,3],
                                                      relativeGap=0.3,
                                                      verbose=0,  weightRange=(-2,1))
        if nifty.Configuration.WITH_GUROBI:
            for x in range(5):
                self.implTestLiftedMulticutIlpBfsGrid(ilpSolver='gurobi')

    def implTestSimpleChainModelOpt(self, size, ilpSolver):

        if nifty.Configuration.WITH_CPLEX:

            #  0 - 1 - 2 - 3
            #

            G = nifty.graph.UndirectedGraph
            g =  G(size)
            for x in range(size - 1):
                g.insertEdge(x, x+1)

            obj = nifty.graph.lifted_multicut.liftedMulticutObjective(g)
            graph = obj.graph
            liftedGraph = obj.liftedGraph

            for x in range(size - 1):
                w = 1.0
                obj.setCost(x, x+1, w)

            w = -1.0 * float(size)
            obj.setCost(0, size-1, w)

            solverFactory = obj.liftedMulticutIlpFactory(ilpSolver=ilpSolver)
            solver = solverFactory.create(obj)
            visitor = obj.verboseVisitor(100)
            arg = solver.optimize()

            self.assertNotEqual(arg[0],arg[size-1])

    def testSimpleChainModelMulticutGlpk(self):

        if False and nifty.Configuration.WITH_GLPK:
            for x in range(10):
                self.implTestSimpleChainModelOpt(ilpSolver='glpk', size=4)

    def testSimpleChainModelMulticutGurobi(self):

        if nifty.Configuration.WITH_GUROBI:
            for x in range(10):
                self.implTestSimpleChainModelOpt(ilpSolver='gurobi', size=4)

    def testSimpleChainModelMulticutCplex(self):

        if nifty.Configuration.WITH_CPLEX:
            for x in range(10):
                self.implTestSimpleChainModelOpt(ilpSolver='cplex', size=4)

ext: py

max_stars_repo_name: k-dominik/nifty

max_issues_repo_name: k-dominik/nifty

max_forks_repo_name: k-dominik/nifty

reason: import numpy


text:
import numpy as np
from driving_gridworld.car import Car
from driving_gridworld.actions import ACTIONS, RIGHT, NO_OP, LEFT
import pytest

@pytest.mark.parametrize("col_action", [(0, LEFT), (3, RIGHT)])

def test_car_does_not_bounce_off_wall(col_action):
    col = col_action[0]
    action = col_action[1]
    car = Car(col, 1)
    speed_limit = 5
    next_car = car.next(action, speed_limit)
    assert next_car.col < 0 or next_car.col > 3


text:
# -*- coding: UTF-8 -*-
"""
    Kmeans聚类
    算法：KMeans
    数据：
"""

import sklearn
import numpy as np

from sklearn.cluster import KMeans
from sklearn.pipeline import Pipeline
from sklearn.datasets import make_blobs

# 数据预处理
centers = 4
x, y = make_blobs(n_samples=1000, n_features=8, centers=centers, random_state=0)

# 模型训练
km = KMeans(n_clusters=centers, init='random', random_state=0)
km.fit(x)
print(km.score(x))

max_stars_repo_name: coding-chenkaikai/scikit

max_issues_repo_name: coding-chenkaikai/scikit

max_forks_repo_name: coding-chenkaikai/scikit


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

# # Data analysis of a 2AFC task

# In[ ]:

# Modifiable variables when changing computer location:
# Directory where the images will be stored:
directory_images = '/home/emma/github/TFG/results/fit_model/'
directory_functions = '/home/emma/github/TFG/functions'
directory_data = '/home/emma/github/TFG/data/'
directory_results = '/home/emma/github/TFG/results/'

# In[ ]:

# Importing all the libraries that will be used
import numpy as np
import statsmodels.api as sm
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import json
from sklearn.linear_model import LinearRegression
import sys
import datetime
import pandas as pd

# Insert path where the functions are stored
sys.path.insert(1, directory_functions)

# Importing the functions that will be used
import rat_functions1 as rf
import help_plot as hp

# ## Storing the data into variables

# In[ ]:

# Name of the rats
rats = ["Rat 24 05 sec","Rat 25 05 sec","Rat 36 05 sec", "Rat 37 05 sec","Rat 25 1 sec"," Rat 35 1 sec","Rat 37 1 sec"]

# In[ ]:

# # Open processed data and store data into variables
# with open(directory_data+"processed_data.json","r") as f:
#     data = json.load(f)
#     stimulus = data[0]             # Data frames obtained from a gaussian distribution of a determined coherence
#     coherences = data[1]         # Target coherences used for each trial
#     rewards = data[2]              # Correct choice side
#     decisions = data[3]            # Actual decision made by the rat each trial
#     performances = data[4]     # 0 if the rat chose the correct choice, 1 if the rat chose the incorrect choice
#     target_sigmas = data[5]     # Target sigma to use for each trial

# ## Storing the fit data

# In[ ]:

PR_total = []
param_fits = []
for rat in ["rat24","rat25","rat36","rat37"]:

    # Opening JSON file
    f = open(directory_results+'model_fitStLinPi/'+rat+'_05sec.json',)
    # returns JSON object as
    # a dictionary
    data = json.load(f)
    f.close()
    data = pd.DataFrame(data)
    index_min=[]
    imin1=data["LL_training"].idxmin()
    #print(df.loc[imin1]["LL_training"])
    LL=data.loc[imin1]["LL_training"]
    param_fits.append(data.loc[imin1]["param_all"])
    if rat=="rat1":
        data.loc[imin1]["LL_training"]
        #Nparam[model].append(len(df.loc[imin1]["param_fit"]))
        Nparam = len(data.loc[imin1]["param_fit"])
    print(data.loc[imin1]["param_all"])
    PR_total.append(data.loc[imin1]["PR_training"])

for rat in ["rat25","rat35","rat37"]:

    # Opening JSON file
    f = open(directory_results+'model_fitStLinPi/'+rat+'_1sec.json',)
    # returns JSON object as
    # a dictionary
    data = json.load(f)
    f.close()
    data = pd.DataFrame(data)
    index_min=[]
    imin1=data["LL_training"].idxmin()
    #print(df.loc[imin1]["LL_training"])
    LL=data.loc[imin1]["LL_training"]
    param_fits.append(data.loc[imin1]["param_all"])
    if rat=="rat1":
        data.loc[imin1]["LL_training"]
        #Nparam[model].append(len(df.loc[imin1]["param_fit"]))
        Nparam = len(data.loc[imin1]["param_fit"])
    print(data.loc[imin1]["param_all"])
    PR_total.append(data.loc[imin1]["PR_training"])
# param_fits = np.delete(param_fits,2,0)
# PR_total = np.delete(PR_total,2,0)

# ## Open fit stimulus

# In[ ]:

stimulus =[]
coherences = []
target_sigmas =[]
rewards = []
performances = []
choices = []
dates = []
n_total_stimulus = []
for rat in [24,25,36,37]:
    print(rat)
    # Opening JSON file
    f = open(directory_data+'processed_data_rat'+str(rat)+'_dataset2_05.json',)
    # returns JSON object as
    # a dictionary
    data = json.load(f)
    f.close()
    stimulus.append(data["stim_10"])
    coherences.append(data["coherences_10"])
    rewards.append(data["rewards_10"])
    target_sigmas.append(data["sigma_10"])
    choices.append(data["choices_10"])
    dates.append(data["date_10"])
    n_total_stimulus.append(data["n_stim_10"])
    performances.append(data["performance_10"])

for rat in [25,35,37]:
    print(rat)
    # Opening JSON file
    f = open(directory_data+'processed_data_rat'+str(rat)+'_dataset2_1.json',)
    # returns JSON object as
    # a dictionary
    data = json.load(f)
    f.close()
    stimulus.append(data["stim_20"])
    coherences.append(data["coherences_20"])
    rewards.append(data["rewards_20"])
    target_sigmas.append(data["sigma_20"])
    choices.append(data["choices_20"])
    print(len(data["stim_20"]))
    dates.append(data["date_20"])
    n_total_stimulus.append(data["n_stim_20"])
    performances.append(data["performance_20"])

# In[ ]:

for i in range(len(stimulus)):
    if i < 4:
        stimulus[i] = (np.reshape(stimulus[i],(int(len(stimulus[i])/10),10)))
        print(len(choices[i]),len(stimulus[i]))
    else:
        stimulus[i] = (np.reshape(stimulus[i],(int(len(stimulus[i])/20),20)))
        print(len(choices[i]),len(stimulus[i]))
#     print(len(np.transpose(np.reshape(stimulus[i],(10,int(len(stimulus[i])/10))))))

# In[ ]:

choices_fit = []
for rat in range(len(rats)):
    choices1 = []
    for prob in PR_total[rat]:
        if np.random.rand() < prob:
            choices1.append(1)
        else:
            choices1.append(0)
    choices_fit.append(choices1)

# In[ ]:

months = []
for rat in range(len(rats)):
    months.append(len(rf.count(dates[rat]))/31)
print(months)

# ## Segment data

# ## Possible model

# In[ ]:

#stimulus transformation
transformed_stimulus = []
for rat in range(len(rats)):
    stimulus_transf = []
    st1 = param_fits[rat]["st"][0]
    st2 = param_fits[rat]["st"][1]
#     st3 = param_fits[rat]["st"][2]
    for stim in stimulus[rat]:
        s_trans = []
        for frame in range(len(stim)):
            s_trans.append(stim[frame]*(st1*np.exp(st2*(frame-1))))

        stimulus_transf.append(s_trans)
    transformed_stimulus.append(stimulus_transf)

# In[ ]:

colors = ["k","blue","lightblue"]
for rat in range(len(rats)):
 st1 = param_fits[rat]["st"][0]
 st2 = param_fits[rat]["st"][1]
 fig,axs = plt.subplots(figsize = (8,5))
 c = 0
 for t in [0,5,10]:
     s_trans = []
     for ph_stim in np.arange(-10,11,1):
         frame = ph_stim/10
         s_trans.append(frame*(st1*np.exp(st2*(t-1))))

     axs.plot(np.arange(-10,11,1)/10,s_trans,colors[c])
     c += 1
     hp.remove_axis(axs)
         #     plt.ylim([-1,1])

 fig.text(0.5, 0.02, "Physical stimulus", ha='center',fontsize = 24)
 fig.text(0.03, 0.5, "Evidence", va='center', rotation='vertical',fontsize = 24)

# ## Separing the data by coherences (real and target)
# ### Separing the data by target coherences

# In[ ]:

# Will compute a vector with the target coherences used for each rat
coherence_vectors = [] # coherence_vectors[rat][coherence]

for coh in coherences:
    coh_vec = rf.return_coherences_vector(coh)
    coherence_vectors.append(coh_vec)

# In[ ]:

coherence_vectors

# In[ ]:

# The stimulus, coherences, rewards, decisions, performances and target sigmas will be divided by coherences
results_divided_coherence = [] # The vector will be organized as follows:
                                                  # results_divided_coherence[rat][coherence]=[rewards, decisions, performances, stimulus, target_sigmas]

for rat in range(len(rats)):
    divided_coherence = []
    for coherence in coherence_vectors[rat]:
        divided_coherence.append(rf.divide_coh(coherences[rat],rewards[rat],choices[rat],performances[rat],stimulus[rat],target_sigmas[rat],coherence))
    results_divided_coherence.append(divided_coherence)

# In[ ]:

len(choices_fit[0])

# In[ ]:

# The stimulus, coherences, rewards, decisions, performances and target sigmas will be divided by coherences
results_divided_coherence_fit = [] # The vector will be organized as follows:
                                                  # results_divided_coherence[rat][coherence]=[rewards, decisions, performances, stimulus, target_sigmas]

for rat in range(len(rats)):
    divided_coherence = []
    for coherence in coherence_vectors[rat]:
        divided_coherence.append(rf.divide_coh(coherences[rat],rewards[rat],choices_fit[rat],performances[rat],stimulus[rat],target_sigmas[rat],coherence))
    results_divided_coherence_fit.append(divided_coherence)

# In[ ]:

len(results_divided_coherence_fit)

# In[ ]:

# The percentages, the number of trials and the coherence vectors avobe x trials will be stored into the following variables for each rat:
percentages = [] # percentages[rat][strong_coherence]
total_number_trials = [] # total_number_trials[rat][strong_coherence]
strong_coherence_vectors = [] # strong_coherence_vectors[rat] coherences which have more than 2500 trials

n_trials_threshold = 100 # Only the coherences which have avobe n_trials_threshold will be used

for rat in range(len(rats)):
    perc = []
    number_trials = []
    coh_vector = []
    # for each coherence which has more than n_trials_threshold, the percentage of right choice will be computed
    for i in range(len(results_divided_coherence[rat])):
        if len(results_divided_coherence[rat][i][1]) > n_trials_threshold:
            perc.append(sum(results_divided_coherence[rat][i][1])/len(results_divided_coherence[rat][i][1]))
            coh_vector.append(coherence_vectors[rat][i])
            number_trials.append(len(results_divided_coherence[rat][i][1]))
    percentages.append(perc)
    total_number_trials.append(number_trials)
    strong_coherence_vectors.append(coh_vector)

# In[ ]:

# The percentages, the number of trials and the coherence vectors avobe x trials will be stored into the following variables for each rat:
percentages_fit = [] # percentages[rat][strong_coherence]
total_number_trials_fit = [] # total_number_trials[rat][strong_coherence]
strong_coherence_vectors_fit = [] # strong_coherence_vectors[rat] coherences which have more than 2500 trials

n_trials_threshold = 100 # Only the coherences which have avobe n_trials_threshold will be used

for rat in range(len(rats)):
    perc = []
    number_trials = []
    coh_vector = []
    # for each coherence which has more than n_trials_threshold, the percentage of right choice will be computed
    for i in range(len(results_divided_coherence_fit[rat])):
        if len(results_divided_coherence_fit[rat][i][1]) > n_trials_threshold:
            perc.append(sum(results_divided_coherence_fit[rat][i][1])/len(results_divided_coherence_fit[rat][i][1]))
            coh_vector.append(coherence_vectors[rat][i])
            number_trials.append(len(results_divided_coherence[rat][i][1]))
    percentages_fit.append(perc)
    total_number_trials_fit.append(number_trials)
    strong_coherence_vectors_fit.append(coh_vector)

# In[ ]:

# For each rat, plot the psychometric curve with the coherence values which have above x trials
# fig, axs = plt.subplots(figsize = (8,5))
for rat in range(len(rats)):
    fig, axs = plt.subplots(figsize = (8,5))
    # Computing the confidence intervals (95%)
    z = 1.96
    conf_int = [[percentages[rat][i]-z*(np.sqrt((percentages[rat][i]*(1-percentages[rat][i]))/total_number_trials[rat][i])) for i in range(len(total_number_trials[rat]))],
                [percentages[rat][i]+z*(np.sqrt((percentages[rat][i]*(1-percentages[rat][i]))/total_number_trials[rat][i])) for i in range(len(total_number_trials[rat]))]]
    conf_int = [[percentages[rat][i]-conf_int[0][i] for i in range(len(percentages[rat]))],[conf_int[1][i]-percentages[rat][i] for i in range(len(percentages[rat]))]]
    axs.errorbar(strong_coherence_vectors[rat],percentages[rat],conf_int,marker='o',label = "Real data")
#     conf_int = [[percentages_fit[rat][i]-z*(np.sqrt((percentages_fit[rat][i]*(1-percentages_fit[rat][i]))/total_number_trials_fit[rat][i])) for i in range(len(total_number_trials_fit[rat]))],
#                 [percentages_fit[rat][i]+z*(np.sqrt((percentages_fit[rat][i]*(1-percentages_fit[rat][i]))/total_number_trials_fit[rat][i])) for i in range(len(total_number_trials_fit[rat]))]]
#     conf_int = [[percentages_fit[rat][i]-conf_int[0][i] for i in range(len(percentages_fit[rat]))],[conf_int[1][i]-percentages_fit[rat][i] for i in range(len(percentages_fit[rat]))]]
# #     axs.errorbar(strong_coherence_vectors_fit[rat],percentages_fit[rat],conf_int,marker='o',label = "Fitted model")
    conf_int = [[percentages_fit[rat][i]-z*(np.sqrt((percentages_fit[rat][i]*(1-percentages_fit[rat][i]))/total_number_trials_fit[rat][i])) for i in range(len(total_number_trials_fit[rat]))],
                [percentages_fit[rat][i]+z*(np.sqrt((percentages_fit[rat][i]*(1-percentages_fit[rat][i]))/total_number_trials_fit[rat][i])) for i in range(len(total_number_trials_fit[rat]))]]
    axs.fill_between(strong_coherence_vectors_fit[rat],conf_int[0],conf_int[1],alpha=0.2,facecolor='#089FFF', linewidth=4, linestyle='dashdot', antialiased=True,label = "Fitted model")

    fig.suptitle(" Psychometric curve "+rats[rat],fontsize = 18)
    axs.legend(loc = "upper left",fontsize = 12)
    fig.text(0.5, 0.02, "Stimulus evidence (coherence)", ha='center',fontsize = 16)
    fig.text(0.03, 0.5, "Probability to choose right", va='center', rotation='vertical',fontsize = 18)
    axs.axis([-1.1,1.1,0,1])
    hp.xticks(axs,[-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1],xticklabels=None,fontsize=14)
    hp.yticks(axs,[0,0.2,0.4,0.6,0.8,1],yticklabels = None,fontsize =14)

    hp.remove_axis(axs)
    plt.hlines(0.5,-1,1,linestyle='--',color='lightgrey')
    plt.vlines(0,0.05,0.95,linestyle='--',color='lightgrey')

# fig.savefig(directory_images+'psychometric_curves/psychometric_curves_target_coherences.png', bbox_inches = 'tight')

# ## Separing the data by sigma and coherence

# In[ ]:

# Will compute a vector with the sigma used for each rat and each coherence
sigma_lists = [] #sigma_lists[rat][sigma]

for rat in range(len(rats)):
    sigma_list = []
    for i in range(len(results_divided_coherence[rat])):
        for a in rf.count(results_divided_coherence[rat][i][4]):
            sigma_list.append(a)
    sigma_list = rf.count(sigma_list)
    sigma_list = sorted(sigma_list)
    sigma_lists.append(sigma_list)

# In[ ]:

# Will compute a vector with the sigma used for each rat and each coherence
sigma_lists_fit = [] #sigma_lists[rat][sigma]

for rat in range(len(rats)):
    sigma_list = []
    for i in range(len(results_divided_coherence_fit[rat])):
        for a in rf.count(results_divided_coherence_fit[rat][i][4]):
            sigma_list.append(a)
    sigma_list = rf.count(sigma_list)
    sigma_list = sorted(sigma_list)
    sigma_lists_fit.append(sigma_list)

# In[ ]:

# Data for each sigma will be stored
results_divided_sigma = [] # results_divided_sigma[rat][coherence][sigma][reward, decision, performance, stim]

for rat in range(len(rats)):
    divided_sigma = []
    for i in range(len(results_divided_coherence[rat])):
        results = []
        for sigma in sigma_lists[rat]:
            results.append(rf.divide_sigma(results_divided_coherence[rat][i][0],results_divided_coherence[rat][i][1],results_divided_coherence[rat][i][2],
                                             results_divided_coherence[rat][i][3],results_divided_coherence[rat][i][4],sigma))
        divided_sigma.append(results)
    results_divided_sigma.append(divided_sigma)

# In[ ]:

# Data for each sigma will be stored
results_divided_sigma_fit = [] # results_divided_sigma[rat][coherence][sigma][reward, decision, performance, stim]

for rat in range(len(rats)):
    divided_sigma = []
    for i in range(len(results_divided_coherence_fit[rat])):
        results = []
        for sigma in sigma_lists[rat]:
            results.append(rf.divide_sigma(results_divided_coherence_fit[rat][i][0],results_divided_coherence_fit[rat][i][1],results_divided_coherence_fit[rat][i][2],
                                             results_divided_coherence_fit[rat][i][3],results_divided_coherence_fit[rat][i][4],sigma))
        divided_sigma.append(results)
    results_divided_sigma_fit.append(divided_sigma)

# In[ ]:

# Compute the percentages of right choice for the results_divided_sigma
percentages_lists, coherences_lists, length_lists, length_all_rats = rf.compute_percentages(rats,results_divided_sigma,coherence_vectors)

# In[ ]:

# Compute the percentages of right choice for the results_divided_sigma
percentages_lists_fit, coherences_lists_fit, length_lists_fit, length_all_rats_fit = rf.compute_percentages(rats,results_divided_sigma_fit,coherence_vectors)

# In[ ]:

# For each rat, plot the psychometric curve with the coherence values which have above x trials
# fig, axs = plt.subplots(figsize = (8,5))
for rat in range(len(rats)):
    for a in range(len(coherences_lists[rat])):
            fig, axs = plt.subplots(figsize = (8,5))

            # Computing the confidence intervals (95%)
            z = 1.96
            conf_int = [[percentages_lists[rat][a][i]-z*(np.sqrt((percentages_lists[rat][a][i]*(1-percentages_lists[rat][a][i]))/length_lists[rat][a][i])) for i in range(len(percentages_lists[rat][a]))],
                        [percentages_lists[rat][a][i]+z*(np.sqrt((percentages_lists[rat][a][i]*(1-percentages_lists[rat][a][i]))/length_lists[rat][a][i])) for i in range(len(percentages_lists[rat][a]))]]
            conf_int = [[percentages_lists[rat][a][i]-conf_int[0][i] for i in range(len(percentages_lists[rat][a]))],[conf_int[1][i]-percentages_lists[rat][a][i] for i in range(len(percentages_lists[rat][a]))]]
            axs.errorbar(coherences_lists[rat][a],percentages_lists[rat][a],conf_int,marker='o',label = "Real data")
            conf_int = [[percentages_lists_fit[rat][a][i]-z*(np.sqrt((percentages_lists_fit[rat][a][i]*(1-percentages_lists_fit[rat][a][i]))/length_lists_fit[rat][a][i])) for i in range(len(percentages_lists_fit[rat][a]))],
                [percentages_lists_fit[rat][a][i]+z*(np.sqrt((percentages_lists_fit[rat][a][i]*(1-percentages_lists_fit[rat][a][i]))/length_lists_fit[rat][a][i])) for i in range(len(percentages_lists_fit[rat][a]))]]
            axs.fill_between(coherences_lists_fit[rat][a],conf_int[0],conf_int[1],alpha=0.2,facecolor='#089FFF', linewidth=4, linestyle='dashdot', antialiased=True,label = "Fitted model")

#             axs.plot(coherences_lists[rat][i],percentages_lists[rat][i],label="real psychometric curve",marker = "o")
            axs.axis([-1,1,0,1])
            axs.vlines(0,-1,1,color = "lightgrey",linestyles = "--",label ="x = 0")
            axs.hlines(0.5,-1,1,color = "lightblue",linestyles = "--",label = "y = 0.5")
            fig.suptitle(rats[rat]+" Psychometric curve for sigma "+str(sigma_lists[rat][a]),fontsize = 18)
            fig.text(0.5, 0.02, "Stimulus evidence (coherence)", ha='center',fontsize = 16)
            fig.text(0.03, 0.5, "Probability to choose right", va='center', rotation='vertical',fontsize = 18)
            axs.legend(loc = "upper left",fontsize = 12)
            axs.spines['right'].set_visible(False)
            axs.spines['top'].set_visible(False)

# fig.savefig(directory_images+'psychometric_curves/psychometric_curves_target_coherences.png', bbox_inches = 'tight')

# ### Comparing coherence distribution for each sigma

# In[ ]:

# Computing the coherence vector for all rats together
coherences_all_rats = []
for rat in range(len(rats)):
    coherences_all_rats.append(coherence_vectors[rat])
coherences_all_rats = rf.return_vector(coherences_all_rats)

# Computing the sigma vector for all rats tohether

sigmas_all_rats = rf.return_vector(sigma_lists)

# In[ ]:

# # Generates a file with a table of the number of trials for each coherence and sigma for all rats
# rf.recount_trials_all_rats(sigma_lists,coherences_all_rats,length_all_rats,"all rats",coherence_vectors,rats,sigmas_all_rats,directory_images)
# # Will make the chart with the recount of trials for each coherence and each sigma for each rat
# for rat in range(len(rats)):
#     rf.recount_trials(sigma_lists[rat],coherence_vectors[rat],length_lists[rat],rats[rat],coherences_lists[rat],directory_images)

# In[ ]:

# Compute sensitivity and bias using curve_fit
all_sensitivity, all_bias = rf.compute_sensitivity(rats,coherences_lists,percentages_lists,sigma_lists,directory_images)

# In[ ]:

# Compute sensitivity and bias using curve_fit
all_sensitivity_fit, all_bias_fit = rf.compute_sensitivity(rats,coherences_lists_fit,percentages_lists_fit,sigma_lists_fit,directory_images)

# In[ ]:

# Performing bootstrap to compute confidence intervals
n_trials = 10

all_percentages_lists_boot = []
all_coherences_lists_boot = []
all_length_lists_boot = []
all_length_all_rats_boot  = []
for i in range(n_trials):
    new_divided_sigma = rf.bootstrap(results_divided_sigma)
    percentages_lists_boot, coherences_lists_boot, length_lists_boot, length_all_rats_boot = rf.compute_percentages(rats,new_divided_sigma,coherence_vectors)
    all_percentages_lists_boot.append(percentages_lists_boot)
    all_coherences_lists_boot.append(coherences_lists_boot)
    all_length_lists_boot.append(length_lists_boot)
    all_length_all_rats_boot.append(length_all_rats_boot)

# In[ ]:

# Performing bootstrap to compute confidence intervals
n_trials = 10

all_percentages_lists_boot_fit = []
all_coherences_lists_boot_fit = []
all_length_lists_boot_fit = []
all_length_all_rats_boot_fit  = []
for i in range(n_trials):
    new_divided_sigma = rf.bootstrap(results_divided_sigma_fit)
    percentages_lists_boot, coherences_lists_boot, length_lists_boot, length_all_rats_boot = rf.compute_percentages(rats,new_divided_sigma,coherence_vectors)
    all_percentages_lists_boot_fit.append(percentages_lists_boot)
    all_coherences_lists_boot_fit.append(coherences_lists_boot)
    all_length_lists_boot_fit.append(length_lists_boot)
    all_length_all_rats_boot_fit.append(length_all_rats_boot)

# In[ ]:

# Compute sensitivity and bias using curve_fit for the bootstrap data
all_sensitivity_boot = []
all_bias_boot = []
for i in range(len(all_percentages_lists_boot)):
    sensitivity_boot, bias_boot = rf.compute_sensitivity_boot(rats,all_coherences_lists_boot[i],all_percentages_lists_boot[i],sigma_lists)
    all_sensitivity_boot.append(sensitivity_boot)
    all_bias_boot.append(bias_boot)

# In[ ]:

# Compute sensitivity and bias using curve_fit for the bootstrap data
all_sensitivity_boot_fit = []
all_bias_boot_fit = []
for i in range(len(all_percentages_lists_boot_fit)):
    sensitivity_boot, bias_boot = rf.compute_sensitivity_boot(rats,all_coherences_lists_boot_fit[i],all_percentages_lists_boot_fit[i],sigma_lists)
    all_sensitivity_boot_fit.append(sensitivity_boot)
    all_bias_boot_fit.append(bias_boot)

# In[ ]:

# Computing the confidence intervals for the sensitivity
sensitivity_sigma = [np.zeros((len(sigma_lists[0]),n_trials)),np.zeros((len(sigma_lists[1]),n_trials)),np.zeros((len(sigma_lists[2]),n_trials)),np.zeros((len(sigma_lists[3]),n_trials)),
                    np.zeros((len(sigma_lists[4]),n_trials)),np.zeros((len(sigma_lists[5]),n_trials)),np.zeros((len(sigma_lists[6]),n_trials))]

for trial in range(len(all_sensitivity_boot)):
    for rat in range(len(all_sensitivity_boot[trial])):
        for sigm in range(len(all_sensitivity_boot[trial][rat])):
             sensitivity_sigma[rat][sigm][trial] = all_sensitivity_boot[trial][rat][sigm]

conf_intervals_sensitivity = []
for rat in range(len(rats)):
    sensitivity_rat = []
    for a in range(len(all_sensitivity_boot[0][rat])):
        sens = sensitivity_sigma[rat][a]

        z = 1.96
        conf_int = [np.mean(sens)-z*np.std(sens),np.mean(sens)+z*np.std(sens)]
        sensitivity_rat.append(conf_int)

    conf_intervals_sensitivity.append(sensitivity_rat)

# In[ ]:

# Computing the confidence intervals for the sensitivity
sensitivity_sigma_fit = [np.zeros((len(sigma_lists[0]),n_trials)),np.zeros((len(sigma_lists[1]),n_trials)),np.zeros((len(sigma_lists[2]),n_trials)),np.zeros((len(sigma_lists[3]),n_trials)),
                    np.zeros((len(sigma_lists[4]),n_trials)),np.zeros((len(sigma_lists[5]),n_trials)),np.zeros((len(sigma_lists[6]),n_trials))]

for trial in range(len(all_sensitivity_boot_fit)):
    for rat in range(len(all_sensitivity_boot_fit[trial])):
        for sigm in range(len(all_sensitivity_boot_fit[trial][rat])):
             sensitivity_sigma_fit[rat][sigm][trial] = all_sensitivity_boot_fit[trial][rat][sigm]

conf_intervals_sensitivity_fit = []
for rat in range(len(rats)):
    sensitivity_rat = []
    for a in range(len(all_sensitivity_boot_fit[0][rat])):
        sens = sensitivity_sigma_fit[rat][a]

        z = 1.96
        conf_int = [np.mean(sens)-z*np.std(sens),np.mean(sens)+z*np.std(sens)]
        sensitivity_rat.append(conf_int)

    conf_intervals_sensitivity_fit.append(sensitivity_rat)

# In[ ]:

# Computing the bias confidence intervals
bias_sigma = [np.zeros((len(sigma_lists[0]),n_trials)),np.zeros((len(sigma_lists[1]),n_trials)),np.zeros((len(sigma_lists[2]),n_trials)),np.zeros((len(sigma_lists[3]),n_trials)),
                    np.zeros((len(sigma_lists[4]),n_trials)),np.zeros((len(sigma_lists[5]),n_trials)),np.zeros((len(sigma_lists[6]),n_trials))]

for trial in range(len(all_bias_boot)):
    for rat in range(len(all_bias_boot[trial])):
        for sigm in range(len(all_bias_boot[trial][rat])):
             bias_sigma[rat][sigm][trial] = all_bias_boot[trial][rat][sigm]

conf_intervals_bias = []
for rat in range(len(rats)):
    bias_rat = []
    for a in range(len(all_bias_boot[0][rat])):
        bias = bias_sigma[rat][a]

        z = 1.96
        conf_int = [np.mean(bias)-z*np.std(bias),np.mean(bias)+z*np.std(bias)]
        bias_rat.append(conf_int)

    conf_intervals_bias.append(bias_rat)

# In[ ]:

# Computing the bias confidence intervals
bias_sigma_fit = [np.zeros((len(sigma_lists[0]),n_trials)),np.zeros((len(sigma_lists[1]),n_trials)),np.zeros((len(sigma_lists[2]),n_trials)),np.zeros((len(sigma_lists[3]),n_trials)),
                    np.zeros((len(sigma_lists[4]),n_trials)),np.zeros((len(sigma_lists[5]),n_trials)),np.zeros((len(sigma_lists[6]),n_trials))]

for trial in range(len(all_bias_boot_fit)):
    for rat in range(len(all_bias_boot_fit[trial])):
        for sigm in range(len(all_bias_boot_fit[trial][rat])):
             bias_sigma_fit[rat][sigm][trial] = all_bias_boot_fit[trial][rat][sigm]

conf_intervals_bias_fit = []
for rat in range(len(rats)):
    bias_rat = []
    for a in range(len(all_bias_boot_fit[0][rat])):
        bias = bias_sigma[rat][a]

        z = 1.96
        conf_int = [np.mean(bias)-z*np.std(bias),np.mean(bias)+z*np.std(bias)]
        bias_rat.append(conf_int)

    conf_intervals_bias_fit.append(bias_rat)

# In[ ]:

# Plot of the sensitivity over sigma
for rat in range(len(rats)):
    fig, axs = plt.subplots(figsize = (8,5))
    conf_int = [[conf_intervals_sensitivity[rat][i][0] for i in range(len(sigma_lists[rat]))],[conf_intervals_sensitivity[rat][i][1] for i in range(len(sigma_lists[rat]))]]
    conf_int_0 = []
    conf_int_1 = []
    new_sigma_list = []
    new_sensitivity = []
    for i in range(len(conf_int[0])):
        if str(conf_int[0][i]) != "nan":
            if all_sensitivity[rat][i] != None:

                conf_int_0.append(conf_int[0][i]-all_sensitivity[rat][i])
                conf_int_1.append(all_sensitivity[rat][i]-conf_int[1][i])
                new_sigma_list.append(sigma_lists[rat][i])
                new_sensitivity.append(all_sensitivity[rat][i])
    conf_int = [conf_int_0,conf_int_1]
    plt.errorbar(new_sigma_list,new_sensitivity,conf_int,marker='o',color= "skyblue")
    conf_int = [[conf_intervals_sensitivity_fit[rat][i][0] for i in range(len(sigma_lists[rat]))],[conf_intervals_sensitivity_fit[rat][i][1] for i in range(len(sigma_lists[rat]))]]
    conf_int_0 = []
    conf_int_1 = []
    new_sigma_list = []
    new_sensitivity = []
    for i in range(len(conf_int[0])):
        if str(conf_int[0][i]) != "nan":
            if all_sensitivity_fit[rat][i] != None:

                conf_int_0.append(all_sensitivity_fit[rat][i]-conf_int[0][i])
                conf_int_1.append(all_sensitivity_fit[rat][i]+conf_int[1][i])
                new_sigma_list.append(sigma_lists[rat][i])
                new_sensitivity.append(all_sensitivity_fit[rat][i])
    conf_int = [conf_int_0,conf_int_1]
    plt.fill_between(new_sigma_list,conf_int_0,conf_int_1,alpha=0.2,facecolor='#089FFF',
    linewidth=4, linestyle='dashdot', antialiased=True)
#     plt.axis([-0.1,0.9,-1,50])

    hp.remove_axis(axs)
    fig.suptitle(rats[rat]+" sensitivity",fontsize = 18)
    fig.text(0.5, 0.02, "Sigma", ha='center',fontsize = 16)
    fig.text(0.03, 0.5, "Sensitivity", va='center', rotation='vertical',fontsize = 18)
#     fig.savefig(directory_images+'sensitivity/sensitivity_'+rats[rat]+'.png', bbox_inches = 'tight')

# In[ ]:

# Plot of the bias over sigma
for rat in range(len(rats)):
    fig, axs = plt.subplots(figsize = (8,5))
    conf_int = [[conf_intervals_bias[rat][i][0] for i in range(len(sigma_lists[rat]))],[conf_intervals_bias[rat][i][1] for i in range(len(sigma_lists[rat]))]]
    conf_int_0 = []
    conf_int_1 = []
    new_sigma_list = []
    new_bias = []
    for i in range(len(conf_int[0])):
        if str(conf_int[0][i]) != "nan":
            if all_bias[rat][i] != None:
                conf_int_0.append(conf_int[0][i]-all_bias[rat][i])
                conf_int_1.append(all_bias[rat][i]-conf_int[1][i])
                new_sigma_list.append(sigma_lists[rat][i])
                new_bias.append(all_bias[rat][i])
    conf_int = [conf_int_0,conf_int_1]
    plt.errorbar(new_sigma_list,new_bias,conf_int,marker='o',color="skyblue")

    conf_int = [[conf_intervals_bias_fit[rat][i][0] for i in range(len(sigma_lists[rat]))],[conf_intervals_bias_fit[rat][i][1] for i in range(len(sigma_lists[rat]))]]
    conf_int_0 = []
    conf_int_1 = []
    new_sigma_list = []
    new_bias = []
    for i in range(len(conf_int[0])):
        if str(conf_int[0][i]) != "nan":
            if all_bias_fit[rat][i] != None:
                conf_int_0.append(all_bias_fit[rat][i]-conf_int[0][i])
                conf_int_1.append(all_bias_fit[rat][i]+conf_int[1][i])
                new_sigma_list.append(sigma_lists[rat][i])
                new_bias.append(all_bias_fit[rat][i])
    plt.fill_between(new_sigma_list,conf_int_0,conf_int_1,alpha=0.2,facecolor='#089FFF',
    linewidth=4, linestyle='dashdot', antialiased=True)

    plt.plot(new_sigma_list,new_bias,marker = 's')
    plt.hlines(0,-0.1,0.9,color = "grey",linestyles = "--")
    plt.axis([-0.1,0.9,-0.8,0.3])
    hp.remove_axis(axs)
    fig.suptitle(rats[rat]+" bias",fontsize = 18)
    fig.text(0.5, 0.02, "Sigma", ha='center',fontsize = 16)
    fig.text(0.03, 0.5, "Bias", va='center', rotation='vertical',fontsize = 18)
#     fig.savefig(directory_images+'bias/bias'+rats[rat]+'.png', bbox_inches = 'tight')

# ## Dividing the data by sigma (without taking coherence into account)

# In[ ]:

results_divided_sigma_nocoh_fit = [] # results_divided_sigma_nocoh[rat][sigma][reward,decision,performance,stim]
perc_sigma_nocoh_fit = []
# Dividing the data over sigma (not by coherence)
results_divided_sigma_nocoh = [] # results_divided_sigma_nocoh[rat][sigma][reward,decision,performance,stim]
perc_sigma_nocoh = []

sigmas_lists =[[0.1129,0.1129],[0.225,0.576],[0.8,0.8]]

for rat in range(len(rats)):
    divided_sigma = []
    for sigma in sigmas_lists:
        divided_sigma.append(rf.divide_sigma_1(rewards[rat],choices[rat],performances[rat],stimulus[rat],target_sigmas[rat],sigma))
    perc = []
    number_trials = []
    results_divided_sigma_nocoh.append(divided_sigma)
    for i in range(len(divided_sigma)):
        perc.append(sum(results_divided_coherence[rat][i][2])/len(results_divided_coherence[rat][i][2]))
        number_trials.append(len(results_divided_coherence[rat][i][1]))
    perc_sigma_nocoh.append(perc)

    fig, axs = plt.subplots(figsize = (8,5))
    plt.plot([s[0] for s in sigmas_lists],perc,marker = 'o',color="skyblue")
    plt.axis([0,1,0,1])
    hp.remove_axis(axs)
    fig.suptitle(rats[rat]+" accuracy",fontsize = 18)
    fig.text(0.5, 0.02, "Sigma", ha='center',fontsize = 16)
    fig.text(0.03, 0.5, "Accuracy", va='center', rotation='vertical',fontsize = 18)
#     fig.savefig(directory_images+'accuracy_vs_sigma/accuracy_vs_sigma'+rats[rat]+'.png', bbox_inches = 'tight')
    divided_sigma = []

    for sigma in sigmas_lists:
        divided_sigma.append(rf.divide_sigma_1(rewards[rat],choices_fit[rat],performances[rat],stimulus[rat],target_sigmas[rat],sigma))
    perc = []
    number_trials = []
    results_divided_sigma_nocoh_fit.append(divided_sigma)
    for i in range(len(divided_sigma)):
        perc.append(sum(results_divided_coherence_fit[rat][i][2])/len(results_divided_coherence_fit[rat][i][2]))
        number_trials.append(len(results_divided_coherence_fit[rat][i][1]))
    perc_sigma_nocoh_fit.append(perc)

    plt.plot([s[0] for s in sigmas_lists],perc,marker = 'o',color="red")
    plt.axis([0,1,0,1])
    hp.remove_axis(axs)
#     fig.suptitle(rats[rat]+" accuracy",fontsize = 18)
#     fig.text(0.5, 0.02, "Sigma", ha='center',fontsize = 16)
#     fig.text(0.03, 0.5, "Accuracy", va='center', rotation='vertical',fontsize = 18)
#     fig.savefig(directory_images+'accuracy_vs_sigma/accuracy_vs_sigma'+rats[rat]+'.png', bbox_inches = 'tight')

# PODRIA FER ELS CONFIDENCE INTERVALS AQUÍ

# ## Kernel

# In[ ]:

def divided_time_PK(rats,results_divided_sigma_nocoh,results_divided_sigma_nocoh_fit,sigma_lists,rats_list,directory_images):
     # results_divided_sigma_coh[rat][sigma][reward,decision,performance,stim]
        fig, ((axs1,axs2,axs3)) = plt.subplots(1,3,figsize = (18,4))

        axs = [0,axs1,axs2,axs3,0,0]
        color = [0,"maroon","brown","red","orangered","orange","yellow"]
        index = 1
        aranges = [np.arange(0,3,1),np.arange(0,3,1),np.arange(0,3,1),np.arange(0,3,1),np.arange(0,3,1),np.arange(0,3,1),np.arange(0,3,1),np.arange(0,3,1)]
        for rat in rats:
            for a in aranges[rat]:
                print(rat,len(results_divided_sigma_nocoh[rat][a][3][0]))
                try:
                    conf_int = PK(results_divided_sigma_nocoh[rat][a][1],results_divided_sigma_nocoh[rat][a][3],axs[index],color[index])
                    print(np.round((sigmas_lists[a][0]+sigmas_lists[a][1])/2,2))

                    conf_int_fit = PK_fit(results_divided_sigma_nocoh_fit[rat][a][1],results_divided_sigma_nocoh_fit[rat][a][3],axs[index],color[index])
                    axs[index].set_title("Sigma = "+str(np.round((sigmas_lists[a][0]+sigmas_lists[a][1])/2,2)))
                    axs[index].spines['right'].set_visible(False)
                    axs[index].spines['top'].set_visible(False)
                except:
                    print("another error")
                index +=1
#         plt.ylim(-1.5,1.5)
#         fig.suptitle("PK for "+rats_list[rat],fontsize = 18)
        fig.text(0.5, 0.001, 'Frame nº', ha='center',fontsize = 16)
        fig.text(0.08, 0.5, 'Impact', va='center', rotation='vertical',fontsize = 18)

#         fig.savefig(directory_images+'PK/temporal_PK'+rats_list[rat]+'.png', bbox_inches = 'tight')

# In[ ]:

# def PK(decision,stim,axs,col):
#     logit_mod = sm.Logit(decision, stim)
#     result = logit_mod.fit()

#     pars_list =result.params
#     confidence_interval = result.conf_int()
#     conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
#     conf_int = [pars_list-conf_int[0],conf_int[1]-pars_list]
#     axs.errorbar(np.arange(0,len(pars_list),1),pars_list,conf_int,color = col,marker='s')

#     return(confidence_interval)

def PK(decision,stim,axs,col):

    logit_mod = sm.Logit(decision, stim)
    result = logit_mod.fit()

    pars_list =result.params
    confidence_interval = result.conf_int()
    conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
    conf_int = [pars_list-conf_int[0],conf_int[1]-pars_list]
    axs.errorbar(np.arange(0,len(pars_list),1),pars_list,conf_int,color = col,marker='s')
#     axs.set_ylim(top=max(pars_list)+0.4,bottom =min(pars_list)-0.4)

#     return(confidence_interval)

# In[ ]:

def PK_fit(decision,stim,axs,col):
    logit_mod = sm.Logit(decision, stim)
    result = logit_mod.fit()

    pars_list =result.params
    confidence_interval = result.conf_int()
    conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
#     conf_int = [pars_list-conf_int[0],conf_int[1]-pars_list]
#     axs.errorbar(np.arange(0,len(pars_list),1),pars_list,conf_int,marker='s')

    axs.fill_between(np.arange(0,len(pars_list),1),conf_int[0],conf_int[1],alpha=0.2, facecolor='#089FFF',
    linewidth=4, linestyle='dashdot', antialiased=True)
#     axs.set_ylim(top=max(pars_list)+0.4,bottom =min(pars_list)-0.4)

    return(confidence_interval)

# def PK_fit(decision,stim,axs,col):
#     # provar sklearn
#     pars_list = LogisticRegression().get_params(stim, decision)
# #     result = logit_mod.fit()

# #     pars_list =result.params

#     confidence_interval = result.conf_int()
#     confidence_interval = decision_function(X)
#     conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
#     conf_int = [pars_list-conf_int[0],conf_int[1]-pars_list]
#     axs.fill_between(np.arange(0,len(pars_list),1),conf_int[0],conf_int[1],alpha=0.2, facecolor='#089FFF',
#     linewidth=4, linestyle='dashdot', antialiased=True)

#     return(confidence_interval)

# In[ ]:

rats_sec = [1,2,4]
plt.figure()
divided_time_PK([0],results_divided_sigma_nocoh,results_divided_sigma_nocoh_fit,sigma_lists,rats,directory_images)

# In[ ]:

# Spatial Kernel for all sigmas for each rat

# For each stimulus, how many stimulus are from -1 to -0.9, etc to get a transformed stimulus, then make a logistic regression
color = [0,"maroon","brown","red","orangered","orange","yellow"]

new_transf_stim = rf.transformed_stimulus(stimulus)
# new_transf_sec_stim  = rf.transformed_stimulus([stimulus[0],stimulus[3]])
new_dec_half = choices
# new_dec_sec = [choices[0],choices[3]]
new_dec_half_fit = choices_fit
# new_dec_sec_fit = [choices_fit[0],choices_fit[3]]

for rat in [0,1,2,3,4,5,6]:
#     try:
    rats_half = [0,1,2,3,4,5,6]

    fig, axs = plt.subplots(figsize = (8,5))
    plt.xticks([0,2.5,5,7.5,10],["-1","-0.5","0","0.5","1"])
    print(len(new_dec_half[rat]),len(new_transf_stim[rat]))
    logit_mod = sm.Logit(new_dec_half[rat], new_transf_stim[rat])
    result = logit_mod.fit()
    pars_list =result.params
    confidence_interval = result.conf_int()
    conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
    conf_int = [pars_list-conf_int[0],conf_int[1]-pars_list]
    x_values = np.arange(0,len(pars_list),1)
    plt.errorbar(x_values,pars_list,conf_int,marker='o')

    slope, intercept = np.polyfit([x_values[4],x_values[5],x_values[6]], [pars_list[4],pars_list[5],pars_list[6]], 1)
    abline_values = [slope * i + intercept for i in x_values]
    plt.plot(x_values, abline_values,'grey', linestyle = "dashed")

    logit_mod = sm.Logit(new_dec_half_fit[rat], new_transf_stim[rat])
    result = logit_mod.fit()
    pars_list =result.params
    confidence_interval = result.conf_int()
    conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
    x_values = np.arange(0,len(pars_list),1)
    plt.fill_between(x_values,conf_int[0],conf_int[1],alpha=0.2,facecolor='#089FFF', linewidth=4, linestyle='dashdot', antialiased=True,label = "Fitted model")

    fig.suptitle("Spatial Kernel "+rats[rat],fontsize = 18)
    fig.text(0.5, 0.02, "Coherence value", ha='center',fontsize = 16)
    fig.text(0.03, 0.5, "Spatial Kernel", va='center', rotation='vertical',fontsize = 18)

    plt.ylim(-0.5,0.5)
    hp.remove_axis(axs)
#     fig.savefig(directory_images+'PK/spatial_PK_'+rats[rats_half[rat]]+'_half_sec.png', bbox_inches = 'tight')
#     except:
#         print("no ha funcionat"+str(rat))

# rats_sec = [0,3]

# for rat in range(len(rats_sec)):
# #     try:
# #         print(rat)
#     fig, axs = plt.subplots(figsize = (8,5))

#     plt.xticks([0,2.5,5,7.5,10], ["-1","-0.5","0","0.5","1"])
#     logit_mod = sm.Logit(new_dec_sec[rat], new_transf_sec_stim[rat])
#     result = logit_mod.fit()
#     pars_list =result.params
#     confidence_interval = result.conf_int()
#     conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
#     conf_int = [pars_list-conf_int[0],conf_int[1]-pars_list]
#     plt.errorbar(np.arange(0,len(pars_list),1),pars_list,conf_int,marker='o')
#     plt.ylim(-0.5,0.5)

#     x_values = np.arange(0,len(pars_list),1)

#     slope, intercept = np.polyfit([x_values[4],x_values[5],x_values[6]], [pars_list[4],pars_list[5],pars_list[6]], 1)
#     abline_values = [slope * i + intercept for i in x_values]
#     plt.plot(x_values, abline_values,'grey', linestyle = "dashed")
#     plt.ylim(-0.5,0.5)

#     logit_mod = sm.Logit(new_dec_sec_fit[rat], new_transf_sec_stim[rat])
#     result = logit_mod.fit()
#     pars_list =result.params
#     confidence_interval = result.conf_int()
#     conf_int = [[i[0] for i in confidence_interval],[i[1] for i in confidence_interval]]
#     x_values = np.arange(0,len(pars_list),1)
#     plt.fill_between(x_values,conf_int[0],conf_int[1],alpha=0.2,facecolor='#089FFF', linewidth=4, linestyle='dashdot', antialiased=True,label = "Fitted model")

#     fig.suptitle("Spatial Kernel "+rats[rats_sec[rat]],fontsize = 18)
#     fig.text(0.5, 0.02, "Coherence value", ha='center',fontsize = 16)
#     fig.text(0.03, 0.5, "Spatial Kernel", va='center', rotation='vertical',fontsize = 18)

#     hp.remove_axis(axs)
#     fig.savefig(directory_images+'PK/spatial_PK_'+rats[rats_sec[rat]]+'_sec.png', bbox_inches = 'tight')
# #     except:
# #         print("no ha funcionat"+str(rat))

max_stars_repo_name: emmafdz/FinalDegreeProject

max_issues_repo_name: emmafdz/FinalDegreeProject

max_forks_repo_name: emmafdz/FinalDegreeProject

reason: import numpy,from scipy,import statsmodels


text:
import numpy as np

class RunningStatistic(object):
    def __init__(self, phis, aggregator):
        self.phis = phis
        self.phisums = [0] * len(phis)
        self.nphis = len(phis)
        self.aggregator = aggregator

    def observe(self, *x):
        for i in range(self.nphis):
            self.phisums[i] += self.phis[i](*x)

    def aggregate(self):
        return self.aggregator(*self.phisums)

class RunningMean(RunningStatistic):
    def __init__(self):
        super(RunningMean, self).__init__(phis=[lambda x: x, lambda x: 1],
                                          aggregator=lambda sumx, count: float(sumx)/count)

class RunningSTD(RunningStatistic):
    def __init__(self):
        super(RunningSTD, self).__init__(phis=[lambda x: x,lambda x: x*x, lambda x: 1],
                                          aggregator=lambda sumx, sumx2, count: np.sqrt(float(sumx2)/count-(float(sumx)/count)**2) )

class ElementWiseRunningStatistic():
    def __init__(self, rs):
        self.rs = rs
        self.shape = None

    def observe(self, *x):
        if self.shape is None:
            self.shape = x[0].shape
            self.rs_arrays = np.array([[self.rs() for j in range(self.shape[1])] for i in range(self.shape[0])])
            self.ij = [(i,j) for j in range(self.shape[1]) for i in range(self.shape[0])]
        for i, j in self.ij:
            self.rs_arrays[i,j].observe(*[xx[i,j] for xx in x])

    def aggregate(self):
        return np.array([[self.rs_arrays[i,j].aggregate() for j in range(self.shape[1])] for i in range(self.shape[0])])

def main():
    from functools import reduce
    import operator
    shape = (5, 1, 10)
    prodshape = reduce(operator.mul, shape)

    x = np.random.randn(prodshape).reshape(shape)
    y = np.random.randn(prodshape).reshape(shape)

    mrm = ElementWiseRunningStatistic(RunningMean)
    for xx in x:
        mrm.observe(xx)
    assert np.allclose(mrm.aggregate(), np.mean(x, axis=0))

    rstd = ElementWiseRunningStatistic(RunningSTD)
    for xx in x:
        rstd.observe(xx)
        print(xx)
    assert np.allclose(rstd.aggregate(), np.std(x, axis=0))
    print(rstd.aggregate(),np.std(x,axis=0))

    # mrrho = ElementWiseRunningStatistic(RunningRho)
    # for xx, yy in zip(x, y):
    #     mrrho.observe(xx, yy)
    # agged = mrrho.aggregate()
    #
    # for i, j in itertools.product(*list(map(range, xx.shape))):
    #     rhoij = rho(x[:, i, j].flatten(), y[:, i, j].flatten())
    #     assert np.allclose(agged[i, j], rhoij)

if __name__=="__main__":
    main()


text:
import os
import numpy as np
import torch
import pickle
from PIL import Image
from utils.misc import AverageMeter, evaluate_incremental, freeze_bn
from utils.segmentor import Segmentor
import torchvision.transforms.functional as F
import cv2

class Validator():
    def __init__(self, data_loader, n_classes=19,
                 save_snapshot=False, extra_name_str=''):
        self.data_loader = data_loader
        self.n_classes = n_classes
        self.save_snapshot = save_snapshot
        self.extra_name_str = extra_name_str

    def run(self, net, optimizer, best_record, epoch,
            res_dir, f_handle, iter_max, writer=None):
        # the following code is written assuming that batch size is 1
        net.eval()
        segmentor = Segmentor( net, self.n_classes, colorize_fcn=None,
                n_slices_per_pass=10)

        confmat = np.zeros((self.n_classes, self.n_classes))
        for vi, data in enumerate(self.data_loader):
            if vi==iter_max: # TODO: I do this to save time but once everything
                #works, get rid of this break
                break

            img_slices, gt, slices_info = data
            gt = gt.squeeze_(0).numpy()
            prediction_tmp = segmentor.run_on_slices(
                img_slices.squeeze_(0), slices_info.squeeze_(0))

            if prediction_tmp.shape != gt.shape:
                prediction_tmp = prediction_tmp.astype(np.uint8)
                print(gt.shape)
                prediction_tmp = cv2.resize(prediction_tmp, gt.shape,
                        cv2.INTER_NEAREST)

            acc, acc_cls, mean_iu, fwavacc, confmat = evaluate_incremental(
                confmat, prediction_tmp, gt, self.n_classes)

            str2write = 'validating: %d / %d' % (vi + 1, iter_max)
            #str2write = 'validating: %d / %d' % (vi + 1, len(self.data_loader))
            print(str2write)
            f_handle.write(str2write + "\n")

        # Store confusion matrix
        confmatdir = '%s/val/confmat'%res_dir
        os.makedirs(confmatdir, exist_ok=True)
        with open(os.path.join(confmatdir, self.extra_name_str + str(epoch) + '_confmat.pkl'), 'wb') as confmat_file:
            pickle.dump(confmat, confmat_file)

        if self.save_snapshot:
            # save state
            snapshot_name = 'iter_%d_acc_%.5f_acc-cls_%.5f_mean-iu_%.5f_fwavacc_%.5f_lr_%.10f' % (
                epoch, acc, acc_cls, mean_iu, fwavacc, optimizer.param_groups[1]['lr'])
            torch.save(net.state_dict(), '%s/snap/%s.pth'%(res_dir, snapshot_name))
            torch.save( optimizer.state_dict(), '%s/snap/opt_%s.pth'%(res_dir,snapshot_name))

            # save perf of this state if it is the new best state
            if best_record['mean_iu'] < mean_iu:
                best_record['epoch'] = epoch
                best_record['acc'] = acc
                best_record['acc_cls'] = acc_cls
                best_record['mean_iu'] = mean_iu
                best_record['fwavacc'] = fwavacc
                best_record['snapshot'] = snapshot_name
                with open('%s/val/bestval.txt'%res_dir, 'w') as f:
                    f.write( str(best_record) + '\n\n')

            str2write = 'best record: iter: %d\tacc: %.5f\tacc_cls: %.5f\tmean_iu: %.5f\tfwavacc: %.5f]' % (
                    best_record['iter'], best_record['acc'], best_record['acc_cls'],
                    best_record['mean_iu'], best_record['fwavacc'])
            print(str2write)
            f_handle.write(str2write + "\n")

        str2write = 'Current    : iter: %d\tacc: %.5f\tacc_cls: %.5f\tmean_iu %.5f\tfwavacc %.5f' % (
                epoch, acc, acc_cls, mean_iu, fwavacc)
        print(str2write)
        f_handle.write(str2write + "\n")

        if writer is not None:
            writer.add_scalar('acc', acc, epoch)
            writer.add_scalar('acc_cls', acc_cls, epoch)
            writer.add_scalar('mean_iu', mean_iu, epoch)
            writer.add_scalar('fwavacc', fwavacc, epoch)

        net.train()
        #if 'freeze_bn' not in args or args.freeze_bn:
        freeze_bn(net)

        return mean_iu


text:
#!/usr/bin/env python

import logging
import re
from time import time
import numpy as np

from flask import Flask, request, jsonify
from os import getenv
import sentry_sdk

from zdialog import Request
from src.skill import AlexaPrizeSkill

from common.constants import CAN_CONTINUE_SCENARIO
from common.news import is_breaking_news_requested
from common.utils import get_topics

sentry_sdk.init(getenv("SENTRY_DSN"))
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)

app = Flask(__name__)

with open("./src/google-10000-english-no-swears.txt", "r") as f:
    UNIGRAMS = f.read().splitlines()[:1003]

@app.route("/respond", methods=["POST"])
def respond():
    st_time = time()
    dialogs = request.json["dialogs"]
    responses = []
    confidences = []
    attributes = []

    for i, dialog in enumerate(dialogs):
        attr = {}
        response = ""
        confidence = 0.0

        try:
            curr_uttr = dialog["utterances"][-1]
            logger.info(f"User uttr: {curr_uttr['text']}")
            cobot_topics = set(get_topics(dialog["utterances"][-1], which="cobot_topics"))
            cobot_dialogact_topics = set(get_topics(dialog["utterances"][-1], which="cobot_dialogact_topics"))
            news_cobot_dialogacts = {"Science_and_Technology", "Sports", "Politics"}
            news_cobot_topics = {"News"}
            about_news = (news_cobot_dialogacts & cobot_dialogact_topics) | (news_cobot_topics & cobot_topics)

            prev_bot_uttr = {}
            if len(dialog["utterances"]) > 1:
                prev_bot_uttr = dialog["utterances"][-2]
                about_news = about_news or is_breaking_news_requested(prev_bot_uttr, dialog["utterances"][-1])

            entities = []
            for ent in curr_uttr["annotations"].get("ner", []):
                if not ent:
                    continue
                ent = ent[0]
                if not (ent["text"].lower() == "alexa" and curr_uttr["text"].lower()[:5] == "alexa"):
                    if ent["text"].lower() == "trump":
                        entities.append("donald trump")
                    elif ent["text"].lower() == "putin":
                        entities.append("vladimir putin")
                    else:
                        entities.append(ent["text"].lower())

            for ent in curr_uttr["annotations"].get("cobot_nounphrases", []):
                if ent.lower() not in UNIGRAMS:
                    if ent in entities + ["I", "i"]:
                        pass
                    else:
                        if ent.lower() == "trump":
                            entities.append("donald trump")
                        elif ent.lower() == "putin":
                            entities.append("vladimir putin")
                        else:
                            entities.append(ent.lower())

            logger.info(f"Found entities: {entities}")

            if curr_uttr["annotations"].get("intent_catcher", {}).get("yes", {}).get("detected", 0) == 1:
                sent_text = "yes"
            else:
                sent_text = curr_uttr["text"].lower()

            if about_news or ("news" in curr_uttr["text"]) or ("new" in curr_uttr["text"]) or sent_text == "yes":
                news_info, mode, response = eval(
                    AlexaPrizeSkill.handle(Request(user_id=dialog["id"], message=sent_text, raw=entities)).message
                )
                response = re.sub(r"\n", " ", response)
                if "Unfortunately, I couldn't find anything relevant." in response:
                    confidence = 0.6
                elif ("Sorry, something went wrong." in response) or (
                    "Sign up for the Today’s WorldView newsletter." in response
                ):
                    response = ""
                    confidence = 0.0
                elif mode == "body":
                    confidence = 1.0
                    news_body = [f"The following news is from Washington Post: '{response}'."]
                    response = np.random.choice(news_body)
                    attr["mode"] = "body"
                elif mode == "entity" or mode == "headline":
                    confidence = 0.98
                    attr["can_continue"] = CAN_CONTINUE_SCENARIO
                    attr["mode"] = mode
                    offer_news = [
                        f"I've heard the following news from Washington Post: '{response}'. "
                        f"Do you want to hear more?",
                        f"I've read the following news from Washington Post: '{response}'. "
                        f"Do you want to hear more?",
                        f"Here is the news from Washington Post: '{response}'. " f"Do you want to hear more?",
                    ]
                    response = np.random.choice(offer_news)
                elif mode == "subtopic" or mode == "topic":
                    confidence = 0.0
                    attr["mode"] = "subtopic"
                    response = ""
                else:
                    # mode is "none"
                    attr["can_continue"] = CAN_CONTINUE_SCENARIO
                    attr["mode"] = None
                    confidence = 0.95
                    offer_news = [
                        f"I could not find some specific news. So, here is one of the latest news "
                        f"from Washington Post: '{response}'. Do you want to hear more?"
                    ]
                    response = np.random.choice(offer_news)
            else:
                response = ""
                confidence = 0.0
        except Exception as e:
            logger.exception(f"exception in news_skill {e}")
            with sentry_sdk.push_scope() as scope:
                dialog_replies = []
                for reply in dialog["utterances"]:
                    dialog_replies.append(reply["text"])
                # This will be changed only for the error caught inside and automatically discarded afterward
                scope.set_extra("dialogs", dialog_replies)
                sentry_sdk.capture_exception(e)

        logger.info(f"Response: {response}")
        responses.append(response)
        confidences.append(confidence)
        attributes.append(attr)

    total_time = time() - st_time
    logger.info(f"news_skill exec time: {total_time:.3f}s")
    return jsonify(list(zip(responses, confidences, attributes)))

if __name__ == "__main__":
    app.run(debug=False, host="0.0.0.0", port=3000)

max_stars_repo_name: stefanrer/commonbsecret

max_issues_repo_name: stefanrer/commonbsecret

max_forks_repo_name: stefanrer/commonbsecret


text:
from keras.layers import Flatten, Conv2D
from rl.agents import DQNAgent, CEMAgent
from rl.memory import SequentialMemory, EpisodeParameterMemory
from discrete_spatial_agent import DiscreteSpatial2DAgent

from centaur import *
from random import shuffle
from multi_agent import *
import sys
import hexagon_ui_api
import os
from square_grid import *
import numpy as np
from numpy.core.multiarray import *
import copy

# ______________________________________________________________________________________________________________________________
class EnvDef:
  centaur_name = 'centaur'
  game_name = '1'
  HASH_POOL = 10000
  NODE_FEATURE_COUNT = 5
  DECISION_ACTION_SPACE = 2
  SHORT_MEMORY_SIZE = 1
  MAX_ROUND = 2000
  CELL_FEATURE = 1
  MAX_GRID_LENGTH = 33
  SPATIAL_INPUT = (MAX_GRID_LENGTH, MAX_GRID_LENGTH)
  SPATIAL_OUTPUT = (MAX_GRID_LENGTH, MAX_GRID_LENGTH)

class AgentType:
  BoostDecision = 'BoostDecision'
  Attack = 'Attack'
  Boost = 'Boost'

# __________________________________________________________________________________________________________________________
class SuperCentaurPlayer(Aliostad):
  def __init__(self, name):
    Aliostad.__init__(self, name)
    self.reset_state()

  def reset_state(self):
    self.actions = {}
    self.current_move = None
    self.was_called = {}
    self.illegal_move_by_agents = {}

  #  just overriding for instrumentation purposes.
  def turnx(self, world):
    return Aliostad.turnx(self, world)

  def timeForBoost(self, world):
    """

    :type world: World
    :return:
    """
    #self.was_called[AgentType.BoostDecision] = True
    #return self.actions[AgentType.BoostDecision] == 1
    return np.random.uniform() < 0.23

  def move(self, playerView):
    mv = self.current_move
    return mv

  def getAttackFromCellId(self, world):
    self.was_called[AgentType.Attack] = True
    cellId = self.actions[AgentType.Attack]
    if cellId not in world.uberCells:
      self.illegal_move_by_agents[AgentType.Attack] = True
      print('illegal move: {}'.format(cellId))
      return None
    if not world.uberCells[cellId].canAttackOrExpand:
      self.illegal_move_by_agents[AgentType.Attack] = True
      print('illegal move: {}'.format(cellId))
      return None
    return cellId

# __________________________________________________________________________________________________________________________
class HierarchicalCentaurEnv(Env):
  def __init__(self):
    self.players = []
    self.game = None
    self._seed = 0
    self.centaur = None
    self.cell_count = 1
    self.resources = 100
    self.world = None
    self.leaderBoard = {}
    self.cellLeaderBoard = {}
    self.shortMemory = []

  def configure(self, *args, **kwargs):
    pass

  def seed(self, seed=None):
    self._seed = seed
    return [self._seed]

  def render(self, mode='human', close=False):
    pass

  def step(self, actions):
    for name in actions:
      self.centaur.actions[name] = actions[name]  # we can also deep copy

    self.centaur.current_move = self.centaur.movex(self.world)
    stats, isFinished = self.game.run_sync()
    info = self.game.board.get_cell_infos_for_player(EnvDef.centaur_name)
    reward = -1
    if len(info) == 0 or self.game.round_no > EnvDef.MAX_ROUND:
      isFinished = True  # it has no cells anymore
    else:
      reward = (len(info) - self.cell_count) if self.centaur.was_called else 0
      self.cell_count = len(info)
    if self.game.round_no % 100 == 0:
      print(self.cell_count)

    if isFinished:
      winner = stats[0]
      reward = 1000 if winner.playerName == EnvDef.centaur_name else -1000
      if winner.playerName in self.leaderBoard:
        self.leaderBoard[winner.playerName] += 1
      else:
        self.leaderBoard[winner.playerName] = 1
      for stat in stats:
        if stat.playerName not in self.cellLeaderBoard:
          self.cellLeaderBoard[stat.playerName] = 0
        self.cellLeaderBoard[stat.playerName] += stat.cellsOwned
        print('{} {} ({})'.format(stat.playerName, stat.cellsOwned, stat.totalResources))

      for name in self.leaderBoard:
        print(' - {}: {} ({})'.format(name, self.leaderBoard[name], self.cellLeaderBoard[name]))

    playerView = PlayerView(self.game.round_no, info)
    wrld = self.centaur.build_world(playerView.ownedCells)
    self.push_world(wrld)
    rewards = {name: -50 if name in self.centaur.illegal_move_by_agents else reward for name in self.centaur.was_called}
    self.centaur.reset_state()
    return wrld, rewards, isFinished, {}

  def push_world(self, world):
    """

    :type world: World
    :return:
    """
    self.world = world
    self.shortMemory.append(world)
    if len(self.shortMemory) > EnvDef.SHORT_MEMORY_SIZE:
      del self.shortMemory[0]

  def close(self):
    print('closing CentaurEnv')

  def reset(self):
    self.cell_count = 1
    self.resources = 100
    if self.game is not None:
      self.game.finish()

    self.shortMemory = []
    for i in range(0, EnvDef.SHORT_MEMORY_SIZE):
      self.shortMemory.append(World([]))

    self.centaur = SuperCentaurPlayer(EnvDef.centaur_name)
    self.players = [Aliostad('ali'), Aliostad('random80', 0.80), self.centaur, Aliostad('random30', 0.3),
                    Aliostad('random60', 0.6), Aliostad('random70', 0.7)]
    shuffle(self.players)
    self.game = Game(EnvDef.game_name, self.players, radius=11)
    hexagon_ui_api.games[EnvDef.game_name] = self.game
    self.game.start()
    playerView = PlayerView(self.game.round_no, self.game.board.get_cell_infos_for_player(EnvDef.centaur_name))
    wrld = self.centaur.build_world(playerView.ownedCells)
    self.push_world(wrld)
    return wrld

# ____________________________________________________________________________________________________________________________
class CentaurDecisionProcessor(Processor):

  @staticmethod
  def calculate_hash_index(cellName):
    # type: (str) -> int
    return int(abs(hash(cellName))) % EnvDef.HASH_POOL

  def buildInput(self, world):
    """
    Each input has 5 values:
      - first 4 items is 1-hot-vector:
        - No cell (we do not see)
        - My cell
        - Enemy cell
        - Not-captured cell
      - Resources

    :type world:
    :return:
    """
    inpt = [np.array([1, 0, 0, 0, 0]) for i in range(0, EnvDef.HASH_POOL)]

    for cell in world.cells.values():
      id = self.calculate_hash_index(str(cell.id))
      inpt[id] = np.array([0, 1, 0, 0, cell.resources])
      for n in cell.neighbours:
        id = int(abs(hash(n.id))) % EnvDef.HASH_POOL
        if n.isOwned is None:  # neutral
          inpt[id] = np.array(
            [0, 0, 0, 1, cell.resources])  # resources would be 0 but better just to use resources property
        elif n.isOwned is False:  # enemy
          inpt[id] = np.array([0, 0, 1, 0, cell.resources])
    inpt = np.array(inpt).flatten()
    return inpt

  def process_action(self, action):
    return action  # this is due to a BUG in keras-rl when it tries to calc mean

  def process_observation(self, observation):
    """

    :type observation: World
    :return:
    """
    return self.buildInput(observation)

# ______________________________________________________________________________________________________________________________
class CentaurAttackProcessor(Processor):
  def __init__(self, masking=True):
    self.masking = masking
    self.last_world = None

  def buildInput(self, world):
    """
    returns a MxN map of the world with hexagon grid transposed to square grid

    :type world: World
    :return:
    """
    self.last_world = world
    hector = np.zeros(EnvDef.SPATIAL_INPUT)
    for cid in world.worldmap:
      hid = GridCellId.fromHexCellId(cid)
      thid = hid.transpose(EnvDef.SPATIAL_INPUT[0] / 2, EnvDef.SPATIAL_INPUT[1] / 2)
      hector[thid.x][thid.y] = world.worldmap[cid]
    return hector

  def buildOutput(self, world):
    """
    returns a MxN map of the world with hexagon grid transposed to square grid

    :type world: World
    :return:
    """
    hector = np.zeros(EnvDef.SPATIAL_INPUT)
    for cid in world.uberCells:
      hid = GridCellId.fromHexCellId(cid)
      thid = hid.transpose(EnvDef.SPATIAL_INPUT[0] / 2, EnvDef.SPATIAL_INPUT[1] / 2)
      hector[thid.x][thid.y] = 0 if not world.uberCells[cid].canAttackOrExpand else world.uberCells[cid].attackPotential
    return hector

  def process_action(self, action):
    """

    :type action: ndarray
    :return:
    """
    flat = action.flatten()
    idx = np.argmax(flat, 0)
    y = idx % action.shape[1]
    x = idx / action.shape[1]
    if idx == 0:
      print('zero!')
    thid = GridCellId(x, y).transpose(-(EnvDef.SPATIAL_INPUT[0] / 2), -(EnvDef.SPATIAL_INPUT[1] / 2))
    return thid.to_cell_id()

  @staticmethod
  def add_noise(arr, noise_range=0.3):
    """

    :type arr: ndarray
    :type noise_range: float
    :return:
    """
    shp = arr.shape
    flat = arr.flatten()
    noisy = np.array([value * np.random.uniform(1. - noise_range, 1. + noise_range) for value in flat])
    return noisy.reshape(shp)

  def process_and_mask(self, Y):
    """

    :type Y: ndarray
    :return:
    """
    shibo = AttackModel.process_y(Y)
    real_shibo = shibo
    if self.masking and self.last_world is not None:
      if min(shibo.flat) == sum(shibo.flat):
        print('all zero output')
        return self.add_noise(self.buildOutput(self.last_world))
      mask = np.zeros(shibo.shape)
      for cid in self.last_world.uberCells:
        if self.last_world.uberCells[cid].canAttackOrExpand:
          hid = GridCellId.fromHexCellId(cid)
          thid = hid.transpose(EnvDef.SPATIAL_INPUT[0] / 2, EnvDef.SPATIAL_INPUT[1] / 2)
          mask[thid.x][thid.y] = 1
      minimum = min(shibo.flatten())
      if minimum < 0:  # rescale to zero
        shibo += -minimum
      real_shibo = shibo * mask
      if sum(real_shibo.flat) == 0:
        print('all zero output after masking')
        return self.add_noise(self.buildOutput(self.last_world))
    return real_shibo

  def process_observation(self, observation):
    """

    :type observation: World
    :return:
    """
    return self.buildInput(observation)

class CentaurBoostProcessor(CentaurAttackProcessor):

  def __init__(self, masking=True):
    CentaurAttackProcessor.__init__(self, masking)

  def process_and_mask(self, Y):
    """

    :type Y: ndarray
    :return:
    """
    shibo = AttackModel.process_y(Y)
    real_shibo = shibo
    if self.masking and self.last_world is not None:
      mask = np.zeros(Y.shape)
      for cid in self.last_world.uberCells:
        hid = GridCellId.fromHexCellId(cid)
        thid = hid.transpose(EnvDef.SPATIAL_INPUT[0] / 2, EnvDef.SPATIAL_INPUT[1] / 2)
        mask[thid.x][thid.y] = 1
      real_shibo = shibo * mask
      if max(real_shibo.flatten()) == 0:
        print('what??')
    return real_shibo
# ______________________________________________________________________________________________________________________________

class DecisionModel:
  def __init__(self, modelName=None):
    """

    :type theMethod: str
    """
    self.modelName = modelName if modelName is not None else 'Decision_model_params.h5f' + str(r.uniform(0, 10000))
    model = Sequential()
    model.add(Flatten(input_shape=(1,) + (EnvDef.HASH_POOL * EnvDef.NODE_FEATURE_COUNT * EnvDef.SHORT_MEMORY_SIZE,)))
    model.add(Dense(32, activation="relu"))
    model.add(Dense(16, activation="relu"))
    model.add(Dense(EnvDef.DECISION_ACTION_SPACE))
    model.add(Activation('softmax'))
    print(model.summary())
    model.compile(loss="categorical_crossentropy",
                  optimizer='adadelta', metrics=['accuracy'])
    self.model = model

# ______________________________________________________________________________________________________________________________

class AttackModel:
  def __init__(self, modelName=None):
    """

    :type theMethod: str
    """
    self.modelName = modelName if modelName is not None else 'Attack_model_params.h5f' + str(r.uniform(0, 10000))

    model = Sequential()
    model.add(Conv2D(128, (3, 3), padding='same', activation='relu', input_shape=EnvDef.SPATIAL_INPUT + (1, )))
    model.add(Conv2D(16, (3, 3), padding='same', activation='relu'))
    model.add(Conv2D(4, (3, 3), padding='same', activation='relu'))
    model.add(Conv2D(1, (1, 1), padding='same', activation='relu'))

    if os.path.exists(self.modelName):
      model.load_weights(self.modelName)
    model.compile(loss='mean_squared_logarithmic_error', optimizer='adam')

    self.model = model

  @staticmethod
  def prepare_x(X, batch=False):
    """

    :type X: ndarray
    :type batch: bool
    :return:
    """
    shape = EnvDef.SPATIAL_INPUT + (1, )
    if batch:
      shape = (X.shape[0], ) + shape
    return np.reshape(X, shape)

  @staticmethod
  def prepare_y(Y, batch=False):
    """

    :type Y: ndarray
    :type batch: bool
    :return:
    """
    shape = EnvDef.SPATIAL_OUTPUT + (1, )
    if batch:
      shape = (Y.shape[0], ) + shape
    return np.reshape(Y, shape)

  @staticmethod
  def process_y(Y):
    """

    :type Y: ndarray
    :return:
    """
    return np.reshape(Y, EnvDef.SPATIAL_OUTPUT)

# ______________________________________________________________________________________________________________________________

if __name__ == '__main__':
  env = HierarchicalCentaurEnv()
  np.random.seed(42)
  env.seed(42)

  if len(sys.argv) > 2:
    method = sys.argv[2]

  prc = CentaurDecisionProcessor()
  dec_model = DecisionModel()
  attack_model = AttackModel('Attack_model_params.h5f')

  prc = MultiProcessor({AgentType.BoostDecision: prc, AgentType.Attack: CentaurAttackProcessor()})
  memory = EpisodeParameterMemory(limit=1000, window_length=1)
  decision_agent = CEMAgent(model=dec_model.model, nb_actions=EnvDef.DECISION_ACTION_SPACE, memory=memory,
                            batch_size=50, nb_steps_warmup=200, train_interval=50, elite_frac=0.05)

  decision_agent.compile()
  memory2 = EpisodeParameterMemory(limit=1000, window_length=1)
  attack_agent = DiscreteSpatial2DAgent(attack_model.model, x_preparation=AttackModel.prepare_x,
                                        y_preparation=AttackModel.prepare_y,
                                        y_processing=
                                        prc.inner_processors[AgentType.Attack].process_and_mask)

  agent = MultiAgent({AgentType.BoostDecision: decision_agent, AgentType.Attack: attack_agent}, processor=prc)

  hexagon_ui_api.run_in_background()
  if len(sys.argv) == 1:
    print('Usage: python centaur_ai_gym.py (train|test)')
  elif sys.argv[1] == 'train':
    agent.fit(env, nb_steps=300 * 1000, visualize=False, verbose=2, interim_filenames={AgentType.Attack: attack_model.modelName})
    agent.save_weights({AgentType.BoostDecision: dec_model.modelName, AgentType.Attack: attack_model.modelName}, overwrite=True)
  elif sys.argv[1] == 'test':
    agent.test(env, nb_episodes=100)
  else:
    print('argument not recognised: ' + sys.argv[1])

max_stars_repo_name: aliostad/hexagon-rl

max_issues_repo_name: aliostad/hexagon-rl

max_forks_repo_name: aliostad/hexagon-rl

reason: import numpy,from numpy