medpipe 0.0.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.
- medpipe-0.0.1/LICENSE +201 -0
- medpipe-0.0.1/PKG-INFO +152 -0
- medpipe-0.0.1/README.md +126 -0
- medpipe-0.0.1/pyproject.toml +29 -0
- medpipe-0.0.1/requirements.txt +9 -0
- medpipe-0.0.1/setup.cfg +4 -0
- medpipe-0.0.1/src/medpipe/__init__.py +54 -0
- medpipe-0.0.1/src/medpipe/data/Preprocessor.py +179 -0
- medpipe-0.0.1/src/medpipe/data/__init__.py +14 -0
- medpipe-0.0.1/src/medpipe/data/db.py +114 -0
- medpipe-0.0.1/src/medpipe/data/preprocessing.py +283 -0
- medpipe-0.0.1/src/medpipe/data/sampler.py +561 -0
- medpipe-0.0.1/src/medpipe/data/weighting.py +241 -0
- medpipe-0.0.1/src/medpipe/metrics/__init__.py +21 -0
- medpipe-0.0.1/src/medpipe/metrics/core.py +395 -0
- medpipe-0.0.1/src/medpipe/metrics/plots.py +308 -0
- medpipe-0.0.1/src/medpipe/models/Calibrator.py +147 -0
- medpipe-0.0.1/src/medpipe/models/Predictor.py +144 -0
- medpipe-0.0.1/src/medpipe/models/__init__.py +11 -0
- medpipe-0.0.1/src/medpipe/models/core.py +266 -0
- medpipe-0.0.1/src/medpipe/pipeline/Pipeline.py +823 -0
- medpipe-0.0.1/src/medpipe/pipeline/__init__.py +8 -0
- medpipe-0.0.1/src/medpipe/utils/__init__.py +13 -0
- medpipe-0.0.1/src/medpipe/utils/config.py +199 -0
- medpipe-0.0.1/src/medpipe/utils/exceptions.py +179 -0
- medpipe-0.0.1/src/medpipe/utils/io.py +102 -0
- medpipe-0.0.1/src/medpipe/utils/logger.py +149 -0
- medpipe-0.0.1/src/medpipe.egg-info/PKG-INFO +152 -0
- medpipe-0.0.1/src/medpipe.egg-info/SOURCES.txt +42 -0
- medpipe-0.0.1/src/medpipe.egg-info/dependency_links.txt +1 -0
- medpipe-0.0.1/src/medpipe.egg-info/requires.txt +9 -0
- medpipe-0.0.1/src/medpipe.egg-info/top_level.txt +1 -0
- medpipe-0.0.1/test/test_data_Preprocessor.py +99 -0
- medpipe-0.0.1/test/test_data_db.py +90 -0
- medpipe-0.0.1/test/test_data_preprocessing.py +166 -0
- medpipe-0.0.1/test/test_data_sampler.py +477 -0
- medpipe-0.0.1/test/test_data_weighting.py +253 -0
- medpipe-0.0.1/test/test_metrics_core.py +44 -0
- medpipe-0.0.1/test/test_models_core.py +84 -0
- medpipe-0.0.1/test/test_pipeline_Pipeline.py +70 -0
- medpipe-0.0.1/test/test_utils_config.py +224 -0
- medpipe-0.0.1/test/test_utils_exceptions.py +211 -0
- medpipe-0.0.1/test/test_utils_io.py +76 -0
- medpipe-0.0.1/test/test_utils_logger.py +44 -0
medpipe-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
medpipe-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: medpipe
|
|
3
|
+
Version: 0.0.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: Repository, https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe
|
|
8
|
+
Classifier: Operating System :: Unix
|
|
9
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
License-File: LICENSE
|
|
16
|
+
Requires-Dist: imbalanced_learn==0.14.1
|
|
17
|
+
Requires-Dist: imblearn==0.0
|
|
18
|
+
Requires-Dist: matplotlib==3.10.8
|
|
19
|
+
Requires-Dist: numpy==2.4.2
|
|
20
|
+
Requires-Dist: pandas==2.3.3
|
|
21
|
+
Requires-Dist: pytest==9.0.1
|
|
22
|
+
Requires-Dist: scikit_learn==1.8.0
|
|
23
|
+
Requires-Dist: scipy==1.17.0
|
|
24
|
+
Requires-Dist: pyarrow==23.0.1
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
# medpipe
|
|
28
|
+
|
|
29
|
+

|
|
30
|
+
[](https://www.python.org/downloads/)
|
|
31
|
+
[](https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/actions/workflows/run_test.yml)
|
|
32
|
+
## Table of content
|
|
33
|
+
1. [Overview](#overview)
|
|
34
|
+
2. [Installation](#installation)
|
|
35
|
+
3. [Usage](#usage)
|
|
36
|
+
1. [Preprocessing operations](#preprocess)
|
|
37
|
+
2. [Models](#models)
|
|
38
|
+
3. [Recalibration](#recalibration)
|
|
39
|
+
4. [Metrics](#metrics)
|
|
40
|
+
5. [Plots](#plots)
|
|
41
|
+
5. [Examples](#examples)
|
|
42
|
+
|
|
43
|
+
## Overview
|
|
44
|
+
The **medpipe** package is a layer to help create AI models for clinical applications from tabular data. It covers data loading and preprocessing, model creation and training, recalibration, and visualisation.
|
|
45
|
+
___
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
To install **medpipe** clone the GitHub repository and install the package with pip:
|
|
49
|
+
```
|
|
50
|
+
$ git clone git@github.com:Surgical-Recovery-and-Safety-Lab/medpipe.git
|
|
51
|
+
$ cd medpipe
|
|
52
|
+
$ pip install .
|
|
53
|
+
```
|
|
54
|
+
**NOTE**: It is recommended to use a virtual environment (venv) to install this package.
|
|
55
|
+
|
|
56
|
+
Ensure that the installation was succesfull and that all tests pass by running the following command in the medpipe directory:
|
|
57
|
+
```
|
|
58
|
+
$ pytest
|
|
59
|
+
```
|
|
60
|
+
___
|
|
61
|
+
|
|
62
|
+
## Usage
|
|
63
|
+
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.
|
|
64
|
+
|
|
65
|
+
A Pipeline contains the preprocessing operations, a model for each prediction label, and a recalibration model (if specified) for each label. Thus, with only a few lines of code, several models can be created from the same data and fitted.
|
|
66
|
+
|
|
67
|
+
### Preprocessing operations
|
|
68
|
+
Currently four preprocessing operations are available:
|
|
69
|
+
* **standarise**, this operation standardises the input features by removing the mean and scaling to unit variance;
|
|
70
|
+
* **ordinal encoding**, this operation converts non-numerical categorical input features into ordinal ones;
|
|
71
|
+
* **power transform**, this operation applies a power transform to make the data more Gaussian-like;
|
|
72
|
+
* **binning**, this operation converts a continuous input feature into bins and caps the value.
|
|
73
|
+
|
|
74
|
+
### Models
|
|
75
|
+
There is only one classifier available at the moment: the histogram boosted gradient classifier.
|
|
76
|
+
|
|
77
|
+
**NOTE:** Adding a new model only requires editing the **create_model** function in *models/core*. To work, it must have a fit and predict method.
|
|
78
|
+
|
|
79
|
+
### Recalibration
|
|
80
|
+
Two recalibration models are available: logistic regression, and isotonic regression.
|
|
81
|
+
|
|
82
|
+
### Metrics
|
|
83
|
+
The available metrics are divided into the score metrics and prediction metrics. The list of available metrics is the following:
|
|
84
|
+
|
|
85
|
+
| Metric | Type | Description |
|
|
86
|
+
| :--- | :--- | :--- |
|
|
87
|
+
| Accuracy | Prediction | Proportion of all classifications that were correct. |
|
|
88
|
+
| Recall | Prediction | Proportion of all actual positives that were classified correctly (true positive rate). |
|
|
89
|
+
| Precision | Prediction | Proportion of all the positive classifications that are actually positive. |
|
|
90
|
+
| F1 score | Prediction | Harmonic mean of precision and recall. |
|
|
91
|
+
| AUROC | Score | Area under the ROC curve. |
|
|
92
|
+
| AP | Score | Area under the precision-recall curve. |
|
|
93
|
+
| Log loss | Score | Logarithmic loss. |
|
|
94
|
+
|
|
95
|
+
### Plots
|
|
96
|
+
|
|
97
|
+
Three types of plots are available: bar graphs for the metrics, predicted probability distributions, and calibration curves.
|
|
98
|
+
|
|
99
|
+
The following graphs are from one pipeline with two models, one to predict complications and the other to predict 90-day mortality. The predictor and calibrator results are plotted on the same graphs to compare the effect of recalibration.
|
|
100
|
+
|
|
101
|
+
Plots of the AUROC and log loss metric values with confidence intervals for each outcome:
|
|
102
|
+
|
|
103
|
+
| Any complication | 90-day mortality |
|
|
104
|
+
| :---: | :---: |
|
|
105
|
+
|  |  |
|
|
106
|
+
|  |  |
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
Predicted probability distributions for each outcome:
|
|
110
|
+
|
|
111
|
+
| Any complication | 90-day mortality |
|
|
112
|
+
| :---: | :---: |
|
|
113
|
+
|  |  |
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
Calibration curves for each outcome:
|
|
117
|
+
|
|
118
|
+
| Any complication | 90-day mortality |
|
|
119
|
+
| :---: | :---: |
|
|
120
|
+
|  |  |
|
|
121
|
+
___
|
|
122
|
+
## Example
|
|
123
|
+
|
|
124
|
+
Here is a short example that shows how to load data, train the models, and plot the calibration curves:
|
|
125
|
+
|
|
126
|
+
``` py linenums="1"
|
|
127
|
+
from medpipe import (
|
|
128
|
+
Pipeline
|
|
129
|
+
read_toml_configuration,
|
|
130
|
+
load_data_from_csv,
|
|
131
|
+
get_positive_proba,
|
|
132
|
+
extract_labels,
|
|
133
|
+
plot_reliability_diagrams,
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
# Load configuration and data
|
|
137
|
+
config = read_toml_configuration("config_file.toml")
|
|
138
|
+
data = load_data_from_csv("data.csv")
|
|
139
|
+
|
|
140
|
+
# Create pipeline
|
|
141
|
+
pipeline = Pipeline(general_config)
|
|
142
|
+
|
|
143
|
+
# Split data into sets and train model
|
|
144
|
+
X_train, X_test = pipeline.get_test_data(data)
|
|
145
|
+
pipeline.run(X_train)
|
|
146
|
+
|
|
147
|
+
# Plot calibration curve
|
|
148
|
+
X_test, y_test = extract_labels(X_test, pipeline.label_list)
|
|
149
|
+
y_pred_proba = pipeline.predict_proba(X_test)
|
|
150
|
+
plot_reliability_diagrams(y_test, get_positive_proba(y_pred_proba, display_kwargs={"n_bins": 10, "strategy": "quantile"})
|
|
151
|
+
|
|
152
|
+
```
|
medpipe-0.0.1/README.md
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# medpipe
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://www.python.org/downloads/)
|
|
5
|
+
[](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 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** clone the GitHub repository and install the package with pip:
|
|
23
|
+
```
|
|
24
|
+
$ git clone git@github.com:Surgical-Recovery-and-Safety-Lab/medpipe.git
|
|
25
|
+
$ cd medpipe
|
|
26
|
+
$ pip install .
|
|
27
|
+
```
|
|
28
|
+
**NOTE**: It is recommended to use a virtual environment (venv) to install this package.
|
|
29
|
+
|
|
30
|
+
Ensure that the installation was succesfull and that all tests pass by running the following command in the medpipe directory:
|
|
31
|
+
```
|
|
32
|
+
$ pytest
|
|
33
|
+
```
|
|
34
|
+
___
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
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.
|
|
38
|
+
|
|
39
|
+
A Pipeline contains the preprocessing operations, a model for each prediction label, and a recalibration model (if specified) for each label. Thus, with only a few lines of code, several models can be created from the same data and fitted.
|
|
40
|
+
|
|
41
|
+
### Preprocessing operations
|
|
42
|
+
Currently four preprocessing operations are available:
|
|
43
|
+
* **standarise**, this operation standardises the input features by removing the mean and scaling to unit variance;
|
|
44
|
+
* **ordinal encoding**, this operation converts non-numerical categorical input features into ordinal ones;
|
|
45
|
+
* **power transform**, this operation applies a power transform to make the data more Gaussian-like;
|
|
46
|
+
* **binning**, this operation converts a continuous input feature into bins and caps the value.
|
|
47
|
+
|
|
48
|
+
### Models
|
|
49
|
+
There is only one classifier available at the moment: the histogram boosted gradient classifier.
|
|
50
|
+
|
|
51
|
+
**NOTE:** Adding a new model only requires editing the **create_model** function in *models/core*. To work, it must have a fit and predict method.
|
|
52
|
+
|
|
53
|
+
### Recalibration
|
|
54
|
+
Two recalibration models are available: logistic regression, and isotonic regression.
|
|
55
|
+
|
|
56
|
+
### Metrics
|
|
57
|
+
The available metrics are divided into the score metrics and prediction metrics. The list of available metrics is the following:
|
|
58
|
+
|
|
59
|
+
| Metric | Type | Description |
|
|
60
|
+
| :--- | :--- | :--- |
|
|
61
|
+
| Accuracy | Prediction | Proportion of all classifications that were correct. |
|
|
62
|
+
| Recall | Prediction | Proportion of all actual positives that were classified correctly (true positive rate). |
|
|
63
|
+
| Precision | Prediction | Proportion of all the positive classifications that are actually positive. |
|
|
64
|
+
| F1 score | Prediction | Harmonic mean of precision and recall. |
|
|
65
|
+
| AUROC | Score | Area under the ROC curve. |
|
|
66
|
+
| AP | Score | Area under the precision-recall curve. |
|
|
67
|
+
| Log loss | Score | Logarithmic loss. |
|
|
68
|
+
|
|
69
|
+
### Plots
|
|
70
|
+
|
|
71
|
+
Three types of plots are available: bar graphs for the metrics, predicted probability distributions, and calibration curves.
|
|
72
|
+
|
|
73
|
+
The following graphs are from one pipeline with two models, one to predict complications and the other to predict 90-day mortality. The predictor and calibrator results are plotted on the same graphs to compare the effect of recalibration.
|
|
74
|
+
|
|
75
|
+
Plots of the AUROC and log loss metric values with confidence intervals for each outcome:
|
|
76
|
+
|
|
77
|
+
| Any complication | 90-day mortality |
|
|
78
|
+
| :---: | :---: |
|
|
79
|
+
|  |  |
|
|
80
|
+
|  |  |
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
Predicted probability distributions for each outcome:
|
|
84
|
+
|
|
85
|
+
| Any complication | 90-day mortality |
|
|
86
|
+
| :---: | :---: |
|
|
87
|
+
|  |  |
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Calibration curves for each outcome:
|
|
91
|
+
|
|
92
|
+
| Any complication | 90-day mortality |
|
|
93
|
+
| :---: | :---: |
|
|
94
|
+
|  |  |
|
|
95
|
+
___
|
|
96
|
+
## Example
|
|
97
|
+
|
|
98
|
+
Here is a short example that shows how to load data, train the models, and plot the calibration curves:
|
|
99
|
+
|
|
100
|
+
``` py linenums="1"
|
|
101
|
+
from medpipe import (
|
|
102
|
+
Pipeline
|
|
103
|
+
read_toml_configuration,
|
|
104
|
+
load_data_from_csv,
|
|
105
|
+
get_positive_proba,
|
|
106
|
+
extract_labels,
|
|
107
|
+
plot_reliability_diagrams,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
# Load configuration and data
|
|
111
|
+
config = read_toml_configuration("config_file.toml")
|
|
112
|
+
data = load_data_from_csv("data.csv")
|
|
113
|
+
|
|
114
|
+
# Create pipeline
|
|
115
|
+
pipeline = Pipeline(general_config)
|
|
116
|
+
|
|
117
|
+
# Split data into sets and train model
|
|
118
|
+
X_train, X_test = pipeline.get_test_data(data)
|
|
119
|
+
pipeline.run(X_train)
|
|
120
|
+
|
|
121
|
+
# Plot calibration curve
|
|
122
|
+
X_test, y_test = extract_labels(X_test, pipeline.label_list)
|
|
123
|
+
y_pred_proba = pipeline.predict_proba(X_test)
|
|
124
|
+
plot_reliability_diagrams(y_test, get_positive_proba(y_pred_proba, display_kwargs={"n_bins": 10, "strategy": "quantile"})
|
|
125
|
+
|
|
126
|
+
```
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "setuptools.build_meta"
|
|
3
|
+
requires = [ "setuptools>=80.9" ]
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "medpipe"
|
|
7
|
+
version = "0.0.1"
|
|
8
|
+
description = "A package for clinical AI pipeline development"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = [ "LICEN[CS]E*" ]
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Mathias Roesler", email = "mathias.roesler@auckland.co.nz" },
|
|
14
|
+
]
|
|
15
|
+
requires-python = ">=3.12"
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Operating System :: Unix",
|
|
18
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
19
|
+
"Programming Language :: Python :: 3.12",
|
|
20
|
+
"Programming Language :: Python :: 3.13",
|
|
21
|
+
"Programming Language :: Python :: 3.14",
|
|
22
|
+
]
|
|
23
|
+
dynamic = [ "dependencies" ]
|
|
24
|
+
urls.Repository = "https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe"
|
|
25
|
+
|
|
26
|
+
[tool.setuptools]
|
|
27
|
+
package-dir = { "" = "src" }
|
|
28
|
+
packages = [ "medpipe", "medpipe.data", "medpipe.models", "medpipe.metrics", "medpipe.pipeline", "medpipe.utils" ]
|
|
29
|
+
dynamic.dependencies = { file = [ "requirements.txt" ] }
|
medpipe-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
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 db, extract_data_from_db, extract_labels, preprocessing
|
|
13
|
+
from .metrics import (
|
|
14
|
+
compute_all_CI,
|
|
15
|
+
compute_pred_metrics,
|
|
16
|
+
compute_score_metrics,
|
|
17
|
+
plot_metrics_CI,
|
|
18
|
+
plot_prediction_distribution,
|
|
19
|
+
plot_reliability_diagrams,
|
|
20
|
+
print_metrics,
|
|
21
|
+
print_metrics_CI,
|
|
22
|
+
)
|
|
23
|
+
from .models import get_full_proba, get_positive_proba, load_pipeline, save_pipeline
|
|
24
|
+
from .pipeline.Pipeline import Pipeline
|
|
25
|
+
from .utils import (
|
|
26
|
+
exception_handler,
|
|
27
|
+
load_data_from_csv,
|
|
28
|
+
print_message,
|
|
29
|
+
read_toml_configuration,
|
|
30
|
+
setup_logger,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"Pipeline",
|
|
35
|
+
"exception_handler",
|
|
36
|
+
"load_data_from_csv",
|
|
37
|
+
"print_message",
|
|
38
|
+
"read_toml_configuration",
|
|
39
|
+
"setup_logger",
|
|
40
|
+
"get_full_proba",
|
|
41
|
+
"get_positive_proba",
|
|
42
|
+
"load_pipeline",
|
|
43
|
+
"save_pipeline",
|
|
44
|
+
"compute_all_CI",
|
|
45
|
+
"compute_pred_metrics",
|
|
46
|
+
"compute_score_metrics",
|
|
47
|
+
"plot_metrics_CI",
|
|
48
|
+
"plot_prediction_distribution",
|
|
49
|
+
"plot_reliability_diagrams",
|
|
50
|
+
"print_metrics",
|
|
51
|
+
"print_metrics_CI",
|
|
52
|
+
"extract_data_from_db",
|
|
53
|
+
"extract_labels",
|
|
54
|
+
]
|