def exercise_map_to_asu(sg_symbol): sg_type = sgtbx.space_group_type(sg_symbol) index_abs_range = (4,4,4) for anomalous_flag in (False,True): m = miller.index_generator( sg_type, anomalous_flag, index_abs_range).to_array() a = flex.double() p = flex.double() c = flex.hendrickson_lattman() for i in range(m.size()): a.append(random.random()) p.append(random.random() * 2) c.append([random.random() for j in range(4)]) f = flex.polar(a, p) p = [p, p*(180/math.pi)] m_random = flex.miller_index() p_random = [flex.double(), flex.double()] c_random = flex.hendrickson_lattman() f_random = flex.complex_double() for i,h_asym in enumerate(m): h_eq = miller.sym_equiv_indices(sg_type.group(), h_asym) i_eq = random.randrange(h_eq.multiplicity(anomalous_flag)) h_i = h_eq(i_eq) m_random.append(h_i.h()) for deg in (False,True): p_random[deg].append(h_i.phase_eq(p[deg][i], deg)) f_random.append(h_i.complex_eq(f[i])) c_random.append(h_i.hendrickson_lattman_eq(c[i])) m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, f_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,f_asym in enumerate(f): assert abs(f_asym - f_random[i]) < 1.e-6 m_random_copy = m_random.deep_copy() a_random = a.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, a_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,a_asym in enumerate(a): assert a_asym == a_random[i] for deg in (False,True): m_random_copy = m_random.deep_copy() miller.map_to_asu( sg_type, anomalous_flag, m_random_copy, p_random[deg], deg) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,p_asym in enumerate(p[deg]): assert scitbx.math.phase_error(p_asym, p_random[deg][i], deg) < 1.e-5 m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, c_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,c_asym in enumerate(c): for j in range(4): assert abs(c_asym[j] - c_random[i][j]) < 1.e-5
def exercise_map_to_asu(sg_symbol): sg_type = sgtbx.space_group_type(sg_symbol) index_abs_range = (4,4,4) for anomalous_flag in (False,True): m = miller.index_generator( sg_type, anomalous_flag, index_abs_range).to_array() a = flex.double() p = flex.double() c = flex.hendrickson_lattman() for i in xrange(m.size()): a.append(random.random()) p.append(random.random() * 2) c.append([random.random() for j in xrange(4)]) f = flex.polar(a, p) p = [p, p*(180/math.pi)] m_random = flex.miller_index() p_random = [flex.double(), flex.double()] c_random = flex.hendrickson_lattman() f_random = flex.complex_double() for i,h_asym in enumerate(m): h_eq = miller.sym_equiv_indices(sg_type.group(), h_asym) i_eq = random.randrange(h_eq.multiplicity(anomalous_flag)) h_i = h_eq(i_eq) m_random.append(h_i.h()) for deg in (False,True): p_random[deg].append(h_i.phase_eq(p[deg][i], deg)) f_random.append(h_i.complex_eq(f[i])) c_random.append(h_i.hendrickson_lattman_eq(c[i])) m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, f_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,f_asym in enumerate(f): assert abs(f_asym - f_random[i]) < 1.e-6 m_random_copy = m_random.deep_copy() a_random = a.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, a_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,a_asym in enumerate(a): assert a_asym == a_random[i] for deg in (False,True): m_random_copy = m_random.deep_copy() miller.map_to_asu( sg_type, anomalous_flag, m_random_copy, p_random[deg], deg) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,p_asym in enumerate(p[deg]): assert scitbx.math.phase_error(p_asym, p_random[deg][i], deg) < 1.e-5 m_random_copy = m_random.deep_copy() miller.map_to_asu(sg_type, anomalous_flag, m_random_copy, c_random) for i,h_asym in enumerate(m): assert h_asym == m_random_copy[i] for i,c_asym in enumerate(c): for j in xrange(4): assert abs(c_asym[j] - c_random[i][j]) < 1.e-5
def join_hl_group(self, group_index=None): if (group_index is None): assert len(self.groups) == 1 group_index = 0 selected_group = self.groups[group_index] assert len(selected_group) == 4 names = [] miller_indices = 0 rsos = [] matches = [] for name in selected_group: names.append(name) rso = self.reciprocal_space_objects[name] assert rso.type == "real" rsos.append(rso) if (type(miller_indices) == type(0)): miller_indices = rso.indices match = miller.match_indices(miller_indices, rso.indices) assert not match.have_singles() matches.append(match) hl = flex.hendrickson_lattman() for ih in range(miller_indices.size()): coeff = [] for ic in range(4): ih0, ih1 = matches[ic].pairs()[ih] assert ih0 == ih coeff.append(rsos[ic].data[ih1]) hl.append(coeff) return names, miller_indices, hl
def exercise_get_experimental_phases(): crystal_symmetry = crystal.symmetry(unit_cell=(30, 31, 32, 85, 95, 100), space_group_symbol="P 1") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_array = miller_set.array( data=flex.hendrickson_lattman(miller_set.indices().size(), (0, 0, 0, 0))) mtz_dataset = input_array.as_mtz_dataset(column_root_label="P") mtz_dataset.mtz_object().write("tmp.mtz") reflection_files = [ reflection_file_reader.any_reflection_file(file_name="tmp.mtz") ] err = StringIO() reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=err) experimental_phases = reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=False, parameter_scope="experimental_phases") assert str(experimental_phases.info()) == "tmp.mtz:PA,PB,PC,PD" try: reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=True, parameter_scope="experimental_phases") except Sorry, e: assert str(e) == "No array of experimental phases found." assert err.getvalue() == """\
def exercise_mmcif_structure_factors(): miller_arrays = cif.reader(input_string=r3adrsf).as_miller_arrays() assert len(miller_arrays) == 16 hl_coeffs = find_miller_array_from_labels( miller_arrays, ','.join([ 'scale_group_code=1', 'crystal_id=2', 'wavelength_id=3', '_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'])) assert hl_coeffs.is_hendrickson_lattman_array() assert hl_coeffs.size() == 2 mas_as_cif_block = cif.miller_arrays_as_cif_block( hl_coeffs, column_names=('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso')) abcd = [] for key in ('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'): assert key in mas_as_cif_block.cif_block.keys() abcd.append(flex.double(mas_as_cif_block.cif_block[key])) hl_coeffs_from_cif_block = flex.hendrickson_lattman(*abcd) assert approx_equal(hl_coeffs.data(), hl_coeffs_from_cif_block) f_meas_au = find_miller_array_from_labels( miller_arrays, ','.join([ 'scale_group_code=1', 'crystal_id=1', 'wavelength_id=1', '_refln.F_meas_au', '_refln.F_meas_sigma_au'])) assert f_meas_au.is_xray_amplitude_array() assert f_meas_au.size() == 5 assert f_meas_au.sigmas() is not None assert f_meas_au.space_group_info().symbol_and_number() == 'C 1 2 1 (No. 5)' assert approx_equal(f_meas_au.unit_cell().parameters(), (163.97, 45.23, 110.89, 90.0, 131.64, 90.0)) pdbx_I_plus_minus = find_miller_array_from_labels( miller_arrays, ','.join( ['_refln.pdbx_I_plus', '_refln.pdbx_I_plus_sigma', '_refln.pdbx_I_minus', '_refln.pdbx_I_minus_sigma'])) assert pdbx_I_plus_minus.is_xray_intensity_array() assert pdbx_I_plus_minus.anomalous_flag() assert pdbx_I_plus_minus.size() == 21 assert pdbx_I_plus_minus.unit_cell() is None # no symmetry information in assert pdbx_I_plus_minus.space_group() is None # this CIF block # miller_arrays = cif.reader(input_string=r3ad7sf).as_miller_arrays() assert len(miller_arrays) == 11 f_calc = find_miller_array_from_labels( miller_arrays, ','.join([ 'crystal_id=2', 'wavelength_id=1', '_refln.F_calc', '_refln.phase_calc'])) assert f_calc.is_complex_array() assert f_calc.size() == 4 # miller_arrays = cif.reader(input_string=integer_observations).as_miller_arrays() assert len(miller_arrays) == 2 assert isinstance(miller_arrays[0].data(), flex.double) assert isinstance(miller_arrays[0].sigmas(), flex.double) # miller_arrays = cif.reader(input_string=r3v56sf).as_miller_arrays() assert len(miller_arrays) == 2 for ma in miller_arrays: assert ma.is_complex_array() assert miller_arrays[0].info().labels == [ 'r3v56sf', '_refln.pdbx_DELFWT', '_refln.pdbx_DELPHWT'] assert miller_arrays[1].info().labels == [ 'r3v56sf', '_refln.pdbx_FWT', '_refln.pdbx_PHWT']
def exercise_expand(): sg = sgtbx.space_group("P 41 (1,-1,0)") h = flex.miller_index(((3,1,-2), (1,-2,0))) assert tuple(sg.is_centric(h)) == (0, 1) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=False, indices=h, build_iselection=False) p1_i0 = ((-3,-1,2), (-1, 3,2),(3,1,2),(1,-3,2),(1,-2, 0),(2,1,0)) assert tuple(p1.indices) == p1_i0 assert p1.iselection.size() == 0 p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=True, indices=h, build_iselection=False) assert tuple(p1.indices) \ == ((3,1,-2), (1,-3,-2), (-3,-1,-2), (-1,3,-2), (1,-2,0), (-2,-1,0), (-1,2,0), (2,1,0)) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=False, indices=h, build_iselection=True) assert tuple(p1.indices) == p1_i0 assert tuple(p1.iselection) == (0,0,0,0,1,1) a = flex.double((1,2)) p = flex.double((10,90)) p1 = miller.expand_to_p1_phases( space_group=sg, anomalous_flag=False, indices=h, data=p, deg=True) assert approx_equal(tuple(p1.data), (-10,110,110,-10, 90,30)) p1 = miller.expand_to_p1_phases( space_group=sg, anomalous_flag=True, indices=h, data=p, deg=True) assert approx_equal(tuple(p1.data), (10,-110,-110,10, 90,-30,-90,30)) p = flex.double([x * math.pi/180 for x in p]) v = [x * math.pi/180 for x in p1.data] p1 = miller.expand_to_p1_phases( space_group=sg, anomalous_flag=True, indices=h, data=p, deg=False) assert approx_equal(tuple(p1.data), v) f = flex.polar(a, p) p1 = miller.expand_to_p1_complex( space_group=sg, anomalous_flag=True, indices=h, data=f) assert approx_equal(tuple(flex.abs(p1.data)), (1,1,1,1,2,2,2,2)) assert approx_equal(tuple(flex.arg(p1.data)), v) hl = flex.hendrickson_lattman([(1,2,3,4), (5,6,7,8)]) p1 = miller.expand_to_p1_hendrickson_lattman( space_group=sg, anomalous_flag=True, indices=h, data=hl) assert approx_equal(p1.data, [ [1,2,3,4], [1.232051,-1.866025,-4.964102,0.5980762], [1.232051,-1.866025,-4.964102,0.5980762], [1,2,3,4], [5,6,7,8], [2.696152,-7.330127,-10.4282,2.062178], [-5,-6,7,8], [7.696152,-1.330127,3.428203,-10.06218]]) b = flex.bool([True,False]) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=True, indices=h, build_iselection=True) assert b.select(p1.iselection).all_eq( flex.bool([True, True, True, True, False, False, False, False])) i = flex.int([13,17]) p1 = miller.expand_to_p1_iselection( space_group=sg, anomalous_flag=True, indices=h, build_iselection=True) assert i.select(p1.iselection).all_eq(flex.int([13,13,13,13,17,17,17,17])) # assert approx_equal(miller.statistical_mean(sg, False, h, a), 4/3.) assert approx_equal(miller.statistical_mean(sg, True, h, a), 3/2.)
def exercise_get_experimental_phases(): crystal_symmetry = crystal.symmetry( unit_cell=(30,31,32,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_array = miller_set.array( data=flex.hendrickson_lattman(miller_set.indices().size(), (0,0,0,0))) mtz_dataset = input_array.as_mtz_dataset(column_root_label="P") mtz_dataset.mtz_object().write("tmp.mtz") reflection_files = [reflection_file_reader.any_reflection_file( file_name="tmp.mtz")] err = StringIO() reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=err) experimental_phases = reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=False, parameter_scope="experimental_phases") assert str(experimental_phases.info()) == "tmp.mtz:PA,PB,PC,PD" try: reflection_file_srv.get_experimental_phases( file_name=None, labels=None, ignore_all_zeros=True, parameter_scope="experimental_phases") except Sorry, e: assert str(e) == "No array of experimental phases found." assert err.getvalue() == """\
def join_hl_group(self, group_index=None): if (group_index is None): assert len(self.groups) == 1 group_index = 0 selected_group = self.groups[group_index] assert len(selected_group) == 4 names = [] miller_indices = 0 rsos = [] matches = [] for name in selected_group: names.append(name) rso = self.reciprocal_space_objects[name] assert rso.type == "real" rsos.append(rso) if (type(miller_indices) == type(0)): miller_indices = rso.indices match = miller.match_indices(miller_indices, rso.indices) assert not match.have_singles() matches.append(match) hl = flex.hendrickson_lattman() for ih in xrange(miller_indices.size()): coeff = [] for ic in xrange(4): ih0, ih1 = matches[ic].pairs()[ih] assert ih0 == ih coeff.append(rsos[ic].data[ih1]) hl.append(coeff) return names, miller_indices, hl
def get_hl(f_obs_cmpl, k_blur, b_blur): f_model_phases = f_obs_cmpl.phases().data() sin_f_model_phases = flex.sin(f_model_phases) cos_f_model_phases = flex.cos(f_model_phases) ss = 1. / flex.pow2(f_obs_cmpl.d_spacings().data()) / 4. t = 2 * k_blur * flex.exp(-b_blur * ss) hl_a_model = t * cos_f_model_phases hl_b_model = t * sin_f_model_phases hl_data = flex.hendrickson_lattman(a=hl_a_model, b=hl_b_model) hl = f_obs_cmpl.customized_copy(data=hl_data) return hl
def get_hl(f_obs_cmpl, k_blur, b_blur): f_model_phases = f_obs_cmpl.phases().data() sin_f_model_phases = flex.sin(f_model_phases) cos_f_model_phases = flex.cos(f_model_phases) ss = 1./flex.pow2(f_obs_cmpl.d_spacings().data()) / 4. t = 2*k_blur * flex.exp(-b_blur*ss) hl_a_model = t * cos_f_model_phases hl_b_model = t * sin_f_model_phases hl_data = flex.hendrickson_lattman(a = hl_a_model, b = hl_b_model) hl = f_obs_cmpl.customized_copy(data = hl_data) return hl
def exercise_asu(): sg_type = sgtbx.space_group_type("P 41") asu = sgtbx.reciprocal_space_asu(sg_type) miller_indices = flex.miller_index(((1, 2, 3), (3, 5, 0))) for h in miller_indices: h_eq = miller.sym_equiv_indices(sg_type.group(), h) for i_eq in xrange(h_eq.multiplicity(False)): h_i = h_eq(i_eq) for anomalous_flag in (False, True): a = miller.asym_index(sg_type.group(), asu, h_i.h()) assert a.h() == h o = a.one_column(anomalous_flag) assert o.i_column() == 0 t = a.two_column(anomalous_flag) assert t.h() == h assert (o.h() != h) == (t.i_column() == 1) assert not anomalous_flag or (t.i_column() != 0) == h_i.friedel_flag() assert anomalous_flag or t.i_column() == 0 miller.map_to_asu(sg_type, False, miller_indices) data = flex.double((0, 0)) miller.map_to_asu(sg_type, False, miller_indices, data) miller.map_to_asu(sg_type, False, miller_indices, data, False) miller.map_to_asu(sg_type, False, miller_indices, data, True) data = flex.complex_double((0, 0)) miller.map_to_asu(sg_type, False, miller_indices, data) data = flex.hendrickson_lattman(((1, 2, 3, 4), (2, 3, 4, 5))) miller.map_to_asu(sg_type, False, miller_indices, data) for sg_symbol in ("P 41", "P 31 1 2"): exercise_map_to_asu(sg_symbol) # sg_type = sgtbx.space_group_type("P 2") miller_indices = flex.miller_index(((1, 2, 3), (-1, -2, -3))) assert not miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices) assert miller.is_unique_set_under_symmetry(space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices) assert list( miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices)) == [0] assert list( miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices)) == [0, 1]
def exercise_asu(): sg_type = sgtbx.space_group_type("P 41") asu = sgtbx.reciprocal_space_asu(sg_type) miller_indices = flex.miller_index(((1,2,3), (3,5,0))) for h in miller_indices: h_eq = miller.sym_equiv_indices(sg_type.group(), h) for i_eq in xrange(h_eq.multiplicity(False)): h_i = h_eq(i_eq) for anomalous_flag in (False,True): a = miller.asym_index(sg_type.group(), asu, h_i.h()) assert a.h() == h o = a.one_column(anomalous_flag) assert o.i_column() == 0 t = a.two_column(anomalous_flag) assert t.h() == h assert (o.h() != h) == (t.i_column() == 1) assert not anomalous_flag or (t.i_column() != 0) == h_i.friedel_flag() assert anomalous_flag or t.i_column() == 0 miller.map_to_asu(sg_type, False, miller_indices) data = flex.double((0,0)) miller.map_to_asu(sg_type, False, miller_indices, data) miller.map_to_asu(sg_type, False, miller_indices, data, False) miller.map_to_asu(sg_type, False, miller_indices, data, True) data = flex.complex_double((0,0)) miller.map_to_asu(sg_type, False, miller_indices, data) data = flex.hendrickson_lattman(((1,2,3,4),(2,3,4,5))) miller.map_to_asu(sg_type, False, miller_indices, data) for sg_symbol in ("P 41", "P 31 1 2"): exercise_map_to_asu(sg_symbol) # sg_type = sgtbx.space_group_type("P 2") miller_indices = flex.miller_index(((1,2,3), (-1,-2,-3))) assert not miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices) assert miller.is_unique_set_under_symmetry( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices) assert list(miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=False, miller_indices=miller_indices)) == [0] assert list(miller.unique_under_symmetry_selection( space_group_type=sg_type, anomalous_flag=True, miller_indices=miller_indices)) == [0,1]
def exercise_phase_integral(): sg = sgtbx.space_group_info("P 21 21 21").group() i = flex.miller_index([(1,2,3), (3,0,3)]) hl = flex.hendrickson_lattman([(1,2,3,4),(-2,3,-4,-5)]) integrator = miller.phase_integrator(n_steps=10) assert integrator.n_steps() == 10 integrator = miller.phase_integrator() assert integrator.n_steps() == 360//5 assert approx_equal(integrator(sg.phase_restriction(i[0]), hl[0]), 0.78832161462+0.466993941292j) assert approx_equal(integrator(sg.phase_restriction(i[1]), hl[1]), 6.09275186883e-17+0.995054753687j) assert approx_equal(integrator( space_group=sg, miller_indices=i, hendrickson_lattman_coefficients=hl), [(0.78832161462020822+0.46699394129187444j), (6.0927518688296534e-17+0.99505475368673046j)])
def ExtendAnyData(data, nsize): if isinstance(data, flex.bool): # flex.bool cannot be extended with NaN so cast the data to ints and extend with inanval instead data = data.as_int().extend( flex.int(nsize, inanval) ) if isinstance(data, flex.hendrickson_lattman): data.extend( flex.hendrickson_lattman(nsize, (nanval, nanval, nanval, nanval)) ) if isinstance(data, flex.int) or isinstance(data, flex.long) \ or isinstance(data, flex.size_t): data.extend( flex.int(nsize, inanval) ) if isinstance(data, flex.float) or isinstance(data, flex.double): data.extend( flex.double(nsize, nanval) ) if isinstance(data, flex.complex_double): data.extend( flex.complex_double(nsize, nanval) ) if isinstance(data, flex.vec3_double): data.extend( flex.vec3_double(nsize, (1.,1.,1.)) ) return data
def ExtendAnyData(data, nsize): if isinstance(data, flex.bool): data.extend( flex.bool(nsize, False) ) # insert NAN values as default values for real and integer values if isinstance(data, flex.hendrickson_lattman): data.extend( flex.hendrickson_lattman(nsize, (nanval, nanval, nanval, nanval)) ) if isinstance(data, flex.int) or isinstance(data, flex.long) \ or isinstance(data, flex.size_t): data.extend( flex.int(nsize, inanval) ) if isinstance(data, flex.float) or isinstance(data, flex.double): data.extend( flex.double(nsize, nanval) ) if isinstance(data, flex.complex_double): data.extend( flex.complex_double(nsize, nanval) ) if isinstance(data, flex.vec3_double): data.extend( flex.vec3_double(nsize, (1.,1.,1.)) ) return data
def exercise_extract_miller_array_from_file(): from iotbx import reflection_file_utils as rfu from libtbx.test_utils import approx_equal log = null_out() sorry_counts = 0 crystal_symmetry = crystal.symmetry( unit_cell=(30,31,32,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) size = miller_set.indices().size() a1 = miller_set.array( data=flex.hendrickson_lattman(size, (1,1,1,1))) a2 = miller_set.array(data=flex.double(size, 2)) a3 = miller_set.array(data=flex.double(size, 3)) a4 = miller_set.array(data=flex.complex_double(size, 4+4j)) a5 = miller_set.array(data=flex.complex_double(size, 5+5j)) # mtz_dataset = a1.as_mtz_dataset(column_root_label="A1") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.hendrickson_lattman # mtz_dataset = a5.as_mtz_dataset(column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.complex_double # for tp in [None, "complex"]: mtz_dataset = a4.as_mtz_dataset(column_root_label="A4") mtz_dataset.add_miller_array( miller_array=a5, column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") try: rfu.extract_miller_array_from_file(file_name="tmp.mtz",type=tp, log=log) except Sorry, e: assert ("Multiple choices available." in str(e)) sorry_counts += 1
def exercise_extract_miller_array_from_file(): from iotbx import reflection_file_utils as rfu from libtbx.test_utils import approx_equal log = null_out() sorry_counts = 0 crystal_symmetry = crystal.symmetry(unit_cell=(30, 31, 32, 85, 95, 100), space_group_symbol="P 1") miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) size = miller_set.indices().size() a1 = miller_set.array(data=flex.hendrickson_lattman(size, (1, 1, 1, 1))) a2 = miller_set.array(data=flex.double(size, 2)) a3 = miller_set.array(data=flex.double(size, 3)) a4 = miller_set.array(data=flex.complex_double(size, 4 + 4j)) a5 = miller_set.array(data=flex.complex_double(size, 5 + 5j)) # mtz_dataset = a1.as_mtz_dataset(column_root_label="A1") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.hendrickson_lattman # mtz_dataset = a5.as_mtz_dataset(column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") ma = rfu.extract_miller_array_from_file(file_name="tmp.mtz", log=log) assert type(ma.data()) == flex.complex_double # for tp in [None, "complex"]: mtz_dataset = a4.as_mtz_dataset(column_root_label="A4") mtz_dataset.add_miller_array(miller_array=a5, column_root_label="A5") mtz_dataset.mtz_object().write("tmp.mtz") try: rfu.extract_miller_array_from_file(file_name="tmp.mtz", type=tp, log=log) except Sorry, e: assert ("Multiple choices available." in str(e)) sorry_counts += 1
def exercise_reflections () : hkl_handler = reflections.reflections_handler(allowed_param_names=phil_names) from cctbx import miller from cctbx import crystal from cctbx.array_family import flex symm = crystal.symmetry( unit_cell=(30,30,40,90,90,120), space_group_symbol="P 61 2 2") miller_set = miller.build_set( crystal_symmetry=symm, anomalous_flag=True, d_min=1.5) n_refl = miller_set.indices().size() data = flex.random_double(n_refl) sigmas = flex.random_double(n_refl) f_obs = miller_set.array(data=data, sigmas=sigmas) f_obs_merged = f_obs.average_bijvoet_mates() flags = f_obs_merged.generate_r_free_flags() # single dataset mtz_dataset = f_obs_merged.as_mtz_dataset( column_root_label="F-obs", wavelength=1.54) mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file(file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) assert (hkl_handler.set_param_file(file_name=file_name, file_param_name="refinement.input.xray_data.r_free_flags.file_name") == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=False) == hkl_handler.get_rfree_labels(file_name=file_name, neutron=True) == ['R-free-flags']) assert approx_equal(1.54, hkl_handler.get_wavelength(file_name=file_name, labels="F-obs,SIGF-obs")) # join X/N datasets hkl_handler = reflections.reflections_handler(allowed_param_names=phil_names) data_neutron = flex.random_double(n_refl) sigmas_neutron = flex.random_double(n_refl) f_obs_neutron = miller_set.array(data=data_neutron, sigmas=sigmas_neutron) mtz_dataset = f_obs_merged.as_mtz_dataset( column_root_label="F-obs-xray") mtz_dataset.add_miller_array(f_obs_neutron, column_root_label="F-obs-neutron") mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags-xray") mtz_dataset.add_miller_array(flags.deep_copy(), column_root_label="R-free-flags-neutron") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file(file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) for i, phil_name in enumerate(phil_names[1:4]) : assert (hkl_handler.set_param_file(file_name=file_name, file_param_name=phil_name) == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags-xray', 'R-free-flags-neutron']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=False) == ['R-free-flags-xray']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=True) == ['R-free-flags-neutron']) hkl_handler.check_symmetry(file_name=file_name) assert (hkl_handler.get_data_labels( file_param_name="refinement.input.xray_data.file_name") == hkl_handler.get_data_labels(file_name=file_name) == hkl_handler.get_amplitude_labels(file_name=file_name) == ["F-obs-xray,SIGF-obs-xray", "F-obs-neutron(+),SIGF-obs-neutron(+),"+ "F-obs-neutron(-),SIGF-obs-neutron(-)"]) assert (hkl_handler.get_anomalous_data_labels( file_param_name="refinement.input.xray_data.file_name") == ["F-obs-neutron(+),SIGF-obs-neutron(+)," + "F-obs-neutron(-),SIGF-obs-neutron(-)"]) assert (hkl_handler.has_anomalous_data( file_param_name="refinement.input.xray_data.file_name")) assert (hkl_handler.get_rfree_flag_value( array_name="F-obs-xray,SIGF-obs-xray", file_param_name="refinement.input.xray_data.file_name") is None) assert (hkl_handler.get_rfree_flag_value(array_name='R-free-flags-xray', file_param_name="refinement.input.xray_data.r_free_flags.file_name") == 1) (d_max, d_min) = hkl_handler.d_max_min() assert approx_equal(d_max, 25.98, eps=0.01) assert approx_equal(d_min, 1.5, eps=0.01) assert hkl_handler.space_group_as_str() == "P 61 2 2" assert (hkl_handler.unit_cell_as_str() == "30 30 40 90 90 120") assert (hkl_handler.unit_cell_as_str(separator=",") == "30,30,40,90,90,120") n_refl_merged = len(f_obs_merged.indices()) phi_array = f_obs_merged.random_phases_compatible_with_phase_restrictions( deg=True) fom_array = phi_array.array(data=flex.random_double(n_refl_merged)) hl_data = flex.hendrickson_lattman(n_refl_merged, (0,0,0,0)) hl_coeffs = phi_array.array(data=hl_data) assert (hl_coeffs.is_hendrickson_lattman_array()) from iotbx.mtz import label_decorator import iotbx.mtz class resolve_label_decorator (label_decorator) : def phases (self, *args, **kwds) : return label_decorator.phases(self, *args, **kwds) + "M" def hendrickson_lattman (self, *args, **kwds) : return label_decorator.hendrickson_lattman(self, *args, **kwds) + "M" mtz_dataset = f_obs_merged.as_mtz_dataset( column_root_label="FP") mtz_dataset.add_miller_array(phi_array, column_root_label="PHIM", label_decorator=resolve_label_decorator(), column_types="P") mtz_dataset.add_miller_array(fom_array, column_root_label="FOMM", column_types="W") mtz_dataset.add_miller_array(hl_coeffs, column_root_label="HL", label_decorator=resolve_label_decorator()) fwt_map = f_obs_merged.customized_copy(sigmas=None).phase_transfer(phi_array) mtz_dataset.add_miller_array(fwt_map, column_root_label="FWT", label_decorator=iotbx.mtz.ccp4_label_decorator()) mtz_dataset.add_miller_array(flags, column_root_label="FreeR_flag") resolve_file = "tst_iotbx_gui_tools_resolve.mtz" mtz_dataset.mtz_object().write(resolve_file) hkl_handler = reflections.reflections_handler( allowed_param_names=["map_coeffs"]) hkl_handler.set_param_file( file_name=resolve_file, file_param_name="map_coeffs") assert hkl_handler.has_data(file_name=resolve_file) l1 = hkl_handler.get_map_coeff_labels(file_name=resolve_file) assert (l1 == ['FWT,PHWT', 'FP,PHIM,FOMM']) l2 = hkl_handler.get_phase_deg_labels(file_name=resolve_file) assert (l2 == ['HLAM,HLBM,HLCM,HLDM', 'FWT,PHWT', 'PHIM',]), l2 l3 = hkl_handler.get_experimental_phase_labels(file_name=resolve_file) #print l3 l4 = hkl_handler.get_data_labels_for_wizard(file_name=resolve_file) assert (l4 == ['FP SIGFP']) l5 = hkl_handler.get_map_coeff_labels_for_build(file_name=resolve_file) assert (l5 == ['FWT,PHWT', 'FP,PHIM,FOMM']), l5 hkl_in = hkl_handler.get_file(file_name=resolve_file) assert (reflections.get_mtz_label_prefix(hkl_in) == "/crystal/dataset") map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file, f_label="FP,SIGFP") assert map_coeffs.is_complex_array() try : map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file) except Sorry : pass else : raise Exception_expected assert (hkl_handler.get_map_coeff_labels_for_fft(file_name=resolve_file) == ['FWT,PHWT', 'FP,SIGFP PHIM FOMM']) # miscellaneous utilities file_name = resolve_file hkl_in = file_reader.any_file(file_name) hkl_server = hkl_in.file_server assert approx_equal(reflections.get_high_resolution(hkl_server), 1.5, eps=0.0001) descriptions = [] for miller_array in hkl_server.miller_arrays : (sg, uc) = reflections.get_miller_array_symmetry(miller_array) assert (uc == "30 30 40 90 90 120") assert (str(sg) == "P 61 2 2") descriptions.append(reflections.get_array_description(miller_array)) assert (descriptions == [ 'Amplitude', 'Phases', 'Weights', 'HL coeffs', 'Map coeffs', 'R-free flag']), descriptions handler = reflections.reflections_handler() handler.save_file(input_file=hkl_in) assert (not handler.has_anomalous_data()) assert (handler.get_resolution_range(file_name=file_name)=="(25.981 - 1.500)") assert (handler.get_resolution_limits(file_name=file_name) == ('(25.981)', '(1.500)')) fmodel = phi_array.array(data=flex.complex_double(n_refl_merged, complex(0.5,0.8))) m1 = phi_array.array(data=flex.complex_double(n_refl_merged, complex(1,0))) m2 = phi_array.array(data=flex.complex_double(n_refl_merged, complex(0.5,0))) m3 = phi_array.array(flex.complex_double(n_refl_merged, complex(1,1))) dec = label_decorator(phases_prefix="PH") mtz_dataset = fmodel.as_mtz_dataset( column_root_label="F-model", label_decorator=dec) mtz_dataset.add_miller_array(m1, column_root_label="2FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m2, column_root_label="FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m3, column_root_label="2FOFCWT_no_fill", label_decorator=dec) file_name = "tst_iotbx_gui_tools_map_coeffs.mtz" mtz_dataset.mtz_object().write(file_name) hkl_handler = reflections.reflections_handler( allowed_param_names=["fmodel", "map_coeffs"]) hkl_handler.set_param_file( file_name=file_name, file_param_name="fmodel") assert (hkl_handler.get_fmodel_labels(file_name=file_name) == ['F-model,PHF-model']) assert (hkl_handler.get_amplitude_labels(file_name=file_name) == []) phi_labels = hkl_handler.get_phase_deg_labels(file_name=file_name) assert (len(phi_labels) == 4) phi_cols = hkl_handler.get_phase_column_labels(file_name=file_name) assert (phi_cols == ['PHF-model','PH2FOFCWT','PHFOFCWT','PH2FOFCWT_no_fill']) assert (len(hkl_handler.get_amplitude_column_labels(file_name=file_name, allow_conversion=True)) == 0) fc_cols = hkl_handler.get_fmodel_labels(file_name=file_name, first_column_only=True) assert (fc_cols == ['F-model']) hkl_server = file_reader.any_file(file_name).file_server map_labels = reflections.get_map_coeff_labels(hkl_server) assert (map_labels == ['2FOFCWT,PH2FOFCWT', 'FOFCWT,PHFOFCWT', '2FOFCWT_no_fill,PH2FOFCWT_no_fill',]) map_labels = reflections.get_map_coeffs_for_build(hkl_server) assert map_labels == ['2FOFCWT,PH2FOFCWT','2FOFCWT_no_fill,PH2FOFCWT_no_fill'] map_coeffs = reflections.extract_phenix_refine_map_coeffs(file_name) assert (len(map_coeffs) == 3) hkl_file = file_reader.any_file(file_name) assert reflections.get_mtz_label_prefix(hkl_file) == "/crystal/dataset" # other stuff (fp, fpp) = reflections.get_fp_fpp_from_sasaki("Se", 0.979) assert fp is not None and fpp is not None
def exercise_merge_equivalents(): i = flex.miller_index(((1,2,3), (1,2,3), (3,0,3), (3,0,3), (3,0,3), (1,1,2))) d = flex.double((1,2,3,4,5,6)) m = miller.ext.merge_equivalents_real(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, (3/2., 4, 6)) assert tuple(m.redundancies) == (2,3,1) assert approx_equal(m.r_linear, (1/3., 1/6., 0)) assert approx_equal(m.r_square, (0.1, 0.04, 0)) assert approx_equal(m.r_int, (1.+2.)/(3.+12.)) # s = flex.double((1/3.,1/2.,1/4.,1/6.,1/3.,1/5.)) m = miller.ext.merge_equivalents_obs(i, d, s, sigma_dynamic_range=2e-6) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, (17/13., (16*3+36*4+9*5)/(16+36+9.), 6)) assert approx_equal(m.sigmas, (math.sqrt(1/2./2),0.84077140277/3**0.5,1/5.)) assert m.sigma_dynamic_range == 2e-6 assert tuple(m.redundancies) == (2,3,1) assert approx_equal(m.r_linear, (1/3., 0.1762295, 0)) assert approx_equal(m.r_square, (0.1147929, 0.0407901, 0)) assert approx_equal(m.r_int, (abs(1-17/13.)+abs(2-17/13.) + abs(3-237/61.)+abs(4-237/61.)+abs(5-237/61.) ) / (1 + 2 + 3 + 4 + 5) ) # d = flex.complex_double( [complex(-1.706478, 0.248638), complex( 1.097872, -0.983523), complex( 0.147183, 2.625064), complex(-0.933310, 2.496886), complex( 1.745500, -0.686761), complex(-0.620066, 2.097776)]) m = miller.ext.merge_equivalents_complex(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, [ complex(-0.304303,-0.367443), complex( 0.319791, 1.478396), complex(-0.620066, 2.097776)]) assert tuple(m.redundancies) == (2,3,1) # d = flex.hendrickson_lattman( [(-1.706478, 0.248638, 1.653352, -2.411313), ( 1.097872, -0.983523, -2.756402, 0.294464), ( 0.147183, 2.625064, 1.003636, 2.563517), (-0.933310, 2.496886, 2.040418, 0.371885), ( 1.745500, -0.686761, -2.291345, -2.386650), (-0.620066, 2.097776, 0.099784, 0.268107)]) m = miller.ext.merge_equivalents_hl(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, [ (-0.3043030, -0.3674425, -0.5515250, -1.0584245), ( 0.3197910, 1.4783963, 0.2509030, 0.1829173), (-0.6200660, 2.0977760, 0.0997840, 0.2681070)]) assert tuple(m.redundancies) == (2,3,1) # d = flex.bool((True,True,False,False,False,True)) m = miller.ext.merge_equivalents_exact_bool(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert list(m.data) == [True, False, True] assert tuple(m.redundancies) == (2,3,1) d = flex.bool((True,True,False,True,False,True)) try: m = miller.ext.merge_equivalents_exact_bool(i, d) except RuntimeError, e: assert str(e) == "cctbx Error: merge_equivalents_exact:"\ " incompatible flags for hkl = (3, 0, 3)"
def exercise_mmcif_structure_factors(): miller_arrays = cif.reader(input_string=r3adrsf).as_miller_arrays() assert len(miller_arrays) == 16 hl_coeffs = find_miller_array_from_labels(miller_arrays, [ '_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso', 'scale_group_code=1', 'crystal_id=2', 'wavelength_id=3' ]) assert hl_coeffs.is_hendrickson_lattman_array() assert hl_coeffs.size() == 2 mas_as_cif_block = cif.miller_arrays_as_cif_block( hl_coeffs, column_names=('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso')) abcd = [] for key in ('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'): assert key in list(mas_as_cif_block.cif_block.keys()) abcd.append(flex.double(mas_as_cif_block.cif_block[key])) hl_coeffs_from_cif_block = flex.hendrickson_lattman(*abcd) assert approx_equal(hl_coeffs.data(), hl_coeffs_from_cif_block) f_meas_au = find_miller_array_from_labels(miller_arrays, [ '_refln.F_meas_au', '_refln.F_meas_sigma_au', 'scale_group_code=1', 'crystal_id=1', 'wavelength_id=1' ]) assert f_meas_au.is_xray_amplitude_array() assert f_meas_au.size() == 5 assert f_meas_au.sigmas() is not None assert f_meas_au.space_group_info().symbol_and_number( ) == 'C 1 2 1 (No. 5)' assert approx_equal(f_meas_au.unit_cell().parameters(), (163.97, 45.23, 110.89, 90.0, 131.64, 90.0)) pdbx_I_plus_minus = find_miller_array_from_labels(miller_arrays, [ '_refln.pdbx_I_plus', '_refln.pdbx_I_plus_sigma', '_refln.pdbx_I_minus', '_refln.pdbx_I_minus_sigma' ]) assert pdbx_I_plus_minus.is_xray_intensity_array() assert pdbx_I_plus_minus.anomalous_flag() assert pdbx_I_plus_minus.size() == 21 assert pdbx_I_plus_minus.unit_cell() is None # no symmetry information in assert pdbx_I_plus_minus.space_group() is None # this CIF block # miller_arrays = cif.reader(input_string=r3ad7sf).as_miller_arrays() assert len(miller_arrays) == 11 f_calc = find_miller_array_from_labels( miller_arrays, ['r3ad7sf', '_refln.F_calc', '_refln.phase_calc', 'crystal_id=2' ]) #, 'wavelength_id=1'])) assert f_calc.is_complex_array() assert f_calc.size() == 4 # miller_arrays = cif.reader( input_string=integer_observations).as_miller_arrays() assert len(miller_arrays) == 2 fmeas_sigmeas = find_miller_array_from_labels(miller_arrays, ['_refln.F_meas_au']) assert isinstance(fmeas_sigmeas.data(), flex.double) assert isinstance(fmeas_sigmeas.sigmas(), flex.double) # miller_arrays = cif.reader(input_string=r3v56sf).as_miller_arrays() assert len(miller_arrays) == 2 for ma in miller_arrays: assert ma.is_complex_array() find_miller_array_from_labels( miller_arrays, ['r3v56sf', '_refln.pdbx_DELFWT', '_refln.pdbx_DELPHWT']) find_miller_array_from_labels( miller_arrays, ['r3v56sf', '_refln.pdbx_FWT', '_refln.pdbx_PHWT']) # verify _refln.pdbx_FWT', '_refln.pdbx_PHWT' are parsed as complex arrays in the presence of other columns miller_arrays = cif.reader(input_string=r6cxosf).as_miller_arrays() assert len(miller_arrays) == 11 ma = find_miller_array_from_labels( miller_arrays, ['r6cxosf', '_refln.pdbx_FWT', '_refln.pdbx_PHWT']) assert ma.is_complex_array() ma = find_miller_array_from_labels( miller_arrays, ['r6cxosf', '_refln.pdbx_DELFWT', '_refln.pdbx_DELPHWT']) assert ma.is_complex_array() # accept unconventional cif column labels resembling mtz column labels miller_arrays = cif.reader(input_string=r6c5f_phases).as_miller_arrays() assert len(miller_arrays) == 8 ma = find_miller_array_from_labels( miller_arrays, ['6c5f_phases', '_refln.FP', '_refln.SIGFP']) assert ma.is_xray_amplitude_array() ma = find_miller_array_from_labels( miller_arrays, ['6c5f_phases', '_refln.FC', '_refln.PHIC']) assert ma.is_complex_array() ma = find_miller_array_from_labels( miller_arrays, ['6c5f_phases', '_refln.FC_ALL', '_refln.PHIC_ALL']) assert ma.is_complex_array() ma = find_miller_array_from_labels( miller_arrays, ['6c5f_phases', '_refln.DELFWT', '_refln.PHDELWT']) assert ma.is_complex_array()
miller_arrays, fp_sort_index_stacks, txt_out_format = mtzh.format_miller_arrays( iparams) print(txt_out_format) txt_out += txt_out_format for i in range(iparams.n_macro_cycles): txt_out += 'Macrocycle no. %4.0f\n' % (i + 1) print('Macrocycle no. %4.0f\n' % (i + 1)) for j in range(len(fp_sort_index_stacks)): #select the index group i_sel = fp_sort_index_stacks[j] #generate cdf_set for selected reflections from mmtbx.sisa.optimize.mod_optimize import sisa_optimizer somer = sisa_optimizer() hl_selected = flex.hendrickson_lattman( [miller_arrays[3].data()[ii_sel] for ii_sel in i_sel]) cdf_set = somer.calc_pdf_cdf_from_hl(hl_selected) def sisa_optimize_mproc_wrapper(arg): return sisa_optimize_mproc(arg, j, miller_arrays, i_sel, cdf_set, iparams) sisa_optimize_results = pool_map(args=range( iparams.n_micro_cycles), func=sisa_optimize_mproc_wrapper, processes=iparams.n_processors) list_phis = [] foms_sum = None list_skews = [] for result in sisa_optimize_results:
def __init__(self, cif_block, base_array_info=None): crystal_symmetry_builder.__init__(self, cif_block) if base_array_info is not None: self.crystal_symmetry = self.crystal_symmetry.join_symmetry( other_symmetry=base_array_info.crystal_symmetry_from_file, force=True) self._arrays = OrderedDict() if base_array_info is None: base_array_info = miller.array_info(source_type="cif") refln_containing_loops = self.get_miller_indices_containing_loops() for self.indices, refln_loop in refln_containing_loops: self.wavelength_id_array = None self.crystal_id_array = None self.scale_group_array = None wavelength_ids = [None] crystal_ids = [None] scale_groups = [None] for key, value in refln_loop.iteritems(): # need to get these arrays first if (key.endswith('wavelength_id') or key.endswith('crystal_id') or key.endswith('scale_group_code')): data = as_int_or_none_if_all_question_marks( value, column_name=key) if data is None: continue counts = data.counts() if len(counts) == 1: continue array = miller.array( miller.set(self.crystal_symmetry, self.indices).auto_anomalous(), data) if key.endswith('wavelength_id'): self.wavelength_id_array = array wavelength_ids = counts.keys() elif key.endswith('crystal_id'): self.crystal_id_array = array crystal_ids = counts.keys() elif key.endswith('scale_group_code'): self.scale_group_array = array scale_groups = counts.keys() for label, value in sorted(refln_loop.items()): for w_id in wavelength_ids: for crys_id in crystal_ids: for scale_group in scale_groups: if 'index_' in label: continue key = label labels = [label] if (key.endswith('wavelength_id') or key.endswith('crystal_id') or key.endswith('scale_group_code')): w_id = None crys_id = None scale_group = None key_suffix = '' if w_id is not None: key_suffix += '_%i' % w_id labels.insert(0, "wavelength_id=%i" % w_id) if crys_id is not None: key_suffix += '_%i' % crys_id labels.insert(0, "crystal_id=%i" % crys_id) if scale_group is not None: key_suffix += '_%i' % scale_group labels.insert( 0, "scale_group_code=%i" % scale_group) key += key_suffix sigmas = None if key in self._arrays: continue array = self.flex_std_string_as_miller_array( value, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) if array is None: continue if '_sigma' in key: sigmas_label = label key = None for suffix in ('', '_meas', '_calc'): if sigmas_label.replace( '_sigma', suffix) in refln_loop: key = sigmas_label.replace( '_sigma', suffix) + key_suffix break if key is None: key = sigmas_label + key_suffix elif key in self._arrays and self._arrays[ key].sigmas() is None: sigmas = array array = self._arrays[key] check_array_sizes(array, sigmas, key, sigmas_label) sigmas = as_flex_double( sigmas, sigmas_label) array.set_sigmas(sigmas.data()) info = array.info() array.set_info( info.customized_copy( labels=info.labels + [sigmas_label])) continue elif 'PHWT' in key: phwt_label = label fwt_label = label.replace('PHWT', 'FWT') if fwt_label not in refln_loop: continue phwt_array = array if fwt_label in self._arrays: array = self._arrays[fwt_label] check_array_sizes(array, phwt_array, fwt_label, phwt_label) phases = as_flex_double( phwt_array, phwt_label) info = array.info() array = array.phase_transfer(phases, deg=True) array.set_info( info.customized_copy( labels=info.labels + [phwt_label])) self._arrays[fwt_label] = array continue elif 'HL_' in key: hl_letter = key[key.find('HL_') + 3] hl_key = 'HL_' + hl_letter key = key.replace(hl_key, 'HL_A') if key in self._arrays: continue # this array is already dealt with hl_labels = [ label.replace(hl_key, 'HL_' + letter) for letter in 'ABCD' ] hl_keys = [ key.replace(hl_key, 'HL_' + letter) for letter in 'ABCD' ] hl_values = [ cif_block.get(hl_key) for hl_key in hl_labels ] if hl_values.count(None) == 0: selection = self.get_selection( hl_values[0], wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) hl_values = [ as_double_or_none_if_all_question_marks( hl.select(selection), column_name=lab) for hl, lab in zip( hl_values, hl_labels) ] array = miller.array( miller.set( self.crystal_symmetry, self.indices.select( selection)).auto_anomalous(), flex.hendrickson_lattman(*hl_values)) labels = labels[:-1] + hl_labels elif '.B_' in key or '_B_' in key: if '.B_' in key: key, key_b = key.replace('.B_', '.A_'), key label, label_b = label.replace( '.B_', '.A_'), label elif '_B_' in key: key, key_b = key.replace('_B', '_A'), key label, label_b = label.replace('_B', '_A'), label if key in refln_loop and key_b in refln_loop: b_part = array.data() if key in self._arrays: info = self._arrays[key].info() a_part = self._arrays[key].data() self._arrays[key] = self._arrays[ key].array( data=flex.complex_double( a_part, b_part)) self._arrays[key].set_info( info.customized_copy( labels=info.labels + [key_b])) continue elif ('phase_' in key and not key.endswith('_meas') and self.crystal_symmetry.space_group() is not None): alt_key1 = label.replace('phase_', 'F_') alt_key2 = alt_key1 + '_au' if alt_key1 in refln_loop: phase_key = label key = alt_key1 + key_suffix elif alt_key2 in refln_loop: phase_key = label key = alt_key2 + key_suffix else: phase_key = None if phase_key is not None: phases = array.data() if key in self._arrays: array = self._arrays[key] array = as_flex_double(array, key) check_array_sizes( array, phases, key, phase_key) info = self._arrays[key].info() self._arrays[ key] = array.phase_transfer( phases, deg=True) self._arrays[key].set_info( info.customized_copy( labels=info.labels + [phase_key])) else: array = self.flex_std_string_as_miller_array( refln_loop[label], wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) check_array_sizes( array, phases, key, phase_key) array.phase_transfer(phases, deg=True) labels = labels + [label, phase_key] if base_array_info.labels is not None: labels = base_array_info.labels + labels def rstrip_substrings(string, substrings): for substr in substrings: if substr == '': continue if string.endswith(substr): string = string[:-len(substr)] return string # determine observation type stripped_key = rstrip_substrings( key, [ key_suffix, '_au', '_meas', '_calc', '_plus', '_minus' ]) if (stripped_key.endswith('F_squared') or stripped_key.endswith('intensity') or stripped_key.endswith('.I') or stripped_key.endswith('_I')) and ( array.is_real_array() or array.is_integer_array()): array.set_observation_type_xray_intensity() elif (stripped_key.endswith('F') and (array.is_real_array() or array.is_integer_array())): array.set_observation_type_xray_amplitude() if (array.is_xray_amplitude_array() or array.is_xray_amplitude_array()): # e.g. merge_equivalents treats integer arrays differently, so must # convert integer observation arrays here to be safe if isinstance(array.data(), flex.int): array = array.customized_copy( data=array.data().as_double()) array.set_info( base_array_info.customized_copy(labels=labels)) self._arrays.setdefault(key, array) for key, array in self._arrays.copy().iteritems(): if (key.endswith('_minus') or '_minus_' in key or key.endswith('_plus') or '_plus_' in key): if '_minus' in key: minus_key = key plus_key = key.replace('_minus', '_plus') elif '_plus' in key: plus_key = key minus_key = key.replace('_plus', '_minus') if plus_key in self._arrays and minus_key in self._arrays: plus_array = self._arrays.pop(plus_key) minus_array = self._arrays.pop(minus_key) minus_array = minus_array.customized_copy( indices=-minus_array.indices()).set_info( minus_array.info()) array = plus_array.concatenate( minus_array, assert_is_similar_symmetry=False) array = array.customized_copy(anomalous_flag=True) array.set_info( minus_array.info().customized_copy(labels=list( OrderedSet(plus_array.info().labels + minus_array.info().labels)))) array.set_observation_type(plus_array.observation_type()) self._arrays.setdefault(key, array) if len(self._arrays) == 0: raise CifBuilderError("No reflection data present in cif block")
def exercise_miller_array_as_cns_hkl(): s = StringIO() crystal_symmetry = crystal.symmetry() for anomalous_flag in [False, True]: miller_set = miller.set(crystal_symmetry=crystal_symmetry, indices=flex.miller_index([(1, 2, 3), (-3, 5, -7)]), anomalous_flag=anomalous_flag) for data in [ flex.bool((False, True)), flex.int((-3, 4)), flex.double((10, 13)), flex.complex_double((10, 13)), flex.hendrickson_lattman(((0, 1, 2, 3), (4, 5, 6, 7))) ]: miller_array = miller_set.array(data=data) miller_array.export_as_cns_hkl(file_object=s) if (isinstance(data, flex.double)): miller_array = miller_set.array(data=data, sigmas=data / 10.) miller_array.export_as_cns_hkl(file_object=s) assert not show_diff( s.getvalue(), """\ NREFlections=2 ANOMalous=FALSe DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= 0 INDEx -3 5 -7 DATA= 1 NREFlections=2 ANOMalous=FALSe DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= -3 INDEx -3 5 -7 DATA= 4 NREFlections=2 ANOMalous=FALSe DECLare NAME=DATA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 DATA= 10 INDEx -3 5 -7 DATA= 13 NREFlections=2 ANOMalous=FALSe DECLare NAME=FOBS DOMAin=RECIprocal TYPE=REAL END DECLare NAME=SIGMA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 FOBS= 10 SIGMA= 1 INDEx -3 5 -7 FOBS= 13 SIGMA= 1.3 NREFlections=2 ANOMalous=FALSe DECLare NAME=F DOMAin=RECIprocal TYPE=COMPLEX END INDEx 1 2 3 F= 10 0 INDEx -3 5 -7 F= 13 0 NREFlections=2 ANOMalous=FALSe DECLare NAME=PA DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PB DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PC DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PD DOMAin=RECIprocal TYPE=REAL END GROUp TYPE=HL OBJEct=PA OBJEct=PB OBJEct=PC OBJEct=PD END INDEx 1 2 3 PA= 0 PB= 1 PC= 2 PD= 3 INDEx -3 5 -7 PA= 4 PB= 5 PC= 6 PD= 7 NREFlections=2 ANOMalous=TRUE DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= 0 INDEx -3 5 -7 DATA= 1 NREFlections=2 ANOMalous=TRUE DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= -3 INDEx -3 5 -7 DATA= 4 NREFlections=2 ANOMalous=TRUE DECLare NAME=DATA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 DATA= 10 INDEx -3 5 -7 DATA= 13 NREFlections=2 ANOMalous=TRUE DECLare NAME=FOBS DOMAin=RECIprocal TYPE=REAL END DECLare NAME=SIGMA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 FOBS= 10 SIGMA= 1 INDEx -3 5 -7 FOBS= 13 SIGMA= 1.3 NREFlections=2 ANOMalous=TRUE DECLare NAME=F DOMAin=RECIprocal TYPE=COMPLEX END INDEx 1 2 3 F= 10 0 INDEx -3 5 -7 F= 13 0 NREFlections=2 ANOMalous=TRUE DECLare NAME=PA DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PB DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PC DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PD DOMAin=RECIprocal TYPE=REAL END GROUp TYPE=HL OBJEct=PA OBJEct=PB OBJEct=PC OBJEct=PD END INDEx 1 2 3 PA= 0 PB= 1 PC= 2 PD= 3 INDEx -3 5 -7 PA= 4 PB= 5 PC= 6 PD= 7 """)
def t_1(xray_structure, d_min=3.5): sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() for deep_copy in [True, False]: for hl in [True, False]: for target_name in ["ls_wunit_k1", "ml"]: for algorithm in ["direct", "fft"]: sfg_params.algorithm = algorithm for anomalous_flag in [True, False]: for cos_sin_table in [True, False]: sfg_params.cos_sin_table = cos_sin_table f_obs = abs(xray_structure.structure_factors( d_min = d_min, anomalous_flag = anomalous_flag, cos_sin_table = cos_sin_table, algorithm = algorithm).f_calc()) fc = f_obs.structure_factors_from_scatterers( xray_structure = xray_structure, algorithm = algorithm, cos_sin_table = cos_sin_table).f_calc() f_obs = abs(fc) flags = f_obs.generate_r_free_flags(fraction = 0.1, max_free = 99999999) f_obs.set_observation_type_xray_amplitude() fm = f_obs.array(data = flex.complex_double(f_obs.data().size(), 0)) for xff in [(xray_structure, None, None),(None, fc, fm)]: xrs, f_calc, f_mask = xff ### ### default states ### hl_coeffs = None if(hl): hl_coeffs = f_obs.customized_copy(data = flex.hendrickson_lattman(f_obs.size())) fmodel = mmtbx.f_model.manager( xray_structure = xrs, abcd = hl_coeffs, f_calc = f_calc, f_mask = f_mask, f_obs = f_obs, r_free_flags = flags, target_name = target_name, sf_and_grads_accuracy_params = sfg_params) if(deep_copy): fmodel = fmodel.deep_copy() # fi = fmodel.info() assert approx_equal(fi.alpha_work_max , 1.0) assert approx_equal(fi.alpha_work_mean , 1.0) assert approx_equal(fi.alpha_work_min , 1.0) assert approx_equal(fi.beta_work_max , 0.0) assert approx_equal(fi.beta_work_mean , 0.0) assert approx_equal(fi.beta_work_min , 0.0) assert approx_equal(fi.completeness_6_inf , 1.0) assert approx_equal(fi.completeness_d_min_inf, 1.0) assert approx_equal(fi.completeness_in_range , 1.0) assert approx_equal(fi.fom_work_max , 1.0) assert approx_equal(fi.fom_work_mean , 1.0) assert approx_equal(fi.fom_work_min , 1.0) assert approx_equal(fi.ml_coordinate_error , 0.0) assert approx_equal(fi.ml_phase_error , 0.0) assert approx_equal(fi.number_of_reflections , f_obs.size()) assert fi.number_of_reflections_merged== f_obs.deep_copy().average_bijvoet_mates().data().size() assert fi.number_of_test_reflections == flags.data().count(True) assert fi.number_of_work_reflections == flags.data().count(False) assert approx_equal(fi.overall_scale_k1, 1.0) assert approx_equal(fi.pher_free_max , 0.0) assert approx_equal(fi.pher_free_mean , 0.0) assert approx_equal(fi.pher_free_min , 0.0) assert approx_equal(fi.pher_work_max , 0.0) assert approx_equal(fi.pher_work_mean , 0.0) assert approx_equal(fi.pher_work_min , 0.0) assert approx_equal(fi.r_all , 0.0) assert approx_equal(fi.r_free , 0.0) assert approx_equal(fi.r_work , 0.0) assert fi.sf_algorithm == algorithm assert fi.target_name == target_name assert approx_equal(fi.target_free, 0.0) assert approx_equal(fi.target_work, 0.0) assert fi.twin_fraction is None assert fi.twin_law is None # if(hl): fmodel.hl_coeffs() is not None else: fmodel.hl_coeffs() is None assert fmodel.f_model_free().data().size() == flags.data().count(True) assert fmodel.f_model_work().data().size() == flags.data().count(False) assert fmodel.f_obs().data().all_eq(f_obs.data()) assert abs(fmodel.f_calc()).data().all_eq(f_obs.data()) assert abs(fmodel.f_model()).data().all_eq(f_obs.data()) assert approx_equal(fmodel.r_work(), 0, 1.e-9) assert approx_equal(fmodel.r_free(), 0, 1.e-9) assert approx_equal(fmodel.r_all(), 0, 1.e-9) assert fmodel.k_anisotropic().all_eq(1.0) assert fmodel.k_anisotropic_work().all_eq(1.0) assert fmodel.k_anisotropic_test().all_eq(1.0) assert abs(fmodel.target_w()) < 1.e-9 assert abs(fmodel.target_t()) < 1.e-9 assert len(fmodel.k_masks())==1 assert fmodel.k_masks()[0].all_eq(0) assert fmodel.k_isotropic().all_eq(1.0) assert fmodel.f_obs_work().data().all_eq(f_obs.select(~flags.data()).data()) assert fmodel.f_obs_free().data().all_eq(f_obs.select(flags.data()).data()) assert abs(fmodel.f_calc_w()).data().all_eq(f_obs.select(~flags.data()).data()) assert abs(fmodel.f_calc_t()).data().all_eq(f_obs.select(flags.data()).data()) assert abs(fmodel.f_model_work()).data().all_eq(f_obs.select(~flags.data()).data()) assert abs(fmodel.f_model_free()).data().all_eq(f_obs.select(flags.data()).data()) assert abs(fmodel.f_bulk_w()).data().all_eq(0) assert abs(fmodel.f_bulk_t()).data().all_eq(0) assert fmodel.f_model().data().all_eq(fmodel.f_calc().data()) assert fmodel.f_model_scaled_with_k1().data().all_eq(fmodel.f_calc().data()) assert approx_equal(fmodel.scale_k1() , 1.0, 1.e-9) assert approx_equal(fmodel.scale_k1_w(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k1_t(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k2_w(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k2_t(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k3_w(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k3_t(), 1.0, 1.e-9) assert fmodel.figures_of_merit().all_approx_equal(1.0, 1.e-6) assert fmodel.phase_errors().all_approx_equal(0.0, 1.e-6) assert fmodel.phase_errors_work().all_approx_equal(0.0, 1.e-6) assert fmodel.phase_errors_test().all_approx_equal(0.0, 1.e-6) a,b = fmodel.alpha_beta() assert a.data().all_approx_equal(1.0, 1.e-9) assert b.data().all_approx_equal(0.0, 1.e-9) a,b = fmodel.alpha_beta_w() assert a.data().all_approx_equal(1.0, 1.e-9) assert b.data().all_approx_equal(0.0, 1.e-9) a,b = fmodel.alpha_beta_t() assert a.data().all_approx_equal(1.0, 1.e-9) assert b.data().all_approx_equal(0.0, 1.e-9) assert approx_equal(fmodel.model_error_ml(), 0) # # update k_isotropic, k_anisotropic, resolution_filter # fmodel.update_core( k_isotropic = flex.double(f_obs.data().size(), 1./2), k_anisotropic = flex.double(f_obs.data().size(), 1./3)) assert approx_equal(fmodel.r_work(), 0) assert approx_equal(fmodel.scale_k1(), 6.0) fmodel = fmodel.deep_copy() assert approx_equal(fmodel.r_work(), 0) assert approx_equal(fmodel.scale_k1(), 6.0) # fmodel_ = fmodel.resolution_filter(d_min=fmodel.f_obs().d_min()+0.5) assert fmodel_.f_obs().size() < fmodel.f_obs().size() fmodel_ = fmodel.resolution_filter(d_max=fmodel.f_obs().d_max_min()[0]-1.) assert fmodel_.f_obs().size() < fmodel.f_obs().size()
def exercise_reflections(): hkl_handler = reflections.reflections_handler( allowed_param_names=phil_names) from cctbx import miller from cctbx import crystal from cctbx.array_family import flex symm = crystal.symmetry(unit_cell=(30, 30, 40, 90, 90, 120), space_group_symbol="P 61 2 2") miller_set = miller.build_set(crystal_symmetry=symm, anomalous_flag=True, d_min=1.5) n_refl = miller_set.indices().size() data = flex.random_double(n_refl) sigmas = flex.random_double(n_refl) f_obs = miller_set.array(data=data, sigmas=sigmas) f_obs_merged = f_obs.average_bijvoet_mates() flags = f_obs_merged.generate_r_free_flags() # single dataset mtz_dataset = f_obs_merged.as_mtz_dataset(column_root_label="F-obs", wavelength=1.54) mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file( file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) assert (hkl_handler.set_param_file( file_name=file_name, file_param_name="refinement.input.xray_data.r_free_flags.file_name") == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags']) assert (hkl_handler.get_rfree_labels(file_name=file_name, neutron=False) == hkl_handler.get_rfree_labels(file_name=file_name, neutron=True) == ['R-free-flags']) assert approx_equal( 1.54, hkl_handler.get_wavelength(file_name=file_name, labels="F-obs,SIGF-obs")) # join X/N datasets hkl_handler = reflections.reflections_handler( allowed_param_names=phil_names) data_neutron = flex.random_double(n_refl) sigmas_neutron = flex.random_double(n_refl) f_obs_neutron = miller_set.array(data=data_neutron, sigmas=sigmas_neutron) mtz_dataset = f_obs_merged.as_mtz_dataset(column_root_label="F-obs-xray") mtz_dataset.add_miller_array(f_obs_neutron, column_root_label="F-obs-neutron") mtz_dataset.add_miller_array(flags, column_root_label="R-free-flags-xray") mtz_dataset.add_miller_array(flags.deep_copy(), column_root_label="R-free-flags-neutron") file_name = "tst_iotbs_gui_tools.mtz" mtz_dataset.mtz_object().write(file_name) assert (hkl_handler.set_param_file( file_name=file_name, file_param_name="refinement.input.xray_data.file_name") == True) for i, phil_name in enumerate(phil_names[1:4]): assert (hkl_handler.set_param_file(file_name=file_name, file_param_name=phil_name) == False) assert (hkl_handler.get_rfree_labels( file_param_name="refinement.input.xray_data.r_free_flags.file_name") == hkl_handler.get_rfree_labels(file_name=file_name) == ['R-free-flags-xray', 'R-free-flags-neutron']) assert (hkl_handler.get_rfree_labels( file_name=file_name, neutron=False) == ['R-free-flags-xray']) assert (hkl_handler.get_rfree_labels( file_name=file_name, neutron=True) == ['R-free-flags-neutron']) hkl_handler.check_symmetry(file_name=file_name) assert (hkl_handler.get_data_labels( file_param_name="refinement.input.xray_data.file_name") == hkl_handler.get_data_labels(file_name=file_name) == hkl_handler.get_amplitude_labels(file_name=file_name) == [ "F-obs-xray,SIGF-obs-xray", "F-obs-neutron(+),SIGF-obs-neutron(+)," + "F-obs-neutron(-),SIGF-obs-neutron(-)" ]) assert (hkl_handler.get_anomalous_data_labels( file_param_name="refinement.input.xray_data.file_name") == [ "F-obs-neutron(+),SIGF-obs-neutron(+)," + "F-obs-neutron(-),SIGF-obs-neutron(-)" ]) assert (hkl_handler.has_anomalous_data( file_param_name="refinement.input.xray_data.file_name")) assert (hkl_handler.get_rfree_flag_value( array_name="F-obs-xray,SIGF-obs-xray", file_param_name="refinement.input.xray_data.file_name") is None) assert (hkl_handler.get_rfree_flag_value( array_name='R-free-flags-xray', file_param_name="refinement.input.xray_data.r_free_flags.file_name") == 1) (d_max, d_min) = hkl_handler.d_max_min() assert approx_equal(d_max, 25.98, eps=0.01) assert approx_equal(d_min, 1.5, eps=0.01) assert hkl_handler.space_group_as_str() == "P 61 2 2" assert (hkl_handler.unit_cell_as_str() == "30 30 40 90 90 120") assert (hkl_handler.unit_cell_as_str( separator=",") == "30,30,40,90,90,120") n_refl_merged = len(f_obs_merged.indices()) phi_array = f_obs_merged.random_phases_compatible_with_phase_restrictions( deg=True) fom_array = phi_array.array(data=flex.random_double(n_refl_merged)) hl_data = flex.hendrickson_lattman(n_refl_merged, (0, 0, 0, 0)) hl_coeffs = phi_array.array(data=hl_data) assert (hl_coeffs.is_hendrickson_lattman_array()) from iotbx.mtz import label_decorator import iotbx.mtz class resolve_label_decorator(label_decorator): def phases(self, *args, **kwds): return label_decorator.phases(self, *args, **kwds) + "M" def hendrickson_lattman(self, *args, **kwds): return label_decorator.hendrickson_lattman(self, *args, ** kwds) + "M" mtz_dataset = f_obs_merged.as_mtz_dataset(column_root_label="FP") mtz_dataset.add_miller_array(phi_array, column_root_label="PHIM", label_decorator=resolve_label_decorator(), column_types="P") mtz_dataset.add_miller_array(fom_array, column_root_label="FOMM", column_types="W") mtz_dataset.add_miller_array(hl_coeffs, column_root_label="HL", label_decorator=resolve_label_decorator()) fwt_map = f_obs_merged.customized_copy( sigmas=None).phase_transfer(phi_array) mtz_dataset.add_miller_array( fwt_map, column_root_label="FWT", label_decorator=iotbx.mtz.ccp4_label_decorator()) mtz_dataset.add_miller_array(flags, column_root_label="FreeR_flag") resolve_file = "tst_iotbx_gui_tools_resolve.mtz" mtz_dataset.mtz_object().write(resolve_file) hkl_handler = reflections.reflections_handler( allowed_param_names=["map_coeffs"]) hkl_handler.set_param_file(file_name=resolve_file, file_param_name="map_coeffs") assert hkl_handler.has_data(file_name=resolve_file) l1 = hkl_handler.get_map_coeff_labels(file_name=resolve_file) assert (l1 == ['FWT,PHWT', 'FP,PHIM,FOMM']) l2 = hkl_handler.get_phase_deg_labels(file_name=resolve_file) assert (l2 == [ 'HLAM,HLBM,HLCM,HLDM', 'FWT,PHWT', 'PHIM', ]), l2 l3 = hkl_handler.get_experimental_phase_labels(file_name=resolve_file) #print l3 l4 = hkl_handler.get_data_labels_for_wizard(file_name=resolve_file) assert (l4 == ['FP SIGFP']) l5 = hkl_handler.get_map_coeff_labels_for_build(file_name=resolve_file) assert (l5 == ['FWT,PHWT', 'FP,PHIM,FOMM']), l5 hkl_in = hkl_handler.get_file(file_name=resolve_file) assert (reflections.get_mtz_label_prefix(hkl_in) == "/crystal/dataset") map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file, f_label="FP,SIGFP") assert map_coeffs.is_complex_array() try: map_coeffs = reflections.map_coeffs_from_mtz_file(resolve_file) except Sorry: pass else: raise Exception_expected assert (hkl_handler.get_map_coeff_labels_for_fft( file_name=resolve_file) == ['FWT,PHWT', 'FP,SIGFP PHIM FOMM']) # miscellaneous utilities file_name = resolve_file hkl_in = file_reader.any_file(file_name) hkl_server = hkl_in.file_server assert approx_equal(reflections.get_high_resolution(hkl_server), 1.5, eps=0.0001) descriptions = [] for miller_array in hkl_server.miller_arrays: (sg, uc) = reflections.get_miller_array_symmetry(miller_array) assert (uc == "30 30 40 90 90 120") assert (str(sg) == "P 61 2 2") descriptions.append(reflections.get_array_description(miller_array)) assert (descriptions == [ 'Amplitude', 'Phases', 'Weights', 'HL coeffs', 'Map coeffs', 'R-free flag' ]), descriptions handler = reflections.reflections_handler() handler.save_file(input_file=hkl_in) assert (not handler.has_anomalous_data()) assert (handler.get_resolution_range( file_name=file_name) == "(25.981 - 1.500)") assert (handler.get_resolution_limits(file_name=file_name) == ('(25.981)', '(1.500)')) fmodel = phi_array.array( data=flex.complex_double(n_refl_merged, complex(0.5, 0.8))) m1 = phi_array.array( data=flex.complex_double(n_refl_merged, complex(1, 0))) m2 = phi_array.array( data=flex.complex_double(n_refl_merged, complex(0.5, 0))) m3 = phi_array.array(flex.complex_double(n_refl_merged, complex(1, 1))) dec = label_decorator(phases_prefix="PH") mtz_dataset = fmodel.as_mtz_dataset(column_root_label="F-model", label_decorator=dec) mtz_dataset.add_miller_array(m1, column_root_label="2FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m2, column_root_label="FOFCWT", label_decorator=dec) mtz_dataset.add_miller_array(m3, column_root_label="2FOFCWT_no_fill", label_decorator=dec) file_name = "tst_iotbx_gui_tools_map_coeffs.mtz" mtz_dataset.mtz_object().write(file_name) hkl_handler = reflections.reflections_handler( allowed_param_names=["fmodel", "map_coeffs"]) hkl_handler.set_param_file(file_name=file_name, file_param_name="fmodel") assert (hkl_handler.get_fmodel_labels(file_name=file_name) == [ 'F-model,PHF-model' ]) assert (hkl_handler.get_amplitude_labels(file_name=file_name) == []) phi_labels = hkl_handler.get_phase_deg_labels(file_name=file_name) assert (len(phi_labels) == 4) phi_cols = hkl_handler.get_phase_column_labels(file_name=file_name) assert (phi_cols == [ 'PHF-model', 'PH2FOFCWT', 'PHFOFCWT', 'PH2FOFCWT_no_fill' ]) assert (len( hkl_handler.get_amplitude_column_labels(file_name=file_name, allow_conversion=True)) == 0) fc_cols = hkl_handler.get_fmodel_labels(file_name=file_name, first_column_only=True) assert (fc_cols == ['F-model']) hkl_server = file_reader.any_file(file_name).file_server map_labels = reflections.get_map_coeff_labels(hkl_server) assert (map_labels == [ '2FOFCWT,PH2FOFCWT', 'FOFCWT,PHFOFCWT', '2FOFCWT_no_fill,PH2FOFCWT_no_fill', ]) map_labels = reflections.get_map_coeffs_for_build(hkl_server) assert map_labels == [ '2FOFCWT,PH2FOFCWT', '2FOFCWT_no_fill,PH2FOFCWT_no_fill' ] map_coeffs = reflections.extract_phenix_refine_map_coeffs(file_name) assert (len(map_coeffs) == 3) hkl_file = file_reader.any_file(file_name) assert reflections.get_mtz_label_prefix(hkl_file) == "/crystal/dataset" # other stuff (fp, fpp) = reflections.get_fp_fpp_from_sasaki("Se", 0.979) assert fp is not None and fpp is not None
def t_1(xray_structure, d_min=3.5): sfg_params = mmtbx.f_model.sf_and_grads_accuracy_master_params.extract() for deep_copy in [True, False]: for hl in [True, False]: for target_name in ["ls_wunit_k1", "ml"]: for algorithm in ["direct", "fft"]: sfg_params.algorithm = algorithm for anomalous_flag in [True, False]: for cos_sin_table in [True, False]: sfg_params.cos_sin_table = cos_sin_table f_obs = abs( xray_structure.structure_factors( d_min=d_min, anomalous_flag=anomalous_flag, cos_sin_table=cos_sin_table, algorithm=algorithm).f_calc()) fc = f_obs.structure_factors_from_scatterers( xray_structure=xray_structure, algorithm=algorithm, cos_sin_table=cos_sin_table).f_calc() f_obs = abs(fc) flags = f_obs.generate_r_free_flags( fraction=0.1, max_free=99999999) f_obs.set_observation_type_xray_amplitude() fm = f_obs.array(data=flex.complex_double( f_obs.data().size(), 0)) for xff in [(xray_structure, None, None), (None, fc, fm)]: xrs, f_calc, f_mask = xff ### ### default states ### hl_coeffs = None if (hl): hl_coeffs = f_obs.customized_copy( data=flex.hendrickson_lattman( f_obs.size())) fmodel = mmtbx.f_model.manager( xray_structure=xrs, abcd=hl_coeffs, f_calc=f_calc, f_mask=f_mask, f_obs=f_obs, r_free_flags=flags, target_name=target_name, sf_and_grads_accuracy_params=sfg_params) if (deep_copy): fmodel = fmodel.deep_copy() # fi = fmodel.info() assert approx_equal(fi.alpha_work_max, 1.0) assert approx_equal(fi.alpha_work_mean, 1.0) assert approx_equal(fi.alpha_work_min, 1.0) assert approx_equal(fi.beta_work_max, 0.0) assert approx_equal(fi.beta_work_mean, 0.0) assert approx_equal(fi.beta_work_min, 0.0) assert approx_equal(fi.completeness_6_inf, 1.0) assert approx_equal(fi.completeness_d_min_inf, 1.0) assert approx_equal(fi.completeness_in_range, 1.0) assert approx_equal(fi.fom_work_max, 1.0) assert approx_equal(fi.fom_work_mean, 1.0) assert approx_equal(fi.fom_work_min, 1.0) assert approx_equal(fi.ml_coordinate_error, 0.0) assert approx_equal(fi.ml_phase_error, 0.0) assert approx_equal(fi.number_of_reflections, f_obs.size()) assert fi.number_of_reflections_merged == f_obs.deep_copy( ).average_bijvoet_mates().data().size() assert fi.number_of_test_reflections == flags.data( ).count(True) assert fi.number_of_work_reflections == flags.data( ).count(False) assert approx_equal(fi.overall_scale_k1, 1.0) assert approx_equal(fi.pher_free_max, 0.0) assert approx_equal(fi.pher_free_mean, 0.0) assert approx_equal(fi.pher_free_min, 0.0) assert approx_equal(fi.pher_work_max, 0.0) assert approx_equal(fi.pher_work_mean, 0.0) assert approx_equal(fi.pher_work_min, 0.0) assert approx_equal(fi.r_all, 0.0) assert approx_equal(fi.r_free, 0.0) assert approx_equal(fi.r_work, 0.0) assert fi.sf_algorithm == algorithm assert fi.target_name == target_name assert approx_equal(fi.target_free, 0.0) assert approx_equal(fi.target_work, 0.0) assert fi.twin_fraction is None assert fi.twin_law is None # if (hl): fmodel.hl_coeffs() is not None else: fmodel.hl_coeffs() is None assert fmodel.f_model_free().data().size( ) == flags.data().count(True) assert fmodel.f_model_work().data().size( ) == flags.data().count(False) assert fmodel.f_obs().data().all_eq( f_obs.data()) assert abs(fmodel.f_calc()).data().all_eq( f_obs.data()) assert abs(fmodel.f_model()).data().all_eq( f_obs.data()) assert approx_equal(fmodel.r_work(), 0, 1.e-9) assert approx_equal(fmodel.r_free(), 0, 1.e-9) assert approx_equal(fmodel.r_all(), 0, 1.e-9) assert fmodel.k_anisotropic().all_eq(1.0) assert fmodel.k_anisotropic_work().all_eq(1.0) assert fmodel.k_anisotropic_test().all_eq(1.0) assert abs(fmodel.target_w()) < 1.e-9 assert abs(fmodel.target_t()) < 1.e-9 assert len(fmodel.k_masks()) == 1 assert fmodel.k_masks()[0].all_eq(0) assert fmodel.k_isotropic().all_eq(1.0) assert fmodel.f_obs_work().data().all_eq( f_obs.select(~flags.data()).data()) assert fmodel.f_obs_free().data().all_eq( f_obs.select(flags.data()).data()) assert abs(fmodel.f_calc_w()).data().all_eq( f_obs.select(~flags.data()).data()) assert abs(fmodel.f_calc_t()).data().all_eq( f_obs.select(flags.data()).data()) assert abs( fmodel.f_model_work()).data().all_eq( f_obs.select(~flags.data()).data()) assert abs( fmodel.f_model_free()).data().all_eq( f_obs.select(flags.data()).data()) assert abs(fmodel.f_bulk_w()).data().all_eq(0) assert abs(fmodel.f_bulk_t()).data().all_eq(0) assert fmodel.f_model().data().all_eq( fmodel.f_calc().data()) assert fmodel.f_model_scaled_with_k1().data( ).all_eq(fmodel.f_calc().data()) assert approx_equal(fmodel.scale_k1(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k1_w(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k1_t(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k2_w(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k2_t(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k3_w(), 1.0, 1.e-9) assert approx_equal(fmodel.scale_k3_t(), 1.0, 1.e-9) assert fmodel.figures_of_merit( ).all_approx_equal(1.0, 1.e-6) assert fmodel.phase_errors().all_approx_equal( 0.0, 1.e-6) assert fmodel.phase_errors_work( ).all_approx_equal(0.0, 1.e-6) assert fmodel.phase_errors_test( ).all_approx_equal(0.0, 1.e-6) a, b = fmodel.alpha_beta() assert a.data().all_approx_equal(1.0, 1.e-9) assert b.data().all_approx_equal(0.0, 1.e-9) a, b = fmodel.alpha_beta_w() assert a.data().all_approx_equal(1.0, 1.e-9) assert b.data().all_approx_equal(0.0, 1.e-9) a, b = fmodel.alpha_beta_t() assert a.data().all_approx_equal(1.0, 1.e-9) assert b.data().all_approx_equal(0.0, 1.e-9) assert approx_equal(fmodel.model_error_ml(), 0) # # update k_isotropic, k_anisotropic, resolution_filter # fmodel.update_core(k_isotropic=flex.double( f_obs.data().size(), 1. / 2), k_anisotropic=flex.double( f_obs.data().size(), 1. / 3)) assert approx_equal(fmodel.r_work(), 0) assert approx_equal(fmodel.scale_k1(), 6.0) fmodel = fmodel.deep_copy() assert approx_equal(fmodel.r_work(), 0) assert approx_equal(fmodel.scale_k1(), 6.0) # fmodel_ = fmodel.resolution_filter( d_min=fmodel.f_obs().d_min() + 0.5) assert fmodel_.f_obs().size() < fmodel.f_obs( ).size() fmodel_ = fmodel.resolution_filter( d_max=fmodel.f_obs().d_max_min()[0] - 1.) assert fmodel_.f_obs().size() < fmodel.f_obs( ).size()
def exercise_flex_hendrickson_lattman(): a = flex.hendrickson_lattman() assert a.size() == 0 a = flex.hendrickson_lattman(132) for x in a: assert x == (0, 0, 0, 0) a = flex.hendrickson_lattman(((1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6))) assert a.size() == 3 assert a.count((1, 2, 3, 4)) == 1 assert a.count((0, 0, 0, 0)) == 0 assert tuple(a) == ((1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)) assert tuple(a + a) == ((2, 4, 6, 8), (4, 6, 8, 10), (6, 8, 10, 12)) a += a assert tuple(a) == ((2, 4, 6, 8), (4, 6, 8, 10), (6, 8, 10, 12)) p = pickle.dumps(a) b = pickle.loads(p) assert tuple(a) == tuple(b) centric_flags = flex.bool([False, True]) phase_integrals = flex.complex_double( [complex(0.5, -0.7), complex(-0.3, 0.4)]) a = flex.hendrickson_lattman(centric_flags=centric_flags, phase_integrals=phase_integrals, max_figure_of_merit=1 - 1.e-6) assert approx_equal(a, [(2.2684820912654264, -3.1758749277715967, 0, 0), (-0.3295836866004328, 0.43944491546724396, 0, 0)]) assert approx_equal( [a.slice(i) for i in xrange(4)], [[2.2684820912654264, -0.3295836866004328], [-3.1758749277715967, 0.43944491546724396], [0.0, 0.0], [0.0, 0.0]]) a = flex.hendrickson_lattman(3, (1, 2, 3, 4)) assert a.all_eq((1, 2, 3, 4)) assert not a.all_eq((1, 2, 0, 4)) assert approx_equal(a.conj(), [(1, -2, 3, -4), (1, -2, 3, -4), (1, -2, 3, -4)]) assert approx_equal(a.conj().conj(), a) # a = flex.double() h = flex.hendrickson_lattman(a=a, b=a) assert h.size() == 0 a = flex.double([1, 2, 3]) b = flex.double([-3, 4, 5]) h = flex.hendrickson_lattman(a=a, b=b) assert approx_equal(h, [(1, -3, 0, 0), (2, 4, 0, 0), (3, 5, 0, 0)]) assert approx_equal(h == (1, -3, 0, 0), (True, False, False)) assert approx_equal(h != (1, -3, 0, 0), (False, True, True)) assert approx_equal(h != (0, 0, 0, 0), (True, True, True)) assert approx_equal(h == h.deep_copy(), (True, True, True)) assert approx_equal(h == flex.hendrickson_lattman(a=b, b=a), (False, False, False)) assert approx_equal(h != flex.hendrickson_lattman(a=b, b=a), (True, True, True)) assert approx_equal(h != flex.hendrickson_lattman(a=b, b=a), (True, True, True)) assert approx_equal(h != h.deep_copy(), (False, False, False)) c = flex.double([4, 5, 6]) d = flex.double([-4, 7, 8]) h = flex.hendrickson_lattman(a=a, b=b, c=c, d=d) assert approx_equal(h, [(1, -3, 4, -4), (2, 4, 5, 7), (3, 5, 6, 8)]) assert approx_equal(h.as_abcd(), [a, b, c, d]) h = h * 2 assert approx_equal(h, [(2, -6, 8, -8), (4, 8, 10, 14), (6, 10, 12, 16)])
def exercise_merge_equivalents(): i = flex.miller_index(((1,2,3), (1,2,3), (3,0,3), (3,0,3), (3,0,3), (1,1,2))) d = flex.double((1,2,3,4,5,6)) m = miller.ext.merge_equivalents_real(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, (3/2., 4, 6)) assert tuple(m.redundancies) == (2,3,1) assert approx_equal(m.r_linear, (1/3., 1/6., 0)) assert approx_equal(m.r_square, (0.1, 0.04, 0)) assert approx_equal(m.r_int, (1.+2.)/(3.+12.)) # s = flex.double((1/3.,1/2.,1/4.,1/6.,1/3.,1/5.)) m = miller.ext.merge_equivalents_obs(i, d, s, sigma_dynamic_range=2e-6) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, (17/13., (16*3+36*4+9*5)/(16+36+9.), 6)) assert approx_equal(m.sigmas, (math.sqrt(1/2./2),0.84077140277/3**0.5,1/5.)) assert m.sigma_dynamic_range == 2e-6 assert tuple(m.redundancies) == (2,3,1) assert approx_equal(m.r_linear, (1/3., 0.1762295, 0)) assert approx_equal(m.r_square, (0.1147929, 0.0407901, 0)) assert approx_equal(m.r_int, (abs(1-17/13.)+abs(2-17/13.) + abs(3-237/61.)+abs(4-237/61.)+abs(5-237/61.) ) / (1 + 2 + 3 + 4 + 5) ) # d = flex.complex_double( [complex(-1.706478, 0.248638), complex( 1.097872, -0.983523), complex( 0.147183, 2.625064), complex(-0.933310, 2.496886), complex( 1.745500, -0.686761), complex(-0.620066, 2.097776)]) m = miller.ext.merge_equivalents_complex(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, [ complex(-0.304303,-0.367443), complex( 0.319791, 1.478396), complex(-0.620066, 2.097776)]) assert tuple(m.redundancies) == (2,3,1) # d = flex.hendrickson_lattman( [(-1.706478, 0.248638, 1.653352, -2.411313), ( 1.097872, -0.983523, -2.756402, 0.294464), ( 0.147183, 2.625064, 1.003636, 2.563517), (-0.933310, 2.496886, 2.040418, 0.371885), ( 1.745500, -0.686761, -2.291345, -2.386650), (-0.620066, 2.097776, 0.099784, 0.268107)]) m = miller.ext.merge_equivalents_hl(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert approx_equal(m.data, [ (-0.3043030, -0.3674425, -0.5515250, -1.0584245), ( 0.3197910, 1.4783963, 0.2509030, 0.1829173), (-0.6200660, 2.0977760, 0.0997840, 0.2681070)]) assert tuple(m.redundancies) == (2,3,1) # d = flex.bool((True,True,False,False,False,True)) m = miller.ext.merge_equivalents_exact_bool(i, d) assert tuple(m.indices) == ((1,2,3), (3,0,3), (1,1,2)) assert list(m.data) == [True, False, True] assert tuple(m.redundancies) == (2,3,1) d = flex.bool((True,True,False,True,False,True)) try: m = miller.ext.merge_equivalents_exact_bool(i, d) except RuntimeError as e: assert str(e) == "cctbx Error: merge_equivalents_exact:"\ " incompatible flags for hkl = (3, 0, 3)" else: raise Exception_expected d = flex.int((3,3,5,5,5,7)) m = miller.ext.merge_equivalents_exact_int(i, d) assert list(m.data) == [3, 5, 7] # i = flex.miller_index(((1,2,3), (3,0,3), (1,1,2))) d = flex.double((1,2,3)) m = miller.ext.merge_equivalents_real(i,d) assert m.r_int == 0
def exercise_get_amplitudes_and_get_phases_deg(): crystal_symmetry = crystal.symmetry( unit_cell=(10,11,12,85,95,100), space_group_symbol="P 1") miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=False, d_min=3) input_arrays = [miller_set.array( data=flex.random_double(size=miller_set.indices().size())) .set_observation_type_xray_amplitude() for i in [0,1]] mtz_dataset = input_arrays[0].as_mtz_dataset(column_root_label="F0") mtz_dataset.mtz_object().write("tmp_rfu1.mtz") reflection_files = [reflection_file_reader.any_reflection_file( file_name="tmp_rfu1.mtz")] reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files) ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=None, convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") assert str(ampl.info()) == "tmp_rfu1.mtz:F0" ampl = reflection_file_srv.get_miller_array(labels="F0") assert str(ampl.info()) == "tmp_rfu1.mtz:F0" mtz_dataset.add_miller_array( miller_array=input_arrays[1], column_root_label="F1") mtz_dataset.mtz_object().write("tmp_rfu2.mtz") reflection_files = [reflection_file_reader.any_reflection_file( file_name="tmp_rfu2.mtz")] err = StringIO() reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=err) try: reflection_file_srv.get_amplitudes( file_name=None, labels=None, convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") except Sorry: assert not show_diff(err.getvalue(), """\ Multiple equally suitable arrays of amplitudes found. Possible choices: tmp_rfu2.mtz:F0 tmp_rfu2.mtz:F1 Please use amplitudes.labels to specify an unambiguous substring of the target label. """) err = reflection_file_srv.err = StringIO() else: raise Exception_expected ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=["F1"], convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") assert str(ampl.info()) == "tmp_rfu2.mtz:F1" try: reflection_file_srv.get_amplitudes( file_name=None, labels=["F2"], convert_to_amplitudes_if_necessary=True, parameter_name="labels", parameter_scope=None) except Sorry: assert not show_diff(err.getvalue(), """\ No matching array: labels=F2 Possible choices: tmp_rfu2.mtz:F0 tmp_rfu2.mtz:F1 Please use labels to specify an unambiguous substring of the target label. """) err = reflection_file_srv.err = StringIO() else: raise Exception_expected assert len(reflection_file_srv.file_name_miller_arrays) == 1 ampl = reflection_file_srv.get_amplitudes( file_name="tmp_rfu1.mtz", labels=None, convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") assert len(reflection_file_srv.file_name_miller_arrays) == 2 assert str(ampl.info()) == "tmp_rfu1.mtz:F0" ampl = reflection_file_srv.get_amplitudes( file_name=os.path.abspath("tmp_rfu1.mtz"), labels=["f0"], convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") assert len(reflection_file_srv.file_name_miller_arrays) == 2 assert str(ampl.info()) == "tmp_rfu1.mtz:F0" try: reflection_file_srv.get_amplitudes( file_name=None, labels=None, convert_to_amplitudes_if_necessary=True, parameter_scope=None, parameter_name=None) except Sorry: assert not show_diff(err.getvalue(), """\ Multiple equally suitable arrays of amplitudes found. Possible choices: tmp_rfu2.mtz:F0 tmp_rfu2.mtz:F1 Please specify an unambiguous substring of the target label. """) err = reflection_file_srv.err = StringIO() else: raise Exception_expected # mtz_dataset.add_miller_array( miller_array=miller_set.array( data=flex.polar( flex.random_double(size=miller_set.indices().size()), flex.random_double(size=miller_set.indices().size()))), column_root_label="F2") mtz_dataset.add_miller_array( miller_array=miller_set.array( data=flex.random_double(size=miller_set.indices().size()), sigmas=flex.random_double(size=miller_set.indices().size())/10) .set_observation_type_xray_intensity(), column_root_label="F3") mtz_dataset.add_miller_array( miller_array=miller_set.array( data=flex.hendrickson_lattman(miller_set.indices().size(), (0,0,0,0))), column_root_label="P") mtz_dataset.mtz_object().write("tmp_rfu3.mtz") reflection_files = [reflection_file_reader.any_reflection_file( file_name="tmp_rfu3.mtz")] err = StringIO() reflection_file_srv = reflection_file_server( crystal_symmetry=crystal_symmetry, force_symmetry=True, reflection_files=reflection_files, err=err) ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=["f2"], convert_to_amplitudes_if_necessary=False, parameter_scope="amplitudes", parameter_name="labels") assert str(ampl.info()) == "tmp_rfu3.mtz:F2,PHIF2" assert ampl.is_complex_array() ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=["f2"], convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") assert str(ampl.info()) == "tmp_rfu3.mtz:F2" assert ampl.is_real_array() ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=["f3"], convert_to_amplitudes_if_necessary=False, parameter_scope="amplitudes", parameter_name="labels") assert str(ampl.info()) == "tmp_rfu3.mtz:F3,SIGF3" assert ampl.is_xray_intensity_array() ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=["f3"], convert_to_amplitudes_if_necessary=True, parameter_scope="amplitudes", parameter_name="labels") assert str(ampl.info()) == "tmp_rfu3.mtz:F3,as_amplitude_array" assert ampl.is_real_array() ampl = reflection_file_srv.get_amplitudes( file_name=None, labels=None, convert_to_amplitudes_if_necessary=False, parameter_scope="amplitudes", parameter_name="labels", return_all_valid_arrays=True, strict=True) assert (len(ampl) == 2) for f in ampl : assert (not f.is_xray_intensity_array()) and (not f.is_complex_array()) # phases = reflection_file_srv.get_phases_deg( file_name=None, labels=["f2"], convert_to_phases_if_necessary=False, original_phase_units=None, parameter_scope="phases", parameter_name="labels") assert str(phases.info()) == "tmp_rfu3.mtz:F2,PHIF2" assert phases.is_complex_array() phases = reflection_file_srv.get_phases_deg( file_name=None, labels=["f2"], convert_to_phases_if_necessary=True, original_phase_units=None, parameter_scope=None, parameter_name="labels") assert str(phases.info()) == "tmp_rfu3.mtz:PHIF2" assert phases.is_real_array() assert flex.mean(phases.data()) > 5 phases = reflection_file_srv.get_phases_deg( file_name=None, labels=["PA"], convert_to_phases_if_necessary=False, original_phase_units=None, parameter_scope="phases", parameter_name="labels") assert str(phases.info()) == "tmp_rfu3.mtz:PA,PB,PC,PD" phases = reflection_file_srv.get_phases_deg( file_name=None, labels=["PA"], convert_to_phases_if_necessary=True, original_phase_units=None, parameter_scope="phases", parameter_name="labels") assert str(phases.info()) \ == "tmp_rfu3.mtz:PA,PB,PC,PD,converted_to_centroid_phases" assert phases.is_real_array() for original_phase_units in [None, "deg", "rad"]: phases = reflection_file_srv.get_phases_deg( file_name=None, labels=["F0"], convert_to_phases_if_necessary=False, original_phase_units=original_phase_units, parameter_scope=None, parameter_name="labels") if (original_phase_units != "rad"): assert str(phases.info()) == "tmp_rfu3.mtz:F0" else: assert str(phases.info()) == "tmp_rfu3.mtz:F0,converted_to_deg"
def exercise_mmcif_structure_factors(): miller_arrays = cif.reader(input_string=r3adrsf).as_miller_arrays() assert len(miller_arrays) == 16 hl_coeffs = find_miller_array_from_labels( miller_arrays, ','.join([ 'scale_group_code=1', 'crystal_id=2', 'wavelength_id=3', '_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso' ])) assert hl_coeffs.is_hendrickson_lattman_array() assert hl_coeffs.size() == 2 mas_as_cif_block = cif.miller_arrays_as_cif_block( hl_coeffs, column_names=('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso')) abcd = [] for key in ('_refln.pdbx_HL_A_iso', '_refln.pdbx_HL_B_iso', '_refln.pdbx_HL_C_iso', '_refln.pdbx_HL_D_iso'): assert key in list(mas_as_cif_block.cif_block.keys()) abcd.append(flex.double(mas_as_cif_block.cif_block[key])) hl_coeffs_from_cif_block = flex.hendrickson_lattman(*abcd) assert approx_equal(hl_coeffs.data(), hl_coeffs_from_cif_block) f_meas_au = find_miller_array_from_labels( miller_arrays, ','.join([ 'scale_group_code=1', 'crystal_id=1', 'wavelength_id=1', '_refln.F_meas_au', '_refln.F_meas_sigma_au' ])) assert f_meas_au.is_xray_amplitude_array() assert f_meas_au.size() == 5 assert f_meas_au.sigmas() is not None assert f_meas_au.space_group_info().symbol_and_number( ) == 'C 1 2 1 (No. 5)' assert approx_equal(f_meas_au.unit_cell().parameters(), (163.97, 45.23, 110.89, 90.0, 131.64, 90.0)) pdbx_I_plus_minus = find_miller_array_from_labels( miller_arrays, ','.join([ '_refln.pdbx_I_plus', '_refln.pdbx_I_plus_sigma', '_refln.pdbx_I_minus', '_refln.pdbx_I_minus_sigma' ])) assert pdbx_I_plus_minus.is_xray_intensity_array() assert pdbx_I_plus_minus.anomalous_flag() assert pdbx_I_plus_minus.size() == 21 assert pdbx_I_plus_minus.unit_cell() is None # no symmetry information in assert pdbx_I_plus_minus.space_group() is None # this CIF block # miller_arrays = cif.reader(input_string=r3ad7sf).as_miller_arrays() assert len(miller_arrays) == 11 f_calc = find_miller_array_from_labels( miller_arrays, ','.join([ 'crystal_id=2', 'wavelength_id=1', '_refln.F_calc', '_refln.phase_calc' ])) assert f_calc.is_complex_array() assert f_calc.size() == 4 # miller_arrays = cif.reader( input_string=integer_observations).as_miller_arrays() assert len(miller_arrays) == 2 assert isinstance(miller_arrays[0].data(), flex.double) assert isinstance(miller_arrays[0].sigmas(), flex.double) # miller_arrays = cif.reader(input_string=r3v56sf).as_miller_arrays() assert len(miller_arrays) == 2 for ma in miller_arrays: assert ma.is_complex_array() assert miller_arrays[0].info().labels == [ 'r3v56sf', '_refln.pdbx_DELFWT', '_refln.pdbx_DELPHWT' ] assert miller_arrays[1].info().labels == [ 'r3v56sf', '_refln.pdbx_FWT', '_refln.pdbx_PHWT' ]
def format_miller_arrays(self, iparams): ''' Read in mtz file and format to miller_arrays_out object with index[0] --> FP, SIGFP index[1] --> PHIB index[2] --> FOM index[3] --> HLA, HLB, HLC, HLD index[4] --> optional PHIC ''' #readin reflection file reflection_file = reflection_file_reader.any_reflection_file(iparams.data) file_content=reflection_file.file_content() column_labels=file_content.column_labels() col_name=iparams.column_names.split(',') miller_arrays=reflection_file.as_miller_arrays() flex_centric_flags = miller_arrays[0].centric_flags().data() crystal_symmetry = crystal.symmetry( unit_cell=miller_arrays[0].unit_cell(), space_group=miller_arrays[0].space_group()) #grab all required columns flag_fp_found = 0 flag_phib_found = 0 flag_fom_found = 0 flag_hl_found = 0 ind_miller_array_fp = 0 ind_miller_array_phib = 0 ind_miller_array_fom = 0 ind_miller_array_hl = 0 for i in range(len(miller_arrays)): label_string = miller_arrays[i].info().label_string() labels=label_string.split(',') #only look at first index string if labels[0]==col_name[0]: #grab FP, SIGFP flex_fp_all=miller_arrays[i].data() flex_sigmas_all=miller_arrays[i].sigmas() flag_fp_found=1 ind_miller_array_fp = i elif labels[0]==col_name[2]: #grab PHIB flex_phib_all=miller_arrays[i].data() flag_phib_found=1 ind_miller_array_phib = i elif labels[0]==col_name[3]: #grab FOM flex_fom_all=miller_arrays[i].data() flag_fom_found=1 ind_miller_array_fom = i elif labels[0]==col_name[4]: #grab HLA,HLB,HLC,HLD flex_hl_all=miller_arrays[i].data() flag_hl_found=1 ind_miller_array_hl = i if flag_hl_found==1 and flag_phib_found == 0: #calculate PHIB and FOM from HL miller_array_phi_fom = miller_arrays[ind_miller_array_hl].phase_integrals() flex_phib_all = miller_array_phi_fom.phases(deg=True).data() flex_fom_all = miller_array_phi_fom.amplitudes().data() flag_phib_found = 1 flag_fom_found = 1 if flag_fp_found==0 or flag_phib_found==0 or flag_fom_found==0 or flag_hl_found==0: print "couldn't find all required columns" sys.exit() miller_indices_sel = miller_arrays[ind_miller_array_fp].indices() print 'No. reflections for read-in miller arrays - indices:%6.0f fp:%6.0f phib:%6.0f fom:%6.0f HL:%6.0f)'%( \ len(miller_indices_sel), len(flex_fp_all), len(flex_phib_all), len(flex_fom_all), len(flex_hl_all)) miller_indices = flex.miller_index() flex_fp = flex.double() flex_sigmas = flex.double() flex_phib = flex.double() flex_fom = flex.double() flex_hl = flex.hendrickson_lattman() #format all miller arrays to the same length for miller_index in miller_indices_sel: fp_cn, phib_cn, fom_cn, hl_cn = (0,0,0,0) matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_fp].indices()) if len(matches.pairs()) > 0: fp_cn = 1 fp = flex_fp_all[matches.pairs()[0][1]] sigmas = flex_sigmas_all[matches.pairs()[0][1]] matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_phib].indices()) if len(matches.pairs()) > 0: phib_cn = 1 phib = flex_phib_all[matches.pairs()[0][1]] matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_fom].indices()) if len(matches.pairs()) > 0: fom_cn = 1 fom = flex_fom_all[matches.pairs()[0][1]] matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_hl].indices()) if len(matches.pairs()) > 0: hl_cn = 1 hl = flex_hl_all[matches.pairs()[0][1]] if (fp_cn + phib_cn + fom_cn + hl_cn) == 4: miller_indices.append(miller_index) flex_fp.append(fp) flex_sigmas.append(sigmas) flex_phib.append(phib) flex_fom.append(fom) flex_hl.append(hl) print 'No. reflections after format - indices:%6.0f fp:%6.0f phib:%6.0f fom:%6.0f HL:%6.0f)'%( \ len(miller_indices), len(flex_fp), len(flex_phib), len(flex_fom), len(flex_hl)) flex_hla = flex.double() flex_hlb = flex.double() flex_hlc = flex.double() flex_hld = flex.double() for i in range(len(flex_hl)): data_hl_row=flex_hl[i] flex_hla.append(data_hl_row[0]) flex_hlb.append(data_hl_row[1]) flex_hlc.append(data_hl_row[2]) flex_hld.append(data_hl_row[3]) ''' Read benchmark MTZ (PHICalc) for MPE calculation ''' flex_phic = flex.double([0]*len(flex_fp)) if iparams.hklrefin is not None: reflection_file = reflection_file_reader.any_reflection_file(iparams.hklrefin) miller_arrays_bench=reflection_file.as_miller_arrays() flex_phic_raw = None for i in range(len(miller_arrays_bench)): label_string = miller_arrays_bench[i].info().label_string() labels=label_string.split(',') #only look at first index string if labels[0] == iparams.column_phic: #grab PHIC if miller_arrays_bench[i].is_complex_array(): flex_phic_raw = miller_arrays_bench[i].phases(deg=True).data() else: flex_phic_raw = miller_arrays_bench[i].data() miller_indices_phic = miller_arrays_bench[i].indices() if flex_phic is not None: matches = miller.match_multi_indices( miller_indices_unique=miller_indices, miller_indices=miller_indices_phic) flex_phic = flex.double([flex_phic_raw[pair[1]] for pair in matches.pairs()]) #format miller_arrays_out miller_set=miller.set( crystal_symmetry=crystal_symmetry, indices=miller_indices, anomalous_flag=False) miller_array_out = miller_set.array( data=flex_fp, sigmas=flex_sigmas).set_observation_type_xray_amplitude() #check if Wilson B-factor is applied flex_fp_for_sort = flex_fp[:] if iparams.flag_apply_b_factor: try: #get wilson_plot from mmtbx.scaling import xtriage from libtbx.utils import null_out xtriage_args = [ iparams.data, "", "", "log=tst_xtriage_1.log" ] result = xtriage.run(args=xtriage_args, out=null_out()) ws = result.wilson_scaling print 'Wilson K=%6.2f B=%6.2f'%(ws.iso_p_scale, ws.iso_b_wilson) sin_theta_over_lambda_sq = miller_array_out.two_theta(wavelength=iparams.wavelength) \ .sin_theta_over_lambda_sq().data() wilson_expect = flex.exp(-2 * ws.iso_b_wilson * sin_theta_over_lambda_sq) flex_fp_for_sort = wilson_expect * flex_fp except Exception: print 'Error calculating Wilson scale factors. Continue without applying B-factor.' flex_d_spacings = miller_array_out.d_spacings().data() mtz_dataset = miller_array_out.as_mtz_dataset(column_root_label="FP") for data,lbl,typ in [(flex_phib, "PHIB", "P"), (flex_fom, "FOMB", "W"), (flex_hla,"HLA","A"), (flex_hlb,"HLB","A"), (flex_hlc,"HLC","A"), (flex_hld,"HLD","A"), (flex_phic,"PHIC","P")]: mtz_dataset.add_miller_array(miller_array_out.array(data=data), column_root_label=lbl, column_types=typ) miller_arrays_out = mtz_dataset.mtz_object().as_miller_arrays() ''' getting sorted indices for the selected reflections in input mtz file list_fp_sort_index: stores indices of sorted FP in descending order ''' import operator fp_sort_index= [i for (i,j) in sorted(enumerate(flex_fp_for_sort), key=operator.itemgetter(1))] fp_sort_index.reverse() """ for i in range(100): print miller_indices[fp_sort_index[i]], flex_d_spacings[fp_sort_index[i]], flex_fp[fp_sort_index[i]], flex_sigmas[fp_sort_index[i]], wilson_expect[fp_sort_index[i]] exit() """ #calculate sum of fp^2 from percent_f_squared flex_fp_squared = flex_fp ** 2 f_squared_per_stack = (iparams.percent_f_squared * np.sum(flex_fp_squared))/100 fp_sort_index_stacks = [] sum_fp_now, i_start = (0,0) for i in range(len(fp_sort_index)): i_sel = fp_sort_index[i_start:i+1] sum_fp_now = np.sum([flex_fp_squared[ii_sel] for ii_sel in i_sel]) if sum_fp_now >= f_squared_per_stack: fp_sort_index_stacks.append(fp_sort_index[i_start:i+1]) i_start = i+1 if len(fp_sort_index_stacks) == iparams.n_stacks: break txt_out = 'stack_no sum(f_squared) %total n_refl\n' for i in range(len(fp_sort_index_stacks)): sum_fp = np.sum([flex_fp_squared[ii_sel] for ii_sel in fp_sort_index_stacks[i]]) txt_out += '%6.0f %14.2f %8.2f %6.0f\n'%(i+1, sum_fp, \ (sum_fp/np.sum(flex_fp_squared))*100, len(fp_sort_index_stacks[i])) return miller_arrays_out, fp_sort_index_stacks, txt_out
def exercise_miller_array_as_cns_hkl(): s = StringIO() crystal_symmetry = crystal.symmetry() for anomalous_flag in [False, True]: miller_set = miller.set( crystal_symmetry=crystal_symmetry, indices=flex.miller_index([(1,2,3),(-3,5,-7)]), anomalous_flag=anomalous_flag) for data in [flex.bool((False,True)), flex.int((-3,4)), flex.double((10,13)), flex.complex_double((10,13)), flex.hendrickson_lattman(((0,1,2,3),(4,5,6,7)))]: miller_array = miller_set.array(data=data) miller_array.export_as_cns_hkl(file_object=s) if (isinstance(data, flex.double)): miller_array = miller_set.array(data=data, sigmas=data/10.) miller_array.export_as_cns_hkl(file_object=s) assert not show_diff(s.getvalue(), """\ NREFlections=2 ANOMalous=FALSe DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= 0 INDEx -3 5 -7 DATA= 1 NREFlections=2 ANOMalous=FALSe DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= -3 INDEx -3 5 -7 DATA= 4 NREFlections=2 ANOMalous=FALSe DECLare NAME=DATA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 DATA= 10 INDEx -3 5 -7 DATA= 13 NREFlections=2 ANOMalous=FALSe DECLare NAME=FOBS DOMAin=RECIprocal TYPE=REAL END DECLare NAME=SIGMA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 FOBS= 10 SIGMA= 1 INDEx -3 5 -7 FOBS= 13 SIGMA= 1.3 NREFlections=2 ANOMalous=FALSe DECLare NAME=F DOMAin=RECIprocal TYPE=COMPLEX END INDEx 1 2 3 F= 10 0 INDEx -3 5 -7 F= 13 0 NREFlections=2 ANOMalous=FALSe DECLare NAME=PA DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PB DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PC DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PD DOMAin=RECIprocal TYPE=REAL END GROUp TYPE=HL OBJEct=PA OBJEct=PB OBJEct=PC OBJEct=PD END INDEx 1 2 3 PA= 0 PB= 1 PC= 2 PD= 3 INDEx -3 5 -7 PA= 4 PB= 5 PC= 6 PD= 7 NREFlections=2 ANOMalous=TRUE DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= 0 INDEx -3 5 -7 DATA= 1 NREFlections=2 ANOMalous=TRUE DECLare NAME=DATA DOMAin=RECIprocal TYPE=INTEger END INDEx 1 2 3 DATA= -3 INDEx -3 5 -7 DATA= 4 NREFlections=2 ANOMalous=TRUE DECLare NAME=DATA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 DATA= 10 INDEx -3 5 -7 DATA= 13 NREFlections=2 ANOMalous=TRUE DECLare NAME=FOBS DOMAin=RECIprocal TYPE=REAL END DECLare NAME=SIGMA DOMAin=RECIprocal TYPE=REAL END INDEx 1 2 3 FOBS= 10 SIGMA= 1 INDEx -3 5 -7 FOBS= 13 SIGMA= 1.3 NREFlections=2 ANOMalous=TRUE DECLare NAME=F DOMAin=RECIprocal TYPE=COMPLEX END INDEx 1 2 3 F= 10 0 INDEx -3 5 -7 F= 13 0 NREFlections=2 ANOMalous=TRUE DECLare NAME=PA DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PB DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PC DOMAin=RECIprocal TYPE=REAL END DECLare NAME=PD DOMAin=RECIprocal TYPE=REAL END GROUp TYPE=HL OBJEct=PA OBJEct=PB OBJEct=PC OBJEct=PD END INDEx 1 2 3 PA= 0 PB= 1 PC= 2 PD= 3 INDEx -3 5 -7 PA= 4 PB= 5 PC= 6 PD= 7 """)
def exercise_flex_hendrickson_lattman(): a = flex.hendrickson_lattman() assert a.size() == 0 a = flex.hendrickson_lattman(132) for x in a: assert x == (0,0,0,0) a = flex.hendrickson_lattman(((1,2,3,4), (2,3,4,5), (3,4,5,6))) assert a.size() == 3 assert a.count((1,2,3,4)) == 1 assert a.count((0,0,0,0)) == 0 assert tuple(a) == ((1,2,3,4), (2,3,4,5), (3,4,5,6)) assert tuple(a+a) == ((2,4,6,8), (4,6,8,10), (6,8,10,12)) a += a assert tuple(a) == ((2,4,6,8), (4,6,8,10), (6,8,10,12)) p = pickle.dumps(a) b = pickle.loads(p) assert tuple(a) == tuple(b) centric_flags = flex.bool([False, True]) phase_integrals = flex.complex_double([complex(0.5,-0.7), complex(-0.3,0.4)]) a = flex.hendrickson_lattman( centric_flags=centric_flags, phase_integrals=phase_integrals, max_figure_of_merit=1-1.e-6) assert approx_equal(a, [(2.2684820912654264, -3.1758749277715967, 0, 0), (-0.3295836866004328, 0.43944491546724396, 0, 0)]) assert approx_equal( [a.slice(i) for i in xrange(4)], [[2.2684820912654264, -0.3295836866004328], [-3.1758749277715967, 0.43944491546724396], [0.0, 0.0], [0.0, 0.0]]) a = flex.hendrickson_lattman(3, (1,2,3,4)) assert a.all_eq((1,2,3,4)) assert not a.all_eq((1,2,0,4)) assert approx_equal(a.conj(), [(1,-2,3,-4), (1,-2,3,-4), (1,-2,3,-4)]) assert approx_equal(a.conj().conj(), a) # a = flex.double() h = flex.hendrickson_lattman(a=a, b=a) assert h.size() == 0 a = flex.double([1,2,3]) b = flex.double([-3,4,5]) h = flex.hendrickson_lattman(a=a, b=b) assert approx_equal(h, [(1,-3,0,0), (2,4,0,0), (3,5,0,0)]) assert approx_equal(h == (1,-3,0,0), (True,False,False)) assert approx_equal(h != (1,-3,0,0), (False,True,True)) assert approx_equal(h != (0,0,0,0), (True,True,True)) assert approx_equal(h == h.deep_copy(), (True, True, True)) assert approx_equal( h == flex.hendrickson_lattman(a=b, b=a), (False, False, False)) assert approx_equal( h != flex.hendrickson_lattman(a=b, b=a), (True, True, True)) assert approx_equal( h != flex.hendrickson_lattman(a=b, b=a), (True, True, True)) assert approx_equal( h != h.deep_copy(), (False, False, False)) c = flex.double([4,5,6]) d = flex.double([-4,7,8]) h = flex.hendrickson_lattman(a=a, b=b, c=c, d=d) assert approx_equal(h, [(1,-3,4,-4), (2,4,5,7), (3,5,6,8)]) assert approx_equal(h.as_abcd(), [a, b, c, d]) h = h * 2 assert approx_equal(h, [(2, -6, 8, -8), (4, 8, 10, 14), (6, 10, 12, 16)])
mtzh = mtz_handler() miller_arrays, fp_sort_index_stacks, txt_out_format = mtzh.format_miller_arrays(iparams) print txt_out_format txt_out += txt_out_format for i in range(iparams.n_macro_cycles): txt_out += 'Macrocycle no. %4.0f\n'%(i+1) print 'Macrocycle no. %4.0f\n'%(i+1) for j in range(len(fp_sort_index_stacks)): #select the index group i_sel = fp_sort_index_stacks[j] #generate cdf_set for selected reflections from mmtbx.sisa.optimize.mod_optimize import sisa_optimizer somer = sisa_optimizer() hl_selected = flex.hendrickson_lattman([miller_arrays[3].data()[ii_sel] for ii_sel in i_sel]) cdf_set = somer.calc_pdf_cdf_from_hl(hl_selected) def sisa_optimize_mproc_wrapper(arg): return sisa_optimize_mproc(arg, j, miller_arrays, i_sel, cdf_set, iparams) sisa_optimize_results = pool_map( args=range(iparams.n_micro_cycles), func=sisa_optimize_mproc_wrapper, processes=iparams.n_processors) list_phis = [] foms_sum = None list_skews = [] for result in sisa_optimize_results: if result is not None:
def format_miller_arrays(self, iparams): ''' Read in mtz file and format to miller_arrays_out object with index[0] --> FP, SIGFP index[1] --> PHIB index[2] --> FOM index[3] --> HLA, HLB, HLC, HLD index[4] --> optional PHIC ''' #readin reflection file reflection_file = reflection_file_reader.any_reflection_file( iparams.data) file_content = reflection_file.file_content() column_labels = file_content.column_labels() col_name = iparams.column_names.split(',') miller_arrays = reflection_file.as_miller_arrays() flex_centric_flags = miller_arrays[0].centric_flags().data() crystal_symmetry = crystal.symmetry( unit_cell=miller_arrays[0].unit_cell(), space_group=miller_arrays[0].space_group()) #grab all required columns flag_fp_found = 0 flag_phib_found = 0 flag_fom_found = 0 flag_hl_found = 0 ind_miller_array_fp = 0 ind_miller_array_phib = 0 ind_miller_array_fom = 0 ind_miller_array_hl = 0 for i in range(len(miller_arrays)): label_string = miller_arrays[i].info().label_string() labels = label_string.split(',') #only look at first index string if labels[0] == col_name[0]: #grab FP, SIGFP flex_fp_all = miller_arrays[i].data() flex_sigmas_all = miller_arrays[i].sigmas() flag_fp_found = 1 ind_miller_array_fp = i elif labels[0] == col_name[2]: #grab PHIB flex_phib_all = miller_arrays[i].data() flag_phib_found = 1 ind_miller_array_phib = i elif labels[0] == col_name[3]: #grab FOM flex_fom_all = miller_arrays[i].data() flag_fom_found = 1 ind_miller_array_fom = i elif labels[0] == col_name[4]: #grab HLA,HLB,HLC,HLD flex_hl_all = miller_arrays[i].data() flag_hl_found = 1 ind_miller_array_hl = i if flag_hl_found == 1 and flag_phib_found == 0: #calculate PHIB and FOM from HL miller_array_phi_fom = miller_arrays[ ind_miller_array_hl].phase_integrals() flex_phib_all = miller_array_phi_fom.phases(deg=True).data() flex_fom_all = miller_array_phi_fom.amplitudes().data() flag_phib_found = 1 flag_fom_found = 1 if flag_fp_found == 0 or flag_phib_found == 0 or flag_fom_found == 0 or flag_hl_found == 0: print "couldn't find all required columns" sys.exit() miller_indices_sel = miller_arrays[ind_miller_array_fp].indices() print 'No. reflections for read-in miller arrays - indices:%6.0f fp:%6.0f phib:%6.0f fom:%6.0f HL:%6.0f)'%( \ len(miller_indices_sel), len(flex_fp_all), len(flex_phib_all), len(flex_fom_all), len(flex_hl_all)) miller_indices = flex.miller_index() flex_fp = flex.double() flex_sigmas = flex.double() flex_phib = flex.double() flex_fom = flex.double() flex_hl = flex.hendrickson_lattman() #format all miller arrays to the same length for miller_index in miller_indices_sel: fp_cn, phib_cn, fom_cn, hl_cn = (0, 0, 0, 0) matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_fp].indices()) if len(matches.pairs()) > 0: fp_cn = 1 fp = flex_fp_all[matches.pairs()[0][1]] sigmas = flex_sigmas_all[matches.pairs()[0][1]] matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_phib].indices()) if len(matches.pairs()) > 0: phib_cn = 1 phib = flex_phib_all[matches.pairs()[0][1]] matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_fom].indices()) if len(matches.pairs()) > 0: fom_cn = 1 fom = flex_fom_all[matches.pairs()[0][1]] matches = miller.match_multi_indices( miller_indices_unique=flex.miller_index([miller_index]), miller_indices=miller_arrays[ind_miller_array_hl].indices()) if len(matches.pairs()) > 0: hl_cn = 1 hl = flex_hl_all[matches.pairs()[0][1]] if (fp_cn + phib_cn + fom_cn + hl_cn) == 4: miller_indices.append(miller_index) flex_fp.append(fp) flex_sigmas.append(sigmas) flex_phib.append(phib) flex_fom.append(fom) flex_hl.append(hl) print 'No. reflections after format - indices:%6.0f fp:%6.0f phib:%6.0f fom:%6.0f HL:%6.0f)'%( \ len(miller_indices), len(flex_fp), len(flex_phib), len(flex_fom), len(flex_hl)) flex_hla = flex.double() flex_hlb = flex.double() flex_hlc = flex.double() flex_hld = flex.double() for i in range(len(flex_hl)): data_hl_row = flex_hl[i] flex_hla.append(data_hl_row[0]) flex_hlb.append(data_hl_row[1]) flex_hlc.append(data_hl_row[2]) flex_hld.append(data_hl_row[3]) ''' Read benchmark MTZ (PHICalc) for MPE calculation ''' flex_phic = flex.double([0] * len(flex_fp)) if iparams.hklrefin is not None: reflection_file = reflection_file_reader.any_reflection_file( iparams.hklrefin) miller_arrays_bench = reflection_file.as_miller_arrays() flex_phic_raw = None for i in range(len(miller_arrays_bench)): label_string = miller_arrays_bench[i].info().label_string() labels = label_string.split(',') #only look at first index string if labels[0] == iparams.column_phic: #grab PHIC if miller_arrays_bench[i].is_complex_array(): flex_phic_raw = miller_arrays_bench[i].phases( deg=True).data() else: flex_phic_raw = miller_arrays_bench[i].data() miller_indices_phic = miller_arrays_bench[i].indices() if flex_phic is not None: matches = miller.match_multi_indices( miller_indices_unique=miller_indices, miller_indices=miller_indices_phic) flex_phic = flex.double( [flex_phic_raw[pair[1]] for pair in matches.pairs()]) #format miller_arrays_out miller_set = miller.set(crystal_symmetry=crystal_symmetry, indices=miller_indices, anomalous_flag=False) miller_array_out = miller_set.array( data=flex_fp, sigmas=flex_sigmas).set_observation_type_xray_amplitude() #check if Wilson B-factor is applied flex_fp_for_sort = flex_fp[:] if iparams.flag_apply_b_factor: try: #get wilson_plot from mmtbx.scaling import xtriage from libtbx.utils import null_out xtriage_args = [iparams.data, "", "", "log=tst_xtriage_1.log"] result = xtriage.run(args=xtriage_args, out=null_out()) ws = result.wilson_scaling print 'Wilson K=%6.2f B=%6.2f' % (ws.iso_p_scale, ws.iso_b_wilson) sin_theta_over_lambda_sq = miller_array_out.two_theta(wavelength=iparams.wavelength) \ .sin_theta_over_lambda_sq().data() wilson_expect = flex.exp(-2 * ws.iso_b_wilson * sin_theta_over_lambda_sq) flex_fp_for_sort = wilson_expect * flex_fp except Exception: print 'Error calculating Wilson scale factors. Continue without applying B-factor.' flex_d_spacings = miller_array_out.d_spacings().data() mtz_dataset = miller_array_out.as_mtz_dataset(column_root_label="FP") for data, lbl, typ in [(flex_phib, "PHIB", "P"), (flex_fom, "FOMB", "W"), (flex_hla, "HLA", "A"), (flex_hlb, "HLB", "A"), (flex_hlc, "HLC", "A"), (flex_hld, "HLD", "A"), (flex_phic, "PHIC", "P")]: mtz_dataset.add_miller_array(miller_array_out.array(data=data), column_root_label=lbl, column_types=typ) miller_arrays_out = mtz_dataset.mtz_object().as_miller_arrays() ''' getting sorted indices for the selected reflections in input mtz file list_fp_sort_index: stores indices of sorted FP in descending order ''' import operator fp_sort_index = [ i for (i, j) in sorted(enumerate(flex_fp_for_sort), key=operator.itemgetter(1)) ] fp_sort_index.reverse() """ for i in range(100): print miller_indices[fp_sort_index[i]], flex_d_spacings[fp_sort_index[i]], flex_fp[fp_sort_index[i]], flex_sigmas[fp_sort_index[i]], wilson_expect[fp_sort_index[i]] exit() """ #calculate sum of fp^2 from percent_f_squared flex_fp_squared = flex_fp**2 f_squared_per_stack = (iparams.percent_f_squared * np.sum(flex_fp_squared)) / 100 fp_sort_index_stacks = [] sum_fp_now, i_start = (0, 0) for i in range(len(fp_sort_index)): i_sel = fp_sort_index[i_start:i + 1] sum_fp_now = np.sum([flex_fp_squared[ii_sel] for ii_sel in i_sel]) if sum_fp_now >= f_squared_per_stack: fp_sort_index_stacks.append(fp_sort_index[i_start:i + 1]) i_start = i + 1 if len(fp_sort_index_stacks) == iparams.n_stacks: break txt_out = 'stack_no sum(f_squared) %total n_refl\n' for i in range(len(fp_sort_index_stacks)): sum_fp = np.sum([ flex_fp_squared[ii_sel] for ii_sel in fp_sort_index_stacks[i] ]) txt_out += '%6.0f %14.2f %8.2f %6.0f\n'%(i+1, sum_fp, \ (sum_fp/np.sum(flex_fp_squared))*100, len(fp_sort_index_stacks[i])) return miller_arrays_out, fp_sort_index_stacks, txt_out
def __init__(self, cif_block, base_array_info=None, wavelengths=None): crystal_symmetry_builder.__init__(self, cif_block) if base_array_info is not None: self.crystal_symmetry = self.crystal_symmetry.join_symmetry( other_symmetry=base_array_info.crystal_symmetry_from_file, force=True) self._arrays = OrderedDict() if (wavelengths is None) : wavelengths = {} if base_array_info is None: base_array_info = miller.array_info(source_type="cif") refln_containing_loops = self.get_miller_indices_containing_loops() for self.indices, refln_loop in refln_containing_loops: self.wavelength_id_array = None self.crystal_id_array = None self.scale_group_array = None wavelength_ids = [None] crystal_ids = [None] scale_groups = [None] for key, value in refln_loop.iteritems(): # need to get these arrays first if (key.endswith('wavelength_id') or key.endswith('crystal_id') or key.endswith('scale_group_code')): data = as_int_or_none_if_all_question_marks(value, column_name=key) if data is None: continue counts = data.counts() if key.endswith('wavelength_id'): wavelength_ids = counts.keys() if len(counts) == 1: continue array = miller.array( miller.set(self.crystal_symmetry, self.indices).auto_anomalous(), data) if key.endswith('wavelength_id'): self.wavelength_id_array = array wavelength_ids = counts.keys() elif key.endswith('crystal_id'): self.crystal_id_array = array crystal_ids = counts.keys() elif key.endswith('scale_group_code'): self.scale_group_array = array scale_groups = counts.keys() for label, value in sorted(refln_loop.items()): for w_id in wavelength_ids: for crys_id in crystal_ids: for scale_group in scale_groups: if 'index_' in label: continue key = label labels = [label] wavelength = None if (key.endswith('wavelength_id') or key.endswith('crystal_id') or key.endswith('scale_group_code')): w_id = None crys_id = None scale_group = None key_suffix = '' if w_id is not None: key_suffix += '_%i' %w_id labels.insert(0, "wavelength_id=%i" %w_id) wavelength = wavelengths.get(w_id, None) if crys_id is not None: key_suffix += '_%i' %crys_id labels.insert(0, "crystal_id=%i" %crys_id) if scale_group is not None: key_suffix += '_%i' %scale_group labels.insert(0, "scale_group_code=%i" %scale_group) key += key_suffix sigmas = None if key in self._arrays: continue array = self.flex_std_string_as_miller_array( value, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) if array is None: continue if '_sigma' in key: sigmas_label = label key = None for suffix in ('', '_meas', '_calc'): if sigmas_label.replace('_sigma', suffix) in refln_loop: key = sigmas_label.replace('_sigma', suffix) + key_suffix break if key is None: key = sigmas_label + key_suffix elif key in self._arrays and self._arrays[key].sigmas() is None: sigmas = array array = self._arrays[key] check_array_sizes(array, sigmas, key, sigmas_label) sigmas = as_flex_double(sigmas, sigmas_label) array.set_sigmas(sigmas.data()) info = array.info() array.set_info( info.customized_copy(labels=info.labels+[sigmas_label], wavelength=wavelength)) continue elif 'PHWT' in key: phwt_label = label fwt_label = label.replace('PHWT', 'FWT') if fwt_label not in refln_loop: continue phwt_array = array if fwt_label in self._arrays: array = self._arrays[fwt_label] check_array_sizes(array, phwt_array, fwt_label, phwt_label) phases = as_flex_double(phwt_array, phwt_label) info = array.info() array = array.phase_transfer(phases, deg=True) array.set_info( info.customized_copy(labels=info.labels+[phwt_label])) self._arrays[fwt_label] = array continue elif 'HL_' in key: hl_letter = key[key.find('HL_')+3] hl_key = 'HL_' + hl_letter key = key.replace(hl_key, 'HL_A') if key in self._arrays: continue # this array is already dealt with hl_labels = [label.replace(hl_key, 'HL_'+letter) for letter in 'ABCD'] hl_keys = [key.replace(hl_key, 'HL_'+letter) for letter in 'ABCD'] hl_values = [cif_block.get(hl_key) for hl_key in hl_labels] if hl_values.count(None) == 0: selection = self.get_selection( hl_values[0], wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) hl_values = [as_double_or_none_if_all_question_marks( hl.select(selection), column_name=lab) for hl, lab in zip(hl_values, hl_labels)] array = miller.array(miller.set( self.crystal_symmetry, self.indices.select(selection) ).auto_anomalous(), flex.hendrickson_lattman(*hl_values)) labels = labels[:-1]+hl_labels elif '.B_' in key or '_B_' in key: if '.B_' in key: key, key_b = key.replace('.B_', '.A_'), key label, label_b = label.replace('.B_', '.A_'), label elif '_B_' in key: key, key_b = key.replace('_B', '_A'), key label, label_b = label.replace('_B', '_A'), label if key in refln_loop and key_b in refln_loop: b_part = array.data() if key in self._arrays: info = self._arrays[key].info() a_part = self._arrays[key].data() self._arrays[key] = self._arrays[key].array( data=flex.complex_double(a_part, b_part)) self._arrays[key].set_info( info.customized_copy(labels=info.labels+[key_b])) continue elif ('phase_' in key and not "_meas" in key and self.crystal_symmetry.space_group() is not None): alt_key1 = label.replace('phase_', 'F_') alt_key2 = alt_key1 + '_au' if alt_key1 in refln_loop: phase_key = label key = alt_key1+key_suffix elif alt_key2 in refln_loop: phase_key = label key = alt_key2+key_suffix else: phase_key = None if phase_key is not None: phases = array.data() if key in self._arrays: array = self._arrays[key] array = as_flex_double(array, key) check_array_sizes(array, phases, key, phase_key) info = self._arrays[key].info() self._arrays[key] = array.phase_transfer(phases, deg=True) self._arrays[key].set_info( info.customized_copy(labels=info.labels+[phase_key])) else: array = self.flex_std_string_as_miller_array( refln_loop[label], wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) check_array_sizes(array, phases, key, phase_key) array.phase_transfer(phases, deg=True) labels = labels+[label, phase_key] if base_array_info.labels is not None: labels = base_array_info.labels + labels def rstrip_substrings(string, substrings): for substr in substrings: if substr == '': continue if string.endswith(substr): string = string[:-len(substr)] return string # determine observation type stripped_key = rstrip_substrings( key, [key_suffix, '_au', '_meas', '_calc', '_plus', '_minus']) if (stripped_key.endswith('F_squared') or stripped_key.endswith('intensity') or stripped_key.endswith('.I') or stripped_key.endswith('_I')) and ( array.is_real_array() or array.is_integer_array()): array.set_observation_type_xray_intensity() elif (stripped_key.endswith('F') and ( array.is_real_array() or array.is_integer_array())): array.set_observation_type_xray_amplitude() if (array.is_xray_amplitude_array() or array.is_xray_amplitude_array()): # e.g. merge_equivalents treats integer arrays differently, so must # convert integer observation arrays here to be safe if isinstance(array.data(), flex.int): array = array.customized_copy(data=array.data().as_double()) array.set_info(base_array_info.customized_copy(labels=labels)) if (array.is_xray_amplitude_array() or array.is_xray_amplitude_array()): info = array.info() array.set_info(info.customized_copy(wavelength=wavelength)) self._arrays.setdefault(key, array) for key, array in self._arrays.copy().iteritems(): if ( key.endswith('_minus') or '_minus_' in key or key.endswith('_plus') or '_plus_' in key): if '_minus' in key: minus_key = key plus_key = key.replace('_minus', '_plus') elif '_plus' in key: plus_key = key minus_key = key.replace('_plus', '_minus') if plus_key in self._arrays and minus_key in self._arrays: plus_array = self._arrays.pop(plus_key) minus_array = self._arrays.pop(minus_key) minus_array = minus_array.customized_copy( indices=-minus_array.indices()).set_info(minus_array.info()) array = plus_array.concatenate( minus_array, assert_is_similar_symmetry=False) array = array.customized_copy(anomalous_flag=True) array.set_info(minus_array.info().customized_copy( labels=list( OrderedSet(plus_array.info().labels+minus_array.info().labels)))) array.set_observation_type(plus_array.observation_type()) self._arrays.setdefault(key, array) if len(self._arrays) == 0: raise CifBuilderError("No reflection data present in cif block")
def compute_map_coefficients(self): f_obs = self.f_obs_complete.select( self.f_obs_complete.d_spacings().data() >= self.d_min) f_calc = f_obs.structure_factors_from_map(self.map, use_sg=True) f_obs_active = f_obs.select_indices(self.active_indices) minimized = relative_scaling.ls_rel_scale_driver( f_obs_active, f_calc.as_amplitude_array().select_indices(self.active_indices), use_intensities=False, use_weights=False) #minimized.show() f_calc = f_calc.customized_copy(data=f_calc.data()\ * math.exp(-minimized.p_scale)\ * adptbx.debye_waller_factor_u_star( f_calc.indices(), minimized.u_star)) f_calc_active = f_calc.common_set(f_obs_active) matched_indices = f_obs.match_indices(self.f_obs_active) lone_indices_selection = matched_indices.single_selection(0) from mmtbx.max_lik import maxlik alpha_beta_est = maxlik.alpha_beta_est_manager( f_obs=f_obs_active, f_calc=f_calc_active, free_reflections_per_bin=140, flags=flex.bool(f_obs_active.size()), interpolation=True, epsilons=f_obs_active.epsilons().data().as_double()) alpha, beta = alpha_beta_est.alpha_beta_for_each_reflection( f_obs=self.f_obs_complete.select( self.f_obs_complete.d_spacings().data() >= self.d_min)) f_obs.data().copy_selected(lone_indices_selection.iselection(), flex.abs(f_calc.data())) t = maxlik.fo_fc_alpha_over_eps_beta(f_obs=f_obs, f_model=f_calc, alpha=alpha, beta=beta) hl_coeff = flex.hendrickson_lattman( t * flex.cos(f_calc.phases().data()), t * flex.sin(f_calc.phases().data())) dd = alpha.data() # hl_array = f_calc.array( data=self.hl_coeffs_start.common_set(f_calc).data() + hl_coeff) self.compute_phase_source(hl_array) fom = flex.abs(self.phase_source.data()) mFo = hl_array.array(data=f_obs.data() * self.phase_source.data()) DFc = hl_array.array(data=dd * f_calc.as_amplitude_array().phase_transfer( self.phase_source).data()) centric_flags = f_obs.centric_flags().data() acentric_flags = ~centric_flags fo_scale = flex.double(centric_flags.size()) fc_scale = flex.double(centric_flags.size()) fo_scale.set_selected(acentric_flags, 2) fo_scale.set_selected(centric_flags, 1) fc_scale.set_selected(acentric_flags, 1) fc_scale.set_selected(centric_flags, 0) fo_scale.set_selected(lone_indices_selection, 0) fc_scale.set_selected(lone_indices_selection, -1) self.map_coeffs = hl_array.array(data=mFo.data() * fo_scale - DFc.data() * fc_scale) self.fom = hl_array.array(data=fom) self.hl_coeffs = hl_array # statistics self.r1_factor = f_obs_active.r1_factor(f_calc_active) fom = fom.select(matched_indices.pair_selection(0)) self.r1_factor_fom = flex.sum( fom * flex.abs(f_obs_active.data() - f_calc_active.as_amplitude_array().data())) \ / flex.sum(fom * f_obs_active.data()) phase_source, phase_source_previous = self.phase_source.common_sets( self.phase_source_previous) self.mean_delta_phi = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_previous.data())) phase_source, phase_source_initial = self.phase_source.common_sets( self.phase_source_initial) self.mean_delta_phi_initial = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_initial.data())) self.mean_fom = flex.mean(fom) fom = f_obs_active.array(data=fom) if fom.data().size() < 1000: # 2013-12-14 was hard-wired at 1000 tt reflections_per_bin = fom.data().size() else: reflections_per_bin = 1000 fom.setup_binner(reflections_per_bin=reflections_per_bin) self.mean_fom_binned = fom.mean(use_binning=True)
def compute_map_coefficients(self): f_obs = self.f_obs_complete.select(self.f_obs_complete.d_spacings().data() >= self.d_min) f_calc = f_obs.structure_factors_from_map(self.map, use_sg=True) f_obs_active = f_obs.select_indices(self.active_indices) minimized = relative_scaling.ls_rel_scale_driver( f_obs_active, f_calc.as_amplitude_array().select_indices(self.active_indices), use_intensities=False, use_weights=False) #minimized.show() f_calc = f_calc.customized_copy(data=f_calc.data()\ * math.exp(-minimized.p_scale)\ * adptbx.debye_waller_factor_u_star( f_calc.indices(), minimized.u_star)) f_calc_active = f_calc.common_set(f_obs_active) matched_indices = f_obs.match_indices(self.f_obs_active) lone_indices_selection = matched_indices.single_selection(0) from mmtbx.max_lik import maxlik alpha_beta_est = maxlik.alpha_beta_est_manager( f_obs=f_obs_active, f_calc=f_calc_active, free_reflections_per_bin=140, flags=flex.bool(f_obs_active.size()), interpolation=True, epsilons=f_obs_active.epsilons().data().as_double()) alpha, beta = alpha_beta_est.alpha_beta_for_each_reflection( f_obs=self.f_obs_complete.select(self.f_obs_complete.d_spacings().data() >= self.d_min)) f_obs.data().copy_selected( lone_indices_selection.iselection(), flex.abs(f_calc.data())) t = maxlik.fo_fc_alpha_over_eps_beta( f_obs=f_obs, f_model=f_calc, alpha=alpha, beta=beta) hl_coeff = flex.hendrickson_lattman( t * flex.cos(f_calc.phases().data()), t * flex.sin(f_calc.phases().data())) dd = alpha.data() # hl_array = f_calc.array( data=self.hl_coeffs_start.common_set(f_calc).data()+hl_coeff) self.compute_phase_source(hl_array) fom = flex.abs(self.phase_source.data()) mFo = hl_array.array(data=f_obs.data()*self.phase_source.data()) DFc = hl_array.array(data=dd*f_calc.as_amplitude_array().phase_transfer( self.phase_source).data()) centric_flags = f_obs.centric_flags().data() acentric_flags = ~centric_flags fo_scale = flex.double(centric_flags.size()) fc_scale = flex.double(centric_flags.size()) fo_scale.set_selected(acentric_flags, 2) fo_scale.set_selected(centric_flags, 1) fc_scale.set_selected(acentric_flags, 1) fc_scale.set_selected(centric_flags, 0) fo_scale.set_selected(lone_indices_selection, 0) fc_scale.set_selected(lone_indices_selection, -1) self.map_coeffs = hl_array.array( data=mFo.data()*fo_scale - DFc.data()*fc_scale) self.fom = hl_array.array(data=fom) self.hl_coeffs = hl_array # statistics self.r1_factor = f_obs_active.r1_factor(f_calc_active) fom = fom.select(matched_indices.pair_selection(0)) self.r1_factor_fom = flex.sum( fom * flex.abs(f_obs_active.data() - f_calc_active.as_amplitude_array().data())) \ / flex.sum(fom * f_obs_active.data()) phase_source, phase_source_previous = self.phase_source.common_sets( self.phase_source_previous) self.mean_delta_phi = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_previous.data())) phase_source, phase_source_initial = self.phase_source.common_sets( self.phase_source_initial) self.mean_delta_phi_initial = phase_error( flex.arg(phase_source.data()), flex.arg(phase_source_initial.data())) self.mean_fom = flex.mean(fom) fom = f_obs_active.array(data=fom) if fom.data().size()<1000: # 2013-12-14 was hard-wired at 1000 tt reflections_per_bin=fom.data().size() else: reflections_per_bin=1000 fom.setup_binner(reflections_per_bin=reflections_per_bin) self.mean_fom_binned = fom.mean(use_binning=True)
def __init__(self, cif_block, base_array_info=None, wavelengths=None): crystal_symmetry_builder.__init__(self, cif_block) self._arrays = OrderedDict() self._origarrays = OrderedDict( ) # used for presenting raw data tables in HKLviewer basearraylabels = [] if base_array_info is not None: self.crystal_symmetry = self.crystal_symmetry.join_symmetry( other_symmetry=base_array_info.crystal_symmetry_from_file, force=True) if base_array_info.labels: basearraylabels = base_array_info.labels if (wavelengths is None): wavelengths = {} if base_array_info is None: base_array_info = miller.array_info(source_type="cif") refln_containing_loops = self.get_miller_indices_containing_loops() for self.indices, refln_loop in refln_containing_loops: self.wavelength_id_array = None self.crystal_id_array = None self.scale_group_array = None wavelength_ids = [None] crystal_ids = [None] scale_groups = [None] for key, value in six.iteritems(refln_loop): # Get wavelength_ids, crystal_id, scale_group_code columns for selecting data of other # columns in self.get_selection() used by self.flex_std_string_as_miller_array() if (key.endswith('wavelength_id') or key.endswith('crystal_id') or key.endswith('scale_group_code')): data = as_int_or_none_if_all_question_marks( value, column_name=key) if data is None: continue counts = data.counts() if key.endswith('wavelength_id'): wavelength_ids = list(counts.keys()) if len(counts) == 1: continue array = miller.array( miller.set(self.crystal_symmetry, self.indices).auto_anomalous(), data) if key.endswith('wavelength_id'): self.wavelength_id_array = array wavelength_ids = list(counts.keys()) elif key.endswith('crystal_id'): self.crystal_id_array = array crystal_ids = list(counts.keys()) elif key.endswith('scale_group_code'): self.scale_group_array = array scale_groups = list(counts.keys()) labelsuffix = [] wavelbl = [] cryslbl = [] scalegrplbl = [] self._origarrays["HKLs"] = self.indices alllabels = list(sorted(refln_loop.keys())) remaininglabls = alllabels[:] # deep copy the list # Parse labels matching cif column conventions # https://mmcif.wwpdb.org/dictionaries/mmcif_pdbx_v50.dic/Categories/refln.html # and extract groups of labels or just single columns. # Groups corresponds to the map coefficients, phase and amplitudes, # amplitudes or intensities with sigmas and hendrickson-lattman columns. phaseamplabls, remaininglabls = self.get_phase_amplitude_labels( remaininglabls) mapcoefflabls, remaininglabls = self.get_mapcoefficient_labels( remaininglabls) HLcoefflabls, remaininglabls = self.get_HL_labels(remaininglabls) data_sig_obstype_labls, remaininglabls = self.get_FSigF_ISigI_labels( remaininglabls) for w_id in wavelength_ids: for crys_id in crystal_ids: for scale_group in scale_groups: # If reflection data files contain more than one crystal, wavelength or scalegroup # then add their id(s) as a suffix to data labels computed below. Needed for avoiding # ambuguity but avoid when not needed to make labels more human readable! if (len(wavelength_ids) > 1 or len(wavelengths) > 1) and w_id is not None: wavelbl = ["wavelength_id=%i" % w_id] if len(crystal_ids) > 1 and crys_id is not None: cryslbl = ["crystal_id=%i" % crys_id] if len(scale_groups) > 1 and scale_group is not None: scalegrplbl = ["scale_group_code=%i" % scale_group] labelsuffix = scalegrplbl + cryslbl + wavelbl jlablsufx = "" if len(labelsuffix): jlablsufx = "," + ",".join(labelsuffix) for mapcoefflabl in mapcoefflabls: A_array = refln_loop[mapcoefflabl[0]] B_array = refln_loop[mapcoefflabl[1]] # deselect any ? marks in the two arrays, assuming both A and B have the same ? marks selection = self.get_selection( A_array, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) A_array = A_array.select(selection) B_array = B_array.select(selection) # form the miller array with map coefficients data = flex.complex_double(flex.double(A_array), flex.double(B_array)) millarr = miller.array( miller.set(self.crystal_symmetry, self.indices.select( selection)).auto_anomalous(), data) # millarr will be None for column data not matching w_id,crys_id,scale_group values if millarr is None: continue labl = basearraylabels + mapcoefflabl + labelsuffix millarr.set_info( base_array_info.customized_copy( labels=labl, wavelength=wavelengths.get(w_id, None))) self._arrays[mapcoefflabl[0] + jlablsufx] = millarr for phaseamplabl in phaseamplabls: amplitudestrarray = refln_loop[phaseamplabl[0]] phasestrarray = refln_loop[phaseamplabl[1]] millarr = self.flex_std_string_as_miller_array( amplitudestrarray, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) phasesmillarr = self.flex_std_string_as_miller_array( phasestrarray, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) # millarr will be None for column data not matching w_id,crys_id,scale_group values if millarr is None or phasesmillarr is None: continue phases = as_flex_double(phasesmillarr, phaseamplabl[1]) millarr = millarr.phase_transfer(phases, deg=True) labl = basearraylabels + phaseamplabl + labelsuffix millarr.set_info( base_array_info.customized_copy( labels=labl, wavelength=wavelengths.get(w_id, None))) self._arrays[phaseamplabl[0] + jlablsufx] = millarr for datlabl, siglabl, otype in data_sig_obstype_labls: datastrarray = refln_loop[datlabl] millarr = self.flex_std_string_as_miller_array( datastrarray, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) # millarr will be None for column data not matching w_id,crys_id,scale_group values if millarr is None: continue millarr = as_flex_double(millarr, datlabl) datsiglabl = [datlabl] if siglabl: sigmasstrarray = refln_loop[siglabl] sigmas = self.flex_std_string_as_miller_array( sigmasstrarray, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) sigmas = as_flex_double(sigmas, siglabl) millarr.set_sigmas(sigmas.data()) datsiglabl = [datlabl, siglabl] datsiglabl = basearraylabels + datsiglabl + labelsuffix millarr.set_info( base_array_info.customized_copy( labels=datsiglabl, wavelength=wavelengths.get(w_id, None))) if otype is not None: millarr.set_observation_type(otype) self._arrays[datlabl + jlablsufx] = millarr for hl_labels in HLcoefflabls: hl_values = [ cif_block.get(hl_key) for hl_key in hl_labels ] if hl_values.count(None) == 0: selection = self.get_selection( hl_values[0], wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) hl_values = [ as_double_or_none_if_all_question_marks( hl.select(selection), column_name=lab) for hl, lab in zip(hl_values, hl_labels) ] # hl_values will be None for column data not matching w_id,crys_id,scale_group values if hl_values == [None, None, None, None]: continue millarr = miller.array( miller.set( self.crystal_symmetry, self.indices.select( selection)).auto_anomalous(), flex.hendrickson_lattman(*hl_values)) hlabels = basearraylabels + hl_labels + labelsuffix millarr.set_info( base_array_info.customized_copy( labels=hlabels, wavelength=wavelengths.get(w_id, None))) self._arrays[hl_labels[0] + jlablsufx] = millarr # pick up remaining columns if any that weren't identified above for label in alllabels: if "index_" in label: continue datastrarray = refln_loop[label] if label in remaininglabls: labels = basearraylabels + [label ] + labelsuffix lablsufx = jlablsufx millarr = self.flex_std_string_as_miller_array( datastrarray, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) # millarr will be None for column data not matching w_id,crys_id,scale_group values if (label.endswith( 'wavelength_id' ) or label.endswith( 'crystal_id' ) or # get full array if any of these labels, not just subsets label.endswith('scale_group_code')): millarr = self.flex_std_string_as_miller_array( datastrarray, wavelength_id=None, crystal_id=None, scale_group_code=None) lablsufx = "" labels = basearraylabels + [label] if millarr is None: continue otype = self.guess_observationtype(label) if otype is not None: millarr.set_observation_type(otype) millarr.set_info( base_array_info.customized_copy( labels=labels, wavelength=wavelengths.get(w_id, None))) self._arrays[label + lablsufx] = millarr origarr = self.flex_std_string_as_miller_array( datastrarray, wavelength_id=w_id, crystal_id=crys_id, scale_group_code=scale_group) newlabel = label.replace("_refln.", "") newlabel2 = newlabel.replace("_refln_", "") if origarr: # want only genuine miller arrays self._origarrays[newlabel2 + jlablsufx] = origarr.data() # Convert any groups of I+,I-,SigI+,SigI- (or amplitudes) arrays into anomalous arrays # i.e. both friedel mates in the same array for key, array in six.iteritems(self._arrays.copy()): plus_key = "" if '_minus' in key: minus_key = key plus_key = key.replace('_minus', '_plus') elif '-' in key: minus_key = key plus_key = key.replace('-', '+') elif '_plus' in key: plus_key = key minus_key = key.replace('_plus', '_minus') elif '+' in key: plus_key = key minus_key = key.replace('+', '-') if plus_key in self._arrays and minus_key in self._arrays: plus_array = self._arrays.pop(plus_key) minus_array = self._arrays.pop(minus_key) minus_array = minus_array.customized_copy( indices=-minus_array.indices()).set_info( minus_array.info()) array = plus_array.concatenate( minus_array, assert_is_similar_symmetry=False) array = array.customized_copy(anomalous_flag=True) array.set_info(minus_array.info().customized_copy(labels=list( OrderedSet(plus_array.info().labels + minus_array.info().labels)))) array.set_observation_type(plus_array.observation_type()) self._arrays.setdefault(key, array) if len(self._arrays) == 0: raise CifBuilderError("No reflection data present in cif block") # Sort the ordered dictionary to resemble the order of columns in the cif file # This is to avoid any F_meas arrays accidentally being put adjacent to # pdbx_anom_difference arrays in the self._arrays OrderedDict. Otherwise these # arrays may unintentionally be combined into a reconstructed anomalous amplitude # array when saving as an mtz file due to a problem in the iotbx/mtz module. # See http://phenix-online.org/pipermail/cctbxbb/2021-March/002289.html arrlstord = [] arrlst = list(self._arrays) for arr in arrlst: for i, k in enumerate(refln_loop.keys()): if arr.split(",")[0] == k: arrlstord.append((arr, i)) # arrlstord must have the same keys as in the self._arrays dictionary assert sorted(arrlst) == sorted([e[0] for e in arrlstord]) sortarrlst = sorted(arrlstord, key=lambda arrord: arrord[1]) self._ordarrays = OrderedDict() for sortkey, i in sortarrlst: self._ordarrays.setdefault(sortkey, self._arrays[sortkey]) self._arrays = self._ordarrays