text:
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Copyright (c) 2022. Reda Bouadjenek, Deakin University                      +
#     Email:  reda.bouadjenek@deakin.edu.au                                    +
#                                                                              +
#  Licensed under the Apache License, Version 2.0 (the "License");             +
#   you may not use this file except in compliance with the License.           +
#    You may obtain a copy of the License at:                                  +
#                                                                              +
#                 http://www.apache.org/licenses/LICENSE-2.0                   +
#                                                                              +
#    Unless required by applicable law or agreed to in writing, software       +
#    distributed under the License is distributed on an "AS IS" BASIS,         +
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  +
#    See the License for the specific language governing permissions and       +
#    limitations under the License.                                            +
#                                                                              +
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import sys, os, h5py, json
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.python.keras.saving import hdf5_format

if __name__ == "__main__":
    if len(sys.argv) == 1:
        input_dir = '.'
        output_dir = '.'
    else:
        input_dir = os.path.abspath(sys.argv[1])
        output_dir = os.path.abspath(sys.argv[2])

    print("Using input_dir: " + input_dir)
    print("Using output_dir: " + output_dir)
    print(sys.version)
    print("Tensorflow version: " + tf.__version__)

    # Loading the model.
    model = 'model.h5'
    with h5py.File(model, mode='r') as f:
        model_loaded = hdf5_format.load_model_from_hdf5(f)
        vocab = list(f.attrs['vocab1'])
        vocab.extend(list(f.attrs['vocab2']))
        print(model_loaded.summary())
        try:
            answers = f.attrs['class_names']
        except:
            answers = ['yes', 'no']

    input_shape = model_loaded.input_shape
    image_size = np.array(input_shape[0][1:3])

    print('Size of inputs images: ' + str(image_size))

    # We now prepare the vocabulary that has been used.
    # Mapping tokens to integers
    token_to_num = keras.layers.StringLookup(vocabulary=vocab)
    # Mapping integers back to original tokens
    num_to_token = keras.layers.StringLookup(vocabulary=token_to_num.get_vocabulary(),
                                             invert=True)
    vocab_size = token_to_num.vocabulary_size()
    print(f"The size of the vocabulary ={token_to_num.vocabulary_size()}")
    print("Top 20 tokens in the vocabulary: ", token_to_num.get_vocabulary()[:20])

    # Read test dataset
    imgs_path_test = input_dir + '/simpsons_test/'
    q_val_file = imgs_path_test + 'questions.json'
    q_test = json.load(open(q_val_file))

    def preprocessing(questions, imgs_path):
        # Make sure the questions and annotations are alligned
        questions['questions'] = sorted(questions['questions'], key=lambda x: x['question_id'])
        q_out = []
        imgs_out = []
        q_ids = []
        # Preprocess questions
        for q in questions['questions']:
            # Preprocessing the question
            q_text = q['question'].lower()
            q_text = q_text.replace('?', ' ? ')
            q_text = q_text.replace('.', ' . ')
            q_text = q_text.replace(',', ' . ')
            q_text = q_text.replace('!', ' . ').strip()
            q_out.append(q_text)
            file_name = imgs_path + str(q['image_id']) + '.png'
            imgs_out.append(file_name)
            q_ids.append(q['question_id'])
        return imgs_out, q_out, q_ids

    imgs_test, q_test, q_ids_test = preprocessing(q_test, imgs_path_test)

    def encode_single_sample(img_file, q):
        ###########################################
        ##  Process the Image
        ##########################################
        # 1. Read image file
        img = tf.io.read_file(img_file)
        # 2. Decode the image
        img = tf.image.decode_jpeg(img, channels=3)
        # 3. Convert to float32 in [0, 1] range
        img = tf.image.convert_image_dtype(img, tf.float32)
        # 4. Resize to the desired size
        img = tf.image.resize(img, image_size)
        ###########################################
        ##  Process the question
        ##########################################
        # 5. Split into list of tokens
        word_splits = tf.strings.split(q, sep=" ")
        # 6. Map tokens to indices
        q = token_to_num(word_splits)
        # 7. Return an inputs to for the model
        return (img, q), 0

    # We define the batch size
    batch_size = 1
    # Define the test dataset
    test_dataset = tf.data.Dataset.from_tensor_slices((imgs_test, q_test))
    test_dataset = (test_dataset.map(encode_single_sample, num_parallel_calls=tf.data.AUTOTUNE)
                    .padded_batch(batch_size)
                    .prefetch(buffer_size=tf.data.AUTOTUNE)
                    )

    # Making predictions!
    y_proba = model_loaded.predict(test_dataset)
    y_predict = np.argmax(y_proba, axis=1)

    # Writting predictions to file.
    with open(os.path.join(output_dir, 'answers.txt'), 'w') as result_file:
        for i in range(len(y_predict)):
            result_file.write(str(q_ids_test[i]) + ',' + answers[y_predict[i]] + '\n')


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""Model parameter classes."""
import copy
import numpy as np
from astropy import units as u
from astropy.table import Table
from ..array import check_type

__all__ = ["Parameter", "Parameters"]

class Parameter:
    """
    Class representing model parameters.

    Note that the parameter value has been split into
    a factor and scale like this::

        value = factor x scale

    Users should interact with the ``value``, ``quantity``
    or ``min`` and ``max`` properties and consider the fact
    that there is a ``factor``` and ``scale`` an implementation detail.

    That was introduced for numerical stability in parameter and error
    estimation methods, only in the Gammapy optimiser interface do we
    interact with the ``factor``, ``factor_min`` and ``factor_max`` properties,
    i.e. the optimiser "sees" the well-scaled problem.

    Parameters
    ----------
    name : str
        Name
    factor : float or `~astropy.units.Quantity`
        Factor
    scale : float, optional
        Scale (sometimes used in fitting)
    unit : `~astropy.units.Unit` or str, optional
        Unit
    min : float, optional
        Minimum (sometimes used in fitting)
    max : float, optional
        Maximum (sometimes used in fitting)
    frozen : bool, optional
        Frozen? (used in fitting)
    """

    __slots__ = ["_name", "_factor", "_scale", "_unit", "_min", "_max", "_frozen"]

    def __init__(
        self, name, factor, unit="", scale=1, min=np.nan, max=np.nan, frozen=False
    ):
        self.name = name
        self.scale = scale

        if isinstance(factor, u.Quantity) or isinstance(factor, str):
            self.quantity = factor
        else:
            self.factor = factor
            self.unit = unit

        self.min = min
        self.max = max
        self.frozen = frozen

    @property
    def name(self):
        """Name (str)."""
        return self._name

    @name.setter
    def name(self, val):
        self._name = check_type(val, "str")

    @property
    def factor(self):
        """Factor (float)."""
        return self._factor

    @factor.setter
    def factor(self, val):
        self._factor = float(val)

    @property
    def scale(self):
        """Scale (float)."""
        return self._scale

    @scale.setter
    def scale(self, val):
        self._scale = float(val)

    @property
    def unit(self):
        """Unit (`~astropy.units.Unit`)."""
        return self._unit

    @unit.setter
    def unit(self, val):
        self._unit = u.Unit(val)

    @property
    def min(self):
        """Minimum (float)."""
        return self._min

    @min.setter
    def min(self, val):
        self._min = float(val)

    @property
    def factor_min(self):
        """Factor min (float).

        This ``factor_min = min / scale`` is for the optimizer interface.
        """
        return self.min / self.scale

    @property
    def max(self):
        """Maximum (float)."""
        return self._max

    @max.setter
    def max(self, val):
        self._max = float(val)

    @property
    def factor_max(self):
        """Factor max (float).

        This ``factor_max = max / scale`` is for the optimizer interface.
        """
        return self.max / self.scale

    @property
    def frozen(self):
        """Frozen? (used in fitting) (bool)."""
        return self._frozen

    @frozen.setter
    def frozen(self, val):
        self._frozen = check_type(val, "bool")

    @property
    def value(self):
        """Value = factor x scale (float)."""
        return self._factor * self._scale

    @value.setter
    def value(self, val):
        self._factor = float(val) / self._scale

    @property
    def quantity(self):
        """Value times unit (`~astropy.units.Quantity`)."""
        return self.value * self.unit

    @quantity.setter
    def quantity(self, val):
        val = u.Quantity(val)
        self.value = val.value
        self.unit = val.unit

    def __repr__(self):
        return (
            "Parameter(name={name!r}, value={value!r}, "
            "factor={factor!r}, scale={scale!r}, unit={unit!r}, "
            "min={min!r}, max={max!r}, frozen={frozen!r})"
        ).format(**self.to_dict())

    def to_dict(self):
        return dict(
            name=self.name,
            value=self.value,
            factor=self.factor,
            scale=self.scale,
            unit=self.unit.to_string("fits"),
            min=self.min,
            max=self.max,
            frozen=self.frozen,
        )

    def autoscale(self, method="scale10"):
        """Autoscale the parameters.

        Set ``factor`` and ``scale`` according to ``method``

        Available methods:

        * ``scale10`` sets ``scale`` to power of 10,
          so that abs(factor) is in the range 1 to 10
        * ``factor1`` sets ``factor, scale = 1, value``

        In both cases the sign of value is stored in ``factor``,
        i.e. the ``scale`` is always positive.

        Parameters
        ----------
        method : {'factor1', 'scale10'}
            Method to apply
        """
        if method == "scale10":
            value = self.value
            if value != 0:
                exponent = np.floor(np.log10(np.abs(value)))
                scale = np.power(10.0, exponent)
                self.factor = value / scale
                self.scale = scale
        elif method == "factor1":
            self.factor, self.scale = 1, self.value
        else:
            raise ValueError("Invalid method: {}".format(method))

class Parameters:
    """List of `Parameter`.

    Holds covariance matrix

    Parameters
    ----------
    parameters : list of `Parameter`
        List of parameters
    covariance : `~numpy.ndarray`, optional
        Parameters covariance matrix.
        Order of values as specified by `parameters`.
    apply_autoscale : bool, optional
        Flag for optimizers, if True parameters are autoscaled before the fit,
        see `~gammapy.utils.modeling.Parameter.autoscale`
    """

    def __init__(self, parameters, covariance=None, apply_autoscale=True):
        self._parameters = parameters
        self.covariance = covariance
        self.apply_autoscale = apply_autoscale

    def _init_covariance(self):
        if self.covariance is None:
            shape = (len(self.parameters), len(self.parameters))
            self.covariance = np.zeros(shape)

    def copy(self):
        """A deep copy"""
        return copy.deepcopy(self)

    @property
    def parameters(self):
        """List of `Parameter`."""
        return self._parameters

    @property
    def free_parameters(self):
        """List of free parameters"""
        return [par for par in self.parameters if not par.frozen]

    # TODO: replace this with a better API to update parameters
    @parameters.setter
    def parameters(self, vals):
        self._parameters = vals

    @property
    def names(self):
        """List of parameter names"""
        return [par.name for par in self.parameters]

    def __str__(self):
        ss = self.__class__.__name__
        for par in self.parameters:
            ss += "\n{}".format(par)
        ss += "\n\ncovariance: \n{}".format(self.covariance)
        return ss

    def _get_idx(self, val):
        """Get position index for a given parameter.

        The input can be a parameter object, parameter name (str)
        or if a parameter index (int) is passed in, it is simply returned.
        """
        if isinstance(val, int):
            return val
        elif isinstance(val, Parameter):
            for idx, par in enumerate(self.parameters):
                if val is par:
                    return idx
            raise IndexError("No parameter: {!r}".format(val))
        elif isinstance(val, str):
            for idx, par in enumerate(self.parameters):
                if val == par.name:
                    return idx
            raise IndexError("No parameter: {!r}".format(val))
        else:
            raise TypeError("Invalid type: {!r}".format(type(val)))

    def __getitem__(self, name):
        """Access parameter by name or index"""
        idx = self._get_idx(name)
        return self.parameters[idx]

    def to_dict(self):
        retval = dict(parameters=[], covariance=None)
        for par in self.parameters:
            retval["parameters"].append(par.to_dict())
        if self.covariance is not None:
            retval["covariance"] = self.covariance.tolist()
        return retval

    def to_table(self):
        """Convert parameter attributes to `~astropy.table.Table`."""
        t = Table()
        t["name"] = [p.name for p in self.parameters]
        t["value"] = [p.value for p in self.parameters]
        if self.covariance is None:
            t["error"] = np.nan
        else:
            t["error"] = [self.error(idx) for idx in range(len(self.parameters))]

        t["unit"] = [p.unit.to_string("fits") for p in self.parameters]
        t["min"] = [p.min for p in self.parameters]
        t["max"] = [p.max for p in self.parameters]
        t["frozen"] = [p.frozen for p in self.parameters]

        for name in ["value", "error", "min", "max"]:
            t[name].format = ".3e"

        return t

    @classmethod
    def from_dict(cls, val):
        pars = []
        for par in val["parameters"]:
            pars.append(
                Parameter(
                    name=par["name"],
                    factor=float(par["value"]),
                    unit=par["unit"],
                    min=float(par["min"]),
                    max=float(par["max"]),
                    frozen=par["frozen"],
                )
            )
        try:
            covariance = np.array(val["covariance"])
        except KeyError:
            covariance = None

        return cls(parameters=pars, covariance=covariance)

    def covariance_to_table(self):
        """Convert covariance matrix to `~astropy.table.Table`."""
        if self.covariance is None:
            raise ValueError("No covariance available")

        table = Table()
        table["name"] = self.names
        for idx, par in enumerate(self.parameters):
            vals = self.covariance[idx]
            table[par.name] = vals
            table[par.name].format = ".3e"
        return table

    @property
    def _ufloats(self):
        """
        Return dict of ufloats with covariance
        """
        from uncertainties import correlated_values

        values = [_.value for _ in self.parameters]

        try:
            # convert existing parameters to ufloats
            uarray = correlated_values(values, self.covariance)
        except np.linalg.LinAlgError:
            raise ValueError("Covariance matrix not set.")

        upars = {}
        for par, upar in zip(self.parameters, uarray):
            upars[par.name] = upar

        return upars

    # TODO: deprecate or remove this?
    def set_parameter_errors(self, errors):
        """
        Set uncorrelated parameters errors.

        Parameters
        ----------
        errors : dict of `~astropy.units.Quantity`
            Dict of parameter errors.
        """
        diag = []
        for par in self.parameters:
            error = errors.get(par.name, 0)
            error = u.Quantity(error, par.unit).value
            diag.append(error)
        self.covariance = np.diag(np.power(diag, 2))

    # TODO: this is a temporary solution until we have a better way
    # to handle covariance matrices via a class
    def error(self, parname):
        """Get parameter error.

        Parameters
        ----------
        parname : str, int
            Parameter name or index
        """
        if self.covariance is None:
            raise ValueError("Covariance matrix not set.")

        idx = self._get_idx(parname)
        return np.sqrt(self.covariance[idx, idx])

    # TODO: this is a temporary solution until we have a better way
    # to handle covariance matrices via a class
    def set_error(self, parname, err):
        """Set parameter error.

        Parameters
        ----------
        parname : str, int
            Parameter name or index
        err : float or Quantity
            Parameter error
        """
        self._init_covariance()

        idx = self._get_idx(parname)
        err = u.Quantity(err, self[idx].unit).value
        self.covariance[idx, idx] = err ** 2

    @property
    def correlation(self):
        r"""Correlation matrix (`numpy.ndarray`).

        Correlation :math:`C` is related to covariance :math:`\Sigma` via:

        .. math::
            C_{ij} = \frac{ \Sigma_{ij} }{ \sqrt{\Sigma_{ii} \Sigma_{jj}} }
        """
        err = np.sqrt(np.diag(self.covariance))
        return self.covariance / np.outer(err, err)

    def set_parameter_factors(self, factors):
        """Set factor of all parameters.

        Used in the optimizer interface.
        """
        for factor, parameter in zip(factors, self.parameters):
            if not parameter.frozen:
                parameter.factor = factor

    @property
    def _scale_matrix(self):
        scales = [par.scale for par in self.parameters]
        return np.outer(scales, scales)

    def _expand_factor_matrix(self, matrix):
        """Expand covariance matrix with zeros for frozen parameters"""
        idxs = np.where([par.frozen for par in self.parameters])[0]
        matrix = np.insert(matrix, idxs, 0, axis=1)
        matrix = np.insert(matrix, idxs, 0, axis=0)
        return matrix

    def set_covariance_factors(self, matrix):
        """Set covariance from factor covariance matrix.

        Used in the optimizer interface.
        """
        if not np.sqrt(matrix.size) == len(self.parameters):
            matrix = self._expand_factor_matrix(matrix)

        self.covariance = self._scale_matrix * matrix

    def autoscale(self, method="scale10"):
        """Autoscale all parameters.

        See :func:`~gammapy.utils.modelling.Parameter.autoscale`

        Parameters
        ----------
        method : {'factor1', 'scale10'}
            Method to apply
        """
        for par in self.parameters:
            par.autoscale(method)

    @property
    def restore_values(self):
        """Context manager to restore values.

        A copy of the values is made on enter,
        and those values are restored on exit.

        Examples
        --------
        ::

            from gammapy.spectrum.models import PowerLaw

            pwl = PowerLaw(index=2)
            with pwl.parameters.restore_values:
                pwl.parameters["index"].value = 3

            print(pwl.parameters["index"].value)
        """
        return restore_parameters_values(self)

class restore_parameters_values:
    def __init__(self, parameters):
        self.parameters = parameters
        self.values = [_.value for _ in parameters]

    def __enter__(self):
        pass

    def __exit__(self, type, value, traceback):
        for value, par in zip(self.values, self.parameters):
            par.value = value

ext: py

max_stars_repo_name: watsonjj/gammapy

max_issues_repo_name: watsonjj/gammapy

max_forks_repo_name: watsonjj/gammapy

reason: import numpy,from astropy


text:
import abc
import functools
import logging
from typing import Iterable, List, Any, Tuple, Dict, Callable

import numpy as np
import pandas as pd
import string_utils

from blessings import Terminal
from phdTester.ks001.ks001 import KS001
from phdTester import commons
from phdTester.common_types import PathStr, DataTypeStr, KS001Str
from phdTester.model_interfaces import ITestContextRepo, ITestContext, ITestContextMask, ITestContextRepoView, \
    IOptionDict, IStuffUnderTest, ITestEnvironment, IStuffUnderTestMask, ITestEnvironmentMask, \
    IGlobalSettings, ICsvRow, IDataWriter, IFunctionsDict, IDataContainerPathGenerator, ISubtitleGenerator, \
    ISlotValueFetcher, IDataRowConverter
from phdTester.option_dicts import StandardOptionDict, DynamicOptionDict, DefaultAnonymuousOptionObject

class PhdFormatter(logging.Formatter):
    """
    A logging formatter allowing to tweak the log format as I want

    What I want? the following:
     - log level is encoded by the color of the log;
     - the filename where the log comes from is truncated if too long

    """

    def __init__(self, fmt=None, datefmt=None, style='%', colors_dict: Dict[str, Callable[[str], str]] = None, limit_filename_size: int = None):
        super().__init__(fmt, datefmt, style)
        self.__limit_filename_size = limit_filename_size
        self.__colors = colors_dict

        self.__terminal = Terminal()

        if self.__colors is None:
            self.__colors = {}
            self.__colors['DEBUG'] = self.__terminal.cyan
            self.__colors['INFO'] = self.__standard_print
            self.__colors['WARN'] = self.__terminal.yellow
            self.__colors['ERROR'] = self.__terminal.red
            self.__colors['CRITICAL'] = self.__terminal.red

    def __standard_print(self, x: str) -> str:
        return x

    def format(self, record: logging.LogRecord):
        # colorized logs
        try:
            func = self.__colors[record.levelname]
        except KeyError:
            func = self.__standard_print

        # remove .py extension in filename
        record.filename = record.filename[:-3]
        # add "..." on too long filenames
        if self.__limit_filename_size is not None and len(record.filename) > self.__limit_filename_size:
            record.filename = record.filename[:(self.__limit_filename_size-3)] + '...'

        result = super().format(record)
        return func(result)

class UpperBoundSlotValueFetcher(ISlotValueFetcher):
    """
    Slot value which always return the upperbound of an interval, regardless of its inclusion
    """

    def fetch(self, lb: float, ub: float, lb_included: bool, ub_included: bool) -> float:
        return ub

class MeanSlotValueFetcher(ISlotValueFetcher):
    """
    Slot value which always return the mean between lower and upper bound of an interval, regardles of their inclusions
    """

    def fetch(self, lb: float, ub: float, lb_included: bool, ub_included: bool) -> float:
        return (lb + ub)/2

class GnuplotDataWriter(IDataWriter):
    """
    Gnuplot sucks!
    """

    def __init__(self, filename: str, separator: str = " ", alias: str = "", carriage_return: str = "\n"):
        self.filename = filename
        self.alias = alias
        self.separator = separator
        self.carriage_return = carriage_return
        self._file = None

    def __enter__(self):
        self._file = open(self.filename, "w")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._file.close()

    def writeline(self, data: Iterable[Any]):
        if any(map(lambda x: isinstance(x, str), data)):
            return
        self._file.write(self.separator.join(map(lambda x: x.replace(self.separator, self.alias), map(lambda x: str(x), data))) + self.carriage_return)

class CsvDataWriter(IDataWriter):

    def __init__(self, filename: str, separator: str = ","):
        self.filename = filename
        self.separator = separator
        self._file = None

    def __enter__(self):
        self._file = open(self.filename, "w")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._file.close()

    def writeline(self, data: Iterable[Any]):
        self._file.write(self.separator.join(map(lambda x: x.replace(self.separator, ""), map(lambda x: str(x), data))) + "\n")

class DefaultSubtitleGenerator(ISubtitleGenerator):
    """
    Subtitle generattor which generate a string containing all the mask options which have a well defined values
    for the TestEnvironment
    """

    def fetch(self, tcm: "ITestContextMask") -> str:
        return tcm.te.get_have_value_string(ignore_some_tcm_keys=[])

class FixedPathGenerator(IDataContainerPathGenerator):
    """
    A path generator which will always return the given string, regardless of the ITestContextMask
    """

    __slots__ = ('__path', )

    def __init__(self, path: PathStr):
        self.__path = path

    def fetch(self, tcm: "ITestContextMask") -> PathStr:
        return self.__path

class IdentityDataRowConverter(IDataRowConverter):
    """
    Generates a DefaultCSVRow object which simply allows you to access the dictionary of the data row
    as you were calling instance fields.
    """

    def get_csv_row(self, d: Dict[str, str], path: "PathStr", name: "KS001Str", ks001: "KS001",
                    data_type: "DataTypeStr") -> "ICsvRow":
        return DefaultCSVRow()

class AbstractTestingGlobalSettings(IGlobalSettings, StandardOptionDict, abc.ABC):

    def __init__(self):
        IGlobalSettings.__init__(self)
        StandardOptionDict.__init__(self)

class DefaultGlobalSettings(IGlobalSettings, DynamicOptionDict):
    """
    A global settings implementation when the user do not want the help of the content assist

    This allows you to avoid generating the class containing the global settings yourself
    """
    def __init__(self):
        IGlobalSettings.__init__(self)
        DynamicOptionDict.__init__(self)

class AbstractStuffUnderTest(IStuffUnderTest, StandardOptionDict, abc.ABC):

    def __init__(self):
        IStuffUnderTest.__init__(self)
        StandardOptionDict.__init__(self)

    @property
    def key_alias(self) -> Dict[str, str]:
        return commons.generate_aliases(list(self.options()))

    @property
    def value_alias(self):
        return {}

class DefaultStuffUnderTest(IStuffUnderTest, DefaultAnonymuousOptionObject):
    """
    A stuff under test implementation when the user do not want the help of the content assist

    This allows you to avoid generating the class containing the stuff under test yourself
    """

    def __init__(self, fields: Iterable[str]):
        IStuffUnderTest.__init__(self)
        DefaultAnonymuousOptionObject.__init__(self, fields)

    @property
    def key_alias(self) -> Dict[str, str]:
        return commons.generate_aliases(list(self.options()))

    @property
    def value_alias(self) -> Dict[str, str]:
        return {}

class AbstractTestingEnvironment(ITestEnvironment, StandardOptionDict, abc.ABC):

    def __init__(self):
        ITestEnvironment.__init__(self)
        StandardOptionDict.__init__(self)

    @property
    def key_alias(self) -> Dict[str, str]:
        return commons.generate_aliases(list(self.options()))

    @property
    def value_alias(self) -> Dict[str, str]:
        return {}

class DefaultTestEnvironment(ITestEnvironment, DefaultAnonymuousOptionObject):
    """
    A global settings implementation when the user do not want the help of the content assist

    This allows you to avoid generating the class containing the global settings yourself
    """

    def __init__(self, fields: Iterable[str]):
        ITestEnvironment.__init__(self)
        DefaultAnonymuousOptionObject.__init__(self, fields)

    @property
    def key_alias(self) -> Dict[str, str]:
        return commons.generate_aliases(list(self.options()))

    @property
    def value_alias(self) -> Dict[str, str]:
        return {}

class AbstractTestContext(ITestContext, abc.ABC):

    def __init__(self, ut: "IStuffUnderTest", te: "ITestEnvironment"):
        ITestContext.__init__(self, ut=ut, te=te)

    @property
    @abc.abstractmethod
    def ut(self) -> "IStuffUnderTest":
        """
        Return the stuff under test

        This abstract property exists because in this way IDE (like PyCharm) can detect an abstract method to implement.
        In this way the developer can remember to implement this property. Furthermore, this is an opportunity for
        the developer to specify the type of IStuffUnderTest for her purpose. In the context of sorting algorithm,
        The developer can implement the property as follows:

        ```
        @property
        def te(self) -> "SortAlgorithm":
            return self._ut
        ```

        The implementation is normally just `return self._ut`: the important piece here is the annotation of the return
        type; this allows IDE (e.g., PyCharm) to help the developer when coding the tests

        :return: the test enviroment
        """
        pass

    @property
    @abc.abstractmethod
    def te(self) -> "ITestEnvironment":
        """
        Return the test environment

        This abstract property exists because in this way IDE (like PyCharm) can detect an abstract method to implement.
        In this way the developer can remember to implement this property. Furthermore, this is an opportunity for
        the developer to specify the type of ITestEnviroment for her purpose. In the context of sorting algorithm,
        The developer can implement the property as follows:

        ```
        @property
        def te(self) -> "SortEnvironment":
            return self._te
        ```

        The implementation is normally just `return self._te`: the important piece here is the annotation of the return
        type; this allows IDE (e.g., PyCharm) to help the developer when coding the tests

        :return: the test enviroment
        """
        pass

class DefaultTestContext(ITestContext):

    @property
    def ut(self) -> "IStuffUnderTest":
        return self._ut

    @property
    def te(self) -> "ITestEnvironment":
        return self._te

    def __init__(self, ut: IStuffUnderTest, te: ITestEnvironment):
        ITestContext.__init__(self, ut, te)

class AbstractStuffUnderTestMask(IStuffUnderTestMask, StandardOptionDict, abc.ABC):

    @abc.abstractmethod
    def __init__(self):
        IStuffUnderTestMask.__init__(self)

class DefaultStuffUnderTestMask(IStuffUnderTestMask, DefaultAnonymuousOptionObject):

    def __init__(self, fields: Iterable[str]):
        IStuffUnderTestMask.__init__(self)
        DefaultAnonymuousOptionObject.__init__(self, fields)

class AbstractTestEnvironmentMask(ITestEnvironmentMask, StandardOptionDict, abc.ABC):

    def __init__(self):
        ITestEnvironmentMask.__init__(self)

class DefaultTestEnvironmentMask(ITestEnvironmentMask, DefaultAnonymuousOptionObject):

    def __init__(self, fields: Iterable[str]):
        ITestEnvironmentMask.__init__(self)
        DefaultAnonymuousOptionObject.__init__(self, fields)

class AbstractTestContextMask(ITestContextMask, abc.ABC):

    def __init__(self, ut: "IStuffUnderTestMask", te: "ITestEnvironmentMask"):
        ITestContextMask.__init__(self, ut=ut, te=te)

class DefaultTestContextMask(ITestContextMask):

    def __init__(self, ut: "IStuffUnderTestMask", te: "ITestEnvironmentMask"):
        ITestContextMask.__init__(self, ut=ut, te=te)

    @property
    def ut(self) -> "IStuffUnderTestMask":
        return self._ut

    @property
    def te(self) -> "ITestEnvironmentMask":
        return self._te

def _query_by_mask(m: "ITestContextMask", iterable: Iterable["ITestContext"]) -> Iterable["ITestContext"]:
    for tc in iterable:
        if m.is_complaint_with_test_context(tc, list(iterable)):
            yield tc

class AbstractCSVRow(ICsvRow, StandardOptionDict, abc.ABC):
    """
    A csv row which natively implements all the IOptionDict methods by looking at  __dict__ public fields
    of the subtype
    """

    def __init__(self):
        ICsvRow.__init__(self)
        StandardOptionDict.__init__(self)

class DefaultCSVRow(ICsvRow, DefaultAnonymuousOptionObject):

    def __init__(self):
        ICsvRow.__init__(self)
        DefaultAnonymuousOptionObject.__init__(self, fields=[])

class SimpleTestContextRepoView(ITestContextRepoView):

    def __init__(self, values: Iterable[ITestContext], repo: "ITestContextRepo"):
        self.values = list(values)
        self._repo = repo

    def __iter__(self) -> Iterable[ITestContext]:
        return iter(self.values)

    def __getitem__(self, item: int) -> "ITestContext":
        return self.values[item]

    @property
    def repository(self) -> "ITestContextRepo":
        return self._repo

    def query_by_mask(self, m: "ITestContextMask") -> "ITestContextRepoView":
        return SimpleTestContextRepoView(list(_query_by_mask(m, self.values)), self._repo)

    def __len__(self) -> int:
        return len(self.values)

class SimpleTestContextRepo(ITestContextRepo):

    def __init__(self):
        ITestContextRepo.__init__(self)
        self.repo: List["ITestContext"] = []

    def append(self, v: "ITestContext"):
        self.repo.append(v)

    def __iter__(self) -> Iterable[ITestContext]:
        return iter(self.repo)

    def query_by_mask(self, m: "ITestContextMask") -> "ITestContextRepoView":
        return SimpleTestContextRepoView(list(_query_by_mask(m, self.repo)), self)

    def query_by_finding_mask(self, m: "ITestContextMask") -> "ITestContextRepoView":
        """
        like query_by_mask but we autyomatically check if there is only one result and, if so, we return it
        :param m: the mask to apply
        :return:  the single element computed
        :raises ValueError: if the query returns 0 or more than 1 element
        """
        result = list(self.query_by_mask(m))
        if len(result) != 1:
            logging.critical("We obtained {} elements:\ntest context mask: {}\nelements:{}".format(len(result), str(m), "\n".join(map(str, result))))
            raise ValueError(f"we expected to have 1 element, not {len(result)}!")
        return result[0]

    def __len__(self) -> int:
        return len(self.repo)

    def clear(self):
        self.repo.clear()

max_stars_repo_name: Koldar/phdTester

max_issues_repo_name: Koldar/phdTester

max_forks_repo_name: Koldar/phdTester

reason: import numpy


text:
import time
import cv2
import numpy as np
import os
import pyaudio
import math
from PIL import Image, ImageEnhance, ImageFilter

# make input 720p
def make_640p(cap):
	cap.set(CV_CAP_PROP_FRAME_WIDTH,640)
	cap.set(CV_CAP_PROP_FRAME_HEIGHT,480)
	return cap

# play single tone
def playTone(volume,f,duration):
	fs=44100
	#innit pyaudio
	PyAudio = pyaudio.PyAudio

	# create sine wave
	samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)

	p = PyAudio()

	# for paFloat32 sample values must be in range [-1.0, 1.0]
	stream = p.open(format=pyaudio.paFloat32,
					channels=1,
					rate=fs,
					output=True)

	# stream = p.open(format = p.get_format_from_width(1),channels = 2,rate = sps,output = True)
	# print(samples)
	stream.write((samples*volume).tobytes())
	stream.stop_stream()
	stream.close()
	p.terminate()
	return samples

#Get the contours
def image2bin(image):

	# convert to binary
	_, thrash = cv2.threshold(image, 80, 255, cv2.THRESH_BINARY)

	kernel = np.ones((5,5),np.uint8)
	thrash = cv2.morphologyEx(thrash, cv2.MORPH_OPEN, kernel)
	thrash = cv2.morphologyEx(thrash, cv2.MORPH_CLOSE, kernel)
	# cv2.imshow("thrash",thrash)

	# get contours
	contours, _ = cv2.findContours(thrash.astype(np.uint8).copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
	return contours, thrash

# Get the masked image
def mask_red(frame):
	hsv_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
	# HSV
	low_red = np.array([140,80,50])
	high_red = np.array([190,255,255])
	#masking
	mask= cv2.inRange(hsv_frame,low_red,high_red)
	# mask=cv2.bitwise_not(mask)
	return mask

def obj_tune(shape,dur):
	x1 ,y1, w, h = cv2.boundingRect(clay)
	# print (x1 ,y1, w, h,"\n")
	blank_img=np.zeros([width, height],np.float32)
	cv2.fillPoly(blank_img, [clay], 255)
	# playTone(h/200,height-y1,.5)
	return sample

def object_position(contours):
	count=0
	obj_pos=[]
	for contour in contours:
		approx = cv2.approxPolyDP(contour, 0.001* cv2.arcLength(contour, True), True)
		x1 ,y1, w, h = cv2.boundingRect(approx)
		obj_pos.append([x1 ,y1, w, h])
		count += 1
	print(obj_pos,count)

	# sorted according to starting points
	obj_pos.sort(key=lambda x: x[0])
	print(obj_pos,count)

	return obj_pos,count

def find_limits(conture,copy,x_pos,y,h):
	bottom=-1
	top=-1
	print ("scanning",x_pos)
	for y_pos in range(h):

		# testing
		# copy = cv2.circle(copy, (x_pos,y+y_pos), radius=0, color=(50,50, 50), thickness=-1)
		# cv2.imshow("Scanning...",copy)

		if cv2.waitKey(10) & 0xFF == ord('q'):
			break
		if conture[y+y_pos,x_pos]>0:
			# print("\n found top",x_pos,y+y_pos)
			top=y+y_pos
			break
	# print ("scanning bottom")
	for y_pos_r in range(h):

		# testing
		# copy = cv2.circle(copy, (x_pos,y+h-y_pos_r), radius=0, color=(100,100, 100), thickness=-1)
		# cv2.imshow("Scanning...",copy)

		if cv2.waitKey(10) & 0xFF == ord('q'):
			break
		if conture[y+h-y_pos_r,x_pos]==255:
			# print("\n found bottom",x_pos,y+h-y_pos_r)
			bottom=y+h-y_pos_r
			break
	return bottom,top
		# print("\n")

def main():
	camera_port = 3
	duration = 15

	# duration=input("Input Duration in seconds")
	# input("Enter your cam port (default = 0): ")

	duration = duration/640 # (per note)

	# turn on camera
	camera = cv2.VideoCapture(int(camera_port), cv2.CAP_DSHOW)
	time.sleep(0.1)
	# make_640p(camera)
	width, height = 640,480

	# test
	def color_pick(event,x,y,flags,params):
		frame_copy = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
		if event!=0:
			print (frame_copy[y,x])
			print (frame_copy2[y,x])

	cv2.namedWindow("color_picker")
	cv2.setMouseCallback("color_picker",color_pick)

	index=0

	# testing
	frame = cv2.imread("Images/sample.png", cv2.IMREAD_COLOR)

	while camera.isOpened():
		#read from camera
		# _ ,frame= camera.read()
		index += 1

		# every 10th frame
		if ((index%10)==0):

			cv2.imshow('Original', frame)

			# Get the masked image
			mask=mask_red(frame)
			# print ("frame no", index,"\n")

			#Get the contours
			frame_copy=frame.copy()
			cv2.imshow("color_picker", frame_copy)
			contours, thrash=image2bin(mask)

			# detect bounding box
			obj_pos,count =object_position(contours)

			# scan each object
			for shape_no in range(count):

				'''
				testing
				print ("Shape: ",shape_no+1)
				print(obj_pos[shape_no])
				frame_copy=cv2.rectangle(frame_copy,(obj_pos[shape_no][0],obj_pos[shape_no][1]),(obj_pos[shape_no][0]+obj_pos[shape_no][2],obj_pos[shape_no][1]+obj_pos[shape_no][3]),(100,100,100),2)
				cv2.imshow("box Show",frame_copy)
				'''

				# scan  left to right along x axis
				for x_pos in range(obj_pos[shape_no][0],obj_pos[shape_no][0]+obj_pos[shape_no][2]):

					# scan vertical line for start and end
					bottom,top= find_limits(thrash,frame_copy,x_pos,obj_pos[shape_no][1],obj_pos[shape_no][3])

					# test mark top, bottom
					# frame_copy = cv2.circle(frame_copy, (x_pos,top), radius=0, color=(100, 100, 0), thickness=-1)
					# frame_copy = cv2.circle(frame_copy, (x_pos,bottom), radius=0, color=(100, 100, 0), thickness=-1)

				print ("___________________________________")

			# innitalize all the streams of audio from different clays
			# for i_pos in range(count):
				# obj_tune

			del obj_pos
			cv2.waitKey(1000)
			# break
			if cv2.waitKey(2) & 0xFF == ord('q'):
				break

	camera.release()
	cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

max_stars_repo_name: soumyomkhj/clay-time

max_issues_repo_name: soumyomkhj/clay-time

max_forks_repo_name: soumyomkhj/clay-time


text:
# -*- coding: utf-8 -*-
"""
Chapter 02 script 02
"""

import numpy as np
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
#should see 2.0.0 or higher1.0
print(keras.__version__)
#should see 2.2.4 – tf

# the four different states of the XOR gate
training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")

# the four expected results in the same order
target_data = np.array([[0],[1],[1],[0]], "float32")

model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[2]))
model.add(keras.layers.Dense(16, activation = 'sigmoid'))
model.add(keras.layers.Dense(16, activation = 'sigmoid'))
model.add(keras.layers.Dense(1, activation = 'sigmoid'))

model.summary()

model.layers

hidden1 = model.layers[1]
hidden1.name

model.get_layer('dense') is hidden1

weights, biases = hidden1.get_weights()
print(weights)
print(weights.shape)

weights.shape

biases

biases.shape

model.compile(loss='mean_squared_error', optimizer='adam', metrics=['binary_accuracy'])

history = model.fit(training_data, target_data, epochs=750, verbose=2)

import pandas as pd
import matplotlib.pyplot as plt

pd.DataFrame(history.history).plot(figsize=(8, 5))
plt.grid(True)
plt.gca().set_ylim(0, 1)
plt.show()

print(model.predict(training_data).round())


text:
"""
ITU-R BT.601-7
==============

Defines the *ITU-R BT.601-7* opto-electrical transfer function (OETF)
and its inverse:

-   :func:`colour.models.oetf_BT601`
-   :func:`colour.models.oetf_inverse_BT601`

References
----------
-   :cite:`InternationalTelecommunicationUnion2011f` : International
    Telecommunication Union. (2011). Recommendation ITU-R BT.601-7 - Studio
    encoding parameters of digital television for standard 4:3 and wide-screen
    16:9 aspect ratios.
    http://www.itu.int/dms_pubrec/itu-r/rec/bt/\
R-REC-BT.601-7-201103-I!!PDF-E.pdf
"""

from __future__ import annotations

import numpy as np

from colour.algebra import spow
from colour.hints import FloatingOrArrayLike, FloatingOrNDArray
from colour.utilities import (
    as_float,
    domain_range_scale,
    from_range_1,
    to_domain_1,
)

__author__ = "Colour Developers"
__copyright__ = "Copyright (C) 2013-2022 - Colour Developers"
__license__ = "New BSD License - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "colour-developers@colour-science.org"
__status__ = "Production"

__all__ = [
    "oetf_BT601",
    "oetf_inverse_BT601",
]

def oetf_BT601(L: FloatingOrArrayLike) -> FloatingOrNDArray:
    """
    Defines *Recommendation ITU-R BT.601-7* opto-electronic transfer function
    (OETF).

    Parameters
    ----------
    L
        *Luminance* :math:`L` of the image.

    Returns
    -------
    :class:`numpy.floating` or :class:`numpy.ndarray`
        Corresponding electrical signal :math:`E`.

    Notes
    -----

    +------------+-----------------------+---------------+
    | **Domain** | **Scale - Reference** | **Scale - 1** |
    +============+=======================+===============+
    | ``L``      | [0, 1]                | [0, 1]        |
    +------------+-----------------------+---------------+

    +------------+-----------------------+---------------+
    | **Range**  | **Scale - Reference** | **Scale - 1** |
    +============+=======================+===============+
    | ``E``      | [0, 1]                | [0, 1]        |
    +------------+-----------------------+---------------+

    References
    ----------
    :cite:`InternationalTelecommunicationUnion2011f`

    Examples
    --------
    >>> oetf_BT601(0.18)  # doctest: +ELLIPSIS
    0.4090077...
    """

    L = to_domain_1(L)

    E = np.where(L < 0.018, L * 4.5, 1.099 * spow(L, 0.45) - 0.099)

    return as_float(from_range_1(E))

def oetf_inverse_BT601(E: FloatingOrArrayLike) -> FloatingOrNDArray:
    """
    Defines *Recommendation ITU-R BT.601-7* inverse opto-electronic transfer
    function (OETF).

    Parameters
    ----------
    E
        Electrical signal :math:`E`.

    Returns
    -------
    :class:`numpy.floating` or :class:`numpy.ndarray`
        Corresponding *luminance* :math:`L` of the image.

    Notes
    -----

    +------------+-----------------------+---------------+
    | **Domain** | **Scale - Reference** | **Scale - 1** |
    +============+=======================+===============+
    | ``E``      | [0, 1]                | [0, 1]        |
    +------------+-----------------------+---------------+

    +------------+-----------------------+---------------+
    | **Range**  | **Scale - Reference** | **Scale - 1** |
    +============+=======================+===============+
    | ``L``      | [0, 1]                | [0, 1]        |
    +------------+-----------------------+---------------+

    References
    ----------
    :cite:`InternationalTelecommunicationUnion2011f`

    Examples
    --------
    >>> oetf_inverse_BT601(0.409007728864150)  # doctest: +ELLIPSIS
    0.1...
    """

    E = to_domain_1(E)

    with domain_range_scale("ignore"):
        L = np.where(
            E < oetf_BT601(0.018),
            E / 4.5,
            spow((E + 0.099) / 1.099, 1 / 0.45),
        )

    return as_float(from_range_1(L))


text:
#
# Princeton University licenses this file to You under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.  You may obtain a copy of the License at:
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
#
#
# *********************************************  INTERFACE FUNCTIONS ***************************************************

'''

* InterfaceStateMap

'''

import numpy as np
import typecheck as tc

from psyneulink.core.components.functions.function import Function_Base
from psyneulink.core.globals.context import ContextFlags
from psyneulink.core.globals.keywords import \
    FUNCTION_OUTPUT_TYPE_CONVERSION, PARAMETER_STATE_PARAMS, STATE_MAP_FUNCTION, TRANSFER_FUNCTION_TYPE, \
    kwPreferenceSetName
from psyneulink.core.globals.preferences.componentpreferenceset import \
    PreferenceEntry, PreferenceLevel, is_pref_set, kpReportOutputPref

__all__ = ['InterfaceFunction', 'InterfaceStateMap']

class InterfaceFunction(Function_Base):
    """Simple functions for CompositionInterfaceMechanisms
    """
    componentType = TRANSFER_FUNCTION_TYPE

    def __init__(self,
                 default_variable,
                 params,
                 owner,
                 prefs,
                 context):
        super().__init__(default_variable=default_variable,
                         params=params,
                         owner=owner,
                         prefs=prefs,
                         context=context)

class InterfaceStateMap(InterfaceFunction):
    """
    Identity(                \
             default_variable, \
             params=None,      \
             owner=None,       \
             name=None,        \
             prefs=None        \
            )

    .. _Identity:

    Returns `variable <InterfaceStateMap.variable>`.

    Arguments
    ---------

    default_variable : number or np.array : default class_defaults.variable
        specifies a template for the value to be transformed.

    params : Dict[param keyword: param value] : default None
        a `parameter dictionary <ParameterState_Specification>` that specifies the parameters for the
        function.  Values specified for parameters in the dictionary override any assigned to those parameters in
        arguments of the constructor.

    owner : Component
        `component <Component>` to which to assign the Function.

    name : str : default see `name <Function.name>`
        specifies the name of the Function.

    prefs : PreferenceSet or specification dict : default Function.classPreferences
        specifies the `PreferenceSet` for the Function (see `prefs <Function_Base.prefs>` for details).

    Attributes
    ----------

    variable : number or np.array
        contains value to be transformed.

    owner : Component
        `component <Component>` to which the Function has been assigned.

    name : str
        the name of the Function; if it is not specified in the **name** argument of the constructor, a
        default is assigned by FunctionRegistry (see `Naming` for conventions used for default and duplicate names).

    prefs : PreferenceSet or specification dict : Function.classPreferences
        the `PreferenceSet` for function; if it is not specified in the **prefs** argument of the Function's
        constructor, a default is assigned using `classPreferences` defined in __init__.py (see :doc:`PreferenceSet
        <LINK>` for details).
    """

    componentName = STATE_MAP_FUNCTION

    classPreferences = {
        kwPreferenceSetName: 'LinearClassPreferences',
        kpReportOutputPref: PreferenceEntry(False, PreferenceLevel.INSTANCE),
    }

    paramClassDefaults = Function_Base.paramClassDefaults.copy()
    paramClassDefaults.update({
        FUNCTION_OUTPUT_TYPE_CONVERSION: True,
        PARAMETER_STATE_PARAMS: None
    })

    @tc.typecheck
    def __init__(self,
                 default_variable=None,
                 corresponding_input_state=None,
                 params=None,
                 owner=None,
                 prefs: is_pref_set = None):

        # Assign args to params and functionParams dicts
        params = self._assign_args_to_param_dicts(corresponding_input_state=corresponding_input_state,
                                                  params=params)

        super().__init__(default_variable=default_variable,
                         params=params,
                         owner=owner,
                         prefs=prefs,
                         context=ContextFlags.CONSTRUCTOR)

        # self.functionOutputType = None

    def function(
        self,
        variable=None,
        execution_id=None,
        params=None,
        context=None
    ):
        """
        Return: The item of `value <InterfaceStateMap.value>` whose index corresponds to the index of
        `corresponding_input_state <InterfaceStateMap.corresponding_input_state>` in `input_states
        <InterfaceStateMap.input_states>`

        Arguments
        ---------

        variable : number or np.array : default class_defaults.variable
           a single value or array to be transformed.

        corresponding_input_state : InputState : default None
            the InputState on the owner CompositionInterfaceMechanism to which this OutputState corresponds

        params : Dict[param keyword: param value] : default None
            a `parameter dictionary <ParameterState_Specification>` that specifies the parameters for the
            function.  Values specified for parameters in the dictionary override any assigned to those parameters in
            arguments of the constructor.

        Returns
        -------

        The item of `value <InterfaceStateMap.value>` whose index corresponds to the index of
        `corresponding_input_state <InterfaceStateMap.corresponding_input_state>` in `input_states
        <InterfaceStateMap.input_states>`

        """
        variable = self._check_args(variable=variable, execution_id=execution_id, params=params, context=context)

        index = self.corresponding_input_state.position_in_mechanism

        if self.corresponding_input_state.owner.parameters.value.get(execution_id) is not None:

            # If CIM's variable does not match its value, then a new pair of states was added since the last execution
            if not np.shape(self.corresponding_input_state.owner.get_input_values(execution_id)) == np.shape(self.corresponding_input_state.owner.parameters.value.get(execution_id)):
                return self.corresponding_input_state.owner.defaults.variable[index]

            # If the variable is 1D (e.g. [0. , 0.], NOT [[0. , 0.]]), and the index is 0, then return whole variable
            # np.atleast_2d fails in cases like var = [[0., 0.], [0.]] (transforms it to [[[0., 0.], [0.]]])
            if index == 0:
                if not isinstance(variable[0], (list, np.ndarray)):
                    return variable
            return variable[index]
        # CIM value = None, use CIM's default variable instead
        return self.corresponding_input_state.owner.defaults.variable[index]

    def _get_input_struct_type(self, ctx):
        #FIXME: Workaround for CompositionInterfaceMechanism that
        #       does not update its defaults shape
        from psyneulink.core.components.mechanisms.processing.compositioninterfacemechanism import CompositionInterfaceMechanism
        if hasattr(self.owner, 'owner') and isinstance(self.owner.owner, CompositionInterfaceMechanism):
            return ctx.get_output_struct_type(self.owner.owner.function)
        return ctx.get_input_struct_type(super())

    def _gen_llvm_function_body(self, ctx, builder, _1, _2, arg_in, arg_out):
        index = self.corresponding_input_state.position_in_mechanism
        val = builder.load(builder.gep(arg_in, [ctx.int32_ty(0), ctx.int32_ty(index)]))
        builder.store(val, arg_out)
        return builder

max_stars_repo_name: bdsinger/PsyNeuLink

max_issues_repo_name: bdsinger/PsyNeuLink

max_forks_repo_name: bdsinger/PsyNeuLink


text:
import numpy as np
import matplotlib.pyplot as plt
import math

# Robot Link Length Parameter
link = [50, 40, 35, 30]
# Robot Initial Joint Values (degree)
angle = [0, 0, 0, 0]
# Target End of Effector Position
target = [0, 0, 0]

# Create figure to plot
fig = plt.figure()
ax = fig.add_subplot(1,1,1)

# Draw Axis
def draw_axis(ax, scale=1.0, A=np.eye(4), style='-', draw_2d = False):
    xaxis = np.array([[0, 0, 0, 1],
                      [scale, 0, 0, 1]]).T
    yaxis = np.array([[0, 0, 0, 1],
                      [0, scale, 0, 1]]).T
    zaxis = np.array([[0, 0, 0, 1],
                      [0, 0, scale, 1]]).T

    xc = A.dot( xaxis )
    yc = A.dot( yaxis )
    zc = A.dot( zaxis )

    if draw_2d:
        ax.plot(xc[0,:], xc[1,:], 'r' + style)
        ax.plot(yc[0,:], yc[1,:], 'g' + style)
    else:
        ax.plot(xc[0,:], xc[1,:], xc[2,:], 'r' + style)
        ax.plot(yc[0,:], yc[1,:], yc[2,:], 'g' + style)
        ax.plot(zc[0,:], zc[1,:], zc[2,:], 'b' + style)

def rotateZ(theta):
    rz = np.array([[math.cos(theta), - math.sin(theta), 0, 0],
                   [math.sin(theta), math.cos(theta), 0, 0],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])
    return rz

def translate(dx, dy, dz):
    t = np.array([[1, 0, 0, dx],
                  [0, 1, 0, dy],
                  [0, 0, 1, dz],
                  [0, 0, 0, 1]])
    return t

# Forward Kinematics
# Input initial angles and length of links
# Output positions each points
def FK(angle, link):
    n_links = len(link)
    P = []
    P.append(np.eye(4))
    for i in range(0, n_links):
        R = rotateZ(angle[i]/180*math.pi)
        T = translate(link[i], 0, 0)
        P.append(P[-1].dot(R).dot(T))
    return P

def IK(target, angle, link, max_iter = 10000, err_min = 0.1):
    solved = False
    err_end_to_target = math.inf

    for loop in range(max_iter):
        for i in range(len(link)-1, -1, -1):
            P = FK(angle, link)
            end_to_target = target - P[-1][:3, 3]
            err_end_to_target = math.sqrt(end_to_target[0] ** 2 + end_to_target[1] ** 2)
            if err_end_to_target < err_min:
                solved = True
            else:
                # Calculate distance between i-joint position to end effector position
                # P[i] is position of current joint
                # P[-1] is position of end effector
                cur_to_end = P[-1][:3, 3] - P[i][:3, 3]
                cur_to_end_mag = math.sqrt(cur_to_end[0] ** 2 + cur_to_end[1] ** 2)
                cur_to_target = target - P[i][:3, 3]
                cur_to_target_mag = math.sqrt(cur_to_target[0] ** 2 + cur_to_target[1] ** 2)

                end_target_mag = cur_to_end_mag * cur_to_target_mag

                if end_target_mag <= 0.0001:
                    cos_rot_ang = 1
                    sin_rot_ang = 0
                else:
                    cos_rot_ang = (cur_to_end[0] * cur_to_target[0] + cur_to_end[1] * cur_to_target[1]) / end_target_mag
                    sin_rot_ang = (cur_to_end[0] * cur_to_target[1] - cur_to_end[1] * cur_to_target[0]) / end_target_mag

                rot_ang = math.acos(max(-1, min(1,cos_rot_ang)))

                if sin_rot_ang < 0.0:
                    rot_ang = -rot_ang

                # Update current joint angle values
                angle[i] = angle[i] + (rot_ang * 180 / math.pi)

                if angle[i] >= 360:
                    angle[i] = angle[i] - 360
                if angle[i] < 0:
                    angle[i] = 360 + angle[i]

        if solved:
            break

    return angle, err_end_to_target, solved, loop

# Have not implemented
def onclick(event):
    global target, link, angle, ax
    target[0] = event.xdata
    target[1] = event.ydata

    print("Target Position : ", target)
    plt.cla()
    ax.set_xlim(-50, 150)
    ax.set_ylim(-50, 150)

    # Inverse Kinematics
    angle, err, solved, iteration = IK(target, angle, link, max_iter=1000)

    P = FK(angle, link)
    for i in range(len(link)):
        start_point = P[i]
        end_point = P[i+1]
        ax.plot([start_point[0,3], end_point[0,3]], [start_point[1,3], end_point[1,3]], linewidth=5)
        # draw_axis(ax, scale=5, A=P[i+1], draw_2d=True)

    if solved:
        print("\nIK solved\n")
        print("Iteration :", iteration)
        print("Angle :", angle)
        print("Target :", target)
        print("End Effector :", P[-1][:3, 3])
        print("Error :", err)
    else:
        print("\nIK error\n")
        print("Angle :", angle)
        print("Target :", target)
        print("End Effector :", P[-1][:3, 3])
        print("Error :", err)
    fig.canvas.draw()

def main():
    fig.canvas.mpl_connect('button_press_event', onclick)
    fig.suptitle("Cyclic Coordinate Descent - Inverse Kinematics", fontsize=12)
    ax.set_xlim(-50, 150)
    ax.set_ylim(-50, 150)

    # Forward Kinematics
    P = FK(angle, link)
    # Plot Link
    for i in range(len(link)):
        start_point = P[i]
        end_point = P[i+1]
        ax.plot([start_point[0,3], end_point[0,3]], [start_point[1,3], end_point[1,3]], linewidth=5)
        # draw_axis(ax, scale=5, A=P[i+1], draw_2d=True)
    plt.show()

if __name__ == "__main__":
    main()


text:
"""keithley_2700
================

.. autoclass:: Keithley2700
   :members:
   :private-members:

"""

__all__ = ["Keithley2700"]

import numpy as np
import time

from fluiddyn.util.terminal_colors import cprint
from fluidlab.instruments.iec60488 import IEC60488
from fluidlab.instruments.features import SuperValue, BoolValue

class Keithley2700(IEC60488):
    """Driver for the multiplexer Keithley 2700 Series"""

    def __init__(self, interface=None):
        super().__init__(interface)
        self.Range = dict()
        self.NPLC = dict()

    def set_range(self, *, channelNumber=1, manualRange=False, rangeValue=None):
        if not manualRange and channelNumber in self.Range:
            del self.Range[channelNumber]
        elif manualRange:
            self.Range[channelNumber] = rangeValue

    def set_nplc(self, *, channelNumber=1, nplcValue=1.0):
        """This function sets the integration time of the ADC.

        :param channelNumber: the channel for which to set the NPLC value
        :type channelNumber: int
        :param nplcValue: Integration time, expressed in terms of line frequency (PLC). It spans from 0.01 to 10. The default PLC is 1.
        :type nplcValue: float

        .. note::

            Per the documentation, there is a relation between the integration
            time, the short name (fast, med, slow) and the effective number of digits

            ==========  ====  =============
            Short name  NPLC  Resolution
            ==========  ====  =============
            >Fast       0.01  3 1/2 digits
            Fast        0.1   5 1/2 digits
            Med         1.0   6 1/2 digits
            Slow        5.0
            ==========  ====  =============

        """
        nplcValue = float(nplcValue)
        self.NPLC[channelNumber] = nplcValue

    def scan(
        self, channelList, functionName, samplesPerChan, sampleRate, verbose
    ):
        """Initiates a scan.
        This method is called by the features get method.

        :param channelList: channel number or iterable of channel numbers
        :type channelList: int or list
        :param functionName: measurement function to configure. Some possible values are VOLT:DC, VOLT:AC, FRES, RES, CURR:DC or CURR:AC. Refer to Keithley documentation for other functions.
        :type functionName: str
        :param samplesPerChan: Number of samples to be acquired on each channel. They are stored in the device buffer during acquisition (maximum 450000). Defaults to 1.
        :type samplesPerChan: int
        :param sampleRate: frequency of the internal clock used to trigger measurements. The instrument resolution is 1 ms. Defaults to 1 kHz (maximum frequency).
        :type sampleRate: float
        :param verbose: prints additionnal information for debugging purposes
        :type verbose: bool

        .. note::

            If len(channelList) == 1 and samplesPerChan = 1, a one-shot
            measurement is performed, instead of a scan.

        """

        # Make sure channelList is iterable
        try:
            channelList[0]
        except TypeError:
            channelList = [channelList]

        # Check Front/Rear switch
        if len(channelList) > 1 and self.front.get() == True:
            raise ValueError(
                "Cannot get several channels while Front/Read switch is Front"
            )

        # Check number of points (max memory 450000 data points)
        if samplesPerChan * len(channelList) > 450_000:
            raise ValueError("Cannot request more than 450000 on Keithley 2700")

        # Check sampleRate
        if sampleRate:
            timeInterval = 1 / sampleRate
            if timeInterval < 1e-3:
                raise ValueError("The timer resolution is 1 ms")
        else:
            timeInterval = 1e-3

        if channelList == [1]:
            # Lecture en face avant
            chan = 1

            self.clear_status()
            self.interface.write("TRAC:CLE")  # clear buffer
            self.interface.write(
                "INIT:CONT OFF"
            )  # disable continuous initialisation
            self.interface.write(f'SENS:FUNC "{functionName}"')

            # Set range
            if chan in self.Range:
                self.interface.write(
                    "SENS:{func:}:RANG {rang:}".format(
                        func=functionName, rang=self.Range[chan]
                    )
                )
            else:
                self.interface.write(f"SENS:{functionName}:RANG:AUTO ON")

            # Set NPLC
            max_nplc = None
            if chan in self.NPLC:
                nplc = self.NPLC[chan]
            else:
                nplc = 1.0  # med (default value)
            if max_nplc is None or nplc > max_nplc:
                max_nplc = nplc
            self.interface.write(f"SENS:{functionName}:NPLC {nplc}")
            self.interface.write("FORM:ELEM READ,TST,CHAN")
            data = self.interface.query(f"READ?", time_delay=nplc / 50.0)
            start = time.monotonic()
            total_timeout = 5.0 + nplc / 50
            while not data.endswith("\n"):
                # time_delay was insufficiant
                time.sleep(0.1)
                data += self.interface.read()
                if time.monotonic() - start > total_timeout:
                    print("Timeout!")
                    break
            # print(data)
            parsed = data.split(",")
            values = parsed[0]
            return (float(values),)

        else:
            ListeChan = "(@" + ",".join([str(c) for c in channelList]) + ")"
            self.clear_status()
            self.interface.write("TRAC:CLE")  # clear buffer
            self.interface.write(
                "INIT:CONT OFF"
            )  # disable continuous initialisation

            # Set up the trigger subsystem
            if samplesPerChan > 1:
                self.interface.write("TRIG:SOUR TIM")
                self.interface.write(f"TRIG:TIM {timeInterval}")
            self.interface.write(f"TRIG:COUN {samplesPerChan}")
            self.interface.write("SAMP:COUN {:}".format(len(channelList)))

            # Measurement subsystem
            self.interface.write(f'SENS:FUNC "{functionName}", {ListeChan}')

            # Set range on specified channels
            for chan in channelList:
                if chan in self.Range:
                    self.interface.write(
                        "SENS:{func:}:RANG {rang:},(@{chan:})".format(
                            func=functionName, rang=self.Range[chan], chan=chan
                        )
                    )
                else:
                    self.interface.write(
                        "SENS:{func:}:RANG:AUTO ON,(@{chan:})".format(
                            func=functionName, chan=chan
                        )
                    )

            # Set NPLC
            max_nplc = None
            for chan in channelList:
                if chan in self.NPLC:
                    nplc = self.NPLC[chan]
                else:
                    nplc = 1.0  # med (default value)
                if max_nplc is None or nplc > max_nplc:
                    max_nplc = nplc
                self.interface.write(
                    "SENS:{func:}:NPLC {nplc:},(@{chan:})".format(
                        func=functionName, nplc=nplc, chan=chan
                    )
                )

            # Starts scan
            self.interface.write(f"ROUT:SCAN {ListeChan}")
            self.interface.write("ROUT:SCAN:TSO IMM")
            self.interface.write("ROUT:SCAN:LSEL INT")

            if samplesPerChan * len(channelList) > 1:
                self.interface.write("TRAC:CLE")  # clear buffer
                self.interface.write(
                    "TRAC:POIN {:}".format(samplesPerChan * len(channelList))
                )
                self.interface.write(
                    "TRAC:NOT {:}".format(samplesPerChan * len(channelList) - 1)
                )  # notify on nth reading
                self.interface.write("TRAC:FEED SENS; FEED:CONT NEXT")
                # self.interface.write("TRIG:COUN 1")
                # self.interface.write("SAMP:COUN {:}".format(len(channelList)))

                self.interface.write("STAT:PRES")  # Reset measure enable bits
                self.clear_status()  # *CLS
                self.interface.write(
                    "STAT:MEAS:ENAB 64"
                )  # Enable buffer bits B6 (buffer notify) (, 8, 9, 12, 13)
                self.event_status_enable_register.set(0)  # *ESE 0
                self.status_enable_register.set(1)  # *SRE 1

                self.interface.write("INIT:IMM")
                start_meas = time.monotonic()
                # self.wait_till_completion_of_operations()  # *OPC
                if samplesPerChan > 1:
                    tmo = 1000 * (samplesPerChan / sampleRate) * 1.5
                else:
                    tmo = 1000 * len(channelList) * max_nplc * (1 / 50) * 1.5
                tmo *= 10
                if tmo < 10e3:
                    tmo = 10e3
                # print("tmo =", tmo, "ms")
                try:
                    self.interface.wait_for_srq(timeout=tmo)
                except:
                    cprint.red("Error while waiting SRQ")
                else:
                    cprint.green(
                        "SRQ received after "
                        f"{time.monotonic() - start_meas:.1f} seconds"
                    )

                # Unassert SRQ
                self.clear_status()

                # Query number of points in buffer
                npoints = int(self.interface.query("TRAC:POIN?"))
                print("npoints =", npoints)

                # Fetch data
                self.interface.write("FORM:ELEM READ,TST,CHAN")
                self.interface.write("TRAC:DATA?")
                data = ""
                start_fetch = time.monotonic()
                while True:
                    try:
                        data += self.interface.read()
                        start_fetch = (
                            time.monotonic()
                        )  # reset if something was returned
                    except Exception:
                        print("Timeout reading on interface")
                    nread = len(data.split(",")) // 3
                    if nread == npoints:
                        cprint.green("All datapoints read")
                        break
                    time.sleep(0.5)
                    self.interface.write("TRAC:DATA?")
                    if time.monotonic() - start_fetch > 15:
                        cprint.red("Timeout fetching data")
                        break
            else:
                # self.interface.write("TRIG:COUN {:}".format(samplesPerChan))
                # self.interface.write("SAMP:COUN {:}".format(len(channelList)))
                self.interface.write("FORM:ELEM READ,TST,CHAN")
                data = self.interface.query("READ?")
                npoints = 1

            self.interface.write(":ROUT:SCAN:LSEL NONE")

            # Parsing data
            # print(data.strip())
            try:
                data = np.array([float(x) for x in data.split(",")])
            except ValueError:
                print("K2700 returned:", data)
                raise
            # print(data.size//3, "values returned")
            if data.size // 3 != npoints:
                raise ValueError("Not all points were fetched")
            values = data[::3]
            timestamps = data[1::3]
            channels = data[2::3]
            if values.size != timestamps.size:
                raise ValueError("Error while parsing")
            if channels.size != timestamps.size:
                raise ValueError("Error while parsing")
            if samplesPerChan > 1:
                # returns timeStamp, value for each channelList
                retval = list()
                for channum, chan in enumerate(channelList):
                    this_values = values[channum :: len(channelList)]
                    this_timestamps = timestamps[channum :: len(channelList)]
                    this_chans = channels[channum :: len(channelList)]
                    if not (this_chans == chan).all():
                        raise ValueError("Error while parsing")
                    retval.append(this_timestamps)
                    retval.append(this_values)
            else:
                # returns values only
                retval = values

            return retval

class Keithley2700Value(SuperValue):
    """Custom :class:`Value` class for the Keithley 2700 features."""

    def __init__(self, name, doc="", function_name=None):
        super().__init__(name, doc)
        self.function_name = function_name

    def _build_driver_class(self, Driver):
        name = self._name
        function_name = self.function_name

        setattr(Driver, name, self)

        def get(
            self, chanList=[1], samplesPerChan=1, sampleRate=None, verbose=None
        ):
            """Get """ + name
            if verbose is None:
                # default is verbose for acquisitions
                verbose = samplesPerChan > 1
            result = self._driver.scan(
                chanList, function_name, samplesPerChan, sampleRate, verbose
            )
            if len(result) == 1:
                result = result[0]
            return result

        self.get = get.__get__(self, self.__class__)

        def set(self, channel, value, warn=True):
            """Set """ + name
            # Makes sense for voltage on AO channels only
            if name == "vdc":
                self._driver.write_vdc(channel, value)
            else:
                raise ValueError("Specified value cannot be written")

        self.set = set.__get__(self, self.__class__)

features = [
    BoolValue(
        "front",
        doc="True if switch Front/Rear is on Front (read-only)",
        command_get=":SYST:FRSW?",
    ),
    Keithley2700Value("vdc", doc="DC voltage", function_name="VOLT:DC"),
    Keithley2700Value("vrms", doc="RMS voltage", function_name="VOLT:AC"),
    Keithley2700Value("ohm_4w", doc="4-wire resistance", function_name="FRES"),
    Keithley2700Value("ohm", doc="2-wire resistance", function_name="RES"),
    Keithley2700Value("idc", doc="DC current", function_name="CURR:DC"),
    Keithley2700Value("irms", doc="RMS current", function_name="CURR:AC"),
]

Keithley2700._build_class_with_features(features)

if __name__ == "__main__":
    from fluidlab.interfaces.gpib_inter import GPIBInterface

    with Keithley2700(GPIBInterface(0, 16)) as km:
        front = km.front.get()
        print("Front/Read switch:", front)
        if front:
            km.set_range(manualRange=False)
            km.set_nplc(nplcValue=10.0)
            v = km.vdc.get()
            print("v =", v)
        else:
            print("Single channel one-shot measurement")
            print(km.vdc.get(101))
            print("Multiple channel one-shot measurement")
            R1, R2 = km.ohm.get([101, 102])
            print(R1, R2)
            print("Single channel timeseries")
            ts, R = km.ohm.get(101, samplesPerChan=100, sampleRate=10.0)
            print("actual frame rate:", 1 / np.mean(ts[1:] - ts[:-1]), "Hz")
            import matplotlib.pyplot as plt

            plt.plot(ts, R, "o")
            plt.show()

max_stars_repo_name: fluiddyn/fluidlab

max_issues_repo_name: fluiddyn/fluidlab

max_forks_repo_name: fluiddyn/fluidlab


text:
import datetime as dt
import logging
import os
import sys
import time

import bokeh
import numpy as np
import pandas as pd
import param

from ..config import config
from ..models import terminal # noqa
from ..pane import HTML
from ..layout import Accordion, Column, Row, Tabs, FlexBox
from ..template import FastListTemplate
from ..widgets import MultiSelect, Terminal, TextInput
from ..widgets.indicators import Trend
from . import panel_logger
from .profile import profiling_tabs
from .server import set_curdoc
from .state import state

try:
    import psutil
    process = psutil.Process(os.getpid())
except Exception:
    process = None

log_sessions = []

class LogFilter(logging.Filter):

    def filter(self, record):
        if 'Session ' not in record.msg:
            return True
        session_id = record.args[0]
        if session_id in log_sessions:
            return False
        elif session_id not in session_filter.options:
            session_filter.options = session_filter.options + [session_id]
        if session_filter.value and session_id not in session_filter.value:
            return False
        if name_filter.value and name_filter.value not in record.name:
            return False
        return True

# Set up logging
log_handler = logging.StreamHandler()
log_handler.setLevel('DEBUG')
panel_logger.addHandler(log_handler)

log_filter = LogFilter()
log_handler.addFilter(log_filter)
formatter = logging.Formatter('%(asctime)s %(levelname)s: %(name)s - %(message)s')
log_handler.setFormatter(formatter)
log_terminal = Terminal(sizing_mode='stretch_both', min_height=400)
log_handler.setStream(log_terminal)

session_filter = MultiSelect(name='Filter by session', options=[])
name_filter = TextInput(name='Filter by component')

def get_mem():
    return pd.DataFrame([(time.time(), process.memory_info().rss/1024/1024)], columns=['time', 'memory'])

def get_cpu():
    return pd.DataFrame([(time.time(), process.cpu_percent())], columns=['time', 'cpu'])

def get_session_info():
    durations, renders, sessions = [], [], []
    session_info = state.session_info['sessions']
    for i, session in enumerate(session_info.values()):
        is_live = session['ended'] is None
        live = sum([
            1 for s in session_info.values()
            if s['launched'] < session['launched'] and
            (not s['ended'] or s['ended'] > session['launched'])
        ]) + 1
        if session['rendered'] is not None:
            renders.append(session['rendered']-session['started'])
        if not is_live:
            durations.append(session['ended']-session['launched'])
        duration = np.mean(durations) if durations else 0
        render = np.mean(renders) if renders else 0
        sessions.append((session['launched'], live, i+1, render, duration))
    if not sessions:
        i = -1
        duration = 0
        render = 0
    now = dt.datetime.now().timestamp()
    live = sum([
        1 for s in session_info.values()
        if s['launched'] < now and
        (not s['ended'] or s['ended'] > now)
    ])
    sessions.append((now, live, i+1, render, duration))
    return pd.DataFrame(sessions, columns=['time', 'live', 'total', 'render', 'duration'])

def overview():
    from panel import __version__
    df = get_session_info()
    active = Trend(
        data=df[['time', 'total']], plot_x='time', plot_y='total', plot_type='step',
        title='Total Sessions', width=300, height=300
    )
    total = Trend(
        data=df[['time', 'live']], plot_x='time', plot_y='live', plot_type='step',
        title='Active Sessions', width=300, height=300
    )
    render = Trend(
        data=df[['time', 'render']], plot_x='time', plot_y='render', plot_type='step',
        title='Avg. Time to Render (s)', width=300, height=300
    )
    duration = Trend(
        data=df[['time', 'duration']], plot_x='time', plot_y='duration', plot_type='step',
        title='Avg. Session Duration (s)', width=300, height=300
    )
    def update_session_info(event):
        df = get_session_info()
        for trend in layout.objects[:4]:
            trend.data = df[[trend.plot_x, trend.plot_y]]
    state.param.watch(update_session_info, 'session_info')
    layout = FlexBox(
        active,
        total,
        render,
        duration,
        margin=0,
        sizing_mode='stretch_width'
    )
    info = HTML(f"""

    Panel Server running on following versions:

    Python {sys.version.split('|')[0]}

    Panel: {__version__}

    Bokeh: {bokeh.__version__}

    Param: {param.__version__}

    """, width=300, height=300, margin=(0, 5))
    if process is None:
        layout.append(info)
        return layout

    memory = Trend(
        data=get_mem(), plot_x='time', plot_y='memory', plot_type='step',
        title='Memory Usage (MB)', width=300, height=300
    )
    cpu = Trend(
        data=get_cpu(), plot_x='time', plot_y='cpu', plot_type='step',
        title='CPU Usage (%)', width=300, height=300
    )
    def update_memory(): memory.stream(get_mem())
    def update_cpu(): cpu.stream(get_cpu())
    state.add_periodic_callback(update_memory, period=1000)
    state.add_periodic_callback(update_cpu, period=1000)

    layout.extend([memory, cpu, info])
    return layout

def log_component():
    return Column(
        Accordion(
            ('Filters', Row(
                session_filter,
                name_filter
            )),
            active=[],
            active_header_background='#444444',
            header_background='#333333',
            sizing_mode='stretch_width'
        ),
        log_terminal,
        sizing_mode='stretch_both'
    )

def admin_panel(doc):
    log_sessions.append(id(doc))
    template = FastListTemplate(title='Admin Panel', theme='dark')
    tabs = Tabs(
        ('Overview', overview()),
        margin=0,
        sizing_mode='stretch_both'
    )
    if config.profiler:
        tabs.append(
            ('Launch Profiling', profiling_tabs(state, r'^\/.*', None))
        )
    tabs.extend([
        ('User Profiling', profiling_tabs(state, None, r'^\/.*')),
        ('Logs', log_component())
    ])
    template.main.append(tabs)
    with set_curdoc(doc):
        template.server_doc(doc)
    return doc

max_stars_repo_name: nghenzi/panel

max_issues_repo_name: Stubatiger/panel

max_forks_repo_name: Stubatiger/panel


text:
from curve_analysis.curve_analysis import *
from curve_analysis.osu_dump import OsuDB
from curve_analysis.bin.config import DATA_PATH, SCORES_PATH

import numpy as np
import matplotlib.pyplot as plt
from pygam import PoissonGAM, LinearGAM, s, f
from scipy.optimize import curve_fit

if __name__ == '__main__':
    dump = OsuDB()

    print('EXTRACTING FROM DATA SETS')
    beatmaps = lex(np.array(dump.fetch_all('osu_beatmaps', ['beatmap_id', 'difficultyrating'])))

    print('- Generating lex tables for user scores & user pp')
    all_scores = np.load(SCORES_PATH + 'all_scores.npy')
    u_pp_lex = lex(np.load(DATA_PATH + 'u_pp_revised.npy'))

    acc_scores = all_scores[:, [-9, 1, 2, -1]]  # cols: mods, bm_id, user_id, acc
    acc_scores = lex(acc_scores)

    print('- Subsetting NM scores')
    acc_scores_subset = np.concatenate((lex_2d(lex_bounds(acc_scores, [0], get="all")),
                                        lex_2d(lex_bounds(acc_scores, [1], get="all"))), axis=0)
    acc_scores_subset = acc_scores_subset[:, 1:4]

    acc_scores_subset = acc_scores_subset.astype('float32')
    acc_scores_subset[:, 2] /= 1000000  # converting pp * 10^6 back to float pp
    acc_scores_subset = lex(acc_scores_subset)

    print('- Finding beatmaps w/ most samples')
    bm_samples = freq_keys(acc_scores_subset)[:100]

    N = 1000
    x = np.arange(0, 16000 - N)

    print('\nPLOTTING BEATMAPS')
    for bm_id in bm_samples[:10, 0]:
        stars = lex_bounds(beatmaps, [bm_id], get="one")[1]
        ranges = [[i, i + N] for i in range(0, 16000 - N)]
        data = bm_data(acc_scores_subset, u_pp_lex, bm_id, 2)

        prop_curve = proportion_curve(data, ranges, .95)

        plt.figure(figsize=(15, 6))
        plt.plot(np.arange(0, 16000 - N), prop_curve[:, 1])
        plt.show(block=False)

        prop_curve[prop_curve[:, 1] == 0] = -1
        props = prop_curve[:, 0] / prop_curve[:, 1]
        props = np.column_stack((x, props))
        props = props[prop_curve[:, 1] > 20].T

        plt.figure(figsize=(15, 6))

        plt.plot(props[0], props[1])

        # Choose a curve fitting method (Sigmoid only works on > 5* maps):

        # Curve fitting with pyGAM
        gam = LinearGAM(s(0, n_splines=13, constraints='monotonic_inc')).fit(props[0], props[1])
        plt.plot(x, gam.predict(x))

        # Curve fitting with sigmoid
        # popt, pcov = curve_fit(sigmoid, props[0], props[1], p0=[-.001, 1, -1])
        # a, b, k = popt
        # median = - b / a
        # plt.plot(x, sigmoid(x, *popt))
        # print('{} - Median: {}'.format(int(bm_id), median))

        plt.xlabel('Total PP')
        plt.ylabel('% Players')

        plt.title('{} ({:.1f}*): Proportion of players w/ 90% acc'.format(int(bm_id), stars))
        plt.show()

max_stars_repo_name: Cozieee/mlpp-playground

max_issues_repo_name: Cozieee/mlpp-playground

max_forks_repo_name: Cozieee/mlpp-playground

reason: import numpy,from scipy


text:
# coding: utf-8

"""Load SNP data, create SNP signatures

Author: Albert Chen - Vector Engineering Team (chena@broadinstitute.org)
"""

import io
import numpy as np
import pandas as pd

def process_snps(
    snp_files,
    # SNPs must occur at least this many times to pass filters
    count_threshold=3,
    mode="dna",  # dna, gene_aa, protein_aa
):

    # Dump all SNP chunks into a text buffer
    snp_df_io = io.StringIO()
    for i, chunk in enumerate(snp_files):
        with open(chunk, "r") as fp_in:
            for j, line in enumerate(fp_in):
                # Write the header of the first file
                # Or write any line that's not the header
                # (to avoid writing the header more than once)
                if (i == 0 and j == 0) or j > 0:
                    snp_df_io.write(line)

    # Read the buffer into a dataframe, then discard the buffer
    snp_df_io.seek(0)
    snp_df = pd.read_csv(snp_df_io)
    snp_df_io.close()

    snp_df = snp_df.fillna("-")

    groupby_cols = ["pos", "ref", "alt"]
    if mode == "gene_aa":
        groupby_cols.insert(0, "gene")
    elif mode == "protein_aa":
        groupby_cols.insert(0, "protein")

    # Collapse by Accession ID and count occurrences
    snp_count_df = (
        snp_df.groupby(groupby_cols, as_index=False)
        .count()
        .rename(columns={"Accession ID": "count"})
    )

    # Filter out low global occurrence SNPs
    valid_snps = (
        snp_count_df.loc[snp_count_df["count"] >= count_threshold, :]
        .reset_index(drop=True)
        .sort_values("count", ascending=False)
    )
    # Create unique SNP string
    valid_snps["snp_str"] = valid_snps[groupby_cols].applymap(str).agg("|".join, axis=1)

    # Generate SNP strings for initial dataframes
    # e.g., for DNA, its pos|ref|alt
    snp_df["snp_str"] = (
        snp_df[groupby_cols[0]]
        .astype(str)
        .str.cat([snp_df[col].astype(str) for col in groupby_cols[1:]], sep="|")
    )
    # Filter SNPs by valid SNPs
    snp_df = snp_df.loc[snp_df["snp_str"].isin(valid_snps["snp_str"]), :].reset_index(
        drop=True
    )
    # Group by Accession ID and make a ';' delimited list of snp_strs
    snp_group_df = snp_df.groupby("Accession ID")["snp_str"].agg(";".join).reset_index()

    # Map SNPs to integer IDs
    snp_map = pd.Series(
        np.unique(np.concatenate(snp_group_df["snp_str"].str.split(";").values).ravel())
    )

    # Flip index and values
    snp_map = pd.Series(snp_map.index.values, index=snp_map.values)

    # Convert SNP strings to integer lists
    snp_group_df["snp_str"] = (
        snp_group_df["snp_str"]
        .str.split(";")
        .apply(lambda x: ";".join([str(snp_map[a]) for a in x] if x else None))
    )

    return snp_group_df.set_index("Accession ID"), snp_map

max_stars_repo_name: apetkau/covidcg

max_issues_repo_name: apetkau/covidcg

max_forks_repo_name: apetkau/covidcg