Esempio n. 1
0
 def calc_mean_intensity(self, pickle_filename, iparams, avg_mode):
     observations_pickle = read_frame(pickle_filename)
     pickle_filepaths = pickle_filename.split('/')
     txt_exception = ' {0:40} ==> '.format(
         pickle_filepaths[len(pickle_filepaths) - 1])
     inputs, txt_organize_input = self.organize_input(
         observations_pickle,
         iparams,
         avg_mode,
         pickle_filename=pickle_filename)
     if inputs is not None:
         observations_original, alpha_angle_obs, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, wavelength, crystal_init_orientation = inputs
     else:
         txt_exception += txt_organize_input + '\n'
         return None, txt_exception
     #filter resolution
     observations_sel = observations_original.resolution_filter(
         d_min=iparams.scale.d_min, d_max=iparams.scale.d_max)
     #filer sigma
     i_sel = (observations_sel.data() /
              observations_sel.sigmas()) > iparams.scale.sigma_min
     if len(observations_sel.data().select(i_sel)) == 0:
         return None, txt_exception
     mean_I = flex.median(observations_sel.data().select(i_sel))
     return mean_I, txt_exception + 'ok'
Esempio n. 2
0
 def scale_frame_by_mean_I(self, frame_no, pickle_filename, iparams,
                           mean_of_mean_I, avg_mode):
     observations_pickle = read_frame(pickle_filename)
     pickle_filepaths = pickle_filename.split('/')
     img_filename_only = pickle_filepaths[len(pickle_filepaths) - 1]
     inputs, txt_organize_input = self.organize_input(
         observations_pickle,
         iparams,
         avg_mode,
         pickle_filename=pickle_filename)
     txt_exception = ' {0:40} ==> '.format(img_filename_only)
     if inputs is not None:
         observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, wavelength, crystal_init_orientation = inputs
     else:
         txt_exception += txt_organize_input + '\n'
         return None, txt_exception
     #select only reflections matched with scale input params.
     #filter by resolution
     i_sel_res = observations_original.resolution_filter_selection(
         d_min=iparams.scale.d_min, d_max=iparams.scale.d_max)
     observations_original_sel = observations_original.select(i_sel_res)
     alpha_angle_sel = alpha_angle.select(i_sel_res)
     spot_pred_x_mm_sel = spot_pred_x_mm.select(i_sel_res)
     spot_pred_y_mm_sel = spot_pred_y_mm.select(i_sel_res)
     #filter by sigma
     i_sel_sigmas = (
         observations_original_sel.data() /
         observations_original_sel.sigmas()) > iparams.scale.sigma_min
     observations_original_sel = observations_original_sel.select(
         i_sel_sigmas)
     alpha_angle_sel = alpha_angle_sel.select(i_sel_sigmas)
     spot_pred_x_mm_sel = spot_pred_x_mm_sel.select(i_sel_sigmas)
     spot_pred_y_mm_sel = spot_pred_y_mm_sel.select(i_sel_sigmas)
     observations_non_polar_sel, index_basis_name = self.get_observations_non_polar(
         observations_original_sel, pickle_filename, iparams)
     observations_non_polar, index_basis_name = self.get_observations_non_polar(
         observations_original, pickle_filename, iparams)
     uc_params = observations_original.unit_cell().parameters()
     ph = partiality_handler()
     r0 = ph.calc_spot_radius(
         sqr(crystal_init_orientation.reciprocal_matrix()),
         observations_original_sel.indices(), wavelength)
     #calculate first G
     (G, B) = (1, 0)
     stats = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
     if mean_of_mean_I > 0:
         G = flex.median(observations_original_sel.data()) / mean_of_mean_I
     if iparams.flag_apply_b_by_frame:
         try:
             mxh = mx_handler()
             asu_contents = mxh.get_asu_contents(iparams.n_residues)
             observations_as_f = observations_non_polar_sel.as_amplitude_array(
             )
             binner_template_asu = observations_as_f.setup_binner(
                 auto_binning=True)
             wp = statistics.wilson_plot(observations_as_f,
                                         asu_contents,
                                         e_statistics=True)
             G = wp.wilson_intensity_scale_factor * 1e2
             B = wp.wilson_b
         except Exception:
             txt_exception += 'warning B-factor calculation failed.\n'
             return None, txt_exception
     two_theta = observations_original.two_theta(
         wavelength=wavelength).data()
     sin_theta_over_lambda_sq = observations_original.two_theta(
         wavelength=wavelength).sin_theta_over_lambda_sq().data()
     ry, rz, re, voigt_nu, rotx, roty = (0, 0, iparams.gamma_e,
                                         iparams.voigt_nu, 0, 0)
     partiality_init, delta_xy_init, rs_init, rh_init = ph.calc_partiality_anisotropy_set(\
                                                           crystal_init_orientation.unit_cell(),
                                                           rotx, roty, observations_original.indices(),
                                                           ry, rz, r0, re, voigt_nu,
                                                           two_theta, alpha_angle, wavelength,
                                                           crystal_init_orientation, spot_pred_x_mm, spot_pred_y_mm,
                                                           detector_distance_mm, iparams.partiality_model,
                                                           iparams.flag_beam_divergence)
     if iparams.flag_plot_expert:
         n_bins = 20
         binner = observations_original.setup_binner(n_bins=n_bins)
         binner_indices = binner.bin_indices()
         avg_partiality_init = flex.double()
         avg_rs_init = flex.double()
         avg_rh_init = flex.double()
         one_dsqr_bin = flex.double()
         for i in range(1, n_bins + 1):
             i_binner = (binner_indices == i)
             if len(observations_original.data().select(i_binner)) > 0:
                 print binner.bin_d_range(i)[1], flex.mean(
                     partiality_init.select(i_binner)), flex.mean(
                         rs_init.select(i_binner)), flex.mean(
                             rh_init.select(i_binner)), len(
                                 partiality_init.select(i_binner))
     #monte-carlo merge
     if iparams.flag_monte_carlo:
         G = 1
         B = 0
         partiality_init = flex.double([1] * len(partiality_init))
     #save results
     refined_params = flex.double([
         G, B, rotx, roty, ry, rz, r0, re, voigt_nu, uc_params[0],
         uc_params[1], uc_params[2], uc_params[3], uc_params[4],
         uc_params[5]
     ])
     pres = postref_results()
     pres.set_params(observations=observations_non_polar,
                     observations_original=observations_original,
                     refined_params=refined_params,
                     stats=stats,
                     partiality=partiality_init,
                     rs_set=rs_init,
                     rh_set=rh_init,
                     frame_no=frame_no,
                     pickle_filename=pickle_filename,
                     wavelength=wavelength,
                     crystal_orientation=crystal_init_orientation,
                     detector_distance_mm=detector_distance_mm)
     txt_scale_frame_by_mean_I = ' {0:40} ==> RES:{1:5.2f} NREFL:{2:5d} G:{3:6.4f} B:{4:6.1f} CELL:{5:6.2f} {6:6.2f} {7:6.2f} {8:6.2f} {9:6.2f} {10:6.2f}'.format(
         img_filename_only + ' (' + index_basis_name + ')',
         observations_original.d_min(),
         len(observations_original_sel.data()), G, B, uc_params[0],
         uc_params[1], uc_params[2], uc_params[3], uc_params[4],
         uc_params[5])
     print txt_scale_frame_by_mean_I
     txt_scale_frame_by_mean_I += '\n'
     return pres, txt_scale_frame_by_mean_I
Esempio n. 3
0
 def postrefine_by_frame(self, frame_no, pickle_filename, iparams,
                         miller_array_ref, pres_in, avg_mode):
     #1. Prepare data
     observations_pickle = read_frame(pickle_filename)
     pickle_filepaths = pickle_filename.split('/')
     img_filename_only = pickle_filepaths[len(pickle_filepaths) - 1]
     txt_exception = ' {0:40} ==> '.format(img_filename_only)
     inputs, txt_organize_input = self.organize_input(
         observations_pickle,
         iparams,
         avg_mode,
         pickle_filename=pickle_filename)
     if inputs is not None:
         observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, wavelength, crystal_init_orientation = inputs
     else:
         txt_exception += txt_organize_input + '\n'
         return None, txt_exception
     #2. Select data for post-refinement (only select indices that are common with the reference set
     observations_non_polar, index_basis_name = self.get_observations_non_polar(
         observations_original, pickle_filename, iparams)
     matches = miller.match_multi_indices(
         miller_indices_unique=miller_array_ref.indices(),
         miller_indices=observations_non_polar.indices())
     pair_0 = flex.size_t([pair[0] for pair in matches.pairs()])
     pair_1 = flex.size_t([pair[1] for pair in matches.pairs()])
     references_sel = miller_array_ref.select(pair_0)
     observations_original_sel = observations_original.select(pair_1)
     observations_non_polar_sel = observations_non_polar.select(pair_1)
     alpha_angle_set = alpha_angle.select(pair_1)
     spot_pred_x_mm_set = spot_pred_x_mm.select(pair_1)
     spot_pred_y_mm_set = spot_pred_y_mm.select(pair_1)
     #4. Do least-squares refinement
     lsqrh = leastsqr_handler()
     try:
         refined_params, stats, n_refl_postrefined = lsqrh.optimize(
             references_sel.data(), observations_original_sel, wavelength,
             crystal_init_orientation, alpha_angle_set, spot_pred_x_mm_set,
             spot_pred_y_mm_set, iparams, pres_in,
             observations_non_polar_sel, detector_distance_mm)
     except Exception:
         txt_exception += 'optimization failed.\n'
         return None, txt_exception
     #caculate partiality for output (with target_anomalous check)
     G_fin, B_fin, rotx_fin, roty_fin, ry_fin, rz_fin, r0_fin, re_fin, voigt_nu_fin, \
         a_fin, b_fin, c_fin, alpha_fin, beta_fin, gamma_fin = refined_params
     inputs, txt_organize_input = self.organize_input(
         observations_pickle,
         iparams,
         avg_mode,
         pickle_filename=pickle_filename)
     observations_original, alpha_angle, spot_pred_x_mm, spot_pred_y_mm, detector_distance_mm, wavelength, crystal_init_orientation = inputs
     observations_non_polar, index_basis_name = self.get_observations_non_polar(
         observations_original, pickle_filename, iparams)
     from cctbx.uctbx import unit_cell
     uc_fin = unit_cell(
         (a_fin, b_fin, c_fin, alpha_fin, beta_fin, gamma_fin))
     if pres_in is not None:
         crystal_init_orientation = pres_in.crystal_orientation
     two_theta = observations_original.two_theta(
         wavelength=wavelength).data()
     ph = partiality_handler()
     partiality_fin, dummy, rs_fin, rh_fin = ph.calc_partiality_anisotropy_set(
         uc_fin, rotx_fin, roty_fin, observations_original.indices(),
         ry_fin, rz_fin, r0_fin, re_fin, voigt_nu_fin, two_theta,
         alpha_angle, wavelength, crystal_init_orientation, spot_pred_x_mm,
         spot_pred_y_mm, detector_distance_mm, iparams.partiality_model,
         iparams.flag_beam_divergence)
     #calculate the new crystal orientation
     O = sqr(uc_fin.orthogonalization_matrix()).transpose()
     R = sqr(crystal_init_orientation.crystal_rotation_matrix()).transpose()
     from cctbx.crystal_orientation import crystal_orientation, basis_type
     CO = crystal_orientation(O * R, basis_type.direct)
     crystal_fin_orientation = CO.rotate_thru(
         (1, 0, 0), rotx_fin).rotate_thru((0, 1, 0), roty_fin)
     #remove reflections with partiality below threshold
     i_sel = partiality_fin > iparams.merge.partiality_min
     partiality_fin_sel = partiality_fin.select(i_sel)
     rs_fin_sel = rs_fin.select(i_sel)
     rh_fin_sel = rh_fin.select(i_sel)
     observations_non_polar_sel = observations_non_polar.customized_copy(\
         indices=observations_non_polar.indices().select(i_sel),
         data=observations_non_polar.data().select(i_sel),
         sigmas=observations_non_polar.sigmas().select(i_sel))
     observations_original_sel = observations_original.customized_copy(\
         indices=observations_original.indices().select(i_sel),
         data=observations_original.data().select(i_sel),
         sigmas=observations_original.sigmas().select(i_sel))
     pres = postref_results()
     pres.set_params(observations=observations_non_polar_sel,
                     observations_original=observations_original_sel,
                     refined_params=refined_params,
                     stats=stats,
                     partiality=partiality_fin_sel,
                     rs_set=rs_fin_sel,
                     rh_set=rh_fin_sel,
                     frame_no=frame_no,
                     pickle_filename=pickle_filename,
                     wavelength=wavelength,
                     crystal_orientation=crystal_fin_orientation,
                     detector_distance_mm=detector_distance_mm)
     r_change = ((pres.R_final - pres.R_init) / pres.R_init) * 100
     r_xy_change = (
         (pres.R_xy_final - pres.R_xy_init) / pres.R_xy_init) * 100
     cc_change = ((pres.CC_final - pres.CC_init) / pres.CC_init) * 100
     txt_postref = '{0:40} => RES:{1:5.2f} NREFL:{2:5d} R:{3:6.1f}% RXY:{4:5.1f}% CC:{5:5.1f}% G:{6:6.4f} B:{7:5.1f} CELL:{8:6.1f}{9:6.1f} {10:6.1f} {11:5.1f} {12:5.1f} {13:5.1f}'.format(
         img_filename_only + ' (' + index_basis_name + ')',
         observations_original_sel.d_min(),
         len(observations_original_sel.data()), r_change, r_xy_change,
         cc_change, pres.G, pres.B, a_fin, b_fin, c_fin, alpha_fin,
         beta_fin, gamma_fin)
     print txt_postref
     txt_postref += '\n'
     return pres, txt_postref