def main(): args = parse_args() mask = dwi.mask.read_mask(args.input) selected_slices = list(mask.selected_slices()) mbb = mask.bounding_box() mbb_shape = tuple([b - a for a, b in mbb]) d = dict(infile=args.input, shape=mask.shape(), size=np.prod(mask.shape()), nsel=mask.n_selected(), mbb=mbb, mbb_shape=mbb_shape, mbb_size=np.prod(mbb_shape), mbb_all=mask.mbb_equals_selection(), nsl=len(selected_slices), sl=selected_slices, msl=mask.max_slices()) s = ( 'mask: {infile}\n' 'mask shape: {shape}\n' 'mask size: {size}\n' 'selected voxels: {nsel}\n' 'minimum bounding box coordinates: {mbb}\n' 'minimum bounding box shape: {mbb_shape}\n' 'minimum bounding box size: {mbb_size} (equals selection: {mbb_all})\n' 'selected slices: {nsl}: {sl}\n' 'maximum slices: {msl}' ) print(s.format(**d)) if args.subregion: write_subregion(mask, args.pad, args.input, args.subregion)
def write_subregion(mask, pad, infile, filename): """Write a subregion file from mask.""" bb = mask.bounding_box(pad=(np.inf, pad, pad)) bb = tuple(np.ravel(bb)) comment = '%s, %i' % (os.path.basename(infile), pad) print('Writing subregion to %s with padding of %s...' % (filename, pad)) dwi.files.write_subregion_file(filename, bb, comment=comment)
def main(): """Main.""" models = ['{n}: {d}'.format(n=x.name, d=x.desc) for x in dwi.models.Models] args = parse_args(models) model = [x for x in dwi.models.Models if x.name == args.model][0] image, attrs = dwi.files.read_pmap(args.input, params=args.params) assert image.ndim == 4, image.ndim if args.verbose: print('Read image', image.shape, image.dtype, args.input) print('Parameters', attrs['parameters']) if args.mask: if args.verbose: print('Applying mask', args.mask) mask = dwi.mask.read_mask(args.mask) if args.mbb: mbb = mask.bounding_box(args.mbb) if args.verbose: print('Using minimum bounding box {m}'.format(m=mbb)) z, y, x = [slice(*t) for t in mbb] mask.array[z, y, x] = True attrs['mbb'] = args.mbb image = mask.apply_mask(image, value=np.nan) attrs['mask'] = args.mask if args.subwindow: if args.verbose: print('Using subwindow', args.subwindow) # image = dwi.util.crop_image(image, args.subwindow, onebased=True) image = dwi.util.select_subwindow(image, args.subwindow, onebased=True) print(image.shape, np.count_nonzero(np.isnan(image))) attrs['subwindow'] = args.subwindow if args.average: image = np.mean(image, axis=(0, 1, 2), keepdims=True) if model.name == 'T2': image, attrs = fix_t2(image, attrs) if args.verbose: n = np.count_nonzero(~np.isnan(image[..., 0])) print('Fitting {m} to {n} voxels'.format(m=model.name, n=n)) print('Guesses:', [len(p.guesses(1)) for p in model.params]) timepoints = get_timepoints(model, attrs) params = get_params(model, timepoints) pmap = fit(image, timepoints, model) d = dict(attrs) d.update(parameters=params, source=args.input, model=model.name, description=repr(model)) dwi.files.write_pmap(args.output, pmap, d) if args.verbose: print('Wrote', pmap.shape, pmap.dtype, args.output)