Skip to content

Commit 9d175be

Browse files
committed
Expand taxa, areas params for plot_frequencies_time_series()
1 parent 18ae197 commit 9d175be

File tree

4 files changed

+46
-22
lines changed

4 files changed

+46
-22
lines changed

malariagen_data/anoph/frq_params.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Parameter definitions for functions computing and plotting allele frequencies."""
22

3-
from typing import Literal
3+
from typing import Literal, List, Optional, Tuple, Union
44

55
import xarray as xr
66
from typing_extensions import Annotated, TypeAlias
@@ -70,3 +70,13 @@
7070
bool,
7171
"Include columns with allele counts and number of non-missing allele calls (nobs).",
7272
]
73+
74+
taxa: TypeAlias = Annotated[
75+
Optional[Union[str, List[str], Tuple[str, ...]]],
76+
"The taxon or taxa to restrict the dataset to.",
77+
]
78+
79+
areas: TypeAlias = Annotated[
80+
Optional[Union[str, List[str], Tuple[str, ...]]],
81+
"The area or areas to restrict the dataset to.",
82+
]

malariagen_data/anoph/plotly_params.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,3 @@
215215
],
216216
"Scope of the map.",
217217
]
218-
219-
taxon: TypeAlias = Annotated[
220-
Optional[str],
221-
"The taxon to restrict the dataset to.",
222-
]
223-
224-
area: TypeAlias = Annotated[
225-
Optional[str],
226-
"The area to restrict the dataset to.",
227-
]

malariagen_data/anoph/snp_frq.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -936,8 +936,8 @@ def plot_frequencies_time_series(
936936
legend_sizing: plotly_params.legend_sizing = "constant",
937937
show: plotly_params.show = True,
938938
renderer: plotly_params.renderer = None,
939-
taxon: plotly_params.taxon = None,
940-
area: plotly_params.area = None,
939+
taxa: frq_params.taxa = None,
940+
areas: frq_params.areas = None,
941941
**kwargs,
942942
) -> plotly_params.figure:
943943
# Handle title.
@@ -949,13 +949,17 @@ def plot_frequencies_time_series(
949949
df_cohorts = ds[cohort_vars].to_dataframe()
950950
df_cohorts.columns = [c.split("cohort_")[1] for c in df_cohorts.columns] # type: ignore
951951

952-
# If specified, restrict the dataframe by taxon.
953-
if taxon:
954-
df_cohorts = df_cohorts[df_cohorts["taxon"] == taxon]
955-
956-
# If specified, restrict the dataframe by area.
957-
if area:
958-
df_cohorts = df_cohorts[df_cohorts["area"] == area]
952+
# If specified, restrict the dataframe by taxa.
953+
if isinstance(taxa, str):
954+
df_cohorts = df_cohorts[df_cohorts["taxon"] == taxa]
955+
elif isinstance(taxa, (list, tuple)):
956+
df_cohorts = df_cohorts[df_cohorts["taxon"].isin(taxa)]
957+
958+
# If specified, restrict the dataframe by areas.
959+
if isinstance(areas, str):
960+
df_cohorts = df_cohorts[df_cohorts["area"] == areas]
961+
elif isinstance(areas, (list, tuple)):
962+
df_cohorts = df_cohorts[df_cohorts["area"].isin(areas)]
959963

960964
# Extract variant labels.
961965
variant_labels = ds["variant_label"].values

notebooks/plot_frequencies_space_time.ipynb

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,17 @@
8282
"metadata": {},
8383
"outputs": [],
8484
"source": [
85-
"ag3.plot_frequencies_time_series(ds, taxon=\"gambiae\", height=500, width=1000)"
85+
"ag3.plot_frequencies_time_series(ds, taxa=\"gambiae\", height=500, width=1000)"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"id": "1bfc7298",
92+
"metadata": {},
93+
"outputs": [],
94+
"source": [
95+
"ag3.plot_frequencies_time_series(ds, taxa=(\"gambiae\", \"arabiensis\"), height=500, width=1000)"
8696
]
8797
},
8898
{
@@ -275,7 +285,17 @@
275285
"metadata": {},
276286
"outputs": [],
277287
"source": [
278-
"ag3.plot_frequencies_time_series(ds, area=\"BF-09\", height=400, width=900)"
288+
"ag3.plot_frequencies_time_series(ds, areas=\"BF-09\", height=400, width=900)"
289+
]
290+
},
291+
{
292+
"cell_type": "code",
293+
"execution_count": null,
294+
"id": "26af27a1",
295+
"metadata": {},
296+
"outputs": [],
297+
"source": [
298+
"ag3.plot_frequencies_time_series(ds, areas=(\"BF-09\", \"TZ-25\"), height=400, width=900)"
279299
]
280300
},
281301
{

0 commit comments

Comments
 (0)