예제 #1
0
    def _transform(self, sampled_parameters, parameters):
        #TODO:: add some more useful debug logging
        temp = {}
        for par in parameters:
            # only keep uncertainties that exist in this model
            try:
                value = sampled_parameters[par.name]
            except KeyError:
                if par.default is not None:
                    value = par.default
                else:
                    ema_logging.debug('{} not found'.format(par.name))
                    continue

            multivalue = False
            if isinstance(par, CategoricalParameter):
                category = par.categories[value]

                value = category.value

                if category.multivalue == True:
                    multivalue = True
                    values = value

            # translate uncertainty name to variable name
            for i, varname in enumerate(par.variable_name):
                # a bit hacky implementation, investigate some kind of
                # zipping of variable_names and values
                if multivalue:
                    value = values[i]

                temp[varname] = value

        sampled_parameters.data = temp
예제 #2
0
def set_fig_to_bw(fig,
                  style=HATCHING,
                  line_style='continuous',
                  all_colors=None):
    """
    TODO it would be nice if for lines you can select either markers, gray 
    scale, or simple black
    
    Take each axes in the figure and transform its content to black and white. 
    Lines are tranformed based on different line styles. Fills such as can 
    be used in `meth`:envelopes are gray-scaled. Heathmaps are also gray-scaled.
    
    
    derived from and expanded for my use from:
    http://stackoverflow.com/questions/7358118/matplotlib-black-white-colormap-with-dashes-dots-etc
    
    Parameters
    ----------
    fig : figure
          the figure to transform to black and white
    style : {HATCHING, GREYSCALE}
            parameter controlling how collections are transformed.
    line_style: str
                linestyle to use for converting, can be continuous, black
                or None  
    
    """
    all_colors = _identify_colors(fig)

    if len(all_colors) > len(bw_mapping):
        mapping_cycle = itertools.cycle(bw_mapping)
        ema_logging.warning(
            'more colors used than provided in B&W mapping, cycling over mapping'
        )
    else:
        mapping_cycle = bw_mapping
    colormap = dict(zip(all_colors, mapping_cycle))
    ema_logging.debug(colormap.keys())

    max_shade = 0.9
    for i, color in enumerate(colormap.keys()):
        relative_color = max_shade * ((i + 1) / len(all_colors))
        colormap[color]['fill'] = str(relative_color)

    for ax in fig.get_axes():
        set_ax_lines_bw(ax, colormap)
        set_ax_patches_bw(ax, colormap)
        set_ax_collections_to_bw(ax, style, colormap)
        set_ax_legend_to_bw(ax, style, colormap)

    set_legend_to_bw(fig.legends, style, colormap)
예제 #3
0
def set_fig_to_bw(fig, style=HATCHING,
                  line_style='continuous', all_colors=None):
    """
    TODO it would be nice if for lines you can select either markers, gray
    scale, or simple black

    Take each axes in the figure and transform its content to black and white.
    Lines are tranformed based on different line styles. Fills such as can
    be used in `meth`:envelopes are gray-scaled. Heathmaps are also gray-scaled.


    derived from and expanded for my use from:
    http://stackoverflow.com/questions/7358118/matplotlib-black-white-colormap-with-dashes-dots-etc

    Parameters
    ----------
    fig : figure
          the figure to transform to black and white
    style : {HATCHING, GREYSCALE}
            parameter controlling how collections are transformed.
    line_style: str
                linestyle to use for converting, can be continuous, black
                or None

    """
    all_colors = _identify_colors(fig)

    if len(all_colors) > len(bw_mapping):
        mapping_cycle = itertools.cycle(bw_mapping)
        ema_logging.warning(
            'more colors used than provided in B&W mapping, cycling over mapping')
    else:
        mapping_cycle = bw_mapping
    colormap = dict(zip(all_colors, mapping_cycle))
    ema_logging.debug(colormap.keys())

    max_shade = 0.9
    for i, color in enumerate(colormap.keys()):
        relative_color = max_shade * ((i + 1) / len(all_colors))
        colormap[color]['fill'] = str(relative_color)

    for ax in fig.get_axes():
        set_ax_lines_bw(ax, colormap)
        set_ax_patches_bw(ax, colormap)
        set_ax_collections_to_bw(ax, style, colormap)
        set_ax_legend_to_bw(ax, style, colormap)

    set_legend_to_bw(fig.legends, style, colormap)
    def test_log_messages(self):
        ema_logging.log_to_stderr(ema_logging.DEBUG)
        
        with mock.patch('ema_workbench.util.ema_logging._logger') as mocked_logger:
            message = 'test message'
            ema_logging.debug(message)
            mocked_logger.debug.assert_called_with(message)

            ema_logging.info(message)
            mocked_logger.info.assert_called_with(message)
            
            ema_logging.warning(message)
            mocked_logger.warning.assert_called_with(message)
            
            ema_logging.error(message)
            mocked_logger.error.assert_called_with(message)
            
            ema_logging.exception(message)
            mocked_logger.exception.assert_called_with(message)
            
            ema_logging.critical(message)
            mocked_logger.critical.assert_called_with(message)            
예제 #5
0
    def _transform(self, sampled_parameters, parameters):
        # TODO:: add some more useful debug logging
        temp = {}
        for par in parameters:
            # only keep uncertainties that exist in this model
            try:
                value = sampled_parameters[par.name]
            except KeyError:
                if par.default is not None:
                    value = par.default
                else:
                    ema_logging.debug("{} not found".format(par.name))
                    continue

            if (isinstance(par, CategoricalParameter)) or (isinstance(par, IntegerParameter)):
                value = int(value)

            multivalue = False
            if isinstance(par, CategoricalParameter):
                value = par.cat_for_index(value)
                category = par.categories[value]

                value = category.value

                if category.multivalue == True:
                    multivalue = True
                    values = value

            # translate uncertainty name to variable name
            for i, varname in enumerate(par.variable_name):
                # a bit hacky implementation, investigate some kind of
                # zipping of variable_names and values
                if multivalue:
                    value = values[i]

                temp[varname] = value

        sampled_parameters.data = temp
예제 #6
0
    def test_log_messages(self):
        ema_logging.log_to_stderr(ema_logging.DEBUG)

        with mock.patch(
                'ema_workbench.util.ema_logging._logger') as mocked_logger:
            message = 'test message'
            ema_logging.debug(message)
            mocked_logger.debug.assert_called_with(message)

            ema_logging.info(message)
            mocked_logger.info.assert_called_with(message)

            ema_logging.warning(message)
            mocked_logger.warning.assert_called_with(message)

            ema_logging.error(message)
            mocked_logger.error.assert_called_with(message)

            ema_logging.exception(message)
            mocked_logger.exception.assert_called_with(message)

            ema_logging.critical(message)
            mocked_logger.critical.assert_called_with(message)
예제 #7
0
    def _transform(self, sampled_parameters, parameters):
        
        if not parameters:
            # no parameters defined, so nothing to transform, mainly
            # useful for manual specification of scenario /  policy
            # without having to define uncertainties / levers
            return
        
        temp = {}
        for par in parameters:
            # only keep uncertainties that exist in this model
            try:
                value = sampled_parameters[par.name]
            except KeyError:
                if par.default is not None:
                    value = par.default
                else:
                    ema_logging.debug('{} not found'.format(par.name))
                    continue

            multivalue = False
            if isinstance(par, CategoricalParameter):
                if par.multivalue == True:
                    multivalue = True
                    values = value
            
            # translate uncertainty name to variable name
            for i, varname in enumerate(par.variable_name):
                # a bit hacky implementation, investigate some kind of 
                # zipping of variable_names and values
                if multivalue:
                    value = values[i]
                
                temp[varname] = value
        
        sampled_parameters.data = temp