requipso 0.2.2__py3-none-any.whl

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 (75) hide show
  1. requipso/__init__.py +27 -0
  2. requipso/cli.py +11 -0
  3. requipso/completer.py +103 -0
  4. requipso/data.py +72 -0
  5. requipso/descriptive_pipeline.py +271 -0
  6. requipso/docs/macros.py +226 -0
  7. requipso/docs/mkdocs.yml +140 -0
  8. requipso/docs/pages/assets/consort_flowchart.png +0 -0
  9. requipso/docs/pages/assets/demo.gif +0 -0
  10. requipso/docs/pages/blocs/age.md +61 -0
  11. requipso/docs/pages/blocs/ald.md +65 -0
  12. requipso/docs/pages/blocs/biometry.md +98 -0
  13. requipso/docs/pages/blocs/cim10.md +80 -0
  14. requipso/docs/pages/blocs/cisp2.md +59 -0
  15. requipso/docs/pages/blocs/code-postal.md +47 -0
  16. requipso/docs/pages/blocs/consultations.md +65 -0
  17. requipso/docs/pages/blocs/departement.md +50 -0
  18. requipso/docs/pages/blocs/est-vivant.md +44 -0
  19. requipso/docs/pages/blocs/genre.md +46 -0
  20. requipso/docs/pages/blocs/index.md +40 -0
  21. requipso/docs/pages/blocs/insurance.md +55 -0
  22. requipso/docs/pages/blocs/medecin-traitant.md +56 -0
  23. requipso/docs/pages/blocs/paquet-annees.md +70 -0
  24. requipso/docs/pages/blocs/prescription-atc.md +69 -0
  25. requipso/docs/pages/blocs/regex-consultation.md +64 -0
  26. requipso/docs/pages/blocs/regex-nlp.md +107 -0
  27. requipso/docs/pages/blocs/regex-patient-observation.md +81 -0
  28. requipso/docs/pages/blocs/regex-prescription.md +64 -0
  29. requipso/docs/pages/guide/application.md +81 -0
  30. requipso/docs/pages/guide/combiner-blocs.md +61 -0
  31. requipso/docs/pages/guide/demarrage.md +74 -0
  32. requipso/docs/pages/guide/erreurs.md +83 -0
  33. requipso/docs/pages/guide/exemple-complet.md +67 -0
  34. requipso/docs/pages/guide/google-sheet.md +50 -0
  35. requipso/docs/pages/guide/onglet-inclusion.md +91 -0
  36. requipso/docs/pages/guide/onglet-projet.md +48 -0
  37. requipso/docs/pages/guide/onglet-tableau-descriptif.md +124 -0
  38. requipso/docs/pages/guide/parametres.md +162 -0
  39. requipso/docs/pages/index.md +73 -0
  40. requipso/docs/pages/stylesheets/extra.css +3 -0
  41. requipso/docs/pages/technique/ajouter-un-bloc.md +147 -0
  42. requipso/docs/pages/technique/architecture.md +80 -0
  43. requipso/docs/pages/technique/blocs-internes.md +176 -0
  44. requipso/docs/pages/technique/chargement-sheet.md +107 -0
  45. requipso/docs/pages/technique/developpement.md +124 -0
  46. requipso/docs/pages/technique/donnees.md +106 -0
  47. requipso/docs/pages/technique/export-signatures.md +131 -0
  48. requipso/docs/pages/technique/nlp.md +82 -0
  49. requipso/docs/pages/technique/parametres-internes.md +140 -0
  50. requipso/docs/pages/technique/pipeline.md +66 -0
  51. requipso/docs/pages/technique/tableau-descriptif.md +74 -0
  52. requipso/google.py +168 -0
  53. requipso/io.py +95 -0
  54. requipso/notebooks/marimo/run.py +352 -0
  55. requipso/pipeline.py +364 -0
  56. requipso/pipeline_loader.py +572 -0
  57. requipso/pipes/__init__.py +43 -0
  58. requipso/pipes/atcd.py +365 -0
  59. requipso/pipes/base.py +431 -0
  60. requipso/pipes/biometry.py +170 -0
  61. requipso/pipes/consultations.py +117 -0
  62. requipso/pipes/insurance.py +90 -0
  63. requipso/pipes/params.py +283 -0
  64. requipso/pipes/patient.py +377 -0
  65. requipso/pipes/prescription.py +134 -0
  66. requipso/pipes/processors.py +250 -0
  67. requipso/polars.py +24 -0
  68. requipso/resources/pathology/__init__.py +7 -0
  69. requipso/signature_exporter.py +473 -0
  70. requipso/stats.py +130 -0
  71. requipso-0.2.2.dist-info/METADATA +102 -0
  72. requipso-0.2.2.dist-info/RECORD +75 -0
  73. requipso-0.2.2.dist-info/WHEEL +5 -0
  74. requipso-0.2.2.dist-info/entry_points.txt +2 -0
  75. requipso-0.2.2.dist-info/top_level.txt +1 -0
requipso/__init__.py ADDED
@@ -0,0 +1,27 @@
1
+ from pathlib import Path
2
+
3
+ # Define paths FIRST before any other imports that might need them
4
+ BASE_DIR = Path(__file__).parent.resolve()
5
+ RESOURCES_DIR = BASE_DIR / "resources"
6
+
7
+ from .data import Data
8
+
9
+ # Registry and schema are imported lazily to avoid circular imports
10
+ # Use: from requipso.registry import registry
11
+ # Use: from requipso.schema import schema_provider
12
+
13
+ # Auto-install Polars column completer in IPython contexts
14
+ try:
15
+ from IPython import get_ipython
16
+
17
+ if get_ipython() is not None:
18
+ from .completer import install as _install_completer
19
+
20
+ _install_completer()
21
+ except ImportError:
22
+ pass
23
+
24
+ __all__ = [
25
+ "BASE_DIR",
26
+ "RESOURCES_DIR",
27
+ ]
requipso/cli.py ADDED
@@ -0,0 +1,11 @@
1
+ import subprocess
2
+ import sys
3
+ from pathlib import Path
4
+
5
+
6
+ def main() -> None:
7
+ notebook = Path(__file__).parent / "notebooks" / "marimo" / "run.py"
8
+ code = subprocess.call(
9
+ ["marimo", "run", str(notebook), "--show-tracebacks", *sys.argv[1:]]
10
+ )
11
+ sys.exit(code)
requipso/completer.py ADDED
@@ -0,0 +1,103 @@
1
+ """Custom IPython completer for Polars column names.
2
+
3
+ Provides tab-completion for column names inside ``pl.col("...")`` expressions.
4
+ Columns are sourced from all Polars DataFrames/LazyFrames referenced in the
5
+ current cell being edited.
6
+
7
+ Usage::
8
+
9
+ from requipso.completer import install
10
+ install()
11
+ """
12
+
13
+ from __future__ import annotations
14
+ from typing import Iterator
15
+
16
+ import ast
17
+ import re
18
+
19
+ import polars as pl
20
+ from IPython.core.getipython import get_ipython
21
+ from IPython.core.completer import Completion
22
+
23
+ # Matches cursor inside pl.col("...") / pl.cols("...") / pl.col(["..."])
24
+ _COL_RE = re.compile(r"pl\.cols?\(\[?[\"']([^\"']*?)$")
25
+
26
+
27
+ def _extract_names(code: str) -> set[str]:
28
+ """Extract variable names referenced in a code string.
29
+
30
+ Uses AST when possible; falls back to regex for incomplete code
31
+ (the usual state when the user is still typing).
32
+ """
33
+ try:
34
+ tree = ast.parse(code)
35
+ return {node.id for node in ast.walk(tree) if isinstance(node, ast.Name)}
36
+ except SyntaxError:
37
+ return set(re.findall(r"\b([a-zA-Z_]\w*)\b", code))
38
+
39
+
40
+ def _polars_col_completions(text: str, offset: int) -> Iterator[Completion]:
41
+ """Yield column-name ``Completion`` objects when the cursor is inside ``pl.col("…")``."""
42
+ text_to_cursor = text[:offset]
43
+ match = _COL_RE.search(text_to_cursor)
44
+ if not match:
45
+ return
46
+
47
+ prefix = match.group(1)
48
+ start = match.start(1)
49
+
50
+ ip = get_ipython()
51
+ if ip is None:
52
+ # IPython environment not available, can't provide completions
53
+ return
54
+ # Collect names referenced in the *current cell* (= full `text`)
55
+ cell_names = _extract_names(text)
56
+
57
+ columns: set[str] = set()
58
+ for name in cell_names:
59
+ obj = ip.user_ns.get(name)
60
+ if isinstance(obj, (pl.DataFrame, pl.LazyFrame)):
61
+ columns.update(obj.columns)
62
+
63
+ for col in sorted(columns):
64
+ if col.startswith(prefix):
65
+ yield Completion(start=start, end=offset, text=col, type="property")
66
+
67
+
68
+ # ---------------------------------------------------------------------------
69
+ # Public API
70
+ # ---------------------------------------------------------------------------
71
+
72
+
73
+ def install() -> None:
74
+ """Patch IPython's completer to suggest Polars column names."""
75
+ ip = get_ipython()
76
+ if ip is None:
77
+ return
78
+
79
+ completer = ip.Completer
80
+
81
+ # Store the original only once so repeated calls don't stack wrappers.
82
+ if not hasattr(completer, "_original_completions"):
83
+ completer._original_completions = completer.completions
84
+
85
+ original = completer._original_completions
86
+
87
+ def _patched(text, offset):
88
+ yield from _polars_col_completions(text, offset)
89
+ yield from original(text, offset)
90
+
91
+ completer.completions = _patched
92
+
93
+
94
+ def uninstall() -> None:
95
+ """Restore the original completer."""
96
+ ip = get_ipython()
97
+ if ip is None:
98
+ return
99
+ completer = ip.Completer
100
+ if hasattr(completer, "_original_completions"):
101
+ completer.completions = completer._original_completions
102
+ del completer._original_completions
103
+ print("✓ Polars column completer removed")
requipso/data.py ADDED
@@ -0,0 +1,72 @@
1
+ import os
2
+ from collections import defaultdict
3
+ from pathlib import Path
4
+
5
+ import polars as pl
6
+
7
+ from requipso.io import S3IO, BaseIO, LocalIO
8
+
9
+
10
+ class Data:
11
+ def __init__(
12
+ self,
13
+ data_path: Path | str,
14
+ empty: bool = False,
15
+ patient_ids: pl.Series | None = None,
16
+ ):
17
+ self.data_path = data_path
18
+ self.io: BaseIO
19
+ self.patient_ids = patient_ids
20
+
21
+ if isinstance(data_path, str) and data_path.startswith("s3://"):
22
+ self.io = S3IO(data_path)
23
+ else:
24
+ self.io = LocalIO(data_path)
25
+
26
+ self.data = self.load_data(empty=empty)
27
+
28
+ self.results = self.data["patient"].select("patient_id")
29
+ self.all_results = defaultdict(dict)
30
+
31
+ def load_data(self, empty: bool = False) -> dict[str, pl.DataFrame]:
32
+ n_rows = 0 if empty else None
33
+ tables = {
34
+ table_name: self.io.read_table(table_name, n_rows=n_rows)
35
+ for table_name in self.io.list_tables()
36
+ }
37
+
38
+ if self.patient_ids is not None:
39
+ tables = {
40
+ name: df.filter(pl.col("patient_id").is_in(self.patient_ids))
41
+ if "patient_id" in df.columns
42
+ else df
43
+ for name, df in tables.items()
44
+ }
45
+ return tables
46
+
47
+ def __getitem__(self, table_name: str) -> pl.DataFrame:
48
+ return self.data[table_name]
49
+
50
+ def __setitem__(self, table_name: str, table: pl.DataFrame) -> None:
51
+ self.data[table_name] = table
52
+
53
+ def __getattr__(self, name: str) -> pl.DataFrame:
54
+ if "data" in self.__dict__ and name in self.data:
55
+ return self.data[name]
56
+ raise AttributeError(f"'{type(self).__name__}' has no attribute '{name}'")
57
+
58
+ def __dir__(self) -> list[str]:
59
+ default = list(super().__dir__())
60
+ if "data" in self.__dict__:
61
+ return list(set(default) | set(self.data.keys()))
62
+ return default
63
+
64
+ def get_table(self, table_name: str) -> pl.DataFrame:
65
+ return self.data[table_name]
66
+
67
+ def set_table(self, table_name: str, table: pl.DataFrame) -> None:
68
+ self.data[table_name] = table
69
+
70
+ def reset(self) -> None:
71
+ self.all_results = defaultdict(dict)
72
+ self.results = self.data["patient"].select("patient_id")
@@ -0,0 +1,271 @@
1
+ from datetime import datetime
2
+ from functools import reduce
3
+ from sqlite3.dbapi2 import Row
4
+ from typing import Any, Generator, Literal, cast, get_args
5
+
6
+ import gspread # type: ignore
7
+ import pandas as pd
8
+ import polars as pl
9
+ from gspread_formatting import ( # type: ignore
10
+ CellFormat,
11
+ Color,
12
+ TextFormat,
13
+ format_cell_ranges,
14
+ )
15
+ from gspread_pandas import Spread # type: ignore
16
+
17
+ from requipso.data import Data
18
+ from requipso.google import GoogleConnector
19
+ from requipso.pipeline import Pipeline
20
+ from requipso.pipes.base import BaseBlock
21
+ from requipso.stats import (
22
+ compute_stats_boolean,
23
+ compute_stats_categories,
24
+ compute_stats_quantitative,
25
+ )
26
+
27
+ VARIABLE_TYPE = Literal["categorical", "numerical", "boolean"]
28
+
29
+ RESULTS_SHEET_NAME = "🔒 Results"
30
+
31
+
32
+ class DescriptivePipeline:
33
+ def __init__(
34
+ self,
35
+ data: Data,
36
+ inclusion_pipeline: Pipeline,
37
+ populations: list[str],
38
+ google_connector: GoogleConnector,
39
+ ):
40
+
41
+ missing_populations = set(populations) - set(
42
+ inclusion_pipeline.steps_names.keys()
43
+ )
44
+ if missing_populations:
45
+ raise ValueError(
46
+ f"Populations {missing_populations} not found in inclusion pipeline"
47
+ )
48
+
49
+ self.inclusion_pipeline = inclusion_pipeline
50
+ self.populations = {
51
+ name: data.all_results[name]["post"]
52
+ .filter(pl.col(name))
53
+ .get_column("patient_id")
54
+ .unique()
55
+ for name in populations
56
+ }
57
+ self.reference_date = inclusion_pipeline.reference_date
58
+ self.rows: list[dict[str, Any]] = []
59
+
60
+ self.stats: dict[str, dict[str, pl.DataFrame]] = dict()
61
+ self.stats["Total"] = dict()
62
+
63
+ self.patient_ids = pl.concat(self.populations.values()).unique()
64
+ self.google_connector = google_connector
65
+
66
+ def add_row(
67
+ self,
68
+ step: BaseBlock,
69
+ variable_type: VARIABLE_TYPE,
70
+ display_name: str | None = None,
71
+ subsets: list[Any] | None = None,
72
+ ) -> None:
73
+ if variable_type not in get_args(VARIABLE_TYPE):
74
+ raise ValueError(f"Variable type {variable_type} not supported")
75
+ if step.name in self.inclusion_pipeline.steps_names:
76
+ raise ValueError(
77
+ f"Step {step.name} already exists in inclusion pipeline, use a different name"
78
+ )
79
+ self.rows.append(
80
+ {
81
+ "step": step,
82
+ "display_name": display_name or step.name,
83
+ "variable_type": variable_type,
84
+ "subsets": subsets,
85
+ }
86
+ )
87
+
88
+ def run(self, data: Data) -> pd.DataFrame:
89
+ for _ in self.run_iter(data):
90
+ pass
91
+ df_stats = self.format_stats(self.stats)
92
+ self.upload_stats(df_stats)
93
+ return df_stats
94
+
95
+ def run_iter(self, data: Data) -> Generator[dict[str, pl.DataFrame], None, None]:
96
+ main_stats = pl.DataFrame(
97
+ {pop_name: pop.len() for pop_name, pop in self.populations.items()}
98
+ )
99
+
100
+ n_general_pop = main_stats.item(0, 0)
101
+
102
+ for pop_name in main_stats.columns:
103
+ main_stats = main_stats.with_columns(
104
+ pl.col(pop_name).alias("name"),
105
+ (pl.col(pop_name) / n_general_pop * 100).round(2).alias("pct"),
106
+ ).with_columns(pl.format("{name} ({pct}%)").alias("(n, %)"))
107
+
108
+ self.stats["Total"][pop_name] = main_stats.select("(n, %)")
109
+
110
+ for row in self.rows:
111
+ step = row["step"]
112
+ variable_type = row["variable_type"]
113
+ subsets = row["subsets"]
114
+
115
+ data = step.execute(
116
+ data,
117
+ patient_ids=self.patient_ids,
118
+ reference_date=self.reference_date,
119
+ )
120
+
121
+ df: pl.DataFrame = data.all_results[step.name]["post"].select(
122
+ "patient_id",
123
+ pl.col(step.name),
124
+ )
125
+ col = df.get_column(step.name)
126
+
127
+ # Check concordance between variable_type and true dtype
128
+ if variable_type == "numerical":
129
+ if not col.dtype.is_numeric():
130
+ raise ValueError(
131
+ f"Column {step.name} is not numeric (got {col.dtype}), but variable_type is numerical. Maybe check post_func ?"
132
+ )
133
+ elif variable_type == "boolean":
134
+ if col.dtype != pl.Boolean:
135
+ raise ValueError(
136
+ f"Column {step.name} is not boolean (got {col.dtype}), but variable_type is boolean. Maybe check post_func ?"
137
+ )
138
+
139
+ self.stats[step.name] = dict()
140
+ for population in self.populations:
141
+ self.stats[step.name][population] = self.compute_stats(
142
+ df.join(
143
+ self.populations[population].to_frame("patient_id"),
144
+ on="patient_id",
145
+ how="inner",
146
+ ),
147
+ step.name,
148
+ subsets,
149
+ variable_type,
150
+ )
151
+
152
+ yield self.stats[step.name]
153
+
154
+ def compute_stats(
155
+ self,
156
+ df: pl.DataFrame,
157
+ name: str,
158
+ subsets: list[Any],
159
+ variable_type: VARIABLE_TYPE,
160
+ ) -> pl.DataFrame:
161
+ if variable_type == "categorical":
162
+ return compute_stats_categories(df, name, subsets)
163
+ elif variable_type == "numerical":
164
+ return compute_stats_quantitative(df, name, subsets)
165
+ elif variable_type == "boolean":
166
+ return compute_stats_boolean(df, name)
167
+ else:
168
+ raise ValueError(f"Variable type {variable_type} not supported")
169
+
170
+ def format_stats(self, stats: dict[str, dict[str, pl.DataFrame]]) -> pd.DataFrame:
171
+ subtables: list[pl.DataFrame] = []
172
+ for block, pop in stats.items():
173
+ rows = [
174
+ df.transpose(include_header=True, column_names=[pop_name])
175
+ for pop_name, df in pop.items()
176
+ ]
177
+
178
+ rows_df = reduce(
179
+ lambda left, right: left.join(
180
+ right, on="column", how="inner", maintain_order="left"
181
+ ),
182
+ rows,
183
+ )
184
+ header = rows_df.sample(1).select(pl.all().replace(pl.all(), None))
185
+ header[0, "column"] = block
186
+ rows_df = pl.concat([header, rows_df])
187
+
188
+ subtables.append(rows_df)
189
+
190
+ df = pl.concat(subtables).to_pandas()
191
+ df.columns = [""] + df.columns[1:].tolist() # type: ignore
192
+ return df
193
+
194
+ def upload_stats(self, stats: pd.DataFrame) -> None:
195
+
196
+ spread = cast(Spread, self.google_connector.spread)
197
+
198
+ # Clear
199
+ sh = spread.spread
200
+ ws = sh.worksheet(RESULTS_SHEET_NAME)
201
+
202
+ sheet_id = ws._properties["sheetId"]
203
+
204
+ sh.batch_update(
205
+ {
206
+ "requests": [
207
+ {"updateCells": {"range": {"sheetId": sheet_id}, "fields": "*"}}
208
+ ]
209
+ }
210
+ )
211
+
212
+ # Upload
213
+ spread.df_to_sheet(
214
+ stats,
215
+ index=False,
216
+ sheet=RESULTS_SHEET_NAME,
217
+ start="A1",
218
+ replace=True,
219
+ )
220
+
221
+ # Format
222
+ ws = spread.spread.worksheet(RESULTS_SHEET_NAME)
223
+
224
+ to_format = []
225
+
226
+ numbers_fmt = CellFormat(
227
+ textFormat=TextFormat(bold=False), horizontalAlignment="CENTER"
228
+ )
229
+ to_format.append(
230
+ [
231
+ f"A1:{gspread.utils.rowcol_to_a1(len(stats) + 1, len(stats.columns))}",
232
+ numbers_fmt,
233
+ ]
234
+ )
235
+
236
+ header_fmt = CellFormat(
237
+ textFormat=TextFormat(bold=True),
238
+ horizontalAlignment="CENTER",
239
+ )
240
+ to_format.append(
241
+ [
242
+ f"A1:{gspread.utils.rowcol_to_a1(1, len(stats.columns))}",
243
+ header_fmt,
244
+ ]
245
+ )
246
+
247
+ block_fmt = CellFormat(
248
+ textFormat=TextFormat(bold=True),
249
+ horizontalAlignment="LEFT",
250
+ backgroundColor=Color(159 / 255, 197 / 255, 232 / 255),
251
+ )
252
+ normal_fmt = CellFormat(horizontalAlignment="RIGHT")
253
+
254
+ for idx, row in stats.iterrows():
255
+ idx = cast(int, idx)
256
+ if bool(row.drop("").isna().all()): # Header
257
+ to_format.append(
258
+ [
259
+ f"{gspread.utils.rowcol_to_a1(idx + 2, 1)}:{gspread.utils.rowcol_to_a1(idx + 2, len(stats.columns))}",
260
+ block_fmt,
261
+ ]
262
+ )
263
+ else:
264
+ to_format.append(
265
+ [
266
+ f"{gspread.utils.rowcol_to_a1(idx + 2, 1)}",
267
+ normal_fmt,
268
+ ]
269
+ )
270
+
271
+ format_cell_ranges(ws, to_format)