Esempio n. 1
0
def kde_normalize(img, mask=None, contrast='t1', norm_value=1):
    """
    use kernel density estimation to find the peak of the white
    matter in the histogram of a skull-stripped image. Normalize
    the WM of the non-skull-stripped image to norm_value

    Args:
        img (nibabel.nifti1.Nifti1Image): target MR image
        mask (nibabel.nifti1.Nifti1Image): brain mask of img
        contrast (str): contrast of img (T1,T2,FA,MD)
        norm_value (float): value at which to place WM peak

    Returns:
        normalized (nibabel.nifti1.Nifti1Image): WM normalized img
    """
    if mask is not None:
        voi = img.get_fdata()[mask.get_fdata() == 1].flatten()
    else:
        voi = img.get_fdata()[img.get_fdata() > img.get_fdata().mean()].flatten()
    if contrast.lower() in ['t1', 'flair', 'last']:
        wm_peak = hist.get_last_mode(voi)
    elif contrast.lower() in ['t2', 'largest']:
        wm_peak = hist.get_largest_mode(voi)
    elif contrast.lower() in ['md', 'first']:
        wm_peak = hist.get_first_mode(voi)
    else:
        raise NormalizationError(
            'Contrast {} not valid, needs to be `t1`,`t2`,`flair`,`md`,`first`,`largest`,`last`'.format(contrast))
    normalized = nib.Nifti1Image((img.get_fdata() / wm_peak) * norm_value,
                                 img.affine, img.header)
    return normalized
Esempio n. 2
0
def whitestripe(img,
                contrast,
                mask=None,
                width=0.05,
                width_l=None,
                width_u=None):
    """
    find the "(normal appearing) white (matter) stripe" of the input MR image
    and return the indices

    Args:
        img (nibabel.nifti1.Nifti1Image): target MR image
        contrast (str): contrast of img (e.g., T1)
        mask (nibabel.nifti1.Nifti1Image): brainmask for img (None is default, for skull-stripped img)
        width (float): width quantile for the "white (matter) stripe"
        width_l (float): lower bound for width (default None, derives from width)
        width_u (float): upper bound for width (default None, derives from width)

    Returns:
        ws_ind (np.ndarray): the white stripe indices (boolean mask)
    """
    if width_l is None and width_u is None:
        width_l = width
        width_u = width
    img_data = img.get_data()
    if mask is not None:
        mask_data = mask.get_data()
        masked = img_data * mask_data
        voi = img_data[mask_data == 1]
    else:
        masked = img_data
        voi = img_data[img_data > img_data.mean()]
    if contrast.lower() in ['t1', 'last']:
        mode = hist.get_last_mode(voi)
    elif contrast.lower() in ['t2', 'flair', 'largest']:
        mode = hist.get_largest_mode(voi)
    elif contrast.lower() in ['md', 'first']:
        mode = hist.get_first_mode(voi)
    else:
        raise NormalizationError(
            'Contrast {} not valid, needs to be `t1`,`t2`,`flair`,`md`,`first`,`largest`,`last`'
            .format(contrast))
    img_mode_q = np.mean(voi < mode)
    ws = np.percentile(voi, (max(img_mode_q - width_l, 0) * 100,
                             min(img_mode_q + width_u, 1) * 100))
    ws_ind = np.logical_and(masked > ws[0], masked < ws[1])
    if len(ws_ind) == 0:
        raise NormalizationError(
            'WhiteStripe failed to find any valid indices!')
    return ws_ind