Ejemplo n.º 1
0
    def test_from_raw_default(self):
        """ Test that DiffractionDataset.from_raw() works with default settigns """
        raw = TestRawDataset()

        with DiffractionDataset.from_raw(raw, filename=self.fname,
                                         mode="w") as dataset:
            self.assertSequenceEqual(
                dataset.diffraction_group["intensity"].shape,
                raw.resolution + (len(raw.time_points), ),
            )
Ejemplo n.º 2
0
 def setUp(self):
     self.patterns = list(repeat(random(size=(256, 256)), 5))
     self.metadata = {"fluence": 10, "energy": 90}
     self.dataset = DiffractionDataset.from_collection(
         self.patterns,
         filename=os.path.join(gettempdir(), "test.hdf5"),
         time_points=range(5),
         metadata=self.metadata,
         mode="w",
     )
Ejemplo n.º 3
0
    def test_from_raw_alignment(self):
        """ Test that DiffractionDataset.from_raw(..., align = True) does not throw any errors """
        raw = TestRawDataset()

        with DiffractionDataset.from_raw(raw,
                                         filename=self.fname,
                                         align=True,
                                         mode="w") as dataset:
            self.assertSequenceEqual(
                dataset.diffraction_group["intensity"].shape,
                raw.resolution + (len(raw.time_points), ),
            )
Ejemplo n.º 4
0
 def setUp(self):
     self.patterns = list(repeat(random(size=(128, 128)), 5))
     self.metadata = {"fluence": 10, "energy": 90}
     diff_dataset = DiffractionDataset.from_collection(
         self.patterns,
         filename="test.hdf5",
         time_points=range(5),
         metadata=self.metadata,
         mode="w",
     )
     self.dataset = PowderDiffractionDataset.from_dataset(diff_dataset,
                                                          center=(23, 45))
Ejemplo n.º 5
0
    def test_file_modes(self):
        """ Successively open and close the same dataset with different file modes. """
        fname = self.dataset.filename
        metadata = self.dataset.metadata
        self.dataset.close()

        with self.subTest("Read-only mode"):
            with DiffractionDataset(fname, mode="r") as dset:
                self.assertEqual(metadata, dset.metadata)
                self.assertEqual(dset.mode, "r")

        with self.subTest("Read/write modes"):
            for mode in ("r+", "a"):
                with DiffractionDataset(fname, mode=mode) as dset:
                    self.assertEqual(metadata, dset.metadata)
                    self.assertEqual(dset.mode, "r+")

            with self.assertRaises(OSError):
                DiffractionDataset(fname, mode="x")

        # Reopen dataset so it can be deleted
        self.dataset = DiffractionDataset(fname, mode="r")
Ejemplo n.º 6
0
    def test_from_collection(self):
        """ Test the creation of a DiffractionDataset from a collection of patterns """
        patterns = repeat(random(size=(256, 256)), 10)
        metadata = {"fluence": 10, "energy": 90}

        with DiffractionDataset.from_collection(
                patterns,
                filename=self.fname,
                time_points=list(range(10)),
                metadata=metadata,
                dtype=np.float16,
                mode="w",
        ) as dataset:

            self.assertSequenceEqual(
                dataset.diffraction_group["intensity"].shape, (256, 256, 10))
            self.assertEqual(dataset.diffraction_group["intensity"].dtype,
                             np.float16)
            self.assertEqual(dataset.fluence, metadata["fluence"])
            self.assertEqual(dataset.energy, metadata["energy"])
            self.assertSequenceEqual(tuple(dataset.time_points),
                                     list(range(10)))
Ejemplo n.º 7
0
class TestDiffractionDataset(unittest.TestCase):
    def setUp(self):
        self.patterns = list(repeat(random(size=(256, 256)), 5))
        self.metadata = {"fluence": 10, "energy": 90}
        self.dataset = DiffractionDataset.from_collection(
            self.patterns,
            filename=os.path.join(gettempdir(), "test.hdf5"),
            time_points=range(5),
            metadata=self.metadata,
            mode="w",
        )

    def test_file_modes(self):
        """ Successively open and close the same dataset with different file modes. """
        fname = self.dataset.filename
        metadata = self.dataset.metadata
        self.dataset.close()

        with self.subTest("Read-only mode"):
            with DiffractionDataset(fname, mode="r") as dset:
                self.assertEqual(metadata, dset.metadata)
                self.assertEqual(dset.mode, "r")

        with self.subTest("Read/write modes"):
            for mode in ("r+", "a"):
                with DiffractionDataset(fname, mode=mode) as dset:
                    self.assertEqual(metadata, dset.metadata)
                    self.assertEqual(dset.mode, "r+")

            with self.assertRaises(OSError):
                DiffractionDataset(fname, mode="x")

        # Reopen dataset so it can be deleted
        self.dataset = DiffractionDataset(fname, mode="r")

    def test_dataset_metadata(self):
        """ Test that the property 'metadata' is working correctly"""
        metadata = self.dataset.metadata
        for required in DiffractionDataset.valid_metadata:
            self.assertIn(required, metadata)
        self.assertIn("filename", metadata)

    def test_notes(self):
        """ Test that updating the notes works as intended """
        self.dataset.notes = "test notes"
        self.assertEqual(self.dataset.notes, "test notes")
        self.dataset.notes = "different notes"
        self.assertEqual(self.dataset.notes, "different notes")

    def test_diff_apply(self):
        """ Test that the diff_apply method works as expected """
        with self.subTest("Applying an operation"):
            before = np.array(self.dataset.diffraction_group["intensity"])
            self.dataset.diff_apply(lambda arr: arr * 2)
            after = np.array(self.dataset.diffraction_group["intensity"])
            self.assertTrue(np.allclose(2 * before, after))

        with self.subTest("Checking for callable"):
            with self.assertRaises(TypeError):
                self.dataset.diff_apply(None)

    @unittest.skipIf(not SWMR_AVAILABLE,
                     reason="Parallel execution is not available")
    def test_diff_apply_parallel(self):
        """ Test that the diff_apply method works as expected in parallel mode """
        with self.subTest("Applying an operation"):
            before = np.array(self.dataset.diffraction_group["intensity"])
            self.dataset.diff_apply(double, processes=2)
            after = np.array(self.dataset.diffraction_group["intensity"])
            self.assertTrue(np.allclose(2 * before, after))

        with self.subTest("Checking for callable"):
            with self.assertRaises(TypeError):
                self.dataset.diff_apply(None)

    def test_resolution(self):
        """ Test that dataset resolution is correct """
        self.assertSequenceEqual(self.patterns[0].shape,
                                 self.dataset.resolution)

    def test_symmetrization(self):
        """ Test correctness of symmetrization operation """
        before = np.array(self.dataset.diffraction_group["intensity"])
        symmetrized = np.array(before, copy=True)
        for index, _ in enumerate(self.dataset.time_points):
            symmetrized[:, :, index] = nfold(before[:, :, index],
                                             mod=3,
                                             center=(63, 65))

        self.dataset.symmetrize(mod=3, center=(63, 65))
        after = np.array(self.dataset.diffraction_group["intensity"])

        self.assertTrue(np.allclose(symmetrized, after))
        self.assertEqual(
            self.dataset.center,
            (63, 65),
            "Diffraction center was not properly set after symmetrization",
        )

    @unittest.skipIf(not SWMR_AVAILABLE,
                     reason="Parallel execution is not available")
    def test_symmetrization_parallel(self):
        """ Test correctness of symmetrization operation in parallel mode """
        before = np.array(self.dataset.diffraction_group["intensity"])
        symmetrized = np.array(before, copy=True)
        for index, _ in enumerate(self.dataset.time_points):
            symmetrized[:, :, index] = nfold(before[:, :, index],
                                             mod=3,
                                             center=(63, 65))

        self.dataset.symmetrize(mod=3, center=(63, 65), processes=2)
        after = np.array(self.dataset.diffraction_group["intensity"])

        self.assertTrue(np.allclose(symmetrized, after))

    def test_symmetrization_shape(self):
        """ Test that dataset symmetrization raises no errors """
        with self.subTest("No smoothing"):
            self.dataset.symmetrize(mod=2, center=(128, 128))
            self.assertSequenceEqual(self.patterns[0].shape,
                                     self.dataset.resolution)

        with self.subTest("with smoothing"):
            self.dataset.symmetrize(mod=2, center=(128, 128), kernel_size=5)
            self.assertSequenceEqual(self.patterns[0].shape,
                                     self.dataset.resolution)

    def test_data(self):
        """ Test that data stored in DiffractionDataset is correct """
        for time, pattern in zip(list(self.dataset.time_points),
                                 self.patterns):
            self.assertTrue(np.allclose(self.dataset.diff_data(time), pattern))

    def test_time_zero_shift(self):
        """ Test that data changed with time_zero_shift() """
        unshifted = np.array(self.dataset.time_points)
        self.dataset.shift_time_zero(100)
        shifted = np.array(self.dataset.time_points)

        self.assertFalse(np.allclose(unshifted, shifted))
        self.assertTrue(np.allclose(unshifted + 100, shifted))

    def test_diff_eq(self):
        """ test that DiffractionDataset.diff_eq() returns the correct array """
        with self.subTest("No data before time-zero"):
            self.dataset.shift_time_zero(10)
            self.assertTrue(
                np.allclose(self.dataset.diff_eq(),
                            np.zeros(self.dataset.resolution)))

        with self.subTest("All data before time-zero"):
            self.dataset.shift_time_zero(-20)
            eq = np.mean(np.stack(self.patterns, axis=-1), axis=2)
            self.assertTrue(np.allclose(self.dataset.diff_eq(), eq))

    def test_time_series(self):
        """ Test that the DiffractionDataset.time_series method is working """

        r1, r2, c1, c2 = 100, 120, 45, 57
        stack = np.stack(self.patterns, axis=-1)
        ts = np.mean(stack[r1:r2, c1:c2], axis=(0, 1))

        self.assertTrue(
            np.allclose(self.dataset.time_series([r1, r2, c1, c2]), ts))

    def tearDown(self):
        fname = self.dataset.filename
        self.dataset.close()
        del self.dataset
        with suppress(OSError):
            os.remove(fname)