@@ -114,6 +114,30 @@ def random_transcript(*, api):
114114 return transcript
115115
116116
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+ random_years_as_list = np .random .choice (
127+ range (1900 , 2100 ), len (sample_metadata_df )
128+ )
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+
117141@parametrize_with_cases ("fixture,api" , cases = "." )
118142def test_snp_effects (fixture , api : AnophelesSnpFrequencyAnalysis ):
119143 # Pick a random transcript.
@@ -888,7 +912,7 @@ def check_snp_allele_frequencies_advanced(
888912 if area_by is None :
889913 area_by = random .choice (["country" , "admin1_iso" , "admin2_name" ])
890914 if period_by is None :
891- period_by = random .choice (["year" , "quarter" , "month" ])
915+ period_by = random .choice (["year" , "quarter" , "month" , "random_year" ])
892916 if sample_sets is None :
893917 all_sample_sets = api .sample_sets ()["sample_set" ].to_list ()
894918 sample_sets = random .choice (all_sample_sets )
@@ -897,6 +921,10 @@ def check_snp_allele_frequencies_advanced(
897921 if site_mask is None :
898922 site_mask = random .choice (api .site_mask_ids + (None ,))
899923
924+ if period_by == "random_year" :
925+ # Add a random_year column to the sample metadata, if there isn't already.
926+ api = add_random_year (api = api )
927+
900928 # Run function under test.
901929 ds = api .snp_allele_frequencies_advanced (
902930 transcript = transcript ,
@@ -1002,6 +1030,8 @@ def check_snp_allele_frequencies_advanced(
10021030 expected_freqstr = "M"
10031031 elif period_by == "quarter" :
10041032 expected_freqstr = "Q-DEC"
1033+ elif period_by == "random_year" :
1034+ expected_freqstr = "Y-DEC"
10051035 else :
10061036 assert False , "not implemented"
10071037 for p in period_values :
@@ -1082,13 +1112,17 @@ def check_aa_allele_frequencies_advanced(
10821112 if area_by is None :
10831113 area_by = random .choice (["country" , "admin1_iso" , "admin2_name" ])
10841114 if period_by is None :
1085- period_by = random .choice (["year" , "quarter" , "month" ])
1115+ period_by = random .choice (["year" , "quarter" , "month" , "random_year" ])
10861116 if sample_sets is None :
10871117 all_sample_sets = api .sample_sets ()["sample_set" ].to_list ()
10881118 sample_sets = random .choice (all_sample_sets )
10891119 if min_cohort_size is None :
10901120 min_cohort_size = random .randint (0 , 2 )
10911121
1122+ if period_by == "random_year" :
1123+ # Add a random_year column to the sample metadata, if there isn't already.
1124+ api = add_random_year (api = api )
1125+
10921126 # Run function under test.
10931127 ds = api .aa_allele_frequencies_advanced (
10941128 transcript = transcript ,
@@ -1185,6 +1219,8 @@ def check_aa_allele_frequencies_advanced(
11851219 expected_freqstr = "M"
11861220 elif period_by == "quarter" :
11871221 expected_freqstr = "Q-DEC"
1222+ elif period_by == "random_year" :
1223+ expected_freqstr = "Y-DEC"
11881224 else :
11891225 assert False , "not implemented"
11901226 for p in period_values :
@@ -1266,7 +1302,7 @@ def test_allele_frequencies_advanced_with_area_by(
12661302 )
12671303
12681304
1269- @pytest .mark .parametrize ("period_by" , ["year" , "quarter" , "month" ])
1305+ @pytest .mark .parametrize ("period_by" , ["year" , "quarter" , "month" , "random_year" ])
12701306@parametrize_with_cases ("fixture,api" , cases = "." )
12711307def test_allele_frequencies_advanced_with_period_by (
12721308 fixture ,
0 commit comments