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") )
def backengine(self): """ This method drives the backengine CrystFEL.pattern_sim.""" # pattern_sim backengine does not support MPI. return self._run() # collect MPI arguments if self.parameters.forced_mpi_command == "": np = self.computeNTasks() mpicommand = ParallelUtilities.prepareMPICommandArguments(np) else: mpicommand = self.parameters.forced_mpi_command # Dump to a temporary file. fname = IOUtilities.getTmpFileName() self.dumpToFile(fname) mpicommand += " ".join([" ", sys.executable, __file__, fname]) args = shlex.split(mpicommand) if 'SIMEX_VERBOSE' in os.environ and os.environ[ 'SIMEX_VERBOSE'] == 'MPI': print("MPI command: " + mpicommand) try: proc = subprocess.Popen(args, universal_newlines=True) proc.wait() except: raise finally: os.remove(fname) return proc.returncode
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") )
def backengine(self): """ Starts WPG simulations in parallel in a subprocess """ fname = IOUtilities.getTmpFileName() self.dumpToFile(fname) forcedMPIcommand = self.parameters.forced_mpi_command if forcedMPIcommand == "": (np, ncores) = self.computeNTasks() mpicommand = ParallelUtilities.prepareMPICommandArguments( np, ncores) else: mpicommand = forcedMPIcommand if 'SIMEX_VERBOSE' in os.environ: if 'MPI' in os.environ['SIMEX_VERBOSE']: print(("WavePropagator backengine mpicommand: " + mpicommand)) if 'PYTHON' in os.environ['SIMEX_VERBOSE']: import platform print("Running python %s." % (platform.python_version())) mpicommand += " python " + __file__ + " " + fname args = shlex.split(mpicommand) proc = subprocess.Popen(args, universal_newlines=True) proc.wait() os.remove(fname) return proc.returncode
def sample(self, val): """ Set the 'sample' parameter to val.""" if val is None: raise ValueError( "A sample must be defined.") if val.split(".")[-1] == "pdb": print("Checking presence of %s. Will query from PDB if not found in $PWD." % (val)) self.__sample = IOUtilities.checkAndGetPDB(val) print("Sample path is set to %s." % (self.__sample))
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)
def sample(self, val): """ Set the 'sample' parameter to val.""" if val is None: self.__sample = None return if val.split(".")[-1] == "pdb": self.__sample = IOUtilities.checkAndGetPDB(val) return raise IOError("Samples must be in pdb format.")
def testQueryPDBTwice(self): """ Check that we can do two subsequent queries (fails if urllib.urlcleanup is not called.) """ # Setup path to pdb file. pdb_path = '5loy.pdb' self.__files_to_remove.append(pdb_path) self.__paths_to_remove.append('obsolete') # Attempt to load it. pdb_path = IOUtilities.checkAndGetPDB(pdb_path) # Check it's there. self.assertTrue( os.path.isfile( pdb_path ) ) # Remove it. os.remove(pdb_path) # Query again. pdb_path = IOUtilities.checkAndGetPDB(pdb_path) # Check it's there. self.assertTrue( os.path.isfile( pdb_path ) )
def testQueryPDB(self): """ Check that we can query a non-existing pdb from pdb.org and convert it to a dict. """ # Setup path to pdb file. pdb_path = '5UDC.pdb' self.__files_to_remove.append(pdb_path) self.__paths_to_remove.append('obsolete') # Attempt to load it. pdb_path = IOUtilities.checkAndGetPDB(pdb_path) # Check it's there. self.assertTrue( os.path.isfile( pdb_path ) )
def backengine(self): """ Starts EMC simulations in parallel in a subprocess """ # Set paths. self._setupPaths() fname = IOUtilities.getTmpFileName() self.dumpToFile(fname) # collect MPI arguments if self.parameters.forced_mpi_command == "": np = self.computeNTasks() #mpicommand=ParallelUtilities.prepareMPICommandArguments(np) mpicommand = "" else: mpicommand = self.parameters.forced_mpi_command # collect program arguments command_sequence = [ 'python', __file__, fname, ] # put MPI and program arguments together args = shlex.split(mpicommand) + command_sequence print(' '.join(args)) if 'SIMEX_VERBOSE' in os.environ: if 'MPI' in os.environ['SIMEX_VERBOSE']: print(("EMCOrientation backengine mpicommand: " + mpicommand)) print(' '.join(args)) if 'PYTHON' in os.environ['SIMEX_VERBOSE']: import platform print("Running python version %s." % (platform.python_version())) # Run the backengine command. proc = subprocess.Popen(args) proc.wait() os.remove(fname) # Return the return code from the backengine. return proc.returncode
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) )
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) )
def _backengineWithPdb(self): """ """ """ Run the diffraction simulation if the sample is a pdb. Codes is based on pysingfel/tests/test_particle.test_calFromPDB """ # Dump self to file. fname = IOUtilities.getTmpFileName() self.dumpToFile(fname) # Setup the mpi call. forcedMPIcommand = self.parameters.forced_mpi_command if forcedMPIcommand == "" or forcedMPIcommand is None: (np, ncores) = self.computeNTasks() mpicommand = ParallelUtilities.prepareMPICommandArguments( np, ncores) else: mpicommand = forcedMPIcommand mpicommand += " ".join(("", sys.executable, __file__, fname)) if 'SIMEX_VERBOSE' in os.environ: if 'MPI' in os.environ['SIMEX_VERBOSE']: print(("SingFELPhotonDiffractor backengine mpicommand: " + mpicommand)) # Launch the system command. args = shlex.split(mpicommand) with subprocess.Popen(args, universal_newlines=True) as proc: out, err = proc.communicate() # Remove the dumped class. os.remove(fname) return proc.returncode
def notestQueryPDB(self): """ Check that we can query a non-existing pdb from pdb.org and convert it to a dict. """ # Setup path to pdb file. pdb_path = '2nip.pdb' self.__files_to_remove.append(pdb_path) 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, 'xyz.pdb' )
def testQueryPDBDirectory(self): """ Check that if the pdb does not exist in the local database, it is queried from pdb.org, save in a dir, and convert it to a dict. """ # Setup path to pdb file. pdb_path = 'pdb/2nip.pdb' self.__files_to_remove.append(pdb_path) self.__paths_to_remove.append('obsolete') self.__paths_to_remove.append('pdb') os.mkdir('pdb') # 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 )
def sample(self, val): """ Set the 'sample' parameter to val.""" if val is None: raise ValueError( "A sample must be defined.") if val.split(".")[-1] == "pdb": self.__sample = IOUtilities.checkAndGetPDB(val)
def backengine(self): """ This method drives the backengine singFEL.""" uniform_rotation = self.parameters.uniform_rotation calculate_Compton = int(self.parameters.calculate_Compton) slice_interval = self.parameters.slice_interval number_of_slices = self.parameters.number_of_slices pmi_start_ID = self.parameters.pmi_start_ID pmi_stop_ID = self.parameters.pmi_stop_ID number_of_diffraction_patterns = self.parameters.number_of_diffraction_patterns if not os.path.isdir(self.output_path): os.mkdir(self.output_path) self.__output_dir = self.output_path # If the sample is passed as a pdb, branch out to separate backengine implementation. if self.input_path.split(".")[-1].lower() == 'pdb': if not os.path.isfile(self.input_path): # Attempt to query from pdb. self.input_path = IOUtilities.checkAndGetPDB(self.input_path) return self._backengineWithPdb() # Ok, not a pdb, proceed. # Serialize the geometry file. beam_geometry_file = "tmp.geom" self.parameters.detector_geometry.serialize(beam_geometry_file) # Setup directory to pmi output. # Backengine expects a directory name, so have to check if # input_path is dir or file and handle accordingly. if os.path.isdir(self.input_path): input_dir = self.input_path elif os.path.isfile(self.input_path): input_dir = os.path.dirname(self.input_path) config_file = '/dev/null' # collect MPI arguments if self.parameters.forced_mpi_command == "": np, ncores = self.computeNTasks() mpicommand = ParallelUtilities.prepareMPICommandArguments(np,1) else: mpicommand = self.parameters.forced_mpi_command # collect program arguments command_sequence = ['radiationDamageMPI', '--inputDir', str(input_dir), '--outputDir', str(self.__output_dir), '--geomFile', str(beam_geometry_file), '--configFile', str(config_file), '--uniformRotation', str(uniform_rotation), '--calculateCompton', str(calculate_Compton), '--sliceInterval', str(slice_interval), '--numSlices', str(number_of_slices), '--pmiStartID', str(pmi_start_ID), '--pmiEndID', str(pmi_stop_ID), '--numDP', str(number_of_diffraction_patterns), ] if self.parameters.beam_parameters is not None: beam_parameter_file = "tmp.beam" self.parameters.beam_parameters.serialize(beam_parameter_file) command_sequence.append('--beamFile') command_sequence.append(str(beam_parameter_file)) # put MPI and program arguments together args = shlex.split(mpicommand) + command_sequence if 'SIMEX_VERBOSE' in os.environ: print(("SingFELPhotonDiffractor backengine command: " + " ".join(args)),flush=True) # Run the backengine command. proc = subprocess.Popen(args) proc.wait() # Return the return code from the backengine. return proc.returncode
def backengine(self): """ This method drives the backengine code.""" status = 0 input_files = [] if os.path.isfile(self.input_path): input_files = [self.input_path] elif os.path.isdir(self.input_path): input_files = [ os.path.join( self.input_path, f) for f in os.listdir( self.input_path ) if f.split('.')[-1] == 'h5' and f.split('.')[-2] != 'opmd' ] input_files.sort() else: raise IOError("Input file %s does not exist or cannot be read." % (self.input_path) ) # Create output directory if not existing yet. if not os.path.isdir( self.output_path ): os.mkdir( self.output_path ) elif os.path.isfile( self.output_path ): raise IOError( "Output file %s already exists, cowardly refusing to overwrite." % (self.output_path) ) # Generate formatted output files (i.e. attach history to output file). for i,input_file in enumerate(input_files): tail = input_file.split( 'prop' )[-1] output_file = os.path.join( self.output_path , 'pmi_out_%07d.h5' % (i+1) ) pmi_script.f_h5_out2in( input_file, output_file) # Get the backengine calculator. pmi_demo = PMIDemo() # Transfer some parameters. pmi_demo.g_s2e['prj'] = '' pmi_demo.g_s2e['id'] = tail.split('_')[-1].split('.')[0] pmi_demo.g_s2e['prop_out'] = input_file pmi_demo.g_s2e['setup'] = dict() pmi_demo.g_s2e['sys'] = dict() pmi_demo.g_s2e['setup']['num_digits'] = 7 if 'number_of_steps' in self.parameters.keys(): pmi_demo.g_s2e['steps'] = self.parameters['number_of_steps'] else: pmi_demo.g_s2e['steps'] = 100 pmi_demo.g_s2e['maxZ'] = 100 pmi_demo.g_s2e['setup']['pmi_out'] = output_file # Setup the database. pmi_demo.f_dbase_setup() # Go through the pmi workflow. pmi_demo.f_init_random() pmi_demo.f_save_info() pmi_demo.f_load_pulse( pmi_demo.g_s2e['prop_out'] ) # Check if sample is a h5 file. Horrible hack. try: h5 = h5py.File(self.__sample_path) h5.close() pmi_demo.f_load_sample(self.__sample_path) except: # Assume it's a pdb file. Will raise if not. atoms_dict = IOUtilities.loadPDB(self.__sample_path) pmi_demo.g_s2e['sample'] = atoms_dict pmi_demo.f_rotate_sample() pmi_demo.f_system_setup() # Perform the trajectories for this pulse and orientation. for traj in range( self.parameters['number_of_trajectories'] ): pmi_demo.f_time_evolution() return status