Spaces:
Sleeping
Sleeping
update app.py -- added random sampling by BodyPartExamined
Browse filesadded random sampling by BodyPartExamined
BodyPartExamined is as of now defined in a static manner, due to large variability in orientation and dcm2niix behaviour, in the future use idc_index to retrieve all lists and incorporate sanity checks.
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import glob
|
|
| 7 |
import shutil
|
| 8 |
import dcm2niix
|
| 9 |
import subprocess
|
|
|
|
| 10 |
|
| 11 |
from model.data_process.demo_data_process import process_ct_gt
|
| 12 |
import numpy as np
|
|
@@ -42,19 +43,46 @@ def download_idc_data_serieUID(serieUID_lst, output_folder):
|
|
| 42 |
"-f", "IDC_%i", "-g", "y", sample_dcm_dir])
|
| 43 |
return glob.glob(os.path.join(output_folder, "*nii/*.nii.gz"))
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
#############################################
|
| 46 |
st.session_state.option = None
|
| 47 |
|
| 48 |
if 'idc_data' not in st.session_state:
|
| 49 |
-
case_list = download_idc_data_serieUID(serieUID_lst=["1.3.6.1.4.1.14519.5.2.1.8421.4008.125612661111422710051062993644",
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
st.session_state.idc_data = True
|
| 56 |
else:
|
| 57 |
case_list = glob.glob("model/asset/idc_samples/*nii/*.nii.gz")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if 'idc_serieUID_sample' not in st.session_state:
|
| 59 |
st.session_state.idc_serieUID_sample = None
|
| 60 |
# init session_state
|
|
@@ -110,11 +138,13 @@ def reset_demo_case():
|
|
| 110 |
st.session_state.data_item = None
|
| 111 |
st.session_state.idc_serieUID_sample = None
|
| 112 |
st.session_state.reset_demo_case = True
|
|
|
|
| 113 |
clear_prompts()
|
| 114 |
|
| 115 |
def clear_file():
|
| 116 |
st.session_state.option = None
|
| 117 |
st.session_state.idc_serieUID_sample = None
|
|
|
|
| 118 |
process_ct_gt.clear()
|
| 119 |
reset_demo_case()
|
| 120 |
clear_prompts()
|
|
@@ -135,7 +165,9 @@ with arxive_col:
|
|
| 135 |
# modify demo case here
|
| 136 |
demo_type = st.radio(
|
| 137 |
"Demo case source",
|
| 138 |
-
["Select an IDC demo case from tcga_lihc collection",
|
|
|
|
|
|
|
| 139 |
on_change=clear_file
|
| 140 |
)
|
| 141 |
|
|
@@ -146,7 +178,7 @@ if demo_type=="Select an IDC demo case from tcga_lihc collection":
|
|
| 146 |
index=None,
|
| 147 |
placeholder="Select a demo case...",
|
| 148 |
on_change=reset_demo_case)
|
| 149 |
-
|
| 150 |
with st.form("Filter by DICOM SerieUID"):
|
| 151 |
uploaded_serieUID = st.text_input("Enter a DICOM SeriesInstanceUID", value=None)
|
| 152 |
submitted = st.form_submit_button("Submit", on_click=clear_prompts)
|
|
@@ -156,6 +188,34 @@ else:
|
|
| 156 |
uploaded_file = st.session_state.idc_serieUID_sample
|
| 157 |
else:
|
| 158 |
uploaded_file = st.session_state.idc_serieUID_sample
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
st.session_state.option = uploaded_file
|
| 161 |
|
|
|
|
| 7 |
import shutil
|
| 8 |
import dcm2niix
|
| 9 |
import subprocess
|
| 10 |
+
import random
|
| 11 |
|
| 12 |
from model.data_process.demo_data_process import process_ct_gt
|
| 13 |
import numpy as np
|
|
|
|
| 43 |
"-f", "IDC_%i", "-g", "y", sample_dcm_dir])
|
| 44 |
return glob.glob(os.path.join(output_folder, "*nii/*.nii.gz"))
|
| 45 |
|
| 46 |
+
|
| 47 |
+
def get_random_sample_idc_from_bodypart(bodypart_selected):
|
| 48 |
+
client = index.IDCClient()
|
| 49 |
+
# body_parts = client.index[(client.index['Modality'].isin(['CT']))&(idc_client.index['instanceCount']> '100')]['BodyPartExamined'].unique()
|
| 50 |
+
matching_series_list = client.index[client.index['Modality'].isin(["CT"]) \
|
| 51 |
+
& (client.index['BodyPartExamined'] == bodypart_selected) & \
|
| 52 |
+
(client.index['instanceCount']> '100')]['SeriesInstanceUID'].values
|
| 53 |
+
# select random series from the list
|
| 54 |
+
random_series_uid = random.choice(matching_series_list)
|
| 55 |
+
random_series_viewer_url = client.get_viewer_URL(random_series_uid)
|
| 56 |
+
return random_series_uid, random_series_viewer_url
|
| 57 |
+
|
| 58 |
+
def retrieve_idc_index_body_parts():
|
| 59 |
+
idc_client = index.IDCClient()
|
| 60 |
+
body_parts = idc_client.index[(idc_client.index['Modality'].isin(['CT']))&(idc_client.index['instanceCount']< '150')]['BodyPartExamined'].unique()
|
| 61 |
+
return body_parts
|
| 62 |
+
|
| 63 |
#############################################
|
| 64 |
st.session_state.option = None
|
| 65 |
|
| 66 |
if 'idc_data' not in st.session_state:
|
| 67 |
+
# case_list = download_idc_data_serieUID(serieUID_lst=["1.3.6.1.4.1.14519.5.2.1.8421.4008.125612661111422710051062993644",
|
| 68 |
+
# "1.3.6.1.4.1.14519.5.2.1.3344.4008.552105302448832783460360105045",
|
| 69 |
+
# "1.3.6.1.4.1.14519.5.2.1.3344.4008.217290429362492484143666931850",
|
| 70 |
+
# "1.3.6.1.4.1.14519.5.2.1.3344.4008.315023636447426194723399171147",
|
| 71 |
+
# "1.3.6.1.4.1.14519.5.2.1.3344.4008.307374355712319704057189924161"],
|
| 72 |
+
# output_folder="model/asset/idc_samples")
|
| 73 |
+
case_list = []
|
| 74 |
st.session_state.idc_data = True
|
| 75 |
else:
|
| 76 |
case_list = glob.glob("model/asset/idc_samples/*nii/*.nii.gz")
|
| 77 |
+
# if 'idc_index_body_part' not in st.session_state:
|
| 78 |
+
# body_part_list = retrieve_idc_index_body_parts()
|
| 79 |
+
# st.session_state.idc_index_body_part = True
|
| 80 |
+
# else:
|
| 81 |
+
# body_part_list = [""]
|
| 82 |
+
# if 'init_idc_client' not in st.session_state:
|
| 83 |
+
# st.session_state.idc_client = index.IDCClient()
|
| 84 |
+
# if 'idc_bodypart_selected' not in st.session_state:
|
| 85 |
+
# st.session_state.idc_bodypart_selected = False
|
| 86 |
if 'idc_serieUID_sample' not in st.session_state:
|
| 87 |
st.session_state.idc_serieUID_sample = None
|
| 88 |
# init session_state
|
|
|
|
| 138 |
st.session_state.data_item = None
|
| 139 |
st.session_state.idc_serieUID_sample = None
|
| 140 |
st.session_state.reset_demo_case = True
|
| 141 |
+
st.session_state.idc_bodypart_selected = False
|
| 142 |
clear_prompts()
|
| 143 |
|
| 144 |
def clear_file():
|
| 145 |
st.session_state.option = None
|
| 146 |
st.session_state.idc_serieUID_sample = None
|
| 147 |
+
st.session_state.idc_bodypart_selected = False
|
| 148 |
process_ct_gt.clear()
|
| 149 |
reset_demo_case()
|
| 150 |
clear_prompts()
|
|
|
|
| 165 |
# modify demo case here
|
| 166 |
demo_type = st.radio(
|
| 167 |
"Demo case source",
|
| 168 |
+
["Select an IDC demo case from tcga_lihc collection",
|
| 169 |
+
"Filter by DICOM SeriesInstanceUID",
|
| 170 |
+
"Random sampling based on BodyPartExamined"],
|
| 171 |
on_change=clear_file
|
| 172 |
)
|
| 173 |
|
|
|
|
| 178 |
index=None,
|
| 179 |
placeholder="Select a demo case...",
|
| 180 |
on_change=reset_demo_case)
|
| 181 |
+
elif demo_type=="Filter by DICOM SeriesInstanceUID":
|
| 182 |
with st.form("Filter by DICOM SerieUID"):
|
| 183 |
uploaded_serieUID = st.text_input("Enter a DICOM SeriesInstanceUID", value=None)
|
| 184 |
submitted = st.form_submit_button("Submit", on_click=clear_prompts)
|
|
|
|
| 188 |
uploaded_file = st.session_state.idc_serieUID_sample
|
| 189 |
else:
|
| 190 |
uploaded_file = st.session_state.idc_serieUID_sample
|
| 191 |
+
else:#elif demo_type == "Random sampling based on BodyPartExamined":
|
| 192 |
+
with st.form("Filter by DICOM BodyPartExamined Tag") as form_body_part:
|
| 193 |
+
# body_part_list = retrieve_idc_index_body_parts()
|
| 194 |
+
body_part_selected = st.selectbox(
|
| 195 |
+
"Select a bodypart to randomly sample a CT scan from",
|
| 196 |
+
["ABDOMEN", "LUNG", "LIVER",
|
| 197 |
+
"PELVIS"],
|
| 198 |
+
index=None,
|
| 199 |
+
placeholder="Select a bodypart to pick a SeriesInstanceUID from...")
|
| 200 |
+
submitted = st.form_submit_button("Submit", on_click=reset_demo_case)
|
| 201 |
+
#if st.session_state.reset_demo_case == True and body_part_selected is not None:# and st.session_state.idc_bodypart_selected == False and
|
| 202 |
+
if submitted:
|
| 203 |
+
serieUID, ohif_link = get_random_sample_idc_from_bodypart(body_part_selected)
|
| 204 |
+
for i in range(0,5):
|
| 205 |
+
if os.path.exists("model/asset/idc_serieUID_random_sample"):
|
| 206 |
+
shutil.rmtree("model/asset/idc_serieUID_random_sample")
|
| 207 |
+
st.session_state.idc_serieUID_sample = download_idc_data_serieUID([str(serieUID)], "model/asset/idc_serieUID_random_sample")[0]
|
| 208 |
+
path_file = glob.glob(f"model/asset/idc_serieUID_random_sample/ddl_series0_nii/*.nii.gz")
|
| 209 |
+
if path_file and len(path_file) == 1:
|
| 210 |
+
break
|
| 211 |
+
else:
|
| 212 |
+
print("serieUID NOT FILLING BASIC REQs --> MORE THAN 1 NII FILE OR NO NII FILE")
|
| 213 |
+
# st.write(f"SeriesInstanceUID randomly sampled from chosen BodyPartExamined : {random_series_uid}")
|
| 214 |
+
# st.write(f"OHIF URL of selected sample : {random_series_viewer_url}")
|
| 215 |
+
# st.session_state.idc_bodypart_selected = True
|
| 216 |
+
uploaded_file = st.session_state.idc_serieUID_sample
|
| 217 |
+
else:
|
| 218 |
+
uploaded_file = st.session_state.idc_serieUID_sample
|
| 219 |
|
| 220 |
st.session_state.option = uploaded_file
|
| 221 |
|