def test_apply_mask(self): mask = np.zeros((3,4,5)) ix = [[0,1],[1,2],[2,3]] mask[ix] = 1 mask_path = os.path.join(test_data_directory, 'mask.nii') nbsave(Nifti1Image(mask, np.eye(4)), mask_path) apply_mask = ApplyMask() apply_mask.load_mask(mask_path) self.assertTrue(apply_mask.mask.shape==(3,4,5)) data = np.random.randn(2) voxels = np.zeros_like(mask) voxels[ix] = data volume = Nifti1Image(voxels, np.eye(4)) data_out = apply_mask.run(volume) self.assertTrue((data==data_out).all())
def run(self, inp): fpath = self.path_format % self._i nbsave(inp, os.path.join(self.recording_dir, fpath)) self._i += 1
def transform(inp, base, output_transform=False): if type(base)==str: base_path = base else: base_path = str(uuid4())+'.nii' nbsave(base, base_path) if type(inp)==str: inp_path = inp else: inp_path = str(uuid4())+'.nii' nbsave(inp, inp_path) out_path = str(uuid4())+'.nii' params = [ { 'name': 'input file path', 'position': 'last', 'value': inp_path }, { 'name': 'reference path', 'flag': 'base', 'value': base_path }, { 'name': 'output file path', 'flag': 'prefix', 'value': out_path } ] if output_transform: transform_path = str(uuid4())+'.aff12.1D' params.append({ 'name': 'transform path', 'flag': '1Dmatrix_save', 'value': transform_path }) cmd = generate_command('3dvolreg', params) devnull = open(os.devnull, 'w') ret = call(cmd, stdout=devnull, stderr=STDOUT, close_fds=True) if ret>0: print ' '.join(cmd) out_img = nbload(out_path) out_img.get_data() os.remove(out_path) if inp is not inp_path: os.remove(inp_path) if base is not base_path: os.remove(base_path) if output_transform: xfm = load_afni_xfm(transform_path) os.remove(transform_path) return out_img, xfm else: return out_img