def testSingleDiffrFileMultiplePatterns(self): """ Test that EMC accepts a single (new style) input file from diffraction containing multiple patterns. """ # Cleanup. self.__files_to_remove.append('orient_out.h5') # Construct the object. emc_parameters = {"initial_number_of_quaternions" : 3, "max_number_of_quaternions" : 4, "max_number_of_iterations" : 3, "min_error" : 1.e-6, "beamstop" : True, "detailed_output" : True, } emc = EMCOrientation(parameters=emc_parameters, input_path=TestUtilities.generateTestFilePath("diffr_newstyle.h5"), output_path='orient_out.h5', tmp_files_path=None, run_files_path=None,) # Call backengine. status = emc.backengine() # Check success. self.assertEqual(status, 0)
def testPaths(self): """ Test that we can start a test calculation. """ self.__files_to_remove.append('orient_out.h5') # Construct the object. analyzer = EMCOrientation(parameters=None, input_path=self.input_h5, output_path='orient_out.h5') # Keep the test short. analyzer.parameters.max_number_of_iterations=2 # Call backengine. status = analyzer.backengine() # Check paths exist and are populated. run_files_path = analyzer.run_files_path tmp_files_path = analyzer.tmp_files_path self.assertTrue( os.path.isdir( run_files_path ) ) self.assertTrue( os.path.isdir( tmp_files_path ) ) expected_run_files = ["finish_intensity.dat", "quaternion.dat", "detector.dat", "most_likely_orientations.dat", "start_intensity.dat", "EMC_extended.log", "mutual_info.dat", "EMC.log", "photons.dat", ] for ef in expected_run_files: self.assertIn( ef, os.listdir(run_files_path) )
def testBackengine(self): """ Test that we can start a test calculation. """ self.__files_to_remove.append('orient_out.h5') # Construct the object. analyzer = EMCOrientation(parameters=None, input_path=self.input_h5, output_path='orient_out.h5') # Call backengine. status = analyzer.backengine() self.assertEqual(status, 0)
def testPaths(self): """ Test that we can start a test calculation. """ self.__files_to_remove.append('orient_out.h5') # Construct the object with very nonsensical iteration control parameters. emc_parameters = { "initial_number_of_quaternions": 1, "max_number_of_quaternions": 2, "max_number_of_iterations": 2, "min_error": 5.e-6, "beamstop": True, "detailed_output": True, } # Case 1: no paths given, make dirs in /tmp emc = EMCOrientation( parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path=None, run_files_path=None, ) # Construct the object. # Keep the test short. emc.parameters.max_number_of_iterations = 2 # Call backengine. status = emc.backengine() # Check paths exist and are populated. run_files_path = emc.run_files_path tmp_files_path = emc.tmp_files_path self.assertTrue(os.path.isdir(run_files_path)) self.assertTrue(os.path.isdir(tmp_files_path)) expected_run_files = [ "finish_intensity.dat", "quaternion.dat", "detector.dat", "most_likely_orientations.dat", "start_intensity.dat", "EMC_extended.log", "mutual_info.dat", "EMC.log", "photons.dat", ] for ef in expected_run_files: self.assertIn(ef, os.listdir(run_files_path))
def testConstruction(self): """ Testing the default construction of the class. """ # Construct the object. analyzer = EMCOrientation(parameters=None, input_path=self.input_h5, output_path='orient_out.h5') self.assertIsInstance(analyzer, EMCOrientation)
def testConstruction(self): """ Testing the default construction of the class. """ # Construct the object. analyzer = EMCOrientation() # Test a parameter. self.assertEqual(analyzer.parameters.initial_number_of_quaternions, 1) self.assertIsInstance(analyzer, EMCOrientation)
def testDiffr0_2(self): """ Test that we can handle diffr input version 0.2 """ self.__files_to_remove.append('orient_out.h5') # Construct the object. emc_parameters = { "initial_number_of_quaternions": 2, "max_number_of_quaternions": 4, "max_number_of_iterations": 10, "min_error": 1.e-6, "beamstop": True, "detailed_output": True, } emc = EMCOrientation( parameters=emc_parameters, input_path=TestUtilities.generateTestFilePath("diffr_0.2"), output_path='orient_out.h5', tmp_files_path=None, run_files_path=None, ) # Call backengine. status = emc.backengine() self.assertEqual(status, 0) # Cleanup. self.__files_to_remove.append('orient_out.h5') # Construct the object. emc_parameters = { "initial_number_of_quaternions": 3, "max_number_of_quaternions": 4, "max_number_of_iterations": 3, "min_error": 1.e-6, "beamstop": True, "detailed_output": True, } emc = EMCOrientation( parameters=emc_parameters, input_path=TestUtilities.generateTestFilePath("diffr_newstyle.h5"), output_path='orient_out.h5', tmp_files_path=None, run_files_path=None, ) # Call backengine. status = emc.backengine() # Check success. self.assertEqual(status, 0)
def testConstructionParameters(self): """ Testing the construction of the class with a parameters object. """ # Construct the object. emc_parameters = EMCOrientationParameters( initial_number_of_quaternions=2, max_number_of_quaternions=4, max_number_of_iterations=5, min_error=1.e-6, beamstop=False, detailed_output=False) analyzer = EMCOrientation(parameters=emc_parameters) # Test a parameter. self.assertEqual(analyzer.parameters.initial_number_of_quaternions, 2) self.assertIsInstance(analyzer, EMCOrientation)
def testConstructionParametersDict(self): """ Testing the construction of the class with a parameters dictionary. """ # Construct the object. emc_parameters = { 'initial_number_of_quaternions': 2, 'max_number_of_quaternions': 4, 'max_number_of_iterations': 5, 'min_error': 1.e-6, 'beamstop': False, 'detailed_output': False } analyzer = EMCOrientation(parameters=emc_parameters) # Test a parameter. self.assertEqual(analyzer.parameters.initial_number_of_quaternions, 2) self.assertIsInstance(analyzer, EMCOrientation)
def __init__(self, parameters=None, input_path=None, output_path=None): """ :param parameters: The parameters for the reconstruction. :type parameters: dict :example parameters: parameters={'EMC_Parameters' : EMCOrientationParameters(), 'DM_Parameters' : DMPhasingParameters()} # Use default parameters for EMC and DM. :param input_path: Path for input data. :type input_path: str :param output_path: Path where to write output to. :type output_path: str """ # Initialize base class. super(S2EReconstruction, self).__init__(parameters, input_path, output_path) self.__provided_data = [ '/data/electronDensity', '/params/info', '/history', '/info', '/misc', '/version', ] self.__expected_data = [ '/data/data', '/data/diffr', '/data/angle', '/history/parent/detail', '/history/parent/parent', '/info/package_version', '/info/contact', '/info/data_description', '/info/method_description', '/params/geom/detectorDist', '/params/geom/pixelWidth', '/params/geom/pixelHeight', '/params/geom/mask', '/params/beam/photonEnergy', '/params/beam/photons', '/params/beam/focusArea', '/params/info', ] emc_parameters = None dm_parameters = None # Construct emc and dm calculators. if self.parameters != {}: emc_parameters = self.parameters['EMC_Parameters'] dm_parameters = self.parameters['DM_Parameters'] if os.path.isdir(self.output_path): intermediate_output_path = os.path.join(self.output_path, 'orient_out.h5') else: intermediate_output_path = 'orient_out.h5' self.__emc = EMCOrientation(emc_parameters, self.input_path, intermediate_output_path) self.__dm = DMPhasing(dm_parameters, intermediate_output_path, self.output_path)
class S2EReconstruction(AbstractPhotonAnalyzer): """ :class S2EReconstruction: Class representing photon data analysis for electron density reconstruction from 2D diffraction patterns. Wraps the EMC orientation module and the DM phasing module. """ def __init__(self, parameters=None, input_path=None, output_path=None): """ :param parameters: The parameters for the reconstruction. :type parameters: dict :example parameters: parameters={'EMC_Parameters' : EMCOrientationParameters(), 'DM_Parameters' : DMPhasingParameters()} # Use default parameters for EMC and DM. :param input_path: Path for input data. :type input_path: str :param output_path: Path where to write output to. :type output_path: str """ # Initialize base class. super(S2EReconstruction, self).__init__(parameters, input_path, output_path) self.__provided_data = [ '/data/electronDensity', '/params/info', '/history', '/info', '/misc', '/version', ] self.__expected_data = [ '/data/data', '/data/diffr', '/data/angle', '/history/parent/detail', '/history/parent/parent', '/info/package_version', '/info/contact', '/info/data_description', '/info/method_description', '/params/geom/detectorDist', '/params/geom/pixelWidth', '/params/geom/pixelHeight', '/params/geom/mask', '/params/beam/photonEnergy', '/params/beam/photons', '/params/beam/focusArea', '/params/info', ] emc_parameters = None dm_parameters = None # Construct emc and dm calculators. if self.parameters != {}: emc_parameters = self.parameters['EMC_Parameters'] dm_parameters = self.parameters['DM_Parameters'] if os.path.isdir(self.output_path): intermediate_output_path = os.path.join(self.output_path, 'orient_out.h5') else: intermediate_output_path = 'orient_out.h5' self.__emc = EMCOrientation(emc_parameters, self.input_path, intermediate_output_path) self.__dm = DMPhasing(dm_parameters, intermediate_output_path, self.output_path) def expectedData(self): """ Query for the data expected by the Analyzer. """ return self.__expected_data def providedData(self): """ Query for the data provided by the Analyzer. """ return self.__provided_data @property def data(self): """ Query for the field data. """ return self.__data def _readH5(self): """ """ """ Private method for reading the hdf5 input and extracting the parameters and data relevant to initialize the object. """ pass # Nothing to be done since IO happens in backengine. def saveH5(self): """ """ """ Private method to save the object to a file. :param output_path: The file where to save the object's data. :type output_path: str """ pass # No action required since output is written in backengine. def backengine(self): """ Run the EMC and DM backengine executables. """ # Run EMC. emc_status = self.__emc.backengine() # If EMC was not successful, return with error code. if emc_status != 0: return emc_status # Else run DM. return self.__dm.backengine()
def testPhotonFileConsecutiveRuns(self): """ Check that the photons.dat from the previous run is reused. """ self.__files_to_remove.append('orient_out.h5') self.__files_to_remove.append('orient_out2.h5') # Construct the object. emc_parameters = { "initial_number_of_quaternions": 1, "max_number_of_quaternions": 2, "max_number_of_iterations": 2, "min_error": 1.e-6, "beamstop": True, "detailed_output": True, } emc = EMCOrientation( parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path=None, run_files_path=None, ) # Call backengine. status = emc.backengine() # Check paths exist and are populated. run_files_path = emc.run_files_path tmp_files_path = emc.tmp_files_path self.assertTrue(os.path.isdir(run_files_path)) self.assertTrue(os.path.isdir(tmp_files_path)) expected_run_files = [ "finish_intensity.dat", "quaternion.dat", "detector.dat", "most_likely_orientations.dat", "start_intensity.dat", "EMC_extended.log", "mutual_info.dat", "EMC.log", "photons.dat", ] for ef in expected_run_files: self.assertIn(ef, os.listdir(run_files_path)) # Second run. emc2 = EMCOrientation(parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out2.h5', tmp_files_path=emc.tmp_files_path, run_files_path=None) # Call backengine. status = emc2.backengine() # Check paths exist and are populated. run_files_path2 = emc2.run_files_path tmp_files_path2 = emc2.tmp_files_path self.assertNotEqual(run_files_path, run_files_path2) self.assertEqual(tmp_files_path, tmp_files_path2) expected_run_files2 = [ "finish_intensity.dat", "quaternion.dat", "detector.dat", "most_likely_orientations.dat", "start_intensity.dat", "EMC_extended.log", "mutual_info.dat", "EMC.log", "photons.dat", ] for ef in expected_run_files2: self.assertIn(ef, os.listdir(run_files_path2))
def testSetupPaths(self): """ Check that setting up paths works correctly. """ # Clean up no matter how tests go. self.__dirs_to_remove.append("emc_run") self.__dirs_to_remove.append("emc_tmp") # Construct the object with very nonsensical iteration control parameters. emc_parameters = { "initial_number_of_quaternions": 1, "max_number_of_quaternions": 2, "max_number_of_iterations": 2, "min_error": 5.e-6, "beamstop": True, "detailed_output": True, } # Case 1: no paths given, make dirs in /tmp emc = EMCOrientation( parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path=None, run_files_path=None, ) run, tmp = emc._setupPaths() # Check. self.assertTrue(os.path.isdir(emc.run_files_path)) self.assertTrue(os.path.isdir(emc.tmp_files_path)) self.assertEqual(run, emc.run_files_path) self.assertEqual(tmp, emc.tmp_files_path) # Case 1: non-existing paths given, make dirs. emc2 = EMCOrientation( parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path="emc_tmp", run_files_path="emc_run", ) run, tmp = emc2._setupPaths() # Check. self.assertEqual(emc2.run_files_path, "emc_run") self.assertEqual(emc2.tmp_files_path, "emc_tmp") self.assertEqual(emc2.run_files_path, run) self.assertEqual(emc2.tmp_files_path, tmp) # Case 3: existing tmp path given, make dirs. emc3 = EMCOrientation( parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path="emc_tmp", run_files_path=None, ) run, tmp = emc3._setupPaths() # Check. self.assertTrue(os.path.dirname(emc3.run_files_path), "tmp") self.assertEqual(emc3.tmp_files_path, "emc_tmp") self.assertEqual(emc3.run_files_path, run) self.assertEqual(emc3.tmp_files_path, tmp) # Case 4: existing run path given, raise exception. self.assertRaises(IOError, EMCOrientation, parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path="emc_tmp", run_files_path=emc3.run_files_path)
def testPhotonFileConsecutiveRuns(self): """ Check that the photons.dat from the previous run is reused. """ self.__files_to_remove.append('orient_out.h5') self.__files_to_remove.append('orient_out2.h5') # Construct the object. emc_parameters = {"initial_number_of_quaternions" : 1, "max_number_of_quaternions" : 2, "max_number_of_iterations" : 2, "min_error" : 1.e-6, "beamstop" : True, "detailed_output" : True, } emc = EMCOrientation(parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path=None, run_files_path=None,) # Call backengine. status = emc.backengine() # Check paths exist and are populated. run_files_path = emc.run_files_path tmp_files_path = emc.tmp_files_path self.assertTrue( os.path.isdir( run_files_path ) ) self.assertTrue( os.path.isdir( tmp_files_path ) ) expected_run_files = ["finish_intensity.dat", "quaternion.dat", "detector.dat", "most_likely_orientations.dat", "start_intensity.dat", "EMC_extended.log", "mutual_info.dat", "EMC.log", "photons.dat", ] for ef in expected_run_files: self.assertIn( ef, os.listdir(run_files_path) ) # Second run. emc2 = EMCOrientation(parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out2.h5', tmp_files_path=emc.tmp_files_path, run_files_path=None) # Call backengine. status = emc2.backengine() # Check paths exist and are populated. run_files_path2 = emc2.run_files_path tmp_files_path2 = emc2.tmp_files_path self.assertNotEqual( run_files_path, run_files_path2 ) self.assertEqual( tmp_files_path, tmp_files_path2 ) expected_run_files2 = ["finish_intensity.dat", "quaternion.dat", "detector.dat", "most_likely_orientations.dat", "start_intensity.dat", "EMC_extended.log", "mutual_info.dat", "EMC.log", "photons.dat", ] for ef in expected_run_files2: self.assertIn( ef, os.listdir(run_files_path2) )
def testSetupPaths( self ): """ Check that setting up paths works correctly. """ # Clean up no matter how tests go. self.__dirs_to_remove.append("emc_run") self.__dirs_to_remove.append("emc_tmp") # Construct the object with very nonsensical iteration control parameters. emc_parameters = {"initial_number_of_quaternions" : 1, "max_number_of_quaternions" : 2, "max_number_of_iterations" : 2, "min_error" : 5.e-6, "beamstop" : True, "detailed_output" : True, } # Case 1: no paths given, make dirs in /tmp emc = EMCOrientation(parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path=None, run_files_path=None,) run, tmp = emc._setupPaths() # Check. self.assertTrue( os.path.isdir( emc.run_files_path ) ) self.assertTrue( os.path.isdir( emc.tmp_files_path ) ) self.assertEqual( run, emc.run_files_path ) self.assertEqual( tmp, emc.tmp_files_path ) # Case 1: non-existing paths given, make dirs. emc2 = EMCOrientation(parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path="emc_tmp", run_files_path="emc_run",) run, tmp = emc2._setupPaths() # Check. self.assertEqual( emc2.run_files_path, "emc_run" ) self.assertEqual( emc2.tmp_files_path, "emc_tmp" ) self.assertEqual( emc2.run_files_path, run ) self.assertEqual( emc2.tmp_files_path, tmp ) # Case 3: existing tmp path given, make dirs. emc3 = EMCOrientation(parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path="emc_tmp", run_files_path=None,) run, tmp = emc3._setupPaths() # Check. self.assertTrue( os.path.dirname( emc3.run_files_path ), "tmp" ) self.assertEqual( emc3.tmp_files_path, "emc_tmp" ) self.assertEqual( emc3.run_files_path, run ) self.assertEqual( emc3.tmp_files_path, tmp ) # Case 4: existing run path given, raise exception. self.assertRaises( IOError, EMCOrientation, parameters=emc_parameters, input_path=self.input_h5, output_path='orient_out.h5', tmp_files_path="emc_tmp", run_files_path=emc3.run_files_path )