Spaces:
Configuration error
Configuration error
PGx Analyzer Team commited on
Commit ·
7c802d4
1
Parent(s): a2b1915
Fix TypeError in ReportGenerator by adding __init__ and instance helper methods for report exports
Browse files- reports/report_generator.py +16 -0
- tests/test_report_generator.py +31 -0
reports/report_generator.py
CHANGED
|
@@ -23,6 +23,22 @@ logger = logging.getLogger(__name__)
|
|
| 23 |
|
| 24 |
|
| 25 |
class ReportGenerator:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@staticmethod
|
| 27 |
def _dict_to_summary_table(stats_dict: Dict[str, Any]) -> pd.DataFrame:
|
| 28 |
"""Converts statistical dictionary into a flat Pandas DataFrame for Excel export."""
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
class ReportGenerator:
|
| 26 |
+
def __init__(self, full_results: Dict[str, Any] = None, qc_report: Dict[str, Any] = None):
|
| 27 |
+
self.full_results = full_results or {}
|
| 28 |
+
self.qc_report = qc_report or {}
|
| 29 |
+
|
| 30 |
+
def generate_excel(self) -> bytes:
|
| 31 |
+
return self.export_to_excel(self.full_results)
|
| 32 |
+
|
| 33 |
+
def generate_pdf(self) -> bytes:
|
| 34 |
+
return self.export_to_pdf(self.full_results)
|
| 35 |
+
|
| 36 |
+
def generate_csv(self) -> bytes:
|
| 37 |
+
if 'processed_dataframe' in self.full_results and isinstance(self.full_results['processed_dataframe'], pd.DataFrame) and not self.full_results['processed_dataframe'].empty:
|
| 38 |
+
return self.full_results['processed_dataframe'].to_csv(index=False).encode('utf-8')
|
| 39 |
+
ov_df = self._dict_to_summary_table(self.full_results.get('overall', {}))
|
| 40 |
+
return ov_df.to_csv(index=False).encode('utf-8')
|
| 41 |
+
|
| 42 |
@staticmethod
|
| 43 |
def _dict_to_summary_table(stats_dict: Dict[str, Any]) -> pd.DataFrame:
|
| 44 |
"""Converts statistical dictionary into a flat Pandas DataFrame for Excel export."""
|
tests/test_report_generator.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from core.qc import DataQCEngine
|
| 4 |
+
from core.stratification import DemographicStratifier
|
| 5 |
+
from reports.report_generator import ReportGenerator
|
| 6 |
+
|
| 7 |
+
def test_report_generator():
|
| 8 |
+
df = pd.DataFrame({
|
| 9 |
+
'Sample ID': ['IND-001', 'IND-002', 'IND-003'],
|
| 10 |
+
'Gender': ['Male', 'Female', 'Male'],
|
| 11 |
+
'State': ['Tamil Nadu', 'Delhi', 'Maharashtra'],
|
| 12 |
+
'CYP2C19*2': ['GG', 'GA', 'AA'],
|
| 13 |
+
'CYP2C19*3': ['GG', 'GG', 'GG'],
|
| 14 |
+
'CYP2C19*17': ['CC', 'CT', 'TT']
|
| 15 |
+
})
|
| 16 |
+
qc_engine = DataQCEngine(df)
|
| 17 |
+
clean_df, qc_report = qc_engine.run_qc()
|
| 18 |
+
full_results = DemographicStratifier.stratify_and_analyze(clean_df)
|
| 19 |
+
|
| 20 |
+
# 1. Test Class Methods
|
| 21 |
+
excel_bytes = ReportGenerator.export_to_excel(full_results)
|
| 22 |
+
assert len(excel_bytes) > 0
|
| 23 |
+
|
| 24 |
+
pdf_bytes = ReportGenerator.export_to_pdf(full_results)
|
| 25 |
+
assert len(pdf_bytes) > 0
|
| 26 |
+
|
| 27 |
+
# 2. Test Instance Methods
|
| 28 |
+
rep_gen = ReportGenerator(full_results, qc_report)
|
| 29 |
+
assert len(rep_gen.generate_excel()) > 0
|
| 30 |
+
assert len(rep_gen.generate_pdf()) > 0
|
| 31 |
+
assert len(rep_gen.generate_csv()) > 0
|