def report(O, plot=None, xy_prefix=None):
        from cctbx.array_family import flex
        print "Number of shots:", O.completeness_history.size() - 1
        print
        print "Histogram of counts per reflection:"
        flex.histogram(O.counts.as_double(),
                       n_slots=8).show(prefix="  ", format_cutoffs="%7.0f")
        print
        print "Observations per reflection:"
        flex.show_count_stats(counts=O.counts, prefix="  ")
        print "  Median:", int(flex.median(O.counts.as_double()) + 0.5)
        print
        sys.stdout.flush()
        if (xy_prefix is None):
            xy_prefix = ""
        elif (len(xy_prefix) != 0):
            xy_prefix = xy_prefix + "_"

        def dump_xy(name, array):
            f = open(xy_prefix + "%s.xy" % name, "w")
            for i, c in enumerate(array):
                print >> f, i, c

        dump_xy("completeness_history", O.completeness_history)
        dump_xy("min_count_history", O.min_count_history)
        if (O.use_symmetry): _ = O.i_calc.asu
        else: _ = O.i_calc.p1_anom
        _ = _.customized_copy(data=O.counts).sort(by_value="resolution")
        sym_factors = _.space_group().order_p()
        if (not O.i_calc.asu.anomalous_flag()):
            sym_factors *= 2
        sym_factors /= _.multiplicities().data()
        counts_sorted_by_resolution = _.data().as_int() * sym_factors
        dump_xy("counts_sorted_by_resolution", counts_sorted_by_resolution)
        dump_xy("d_spacings_sorted_by_resolution", _.d_spacings().data())
        if (plot == "completeness"):
            from libtbx import pyplot
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            _ = O.completeness_history
            nx = _.size()
            ax.plot(range(nx), _, "r-")
            ax.axis([0, nx, 0, 1])
            pyplot.show()
        elif (plot == "redundancy"):
            from libtbx import pyplot
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            _ = counts_sorted_by_resolution
            ax.plot(range(len(_)), _, "r-")
            ax.axis([-_.size() * 0.05, _.size() * 1.05, 0, None])
            pyplot.show()
        elif (plot is not None):
            raise RuntimeError('Unknown plot type: "%s"' % plot)
 def report(O, plot=None, xy_prefix=None):
   from cctbx.array_family import flex
   print "Number of shots:", O.completeness_history.size()-1
   print
   print "Histogram of counts per reflection:"
   flex.histogram(O.counts.as_double(), n_slots=8).show(
     prefix="  ", format_cutoffs="%7.0f")
   print
   print "Observations per reflection:"
   flex.show_count_stats(counts=O.counts, prefix="  ")
   print "  Median:", int(flex.median(O.counts.as_double())+0.5)
   print
   sys.stdout.flush()
   if (xy_prefix is None):
     xy_prefix = ""
   elif (len(xy_prefix) != 0):
     xy_prefix = xy_prefix + "_"
   def dump_xy(name, array):
     f = open(xy_prefix + "%s.xy" % name, "w")
     for i,c in enumerate(array):
       print >> f, i, c
   dump_xy("completeness_history", O.completeness_history)
   dump_xy("min_count_history", O.min_count_history)
   if (O.use_symmetry): _ = O.i_calc.asu
   else:                _ = O.i_calc.p1_anom
   _ = _.customized_copy(data=O.counts).sort(by_value="resolution")
   sym_factors = _.space_group().order_p()
   if (not O.i_calc.asu.anomalous_flag()):
     sym_factors *= 2
   sym_factors /= _.multiplicities().data()
   counts_sorted_by_resolution = _.data().as_int() * sym_factors
   dump_xy("counts_sorted_by_resolution", counts_sorted_by_resolution)
   dump_xy("d_spacings_sorted_by_resolution", _.d_spacings().data())
   if (plot == "completeness"):
     from libtbx import pyplot
     fig = pyplot.figure()
     ax = fig.add_subplot(1, 1, 1)
     _ = O.completeness_history
     nx = _.size()
     ax.plot(range(nx), _, "r-")
     ax.axis([0, nx, 0, 1])
     pyplot.show()
   elif (plot == "redundancy"):
     from libtbx import pyplot
     fig = pyplot.figure()
     ax = fig.add_subplot(1, 1, 1)
     _ = counts_sorted_by_resolution
     ax.plot(range(len(_)), _, "r-")
     ax.axis([-_.size()*0.05, _.size()*1.05, 0, None])
     pyplot.show()
   elif (plot is not None):
     raise RuntimeError('Unknown plot type: "%s"' % plot)
def run(args):
    assert len(args) == 0
    n_samples = 1000
    from matplotlib.backends.backend_pdf import PdfPages
    all_pdf = PdfPages("all.pdf")
    from libtbx import pyplot
    from scitbx.array_family import flex
    for element in e_scattering.ito_vol_c_2011_table_4_3_2_2_elements():
        fig = pyplot.figure()
        fig.set_size_inches(11, 8.5)
        ax = fig.add_subplot(1, 1, 1)
        ax.set_title(element, fontsize=12)

        def one_curv(g, code):
            x = flex.double()
            y = flex.double()
            for i_stol in xrange(n_samples + 1):
                stol = 6 * i_stol / n_samples
                x.append(stol)
                y.append(g.at_stol(stol))
            ax.plot(x.as_numpy_array(), y.as_numpy_array(), code)

        g = e_scattering.ito_vol_c_2011_table_4_3_2_2_entry_as_gaussian(
            label=element, exact=True)
        one_curv(g, "b-")
        one_curv(e_scattering.gaussian(g.array_of_a()[:4],
                                       g.array_of_b()[:4]), "r-")
        all_pdf.savefig(fig, bbox_inches="tight")
    all_pdf.close()
    print "plots written to file: all.pdf"
def run(args):
  assert len(args) == 0
  n_samples = 1000
  from matplotlib.backends.backend_pdf import PdfPages
  all_pdf = PdfPages("all.pdf")
  from libtbx import pyplot
  from scitbx.array_family import flex
  for element in e_scattering.ito_vol_c_2011_table_4_3_2_2_elements():
    fig = pyplot.figure()
    fig.set_size_inches(11, 8.5)
    ax = fig.add_subplot(1, 1, 1)
    ax.set_title(element, fontsize=12)
    def one_curv(g, code):
      x = flex.double()
      y = flex.double()
      for i_stol in xrange(n_samples+1):
        stol = 6 * i_stol / n_samples
        x.append(stol)
        y.append(g.at_stol(stol))
      ax.plot(x.as_numpy_array(), y.as_numpy_array(), code)
    g = e_scattering.ito_vol_c_2011_table_4_3_2_2_entry_as_gaussian(
      label=element, exact=True)
    one_curv(g, "b-")
    one_curv(e_scattering.gaussian(
      g.array_of_a()[:4],
      g.array_of_b()[:4]), "r-")
    all_pdf.savefig(fig, bbox_inches="tight")
  all_pdf.close()
  print "plots written to file: all.pdf"
def run(args, log=sys.stdout, as_gui_program=False):
    if (len(args) == 0):
        parsed = defaults(log=log)
        parsed.show(prefix="  ", out=log)
        return
    command_line = (option_parser().enable_symmetry_comprehensive().option(
        "-q",
        "--quiet",
        action="store_true",
        default=False,
        help="suppress output").option("--output_plots",
                                       action="store_true",
                                       default=False)).process(args=args)
    parsed = defaults(log=log)
    processed_args = mmtbx.utils.process_command_line_args(
        args=command_line.args,
        cmd_cs=command_line.symmetry,
        master_params=parsed,
        log=log,
        suppress_symmetry_related_errors=True)
    processed_args.params.show(out=log)
    params = processed_args.params.extract().density_modification
    output_plots = command_line.options.output_plots

    crystal_symmetry = crystal.symmetry(
        unit_cell=params.input.unit_cell,
        space_group_info=params.input.space_group)
    reflection_files = {}
    for rfn in (params.input.reflection_data.file_name,
                params.input.experimental_phases.file_name,
                params.input.map_coefficients.file_name):
        if os.path.isfile(str(rfn)) and rfn not in reflection_files:
            reflection_files.setdefault(
                rfn,
                iotbx.reflection_file_reader.any_reflection_file(
                    file_name=rfn, ensure_read_access=False))
    # TODO is reflection_files a dict ?
    server = iotbx.reflection_file_utils.reflection_file_server(
        crystal_symmetry=crystal_symmetry,
        reflection_files=list(reflection_files.values()))
    fo = mmtbx.utils.determine_data_and_flags(
        server,
        parameters=params.input.reflection_data,
        extract_r_free_flags=False,
        log=log).f_obs
    hl_coeffs = mmtbx.utils.determine_experimental_phases(
        server,
        params.input.experimental_phases,
        log=log,
        parameter_scope="",
        working_point_group=None,
        symmetry_safety_check=True,
        ignore_all_zeros=True)
    if params.input.map_coefficients.file_name is not None:
        map_coeffs = server.get_phases_deg(
            file_name=params.input.map_coefficients.file_name,
            labels=params.input.map_coefficients.labels,
            convert_to_phases_if_necessary=False,
            original_phase_units=None,
            parameter_scope="",
            parameter_name="labels").map_to_asu()
    else:
        map_coeffs = None
    ncs_object = None
    if params.input.ncs_file_name is not None:
        ncs_object = ncs.ncs()
        ncs_object.read_ncs(params.input.ncs_file_name)
        ncs_object.display_all(log=log)

    fo = fo.map_to_asu()
    hl_coeffs = hl_coeffs.map_to_asu()

    fo = fo.eliminate_sys_absent().average_bijvoet_mates()
    hl_coeffs = hl_coeffs.eliminate_sys_absent().average_bijvoet_mates()

    model_map = None
    model_map_coeffs = None
    if len(processed_args.pdb_file_names):
        pdb_inp = mmtbx.utils.pdb_inp_from_multiple_files(
            pdb_files=processed_args.pdb_file_names, log=log)
        xs = pdb_inp.xray_structure_simple()
        fo_, hl_ = fo, hl_coeffs
        if params.change_basis_to_niggli_cell:
            change_of_basis_op = xs.change_of_basis_op_to_niggli_cell()
            xs = xs.change_basis(change_of_basis_op)
            fo_ = fo_.change_basis(change_of_basis_op).map_to_asu()
            hl_ = hl_.change_basis(change_of_basis_op).map_to_asu()
        #fo_, hl_ = fo_.common_sets(hl_)
        fmodel_refined = mmtbx.utils.fmodel_simple(
            f_obs=fo_,
            scattering_table=
            "wk1995",  #XXX pva: 1) neutrons? 2) move up as a parameter.
            xray_structures=[xs],
            bulk_solvent_correction=True,
            anisotropic_scaling=True,
            r_free_flags=fo_.array(data=flex.bool(fo_.size(), False)))
        fmodel_refined.update(abcd=hl_)

        master_phil = mmtbx.maps.map_and_map_coeff_master_params()
        map_params = master_phil.fetch(
            iotbx.phil.parse("""\
map_coefficients {
  map_type = 2mFo-DFc
  isotropize = True
}
""")).extract().map_coefficients[0]
        model_map_coeffs = mmtbx.maps.map_coefficients_from_fmodel(
            fmodel=fmodel_refined, params=map_params)
        model_map = model_map_coeffs.fft_map(
            resolution_factor=params.grid_resolution_factor).real_map_unpadded(
            )

    import time

    t0 = time.time()
    dm = density_modify(params,
                        fo,
                        hl_coeffs,
                        ncs_object=ncs_object,
                        map_coeffs=map_coeffs,
                        model_map_coeffs=model_map_coeffs,
                        log=log,
                        as_gui_program=as_gui_program)
    time_dm = time.time() - t0
    print("Time taken for density modification: %.2fs" % time_dm, file=log)
    # run cns
    if 0:
        from cctbx.development import cns_density_modification
        cns_result = cns_density_modification.run(params, fo, hl_coeffs)
        print(cns_result.modified_map.all())
        print(dm.map.all())
        dm_map_coeffs = dm.map_coeffs_in_original_setting
        from cctbx import maptbx, miller
        crystal_gridding = maptbx.crystal_gridding(
            dm_map_coeffs.unit_cell(),
            space_group_info=dm_map_coeffs.space_group().info(),
            pre_determined_n_real=cns_result.modified_map.all())
        dm_map = miller.fft_map(crystal_gridding,
                                dm_map_coeffs).apply_sigma_scaling()
        corr = flex.linear_correlation(cns_result.modified_map.as_1d(),
                                       dm_map.real_map_unpadded().as_1d())
        print("CNS dm/mmtbx dm correlation:")
        corr.show_summary()
        if dm.model_map_coeffs is not None:
            model_map = miller.fft_map(
                crystal_gridding,
                dm.miller_array_in_original_setting(
                    dm.model_map_coeffs)).apply_sigma_scaling()
            corr = flex.linear_correlation(
                cns_result.modified_map.as_1d(),
                model_map.real_map_unpadded().as_1d())
            print("CNS dm/model correlation:")
            corr.show_summary()

    if output_plots:
        plots_to_make = (
            "fom",
            "skewness",
            "r1_factor",
            "r1_factor_fom",
            "mean_solvent_density",
            "mean_protein_density",
            "f000_over_v",
            "k_flip",
            "rms_solvent_density",
            "rms_protein_density",
            "standard_deviation_local_rms",
            "mean_delta_phi",
            "mean_delta_phi_initial",
        )
        from matplotlib.backends.backend_pdf import PdfPages
        from libtbx import pyplot

        stats = dm.get_stats()
        pdf = PdfPages("density_modification.pdf")

        if len(dm.correlation_coeffs) > 1:
            if 0:
                start_coeffs, model_coeffs = dm.map_coeffs_start.common_sets(
                    model_map_coeffs)
                model_phases = model_coeffs.phases(deg=True).data()
                exptl_phases = nearest_phase(
                    model_phases,
                    start_coeffs.phases(deg=True).data(),
                    deg=True)
                corr = flex.linear_correlation(exptl_phases, model_phases)
                corr.show_summary()
                fig = pyplot.figure()
                ax = fig.add_subplot(1, 1, 1)
                ax.set_title("phases start")
                ax.set_xlabel("Experimental phases")
                ax.set_ylabel("Phases from refined model")
                ax.scatter(exptl_phases, model_phases, marker="x", s=10)
                pdf.savefig(fig)
                #
                dm_coeffs, model_coeffs = dm.map_coeffs.common_sets(
                    model_map_coeffs)
                model_phases = model_coeffs.phases(deg=True).data()
                dm_phases = nearest_phase(model_phases,
                                          dm_coeffs.phases(deg=True).data(),
                                          deg=True)
                corr = flex.linear_correlation(dm_phases, model_phases)
                corr.show_summary()
                fig = pyplot.figure()
                ax = fig.add_subplot(1, 1, 1)
                ax.set_title("phases dm")
                ax.set_xlabel("Phases from density modification")
                ax.set_ylabel("Phases from refined model")
                ax.scatter(dm_phases, model_phases, marker="x", s=10)
                pdf.savefig(fig)
            #
            data = dm.correlation_coeffs
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_title("correlation coefficient")
            ax.plot(list(range(1, dm.i_cycle + 2)), data)
            pdf.savefig(fig)
            #
            data = dm.mean_phase_errors
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_title("Mean effective phase errors")
            ax.plot(list(range(1, dm.i_cycle + 2)), data)
            pdf.savefig(fig)

        for plot in plots_to_make:
            data = [
                getattr(stats.get_cycle_stats(i), plot)
                for i in range(1, dm.i_cycle + 2)
            ]
            fig = pyplot.figure()
            ax = fig.add_subplot(1, 1, 1)
            ax.set_title(plot.replace("_", " "))
            ax.plot(list(range(1, dm.i_cycle + 2)), data)
            pdf.savefig(fig)

        data = [
            stats.get_cycle_stats(i).rms_solvent_density /
            stats.get_cycle_stats(i).rms_protein_density
            for i in range(1, dm.i_cycle + 2)
        ]
        fig = pyplot.figure()
        ax = fig.add_subplot(1, 1, 1)
        ax.set_title("RMS solvent/protein density ratio")
        ax.plot(list(range(1, dm.i_cycle + 2)), data)
        pdf.savefig(fig)

        pdf.close()

    dm_map_coeffs = dm.map_coeffs_in_original_setting
    dm_hl_coeffs = dm.hl_coeffs_in_original_setting

    # output map if requested
    map_params = params.output.map
    if map_params.file_name is not None:
        fft_map = dm_map_coeffs.fft_map(
            resolution_factor=params.grid_resolution_factor)
        if map_params.scale == "sigma":
            fft_map.apply_sigma_scaling()
        else:
            fft_map.apply_volume_scaling()
        gridding_first = gridding_last = None
        title_lines = []
        if map_params.format == "xplor":
            fft_map.as_xplor_map(file_name=map_params.file_name,
                                 title_lines=title_lines,
                                 gridding_first=gridding_first,
                                 gridding_last=gridding_last)
        else:
            fft_map.as_ccp4_map(file_name=map_params.file_name,
                                gridding_first=gridding_first,
                                gridding_last=gridding_last,
                                labels=title_lines)

    # output map coefficients if requested
    mtz_params = params.output.mtz

    # Decide if we are going to actually write the mtz
    if mtz_params.file_name is not None:
        orig_fom, final_fom = dm.start_and_end_fom()
        if mtz_params.skip_output_if_worse and final_fom < orig_fom:
            ok_to_write_mtz = False
            print(
                "Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)"
                % (final_fom, orig_fom))
        else:  # usual
            ok_to_write_mtz = True
    else:
        ok_to_write_mtz = True

    if mtz_params.file_name is not None and ok_to_write_mtz:
        label_decorator = iotbx.mtz.ccp4_label_decorator()
        fo = dm.miller_array_in_original_setting(
            dm.f_obs_complete).common_set(dm_map_coeffs)
        mtz_dataset = fo.as_mtz_dataset(column_root_label="F",
                                        label_decorator=label_decorator)
        mtz_dataset.add_miller_array(dm_map_coeffs,
                                     column_root_label="FWT",
                                     label_decorator=label_decorator)
        phase_source = dm.miller_array_in_original_setting(
            dm.phase_source).common_set(dm_map_coeffs)
        mtz_dataset.add_miller_array(
            phase_source.array(data=flex.abs(phase_source.data())),
            column_root_label="FOM",
            column_types='W',
            label_decorator=label_decorator)
        mtz_dataset.add_miller_array(
            phase_source.array(data=phase_source.phases(deg=True).data()),
            column_root_label="PHIB",
            column_types='P',
            label_decorator=None)
        if mtz_params.output_hendrickson_lattman_coefficients:
            mtz_dataset.add_miller_array(dm_hl_coeffs,
                                         column_root_label="HL",
                                         label_decorator=label_decorator)
        mtz_dataset.mtz_object().write(mtz_params.file_name)

    return result(map_file=map_params.file_name,
                  mtz_file=mtz_params.file_name,
                  stats=dm.get_stats())
def run(args, log = sys.stdout, as_gui_program=False):
  if(len(args)==0):
    parsed = defaults(log=log)
    parsed.show(prefix="  ", out=log)
    return
  command_line = (option_parser()
                  .enable_symmetry_comprehensive()
                  .option("-q", "--quiet",
                          action="store_true",
                          default=False,
                          help="suppress output")
                  .option("--output_plots",
                          action="store_true",
                          default=False)
                  ).process(args=args)
  parsed = defaults(log=log)
  processed_args = mmtbx.utils.process_command_line_args(
    args=command_line.args,
    cmd_cs=command_line.symmetry,
    master_params=parsed,
    log=log,
    suppress_symmetry_related_errors=True)
  processed_args.params.show(out=log)
  params = processed_args.params.extract().density_modification
  output_plots = command_line.options.output_plots

  crystal_symmetry = crystal.symmetry(
    unit_cell=params.input.unit_cell,
    space_group_info=params.input.space_group)
  reflection_files = {}
  for rfn in (params.input.reflection_data.file_name,
              params.input.experimental_phases.file_name,
              params.input.map_coefficients.file_name):
    if os.path.isfile(str(rfn)) and rfn not in reflection_files:
      reflection_files.setdefault(
        rfn, iotbx.reflection_file_reader.any_reflection_file(
          file_name=rfn, ensure_read_access=False))
  server = iotbx.reflection_file_utils.reflection_file_server(
    crystal_symmetry=crystal_symmetry,
    reflection_files=reflection_files.values())
  fo = mmtbx.utils.determine_data_and_flags(
    server,
    parameters=params.input.reflection_data,
    extract_r_free_flags=False,log=log).f_obs
  hl_coeffs = mmtbx.utils.determine_experimental_phases(
    server,
    params.input.experimental_phases,
    log=log,
    parameter_scope="",
    working_point_group=None,
    symmetry_safety_check=True,
    ignore_all_zeros=True)
  if params.input.map_coefficients.file_name is not None:
    map_coeffs = server.get_phases_deg(
      file_name=params.input.map_coefficients.file_name,
      labels=params.input.map_coefficients.labels,
      convert_to_phases_if_necessary=False,
      original_phase_units=None,
      parameter_scope="",
      parameter_name="labels").map_to_asu()
  else:
    map_coeffs = None
  ncs_object = None
  if params.input.ncs_file_name is not None:
    ncs_object = ncs.ncs()
    ncs_object.read_ncs(params.input.ncs_file_name)
    ncs_object.display_all(log=log)

  fo = fo.map_to_asu()
  hl_coeffs = hl_coeffs.map_to_asu()

  fo = fo.eliminate_sys_absent().average_bijvoet_mates()
  hl_coeffs = hl_coeffs.eliminate_sys_absent().average_bijvoet_mates()

  model_map = None
  model_map_coeffs = None
  if len(processed_args.pdb_file_names):
    pdb_file = mmtbx.utils.pdb_file(
      pdb_file_names=processed_args.pdb_file_names)
    xs = pdb_file.pdb_inp.xray_structure_simple()
    fo_, hl_ = fo, hl_coeffs
    if params.change_basis_to_niggli_cell:
      change_of_basis_op = xs.change_of_basis_op_to_niggli_cell()
      xs = xs.change_basis(change_of_basis_op)
      fo_ = fo_.change_basis(change_of_basis_op).map_to_asu()
      hl_ = hl_.change_basis(change_of_basis_op).map_to_asu()
    #fo_, hl_ = fo_.common_sets(hl_)
    fmodel_refined = mmtbx.utils.fmodel_simple(
      f_obs=fo_,
      scattering_table="wk1995",#XXX pva: 1) neutrons? 2) move up as a parameter.
      xray_structures=[xs],
      bulk_solvent_correction=True,
      anisotropic_scaling=True,
      r_free_flags=fo_.array(data=flex.bool(fo_.size(), False)))
    fmodel_refined.update(abcd=hl_)

    master_phil = mmtbx.maps.map_and_map_coeff_master_params()
    map_params = master_phil.fetch(iotbx.phil.parse("""\
map_coefficients {
  map_type = 2mFo-DFc
  isotropize = True
}
""")).extract().map_coefficients[0]
    model_map_coeffs = mmtbx.maps.map_coefficients_from_fmodel(
      fmodel=fmodel_refined, params=map_params)
    model_map = model_map_coeffs.fft_map(
      resolution_factor=params.grid_resolution_factor).real_map_unpadded()

  import time

  t0 = time.time()
  dm = density_modify(
    params,
    fo,
    hl_coeffs,
    ncs_object=ncs_object,
    map_coeffs=map_coeffs,
    model_map_coeffs=model_map_coeffs,
    log=log,
    as_gui_program=as_gui_program)
  time_dm = time.time()-t0
  print >> log, "Time taken for density modification: %.2fs" %time_dm
  # run cns
  if 0:
    from cctbx.development import cns_density_modification
    cns_result = cns_density_modification.run(params, fo, hl_coeffs)
    print cns_result.modified_map.all()
    print dm.map.all()
    dm_map_coeffs = dm.map_coeffs_in_original_setting
    from cctbx import maptbx, miller
    crystal_gridding = maptbx.crystal_gridding(
      dm_map_coeffs.unit_cell(),
      space_group_info=dm_map_coeffs.space_group().info(),
      pre_determined_n_real=cns_result.modified_map.all())
    dm_map = miller.fft_map(crystal_gridding, dm_map_coeffs).apply_sigma_scaling()
    corr = flex.linear_correlation(cns_result.modified_map.as_1d(), dm_map.real_map_unpadded().as_1d())
    print "CNS dm/mmtbx dm correlation:"
    corr.show_summary()
    if dm.model_map_coeffs is not None:
      model_map = miller.fft_map(
        crystal_gridding,
        dm.miller_array_in_original_setting(dm.model_map_coeffs)).apply_sigma_scaling()
      corr = flex.linear_correlation(cns_result.modified_map.as_1d(), model_map.real_map_unpadded().as_1d())
      print "CNS dm/model correlation:"
      corr.show_summary()

  if output_plots:
    plots_to_make = (
      "fom", "skewness",
      "r1_factor", "r1_factor_fom", "mean_solvent_density", "mean_protein_density",
      "f000_over_v", "k_flip", "rms_solvent_density", "rms_protein_density",
      "standard_deviation_local_rms", "mean_delta_phi", "mean_delta_phi_initial",
      )
    from matplotlib.backends.backend_pdf import PdfPages
    from libtbx import pyplot

    stats = dm.get_stats()
    pdf = PdfPages("density_modification.pdf")

    if len(dm.correlation_coeffs) > 1:
      if 0:
        start_coeffs, model_coeffs = dm.map_coeffs_start.common_sets(model_map_coeffs)
        model_phases = model_coeffs.phases(deg=True).data()
        exptl_phases = nearest_phase(
          model_phases, start_coeffs.phases(deg=True).data(), deg=True)
        corr = flex.linear_correlation(exptl_phases, model_phases)
        corr.show_summary()
        fig = pyplot.figure()
        ax = fig.add_subplot(1,1,1)
        ax.set_title("phases start")
        ax.set_xlabel("Experimental phases")
        ax.set_ylabel("Phases from refined model")
        ax.scatter(exptl_phases,
                   model_phases,
                   marker="x", s=10)
        pdf.savefig(fig)
        #
        dm_coeffs, model_coeffs = dm.map_coeffs.common_sets(model_map_coeffs)
        model_phases = model_coeffs.phases(deg=True).data()
        dm_phases = nearest_phase(
          model_phases, dm_coeffs.phases(deg=True).data(), deg=True)
        corr = flex.linear_correlation(dm_phases, model_phases)
        corr.show_summary()
        fig = pyplot.figure()
        ax = fig.add_subplot(1,1,1)
        ax.set_title("phases dm")
        ax.set_xlabel("Phases from density modification")
        ax.set_ylabel("Phases from refined model")
        ax.scatter(dm_phases,
                   model_phases,
                   marker="x", s=10)
        pdf.savefig(fig)
      #
      data = dm.correlation_coeffs
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title("correlation coefficient")
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)
      #
      data = dm.mean_phase_errors
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title("Mean effective phase errors")
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)

    for plot in plots_to_make:
      data = [getattr(stats.get_cycle_stats(i), plot) for i in range(1, dm.i_cycle+2)]
      fig = pyplot.figure()
      ax = fig.add_subplot(1,1,1)
      ax.set_title(plot.replace("_", " "))
      ax.plot(range(1, dm.i_cycle+2), data)
      pdf.savefig(fig)

    data = [stats.get_cycle_stats(i).rms_solvent_density/
            stats.get_cycle_stats(i).rms_protein_density
            for i in range(1, dm.i_cycle+2)]
    fig = pyplot.figure()
    ax = fig.add_subplot(1,1,1)
    ax.set_title("RMS solvent/protein density ratio")
    ax.plot(range(1, dm.i_cycle+2), data)
    pdf.savefig(fig)

    pdf.close()

  dm_map_coeffs = dm.map_coeffs_in_original_setting
  dm_hl_coeffs = dm.hl_coeffs_in_original_setting

  # output map if requested
  map_params = params.output.map
  if map_params.file_name is not None:
    fft_map = dm_map_coeffs.fft_map(resolution_factor=params.grid_resolution_factor)
    if map_params.scale == "sigma":
      fft_map.apply_sigma_scaling()
    else:
      fft_map.apply_volume_scaling()
    gridding_first = gridding_last = None
    title_lines = []
    if map_params.format == "xplor":
      fft_map.as_xplor_map(
        file_name      = map_params.file_name,
        title_lines    = title_lines,
        gridding_first = gridding_first,
        gridding_last  = gridding_last)
    else :
      fft_map.as_ccp4_map(
        file_name      = map_params.file_name,
        gridding_first = gridding_first,
        gridding_last  = gridding_last,
        labels=title_lines)

  # output map coefficients if requested
  mtz_params = params.output.mtz

  # Decide if we are going to actually write the mtz
  if mtz_params.file_name is not None:
    orig_fom,final_fom=dm.start_and_end_fom()
    if mtz_params.skip_output_if_worse and final_fom < orig_fom:
      ok_to_write_mtz=False
      print "Not writing out mtz. Final FOM (%7.3f) worse than start (%7.3f)" %(
        final_fom,orig_fom)
    else:  # usual
      ok_to_write_mtz=True
  else:
      ok_to_write_mtz=True

  if mtz_params.file_name is not None and ok_to_write_mtz:
    label_decorator=iotbx.mtz.ccp4_label_decorator()
    fo = dm.miller_array_in_original_setting(dm.f_obs_complete).common_set(dm_map_coeffs)
    mtz_dataset = fo.as_mtz_dataset(
      column_root_label="F",
      label_decorator=label_decorator)
    mtz_dataset.add_miller_array(
      dm_map_coeffs,
      column_root_label="FWT",
      label_decorator=label_decorator)
    phase_source = dm.miller_array_in_original_setting(dm.phase_source).common_set(dm_map_coeffs)
    mtz_dataset.add_miller_array(
      phase_source.array(data=flex.abs(phase_source.data())),
      column_root_label="FOM",
      column_types='W',
      label_decorator=label_decorator)
    mtz_dataset.add_miller_array(
      phase_source.array(data=phase_source.phases(deg=True).data()),
      column_root_label="PHIB",
      column_types='P',
      label_decorator=None)
    if mtz_params.output_hendrickson_lattman_coefficients:
      mtz_dataset.add_miller_array(
        dm_hl_coeffs,
        column_root_label="HL",
        label_decorator=label_decorator)
    mtz_dataset.mtz_object().write(mtz_params.file_name)

  return result(
    map_file=map_params.file_name,
    mtz_file=mtz_params.file_name,
    stats=dm.get_stats())
示例#7
0
 def plot_samples_ix(O, stage, ix):
   p = O.params.plot_samples
   i_sc, x_type = O.x_info[ix]
   def f_calc_without_moving_scatterer():
     sc = O.xray_structure.scatterers()[i_sc]
     occ = sc.occupancy
     try:
       sc.occupancy = 0
       result = O.get_f_calc().data()
     finally:
       sc.occupancy = occ
     return result
   f_calc_fixed = f_calc_without_moving_scatterer()
   xs = O.xray_structure.select(flex.size_t([i_sc]))
   def f_calc_moving():
     return O.f_obs.structure_factors_from_scatterers(
       xray_structure=xs,
       algorithm="direct",
       cos_sin_table=False).f_calc().data()
   sc = xs.scatterers()[0]
   ss = xs.site_symmetry_table().get(0)
   def build_info_str():
     return "%s|%03d|%03d|%s|%s|occ=%.2f|%s|%s" % (
       O.plot_samples_id,
       ix,
       i_sc,
       sc.label,
       sc.scattering_type,
       sc.occupancy,
       ss.special_op_simplified(),
       x_type)
   info_str = build_info_str()
   print "plot_samples:", info_str
   sys.stdout.flush()
   ys = []
   def ys_append():
     tg = O.__get_tg(
       f_cbs=O.get_f_cbs(
         f_calc=O.f_obs.customized_copy(data=f_calc_moving()+f_calc_fixed)),
       derivatives_depth=0,
       target=O.ps_target,
       weights=O.ps_weights)
     y = tg.target_work()
     ys.append(y)
     return y
   xyv = []
   if (x_type == "u"):
     assert p.u_min < p.u_max
     assert p.u_steps > 0
     for i_step in xrange(p.u_steps+1):
       u = p.u_min + i_step / p.u_steps * (p.u_max - p.u_min)
       sc.u_iso = u
       y = ys_append()
       xyv.append((u,y,sc.u_iso))
   else:
     assert p.x_radius > 0
     assert p.x_steps > 0
     ss_constr = ss.site_constraints()
     ss_np = ss_constr.n_independent_params()
     ss_ip = "xyz".find(x_type)
     assert ss_ip >= 0
     ixx = ix - ss_ip
     indep = list(O.x[ixx:ixx+ss_np])
     i_inp = indep[ss_ip]
     from libtbx.test_utils import approx_equal
     assert approx_equal(
       ss_constr.all_params(independent_params=indep), sc.site)
     site_inp = sc.site
     indep[ss_ip] = i_inp + 1
     sc.site = ss_constr.all_params(independent_params=indep)
     dist = xs.unit_cell().distance(sc.site, site_inp)
     assert dist != 0
     x_scale = p.x_radius / dist
     for i_step in xrange(-p.x_steps//2, p.x_steps//2+1):
       x = i_step / p.x_steps * 2 * x_scale
       indep[ss_ip] = i_inp + x
       sc.site = ss_constr.all_params(independent_params=indep)
       y = ys_append()
       dist = xs.unit_cell().distance(sc.site, site_inp)
       if (i_step < 0): dist *= -1
       xyv.append((dist,y,indep[ss_ip]))
   #
   base_name_plot_files = "%s_%03d_%s" % (p.file_prefix, ix, stage)
   def write_xyv():
     if (p.file_prefix is None): return
     f = open(base_name_plot_files+".xy", "w")
     print >> f, "# %s" % info_str
     print >> f, "# %s" % str(xs.unit_cell())
     print >> f, "# %s" % str(xs.space_group_info().symbol_and_number())
     for x,y,v in xyv:
       print >> f, x, y, v
   write_xyv()
   if (len(p.pyplot) != 0):
     from libtbx import pyplot
     fig = pyplot.figure()
     ax = fig.add_subplot(1, 1, 1)
     x,y,v = zip(*xyv)
     ax.plot(x,y, "r-")
     x = O.x[ix]
     ax.plot([x, x], [min(ys), max(ys)], "k--")
     if (O.x_reference is not None):
       x = O.x_reference[ix]
       ax.plot([x, x], [min(ys), max(ys)], "r--")
     ax.set_title(info_str, fontsize=12)
     ax.axis([xyv[0][0], xyv[-1][0], None, None])
     def write_pdf():
       if (p.file_prefix is None): return
       fig.savefig(base_name_plot_files+".pdf", bbox_inches="tight")
     if ("pdf" in p.pyplot):
       write_pdf()
     if ("gui" in p.pyplot):
       pyplot.show()
示例#8
0
 def plot_samples_ix(O, stage, ix):
   p = O.params.plot_samples
   i_sc, x_type = O.x_info[ix]
   def f_calc_without_moving_scatterer():
     sc = O.xray_structure.scatterers()[i_sc]
     occ = sc.occupancy
     try:
       sc.occupancy = 0
       result = O.get_f_calc().data()
     finally:
       sc.occupancy = occ
     return result
   f_calc_fixed = f_calc_without_moving_scatterer()
   xs = O.xray_structure.select(flex.size_t([i_sc]))
   def f_calc_moving():
     return O.f_obs.structure_factors_from_scatterers(
       xray_structure=xs,
       algorithm="direct",
       cos_sin_table=False).f_calc().data()
   sc = xs.scatterers()[0]
   ss = xs.site_symmetry_table().get(0)
   def build_info_str():
     return "%s|%03d|%03d|%s|%s|occ=%.2f|%s|%s" % (
       O.plot_samples_id,
       ix,
       i_sc,
       sc.label,
       sc.scattering_type,
       sc.occupancy,
       ss.special_op_simplified(),
       x_type)
   info_str = build_info_str()
   print "plot_samples:", info_str
   sys.stdout.flush()
   ys = []
   def ys_append():
     tg = O.__get_tg(
       f_cbs=O.get_f_cbs(
         f_calc=O.f_obs.customized_copy(data=f_calc_moving()+f_calc_fixed)),
       derivatives_depth=0,
       target=O.ps_target,
       weights=O.ps_weights)
     y = tg.target_work()
     ys.append(y)
     return y
   xyv = []
   if (x_type == "u"):
     assert p.u_min < p.u_max
     assert p.u_steps > 0
     for i_step in xrange(p.u_steps+1):
       u = p.u_min + i_step / p.u_steps * (p.u_max - p.u_min)
       sc.u_iso = u
       y = ys_append()
       xyv.append((u,y,sc.u_iso))
   else:
     assert p.x_radius > 0
     assert p.x_steps > 0
     ss_constr = ss.site_constraints()
     ss_np = ss_constr.n_independent_params()
     ss_ip = "xyz".find(x_type)
     assert ss_ip >= 0
     ixx = ix - ss_ip
     indep = list(O.x[ixx:ixx+ss_np])
     i_inp = indep[ss_ip]
     from libtbx.test_utils import approx_equal
     assert approx_equal(
       ss_constr.all_params(independent_params=indep), sc.site)
     site_inp = sc.site
     indep[ss_ip] = i_inp + 1
     sc.site = ss_constr.all_params(independent_params=indep)
     dist = xs.unit_cell().distance(sc.site, site_inp)
     assert dist != 0
     x_scale = p.x_radius / dist
     for i_step in xrange(-p.x_steps//2, p.x_steps//2+1):
       x = i_step / p.x_steps * 2 * x_scale
       indep[ss_ip] = i_inp + x
       sc.site = ss_constr.all_params(independent_params=indep)
       y = ys_append()
       dist = xs.unit_cell().distance(sc.site, site_inp)
       if (i_step < 0): dist *= -1
       xyv.append((dist,y,indep[ss_ip]))
   #
   base_name_plot_files = "%s_%03d_%s" % (p.file_prefix, ix, stage)
   def write_xyv():
     if (p.file_prefix is None): return
     f = open(base_name_plot_files+".xy", "w")
     print >> f, "# %s" % info_str
     print >> f, "# %s" % str(xs.unit_cell())
     print >> f, "# %s" % str(xs.space_group_info().symbol_and_number())
     for x,y,v in xyv:
       print >> f, x, y, v
   write_xyv()
   if (len(p.pyplot) != 0):
     from libtbx import pyplot
     fig = pyplot.figure()
     ax = fig.add_subplot(1, 1, 1)
     x,y,v = zip(*xyv)
     ax.plot(x,y, "r-")
     x = O.x[ix]
     ax.plot([x, x], [min(ys), max(ys)], "k--")
     if (O.x_reference is not None):
       x = O.x_reference[ix]
       ax.plot([x, x], [min(ys), max(ys)], "r--")
     ax.set_title(info_str, fontsize=12)
     ax.axis([xyv[0][0], xyv[-1][0], None, None])
     def write_pdf():
       if (p.file_prefix is None): return
       fig.savefig(base_name_plot_files+".pdf", bbox_inches="tight")
     if ("pdf" in p.pyplot):
       write_pdf()
     if ("gui" in p.pyplot):
       pyplot.show()