Skip to content

Commit

Permalink
Merge pull request #352 from MannLabs/simplify_analysis-III
Browse files Browse the repository at this point in the history
simplify_analysis-III
  • Loading branch information
mschwoer authored Nov 8, 2024
2 parents 0b26018 + 57a8b83 commit 0c822e8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 249 deletions.
138 changes: 40 additions & 98 deletions alphastats/gui/pages/04_Analysis.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
import streamlit as st
from gui.utils.options import get_plotting_options, get_statistic_options

from alphastats.gui.utils.analysis_helper import (
display_df,
display_figure,
download_figure,
download_preprocessing_info,
get_analysis,
save_plot_to_session_state,
display_plot,
do_analysis,
)
from alphastats.gui.utils.options import get_plotting_options, get_statistic_options
from alphastats.gui.utils.ui_helper import (
StateKeys,
convert_df,
init_session_state,
sidebar_info,
)


def select_analysis():
"""
select box
loads keys from option dicts
"""
method = st.selectbox(
"Analysis",
options=list(get_plotting_options(st.session_state).keys())
+ list(get_statistic_options(st.session_state).keys()),
)
return method


init_session_state()
sidebar_info()

Expand All @@ -50,90 +33,49 @@ def select_analysis():
st.markdown(styl, unsafe_allow_html=True)


if StateKeys.PLOT_LIST not in st.session_state:
st.session_state[StateKeys.PLOT_LIST] = []


if StateKeys.DATASET in st.session_state:
c1, c2 = st.columns((1, 2))

plot_to_display = False
df_to_display = False
method_plot = None

with c1:
method = select_analysis()

if method in (plotting_options := get_plotting_options(st.session_state)):
analysis_result = get_analysis(method=method, options_dict=plotting_options)
plot_to_display = True

elif method in (statistic_options := get_statistic_options(st.session_state)):
analysis_result = get_analysis(
method=method,
options_dict=statistic_options,
)
df_to_display = True

st.markdown("")
st.markdown("")
st.markdown("")
st.markdown("")

with c2:
# --- Plot -------------------------------------------------------

if analysis_result is not None and method != "Clustermap" and plot_to_display:
display_figure(analysis_result)

save_plot_to_session_state(analysis_result, method)

method_plot = [method, analysis_result]

elif method == "Clustermap":
st.write("Download Figure to see full size.")

display_figure(analysis_result)

save_plot_to_session_state(analysis_result, method)

# --- STATISTICAL ANALYSIS -------------------------------------------------------

elif analysis_result is not None and df_to_display:
display_df(analysis_result)

filename = method + ".csv"
csv = convert_df(analysis_result)

if analysis_result is not None and method != "Clustermap" and plot_to_display:
col1, col2, col3 = st.columns([1, 1, 1])

with col1:
download_figure(method_plot, format="pdf")
if StateKeys.DATASET not in st.session_state:
st.info("Import Data first")
st.stop()

with col2:
download_figure(method_plot, format="svg")
# --- SELECTION -------------------------------------------------------
show_plot = False
show_df = False
analysis_result = None

with col3:
download_preprocessing_info(method_plot)
c1, *_ = st.columns([1, 1, 1])
with c1:
method = st.selectbox(
"Analysis",
options=["<select>"]
+ ["------- plots -------"]
+ list(get_plotting_options(st.session_state).keys())
+ ["------- statistics -------"]
+ list(get_statistic_options(st.session_state).keys()),
)

elif analysis_result is not None and df_to_display and method_plot:
col1, col2, col3 = st.columns([1, 1, 1])
if method in (plotting_options := get_plotting_options(st.session_state)):
analysis_result = do_analysis(method, options_dict=plotting_options)
show_plot = analysis_result is not None

with col1:
download_figure(method_plot, format="pdf", plotting_library="seaborn")
elif method in (statistic_options := get_statistic_options(st.session_state)):
analysis_result = do_analysis(
method,
options_dict=statistic_options,
)
show_df = analysis_result is not None

with col2:
download_figure(method_plot, format="svg", plotting_library="seaborn")

with col3:
download_preprocessing_info(method_plot)
# --- SHOW PLOT -------------------------------------------------------
if show_plot:
display_plot(method, analysis_result)

elif analysis_result is not None and df_to_display:
st.download_button(
"Download as .csv", csv, filename, "text/csv", key="download-csv"
)
# --- SHOW STATISTICAL ANALYSIS -------------------------------------------------------
elif show_df:
display_df(analysis_result)

csv = convert_df(analysis_result)

else:
st.info("Import Data first")
# TODO do we want to save statistical analysis to results page as well?
st.download_button(
"Download as .csv", csv, method + ".csv", "text/csv", key="download-csv"
)
2 changes: 1 addition & 1 deletion alphastats/gui/pages/05_LLM.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def select_analysis():
st.stop()
print("genes_of_interest", genes_of_interest_colored)

save_plot_to_session_state(volcano_plot, method)
save_plot_to_session_state(method, volcano_plot)
st.session_state[StateKeys.GENES_OF_INTEREST_COLORED] = genes_of_interest_colored
# st.session_state["gene_functions"] = get_info(genes_of_interest_colored, organism)
st.session_state[StateKeys.UPREGULATED] = [
Expand Down
70 changes: 14 additions & 56 deletions alphastats/gui/pages/06_Results.py
Original file line number Diff line number Diff line change
@@ -1,74 +1,32 @@
import io

import pandas as pd
import streamlit as st

from alphastats.gui.utils.analysis_helper import display_plot
from alphastats.gui.utils.ui_helper import (
StateKeys,
convert_df,
init_session_state,
sidebar_info,
)


def display_plotly_figure(plot):
st.plotly_chart(plot)


def save_plotly(plot, name, format):
# Create an in-memory buffer
buffer = io.BytesIO()
# Save the figure as a pdf to the buffer
plot.write_image(file=buffer, format=format)
st.download_button(
label="Download as " + format, data=buffer, file_name=name + "." + format
)


def download_preprocessing_info(plot, name, count):
preprocesing_dict = plot.preprocessing
df = pd.DataFrame(preprocesing_dict.items())
filename = "plot" + name + "preprocessing_info.csv"
csv = convert_df(df)
print("preprocessing" + count)
st.download_button(
"Download DataSet Info as .csv",
csv,
filename,
"text/csv",
key="preprocessing" + count,
)


init_session_state()
sidebar_info()

st.markdown("### Results")

if not st.session_state[StateKeys.PLOT_LIST]:
st.info("No analysis saved yet.")
st.stop()

if StateKeys.PLOT_LIST in st.session_state:
for count, plot in enumerate(st.session_state[StateKeys.PLOT_LIST]):
print("plot", type(plot), count)
count = str(count)

st.markdown("\n\n")
name = plot[0]
plot = plot[1]
if name == "ttest":
plot = plot.plot
st.write(name)

display_plotly_figure(plot)
for count, saved_item in enumerate(st.session_state[StateKeys.PLOT_LIST]):
print("plot", type(saved_item), count)

col1, col2, col3 = st.columns([1, 1, 1])
method = saved_item[0]
plot = saved_item[1]

with col1:
save_plotly(plot, name + count, format="pdf")
st.markdown("\n\n\n")
st.markdown(f"#### {method}")

with col2:
save_plotly(plot, name + count, format="svg")
if st.button("x remove analysis", key="remove" + method + str(count)):
st.session_state[StateKeys.PLOT_LIST].remove(saved_item)
st.rerun()

with col3:
download_preprocessing_info(plot, name, count)
else:
st.info("No analysis performed yet.")
display_plot(method + str(count), plot, show_save_button=False)
Loading

0 comments on commit 0c822e8

Please sign in to comment.