Skip to main content

A package for converting time series data from e.g. electronic health records into wide format data.

Project description

Timeseriesflattener

github actions docs github actions pytest python versions Code style: black

PyPI version status

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

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.lazy(), 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.lazy(), 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.lazy(), entity_id_col_name="id", timestamp_col_name="date"
    )
).aggregate_timeseries(specs=[predictor_spec, outcome_spec])
result.collect()

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

📖 Documentation

Documentation
🎓 Tutorial Simple and advanced tutorials to get you started using timeseriesflattener
🎛 General docs The detailed reference for timeseriesflattener's API.
🙋 FAQ Frequently asked question
🗺️ Roadmap Kanban board for the roadmap for the project

💬 Where to ask questions

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

🎓 Projects

PSYCOP projects which use timeseriesflattener. Note that some of these projects have yet to be published and are thus private.

Project Publications
Type 2 Diabetes Prediction of type 2 diabetes among patients with visits to psychiatric hospital departments

Project details


Release history Release notifications | RSS feed

This version

2.2.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timeseriesflattener-2.2.3.tar.gz (9.5 MB view details)

Uploaded Source

Built Distribution

timeseriesflattener-2.2.3-py3-none-any.whl (8.6 MB view details)

Uploaded Python 3

File details

Details for the file timeseriesflattener-2.2.3.tar.gz.

File metadata

  • Download URL: timeseriesflattener-2.2.3.tar.gz
  • Upload date:
  • Size: 9.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/43.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.2.1 tqdm/4.66.4 importlib-metadata/7.1.0 keyring/25.2.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.14

File hashes

Hashes for timeseriesflattener-2.2.3.tar.gz
Algorithm Hash digest
SHA256 9850f45838b03875697b4a5308efa1856d4c8ec4ee564574167df26e34aea266
MD5 e24325adb21c0730314ccb0ec3d0b65d
BLAKE2b-256 b7da5455c5646222a39893726e490b78ff64ce83c16a31c06cfa7657ec5d10c4

See more details on using hashes here.

File details

Details for the file timeseriesflattener-2.2.3-py3-none-any.whl.

File metadata

  • Download URL: timeseriesflattener-2.2.3-py3-none-any.whl
  • Upload date:
  • Size: 8.6 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.10.0 readme-renderer/43.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.2.1 tqdm/4.66.4 importlib-metadata/7.1.0 keyring/25.2.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.14

File hashes

Hashes for timeseriesflattener-2.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9b7bd66130965603a307695986f0383c8d83f63c781ac5ace533da1cb7789d05
MD5 b9d2004b368cdd805fd8c82e18bd143f
BLAKE2b-256 0d3ae08f5ec7adaa89bd9157a3cf72107cce972d02b0098996dab0d573d74297

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page