medpipe 0.3.0__tar.gz → 0.4.0.dev2__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.3.0 → medpipe-0.4.0.dev2}/LICENSE +1 -1
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/PKG-INFO +26 -16
- medpipe-0.4.0.dev2/pyproject.toml +93 -0
- medpipe-0.4.0.dev2/src/medpipe/__init__.py +41 -0
- medpipe-0.4.0.dev2/src/medpipe/data/__init__.py +26 -0
- medpipe-0.4.0.dev2/src/medpipe/data/registry.py +19 -0
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/src/medpipe/data/utils.py +63 -46
- medpipe-0.4.0.dev2/src/medpipe/metrics/__init__.py +31 -0
- medpipe-0.4.0.dev2/src/medpipe/metrics/core.py +650 -0
- medpipe-0.4.0.dev2/src/medpipe/metrics/registry.py +136 -0
- medpipe-0.4.0.dev2/src/medpipe/models/__init__.py +15 -0
- medpipe-0.4.0.dev2/src/medpipe/models/registry.py +25 -0
- medpipe-0.4.0.dev2/src/medpipe/pipeline/__init__.py +29 -0
- medpipe-0.4.0.dev2/src/medpipe/pipeline/estimator.py +98 -0
- medpipe-0.4.0.dev2/src/medpipe/pipeline/evaluator.py +902 -0
- medpipe-0.4.0.dev2/src/medpipe/pipeline/orchestrator.py +733 -0
- medpipe-0.4.0.dev2/src/medpipe/pipeline/pipeline.py +1252 -0
- medpipe-0.4.0.dev2/src/medpipe/pipeline/runner.py +826 -0
- medpipe-0.4.0.dev2/src/medpipe/utils/__init__.py +41 -0
- medpipe-0.4.0.dev2/src/medpipe/utils/config.py +1703 -0
- medpipe-0.4.0.dev2/src/medpipe/utils/io.py +190 -0
- medpipe-0.4.0.dev2/src/medpipe/utils/logger.py +196 -0
- medpipe-0.4.0.dev2/src/medpipe/utils/registry.py +104 -0
- medpipe-0.4.0.dev2/src/medpipe/utils/reproducibility.py +303 -0
- medpipe-0.3.0/src/medpipe/utils/exceptions.py → medpipe-0.4.0.dev2/src/medpipe/utils/validation.py +6 -82
- medpipe-0.4.0.dev2/src/medpipe/visualisation/__init__.py +27 -0
- medpipe-0.4.0.dev2/src/medpipe/visualisation/displayer.py +2432 -0
- medpipe-0.4.0.dev2/src/medpipe/visualisation/plots.py +1332 -0
- medpipe-0.4.0.dev2/src/medpipe/visualisation/themes.py +134 -0
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/src/medpipe.egg-info/PKG-INFO +26 -16
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/src/medpipe.egg-info/SOURCES.txt +15 -8
- medpipe-0.4.0.dev2/src/medpipe.egg-info/requires.txt +17 -0
- medpipe-0.3.0/pyproject.toml +0 -36
- medpipe-0.3.0/requirements.txt +0 -14
- medpipe-0.3.0/src/medpipe/__init__.py +0 -46
- medpipe-0.3.0/src/medpipe/_types.py +0 -39
- medpipe-0.3.0/src/medpipe/data/__init__.py +0 -9
- medpipe-0.3.0/src/medpipe/data/transformers.py +0 -87
- medpipe-0.3.0/src/medpipe/metrics/__init__.py +0 -16
- medpipe-0.3.0/src/medpipe/metrics/core.py +0 -364
- medpipe-0.3.0/src/medpipe/metrics/plots.py +0 -722
- medpipe-0.3.0/src/medpipe/models/__init__.py +0 -9
- medpipe-0.3.0/src/medpipe/models/core.py +0 -157
- medpipe-0.3.0/src/medpipe/pipeline/__init__.py +0 -8
- medpipe-0.3.0/src/medpipe/pipeline/pipeline.py +0 -1519
- medpipe-0.3.0/src/medpipe/py.typed +0 -0
- medpipe-0.3.0/src/medpipe/utils/__init__.py +0 -13
- medpipe-0.3.0/src/medpipe/utils/config.py +0 -542
- medpipe-0.3.0/src/medpipe/utils/io.py +0 -126
- medpipe-0.3.0/src/medpipe/utils/logger.py +0 -156
- medpipe-0.3.0/src/medpipe.egg-info/requires.txt +0 -14
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/README.md +0 -0
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/setup.cfg +0 -0
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/src/medpipe.egg-info/dependency_links.txt +0 -0
- {medpipe-0.3.0 → medpipe-0.4.0.dev2}/src/medpipe.egg-info/top_level.txt +0 -0
|
@@ -186,7 +186,7 @@
|
|
|
186
186
|
same "printed page" as the copyright notice for easier
|
|
187
187
|
identification within third-party archives.
|
|
188
188
|
|
|
189
|
-
Copyright
|
|
189
|
+
Copyright 2026 Surgical Recovery and Safety Lab & Mathias Roesler
|
|
190
190
|
|
|
191
191
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
192
|
you may not use this file except in compliance with the License.
|
|
@@ -1,32 +1,42 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: medpipe
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.4.0.dev2
|
|
4
|
+
Summary: Clinical AI pipeline developement following TRIPOD+AI guidelines
|
|
5
5
|
Author-email: Mathias Roesler <mathias.roesler@auckland.co.nz>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
7
|
-
Project-URL:
|
|
7
|
+
Project-URL: Changelog, 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
|
+
Project-URL: Repository, https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe.git
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Healthcare Industry
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
8
13
|
Classifier: Operating System :: POSIX :: Linux
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
15
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
10
16
|
Classifier: Programming Language :: Python :: 3.12
|
|
11
17
|
Classifier: Programming Language :: Python :: 3.13
|
|
12
18
|
Classifier: Programming Language :: Python :: 3.14
|
|
19
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
13
21
|
Requires-Python: >=3.12
|
|
14
22
|
Description-Content-Type: text/markdown
|
|
15
23
|
License-File: LICENSE
|
|
16
|
-
Requires-Dist:
|
|
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: pytest-mock>=3.15.1
|
|
23
|
-
Requires-Dist: scikit_learn==1.8.0
|
|
24
|
-
Requires-Dist: scipy>=1.17.0
|
|
25
|
-
Requires-Dist: pyarrow>=23.0.1
|
|
26
|
-
Requires-Dist: pydantic>=2.13.4
|
|
27
|
-
Requires-Dist: ml_insights>=1.1.0
|
|
24
|
+
Requires-Dist: matplotlib>=3.10.0
|
|
28
25
|
Requires-Dist: ngboost>=0.5.10
|
|
29
|
-
Requires-Dist:
|
|
26
|
+
Requires-Dist: numpy>=2.0.0
|
|
27
|
+
Requires-Dist: ordboost>=0.3.1
|
|
28
|
+
Requires-Dist: pandas>=2.2.0
|
|
29
|
+
Requires-Dist: pyarrow>=15.0.0
|
|
30
|
+
Requires-Dist: pydantic>=2.0.0
|
|
31
|
+
Requires-Dist: scikit-learn>=1.8.0
|
|
32
|
+
Requires-Dist: scipy>=1.12.0
|
|
33
|
+
Requires-Dist: scores>=2.6.0
|
|
34
|
+
Requires-Dist: splinecalib>=0.0.13
|
|
35
|
+
Provides-Extra: dev
|
|
36
|
+
Requires-Dist: openpyxl>=3.1.0; extra == "dev"
|
|
37
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
38
|
+
Requires-Dist: pytest-mock>=3.12.0; extra == "dev"
|
|
39
|
+
Requires-Dist: ruff>=0.16.7; extra == "dev"
|
|
30
40
|
Dynamic: license-file
|
|
31
41
|
|
|
32
42
|
# medpipe
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "medpipe"
|
|
3
|
+
description = "Clinical AI pipeline developement following TRIPOD+AI guidelines"
|
|
4
|
+
readme = "README.md"
|
|
5
|
+
requires-python = ">=3.12"
|
|
6
|
+
license = "Apache-2.0"
|
|
7
|
+
license-files = ["LICEN[CS]E*"]
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Mathias Roesler", email = "mathias.roesler@auckland.co.nz" },
|
|
10
|
+
]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 4 - Beta",
|
|
13
|
+
"Intended Audience :: Healthcare Industry",
|
|
14
|
+
"Intended Audience :: Science/Research",
|
|
15
|
+
"Operating System :: POSIX :: Linux",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
18
|
+
"Programming Language :: Python :: 3.12",
|
|
19
|
+
"Programming Language :: Python :: 3.13",
|
|
20
|
+
"Programming Language :: Python :: 3.14",
|
|
21
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
|
22
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
23
|
+
]
|
|
24
|
+
dynamic = ["version"]
|
|
25
|
+
|
|
26
|
+
# Core runtime dependencies
|
|
27
|
+
dependencies = [
|
|
28
|
+
"matplotlib>=3.10.0",
|
|
29
|
+
"ngboost>=0.5.10",
|
|
30
|
+
"numpy>=2.0.0",
|
|
31
|
+
"ordboost>=0.3.1",
|
|
32
|
+
"pandas>=2.2.0",
|
|
33
|
+
"pyarrow>=15.0.0",
|
|
34
|
+
"pydantic>=2.0.0",
|
|
35
|
+
"scikit-learn>=1.8.0",
|
|
36
|
+
"scipy>=1.12.0",
|
|
37
|
+
"scores>=2.6.0",
|
|
38
|
+
"splinecalib>=0.0.13",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Changelog = "https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe/blob/main/CHANGELOG.md"
|
|
43
|
+
Homepage = "https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe"
|
|
44
|
+
Repository = "https://github.com/Surgical-Recovery-and-Safety-Lab/medpipe.git"
|
|
45
|
+
|
|
46
|
+
# Development and testing dependencies
|
|
47
|
+
[project.optional-dependencies]
|
|
48
|
+
dev = [
|
|
49
|
+
"openpyxl>=3.1.0",
|
|
50
|
+
"pytest>=8.0.0",
|
|
51
|
+
"pytest-mock>=3.12.0",
|
|
52
|
+
"ruff>=0.16.7",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[build-system]
|
|
56
|
+
requires = ["setuptools>=61.0"]
|
|
57
|
+
build-backend = "setuptools.build_meta"
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
addopts = "-ra -q"
|
|
61
|
+
minversion = "8.0"
|
|
62
|
+
testpaths = ["tests"]
|
|
63
|
+
filterwarnings = [
|
|
64
|
+
# pandas' own feather I/O calls pyarrow's deprecated feather.write_feather /
|
|
65
|
+
# read_feather instead of the new pyarrow.ipc API. This is pandas-internal,
|
|
66
|
+
# not something medpipe controls, and will resolve itself once pandas
|
|
67
|
+
# updates its feather backend.
|
|
68
|
+
"ignore:pyarrow\\.feather\\.(write|read)_feather is deprecated.*:FutureWarning",
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
[tool.setuptools.packages.find]
|
|
72
|
+
where = ["src"]
|
|
73
|
+
|
|
74
|
+
[tool.setuptools.dynamic]
|
|
75
|
+
version = { attr = "medpipe.__version__" }
|
|
76
|
+
|
|
77
|
+
[tool.ruff]
|
|
78
|
+
target-version = "py312"
|
|
79
|
+
line-length = 88
|
|
80
|
+
src = ["src"]
|
|
81
|
+
|
|
82
|
+
[tool.ruff.lint]
|
|
83
|
+
select = [
|
|
84
|
+
"E", # pycodestyle errors
|
|
85
|
+
"W", # pycodestyle warnings
|
|
86
|
+
"F", # Pyflakes
|
|
87
|
+
"I", # isort
|
|
88
|
+
"UP", # pyupgrade
|
|
89
|
+
"B", # flake8-bugbear
|
|
90
|
+
"C4", # flake8-comprehensions
|
|
91
|
+
"SIM", # flake8-simplify
|
|
92
|
+
"RUF", # Ruff-specific rules
|
|
93
|
+
]
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Medpipe: A Python framework for clinical machine learning pipeline
|
|
3
|
+
orchestration.
|
|
4
|
+
|
|
5
|
+
Provides unified high-level interfaces for data preparation, model fitting,
|
|
6
|
+
post-hoc calibration, TRIPOD+AI compliant evaluation, and reproducibility tracking.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from medpipe.metrics import MetricRegistry
|
|
10
|
+
from medpipe.models import ModelRegistry
|
|
11
|
+
from medpipe.pipeline import (
|
|
12
|
+
MedpipeClassifier,
|
|
13
|
+
MedpipeClassifierEvaluator,
|
|
14
|
+
MedpipeClassifierRunner,
|
|
15
|
+
MedpipeOrchestrator,
|
|
16
|
+
MedpipeRegressor,
|
|
17
|
+
MedpipeRegressorEvaluator,
|
|
18
|
+
MedpipeRegressorRunner,
|
|
19
|
+
)
|
|
20
|
+
from medpipe.utils import MedpipeClassifierConfig, MedpipeRegressorConfig
|
|
21
|
+
|
|
22
|
+
__version__ = "0.4.0.dev2"
|
|
23
|
+
|
|
24
|
+
__all__ = [ # noqa: RUF022 (grouped by category, not alphabetical)
|
|
25
|
+
# Primary API Entry Points
|
|
26
|
+
"MedpipeClassifier",
|
|
27
|
+
"MedpipeClassifierConfig",
|
|
28
|
+
"MedpipeRegressor",
|
|
29
|
+
"MedpipeRegressorConfig",
|
|
30
|
+
# Sub-Orchestrators (for custom/modular workflows)
|
|
31
|
+
"MedpipeOrchestrator",
|
|
32
|
+
"MedpipeClassifierRunner",
|
|
33
|
+
"MedpipeClassifierEvaluator",
|
|
34
|
+
"MedpipeRegressorRunner",
|
|
35
|
+
"MedpipeRegressorEvaluator",
|
|
36
|
+
# Component Registries (for custom models & metrics)
|
|
37
|
+
"ModelRegistry",
|
|
38
|
+
"MetricRegistry",
|
|
39
|
+
# Package Metadata
|
|
40
|
+
"__version__",
|
|
41
|
+
]
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""
|
|
2
|
+
medpipe.data
|
|
3
|
+
------------
|
|
4
|
+
Data processing, label extraction, dataset splitting, and transformation utilities.
|
|
5
|
+
|
|
6
|
+
Provides functions for label manipulation, train/test/recalibration splitting,
|
|
7
|
+
subgroup mask resolution, and preprocessor registry management.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from medpipe.data.registry import PreprocessorRegistry
|
|
11
|
+
from medpipe.data.utils import (
|
|
12
|
+
extract_labels,
|
|
13
|
+
get_split_idx,
|
|
14
|
+
resolve_subgroup_mask,
|
|
15
|
+
split_data,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
__all__ = [ # noqa: RUF022 (grouped by category, not alphabetical)
|
|
19
|
+
# Data manipulation & split utilities
|
|
20
|
+
"extract_labels",
|
|
21
|
+
"get_split_idx",
|
|
22
|
+
"split_data",
|
|
23
|
+
"resolve_subgroup_mask",
|
|
24
|
+
# Preprocessor registry
|
|
25
|
+
"PreprocessorRegistry",
|
|
26
|
+
]
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from types import ModuleType
|
|
3
|
+
from typing import ClassVar
|
|
4
|
+
|
|
5
|
+
import sklearn.impute
|
|
6
|
+
import sklearn.preprocessing
|
|
7
|
+
|
|
8
|
+
from medpipe.utils.registry import BaseRegistry
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class PreprocessorRegistry(BaseRegistry[type[Callable]]):
|
|
12
|
+
"""
|
|
13
|
+
Registry for managing and resolving data preprocessing operations.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
_fallback_modules: ClassVar[list[ModuleType]] = [
|
|
17
|
+
sklearn.preprocessing,
|
|
18
|
+
sklearn.impute,
|
|
19
|
+
]
|
|
@@ -7,22 +7,17 @@ Functions:
|
|
|
7
7
|
- get_split_idx: Returns the indices for the data splits.
|
|
8
8
|
- split_data: Split data into train and test or train and recalibration sets.
|
|
9
9
|
- extract_labels: Extracts prediction labels from data.
|
|
10
|
-
- convert_dtypes: Converts data types to category in a pd.DataFrame.
|
|
11
10
|
"""
|
|
12
11
|
|
|
13
12
|
from __future__ import annotations
|
|
14
13
|
|
|
15
|
-
from typing import
|
|
14
|
+
from typing import Any, Literal, cast
|
|
16
15
|
|
|
17
16
|
import numpy as np
|
|
17
|
+
import numpy.typing as npt
|
|
18
18
|
import pandas as pd
|
|
19
19
|
from sklearn.model_selection import train_test_split
|
|
20
|
-
|
|
21
|
-
from medpipe._types import Labels
|
|
22
|
-
from medpipe.utils.exceptions import array_check, array_dim_check
|
|
23
|
-
|
|
24
|
-
if TYPE_CHECKING:
|
|
25
|
-
import numpy.typing as npt
|
|
20
|
+
from sklearn.utils import check_array, check_consistent_length
|
|
26
21
|
|
|
27
22
|
|
|
28
23
|
def get_split_idx(
|
|
@@ -56,7 +51,7 @@ def get_split_idx(
|
|
|
56
51
|
If values is not a list or a np.ndarray.
|
|
57
52
|
|
|
58
53
|
"""
|
|
59
|
-
|
|
54
|
+
check_array(idx_list, ensure_2d=False)
|
|
60
55
|
|
|
61
56
|
# Standardize groups to numpy
|
|
62
57
|
if isinstance(column, pd.Series):
|
|
@@ -64,7 +59,7 @@ def get_split_idx(
|
|
|
64
59
|
elif not isinstance(column, np.ndarray):
|
|
65
60
|
raise TypeError(f"column should be pd.Series or np.array, got {type(column)}")
|
|
66
61
|
|
|
67
|
-
|
|
62
|
+
check_consistent_length(idx_list, column) # Ensure dimension match
|
|
68
63
|
|
|
69
64
|
# Type checking validation
|
|
70
65
|
if not isinstance(values, (list, np.ndarray)):
|
|
@@ -84,13 +79,14 @@ def get_split_idx(
|
|
|
84
79
|
|
|
85
80
|
def split_data(
|
|
86
81
|
features: pd.DataFrame,
|
|
87
|
-
labels:
|
|
82
|
+
labels: npt.NDArray,
|
|
88
83
|
strategy: Literal["random", "group"],
|
|
89
84
|
group_column: str | None = None,
|
|
90
85
|
values: list[str] | list[int] | None = None,
|
|
91
86
|
test_size: float | None = None,
|
|
92
87
|
recalibration_size: float | None = None,
|
|
93
|
-
|
|
88
|
+
random_state: int | None = None,
|
|
89
|
+
) -> tuple[pd.DataFrame, npt.NDArray, pd.DataFrame, npt.NDArray]:
|
|
94
90
|
"""
|
|
95
91
|
Split data into train and test or train and recalibration sets.
|
|
96
92
|
|
|
@@ -102,7 +98,7 @@ def split_data(
|
|
|
102
98
|
----------
|
|
103
99
|
features : pd.DataFrame
|
|
104
100
|
Features to split.
|
|
105
|
-
labels :
|
|
101
|
+
labels : npt.NDArray
|
|
106
102
|
Labels to split.
|
|
107
103
|
strategy : {"random", "group"}
|
|
108
104
|
Strategy used to split the data.
|
|
@@ -114,12 +110,15 @@ def split_data(
|
|
|
114
110
|
Test set size if the strategy is random.
|
|
115
111
|
recalibration_size : float | None, default: None
|
|
116
112
|
Recalibration set size if the strategy is random.
|
|
113
|
+
random_state : int | None, default: None
|
|
114
|
+
Seed controlling the shuffling applied before the random split, for
|
|
115
|
+
reproducibility. Ignored when strategy is group.
|
|
117
116
|
|
|
118
117
|
Returns
|
|
119
118
|
-------
|
|
120
119
|
X_train, X_test : pd.DataFrame
|
|
121
120
|
Train and test / recalibration set.
|
|
122
|
-
y_train, y_test :
|
|
121
|
+
y_train, y_test : npt.NDArray
|
|
123
122
|
Train and test / recalibration labels.
|
|
124
123
|
|
|
125
124
|
Raises
|
|
@@ -165,7 +164,7 @@ def split_data(
|
|
|
165
164
|
)
|
|
166
165
|
|
|
167
166
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
168
|
-
features, labels, test_size=size
|
|
167
|
+
features, labels, test_size=size, random_state=random_state
|
|
169
168
|
)
|
|
170
169
|
|
|
171
170
|
else:
|
|
@@ -173,15 +172,15 @@ def split_data(
|
|
|
173
172
|
|
|
174
173
|
return (
|
|
175
174
|
cast(pd.DataFrame, X_train),
|
|
176
|
-
cast(
|
|
175
|
+
cast(npt.NDArray, y_train),
|
|
177
176
|
cast(pd.DataFrame, X_test),
|
|
178
|
-
cast(
|
|
177
|
+
cast(npt.NDArray, y_test),
|
|
179
178
|
)
|
|
180
179
|
|
|
181
180
|
|
|
182
181
|
def extract_labels(
|
|
183
182
|
data: pd.DataFrame, labels: list[str]
|
|
184
|
-
) -> tuple[pd.DataFrame,
|
|
183
|
+
) -> tuple[pd.DataFrame, npt.NDArray]:
|
|
185
184
|
"""
|
|
186
185
|
Extracts the prediction labels from the training data.
|
|
187
186
|
|
|
@@ -196,7 +195,7 @@ def extract_labels(
|
|
|
196
195
|
-------
|
|
197
196
|
X : pd.DataFrame
|
|
198
197
|
DataFrame containing the data.
|
|
199
|
-
y :
|
|
198
|
+
y : npt.NDArray
|
|
200
199
|
Array containing the prediction labels.
|
|
201
200
|
|
|
202
201
|
Raises
|
|
@@ -227,43 +226,61 @@ def extract_labels(
|
|
|
227
226
|
return X, y
|
|
228
227
|
|
|
229
228
|
|
|
230
|
-
def
|
|
229
|
+
def resolve_subgroup_mask(
|
|
230
|
+
df: pd.DataFrame,
|
|
231
|
+
column: str,
|
|
232
|
+
group: Any,
|
|
233
|
+
) -> pd.Series:
|
|
231
234
|
"""
|
|
232
|
-
|
|
233
|
-
functions.
|
|
235
|
+
Generate a boolean mask for a dataset matching a stratum group definition.
|
|
234
236
|
|
|
235
|
-
|
|
236
|
-
checked to see if it can be converted to numeric before
|
|
237
|
-
converting to categorical.
|
|
238
|
-
Timedeltas are converted to days.
|
|
237
|
+
Ranges are inclusive so [18, 50] will include 18 and 50 in the range.
|
|
239
238
|
|
|
240
239
|
Parameters
|
|
241
240
|
----------
|
|
242
|
-
|
|
243
|
-
|
|
241
|
+
df : pd.DataFrame
|
|
242
|
+
Dataset containing the stratum column.
|
|
243
|
+
column : str
|
|
244
|
+
Name of the column to stratify on.
|
|
245
|
+
group : Any
|
|
246
|
+
Group definition. Can be a discrete scalar ('M'), a range tuple (18, 65),
|
|
247
|
+
a pd.Interval.
|
|
244
248
|
|
|
245
249
|
Returns
|
|
246
250
|
-------
|
|
247
|
-
|
|
248
|
-
|
|
251
|
+
pd.Series
|
|
252
|
+
Boolean mask of shape (n_samples,) matching the group criteria.
|
|
249
253
|
|
|
250
254
|
Raises
|
|
251
255
|
------
|
|
252
|
-
|
|
253
|
-
If
|
|
256
|
+
KeyError
|
|
257
|
+
If `column` is not present in `df`.
|
|
258
|
+
ValueError
|
|
259
|
+
If `group` is a tuple or list that does not have exactly 2 elements.
|
|
254
260
|
|
|
255
261
|
"""
|
|
256
|
-
if not
|
|
257
|
-
raise
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
262
|
+
if column not in df.columns:
|
|
263
|
+
raise KeyError(f"Stratum column '{column}' not found in DataFrame.")
|
|
264
|
+
|
|
265
|
+
col_data = df[column]
|
|
266
|
+
|
|
267
|
+
# Column is already a Pandas Categorical with pd.Interval values
|
|
268
|
+
if isinstance(group, pd.Interval):
|
|
269
|
+
return col_data == group
|
|
270
|
+
|
|
271
|
+
# Group passed as a tuple/list of bounds: (min, max)
|
|
272
|
+
elif isinstance(group, (tuple, list)) and len(group) == 2:
|
|
273
|
+
lower, upper = group[0], group[1]
|
|
274
|
+
return (col_data >= lower) & (col_data <= upper)
|
|
275
|
+
|
|
276
|
+
# A tuple/list of any other length is not a valid range definition, and
|
|
277
|
+
# would otherwise silently fall through to a positional/length-mismatched
|
|
278
|
+
# equality comparison below.
|
|
279
|
+
elif isinstance(group, (tuple, list)):
|
|
280
|
+
raise ValueError(
|
|
281
|
+
"Range group definitions must have exactly 2 elements (min, max), "
|
|
282
|
+
f"but got {len(group)}: {group!r}"
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
# Standard discrete scalar equality (e.g. string, int, float)
|
|
286
|
+
return col_data == group
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""
|
|
2
|
+
medpipe.metrics
|
|
3
|
+
---------------
|
|
4
|
+
Metrics, scoring registries, and plotting utilities for the medpipe package.
|
|
5
|
+
|
|
6
|
+
Provides core metric computation functions, bootstrap confidence interval
|
|
7
|
+
estimation, cross-validation scorer building, and metric registries.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from medpipe.metrics.core import (
|
|
11
|
+
PredictionBundle,
|
|
12
|
+
bootstrap_confidence_intervals,
|
|
13
|
+
build_scorers,
|
|
14
|
+
compute_metrics,
|
|
15
|
+
crps_score,
|
|
16
|
+
ici_score,
|
|
17
|
+
)
|
|
18
|
+
from medpipe.metrics.registry import MetricRegistry, MetricSpec
|
|
19
|
+
|
|
20
|
+
__all__ = [ # noqa: RUF022 (grouped by category, not alphabetical)
|
|
21
|
+
# Core metric functions
|
|
22
|
+
"compute_metrics",
|
|
23
|
+
"build_scorers",
|
|
24
|
+
"bootstrap_confidence_intervals",
|
|
25
|
+
"ici_score",
|
|
26
|
+
"crps_score",
|
|
27
|
+
"PredictionBundle",
|
|
28
|
+
# Metric registry and specifications
|
|
29
|
+
"MetricRegistry",
|
|
30
|
+
"MetricSpec",
|
|
31
|
+
]
|