-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Expand file tree
/
Copy pathutil.rs
More file actions
33 lines (31 loc) · 1.06 KB
/
util.rs
File metadata and controls
33 lines (31 loc) · 1.06 KB
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
use log::{debug, error};
use npyz::WriterBuilder;
use npyz::{AutoSerialize, WriteOptions};
use std::io::BufWriter;
use std::{
fs::File,
io::{self, BufRead},
};
pub fn load_batch_prediction_request_base64(file_name: &str) -> Vec<Vec<u8>> {
let file = File::open(file_name).expect("could not read file");
let mut result = vec![];
for (mut line_count, line) in io::BufReader::new(file).lines().enumerate() {
line_count += 1;
match base64::decode(line.unwrap().trim()) {
Ok(payload) => result.push(payload),
Err(err) => error!("error decoding line {file_name}:{line_count} - {err}"),
}
}
debug!("result len: {}", result.len());
result
}
pub fn save_to_npy<T: npyz::Serialize + AutoSerialize>(data: &[T], save_to: String) {
let mut writer = WriteOptions::new()
.default_dtype()
.shape(&[data.len() as u64, 1])
.writer(BufWriter::new(File::create(save_to).unwrap()))
.begin_nd()
.unwrap();
writer.extend(data.to_owned()).unwrap();
writer.finish().unwrap();
}