1use crate::types::{ClusteringResult, DataMatrix, DistanceMetric};
7use serde::{Deserialize, Serialize};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct KMeansInput {
16 pub data: DataMatrix,
18 pub k: usize,
20 pub max_iterations: u32,
22 pub tolerance: f64,
24}
25
26impl KMeansInput {
27 pub fn new(data: DataMatrix, k: usize) -> Self {
29 Self {
30 data,
31 k,
32 max_iterations: 100,
33 tolerance: 1e-4,
34 }
35 }
36
37 pub fn with_max_iterations(mut self, max_iterations: u32) -> Self {
39 self.max_iterations = max_iterations;
40 self
41 }
42
43 pub fn with_tolerance(mut self, tolerance: f64) -> Self {
45 self.tolerance = tolerance;
46 self
47 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct KMeansOutput {
53 pub result: ClusteringResult,
55 pub compute_time_us: u64,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct DBSCANInput {
66 pub data: DataMatrix,
68 pub eps: f64,
70 pub min_samples: usize,
72 pub metric: DistanceMetric,
74}
75
76impl DBSCANInput {
77 pub fn new(data: DataMatrix, eps: f64, min_samples: usize) -> Self {
79 Self {
80 data,
81 eps,
82 min_samples,
83 metric: DistanceMetric::Euclidean,
84 }
85 }
86
87 pub fn with_metric(mut self, metric: DistanceMetric) -> Self {
89 self.metric = metric;
90 self
91 }
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct DBSCANOutput {
97 pub result: ClusteringResult,
99 pub compute_time_us: u64,
101}
102
103#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
109pub enum Linkage {
110 Single,
112 Complete,
114 Average,
116 Ward,
118}
119
120#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct HierarchicalInput {
123 pub data: DataMatrix,
125 pub n_clusters: usize,
127 pub linkage: Linkage,
129 pub metric: DistanceMetric,
131}
132
133impl HierarchicalInput {
134 pub fn new(data: DataMatrix, n_clusters: usize) -> Self {
136 Self {
137 data,
138 n_clusters,
139 linkage: Linkage::Complete,
140 metric: DistanceMetric::Euclidean,
141 }
142 }
143
144 pub fn with_linkage(mut self, linkage: Linkage) -> Self {
146 self.linkage = linkage;
147 self
148 }
149
150 pub fn with_metric(mut self, metric: DistanceMetric) -> Self {
152 self.metric = metric;
153 self
154 }
155}
156
157#[derive(Debug, Clone, Serialize, Deserialize)]
159pub struct HierarchicalOutput {
160 pub result: ClusteringResult,
162 pub compute_time_us: u64,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
172pub struct IsolationForestInput {
173 pub data: DataMatrix,
175 pub n_trees: usize,
177 pub contamination: f64,
179}
180
181impl IsolationForestInput {
182 pub fn new(data: DataMatrix) -> Self {
184 Self {
185 data,
186 n_trees: 100,
187 contamination: 0.1,
188 }
189 }
190
191 pub fn with_n_trees(mut self, n_trees: usize) -> Self {
193 self.n_trees = n_trees;
194 self
195 }
196
197 pub fn with_contamination(mut self, contamination: f64) -> Self {
199 self.contamination = contamination;
200 self
201 }
202}
203
204#[derive(Debug, Clone, Serialize, Deserialize)]
206pub struct AnomalyOutput {
207 pub scores: Vec<f64>,
209 pub labels: Vec<i32>,
211 pub threshold: f64,
213 pub compute_time_us: u64,
215}
216
217#[derive(Debug, Clone, Serialize, Deserialize)]
219pub struct LOFInput {
220 pub data: DataMatrix,
222 pub n_neighbors: usize,
224 pub contamination: f64,
226 pub metric: DistanceMetric,
228}
229
230impl LOFInput {
231 pub fn new(data: DataMatrix) -> Self {
233 Self {
234 data,
235 n_neighbors: 20,
236 contamination: 0.1,
237 metric: DistanceMetric::Euclidean,
238 }
239 }
240
241 pub fn with_n_neighbors(mut self, n_neighbors: usize) -> Self {
243 self.n_neighbors = n_neighbors;
244 self
245 }
246}
247
248#[derive(Debug, Clone, Serialize, Deserialize)]
254pub struct RegressionInput {
255 pub x: DataMatrix,
257 pub y: Vec<f64>,
259 pub fit_intercept: bool,
261 pub alpha: Option<f64>,
263}
264
265impl RegressionInput {
266 pub fn new(x: DataMatrix, y: Vec<f64>) -> Self {
268 Self {
269 x,
270 y,
271 fit_intercept: true,
272 alpha: None,
273 }
274 }
275
276 pub fn with_ridge(mut self, alpha: f64) -> Self {
278 self.alpha = Some(alpha);
279 self
280 }
281}
282
283#[derive(Debug, Clone, Serialize, Deserialize)]
285pub struct RegressionOutput {
286 pub coefficients: Vec<f64>,
288 pub intercept: Option<f64>,
290 pub r_squared: f64,
292 pub compute_time_us: u64,
294}
295
296#[cfg(test)]
297mod tests {
298 use super::*;
299
300 #[test]
301 fn test_kmeans_input_builder() {
302 let data = DataMatrix::from_rows(&[&[1.0, 2.0], &[3.0, 4.0]]);
303 let input = KMeansInput::new(data, 2)
304 .with_max_iterations(50)
305 .with_tolerance(1e-6);
306 assert_eq!(input.k, 2);
307 assert_eq!(input.max_iterations, 50);
308 }
309
310 #[test]
311 fn test_dbscan_input_builder() {
312 let data = DataMatrix::from_rows(&[&[1.0, 2.0]]);
313 let input = DBSCANInput::new(data, 0.5, 3).with_metric(DistanceMetric::Manhattan);
314 assert_eq!(input.eps, 0.5);
315 assert_eq!(input.min_samples, 3);
316 }
317}