From a17c5a3b2b416483227297a484f178c7d6665af5 Mon Sep 17 00:00:00 2001 From: Maximilian Strauss Date: Fri, 11 Mar 2022 15:17:16 -0800 Subject: [PATCH] FIX --- alphapept/gui/history.py | 85 ++++++++++++++++++++++++++++++++++++---- alphapept/gui/utils.py | 22 ++++++++--- 2 files changed, 95 insertions(+), 12 deletions(-) diff --git a/alphapept/gui/history.py b/alphapept/gui/history.py index 28d6c96e..598e9133 100644 --- a/alphapept/gui/history.py +++ b/alphapept/gui/history.py @@ -45,12 +45,12 @@ def create_single_plot( if plot == "timing (min)": try: vals[idx] = all_results[_]["summary"]["timing"]["total (min)"] - except KeyError: + except (KeyError, ValueError): vals[idx] = np.nan else: try: vals[idx] = all_results[_]["summary"][files[idx]][plot] - except KeyError: + except (KeyError, ValueError): vals[idx] = np.nan plot_df = pd.DataFrame([files, acquisition_date_times, vals]).T @@ -129,13 +129,38 @@ def create_multiple_plots(all_results: dict, groups: list, to_plot: list): all_results, files, acquisition_date_times, mode, groups, plot, minimum_date, ) +@st.cache +def filter_for_single_file(results): + + results_list = [] + for _ in results.keys(): + + if len(results[_]['experiment']['shortnames']) == 1: + try: + x = results[_]['summary'][results[_]['experiment']['shortnames'][0]] + x['timing (min)'] = results[_]['summary']['timing']['total (min)'] + x['filename'] = results[_]['experiment']['shortnames'][0] + + results_list.append(x) + except Exception as e: + pass + + result_df = pd.DataFrame(results_list) + + return result_df + +@st.cache +def convert_df(df): + return df.to_csv().encode('utf-8') + def history(): """Streamlit page to plot a historical overview of previous results.""" st.write("# History") st.text( - f"History allows to visualize summary information from multiple previous analysis." + f"History allows to display summary information from multiple previous analysis." f"\nIt checks {PROCESSED_PATH} for *.yaml files." "\nFiles can be filtered to only include a subset." + "\nOnly experiments with a single file will be displayed." ) processed_files = files_in_folder(PROCESSED_PATH, ".yaml") @@ -164,8 +189,54 @@ def history(): ) st.write(history_settings) - filtered = filter_by_tag(processed_files) - all_results = load_files(filtered, callback=st.progress(0)) - if len(all_results) > 0: - create_multiple_plots(all_results, groups, to_plot) + all_results = load_files(processed_files, callback=st.progress(0)) + + filtered = filter_by_tag(all_results) + + if len(filtered) > 0: + mode = st.selectbox('Select Mode', ['Graph', 'Table']) + if mode == 'Table': + st.table(filtered.style.bar(color='lightgreen').format(precision=3)) + csv = convert_df(filtered) + st.download_button( + f"Click to download as (csv)", + csv, + "file.csv", + "text/csv", + key='download-csv' + ) + else: + plot_df = filtered.copy() + c1, c2 = st.columns(2) + x = c1.selectbox('X axis', filtered.columns) + y = c2.selectbox('Y axis', [_ for _ in filtered.columns if _ is not x]) + + if groups != []: + plot_df["group"] = plot_df["file_in_experiment"].apply(lambda x: check_group(x, groups)) + else: + plot_df["group"] = "None" + + try: + median_ = plot_df[y].median() + plot_df = plot_df.sort_values(x) + + if mode == "Filename": + height = 800 + else: + height = 400 + + fig = px.scatter( + plot_df, + x=x, + y=y, + color="group", + hover_name="file_in_experiment", + hover_data=["acquisition_date_time"], + title=f"{x} - median {median_:.2f}", + height=height, + ).update_traces(mode="lines+markers") + fig.add_hline(y=median_, line_dash="dash") + st.plotly_chart(fig) + except Exception as e: + st.warning(e) diff --git a/alphapept/gui/utils.py b/alphapept/gui/utils.py index 610de43a..67485eaf 100644 --- a/alphapept/gui/utils.py +++ b/alphapept/gui/utils.py @@ -35,16 +35,25 @@ def load_files(file_list: list, callback: Union[Callable, None] = None) -> dict: Returns: dict: A dictionary containing the summary of all results.yaml files. """ - all_results = {} + all_results = [] for idx, _ in enumerate(file_list): base, ext = os.path.splitext(_) - all_results[base] = load_file(_) + results = load_file(_) + + f = results['summary']['processed_files'] + + for f_ in f: + f__ = os.path.splitext(f_)[0] + res_ = results['summary'][f__] + res_['file_in_experiment'] = f__ + res_['experiment'] = base + all_results.append(res_) if callback: callback.progress((idx + 1) / len(file_list)) - return all_results + return pd.DataFrame(all_results) def filter_by_tag(files: list) -> list: @@ -60,9 +69,12 @@ def filter_by_tag(files: list) -> list: multi = filter.split('&') multi = [_.replace(' ', '') for _ in multi] st.write(f"Searching for files containing {multi}") + #files['keep'] = False + #for tag in multi: + # files['keep'].apply.lambda(x) + + #filtered = files[files['file_in_experiment'].str.contains(multi)] filtered = files - for tag in multi: - filtered = [_ for _ in filtered if tag in _] else: filtered = files