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, )
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,)
def plot_anat_parcel_func_fusion(anat, func, parcel, parcel_col='white', parcels_line_width=.5, func_cmap=None, func_norm=None, anat_norm=None, func_mask=None, highlighted_parcels_col=None, highlighted_parcels_line_width=1.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') parcel = parcel.astype(int) parcel_rebin = rebin(parcel, anat.shape) #parcel_rebin_ma = np.ma.masked_where(parcel_rebin==0, parcel_rebin) func_rebin = rebin(func, anat.shape) fmask = (parcel_rebin > 0).astype(bool) if func_mask is not None: func_mask_rebin = rebin(func_mask.astype(bool), anat.shape) #print 'func_mask:', func_mask_rebin.sum() fmask = np.bitwise_and(fmask, func_mask_rebin) func_rebin_ma = np.ma.masked_where(fmask == 0, func_rebin) anat_cmap = plt.get_cmap('gray') plt.figure() plt.hold(True) if 0: #TODO: better color mixing ax = plt.imshow(anat, cmap=anat_cmap, norm=anat_norm, interpolation='nearest') plt.imshow(func_rebin_ma, interpolation='nearest', alpha=.5, cmap=func_cmap, norm=func_norm, axes=ax) if 1: ax = plt.imshow(mix_cmap(func_rebin_ma, func_cmap, anat, anat_cmap, norm1=func_norm, norm2=anat_norm, blend_r=.5), interpolation='nearest') ax.axes.set_axis_off() if 0: plt.imshow(func_rebin_ma * anat, interpolation='nearest', cmap=func_cmap, norm=func_norm) if 0: ax = plt.imshow(func_rebin_ma, interpolation='nearest', cmap=func_cmap, norm=func_norm) plt.colorbar() plt.imshow(anat, cmap=plt.get_cmap('gray'), alpha=.65, interpolation='nearest', axes=ax) if 0: plt.imshow(mix_cmap(func_rebin_ma, func_cmap, anat, anat_cmap), interpolation='nearest') labs = np.unique(parcel) # 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 alpha = .5 else: lw = parcels_line_width alpha = .2 plt.contour( (parcel_rebin == lab).astype(int), 1, colors=[col], linewidths=lw, alpha=alpha, )
def plot_anat_parcel_func_fusion(anat, func, parcel, parcel_col='white', parcels_line_width=.5, func_cmap=None, func_norm=None, anat_norm=None, func_mask=None, highlighted_parcels_col=None, highlighted_parcels_line_width=1.5): if highlighted_parcels_col is None: highlighted_parcels_col = {} if func_cmap is None: func_cmap = plt.get_cmap('jet') parcel = parcel.astype(int) parcel_rebin = rebin(parcel, anat.shape) #parcel_rebin_ma = np.ma.masked_where(parcel_rebin==0, parcel_rebin) func_rebin = rebin(func, anat.shape) fmask = (parcel_rebin>0).astype(bool) if func_mask is not None: func_mask_rebin = rebin(func_mask.astype(bool), anat.shape) #print 'func_mask:', func_mask_rebin.sum() fmask = np.bitwise_and(fmask, func_mask_rebin) func_rebin_ma = np.ma.masked_where(fmask==0, func_rebin) anat_cmap = plt.get_cmap('gray') plt.figure() plt.hold(True) if 0: #TODO: better color mixing ax = plt.imshow(anat, cmap=anat_cmap, norm=anat_norm, interpolation='nearest') plt.imshow(func_rebin_ma, interpolation='nearest', alpha=.5, cmap=func_cmap, norm=func_norm, axes=ax) if 1: ax = plt.imshow(mix_cmap(func_rebin_ma, func_cmap, anat, anat_cmap, norm1=func_norm, norm2=anat_norm, blend_r=.5), interpolation='nearest') ax.get_axes().set_axis_off() if 0: plt.imshow(func_rebin_ma*anat, interpolation='nearest', cmap=func_cmap, norm=func_norm) if 0: ax = plt.imshow(func_rebin_ma, interpolation='nearest', cmap=func_cmap, norm=func_norm) plt.colorbar() plt.imshow(anat, cmap=plt.get_cmap('gray'), alpha=.65, interpolation='nearest', axes=ax) if 0: plt.imshow(mix_cmap(func_rebin_ma, func_cmap, anat, anat_cmap), interpolation='nearest') labs = np.unique(parcel) # 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 alpha = .5 else: lw = parcels_line_width alpha = .2 plt.contour((parcel_rebin==lab).astype(int), 1, colors=[col], linewidths=lw, alpha=alpha,)