Esempio n. 1
0
    def test_node_color_function_must_be_np_function(self, sc):
        mapper = KeplerMapper()

        with pytest.raises(
                AttributeError,
                match=r".*must be a function available on `numpy` class.*"):
            mapper.visualize(sc, node_color_function=["yinz"])
Esempio n. 2
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)
Esempio n. 3
0
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)
Esempio n. 4
0
    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
Esempio n. 5
0
    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"])
Esempio n. 6
0
 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"],
     )
Esempio n. 7
0
    def test_visualize_multiple_color_functions(self):
        """ convenience test for generating a vis with multiple 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]

        cv1 = np.array(lens)
        cv2 = np.flip(cv1)
        cv = np.column_stack([cv1, cv2])
        mapper.visualize(graph,
                         color_values=cv,
                         color_function_name=["hotdog", "hotdiggitydog"])
Esempio n. 8
0
    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_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
Esempio n. 10
0
 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)
Esempio n. 11
0
    def test_visualize_search_bar(self):
        """ convenience test for generating a vis with a search bar (and also with multiple color_values _and_ multiple node_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]

        cv1 = np.array(lens)
        cv2 = np.flip(cv1)
        cv = np.column_stack([cv1, cv2])
        mapper.visualize(
            graph,
            color_values=cv,
            node_color_function=["mean", "std"],
            color_function_name=["hotdog", "hotdiggitydog"],
            include_searchbar=True,
        )
Esempio n. 12
0
    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(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_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_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"
Esempio n. 16
0
    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)
Esempio n. 17
0
    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)
Esempio n. 18
0
    def test_no_color_values_one_color_function_no_exception_yes_warning(self):
        mapper = KeplerMapper()
        data, labels = make_circles(1000, random_state=0)
        lens = mapper.fit_transform(data, projection=[0])
        graph = mapper.map(lens, data)

        with warnings.catch_warnings(record=True) as w:
            # Cause all warnings to always be triggered.
            warnings.simplefilter("always")

            color_values = mapper.visualize(graph,
                                            color_values=None,
                                            color_function_name=["hotdog"])
        assert "unexpected" in str(w[-1].message)
Esempio n. 19
0
    def test_color_function_names_unequal_exception(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]

        cv1 = np.array(lens)
        cv2 = np.flip(cv1)
        cv = np.column_stack([cv1, cv2])
        with pytest.raises(Exception) as excinfo:
            mapper.visualize(graph,
                             color_values=cv,
                             color_function_name="hotdog")
        assert "Must be equal" in str(excinfo.value)

        with pytest.raises(Exception) as excinfo:
            color_values = mapper.visualize(
                graph,
                color_values=cv,
                color_function_name=["hotdog", "hotdog", "hotdiggitydog"],
            )
        assert "Must be equal" in str(excinfo.value)
    def test_empty_graph_warning(self):
        mapper = KeplerMapper()

        graph = {"nodes": {}}
        with pytest.raises(Exception):
            mapper.visualize(graph)
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)