Name | timeseriesflattener JSON |
Version |
2.4.0
JSON |
| download |
home_page | None |
Summary | A package for converting time series data from e.g. electronic health records into wide format data. |
upload_time | 2024-09-27 09:48:30 |
maintainer | None |
docs_url | None |
author | Kenneth Enevoldsen |
requires_python | <3.13.0,>=3.9.0 |
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. |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<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).
Raw data
{
"_id": null,
"home_page": null,
"name": "timeseriesflattener",
"maintainer": null,
"docs_url": null,
"requires_python": "<3.13.0,>=3.9.0",
"maintainer_email": null,
"keywords": null,
"author": "Kenneth Enevoldsen",
"author_email": "Lasse Hansen <lasseh0310@gmail.com>, Jakob Gr\u00f8hn Damgaard <bokajgd@gmail.com>, Martin Bernstorff <martinbernstorff@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/7a/af/787698d5417f58aebc54322b97194f491ab1234fc580f39be38348374a9d/timeseriesflattener-2.4.0.tar.gz",
"platform": null,
"description": "<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>\n\n## Timeseriesflattener\n\n[![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)\n[![python versions](https://img.shields.io/pypi/pyversions/timeseriesflattener)](https://pypi.org/project/timeseriesflattener/)\n\n[![PyPI version](https://badge.fury.io/py/timeseriesflattener.svg)](https://pypi.org/project/timeseriesflattener/)\n[![status](https://joss.theoj.org/papers/3bbea8745668d1aa40ff796c6fd3db87/status.svg)](https://joss.theoj.org/papers/3bbea8745668d1aa40ff796c6fd3db87)\n\nTime 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. \n\nIn 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.\n\n`timeseriesflattener` aims to simplify this process by providing an easy-to-use and fully-specified pipeline for flattening complex time series.\n\n## \ud83d\udd27 Installation\n\nTo get started using timeseriesflattener simply install it using pip by running the following line in your terminal: \n\n```\npip install timeseriesflattener\n```\n\n## \u26a1 Quick start\n\n```py\nimport datetime as dt\n\nimport numpy as np\nimport polars as pl\n\n# Load a dataframe with times you wish to make a prediction\nprediction_times_df = pl.DataFrame(\n {\"id\": [1, 1, 2], \"date\": [\"2020-01-01\", \"2020-02-01\", \"2020-02-01\"]}\n)\n# Load a dataframe with raw values you wish to aggregate as predictors\npredictor_df = pl.DataFrame(\n {\n \"id\": [1, 1, 1, 2],\n \"date\": [\"2020-01-15\", \"2019-12-10\", \"2019-12-15\", \"2020-01-02\"],\n \"predictor_value\": [1, 2, 3, 4],\n }\n)\n# Load a dataframe specifying when the outcome occurs\noutcome_df = pl.DataFrame({\"id\": [1], \"date\": [\"2020-03-01\"], \"outcome_value\": [1]})\n\n# Specify how to aggregate the predictors and define the outcome\nfrom timeseriesflattener import (\n MaxAggregator,\n MinAggregator,\n OutcomeSpec,\n PredictionTimeFrame,\n PredictorSpec,\n ValueFrame,\n)\n\npredictor_spec = PredictorSpec(\n value_frame=ValueFrame(\n init_df=predictor_df, entity_id_col_name=\"id\", value_timestamp_col_name=\"date\"\n ),\n lookbehind_distances=[dt.timedelta(days=1)],\n aggregators=[MaxAggregator(), MinAggregator()],\n fallback=np.nan,\n column_prefix=\"pred\",\n)\n\noutcome_spec = OutcomeSpec(\n value_frame=ValueFrame(\n init_df=outcome_df, entity_id_col_name=\"id\", value_timestamp_col_name=\"date\"\n ),\n lookahead_distances=[dt.timedelta(days=1)],\n aggregators=[MaxAggregator(), MinAggregator()],\n fallback=np.nan,\n column_prefix=\"outc\",\n)\n\n# Instantiate TimeseriesFlattener and add the specifications\nfrom timeseriesflattener import Flattener\n\nresult = Flattener(\n predictiontime_frame=PredictionTimeFrame(\n init_df=prediction_times_df, entity_id_col_name=\"id\", timestamp_col_name=\"date\"\n )\n).aggregate_timeseries(specs=[predictor_spec, outcome_spec])\nresult.df\n\n```\n\nOutput:\n\n| | id | date | prediction_time_uuid | pred_test_feature_within_30_days_mean_fallback_nan | outc_test_outcome_within_31_days_maximum_fallback_0_dichotomous |\n| --: | --: | :------------------ | :-------------------- | -------------------------------------------------: | --------------------------------------------------------------: |\n| 0 | 1 | 2020-01-01 00:00:00 | 1-2020-01-01-00-00-00 | 2.5 | 0 |\n| 1 | 1 | 2020-02-01 00:00:00 | 1-2020-02-01-00-00-00 | 1 | 1 |\n| 2 | 2 | 2020-02-01 00:00:00 | 2-2020-02-01-00-00-00 | 4 | 0 |\n\n## \ud83d\udcd6 Tutorial\n* \ud83c\udf93 [Tutorials](https://aarhus-psychiatry-research.github.io/timeseriesflattener/tutorials.html)\n* \ud83d\udcd6 [General docs](https://Aarhus-Psychiatry-Research.github.io/timeseriesflattener/)\n\n## \ud83d\udcac Where to ask questions\n\n| Type | |\n| ------------------------------- | ---------------------- |\n| \ud83d\udea8 **Bug Reports** | [GitHub Issue Tracker] |\n| \ud83c\udf81 **Feature Requests & Ideas** | [GitHub Issue Tracker] |\n| \ud83d\udc69\u200d\ud83d\udcbb **Usage Questions** | [GitHub Discussions] |\n| \ud83d\uddef **General Discussion** | [GitHub Discussions] |\n\n[github issue tracker]: https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/issues\n[github discussions]: https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener/discussions\n\n## \ud83c\udf93 Projects\n\nPSYCOP projects use `timeseriesflattener`, see more at the [monorepo](https://github.com/Aarhus-Psychiatry-Research/psycop-common/tree/main/psycop/projects).\n",
"bugtrack_url": null,
"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.",
"summary": "A package for converting time series data from e.g. electronic health records into wide format data.",
"version": "2.4.0",
"project_urls": {
"documentation": "https://aarhus-psychiatry-research.github.io/timeseriesflattener/",
"homepage": "https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener",
"repository": "https://github.com/Aarhus-Psychiatry-Research/timeseriesflattener"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "39ea17b134b912fa8465e441be9d1818d1b8e30758fa22d00aec71318280fac3",
"md5": "69db11b5e47534a5d95505bab38bbaf8",
"sha256": "bba93d5e35112689adc671231a6994cb4024cbc0ca697aa950c748445d8b91a2"
},
"downloads": -1,
"filename": "timeseriesflattener-2.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "69db11b5e47534a5d95505bab38bbaf8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<3.13.0,>=3.9.0",
"size": 8613552,
"upload_time": "2024-09-27T09:48:27",
"upload_time_iso_8601": "2024-09-27T09:48:27.516654Z",
"url": "https://files.pythonhosted.org/packages/39/ea/17b134b912fa8465e441be9d1818d1b8e30758fa22d00aec71318280fac3/timeseriesflattener-2.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7aaf787698d5417f58aebc54322b97194f491ab1234fc580f39be38348374a9d",
"md5": "bfe9583584e14e07fea54afd438bf4d0",
"sha256": "72e67a1daf75df89d10d19a6a4da4e4ce58894a09f56a620d459ad618055d74e"
},
"downloads": -1,
"filename": "timeseriesflattener-2.4.0.tar.gz",
"has_sig": false,
"md5_digest": "bfe9583584e14e07fea54afd438bf4d0",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<3.13.0,>=3.9.0",
"size": 9594561,
"upload_time": "2024-09-27T09:48:30",
"upload_time_iso_8601": "2024-09-27T09:48:30.178085Z",
"url": "https://files.pythonhosted.org/packages/7a/af/787698d5417f58aebc54322b97194f491ab1234fc580f39be38348374a9d/timeseriesflattener-2.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-27 09:48:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Aarhus-Psychiatry-Research",
"github_project": "timeseriesflattener",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "timeseriesflattener"
}