def test_mosflm_refine_cell(dials_regression, tmpdir): tmpdir.chdir() templ, directory = get_template_and_directory(dials_regression) matrix = ''' -0.00728371 -0.00173706 -0.00994261 0.01008485 -0.00175152 -0.00708190 -0.00041078 -0.01220000 0.00243238 0.000 0.000 0.000 -0.5851825 -0.1395579 -0.7988023 0.8102298 -0.1407195 -0.5689691 -0.0330029 -0.9801641 0.1954206 78.6541 78.6541 78.6542 90.0000 90.0000 90.0000 0.000 0.000 0.000 SYMM I23 \n''' tmpdir.join('xiaindex.mat').write(matrix) refiner = MosflmRefineCell() refiner.set_images(((1,3), (21,23), (43,45))) refiner.set_input_mat_file("xiaindex.mat") refiner.set_output_mat_file("xiarefine.mat") refiner.set_directory(directory) refiner.set_template(templ) refiner.set_beam_centre(two_images_indexing['beam_centre']) refiner.set_mosaic(two_images_indexing['mosaicity']) refiner.run() output = ''.join(refiner.get_all_output()) print(output) background_residual = refiner.get_background_residual() rms_values = refiner.get_rms_values() ref_residual = { 1: {1: 0.1, 2: 0.1, 3: 0.1, 43: 0.1, 44: 0.2, 45: 0.1, 21: 0.1, 22: 0.1, 23: 0.1}, 2: {1: 0.1, 2: 0.1, 3: 0.1, 43: 0.1, 44: 0.2, 45: 0.1, 21: 0.1, 22: 0.1, 23: 0.1}, 3: {1: 0.1, 2: 0.1, 3: 0.1, 43: 0.1, 44: 0.2, 45: 0.1, 21: 0.1, 22: 0.1, 23: 0.1} } for cycle in background_residual: for frame in background_residual[cycle]: assert background_residual[cycle][frame] == \ pytest.approx(ref_residual[cycle][frame], abs=0.1) ref_values = { 1: [0.027, 0.029, 0.027, 0.025, 0.027, 0.025, 0.024, 0.022, 0.025], 2: [0.02, 0.021, 0.023, 0.021, 0.02, 0.017, 0.018, 0.019, 0.022], 3: [0.02, 0.021, 0.025, 0.022, 0.021, 0.019, 0.018, 0.019, 0.021] } for cycle in rms_values: for frame, value in enumerate(rms_values[cycle]): assert value == \ pytest.approx(ref_values[cycle][frame], abs=0.05)
def _mosflm_refine_cell(self, idxr, set_spacegroup=None): '''Perform the refinement of the unit cell. This will populate all of the information needed to perform the integration.''' # FIXME this will die after #1285 #if not self.get_integrater_indexer(): #Debug.write('Replacing indexer of %s with self at %d' % \ #(str(self.get_integrater_indexer()), __line__)) #self.set_integrater_indexer(self) #idxr = self.get_integrater_indexer() if not idxr.get_indexer_payload('mosflm_orientation_matrix'): raise RuntimeError('unexpected situation in indexing') lattice = idxr.get_indexer_lattice() mosaic = idxr.get_indexer_mosaic() cell = idxr.get_indexer_cell() beam_centre = idxr.get_indexer_beam_centre() # bug # 3174 - if mosaic is very small (here defined to be # 0.25 x osc_width) then set to this minimum value. phi_width = idxr.get_phi_width() if mosaic < 0.25 * phi_width: mosaic = 0.25 * phi_width if idxr.get_indexer_payload('mosflm_beam_centre'): beam_centre = idxr.get_indexer_payload('mosflm_beam_centre') distance = idxr.get_indexer_distance() matrix = idxr.get_indexer_payload('mosflm_orientation_matrix') integration_params = idxr.get_indexer_payload( 'mosflm_integration_parameters') if integration_params is None: integration_params = {} if integration_params: if 'separation' in integration_params: self.set_refiner_parameter( 'mosflm', 'separation', '%f %f' % tuple(integration_params['separation'])) if 'raster' in integration_params: self.set_refiner_parameter( 'mosflm', 'raster', '%d %d %d %d %d' % tuple(integration_params['raster'])) idxr.set_indexer_payload('mosflm_integration_parameters', None) spacegroup_number = lattice_to_spacegroup(lattice) # copy these into myself for later reference, if indexer # is not myself - everything else is copied via the # cell refinement process... from cctbx import sgtbx from dxtbx.model import Crystal from dxtbx.model.detector_helpers import set_mosflm_beam_centre experiment = idxr.get_indexer_experiment_list()[0] set_mosflm_beam_centre(experiment.detector, experiment.beam, beam_centre) space_group = sgtbx.space_group_info(number=spacegroup_number).group() a, b, c = experiment.crystal.get_real_space_vectors() experiment.crystal = Crystal(a, b, c, space_group=space_group) # FIXME surely these have been assigned further up?! if not self._mosflm_cell_ref_images: self._mosflm_cell_ref_images = self._refine_select_images(mosaic) f = open( os.path.join(self.get_working_directory(), 'xiaindex-%s.mat' % lattice), 'w') for m in matrix: f.write(m) f.close() # then start the cell refinement refiner = MosflmRefineCell() refiner.set_working_directory(self.get_working_directory()) auto_logfiler(refiner) if self._mosflm_gain: refiner.set_gain(self._mosflm_gain) refiner.set_template(os.path.basename(idxr.get_template())) refiner.set_directory(idxr.get_directory()) refiner.set_input_mat_file('xiaindex-%s.mat' % lattice) refiner.set_output_mat_file('xiarefine.mat') refiner.set_beam_centre(beam_centre) refiner.set_unit_cell(cell) refiner.set_distance(distance) if set_spacegroup: refiner.set_space_group_number(set_spacegroup) else: refiner.set_space_group_number(spacegroup_number) # FIXME 18/JUN/08 - it may help to have an overestimate # of the mosaic spread in here as it *may* refine down # better than up... - this is not a good idea as it may # also not refine at all! - 12972 # integration failed # Bug # 3103 if self._mosflm_cell_ref_double_mosaic: mosaic *= 2.0 refiner.set_mosaic(mosaic) # if set, use the resolution for cell refinement - see # bug # 2078... if self._mosflm_cell_ref_resolution: refiner.set_resolution(self._mosflm_cell_ref_resolution) refiner.set_fix_mosaic(self._mosflm_postref_fix_mosaic) # note well that the beam centre is coming from indexing so # should be already properly handled #if idxr.get_wavelength_prov() == 'user': #refiner.set_wavelength(idxr.get_wavelength()) # belt + braces mode - only to be used when considering failover, # will run an additional step of autoindexing prior to cell # refinement, to be used only after proving that not going it # will result in cell refinement failure - will use the first # wedge... N.B. this is only useful if the indexer is Labelit # not Mosflm... refiner.set_add_autoindex(self._mosflm_cell_ref_add_autoindex) # get all of the stored parameter values parameters = self.get_refiner_parameters('mosflm') refiner.update_parameters(parameters) detector = idxr.get_detector() detector_width, detector_height = detector[0].get_image_size_mm() lim_x = 0.5 * detector_width lim_y = 0.5 * detector_height Debug.write('Scanner limits: %.1f %.1f' % (lim_x, lim_y)) refiner.set_limits(lim_x, lim_y) refiner.set_images(self._mosflm_cell_ref_images) failover = PhilIndex.params.xia2.settings.failover if failover and not self._mosflm_cell_ref_add_autoindex: refiner.set_ignore_cell_refinement_failure(True) refiner.run() # then look to see if the cell refinement worked ok - if it # didn't then this may indicate that the lattice was wrongly # selected. cell_refinement_ok = refiner.cell_refinement_ok() if not cell_refinement_ok: Debug.write('Repeating cell refinement...') self.set_integrater_prepare_done(False) self._mosflm_cell_ref_add_autoindex = True return [0.0], [0.0] rms_values = refiner.get_rms_values() background_residual = refiner.get_background_residual() self._refinr_cell = refiner.get_refined_unit_cell() distance = refiner.get_refined_distance2() experiment = idxr.get_indexer_experiment_list()[0] from xia2.Wrappers.Mosflm.AutoindexHelpers import set_distance set_distance(experiment.detector, distance) self.set_refiner_parameter('mosflm', 'distortion yscale', refiner.get_refined_distortion_yscale()) self.set_refiner_parameter('mosflm', 'raster', refiner.get_raster()) #integration_params['distortion yscale'] \ #= refiner.get_refined_distortion_yscale() #integration_params['raster'] = refiner.get_raster() separation = refiner.get_separation() if separation is not None: self.set_refiner_parameter('mosflm', 'separation', '%s %s' % refiner.get_separation()) #integration_params['separation'] = refiner.get_separation() self.set_refiner_parameter('mosflm', 'beam', '%s %s' % refiner.get_refined_beam_centre()) self.set_refiner_parameter('mosflm', 'distance', refiner.get_refined_distance()) self.set_refiner_parameter('mosflm', 'distortion tilt', refiner.get_refined_distortion_tilt()) self.set_refiner_parameter('mosflm', 'distortion twist', refiner.get_refined_distortion_twist()) integration_params['beam'] = tuple( float(b) for b in refiner.get_refined_beam_centre()) integration_params['distance'] = refiner.get_refined_distance() integration_params[ 'distortion tilt'] = refiner.get_refined_distortion_tilt() integration_params[ 'distortion twist'] = refiner.get_refined_distortion_twist() idxr._indxr_mosaic = refiner.get_refined_mosaic() idxr.set_indexer_payload( 'mosflm_orientation_matrix', open(os.path.join(self.get_working_directory(), 'xiarefine.mat'), 'r').readlines()) self.set_refiner_payload( 'mosflm_orientation_matrix', idxr.get_indexer_payload('mosflm_orientation_matrix')) self.set_refiner_payload('mosaic', refiner.get_refined_mosaic()) self.set_refiner_payload('beam', integration_params['beam']) self.set_refiner_payload('distance', integration_params['distance']) from xia2.Wrappers.Mosflm.AutoindexHelpers import crystal_model_from_mosflm_mat # make a dxtbx crystal_model object from the mosflm matrix experiment = idxr.get_indexer_experiment_list()[0] crystal_model = crystal_model_from_mosflm_mat( idxr._indxr_payload['mosflm_orientation_matrix'], unit_cell=refiner.get_refined_unit_cell(), space_group=experiment.crystal.get_space_group()) experiment.crystal = crystal_model #self.set_refiner_payload( #'mosflm_integration_parameters', integration_params) self._refinr_refined_experiment_list = ExperimentList([experiment]) return rms_values, background_residual
def _mosflm_test_refine_cell(self, idxr, test_lattice): '''Test performing cell refinement in with a different lattice to the one which was selected by the autoindex procedure. This should not change anything in the class.''' #idxr = self.get_integrater_indexer() lattice = idxr.get_indexer_lattice() mosaic = idxr.get_indexer_mosaic() beam_centre = idxr.get_indexer_beam_centre() distance = idxr.get_indexer_distance() matrix = idxr.get_indexer_payload('mosflm_orientation_matrix') phi_width = idxr.get_phi_width() if mosaic < 0.25 * phi_width: mosaic = 0.25 * phi_width input_matrix = '' for m in matrix: input_matrix += '%s\n' % m new_matrix = transmogrify_matrix(lattice, input_matrix, test_lattice, idxr.get_wavelength(), self.get_working_directory()) spacegroup_number = lattice_to_spacegroup(test_lattice) if not self._mosflm_cell_ref_images: raise RuntimeError('wedges must be assigned already') open( os.path.join(self.get_working_directory(), 'test-xiaindex-%s.mat' % lattice), 'w').write(new_matrix) refiner = MosflmRefineCell() refiner.set_working_directory(self.get_working_directory()) auto_logfiler(refiner) if self._mosflm_gain: refiner.set_gain(self._mosflm_gain) refiner.set_template(os.path.basename(idxr.get_template())) refiner.set_directory(idxr.get_directory()) refiner.set_input_mat_file('test-xiaindex-%s.mat' % lattice) refiner.set_output_mat_file('test-xiarefine.mat') refiner.set_beam_centre(beam_centre) refiner.set_distance(distance) refiner.set_space_group_number(spacegroup_number) # FIXME 18/JUN/08 - it may help to have an overestimate # of the mosaic spread in here as it *may* refine down # better than up... - this is not a good idea as it may # also not refine at all! - 12972 # integration failed # Bug # 3103 if self._mosflm_cell_ref_double_mosaic: mosaic *= 2.0 refiner.set_mosaic(mosaic) # if set, use the resolution for cell refinement - see # bug # 2078... if self._mosflm_cell_ref_resolution: refiner.set_resolution(self._mosflm_cell_ref_resolution) refiner.set_fix_mosaic(self._mosflm_postref_fix_mosaic) # note well that the beam centre is coming from indexing so # should be already properly handled #if idxr.get_wavelength_prov() == 'user': #refiner.set_wavelength(idxr.get_wavelength()) # belt + braces mode - only to be used when considering failover, # will run an additional step of autoindexing prior to cell # refinement, to be used only after proving that not going it # will result in cell refinement failure - will use the first # wedge... N.B. this is only useful if the indexer is Labelit # not Mosflm... refiner.set_add_autoindex(self._mosflm_cell_ref_add_autoindex) # XXX # get all of the stored parameter values parameters = self.get_refiner_parameters('mosflm') refiner.update_parameters(parameters) detector = idxr.get_detector() detector_width, detector_height = detector[0].get_image_size_mm() lim_x = 0.5 * detector_width lim_y = 0.5 * detector_height Debug.write('Scanner limits: %.1f %.1f' % (lim_x, lim_y)) refiner.set_limits(lim_x, lim_y) refiner.set_images(self._mosflm_cell_ref_images) refiner.run() rms_values = refiner.get_rms_values() background_residual = refiner.get_background_residual() return rms_values, background_residual
def exercise_mosflm_refine_cell(): if not have_dials_regression: print "Skipping exercise_mosflm_refine_cell(): dials_regression not configured" return xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data") template = os.path.join(xia2_demo_data, "insulin_1_%03i.img") from xia2.Wrappers.Mosflm.MosflmIndex import MosflmIndex from xia2.Wrappers.Mosflm.MosflmRefineCell import MosflmRefineCell # exercise basic indexing from two images cwd = os.path.abspath(os.curdir) tmp_dir = open_tmp_directory() os.chdir(tmp_dir) from xia2.Experts.FindImages import image2template_directory templ, directory = image2template_directory(template %1) indexer = MosflmIndex() indexer.set_images((1,45)) indexer.set_directory(directory) indexer.set_template(templ) indexer.run() refiner = MosflmRefineCell() refiner.set_images(((1,3),(21,23), (43,45))) refiner.set_input_mat_file("xiaindex.mat") refiner.set_output_mat_file("xiarefine.mat") refiner.set_directory(directory) refiner.set_template(templ) refiner.set_beam_centre(indexer.get_refined_beam_centre()) refiner.set_mosaic( sum(indexer.get_mosaic_spreads())/len(indexer.get_mosaic_spreads())) refiner.run() output = ''.join(refiner.get_all_output()) print output background_residual = refiner.get_background_residual() rms_values = refiner.get_rms_values() ref_residual = { 1: {1: 0.1, 2: 0.1, 3: 0.1, 43: 0.1, 44: 0.2, 45: 0.1, 21: 0.1, 22: 0.1, 23: 0.1}, 2: {1: 0.1, 2: 0.1, 3: 0.1, 43: 0.1, 44: 0.2, 45: 0.1, 21: 0.1, 22: 0.1, 23: 0.1}, 3: {1: 0.1, 2: 0.1, 3: 0.1, 43: 0.1, 44: 0.2, 45: 0.1, 21: 0.1, 22: 0.1, 23: 0.1} } for cycle in background_residual: for frame in background_residual[cycle]: assert abs(background_residual[cycle][frame] - ref_residual[cycle][frame]) <= 0.1, (cycle, frame) ref_values = { 1: [0.027, 0.029, 0.027, 0.025, 0.027, 0.025, 0.024, 0.022, 0.025], 2: [0.02, 0.021, 0.023, 0.021, 0.02, 0.017, 0.018, 0.019, 0.022], 3: [0.02, 0.021, 0.025, 0.022, 0.021, 0.019, 0.018, 0.019, 0.021] } for cycle in rms_values: for frame in range(len(rms_values[cycle])): assert abs(rms_values[cycle][frame] - ref_values[cycle][frame]) <= 0.05
def exercise_mosflm_integrate(): if not have_dials_regression: print "Skipping exercise_mosflm_integrate(): dials_regression not configured" return xia2_demo_data = os.path.join(dials_regression, "xia2_demo_data") template = os.path.join(xia2_demo_data, "insulin_1_%03i.img") from xia2.Wrappers.Mosflm.MosflmIndex import MosflmIndex from xia2.Wrappers.Mosflm.MosflmRefineCell import MosflmRefineCell from xia2.Wrappers.Mosflm.MosflmIntegrate import MosflmIntegrate # exercise basic indexing from two images cwd = os.path.abspath(os.curdir) tmp_dir = open_tmp_directory() os.chdir(tmp_dir) from xia2.Experts.FindImages import image2template_directory templ, directory = image2template_directory(template %1) indexer = MosflmIndex() indexer.set_images((1,45)) indexer.set_directory(directory) indexer.set_template(templ) indexer.run() refiner = MosflmRefineCell() refiner.set_images(((1,3),(21,23), (43,45))) refiner.set_input_mat_file("xiaindex.mat") refiner.set_output_mat_file("xiarefine.mat") refiner.set_directory(directory) refiner.set_template(templ) refiner.set_beam_centre(indexer.get_refined_beam_centre()) refiner.set_mosaic( sum(indexer.get_mosaic_spreads())/len(indexer.get_mosaic_spreads())) refiner.run() #output = ''.join(refiner.get_all_output()) #print output integrater = MosflmIntegrate() integrater.set_image_range((1,45)) integrater.set_input_mat_file("xiaindex.mat") #integrater.set_output_mat_file("xiarefine.mat") integrater.set_directory(directory) integrater.set_template(templ) integrater.set_beam_centre( tuple(float(x) for x in refiner.get_refined_beam_centre())) integrater.set_distance(refiner.get_refined_distance()) integrater.set_mosaic(refiner.get_refined_mosaic()) integrater.set_space_group_number(197) integrater.set_unit_cell(refiner.get_refined_unit_cell()) integrater.run() hklout = integrater.get_hklout() assert os.path.exists(hklout) from iotbx.reflection_file_reader import any_reflection_file miller_arrays = any_reflection_file(hklout).as_miller_arrays( merge_equivalents=False) for ma in miller_arrays: assert ma.size() == 81011, ma.size() assert len(miller_arrays) == 13, len(miller_arrays) assert not integrater.get_bgsig_too_large() assert not integrater.get_getprof_error() assert integrater.get_batches_out() == (1, 45) assert integrater.get_mosaic_spreads() == [ 0.43, 0.42, 0.42, 0.41, 0.41, 0.41, 0.42, 0.42, 0.42, 0.42, 0.42, 0.42, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.41, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.39, 0.39, 0.39, 0.39, 0.39] assert integrater.get_nref() == 81011 assert len(integrater.get_postref_result()) == 45 assert integrater.get_postref_result()[1] == { 'ovrl': 0.0, 'full': 507.0, 'dist': 158.6, 'ccx': -0.01, 'yscale': 1.0, 'sdrat': 7.5, 'tilt': 25.0, 'rsym': 0.027, 'bad': 0.0, 'i/sigi': 18.1, 'i/sigi_out': 1.6, 'twist': 13.0, 'resid': 0.021, 'wresid': 1.1, 'part': 1309.0, 'nsym': 18.0, 'neg': 158.0, 'ccy': -0.01, 'ccom': -0.01, 'toff': 0.0, 'roff': 0.0} assert integrater.get_residuals() == [ 1.1, 0.9, 1.0, 1.0, 0.8, 0.9, 1.0, 0.8, 0.9, 0.9, 0.9, 0.9, 1.0, 1.0, 1.0, 0.9, 0.9, 0.9, 0.9, 0.8, 1.0, 0.9, 0.8, 0.9, 1.0, 0.8, 1.0, 0.9, 0.8, 0.8, 0.9, 0.9, 0.9, 0.9, 0.9, 1.0, 0.8, 0.9, 1.0, 0.7, 0.8, 0.9, 0.8, 0.9, 1.0] assert integrater.get_spot_status() \ == 'ooooooooooooooooooooooooooooooooooooooooooooo'