mine_rs/
mine.rs

1use crate::bindings::{mine_compute_score, mine_mic, mine_parameter, mine_problem, mine_score,self};
2
3pub struct Mine {
4    alpha: f64,
5    c: f64,
6    x: Vec<f64>,
7    y: Vec<f64>,
8    score: Option<mine_score>,
9}
10
11unsafe impl Send for Mine {}
12
13impl Mine {
14    pub fn new(alpha: f64, c: f64) -> Self {
15        Self {
16            alpha,
17            c,
18            x: Vec::new(),
19            y: Vec::new(),
20            score: None,
21        }
22    }
23    pub fn compute_score(&mut self, x: Vec<f64>, y: Vec<f64>) {
24        let n = x.len() as i32;
25        self.x = x;
26        self.y = y;
27        let mut prob = mine_problem {
28            n,
29            x: self.x.as_mut_ptr(),
30            y: self.y.as_mut_ptr(),
31        };
32        let est = bindings::EST_MIC_APPROX as i32;
33        let mut param = mine_parameter {
34            alpha: self.alpha,
35            c: self.c,
36            est,
37        };
38        let score = unsafe { mine_compute_score(&mut prob, &mut param) };
39        let score = unsafe { *score };
40        self.score = Some(score);
41    }
42    pub fn mic(&mut self) -> Option<f64> {
43        self.score.as_mut().map(|score| unsafe { mine_mic(score) })
44    }
45    pub fn mas(&mut self) -> Option<f64> {
46        self.score.as_mut().map(|score| unsafe { bindings::mine_mas(score) })
47    }
48    pub fn mev(&mut self) -> Option<f64> {
49        self.score.as_mut().map(|score| unsafe { bindings::mine_mev(score) })
50    }
51    pub fn mcn(&mut self,eps: f64) -> Option<f64> {
52        self.score.as_mut().map(|score| unsafe { bindings::mine_mcn(score, eps) })
53    }
54    pub fn gmic(&mut self,p:f64) -> Option<f64> {
55        self.score.as_mut().map(|score| unsafe { bindings::mine_gmic(score, p) })
56    }
57    pub fn tic(&mut self,norm: bool)  -> Option<f64> {
58        self.score.as_mut().map(|score| unsafe { bindings::mine_tic(score, norm as i32) })
59    }
60    
61}
62/// Calculates the MIC score for a given dataset.
63///
64/// This function takes in two vectors of data points `x` and `y`, as well
65/// as two parameters, `alpha` and `c`. It returns the MIC score for the
66/// given dataset.
67pub fn compute_mic(x: &mut [f64], y: &mut [f64], alpha: f64, c: f64) -> f64 {
68    let n = x.len() as i32;
69    let mut prob = mine_problem {
70        n,
71        x: x.as_mut_ptr(),
72        y: y.as_mut_ptr(),
73    };
74    let est = bindings::EST_MIC_APPROX as i32;
75    let mut param = mine_parameter { alpha, c, est };
76    let score = unsafe { mine_compute_score(&mut prob, &mut param) };
77    unsafe { bindings::mine_mic(score) }
78}