Skip to content

Commit

Permalink
show parameters in plot page & download
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwoer committed Oct 30, 2024
1 parent 2b2ef2e commit 8080336
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 41 deletions.
6 changes: 3 additions & 3 deletions alphastats/gui/pages/04_Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
method = st.selectbox(
"Analysis",
options=["<select>"]
+ ["------- plots -------"]
+ ["------- plots ------------"]
+ plotting_options
+ ["------- statistics -------"]
+ statistic_options,
Expand All @@ -64,15 +64,15 @@
show_plot = analysis_result is not None

elif method in statistic_options:
analysis_result, *_ = gather_parameters_and_do_analysis(
analysis_result, _, parameters = gather_parameters_and_do_analysis(
method,
)
show_df = analysis_result is not None

with c2:
# --- SHOW PLOT -------------------------------------------------------
if show_plot:
display_plot(method, analysis_result)
display_plot(method, analysis_result, parameters)

# --- SHOW STATISTICAL ANALYSIS -------------------------------------------------------
elif show_df:
Expand Down
2 changes: 2 additions & 0 deletions alphastats/gui/pages/06_Results.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@

method = saved_item[0]
plot = saved_item[1]
parameters = saved_item[2]

st.markdown("\n\n\n")
st.markdown(f"#### {method}")
st.write(f"Parameters used for analysis: {parameters}")

if st.button("x remove analysis", key="remove" + method + str(count)):
st.session_state[StateKeys.PLOT_LIST].remove(saved_item)
Expand Down
26 changes: 13 additions & 13 deletions alphastats/gui/utils/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def do_analysis(self):
if not self._works_with_nans and self._dataset.mat.isnull().values.any():
st.error("This analysis does not work with NaN values.")
st.stop()
return self._do_analysis()
return *self._do_analysis(), dict(self._parameters)

@abstractmethod
def _do_analysis(self):
Expand Down Expand Up @@ -173,7 +173,7 @@ def _do_analysis(self):
method=self._parameters["method"],
group=self._parameters["group"],
)
return intensity_plot, None, self._parameters
return intensity_plot, None


class SampleDistributionPlot(AbstractIntensityPlot, ABC):
Expand All @@ -185,7 +185,7 @@ def _do_analysis(self):
method=self._parameters["method"],
color=self._parameters["group"], # no typo
)
return intensity_plot, None, self._parameters
return intensity_plot, None


class PCAPlotAnalysis(AbstractDimensionReductionAnalysis):
Expand All @@ -198,7 +198,7 @@ def _do_analysis(self):
group=self._parameters["group"],
circle=self._parameters["circle"],
)
return pca_plot, None, self._parameters
return pca_plot, None


class UMAPPlotAnalysis(AbstractDimensionReductionAnalysis):
Expand All @@ -210,7 +210,7 @@ def _do_analysis(self):
group=self._parameters["group"],
circle=self._parameters["circle"],
)
return umap_plot, None, self._parameters
return umap_plot, None


class TSNEPlotAnalysis(AbstractDimensionReductionAnalysis):
Expand Down Expand Up @@ -242,7 +242,7 @@ def _do_analysis(self):
perplexity=self._parameters["perplexity"],
n_iter=self._parameters["n_iter"],
)
return tsne_plot, None, self._parameters
return tsne_plot, None


class VolcanoPlotAnalysis(AbstractGroupCompareAnalysis):
Expand Down Expand Up @@ -308,7 +308,7 @@ def _do_analysis(self):
fdr=self._parameters["fdr"],
color_list=self._parameters["color_list"],
)
return volcano_plot.plot, volcano_plot, self._parameters
return volcano_plot.plot, volcano_plot


class ClustermapAnalysis(AbstractAnalysis):
Expand All @@ -319,7 +319,7 @@ class ClustermapAnalysis(AbstractAnalysis):
def _do_analysis(self):
"""Draw Clustermap using the Clustermap class."""
clustermap = self._dataset.plot_clustermap()
return clustermap, None, self._parameters
return clustermap, None


class DendrogramAnalysis(AbstractAnalysis):
Expand All @@ -330,7 +330,7 @@ class DendrogramAnalysis(AbstractAnalysis):
def _do_analysis(self):
"""Draw Clustermap using the Clustermap class."""
dendrogram = self._dataset.plot_dendrogram()
return dendrogram, None, self._parameters
return dendrogram, None


class DifferentialExpressionAnalysis(AbstractGroupCompareAnalysis):
Expand Down Expand Up @@ -359,7 +359,7 @@ def _do_analysis(self):
group2=self._parameters["group2"],
column=self._parameters["column"],
)
return diff_exp_analysis, None, self._parameters
return diff_exp_analysis, None


class TukeyTestAnalysis(AbstractAnalysis):
Expand All @@ -384,7 +384,7 @@ def _do_analysis(self):
protein_id=self._parameters["protein_id"],
group=self._parameters["group"],
)
return tukey_test_analysis, None, self._parameters
return tukey_test_analysis, None


class AnovaAnalysis(AbstractGroupCompareAnalysis):
Expand Down Expand Up @@ -415,7 +415,7 @@ def _do_analysis(self):
protein_ids=self._parameters["protein_ids"],
tukey=self._parameters["tukey"],
)
return anova_analysis, None, self._parameters
return anova_analysis, None


class AncovaAnalysis(AbstractAnalysis):
Expand Down Expand Up @@ -448,7 +448,7 @@ def _do_analysis(self):
covar=self._parameters["covar"],
between=self._parameters["between"],
)
return ancova_analysis, None, self._parameters
return ancova_analysis, None


ANALYSIS_OPTIONS = {
Expand Down
59 changes: 34 additions & 25 deletions alphastats/gui/utils/analysis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,35 @@ def display_figure(plot):
st.pyplot(plot)


def save_plot_to_session_state(method, plot):
"""
save plot with method to session state to retrieve old results
"""
st.session_state[StateKeys.PLOT_LIST] += [(method, plot)]


def display_df(df):
mask = df.applymap(type) != bool # noqa: E721
d = {True: "TRUE", False: "FALSE"}
df = df.where(mask, df.replace(d))
st.dataframe(df)


@st.fragment
def display_plot(method, analysis_result, show_save_button=True) -> None:
def display_plot(
method: str,
analysis_result: Any,
parameters: Optional[Dict] = None,
show_save_button=True,
) -> None:
"""A fragment to display the plot and download options."""
display_figure(analysis_result)

c1, c2, c3 = st.columns([1, 1, 1])

with c1:
if show_save_button and st.button("Save to results page.."):
save_plot_to_session_state(method, analysis_result)
save_plot_to_session_state(method, analysis_result, parameters)

with c2:
download_figure(method, analysis_result, format="pdf")
download_figure(method, analysis_result, format="svg")

with c3:
download_preprocessing_info(
method, analysis_result
) # TODO this should go elsewhere
download_analysis_and_preprocessing_info(method, analysis_result, parameters)


def display_df(df):
mask = df.applymap(type) != bool # noqa: E721
d = {True: "TRUE", False: "FALSE"}
df = df.where(mask, df.replace(d))
st.dataframe(df)


def download_figure(method, plot, format):
Expand All @@ -70,22 +66,35 @@ def download_figure(method, plot, format):
st.download_button(label="Download as " + format, data=buffer, file_name=filename)


def download_preprocessing_info(method, plot):
preprocesing_dict = plot.preprocessing
df = pd.DataFrame(preprocesing_dict.items())
filename = "plot" + method + "preprocessing_info.csv"
def download_analysis_and_preprocessing_info(method, plot, parameters):
parameters_pretty = {f"analysis_parameter__{k}": v for k, v in parameters.items()}
dict_to_save = {
**plot.preprocessing,
**parameters_pretty,
} # TODO why is the preprocessing info saved in the plots?

df = pd.DataFrame(dict_to_save.items())

filename = "plot" + method + "analysis_info.csv"
csv = convert_df(df)
st.download_button(
"Download DataSet Info as .csv",
"Download Analysis info as .csv",
csv,
filename,
"text/csv",
)


def save_plot_to_session_state(method, plot, parameters):
"""
save plot with method to session state to retrieve old results
"""
st.session_state[StateKeys.PLOT_LIST] += [(method, plot, parameters)]


def gather_parameters_and_do_analysis(
analysis_name: str,
) -> Tuple[Optional[Any], Optional[Any], Dict[str, Any]]:
) -> Tuple[Optional[Any], Optional[Any], Optional[Dict[str, Any]]]:
"""Extract plotting options and display.
Returns a tuple(figure, analysis_object, parameters) where figure is the plot,
Expand Down

0 comments on commit 8080336

Please sign in to comment.