Beispiel #1
0
def test_color_mean_gray():
    data = np.array([[0., 0.],
                     [0.1, 0.1],
                     [0.2, 0.2],
                     [0.2, 0.8],
                     [0.1, 0.9],
                     [0., 1.],
                     [0.8, 0.8],
                     [0.9, 0.9],
                     [1., 1.]])

    target = np.array([[0], [0], [0],
                       [1], [1.1], [0.9],
                       [2], [2], [2]])

    t = Topology()
    t.load_data(data)
    t.fit_transform(metric=None, lens=None)

    t.map(resolution=2, overlap=0.3, eps=0.2, min_samples=3)

    t.color(target, color_method="mean", color_type="gray", normalize=True)

    test_color = ['#dcdcdc', '#787878', '#141414']

    assert t.hex_colors == test_color
Beispiel #2
0
def test_search_number_data():
    data = np.array([[0., 0.],
                     [0.1, 0.1],
                     [0.2, 0.2],
                     [0.2, 0.8],
                     [0.1, 0.9],
                     [0., 1.],
                     [0.8, 0.8],
                     [0.9, 0.9],
                     [1., 1.]])

    target = np.array([[0], [0], [0],
                       [1], [1], [1],
                       [2], [2], [2]])

    text_data = np.array([["a"], ["a"], ["a"],
                          ["b"], ["b"], ["b"],
                          ["c"], ["c"], ["c"]])

    t = Topology()
    t.load_data(data, text_data=text_data)
    t.fit_transform(metric=None, lens=None)
    t.map(resolution=2, overlap=0.3)
    t.color(target, color_method="mean", color_type="rgb", normalize=True)
    search_dicts = [{
        "data_type": "number",
        "operator": ">",
        "column": 0,
        "value": 0.7
    }]
    t.search_from_values(search_dicts=search_dicts, target=None, search_type="and")

    test_color = ['#cccccc', '#cccccc', '#b20000']
    assert t.hex_colors == test_color
Beispiel #3
0
def test_map():
    data = np.array([[0., 0.],
                     [0.25, 0.25],
                     [0.5, 0.5],
                     [0.75, 0.75],
                     [1., 1.],
                     [1., 0.],
                     [0.25, 0.75],
                     [0.75, 0.25],
                     [0., 1.]])

    t = Topology()
    t.load_data(data)
    t.fit_transform(metric=None, lens=None)

    t.map(resolution=2, overlap=0.3, eps=0.3, min_samples=3)

    test_nodes = np.array([[0.25, 0.25],
                           [0.25, 0.75],
                           [0.75, 0.25],
                           [0.75, 0.75]])

    test_edges = np.array([[0, 1],
                           [0, 2],
                           [0, 3],
                           [1, 2],
                           [1, 3],
                           [2, 3]])

    assert_array_equal(t.nodes, test_nodes)
    assert_array_equal(t.edges, test_edges)
Beispiel #4
0
def test_map_min_samples_under_zero():
    data = np.array([[0., 0.],
                     [0.25, 0.25],
                     [0.5, 0.5],
                     [0.75, 0.75],
                     [1., 1.],
                     [1., 0.],
                     [0.25, 0.75],
                     [0.75, 0.25],
                     [0., 1.]])

    t = Topology()
    t.load_data(data)
    t.fit_transform(metric=None, lens=None)
    with pytest.raises(Exception):
        t.map(min_samples=-1)
Beispiel #5
0
def test_color_different_size_input():
    data = np.array([[0., 0.],
                     [0.1, 0.1],
                     [0.2, 0.2],
                     [0.2, 0.8],
                     [0.1, 0.9],
                     [0., 1.],
                     [0.8, 0.8],
                     [0.9, 0.9],
                     [1., 1.]])

    target = np.array([0, 1, 2])

    t = Topology()
    t.load_data(data)
    t.fit_transform()
    t.map()

    with pytest.raises(Exception):
        t.color(target)
Beispiel #6
0
def test_color_ctype():
    data = np.array([[0., 0.],
                     [0.1, 0.1],
                     [0.2, 0.2],
                     [0.2, 0.8],
                     [0.1, 0.9],
                     [0., 1.],
                     [0.8, 0.8],
                     [0.9, 0.9],
                     [1., 1.]])

    target = np.array([[0], [0], [0],
                       [1], [1], [1],
                       [2], [2], [2]])

    t = Topology()
    t.load_data(data)
    t.fit_transform()
    t.map(resolution=2, overlap=0.3)

    with pytest.raises(Exception):
        t.color(target, color_type="hoge")
Beispiel #7
0
def create():
    try:
        # get request params
        data = json.loads(request.params.data)

        file_id = int(data["file_id"])
        file_name = _get_file_name_from_id(file_id)
        file_path = os.path.join(DATA_DIR, file_name)

        # create topology instance
        topology = Topology(verbose=0)
        loader = CSVLoader(file_path)
        topology.load(loader=loader, standardize=True)

        target_index = data["target_index"]
        mode = int(data["mode"])
        clustering_algorithm = int(data["clustering_algorithm"])
        train_size = float(data["train_size"])
        k = int(data["k"])
        eps = float(data["eps"])
        min_samples = int(data["min_samples"])
        resolution = int(data["resolution"])
        overlap = float(data["overlap"])
        topology.point_cloud = np.array(data["point_cloud"])

        visualize_mode = int(data["visualize_mode"])

        if mode == 0:
            # scatter plot
            colors = []
            for i in range(topology.number_data.shape[1]):
                t = topology.number_data[:, i]
                scaler = preprocessing.MinMaxScaler()
                t = scaler.fit_transform(t.reshape(-1, 1))
                topology.color_point_cloud(target=t, normalize=False)
                colors.append(topology.point_cloud_hex_colors)

        elif mode == 1:
            # unsupervised_clusterings
            # If target index isn't exists, use all data to calculate
            if target_index != '':
                topology.number_data, target = _split_target(
                    topology.number_data, int(target_index))

            clusters = [
                cluster.KMeans(n_clusters=k),
                cluster.DBSCAN(eps=eps, min_samples=min_samples)
            ]
            topology.unsupervised_clustering_point_cloud(
                clusterer=clusters[clustering_algorithm])

            if target_index != '':
                topology.number_data = _concat_target(topology.number_data,
                                                      target,
                                                      int(target_index))

            colors = []
            for i in range(topology.number_data.shape[1]):
                colors.append(topology.point_cloud_hex_colors)

        elif mode == 2:
            # supervised_clusterings
            # If target index isn't exists, use all data to calculate
            if target_index != '':
                topology.number_data, target = _split_target(
                    topology.number_data, int(target_index))

            clusters = [neighbors.KNeighborsClassifier(n_neighbors=k)]
            topology.supervised_clustering_point_cloud(
                clusterer=clusters[clustering_algorithm],
                target=target,
                train_size=train_size)

            if target_index != '':
                topology.number_data = _concat_target(topology.number_data,
                                                      target,
                                                      int(target_index))

            colors = []
            for i in range(topology.number_data.shape[1]):
                colors.append(topology.point_cloud_hex_colors)

        elif mode == 3:
            # tda
            topology.map(resolution=resolution,
                         overlap=overlap,
                         eps=eps,
                         min_samples=min_samples)

            if visualize_mode == 2:
                presenter = SpectralPresenter(fig_size=(10, 10),
                                              node_size=5,
                                              edge_width=1)
                pos = presenter._get_position(topology.nodes, topology.edges)
                topology.nodes = np.array(list(pos.values()))
                print("spectral")

            colors = []
            for i in range(topology.number_data.shape[1]):
                t = topology.number_data[:, i]
                scaler = preprocessing.MinMaxScaler()
                t = scaler.fit_transform(t.reshape(-1, 1))
                topology.color(target=t)
                colors.append(topology.hex_colors)

        if mode < 3:
            hypercubes = _ndarray_to_list(
                np.arange(len(topology.point_cloud)).reshape(-1, 1))
            nodes = _ndarray_to_list(topology.point_cloud)
            edges = []
            node_sizes = [MIN_NODE_SIZE] * len(topology.point_cloud)
            colors = colors
        elif mode == 3:
            hypercubes = _convert_hypercubes_to_array(topology.hypercubes)
            nodes = _ndarray_to_list(topology.nodes)
            edges = _ndarray_to_list(topology.edges)
            scaler = preprocessing.MinMaxScaler(feature_range=(MIN_NODE_SIZE,
                                                               MAX_NODE_SIZE))
            node_sizes = _ndarray_to_list(
                scaler.fit_transform(topology.node_sizes))
            colors = colors

        body = {
            "hypercubes": hypercubes,
            "nodes": nodes,
            "edges": edges,
            "node_sizes": node_sizes,
            "colors": colors,
            "train_index": _ndarray_to_list(topology.train_index)
        }
        r = create_response(body)
        return r
    except Exception as e:
        body = json.dumps({"error_msg": e.args[0]})
        r = create_response(body)
        return r
Beispiel #8
0
def test_map_point_cloud_none():
    t = Topology()
    with pytest.raises(Exception):
        t.map()