text:
"""
Copyright (C) 2013-2019 Calliope contributors listed in AUTHORS.
Licensed under the Apache 2.0 License (see LICENSE file).

capacity.py
~~~~~~~~~~~~~~~~~

Capacity constraints for technologies (output, resource, area, and storage).

"""

import pyomo.core as po  # pylint: disable=import-error
import numpy as np

from calliope.backend.pyomo.util import get_param, split_comma_list
from calliope import exceptions

ORDER = 10  # order in which to invoke constraints relative to other constraint files

def load_constraints(backend_model):
    sets = backend_model.__calliope_model_data['sets']

    if backend_model.__calliope_run_config['mode'] == 'operate':
        return None
    else:
        if 'loc_techs_storage_capacity_constraint' in sets:
            backend_model.storage_capacity_constraint = po.Constraint(
                backend_model.loc_techs_storage_capacity_constraint,
                rule=storage_capacity_constraint_rule
            )

        if 'loc_techs_energy_capacity_storage_min_constraint' in sets:
            backend_model.energy_capacity_storage_min_constraint = po.Constraint(
                backend_model.loc_techs_energy_capacity_storage_min_constraint,
                rule=energy_capacity_storage_min_constraint_rule
            )

        if 'loc_techs_energy_capacity_storage_max_constraint' in sets:
            backend_model.energy_capacity_storage_max_constraint = po.Constraint(
                backend_model.loc_techs_energy_capacity_storage_max_constraint,
                rule=energy_capacity_storage_max_constraint_rule
            )

        if 'loc_techs_energy_capacity_storage_equals_constraint' in sets:
            backend_model.energy_capacity_storage_equals_constraint = po.Constraint(
                backend_model.loc_techs_energy_capacity_storage_equals_constraint,
                rule=energy_capacity_storage_equals_constraint_rule
            )

        if 'loc_techs_energy_capacity_storage_constraint_old' in sets:
            backend_model.energy_capacity_storage_constraint_old = po.Constraint(
                backend_model.loc_techs_energy_capacity_storage_constraint_old,
                rule=energy_capacity_storage_constraint_rule_old
            )

        if 'loc_techs_resource_capacity_constraint' in sets:
            backend_model.resource_capacity_constraint = po.Constraint(
                backend_model.loc_techs_resource_capacity_constraint,
                rule=resource_capacity_constraint_rule
            )

        if 'loc_techs_resource_capacity_equals_energy_capacity_constraint' in sets:
            backend_model.resource_capacity_equals_energy_capacity_constraint = po.Constraint(
                backend_model.loc_techs_resource_capacity_equals_energy_capacity_constraint,
                rule=resource_capacity_equals_energy_capacity_constraint_rule
            )

        if 'loc_techs_resource_area_constraint' in sets:
            backend_model.resource_area_constraint = po.Constraint(
                backend_model.loc_techs_resource_area_constraint,
                rule=resource_area_constraint_rule
            )

        if 'loc_techs_resource_area_per_energy_capacity_constraint' in sets:
            backend_model.resource_area_per_energy_capacity_constraint = po.Constraint(
                backend_model.loc_techs_resource_area_per_energy_capacity_constraint,
                rule=resource_area_per_energy_capacity_constraint_rule
            )

        if 'locs_resource_area_capacity_per_loc_constraint' in sets:
            backend_model.resource_area_capacity_per_loc_constraint = po.Constraint(
                backend_model.locs_resource_area_capacity_per_loc_constraint,
                rule=resource_area_capacity_per_loc_constraint_rule
            )

        if 'loc_techs_energy_capacity_constraint' in sets:
            backend_model.energy_capacity_constraint = po.Constraint(
                backend_model.loc_techs_energy_capacity_constraint,
                rule=energy_capacity_constraint_rule
            )

        if 'techs_energy_capacity_systemwide_constraint' in sets:
            backend_model.energy_capacity_systemwide_constraint = po.Constraint(
                backend_model.techs_energy_capacity_systemwide_constraint,
                rule=energy_capacity_systemwide_constraint_rule
            )

def get_capacity_constraint(backend_model, parameter, loc_tech,
                            _equals=None, _max=None, _min=None, scale=None):

    decision_variable = getattr(backend_model, parameter)

    if not _equals:
        _equals = get_param(backend_model, parameter + '_equals', loc_tech)
    if not _max:
        _max = get_param(backend_model, parameter + '_max', loc_tech)
    if not _min:
        _min = get_param(backend_model, parameter + '_min', loc_tech)
    if po.value(_equals) is not False and po.value(_equals) is not None:
        if np.isinf(po.value(_equals)):
            e = exceptions.ModelError
            raise e('Cannot use inf for {}_equals for loc:tech `{}`'.format(parameter, loc_tech))
        if scale:
            _equals *= scale
        return decision_variable[loc_tech] == _equals
    else:
        if po.value(_min) == 0 and np.isinf(po.value(_max)):
            return po.Constraint.NoConstraint
        else:
            if scale:
                _max *= scale
                _min *= scale
            return (_min, decision_variable[loc_tech], _max)

def storage_capacity_constraint_rule(backend_model, loc_tech):
    """
    Set maximum storage capacity. Supply_plus & storage techs only

    The first valid case is applied:

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{storage_{cap}}(loc::tech)
            \\begin{cases}
                = storage_{cap, equals}(loc::tech),& \\text{if } storage_{cap, equals}(loc::tech)\\\\
                \\leq storage_{cap, max}(loc::tech),& \\text{if } storage_{cap, max}(loc::tech)\\\\
                \\text{unconstrained},& \\text{otherwise}
            \\end{cases}
            \\forall loc::tech \\in loc::techs_{store}

    and (if ``equals`` not enforced):

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{storage_{cap}}(loc::tech) \\geq storage_{cap, min}(loc::tech)
            \\quad \\forall loc::tech \\in loc::techs_{store}

    """
    return get_capacity_constraint(backend_model, 'storage_cap', loc_tech)

def energy_capacity_storage_constraint_rule_old(backend_model, loc_tech):
    """
    Set an additional energy capacity constraint on storage technologies,
    based on their use of `charge_rate`.

    This is deprecated and will be removed in Calliope 0.7.0. Instead of
    `charge_rate`, please use `energy_cap_per_storage_cap_max`.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{energy_{cap}}(loc::tech)
            \\leq \\boldsymbol{storage_{cap}}(loc::tech) \\times charge\\_rate(loc::tech)
            \\quad \\forall loc::tech \\in loc::techs_{store}

    """
    charge_rate = get_param(backend_model, 'charge_rate', loc_tech)

    return backend_model.energy_cap[loc_tech] <= (
        backend_model.storage_cap[loc_tech] * charge_rate
    )

def energy_capacity_storage_min_constraint_rule(backend_model, loc_tech):
    """
    Limit energy capacities of storage technologies based on their storage capacities.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{energy_{cap}}(loc::tech)
                \\geq \\boldsymbol{storage_{cap}}(loc::tech) \\times energy\\_cap\\_per\\_storage\\_cap\\_min(loc::tech)\\\\
            \\forall loc::tech \\in loc::techs_{store}

    """
    return backend_model.energy_cap[loc_tech] >= (
        backend_model.storage_cap[loc_tech] * get_param(backend_model, 'energy_cap_per_storage_cap_min', loc_tech)
    )

def energy_capacity_storage_max_constraint_rule(backend_model, loc_tech):
    """
    Limit energy capacities of storage technologies based on their storage capacities.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{energy_{cap}}(loc::tech)
                \\leq \\boldsymbol{storage_{cap}}(loc::tech) \\times energy\\_cap\\_per\\_storage\\_cap\\_max(loc::tech)\\\\
            \\forall loc::tech \\in loc::techs_{store}

    """
    return backend_model.energy_cap[loc_tech] <= (
        backend_model.storage_cap[loc_tech] * get_param(backend_model, 'energy_cap_per_storage_cap_max', loc_tech)
    )

def energy_capacity_storage_equals_constraint_rule(backend_model, loc_tech):
    """
    Limit energy capacities of storage technologies based on their storage capacities.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{energy_{cap}}(loc::tech)
                = \\boldsymbol{storage_{cap}}(loc::tech) \\times energy\\_cap\\_per\\_storage\\_cap\\_equals(loc::tech)
            \\forall loc::tech \\in loc::techs_{store}

    """
    return backend_model.energy_cap[loc_tech] == (
        backend_model.storage_cap[loc_tech] * get_param(backend_model, 'energy_cap_per_storage_cap_equals', loc_tech)
    )

def resource_capacity_constraint_rule(backend_model, loc_tech):
    """
    Add upper and lower bounds for resource_cap.

    The first valid case is applied:

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{resource_{cap}}(loc::tech)
            \\begin{cases}
                = resource_{cap, equals}(loc::tech),& \\text{if } resource_{cap, equals}(loc::tech)\\\\
                \\leq resource_{cap, max}(loc::tech),& \\text{if } resource_{cap, max}(loc::tech)\\\\
                \\text{unconstrained},& \\text{otherwise}
            \\end{cases}
            \\forall loc::tech \\in loc::techs_{finite\\_resource\\_supply\\_plus}

    and (if ``equals`` not enforced):

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{resource_{cap}}(loc::tech) \\geq resource_{cap, min}(loc::tech)
            \\quad \\forall loc::tech \\in loc::techs_{finite\\_resource\\_supply\\_plus}
    """

    return get_capacity_constraint(backend_model, 'resource_cap', loc_tech)

def resource_capacity_equals_energy_capacity_constraint_rule(backend_model, loc_tech):
    """
    Add equality constraint for resource_cap to equal energy_cap, for any technologies
    which have defined resource_cap_equals_energy_cap.

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{resource_{cap}}(loc::tech) = \\boldsymbol{energy_{cap}}(loc::tech)
            \\quad \\forall loc::tech \\in loc::techs_{finite\\_resource\\_supply\\_plus}
            \\text{ if } resource\\_cap\\_equals\\_energy\\_cap = \\text{True}
    """
    return backend_model.resource_cap[loc_tech] == backend_model.energy_cap[loc_tech]

def resource_area_constraint_rule(backend_model, loc_tech):
    """
    Set upper and lower bounds for resource_area.

    The first valid case is applied:

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{resource_{area}}(loc::tech)
            \\begin{cases}
                = resource_{area, equals}(loc::tech),& \\text{if } resource_{area, equals}(loc::tech)\\\\
                \\leq resource_{area, max}(loc::tech),& \\text{if } resource_{area, max}(loc::tech)\\\\
                \\text{unconstrained},& \\text{otherwise}
            \\end{cases}
            \\forall loc::tech \\in loc::techs_{area}

    and (if ``equals`` not enforced):

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{resource_{area}}(loc::tech) \\geq resource_{area, min}(loc::tech)
            \\quad \\forall loc::tech \\in loc::techs_{area}
    """
    energy_cap_max = get_param(backend_model, 'energy_cap_max', loc_tech)
    area_per_energy_cap = get_param(backend_model, 'resource_area_per_energy_cap', loc_tech)

    if po.value(energy_cap_max) == 0 and not po.value(area_per_energy_cap):
        # If a technology has no energy_cap here, we force resource_area to zero,
        # so as not to accrue spurious costs
        return backend_model.resource_area[loc_tech] == 0
    else:
        return get_capacity_constraint(backend_model, 'resource_area', loc_tech)

def resource_area_per_energy_capacity_constraint_rule(backend_model, loc_tech):
    """
    Add equality constraint for resource_area to equal a percentage of energy_cap,
    for any technologies which have defined resource_area_per_energy_cap

    .. container:: scrolling-wrapper

        .. math::

            \\boldsymbol{resource_{area}}(loc::tech) =
            \\boldsymbol{energy_{cap}}(loc::tech) \\times area\\_per\\_energy\\_cap(loc::tech)
            \\quad \\forall loc::tech \\in locs::techs_{area} \\text{ if } area\\_per\\_energy\\_cap(loc::tech)
    """
    area_per_energy_cap = get_param(backend_model, 'resource_area_per_energy_cap', loc_tech)

    return (backend_model.resource_area[loc_tech] ==
            backend_model.energy_cap[loc_tech] * area_per_energy_cap)

def resource_area_capacity_per_loc_constraint_rule(backend_model, loc):
    """
    Set upper bound on use of area for all locations which have `available_area`
    constraint set. Does not consider resource_area applied to demand technologies

    .. container:: scrolling-wrapper

        .. math::

            \\sum_{tech} \\boldsymbol{resource_{area}}(loc::tech) \\leq available\\_area
            \\quad \\forall loc \\in locs \\text{ if } available\\_area(loc)
    """
    model_data_dict = backend_model.__calliope_model_data['data']
    available_area = model_data_dict['available_area'][loc]

    loc_techs = split_comma_list(model_data_dict['lookup_loc_techs_area'][loc])

    return (
        sum(backend_model.resource_area[loc_tech] for loc_tech in loc_techs)
        <= available_area
    )

def energy_capacity_constraint_rule(backend_model, loc_tech):
    """
    Set upper and lower bounds for energy_cap.

    The first valid case is applied:

    .. container:: scrolling-wrapper

        .. math::

            \\frac{\\boldsymbol{energy_{cap}}(loc::tech)}{energy_{cap, scale}(loc::tech)}
            \\begin{cases}
                = energy_{cap, equals}(loc::tech),& \\text{if } energy_{cap, equals}(loc::tech)\\\\
                \\leq energy_{cap, max}(loc::tech),& \\text{if } energy_{cap, max}(loc::tech)\\\\
                \\text{unconstrained},& \\text{otherwise}
            \\end{cases}
            \\forall loc::tech \\in loc::techs

    and (if ``equals`` not enforced):

    .. container:: scrolling-wrapper

        .. math::

            \\frac{\\boldsymbol{energy_{cap}}(loc::tech)}{energy_{cap, scale}(loc::tech)}
            \\geq energy_{cap, min}(loc::tech)
            \\quad \\forall loc::tech \\in loc::techs
    """
    scale = get_param(backend_model, 'energy_cap_scale', loc_tech)
    return get_capacity_constraint(backend_model, 'energy_cap', loc_tech, scale=scale)

def energy_capacity_systemwide_constraint_rule(backend_model, tech):
    """
    Set constraints to limit the capacity of a single technology type across all locations in the model.

    The first valid case is applied:

    .. container:: scrolling-wrapper

        .. math::

            \\sum_{loc}\\boldsymbol{energy_{cap}}(loc::tech)
            \\begin{cases}
                = energy_{cap, equals, systemwide}(loc::tech),&
                    \\text{if } energy_{cap, equals, systemwide}(loc::tech)\\\\
                \\leq energy_{cap, max, systemwide}(loc::tech),&
                    \\text{if } energy_{cap, max, systemwide}(loc::tech)\\\\
                \\text{unconstrained},& \\text{otherwise}
            \\end{cases}
            \\forall tech \\in techs

    """

    if tech in getattr(backend_model, 'techs_transmission_names', []):
        all_loc_techs = [
            i for i in backend_model.loc_techs_transmission
            if i.split('::')[1].split(':')[0] == tech
        ]
        multiplier = 2  # there are always two technologies associated with one link
    else:
        all_loc_techs = [
            i for i in backend_model.loc_techs
            if i.split('::')[1] == tech
        ]
        multiplier = 1

    max_systemwide = get_param(backend_model, 'energy_cap_max_systemwide', tech)
    equals_systemwide = get_param(backend_model, 'energy_cap_equals_systemwide', tech)

    if np.isinf(po.value(max_systemwide)) and not equals_systemwide:
        return po.Constraint.NoConstraint
    elif equals_systemwide and np.isinf(po.value(equals_systemwide)):
        raise exceptions.ModelError(
            'Cannot use inf for energy_cap_equals_systemwide for tech `{}`'.format(tech)
        )

    sum_expr = sum(backend_model.energy_cap[loc_tech] for loc_tech in all_loc_techs)

    if equals_systemwide:
        return sum_expr == equals_systemwide * multiplier
    else:
        return sum_expr <= max_systemwide * multiplier

reason: import numpy,import pyomo


text:
"""

@author : Spencer Lyon

"""
from __future__ import division
import unittest
import numpy as np
from ifp import ConsumerProblem
from quantecon import compute_fixed_point
from quantecon.tests import get_h5_data_file, write_array, max_abs_diff

def _solve_via_vfi(cp, v_init, return_both=False):
    "compute policy rule using value function iteration"
    v = compute_fixed_point(cp.bellman_operator, v_init, verbose=False,
                            error_tol=1e-5,
                            max_iter=1000)

    # Run one more time to get the policy
    p = cp.bellman_operator(v, return_policy=True)

    if return_both:
        return v, p
    else:
        return p

def _solve_via_pfi(cp, c_init):
    "compute policy rule using policy function iteration"
    p = compute_fixed_point(cp.coleman_operator, c_init, verbose=False,
                            error_tol=1e-5,
                            max_iter=1000)

    return p

def _get_vfi_pfi_guesses(cp, force_new=False):
    """
    load precomputed vfi/pfi solutions, or compute them if requested
    or we can't find old ones
    """
    # open the data file
    with get_h5_data_file() as f:

        # See if the ifp group already exists
        group_existed = True
        try:
            ifp_group = f.getNode("/ifp")
        except:
            # doesn't exist
            group_existed = False
            ifp_group = f.create_group("/", "ifp", "data for ifp.py tests")

        if force_new or not group_existed:
            # group doesn't exist, or forced to create new data.
            # This function updates f in place and returns v_vfi, c_vfi, c_pfi
            v_vfi, c_vfi, c_pfi = _new_solutions(cp, f, ifp_group)

            # We have what we need, so return
            return v_vfi, c_pfi

        # if we made it here, the group exists and we should try to read
        # existing solutions
        try:  # read in vfi
            # Try reading vfi
            c_vfi = ifp_group.c_vfi[:]
            v_vfi = ifp_group.v_vfi[:]

        except:
            # doesn't exist. Let's create it
            v_vfi, c_vfi = _new_solutions(cp, f, ifp_group, which="vfi")

        try:  # read in pfi
            # Try reading pfi
            c_pfi = ifp_group.c_pfi[:]

        except:
            # doesn't exist. Let's create it
            c_pfi = _new_solutions(cp, f, ifp_group, which="pfi")

    return v_vfi, c_pfi

def _new_solutions(cp, f, grp, which="both"):
    v_init, c_init = cp.initialize()
    if which == "both":

        v_vfi, c_vfi = _solve_via_vfi(cp, v_init, return_both=True)
        c_pfi = _solve_via_pfi(cp, c_init)

        # Store solutions in chunked arrays...
        write_array(f, grp, c_vfi, "c_vfi")
        write_array(f, grp, v_vfi, "v_vfi")
        write_array(f, grp, c_pfi, "c_pfi")

        return v_vfi, c_vfi, c_pfi

    elif which == "vfi":
        v_vfi, c_vfi = _solve_via_vfi(cp, v_init, return_both=True)
        write_array(f, grp, c_vfi, "c_vfi")
        write_array(f, grp, v_vfi, "v_vfi")

        return v_vfi, c_vfi

    elif which == "pfi":
        c_pfi = _solve_via_pfi(cp, c_init)
        write_array(f, grp, c_pfi, "c_pfi")

        return c_pfi

class TestConsumerProblem(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls.cp = ConsumerProblem()

        # get precomputed answer for each method
        print("reading old solutions")
        old_vfi_sol, old_pfi_sol = _get_vfi_pfi_guesses(cls.cp)
        cls.v_vfi = old_vfi_sol

        # compute answers again, using something close to old answer as
        # initial value so it goes really fast
        print("computing new vfi")
        cls.c_vfi = _solve_via_vfi(cls.cp, old_vfi_sol * 0.99999)
        print("computing new pfi")
        cls.c_pfi = _solve_via_pfi(cls.cp, old_pfi_sol * 0.99999)

    def test_bellman_coleman_solutions_agree(self):
        "ifp: bellman and coleman solutions agree"
        self.assertLessEqual(max_abs_diff(self.c_vfi, self.c_pfi), 0.2)

    def test_bellman_fp(self):
        "ifp: solution to bellman is a fixed point"
        new_v = self.cp.bellman_operator(self.v_vfi)
        self.assertLessEqual(max_abs_diff(self.v_vfi, new_v), 1e-3)

    def test_coleman_fp(self):
        "ifp: solution to coleman is a fixed point"
        new_c = self.cp.coleman_operator(self.c_pfi)
        self.assertLessEqual(max_abs_diff(self.c_pfi, new_c), 1e-3)

    def test_initialize(self):
        "ifp: initialize function works"
        i = self.cp.initialize()

        # returned two things?
        self.assertEqual(len(i), 2)

        shapes = (len(self.cp.asset_grid), len(self.cp.z_vals))
        self.assertEqual(i[0].shape, shapes)
        self.assertEqual(i[1].shape, shapes)

ext: py

reason: import numpy


text:
#!/usr/bin/env python3
import sys

import numpy as np
import scipy.stats

def parse_angsd_abbababa(filename):
    # print("H1", "H2", "H3", "nABBA", "nBABA", "Dstat", "bootEst", "SE", "Z", sep="\t")
    return np.loadtxt(
        filename,
        usecols=(0, 1, 2, 3, 4, 5, 6, 7, 8),
        skiprows=1,
        dtype=[
            ("h1", object),
            ("h2", object),
            ("h3", object),
            ("nABBA", float),
            ("nBABA", float),
            ("Dstat", float),
            ("bootEst", float),
            ("SE", float),
            ("Z", float),
        ],
        unpack=True,
    )

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print(f"usage: {sys.argv[0]} angsd.abbababa")
        exit(1)

    angsd_file = sys.argv[1]
    *_, Z = parse_angsd_abbababa(angsd_file)
    Z[np.where(Z < 0)] *= -1  # make negative values positve
    p = scipy.stats.norm.sf(Z)  # 1-tailed p-value
    p = np.sort(p)
    alpha = 0.05
    for i, pval in enumerate(p):
        thres = alpha / (len(p) - i)
        if pval >= thres:
            print(i, thres)
            break

reason: import numpy,import scipy


text:
import torch
import numpy as np
import torch.nn as nn
from models.NonBayesianModels.AlexNet import AlexNet
from main_frequentist import getModel
import data

import config_frequentist as cfg
if __name__ == '__main__':

    train_on_gpu = torch.cuda.is_available()
    dataset='covid19'
    net_type = 'alexnet'
    classes = ["covid19", "non"]

    # CUDA settings
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    # Hyper Parameter settings
    n_epochs = cfg.n_epochs
    lr = cfg.lr
    num_workers = cfg.num_workers
    valid_size = cfg.valid_size
    batch_size = cfg.batch_size
    num_classes = len(classes)

    trainset, testset, inputs, outputs = data.getDataset(dataset)
    train_loader, valid_loader, test_loader = data.getDataloader(
        trainset, testset, valid_size, batch_size, num_workers)

    model = getModel(net_type, inputs, outputs)
    #model = getModel(net_type, 1, 10, priors=None, layer_type=layer_type, activation_type=activation_type)
    model.load_state_dict(torch.load("./checkpoints/covid19/frequentist/model_alexnet.pt"))
    model.train()
    model.to(device)

    criterion = nn.CrossEntropyLoss()

    Num_fruits = len(classes)

    test_loss = 0.0
    class_correct = list(0. for i in range(Num_fruits))
    class_total = list(0. for i in range(Num_fruits))

    # iterate over test data
    for data, target in test_loader:
        print(np.shape(data))
        # move tensors to GPU if CUDA is available
        data, target = data.cuda(), target.cuda()
        # forward pass: compute predicted outputs by passing inputs to the model
        output = model(data)
        # calculate the batch loss
        loss = criterion(output, target)
        # update  test loss
        test_loss += loss.item()*data.size(0)
        # convert output probabilities to predicted class
        _, pred = torch.max(output, 1)
        # compare predictions to true label
        correct_tensor = pred.eq(target.data.view_as(pred))
        correct = np.squeeze(correct_tensor.numpy()) if not train_on_gpu else np.squeeze(correct_tensor.cpu().numpy())
        # calculate test accuracy for each object class
        for i in range(len(target)):
            label = target.data[i]
            class_correct[label] += correct[i].item()
            class_total[label] += 1

    # calculate avg test loss
    test_loss = test_loss/len(test_loader.dataset)
    print('Test Loss: {:.6f}\n'.format(test_loss))

    for i in range(Num_fruits):
        if class_total[i] > 0:
            print('Test Accuracy of %5s: %2d%% (%2d/%2d)' % (
                classes[i], 100 * class_correct[i] / class_total[i],
                np.sum(class_correct[i]), np.sum(class_total[i])))
        else:
            print('Test Accuracy of %5s: N/A (no training examples)' % (classes[i]))

    print('\nTest Accuracy (Overall): %2d%% (%2d/%2d)' % (
        100. * np.sum(class_correct) / np.sum(class_total),
        np.sum(class_correct), np.sum(class_total)))


text:
from functools import reduce
import time
import math
import copy
import types
import cv2
import numpy as np
import json
from typing import List, Union, Tuple, Any

from ..thresholds.color import built_in_colors
from ..exceptions.exceptions import InvalidCustomFunctionError, CameraError
from ..camera.camera import Camera
from ..partials.filter_applier import filter_applier
from ..thresholds.threshold import Threshold
from ..thresholds.color.color import Color
from ..connections.connection import Connection
from ..directions.director import Director
from ..camera.camera_settings import CameraSettings
from ..connections.network_location import NetworkLocation
from ..directions.directing_functions import center_directions

class Vision:
    """
    The Vision object represents the specifications to detect an object in an image.
    It performs the detection using a Threshold and filter functions.

    Additional capabilities and tuning options are:
        Image filters (Blurs, rotations, cropping),
        Morphological functions,
        Ovl color HSVCalibration,

    """
    def __init__(self, threshold: Threshold = None, contour_filters: List[types.FunctionType] = None,
                 director: Director = None, width=320, height=240, connection: Connection = None,
                 camera: Union[int, str, Camera, cv2.VideoCapture, Any] = None,
                 camera_settings: CameraSettings = None, morphological_functions: List[types.FunctionType] = None,
                 image_filters: List[types.FunctionType] = None, ovl_camera: bool = False, calibration: str = None):
        """
        The object that represents the pipeline of processing, detection, direction of values
        A connection object can be passed
        :param threshold: threshold object, creates the binary mask from a given image
        :param contour_filters: the list of contour_filter functions that
                                remove contours that aren't the target(s)
        :param director: a functions that receives a list or a single contour and returns director
        :param width: the width (in pixels) of images taken with the camera
        :param height: the height (in pixels)
        :param connection: a connection object that passes the result to the connection target
        :param camera: a Camera object (cv2.VideoCapture, ovl.Camera) or source from which to open a camera
        :param camera_settings: Special camera settings like calibration or offset used for
                                image correction and various direction calculations.
        :param morphological_functions: morphological functions used to apply on the binary
                               mask generated by the Threshold.
        :param image_filters: a list of image altering functions that are applied on the image.
        :param ovl_camera: a boolean that makes the camera opened to be ovl.Camera instead of cv2.VideoCapture
        :param calibration: a dictionary containing color calibration (HSVCalibration) coefficients and intercepts,
                            used for applying the calibration on the vision object

        """
        self.width = width
        self.height = height
        self.threshold = threshold
        self.contour_filters = contour_filters or []
        self.director = director or Director(center_directions, failed_detection=9999, target_amount=1)
        self.connection = connection
        self.image_filters = image_filters or []
        self.morphological_functions = morphological_functions or []
        self.camera = None
        self.camera_port = None
        self.camera_settings = camera_settings
        if isinstance(camera, (cv2.VideoCapture, Camera)):
            self.camera = camera
        elif camera is None:
            pass
        else:
            self.camera_setup(camera, width, height, ovl_camera=ovl_camera)

        self.calibration_path = None
        if calibration:
            self.calibration_path = calibration
            calibration = json.load(calibration)
            self.saturation_weight = calibration['saturation'] if 'saturation' in calibration else None
            self.brightness_weight = calibration['brightness'] if 'brightness' in calibration else None

    def __repr__(self):
        return str(self)

    def __str__(self):
        filters = [filter_function.__name__ for filter_function in self.contour_filters]
        threshold = self.threshold
        return "Vision: \n Threshold: {} \n Filters: {}".format(threshold, filters)

    @property
    def target_amount(self):
        """
        The wanted amount of targets
        Determined by self.director
        (0 None or math.inf if there is no limit, 1 if 1 target is wanted etc.)

        """
        if self.director is None:
            return math.inf
        return self.director.target_amount

    def apply_morphological_functions(self, mask, morphological_functions=None):
        """
        Applies all morphological functions on the mask (binary images) created using the threshold,
        Morphological functions are functions that are applied
        to binary images to alter the shape of "detected" regions

        NOTE: Vision.detect is mainly used for full object detection and filtering,
        refer to it for common use of Vision

        :param mask: the mask on which the functions should be applied
        :param morphological_functions: list of morphological_functions to be
         applied instead of self.morphological_functions
        :return: the applied mask

        """
        if type(self.morphological_functions) not in (tuple, list, set):
            return mask
        morphological_functions = morphological_functions or self.morphological_functions
        return reduce(filter_applier, morphological_functions, mask)

    def send(self, data, *args, **kwargs) -> Any:
        """
        Sends data to the destination using self.connection

        :param data: The data to send to the Connection
        :param args: any other arguments for the send function in your connection
        :param kwargs: any other named arguments for the connection object
        :return: Depends on the connection object used, returns its result

        """
        return self.connection.send(*args, **kwargs, data=data)

    def send_to_location(self, data, network_location: NetworkLocation, *args, **kwargs):
        """

        :param data: the data to be sent
        :param network_location: information used to send the data to a specific 'location'
         in the network
        :return: Depends on the connection object

        """
        return self.connection.send_to_location(data, network_location, *args, **kwargs)

    def get_image(self) -> np.ndarray:
        """
        Gets an image from self.camera and applies image filters
        :return: the image, false if failed to get it

        """
        if self.camera is None:
            raise CameraError("No camera given, (Camera is None)")
        if not self.camera.isOpened():
            raise CameraError("Camera given is not open (Has it been closed or disconnected?)")
        output = self.camera.read()
        if len(output) == 2:
            ret, image = output
            return image if ret else False
        else:
            return output

    def get_filtered_image(self):
        """
        Gets an image from self.camera and applies all image filters
        :return: the image filter applied image

        """
        output = self.get_image()
        return self.apply_image_filters(output)

    def apply_filter(self, filter_function, contours, verbose=False):
        """
        Applies a filter function on the contour list, this is used to remove contours
        that do not match desired features

        NOTE: Vision.detect is mainly used for full object detection and filtering,
        refer to it for common use of Vision

        :param filter_function: Filter functions are function with a contour list variable that apply some
            sort of filter on the contours, thus removing ones that don't fit the limit given by the filter.
            for example: straight_rectangle_filter removes contours that are not rectangles that are parallel
            to the frame of the picture
        :param contours: the contours on which the filter should be applied (list of numpy.ndarrays)
        :param verbose: if true_shape does not print anything
        :return: returns the output of the filter function.

        """
        if verbose:
            print('Before "{}": {}'.format(filter_function.__name__, len(contours)))
        filter_function_output = filter_function(contours)

        if isinstance(filter_function_output, tuple):
            if len(filter_function_output) == 2:
                filtered_contours, ratio = filter_function_output[0], filter_function_output[1]
            else:
                raise InvalidCustomFunctionError('Filter function must return between 1 and 2 lists.'
                                                 'Please refer to the Documentation: '
                                                 'https://github.com/1937Elysium/Ovl-Python')
        elif isinstance(filter_function_output, list):
            filtered_contours, ratio = filter_function_output, []
        else:
            raise TypeError('The contour list must be a list or tuple of 2 lists (contours and ratios)')
        return filtered_contours, ratio

    def apply_all_filters(self, contours: List[np.ndarray], verbose=False
                          ) -> Tuple[List[np.ndarray], List[float]]:
        """
        Applies all of the filters on a list of contours
        :param contours: List of contours (numpy arrays) to
        :param verbose: prints out information about filtering process if true (useful for debugging)
        :return: a list of all of the ratios given by the filter function in order.

        """
        ratios = []
        for filter_func in self.contour_filters:
            contours, ratio = self.apply_filter(filter_func, contours, verbose=verbose)
            ratios.append(ratio)
        if verbose:
            print("After all filters: {}".format(len(contours)))
        return contours, ratios

    def apply_image_filters(self, image: np.ndarray) -> np.ndarray:
        """
        Applies all given image filters to the given image
        This is used to apply various image filters on your image in a pipeline,
        like blurs, image cropping, contrasting, sharpening, rotations, translations etc.
        :param image: the image that the image filters should be applied on (numpy array)
        :return: the image with the filters applied

        """
        return reduce(filter_applier, self.image_filters, image)

    def apply_threshold(self, image: np.ndarray, threshold=None) -> np.ndarray:
        """
        Gets a mask (binary image) for a given image and Threshold object
        (uses self.Threshold if given threshold was none)
        :param image: the numpy array of the image
        :param threshold: the Threshold object used to create the binary mask
        :return: the binary mask

        """
        threshold = threshold or self.threshold
        return threshold.convert(image)

    def find_contours_in_mask(self, mask: np.ndarray, return_hierarchy=False, apply_morphs=True
                              ) -> List[np.ndarray]:
        """
        Gets contours from the given mask and apply

        :param mask: binary image (mask), a numpy array
        :param return_hierarchy: if the hierarchy should be returned
        :param apply_morphs: if the morphological functions should be applied.
        :return: the list of contours

        """
        mask = self.apply_morphological_functions(mask) if apply_morphs else mask
        result = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        if len(result) == 3:
            _, contours, hierarchy = result
        elif len(result) == 2:
            contours, hierarchy = result
        else:
            raise ValueError("Invalid output from cv2.findContours, check that your cv2 (OpenCV) version is supported")
        return (contours, hierarchy) if return_hierarchy else contours

    def find_contours(self, image: np.ndarray, threshold=None, return_hierarchy=False) -> List[np.ndarray]:
        """
        Gets a list of all the contours within the threshold that was given

        :param threshold: the threshold that determines the binary masks
        :param image: image from which to get the contours
        :param return_hierarchy: if the hierarchy should be returned
        :return: list of all contours matching the range of hsv colours

        """
        threshold = threshold or self.threshold
        image_mask = self.apply_threshold(image, threshold=threshold)
        return self.find_contours_in_mask(image_mask, return_hierarchy=return_hierarchy)

    def get_directions(self, contours: List[np.ndarray], image: np.ndarray, sorter=None):
        """
        Calculates the directions, based on contours found in the given image

        :param contours: final contours after filtering
        :param image: the image from which to find the contours
        :param sorter: optional parameter, applies a sorter on the given contours
        :return: a string of the director (output of the director function),
                 length depends on the director function

        """
        return self.director.direct(contours, image, camera_settings=self.camera_settings, sorter=sorter)

    def camera_setup(self, source=0, image_width=None, image_height=None, ovl_camera=False):
        """
        Opens up the camera reference and sets a given width and height to all images taken

        :param image_width: the width of the images to be taken, 0 does not set a width
        :param image_height: the height of the images to be taken, 0 does not set a height
        :param source: the location from which to open the camera
         string for network connections int for local USB connections.
        :param ovl_camera: if the camera object should be ovl.Camera
        :return: the camera object, also sets self.camera to the object.

        """

        image_height = image_height or self.height
        image_width = image_width or self.width
        self.camera_port = source
        if ovl_camera:
            camera = Camera(source=source, image_width=image_width, image_height=image_height)
        else:
            camera = cv2.VideoCapture(source)
            if image_width != -1:
                camera.set(3, image_width)
            if image_height != -1:
                camera.set(4, image_height)

        if not camera.isOpened():
            raise CameraError("Camera did not open correctly! Camera source: {}".format(self.camera_port))
        self.camera = camera
        return camera

    def apply_on_sample(self, image: Union[np.ndarray, str], threshold: Threshold = None, display_result: bool = False,
                        delay=0, result_color: Color = built_in_colors.RED_HIGH_HSV, return_hierarchy=False
                        ) -> Tuple[List[np.ndarray], np.ndarray]:
        """
        Finds contours and applies filters on a single image given by a image path or image object (numpy array)

        :param image: an image on which to apply the vision object
        :param threshold: a custom threshold can be passed
        :param display_result: if the contours found should be displayed
        :param delay: waitKey delay (0 if not in a loop, non-zero if it is)
        :param result_color: the colour of the contour outline
        :param return_hierarchy: if the contour hierarchies should be return
        :return: the image (numpy array) and the contours found (list of numpy arrays)
                 Can also return the hierarchies if return_hierarchy is true

        """
        threshold = threshold or self.threshold
        image = cv2.imread(image) if type(image) == str else image
        contours, hierarchy = self.find_contours(image, threshold, return_hierarchy)
        if display_result:
            image_for_display = copy.copy(image)
            cv2.drawContours(image_for_display, contours, -1, result_color)
            cv2.imshow(str(time.time()), image_for_display)
            cv2.waitKey(delay)
        return (contours, image, hierarchy) if return_hierarchy else (contours, image)

    def detect(self, image, verbose=False, return_ratios=False):
        """
        Gets contours and applies all filters and returns the result, thus detecting the object according
        to the specifications in the vision
        :param image: image in which the vision should detect an object
        :param verbose: If information about the filtering should be printed
        :param return_ratios: if the ratios from the filters should be returned
        :return: contours and the filtered image and the ratios if return_ratios is true

        """
        image = self.apply_image_filters(image)
        contours = self.find_contours(image)
        contours, ratios = self.apply_all_filters(contours, verbose)
        return (contours, image, ratios) if return_ratios else (contours, image)

max_stars_repo_name: ofekashery/Ovl-Python

max_issues_repo_name: ofekashery/Ovl-Python

max_forks_repo_name: ofekashery/Ovl-Python


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

import numpy as np
import numpy.random as random

def block_bootstrap(data, B, w):
    '''
    Implements circular block bootstrap for stationary, dependent series

    INPUTS:
        data = T x 1 vector of data to be bootstrapped
        B    = number of bootstraps
        W    = block length

    OUTPUTS:
        bsdata  = T x B matrix of bootstrapped data
        indices = T x B matrix of locations of the original BSDATA = data[indexes]

    COMMENTS:
        To generate bootstrap sequences for other uses, such as bootstrapping vector
        processes, set data to (1:N)^T

    -Translated from Kevin Sheppard MFE MATLAB toolbox.
    '''
    # ====================================================================== #
    # Input Checking
    t,k = data.shape
    if k > 1:
        raise ValueError('DATA must be a column vector')

    if t < 2:
        raise ValueError('DATA must have at least 2 observations')

    if not np.isscalar(w) or w < 1 or np.floor(w) != w or w > t:
        raise ValueError('W must be a positive scalar integer smaller than T')

    if not np.isscalar(B) or B < 1 or np.floor(B) != B:
        raise ValueError('B must be a positive scalar integer')
    # ====================================================================== #

    # Compute the number of blocks needed
    s = int(np.ceil(t / w))

    # Generate the starting points
    Bs = np.floor(random.rand(s,B)*t) + 1
    indices = np.zeros((s*w, B))
    index = 0

    # Adder is a variable that needs to be added each loop
    adder = np.kron(np.ones((1,B)), np.arange(0,w,1).reshape((w,1)))
    for i in np.arange(0,t+1,w):
        indices[i:(i+w),:] = np.kron(np.ones((w,1)),Bs[index,:].reshape((1,B))) + adder
        index += 1

    indices = indices[:t,:]
    indices[indices >= t] = (indices[indices >= t] - t)
    indices = indices.astype(int)

    bsdata = data[indices].squeeze()

    return bsdata, indices

max_stars_repo_name: vsellemi/macroeconomic-forecasting

max_issues_repo_name: vsellemi/macroeconomic-forecasting

max_forks_repo_name: vsellemi/macroeconomic-forecasting


text:
"""
A pytest module to test exception raising for invalid Galois field array arithmetic.
"""
import pytest
import numpy as np

import galois

from ..helper import randint

def test_add_int_scalar(field):
    x = field.Random(10)
    y = int(randint(0, field.order, 1, field.dtypes[-1]))
    with pytest.raises(TypeError):
        z = x + y
    with pytest.raises(TypeError):
        z = y + x

def test_add_int_array(field):
    x = field.Random(10)
    y = randint(0, field.order, 10, field.dtypes[-1])
    with pytest.raises(TypeError):
        z = x + y
    with pytest.raises(TypeError):
        z = y + x

def test_right_add_int_scalar(field):
    x = field.Random(10)
    y = int(randint(0, field.order, 1, field.dtypes[-1]))
    with pytest.raises(TypeError):
        x += y
    with pytest.raises(TypeError):
        y += x

def test_right_add_int_array(field):
    x = field.Random(10)
    y = randint(0, field.order, 10, field.dtypes[-1])
    with pytest.raises(TypeError):
        x += y
    with pytest.raises(TypeError):
        y += x

def test_subtract_int_scalar(field):
    x = field.Random(10)
    y = int(randint(0, field.order, 1, field.dtypes[-1]))
    with pytest.raises(TypeError):
        z = x - y
    with pytest.raises(TypeError):
        z = y - x

def test_subtract_int_array(field):
    x = field.Random(10)
    y = randint(0, field.order, 10, field.dtypes[-1])
    with pytest.raises(TypeError):
        z = x - y
    with pytest.raises(TypeError):
        z = y - x

def test_right_subtract_int_scalar(field):
    x = field.Random(10)
    y = int(randint(0, field.order, 1, field.dtypes[-1]))
    with pytest.raises(TypeError):
        x -= y
    with pytest.raises(TypeError):
        y -= x

def test_right_subtract_int_array(field):
    x = field.Random(10)
    y = randint(0, field.order, 10, field.dtypes[-1])
    with pytest.raises(TypeError):
        x -= y
    with pytest.raises(TypeError):
        y -= x

# NOTE: Don't test multiply with integer because that is a valid operation, namely "multiple addition"

def test_divide_int_scalar(field):
    x = field.Random(10, low=1)
    y = int(randint(1, field.order, 1, field.dtypes[-1]))
    with pytest.raises(TypeError):
        z = x / y
    with pytest.raises(TypeError):
        z = x // y
    with pytest.raises(TypeError):
        z = y / x
    with pytest.raises(TypeError):
        z = y // x

def test_divide_int_array(field):
    x = field.Random(10, low=1)
    y = randint(1, field.order, 10, field.dtypes[-1])
    with pytest.raises(TypeError):
        z = x / y
    with pytest.raises(TypeError):
        z = x // y
    with pytest.raises(TypeError):
        z = y / x
    with pytest.raises(TypeError):
        z = y // x

def test_right_divide_int_scalar(field):
    x = field.Random(10)
    y = int(randint(1, field.order, 1, field.dtypes[-1]))
    with pytest.raises(TypeError):
        x /= y
    with pytest.raises(TypeError):
        x //= y
    with pytest.raises(TypeError):
        y /= x
    with pytest.raises(TypeError):
        y //= x

def test_right_divide_int_array(field):
    x = field.Random(10)
    y = randint(1, field.order, 10, field.dtypes[-1])
    with pytest.raises(TypeError):
        x /= y
    with pytest.raises(TypeError):
        x //= y
    with pytest.raises(TypeError):
        y /= x
    with pytest.raises(TypeError):
        y //= x

def test_divide_by_zero(field):
    x = field.Random(10)
    with pytest.raises(ZeroDivisionError):
        y = field(0)
        z = x / y
    with pytest.raises(ZeroDivisionError):
        y = field.Random(10)
        y[0] = 0  # Ensure one value is zero
        z = x / y

def test_multiplicative_inverse_of_zero(field):
    x = field.Random(10)
    x[0] = 0  # Ensure one value is zero
    with pytest.raises(ZeroDivisionError):
        z = x ** -1

# NOTE: Don't test power to integer because that's valid

def test_zero_to_negative_power(field):
    x = field.Random(10)
    x[0] = 0  # Ensure one value is zero
    with pytest.raises(ZeroDivisionError):
        y = -3
        z = x ** y
    with pytest.raises(ZeroDivisionError):
        y = -3*np.ones(x.size, field.dtypes[-1])
        z = x ** y

def test_log_of_zero(field):
    with pytest.raises(ArithmeticError):
        x = field(0)
        z = np.log(x)
    with pytest.raises(ArithmeticError):
        x = field.Random(10)
        x[0] = 0  # Ensure one value is zero
        z = np.log(x)

max_stars_repo_name: BK-Modding/galois

max_issues_repo_name: BK-Modding/galois

max_forks_repo_name: BK-Modding/galois


text:
import torch
import numpy as np
import time
from torch import nn
from tqdm import tqdm
from glob import glob
from sklearn.feature_extraction import image
from matplotlib import pyplot as plt

class Interpolate(nn.Module):
    """Wrapper for torch.nn.functional.interpolate."""

    def __init__(self,
                 size=None,
                 scale=None,
                 mode='bilinear',
                 align_corners=False):
        super().__init__()
        assert (size is None) == (scale is not None)
        self.size = size
        self.scale = scale
        self.mode = mode
        self.align_corners = align_corners

    def forward(self, x):
        out = F.interpolate(x,
                            size=self.size,
                            scale_factor=self.scale,
                            mode=self.mode,
                            align_corners=self.align_corners)
        return out

class CropImage(nn.Module):
    """Crops image to given size.
    Args:
        size
    """

    def __init__(self, size):
        super().__init__()
        self.size = size

    def forward(self, x):
        return crop_img_tensor(x, self.size)

def normalize(img, mean, std):
    """Normalize an array of images with mean and standard deviation.
        Parameters
        ----------
        img: array
            An array of images.
        mean: float
            Mean of img array.
        std: float
            Standard deviation of img array.
        """
    return (img - mean)/std

def denormalize(img, mean, std):
    """Denormalize an array of images with mean and standard deviation.
    Parameters
    ----------
    img: array
        An array of images.
    mean: float
        Mean of img array.
    std: float
        Standard deviation of img array.
    """
    return (img * std) + mean

def convertToFloat32(train_images,val_images):
    """Converts the data to float 32 bit type.
    Parameters
    ----------
    train_images: array
        Training data.
    val_images: array
        Validation data.
        """
    x_train = train_images.astype('float32')
    x_val = val_images.astype('float32')
    return x_train, x_val

def getMeanStdData(train_images,val_images):
    """Compute mean and standrad deviation of data.
    Parameters
    ----------
    train_images: array
        Training data.
    val_images: array
        Validation data.
    """
    x_train_ = train_images.astype('float32')
    x_val_ = val_images.astype('float32')
    data = np.concatenate((x_train_,x_val_), axis=0)
    mean, std = np.mean(data), np.std(data)
    return mean, std

def convertNumpyToTensor(numpy_array):
    """Convert numpy array to PyTorch tensor.
    Parameters
    ----------
    numpy_array: numpy array
        Numpy array.
    """
    return torch.from_numpy(numpy_array)

def augment_data(X_train):
    """Augment data by 8-fold with 90 degree rotations and flips.
    Parameters
    ----------
    X_train: numpy array
        Array of training images.
    """
    X_ = X_train.copy()

    X_train_aug = np.concatenate((X_train, np.rot90(X_, 1, (1, 2))))
    X_train_aug = np.concatenate((X_train_aug, np.rot90(X_, 2, (1, 2))))
    X_train_aug = np.concatenate((X_train_aug, np.rot90(X_, 3, (1, 2))))
    X_train_aug = np.concatenate((X_train_aug, np.flip(X_train_aug, axis=1)))

    print('Raw image size after augmentation', X_train_aug.shape)
    return X_train_aug

def extract_patches(x,patch_size,num_patches):
    """Deterministically extract patches from array of images.
    Parameters
    ----------
    x: numpy array
        Array of images.
    patch_size: int
        Size of patches to be extracted from each image.
    num_patches: int
        Number of patches to be extracted from each image.
    """
    patches = np.zeros(shape=(x.shape[0]*num_patches,patch_size,patch_size))

    for i in tqdm(range(x.shape[0])):
        patches[i*num_patches:(i+1)*num_patches] = image.extract_patches_2d(x[i],(patch_size,patch_size), num_patches,
                                                                           random_state=i)
    return patches

def crop_img_tensor(x, size) -> torch.Tensor:
    """Crops a tensor.
    Crops a tensor of shape (batch, channels, h, w) to new height and width
    given by a tuple.
    Args:
        x (torch.Tensor): Input image
        size (list or tuple): Desired size (height, width)
    Returns:
        The cropped tensor
    """
    return _pad_crop_img(x, size, 'crop')

def _pad_crop_img(x, size, mode) -> torch.Tensor:
    """ Pads or crops a tensor.
    Pads or crops a tensor of shape (batch, channels, h, w) to new height
    and width given by a tuple.
    Args:
        x (torch.Tensor): Input image
        size (list or tuple): Desired size (height, width)
        mode (str): Mode, either 'pad' or 'crop'
    Returns:
        The padded or cropped tensor
    """

    assert x.dim() == 4 and len(size) == 2
    size = tuple(size)
    x_size = x.size()[2:4]
    if mode == 'pad':
        cond = x_size[0] > size[0] or x_size[1] > size[1]
    elif mode == 'crop':
        cond = x_size[0] < size[0] or x_size[1] < size[1]
    else:
        raise ValueError("invalid mode '{}'".format(mode))
    if cond:
        raise ValueError('trying to {} from size {} to size {}'.format(
            mode, x_size, size))
    dr, dc = (abs(x_size[0] - size[0]), abs(x_size[1] - size[1]))
    dr1, dr2 = dr // 2, dr - (dr // 2)
    dc1, dc2 = dc // 2, dc - (dc // 2)
    if mode == 'pad':
        return nn.functional.pad(x, [dc1, dc2, dr1, dr2, 0, 0, 0, 0])
    elif mode == 'crop':
        return x[:, :, dr1:x_size[0] - dr2, dc1:x_size[1] - dc2]

def free_bits_kl(kl,
                 free_bits,
                 batch_average = False,
                 eps = 1e-6) -> torch.Tensor:
    """Computes free-bits version of KL divergence.
    Takes in the KL with shape (batch size, layers), returns the KL with
    free bits (for optimization) with shape (layers,), which is the average
    free-bits KL per layer in the current batch.
    If batch_average is False (default), the free bits are per layer and
    per batch element. Otherwise, the free bits are still per layer, but
    are assigned on average to the whole batch. In both cases, the batch
    average is returned, so it's simply a matter of doing mean(clamp(KL))
    or clamp(mean(KL)).
    Args:
        kl (torch.Tensor)
        free_bits (float)
        batch_average (bool, optional))
        eps (float, optional)
    Returns:
        The KL with free bits
    """

    assert kl.dim() == 2
    if free_bits < eps:
        return kl.mean(0)
    if batch_average:
        return kl.mean(0).clamp(min=free_bits)
    return kl.clamp(min=free_bits).mean(0)

def pad_img_tensor(x, size) -> torch.Tensor:
    """Pads a tensor.
    Pads a tensor of shape (batch, channels, h, w) to new height and width
    given by a tuple.
    Args:
        x (torch.Tensor): Input image
        size (list or tuple): Desired size (height, width)
    Returns:
        The padded tensor
    """

    return _pad_crop_img(x, size, 'pad')

def plotProbabilityDistribution(signalBinIndex, histogram, gaussianMixtureNoiseModel, min_signal, max_signal, n_bin, device):
    """Plots probability distribution P(x|s) for a certain ground truth signal.
       Predictions from both Histogram and GMM-based Noise models are displayed for comparison.
        Parameters
        ----------
        signalBinIndex: int
            index of signal bin. Values go from 0 to number of bins (`n_bin`).
        histogram: numpy array
            A square numpy array of size `nbin` times `n_bin`.
        gaussianMixtureNoiseModel: GaussianMixtureNoiseModel
            Object containing trained parameters.
        min_signal: float
            Lowest pixel intensity present in the actual sample which needs to be denoised.
        max_signal: float
            Highest pixel intensity present in the actual sample which needs to be denoised.
        n_bin: int
            Number of Bins.
        device: GPU device
        """
    histBinSize=(max_signal-min_signal)/n_bin
    querySignal_numpy= (signalBinIndex/float(n_bin)*(max_signal-min_signal)+min_signal)
    querySignal_numpy +=histBinSize/2
    querySignal_torch = torch.from_numpy(np.array(querySignal_numpy)).float().to(device)

    queryObservations_numpy=np.arange(min_signal, max_signal, histBinSize)
    queryObservations_numpy+=histBinSize/2
    queryObservations = torch.from_numpy(queryObservations_numpy).float().to(device)
    pTorch=gaussianMixtureNoiseModel.likelihood(queryObservations, querySignal_torch)
    pNumpy=pTorch.cpu().detach().numpy()

    plt.figure(figsize=(12, 5))

    plt.subplot(1, 2, 1)
    plt.xlabel('Observation Bin')
    plt.ylabel('Signal Bin')
    plt.imshow(histogram**0.25, cmap='gray')
    plt.axhline(y=signalBinIndex+0.5, linewidth=5, color='blue', alpha=0.5)

    plt.subplot(1, 2, 2)
    plt.plot(queryObservations_numpy, histogram[signalBinIndex, :]/histBinSize, label='GT Hist: bin ='+str(signalBinIndex), color='blue', linewidth=2)
    plt.plot(queryObservations_numpy, pNumpy, label='GMM : '+' signal = '+str(np.round(querySignal_numpy,2)), color='red',linewidth=2)
    plt.xlabel('Observations (x) for signal s = ' + str(querySignal_numpy))
    plt.ylabel('Probability Density')
    plt.title("Probability Distribution P(x|s) at signal =" + str(querySignal_numpy))
    plt.legend()

def PSNR(gt, img, psnrRange):
    '''
    Compute PSNR.
    Parameters
    ----------
    gt: array
        Ground truth image.
    img: array
        Predicted image.
    psnrRange: float
        Range PSNR
    '''
    mse = np.mean(np.square(gt - img))
    return 20 * np.log10(psnrRange) - 10 * np.log10(mse)

def get_normalized_tensor(img,model,device):
    '''
    Normalizes tensor with mean and std.
    Parameters
    ----------
    img: array
        Image.
    model: Hierarchical DivNoising model
    device: GPU device.
    '''
    test_images = torch.from_numpy(img).to(device)
    data_mean = model.data_mean
    data_std = model.data_std
    test_images = (test_images-data_mean)/data_std
    return test_images

max_stars_repo_name: juglab/HDN

max_issues_repo_name: juglab/HDN

max_forks_repo_name: juglab/HDN


text:
# -*- coding: utf-8 -*-
"""
Created on 8/31/2020

This is a simple battery value federate that models the physics of an EV
battery as it is being charged. The federate receives a voltage signal
representing the votlage applied to the charging terminals of the battery
and based on its internally modeled SOC, calculates the current draw of
the battery and sends it back to the EV federate. Note that this SOC should
be considered the true SOC of the battery which may be different than the
SOC modeled by the charger

@author: Trevor Hardy
trevor.hardy@pnnl.gov
"""

import helics as h
import random
import string
import time
from datetime import datetime, timedelta
import json
import logging
import numpy as np
import sys
import matplotlib.pyplot as plt

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

# Define battery physics as empirical values
socs = np.array([0, 1])
effective_R = np.array([10, 150])

def destroy_federate(fed):
    '''
    As part of ending a HELICS co-simulation it is good housekeeping to
    formally destroy a federate. Doing so informs the rest of the
    federation that it is no longer a part of the co-simulation and they
    should proceed without it (if applicable). Generally this is done
    when the co-simulation is complete and all federates end execution
    at more or less the same wall-clock time.

    :param fed: Federate to be destroyed
    :return: (none)
    '''
    status = h.helicsFederateFinalize(fed)
    h.helicsFederateFree(fed)
    h.helicsCloseLibrary()
    print("EV: Federate finalized")

if __name__ == "__main__":
    np.random.seed(1)

    ##############  Registering  federate from json  ##########################
    name = "Battery_federate"
    fed = h.helicsCreateValueFederateFromConfig("BatteryConfig.json")
    federate_name = h.helicsFederateGetName(fed)
    logging.info(f'Created federate {federate_name}')

    sub_count = h.helicsFederateGetInputCount(fed)
    logging.info(f'\tNumber of subscriptions: {sub_count}')
    pub_count = h.helicsFederateGetPublicationCount(fed)
    logging.info(f'\tNumber of publications: {pub_count}')
    print(f'\tNumber of subscriptions: {sub_count}')
    print(f'\tNumber of publications: {pub_count}')

    # Diagnostics to confirm JSON config correctly added the required
    #   publications and subscriptions
    subid = {}
    sub_name = {}
    for i in range(0, sub_count):
        subid[i] = h.helicsFederateGetInputByIndex(fed, i)
        sub_name[i] = h.helicsSubscriptionGetKey(subid[i])
        logger.info(f'\tRegistered subscription---> {sub_name[i]}')

    pubid = {}
    pub_name = {}
    for i in range(0, pub_count):
        pubid[i] = h.helicsFederateGetPublicationByIndex(fed, i)
        pub_name[i] = h.helicsPublicationGetKey(pubid[i])
        logger.info(f'\tRegistered publication---> {pub_name[i]}')

    ##############  Entering Execution Mode  ##################################
    h.helicsFederateEnterExecutingMode(fed)
    logger.info('Entered HELICS execution mode')

    hours = 24*7 # one week
    total_interval = int(60 * 60 * hours)
    update_interval = 60 # seconds
    grantedtime = -1

    batt_size = 62  # kWh
    batt_cell_series = 96
    batt_cell_parallel = 3

    current_soc = {}
    for i in range (0, pub_count):
        current_soc[i] = (np.random.randint(0,80))/100

    # Data collection lists
    time_sim = []
    current = []

    t = 0

    # As long as granted time is in the time range to be simulated...
    while t < total_interval:

        # Time request for the next physical interval to be simulated
        requested_time = (t+update_interval)
        logger.debug(f'Requesting time {requested_time}')
        grantedtime = h.helicsFederateRequestTime (fed, requested_time)
        logger.debug(f'Granted time {grantedtime}')

        t = grantedtime

        for j in range(0,sub_count):
            logger.debug(f'Battery {j+1} time {t}')

            # Get the applied charging voltage from the EV
            charging_voltage = h.helicsInputGetDouble((subid[j]))
            logger.debug(f'\tReceived voltage {charging_voltage:.2f} from input'
                         f' {h.helicsSubscriptionGetKey(subid[j])}')
            # EV is fully charged and a new EV is moving in:
            if charging_voltage == 0:
                current_soc[j] = (np.random.randint(0,80))/100

            # Calculate charging current and update SOC
            R = (batt_cell_series / batt_cell_parallel) * np.interp(
                    current_soc[j],
                    socs,
                    effective_R)
            logger.debug(f'\t Effective R (ohms): {R:.2f}')
            charging_current = charging_voltage / R
            logger.debug(f'\t Charging current (A): {charging_current:.2f}')
            added_energy = (charging_current * charging_voltage * \
                           update_interval/3600) / 1000
            logger.debug(f'\t Added energy (kWh): {added_energy:.2f}')
            current_soc[j] = current_soc[j] + added_energy / batt_size
            logger.debug(f'\t SOC: {current_soc[j]:.4f}')

            # Publish out charging current
            h.helicsPublicationPublishDouble(pubid[j], charging_current)
            logger.debug(f'\tPublished {pub_name[j]} with value '
                         f'{charging_current:.2f}')

        # Data collection vectors
        time_sim.append(t)
        current.append(charging_current)

    # Cleaning up HELICS stuff once we've finished the co-simulation.
    destroy_federate(fed)

max_stars_repo_name: activeshadow/HELICS-Examples

max_issues_repo_name: activeshadow/HELICS-Examples

max_forks_repo_name: activeshadow/HELICS-Examples


text:
#imports
import spacy
import base64
import time
import collections
import re
import itertools
import nltk
import gensim
import kaleido
import plotly
import pandas as pd
import numpy as np
from spacy.lang.en import English
import matplotlib.pyplot as plt
from nltk.corpus import stopwords
nltk.download('stopwords')
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk.stem import WordNetLemmatizer
nltk.download('wordnet')
from kaleido.scopes.plotly import PlotlyScope
from gensim.models import Word2Vec
from gensim.models import KeyedVectors
from sklearn.manifold import TSNE
import plotly.graph_objects as go
from sklearn.decomposition import PCA
from scipy import sparse
from soyclustering import SphericalKMeans
from soyclustering import proportion_keywords
from sklearn.cluster import KMeans
from sklearn import metrics

ndocs = 1000

#datasets and log
asrs = pd.read_csv('./ASRS_data.csv', sep="|",nrows = ndocs)
abr1 = pd.read_csv('./ASRS_Abr.csv')
abr2 = pd.read_csv('./ASRS_Abbreviations.csv')
f= open("logs.html","w")
a = '
ASRS Data
'
f.write(a)

#Removing some abreviations

#Preparation
abr1 = abr1[['Abr','word']]
abr1['Abr'] = abr1['Abr'].apply(lambda x : x.lower())
abr1['word'] = abr1['word'].apply(lambda x : x.lower())
trans_abr = abr1.set_index("Abr").T
abrd = trans_abr.to_dict('list')
asrs['Narrative'] = asrs['Narrative'].apply(lambda x : x.lower())
asrs["Narrative"] = asrs['Narrative'].str.replace('[^\w\s\d]','')
asrs["Narrative"] = asrs['Narrative'].str.replace(r'\d+','')

#function
def replace_abr(narr):
    # find all states that exist in the string
    abr_found = filter(lambda abr: abr in narr, abrd.keys())

    # replace each state with its abbreviation
    for abr in abr_found:
        narr = narr.replace(' '+abr+' ', ' '+abrd[abr][0]+' ')
    # return the modified string (or original if no states were found)
    return narr

#Replacement
asrs['Narrative'] = asrs['Narrative'].apply(replace_abr)

#Stopwords, lemmatization, tokenization
lemmatizer = WordNetLemmatizer()
stop_words = set(stopwords.words('english'))
clean = lambda new : " ".join([lemmatizer.lemmatize(i) for i in re.sub("[^a-zA-Z]", " " ,
	new.lower()).split() if i not in stop_words]).split()
start_time = time.time()
asrs['cleaned']=asrs['Narrative'].apply(clean)
f.write('Time to preprocess : %s s
' % (time.time() - start_time))

#1st Vocab size
a = asrs['cleaned'].tolist()
b = list(set(list(itertools.chain.from_iterable(a))))
f.write('Initial vocabulary size : %s s
' % len(b))

freq_min = 10

#freq discrimination
#apply(low) takes a lot of time
biglist = list(itertools.chain.from_iterable(asrs['cleaned']))
count = collections.Counter(biglist).most_common()
def lowfreq(count):
    i = 0
    for word, num in reversed(count):
        i+=1
        if num > freq_min:
            return i
ind = lowfreq(count)
to_remove = list(dict(count[-ind:]).keys())
low = lambda x : list(filter(lambda m : m not in to_remove,x))
start_time = time.time()
asrs['cleaned'] = asrs['cleaned'].apply(low)
f.write('Low frequency discrimination time : %s
' % (time.time() - start_time))

#2nd Vocab size
a = asrs['cleaned'].tolist()
b = list(set(list(itertools.chain.from_iterable(a))))
f.write('Initial vocabulary size : %s
' % len(b))

idfmin = 5.4

#IDF discrimination
#apply(low) also takes a lot of time
vectorizer = TfidfVectorizer(analyzer = lambda x : [w for w in x if w not in stop_words])
vectorized = vectorizer.fit(asrs["cleaned"])
idf = vectorized.idf_
idex = np.argwhere(idf>idfmin)
idex = [idx[0] for idx in idex]
vocab = vectorized.get_feature_names()
to_remove2 = {vocab[i] for i in idex}
low = lambda x : list(filter(lambda m : m not in to_remove2,x))
start_time = time.time()
asrs['cleaned'] = asrs['cleaned'].apply(low)
f.write('idf discrimination time : %s s
' % (time.time() - start_time))

#3rd Vocab size
a = asrs['cleaned'].tolist()
b = list(set(list(itertools.chain.from_iterable(a))))
f.write('Initial vocabulary size : %s
' % len(b))

#Word2vec and normalization
wlist = asrs['cleaned'].tolist()
model = Word2Vec(wlist, min_count=1,size= 50,workers=3, window =3, sg = 1)
model.init_sims(replace=True)
key_vec = model.wv
word_vec = key_vec.vectors
dic = key_vec.index2word

n_neigh = 15

#TSNE Plots
def plot_TSNE_local(keyed_vec, word, n_words):
    arr = []
    labels = []

    close_words = key_vec.similar_by_word(word,topn = n_words)

    arr.append(np.array(keyed_vec[word]))
    labels.append(word)

    for wrd in close_words:
        arr.append(np.array(keyed_vec[wrd[0]]))
        labels.append(wrd[0])

    tsne = TSNE(n_components = 2)
    Y = tsne.fit_transform(arr)

    x_coords = Y[:, 0]
    y_coords = Y[:, 1]

    fig = go.Figure()
    fig.add_trace(go.Scatter(
                    x = x_coords,
                    y = y_coords,
                    mode = 'markers + text',
                    text = labels,
                    textposition="bottom center"
                            ))

    title = str(n_words) + ' Closest words to the word ' + word + ' :'
    fig.update_layout(
            title = title,
            autosize=False,
            width=700,
            height=700)

    #fig.show()
    #fig.write_image("fig1.png")
    return fig
scope = PlotlyScope()

with open("twr.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'twr',n_neigh), format="png"))

data_uri = base64.b64encode(open('twr.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

with open("smoke.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'smoke',n_neigh), format="png"))

data_uri = base64.b64encode(open('smoke.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

with open("flight.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'flight',n_neigh), format="png"))

data_uri = base64.b64encode(open('flight.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

with open("terrain.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'terrain',n_neigh), format="png"))

data_uri = base64.b64encode(open('terrain.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

with open("rain.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'rain',n_neigh), format="png"))

data_uri = base64.b64encode(open('rain.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

with open("vfr.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'vfr',n_neigh), format="png"))

data_uri = base64.b64encode(open('vfr.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

with open("passenger.png", "wb") as file:
    file.write(scope.transform(plot_TSNE_local(key_vec,'seat',n_neigh), format="png"))

data_uri = base64.b64encode(open('passenger.png', 'rb').read()).decode('utf-8')
img_tag = ''.format(data_uri)
f.write(img_tag)

Y = word_vec

n_clusters = 15

#faut normaliser

#spherical K means clustering
km = KMeans(n_clusters=n_clusters)
km.fit_transform(word_vec)
print("Top terms per cluster:")
order_centroids = km.cluster_centers_.argsort()[:, ::-1]
terms = dic
for i in range(n_clusters):
    print("Cluster %d:" % i, end='')
    for ind in order_centroids[i, :10]:
        print(' %s' % terms[ind], end='')
    print()

#faire kmeans sur les tsne, kmeans na pas de sens du tout..
#voir pk kmeans fait de la merde

#feed into LDA?

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-

"""This module contains various design methods/classes for amplifier components."""

from typing import TYPE_CHECKING, List, Tuple, Optional, Dict, Any

import numpy as np
import scipy.optimize as sciopt

from bag.util.search import BinaryIterator
from bag.math.dfun import DiffFunction

if TYPE_CHECKING:
    from verification.mos.query import MOSDBDiscrete

class LoadDiodePFB(object):
    """A differential load consists of diode transistor and negative gm cell.

    This topology is designed to have a large differential mode resistance and a
    small common mode resistance, plus a well defined output common mode

    Parameters
    ----------
    mos_db : MOSDBDiscrete
        the transistor small signal parameters database.
    """
    def __init__(self, mos_db):
        # type: (MOSDBDiscrete) -> None
        self._db = mos_db
        self._dsn_params = mos_db.dsn_params
        if 'stack' not in self._dsn_params:
            raise ValueError('This class assumes transistor stack is swept.')

        self._stack_list = sorted(mos_db.get_dsn_param_values('stack'))
        self._intent_list = mos_db.get_dsn_param_values('intent')
        self._valid_widths = mos_db.width_list
        self._best_op = None

    def design(self, itarg_list, vds2_list, ft_min, stack_list=None):
        # type: (List[float], List[float], float, Optional[List[int]]) -> None
        """Design the diode load.

        Parameters
        ----------
        itarg_list : List[float]
            target single-ended bias current across simulation environments.
        vds2_list : List[float]
            list of op-amp stage 2 vds voltage across simulation environments.
        ft_min : float
            minimum transit frequency of the composit transistor.
        stack_list : Optional[List[int]]
            list of valid stack numbers.
        """
        if stack_list is None:
            stack_list = self._stack_list

        vgs_idx = self._db.get_fun_arg_index('vgs')

        num_stack = len(stack_list)

        self._best_op = None
        best_score = None
        for intent in self._intent_list:
            for w in self._valid_widths:
                for idx1 in range(num_stack):
                    stack1 = stack_list[idx1]
                    self._db.set_dsn_params(w=w, intent=intent, stack=stack1)
                    ib1 = self._db.get_function_list('ibias')
                    gm1 = self._db.get_function_list('gm')
                    gds1 = self._db.get_function_list('gds')
                    cd1 = self._db.get_function_list('cdd')
                    vgs1_min, vgs1_max = ib1[0].get_input_range(vgs_idx)

                    for idx2 in range(idx1, num_stack):
                        stack2 = stack_list[idx2]
                        self._db.set_dsn_params(stack=stack2)
                        ib2 = self._db.get_function_list('ibias')
                        gm2 = self._db.get_function_list('gm')
                        gds2 = self._db.get_function_list('gds')
                        cd2 = self._db.get_function_list('cdd')
                        vgs2_min, vgs2_max = ib2[0].get_input_range(vgs_idx)

                        vgs_min = max(vgs1_min, vgs2_min)
                        vgs_max = min(vgs1_max, vgs2_max)

                        seg1_iter = BinaryIterator(2, None, step=2)
                        while seg1_iter.has_next():
                            seg1 = seg1_iter.get_next()

                            all_neg = True
                            one_pass = False
                            seg2_iter = BinaryIterator(0, None, step=2)
                            while seg2_iter.has_next():
                                seg2 = seg2_iter.get_next()

                                vgs_list, err_code = self._solve_vgs(itarg_list, seg1, seg2, ib1,
                                                                     ib2, vgs_min, vgs_max)
                                if err_code < 0:
                                    # too few fingers
                                    seg2_iter.up()
                                elif err_code > 0:
                                    # too many fingers
                                    seg2_iter.down()
                                else:
                                    one_pass = True
                                    cur_score = self._compute_score(ft_min, seg1, seg2, gm1, gm2,
                                                                    gds1, gds2, cd1, cd2, vgs_list)

                                    if cur_score != -1:
                                        all_neg = False

                                    if cur_score < 0:
                                        seg2_iter.down()
                                    else:
                                        seg2_iter.save()
                                        seg2_iter.up()
                                        if best_score is None or cur_score > best_score:
                                            best_score = cur_score
                                            self._best_op = (intent, stack1, stack2, w, seg1, seg2,
                                                             vgs_list, vds2_list)

                            if seg2_iter.get_last_save() is None:
                                # no solution for seg2
                                if all_neg and one_pass:
                                    # all solutions encountered have negative resistance,
                                    # this means we have insufficent number of diode fingers.
                                    seg1_iter.up()
                                elif not one_pass:
                                    # exit immediately with no solutions at all; too many fingers
                                    seg1_iter.down()
                                else:
                                    # all positive resistance solution break V*_min specs.
                                    # this means we have too many number of fingers.
                                    seg1_iter.down()
                            else:
                                seg1_iter.save()
                                seg1_iter.up()

    def _solve_vgs(self,
                   itarg_list,  # type: List[float]
                   k1,  # type: float
                   k2,  # type: float
                   ib1,  # type: List[DiffFunction]
                   ib2,  # type: List[DiffFunction]
                   vgs_min,  # type: float
                   vgs_max,  # type: float
                   ):
        # type: (...) -> Tuple[List[float], int]

        vgs_list = []
        for itarg, ifun1, ifun2 in zip(itarg_list, ib1, ib2):
            def zero_fun(vgs):
                fun_arg = self._db.get_fun_arg(vbs=0, vds=vgs, vgs=vgs)
                return ifun1(fun_arg) * k1 + ifun2(fun_arg) * k2 - itarg

            itest0 = zero_fun(vgs_min)
            itest1 = zero_fun(vgs_max)
            if itest0 < 0 and itest1 < 0:
                # too few fingers
                return [], -1
            elif itest0 > 0 and itest1 > 0:
                # too many fingers
                return [], 1
            else:
                vgs_cur = sciopt.brentq(zero_fun, vgs_min, vgs_max)
                vgs_list.append(vgs_cur)

        return vgs_list, 0

    def _compute_score(self, ft_min, scale1, scale2, gm1, gm2, gds1, gds2,
                       cd1, cd2, vgs_list):
        score = float('inf')
        for fgm1, fgm2, fgds1, fgds2, fcd1, fcd2, vgs in \
                zip(gm1, gm2, gds1, gds2, cd1, cd2, vgs_list):
            arg = self._db.get_fun_arg(vbs=0, vds=vgs, vgs=vgs)
            cur_gm1 = scale1 * fgm1(arg)
            cur_gm2 = scale2 * fgm2(arg)
            cur_gds1 = scale1 * fgds1(arg)
            cur_gds2 = scale2 * fgds2(arg)
            cur_cd = scale1 * fcd1(arg) + scale2 * fcd2(arg)
            cur_ft = (cur_gm1 + cur_gm1) / (2 * cur_cd) / 2 / np.pi
            cur_gds_tot = cur_gds1 + cur_gds2 + cur_gm1 - cur_gm2
            if cur_gm1 <= cur_gm2:
                # negative resistance
                return -1
            if cur_ft < ft_min:
                # break minimum V* spec
                return -2

            score = min(score, (cur_gm1 + cur_gm2) / cur_gds_tot)

        return score

    def get_dsn_info(self):
        # type: () -> Optional[Dict[str, Any]]
        if self._best_op is None:
            return None

        intent, stack1, stack2, w, seg1, seg2, vgs_list, vds2_list = self._best_op
        self._db.set_dsn_params(w=w, intent=intent, stack=stack1)
        ib1 = self._db.get_function_list('ibias')
        gm1 = self._db.get_function_list('gm')
        gds1 = self._db.get_function_list('gds')
        cg1 = self._db.get_function_list('cgg')
        cd1 = self._db.get_function_list('cdd')
        self._db.set_dsn_params(w=w, intent=intent, stack=stack2)
        ib2 = self._db.get_function_list('ibias')
        gm2 = self._db.get_function_list('gm')
        gds2 = self._db.get_function_list('gds')
        cg2 = self._db.get_function_list('cgg')
        cd2 = self._db.get_function_list('cdd')

        ctot1_list, gds1_list = [], []
        vstar2_list, gm2_list, gds2_list, cgg2_list, cdd2_list, ft_list = [], [], [], [], [], []
        for ib1f, gm1f, gds1f, cg1f, cd1f, ib2f, gm2f, gds2f, cg2f, cd2f, vgs, vds2 in \
                zip(ib1, gm1, gds1, cg1, cd1, ib2, gm2, gds2, cg2, cd2, vgs_list, vds2_list):
            arg1 = self._db.get_fun_arg(vbs=0, vds=vgs, vgs=vgs)
            cur_gm1 = seg1 * gm1f(arg1)
            cur_gm2 = seg2 * gm2f(arg1)
            cur_gds1 = seg1 * gds1f(arg1)
            cur_gds2 = seg2 * gds2f(arg1)
            cur_cg1 = seg1 * cg1f(arg1)
            cur_cg2 = seg2 * cg2f(arg1)
            cur_cd1 = seg1 * cd1f(arg1)
            cur_cd2 = seg2 * cd2f(arg1)
            cur_ctot = cur_cg1 + cur_cg2 + cur_cd1 + cur_cd2
            ctot1_list.append(cur_ctot)
            ft_list.append((cur_gm1 + cur_gm2) / (2 * (cur_cd1 + cur_cd2)) / 2 / np.pi)
            gds1_list.append(cur_gds1 + cur_gds2 + cur_gm1 - cur_gm2)

            arg2 = self._db.get_fun_arg(vbs=0, vds=vds2, vgs=vgs)
            cur_ib1 = seg1 * ib1f(arg2)
            cur_ib2 = seg2 * ib2f(arg2)
            cur_gm1 = seg1 * gm1f(arg2)
            cur_gm2 = seg2 * gm2f(arg2)
            cur_gds1 = seg1 * gds1f(arg2)
            cur_gds2 = seg2 * gds2f(arg2)
            cur_cg1 = seg1 * cg1f(arg2)
            cur_cg2 = seg2 * cg2f(arg2)
            cur_cd1 = seg1 * cd1f(arg2)
            cur_cd2 = seg2 * cd2f(arg2)
            vstar2_list.append(2 * (cur_ib1 + cur_ib2) / (cur_gm1 + cur_gm2))
            gm2_list.append(cur_gm1 + cur_gm2)
            gds2_list.append(cur_gds1 + cur_gds2)
            cgg2_list.append(cur_cg1 + cur_cg2)
            cdd2_list.append(cur_cd1 + cur_cd2)

        return dict(
            vgs=vgs_list,
            ft=ft_list,
            ctot1=ctot1_list,
            gds1=gds1_list,
            vstar2=vstar2_list,
            gm2=gm2_list,
            gds2=gds2_list,
            cgg2=cgg2_list,
            cdd2=cdd2_list,
            intent=intent,
            stack_diode=stack1,
            stack_ngm=stack2,
            w=w,
            seg_diode=seg1,
            seg_ngm=seg2,
        )

class InputGm(object):
    """A simple differential input gm stage.

    This class maximizes the gain given V* constraint.
    """

    def __init__(self, mos_db):
        # type: (MOSDBDiscrete) -> None
        self._db = mos_db
        self._dsn_params = mos_db.dsn_params
        if 'stack' not in self._dsn_params:
            raise ValueError('This class assumes transistor stack is swept.')

        self._stack_list = sorted(mos_db.get_dsn_param_values('stack'))
        self._intent_list = mos_db.get_dsn_param_values('intent')
        self._valid_widths = mos_db.width_list
        self._best_op = None

    def design(self,
               itarg_list,  # type: List[float]
               vg_list,  # type: List[float]
               vd_list,  # type: List[float]
               gds_load_list,  # type: List[float]
               vb,  # type: float
               vstar_min,  # type: float
               vds_tail_min,  # type: float
               seg_min=2,  # type: int
               stack_list=None,  # type: Optional[List[int]]
               ):
        # type: (...) -> None
        """Design the input gm stage.

        Parameters
        ----------
        itarg_list : List[float]
            target single-ended bias current across simulation environments.
        vg_list : List[float]
            gate voltage across simulation environments.
        vd_list : List[float]
            drain voltage across simulation environments.
        gds_load_list : List[float]
            load conductance across simulation environments.
        vb : float
            body bias voltage.
        vstar_min : float
            minimum V* of the diode.
        vds_tail_min : float
            minimum absolute vds voltage of tail device.
        seg_min : int
            minimum number of segments.
        stack_list : Optional[List[str]]
            If given, we will only consider these stack values.
        """
        vgs_idx = self._db.get_fun_arg_index('vgs')
        vds_idx = self._db.get_fun_arg_index('vds')

        if stack_list is None:
            stack_list = self._stack_list

        best_score = None
        self._best_op = None
        for intent in self._intent_list:
            for w in self._valid_widths:
                for stack in stack_list:
                    self._db.set_dsn_params(w=w, intent=intent, stack=stack)
                    ib = self._db.get_function_list('ibias')
                    gm = self._db.get_function_list('gm')
                    gds = self._db.get_function_list('gds')

                    # get valid vs range across simulation environments.
                    vgs_min, vgs_max = ib[0].get_input_range(vgs_idx)
                    vds_min, vds_max = ib[0].get_input_range(vds_idx)
                    vs_bnds = [(max(vg - vgs_max, vd - vds_max), min(vg - vgs_min, vd - vds_min))
                               for vg, vd in zip(vg_list, vd_list)]

                    iunit_list = self._solve_iunit_from_vstar(vstar_min, vb, vg_list, vd_list,
                                                              vs_bnds, ib, gm)
                    if iunit_list is not None:
                        tot_seg = min((itarg / iunit for itarg, iunit in
                                       zip(itarg_list, iunit_list)))
                        # now get actual numbers
                        num_seg = max(seg_min, int(tot_seg // 2) * 2)
                        vs_list, score = self._solve_vs(itarg_list, vg_list, vd_list, vs_bnds, vb,
                                                        num_seg, ib, gm, gds, gds_load_list,
                                                        vds_tail_min, vstar_min)
                        if score is not None and (best_score is None or score > best_score):
                            best_score = score
                            self._best_op = (intent, stack, w, num_seg, vg_list, vd_list,
                                             vs_list, vb)

    def _solve_vs(self, itarg_list, vg_list, vd_list, vs_bnds, vb, scale, ib, gm, gds,
                  gds_load_list, vds_tail_min, vstar_min):
        vs_list = []
        score = None
        for itarg, ibf, gmf, gdsf, vg, vd, gds_load, (vs_min, vs_max) in \
                zip(itarg_list, ib, gm, gds, vg_list, vd_list, gds_load_list, vs_bnds):

            def zero_fun(vs):
                arg = self._db.get_fun_arg(vbs=vb - vs, vds=vd - vs, vgs=vg - vs)
                return scale * ibf(arg) - itarg

            v1 = zero_fun(vs_min)
            v2 = zero_fun(vs_max)
            if v1 < 0 and v2 < 0 or v1 > 0 and v2 > 0:
                # no solution
                return None, None

            vs_cur = sciopt.brentq(zero_fun, vs_min, vs_max)  # type: float
            if abs(vs_cur - vb) < vds_tail_min:
                return None, None
            cur_arg = self._db.get_fun_arg(vbs=vb - vs_cur, vds=vd - vs_cur, vgs=vg - vs_cur)
            gm_cur = gmf(cur_arg) * scale
            ib_cur = ibf(cur_arg) * scale
            if 2 * ib_cur / gm_cur < vstar_min:
                # check V* spec again
                return None, None
            gds_cur = gdsf(cur_arg) * scale
            score_cur = gm_cur / (gds_cur + gds_load)
            if score is None:
                score = score_cur
            else:
                score = min(score, score_cur)

            vs_list.append(vs_cur)

        return vs_list, score

    def _solve_iunit_from_vstar(self, vstar_min, vb, vg_list, vd_list, vs_bnds, ib, gm):
        iunit_list = []
        for ibf, gmf, vg, vd, (vs_min, vs_max) in zip(ib, gm, vg_list, vd_list, vs_bnds):

            def zero_fun(vs):
                arg = self._db.get_fun_arg(vbs=vb - vs, vds=vd - vs, vgs=vg - vs)
                return 2 * ibf(arg) / gmf(arg) - vstar_min

            v1 = zero_fun(vs_min)
            v2 = zero_fun(vs_max)
            if v1 < 0 and v2 < 0:
                # cannot meet vstar_min spec
                return None
            elif v1 > 0 and v2 > 0:
                # NOTE: for very small V*, it may be the case that V* versus vs is not monotonic.
                vs_sol = vs_min if v1 < v2 else vs_max
            else:
                vs_sol = sciopt.brentq(zero_fun, vs_min, vs_max)

            cur_arg = self._db.get_fun_arg(vbs=vb - vs_sol, vds=vd - vs_sol, vgs=vg - vs_sol)
            iunit_list.append(ibf(cur_arg))

        return iunit_list

    def get_dsn_info(self):
        # type: () -> Optional[Dict[str, Any]]
        if self._best_op is None:
            return None

        intent, stack, w, seg, vg_list, vd_list, vs_list, vb = self._best_op

        self._db.set_dsn_params(w=w, intent=intent, stack=stack)
        ib = self._db.get_function_list('ibias')
        gm = self._db.get_function_list('gm')
        gds = self._db.get_function_list('gds')
        cgg = self._db.get_function_list('cgg')
        cdd = self._db.get_function_list('cdd')

        vstar_list, gm_list, gds_list, cgg_list, cdd_list = [], [], [], [], []
        for ibf, gmf, gdsf, cggf, cddf, vg, vd, vs in \
                zip(ib, gm, gds, cgg, cdd, vg_list, vd_list, vs_list):
            arg = self._db.get_fun_arg(vbs=vb - vs, vds=vd - vs, vgs=vg - vs)
            cur_ib = seg * ibf(arg)
            cur_gm = seg * gmf(arg)
            cur_gds = seg * gdsf(arg)
            cur_cgg = seg * cggf(arg)
            cur_cdd = seg * cddf(arg)
            vstar_list.append(2 * cur_ib / cur_gm)
            gds_list.append(cur_gds)
            cgg_list.append(cur_cgg)
            cdd_list.append(cur_cdd)
            gm_list.append(cur_gm)

        return dict(
            vstar=vstar_list,
            gm=gm_list,
            gds=gds_list,
            cgg=cgg_list,
            cdd=cdd_list,
            intent=intent,
            stack=stack,
            w=w,
            seg=seg,
            vs=vs_list,
        )

max_stars_repo_name: tinapiao/Software-IC-Automation

max_issues_repo_name: tinapiao/Software-IC-Automation

max_forks_repo_name: tinapiao/Software-IC-Automation