text:
"""
Takes an input NIST file and a Propellant, and creates polynomials for all the relevant variables

"""
from numpy import polyfit, poly1d
import numpy as np
import os.path
import warnings

warnings.simplefilter('ignore', np.RankWarning)

POLY_DEG = 10

def get_prop_data(filename):
    script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
    rel_path = "prop-data/"+filename+".txt"
    abs_file_path = os.path.join(script_dir, rel_path)

    file = open(abs_file_path,"r")
    all_data = file.read()

    all_data = all_data.split("\n")
    split_data = []
    for i in all_data:
        split_data.append(i.split("\t"))

    return(split_data[1:])

def get_combo_data(filename):
    script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
    rel_path = "combo-data/"+filename+".txt"
    abs_file_path = os.path.join(script_dir, rel_path)

    file = open(abs_file_path,"r")
    all_data = file.read()

    all_data = all_data.split("\n")
    split_data = []
    for i in all_data:
        split_data.append(i.split("\t"))

    return(split_data[1:])

def get_fit(data,position,degree):
    temperatures = []
    values = []

    for row in data:
        if len(row)<=position:
            continue
        if row[0]=="undefined":
            continue
        if row[position]=="undefined":
            continue
        temperatures.append(float(row[0]))
        values.append(float(row[position]))

    if len(temperatures)==0:
        return(None)
    if degree>len(temperatures):
        coeffs = polyfit(temperatures,values,len(temperatures))
    else:
        coeffs = polyfit(temperatures,values,degree)

    polynom = poly1d(coeffs)

    return(polynom)

def propellant_data(propellant,filename):
    split_data = get_prop_data(filename)

    #values for position of each data type are hard coded and manually read from file
    if propellant.twophase == 2:
        propellant.F_pressure = get_fit(split_data,1,POLY_DEG)
        propellant.F_density_liquid = get_fit(split_data,2,POLY_DEG)

        propellant.F_density_vapour = get_fit(split_data,14,POLY_DEG)
    elif propellant.twophase == 0:
        propellant.F_pressure = get_fit(split_data,1,POLY_DEG)
        propellant.F_density_liquid = get_fit(split_data,2,POLY_DEG)

def combo_data(propellant_mix, filename):
    split_data = get_combo_data(filename)

    data = []
    for row in split_data:
        if row[0]==propellant_mix.oxidiser_name and row[1]==propellant_mix.fuel_name:
            data = row
            break
    propellant_mix.ISP_sea_level = float(row[3])
    propellant_mix.OF_molar_ratio = float(row[2])
    propellant_mix.chamber_temp = float(row[4])
    propellant_mix.exhaust_temp = float(row[5])

reason: import numpy,from numpy


text:
# Multi-armed bandit-based algorithms

import numpy as np
from .genetic import GeneticOperators

class HyperBand:
    def __init__(self, config_ranges, loss, max_resources_per_conf, eta=3):
        """
        Init the hyper band algorithm
        :param config_ranges: Config ranges eg taken from graph.get_hpopt_config_ranges()
        :param loss: A loss function with params (config, status, resources) which evaluates the configuration given
        the resources allocation. The returned value is the tuple (loss_value, status). The status is an internal value used
        by the function loss to store information during successive calls. The initial status (first execution) is None.
        The status parameter is optional.
        :param max_resources_per_conf: The maximum amount of resources allocated to evaluate a configuration
        :param eta: Param that controls the proportion of configurations discarded in each round of successive halving.
        """
        self.config_ranges = dict(config_ranges)
        self.loss = loss
        self.max_resources_per_conf = max_resources_per_conf
        self.eta = eta

    def __call__(self):
        """
        Execute the algorithm
        :return: The best config
        """
        # TODO statistics

        f_eta = float(self.eta)
        s_max = int(np.floor(np.log(self.max_resources_per_conf)/np.log(f_eta)))
        budget = (s_max+1.)*self.max_resources_per_conf
        gene = GeneticOperators(self.config_ranges)
        best = (np.inf, None)   # the best observed config, the tuple is of the form (loss_value, config)

        for s in range(s_max, -1, -1):
            n = int(np.ceil((budget/self.max_resources_per_conf)*np.power(f_eta, s)/(s+1)))
            r = self.max_resources_per_conf*np.power(f_eta, -s)
            # begin SuccessiveHalving with (n, r) inner loop
            configs = gene.create_population(size=n)
            for i in range(s+1):
                assert len(configs) > 0
                n_i = int(np.floor(n*np.power(f_eta, -i)))
                r_i = r*np.power(f_eta, i)  # TODO is this supposed to be integer?
                results = [self.loss(config=config, resources=r_i)[0] for config in configs]
                k = int(np.floor(n_i/f_eta))
                if k == 0:
                    break
                # take top k performing configurations indexes (based on loss)
                selection = np.argsort(results)[:k]

                # update the best observed config
                local_best_loss = results[selection[0]]
                if local_best_loss < best[0]:
                    best = (local_best_loss, configs[selection[0]])

                configs = [configs[t] for t in selection]

        return best[1]

ext: py

max_stars_repo_name: sflinter/hypergraph

max_issues_repo_name: sflinter/hypergraph

max_forks_repo_name: sflinter/hypergraph

reason: import numpy


text:
#################################################################################
# The Institute for the Design of Advanced Energy Systems Integrated Platform
# Framework (IDAES IP) was produced under the DOE Institute for the
# Design of Advanced Energy Systems (IDAES), and is copyright (c) 2018-2021
# by the software owners: The Regents of the University of California, through
# Lawrence Berkeley National Laboratory,  National Technology & Engineering
# Solutions of Sandia, LLC, Carnegie Mellon University, West Virginia University
# Research Corporation, et al.  All rights reserved.
#
# Please see the files COPYRIGHT.md and LICENSE.md for full copyright and
# license information.
#################################################################################
"""
IDAES plate heat exchanger model (PHE) using effectiveness-NTU method, derived
from the core HeatExchangerNTU model.

Notes:
Let P = number of passes(same for both fluids)
if P is even PHE is in counter-current mode
if P is odd PHE  is in co-current mode
Hot and cold fluids flow alternatively in the channels.
Divider plates may be used to partition the passes in separate sections.
The heat transfer area  of a single plate depends on the total heat transfer
area as specified by the manufacturer and the total number of active plates.

Reference
Detailed model equations can be found in the paper :
[1] Akula, P., Eslick, J., Bhattacharyya, D. and Miller, D.C., 2019.
Modelling and Parameter Estimation of a Plate Heat Exchanger
as Part of a Solvent-Based Post-Combustion CO2 Capture System.
In Computer Aided Chemical Engineering (Vol. 47, pp. 47-52). Elsevier.

"""

# Import Pyomo libraries
from pyomo.environ import (Constraint,
                           exp,
                           Expression,
                           NonNegativeIntegers,
                           Param,
                           PositiveIntegers,
                           PositiveReals,
                           units as pyunits,
                           Var)
from pyomo.common.config import ConfigValue, In, Integer
from pyomo.util.calc_var_value import calculate_variable_from_constraint

# Import IDAES cores
from idaes.core import declare_process_block_class
from idaes.generic_models.unit_models.heat_exchanger_ntu import \
    HeatExchangerNTUData
from idaes.core.util.constants import Constants
from idaes.core.util import get_solver
import idaes.core.util.unit_costing as costing
import idaes.logger as idaeslog

__author__ = "Paul Akula, Andrew Lee"

# Set up logger
_log = idaeslog.getLogger(__name__)

@declare_process_block_class("PlateHeatExchanger")
class PlateHeatExchangerData(HeatExchangerNTUData):
    """Plate Heat Exchanger(PHE) Unit Model."""

    CONFIG = HeatExchangerNTUData.CONFIG()

    CONFIG.declare("passes", ConfigValue(
        default=4,
        domain=Integer,
        description="Number of passes",
        doc="""Number of passes of the fluids through the heat exchanger"""))
    CONFIG.declare("channels_per_pass", ConfigValue(
        default=12,
        domain=Integer,
        description="Number of channels for each pass",
        doc="""Number of channels to be used in each pass where a channel
               is the space between two plates with a flowing fluid"""))
    CONFIG.declare("number_of_divider_plates", ConfigValue(
        default=0,
        domain=Integer,
        description="Number of divider plates in heat exchanger",
        doc="""Divider plates are used to create separate partitions in the
        unit. Each pass can be separated by a divider plate"""))

    # Update config block setting for pressure change to always be true
    CONFIG.hot_side.has_pressure_change = True
    CONFIG.hot_side.get("has_pressure_change").set_domain(In([True]))
    CONFIG.hot_side.get("has_pressure_change")._description = (
        "Pressure change term construction flag - must be True")
    CONFIG.hot_side.get("has_pressure_change")._doc = (
        "Plate Heat Exchanger model includes correlations for pressure drop "
        "thus has_pressure_change must be True")

    CONFIG.cold_side.has_pressure_change = True
    CONFIG.cold_side.get("has_pressure_change").set_domain(In([True]))
    CONFIG.cold_side.get("has_pressure_change")._description = (
        "Pressure change term construction flag - must be True")
    CONFIG.cold_side.get("has_pressure_change")._doc = (
        "Plate Heat Exchanger model includes correlations for pressure drop "
        "thus has_pressure_change must be True")

    def build(self):
        # Call super.build to setup model
        # This will create the control volumes, ports and basic equations
        super().build()

        # Units will be based on hot side properties
        units_meta = self.config.hot_side.property_package.get_metadata(
            ).get_derived_units

        # ---------------------------------------------------------------------
        # Plate design variables and parameter
        self.number_of_passes = Param(
            initialize=self.config.passes,
            units=pyunits.dimensionless,
            domain=PositiveIntegers,
            doc="Number of hot/cold fluid passes",
            mutable=True)

        # Assuming number of channels is equal in all plates
        self.channels_per_pass = Param(
            initialize=self.config.channels_per_pass,
            units=pyunits.dimensionless,
            domain=PositiveIntegers,
            doc="Number of channels in each pass",
            mutable=True)

        self.number_of_divider_plates = Param(
            initialize=self.config.number_of_divider_plates,
            units=pyunits.dimensionless,
            domain=NonNegativeIntegers,
            doc="Number of divider plates in heat exchanger",
            mutable=True)

        self.plate_length = Var(initialize=1.6925,
                                units=units_meta("length"),
                                domain=PositiveReals,
                                doc="Length of heat exchanger plate")
        self.plate_width = Var(initialize=0.6135,
                               units=units_meta("length"),
                               domain=PositiveReals,
                               doc="Width of heat exchanger plate")
        self.plate_thickness = Var(initialize=0.0006,
                                   units=units_meta("length"),
                                   domain=PositiveReals,
                                   doc="Thickness of heat exchanger plate")
        self.plate_pact_length = Var(initialize=0.381,
                                     units=units_meta("length"),
                                     domain=PositiveReals,
                                     doc="Compressed plate pact length")
        self.port_diameter = Var(initialize=0.2045,
                                 units=units_meta("length"),
                                 domain=PositiveReals,
                                 doc="Port diamter")

        self.plate_therm_cond = Var(
            initialize=16.2,
            units=units_meta("thermal_conductivity"),
            domain=PositiveReals,
            doc="Thermal conductivity heat exchanger plates")

        # Set default value of total heat transfer area
        self.area.set_value(114.3)

        # ---------------------------------------------------------------------
        # Derived geometric quantities
        total_plates = (
            2*self.channels_per_pass*self.number_of_passes +
            1 + self.number_of_divider_plates)
        total_active_plates = (
            2*self.channels_per_pass*self.number_of_passes -
            (1 + self.number_of_divider_plates))

        self.plate_gap = Expression(
            expr=self.plate_pact_length/total_plates - self.plate_thickness)

        self.plate_area = Expression(
            expr=self.area / total_active_plates,
            doc='Heat transfer area of single plate')

        self.surface_enlargement_factor = Expression(
            expr=self.plate_area/(self.plate_length*self.plate_width))

        # Channel equivalent diameter
        self.channel_diameter = Expression(
            expr=2*self.plate_gap/self.surface_enlargement_factor,
            doc="Channel equivalent diameter")

        # ---------------------------------------------------------------------
        # Fluid velocities
        def rule_port_vel_hot(blk, t):
            return (4*blk.hot_side.properties_in[t].flow_vol /
                    (Constants.pi*blk.port_diameter**2))
        self.hot_port_velocity = Expression(
            self.flowsheet().time,
            rule=rule_port_vel_hot,
            doc='Hot side port velocity')

        def rule_port_vel_cold(blk, t):
            return (4*pyunits.convert(blk.cold_side.properties_in[t].flow_vol,
                                      to_units=units_meta("flow_vol")) /
                    (Constants.pi*blk.port_diameter**2))
        self.cold_port_velocity = Expression(
            self.flowsheet().time,
            rule=rule_port_vel_cold,
            doc='Cold side port velocity')

        def rule_channel_vel_hot(blk, t):
            return (blk.hot_side.properties_in[t].flow_vol /
                    (blk.channels_per_pass*blk.plate_width*blk.plate_gap))
        self.hot_channel_velocity = Expression(
            self.flowsheet().time,
            rule=rule_channel_vel_hot,
            doc='Hot side channel velocity')

        def rule_channel_vel_cold(blk, t):
            return (pyunits.convert(blk.cold_side.properties_in[t].flow_vol,
                                    to_units=units_meta("flow_vol")) /
                    (blk.channels_per_pass*blk.plate_width*blk.plate_gap))
        self.cold_channel_velocity = Expression(
            self.flowsheet().time,
            rule=rule_channel_vel_cold,
            doc='Cold side channel velocity')

        # ---------------------------------------------------------------------
        # Reynolds & Prandtl numbers
        # Density cancels out of Reynolds number if mass flow rate is used
        def rule_Re_h(blk, t):
            return (
                blk.hot_side.properties_in[t].flow_mass *
                blk.channel_diameter /
                (blk.channels_per_pass*blk.plate_width*blk.plate_gap *
                 blk.hot_side.properties_in[t].visc_d_phase["Liq"]))
        self.Re_hot = Expression(self.flowsheet().time,
                                 rule=rule_Re_h,
                                 doc='Hot side Reynolds number')

        def rule_Re_c(blk, t):
            return (
                pyunits.convert(
                    blk.cold_side.properties_in[t].flow_mass /
                    blk.cold_side.properties_in[t].visc_d_phase["Liq"],
                    to_units=units_meta("length")) *
                blk.channel_diameter /
                (blk.channels_per_pass*blk.plate_width*blk.plate_gap))
        self.Re_cold = Expression(self.flowsheet().time,
                                  rule=rule_Re_c,
                                  doc='Cold side Reynolds number')

        def rule_Pr_h(blk, t):
            return (blk.hot_side.properties_in[t].cp_mol /
                    blk.hot_side.properties_in[t].mw *
                    blk.hot_side.properties_in[t].visc_d_phase["Liq"] /
                    blk.hot_side.properties_in[t].therm_cond_phase["Liq"])
        self.Pr_hot = Expression(self.flowsheet().time,
                                 rule=rule_Pr_h,
                                 doc='Hot side Prandtl number')

        def rule_Pr_c(blk, t):
            return (blk.cold_side.properties_in[t].cp_mol /
                    blk.cold_side.properties_in[t].mw *
                    blk.cold_side.properties_in[t].visc_d_phase["Liq"] /
                    blk.cold_side.properties_in[t].therm_cond_phase["Liq"])
        self.Pr_cold = Expression(self.flowsheet().time,
                                  rule=rule_Pr_c,
                                  doc='Cold side Prandtl number')

        # ---------------------------------------------------------------------
        # Heat transfer coefficients
        # Parameters for Nusselt number correlation
        self.Nusselt_param_a = Param(initialize=0.4,
                                     domain=PositiveReals,
                                     units=pyunits.dimensionless,
                                     mutable=True,
                                     doc='Nusselt parameter A')
        self.Nusselt_param_b = Param(initialize=0.663,
                                     domain=PositiveReals,
                                     units=pyunits.dimensionless,
                                     mutable=True,
                                     doc='Nusselt parameter B')
        self.Nusselt_param_c = Param(initialize=0.333,
                                     domain=PositiveReals,
                                     units=pyunits.dimensionless,
                                     mutable=True,
                                     doc='Nusselt parameter C')

        # Film heat transfer coefficients
        def rule_hotside_transfer_coeff(blk, t):
            return (blk.hot_side.properties_in[t].therm_cond_phase["Liq"] /
                    blk.channel_diameter *
                    blk.Nusselt_param_a * blk.Re_hot[t]**blk.Nusselt_param_b *
                    blk.Pr_hot[t]**blk.Nusselt_param_c)
        self.heat_transfer_coefficient_hot_side = Expression(
            self.flowsheet().time,
            rule=rule_hotside_transfer_coeff,
            doc='Hot side heat transfer coefficient')

        def rule_coldside_transfer_coeff(blk, t):
            return (
                pyunits.convert(
                    blk.cold_side.properties_in[t].therm_cond_phase["Liq"],
                    to_units=units_meta("thermal_conductivity")) /
                blk.channel_diameter *
                blk.Nusselt_param_a * blk.Re_cold[t]**blk.Nusselt_param_b *
                blk.Pr_cold[t]**blk.Nusselt_param_c)
        self.heat_transfer_coefficient_cold_side = Expression(
            self.flowsheet().time,
            rule=rule_coldside_transfer_coeff,
            doc='Cold side heat transfer coefficient')

        # Overall heat transfer coefficient
        def rule_U(blk, t):
            return blk.heat_transfer_coefficient[t] == (
                1.0 / (
                    1.0/blk.heat_transfer_coefficient_hot_side[t] +
                    blk.plate_gap/blk.plate_therm_cond +
                    1.0/blk.heat_transfer_coefficient_cold_side[t]))
        self.overall_heat_transfer_eq = Constraint(
            self.flowsheet().time,
            rule=rule_U,
            doc='Calculations of overall heat transfer coefficient')

        # Effectiveness based on sub-heat exchangers
        # Divide NTU by number of channels per pass
        def rule_Ecf(blk, t):
            if blk.number_of_passes.value % 2 == 0:
                return (
                    blk.effectiveness[t] ==
                    (1 -
                     exp(-blk.NTU[t]/blk.channels_per_pass *
                         (1 - blk.Cratio[t]))) /
                    (1 -
                     blk.Cratio[t] *
                     exp(-blk.NTU[t]/blk.channels_per_pass *
                         (1 - blk.Cratio[t]))))
            elif blk.pass_num.value % 2 == 1:
                return (
                    blk.effectiveness[t] ==
                    (1 -
                     exp(-blk.NTU[t]/blk.channels_per_pass *
                         (1 + blk.Cratio[t]))) /
                    (1 + blk.Cratio[t]))
        self.effectiveness_correlation = Constraint(
            self.flowsheet().time,
            rule=rule_Ecf,
            doc='Correlation for effectiveness factor')

        # ---------------------------------------------------------------------
        # Pressure drop correlations
        # Friction factor calculation
        self.friction_factor_param_a = Param(
            initialize=0.0,
            units=pyunits.dimensionless,
            doc='Friction factor parameter A',
            mutable=True)
        self.friction_factor_param_b = Param(
            initialize=18.29,
            units=pyunits.dimensionless,
            doc='Friction factor parameter B',
            mutable=True)
        self.friction_factor_param_c = Param(
            initialize=-0.652,
            units=pyunits.dimensionless,
            doc='Friction factor parameter C',
            mutable=True)

        def rule_fric_h(blk, t):
            return (blk.friction_factor_param_a +
                    blk.friction_factor_param_b *
                    blk.Re_hot[t]**(blk.friction_factor_param_c))
        self.friction_factor_hot = Expression(self.flowsheet().time,
                                              rule=rule_fric_h,
                                              doc='Hot side friction factor')

        def rule_fric_c(blk, t):
            return (blk.friction_factor_param_a +
                    blk.friction_factor_param_b *
                    blk.Re_cold[t]**(blk.friction_factor_param_c))
        self.friction_factor_cold = Expression(self.flowsheet().time,
                                               rule=rule_fric_c,
                                               doc='Cold side friction factor')

        def rule_hotside_dP(blk, t):
            return blk.hot_side.deltaP[t] == -(
                (2*blk.friction_factor_hot[t] *
                 (blk.plate_length + blk.port_diameter) *
                 blk.number_of_passes*blk.hot_channel_velocity[t]**2 *
                 blk.hot_side.properties_in[t].dens_mass /
                 blk.channel_diameter) +
                (0.7*blk.number_of_passes*blk.hot_port_velocity[t]**2 *
                 blk.hot_side.properties_in[t].dens_mass) +
                (blk.hot_side.properties_in[t].dens_mass *
                 pyunits.convert(Constants.acceleration_gravity,
                                 to_units=units_meta("acceleration")) *
                 (blk.plate_length + blk.port_diameter)))
        self.hot_side_deltaP_eq = Constraint(self.flowsheet().time,
                                             rule=rule_hotside_dP)

        def rule_coldside_dP(blk, t):
            return blk.cold_side.deltaP[t] == -(
                (2*blk.friction_factor_cold[t] *
                 (blk.plate_length + blk.port_diameter) *
                 blk.number_of_passes*blk.cold_channel_velocity[t]**2 *
                 pyunits.convert(blk.cold_side.properties_in[t].dens_mass,
                                 to_units=units_meta("density_mass")) /
                 blk.channel_diameter) +
                (0.7*blk.number_of_passes*blk.cold_port_velocity[t]**2 *
                 pyunits.convert(blk.cold_side.properties_in[t].dens_mass,
                                 to_units=units_meta("density_mass"))) +
                (pyunits.convert(blk.cold_side.properties_in[t].dens_mass,
                                 to_units=units_meta("density_mass")) *
                 pyunits.convert(Constants.acceleration_gravity,
                                 to_units=units_meta("acceleration")) *
                 (blk.plate_length + blk.port_diameter)))
        self.cold_side_deltaP_eq = Constraint(self.flowsheet().time,
                                              rule=rule_coldside_dP)

    def initialize(
        self,
        hot_side_state_args=None,
        cold_side_state_args=None,
        outlvl=idaeslog.NOTSET,
        solver=None,
        optarg=None,
        duty=None,
    ):
        """
        Heat exchanger initialization method.

        Args:
            hot_side_state_args : a dict of arguments to be passed to the
                property initialization for the hot side (see documentation of
                the specific property package) (default = None).
            cold_side_state_args : a dict of arguments to be passed to the
                property initialization for the cold side (see documentation of
                the specific property package) (default = None).
            outlvl : sets output level of initialization routine
            optarg : solver options dictionary object (default=None, use
                     default solver options)
            solver : str indicating which solver to use during
                     initialization (default = None, use default solver)
            duty : an initial guess for the amount of heat transfered. This
                should be a tuple in the form (value, units), (default
                = (1000 J/s))

        Returns:
            None

        """
        # Set solver options
        init_log = idaeslog.getInitLogger(self.name, outlvl, tag="unit")
        solve_log = idaeslog.getSolveLogger(self.name, outlvl, tag="unit")

        hot_side = self.hot_side
        cold_side = self.cold_side

        # Create solver
        opt = get_solver(solver, optarg)

        flags1 = hot_side.initialize(
            outlvl=outlvl, optarg=optarg, solver=solver,
            state_args=hot_side_state_args
        )

        init_log.info_high("Initialization Step 1a (hot side) Complete.")

        flags2 = cold_side.initialize(
            outlvl=outlvl, optarg=optarg, solver=solver,
            state_args=cold_side_state_args
        )

        init_log.info_high("Initialization Step 1b (cold side) Complete.")

        # ---------------------------------------------------------------------
        # Solve unit without heat transfer equation
        # if costing block exists, deactivate
        if hasattr(self, "costing"):
            self.costing.deactivate()

        self.energy_balance_constraint.deactivate()
        self.effectiveness_correlation.deactivate()
        self.effectiveness.fix(0.68)

        # Get side 1 and side 2 heat units, and convert duty as needed
        s1_units = hot_side.heat.get_units()
        s2_units = cold_side.heat.get_units()

        if duty is None:
            # Assume 1000 J/s and check for unitless properties
            if s1_units is None and s2_units is None:
                # Backwards compatability for unitless properties
                s1_duty = - 1000
                s2_duty = 1000
            else:
                s1_duty = pyunits.convert_value(-1000,
                                                from_units=pyunits.W,
                                                to_units=s1_units)
                s2_duty = pyunits.convert_value(1000,
                                                from_units=pyunits.W,
                                                to_units=s2_units)
        else:
            # Duty provided with explicit units
            s1_duty = -pyunits.convert_value(duty[0],
                                             from_units=duty[1],
                                             to_units=s1_units)
            s2_duty = pyunits.convert_value(duty[0],
                                            from_units=duty[1],
                                            to_units=s2_units)

        cold_side.heat.fix(s2_duty)
        for i in hot_side.heat:
            hot_side.heat[i].value = s1_duty

        with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc:
            res = opt.solve(self, tee=slc.tee)

        init_log.info_high("Initialization Step 2 {}.".format(
            idaeslog.condition(res)))

        cold_side.heat.unfix()
        self.energy_balance_constraint.activate()

        for t in self.effectiveness:
            calculate_variable_from_constraint(
                self.effectiveness[t], self.effectiveness_correlation[t])

        # ---------------------------------------------------------------------
        # Solve unit with new effectiveness factor
        with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc:
            res = opt.solve(self, tee=slc.tee)
        init_log.info_high("Initialization Step 3 {}.".format(
            idaeslog.condition(res)))

        self.effectiveness_correlation.activate()
        self.effectiveness.unfix()

        # ---------------------------------------------------------------------
        # Final solve of full modelr
        with idaeslog.solver_log(solve_log, idaeslog.DEBUG) as slc:
            res = opt.solve(self, tee=slc.tee)
        init_log.info_high("Initialization Step 4 {}.".format(
            idaeslog.condition(res)))

        # ---------------------------------------------------------------------
        # Release Inlet state
        hot_side.release_state(flags1, outlvl=outlvl)
        cold_side.release_state(flags2, outlvl=outlvl)

        init_log.info("Initialization Completed, {}".format(
            idaeslog.condition(res)))

        # if costing block exists, activate and initialize
        if hasattr(self, "costing"):
            self.costing.activate()
            costing.initialize(self.costing)

max_stars_repo_name: carldlaird/idaes-pse

max_issues_repo_name: carldlaird/idaes-pse

max_forks_repo_name: carldlaird/idaes-pse

reason: from pyomo


text:
from typing import Union, List
from pathlib import Path
from tifffile import imread
import zarr
import dask.array as da
import numpy as np
from tiler import Tiler

# use zarr==2.10.3

def tifffile_to_dask(im_fp: Union[str,Path]) -> Union[da.array, List[da.Array]]:
    imdata = zarr.open(imread(im_fp, aszarr=True))
    if isinstance(imdata, zarr.hierarchy.Group):
        imdata = [da.from_zarr(imdata[z]) for z in imdata.array_keys()]
    else:
        imdata = da.from_zarr(imdata)
    return imdata

af_im = tifffile_to_dask("./AF/S01-AF.tiff")

mask_im = tifffile_to_dask("./tissue-images/tissue-masks/S01-tissue-mask.tiff")
# read image into memory it's np.uint8 so smaller footprint
mask_im_in_memory = dask_im.compute()

# initialize tiler for mask
mask_tiler = Tiler(data_shape=mask_im.shape,
              tile_shape=(512, 512),
              overlap=0.2)

# initialize tiler for AF image (needs multi-channel shape)
af_tiler = Tiler(data_shape=af_im.shape,
              tile_shape=(3, 512, 512),
              overlap=0.2)

# get index of all tiles that overlap with the mask greater than 75%
tile_overlaps = []
for idx, tile in mask_tiler.iterate(mask_im):
    npx_in_mask = np.sum(tile) / 255
    overlap_with_mask = (np.sum(tile) / 255) / tile.size
    if overlap_with_mask > 0.75:
        tile_overlaps.append(idx)

#check the tiling is the same
assert mask_tiler.n_tiles == af_tiler.n_tiles

# get an "in-mask" tile on AF image...
# could then be written to disk... sent to model, etc.
good_tile = af_tiler.get_tile(af_im, tile_id=tile_overlaps[1])


text:
# Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import

import os

import pytest
from sagemaker.pytorch import PyTorch

from test.integration import data_dir, dist_operations_path, mnist_script, ROLE
from test.utils.local_mode_utils import assert_files_exist

MODEL_SUCCESS_FILES = {
    'model': ['success'],
    'output': ['success'],
}

@pytest.fixture(scope='session', name='dist_gpu_backend', params=['gloo'])
def fixture_dist_gpu_backend(request):
    return request.param

@pytest.mark.skip_gpu
def test_dist_operations_path_cpu(docker_image, dist_cpu_backend, sagemaker_local_session, tmpdir):
    estimator = PyTorch(entry_point=dist_operations_path,
                        role=ROLE,
                        image_name=docker_image,
                        train_instance_count=2,
                        train_instance_type='local',
                        sagemaker_session=sagemaker_local_session,
                        hyperparameters={'backend': dist_cpu_backend},
                        output_path='file://{}'.format(tmpdir))

    _train_and_assert_success(estimator, str(tmpdir))

@pytest.mark.skip_cpu
def test_dist_operations_path_gpu_nccl(docker_image, sagemaker_local_session, tmpdir):
    estimator = PyTorch(entry_point=dist_operations_path,
                        role=ROLE,
                        image_name=docker_image,
                        train_instance_count=1,
                        train_instance_type='local_gpu',
                        sagemaker_session=sagemaker_local_session,
                        hyperparameters={'backend': 'nccl'},
                        output_path='file://{}'.format(tmpdir))

    _train_and_assert_success(estimator, str(tmpdir))

@pytest.mark.skip_gpu
def test_cpu_nccl(docker_image, sagemaker_local_session, tmpdir):
    estimator = PyTorch(entry_point=mnist_script,
                        role=ROLE,
                        image_name=docker_image,
                        train_instance_count=2,
                        train_instance_type='local',
                        sagemaker_session=sagemaker_local_session,
                        hyperparameters={'backend': 'nccl'},
                        output_path='file://{}'.format(tmpdir))

    with pytest.raises(RuntimeError):
        estimator.fit({'training': 'file://{}'.format(os.path.join(data_dir, 'training'))})

    failure_file = {'output': ['failure']}
    assert_files_exist(str(tmpdir), failure_file)

@pytest.mark.skip_gpu
def test_mnist_cpu(docker_image, dist_cpu_backend, sagemaker_local_session, tmpdir):
    estimator = PyTorch(entry_point=mnist_script,
                        role=ROLE,
                        image_name=docker_image,
                        train_instance_count=2,
                        train_instance_type='local',
                        sagemaker_session=sagemaker_local_session,
                        hyperparameters={'backend': dist_cpu_backend},
                        output_path='file://{}'.format(tmpdir))

    success_files = {
        'model': ['model.pth'],
        'output': ['success'],
    }
    _train_and_assert_success(estimator, str(tmpdir), success_files)

def _train_and_assert_success(estimator, output_path, output_files=MODEL_SUCCESS_FILES):
    estimator.fit({'training': 'file://{}'.format(os.path.join(data_dir, 'training'))})
    assert_files_exist(output_path, output_files)

max_stars_repo_name: arjkesh/sagemaker-pytorch-container

max_issues_repo_name: arjkesh/sagemaker-pytorch-container

max_forks_repo_name: saimidu/sagemaker-pytorch-container

reason: from sage


text:
import numpy as np
import matplotlib.pyplot as plt
from Maximum_likelihood_estimation import get_data

def kernel(u):
    return 1 / (np.sqrt(2 * np.pi)) * np.exp(-u ** 2 / 2)

def main(h):
    s = 0
    for j in range(len(data)):
        s += kernel((u - data[j]) / h)
    return s

data = get_data('data/residential_premises.xlsx', 'data')
margin = 7
u = np.linspace(min(data) - margin, max(data) + margin, 10000)
# window width
h = np.array([0.4, 1, 1.6])

plt.figure(figsize=(12, 7))
plt.plot(data, np.zeros(len(data)), 'o', color='royalblue', alpha=0.15, label='data (living space area [m2])')
for i in range(len(h)):
    f = 1 / (len(data) * h[i]) * main(h[i])
    plt.plot(u, f, label=h[i])
    plt.legend()
plt.show()

max_stars_repo_name: W-Sikora/LeastSquares

max_issues_repo_name: W-Sikora/LeastSquares

max_forks_repo_name: W-Sikora/LeastSquares


text:
#this is the real create csv file from NECTEC Dataset
import matplotlib.pyplot as plt
import numpy as np
from skimage.feature import hog
from skimage import data, exposure
import cv2 as cv
import sys
from thresh import *
from PIL import Image
import Image, ImageDraw
import pandas as pd
import glob
data = []
filenamee = []
for filename in glob.glob('Dataset2/Char/*/*.bmp'):
    img = Image.open(filename)
    img = img.resize((16,32), Image.ANTIALIAS)
    fd, hog_image = hog(img, orientations=9, pixels_per_cell=(4, 4),
                        cells_per_block=(2, 2),block_norm="L1", transform_sqrt=True, visualize=True, multichannel=False)
    data.append(fd)
    filenamee.append(filename)

df = pd.DataFrame(data)
ff = pd.DataFrame(filenamee)
ff.to_csv('filename2.csv')
df.to_csv('testglob2.csv')


text:
import numpy as np
import math
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

from sklearn.metrics.classification import accuracy_score, log_loss
from sklearn.gaussian_process import GaussianProcessClassifier
from sklearn.gaussian_process.kernels import RBF
from sklearn.gaussian_process.kernels import Matern

# This file is used to test can GP represent certain occupancy grid map with given sensors position and measurements
num_row = 30
num_col = 30

x = np.arange(0.5,num_row + 0.5,1.0)
y = np.arange(0.5,num_col + 0.5,1.0)

# Define obstacle
obs1_x = np.arange(18.5,27.5,1.0)
obs1_y = np.arange(24.5,30.5,1.0)
obs1 = [[idx1,idx2] for idx1 in obs1_x for idx2 in obs1_y]

obs2_x = np.arange(0.5,8.5,1.0)
obs2_y = np.arange(13.5,24.5,1.0)
obs2 = [[idx1,idx2] for idx1 in obs2_x for idx2 in obs2_y]

obs3_x = np.arange(15.5,30.5,1.0)
obs3_y = np.arange(17.5,21.5,1.0)
obs3 = [[idx1,idx2] for idx1 in obs3_x for idx2 in obs3_y]

obs4_x = np.arange(27.5,30.5,1.0)
obs4_y = np.arange(12.5,17.5,1.0)
obs4 = [[idx1,idx2] for idx1 in obs4_x for idx2 in obs4_y]

obs5_x = np.arange(0.5,5.5,1.0)
obs5_y = np.arange(5.5,10.5,1.0)
obs5 = [[idx1,idx2] for idx1 in obs5_x for idx2 in obs5_y]

obs6_x = np.arange(9.5,20.5,1.0)
obs6_y = np.arange(0.5,6.5,1.0)
obs6 = [[idx1,idx2] for idx1 in obs6_x for idx2 in obs6_y]

obs7_x = np.arange(23.5,28.5,1.0)
obs7_y = np.arange(3.5,7.5,1.0)
obs7 = [[idx1,idx2] for idx1 in obs7_x for idx2 in obs7_y]

obs_ = obs1 + obs2 + obs3 + obs4 + obs5 + obs6 + obs7
print("the length of obs is {}".format(len(obs_)))
training_X = []
training_Y = []

# Sensors position
sensors_1 = [396,337,309,865,275]
sensors_2 = [691,632,92,667,664]
sensors_3 = [843,813,443,595,188]
sensors_4 = [545,514,137,513,107]
sensors_5 = [725,755,756,786,745]
sensors_6 = [804,48,774,127,128]
sensors_7 = [686,51,806,655,716]
sensors_8 = [54,688,718,282,197]
sensors_9 = [411,808,809,779,729]
sensors_10 = [605,31,541,481,670]
sensors_11 = [841,369,505,255,46]
sensors_12 = [249,278,248,94,219]
sensors_13 = [472,638,168,636,228]
sensors_14 = [868,156,845,784,814]
sensors_15 = [440,253,313,252,566]
sensors_16 = [90,60,748,63,624]
sensors_17 = [338,444.310,872,512]
sensors_18 = [257,226,159,413,446]
sensors_19 = [839,696,700,77,224]
sensors_20 = [399,669,32,281,1]
sensors_21 = [840,506,535,536,475]

sensors = sensors_1 + sensors_2 + sensors_3 + sensors_4 + sensors_5 + sensors_6 + sensors_7 + sensors_8 + sensors_9 + sensors_10
sensors = sensors + sensors_11 + sensors_12 + sensors_13 + sensors_14 + sensors_15 + sensors_16 + sensors_17 + sensors_18 + sensors_19 + sensors_20 + sensors_21
sensors_X = []
for ss in sensors:
    col = ss%num_row + 0.5
    row = (30 - (ss - col + 0.5)/num_row) - 0.5
    sensors_X.append([row,col])

print(sensors_X)
# sensors_X = [[0.5,6.5],[0.5,1.5],[1.5,8.5],[1.5,7.5],[1.5,6.5],[1.5,5.5],[1.5,3.5],[1.5,1.5],[2.5,9.5],[2.5,6.5],[2.5,5.5],[2.5,4.5],[3.5,8.5],[3.5,5.5],[3.5,4.5],[3.5,3.5],[3.5,1.5],[4.5,6.5],[4.5,4.5],[4.5,1.5],[5.5,7.5],[5.5,4.5],[5.5,2.5],[6.5,5.5],[6.5,3.5],[7.5,7.5],[7.5,5.5],[8.5,8.5],[8.5,6.5]]
print("The # of sensors is {}".format(len(sensors_X)))
for c_sensor in sensors_X:
    s_x = c_sensor[0]
    s_y = c_sensor[1]
    if [s_x,s_y] not in training_X:
        training_X.append([s_x,s_y])
        if c_sensor not in obs_:
            training_Y.append(0)
        else:
            training_Y.append(1)

    # Up
    n_y = s_y + 1
    if n_y < num_row + 0.5:
        if [s_x,n_y] not in training_X:
            training_X.append([s_x,n_y])
            if [s_x,n_y] not in obs_:
                training_Y.append(0)
            else:
                training_Y.append(1)

    # Down
    n_y = s_y - 1
    if n_y >= 0.5:
        if [s_x, n_y] not in training_X:
            training_X.append([s_x,n_y])
            if [s_x,n_y] not in obs_:
                training_Y.append(0)
            else:
                training_Y.append(1)

    # Left
    n_x = s_x - 1
    if n_x >= 0.5:
        if [n_x,s_y] not in training_X:
            training_X.append([n_x,s_y])
            if [n_x,s_y] not in obs_:
                training_Y.append(0)
            else:
                training_Y.append(1)

    # Right
    n_x = s_x + 1
    if n_x < num_col + 0.5:
        if [n_x,s_y] not in training_X:
            training_X.append([n_x,s_y])
            if [n_x,s_y] not in obs_:
                training_Y.append(0)
            else:
                training_Y.append(1)

# training_X.append([0.5,9.5])
# training_Y.append(0)

# training_X.append([0.5,4.5])
# training_Y.append(0)

# training_X.append([5.5,0.5])
# training_Y.append(0)

# training_X.append([7.5,9.5])
# training_Y.append(0)

print(len(training_X))
print(len(training_Y))

# Specify Gaussian Processes with fixed and optimized hyperparameters
gp_opt = GaussianProcessClassifier(RBF(length_scale=1.0))
gp_opt.fit(training_X,training_Y)
print("The trained hyperparameter are {}".format((gp_opt.kernel_.theta)))
# print("Log Marginal Likelihood (optimized): %.3f"
#       % gp_opt.log_marginal_likelihood(gp_opt.kernel_.theta))

# print("The probability of occupancy is {}".format(p_occ))

fig = plt.figure()
ZZ = np.empty([30,30])
for idx1, row in enumerate(x):
    for idx2,col in enumerate(y):
        K = [row,col]
        if K in training_X:
            ZZ[idx1,idx2] = 0.0
        else:
        # print("({},{})".format(row,col))
        # print("({},{})".format(idx1,idx2))
            p_occ = gp_opt.predict_proba(np.reshape(K,(-1,2)))[:,1]

            occ_ = gp_opt.predict(np.reshape(K,(-1,2)))
            ZZ[idx1,idx2] = p_occ
            plt.text(row,col,"{}".format(round(p_occ[0],3)), color='black',fontsize=5)
            if occ_ == 1:
                plt.text(row + 0.25, col+0.25, "OCC", color='black', fontsize=5)
            else:
                plt.text(row + 0.25, col + 0.25, "FREE", color='black', fontsize=5)

XX ,YY = np.mgrid[0:30:31j,0:30:31j]

CMAP = plt.get_cmap('jet')
plt.pcolormesh(XX,YY,ZZ,cmap=CMAP)
cb = plt.colorbar(shrink = 1.0)

plt.show()


text:
from numpy.random import uniform, randint
import numpy as np

from kheppy.evocom.commons import Controller

class ControllerGA(Controller):

    def copy(self):
        return ControllerGA(self.weights, self.biases, self.fitness)

    def mutate(self, prob):
        self.weights = [(w + uniform(-0.05, 0.05, w.shape) * (uniform(0, 1, w.shape) < prob)) for w in self.weights]
        self.biases = [(b + uniform(-0.05, 0.05, b.shape) * (uniform(0, 1, b.shape) < prob)) for b in self.biases]

    def cross(self, c2):
        c1 = self
        c1_new = ControllerGA()
        c2_new = ControllerGA()
        for w1, w2, b1, b2 in zip(c1.weights, c2.weights, c1.biases, c2.biases):
            w1f, w2f, b1f, b2f = w1.flatten(), w2.flatten(), b1.flatten(), b2.flatten()

            half_w = randint(0, len(w1f))  # int(len(w1f)/2)
            half_b = randint(0, len(b1f))  # int(len(b1f)/2)
            w1_new = np.reshape(np.append(w1f[:half_w], w2f[half_w:]), w1.shape)
            w2_new = np.reshape(np.append(w2f[:half_w], w1f[half_w:]), w1.shape)
            b1_new = np.reshape(np.append(b1f[:half_b], b2f[half_b:]), b1.shape)
            b2_new = np.reshape(np.append(b2f[:half_b], b1f[half_b:]), b1.shape)
            c1_new.weights.append(w1_new)
            c2_new.weights.append(w2_new)
            c1_new.biases.append(b1_new)
            c2_new.biases.append(b2_new)

        return c1_new, c2_new

max_stars_repo_name: Ewande/kheppy

max_issues_repo_name: Ewande/kheppy

max_forks_repo_name: Ewande/kheppy