Ejemplo n.º 1
0
def get_options(parser):
    """
    Command line interface for making a mapping
    Add our options to a parser object
    """
    parser = ImageD11_file_series.get_options(parser)
    parser.add_option("--lookup",
                      action="store",
                      type="string",
                      dest="lookup",
                      default="None",
                      help="Lookup tables stem name")
    parser.add_option("--outdir",
                      action="store",
                      type="string",
                      dest="outdir",
                      default=None,
                      help="Output directory")
    parser.add_option("--mask",
                      action="store",
                      type="string",
                      dest="mask",
                      default=None,
                      help="fit2d mask file")

    return parser
Ejemplo n.º 2
0
def get_options(parser):
    """
    Command line interface for making a mapping
    Add our options to a parser object
    """
    parser = ImageD11_file_series.get_options( parser )

    parser.add_argument("-p", "--pars", action="store",
                      dest = "pars", default = None,
                      type=ImageD11options.ParameterFileType(mode='r'),
                      help = "ImageD11 parameter file for experiment")
    
    parser.add_argument("-o", "--output", action="store",
                      dest = "output", default = None,
                      type=ImageD11options.HdfFileType(mode='r'),
                      help = "Name of hdf5 output file")

    parser.add_argument("-s", "--splinefile", action="store", 
                      dest = "spline", default = None,
                      type=ImageD11options.SplineFileType(mode='r'),
                      help = "Name of fit2d spline file for spatial dist")

    parser.add_argument("-u", "--ubifile", action="store", 
                      dest = "ubifile", default = None,
                      type = ImageD11options.UbiFileType(mode='r'),
                      help = "Name of ubi file (first matrix is used)")

    parser.add_argument("-x", "--npixels", action="store", type=int,
                      dest = "npixels", default = 16,
      help = "Number of pixels in reciprocal space map per integer hkl [16]")

    parser.add_argument("-i", "--images", action="store", type=int,
                      dest = "images", default = None,
                      help = "Number of images to process [all]")

    parser.add_argument("-b", "--border", action="store", type=int,
                       dest = "border", default = 10,
                       help = "Border around images to allocate space, px [10]")
    parser.add_argument("-t", "--saturation", action="store", type=float,
                      dest = "maxpix", default = None,
                      help = "Saturation value for excluding pixels")


    #parser.add_argument("-t", "--testcolfile", action="store", type="string",
    #                  dest = "testcolfile", default=None,
    #                  help = "A columnfile to test geometry")

    parser.add_argument("-c", "--subslice", action="store", type=int,
                      dest = "subslice", default=1,
                      help = "Number of omega subslices to repeat images")

    parser.add_argument("--maskfilename", action="store", type=str,
                      dest = "maskfilename", default=None,
                      help = "Mask image (fit2d style)" )
    
    return parser
Ejemplo n.º 3
0
def main():
    """
    A user interface
    """
    import sys, time, os, logging
    start = time.time()

    root = logging.getLogger('')
    root.setLevel(logging.WARNING)

    try:
        from optparse import OptionParser
        parser = OptionParser()
        parser = get_options(parser)
        options, args = parser.parse_args()
    except SystemExit:
        raise
    except:
        parser.print_help()
        print("\nProblem with your options:")
        raise

    if options.output is None:
        print("You must supply an output file (-o vol.h5)")
        sys.exit()

    if os.path.exists(options.output):
        print("I would overwrite your output file", options.output)
        print("If you really want that then delete it first and re-run")
        sys.exit()

    try:
        if options.pars is None:
            print("You must supply a parameter file, -p file.pars")
            sys.exit()
        pars = parameters.parameters()
        pars.loadparameters(options.pars)
        print("Got parameters from", options.pars)
        pd = pars.get_parameters()
        names = list(pd.keys())
        names.sort()
        for name in names:
            print("%30s   %s" % (name, pd[name]))
    except:
        print("Problem with parameters:", options.pars)
        raise

    try:
        if options.ubifile is None:
            print("You must supply an input ubifile")
        ubi = indexing.readubis(options.ubifile)[0]
        print("UBI:\n", ubi)
        print("Cell parameters:")
        print("%.5f %.5f %.5f %.4f %.4f %.4f" % \
              indexing.ubitocellpars(ubi))
    except:
        print("Problem with ubi file:", options.ubifile)
        raise

    if options.maskfilename is not None:
        from fabio.openimage import openimage
        try:
            mask = (openimage(options.maskfilename).data == 0)
        except:
            print("Problem with your mask image", options.maskfilename)
            raise
        print("Using a mask from", options.maskfilename)
        print("percent of image used %.3f" %
              (100.0 * mask.sum() / mask.shape[0] / mask.shape[1]))
    else:
        mask = None

    first_image = True
    nimage = 0

    imagefiles = ImageD11_file_series.get_series_from_options(options, args)

    print("Subslicing by", options.subslice)

    try:
        for fim in imagefiles:

            if first_image:  # allocate volume, compute k etc

                first_image = False

                mapper = rsv_mapper(fim.data.shape,
                                    pars,
                                    ubi,
                                    options.spline,
                                    np=options.npixels,
                                    border=options.border,
                                    maxpix=options.maxpix,
                                    mask=mask
                                    # FIXME omegarange
                                    )

                logging.info("Setting up time %.4f s" % (time.time() - start))

            ltp = time.time()
            om = float(fim.header['Omega'])
            oms = float(fim.header['OmegaStep'])
            for i in range(options.subslice):
                print(".", end=' ')
                omv = om + i * oms / options.subslice
                # ==1 : 0*s/1
                # ==2 : 0*s/2 , 1*s/2
                # ==3 : 0*s/3 , 1*s/3, 2*s/3 etc
                mapper.add_image(omv, fim.data)

            nimage = nimage + 1

            print("  %d %.3f %.4f s, %.4f s" %
                  (nimage, om, time.time() - ltp, time.time() - start))
            if options.images is not None:
                if nimage >= options.images:
                    break

    except KeyboardInterrupt:
        print("\nCaught a control-c")
        if nimage > 0:
            print("Problem, trying to save volume so far to:", options.output)
            mapper.writevol(options.output)
            print("Saved what I had")
        sys.exit()
    except:
        print("\nAn error occured")
        if nimage > 0:
            print("Problem, trying to save volume so far to:", options.output)
            mapper.writevol(options.output)
            print("Saved what I had")
        raise

    if nimage > 0:
        mapper.writevol(options.output)
Ejemplo n.º 4
0
def main():
    """
    A CLI user interface
    """
    import sys, time, os, logging
    start = time.time()

    root = logging.getLogger('')
    root.setLevel(logging.WARNING)

    try:
        from optparse import OptionParser
        parser = OptionParser()
        parser = get_options(parser)
        options, args = parser.parse_args()
    except SystemExit:
        raise
    except:
        parser.print_help()
        print("\nProblem with your options:")
        raise

    if options.mask is not None:
        fit2dmask = (1 - openimage(options.mask).data).ravel()
    else:
        fit2dmask = 1.0

    first_image = True

    imagefiles = ImageD11_file_series.get_series_from_options(options, args)

    tthvals = numpy.load(options.lookup + "_tth.npy")

    try:
        for fim in imagefiles:
            dataim = fim.data
            print(fim.filename)
            if first_image:  # allocate volume, compute k etc

                first_image = False

                dxim = openimage(options.lookup + "_dx.edf").data
                dyim = openimage(options.lookup + "_dy.edf").data

                outsum = numpy.ravel(numpy.zeros(dataim.shape, numpy.float32))
                outnp = numpy.ravel(numpy.zeros(dataim.shape, numpy.float32))

                e = edfimage()

                # C code from rsv_mapper (not intended to be obfuscated)
                o = blobcorrector.perfect()
                idealx, idealy = o.make_pixel_lut(dataim.shape)

                destx, desty = idealx + dxim, idealy + dyim

                assert destx.min() >= 0
                assert destx.max() < dataim.shape[1]
                assert desty.min() >= 0
                assert desty.max() < dataim.shape[1]

                imageshape = dataim.shape

                indices = numpy.ravel(destx).astype(numpy.intp)
                numpy.multiply(indices, dataim.shape[1], indices)
                numpy.add(indices,
                          numpy.ravel(desty).astype(numpy.intp), indices)

                assert indices.min() >= 0
                assert indices.max() < dataim.shape[0] * dataim.shape[1]
                on = numpy.ones(len(outnp), numpy.float32)
                if fit2dmask is not None:
                    on = on * fit2dmask

                # Number of pixels and mask are constant
                cImageD11.put_incr(outnp, indices, on)

                mask = outnp < 0.1
                scalar = (1.0 - mask) / (outnp + mask)

                flatshape = outsum.shape

                #

                arsorted = mask.copy()
                outmask = mask.copy()
                outmask = outmask * 1e6
                outmask.shape = imageshape
                arsorted.shape = imageshape
                arsorted.sort(axis=1)
                minds = numpy.array([l.searchsorted(0.5) for l in arsorted])

            # ENDIF firstimage

            start = time.time()

            numpy.multiply(outsum, 0, outsum)
            outsum.shape = flatshape

            dm = (dataim.ravel() * fit2dmask).astype(numpy.float32)

            cImageD11.put_incr(outsum, indices, dm)

            # outsum = outsum.reshape( dataim.shape )
            # outnp = outnp.reshape( dataim.shape ).astype(numpy.int32)
            # Normalise
            numpy.multiply(outsum, scalar, outsum)
            print(dataim.max(), dataim.min(), end=' ')
            print(scalar.max(), scalar.min(), outsum.min(), outsum.max())

            outsum.shape = imageshape
            # saving edf
            e.data = outsum
            e.write("r_" + fim.filename, force_type=numpy.float32)

            print(time.time() - start)

    except:
        raise