def testLoadPDB(self):
        """ Check that we can load a pdb and convert it to a dict. """

        # Setup path to pdb file.
        pdb_path = generateTestFilePath("2nip.pdb")

        self.__paths_to_remove.append('obsolete')

        # Attempt to load it.
        return_dict = IOUtilities.loadPDB(pdb_path)

        # Check items on dict.
        self.assertIsInstance( return_dict['Z'], numpy.ndarray )
        self.assertIsInstance( return_dict['r'], numpy.ndarray )
        self.assertIsInstance( return_dict['selZ'], dict )
        self.assertIsInstance( return_dict['N'], int )
        self.assertEqual( return_dict['Z'].shape, (4728,) )
        self.assertEqual( return_dict['r'].shape, (4728,3) )

        # Check that return type is a dict.
        self.assertIsInstance( return_dict, dict )

        # Check exception on corrupt file.
        # does not raise due to highly tolerant default acceptance levels in Bio.PDB.
        #self.assertRaises( IOError, IOUtilities.loadPDB, generateTestFilePath("2nip_corrupt.pdb" ) )

        # Check exception on wrong input type.
        self.assertRaises( IOError, IOUtilities.loadPDB, [1,2] )

        # Check exception on wrong file type.
        self.assertRaises( IOError, IOUtilities.loadPDB, generateTestFilePath("sample.h5") )
Esempio n. 2
0
    def testLoadPDB(self):
        """ Check that we can load a pdb and convert it to a dict. """

        # Setup path to pdb file.
        pdb_path = generateTestFilePath("2nip.pdb")

        self.__paths_to_remove.append('obsolete')

        # Attempt to load it.
        return_dict = IOUtilities.loadPDB(pdb_path)

        # Check items on dict.
        self.assertIsInstance( return_dict['Z'], numpy.ndarray )
        self.assertIsInstance( return_dict['r'], numpy.ndarray )
        self.assertIsInstance( return_dict['selZ'], dict )
        self.assertIsInstance( return_dict['N'], int )
        self.assertEqual( return_dict['Z'].shape, (4728,) )
        self.assertEqual( return_dict['r'].shape, (4728,3) )

        # Check that return type is a dict.
        self.assertIsInstance( return_dict, dict )

        # Check exception on wrong input type.
        self.assertRaises( IOError, IOUtilities.loadPDB, [1,2] )

        # Check exception on wrong file type.
        self.assertRaises( IOError, IOUtilities.loadPDB, generateTestFilePath("sample.h5") )
Esempio n. 3
0
    def testLegacyDictionary(self):
        """ Check parameter object can be initialized via a old-style dictionary. """
        parameters_dict = {
            'uniform_rotation': False,
            'calculate_Compton': True,
            'slice_interval': 12,
            'number_of_slices': 2,
            'pmi_start_ID': 4,
            'pmi_stop_ID': 5,
            'number_of_diffraction_patterns': 2,
            'beam_parameters': None,
            'detector_geometry': self.detector_geometry,
            'number_of_MPI_processes': 4,  # Legacy, has no effect.
            'sample': generateTestFilePath('2nip.pdb')
        }

        parameters = SingFELPhotonDiffractorParameters(**parameters_dict)

        # Check all parameters are set correctly.
        self.assertFalse(parameters.uniform_rotation)
        self.assertTrue(parameters.calculate_Compton)
        self.assertEqual(parameters.slice_interval, 12)
        self.assertEqual(parameters.number_of_slices, 2)
        self.assertEqual(parameters.pmi_start_ID, 4)
        self.assertEqual(parameters.pmi_stop_ID, 5)
        self.assertEqual(parameters.beam_parameters, None)
        self.assertEqual(parameters.detector_geometry, self.detector_geometry)
        self.assertEqual(parameters.sample, generateTestFilePath('2nip.pdb'))
Esempio n. 4
0
    def testGenesisDFLToWPGWavefront(self):
        """ Check the conversion from genesis dfl to wpg readable hdf5. """

        genesis_out_file = generateTestFilePath("genesis/lcls/lcls.out")
        genesis_dfl_file = generateTestFilePath("genesis/lcls/lcls.out.dfl")

        wf = IOUtilities.genesis_dfl_to_wavefront(genesis_out_file, genesis_dfl_file)

        self.assertIsInstance(wf, Wavefront)
Esempio n. 5
0
    def setUp(self):
        """ Setting up a test. """
        self.__files_to_remove = []
        self.__dirs_to_remove = []

        path_to_field_data = generateTestFilePath(
                os.path.join("issue-sample", "no_particles", "data%T.h5"))
        path_to_particle_data = generateTestFilePath(
                os.path.join("issue-sample", "no_fields", "data%T.h5"))
        path_to_data = generateTestFilePath(
                os.path.join("git-sample", "data%T.h5"))
        mode = api.Access_Type.read_only
        self.__field_series = api.Series(path_to_field_data, mode)
        self.__particle_series = api.Series(path_to_particle_data, mode)
        self.__series = api.Series(path_to_data, mode)
Esempio n. 6
0
    def testWpgToOPMDLegacyConverter(self):
        """ Test the conversion of wpg output to openPMD conform file, omitting the openpmd-api."""

        # Get sample file.
        h5_input = generateTestFilePath('prop_out/prop_out_0000011.h5')

        # Convert.
        convertToOPMDLegacy(h5_input)

        # New file name.
        opmd_h5_file = h5_input.replace(".h5", ".opmd.h5")

        # Make sure we clean up after test.
        self.__files_to_remove.append(opmd_h5_file)

        # Check new file was generated.
        self.assertTrue(os.path.isfile(opmd_h5_file))

        # Validate the new file.
        g = opmd_validator.open_file(opmd_h5_file)

        # Setup result array.
        result_array = numpy.array([0, 0])
        result_array += opmd_validator.check_root_attr(g, False)

        # Go through all the iterations, checking both the particles and the meshes
        extensions = {'ED-PIC': False, 'HYDRO1D': False}
        result_array += opmd_validator.check_iterations(g, False, extensions)

        # Assert that no errors nor warnings were issued.
        self.assertEqual(result_array[0], 0)
        self.assertEqual(result_array[1], 0)
Esempio n. 7
0
    def testHydroTxtToOPMDConverter(self):
        """ Test the conversion of esther output to openPMD conform hdf5 file."""

        # Get sample file.
        esther_output = generateTestFilePath('hydroTests')

        # Convert.
        convertTxtToOPMD(esther_output)

        # New file name.
        opmd_h5_file = esther_output + '/output.opmd.h5'

        # Make sure we clean up after test.
        self.__files_to_remove.append(opmd_h5_file)

        # Check new file was generated.
        self.assertTrue(os.path.isfile(opmd_h5_file))

        # Validate the new file.
        g = opmd_validator.open_file(opmd_h5_file)

        # Setup result array.
        result_array = numpy.array([0, 0])
        result_array += opmd_validator.check_root_attr(g, False)

        # Go through all the iterations, checking both the particles.
        # and the meshes.
        extensions = {'ED-PIC': False, 'HYDRO1D': True}
        result_array += opmd_validator.check_iterations(g, False, extensions)

        # Assert that no errors nor warnings were issued.
        self.assertEqual(result_array[0], 0)
        self.assertEqual(result_array[1], 0)
Esempio n. 8
0
    def test_sample2h5(self):
        """ Test the conversion utility to convert a sample directory to hdf5. """
        # Expected file to be generated.
        sample_file = 'sample.h5'

        # Clean up.
        self.__files_to_remove.append(sample_file)

        # Input dir.
        sample_dir = generateTestFilePath("sample_dir")

        # Run utility.
        sample2h5(sample_dir)

        # Check file was generated.
        self.assertIn(sample_file, os.listdir('.'))
        self.assertTrue(os.path.isfile(sample_file))

        # Check data is sane.
        expected_keys = ['Z', 'r']
        with h5py.File(sample_file, 'r') as h5:
            present_keys = h5.keys()
            for pk in present_keys:
                self.assertIn(pk, expected_keys)

            Z = h5['Z'].value
            r = h5['r'].value

        # Check data shapes are consistent.
        self.assertEqual(r.shape, (len(Z), 3))
    def testHydroTxtToOPMDConverter(self):
        """ Test the conversion of esther output to openPMD conform hdf5 file."""

        # Get sample file.
        esther_output = generateTestFilePath('hydroTests')

        # Convert.
        convertTxtToOPMD(esther_output)

        # New file name.
        opmd_h5_file = esther_output + '.opmd.h5'

        # Make sure we clean up after test.
        self.__files_to_remove.append(opmd_h5_file)


        # Check new file was generated.
        self.assertTrue( os.path.isfile( opmd_h5_file ) )

        # Validate the new file.
        g = opmd_validator.open_file(opmd_h5_file)

        # Setup result array.
        result_array = numpy.array([0, 0])
        result_array += opmd_validator.check_root_attr(g, False)

        # Go through all the iterations, checking both the particles.
        # and the meshes.
        extensions = {'ED-PIC': False, 'HYDRO1D': True}
        result_array += opmd_validator.check_iterations(g,False,extensions)

        # Assert that no errors nor warnings were issued.
        self.assertEqual( result_array[0], 0 )
        self.assertEqual( result_array[1], 0 )
Esempio n. 10
0
    def testWpgToOPMDConverter(self):
        """ Test the conversion of wpg output to openPMD conform file."""

        # Get sample file.
        h5_input = generateTestFilePath('prop_out/prop_out_0000011.h5')

        # Convert.
        convertToOPMD(h5_input)

        # New file name.
        opmd_h5_file = h5_input.replace(".h5", ".opmd.h5")
        self.__files_to_remove.append(opmd_h5_file)

        # Check new file was generated.
        self.assertTrue(os.path.isfile(opmd_h5_file))

        # Read the file back in through the API.
        series = opmd.Series(opmd_h5_file, opmd.Access_Type.read_only)

        self.assertIsInstance(series, opmd.Series)

        # Check attributes are present.
        try:
            series.author
            series.date
            series.software
            series.software_version
            series.get_attribute("radius of curvature in x")
            series.get_attribute("z coordinate")
            series.get_attribute("Rx_Unit_Dimension")
            series.get_attribute("Rx_UnitSI")
            series.get_attribute("radius of curvature in y")
            series.get_attribute("Ry_Unit_Dimension")
            series.get_attribute("Ry_UnitSI")
            series.get_attribute("Delta radius of curvature in x")
            series.get_attribute("DRx_Unit_Dimension")
            series.get_attribute("DRx_UnitSI")
            series.get_attribute("Delta radius of curvature in y")
            series.get_attribute("DRy_Unit_Dimension")
            series.get_attribute("DRy_UnitSI")
            series.get_attribute("photon energy")
            series.get_attribute("photon energy unit dimension")
            series.get_attribute("photon energy UnitSI")

        except RuntimeError:
            self.fail("Error while querying attribute.")
        except:
            raise

        # Check the beamline serialization
        self.assertIsInstance(series.get_attribute("beamline"), str)
Esempio n. 11
0
    def testConstructionWithSample(self):
        """ Testing the construction of the class with a PhotonBeamParameters instance. """

        # Attempt to construct an instance of the class.
        parameters = SingFELPhotonDiffractorParameters(
            sample=generateTestFilePath('2nip.pdb'),
            detector_geometry=self.detector_geometry,
            beam_parameters=self.beam)

        # Check instance and inheritance.
        self.assertIsInstance(parameters, SingFELPhotonDiffractorParameters)
        self.assertIsInstance(parameters, AbstractCalculatorParameters)

        # Check all parameters are set to default values.
        self.assertEqual(parameters.sample, generateTestFilePath('2nip.pdb'))
        self.assertEqual(parameters.uniform_rotation, False)
        self.assertFalse(parameters.calculate_Compton)
        self.assertEqual(parameters.slice_interval, 100)
        self.assertEqual(parameters.number_of_slices, 1)
        self.assertEqual(parameters.pmi_start_ID, 1)
        self.assertEqual(parameters.pmi_stop_ID, 1)
        self.assertEqual(parameters.beam_parameters, self.beam)
        self.assertEqual(parameters.detector_geometry, self.detector_geometry)
Esempio n. 12
0
    def testLoadOPMDWavefront(self):
        """ Test if loading a wavefront from openpmd-hdf into a WPG structure works."""

        # Get sample file.
        h5_input = generateTestFilePath('prop_out/prop_out_0000011.h5')

        # Convert.
        convertToOPMD(h5_input)

        # New file name.
        opmd_h5_file = h5_input.replace(".h5", ".opmd.h5")
        self.__files_to_remove.append(opmd_h5_file)

        # Reconstruct the series.
        series = opmd.Series(opmd_h5_file, opmd.Access_Type.read_only)
        wavefront = wpg.Wavefront()
Esempio n. 13
0
    def testLoadXYZ(self):
        """ Check that we can load a xyz file and convert it to a dict. """

        # Setup path to xyz file.
        xyz_path = generateTestFilePath("Fe2O3_poly_test.xyz")

        # Attempt to load it.
        return_dict = IOUtilities.loadXYZ(xyz_path)

        # Check that return type is a dict.
        self.assertIsInstance( return_dict, dict )

        # Check items on dict.
        self.assertIsInstance( return_dict['Z'], numpy.ndarray )
        self.assertIsInstance( return_dict['r'], numpy.ndarray )
        self.assertIsInstance( return_dict['selZ'], dict )
        self.assertIsInstance( return_dict['N'], int )
        self.assertEqual( return_dict['Z'].shape, (100,) )
        self.assertEqual( return_dict['r'].shape, (100,3) )
Esempio n. 14
0
    def testPdbToS2ESampleDict(self):
        """ Check the utility that converts a pdb file to an s2e sample dict.
        """

        # Setup path to pdb file.
        pdb_path = generateTestFilePath("2nip.pdb")

        # Attempt to load it.
        return_dict = IOUtilities._pdbToS2ESampleDict(pdb_path)

        # Check that return type is a dict.
        self.assertIsInstance( return_dict, dict )

        # Check items on dict.
        self.assertIsInstance( return_dict['Z'], numpy.ndarray )
        self.assertIsInstance( return_dict['r'], numpy.ndarray )
        self.assertIsInstance( return_dict['selZ'], dict )
        self.assertIsInstance( return_dict['N'], int )
        self.assertEqual( return_dict['Z'].shape, (4728,) )
        self.assertEqual( return_dict['r'].shape, (4728,3) )
Esempio n. 15
0
    def testDefaultConstruction(self):
        """ Testing the default construction of the class using a dictionary. """

        # Attempt to construct an instance of the class.
        parameters = SingFELPhotonDiffractorParameters(
            sample=generateTestFilePath("2nip.pdb"))

        # Check instance and inheritance.
        self.assertIsInstance(parameters, SingFELPhotonDiffractorParameters)
        self.assertIsInstance(parameters, AbstractCalculatorParameters)

        # Check all parameters are set to default values.
        self.assertEqual(parameters.uniform_rotation, False)
        self.assertFalse(parameters.calculate_Compton)
        self.assertEqual(parameters.slice_interval, 100)
        self.assertEqual(parameters.number_of_slices, 1)
        self.assertEqual(parameters.pmi_start_ID, 1)
        self.assertEqual(parameters.pmi_stop_ID, 1)
        self.assertEqual(parameters.beam_parameters, None)
        self.assertEqual(parameters.detector_geometry, None)
Esempio n. 16
0
 def testWPGDataPropConstruction(self):
     """ Testing the construction of the class with the prop hdf5 file. """
     file_path = generateTestFilePath("prop_out_0000001.h5")
     print("Testing path: ", file_path)
     prop_data = WPGdata(file_path)
     self.assertIsInstance(prop_data.wavefront, Wavefront)
Esempio n. 17
0
 def testWPGDataSourceConstruction(self):
     """ Testing the construction of the class with the source hdf5 file. """
     file_path = generateTestFilePath("FELsource_out.h5")
     print("Testing path: ", file_path)
     FELsource_data = WPGdata(file_path)
     self.assertIsInstance(FELsource_data.wavefront, Wavefront)