def getTrimedMap(self): """Obtains the contact map without unmapped regions.""" if self._map is None: return None if np.isscalar(self.mask): return self._map M = ma.array(self._map) M.mask = np.diag(~self.mask) return ma.compress_rowcols(M)
def whitening_masked(Y, X_sig, F, S, W, P): """ Spectral whitening while ignoring a masked region. Note : doit pouvoir etre simplifie. :param ndarray Y: Hyperspectral, multiple observation datacube. Shape: (spatial,spatial,spectral,observation). :param ndarray X_sig: region to ignore in the whitening. Shape: (spatial,spatial). :param ndarray F: FSF to consider in the 3D repliation process. :param int S: spatial size (Y is supposed isotropic, *i.e.* square). :param int W: number of spectral band. :param int P: number of observation """ if P != 1: # When there are multiple observations, the whitening is done by observation. Y_tout_snr_blanc = np.zeros(shape=Y.shape) for p in range(P): Y_pose = Y[:, :, :, p] # reshaping the datacube into an array. liste_vec = np.reshape(Y_pose, (S * S, Y_pose.shape[2])) liste_msk1d = np.reshape(X_sig, S * S) liste_msk = np.tile(liste_msk1d[:, np.newaxis], (1, W)) liste_ext = np.ma.masked_array(liste_vec, np.invert(liste_msk == 0)) liste_ext = ma.compress_rowcols(liste_ext, axis=0) liste_ext = liste_ext[~np.isnan(liste_ext).all(1)] # Covariance estimation on the averaged datacube. Sig_init = np.cov(liste_ext, rowvar=0) Sig_inv_dem = la.inv(la.sqrtm(Sig_init)) # Actual whitening: liste_blanc = np.dot(liste_vec, Sig_inv_dem) # Reshaping the whitened array into a datacube. Y_tout_snr_blanc[:, :, :, p] = np.reshape(liste_blanc, (S, S, W)) Y_src = np.reshape(Y_tout_snr_blanc[:, :, :, :], (S, S, W * P)) # Now we process data averaged over multiple observations, which will # used altogether with the multiple observation data. if Y.ndim == 4: Y_src_unip = np.mean(Y[:, :, :, :], axis=3) else: Y_src_unip = Y liste_vec = np.reshape(Y_src_unip, (S * S, Y_src_unip.shape[2])) liste_msk1d = np.reshape(X_sig, S * S) liste_msk = np.tile(liste_msk1d[:, np.newaxis], (1, W)) liste_ext = np.ma.masked_array(liste_vec, np.invert(liste_msk == 0)) liste_ext = ma.compress_rowcols(liste_ext, axis=0) liste_ext = liste_ext[~np.isnan(liste_ext).all(1)] Sig_init = np.cov(liste_ext, rowvar=0) Sig_inv_dem = la.inv(la.sqrtm(Sig_init)) liste_blanc = np.dot(liste_vec, Sig_inv_dem) Y_src_unip = np.reshape(liste_blanc, (S, S, W)) D_unip = dp.gen_dic(W, P=1) Y_3d_unip, D_3d_unip = dp.replique_3d_pose(Y_src_unip, F, D_unip, P=1) D_3d_unip = D_3d_unip[:Y_3d_unip.shape[2], :] liste_vec_unip = Y_3d_unip.reshape( (Y_3d_unip.shape[0]**2, Y_3d_unip.shape[2])) else: # One observation. The process is the same as above. if Y.ndim == 4: Y_src = np.mean(Y[:, :, :, :], axis=3) else: Y_src = Y liste_vec = np.reshape(Y_src, (S * S, Y_src.shape[2])) liste_msk1d = np.reshape(X_sig, S * S) liste_msk = np.tile(liste_msk1d[:, np.newaxis], (1, W)) liste_ext = np.ma.masked_array(liste_vec, np.invert(liste_msk == 0)) liste_ext = ma.compress_rowcols(liste_ext, axis=0) liste_ext = liste_ext[~np.isnan(liste_ext).all(1)] Sig_init = np.cov(liste_ext, rowvar=0) Sig_inv_dem = la.inv(la.sqrtm(Sig_init)) liste_blanc = np.dot(liste_vec, Sig_inv_dem) Y_src = np.reshape(liste_blanc, (S, S, W)) liste_vec_unip = 0 D_3d_unip = 0 return Y_src, liste_vec_unip, D_3d_unip
def align(self, array, axis=None): if not isinstance(map, np.ndarray): array = np.array(array) ret = array = array.copy() if np.isscalar(self.mask): return ret mask = self.mask.copy() l_full = self.getCompleteMap().shape[0] l_trim = self.getTrimedMap().shape[0] if len(array.shape) == 0: raise ValueError('Aligned array cannot be empty.') elif len(array.shape) == 1: l = array.shape[0] if l == l_trim: N = len(mask) ret = np.zeros(N) ret[mask] = array elif l == l_full: ret = array[mask] else: raise ValueError('The length of the array (%d) does not ' 'match that of either the full (%d) ' 'or trimed (%d).' % (l, l_full, l_trim)) elif len(array.shape) == 2: s = array.shape if axis is None: if s[0] != s[1]: raise ValueError('The array must be a square matrix ' 'if axis is set to None.') if s[0] == l_trim: N = len(mask) whole_mat = np.zeros((N, N)) mask = np.outer(mask, mask) whole_mat[mask] = array.flatten() ret = whole_mat elif s[0] == l_full: M = ma.array(array) M.mask = np.diag(mask) ret = ma.compress_rowcols(M) else: raise ValueError('The size of the array (%d) does not ' 'match that of either the full (%d) ' 'or trimed (%d).' % (s[0], l_full, l_trim)) else: new_shape = list(s) otheraxis = 0 if axis != 0 else 1 if s[axis] == l_trim: N = len(mask) new_shape[axis] = N whole_mat = np.zeros(new_shape) mask = np.expand_dims(mask, axis=otheraxis) mask = mask.repeat(s[otheraxis], axis=otheraxis) whole_mat[mask] = array.flatten() ret = whole_mat elif s[axis] == l_full: mask = np.expand_dims(mask, axis=otheraxis) mask = mask.repeat(s[otheraxis]) ret = self._map[mask] else: raise ValueError('The size of the array (%d) does not ' 'match that of either the full (%d) ' 'or trimed (%d).' % (s[0], l_full, l_trim)) return ret
v = np.linspace(min(prob_img.flatten()), max(prob_img.flatten()), 10, endpoint=True) divider = make_axes_locatable(ax) caxEdges = divider.append_axes("right", size="20%", pad=0.05) plt.colorbar(improb, cax=caxEdges, ticks=v) #################### ### 1) Extract lesion SI/enhancement #################### mask_queryVols = [] onlyROI = [] for k in range(5): mx = ma.masked_array(mriVols[k], mask=mx_query==0) print "masked lesionVol_%i, lesion mean SI/enhancement = %f" % (k, mx.mean()) mask_queryVols.append( ma.filled(mx, fill_value=None) ) onlyROI.append( ma.compress_rowcols( mask_queryVols[k][zslice,:,:] )) # Compute SI difference rSIvols = [] for k in range(1,len(onlyROI)): rSIvols.append( onlyROI[k] - onlyROI[0] ) print "lesion rSIvol_s%i, lesion mean realative SI/enhancement = %f" % (k, rSIvols[k-1].mean()) #################### # ROI network formation # get list of nodes allcoords_wv_redc = approximate_polygon(allcoords_wv, tolerance=0.01) allcoords_wv_redc.shape nodepts = np.asarray( [allcoords_wv_redc[:, 1], allcoords_wv_redc[:, 0]]).transpose() y = np.ascontiguousarray(nodepts).view(np.dtype((np.void, nodepts.dtype.itemsize * nodepts.shape[1]))) _, idx = np.unique(y, return_index=True)