Skip to content

Commit

Permalink
Merge pull request #352 from jeromekelleher/allow-location-nans
Browse files Browse the repository at this point in the history
Allow location nans
  • Loading branch information
jeromekelleher authored Sep 1, 2019
2 parents 47decb2 + ba0923b commit 8759e8c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 40 deletions.
14 changes: 6 additions & 8 deletions c/tests/test_trees.c
Original file line number Diff line number Diff line change
Expand Up @@ -1849,17 +1849,15 @@ test_simplest_individuals(void)
CU_ASSERT_EQUAL_FATAL(ret, TSK_ERR_INDIVIDUAL_OUT_OF_BOUNDS);
tsk_treeseq_free(&ts);

/* NaN/ifinity values are allowed in locations they do not
* affect the integrity of the model. */
tables.individuals.location[0] = NAN;
ret = tsk_treeseq_init(&ts, &tables, load_flags);
CU_ASSERT_EQUAL(ret, TSK_ERR_SPATIAL_LOCATION_NONFINITE);
tsk_treeseq_free(&ts);
tables.individuals.location[0] = 0.25;

tables.individuals.location[2] = INFINITY;
ret = tsk_treeseq_init(&ts, &tables, load_flags);
CU_ASSERT_EQUAL(ret, TSK_ERR_SPATIAL_LOCATION_NONFINITE);
CU_ASSERT_EQUAL(ret, 0);
ret = tsk_treeseq_get_individual(&ts, 0, &individual);
CU_ASSERT_EQUAL_FATAL(ret, 0);
CU_ASSERT(! isfinite(individual.location[0]));
tsk_treeseq_free(&ts);
tables.individuals.location[0] = 0.25;

tsk_table_collection_free(&tables);
}
Expand Down
3 changes: 0 additions & 3 deletions c/tskit/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ tsk_strerror_internal(int err)
case TSK_ERR_GENOME_COORDS_NONFINITE:
ret = "Genome coordinates must be finite numbers";
break;
case TSK_ERR_SPATIAL_LOCATION_NONFINITE:
ret = "Location values must be finite numbers";
break;

/* Edge errors */
case TSK_ERR_NULL_PARENT:
Expand Down
1 change: 0 additions & 1 deletion c/tskit/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ of tskit.
#define TSK_ERR_PROVENANCE_OUT_OF_BOUNDS -209
#define TSK_ERR_TIME_NONFINITE -210
#define TSK_ERR_GENOME_COORDS_NONFINITE -211
#define TSK_ERR_SPATIAL_LOCATION_NONFINITE -212

/* Edge errors */
#define TSK_ERR_NULL_PARENT -300
Expand Down
8 changes: 0 additions & 8 deletions c/tskit/tables.c
Original file line number Diff line number Diff line change
Expand Up @@ -5672,14 +5672,6 @@ tsk_table_collection_check_integrity(tsk_table_collection_t *self, tsk_flags_t o
goto out;
}

/* Individuals */
for (j = 0; j < self->individuals.location_offset[self->individuals.num_rows]; j++) {
if (! isfinite(self->individuals.location[j])) {
ret = TSK_ERR_SPATIAL_LOCATION_NONFINITE;
goto out;
}
}

/* Nodes */
for (j = 0; j < self->nodes.num_rows; j++) {
node_time = self->nodes.time[j];
Expand Down
10 changes: 7 additions & 3 deletions python/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
--------------------
[0.2.2] - xxxx-xx-xx
[0.2.2] - 2019-09-01
--------------------

In development.
Minor bugfix release.

Relaxes overly-strict input requirements on individual location data that
caused some SLiM tree sequences to fail loading in version 0.2.1
(see :issue:`351`).

**New features**

- Add log_time height scaling option for drawing SVG trees
- Add log_time height scaling option for drawing SVG trees
(:user:`marianne-aspbury`). See :pr:`324` and :issue:`303`.

**Bugfixes**
Expand Down
55 changes: 39 additions & 16 deletions python/tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,45 +1396,68 @@ def test_round_trip(self):
self.assertTrue(
all(a == b for a, b in zip(a.tables, b.tables) if a[0] != 'provenances'))

def test_bad_edge_coords(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=self.random_seed)

class TestNanDoubleValues(unittest.TestCase):
"""
In some tables we need to guard against NaN/infinite values in the input.
"""

def test_edge_coords(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)

tables = ts.dump_tables()
bad_coords = tables.edges.left + float('inf')
tables.edges.set_columns(**dict(tables.edges.asdict(), left=bad_coords))
tables.edges.left = bad_coords
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

tables = ts.dump_tables()
bad_coords = tables.edges.right + float('nan')
tables.edges.set_columns(**dict(tables.edges.asdict(), right=bad_coords))
tables.edges.right = bad_coords
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

def test_migrations(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)

tables = ts.dump_tables()
tables.populations.add_row()
tables.migrations.add_row(float('inf'), 1, time=0, node=0, source=0, dest=1)
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

def test_bad_site_positions(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=self.random_seed)
tables = ts.dump_tables()
tables.populations.add_row()
tables.migrations.add_row(0, float('nan'), time=0, node=0, source=0, dest=1)
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

tables = ts.dump_tables()
tables.populations.add_row()
tables.migrations.add_row(0, 1, time=float('nan'), node=0, source=0, dest=1)
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

def test_site_positions(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)
tables = ts.dump_tables()
bad_pos = tables.sites.position.copy()
bad_pos[-1] = np.inf
tables.sites.set_columns(**dict(tables.sites.asdict(), position=bad_pos))
tables.sites.position = bad_pos
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

def test_bad_node_times(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=self.random_seed)
def test_node_times(self):
ts = msprime.simulate(5, mutation_rate=1, random_seed=42)
tables = ts.dump_tables()
bad_times = tables.nodes.time.copy()
bad_times[-1] = np.inf
tables.nodes.set_columns(**dict(tables.nodes.asdict(), time=bad_times))
tables.nodes.time = bad_times
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)

def test_bad_individual(self):
ts = msprime.simulate(12, mutation_rate=1, random_seed=self.random_seed)
ts = tsutil.insert_random_ploidy_individuals(ts, seed=self.random_seed)
def test_individual(self):
ts = msprime.simulate(12, mutation_rate=1, random_seed=42)
ts = tsutil.insert_random_ploidy_individuals(ts, seed=42)
self.assertGreater(ts.num_individuals, 1)
tables = ts.dump_tables()
bad_locations = tables.individuals.location.copy()
bad_locations[0] = np.inf
tables.individuals.set_columns(
**dict(tables.individuals.asdict(), location=bad_locations))
self.assertRaises(_tskit.LibraryError, tables.tree_sequence)
tables.individuals.location = bad_locations
ts = tables.tree_sequence()


class TestSimplifyTables(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion python/tskit/_version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Definitive location for the version number.
# During development, should be x.y.z.devN
tskit_version = "0.2.2.dev0"
tskit_version = "0.2.2"

0 comments on commit 8759e8c

Please sign in to comment.