Exemplo n.º 1
0
def calculate_statistics(model_name, ref_name, data, config=None):
    """
    Calculates the statistics for the given model and reference variables located in the data file. Calculation will be
    performed according to the provided configuration.
    @param model_name: the name of the model variable.
    @param ref_name: the name of the reference variable.
    @param data: the input data object.
    @param config: the optional configuration.
    @return: a dictionary of statistics.
    """

    if config is None:
        config = get_default_config()

    is_gridded = len(data.get_reference_dimensions(ref_name)) > 1
    if is_gridded:
        reference_values, model_values = data.get_values(ref_name, model_name)
        unit = data.unit(model_name)
        return processor.calculate_statistics(model_values, reference_values, model_name, ref_name, unit, config)

    me = MatchupEngine(data, config)
    matchups = me.find_all_matchups()
    if config.remove_empty_matchups:
        matchups = me.remove_empty_matchups(matchups)
    if len(matchups) == 0:
        print("No matchups found; maybe allow higher maximum time delta.")
        return
    unit = data.unit(model_name)
    return calculate_statistics_from_matchups(matchups, model_name, ref_name, data, unit, config=None)
Exemplo n.º 2
0
def create_target_diagram(statistics, config=None):
    figure = plt.figure()
    if config is None:
        config = get_default_config()

    statistics = utils.ensure_list(statistics)

    diagram = TargetDiagram(figure, config.normalise_target_diagram,
                            config.show_legends,
                            config.utilise_stddev_difference)

    diagram.setup_axes()
    for stats in statistics:
        diagram.plot_sample(
            stats['bias'], stats['unbiased_rmse'], stats['normalised_rmse'],
            stats['rmse'], stats['ref_stddev'], stats['stddev'],
            create_sample_name(stats['model_name'], stats['unit']))

    if config.normalise_target_diagram:
        diagram.plot_correcoeff_marker_line()

    diagram.update_legend()
    if not config.normalise_target_diagram:
        diagram.update_ranges(config.target_diagram_bounds)

    return diagram
Exemplo n.º 3
0
    def __init__(self, config=None):
        """Constructs a new instance of Output.

        Keyword arguments:
            data -- the data object
            config -- the configuration the processors and matchup engine have been run with (optional)
        """
        self.config = config if config is not None else get_default_config()
        self.separator = self.config.separator
Exemplo n.º 4
0
    def __init__(self, config=None):
        """Constructs a new instance of Output.

        Keyword arguments:
            data -- the data object
            config -- the configuration the processors and matchup engine have been run with (optional)
        """
        self.config = config if config is not None else get_default_config()
        self.separator = self.config.separator
Exemplo n.º 5
0
def create_taylor_diagrams(statistics, config=None):
    if config is None:
        config = get_default_config()

    statistics = utils.ensure_list(statistics)

    statistics_by_name_and_unit = sort_statistics_by_name_and_unit(
        config, statistics)
    diagrams = []

    for name in statistics_by_name_and_unit.keys():
        for current_unit in statistics_by_name_and_unit[name].keys():
            current_statistics = statistics_by_name_and_unit[name][
                current_unit]

            ref_names = list(
                map(lambda x: x.get('ref_name'), current_statistics))
            ref_stddevs = list(
                map(lambda x: x.get('ref_stddev'), current_statistics)
            ) if config.use_absolute_standard_deviation else list(
                np.ones(len(current_statistics)))
            units = list(map(lambda x: x.get('unit'), current_statistics))
            ref = tuple(zip(ref_names, ref_stddevs, units))
            model_stddevs = list(
                map(lambda x: x.get('stddev'), current_statistics)
            ) if config.use_absolute_standard_deviation else list(
                map(lambda x: x.get('normalised_stddev'), current_statistics))
            max_stddev = max(ref_stddevs + model_stddevs) * 1.5

            for v in ref_stddevs:
                if v == 0.0 or np.isnan(v):
                    logging.warning(
                        'Unable to create Taylor diagram from statistics.')
                    logging.debug('Statistics: %s' % current_statistics)
                    continue

            figure = plt.figure()
            diagram = TaylorDiagram(figure, ref,
                                    config.show_negative_corrcoeff,
                                    config.use_absolute_standard_deviation,
                                    config.show_legends, max_stddev)

            diagram.setup_axes()
            for stats in current_statistics:
                model_name = stats[
                    'model_name'] if 'model_name' in stats else None
                stddev = stats[
                    'stddev'] if config.use_absolute_standard_deviation else stats[
                        'normalised_stddev']
                diagram.plot_sample(stats['corrcoeff'], stddev, model_name,
                                    stats['unit'])

            diagram.update_legend()
            diagrams.append(diagram)

    return diagrams
Exemplo n.º 6
0
def calculate_statistics(model_values,
                         reference_values,
                         model_name=None,
                         ref_name=None,
                         unit=None,
                         config=None):
    """Calculate the statistics for the given reference and model arrays.
    It is recommended to provide model_name and ref_name in order to allow for meaningful output.
    """

    if config is None:
        config = get_default_config()

    model_percentiles = percentiles(model_values.ravel(), config.alpha,
                                    config.beta)
    ref_percentiles = percentiles(reference_values.ravel(), config.alpha,
                                  config.beta)
    model_minmax = minmax(model_values)
    ref_minmax = minmax(reference_values)
    stats = dict()
    stats['model_name'] = model_name
    stats['ref_name'] = ref_name
    stats['unit'] = unit
    stats['rmse'] = rmse(reference_values, model_values)
    stats['corrcoeff'] = correlation(reference_values, model_values)
    stats['unbiased_rmse'] = unbiased_rmse(reference_values, model_values)
    stats['normalised_rmse'] = normalised_rmse(reference_values, model_values,
                                               config.ddof, stats['corrcoeff'])
    stats['pbias'] = percentage_model_bias(reference_values, model_values)
    stats['bias'] = bias(reference_values, model_values)
    stats['reliability_index'] = reliability_index(reference_values,
                                                   model_values)
    stats['model_efficiency'] = model_efficiency(reference_values,
                                                 model_values)
    stats['mean'] = mean(model_values)
    stats['ref_mean'] = mean(reference_values)
    stats['stddev'] = stddev(model_values, config.ddof)
    stats['ref_stddev'] = stddev(reference_values, config.ddof)
    stats['normalised_stddev'] = stats['stddev'] / stats['ref_stddev']
    stats['median'] = model_percentiles[0]
    stats['ref_median'] = ref_percentiles[0]
    stats['p90'] = model_percentiles[1]
    stats['ref_p90'] = ref_percentiles[1]
    stats['p95'] = model_percentiles[2]
    stats['ref_p95'] = ref_percentiles[2]
    stats['min'] = model_minmax[0]
    stats['ref_min'] = ref_minmax[0]
    stats['max'] = model_minmax[1]
    stats['ref_max'] = ref_minmax[1]
    return stats
Exemplo n.º 7
0
def calculate_statistics(model_values, reference_values, model_name=None, ref_name=None, unit=None, config=None):
    """Calculate the statistics for the given reference and model arrays.
    It is recommended to provide model_name and ref_name in order to allow for meaningful output.
    """

    if config is None:
        config = get_default_config()

    model_percentiles = percentiles(model_values.ravel(), config.alpha, config.beta)
    ref_percentiles = percentiles(reference_values.ravel(), config.alpha, config.beta)
    model_minmax = minmax(model_values)
    ref_minmax = minmax(reference_values)
    stats = dict()
    stats['model_name'] = model_name
    stats['ref_name'] = ref_name
    stats['unit'] = unit
    stats['rmse'] = rmse(reference_values, model_values)
    stats['corrcoeff'] = correlation(reference_values, model_values)
    stats['unbiased_rmse'] = unbiased_rmse(reference_values, model_values)
    stats['normalised_rmse'] = normalised_rmse(reference_values, model_values, config.ddof, stats['corrcoeff'])
    stats['pbias'] = percentage_model_bias(reference_values, model_values)
    stats['bias'] = bias(reference_values, model_values)
    stats['reliability_index'] = reliability_index(reference_values, model_values)
    stats['model_efficiency'] = model_efficiency(reference_values, model_values)
    stats['mean'] = mean(model_values)
    stats['ref_mean'] = mean(reference_values)
    stats['stddev'] = stddev(model_values, config.ddof)
    stats['ref_stddev'] = stddev(reference_values, config.ddof)
    stats['normalised_stddev'] = stats['stddev'] / stats['ref_stddev']
    stats['median'] = model_percentiles[0]
    stats['ref_median'] = ref_percentiles[0]
    stats['p90'] = model_percentiles[1]
    stats['ref_p90'] = ref_percentiles[1]
    stats['p95'] = model_percentiles[2]
    stats['ref_p95'] = ref_percentiles[2]
    stats['min'] = model_minmax[0]
    stats['ref_min'] = ref_minmax[0]
    stats['max'] = model_minmax[1]
    stats['ref_max'] = ref_minmax[1]
    return stats
Exemplo n.º 8
0
def create_target_diagram(statistics, config=None):
    figure = plt.figure()
    if config is None:
        config = get_default_config()

    statistics = utils.ensure_list(statistics)

    diagram = TargetDiagram(figure, config.normalise_target_diagram, config.show_legends, config.utilise_stddev_difference)

    diagram.setup_axes()
    for stats in statistics:
        diagram.plot_sample(stats['bias'], stats['unbiased_rmse'], stats['normalised_rmse'], stats['rmse'],
            stats['ref_stddev'], stats['stddev'], create_sample_name(stats['model_name'], stats['unit']))

    if config.normalise_target_diagram:
        diagram.plot_correcoeff_marker_line()

    diagram.update_legend()
    if not config.normalise_target_diagram:
        diagram.update_ranges(config.target_diagram_bounds)

    return diagram
Exemplo n.º 9
0
def create_taylor_diagrams(statistics, config=None):
    if config is None:
        config = get_default_config()

    statistics = utils.ensure_list(statistics)

    statistics_by_name_and_unit = sort_statistics_by_name_and_unit(config, statistics)
    diagrams = []

    for name in statistics_by_name_and_unit.keys():
        for current_unit in statistics_by_name_and_unit[name].keys():
            current_statistics = statistics_by_name_and_unit[name][current_unit]

            ref_names = list(map(lambda x: x.get('ref_name'), current_statistics))
            ref_stddevs = list(map(lambda x: x.get('ref_stddev'), current_statistics)) if config.use_absolute_standard_deviation else list(np.ones(len(current_statistics)))
            units = list(map(lambda x: x.get('unit'), current_statistics))
            ref = tuple(zip(ref_names, ref_stddevs, units))
            model_stddevs = list(map(lambda x: x.get('stddev'), current_statistics)) if config.use_absolute_standard_deviation else list(map(lambda x: x.get('normalised_stddev'), current_statistics))
            max_stddev = max(ref_stddevs + model_stddevs) * 1.5

            for v in ref_stddevs:
                if v == 0.0 or np.isnan(v):
                    logging.warning('Unable to create Taylor diagram from statistics.')
                    logging.debug('Statistics: %s' % current_statistics)
                    continue

            figure = plt.figure()
            diagram = TaylorDiagram(figure, ref, config.show_negative_corrcoeff, config.use_absolute_standard_deviation, config.show_legends, max_stddev)

            diagram.setup_axes()
            for stats in current_statistics:
                model_name = stats['model_name'] if 'model_name' in stats else None
                stddev = stats['stddev'] if config.use_absolute_standard_deviation else stats['normalised_stddev']
                diagram.plot_sample(stats['corrcoeff'], stddev, model_name, stats['unit'])

            diagram.update_legend()
            diagrams.append(diagram)

    return diagrams
Exemplo n.º 10
0
def calculate_statistics(model_name, ref_name, data, config=None):
    """
    Calculates the statistics for the given model and reference variables located in the data file. Calculation will be
    performed according to the provided configuration.
    @param model_name: the name of the model variable.
    @param ref_name: the name of the reference variable.
    @param data: the input data object.
    @param config: the optional configuration.
    @return: a dictionary of statistics.
    """

    if config is None:
        config = get_default_config()

    is_gridded = len(data.get_reference_dimensions(ref_name)) > 1
    if is_gridded:
        reference_values, model_values = data.get_values(ref_name, model_name)
        unit = data.unit(model_name)
        return processor.calculate_statistics(model_values, reference_values,
                                              model_name, ref_name, unit,
                                              config)

    me = MatchupEngine(data, config)
    matchups = me.find_all_matchups()
    if config.remove_empty_matchups:
        matchups = me.remove_empty_matchups(matchups)
    if len(matchups) == 0:
        print("No matchups found; maybe allow higher maximum time delta.")
        return
    unit = data.unit(model_name)
    return calculate_statistics_from_matchups(matchups,
                                              model_name,
                                              ref_name,
                                              data,
                                              unit,
                                              config=None)
Exemplo n.º 11
0
 def __init__(self, data, configuration=None):
     self.data = data
     self.config = configuration if configuration is not None else get_default_config(
     )
     self.pixel_sizes = {}
Exemplo n.º 12
0
 def __init__(self, data, configuration=None):
     self.data = data
     self.config = configuration if configuration is not None else get_default_config()
     self.pixel_sizes = {}