Skip to main content

deepcorr_normalization/
lib.rs

1/*
2This file is part of DeepCorr.
3
4DeepCorr is free software: you can redistribute it and/or modify it under 
5the terms of the GNU General Public License as published by the Free 
6Software Foundation, either version 3 of the License, or any later version.
7
8DeepCorr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
9without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
10PURPOSE. See the GNU General Public License for more details.
11
12You should have received a copy of the GNU General Public License along with 
13DeepCorr. If not, see <https://www.gnu.org/licenses/>.
14*/
15
16pub mod cosine;
17pub mod z_score;
18pub mod min_max;
19
20// External
21use thiserror::Error;
22use ndarray::Array2;
23use crate::cosine::CosineNormalizer;
24use crate::z_score::ZScoreNormalizer;
25use crate::min_max::MinMaxNormalizer;
26
27#[derive(Error, Debug)]
28pub enum NormError {
29    #[error("Input data is empty")]
30    EmptyInput,
31    #[error("Zero-magnitude vector found at row {0}")]
32    ZeroMagnitude(usize),
33}
34
35pub enum NormMethod {
36    Cosine,
37    ZScore,
38    MinMaxScore,
39}
40
41pub fn normalize_data(
42    data: &Array2<f64>, 
43    method: NormMethod, 
44    epsilon: f64
45) -> Result<Array2<f64>, NormError> {
46    match method {
47        NormMethod::Cosine => {
48            let n = CosineNormalizer::new(epsilon);
49            n.normalize(data)
50        },
51        NormMethod::ZScore => {
52            let n = ZScoreNormalizer::new(epsilon);
53            n.normalize(data)
54        }
55        NormMethod::MinMaxScore => {
56            let n = MinMaxNormalizer::new(epsilon);
57            n.normalize(data)
58        }
59    }
60}