Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 1 addition & 35 deletions Common/GTesting/itkImageRandomSamplerGTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ GTEST_TEST(ImageRandomSampler, CheckImageValuesOfSamples)

elx::DefaultConstruct<SamplerType> sampler{};

sampler.SetSeed(1);

const size_t numberOfSamples{ 3 };
sampler.SetNumberOfSamples(numberOfSamples);
sampler.SetInput(image);
Expand All @@ -58,7 +56,7 @@ GTEST_TEST(ImageRandomSampler, CheckImageValuesOfSamples)
ASSERT_EQ(samples.size(), numberOfSamples);

// The image values that appeared during the development of the test.
const std::array<PixelType, numberOfSamples> expectedImageValues = { 16, 12, 15 };
const std::array<PixelType, numberOfSamples> expectedImageValues = { 13, 5, 7 };

for (size_t i{}; i < numberOfSamples; ++i)
{
Expand All @@ -67,37 +65,6 @@ GTEST_TEST(ImageRandomSampler, CheckImageValuesOfSamples)
}


GTEST_TEST(ImageRandomSampler, SetSeedMakesRandomizationDeterministic)
{
using PixelType = int;
using ImageType = itk::Image<PixelType>;
using SamplerType = itk::ImageRandomSampler<ImageType>;

const auto image =
CreateImageFilledWithSequenceOfNaturalNumbers<PixelType>(ImageType::SizeType::Filled(minimumImageSizeValue));

for (const SamplerType::SeedIntegerType seed : { 0, 1 })
{
const auto generateSamples = [seed, image] {
elx::DefaultConstruct<SamplerType> sampler{};
sampler.SetSeed(seed);
sampler.SetInput(image);
sampler.Update();
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
};

const auto samples = generateSamples();

// The test would be trivial (uninteresting) if there were no samples. Note that itk::ImageSamplerBase does
// zero-initialize m_NumberOfSamples, but itk::ImageRandomSamplerBase does m_NumberOfSamples = 1000 afterwards.
EXPECT_FALSE(samples.empty());

// Do the same test another time, to check that the result remains the same.
EXPECT_EQ(generateSamples(), samples);
}
}


GTEST_TEST(ImageRandomSampler, HasSameOutputWhenUsingMultiThread)
{
using PixelType = int;
Expand All @@ -110,7 +77,6 @@ GTEST_TEST(ImageRandomSampler, HasSameOutputWhenUsingMultiThread)
const auto generateSamples = [image](const bool useMultiThread) {
elx::DefaultConstruct<SamplerType> sampler{};
sampler.SetUseMultiThread(useMultiThread);
sampler.SetSeed(1);
sampler.SetInput(image);
sampler.Update();
return std::move(Deref(sampler.GetOutput()).CastToSTLContainer());
Expand Down
6 changes: 1 addition & 5 deletions Common/ImageSamplers/itkImageRandomSampler.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ ImageRandomSampler<TInputImage>::GenerateData()

/** Setup a random iterator over the input image. */
ImageRandomConstIteratorWithIndex<InputImageType> randIter(&inputImage, this->GetCroppedInputImageRegion());

if (const auto optionalSeed = Superclass::GetOptionalSeed())
{
randIter.ReinitializeSeed(*optionalSeed);
}
randIter.ReinitializeSeed(Superclass::GetNextSeed());
randIter.GoToBegin();

if (mask == nullptr)
Expand Down
36 changes: 7 additions & 29 deletions Common/ImageSamplers/itkImageRandomSamplerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "itkImageSamplerBase.h"
#include <itkMersenneTwisterRandomVariateGenerator.h>
#include "elxDefaultConstruct.h"
#include <optional>

namespace itk
{
Expand Down Expand Up @@ -69,32 +68,6 @@ class ITK_TEMPLATE_EXPORT ImageRandomSamplerBase : public ImageSamplerBase<TInpu

using SeedIntegerType = Statistics::MersenneTwisterRandomVariateGenerator::IntegerType;

/** Allows setting the randomization seed. Optional: when the argument does not have a seed value, an arbitrary seed
* may be used for randomization (which is the default behavior). */
void
SetOptionalSeed(const std::optional<SeedIntegerType> arg)
{
if (arg != m_OptionalSeed)
{
m_OptionalSeed = arg;
this->Modified();
}
}

/** Sets the randomization seed as specified. */
void
SetSeed(const SeedIntegerType arg)
{
SetOptionalSeed(std::make_optional(arg));
}

/** Retrieves the optional user specified randomization seed. */
[[nodiscard]] std::optional<SeedIntegerType>
GetOptionalSeed() const
{
return m_OptionalSeed;
}

void
SetRandomVariateGenerator(Statistics::MersenneTwisterRandomVariateGenerator & randomVariateGenerator)
{
Expand All @@ -116,6 +89,12 @@ class ITK_TEMPLATE_EXPORT ImageRandomSamplerBase : public ImageSamplerBase<TInpu
void
GenerateRandomNumberList();

SeedIntegerType
GetNextSeed()
{
return ++m_Seed;
}

Statistics::MersenneTwisterRandomVariateGenerator &
GetRandomVariateGenerator()
{
Expand All @@ -130,8 +109,7 @@ class ITK_TEMPLATE_EXPORT ImageRandomSamplerBase : public ImageSamplerBase<TInpu
std::vector<double> m_RandomNumberList{};

private:
std::optional<SeedIntegerType> m_OptionalSeed{};
SeedIntegerType m_Seed{ 121212 + 1 };
SeedIntegerType m_Seed{ 121212 + 1 };

elx::DefaultConstruct<Statistics::MersenneTwisterRandomVariateGenerator> m_DefaultRandomVariateGenerator{};
Statistics::MersenneTwisterRandomVariateGenerator * m_RandomVariateGenerator{ &m_DefaultRandomVariateGenerator };
Expand Down
2 changes: 1 addition & 1 deletion Common/ImageSamplers/itkImageRandomSamplerBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void
ImageRandomSamplerBase<TInputImage>::GenerateRandomNumberList()
{
elx::DefaultConstruct<Statistics::MersenneTwisterRandomVariateGenerator> randomVariateGenerator{};
randomVariateGenerator.SetSeed(m_OptionalSeed.value_or(++m_Seed));
randomVariateGenerator.SetSeed(GetNextSeed());

/** Clear the random number list. */
this->m_RandomNumberList.clear();
Expand Down
Loading