Esempio n. 1
0
def crop_data_file(data_fn, mask_fn, output_data_fn):

    data,h = read_volume(data_fn)
    mask,mh = read_volume(mask_fn)

    if data.ndim > mask.ndim:
        mask = mask[:,:,:,np.newaxis] * np.ones(data.shape, dtype=int)
    elif mask.ndim > data.ndim:
        raise Exception('Mask has more dims (%d) than data (%d).' \
                        'Maybe data and mask were switched?' \
                        %(mask.ndim, data.ndim))

    cropped_data = crop_array(data, mask)

    if 1:
        print 'cropped_data:', cropped_data.shape
        print 'input_data_fn:', data_fn
        print 'output_data_fn:', output_data_fn

    if len(h[1].extensions) > 0:
        #remove cuboid nifti extension
        h[1].extensions.pop()

    write_volume(cropped_data, output_data_fn, h)
Esempio n. 2
0
def crop_data_file(data_fn, mask_fn, output_data_fn):

    data, h = read_volume(data_fn)
    mask, mh = read_volume(mask_fn)

    if data.ndim > mask.ndim:
        mask = mask[:, :, :, np.newaxis] * np.ones(data.shape, dtype=int)
    elif mask.ndim > data.ndim:
        raise Exception('Mask has more dims (%d) than data (%d).'
                        'Maybe data and mask were switched?'
                        % (mask.ndim, data.ndim))

    cropped_data = crop_array(data, mask)

    if 1:
        print 'cropped_data:', cropped_data.shape
        print 'input_data_fn:', data_fn
        print 'output_data_fn:', output_data_fn

    if len(h[1].extensions) > 0:
        # remove cuboid nifti extension
        h[1].extensions.pop()

    write_volume(cropped_data, output_data_fn, h)
Esempio n. 3
0
def plot_func_slice(func_slice_data,
                    anatomy=None,
                    parcellation=None,
                    parcel_col='white',
                    parcels_line_width=2.5,
                    func_cmap=None,
                    func_norm=None,
                    anat_norm=None,
                    func_mask=None,
                    highlighted_parcels_col=None,
                    highlighted_parcels_line_width=2.5,
                    resolution=None,
                    crop_extension=None,
                    blend=.5):

    import matplotlib.pyplot as plt

    if highlighted_parcels_col is None:
        highlighted_parcels_col = {}

    if func_cmap is None:
        func_cmap = plt.get_cmap('jet')

    if anatomy is not None:
        if func_slice_data.shape != anatomy.shape:
            func_slice_data = rebin(func_slice_data, anatomy.shape)

    if parcellation is not None:
        parcellation = parcellation.astype(int)

        if anatomy is not None:
            if parcellation.shape != anatomy.shape:
                parcellation = rebin(parcellation, anatomy.shape)

        fmask = (parcellation > 0).astype(bool)
    else:
        fmask = np.ones_like(func_slice_data).astype(bool)

    if func_mask is not None:
        if anatomy is not None:
            func_mask = rebin(func_mask.astype(bool), anatomy.shape)
        #print 'func_mask:', func_mask.sum()
        fmask = np.bitwise_and(fmask, func_mask)

    if crop_extension is not None:
        from pyhrf.tools import crop_array
        func_slice_data = crop_array(func_slice_data, fmask, crop_extension)
        if anatomy is not None:
            m_tmp = np.where(fmask != 0)
            mask_anat_tmp = np.zeros_like(anatomy)
            e = crop_extension
            start_i = m_tmp[0].min() - e - 1
            start_j = m_tmp[1].min() - e - 1
            if start_i < 0 or start_j < 0:
                raise Exception('Anatomy is not large enough to extend view.')

            mask_anat_tmp[start_i:m_tmp[0].max() + e,
                          start_j:m_tmp[1].max() + e] = 1
            anatomy = crop_array(anatomy, mask_anat_tmp)
            # print 'func_slice_data:', func_slice_data.shape
            # print 'anatomy:', anatomy.shape
        fmask = crop_array(fmask, fmask, crop_extension)

    func_masked = np.ma.masked_where(fmask == 0, func_slice_data)

    anat_cmap = plt.get_cmap('gray')

    plt.figure()
    # plt.hold(True) Deprecated function, no longer necessary to call it.

    if anatomy is not None:
        ax = plt.imshow(mix_cmap(func_masked,
                                 func_cmap,
                                 anatomy,
                                 anat_cmap,
                                 norm1=func_norm,
                                 norm2=anat_norm,
                                 blend_r=blend),
                        interpolation='nearest')
    else:
        ax = plt.imshow(func_masked,
                        cmap=func_cmap,
                        norm=func_norm,
                        interpolation='nearest')
    ax.axes.set_axis_off()

    if resolution is not None:
        ratio = resolution[0] / resolution[1]
        print 'Set aspect ratio to:', ratio
        ax.axes.set_aspect(ratio)

    if parcellation is not None:
        labs = np.unique(parcellation)

        # nr, nc = parcel_rebin.shape
        # extent = [-0.5, nc-0.5, -0.5, nr-0.5]

        for lab in labs:  #[484,427,540]: #
            if lab != 0:
                col = highlighted_parcels_col.get(lab, parcel_col)
                if lab in highlighted_parcels_col:
                    lw = highlighted_parcels_line_width
                else:
                    lw = parcels_line_width
                plt.contour(
                    (parcellation == lab).astype(int),
                    1,
                    colors=[col],
                    linewidths=lw,
                    alpha=.6,
                )
Esempio n. 4
0
File: plot.py Progetto: Solvi/pyhrf
def plot_func_slice(func_slice_data, anatomy=None, parcellation=None,
                    parcel_col='white',
                    parcels_line_width=2.5, func_cmap=None,
                    func_norm=None, anat_norm=None, func_mask=None,
                    highlighted_parcels_col=None,
                    highlighted_parcels_line_width=2.5,
                    resolution=None, crop_extension=None, blend=.5):

    if highlighted_parcels_col is None:
        highlighted_parcels_col = {}

    if func_cmap is None:
        func_cmap = plt.get_cmap('jet')

    if anatomy is not None:
        if func_slice_data.shape != anatomy.shape:
            func_slice_data = rebin(func_slice_data, anatomy.shape)

    if parcellation is not None:
        parcellation = parcellation.astype(int)

        if anatomy is not None:
            if parcellation.shape != anatomy.shape:
                parcellation = rebin(parcellation, anatomy.shape)

        fmask = (parcellation>0).astype(bool)
    else:
        fmask = np.ones_like(func_slice_data).astype(bool)

    if func_mask is not None:
        if anatomy is not None:
            func_mask = rebin(func_mask.astype(bool), anatomy.shape)
         #print 'func_mask:', func_mask.sum()
        fmask = np.bitwise_and(fmask, func_mask)

    if crop_extension is not None:
        from pyhrf.tools import crop_array
        func_slice_data = crop_array(func_slice_data, fmask, crop_extension)
        if anatomy is not None:
            m_tmp = np.where(fmask!=0)
            mask_anat_tmp = np.zeros_like(anatomy)
            e = crop_extension
            start_i = m_tmp[0].min()-e-1
            start_j = m_tmp[1].min()-e-1
            if start_i < 0 or start_j < 0:
                raise Exception('Anatomy is not large enough to extend view.')

            mask_anat_tmp[start_i:m_tmp[0].max()+e,start_j:m_tmp[1].max()+e] = 1
            anatomy = crop_array(anatomy, mask_anat_tmp)
            # print 'func_slice_data:', func_slice_data.shape
            # print 'anatomy:', anatomy.shape
        fmask = crop_array(fmask, fmask, crop_extension)

    func_masked = np.ma.masked_where(fmask==0, func_slice_data)


    anat_cmap = plt.get_cmap('gray')

    plt.figure()
    plt.hold(True)

    if anatomy is not None:
        ax = plt.imshow(mix_cmap(func_masked, func_cmap,
                                 anatomy, anat_cmap,
                                 norm1=func_norm,
                                 norm2=anat_norm, blend_r=blend),
                        interpolation='nearest')
    else:
        ax = plt.imshow(func_masked, cmap=func_cmap,
                        norm=func_norm,
                        interpolation='nearest')
    ax.get_axes().set_axis_off()


    if resolution is not None:
        ratio = resolution[0] / resolution[1]
        print 'Set aspect ratio to:', ratio
        ax.get_axes().set_aspect(ratio)

    if parcellation is not None:
        labs = np.unique(parcellation)

        # nr, nc = parcel_rebin.shape
        # extent = [-0.5, nc-0.5, -0.5, nr-0.5]

        for lab in labs: #[484,427,540]: #
            if lab != 0:
                col = highlighted_parcels_col.get(lab, parcel_col)
                if lab in highlighted_parcels_col:
                    lw = highlighted_parcels_line_width
                else:
                    lw = parcels_line_width
                plt.contour((parcellation==lab).astype(int), 1,
                            colors=[col], linewidths=lw, alpha=.6,)