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)
Пример #2
0
    '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='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': 100,
    'min_error': 1.0e-8,
    'beamstop': 1.0e-5,
    'detailed_output': False
}

dm_parameters = {
    'number_of_trials': 5,
    'number_of_iterations': 2,
    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 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,
        )