Skip to content

Commit

Permalink
Edit the plot and view scripts to initialize the ROI variable in img …
Browse files Browse the repository at this point in the history
…viewer to the yvar in the active plot
  • Loading branch information
gurayerus committed Sep 18, 2024
1 parent 2878d0c commit 2b22989
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/NiChart_Viewer/src/pages/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Dataframe to keep plot ids
st.session_state.plots = pd.DataFrame(columns = ['pid', 'xvar', 'yvar', 'hvar', 'trend'])
st.session_state.plot_index = 1
st.session_state.plot_active = ''

# Path to root folder
st.session_state.path_root = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd())))
Expand Down
3 changes: 2 additions & 1 deletion src/NiChart_Viewer/src/pages/view_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ def prep_images(f_img, f_mask, sel_var_ind, dict_derived):
st.warning(f'Selected {sel_type}: {sel_mrid}')

# Selection of ROI
sel_var = st.session_state.sel_var
# - The variable will be selected from the active plot
sel_var = st.session_state.plots.loc[st.session_state.plot_active, 'yvar']
if sel_var == '':
sel_ind = 0
sel_type = '(auto)'
Expand Down
27 changes: 15 additions & 12 deletions src/NiChart_Viewer/src/pages/view_plot_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def add_plot():
st.session_state.plot_hvar,
st.session_state.plot_trend
]

st.session_state.plot_index += 1

# Remove a plot
Expand All @@ -64,16 +63,20 @@ def remove_plot(plot_id):
df_p = df_p[df_p.PID != plot_id]
st.session_state.plots = df_p


def display_plot(plot_id):
'''
Displays the plot with the plot_id
'''

def callback_plot_clicked():
'''
Set the active plot id to plot that was clicked
'''
st.session_state.plot_active = plot_id

# Create a copy of dataframe for filtered data
df_filt = df.copy()


# Main container for the plot
with st.container(border=True):

Expand Down Expand Up @@ -138,22 +141,19 @@ def display_plot(plot_id):

# Add plot
# - on_select: when clicked it will rerun and return the info
sel_info = st.plotly_chart(scatter_plot, on_select='rerun', key=f"bubble_chart_{plot_id}")

sel_info = st.plotly_chart(scatter_plot, key=f"bubble_chart_{plot_id}",
on_select = callback_plot_clicked)

# Detect MRID from the click info
try:
if len(sel_info['selection']['points'])>0:

sind = sel_info['selection']['point_indices'][0]
lgroup = sel_info['selection']['points'][0]['legendgroup']
mrid = df_filt[df_filt[hue_var] == lgroup].iloc[sind]['MRID']
mrid = df_filt[df_filt[hvar] == lgroup].iloc[sind]['MRID']
st.sidebar.warning('Selected subject: ' + mrid)
st.session_state.sel_mrid = mrid

except:
print('Warning: Could not detect point!')
return

# ## FIXME: this is temp (for debugging the selection of clicked subject)
# st.dataframe(df_filt)

def filter_dataframe(df: pd.DataFrame, plot_id) -> pd.DataFrame:
"""
Expand Down Expand Up @@ -311,6 +311,9 @@ def filter_dataframe(df: pd.DataFrame, plot_id) -> pd.DataFrame:


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

with st.expander('session_state: Plots'):
st.session_state.plots

Expand Down

0 comments on commit 2b22989

Please sign in to comment.