def applyDimOrderToList(L, dimorder): """Permutes the values in L as specified by dimorder. Arguments: L: A list of four values, corresponding (respectively) to the X, Y, Z, and I dimensions of some dataset. If you've only got values for X, Y, and Z, pad before calling. dimorder: Four-character string that is a permutation of "XYZI", indicating the desired dimension order of the output. Returns a permuted version of L. """ return [L[i] for i in _miraw_helpers.dimorderToDimmap(dimorder)]
def applyDimOrder(img, dimorder): """Permutes the data dimensions of img as specified by dimorder. Arguments: img: numpy ndarray with four dimensions. dimorder: Four-character string that is a permutation of "XYZI", indicating the desired dimension order of the output. Preconditions: - The current dimension order of "img" is "XYZI". Returns new_img: new_img: numpy ndarray with four dimensions. The values in new_img will be rearranged so that the dimension order is as specified by dimorder. Note that if you have any metadata about the image, like voxel sizes, the order of values in your metadata will no longer match the order of the dimensions in the output image. You'll need to rearrange those manually with applyDimOrderToList(). """ return img.transpose(_miraw_helpers.dimorderToDimmap(dimorder))
def readRawWithSizeInfo(f, sizefile=None, dtype=None, cropped=None, dimorder=None, diskorder='F', memorder='C'): """Loads a raw image file from disk, using a size_info metadata file. Arguments: f: A filename or open file object for the raw data file. sizefile: A filename for the size_info metadata file. If None, looks for a file called "size_info" in f's directory. dtype: The numpy dtype of the raw data, or None. cropped: A boolean: whether f is a cropped or full volume (as described in the sizefile), or None (in which case this will be inferred). dimorder: Four-character string that is a permutation of "XYZI", indicating the dimension order of the image being read from disk. The purpose of this argument is to map from the dimension extents stored in the size_info file, which are always stored in XYZI order, to the actual shape of the ndarray we create. Namely, if we create a 4-tuple "dimmap" by converting each character X->0, Y->1, Z->2, I->3, then vol.shape[i] = sz[dimmap[i]] for i from 0 to 3, where vol is the returned volume and sz is the volume size, in (X,Y,Z,I) order, read from size_info. The default value, None, is equivalent to "XYZI". This can be a confusing argument, so please note: - dimorder is overridden if the size_info file specifies a "dimension_order" value. - dimorder only indicates a rearrangement of dimension extents from the default order (as read in the size_info file) to the order that dictates the shape attribute of the returned array. Though it interacts in complicated ways with the diskorder and memorder arguments, ultimately it is not equivalent to calling transpose(dimmap) on the returned array. - dimorder does not change the order of the dimension extents as stored in the returned dictionary "cfg". diskorder: The array traversal order of the file. memorder: The desired traversal order of the output array. (See readRaw for more explanation of the last two arguments.) This function attempts, usually successfully, to infer the values of arguments left None. Returns (vol, cfg), where vol is a numpy ndarray and cfg is the dict of settings in sizefile. In addition, this function defines an additional key, cfg['cropped'], with Boolean value. """ # Read the image into a 1-D array. raw = readRaw(f, (-1,1), dtype=dtype, diskorder=diskorder, memorder=memorder) # Read the size file. imgname = _miraw_helpers.getFilename(f) if sizefile is None: try: sizefile = os.path.join(os.path.dirname(imgname), 'size_info') except: raise TypeError("Can't infer sizefile from filename '%s'." % imgname) cfg = readConfigFile(sizefile) sz = cfg['full_image_size_(voxels)'] sz_c = cfg['cropped_image_size_(voxels)'] try: n_imgs = cfg['num_dwis'] except KeyError: n_imgs = 1 # Try to figure out whether the image is cropped. cropped, threeD = _miraw_helpers.detectShapeAndCropping(raw.size, np.prod(sz), np.prod(sz_c), n_imgs, cropped) if cropped: sz = sz_c sz = sz + [n_imgs] cfg['cropped'] = cropped # Finally set the size and return. try: dimorder = cfg['dimension_order'] except KeyError: if dimorder is None: dimorder = _miraw_helpers.DIM_DEFAULT_ORDER if not _miraw_helpers.isValidDimorder(dimorder): raise ValueError('"%s" is not a valid dimorder argument.' % repr(dimorder)) if threeD: sz = sz[0:3] else: sz = np.take(sz, _miraw_helpers.dimorderToDimmap(dimorder), axis=0) return (_miraw_helpers.ndcopyWithOrder(raw.reshape(sz, order=diskorder), memorder), cfg)