"""Named evaluation recipes, common-sample comparisons and portable results."""
import base64
from copy import deepcopy
from dataclasses import dataclass
from datetime import datetime
from html import escape
from importlib.resources import files
from io import BytesIO
import json
import logging
from pathlib import Path
from string import Template
import tomllib
import numpy as np
import pandas as pd
import xarray as xr
from matplotlib.figure import Figure
from matplotlib import colormaps
from openMWR.config import (check_keys, check_name, check_strings, require_keys,
check_time_selection, select_time, TIME_SELECTION_KEYS,
check_height_range, select_height_range)
from openMWR.calc_errors import cal_RMSE, calculate_errors
from openMWR.devices import resolve_device, device_name
from openMWR.models import load_model
from openMWR.observations import load_analysis_dataset, reference_source
from openMWR.site import load_site_config
from openMWR.paths import (project_root, site_config_file, dataset_file, checkpoint_file,
dataset_recipe_file, evaluation_recipe_file)
from openMWR.plot import plot_scatter_lwp
from openMWR.predict import run_model_list, validate_model_data
from openMWR.provenance import checkpoint_id, plain_value, software_identity, utc_now
from openMWR.xr_utils import load_combined_dataset
logger = logging.getLogger(__name__)
MODE_TITLES = {'zenith': 'Zenith', 'bls': 'Boundary layer scan'}
VARIABLE_LABELS = {
'T': 'Temperature [K]', 'rh': 'Relative humidity [%]',
'lwc': 'Liquid water content [g/m³]', 'lwp': 'Liquid water path [g/m²]',
'iwv': 'Integrated water vapour [kg/m²]', 'TB': 'Brightness temperature [K]',
}
[docs]
@dataclass
class EvaluationRecipe:
"""Editable comparison settings. Both the command and Python use this object."""
project_dir: Path
site: str
name: str
values: dict
[docs]
@classmethod
def load(cls, project_dir, site, name):
check_name(site, 'site')
check_name(name, 'evaluation')
root = project_root(project_dir)
if not site_config_file(root, site).is_file():
raise FileNotFoundError(f'Missing site configuration: {site_config_file(root, site)}')
path = evaluation_recipe_file(root, site, name)
with path.open('rb') as stream:
recipe = cls(root, site, name, tomllib.load(stream))
recipe.validate()
return recipe
[docs]
def validate(self):
check_name(self.site, 'site')
check_name(self.name, 'evaluation')
check_keys(self.values, ('schema_version', 'retrievals', 'bls_baseline', 'observations', 'test'), 'evaluation recipe')
if type(self.values.get('schema_version')) is not int or self.values['schema_version'] != 1:
raise ValueError('Evaluation schema_version must be 1.')
check_strings(self.values.get('retrievals'), 'retrievals')
if not self.values['retrievals']:
raise ValueError('Select at least one retrieval.')
for name in self.values['retrievals']:
check_name(name, 'retrieval')
if 'bls_baseline' in self.values:
check_name(self.values['bls_baseline'], 'bls_baseline')
if self.values['bls_baseline'] not in self.values['retrievals']:
raise ValueError('bls_baseline must name a retrieval in retrievals.')
if not {'observations', 'test'} & self.values.keys():
raise ValueError('Configure [observations], [test], or both.')
for section in ('observations', 'test'):
if section not in self.values:
continue
settings = self.values[section]
allowed = ('data_source', 'station_id', 'include_rpg', *TIME_SELECTION_KEYS) if section == 'observations' else ('sources',)
check_keys(settings, ('dataset', 'variables', 'height_range', *allowed), section)
require_keys(settings, ('dataset', 'variables'), section)
check_name(settings['dataset'], f'{section}.dataset')
check_strings(settings['variables'], f'{section}.variables')
if not settings['variables'] or set(settings['variables']) - VARIABLE_LABELS.keys():
raise ValueError(f'{section}.variables must select from {list(VARIABLE_LABELS)}.')
if 'TB' in settings['variables'] and len(settings['variables']) != 1:
raise ValueError('Evaluate TB separately from profile and column variables.')
if set(settings['variables']) & {'T', 'rh', 'lwc'}:
require_keys(settings, ('height_range',), section)
check_height_range(settings['height_range'], f'{section}.height_range', allow_model=True)
elif 'height_range' in settings:
raise ValueError(f'{section}.height_range applies only to profile variables.')
if section == 'observations':
if 'data_source' in settings and settings['data_source'] not in ('radiosonde', 'era5'):
raise ValueError('observations.data_source must be radiosonde or era5.')
check_time_selection(settings, section)
if 'station_id' in settings:
check_name(settings['station_id'], 'observations.station_id')
if settings.get('data_source') not in (None, 'radiosonde'):
raise ValueError('observations.station_id applies only to radiosondes.')
if 'include_rpg' in settings and type(settings['include_rpg']) is not bool:
raise ValueError('observations.include_rpg must be true or false.')
if settings.get('include_rpg') and 'TB' in settings['variables']:
raise ValueError('RPG comparison applies to profile retrievals.')
else:
if 'sources' in settings:
check_strings(settings['sources'], 'test.sources')
if not settings['sources']:
raise ValueError('Select at least one test source.')
for source in settings['sources']:
check_name(source, 'test.sources')
return self
[docs]
@dataclass
class Evaluation:
key: str
mode: str
title: str
error_types: tuple[str, ...]
samples: dict
metrics: dict[str, xr.Dataset]
note: str = ''
[docs]
@dataclass
class Analysis:
metadata: dict
evaluations: list[Evaluation]
def _sample_summary(ds):
variable = 'T' if 'T' in ds else next(iter(ds.data_vars))
finite = np.isfinite(ds[variable])
for dim in ('height', 'frq'):
if dim in finite.dims:
finite = finite.any(dim)
return dict(time_coordinates=ds.sizes['time'], reference_profiles=int(finite.sum()),
stations=ds.station.values.tolist() if 'station' in ds.coords else ds.attrs.get('analysis_stations', []),
start=str(ds.time.min().values), end=str(ds.time.max().values))
def _paired_profiles(pred, reference, variables):
counts = {}
for var in variables:
valid = np.isfinite(pred[var]) & np.isfinite(reference[var])
for dim in ('height', 'frq'):
if dim in valid.dims:
valid = valid.any(dim)
counts[var] = valid.sum([dim for dim in ('time', 'station') if dim in valid.dims])
counts[var].attrs = {'units': '1'}
return xr.Dataset(counts)
def _select_station(data, station_id, path):
if 'station' not in data.dims:
raise ValueError(f'Missing radiosonde station dimension in {path}.')
available = [str(value) for value in data.station.values]
if len(set(available)) != len(available):
raise ValueError(f'Duplicate station IDs in {path}.')
if station_id is None:
if len(available) != 1:
raise ValueError(f'Select observations.station_id. Available stations in {path}: {available}.')
station_id = available[0]
if station_id not in available:
raise ValueError(f'Station ID {station_id!r} is unavailable in {path}. Available stations: {available}.')
selected = data.assign_coords(station=available).sel(station=station_id, drop=True)
selected.attrs['analysis_stations'] = [station_id]
return selected
def _observation_note(settings):
days = settings.get('days_of_month')
note = (f"Observation days {days['first']}-{days['last']} of each month (inclusive)."
if days else 'All observation days.')
if 'start_date' in settings or 'end_date' in settings:
note += f" Date range: {settings.get('start_date', 'first available')} through {settings.get('end_date', 'last available')} (inclusive)."
return note
def _reference_grid(models, reference, variables, settings):
"""Select one explicit grid. Different model grids require another height range."""
grid = None
for model in models:
missing = set(variables) - set(model.output)
if missing:
raise ValueError(f'{model.name} does not predict requested variables {sorted(missing)}.')
output = select_height_range(model.output[variables], settings.get('height_range', 'model'))
coordinates = {name: value for name, value in output.coords.items() if name in output.dims}
if grid is None:
grid = coordinates
elif grid.keys() != coordinates.keys() or any(not np.array_equal(grid[key], coordinates[key]) for key in grid):
raise ValueError('Retrieval output grids differ. Choose a shared height range or evaluate the grids separately.')
reference = select_height_range(reference[variables], settings.get('height_range', 'model'))
try:
reference = reference.sel(grid)
except KeyError as exc:
raise ValueError('Reference coordinates do not cover the selected model grid. Prepare matching reference data.') from exc
return reference.reset_coords(drop=True)
def _check_references(reference, other):
try:
xr.merge([reference, other], join='outer', compat='no_conflicts')
except xr.MergeError as exc:
raise ValueError('Zenith and scan analysis files disagree on reference values. Prepare consistent analysis datasets.') from exc
def _rpg_predictions(mode, settings, models, observations, reference):
"""Prepare vendor products on the comparison grid with consistent references."""
predictions = {}
variables = settings['variables']
for product_mode in (('zenith', 'bls') if mode == 'bls' else ('zenith',)):
name = 'RPG_BLS' if product_mode == 'bls' else 'RPG'
data = observations[product_mode]
product = data.sel(data_source='hatpro', drop=True)
available = ((set(variables) | {'lwp'}) & set(product)) - ({'rh', 'lwc', 'lwp', 'iwv'} if product_mode == 'bls' else set())
if not set(variables) & available:
continue
if product_mode != mode:
other = _reference_grid(models, data.sel(data_source=settings['data_source'], drop=True), variables, settings)
_check_references(reference, other)
pred = product[sorted(available)].sel({dim: reference[dim] for dim in ('height', 'frq') if dim in reference.dims}).reset_coords(drop=True)
predictions[name] = pred.expand_dims(model=[name])
if not predictions:
raise ValueError(f'No requested RPG outputs in {settings["dataset"]}. Disable observations.include_rpg.')
return predictions
def _predict_cases(recipe, section, settings, groups, device, inputs, site_config):
variables = settings['variables']
logger.info('%s: dataset %s, variables %s.', section, settings['dataset'], ', '.join(variables))
cases = {}
test_data = None
observations = {}
if section == 'observations':
for mode, group in groups.items():
# Scan comparisons also need zenith RPG, even without a zenith model.
if not group and not (mode == 'zenith' and settings.get('include_rpg')):
continue
path = dataset_file(recipe.project_dir, recipe.site, settings['dataset'], bls=mode == 'bls')
inputs[path] = checkpoint_id(path)
data = load_analysis_dataset(recipe.project_dir, recipe.site, settings['dataset'], bls=mode == 'bls', site_config=site_config)
settings['data_source'] = reference_source(data, settings.get('data_source'))
if 'station_id' in settings and settings['data_source'] != 'radiosonde':
raise ValueError('observations.station_id applies only to radiosondes.')
data = select_time(data, **{key: settings[key] for key in TIME_SELECTION_KEYS if key in settings})
if settings['data_source'] == 'radiosonde':
data = _select_station(data, settings.get('station_id'), path)
if mode == 'zenith' and 'ang' in data.dims:
data = data.sel(ang=90, drop=True)
observations[mode] = data
else:
if 'sources' not in settings:
from openMWR.datasets import DatasetWorkflow
datasets = DatasetWorkflow(recipe.project_dir, recipe.site, site_config=site_config)
if datasets.recipe(settings['dataset']).kind != 'split':
raise ValueError('test.dataset must select a split dataset.')
settings['sources'] = datasets.sources(settings['dataset'])
for source in settings['sources']:
path = dataset_file(recipe.project_dir, recipe.site, settings['dataset'], source, split='test')
inputs[path] = checkpoint_id(path)
test_data = load_combined_dataset(recipe.site, recipe.project_dir, 'test', settings['sources'], dataset=settings['dataset'])
for mode, group in groups.items():
if not group:
continue
bls = mode == 'bls'
predictions, supported = [], {}
if section == 'observations':
data = observations[mode]
measured = data.sel(data_source='hatpro', drop=True)
reference = data.sel(data_source=settings['data_source'], drop=True)
else:
measured = reference = test_data
if not bls and 'ang' in measured.dims:
measured = measured.sel(ang=90, drop=True)
if not bls and 'ang' in reference.dims:
reference = reference.sel(ang=90, drop=True)
if not reference.sizes.get('time', 0):
raise ValueError(f'No {section} samples remain after selection.')
reference = _reference_grid(group, reference, variables, settings)
for model in group:
logger.info('Predicting %s for %s.', model.name, section)
outputs = list(variables)
if section == 'observations' and 'lwp' in model.output and 'lwp' not in outputs:
outputs.append('lwp')
valid = validate_model_data(model.input, measured, f'{section} inputs for {model.name}', require_samples=False)
if bool(valid.any()):
pred = run_model_list([model], measured, device, include_input_vars=False)
else:
pred = xr.full_like(model.output[outputs], np.nan, dtype=float).expand_dims(
model=[model.name], **{dim: reference[dim] for dim in ('time', 'station') if dim in reference.dims})
pred = pred[outputs].sel({dim: reference[dim] for dim in ('height', 'frq') if dim in reference.dims})
predictions.append(pred.reset_coords(drop=True))
supported[model.name] = set(outputs)
if section == 'observations' and settings.get('include_rpg'):
for name, pred in _rpg_predictions(mode, settings, group, observations, reference).items():
predictions.append(pred)
supported[name] = set(pred.data_vars)
cases[mode] = (reference, xr.concat(predictions, dim='model', join='outer'), supported)
return cases
def _score_case(mode, section, settings, pred, reference, supported):
"""Apply the same common-pair selection and metrics to each comparison group."""
variables = settings['variables']
# Retain the full reference cohort, including cases a model could not process.
pred = pred.reindex({dim: reference[dim] for dim in ('time', 'station') if dim in reference.dims})
available = _paired_profiles(pred, reference, variables)
scored = pred.copy()
truth = reference.copy()
for variable in variables:
participating = [name for name, outputs in supported.items() if variable in outputs]
common = np.isfinite(reference[variable]) & np.isfinite(pred[variable].sel(model=participating)).all('model')
scored[variable] = pred[variable].where(common)
truth[variable] = reference[variable].where(common)
error_types = ('RMSE', 'MAE', 'ME') if section == 'observations' else ('RMSE', 'ME')
errors, profiles = calculate_errors(scored, truth, error_types=error_types, variables=variables)
metrics = dict(errors=errors, error_profiles=profiles, available_profiles=available,
paired_profiles=_paired_profiles(scored, truth, variables))
if 'T' in variables:
metrics['rmse_time'] = cal_RMSE(scored[['T']], truth[['T']], dims_to_mean='height')
metrics['monthly_temperature_rmse'] = metrics['rmse_time'].resample(time='1MS').mean()
if 'height' in profiles.dims:
metrics['mame'] = np.abs(profiles.sel(error='ME', drop=True)).mean('height')
if 'lwp' in variables:
metrics['lwp_scatter'] = xr.Dataset({'predicted_lwp': scored.lwp, 'reference_lwp': truth.lwp})
if section == 'observations' and 'lwp' in pred:
names = [name for name, outputs in supported.items() if 'lwp' in outputs]
lwp = pred.lwp.sel(model=names)
lwp = lwp.where(np.isfinite(lwp).all('model'))
dimensions = [dim for dim in ('time', 'station') if dim in lwp.dims]
mean = lwp.mean(dimensions)
mean.attrs = {'units': 'g/m²'}
metrics['mean_lwp'] = xr.Dataset({'lwp': mean, 'profiles': lwp.count(dimensions)})
if section == 'observations' and 'RPG' in supported and profiles.data_vars:
rpg_variables = [name for name in profiles if name in supported['RPG']]
rpg_profiles = profiles[rpg_variables]
rpg = rpg_profiles.sel(model='RPG')
relative = ((rpg_profiles - rpg) / rpg.where(rpg != 0) * 100).sel(error='RMSE', drop=True)
for values in relative.values():
values.attrs = {'units': '%'}
metrics.update(relative_rmse_profiles=relative, relative_rmse=relative.mean('height'))
# Dataset attributes from source files belong in run metadata, not metrics.
for values in metrics.values():
values.attrs = {}
for key in ('relative_rmse', 'relative_rmse_profiles'):
if key in metrics:
metrics[key].attrs['reference'] = 'zenith RPG'
note = (_observation_note(settings) if section == 'observations' else f"Explicit test dataset: {settings['dataset']}. Sources: {', '.join(settings['sources'])}.")
note += ' Scores use common finite reference/prediction pairs within this comparison group, separately for each variable and level. No reference interpolation is performed.'
title = f"{MODE_TITLES[mode]} · {'Observations' if section == 'observations' else 'Simulated test data'}"
return Evaluation(f'{mode}_{section}', mode, title, error_types, _sample_summary(reference), metrics, note)
def _model_notes(model, recipe):
metadata = model.training_metadata or {}
setup = metadata.get('setup', {})
notes = []
if 'test' in recipe.values:
notes.append(f"Checkpoint training dataset: {model.training_dataset}. Evaluation test dataset: {recipe.values['test']['dataset']}.")
if not setup.get('training_selection', model.hyper_params.get('training_selection')):
notes.append('The checkpoint has no recorded calendar training selection. Independence of these test data is not established.')
return notes
[docs]
def evaluate(recipe: EvaluationRecipe, *, retrievals=None, device='auto'):
"""Evaluate a named comparison without training, downloading or saving results."""
site_config = load_site_config(recipe.site, recipe.project_dir)
settings = deepcopy(recipe.values)
if retrievals is not None:
settings['retrievals'] = list(retrievals)
if settings.get('bls_baseline') not in settings['retrievals']:
settings.pop('bls_baseline', None)
recipe = EvaluationRecipe(project_root(recipe.project_dir), recipe.site, recipe.name, settings).validate()
started = utc_now()
device = resolve_device(device)
hashes = {name: checkpoint_id(checkpoint_file(recipe.project_dir, recipe.site, name)) for name in settings['retrievals']}
models = [load_model(name, recipe.site, recipe.project_dir) for name in settings['retrievals']]
if len({'TB' in model.output for model in models}) > 1:
raise ValueError('Evaluate spectral and profile retrievals in separate recipes.')
groups = {mode: [model for model in models if model.is_bls_model == (mode == 'bls')]
for mode in MODE_TITLES}
baseline_name = settings.get('bls_baseline')
baseline = next((model for model in models if model.name == baseline_name), None)
if baseline is not None and (baseline.is_bls_model or 'TB' in baseline.output):
raise ValueError('bls_baseline must be a zenith profile retrieval.')
if not groups['bls']:
settings.pop('bls_baseline', None)
baseline = None
configuration = [site_config_file(recipe.project_dir, recipe.site)]
configuration += [dataset_recipe_file(recipe.project_dir, recipe.site, settings[key]['dataset'])
for key in ('observations', 'test') if key in settings]
inputs = {path: checkpoint_id(path) for path in configuration if path.is_file()}
notes = {model.name: _model_notes(model, recipe) for model in models}
logger.info('Evaluating %s with retrievals %s.', recipe.name, ', '.join(settings['retrievals']))
evaluations = {mode: [] for mode in MODE_TITLES}
for section in ('observations', 'test'):
if section not in settings:
continue
selection = settings[section]
cases = _predict_cases(recipe, section, selection, groups, device, inputs, site_config)
for mode, group in groups.items():
if not group:
continue
reference, predicted, supported = cases[mode]
compared = group
if mode == 'bls' and baseline is not None:
compared = [baseline, *group]
reference = _reference_grid(compared, reference, selection['variables'], selection)
zenith_reference, zenith_predictions, zenith_supported = cases['zenith']
_check_references(reference, zenith_reference)
baseline_rows = [baseline.name]
predicted = xr.concat([zenith_predictions.sel(model=baseline_rows), predicted], dim='model', join='outer')
supported = {**{name: zenith_supported[name] for name in baseline_rows}, **supported}
if section == 'observations':
for model in compared:
optimization = (model.training_metadata or {}).get('setup', {}).get('optimization')
if optimization:
dates = {key: optimization[key] for key in TIME_SELECTION_KEYS if key in optimization}
if isinstance(dates.get('days_of_month'), list):
dates['days_of_month'] = dict(zip(('first', 'last'), dates['days_of_month']))
overlap = bool(select_time(reference, **dates).sizes['time'])
notes[model.name].append(
f"{MODE_TITLES[mode]} observations: optimized against {optimization.get('data_source', 'an unrecorded reference')}. "
f"Optimization selection: {_observation_note(dates)} "
+ ('Selected evaluation dates overlap this selection.' if overlap else 'Selected evaluation dates are outside this selection.'))
evaluations[mode].append(_score_case(mode, section, selection, predicted, reference, supported))
evaluations = [case for group in evaluations.values() for case in group]
metadata = dict(site=recipe.site, evaluation=recipe.name, project_dir=str(recipe.project_dir),
started_utc=started, finished_utc=utc_now(), settings=plain_value(settings),
device=device, device_name=device_name(device), code=software_identity(),
inputs=[dict(path=str(path.relative_to(recipe.project_dir)), sha256=digest) for path, digest in sorted(inputs.items())],
models=[dict(name=model.name, mode='Boundary layer scan' if model.is_bls_model else 'Zenith',
checkpoint_sha256=hashes[model.name],
training_metadata=model.training_metadata, notes=notes[model.name]) for model in models])
for evaluation in evaluations:
for name in evaluation.metrics['errors'].model.values:
if name not in {model['name'] for model in metadata['models']}:
metadata['models'].append(dict(name=str(name), mode='RPG product', notes=[]))
logger.info('Finished evaluation %s.', recipe.name)
return Analysis(metadata, evaluations)
[docs]
def save_evaluation(analysis, output_file=None):
"""Save one standalone HTML report, preserving existing files.
Numerical results remain available on ``analysis.evaluations`` for custom
Python work. No numerical export or report subdirectory is created.
"""
meta = analysis.metadata
if output_file is None:
stamp = datetime.fromisoformat(meta['started_utc']).strftime('%Y-%m-%d_%H-%M-%S')
recipe = evaluation_recipe_file(meta['project_dir'], meta['site'], meta['evaluation'])
output_file = recipe.with_name(f'{meta["evaluation"]}_{stamp}.html')
output_file = Path(output_file).resolve()
if output_file.exists():
raise FileExistsError(f'Evaluation output already exists: {output_file}. Choose a new filename.')
document = render_report(analysis)
output_file.parent.mkdir(parents=True, exist_ok=True)
with output_file.open('x', encoding='utf-8') as stream:
stream.write(document)
logger.info('Evaluation saved to %s', output_file)
return output_file
def _table(ds, labels=VARIABLE_LABELS):
frame = ds.to_dataframe()[list(ds.data_vars)].rename(columns=labels)
return frame.to_html(
float_format=lambda value: f"{value:.4g}", na_rep="—", border=0
)
def _profile_figure(ds, metric, colors):
variables = [name for name, values in ds.items() if {'height', 'frq'} & set(values.dims)]
if not variables:
return None
figure = Figure(figsize=(6 * len(variables), 5))
axes = np.atleast_1d(figure.subplots(1, len(variables)))
for ax, variable in zip(axes, variables):
values = ds[variable]
for model in values.model.values:
selected = values.sel(model=model)
if 'height' in selected.dims:
ax.plot(selected, selected.height / 1000, label=str(model), color=colors[str(model)])
else:
ax.plot(selected.frq, selected, label=str(model), color=colors[str(model)])
if 'height' in values.dims:
ax.set_xlabel(f'{metric} [%]' if metric == 'Relative RMSE' else f'{metric}: {VARIABLE_LABELS.get(variable, variable)}')
ax.set_ylabel('Height above instrument [km]')
if metric in ('RMSE', 'MAE'):
ax.set_xlim(left=0)
else:
ax.axvline(0, color='black', linewidth=0.8)
else:
ax.set_xlabel('Frequency [GHz]')
ax.set_ylabel(f'{metric}: {VARIABLE_LABELS.get(variable, variable)}')
if metric in ('RMSE', 'MAE'):
ax.set_ylim(bottom=0)
else:
ax.axhline(0, color='black', linewidth=0.8)
ax.legend()
ax.grid()
return figure
def _scatter_figure(ds, model):
figure = Figure(figsize=(10, 5))
predicted = ds[["predicted_lwp"]].rename(predicted_lwp="lwp").sel(model=model)
reference = ds[["reference_lwp"]].rename(reference_lwp="lwp")
if 'station' not in ds.dims:
predicted = predicted.expand_dims(station=['site'])
reference = reference.expand_dims(station=['site'])
plot_scatter_lwp(predicted, reference, figure=figure, show=False)
for ax in figure.axes:
ax.set_xlabel('Reference LWP [g/m²]')
return figure
def _figure_html(figure, caption):
stream = BytesIO()
figure.savefig(stream, format="svg", bbox_inches="tight")
encoded = base64.b64encode(stream.getvalue()).decode("ascii")
return (
f'<figure><img alt="{escape(caption, quote=True)}" src="data:image/svg+xml;base64,{encoded}">'
f"<figcaption>{escape(caption)}</figcaption></figure>"
)
def _samples_html(samples):
stations = ", ".join(map(str, samples["stations"])) or "Single site"
start, end = [
np.datetime_as_string(np.datetime64(samples[key]), unit="m").replace("T", " ")
for key in ("start", "end")
]
return (
f'<p class="samples">Period: {escape(start)} to {escape(end)}. '
f"Time coordinates: {samples['time_coordinates']}. Reference profiles with finite values: "
f"{samples['reference_profiles']}. Stations: {escape(stations)}.</p>"
)
[docs]
def render_report(analysis):
"""Build the complete HTML in memory, including its figures."""
meta = analysis.metadata
# Use the same model colors throughout comparisons and individual sections.
model_names = list(
dict.fromkeys(
str(model)
for evaluation in analysis.evaluations
for model in evaluation.metrics["errors"].model.values
)
)
palette = colormaps["tab10"].colors
colors = {name: palette[i % len(palette)] for i, name in enumerate(model_names)}
toc = [(evaluation.key, evaluation.title) for evaluation in analysis.evaluations]
toc += [(f"retrieval-{i}", model["name"]) for i, model in enumerate(meta["models"])]
parts = [
'<nav aria-label="Contents"><h2>Contents</h2><ul>'
+ "".join(
f'<li><a href="#{escape(key, quote=True)}">{escape(title)}</a></li>'
for key, title in toc
)
+ '<li><a href="#provenance">Run details</a></li></ul></nav>'
]
parts.append(
"<p>ME is prediction minus reference. MAME is the mean absolute vertical "
"mean error. Summary errors for profile variables aggregate over time, stations "
"(where present) and height. Vertical error profiles aggregate over time and "
"stations. Zenith and scan retrievals are scored separately. Within each group, scores use the same finite pairs for all participating retrievals in each variable and level. No common pairs means an unavailable score.</p>"
)
current_mode = None
for evaluation in analysis.evaluations:
if evaluation.mode != current_mode:
if current_mode is not None:
parts.append('</section>')
current_mode = evaluation.mode
parts.append(f'<section id="mode-{current_mode}"><h2>{MODE_TITLES[current_mode]} retrievals</h2>')
baseline = meta['settings'].get('bls_baseline')
if current_mode == 'bls' and baseline:
parts.append(f'<p>Zenith baseline: {escape(baseline)}. Its scores are recalculated on this group’s common valid cases and can differ from its zenith results.</p>')
case_title = evaluation.title.split(' · ', 1)[1]
parts.append(f'<section id="{evaluation.key}"><h3>{escape(case_title)}</h3>')
parts.append(_samples_html(evaluation.samples))
if evaluation.note:
parts.append(f"<p>{escape(evaluation.note)}</p>")
parts.append(
"<details><summary>Coverage and common profiles per retrieval and variable</summary>"
"<p>A profile counts when prediction and reference have at least one finite paired "
"height. LWP counts finite paired column values. Missing results appear as —.</p>"
+ "<h4>Available before common-sample selection</h4>"
+ _table(evaluation.metrics["available_profiles"], labels={})
+ "<h4>Used for scores</h4>"
+ _table(evaluation.metrics["paired_profiles"], labels={})
+ "</details>"
)
if "mean_lwp" in evaluation.metrics:
parts.append(
"<h4>Mean retrieved LWP</h4>"
"<p>Mean over common finite LWP predictions in this group. This is a diagnostic, "
"without an LWP reference.</p>"
+ _table(evaluation.metrics["mean_lwp"], labels={**VARIABLE_LABELS, 'profiles': 'Profiles'})
)
for metric in evaluation.error_types:
parts.append(
f"<h4>{metric}</h4>"
+ _table(evaluation.metrics["errors"].sel(error=metric, drop=True))
)
profile_errors = evaluation.metrics["error_profiles"]
if not profile_errors.data_vars:
profile_errors = evaluation.metrics['errors']
profiles = profile_errors.sel(error=metric, drop=True)
figure = _profile_figure(profiles, metric, colors)
if figure is not None:
parts.append(_figure_html(figure, f'{evaluation.title}: {metric}'))
if metric == "ME" and "mame" in evaluation.metrics:
parts.append("<h5>MAME</h5>" + _table(evaluation.metrics["mame"]))
if "relative_rmse" in evaluation.metrics:
rpg_reference = evaluation.metrics['relative_rmse'].attrs.get('reference')
if not rpg_reference:
raise ValueError(f'Missing relative RMSE reference for {evaluation.key}. Rebuild the evaluation.')
parts.append(
f"<h4>Relative RMSE to {escape(rpg_reference)} [%]</h4><p>The reference is scored "
"on this group's common pairs. Differences are calculated at each height, "
f"then averaged over height. Negative values indicate a smaller RMSE than {escape(rpg_reference)}. "
"A zero reference RMSE gives an undefined relative difference.</p>"
+ _table(evaluation.metrics["relative_rmse"], labels={})
)
parts.append(
_figure_html(
_profile_figure(
evaluation.metrics["relative_rmse_profiles"],
"Relative RMSE",
colors,
),
f"Relative RMSE to {rpg_reference} [%]",
)
)
if "monthly_temperature_rmse" in evaluation.metrics:
monthly = evaluation.metrics["monthly_temperature_rmse"]
fig = Figure(figsize=(10, 4))
ax = fig.subplots()
for name in monthly.model.values:
values = monthly.T.sel(model=name)
if 'station' in values.dims:
values = values.mean('station')
ax.plot(values.time.values, values.values, label=str(name), color=colors[str(name)])
ax.legend()
ax.set_ylabel("Temperature RMSE [K]")
ax.set_ylim(bottom=0)
ax.set_xlabel("Month")
ax.grid()
fig.autofmt_xdate()
parts.append(
"<h4>Monthly temperature RMSE</h4>"
+ _figure_html(
fig,
"Mean of per-profile temperature RMSE",
)
)
parts.append("</section>")
parts.append("</section><h2>Individual retrievals</h2>")
for i, model in enumerate(meta["models"]):
name = model["name"]
parts.append(
f'<section id="retrieval-{i}"><h3>{escape(name)}</h3><p>{escape(model["mode"])} retrieval.</p>'
)
for note in model.get('notes', []):
parts.append(f'<p>{escape(note)}</p>')
frames = {}
for evaluation in analysis.evaluations:
errors = evaluation.metrics["errors"]
if name in errors.model.values:
ds = errors.sel(model=name, drop=True).sel(
error=list(evaluation.error_types)
)
frames[evaluation.title] = ds.to_dataframe()[list(ds.data_vars)]
frame = pd.concat(frames, names=["Evaluation"]).rename(columns=VARIABLE_LABELS)
parts.append(
frame.to_html(
float_format=lambda value: f"{value:.4g}", na_rep="—", border=0
)
)
for evaluation in analysis.evaluations:
scatter = evaluation.metrics.get('lwp_scatter')
if scatter is not None and name in scatter.model.values:
parts.append(_figure_html(
_scatter_figure(scatter, name),
f"{evaluation.title}: full range and LWP zoom"))
details = json.dumps(model, indent=2, ensure_ascii=False)
parts.append(
f"<details><summary>Checkpoint identifier</summary><pre>{escape(details)}</pre></details></section>"
)
provenance = json.dumps(meta, indent=2, ensure_ascii=False)
parts.append(
f'<section id="provenance"><h2>Run details</h2><details><summary>Inputs, filters, checkpoints and code versions</summary>'
f"<pre>{escape(provenance)}</pre></details></section>"
)
observations = meta['settings'].get('observations')
test = meta['settings'].get('test')
filter_text = _observation_note(observations) if observations else 'No observation evaluation.'
for label, settings in (('Observation', observations), ('Test', test)):
if settings and 'height_range' in settings:
heights = settings['height_range']
selection = 'full model grid' if heights == 'model' else f'{heights[0]}-{heights[1]} m'
filter_text += f' {label} profile heights: {selection}.'
title = f"Retrieval evaluation · {meta['site']} · {meta['evaluation']}"
template = Template(files('openMWR').joinpath('templates', 'evaluation.html').read_text(encoding='utf-8'))
return template.substitute(
title=escape(title),
source=escape(observations['data_source'] if observations else 'Simulated test data'),
started=escape(meta["started_utc"]),
filters=filter_text,
stations=escape(', '.join(test['sources']) if test else 'No test evaluation'),
content="".join(parts),
)