Skip to content

Commit

Permalink
Edit
Browse files Browse the repository at this point in the history
  • Loading branch information
gurayerus committed Sep 13, 2024
1 parent a1c34e9 commit dbdf31d
Show file tree
Hide file tree
Showing 5 changed files with 1,035 additions and 91 deletions.
161 changes: 95 additions & 66 deletions src/NiChart_Viewer/src/pages/view_plot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
from streamlit_plotly_events import plotly_events
from utils_trace import *
from st_pages import hide_pages
import os
import tkinter as tk
from tkinter import filedialog


def browse_file_folder(is_file, init_dir):
root = tk.Tk()
# root.withdraw() # Hide the main window
if is_file == True:
out_path = filedialog.askopenfilenames(initialdir = init_dir, multiple=0)
else:
out_path = filedialog.askdirectory(initialdir = init_dir)

print('aaaaaa')
print(out_path[0:5])
input()

return out_path

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

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

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

# Page controls in side bar
with st.sidebar:

with st.container(border=True):

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

with st.container(border=True):
if os.path.exists(spare_csv):
df = pd.read_csv(spare_csv)

with st.sidebar:
with st.container(border=True):

# Slider to set number of plots in a row
st.session_state.plot_per_raw = st.slider('Plots per raw',1, 5, 3, key='a_per_page')

with st.container(border=True):

st.write('Plot Settings')

# Tabs for parameters
ptabs = st.tabs([":lock:", ":large_orange_circle:", ":large_yellow_circle:",
":large_green_circle:"])

st.write('Plot Settings')

# Tabs for parameters
ptabs = st.tabs([":lock:", ":large_orange_circle:", ":large_yellow_circle:",
":large_green_circle:"])

# Tab 0: to set plotting parameters
with ptabs[1]:
# Default values for plot params
st.session_state.default_hue_var = 'Sex'

def_ind_x = 0
if st.session_state.default_x_var in df.columns:
def_ind_x = df.columns.get_loc(st.session_state.default_x_var)

def_ind_y = 0
if st.session_state.default_y_var in df.columns:
def_ind_y = df.columns.get_loc(st.session_state.default_y_var)

def_ind_hue = 0
if st.session_state.default_hue_var in df.columns:
def_ind_hue = df.columns.get_loc(st.session_state.default_hue_var)

st.session_state.default_x_var = st.selectbox("Default X Var", df.columns, key=f"x_var_init",
index = def_ind_x)
st.session_state.default_y_var = st.selectbox("Default Y Var", df.columns, key=f"y_var_init",
index = def_ind_y)
st.session_state.default_hue_var = st.selectbox("Default Hue Var", df.columns, key=f"hue_var_init",
index = def_ind_hue)
trend_index = st.session_state.trend_types.index(st.session_state.default_trend_type)
st.session_state.default_trend_type = st.selectbox("Default Trend Line", st.session_state.trend_types,
key=f"trend_type_init", index = trend_index)

# Button to add a new plot
if st.button("Add plot"):
# Tab 0: to set plotting parameters
with ptabs[1]:
# Default values for plot params
st.session_state.default_hue_var = 'Sex'

def_ind_x = 0
if st.session_state.default_x_var in df.columns:
def_ind_x = df.columns.get_loc(st.session_state.default_x_var)

def_ind_y = 0
if st.session_state.default_y_var in df.columns:
def_ind_y = df.columns.get_loc(st.session_state.default_y_var)

def_ind_hue = 0
if st.session_state.default_hue_var in df.columns:
def_ind_hue = df.columns.get_loc(st.session_state.default_hue_var)

st.session_state.default_x_var = st.selectbox("Default X Var", df.columns, key=f"x_var_init",
index = def_ind_x)
st.session_state.default_y_var = st.selectbox("Default Y Var", df.columns, key=f"y_var_init",
index = def_ind_y)
st.session_state.default_hue_var = st.selectbox("Default Hue Var", df.columns, key=f"hue_var_init",
index = def_ind_hue)
trend_index = st.session_state.trend_types.index(st.session_state.default_trend_type)
st.session_state.default_trend_type = st.selectbox("Default Trend Line", st.session_state.trend_types,
key=f"trend_type_init", index = trend_index)

# Button to add a new plot
if st.button("Add plot"):
add_plot()

# Add a single plot (initial page includes one plot)
if st.session_state.plots.shape[0] == 0:
add_plot()

# Add a single plot (initial page includes one plot)
if st.session_state.plots.shape[0] == 0:
add_plot()

# Read plot ids
df_p = st.session_state.plots
p_index = df_p.PID.tolist()
plot_per_raw = st.session_state.plot_per_raw

# Render plots
# - iterates over plots;
# - for every "plot_per_raw" plots, creates a new columns block, resets column index, and displays the plot
for i in range(0, len(p_index)):
column_no = i % plot_per_raw
if column_no == 0:
blocks = st.columns(plot_per_raw)
with blocks[column_no]:
display_plot(p_index[i])


# FIXME: this is for debugging for now; will be removed
# with st.expander('Saved DataFrames'):
with st.container():
st.session_state.plots
# Read plot ids
df_p = st.session_state.plots
p_index = df_p.PID.tolist()
plot_per_raw = st.session_state.plot_per_raw

# Render plots
# - iterates over plots;
# - for every "plot_per_raw" plots, creates a new columns block, resets column index, and displays the plot
for i in range(0, len(p_index)):
column_no = i % plot_per_raw
if column_no == 0:
blocks = st.columns(plot_per_raw)
with blocks[column_no]:
display_plot(p_index[i])


# FIXME: this is for debugging for now; will be removed
# with st.expander('Saved DataFrames'):
with st.container():
st.session_state.plots


98 changes: 73 additions & 25 deletions src/NiChart_Viewer/src/pages/workflow_sMRI.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,28 @@
import plotly.express as px
from math import ceil
import os
from tempfile import NamedTemporaryFile

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

st.write('Select folder')
dirname = dir_selector()
def browse_file_folder(is_file, init_dir):
root = tk.Tk()
root.withdraw() # Hide the main window
if is_file == True:
out_path = filedialog.askopenfilename(initialdir = init_dir)
else:
out_path = filedialog.askdirectory(initialdir = init_dir)

print('bb')
print(out_path)

root.destroy()

print('aaa')
print(out_path)

return out_path

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

with st.container(border=True):

# Input text boxes
# Get path to root folder
dir_root = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd())))

# Default file names
# FIXME: The init values for the input fields are set here to run tests quickly
# they will be removed or replaced in the future
dset_name = st.text_input("Give a name to your dataset", value = 'Study1')
# - they will be replaced in the future with values used to link I/O
# between modules (i.e if DLMUSE pipeline was run, it's out file will be set as input'
default_study_name = 'Study1'
default_roi_name = f'{dir_root}/test/test_input/test2_rois/Study1/Study1_DLMUSE.csv'
default_demog_name = f'{dir_root}/test/test_input/test2_rois/Study1/Study1_Demog.csv'
default_out_name = f'{dir_root}/test/test_output/test2_rois/Study1'

# Dset name: We use this to name all output for a study
dset_name = st.text_input("Give a name to your dataset", value = default_study_name)

# input_rois = st.text_input("Enter the input rois file name:", key = 'input_rois')
# upload_rois = st.file_uploader("Upload the file:", key = 'upload_rois')
# Roi file name (user can enter either using the file browser or type full path)
tmpcol = st.columns((1,8))
fname_rois = default_roi_name
with tmpcol[0]:
if st.button("Select ROI file"):
fname_rois = browse_file_folder(True, dir_root)
with tmpcol[1]:
input_rois = st.text_input("Enter the name of the ROI csv file:", value = fname_rois,
label_visibility="collapsed")

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')
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')
dir_output = st.text_input("path to output folder", value = '/home/gurayerus/GitHub/CBICA/NiChart_Project/test/test_output/test2_rois')
# Demog file name (user can enter either using the file browser or type full path)
tmpcol = st.columns((1,8))
fname_demog = default_demog_name
with tmpcol[0]:
if st.button("Select demog file"):
fname_demog = browse_file_folder(True, dir_root)
with tmpcol[1]:
input_demog = st.text_input("Enter the name of the demog csv file:", value = fname_demog,
label_visibility="collapsed")

# Out folder name (user can enter either using the file browser or type full path)
tmpcol = st.columns((1,8))
dname_out = default_out_name
with tmpcol[0]:
if st.button("Select output folder"):
dname_out = browse_file_folder(False, dir_root)
with tmpcol[1]:
dir_output = st.text_input("Enter the name of the output folder:", value = dname_out,
label_visibility="collapsed")

flag_files = 1

# Check input files
if not os.path.exists(input_rois):
st.warning("Path to input DLMUSE csv doesn't exist")
if not os.path.exists(input_rois):
flag_files = 0

if not os.path.exists(input_demog):
st.warning("Path to input demographic csv doesn't exist")
if not os.path.exists(dir_output):
st.warning("Path to output folder doesn't exist")
flag_files = 0

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

# Run workflow
if st.button("Run w_sMRI"):
st.write("Pipeline is running, please wait!")
os.system(f"cd {run_dir}")
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}"
os.system(cmd)
st.write("Run completed!")
if flag_files == 1:
if st.button("Run w_sMRI"):
st.write("Pipeline is running, please wait!")
os.system(f"cd {run_dir}")
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}"
os.system(cmd)
st.write("Run completed!")
Loading

0 comments on commit dbdf31d

Please sign in to comment.