test_DSFMap.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Unit tests for Disjoint Set Forest.
9 Author: Frank Dellaert & Varun Agrawal & John Lambert
10 """
11 # pylint: disable=invalid-name, no-name-in-module, no-member
12 
13 from __future__ import print_function
14 
15 import unittest
16 from typing import Tuple
17 
18 from gtsam import DSFMapIndexPair, IndexPair, IndexPairSetAsArray
19 from gtsam.utils.test_case import GtsamTestCase
20 
21 
23  """Tests for DSFMap."""
24 
25  def test_all(self) -> None:
26  """Test everything in DFSMap."""
27 
28  def key(index_pair) -> Tuple[int, int]:
29  return index_pair.i(), index_pair.j()
30 
31  dsf = DSFMapIndexPair()
32  pair1 = IndexPair(1, 18)
33  self.assertEqual(key(dsf.find(pair1)), key(pair1))
34  pair2 = IndexPair(2, 2)
35 
36  # testing the merge feature of dsf
37  dsf.merge(pair1, pair2)
38  self.assertEqual(key(dsf.find(pair1)), key(dsf.find(pair2)))
39 
40  def test_sets(self) -> None:
41  """Ensure that pairs are merged correctly during Union-Find.
42 
43  An IndexPair (i,k) representing a unique key might represent the
44  k'th detected keypoint in image i. For the data below, merging such
45  measurements into feature tracks across frames should create 2 distinct sets.
46  """
47  dsf = DSFMapIndexPair()
48  dsf.merge(IndexPair(0, 1), IndexPair(1, 2))
49  dsf.merge(IndexPair(0, 1), IndexPair(3, 4))
50  dsf.merge(IndexPair(4, 5), IndexPair(6, 8))
51  sets = dsf.sets()
52 
53  merged_sets = set()
54 
55  for i in sets:
56  set_keys = []
57  s = sets[i]
58  for val in IndexPairSetAsArray(s):
59  set_keys.append((val.i(), val.j()))
60  merged_sets.add(tuple(set_keys))
61 
62  # fmt: off
63  expected_sets = {
64  ((0, 1), (1, 2), (3, 4)), # set 1
65  ((4, 5), (6, 8)) # set 2
66  }
67  # fmt: on
68  assert expected_sets == merged_sets
69 
70 
71 if __name__ == "__main__":
72  unittest.main()
const gtsam::Symbol key('X', 0)
DSFMap< IndexPair > DSFMapIndexPair
Definition: DSFMap.h:131
IndexPairVector IndexPairSetAsArray(IndexPairSet &set)
Definition: DSFMap.h:128
Small utility class for representing a wrappable pairs of ints.
Definition: DSFMap.h:117
Definition: pytypes.h:2030


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45