Commit 925a9a5d authored by Niklas Selke's avatar Niklas Selke
Browse files

Added a check for the 'data' argument in 'interface.py'. The check function is...

Added a check for the 'data' argument in 'interface.py'. The check function is included in 'input_checks.py'.
parent 7279a05b
......@@ -4,6 +4,9 @@ This module contains the following function:
check_sampling - check the given sampling argument
"""
import numpy as np
import pandas as pd
from toarstats import ozone_metrics
from toarstats import stats
from toarstats.stats_utils import RSTAGS
......@@ -53,3 +56,47 @@ def check_statistics(input_argument):
and len(statistics) > 1)):
return None
return statistics
def check_data(input_argument, alt_input_idx, alt_input_vals):
"""Check the given data argument.
:param input_argument: the given argument
:param alt_input_idx: the alternative input's index
:param alt_input_vals: the alternative input's values
:return: The processed arguments or None if there was a problem with
the input
"""
if ((input_argument is not None and not (alt_input_idx is None
and alt_input_vals is None))
or (input_argument is None and (alt_input_idx is None
or alt_input_vals is None))):
return None, None
if isinstance(input_argument, (pd.DataFrame, pd.Series)):
if not isinstance(input_argument.index, pd.DatetimeIndex):
return None, None
index = pd.DatetimeIndex(input_argument.index, copy=True)
if isinstance(input_argument, pd.Series):
values = input_argument.to_numpy(copy=True)
elif "value" in input_argument.columns:
values = input_argument["value"].to_numpy(copy=True)
elif "values" in input_argument.columns:
values = input_argument["values"].to_numpy(copy=True)
else:
values = input_argument.iloc[:, 0].to_numpy(copy=True)
elif (isinstance(alt_input_idx, pd.DatetimeIndex)
and isinstance(alt_input_vals, np.ndarray)):
index = pd.DatetimeIndex(alt_input_idx, copy=True)
values = np.array(alt_input_vals, copy=True)
else:
return None, None
if (index.dropna().empty or all(np.isnan(values))
or index.size != values.size):
return None, None
if not (np.issubdtype(values.dtype, np.floating)
or np.issubdtype(values.dtype, np.integer)):
return None, None
if index.tz:
index = index.tz_localize(None)
return index, values
......@@ -4,7 +4,7 @@ This module contains the following function:
calculate_statistics - calculate the requested statistics
"""
from toarstats.input_checks import check_sampling, check_statistics
from toarstats.input_checks import check_data, check_sampling, check_statistics
def calculate_statistics(sampling, statistics, data=None, metadata=None,
......@@ -92,3 +92,4 @@ def calculate_statistics(sampling, statistics, data=None, metadata=None,
"""
sampling_method = check_sampling(sampling)
stats_list = check_statistics(statistics)
idx, vals = check_data(data, datetimes, values)
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