Skip to content

Commit

Permalink
Fix processing outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
oscgonfer committed Nov 27, 2024
1 parent db52fab commit dc66232
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
6 changes: 3 additions & 3 deletions scdata/device/process/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from scdata.tools.cleaning import clean
from pandas import DataFrame
from numpy import array
from scdata.device.process.error_codes import StatusCode, ProcessResult

def apply_regressor(dataframe, **kwargs):
'''
Expand Down Expand Up @@ -37,8 +38,7 @@ def apply_regressor(dataframe, **kwargs):
inputdf = inputdf.reindex(sorted(inputdf.columns), axis=1)
except KeyError:
logger.error('Inputs not in dataframe')
pass
return None
return ProcessResult(None, StatusCode.ERROR_MISSING_INPUTS)

if 'model' not in kwargs:
logger.error('Model not in inputs')
Expand All @@ -56,4 +56,4 @@ def apply_regressor(dataframe, **kwargs):
features = array(inputdf)
result = DataFrame(model.predict(features)).set_index(inputdf.index)

return result
return ProcessResult(result, StatusCode.SUCCESS)
34 changes: 21 additions & 13 deletions scdata/device/process/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from scipy import ndimage
from scdata.device.process import is_within_circle
from scdata.tools.custom_logger import logger
from scdata.device.process.error_codes import StatusCode, ProcessResult

# TODO UPDATE all these functions to make them comply with StatusCode types

def delta_index_ts(dataframe, **kwargs):
result = dataframe.index.to_series().diff().astype('timedelta64[s]')
return result
try:
result = dataframe.index.to_series().diff().astype('timedelta64[s]')
except:
return ProcessResult(None, StatusCode.ERROR_UNDEFINED)
return ProcessResult(result, StatusCode.SUCCESS)

def poly_ts(dataframe, **kwargs):
"""
Expand Down Expand Up @@ -45,7 +51,7 @@ def poly_ts(dataframe, **kwargs):
for i in range(n_channels):
result += power(dataframe[channels[i]], exponents[i]) * coefficients[i]

return result + extra_term
return ProcessResult(result + extra_term, StatusCode.SUCCESS)

def clean_ts(dataframe, **kwargs):
"""
Expand Down Expand Up @@ -91,7 +97,7 @@ def clean_ts(dataframe, **kwargs):
if window is not None:
result.rolling(window = window, win_type = win_type).mean()

return result
return ProcessResult(result, StatusCode.SUCCESS)

def merge_ts(dataframe, **kwargs):
"""
Expand Down Expand Up @@ -168,7 +174,7 @@ def merge_ts(dataframe, **kwargs):
# elif pick == 'min_nonzero':
# df['result'] = df.loc[df['flag'] == True, kwargs['names']].min(skipna=True, axis = 1)

return df['result']
return ProcessResult(df['result'], StatusCode.SUCCESS)

def rolling_avg(dataframe, **kwargs):
"""
Expand Down Expand Up @@ -208,11 +214,14 @@ def rolling_avg(dataframe, **kwargs):
else: win_type = None

if 'type' in kwargs:
if kwargs['type'] == 'mean': return result.rolling(window = window, win_type = win_type).mean()
if kwargs['type'] == 'max': return result.rolling(window = window, win_type = win_type).max()
if kwargs['type'] == 'min': return result.rolling(window = window, win_type = win_type).min()
if kwargs['type'] == 'mean':
return ProcessResult(result.rolling(window = window, win_type = win_type).mean(), StatusCode.SUCCESS)
if kwargs['type'] == 'max':
return ProcessResult(result.rolling(window = window, win_type = win_type).max(), StatusCode.SUCCESS)
if kwargs['type'] == 'min':
return ProcessResult(result.rolling(window = window, win_type = win_type).min(), StatusCode.SUCCESS)
else:
return result.rolling(window = window, win_type = win_type).mean()
return ProcessResult(result.rolling(window = window, win_type = win_type).mean(), StatusCode.SUCCESS)

def time_derivative(dataframe, **kwargs):
"""
Expand Down Expand Up @@ -243,9 +252,8 @@ def time_derivative(dataframe, **kwargs):

if gaussian_filter1d:
df['diff_filter1d'] = ndimage.gaussian_filter1d(df['diff'], sigma=1, order=1, mode='wrap') / dx
return df['diff_filter1d']

return df['diff']
return ProcessResult(df['diff_filter1d'], StatusCode.SUCCESS)
return ProcessResult(df['diff'], StatusCode.SUCCESS)

def geo_located(dataframe, **kwargs):
"""
Expand Down Expand Up @@ -276,4 +284,4 @@ def geo_located(dataframe, **kwargs):

result['within'] = result.apply(lambda x: is_within_circle(x, kwargs['within'], lat_name, long_name), axis=1)

return result['within']
return ProcessResult(result['within'], StatusCode.SUCCESS)

0 comments on commit dc66232

Please sign in to comment.