def verify(self, ts, reference_sets, focal=None): if focal is None: focal = [u for refset in reference_sets for u in refset] A1 = naive_genealogical_nearest_neighbours(ts, focal, reference_sets) A2 = tsutil.genealogical_nearest_neighbours(ts, focal, reference_sets) A3 = ts.genealogical_nearest_neighbours(focal, reference_sets) A4 = ts.genealogical_nearest_neighbours(focal, reference_sets, num_threads=3) A5 = windowed_genealogical_nearest_neighbours(ts, focal, reference_sets) assert np.array_equal(A3, A4) assert A1.shape == A2.shape assert A1.shape == A3.shape assert np.allclose(A1, A2) assert np.allclose(A1, A3) mask = ~np.isnan(A5) assert np.sum(mask) > 0 or ts.num_edges == 0 assert np.allclose(A1[mask], A5[mask]) assert np.allclose(A5[mask], A2[mask]) assert np.allclose(A5[mask], A3[mask]) if ts.num_edges > 0 and all(ts.node(u).is_sample() for u in focal): # When the focal nodes are samples, we can assert some stronger properties. assert np.allclose(np.sum(A1, axis=1), 1) assert np.allclose(np.sum(A5, axis=1), 1) return A1
def test_simple_example_internal_focal_node(self): ts = tskit.load_text(nodes=io.StringIO(self.small_tree_ex_nodes), edges=io.StringIO(self.small_tree_ex_edges), strict=False) focal = [7] # An internal node reference_sets = [[4, 0, 1], [2, 3]] GNN = naive_genealogical_nearest_neighbours(ts, focal, reference_sets) self.assertTrue(np.allclose(GNN[0], np.array([1.0, 0.0]))) GNN = tsutil.genealogical_nearest_neighbours(ts, focal, reference_sets) self.assertTrue(np.allclose(GNN[0], np.array([1.0, 0.0]))) GNN = ts.genealogical_nearest_neighbours(focal, reference_sets) self.assertTrue(np.allclose(GNN[0], np.array([1.0, 0.0]))) focal = [8] # The root GNN = naive_genealogical_nearest_neighbours(ts, focal, reference_sets) self.assertTrue(np.allclose(GNN[0], np.array([0.6, 0.4]))) GNN = tsutil.genealogical_nearest_neighbours(ts, focal, reference_sets) self.assertTrue(np.allclose(GNN[0], np.array([0.6, 0.4]))) GNN = ts.genealogical_nearest_neighbours(focal, reference_sets) self.assertTrue(np.allclose(GNN[0], np.array([0.6, 0.4])))
def verify(self, ts, reference_sets, focal=None): if focal is None: focal = [u for refset in reference_sets for u in refset] A1 = naive_genealogical_nearest_neighbours(ts, focal, reference_sets) A2 = tsutil.genealogical_nearest_neighbours(ts, focal, reference_sets) A3 = ts.genealogical_nearest_neighbours(focal, reference_sets) A4 = ts.genealogical_nearest_neighbours(focal, reference_sets, num_threads=3) self.assertTrue(np.array_equal(A3, A4)) self.assertEqual(A1.shape, A2.shape) self.assertEqual(A1.shape, A3.shape) self.assertTrue(np.allclose(A1, A2)) self.assertTrue(np.allclose(A1, A3)) if ts.num_edges > 0 and all(ts.node(u).is_sample() for u in focal): # When the focal nodes are samples, we can assert some stronger properties. self.assertTrue(np.allclose(np.sum(A1, axis=1), 1)) return A1
def verify(self, ts, reference_sets, focal=None): if focal is None: focal = [u for refset in reference_sets for u in refset] A1 = naive_genealogical_nearest_neighbours(ts, focal, reference_sets) A2 = tsutil.genealogical_nearest_neighbours(ts, focal, reference_sets) A3 = ts.genealogical_nearest_neighbours(focal, reference_sets) A4 = ts.genealogical_nearest_neighbours(focal, reference_sets, num_threads=3) self.assertTrue(np.array_equal(A3, A4)) self.assertEqual(A1.shape, A2.shape) self.assertEqual(A1.shape, A3.shape) self.assertTrue(np.allclose(A1, A2)) self.assertTrue(np.allclose(A1, A3)) if all(ts.node(u).is_sample() for u in focal): # When the focal nodes are samples, we can assert some stronger properties. fully_rooted = True for tree in ts.trees(): if tree.num_roots > 1: fully_rooted = False break if fully_rooted: self.assertTrue(np.allclose(np.sum(A1, axis=1), 1)) else: all_refs = [u for refset in reference_sets for u in refset] # Any node that hits a root before meeting a descendent of the reference # nodes must have total zero. coalescence_found = np.array([False for _ in all_refs]) for tree in ts.trees(tracked_samples=all_refs): for j, u in enumerate(focal): while u != tskit.NULL: if tree.num_tracked_samples(u) > 1: coalescence_found[j] = True break u = tree.parent(u) self.assertTrue( np.allclose(np.sum(A1[coalescence_found], axis=1), 1)) # Anything where there's no coalescence, ever is zero by convention. self.assertTrue( np.allclose( np.sum(A1[np.logical_not(coalescence_found)], axis=1), 0)) return A1