def as_miller_arrays(self, crystal_symmetry=None, force_symmetry=False, merge_equivalents=True, base_array_info=None, anomalous=None): from cctbx import miller from cctbx import crystal if (crystal_symmetry is None): crystal_symmetry = crystal.symmetry() if (base_array_info is None): base_array_info = miller.array_info(source_type="shelx_hklf") miller_set = miller.set(crystal_symmetry=crystal_symmetry, indices=self.indices(), anomalous_flag=anomalous) if anomalous is None: miller_set = miller_set.auto_anomalous() miller_arrays = [] obs = (miller.array( miller_set=miller_set, data=self.data(), sigmas=self.sigmas()).set_info( base_array_info.customized_copy(labels=["obs", "sigmas"]))) miller_arrays.append(obs) if (self.alphas() is not None): miller_arrays.append( miller.array( miller_set=miller_set, data=self.alphas()).set_info( base_array_info.customized_copy(labels=["alphas"]))) return miller_arrays
def phase_transfer(miller_array, phase_source): tmp = miller.array( miller_set=miller_array, data=flex.double(miller_array.indices().size(), 1)).phase_transfer(phase_source=phase_source) return miller.array(miller_set=miller_array, data=miller_array.data() * tmp.data())
def calculate_cc_int(self, odd_reflections, even_reflections): odd_reflections_merged = reflection_table_utils.merge_reflections( odd_reflections, self.params.merging.minimum_multiplicity) even_reflections_merged = reflection_table_utils.merge_reflections( even_reflections, self.params.merging.minimum_multiplicity) # Create target symmetry if self.params.merging.set_average_unit_cell: assert 'average_unit_cell' in (self.params.statistics).__dict__ unit_cell = self.params.statistics.__phil_get__( 'average_unit_cell') else: unit_cell = self.params.scaling.unit_cell target_symm = symmetry( unit_cell=unit_cell, space_group_info=self.params.scaling.space_group) # Build miller arrays miller_indices_odd = miller.set(target_symm, odd_reflections_merged['miller_index'], True) intensities_odd = miller.array( miller_indices_odd, odd_reflections_merged['intensity'], flex.double(odd_reflections_merged.size(), 1.0)) miller_indices_even = miller.set( target_symm, even_reflections_merged['miller_index'], True) intensities_even = miller.array( miller_indices_even, even_reflections_merged['intensity'], flex.double(even_reflections_merged.size(), 1.0)) # Calculate crosss-correlation self.Total_CC_OneHalf_Table = self.calculate_cross_correlation( intensities_odd, intensities_even)
def reflection_tables_to_batch_dependent_properties(reflection_tables, experiments, scaled_array=None): """Extract batch dependent properties from a reflection table list.""" offsets = calculate_batch_offsets(experiments) reflection_tables = assign_batches_to_reflections(reflection_tables, offsets) # filter bad refls and negative scales batches = flex.int() scales = flex.double() for r in reflection_tables: sel = ~r.get_flags(r.flags.bad_for_scaling, all=False) sel &= r["inverse_scale_factor"] > 0 batches.extend(r["batch"].select(sel)) scales.extend(r["inverse_scale_factor"].select(sel)) if not scaled_array: scaled_array = scaled_data_as_miller_array(reflection_tables, experiments) ms = scaled_array.customized_copy() batch_array = miller.array(ms, data=batches) batch_ranges = get_batch_ranges(experiments, offsets) batch_data = [{"id": i, "range": r} for i, r in enumerate(batch_ranges)] properties = batch_dependent_properties(batch_array, scaled_array, miller.array(ms, data=scales)) return properties + (batch_data, )
def as_miller_arrays(self, crystal_symmetry=None, force_symmetry=False, merge_equivalents=True, base_array_info=None): from cctbx import miller from cctbx import crystal if (crystal_symmetry is None): crystal_symmetry = crystal.symmetry() if (base_array_info is None): base_array_info = miller.array_info(source_type="shelx_hklf") miller_set = miller.set( crystal_symmetry=crystal_symmetry, indices=self.indices()).auto_anomalous() miller_arrays = [] obs = (miller.array( miller_set=miller_set, data=self.data(), sigmas=self.sigmas()) .set_info(base_array_info.customized_copy(labels=["obs", "sigmas"]))) miller_arrays.append(obs) if (self.alphas() is not None): miller_arrays.append(miller.array( miller_set=miller_set, data=self.alphas()) .set_info(base_array_info.customized_copy(labels=["alphas"]))) return miller_arrays
def from_reflections_and_experiments(cls, reflection_tables, experiments, params): """Construct the resolutionizer from native dials datatypes.""" # add some assertions about data # do batch assignment (same functions as in dials.export) offsets = calculate_batch_offsets(experiments) reflection_tables = assign_batches_to_reflections( reflection_tables, offsets) batches = flex.int() intensities = flex.double() indices = flex.miller_index() variances = flex.double() for table in reflection_tables: if "intensity.scale.value" in table: table = filter_reflection_table(table, ["scale"], partiality_threshold=0.4) intensities.extend(table["intensity.scale.value"]) variances.extend(table["intensity.scale.variance"]) else: table = filter_reflection_table(table, ["profile"], partiality_threshold=0.4) intensities.extend(table["intensity.prf.value"]) variances.extend(table["intensity.prf.variance"]) indices.extend(table["miller_index"]) batches.extend(table["batch"]) crystal_symmetry = miller.crystal.symmetry( unit_cell=determine_best_unit_cell(experiments), space_group=experiments[0].crystal.get_space_group(), assert_is_compatible_unit_cell=False, ) miller_set = miller.set(crystal_symmetry, indices, anomalous_flag=False) i_obs = miller.array(miller_set, data=intensities, sigmas=flex.sqrt(variances)) i_obs.set_observation_type_xray_intensity() i_obs.set_info(miller.array_info(source="DIALS", source_type="refl")) ms = i_obs.customized_copy() batch_array = miller.array(ms, data=batches) if params.reference is not None: reference, _ = miller_array_from_mtz(params.reference, anomalous=params.anomalous, labels=params.labels) else: reference = None return cls(i_obs, params, batches=batch_array, reference=reference)
def _get_unique_Hi(self): COMM.barrier() if COMM.rank == 0: from cctbx.crystal import symmetry from cctbx import miller from cctbx.array_family import flex as cctbx_flex ii = list(self.Modelers.keys())[0] uc = self.Modelers[ii].ucell_man params = uc.a, uc.b, uc.c, uc.al * 180 / np.pi, uc.be * 180 / np.pi, uc.ga * 180 / np.pi if self.params.refiner.force_unit_cell is not None: params = self.params.refiner.force_unit_cell symm = symmetry(unit_cell=params, space_group_symbol=self.symbol) hi_asu_flex = cctbx_flex.miller_index(self.Hi_asu_all_ranks) mset = miller.set(symm, hi_asu_flex, anomalous_flag=True) marr = miller.array(mset) binner = marr.setup_binner( d_max=self.params.refiner.stage_two.d_max, d_min=self.params.refiner.stage_two.d_min, n_bins=self.params.refiner.stage_two.n_bin) from collections import Counter print("Average multiplicities:") print("<><><><><><><><><><><><>") for i_bin in range(self.params.refiner.stage_two.n_bin - 1): dmax, dmin = binner.bin_d_range(i_bin + 1) F_in_bin = marr.resolution_filter(d_max=dmax, d_min=dmin) multi_in_bin = np.array( list(Counter(F_in_bin.indices()).values())) print("%2.5g-%2.5g : Multiplicity=%.4f" % (dmax, dmin, multi_in_bin.mean())) for ii in range(1, 100, 8): print("\t %d refls with multi %d" % (sum(multi_in_bin == ii), ii)) print("Overall completeness\n<><><><><><><><>") symm = symmetry(unit_cell=params, space_group_symbol=self.symbol) hi_flex_unique = cctbx_flex.miller_index( list(set(self.Hi_asu_all_ranks))) mset = miller.set(symm, hi_flex_unique, anomalous_flag=True) self.binner = mset.setup_binner( d_min=self.params.refiner.stage_two.d_min, d_max=self.params.refiner.stage_two.d_max, n_bins=self.params.refiner.stage_two.n_bin) mset.completeness(use_binning=True).show() marr_unique_h = miller.array(mset) print("Rank %d: total miller vars=%d" % (COMM.rank, len(set(self.Hi_asu_all_ranks)))) else: marr_unique_h = None marr_unique_h = COMM.bcast(marr_unique_h) return marr_unique_h
def read_file(self, hklin): assert self.symm is not None # XXX more careful check fin = open(hklin) line = fin.readline() # first line #TODO support other format. assert "CrystFEL reflection list version 2.0" in line line = fin.readline() # second line sym_str = line[line.find(":") + 1:].strip() print "HKL pointgroup:", sym_str, anomalous_flag = None if sym_str == self.symm.space_group().laue_group_type(): anomalous_flag = False elif sym_str == self.symm.space_group().point_group_type(): anomalous_flag = True else: Sorry("Incompatible symmetry: %s and %s" % (sym_str, self.symm.space_group().info())) print "Anomalous:", anomalous_flag line = fin.readline() # third line assert " h k l I phase sigma(I) nmeas" in line indices, Is, sigIs, ns = [], [], [], [] for l in fin: if l.startswith("End of reflections"): break h, k, l, i, phase, sigi, nmeas = l.strip().split() h, k, l, nmeas = map(int, (h, k, l, nmeas)) i, sigi = map(float, (i, sigi)) indices.append((h, k, l)) Is.append(i) sigIs.append(sigi) ns.append(nmeas) miller_set = miller.set(crystal_symmetry=self.symm, indices=flex.miller_index(indices), anomalous_flag=anomalous_flag) self.array = miller.array( miller_set=miller_set, data=flex.double(Is), sigmas=flex.double(sigIs)).set_observation_type_xray_intensity() self.redundancies = miller.array(miller_set=miller_set, data=flex.int(ns))
def read_file(self, hklin): assert self.symm is not None # XXX more careful check fin = open(hklin) line = fin.readline() # first line #TODO support other format. assert "CrystFEL reflection list version 2.0" in line line = fin.readline() # second line sym_str = line[line.find(":")+1:].strip() print "HKL pointgroup:",sym_str, anomalous_flag = None if sym_str == self.symm.space_group().laue_group_type(): anomalous_flag = False elif sym_str == self.symm.space_group().point_group_type(): anomalous_flag = True else: Sorry("Incompatible symmetry: %s and %s" % (sym_str, self.symm.space_group().info())) print "Anomalous:", anomalous_flag line = fin.readline() # third line assert " h k l I phase sigma(I) nmeas" in line indices, Is, sigIs, ns = [], [], [], [] for l in fin: if l.startswith("End of reflections"): break h,k,l,i,phase,sigi,nmeas = l.strip().split() h,k,l,nmeas = map(int, (h,k,l,nmeas)) i,sigi = map(float, (i,sigi)) indices.append((h,k,l)) Is.append(i) sigIs.append(sigi) ns.append(nmeas) miller_set = miller.set(crystal_symmetry=self.symm, indices=flex.miller_index(indices), anomalous_flag=anomalous_flag) self.array = miller.array(miller_set=miller_set, data=flex.double(Is), sigmas=flex.double(sigIs) ).set_observation_type_xray_intensity() self.redundancies = miller.array(miller_set=miller_set, data=flex.int(ns))
def reflections_as_miller_arrays(self, combined=False): from dials.util.batch_handling import ( # calculate_batch_offsets, # get_batch_ranges, assign_batches_to_reflections, ) from dials.report.analysis import scaled_data_as_miller_array # offsets = calculate_batch_offsets(experiments) reflection_tables = [] for id_ in set(self._reflections["id"]).difference({-1}): reflection_tables.append( self._reflections.select(self._reflections["id"] == id_)) offsets = [expt.scan.get_batch_offset() for expt in self._experiments] reflection_tables = assign_batches_to_reflections( reflection_tables, offsets) if combined: # filter bad refls and negative scales batches = flex.int() scales = flex.double() for r in reflection_tables: sel = ~r.get_flags(r.flags.bad_for_scaling, all=False) sel &= r["inverse_scale_factor"] > 0 batches.extend(r["batch"].select(sel)) scales.extend(r["inverse_scale_factor"].select(sel)) scaled_array = scaled_data_as_miller_array(reflection_tables, self._experiments) batch_array = miller.array(scaled_array, data=batches) scale_array = miller.array(scaled_array, data=scales) return scaled_array, batch_array, scale_array else: scaled_arrays = [] batch_arrays = [] scale_arrays = [] for expt, r in zip(self._experiments, reflection_tables): sel = ~r.get_flags(r.flags.bad_for_scaling, all=False) sel &= r["inverse_scale_factor"] > 0 batches = r["batch"].select(sel) scales = r["inverse_scale_factor"].select(sel) scaled_arrays.append(scaled_data_as_miller_array([r], [expt])) batch_arrays.append( miller.array(scaled_arrays[-1], data=batches)) scale_arrays.append( miller.array(scaled_arrays[-1], data=scales)) return scaled_arrays, batch_arrays, scale_arrays
def pha2mtz(phain, xs, mtzout): hkl, f, fom, phi, sigf = [], [], [], [], [] for l in open(phain): sp = l.split() if len(sp) != 7: break hkl.append(tuple(map(int, sp[:3]))) f.append(float(sp[3])) fom.append(float(sp[4])) phi.append(float(sp[5])) sigf.append(float(sp[6])) if not hkl: return f_array = miller.array(miller.set(xs, flex.miller_index(hkl)), data=flex.double(f), sigmas=flex.double(sigf)) mtz_ds = f_array.as_mtz_dataset( column_root_label="ANOM", column_types="FQ" ) # To open with Coot, column type F is required (not D) mtz_ds.add_miller_array(f_array.customized_copy(data=flex.double(phi), sigmas=None), column_root_label="PANOM", column_types="P") mtz_ds.add_miller_array(f_array.customized_copy(data=flex.double(fom), sigmas=None), column_root_label="FOM", column_types="W") mtz_ds.mtz_object().write(mtzout)
def load_sfall(fname): """ special script for loading the structure factor file generated in main() :param fname: file generated in the main method above.. :return: mil_ar, energies mil_ar: dict of miller arrays (complex) energies: array of xray energies in electron volts such that mil_ar[0] is Fhkl at energy energies[0] """ f = h5py.File(fname, "r") data = f["data"][()] indices = f["indices"][()] hall = f["hall_symbol"][()] ucell_param = f["ucell_tuple"][()] energies = f["energies"][()] sg = sgtbx.space_group(hall) Symm = crystal.symmetry(unit_cell=ucell_param, space_group=sg) indices_flex = tuple(map(tuple, indices)) mil_idx = flex.miller_index(indices_flex) mil_set = miller.set(crystal_symmetry=Symm, indices=mil_idx, anomalous_flag=True) mil_ar = {} # load a dict of "sfall at each energy" for i_chan, data_chan in enumerate(data): data_flex = flex.complex_double(np.ascontiguousarray(data_chan)) mil_ar[i_chan] = miller.array(mil_set, data=data_flex) return mil_ar, energies
def random_f_calc(space_group_info, n_scatterers, d_min, anomalous_flag, verbose=0): if (anomalous_flag and space_group_info.group().is_centric()): return None structure = random_structure.xray_structure(space_group_info, elements=["const"] * n_scatterers, volume_per_atom=500, min_distance=2., general_positions_only=True) if (0 or verbose): structure.show_summary().show_scatterers() f_calc = structure.structure_factors( d_min=d_min, anomalous_flag=anomalous_flag).f_calc() f_calc = miller.array(miller_set=f_calc, data=f_calc.data() / flex.mean(flex.abs(f_calc.data()))) if (f_calc.anomalous_flag()): selection = flex.bool(f_calc.indices().size(), True) for i in xrange(f_calc.indices().size() // 10): j = random.randrange(f_calc.indices().size()) selection[j] = False f_calc = f_calc.select(selection) return f_calc
def as_mtz(self,scale_factor,prefix): #This reads in an hkl map and returns a .mtz map #Read in hkl file and populate miller array inf = open(self.diffuse_file, 'r') indices = flex.miller_index() i_obs = flex.double() sig_i = flex.double() for line in inf.readlines(): assert len(line.split())==4 line = line.strip().split() #####ATTENTION:SCALE FACTOR############## i_obs_ = float(line[3])/scale_factor #is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros indices.append([int(line[0]),int(line[1]),int(line[2])]) i_obs.append(i_obs_) sig_i.append(sig_i_) inf.close() # get miller array object cs = self.symmetry ma = miller.array(miller_set=miller.set(cs, indices), data=i_obs, sigmas=sig_i) ma.set_observation_type_xray_intensity() mtz_dataset = ma.as_mtz_dataset(column_root_label="I") mtz_dataset.mtz_object().write(prefix + '.mtz')
def amplitude_quasi_normalisations(ma, d_star_power=1, set_to_minimum=None): epsilons = ma.epsilons().data().as_double() mean_f_sq_over_epsilon = flex.double() for i_bin in ma.binner().range_used(): sel = ma.binner().selection(i_bin) #sel_f_sq = flex.pow2(ma.data().select(sel)) sel_f_sq = ma.data().select(sel) if (sel_f_sq.size() > 0): sel_epsilons = epsilons.select(sel) sel_f_sq_over_epsilon = sel_f_sq / sel_epsilons mean_f_sq_over_epsilon.append(flex.mean(sel_f_sq_over_epsilon)) else: mean_f_sq_over_epsilon.append(0) mean_f_sq_over_epsilon_interp = ma.binner().interpolate( mean_f_sq_over_epsilon, d_star_power) if set_to_minimum and not mean_f_sq_over_epsilon_interp.all_gt(0): # HACK NO REASON THIS SHOULD WORK sel = (mean_f_sq_over_epsilon_interp <= set_to_minimum) mean_f_sq_over_epsilon_interp.set_selected(sel,-mean_f_sq_over_epsilon_interp) sel = (mean_f_sq_over_epsilon_interp <= set_to_minimum) mean_f_sq_over_epsilon_interp.set_selected(sel,set_to_minimum) assert mean_f_sq_over_epsilon_interp.all_gt(0) from cctbx.miller import array #return array(ma, flex.sqrt(mean_f_sq_over_epsilon_interp)) return array(ma, mean_f_sq_over_epsilon_interp)
def mtz_to_miller_array(mtz_object): """ Recombine intensities and phases (under 'IMEAN' and 'PHIB' labels, respectively) from input MTZ file into a Miller array object. Probably there's a CCTBX utility to accomplish this, but I haven't been able to locate it. Inputs: ------- mtz_object: a CCTBX MTZ object (from mtz.object(file_name=filename.mtz)) Outputs: -------- ma: a CCTBX Miller array object of complex structure factors """ # extract Miller arrays and crystal symmetry I_ma = mtz_object.as_miller_arrays_dict()[('crystal', 'dataset', 'IMEAN')] p_ma = mtz_object.as_miller_arrays_dict()[('crystal', 'dataset', 'PHIB')] assert list(I_ma.indices()) == list(p_ma.indices()) cs = mtz_object.crystals()[1].crystal_symmetry() # compute complex structure factors I, p = np.array(I_ma.data()), np.deg2rad(np.array(p_ma.data())) A, B = np.sqrt(I) * np.cos(p), np.sqrt(I) * np.sin(p) indices = I_ma.indices() sf_data = flex.complex_double(flex.double(A), flex.double(B)) # convert complex structure factors to CCTBX-style Miller array ma = miller.array(miller_set = miller.set(cs, indices, anomalous_flag=False), data = sf_data) return ma
def random_hkl_selection(cs, resolution, completeness): """ Retrieve a list of random Miller indices to achieve the desired completeness in P1, assuming no anomalous signal. Inputs: ------- cs: CCTBX crystal symmetry object resolution: high-resolution limit completeness: desired P1 completeness Outputs: -------- hkl_sel: list of Miller indices to retain """ # get list of P1 reflections, assuming no anomalous signal hkl_p1 = list( cs.build_miller_set(anomalous_flag=False, d_min=resolution).expand_to_p1().indices()) # retrieve random list of reflections and their friedel mates n_est = int(np.around(completeness * len(hkl_p1))) hkl_sel = random.sample(hkl_p1, n_est) fhkl_sel = [(-1 * h[0], -1 * h[1], -1 * h[2]) for h in hkl_sel] hkl_sel += fhkl_sel # print information about P1 and SG completeness for cs_obj, tag in zip([cs, cs.cell_equivalent_p1()], ['SG', 'P1']): ma_calc = miller.array(miller_set=miller.set( cs_obj, flex.miller_index(hkl_sel), anomalous_flag=False), data=flex.double(np.ones_like(hkl_sel))) print "%s completeness: %.2f" % ( tag, ma_calc.merge_equivalents().array().completeness()) return hkl_sel
def calculate_exp_i_two_phi_peaks(xray_structure, d_min, min_peak_distance, max_reduced_peaks): f_h = xray_structure.structure_factors( anomalous_flag=False, d_min=d_min).f_calc() two_i_phi_h = miller.array( miller_set=f_h, data=flex.polar(1, flex.arg(f_h.data())*2)) fft_map = two_i_phi_h.fft_map( d_min=d_min, symmetry_flags=maptbx.use_space_group_symmetry) real_map = fft_map.real_map() real_map = maptbx.copy(real_map, flex.grid(real_map.focus())) stats = maptbx.statistics(real_map) if (stats.max() != 0): real_map /= abs(stats.max()) grid_tags = maptbx.grid_tags(real_map.focus()) grid_tags.build(fft_map.space_group_info().type(), fft_map.symmetry_flags()) grid_tags.verify(real_map) peak_list = maptbx.peak_list( data=real_map, tags=grid_tags.tag_array(), max_peaks=10*max_reduced_peaks, interpolate=True) reduced_peaks = peak_cluster_reduction( crystal_symmetry=xray_structure, peak_list=peak_list, min_peak_distance=min_peak_distance, max_reduced_peaks=max_reduced_peaks) return reduced_peaks
def exercise_recycle(space_group_info, anomalous_flag, n_scatterers=8, d_min=2.5, verbose=0): f_calc = random_f_calc( space_group_info=space_group_info, n_scatterers=n_scatterers, d_min=d_min, anomalous_flag=anomalous_flag, verbose=verbose, ) if f_calc is None: return recycle(f_calc, "f_calc", verbose=verbose) for column_root_label, column_types in [("f_obs", None), ("Ework", "E")]: if anomalous_flag and column_types == "E": continue recycle( miller_array=abs(f_calc), column_root_label=column_root_label, column_types=column_types, verbose=verbose ) if not anomalous_flag: recycle(abs(f_calc), "f_obs", column_types="R", verbose=verbose) for column_root_label, column_types in [("f_obs", None), ("Ework", "EQ")]: if anomalous_flag and column_types == "EQ": continue recycle( miller_array=miller.array( miller_set=f_calc, data=flex.abs(f_calc.data()), sigmas=flex.abs(f_calc.data()) / 10 ), column_root_label=column_root_label, column_types=column_types, verbose=verbose, ) recycle(f_calc.centric_flags(), "cent", verbose=verbose) recycle(generate_random_hl(miller_set=f_calc), "prob", verbose=verbose)
def exercise(space_group_info, anomalous_flag, n_scatterers=8, d_min=2, verbose=0): structure = random_structure.xray_structure( space_group_info, elements=["const"]*n_scatterers) f_calc = structure.structure_factors( d_min=d_min, anomalous_flag=anomalous_flag).f_calc() f = abs(f_calc) f = miller.array(miller_set=f, data=f.data(), sigmas=flex.sqrt(f.data())) f = f.f_as_f_sq() g = f.expand_to_p1() merger_p1 = xray.merger( g.indices(), g.data(), g.sigmas(), g.space_group(), g.anomalous_flag(), g.unit_cell() ) p1_bic = merger_p1.bic() p1_r = merger_p1.r_abs() merger_nat = xray.merger( g.indices(), g.data(), g.sigmas(), f.space_group(), g.anomalous_flag(), g.unit_cell() ) nat_bic = merger_nat.bic() nat_r = merger_nat.r_abs() assert nat_bic >= p1_bic assert p1_r <= 1e-8
def extend_symmetry(self,scale_factor,prefix): #This reads in an hkl map and returns a .sca map #Read in hkl file and populate miller array inf = open(self.diffuse_file, 'r') indices = flex.miller_index() i_obs = flex.double() sig_i = flex.double() for line in inf.readlines(): assert len(line.split())==4 line = line.strip().split() #####ATTENTION:SCALE FACTOR############## i_obs_ = float(line[3])/scale_factor #is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros indices.append([int(line[0]),int(line[1]),int(line[2])]) i_obs.append(i_obs_) sig_i.append(sig_i_) inf.close() # get miller array object cs = self.symmetry ma = miller.array(miller_set=miller.set(cs, indices), data=i_obs, sigmas=sig_i) ma.set_observation_type_xray_intensity() ma_anom = ma.customized_copy(anomalous_flag=False) ma_p1 = ma_anom.expand_to_p1() merge.write(file_name= prefix + '.sca', miller_array=ma_p1) self.p1_map = prefix + '.sca'
def exercise(space_group_info, anomalous_flag, n_scatterers=8, d_min=2, verbose=0): structure = random_structure.xray_structure(space_group_info, elements=["const"] * n_scatterers) f_calc = structure.structure_factors( d_min=d_min, anomalous_flag=anomalous_flag).f_calc() f = abs(f_calc) f = miller.array(miller_set=f, data=f.data(), sigmas=flex.sqrt(f.data())) f = f.f_as_f_sq() g = f.expand_to_p1() merger_p1 = xray.merger(g.indices(), g.data(), g.sigmas(), g.space_group(), g.anomalous_flag(), g.unit_cell()) p1_bic = merger_p1.bic() p1_r = merger_p1.r_abs() merger_nat = xray.merger(g.indices(), g.data(), g.sigmas(), f.space_group(), g.anomalous_flag(), g.unit_cell()) nat_bic = merger_nat.bic() nat_r = merger_nat.r_abs() assert nat_bic >= p1_bic assert p1_r <= 1e-8
def as_miller_array(self, crystal_symmetry=None, force_symmetry=False, merge_equivalents=True, base_array_info=None): if (base_array_info is None): base_array_info = miller.array_info(source_type="scalepack_merge") crystal_symmetry_from_file = self.crystal_symmetry() if (self.anomalous): labels = ["I(+)", "SIGI(+)", "I(-)", "SIGI(-)"] else: labels = ["I", "SIGI"] return (miller.array( miller_set=miller.set( crystal_symmetry=crystal_symmetry_from_file.join_symmetry( other_symmetry=crystal_symmetry, force=force_symmetry), indices=self.miller_indices, anomalous_flag=self.anomalous), data=self.i_obs, sigmas=self.sigmas) .set_info(base_array_info.customized_copy( labels=labels, crystal_symmetry_from_file=crystal_symmetry_from_file)) .set_observation_type_xray_intensity())
def cluster_completeness(self, clno, anomalous_flag, calc_redundancy=True): if clno not in self.clusters: print "Cluster No. %d not found" % clno return cls = self.clusters[clno][3] msets = map(lambda x: self.miller_sets[self.files[x-1]], cls) num_idx = sum(map(lambda x: x.size(), msets)) all_idx = flex.miller_index() all_idx.reserve(num_idx) for mset in msets: all_idx.extend(mset.indices()) # Calc median cell cells = numpy.array(map(lambda x: x.unit_cell().parameters(), msets)) median_cell = map(lambda i: numpy.median(cells[:,i]), xrange(6)) symm = msets[0].customized_copy(unit_cell=median_cell) assert anomalous_flag is not None # XXX all must belong to the same Laue group and appropriately reindexed.. all_set = miller.set(indices=all_idx, crystal_symmetry=symm, anomalous_flag=anomalous_flag) all_set = all_set.resolution_filter(d_min=self.d_min) # dummy for redundancy calculation. dirty way.. if calc_redundancy: dummy_array = miller.array(miller_set=all_set, data=flex.int(all_set.size())) merge = dummy_array.merge_equivalents() cmpl = merge.array().completeness() redun = merge.redundancies().as_double().mean() return cmpl, redun else: cmpl = all_set.unique_under_symmetry().completeness() return cmpl
def exercise(): ma = miller.array( miller.set(crystal.symmetry(unit_cell=(5,5,5, 90, 90, 90), space_group=sgtbx.space_group('P 2x')), indices=flex.miller_index( [(1,0,0), (0,1,0), (0,0,1), (-1,0,0), (0,-1,0), (0,0,-1), (1,1,0), (1,0,1), (0,1,1), (-1,-1,0), (-1,0,-1), (0,-1,-1), (1,-1,0), (1,0,-1), (0,1,-1), (-1,1,0), (-1,0,1), (0,-1,1), (1,1,1), (-1,1,1), (1,-1,1), (1,1,-1), (-1,-1,-1), (1,-1,-1), (-1,1,-1), (-1,-1,1)])), data=flex.complex_double(flex.random_double(26), flex.random_double(26))) f_at_h = dict(zip(ma.indices(), ma.data())) for op in ("-x, y+1/2, -z", "x+1/2, -y, z-1/2"): op = sgtbx.rt_mx(op) original, transformed = ma.common_sets( ma.change_basis(sgtbx.change_of_basis_op(op.inverse()))) for h, f in original: assert f == f_at_h[h] for h, op_f in transformed: assert approx_equal( op_f, f_at_h[h*op.r()]*exp(1j*2*pi*row(h).dot(col(op.t().as_double()))))
def exercise_SFweight_spline_core(structure, d_min, verbose=0): structure.scattering_type_registry(d_min=d_min) f_obs = abs(structure.structure_factors( d_min=d_min, anomalous_flag=False).f_calc()) if (0 or verbose): f_obs.show_summary() f_obs = miller.array( miller_set=f_obs, data=f_obs.data(), sigmas=flex.sqrt(f_obs.data())) partial_structure = xray.structure( crystal_symmetry=structure, scatterers=structure.scatterers()[:-2]) f_calc = f_obs.structure_factors_from_scatterers( xray_structure=partial_structure).f_calc() test_set_flags = (flex.random_double(size=f_obs.indices().size()) < 0.1) sfweight = clipper.SFweight_spline_interface( unit_cell=f_obs.unit_cell(), space_group=f_obs.space_group(), miller_indices=f_obs.indices(), anomalous_flag=f_obs.anomalous_flag(), f_obs_data=f_obs.data(), f_obs_sigmas=f_obs.sigmas(), f_calc=f_calc.data(), test_set_flags=test_set_flags, n_refln=f_obs.indices().size()//10, n_param=20) if (0 or verbose): print "number_of_spline_parameters:",sfweight.number_of_spline_parameters() print "mean fb: %.8g" % flex.mean(flex.abs(sfweight.fb())) print "mean fd: %.8g" % flex.mean(flex.abs(sfweight.fd())) print "mean phi: %.8g" % flex.mean(sfweight.centroid_phases()) print "mean fom: %.8g" % flex.mean(sfweight.figures_of_merit()) return sfweight
def run(lstin): data = [] for l in open(lstin): xdsasc = l.strip() xa = XDS_ASCII(xdsasc, sys.stdout, i_only=True) ma = miller.array(miller_set=xa.as_miller_set(anomalous_flag=False), data=xa.iobs) data.append((xdsasc, ma)) print "index filename" for i, d in enumerate(data): print i, d[0] print "i j n.i n.j n.common cc" for i in xrange(len(data) - 1): for j in xrange(i + 1, len(data)): di, dj = data[i][1].common_sets(data[j][1], assert_is_similar_symmetry=False) print i, j, data[i][1].data().size(), data[j][1].data().size(), if len(di.data()) == 0: print 0, "nan" else: corr = flex.linear_correlation(di.data(), dj.data()) assert corr.is_well_defined() cc = corr.coefficient() print len(di.data()), cc
def generate_table(complex_data, indices, numpy_args=False, anom=True): """ :param complex_data: structure factors :param indices: miller indices in a list, Nx3 :param numpy_args: are the complex data and indices numpy type, if not assume flex :param anom: return a miller array with +H and -H ? :return: dictionary whose keys are miller index tuple and values are structure fact """ sg = sgtbx.space_group(" P 4nw 2abw") Symm = crystal.symmetry(unit_cell=(79, 79, 38, 90, 90, 90), space_group=sg) if numpy_args: assert type(indices) == tuple assert (type(indices[0]) == tuple) indices = flex.miller_index(indices) mil_set = miller.set(crystal_symmetry=Symm, indices=indices, anomalous_flag=anom) if numpy_args: complex_data = flex.complex_double(np.ascontiguousarray(complex_data)) mil_ar = miller.array(mil_set, data=complex_data) mil_dict = {h: val for h, val in izip(mil_ar.indices(), mil_ar.data())} return mil_dict
def exercise_recycle(space_group_info, anomalous_flag, n_scatterers=8, d_min=2.5, verbose=0): f_calc = random_f_calc(space_group_info=space_group_info, n_scatterers=n_scatterers, d_min=d_min, anomalous_flag=anomalous_flag, verbose=verbose) if (f_calc is None): return recycle(f_calc, "f_calc", verbose=verbose) for column_root_label, column_types in [("f_obs", None), ("Ework", "E")]: if (anomalous_flag and column_types == "E"): continue recycle(miller_array=abs(f_calc), column_root_label=column_root_label, column_types=column_types, verbose=verbose) if (not anomalous_flag): recycle(abs(f_calc), "f_obs", column_types="R", verbose=verbose) for column_root_label, column_types in [("f_obs", None), ("Ework", "EQ")]: if (anomalous_flag and column_types == "EQ"): continue recycle(miller_array=miller.array(miller_set=f_calc, data=flex.abs(f_calc.data()), sigmas=flex.abs(f_calc.data()) / 10), column_root_label=column_root_label, column_types=column_types, verbose=verbose) recycle(f_calc.centric_flags(), "cent", verbose=verbose) recycle(generate_random_hl(miller_set=f_calc), "prob", verbose=verbose)
def scale_data(indices, iobs, scale_ref, parameter, calc_cc): k, b, cc = 1, float("nan"), float("nan") sortp = yamtbx_utils_ext.sort_permutation_fast_less(indices) indices = indices.select(sortp) iobs = iobs.select(sortp) sel0, sel1 = yamtbx_utils_ext.my_common_indices(scale_ref.indices(), indices) #indices = indices.select(sel1) iobs_c = iobs.select(sel1) ref_c = scale_ref.data().select(sel0) if iobs_c.size() < 10 and ref_c.size() < 10: return k, b, cc if parameter == "k": k = flex.sum(ref_c*iobs_c) / flex.sum(flex.pow2(iobs_c)) elif parameter == "kb": from yamtbx.dataproc.scale_data import kBdecider kbd = kBdecider(scale_ref, miller.array(scale_ref.customized_copy(indices=indices),data=iobs)) k, b = kbd.run() else: raise "Never reaches here" if calc_cc: corr = flex.linear_correlation(ref_c, iobs_c) if corr.is_well_defined(): cc = corr.coefficient() return k, b, cc
def as_miller_array(self, crystal_symmetry=None, force_symmetry=False, merge_equivalents=True, base_array_info=None, anomalous=None): if (base_array_info is None): base_array_info = miller.array_info(source_type="xds_ascii") crystal_symmetry_from_file = self.crystal_symmetry() array = (miller.array( miller_set=self.miller_set( crystal_symmetry=crystal_symmetry, force_symmetry=force_symmetry, anomalous=anomalous, ), data=self.iobs, sigmas=self.sigma_iobs) .set_info(base_array_info.customized_copy( labels=["iobs", "sigma_iobs"], crystal_symmetry_from_file=crystal_symmetry_from_file, wavelength=self.wavelength)) .set_observation_type_xray_intensity()) if (merge_equivalents): info = array.info() info.merged = True array = array.merge_equivalents().array().set_info(info) return array
def generate_mtz_files(space_group_info, anomalous_flag): crystal_symmetry = crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=1000), space_group_info=space_group_info) miller_set = miller.build_set(crystal_symmetry=crystal_symmetry, anomalous_flag=anomalous_flag, d_min=1) miller_array = miller.array( miller_set=miller_set, data=flex.random_double(size=miller_set.indices().size())) miller_array_p1 = miller_array.expand_to_p1() miller_arrays = [] file_names = [] subgrs = subgroups.subgroups(space_group_info).groups_parent_setting() for i_subgroup, subgroup in enumerate(subgrs): subgroup_miller_array = miller_array_p1.customized_copy( space_group_info=sgtbx.space_group_info(group=subgroup)) \ .merge_equivalents() \ .array() \ .as_reference_setting() \ .set_observation_type_xray_intensity() file_name = "tmp_refl_stats%d.mtz" % i_subgroup mtz_object = subgroup_miller_array.as_mtz_dataset( column_root_label="FOBS").mtz_object().write(file_name=file_name) miller_arrays.append( subgroup_miller_array.f_sq_as_f().expand_to_p1().map_to_asu()) file_names.append(file_name) return miller_arrays, file_names
def __init__(self, obs, is_amplitude=True, weighting=None ): """ Construct a least-square residuals .. |Sigma| unicode:: U+003A3 .. GREEK CAPITAL LETTER SIGMA |Sigma|:sub:`i` w[i] ( f_obs.data[i] - k abs(f_calc.data[i]) )^2 / |Sigma|:sub:`i` w[i] f_obs.data[i]^2 or |Sigma|:sub:`i` w[i] ( f_obs_square.data[i] - k abs(f_calc.data[i])^2 )^2 / |Sigma|:sub:`i` w[i] f_obs_square.data[i]^2 depending on which of f_obs and f_obs_square is not None. Note that - the sums are over the indices i of the reflections, - f_calc is to be passed to the __call__ method, - the weights w and the scale factor k are discussed below. :Parameters: obs : real miller.array the observed reflections, with F and sigma(F) (or F^2 and sigma(F^2)) respectively in obs.data() and obs.sigmas() is_amplitude : bool a flag to discriminate the type of data in obs if the latter does not spell out whether it contains amplitudes or intensities; the default means that amplitudes is the default when data type is unknown. weighting a weighting scheme for the data (c.f. cctbx.xray.weighting for common ones) or None, which means no weights """ if not(obs.is_xray_amplitude_array() or obs.is_xray_intensity_array()): self._obs = miller.array(obs, data=obs.data(), sigmas=obs.sigmas()) if is_amplitude: self._obs.set_observation_type_xray_amplitude() else: self._obs.set_observation_type_xray_intensity() else: self._obs = obs assert(self._obs.is_xray_amplitude_array() or self._obs.is_xray_intensity_array()) if self.obs().is_xray_amplitude_array(): self._ext_ls_residual = ext.targets_least_squares_residual default_weighting = weighting_schemes.amplitude_unit_weighting() elif self.obs().is_xray_intensity_array(): self._ext_ls_residual = ext.targets_least_squares_residual_for_intensity default_weighting = weighting_schemes.intensity_quasi_unit_weighting() if weighting is None: weighting = default_weighting self._weighting = weighting self._weighting.observed = self._obs self._scale_factor = None
def as_miller_array(self, crystal_symmetry=None, force_symmetry=False, merge_equivalents=True, base_array_info=None): if (base_array_info is None): base_array_info = miller.array_info( source_type="scalepack_no_merge_original_index") crystal_symmetry_from_file = self.crystal_symmetry() crystal_symmetry = crystal_symmetry_from_file.join_symmetry( other_symmetry=crystal_symmetry, force=force_symmetry) result = miller.array( miller_set=self.unmerged_miller_set( crystal_symmetry=crystal_symmetry, force_symmetry=True), data=self.i_obs, sigmas=self.sigmas) if (merge_equivalents): result = result.merge_equivalents().array() return (result .set_info(base_array_info.customized_copy( labels=["I(+)", "SIGI(+)", "I(-)", "SIGI(-)"], merged=merge_equivalents, crystal_symmetry_from_file=crystal_symmetry_from_file)) .set_observation_type_xray_intensity())
def df2m(df, cell, spgr, data=None, sigmas=None): """Constructs a miller.array from the columns specified by data/sigmas in the dataframe, if both are None, returns just the indices. needs cell and spgr to generate a symmetry object.""" anomalous_flag = False if isinstance(df, pd.DataFrame): try: sel = df[data].notnull() # select notnull data items for index except ValueError: index = df.index else: index = df.index[sel] else: index = df indices = flex.miller_index(index) if data: data = flex.double(df[data][sel]) if sigmas: sigmas = flex.double(df[sigmas][sel]) symm = make_symmetry(cell, spgr) ms = miller.set( crystal_symmetry=symm, indices=indices, anomalous_flag=anomalous_flag) return miller.array(ms, data=data, sigmas=sigmas)
def as_miller_array(gsas_exp, gsas_rfl, divide_pr, phase_id=1, histogram_id=1): from cctbx import miller from cctbx import crystal from cctbx.array_family import flex from scitbx.python_utils import easy_pickle info = gsas_exp.DESCR if (divide_pr): info += ", FIPS partitioning" indices = flex.miller_index() data = flex.double() if (not divide_pr): sigmas = flex.double() for r in gsas_rfl.records: indices.append(r.ihkl) data.append(r.fosq) sigmas.append(r.sig) else: sigmas = None for r in divide_pr.records: indices.append(r.hkl) data.append(r.fnew**2) miller_array = miller.array( miller_set=miller.set(crystal_symmetry=crystal.symmetry( unit_cell=gsas_exp.get_unit_cell_parameters(phase_id), space_group_symbol=gsas_exp.get_space_group_symbol(phase_id)), indices=indices, anomalous_flag=00000), data=data, sigmas=sigmas).set_info(info).set_observation_type_xray_intensity() miller_array.show_comprehensive_summary() print "Writing file: miller_array.pickle" easy_pickle.dump("miller_array.pickle", miller_array)
def run(lstin): data = [] for l in open(lstin): xdsasc = l.strip() xa = XDS_ASCII(xdsasc, sys.stdout, i_only=True) ma = miller.array(miller_set=xa.as_miller_set(anomalous_flag=False), data=xa.iobs) data.append((xdsasc, ma)) print "index filename" for i, d in enumerate(data): print i, d[0] print "i j n.i n.j n.common cc" for i in xrange(len(data)-1): for j in xrange(i+1, len(data)): di, dj = data[i][1].common_sets(data[j][1], assert_is_similar_symmetry=False) print i, j, data[i][1].data().size(), data[j][1].data().size(), if len(di.data()) == 0: print 0, "nan" else: corr = flex.linear_correlation(di.data(), dj.data()) assert corr.is_well_defined() cc = corr.coefficient() print len(di.data()), cc
def read_x(xfile, take_full=False): """ according to http://www.hkl-xray.com/denzo-output Regular (film output file, Denzo _ip) imaging plate output format (i.e. the format you would you to read in in FORTRAN) is: format (3i4,i2,2f8.0,f7.0,f6.0,f6.0,2f7.0,f6.0,f8.0) h,k,l Flag 0 - full 1 - partial Intensity (F**2) by profile fitting s of intensity (F**2) c2 of profile fitting Intensity (F**2) by profile summation Cosine of incidence angle at detector Predicted pixel position of spot centroid (slow, fast) directions Lorentz, polarization, obliquity combined factor Strength of averaged profile in arbitrary units """ lines = open(xfile).readlines() indices = flex.miller_index() data = flex.double() sigmas = flex.double() full_flags = flex.bool() cell, sg_str = None, None read_hkl = True for l in lines[5:]: if l.startswith(" 999") and len(l.split()) == 3: read_hkl = False if read_hkl: hkl = map(int, (l[0:4], l[4:8], l[8:12])) ispart = int(l[13]) iobs, sigma = map(float, (l[14:22], l[22:30])) indices.append(hkl) data.append(iobs) sigmas.append(sigma) full_flags.append(ispart==0) else: if l.startswith("unit cell"): cell = map(float, l.split()[2:]) elif l.startswith("space group"): sg_str = l.split()[-1] #sg_str="p1" symm = crystal.symmetry(unit_cell=cell, space_group=sg_str) array = miller.array(miller_set=miller.set(crystal_symmetry=symm, indices=indices, anomalous_flag=True), data=data, sigmas=sigmas).set_observation_type_xray_intensity() if take_full: array = array.select(full_flags) return array
def aniso_convert(uc,sg,map,res): os.system('hkl2vtk %s_friedel.hkl %s_friedel.vtk %s_raw.vtk' %(map,map,map)) os.system('vtk2lat %s_friedel.vtk %s_friedel.lat' %(map,map)) os.system('avgrlt %s_friedel.lat %s_friedel.rf' %(map,map)) os.system('subrflt %s_friedel.rf %s_friedel.lat anisotropic.lat' %(map,map)) os.system('lat2hkl anisotropic.lat anisotropic.hkl') #Read in hkl file and populate miller array from cctbx import crystal inf = open('anisotropic.hkl', "r") indices = flex.miller_index() i_obs = flex.double() #sig_i = flex.double() for line in inf.readlines(): assert len(line.split())==4 line = line.strip().split() i_obs_ = float(line[3])#/10000 #10000 is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) #sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros if i_obs_ != -32768.0: indices.append([int(line[0]),int(line[1]),int(line[2])]) i_obs.append(i_obs_) #sig_i.append(sig_i_) inf.close() #Get miller array object cs = crystal.symmetry(unit_cell=(float(uc[0]), float(uc[1]), float(uc[2]), float(uc[3]), float(uc[4]), float(uc[5])), space_group=sg) miller_set=miller.set(cs, indices) ma = miller.array(miller_set=miller_set, data=i_obs, sigmas=None) mtz_dataset = ma.as_mtz_dataset(column_root_label="Amplitude") mtz_dataset.mtz_object().write('%s_anisotropic.mtz' %map)
def convert_to_sf(hkl, intensities, phases, cs): """ Reformat intensities and phases into a CCTBX-style Miller array, with space group symmetry enforced. Inputs: ------- hkl: list of Miller indices, formatted as tuples intensities: array of intensities, with ordering matching hkl phases: array of phases with ordering matching hkl cs: CCTBX crystal.symmetry object Outputs: -------- ma: CCTBX-style Miller array """ # compute structure factors in complex number format if (phases.min() < -1*np.pi) or (phases.max() > np.pi): print "Error: Invalid phase values; may be in degrees rather than radians." A, B = np.sqrt(intensities)*np.cos(phases), np.sqrt(intensities)*np.sin(phases) B[np.abs(B)<1e-12] = 0 # reformat miller indices and structure factor information into CCTBX format indices = flex.miller_index(hkl) sf_data = flex.complex_double(flex.double(A),flex.double(B)) ma = miller.array(miller_set=miller.set(cs, indices, anomalous_flag=False), data=sf_data) return ma
def as_mtz(self, scale_factor, prefix): #This reads in an hkl map and returns a .mtz map #Read in hkl file and populate miller array inf = open(self.diffuse_file, 'r') indices = flex.miller_index() i_obs = flex.double() sig_i = flex.double() for line in inf.readlines(): assert len(line.split()) == 4 line = line.strip().split() #####ATTENTION:SCALE FACTOR############## i_obs_ = float( line[3] ) / scale_factor #is a uniform scale factor meant to re-size all diffuse intensities (normally too large for scalepack) sig_i_ = math.sqrt(i_obs_) #if(abs(i_obs_)>1.e-6): # perhaps you don't want zeros indices.append([int(line[0]), int(line[1]), int(line[2])]) i_obs.append(i_obs_) sig_i.append(sig_i_) inf.close() # get miller array object cs = self.symmetry ma = miller.array(miller_set=miller.set(cs, indices), data=i_obs, sigmas=sig_i) ma.set_observation_type_xray_intensity() mtz_dataset = ma.as_mtz_dataset(column_root_label="I") mtz_dataset.mtz_object().write(prefix + '.mtz')
def merge_obs(indices, iobs, sel, symm, anomalous_flag, d_min, d_max): #indices = flex.miller_index(indices) #iobs = flex.double(iobs) if sel is not None and len(sel) > 0: sel = flex.bool(sel) indices = indices.select(sel) iobs = iobs.select(sel) miller_set = miller.set(crystal_symmetry=symm, indices=indices, anomalous_flag=anomalous_flag) array = miller.array(miller_set=miller_set, data=iobs, ).set_observation_type_xray_intensity() array = array.resolution_filter(d_min=d_min, d_max=d_max) ## New way #array = array.map_to_asu() # #merger = yamtbx_dataproc_crystfel_ext.merge_equivalents_crystfel() #merger.add_observations(array.indices(), array.data()) #merger.merge() return array.merge_equivalents(algorithm="crystfel") # if sigmas is None, merge_equivalents_real() is used which simply averages.
def generate_mtz_files(space_group_info, anomalous_flag): crystal_symmetry = crystal.symmetry( unit_cell=space_group_info.any_compatible_unit_cell(volume=1000), space_group_info=space_group_info) miller_set = miller.build_set( crystal_symmetry=crystal_symmetry, anomalous_flag=anomalous_flag, d_min=1) miller_array = miller.array( miller_set=miller_set, data=flex.random_double(size=miller_set.indices().size())) miller_array_p1 = miller_array.expand_to_p1() miller_arrays = [] file_names = [] subgrs = subgroups.subgroups(space_group_info).groups_parent_setting() for i_subgroup, subgroup in enumerate(subgrs): subgroup_miller_array = miller_array_p1.customized_copy( space_group_info=sgtbx.space_group_info(group=subgroup)) \ .merge_equivalents() \ .array() \ .as_reference_setting() \ .set_observation_type_xray_intensity() file_name = "tmp_refl_stats%d.mtz" % i_subgroup mtz_object = subgroup_miller_array.as_mtz_dataset( column_root_label="FOBS").mtz_object().write(file_name=file_name) miller_arrays.append( subgroup_miller_array.f_sq_as_f().expand_to_p1().map_to_asu()) file_names.append(file_name) return miller_arrays, file_names
def amplitude_quasi_normalisations(ma, d_star_power=1, set_to_minimum=None, pseudo_likelihood=False): # Used for pseudo-likelihood calculation epsilons = ma.epsilons().data().as_double() mean_f_sq_over_epsilon = flex.double() for i_bin in ma.binner().range_used(): sel = ma.binner().selection(i_bin) if pseudo_likelihood: sel_f_sq = flex.pow2(ma.data().select(sel)) # original method used else: # usual sel_f_sq = ma.data().select(sel) if (sel_f_sq.size() > 0): sel_epsilons = epsilons.select(sel) sel_f_sq_over_epsilon = sel_f_sq / sel_epsilons mean_f_sq_over_epsilon.append(flex.mean(sel_f_sq_over_epsilon)) else: mean_f_sq_over_epsilon.append(0) mean_f_sq_over_epsilon_interp = ma.binner().interpolate( mean_f_sq_over_epsilon, d_star_power) if set_to_minimum and not mean_f_sq_over_epsilon_interp.all_gt(0): # HACK NO REASON THIS SHOULD WORK BUT IT GETS BY THE FAILURE sel = (mean_f_sq_over_epsilon_interp <= set_to_minimum) mean_f_sq_over_epsilon_interp.set_selected(sel,-mean_f_sq_over_epsilon_interp) sel = (mean_f_sq_over_epsilon_interp <= set_to_minimum) mean_f_sq_over_epsilon_interp.set_selected(sel,set_to_minimum) assert mean_f_sq_over_epsilon_interp.all_gt(0) from cctbx.miller import array return array(ma, flex.sqrt(mean_f_sq_over_epsilon_interp))
def __init__(self, strategies, n_bins=8, degrees_per_bin=5): from cctbx import crystal, miller import copy sg = strategies[0].experiment.crystal.get_space_group() \ .build_derived_reflection_intensity_group(anomalous_flag=True) cs = crystal.symmetry( unit_cell=strategies[0].experiment.crystal.get_unit_cell(), space_group=sg) for i, strategy in enumerate(strategies): if i == 0: predicted = copy.deepcopy(strategy.predicted) else: predicted_ = copy.deepcopy(strategy.predicted) predicted_['dose'] += (flex.max(predicted['dose']) + 1) predicted.extend(predicted_) ms = miller.set(cs, indices=predicted['miller_index'], anomalous_flag=True) ma = miller.array(ms, data=flex.double(ms.size(),1), sigmas=flex.double(ms.size(), 1)) if 1: merging = ma.merge_equivalents() o = merging.array().customized_copy( data=merging.redundancies().data().as_double()).as_mtz_dataset('I').mtz_object() o.write('predicted.mtz') d_star_sq = ma.d_star_sq().data() binner = ma.setup_binner_d_star_sq_step( d_star_sq_step=(flex.max(d_star_sq)-flex.min(d_star_sq)+1e-8)/n_bins) dose = predicted['dose'] range_width = 1 range_min = flex.min(dose) - range_width range_max = flex.max(dose) n_steps = 2 + int((range_max - range_min) - range_width) binner_non_anom = ma.as_non_anomalous_array().use_binning( binner) self.n_complete = flex.size_t(binner_non_anom.counts_complete()[1:-1]) from xia2.Modules.PyChef2 import ChefStatistics chef_stats = ChefStatistics( ma.indices(), ma.data(), ma.sigmas(), ma.d_star_sq().data(), dose, self.n_complete, binner, ma.space_group(), ma.anomalous_flag(), n_steps) def fraction_new(completeness): # Completeness so far at end of image completeness_end = completeness[1:] # Completeness so far at start of image completeness_start = completeness[:-1] # Fraction of unique reflections observed for the first time on each image return completeness_end - completeness_start self.dose = dose self.ieither_completeness = chef_stats.ieither_completeness() self.iboth_completeness = chef_stats.iboth_completeness() self.frac_new_ref = fraction_new(self.ieither_completeness) / degrees_per_bin self.frac_new_pairs = fraction_new(self.iboth_completeness) / degrees_per_bin
def verify(sg_fcalc, sg_hl, sg_cns, p1_cns): sg_phase_integrals = miller.array( miller_set=miller.set( crystal_symmetry=sg_fcalc, indices=sg_cns.miller_indices, anomalous_flag=sg_fcalc.anomalous_flag()), data=sg_cns.hl).phase_integrals() for h, cns_pi, miller_pi in zip(sg_cns.miller_indices, sg_cns.pi.data, sg_phase_integrals.data()): if (abs(cns_pi - miller_pi) > 1.e-2): print "Error:", h, cns_pi, miller_pi if (0): return raise AssertionError # p1_phase_integrals = miller.array( miller_set=miller.set( crystal_symmetry=sg_fcalc.cell_equivalent_p1(), indices=p1_cns.miller_indices, anomalous_flag=sg_fcalc.anomalous_flag()), data=p1_cns.hl).phase_integrals() for h, cns_pi, miller_pi in zip(p1_cns.miller_indices, p1_cns.pi.data, p1_phase_integrals.data()): if (abs(cns_pi - miller_pi) > 1.e-2): print "Error:", h, cns_pi, miller_pi if (0): return raise AssertionError # space_group = sg_fcalc.space_group() asu = sg_fcalc.space_group_info().reciprocal_space_asu() lookup_dict = miller.make_lookup_dict(sg_fcalc.indices()) for p1_i, h in enumerate(p1_cns.miller_indices): h_asu = miller.asym_index(space_group, asu, h) h_eq = h_asu.one_column(sg_fcalc.anomalous_flag()) fcalc_asu = h_eq.complex_eq(p1_cns.fcalc.data[p1_i]) hl_asu = h_eq.hendrickson_lattman_eq(p1_cns.hl[p1_i]) sg_i = lookup_dict[h_eq.h()] assert abs(sg_fcalc.data()[sg_i] - fcalc_asu) < 1.e-2 if (not approx_equal(sg_hl[sg_i], hl_asu, eps=1.e-2)): print "Error:", sg_fcalc.space_group_info() print sg_fcalc.indices()[sg_i] print "i:", sg_hl[sg_i] print "o:", hl_asu if (0): return raise AssertionError
def run(show_plots,args): from xfel.command_line.cxi_merge import master_phil phil = iotbx.phil.process_command_line(args=args, master_string=master_phil).show() work_params = phil.work.extract() from xfel.merging.phil_validation import application,samosa application(work_params) samosa(work_params) if ("--help" in args) : libtbx.phil.parse(master_phil.show()) return datadir = "." written_files = [] if work_params.levmar.compute_cc_half: for half_data_flag in [1,2,0]: case = execute_case(datadir, work_params, plot=show_plots, half_data_flag=half_data_flag) assert len(case.Fit_I)==len(case.ordered_intensities.indices())==len(case.reference_millers.indices()) model_subset = case.reference_millers[0:len(case.Fit_I)] fitted_miller_array = miller.array (miller_set = model_subset, data = case.Fit_I, sigmas = case.Fit_I_stddev) fitted_miller_array.set_observation_type_xray_intensity() output_result = fitted_miller_array.select(case.I_visited==1) outfile = "%s_s%1d_levmar.mtz"%(work_params.output.prefix,half_data_flag) output_result.show_summary(prefix="%s: "%outfile) mtz_out = output_result.as_mtz_dataset(column_root_label="Iobs",title=outfile,wavelength=None) mtz_obj = mtz_out.mtz_object() mtz_obj.write(outfile) written_files.append(outfile) print "OK s%1d"%half_data_flag #raw_input("OK?") """Guest code to retrieve the modified orientations after rotational fitting is done""" if "Rxy" in work_params.levmar.parameter_flags: all_A = [e.crystal.get_A() for e in case.experiments.get_experiments()] all_files = case.experiments.get_files() all_x = case.Fit["Ax"] all_y = case.Fit["Ay"] from scitbx import matrix x_axis = matrix.col((1.,0.,0.)) y_axis = matrix.col((0.,1.,0.)) out = open("aaaaa","w") for x in xrange(len(all_A)): Rx = x_axis.axis_and_angle_as_r3_rotation_matrix(angle=all_x[x], deg=True) Ry = y_axis.axis_and_angle_as_r3_rotation_matrix(angle=all_y[x], deg=True) modified_A = Rx * Ry * all_A[x] filename = all_files[x] print >>out, filename, " ".join([str(a) for a in modified_A.elems]) work_params.scaling.algorithm="levmar" from xfel.cxi.cxi_cc import run_cc run_cc(work_params,work_params.model_reindex_op,sys.stdout) else: execute_case(datadir, work_params, plot=show_plots)
def as_miller_arrays(self, crystal_symmetry=None, force_symmetry=False, merge_equivalents=True, base_array_info=None): if (base_array_info is None): base_array_info = miller.array_info(source_type="xds_integrate_hkl") from cctbx.array_family import flex from cctbx import crystal, miller, sgtbx crystal_symmetry = crystal.symmetry( unit_cell=self.unit_cell, space_group_info=sgtbx.space_group_info(number=self.space_group)) indices = flex.miller_index(self.hkl) miller_set = miller.set(crystal_symmetry, indices) return (miller.array( miller_set, data=flex.double(self.iobs), sigmas=flex.double(self.sigma)) .set_info(base_array_info.customized_copy( labels=["iobs", "sigma_iobs"])), miller.array(miller_set, data=flex.vec3_double(self.xyzcal)) .set_info(base_array_info.customized_copy( labels=["xyzcal"])), miller.array(miller_set, data=flex.vec3_double(self.xyzobs)) .set_info(base_array_info.customized_copy( labels=["xyzobs"])), miller.array(miller_set, data=flex.double(self.rlp)) .set_info(base_array_info.customized_copy( labels=["rlp"])), miller.array(miller_set, data=flex.double(self.peak)) .set_info(base_array_info.customized_copy( labels=["peak"])), miller.array(miller_set, data=flex.double(self.corr)) .set_info(base_array_info.customized_copy( labels=["corr"])), miller.array(miller_set, data=flex.double(self.maxc)) .set_info(base_array_info.customized_copy( labels=["maxc"])), miller.array(miller_set, data=flex.vec2_double(self.alfbet0)) .set_info(base_array_info.customized_copy( labels=["alfbet0"])), miller.array(miller_set, data=flex.vec2_double(self.alfbet1)) .set_info(base_array_info.customized_copy( labels=["alfbet1"])), miller.array(miller_set, data=flex.double(self.psi)) .set_info(base_array_info.customized_copy( labels=["psi"])))
def run(files): assert len(files) == 2 hkl1 = xds_ascii.XDS_ASCII(files[0], sys.stdout) hkl2 = xds_ascii.XDS_ASCII(files[1], sys.stdout) hkl1_points = numpy.column_stack((hkl1.xd, hkl1.yd, hkl1.zd)) tree1 = spatial.cKDTree(hkl1_points) n_ovl, n_nonovl = 0, 0 novl_indices, novl_i, novl_sigma = flex.miller_index(), flex.double(), flex.double() for i in xrange(len(hkl2.indices)): x, y, z = hkl2.xd[i], hkl2.yd[i], hkl2.zd[i] #if z > 180: # continue dists, idxs = tree1.query((x,y,z), k=3, p=1) overlaps = [] for dist, idx in zip(dists, idxs): idx = int(idx) xo, yo, zo = hkl1.xd[idx], hkl1.yd[idx], hkl1.zd[idx] if abs(z-zo) < 2.5 and (xo-x)**2+(yo-y)**2 < 15**2: # FIXME MAGIC NUMBER! overlaps.append((dist,idx)) if len(overlaps) == 0: novl_indices.append(hkl2.indices[i]) novl_i.append(hkl2.iobs[i]) novl_sigma.append(hkl2.sigma_iobs[i]) n_nonovl += 1 else: print hkl2.indices[i], x, y, z for dist, idx in overlaps: xo, yo, zo = hkl1.xd[idx], hkl1.yd[idx], hkl1.zd[idx] print hkl1.indices[idx], xo, yo, zo print dist, idx print print n_ref = len(hkl2.indices) print "%.2f%% overlap!" % (100.*(n_ref-n_nonovl)/n_ref) novl_array = miller.array(miller_set=miller.set(crystal_symmetry=hkl2.symm, indices=novl_indices), data=novl_i, sigmas=novl_sigma) stats = dataset_statistics(novl_array, anomalous=False, sigma_filtering="xds") stats.show(out=sys.stdout) novl_array = novl_array.customized_copy(anomalous_flag=False).map_to_asu() novl_array = novl_array.eliminate_sys_absent() novl_array = novl_array.select(novl_array.sigmas() >= 0) filtr = filter_intensities_by_sigma(novl_array, "xds") hklout = os.path.splitext(os.path.basename(files[1]))[0] + "_novl.mtz" filtr.array_merged.set_observation_type_xray_intensity().as_mtz_dataset(column_root_label="IMEAN").mtz_object().write(hklout)
def exercise(space_group_info, anomalous_flag, d_min=1.0, reflections_per_bin=200, n_bins=10, verbose=0): elements = ("N", "C", "C", "O") * 5 structure_factors = random_structure.xray_structure( space_group_info, elements=elements, volume_per_atom=50., min_distance=1.5, general_positions_only=True, use_u_aniso=False, u_iso=adptbx.b_as_u(10) ).structure_factors( anomalous_flag=anomalous_flag, d_min=d_min, algorithm="direct") if (0 or verbose): structure_factors.xray_structure().show_summary() asu_contents = dicts.with_default_value(0) for elem in elements: asu_contents[elem] += 1 f_calc = abs(structure_factors.f_calc()) f_calc.setup_binner( auto_binning=True, reflections_per_bin=reflections_per_bin, n_bins=n_bins) if (0 or verbose): f_calc.binner().show_summary() for k_given in [1,0.1,0.01,10,100]: f_obs = miller.array( miller_set=f_calc, data=f_calc.data()*k_given).set_observation_type_xray_amplitude() f_obs.use_binner_of(f_calc) wp = statistics.wilson_plot(f_obs, asu_contents, e_statistics=True) if (0 or verbose): print "wilson_k, wilson_b:", wp.wilson_k, wp.wilson_b print "space group:", space_group_info.group().type().hall_symbol() print "<E^2-1>:", wp.mean_e_sq_minus_1 assert 0.8 < wp.wilson_k/k_given < 1.2 assert 0.64 < wp.wilson_intensity_scale_factor/(k_given*k_given) < 1.44 assert 9 < wp.wilson_b < 11 assert wp.xy_plot_info().fit_correlation == wp.fit_correlation if space_group_info.group().is_centric(): assert 0.90 < wp.mean_e_sq_minus_1 < 1.16 assert 3.15 < wp.percent_e_sq_gt_2 < 6.5 else: assert 0.65 < wp.mean_e_sq_minus_1 < 0.90 assert 1.0 < wp.percent_e_sq_gt_2 < 3.15 assert wp.normalised_f_obs.size() == f_obs.size() f_obs = f_calc.array(data=flex.double(f_calc.indices().size(), 0)) f_obs.use_binner_of(f_calc) n_bins = f_obs.binner().n_bins_used() try: statistics.wilson_plot(f_obs, asu_contents) except RuntimeError, e: assert not show_diff(str(e), """\ wilson_plot error: %d empty bins: Number of bins: %d Number of f_obs > 0: 0 Number of f_obs <= 0: %d""" % (n_bins, n_bins, f_obs.indices().size()))
def anomalous_residual_map_coefficients (fmodel, weighted=False, exclude_free_r_reflections=True) : """ EXPERIMENTAL Calculates map coefficients showing the difference in anomalous scattering between F-obs and F-model. Similar to the Phaser SAD LLG map, but appears to be less sensitive. """ assert (fmodel.f_obs().anomalous_flag()) f_obs_anom = fmodel.f_obs().anomalous_differences() f_model_anom = abs(fmodel.f_model()).anomalous_differences() if (weighted) : mch = fmodel.map_calculation_helper() fom = fmodel.f_obs().customized_copy( data=mch.fom, sigmas=None).average_bijvoet_mates() alpha = mch.alpha.average_bijvoet_mates() fom = fom.common_set(other=f_obs_anom) alpha = alpha.common_set(other=f_obs_anom) f_obs_anom = f_obs_anom.customized_copy(data=f_obs_anom.data()*fom.data()) f_model_anom = f_model_anom.customized_copy( data=f_model_anom.data()*alpha.data()) anom_diff_diff = f_obs_anom.customized_copy( data=f_obs_anom.data() - f_model_anom.data()) f_model = fmodel.f_model().as_non_anomalous_array().\ merge_equivalents().array() fmodel_match_anom_diff, anom_diff_diff_common = \ f_model.common_sets(other = anom_diff_diff) assert (anom_diff_diff_common.indices().size() == anom_diff_diff.indices().size()) phases_tmp = miller.array( miller_set=anom_diff_diff_common, data=flex.double(anom_diff_diff_common.indices().size(), 1) ).phase_transfer(phase_source=fmodel_match_anom_diff) map_coeffs = miller.array( miller_set=anom_diff_diff_common, data = anom_diff_diff_common.data() * phases_tmp.data()) if (exclude_free_r_reflections) : r_free_flags = fmodel.r_free_flags().average_bijvoet_mates() r_free_flags, map_coeffs = r_free_flags.common_sets(map_coeffs) map_coeffs = map_coeffs.select(~(r_free_flags.data())) return miller.array( miller_set=map_coeffs, data=map_coeffs.data()/(2j))
def i_obs(self, anomalous_flag=None): assert "IOBS" in self.data assert "SIGMA" in self.data array_info = miller.array_info(source_type="xds_integrate")#, wavelength=) return miller.array(miller_set=miller.set(crystal_symmetry=self.crystal_symmetry(), indices=self.hkl, anomalous_flag=anomalous_flag), data=self.data["IOBS"], sigmas=self.data["SIGMA"]).set_info(array_info).set_observation_type_xray_intensity()
def exercise_sys_absent_intensity_distribution(): xs = crystal.symmetry((3,4,5), "F222") mi = flex.miller_index(((1,2,3), (1,1,1), (1,2,2), (0,0,4))) ms = miller.set(xs, mi) data = flex.double((-1,-2,3,4)) sigmas = flex.double((2,3,4,5)) f_obs = miller.array(ms, data=data, sigmas=sigmas).set_observation_type_xray_intensity() dist = statistics.sys_absent_intensity_distribution(f_obs) assert approx_equal(dist.x,(-0.5,0.75)) assert approx_equal(dist.y,(-1,3)) assert approx_equal(dist.indices,((1,2,3), (1,2,2)))
def alpha_beta_for_each_reflection(self, f_obs=None): if f_obs is None: f_obs = self.f_obs alpha = flex.double(f_obs.size()) beta = flex.double(f_obs.size()) f_obs.setup_binner(n_bins= len(self.alpha_in_zones)) binner = f_obs.binner() if(self.interpolation == True): az = flex.double(self.smooth(self.alpha_in_zones)) bz = flex.double(self.smooth(self.beta_in_zones) ) alpha = binner.interpolate(az, 0) beta = binner.interpolate(bz, 0) elif(self.interpolation == False): for i_bin, az, bz in zip(binner.range_used(),self.alpha_in_zones, self.beta_in_zones): sel = binner.selection(i_bin) alpha.set_selected(sel, az) beta.set_selected(sel, bz) alpha = miller.array(miller_set=f_obs, data=alpha) beta = miller.array(miller_set=f_obs, data=beta) return alpha, beta
def arrays(self): ret = {} for key in self.data: arr = miller.array(miller_set=miller.set(crystal_symmetry=self.crystal_symmetry(), indices=self.hkl, anomalous_flag=False), data=self.data[key]) ret[key] = arr return ret