File size: 5,142 Bytes
7837144
 
 
 
 
 
 
 
 
 
b8d3c98
 
15b72a2
7837144
2b62c92
7837144
 
 
 
2b62c92
 
7837144
 
 
 
 
 
 
 
2b62c92
3d36514
 
2b62c92
7837144
 
 
 
 
 
758f853
7837144
 
 
 
 
758f853
 
7837144
 
 
 
 
 
758f853
7837144
 
 
 
 
 
758f853
2b62c92
 
 
 
 
 
 
7837144
 
9a8d565
7837144
b7cebe5
 
7837144
b7cebe5
2b62c92
b7cebe5
d606a36
 
5eec800
d606a36
5eec800
 
d606a36
5eec800
 
 
d606a36
5eec800
 
25188f0
5eec800
 
 
 
25188f0
b8d3c98
5eec800
 
b7cebe5
 
 
 
 
 
 
d606a36
5eec800
8017014
15b72a2
 
 
 
 
b7cebe5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import streamlit as st
from idc_index import index
from pathlib import Path
import pydicom
import pandas as pd
from tempfile import TemporaryDirectory
import os
from pathlib import Path
import pydicom.datadict as dd
import shutil
import papermill as pm 
import subprocess 
from PIL import Image

# st.write(os.listdir())

# Main Streamlit app code
st.title("DICOM Classification Demo")
st.write("Select IDC data to download, extract images and metadata, and perform inference using three pre-trained models")
st.write("NOTE: This demo only works for classification of MR series of the prostate - T2 weighted axial, DWI, ADC and DCE")
st.write("NOTE: These models were trained on patients from QIN-Prostate-Repeatability PCAMPMRI-00001 to PCAMPMRI-00012 and on ProstateX ProstateX-0000 to ProstateX-0275 patients. Therefore it is wise to not evaluate on those patients." )

# Fetch IDC index
client = index.IDCClient()
index_df = client.index

# Option to choose IDC data
st.subheader("Choose IDC Data to Process")
collection_ids = index_df["collection_id"].unique()
# only keep collection_ids with prostate in the name 
# collection_ids = [f if "prostate" in f for f in collection_ids]
collection_ids = [f for f in collection_ids if "prostate" in f]
print('collection_ids: ' + str(collection_ids))
selected_collection_id = st.selectbox("Select Collection ID", collection_ids)

# Filter dataframe based on selected collection_id
df_filtered_by_collection = index_df[index_df["collection_id"] == selected_collection_id]

patients = df_filtered_by_collection["PatientID"].unique()
patients = sorted(patients)
selected_patient_id = st.selectbox("Select Patient ID", patients)

# Filter dataframe based on selected patient_id
df_filtered_by_patient = df_filtered_by_collection[df_filtered_by_collection["PatientID"] == selected_patient_id]

# modalities = df_filtered_by_patient["Modality"].unique()
modalities = ["MR"]
selected_modality = st.selectbox("Select Modality", modalities)

# Filter dataframe based on selected modality
df_filtered_by_modality = df_filtered_by_patient[df_filtered_by_patient["Modality"] == selected_modality]

studies = df_filtered_by_modality["StudyInstanceUID"].unique()
studies = sorted(studies)
selected_study = st.selectbox("Select Study", studies)

# Filter dataframe based on selected study
df_filtered_by_study = df_filtered_by_modality[df_filtered_by_modality["StudyInstanceUID"] == selected_study]

series = df_filtered_by_study["SeriesInstanceUID"].unique()
series = sorted(series)
# Get the corresponding list of SeriesDescriptions 
series_descriptions = df_filtered_by_study[df_filtered_by_study['SeriesInstanceUID'].isin(series)]['SeriesDescription'].values 

print('number of series: ' + str(len(series)))
print('series_descriptions: ' + str(series_descriptions))
print('number of series_descriptions: ' + str(len(series_descriptions)))

selected_series = st.selectbox("Select Series", series)

print('selected_series: ' + str(selected_series))

# Button to run the notebook - which loads the pretrained models and runs inference 
if st.button("Run inference"):

    # Code to run when the button is pressed
    st.write("Button pressed! Running inference using three models")

    if not os.path.exists("DICOMScanClassification_user_demo.ipynb"):
        subprocess.run(["wget", "https://raw.githubusercontent.com/deepakri201/DICOMScanClassification_pw41/main/DICOMScanClassification_user_demo.ipynb"])

    if not os.path.exists("scaling_factors.csv"):
        subprocess.run(["wget", "https://github.com/deepakri201/DICOMScanClassification/releases/download/v1.0.0/scaling_factors.csv"])

    if not os.path.exists("metadata_only_model.zip"):
        subprocess.run(["wget", "https://github.com/deepakri201/DICOMScanClassification/releases/download/v1.0.0/metadata_only_model.zip"])
        subprocess.run(["unzip", "metadata_only_model.zip"])

    if not os.path.exists("images_and_metadata_model.zip"):
        subprocess.run(["wget", "https://github.com/deepakri201/DICOMScanClassification/releases/download/v1.0.0/images_and_metadata_model.zip"])
        subprocess.run(["unzip", "images_and_metadata_model.zip"])
    
    if not os.path.exists("images_only_model.zip"):
        subprocess.run(["wget", "https://github.com/deepakri201/DICOMScanClassification/releases/download/v1.0.0/images_only_model.zip"])
        subprocess.run(["unzip", "images_only_model.zip"])
        
    subprocess.run(["papermill", "-p", "SeriesInstanceUID", selected_series,  "DICOMScanClassification_user_demo.ipynb", "output.ipynb"])

    st.write(subprocess.run(["ls","-R"]))
             
    with open('output.ipynb', "rb") as f:
        st.download_button(
            label="Download the output notebook file",
            data=f,
            file_name="output.ipynb",
            mime="application/json"
        )

    # show classification results df 
    st.write(pd.read_csv('classification_results.csv'))

    # show image 
    image_path = 'image_for_classification.png'
    image = Image.open(image_path).convert('L')
    st.image(image, caption='input image for classification', use_column_width=True)