def build_summary_stats_json(report): """Creates a dict from the summary statistics in the report. Parameters ---------- report: :py:class:`solarforecastarbiter.datamodel.Report` Returns ------- str The json representing the summary statistics. Will be a string representing an empty json array if the report does not have a computed raw_report. Raises ------ ValueError If report.raw_report is populated but no report.raw_report.metrics have `is_summary == True` indicating that the report was made without summary statistics. """ if getattr(report, 'raw_report') is not None: df = plotly_figures.construct_metrics_dataframe( list( filter(lambda x: getattr(x, 'is_summary', False), report.raw_report.metrics)), rename=plotly_figures.abbreviate) if df.empty: raise ValueError('No summary statistics in report.') return df.to_json(orient="records") else: return "[]"
def test_construct_metrics_dataframe_with_rename(report_with_raw): metrics = report_with_raw.raw_report.metrics df = figures.construct_metrics_dataframe(metrics, rename=figures.abbreviate) report_params = report_with_raw.report_parameters original_names = [ fxobs.forecast.name for fxobs in report_params.object_pairs ] abbreviated = list(map(figures.abbreviate, original_names)) assert np.all(df['abbrev'] == np.repeat( np.array(abbreviated, dtype=object), len(report_params.metrics) * len(report_params.categories)))
def build_metrics_json(report): """Creates a dict from the metrics results in the report. Parameters ---------- report: :py:class:`solarforecastarbiter.datamodel.Report` Returns ------- str The json representing the report metrics. The string will be a string representing an empty json array if the report does not have a computed raw_report. """ if getattr(report, 'raw_report') is not None: df = plotly_figures.construct_metrics_dataframe( report.raw_report.metrics, rename=plotly_figures.abbreviate) return df.to_json(orient="records") else: return "[]"
def test_construct_metrics_dataframe(report_with_raw): report = report_with_raw metrics = report.raw_report.metrics df = figures.construct_metrics_dataframe(metrics) names = df['name'] abbrev = df['abbrev'] categories = df['category'] metrics = df['metric'] values = df['value'] report_params = report.report_parameters expected_length = (len(report_params.metrics) * len(report_params.categories) * len(report_params.object_pairs)) assert all([len(v) == expected_length for k, v in df.items()]) original_names = [ fxobs.forecast.name for fxobs in report_params.object_pairs ] assert np.all(names == np.repeat( np.array(original_names), len(report_params.metrics) * len(report_params.categories))) assert np.all(names == abbrev) assert np.all(metrics == np.tile( np.repeat(np.array(report_params.metrics, dtype=object), len(report_params.categories)), len(report_params.object_pairs))) assert np.all(categories == np.tile( np.array(report_params.categories), len(report_params.metrics) * len(report_params.object_pairs))) # this could maybe use value variance, but asserting the dataframe process # did not mangle values for now assert (values == 2).all()
def test_construct_metric_dataframe_no_values(): # Iterative metrics datafame creation just builds an empty dataframe # with correct columns if no MetricResults are found in the metrics tuple df = figures.construct_metrics_dataframe(()) assert df['index'].size == 0 assert 'abbrev' in df