Beispiel #1
0
    def testBackengine(self):
        """ Test that we can start a test calculation. """

        self.__files_to_remove.append('orient_out.h5')
        self.__files_to_remove.append('recon_out.h5')

        emc_parameters = {
            'initial_number_of_quaternions': 1,
            'max_number_of_quaternions': 2,
            'max_number_of_iterations': 10,
            'min_error': 1.0e-6,
            'beamstop': True,
            'detailed_output': False
        }

        dm_parameters = {
            'number_of_trials': 5,
            'number_of_iterations': 2,
            'averaging_start': 15,
            'leash': 0.2,
            'number_of_shrink_cycles': 2,
        }

        # Construct the object.
        analyzer = S2EReconstruction(parameters={
            'EMC_Parameters': emc_parameters,
            'DM_Parameters': dm_parameters
        },
                                     input_path=self.input_h5,
                                     output_path='recon_out.h5')

        # Call backengine.
        status = analyzer.backengine()

        self.assertEqual(status, 0)
Beispiel #2
0
    def testCheckInterfaceConsistency(self):
        """ Test if the check for interface consistency works correctly. """

        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        pmi_parameters = {
            'sample_path': TestUtilities.generateTestFilePath('sample.h5')
        }
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=pmi_input,
            output_path='pmi_out.h5',
            sample_path=self.__sample_path)

        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 1,
            'number_of_diffraction_patterns': 2,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
            'number_of_MPI_processes': 2,
        }
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr_out.h5',
                                              output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        interfaces_are_consistent = pxs._checkInterfaceConsistency()

        self.assertTrue(interfaces_are_consistent)
Beispiel #3
0
    def testShapedConstruction(self):
        """ Testing the default construction of the class. """

        self.__files_to_remove.append('orient_out.h5')
        self.__files_to_remove.append('recon_out.h5')
        # Construct the object.
        analyzer = S2EReconstruction(parameters=None, input_path=self.input_h5, output_path='recon_out.h5')

        self.assertIsInstance(analyzer, S2EReconstruction)
    def testCalculatorQueries(self):
        """ Test that the calculator queries return the correct calculators. """
        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        photon_interactor = FakePhotonMatterInteractor(
            parameters=None, input_path=pmi_input, output_path='pmi_out.h5')

        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 1,
            'number_of_diffraction_patterns': 2,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
        }
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = PerfectPhotonDetector(parameters=None,
                                                input_path='diffr_out.h5',
                                                output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check queries.
        self.assertIs(pxs.photon_source, photon_source)
        self.assertIs(pxs.photon_propagator, photon_propagator)
        self.assertIs(pxs.photon_interactor, photon_interactor)
        self.assertIs(pxs.photon_diffractor, photon_diffractor)
        self.assertIs(pxs.photon_detector, photon_detector)
        self.assertIs(pxs.photon_analyzer, photon_analyzer)
Beispiel #5
0
    def testConstruction(self):
        """ Test the default constructor of this class. """
        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=pmi_input,
            output_path='pmi_out.h5',
            sample_path=self.__sample_path)

        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 1,
            'number_of_diffraction_patterns': 2,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
            'number_of_MPI_processes': 2,
        }
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr_out.h5',
                                              output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check instance.
        self.assertIsInstance(pxs, PhotonExperimentSimulation)
Beispiel #6
0
    def testDefaultConstruction(self):
        """ Testing the default construction of the class. """

        # Ensure proper cleanup.
        self.__dirs_to_remove.append('analysis')

        # Construct the object.
        analyzer = S2EReconstruction()
        self.assertIsInstance(analyzer, S2EReconstruction)

        # Check.
        self.assertEqual(analyzer.input_path, os.path.abspath('detector'))
        self.assertEqual(analyzer.output_path, os.path.abspath('analysis'))
    def testCheckInterfaceConsistency(self):
        """ Test if the check for interface consistency works correctly. """

        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        pmi_parameters = {
            'sample_path': TestUtilities.generateTestFilePath('sample.h5')
        }
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=pmi_input,
            output_path='pmi_out.h5',
            sample_path=self.__sample_path)

        diffraction_parameters = self.diffractorParam_1
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr_out.h5',
                                              output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        interfaces_are_consistent = pxs._checkInterfaceConsistency()

        self.assertTrue(interfaces_are_consistent)
    def testCalculatorQueries(self):
        """ Test that the calculator queries return the correct calculators. """
        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=pmi_input,
            output_path='pmi_out.h5',
            sample_path=self.__sample_path)

        diffraction_parameters = self.diffractorParam_2
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr_out.h5',
                                              output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check queries.
        self.assertIs(pxs.photon_source, photon_source)
        self.assertIs(pxs.photon_propagator, photon_propagator)
        self.assertIs(pxs.photon_interactor, photon_interactor)
        self.assertIs(pxs.photon_diffractor, photon_diffractor)
        self.assertIs(pxs.photon_detector, photon_detector)
        self.assertIs(pxs.photon_analyzer, photon_analyzer)
Beispiel #9
0
    def testBackengineDefaultPath(self):
        """ Test that we can start a test calculation. """

        # Prepare path.
        shutil.copytree(TestUtilities.generateTestFilePath('diffr'),
                        'detector')

        # Ensure proper cleanup.
        self.__dirs_to_remove.append('detector')
        self.__dirs_to_remove.append('analysis')

        emc_parameters = {
            'initial_number_of_quaternions': 1,
            'max_number_of_quaternions': 2,
            'max_number_of_iterations': 10,
            'min_error': 1.0e-6,
            'beamstop': True,
            'detailed_output': False
        }

        dm_parameters = {
            'number_of_trials': 5,
            'number_of_iterations': 2,
            'averaging_start': 15,
            'leash': 0.2,
            'number_of_shrink_cycles': 2,
        }

        # Construct the object.
        analyzer = S2EReconstruction(parameters={
            'EMC_Parameters': emc_parameters,
            'DM_Parameters': dm_parameters
        })

        # Call backengine.
        status = analyzer.backengine()

        # Check return value.
        self.assertEqual(status, 0)

        # Check presence of output files.
        self.assertTrue(os.path.isdir('analysis'))
        self.assertIn('orient_out.h5', os.listdir('analysis'))
        self.assertIn('phase_out.h5', os.listdir('analysis'))
    def testConstruction(self):
        """ Test the default constructor of this class. """
        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=pmi_input,
            output_path='pmi_out.h5',
            sample_path=self.__sample_path)

        diffraction_parameters = self.diffractorParam_1
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr_out.h5',
                                              output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check instance.
        self.assertIsInstance(pxs, PhotonExperimentSimulation)
    def testConstructionExceptions(self):
        """ Test that the appropriate exceptions are thrown if the object is constructed incorrectly. """
        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=pmi_input,
            output_path='pmi_out.h5',
            sample_path=self.__sample_path)

        diffraction_parameters = self.diffractorParam_1
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr_out.h5',
                                              output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        # Check wrong source.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=None,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_propagator,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong propagator.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=None,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_source,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong interactor.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=None,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_source,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong diffractor.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=None,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_source,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong analyzer.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=None,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_diffractor,
        )
Beispiel #12
0
    'min_error': 1.0e-8,
    'beamstop': 1.0e-5,
    'detailed_output': False
}

dm_parameters = {
    'number_of_trials': 5,
    'number_of_iterations': 2,
    'averaging_start': 15,
    'leash': 0.2,
    'number_of_shrink_cycles': 2,
}

reconstructor = S2EReconstruction(parameters={
    'EMC_Parameters': emc_parameters,
    'DM_Parameters': dm_parameters
},
                                  input_path='detector',
                                  output_path='recon.h5')

# Setup the photon experiment.
pxs = PhotonExperimentSimulation(
    photon_source=photon_source,
    photon_propagator=photon_propagator,
    photon_interactor=photon_interactor,
    photon_diffractor=photon_diffractor,
    photon_detector=photon_detector,
    photon_analyzer=reconstructor,
)

# Run the experiment.
pxs.run()
    def testHistory(self):
        """ Testing that all generated output files contain the history """

        # Setup directories.
        working_directory = 'SPI'
        self.__dirs_to_remove.append(working_directory)

        source_dir = os.path.join(working_directory, 'FELsource')
        prop_dir = os.path.join(working_directory, 'prop')
        pmi_dir = os.path.join(working_directory, 'pmi')
        diffr_dir = os.path.join(working_directory, 'diffr')
        detector_dir = os.path.join(working_directory, 'detector')
        recon_dir = os.path.join(working_directory, 'recon')

        # Make directories.
        os.mkdir(working_directory)
        os.mkdir(source_dir)
        os.mkdir(prop_dir)
        os.mkdir(pmi_dir)
        os.mkdir(diffr_dir)
        os.mkdir(detector_dir)
        os.mkdir(recon_dir)

        # Location of the FEL source file.
        source_input = TestUtilities.generateTestFilePath('FELsource_out')

        # Photon source.
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path=source_dir)

        # Photon propagator, default parameters.
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path=source_dir,
                                                 output_path=prop_dir)

        # Photon interactor with default parameters.
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=prop_dir,
            output_path=pmi_dir,
            sample_path=self.__sample_path)

        #  Diffraction with parameters.
        diffraction_parameters = self.diffractorParam_2

        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=TestUtilities.generateTestFilePath('pmi_out'),
            output_path=diffr_dir)

        # Reconstruction: EMC+DM
        emc_parameters = {
            'initial_number_of_quaternions': 1,
            'max_number_of_quaternions': 9,
            'max_number_of_iterations': 3,
            'min_error': 1.0e-8,
            'beamstop': True,
            'detailed_output': False
        }

        dm_parameters = {
            'number_of_trials': 5,
            'number_of_iterations': 2,
            'averaging_start': 15,
            'leash': 0.2,
            'number_of_shrink_cycles': 2,
        }

        reconstructor = S2EReconstruction(parameters={
            'EMC_Parameters': emc_parameters,
            'DM_Parameters': dm_parameters
        },
                                          input_path=diffr_dir,
                                          output_path=recon_dir)

        # Perfect detector.
        photon_detector = IdealPhotonDetector(parameters=None,
                                              input_path='diffr',
                                              output_path='detector')

        # Setup the photon experiment.
        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=reconstructor,
        )

        # Run the experiment.
        pxs.run()

        # Check histories.
        # prop.
        with h5py.File(os.path.join(prop_dir, 'prop_out_0000000.h5'),
                       'r') as prop_h5:
            self.assertIn('history', list(prop_h5.keys()))
            self.assertIn('parent', list(prop_h5['history'].keys()))
            self.assertIn('detail', list(prop_h5['history/parent'].keys()))
            self.assertIn('parent', list(prop_h5['history/parent'].keys()))

            prop_h5.close()

        # pmi.
        with h5py.File(os.path.join(pmi_dir, 'pmi_out_0000001.h5'),
                       'r') as pmi_h5:

            self.assertIn('history', list(pmi_h5.keys()))
            self.assertIn('parent', list(pmi_h5['history'].keys()))
            self.assertIn('detail', list(pmi_h5['history/parent'].keys()))
            self.assertIn('parent', list(pmi_h5['history/parent'].keys()))
            self.assertIn('detail',
                          list(pmi_h5['history/parent/parent'].keys()))
            self.assertIn('parent',
                          list(pmi_h5['history/parent/parent'].keys()))

            pmi_h5.close()

        # diffr.
        with h5py.File(os.path.join(diffr_dir, 'diffr_out_0000001.h5'),
                       'r') as diffr_h5:

            tasks = list(diffr_h5['data'].keys())
            for task in tasks:
                self.assertIn('history', list(diffr_h5["data"][task].keys()))
                self.assertIn('parent',
                              list(diffr_h5["data"][task]['history'].keys()))
                self.assertIn(
                    'detail',
                    list(diffr_h5["data"][task]['history/parent'].keys()))
                self.assertIn(
                    'parent',
                    list(diffr_h5["data"][task]['history/parent'].keys()))
                self.assertIn(
                    'parent',
                    list(diffr_h5["data"][task]
                         ['history/parent/parent'].keys()))
                self.assertIn(
                    'detail',
                    list(diffr_h5["data"][task]
                         ['history/parent/parent'].keys()))
                self.assertIn(
                    'parent',
                    list(diffr_h5["data"][task]
                         ['history/parent/parent/parent'].keys()))
                self.assertIn(
                    'detail',
                    list(diffr_h5["data"][task]
                         ['history/parent/parent/parent'].keys()))

            diffr_h5.close()

        # phase.
        with h5py.File(os.path.join(recon_dir, 'phase_out.h5'), 'r') as dm_h5:

            self.assertIn('history', list(dm_h5.keys()))
            self.assertIn('error', list(dm_h5['history'].keys()))
            self.assertIn('object', list(dm_h5['history'].keys()))

            dm_h5.close()
    def testSimS2EWorkflowDefaultPaths(self):
        """ Testing that a workflow akin to the simS2E example workflow works. No IO paths specified. """

        # These directories and files are expected to be present after a successfull calculation.
        expected_dirs = [
            'source_in',
            'source',
            'prop',
            'pmi',
            'diffr',
            'analysis',
        ]

        expected_files = [
            'source/FELsource_out_0000000.h5',
            'source/FELsource_out_0000001.h5',
            'prop/prop_out_0000000.h5',
            'prop/prop_out_0000001.h5',
            'pmi/pmi_out_0000001.h5',
            'pmi/pmi_out_0000002.h5',
            'diffr/diffr_out_0000001.h5',
            'analysis/orient_out.h5',
            'analysis/phase_out.h5',
        ]

        # Ensure proper cleanup.
        self.__dirs_to_remove = expected_dirs
        self.__files_to_remove.append('diffr.h5')
        self.__files_to_remove.append('detector')

        # Get proper FEL source files to start from.
        shutil.copytree(TestUtilities.generateTestFilePath('FELsource_out'),
                        'source_in')

        # Photon source.
        photon_source = XFELPhotonSource(input_path='source_in')

        # Photon propagator, default parameters.
        photon_propagator = XFELPhotonPropagator()

        # Photon interactor with default parameters.
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            sample_path=self.__sample_path)

        #  Diffraction with parameters.
        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 2,
            'number_of_diffraction_patterns': 1,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
            'number_of_MPI_processes': 2
        }

        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=TestUtilities.generateTestFilePath('pmi_out'))

        photon_diffractor.parameters.cpus_per_task = 1

        # Reconstruction: EMC+DM
        emc_parameters = {
            'initial_number_of_quaternions': 1,
            'max_number_of_quaternions': 2,
            'max_number_of_iterations': 10,
            'min_error': 1.0e-6,
            'beamstop': True,
            'detailed_output': False
        }

        dm_parameters = {
            'number_of_trials': 5,
            'number_of_iterations': 2,
            'averaging_start': 15,
            'leash': 0.2,
            'number_of_shrink_cycles': 2,
        }

        reconstructor = S2EReconstruction(parameters={
            'EMC_Parameters': emc_parameters,
            'DM_Parameters': dm_parameters
        })

        # Setup the photon experiment.
        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_analyzer=reconstructor,
        )

        # Run the experiment.
        pxs.run()

        # Check that all output files and directories are present.
        for directory in expected_dirs:
            print(directory)
            self.assertTrue(os.path.isdir(directory))
        for f in expected_files:
            print(f)
            self.assertTrue(os.path.isfile(f))
    def testSimS2EWorkflowSingleFile(self):
        """ Testing that a workflow akin to the simS2E example workflow works. Only one I/O file per calculator. """

        # These directories and files are expected to be present after a successfull calculation.
        expected_dirs = [
            'pmi',
            'diffr',
        ]

        expected_symlinks = ['detector']

        expected_files = [
            'FELsource_out.h5',
            'prop_out.h5',
            'pmi/pmi_out_0000001.h5',
            'diffr/diffr_out_0000001.h5',
            'detector/diffr_out_0000001.h5',
            'recon.h5',
            'orient_out.h5',
        ]

        # Ensure proper cleanup.
        self.__files_to_remove = expected_files + expected_symlinks
        self.__files_to_remove.append('prepHDF5.py')
        self.__dirs_to_remove = expected_dirs

        # Location of the FEL source file.
        source_input = TestUtilities.generateTestFilePath(
            'FELsource_out/FELsource_out_0000001.h5')

        # Photon source.
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')

        # Photon propagator, default parameters.
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')

        # Photon interactor with default parameters.
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path='prop_out.h5',
            output_path='pmi',
            sample_path=TestUtilities.generateTestFilePath('sample.h5'))

        #  Diffraction with parameters.
        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 1,
            'number_of_diffraction_patterns': 1,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
        }

        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path='pmi',
            output_path='diffr')

        # Perfect detector.
        photon_detector = PerfectPhotonDetector(parameters=None,
                                                input_path='diffr',
                                                output_path='detector')

        # Reconstruction: EMC+DM
        emc_parameters = {
            'initial_number_of_quaternions': 1,
            'max_number_of_quaternions': 9,
            'max_number_of_iterations': 3,
            'min_error': 1.0e-8,
            'beamstop': 1.0e-5,
            'detailed_output': False
        }

        dm_parameters = {
            'number_of_trials': 5,
            'number_of_iterations': 2,
            'averaging_start': 15,
            'leash': 0.2,
            'number_of_shrink_cycles': 2,
        }

        reconstructor = S2EReconstruction(parameters={
            'EMC_Parameters': emc_parameters,
            'DM_Parameters': dm_parameters
        },
                                          input_path='detector',
                                          output_path='recon.h5')

        # Setup the photon experiment.
        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=reconstructor,
        )

        # Run the experiment.
        pxs.run()

        # Check that all output files and directories are present.
        for directory in expected_dirs + expected_symlinks:
            self.assertTrue(os.path.isdir(directory))
        for f in expected_files:
            print f
            self.assertTrue(os.path.isfile(f))
    def testSimS2EWorkflowDirectories(self):
        """ Testing that a workflow akin to the simS2E example workflow works.
            Two sources, two diffraction patterns."""

        # Setup directories.
        working_directory = 'SPI'
        source_dir = os.path.join(working_directory, 'FELsource')
        prop_dir = os.path.join(working_directory, 'prop')
        pmi_dir = os.path.join(working_directory, 'pmi')
        diffr_dir = os.path.join(working_directory, 'diffr')
        detector_dir = os.path.join(working_directory, 'detector')
        recon_dir = os.path.join(working_directory, 'recon')

        # Make directories.
        os.mkdir(working_directory)
        os.mkdir(source_dir)
        os.mkdir(prop_dir)
        os.mkdir(pmi_dir)
        os.mkdir(diffr_dir)
        os.mkdir(detector_dir)
        os.mkdir(recon_dir)

        # Ensure proper cleanup.
        self.__dirs_to_remove.append(working_directory)

        # Location of the FEL source file.
        source_input = TestUtilities.generateTestFilePath('FELsource_out')

        # Photon source.
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path=source_dir)

        # Photon propagator, default parameters.
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path=source_dir,
                                                 output_path=prop_dir)

        # Photon interactor with default parameters.
        photon_interactor = XMDYNDemoPhotonMatterInteractor(
            parameters=None,
            input_path=prop_dir,
            output_path=pmi_dir,
            sample_path=TestUtilities.generateTestFilePath('sample.h5'))

        #  Diffraction with parameters.
        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 2,
            'number_of_diffraction_patterns': 2,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
        }

        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=pmi_dir,
            output_path=diffr_dir)

        # Reconstruction: EMC+DM
        emc_parameters = {
            'initial_number_of_quaternions': 1,
            'max_number_of_quaternions': 9,
            'max_number_of_iterations': 3,
            'min_error': 1.0e-8,
            'beamstop': 1.0e-5,
            'detailed_output': False
        }

        dm_parameters = {
            'number_of_trials': 5,
            'number_of_iterations': 2,
            'averaging_start': 15,
            'leash': 0.2,
            'number_of_shrink_cycles': 2,
        }

        reconstructor = S2EReconstruction(parameters={
            'EMC_Parameters': emc_parameters,
            'DM_Parameters': dm_parameters
        },
                                          input_path=diffr_dir,
                                          output_path=recon_dir)

        # Setup the photon experiment.
        pxs = PhotonExperimentSimulation(
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=None,
            photon_analyzer=reconstructor,
        )

        # Run the experiment.
        pxs.run()
    def testConstructionExceptions(self):
        """ Test that the appropriate exceptions are thrown if the object is constructed incorrectly. """
        # Setup a minimal experiment simulation.
        source_input = TestUtilities.generateTestFilePath('FELsource_out.h5')
        diffr_input = TestUtilities.generateTestFilePath('pmi_out.h5')
        pmi_input = TestUtilities.generateTestFilePath('prop_out.h5')
        photon_source = XFELPhotonSource(parameters=None,
                                         input_path=source_input,
                                         output_path='FELsource_out.h5')
        photon_propagator = XFELPhotonPropagator(parameters=None,
                                                 input_path='FELsource_out.h5',
                                                 output_path='prop_out.h5')
        photon_interactor = FakePhotonMatterInteractor(
            parameters=None, input_path=pmi_input, output_path='pmi_out.h5')

        diffraction_parameters = {
            'uniform_rotation': True,
            'calculate_Compton': False,
            'slice_interval': 100,
            'number_of_slices': 2,
            'pmi_start_ID': 1,
            'pmi_stop_ID': 1,
            'number_of_diffraction_patterns': 2,
            'beam_parameter_file':
            TestUtilities.generateTestFilePath('s2e.beam'),
            'beam_geometry_file':
            TestUtilities.generateTestFilePath('s2e.geom'),
        }
        photon_diffractor = SingFELPhotonDiffractor(
            parameters=diffraction_parameters,
            input_path=diffr_input,
            output_path='diffr_out.h5')

        photon_detector = PerfectPhotonDetector(parameters=None,
                                                input_path='diffr_out.h5',
                                                output_path='detector_out.h5')
        photon_analyzer = S2EReconstruction(parameters=None,
                                            input_path='detector_out.h5',
                                            output_path='analyzer_out.h5')

        # Check wrong source.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=None,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_propagator,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong propagator.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=None,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_source,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong interactor.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=None,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_source,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong diffractor.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=None,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_source,
            photon_detector=photon_detector,
            photon_analyzer=photon_analyzer,
        )

        # Check wrong analyzer.
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=None,
        )
        self.assertRaises(
            TypeError,
            PhotonExperimentSimulation,
            photon_source=photon_source,
            photon_propagator=photon_propagator,
            photon_interactor=photon_interactor,
            photon_diffractor=photon_diffractor,
            photon_detector=photon_detector,
            photon_analyzer=photon_diffractor,
        )