Skip to content

Commit 36d8c21

Browse files
committed
Renamed from_filespec to from_filename for consistency; move methods into base class
1 parent f47a4f0 commit 36d8c21

File tree

5 files changed

+120
-79
lines changed

5 files changed

+120
-79
lines changed

doc/examples.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ preserving as much header information as possible
115115
[ 4 32 32 16 10 1 1 1]
116116
>>> # a filename in our temporary directory
117117
>>> fname = pjoin(tmpdir, 'part.hdr.gz')
118-
>>> nim2.to_filespec(fname)
118+
>>> nim2.to_filename(fname)
119119

120120
The :class:`~nifti.image.NiftiImage` constructor takes a dictionary with header
121121
information as an optional argument. Settings that are not determined by the

nibabel/analyze.py

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
therefore must know how to predict image filenames from header
2020
filenames, whether these are different, and so on.
2121
22-
You can access and set fields of a particular header type using standard __getitem__ / __setitem__ syntax:
22+
You can access and set fields of a particular header type using standard
23+
__getitem__ / __setitem__ syntax:
2324
2425
hdr['field'] = 10
2526
@@ -1136,11 +1137,6 @@ def get_data_dtype(self):
11361137
def set_data_dtype(self, dtype):
11371138
self._header.set_data_dtype(dtype)
11381139

1139-
@classmethod
1140-
def from_filespec(klass, filespec):
1141-
files = klass.filespec_to_files(filespec)
1142-
return klass.from_files(files)
1143-
11441140
@classmethod
11451141
def from_files(klass, files):
11461142
fname = files['header']
@@ -1150,14 +1146,6 @@ def from_files(klass, files):
11501146
ret._files = files
11511147
return ret
11521148

1153-
@classmethod
1154-
def from_image(klass, img):
1155-
orig_hdr = img.get_header()
1156-
return klass(img.get_data(),
1157-
img.get_affine(),
1158-
img.get_header(),
1159-
img.extra)
1160-
11611149
@staticmethod
11621150
def filespec_to_files(filespec):
11631151
ftups = filetuples.FileTuples(
@@ -1171,29 +1159,6 @@ def filespec_to_files(filespec):
11711159
files = dict(zip(('header', 'image'), ftups.get_filenames()))
11721160
return files
11731161

1174-
def to_filespec(self, filename):
1175-
warnings.warn('``to_filespec`` is deprecated, please '
1176-
'use ``to_filename`` instead',
1177-
DeprecationWarning)
1178-
self.to_filename(filename)
1179-
1180-
def to_filename(self, filename):
1181-
''' Write image to files implied by filename string
1182-
1183-
Paraameters
1184-
-----------
1185-
filename : str
1186-
filename to which to save image. We will parse `filename`
1187-
with ``filespec_to_files`` to work out names for image,
1188-
header etc.
1189-
1190-
Returns
1191-
-------
1192-
None
1193-
'''
1194-
files = self.filespec_to_files(filename)
1195-
self.to_files(files)
1196-
11971162
def to_files(self, files=None):
11981163
''' Write image to files passed, or self._files
11991164
'''
@@ -1251,23 +1216,6 @@ def _update_header(self):
12511216
RZS = self._affine[:3,:3]
12521217
vox = np.sqrt(np.sum(RZS * RZS, axis=0))
12531218
hdr['pixdim'][1:4] = vox
1254-
1255-
@classmethod
1256-
def load(klass, filespec):
1257-
return klass.from_filespec(filespec)
1258-
1259-
@classmethod
1260-
def save(klass, img, filename):
1261-
warnings.warn('``save`` class method is deprecated\n'
1262-
'You probably want the ``to_filename`` instance '
1263-
'method, or the module-level ``save`` function',
1264-
DeprecationWarning)
1265-
klass.instance_to_filename(img, filename)
1266-
1267-
@classmethod
1268-
def instance_to_filename(klass, img, filename):
1269-
img = klass.from_image(img)
1270-
img.to_filename(filename)
12711219

12721220

12731221
load = AnalyzeImage.load

nibabel/minc.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,6 @@ def get_shape(self):
255255
def get_data_dtype(self):
256256
return self._header.get_data_dtype()
257257

258-
@classmethod
259-
def from_filespec(klass, filespec):
260-
files = klass.filespec_to_files(filespec)
261-
return klass.from_files(files)
262-
263258
@classmethod
264259
def from_files(klass, files):
265260
fname = files['image']
@@ -269,20 +264,9 @@ def from_files(klass, files):
269264
ret._files = files
270265
return ret
271266

272-
@classmethod
273-
def from_image(klass, img):
274-
return klass(img.get_data(),
275-
img.get_affine(),
276-
img.get_header(),
277-
img.extra)
278-
279267
@staticmethod
280268
def filespec_to_files(filespec):
281269
return {'image':filespec}
282270

283-
@classmethod
284-
def load(klass, filespec):
285-
return klass.from_filespec(filespec)
286-
287271

288272
load = MincImage.load

nibabel/spatialimages.py

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@
1919
* .set_shape(shape)
2020
* .to_filename(fname) - writes data to filename(s) derived from
2121
``fname``, where the derivation may differ between formats.
22-
*
22+
* to_files() - save image to files with which the image is already
23+
associated. Or ``img.to_files(files)`` saves to the files passed.
24+
25+
classmethods:
26+
27+
* from_filename(fname) - make instance by loading from filename
28+
* instance_to_filename(img, fname) - save ``img`` instance to
29+
filename ``fname``.
2330
2431
There are several ways of writing data.
2532
=======================================
@@ -32,6 +39,19 @@
3239
the datatype the header expects, setting any available header scaling
3340
into the header to help the data match.
3441
42+
You can load the data into an image from file with::
43+
44+
img.from_filename(fname)
45+
46+
The image stores its associated files in a rather secretive way. In
47+
order to just save an image, for which you know there is an associated
48+
filename, or other storage, you can do::
49+
50+
img.to_files()
51+
52+
alternatively, you can pass in the needed files yourself, into this
53+
method, as an argument.
54+
3555
You can get the data out again with of::
3656
3757
img.get_data(fileobj)
@@ -41,6 +61,9 @@
4161
4262
unscaled_data = img.get_unscaled_data()
4363
64+
Analyze-type images (including nifti) support this, but others may not
65+
(MINC, for example).
66+
4467
Sometimes you might to avoid any loss of precision by making the
4568
data type the same as the input::
4669
@@ -50,9 +73,12 @@
5073
5174
'''
5275

76+
import warnings
77+
78+
5379
class SpatialImage(object):
5480
_header_maker = dict
55-
''' Template class for lightweight image '''
81+
''' Template class for images '''
5682
def __init__(self, data, affine, header=None, extra=None):
5783
if extra is None:
5884
extra = {}
@@ -108,8 +134,17 @@ def _set_header(self, header=None):
108134
self.extra[key] = value
109135

110136
@classmethod
111-
def from_filespec(klass, filespec):
112-
raise NotImplementedError
137+
def from_filename(klass, filename):
138+
files = klass.filespec_to_files(filename)
139+
return klass.from_files(files)
140+
141+
@classmethod
142+
def from_filespec(klass, img, filespec):
143+
warnings.warn('``from_filespec`` class method is deprecated\n'
144+
'Please use the ``from_filename`` class method '
145+
'instead',
146+
DeprecationWarning)
147+
klass.from_filespec(filespec)
113148

114149
def from_files(klass, files):
115150
raise NotImplementedError
@@ -121,8 +156,82 @@ def from_image(klass, img):
121156
def filespec_to_files(filespec):
122157
raise NotImplementedError
123158

124-
def to_filespec(self, filespec):
125-
raise NotImplementedError
159+
def to_filename(self, filename):
160+
''' Write image to files implied by filename string
161+
162+
Paraameters
163+
-----------
164+
filename : str
165+
filename to which to save image. We will parse `filename`
166+
with ``filespec_to_files`` to work out names for image,
167+
header etc.
168+
169+
Returns
170+
-------
171+
None
172+
'''
173+
files = self.filespec_to_files(filename)
174+
self.to_files(files)
175+
176+
def to_filespec(self, filename):
177+
warnings.warn('``to_filespec`` is deprecated, please '
178+
'use ``to_filename`` instead',
179+
DeprecationWarning)
180+
self.to_filename(filename)
126181

127182
def to_files(self, files=None):
128183
raise NotImplementedError
184+
185+
@classmethod
186+
def load(klass, filename):
187+
return klass.from_filename(filename)
188+
189+
@classmethod
190+
def save(klass, img, filename):
191+
warnings.warn('``save`` class method is deprecated\n'
192+
'You probably want the ``to_filename`` instance '
193+
'method, or the module-level ``save`` function',
194+
DeprecationWarning)
195+
klass.instance_to_filename(img, filename)
196+
197+
@classmethod
198+
def instance_to_filename(klass, img, filename):
199+
''' Save `img` in our own format, to name implied by `filename`
200+
201+
This is a class method
202+
203+
Parameters
204+
----------
205+
img : ``spatialimage`` instance
206+
In fact, an object with the API of ``spatialimage`` -
207+
specifically ``get_data``, ``get_affine``, ``get_header`` and
208+
``extra``.
209+
filename : str
210+
Filename, implying name to which to save image.
211+
'''
212+
img = klass.from_image(img)
213+
img.to_filename(filename)
214+
215+
@classmethod
216+
def from_image(klass, img):
217+
''' Create new instance of own class from `img`
218+
219+
This is a class method
220+
221+
Parameters
222+
----------
223+
img : ``spatialimage`` instance
224+
In fact, an object with the API of ``spatialimage`` -
225+
specifically ``get_data``, ``get_affine``, ``get_header`` and
226+
``extra``.
227+
228+
Returns
229+
-------
230+
cimg : ``spatialimage`` instance
231+
Image, of our own class
232+
'''
233+
return klass(img.get_data(),
234+
img.get_affine(),
235+
img.get_header(),
236+
img.extra)
237+

nibabel/spm99analyze.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ def _chk_origin(hdr, fix=True):
214214
class Spm99AnalyzeImage(analyze.AnalyzeImage):
215215
_header_maker = Spm99AnalyzeHeader
216216
@classmethod
217-
def from_filespec(klass, filespec):
218-
ret = super(Spm99AnalyzeImage, klass).from_filespec(filespec)
217+
def from_filename(klass, filename):
218+
ret = super(Spm99AnalyzeImage, klass).from_filename(filename)
219219
import scipy.io as sio
220220
matf = ret._files['mat']
221221
try:

0 commit comments

Comments
 (0)