Skip to content

Commit

Permalink
Keep input file names in session state and let the pages edit them to…
Browse files Browse the repository at this point in the history
… link I/O
  • Loading branch information
gurayerus committed Sep 13, 2024
1 parent d2b37ac commit 05f9d78
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 69 deletions.
41 changes: 41 additions & 0 deletions src/NiChart_Viewer/src/pages/home.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,48 @@
import streamlit as st
from PIL import Image
import pandas as pd
import os

# Initiate Session State Values
if 'instantiated' not in st.session_state:

# Dataframe to keep plot ids
st.session_state.plots = pd.DataFrame({'PID':[]})
st.session_state.pid = 1

# Path to root folder
st.session_state.dir_root = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd())))

# Path to folder used as the init folder in file selection
st.session_state.init_dir = st.session_state.dir_root

# Default values for plotting parameters
st.session_state.default_x_var = 'Age'
st.session_state.default_y_var = 'GM'
st.session_state.default_hue_var = 'Sex'
st.session_state.trend_types = ['none', 'ols', 'lowess']
st.session_state.default_trend_type = 'none'

# ID selected by user
st.session_state.sel_id = ''

# Input fields for w_sMRI
st.session_state.study_name = 'MyStudy'
st.session_state.in_csv_MUSE = ''
st.session_state.in_csv_Demog = ''

# # FIXME: set default values for easy run
# st.session_state.in_csv_MUSE = f'{st.session_state.dir_root}/test/test_input/test2_rois/Study1/Study1_DLMUSE.csv'
# st.session_state.in_csv_Demog = f'{st.session_state.dir_root}/test/test_input/test2_rois/Study1/Study1_Demog.csv'


# Path to out folder
st.session_state.out_dir = ''

# Input fields for plotting
st.session_state.in_csv_sMRI = ''

st.session_state.instantiated = True

st.sidebar.image("../resources/nichart1.png")

Expand Down
45 changes: 11 additions & 34 deletions src/NiChart_Viewer/src/pages/view_plot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,6 @@ def browse_file_folder(is_file, init_dir):

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


# Initiate Session State Values
if 'instantiated' not in st.session_state:

# Dataframe to keep plot ids
st.session_state.plots = pd.DataFrame({'PID':[]})
st.session_state.pid = 1

# Default values for plotting parameters
st.session_state.default_x_var = 'Age'
st.session_state.default_y_var = 'GM'
st.session_state.default_hue_var = 'Sex'
st.session_state.trend_types = ['none', 'ols', 'lowess']
st.session_state.default_trend_type = 'none'

# ID selected by user (default: empty)
st.session_state.sel_id = ''

st.session_state.instantiated = True

def add_plot():
'''
Adds a new plot (updates a dataframe with plot ids)
Expand Down Expand Up @@ -142,8 +122,6 @@ def display_plot(pid):
# ## FIXME: this is temp (for debugging the selection of clicked subject)
# st.dataframe(df_filt)



def filter_dataframe(df: pd.DataFrame, pid) -> pd.DataFrame:
"""
Adds a UI on top of a dataframe to let viewers filter columns
Expand Down Expand Up @@ -220,20 +198,19 @@ def filter_dataframe(df: pd.DataFrame, pid) -> pd.DataFrame:

# Page controls in side bar
with st.sidebar:
with st.container(border=True):

# Input file name (check if saved to session_state from a previous module)
if "viewer_in_csv" in st.session_state:
fname_spare = st.session_state.viewer_in_csv
with st.container(border=True):

# Input file name (user can enter either using the file browser or type full path)
# Input file name
if st.sidebar.button("Select input file"):
fname_spare = browse_file_folder(True, dir_root)
st.session_state.in_csv_sMRI = browse_file_folder(True, dir_root)
spare_csv = st.sidebar.text_input("Enter the name of the ROI csv file:",
value = fname_spare,
value = st.session_state.in_csv_sMRI,
label_visibility="collapsed")

if os.path.exists(spare_csv):

# Read input csv
df = pd.read_csv(spare_csv)

with st.sidebar:
Expand Down Expand Up @@ -281,7 +258,7 @@ def filter_dataframe(df: pd.DataFrame, pid) -> pd.DataFrame:
if st.button("Add plot"):
add_plot()

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

Expand All @@ -301,9 +278,9 @@ def filter_dataframe(df: pd.DataFrame, pid) -> pd.DataFrame:
display_plot(p_index[i])


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


with st.expander('session_state: All'):
st.write(st.session_state)
78 changes: 43 additions & 35 deletions src/NiChart_Viewer/src/pages/workflow_sMRI.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,26 @@
import tkinter as tk
from tkinter import filedialog

def browse_file_folder(is_file, init_dir):
def browse_file(init_dir):
'''
File selector
Returns the file name selected by the user and the parent folder
'''
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)
out_path = filedialog.askopenfilename(initialdir = init_dir)
out_dir = os.path.dirname(out_path)
root.destroy()
return out_path, out_dir

def browse_folder(init_dir):
'''
Folder selector
Returns the folder name selected by the user
'''
root = tk.Tk()
root.withdraw() # Hide the main window
out_path = filedialog.askdirectory(initialdir = init_dir)
root.destroy()
return out_path

Expand All @@ -38,54 +51,43 @@ def browse_file_folder(is_file, init_dir):

with st.container(border=True):

# 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 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)
# Dataset name: Used to create a main folder for all outputs
dset_name = st.text_input("Give a name to your dataset", value = st.session_state.study_name)
st.session_state.study_name = dset_name

# Roi file name (user can enter either using the file browser or type full path)
# Roi file name
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)
st.session_state.in_csv_MUSE, st.session_state.init_dir = browse_file(st.session_state.init_dir)
with tmpcol[1]:
input_rois = st.text_input("Enter the name of the ROI csv file:", value = fname_rois,
input_rois = st.text_input("Enter the name of the ROI csv file:",
value = st.session_state.in_csv_MUSE,
label_visibility="collapsed")

# Demog file name (user can enter either using the file browser or type full path)
# Demog file name
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)
st.session_state.in_csv_Demog, st.session_state.init_dir = browse_file(st.session_state.init_dir)
with tmpcol[1]:
input_demog = st.text_input("Enter the name of the demog csv file:", value = fname_demog,
label_visibility="collapsed")
input_demog = st.text_input("Enter the name of the demog csv file:",
value = st.session_state.in_csv_Demog,
label_visibility="collapsed")

# Out folder name (user can enter either using the file browser or type full path)
# Out folder name
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)
st.session_state.out_dir = browse_folder(st.session_state.dir_root)
with tmpcol[1]:
dir_output = st.text_input("Enter the name of the output folder:", value = dname_out,
dir_output = st.text_input("Enter the name of the output folder:",
value = st.session_state.out_dir,
label_visibility="collapsed")

flag_files = 1

# Check input files
flag_files = 1
if not os.path.exists(input_rois):
st.warning("Path to input DLMUSE csv doesn't exist")
flag_files = 0
Expand All @@ -105,9 +107,15 @@ def browse_file_folder(is_file, init_dir):
os.system(cmd)
st.write("Run completed!")

# Set output as the input file of the viewer
# Set the output file as the input for the related viewers
out_csv = f"{dir_output}/out_combined/{dset_name}_All.csv"
if os.path.exists(out_csv):
st.session_state.viewer_in_csv = out_csv
st.session_state.in_csv_sMRI = out_csv

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

with st.expander('session_state: All'):
st.write(st.session_state)

0 comments on commit 05f9d78

Please sign in to comment.