text:
#  Version: 2020.02.21
#
#  MIT License
#
#  Copyright (c) 2018 Jiankang Deng and Jia Guo
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in all
#  copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
#  SOFTWARE.
#

import json
import os

import numpy as np
from easydict import EasyDict as edict

def load_property(data_dir):
    prop = edict()
    for line in open(os.path.join(data_dir, 'property')):
        vec = line.strip().split(',')
        assert len(vec) == 3
        prop.num_classes = int(vec[0])
        prop.image_size = [int(vec[1]), int(vec[2])]
    return prop

def get_dataset_webface(input_dir):
    clean_list_file = input_dir + "_clean_list.txt"
    ret = []
    for line in open(clean_list_file, 'r'):
        vec = line.strip().split()
        assert len(vec) == 2
        fimage = edict()
        fimage.id = vec[0].replace("\\", '/')
        fimage.classname = vec[1]
        fimage.image_path = os.path.join(input_dir, fimage.id)
        ret.append(fimage)
    return ret

def get_dataset_celeb(input_dir):
    clean_list_file = input_dir + "_clean_list.txt"
    ret = []
    dir2label = {}
    for line in open(clean_list_file, 'r'):
        line = line.strip()
        if not line.startswith('./m.'):
            continue
        line = line[2:]
        vec = line.split('/')
        assert len(vec) == 2
        if vec[0] in dir2label:
            label = dir2label[vec[0]]
        else:
            label = len(dir2label)
            dir2label[vec[0]] = label

        fimage = edict()
        fimage.id = line
        fimage.classname = str(label)
        fimage.image_path = os.path.join(input_dir, fimage.id)
        ret.append(fimage)
    return ret

def _get_dataset_celeb(input_dir):
    list_file = input_dir + "_original_list.txt"
    ret = []
    for line in open(list_file, 'r'):
        vec = line.strip().split()
        assert len(vec) == 2
        fimage = edict()
        fimage.id = vec[0]
        fimage.classname = vec[1]
        fimage.image_path = os.path.join(input_dir, fimage.id)
        ret.append(fimage)
    return ret

def get_dataset_facescrub(input_dir):
    ret = []
    label = 0
    person_names = []
    for person_name in os.listdir(input_dir):
        person_names.append(person_name)
    person_names = sorted(person_names)
    for person_name in person_names:
        subdir = os.path.join(input_dir, person_name)
        if not os.path.isdir(subdir):
            continue
        for _img in os.listdir(subdir):
            fimage = edict()
            fimage.id = os.path.join(person_name, _img)
            fimage.classname = str(label)
            fimage.image_path = os.path.join(subdir, _img)
            fimage.landmark = None
            fimage.bbox = None
            ret.append(fimage)
        label += 1
    return ret

def get_dataset_megaface(input_dir):
    ret = []
    label = 0
    for prefixdir in os.listdir(input_dir):
        _prefixdir = os.path.join(input_dir, prefixdir)
        for subdir in os.listdir(_prefixdir):
            _subdir = os.path.join(_prefixdir, subdir)
            if not os.path.isdir(_subdir):
                continue
            for img in os.listdir(_subdir):
                if not img.endswith('.jpg.jpg') and img.endswith('.jpg'):
                    fimage = edict()
                    fimage.id = os.path.join(prefixdir, subdir, img)
                    fimage.classname = str(label)
                    fimage.image_path = os.path.join(_subdir, img)
                    json_file = fimage.image_path + ".json"
                    data = None
                    fimage.bbox = None
                    fimage.landmark = None
                    if os.path.exists(json_file):
                        with open(json_file, 'r') as f:
                            data = f.read()
                            data = json.loads(data)
                        assert data is not None
                        if 'bounding_box' in data:
                            fimage.bbox = np.zeros((4,), dtype=np.float32)
                            bb = data['bounding_box']
                            fimage.bbox[0] = bb['x']
                            fimage.bbox[1] = bb['y']
                            fimage.bbox[2] = bb['x'] + bb['width']
                            fimage.bbox[3] = bb['y'] + bb['height']
                            # print('bb')
                        if 'landmarks' in data:
                            landmarks = data['landmarks']
                            if '1' in landmarks and '0' in landmarks and '2' in landmarks:
                                fimage.landmark = np.zeros((3, 2), dtype=np.float32)
                                fimage.landmark[0][0] = landmarks['1']['x']
                                fimage.landmark[0][1] = landmarks['1']['y']
                                fimage.landmark[1][0] = landmarks['0']['x']
                                fimage.landmark[1][1] = landmarks['0']['y']
                                fimage.landmark[2][0] = landmarks['2']['x']
                                fimage.landmark[2][1] = landmarks['2']['y']
                            # print('lm')

                    ret.append(fimage)
            label += 1
    return ret

def get_dataset_fgnet(input_dir):
    ret = []
    label = 0
    for subdir in os.listdir(input_dir):
        _subdir = os.path.join(input_dir, subdir)
        if not os.path.isdir(_subdir):
            continue
        for img in os.listdir(_subdir):
            if img.endswith('.JPG'):
                fimage = edict()
                fimage.id = os.path.join(_subdir, img)
                fimage.classname = str(label)
                fimage.image_path = os.path.join(_subdir, img)
                json_file = fimage.image_path + ".json"
                data = None
                fimage.bbox = None
                fimage.landmark = None
                if os.path.exists(json_file):
                    with open(json_file, 'r') as f:
                        data = f.read()
                        data = json.loads(data)
                    assert data is not None
                    if 'bounding_box' in data:
                        fimage.bbox = np.zeros((4,), dtype=np.float32)
                        bb = data['bounding_box']
                        fimage.bbox[0] = bb['x']
                        fimage.bbox[1] = bb['y']
                        fimage.bbox[2] = bb['x'] + bb['width']
                        fimage.bbox[3] = bb['y'] + bb['height']
                        # print('bb')
                    if 'landmarks' in data:
                        landmarks = data['landmarks']
                        if '1' in landmarks and '0' in landmarks and '2' in landmarks:
                            fimage.landmark = np.zeros((3, 2), dtype=np.float32)
                            fimage.landmark[0][0] = landmarks['1']['x']
                            fimage.landmark[0][1] = landmarks['1']['y']
                            fimage.landmark[1][0] = landmarks['0']['x']
                            fimage.landmark[1][1] = landmarks['0']['y']
                            fimage.landmark[2][0] = landmarks['2']['x']
                            fimage.landmark[2][1] = landmarks['2']['y']
                        # print('lm')

                # fimage.landmark = None
                ret.append(fimage)
        label += 1
    return ret

def get_dataset_ytf(input_dir):
    ret = []
    label = 0
    person_names = []
    for person_name in os.listdir(input_dir):
        person_names.append(person_name)
    person_names = sorted(person_names)
    for person_name in person_names:
        _subdir = os.path.join(input_dir, person_name)
        if not os.path.isdir(_subdir):
            continue
        for _subdir2 in os.listdir(_subdir):
            _subdir2 = os.path.join(_subdir, _subdir2)
            if not os.path.isdir(_subdir2):
                continue
            _ret = []
            for img in os.listdir(_subdir2):
                fimage = edict()
                fimage.id = os.path.join(_subdir2, img)
                fimage.classname = str(label)
                fimage.image_path = os.path.join(_subdir2, img)
                fimage.bbox = None
                fimage.landmark = None
                _ret.append(fimage)
            ret += _ret
        label += 1
    return ret

def get_dataset_clfw(input_dir):
    ret = []
    label = 0
    for img in os.listdir(input_dir):
        fimage = edict()
        fimage.id = img
        fimage.classname = str(0)
        fimage.image_path = os.path.join(input_dir, img)
        fimage.bbox = None
        fimage.landmark = None
        ret.append(fimage)
    return ret

def get_dataset_common(input_dir, min_images=1):
    ret = []
    label = 0
    person_names = []
    for person_name in os.listdir(input_dir):
        person_names.append(person_name)
    person_names = sorted(person_names)
    for person_name in person_names:
        _subdir = os.path.join(input_dir, person_name)
        if not os.path.isdir(_subdir):
            continue
        _ret = []
        for img in os.listdir(_subdir):
            if not img.endswith('.jpg') and not img.endswith('.png'):
                continue
            fimage = edict()
            fimage.id = os.path.join(person_name, img)
            fimage.classname = str(label)
            fimage.image_path = os.path.join(_subdir, img)
            fimage.bbox = None
            fimage.landmark = None
            _ret.append(fimage)
        if len(_ret) >= min_images:
            ret += _ret
            label += 1
    return ret

def get_dataset(name, input_dir):
    if name == 'webface' or name == 'lfw' or name == 'vgg':
        return get_dataset_common(input_dir)
    if name == 'celeb':
        return get_dataset_celeb(input_dir)
    if name == 'facescrub':
        return get_dataset_facescrub(input_dir)
    if name == 'megaface':
        return get_dataset_megaface(input_dir)
    if name == 'fgnet':
        return get_dataset_fgnet(input_dir)
    if name == 'ytf':
        return get_dataset_ytf(input_dir)
    if name == 'clfw':
        return get_dataset_clfw(input_dir)
    return None

max_stars_repo_name: drawdy/CompreFace

max_issues_repo_name: drawdy/CompreFace

max_forks_repo_name: drawdy/CompreFace


text:
from pyscf import lib
from automr import guess, autocas, mcpdft
import os, contextlib
import numpy as np

lib.num_threads(4)

os.system('mkdir -p scan')
for r in np.arange(0.8, 2.55, 0.1):
    output = 'scan/n2_%.1f.out' % r
    os.system("echo '\n' > %s" % output)
    print('scan %.1f' % r)
    with open(output, 'a', encoding='utf-8') as f:
        with contextlib.redirect_stdout(f):
            xyz = '''N 0.0 0.0 0.0; N 0.0 0.0 %f'''%r
            bas = 'cc-pvtz'

            mf = guess.from_frag(xyz, bas, [[0],[1]], [0,0], [3,-3], cycle=50)
            mf = guess.check_stab(mf)

            mf2 = autocas.cas(mf, (6,(3,3)), natorb=False)

            #mf3 = mcpdft.PDFT(mf2, 'tpbe')
            #mf3.kernel()

            mf4 = autocas.nevpt2(mf2)

ext: py

max_stars_repo_name: hebrewsnabla/pyAutoMR

max_issues_repo_name: hebrewsnabla/pyAutoMR

max_forks_repo_name: hebrewsnabla/pyAutoMR

reason: import numpy


text:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import math

import numpy as np  # type: ignore

import onnx
from ..base import Base
from . import expect

class Det(Base):

    @staticmethod
    def export_2d():  # type: () -> None
        node = onnx.helper.make_node(
            'Det',
            inputs=['x'],
            outputs=['y'],
        )

        x = np.arange(4).reshape(2, 2).astype(np.float32)
        y = np.linalg.det(x)  # expect -2
        expect(node, inputs=[x], outputs=[y],
               name='test_det_2d')

    @staticmethod
    def export_nd():  # type: () -> None
        node = onnx.helper.make_node(
            'Det',
            inputs=['x'],
            outputs=['y'],
        )

        x = np.array([[[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]]).astype(np.float32)
        y = np.linalg.det(x)  # expect array([-2., -3., -8.])
        expect(node, inputs=[x], outputs=[y],
               name='test_det_nd')

max_stars_repo_name: cnheider/onnx

max_issues_repo_name: cnheider/onnx

max_forks_repo_name: cnheider/onnx


text:
from typing import TYPE_CHECKING

import numpy as np
from chainer import functions as F

if TYPE_CHECKING:
    from chainer import Variable

def reshape_batch(batch: 'Variable', n_batch_axes: int) -> 'Variable':
    if batch.ndim == n_batch_axes:
        return batch
    shape = (np.prod(batch.shape[:n_batch_axes]), -1)
    return F.reshape(batch, shape)

max_stars_repo_name: msakai/DeepSentinel

max_issues_repo_name: msakai/DeepSentinel

max_forks_repo_name: msakai/DeepSentinel


text:
from copy import deepcopy

import numpy as np
from scipy.stats import gamma, norm

from ... import constants
from ...constants import Version
from .base import BMDModel, DefaultParams

class Dichotomous(BMDModel):
    dtype = constants.DICHOTOMOUS
    possible_bmr = ("Extra", "Added")

    def get_bmr_text(self):
        return "{:.0%} {} risk".format(
            self.values["bmr"],
            constants.BMR_INVERTED_CROSSALK[self.dtype][self.values["bmr_type"]],
        )

class DichotomousCancer(Dichotomous):
    dtype = constants.DICHOTOMOUS_CANCER
    possible_bmr = ("Extra", "Added")

# MULTISTAGE
class Multistage_34(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 3.4
    date = "05/02/2014"
    minimum_dose_groups = 2
    model_name = constants.M_Multistage
    exe = "multistage"
    exe_plot = "10multista"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(name="Background"),
        "beta1": DefaultParams.param_generator(name="Beta1"),
        "beta2": DefaultParams.param_generator(name="Beta2"),
        "beta3": DefaultParams.param_generator(name="Beta3"),
        "beta4": DefaultParams.param_generator(name="Beta4"),
        "beta5": DefaultParams.param_generator(name="Beta5"),
        "beta6": DefaultParams.param_generator(name="Beta6"),
        "beta7": DefaultParams.param_generator(name="Beta7"),
        "beta8": DefaultParams.param_generator(name="Beta8"),
        "restrict_beta": DefaultParams.restrict(d=1, n="Restrict beta"),
        "degree_poly": DefaultParams.degree_poly(),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    @property
    def name(self):
        return "{}-{}".format(self.model_name, self._get_degrees())

    def _get_degrees(self):
        degree = int(self.values["degree_poly"])
        if not 0 < degree <= 8:
            raise ValueError("Degree must be between 1 and 8, inclusive")
        return degree

    def as_dfile(self):
        self._set_values()
        degree_poly = self._get_degrees()

        params = ["beta{}".format(i) for i in range(1, degree_poly + 1)]
        params.insert(0, "background")

        return "\n".join(
            [
                self._dfile_print_header_rows(),
                "{} {}".format(self.dataset.dataset_length, degree_poly),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "restrict_beta",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters(*params),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        background = self._get_param("Background")
        ys = np.zeros(xs.size)
        for i in range(1, self._get_degrees() + 1):
            param = self._get_param("Beta({})".format(i))
            ys += param * np.power(xs, i)
        ys = background + (1.0 - background) * (1.0 - np.exp(-1.0 * ys))
        return ys

# QUANTAL LINEAR
class QuantalLinear_34(Multistage_34):
    defaults = deepcopy(Multistage_34.defaults)
    defaults["degree_poly"] = DefaultParams.degree_poly(d=1)

    @property
    def name(self):
        return "Quantal linear"

# MULTISTAGE CANCER
class MultistageCancer_34(DichotomousCancer):
    bmds_version_dir = Version.BMDS270
    version = 3.4
    date = "05/02/2014"
    minimum_dose_groups = 2
    model_name = constants.M_MultistageCancer
    exe = "cancer"
    exe_plot = "10cancer"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(name="Background"),
        "beta1": DefaultParams.param_generator(name="Beta1"),
        "beta2": DefaultParams.param_generator(name="Beta2"),
        "beta3": DefaultParams.param_generator(name="Beta3"),
        "beta4": DefaultParams.param_generator(name="Beta4"),
        "beta5": DefaultParams.param_generator(name="Beta5"),
        "beta6": DefaultParams.param_generator(name="Beta6"),
        "beta7": DefaultParams.param_generator(name="Beta7"),
        "beta8": DefaultParams.param_generator(name="Beta8"),
        "restrict_beta": DefaultParams.restrict(d=1, n="Restrict beta"),
        "degree_poly": DefaultParams.degree_poly(),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    @property
    def name(self):
        return "{}-{}".format(self.model_name, self._get_degrees())

    def _get_degrees(self):
        degree = int(self.values["degree_poly"])
        if not 0 < degree <= 8:
            raise ValueError("Degree must be between 1 and 8, inclusive")
        return degree

    def as_dfile(self):
        self._set_values()
        degree_poly = self._get_degrees()

        params = ["beta{}".format(i) for i in range(1, degree_poly + 1)]
        params.insert(0, "background")

        return "\n".join(
            [
                self._dfile_print_header_rows(),
                "{} {}".format(self.dataset.dataset_length, degree_poly),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "restrict_beta",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters(*params),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        background = self._get_param("Background")
        ys = np.zeros(xs.size)
        for i in range(1, self._get_degrees() + 1):
            param = self._get_param("Beta({})".format(i))
            ys += param * np.power(xs, i)
        ys = background + (1.0 - background) * (1.0 - np.exp(-1.0 * ys))
        return ys

# WEIBULL
class Weibull_217(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 2.17
    date = "06/23/2017"
    minimum_dose_groups = 3
    model_name = constants.M_Weibull
    exe = "weibull"
    exe_plot = "10weibull"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "log_transform": DefaultParams.log_transform(d=0),
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(name="Slope"),
        "power": DefaultParams.param_generator(name="Power"),
        "restrict_power": DefaultParams.restrict(d=1, n="Restrict power"),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "restrict_power",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("background", "slope", "power"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        background = self._get_param("Background")
        slope = self._get_param("Slope")
        power = self._get_param("Power")
        ys = background + (1.0 - background) * (1 - np.exp(-1.0 * slope * np.power(xs, power)))
        return ys

# LOGPROBIT
class LogProbit_34(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 3.4
    date = "05/21/2017"
    minimum_dose_groups = 3
    model_name = constants.M_LogProbit
    exe = "probit"
    exe_plot = "10probit"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "log_transform": DefaultParams.log_transform(d=1),
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(name="Slope"),
        "intercept": DefaultParams.param_generator(name="Intercept"),
        "restrict_slope": DefaultParams.restrict(d=1, n="Restrict slope"),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "log_transform",
                    "restrict_slope",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("background", "slope", "intercept"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        background = self._get_param("background")
        slope = self._get_param("slope")
        intercept = self._get_param("intercept")
        ys = background + (1.0 - background) * norm.cdf(intercept + slope * np.log(xs))
        return ys

# PROBIT
class Probit_34(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 3.4
    date = "05/21/2017"
    minimum_dose_groups = 3
    model_name = constants.M_Probit
    exe = "probit"
    exe_plot = "10probit"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "log_transform": DefaultParams.log_transform(d=0),
        "restrict_slope": DefaultParams.restrict(d=0, n="Restrict slope"),
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(name="Slope"),
        "intercept": DefaultParams.param_generator(name="Intercept"),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "log_transform",
                    "restrict_slope",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("background", "slope", "intercept"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        slope = self._get_param("slope")
        intercept = self._get_param("intercept")
        ys = norm.cdf(intercept + slope * xs)
        return ys

# GAMMA
class Gamma_217(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 2.17
    date = "06/22/2017"
    minimum_dose_groups = 3
    model_name = constants.M_Gamma
    exe = "gamma"
    exe_plot = "10gammhit"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(name="Slope"),
        "power": DefaultParams.param_generator(name="Power"),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "restrict_power": DefaultParams.restrict(d=1, n="Restrict power"),
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "restrict_power",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("background", "slope", "power"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        background = self._get_param("Background")
        slope = self._get_param("Slope")
        power = self._get_param("Power")
        ys = background + (1.0 - background) * gamma.cdf(xs * slope, power)
        return ys

# LOGLOGISTIC
class LogLogistic_215(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 2.15
    date = "03/20/2017"
    minimum_dose_groups = 3
    model_name = constants.M_LogLogistic
    exe = "logist"
    exe_plot = "10logist"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "log_transform": DefaultParams.log_transform(d=1),
        "restrict_slope": DefaultParams.restrict(d=1),
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(name="Slope"),
        "intercept": DefaultParams.param_generator(name="Intercept"),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "log_transform",
                    "restrict_slope",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("background", "slope", "intercept"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        background = self._get_param("background")
        intercept = self._get_param("intercept")
        slope = self._get_param("slope")
        ys = background + (1.0 - background) / (
            1 + np.exp(-1.0 * intercept - 1.0 * slope * np.log(xs))
        )
        return ys

# LOGISTIC
class Logistic_215(Dichotomous):
    bmds_version_dir = Version.BMDS270
    version = 2.15
    date = "03/20/2017"
    minimum_dose_groups = 3
    model_name = constants.M_Logistic
    exe = "logist"
    exe_plot = "10logist"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "log_transform": DefaultParams.log_transform(d=0),
        "restrict_slope": DefaultParams.restrict(d=0),
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "background": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(name="Slope"),
        "intercept": DefaultParams.param_generator(name="Intercept"),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "log_transform",
                    "restrict_slope",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("background", "slope", "intercept"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        intercept = self._get_param("intercept")
        slope = self._get_param("slope")
        ys = 1.0 / (1.0 + np.exp(-1.0 * intercept - slope * xs))
        return ys

# DICHOTOMOUS HILL
class DichotomousHill_13(Dichotomous):
    minimum_dose_groups = 4
    model_name = constants.M_DichotomousHill
    bmds_version_dir = Version.BMDS270
    exe = "DichoHill"
    exe_plot = "10DichoHill"
    version = 1.3
    date = "02/28/2013"
    defaults = {
        "bmdl_curve_calculation": DefaultParams.bmdl_curve_calculation,
        "restrict_power": DefaultParams.log_transform(d=1),
        "append_or_overwrite": DefaultParams.append_or_overwrite,
        "smooth_option": DefaultParams.smooth_option,
        "max_iterations": DefaultParams.max_iterations,
        "relative_fn_conv": DefaultParams.relative_fn_conv,
        "parameter_conv": DefaultParams.parameter_conv,
        "v": DefaultParams.param_generator(),
        "g": DefaultParams.param_generator(),
        "intercept": DefaultParams.param_generator(),
        "slope": DefaultParams.param_generator(),
        "bmd_calculation": DefaultParams.bmd_calculation,
        "dose_drop": DefaultParams.dose_drop,
        "bmr": DefaultParams.dich_bmr,
        "bmr_type": DefaultParams.dich_bmr_type,
        "confidence_level": DefaultParams.confidence_level,
    }

    def as_dfile(self):
        self._set_values()
        return "\n".join(
            [
                self._dfile_print_header_rows(),
                str(self.dataset.dataset_length),
                self._dfile_print_options(
                    "max_iterations",
                    "relative_fn_conv",
                    "parameter_conv",
                    "bmdl_curve_calculation",
                    "restrict_power",
                    "bmd_calculation",
                    "append_or_overwrite",
                    "smooth_option",
                ),
                self._dfile_print_options("bmr", "bmr_type", "confidence_level"),
                self._dfile_print_parameters("v", "g", "intercept", "slope"),
                self.dataset.as_dfile(),
            ]
        )

    def get_ys(self, xs):
        v = self._get_param("v")
        g = self._get_param("g")
        intercept = self._get_param("intercept")
        slope = self._get_param("slope")
        ys = v * g + (v - v * g) / (1.0 + np.exp(-intercept - slope * np.log(xs)))
        return ys

max_stars_repo_name: shapiromatron/bmds

max_issues_repo_name: shapiromatron/bmds

max_forks_repo_name: shapiromatron/bmds

reason: import numpy,from scipy


text:
import numpy as np

import warnings

from synthetic_data.trajectory import get_stay_paths, get_seg_mask

'''
def get_stay_paths(x, seg_list):

    fff = np.zeros(x.size)

    for seg in seg_list:

        _, loc, start, stop, _ = get_seg_info(seg)

        if start < stop:
            start_ind = np.where((x>=start))[0][0]
            stop_ind = np.where((x<stop))[0][0]

            mask = np.where((x>=start) & (x<stop))
        else:
            mask = np.where((x==start))

        fff[mask] = loc

    return fff
'''

"""
TODO

**also** chekc the TODOs in the script
0. break this into smaller subscripts
    * trajectory creation
    * masking
    * noise

0. rename 'journey' with 'trajectory'
    * journey implies travel

0. since slopes are determined by stays, maybe remove this field
    * specify the slope of the travel
        * the location of the travel (and stays) will be sorted to minimize the slope, cutting the adjacent stays as needed

0. add Assert so that always [stay, trav, ..., trav, stay]

1. add some documentation
2. keep the segment indices
    * use later for seg. dept. noise, also training
3. Update the noise enrichment
    * segment-dependent noise
    * configurable noise distributions for each segment
4. include asserts to ensure no overlaps of stay regions
5. Put all into class
    * x, y, noisy y
    * segment idices, features
    * various returns: np.arrays, pd.DataFrames

6. segments' endpoints to coincide with the sparse stays
    * some stays are shorter after masking
7. improve the duplication for the data
    * include some specific $x$-locations which are duplicated in certain segments
        * this is like a tower which is pinged multiple times but only gives it's location
    * include some specific $\Delta x$'s which are duplicated showing an effecitve radius when triangulation fails
    * segement-/location-specific noise
        * try also with the array of weights passed in `np.random.choice`
            * changes the probab. of picking up specific events in the full array

"""
def get_seg_noise(seg,noise):
    seg['noise'] = noise
    return seg

get_noisy_seg_info = lambda seg: (seg['noise'], seg['loc'], seg['start'], seg['end'])

get_noisy_segs = lambda segments, noises: [get_seg_noise(seg,noises[n]) for n, seg in enumerate(segments)]

get_noise_arr = lambda mn, mx, size: (mx - mn)*np.random.random_sample(size) + mn

get_add_noise = lambda eta: lambda y1: y1 + np.random.normal(loc=0.0, scale=eta, size=1)

def get_noisy_path(x, y, seg_list):

    y = y.copy()

    for seg in seg_list:

        #### NOTE
        # keep all here since mask indices are scope-depstopent

        # Get the segment details
        noise, loc, start, stop = get_noisy_seg_info(seg)

        # Find the associated indices
        mask = get_seg_mask(x,start,stop)

        y[mask] = map_array(get_add_noise(noise))(y[mask])

    return y

def get_noisy_bumps(x, **kwargs):

    for nn in range(kwargs_len):
        n=nn+1

        # The tanh-bump
        ggg = amp*np.tanh( 1*slope*(x-start)) \
            + amp*np.tanh(-1*slope*(x-end))

        # Create the mask to target only the bumps
        mask = ggg>0.001
        mask = np.array(list(map(remask, mask)))

        noise = np.random.normal(loc=0.0, scale=eta, size=ggg.size)
        if nn != 0:
            noise = noise*mask

        # Add the noisy bumps
        fff += ggg + noise

    return fff

def get_noise_event(y):
    """Under construction!!!"""
    act0=0.050
    act1=0.025
    act2=0.085
    trp0=0.035
    trp1=0.030
    trp2=0.080

    if abs(y) < 0.1:
        eta = np.random.normal(loc=0.0, scale=act0, size=1)

    elif (y >= 0.91) & (y < 1.01):
        eta = np.random.normal(loc=0.0, scale=act1, size=1)

    elif (y >= -2.01) & (y < -1.81):
        eta = np.random.normal(loc=0.0, scale=act2, size=1)

    else:
        eta = np.random.normal(loc=0.0, scale=trp2, size=1)

    return y + eta

# np.array function
map_array = lambda f: lambda x: np.fromiter((f(xi) for xi in x), x.dtype, count=len(x))

def get_noise_(yyy):
    yyy = yyy.copy()
    for n,yy in enumerate(yyy):
        #print(n)
        yyy[n] = get_noise_event(yy)

    return yyy

def get_noise(yyy):

    yyy = yyy.copy()

    yyy = map_array(get_noise_event)(yyy)

    return yyy

def get_radial_noise(x, loc, rad, sig):

    x = x.copy()

    rand_range_ = lambda l, h, sig: ((sig+h)-h)*np.random.random() + l

    rand_radius = lambda loc, rad, sig: ((-1)**np.random.randint(0,2,1)[0] )*rand_range_(loc, rad, sig)

    x = map_array(lambda y: rand_radius(y, 0.5, 0.01), x)

    return x


text:
from solver.classesAndResources import ClassesAndResources
from solver.specialist import Specialist
from solver.group import Group
from problemInstances import school1
import numpy as np

def class1Instance():

    school = school1.school1Instance()

    groups = []
    for groupId in range(6):
        groups.append(Group(groupId, "Year " + str(groupId + 1), np.asarray([4, 3, 2])))

    specialists = [
        Specialist(0, "english teacher", [[True]*school.periodsInDay]*school.daysInCycle, 0),
        Specialist(1, "physical education teacher", [[True]*school.periodsInDay]*school.daysInCycle, 1),
        Specialist(2, "music teacher", [[True]*school.periodsInDay]*school.daysInCycle, 2)
    ]

    return ClassesAndResources(school, groups, specialists)

max_stars_repo_name: XavierCL/elementary-school-solver

max_issues_repo_name: XavierCL/elementary-school-solver

max_forks_repo_name: XavierCL/elementary-school-solver


text:
from concurrent.futures import ThreadPoolExecutor
from typing import NamedTuple, Callable, Union, List, Dict

import numpy as np
from keras import optimizers

from kerl.common.agent import MultiAgent, EpisodeSample
from kerl.common.multi_gym_env import MultiGymEnv
from kerl.wm.networks import WorldModelVAE, WorldEnvModel, I2AController

class WMInternalStates(NamedTuple):
    env_model_states: list

def rename_total_loss(loss_dict: Dict[str, float],
                      model_name: str) -> Dict[str, float]:
    full_loss = loss_dict['loss']
    result = loss_dict.copy()
    del result['loss']
    result[model_name + '_loss'] = full_loss
    return result

class WMMultiAgent(MultiAgent):
    latent_dim_size = 128
    world_rnn_units = 512

    def __init__(self, env: MultiGymEnv,
                 optimizer_maker: Callable[[], optimizers.Optimizer],
                 **kwargs):
        vae_optimizer = optimizer_maker()
        env_model_optimizer = optimizer_maker()
        controller_optimizer = optimizer_maker()
        super().__init__(env, **kwargs)
        width, height, rgb_channels = env.observation_space.shape
        vae = WorldModelVAE(
            'agent_vae_',
            (width, height, rgb_channels),
            vae_optimizer, latent_dim=self.latent_dim_size)
        self.vae = vae
        self.world = WorldEnvModel(
            'agent_world_', batch_size=self._num_envs, time_steps=None,
            num_actions=self._num_actions,
            latent_dim_size=self.latent_dim_size,
            num_rnn_units=self.world_rnn_units,
            mixture_size=6, temperature=1.0, optimizer=env_model_optimizer)
        self.controller = I2AController(
            'a2c_agent_',
            observation_size=self.latent_dim_size + self.world_rnn_units,
            num_actions=self._num_actions, optimizer=controller_optimizer,
            normalize_returns=self.normalize_returns,
            reward_scale=self.reward_scale)
        self.executor = ThreadPoolExecutor(max_workers=3)

    @property
    def models(self):
        return [self.vae.trainable_model, self.world.model,
                self.controller.trainable_model]

    def reset_states(self, states=None):
        self.world.reset_states(
            states.env_model_states if states is not None else None)

    def reset_particular_agents(self, mask: Union[List[bool], np.ndarray]):
        self.world.reset_particular_states(mask)

    def current_states(self):
        return WMInternalStates(
            env_model_states=self.world.current_states())

    def multi_env_act(self, one_step_observations):
        batch_size = one_step_observations.shape[0]
        internal_states = self.current_states()
        current_obs = one_step_observations[:, :, :, :, -1]
        _, _, encoded_current_obs = self.vae.compress(current_obs)

        encoded_obs_and_world_state = (
            np.concatenate(
                [self.world.state_for_controller(
                    internal_states.env_model_states),
                 encoded_current_obs],
                axis=-1)
            .reshape((batch_size, -1)))

        policy_output, value_output = self.controller.predict_on_actions(
            encoded_obs_and_world_state)
        sampled_actions = np.array(
            [np.random.choice(self._num_actions, p=po)
             for po in policy_output])

        encoded_current_obs_with_time = encoded_current_obs.reshape(
            batch_size, 1, self.latent_dim_size)
        self.world.model.predict_on_batch(
            x=[encoded_current_obs_with_time,
               sampled_actions,
               np.zeros_like(encoded_current_obs_with_time)])

        return (policy_output, sampled_actions, value_output[:, 0],
                internal_states)

    def train_on_sample(self, sample: EpisodeSample):
        # Encoding observations using VAE so we could use them to train both
        # the environment model and the controller.
        current_obs = sample.batch_observations[:, :, :, :, :, -1]
        last_obs = sample.last_observations[:, :, :, :, -1]
        batch_size, time_steps, width, height, channels = current_obs.shape
        _, _, encoded_current_obs = self.vae.compress(
            np.reshape(current_obs,
                       (batch_size * time_steps, width, height, channels)))
        encoded_current_obs_with_time = encoded_current_obs.reshape(
            batch_size, time_steps, self.latent_dim_size)
        _, _, encoded_last_obs = self.vae.compress(last_obs)
        encoded_future_obs_with_time = np.concatenate(
            [encoded_current_obs_with_time[:, 1:],
             np.expand_dims(encoded_last_obs, 1)],
            axis=1)
        # Training all networks
        all_losses = {}
        all_losses.update(
            self.train_vae(sample))
        all_losses.update(
            self.train_env_model(
                encoded_current_obs_with_time,
                encoded_future_obs_with_time,
                sample))
        all_losses.update(
            self.train_controller(
                encoded_current_obs_with_time, self.reward_discount, sample))
        return all_losses

    def train_vae(self, sample: EpisodeSample):
        batch_size, time_steps, width, height, channels, frames = (
            sample.batch_observations.shape)
        env_observations = (
            sample.batch_observations[:, :, :, :, :, -1]
            .reshape((batch_size * time_steps, width, height, channels)))
        vae_loss = self.vae.trainable_model.train_on_batch(
            x=env_observations, y=None)
        return {self.vae.trainable_model.name + '_loss': vae_loss}

    def train_controller(self, encoded_current_obs_with_time, reward_discount,
                         sample: EpisodeSample):
        batch_size, time_steps, width, height, channels, frames = (
            sample.batch_observations.shape)
        rnn_states_before_actions = np.transpose(
            [self.world.state_for_controller(item.env_model_states)
             for item in sample.agent_internal_states],
            axes=(1, 0, 2))
        encoded_obs_and_world_state = (
            np.concatenate(
                [rnn_states_before_actions, encoded_current_obs_with_time],
                axis=-1)
            .reshape((batch_size * time_steps, -1)))
        controller_loss = self.controller.train_on_sample(
            sample, encoded_obs_and_world_state, reward_discount)
        return rename_total_loss(
            controller_loss, self.controller.trainable_model.name)

    def train_env_model(self, encoded_current_obs_with_time,
                        encoded_future_obs_with_time, sample: EpisodeSample):
        state_before_training = self.world.current_states()
        self.world.reset_states(
            sample.agent_internal_states[0].env_model_states)
        env_model_loss = self.world.model.train_on_batch(
            x=[encoded_current_obs_with_time,
               sample.batch_actions,
               encoded_future_obs_with_time],
            y=[np.expand_dims(sample.batch_rewards, axis=-1),
               np.expand_dims(np.float32(sample.batch_dones), axis=-1)])
        env_model_named_losses = {
            n: v for n, v in zip(self.world.model.metrics_names,
                                 env_model_loss)}
        self.world.reset_states(state_before_training)
        return rename_total_loss(env_model_named_losses,
                                 self.world.model.name)

max_stars_repo_name: greenawaysean/kerl

max_issues_repo_name: greenawaysean/kerl

max_forks_repo_name: greenawaysean/kerl


text:
import numpy as np
import netCDF4 as nc

def haversine(lon1, lat1, lon2, lat2):
    """ This is copied from salishsea_tools """
    lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = np.sin(dlat / 2) ** 2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon / 2) ** 2
    c = 2 * np.arcsin(np.sqrt(a))
    km = 6367 * c  # from ss tools
    # km = 6371 * c  # from earthdist.m
    return km * 1000

def t2u(lont,latt):
    # We don't have a value for the rightmost u points
    lonu = np.zeros(lont.shape)
    latu = np.zeros(lont.shape)
    latu[:,0:-1]=0.5*(latt[:,0:-1] + latt[:,1:])
    lonu[:,0:-1]=0.5*(lont[:,0:-1] + lont[:,1:])
    return lonu,latu

def t2v(lont,latt):
    # We don't have a value for the northmost v points
    lonv = np.zeros(lont.shape)
    latv = np.zeros(lont.shape)
    latv[0:-1,:]=0.5*(latt[0:-1,:] + latt[1:,:])
    lonv[0:-1,:]=0.5*(lont[0:-1,:] + lont[1:,:])
    return lonv,latv

def t2f(lont,latt):
    # We don't have values in the rightmost u pt or northmost v pt
    lonf = np.zeros(lont.shape)
    latf = np.zeros(lont.shape)
    lonf[0:-1,0:-1] = 0.25*(lont[0:-1,0:-1] + lont[1:,0:-1] + lont[0:-1,1:] + lont[1:,1:])
    latf[0:-1,0:-1] = 0.25*(latt[0:-1,0:-1] + latt[1:,0:-1] + latt[0:-1,1:] + latt[1:,1:])
    return lonf,latf

def gete1(lon, lat, expandleft=False):
    if expandleft:
        lon, lat = expandi(lon, lat)
    dx = np.zeros(lon.shape)
    lon1 = lon[0:-1, 0:-1]
    lat1 = lat[0:-1, 0:-1]
    lon2 = lon[0:-1, 1:]
    lat2 = lat[0:-1, 1:]
    dx[:-1, :-1] = haversine(lon1, lat1, lon2, lat2)
    return dx

def gete2(lon, lat, expanddown=False):
    if expanddown:
        lon, lat = expandj(lon, lat)
    dy = np.zeros(lon.shape)
    lon1 = lon[0:-1, 0:-1]
    lat1 = lat[0:-1, 0:-1]
    lon2 = lon[1:, 0:-1]
    lat2 = lat[1:, 0:-1]
    dy[:-1, :-1] = haversine(lon1, lat1, lon2, lat2)
    return dy

def expandi(lon, lat):
    # Expands the grid one to the left by linear extrapolation
    NY, NX = lon.shape
    lon2, lat2 = np.zeros([NY, NX + 1]), np.zeros([NY, NX + 1])
    # Lon
    lon2[:, 1:] = lon
    lon2[:, 0] = lon[:, 0] - (lon[:, 1] - lon[:, 0])
    # Lat
    lat2[:, 1:] = lat
    lat2[:, 0] = lat[:, 0] - (lat[:, 1] - lat[:, 0])
    return lon2, lat2

def expandj(lon, lat):
    # Expands the grid one down by linear extrapolation
    NY, NX = lon.shape
    lon2, lat2 = np.zeros([NY + 1, NX]), np.zeros([NY + 1, NX])
    # Long
    lon2[1:, :] = lon
    lon2[0, :] = lon[0, :] - (lon[1, :] - lon[0, :])
    # Lat
    lat2[1:, :] = lat
    lat2[0, :] = lat[0, :] - (lat[1, :] - lat[0, :])
    return lon2, lat2

def expandf(glamf, gphif):
    # Expand the f points grid so the f points form complete boxes around the t points
    # This is needed because the coordinates file truncates the f points by one.
    NY, NX = glamf.shape[0], glamf.shape[1]
    glamfe = np.zeros([NY+1, NX+1])
    gphife = np.zeros([NY+1, NX+1])
    # Long
    glamfe[1:,1:] = glamf
    glamfe[0,1:] = glamf[0,:] - (glamf[1,:] - glamf[0,:])     # extraoplation
    glamfe[:,0] = glamfe[:,1] - (glamfe[:,2] - glamfe[:,1])   # extraoplation
    # Lat
    gphife[1:,1:] = gphif
    gphife[0,1:] = gphif[0,:] - (gphif[1,:] - gphif[0,:])     # extraoplation
    gphife[:,0] = gphife[:,1] - (gphife[:,2] - gphife[:,1])   # extraoplation
    return glamfe, gphife

def grid_angle(f):
    with nc.Dataset(f, 'r') as cnc:
        glamu = cnc.variables['glamu'][0, ...]
        gphiu = cnc.variables['gphiu'][0, ...]
    # First point
    xA = glamu[0:-1, 0:-1]
    yA = gphiu[0:-1, 0:-1]
    # Second point
    xB = glamu[0:-1, 1:]
    yB = gphiu[0:-1, 1:]
    # Third point: same longitude as second point, same latitude as first point
    xC = xB
    yC = yA
    # Find angle by spherical trig
    # https://en.wikipedia.org/wiki/Solution_of_triangles#Three_sides_given_.28spherical_SSS.29
    R = 6367  # from geo_tools.haversine
    a = haversine(xB, yB, xC, yC) / R
    b = haversine(xA, yA, xC, yC) / R
    c = haversine(xA, yA, xB, yB) / R
    cosA = (np.cos(a) - np.cos(b) * np.cos(c)) / (np.sin(b) * np.sin(c))
    # cosB = (np.cos(b) - np.cos(c)*np.cos(a))/(np.sin(c)*np.sin(a))
    # cosC = (np.cos(c) - np.cos(a)*np.cos(b))/(np.sin(a)*np.sin(b))
    A = np.degrees(np.arccos(cosA))  # A is the angle counterclockwise from from due east
    return A

def writecoords(fname,
                glamt, glamu, glamv, glamf,
                gphit, gphiu, gphiv, gphif,
                e1t, e1u, e1v, e1f,
                e2t, e2u, e2v, e2f):
    # Build a NEMO format coordinates file

    cnc = nc.Dataset(fname, 'w', clobber=True)
    NY, NX = glamt.shape

    # Create the dimensions
    cnc.createDimension('x', NX)
    cnc.createDimension('y', NY)
    cnc.createDimension('time', None)

    # Create the float variables
    cnc.createVariable('nav_lon', 'f', ('y', 'x'), zlib=True, complevel=4)
    cnc.variables['nav_lon'].setncattr('units', 'degrees_east')
    cnc.variables['nav_lon'].setncattr('comment', 'at t points')

    cnc.createVariable('nav_lat', 'f', ('y', 'x'), zlib=True, complevel=4)
    cnc.variables['nav_lat'].setncattr('units', 'degrees_north')
    cnc.variables['nav_lat'].setncattr('comment', 'at t points')

    cnc.createVariable('time', 'f', ('time'), zlib=True, complevel=4)
    cnc.variables['time'].setncattr('units', 'seconds since 0001-01-01 00:00:00')
    cnc.variables['time'].setncattr('time_origin', '0000-JAN-01 00:00:00')
    cnc.variables['time'].setncattr('calendar', 'gregorian')

    # Create the double variables
    varlist = ['glamt', 'glamu', 'glamv', 'glamf', 'gphit', 'gphiu', 'gphiv', 'gphif']
    varlist += ['e1t', 'e1u', 'e1v', 'e1f', 'e2t', 'e2u', 'e2v', 'e2f']
    for v in varlist:
        cnc.createVariable(v, 'd', ('time', 'y', 'x'), zlib=True, complevel=4)
        cnc.variables[v].setncattr('missing_value', 1e+20)

    # Write the data
    cnc.variables['nav_lon'][...] = glamt
    cnc.variables['nav_lat'][...] = gphit
    #
    cnc.variables['glamt'][0, ...] = glamt
    cnc.variables['glamu'][0, ...] = glamu
    cnc.variables['glamv'][0, ...] = glamv
    cnc.variables['glamf'][0, ...] = glamf
    #
    cnc.variables['gphit'][0, ...] = gphit
    cnc.variables['gphiu'][0, ...] = gphiu
    cnc.variables['gphiv'][0, ...] = gphiv
    cnc.variables['gphif'][0, ...] = gphif
    #
    cnc.variables['e1t'][0, ...] = e1t
    cnc.variables['e1u'][0, ...] = e1u
    cnc.variables['e1v'][0, ...] = e1v
    cnc.variables['e1f'][0, ...] = e1f
    #
    cnc.variables['e2t'][0, ...] = e2t
    cnc.variables['e2u'][0, ...] = e2u
    cnc.variables['e2v'][0, ...] = e2v
    cnc.variables['e2f'][0, ...] = e2f

    cnc.close()

def writebathy(filename,glamt,gphit,bathy):

    bnc = nc.Dataset(filename, 'w', clobber=True)
    NY,NX = glamt.shape

    # Create the dimensions
    bnc.createDimension('x', NX)
    bnc.createDimension('y', NY)

    bnc.createVariable('nav_lon', 'f', ('y', 'x'), zlib=True, complevel=4)
    bnc.variables['nav_lon'].setncattr('units', 'degrees_east')

    bnc.createVariable('nav_lat', 'f', ('y', 'x'), zlib=True, complevel=4)
    bnc.variables['nav_lat'].setncattr('units', 'degrees_north')

    bnc.createVariable('Bathymetry', 'd', ('y', 'x'), zlib=True, complevel=4, fill_value=0)
    bnc.variables['Bathymetry'].setncattr('units', 'metres')

    bnc.variables['nav_lon'][:] = glamt
    bnc.variables['nav_lat'][:] = gphit
    bnc.variables['Bathymetry'][:] = bathy

    bnc.close()

max_stars_repo_name: goldford/Ecosystem-Model-Data-Framework

max_issues_repo_name: goldford/Ecosystem-Model-Data-Framework

max_forks_repo_name: goldford/Ecosystem-Model-Data-Framework


text:
import sys
import numpy as np
from ..roll_pass import RollPass

@RollPass.hookimpl
def tip_width(roll_pass):
    return roll_pass.roll.groove.usable_width + roll_pass.gap / 2 / np.tan(roll_pass.roll.groove.alpha1)

@RollPass.hookimpl
def height(roll_pass):
    return roll_pass.gap + 2 * roll_pass.roll.groove.depth

@RollPass.hookimpl
def volume(roll_pass: RollPass):
    return (roll_pass.in_profile.cross_section.area + 2 * roll_pass.out_profile.cross_section.area
            ) / 3 * roll_pass.roll.contact_length

RollPass.plugin_manager.register(sys.modules[__name__])

max_stars_repo_name: pyroll-project/pyroll-core

max_issues_repo_name: pyroll-project/pyroll-core

max_forks_repo_name: pyroll-project/pyroll-core


text:
import numpy as np #(activate this if CPU is used)
# import cupy as np #(activate this if GPU is used)

from mlxtend.data import loadlocal_mnist
import json

def Read_MNIST(par, Agent):
  ################################################################################################################################################
  ##### MNIST
  ##### type=np.ndarray (x_train: 60000 x 784 (float:0.~1.), y_train: 60000 x 1 (int:0~9), ... )
  ################################################################################################################################################
  par.num_features = 784 # 28*28
  par.num_classes = 10 # 0 to 9 digits
  par.split_number = int(Agent)
  # Load MNIST
  x_train, y_train = loadlocal_mnist(images_path='./Inputs/train-images-idx3-ubyte', labels_path='./Inputs/train-labels-idx1-ubyte')
  x_test, y_test = loadlocal_mnist(images_path='./Inputs/t10k-images-idx3-ubyte', labels_path='./Inputs/t10k-labels-idx1-ubyte')

  # Convert to float32.
  x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)
  y_train, y_test = np.array(y_train), np.array(y_test)

  # Flatten images to 1-D vector of 784 features (28*28).
  x_train, x_test = x_train.reshape([-1, par.num_features]), x_test.reshape([-1, par.num_features])

  # Normalize images value from [0, 255] to [0, 1].
  x_train, x_test = x_train / 255., x_test / 255.

  # Split data per agent
  x_train_agent = np.split(x_train, par.split_number)
  y_train_agent = np.split(y_train, par.split_number)
  x_train_agent, y_train_agent = np.array(x_train_agent), np.array(y_train_agent)

  x_list = []; y_list = [];
  for p in range(par.split_number):
    x_list.append(x_train_agent[p])
    y_list.append(y_train_agent[p])

  x_train_new = np.concatenate( np.array(x_list) )
  y_train_new = np.concatenate( np.array(y_list) )

  return x_test, y_test, x_train_new, y_train_new, x_train_agent, y_train_agent

def Read_FEMNIST(par, Agent):
  ################################################################################################################################################
  ##### FEMNIST (datatype=dict)
  ##### Example: "users": ["f3795_00", "f3608_13"], "num_samples": [149, 162], "user_data": {"f3795_00": {"x": [], ..., []}, "y": [4, ..., 31]},
  ################################################################################################################################################
  par.num_features = 784 # 28*28
  par.num_classes = 62 # 0 to 9 digits + alphabet (26 + 26)
  TS = 36 ## TS <= 36 for FEMNIST dataset

  train_data={};   x_train_agent={};  y_train_agent={};
  tmp_x_train=[]; tmp_y_train=[];
  tmpcnt = 0
  for testset in range(TS):
    with open('./Inputs/Femnist_Train_%s/all_data_%s_niid_05_keep_0_train_9.json'%(Agent,testset)) as f:
      train_data[testset] = json.load(f)
    for user in train_data[testset]["users"]:
      Temp_x_train = []; Temp_y_train = [];
      ## x_train
      for x_elem in train_data[testset]["user_data"][user]["x"]:
        tmp_x_train.append( 1.0-np.array(x_elem) )
        Temp_x_train.append( 1.0-np.array(x_elem) )
      Temp_x_train=np.array( Temp_x_train,np.float32 )
      x_train_agent[tmpcnt] = Temp_x_train
      ## y_train
      for y_elem in train_data[testset]["user_data"][user]["y"]:
        tmp_y_train.append( np.array(y_elem) )
        Temp_y_train.append( np.array(y_elem) )
      Temp_y_train=np.array( Temp_y_train,np.uint8 )
      y_train_agent[tmpcnt] = Temp_y_train
      tmpcnt += 1

  x_train_new = np.array(tmp_x_train,np.float32)
  y_train_new = np.array(tmp_y_train,np.uint8)

  x_train_new = np.array(tmp_x_train,np.float32)
  y_train_new = np.array(tmp_y_train,np.uint8)

  par.split_number = tmpcnt

  ## Testing
  test_data={}
  temp_x_test=[]; temp_y_test=[]; total_num_test_data=0;
  for testset in range(TS):
    with open('./Inputs/Femnist_Test_%s/all_data_%s_niid_05_keep_0_test_9.json'%(Agent,testset)) as f:
      test_data[testset] = json.load(f)
    for user in test_data[testset]["users"]:
      total_num_test_data += len(test_data[testset]["user_data"][user]["y"])

      ## x_test
      for x_elem in test_data[testset]["user_data"][user]["x"]:
        temp_x_test.append( 1.0-np.array(x_elem) )

      ## y_test
      for y_elem in test_data[testset]["user_data"][user]["y"]:
        temp_y_test.append( y_elem )

  x_test = np.array(temp_x_test,np.float32)
  y_test = np.array(temp_y_test,np.uint8)

  return x_test, y_test, x_train_new, y_train_new, x_train_agent, y_train_agent

reason: import numpy,import cupy


text:
import re
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

color = sns.color_palette('deep') + sns.color_palette('bright')

def FeatureSort(feature_name, group=np.array(()), group_name=[], value=[], store_path='',
                is_sort=True, is_show=True, fig=plt.figure()):
    '''
    Draw the plot of the sorted feature, an option is to draw different color according to the group and group value.

    :param feature_name: The name of the features
    :param group: the array to map the feature name to the group name, default is zeros
    :param group_name: the group name list to denote the group. default is []
    :param value: The value of each feature_name. Default is []
    :param store_path: The store path, supporting .jpeg and .tif
    :param is_sort: Boolen, to sort the features according to value. Default is True
    :return:

    Apr-29-18, Yang SONG [yang.song.91@foxmail.com]
    '''
    if group.size == 0 and group_name == []:
        group = np.zeros((len(feature_name), ), dtype=np.uint8)
        group_name = ['']

    if value == []:
        value = [len(feature_name) - index for index in range(len(feature_name))]
    else:
        value = np.abs(np.squeeze(value))

    if is_sort:
        sort_index = sorted(range(len(value)), key=lambda k: value[k], reverse=True)

        value = [value[index] for index in sort_index]
        feature_name = [feature_name[index] for index in sort_index]
        group = [group[index] for index in sort_index]

    assert(np.max(group) + 1 == len(group_name))
    sub_group = np.zeros((len(group_name), len(feature_name)))
    for index in range(len(feature_name)):
        sub_group[group[index], index] = value[index]
    y = range(len(feature_name))

    fig.clear()
    ax = fig.add_subplot(111)

    for index in range(sub_group.shape[0]):
        ax.barh(y, sub_group[index, :], color=color[index])

    ax.set_yticks(range(len(feature_name)))
    ax.set_yticklabels(feature_name)
    ax.set_xticks([])
    if len(group_name) > 1:
        ax.legend(group_name)

    if store_path:
        fig.set_tight_layout(True)
        if store_path[-3:] == 'jpg':
            fig.savefig(store_path, dpi=300, format='jpeg')
        elif store_path[-3:] == 'eps':
            fig.savefig(store_path, dpi=1200, format='eps')

    if is_show:
        fig.show()

    return ax

def ShortFeatureFullName(feature_full_name):
    if len(feature_full_name) <= 5:
        return feature_full_name

    sub = re.findall("[A-Z]", feature_full_name)
    if len(sub) == 1:
        return feature_full_name[:5]
    elif len(sub) == 0:
        return feature_full_name[:5]
    else:
        return ''.join(sub)

def SeperateRadiomicsFeatures(feature_name):
    '''
    Generate the feature name, group, and group cound according to radiomics features.

    :param feature_name: The generated radiomis featues. which should including sequence_name, image_class,
    feature_class, and the feature_name, like T2_origin_glszm_ZonePercentage
    :return:

    Apr-29-18 Yang SONG [yang.song.91@foxmail.com]
    '''
    sub_feature_name = []
    group = []
    group_name = []

    seq_group = []
    seq_count = 0
    image_class_group = []
    image_class_count = 0
    feature_class_group = []
    feature_class_count = 0

    for feature in feature_name:
        sep = feature.split('_')
        if len(sep) == 2:
            sep = [sep[0], '', '', sep[1]]

        seq = sep[0]
        image_class = sep[1]
        feature_class = sep[-2]

        if not seq in seq_group:
            seq_count += 1
        seq_group.append(seq)
        if not image_class in image_class_group:
            image_class_count += 1
        image_class_group.append(image_class)
        if not feature_class in feature_class_group:
            feature_class_count += 1
        feature_class_group.append(feature_class)

        sub_feature_name.append(ShortFeatureFullName(sep[-1]))

    if seq_count == 1:
        seq_group = ['' for index in range(len(feature_name))]
    if image_class_count == 1:
        image_class_group = ['' for index in range(len(feature_name))]
    if feature_class_count == 1:
        feature_class_group = ['' for index in range(len(feature_name))]

    group_count = 0
    for index in range(len(feature_name)):
        temp_name = seq_group[index] + '-' + image_class_group[index] + '-' + feature_class_group[index]
        if not temp_name in group_name:
            group_name.append(temp_name)
            group.append(group_count)
            group_count += 1
        else:
            group.append(group_name.index(temp_name))

    return sub_feature_name, np.asarray(group, dtype=np.uint8), group_name

def SortRadiomicsFeature(feature_name, value=[], store_path='', is_show=False, fig=plt.figure()):
    sub_feature_name, group, group_name = SeperateRadiomicsFeatures(feature_name)
    FeatureSort(sub_feature_name, group, group_name, value, store_path, is_show=is_show, fig=fig)

def GeneralFeatureSort(feature_name, value=[], store_path='', is_sort=True, max_num=-1, is_show=True, fig=plt.figure()):
    if not isinstance(value, list):
        value = list(value)
    if value == []:
        value = [len(feature_name) - index for index in range(len(feature_name))]

    if is_sort:
        sort_index = sorted(range(len(value)), key=lambda k: value[k], reverse=True)

        value = [value[index] for index in sort_index]
        feature_name = [feature_name[index] for index in sort_index]

    if max_num > 0:
        value = value[:max_num]
        feature_name = feature_name[:max_num]

    fig.clear()
    # margin = 0.2

    left, bottom, width, height = 0.75, 0.1, 0.2, 0.8

    ax = fig.add_axes([left, bottom, width, height])
    # ax = fig.add_subplot(111)
    ax.barh(range(len(feature_name)), value, color=color[0])
    ax.set_yticks(range(len(feature_name)))
    ax.set_yticklabels(feature_name)

    ax.set_xticks([])

    if store_path:
        fig.set_tight_layout(True)
        if store_path[-3:] == 'jpg':
            plt.savefig(store_path, dpi=300, format='jpeg')
        elif store_path[-3:] == 'eps':
            plt.savefig(store_path, dpi=1200, format='eps')

    if is_show:
        fig.show()

    return ax

if __name__ == '__main__':
    # feature_name = ['DE', 'SAE', 'SZNUM', 'JE', 'Id']
    feature_name = ['10Per', 'Autoc', 'IR', 'GLV']
    value = [72.9, 45.4, 45.2, 41]
    group = np.array([1, 3, 2, 0])
    group_name = ['ADC--firstorder', 'DWI--firstorder', 'DWI--glcm', 'DWI--glszm']
    # group = [0, 1, 1, 0, 0]
    # group_name = ['GLCM', 'GLSZM']
    # value = 0.1, 0.5, 0.9, 0.2, 0.1
    FeatureSort(feature_name, group, group_name, value, is_show=True)

    # import pandas as pd
    # df = pd.read_csv(r'C:\Users\yangs\Desktop\anova_sort.csv', index_col=0)
    # feature_name = list(df.index)
    # value = list(df['F'])
    # new_feature_name = [ShortFeatureFullName(index) for index in feature_name]
    # GeneralFeatureSort(new_feature_name, value, max_num=4, is_show=True, store_path=r'D:\MyDocs\Document\研究生\毕业\毕业论文\图\组学\ANOVA_sort.jpg')
    # SortRadiomicsFeature(new_feature_name, value, is_show=True)

max_stars_repo_name: Eggiverse/FAE

max_issues_repo_name: Eggiverse/FAE

max_forks_repo_name: Eggiverse/FAE


text:
import pandas as pd
import numpy as np

def demo_list(filepath='data/hh_demographic.csv',
                     id_col='household_key',
                    ):
    demo = pd.read_csv(filepath)
    lst= list(demo[id_col].unique())

    return lst


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

import numpy as np
# Matplotlib
import matplotlib as mpl
# Matplotlib中最基础的模块是pyplot
import matplotlib.pyplot as plt
# 3D图表必须的模块，project='3d'的定义
from mpl_toolkits.mplot3d import Axes3D

# Matplotlib是Python中最常用的可视化工具之一，可以非常方便地创建海量类型地2D图表和一些基本的3D图表。
# Matplotlib最早是为了可视化癫痫病人的脑皮层电图相关的信号而研发，因为在函数的设计上参考了MATLAB，所以叫做Matplotlib。
# Matplotlib首次发表于2007年，在开源和社区的推动下，现在在基于Python的各个科学计算领域都得到了广泛应用。
# Matplotlib的原作者John D. Hunter博士是一名神经生物学家，2012年不幸因癌症去世，感谢他创建了这样一个伟大的库。

# 在Matplotlib中，画图时有两个常用概念，一个是平时画图蹦出的一个窗口，这叫一个figure。
# Figure相当于一个大的画布，在每个figure中，又可以存在多个子图，这种子图叫做axes。
# 顾名思义，有了横纵轴就是一幅简单的图表。
# 把figure定义成了一个一行两列的大画布，然后通过fig.add_subplot()加入两个新的子图。
# subplot的定义格式很有趣，数字的前两位分别定义行数和列数，最后一位定义新加入子图的所处顺序，当然想写明确些也没问题，用逗号分开即可。

# 点线图 ------------------------------------------------------------------------

# 通过rcParams设置全局横纵轴字体大小
mpl.rcParams['xtick.labelsize'] = 24
mpl.rcParams['ytick.labelsize'] = 24

# np.random.seed(42)

# x轴的采样点
x = np.linspace(0, 5, 100)

# 通过下面曲线加上噪声生成数据，所以拟合模型就用y了……
y = 2 * np.sin(x) + 0.3 * x**2
y_data = y + np.random.normal(scale=0.3, size=100)

# figure()指定图表名称
plt.figure('data')

# '.'标明画散点图，每个散点的形状是个圆
plt.plot(x, y_data, '.')

# 画模型的图，plot函数默认画连线图
plt.figure('model')
plt.plot(x, y)

# 两个图画一起
plt.figure('data & model')

# 通过'k'指定线的颜色，lw指定线的宽度
# 第三个参数除了颜色也可以指定线形，比如'r--'表示红色虚线
# 更多属性可以参考官网：http://matplotlib.org/api/pyplot_api.html
plt.plot(x, y, 'k', lw=3)

# scatter可以更容易地生成散点图
plt.scatter(x, y_data)

# 将当前figure的图保存到文件result.png
plt.savefig('result.png')

# 一定要加上这句才能让画好的图显示在屏幕上
plt.show()

# 柱状图和饼图 ---------------------------------------------------------------------

mpl.rcParams['axes.titlesize'] = 20
mpl.rcParams['xtick.labelsize'] = 16
mpl.rcParams['ytick.labelsize'] = 16
mpl.rcParams['axes.labelsize'] = 16
mpl.rcParams['xtick.major.size'] = 0
mpl.rcParams['ytick.major.size'] = 0

# 包含了狗，猫和猎豹的最高奔跑速度，还有对应的可视化颜色
speed_map = {
    'dog': (48, '#7199cf'),
    'cat': (45, '#4fc4aa'),
    'cheetah': (120, '#e1a7a2')
}

# 整体图的标题
fig = plt.figure('Bar chart & Pie chart')

# 在整张图上加入一个子图，121的意思是在一个1行2列的子图中的第一张
ax = fig.add_subplot(121)
ax.set_title('Running speed - bar chart')

# 生成x轴每个元素的位置
xticks = np.arange(3)

# 定义柱状图每个柱的宽度
bar_width = 0.5

# 动物名称
animals = speed_map.keys()

# 奔跑速度
speeds = [x[0] for x in speed_map.values()]

# 对应颜色
colors = [x[1] for x in speed_map.values()]

# 画柱状图，横轴是动物标签的位置，纵轴是速度，定义柱的宽度，同时设置柱的边缘为透明
bars = ax.bar(xticks, speeds, width=bar_width, edgecolor='none')

# 设置y轴的标题
ax.set_ylabel('Speed(km/h)')

# x轴每个标签的具体位置，设置为每个柱的中央
ax.set_xticks(xticks + bar_width / 2)

# 设置每个标签的名字
ax.set_xticklabels(animals)

# 设置x轴的范围
ax.set_xlim([bar_width / 2 - 0.5, 3 - bar_width / 2])

# 设置y轴的范围
ax.set_ylim([0, 125])

# 给每个bar分配指定的颜色
for bar, color in zip(bars, colors):
    bar.set_color(color)

# 在122位置加入新的图
ax = fig.add_subplot(122)
ax.set_title('Running speed - pie chart')

# 生成同时包含名称和速度的标签
labels = ['{0}\n{1} km/h'.format(animal, speed)
          for animal, speed in zip(animals, speeds)]

# 画饼状图，并指定标签和对应颜色
ax.pie(speeds, labels=labels, colors=colors)

plt.show()

# 3D图表 -----------------------------------------------------------------------

# 先生成一个所有值均为0的复数array作为初始频谱，然后把频谱中央部分用随机生成，但同时共轭关于中心对称的子矩阵进行填充。
# 这相当于只有低频成分的一个随机频谱。最后进行反傅里叶变换就得到一个随机波动的曲面

n_grids = 51        	# x-y平面的格点数
c = n_grids / 2     	# 中心位置
nf = 2              	# 低频成分的个数

# 生成格点
x = np.linspace(0, 1, n_grids)
y = np.linspace(0, 1, n_grids)

# x和y是长度为n_grids的array
# meshgrid会把x和y组合成n_grids*n_grids的array，X和Y对应位置就是所有格点的坐标
X, Y = np.meshgrid(x, y)

# 生成一个0值的傅里叶谱
spectrum = np.zeros((n_grids, n_grids), dtype=np.complex)

# 生成一段噪音，长度是(2*nf+1)**2/2
noise = [np.complex(x, y)
         for x, y in np.random.uniform(-1, 1, ((2 * nf + 1)**2 / 2, 2))]

# 傅里叶频谱的每一项和其共轭关于中心对称
noisy_block = np.concatenate((noise, [0j], np.conjugate(noise[::-1])))

# 将生成的频谱作为低频成分
spectrum[c - nf:c + nf + 1, c - nf:c + nf +
         1] = noisy_block.reshape((2 * nf + 1, 2 * nf + 1))

# 进行反傅里叶变换
Z = np.real(np.fft.ifft2(np.fft.ifftshift(spectrum)))

# 创建图表
fig = plt.figure('3D surface & wire')

# 第一个子图，surface图
ax = fig.add_subplot(1, 2, 1, projection='3d')

# alpha定义透明度，cmap是color map
# rstride和cstride是两个方向上的采样，越小越精细，lw是线宽
ax.plot_surface(X, Y, Z, alpha=0.7, cmap='jet', rstride=1, cstride=1, lw=0)

# 第二个子图，网线图
ax = fig.add_subplot(1, 2, 2, projection='3d')
ax.plot_wireframe(X, Y, Z, rstride=3, cstride=3, lw=0.5)

plt.show()

# -------------
# 直接先采样了一堆3维的正态分布样本，保证方向上的均匀性。
# 然后归一化，让每个样本到原点的距离为1，相当于得到了一个均匀分布在球面上的样本。
# 再接着把每个样本都乘上一个均匀分布随机数的开3次方，这样就得到了在球体内均匀分布的样本，
# 最后根据判别平面3x+2y-z-1=0对平面两侧样本用不同的形状和颜色画出

# 采样个数500
n_samples = 500
dim = 3

# 先生成一组3维正态分布数据，数据方向完全随机
samples = np.random.multivariate_normal(
    np.zeros(dim),
    np.eye(dim),
    n_samples
)

# 通过把每个样本到原点距离和均匀分布吻合得到球体内均匀分布的样本
for i in range(samples.shape[0]):
    r = np.power(np.random.random(), 1.0 / 3.0)
    samples[i] *= r / np.linalg.norm(samples[i])

upper_samples = []
lower_samples = []

for x, y, z in samples:
    # 3x+2y-z=1作为判别平面
    if z > 3 * x + 2 * y - 1:
        upper_samples.append((x, y, z))
    else:
        lower_samples.append((x, y, z))

fig = plt.figure('3D scatter plot')
ax = fig.add_subplot(111, projection='3d')

uppers = np.array(upper_samples)
lowers = np.array(lower_samples)

# 用不同颜色不同形状的图标表示平面上下的样本
# 判别平面上半部分为红色圆点，下半部分为绿色三角
ax.scatter(uppers[:, 0], uppers[:, 1], uppers[:, 2], c='r', marker='o')
ax.scatter(lowers[:, 0], lowers[:, 1], lowers[:, 2], c='g', marker='^')

plt.show()

# 图像显示 -----------------------------------------------------------------------

# 读取一张小白狗的照片并显示
plt.figure('A Little White Dog')
little_dog_img = plt.imread('little_white_dog.jpg')
plt.imshow(little_dog_img)

# Z是上小节生成的随机图案，img0就是Z，img1是Z做了个简单的变换
img0 = Z
img1 = 3 * Z + 4

# cmap指定为'gray'用来显示灰度图
fig = plt.figure('Auto Normalized Visualization')
ax0 = fig.add_subplot(121)
ax0.imshow(img0, cmap='gray')

ax1 = fig.add_subplot(122)
ax1.imshow(img1, cmap='gray')

plt.show()

max_stars_repo_name: xiaomailong/prestudy

max_issues_repo_name: xiaomailong/prestudy

max_forks_repo_name: xiaomailong/prestudy


text:
import numpy as np
import random

from Discretization import DiscretizationScheme as DS

#action : esquema de discretizacion DS
def SUKP(poblacion,matrixBin,solutionsRanking,ds,repairType,numItem, numElement, knapsackSize, relationMatrix, weight, profit):

    #Binarización de 2 pasos
    ds = ds.split(",")
    ds = DS.DiscretizationScheme(poblacion,matrixBin,solutionsRanking,ds[0],ds[1])
    matrixBin = ds.binariza()

    #Evaluamos restricción, sila función evaluar_restriccion_SUKP devuelve un 1 es factible, si devuelve un 0 no es factible y hay que reparar
    EvaluarReparacion, PesosDeItems = evaluar_restriccion_SUKP(matrixBin,relationMatrix,weight,knapsackSize,numItem)

    #Reparamos si corresponde
    numReparacionesVector = []
    for i in range(len(EvaluarReparacion)):
        if EvaluarReparacion[i] == 0:
            if repairType == 1: #Reparación "Simple" con un orden según peso de los items
                vectorSinReparar = matrixBin[i]
                matrixBin[i] = reparar1(matrixBin[i], knapsackSize, relationMatrix, weight, profit,PesosDeItems)   #*** Ver que más necesita la función para reparar
                vectorReparada = matrixBin[i]
                numReparacionesVector.append(np.sum(vectorReparada - vectorSinReparar))
            if repairType == 2: #Reparación según ponderación p_i/R_i del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
                vectorSinReparar = matrixBin[i]
                matrixBin[i] = reparar2(matrixBin[i], knapsackSize, relationMatrix, weight, profit,PesosDeItems,numItem)   #*** Ver que más necesita la función para reparar
                vectorReparada = matrixBin[i]
                numReparacionesVector.append(np.sum(vectorReparada - vectorSinReparar))
            if repairType == 3: #Reparación según ponderación p_i/R_i del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
                vectorSinReparar = matrixBin[i]
                matrixBin[i] = reparar3(matrixBin[i], knapsackSize, relationMatrix, weight, profit,PesosDeItems,numItem)   #*** Ver que más necesita la función para reparar
                vectorReparada = matrixBin[i]
                numReparacionesVector.append(np.sum(vectorReparada - vectorSinReparar))
            if repairType == 4: #Reparación según ponderación p_i/R_i del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
                vectorSinReparar = matrixBin[i]
                matrixBin[i] = reparar3(matrixBin[i], knapsackSize, relationMatrix, weight, profit,PesosDeItems,numItem)   #*** Ver que más necesita la función para reparar
                vectorReparada = matrixBin[i]
                numReparacionesVector.append(np.sum(vectorReparada - vectorSinReparar))
    numReparaciones = np.sum(numReparacionesVector)

    #Calculamos Fitness
    # print(f'matrixBin: {matrixBin}')
    # print(f'matrixBin.sum: {np.sum(matrixBin)}')
    fitness = np.sum(np.multiply(matrixBin,profit),axis =1)
    solutionsRanking = (-fitness).argsort() # rankings de los mejores fitness

    return matrixBin,fitness,solutionsRanking,numReparaciones

def evaluar_restriccion_SUKP(matrixBin,relationMatrix,weight,knapsackSize,numItem):

    PesosDeMatrizRelacion = np.multiply(relationMatrix,weight)
    PesosDeItems = np.sum(PesosDeMatrizRelacion,axis=1)
    PesosParaEvaluarFactibilidad = np.sum(np.multiply(matrixBin,PesosDeItems),axis=1)
    EvaluarReparacion = np.where(PesosParaEvaluarFactibilidad <= knapsackSize, 1, 0)

    return EvaluarReparacion, PesosDeItems

def reparar1(vectorBin, knapsackSize, relationMatrix, weight, profit,PesosDeItems): #Reparación "Simple" con un orden según peso de los items

    indicadorReparacion = np.divide(profit,PesosDeItems) #Nos entregará un vector de indicadores de la reción profit/weight, en donde el menor valor corresponde a los items menos importantes
    RankIndicadorReparacion= (-indicadorReparacion).argsort()

    for i in range(len(RankIndicadorReparacion)):
        if np.sum(np.multiply(vectorBin,PesosDeItems)) > knapsackSize:
            vectorBin[RankIndicadorReparacion[i]] = 0
        else:
            break

    return vectorBin

def reparar2(vectorBin, knapsackSize, relationMatrix, weight, profit,PesosDeItems,numItem): #Reparación según ponderación p_i/R_i del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)

    frecuenciasDeElementos = np.divide(np.sum(relationMatrix, axis=0),numItem)
    PesosDeMatrizRelacion = np.multiply(relationMatrix,weight)
    R = np.sum(np.divide(PesosDeMatrizRelacion,frecuenciasDeElementos), axis=1) #Vector de tamaño items
    indicadorReparacion = np.divide(profit,R) #Vector tamaño items

    indicadorReparacion = np.multiply(indicadorReparacion,vectorBin)

    maxindicador = np.sum(indicadorReparacion)
    indicadorReparacion = np.where(indicadorReparacion == 0, indicadorReparacion-maxindicador, indicadorReparacion)
    # print(f'indicadorReparacion: {indicadorReparacion}')

    RankIndicadorReparacion= (-indicadorReparacion).argsort()

    cant = np.sum(np.where(indicadorReparacion == -maxindicador, 1, 0)) #Cantidad de "0" en nuestro indicador, es decir items no elegidos en nuestro vector solución evaluado

    RankIndicadorReparacion = RankIndicadorReparacion[:-cant]

    # print(f'RankIndicadorReparacion.shape: {RankIndicadorReparacion.shape}')

    vectorBinZ = np.zeros(vectorBin.shape)

    #Linea 4 del algoritmo 2 del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
    for i in range(len(RankIndicadorReparacion)):
        if PesosDeItems[RankIndicadorReparacion[i]] <= (knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems))): #Comprobamos si el items entra en el espacio disponible en la mochila
            vectorBinZ[RankIndicadorReparacion[i]] = 1
        else:
            continue

    #Linea 8 del algoritmo 2 del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)

    vectorBinZInvertida = np.where(vectorBinZ == 1, 0, 1) #Es decir que los 1 serán 0 y los 0 serán 1.

    indicadorReparacion = np.multiply(indicadorReparacion,vectorBinZInvertida)

    maxindicador = np.sum(indicadorReparacion)
    indicadorReparacion = np.where(indicadorReparacion == 0, indicadorReparacion-maxindicador, indicadorReparacion)

    RankIndicadorReparacion= (-indicadorReparacion).argsort()

    cant = np.sum(np.where(RankIndicadorReparacion == -maxindicador, 1, 0)) #Cantidad de "0" en nuestro indicador, es decir items no elegidos en nuestro vector solución evaluado

    RankIndicadorReparacion = RankIndicadorReparacion[:-cant]

    for i in range(len(RankIndicadorReparacion)):
        if PesosDeItems[RankIndicadorReparacion[i]] <= (knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems))): #Comprobamos si el items entra en el espacio disponible en la mochila
            vectorBinZ[RankIndicadorReparacion[i]] = 1
        else:
            continue

    vectorBin = vectorBinZ
    # print(f'Peso de la mochila: {np.sum(np.multiply(vectorBin,PesosDeItems))}')

    return vectorBin

def reparar3(vectorBin, knapsackSize, relationMatrix, weight, profit,PesosDeItems,numItem): #Reparación según ponderación p_i/R_i del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)

    frecuenciasDeElementos = np.divide(np.sum(relationMatrix, axis=0),numItem)
    PesosDeMatrizRelacion = np.multiply(relationMatrix,weight)
    R = np.sum(np.divide(PesosDeMatrizRelacion,frecuenciasDeElementos), axis=1) #Vector de tamaño items
    indicadorReparacion = np.divide(profit,R) #Vector tamaño items

    # indicadorReparacion = np.multiply(indicadorReparacion,vectorBin)

    # maxindicador = np.sum(indicadorReparacion)
    # indicadorReparacion = np.where(indicadorReparacion == 0, indicadorReparacion-maxindicador, indicadorReparacion)
    # print(f'indicadorReparacion: {indicadorReparacion}')

    RankIndicadorReparacion= (indicadorReparacion).argsort()

    # cant = np.sum(np.where(indicadorReparacion == -maxindicador, 1, 0)) #Cantidad de "0" en nuestro indicador, es decir items no elegidos en nuestro vector solución evaluado

    # RankIndicadorReparacion = RankIndicadorReparacion[:-cant]

    # print(f'RankIndicadorReparacion.shape: {RankIndicadorReparacion.shape}')

    #vectorBinZ = np.zeros(vectorBin.shape)
    vectorBinZ = vectorBin

    #Linea 4 del algoritmo 2 del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
    for i in range(len(RankIndicadorReparacion)):
        # print(f'np.sum(np.multiply(vectorBinZ,PesosDeItems)): {np.sum(np.multiply(vectorBinZ,PesosDeItems))}')
        # print(f'knapsackSize: {knapsackSize}')
        if np.sum(np.multiply(vectorBinZ,PesosDeItems)) >= knapsackSize: #Comprobamos si el items entra en el espacio disponible en la mochila
            vectorBinZ[RankIndicadorReparacion[i]] = 0
        else:
            continue

    #Linea 8 del algoritmo 2 del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)

    vectorBinZInvertida = np.where(vectorBinZ == 1, 0, 1) #Es decir que los 1 serán 0 y los 0 serán 1.

    # indicadorReparacion = np.multiply(indicadorReparacion,vectorBinZInvertida)

    # maxindicador = np.sum(indicadorReparacion)
    # indicadorReparacion = np.where(indicadorReparacion == 0, indicadorReparacion-maxindicador, indicadorReparacion)

    RankIndicadorReparacion= (-indicadorReparacion).argsort()

    # cant = np.sum(np.where(RankIndicadorReparacion == -maxindicador, 1, 0)) #Cantidad de "0" en nuestro indicador, es decir items no elegidos en nuestro vector solución evaluado

    # RankIndicadorReparacion = RankIndicadorReparacion[:-cant]

    for i in range(len(RankIndicadorReparacion)):
        # print("*************************************************")
        # print(f'np.sum(np.multiply(vectorBinZ,PesosDeItems)): {np.sum(np.multiply(vectorBinZ,PesosDeItems))}')
        # print(f'knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems)): {knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems))}')

        if PesosDeItems[RankIndicadorReparacion[i]] <= (knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems))): #Comprobamos si el items entra en el espacio disponible en la mochila
            vectorBinZ[RankIndicadorReparacion[i]] = 1
        else:
            continue

    vectorBin = vectorBinZ
    # print(f'Peso de la mochila: {np.sum(np.multiply(vectorBin,PesosDeItems))}')

    return vectorBin

def reparar4(vectorBin, knapsackSize, relationMatrix, weight, profit,PesosDeItems,numItem): #Reparación según ponderación p_i/R_i del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)

    frecuenciasDeElementos = np.divide(np.sum(relationMatrix, axis=0),numItem)
    PesosDeMatrizRelacion = np.multiply(relationMatrix,weight)
    R = np.sum(np.divide(PesosDeMatrizRelacion,frecuenciasDeElementos), axis=1) #Vector de tamaño items
    indicadorReparacion = np.divide(profit,R) #Vector tamaño items

    RankIndicadorReparacion= (-indicadorReparacion).argsort()

    # vectorBinZ = vectorBin
    vectorBinZ = np.zeros(numItem)

    #Linea 4 del algoritmo 2 del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
    for i in range(len(RankIndicadorReparacion)):
        # if np.sum(np.multiply(vectorBinZ,PesosDeItems)) >= knapsackSize: #Comprobamos si el items entra en el espacio disponible en la mochila
        if PesosDeItems[RankIndicadorReparacion[i]] <= (knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems))) and vectorBin[RankIndicadorReparacion[i]] == 1: #Comprobamos si el items entra en el espacio disponible en la mochila
            rand = np.random.rand()
            if rand <= 1:
                vectorBinZ[RankIndicadorReparacion[i]] = 1
        else:
            continue
   #Linea 8 del algoritmo 2 del paper (https://sci-hub.se/https://doi.org/10.1016/j.future.2017.05.044)
    RankIndicadorReparacion= (-indicadorReparacion).argsort()

    for i in range(len(RankIndicadorReparacion)):

        if PesosDeItems[RankIndicadorReparacion[i]] <= (knapsackSize - np.sum(np.multiply(vectorBinZ,PesosDeItems))) and vectorBinZ[RankIndicadorReparacion[i]] == 0: #Comprobamos si el items entra en el espacio disponible en la mochila
            rand = np.random.rand()
            if rand <= 1:
                vectorBinZ[RankIndicadorReparacion[i]] = 1
        else:
            continue

    vectorBin = vectorBinZ
    # print(f'Peso de la mochila: {np.sum(np.multiply(vectorBin,PesosDeItems))}')

    return vectorBin

def read_instance_SUKP(file,instance_dir,workdirInstance):
    # print(f'file: {file}')
    path = workdirInstance + instance_dir

    numItem = 0 # i, el numero de itemes.
    numElement = 0 # j, el numero de elemento.
    knapsackSize = 0 # Esta es la restriccion.
    profit = np.zeros(numItem) # m
    weight = np.zeros(numElement) # n
    fo = open(path + file , 'r')
    fo, line  = read_line(3,fo)
    posicion = line.find('=')
    numItem = int(line[posicion+1:posicion+5])
    posicion = line.find('=',posicion+1)
    numElement = int(line[posicion+1:posicion+5])
    relationMatrix = np.zeros((numItem, numElement))
    posicion = line.find('=',posicion+1)
    knapsackSize = int(line[posicion+1:posicion+7])
    fo, line  = read_line(3,fo)
    line = line.replace(' \n','').split(' ')
    line = [int(i) for i in line]
    profit = np.asarray(line)
    fo, line  = read_line(3,fo)
    line = line.replace(' \n','').split(' ')
    line = [int(i) for i in line]
    weight = np.asarray(line)
    fo, line  = read_line(2,fo)
    for i in range(0,numItem):
        relation = fo.readline().replace(' \n','').split(' ')
        for j in range(0,numElement):
            relationMatrix[i,j] = int(relation[j])
    print(relationMatrix.shape)
    fo.close()
    return numItem, numElement, knapsackSize, relationMatrix, weight, profit

def read_line(n,fo):
    for i in range(0,n):
        line = fo.readline()
    return fo, line

max_stars_repo_name: m-arnao-molina/SCA-QL-SARSA

max_issues_repo_name: m-arnao-molina/SCA-QL-SARSA

max_forks_repo_name: m-arnao-molina/SCA-QL-SARSA


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

# Copyright (c) Philipp Wagner. All rights reserved.
# Licensed under the BSD license. See LICENSE file in the project root for full license information.

import numpy as np

def minmax(X, low, high, minX=None, maxX=None, dtype=np.float):
    X = np.asarray(X)
    if minX is None:
        minX = np.min(X)
    if maxX is None:
        maxX = np.max(X)
    # normalize to [0...1].
    X = X - float(minX)
    X = X / float((maxX - minX))
    # scale to [low...high].
    X = X * (high-low)
    X = X + low
    return np.asarray(X,dtype=dtype)

def zscore(X, mean=None, std=None):
    X = np.asarray(X)
    if mean is None:
        mean = X.mean()
    if std is None:
        std = X.std()
    X = (X-mean)/std
    return X

max_stars_repo_name: ArianeFire/HaniCam

max_issues_repo_name: ArianeFire/HaniCam

max_forks_repo_name: ArianeFire/HaniCam


text:
#!/usr/bin/env python3

import numpy as np
import h5py
import sys, os, time
import matplotlib.pyplot as plt
import wave, struct

fn = 'x_output.npy'
if len(sys.argv) > 1:
    fn = sys.argv[1]

x = np.load(fn)

with h5py.File('datasets/kicks_dataset.hdf5') as h:
    pcm = np.copy(h['data'][:,:x.shape[1]])
    pcm = pcm/np.maximum(pcm.max(),-pcm.min())

F = 20*np.log10(np.abs(np.fft.rfft(pcm,axis=1))+1e-10)
X = 20*np.log10(np.abs(np.fft.rfft(x,axis=1))+1e-10)
closest = np.zeros(X.shape[0],dtype=int)
similarity_cos = lambda F,X: np.dot(F,X)/(np.linalg.norm(F,axis=1)*np.linalg.norm(X))
similarityL2 = lambda F,X: -np.mean((F-X)**2,axis=1)
for i in range(X.shape[0]):
    closest[i] = np.argmax(similarityL2(F,X[i]))

plt.clf()
for j in range(x.shape[0]):
    plt.subplot(4,4,j*2+1)
    plt.plot(x[j])
    plt.plot(pcm[closest[j]])
    plt.subplot(4,4,j*2+2)
    plt.plot(X[j])
    plt.plot(F[closest[j]])
plt.show()


text:
import os
import json
import sys
import operator
from functools import reduce

import cv2
import matplotlib.pyplot as plt
import numpy as np
from sklearn import linear_model
import scipy.signal as sig

from helpers import first, second, unzip
from imagevariants import generate_jpeg_variants
from imagemeasures import mean_squared_error, normalized_root_mean_square_deviation, peak_signal_to_noise_ratio

def main():
    labels, assessments = load_results('survey-results.json')
    # plot_raw_results(labels, assessments)
    mean_moses = list(mean_opinion_scores(assessments))

    original_image = cv2.imread(os.path.join(
        sys.path[0], 'image-grayscale.png'), cv2.IMREAD_UNCHANGED)
    image_variants = generate_jpeg_variants(original_image)

    mses = [mean_squared_error(original_image, image)
            for (_, image) in image_variants]

    pstnrs = [peak_signal_to_noise_ratio(original_image, image)
              for (_, image) in image_variants]

    nrmsds = [normalized_root_mean_square_deviation(original_image, image)
             for (_, image) in image_variants]

    plt.figure()

    plt.subplot(3, 1, 1)
    plt.plot(labels, mses)
    plt.title('mean squared error')
    plt.xticks(labels)
    plt.grid()

    plt.subplot(3, 1, 2)
    plt.plot(labels, pstnrs)
    plt.title('peak signal to noise ratio')
    plt.xticks(labels)
    plt.grid()

    plt.subplot(3, 1, 3)
    plt.plot(labels, nrmsds)
    plt.xticks(labels)
    plt.grid()
    plt.title('normalized root mean square deviation')

    plt.suptitle('Simple image quality measures')

    plt.figure()
    for i, assessment in enumerate(assessments):
        plt.plot(labels, assessment, '*-', label=f'{i}')
    plt.title('Opinion scores')
    plt.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    plt.yticks([1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5])
    plt.xticks(labels)
    plt.grid()

    plt.figure()

    regr = linear_model.LinearRegression()
    regr.fit(np.array(labels).reshape(-1, 1), mean_moses)
    x_pred = np.linspace(0, 1, 101)
    regr_pred = regr.predict(x_pred.reshape(-1, 1))

    plt.plot(x_pred, regr_pred, '-c',
             label=f'Linear regression model: {round(regr.coef_[0], 2)}x + {round(regr.intercept_, 2)}')
    plt.plot(labels, mean_moses, '*m')

    for xy in zip(labels, mean_moses):
        plt.annotate(second(xy), xy=(first(xy) - 0.02,
                                     second(xy) + 0.1), textcoords='data')

    plt.title('Mean opinion score')
    plt.legend()
    plt.yticks([1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5])
    plt.xticks(labels)
    plt.grid()

    plt.figure()
    plot_correlation(mean_moses, mses, '`Mean MOS-es and mean squared errors correlation')

    plt.figure()
    plot_correlation(
        mean_moses, pstnrs, 'Mean MOSes and peak signal to noise ratios correlation')

    plt.figure()
    plot_correlation(
        mean_moses, nrmsds, 'Mean MOSes and normalized root mean square deviations correlation')

    plt.figure()
    plot_linreg(reduce(operator.concat, assessments), list(mses) * len(assessments), 'MOS', 'mean squared error')

    plt.figure()
    plot_linreg(reduce(operator.concat, assessments), list(pstnrs) * len(assessments), 'MOS', 'peak signal to noise ratio')

    plt.figure()
    plot_linreg(reduce(operator.concat, assessments), list(nrmsds) * len(assessments), 'MOS', 'normalized root mean square deviation')

    plt.show()

def load_results(filename):
    with open(filename) as f:
        data = json.load(f)
        results = list(map(lambda xy_tuples: list(
            unzip(sorted(xy_tuples))), data))
        labels = first(results[0])
        assessments = list(map(second, results))
        return labels, assessments

def mean_opinion_scores(assessments):
    return map(np.mean, zip(*assessments))

def plot_raw_results(labels, assessments):
    for a in assessments:
        plt.plot(labels, a, '-*')
    plt.xticks(labels)
    plt.yticks([1, 2, 3, 4, 5])
    plt.show()

def plot_correlation(a, b, title):
    c = sig.correlate(a, b)
    plt.plot(np.arange(len(c)) - (len(b) - 1), c, '*-')
    plt.axvline(0, lw=1, c='k')
    plt.xlabel('delay')
    plt.ylabel('correlation')
    plt.title(title)
    plt.grid()

def plot_linreg(a, b, a_label, b_label):
    regr = linear_model.LinearRegression()
    regr.fit(np.array(a).reshape(-1, 1), b)
    regr_pred = regr.predict(np.array(a).reshape(-1, 1))

    plt.plot(a, regr_pred, '-c',
             label=f'{round(regr.coef_[0], 2)}x + {round(regr.intercept_, 2)}')
    plt.plot(a, b, '*m')
    plt.legend()
    plt.xlabel(a_label)
    plt.ylabel(b_label)
    plt.title(f'{a_label} to {b_label} linear regression')
    plt.grid()

if __name__ == "__main__":
    main()

max_stars_repo_name: hckr/interactive-multimedia-systems

max_issues_repo_name: hckr/interactive-multimedia-systems

max_forks_repo_name: hckr/interactive-multimedia-systems

reason: import numpy,import scipy


text:
import tellurium as te
import numpy

rr = te.loada ('''
    J1: $Xo -> x; 0.1 + k1*x^4/(k2+x^4);
    x -> $w; k3*x;

    k1 = 0.9;
    k2 = 0.3;
    k3 = 0.7;
    x = 0.05;
''')

m = rr.simulate(0, 15, 100)
for i in range(1, 10):
    rr.x = i*0.2
    mm = rr.simulate(0, 15, 100, ["x"])
    m = numpy.hstack((m, mm))
te.plotArray(m)

max_stars_repo_path: Chapter 13/positiveFeedback.py

max_stars_repo_name: hsauro/PathwayModelingBook

max_issues_repo_path: Chapter 13/positiveFeedback.py

max_issues_repo_name: hsauro/PathwayModelingBook

max_forks_repo_path: Chapter 13/positiveFeedback.py

max_forks_repo_name: hsauro/PathwayModelingBook


text:
from sklearn.datasets import load_iris
iris_dataset = load_iris()
import numpy as np
# print("Keys of iris_dataset: \n {}".format(iris_dataset.keys()))

# print(iris_dataset['DESCR'][:193] + "\n...")

# print("Target names: {}".format(iris_dataset['target_names']))

print("First five columns of data:\n{}".format(iris_dataset['data'][:5]))

from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_split = train_test_split(iris_dataset['data'], iris_dataset['target'], random_state = 0)

from sklearn.neighbors import KNeighborsClassifier

knn = KNeighborsClassifier(n_neighbors=1)

knn.fit(X_train, y_train)

X_new = np.array([[5, 2.9, 1, 0.2]])

print("X_new.shape: {}".format(X_new.shape))
prediction = knn.predict(X_new)
print("Prediction: {}".format(prediction))

print("Predicted target name: {}".format(iris_dataset['target_names'][prediction]))