Commit
·
7837144
1
Parent(s):
83aadff
added file
Browse files
app.py
CHANGED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from idc_index import index
|
3 |
+
from pathlib import Path
|
4 |
+
import pydicom
|
5 |
+
import pandas as pd
|
6 |
+
import pyarrow as pa
|
7 |
+
import pyarrow.parquet as pq
|
8 |
+
from tempfile import TemporaryDirectory
|
9 |
+
import os
|
10 |
+
from pathlib import Path
|
11 |
+
import polars
|
12 |
+
import pydicom.datadict as dd
|
13 |
+
import shutil
|
14 |
+
|
15 |
+
|
16 |
+
# Main Streamlit app code
|
17 |
+
st.title("DICOM Classification Demo")
|
18 |
+
st.write("Select IDC data to download, extract images and metadata, and perform inference using three pre-trained models")
|
19 |
+
|
20 |
+
# Fetch IDC index
|
21 |
+
client = index.IDCClient()
|
22 |
+
index_df = client.index
|
23 |
+
|
24 |
+
# Option to choose IDC data
|
25 |
+
st.subheader("Choose IDC Data to Process")
|
26 |
+
collection_ids = index_df["collection_id"].unique()
|
27 |
+
selected_collection_id = st.selectbox("Select Collection ID", collection_ids)
|
28 |
+
|
29 |
+
# Filter dataframe based on selected collection_id
|
30 |
+
df_filtered_by_collection = index_df[index_df["collection_id"] == selected_collection_id]
|
31 |
+
|
32 |
+
patients = df_filtered_by_collection["PatientID"].unique()
|
33 |
+
selected_patient_id = st.selectbox("Select Patient ID", patients)
|
34 |
+
|
35 |
+
# Filter dataframe based on selected patient_id
|
36 |
+
df_filtered_by_patient = df_filtered_by_collection[df_filtered_by_collection["PatientID"] == selected_patient_id]
|
37 |
+
|
38 |
+
modalities = df_filtered_by_patient["Modality"].unique()
|
39 |
+
selected_modality = st.selectbox("Select Modality", modalities)
|
40 |
+
|
41 |
+
# Filter dataframe based on selected modality
|
42 |
+
df_filtered_by_modality = df_filtered_by_patient[df_filtered_by_patient["Modality"] == selected_modality]
|
43 |
+
|
44 |
+
studies = df_filtered_by_modality["StudyInstanceUID"].unique()
|
45 |
+
selected_study = st.selectbox("Select Study", studies)
|
46 |
+
|
47 |
+
# Filter dataframe based on selected study
|
48 |
+
df_filtered_by_study = df_filtered_by_modality[df_filtered_by_modality["StudyInstanceUID"] == selected_study]
|
49 |
+
|
50 |
+
series = df_filtered_by_study["SeriesInstanceUID"].unique()
|
51 |
+
selected_series = st.selectbox("Select Series", series)
|
52 |
+
|
53 |
+
|
54 |
+
# Button to process IDC data
|
55 |
+
if st.button("Process IDC data"):
|
56 |
+
# Fetch data from IDC based on selection
|
57 |
+
selection = index_df[
|
58 |
+
(index_df["SeriesInstanceUID"] == selected_series)
|
59 |
+
]
|
60 |
+
|
61 |
+
series_instance_uids = selection["SeriesInstanceUID"].tolist()
|
62 |
+
|
63 |
+
# with TemporaryDirectory() as temp_dir:
|
64 |
+
download_errors = []
|
65 |
+
#input_dir = os.path.join(temp_dir, "input_data")
|
66 |
+
input_dir=Path("input_data/")
|
67 |
+
if input_dir.exists():
|
68 |
+
shutil.rmtree(input_dir)
|
69 |
+
os.makedirs(input_dir, exist_ok=True)
|
70 |
+
|
71 |
+
try:
|
72 |
+
client.download_from_selection(seriesInstanceUID=series_instance_uids, downloadDir=input_dir)
|
73 |
+
except Exception as e:
|
74 |
+
download_errors.append(f"Error downloading data: {str(e)}")
|
75 |
+
|
76 |
+
if download_errors:
|
77 |
+
st.error("\n".join(download_errors))
|
78 |
+
else:
|
79 |
+
st.success("Data downloaded successfully.")
|
80 |
+
|
81 |
+
# Process downloaded DICOM data
|
82 |
+
dicom_files = [str(file) for file in input_dir.glob('**/*.dcm')]
|
83 |
+
# parquet_file = 'dcm2parquet_output.parquet'
|
84 |
+
# save_dicom_header_to_parquet(dicom_files, parquet_file)
|
85 |
+
|
86 |
+
st.success("Processing complete.")
|
87 |
+
# with open(parquet_file, "rb") as f:
|
88 |
+
# st.download_button(
|
89 |
+
# label="Download the metadata parquet file",
|
90 |
+
# data=f,
|
91 |
+
# file_name="dcm2parquet_output.parquet",
|
92 |
+
# mime="application/parquet"
|
93 |
+
# )
|