Update app.py
Browse files
app.py
CHANGED
@@ -241,7 +241,7 @@ class ClinicalKPIMonitoring():
|
|
241 |
class DiagnosisSupport(ABC):
|
242 |
"""Abstract class for implementing clinical diagnoses."""
|
243 |
@abstractmethod
|
244 |
-
def diagnose(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame:
|
245 |
pass
|
246 |
|
247 |
class SimpleDiagnosis(DiagnosisSupport):
|
@@ -249,35 +249,35 @@ class SimpleDiagnosis(DiagnosisSupport):
|
|
249 |
def __init__(self):
|
250 |
self.model : LogisticRegressionTrainer = LogisticRegressionTrainer()
|
251 |
|
252 |
-
def diagnose(self, data: pd.DataFrame, target_col: str, columns: List[str], **kwargs) -> pd.DataFrame:
|
253 |
try:
|
254 |
result = self.model.invoke(data, target_col=target_col, columns = columns)
|
255 |
if "accuracy" in result:
|
256 |
-
return pd.DataFrame({
|
257 |
"model": result["model_type"]})
|
258 |
else:
|
259 |
-
return pd.DataFrame({
|
260 |
|
261 |
except Exception as e:
|
262 |
-
return pd.DataFrame({
|
263 |
|
264 |
|
265 |
class TreatmentRecommendation(ABC):
|
266 |
"""Abstract class for treatment recommendations"""
|
267 |
@abstractmethod
|
268 |
-
def recommend(self, data: pd.DataFrame, **kwargs) -> pd.DataFrame:
|
269 |
pass
|
270 |
|
271 |
class BasicTreatmentRecommendation(TreatmentRecommendation):
|
272 |
"""A placeholder class for basic treatment recommendations"""
|
273 |
-
def recommend(self, data: pd.DataFrame, condition_col: str, treatment_col:str, **kwargs) -> pd.DataFrame:
|
274 |
if condition_col not in data.columns or treatment_col not in data.columns:
|
275 |
-
return pd.DataFrame({
|
276 |
treatment = data[data[condition_col] == "High"][treatment_col].to_list()
|
277 |
if len(treatment)>0:
|
278 |
-
return pd.DataFrame({
|
279 |
else:
|
280 |
-
return pd.DataFrame({
|
281 |
|
282 |
|
283 |
class MedicalKnowledgeBase():
|
@@ -289,12 +289,15 @@ class MedicalKnowledgeBase():
|
|
289 |
class SimpleMedicalKnowledge(MedicalKnowledgeBase):
|
290 |
"""Simple Medical Knowledge Class"""
|
291 |
def search_medical_info(self, query: str) -> str:
|
|
|
292 |
if "diabetes treatment" in query.lower():
|
293 |
return "The recommended treatment for diabetes includes lifestyle changes, medication, and monitoring"
|
294 |
elif "heart disease risk factors" in query.lower():
|
295 |
return "Risk factors for heart disease include high blood pressure, high cholesterol, and smoking"
|
296 |
else:
|
297 |
-
|
|
|
|
|
298 |
|
299 |
|
300 |
class ForecastingEngine(ABC):
|
@@ -665,27 +668,51 @@ def main():
|
|
665 |
st.json(result)
|
666 |
with insights_tab:
|
667 |
if selected_data_key:
|
668 |
-
|
669 |
-
|
670 |
-
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
|
678 |
-
|
679 |
-
|
680 |
-
|
681 |
-
|
682 |
-
|
683 |
-
|
684 |
-
|
685 |
-
|
686 |
-
|
687 |
-
|
688 |
-
|
689 |
-
|
690 |
-
|
691 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
241 |
class DiagnosisSupport(ABC):
|
242 |
"""Abstract class for implementing clinical diagnoses."""
|
243 |
@abstractmethod
|
244 |
+
def diagnose(self, data: pd.DataFrame, target_col: str, columns: List[str], diagnosis_key : str = "diagnosis" , **kwargs) -> pd.DataFrame:
|
245 |
pass
|
246 |
|
247 |
class SimpleDiagnosis(DiagnosisSupport):
|
|
|
249 |
def __init__(self):
|
250 |
self.model : LogisticRegressionTrainer = LogisticRegressionTrainer()
|
251 |
|
252 |
+
def diagnose(self, data: pd.DataFrame, target_col: str, columns: List[str], diagnosis_key : str = "diagnosis", **kwargs) -> pd.DataFrame:
|
253 |
try:
|
254 |
result = self.model.invoke(data, target_col=target_col, columns = columns)
|
255 |
if "accuracy" in result:
|
256 |
+
return pd.DataFrame({diagnosis_key: [f"Accuracy {result['accuracy']}"],
|
257 |
"model": result["model_type"]})
|
258 |
else:
|
259 |
+
return pd.DataFrame({diagnosis_key: [f"Diagnosis failed: {result}"]})
|
260 |
|
261 |
except Exception as e:
|
262 |
+
return pd.DataFrame({diagnosis_key:[f"Error during diagnosis {e}"]})
|
263 |
|
264 |
|
265 |
class TreatmentRecommendation(ABC):
|
266 |
"""Abstract class for treatment recommendations"""
|
267 |
@abstractmethod
|
268 |
+
def recommend(self, data: pd.DataFrame, condition_col: str, treatment_col:str, recommendation_key: str = "recommendation", **kwargs) -> pd.DataFrame:
|
269 |
pass
|
270 |
|
271 |
class BasicTreatmentRecommendation(TreatmentRecommendation):
|
272 |
"""A placeholder class for basic treatment recommendations"""
|
273 |
+
def recommend(self, data: pd.DataFrame, condition_col: str, treatment_col:str, recommendation_key: str = "recommendation", **kwargs) -> pd.DataFrame:
|
274 |
if condition_col not in data.columns or treatment_col not in data.columns:
|
275 |
+
return pd.DataFrame({recommendation_key: ["Condition or Treatment columns not found!"]})
|
276 |
treatment = data[data[condition_col] == "High"][treatment_col].to_list()
|
277 |
if len(treatment)>0:
|
278 |
+
return pd.DataFrame({recommendation_key: [f"Treatment recommended for High risk patients: {treatment}"]})
|
279 |
else:
|
280 |
+
return pd.DataFrame({recommendation_key: [f"No treatment recommendation found!"]})
|
281 |
|
282 |
|
283 |
class MedicalKnowledgeBase():
|
|
|
289 |
class SimpleMedicalKnowledge(MedicalKnowledgeBase):
|
290 |
"""Simple Medical Knowledge Class"""
|
291 |
def search_medical_info(self, query: str) -> str:
|
292 |
+
try:
|
293 |
if "diabetes treatment" in query.lower():
|
294 |
return "The recommended treatment for diabetes includes lifestyle changes, medication, and monitoring"
|
295 |
elif "heart disease risk factors" in query.lower():
|
296 |
return "Risk factors for heart disease include high blood pressure, high cholesterol, and smoking"
|
297 |
else:
|
298 |
+
return "No specific information is available"
|
299 |
+
except Exception as e:
|
300 |
+
return f"Medical Knowledge Search Failed {e}"
|
301 |
|
302 |
|
303 |
class ForecastingEngine(ABC):
|
|
|
668 |
st.json(result)
|
669 |
with insights_tab:
|
670 |
if selected_data_key:
|
671 |
+
data = st.session_state.data[selected_data_key]
|
672 |
+
available_analysis = ["EDA", "temporal", "distribution", "hypothesis", "model"]
|
673 |
+
selected_analysis = st.multiselect("Select Analysis", available_analysis)
|
674 |
+
if st.button("Generate Automated Insights"):
|
675 |
+
with st.spinner("Generating Insights"):
|
676 |
+
results = st.session_state.automated_insights.generate_insights(data, analysis_names=selected_analysis)
|
677 |
+
st.json(results)
|
678 |
+
st.subheader("Diagnosis Support")
|
679 |
+
target_col = st.selectbox("Select Target Variable for Diagnosis", data.columns.tolist())
|
680 |
+
num_cols = data.select_dtypes(include=np.number).columns.tolist()
|
681 |
+
selected_cols_diagnosis = st.multiselect("Select Feature Variables for Diagnosis", num_cols)
|
682 |
+
if st.button("Generate Diagnosis"):
|
683 |
+
if target_col and selected_cols_diagnosis:
|
684 |
+
with st.spinner("Generating Diagnosis"):
|
685 |
+
result = st.session_state.diagnosis_support.diagnose(data, target_col=target_col, columns=selected_cols_diagnosis, diagnosis_key="diagnosis_result")
|
686 |
+
st.json(result)
|
687 |
+
|
688 |
+
st.subheader("Treatment Recommendation")
|
689 |
+
condition_col = st.selectbox("Select Condition Column for Treatment Recommendation", data.columns.tolist())
|
690 |
+
treatment_col = st.selectbox("Select Treatment Column for Treatment Recommendation", data.columns.tolist())
|
691 |
+
if st.button("Generate Treatment Recommendation"):
|
692 |
+
if condition_col and treatment_col:
|
693 |
+
with st.spinner("Generating Treatment Recommendation"):
|
694 |
+
result = st.session_state.treatment_recommendation.recommend(data, condition_col = condition_col, treatment_col = treatment_col, recommendation_key="treatment_recommendation")
|
695 |
+
st.json(result)
|
696 |
+
|
697 |
+
with reports_tab:
|
698 |
+
st.header("Reports")
|
699 |
+
report_name = st.text_input("Report Name")
|
700 |
+
report_def = st.text_area("Report definition")
|
701 |
+
if st.button("Create Report Definition"):
|
702 |
+
st.session_state.automated_reports.create_report_definition(report_name, report_def)
|
703 |
+
st.success("Report definition created")
|
704 |
+
if selected_data_key:
|
705 |
+
data = st.session_state.data
|
706 |
+
if st.button("Generate Report"):
|
707 |
+
with st.spinner("Generating Report..."):
|
708 |
+
report = st.session_state.automated_reports.generate_report(report_name, data)
|
709 |
+
with knowledge_tab:
|
710 |
+
st.header("Medical Knowledge")
|
711 |
+
query = st.text_input("Enter your medical question here:")
|
712 |
+
if st.button("Search"):
|
713 |
+
with st.spinner("Searching..."):
|
714 |
+
result = st.session_state.knowledge_base.search_medical_info(query)
|
715 |
+
st.write(result)
|
716 |
+
|
717 |
+
if __name__ == "__main__":
|
718 |
+
main()
|