Skip to content

Commit

Permalink
inline file uploader
Browse files Browse the repository at this point in the history
  • Loading branch information
mschwoer committed Sep 9, 2024
1 parent 0630b2c commit 86c69df
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 48 deletions.
32 changes: 23 additions & 9 deletions alphastats/gui/pages/02_Import Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
from alphastats.gui.utils.import_helper import (
load_example_data,
empty_session_state,
load_softwarefile_df,
show_metadata_file_uploader,
uploaded_file_to_df,
show_loader_columns_selection,
load_proteomics_data,
load_options,
show_select_sample_column_for_metadata,
init_session_state,
show_button_download_metadata_template_file,
)
from alphastats.gui.utils.ui_helper import sidebar_info

Expand Down Expand Up @@ -80,7 +80,7 @@
if softwarefile is None:
st.stop()

softwarefile_df = load_softwarefile_df(software, softwarefile)
softwarefile_df = uploaded_file_to_df(softwarefile, software)

intensity_column, index_column = show_loader_columns_selection(
software=software, softwarefile_df=softwarefile_df
Expand All @@ -96,12 +96,26 @@

# ########## Load Metadata File
st.markdown("##### 3. Prepare Metadata (optional)")
sample_column = None
metadatafile_df = show_metadata_file_uploader(loader)
if metadatafile_df is not None:
sample_column = show_select_sample_column_for_metadata(
metadatafile_df, software, loader
)

st.write(
"Download the template file and add additional information "
+ "to your samples as columns (e.g. 'disease group'). "
+ "Then upload the updated metadata file."
)
show_button_download_metadata_template_file(loader)

metadatafile_upload = st.file_uploader(
"Upload metadata file with information about your samples",
)

if metadatafile_upload is None:
st.stop()

metadatafile_df = uploaded_file_to_df(metadatafile_upload)

sample_column = show_select_sample_column_for_metadata(
metadatafile_df, software, loader
)


# ########## Create dataset
Expand Down
5 changes: 4 additions & 1 deletion alphastats/gui/utils/analysis_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def download_preprocessing_info(plot):


def _read_file_to_df(file: UploadedFile, decimal: str = ".") -> Optional[pd.DataFrame]:
"""Read file to DataFrame based on file extension."""
"""Read file to DataFrame based on file extension.
TODO rename: softwarefile -> data_file
"""

extension = Path(file.name).suffix

Expand Down
50 changes: 12 additions & 38 deletions alphastats/gui/utils/import_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,51 +45,25 @@ def load_proteomics_data(uploaded_file, intensity_column, index_column, software
return loader


def load_softwarefile_df(software: str, softwarefile: UploadedFile) -> pd.DataFrame:
"""Load software file into pandas DataFrame.
def uploaded_file_to_df(
uploaded_file: UploadedFile, software: str = None
) -> pd.DataFrame:
"""Load uploaded file into pandas DataFrame. If `software` is given, do some additional checks."""
df = _read_file_to_df(uploaded_file)

TODO rename: softwarefile -> data_file
"""
softwarefile_df = _read_file_to_df(softwarefile)

_check_softwarefile_df(softwarefile_df, software)

st.write(
f"File successfully uploaded. Number of rows: {softwarefile_df.shape[0]}"
f", Number of columns: {softwarefile_df.shape[1]}."
)

st.write("Preview:")
st.dataframe(softwarefile_df.head(5))
if software is not None:
# assuming it's a softwarefile
_check_softwarefile_df(df, software)

return softwarefile_df


def show_metadata_file_uploader(loader: BaseLoader) -> Optional[pd.DataFrame]:
"""Show the 'upload metadata file' component and return the data."""
st.write(
"Download the template file and add additional information "
+ "to your samples as columns (e.g. 'disease group'). "
+ "Then upload the updated metadata file."
)
show_button_download_metadata_template_file(loader)

metadatafile_upload = st.file_uploader(
"Upload metadata file with information about your samples",
f"File successfully uploaded. Number of rows: {df.shape[0]}"
f", Number of columns: {df.shape[1]}."
)

if metadatafile_upload is None:
return None

metadatafile_df = _read_file_to_df(metadatafile_upload)
st.write(
f"File successfully uploaded. Number of rows: {metadatafile_df.shape[0]}"
f", Number of columns: {metadatafile_df.shape[1]}."
)
st.write("Preview:")
st.dataframe(metadatafile_df.head(5))
st.dataframe(df.head(5))

return metadatafile_df
return df


def load_example_data():
Expand Down

0 comments on commit 86c69df

Please sign in to comment.