Skip to content

Commit dbdf31d

Browse files
author
gurayerus
committed
Edit
1 parent a1c34e9 commit dbdf31d

File tree

5 files changed

+1035
-91
lines changed

5 files changed

+1035
-91
lines changed

src/NiChart_Viewer/src/pages/view_plot_data.py

Lines changed: 95 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@
1111
from streamlit_plotly_events import plotly_events
1212
from utils_trace import *
1313
from st_pages import hide_pages
14+
import os
15+
import tkinter as tk
16+
from tkinter import filedialog
17+
18+
19+
def browse_file_folder(is_file, init_dir):
20+
root = tk.Tk()
21+
# root.withdraw() # Hide the main window
22+
if is_file == True:
23+
out_path = filedialog.askopenfilenames(initialdir = init_dir, multiple=0)
24+
else:
25+
out_path = filedialog.askdirectory(initialdir = init_dir)
26+
27+
print('aaaaaa')
28+
print(out_path[0:5])
29+
input()
30+
31+
return out_path
1432

1533
#hide_pages(["Image Processing", "Data Analytics"])
1634

@@ -202,80 +220,91 @@ def filter_dataframe(df: pd.DataFrame, pid) -> pd.DataFrame:
202220
# # Config page
203221
# st.set_page_config(page_title="DataFrame Demo", page_icon="📊", layout='wide')
204222

205-
# FIXME: Input data is hardcoded here for now
206-
fname = "../examples/test_input3/ROIS_tmp2.csv"
207-
df = pd.read_csv(fname)
223+
dir_root = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd())))
208224

209225
# Page controls in side bar
210226
with st.sidebar:
211-
212227
with st.container(border=True):
213228

214-
# Slider to set number of plots in a row
215-
st.session_state.plot_per_raw = st.slider('Plots per raw',1, 5, 3, key='a_per_page')
229+
# Input file name (user can enter either using the file browser or type full path)
230+
default_spare_name = ''
231+
fname_spare = st.sidebar.button("Select input file", on_click = browse_file_folder, key = 'fname_spare_btn', args=[True, dir_root])
232+
if fname_spare == False:
233+
fname_spare = default_spare_name
234+
spare_csv = st.sidebar.text_input("Enter the name of the ROI csv file:", value = fname_spare,
235+
label_visibility="collapsed")
216236

217-
with st.container(border=True):
237+
if os.path.exists(spare_csv):
238+
df = pd.read_csv(spare_csv)
239+
240+
with st.sidebar:
241+
with st.container(border=True):
242+
243+
# Slider to set number of plots in a row
244+
st.session_state.plot_per_raw = st.slider('Plots per raw',1, 5, 3, key='a_per_page')
245+
246+
with st.container(border=True):
247+
248+
st.write('Plot Settings')
249+
250+
# Tabs for parameters
251+
ptabs = st.tabs([":lock:", ":large_orange_circle:", ":large_yellow_circle:",
252+
":large_green_circle:"])
218253

219-
st.write('Plot Settings')
220-
221-
# Tabs for parameters
222-
ptabs = st.tabs([":lock:", ":large_orange_circle:", ":large_yellow_circle:",
223-
":large_green_circle:"])
224-
225-
# Tab 0: to set plotting parameters
226-
with ptabs[1]:
227-
# Default values for plot params
228-
st.session_state.default_hue_var = 'Sex'
229-
230-
def_ind_x = 0
231-
if st.session_state.default_x_var in df.columns:
232-
def_ind_x = df.columns.get_loc(st.session_state.default_x_var)
233-
234-
def_ind_y = 0
235-
if st.session_state.default_y_var in df.columns:
236-
def_ind_y = df.columns.get_loc(st.session_state.default_y_var)
237-
238-
def_ind_hue = 0
239-
if st.session_state.default_hue_var in df.columns:
240-
def_ind_hue = df.columns.get_loc(st.session_state.default_hue_var)
241-
242-
st.session_state.default_x_var = st.selectbox("Default X Var", df.columns, key=f"x_var_init",
243-
index = def_ind_x)
244-
st.session_state.default_y_var = st.selectbox("Default Y Var", df.columns, key=f"y_var_init",
245-
index = def_ind_y)
246-
st.session_state.default_hue_var = st.selectbox("Default Hue Var", df.columns, key=f"hue_var_init",
247-
index = def_ind_hue)
248-
trend_index = st.session_state.trend_types.index(st.session_state.default_trend_type)
249-
st.session_state.default_trend_type = st.selectbox("Default Trend Line", st.session_state.trend_types,
250-
key=f"trend_type_init", index = trend_index)
251-
252-
# Button to add a new plot
253-
if st.button("Add plot"):
254+
# Tab 0: to set plotting parameters
255+
with ptabs[1]:
256+
# Default values for plot params
257+
st.session_state.default_hue_var = 'Sex'
258+
259+
def_ind_x = 0
260+
if st.session_state.default_x_var in df.columns:
261+
def_ind_x = df.columns.get_loc(st.session_state.default_x_var)
262+
263+
def_ind_y = 0
264+
if st.session_state.default_y_var in df.columns:
265+
def_ind_y = df.columns.get_loc(st.session_state.default_y_var)
266+
267+
def_ind_hue = 0
268+
if st.session_state.default_hue_var in df.columns:
269+
def_ind_hue = df.columns.get_loc(st.session_state.default_hue_var)
270+
271+
st.session_state.default_x_var = st.selectbox("Default X Var", df.columns, key=f"x_var_init",
272+
index = def_ind_x)
273+
st.session_state.default_y_var = st.selectbox("Default Y Var", df.columns, key=f"y_var_init",
274+
index = def_ind_y)
275+
st.session_state.default_hue_var = st.selectbox("Default Hue Var", df.columns, key=f"hue_var_init",
276+
index = def_ind_hue)
277+
trend_index = st.session_state.trend_types.index(st.session_state.default_trend_type)
278+
st.session_state.default_trend_type = st.selectbox("Default Trend Line", st.session_state.trend_types,
279+
key=f"trend_type_init", index = trend_index)
280+
281+
# Button to add a new plot
282+
if st.button("Add plot"):
283+
add_plot()
284+
285+
# Add a single plot (initial page includes one plot)
286+
if st.session_state.plots.shape[0] == 0:
254287
add_plot()
255288

256-
# Add a single plot (initial page includes one plot)
257-
if st.session_state.plots.shape[0] == 0:
258-
add_plot()
259-
260-
# Read plot ids
261-
df_p = st.session_state.plots
262-
p_index = df_p.PID.tolist()
263-
plot_per_raw = st.session_state.plot_per_raw
264-
265-
# Render plots
266-
# - iterates over plots;
267-
# - for every "plot_per_raw" plots, creates a new columns block, resets column index, and displays the plot
268-
for i in range(0, len(p_index)):
269-
column_no = i % plot_per_raw
270-
if column_no == 0:
271-
blocks = st.columns(plot_per_raw)
272-
with blocks[column_no]:
273-
display_plot(p_index[i])
274-
275-
276-
# FIXME: this is for debugging for now; will be removed
277-
# with st.expander('Saved DataFrames'):
278-
with st.container():
279-
st.session_state.plots
289+
# Read plot ids
290+
df_p = st.session_state.plots
291+
p_index = df_p.PID.tolist()
292+
plot_per_raw = st.session_state.plot_per_raw
293+
294+
# Render plots
295+
# - iterates over plots;
296+
# - for every "plot_per_raw" plots, creates a new columns block, resets column index, and displays the plot
297+
for i in range(0, len(p_index)):
298+
column_no = i % plot_per_raw
299+
if column_no == 0:
300+
blocks = st.columns(plot_per_raw)
301+
with blocks[column_no]:
302+
display_plot(p_index[i])
303+
304+
305+
# FIXME: this is for debugging for now; will be removed
306+
# with st.expander('Saved DataFrames'):
307+
with st.container():
308+
st.session_state.plots
280309

281310

src/NiChart_Viewer/src/pages/workflow_sMRI.py

Lines changed: 73 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,28 @@
99
import plotly.express as px
1010
from math import ceil
1111
import os
12+
from tempfile import NamedTemporaryFile
1213

13-
def dir_selector(folder_path='.'):
14-
dirnames = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d))]
15-
selected_folder = st.sidebar.selectbox('Select a folder', dirnames)
16-
if selected_folder is None:
17-
return None
18-
return os.path.join(folder_path, selected_folder)
14+
import tkinter as tk
15+
from tkinter import filedialog
1916

20-
st.write('Select folder')
21-
dirname = dir_selector()
17+
def browse_file_folder(is_file, init_dir):
18+
root = tk.Tk()
19+
root.withdraw() # Hide the main window
20+
if is_file == True:
21+
out_path = filedialog.askopenfilename(initialdir = init_dir)
22+
else:
23+
out_path = filedialog.askdirectory(initialdir = init_dir)
2224

25+
print('bb')
26+
print(out_path)
27+
28+
root.destroy()
29+
30+
print('aaa')
31+
print(out_path)
32+
33+
return out_path
2334

2435
st.markdown(
2536
"""
@@ -35,32 +46,69 @@ def dir_selector(folder_path='.'):
3546

3647
with st.container(border=True):
3748

38-
# Input text boxes
49+
# Get path to root folder
50+
dir_root = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd())))
51+
52+
# Default file names
3953
# FIXME: The init values for the input fields are set here to run tests quickly
40-
# they will be removed or replaced in the future
41-
dset_name = st.text_input("Give a name to your dataset", value = 'Study1')
54+
# - they will be replaced in the future with values used to link I/O
55+
# between modules (i.e if DLMUSE pipeline was run, it's out file will be set as input'
56+
default_study_name = 'Study1'
57+
default_roi_name = f'{dir_root}/test/test_input/test2_rois/Study1/Study1_DLMUSE.csv'
58+
default_demog_name = f'{dir_root}/test/test_input/test2_rois/Study1/Study1_Demog.csv'
59+
default_out_name = f'{dir_root}/test/test_output/test2_rois/Study1'
60+
61+
# Dset name: We use this to name all output for a study
62+
dset_name = st.text_input("Give a name to your dataset", value = default_study_name)
4263

43-
# input_rois = st.text_input("Enter the input rois file name:", key = 'input_rois')
44-
# upload_rois = st.file_uploader("Upload the file:", key = 'upload_rois')
64+
# Roi file name (user can enter either using the file browser or type full path)
65+
tmpcol = st.columns((1,8))
66+
fname_rois = default_roi_name
67+
with tmpcol[0]:
68+
if st.button("Select ROI file"):
69+
fname_rois = browse_file_folder(True, dir_root)
70+
with tmpcol[1]:
71+
input_rois = st.text_input("Enter the name of the ROI csv file:", value = fname_rois,
72+
label_visibility="collapsed")
4573

46-
input_rois = st.text_input("path to DLMUSE csv file", value = '/home/gurayerus/GitHub/CBICA/NiChart_Project/test/test_input/test2_rois/Study1/Study1_DLMUSE.csv')
47-
input_demog = st.text_input("path to demographic csv file", value = '/home/gurayerus/GitHub/CBICA/NiChart_Project/test/test_input/test2_rois/Study1/Study1_Demog.csv')
48-
dir_output = st.text_input("path to output folder", value = '/home/gurayerus/GitHub/CBICA/NiChart_Project/test/test_output/test2_rois')
74+
# Demog file name (user can enter either using the file browser or type full path)
75+
tmpcol = st.columns((1,8))
76+
fname_demog = default_demog_name
77+
with tmpcol[0]:
78+
if st.button("Select demog file"):
79+
fname_demog = browse_file_folder(True, dir_root)
80+
with tmpcol[1]:
81+
input_demog = st.text_input("Enter the name of the demog csv file:", value = fname_demog,
82+
label_visibility="collapsed")
83+
84+
# Out folder name (user can enter either using the file browser or type full path)
85+
tmpcol = st.columns((1,8))
86+
dname_out = default_out_name
87+
with tmpcol[0]:
88+
if st.button("Select output folder"):
89+
dname_out = browse_file_folder(False, dir_root)
90+
with tmpcol[1]:
91+
dir_output = st.text_input("Enter the name of the output folder:", value = dname_out,
92+
label_visibility="collapsed")
93+
94+
flag_files = 1
4995

5096
# Check input files
5197
if not os.path.exists(input_rois):
5298
st.warning("Path to input DLMUSE csv doesn't exist")
53-
if not os.path.exists(input_rois):
99+
flag_files = 0
100+
101+
if not os.path.exists(input_demog):
54102
st.warning("Path to input demographic csv doesn't exist")
55-
if not os.path.exists(dir_output):
56-
st.warning("Path to output folder doesn't exist")
103+
flag_files = 0
57104

58105
run_dir='../../workflow/workflows/w_sMRI'
59106

60107
# Run workflow
61-
if st.button("Run w_sMRI"):
62-
st.write("Pipeline is running, please wait!")
63-
os.system(f"cd {run_dir}")
64-
cmd = f"python3 {run_dir}/call_snakefile.py --run_dir {run_dir} --dset_name {dset_name} --input_rois {input_rois} --input_demog {input_demog} --dir_output {dir_output}"
65-
os.system(cmd)
66-
st.write("Run completed!")
108+
if flag_files == 1:
109+
if st.button("Run w_sMRI"):
110+
st.write("Pipeline is running, please wait!")
111+
os.system(f"cd {run_dir}")
112+
cmd = f"python3 {run_dir}/call_snakefile.py --run_dir {run_dir} --dset_name {dset_name} --input_rois {input_rois} --input_demog {input_demog} --dir_output {dir_output}"
113+
os.system(cmd)
114+
st.write("Run completed!")

0 commit comments

Comments
 (0)