medpipe 0.2.2.dev0__tar.gz → 0.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. medpipe-0.3.1/PKG-INFO +146 -0
  2. medpipe-0.3.1/README.md +114 -0
  3. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/pyproject.toml +21 -11
  4. medpipe-0.3.1/requirements.txt +14 -0
  5. medpipe-0.3.1/src/medpipe/__init__.py +46 -0
  6. medpipe-0.3.1/src/medpipe/_types.py +39 -0
  7. medpipe-0.3.1/src/medpipe/data/__init__.py +9 -0
  8. medpipe-0.3.1/src/medpipe/data/transformers.py +87 -0
  9. medpipe-0.3.1/src/medpipe/data/utils.py +269 -0
  10. medpipe-0.3.1/src/medpipe/metrics/__init__.py +16 -0
  11. medpipe-0.3.1/src/medpipe/metrics/core.py +364 -0
  12. medpipe-0.3.1/src/medpipe/metrics/plots.py +722 -0
  13. medpipe-0.3.1/src/medpipe/models/__init__.py +9 -0
  14. medpipe-0.3.1/src/medpipe/models/core.py +157 -0
  15. medpipe-0.3.1/src/medpipe/pipeline/__init__.py +8 -0
  16. medpipe-0.3.1/src/medpipe/pipeline/pipeline.py +1519 -0
  17. medpipe-0.3.1/src/medpipe/py.typed +0 -0
  18. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/src/medpipe/utils/__init__.py +1 -1
  19. medpipe-0.3.1/src/medpipe/utils/config.py +542 -0
  20. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/src/medpipe/utils/exceptions.py +54 -46
  21. medpipe-0.3.1/src/medpipe/utils/io.py +126 -0
  22. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/src/medpipe/utils/logger.py +13 -6
  23. medpipe-0.3.1/src/medpipe.egg-info/PKG-INFO +146 -0
  24. medpipe-0.3.1/src/medpipe.egg-info/SOURCES.txt +27 -0
  25. medpipe-0.3.1/src/medpipe.egg-info/requires.txt +14 -0
  26. medpipe-0.2.2.dev0/PKG-INFO +0 -157
  27. medpipe-0.2.2.dev0/README.md +0 -130
  28. medpipe-0.2.2.dev0/requirements.txt +0 -10
  29. medpipe-0.2.2.dev0/src/medpipe/__init__.py +0 -54
  30. medpipe-0.2.2.dev0/src/medpipe/data/Preprocessor.py +0 -179
  31. medpipe-0.2.2.dev0/src/medpipe/data/__init__.py +0 -14
  32. medpipe-0.2.2.dev0/src/medpipe/data/db.py +0 -114
  33. medpipe-0.2.2.dev0/src/medpipe/data/preprocessing.py +0 -320
  34. medpipe-0.2.2.dev0/src/medpipe/data/sampler.py +0 -561
  35. medpipe-0.2.2.dev0/src/medpipe/data/weighting.py +0 -241
  36. medpipe-0.2.2.dev0/src/medpipe/metrics/__init__.py +0 -21
  37. medpipe-0.2.2.dev0/src/medpipe/metrics/core.py +0 -395
  38. medpipe-0.2.2.dev0/src/medpipe/metrics/plots.py +0 -429
  39. medpipe-0.2.2.dev0/src/medpipe/models/Calibrator.py +0 -147
  40. medpipe-0.2.2.dev0/src/medpipe/models/Predictor.py +0 -144
  41. medpipe-0.2.2.dev0/src/medpipe/models/__init__.py +0 -11
  42. medpipe-0.2.2.dev0/src/medpipe/models/core.py +0 -275
  43. medpipe-0.2.2.dev0/src/medpipe/pipeline/Pipeline.py +0 -832
  44. medpipe-0.2.2.dev0/src/medpipe/pipeline/__init__.py +0 -8
  45. medpipe-0.2.2.dev0/src/medpipe/utils/config.py +0 -199
  46. medpipe-0.2.2.dev0/src/medpipe/utils/io.py +0 -102
  47. medpipe-0.2.2.dev0/src/medpipe.egg-info/PKG-INFO +0 -157
  48. medpipe-0.2.2.dev0/src/medpipe.egg-info/SOURCES.txt +0 -42
  49. medpipe-0.2.2.dev0/src/medpipe.egg-info/requires.txt +0 -10
  50. medpipe-0.2.2.dev0/test/test_data_Preprocessor.py +0 -99
  51. medpipe-0.2.2.dev0/test/test_data_db.py +0 -90
  52. medpipe-0.2.2.dev0/test/test_data_preprocessing.py +0 -243
  53. medpipe-0.2.2.dev0/test/test_data_sampler.py +0 -477
  54. medpipe-0.2.2.dev0/test/test_data_weighting.py +0 -253
  55. medpipe-0.2.2.dev0/test/test_metrics_core.py +0 -44
  56. medpipe-0.2.2.dev0/test/test_models_core.py +0 -71
  57. medpipe-0.2.2.dev0/test/test_pipeline_Pipeline.py +0 -122
  58. medpipe-0.2.2.dev0/test/test_utils_config.py +0 -224
  59. medpipe-0.2.2.dev0/test/test_utils_exceptions.py +0 -211
  60. medpipe-0.2.2.dev0/test/test_utils_io.py +0 -76
  61. medpipe-0.2.2.dev0/test/test_utils_logger.py +0 -44
  62. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/LICENSE +0 -0
  63. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/setup.cfg +0 -0
  64. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/src/medpipe.egg-info/dependency_links.txt +0 -0
  65. {medpipe-0.2.2.dev0 → medpipe-0.3.1}/src/medpipe.egg-info/top_level.txt +0 -0
medpipe-0.3.1/PKG-INFO ADDED
@@ -0,0 +1,146 @@
1
+ Metadata-Version: 2.4
2
+ Name: medpipe
3
+ Version: 0.3.1
4
+ Summary: A package for clinical AI pipeline development
5
+ Author-email: Mathias Roesler <mathias.roesler@auckland.co.nz>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Changelog, https://github.com/https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/blob/main/CHANGELOG.md
8
+ Project-URL: Homepage, https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe
9
+ Classifier: Operating System :: POSIX :: Linux
10
+ Classifier: Programming Language :: Python :: 3 :: Only
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: 3.14
14
+ Requires-Python: >=3.12
15
+ Description-Content-Type: text/markdown
16
+ License-File: LICENSE
17
+ Requires-Dist: imbalanced_learn>=0.14.1
18
+ Requires-Dist: imblearn==0.0
19
+ Requires-Dist: matplotlib>=3.10.8
20
+ Requires-Dist: numpy>=2.4.2
21
+ Requires-Dist: pandas>=2.3.3
22
+ Requires-Dist: pytest>=9.0.1
23
+ Requires-Dist: pytest-mock>=3.15.1
24
+ Requires-Dist: scikit_learn==1.8.0
25
+ Requires-Dist: scipy>=1.17.0
26
+ Requires-Dist: pyarrow>=23.0.1
27
+ Requires-Dist: pydantic>=2.13.4
28
+ Requires-Dist: ml_insights>=1.1.0
29
+ Requires-Dist: ngboost>=0.5.10
30
+ Requires-Dist: pytest-mock>=3.15.1
31
+ Dynamic: license-file
32
+
33
+ # medpipe
34
+
35
+ ![GitHub License](https://img.shields.io/github/license/Surgical-Recovery-and-Safety-Lab/medpipe)
36
+ [![Python 3.12+](https://img.shields.io/badge/python-3.12+-green.svg)](https://www.python.org/downloads/)
37
+ [![tests](https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/actions/workflows/run_test.yml/badge.svg)](https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/actions/workflows/run_test.yml)
38
+ ## Table of content
39
+ 1. [Overview](#overview)
40
+ 2. [Installation](#installation)
41
+ 3. [Usage](#usage)
42
+ 1. [Preprocessing operations](#preprocess)
43
+ 2. [Models](#models)
44
+ 3. [Recalibration](#recalibration)
45
+ 4. [Metrics](#metrics)
46
+ 5. [Plots](#plots)
47
+ 5. [Examples](#examples)
48
+
49
+ ## Overview
50
+ The **medpipe** package is a layer to help create AI models (classifiers and regressors) for clinical applications from tabular data. It covers data loading and preprocessing, model creation and training, recalibration, and visualisation.
51
+ ___
52
+ ## Installation
53
+
54
+ To install **medpipe** use pip:
55
+ ```
56
+ $ pip install medpipe
57
+ ```
58
+
59
+ or clone the GitHub repository and install the package with pip:
60
+ ```
61
+ $ git clone git@github.com:Surgical-Recovery-and-Safety-Lab/medpipe.git
62
+ $ cd medpipe
63
+ $ pip install .
64
+ ```
65
+ **NOTE**: It is recommended to use a virtual environment (venv) to install this package.
66
+
67
+ Ensure that the installation was successful and that all tests pass by running the following command in the medpipe directory:
68
+ ```
69
+ $ pytest
70
+ ```
71
+ ___
72
+
73
+ ## Usage
74
+ This package was tested on a Linux distribution (Ubuntu 24.04) with Python v3.12.3. The [sckit-learn](https://scikit-learn.org/stable/index.html) was used as the base of most of the code.
75
+
76
+ A MedpipePipeline contains the preprocessing operations, a model for each prediction outcome, and a recalibration model (if specified) for each outcome. Thus, with only a few lines of code, several models can be created and fitted using the same data.
77
+
78
+ ### Preprocessing operations
79
+ Preprocessing operations are chained and applied sequentially. Available operations are listed in the [sklearn.preprocessing](https://scikit-learn.org/stable/api/sklearn.preprocessing.html) and [sklearn.impute](https://scikit-learn.org/stable/api/sklearn.impute.html) modules.
80
+
81
+ ### Models
82
+ Any model from the [sklearn.ensemble](https://scikit-learn.org/stable/api/sklearn.ensemble.html), [sklearn.linear_model](https://scikit-learn.org/stable/api/sklearn.linear_model.html), and [sklearn.isotonic](https://scikit-learn.org/stable/api/sklearn.isotonic.html) modules are available. Additionally, models from the [ngboost](https://stanfordmlgroup.github.io/ngboost/intro) package can be used.
83
+
84
+ **NOTE:** The package can handle binary classifiers and regressors. However, handling of regressors is not as developed yet.
85
+
86
+ ### Recalibration
87
+ Two recalibration models are available: logistic regression (Platt scaling), and isotonic regression.
88
+
89
+ ### Metrics
90
+ The list of available metrics is the following:
91
+
92
+ | Metric | Description |
93
+ | :--- | :--- |
94
+ | Accuracy | Proportion of all classifications that were correct. |
95
+ | Recall | Proportion of all actual positives that were classified correctly (true positive rate). |
96
+ | Precision | Proportion of all the positive classifications that are actually positive. |
97
+ | F1 score | Harmonic mean of precision and recall. |
98
+ | AUROC | Area under the ROC curve. |
99
+ | AP | Area under the precision-recall curve. |
100
+ | Log loss | Logarithmic loss. |
101
+ | Brier score | Brier score loss. |
102
+ | ICI | Integrated calibration index. |
103
+ | RMSE | Root mean squared error. |
104
+ | MAE| Mean absolute error. |
105
+
106
+ **NOTE:** Metrics can be added by editing the METRIC_MAPPING map in the metrics/core.py module.
107
+
108
+ ### Plots
109
+ There are four different plots that can be generated (mainly for classifiers):
110
+ 1. Probability distribution, histrogram of predicted probabilities;
111
+ 2. ROC curve, receiver operating characteristic curve;
112
+ 3. Reliability diagram, classifier calibration curve;
113
+ 4. Fairness heatmap, heatmap of differences for different strata to assess model bias.
114
+
115
+ ___
116
+ ## Examples
117
+
118
+ Minimal example based on configuration files:
119
+ ``` py linenums="1"
120
+ import medpipe as mp
121
+
122
+ config_path = "config-examples/HGBc_config.toml"
123
+ pipe = mp.MedpipePipeline(config_path, logger=None)
124
+ pipe.run() # Run pipeline based on configuration
125
+ pipe.save() # Save pipeline
126
+ ```
127
+
128
+ Example allowing the user more control over the data and plots:
129
+ ``` py linenums="1"
130
+ import medpipe as mp
131
+
132
+ config_path = "config-examples/HGBc_config.toml"
133
+ pipe = mp.MedpipePipeline(config_path, logger=None)
134
+ data = load_data(pipe.medpipe_config.data.path) # Load the data
135
+
136
+ # Get the different data sets
137
+ X_train, y_train, X_test, y_test, X_recal, y_recal, _ = pipe.get_data_sets(data)
138
+ pipe.fit(X_train, y_train, X_recal, y_recal) # Fit the pipeline
139
+
140
+ # Make predictions for each outcome and plot some figures
141
+ for outcome in pipe.outcomes:
142
+ y_preds = pipe.predict_proba(X_test, outcome)[0]
143
+ mp.plot_ROC_curve(y_test, y_preds, "ROC curve")
144
+ mp.plot_reliability_diagram(y_test, y_preds, "Calibration", strategy="quantile")
145
+
146
+ ```
@@ -0,0 +1,114 @@
1
+ # medpipe
2
+
3
+ ![GitHub License](https://img.shields.io/github/license/Surgical-Recovery-and-Safety-Lab/medpipe)
4
+ [![Python 3.12+](https://img.shields.io/badge/python-3.12+-green.svg)](https://www.python.org/downloads/)
5
+ [![tests](https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/actions/workflows/run_test.yml/badge.svg)](https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/actions/workflows/run_test.yml)
6
+ ## Table of content
7
+ 1. [Overview](#overview)
8
+ 2. [Installation](#installation)
9
+ 3. [Usage](#usage)
10
+ 1. [Preprocessing operations](#preprocess)
11
+ 2. [Models](#models)
12
+ 3. [Recalibration](#recalibration)
13
+ 4. [Metrics](#metrics)
14
+ 5. [Plots](#plots)
15
+ 5. [Examples](#examples)
16
+
17
+ ## Overview
18
+ The **medpipe** package is a layer to help create AI models (classifiers and regressors) for clinical applications from tabular data. It covers data loading and preprocessing, model creation and training, recalibration, and visualisation.
19
+ ___
20
+ ## Installation
21
+
22
+ To install **medpipe** use pip:
23
+ ```
24
+ $ pip install medpipe
25
+ ```
26
+
27
+ or clone the GitHub repository and install the package with pip:
28
+ ```
29
+ $ git clone git@github.com:Surgical-Recovery-and-Safety-Lab/medpipe.git
30
+ $ cd medpipe
31
+ $ pip install .
32
+ ```
33
+ **NOTE**: It is recommended to use a virtual environment (venv) to install this package.
34
+
35
+ Ensure that the installation was successful and that all tests pass by running the following command in the medpipe directory:
36
+ ```
37
+ $ pytest
38
+ ```
39
+ ___
40
+
41
+ ## Usage
42
+ This package was tested on a Linux distribution (Ubuntu 24.04) with Python v3.12.3. The [sckit-learn](https://scikit-learn.org/stable/index.html) was used as the base of most of the code.
43
+
44
+ A MedpipePipeline contains the preprocessing operations, a model for each prediction outcome, and a recalibration model (if specified) for each outcome. Thus, with only a few lines of code, several models can be created and fitted using the same data.
45
+
46
+ ### Preprocessing operations
47
+ Preprocessing operations are chained and applied sequentially. Available operations are listed in the [sklearn.preprocessing](https://scikit-learn.org/stable/api/sklearn.preprocessing.html) and [sklearn.impute](https://scikit-learn.org/stable/api/sklearn.impute.html) modules.
48
+
49
+ ### Models
50
+ Any model from the [sklearn.ensemble](https://scikit-learn.org/stable/api/sklearn.ensemble.html), [sklearn.linear_model](https://scikit-learn.org/stable/api/sklearn.linear_model.html), and [sklearn.isotonic](https://scikit-learn.org/stable/api/sklearn.isotonic.html) modules are available. Additionally, models from the [ngboost](https://stanfordmlgroup.github.io/ngboost/intro) package can be used.
51
+
52
+ **NOTE:** The package can handle binary classifiers and regressors. However, handling of regressors is not as developed yet.
53
+
54
+ ### Recalibration
55
+ Two recalibration models are available: logistic regression (Platt scaling), and isotonic regression.
56
+
57
+ ### Metrics
58
+ The list of available metrics is the following:
59
+
60
+ | Metric | Description |
61
+ | :--- | :--- |
62
+ | Accuracy | Proportion of all classifications that were correct. |
63
+ | Recall | Proportion of all actual positives that were classified correctly (true positive rate). |
64
+ | Precision | Proportion of all the positive classifications that are actually positive. |
65
+ | F1 score | Harmonic mean of precision and recall. |
66
+ | AUROC | Area under the ROC curve. |
67
+ | AP | Area under the precision-recall curve. |
68
+ | Log loss | Logarithmic loss. |
69
+ | Brier score | Brier score loss. |
70
+ | ICI | Integrated calibration index. |
71
+ | RMSE | Root mean squared error. |
72
+ | MAE| Mean absolute error. |
73
+
74
+ **NOTE:** Metrics can be added by editing the METRIC_MAPPING map in the metrics/core.py module.
75
+
76
+ ### Plots
77
+ There are four different plots that can be generated (mainly for classifiers):
78
+ 1. Probability distribution, histrogram of predicted probabilities;
79
+ 2. ROC curve, receiver operating characteristic curve;
80
+ 3. Reliability diagram, classifier calibration curve;
81
+ 4. Fairness heatmap, heatmap of differences for different strata to assess model bias.
82
+
83
+ ___
84
+ ## Examples
85
+
86
+ Minimal example based on configuration files:
87
+ ``` py linenums="1"
88
+ import medpipe as mp
89
+
90
+ config_path = "config-examples/HGBc_config.toml"
91
+ pipe = mp.MedpipePipeline(config_path, logger=None)
92
+ pipe.run() # Run pipeline based on configuration
93
+ pipe.save() # Save pipeline
94
+ ```
95
+
96
+ Example allowing the user more control over the data and plots:
97
+ ``` py linenums="1"
98
+ import medpipe as mp
99
+
100
+ config_path = "config-examples/HGBc_config.toml"
101
+ pipe = mp.MedpipePipeline(config_path, logger=None)
102
+ data = load_data(pipe.medpipe_config.data.path) # Load the data
103
+
104
+ # Get the different data sets
105
+ X_train, y_train, X_test, y_test, X_recal, y_recal, _ = pipe.get_data_sets(data)
106
+ pipe.fit(X_train, y_train, X_recal, y_recal) # Fit the pipeline
107
+
108
+ # Make predictions for each outcome and plot some figures
109
+ for outcome in pipe.outcomes:
110
+ y_preds = pipe.predict_proba(X_test, outcome)[0]
111
+ mp.plot_ROC_curve(y_test, y_preds, "ROC curve")
112
+ mp.plot_reliability_diagram(y_test, y_preds, "Calibration", strategy="quantile")
113
+
114
+ ```
@@ -1,18 +1,14 @@
1
- [build-system]
2
- build-backend = "setuptools.build_meta"
3
- requires = [ "setuptools>=80.9" ]
4
-
5
1
  [project]
6
2
  name = "medpipe"
7
- version = "0.2.2.dev0"
3
+ version = "0.3.1"
8
4
  description = "A package for clinical AI pipeline development"
9
5
  readme = "README.md"
6
+ requires-python = ">=3.12"
10
7
  license = "Apache-2.0"
11
- license-files = [ "LICEN[CS]E*" ]
8
+ license-files = ["LICEN[CS]E*"]
12
9
  authors = [
13
10
  { name = "Mathias Roesler", email = "mathias.roesler@auckland.co.nz" },
14
11
  ]
15
- requires-python = ">=3.12"
16
12
  classifiers = [
17
13
  "Operating System :: POSIX :: Linux",
18
14
  "Programming Language :: Python :: 3 :: Only",
@@ -20,10 +16,24 @@ classifiers = [
20
16
  "Programming Language :: Python :: 3.13",
21
17
  "Programming Language :: Python :: 3.14",
22
18
  ]
23
- dynamic = [ "dependencies" ]
24
- urls.Repository = "https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe"
19
+ dynamic = ["dependencies"]
20
+
21
+ [project.urls]
22
+ Changelog = "https://github.com/https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/blob/main/CHANGELOG.md"
23
+ Homepage = "https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe"
24
+
25
+ [build-system]
26
+ requires = ["setuptools>=80.9"]
27
+ build-backend = "setuptools.build_meta"
25
28
 
26
29
  [tool.setuptools]
27
30
  package-dir = { "" = "src" }
28
- packages = [ "medpipe", "medpipe.data", "medpipe.models", "medpipe.metrics", "medpipe.pipeline", "medpipe.utils" ]
29
- dynamic.dependencies = { file = [ "requirements.txt" ] }
31
+ packages = [
32
+ "medpipe",
33
+ "medpipe.data",
34
+ "medpipe.models",
35
+ "medpipe.metrics",
36
+ "medpipe.pipeline",
37
+ "medpipe.utils"
38
+ ]
39
+ dynamic.dependencies = { file = ["requirements.txt"] }
@@ -0,0 +1,14 @@
1
+ imbalanced_learn>=0.14.1
2
+ imblearn==0.0
3
+ matplotlib>=3.10.8
4
+ numpy>=2.4.2
5
+ pandas>=2.3.3
6
+ pytest>=9.0.1
7
+ pytest-mock>=3.15.1
8
+ scikit_learn==1.8.0
9
+ scipy>=1.17.0
10
+ pyarrow>=23.0.1
11
+ pydantic>=2.13.4
12
+ ml_insights>=1.1.0
13
+ ngboost>=0.5.10
14
+ pytest-mock>=3.15.1
@@ -0,0 +1,46 @@
1
+ """
2
+ medpipe module
3
+
4
+ submodules:
5
+ - data: contains data related functions
6
+ - metrics: contains model metric functions
7
+ - models: contains model creation functions
8
+ - pipeline: contains pipeline functions
9
+ - utils: contains utility functions
10
+ """
11
+
12
+ from .data import extract_labels
13
+ from .metrics import (
14
+ plot_probability_distribution,
15
+ plot_reliability_diagram,
16
+ plot_ROC_curve,
17
+ plot_strata_heatmap,
18
+ print_metrics,
19
+ )
20
+ from .models import load_pipeline, save_pipeline
21
+ from .pipeline.pipeline import MedpipePipeline
22
+ from .utils import (
23
+ exception_handler,
24
+ load_data,
25
+ print_message,
26
+ read_toml_configuration,
27
+ setup_logger,
28
+ )
29
+
30
+ __all__ = [
31
+ "MedpipePipeline",
32
+ "exception_handler",
33
+ "load_data",
34
+ "print_message",
35
+ "read_toml_configuration",
36
+ "setup_logger",
37
+ "load_pipeline",
38
+ "save_pipeline",
39
+ "plot_probability_distribution",
40
+ "plot_reliability_diagram",
41
+ "plot_ROC_curve",
42
+ "plot_strata_heatmap",
43
+ "print_metrics",
44
+ "extract_labels",
45
+ ]
46
+ __version__ = "v0.3.0"
@@ -0,0 +1,39 @@
1
+ """
2
+ Medpipe typings.
3
+
4
+ This module provides special types defined for the medpipe package.
5
+
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from typing import TYPE_CHECKING, Annotated, Literal, TypeAlias
11
+
12
+ import numpy as np
13
+ import numpy.typing as npt
14
+ import pandas as pd
15
+ from scipy.sparse import csr_array, csr_matrix
16
+
17
+ if TYPE_CHECKING:
18
+ from medpipe.utils.config import (
19
+ DataConfig,
20
+ HyperparameterConfig,
21
+ MedpipeConfig,
22
+ TopLevelConfig,
23
+ WorkflowConfig,
24
+ )
25
+
26
+ # ==============================================================================
27
+ # CORE PIPELINE & DATA TYPES
28
+ # ==============================================================================
29
+
30
+ # Define data and label types
31
+ SingleClassLabels: TypeAlias = Annotated[npt.NDArray[np.integer], Literal["N"]]
32
+ MultiClassLabels: TypeAlias = Annotated[npt.NDArray[np.integer], Literal["N", "C"]]
33
+ Labels: TypeAlias = SingleClassLabels | MultiClassLabels
34
+ TransformedData: TypeAlias = pd.DataFrame | npt.NDArray | csr_matrix | csr_array
35
+
36
+ # Define a generic config type as a union
37
+ Config: TypeAlias = (
38
+ "MedpipeConfig | TopLevelConfig | DataConfig | WorkflowConfig | HyperparameterConfig"
39
+ )
@@ -0,0 +1,9 @@
1
+ """
2
+ medpipe.data module
3
+
4
+ submodules:
5
+ - utils: utility functions for data manipulation.
6
+ """
7
+
8
+ from . import utils
9
+ from .utils import convert_dtypes, extract_labels, get_split_idx, split_data
@@ -0,0 +1,87 @@
1
+ """
2
+ Transformers functions module.
3
+
4
+ This module provides transformers for regressors.
5
+
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ from typing import TYPE_CHECKING
11
+
12
+ import numpy as np
13
+ from sklearn.base import BaseEstimator, TransformerMixin
14
+
15
+ if TYPE_CHECKING:
16
+ import numpy.typing as npt
17
+
18
+
19
+ class BoundedLogitTransformer(BaseEstimator, TransformerMixin):
20
+ """
21
+ Class that transforms data to safely bound it in the logit space.
22
+
23
+ Attributes
24
+ ----------
25
+ max_value : float, default: 1.0
26
+ Max value in the data, needed for back-transformation.
27
+ epsilon : float, default: 1e-7
28
+ Value for clipping to avoid log(0).
29
+
30
+ Methods
31
+ -------
32
+ fit(y)
33
+ Needed for compatibility.
34
+ transform(y)
35
+ Transform data from the original space into the logit space.
36
+ inverse_transform(y)
37
+ Transforms data from the logit space back to the original space.
38
+ """
39
+
40
+ def __init__(self, max_value: float = 1.0, epsilon: float = 1e-7) -> None:
41
+ self.max_value = max_value
42
+ self.epsilon = epsilon # To prevent log(0) or division by zero
43
+
44
+ def fit(self, y: npt.NDArray) -> "BoundedLogitTransformer":
45
+ """Needed for compatibility."""
46
+ return self
47
+
48
+ def transform(self, y: npt.NDArray) -> npt.NDArray:
49
+ """
50
+ Transform data from the original space into the
51
+ logit space.
52
+
53
+ The data is clipped to avoid log(0).
54
+
55
+ Parameters
56
+ ----------
57
+ y : npt.NDArray
58
+ Data to transform into the logit space.
59
+
60
+ Returns
61
+ -------
62
+ y_transformed : npt.NDArray
63
+ Transformed data in the logit space.
64
+
65
+ """
66
+ self.max_value = np.max(y) # Save max value
67
+ y_scaled = np.clip(y / self.max_value, self.epsilon, 1 - self.epsilon)
68
+ return np.log(y_scaled / (1 - y_scaled))
69
+
70
+ def inverse_transform(self, y: npt.NDArray) -> npt.NDArray:
71
+ """
72
+ Transforms data from the logit space back to the
73
+ original space.
74
+
75
+ Parameters
76
+ ----------
77
+ y : npt.NDArray
78
+ Values in the logit space to be transformed.
79
+
80
+ Returns
81
+ -------
82
+ y_transformed : npt.NDArray
83
+ Back-transformed y into the original space.
84
+
85
+ """
86
+ exp_y = np.exp(y)
87
+ return (exp_y / (1 + exp_y)) * self.max_value