Skip to content

Commit 52bfb15

Browse files
committed
fix: moving save from numpy to pickle
1 parent e57cad6 commit 52bfb15

File tree

4 files changed

+23
-43
lines changed

4 files changed

+23
-43
lines changed

nipype/pipeline/engine.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,15 @@ def __repr__(self):
205205

206206
def save(self, filename=None):
207207
if filename is None:
208-
filename = 'temp.npz'
209-
np.savez(filename, object=self)
208+
filename = 'temp.pklz'
209+
savepkl(filename, self)
210210

211211
def load(self, filename):
212-
return np.load(filename)
212+
if '.npz' in filename:
213+
DeprecationWarning(('npz files will be deprecated in the next '
214+
'release. you can use numpy to open them.'))
215+
return np.load(filename)
216+
return loadpkl(filename)
213217

214218

215219
class Workflow(WorkflowBase):

nipype/pipeline/plugins/base.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from copy import deepcopy
77
from glob import glob
88
import os
9+
import pickle
910
import pwd
1011
import shutil
1112
from socket import gethostname
@@ -55,7 +56,7 @@ def report_crash(node, traceback=None, hostname=None):
5556
exc_traceback)
5657
timeofcrash = strftime('%Y%m%d-%H%M%S')
5758
login_name = pwd.getpwuid(os.geteuid())[0]
58-
crashfile = 'crash-%s-%s-%s.npz' % (timeofcrash,
59+
crashfile = 'crash-%s-%s-%s.pklz' % (timeofcrash,
5960
login_name,
6061
name)
6162
crashdir = node.config['execution']['crashdump_dir']
@@ -66,7 +67,8 @@ def report_crash(node, traceback=None, hostname=None):
6667
crashfile = os.path.join(crashdir, crashfile)
6768
logger.info('Saving crash info to %s' % crashfile)
6869
logger.info(''.join(traceback))
69-
np.savez(crashfile, node=node, traceback=traceback)
70+
savepkl(crashfile, dict(node=node, traceback=traceback))
71+
#np.savez(crashfile, node=node, traceback=traceback)
7072
return crashfile
7173

7274

nipype/utils/filemanip.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -378,30 +378,21 @@ def load_json(filename):
378378
fp.close()
379379
return data
380380

381-
382-
def loadflat(infile, *args):
383-
"""Load an npz file into a dict
384-
"""
385-
data = np.load(infile)
386-
out = {}
387-
if args:
388-
outargs = np.setdiff1d(args, data.files)
389-
if outargs:
390-
raise IOError('File does not contain variables: '+str(outargs))
391-
for k in data.files:
392-
if k in args or not args:
393-
out[k] = [f for f in data[k].flat]
394-
if len(out[k]) == 1:
395-
out[k] = out[k].pop()
396-
return out
397-
398-
399381
def loadcrash(infile, *args):
400382
if '.pkl' in infile:
401383
return loadpkl(infile)
384+
elif '.npz' in infile:
385+
DeprecationWarning(('npz files will be deprecated in the next '
386+
'release. you can use numpy to open them.'))
387+
data = np.load(infile)
388+
out = {}
389+
for k in data.files:
390+
out[k] = [f for f in data[k].flat]
391+
if len(out[k]) == 1:
392+
out[k] = out[k].pop()
393+
return out
402394
else:
403-
return loadflat(infile, *args)
404-
395+
raise ValueError('Only pickled crashfiles are supported')
405396

406397
def loadpkl(infile):
407398
"""Load a zipped or plain cPickled file

nipype/utils/tests/test_filemanip.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from tempfile import mkstemp, mkdtemp
55

66
from nipype.testing import assert_equal, assert_true, assert_false
7-
from nipype.utils.filemanip import (save_json, load_json, loadflat,
7+
from nipype.utils.filemanip import (save_json, load_json,
88
fname_presuffix, fnames_presuffix,
99
hash_rename, check_forhash,
1010
copyfile, copyfiles,
@@ -143,23 +143,6 @@ def test_json():
143143
os.unlink(name)
144144
yield assert_equal, sorted(adict.items()), sorted(new_dict.items())
145145

146-
def test_loadflat():
147-
alist = [dict(a='one', c='three', b='two'),
148-
dict(a='one', c='three', b='two')]
149-
fd, name = mkstemp(suffix='.npz')
150-
np.savez(name,a=alist)
151-
aloaded = loadflat(name)['a']
152-
os.unlink(name)
153-
yield assert_equal, len(aloaded), 2
154-
yield assert_equal, sorted(aloaded[0].items()), sorted(alist[0].items())
155-
adict = dict(a='one', c='three', b='two')
156-
fd, name = mkstemp(suffix='.npz')
157-
np.savez(name,a=adict)
158-
aloaded = loadflat(name)['a']
159-
os.unlink(name)
160-
yield assert_true, isinstance(aloaded, dict)
161-
yield assert_equal, sorted(aloaded.items()), sorted(adict.items())
162-
163146
def test_related_files():
164147
file1 = '/path/test.img'
165148
file2 = '/path/test.hdr'

0 commit comments

Comments
 (0)