Metadata-Version: 2.1
Name: timeseriesflattener
Version: 2.5.0
Summary: A package for converting time series data from e.g. electronic health records into wide format data.
Author: Kenneth Enevoldsen
Author-email: Lasse Hansen <lasseh0310@gmail.com>, Jakob Grøhn Damgaard <bokajgd@gmail.com>, Martin Bernstorff <martinbernstorff@gmail.com>
License: MIT License
        
        Copyright (c) 2022 PSYCOP Group, Aarhus University
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: homepage, https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener
Project-URL: repository, https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener
Project-URL: documentation, https://aarhus-psychiatry-research.github.io/timeseriesflattener/
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: Microsoft :: Windows
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Requires-Python: <3.13.0,>=3.9.0
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: scipy>=1.8.0
Requires-Dist: scikit-learn>=1.1.2
Requires-Dist: pydantic>=2.0.0
Requires-Dist: pandas>=1.4.0
Requires-Dist: catalogue>=2.0.0
Requires-Dist: numpy>=1.23.3
Requires-Dist: pyarrow>=8.0.0
Requires-Dist: protobuf<=4.24.4
Requires-Dist: frozendict>=2.3.4
Requires-Dist: coloredlogs>14.0.0
Requires-Dist: tqdm>4.1.0
Requires-Dist: polars>=0.19.0
Requires-Dist: iterpy==1.6.0
Requires-Dist: rich>=13.0.0
Requires-Dist: ipykernel[docs]>=6.29.5
Provides-Extra: dev
Requires-Dist: pyright==1.1.330.post0; extra == "dev"
Requires-Dist: pre-commit==3.4.0; extra == "dev"
Requires-Dist: ruff==0.2.1; extra == "dev"
Requires-Dist: pandas-stubs==2.2.0.240218; extra == "dev"
Requires-Dist: invoke==2.1.1; extra == "dev"
Provides-Extra: test
Requires-Dist: pytest==7.2.2; extra == "test"
Requires-Dist: pytest-cov==3.0.0; extra == "test"
Requires-Dist: pytest-xdist==3.1.0; extra == "test"
Requires-Dist: pytest-sugar==0.9.7; extra == "test"
Requires-Dist: pytest-testmon==2.1.0; extra == "test"
Requires-Dist: pytest-benchmark==4.0.0; extra == "test"
Requires-Dist: pytest-codspeed==2.2.0; extra == "test"
Provides-Extra: docs
Requires-Dist: sphinx==5.3.0; extra == "docs"
Requires-Dist: furo==2023.3.27; extra == "docs"
Requires-Dist: sphinx-copybutton==0.5.2; extra == "docs"
Requires-Dist: sphinxext-opengraph==0.8.2; extra == "docs"
Requires-Dist: myst-nb==0.17.2; extra == "docs"
Requires-Dist: sphinx_design==0.3.0; extra == "docs"
Requires-Dist: ipykernel>=6.29.5; extra == "docs"
Provides-Extra: tutorials
Requires-Dist: jupyter<1.1.0,>=1.0.0; extra == "tutorials"
Requires-Dist: skimpy==0.0.15; extra == "tutorials"

<a href="https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener"><img src="https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/blob/main/docs/_static/icon.png?raw=true" width="200" align="right"/></a>

## Timeseriesflattener

[![github actions pytest](https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/actions/workflows/main_test_and_release.yml/badge.svg)](https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/actions)
[![python versions](https://img.shields.io/pypi/pyversions/timeseriesflattener)](https://pypi.org/project/timeseriesflattener/)

[![PyPI version](https://badge.fury.io/py/timeseriesflattener.svg)](https://pypi.org/project/timeseriesflattener/)
[![status](https://joss.theoj.org/papers/3bbea8745668d1aa40ff796c6fd3db87/status.svg)](https://joss.theoj.org/papers/3bbea8745668d1aa40ff796c6fd3db87)

Time series from e.g. electronic health records often have a large number of variables, are sampled at irregular intervals and tend to have a large number of missing values. Before this type of data can be used for prediction modelling with machine learning methods such as logistic regression or XGBoost, the data needs to be reshaped. 

In essence, the time series need to be _flattened_ so that each prediction time is represented by a set of predictor values and an outcome value. These predictor values can be constructed by aggregating the preceding values in the time series within a certain time window.

`timeseriesflattener` aims to simplify this process by providing an easy-to-use and fully-specified pipeline for flattening complex time series.

## 🔧 Installation

To get started using timeseriesflattener simply install it using pip by running the following line in your terminal: 

```
pip install timeseriesflattener
```

## ⚡ Quick start

```py
import datetime as dt

import numpy as np
import polars as pl

# Load a dataframe with times you wish to make a prediction
prediction_times_df = pl.DataFrame(
    {"id": [1, 1, 2], "date": ["2020-01-01", "2020-02-01", "2020-02-01"]}
)
# Load a dataframe with raw values you wish to aggregate as predictors
predictor_df = pl.DataFrame(
    {
        "id": [1, 1, 1, 2],
        "date": ["2020-01-15", "2019-12-10", "2019-12-15", "2020-01-02"],
        "predictor_value": [1, 2, 3, 4],
    }
)
# Load a dataframe specifying when the outcome occurs
outcome_df = pl.DataFrame({"id": [1], "date": ["2020-03-01"], "outcome_value": [1]})

# Specify how to aggregate the predictors and define the outcome
from timeseriesflattener import (
    MaxAggregator,
    MinAggregator,
    OutcomeSpec,
    PredictionTimeFrame,
    PredictorSpec,
    ValueFrame,
)

predictor_spec = PredictorSpec(
    value_frame=ValueFrame(
        init_df=predictor_df, entity_id_col_name="id", value_timestamp_col_name="date"
    ),
    lookbehind_distances=[dt.timedelta(days=1)],
    aggregators=[MaxAggregator(), MinAggregator()],
    fallback=np.nan,
    column_prefix="pred",
)

outcome_spec = OutcomeSpec(
    value_frame=ValueFrame(
        init_df=outcome_df, entity_id_col_name="id", value_timestamp_col_name="date"
    ),
    lookahead_distances=[dt.timedelta(days=1)],
    aggregators=[MaxAggregator(), MinAggregator()],
    fallback=np.nan,
    column_prefix="outc",
)

# Instantiate TimeseriesFlattener and add the specifications
from timeseriesflattener import Flattener

result = Flattener(
    predictiontime_frame=PredictionTimeFrame(
        init_df=prediction_times_df, entity_id_col_name="id", timestamp_col_name="date"
    )
).aggregate_timeseries(specs=[predictor_spec, outcome_spec])
result.df

```

Output:

|     |  id | date                | prediction_time_uuid  | pred_test_feature_within_30_days_mean_fallback_nan | outc_test_outcome_within_31_days_maximum_fallback_0_dichotomous |
| --: | --: | :------------------ | :-------------------- | -------------------------------------------------: | --------------------------------------------------------------: |
|   0 |   1 | 2020-01-01 00:00:00 | 1-2020-01-01-00-00-00 |                                                2.5 |                                                               0 |
|   1 |   1 | 2020-02-01 00:00:00 | 1-2020-02-01-00-00-00 |                                                  1 |                                                               1 |
|   2 |   2 | 2020-02-01 00:00:00 | 2-2020-02-01-00-00-00 |                                                  4 |                                                               0 |

## 📖 Tutorial
* 🎓 [Tutorials](https://aarhus-psychiatry-research.github.io/timeseriesflattener/tutorials.html)
* 📖 [General docs](https://Aarhus-Psychiatry-Research.github.io/timeseriesflattener/)

## 💬 Where to ask questions

| Type                            |                        |
| ------------------------------- | ---------------------- |
| 🚨 **Bug Reports**              | [GitHub Issue Tracker] |
| 🎁 **Feature Requests & Ideas** | [GitHub Issue Tracker] |
| 👩‍💻 **Usage Questions**          | [GitHub Discussions]   |
| 🗯 **General Discussion**        | [GitHub Discussions]   |

[github issue tracker]: https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/issues
[github discussions]: https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/discussions

## 🎓 Projects

PSYCOP projects use `timeseriesflattener`, see more at the [monorepo](https://github.com/Aarhus-Psychiatry-Research/psycop-common/tree/main/psycop/projects).
