diff --git a/darfix/core/test/test_image_registration.py b/darfix/core/test/test_image_registration.py index 97b9ab4422fa71a4b270cfdb6caa26a5df2006de..9cd12258393f6db46681fc09901234c1d6803a8a 100644 --- a/darfix/core/test/test_image_registration.py +++ b/darfix/core/test/test_image_registration.py @@ -50,7 +50,7 @@ class TestImageRegistration(unittest.TestCase): @classmethod def setUpClass(cls): - + cls.rstate = numpy.random.RandomState(seed=1000) cls.data = numpy.array([[[1, 2, 3, 4, 5], [2, 2, 3, 4, 5], [3, 2, 3, 4, 5], @@ -98,7 +98,7 @@ class TestImageRegistration(unittest.TestCase): """ Tests the shift detection with tolerance of 3 decimals""" first_frame = numpy.zeros((100, 100)) # Simulating a series of frame with information in the middle. - first_frame[25:75, 25:75] = numpy.random.randint(50, 300, size=(50, 50)) + first_frame[25:75, 25:75] = self.rstate.randint(50, 300, size=(50, 50)) data = [first_frame] shift = [1.0, 0] for i in range(9): @@ -116,7 +116,7 @@ class TestImageRegistration(unittest.TestCase): """ Tests the shift detection with tolerance of 5 decimals""" # Create a frame and repeat it shifting it every time first_frame = numpy.zeros((100, 100)) - first_frame[25:75, 25:75] = numpy.random.randint(50, 300, size=(50, 50)) + first_frame[25:75, 25:75] = self.rstate.randint(50, 300, size=(50, 50)) data = [first_frame] shift = [0, 1] for i in range(9): @@ -134,7 +134,7 @@ class TestImageRegistration(unittest.TestCase): """ Tests the shift detection with tolerance of 2 decimals""" # Create a frame and repeat it shifting it every time first_frame = numpy.zeros((100, 100)) - first_frame[25:75, 25:75] = numpy.random.randint(50, 300, size=(50, 50)) + first_frame[25:75, 25:75] = self.rstate.randint(50, 300, size=(50, 50)) data = [first_frame] shift = [1, 1] for i in range(9): @@ -153,7 +153,7 @@ class TestImageRegistration(unittest.TestCase): """ Tests the shift detection using shifted float with tolerance of 2 decimals""" first_frame = numpy.zeros((100, 100)) # Simulating a series of frame with information in the middle. - first_frame[25:75, 25:75] = numpy.random.randint(50, 300, size=(50, 50)) + first_frame[25:75, 25:75] = self.rstate.randint(50, 300, size=(50, 50)) data = [first_frame] shift = [0.5, 0.2] for i in range(9): @@ -271,9 +271,10 @@ class TestReshapedShift(unittest.TestCase): # Create headers header = [] # Dimensions for reshaping - self.first_dim = numpy.random.rand(5) - self.second_dim = numpy.random.rand(2) - motors = numpy.random.rand(7) + self.rstate = numpy.random.RandomState(seed=1000) + self.first_dim = self.rstate.rand(5) + self.second_dim = self.rstate.rand(2) + motors = self.rstate.rand(7) for i in numpy.arange(10): header.append({}) header[i]["HeaderID"] = i @@ -282,7 +283,7 @@ class TestReshapedShift(unittest.TestCase): header[i]["counter_pos"] = "" header[i]["motor_pos"] = "" for c in counter_mne: - header[i]["counter_pos"] += str(numpy.random.rand(1)[0]) + " " + header[i]["counter_pos"] += str(self.rstate.rand(1)[0]) + " " for j, m in enumerate(motor_mne.split()): if m == "z": header[i]["motor_pos"] += str(self.first_dim[i % 5]) + " " @@ -293,7 +294,7 @@ class TestReshapedShift(unittest.TestCase): self.header = header self.first_frame = numpy.zeros((100, 100)) - self.first_frame[30:40, 30:40] = numpy.random.randint(50, 100, size=(10, 10)) + self.first_frame[30:40, 30:40] = self.rstate.randint(50, 100, size=(10, 10)) def test_shift_detection0(self): """ Tests the shift detection using only an axis (dimension). diff --git a/darfix/decomposition/test/test_ipca.py b/darfix/decomposition/test/test_ipca.py index acfe1a8c779dd07d078f4a7c8e3f9f016f996709..321e3fe16f5f0c6543fee5da3b41614697c2c288 100644 --- a/darfix/decomposition/test/test_ipca.py +++ b/darfix/decomposition/test/test_ipca.py @@ -28,8 +28,6 @@ __authors__ = ["J. Garriga"] __license__ = "MIT" __date__ = "22/04/2020" -import random - import numpy import unittest @@ -41,8 +39,12 @@ from darfix.decomposition.ipca import IPCA class TestIPCA(unittest.TestCase): """Tests for `ipca.py`.""" + @classmethod + def setUpClass(cls): + cls.rstate = numpy.random.RandomState(seed=1000) + def test_singular_values(self): - data = numpy.random.random((100, 1000)) + data = self.rstate.random((100, 1000)) ipca = IPCA(data, 50, num_components=3) self.assertEqual(ipca.singular_values, None) ipca.fit_transform() @@ -57,14 +59,17 @@ class TestIPCA(unittest.TestCase): r = 5 # Build principal components - V = numpy.eye(n)[:, random.sample(range(n), r)] # Subset of the standard basis vectors + all_dims = numpy.arange(n) + self.rstate.shuffle(all_dims) + sub_dims = all_dims[:r] + V = numpy.eye(n)[:, sub_dims] # Subset of the standard basis vectors R = special_ortho_group.rvs(n) # Random rotation in R^n V = numpy.dot(R, V) # Principal components - mean = numpy.random.rand(n) # Affine space translation + mean = self.rstate.rand(n) # Affine space translation # Build observations D = numpy.diag(range(r, 0, -1))**2 # Matrix of decreasing eigenvalues - Z = numpy.random.normal(size=(k, r)) # Dimensionality-reduced samples + Z = self.rstate.normal(size=(k, r)) # Dimensionality-reduced samples Z = numpy.dot(Z, D) X = numpy.dot(Z, V.T) + mean # Observations @@ -88,14 +93,17 @@ class TestIPCA(unittest.TestCase): r = 5 # Build principal components - V = numpy.eye(n)[:, random.sample(range(n), r)] # Subset of the standard basis vectors + all_dims = numpy.arange(n) + self.rstate.shuffle(all_dims) + sub_dims = all_dims[:r] + V = numpy.eye(n)[:, sub_dims] # Subset of the standard basis vectors R = special_ortho_group.rvs(n) # Random rotation in R^n V = numpy.dot(R, V) # Principal components - mean = numpy.random.rand(n) # Affine space translation + mean = self.rstate.rand(n) # Affine space translation # Build observations D = numpy.diag(range(r, 0, -1))**2 # Matrix of decreasing eigenvalues - Z = numpy.random.normal(size=(k, r)) # Dimensionality-reduced samples + Z = self.rstate.normal(size=(k, r)) # Dimensionality-reduced samples Z = numpy.dot(Z, D) X = numpy.dot(Z, V.T) + mean # Observations diff --git a/darfix/decomposition/test/test_nmf.py b/darfix/decomposition/test/test_nmf.py index 917a1490d9804273965b25ab14a87e81f1e25a5c..0de2e100a21557e5b2eea8faf3f4481d8d577a9b 100644 --- a/darfix/decomposition/test/test_nmf.py +++ b/darfix/decomposition/test/test_nmf.py @@ -39,7 +39,8 @@ class TestNMF(unittest.TestCase): """Tests for `nmf.py`.""" def setUp(self): - self.images = numpy.random.random((100, 1000)) + self.rstate = numpy.random.RandomState(seed=1000) + self.images = self.rstate.random((100, 1000)) def test_fit_transform(self): resources = ["circle", "star", "pentagon", "square"] @@ -49,8 +50,11 @@ class TestNMF(unittest.TestCase): sample = sampler(resources, means) X = numpy.array([sample(i).flatten() for i in range(num_images)]) + nmf = NMF(X, 4) - nmf.fit_transform(max_iter=500) + H = self.rstate.random((nmf.num_components, nmf.num_features)) + 10**-4 + W = self.rstate.random((nmf.num_samples, nmf.num_components)) + 10**-4 + nmf.fit_transform(max_iter=500, H=H, W=W) stack = numpy.asarray(images(resources)) for img in stack: