From 50b55d24a1f68fa592869957cf59e64ef52a56a9 Mon Sep 17 00:00:00 2001 From: Tom Birdsong Date: Fri, 11 Aug 2023 14:10:27 -0400 Subject: [PATCH] COMP: Reduce HTTP test coverage redundancy Removes metadata validation from HTTP tests. Both metadata validation and image buffer validation are now covered under baseline image comparison after the test body completes. --- test/itkOMEZarrNGFFHTTPTest.cxx | 150 +++++++++----------------------- 1 file changed, 40 insertions(+), 110 deletions(-) diff --git a/test/itkOMEZarrNGFFHTTPTest.cxx b/test/itkOMEZarrNGFFHTTPTest.cxx index 753cdf7..35aaac8 100644 --- a/test/itkOMEZarrNGFFHTTPTest.cxx +++ b/test/itkOMEZarrNGFFHTTPTest.cxx @@ -37,6 +37,14 @@ makeOutputName(const std::string & outputPrefix, size_t datasetIndex) return outputPrefix + "_" + std::to_string(datasetIndex) + ".mha"; } +template +bool +writeOutputImage(TImagePointer image, const std::string & outputPrefix, size_t datasetIndex) +{ + std::string outputFilename = makeOutputName(outputPrefix, datasetIndex); + itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); +} + bool test2DImage(const std::string & outputPrefix) { @@ -45,67 +53,33 @@ test2DImage(const std::string & outputPrefix) itk::OMEZarrNGFFImageIOFactory::RegisterOneFactory(); - // Baseline image metadata - auto baselineImage0 = ImageType::New(); - baselineImage0->SetRegions(itk::MakeSize(1024, 930)); - // unit origin, spacing, direction - - auto baselineImage1 = ImageType::New(); - baselineImage1->SetRegions(itk::MakeSize(512, 465)); - const float spacing1[2] = { 2.0, 2.0 }; - baselineImage1->SetSpacing(spacing1); - // unit origin, direction - - auto baselineImage2 = ImageType::New(); - baselineImage2->SetRegions(itk::MakeSize(256, 232)); - const float spacing2[2] = { 4.0, 4.0 }; - baselineImage2->SetSpacing(spacing2); - // unit origin, direction - - // Resolution 0 - auto image = itk::ReadImage(resourceURL); + size_t resolution = 0; + auto image = itk::ReadImage(resourceURL); image->Print(std::cout); - ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), baselineImage0->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion(), baselineImage0->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), baselineImage0->GetSpacing()); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), baselineImage0->GetOrigin()); - - std::string outputFilename = makeOutputName(outputPrefix, 0); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); + ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), image->GetBufferedRegion()); + writeOutputImage(image, outputPrefix, resolution); - // Resolution 1 + resolution = 1; auto imageIO = itk::OMEZarrNGFFImageIO::New(); - imageIO->SetDatasetIndex(1); + imageIO->SetDatasetIndex(resolution); auto reader1 = itk::ImageFileReader::New(); reader1->SetFileName(resourceURL); reader1->SetImageIO(imageIO); reader1->Update(); image = reader1->GetOutput(); image->Print(std::cout); - ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), baselineImage1->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion(), baselineImage1->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), baselineImage1->GetSpacing()); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), baselineImage1->GetOrigin()); - - outputFilename = makeOutputName(outputPrefix, 1); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); + writeOutputImage(image, outputPrefix, resolution); // Resolution 2 - imageIO->SetDatasetIndex(2); + imageIO->SetDatasetIndex(resolution); auto reader2 = itk::ImageFileReader::New(); reader2->SetFileName(resourceURL); reader2->SetImageIO(imageIO); reader2->Update(); image = reader2->GetOutput(); image->Print(std::cout); - ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), baselineImage2->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion(), baselineImage2->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), baselineImage2->GetSpacing()); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), baselineImage2->GetOrigin()); - - outputFilename = makeOutputName(outputPrefix, 2); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); + writeOutputImage(image, outputPrefix, resolution); return EXIT_SUCCESS; } @@ -116,68 +90,33 @@ test3DImage(const std::string & outputPrefix) using ImageType = itk::Image; const std::string resourceURL = "https://s3.embl.de/i2k-2020/ngff-example-data/v0.4/zyx.ome.zarr"; - // Baseline image metadata - auto baselineImage0 = ImageType::New(); - baselineImage0->SetRegions(itk::MakeSize(483, 393, 603)); - const float spacing0[3] = { 64, 64, 64 }; - baselineImage0->SetSpacing(spacing0); - - auto baselineImage1 = ImageType::New(); - baselineImage1->SetRegions(itk::MakeSize(242, 196, 302)); - const float spacing1[3] = { 128, 128, 128 }; - baselineImage1->SetSpacing(spacing1); - - auto baselineImage2 = ImageType::New(); - baselineImage2->SetRegions(itk::MakeSize(121, 98, 151)); - const float spacing2[3] = { 256, 256, 256 }; - baselineImage2->SetSpacing(spacing2); - itk::OMEZarrNGFFImageIOFactory::RegisterOneFactory(); - // Resolution 0 - auto image = itk::ReadImage(resourceURL); + size_t resolution = 0; + auto image = itk::ReadImage(resourceURL); image->Print(std::cout); + writeOutputImage(image, outputPrefix, resolution); - ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), baselineImage0->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion(), baselineImage0->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), baselineImage0->GetSpacing()); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), baselineImage0->GetOrigin()); - - std::string outputFilename = makeOutputName(outputPrefix, 0); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); - - // Resolution 1 + resolution = 1; auto imageIO = itk::OMEZarrNGFFImageIO::New(); - imageIO->SetDatasetIndex(1); + imageIO->SetDatasetIndex(resolution); auto reader1 = itk::ImageFileReader::New(); reader1->SetFileName(resourceURL); reader1->SetImageIO(imageIO); reader1->Update(); image = reader1->GetOutput(); image->Print(std::cout); - ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), baselineImage1->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion(), baselineImage1->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), baselineImage1->GetSpacing()); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), baselineImage1->GetOrigin()); + writeOutputImage(image, outputPrefix, resolution); - outputFilename = makeOutputName(outputPrefix, 1); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); - - // Resolution 2 - imageIO->SetDatasetIndex(2); + resolution = 2; + imageIO->SetDatasetIndex(resolution); auto reader2 = itk::ImageFileReader::New(); reader2->SetFileName(resourceURL); reader2->SetImageIO(imageIO); reader2->Update(); image = reader2->GetOutput(); image->Print(std::cout); - ITK_TEST_EXPECT_EQUAL(image->GetLargestPossibleRegion(), baselineImage2->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion(), baselineImage2->GetLargestPossibleRegion()); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), baselineImage2->GetSpacing()); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), baselineImage2->GetOrigin()); - - outputFilename = makeOutputName(outputPrefix, 2); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); + writeOutputImage(image, outputPrefix, resolution); return EXIT_SUCCESS; } @@ -187,11 +126,13 @@ testTimeSlice(const std::string & outputPrefix) { // Read a subregion of an arbitrary time point from a 3D image buffer into a 2D image using ImageType = itk::Image; - const std::string resourceURL = "https://s3.embl.de/i2k-2020/ngff-example-data/v0.4/tyx.ome.zarr"; + const std::string resourceURL = "https://s3.embl.de/i2k-2020/ngff-example-data/v0.4/tyx.ome.zarr"; + static constexpr size_t RESOLUTION = 0; + static constexpr size_t TIME_INDEX = 2; auto imageIO = itk::OMEZarrNGFFImageIO::New(); - imageIO->SetDatasetIndex(0); - imageIO->SetTimeIndex(2); + imageIO->SetDatasetIndex(RESOLUTION); + imageIO->SetTimeIndex(TIME_INDEX); auto requestedRegion = itk::ImageRegion<2>(); requestedRegion.SetSize(itk::MakeSize(50, 50)); @@ -212,13 +153,7 @@ testTimeSlice(const std::string & outputPrefix) ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion().GetSize(), requestedRegion.GetSize()); ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion().GetIndex(), requestedRegion.GetIndex()); - typename ImageType::SpacingType expectedSpacing; - expectedSpacing.Fill(0.65); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), expectedSpacing); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), itk::MakePoint(0.0, 0.0)); - - std::string outputFilename = makeOutputName(outputPrefix, 0); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); + writeOutputImage(image, outputPrefix, RESOLUTION); return EXIT_SUCCESS; } @@ -228,12 +163,15 @@ testTimeAndChannelSlice(const std::string & outputPrefix) { // Read a subregion of an arbitrary channel and time point from a 5D image buffer into a 3D image using ImageType = itk::Image; - const std::string resourceURL = "https://s3.embl.de/i2k-2020/ngff-example-data/v0.4/tczyx.ome.zarr"; + const std::string resourceURL = "https://s3.embl.de/i2k-2020/ngff-example-data/v0.4/tczyx.ome.zarr"; + static constexpr size_t RESOLUTION = 2; + static constexpr size_t TIME_INDEX = 0; + static constexpr size_t CHANNEL_INDEX = 0; auto imageIO = itk::OMEZarrNGFFImageIO::New(); - imageIO->SetDatasetIndex(2); - imageIO->SetTimeIndex(0); - imageIO->SetChannelIndex(0); + imageIO->SetDatasetIndex(RESOLUTION); + imageIO->SetTimeIndex(TIME_INDEX); + imageIO->SetChannelIndex(CHANNEL_INDEX); auto requestedRegion = itk::ImageRegion<3>(); requestedRegion.SetSize(itk::MakeSize(10, 20, 30)); @@ -254,15 +192,7 @@ testTimeAndChannelSlice(const std::string & outputPrefix) ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion().GetSize(), requestedRegion.GetSize()); ITK_TEST_EXPECT_EQUAL(image->GetBufferedRegion().GetIndex(), requestedRegion.GetIndex()); - typename ImageType::SpacingType expectedSpacing; - expectedSpacing.SetElement(0, 2.6); - expectedSpacing.SetElement(1, 2.6); - expectedSpacing.SetElement(2, 4.0); - ITK_TEST_EXPECT_EQUAL(image->GetSpacing(), expectedSpacing); - ITK_TEST_EXPECT_EQUAL(image->GetOrigin(), itk::MakePoint(0.0, 0.0, 0.0)); - - std::string outputFilename = makeOutputName(outputPrefix, 2); - itk::WriteImage(image, outputFilename, USE_MHA_COMPRESSION); + writeOutputImage(image, outputPrefix, RESOLUTION); return EXIT_SUCCESS; }