diff --git a/examples/toolbox/ex_curve_clustering.py b/examples/toolbox/ex_curve_clustering.py index 2874d365..b3f761e9 100644 --- a/examples/toolbox/ex_curve_clustering.py +++ b/examples/toolbox/ex_curve_clustering.py @@ -24,14 +24,15 @@ def ex_curve_clustering(label_meas, verbose=False, make_plots=False): Returns ------- - cluster_labels : dict - Cluster indices for each data point for each mode. - cluster_info : dict - Information about each cluster for each mode. - inertia : dict - Inertia (within-cluster sum of squares) for each mode. - avg_curve : dict - Contain all list of average curve for each cluster in each mode. + cluster_labels : list + Cluster indices for each data point + cluster_info : list + Information about each cluster. + inertia : float + For K-Means : Inertia (within-cluster sum of squares). + For GMM : Bayesian Information Criterion. + avg_curve: numpy.ndarray + List of average curve. """ example_root_path_in = get_setting("example_root_path_in") dir_path_in = os.path.join(example_root_path_in, "PZT100n") @@ -39,7 +40,7 @@ def ex_curve_clustering(label_meas, verbose=False, make_plots=False): "mode": "dfrt", 'method': 'kmeans', 'label meas': label_meas, - 'nb clusters': 4} + 'nb clusters': 3} dim_pix = {'x': 7, 'y': 3} dim_mic = {'x': 7, 'y': 3} diff --git a/tests/toolbox/test_curve_clustering.py b/tests/toolbox/test_curve_clustering.py index 31f29a46..8908018e 100644 --- a/tests/toolbox/test_curve_clustering.py +++ b/tests/toolbox/test_curve_clustering.py @@ -11,141 +11,61 @@ # class TestReaderAllMaps(unittest.TestCase): -def test_curve_clustering_piezoresponse(): - """ Test ex_curve_clustering for piezoresponse signal """ +def test_curve_clustering_deflection(): + """ Test ex_curve_clustering for deflection signal """ if os.getenv('GITHUB_ACTIONS') == 'true': skip("Test skipped for Github source") - out = ex_curve_clustering(['piezoresponse']) + out = ex_curve_clustering(['deflection']) (cluster_indexs, cluster_info, inertia, avg_curve) = out - # print(np.sum(avg_curve["off"][0])) - # print(np.sum(avg_curve["off"][1])) - # print(np.sum(avg_curve["off"][2])) - # print(np.sum(avg_curve["off"][3])) - # print(np.sum(avg_curve["off"][4])) - # - # print(np.sum(avg_curve["on"][0])) - # print(np.sum(avg_curve["on"][1])) - # - # print(np.sum(avg_curve["coupled"][0])) - # print(np.sum(avg_curve["coupled"][1])) - # print(np.sum(avg_curve["coupled"][2])) - # print(np.sum(avg_curve["coupled"][3])) - - indexs_off = [3, 1, 3, 3, 3, 4, 1, 3, 2, 2, 2, 1, 3, 3, 3, 3, 2, 2, 2, 2, - 4, 4, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, - 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2, - 3, 3, 3] - indexs_on = [0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 0, 0, 0] - indexs_coupled = [2, 3, 2, 2, 2, 1, 3, 2, 1, 1, 1, 3, 2, 2, 1, 1, 0, 1, - 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 2, 2, 1] - - assert cluster_indexs["off"] == indexs_off - assert cluster_indexs["on"] == indexs_on - assert cluster_indexs["coupled"] == indexs_coupled - - assert inertia["off"] == approx(0.13523678975557835) - assert inertia["on"] == approx(0.28663984068106296) - assert inertia["coupled"] == approx(0.1620727015123082) - - assert np.sum(avg_curve["off"][0]) == approx(-0.5604060286538461) - assert np.sum(avg_curve["off"][1]) == approx(-0.32359891999833335) - assert np.sum(avg_curve["off"][2]) == approx(-0.8637471184000002) - assert np.sum(avg_curve["off"][3]) == approx(-0.15399308193333336) - assert np.sum(avg_curve["off"][4]) == approx(-2.242400310125) - - assert np.sum(avg_curve["on"][0]) == approx(-0.4599520117610169) - assert np.sum(avg_curve["on"][1]) == approx(-0.434709211) - - assert np.sum(avg_curve["coupled"][0]) == approx( - -0.3895835032539335) - assert np.sum(avg_curve["coupled"][1]) == approx( - -0.7495477722962939) - assert np.sum(avg_curve["coupled"][2]) == approx( - 0.2412386496437579) - assert np.sum(avg_curve["coupled"][3]) == approx(-0.39787115575800425) - - assert cluster_info["off"][0] == [0.0, 0.07695897085508642, 'B', 26, 'A'] - assert cluster_info["off"][1] == [0.07695897085508642, 0.07695897085508642, - 'A', 6, 'B'] - assert cluster_info["off"][2] == [0.0991076879668917, 0.09910768796689168, - 'A', 15, 'C'] - assert cluster_info["off"][3] == [0.17859580758813837, 0.11473940080561688, - 'C', 12, 'D'] - assert cluster_info["off"][4] == [0.1911772239978829, 0.14257219838645985, - 'C', 4, 'E'] - - assert cluster_info["on"][0] == [0.0, 0.25579396755296596, 'B', 59, 'A'] - assert cluster_info["on"][1] == [0.25579396755296596, 0.25579396755296596, - 'A', 4, 'B'] - - assert cluster_info["coupled"][0] == \ - [0.0, 0.09424982911577919, 'B', 33, 'A'] - assert cluster_info["coupled"][1] == \ - [0.09424982911577919, 0.09424982911577919, 'A', 18, 'B'] - assert cluster_info["coupled"][2] == \ - [0.169070781895064, 0.12835058633199692, 'B', 9, 'C'] - assert cluster_info["coupled"][3] == \ - [0.31060770971176105, 0.31060770971176105, 'A', 3, 'D'] - - -def test_curve_clustering_amp_pha(): - """ Test ex_curve_clustering for composed amplitude and phase signal """ + # print(np.sum(avg_curve[0])) + # print(np.sum(avg_curve[1])) + # print(np.sum(avg_curve[2])) + + target_cluster_indexs = [1, 0, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 1, 1, 1, 0] + + assert cluster_indexs == target_cluster_indexs + + assert inertia == approx(354140.1914109529) + + assert np.sum(avg_curve[0]) == approx(3659427.028688297) + assert np.sum(avg_curve[1]) == approx(3669009.191778517) + assert np.sum(avg_curve[2]) == approx(3629449.0314160236) + + assert cluster_info[0] == [0.0, 146.99328263341397, 'B', 12, 'A'] + assert cluster_info[1] == [146.99328263341397, 146.99328263341397, 'A', 4, + 'B'] + assert cluster_info[2] == [147.71545254648586, 147.71545254648586, 'A', 5, + 'C'] + + +def test_curve_clustering_height_deflection(): + """ Test ex_curve_clustering for composed height and deflection signal """ if os.getenv('GITHUB_ACTIONS') == 'true': skip("Test skipped for Github source") - out = ex_curve_clustering(['amplitude', 'phase']) + out = ex_curve_clustering(['deflection', 'height']) (cluster_indexs, cluster_info, inertia, avg_curve) = out - # print(np.sum(avg_curve["off"][0])) - # print(np.sum(avg_curve["off"][1])) - # print(np.sum(avg_curve["off"][2])) - # print(np.sum(avg_curve["off"][3])) - # print(np.sum(avg_curve["off"][4])) - # - # print(np.sum(avg_curve["on"][0])) - # print(np.sum(avg_curve["on"][1])) - - indexs_on = [0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, - 0, 0, 0] - indexs_off = [3, 2, 3, 3, 3, 4, 2, 3, 1, 1, 1, 2, 3, 3, 3, 3, 1, 1, 1, 1, - 4, 4, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, - 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 3, - 3, 3, 3] - - assert cluster_indexs["off"] == indexs_off - assert cluster_indexs["on"] == indexs_on - - assert inertia["off"] == approx(111.13578587730699) - assert inertia["on"] == approx(91.94596697427032) - - assert np.sum(avg_curve["off"][0]) == approx(67.44480269778579) - assert np.sum(avg_curve["off"][1]) == approx(85.049189857557) - assert np.sum(avg_curve["off"][2]) == approx(47.21474992974796) - assert np.sum(avg_curve["off"][3]) == approx(98.4523948723894) - assert np.sum(avg_curve["off"][4]) == approx(73.05500191702258) - - assert np.sum(avg_curve["on"][0]) == approx(114.13229998089432) - assert np.sum(avg_curve["on"][1]) == approx(87.0701917281026) - - assert cluster_info["off"][0] == [0.0, 2.436570085411072, 'B', 26, 'A'] - assert cluster_info["off"][1] == \ - [2.436570085411072, 2.436570085411072, 'A', 14, 'B'] - assert cluster_info["off"][2] == \ - [3.1368359442253464, 3.1368359442253464, 'A', 6, 'C'] - assert cluster_info["off"][3] == \ - [3.8185458951727846, 2.4426456403161194, 'B', 13, 'D'] - assert cluster_info["off"][4] == \ - [4.861100035372178, 3.620470997547831, 'C', 4, 'E'] - - assert cluster_info["on"][0] == [0.0, 2.9493831147512637, 'B', 56, 'A'] - assert cluster_info["on"][1] == \ - [2.9493831147512637, 2.9493831147512637, 'A', 7, 'B'] + # print(np.sum(avg_curve[0])) + # print(np.sum(avg_curve[1])) + # print(np.sum(avg_curve[2])) + + target_cluster_indexs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, + 2, 2, 2, 2] + + assert cluster_indexs == target_cluster_indexs + + assert inertia == approx(76.06496724200191) + + assert np.sum(avg_curve[0]) == approx(388455.9994825708) + assert np.sum(avg_curve[1]) == approx(390740.1297729185) + assert np.sum(avg_curve[2]) == approx(392387.2263338187) + + assert cluster_info[0] == [0.0, 4.517480463827678, 'B', 9, 'A'] + assert cluster_info[1] == [4.517480463827678, 3.684231929402842, 'C', 7, + 'B'] + assert cluster_info[2] == [8.006028411446396, 3.684231929402842, 'B', 5, + 'C']