Commit c9ad31c4 authored by Niklas Selke's avatar Niklas Selke
Browse files

Refactored the 'check_data' function in 'input_checks.py'. It now returns a data frame.

parent b44c5acf
"""Functions to check the given user inputs. """Functions to check the given user inputs.
This module contains the following function: This module contains the following functions:
check_sampling - check the given sampling argument check_sampling - check the given sampling argument
check_statistics - check the given statistics argument
check_data - check the given data argument
""" """
import numpy as np import numpy as np
...@@ -56,46 +58,46 @@ def check_statistics(statistics_in): ...@@ -56,46 +58,46 @@ def check_statistics(statistics_in):
return statistics_out return statistics_out
def check_data(input_argument, alt_input_idx, alt_input_vals): def check_data(data_in, datetimes_in, values_in):
"""Check the given data argument. """Check the given data argument.
:param input_argument: the given argument :param data_in: the given data argument
:param alt_input_idx: the alternative input's index :param datetimes_in: the given datetimes argument
:param alt_input_vals: the alternative input's values :param values_in: the given values argument
:return: The processed arguments or None if there was a problem with :return: The processed data argument or None if there was a problem
the input while processing the given data argument
""" """
if ((input_argument is not None and not (alt_input_idx is None if (data_in is not None
and alt_input_vals is None)) and not (datetimes_in is None and values_in is None)):
or (input_argument is None and (alt_input_idx is None return None
or alt_input_vals is None))): if data_in is None and (datetimes_in is None or values_in is None):
return None, None return None
if isinstance(input_argument, (pd.DataFrame, pd.Series)): if isinstance(data_in, (pd.DataFrame, pd.Series)):
index = input_argument.index index_out = data_in.index
if isinstance(input_argument, pd.Series): if isinstance(data_in, pd.Series):
param_vals = input_argument.to_numpy() values_out = data_in.to_numpy()
elif "value" in input_argument.columns.to_numpy(): elif "value" in data_in.columns.to_numpy():
param_vals = input_argument["value"].to_numpy() values_out = data_in["value"].to_numpy()
elif "values" in input_argument.columns.to_numpy(): elif "values" in data_in.columns.to_numpy():
param_vals = input_argument["values"].to_numpy() values_out = data_in["values"].to_numpy()
elif input_argument.shape[1] > 0: elif data_in.shape[1] > 0:
param_vals = input_argument.iloc[:, 0].to_numpy() values_out = data_in.iloc[:, 0].to_numpy()
else: else:
return None, None values_out = np.array([])
else: else:
index = alt_input_idx index_out = datetimes_in
param_vals = alt_input_vals values_out = values_in
if not isinstance(index, pd.DatetimeIndex): if not isinstance(index_out, pd.DatetimeIndex):
return None, None return None
if not isinstance(param_vals, np.ndarray) or param_vals.ndim != 1: if not isinstance(values_out, np.ndarray) or values_out.ndim != 1:
return None, None return None
if (index.dropna().empty or all(np.isnan(param_vals)) if not (np.issubdtype(values_out.dtype, np.floating)
or index.size != param_vals.size): or np.issubdtype(values_out.dtype, np.integer)):
return None, None return None
if not (np.issubdtype(param_vals.dtype, np.floating) if (index_out.dropna().empty or all(np.isnan(values_out))
or np.issubdtype(param_vals.dtype, np.integer)): or index_out.size != values_out.size):
return None, None return None
if index.tz: if index_out.tz:
index = index.tz_localize(None) index_out = index_out.tz_localize(None)
return index, param_vals return pd.DataFrame({"values": values_out}, index=index_out)
...@@ -92,4 +92,4 @@ def calculate_statistics(sampling, statistics, data=None, metadata=None, ...@@ -92,4 +92,4 @@ def calculate_statistics(sampling, statistics, data=None, metadata=None,
""" """
sampling_method = check_sampling(sampling) sampling_method = check_sampling(sampling)
stats_list = check_statistics(statistics) stats_list = check_statistics(statistics)
datetime_index, parameter_values = check_data(data, datetimes, values) data_frame = 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