1use std::path::{Path, PathBuf};
2
3use rand::{distributions::Alphanumeric, prelude::{ThreadRng, SliceRandom}, Rng};
4use walkdir::WalkDir;
5
6
7pub fn get_paths<P: AsRef<Path>>(path: P) -> Result<(Vec<PathBuf>, Vec<PathBuf>), std::io::Error> {
8 let mut entries = std::fs::read_dir(&path)?
9 .flat_map(|res| res.map(|e| e.path()))
10 .filter(|path| path.is_dir())
11 .collect::<Vec<PathBuf>>();
12
13 entries.sort();
14
15 let paths: Vec<PathBuf> = WalkDir::new(path)
16 .into_iter()
17 .flat_map(|entry| entry.map(|e| e.path().to_path_buf()))
18 .filter(|path|
19 path.is_file() &&
20 !path.file_name().unwrap().to_str().unwrap().starts_with('.'))
21 .collect();
22
23 Ok((entries, paths))
24}
25
26pub fn create_path_with_hash<P: AsRef<Path>, P1: AsRef<Path>>(image_path: P, output_to: P1, hash: u64) -> Result<PathBuf, std::io::Error> {
27 let image_path = image_path.as_ref();
28
29 let file_name = format!("{}_{}", hash, image_path.file_name().unwrap().to_str().unwrap());
30 let folder_from_file_path = image_path.parent().unwrap();
31 let folder_name = folder_from_file_path.file_name().unwrap().to_str().unwrap();
32 let path = format!("{}/{}", folder_name, file_name);
33 let output_to = Path::new(output_to.as_ref());
34 let path = output_to.join(path);
35 Ok(path)
36}
37
38pub fn create_new_path_from_old<P: AsRef<Path>, P1: AsRef<Path>>(image_path: P, output_to: P1, rng: &mut ThreadRng) -> Result<PathBuf, std::io::Error> {
39 let addition: String = std::iter::repeat(())
40 .map(|()| rng.sample(Alphanumeric))
41 .map(char::from)
42 .take(22)
43 .collect();
44
45 let image_path = image_path.as_ref();
46
47 let file_name = format!("{}_{}", addition, image_path.file_name().unwrap().to_str().unwrap());
48 let folder_from_file_path = image_path.parent().unwrap();
49 let folder_name = folder_from_file_path.file_name().unwrap().to_str().unwrap();
50 let path = format!("{}/{}", folder_name, file_name);
51 let output_to = Path::new(output_to.as_ref());
52 let path = output_to.join(path);
53 Ok(path)
54}
55
56
57pub fn shuffle<T: Copy, G: Copy>(rows: usize, data: &[T], y_values: &[G]) -> (Vec<T>, Vec<G>) {
58 let mut x: Vec<usize> = (0..rows).into_iter().collect();
59
60 let mut rng = rand::thread_rng();
61 x.shuffle(&mut rng);
62
63 let mut shuffeled = Vec::<T>::new();
64 let mut y_shuffeled = Vec::<G>::new();
65
66 let cols = data.len() / rows;
67
68 for idx in x.iter() {
69 y_shuffeled.push(y_values[*idx]);
70
71 let i = idx*cols;
72 let row = &data[i..i+cols];
73 for value in row {
74 shuffeled.push(*value);
75 }
76 }
77
78 (shuffeled, y_shuffeled)
79}
80
81pub fn random_short_mut<T: Clone+Default>(reduce_arr: &mut [T], max_len: usize) -> Vec<T> {
83 let mut rng = rand::thread_rng();
84 let mut final_arr = Vec::<T>::with_capacity(max_len);
85
86 reduce_arr.shuffle(&mut rng);
87 for elem in &reduce_arr[..max_len] {
88 final_arr.push(elem.clone());
89 }
90 final_arr
91}
92
93pub fn min<T: Copy+PartialOrd>(array: &[T]) -> T {
94 let mut min = array[0];
95 for value in array {
96 if value < &min {
97 min = *value;
98 }
99 }
100 min
101}
102
103pub fn max<T: Copy+PartialOrd>(array: &[T]) -> T {
104 let mut max = array[0];
105 for value in array {
106 if value > &max {
107 max = *value;
108 }
109 }
110 max
111}
112
113pub fn find_idxs<T: Copy+PartialEq>(rows: usize, search_for: &[T], search_with: &[T]) -> Vec<usize> {
117 let cols = search_for.len()/rows;
118
119 let mut idxs = vec![0usize; rows];
120
121 for idx in 0..rows {
122 let index = idx*cols;
123 let row = &search_for[index..index+cols];
124 for (row_idx, value) in row.iter().enumerate() {
125 if *value == search_with[idx] {
126 idxs[idx] = row_idx;
127 }
128 }
129 }
130 idxs
131}
132