Skip to content

Commit 451ac16

Browse files
Allow NaNs in individual locations.
Closes #351
1 parent 47decb2 commit 451ac16

File tree

5 files changed

+45
-36
lines changed

5 files changed

+45
-36
lines changed

c/tests/test_trees.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,17 +1849,15 @@ test_simplest_individuals(void)
18491849
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_INDIVIDUAL_OUT_OF_BOUNDS);
18501850
tsk_treeseq_free(&ts);
18511851

1852+
/* NaN/ifinity values are allowed in locations they do not
1853+
* affect the integrity of the model. */
18521854
tables.individuals.location[0] = NAN;
18531855
ret = tsk_treeseq_init(&ts, &tables, load_flags);
1854-
CU_ASSERT_EQUAL(ret, TSK_ERR_SPATIAL_LOCATION_NONFINITE);
1855-
tsk_treeseq_free(&ts);
1856-
tables.individuals.location[0] = 0.25;
1857-
1858-
tables.individuals.location[2] = INFINITY;
1859-
ret = tsk_treeseq_init(&ts, &tables, load_flags);
1860-
CU_ASSERT_EQUAL(ret, TSK_ERR_SPATIAL_LOCATION_NONFINITE);
1856+
CU_ASSERT_EQUAL(ret, 0);
1857+
ret = tsk_treeseq_get_individual(&ts, 0, &individual);
1858+
CU_ASSERT_EQUAL_FATAL(ret, 0);
1859+
CU_ASSERT(! isfinite(individual.location[0]));
18611860
tsk_treeseq_free(&ts);
1862-
tables.individuals.location[0] = 0.25;
18631861

18641862
tsk_table_collection_free(&tables);
18651863
}

c/tskit/core.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,6 @@ tsk_strerror_internal(int err)
204204
case TSK_ERR_GENOME_COORDS_NONFINITE:
205205
ret = "Genome coordinates must be finite numbers";
206206
break;
207-
case TSK_ERR_SPATIAL_LOCATION_NONFINITE:
208-
ret = "Location values must be finite numbers";
209-
break;
210207

211208
/* Edge errors */
212209
case TSK_ERR_NULL_PARENT:

c/tskit/core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ of tskit.
156156
#define TSK_ERR_PROVENANCE_OUT_OF_BOUNDS -209
157157
#define TSK_ERR_TIME_NONFINITE -210
158158
#define TSK_ERR_GENOME_COORDS_NONFINITE -211
159-
#define TSK_ERR_SPATIAL_LOCATION_NONFINITE -212
160159

161160
/* Edge errors */
162161
#define TSK_ERR_NULL_PARENT -300

c/tskit/tables.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5672,14 +5672,6 @@ tsk_table_collection_check_integrity(tsk_table_collection_t *self, tsk_flags_t o
56725672
goto out;
56735673
}
56745674

5675-
/* Individuals */
5676-
for (j = 0; j < self->individuals.location_offset[self->individuals.num_rows]; j++) {
5677-
if (! isfinite(self->individuals.location[j])) {
5678-
ret = TSK_ERR_SPATIAL_LOCATION_NONFINITE;
5679-
goto out;
5680-
}
5681-
}
5682-
56835675
/* Nodes */
56845676
for (j = 0; j < self->nodes.num_rows; j++) {
56855677
node_time = self->nodes.time[j];

python/tests/test_tables.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,45 +1396,68 @@ def test_round_trip(self):
13961396
self.assertTrue(
13971397
all(a == b for a, b in zip(a.tables, b.tables) if a[0] != 'provenances'))
13981398

1399-
def test_bad_edge_coords(self):
1400-
ts = msprime.simulate(5, mutation_rate=1, random_seed=self.random_seed)
1399+
1400+
class TestNanDoubleValues(unittest.TestCase):
1401+
"""
1402+
In some tables we need to guard against NaN/infinite values in the input.
1403+
"""
1404+
1405+
def test_edge_coords(self):
1406+
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)
14011407

14021408
tables = ts.dump_tables()
14031409
bad_coords = tables.edges.left + float('inf')
1404-
tables.edges.set_columns(**dict(tables.edges.asdict(), left=bad_coords))
1410+
tables.edges.left = bad_coords
14051411
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
14061412

14071413
tables = ts.dump_tables()
14081414
bad_coords = tables.edges.right + float('nan')
1409-
tables.edges.set_columns(**dict(tables.edges.asdict(), right=bad_coords))
1415+
tables.edges.right = bad_coords
1416+
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
1417+
1418+
def test_migrations(self):
1419+
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)
1420+
1421+
tables = ts.dump_tables()
1422+
tables.populations.add_row()
1423+
tables.migrations.add_row(float('inf'), 1, time=0, node=0, source=0, dest=1)
14101424
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
14111425

1412-
def test_bad_site_positions(self):
1413-
ts = msprime.simulate(5, mutation_rate=1, random_seed=self.random_seed)
1426+
tables = ts.dump_tables()
1427+
tables.populations.add_row()
1428+
tables.migrations.add_row(0, float('nan'), time=0, node=0, source=0, dest=1)
1429+
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
1430+
1431+
tables = ts.dump_tables()
1432+
tables.populations.add_row()
1433+
tables.migrations.add_row(0, 1, time=float('nan'), node=0, source=0, dest=1)
1434+
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
1435+
1436+
def test_site_positions(self):
1437+
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)
14141438
tables = ts.dump_tables()
14151439
bad_pos = tables.sites.position.copy()
14161440
bad_pos[-1] = np.inf
1417-
tables.sites.set_columns(**dict(tables.sites.asdict(), position=bad_pos))
1441+
tables.sites.position = bad_pos
14181442
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
14191443

1420-
def test_bad_node_times(self):
1421-
ts = msprime.simulate(5, mutation_rate=1, random_seed=self.random_seed)
1444+
def test_node_times(self):
1445+
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)
14221446
tables = ts.dump_tables()
14231447
bad_times = tables.nodes.time.copy()
14241448
bad_times[-1] = np.inf
1425-
tables.nodes.set_columns(**dict(tables.nodes.asdict(), time=bad_times))
1449+
tables.nodes.time = bad_times
14261450
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
14271451

1428-
def test_bad_individual(self):
1429-
ts = msprime.simulate(12, mutation_rate=1, random_seed=self.random_seed)
1430-
ts = tsutil.insert_random_ploidy_individuals(ts, seed=self.random_seed)
1452+
def test_individual(self):
1453+
ts = msprime.simulate(12, mutation_rate=1, random_seed=42)
1454+
ts = tsutil.insert_random_ploidy_individuals(ts, seed=42)
14311455
self.assertGreater(ts.num_individuals, 1)
14321456
tables = ts.dump_tables()
14331457
bad_locations = tables.individuals.location.copy()
14341458
bad_locations[0] = np.inf
1435-
tables.individuals.set_columns(
1436-
**dict(tables.individuals.asdict(), location=bad_locations))
1437-
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
1459+
tables.individuals.location = bad_locations
1460+
ts = tables.tree_sequence()
14381461

14391462

14401463
class TestSimplifyTables(unittest.TestCase):

0 commit comments

Comments
 (0)