def run(args, source_data=None):
    from xfel import radial_average
    from scitbx.array_family import flex
    from iotbx.detectors.cspad_detector_formats import reverse_timestamp
    from iotbx.detectors.cspad_detector_formats import detector_format_version as detector_format_function
    from spotfinder.applications.xfel import cxi_phil
    from iotbx.detectors.npy import NpyImage
    import os, sys
    from iotbx.detectors.npy import NpyImage

    user_phil = []
    # TODO: replace this stuff with iotbx.phil.process_command_line_with_files
    # as soon as I can safely modify it
    for arg in args:
        if (not "=" in arg):
            try:
                user_phil.append(libtbx.phil.parse("""file_path=%s""" % arg))
            except ValueError as e:
                raise Sorry("Unrecognized argument '%s'" % arg)
        else:
            try:
                user_phil.append(libtbx.phil.parse(arg))
            except RuntimeError as e:
                raise Sorry("Unrecognized argument '%s' (error: %s)" %
                            (arg, str(e)))
    params = master_phil.fetch(sources=user_phil).extract()
    if params.file_path is None or not os.path.isfile(
            params.file_path) and source_data is None:
        master_phil.show()
        raise Usage(
            "file_path must be defined (either file_path=XXX, or the path alone)."
        )
    assert params.handedness is not None
    assert params.n_bins is not None
    assert params.verbose is not None
    assert params.output_bins is not None

    if source_data is None:
        from libtbx import easy_pickle
        source_data = easy_pickle.load(params.file_path)

    if params.output_file is None:
        logger = sys.stdout
    else:
        logger = open(params.output_file, 'w')
        logger.write("%s " % params.output_file)

    if not "DETECTOR_ADDRESS" in source_data:
        # legacy format; try to guess the address
        LCLS_detector_address = 'CxiDs1-0|Cspad-0'
        if "DISTANCE" in source_data and source_data["DISTANCE"] > 1000:
            # downstream CS-PAD detector station of CXI instrument
            LCLS_detector_address = 'CxiDsd-0|Cspad-0'
    else:
        LCLS_detector_address = source_data["DETECTOR_ADDRESS"]
    timesec = reverse_timestamp(source_data["TIMESTAMP"])[0]
    version_lookup = detector_format_function(LCLS_detector_address, timesec)
    args = [
        "distl.detector_format_version=%s" % version_lookup,
        "viewer.powder_arcs.show=False",
        "viewer.powder_arcs.code=3n9c",
    ]

    horizons_phil = cxi_phil.cxi_versioned_extract(args).persist.commands

    img = NpyImage(params.file_path, source_data)
    img.readHeader(horizons_phil)
    img.translate_tiles(horizons_phil)
    if params.verbose:
        img.show_header()

    the_tiles = img.get_tile_manager(
        horizons_phil).effective_tiling_as_flex_int(
            reapply_peripheral_margin=False, encode_inactive_as_zeroes=True)

    if params.beam_x is None:
        params.beam_x = img.beamx / img.pixel_size
    if params.beam_y is None:
        params.beam_y = img.beamy / img.pixel_size
    if params.verbose:
        logger.write("I think the beam center is (%s,%s)\n" %
                     (params.beam_x, params.beam_y))

    bc = (int(params.beam_x), int(params.beam_y))

    extent = int(
        math.ceil(
            max(distance((0, 0), bc), distance((img.size1, 0), bc),
                distance((0, img.size2), bc),
                distance((img.size1, img.size2), bc))))

    if params.n_bins < extent:
        params.n_bins = extent

    extent_in_mm = extent * img.pixel_size
    extent_two_theta = math.atan(extent_in_mm / img.distance) * 180 / math.pi

    sums = flex.double(params.n_bins) * 0
    sums_sq = flex.double(params.n_bins) * 0
    counts = flex.int(params.n_bins) * 0
    data = img.get_raw_data()

    if hasattr(data, "as_double"):
        data = data.as_double()

    logger.write("Average intensity: %9.3f\n" % flex.mean(data))

    if params.verbose:
        logger.write("Generating average...tile:")
        logger.flush()
    for tile in xrange(len(the_tiles) // 4):
        if params.verbose:
            logger.write(" %d" % tile)
            logger.flush()

        x1, y1, x2, y2 = get_tile_coords(the_tiles, tile)

        radial_average(data, bc, sums, sums_sq, counts, img.pixel_size,
                       img.distance, (x1, y1), (x2, y2))

    if params.verbose:
        logger.write(" Finishing...\n")

    # average, avoiding division by zero
    results = sums.set_selected(counts <= 0, 0)
    results /= counts.set_selected(counts <= 0, 1).as_double()

    # calculte standard devations
    std_devs = [
        math.sqrt((sums_sq[i] - sums[i] * results[i]) /
                  counts[i]) if counts[i] > 0 else 0 for i in xrange(len(sums))
    ]

    xvals = flex.double(len(results))
    max_twotheta = float('-inf')
    max_result = float('-inf')

    for i in xrange(len(results)):
        twotheta = i * extent_two_theta / params.n_bins
        xvals[i] = twotheta

        if params.output_bins and "%.3f" % results[i] != "nan":
            #logger.write("%9.3f %9.3f\n"%     (twotheta,results[i]))        #.xy  format for Rex.cell.
            logger.write(
                "%9.3f %9.3f %9.3f\n" %
                (twotheta, results[i], std_devs[i]))  #.xye format for GSASII
        #logger.write("%.3f %.3f %.3f\n"%(twotheta,results[i],ds[i]))  # include calculated d spacings
        if results[i] > max_result:
            max_twotheta = twotheta
            max_result = results[i]

    logger.write(
        "Maximum 2theta for %s, TS %s: %f, value: %f\n" %
        (params.file_path, source_data['TIMESTAMP'], max_twotheta, max_result))

    if params.verbose:
        from pylab import scatter, show, xlabel, ylabel, ylim
        scatter(xvals, results)
        xlabel("2 theta")
        ylabel("Avg ADUs")
        if params.plot_y_max is not None:
            ylim(0, params.plot_y_max)
        show()

    return xvals, results
示例#2
0
  extent = int(math.ceil(max(distance((0,0),bc),
                             distance((img.size1,0),bc),
                             distance((0,img.size2),bc),
                             distance((img.size1,img.size2),bc))))

  if params.n_bins < extent:
    params.n_bins = extent

  extent_in_mm = extent * img.pixel_size
  extent_two_theta = math.atan(extent_in_mm/img.distance)*180/math.pi

  sums    = flex.double(params.n_bins) * 0
  sums_sq = flex.double(params.n_bins) * 0
  counts  = flex.int(params.n_bins) * 0
  data    = img.get_raw_data()

  if hasattr(data,"as_double"):
    data = data.as_double()

  logger.write("Average intensity: %9.3f\n"%flex.mean(data))

  if params.verbose:
    logger.write("Generating average...tile:")
    logger.flush()
  for tile in xrange(len(the_tiles)//4):
    if params.verbose:
      logger.write(" %d"%tile)
      logger.flush()

    x1,y1,x2,y2 = get_tile_coords(the_tiles,tile)
示例#3
0
    extent = int(
        math.ceil(
            max(distance((0, 0), bc), distance((img.size1, 0), bc),
                distance((0, img.size2), bc),
                distance((img.size1, img.size2), bc))))

    if params.n_bins < extent:
        params.n_bins = extent

    extent_in_mm = extent * img.pixel_size
    extent_two_theta = math.atan(extent_in_mm / img.distance) * 180 / math.pi

    sums = flex.double(params.n_bins) * 0
    sums_sq = flex.double(params.n_bins) * 0
    counts = flex.int(params.n_bins) * 0
    data = img.get_raw_data()

    if hasattr(data, "as_double"):
        data = data.as_double()

    logger.write("Average intensity: %9.3f\n" % flex.mean(data))

    if params.verbose:
        logger.write("Generating average...tile:")
        logger.flush()
    for tile in xrange(len(the_tiles) // 4):
        if params.verbose:
            logger.write(" %d" % tile)
            logger.flush()

        x1, y1, x2, y2 = get_tile_coords(the_tiles, tile)