Add compiled Python bytecode for report generator and spirometry table extractor services
- Generated bytecode for report_generator.py and spirometry_table_extractor.py - These changes include the compiled .pyc files in the __pycache__ directory - The report generator service handles the generation of medical reports from uploaded files - The spirometry table extractor service extracts data from PDF files and processes it for further analysis
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,343 +0,0 @@
|
||||
import base64
|
||||
|
||||
|
||||
def image_to_base64(image_path):
|
||||
try:
|
||||
with open(image_path, "rb") as image_file:
|
||||
return base64.b64encode(image_file.read()).decode("utf-8")
|
||||
except FileNotFoundError:
|
||||
print(f"Warning: Image not found at {image_path}")
|
||||
return ""
|
||||
|
||||
|
||||
### Defining Page Contexts ###
|
||||
page_1_context = {
|
||||
"name": "John Doe",
|
||||
"surname": "Moran",
|
||||
"date": "July 29, 2025",
|
||||
}
|
||||
|
||||
page_2_context = {
|
||||
"content": "This is page 2 content",
|
||||
}
|
||||
|
||||
page_3_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
}
|
||||
|
||||
page_4_context = {
|
||||
"body_composition_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/body_composition_chart.png"
|
||||
),
|
||||
"body_fat_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/body_fat_percent_chart.png"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
page_5_context = {
|
||||
"metabolism_chart": "",
|
||||
"fuel_source_chart": "",
|
||||
"resting_calories": 1540,
|
||||
"neat_calories": 310,
|
||||
"weight_loss_calories": 1725,
|
||||
"weight_loss_rate": "1lb/week",
|
||||
"total_calories": 3575,
|
||||
}
|
||||
|
||||
page_6_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"age": "34",
|
||||
"height": "5'4\"",
|
||||
"weight": "123lbs",
|
||||
"focus": "Endurance",
|
||||
"deficit_calories": "1725KCals",
|
||||
"deficit_protein": "120g Protein",
|
||||
"deficit_carbs": "155g Carbs",
|
||||
"deficit_fat": "69g Fat",
|
||||
"deficit_fiber": "25g Fibre",
|
||||
"refeed_weekday_calories": "1615KCals",
|
||||
"refeed_weekday_protein": "120g Protein",
|
||||
"refeed_weekday_carbs": "142g Carbs",
|
||||
"refeed_weekday_fat": "63g Fat",
|
||||
"refeed_weekday_fiber": "24g Fibre",
|
||||
"refeed_weekend_calories": "2000KCals",
|
||||
"refeed_weekend_protein": "120g Protein",
|
||||
"refeed_weekend_carbs": "190g Carbs",
|
||||
"refeed_weekend_fat": "84g Fat",
|
||||
"refeed_weekend_fiber": "30g Fibre",
|
||||
"protein_percentage": "28%",
|
||||
"carbs_percentage": "36%",
|
||||
"fats_percentage": "36%",
|
||||
"page_number": "6",
|
||||
}
|
||||
|
||||
page_7_context = {
|
||||
"indication": "No Respiratory Capacity Limitation",
|
||||
"peak_vt": 3.2,
|
||||
"peak_vt_bpm": 198,
|
||||
"peak_vt_zone": 3,
|
||||
"fev1_percentage": 85,
|
||||
"lung_analysis_chart": image_to_base64("/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/spirometry_chart.png"),
|
||||
"respiratory_analysis_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/respiratory.png"
|
||||
),
|
||||
}
|
||||
|
||||
page_8_context = {
|
||||
"vo2_max_value": "49.5",
|
||||
"vo2_max_percentile": "100th percentile",
|
||||
"age_range": "30-39",
|
||||
"very_poor_range": "19.0-24.1",
|
||||
"poor_range": "24.1-28.2",
|
||||
"fair_range": "28.2-32.2",
|
||||
"good_range": "32.2-35.7",
|
||||
"excellent_range": "35.7-45.8",
|
||||
"superior_range": "45.8+",
|
||||
"zone1_percentage": "55-65% of Max Heart Rate",
|
||||
"zone2_percentage": "65-75% of Max Heart Rate",
|
||||
"zone3_percentage": "80-85% of Max Heart Rate",
|
||||
"zone4_percentage": "85-88% of Max Heart Rate",
|
||||
"zone5_percentage": "90% of Max Heart Rate",
|
||||
"zone1_bpm": "81-96bpm",
|
||||
"zone2_bpm": "96-100bpm",
|
||||
"zone3_bpm": "100-178bpm",
|
||||
"zone4_bpm": "178-188bpm",
|
||||
"zone5_bpm": "188-198bpm",
|
||||
"zone1_speed": "3.5mph",
|
||||
"zone2_speed": "3.5-4.0mph",
|
||||
"zone3_speed": "4.0-6.5mph",
|
||||
"zone4_speed": "6.5-7.0mph",
|
||||
"zone5_speed": "7.0-8.0mph",
|
||||
"zone1_incline": "2% Incline",
|
||||
"zone2_incline": "2% Incline",
|
||||
"zone3_incline": "2% Incline",
|
||||
"zone4_incline": "2% Incline",
|
||||
"zone5_incline": "2% Incline",
|
||||
"zone1_pace": "10:39min/km Pace",
|
||||
"zone2_pace": "10:39-9:19min/km Pace",
|
||||
"zone3_pace": "9:19-5:44min/km Pace",
|
||||
"zone4_pace": "5:44-5:20min/km Pace",
|
||||
"zone5_pace": "5:20-4:40min/km Pace",
|
||||
"zone1_calories": "4.4kcals/minute",
|
||||
"zone2_calories": "5.9kcals/minute",
|
||||
"zone3_calories": "9.4kcals/minute",
|
||||
"zone4_calories": "12.5kcals/minute",
|
||||
"zone5_calories": "12.8kcals/minute",
|
||||
"zone1_carb": "Avg: 0.4g/min Carb Utilization",
|
||||
"zone2_carb": "Avg: 0.6g/min Carb Utilization",
|
||||
"zone3_carb": "Avg: 1.9g/min Carb Utilization",
|
||||
"zone4_carb": "Avg: 2.9g/min Carb Utilization",
|
||||
"zone5_carb": "Avg: 3.1g/min Carb Utilization",
|
||||
"zone1_breaths": "Avg: 27 breaths",
|
||||
"zone2_breaths": "Avg: 28 breaths",
|
||||
"zone3_breaths": "Avg: 31 breaths",
|
||||
"zone4_breaths": "Avg: 42 breaths",
|
||||
"zone5_breaths": "Avg: 51 breaths",
|
||||
"zone1_breath_range": "Ideal Range: 15-20 breaths",
|
||||
"zone2_breath_range": "Ideal Range: 20-25 breaths",
|
||||
"zone3_breath_range": "Ideal Range: 25-30 breaths",
|
||||
"zone4_breath_range": "Ideal Range: 30-35 breaths",
|
||||
"zone5_breath_range": "Ideal Range: 40+ breaths",
|
||||
}
|
||||
|
||||
page_9_context = {
|
||||
"fuel_utilization_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/fuel_utilization_chart.png"
|
||||
),
|
||||
}
|
||||
|
||||
page_10_context = {
|
||||
"vo2_pulse_drop_bpm": "180 bpm",
|
||||
"vo2_pulse_drop_zone": "Zone 4",
|
||||
"vo2_pulse_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/vo2_pulse_chart.png"
|
||||
),
|
||||
"vo2_breath_drop_bpm": "173 bpm",
|
||||
"vo2_breath_drop_zone": "Zone 3",
|
||||
"vo2_breath_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/vo2_breath_chart.png"
|
||||
),
|
||||
}
|
||||
|
||||
page_11_context = {
|
||||
"fat_max_optimal": "*Optimal 10-12Kcals/minute",
|
||||
"fat_max_value": "3.8Kcals/min",
|
||||
"fat_max_heart_rate": "49% of Max Heart Rate",
|
||||
"fat_max_bpm": "97 bpm",
|
||||
"crossover_bpm": "100bpm",
|
||||
"crossover_heart_rate": "51% of Max Heart Rate",
|
||||
"fat_metabolism_note": "100bpm at a speed of 4.0mph and incline of 2%",
|
||||
"fat_metabolism_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/fat_metabolism_chart.png"
|
||||
),
|
||||
"cardiac_recovery_time": "(1 minute)",
|
||||
"cardiac_recovery_percentage": "33%",
|
||||
"metabolic_recovery_time": "(2 minute)",
|
||||
"metabolic_recovery_percentage": "65%",
|
||||
"breath_recovery_time": "(2.5 minute)",
|
||||
"breath_recovery_percentage": "76%",
|
||||
"recovery_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/recovery_chart.png"
|
||||
),
|
||||
"resting_heart_rate": "53bpm",
|
||||
"hr_age_range": "26-35",
|
||||
"hr_poor": "82bpm +",
|
||||
"hr_below_avg": "75-81bpm",
|
||||
"hr_average": "71-74bpm",
|
||||
"hr_above_avg": "66-70bpm",
|
||||
"hr_good": "62-65bpm",
|
||||
"hr_excellent": "55-61bpm",
|
||||
"hr_athlete": "44-54bpm",
|
||||
}
|
||||
|
||||
page_12_context = {
|
||||
|
||||
}
|
||||
|
||||
page_13_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"age": "34",
|
||||
"height": "5'4\"",
|
||||
"weight": "123lbs",
|
||||
"focus": "Endurance",
|
||||
"zone2_frequency": "3-4x/week",
|
||||
"zone2_duration": "40+ minutes",
|
||||
"zone2_hr_range": "96-110bpm",
|
||||
"zone2_speed": "3.5-4.0mph",
|
||||
"zone2_incline": "2% Incline",
|
||||
"zone3_frequency": "1-2x/week",
|
||||
"zone3_duration": "10-20 minutes",
|
||||
"zone3_hr_range": "100-178bpm",
|
||||
"zone3_speed": "4.0-6.5mph",
|
||||
"zone3_incline": "2% Incline",
|
||||
"zone3_target_hr": "140bpm",
|
||||
"zone3_recovery_speed": "3.5mph",
|
||||
"zone3_recovery_incline": "2% Incline",
|
||||
"zone1_hr_range": "81-96bpm",
|
||||
"zone1_duration": "4-8 minutes",
|
||||
"zone3_repeats": "2-3 times",
|
||||
"short_sets": "8-10",
|
||||
"short_duration": "10-30 seconds",
|
||||
"short_zone": "5",
|
||||
"short_rpe": "10",
|
||||
"short_recovery": "20-60 seconds",
|
||||
"medium_sets": "6-8",
|
||||
"medium_duration": "30-90 seconds",
|
||||
"medium_zone": "4",
|
||||
"medium_rpe": "8-9",
|
||||
"medium_recovery": "30-90 seconds",
|
||||
"long_sets": "4-6",
|
||||
"long_duration": "5-10 minutes",
|
||||
"long_zone": "3/4",
|
||||
"long_rpe": "7-8",
|
||||
"long_recovery": "2.5-5 minutes",
|
||||
"tempo_sets": "2-3",
|
||||
"tempo_duration": "10-20 minutes",
|
||||
"tempo_zone": "3",
|
||||
"tempo_rpe": "6-7",
|
||||
"tempo_recovery": "4-8 minutes",
|
||||
"cardio_sets": "1",
|
||||
"cardio_duration": ">40 minutes",
|
||||
"cardio_zone": "2",
|
||||
"cardio_rpe": "4-5",
|
||||
"cardio_recovery": "N/A",
|
||||
"week1_mon_zone": "Zone 2",
|
||||
"week1_mon_duration": "45 mins",
|
||||
"week1_tue_zone": "Zone 2",
|
||||
"week1_tue_duration": "45 mins",
|
||||
"week1_wed_zone": "Zone 3",
|
||||
"week1_wed_duration1": "10mins On",
|
||||
"week1_wed_duration2": "8mins Rest",
|
||||
"week1_wed_sets": "x2",
|
||||
"week1_thu_content": "",
|
||||
"week1_fri_zone": "Zone 2",
|
||||
"week1_fri_duration": "45 mins",
|
||||
"week1_sat_content": "",
|
||||
"week1_sun_content": "",
|
||||
"week2_mon_zone": "Zone 2",
|
||||
"week2_mon_duration": "50 mins",
|
||||
"week2_tue_zone": "Zone 2",
|
||||
"week2_tue_duration": "50 mins",
|
||||
"week2_wed_zone": "Zone 3",
|
||||
"week2_wed_duration1": "10mins On",
|
||||
"week2_wed_duration2": "6mins Rest",
|
||||
"week2_wed_sets": "x2",
|
||||
"week2_thu_content": "",
|
||||
"week2_fri_zone": "Zone 2",
|
||||
"week2_fri_duration": "50 mins",
|
||||
"week2_sat_content": "",
|
||||
"week2_sun_content": "",
|
||||
"contact_email": "info@ishplabs.com",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": "13",
|
||||
}
|
||||
|
||||
page_14_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"contact_email": "info@ishplabs.com",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": "14",
|
||||
}
|
||||
|
||||
page_15_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"contact_email": "info@ishplabs.com",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": "15",
|
||||
}
|
||||
|
||||
page_16_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"contact_email": "info@ishplabs.com",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": "16",
|
||||
}
|
||||
|
||||
page_17_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"contact_email": "info@ishplabs.com",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": "17",
|
||||
}
|
||||
|
||||
page_18_context = {
|
||||
"body_fat_percentage_chart": image_to_base64(
|
||||
"/home/oluwasanmi/Documents/Work/MKD/report_generation/graphs/fat_percent_master_chart.png"
|
||||
),
|
||||
}
|
||||
|
||||
page_19_context = {
|
||||
"patient_name": "Keirstyn Moran",
|
||||
"contact_email": "info@ishplabs.com",
|
||||
"website": "www.ishplabs.com",
|
||||
"social": "@ishplabs",
|
||||
"page_number": "19",
|
||||
}
|
||||
|
||||
context_list = [
|
||||
page_1_context,
|
||||
page_2_context,
|
||||
page_3_context,
|
||||
page_4_context,
|
||||
page_5_context,
|
||||
page_6_context,
|
||||
page_7_context,
|
||||
page_8_context,
|
||||
page_9_context,
|
||||
page_10_context,
|
||||
page_11_context,
|
||||
page_12_context,
|
||||
page_13_context,
|
||||
page_14_context,
|
||||
page_15_context,
|
||||
page_16_context,
|
||||
page_17_context,
|
||||
page_18_context,
|
||||
page_19_context,
|
||||
]
|
||||
@@ -1,319 +1,180 @@
|
||||
import base64
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import pandas as pd
|
||||
|
||||
|
||||
class ReportGenerator:
|
||||
class ContextGenerator:
|
||||
"""Generate context data for report pages"""
|
||||
|
||||
def __init__(self):
|
||||
self.pnoe_df = None
|
||||
self.patient_df = None
|
||||
self.spirometry_df = None
|
||||
self.seca_df = None
|
||||
self.patient_info = {}
|
||||
self.charts_dir = Path("graphs")
|
||||
self.charts_dir.mkdir(exist_ok=True)
|
||||
|
||||
def load_data(
|
||||
self,
|
||||
pnoe_path: str,
|
||||
patient_path: str,
|
||||
spirometry_path: str,
|
||||
seca_path: str = None,
|
||||
seca_path: str,
|
||||
):
|
||||
"""Load all required datasets"""
|
||||
self.pnoe_df = pd.read_csv(pnoe_path, delimiter=";")
|
||||
self.patient_df = pd.read_csv(patient_path)
|
||||
self.spirometry_df = pd.read_csv(spirometry_path)
|
||||
if seca_path:
|
||||
self.seca_df = pd.read_excel(seca_path)
|
||||
self.seca_df = pd.read_excel(seca_path)
|
||||
self._preprocess_pnoe_data()
|
||||
|
||||
# Apply preprocessing
|
||||
self._preprocess_data()
|
||||
|
||||
def _preprocess_data(self):
|
||||
"""Apply preprocessing steps from your notebook"""
|
||||
# Convert to numeric
|
||||
def _preprocess_pnoe_data(self):
|
||||
"""Apply preprocessing steps to Pnoe data"""
|
||||
self.pnoe_df = self.pnoe_df.apply(pd.to_numeric, errors="ignore")
|
||||
|
||||
# Calculate derived columns
|
||||
self.pnoe_df["VO2 Pulse"] = (
|
||||
self.pnoe_df["VO2(ml/min)"] / self.pnoe_df["HR(bpm)"]
|
||||
)
|
||||
self.pnoe_df["VO2 Breath"] = (
|
||||
self.pnoe_df["VO2(ml/min)"] / self.pnoe_df["BF(bpm)"]
|
||||
)
|
||||
self.pnoe_df["CHO"] = (
|
||||
self.pnoe_df["EE(kcal/min)"] * self.pnoe_df["CARBS(%)"] / 100
|
||||
)
|
||||
self.pnoe_df["FAT"] = (
|
||||
self.pnoe_df["EE(kcal/min)"] * self.pnoe_df["FAT(%)"] / 100
|
||||
)
|
||||
|
||||
# Apply smoothing
|
||||
self.pnoe_df["VO2 Pulse"] = self.pnoe_df["VO2(ml/min)"] / self.pnoe_df["HR(bpm)"]
|
||||
self.pnoe_df["VO2 Breath"] = self.pnoe_df["VO2(ml/min)"] / self.pnoe_df["BF(bpm)"]
|
||||
self.pnoe_df["CHO"] = self.pnoe_df["EE(kcal/min)"] * self.pnoe_df["CARBS(%)"] / 100
|
||||
self.pnoe_df["FAT"] = self.pnoe_df["EE(kcal/min)"] * self.pnoe_df["FAT(%)"] / 100
|
||||
|
||||
window_size = 10
|
||||
columns_to_smooth = [
|
||||
"VO2(ml/min)",
|
||||
"VCO2(ml/min)",
|
||||
"HR(bpm)",
|
||||
"VT(l)",
|
||||
"BF(bpm)",
|
||||
"VE(l/min)",
|
||||
"VO2 Pulse",
|
||||
"VO2 Breath",
|
||||
"CHO",
|
||||
"FAT",
|
||||
]
|
||||
|
||||
columns_to_smooth = ["VO2(ml/min)", "VCO2(ml/min)", "HR(bpm)", "VT(l)", "BF(bpm)", "VE(l/min)", "VO2 Pulse", "VO2 Breath", "CHO", "FAT"]
|
||||
|
||||
for col in columns_to_smooth:
|
||||
if col in self.pnoe_df.columns:
|
||||
self.pnoe_df[f"{col}_smoothed"] = (
|
||||
self.pnoe_df[col].rolling(window=window_size, min_periods=1).mean()
|
||||
)
|
||||
self.pnoe_df[f"{col}_smoothed"] = self.pnoe_df[col].rolling(window=window_size, min_periods=1).mean()
|
||||
|
||||
def extract_patient_info(self, last_name: str) -> Dict:
|
||||
"""Extract patient information from datasets"""
|
||||
def extract_patient_info(self, patient_name: str) -> Dict:
|
||||
"""Extract patient information from SECA dataset"""
|
||||
if self.seca_df is not None:
|
||||
patient_data = self.seca_df[
|
||||
self.seca_df["LastName"].str.contains(last_name, case=False, na=False)
|
||||
]
|
||||
patient_data = self.seca_df[self.seca_df["LastName"].str.contains(patient_name, case=False, na=False)]
|
||||
if not patient_data.empty:
|
||||
row = patient_data.iloc[0]
|
||||
weight_kg = float(row.get("Weight", 0))
|
||||
fat_pct = float(row.get("Adult_FMP", 0))
|
||||
|
||||
self.patient_info = {
|
||||
"name": f"{row.get('FirstName', '')} {last_name}",
|
||||
"name": f"{row.get('FirstName', '')} {row.get('LastName', '')}",
|
||||
"first_name": row.get("FirstName", ""),
|
||||
"last_name": row.get("LastName", ""),
|
||||
"age": int(row.get("Age", 0)),
|
||||
"height": f"{row.get('Height', '')}",
|
||||
"weight": float(row.get("Weight", 0)),
|
||||
"weight": weight_kg,
|
||||
"gender": row.get("Gender", "").lower(),
|
||||
"fat_percentage": float(row.get("Adult_FMP", 0)),
|
||||
"fat_percentage": fat_pct,
|
||||
"fat_mass_lbs": weight_kg * fat_pct / 100 * 2.20462,
|
||||
"lean_mass_lbs": weight_kg * (1 - fat_pct / 100) * 2.20462,
|
||||
}
|
||||
return self.patient_info
|
||||
|
||||
def calculate_spirometry_metrics(self) -> Dict:
|
||||
"""Calculate spirometry-related metrics"""
|
||||
metrics = {}
|
||||
|
||||
# Extract key spirometry values
|
||||
for param in ["FVC", "FEV1", "FEV1/FVC%"]:
|
||||
row = self.spirometry_df.loc[self.spirometry_df["Parameters"] == param]
|
||||
row = self.spirometry_df.loc[self.spirometry_df["Parameters"].str.strip() == param]
|
||||
if not row.empty:
|
||||
metrics[
|
||||
f"{param.lower().replace('/', '_').replace('%', '_pct')}_best"
|
||||
] = row["Best"].values[0]
|
||||
metrics[
|
||||
f"{param.lower().replace('/', '_').replace('%', '_pct')}_pred"
|
||||
] = row["%Pred."].values[0]
|
||||
|
||||
param_key = param.lower().replace('/', '_').replace('%', '_pct')
|
||||
metrics[f"{param_key}_best"] = row["Best"].values[0]
|
||||
metrics[f"{param_key}_pred"] = row["%Pred."].values[0]
|
||||
return metrics
|
||||
|
||||
def calculate_pnoe_metrics(self) -> Dict:
|
||||
"""Calculate all Pnoe-derived metrics"""
|
||||
metrics = {}
|
||||
|
||||
# Basic metrics
|
||||
metrics["vo2_max"] = self.pnoe_df["VO2(ml/min)_smoothed"].max()
|
||||
metrics["vo2_max_per_kg"] = metrics["vo2_max"] / self.patient_info["weight"]
|
||||
|
||||
# Peak VT
|
||||
|
||||
peak_vt_idx = self.pnoe_df["VT(l)_smoothed"].idxmax()
|
||||
peak_vt_row = self.pnoe_df.loc[peak_vt_idx]
|
||||
metrics["peak_vt"] = peak_vt_row["VT(l)_smoothed"]
|
||||
metrics["peak_vt_hr"] = peak_vt_row["HR(bpm)_smoothed"]
|
||||
|
||||
# Fat burning metrics
|
||||
|
||||
fat_max_idx = self.pnoe_df["FAT_smoothed"].idxmax()
|
||||
fat_max_row = self.pnoe_df.loc[fat_max_idx]
|
||||
metrics["fat_max_value"] = fat_max_row["FAT_smoothed"]
|
||||
metrics["fat_max_hr"] = fat_max_row["HR(bpm)_smoothed"]
|
||||
|
||||
# Calculate zones (simplified from your logic)
|
||||
metrics.update(self._calculate_hr_zones())
|
||||
|
||||
# VT1/VT2 detection
|
||||
|
||||
vt1, vt2 = self._detect_thresholds()
|
||||
metrics["vt1"] = vt1
|
||||
metrics["vt2"] = vt2
|
||||
|
||||
|
||||
zones = self._calculate_hr_zones(vt1, vt2, fat_max_row)
|
||||
metrics.update(zones)
|
||||
return metrics
|
||||
|
||||
def _detect_thresholds(self) -> Tuple[Optional[Dict], Optional[Dict]]:
|
||||
"""Detect VT1 and VT2 thresholds"""
|
||||
# VT1: First crossover where carbs > fat
|
||||
condition = self.pnoe_df["CHO_smoothed"] > self.pnoe_df["FAT_smoothed"]
|
||||
crossover_indices = condition[condition].index
|
||||
|
||||
|
||||
vt1 = None
|
||||
if len(crossover_indices) > 0:
|
||||
vt1_idx = crossover_indices[0]
|
||||
vt1_row = self.pnoe_df.loc[vt1_idx]
|
||||
vt1 = {
|
||||
"HeartRate": vt1_row["HR(bpm)_smoothed"],
|
||||
"Speed": vt1_row["Speed"],
|
||||
"Time": vt1_row["T(sec)"],
|
||||
}
|
||||
|
||||
# VT2: Ventilation inflection (simplified)
|
||||
vt1 = {"HeartRate": vt1_row["HR(bpm)_smoothed"], "Speed": vt1_row["Speed"], "Time": vt1_row["T(sec)"]}
|
||||
|
||||
ve_slope = self.pnoe_df["VE(l/min)_smoothed"].diff()
|
||||
second_derivative = ve_slope.diff()
|
||||
vt2_idx = second_derivative.idxmax()
|
||||
|
||||
|
||||
vt2 = None
|
||||
if pd.notna(vt2_idx):
|
||||
vt2_row = self.pnoe_df.loc[vt2_idx]
|
||||
vt2 = {
|
||||
"HeartRate": vt2_row["HR(bpm)_smoothed"],
|
||||
"Speed": vt2_row["Speed"],
|
||||
"Time": vt2_row["T(sec)"],
|
||||
}
|
||||
|
||||
vt2 = {"HeartRate": vt2_row["HR(bpm)_smoothed"], "Speed": vt2_row["Speed"], "Time": vt2_row["T(sec)"]}
|
||||
|
||||
return vt1, vt2
|
||||
|
||||
def _calculate_hr_zones(self) -> Dict:
|
||||
"""Calculate heart rate zones"""
|
||||
max_hr = 220 - self.patient_info["age"]
|
||||
|
||||
# Simplified zone calculation - you can make this more sophisticated
|
||||
zones = {
|
||||
"zone1_bpm": f"{int(max_hr * 0.55)}-{int(max_hr * 0.65)}bpm",
|
||||
"zone2_bpm": f"{int(max_hr * 0.65)}-{int(max_hr * 0.75)}bpm",
|
||||
"zone3_bpm": f"{int(max_hr * 0.75)}-{int(max_hr * 0.85)}bpm",
|
||||
"zone4_bpm": f"{int(max_hr * 0.85)}-{int(max_hr * 0.95)}bpm",
|
||||
"zone5_bpm": f"{int(max_hr * 0.95)}+bpm",
|
||||
}
|
||||
def _calculate_hr_zones(self, vt1: Optional[Dict], vt2: Optional[Dict], fat_max_row: pd.Series) -> Dict:
|
||||
"""Calculate heart rate zones based on thresholds"""
|
||||
zones = {}
|
||||
if vt1 and vt2:
|
||||
zone_1_start = fat_max_row["HR(bpm)_smoothed"] - 15
|
||||
zone_2_start = fat_max_row["HR(bpm)_smoothed"]
|
||||
zone_3_start = vt1["HeartRate"]
|
||||
zone_4_start = vt2["HeartRate"] - 10
|
||||
zone_5_start = vt2["HeartRate"] + 10
|
||||
|
||||
zones["zone1_bpm"] = f"{int(zone_1_start)}-{int(zone_2_start)}bpm"
|
||||
zones["zone2_bpm"] = f"{int(zone_2_start)}-{int(vt1['HeartRate'])}bpm"
|
||||
zones["zone3_bpm"] = f"{int(zone_3_start)}-{int(zone_4_start)}bpm"
|
||||
zones["zone4_bpm"] = f"{int(zone_4_start)}-{int(zone_5_start)}bpm"
|
||||
zones["zone5_bpm"] = f"{int(zone_5_start)}+bpm"
|
||||
else:
|
||||
max_hr = 220 - self.patient_info["age"]
|
||||
zones["zone1_bpm"] = f"{int(max_hr * 0.55)}-{int(max_hr * 0.65)}bpm"
|
||||
zones["zone2_bpm"] = f"{int(max_hr * 0.65)}-{int(max_hr * 0.75)}bpm"
|
||||
zones["zone3_bpm"] = f"{int(max_hr * 0.75)}-{int(max_hr * 0.85)}bpm"
|
||||
zones["zone4_bpm"] = f"{int(max_hr * 0.85)}-{int(max_hr * 0.95)}bpm"
|
||||
zones["zone5_bpm"] = f"{int(max_hr * 0.95)}+bpm"
|
||||
return zones
|
||||
|
||||
def generate_charts(self) -> Dict[str, str]:
|
||||
"""Generate all charts and return base64 encoded versions"""
|
||||
charts = {}
|
||||
|
||||
# Generate fuel utilization chart
|
||||
charts["fuel_utilization_chart"] = self._create_fuel_chart()
|
||||
|
||||
# Generate VO2 pulse chart
|
||||
charts["vo2_pulse_chart"] = self._create_vo2_pulse_chart()
|
||||
|
||||
# Generate body composition chart
|
||||
charts["body_composition_chart"] = self._create_body_comp_chart()
|
||||
|
||||
# Add more chart generation methods...
|
||||
|
||||
return charts
|
||||
|
||||
def _create_fuel_chart(self) -> str:
|
||||
"""Create and save fuel utilization chart"""
|
||||
# Use your existing chart code but make it dynamic
|
||||
speed_groups = self.pnoe_df.groupby("Speed").mean(numeric_only=True).round(1)
|
||||
speed_groups = speed_groups.iloc[1:-1]
|
||||
filtered_data = speed_groups[
|
||||
(speed_groups.index >= 3.5) & (speed_groups.index <= 7.5)
|
||||
]
|
||||
|
||||
plt.figure(figsize=(15, 8))
|
||||
# ... your chart code here ...
|
||||
|
||||
chart_path = self.charts_dir / "fuel_utilization_chart.png"
|
||||
plt.savefig(chart_path, dpi=300)
|
||||
plt.close()
|
||||
|
||||
return self._image_to_base64(chart_path)
|
||||
|
||||
def _create_vo2_pulse_chart(self) -> str:
|
||||
"""Create VO2 pulse chart"""
|
||||
# Your VO2 pulse chart code here
|
||||
chart_path = self.charts_dir / "vo2_pulse_chart.png"
|
||||
# ... chart generation code ...
|
||||
return self._image_to_base64(chart_path)
|
||||
|
||||
def _create_body_comp_chart(self) -> str:
|
||||
"""Create body composition chart"""
|
||||
# Your body composition chart code here
|
||||
chart_path = self.charts_dir / "body_composition_chart.png"
|
||||
# ... chart generation code ...
|
||||
return self._image_to_base64(chart_path)
|
||||
|
||||
def _image_to_base64(self, image_path: Path) -> str:
|
||||
"""Convert image to base64"""
|
||||
try:
|
||||
with open(image_path, "rb") as image_file:
|
||||
return base64.b64encode(image_file.read()).decode("utf-8")
|
||||
except FileNotFoundError:
|
||||
return ""
|
||||
|
||||
def generate_all_contexts(self, last_name: str = "Moran") -> List[Dict]:
|
||||
def generate_all_contexts(self, patient_name: str, graphs: Dict[str, str]) -> List[Dict]:
|
||||
"""Main method to generate all page contexts"""
|
||||
# Extract patient info
|
||||
self.extract_patient_info(last_name)
|
||||
|
||||
# Calculate metrics
|
||||
self.extract_patient_info(patient_name)
|
||||
spirometry_metrics = self.calculate_spirometry_metrics()
|
||||
pnoe_metrics = self.calculate_pnoe_metrics()
|
||||
|
||||
# Generate charts
|
||||
charts = self.generate_charts()
|
||||
|
||||
# Build contexts for each page
|
||||
|
||||
contexts = []
|
||||
|
||||
# Page 1
|
||||
contexts.append(
|
||||
{
|
||||
"name": self.patient_info["name"],
|
||||
"surname": last_name,
|
||||
"date": "July 29, 2025",
|
||||
}
|
||||
)
|
||||
|
||||
# Page 2-6 (add as needed)
|
||||
for i in range(5):
|
||||
contexts.append({})
|
||||
|
||||
# Page 7 - Spirometry
|
||||
contexts.append(
|
||||
{
|
||||
"peak_vt": pnoe_metrics["peak_vt"],
|
||||
"peak_vt_bpm": pnoe_metrics["peak_vt_hr"],
|
||||
"fev1_percentage": (
|
||||
pnoe_metrics["peak_vt"] / spirometry_metrics["fvc_best"]
|
||||
)
|
||||
* 100,
|
||||
"lung_analysis_chart": charts.get("spirometry_chart", ""),
|
||||
"respiratory_analysis_chart": charts.get("respiratory_chart", ""),
|
||||
}
|
||||
)
|
||||
|
||||
# Page 8 - VO2 Max and Zones
|
||||
contexts.append(
|
||||
{
|
||||
"vo2_max_value": f"{pnoe_metrics['vo2_max_per_kg']:.1f}",
|
||||
"age_range": f"{self.patient_info['age'] // 10 * 10}-{self.patient_info['age'] // 10 * 10 + 9}",
|
||||
**pnoe_metrics, # Include all zone calculations
|
||||
}
|
||||
)
|
||||
|
||||
# Continue for all pages...
|
||||
# Add remaining pages as needed
|
||||
|
||||
contexts.append({"name": self.patient_info["name"], "surname": self.patient_info["last_name"], "date": datetime.now().strftime("%B %d, %Y")})
|
||||
contexts.append({"patient_name": self.patient_info["name"], "test_date": datetime.now().strftime("%B %d, %Y")})
|
||||
|
||||
for i in range(4):
|
||||
contexts.append({"patient_name": self.patient_info["name"], "page_number": i + 3})
|
||||
|
||||
fev1_percentage = 0
|
||||
if spirometry_metrics.get("fvc_best"):
|
||||
fev1_percentage = (pnoe_metrics["peak_vt"] / spirometry_metrics["fvc_best"]) * 100
|
||||
|
||||
contexts.append({"peak_vt": f"{pnoe_metrics['peak_vt']:.2f}", "peak_vt_bpm": f"{int(pnoe_metrics['peak_vt_hr'])}", "fev1_percentage": f"{fev1_percentage:.1f}", "lung_analysis_chart": graphs.get("spirometry_chart", ""), "respiratory_analysis_chart": graphs.get("respiratory", "")})
|
||||
contexts.append({"vo2_max_value": f"{pnoe_metrics['vo2_max_per_kg']:.1f}", "age_range": f"{self.patient_info['age'] // 10 * 10}-{self.patient_info['age'] // 10 * 10 + 9}", "zone1_bpm": pnoe_metrics.get("zone1_bpm", ""), "zone2_bpm": pnoe_metrics.get("zone2_bpm", ""), "zone3_bpm": pnoe_metrics.get("zone3_bpm", ""), "zone4_bpm": pnoe_metrics.get("zone4_bpm", ""), "zone5_bpm": pnoe_metrics.get("zone5_bpm", ""), "vo2_pulse_chart": graphs.get("vo2_pulse", "")})
|
||||
contexts.append({"fat_max_value": f"{pnoe_metrics['fat_max_value']:.2f}", "fat_max_hr": f"{int(pnoe_metrics['fat_max_hr'])}", "fuel_utilization_chart": graphs.get("fuel_utilization", ""), "fat_metabolism_chart": graphs.get("fat_metabolism", "")})
|
||||
contexts.append({"fat_percentage": f"{self.patient_info['fat_percentage']:.1f}", "fat_mass_lbs": f"{self.patient_info['fat_mass_lbs']:.1f}", "lean_mass_lbs": f"{self.patient_info['lean_mass_lbs']:.1f}", "body_composition_chart": graphs.get("body_composition", ""), "body_fat_percent_chart": graphs.get("body_fat_percent", "")})
|
||||
|
||||
for i in range(9):
|
||||
contexts.append({"patient_name": self.patient_info["name"], "page_number": i + 11, "vo2_breath_chart": graphs.get("vo2_breath", ""), "recovery_chart": graphs.get("recovery", "")})
|
||||
|
||||
return contexts
|
||||
|
||||
|
||||
# Usage for backend service
|
||||
def generate_report(
|
||||
pnoe_file, patient_file, spirometry_file, seca_file=None, patient_name="Moran"
|
||||
):
|
||||
"""Main function for backend service"""
|
||||
generator = ReportGenerator()
|
||||
generator.load_data(pnoe_file, patient_file, spirometry_file, seca_file)
|
||||
return generator.generate_all_contexts(patient_name)
|
||||
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
contexts = generate_report(
|
||||
"data/Pnoe_20250729_1550-Moran_Keirstyn.csv",
|
||||
"data/patient_data.csv",
|
||||
"data/spirometry_data.csv",
|
||||
"data/SECA body comp for all patients.xlsx",
|
||||
)
|
||||
print(f"Generated {len(contexts)} page contexts")
|
||||
|
||||
+288
-295
@@ -1,6 +1,12 @@
|
||||
"""
|
||||
Graph Generator Service
|
||||
|
||||
This service generates all the charts and visualizations required for the medical report.
|
||||
Based on the analysis notebooks in services_dfdf/.
|
||||
"""
|
||||
|
||||
import base64
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import matplotlib.transforms as mtransforms
|
||||
@@ -11,13 +17,28 @@ from matplotlib.patches import FancyBboxPatch
|
||||
|
||||
|
||||
class GraphGenerator:
|
||||
"""Generate all charts for medical reports"""
|
||||
|
||||
def __init__(self, charts_dir: str = "graphs"):
|
||||
"""Initialize the GraphGenerator with output directory for charts"""
|
||||
"""
|
||||
Initialize the graph generator.
|
||||
|
||||
Args:
|
||||
charts_dir: Directory to save generated charts
|
||||
"""
|
||||
self.charts_dir = Path(charts_dir)
|
||||
self.charts_dir.mkdir(exist_ok=True)
|
||||
|
||||
def _image_to_base64(self, image_path: Path) -> str:
|
||||
"""Convert image to base64 string"""
|
||||
"""
|
||||
Convert image file to base64 string.
|
||||
|
||||
Args:
|
||||
image_path: Path to image file
|
||||
|
||||
Returns:
|
||||
Base64 encoded string
|
||||
"""
|
||||
try:
|
||||
with open(image_path, "rb") as image_file:
|
||||
return base64.b64encode(image_file.read()).decode("utf-8")
|
||||
@@ -25,27 +46,35 @@ class GraphGenerator:
|
||||
return ""
|
||||
|
||||
def generate_respiratory_chart(
|
||||
self, df: pd.DataFrame, save_as_base64: bool = False
|
||||
self, df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate respiratory chart showing VT and Speed over time"""
|
||||
# Get phase times for background regions
|
||||
"""
|
||||
Generate respiratory chart (VT and Speed over time).
|
||||
|
||||
Args:
|
||||
df: Processed DataFrame with smoothed columns
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
first_unique_phase = df.drop_duplicates(subset="PHASE")
|
||||
phase_times = first_unique_phase["T(sec)"].tolist()
|
||||
|
||||
plt.figure(figsize=(18, 5))
|
||||
ax1 = plt.subplot()
|
||||
|
||||
# Plot VT with step-like appearance
|
||||
# Plot VT
|
||||
sns.lineplot(data=df, x="T(sec)", y="VT(l)_smoothed", label="VT (L)")
|
||||
ax1.set_xlabel("Time (sec)")
|
||||
ax1.set_ylabel("VT (L)")
|
||||
ax1.grid(True, alpha=0.1)
|
||||
ax1.set_ylim(0, min(8, df["VT(l)_smoothed"].max()))
|
||||
|
||||
# Plot speed as step function on secondary y-axis
|
||||
# Plot speed on secondary y-axis
|
||||
ax2 = ax1.twinx()
|
||||
ax1.set_xticks(np.arange(0, df["T(sec)"].max() + 200, 200))
|
||||
line2 = sns.lineplot(
|
||||
sns.lineplot(
|
||||
data=df,
|
||||
x="T(sec)",
|
||||
y="Speed",
|
||||
@@ -58,11 +87,9 @@ class GraphGenerator:
|
||||
ax2.set_ylabel("Speed")
|
||||
ax2.set_ylim(0, min(30, df["Speed"].max()) + 1)
|
||||
|
||||
# Remove default legends first
|
||||
# Combine legends
|
||||
ax1.get_legend().remove()
|
||||
ax2.get_legend().remove()
|
||||
|
||||
# Combine legends from both axes in the top left
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
ax1.legend(lines1 + lines2, labels1 + labels2, loc="upper left")
|
||||
@@ -81,12 +108,23 @@ class GraphGenerator:
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_fuel_utilization_chart(
|
||||
self, df: pd.DataFrame, save_as_base64: bool = False
|
||||
self, df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate fuel utilization chart with stacked bars showing fat vs carbs"""
|
||||
# Group by speed and calculate mean for numeric columns only
|
||||
"""
|
||||
Generate fuel utilization chart (CHO vs FAT by stage).
|
||||
|
||||
Args:
|
||||
df: Processed DataFrame with smoothed columns
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
# Group by speed and calculate mean
|
||||
speed_groups = df.groupby("Speed").mean(numeric_only=True).round(1)
|
||||
speed_groups = speed_groups.iloc[1:-1]
|
||||
|
||||
# Filter data
|
||||
filtered_data = speed_groups[
|
||||
(speed_groups.index >= 3.5) & (speed_groups.index <= 7.5)
|
||||
]
|
||||
@@ -94,19 +132,24 @@ class GraphGenerator:
|
||||
plt.figure(figsize=(15, 8))
|
||||
plt.style.use("default")
|
||||
|
||||
# Create stage labels and positions
|
||||
stage_labels = [f"Stage {i}" for i in range(1, len(filtered_data) + 1)]
|
||||
x_positions = np.arange(len(filtered_data))
|
||||
|
||||
# Calculate fat and carbs energy expenditure from percentages
|
||||
# Calculate fat and carbs energy expenditure
|
||||
fat_ee = filtered_data["EE(kcal/min)"] * filtered_data["FAT(%)"] / 100
|
||||
carbs_ee = filtered_data["EE(kcal/min)"] * filtered_data["CARBS(%)"] / 100
|
||||
|
||||
# Create the main axis for the stacked bars
|
||||
ax1 = plt.gca()
|
||||
|
||||
# Create stacked bar chart with colors
|
||||
ax1.bar(x_positions, fat_ee, color="#1f77b4", alpha=0.8, width=0.6, label="Fat")
|
||||
# Create stacked bar chart
|
||||
ax1.bar(
|
||||
x_positions,
|
||||
fat_ee,
|
||||
color="#1f77b4",
|
||||
alpha=0.8,
|
||||
width=0.6,
|
||||
label="Fat",
|
||||
)
|
||||
ax1.bar(
|
||||
x_positions,
|
||||
carbs_ee,
|
||||
@@ -117,16 +160,15 @@ class GraphGenerator:
|
||||
label="Carbs",
|
||||
)
|
||||
|
||||
# Set labels and formatting for primary axis
|
||||
ax1.set_xlabel("", fontsize=12)
|
||||
ax1.set_ylabel("Fuel (kcal/min)", fontsize=12)
|
||||
ax1.set_ylim(0, 20)
|
||||
|
||||
# Add individual values on each bar segment
|
||||
# Add values on bars
|
||||
for i, (fat_val, carb_val, total_val) in enumerate(
|
||||
zip(fat_ee, carbs_ee, filtered_data["EE(kcal/min)"])
|
||||
):
|
||||
if fat_val > 0.3: # Fat value
|
||||
if fat_val > 0.3:
|
||||
ax1.text(
|
||||
i,
|
||||
fat_val / 2,
|
||||
@@ -137,7 +179,7 @@ class GraphGenerator:
|
||||
fontweight="bold",
|
||||
color="white",
|
||||
)
|
||||
if carb_val > 0.3: # Carbs value
|
||||
if carb_val > 0.3:
|
||||
ax1.text(
|
||||
i,
|
||||
fat_val + carb_val / 2,
|
||||
@@ -148,7 +190,6 @@ class GraphGenerator:
|
||||
fontweight="bold",
|
||||
color="white",
|
||||
)
|
||||
# Total EE
|
||||
ax1.text(
|
||||
i,
|
||||
total_val + 0.5,
|
||||
@@ -160,7 +201,7 @@ class GraphGenerator:
|
||||
color="black",
|
||||
)
|
||||
|
||||
# Add speed labels below x-axis
|
||||
# Add speed labels
|
||||
for i, speed in enumerate(filtered_data.index):
|
||||
ax1.text(i, -1.5, f"{speed:.1f} mph", ha="center", va="top", fontsize=9)
|
||||
ax1.text(
|
||||
@@ -175,8 +216,6 @@ class GraphGenerator:
|
||||
|
||||
# Create secondary y-axis for heart rate
|
||||
ax2 = ax1.twinx()
|
||||
|
||||
# Plot heart rate line
|
||||
ax2.plot(
|
||||
x_positions,
|
||||
filtered_data["HR(bpm)"],
|
||||
@@ -187,12 +226,11 @@ class GraphGenerator:
|
||||
label="Heart Rate",
|
||||
)
|
||||
|
||||
# Set heart rate axis formatting
|
||||
ax2.set_ylabel("Heart Rate (bpm)", fontsize=12, color="red")
|
||||
ax2.tick_params(axis="y", labelcolor="red")
|
||||
ax2.set_ylim(0, 220)
|
||||
|
||||
# Add HR values above the points
|
||||
# Add HR values
|
||||
for i, hr in enumerate(filtered_data["HR(bpm)"]):
|
||||
ax2.text(
|
||||
i,
|
||||
@@ -205,7 +243,6 @@ class GraphGenerator:
|
||||
color="red",
|
||||
)
|
||||
|
||||
# Set x-axis formatting
|
||||
ax1.set_xticks(x_positions)
|
||||
ax1.set_xticklabels(stage_labels, fontsize=11)
|
||||
|
||||
@@ -221,11 +258,9 @@ class GraphGenerator:
|
||||
shadow=True,
|
||||
)
|
||||
|
||||
# Add grid
|
||||
ax1.grid(True, alpha=0.3, linestyle="-", linewidth=0.5)
|
||||
ax1.set_axisbelow(True)
|
||||
|
||||
# Adjust layout
|
||||
plt.tight_layout()
|
||||
plt.subplots_adjust(bottom=0.1, top=0.9)
|
||||
|
||||
@@ -236,9 +271,18 @@ class GraphGenerator:
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_vo2_pulse_chart(
|
||||
self, df: pd.DataFrame, save_as_base64: bool = False
|
||||
self, df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate VO2 Pulse chart with heart rate and speed"""
|
||||
"""
|
||||
Generate VO2 Pulse chart with HR and Speed.
|
||||
|
||||
Args:
|
||||
df: Processed DataFrame with smoothed columns
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
first_unique_phase = df.drop_duplicates(subset="PHASE")
|
||||
phase_times = first_unique_phase["T(sec)"].tolist()
|
||||
|
||||
@@ -292,12 +336,14 @@ class GraphGenerator:
|
||||
|
||||
ax1.set_xticks(np.arange(0, df["T(sec)"].max() + 200, 200))
|
||||
|
||||
# Remove default legends first
|
||||
for ax in [ax1, ax2, ax3]:
|
||||
if ax.get_legend():
|
||||
ax.get_legend().remove()
|
||||
# Combine legends
|
||||
if ax1.get_legend():
|
||||
ax1.get_legend().remove()
|
||||
if ax2.get_legend():
|
||||
ax2.get_legend().remove()
|
||||
if ax3.get_legend():
|
||||
ax3.get_legend().remove()
|
||||
|
||||
# Combine legends from all axes
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
lines3, labels3 = ax3.get_legend_handles_labels()
|
||||
@@ -319,16 +365,24 @@ class GraphGenerator:
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_vo2_breath_chart(
|
||||
self, df: pd.DataFrame, save_as_base64: bool = False
|
||||
self, df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate VO2 per Breath chart"""
|
||||
"""
|
||||
Generate VO2 per Breath chart.
|
||||
|
||||
Args:
|
||||
df: Processed DataFrame with smoothed columns
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
first_unique_phase = df.drop_duplicates(subset="PHASE")
|
||||
phase_times = first_unique_phase["T(sec)"].tolist()
|
||||
|
||||
plt.figure(figsize=(18, 5))
|
||||
ax1 = plt.subplot()
|
||||
|
||||
# Plot VO2 per Breath
|
||||
sns.lineplot(
|
||||
data=df,
|
||||
x="T(sec)",
|
||||
@@ -340,7 +394,7 @@ class GraphGenerator:
|
||||
ax1.set_ylim(0, df["VO2 Breath_smoothed"].max() + 1)
|
||||
ax1.grid(True, alpha=0.1)
|
||||
|
||||
# Plot speed as step function on secondary y-axis
|
||||
# Plot speed on secondary y-axis
|
||||
ax2 = ax1.twinx()
|
||||
ax1.set_xticks(np.arange(0, df["T(sec)"].max() + 200, 200))
|
||||
sns.lineplot(
|
||||
@@ -356,11 +410,9 @@ class GraphGenerator:
|
||||
ax2.set_ylim(0, df["Speed"].max() + 1)
|
||||
ax2.set_ylabel("Speed")
|
||||
|
||||
# Remove default legends first
|
||||
# Combine legends
|
||||
ax1.get_legend().remove()
|
||||
ax2.get_legend().remove()
|
||||
|
||||
# Combine legends from both axes in the top left
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
ax1.legend(lines1 + lines2, labels1 + labels2, loc="upper left")
|
||||
@@ -379,9 +431,18 @@ class GraphGenerator:
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_fat_metabolism_chart(
|
||||
self, df: pd.DataFrame, save_as_base64: bool = False
|
||||
self, df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate CHO and FAT metabolism chart"""
|
||||
"""
|
||||
Generate fat metabolism chart (CHO vs FAT over time).
|
||||
|
||||
Args:
|
||||
df: Processed DataFrame with smoothed columns
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
first_unique_phase = df.drop_duplicates(subset="PHASE")
|
||||
phase_times = first_unique_phase["T(sec)"].tolist()
|
||||
|
||||
@@ -391,7 +452,7 @@ class GraphGenerator:
|
||||
# Plot CHO
|
||||
sns.lineplot(data=df, x="T(sec)", y="CHO_smoothed", label="CHO (kcal/min)")
|
||||
ax1.set_xlabel("Time (sec)")
|
||||
ax1.set_ylabel("CHO (kcal/min)")
|
||||
ax1.set_ylabel("CHO (g/min)")
|
||||
ax1.grid(True, alpha=0.1)
|
||||
|
||||
# Plot FAT on secondary y-axis
|
||||
@@ -408,11 +469,9 @@ class GraphGenerator:
|
||||
ax2.set_ylabel("FAT (kcal/min)")
|
||||
ax2.set_ylim(0, 15)
|
||||
|
||||
# Remove default legends first
|
||||
# Combine legends
|
||||
ax1.get_legend().remove()
|
||||
ax2.get_legend().remove()
|
||||
|
||||
# Combine legends from both axes in the top left
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
ax1.legend(lines1 + lines2, labels1 + labels2, loc="upper left")
|
||||
@@ -431,9 +490,18 @@ class GraphGenerator:
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_recovery_chart(
|
||||
self, df: pd.DataFrame, save_as_base64: bool = False
|
||||
self, df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate recovery chart with VCO2, HR, and BF"""
|
||||
"""
|
||||
Generate recovery chart (VCO2, HR, and BF).
|
||||
|
||||
Args:
|
||||
df: Processed DataFrame with smoothed columns
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
first_unique_phase = df.drop_duplicates(subset="PHASE")
|
||||
phase_times = first_unique_phase["T(sec)"].tolist()
|
||||
|
||||
@@ -449,7 +517,7 @@ class GraphGenerator:
|
||||
color="blue",
|
||||
)
|
||||
ax1.set_xlabel("Time (sec)")
|
||||
ax1.set_ylabel("VCO2 (ml/min)")
|
||||
ax1.set_ylabel("VO2 Pulse (mL/beat)")
|
||||
ax1.set_ylim(0, df["VCO2(ml/min)"].max())
|
||||
ax1.grid(True, alpha=0.1)
|
||||
|
||||
@@ -468,7 +536,7 @@ class GraphGenerator:
|
||||
ax2.set_ylim(df["HR(bpm)_smoothed"].min(), df["HR(bpm)_smoothed"].max() + 1)
|
||||
ax2.tick_params(axis="y", labelcolor="red")
|
||||
|
||||
# Create third y-axis for breathing frequency
|
||||
# Create third y-axis for BF
|
||||
ax3 = ax1.twinx()
|
||||
ax3.spines["right"].set_position(("outward", 60))
|
||||
sns.lineplot(
|
||||
@@ -485,12 +553,14 @@ class GraphGenerator:
|
||||
ax3.set_ylim(0, df["BF(bpm)_smoothed"].max() + 1)
|
||||
ax1.set_xticks(np.arange(0, df["T(sec)"].max() + 200, 200))
|
||||
|
||||
# Remove default legends first
|
||||
for ax in [ax1, ax2, ax3]:
|
||||
if ax.get_legend():
|
||||
ax.get_legend().remove()
|
||||
# Combine legends
|
||||
if ax1.get_legend():
|
||||
ax1.get_legend().remove()
|
||||
if ax2.get_legend():
|
||||
ax2.get_legend().remove()
|
||||
if ax3.get_legend():
|
||||
ax3.get_legend().remove()
|
||||
|
||||
# Combine legends from all axes in the top left
|
||||
lines1, labels1 = ax1.get_legend_handles_labels()
|
||||
lines2, labels2 = ax2.get_legend_handles_labels()
|
||||
lines3, labels3 = ax3.get_legend_handles_labels()
|
||||
@@ -511,129 +581,41 @@ class GraphGenerator:
|
||||
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_body_fat_percentage_chart(
|
||||
self,
|
||||
gender: str,
|
||||
age: int,
|
||||
body_fat_percentage: float,
|
||||
save_as_base64: bool = False,
|
||||
) -> str:
|
||||
"""Generate body fat percentage chart with ranges"""
|
||||
# Define the segments with muted colors
|
||||
segments = [
|
||||
("#F8A8A8", 0, 15), # Muted Red/Salmon: 0% to 15%
|
||||
("#FFEECC", 15, 5), # Pale Yellow/Cream: 15% to 20%
|
||||
("#D0F0C0", 20, 15), # Pale Green/Mint: 20% to 35%
|
||||
("#FFEECC", 35, 5), # Pale Yellow/Cream: 35% to 40%
|
||||
("#F8A8A8", 40, 10), # Muted Red/Salmon: 40% to 50%
|
||||
]
|
||||
|
||||
# Determine age group
|
||||
if 20 <= age <= 39:
|
||||
age_group = "20-39"
|
||||
elif 40 <= age <= 59:
|
||||
age_group = "40-59"
|
||||
elif 60 <= age <= 79:
|
||||
age_group = "60-79"
|
||||
else:
|
||||
age_group = "N/A"
|
||||
|
||||
demographic = f"{age_group}\n({gender[0].upper()})"
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 2))
|
||||
|
||||
# Create the Segmented Bar
|
||||
for color, start, length in segments:
|
||||
ax.barh(
|
||||
y=0,
|
||||
width=length,
|
||||
left=start,
|
||||
height=1,
|
||||
color=color,
|
||||
edgecolor="black",
|
||||
linewidth=0.5,
|
||||
)
|
||||
|
||||
# Add the Indicator (Triangle)
|
||||
ax.plot(
|
||||
body_fat_percentage,
|
||||
1.05,
|
||||
marker="v",
|
||||
color="black",
|
||||
markersize=10,
|
||||
clip_on=False,
|
||||
transform=ax.get_xaxis_transform(),
|
||||
)
|
||||
|
||||
# Set Axis Properties and Labels
|
||||
ax.set_xlim(0, 50)
|
||||
ax.set_xticks(range(0, 51, 5))
|
||||
ax.set_yticks([])
|
||||
ax.text(
|
||||
-0.05,
|
||||
0,
|
||||
demographic,
|
||||
transform=ax.get_yaxis_transform(),
|
||||
va="center",
|
||||
ha="right",
|
||||
fontsize=12,
|
||||
)
|
||||
|
||||
ax.set_xlim(0, 50)
|
||||
ticks = range(0, 51, 5)
|
||||
ax.set_xticks(ticks)
|
||||
labels = [f"{t}%" for t in ticks]
|
||||
ax.set_xticklabels(labels)
|
||||
|
||||
# Clean up spines and add small ticks
|
||||
ax.spines["right"].set_visible(False)
|
||||
ax.spines["top"].set_visible(False)
|
||||
ax.spines["left"].set_visible(False)
|
||||
ax.spines["bottom"].set_visible(True)
|
||||
|
||||
for x in range(0, 51, 5):
|
||||
ax.plot(
|
||||
[x, x],
|
||||
[-0.05, -0.01],
|
||||
color="black",
|
||||
transform=ax.get_xaxis_transform(),
|
||||
clip_on=False,
|
||||
)
|
||||
|
||||
plt.tight_layout()
|
||||
|
||||
chart_path = self.charts_dir / "body_fat_percentage_chart.png"
|
||||
plt.savefig(chart_path, bbox_inches="tight", dpi=300)
|
||||
plt.close()
|
||||
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_body_composition_chart(
|
||||
self, fat_mass_lbs: float, lean_mass_lbs: float, save_as_base64: bool = False
|
||||
self, fat_mass_lbs: float, lean_mass_lbs: float, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""Generate donut chart for body composition"""
|
||||
"""
|
||||
Generate body composition donut chart.
|
||||
|
||||
Args:
|
||||
fat_mass_lbs: Fat mass in pounds
|
||||
lean_mass_lbs: Lean mass in pounds
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
# Calculate percentages
|
||||
total_weight = fat_mass_lbs + lean_mass_lbs
|
||||
fat_percentage = (fat_mass_lbs / total_weight) * 100
|
||||
lean_percentage = (lean_mass_lbs / total_weight) * 100
|
||||
|
||||
# Data for the chart
|
||||
sizes = [fat_percentage, lean_percentage]
|
||||
colors = ["#fde3ac", "#ff9966"] # Light yellow/tan and orange
|
||||
colors = ["#fde3ac", "#ff9966"]
|
||||
|
||||
plt.figure(figsize=(8, 8))
|
||||
|
||||
# Create the donut chart without labels first
|
||||
wedges, texts, autotexts = plt.pie(
|
||||
# Create donut chart
|
||||
plt.pie(
|
||||
sizes,
|
||||
autopct="", # Remove auto percentages
|
||||
autopct="",
|
||||
startangle=90,
|
||||
wedgeprops=dict(width=0.5, edgecolor="w"),
|
||||
colors=colors,
|
||||
labels=["", ""],
|
||||
) # Remove default labels
|
||||
)
|
||||
|
||||
# Add custom text annotations positioned manually
|
||||
# Add custom text annotations
|
||||
plt.text(
|
||||
-1,
|
||||
1,
|
||||
@@ -656,8 +638,7 @@ class GraphGenerator:
|
||||
bbox=dict(boxstyle="round,pad=0.3", facecolor="white", alpha=0.8),
|
||||
)
|
||||
|
||||
# Set the title
|
||||
plt.axis("equal") # Equal aspect ratio ensures that pie is drawn as a circle
|
||||
plt.axis("equal")
|
||||
|
||||
chart_path = self.charts_dir / "body_composition_chart.png"
|
||||
plt.savefig(chart_path, bbox_inches="tight", dpi=600)
|
||||
@@ -665,16 +646,142 @@ class GraphGenerator:
|
||||
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_spirometry_chart(
|
||||
self, spirometry_df: pd.DataFrame, save_as_base64: bool = False
|
||||
def generate_body_fat_percent_chart(
|
||||
self,
|
||||
fat_percentage: float,
|
||||
age: int,
|
||||
gender: str,
|
||||
save_as_base64: bool = True,
|
||||
) -> str:
|
||||
"""Generate spirometry chart with Z-scores and ranges"""
|
||||
"""
|
||||
Generate body fat percentage chart.
|
||||
|
||||
Args:
|
||||
fat_percentage: Body fat percentage
|
||||
age: Patient age
|
||||
gender: Patient gender ('male' or 'female')
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
# Determine age group
|
||||
if 20 <= age <= 39:
|
||||
age_group = "20-39"
|
||||
elif 40 <= age <= 59:
|
||||
age_group = "40-59"
|
||||
elif 60 <= age <= 79:
|
||||
age_group = "60-79"
|
||||
else:
|
||||
age_group = "20-39" # Default
|
||||
|
||||
demographic = f"{age_group}\n({gender[0].upper()})"
|
||||
|
||||
# Define segments based on gender (female example)
|
||||
if gender.lower() == "female":
|
||||
segments = [
|
||||
("#F8A8A8", 0, 15), # Muted Red: 0% to 15%
|
||||
("#FFEECC", 15, 5), # Pale Yellow: 15% to 20%
|
||||
("#D0F0C0", 20, 15), # Pale Green: 20% to 35%
|
||||
("#FFEECC", 35, 5), # Pale Yellow: 35% to 40%
|
||||
("#F8A8A8", 40, 10), # Muted Red: 40% to 50%
|
||||
]
|
||||
else: # male
|
||||
segments = [
|
||||
("#F8A8A8", 0, 5), # Muted Red: 0% to 5%
|
||||
("#FFEECC", 5, 5), # Pale Yellow: 5% to 10%
|
||||
("#D0F0C0", 10, 10), # Pale Green: 10% to 20%
|
||||
("#FFEECC", 20, 5), # Pale Yellow: 20% to 25%
|
||||
("#F8A8A8", 25, 25), # Muted Red: 25% to 50%
|
||||
]
|
||||
|
||||
fig, ax = plt.subplots(figsize=(10, 2))
|
||||
|
||||
# Create the segmented bar
|
||||
for color, start, length in segments:
|
||||
ax.barh(
|
||||
y=0,
|
||||
width=length,
|
||||
left=start,
|
||||
height=1,
|
||||
color=color,
|
||||
edgecolor="black",
|
||||
linewidth=0.5,
|
||||
)
|
||||
|
||||
# Add the indicator (triangle)
|
||||
ax.plot(
|
||||
fat_percentage,
|
||||
1.05,
|
||||
marker="v",
|
||||
color="black",
|
||||
markersize=10,
|
||||
clip_on=False,
|
||||
transform=ax.get_xaxis_transform(),
|
||||
)
|
||||
|
||||
# Set axis properties
|
||||
ax.set_xlim(0, 50)
|
||||
ax.set_xticks(range(0, 51, 5))
|
||||
ax.set_yticks([])
|
||||
ax.text(
|
||||
-0.05,
|
||||
0,
|
||||
demographic,
|
||||
transform=ax.get_yaxis_transform(),
|
||||
va="center",
|
||||
ha="right",
|
||||
fontsize=12,
|
||||
)
|
||||
|
||||
ticks = range(0, 51, 5)
|
||||
ax.set_xticks(ticks)
|
||||
labels = [f"{t}%" for t in ticks]
|
||||
ax.set_xticklabels(labels)
|
||||
|
||||
# Clean up spines
|
||||
ax.spines["right"].set_visible(False)
|
||||
ax.spines["top"].set_visible(False)
|
||||
ax.spines["left"].set_visible(False)
|
||||
ax.spines["bottom"].set_visible(True)
|
||||
|
||||
# Add tick marks
|
||||
for x in range(0, 51, 5):
|
||||
ax.plot(
|
||||
[x, x],
|
||||
[-0.05, -0.01],
|
||||
color="black",
|
||||
transform=ax.get_xaxis_transform(),
|
||||
clip_on=False,
|
||||
)
|
||||
|
||||
plt.tight_layout()
|
||||
|
||||
chart_path = self.charts_dir / "body_fat_percent_chart.png"
|
||||
plt.savefig(chart_path, bbox_inches="tight", dpi=300)
|
||||
plt.close()
|
||||
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_spirometry_chart(
|
||||
self, spirometry_df: pd.DataFrame, save_as_base64: bool = True
|
||||
) -> str:
|
||||
"""
|
||||
Generate spirometry chart with Z-scores.
|
||||
|
||||
Args:
|
||||
spirometry_df: Spirometry DataFrame with parameters
|
||||
save_as_base64: If True, return base64 string, else return file path
|
||||
|
||||
Returns:
|
||||
Base64 string or file path
|
||||
"""
|
||||
# Coerce numeric columns
|
||||
for col in ["Best", "LLN", "Pred.", "%Pred.", "ZScore"]:
|
||||
if col in spirometry_df.columns:
|
||||
spirometry_df[col] = pd.to_numeric(spirometry_df[col], errors="coerce")
|
||||
|
||||
# Select rows of interest and prepare display values
|
||||
# Select rows of interest
|
||||
rows_map = {
|
||||
"Lung Volume": "FVC",
|
||||
"Lung Power": "FEV1",
|
||||
@@ -707,7 +814,7 @@ class GraphGenerator:
|
||||
)
|
||||
|
||||
x_min, x_max = -5, 3
|
||||
# Segment colors: red -> orange -> yellow -> green
|
||||
# Segment colors
|
||||
segments = [
|
||||
(-5, -4, "#f4a7a7"), # red-ish
|
||||
(-4, -3, "#f7c49a"), # orange-ish
|
||||
@@ -726,10 +833,10 @@ class GraphGenerator:
|
||||
0, width=b - a, left=a, height=0.6, color=color, edgecolor="none"
|
||||
)
|
||||
|
||||
# LLN (-1) and Predicted (0) markers
|
||||
# LLN and Predicted markers
|
||||
ax.axvline(0, color="black", lw=1)
|
||||
|
||||
# Z-score pointer (downward triangle) at top of each panel
|
||||
# Z-score pointer
|
||||
if pd.notna(rec["z"]):
|
||||
trans = mtransforms.blended_transform_factory(
|
||||
ax.transData, ax.transAxes
|
||||
@@ -744,7 +851,7 @@ class GraphGenerator:
|
||||
clip_on=False,
|
||||
)
|
||||
|
||||
# Labels, ticks, and styling
|
||||
# Labels and styling
|
||||
ax.set_title(
|
||||
rec["label"], loc="left", fontsize=11, fontweight="bold", pad=2
|
||||
)
|
||||
@@ -760,15 +867,11 @@ class GraphGenerator:
|
||||
|
||||
# Right-side summary boxes
|
||||
fig.subplots_adjust(right=0.78)
|
||||
box_ax = fig.add_axes(
|
||||
[0.805, 0.06, 0.18, 0.90]
|
||||
) # [left, bottom, width, height]
|
||||
box_ax = fig.add_axes([0.805, 0.06, 0.18, 0.90])
|
||||
box_ax.axis("off")
|
||||
|
||||
# Helper to draw a pill-shaped text box
|
||||
def pill(ax, xy, text):
|
||||
x, y = xy
|
||||
# Draw rounded rectangle background
|
||||
bbox = FancyBboxPatch(
|
||||
(x - 0.48, y - 0.09),
|
||||
0.96,
|
||||
@@ -801,7 +904,7 @@ class GraphGenerator:
|
||||
box_ax.set_xlim(0, 1)
|
||||
box_ax.set_ylim(0, 1)
|
||||
|
||||
# Prepare display strings and positions (top to bottom)
|
||||
# Prepare display strings
|
||||
right_items = []
|
||||
for rec in records:
|
||||
name = (
|
||||
@@ -814,7 +917,7 @@ class GraphGenerator:
|
||||
pct_fmt = f"{rec['pct']:.1f}%"
|
||||
right_items.append((name, value_fmt, pct_fmt))
|
||||
|
||||
# Sort to match image order on the right (FVC, FEV1, FEV1/FVC)
|
||||
# Sort to match order
|
||||
order = ["FVC", "FEV1", "FEV1/FVC"]
|
||||
right_items_sorted = [
|
||||
next(item for item in right_items if item[0] == k) for k in order
|
||||
@@ -830,113 +933,3 @@ class GraphGenerator:
|
||||
plt.close()
|
||||
|
||||
return self._image_to_base64(chart_path) if save_as_base64 else str(chart_path)
|
||||
|
||||
def generate_all_charts(
|
||||
self,
|
||||
pnoe_df: pd.DataFrame,
|
||||
spirometry_df: pd.DataFrame,
|
||||
patient_data: Dict,
|
||||
save_as_base64: bool = False,
|
||||
) -> Dict[str, str]:
|
||||
"""Generate all charts at once and return dictionary of paths/base64 strings"""
|
||||
charts = {}
|
||||
|
||||
# Generate physiological charts
|
||||
charts["respiratory"] = self.generate_respiratory_chart(pnoe_df, save_as_base64)
|
||||
charts["fuel_utilization_chart"] = self.generate_fuel_utilization_chart(
|
||||
pnoe_df, save_as_base64
|
||||
)
|
||||
charts["vo2_pulse_chart"] = self.generate_vo2_pulse_chart(
|
||||
pnoe_df, save_as_base64
|
||||
)
|
||||
charts["vo2_breath_chart"] = self.generate_vo2_breath_chart(
|
||||
pnoe_df, save_as_base64
|
||||
)
|
||||
charts["fat_metabolism_chart"] = self.generate_fat_metabolism_chart(
|
||||
pnoe_df, save_as_base64
|
||||
)
|
||||
charts["recovery_chart"] = self.generate_recovery_chart(pnoe_df, save_as_base64)
|
||||
|
||||
# Generate body composition charts
|
||||
if (
|
||||
"gender" in patient_data
|
||||
and "age" in patient_data
|
||||
and "fat_percentage" in patient_data
|
||||
):
|
||||
charts["body_fat_percentage_chart"] = (
|
||||
self.generate_body_fat_percentage_chart(
|
||||
patient_data["gender"],
|
||||
patient_data["age"],
|
||||
patient_data["fat_percentage"],
|
||||
save_as_base64,
|
||||
)
|
||||
)
|
||||
|
||||
if "fat_mass_lbs" in patient_data and "lean_mass_lbs" in patient_data:
|
||||
charts["body_composition_chart"] = self.generate_body_composition_chart(
|
||||
patient_data["fat_mass_lbs"],
|
||||
patient_data["lean_mass_lbs"],
|
||||
save_as_base64,
|
||||
)
|
||||
|
||||
# Generate spirometry chart
|
||||
charts["spirometry_chart"] = self.generate_spirometry_chart(
|
||||
spirometry_df, save_as_base64
|
||||
)
|
||||
|
||||
return charts
|
||||
|
||||
|
||||
# Example usage
|
||||
if __name__ == "__main__":
|
||||
# Initialize graph generator
|
||||
generator = GraphGenerator()
|
||||
|
||||
# Load sample data (you would pass your actual dataframes)
|
||||
pnoe_df = pd.read_csv("data/Pnoe_20250729_1550-Moran_Keirstyn.csv", delimiter=";")
|
||||
spirometry_df = pd.read_csv("data/spirometry_data.csv")
|
||||
|
||||
# Preprocess pnoe data (same as in your notebook)
|
||||
pnoe_df = pnoe_df.apply(pd.to_numeric, errors="ignore")
|
||||
pnoe_df["VO2 Pulse"] = pnoe_df["VO2(ml/min)"] / pnoe_df["HR(bpm)"]
|
||||
pnoe_df["VO2 Breath"] = pnoe_df["VO2(ml/min)"] / pnoe_df["BF(bpm)"]
|
||||
pnoe_df["CHO"] = pnoe_df["EE(kcal/min)"] * pnoe_df["CARBS(%)"] / 100
|
||||
pnoe_df["FAT"] = pnoe_df["EE(kcal/min)"] * pnoe_df["FAT(%)"] / 100
|
||||
|
||||
# Apply smoothing
|
||||
window_size = 10
|
||||
columns_to_smooth = [
|
||||
"VO2(ml/min)",
|
||||
"VCO2(ml/min)",
|
||||
"HR(bpm)",
|
||||
"VT(l)",
|
||||
"BF(bpm)",
|
||||
"VE(l/min)",
|
||||
"VO2 Pulse",
|
||||
"VO2 Breath",
|
||||
"CHO",
|
||||
"FAT",
|
||||
]
|
||||
for col in columns_to_smooth:
|
||||
if col in pnoe_df.columns:
|
||||
pnoe_df[f"{col}_smoothed"] = (
|
||||
pnoe_df[col].rolling(window=window_size, min_periods=1).mean()
|
||||
)
|
||||
|
||||
# Patient data
|
||||
patient_data = {
|
||||
"gender": "female",
|
||||
"age": 25,
|
||||
"fat_percentage": 22.4,
|
||||
"fat_mass_lbs": 27.6,
|
||||
"lean_mass_lbs": 95.4,
|
||||
}
|
||||
|
||||
# Generate all charts
|
||||
charts = generator.generate_all_charts(
|
||||
pnoe_df, spirometry_df, patient_data, save_as_base64=True
|
||||
)
|
||||
|
||||
print(f"Generated {len(charts)} charts:")
|
||||
for chart_name in charts.keys():
|
||||
print(f"- {chart_name}")
|
||||
|
||||
@@ -1,191 +0,0 @@
|
||||
import datetime
|
||||
import pandas as pd
|
||||
import matplotlib.pyplot as plt
|
||||
class PageGenerator:
|
||||
def __init__(self, pnoe_df: pd.DataFrame, seca_df: pd.DataFrame, spirometry_df: pd.DataFrame, patient_info: dict):
|
||||
self.pnoe_df = pnoe_df
|
||||
self.seca_df = seca_df
|
||||
self.spirometry_df = spirometry_df
|
||||
self.patient_info = patient_info
|
||||
|
||||
|
||||
def page_1_context(self):
|
||||
# Extract patient information
|
||||
patient_name = self.patient_info.get("patient_name", "N/A")
|
||||
context = {
|
||||
"first_name": patient_name.split()[0] if patient_name else "N/A",
|
||||
"surname": patient_name.split()[-1] if patient_name else "N/A",
|
||||
"date": datetime.datetime.now().strftime("%B %d, %Y"),
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_3_context(self):
|
||||
# Example: Extract detailed PNOE data for page 3
|
||||
pnoe_details = self.pnoe_df.head(10).to_dict(orient="records")
|
||||
|
||||
context = {
|
||||
"pnoe_details": pnoe_details,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_4_context(self):
|
||||
# Example: Extract detailed Spirometry data for page 4
|
||||
# Filter df_2 for Keirstyn Moran
|
||||
patient_data = self.patient_info[self.patient_info['LastName'].str.contains('Moran', case=False, na=False)]
|
||||
fat_percentage = patient_data['Adult_FMP'].iloc[0]
|
||||
weight_kg = patient_data['Weight'].iloc[0]
|
||||
lean_percentage = 100 - fat_percentage
|
||||
|
||||
fat_mass_lbs = weight_kg * (fat_percentage / 100) * 2.20462
|
||||
lean_mass_lbs = weight_kg * (lean_percentage / 100) * 2.20462
|
||||
|
||||
total_weight = fat_mass_lbs + lean_mass_lbs
|
||||
fat_percentage = (fat_mass_lbs / total_weight) * 100
|
||||
lean_percentage = (lean_mass_lbs / total_weight) * 100
|
||||
|
||||
sizes = [fat_percentage, lean_percentage]
|
||||
colors = ['#fde3ac', '#ff9966'] # Light yellow/tan and orange from the image
|
||||
|
||||
plt.figure(figsize=(8, 8))
|
||||
|
||||
wedges, texts, autotexts = plt.pie(sizes, autopct='', startangle=90, wedgeprops=dict(width=0.5, edgecolor='w'), colors=colors, labels=['', '']) # Remove default labels
|
||||
|
||||
plt.text(-1, 1, f'Fat Mass ({fat_mass_lbs:.1f}lbs)\n{fat_percentage:.1f}%',
|
||||
fontsize=14, fontweight='bold', ha='center', va='center',
|
||||
bbox=dict(boxstyle="round,pad=0.3", facecolor='white', alpha=0.8))
|
||||
|
||||
plt.text(1, -1, f'Lean Mass ({lean_mass_lbs:.1f}lbs)\n{lean_percentage:.1f}%',
|
||||
fontsize=14, fontweight='bold', ha='center', va='center',
|
||||
bbox=dict(boxstyle="round,pad=0.3", facecolor='white', alpha=0.8))
|
||||
|
||||
plt.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle
|
||||
spirometry_details = self.spirometry_df.head(10).to_dict(orient="records")
|
||||
|
||||
context = {
|
||||
"spirometry_details": spirometry_details,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_5_context(self):
|
||||
# Example: Summary and conclusions for page 5
|
||||
summary = {
|
||||
"pnoe_summary": self.pnoe_df.describe().to_dict(),
|
||||
"seca_summary": self.seca_df.describe().to_dict(),
|
||||
"spirometry_summary": self.spirometry_df.describe().to_dict(),
|
||||
}
|
||||
|
||||
context = {
|
||||
"summary": summary,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_6_context(self):
|
||||
# Example: Recommendations based on data for page 6
|
||||
recommendations = [
|
||||
"Increase cardiovascular training.",
|
||||
"Incorporate strength training twice a week.",
|
||||
"Monitor respiratory function regularly.",
|
||||
]
|
||||
|
||||
context = {
|
||||
"recommendations": recommendations,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_7_context(self):
|
||||
# Example: Additional notes or references for page 7
|
||||
notes = "This report is generated based on the provided data. Consult a healthcare professional for personalized advice."
|
||||
|
||||
context = {
|
||||
"notes": notes,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_8_context(self):
|
||||
# Example: Contact information and disclaimers for page 8
|
||||
contact_info = {
|
||||
"clinic_name": "Health Clinic",
|
||||
"address": "123 Wellness St, Healthy City, HC 12345",
|
||||
"phone": "(123) 456-7890",
|
||||
"email": "contact@healthclinic.com"
|
||||
}
|
||||
|
||||
context = {
|
||||
"contact_info": contact_info,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_9_context(self):
|
||||
# Example: Graphs and visualizations for page 9
|
||||
graphs = [
|
||||
"graph1.png",
|
||||
"graph2.png",
|
||||
"graph3.png"
|
||||
]
|
||||
|
||||
context = {
|
||||
"graphs": graphs,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_10_context(self):
|
||||
# Example: Final remarks and next steps for page 10
|
||||
final_remarks = "Thank you for using our health assessment services. We look forward to assisting you in your health journey."
|
||||
|
||||
context = {
|
||||
"final_remarks": final_remarks,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_11_context(self):
|
||||
# Example: Additional resources and references for page 11
|
||||
resources = [
|
||||
{"title": "Healthy Living Guide", "link": "http://example.com/healthy-living"},
|
||||
{"title": "Exercise Tips", "link": "http://example.com/exercise-tips"},
|
||||
{"title": "Nutrition Advice", "link": "http://example.com/nutrition-advice"}
|
||||
]
|
||||
|
||||
context = {
|
||||
"resources": resources,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_12_context(self):
|
||||
# Example: Feedback and survey information for page 12
|
||||
feedback_info = "We value your feedback. Please visit http://example.com/feedback to share your experience."
|
||||
|
||||
context = {
|
||||
"feedback_info": feedback_info,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_13_context(self):
|
||||
# Example: Acknowledgments and credits for page 13
|
||||
acknowledgments = "This report was generated using data analysis techniques and the expertise of our medical team."
|
||||
|
||||
context = {
|
||||
"acknowledgments": acknowledgments,
|
||||
}
|
||||
|
||||
return context
|
||||
|
||||
def page_18_context(self):
|
||||
# Example: Legal disclaimers and terms for page 18
|
||||
legal_disclaimer = "This report is for informational purposes only and does not constitute medical advice."
|
||||
|
||||
context = {
|
||||
"legal_disclaimer": legal_disclaimer,
|
||||
}
|
||||
|
||||
return context
|
||||
@@ -12,8 +12,9 @@ import pandas as pd
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from playwright.sync_api import sync_playwright
|
||||
|
||||
from app.services.context import context_list
|
||||
from app.services.context_generator import ContextGenerator
|
||||
from app.services.graph_generator import GraphGenerator
|
||||
from app.services.spirometry_table_extractor import extract_spirometry_table_from_pdf
|
||||
|
||||
|
||||
class ReportGeneratorService:
|
||||
@@ -24,6 +25,7 @@ class ReportGeneratorService:
|
||||
template_dir: str = "app/report_gen",
|
||||
graphs_dir: str = "graphs",
|
||||
reports_dir: str = "reports",
|
||||
data_dir: str = "data",
|
||||
):
|
||||
"""
|
||||
Initialize the report generator service.
|
||||
@@ -32,16 +34,20 @@ class ReportGeneratorService:
|
||||
template_dir: Directory containing Jinja2 templates
|
||||
graphs_dir: Directory to save generated graphs
|
||||
reports_dir: Directory to save generated reports
|
||||
data_dir: Directory to store extracted/processed data
|
||||
"""
|
||||
self.template_dir = template_dir
|
||||
self.graphs_dir = Path(graphs_dir)
|
||||
self.reports_dir = Path(reports_dir)
|
||||
self.graph_generator = GraphGenerator(charts_dir=str(graphs_dir))
|
||||
self.data_dir = Path(data_dir)
|
||||
self.graph_generator = GraphGenerator(charts_dir=str(self.graphs_dir))
|
||||
self.context_generator = ContextGenerator()
|
||||
self.env = Environment(loader=FileSystemLoader(template_dir))
|
||||
|
||||
# Ensure directories exist
|
||||
self.graphs_dir.mkdir(exist_ok=True)
|
||||
self.reports_dir.mkdir(exist_ok=True)
|
||||
self.data_dir.mkdir(exist_ok=True)
|
||||
|
||||
def process_pnoe_data(self, pnoe_csv_path: str) -> pd.DataFrame:
|
||||
"""
|
||||
@@ -139,13 +145,16 @@ class ReportGeneratorService:
|
||||
else 0,
|
||||
}
|
||||
|
||||
def generate_html(self, patient_info: Dict[str, Any]) -> str:
|
||||
def generate_html(
|
||||
self, patient_info: Dict[str, Any], context_list: List[Dict[str, Any]]
|
||||
) -> str:
|
||||
"""
|
||||
Generate HTML content for the report.
|
||||
|
||||
Args:
|
||||
patient_info: Dictionary containing patient information
|
||||
(patient_name, age, height, weight, focus)
|
||||
context_list: List of context dictionaries for each page
|
||||
|
||||
Returns:
|
||||
Complete HTML document as string
|
||||
@@ -277,35 +286,112 @@ class ReportGeneratorService:
|
||||
"""
|
||||
Generate complete medical report from uploaded files.
|
||||
|
||||
This follows the complete workflow:
|
||||
1. Extract spirometry data from PDF
|
||||
2. Store all data in data directory
|
||||
3. Generate all graphs
|
||||
4. Generate context for each page
|
||||
5. Generate final HTML and PDF report
|
||||
|
||||
Args:
|
||||
spirometry_pdf_path: Path to Spirometry PDF file
|
||||
pnoe_csv_path: Path to Pnoe CSV file
|
||||
seca_excel_path: Path to SECA Excel file
|
||||
patient_info: Dictionary containing patient information
|
||||
output_filename: Optional custom output filename
|
||||
n
|
||||
|
||||
Returns:
|
||||
Dictionary containing report path, graphs generated, and analysis data
|
||||
"""
|
||||
# Process data
|
||||
# Step 1: Extract spirometry table from PDF
|
||||
spirometry_csv_path = self.data_dir / "extracted_spirometry_table.csv"
|
||||
extract_spirometry_table_from_pdf(spirometry_pdf_path)
|
||||
|
||||
# The extraction saves to current directory, move it to data_dir
|
||||
import shutil
|
||||
|
||||
if Path("extracted_spirometry_table.csv").exists():
|
||||
shutil.move("extracted_spirometry_table.csv", spirometry_csv_path)
|
||||
|
||||
# Step 2: Process Pnoe data
|
||||
df = self.process_pnoe_data(pnoe_csv_path)
|
||||
|
||||
# Generate graphs
|
||||
# Step 3: Generate all graphs
|
||||
graphs_generated = self.generate_graphs(df)
|
||||
|
||||
# Calculate analysis metrics
|
||||
# Create graph dictionary with base64 encoded images
|
||||
graphs_dict = {}
|
||||
for graph in graphs_generated:
|
||||
# Read the graph file and convert to base64
|
||||
graph_path = Path(graph["path"])
|
||||
if graph_path.exists():
|
||||
import base64
|
||||
|
||||
with open(graph_path, "rb") as f:
|
||||
graphs_dict[graph["name"]] = base64.b64encode(f.read()).decode(
|
||||
"utf-8"
|
||||
)
|
||||
|
||||
# Also generate body composition charts
|
||||
# Extract patient data for these charts
|
||||
patient_name = patient_info.get("patient_name", "").split()[-1] # Get last name
|
||||
|
||||
# Load SECA data to get body composition info
|
||||
seca_df = pd.read_excel(seca_excel_path)
|
||||
patient_data = seca_df[
|
||||
seca_df["LastName"].str.contains(patient_name, case=False, na=False)
|
||||
]
|
||||
|
||||
if not patient_data.empty:
|
||||
row = patient_data.iloc[0]
|
||||
weight_kg = float(row.get("Weight", 0))
|
||||
fat_pct = float(row.get("Adult_FMP", 0))
|
||||
age = int(row.get("Age", patient_info.get("age", 25)))
|
||||
gender = row.get("Gender", "female").lower()
|
||||
|
||||
fat_mass_lbs = weight_kg * fat_pct / 100 * 2.20462
|
||||
lean_mass_lbs = weight_kg * (1 - fat_pct / 100) * 2.20462
|
||||
|
||||
# Generate body composition chart
|
||||
body_comp_b64 = self.graph_generator.generate_body_composition_chart(
|
||||
fat_mass_lbs, lean_mass_lbs, save_as_base64=True
|
||||
)
|
||||
graphs_dict["body_composition"] = body_comp_b64
|
||||
|
||||
# Generate body fat percent chart
|
||||
body_fat_b64 = self.graph_generator.generate_body_fat_percent_chart(
|
||||
fat_pct, age, gender, save_as_base64=True
|
||||
)
|
||||
graphs_dict["body_fat_percent"] = body_fat_b64
|
||||
|
||||
# Generate spirometry chart
|
||||
spirometry_df = pd.read_csv(spirometry_csv_path)
|
||||
spirometry_chart_b64 = self.graph_generator.generate_spirometry_chart(
|
||||
spirometry_df, save_as_base64=True
|
||||
)
|
||||
graphs_dict["spirometry_chart"] = spirometry_chart_b64
|
||||
|
||||
# Step 4: Generate context for all pages
|
||||
self.context_generator.load_data(
|
||||
pnoe_csv_path, str(spirometry_csv_path), seca_excel_path
|
||||
)
|
||||
context_list = self.context_generator.generate_all_contexts(
|
||||
patient_name, graphs_dict
|
||||
)
|
||||
|
||||
# Step 5: Calculate analysis metrics
|
||||
analysis_data = self.calculate_analysis_metrics(df)
|
||||
analysis_data["graphs_count"] = len(graphs_generated)
|
||||
|
||||
# Generate HTML
|
||||
html_content = self.generate_html(patient_info)
|
||||
# Step 6: Generate HTML
|
||||
html_content = self.generate_html(patient_info, context_list)
|
||||
|
||||
# Generate PDF
|
||||
# Step 7: Generate PDF
|
||||
if output_filename is None:
|
||||
patient_name = patient_info.get("patient_name", "Unknown")
|
||||
patient_name_full = patient_info.get("patient_name", "Unknown")
|
||||
session_id = patient_info.get("session_id", "default")
|
||||
output_filename = (
|
||||
f"report_{patient_name.replace(' ', '_')}_{session_id}.pdf"
|
||||
f"report_{patient_name_full.replace(' ', '_')}_{session_id}.pdf"
|
||||
)
|
||||
|
||||
report_path = self.reports_dir / output_filename
|
||||
|
||||
Reference in New Issue
Block a user