Esempio n. 1
0
def process(
    *,
    raw_img: np.ndarray,
    ai: AzimuthalIntegrator,
    dk_img: np.ndarray = None,
    dk_sub_bg_img: np.ndarray = None,
    integ_setting: dict = None,
    mask_setting: dict = None,
    pdfgetx_setting: dict = None,
) -> dict:
    """The function to process the data from event."""
    data = dict()
    # dark subtraction
    if dk_img is None:
        dk_img = np.zeros_like(raw_img)
    dk_sub_img = np.subtract(raw_img, dk_img)
    data.update({"dk_sub_image": dk_sub_img})
    # background subtraction
    if dk_sub_bg_img is None:
        dk_sub_bg_img = np.zeros_like(dk_sub_img)
    bg_sub_img = np.subtract(dk_sub_img, dk_sub_bg_img)
    data.update({"bg_sub_image": bg_sub_img})
    # auto masking
    mask, _ = integ.auto_mask(bg_sub_img, ai, mask_setting=mask_setting)
    data.update({"mask": mask})
    # integration
    x, y = ai.integrate1d(bg_sub_img, mask=mask, **integ_setting)
    chi_max_ind = np.argmax(y)
    data.update({
        "chi_Q": x,
        "chi_I": y,
        "chi_max": y[chi_max_ind],
        "chi_argmax": x[chi_max_ind]
    })
    # transformation
    pdfconfig = PDFConfig(dataformat="QA", **pdfgetx_setting)
    pdfgetter = PDFGetter(pdfconfig)
    pdfgetter(x, y)
    iq, sq, fq, gr = pdfgetter.iq, pdfgetter.sq, pdfgetter.fq, pdfgetter.gr
    gr_max_ind = np.argmax(gr[1])
    data.update({
        "iq_Q": iq[0],
        "iq_I": iq[1],
        "sq_Q": sq[0],
        "sq_S": sq[1],
        "fq_Q": fq[0],
        "fq_F": fq[1],
        "gr_r": gr[0],
        "gr_G": gr[1],
        "gr_max": gr[1][gr_max_ind],
        "gr_argmax": gr[0][gr_max_ind]
    })
    return data
Esempio n. 2
0
def get_chi(
    ai: AzimuthalIntegrator,
    img: ndarray,
    dk_img: ndarray = None,
    bg_img: ndarray = None,
    mask: ndarray = None,
    bg_scale: float = None,
    mask_setting: tp.Union[str, dict] = None,
    integ_setting: dict = None,
    img_setting: tp.Union[str, dict] = None,
    plot_setting: tp.Union[str, dict] = None,
) -> tp.Tuple[ndarray, ndarray, ndarray, ndarray, tp.Union[None, ndarray],
              dict, tp.Union[str, dict]]:
    """Process the diffraction image to get I(Q).

    The image will be subtracted by the background image and then auto masked. The results will be
    binned on the azimuthal direction and the average value of the intensity in the bin and their
    corresponding Q will be returned. The I(Q) and background subtracted masked image will
    be visualized.

    Parameters
    ----------
    ai : AzimuthalIntegrator
        The AzimuthalIntegrator.

    img : ndarray
        The of the 2D array of the image.

    dk_img : ndarray
        The dark frame image. The image will be subtracted by it if provided.

    bg_img : ndarray
        The 2D array of the background image. If None, no background subtraction.

    mask : ndarray
        A mask provided by user. The auto generated mask will be multiplied by this mask.

    bg_scale : float
        The scale for background subtraction. If None, use 1.

    mask_setting : dict
        The auto final_mask setting.
        If None, use _AUTOMASK_SETTING in the tools module. To turn off the auto masking, use "OFF".

    integ_setting : dict
        The integration setting.
        If None, use _INTEG_SETTING in the tools module.

    img_setting : dict
        The user's modification to imshow kwargs except a special key 'z_score'. If None, use use empty dict.
        To turn off the imshow, use "OFF".

    plot_setting : dict
        The kwargs for the plot function. If None, use empty dict.

    Returns
    -------
    chi : ndarray
        The 2D array of integrated results. The first row is the Q and the second row is the I.

    bg_sub_img : ndarray
        The background subtracted image. If no background subtraction, it is the dk_sub_image.

    dk_sub_img : ndarray
        The dark subtracted image. If no dark subtraction, it is the input img.

    img : ndarray
        The input image.

    final_mask : ndarray or None
        The final_mask array. If no auto_masking, return None.

    _integ_setting: dict
        The integration setting used.

    _mask_setting : dict or str
        The auto masking setting.
    """
    if dk_img is not None:
        dk_sub_img = bg_sub(img, dk_img, bg_scale=1.)
    else:
        dk_sub_img = img
    if bg_img is not None:
        bg_sub_img = bg_sub(dk_sub_img, bg_img, bg_scale=bg_scale)
    else:
        bg_sub_img = dk_sub_img
    if mask_setting != "OFF":
        final_mask, _mask_setting = auto_mask(bg_sub_img,
                                              ai,
                                              user_mask=mask,
                                              mask_setting=mask_setting)
    elif mask is not None:
        final_mask, _mask_setting = mask, {}
    else:
        final_mask, _mask_setting = None, None
    if img_setting != "OFF":
        vis_img(bg_sub_img, final_mask, img_setting=img_setting)
    chi, _integ_setting = integrate(bg_sub_img,
                                    ai,
                                    mask=final_mask,
                                    integ_setting=integ_setting)
    if plot_setting != "OFF":
        vis_chi(chi,
                plot_setting=plot_setting,
                unit=_integ_setting.get('unit'))
    return chi, bg_sub_img, dk_sub_img, img, final_mask, _integ_setting, _mask_setting
Esempio n. 3
0
def process(
    *,
    user_config: UserConfig,
    raw_img: np.ndarray,
    ai: tp.Union[None, AzimuthalIntegrator],
    dk_img: np.ndarray = None,
    dk_sub_bg_img: np.ndarray = None,
    integ_setting: dict = None,
    mask_setting: dict = None,
    pdfgetx_setting: dict = None,
) -> dict:
    """The function to process the data from event."""
    # initialize the data dictionary
    data = {
        "dk_sub_image": raw_img.copy(),
        "bg_sub_image": raw_img.copy(),
        "mask": np.zeros_like(raw_img),
        "chi_Q": np.array([0.]),
        "chi_I": np.array([0.]),
        "chi_max": 0.,
        "chi_argmax": 0.,
        "iq_Q": np.array([0.]),
        "iq_I": np.array([0.]),
        "sq_Q": np.array([0.]),
        "sq_S": np.array([0.]),
        "fq_Q": np.array([0.]),
        "fq_F": np.array([0.]),
        "gr_r": np.array([0.]),
        "gr_G": np.array([0.]),
        "gr_max": 0.,
        "gr_argmax": 0.
    }
    # dark subtraction
    if dk_img is not None:
        data["dk_sub_image"] = np.subtract(raw_img, dk_img)
    # background subtraction
    if dk_sub_bg_img is not None:
        data["bg_sub_image"] = np.subtract(data["dk_sub_image"], dk_sub_bg_img)
    # if no calibration, output data now
    if ai is None:
        return data
    # do auto masking if specified
    if user_config.do_auto_masking:
        data["mask"], _ = integ.auto_mask(data["bg_sub_image"],
                                          ai,
                                          mask_setting=mask_setting,
                                          user_mask=user_config.user_mask)
    # if user gives a mask, use it
    elif user_config.user_mask is not None:
        data["mask"] = user_config.user_mask.copy()
    # integration
    x, y = ai.integrate1d(data["bg_sub_image"],
                          mask=data["mask"],
                          **integ_setting)
    chi_max_ind = np.argmax(y)
    data.update({
        "chi_Q": x,
        "chi_I": y,
        "chi_max": y[chi_max_ind],
        "chi_argmax": x[chi_max_ind]
    })
    # transformation
    pdfconfig = PDFConfig(dataformat="QA", **pdfgetx_setting)
    pdfgetter = PDFGetter(pdfconfig)
    pdfgetter(x, y)
    iq, sq, fq, gr = pdfgetter.iq, pdfgetter.sq, pdfgetter.fq, pdfgetter.gr
    gr_max_ind = np.argmax(gr[1])
    data.update({
        "iq_Q": iq[0],
        "iq_I": iq[1],
        "sq_Q": sq[0],
        "sq_S": sq[1],
        "fq_Q": fq[0],
        "fq_F": fq[1],
        "gr_r": gr[0],
        "gr_G": gr[1],
        "gr_max": gr[1][gr_max_ind],
        "gr_argmax": gr[0][gr_max_ind]
    })
    return data
Esempio n. 4
0
def process(
    *,
    raw_img: np.ndarray,
    ai: tp.Union[None, AzimuthalIntegrator],
    user_mask: np.ndarray = None,
    auto_mask: bool = True,
    dk_img: np.ndarray = None,
    integ_setting: dict = None,
    mask_setting: dict = None,
    pdfgetx_setting: dict = None,
) -> dict:
    """The function to process the data from event."""
    # initialize the data dictionary
    data = {
        "dk_sub_image": raw_img,
        "mask": np.zeros_like(raw_img),
        "chi_Q": np.array([0.]),
        "chi_I": np.array([0.]),
        "chi_max": np.float(0.),
        "chi_argmax": np.float(0.),
        "iq_Q": np.array([0.]),
        "iq_I": np.array([0.]),
        "sq_Q": np.array([0.]),
        "sq_S": np.array([0.]),
        "fq_Q": np.array([0.]),
        "fq_F": np.array([0.]),
        "gr_r": np.array([0.]),
        "gr_G": np.array([0.]),
        "gr_max": np.float(0.),
        "gr_argmax": np.float(0.)
    }
    # dark subtraction
    if dk_img is not None:
        data["dk_sub_image"] = np.subtract(raw_img, dk_img)
    # if no calibration, output data now
    if ai is None:
        return data
    # do auto masking if specified
    if auto_mask:
        data["mask"], _ = integ.auto_mask(data["dk_sub_image"],
                                          ai,
                                          mask_setting=mask_setting,
                                          user_mask=user_mask)
    elif user_mask is not None:
        data["mask"] = user_mask
    # integration
    x, y = ai.integrate1d(data["dk_sub_image"],
                          mask=data["mask"],
                          **integ_setting)
    chi_max_ind = np.argmax(y)
    data.update({
        "chi_Q": x,
        "chi_I": y,
        "chi_max": y[chi_max_ind],
        "chi_argmax": x[chi_max_ind]
    })
    # transformation
    if not _PDFGETX_AVAILABLE:
        io.server_message(
            "diffpy.pdfgetx is not installed. No use [0.] for all the relevant data."
        )
        return data
    pdfconfig = PDFConfig(**pdfgetx_setting)
    pdfgetter = PDFGetter(pdfconfig)
    pdfgetter(x, y)
    iq, sq, fq, gr = pdfgetter.iq, pdfgetter.sq, pdfgetter.fq, pdfgetter.gr
    gr_max_ind = np.argmax(gr[1])
    data.update({
        "iq_Q": iq[0],
        "iq_I": iq[1],
        "sq_Q": sq[0],
        "sq_S": sq[1],
        "fq_Q": fq[0],
        "fq_F": fq[1],
        "gr_r": gr[0],
        "gr_G": gr[1],
        "gr_max": gr[1][gr_max_ind],
        "gr_argmax": gr[0][gr_max_ind]
    })
    return data
Esempio n. 5
0
def test_auto_mask(db):
    mask, _ = auto_mask(db["Ni_img"], db["ai"])
    assert np.array_equal(mask[0], np.ones_like(mask[0]))