Accessing the same voxel across multiple nifti files in constant time

@alexis is the idea to do this one off for a single voxel, or to do this multiple times for different voxels in the time series?

  1. It is worth noting that standard gzip does not support random access, so one needs to unpack the whole file. In your example, you are trying to load a gzip image (".nii.gz"). Indeed, this is precisely why SPM refuses to support .nii.gz files, insisting on uncompressed .nii files. One can create indexed gzip files that do allow better performance. The files must be created with the indices, there is a slight file size penalty, and while all gzip tools should be able to handle them, they will only be faster with index-gzip aware tools.
  2. The NIfTI specification requires that the first three dimensions are spatial. This is an inherently poor design for operations that want to operate on voxels (e.g. virtually all non-spatial operations: temporal smoothing, slice time correction, statistics, etc.). In other words, for 4D NIfTI time series are always stored XYZT with time being the 4th dimension. Therefore, successive timepoints of a single voxel are stored far apart. The magic of the defunct VoxBo was that its 4D files were stored in TXYZ order, so timepoints for a given voxel were stored contiguously, allowing reading along cache lines dramatically improving performance. If you want to do a lot of voxelwise calculations, you may want to swizzle your datasets to TXYZ and then save these as uncompressed files that can be accessed with a memmap. For full disclosure, when the NIfTI format was being specified I advocated for allowing this configuration, but this was not the consensus.