def test_nested_dict(self):

        nested_dict = {'key': {'nested_dict': 'a value'}}

        actual = crit_dim_var(nested_dict)
        expected = False
        assert actual == expected

        nested_dict = {'another_key': {}, 'key': []}
        actual = crit_dim_var(nested_dict)
        expected = True
        assert actual == expected
Ejemplo n.º 2
0
def replace_variable(_user_defined_vars, strategy_vars):
    """Replace default strategy variables with user defined variables

    Arguments
    ----------
    _user_defined_vars : dict
        User defined strategy variables
    strategy_vars : dict
        Default strategy variables

    Returns
    -------
    strategy_vars : dict
        Updated strategy variables with user defined inputs
    """
    for new_var, new_var_vals in _user_defined_vars.items():

        crit_single_dim = narrative_related.crit_dim_var(new_var_vals)

        if crit_single_dim:
            strategy_vars[new_var] = new_var_vals
        else:
            for sub_var_name, sector_sub_var in new_var_vals.items():

                if type(sector_sub_var) is dict:
                    strategy_vars[new_var][sub_var_name] = {}
                    for sector, sub_var in sector_sub_var.items():
                        strategy_vars[new_var][sub_var_name][sector] = sub_var
                else:
                    strategy_vars[new_var][sub_var_name] = sector_sub_var

            strategy_vars[new_var] = strategy_vars[new_var]
    return strategy_vars
    def test_nested_dict_in_list(self):

        nested_dict_in_list = [{'key': {'nested_dict': 'a value'}}]

        actual = crit_dim_var(nested_dict_in_list)
        expected = False
        print("AC ULT " + str(actual))
        assert actual == expected
Ejemplo n.º 4
0
def spatial_explicit_modelling_strategy_vars(strategy_vars,
                                             spatially_modelled_vars, regions,
                                             fuel_disagg, f_reg, f_reg_norm,
                                             f_reg_norm_abs):
    """
    Spatial explicit modelling of scenario variables
    based on narratives. Convert strategy variables
    (e.g. country level) to regional variables.

    Arguments
    ---------
    strategy_vars : dict
        All non_spatial strategy variables
    spatially_modelled_vars : dict
        All spatial variables
    regions : dict
        Regions
    fuel_disagg : dict
        Disaggregated fuel
    f_reg, f_reg_norm, f_reg_norm_abs : dict
        Spatial factors
    """
    regional_vars = {}

    # Iterate strategy variables and calculate regional variable
    for var_name, strategy_var in strategy_vars.items():
        logging.info("...Spatially explicit diffusion modelling %s", var_name)

        single_dim_var = narrative_related.crit_dim_var(strategy_var)

        if single_dim_var:
            regional_vars[var_name] = get_regional_narrative(
                var_name, strategy_var, spatially_modelled_vars, regions,
                fuel_disagg, f_reg, f_reg_norm, f_reg_norm_abs)
        else:
            regional_vars[var_name] = {}

            for sub_var_name, sector_sub_strategy_var in strategy_var.items():
                regional_vars[var_name][sub_var_name] = {}
                if type(sector_sub_strategy_var) is dict:  # sectors defined
                    for sector, sub_strategy_var in sector_sub_strategy_var.items(
                    ):
                        regional_vars[var_name][sub_var_name][
                            sector] = get_regional_narrative(
                                sub_var_name, sub_strategy_var,
                                spatially_modelled_vars, regions, fuel_disagg,
                                f_reg, f_reg_norm, f_reg_norm_abs)
                else:
                    regional_vars[var_name][
                        sub_var_name] = get_regional_narrative(
                            sub_var_name, sector_sub_strategy_var,
                            spatially_modelled_vars, regions, fuel_disagg,
                            f_reg, f_reg_norm, f_reg_norm_abs)

                regional_vars[var_name] = dict(regional_vars[var_name])

    return regional_vars
def generate_default_parameter_narratives(
        default_streategy_vars,
        end_yr=2050,
        base_yr=2015
    ):
    """Load default parameters and create default timesteps

    Arguments
    ---------
    default_streategy_vars : dict
        Default parameter values
    end_yr : int
        Simulation end year
    base_yr : int
        Base year

    Returns
    -------
    strategy_vars : dict
        Strategy variables
    """
    strategy_vars = defaultdict(dict)

    # Create default narrative for every simulation parameter
    for var_name, var_entries in default_streategy_vars.items():
        crit_single_dim = narrative_related.crit_dim_var(var_entries)

        if crit_single_dim:

            # Create default narrative with only one timestep from simulation base year to simulation end year
            strategy_vars[var_name] = narrative_related.default_narrative(
                end_yr=end_yr,
                value_by=var_entries['default_value'],
                value_ey=var_entries['default_value'],
                diffusion_choice=var_entries['diffusion_type'],
                base_yr=base_yr,
                regional_specific=var_entries['regional_specific']) # Criteria whether the same for all regions or not

        else:
            # Standard narrative for multidimensional narrative
            for sub_var_name, sub_var_entries in var_entries.items():

                try:
                    scenario_value = sub_var_entries['scenario_value']
                except TypeError:
                    raise TypeError("{}: Var_entries dict: {}".format(var_name, var_entries))

                # Crate single-step default narratives (up to end_year)
                strategy_vars[var_name][sub_var_name] = narrative_related.default_narrative(
                    end_yr=end_yr,
                    value_by=sub_var_entries['default_value'],
                    value_ey=scenario_value,
                    diffusion_choice=sub_var_entries['diffusion_type'],
                    base_yr=base_yr,
                    regional_specific=sub_var_entries['regional_specific'])

    return dict(strategy_vars)
    def test_crit_list(self):

        a_list = []
        actual = crit_dim_var(a_list)
        expected = True
        assert actual == expected

        the_bug = {
            'enduse': [],
            'diffusion_type': 'linear',
            'default_value': 15.5,
            'name': 'ss_t_base_heating',
            'sector': True,
            'regional_specific': False,
            'description': 'Base temperature',
            'scenario_value': 15.5
        }
        actual = crit_dim_var(the_bug)
        expected = True
        assert actual == expected
    def test_crit_dim_var_buggy(self):

        fixture = [{
            'sig_steepness': 1,
            'sector': 'dummy_sector',
            'diffusion_choice': 'linear',
            'sig_midpoint': 0,
            'regional_specific': True,
            'base_yr': 2015,
            'value_ey': 0.05,
            'value_by': 0.05,
            'regional_vals_ey': {
                'W06000023': 0.05,
                'W06000010': 0.05
            },
            'regional_vals_by': {
                'W06000023': 0.05,
                'W06000010': 0.05
            },
            'end_yr': 2030,
            'enduse': [],
            'default_by': 0.05
        }, {
            'sig_steepness': 1,
            'sector': 'dummy_sector',
            'diffusion_choice': 'linear',
            'sig_midpoint': 0,
            'regional_specific': True,
            'base_yr': 2030,
            'value_ey': 0.05,
            'value_by': 0.05,
            'regional_vals_ey': {
                'W06000023': 0.05,
                'W06000010': 0.05
            },
            'regional_vals_by': {
                'W06000023': 0.05,
                'W06000010': 0.05
            },
            'end_yr': 2050,
            'enduse': [],
            'default_by': 0.05
        }]
        actual = crit_dim_var(fixture)
        expected = True
        assert actual == expected
    def test_crit_dim_var_ordered(self):

        the_bug = OrderedDict()
        the_bug['a'] = {
            'diffusion_type': 'linear',
            'default_value': 15.5,
            'name': 'ss_t_base_heating',
            'sector': True,
            'regional_specific': False,
            'description': 'Base temperature',
            'scenario_value': 15.5,
            'enduse': []
        }
        the_bug['b'] = 'vakye'
        actual = crit_dim_var(the_bug)
        expected = False
        assert actual == expected
    def test_crit_in_list(self):

        crit_in_list = [{
            'sig_midpoint': 0,
            'value_by': 5,
            'diffusion_choice': 'linear',
            'fueltype_replace': 0,
            'regional_specific': False,
            'base_yr': 2015,
            'value_ey': 5,
            'sig_steepness': 1,
            'end_yr': 2050,
            'fueltype_new': 0
        }]

        actual = crit_dim_var(crit_in_list)
        expected = True
        print("AC ULT " + str(actual))
        assert actual == expected
Ejemplo n.º 10
0
def autocomplete_strategy_vars(
        strategy_vars,
        narrative_crit=False
    ):
    """Autocomplete all narratives or strategy variables with
    and 'enduse' or 'sector' in case they are not defined.

    Arguments
    ----------
    strategy_vars

    Returns
    -------
    narrative_crit : bool
        Criteria wheter inputs are a narrative or not
    """
    if not narrative_crit:
        out_dict = defaultdict(dict)

        for var_name, var_entries in strategy_vars.items():
            crit_single_dim = narrative_related.crit_dim_var(var_entries)

            if crit_single_dim:
                out_dict[var_name] = var_entries

                # If no 'enduse' defined, add empty list of affected enduses
                out_dict[var_name]['scenario_value'] = var_entries['default_value']

                if 'enduse' not in var_entries:
                    out_dict[var_name]['enduse'] = []
                if 'sector' not in var_entries:
                    out_dict[var_name]['sector'] = True  # All sector
            else:
                for sub_var_name, sub_var_entries in var_entries.items():
                    out_dict[var_name][sub_var_name] = sub_var_entries

                    out_dict[var_name][sub_var_name]['scenario_value'] = sub_var_entries['default_value']

                    # If no 'enduse' defined, add empty list of affected enduses
                    if 'enduse' not in sub_var_entries:
                        out_dict[var_name][sub_var_name]['enduse'] = []
                    if 'sector' not in sub_var_entries:
                        out_dict[var_name][sub_var_name]['sector'] = True # All sector
    else:
        # Same but narratives which need to be iterated
        out_dict = {}

        for var_name, var_entries in strategy_vars.items():
            out_dict[var_name] = {}

            crit_single_dim = narrative_related.crit_dim_var(var_entries)

            if crit_single_dim:
                updated_narratives = []

                for narrative in var_entries:
                    #If no 'enduse' defined, add empty list of affected enduses
                    if 'enduse' not in narrative:
                        narrative['enduse'] = []
                    if 'sector' not in narrative:
                        narrative['sector'] = 'dummy_sector' # All sector
                    updated_narratives.append(narrative)

                out_dict[var_name] = updated_narratives
            else:
                #print("   ...user defined variable: %s", var_name)
                for sub_var_name, sector_sub_var_entries in var_entries.items():

                    if type(sector_sub_var_entries) is dict: # If sectors are defined
                        for sector, sub_var_entries in sector_sub_var_entries.items():
                            out_dict[var_name][sub_var_name] = {}

                            updated_narratives = []
                            for narrative in sub_var_entries:

                                # If no 'enduse' defined, add empty list of affected enduses
                                if 'enduse' not in narrative:
                                    narrative['enduse'] = []
                                if 'sector' not in narrative:
                                    narrative['sector'] = sector
                                updated_narratives.append(narrative)

                            out_dict[var_name][sub_var_name][sector] = updated_narratives

                    else: # no sectors defined
                        updated_narratives = []
                        for narrative in sector_sub_var_entries:

                            if 'enduse' not in narrative:
                                narrative['enduse'] = [sub_var_name]
                            updated_narratives.append(narrative)

                        out_dict[var_name][sub_var_name] = updated_narratives

    return dict(out_dict)
Ejemplo n.º 11
0
def generate_annual_param_vals(
        regions,
        strategy_vars,
        sim_yrs,
    ):
    """
    Calculate parameter values for every year based
    on defined narratives and also add a generic
    container of other information necessary
    for parameter.

    Inputs
    -------
    regions : dict
        Regions
    strategy_vars : dict4
        Strategy variable infirmation
    sim_yrs : list
        Simulated years

    Returns
    -------
    container_reg_param : dict
        Values for all simulated years for every region (all parameters for which values
        are provided for every region)
    container_non_reg_param : dict
        Values for all simulated years (all the same for very region)
    """
    reg_param = {}
    for region in regions:
        reg_param[region] = {}
    non_reg_param = {}

    for var_name, strategy_vars_values in strategy_vars.items():

        for region in regions:
            reg_param[region][var_name] = {}
        non_reg_param[var_name] = {}

        single_dim_var = narrative_related.crit_dim_var(
            strategy_vars_values)

        logging.debug("Calculating annual values for parameter: {} {}".format(var_name, single_dim_var))
        if single_dim_var:

            # Additional dictionary passed along with every variable containing additional information
            param_info = {}
            try:
                param_info['fueltype_replace'] = strategy_vars_values[0]['fueltype_replace']
                param_info['fueltype_new'] = strategy_vars_values[0]['fueltype_new']
            except KeyError:
                pass

            # Calculate annual parameter value
            regional_strategy_vary = generate_general_parameter(
                regions=regions,
                narratives=strategy_vars_values,
                sim_yrs=sim_yrs)

            # Test if regional specific or not based on first narrative
            for narrative in strategy_vars_values[:1]:
                reg_specific_crit = narrative['regional_specific']

            if reg_specific_crit:
                for region in regions:
                    reg_param[region][var_name] = regional_strategy_vary[region]
                    reg_param[region][var_name]['param_info'] = param_info
            else:
                non_reg_param[var_name] = regional_strategy_vary
                non_reg_param[var_name]['param_info'] = param_info
        else:
            if not hasattr(strategy_vars_values, 'items'):
                msg = "Error in {}. Object is not a dict, but a {}: {}"
                raise TypeError(msg.format(var_name,
                                           type(strategy_vars_values),
                                           strategy_vars_values))

            for sub_var_name, sector_sub_var_values in strategy_vars_values.items():

                for region in regions:
                    reg_param[region][var_name][sub_var_name] = {}
                non_reg_param[var_name][sub_var_name] = {}

                if type(sector_sub_var_values) is dict:
                    for sector, sub_var_values in sector_sub_var_values.items():

                        if var_name == 'generic_fuel_switch':

                            all_new_fueltypes = []
                            for narrative in sub_var_values:
                                all_new_fueltypes.append(narrative['fueltype_new'])

                            for new_fueltype in all_new_fueltypes:

                                # Get all narratives of this fueltype
                                narratives = []
                                for narrative in sub_var_values:
                                    if narrative['fueltype_new'] == new_fueltype:
                                        narratives.append(narrative)

                                param_info = {}
                                param_info['fueltype_new'] = int(new_fueltype)
                                param_info['fueltype_replace'] = narrative['fueltype_replace']

                                # Calculate annual parameter value
                                regional_strategy_vary = generate_general_parameter(
                                    regions=regions,
                                    narratives=narratives,
                                    sim_yrs=sim_yrs)

                                if narrative['regional_specific']:
                                    for region in regions:
                                        reg_param[region][var_name][sub_var_name][sector][param_info['fueltype_new']] = dict(regional_strategy_vary[region])
                                        reg_param[region][var_name][sub_var_name][sector][param_info['fueltype_new']]['param_info'] = param_info
                                else:
                                    non_reg_param[var_name][sub_var_name][sector][param_info['fueltype_new']] = regional_strategy_vary
                                    non_reg_param[var_name][sub_var_name][sector][param_info['fueltype_new']]['param_info'] = param_info
                        else:
                            # Calculate annual parameter value
                            regional_strategy_vary = generate_general_parameter(
                                regions=regions,
                                narratives=sub_var_values,
                                sim_yrs=sim_yrs)

                            # Test if regional specific or not based on first narrative
                            for narrative in sub_var_values[:1]:
                                reg_specific_crit = narrative['regional_specific']

                            if reg_specific_crit:
                                for region in regions:
                                    reg_param[region][var_name][sub_var_name][sector] = dict(regional_strategy_vary[region])
                            else:
                                non_reg_param[var_name][sub_var_name][sector] = regional_strategy_vary
                # Single dimensional
                else:
                    param_info = {}
                    if var_name == 'generic_fuel_switch':

                        all_new_fueltypes = []
                        for narrative in sector_sub_var_values:
                            all_new_fueltypes.append(narrative['fueltype_new'])

                        for new_fueltype in all_new_fueltypes:
                            narratives = []
                            for narrative in sector_sub_var_values:
                                if narrative['fueltype_new'] == new_fueltype:
                                    narratives.append(narrative)

                            # Calculate annual parameter value
                            regional_strategy_vary = generate_general_parameter(
                                regions=regions,
                                narratives=narratives,
                                sim_yrs=sim_yrs)

                            param_info = {}
                            param_info['fueltype_replace'] = narrative['fueltype_replace']
                            param_info['fueltype_new'] = new_fueltype

                            if narrative['regional_specific']:
                                for region in regions:
                                    reg_param[region][var_name][sub_var_name][param_info['fueltype_new']] = dict(regional_strategy_vary[region])
                                    reg_param[region][var_name][sub_var_name][param_info['fueltype_new']]['param_info'] = param_info
                            else:
                                non_reg_param[var_name][sub_var_name][param_info['fueltype_new']] = regional_strategy_vary
                                non_reg_param[var_name][sub_var_name][param_info['fueltype_new']]['param_info'][param_info['fueltype_replace']] = param_info
                    else:
                        # Calculate annual parameter value
                        regional_strategy_vary = generate_general_parameter(
                            regions=regions,
                            narratives=sector_sub_var_values,
                            sim_yrs=sim_yrs)

                        # Test if regional specific or not based on first narrative
                        for narrative in sector_sub_var_values[:1]:
                            reg_specific_crit = narrative['regional_specific']

                        if reg_specific_crit:
                            for region in regions:
                                reg_param[region][var_name][sub_var_name] = dict(regional_strategy_vary[region])
                        else:
                            non_reg_param[var_name][sub_var_name] = regional_strategy_vary

    return dict(reg_param), dict(non_reg_param)