Skip to content

Commit a8128ab

Browse files
committed
Implementation of time windows for the AFS,
and testing version of time windows for general stat
1 parent 1d8f453 commit a8128ab

36 files changed

+981
-243
lines changed

c/tests/test_stats.c

Lines changed: 96 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424

2525
#include "testlib.h"
26+
#include <math.h>
2627
#include <tskit/stats.h>
2728

2829
#include <unistd.h>
@@ -602,7 +603,7 @@ verify_pair_coalescence_rates(tsk_treeseq_t *ts)
602603
epochs[B] = DBL_MAX;
603604
ret = tsk_treeseq_pair_coalescence_rates(ts, P, sample_set_sizes, sample_sets, I,
604605
index_tuples, T, breakpoints, B, node_bin_map, epochs, 0, C);
605-
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_TIME_WINDOWS);
606+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_TIME_WINDOWS_END);
606607
epochs[B] = INFINITY;
607608

608609
node_bin_map[0] = (tsk_id_t) B;
@@ -868,6 +869,84 @@ verify_one_way_stat_func_errors(tsk_treeseq_t *ts, one_way_sample_stat_method *m
868869
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);
869870
}
870871

872+
// Temporary definition for time_windows in tsk_treeseq_allele_frequency_spectrum
873+
typedef int one_way_sample_stat_method_tw(const tsk_treeseq_t *self,
874+
tsk_size_t num_sample_sets, const tsk_size_t *sample_set_sizes,
875+
const tsk_id_t *sample_sets, tsk_size_t num_windows, const double *windows,
876+
tsk_size_t num_time_windows, const double *time_windows, tsk_flags_t options,
877+
double *result);
878+
879+
// Temporary duplicate for time-windows-having methods
880+
static void
881+
verify_one_way_stat_func_errors_tw(
882+
tsk_treeseq_t *ts, one_way_sample_stat_method_tw *method)
883+
{
884+
int ret;
885+
tsk_id_t num_nodes = (tsk_id_t) tsk_treeseq_get_num_nodes(ts);
886+
tsk_id_t samples[] = { 0, 1, 2, 3 };
887+
tsk_size_t sample_set_sizes = 4;
888+
double windows[] = { 0, 0, 0 };
889+
double time_windows[] = { -1, 0.5, INFINITY };
890+
double result;
891+
892+
ret = method(ts, 0, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
893+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_INSUFFICIENT_SAMPLE_SETS);
894+
895+
samples[0] = TSK_NULL;
896+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
897+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_NODE_OUT_OF_BOUNDS);
898+
samples[0] = -10;
899+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
900+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_NODE_OUT_OF_BOUNDS);
901+
samples[0] = num_nodes;
902+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
903+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_NODE_OUT_OF_BOUNDS);
904+
samples[0] = num_nodes + 1;
905+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
906+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_NODE_OUT_OF_BOUNDS);
907+
908+
samples[0] = num_nodes - 1;
909+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
910+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_SAMPLES);
911+
912+
samples[0] = 1;
913+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
914+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_DUPLICATE_SAMPLE);
915+
916+
samples[0] = 0;
917+
sample_set_sizes = 0;
918+
ret = method(ts, 1, &sample_set_sizes, samples, 0, NULL, 0, NULL, 0, &result);
919+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_EMPTY_SAMPLE_SET);
920+
921+
sample_set_sizes = 4;
922+
/* Window errors */
923+
ret = method(ts, 1, &sample_set_sizes, samples, 0, windows, 0, NULL, 0, &result);
924+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_NUM_WINDOWS);
925+
926+
ret = method(ts, 1, &sample_set_sizes, samples, 2, windows, 0, NULL, 0, &result);
927+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_WINDOWS);
928+
929+
/* Time window errors */
930+
ret = method(
931+
ts, 1, &sample_set_sizes, samples, 0, NULL, 0, time_windows, 0, &result);
932+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_TIME_WINDOWS_DIM);
933+
934+
ret = method(
935+
ts, 1, &sample_set_sizes, samples, 0, NULL, 2, time_windows, 0, &result);
936+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_TIME_WINDOWS);
937+
938+
time_windows[0] = 0.1;
939+
ret = method(
940+
ts, 1, &sample_set_sizes, samples, 0, NULL, 2, time_windows, 0, &result);
941+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_TIME_WINDOWS);
942+
943+
time_windows[0] = 0;
944+
time_windows[1] = 0;
945+
ret = method(
946+
ts, 1, &sample_set_sizes, samples, 0, NULL, 2, time_windows, 0, &result);
947+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_BAD_TIME_WINDOWS);
948+
}
949+
871950
static void
872951
verify_two_way_stat_func_errors(
873952
tsk_treeseq_t *ts, general_sample_stat_method *method, tsk_flags_t options)
@@ -1206,23 +1285,24 @@ verify_afs(tsk_treeseq_t *ts)
12061285
sample_set_sizes[0] = n - 2;
12071286
sample_set_sizes[1] = 2;
12081287
ret = tsk_treeseq_allele_frequency_spectrum(
1209-
ts, 2, sample_set_sizes, samples, 0, NULL, 0, result);
1288+
ts, 2, sample_set_sizes, samples, 0, NULL, 0, NULL, 0, result);
12101289
CU_ASSERT_EQUAL_FATAL(ret, 0);
12111290

12121291
ret = tsk_treeseq_allele_frequency_spectrum(
1213-
ts, 2, sample_set_sizes, samples, 0, NULL, TSK_STAT_POLARISED, result);
1292+
ts, 2, sample_set_sizes, samples, 0, NULL, 0, NULL, TSK_STAT_POLARISED, result);
12141293
CU_ASSERT_EQUAL_FATAL(ret, 0);
12151294

12161295
ret = tsk_treeseq_allele_frequency_spectrum(ts, 2, sample_set_sizes, samples, 0,
1217-
NULL, TSK_STAT_POLARISED | TSK_STAT_SPAN_NORMALISE, result);
1296+
NULL, 0, NULL, TSK_STAT_POLARISED | TSK_STAT_SPAN_NORMALISE, result);
12181297
CU_ASSERT_EQUAL_FATAL(ret, 0);
12191298

12201299
ret = tsk_treeseq_allele_frequency_spectrum(ts, 2, sample_set_sizes, samples, 0,
1221-
NULL, TSK_STAT_BRANCH | TSK_STAT_POLARISED | TSK_STAT_SPAN_NORMALISE, result);
1300+
NULL, 0, NULL, TSK_STAT_BRANCH | TSK_STAT_POLARISED | TSK_STAT_SPAN_NORMALISE,
1301+
result);
12221302
CU_ASSERT_EQUAL_FATAL(ret, 0);
12231303

12241304
ret = tsk_treeseq_allele_frequency_spectrum(ts, 2, sample_set_sizes, samples, 0,
1225-
NULL, TSK_STAT_BRANCH | TSK_STAT_SPAN_NORMALISE, result);
1305+
NULL, 0, NULL, TSK_STAT_BRANCH | TSK_STAT_SPAN_NORMALISE, result);
12261306
CU_ASSERT_EQUAL_FATAL(ret, 0);
12271307

12281308
free(result);
@@ -2416,21 +2496,26 @@ test_paper_ex_afs_errors(void)
24162496
tsk_size_t sample_set_sizes[] = { 2, 2 };
24172497
tsk_id_t samples[] = { 0, 1, 2, 3 };
24182498
double result[10]; /* not thinking too hard about the actual value needed */
2499+
double time_windows[] = { 0, 1 };
24192500
int ret;
24202501

24212502
tsk_treeseq_from_text(&ts, 10, paper_ex_nodes, paper_ex_edges, NULL, paper_ex_sites,
24222503
paper_ex_mutations, paper_ex_individuals, NULL, 0);
24232504

2424-
verify_one_way_stat_func_errors(&ts, tsk_treeseq_allele_frequency_spectrum);
2505+
verify_one_way_stat_func_errors_tw(&ts, tsk_treeseq_allele_frequency_spectrum);
24252506

24262507
ret = tsk_treeseq_allele_frequency_spectrum(
2427-
&ts, 2, sample_set_sizes, samples, 0, NULL, TSK_STAT_NODE, result);
2508+
&ts, 2, sample_set_sizes, samples, 0, NULL, 0, NULL, TSK_STAT_NODE, result);
24282509
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_UNSUPPORTED_STAT_MODE);
24292510

24302511
ret = tsk_treeseq_allele_frequency_spectrum(&ts, 2, sample_set_sizes, samples, 0,
2431-
NULL, TSK_STAT_BRANCH | TSK_STAT_SITE, result);
2512+
NULL, 0, NULL, TSK_STAT_BRANCH | TSK_STAT_SITE, result);
24322513
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_MULTIPLE_STAT_MODES);
24332514

2515+
ret = tsk_treeseq_allele_frequency_spectrum(&ts, 2, sample_set_sizes, samples, 0,
2516+
NULL, 1, time_windows, TSK_STAT_SITE, result);
2517+
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_UNSUPPORTED_STAT_MODE);
2518+
24342519
tsk_treeseq_free(&ts);
24352520
}
24362521

@@ -2448,14 +2533,14 @@ test_paper_ex_afs(void)
24482533
/* we have two singletons and one tripleton */
24492534

24502535
ret = tsk_treeseq_allele_frequency_spectrum(
2451-
&ts, 1, sample_set_sizes, samples, 0, NULL, 0, result);
2536+
&ts, 1, sample_set_sizes, samples, 0, NULL, 0, NULL, 0, result);
24522537
CU_ASSERT_EQUAL_FATAL(ret, 0);
24532538
CU_ASSERT_EQUAL_FATAL(result[0], 0);
24542539
CU_ASSERT_EQUAL_FATAL(result[1], 3.0);
24552540
CU_ASSERT_EQUAL_FATAL(result[2], 0);
24562541

24572542
ret = tsk_treeseq_allele_frequency_spectrum(
2458-
&ts, 1, sample_set_sizes, samples, 0, NULL, TSK_STAT_POLARISED, result);
2543+
&ts, 1, sample_set_sizes, samples, 0, NULL, 0, NULL, TSK_STAT_POLARISED, result);
24592544
CU_ASSERT_EQUAL_FATAL(ret, 0);
24602545
CU_ASSERT_EQUAL_FATAL(result[0], 0);
24612546
CU_ASSERT_EQUAL_FATAL(result[1], 2.0);

c/tests/test_trees.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8026,13 +8026,13 @@ test_time_uncalibrated(void)
80268026
CU_ASSERT_EQUAL_FATAL(ret, 0);
80278027

80288028
ret = tsk_treeseq_allele_frequency_spectrum(
8029-
&ts2, 2, sample_set_sizes, samples, 0, NULL, TSK_STAT_SITE, result);
8029+
&ts2, 2, sample_set_sizes, samples, 0, NULL, 0, NULL, TSK_STAT_SITE, result);
80308030
CU_ASSERT_EQUAL_FATAL(ret, 0);
80318031
ret = tsk_treeseq_allele_frequency_spectrum(
8032-
&ts2, 2, sample_set_sizes, samples, 0, NULL, TSK_STAT_BRANCH, result);
8032+
&ts2, 2, sample_set_sizes, samples, 0, NULL, 0, NULL, TSK_STAT_BRANCH, result);
80338033
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_TIME_UNCALIBRATED);
80348034
ret = tsk_treeseq_allele_frequency_spectrum(&ts2, 2, sample_set_sizes, samples, 0,
8035-
NULL, TSK_STAT_BRANCH | TSK_STAT_ALLOW_TIME_UNCALIBRATED, result);
8035+
NULL, 0, NULL, TSK_STAT_BRANCH | TSK_STAT_ALLOW_TIME_UNCALIBRATED, result);
80368036
CU_ASSERT_EQUAL_FATAL(ret, 0);
80378037

80388038
sigma = tsk_calloc(tsk_treeseq_get_num_nodes(&ts2), sizeof(double));

c/tskit/core.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,13 @@ tsk_strerror_internal(int err)
526526
"(TSK_ERR_BAD_SAMPLE_PAIR_TIMES)";
527527
break;
528528
case TSK_ERR_BAD_TIME_WINDOWS:
529-
ret = "Time windows must be strictly increasing and end at infinity. "
529+
ret = "Time windows must be strictly increasing. "
530530
"(TSK_ERR_BAD_TIME_WINDOWS)";
531531
break;
532+
case TSK_ERR_BAD_TIME_WINDOWS_END:
533+
ret = "Time windows must end at infinity for this method. "
534+
"(TSK_ERR_BAD_TIME_WINDOWS_END)";
535+
break;
532536
case TSK_ERR_BAD_NODE_TIME_WINDOW:
533537
ret = "Node time does not fall within assigned time window. "
534538
"(TSK_ERR_BAD_NODE_TIME_WINDOW)";

c/tskit/core.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,13 +746,17 @@ Sample times do not all equal the start of first time window
746746
*/
747747
#define TSK_ERR_BAD_SAMPLE_PAIR_TIMES -923
748748
/**
749-
Time windows are not strictly increasing ending at infinity
749+
Time windows are not strictly increasing
750750
*/
751751
#define TSK_ERR_BAD_TIME_WINDOWS -924
752752
/**
753+
Time windows do not end at infinity
754+
*/
755+
#define TSK_ERR_BAD_TIME_WINDOWS_END -925
756+
/**
753757
Node time does not fall within assigned time window
754758
*/
755-
#define TSK_ERR_BAD_NODE_TIME_WINDOW -925
759+
#define TSK_ERR_BAD_NODE_TIME_WINDOW -926
756760
/** @} */
757761

758762
/**

0 commit comments

Comments
 (0)