def test_debug(self): """ Test the debugging output of the MorseComplex """ self.setup() test_file = "mc_test_debug.txt" sys.stdout = open(test_file, "w") self.test_object = topopy.MorseComplex(debug=True, graph=self.graph) self.test_object.build(self.X, self.Y) sys.stdout.close() lines = ["Graph Preparation:", "Decomposition:"] with open(test_file, "r") as fp: debug_output = fp.read() for line in lines: self.assertIn(line, debug_output) os.remove(test_file) # Restore stdout sys.stdout = sys.__stdout__
def test_load_data_and_build(self): """ Tests that loading the same test data from file yields an equivalent result """ self.setup() test_object = topopy.MorseComplex() np.savetxt( "test_file.csv", np.hstack((self.X, np.atleast_2d(self.Y).T)), delimiter=",", header="x0,x1,y", ) test_object.load_data_and_build("test_file.csv", delimiter=",") os.remove("test_file.csv") self.assertDictEqual( self.test_object.merge_sequence, test_object.merge_sequence, "loading from file should produce the same merge sequence", ) self.assertDictEqual( dict(self.test_object.base_partitions), dict(test_object.base_partitions), "loading from file should produce the base partitions.", )
def setup(self): """ Setup function will create a fixed point set and parameter settings for testing different aspects of this library. """ self.X = generate_test_grid_2d(40) self.Y = gerber(self.X) self.graph = ngl.EmptyRegionGraph(max_neighbors=10) self.norm_x = {} scaler = sklearn.preprocessing.MinMaxScaler() self.norm_x["feature"] = scaler.fit_transform(np.atleast_2d(self.X)) self.norm_x["zscore"] = sklearn.preprocessing.scale(self.X, axis=0, with_mean=True, with_std=True, copy=True) self.norm_x["none"] = self.X # Methods covered here: # __init__ # build # __set_data self.test_object = topopy.MorseComplex(debug=False, graph=self.graph) self.test_object.build(self.X, self.Y) gold_path = os.path.join("topopy", "tests", "mc_gold.json") with open(gold_path, "r") as data_file: gold_json = data_file.read() gold_json = json.loads(gold_json) self.gold = gold_json
def test_get_partitions(self): """ Test the ability to retrieve partition labels for individual query points and the entire dataset if not specified """ self.setup() gold_stable_partitions = {} for i, max_label in enumerate(self.gold["Partitions"]): if max_label not in gold_stable_partitions: gold_stable_partitions[max_label] = [] gold_stable_partitions[max_label].append(i) partitions = self.test_object.get_partitions() self.assertEqual( 4, len(partitions), "The number of partitions without specifying the persistence " "should be the same as requesting the base (0) persistence level", ) self.assertDictEqual( gold_stable_partitions, partitions, "The base partitions of the stable manifolds should match", ) test_p = 0.5 merge_pattern = {} # Initialize every extrema to point to itself for merge in self.gold["Hierarchy"]: merge_pattern[merge["Dying"]] = merge["Dying"] # Now point to the correct label for the specified persistence for merge in self.gold["Hierarchy"]: if merge["Persistence"] < test_p: if merge["Surviving"] not in merge_pattern: merge_pattern[merge["Surviving"]] = merge["Surviving"] merge_pattern[merge["Dying"]] = merge_pattern[ merge["Surviving"]] gold_stable_partitions = {} for i, max_label in enumerate(self.gold["Partitions"]): max_label = merge_pattern[max_label] if max_label not in gold_stable_partitions: gold_stable_partitions[max_label] = [] gold_stable_partitions[max_label].append(i) partitions = self.test_object.get_partitions(test_p) self.assertEqual( 1, len(partitions), "The number of partitions without specifying the persistence " "should be the same as requesting the base (0) persistence level", ) self.assertDictEqual( gold_stable_partitions, partitions, "The partitions of the stable manifolds should match at the " "test level p={}".format(test_p), ) self.test_object = topopy.MorseComplex() partitions = self.test_object.get_partitions() self.assertEqual( {}, partitions, "Requesting partitions on an unbuilt object should return an " "empty dictionary", )