From 1a305108ea4b0ed0c7adbcda09de7359fde0687a Mon Sep 17 00:00:00 2001 From: jonbrenas <51911846+jonbrenas@users.noreply.github.com> Date: Wed, 22 May 2024 14:59:25 +0100 Subject: [PATCH 1/3] Added an option to have Z scores instead of se in the upper-right corner of the Fst plots --- malariagen_data/anoph/fst.py | 12 ++++++++---- malariagen_data/anoph/fst_params.py | 11 ++++++++++- tests/anoph/test_fst.py | 2 ++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/malariagen_data/anoph/fst.py b/malariagen_data/anoph/fst.py index 1881f1ee8..1061494f8 100644 --- a/malariagen_data/anoph/fst.py +++ b/malariagen_data/anoph/fst.py @@ -480,7 +480,7 @@ def pairwise_average_fst( def plot_pairwise_average_fst( self, fst_df: fst_params.df_pairwise_fst, - annotate_se: bool = False, + annotate_se: fst_params.annotate_se = None, zmin: Optional[plotly_params.zmin] = 0.0, zmax: Optional[plotly_params.zmax] = None, text_auto: plotly_params.text_auto = ".3f", @@ -501,9 +501,13 @@ def plot_pairwise_average_fst( col = fst_df.iloc[index_key]["cohort2"] fst = fst_df.iloc[index_key]["fst"] fig_df[index][col] = fst - if annotate_se is True: - se = fst_df.iloc[index_key]["se"] - fig_df[col][index] = se + if annotate_se: + if annotate_se is True or annotate_se == "standard error": + se = fst_df.iloc[index_key]["se"] + fig_df[col][index] = se + elif annotate_se == "Z score": + zs = fst_df.iloc[index_key]["fst"] / fst_df.iloc[index_key]["se"] + fig_df[col][index] = zs else: fig_df[col][index] = fst diff --git a/malariagen_data/anoph/fst_params.py b/malariagen_data/anoph/fst_params.py index e03c06d76..9168dea6b 100644 --- a/malariagen_data/anoph/fst_params.py +++ b/malariagen_data/anoph/fst_params.py @@ -1,6 +1,6 @@ """Parameter definitions for Fst functions.""" -from typing import Optional +from typing import Optional, Union, Literal import pandas as pd from typing_extensions import Annotated, TypeAlias @@ -22,3 +22,12 @@ A dataframe of pairwise Fst and standard error values. """, ] + +annotate_se: TypeAlias = Annotated[ + Optional[Union[Literal["standard error", "Z score"], bool]], + """ + How to annotate the upper-right corner of the plot. Default behaviour is using Fst, other options + are using the standard error (if annotate_se is True or 'standard error') or the Z score of the two + cohorts being the same (if annotate_se is 'Z score'). + """, +] diff --git a/tests/anoph/test_fst.py b/tests/anoph/test_fst.py index 37c5e7351..8440f8225 100644 --- a/tests/anoph/test_fst.py +++ b/tests/anoph/test_fst.py @@ -208,6 +208,8 @@ def check_pairwise_average_fst(api: AnophelesFstAnalysis, fst_params): assert isinstance(fig, go.Figure) fig = api.plot_pairwise_average_fst(fst_df, annotate_se=True, show=False) assert isinstance(fig, go.Figure) + fig = api.plot_pairwise_average_fst(fst_df, annotate_se="Z score", show=False) + assert isinstance(fig, go.Figure) @pytest.mark.parametrize("cohorts", ["country", "admin1_year", "cohort_admin2_month"]) From 1ebea038f1b2b4dabadfd0120e7ca51cbf81a947 Mon Sep 17 00:00:00 2001 From: jonbrenas <51911846+jonbrenas@users.noreply.github.com> Date: Thu, 20 Jun 2024 20:04:48 +0200 Subject: [PATCH 2/3] Ruffing --- malariagen_data/anoph/fst.py | 8 ++++---- malariagen_data/anoph/fst_params.py | 12 ++++++------ tests/anoph/test_fst.py | 6 ++++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/malariagen_data/anoph/fst.py b/malariagen_data/anoph/fst.py index 1061494f8..67be16c17 100644 --- a/malariagen_data/anoph/fst.py +++ b/malariagen_data/anoph/fst.py @@ -480,7 +480,7 @@ def pairwise_average_fst( def plot_pairwise_average_fst( self, fst_df: fst_params.df_pairwise_fst, - annotate_se: fst_params.annotate_se = None, + annotation: fst_params.annotation = None, zmin: Optional[plotly_params.zmin] = 0.0, zmax: Optional[plotly_params.zmax] = None, text_auto: plotly_params.text_auto = ".3f", @@ -501,11 +501,11 @@ def plot_pairwise_average_fst( col = fst_df.iloc[index_key]["cohort2"] fst = fst_df.iloc[index_key]["fst"] fig_df[index][col] = fst - if annotate_se: - if annotate_se is True or annotate_se == "standard error": + if annotation: + if annotation == "standard error": se = fst_df.iloc[index_key]["se"] fig_df[col][index] = se - elif annotate_se == "Z score": + elif annotation == "Z score": zs = fst_df.iloc[index_key]["fst"] / fst_df.iloc[index_key]["se"] fig_df[col][index] = zs else: diff --git a/malariagen_data/anoph/fst_params.py b/malariagen_data/anoph/fst_params.py index 9168dea6b..596a6bf16 100644 --- a/malariagen_data/anoph/fst_params.py +++ b/malariagen_data/anoph/fst_params.py @@ -1,6 +1,6 @@ """Parameter definitions for Fst functions.""" -from typing import Optional, Union, Literal +from typing import Optional, Literal import pandas as pd from typing_extensions import Annotated, TypeAlias @@ -23,11 +23,11 @@ """, ] -annotate_se: TypeAlias = Annotated[ - Optional[Union[Literal["standard error", "Z score"], bool]], +annotation: TypeAlias = Annotated[ + Optional[Literal["standard error", "Z score"]], """ - How to annotate the upper-right corner of the plot. Default behaviour is using Fst, other options - are using the standard error (if annotate_se is True or 'standard error') or the Z score of the two - cohorts being the same (if annotate_se is 'Z score'). + How to annotate the upper-right corner of the plot. Default behaviour (None) is using Fst, other options + are using the standard error (if annotation is 'standard error') or the Z score of the two + cohorts being the same (if annotation is 'Z score'). """, ] diff --git a/tests/anoph/test_fst.py b/tests/anoph/test_fst.py index 8440f8225..8297e1d11 100644 --- a/tests/anoph/test_fst.py +++ b/tests/anoph/test_fst.py @@ -206,9 +206,11 @@ def check_pairwise_average_fst(api: AnophelesFstAnalysis, fst_params): if len(fst_df) > 0: fig = api.plot_pairwise_average_fst(fst_df, show=False) assert isinstance(fig, go.Figure) - fig = api.plot_pairwise_average_fst(fst_df, annotate_se=True, show=False) + fig = api.plot_pairwise_average_fst( + fst_df, annotation="standard error", show=False + ) assert isinstance(fig, go.Figure) - fig = api.plot_pairwise_average_fst(fst_df, annotate_se="Z score", show=False) + fig = api.plot_pairwise_average_fst(fst_df, annotation="Z score", show=False) assert isinstance(fig, go.Figure) From 1efc4bc542d5f6c6d0bd6390493c47b9931cee3d Mon Sep 17 00:00:00 2001 From: jonbrenas <51911846+jonbrenas@users.noreply.github.com> Date: Thu, 20 Jun 2024 20:29:19 +0200 Subject: [PATCH 3/3] Updated the notebook --- malariagen_data/anoph/fst.py | 13 ++++++------- notebooks/plot_pairwise_average_fst.ipynb | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/malariagen_data/anoph/fst.py b/malariagen_data/anoph/fst.py index 7a6ac2eb2..57511be4e 100644 --- a/malariagen_data/anoph/fst.py +++ b/malariagen_data/anoph/fst.py @@ -501,13 +501,12 @@ def plot_pairwise_average_fst( col = fst_df.iloc[index_key]["cohort2"] fst = fst_df.iloc[index_key]["fst"] fig_df[index][col] = fst - if annotation: - if annotation == "standard error": - se = fst_df.iloc[index_key]["se"] - fig_df[col][index] = se - elif annotation == "Z score": - zs = fst_df.iloc[index_key]["fst"] / fst_df.iloc[index_key]["se"] - fig_df[col][index] = zs + if annotation == "standard error": + se = fst_df.iloc[index_key]["se"] + fig_df[col][index] = se + elif annotation == "Z score": + zs = fst_df.iloc[index_key]["fst"] / fst_df.iloc[index_key]["se"] + fig_df[col][index] = zs else: fig_df.loc[index, col] = fst diff --git a/notebooks/plot_pairwise_average_fst.ipynb b/notebooks/plot_pairwise_average_fst.ipynb index 285d2a792..901bf17fa 100644 --- a/notebooks/plot_pairwise_average_fst.ipynb +++ b/notebooks/plot_pairwise_average_fst.ipynb @@ -94,7 +94,7 @@ "metadata": {}, "outputs": [], "source": [ - "ag3.plot_pairwise_average_fst(pairwise_fst_df, annotate_se=True)" + "ag3.plot_pairwise_average_fst(pairwise_fst_df, annotation=\"standard error\")" ] }, { @@ -104,7 +104,7 @@ "metadata": {}, "outputs": [], "source": [ - "ag3.plot_pairwise_average_fst(pairwise_fst_df, annotate_se=False)" + "ag3.plot_pairwise_average_fst(pairwise_fst_df, annotation=\"Z score\")" ] }, {