Skip to content

Commit

Permalink
Merge pull request #568 from Aarhus-Psychiatry-Research/add-docstring…
Browse files Browse the repository at this point in the history
…-to-flattenerr

docs: add docstring to flattener
  • Loading branch information
HLasse authored May 17, 2024
2 parents 6996013 + dfdf5dd commit 3d662f8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/timeseriesflattener/flattener.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,25 @@ class Flattener:
predictiontime_frame: PredictionTimeFrame
compute_lazily: bool = False
n_workers: int | None = None
"""Flatten multiple irregular time series to a static feature set.
Args:
predictiontime_frame: A frame that contains the prediction times.
compute_lazily: If True, the computation will be done lazily.
n_workers: The number of workers to use for multiprocessing.
If None, multiprocessing will be handled entirely by polars, otherwise,
specify the number of workers to use with joblib. """

def aggregate_timeseries(
self, specs: Sequence[ValueSpecification], step_size: dt.timedelta | None = None
) -> AggregatedFrame:
"""Perform the aggregation/flattening.
Args:
specs: The specifications for the features to be created.
step_size: The step size for the aggregation.
If not None, will aggregate prediction times in chunks of step_size.
Reduce if you encounter memory issues."""
if self.compute_lazily:
print(
"We have encountered performance issues on Windows when using lazy evaluation. If you encounter performance issues, try setting lazy=False."
Expand Down
8 changes: 5 additions & 3 deletions src/timeseriesflattener/test_aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SingleVarAggregatorExample:
aggregator: Aggregator
input_values: Sequence[float | None]
expected_output_values: Sequence[float]
fallback_str: str = "nan"

@property
def input_frame(self) -> pl.LazyFrame:
Expand All @@ -58,7 +59,7 @@ def expected_output(self) -> pl.DataFrame:
return pl.DataFrame(
{
"prediction_time_uuid": [1],
f"value_{self.aggregator.name}_fallback_nan": self.expected_output_values,
f"value_{self.aggregator.name}_fallback_{self.fallback_str}": self.expected_output_values,
}
)

Expand Down Expand Up @@ -90,12 +91,13 @@ def expected_output(self) -> pl.DataFrame:
aggregator=VarianceAggregator(), input_values=[1, 2], expected_output_values=[0.5]
),
SingleVarAggregatorExample(
aggregator=HasValuesAggregator(), input_values=[1, 2], expected_output_values=[1]
aggregator=HasValuesAggregator(), input_values=[1, 2], expected_output_values=[1], fallback_str="False"
),
SingleVarAggregatorExample(
aggregator=HasValuesAggregator(),
input_values=[None], # type: ignore
expected_output_values=[0],
fallback_str="False",
),
ComplexAggregatorExample(
aggregator=SlopeAggregator(timestamp_col_name="timestamp"),
Expand Down Expand Up @@ -153,7 +155,7 @@ def test_aggregator(example: AggregatorExampleType):
timestamp_col_name="timestamp",
),
aggregators=[example.aggregator],
fallback=np.nan,
fallback=np.nan if example.aggregator.name != "bool" else False,
)

assert_frame_equal(result.collect(), example.expected_output)

0 comments on commit 3d662f8

Please sign in to comment.