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.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.graph = ngl.EmptyRegionGraph(max_neighbors=10) self.ct = topopy.ContourTree(graph=self.graph, debug=False) self.ct.build(self.X, self.Y)
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_neighbors(self): """ Tests the neighbors function in both settings, that is where an index is supplied and when it is not. This does not use an input neighborhood graph, thus NGL must prune the complete graph in this case. """ self.setup() graph_rep = nglpy.EmptyRegionGraph(max_neighbors=self.max_neighbors, beta=self.beta, relaxed=self.relaxed, p=self.p, discrete_steps=self.discrete_steps) graph_rep.build(self.points) expected_graph = { 0: (1, ), 1: (0, 2, 4), 2: (1, 3), 3: (2, ), 4: (1, ) } for i in range(len(self.points)): expected = list(expected_graph[i]) actual = sorted(graph_rep.neighbors(i)) msg = "\nNode {} Connectivity:".format(i) msg += "\n\texpected: {}\n\tactual: {} ".format(expected, actual) self.assertEqual(expected, actual, msg) self.assertEqual(graph_rep.neighbors(), expected_graph)
def test_empty(self): """ Tests handling of empty data, we just want to make sure nothing breaks terribly. """ self.setup() graph_rep = nglpy.EmptyRegionGraph(max_neighbors=self.max_neighbors, beta=self.beta, relaxed=True, p=self.p, discrete_steps=self.discrete_steps) graph_rep.build([]) expected_graph = {} self.assertEqual(graph_rep.neighbors(), expected_graph)
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
def __init__( self, graph=None, gradient="steepest", normalization=None, aggregator=None, debug=False, ): super(TopologicalObject, self).__init__() self.reset() if graph is None: graph = ngl.EmptyRegionGraph() self.graph = graph self.gradient = gradient self.normalization = normalization self.debug = debug self.aggregator = aggregator
def get_cbsg(df, beta=0): graph = nx.Graph() graph.add_nodes_from(df.iterrows()) point_set = np.array(df[["x", "y"]]) point_set = point_set + 0.0001 # aGraph = nglpy.Graph(point_set, "beta skeleton", 9, beta) aGraph = nglpy.EmptyRegionGraph(max_neighbors=9, relaxed=False, beta=beta) aGraph.build(point_set) d = aGraph.neighbors() for key, value in d.items(): for v in value: graph.add_edge( key, v, weight=euclidean(df.loc[key, ["x", "y"]], df.loc[v, ["x", "y"]]) ) return graph
def test_aggregation(self): # Since the default function can change, here we will only test # that the correct number of each array is reported X = np.ones((11, 2)) X[10] = [0, 0] Y = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100]) warnings.filterwarnings("ignore") x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y) self.assertEqual( 2, len(x), "aggregate_duplicates should return a list of " "unique items in X.", ) self.assertEqual( 2, len(y), "aggregate_duplicates should return the aggregated " "values of Y for each unique item in X.", ) # Next, we will test each of the string aggregation function # names on scalar Y values x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "min") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [100, 0]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "max") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [100, 9]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "mean") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [100, 4.5]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "average") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [100, 4.5]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "median") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [100, 4.5]) # Next, we will test each of the string aggregation function # names on vector Y values Y = np.array( [ [0, 9], [1, 8], [2, 7], [3, 6], [4, 5], [5, 4], [6, 3], [7, 2], [8, 1], [9, 0], [100, 0], ] ) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "min") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [0, 0]]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "max") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [9, 9]]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "mean") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [4.5, 4.5]]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "median") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [4.5, 4.5]]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "first") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [0, 9]]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y, "last") self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [9, 0]]) # Testing custom callable aggregator x, y = topopy.TopologicalObject.aggregate_duplicates( X, Y, lambda x: x[0] ) self.assertListEqual(x.tolist(), [[0, 0], [1, 1]]) self.assertListEqual(y.tolist(), [[100, 0], [0, 9]]) warnings.filterwarnings("always") # Testing an invalid aggregator with warnings.catch_warnings(record=True) as w: x, y = topopy.TopologicalObject.aggregate_duplicates( X, Y, "invalid" ) self.assertTrue(issubclass(w[-1].category, UserWarning)) self.assertEqual( 'Aggregator "invalid" not understood. Skipping sample ' 'aggregation.', str(w[-1].message), ) self.assertListEqual(x.tolist(), X.tolist()) self.assertListEqual(y.tolist(), Y.tolist()) # Testing aggregator on non-duplicate data X = np.array([[0, 0], [0, 1]]) Y = np.array([0, 1]) x, y = topopy.TopologicalObject.aggregate_duplicates(X, Y) self.assertListEqual(x.tolist(), X.tolist()) self.assertListEqual(y.tolist(), Y.tolist()) warnings.filterwarnings("ignore") # Testing use of the aggregator in the check_duplicates function X = np.ones((11, 2)) X[10] = [0, 0] Y = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100]) to = topopy.TopologicalObject() self.assertRaises(ValueError, to.build, **{"X": X, "Y": Y}) graph = ngl.EmptyRegionGraph(max_neighbors=10) to = topopy.TopologicalObject(aggregator="mean", graph=graph) to.build(X, Y) warnings.filterwarnings("always")