def are_blended_voxels_aligned(blended, main_dr, main_r0, over_arr, over_dr, over_r0): """Tests that the over_arr was mixed into blended in the correct voxels, given the spacing and offset parameters. NOTE: It is assumed that the blended array was zero everywhere before blending, making any nonzero elements the contribution of over_arr """ main_map = ni_api.AffineTransform.from_start_step('ijk', 'xyz', main_r0, main_dr) over_map = ni_api.AffineTransform.from_start_step('ijk', 'xyz', over_r0, over_dr) over_vox_from_main_vox = ni_api.compose(over_map.inverse(), main_map) #checking the red component main_nz_vox = np.array( blended[...,0].nonzero() ).T over_nz_vox = over_vox_from_main_vox(main_nz_vox) over_nz_vox = over_nz_vox[(over_nz_vox >= 0).all(axis=-1)] max_idx = np.array(over_arr.shape[:3]) over_nz_vox = over_nz_vox[(over_nz_vox < max_idx).all(axis=-1)].astype('i') # check that in fact the nonzero components from the main blended # image are the nonzero components from the overlay image.. # this mask is False at all over_nz_vox mask = np.ma.getmask(vu.signal_array_to_masked_vol( np.empty(len(over_nz_vox)), over_nz_vox )) # where the mask is True should be 0 -- well, not so if part of the # over array lies outside of the under array. Then there are regions # where the over array does not blend! ## none_outside = not over_arr[...,0][mask].any() # where the mask is False should be != 0 all_inside = over_arr[...,0][np.logical_not(mask)].all() return all_inside
def to_ni_image(self, t, f, grid_shape=None, prior_mask=None): """Form a NIPY Image of the map of this object at (t,f). The new Image will have a CoordinateMap transforming the array indices into the target space of this Beam's coregistered MRI. """ print 'slicing tf beam at t,f =', (t,f) if (prior_mask is not None) and (prior_mask.shape == self.s.shape): prior_mask = prior_mask[:,t,f] m_arr = signal_array_to_masked_vol(self.s[:,t,f], self.voxel_indices, grid_shape=grid_shape, prior_mask=prior_mask) meg2mri = self.coreg.meg2mri return ni_api.Image(m_arr, compose(meg2mri, self.coordmap))
def to_masked_array(self, t_idx, f_idx, grid_shape=None): sig = self.beam_sig[:,t_idx,f_idx] vox = self.beam.voxel_indices bmask = self.beam_mask if bmask is not None: vox_mask = bmask[:,t_idx,f_idx] if not (vox_mask==self.threshold.binary_mask).all(): print 'warning! beam mask not synced with threshold mask!' else: vox_mask = None m_arr = signal_array_to_masked_vol(sig, vox, grid_shape=grid_shape, prior_mask=vox_mask, fill_value=self.fill_value) return m_arr
def from_blobs(klass, scalar_map, voxel_coordinates, coordmap, **kwargs): arr_indices = np.round(coordmap.inverse()(voxel_coordinates)).astype('i') ## arr_indices = coordmap.inverse(voxel_coordinates).astype('i') arr = vu.signal_array_to_masked_vol(scalar_map, arr_indices) image = ni_api.Image(arr.data, coordmap) return klass(image, mask=np.logical_not(arr.mask), **kwargs)