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.
- requipso/__init__.py +27 -0
- requipso/cli.py +11 -0
- requipso/completer.py +103 -0
- requipso/data.py +72 -0
- requipso/descriptive_pipeline.py +271 -0
- requipso/docs/macros.py +226 -0
- requipso/docs/mkdocs.yml +140 -0
- requipso/docs/pages/assets/consort_flowchart.png +0 -0
- requipso/docs/pages/assets/demo.gif +0 -0
- requipso/docs/pages/blocs/age.md +61 -0
- requipso/docs/pages/blocs/ald.md +65 -0
- requipso/docs/pages/blocs/biometry.md +98 -0
- requipso/docs/pages/blocs/cim10.md +80 -0
- requipso/docs/pages/blocs/cisp2.md +59 -0
- requipso/docs/pages/blocs/code-postal.md +47 -0
- requipso/docs/pages/blocs/consultations.md +65 -0
- requipso/docs/pages/blocs/departement.md +50 -0
- requipso/docs/pages/blocs/est-vivant.md +44 -0
- requipso/docs/pages/blocs/genre.md +46 -0
- requipso/docs/pages/blocs/index.md +40 -0
- requipso/docs/pages/blocs/insurance.md +55 -0
- requipso/docs/pages/blocs/medecin-traitant.md +56 -0
- requipso/docs/pages/blocs/paquet-annees.md +70 -0
- requipso/docs/pages/blocs/prescription-atc.md +69 -0
- requipso/docs/pages/blocs/regex-consultation.md +64 -0
- requipso/docs/pages/blocs/regex-nlp.md +107 -0
- requipso/docs/pages/blocs/regex-patient-observation.md +81 -0
- requipso/docs/pages/blocs/regex-prescription.md +64 -0
- requipso/docs/pages/guide/application.md +81 -0
- requipso/docs/pages/guide/combiner-blocs.md +61 -0
- requipso/docs/pages/guide/demarrage.md +74 -0
- requipso/docs/pages/guide/erreurs.md +83 -0
- requipso/docs/pages/guide/exemple-complet.md +67 -0
- requipso/docs/pages/guide/google-sheet.md +50 -0
- requipso/docs/pages/guide/onglet-inclusion.md +91 -0
- requipso/docs/pages/guide/onglet-projet.md +48 -0
- requipso/docs/pages/guide/onglet-tableau-descriptif.md +124 -0
- requipso/docs/pages/guide/parametres.md +162 -0
- requipso/docs/pages/index.md +73 -0
- requipso/docs/pages/stylesheets/extra.css +3 -0
- requipso/docs/pages/technique/ajouter-un-bloc.md +147 -0
- requipso/docs/pages/technique/architecture.md +80 -0
- requipso/docs/pages/technique/blocs-internes.md +176 -0
- requipso/docs/pages/technique/chargement-sheet.md +107 -0
- requipso/docs/pages/technique/developpement.md +124 -0
- requipso/docs/pages/technique/donnees.md +106 -0
- requipso/docs/pages/technique/export-signatures.md +131 -0
- requipso/docs/pages/technique/nlp.md +82 -0
- requipso/docs/pages/technique/parametres-internes.md +140 -0
- requipso/docs/pages/technique/pipeline.md +66 -0
- requipso/docs/pages/technique/tableau-descriptif.md +74 -0
- requipso/google.py +168 -0
- requipso/io.py +95 -0
- requipso/notebooks/marimo/run.py +352 -0
- requipso/pipeline.py +364 -0
- requipso/pipeline_loader.py +572 -0
- requipso/pipes/__init__.py +43 -0
- requipso/pipes/atcd.py +365 -0
- requipso/pipes/base.py +431 -0
- requipso/pipes/biometry.py +170 -0
- requipso/pipes/consultations.py +117 -0
- requipso/pipes/insurance.py +90 -0
- requipso/pipes/params.py +283 -0
- requipso/pipes/patient.py +377 -0
- requipso/pipes/prescription.py +134 -0
- requipso/pipes/processors.py +250 -0
- requipso/polars.py +24 -0
- requipso/resources/pathology/__init__.py +7 -0
- requipso/signature_exporter.py +473 -0
- requipso/stats.py +130 -0
- requipso-0.2.2.dist-info/METADATA +102 -0
- requipso-0.2.2.dist-info/RECORD +75 -0
- requipso-0.2.2.dist-info/WHEEL +5 -0
- requipso-0.2.2.dist-info/entry_points.txt +2 -0
- requipso-0.2.2.dist-info/top_level.txt +1 -0
requipso/pipeline.py
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
# pyright: reportOptionalContextManager=false
|
|
2
|
+
|
|
3
|
+
import datetime
|
|
4
|
+
from typing import Any, Generator, Optional, Self
|
|
5
|
+
|
|
6
|
+
import networkx as nx
|
|
7
|
+
import polars as pl
|
|
8
|
+
from graphviz import Digraph # type: ignore
|
|
9
|
+
|
|
10
|
+
from requipso.data import Data
|
|
11
|
+
from requipso.pipes.base import BaseBlock, OutputType
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Pipeline:
|
|
15
|
+
def __init__(
|
|
16
|
+
self: Self,
|
|
17
|
+
initial_text: str = "Patients",
|
|
18
|
+
reference_date: datetime.datetime | None = None,
|
|
19
|
+
) -> None:
|
|
20
|
+
self.steps: nx.DiGraph = nx.DiGraph()
|
|
21
|
+
self.initial_text = initial_text
|
|
22
|
+
self.initial_n: int | None = None
|
|
23
|
+
self.source_step: BaseBlock | None = None
|
|
24
|
+
self.steps_names: dict[str, BaseBlock] = dict()
|
|
25
|
+
self.steps_results: dict[str, dict[str, Any]]
|
|
26
|
+
self.reference_date = reference_date
|
|
27
|
+
self.last_addded_step: BaseBlock | None = None
|
|
28
|
+
|
|
29
|
+
def add_step(
|
|
30
|
+
self,
|
|
31
|
+
step: BaseBlock,
|
|
32
|
+
text: str | None = None,
|
|
33
|
+
excluded_text: str | None = None,
|
|
34
|
+
parent_step: BaseBlock | str | None = None,
|
|
35
|
+
) -> None:
|
|
36
|
+
if (self.steps.number_of_nodes() > 0) and parent_step is None:
|
|
37
|
+
parent_step = self.last_addded_step
|
|
38
|
+
|
|
39
|
+
if (self.steps.number_of_nodes() == 0) and parent_step is not None:
|
|
40
|
+
raise ValueError("A parent step is not permitted for the first step")
|
|
41
|
+
|
|
42
|
+
if step.name in self.steps_names.keys():
|
|
43
|
+
raise ValueError(f"Step {step.name} already exists")
|
|
44
|
+
|
|
45
|
+
self.steps_names[step.name] = step
|
|
46
|
+
|
|
47
|
+
if isinstance(parent_step, str):
|
|
48
|
+
parent_step = self.steps_names.get(parent_step)
|
|
49
|
+
if parent_step is None:
|
|
50
|
+
raise ValueError(f"Parent step {parent_step} not found")
|
|
51
|
+
|
|
52
|
+
if self.source_step is None:
|
|
53
|
+
self.source_step = step
|
|
54
|
+
|
|
55
|
+
self.steps.add_node(
|
|
56
|
+
step,
|
|
57
|
+
idx=self.steps.number_of_nodes(),
|
|
58
|
+
name=step.name,
|
|
59
|
+
output_patients=None, # Will store the pl.Series containing the patients that passed the step
|
|
60
|
+
text=text,
|
|
61
|
+
excluded_text=excluded_text,
|
|
62
|
+
)
|
|
63
|
+
if parent_step is not None:
|
|
64
|
+
if parent_step not in self.steps.nodes:
|
|
65
|
+
raise ValueError(f"Parent step {parent_step.name} not found")
|
|
66
|
+
self.steps.add_edge(parent_step, step)
|
|
67
|
+
|
|
68
|
+
self.last_addded_step = step
|
|
69
|
+
|
|
70
|
+
def graph(self, save_name: str) -> Digraph:
|
|
71
|
+
return dag_to_consort_flowchart(
|
|
72
|
+
self.steps,
|
|
73
|
+
name=save_name,
|
|
74
|
+
source_text=self.initial_text,
|
|
75
|
+
source_n=self.initial_n,
|
|
76
|
+
render_path=save_name,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
def reset_output_patients(self):
|
|
80
|
+
# TODO
|
|
81
|
+
pass
|
|
82
|
+
|
|
83
|
+
def iter_run(self, data: Data) -> Generator[tuple[str, int], None, None]:
|
|
84
|
+
"""Yield ``(step_name, n_remaining)`` after executing each step.
|
|
85
|
+
|
|
86
|
+
This generator form lets the caller wrap the iteration with a progress
|
|
87
|
+
bar (e.g. ``mo.status.progress_bar``) so the UI updates correctly
|
|
88
|
+
between each yield.
|
|
89
|
+
"""
|
|
90
|
+
self.steps_results = dict()
|
|
91
|
+
|
|
92
|
+
for step in nx.dfs_preorder_nodes(self.steps, source=self.source_step):
|
|
93
|
+
parents = list[BaseBlock](self.steps.predecessors(step))
|
|
94
|
+
if not parents:
|
|
95
|
+
input_patients = None
|
|
96
|
+
else:
|
|
97
|
+
parent = parents[0]
|
|
98
|
+
input_patients = (
|
|
99
|
+
data.all_results[parent.name]["post"]
|
|
100
|
+
.filter(pl.col(parent.name))
|
|
101
|
+
.get_column("patient_id")
|
|
102
|
+
.unique()
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
output_type = OutputType.BOOLEAN
|
|
106
|
+
if output_type != OutputType.BOOLEAN:
|
|
107
|
+
raise ValueError(
|
|
108
|
+
f"Step {step.name} has output type {output_type}, expected BOOLEAN"
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
data = step.execute(
|
|
112
|
+
data,
|
|
113
|
+
patient_ids=input_patients,
|
|
114
|
+
reference_date=self.reference_date,
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
output_patients = (
|
|
118
|
+
data.all_results[step.name]["post"]
|
|
119
|
+
.filter(pl.col(step.name))
|
|
120
|
+
.get_column("patient_id")
|
|
121
|
+
.unique()
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
if step.has_date:
|
|
125
|
+
data.all_results[step.name]["dates"] = data.all_results[step.name][
|
|
126
|
+
"dates"
|
|
127
|
+
].filter(pl.col("patient_id").is_in(output_patients))
|
|
128
|
+
|
|
129
|
+
self.steps.nodes[step]["n"] = len(output_patients)
|
|
130
|
+
|
|
131
|
+
self.steps_results[step.name] = {
|
|
132
|
+
"n": len(output_patients),
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if not parents:
|
|
136
|
+
self.initial_n = data.patient.n_unique()
|
|
137
|
+
|
|
138
|
+
yield step.name, len(output_patients)
|
|
139
|
+
|
|
140
|
+
def run(self, data: Data) -> Data:
|
|
141
|
+
# simple iter with iter_run
|
|
142
|
+
for step_name, n in self.iter_run(data):
|
|
143
|
+
pass
|
|
144
|
+
return data
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _node_id(node: Any) -> str:
|
|
148
|
+
"""Get a stable, clean string ID for a node."""
|
|
149
|
+
if hasattr(node, "name"):
|
|
150
|
+
return str(node.name)
|
|
151
|
+
return str(id(node))
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def dag_to_consort_flowchart(
|
|
155
|
+
G: nx.DiGraph,
|
|
156
|
+
*,
|
|
157
|
+
name: str = "consort",
|
|
158
|
+
fmt: str = "png",
|
|
159
|
+
fontname: str = "Helvetica",
|
|
160
|
+
fontsize: str = "11",
|
|
161
|
+
nodesep: str = "0.4",
|
|
162
|
+
ranksep: str = "0.5",
|
|
163
|
+
source_text: Optional[str] = None,
|
|
164
|
+
source_n: Optional[int | str] = None,
|
|
165
|
+
render_path: Optional[str] = None,
|
|
166
|
+
) -> Digraph:
|
|
167
|
+
"""
|
|
168
|
+
Build a CONSORT-style inclusion flowchart from a NetworkX DAG.
|
|
169
|
+
|
|
170
|
+
Classical CONSORT layout:
|
|
171
|
+
- Single vertical main chain (top → bottom)
|
|
172
|
+
- "Excluded" boxes branch off to the RIGHT, placed BEFORE (above)
|
|
173
|
+
their corresponding node via a T-junction on the incoming edge
|
|
174
|
+
- Splits (randomisation arms) place children side-by-side on the same rank
|
|
175
|
+
|
|
176
|
+
Parameters:
|
|
177
|
+
source_text : label for a virtual first node displayed above the root
|
|
178
|
+
source_n : sample size shown on the virtual first node
|
|
179
|
+
|
|
180
|
+
Node attributes (on the NetworkX graph):
|
|
181
|
+
- "text": str – main label text
|
|
182
|
+
- "n": int|str – sample size, shown as (n=…)
|
|
183
|
+
- "excluded_text": str – optional; text for the excluded side-box
|
|
184
|
+
"""
|
|
185
|
+
if not nx.is_directed_acyclic_graph(G):
|
|
186
|
+
raise ValueError("G must be a DAG.")
|
|
187
|
+
|
|
188
|
+
dot = Digraph(name=name, format=fmt)
|
|
189
|
+
dot.attr(rankdir="TB", splines="ortho", nodesep=nodesep, ranksep=ranksep)
|
|
190
|
+
dot.attr("node", shape="box", style="rounded", fontname=fontname, fontsize=fontsize)
|
|
191
|
+
dot.attr(
|
|
192
|
+
"edge",
|
|
193
|
+
fontname=fontname,
|
|
194
|
+
fontsize=str(max(int(fontsize) - 2, 8)),
|
|
195
|
+
arrowsize="0.8",
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
# --- helpers ---
|
|
199
|
+
def _label(node: Any) -> str:
|
|
200
|
+
d = G.nodes[node]
|
|
201
|
+
text = str(d.get("text", str(node)))
|
|
202
|
+
n = d.get("n")
|
|
203
|
+
return f"{text}\n(n={n})" if n is not None and str(n).strip() else text
|
|
204
|
+
|
|
205
|
+
def _safe_int(v: Any) -> Optional[int]:
|
|
206
|
+
"""Try to interpret v as int; return None on failure."""
|
|
207
|
+
if v is None:
|
|
208
|
+
return None
|
|
209
|
+
try:
|
|
210
|
+
return int(v)
|
|
211
|
+
except (ValueError, TypeError):
|
|
212
|
+
return None
|
|
213
|
+
|
|
214
|
+
def _excl_label(excl_text: str, parent_n: Any, child_n: Any) -> str:
|
|
215
|
+
"""Build the excluded-box label, with (n=…) when computable."""
|
|
216
|
+
pn = _safe_int(parent_n)
|
|
217
|
+
cn = _safe_int(child_n)
|
|
218
|
+
label = f"Excluded\n{excl_text}"
|
|
219
|
+
if pn is not None and cn is not None:
|
|
220
|
+
label += f"\n(n={pn - cn})"
|
|
221
|
+
return label
|
|
222
|
+
|
|
223
|
+
def _excl_node(node_id: str, label: str) -> None:
|
|
224
|
+
dot.node(
|
|
225
|
+
node_id,
|
|
226
|
+
label,
|
|
227
|
+
shape="box",
|
|
228
|
+
style="rounded,dashed",
|
|
229
|
+
fontname=fontname,
|
|
230
|
+
fontsize=fontsize,
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
# 1) Declare all main nodes
|
|
234
|
+
for u in G.nodes:
|
|
235
|
+
dot.node(_node_id(u), _label(u))
|
|
236
|
+
|
|
237
|
+
topo = list(nx.topological_sort(G))
|
|
238
|
+
excluded_drawn: set = set()
|
|
239
|
+
|
|
240
|
+
# 2) Virtual source node + root exclusion handling
|
|
241
|
+
root = topo[0]
|
|
242
|
+
root_id = _node_id(root)
|
|
243
|
+
root_excl = G.nodes[root].get("excluded_text")
|
|
244
|
+
|
|
245
|
+
# Build the virtual source node label
|
|
246
|
+
if source_text:
|
|
247
|
+
src_label = source_text
|
|
248
|
+
if source_n is not None and str(source_n).strip():
|
|
249
|
+
src_label += f"\n(n={source_n})"
|
|
250
|
+
dot.node("_source", src_label)
|
|
251
|
+
upstream_id = "_source"
|
|
252
|
+
upstream_n = source_n
|
|
253
|
+
else:
|
|
254
|
+
upstream_id = None
|
|
255
|
+
upstream_n = None
|
|
256
|
+
|
|
257
|
+
if root_excl:
|
|
258
|
+
excluded_drawn.add(root)
|
|
259
|
+
mid = f"_mid_{root_id}"
|
|
260
|
+
exc = f"_exc_{root_id}"
|
|
261
|
+
|
|
262
|
+
dot.node(mid, "", shape="point", width="0", height="0")
|
|
263
|
+
_excl_node(exc, _excl_label(root_excl, upstream_n, G.nodes[root].get("n")))
|
|
264
|
+
|
|
265
|
+
with dot.subgraph() as s:
|
|
266
|
+
s.attr(rank="same")
|
|
267
|
+
s.node(mid)
|
|
268
|
+
s.node(exc)
|
|
269
|
+
|
|
270
|
+
if upstream_id:
|
|
271
|
+
dot.edge(upstream_id, mid, arrowhead="none")
|
|
272
|
+
dot.edge(mid, exc)
|
|
273
|
+
dot.edge(mid, root_id)
|
|
274
|
+
elif upstream_id:
|
|
275
|
+
dot.edge(upstream_id, root_id)
|
|
276
|
+
|
|
277
|
+
# 3) Walk in topological order — exclusion boxes go on the
|
|
278
|
+
# CHILD's incoming edge (before the child), not the parent's outgoing edge.
|
|
279
|
+
for u in topo:
|
|
280
|
+
children = list(G.successors(u))
|
|
281
|
+
if not children:
|
|
282
|
+
continue
|
|
283
|
+
|
|
284
|
+
uid = _node_id(u)
|
|
285
|
+
parent_n = G.nodes[u].get("n")
|
|
286
|
+
|
|
287
|
+
# --- CASE A: single child (linear chain) ---
|
|
288
|
+
if len(children) == 1:
|
|
289
|
+
child = children[0]
|
|
290
|
+
cid = _node_id(child)
|
|
291
|
+
child_excl = (
|
|
292
|
+
G.nodes[child].get("excluded_text")
|
|
293
|
+
if child not in excluded_drawn
|
|
294
|
+
else None
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
if child_excl:
|
|
298
|
+
excluded_drawn.add(child)
|
|
299
|
+
mid = f"_mid_{cid}"
|
|
300
|
+
exc = f"_exc_{cid}"
|
|
301
|
+
|
|
302
|
+
dot.node(mid, "", shape="point", width="0", height="0")
|
|
303
|
+
_excl_node(
|
|
304
|
+
exc,
|
|
305
|
+
_excl_label(child_excl, parent_n, G.nodes[child].get("n")),
|
|
306
|
+
)
|
|
307
|
+
with dot.subgraph() as s:
|
|
308
|
+
s.attr(rank="same")
|
|
309
|
+
s.node(mid)
|
|
310
|
+
s.node(exc)
|
|
311
|
+
|
|
312
|
+
dot.edge(uid, mid, arrowhead="none")
|
|
313
|
+
dot.edge(mid, exc)
|
|
314
|
+
dot.edge(mid, cid)
|
|
315
|
+
else:
|
|
316
|
+
dot.edge(uid, cid)
|
|
317
|
+
|
|
318
|
+
# --- CASE B: multiple children (split / randomisation) ---
|
|
319
|
+
else:
|
|
320
|
+
with dot.subgraph() as s:
|
|
321
|
+
s.attr(rank="same")
|
|
322
|
+
for v in children:
|
|
323
|
+
s.node(_node_id(v))
|
|
324
|
+
|
|
325
|
+
child_mids: dict = {}
|
|
326
|
+
for child in children:
|
|
327
|
+
cid = _node_id(child)
|
|
328
|
+
child_excl = (
|
|
329
|
+
G.nodes[child].get("excluded_text")
|
|
330
|
+
if child not in excluded_drawn
|
|
331
|
+
else None
|
|
332
|
+
)
|
|
333
|
+
if child_excl:
|
|
334
|
+
excluded_drawn.add(child)
|
|
335
|
+
mid = f"_mid_{cid}"
|
|
336
|
+
exc_id = f"_exc_{cid}"
|
|
337
|
+
dot.node(mid, "", shape="point", width="0", height="0")
|
|
338
|
+
_excl_node(
|
|
339
|
+
exc_id,
|
|
340
|
+
_excl_label(child_excl, parent_n, G.nodes[child].get("n")),
|
|
341
|
+
)
|
|
342
|
+
child_mids[child] = (mid, exc_id)
|
|
343
|
+
|
|
344
|
+
if child_mids:
|
|
345
|
+
with dot.subgraph() as s:
|
|
346
|
+
s.attr(rank="same")
|
|
347
|
+
for mid, exc_id in child_mids.values():
|
|
348
|
+
s.node(mid)
|
|
349
|
+
s.node(exc_id)
|
|
350
|
+
|
|
351
|
+
for child in children:
|
|
352
|
+
cid = _node_id(child)
|
|
353
|
+
if child in child_mids:
|
|
354
|
+
mid, exc_id = child_mids[child]
|
|
355
|
+
dot.edge(uid, mid, arrowhead="none")
|
|
356
|
+
dot.edge(mid, exc_id)
|
|
357
|
+
dot.edge(mid, cid)
|
|
358
|
+
else:
|
|
359
|
+
dot.edge(uid, cid)
|
|
360
|
+
|
|
361
|
+
if render_path:
|
|
362
|
+
dot.render(render_path, cleanup=True)
|
|
363
|
+
|
|
364
|
+
return dot
|