Files
bio-performx/app/services/__pycache__/context_generator.cpython-312.pyc
T

71 lines
13 KiB
Plaintext
Raw Normal View History

Ë
Óåàhð)ãóHdZddlmZddlmZmZmZmZddlZGdd«Z y)
Context Generator Service
This service processes all data files and generates context dictionaries for each page
of the medical report. It performs analysis on Pnoe, Spirometry, and SECA data.
é)Údatetime)ÚDictÚListÚOptionalÚTupleNcóÖeZdZdZdZdededefdZdZded efd
Z d efd Z
d efd Z d e e
ee
effd
Zde
ede
edej d efdZdedeeefd eefdZy)ÚContextGeneratorz&Generate context data for report pagescó<d|_d|_d|_i|_y)N)Úpnoe_dfÚ
spirometry_dfÚseca_dfÚ patient_info)Úselfs úW/home/oluwasanmi/Documents/Work/MKD/report_generation/app/services/context_generator.pyÚ__init__zContextGenerator.__init__s ؈Œ ØÔ؈Œ ؈ÕóÚ pnoe_pathÚspirometry_pathÚ seca_pathcóÄtj|d¬«|_tj|«|_tj|«|_|j
«y)zLoad all required datasetsú;)Ú delimiterN)ÚpdÚread_csvr r Ú
read_excelr
Ú_preprocess_pnoe_data)rrrrs rÚ load_datazContextGenerator.load_datasCô—{{ 9¸ÔŒ ÜŸ[™[¨ÓÔÜ—}‘} YÓ/ˆŒ Ø ×$rcóú|jjD]2} tj|j|«|j|<Œ4|jd|jdz |jd<|jd|jdz |jd<|jd|jdzdz |jd <|jd|jd
zdz |jd <d }gd
¢}|D]Y}||jjvsŒ|j|j
|d¬«j«|j|d<Œ[y#tt
f$rYŒbwxYw)z&Apply preprocessing steps to Pnoe dataú VO2(ml/min)úHR(bpm)ú VO2 PulseúBF(bpm)ú
VO2 Breathz EE(kcal/min)zCARBS(%)édÚCHOzFAT(%)ÚFATé
)
rz VCO2(ml/min)r zVT(l)r"z VE(l/min)r!r#r%r&é)ÚwindowÚ min_periodsÚ _smoothedN)r ÚcolumnsrÚ
to_numericÚ
ValueErrorÚ TypeErrorÚrollingÚmean)rÚcolÚ window_sizeÚcolumns_to_smooths rrz&ContextGenerator._preprocess_pnoe_data#syð—<<× ˆ
Ü$&§M¡M°$·,±,¸sÑ2CÓ$D ˜SÒ ð
L‰L˜Ñ '¨$¯,©,°yÑ*AÑ 

L‰L˜Ñ '¨$¯,©,°yÑ*AÑ 

L‰L˜Ñ (¨4¯<©<¸
Ñ+CÑ CÀcÑ 
ð
L‰L˜Ñ (¨4¯<©<¸Ñ+AÑ AÀCÑ 
ðˆ ò 
Ðð ˆCØd—l‘l×—L‘L Ñ-°[ÈaÐ  ˜u IÐ øô=¤ Ð
Úð
ús/E'Å'E:Å9E:Ú patient_nameÚreturnc
ó®|j=|j|jdjj|dd¬«}|jsù|jd}t |j
dd««}t |j
dd««}|j
dd«d |j
dd«|j
dd«|j
dd«t|j
d
d««|j
d d«||j
d d«j«|||zd
z dz|d|d
z z
zdzdœ
|_ |jS)z-Extract patient information from SECA datasetÚLastNameF)ÚcaseÚnarÚWeightÚ Adult_FMPÚ FirstNameÚú ÚAgeÚHeightÚGenderr$g3܀ϣ@r()
ÚnameÚ
first_nameÚ last_nameÚageÚheightÚweightÚgenderÚfat_percentageÚ fat_mass_lbsÚ
lean_mass_lbs)
r
ÚstrÚcontainsÚemptyÚilocÚfloatÚgetÚintÚlowerr)rr5Ú patient_dataÚrowÚ weight_kgÚfat_pcts rÚextract_patient_infoz%ContextGenerator.extract_patient_infoMsMà <‰<Ñ Ÿ<™<Ø ˜(×  u°ðñˆLð
 ×"×Ñ*Ü! #§'¡'¨(°AÓ"6Ó7 Ü §¡¨ °QÓ 7Ó8ð #Ÿw™w {°BÓ8¸¸#¿'¹'À*ÈbÓ:QÐ9RÐSØ"%§'¡'¨+°rÓ":Ø!$§¡¨°RÓ!8ܘsŸw™w u¨aÓ1Ø!$§¡¨°2Ó!6Ð 7Ø!Ÿg™g Ó3×;Ø&-Ø$-°Ñ$7¸#Ñ$=ÀÑ$GØ%.°!°gÀ±mÑ2CÑ%DÀwÑ%Nñ %Ô× Ñ Ð rcóti}dD}|jj|jdjj«|k(}|jrŒQ|j «j
dd«j
dd«}|djd||d <|d
jd||d <Œ²|S) z$Calculate spirometry-related metrics)ÚFVCÚFEV1z FEV1/FVC%Ú
Parametersú_pctÚBestrÚ_bestz%Pred.Ú_pred)r ÚlocrMÚstriprOrTÚreplaceÚvalues)rÚmetricsÚparamrVÚ param_keys rÚcalculate_spirometry_metricsz-ContextGenerator.calculate_spirometry_metricshàˆØ GˆEØ×$××" 0×4×ш—9“9Ø!ŸK™K›M×1°#°sÓ;×CÀCÈÓP Ø/2°6©{×/AÑ/AÀ!Ñ/D˜9˜+ ,Ø/2°8©}×/CÑ/CÀAÑ/F˜9˜+  Gðˆrcói}|jdj«|d<|d|jdz |d<|jdj«}|jj|}|d|d<|d|d<|jd j«}|jj|}|d |d
<|d|d <|j «\}}||d <||d
<|j
|||«}|j|«|S)z"Calculate all Pnoe-derived metricszVO2(ml/min)_smoothedÚvo2_maxrHÚvo2_max_per_kgzVT(l)_smoothedÚpeak_vtúHR(bpm)_smoothedÚ
peak_vt_hrÚ FAT_smoothedÚ
fat_max_valueÚ
fat_max_hrÚvt1Úvt2)r ÚmaxrÚidxmaxreÚ_detect_thresholdsÚ_calculate_hr_zonesÚupdate) rriÚ peak_vt_idxÚ peak_vt_rowÚ fat_max_idxÚ fat_max_rowrvrwÚzoness rÚcalculate_pnoe_metricsz'ContextGenerator.calculate_pnoe_metricsus#àˆØ!Ÿ\™\Ð*@ÑGˆ ÑØ$+¨IÑ$6¸×9JÑ9JÈ8Ñ9TÑ$TˆÐ Ñ—l‘lÐ#3Ñ=ˆ Ø—ll×& 3ˆ Ø(Ð)9Ñ:ˆ ÑØ +Ð,>Ñ ?ˆ Ñà—ll ;ˆ Ø—ll×& 3ˆ Ø#.¨~Ñ#>ˆÑ Ø +Ð,>Ñ ?ˆ Ñà×,‰ˆˆS؈؈àרc°;ÓØØˆrcóØ|jd|jdkD}||j}d}t|«dkDr-|d}|jj|}|d|d|ddœ}|jd j «}|j «}|j «}d} t