-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
199 lines (181 loc) · 5.31 KB
/
main.rs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#![recursion_limit = "256"]
#![forbid(unsafe_code)]
use indicatif::{
ProgressBar,
ProgressDrawTarget,
ProgressStyle,
};
use num_derive::{
FromPrimitive,
ToPrimitive,
};
use num_traits::{
FromPrimitive,
ToPrimitive,
};
use structopt::StructOpt;
mod command;
mod common {
pub use std::io::Result;
pub use std::sync::Arc;
pub use std::time::Instant;
pub use anyhow::Context;
pub use indicatif::{
HumanBytes,
HumanDuration,
};
pub use n5::prelude::*;
pub use prettytable::{
cell,
row,
};
pub use structopt::StructOpt;
pub(crate) use crate::{
command::CommandType,
processing::{
BlockReaderMapReduce,
BlockTypeMap,
DataTypeBounds,
},
GridBoundsOption,
Options,
};
pub(crate) fn from_plain_or_json_str<'a, T: serde::Deserialize<'a> + std::str::FromStr>(
serial: &'a str,
) -> serde_json::Result<T> {
serial
.parse::<T>()
.or_else(|_| serde_json::from_str(serial))
}
}
mod iterator;
mod pool;
mod processing;
/// Utilities for N5 files.
#[derive(StructOpt, Debug)]
#[structopt(
name = "n5gest",
author,
global_settings(&[
structopt::clap::AppSettings::ColoredHelp,
structopt::clap::AppSettings::VersionlessSubcommands,
])
)]
struct Options {
/// Number of threads for parallel processing.
/// By default, the number of CPU cores is used.
#[structopt(short = "t", long = "threads")]
threads: Option<usize>,
#[structopt(subcommand)]
command: command::Command,
}
#[derive(FromPrimitive, ToPrimitive)]
enum MetricPrefix {
None = 0,
Kilo,
Mega,
Giga,
Tera,
Peta,
Exa,
Zetta,
Yotta,
}
impl MetricPrefix {
fn reduce(mut number: usize) -> (usize, MetricPrefix) {
let mut order = MetricPrefix::None.to_usize().unwrap();
let max_order = MetricPrefix::Yotta.to_usize().unwrap();
while number > 10_000 && order <= max_order {
number /= 1_000;
order += 1;
}
(number, MetricPrefix::from_usize(order).unwrap())
}
}
impl std::fmt::Display for MetricPrefix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
MetricPrefix::None => " ",
MetricPrefix::Kilo => "K",
MetricPrefix::Mega => "M",
MetricPrefix::Giga => "G",
MetricPrefix::Tera => "T",
MetricPrefix::Peta => "P",
MetricPrefix::Exa => "E",
MetricPrefix::Zetta => "Z",
MetricPrefix::Yotta => "Y",
}
)
}
}
fn main() -> anyhow::Result<()> {
let opt = Options::from_args();
command::dispatch(&opt)
}
fn default_progress_bar(size: u64) -> ProgressBar {
let pbar = ProgressBar::new(size);
pbar.set_draw_target(ProgressDrawTarget::stderr());
pbar.set_style(ProgressStyle::default_bar().template(
"[{elapsed_precise}] [{wide_bar:.cyan/blue}] \
{bytes}/{total_bytes} ({percent}%) [{eta_precise}]",
));
pbar
}
/// Common structopt options for commands that support grid coordinate bounds
/// for their coordinate iterators.
#[derive(StructOpt, Debug)]
struct GridBoundsOption {
/// Axis along which to bound the grid coordinate slab.
#[structopt(long = "slab-axis")]
axis: Option<usize>,
/// Grid coordinate of the slab to bound to along the axis given by `slab-axis`.
/// Equivalent to `--slab-min N --slab-max N + 1`.
#[structopt(
long = "slab-coord",
requires("axis"),
conflicts_with("slab_min"),
conflicts_with("slab_max")
)]
slab_coord: Option<u64>,
/// Min grid coordinate of the slab to bound to along the axis given by `slab-axis`.
#[structopt(long = "slab-min", requires("axis"))]
slab_min: Option<u64>,
/// Max grid coordinate (exclusive) of the slab to bound to along the axis given by `slab-axis`.
#[structopt(long = "slab-max", requires("axis"))]
slab_max: Option<u64>,
}
impl GridBoundsOption {
fn to_factory(&self) -> anyhow::Result<Box<dyn iterator::CoordIteratorFactory>> {
match (self.axis, self.slab_coord) {
(Some(axis), Some(slab_coord)) => Ok(Box::new(iterator::GridSlabCoordIter {
axis,
slab: (Some(slab_coord), Some(slab_coord + 1)),
})),
(Some(axis), None) => {
if let (Some(a), Some(b)) = (self.slab_min, self.slab_max) {
if b <= a {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"Slab max coord must be greater than slab min coord",
)
.into());
}
}
Ok(Box::new(iterator::GridSlabCoordIter {
axis,
slab: (self.slab_min, self.slab_max),
}))
}
_ => Ok(Box::new(iterator::DefaultCoordIter {})),
}
}
}
/// Container with dataset and attributes, for argument brevity.
struct ContainedDataset<N5> {
container: N5,
name: String,
attrs: n5::DatasetAttributes,
}