|
|
|
@ -3,13 +3,22 @@ from collections.abc import Mapping, Sequence
@@ -3,13 +3,22 @@ from collections.abc import Mapping, Sequence
|
|
|
|
|
import pandas |
|
|
|
|
|
|
|
|
|
from .columns import ( |
|
|
|
|
AGGREGATION_PREFIX, |
|
|
|
|
META_DATA_WELL_ROW, |
|
|
|
|
META_DATA_EXPOSURE_ID, |
|
|
|
|
META_DATA_WELL_COLUMN, |
|
|
|
|
SETTINGS_EXPOSURE_TIME, |
|
|
|
|
META_DATA_PARAMETERS_TIME, |
|
|
|
|
SETTINGS_EXPOSURE_CHANNEL, |
|
|
|
|
META_DATA_PARAMETERS_CHANNEL, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
DEFAULT_AGGREGATION_COLUMNS = [ |
|
|
|
|
META_DATA_EXPOSURE_ID, |
|
|
|
|
META_DATA_WELL_ROW, |
|
|
|
|
META_DATA_WELL_COLUMN, |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def split_data_frame(data_frame, column): |
|
|
|
|
""" splits a data frame on unique column values """ |
|
|
|
@ -107,3 +116,52 @@ def apply_exposure_map(data_frame, exposure_map=None):
@@ -107,3 +116,52 @@ def apply_exposure_map(data_frame, exposure_map=None):
|
|
|
|
|
left_on=META_DATA_EXPOSURE_ID, |
|
|
|
|
right_index=True, |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def aggregate( |
|
|
|
|
data_frame, column, method, on=DEFAULT_AGGREGATION_COLUMNS, new_name=None |
|
|
|
|
): |
|
|
|
|
"""returns the aggregates of one data frame column |
|
|
|
|
|
|
|
|
|
data_frame: pandas data frame with the data to aggregate |
|
|
|
|
column: column name to aggregate |
|
|
|
|
method: method of aggregation |
|
|
|
|
on: list of coulumns to group by, defaults to |
|
|
|
|
- Exposure.Id |
|
|
|
|
- Well.Column |
|
|
|
|
- Well.Row |
|
|
|
|
new_name: the name of the aggregate column |
|
|
|
|
if set to None, a prefix will be added to the original name |
|
|
|
|
""" |
|
|
|
|
if new_name is None: |
|
|
|
|
method_as_name = method.title() |
|
|
|
|
new_name = f"{AGGREGATION_PREFIX}.{method_as_name}.{column}" |
|
|
|
|
grouped = data_frame.groupby(on) |
|
|
|
|
aggregated_data = grouped.agg({column: method}) |
|
|
|
|
aggregated_data.columns = [new_name] |
|
|
|
|
return aggregated_data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_aggregate( |
|
|
|
|
data_frame, column, method, on=DEFAULT_AGGREGATION_COLUMNS, new_name=None |
|
|
|
|
): |
|
|
|
|
"""aggregates one column in a data frame and |
|
|
|
|
adds the resulting column to the data frame |
|
|
|
|
|
|
|
|
|
data_frame: pandas data frame with the data to aggregate |
|
|
|
|
column: column name to aggregate |
|
|
|
|
method: method of aggregation |
|
|
|
|
on: list of coulumns to group by, defaults to |
|
|
|
|
- Exposure.Id |
|
|
|
|
- Well.Column |
|
|
|
|
- Well.Row |
|
|
|
|
new_name: the name of the aggregate column, |
|
|
|
|
if set to None, a prefix will be added to the original name |
|
|
|
|
""" |
|
|
|
|
aggregated_data = aggregate(data_frame, column, method, on, new_name) |
|
|
|
|
return data_frame.merge( |
|
|
|
|
aggregated_data, |
|
|
|
|
how="left", |
|
|
|
|
left_on=on, |
|
|
|
|
right_index=True, |
|
|
|
|
) |
|
|
|
|