Esempio n. 1
0
def test_main_fit2d_mask(fast_tmpdir):
    poni_file = pyfai_poni
    # Copy the image file to the temp dir
    msk = np.random.randint(0, 2, 2048 * 2048, dtype=bool).reshape(
        (2048, 2048)
    )
    fit2d_save(msk, "mask_test", str(fast_tmpdir))
    dest_image_file = str(os.path.join(fast_tmpdir, "test.tiff"))
    shutil.copy(image_file, dest_image_file)
    print(dest_image_file)
    # Copy the poni and image files to the temp dir
    no_output_main(
        poni_file,
        dest_image_file,
        edge=None,
        lower_thresh=None,
        alpha=None,
        mask_file=os.path.join(str(fast_tmpdir), "mask_test.msk"),
        flip_input_mask=True,
    )
    files = os.listdir(str(fast_tmpdir))
    for ext in expected_outputs:
        assert "test" + ext in files
    msk2 = read_fit2d_msk(os.path.join(str(fast_tmpdir), "test.msk"))
    assert_equal(msk, msk2)
Esempio n. 2
0
def test_save_output_fit2d():
    filename = "function_values"
    msk = np.random.random_integers(
        0, 1, (np.random.random_integers(0, 200),
               np.random.random_integers(0, 200))).astype(bool)

    fit2d_save(msk, filename, dir_path=None)
    msk2 = read_fit2d_msk(filename)
    assert_array_equal(msk2, msk)

    os.remove("function_values.msk")
Esempio n. 3
0
def test_save_output_fit2d(tmpdir):
    t = tmpdir.join('function_values')
    dir_path = t.dirname
    filename = t.strpath
    msk = np.random.randint(
        0, 2,
        (np.random.randint(0, 201), np.random.randint(0, 201))).astype(bool)

    fit2d_save(msk, filename, dir_path=dir_path)
    msk2 = read_fit2d_msk('%s.msk' % filename)
    assert_array_equal(msk2, msk)
Esempio n. 4
0
def main(poni_file=None,
         image_files=None,
         bg_file=None,
         mask_file=None,
         polarization=.99,
         edge=20,
         lower_thresh=1.,
         upper_thresh=None,
         alpha=3.,
         auto_type='median',
         mask_settings='auto',
         flip_input_mask=True):
    """Run the data processing protocol taking raw images to background
    subtracted I(Q) files.

    The data processing steps included in this protocol are:
    background subtraction, polarization correction, automated masking, and
    pixel resolution integration

    Parameters
    ----------
    poni_file: str or None, optional
        File generated from pyFAI's calibration, if None look in the current
        working directory for the poni file, defaults to None.
    image_files: str or None, optional
        File to process, if None use all the valid files in the directory,
        defaults to None.
    bg_file: str or None, optional
        Background image, if None no background subtraction is performed,
        defaults to None.
    mask_file: str or None, optional
        Mask file to include in the data processing, if None don't use one,
        defaults to None.
    polarization: float, optional
        The polzarization factor to use, defaults to .99, if None do not
        perform polarization correction
    edge: int, optional
        The number of pixels from the edge to mask with an edge mask, defaults
        to 20, if None no edge mask used
    lower_thresh: float, optional
        Threshold for lower threshold mask, all pixels with value less than
        this value (after background subtraction if applicable), defaults to 1.
        if None do not apply lower theshold mask
    upper_thresh: float, optional
        Threshold for upper threshold mask, all pixels with value greater than
        this value (after background subtraction if applicable), defaults to
        None if None do not apply upper theshold mask
    alpha: float, optional
        Number of standard deviations away from the ring mean to mask, defaults
        to 3. if None do not apply automated masking
    auto_type : {'median', 'mean'}, optional
        The type of automasking to use, median is faster, mean is more
        accurate. Defaults to 'median'.
    mask_settings: {'auto', 'first', None}, optional
        If auto mask every image, if first only mask first image, if None
        mask no images. Defaults to None
    flip_input_mask: bool, optional
        If True flip the input mask up down, this helps when using fit2d
        defaults to True.

    Returns
    -------
    q_l : list of ndarrays
        The list of q values
    mean_l : list of ndarrays
        The list of mean values
    median_l : list of ndarrays
        The list of median values
    std_l : list of ndarrays
        The list of standard deviation values
    """
    polarization_array.args = (polarization, )
    if mask_file:
        if mask_file.endswith('.msk'):
            # TODO: may need to flip this?
            tmsk = read_fit2d_msk(mask_file)
        else:
            tmsk = np.load(mask_file)
        if flip_input_mask:
            tmsk = np.flipud(tmsk)
    else:
        tmsk = None

    mask.kwargs.update(tmsk=tmsk,
                       edge=edge,
                       lower_thresh=lower_thresh,
                       upper_thresh=upper_thresh,
                       alpha=alpha,
                       auto_type=auto_type)
    print(mask.kwargs)
    # Load calibration
    if poni_file is None:
        poni_file = [f for f in os.listdir('.') if f.endswith('.poni')]
        if len(poni_file) != 1:
            raise RuntimeError("There can only be one poni file")
        else:
            poni_file = poni_file[0]
    geo = pyFAI.load(poni_file)

    bg = None
    img_filenames = None

    if image_files is None:
        img_filenames = [
            i for i in os.listdir('.')
            if os.path.splitext(i)[-1] in img_extensions
        ]
        # TODO: Test non tiff files
        if all(
            [f.endswith('.tiff') or f.endswith('.tif')
             for f in img_filenames]):
            imgs = (tifffile.imread(i) for i in img_filenames)
        else:
            imgs = (fabio.open(i).data.astype(float) for i in os.listdir('.')
                    if os.path.splitext(i)[-1] in img_extensions)
    else:
        if isinstance(image_files, str):
            image_files = (image_files, )
            img_filenames = image_files
        imgs = (fabio.open(i).data.astype(float) for i in image_files)

    if bg_file is not None:
        bg = fabio.open(bg_file).data.astype(float)

    for k in out_tup:
        k.clear()

    geometry.emit(geo)

    for i, (fn, img) in enumerate(zip(img_filenames, imgs)):
        filename_source.emit(fn)
        if bg is None:
            bg = np.zeros(img.shape)
        dark_corrected_background.emit(bg)
        dark_corrected_foreground.emit(img)

    return tuple([tuple(x) for x in out_tup])