1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145


pub mod glcm;
pub mod features;

#[cfg(test)]
mod tests {
    use core::f32::consts::PI;
    use crate::{glcm::*, features::{calculate_feature, FeatureType}};
    use ndarray::prelude::*;

    #[test]
    fn glcm_test_from_skimage() {
        let result = calculate_glcm_matrix(
            array![
                [0, 0, 1, 1],
                [0, 0, 1, 1],
                [0, 2, 2, 2],
                [2, 2, 3, 3]
            ],
            &[1.0],
            &[0.0, PI / 4.0, PI / 2.0, 3.0 * (PI / 4.0)],
            4,
            false
        );
        
        assert_eq!(
            result.slice(s![.., .., 0, 0]), 
            array![
                [2.0, 2.0, 1.0, 0.0],
                [0.0, 2.0, 0.0, 0.0],
                [0.0, 0.0, 3.0, 1.0],
                [0.0, 0.0, 0.0, 1.0],
            ]
        );
        assert_eq!(
            result.slice(s![.., .., 0, 1]), 
            array![
                [1.0, 1.0, 3.0, 0.0],
                [0.0, 1.0, 1.0, 0.0],
                [0.0, 0.0, 0.0, 2.0],
                [0.0, 0.0, 0.0, 0.0],
            ]
        );
        assert_eq!(
            result.slice(s![.., .., 0, 2]), 
            array![
                [3.0, 0.0, 2.0, 0.0],
                [0.0, 2.0, 2.0, 0.0],
                [0.0, 0.0, 1.0, 2.0],
                [0.0, 0.0, 0.0, 0.0],
            ]
        );
        assert_eq!(
            result.slice(s![.., .., 0, 3]), 
            array![
                [2.0, 0.0, 0.0, 0.0],
                [1.0, 1.0, 2.0, 0.0],
                [0.0, 0.0, 2.0, 1.0],
                [0.0, 0.0, 0.0, 0.0],
            ]
        );

    }

    fn round(x: f32, decimals: u32) -> f32 {
        let y = 10i32.pow(decimals) as f32;
        (x * y).round() / y
    }

    fn assert_eq_round(left: Array2<f32>, right: Array2<f32>, message: String) {
        assert_eq!(
            left.mapv(|x| round(x, 7)),
            right.mapv(|x| round(x, 7)),
            "{}", 
            message
        )
    }

    #[test]
    #[allow(clippy::excessive_precision)] 
    fn contrast_feature() {
        let p = calculate_glcm_matrix(
            array![
                [0, 0, 1, 1],
                [0, 0, 1, 1],
                [0, 2, 2, 2],
                [2, 2, 3, 3]
            ],
            &[1.0, 2.0],
            &[0.0, PI / 2.0],
            4,
            true
        );

        assert_eq_round(
            calculate_feature(&p, FeatureType::Contrast),
            array![
                [0.583333_3, 1.0],
                [1.25, 2.75]
            ],
        format!("Feature type: {:?}", FeatureType::Contrast)
        );
        assert_eq_round(
            calculate_feature(&p, FeatureType::Dissimilarity),
            array![
                [0.41666666, 0.6666667],
                [1.0, 1.5]
            ],
            format!("Feature type: {:?}", FeatureType::Dissimilarity)
        );
        assert_eq_round(
            calculate_feature(&p, FeatureType::Homogeneity),
            array![
                [0.80833333, 0.7],
                [0.525, 0.375]
            ],
            format!("Feature type: {:?}", FeatureType::Homogeneity)
        );
        assert_eq_round(
            calculate_feature(&p, FeatureType::Energy),
            array![
                [0.40824829, 0.42491829],
                [0.58630197, 0.53033009],
            ],
            format!("Feature type: {:?}", FeatureType::Energy)
        );
        assert_eq_round(
            calculate_feature(&p, FeatureType::ASM),
            array![
                [0.16666667, 0.18055556],
                [0.34375, 0.28125],
            ],
            format!("Feature type: {:?}", FeatureType::ASM)
        );
        assert_eq_round(
            calculate_feature(&p, FeatureType::Correlation),
            array![
                [0.79698847, 0.70116959],
                [0.85634884, 0.57735027],
            ],
            format!("Feature type: {:?}", FeatureType::Correlation)
        );
    }
}