Skip to content

Commit 27e4b72

Browse files
committed
Extend test_gene_cnv_frequencies_advanced_with_period_by() to include period_by as random_year
1 parent 524472d commit 27e4b72

3 files changed

Lines changed: 36 additions & 26 deletions

File tree

tests/anoph/test_cnv_frq.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
check_plot_frequencies_time_series_with_taxa,
1818
check_plot_frequencies_time_series_with_areas,
1919
check_plot_frequencies_interactive_map,
20+
add_random_year,
2021
)
2122

2223

@@ -479,7 +480,7 @@ def test_gene_cnv_frequencies_advanced_with_area_by(
479480
)
480481

481482

482-
@pytest.mark.parametrize("period_by", ["year", "quarter", "month"])
483+
@pytest.mark.parametrize("period_by", ["year", "quarter", "month", "random_year"])
483484
@parametrize_with_cases("fixture,api", cases=".")
484485
def test_gene_cnv_frequencies_advanced_with_period_by(
485486
fixture,
@@ -711,13 +712,17 @@ def check_gene_cnv_frequencies_advanced(
711712
if area_by is None:
712713
area_by = random.choice(["country", "admin1_iso", "admin2_name"])
713714
if period_by is None:
714-
period_by = random.choice(["year", "quarter", "month"])
715+
period_by = random.choice(["year", "quarter", "month", "random_year"])
715716
if sample_sets is None:
716717
all_sample_sets = api.sample_sets()["sample_set"].to_list()
717718
sample_sets = random.choice(all_sample_sets)
718719
if min_cohort_size is None:
719720
min_cohort_size = random.randint(0, 2)
720721

722+
if period_by == "random_year":
723+
# Add a random_year column to the sample metadata, if there isn't already.
724+
api = add_random_year(api=api)
725+
721726
# Run function under test.
722727
ds = api.gene_cnv_frequencies_advanced(
723728
region=region,
@@ -816,6 +821,8 @@ def check_gene_cnv_frequencies_advanced(
816821
expected_freqstr = "M"
817822
elif period_by == "quarter":
818823
expected_freqstr = "Q-DEC"
824+
elif period_by == "random_year":
825+
expected_freqstr = "Y-DEC"
819826
else:
820827
assert False, "not implemented"
821828
for p in period_values:

tests/anoph/test_frq.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
import plotly.graph_objects as go # type: ignore
33

4+
import numpy as np
5+
import pandas as pd
46
import random
57

68

@@ -91,3 +93,27 @@ def check_plot_frequencies_interactive_map(api, ds):
9193

9294
# Test.
9395
assert isinstance(fig, ipywidgets.Widget)
96+
97+
98+
def add_random_year(*, api):
99+
# Add a 'random_year' column to the sample_metadata, if it doesn't exist.
100+
101+
# Get the existing sample metadata.
102+
sample_metadata_df = api.sample_metadata()
103+
104+
# Only create the new column if it doesn't already exist.
105+
# Otherwise we'll get multiple columns with different suffixes, e.g. 'random_year_x' and 'random_year_y'.
106+
if "random_year" not in sample_metadata_df.columns:
107+
# Avoid "ValueError: No cohorts available" by selecting only a few different years at random.
108+
selected_years = random.sample(range(1900, 2100), 3)
109+
random_years_as_list = np.random.choice(selected_years, len(sample_metadata_df))
110+
random_years_as_period_index = pd.PeriodIndex(random_years_as_list, freq="Y")
111+
extra_metadata_df = pd.DataFrame(
112+
{
113+
"sample_id": sample_metadata_df["sample_id"],
114+
"random_year": random_years_as_period_index,
115+
}
116+
)
117+
api.add_extra_metadata(extra_metadata_df)
118+
119+
return api

tests/anoph/test_snp_frq.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
check_plot_frequencies_time_series_with_taxa,
1919
check_plot_frequencies_time_series_with_areas,
2020
check_plot_frequencies_interactive_map,
21+
add_random_year,
2122
)
2223

2324

@@ -114,30 +115,6 @@ def random_transcript(*, api):
114115
return transcript
115116

116117

117-
def add_random_year(*, api):
118-
# Add a 'random_year' column to the sample_metadata, if it doesn't exist.
119-
120-
# Get the existing sample metadata.
121-
sample_metadata_df = api.sample_metadata()
122-
123-
# Only create the new column if it doesn't already exist.
124-
# Otherwise we'll get multiple columns with different suffixes, e.g. 'random_year_x' and 'random_year_y'.
125-
if "random_year" not in sample_metadata_df.columns:
126-
# Avoid "ValueError: No cohorts available" by selecting only a few different years at random.
127-
selected_years = random.sample(range(1900, 2100), 3)
128-
random_years_as_list = np.random.choice(selected_years, len(sample_metadata_df))
129-
random_years_as_period_index = pd.PeriodIndex(random_years_as_list, freq="Y")
130-
extra_metadata_df = pd.DataFrame(
131-
{
132-
"sample_id": sample_metadata_df["sample_id"],
133-
"random_year": random_years_as_period_index,
134-
}
135-
)
136-
api.add_extra_metadata(extra_metadata_df)
137-
138-
return api
139-
140-
141118
@parametrize_with_cases("fixture,api", cases=".")
142119
def test_snp_effects(fixture, api: AnophelesSnpFrequencyAnalysis):
143120
# Pick a random transcript.

0 commit comments

Comments
 (0)