Skip to content

Commit

Permalink
ruff format
Browse files Browse the repository at this point in the history
  • Loading branch information
JuliaS92 committed Sep 6, 2024
1 parent caf206e commit 005d161
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 38 deletions.
8 changes: 6 additions & 2 deletions alphastats/gui/pages/03_Data Overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@
with c2:
st.markdown("**Intensity distribution data per sample used for analysis**")
st.plotly_chart(
get_intensity_distribution_processed(st.session_state.user_session_id).update_layout(plot_bgcolor="white"),
get_intensity_distribution_processed(
st.session_state.user_session_id
).update_layout(plot_bgcolor="white"),
use_container_width=True,
)

st.plotly_chart(
get_sample_histogram_matrix(st.session_state.user_session_id).update_layout(plot_bgcolor="white"),
get_sample_histogram_matrix(st.session_state.user_session_id).update_layout(
plot_bgcolor="white"
),
use_container_width=True,
)

Expand Down
99 changes: 66 additions & 33 deletions alphastats/gui/test/test_02_import_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
import pandas as pd
from io import BytesIO


def print_session_state(apptest: AppTest):
for k,v in apptest.session_state.filtered_state.items():
print(f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}")
for k, v in apptest.session_state.filtered_state.items():
print(
f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}"
)


APP_FOLDER = Path(__file__).parent / Path("../")
APP_FILE = f"{APP_FOLDER}/pages/02_Import Data.py"
TEST_INPUT_FILES = f"{APP_FOLDER}/../../testfiles"


def test_page_02_loads_without_input():
"""Test if the page loads without any input and inititalizes the session state with the correct values."""
at = AppTest(APP_FILE, default_timeout=200)
Expand All @@ -20,11 +25,12 @@ def test_page_02_loads_without_input():
assert not at.exception

assert at.session_state.organism == 9606
assert at.session_state.user_session_id == 'test session id'
assert at.session_state.software == '<select>'
assert at.session_state.user_session_id == "test session id"
assert at.session_state.software == "<select>"
assert at.session_state.gene_to_prot_id == {}
assert at.session_state.loader == None


@patch("streamlit.file_uploader")
def test_patched_page_02_loads_without_input(mock_file_uploader: MagicMock):
"""Test if the page loads without any input and inititalizes the session state with the correct value when the file_uploader is patched."""
Expand All @@ -34,11 +40,12 @@ def test_patched_page_02_loads_without_input(mock_file_uploader: MagicMock):
assert not at.exception

assert at.session_state.organism == 9606
assert at.session_state.user_session_id == 'test session id'
assert at.session_state.software == '<select>'
assert at.session_state.user_session_id == "test session id"
assert at.session_state.software == "<select>"
assert at.session_state.gene_to_prot_id == {}
assert at.session_state.loader == None


def test_page_02_loads_sample_data():
"""Test if the page loads the sample data and has the correct session state afterwards."""
at = AppTest(APP_FILE, default_timeout=200)
Expand All @@ -49,40 +56,51 @@ def test_page_02_loads_sample_data():

assert not at.exception

assert at.session_state.metadata_columns == ['sample', 'disease', 'Drug therapy (procedure) (416608005)', 'Lipid-lowering therapy (134350008)']
assert at.session_state.metadata_columns == [
"sample",
"disease",
"Drug therapy (procedure) (416608005)",
"Lipid-lowering therapy (134350008)",
]
assert str(type(at.session_state.dataset)) == "<class 'alphastats.DataSet.DataSet'>"
assert at.session_state.software == "<select>"
assert str(type(at.session_state.distribution_plot)) == "<class 'plotly.graph_objs._figure.Figure'>"
assert str(type(at.session_state.loader)) == "<class 'alphastats.loader.MaxQuantLoader.MaxQuantLoader'>"
assert (
str(type(at.session_state.distribution_plot))
== "<class 'plotly.graph_objs._figure.Figure'>"
)
assert (
str(type(at.session_state.loader))
== "<class 'alphastats.loader.MaxQuantLoader.MaxQuantLoader'>"
)
assert "plotting_options" in at.session_state
assert "statistic_options" in at.session_state


def _data_buf(path_from_testfiles: str):
"""Helper function to open a data file from the testfiles folder and return a BytesIO object.
Additionally add filename as attribute."""
with open(f"{TEST_INPUT_FILES}{path_from_testfiles}", "rb") as f:
buf = BytesIO(f.read())
buf.name = path_from_testfiles.split('/')[-1]
buf.name = path_from_testfiles.split("/")[-1]
return buf


def _metadata_buf(path_from_testfiles: str, at: AppTest):
"""Helper function to open a metadata file from the testfiles folder and return a BytesIO object.
Additionally add filename as attribute and set the metadatafile in the session state."""
with open(f"{TEST_INPUT_FILES}{path_from_testfiles}", "rb") as f:
buf = BytesIO(f.read())
buf.name = path_from_testfiles.split('/')[-1]
buf.name = path_from_testfiles.split("/")[-1]
at.session_state.metadatafile = buf
return buf


@patch("streamlit.file_uploader")
def test_page_02_loads_maxquant_testfiles(mock_file_uploader: MagicMock):
"""Test if the page loads the MaxQuant testfiles and has the correct session state afterwards.
No input to the dropdown menus is simulated, hence the default detected values are used.
Two states are tested:
1. Files are uploaded but not processed yet
Expand All @@ -92,44 +110,59 @@ def test_page_02_loads_maxquant_testfiles(mock_file_uploader: MagicMock):

at = AppTest(APP_FILE, default_timeout=200)
at.run()

# User selects MaxQuant from the dropdown menu
at.selectbox(key='software').select('MaxQuant')
at.selectbox(key="software").select("MaxQuant")
mock_file_uploader.side_effect = [None]
at.run()

# User uploads the data file
mock_file_uploader.side_effect = [_data_buf(DATA_FILE),None]
mock_file_uploader.side_effect = [_data_buf(DATA_FILE), None]
at.run()

# User uploads the metadata file
mock_file_uploader.side_effect = [_data_buf(DATA_FILE),_metadata_buf(METADATA_FILE, at)]
mock_file_uploader.side_effect = [
_data_buf(DATA_FILE),
_metadata_buf(METADATA_FILE, at),
]
at.run()

assert not at.exception

assert str(type(at.session_state.loader)) == "<class 'alphastats.loader.MaxQuantLoader.MaxQuantLoader'>"
assert at.session_state.intensity_column == 'LFQ intensity [sample]'
assert (
str(type(at.session_state.loader))
== "<class 'alphastats.loader.MaxQuantLoader.MaxQuantLoader'>"
)
assert at.session_state.intensity_column == "LFQ intensity [sample]"
assert str(type(at.session_state.metadatafile)) == "<class '_io.BytesIO'>"
assert at.session_state.software == 'MaxQuant'
assert at.session_state.index_column == 'Protein IDs'
assert at.session_state.metadata_columns == ['sample']
assert at.session_state.sample_column == 'sample'
assert at.session_state.software == "MaxQuant"
assert at.session_state.index_column == "Protein IDs"
assert at.session_state.metadata_columns == ["sample"]
assert at.session_state.sample_column == "sample"

# User clicks the Load Data button
mock_file_uploader.side_effect = [_data_buf(DATA_FILE),_metadata_buf(METADATA_FILE, at)]
at.button('FormSubmitter:sample_column-Create DataSet').click()
mock_file_uploader.side_effect = [
_data_buf(DATA_FILE),
_metadata_buf(METADATA_FILE, at),
]
at.button("FormSubmitter:sample_column-Create DataSet").click()
at.run()

assert not at.exception

assert at.session_state.dataset.gene_names == "Gene names"
assert at.session_state.dataset.index_column == "Protein IDs"
assert at.session_state.dataset.intensity_column == 'LFQ intensity [sample]'
assert at.session_state.dataset.intensity_column == "LFQ intensity [sample]"
assert at.session_state.dataset.rawmat.shape == (312, 2611)
assert at.session_state.dataset.software == 'MaxQuant'
assert at.session_state.dataset.sample == 'sample'
assert str(type(at.session_state.distribution_plot)) == "<class 'plotly.graph_objs._figure.Figure'>"
assert str(type(at.session_state.loader)) == "<class 'alphastats.loader.MaxQuantLoader.MaxQuantLoader'>"
assert at.session_state.dataset.software == "MaxQuant"
assert at.session_state.dataset.sample == "sample"
assert (
str(type(at.session_state.distribution_plot))
== "<class 'plotly.graph_objs._figure.Figure'>"
)
assert (
str(type(at.session_state.loader))
== "<class 'alphastats.loader.MaxQuantLoader.MaxQuantLoader'>"
)
assert "plotting_options" in at.session_state
assert "statistic_options" in at.session_state
assert "statistic_options" in at.session_state
11 changes: 8 additions & 3 deletions alphastats/gui/test/test_03_data_overview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import pandas as pd
from io import BytesIO


def print_session_state(apptest: AppTest):
for k,v in apptest.session_state.filtered_state.items():
print(f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}")
for k, v in apptest.session_state.filtered_state.items():
print(
f"{k}: {str(type(v))} {str(v)[:20] if type(v) not in [int, list, str] else v}"
)


APP_FOLDER = Path(__file__).parent / Path("../")
APP_FILE = f"{APP_FOLDER}/pages/03_Data Overview.py"
TEST_INPUT_FILES = f"{APP_FOLDER}/../../testfiles"


def test_page_03_loads_without_input():
"""Test if the page loads without any input and inititalizes the session state with the correct values."""
at = AppTest(APP_FILE, default_timeout=200)
at.run()

assert not at.exception
assert not at.exception

0 comments on commit 005d161

Please sign in to comment.