def test_exact_symmetry(self): r""" If two comparison objects are defined with swapped names of left and right sample sets, the distance should still be identical """ m1 = compP.comparison(self.int_set, self.left_set, self.right_set) m2 = compP.comparison(self.int_set, self.right_set, self.left_set) for dist in ['tv', 'mink', '2-norm', 'sqhell']: d1 = m1.value(dist) d2 = m2.value(dist) nptest.assert_almost_equal(d1 - d2, 0, 12, 'Distance %s not symmetric.' % dist) import scipy.spatial.distance as ds # should be able to overwrite and still get correct answer. for dist in ['tv', ds.cityblock]: m = compP.compare(self.left_set, self.right_set) d1 = m.value(dist) m.set_right(self.left_set) m.set_left(self.right_set) d2 = m.value(dist) nptest.assert_almost_equal(d1 - d2, 0, 12, 'Distance not symmetric.') # grabbing copies like this should also work. ll = m.get_left().copy() m.set_left(m.get_right()) m.set_right(ll) d2 = m.value(dist) nptest.assert_almost_equal(d1 - d2, 0, 12, 'Distance not symmetric.')
def test_metric(self): r""" There are a few ways these functions can get initialized. Here we test the varying permutations """ self.int_set = self.emulation_set compP.compare(self.left_set, self.right_set) compP.compare(self.left_set, self.right_set, 10) compP.comparison(self.int_set, self.left_set, self.right_set) compP.comparison(self.int_set)
def test_no_sample_set(self): r""" Make sure we can initialize the function in several permutations """ test_set = sample.sample_set(dim=self.dim) test_set.set_domain(self.domain) other_set = test_set.copy() self.mtrc = compP.comparison(test_set) self.mtrc = compP.comparison(test_set, None) self.mtrc = compP.comparison(test_set, None, other_set) self.mtrc = compP.comparison(test_set, other_set, None) self.mtrc = compP.comparison(test_set, None, None)
def test_missing_domain(self): r""" Make sure we can initialize the function in several permutations if the domain is missing from the comparison set """ test_set = sample.sample_set(dim=self.dim) # no domain info other_set = test_set.copy() # has domain info other_set.set_domain(self.domain) mm = compP.comparison(None, other_set) mm = compP.comparison(None, None, other_set) mm = compP.comparison(test_set, other_set, None) mm = compP.comparison(test_set, None, other_set) mm = compP.comparison(test_set, None, None) mm.set_left(other_set) try: # we are missing a set, so this should fail mm.check_domain() except AttributeError: pass self.mtrc.set_right(other_set) self.mtrc.check_domain() # now we expect it to pass # the following should error out because not enough information try: self.mtrc = compP.comparison(None) except AttributeError: pass try: self.mtrc = compP.comparison(None, None, test_set) except AttributeError: pass try: self.mtrc = compP.comparison(test_set, None, other_set) except AttributeError: pass
def test_passed_ptrs(self): r""" Passing incorrect pointer shape raises errors """ ptr = np.ones(self.num + 1) try: compP.comparison(self.emulation_set, self.left_set, self.right_set, ptr, None) except AttributeError: pass try: compP.comparison(self.emulation_set, self.left_set, self.right_set, None, ptr) except AttributeError: pass try: compP.comparison(self.emulation_set, self.left_set, self.right_set, ptr, np.ones(self.num)) except AttributeError: pass try: compP.comparison(self.emulation_set, self.left_set, self.right_set, np.ones(self.num), ptr) except AttributeError: pass
def test_missing_vols(self): r""" Check that correct errors get raised """ mm = compP.comparison(self.int_set, self.left_set, self.right_set) try: mm.get_left().set_volumes(None) mm.estimate_left_densities() except AttributeError: pass
def test_dimension(self): r""" Check that improperly setting dimension raises warning. """ dim = self.dim + 1 values = np.ones((200, dim)) emulation_set = sample.sample_set(dim=dim) emulation_set.set_values(values) emulation_set.set_domain(np.tile([0, 1], [dim, 1])) try: compP.comparison(sample_set_left=self.left_set, sample_set_right=self.right_set, comparison_sample_set=emulation_set) except sample.dim_not_matching: pass try: compP.comparison(sample_set_left=self.left_set, sample_set_right=None, comparison_sample_set=emulation_set) except sample.dim_not_matching: pass try: compP.comparison(sample_set_left=self.left_set, sample_set_right=None, comparison_sample_set=emulation_set) except sample.dim_not_matching: pass # if missing domain info, should be able to infer self.emulation_set._domain = None compP.comparison(sample_set_left=None, sample_set_right=self.right_set, comparison_sample_set=self.emulation_set) try: # if not enough info, raise error self.emulation_set._domain = None compP.comparison(sample_set_left=None, sample_set_right=None, comparison_sample_set=self.emulation_set) except AttributeError: pass
def setUp(self): self.dim = 3 self.num1, self.num2, self.num = 100, 100, 500 self.emulation_set = sample.sample_set(dim=self.dim) self.left_set = unit_center_set(self.dim, self.num1, 0.5) self.right_set = unit_center_set(self.dim, self.num2, 0.5) values = np.ones((self.num, self.dim)) self.emulation_set.set_values(values) self.domain = np.tile([0, 1], [self.dim, 1]) self.emulation_set.set_domain(self.domain) self.left_set.set_domain(self.domain) self.right_set.set_domain(self.domain) self.mtrc = compP.comparison(sample_set_left=self.left_set, sample_set_right=self.right_set, comparison_sample_set=self.emulation_set)
def test_missing_probs(self): r""" Check that correct errors get raised """ mm = compP.comparison(self.int_set, self.left_set.copy(), self.right_set) try: mm.get_left().set_probabilities(None) mm.estimate_left_densities() except AttributeError: pass mm.set_left(self.left_set) # if local probs go missing, we should still be fine mm.get_left()._probabilities_local = None mm.estimate_left_densities()
def test_set_domain(self): r""" Check that improperly setting domain raises warning. """ test_set = self.emulation_set.copy() test_set.set_domain(test_set.get_domain() + 0.01) # all the ways to initialize the class test_metr = [ compP.comparison(self.emulation_set), compP.comparison(self.emulation_set, sample_set_right=self.right_set), compP.comparison(self.emulation_set, sample_set_left=self.left_set) ] # setting one of the missing properties for mm in test_metr: test_funs = [mm.set_right, mm.set_left] for fun in test_funs: try: fun(test_set) except sample.domain_not_matching: pass # overwriting integration sample set test_metr = [ compP.comparison(None, sample_set_right=self.right_set), compP.comparison(None, sample_set_left=self.left_set), compP.comparison(self.emulation_set, self.left_set, self.right_set) ] # setting one of the missing properties for mm in test_metr: try: mm.set_comparison(test_set) except sample.domain_not_matching: pass try: # should catch problems on initialization too mm = compP.comparison(self.emulation_set, self.left_set, test_set) except sample.domain_not_matching: pass try: # should catch problems on initialization too mm = compP.comparison(self.emulation_set, test_set, self.right_set) except sample.domain_not_matching: pass
def test_set_emulation(self): r""" Different ways to set emulation set. """ mm = compP.comparison(None, self.left_set, None) emulation_set = self.emulation_set.copy() mm.set_comparison(emulation_set) nptest.assert_array_equal(mm.get_comparison()._values, self.emulation_set._values) mm.set_comparison_sample_set(emulation_set) nptest.assert_array_equal(mm.get_comparison()._values, self.emulation_set._values) try: # None should trigger error mm._comparison_sample_set = None mm.estimate_densities() except AttributeError: pass # the following syntax to should be able to run mm.set_comparison(emulation_set) mm.set_right(self.right_set) mm.estimate_densities() mm.set_left(self.left_set) mm.estimate_densities()