Commit 66a77ddf authored by Niklas Selke's avatar Niklas Selke
Browse files

The 'check_statistics' function now raises a 'ValueError' if there is any...

The 'check_statistics' function now raises a 'ValueError' if there is any problem with the given 'statistics' argument.
parent f1732d03
......@@ -15,8 +15,7 @@ from collections import namedtuple
import numpy as np
import pandas as pd
from toarstats import ozone_metrics
from toarstats import stats
from toarstats import ozone_metrics, stats
from toarstats.stats_utils import CLIMATIC_ZONE, RSTAGS, SEASON_DICT
......@@ -30,7 +29,7 @@ def check_sampling(sampling_in):
:param sampling_in: the given sampling argument
:raises ValueError: raised if the given sampling argument is not a
string or is not recognized
string or if it is not recognized
:return: The given sampling argument with any leading and trailing
whitespace removed
......@@ -49,26 +48,37 @@ def check_statistics(statistics_in):
:param statistics_in: the given statistics argument
:return: The processed statistics argument or None if there was a
problem while processing the given statistics argument
:raises ValueError: raised if the given statistics argument is not a
type of list or a string or if no statistic is
given or if any given statistic is not
recognized or if the ``diurnal_cycle`` statistic
is not given alone
:return: A dictionary of statistics to calculate matched to their
respective function
if not isinstance(statistics_in, (list, str)):
return None
statistics_in_list = (
[statistics_in] if isinstance(statistics_in, str) else statistics_in
statistics_out = []
if not isinstance(statistics_in, (list, set, tuple, str)):
raise ValueError("The statistics argument must be a list, set, tuple"
" or string")
if not statistics_in:
raise ValueError("No statistics given")
statistics_in_list = ([statistics_in]
if isinstance(statistics_in, str)
else list(statistics_in))
statistics_out = {}
for stat in statistics_in_list:
if not isinstance(stat, str):
raise ValueError("Statistics must be given as strings")
stat = stat.strip()
if stat in statistics_out:
func = getattr(stats, stat, getattr(ozone_metrics, stat, None))
if func is not None and func not in statistics_out:
if not statistics_out:
return None
if stats.diurnal_cycle in statistics_out and len(statistics_out) > 1:
return None
if func is not None:
statistics_out[stat] = func
raise ValueError(f"{stat} is not a recognized statistic")
if "diurnal_cycle" in statistics_out and len(statistics_out) > 1:
raise ValueError("Diurnal cycle can only be evaluated alone")
return statistics_out
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment