Skip to content

Commit

Permalink
fix: tests for Openslide 4
Browse files Browse the repository at this point in the history
  • Loading branch information
AzHicham committed Dec 9, 2023
1 parent 49a1ca7 commit 6613a4a
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 48 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ fast_image_resize = { version = "2.7", optional = true}
rstest = "0.18"
assert_approx_eq = "1.1"
bencher = "0.1"
version-compare = "0.1"

[[bench]]
name = "bench_read"
Expand Down
26 changes: 22 additions & 4 deletions benches/bench_read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bencher::{benchmark_group, benchmark_main, Bencher};
use openslide_rs::{traits::Slide, Address, DeepZoomGenerator, OpenSlide, Region, Size};
use std::path::Path;
use std::{path::Path, sync::Arc};

fn openslide_read_region_256(bench: &mut Bencher) {
let slide = OpenSlide::new(Path::new("tests/assets/default.svs")).unwrap();
Expand Down Expand Up @@ -52,14 +52,30 @@ fn openslide_read_image_512(bench: &mut Bencher) {

fn deepzoom_read_image_256(bench: &mut Bencher) {
let slide = OpenSlide::new(Path::new("tests/assets/default.svs")).unwrap();
let dz = DeepZoomGenerator::new(&slide, 257, 0, false).unwrap();
let dz: DeepZoomGenerator<OpenSlide, _> =
DeepZoomGenerator::new(&slide, 257, 0, false).unwrap();

bench.iter(|| dz.get_tile_rgb(12, Address { x: 0, y: 0 }));
}

fn deepzoom_read_image_512(bench: &mut Bencher) {
let slide = OpenSlide::new(Path::new("tests/assets/default.svs")).unwrap();
let dz = DeepZoomGenerator::new(&slide, 511, 0, false).unwrap();
let dz: DeepZoomGenerator<OpenSlide, _> =
DeepZoomGenerator::new(&slide, 511, 0, false).unwrap();

bench.iter(|| dz.get_tile_rgb(12, Address { x: 0, y: 0 }));
}

fn deepzoom_read_image_256_arc(bench: &mut Bencher) {
let slide = Arc::new(OpenSlide::new(Path::new("tests/assets/default.svs")).unwrap());
let dz: DeepZoomGenerator<OpenSlide, _> = DeepZoomGenerator::new(slide, 257, 0, false).unwrap();

bench.iter(|| dz.get_tile_rgb(12, Address { x: 0, y: 0 }));
}

fn deepzoom_read_image_512_arc(bench: &mut Bencher) {
let slide = Arc::new(OpenSlide::new(Path::new("tests/assets/default.svs")).unwrap());
let dz: DeepZoomGenerator<OpenSlide, _> = DeepZoomGenerator::new(slide, 511, 0, false).unwrap();

bench.iter(|| dz.get_tile_rgb(12, Address { x: 0, y: 0 }));
}
Expand All @@ -77,6 +93,8 @@ benchmark_group!(
benchmark_group!(
deepzoom_image,
deepzoom_read_image_256,
deepzoom_read_image_512
deepzoom_read_image_512,
deepzoom_read_image_256_arc,
deepzoom_read_image_512_arc
);
benchmark_main!(benches_region, benches_image, deepzoom_image);
10 changes: 10 additions & 0 deletions src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ impl Deref for OpenSlideWrapper {
unsafe impl Send for OpenSlideWrapper {}
unsafe impl Sync for OpenSlideWrapper {}

pub fn get_version() -> Result<String> {
let version = unsafe { sys::openslide_get_version() };
if !version.is_null() {
let vendor = unsafe { ffi::CStr::from_ptr(version).to_string_lossy().into_owned() };
Ok(vendor)
} else {
Err(OpenSlideError::CoreError("Cannot get version".to_string()))
}
}

pub fn detect_vendor(filename: &str) -> Result<String> {
let c_filename = ffi::CString::new(filename)?;
unsafe {
Expand Down
5 changes: 5 additions & 0 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ impl Drop for OpenSlide {
}

impl OpenSlide {
/// Get the version of the OpenSlide library.
pub fn get_version() -> Result<String> {
bindings::get_version()
}

/// This method tries to open the slide at the given filename location.
///
/// This function can be expensive; avoid calling it unnecessarily. For example, a tile server
Expand Down
108 changes: 64 additions & 44 deletions tests/properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use fixture::{boxes_tiff, default, hamamatsu, leica, mirax, trestle};
use openslide_rs::{properties::VendorProperties, OpenSlide};
use rstest::rstest;
use std::path::Path;
use version_compare::Version;

mod fixture;

Expand All @@ -12,50 +13,61 @@ fn test_slide_properties(#[case] filename: &Path) {

println!("{slide:?}");

assert_eq!(
slide.get_property_names(),
vec![
"openslide.level-count",
"openslide.level[0].downsample",
"openslide.level[0].height",
"openslide.level[0].tile-height",
"openslide.level[0].tile-width",
"openslide.level[0].width",
"openslide.level[1].downsample",
"openslide.level[1].height",
"openslide.level[1].tile-height",
"openslide.level[1].tile-width",
"openslide.level[1].width",
"openslide.level[2].downsample",
"openslide.level[2].height",
"openslide.level[2].tile-height",
"openslide.level[2].tile-width",
"openslide.level[2].width",
"openslide.level[3].downsample",
"openslide.level[3].height",
"openslide.level[3].tile-height",
"openslide.level[3].tile-width",
"openslide.level[3].width",
let base_expected_result = vec![
"openslide.level-count",
"openslide.level[0].downsample",
"openslide.level[0].height",
"openslide.level[0].tile-height",
"openslide.level[0].tile-width",
"openslide.level[0].width",
"openslide.level[1].downsample",
"openslide.level[1].height",
"openslide.level[1].tile-height",
"openslide.level[1].tile-width",
"openslide.level[1].width",
"openslide.level[2].downsample",
"openslide.level[2].height",
"openslide.level[2].tile-height",
"openslide.level[2].tile-width",
"openslide.level[2].width",
"openslide.level[3].downsample",
"openslide.level[3].height",
"openslide.level[3].tile-height",
"openslide.level[3].tile-width",
"openslide.level[3].width",
"openslide.quickhash-1",
"openslide.vendor",
"tiff.ResolutionUnit",
"tiff.XResolution",
"tiff.YResolution",
];

let raw_version = OpenSlide::get_version().unwrap();
let version = Version::from(&raw_version).unwrap();

if version > Version::from("3.4.1").unwrap() {
let mut expected_result = base_expected_result;
expected_result.extend(vec![
"openslide.mpp-x",
"openslide.mpp-y",
"openslide.quickhash-1",
"openslide.vendor",
"tiff.ResolutionUnit",
"tiff.XResolution",
"tiff.YResolution"
]
);

assert_eq!(
slide.get_property_value("tiff.YResolution").unwrap(),
"28.340000157438311"
);
assert_eq!(
slide.get_property_value("tiff.XResolution").unwrap(),
"28.340000157438311"
);
assert_eq!(
slide.get_property_value("tiff.YResolution").unwrap(),
"28.340000157438311"
);
"tiff.YResolution",
]);
assert_eq!(slide.get_property_names(), expected_result);
} else {
let mut expected_result = base_expected_result;
expected_result.extend(vec![
"openslide.quickhash-1",
"openslide.vendor",
"tiff.ResolutionUnit",
"tiff.XResolution",
"tiff.YResolution",
]);
assert_eq!(slide.get_property_names(), expected_result);
}
}

#[rstest]
Expand All @@ -69,8 +81,16 @@ fn test_tiff_properties(#[case] filename: &Path) {

if let VendorProperties::GenericTiff = &properties.vendor_properties {
let prop = &properties.tiff_properties;
assert_eq!(prop.x_resolution, Some(28.34));
assert_eq!(prop.y_resolution, Some(28.34));
let raw_version = OpenSlide::get_version().unwrap();
let version = Version::from(&raw_version).unwrap();

if version > Version::from("3.4.1").unwrap() {
assert_eq!(prop.x_resolution, Some(28.34));
assert_eq!(prop.y_resolution, Some(28.34));
} else {
assert_eq!(prop.x_resolution, None);
assert_eq!(prop.y_resolution, None);
}
assert_eq!(prop.resolution_unit, Some("centimeter".to_string()));
} else {
panic!("Not Aperio")
Expand All @@ -84,8 +104,8 @@ fn test_tiff_properties(#[case] filename: &Path) {
properties.openslide_properties.quickhash_1,
Some("c08b056490bac8bcb329d9b8fb175888083d4097952a55fee99997758c728c36".to_string())
);
assert_eq!(properties.openslide_properties.mpp_x, None);
assert_eq!(properties.openslide_properties.mpp_y, None);
assert_eq!(properties.openslide_properties.mpp_x, Some(352.85815));
assert_eq!(properties.openslide_properties.mpp_y, Some(352.85815));
assert_eq!(properties.openslide_properties.level_count, Some(4));
assert_eq!(
properties.openslide_properties.levels[0].downsample,
Expand Down

1 comment on commit 6613a4a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openslide-rs Benchmark

Benchmark suite Current: 6613a4a Previous: 56bfcfd Ratio
deepzoom_read_image_256 1357352 ns/iter (± 84611) 1234536 ns/iter (± 25430) 1.10
deepzoom_read_image_256_arc 1355890 ns/iter (± 85721)
deepzoom_read_image_512 5425619 ns/iter (± 183806) 4944349 ns/iter (± 383619) 1.10
deepzoom_read_image_512_arc 5416678 ns/iter (± 106093)
openslide_read_image_256 1314720 ns/iter (± 32382) 1197713 ns/iter (± 21322) 1.10
openslide_read_image_512 5440500 ns/iter (± 91571) 4946176 ns/iter (± 81608) 1.10
openslide_read_region_256 1263672 ns/iter (± 29999) 1143209 ns/iter (± 35169) 1.11
openslide_read_region_512 5207967 ns/iter (± 135487) 4728226 ns/iter (± 126050) 1.10

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.