Skip to content

Commit

Permalink
Add more timing tools
Browse files Browse the repository at this point in the history
  • Loading branch information
mobiusklein committed Oct 26, 2023
1 parent 4d4cd80 commit 79e577c
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ lto = true
debug = true

[features]
default = ["intel-mkl", "parallelism", "plotting", "mzmlb", "zlib-ng-compat"]
default = ["intel-mkl", "parallelism", "plotting", "mzmlb", "zlib-ng-compat", ]

openblas = ["mzsignal/openblas"]
netlib = ["mzsignal/netlib"]
Expand Down
67 changes: 67 additions & 0 deletions examples/async_mzcat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{io, path, env};
use std::time;

#[cfg(not(feature = "async"))]
compile_error!("This example requires the async feature");

Check failure on line 5 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

This example requires the async feature

use tokio;

Check failure on line 7 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

unresolved import `tokio`
use tokio::fs;

use mzdata::io::mzml;
use mzdata::io::prelude::*;

Check warning on line 11 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

unused import: `mzdata::io::prelude`

async fn load_file<P: Into<path::PathBuf> + Clone>(path: P) -> io::Result<mzml::AsyncMzMLReader<fs::File>> {

Check failure on line 13 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

cannot find type `AsyncMzMLReader` in module `mzml`
let fh = fs::File::open(path.into()).await?;
let mut reader = mzml::AsyncMzMLReader::new(fh).await;

Check failure on line 15 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

failed to resolve: could not find `AsyncMzMLReader` in `mzml`
reader.read_index_from_end().await.expect("Failed to read index from the file");
Ok(reader)
}


async fn scan_file(
reader: &mut mzml::AsyncMzMLReader<fs::File>,

Check failure on line 22 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

cannot find type `AsyncMzMLReader` in module `mzml`
) {
let start = time::Instant::now();
let n = reader.len();
let mut i = 0;
while let Some(scan) = reader.get_spectrum_by_index(i).await {
if i % 10000 == 0 {
println!(
"\tScan {}: {}|{} ({} seconds)",
i,
scan.id(),
scan.index(),
(time::Instant::now() - start).as_secs_f64(),
);
}
i += 1;
if i == n {
break;
}
}
let end = time::Instant::now();
println!("Loaded in {} spectra {} seconds", i, (end - start).as_secs_f64());
}


#[tokio::main(flavor = "multi_thread", worker_threads = 10)]

Check failure on line 47 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

cannot determine resolution for the attribute macro `tokio::main`
async fn main() -> io::Result<()> {

Check failure on line 48 in examples/async_mzcat.rs

View workflow job for this annotation

GitHub Actions / Test Suite (ubuntu-latest)

`main` function is not allowed to be `async`
let path = path::PathBuf::from(
env::args()
.skip(1)
.next()
.expect("Please pass an MS data file path"),
);
if let Some(ext) = path.extension() {
if ext.to_string_lossy().to_lowercase() == "mzml" {
let mut reader = load_file(path).await?;
scan_file(&mut reader).await;
} else {
panic!("Could not infer the file format")
}
} else {
let mut reader = load_file(path).await?;
scan_file(&mut reader).await;
};
Ok(())
}
91 changes: 91 additions & 0 deletions examples/mzcat.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use std::{io, path, env, fs};
use std::time;

use rayon::prelude::*;

use mzdata::io::{mzml, mzmlb};
use mzdata::io::prelude::*;
use mzdata::spectrum::MultiLayerSpectrum;
use mzpeaks::{CentroidPeak, DeconvolutedPeak};


fn load_file<P: Into<path::PathBuf> + Clone>(path: P) -> io::Result<mzml::MzMLReader<fs::File>> {
let reader = mzml::MzMLReader::open_path(path)?;
Ok(reader)
}

#[cfg(feature = "mzmlb")]
fn load_mzmlb_file<P: Into<path::PathBuf> + Clone>(path: P) -> io::Result<mzmlb::MzMLbReader> {
let reader = mzmlb::MzMLbReader::open_path(&path.into())?;
let blosc_threads = match std::env::var("BLOSC_NUM_THREADS") {
Ok(val) => {
match val.parse() {
Ok(nt) => nt,
Err(e) => {
eprintln!("Failed to parse BLOSC_NUM_THREADS env var: {}", e);
4
},
}
},
Err(_) => 4,
};
mzmlb::MzMLbReader::set_blosc_nthreads(blosc_threads);
Ok(reader)
}

fn scan_file<
R: MZFileReader<
CentroidPeak,
DeconvolutedPeak,
MultiLayerSpectrum<CentroidPeak, DeconvolutedPeak>
> + Iterator<Item=MultiLayerSpectrum<CentroidPeak, DeconvolutedPeak>> + Send,
>(
reader: &mut R,
) {
let start = time::Instant::now();
reader.enumerate().par_bridge().for_each(|(i, scan)| {
if i % 10000 == 0 {
println!(
"\tScan {}: {}|{} ({} seconds)",
i,
scan.id(),
scan.index(),
(time::Instant::now() - start).as_secs_f64(),
);
}
});
let end = time::Instant::now();
println!("Loaded in {} seconds", (end - start).as_secs_f64());
}


fn main() -> io::Result<()> {
let path = path::PathBuf::from(
env::args()
.skip(1)
.next()
.expect("Please pass an MS data file path"),
);
if let Some(ext) = path.extension() {
if ext.to_string_lossy().to_lowercase() == "mzmlb" {
#[cfg(feature = "mzmlb")]
{
let mut reader = load_mzmlb_file(path)?;
scan_file(&mut reader)
}
#[cfg(not(feature = "mzmlb"))]
{
panic!("Cannot read mzMLb file. Recompile enabling the `mzmlb` feature")
}
} else if ext.to_string_lossy().to_lowercase() == "mzml" {
let mut reader = load_file(path)?;
scan_file(&mut reader)
} else {
panic!("Could not infer the file format")
}
} else {
let mut reader = load_file(path)?;
scan_file(&mut reader)
};
Ok(())
}
1 change: 1 addition & 0 deletions src/io/mzml/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::marker::PhantomData;
use std::mem;
use std::pin::Pin;

use super::MzMLSAX;
use super::reader::{
MzMLParserError, MzMLParserState, SpectrumBuilding,
Bytes, FileMetadataBuilder, MzMLSpectrumBuilder,
Expand Down
4 changes: 2 additions & 2 deletions src/io/mzmlb/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,11 +483,11 @@ impl Seek for ByteReader {
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
match pos {
io::SeekFrom::Start(offset) => {
self.position = (self.position + offset as usize).min(0);
self.position = (offset as usize).max(0);
}
io::SeekFrom::End(offset) => {
let mut n = self.handle.size();
self.position = (n + offset as usize).min(0);
self.position = (n + offset as usize).max(n);
}
io::SeekFrom::Current(offset) => {
self.position = (self.position + offset as usize).max(0);
Expand Down

0 comments on commit 79e577c

Please sign in to comment.