Esempio n. 1
0
    def setUpClass(cls):
        """ Setting up the test class. """
        cls.input_h5 = TestUtilities.generateTestFilePath('pmi_out_0000001.h5')
        detector_panel = DetectorPanel(
            ranges={
                'fast_scan_min': 0,
                'fast_scan_max': 21,
                'slow_scan_min': 0,
                'slow_scan_max': 21
            },
            pixel_size=2.2e-4 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.13 * meter,
            corners={
                'x': -11,
                'y': -11
            },
        )

        cls.detector_geometry = DetectorGeometry(panels=[detector_panel])

        cls.beam = PhotonBeamParameters(
            photon_energy=8.6e3 * electronvolt,
            beam_diameter_fwhm=1.0e-6 * meter,
            pulse_energy=1.0e-3 * joule,
            photon_energy_relative_bandwidth=0.001,
            divergence=1e-3 * radian,
            photon_energy_spectrum_type="SASE",
        )
Esempio n. 2
0
    def setUp(self):
        """ Setting up a test. """
        self.__files_to_remove = []
        self.__dirs_to_remove = []

        # Construct the panel.
        self.__panel = DetectorPanel(
            dimensions=["ss", "fs"],
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 511,
                "slow_scan_min": 512,
                "slow_scan_max": 1024,
            },
            pixel_size=2.2e-4 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.13 * meter,
            distance_offset=0.0 * meter,
            fast_scan_xyz=None,
            slow_scan_xyz=None,
            corners={
                "x": -512,
                "y": -256
            },
            saturation_adu=1e4,
            mask=None,
            good_bit_mask=None,
            bad_bit_mask=None,
            saturation_map=None,
            badregion_flag=False,
        )
Esempio n. 3
0
    def setUpClass(cls):
        """ Setting up the test class. """
        # Setup parameters.
        cls.__beam_parameters = PhotonBeamParameters(
            photon_energy=4.96e3 * electronvolt,
            photon_energy_relative_bandwidth=0.01,
            beam_diameter_fwhm=2e-6 * meter,
            divergence=2e-6 * radian,
            pulse_energy=1e-3 * joule,
            photon_energy_spectrum_type='tophat')

        cls.__geometry = DetectorGeometry(panels=DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 63,
                "slow_scan_min": 0,
                "slow_scan_max": 63
            },
            pixel_size=220.0e-6 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.1 * meter,
            corners={
                "x": -32,
                "y": -32
            },
            saturation_adu=1e4,
        ))
    def testBackengineGPU(self):
        """ Check a backengine calculation with openCL enabled. """

        # Ensure cleanup.
        self.__dirs_to_remove.append("diffr")
        self.__files_to_remove.append("diffr.h5")

        # Clean up to make sure no old files mess things up.
        self.tearDown()

        beam_parameters = PhotonBeamParameters(
            photon_energy=4.96e3 * electronvolt,
            photon_energy_relative_bandwidth=0.01,
            beam_diameter_fwhm=2e-6 * meter,
            divergence=2e-6 * radian,
            pulse_energy=1e-3 * joule,
            photon_energy_spectrum_type='tophat')

        geometry = DetectorGeometry(panels=DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 63,
                "slow_scan_min": 0,
                "slow_scan_max": 63
            },
            pixel_size=220.0e-6 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.1 * meter,
            corners={
                "x": -32,
                "y": -32
            },
            saturation_adu=1e4,
        ))

        parameters = CrystFELPhotonDiffractorParameters(
            sample=self.__sample,
            beam_parameters=beam_parameters,
            detector_geometry=geometry,
            number_of_diffraction_patterns=10,
            use_gpu=True)

        # Get calculator.
        diffractor = CrystFELPhotonDiffractor(parameters=parameters,
                                              input_path=None,
                                              output_path='diffr')

        # Run backengine
        status = diffractor.backengine()

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

        output_path = "%s" % diffractor.output_path
        # Check output dir was created.
        self.assertTrue(os.path.isdir(output_path))

        # Check pattern was written.
        diffractor.saveH5()
        self.assertIn("diffr_out_0000001.h5", os.listdir(output_path))
    def setUpClass(cls):
        detector_panel = DetectorPanel(
            ranges={
                'fast_scan_min': 0,
                'fast_scan_max': 1023,
                'slow_scan_min': 0,
                'slow_scan_max': 1023
            },
            pixel_size=2.2e-4 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.13 * meter,
            corners={
                'x': -512,
                'y': 512
            },
        )

        cls.detector_geometry = DetectorGeometry(panels=[detector_panel])

        cls.beam = PhotonBeamParameters(
            photon_energy=8.6e3 * electronvolt,
            beam_diameter_fwhm=1.0e-6 * meter,
            pulse_energy=1.0e-3 * joule,
            photon_energy_relative_bandwidth=0.001,
            divergence=None,
            photon_energy_spectrum_type="SASE",
        )
    def testBackengine(self):
        # Get calculator.

        # Ensure cleanup.
        self.__dirs_to_remove.append("diffr")

        # Setup parameters.
        beam_parameters = PhotonBeamParameters(
            photon_energy=4.96e3 * electronvolt,
            photon_energy_relative_bandwidth=0.01,
            beam_diameter_fwhm=2e-6 * meter,
            divergence=2e-6 * radian,
            pulse_energy=1e-3 * joule,
            photon_energy_spectrum_type='tophat')

        self.assertIsInstance(beam_parameters, PhotonBeamParameters)

        geometry = DetectorGeometry(panels=DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 63,
                "slow_scan_min": 0,
                "slow_scan_max": 63
            },
            pixel_size=220.0e-6 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.1 * meter,
            corners={
                "x": -32,
                "y": -32
            },
            saturation_adu=1e4,
        ))

        sys.stdout.flush()
        parameters = CrystFELPhotonDiffractorParameters(
            sample=self.__sample,
            beam_parameters=beam_parameters,
            detector_geometry=geometry,
            number_of_diffraction_patterns=10)

        diffractor = CrystFELPhotonDiffractor(parameters=parameters,
                                              input_path=None,
                                              output_path='diffr')

        # Run backengine
        status = diffractor.backengine()

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

        # Check output dir was created.
        output_path = "%s" % diffractor.output_path
        self.assertTrue(os.path.isdir(output_path))

        # Check pattern was written.
        self.assertIn("diffr_out-1.h5", os.listdir(output_path))
    def testShapedConstructionGeometry(self):
        """ Testing the construction with a geometry object. """

        beam_parameters = PhotonBeamParameters(
            photon_energy=4.96e3 * electronvolt,
            photon_energy_relative_bandwidth=0.01,
            beam_diameter_fwhm=2e-6 * meter,
            divergence=2e-6 * radian,
            pulse_energy=1e-3 * joule)

        geometry = DetectorGeometry(panels=DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 63,
                "slow_scan_min": 0,
                "slow_scan_max": 63
            },
            pixel_size=220.0e-6 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.1 * meter,
            corners={
                "x": -32,
                "y": -32
            },
            saturation_adu=1e4,
        ))
        # Attempt to construct an instance of the class.
        parameters = CrystFELPhotonDiffractorParameters(
            sample=self.__sample,
            powder=True,
            number_of_diffraction_patterns=10,
            number_of_background_photons=100,
            poissonize=True,
            suppress_fringes=True,
            crystal_size_min=10.0e-9 * meter,
            crystal_size_max=100.0e-9 * meter,
            uniform_rotation=False,
            beam_parameters=beam_parameters,
            detector_geometry=geometry,
        )

        # Check all parameters are set as intended.
        self.assertFalse(parameters.uniform_rotation)
        self.assertEqual(parameters.number_of_diffraction_patterns, 10)
        self.assertTrue(parameters.powder)
        self.assertEqual(parameters.crystal_size_min.m_as(meter), 10.e-9)
        self.assertEqual(parameters.crystal_size_max.m_as(meter), 100.e-9)
        self.assertTrue(parameters.poissonize)
        self.assertEqual(parameters.number_of_background_photons, 100)
        self.assertTrue(parameters.suppress_fringes)
        self.assertIsInstance(parameters.beam_parameters, PhotonBeamParameters)
        self.assertEqual(
            parameters.beam_parameters.photon_energy.m_as(electronvolt),
            4.96e3)
        self.assertEqual(parameters.detector_geometry, geometry)
Esempio n. 8
0
    def setUpClass(cls):
        detector_panel = DetectorPanel(ranges={'fast_scan_min': 0,
                                               'fast_scan_max': 1023,
                                               'slow_scan_min': 0,
                                               'slow_scan_max': 1023},
                                       pixel_size=2.2e-4*meter,
                                       photon_response=1.0,
                                       distance_from_interaction_plane=0.13*meter,
                                       corners={'x': -512, 'y': 512},
                                       )

        cls.detector_geometry = DetectorGeometry(panels=[detector_panel])
Esempio n. 9
0
    def testBackengineWithBeamAndGeometry(self):
        """ Check geom parameter logic if they are set as parameters. """

        # Ensure cleanup.
        self.__dirs_to_remove.append("diffr")

        # Setup beam parameters.
        beam_parameters = PhotonBeamParameters(
            photon_energy=16.0e3 * electronvolt,
            photon_energy_relative_bandwidth=0.001,
            pulse_energy=2.0e-3 * joule,
            beam_diameter_fwhm=100e-9 * meter,
            divergence=None,
            photon_energy_spectrum_type="tophat",
        )

        geometry = DetectorGeometry(panels=DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 63,
                "slow_scan_min": 0,
                "slow_scan_max": 63
            },
            pixel_size=220.0e-6 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.1 * meter,
            corners={
                "x": -32,
                "y": -32
            },
            saturation_adu=1e4,
        ))
        # Get parameters.
        parameters = CrystFELPhotonDiffractorParameters(
            sample=self.__sample,
            detector_geometry=geometry,
            beam_parameters=beam_parameters,
            number_of_diffraction_patterns=2,
            uniform_rotation=True,
        )

        # Get calculator.
        diffractor = CrystFELPhotonDiffractor(parameters=parameters,
                                              input_path=None,
                                              output_path='diffr')

        # Run backengine
        status = diffractor.backengine()

        # Check return code.
        self.assertEqual(status, 0)
Esempio n. 10
0
    def setUp(self):
        """ Setting up a test. """
        self.__panel0 = DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 511,
                "slow_scan_min": 512,
                "slow_scan_max": 1023,
            },
            pixel_size=2.2e-4 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.13 * meter,
            distance_offset=0.0 * meter,
            fast_scan_xyz=None,
            slow_scan_xyz=None,
            corners={
                "x": -512,
                "y": -256
            },
            saturation_adu=1e4,
            mask=None,
            good_bit_mask=None,
            bad_bit_mask=None,
            saturation_map=None,
            badregion_flag=False,
        )

        # Copy-construct a second panel.
        self.__panel1 = self.__panel0(
            ranges={
                'fast_scan_min':
                self.__panel0.ranges['fast_scan_min'],
                'fast_scan_max':
                self.__panel0.ranges['fast_scan_max'],
                'slow_scan_min':
                self.__panel0.ranges['slow_scan_max'] + 1,
                'slow_scan_max':
                2 * self.__panel0.ranges['slow_scan_max'] -
                self.__panel0.ranges['slow_scan_min'] + 1
            },
            corners={
                'x': self.__panel0.corners['x'],
                'y': self.__panel0.corners['y'] + 512
            },
        )

        self.__files_to_remove = []
        self.__dirs_to_remove = []
Esempio n. 11
0
    def plot_diffr_vs_detector(self):
        """ Compare patterns before and after detector sim. """

        # Cleanup.
        #self.__files_to_remove.append('5mzd.pdb')
        #self.__files_to_remove.append('diffr.h5')
        #self.__dirs_to_remove.append('diffr')

        # Avoid crash due to multiple instances of G4RunManager
        del self._detector

        # Setup detector geometry.
        detector_panel = DetectorPanel(
            ranges={
                'fast_scan_min': 0,
                'fast_scan_max': 511,
                'slow_scan_min': 0,
                'slow_scan_max': 511
            },
            pixel_size=2.2e-4 * Units.meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.13 * Units.meter,
            corners={
                'x': -256,
                'y': -256
            },
        )

        detector_geometry = DetectorGeometry(panels=[detector_panel])

        # Setup photon beam.
        beam = PhotonBeamParameters(
            photon_energy=4.96e3 * Units.electronvolt,
            beam_diameter_fwhm=1.0e-6 * Units.meter,
            pulse_energy=1.0e-3 * Units.joule,
            photon_energy_relative_bandwidth=0.001,
            divergence=1e-3 * Units.radian,
            photon_energy_spectrum_type="SASE",
        )

        # Setup and run the diffraction sim.
        diffraction_parameters = SingFELPhotonDiffractorParameters(
            uniform_rotation=None,
            calculate_Compton=False,
            number_of_diffraction_patterns=1,
            detector_geometry=detector_geometry,
            beam_parameters=beam,
            sample="5mzd.pdb",
            forced_mpi_command='mpirun -np 1',
        )

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

        photon_diffractor.backengine()
        photon_diffractor.saveH5()

        analysis1 = DiffractionAnalysis(photon_diffractor.output_path,
                                        pattern_indices=[1],
                                        poissonize=True)
        analysis1.plotPattern(
            operation=None,
            logscale=False,
        )

        parameters = XCSITPhotonDetectorParameters(
            detector_type="AGIPDSPB",
            patterns=[0],
        )

        detector = XCSITPhotonDetector(
            parameters=parameters,
            input_path="diffr.h5",
            output_path="detector_out.h5",
        )

        detector._readH5()
        detector.backengine()
        detector.saveH5()

        # Weak test Check we have photons in the signal.
        pattern = h5py.File("detector_out.h5", 'r')['data/0000001/data'].value

        analysis2 = DiffractionAnalysis(detector.output_path,
                                        pattern_indices=[1],
                                        poissonize=True)
        analysis2.plotPattern(
            operation=None,
            logscale=False,
        )

        mpl.pyplot.show()
Esempio n. 12
0
        if os.path.isdir(d):
            shutil.rmtree(d)
    for f in files_to_remove:
        if os.path.isfile(f):
            os.remove(f)


# Detector setup
p0 = DetectorPanel(ranges={
    'fast_scan_min': 0,
    'fast_scan_max': 99,
    'slow_scan_min': 0,
    'slow_scan_max': 99
},
                   pixel_size=560e-6 * meter,
                   photon_response=1.0,
                   distance_from_interaction_plane=0.13 * meter,
                   corners={
                       'x': -49.5,
                       'y': -49.5
                   },
                   fast_scan_xyz='1.0x',
                   slow_scan_xyz='1.0y')

detector_geometry = DetectorGeometry(panels=[p0])

beam = PhotonBeamParameters(
    photon_energy=6.0e3 * electronvolt,
    beam_diameter_fwhm=226.0e-9 * meter,  #sqrt(250*160/pi)*2
    pulse_energy=4.0e-3 * joule,
)
Esempio n. 13
0
    def testDefaultConstruction(self):
        """ Testing the default construction. """

        # Attempt to construct an instance of the class.
        panel = DetectorPanel()
        self.assertIsInstance(panel, DetectorPanel)
Esempio n. 14
0
    def testSaveH5(self):
        """ Check that saveh5() creates correct filenames. """

        # Ensure cleanup.
        self.__dirs_to_remove.append("diffr")
        self.__files_to_remove.append("diffr.h5")

        # Setup beam parameters.
        beam_parameters = PhotonBeamParameters(
            photon_energy=5e3 * electronvolt,
            pulse_energy=2e-3 * joule,
            photon_energy_relative_bandwidth=1e-3,
            photon_energy_spectrum_type="tophat",
            beam_diameter_fwhm=3e-6 * meter,
        )

        geometry = DetectorGeometry(panels=DetectorPanel(
            ranges={
                "fast_scan_min": 0,
                "fast_scan_max": 63,
                "slow_scan_min": 0,
                "slow_scan_max": 63
            },
            pixel_size=220.0e-6 * meter,
            photon_response=1.0,
            distance_from_interaction_plane=0.1 * meter,
            corners={
                "x": -32,
                "y": -32
            },
            saturation_adu=1e4,
        ))

        # Get parameters.
        parameters = CrystFELPhotonDiffractorParameters(
            sample=self.__sample,
            detector_geometry=geometry,
            beam_parameters=beam_parameters,
            number_of_diffraction_patterns=2,
            uniform_rotation=True,
        )

        # Get calculator.
        diffractor = CrystFELPhotonDiffractor(parameters=parameters,
                                              input_path=None,
                                              output_path='diffr')

        # Run backengine
        status = diffractor.backengine()

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

        # Check output dir was created.
        self.assertTrue(os.path.isdir(diffractor.output_path))

        # Save correctly.
        diffractor.saveH5()

        # Check output file was created.
        self.assertTrue(os.path.isfile(diffractor.output_path))

        # Check pattern was written.
        self.assertIn("diffr_out_0000001.h5", os.listdir("diffr"))
        self.assertIn("diffr_out_0000002.h5", os.listdir("diffr"))

        # Open linked h5 file.
        with h5py.File(diffractor.output_path, 'r') as h5:
            self.assertIn("data", list(h5.keys()))
            self.assertIn("0000001", list(h5["data"].keys()))
            self.assertIn("0000002", list(h5["data"].keys()))
            self.assertIn("data", list(h5["data/0000001"].keys()))
            self.assertIn("data", list(h5["data/0000002"].keys()))

            self.assertIn("params", list(h5.keys()))
            self.assertIn("beam", list(h5["params"].keys()))
            self.assertIn("photonEnergy", list(h5["params/beam"].keys()))
            self.assertIn("focusArea", list(h5["params/beam"].keys()))

        # Check metafile was created.
        self.assertIn(
            os.path.split(diffractor.output_path)[-1],
            os.listdir(os.path.dirname(diffractor.output_path)))