示例#1
0
def run(args):
    if not libtbx.env.has_module("dials_regression"):
        print "Skipping exercise_index_3D_FFT_simple: dials_regression not present"
        return

    exercises = (exercise_1, exercise_2, exercise_3, exercise_4, exercise_5,
                 exercise_6, exercise_7, exercise_8, exercise_9, exercise_10,
                 exercise_11, exercise_12, exercise_13, exercise_14,
                 exercise_15, exercise_16, exercise_17)
    if len(args):
        args = [int(arg) for arg in args]
        for arg in args:
            assert arg > 0
        exercises = [exercises[arg - 1] for arg in args]

    from libtbx import easy_mp

    nproc = easy_mp.get_processes(libtbx.Auto)
    nproc = min(nproc, len(exercises))

    def run_parallel(args):
        assert len(args) == 1
        exercise = args[0]
        exercise()

    easy_mp.parallel_map(func=run_parallel,
                         iterable=[(e, ) for e in exercises],
                         processes=nproc)
示例#2
0
def run(args):
  if not libtbx.env.has_module("dials_regression"):
    print "Skipping exercise_index_3D_FFT_simple: dials_regression not present"
    return

  exercises = (exercise_1, exercise_2, exercise_3, exercise_4, exercise_5,
               exercise_6, exercise_7, exercise_8, exercise_9, exercise_10,
               exercise_11, exercise_12, exercise_13, exercise_14, exercise_15,
               exercise_16)
  if len(args):
    args = [int(arg) for arg in args]
    for arg in args: assert arg > 0
    exercises = [exercises[arg-1] for arg in args]

  from libtbx import easy_mp

  nproc = easy_mp.get_processes(libtbx.Auto)
  nproc = min(nproc, len(exercises))

  def run_parallel(args):
    assert len(args) == 1
    exercise = args[0]
    exercise()

  easy_mp.parallel_map(
    func=run_parallel,
    iterable=[(e,) for e in exercises],
    processes=nproc)
示例#3
0
 def __init__(self,
              residues,
              pdb_hierarchy,
              fmodel,
              restraints_manager,
              params,
              nproc=Auto,
              verbose=False,
              debug=None,
              log=sys.stdout):
     adopt_init_args(self, locals())
     nproc = easy_mp.get_processes(nproc)
     print("", file=log)
     if (nproc == 1):
         print("  running all residues serially", file=log)
         self.results = []
         for i_res in range(len(residues)):
             self.results.append(self.__call__(i_res, log=log))
     else:
         print("  will use %d processes" % nproc, file=log)
         self.results = easy_mp.pool_map(fixed_func=self,
                                         iterable=range(len(residues)),
                                         processes=nproc)
示例#4
0
 def __init__ (self,
     residues,
     pdb_hierarchy,
     fmodel,
     restraints_manager,
     params,
     nproc=Auto,
     verbose=False,
     debug=None,
     log=sys.stdout) :
   adopt_init_args(self, locals())
   nproc = easy_mp.get_processes(nproc)
   print >> log, ""
   if (nproc == 1) :
     print >> log, "  running all residues serially"
     self.results = []
     for i_res in range(len(residues)) :
       self.results.append(self.__call__(i_res, log=log))
   else :
     print >> log, "  will use %d processes" % nproc
     self.results = easy_mp.pool_map(
       fixed_func=self,
       iterable=range(len(residues)),
       processes=nproc)
示例#5
0
def run(args):
    sweep_directories = []
    templates = []
    n_strong_spots = flex.int()
    n_strong_spots_dmin_4 = flex.int()
    d_strong_spots_99th_percentile = flex.double()
    d_strong_spots_95th_percentile = flex.double()
    d_strong_spots_50th_percentile = flex.double()
    n_unindexed_spots = flex.int()
    n_indexed_lattices = flex.int()
    n_integrated_lattices = flex.int()
    sweep_dir_cryst = flex.std_string()

    orig_dir = os.path.abspath(os.curdir)

    rmsds = flex.vec3_double()
    cell_params = flex.sym_mat3_double()
    n_indexed = flex.double()
    d_min_indexed = flex.double()
    rmsds = flex.vec3_double()

    nproc = easy_mp.get_processes(libtbx.Auto)
    # nproc = 1
    results = easy_mp.parallel_map(
        func=run_once,
        iterable=args,
        processes=nproc,
        method="multiprocessing",
        preserve_order=True,
        asynchronous=True,
        preserve_exception_message=True,
    )

    for result in results:
        if result is None:
            continue
        sweep_directories.append(result.sweep_dir)
        templates.append(result.template)
        n_strong_spots.append(result.n_strong_spots)
        n_strong_spots_dmin_4.append(result.n_strong_spots_dmin_4)
        n_unindexed_spots.append(result.n_unindexed_spots)
        n_indexed_lattices.append(result.n_indexed_lattices)
        n_integrated_lattices.append(result.n_integrated_lattices)
        d_strong_spots_50th_percentile.append(
            result.d_strong_spots_50th_percentile)
        d_strong_spots_95th_percentile.append(
            result.d_strong_spots_95th_percentile)
        d_strong_spots_99th_percentile.append(
            result.d_strong_spots_99th_percentile)
        cell_params.extend(result.cell_params)
        n_indexed.extend(result.n_indexed)
        d_min_indexed.extend(result.d_min_indexed)
        rmsds.extend(result.rmsds)
        sweep_dir_cryst.extend(result.sweep_dir_cryst)

    table_data = [(
        "sweep_dir",
        "template",
        "#strong_spots",
        "#unindexed_spots",
        "#lattices",
        "d_spacing_50th_percentile",
        "d_spacing_95th_percentile",
        "d_spacing_99th_percentile",
    )]
    for i in range(len(sweep_directories)):
        table_data.append((
            sweep_directories[i],
            templates[i],
            str(n_strong_spots[i]),
            str(n_unindexed_spots[i]),
            str(n_indexed_lattices[i]),
            str(d_strong_spots_50th_percentile[i]),
            str(d_strong_spots_95th_percentile[i]),
            str(d_strong_spots_99th_percentile[i]),
        ))

    with open("results.txt", "wb") as f:
        print(table_utils.format(table_data, has_header=True, justify="right"),
              file=f)

    table_data = [(
        "sweep_dir",
        "cell_a",
        "cell_b",
        "cell_c",
        "alpha",
        "beta",
        "gamma",
        "#indexed_reflections",
        "d_min_indexed",
        "rmsd_x",
        "rmsd_y",
        "rmsd_phi",
    )]
    for i in range(len(cell_params)):
        table_data.append((
            sweep_dir_cryst[i],
            str(cell_params[i][0]),
            str(cell_params[i][1]),
            str(cell_params[i][2]),
            str(cell_params[i][3]),
            str(cell_params[i][4]),
            str(cell_params[i][5]),
            str(n_indexed[i]),
            str(d_min_indexed[i]),
            str(rmsds[i][0]),
            str(rmsds[i][1]),
            str(rmsds[i][2]),
        ))

    with open("results_indexed.txt", "wb") as f:
        print(table_utils.format(table_data, has_header=True, justify="right"),
              file=f)

    cell_a = flex.double([params[0] for params in cell_params])
    cell_b = flex.double([params[1] for params in cell_params])
    cell_c = flex.double([params[2] for params in cell_params])
    cell_alpha = flex.double([params[3] for params in cell_params])
    cell_beta = flex.double([params[4] for params in cell_params])
    cell_gamma = flex.double([params[5] for params in cell_params])

    from matplotlib import pyplot
    from matplotlib.backends.backend_pdf import PdfPages

    pyplot.rc("font", family="serif")
    pyplot.rc("font", serif="Times New Roman")

    red, blue = "#B2182B", "#2166AC"
    hist = flex.histogram(n_strong_spots_dmin_4.as_double(), n_slots=20)
    hist.show()
    fig = pyplot.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.bar(
        hist.slot_centers(),
        hist.slots(),
        width=0.75 * hist.slot_width(),
        color=blue,
        edgecolor=blue,
    )
    ax.set_xlabel("Spot count")
    ax.set_ylabel("Frequency")
    pdf = PdfPages("spot_count_histogram.pdf")
    pdf.savefig(fig)
    pdf.close()
    # pyplot.show()

    hist = flex.histogram(n_indexed_lattices.as_double(),
                          n_slots=flex.max(n_indexed_lattices))
    hist.show()
    fig = pyplot.figure()
    ax = fig.add_subplot(1, 1, 1)
    ax.bar(
        range(int(hist.data_max())),
        hist.slots(),
        width=0.75 * hist.slot_width(),
        align="center",
        color=blue,
        edgecolor=blue,
    )
    ax.set_xlim(-0.5, hist.data_max() - 0.5)
    ax.set_xticks(range(0, int(hist.data_max())))
    ax.set_xlabel("Number of indexed lattices")
    ax.set_ylabel("Frequency")
    pdf = PdfPages("n_indexed_lattices_histogram.pdf")
    pdf.savefig(fig)
    pdf.close()
    # pyplot.show()

    if flex.max(n_integrated_lattices) > 0:
        hist = flex.histogram(n_integrated_lattices.as_double(),
                              n_slots=flex.max(n_integrated_lattices))
        hist.show()
        fig = pyplot.figure()
        ax = fig.add_subplot(1, 1, 1)
        ax.bar(
            range(int(hist.data_max())),
            hist.slots(),
            width=0.75 * hist.slot_width(),
            align="center",
            color=blue,
            edgecolor=blue,
        )
        ax.set_xlim(-0.5, hist.data_max() - 0.5)
        ax.set_xticks(range(0, int(hist.data_max())))
        ax.set_xlabel("Number of integrated lattices")
        ax.set_ylabel("Frequency")
        pdf = PdfPages("n_integrated_lattices_histogram.pdf")
        pdf.savefig(fig)
        pdf.close()
        # pyplot.show()

    fig, axes = pyplot.subplots(nrows=2, ncols=3, squeeze=False)
    for i, cell_param in enumerate(
        (cell_a, cell_b, cell_c, cell_alpha, cell_beta, cell_gamma)):
        ax = axes.flat[i]
        flex.min_max_mean_double(cell_param).show()
        print(flex.median(cell_param))
        hist = flex.histogram(cell_param, n_slots=20)
        hist.show()
        ax.bar(
            hist.slot_centers(),
            hist.slots(),
            width=0.75 * hist.slot_width(),
            color=blue,
            edgecolor=blue,
        )
        ax.set_xlabel("Cell parameter")
        ax.set_ylabel("Frequency")
    pyplot.tight_layout()
    pdf = PdfPages("cell_parameters.pdf")
    pdf.savefig(fig)
    pdf.close()
示例#6
0
    def __init__(self,
                 pixel_histograms,
                 output_dirname=".",
                 gain_map_path=None,
                 gain_map=None,
                 method="photon_counting",
                 estimated_gain=30,
                 nproc=None,
                 photon_threshold=2 / 3,
                 roi=None,
                 run=None):
        assert method in ("sum_adu", "photon_counting")
        self.sum_img = flex.double(flex.grid(
            370, 391), 0)  # XXX define the image size some other way?
        gain_img = flex.double(self.sum_img.accessor(), 0)

        assert [gain_map, gain_map_path].count(None) > 0
        if gain_map_path is not None:
            d = easy_pickle.load(gain_map_path)
            gain_map = d["DATA"]

        two_photon_threshold = photon_threshold + 1

        mask = flex.int(self.sum_img.accessor(), 0)

        start_row = 370
        end_row = 0
        print len(pixel_histograms.histograms)

        pixels = list(pixel_histograms.pixels())
        n_pixels = len(pixels)
        if roi is not None:
            for k, (i, j) in enumerate(reversed(pixels)):
                if (i < roi[2] or i > roi[3] or j < roi[0] or j > roi[1]):
                    del pixels[n_pixels - k - 1]

        if gain_map is None:
            fixed_func = pixel_histograms.fit_one_histogram
        else:

            def fixed_func(pixel):
                return pixel_histograms.fit_one_histogram(pixel, n_gaussians=1)

        results = None
        if nproc is None: nproc = easy_mp.Auto
        nproc = easy_mp.get_processes(nproc)
        print "nproc: ", nproc

        stdout_and_results = easy_mp.pool_map(
            processes=nproc,
            fixed_func=fixed_func,
            args=pixels,
            func_wrapper="buffer_stdout_stderr")
        results = [r for so, r in stdout_and_results]

        gains = flex.double()

        for i, pixel in enumerate(pixels):
            start_row = min(start_row, pixel[0])
            end_row = max(end_row, pixel[0])
            n_photons = 0
            if results is None:
                # i.e. not multiprocessing
                try:
                    gaussians = pixel_histograms.fit_one_histogram(pixel)
                except RuntimeError, e:
                    print "Error fitting pixel %s" % str(pixel)
                    print str(e)
                    mask[pixel] = 1
                    continue
            else:
                gaussians = results[i]
            hist = pixel_histograms.histograms[pixel]
            if gaussians is None:
                # Presumably the peak fitting failed in some way
                print "Skipping pixel %s" % str(pixel)
                continue
            zero_peak_diff = gaussians[0].params[1]
            if gain_map is None:
                try:
                    view_pixel_histograms.check_pixel_histogram_fit(
                        hist, gaussians)
                except view_pixel_histograms.PixelFitError, e:
                    print "PixelFitError:", str(pixel), str(e)
                    mask[pixel] = 1
                    continue
                gain = gaussians[1].params[1] - gaussians[0].params[1]
                gain_img[pixel] = gain
                gain_ratio = gain / estimated_gain
示例#7
0
def run(args):
  sweep_directories = []
  templates = []
  n_strong_spots = flex.int()
  n_strong_spots_dmin_4 = flex.int()
  d_strong_spots_99th_percentile = flex.double()
  d_strong_spots_95th_percentile = flex.double()
  d_strong_spots_50th_percentile = flex.double()
  n_unindexed_spots = flex.int()
  n_indexed_lattices = flex.int()
  n_integrated_lattices = flex.int()
  sweep_dir_cryst = flex.std_string()

  orig_dir = os.path.abspath(os.curdir)

  rmsds = flex.vec3_double()
  cell_params = flex.sym_mat3_double()
  n_indexed = flex.double()
  d_min_indexed = flex.double()
  rmsds = flex.vec3_double()

  nproc = easy_mp.get_processes(libtbx.Auto)
  #nproc = 1
  results = easy_mp.parallel_map(
    func=run_once,
    iterable=args,
    processes=nproc,
    method="multiprocessing",
    preserve_order=True,
    asynchronous=True,
    preserve_exception_message=True,
  )

  for result in results:
    if result is None: continue
    sweep_directories.append(result.sweep_dir)
    templates.append(result.template)
    n_strong_spots.append(result.n_strong_spots)
    n_strong_spots_dmin_4.append(result.n_strong_spots_dmin_4)
    n_unindexed_spots.append(result.n_unindexed_spots)
    n_indexed_lattices.append(result.n_indexed_lattices)
    n_integrated_lattices.append(result.n_integrated_lattices)
    d_strong_spots_50th_percentile.append(result.d_strong_spots_50th_percentile)
    d_strong_spots_95th_percentile.append(result.d_strong_spots_95th_percentile)
    d_strong_spots_99th_percentile.append(result.d_strong_spots_99th_percentile)
    cell_params.extend(result.cell_params)
    n_indexed.extend(result.n_indexed)
    d_min_indexed.extend(result.d_min_indexed)
    rmsds.extend(result.rmsds)
    sweep_dir_cryst.extend(result.sweep_dir_cryst)

  table_data = [('sweep_dir', 'template', '#strong_spots', '#unindexed_spots', '#lattices',
                 'd_spacing_50th_percentile', 'd_spacing_95th_percentile',
                 'd_spacing_99th_percentile',)]
  for i in range(len(sweep_directories)):
    table_data.append((sweep_directories[i],
                       templates[i],
                       str(n_strong_spots[i]),
                       str(n_unindexed_spots[i]),
                       str(n_indexed_lattices[i]),
                       str(d_strong_spots_50th_percentile[i]),
                       str(d_strong_spots_95th_percentile[i]),
                       str(d_strong_spots_99th_percentile[i]),
                       ))

  with open('results.txt', 'wb') as f:
    print >> f, table_utils.format(
      table_data, has_header=True, justify='right')

  table_data = [('sweep_dir', 'cell_a', 'cell_b', 'cell_c', 'alpha', 'beta', 'gamma',
                 '#indexed_reflections', 'd_min_indexed',
                 'rmsd_x', 'rmsd_y', 'rmsd_phi')]
  for i in range(len(cell_params)):
    table_data.append((sweep_dir_cryst[i],
                       str(cell_params[i][0]),
                       str(cell_params[i][1]),
                       str(cell_params[i][2]),
                       str(cell_params[i][3]),
                       str(cell_params[i][4]),
                       str(cell_params[i][5]),
                       str(n_indexed[i]),
                       str(d_min_indexed[i]),
                       str(rmsds[i][0]),
                       str(rmsds[i][1]),
                       str(rmsds[i][2]),
                       ))

  with open('results_indexed.txt', 'wb') as f:
    print >> f, table_utils.format(
      table_data, has_header=True, justify='right')

  cell_a = flex.double([params[0] for params in cell_params])
  cell_b = flex.double([params[1] for params in cell_params])
  cell_c = flex.double([params[2] for params in cell_params])
  cell_alpha = flex.double([params[3] for params in cell_params])
  cell_beta = flex.double([params[4] for params in cell_params])
  cell_gamma = flex.double([params[5] for params in cell_params])

  from matplotlib import pyplot
  from matplotlib.backends.backend_pdf import PdfPages

  pyplot.rc('font', family='serif')
  pyplot.rc('font', serif='Times New Roman')

  red, blue = '#B2182B', '#2166AC'
  hist = flex.histogram(n_strong_spots_dmin_4.as_double(), n_slots=20)
  hist.show()
  fig = pyplot.figure()
  ax = fig.add_subplot(1,1,1)
  ax.bar(hist.slot_centers(), hist.slots(), width=0.75*hist.slot_width(),
         color=blue, edgecolor=blue)
  ax.set_xlabel('Spot count')
  ax.set_ylabel('Frequency')
  pdf = PdfPages("spot_count_histogram.pdf")
  pdf.savefig(fig)
  pdf.close()
  #pyplot.show()

  hist = flex.histogram(n_indexed_lattices.as_double(),
                        n_slots=flex.max(n_indexed_lattices))
  hist.show()
  fig = pyplot.figure()
  ax = fig.add_subplot(1,1,1)
  ax.bar(range(int(hist.data_max())), hist.slots(),
         width=0.75*hist.slot_width(), align='center',
         color=blue, edgecolor=blue)
  ax.set_xlim(-0.5, hist.data_max()-0.5)
  ax.set_xticks(range(0,int(hist.data_max())))
  ax.set_xlabel('Number of indexed lattices')
  ax.set_ylabel('Frequency')
  pdf = PdfPages("n_indexed_lattices_histogram.pdf")
  pdf.savefig(fig)
  pdf.close()
  #pyplot.show()

  if flex.max(n_integrated_lattices) > 0:
    hist = flex.histogram(n_integrated_lattices.as_double(),
                          n_slots=flex.max(n_integrated_lattices))
    hist.show()
    fig = pyplot.figure()
    ax = fig.add_subplot(1,1,1)
    ax.bar(range(int(hist.data_max())), hist.slots(),
           width=0.75*hist.slot_width(),
           align='center', color=blue, edgecolor=blue)
    ax.set_xlim(-0.5, hist.data_max()-0.5)
    ax.set_xticks(range(0,int(hist.data_max())))
    ax.set_xlabel('Number of integrated lattices')
    ax.set_ylabel('Frequency')
    pdf = PdfPages("n_integrated_lattices_histogram.pdf")
    pdf.savefig(fig)
    pdf.close()
    #pyplot.show()

  fig, axes = pyplot.subplots(nrows=2, ncols=3, squeeze=False)
  for i, cell_param in enumerate(
    (cell_a, cell_b, cell_c, cell_alpha, cell_beta, cell_gamma)):
    ax = axes.flat[i]
    flex.min_max_mean_double(cell_param).show()
    print flex.median(cell_param)
    hist = flex.histogram(cell_param, n_slots=20)
    hist.show()
    ax.bar(hist.slot_centers(), hist.slots(), width=0.75*hist.slot_width(),
           color=blue, edgecolor=blue)
    ax.set_xlabel('Cell parameter')
    ax.set_ylabel('Frequency')
  pyplot.tight_layout()
  pdf = PdfPages("cell_parameters.pdf")
  pdf.savefig(fig)
  pdf.close()
示例#8
0
    def __init__(self,
                 pixel_histograms,
                 output_dirname=".",
                 gain_map_path=None,
                 gain_map=None,
                 method="photon_counting",
                 estimated_gain=30,
                 nproc=None,
                 photon_threshold=2 / 3,
                 roi=None,
                 run=None):
        assert method in ("sum_adu", "photon_counting")
        self.sum_img = flex.double(flex.grid(
            370, 391), 0)  # XXX define the image size some other way?
        gain_img = flex.double(self.sum_img.accessor(), 0)

        assert [gain_map, gain_map_path].count(None) > 0
        if gain_map_path is not None:
            d = easy_pickle.load(gain_map_path)
            gain_map = d["DATA"]

        two_photon_threshold = photon_threshold + 1

        mask = flex.int(self.sum_img.accessor(), 0)

        start_row = 370
        end_row = 0
        print(len(pixel_histograms.histograms))

        pixels = list(pixel_histograms.pixels())
        n_pixels = len(pixels)
        if roi is not None:
            for k, (i, j) in enumerate(reversed(pixels)):
                if (i < roi[2] or i > roi[3] or j < roi[0] or j > roi[1]):
                    del pixels[n_pixels - k - 1]

        if gain_map is None:
            fixed_func = pixel_histograms.fit_one_histogram
        else:

            def fixed_func(pixel):
                return pixel_histograms.fit_one_histogram(pixel, n_gaussians=1)

        results = None
        if nproc is None: nproc = easy_mp.Auto
        nproc = easy_mp.get_processes(nproc)
        print("nproc: ", nproc)

        stdout_and_results = easy_mp.pool_map(
            processes=nproc,
            fixed_func=fixed_func,
            args=pixels,
            func_wrapper="buffer_stdout_stderr")
        results = [r for so, r in stdout_and_results]

        gains = flex.double()

        for i, pixel in enumerate(pixels):
            start_row = min(start_row, pixel[0])
            end_row = max(end_row, pixel[0])
            n_photons = 0
            if results is None:
                # i.e. not multiprocessing
                try:
                    gaussians = pixel_histograms.fit_one_histogram(pixel)
                except RuntimeError as e:
                    print("Error fitting pixel %s" % str(pixel))
                    print(str(e))
                    mask[pixel] = 1
                    continue
            else:
                gaussians = results[i]
            hist = pixel_histograms.histograms[pixel]
            if gaussians is None:
                # Presumably the peak fitting failed in some way
                print("Skipping pixel %s" % str(pixel))
                continue
            zero_peak_diff = gaussians[0].params[1]
            if gain_map is None:
                try:
                    view_pixel_histograms.check_pixel_histogram_fit(
                        hist, gaussians)
                except view_pixel_histograms.PixelFitError as e:
                    print("PixelFitError:", str(pixel), str(e))
                    mask[pixel] = 1
                    continue
                gain = gaussians[1].params[1] - gaussians[0].params[1]
                gain_img[pixel] = gain
                gain_ratio = gain / estimated_gain
            else:
                gain = gain_map[pixel]
                if gain == 0:
                    print("bad gain!!!!!", pixel)
                    continue
                gain = 30 / gain
                gain_ratio = 1 / gain
            gains.append(gain)

            #for g in gaussians:
            #sigma = abs(g.params[2])
            #if sigma < 1 or sigma > 10:
            #print "bad sigma!!!!!", pixel, sigma
            #mask[pixel] = 1
            #continue
            if method == "sum_adu":
                sum_adu = 0
                one_photon_cutoff, two_photon_cutoff = [
                    (threshold * gain + zero_peak_diff)
                    for threshold in (photon_threshold, two_photon_threshold)
                ]
                i_one_photon_cutoff = hist.get_i_slot(one_photon_cutoff)
                slots = hist.slots().as_double()
                slot_centers = hist.slot_centers()
                slots -= gaussians[0](slot_centers)
                for j in range(i_one_photon_cutoff, len(slots)):
                    center = slot_centers[j]
                    sum_adu += slots[j] * (center - zero_peak_diff) * 30 / gain

                self.sum_img[pixel] = sum_adu
            elif method == "photon_counting":
                one_photon_cutoff, two_photon_cutoff = [
                    (threshold * gain + zero_peak_diff)
                    for threshold in (photon_threshold, two_photon_threshold)
                ]
                i_one_photon_cutoff = hist.get_i_slot(one_photon_cutoff)
                i_two_photon_cutoff = hist.get_i_slot(two_photon_cutoff)
                slots = hist.slots()
                for j in range(i_one_photon_cutoff, len(slots)):
                    if j == i_one_photon_cutoff:
                        center = hist.slot_centers()[j]
                        upper = center + 0.5 * hist.slot_width()
                        n_photons += int(
                            round((upper - one_photon_cutoff) /
                                  hist.slot_width() * slots[j]))
                    elif j == i_two_photon_cutoff:
                        center = hist.slot_centers()[j]
                        upper = center + 0.5 * hist.slot_width()
                        n_photons += 2 * int(
                            round((upper - two_photon_cutoff) /
                                  hist.slot_width() * slots[j]))
                    elif j < i_two_photon_cutoff:
                        n_photons += int(round(slots[j]))
                    else:
                        n_photons += 2 * int(round(slots[j]))
                self.sum_img[pixel] = n_photons

        stats = scitbx.math.basic_statistics(gains)
        print("gain statistics:")
        stats.show()

        mask.set_selected(self.sum_img == 0, 1)
        unbound_pixel_mask = xes_finalise.cspad_unbound_pixel_mask()
        mask.set_selected(unbound_pixel_mask > 0, 1)
        bad_pixel_mask = xes_finalise.cspad2x2_bad_pixel_mask_cxi_run7()
        mask.set_selected(bad_pixel_mask > 0, 1)

        for row in range(self.sum_img.all()[0]):
            self.sum_img[row:row + 1, :].count(0)

        spectrum_focus = self.sum_img[start_row:end_row, :]
        mask_focus = mask[start_row:end_row, :]

        spectrum_focus.set_selected(mask_focus > 0, 0)

        xes_finalise.filter_outlying_pixels(spectrum_focus, mask_focus)

        print("Number of rows: %i" % spectrum_focus.all()[0])
        print("Estimated no. photons counted: %i" % flex.sum(spectrum_focus))
        print("Number of images used: %i" %
              flex.sum(pixel_histograms.histograms.values()[0].slots()))

        d = cspad_tbx.dpack(
            address='CxiSc1-0|Cspad2x2-0',
            data=spectrum_focus,
            distance=1,
            ccd_image_saturation=2e8,  # XXX
        )
        if run is not None: runstr = "_%04d" % run
        else: runstr = ""
        cspad_tbx.dwritef(d, output_dirname, 'sum%s_' % runstr)

        if gain_map is None:
            gain_map = flex.double(gain_img.accessor(), 0)
            img_sel = (gain_img > 0).as_1d()
            d = cspad_tbx.dpack(address='CxiSc1-0|Cspad2x2-0',
                                data=gain_img,
                                distance=1)
            cspad_tbx.dwritef(d, output_dirname, 'raw_gain_map_')
            gain_map.as_1d().set_selected(img_sel.iselection(),
                                          1 / gain_img.as_1d().select(img_sel))
            gain_map /= flex.mean(gain_map.as_1d().select(img_sel))
            d = cspad_tbx.dpack(address='CxiSc1-0|Cspad2x2-0',
                                data=gain_map,
                                distance=1)
            cspad_tbx.dwritef(d, output_dirname, 'gain_map_')

        plot_x, plot_y = xes_finalise.output_spectrum(
            spectrum_focus.iround(),
            mask_focus=mask_focus,
            output_dirname=output_dirname,
            run=run)
        self.spectrum = (plot_x, plot_y)
        self.spectrum_focus = spectrum_focus

        xes_finalise.output_matlab_form(
            spectrum_focus, "%s/sum%s.m" % (output_dirname, runstr))
        print(output_dirname)
示例#9
0
  def __init__(self, pixel_histograms, output_dirname=".", gain_map_path=None,
               gain_map=None, method="photon_counting", estimated_gain=30,
               nproc=None, photon_threshold=2/3, roi=None,run=None):
    assert method in ("sum_adu", "photon_counting")
    self.sum_img = flex.double(flex.grid(370,391), 0) # XXX define the image size some other way?
    gain_img = flex.double(self.sum_img.accessor(), 0)

    assert [gain_map, gain_map_path].count(None) > 0
    if gain_map_path is not None:
      d = easy_pickle.load(gain_map_path)
      gain_map = d["DATA"]

    two_photon_threshold = photon_threshold + 1

    mask = flex.int(self.sum_img.accessor(), 0)

    start_row = 370
    end_row = 0
    print len(pixel_histograms.histograms)

    pixels = list(pixel_histograms.pixels())
    n_pixels = len(pixels)
    if roi is not None:
      for k, (i, j) in enumerate(reversed(pixels)):
        if (   i < roi[2]
            or i > roi[3]
            or j < roi[0]
            or j > roi[1]):
          del pixels[n_pixels-k-1]

    if gain_map is None:
      fixed_func = pixel_histograms.fit_one_histogram
    else:
      def fixed_func(pixel):
        return pixel_histograms.fit_one_histogram(pixel, n_gaussians=1)
    results = None
    if nproc is None: nproc = easy_mp.Auto
    nproc = easy_mp.get_processes(nproc)
    print "nproc: ", nproc

    stdout_and_results = easy_mp.pool_map(
      processes=nproc,
      fixed_func=fixed_func,
      args=pixels,
      func_wrapper="buffer_stdout_stderr")
    results = [r for so, r in stdout_and_results]

    gains = flex.double()

    for i, pixel in enumerate(pixels):
      start_row = min(start_row, pixel[0])
      end_row = max(end_row, pixel[0])
      n_photons = 0
      if results is None:
        # i.e. not multiprocessing
        try:
          gaussians = pixel_histograms.fit_one_histogram(pixel)
        except RuntimeError, e:
          print "Error fitting pixel %s" %str(pixel)
          print str(e)
          mask[pixel] = 1
          continue
      else:
        gaussians = results[i]
      hist = pixel_histograms.histograms[pixel]
      if gaussians is None:
        # Presumably the peak fitting failed in some way
        print "Skipping pixel %s" %str(pixel)
        continue
      zero_peak_diff = gaussians[0].params[1]
      if gain_map is None:
        try:
          view_pixel_histograms.check_pixel_histogram_fit(hist, gaussians)
        except view_pixel_histograms.PixelFitError, e:
          print "PixelFitError:", str(pixel), str(e)
          mask[pixel] = 1
          continue
        gain = gaussians[1].params[1] - gaussians[0].params[1]
        gain_img[pixel] = gain
        gain_ratio = gain/estimated_gain