meta-edc 1.0.6__py3-none-any.whl → 1.0.7__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 (33) hide show
  1. meta_analytics/dataframes/__init__.py +3 -0
  2. meta_analytics/dataframes/constants.py +1 -1
  3. meta_analytics/dataframes/enrolled/__init__.py +0 -1
  4. meta_analytics/dataframes/get_eos_df.py +15 -2
  5. meta_analytics/dataframes/get_glucose_df.py +149 -0
  6. meta_analytics/dataframes/get_glucose_fbg_df.py +27 -0
  7. meta_analytics/dataframes/get_glucose_fbg_ogtt_df.py +22 -0
  8. meta_analytics/dataframes/glucose_endpoints/endpoint_by_date.py +106 -120
  9. meta_analytics/dataframes/glucose_endpoints/glucose_endpoints_by_date.py +36 -227
  10. meta_analytics/dataframes/utils.py +18 -4
  11. meta_analytics/notebooks/hiv_regimens.ipynb +425 -0
  12. meta_analytics/notebooks/monitoring_report.ipynb +1561 -0
  13. meta_analytics/notebooks/pharmacy.ipynb +971 -0
  14. meta_analytics/utils.py +81 -0
  15. {meta_edc-1.0.6.dist-info → meta_edc-1.0.7.dist-info}/METADATA +4 -3
  16. {meta_edc-1.0.6.dist-info → meta_edc-1.0.7.dist-info}/RECORD +32 -18
  17. {meta_edc-1.0.6.dist-info → meta_edc-1.0.7.dist-info}/WHEEL +1 -1
  18. meta_edc-1.0.7.dist-info/licenses/AUTHORS.rst +8 -0
  19. meta_reports/migrations/0054_auto_20250422_2003.py +81 -0
  20. meta_reports/migrations/0055_alter_glucosesummary_table.py +17 -0
  21. meta_reports/migrations/0056_auto_20250422_2214.py +54 -0
  22. meta_reports/migrations/0057_auto_20250422_2224.py +54 -0
  23. meta_reports/migrations/0058_auto_20250422_2232.py +54 -0
  24. meta_reports/models/dbviews/glucose_summary/unmanaged_model.py +13 -1
  25. meta_reports/models/dbviews/glucose_summary/view_definition.py +8 -5
  26. meta_subject/form_validators/glucose_form_validator.py +16 -1
  27. meta_subject/forms/study_medication_form.py +5 -3
  28. meta_subject/migrations/0221_auto_20250402_1913.py +42 -0
  29. meta_subject/migrations/0222_alter_historicalstudymedication_stock_codes_and_more.py +46 -0
  30. meta_analytics/dataframes/enrolled/get_glucose_df.py +0 -122
  31. /meta_edc-1.0.6.dist-info/AUTHORS → /meta_analytics/dataframes/glucose_endpoints/utils.py +0 -0
  32. {meta_edc-1.0.6.dist-info → meta_edc-1.0.7.dist-info/licenses}/LICENSE +0 -0
  33. {meta_edc-1.0.6.dist-info → meta_edc-1.0.7.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,425 @@
1
+ {
2
+ "cells": [
3
+ {
4
+ "metadata": {},
5
+ "cell_type": "code",
6
+ "source": [
7
+ "%%capture\n",
8
+ "import os\n",
9
+ "from pathlib import Path\n",
10
+ "import pandas as pd\n",
11
+ "from dj_notebook import activate\n",
12
+ "\n",
13
+ "env_file = os.environ[\"META_ENV\"]\n",
14
+ "reports_folder = Path(os.environ[\"META_REPORTS_FOLDER\"])\n",
15
+ "analysis_folder = Path(os.environ[\"META_ANALYSIS_FOLDER\"])\n",
16
+ "pharmacy_folder = Path(os.environ[\"META_PHARMACY_FOLDER\"])\n",
17
+ "plus = activate(dotenv_file=env_file)\n",
18
+ "pd.set_option('future.no_silent_downcasting', True)"
19
+ ],
20
+ "id": "5c3bc2c5cc22e357",
21
+ "outputs": [],
22
+ "execution_count": null
23
+ },
24
+ {
25
+ "metadata": {},
26
+ "cell_type": "code",
27
+ "source": [
28
+ "from edc_pdutils.dataframes import get_crf, get_subject_visit\n",
29
+ "from edc_constants.constants import YES\n",
30
+ "from edc_appointment.analytics import get_appointment_df\n",
31
+ "from datetime import datetime"
32
+ ],
33
+ "id": "4067e6257bf1c657",
34
+ "outputs": [],
35
+ "execution_count": null
36
+ },
37
+ {
38
+ "metadata": {},
39
+ "cell_type": "code",
40
+ "source": [
41
+ "cutoff_datetime = datetime(2026,3,1)\n",
42
+ "df_patienthistory = get_crf(\"meta_subject.patienthistory\", subject_visit_model=\"meta_subject.subjectvisit\")\n",
43
+ "df_followup_examination = get_crf(\"meta_subject.FollowupExamination\", subject_visit_model=\"meta_subject.subjectvisit\")"
44
+ ],
45
+ "id": "5a00d10dadc9d7f8",
46
+ "outputs": [],
47
+ "execution_count": null
48
+ },
49
+ {
50
+ "metadata": {},
51
+ "cell_type": "code",
52
+ "source": [
53
+ "replacements = {\n",
54
+ " \"ABC+ 3TC+ DTG\": \"ABC + 3TC + DTG\",\n",
55
+ " \"ABC+3TC+DTG\": \"ABC + 3TC + DTG\",\n",
56
+ " \"ABC, 3TC, DTG\":\"ABC + 3TC + DTG\",\n",
57
+ " \"ABC, EFV, DTG\": \"ABC + EFV + DTG\",\n",
58
+ " \"TDF+FTC+DTG\": \"TDF + FTC + DTG\",\n",
59
+ " \"AZT+3TC+DTG\": \"AZT + 3TC + DTG\",\n",
60
+ " \"AZT + 3 TC + DTG\":\"AZT + 3TC + DTG\",\n",
61
+ " \"TDF+3TC+DTG\": \"TDF + 3TC + DTG\",\n",
62
+ "}\n",
63
+ "\n",
64
+ "df_patienthistory[\"other_current_arv_regimen\"] = (\n",
65
+ " df_patienthistory[\"other_current_arv_regimen\"]\n",
66
+ " .replace(replacements)\n",
67
+ ")"
68
+ ],
69
+ "id": "cc2958e795b6c83f",
70
+ "outputs": [],
71
+ "execution_count": null
72
+ },
73
+ {
74
+ "metadata": {},
75
+ "cell_type": "code",
76
+ "source": [
77
+ "df_patienthistory['regimen'] = df_patienthistory[\"current_arv_regimen\"]\n",
78
+ "df_patienthistory.loc[df_patienthistory[\"current_arv_regimen\"]==\"Other, specify ...\", \"regimen\"] = df_patienthistory[\"other_current_arv_regimen\"]"
79
+ ],
80
+ "id": "41a0202e91442199",
81
+ "outputs": [],
82
+ "execution_count": null
83
+ },
84
+ {
85
+ "metadata": {},
86
+ "cell_type": "code",
87
+ "source": [
88
+ "df_followup_examination[\"art_new_regimen_other\"] = (\n",
89
+ " df_followup_examination[\"art_new_regimen_other\"]\n",
90
+ " .replace(replacements)\n",
91
+ ")"
92
+ ],
93
+ "id": "29ee66d690a550b",
94
+ "outputs": [],
95
+ "execution_count": null
96
+ },
97
+ {
98
+ "metadata": {},
99
+ "cell_type": "code",
100
+ "source": [
101
+ "df_followup_examination['regimen'] = pd.NA\n",
102
+ "df_followup_examination.loc[(df_followup_examination[\"art_change\"]==YES) & (df_followup_examination.art_new_regimen_other.notna()), \"regimen\"] = df_followup_examination[\"art_new_regimen_other\"]\n",
103
+ "df_followup_examination.loc[(df_followup_examination[\"art_change\"]==YES) & (df_followup_examination.art_new_regimen_other.isna()), \"regimen\"] = \"CHANGE_NOT_REPORTED\""
104
+ ],
105
+ "id": "3356b70f9415f8cf",
106
+ "outputs": [],
107
+ "execution_count": null
108
+ },
109
+ {
110
+ "metadata": {},
111
+ "cell_type": "code",
112
+ "source": [
113
+ "df_regimen = pd.concat([df_patienthistory[[\"subject_identifier\", \"visit_datetime\", \"regimen\"]], df_followup_examination[[\"subject_identifier\", \"visit_datetime\", \"regimen\"]]])\n",
114
+ "df_regimen[\"regimen\"] = df_regimen[\"regimen\"].replace({\"Other second line\": \"CHANGE_NOT_REPORTED\"})\n",
115
+ "df_regimen[\"regimen\"] = pd.Categorical(df_regimen[\"regimen\"], categories=list(df_regimen.query(\"regimen.notna()\").regimen.unique()), ordered=False)\n",
116
+ "df_regimen = df_regimen.sort_values([\"subject_identifier\", \"visit_datetime\"])\n",
117
+ "df_regimen = df_regimen.reset_index(drop=True)"
118
+ ],
119
+ "id": "bfa119a47ab4827e",
120
+ "outputs": [],
121
+ "execution_count": null
122
+ },
123
+ {
124
+ "metadata": {},
125
+ "cell_type": "code",
126
+ "source": [
127
+ "df_pivot = df_regimen.pivot_table(values=\"visit_datetime\", columns=\"regimen\", index=\"subject_identifier\", observed=True)\n",
128
+ "df_pivot = df_pivot.reset_index()"
129
+ ],
130
+ "id": "10d8f6687f35b5e4",
131
+ "outputs": [],
132
+ "execution_count": null
133
+ },
134
+ {
135
+ "metadata": {},
136
+ "cell_type": "code",
137
+ "source": [
138
+ "subject_identifier = \"105-20-0050-0\"\n",
139
+ "df_pivot[df_pivot.subject_identifier==subject_identifier].melt().query(\"value.notna() and regimen!='subject_identifier'\").sort_values(\"value\", ascending=False)"
140
+ ],
141
+ "id": "d3b3a1a80436cc1d",
142
+ "outputs": [],
143
+ "execution_count": null
144
+ },
145
+ {
146
+ "metadata": {},
147
+ "cell_type": "code",
148
+ "source": "df_melt = df_pivot.melt(id_vars=[\"subject_identifier\"]).query(\"value.notna()\")",
149
+ "id": "b82ea4629050675e",
150
+ "outputs": [],
151
+ "execution_count": null
152
+ },
153
+ {
154
+ "metadata": {},
155
+ "cell_type": "code",
156
+ "source": [
157
+ "df_melt[\"max_date\"] = df_melt.groupby(\"subject_identifier\")[\"value\"].transform(\"max\")\n",
158
+ "df_melt[\"current_regimen\"] = df_melt[df_melt.value==df_melt.max_date][\"regimen\"]"
159
+ ],
160
+ "id": "46906641b5f8e73b",
161
+ "outputs": [],
162
+ "execution_count": null
163
+ },
164
+ {
165
+ "metadata": {},
166
+ "cell_type": "code",
167
+ "source": "df_current_regimens = df_melt.query(\"current_regimen.notna()\")[[\"subject_identifier\", \"max_date\", \"current_regimen\"]].copy()",
168
+ "id": "c297749df4c7397c",
169
+ "outputs": [],
170
+ "execution_count": null
171
+ },
172
+ {
173
+ "metadata": {},
174
+ "cell_type": "code",
175
+ "source": [
176
+ "df_visit = get_subject_visit(model=\"meta_subject.subjectvisit\")\n",
177
+ "df_visit = df_visit[df_visit.visit_code==1000.0].copy()"
178
+ ],
179
+ "id": "ce1f8d3dacba2d99",
180
+ "outputs": [],
181
+ "execution_count": null
182
+ },
183
+ {
184
+ "metadata": {},
185
+ "cell_type": "code",
186
+ "source": [
187
+ "df_appointment = get_appointment_df()\n",
188
+ "df_appointment_next = (\n",
189
+ " df_appointment\n",
190
+ " .groupby(by=[\"subject_identifier\", \"next_appt_datetime\", \"next_visit_code\"])\n",
191
+ " .size()\n",
192
+ " .copy()\n",
193
+ " .reset_index()\n",
194
+ ")"
195
+ ],
196
+ "id": "72c7862945e8a25e",
197
+ "outputs": [],
198
+ "execution_count": null
199
+ },
200
+ {
201
+ "metadata": {},
202
+ "cell_type": "code",
203
+ "source": [
204
+ "df_appointment_last = (\n",
205
+ " df_appointment[df_appointment.appt_datetime<cutoff_datetime][[\"subject_identifier\", \"appt_datetime\", \"visit_code\"]]\n",
206
+ " .sort_values([\"subject_identifier\", \"appt_datetime\", \"visit_code\"])\n",
207
+ " .groupby(by=[\"subject_identifier\"])\n",
208
+ " .agg([\"last\"])\n",
209
+ " .reset_index() )\n",
210
+ "df_appointment_last.columns = [\"_\".join(col).strip() for col in df_appointment_last.columns.values]\n",
211
+ "df_appointment_last = (\n",
212
+ " df_appointment_last\n",
213
+ " .rename(columns={\n",
214
+ " \"subject_identifier_\":\"subject_identifier\",\n",
215
+ " \"appt_datetime_last\":\"last_appt_datetime\",\n",
216
+ " \"visit_code_last\":\"last_visit_code\"\n",
217
+ " }\n",
218
+ " )\n",
219
+ ")"
220
+ ],
221
+ "id": "cecd662f3a76a4ac",
222
+ "outputs": [],
223
+ "execution_count": null
224
+ },
225
+ {
226
+ "metadata": {},
227
+ "cell_type": "code",
228
+ "source": [
229
+ "df = df_current_regimens.merge(df_visit[[ \"subject_identifier\", \"baseline_datetime\", \"endline_visit_datetime\", \"endline_visit_code\"]], on=\"subject_identifier\", how=\"left\")\n",
230
+ "df = df.reset_index(drop=True)\n",
231
+ "df[\"changed\"] = False\n",
232
+ "df.loc[df.max_date != df.baseline_datetime, \"changed\"] = True"
233
+ ],
234
+ "id": "46700ad09717a89f",
235
+ "outputs": [],
236
+ "execution_count": null
237
+ },
238
+ {
239
+ "metadata": {},
240
+ "cell_type": "code",
241
+ "source": [
242
+ "df = df.merge(df_appointment_next[[\"subject_identifier\", \"next_appt_datetime\", \"next_visit_code\"]], on=\"subject_identifier\", how=\"left\")\n",
243
+ "df = df.merge(df_appointment_last[[\"subject_identifier\", \"last_appt_datetime\", \"last_visit_code\"]], on=\"subject_identifier\", how=\"left\")"
244
+ ],
245
+ "id": "21a77db6b3d342ad",
246
+ "outputs": [],
247
+ "execution_count": null
248
+ },
249
+ {
250
+ "metadata": {},
251
+ "cell_type": "code",
252
+ "source": [
253
+ "# from last seen to final scheduled appt\n",
254
+ "df[\"remaining_delta_from_last_seen\"] = df.last_appt_datetime - df.endline_visit_datetime\n",
255
+ "df[\"remaining_delta_from_last_seen\"] = df[\"remaining_delta_from_last_seen\"].apply(lambda x: 0 if x.total_seconds()<0 else x)\n",
256
+ "df[\"remaining_delta_from_last_seen\"] = pd.to_timedelta(df[\"remaining_delta_from_last_seen\"])\n",
257
+ "df[\"remaining_days_last_seen_to_final\"] = df[\"remaining_delta_from_last_seen\"].dt.days\n",
258
+ "\n",
259
+ "# from now to final scheduled appt\n",
260
+ "df[\"remaining_delta_from_now\"] = 0.0\n",
261
+ "df[\"remaining_delta_from_now\"] = df[df.remaining_days_last_seen_to_final>0].last_appt_datetime - datetime.now()\n",
262
+ "df[\"remaining_delta_from_now\"] = df[\"remaining_delta_from_now\"].apply(lambda x: 0 if x.total_seconds()<0 else x)\n",
263
+ "df[\"remaining_delta_from_now\"] = pd.to_timedelta(df[\"remaining_delta_from_now\"])\n",
264
+ "df[\"remaining_days_now_to_final\"] = df[\"remaining_delta_from_now\"].dt.days\n",
265
+ "\n",
266
+ "# from next appointment to final scheduled appt\n",
267
+ "df[\"remaining_delta_from_next\"] = 0.0\n",
268
+ "df[\"remaining_delta_from_next\"] = df[df.remaining_days_last_seen_to_final>0].last_appt_datetime - df[df.remaining_days_last_seen_to_final>0].next_appt_datetime\n",
269
+ "df[\"remaining_delta_from_next\"] = df[\"remaining_delta_from_next\"].apply(lambda x: 0 if x.total_seconds()<0 else x)\n",
270
+ "df[\"remaining_delta_from_next\"] = pd.to_timedelta(df[\"remaining_delta_from_next\"])\n",
271
+ "df[\"remaining_days_next_to_final\"] = df[\"remaining_delta_from_next\"].dt.days"
272
+ ],
273
+ "id": "46455533d4c928a5",
274
+ "outputs": [],
275
+ "execution_count": null
276
+ },
277
+ {
278
+ "metadata": {},
279
+ "cell_type": "code",
280
+ "source": [
281
+ "df_final = (\n",
282
+ " df\n",
283
+ " .rename(columns={\n",
284
+ " \"max_date\": \"current_regimen_date\",\n",
285
+ " \"endline_visit_code\": \"last_attended_visit_code\",\n",
286
+ " \"endline_visit_datetime\": \"last_attended_visit_datetime\",\n",
287
+ " })\n",
288
+ " .copy()\n",
289
+ ")\n",
290
+ "df_final = df_final[[\n",
291
+ " \"subject_identifier\",\n",
292
+ " \"current_regimen\",\n",
293
+ " \"current_regimen_date\",\n",
294
+ " \"changed\",\n",
295
+ " \"baseline_datetime\",\n",
296
+ " \"last_attended_visit_code\",\n",
297
+ " \"last_attended_visit_datetime\",\n",
298
+ " \"next_visit_code\",\n",
299
+ " \"next_appt_datetime\",\n",
300
+ " \"last_visit_code\",\n",
301
+ " \"last_appt_datetime\",\n",
302
+ " \"remaining_days_last_seen_to_final\",\n",
303
+ " \"remaining_days_now_to_final\",\n",
304
+ " \"remaining_days_next_to_final\",\n",
305
+ "]].copy()\n",
306
+ "\n",
307
+ "df_final = (\n",
308
+ " df_final\n",
309
+ " .sort_values(\"subject_identifier\")\n",
310
+ " .reset_index(drop=True)\n",
311
+ ")\n",
312
+ "df_final[\"remaining_days_last_seen_to_final\"] = df_final[\"remaining_days_last_seen_to_final\"].astype(\"float64\").fillna(0)\n",
313
+ "df_final[\"remaining_days_now_to_final\"] = df_final[\"remaining_days_now_to_final\"].astype(\"float64\").fillna(0)\n",
314
+ "df_final[\"remaining_days_next_to_final\"] = df_final[\"remaining_days_next_to_final\"].astype(\"float64\").fillna(0)\n",
315
+ "df_final"
316
+ ],
317
+ "id": "c32993c296d84def",
318
+ "outputs": [],
319
+ "execution_count": null
320
+ },
321
+ {
322
+ "metadata": {},
323
+ "cell_type": "code",
324
+ "source": [
325
+ "# need from now until end of study\n",
326
+ "df_summary1 = (pd.merge(\n",
327
+ " df_final.groupby(by=[\"current_regimen\"]).remaining_days_now_to_final.sum(),\n",
328
+ " df_final.groupby(by=[\"current_regimen\"]).subject_identifier.count(), on=\"current_regimen\")\n",
329
+ " .rename(columns={\n",
330
+ " \"remaining_days_now_to_final\": \"days_medication_needed\",\n",
331
+ " \"subject_identifier\": \"subjects\"\n",
332
+ " })\n",
333
+ " .sort_values(\"days_medication_needed\", ascending=False)\n",
334
+ " .reset_index()\n",
335
+ ")\n",
336
+ "df_summary1"
337
+ ],
338
+ "id": "3b8b54a63bc67608",
339
+ "outputs": [],
340
+ "execution_count": null
341
+ },
342
+ {
343
+ "metadata": {},
344
+ "cell_type": "code",
345
+ "source": [
346
+ "# need from last seen to end of study\n",
347
+ "df_summary2 = (pd.merge(\n",
348
+ " df_final.groupby(by=[\"current_regimen\"]).remaining_days_last_seen_to_final.sum(),\n",
349
+ " df_final.groupby(by=[\"current_regimen\"]).subject_identifier.count(), on=\"current_regimen\")\n",
350
+ " .rename(columns={\n",
351
+ " \"remaining_days_last_seen_to_final\": \"days_medication_needed\",\n",
352
+ " \"subject_identifier\": \"subjects\"\n",
353
+ " })\n",
354
+ " .sort_values(\"days_medication_needed\", ascending=False)\n",
355
+ " .reset_index()\n",
356
+ ")\n",
357
+ "df_summary2"
358
+ ],
359
+ "id": "9fcf09fbc781845b",
360
+ "outputs": [],
361
+ "execution_count": null
362
+ },
363
+ {
364
+ "metadata": {},
365
+ "cell_type": "code",
366
+ "source": [
367
+ "# need from next to end of study\n",
368
+ "df_summary3 = (pd.merge(\n",
369
+ " df_final.groupby(by=[\"current_regimen\"]).remaining_days_next_to_final.sum(),\n",
370
+ " df_final.groupby(by=[\"current_regimen\"]).subject_identifier.count(), on=\"current_regimen\")\n",
371
+ " .rename(columns={\n",
372
+ " \"remaining_days_next_to_final\": \"days_medication_needed\",\n",
373
+ " \"subject_identifier\": \"subjects\"\n",
374
+ " })\n",
375
+ " .sort_values(\"days_medication_needed\", ascending=False)\n",
376
+ " .reset_index()\n",
377
+ ")\n",
378
+ "\n",
379
+ "df_summary3"
380
+ ],
381
+ "id": "9d4bebb0a52a8457",
382
+ "outputs": [],
383
+ "execution_count": null
384
+ },
385
+ {
386
+ "metadata": {},
387
+ "cell_type": "code",
388
+ "source": [
389
+ "with pd.ExcelWriter(\n",
390
+ " analysis_folder / \"hiv_medication.xlsx\",\n",
391
+ " date_format=\"YYYY-MM-DD\",\n",
392
+ " datetime_format=\"YYYY-MM-DD HH:MM:SS\"\n",
393
+ ") as writer:\n",
394
+ " df_final.to_excel(writer, sheet_name=\"subjects\", index=False)\n",
395
+ " df_summary1.to_excel(writer, sheet_name=\"now to final\", index=False)\n",
396
+ " df_summary2.to_excel(writer, sheet_name=\"last seen to final\", index=False)\n",
397
+ " df_summary3.to_excel(writer, sheet_name=\"next to final\", index=False)\n"
398
+ ],
399
+ "id": "e344164f67b3cc46",
400
+ "outputs": [],
401
+ "execution_count": null
402
+ }
403
+ ],
404
+ "metadata": {
405
+ "kernelspec": {
406
+ "display_name": "Python 3",
407
+ "language": "python",
408
+ "name": "python3"
409
+ },
410
+ "language_info": {
411
+ "codemirror_mode": {
412
+ "name": "ipython",
413
+ "version": 2
414
+ },
415
+ "file_extension": ".py",
416
+ "mimetype": "text/x-python",
417
+ "name": "python",
418
+ "nbconvert_exporter": "python",
419
+ "pygments_lexer": "ipython2",
420
+ "version": "2.7.6"
421
+ }
422
+ },
423
+ "nbformat": 4,
424
+ "nbformat_minor": 5
425
+ }