-
Notifications
You must be signed in to change notification settings - Fork 264
MRG: mmap keyword for loading images #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dfdc6db
ea6f9c5
63c788c
7f71741
ad7b1cf
6f0a9e2
1ac33f3
744c631
cca7371
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,11 +13,11 @@ | |
from os.path import splitext | ||
import numpy as np | ||
|
||
from nibabel.volumeutils import (array_to_file, array_from_file, Recoder) | ||
from nibabel.spatialimages import HeaderDataError, ImageFileError, SpatialImage | ||
from nibabel.fileholders import FileHolder, copy_file_map | ||
from nibabel.filename_parser import types_filenames, TypesFilenamesError | ||
from nibabel.arrayproxy import ArrayProxy | ||
from ..volumeutils import (array_to_file, array_from_file, Recoder) | ||
from ..spatialimages import HeaderDataError, SpatialImage | ||
from ..fileholders import FileHolder, copy_file_map | ||
from ..arrayproxy import ArrayProxy | ||
from ..keywordonly import kw_only_meth | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. It might be worth going through the whole package and fixing any remaining non-relative imports before release. WDYT? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we could do that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See: #270 |
||
|
||
# mgh header | ||
# See http://surfer.nmr.mgh.harvard.edu/fswiki/FsTutorial/MghFormat | ||
|
@@ -468,26 +468,64 @@ def filespec_to_file_map(klass, filespec): | |
return super(MGHImage, klass).filespec_to_file_map(filespec) | ||
|
||
@classmethod | ||
def from_file_map(klass, file_map): | ||
@kw_only_meth(1) | ||
def from_file_map(klass, file_map, mmap=True): | ||
'''Load image from `file_map` | ||
|
||
Parameters | ||
---------- | ||
file_map : None or mapping, optional | ||
files mapping. If None (default) use object's ``file_map`` | ||
attribute instead | ||
''' | ||
mmap : {True, False, 'c', 'r'}, optional, keyword only | ||
`mmap` controls the use of numpy memory mapping for reading image | ||
array data. If False, do not try numpy ``memmap`` for data array. | ||
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A `mmap` | ||
value of True gives the same behavior as ``mmap='c'``. If image | ||
data file cannot be memory-mapped, ignore `mmap` value and read | ||
array from file. | ||
''' | ||
if not mmap in (True, False, 'c', 'r'): | ||
raise ValueError("mmap should be one of {True, False, 'c', 'r'}") | ||
mghf = file_map['image'].get_prepare_fileobj('rb') | ||
header = klass.header_class.from_fileobj(mghf) | ||
affine = header.get_affine() | ||
hdr_copy = header.copy() | ||
data = klass.ImageArrayProxy(mghf, hdr_copy) | ||
data = klass.ImageArrayProxy(mghf, hdr_copy, mmap=mmap) | ||
img = klass(data, affine, header, file_map=file_map) | ||
img._load_cache = {'header': hdr_copy, | ||
'affine': affine.copy(), | ||
'file_map': copy_file_map(file_map)} | ||
return img | ||
|
||
@classmethod | ||
@kw_only_meth(1) | ||
def from_filename(klass, filename, mmap=True): | ||
''' class method to create image from filename `filename` | ||
|
||
Parameters | ||
---------- | ||
filename : str | ||
Filename of image to load | ||
mmap : {True, False, 'c', 'r'}, optional, keyword only | ||
`mmap` controls the use of numpy memory mapping for reading image | ||
array data. If False, do not try numpy ``memmap`` for data array. | ||
If one of {'c', 'r'}, try numpy memmap with ``mode=mmap``. A `mmap` | ||
value of True gives the same behavior as ``mmap='c'``. If image | ||
data file cannot be memory-mapped, ignore `mmap` value and read | ||
array from file. | ||
|
||
Returns | ||
------- | ||
img : MGHImage instance | ||
''' | ||
if not mmap in (True, False, 'c', 'r'): | ||
raise ValueError("mmap should be one of {True, False, 'c', 'r'}") | ||
file_map = klass.filespec_to_file_map(filename) | ||
return klass.from_file_map(file_map, mmap=mmap) | ||
|
||
load = from_filename | ||
|
||
def to_file_map(self, file_map=None): | ||
''' Write image to `file_map` or contained ``self.file_map`` | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why even allow
mmap=True
if it just wraps tommap='c'
? Can't you just say the default is'c'
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a discussion we had on the mailing list. The idea was that someone might want to turn on memory mapping, but not know about 'mode', so True is there as a convenience for that person. I don't feel crazy strongly about it though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, I suppose that makes sense. Makes sense to leave it that way, then.