def test_precomputed(self): mapper = KeplerMapper() X = np.random.rand(100, 2) X_pdist = distance.squareform(distance.pdist(X, metric="euclidean")) lens = mapper.fit_transform(X_pdist) graph = mapper.map( lens, X=X_pdist, cover=Cover(n_cubes=10, perc_overlap=0.8), clusterer=cluster.DBSCAN(metric="precomputed", min_samples=3), precomputed=True, ) graph2 = mapper.map( lens, X=X, cover=Cover(n_cubes=10, perc_overlap=0.8), clusterer=cluster.DBSCAN(metric="euclidean", min_samples=3), ) assert graph["links"] == graph2["links"] assert graph["nodes"] == graph2["nodes"] assert graph["simplices"] == graph2["simplices"]
def test_new_api_old_defaults(self): mapper = KeplerMapper() data = np.random.rand(100, 10) lens = mapper.fit_transform(data) _ = mapper.map(lens, data, nr_cubes=10) c2 = mapper.cover assert c2.perc_overlap == 0.1 _ = mapper.map(lens, data, overlap_perc=0.1) c2 = mapper.cover assert c2.n_cubes == 10
def test_warn_old_api(self): """ Confirm old api works but throws warning """ mapper = KeplerMapper() data = np.random.rand(100, 10) lens = mapper.fit_transform(data) with pytest.deprecated_call(): graph = mapper.map(lens, data, nr_cubes=10) with pytest.deprecated_call(): graph = mapper.map(lens, data, overlap_perc=10) with pytest.deprecated_call(): graph = mapper.map(lens, data, nr_cubes=10, overlap_perc=0.1)
def test_map_custom_lens(self): # I think that map currently requires fit_transform to be called first mapper = KeplerMapper() data = np.random.rand(100, 2) #import pdb; pdb.set_trace() graph = mapper.map(data) assert graph["meta_graph"] == "custom"
def test_agglomerative_clustering(self): mapper = KeplerMapper() X = np.random.rand(100, 2) lens = mapper.fit_transform(X) graph = mapper.map(lens, X, clusterer=cluster.AgglomerativeClustering())
def test_convertions(self): mapper = KeplerMapper(verbose=0) data = np.random.rand(100, 2) graph = mapper.map(data) g = to_networkx(graph) assert isinstance(g, nx.Graph)
def test_membership(self): mapper = KeplerMapper(verbose=0) data = np.random.rand(100, 2) graph = mapper.map(data) n, m = list(graph["nodes"].items())[0] g = to_networkx(graph) assert g.nodes[n]["membership"] == m
def profile(): num_sets = 100 blob_size = 1000 n_cubes = 10 overlap = 0.2 blob_list = [] for i in range(num_sets): data, _ = datasets.make_blobs(blob_size) blob_list.append(data) mapper = KeplerMapper(verbose=0) pr = cProfile.Profile() pr.enable() for data in blob_list: lens = mapper.fit_transform(data) graph = mapper.map(lens, data, cover=Cover(n_cubes=n_cubes, perc_overlap=overlap)) pr.disable() s = io.StringIO() sortby = "cumulative" ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats(sortby) ps.print_stats("kmapper") print( "Ran {} blobs of size {} with params (n_cubes:{}\toverlap:{})".format( num_sets, blob_size, n_cubes, overlap ) ) print(s.getvalue())
def profile(): num_sets = 100 blob_size = 1000 nr_cubes = 10 overlap = 0.2 blob_list = [] for i in range(num_sets): data, _ = datasets.make_blobs(blob_size) blob_list.append(data) mapper = KeplerMapper(verbose=0) pr = cProfile.Profile() pr.enable() for data in blob_list: lens = mapper.fit_transform(data) graph = mapper.map(lens, data, nr_cubes=nr_cubes, overlap_perc=overlap) pr.disable() s = io.StringIO() sortby = 'cumulative' ps = pstats.Stats(pr, stream=s).strip_dirs().sort_stats(sortby) ps.print_stats("kmapper") print("Ran {} blobs of size {} with params (nr_cubes:{}\toverlap:{})".format(num_sets, blob_size, nr_cubes, overlap)) print(s.getvalue())
def test_format_mapper_data(self, jinja_env): mapper = KeplerMapper() data, labels = make_circles(1000, random_state=0) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) color_function = lens[:, 0] inverse_X = data projected_X = lens projected_X_names = ["projected_%s" % (i) for i in range(projected_X.shape[1])] inverse_X_names = ["inverse_%s" % (i) for i in range(inverse_X.shape[1])] custom_tooltips = np.array(["customized_%s" % (l) for l in labels]) graph_data = format_mapper_data( graph, color_function, inverse_X, inverse_X_names, projected_X, projected_X_names, custom_tooltips, jinja_env, ) # print(graph_data) # Dump to json so we can easily tell what's in it. graph_data = json.dumps(graph_data) # TODO test more properties! assert "name" in graph_data assert """cube2_cluster0""" in graph_data assert """projected_0""" in graph_data assert """inverse_0""" in graph_data assert """customized_""" in graph_data
def test_color_function_deprecated_replaced(): km = KeplerMapper() X, labels = make_circles(1000, random_state=0) lens = km.fit_transform(X, projection=[0]) color_values = lens[:, 0] sc = km.map(lens, X) X_names=[] lens_names=[] custom_tooltips = np.array(["customized_%s" % (l) for l in labels]) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # TODO: plotlyviz.plotlyviz # plotlyviz.get_mapper_graph json_graph, mapper_summary, colorf_distribution = get_mapper_graph(sc, color_function=color_values) _test_raised_deprecation_warning(w) # plotlyviz.scomplex_to_graph _ = scomplex_to_graph( simplicial_complex=sc, color_function=color_values, X=X, X_names=X_names, lens=lens, lens_names=lens_names, custom_tooltips=custom_tooltips, colorscale=default_colorscale, ) _test_raised_deprecation_warning(w)
def test_map_custom_lens(self): # I think that map currently requires fit_transform to be called first mapper = KeplerMapper() data = np.random.rand(100, 2) graph = mapper.map(data) assert graph["meta_data"]["projection"] == "custom" assert graph["meta_data"]["scaler"] == "None"
def test_wrong_id(self): mapper = KeplerMapper(verbose=1) data = np.random.rand(100, 2) graph = mapper.map(data) mems = mapper.data_from_cluster_id("new node", graph, data) np.testing.assert_array_equal(mems, np.array([]))
def test_affinity_prop_clustering(self): mapper = KeplerMapper() X = np.random.rand(100, 2) lens = mapper.fit_transform(X) graph = mapper.map(lens, X, clusterer=cluster.AffinityPropagation())
def test_runs_with_logging_0(self, capsys): mapper = KeplerMapper(verbose=0) data = np.random.rand(100, 2) graph = mapper.map(data) captured = capsys.readouterr() assert captured[0] == ""
def test_complete_pipeline(self, CoverClass): # TODO: add a mock that asserts the cover was called appropriately.. or test number of cubes etc. data, _ = datasets.make_circles() data = data.astype(np.float64) mapper = KeplerMapper() graph = mapper.map(data, cover=CoverClass()) mapper.visualize(graph)
def test_visualize_graph_with_cluster_stats_above_below(self): mapper = KeplerMapper() X = np.ones((1000, 3)) ids = np.random.choice(20, 1000) X[ids, 0] = 10 lens = mapper.fit_transform(X, projection=[0]) graph = mapper.map(lens, X) output = mapper.visualize(graph, X=X, lens=X)
def test_BasicCover(): # TODO: add a mock that asserts the cover was called appropriately.. or test number of cubes etc. data, _ = datasets.make_circles() data = data.astype(np.float64) mapper = KeplerMapper() graph = mapper.map(data) mapper.visualize(graph)
def test_format_meta_with_meta(self): mapper = KeplerMapper() data = np.random.rand(1000, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) cm = "My custom_meta" fmt = format_meta(graph, cm) assert fmt["custom_meta"] == cm
def test_lens_names(self): mapper = KeplerMapper() data = np.random.rand(100, 5) lens = mapper.fit_transform(data) graph = mapper.map(lens, data) template = mapper.visualize(graph=graph) template2 = mapper.visualize(graph=graph, lens_names=[]) assert template == template2
def test_visualize_one_color_function(self): mapper = KeplerMapper() data, labels = make_circles(1000, random_state=0) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) color_values = lens[:, 0] mapper.visualize(graph, color_values=color_values, color_function_name=["hotdog"])
def test_visualize_standalone(self): # visualize will run on a fresh mapper object mapper = KeplerMapper() data = np.random.rand(100, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) new_mapper = KeplerMapper() viz = new_mapper.visualize(graph)
def test_file_not_written(self, tmpdir): mapper = KeplerMapper() file = tmpdir.join('output.html') data = np.random.rand(1000, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) viz = mapper.visualize(graph, path_html=file.strpath, save_file=False) assert len(tmpdir.listdir()) == 0, "file was never written to"
def test_members_from_id(self): mapper = KeplerMapper(verbose=1) data = np.random.rand(100, 2) ids = np.random.choice(10, 100) data[ids] = 2 graph = mapper.map(data) graph["nodes"]["new node"] = ids mems = mapper.data_from_cluster_id("new node", graph, data) np.testing.assert_array_equal(data[ids], mems)
def test_format_meta(self): mapper = KeplerMapper() data = np.random.rand(1000, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) assert ("<p>%s</p>" % (len(graph["nodes"])) in format_meta(graph)) assert ("<h3>Description</h3>\n<p>A short description</p>" in format_meta(graph, custom_meta=[("Description", "A short description")]))
def test_format_meta_with_meta(self): mapper = KeplerMapper() data = np.random.rand(1000, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) color_function_name = ["Row number"] node_color_function = "mean" cm = "My custom_meta" fmt = _format_meta(graph, color_function_name, node_color_function, cm) assert fmt["custom_meta"] == cm
def test_file_not_written(self, tmpdir): mapper = KeplerMapper(verbose=1) file = tmpdir.join("output.html") data = np.random.rand(1000, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) viz = mapper.visualize(graph, path_html=file.strpath, save_file=False) assert len(tmpdir.listdir()) == 0, "file was never written to"
def test_no_warn_normally(self, recwarn): """ Confirm that deprecation warnings behave as expected""" mapper = KeplerMapper() data = np.random.rand(100, 10) lens = mapper.fit_transform(data) warnings.simplefilter("always") graph = mapper.map(lens, data) assert len(recwarn) == 0 assert DeprecationWarning not in recwarn
def test_file_written(self, tmpdir): mapper = KeplerMapper() file = tmpdir.join("output.html") data = np.random.rand(1000, 2) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) viz = mapper.visualize(graph, path_html=file.strpath) assert file.read() == viz assert len(tmpdir.listdir()) == 1, "file was written to"
def test_no_color_values_many_color_function_exception(self): mapper = KeplerMapper() data, labels = make_circles(1000, random_state=0) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) with pytest.raises(Exception) as excinfo: color_values = mapper.visualize( graph, color_values=None, color_function_name=["hotdog", "hotdog", "hotdiggitydog"], ) assert "Refusing to proceed" in str(excinfo.value)
def test_visualize_multiple_node_color_functions(self): """ convenience test for generating a vis with multiple node_color_values but 1d color_values""" mapper = KeplerMapper() data, labels = make_circles(1000, random_state=0) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) color_values = lens[:, 0] mapper.visualize( graph, color_values=color_values, color_function_name="hotdog", node_color_function=["mean", "std", "median", "max"], )
def test_color_function_deprecated_replaced(self, default_colorscale): mapper = KeplerMapper() data, labels = make_circles(1000, random_state=0) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) color_values = lens[:, 0] node_color_function = "mean" inverse_X = data projected_X = lens projected_X_names = [ "projected_%s" % (i) for i in range(projected_X.shape[1]) ] inverse_X_names = [ "inverse_%s" % (i) for i in range(inverse_X.shape[1]) ] custom_tooltips = np.array(["customized_%s" % (l) for l in labels]) # https://docs.python.org/3/library/warnings.html#testing-warnings with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # kmapper.visualize html = mapper.visualize(graph, color_function=lens, color_function_name="lens[:, 0]") _test_raised_deprecation_warning(w) # visuals._format_mapper_data graph_data = _format_mapper_data( graph=graph, color_function=color_values, node_color_function=node_color_function, X=inverse_X, X_names=inverse_X_names, lens=projected_X, lens_names=projected_X_names, custom_tooltips=custom_tooltips, ) _test_raised_deprecation_warning(w) # visuals._graph_data_distribution histogram = _graph_data_distribution( graph, color_function=lens, node_color_function=node_color_function, colorscale=default_colorscale, ) _test_raised_deprecation_warning(w)
def test_visualize_standalone_same(self, tmpdir): """ ensure that the visualization is not dependent on the actual mapper object. """ mapper = KeplerMapper() file = tmpdir.join('output.html') data = np.random.rand(1000, 10) lens = mapper.fit_transform(data, projection=[0]) graph = mapper.map(lens, data) viz1 = mapper.visualize(graph, path_html=file.strpath) new_mapper = KeplerMapper() viz2 = new_mapper.visualize(graph, path_html=file.strpath) assert viz1 == viz2
def test_BasicCover(): # TODO: add a mock that asserts the cover was called appropriately.. or test number of cubes etc. data, _ = datasets.make_circles() mapper = KeplerMapper() graph = mapper.map(data) mapper.visualize(graph)