Skip to content

Commit d6290db

Browse files
committed
Add depth_threshold when build chi2 MOMs
1 parent 8f28b2d commit d6290db

6 files changed

Lines changed: 181 additions & 108 deletions

File tree

crates/cli/src/map.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::marker::PhantomData;
2-
use std::{error::Error, path::PathBuf};
1+
use std::{error::Error, path::PathBuf, marker::PhantomData};
32

43
use clap::{Args, Subcommand, ValueEnum};
54
use log::{error, warn};

crates/cli/src/mom.rs

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ use hpxlib::nested::{
1818
img::{ColorMapFunctionType, PosConversion, Val},
1919
mom::{
2020
impls::{bslice::FITSMom, zvec::MomVecImpl},
21-
new_chi2_count_merger, Mom as MomTrait, ViewableMom, WritableMom,
21+
new_chi2_count_merger_no_depth_threshold, new_chi2_count_merger_with_depth_threshold,
22+
Mom as MomTrait, ViewableMom, WritableMom,
2223
},
2324
skymap::SkyMapValue,
2425
},
@@ -64,14 +65,22 @@ pub struct Count {
6465
/// Completeness of the chi2 distribution of 3 degrees of freedom.
6566
#[clap(default_value_t = 16.266)]
6667
threshold: f64,
68+
/// Depth threshold
69+
#[clap(short = 't', long, value_name = "DEPTH_MIN")]
70+
depth_threshold: Option<u8>,
6771
/// Count MOM destination FITS file.
6872
output: PathBuf,
6973
#[clap(subcommand)]
7074
input: PosOrHash,
7175
}
7276
impl Count {
7377
pub fn exec(self) -> Result<(), Box<dyn Error>> {
74-
self.input.exec(self.depth, self.threshold, self.output)
78+
self.input.exec(
79+
self.depth,
80+
self.threshold,
81+
self.depth_threshold,
82+
self.output,
83+
)
7584
}
7685
}
7786

@@ -89,22 +98,30 @@ enum PosOrHash {
8998
},
9099
}
91100
impl PosOrHash {
92-
pub fn exec(self, depth: u8, threshold: f64, output: PathBuf) -> Result<(), Box<dyn Error>> {
101+
pub fn exec(
102+
self,
103+
depth: u8,
104+
threshold: f64,
105+
depth_threshold: Option<u8>,
106+
output: PathBuf,
107+
) -> Result<(), Box<dyn Error>> {
93108
match self {
94-
Self::Pos { input } => build_count_mom_from_pos(depth, threshold, input),
95-
Self::Hash { input } => build_count_mom_from_hash(depth, threshold, input),
109+
Self::Pos { input } => build_count_mom_from_pos(depth, threshold, depth_threshold, input),
110+
Self::Hash { input } => build_count_mom_from_hash(depth, threshold, depth_threshold, input),
96111
}
97112
.and_then(|mom| mom.to_fits_file(output, "DENSITY").map_err(|e| e.into()))
98113
}
99114
}
100115
fn build_count_mom_from_pos(
101116
depth: u8,
102117
threshold: f64,
118+
depth_threshold: Option<u8>,
103119
input: PosListInput,
104120
) -> Result<MomVecImpl<u64, u32>, Box<dyn Error>> {
105121
struct Op {
106122
depth: u8,
107123
threshold: f64,
124+
depth_threshold: Option<u8>,
108125
}
109126
impl PosItOperation for Op {
110127
type R = MomVecImpl<u64, u32>;
@@ -123,25 +140,37 @@ fn build_count_mom_from_pos(
123140
self.depth,
124141
SortedHashIt::new(self.depth, it),
125142
);
126-
let chi2_merger = new_chi2_count_merger(self.threshold);
127-
MomVecImpl::<u64, u32>::from_hpx_sorted_entries_fallible(
128-
self.depth,
129-
hash_count_it,
130-
chi2_merger,
131-
)
143+
match self.depth_threshold {
144+
None => MomVecImpl::<u64, u32>::from_hpx_sorted_entries_fallible(
145+
self.depth,
146+
hash_count_it,
147+
new_chi2_count_merger_no_depth_threshold(self.threshold),
148+
),
149+
Some(depth_threshold) => MomVecImpl::<u64, u32>::from_hpx_sorted_entries_fallible(
150+
self.depth,
151+
hash_count_it,
152+
new_chi2_count_merger_with_depth_threshold(self.threshold, depth_threshold),
153+
),
154+
}
132155
.map_err(|e| e.into())
133156
}
134157
}
135-
input.exec(Op { depth, threshold })
158+
input.exec(Op {
159+
depth,
160+
threshold,
161+
depth_threshold,
162+
})
136163
}
137164
fn build_count_mom_from_hash(
138165
depth: u8,
139166
threshold: f64,
167+
depth_threshold: Option<u8>,
140168
input: HashListInput,
141169
) -> Result<MomVecImpl<u64, u32>, Box<dyn Error>> {
142170
struct Op {
143171
depth: u8,
144172
threshold: f64,
173+
depth_threshold: Option<u8>,
145174
}
146175
impl HashItOperation for Op {
147176
type R = MomVecImpl<u64, u32>;
@@ -157,16 +186,26 @@ fn build_count_mom_from_hash(
157186
}
158187
});
159188
let hash_count_it = SortedHash2HashCountIncludingZeroIt::new_from_infallible(self.depth, it);
160-
let chi2_merger = new_chi2_count_merger(self.threshold);
161-
MomVecImpl::<u64, u32>::from_hpx_sorted_entries_fallible(
162-
self.depth,
163-
hash_count_it,
164-
chi2_merger,
165-
)
189+
match self.depth_threshold {
190+
None => MomVecImpl::<u64, u32>::from_hpx_sorted_entries_fallible(
191+
self.depth,
192+
hash_count_it,
193+
new_chi2_count_merger_no_depth_threshold(self.threshold),
194+
),
195+
Some(depth_threshold) => MomVecImpl::<u64, u32>::from_hpx_sorted_entries_fallible(
196+
self.depth,
197+
hash_count_it,
198+
new_chi2_count_merger_with_depth_threshold(self.threshold, depth_threshold),
199+
),
200+
}
166201
.map_err(|e| e.into())
167202
}
168203
}
169-
input.exec(Op { depth, threshold })
204+
input.exec(Op {
205+
depth,
206+
threshold,
207+
depth_threshold,
208+
})
170209
}
171210

172211
#[derive(Debug, Subcommand)]
@@ -229,6 +268,9 @@ pub enum OpType {
229268
/// Completeness of the chi2 distribution of 3 degrees of freedom below which cells are post-merged.
230269
#[clap(short, long, default_value_t = 16.266)]
231270
threshold: f64,
271+
/// Depth threshold
272+
#[clap(short = 't', long, value_name = "DEPTH_MIN")]
273+
depth_threshold: Option<u8>,
232274
},
233275
}
234276

@@ -257,6 +299,7 @@ impl Operation {
257299
cte,
258300
value_name,
259301
threshold,
302+
depth_threshold,
260303
} => match (l, r) {
261304
(FITSMom::U32U32(_l), FITSMom::U32U32(_r)) => {
262305
Err(String::from("Method not available for integers").into())
@@ -267,6 +310,7 @@ impl Operation {
267310
r.get_mom(),
268311
cte as f32,
269312
threshold,
313+
depth_threshold,
270314
)
271315
.to_fits_file(self.output, value_name)
272316
.map_err(|e| e.into())
@@ -277,6 +321,7 @@ impl Operation {
277321
r.get_mom(),
278322
cte,
279323
threshold,
324+
depth_threshold,
280325
)
281326
.to_fits_file(self.output, value_name)
282327
.map_err(|e| e.into())
@@ -290,6 +335,7 @@ impl Operation {
290335
r.get_mom(),
291336
cte as f32,
292337
threshold,
338+
depth_threshold,
293339
)
294340
.to_fits_file(self.output, value_name)
295341
.map_err(|e| e.into())
@@ -300,6 +346,7 @@ impl Operation {
300346
r.get_mom(),
301347
cte,
302348
threshold,
349+
depth_threshold,
303350
)
304351
.to_fits_file(self.output, value_name)
305352
.map_err(|e| e.into())

src/nested/map/mom/impls/zvec.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ where
767767
/// * `chi2_of_3dof_threshold`: threshold on the value of the chi square distribution with 3
768768
/// degrees of freedom below which we consider the 4 values of 4 sibling cells as coming
769769
/// from the same normal distribution which mean and variance comes from a poisson distribution.
770+
/// * `depth_threshold`: threshold on `depth` to avoid making to low resolution cells, i.e MOM minimum depth
771+
///
770772
/// Here a few typical values corresponding the the given completeness:
771773
/// * Completeness = 90.0% => 6.251
772774
/// * Completeness = 95.0% => 7.815
@@ -778,6 +780,7 @@ where
778780
rhs: R,
779781
cte: V,
780782
chi2_of_3dof_threshold: f64,
783+
depth_threshold: Option<u8>,
781784
) -> Self
782785
where
783786
L: Mom<'s, ZUniqHType = Z, ValueType = V>,
@@ -796,7 +799,7 @@ where
796799
rhs,
797800
split,
798801
op,
799-
new_chi2_density_merger(chi2_of_3dof_threshold),
802+
new_chi2_density_merger(chi2_of_3dof_threshold, depth_threshold),
800803
)
801804
}
802805
}
@@ -811,7 +814,9 @@ mod tests {
811814
n_hash,
812815
nested::map::{
813816
img::{to_mom_png_file, ColorMapFunctionType, PosConversion},
814-
mom::{impls::zvec::MomVecImpl, new_chi2_count_ref_merger, Mom, ZUniqHashT},
817+
mom::{
818+
impls::zvec::MomVecImpl, new_chi2_count_ref_merger_no_depth_threshold, Mom, ZUniqHashT,
819+
},
815820
skymap::SkyMapEnum,
816821
},
817822
};
@@ -882,7 +887,7 @@ mod tests {
882887
SkyMapEnum::ImplicitU64I32(skymap) => {
883888
// println!("Skymap size: {}", skymap.len());
884889

885-
let merger = new_chi2_count_ref_merger(16.266);
890+
let merger = new_chi2_count_ref_merger_no_depth_threshold(16.266);
886891
let mut mom = MomVecImpl::from_skymap_ref(&skymap, merger);
887892
/*println!("Mom len: {}", mom.entries.len());
888893
for (z, v) in mom.entries {

0 commit comments

Comments
 (0)