import os import utils import pickle import numpy as np import gradio as gr import tensorflow as tf import matplotlib.pyplot as plt from ttictoc import tic,toc from keras.models import load_model from urllib.request import urlretrieve '''--------------------------- Descarga de modelos ----------------------------''' # 3D U-Net if not os.path.exists("unet.h5"): urlretrieve("https://dl.dropboxusercontent.com/s/ay5q8caqzlad7h5/unet.h5?dl=0", "unet.h5") # Med3D if not os.path.exists("resnet_50_23dataset.pth"): urlretrieve("https://dl.dropboxusercontent.com/s/otxsgx3e31d5h9i/resnet_50_23dataset.pth?dl=0", "resnet_50_23dataset.pth") # Clasificador de imágen SVM if not os.path.exists("svm_model.pickle"): urlretrieve("https://dl.dropboxusercontent.com/s/n3tb3r6oyf06xfx/svm_model.pickle?dl=0", "svm_model.pickle") # Nivel de riesgo if not os.path.exists("mlp_probabilidad.h5"): urlretrieve("https://dl.dropboxusercontent.com/s/78fjlg374mvjygd/mlp_probabilidad.h5?dl=0", "mlp_probabilidad.h5") # Scaler para scores if not os.path.exists("scaler.pickle"): urlretrieve("https://dl.dropboxusercontent.com/s/ow6pe4k45r3xkbl/scaler.pickle?dl=0", "scaler.pickle") # Archivo de texto para reportes if not os.path.exists("report.txt"): urlretrieve("https://dl.dropboxusercontent.com/s/ycjpkd65rhlicxq/report.txt?dl=0", "report.txt") path_3d_unet = 'unet.h5' weight_path = 'resnet_50_23dataset.pth' svm_path = "svm_model.pickle" prob_model_path = "mlp_probabilidad.h5" scaler_path = "scaler.pickle" report_path = "report.txt" '''---------------------------- Carga de modelos ------------------------------''' # 3D U-Net with tf.device("cpu:0"): model_unet = utils.import_3d_unet(path_3d_unet) # MedNet device_ids = [0] mednet_model = utils.create_mednet(weight_path, device_ids) # SVM model svm_model = pickle.load(open(svm_path, 'rb')) # Nivel de riesgo with tf.device("cpu:0"): prob_model = load_model(prob_model_path) # Scaler scaler = pickle.load(open(scaler_path, 'rb')) '''-------------------------------- Funciones ---------------------------------''' def load_img(file): sitk, array = utils.load_img(file.name) # Redimención mri_image = np.transpose(array) mri_image = np.append(mri_image, np.zeros((192-mri_image.shape[0],256,256,)), axis=0) # Rotación mri_image = mri_image.astype(np.float32) mri_image = np.rot90(mri_image, axes=(1,2)) return sitk, mri_image def show_img(img, mri_slice, update): fig = plt.figure() plt.imshow(img[mri_slice,:,:], cmap='gray') if update == True: return fig, gr.update(visible=True), gr.update(visible=True) else: return fig # def show_brain(brain, brain_slice): # fig = plt.figure() # plt.imshow(brain[brain_slice,:,:], cmap='gray') # return fig, gr.update(visible=True) def process_img(img, brain_slice): # progress(None,desc="Processing...") with tf.device("cpu:0"): brain = utils.brain_stripping(img, model_unet) fig, update_slider, _ = show_img(brain, brain_slice, update=True) return brain, fig, update_slider, gr.update(visible=True) def save_file(input_name, input_age, input_phone_num, input_emer_name, input_emer_phone_num, input_sex, input_MMSE, input_GDSCALE, input_CDR, input_FAQ, input_NPI_Q, input_Diastolic_blood_pressure, input_Systolic_blood_pressure, input_Body_heigth, input_Body_weight, input_Heart_rate, input_Respiratory_rate, input_Body_temperature, input_Pluse_oximetry, input_medications, input_allergies,diagnosis): with open(report_path, 'w') as f: # Save Patient Data f.write("Patient data:\n") f.write(f"\tName: {input_name.capitalize()}\n") f.write(f"\tSex: {input_sex}\n") f.write(f"\tAge: {input_age}\n") f.write(f"\tPhone Number: {input_phone_num}\n") f.write(f"\tEmergency Contact Name: {input_emer_name.capitalize()}\n") f.write(f"\tEmergency Contact Phone Number: {input_emer_phone_num}\n\n") # Save Vital Signs f.write("Vital Signs:\n") f.write(f"\tDiastolic blood pressure: {input_Diastolic_blood_pressure} mm Hg\n") f.write(f"\tSystolic blood pressure: {input_Systolic_blood_pressure} mm Hg\n") f.write(f"\tBody height: {input_Body_heigth} cm\n") f.write(f"\tBody weight: {input_Body_weight} kg\n") f.write(f"\tHeart rate: {input_Heart_rate} bpm\n") f.write(f"\tRespiratory rate: {input_Respiratory_rate} bpm\n") f.write(f"\tBody temperature: {input_Body_temperature} °C\n") f.write(f"\tPulse oximetry: {input_Pluse_oximetry}%\n\n") # Save Medications f.write("Medications:\n") f.write(f"\tMedications: {input_medications}\n") f.write(f"\tAllergies: {input_allergies}\n\n") # Save clinical data f.write("Clinical data:\n") f.write(f"\tMMSE total score: {input_MMSE}\n") f.write(f"\tGDSCALE total score: {input_GDSCALE}\n") f.write(f"\tGlobal CDR: {input_CDR}\n") f.write(f"\tFAQ total score: {input_FAQ}\n") f.write(f"\tNPI-Q total score: {input_NPI_Q}\n") # Save Diagnosis f.write("Diagnosis:\n") f.write(f"\t{diagnosis}\n") def get_diagnosis(brain_img, input_name, input_age, input_phone_num, input_emer_name, input_emer_phone_num, input_sex, input_MMSE, input_GDSCALE, input_CDR, input_FAQ, input_NPI_Q, input_Diastolic_blood_pressure, input_Systolic_blood_pressure, input_Body_heigth, input_Body_weight, input_Heart_rate, input_Respiratory_rate, input_Body_temperature, input_Pluse_oximetry, input_medications, input_allergies): # Extracción de características de imagen features = utils.get_features(brain_img, mednet_model) # Clasificación de imagen label_img = np.array([svm_model.predict(features)]) if input_sex == "Male": sex_dum = 1 else: sex_dum = 0 scores = np.array([input_age, input_MMSE, input_GDSCALE, input_CDR, input_FAQ, input_NPI_Q, sex_dum, label_img]) print(scores) # Normalización de scores scores_norm = scaler.transform(scores.reshape(1,-1)) print(scores_norm) with tf.device("cpu:0"): # Probabilidad de tener MCI prob = prob_model.predict(scores_norm)[0,0] # Probabilidad de tener MCI print(prob) diagnosis = f"The patient has a probability of {(100*prob):.2f}% of having MCI with a sensitivity of 92.00% and a specificity of 92.75%" save_file(input_name, input_age, input_phone_num, input_emer_name, input_emer_phone_num, input_sex, input_MMSE, input_GDSCALE, input_CDR, input_FAQ, input_NPI_Q, input_Diastolic_blood_pressure, input_Systolic_blood_pressure, input_Body_heigth, input_Body_weight, input_Heart_rate, input_Respiratory_rate, input_Body_temperature, input_Pluse_oximetry, input_medications, input_allergies,diagnosis) return gr.update(value=diagnosis), gr.update(value=report_path, visible=True), gr.update(visible=True) def clear(): return gr.File.update(value=None), gr.Plot.update(value=None), gr.update(visible=False), gr.Plot.update(value=None), gr.update(visible=False), gr.update(value="The diagnosis will show here..."), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) '''--------------------------------- Interfaz ---------------------------------''' with gr.Blocks(theme=gr.themes.Base()) as demo: with gr.Row(): # gr.HTML(r"""
""") gr.HTML(r"""
""") # Inputs with gr.Row(): with gr.Column(variant="panel", scale=1): with gr.Tab("Patient Information"): gr.Markdown('

Patient Information

') with gr.Tab("Personal data"): # Objeto para subir archivo nifti input_name = gr.Textbox(placeholder='Enter the patient name', label='Patient name') input_age = gr.Number(label='Age', value=None) input_phone_num = gr.Number(label='Phone number') input_emer_name = gr.Textbox(placeholder='Enter the emergency contact name', label='Emergency contact name') input_emer_phone_num = gr.Number(label='Emergency contact name phone number', value=None) input_sex = gr.Dropdown(["Male", "Female"], label="Sex", value="Male") with gr.Tab("Clinical data"): input_MMSE = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="MMSE total score") input_GDSCALE = gr.Slider(minimum=0, maximum=12, value=0, step=1, label="GDSCALE total score") input_CDR = gr.Slider(minimum=0, maximum=3, value=0, step=0.5, label="Global CDR") input_FAQ = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="FAQ total score") input_NPI_Q = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="NPI-Q total score") with gr.Tab("Vital Signs"): input_Diastolic_blood_pressure = gr.Number(label='Diastolic Blood Pressure(mm Hg)') input_Systolic_blood_pressure = gr.Number(label='Systolic Blood Pressure(mm Hg)') input_Body_heigth = gr.Number(label='Body heigth (cm)') input_Body_weight = gr.Number(label='Body weigth (kg)') input_Heart_rate = gr.Number(label='Heart rate (bpm)') input_Respiratory_rate = gr.Number(label='Respiratory rate (bpm)') input_Body_temperature = gr.Number(label='Body temperature (°C)') input_Pluse_oximetry = gr.Number(label='Pluse oximetry (%)') with gr.Tab("Medications"): input_medications = gr.Textbox(label='Medications', lines=5) input_allergies = gr.Textbox(label='Allergies', lines=5) with gr.Box(): gr.Markdown('

Upload MRI

') input_file = gr.File(file_count="single", label="MRI File (.nii)", file_types=[".nii"], show_label=False) with gr.Row(): # Botón para cargar imagen load_img_button = gr.Button(value="Load") # Botón para borrar clear_button = gr.Button(value="Clear") # Botón para procesar imagen process_button = gr.Button(value="Process MRI", visible=False, variant="primary") # Botón para obtener diagnostico diagnostic_button = gr.Button(value="Get diagnosis", visible=False, variant="primary") with gr.Box(visible=False) as download_box: gr.Markdown('

Download diagnosis report

') # Descarga de archivo output_file = gr.File(file_count="single", show_label=False, interactive=False, visible=True) with gr.Tab("About"): gr.Markdown(''' # SIMCI Alzheimer is a degenerative and irreversible neurological disorder that affects cognitive abilities and daily activities. It is divided into three stages: preclinical, mild cognitive impairment (MCI), and dementia. MCI represents a transition to dementia, characterized by a greater-than-expected decline in cognitive function without interference in daily activities. It is estimated that approximately 20% of individuals with MCI progress to dementia, but diagnostic errors are common in this early stage due to ambiguous cognitive changes, including those observed in neuroimaging. SIMCI is a system for detecting mild cognitive impairment that employs a multimodal approach and a stratification process to address this issue. The system utilizes demographic characteristics and clinical test results to provide medical interpretability and assist specialists in decision-making. The database includes magnetic resonance imaging (MRI) brain scans, clinical examination results, and demographic information. SIMCI achieves an F1-score of 0.9233, a sensitivity of 0.9200, and a specificity of 0.9275.
## Repository https://github.com/SebastianBravo/SIMCI ## Authors - Daniel Stiven Zambrano Acosta, B.Sc - Juan Sebastián Bravo Santacruz, B.Sc - Ing. Wilson Javier Arenas López, M.Sc - Psy. Pablo Alexander Reyes Gavilan, PhD - Ing. Miguel Alfonso Altuve, PhD ## Acknowledgement - Data collection and sharing for this project was funded by the Alzheimer's Disease Neuroimaging Initiative (ADNI) (National Institutes of Health Grant U01 AG024904) and DOD ADNI (Department of Defense award number W81XWH-12-2-0012). ADNI is funded by the National Institute on Aging, the National Institute of Biomedical Imaging and Bioengineering, and through generous contributions from the following: AbbVie, Alzheimer’s Association; Alzheimer’s Drug Discovery Foundation; Araclon Biotech; BioClinica, Inc.; Biogen; Bristol-Myers Squibb Company; CereSpir, Inc.; Cogstate; Eisai Inc.; Elan Pharmaceuticals, Inc.; Eli Lilly and Company; EuroImmun; F. Hoffmann-La Roche Ltd and its affiliated company Genentech, Inc.; Fujirebio; GE Healthcare; IXICO Ltd.; Janssen Alzheimer Immunotherapy Research & Development, LLC.; Johnson & Johnson Pharmaceutical Research & Development LLC.; Lumosity; Lundbeck; Merck & Co., Inc.; Meso Scale Diagnostics, LLC.; NeuroRx Research; Neurotrack Technologies; Novartis Pharmaceuticals Corporation; Pfizer Inc.; Piramal Imaging; Servier; Takeda Pharmaceutical Company; and Transition Therapeutics. The Canadian Institutes of Health Research is providing funds to support ADNI clinical sites in Canada. Private sector contributions are facilitated by the Foundation for the National Institutes of Health (www.fnih.org). The grantee organization is the Northern California Institute for Research and Education, and the study is coordinated by the Alzheimer’s Therapeutic Research Institute at the University of Southern California. ADNI data are disseminated by the Laboratory for Neuro Imaging at the University of Southern California. - We thank [MedicalNet](https://github.com/Tencent/MedicalNet) and [segmentation_models_3D](https://github.com/ZFTurbo/segmentation_models_3D) which we build SIMCI. ''') # Outputs with gr.Column(variant="panel", scale=1): gr.Markdown('

MRI visualization

') with gr.Box(): gr.Markdown('

Loaded MRI

') # Plot para imágen original plot_img_original = gr.Plot(show_label=False) # Slider para imágen original mri_slider = gr.Slider(minimum=0, maximum=192, value=100, step=1, label="MRI Slice", visible=False) with gr.Box(): gr.Markdown('

Proccessed MRI

') # Plot para imágen procesada plot_brain = gr.Plot(show_label=False, visible=True) # Slider para imágen procesada brain_slider = gr.Slider(minimum=0, maximum=192, value=100, step=1, label="MRI Slice", visible=False) with gr.Box(): gr.Markdown('

Diagnosis

') # Texto del diagnostico diagnosis_text = gr.Textbox(label="Diagnosis",interactive=False, placeholder="The diagnosis will show here...") # Variables original_input_sitk = gr.State() original_input_img = gr.State() brain_img = gr.State() update_true = gr.State(True) update_false = gr.State(False) # Cambios # Cargar imagen nueva input_file.change(load_img, input_file, [original_input_sitk, original_input_img]) # Mostrar imagen nueva load_img_button.click(show_img, [original_input_img, mri_slider, update_true], [plot_img_original, mri_slider, process_button]) # Actualizar imagen original mri_slider.change(show_img, [original_input_img, mri_slider, update_false], [plot_img_original]) # Procesar imagen process_button.click(fn=process_img, inputs=[original_input_sitk, brain_slider], outputs=[brain_img,plot_brain,brain_slider, diagnostic_button]) # Actualizar imagen procesada brain_slider.change(show_img, [brain_img, brain_slider, update_false], [plot_brain]) # Actualizar diagnostico diagnostic_button.click(fn=get_diagnosis, inputs=[brain_img, input_name, input_age, input_phone_num, input_emer_name, input_emer_phone_num, input_sex, input_MMSE, input_GDSCALE, input_CDR, input_FAQ, input_NPI_Q, input_Diastolic_blood_pressure, input_Systolic_blood_pressure, input_Body_heigth, input_Body_weight, input_Heart_rate, input_Respiratory_rate, input_Body_temperature, input_Pluse_oximetry, input_medications, input_allergies], outputs=[diagnosis_text, output_file, download_box]) # Limpiar campos clear_button.click(fn=clear, outputs=[input_file, plot_img_original, mri_slider, plot_brain, brain_slider, diagnosis_text, process_button, diagnostic_button, download_box]) if __name__ == "__main__": # demo.queue(concurrency_count=20) demo.launch()