Package: timeseriesflattener
TimeseriesflattenerTime 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 beflattenedso 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.timeseriesflatteneraims to simplify this process by providing an easy-to-use and fully-specified pipeline for flattening complex time series.🔧 InstallationTo get started using timeseriesflattener simply install it using pip by running the following line in your terminal:pip install timeseriesflattener⚡ Quick startimportdatetimeasdtimportnumpyasnpimportpolarsaspl# Load a dataframe with times you wish to make a predictionprediction_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 predictorspredictor_df=pl.DataFrame({"id":[1,1,1,2],"date":["2020-01-15","2019-12-10","2019-12-15","2020-01-02"],"value":[1,2,3,4],})# Load a dataframe specifying when the outcome occursoutcome_df=pl.DataFrame({"id":[1],"date":["2020-03-01"],"value":[1]})# Specify how to aggregate the predictors and define the outcomefromtimeseriesflattenerimport(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 specificationsfromtimeseriesflattenerimportFlattenerresult=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:iddateprediction_time_uuidpred_test_feature_within_30_days_mean_fallback_nanoutc_test_outcome_within_31_days_maximum_fallback_0_dichotomous012020-01-01 00:00:001-2020-01-01-00-00-002.50112020-02-01 00:00:001-2020-02-01-00-00-0011222020-02-01 00:00:002-2020-02-01-00-00-0040📖 DocumentationDocumentation🎓TutorialSimple and advanced tutorials to get you started usingtimeseriesflattener🎛General docsThe detailed reference for timeseriesflattener's API.🙋FAQFrequently asked question🗺️RoadmapKanban board for the roadmap for the project💬 Where to ask questionsType🚨Bug ReportsGitHub Issue Tracker🎁Feature Requests & IdeasGitHub Issue Tracker👩‍💻Usage QuestionsGitHub Discussions🗯General DiscussionGitHub Discussions🎓 ProjectsPSYCOP projects which usetimeseriesflattener. Note that some of these projects have yet to be published and are thus private.ProjectPublicationsType 2 DiabetesPrediction of type 2 diabetes among patients with visits to psychiatric hospital departments