Skip to content

Commit

Permalink
feat: Add par query and rm fold in agg trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jspaezp committed Sep 24, 2024
1 parent 989049c commit b925ef5
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 26 deletions.
22 changes: 9 additions & 13 deletions src/models/aggregators/raw_peak_agg/chromatogram_agg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ impl Aggregator<RawPeak> for ExtractedIonChromatomobilogram {
.or_insert(u64_intensity);
}

fn fold(&mut self, other: Self) {
panic!("Not Implemented;")
}

fn finalize(self) -> ChromatomobilogramVectorArrayTuples {
ChromatomobilogramVectorArrayTuples {
scan_indices: self.scan_tree.into_iter().collect(),
Expand All @@ -72,6 +68,13 @@ impl Aggregator<RawPeak> for ExtractedIonChromatomobilogram {
// type MappingCollection<T1, T2> = BTreeMap<T1, T2>;
type MappingCollection<T1, T2> = HashMap<T1, T2>;

#[derive(Debug, Clone)]
pub struct MultiChromatomobilogramStats {
pub scan_tof_mapping:
MappingCollection<(usize, u32), (RunningStatsCalculator, RunningStatsCalculator)>,
pub id: u64,
}

#[derive(Debug, Clone)]
pub struct ChromatomobilogramStats {
// TODO OPTIMIZE THIS ... as needed.
Expand Down Expand Up @@ -107,7 +110,6 @@ impl Aggregator<RawPeak> for ChromatomobilogramStats {
let u64_intensity = peak.intensity as u64;
let rt_miliseconds = (peak.retention_time * 1000.0) as u32;

// TODO make this macro as well ...
self.scan_tof_mapping
.entry(rt_miliseconds)
.and_modify(|curr| {
Expand All @@ -120,15 +122,9 @@ impl Aggregator<RawPeak> for ChromatomobilogramStats {
));
}

fn fold(&mut self, _other: Self) {
panic!("Not Implemented;")
}

fn finalize(self) -> ChromatomobilogramStatsArrays {
let ((scan_means, scan_sds), (tof_means, tof_sds)): (
(Vec<f64>, Vec<f64>),
(Vec<f64>, Vec<f64>),
) = self
type VecTuple = (Vec<f64>, Vec<f64>);
let ((scan_means, scan_sds), (tof_means, tof_sds)): (VecTuple, VecTuple) = self
.scan_tof_mapping
.values()
.map(|(scan, tof)| {
Expand Down
4 changes: 2 additions & 2 deletions src/models/indices/raw_file_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ impl RawFileIndex {
}

impl IndexedData<FragmentGroupIndexQuery, RawPeak> for RawFileIndex {
fn query(&self, fragment_query: &FragmentGroupIndexQuery) -> Option<Vec<RawPeak>> {
fn query(&self, fragment_query: &FragmentGroupIndexQuery) -> Vec<RawPeak> {
let mut out = Vec::new();
self.apply_on_query(fragment_query, &mut |peak, _| out.push(peak));
Some(out)
out
}

fn add_query<O, AG: crate::Aggregator<RawPeak, Output = O>>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ impl QuadSplittedTransposedIndex {
rt_range,
precursor_mz_range,
);
// let matching_quads = self.get_matching_quad_settings(precursor_mz_range, scan_range);
let matching_quads: Vec<SingleQuadrupoleSetting> = self
.get_matching_quad_settings(precursor_mz_range, scan_range)
.collect();
Expand Down Expand Up @@ -127,7 +126,8 @@ impl QuadSplittedTransposedIndex {

assert!(frame_index_range.0 <= frame_index_range.1);
assert!(mz_index_range.0 <= mz_index_range.1);
// assert!(mobility_index_range.0 <= mobility_index_range.1);
// Since mobilities get mixed up bc low scan ranges are high 1/k0, I
// Just make sure they are sorted here.
let mobility_index_range = (
mobility_index_range.0.min(mobility_index_range.1),
mobility_index_range.1.max(mobility_index_range.0),
Expand Down Expand Up @@ -402,7 +402,7 @@ fn display_opt_peak_bucket_vec(opt_peak_buckets: &[Option<PeakBucket>]) -> Strin
}

impl IndexedData<FragmentGroupIndexQuery, RawPeak> for QuadSplittedTransposedIndex {
fn query(&self, fragment_query: &FragmentGroupIndexQuery) -> Option<Vec<RawPeak>> {
fn query(&self, fragment_query: &FragmentGroupIndexQuery) -> Vec<RawPeak> {
let precursor_mz_range = (
fragment_query.precursor_query.isolation_mz_range.0 as f64,
fragment_query.precursor_query.isolation_mz_range.0 as f64,
Expand All @@ -412,7 +412,7 @@ impl IndexedData<FragmentGroupIndexQuery, RawPeak> for QuadSplittedTransposedInd
fragment_query.precursor_query.frame_index_range,
));

let out = fragment_query
fragment_query
.mz_index_ranges
.iter()
.flat_map(|tof_range| {
Expand All @@ -424,9 +424,7 @@ impl IndexedData<FragmentGroupIndexQuery, RawPeak> for QuadSplittedTransposedInd
)
.map(RawPeak::from)
})
.collect();

Some(out)
.collect()
}

fn add_query<O, AG: crate::Aggregator<RawPeak, Output = O>>(
Expand Down Expand Up @@ -466,8 +464,8 @@ impl IndexedData<FragmentGroupIndexQuery, RawPeak> for QuadSplittedTransposedInd
aggregator: &mut [AG],
) {
fragment_queries
.iter()
.zip(aggregator.iter_mut())
.par_iter()
.zip(aggregator.par_iter_mut())
.for_each(|(fragment_query, agg)| {
let precursor_mz_range = (
fragment_query.precursor_query.isolation_mz_range.0 as f64,
Expand Down
2 changes: 1 addition & 1 deletion src/traits/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ pub trait Aggregator<I>: Send + Sync {
type Output;

fn add(&mut self, item: &I);
fn fold(&mut self, item: Self);
// fn fold(&mut self, item: Self);
fn finalize(self) -> Self::Output;
}
2 changes: 1 addition & 1 deletion src/traits/indexed_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Aggregator;

pub trait IndexedData<QF, A> {
fn query(&self, fragment_query: &QF) -> Option<Vec<A>>;
fn query(&self, fragment_query: &QF) -> Vec<A>;
fn add_query<O, AG: Aggregator<A, Output = O>>(&self, fragment_query: &QF, aggregator: &mut AG);
fn add_query_multi_group<O, AG: Aggregator<A, Output = O>>(
&self,
Expand Down

0 comments on commit b925ef5

Please sign in to comment.