Beispiel #1
0
def main():
    description = "A tool to extract the mask from an Eiger detector file."
    version = "eiger-mask version %s from %s" % (pyFAI.version, pyFAI.date)
    epilog = None
    if h5py is None:
        epilog = "Python h5py module is missing. It have to be installed to use this application"
    parser = ArgumentParser(description=description, epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument('input_file', help='Input file. Must be an HDF5 file.')
    parser.add_argument('output_file', nargs="?", help='Output file. It can be an msk, tif, or an edf file.')
    options = parser.parse_args()

    if h5py is None:
        logger.error("Python h5py module is expected to use this script")
        sys.exit(1)

    infile = os.path.abspath(options.input_file)
    if options.output_file is not None:
        outfile = options.output_file
    else:
        outfile = os.path.splitext(infile)[0] + "_mask.edf"

    mask = extract_mask(infile)
    if outfile.endswith("msk"):
        fabio.fit2dmaskimage.fit2dmaskimage(data=mask).write(outfile)
    elif outfile.endswith("tif"):
        fabio.tifimage.tifimage(data=mask).write(outfile)
    else:
        fabio.edfimage.edfimage(header={"data_file": infile}, data=mask).write(outfile)
Beispiel #2
0
def main():
    description = "A tool to extract the mask from an Eiger detector file."
    version = "eiger-mask version %s from %s" % (pyFAI.version, pyFAI.date)
    epilog = None
    if h5py is None:
        epilog = "Python h5py module is missing. It have to be installed to use this application"
    parser = ArgumentParser(description=description, epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument('input_file', help='Input file. Must be an HDF5 file.')
    parser.add_argument('output_file', nargs="?", help='Output file. It can be an msk, tif, or an edf file.')
    options = parser.parse_args()

    if h5py is None:
        logger.error("Python h5py module is expected to use this script")
        sys.exit(1)

    infile = os.path.abspath(options.input_file)
    if options.output_file is not None:
        outfile = options.output_file
    else:
        outfile = os.path.splitext(infile)[0] + "_mask.edf"

    mask = extract_mask(infile)
    if outfile.endswith("msk"):
        fabio.fit2dmaskimage.fit2dmaskimage(data=mask).write(outfile)
    elif outfile.endswith("tif"):
        fabio.tifimage.tifimage(data=mask).write(outfile)
    else:
        fabio.edfimage.edfimage(header={"data_file": infile}, data=mask).write(outfile)
Beispiel #3
0
def main():
    usage = "pyFAI-drawmask file1.edf file2.edf ..."
    version = "pyFAI-average version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    Draw a mask, i.e. an image containing the list of pixels which are considered invalid
    (no scintillator, module gap, beam stop shadow, ...).
    This will open a window and let you draw on the first image
    (provided) with different tools (brush, rectangle selection...)
    When you are finished, click on the "Save and quit" button.
    """
    epilog = """The mask image is saved into file1-masked.edf.
    Optionally the script will print the number of pixel masked
    and the intensity masked (as well on other files provided in input)"""
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("args",
                        metavar='FILE',
                        type=str,
                        nargs='+',
                        help="Files to be processed")

    options = parser.parse_args()
    if len(options.args) < 1:
        parser.error(
            "Incorrect number of arguments: please provide an image to draw a mask"
        )

    processFile = pyFAI.utils.expand_args(options.args)

    app = qt.QApplication([])

    window = MaskImageWidget()
    image = fabio.open(processFile[0]).data
    window.setImageData(image)
    window.show()
    outfile = os.path.splitext(processFile[0])[0] + "-mask.edf"
    window.setOutputFile(outfile)

    print("Your mask-file will be saved into %s" % (outfile))

    app.exec_()

    mask = window.getSelectionMask()
    postProcessId21(processFile, mask)
Beispiel #4
0
def main():
    usage = "pyFAI-drawmask file1.edf file2.edf ..."
    version = "pyFAI-average version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    Draw a mask, i.e. an image containing the list of pixels which are considered invalid
    (no scintillator, module gap, beam stop shadow, ...).
    This will open a window and let you draw on the first image
    (provided) with different tools (brush, rectangle selection...)
    When you are finished, click on the "Save and quit" button.
    """
    epilog = """The mask image is saved into file1-masked.edf.
    Optionally the script will print the number of pixel masked
    and the intensity masked (as well on other files provided in input)"""
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("args", metavar='FILE', type=str, nargs='+',
                        help="Files to be processed")

    options = parser.parse_args()
    if len(options.args) < 1:
        parser.error("Incorrect number of arguments: please provide an image to draw a mask")

    processFile = pyFAI.utils.expand_args(options.args)

    app = qt.QApplication([])

    window = MaskImageWidget()
    image = fabio.open(processFile[0]).data
    window.setImageData(image)
    window.show()
    outfile = os.path.splitext(processFile[0])[0] + "-mask.edf"
    window.setOutputFile(outfile)

    print("Your mask-file will be saved into %s" % (outfile))

    app.exec_()

    mask = window.getSelectionMask()
    postProcessId21(processFile, mask)
Beispiel #5
0
def main():
    usage = "pyFAI-integrate [options] file1.edf file2.edf ..."
    version = "pyFAI-integrate version %s from %s" % (pyFAI.version,
                                                      pyFAI.date)
    description = """
    PyFAI-integrate is a graphical interface (based on Python/Qt4) to perform azimuthal integration
on a set of files. It exposes most of the important options available within pyFAI and allows you
to select a GPU (or an openCL platform) to perform the calculation on."""
    epilog = """PyFAI-integrate saves all parameters in a .azimint.json (hidden) file. This JSON file
is an ascii file which can be edited and used to configure online data analysis using
the LImA plugin of pyFAI.

Nota: there is bug in debian6 making the GUI crash (to be fixed inside pyqt)
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=697348"""
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        dest="verbose",
                        default=False,
                        help="switch to verbose/debug mode")
    parser.add_argument(
        "-o",
        "--output",
        dest="output",
        default=None,
        help="Directory or file where to store the output data")
    parser.add_argument("-f",
                        "--format",
                        dest="format",
                        default=None,
                        help="output data format (can be HDF5)")
    parser.add_argument(
        "-s",
        "--slow-motor",
        dest="slow",
        default=None,
        help=
        "Dimension of the scan on the slow direction (makes sense only with HDF5)"
    )
    parser.add_argument(
        "-r",
        "--fast-motor",
        dest="rapid",
        default=None,
        help=
        "Dimension of the scan on the fast direction (makes sense only with HDF5)"
    )
    parser.add_argument(
        "--no-gui",
        dest="gui",
        default=True,
        action="store_false",
        help="Process the dataset without showing the user interface.")
    parser.add_argument(
        "-j",
        "--json",
        dest="json",
        default=".azimint.json",
        help="Configuration file containing the processing to be done")
    parser.add_argument("args",
                        metavar='FILE',
                        type=str,
                        nargs='*',
                        help="Files to be integrated")
    parser.add_argument("--monitor-name",
                        dest="monitor_key",
                        default=None,
                        help="Name of the monitor in the header of each input \
                        files. If defined the contribution of each input file \
                        is divided by the monitor. If the header does not \
                        contain or contains a wrong value, the contribution \
                        of the input file is ignored.\
                        On EDF files, values from 'counter_pos' can accessed \
                        by using the expected mnemonic. \
                        For example 'counter/bmon'.")
    options = parser.parse_args()

    # Analysis arguments and options
    args = pyFAI.utils.expand_args(options.args)

    if options.verbose:
        logger.info("setLevel: debug")
        logger.setLevel(logging.DEBUG)

    if options.gui:
        result = integrate_gui(options, args)
    else:
        result = integrate_shell(options, args)
    sys.exit(result)
Beispiel #6
0
def main():
    usage = "pyFAI-saxs [options] -n 1000 -p ponifile file1.edf file2.edf ..."
    version = "PyFAI-saxs version %s from %s " % (pyFAI.version, pyFAI.date)
    description = """Azimuthal integration for SAXS users."""
    epilog = """pyFAI-saxs is the SAXS script of pyFAI that allows data
    reduction (azimuthal integration) for Small Angle Scattering with output
    axis in q space."""
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("args", metavar="FILE", type=str, nargs='+',
                        help="Image files to integrate")
    parser.add_argument("-p", dest="ponifile",
                        type=str, default=None,
                        help="PyFAI parameter file (.poni)")
    parser.add_argument("-n", "--npt", dest="npt",
                        type=int, default=None,
                        help="Number of points in radial dimension")
    parser.add_argument("-w", "--wavelength", dest="wavelength", type=float,
                        help="wavelength of the X-Ray beam in Angstrom", default=None)
    parser.add_argument("-e", "--energy", dest="energy", type=float,
                        help="energy of the X-Ray beam in keV (hc=%skeV.A)" %
                        hc, default=None)
    parser.add_argument("-u", "--dummy", dest="dummy",
                        type=float, default=None,
                        help="dummy value for dead pixels")
    parser.add_argument("-U", "--delta_dummy", dest="delta_dummy",
                        type=float, default=None,
                        help="delta dummy value")
    parser.add_argument("-m", "--mask", dest="mask",
                        type=str, default=None,
                        help="name of the file containing the mask image")
    parser.add_argument("-d", "--dark", dest="dark",
                        type=str, default=None,
                        help="name of the file containing the dark current")
    parser.add_argument("-f", "--flat", dest="flat",
                        type=str, default=None,
                        help="name of the file containing the flat field")
#    parser.add_argument("-b", "--background", dest="background",
#                      type="string", default=None,
#                      help="name of the file containing the background")
    parser.add_argument("-P", "--polarization", dest="polarization_factor",
                        type=float, default=None,
                        help="Polarization factor, from -1 (vertical) to +1 (horizontal), \
                      default is None for no correction, synchrotrons are around 0.95")
    parser.add_argument("--error-model", dest="error_model",
                        type=str, default=None,
                        help="Error model to use. Currently on 'poisson' is implemented ")
    parser.add_argument("--unit", dest="unit",
                        type=str, default="q_nm^-1",
                        help="unit for the radial dimension: can be q_nm^-1, q_A^-1, 2th_deg, \
                          2th_rad or r_mm")
    parser.add_argument("--ext", dest="ext",
                        type=str, default=".dat",
                        help="extension of the regrouped filename (.dat)")
    parser.add_argument("--method", dest="method",
                        type=str, default=None,
                        help="Integration method ")

    options = parser.parse_args()
    if len(options.args) < 1:
        logger.error("incorrect number of arguments")
    to_process = pyFAI.utils.expand_args(options.args)

    if options.ponifile and to_process:
        integrator = pyFAI.load(options.ponifile)
        if options.wavelength:
            integrator.wavelength = options.wavelength * 1e-10
        elif options.energy:
            integrator.wavelength = hc / options.energy * 1e-10
        if options.mask and os.path.exists(options.mask):  # override with the command line mask
            integrator.maskfile = options.mask
        if options.dark and os.path.exists(options.dark):  # set dark current
            integrator.darkcurrent = fabio.open(options.dark).data
        if options.flat and os.path.exists(options.flat):  # set Flat field
            integrator.flatfield = fabio.open(options.flat).data
        if to_process:
            first = to_process[0]
            fabimg = fabio.open(first)
            integrator.detector.guess_binning(fabimg.data)

        if options.method:
            method = options.method
        else:
            if len(to_process) > 5:
                method = "full_csr"
            else:
                method = "splitpixel"
        print(integrator)
        print("Mask: %s\tMethod: %s" % (integrator.maskfile, method))

        for afile in to_process:
            sys.stdout.write("Integrating %s --> " % afile)
            outfile = os.path.splitext(afile)[0] + options.ext
            t0 = time.time()
            fimg = fabio.open(afile)
            t1 = time.time()
            if fimg.nframes > 1:
                integrator.integrate1d(data=fimg.data,
                                       npt=options.npt or min(fimg.data.shape),
                                       dummy=options.dummy,
                                       delta_dummy=options.delta_dummy,
                                       filename=outfile,
                                       variance=fimg.next().data,
                                       method=method,
                                       unit=options.unit,
                                       error_model=options.error_model,
                                       polarization_factor=options.polarization_factor,
                                       metadata=fimg.header
                                       )
            else:
                integrator.integrate1d(data=fimg.data,
                                       npt=options.npt or min(fimg.data.shape),
                                       dummy=options.dummy,
                                       delta_dummy=options.delta_dummy,
                                       filename=outfile,
                                       method=method,
                                       unit=options.unit,
                                       error_model=options.error_model,
                                       polarization_factor=options.polarization_factor,
                                       metadata=fimg.header)
            t2 = time.time()

            msg = "%s,\t reading: %.3fs\t 1D integration: %.3fs."
            print(msg % (outfile, t1 - t0, t2 - t1))
Beispiel #7
0
def main():
    from pyFAI.third_party.argparse import ArgumentParser
    description = """Benchmark for Azimuthal integration
    """
    epilog = """  """
    usage = """benchmark [options] """
    version = "pyFAI benchmark version " + pyFAI.version
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("-d",
                        "--debug",
                        action="store_true",
                        dest="debug",
                        default=False,
                        help="switch to verbose/debug mode")
    parser.add_argument("-c",
                        "--cpu",
                        action="store_true",
                        dest="opencl_cpu",
                        default=False,
                        help="perform benchmark using OpenCL on the CPU")
    parser.add_argument("-g",
                        "--gpu",
                        action="store_true",
                        dest="opencl_gpu",
                        default=False,
                        help="perform benchmark using OpenCL on the GPU")
    parser.add_argument(
        "-a",
        "--acc",
        action="store_true",
        dest="opencl_acc",
        default=False,
        help=
        "perform benchmark using OpenCL on the Accelerator (like XeonPhi/MIC)")
    parser.add_argument(
        "-s",
        "--size",
        type=float,
        dest="size",
        default=1000,
        help=
        "Limit the size of the dataset to X Mpixel images (for computer with limited memory)"
    )
    parser.add_argument("-n",
                        "--number",
                        dest="number",
                        default=10,
                        type=int,
                        help="Number of repetition of the test, by default 10")
    parser.add_argument("-2d",
                        "--2dimention",
                        action="store_true",
                        dest="twodim",
                        default=False,
                        help="Benchmark also algorithm for 2D-regrouping")
    parser.add_argument("--no-1dimention",
                        action="store_false",
                        dest="onedim",
                        default=True,
                        help="Do not benchmark algorithms for 1D-regrouping")

    parser.add_argument("-m",
                        "--memprof",
                        action="store_true",
                        dest="memprof",
                        default=False,
                        help="Perfrom memory profiling (Linux only)")
    parser.add_argument("-r",
                        "--repeat",
                        dest="repeat",
                        default=1,
                        type=int,
                        help="Repeat each benchmark x times to take the best")

    options = parser.parse_args()
    if options.debug:
        pyFAI.logger.setLevel(logging.DEBUG)
    devices = ""
    if options.opencl_cpu:
        devices += "cpu,"
    if options.opencl_gpu:
        devices += "gpu,"
    if options.opencl_acc:
        devices += "acc,"

    pyFAI.benchmark.run(number=options.number,
                        repeat=options.repeat,
                        memprof=options.memprof,
                        max_size=options.size,
                        do_1d=options.onedim,
                        do_2d=options.twodim,
                        devices=devices)

    pyFAI.benchmark.pylab.ion()
    six.moves.input("Enter to quit")
Beispiel #8
0
def main():
    usage = "detector2nexus [options] [options] -o nxs.h5"
    version = "detector2nexus version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    Convert a complex detector definition (multiple modules, possibly in 3D)
    into a single NeXus detector definition together with the mask (and much more in the future)
    """
    epilog = """
    This summarizes detector2nexus
    """
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument("-o", "--output", dest="output",
                        type=str, default=None,
                        help="Output nexus file, unless detector_name.h5")
    parser.add_argument("-n", "--name", dest="name",
                        type=str, default=None,
                        help="name of the detector")
    parser.add_argument("-m", "--mask", dest="mask",
                        type=str, default=None,
                        help="mask corresponding to the detector")
    parser.add_argument("-D", "--detector", dest="detector", type=str,
                        default="Detector",
                        help="Base detector name (see documentation of pyFAI.detectors")
    parser.add_argument("-s", "--splinefile", dest="splinefile", type=str,
                        default=None,
                        help="Geometric distortion file from FIT2D")
    parser.add_argument("--dx", "--x-corr", dest="dx", type=str, default=None,
                        help="Geometric correction for pilatus")
    parser.add_argument("--dy", "--y-corr", dest="dy", type=str, default=None,
                        help="Geometric correction for pilatus")
    parser.add_argument("-p", "--pixel", dest="pixel", type=str, default=None,
                        help="pixel size (comma separated): x,y")
    parser.add_argument("-S", "--shape", dest="shape", type=str, default=None,
                        help="shape of the detector (comma separated): x,y")
    parser.add_argument("-d", "--dark", dest="dark", type=str, default=None,
                        help="Dark noise to be subtracted")
    parser.add_argument("-f", "--flat", dest="flat", type=str, default=None,
                        help="Flat field correction")
    parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=False,
                        help="switch to verbose/debug mode")
    # parser.add_argument("args", metavar='FILE', type=str, nargs='+',
    #                     help="Files to be processed")

    argv = sys.argv
    # hidden backward compatibility for -dx and -dy
    # A short option only expect a single char
    argv = ["-" + a if a.startswith("-dx") else a for a in argv]
    argv = ["-" + a if a.startswith("-dy") else a for a in argv]
    print(argv)
    options = parser.parse_args(args=argv[1:])

    if options.verbose:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    detector = pyFAI.detectors.detector_factory(options.detector)
    if options.output:
        output = options.output
    else:
        output = detector.name + ".h5"

    if options.mask:
        mask = fabio.open(options.mask).data.astype(bool)
        if detector.mask is None:
            detector.mask = mask
        else:
            detector.mask = numpy.logical_or(mask, detector.mask)

    if options.flat:
        detector.flat = fabio.open(options.flat).data
    if options.dark:
        detector.dark = fabio.open(options.dark).data
    if options.splinefile:
        detector.set_splineFile(options.splinefile)
    else:
        if options.pixel:
            p = options.pixel.split(",")
            psize = float(p[0])
            if len(p) == 1:
                detector.pixel1 = psize
                detector.pixel2 = psize
            else:
                detector.pixel1 = float(p[1])
                detector.pixel2 = psize
        if options.shape:
            p = options.shape.split(",")
            psize = int(p[0])
            if len(p) == 1:
                detector.shape = psize, psize
            else:
                detector.shape = int(p[1]), psize
        if options.dx and options.dy:
            dx = fabio.open(options.dx).data
            dy = fabio.open(options.dy).data
            # pilatus give displaceemt in percent of pixel ...
            if ".cbf" in options.dx:
                dx *= 0.01
            if ".cbf" in options.dy:
                dy *= 0.01
            detector.set_dx(dx)
            detector.set_dy(dy)
    detector.save(output)
Beispiel #9
0
def parse():
    """
    Parse command line arguments
    """
    parser = ArgumentParser(description=__doc__)
    parser.add_argument('-d', '--dir', dest='directory', default=tempfile.gettempdir(),
                        help='Destination directory (/tmp)')
    parser.add_argument('-n', '--number', dest='n', default=1024, type=int,
                        help='Number of frames to write')
    parser.add_argument('-w', '--width', dest='width', default=1024, type=int,
                        help='width of a frame (1024)')
    parser.add_argument('-H', '--height', dest='height', default=1024, type=int,
                        help='height of the image (1024)')
    parser.add_argument('-t', '--type', dest='dtype', default="float32", type=str,
                        help='data type of item (float32)')
    parser.add_argument('-b', '--bsize', dest='bsize', default=10, type=int,
                        help='size of the random buffer for frames (10)')
    opt = parser.parse_args()
    return opt
Beispiel #10
0
def main():
    usage = "pyFAI-waxs [options] -p ponifile file1.edf file2.edf ..."
    version = "pyFAI-waxs version %s from %s" % (pyFAI.version, pyFAI.date)
    description = "Azimuthal integration for powder diffraction."
    epilog = """pyFAI-waxs is the script of pyFAI that allows data reduction
    (azimuthal integration) for Wide Angle Scattering to produce X-Ray Powder
    Diffraction Pattern with output axis in 2-theta space.
    """
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("args", metavar="FILE", type=str, nargs='+',
                        help="Image files to integrate")
    parser.add_argument("-p", dest="ponifile",
                        type=str, default=None,
                        help="PyFAI parameter file (.poni)")
    parser.add_argument("-n", "--npt", dest="npt",
                        type=int, default=None,
                        help="Number of points in radial dimension")
    parser.add_argument("-w", "--wavelength", dest="wavelength", type=float,
                        help="wavelength of the X-Ray beam in Angstrom", default=None)
    parser.add_argument("-e", "--energy", dest="energy", type=float,
                        help="energy of the X-Ray beam in keV (hc=%skeV.A)" %
                        hc, default=None)
    parser.add_argument("-u", "--dummy", dest="dummy",
                        type=float, default=None,
                        help="dummy value for dead pixels")
    parser.add_argument("-U", "--delta_dummy", dest="delta_dummy",
                        type=float, default=None,
                        help="delta dummy value")
    parser.add_argument("-m", "--mask", dest="mask",
                        type=str, default=None,
                        help="name of the file containing the mask image")
    parser.add_argument("-d", "--dark", dest="dark",
                        type=str, default=None,
                        help="name of the file containing the dark current")
    parser.add_argument("-f", "--flat", dest="flat",
                        type=str, default=None,
                        help="name of the file containing the flat field")
    parser.add_argument("-P", "--polarization", dest="polarization_factor",
                        type=float, default=None,
                        help="Polarization factor, from -1 (vertical) to +1 (horizontal), \
                          default is None for no correction, synchrotrons are around 0.95")

#    parser.add_argument("-b", "--background", dest="background",
#                      type=str, default=None,
#                      help="name of the file containing the background")
    parser.add_argument("--error-model", dest="error_model",
                        type=str, default=None,
                        help="Error model to use. Currently on 'poisson' is implemented ")
    parser.add_argument("--unit", dest="unit",
                        type=str, default="2th_deg",
                        help="unit for the radial dimension: can be q_nm^-1, q_A^-1, 2th_deg, \
                          2th_rad or r_mm")
    parser.add_argument("--ext", dest="ext",
                        type=str, default=".xy",
                        help="extension of the regrouped filename (.xy) ")
    parser.add_argument("--method", dest="method",
                        type=str, default=None,
                        help="Integration method ")
    parser.add_argument("--multi", dest="multiframe",  # type=bool,
                        default=False, action="store_true",
                        help="Average out all frame in a file before integrating extracting variance, otherwise treat every single frame")
    parser.add_argument("--average", dest="average", type=str,
                        default="mean",
                        help="Method for averaging out: can be 'mean' (default), 'min', 'max' or 'median")
    parser.add_argument("--do-2D", dest="do_2d",
                        default=False, action="store_true",
                        help="Perform 2D integration in addition to 1D")

    options = parser.parse_args()
    if len(options.args) < 1:
        logger.error("incorrect number of arguments")

    to_process = pyFAI.utils.expand_args(options.args)

    if options.ponifile and to_process:
        integrator = pyFAI.load(options.ponifile)
        if options.wavelength:
            integrator.wavelength = options.wavelength * 1e-10
        elif options.energy:
            integrator.wavelength = hc / options.energy * 1e-10
        if options.mask and os.path.exists(options.mask):  # override with the command line mask
            integrator.maskfile = options.mask
        if options.dark and os.path.exists(options.dark):  # set dark current
            integrator.darkcurrent = fabio.open(options.dark).data
        if options.flat and os.path.exists(options.flat):  # set Flat field
            integrator.flatfield = fabio.open(options.flat).data

        if options.method:
            method = options.method
        else:
            if len(to_process) > 5:
                method = "full_csr"
            else:
                method = "splitpixel"
        if to_process:
            first = to_process[0]
            fabimg = fabio.open(first)
            integrator.detector.guess_binning(fabimg.data)
        print(integrator)
        print("Mask: %s\tMethod: %s" % (integrator.maskfile, method))
        for afile in to_process:
            sys.stdout.write("Integrating %s --> " % afile)
            outfile = os.path.splitext(afile)[0] + options.ext
            azimFile = os.path.splitext(afile)[0] + ".azim"
            t0 = time.time()
            fabimg = fabio.open(afile)
            if options.multiframe:
                data = pyFAI.average.average_dark([fabimg.getframe(i).data for i in range(fabimg.nframes)], center_method=options.average)
            else:
                data = fabimg.data
            t1 = time.time()
            integrator.integrate1d(data,
                                   options.npt or min(fabimg.data.shape),
                                   filename=outfile,
                                   dummy=options.dummy,
                                   delta_dummy=options.delta_dummy,
                                   method=method,
                                   unit=options.unit,
                                   error_model=options.error_model,
                                   polarization_factor=options.polarization_factor,
                                   metadata=fabimg.header
                                   )
            t2 = time.time()
            if options.do_2d:
                integrator.integrate2d(data,
                                       options.npt or min(fabimg.data.shape),
                                       360,
                                       filename=azimFile,
                                       dummy=options.dummy,
                                       delta_dummy=options.delta_dummy,
                                       method=method,
                                       unit=options.unit,
                                       error_model=options.error_model,
                                       polarization_factor=options.polarization_factor,
                                       metadata=fabimg.header
                                       )
                msg = "%s\t reading: %.3fs\t 1D integration: %.3fs,\t 2D integration %.3fs."
                print(msg % (outfile, t1 - t0, t2 - t1, time.time() - t2))
            else:
                msg = "%s,\t reading: %.3fs\t 1D integration: %.3fs."
                print(msg % (outfile, t1 - t0, t2 - t1))
Beispiel #11
0
def setup(model):
    usage = "pyFAI-calib2 [options] input_image.edf"
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    version = "calibration from pyFAI  version %s: %s" % (pyFAI.version,
                                                          pyFAI.date)
    parser.add_argument("-V", "--version", action='version', version=version)
    configure_parser_arguments(parser)

    # Analyse aruments and options
    options = parser.parse_args()
    args = options.args

    # Settings
    settings = model.experimentSettingsModel()
    if options.spacing:
        calibrant = None
        if options.spacing in pyFAI.calibrant.CALIBRANT_FACTORY:
            calibrant = pyFAI.calibrant.CALIBRANT_FACTORY(options.spacing)
        elif os.path.isfile(options.spacing):
            calibrant = pyFAI.calibrant.Calibrant(options.spacing)
        else:
            logger.error("No such Calibrant / d-Spacing file: %s",
                         options.spacing)
        if calibrant:
            settings.calibrantModel().setCalibrant(calibrant)

    if options.wavelength:
        settings.wavelength().setValue(options.wavelength)

    if options.energy:
        settings.wavelength().setValue(pyFAI.units.hc / options.energy)

    if options.polarization_factor:
        settings.polarizationFactor(options.polarization_factor)

    if options.detector_name:
        detector = pyFAI.calibration.get_detector(options.detector_name, args)
        settings.detectorModel().setDetector(detector)

    if options.spline:
        settings.splineFile().setValue(options.spline)

    if options.mask:
        settings.maskFile().setValue(options.mask)
        with fabio.open(options.mask) as mask:
            settings.mask().setValue(mask.data)

    if len(args) == 0:
        pass
    elif len(args) == 1:
        image_file = args[0]
        settings.imageFile().setValue(image_file)
        with fabio.open(image_file) as image:
            settings.image().setValue(image.data)
    else:
        logger.error("Too much images provided. Only one is expected")

    # Geometry
    # FIXME it will not be used cause the fitted geometry will be overwrited
    geometry = model.fittedGeometry()
    if options.distance:
        geometry.distance().setValue(1e-3 * options.distance)
    if options.dist:
        geometry.distance().setValue(options.dist)
    if options.dist:
        geometry.poni1().setValue(options.poni1)
    if options.dist:
        geometry.poni2().setValue(options.poni2)
    if options.dist:
        geometry.rotation1().setValue(options.rot1)
    if options.dist:
        geometry.rotation2().setValue(options.rot2)
    if options.dist:
        geometry.rotation3().setValue(options.rot3)

    # Constraints
    constraints = model.geometryConstraintsModel()
    if options.fix_wavelength is not None:
        constraints.wavelength().setFixed(options.fix_wavelength)
    if options.fix_dist is not None:
        constraints.distance().setFixed(options.fix_dist)
    if options.fix_poni1 is not None:
        constraints.poni1().setFixed(options.fix_poni1)
    if options.fix_poni2 is not None:
        constraints.poni2().setFixed(options.fix_poni2)
    if options.fix_rot1 is not None:
        constraints.rotation1().setFixed(options.fix_rot1)
    if options.fix_rot2 is not None:
        constraints.rotation2().setFixed(options.fix_rot2)
    if options.fix_rot3 is not None:
        constraints.rotation3().setFixed(options.fix_rot3)

    # Integration
    if options.unit:
        unit = pyFAI.units.to_unit(options.unit)
        model.integrationSettingsModel().radialUnit().setValue(unit)

    if options.outfile:
        logger.error("outfile option not supported")
    if options.debug:
        logger.error("debug option not supported")

    if options.reconstruct:
        logger.error("reconstruct option not supported")
    if options.gaussian:
        logger.error("gaussian option not supported")
    if options.square:
        logger.error("square option not supported")
    if options.pixel:
        logger.error("pixel option not supported")

    # FIXME poni file should be supported
    if options.poni:
        logger.error("poni option not supported")
    if options.background:
        logger.error("background option not supported")
    if options.dark:
        logger.error("dark option not supported")
    if options.flat:
        logger.error("flat option not supported")
    if options.npt:
        logger.error("npt option not supported")
    if options.filter:
        logger.error("filter option not supported")

    if options.tilt:
        logger.error("tilt option not supported")
    if options.saturation:
        logger.error("saturation option not supported")
    if options.weighted:
        logger.error("weighted option not supported")
    if options.nPt_1D:
        logger.error("nPt_1D option not supported")
    if options.nPt_2D_azim:
        logger.error("nPt_2D_azim option not supported")
    if options.nPt_2D_rad:
        logger.error("nPt_2D_rad option not supported")

    if options.gui is not True:
        logger.error("gui option not supported")
    if options.interactive is not True:
        logger.error("interactive option not supported")
Beispiel #12
0
def setup(model):
    usage = "pyFAI-calib2 [options] input_image.edf"
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    version = "calibration from pyFAI  version %s: %s" % (pyFAI.version,
                                                          pyFAI.date)
    parser.add_argument("-V", "--version", action='version', version=version)
    configure_parser_arguments(parser)

    # Analyse aruments and options
    options = parser.parse_args()
    args = options.args

    if options.debug:
        logging.root.setLevel(logging.DEBUG)

    # Settings
    settings = model.experimentSettingsModel()
    if options.spacing:
        calibrant = None
        if options.spacing in pyFAI.calibrant.CALIBRANT_FACTORY:
            calibrant = pyFAI.calibrant.CALIBRANT_FACTORY(options.spacing)
        elif os.path.isfile(options.spacing):
            calibrant = pyFAI.calibrant.Calibrant(options.spacing)
        else:
            logger.error("No such Calibrant / d-Spacing file: %s",
                         options.spacing)
        if calibrant:
            settings.calibrantModel().setCalibrant(calibrant)

    if options.wavelength:
        value = units.convert(options.wavelength, units.Unit.ANGSTROM,
                              units.Unit.METER_WL)
        settings.wavelength().setValue(value)

    if options.energy:
        value = units.convert(options.energy, units.Unit.ENERGY,
                              units.Unit.METER_WL)
        settings.wavelength().setValue(value)

    if options.polarization_factor:
        settings.polarizationFactor(options.polarization_factor)

    if options.detector_name:
        detector = pyFAI.gui.cli_calibration.get_detector(
            options.detector_name, args)
        if options.pixel:
            logger.warning(
                "Detector model already specified. Pixel size argument ignored."
            )
    elif options.pixel:
        pixel_size = parse_pixel_size(options.pixel)
        detector = pyFAI.detectors.Detector(pixel1=pixel_size[0],
                                            pixel2=pixel_size[0])
    else:
        detector = None

    if options.spline:
        if detector is None:
            detector = pyFAI.detectors.Detector(splineFile=options.spline)
        elif detector.__class__ is pyFAI.detectors.Detector or detector.HAVE_TAPER:
            detector.set_splineFile(options.spline)
        else:
            logger.warning(
                "Spline file not supported with this kind of detector. Argument ignored."
            )

    settings.detectorModel().setDetector(detector)

    if options.mask:
        settings.maskFile().setValue(options.mask)
        with fabio.open(options.mask) as mask:
            settings.mask().setValue(mask.data)

    if len(args) == 0:
        pass
    elif len(args) == 1:
        image_file = args[0]
        settings.imageFile().setValue(image_file)
        with fabio.open(image_file) as image:
            settings.image().setValue(image.data)
    else:
        logger.error("Too much images provided. Only one is expected")

    # Geometry
    # FIXME it will not be used cause the fitted geometry will be overwrited
    geometry = model.fittedGeometry()
    if options.distance:
        geometry.distance().setValue(1e-3 * options.distance)
    if options.dist:
        geometry.distance().setValue(options.dist)
    if options.dist:
        geometry.poni1().setValue(options.poni1)
    if options.dist:
        geometry.poni2().setValue(options.poni2)
    if options.dist:
        geometry.rotation1().setValue(options.rot1)
    if options.dist:
        geometry.rotation2().setValue(options.rot2)
    if options.dist:
        geometry.rotation3().setValue(options.rot3)

    # Constraints
    constraints = model.geometryConstraintsModel()
    if options.fix_wavelength is not None:
        constraints.wavelength().setFixed(options.fix_wavelength)
    if options.fix_dist is not None:
        constraints.distance().setFixed(options.fix_dist)
    if options.fix_poni1 is not None:
        constraints.poni1().setFixed(options.fix_poni1)
    if options.fix_poni2 is not None:
        constraints.poni2().setFixed(options.fix_poni2)
    if options.fix_rot1 is not None:
        constraints.rotation1().setFixed(options.fix_rot1)
    if options.fix_rot2 is not None:
        constraints.rotation2().setFixed(options.fix_rot2)
    if options.fix_rot3 is not None:
        constraints.rotation3().setFixed(options.fix_rot3)

    integrationSettingsModel = model.integrationSettingsModel()
    npt = None
    if options.npt_1d is not None:
        npt = options.npt_1d
    if options.npt_2d_rad is not None:
        if npt is not None:
            logger.error(
                "Both --npt and --npt-rad defined. The biggest is used.")
            npt = max(npt, options.npt_2d_rad)

    if npt is not None:
        integrationSettingsModel.nPointsRadial().setValue(npt)
    else:
        integrationSettingsModel.nPointsRadial().setValue(1024)

    if options.npt_2d_azim is not None:
        integrationSettingsModel.nPointsAzimuthal().setValue(
            options.npt_2d_azim)
    else:
        integrationSettingsModel.nPointsAzimuthal().setValue(360)

    # Integration
    if options.unit:
        unit = pyFAI.units.to_unit(options.unit)
        integrationSettingsModel.radialUnit().setValue(unit)

    if options.outfile:
        logger.error("outfile option not supported")
    if options.debug:
        logger.error("debug option not supported")

    if options.reconstruct:
        logger.error("reconstruct option not supported")
    if options.gaussian:
        logger.error("gaussian option not supported")
    if options.square:
        logger.error("square option not supported")
    if options.pixel:
        logger.error("pixel option not supported")

    # FIXME poni file should be supported
    if options.poni:
        logger.error("poni option not supported")
    if options.background:
        logger.error("background option not supported")
    if options.dark:
        logger.error("dark option not supported")
    if options.flat:
        logger.error("flat option not supported")
    if options.filter:
        logger.error("filter option not supported")

    if options.tilt:
        logger.error("tilt option not supported")
    if options.saturation:
        logger.error("saturation option not supported")
    if options.weighted:
        logger.error("weighted option not supported")

    if options.gui is not True:
        logger.error("gui option not supported")
    if options.interactive is not True:
        logger.error("interactive option not supported")
Beispiel #13
0
 def get_options(self):
     """
     Parse the command line to analyse options ... returns options
     """
     if self.options is None:
         parser = ArgumentParser(usage="Tests for %s" % self.name)
         parser.add_argument("-d", "--debug", dest="debug", help="run in debugging mode",
                             default=False, action="store_true")
         parser.add_argument("-i", "--info", dest="info", help="run in more verbose mode ",
                             default=False, action="store_true")
         parser.add_argument("-f", "--force", dest="force", help="force the build of the library",
                             default=False, action="store_true")
         parser.add_argument("-r", "--really-force", dest="remove",
                             help="remove existing build and force the build of the library",
                             default=False, action="store_true")
         parser.add_argument(dest="args", type=str, nargs='*')
         self.options = parser.parse_args([])
     return self.options
Beispiel #14
0
def main():
    usage = "pyFAI-average [options] [options] -o output.edf file1.edf file2.edf ..."
    version = "pyFAI-average version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    This tool can be used to average out a set of dark current images using
    mean or median filter (along the image stack). One can also reject outliers
    be specifying a cutoff (remove cosmic rays / zingers from dark)
    """
    epilog = """It can also be used to merge many images from the same sample when using a small beam
    and reduce the spotty-ness of Debye-Sherrer rings. In this case the "max-filter" is usually
    recommended.
    """
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument("-o",
                        "--output",
                        dest="output",
                        type=str,
                        default=None,
                        help="Output/ destination of average image")
    parser.add_argument("-m",
                        "--method",
                        dest="method",
                        type=str,
                        default="",
                        help="Method used for averaging, can be 'mean' \
                        (default) or 'min', 'max', 'median', 'sum', 'quantiles'\
                        , 'cutoff', 'std'. Multiple filters can be defined with \
                        ',' separator.")
    parser.add_argument(
        "-c",
        "--cutoff",
        dest="cutoff",
        type=float,
        default=None,
        help="Take the mean of the average +/- cutoff * std_dev.")
    parser.add_argument("-F",
                        "--format",
                        dest="format",
                        type=str,
                        default="edf",
                        help="Output file/image format (by default EDF)")
    parser.add_argument("-d",
                        "--dark",
                        dest="dark",
                        type=str,
                        default=None,
                        help="Dark noise to be subtracted")
    parser.add_argument("-f",
                        "--flat",
                        dest="flat",
                        type=str,
                        default=None,
                        help="Flat field correction")
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        dest="verbose",
                        default=None,
                        help="switch to verbose/debug mode")
    parser.add_argument("-q",
                        "--quantiles",
                        dest="quantiles",
                        default=None,
                        help="average out between two quantiles -q 0.20-0.90")
    parser.add_argument("--monitor-name",
                        dest="monitor_key",
                        default=None,
                        help="Name of the monitor in the header of each input \
                        files. If defined the contribution of each input file \
                        is divided by the monitor. If the header does not \
                        contain or contains a wrong value, the contribution of \
                        the input file is ignored.\
                        On EDF files, values from 'counter_pos' can accessed by \
                        using the expected mnemonic. \
                        For example 'counter/bmon'.")
    parser.add_argument("--quiet",
                        dest="verbose",
                        default=None,
                        action="store_false",
                        help="Only error messages are printed out")
    parser.add_argument("args",
                        metavar='FILE',
                        type=str,
                        nargs='+',
                        help="Files to be processed")

    options = parser.parse_args()

    # logging
    if options.verbose is True:
        average.logger.setLevel(logging.DEBUG)
    elif options.verbose is False:
        average.logger.setLevel(logging.ERROR)
    else:
        average.logger.setLevel(logging.WARN)

    # shell output
    if options.verbose is not False:
        observer = ShellAverageObserver()
        # clean up the progress bar before displaying a log
        patch_logging_handler(observer.clear)
    else:
        observer = None

    # Analyze arguments and options
    images = pyFAI.utils.expand_args(options.args)

    if options.flat:
        flats = pyFAI.utils.expand_args([options.flat])
    else:
        flats = None

    if options.dark:
        darks = pyFAI.utils.expand_args([options.dark])
    else:
        darks = None

    algorithms = parse_algorithms(options)
    if len(algorithms) == 0:
        logger.warning("Configure process with a mean filter")
        algorithms = [average.MeanAveraging()]

    writer = parse_writer(images, options, algorithms)

    if images:
        process = average.Average()
        process.set_observer(observer)
        process.set_images(images)
        process.set_dark(darks)
        process.set_flat(flats)
        # average.set_correct_flat_from_dark(correct_flat_from_dark)
        process.set_monitor_name(options.monitor_key)
        process.set_pixel_filter(threshold=0, minimum=None, maximum=None)
        for algorithm in algorithms:
            process.add_algorithm(algorithm)
        process.set_writer(writer)
        process.process()
    else:
        logger.warning("No input file specified.")
Beispiel #15
0
def parse():
    """
    Parse command line arguments
    """
    parser = ArgumentParser(description=__doc__)
    parser.add_argument('-d', '--dir', dest='directory', default=tempfile.gettempdir(),
                        help='Destination directory (/tmp)')
    parser.add_argument('-n', '--number', dest='n', default=1024, type=int,
                        help='Number of frames to write')
    parser.add_argument('-w', '--width', dest='width', default=1024, type=int,
                        help='width of a frame (1024)')
    parser.add_argument('-H', '--height', dest='height', default=1024, type=int,
                        help='height of the image (1024)')
    parser.add_argument('-t', '--type', dest='dtype', default="float32", type=str,
                        help='data type of item (float32)')
    parser.add_argument('-b', '--bsize', dest='bsize', default=10, type=int,
                        help='size of the random buffer for frames (10)')
    opt = parser.parse_args()
    return opt
Beispiel #16
0
def main():
    usage = "pyFAI-saxs [options] -n 1000 -p ponifile file1.edf file2.edf ..."
    version = "PyFAI-saxs version %s from %s " % (pyFAI.version, pyFAI.date)
    description = """Azimuthal integration for SAXS users."""
    epilog = """pyFAI-saxs is the SAXS script of pyFAI that allows data
    reduction (azimuthal integration) for Small Angle Scattering with output
    axis in q space."""
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("args",
                        metavar="FILE",
                        type=str,
                        nargs='+',
                        help="Image files to integrate")
    parser.add_argument("-p",
                        dest="ponifile",
                        type=str,
                        default=None,
                        help="PyFAI parameter file (.poni)")
    parser.add_argument("-n",
                        "--npt",
                        dest="npt",
                        type=int,
                        default=None,
                        help="Number of points in radial dimension")
    parser.add_argument("-w",
                        "--wavelength",
                        dest="wavelength",
                        type=float,
                        help="wavelength of the X-Ray beam in Angstrom",
                        default=None)
    parser.add_argument("-e",
                        "--energy",
                        dest="energy",
                        type=float,
                        help="energy of the X-Ray beam in keV (hc=%skeV.A)" %
                        hc,
                        default=None)
    parser.add_argument("-u",
                        "--dummy",
                        dest="dummy",
                        type=float,
                        default=None,
                        help="dummy value for dead pixels")
    parser.add_argument("-U",
                        "--delta_dummy",
                        dest="delta_dummy",
                        type=float,
                        default=None,
                        help="delta dummy value")
    parser.add_argument("-m",
                        "--mask",
                        dest="mask",
                        type=str,
                        default=None,
                        help="name of the file containing the mask image")
    parser.add_argument("-d",
                        "--dark",
                        dest="dark",
                        type=str,
                        default=None,
                        help="name of the file containing the dark current")
    parser.add_argument("-f",
                        "--flat",
                        dest="flat",
                        type=str,
                        default=None,
                        help="name of the file containing the flat field")
    #    parser.add_argument("-b", "--background", dest="background",
    #                      type="string", default=None,
    #                      help="name of the file containing the background")
    parser.add_argument(
        "-P",
        "--polarization",
        dest="polarization_factor",
        type=float,
        default=None,
        help="Polarization factor, from -1 (vertical) to +1 (horizontal), \
                      default is None for no correction, synchrotrons are around 0.95"
    )
    parser.add_argument(
        "--error-model",
        dest="error_model",
        type=str,
        default=None,
        help="Error model to use. Currently on 'poisson' is implemented ")
    parser.add_argument(
        "--unit",
        dest="unit",
        type=str,
        default="q_nm^-1",
        help="unit for the radial dimension: can be q_nm^-1, q_A^-1, 2th_deg, \
                          2th_rad or r_mm")
    parser.add_argument("--ext",
                        dest="ext",
                        type=str,
                        default=".dat",
                        help="extension of the regrouped filename (.dat)")
    parser.add_argument("--method",
                        dest="method",
                        type=str,
                        default=None,
                        help="Integration method ")

    options = parser.parse_args()
    if len(options.args) < 1:
        logger.error("incorrect number of arguments")
    to_process = pyFAI.utils.expand_args(options.args)

    if options.ponifile and to_process:
        integrator = pyFAI.load(options.ponifile)

        if to_process:
            first = to_process[0]
            fabimg = fabio.open(first)
            integrator.detector.guess_binning(fabimg.data)
        if options.wavelength:
            integrator.wavelength = options.wavelength * 1e-10
        elif options.energy:
            integrator.wavelength = hc / options.energy * 1e-10
        if options.mask and os.path.exists(
                options.mask):  # override with the command line mask
            integrator.maskfile = options.mask
        if options.dark and os.path.exists(options.dark):  # set dark current
            integrator.darkcurrent = fabio.open(options.dark).data
        if options.flat and os.path.exists(options.flat):  # set Flat field
            integrator.flatfield = fabio.open(options.flat).data
        if options.method:
            method = options.method
        else:
            if len(to_process) > 5:
                method = "full_csr"
            else:
                method = "splitpixel"
        print(integrator)
        print("Mask: %s\tMethod: %s" % (integrator.maskfile, method))

        for afile in to_process:
            sys.stdout.write("Integrating %s --> " % afile)
            outfile = os.path.splitext(afile)[0] + options.ext
            t0 = time.time()
            fimg = fabio.open(afile)
            t1 = time.time()
            if fimg.nframes > 1:
                integrator.integrate1d(
                    data=fimg.data,
                    npt=options.npt or min(fimg.data.shape),
                    dummy=options.dummy,
                    delta_dummy=options.delta_dummy,
                    filename=outfile,
                    variance=fimg.next().data,
                    method=method,
                    unit=options.unit,
                    error_model=options.error_model,
                    polarization_factor=options.polarization_factor,
                    metadata=fimg.header)
            else:
                integrator.integrate1d(
                    data=fimg.data,
                    npt=options.npt or min(fimg.data.shape),
                    dummy=options.dummy,
                    delta_dummy=options.delta_dummy,
                    filename=outfile,
                    method=method,
                    unit=options.unit,
                    error_model=options.error_model,
                    polarization_factor=options.polarization_factor,
                    metadata=fimg.header)
            t2 = time.time()

            msg = "%s,\t reading: %.3fs\t 1D integration: %.3fs."
            print(msg % (outfile, t1 - t0, t2 - t1))
Beispiel #17
0
def setup(model):
    usage = "pyFAI-calib2 [options] input_image.edf"
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    version = "calibration from pyFAI  version %s: %s" % (pyFAI.version, pyFAI.date)
    parser.add_argument("-V", "--version", action='version', version=version)
    configure_parser_arguments(parser)

    # Analyse aruments and options
    options = parser.parse_args()
    args = options.args

    # Settings
    settings = model.experimentSettingsModel()
    if options.spacing:
        calibrant = None
        if options.spacing in pyFAI.calibrant.CALIBRANT_FACTORY:
            calibrant = pyFAI.calibrant.CALIBRANT_FACTORY(options.spacing)
        elif os.path.isfile(options.spacing):
            calibrant = pyFAI.calibrant.Calibrant(options.spacing)
        else:
            logger.error("No such Calibrant / d-Spacing file: %s", options.spacing)
        if calibrant:
            settings.calibrantModel().setCalibrant(calibrant)

    if options.wavelength:
        settings.wavelength().setValue(options.wavelength)

    if options.energy:
        settings.wavelength().setValue(pyFAI.units.hc / options.energy)

    if options.polarization_factor:
        settings.polarizationFactor(options.polarization_factor)

    if options.detector_name:
        detector = pyFAI.calibration.get_detector(options.detector_name, args)
        settings.detectorModel().setDetector(detector)

    if options.spline:
        settings.splineFile().setValue(options.spline)

    if options.mask:
        settings.maskFile().setValue(options.mask)
        with fabio.open(options.mask) as mask:
            settings.mask().setValue(mask.data)

    if len(args) == 0:
        pass
    elif len(args) == 1:
        image_file = args[0]
        settings.imageFile().setValue(image_file)
        with fabio.open(image_file) as image:
            settings.image().setValue(image.data)
    else:
        logger.error("Too much images provided. Only one is expected")

    # Geometry
    # FIXME it will not be used cause the fitted geometry will be overwrited
    geometry = model.fittedGeometry()
    if options.distance:
        geometry.distance().setValue(1e-3 * options.distance)
    if options.dist:
        geometry.distance().setValue(options.dist)
    if options.dist:
        geometry.poni1().setValue(options.poni1)
    if options.dist:
        geometry.poni2().setValue(options.poni2)
    if options.dist:
        geometry.rotation1().setValue(options.rot1)
    if options.dist:
        geometry.rotation2().setValue(options.rot2)
    if options.dist:
        geometry.rotation3().setValue(options.rot3)

    # Constraints
    constraints = model.geometryConstraintsModel()
    if options.fix_wavelength is not None:
        constraints.wavelength().setFixed(options.fix_wavelength)
    if options.fix_dist is not None:
        constraints.distance().setFixed(options.fix_dist)
    if options.fix_poni1 is not None:
        constraints.poni1().setFixed(options.fix_poni1)
    if options.fix_poni2 is not None:
        constraints.poni2().setFixed(options.fix_poni2)
    if options.fix_rot1 is not None:
        constraints.rotation1().setFixed(options.fix_rot1)
    if options.fix_rot2 is not None:
        constraints.rotation2().setFixed(options.fix_rot2)
    if options.fix_rot3 is not None:
        constraints.rotation3().setFixed(options.fix_rot3)

    # Integration
    if options.unit:
        unit = pyFAI.units.to_unit(options.unit)
        model.integrationSettingsModel().radialUnit().setValue(unit)

    if options.outfile:
        logger.error("outfile option not supported")
    if options.debug:
        logger.error("debug option not supported")

    if options.reconstruct:
        logger.error("reconstruct option not supported")
    if options.gaussian:
        logger.error("gaussian option not supported")
    if options.square:
        logger.error("square option not supported")
    if options.pixel:
        logger.error("pixel option not supported")

    # FIXME poni file should be supported
    if options.poni:
        logger.error("poni option not supported")
    if options.background:
        logger.error("background option not supported")
    if options.dark:
        logger.error("dark option not supported")
    if options.flat:
        logger.error("flat option not supported")
    if options.npt:
        logger.error("npt option not supported")
    if options.filter:
        logger.error("filter option not supported")

    if options.tilt:
        logger.error("tilt option not supported")
    if options.saturation:
        logger.error("saturation option not supported")
    if options.weighted:
        logger.error("weighted option not supported")
    if options.nPt_1D:
        logger.error("nPt_1D option not supported")
    if options.nPt_2D_azim:
        logger.error("nPt_2D_azim option not supported")
    if options.nPt_2D_rad:
        logger.error("nPt_2D_rad option not supported")

    if options.gui is not True:
        logger.error("gui option not supported")
    if options.interactive is not True:
        logger.error("interactive option not supported")
Beispiel #18
0
    def parse(self, *args, **kwargs):
        """
        parse options from command line
        """
        description = """Azimuthal integration for diffraction tomography.

Diffraction tomography is an experiment where 2D diffraction patterns are recorded
while performing a 2D scan, one (the slowest) in rotation around the sample center
and the other (the fastest) along a translation through the sample.
Diff_tomo is a script (based on pyFAI and h5py) which allows the reduction of this
4D dataset into a 3D dataset containing the rotations angle (hundreds), the translation step (hundreds)
and the many diffraction angles (thousands).
The resulting dataset can be opened using the PyMca ROItool
where the 1d dataset has to be selected as last dimension.
The output file aims at being NeXus compliant.

This tool can be used for mapping experiments if one considers the slow scan
direction as the rotation. but the *diff_map* tool provides in addition a graphical
user interface.
        """
        epilog = """If the number of files is too large, use double quotes "*.edf" """
        usage = """diff_tomo [options] -p ponifile imagefiles*"""
        version = "diff_tomo from pyFAI  version %s: %s" % (PyFAI_VERSION,
                                                            PyFAI_DATE)
        parser = ArgumentParser(usage=usage,
                                description=description,
                                epilog=epilog)
        parser.add_argument("-V",
                            "--version",
                            action='version',
                            version=version)
        parser.add_argument("args",
                            metavar="FILE",
                            help="List of files to calibrate",
                            nargs='+')
        parser.add_argument(
            "-o",
            "--output",
            dest="outfile",
            help=
            "HDF5 File where processed sinogram was saved, by default diff_tomo.h5",
            metavar="FILE",
            default="diff_tomo.h5")
        parser.add_argument("-v",
                            "--verbose",
                            action="store_true",
                            dest="verbose",
                            default=False,
                            help="switch to verbose/debug mode, defaut: quiet")
        parser.add_argument("-P",
                            "--prefix",
                            dest="prefix",
                            help="Prefix or common base for all files",
                            metavar="FILE",
                            default="",
                            type=str)
        parser.add_argument("-e",
                            "--extension",
                            dest="extension",
                            help="Process all files with this extension",
                            default="")
        parser.add_argument("-t",
                            "--nTrans",
                            dest="nTrans",
                            help="number of points in translation. Mandatory",
                            default=None)
        parser.add_argument("-r",
                            "--nRot",
                            dest="nRot",
                            help="number of points in rotation. Mandatory",
                            default=None)
        parser.add_argument(
            "-c",
            "--nDiff",
            dest="nDiff",
            help="number of points in diffraction powder pattern, Mandatory",
            default=None)
        parser.add_argument("-d",
                            "--dark",
                            dest="dark",
                            metavar="FILE",
                            help="list of dark images to average and subtract",
                            default=None)
        parser.add_argument("-f",
                            "--flat",
                            dest="flat",
                            metavar="FILE",
                            help="list of flat images to average and divide",
                            default=None)
        parser.add_argument(
            "-m",
            "--mask",
            dest="mask",
            metavar="FILE",
            help="file containing the mask, no mask by default",
            default=None)
        parser.add_argument(
            "-p",
            "--poni",
            dest="poni",
            metavar="FILE",
            help=
            "file containing the diffraction parameter (poni-file), Mandatory",
            default=None)
        parser.add_argument("-O",
                            "--offset",
                            dest="offset",
                            help="do not process the first files",
                            default=None)
        parser.add_argument("-g",
                            "--gpu",
                            dest="gpu",
                            action="store_true",
                            help="process using OpenCL on GPU ",
                            default=False)
        parser.add_argument("-S",
                            "--stats",
                            dest="stats",
                            action="store_true",
                            help="show statistics at the end",
                            default=False)

        options = parser.parse_args()
        args = options.args

        if options.verbose:
            logger.setLevel(logging.DEBUG)
        self.hdf5 = options.outfile
        if options.dark:
            dark_files = [
                os.path.abspath(urlparse(f).path)
                for f in options.dark.split(",")
                if os.path.isfile(urlparse(f).path)
            ]
            if dark_files:
                self.dark = dark_files
            else:
                raise RuntimeError("No such dark files")

        if options.flat:
            flat_files = [
                os.path.abspath(urlparse(f).path)
                for f in options.flat.split(",")
                if os.path.isfile(urlparse(f).path)
            ]
            if flat_files:
                self.flat = flat_files
            else:
                raise RuntimeError("No such flat files")

        self.use_gpu = options.gpu
        self.inputfiles = []
        for fn in args:
            f = urlparse(fn).path
            if os.path.isfile(f) and f.endswith(options.extension):
                self.inputfiles.append(os.path.abspath(f))
            elif os.path.isdir(f):
                self.inputfiles += [
                    os.path.abspath(os.path.join(f, g)) for g in os.listdir(f)
                    if g.endswith(options.extension)
                    and g.startswith(options.prefix)
                ]
            else:
                self.inputfiles += [os.path.abspath(f) for f in glob.glob(f)]
        self.inputfiles.sort(key=self.to_tuple)
        if not self.inputfiles:
            raise RuntimeError("No input files to process, try --help")
        if options.mask:
            mask = urlparse(options.mask).path
            if os.path.isfile(mask):
                logger.info("Reading Mask file from: %s" % mask)
                self.mask = os.path.abspath(mask)
            else:
                logger.warning("No such mask file %s" % mask)
        if options.poni:
            if os.path.isfile(options.poni):
                logger.info("Reading PONI file from: %s" % options.poni)
                self.poni = options.poni
            else:
                logger.warning("No such poni file %s" % options.poni)

        if options.nTrans is not None:
            self.npt_fast = int(options.nTrans)
        if options.nRot is not None:
            self.npt_slow = int(options.nRot)
        if options.nDiff is not None:
            self.npt_rad = int(options.nDiff)
        if options.offset is not None:
            self.offset = int(options.offset)
        else:
            self.offset = 0
        self.stats = options.stats
        return options
Beispiel #19
0
def main():
    usage = "pyFAI-integrate [options] file1.edf file2.edf ..."
    version = "pyFAI-integrate version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    PyFAI-integrate is a graphical interface (based on Python/Qt4) to perform azimuthal integration
on a set of files. It exposes most of the important options available within pyFAI and allows you
to select a GPU (or an openCL platform) to perform the calculation on."""
    epilog = """PyFAI-integrate saves all parameters in a .azimint.json (hidden) file. This JSON file
is an ascii file which can be edited and used to configure online data analysis using
the LImA plugin of pyFAI.

Nota: there is bug in debian6 making the GUI crash (to be fixed inside pyqt)
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=697348"""
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument("-v", "--verbose",
                        action="store_true", dest="verbose", default=False,
                        help="switch to verbose/debug mode")
    parser.add_argument("-o", "--output",
                        dest="output", default=None,
                        help="Directory or file where to store the output data")
    parser.add_argument("-f", "--format",
                        dest="format", default=None,
                        help="output data format (can be HDF5)")
    parser.add_argument("-s", "--slow-motor",
                        dest="slow", default=None,
                        help="Dimension of the scan on the slow direction (makes sense only with HDF5)")
    parser.add_argument("-r", "--fast-motor",
                        dest="rapid", default=None,
                        help="Dimension of the scan on the fast direction (makes sense only with HDF5)")
    parser.add_argument("--no-gui",
                        dest="gui", default=True, action="store_false",
                        help="Process the dataset without showing the user interface.")
    parser.add_argument("-j", "--json",
                        dest="json", default=".azimint.json",
                        help="Configuration file containing the processing to be done")
    parser.add_argument("args", metavar='FILE', type=str, nargs='*',
                        help="Files to be integrated")
    parser.add_argument("--monitor-name", dest="monitor_key", default=None,
                        help="Name of the monitor in the header of each input \
                        files. If defined the contribution of each input file \
                        is divided by the monitor. If the header does not \
                        contain or contains a wrong value, the contribution \
                        of the input file is ignored.\
                        On EDF files, values from 'counter_pos' can accessed \
                        by using the expected mnemonic. \
                        For example 'counter/bmon'.")
    options = parser.parse_args()

    # Analysis arguments and options
    args = pyFAI.utils.expand_args(options.args)

    if options.verbose:
        logger.info("setLevel: debug")
        logger.setLevel(logging.DEBUG)

    if options.gui:
        result = integrate_gui(options, args)
    else:
        result = integrate_shell(options, args)
    sys.exit(result)
Beispiel #20
0
def main():
    usage = "detector2nexus [options] [options] -o nxs.h5"
    version = "detector2nexus version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    Convert a complex detector definition (multiple modules, possibly in 3D)
    into a single NeXus detector definition together with the mask (and much more in the future)
    """
    epilog = """
    This summarizes detector2nexus
    """
    parser = ArgumentParser(usage=usage,
                            description=description,
                            epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument("-o",
                        "--output",
                        dest="output",
                        type=str,
                        default=None,
                        help="Output nexus file, unless detector_name.h5")
    parser.add_argument("-n",
                        "--name",
                        dest="name",
                        type=str,
                        default=None,
                        help="name of the detector")
    parser.add_argument("-m",
                        "--mask",
                        dest="mask",
                        type=str,
                        default=None,
                        help="mask corresponding to the detector")
    parser.add_argument(
        "-D",
        "--detector",
        dest="detector",
        type=str,
        default="Detector",
        help="Base detector name (see documentation of pyFAI.detectors")
    parser.add_argument("-s",
                        "--splinefile",
                        dest="splinefile",
                        type=str,
                        default=None,
                        help="Geometric distortion file from FIT2D")
    parser.add_argument("--dx",
                        "--x-corr",
                        dest="dx",
                        type=str,
                        default=None,
                        help="Geometric correction for pilatus")
    parser.add_argument("--dy",
                        "--y-corr",
                        dest="dy",
                        type=str,
                        default=None,
                        help="Geometric correction for pilatus")
    parser.add_argument("-p",
                        "--pixel",
                        dest="pixel",
                        type=str,
                        default=None,
                        help="pixel size (comma separated): x,y")
    parser.add_argument("-S",
                        "--shape",
                        dest="shape",
                        type=str,
                        default=None,
                        help="shape of the detector (comma separated): x,y")
    parser.add_argument("-d",
                        "--dark",
                        dest="dark",
                        type=str,
                        default=None,
                        help="Dark noise to be subtracted")
    parser.add_argument("-f",
                        "--flat",
                        dest="flat",
                        type=str,
                        default=None,
                        help="Flat field correction")
    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        dest="verbose",
                        default=False,
                        help="switch to verbose/debug mode")
    # parser.add_argument("args", metavar='FILE', type=str, nargs='+',
    #                     help="Files to be processed")

    argv = sys.argv
    # hidden backward compatibility for -dx and -dy
    # A short option only expect a single char
    argv = ["-" + a if a.startswith("-dx") else a for a in argv]
    argv = ["-" + a if a.startswith("-dy") else a for a in argv]
    print(argv)
    options = parser.parse_args(args=argv[1:])

    if options.verbose:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    detector = pyFAI.detectors.detector_factory(options.detector)
    if options.output:
        output = options.output
    else:
        output = detector.name + ".h5"

    if options.mask:
        mask = fabio.open(options.mask).data.astype(bool)
        if detector.mask is None:
            detector.mask = mask
        else:
            detector.mask = numpy.logical_or(mask, detector.mask)

    if options.flat:
        detector.flat = fabio.open(options.flat).data
    if options.dark:
        detector.dark = fabio.open(options.dark).data
    if options.splinefile:
        detector.set_splineFile(options.splinefile)
    else:
        if options.pixel:
            p = options.pixel.split(",")
            psize = float(p[0])
            if len(p) == 1:
                detector.pixel1 = psize
                detector.pixel2 = psize
            else:
                detector.pixel1 = float(p[1])
                detector.pixel2 = psize
        if options.shape:
            p = options.shape.split(",")
            psize = int(p[0])
            if len(p) == 1:
                detector.shape = psize, psize
            else:
                detector.shape = int(p[1]), psize
        if options.dx and options.dy:
            dx = fabio.open(options.dx).data
            dy = fabio.open(options.dy).data
            # pilatus give displaceemt in percent of pixel ...
            if ".cbf" in options.dx:
                dx *= 0.01
            if ".cbf" in options.dy:
                dy *= 0.01
            detector.set_dx(dx)
            detector.set_dy(dy)
    detector.save(output)
Beispiel #21
0
def main():
    from pyFAI.third_party.argparse import ArgumentParser
    description = """Benchmark for Azimuthal integration
    """
    epilog = """  """
    usage = """benchmark [options] """
    version = "pyFAI benchmark version " + pyFAI.version
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-v", "--version", action='version', version=version)
    parser.add_argument("-d", "--debug",
                        action="store_true", dest="debug", default=False,
                        help="switch to verbose/debug mode")
    parser.add_argument("-c", "--cpu",
                        action="store_true", dest="opencl_cpu", default=False,
                        help="perform benchmark using OpenCL on the CPU")
    parser.add_argument("-g", "--gpu",
                        action="store_true", dest="opencl_gpu", default=False,
                        help="perform benchmark using OpenCL on the GPU")
    parser.add_argument("-a", "--acc",
                        action="store_true", dest="opencl_acc", default=False,
                        help="perform benchmark using OpenCL on the Accelerator (like XeonPhi/MIC)")
    parser.add_argument("-s", "--size", type=float,
                        dest="size", default=1000,
                        help="Limit the size of the dataset to X Mpixel images (for computer with limited memory)")
    parser.add_argument("-n", "--number",
                        dest="number", default=10, type=int,
                        help="Number of repetition of the test, by default 10")
    parser.add_argument("-2d", "--2dimention",
                        action="store_true", dest="twodim", default=False,
                        help="Benchmark also algorithm for 2D-regrouping")
    parser.add_argument("--no-1dimention",
                        action="store_false", dest="onedim", default=True,
                        help="Do not benchmark algorithms for 1D-regrouping")

    parser.add_argument("-m", "--memprof",
                        action="store_true", dest="memprof", default=False,
                        help="Perfrom memory profiling (Linux only)")
    parser.add_argument("-r", "--repeat",
                        dest="repeat", default=1, type=int,
                        help="Repeat each benchmark x times to take the best")

    options = parser.parse_args()
    if options.debug:
            pyFAI.logger.setLevel(logging.DEBUG)
    devices = ""
    if options.opencl_cpu:
        devices += "cpu,"
    if options.opencl_gpu:
        devices += "gpu,"
    if options.opencl_acc:
        devices += "acc,"

    pyFAI.benchmark.run(number=options.number,
                        repeat=options.repeat,
                        memprof=options.memprof,
                        max_size=options.size,
                        do_1d=options.onedim,
                        do_2d=options.twodim,
                        devices=devices)

    pyFAI.benchmark.pylab.ion()
    six.moves.input("Enter to quit")
Beispiel #22
0
def main():
    usage = "pyFAI-average [options] [options] -o output.edf file1.edf file2.edf ..."
    version = "pyFAI-average version %s from %s" % (pyFAI.version, pyFAI.date)
    description = """
    This tool can be used to average out a set of dark current images using
    mean or median filter (along the image stack). One can also reject outliers
    be specifying a cutoff (remove cosmic rays / zingers from dark)
    """
    epilog = """It can also be used to merge many images from the same sample when using a small beam
    and reduce the spotty-ness of Debye-Sherrer rings. In this case the "max-filter" is usually
    recommended.
    """
    parser = ArgumentParser(usage=usage, description=description, epilog=epilog)
    parser.add_argument("-V", "--version", action='version', version=version)
    parser.add_argument("-o", "--output", dest="output",
                        type=str, default=None,
                        help="Output/ destination of average image")
    parser.add_argument("-m", "--method", dest="method",
                        type=str, default="",
                        help="Method used for averaging, can be 'mean' \
                        (default) or 'min', 'max', 'median', 'sum', 'quantiles'\
                        , 'cutoff', 'std'. Multiple filters can be defined with \
                        ',' separator.")
    parser.add_argument("-c", "--cutoff", dest="cutoff", type=float, default=None,
                        help="Take the mean of the average +/- cutoff * std_dev.")
    parser.add_argument("-F", "--format", dest="format", type=str, default="edf",
                        help="Output file/image format (by default EDF)")
    parser.add_argument("-d", "--dark", dest="dark", type=str, default=None,
                        help="Dark noise to be subtracted")
    parser.add_argument("-f", "--flat", dest="flat", type=str, default=None,
                        help="Flat field correction")
    parser.add_argument("-v", "--verbose", action="store_true", dest="verbose", default=None,
                        help="switch to verbose/debug mode")
    parser.add_argument("-q", "--quantiles", dest="quantiles", default=None,
                        help="average out between two quantiles -q 0.20-0.90")
    parser.add_argument("--monitor-name", dest="monitor_key", default=None,
                        help="Name of the monitor in the header of each input \
                        files. If defined the contribution of each input file \
                        is divided by the monitor. If the header does not \
                        contain or contains a wrong value, the contribution of \
                        the input file is ignored.\
                        On EDF files, values from 'counter_pos' can accessed by \
                        using the expected mnemonic. \
                        For example 'counter/bmon'.")
    parser.add_argument("--quiet", dest="verbose", default=None, action="store_false",
                        help="Only error messages are printed out")
    parser.add_argument("args", metavar='FILE', type=str, nargs='+',
                        help="Files to be processed")

    options = parser.parse_args()

    # logging
    if options.verbose is True:
        average.logger.setLevel(logging.DEBUG)
    elif options.verbose is False:
        average.logger.setLevel(logging.ERROR)
    else:
        average.logger.setLevel(logging.WARN)

    # shell output
    if options.verbose is not False:
        observer = ShellAverageObserver()
        # clean up the progress bar before displaying a log
        patch_logging_handler(observer.clear)
    else:
        observer = None

    # Analyze arguments and options
    images = pyFAI.utils.expand_args(options.args)

    if options.flat:
        flats = pyFAI.utils.expand_args([options.flat])
    else:
        flats = None

    if options.dark:
        darks = pyFAI.utils.expand_args([options.dark])
    else:
        darks = None

    algorithms = parse_algorithms(options)
    if len(algorithms) == 0:
        logger.warning("Configure process with a mean filter")
        algorithms = [average.MeanAveraging()]

    writer = parse_writer(images, options, algorithms)

    if images:
        process = average.Average()
        process.set_observer(observer)
        process.set_images(images)
        process.set_dark(darks)
        process.set_flat(flats)
        # average.set_correct_flat_from_dark(correct_flat_from_dark)
        process.set_monitor_name(options.monitor_key)
        process.set_pixel_filter(threshold=0, minimum=None, maximum=None)
        for algorithm in algorithms:
            process.add_algorithm(algorithm)
        process.set_writer(writer)
        process.process()
    else:
        logger.warning("No input file specified.")