def test_decay_micro_clusters(self, denstream): micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([5.0, 5.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 5 denstream.p_micro_clusters.append(micro_cluster) micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([1.0, 5.0]) micro_cluster.CF2 = np.array([3.0, 0.0]) micro_cluster.weight = 10 denstream.p_micro_clusters.append(micro_cluster) micro_cluster = MicroCluster(2, 2, 0, 0.15) micro_cluster.CF = np.array([0.0, 0.0]) micro_cluster.CF2 = np.array([10.0, 2.0]) micro_cluster.weight = 3 denstream.p_micro_clusters.append(micro_cluster) denstream.decay_micro_clusters(0) assert np.all(denstream.p_micro_clusters[0].CF == np.array([5, 5])) assert np.all(denstream.p_micro_clusters[0].CF2 == np.array([1, 1])) assert np.all(denstream.p_micro_clusters[0].weight == 5) assert np.all(denstream.p_micro_clusters[1].CF == np.array([1, 5]) * 2**(-0.15)) assert np.all(denstream.p_micro_clusters[1].CF2 == np.array([3, 0]) * 2**(-0.15)) assert np.all(denstream.p_micro_clusters[1].weight == 10 * 2**(-0.15)) assert np.all(denstream.p_micro_clusters[2].CF == np.array([0, 0]) * 2**(-0.15)) assert np.all(denstream.p_micro_clusters[2].CF2 == np.array([10, 2]) * 2**(-0.15)) assert np.all(denstream.p_micro_clusters[2].weight == 3 * 2**(-0.15))
def test_radius_with_new_point(): micro_cluster = MicroCluster(0, 2, 0, 0.15) point1 = np.array([0, 1]) radius = micro_cluster.radius_with_new_point(point1) assert isinstance(radius, np.float64) assert radius == 0 micro_cluster.CF = np.array([0, 0]) micro_cluster.CF2 = np.array([0, 1]) point1 = np.array([0, 1]) radius = micro_cluster.radius_with_new_point(point1) assert radius == 1 micro_cluster.CF = np.array([0, 0]) micro_cluster.CF2 = np.array([1, 1]) point1 = np.array([0, 1]) radius = micro_cluster.radius_with_new_point(point1) micro_cluster.CF = np.array([0, 1]) micro_cluster.CF2 = np.array([1, 2]) micro_cluster.weight = 1 assert radius == micro_cluster.radius micro_cluster.CF = np.array([1, 1]) micro_cluster.CF2 = np.array([0, 0]) point1 = np.array([1, 1]) radius = micro_cluster.radius_with_new_point(point1) micro_cluster.CF = np.array([2, 2]) micro_cluster.CF2 = np.array([1, 1]) micro_cluster.weight = 1 assert radius == 0 assert radius == micro_cluster.radius
def test_add_point(self, denstream): case_list = [] case_1 = Case('1') case_1.set_activity('activityA', datetime(2015, 5, 10, 8, 00, 00)) case_1.set_activity('activityB', datetime(2015, 5, 10, 8, 00, 10)) case_1.set_activity('activityC', datetime(2015, 5, 10, 8, 00, 20)) case_list.append(case_1) case_2 = Case('2') case_2.set_activity('activityA', datetime(2015, 5, 10, 8, 00, 00)) case_2.set_activity('activityB', datetime(2015, 5, 10, 8, 00, 10)) case_list.append(case_2) graph = nx.DiGraph() graph = initialize_graph(graph, case_list) case_3 = Case('3') case_3.set_activity('activityA', datetime(2015, 5, 10, 8, 00, 00)) case_3.set_activity('activityB', datetime(2015, 5, 10, 8, 00, 10)) case_3.set_activity('activityC', datetime(2015, 5, 10, 8, 00, 20)) case_3.set_activity('activityD', datetime(2015, 5, 10, 8, 00, 30)) trace_distance, time_distance = extract_case_distances(graph, case_3) case_3.graph_distance = trace_distance case_3.time_distance = time_distance micro_cluster = MicroCluster(10, 2, 0, 0.15) micro_cluster.CF = np.array([0.5, -0.5]) micro_cluster.CF2 = np.array([0.5, -0.1]) micro_cluster.weight = 10 denstream.p_micro_clusters.append(micro_cluster) micro_cluster = MicroCluster(11, 2, 0, 0.15) micro_cluster.CF = np.array([0.0, 0.0]) micro_cluster.CF2 = np.array([0.0, 0.0]) micro_cluster.weight = 5 denstream.o_micro_clusters.append(micro_cluster) denstream.mc_id = 2 mc_id = denstream.add_point(case_3) assert mc_id == 2 assert len(denstream.o_micro_clusters) == 2 assert denstream.o_micro_clusters[1].radius == 0 assert denstream.o_micro_clusters[1].weight == 1 assert np.all(denstream.o_micro_clusters[1].CF == case_3.point) assert np.all(denstream.o_micro_clusters[1].CF2 == case_3.point * case_3.point) cf = denstream.o_micro_clusters[1].CF.copy() cf2 = denstream.o_micro_clusters[1].CF2.copy() mc_id = denstream.add_point(case_3) assert mc_id == 2 assert len(denstream.o_micro_clusters) == 1 assert len(denstream.p_micro_clusters) == 2 assert denstream.p_micro_clusters[1].weight == 2 assert np.all(denstream.p_micro_clusters[1].CF == cf + case_3.point) assert np.all(denstream.p_micro_clusters[1].CF2 == cf2 + case_3.point * case_3.point)
def test_decay(): micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.weight = 1 micro_cluster.CF = np.array([0.5, 0.7]) micro_cluster.CF2 = np.array([0.0, 1.0]) cf = micro_cluster.CF.copy() cf2 = micro_cluster.CF2.copy() weight = micro_cluster.weight micro_cluster.decay() assert np.all(micro_cluster.CF == cf * (2**(-0.15))) assert np.all(micro_cluster.CF2 == cf2 * (2**(-0.15))) assert micro_cluster.weight == weight * (2**(-0.15))
def test_radius(): micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.weight = 1 micro_cluster.CF = np.array([0, 0]) micro_cluster.CF2 = np.array([0, 1]) assert isinstance(micro_cluster.radius, np.float64) assert micro_cluster.radius == 1.0 micro_cluster.CF = np.array([0, 0]) micro_cluster.CF2 = np.array([0, 2]) assert micro_cluster.radius == sqrt(2) micro_cluster.CF = np.array([0, 1]) micro_cluster.CF2 = np.array([0, 2]) assert micro_cluster.radius == 1 micro_cluster.CF = np.array([0, 2]) micro_cluster.CF2 = np.array([0, 2]) assert micro_cluster.radius == 0 micro_cluster.CF = np.array([1, 2]) micro_cluster.CF2 = np.array([0, 0]) assert micro_cluster.radius == 0
def test_generate_clusters(self, denstream): assert len(denstream.p_micro_clusters) == 0 dense_group, not_dense_group = denstream.generate_clusters() assert dense_group == [[]] assert not_dense_group == [[]] case_0 = Case('0') case_1 = Case('1') micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([5.0, 5.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 5 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id assert len(denstream.p_micro_clusters) == 1 assert micro_cluster.weight >= denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert not dense_group == [[]] assert not_dense_group == [[]] cluster_list = dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 5 assert cluster.case_ids == ['0', '1'] micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([3.0, 3.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 3 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id assert len(denstream.p_micro_clusters) == 1 assert micro_cluster.weight < denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert dense_group == [[]] assert not not_dense_group == [[]] cluster_list = not_dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 3 assert cluster.case_ids == ['0', '1'] case_2 = Case('2') case_3 = Case('3') micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([3.0, 3.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 3 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([4.0, 4.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 4 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_2.id] = micro_cluster.id denstream.all_cases[case_3.id] = micro_cluster.id cl1 = denstream.p_micro_clusters[0] cl2 = denstream.p_micro_clusters[1] assert len(denstream.p_micro_clusters) > 1 assert denstream.euclidean_distance( cl1.centroid, cl2.centroid) <= 2 * denstream.epsilon assert cl1.weight + cl2.weight >= denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert not dense_group == [[]] assert not_dense_group == [[]] cluster_list = dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 3 assert cluster.case_ids == ['0', '1'] cluster = cluster_list[1] assert isinstance(cluster, Cluster) assert cluster.id == 1 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 4 assert cluster.case_ids == ['2', '3'] with pytest.raises(IndexError): assert dense_group[1] assert cluster_list[2] micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([3.0, 3.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 1 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([3.0, 3.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 1 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_2.id] = micro_cluster.id denstream.all_cases[case_3.id] = micro_cluster.id cl1 = denstream.p_micro_clusters[0] cl2 = denstream.p_micro_clusters[1] assert len(denstream.p_micro_clusters) > 1 assert denstream.euclidean_distance( cl1.centroid, cl2.centroid) <= 2 * denstream.epsilon assert cl1.weight + cl2.weight < denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert dense_group == [[]] assert not not_dense_group == [[]] cluster_list = not_dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [3, 3]) assert cluster.radius == 0 assert cluster.weight == 1 assert cluster.case_ids == ['0', '1'] cluster = cluster_list[1] assert isinstance(cluster, Cluster) assert cluster.id == 1 assert np.all(cluster.centroid == [3, 3]) assert cluster.radius == 0 assert cluster.weight == 1 assert cluster.case_ids == ['2', '3'] with pytest.raises(IndexError): assert not_dense_group[1] assert cluster_list[2] micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([3.0, 3.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 3 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([1.0, 1.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 4 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_2.id] = micro_cluster.id denstream.all_cases[case_3.id] = micro_cluster.id cl1 = denstream.p_micro_clusters[0] cl2 = denstream.p_micro_clusters[1] assert len(denstream.p_micro_clusters) > 1 assert denstream.euclidean_distance( cl1.centroid, cl2.centroid) > 2 * denstream.epsilon assert cl1.weight < denstream.mu assert cl2.weight >= denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert not dense_group == [[]] assert not not_dense_group == [[]] cluster_list = not_dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 3 assert cluster.case_ids == ['0', '1'] with pytest.raises(IndexError): assert cluster_list[1] cluster_list = dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 1 assert np.all(cluster.centroid == [0.25, 0.25]) assert cluster.radius == cl2.radius assert cluster.weight == 4 assert cluster.case_ids == ['2', '3'] with pytest.raises(IndexError): assert not_dense_group[1] assert dense_group[1] assert cluster_list[1] micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([6.0, 6.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 6 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([1.0, 1.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 2 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_2.id] = micro_cluster.id denstream.all_cases[case_3.id] = micro_cluster.id cl1 = denstream.p_micro_clusters[0] cl2 = denstream.p_micro_clusters[1] assert len(denstream.p_micro_clusters) > 1 assert denstream.euclidean_distance( cl1.centroid, cl2.centroid) > 2 * denstream.epsilon assert cl1.weight >= denstream.mu assert cl2.weight < denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert not dense_group == [[]] assert not not_dense_group == [[]] cluster_list = dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 6 assert cluster.case_ids == ['0', '1'] with pytest.raises(IndexError): assert cluster_list[1] cluster_list = not_dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 1 assert np.all(cluster.centroid == [0.5, 0.5]) assert cluster.radius == cl2.radius assert cluster.weight == 2 assert cluster.case_ids == ['2', '3'] with pytest.raises(IndexError): assert not_dense_group[1] assert dense_group[1] assert cluster_list[1] micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([6.0, 6.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 6 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([1.0, 1.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 4 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_2.id] = micro_cluster.id denstream.all_cases[case_3.id] = micro_cluster.id cl1 = denstream.p_micro_clusters[0] cl2 = denstream.p_micro_clusters[1] assert len(denstream.p_micro_clusters) > 1 assert denstream.euclidean_distance( cl1.centroid, cl2.centroid) > 2 * denstream.epsilon assert cl1.weight >= denstream.mu assert cl2.weight >= denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert not dense_group == [[]] assert not_dense_group == [[]] cluster_list = dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [1, 1]) assert cluster.radius == 0 assert cluster.weight == 6 assert cluster.case_ids == ['0', '1'] with pytest.raises(IndexError): assert cluster_list[1] cluster_list = dense_group[1] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 1 assert np.all(cluster.centroid == [0.25, 0.25]) assert cluster.radius == cl2.radius assert cluster.weight == 4 assert cluster.case_ids == ['2', '3'] with pytest.raises(IndexError): assert dense_group[2] assert cluster_list[1] micro_cluster = MicroCluster(0, 2, 0, 0.15) micro_cluster.CF = np.array([1.0, 1.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 3 denstream.p_micro_clusters = [] denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_0.id] = micro_cluster.id denstream.all_cases[case_1.id] = micro_cluster.id micro_cluster = MicroCluster(1, 2, 0, 0.15) micro_cluster.CF = np.array([1.0, 1.0]) micro_cluster.CF2 = np.array([1.0, 1.0]) micro_cluster.weight = 2 denstream.p_micro_clusters.append(micro_cluster) denstream.all_cases[case_2.id] = micro_cluster.id denstream.all_cases[case_3.id] = micro_cluster.id cl1 = denstream.p_micro_clusters[0] cl2 = denstream.p_micro_clusters[1] assert len(denstream.p_micro_clusters) > 1 assert denstream.euclidean_distance( cl1.centroid, cl2.centroid) > 2 * denstream.epsilon assert cl1.weight < denstream.mu assert cl2.weight < denstream.mu dense_group, not_dense_group = denstream.generate_clusters() assert dense_group == [[]] assert not not_dense_group == [[]] cluster_list = not_dense_group[0] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 0 assert np.all(cluster.centroid == [(1 / 3), (1 / 3)]) assert cluster.radius == cl1.radius assert cluster.weight == 3 assert cluster.case_ids == ['0', '1'] with pytest.raises(IndexError): assert cluster_list[1] cluster_list = not_dense_group[1] cluster = cluster_list[0] assert isinstance(cluster, Cluster) assert cluster.id == 1 assert np.all(cluster.centroid == [0.5, 0.5]) assert cluster.radius == cl2.radius assert cluster.weight == 2 assert cluster.case_ids == ['2', '3'] with pytest.raises(IndexError): assert not_dense_group[2] assert cluster_list[1]