Skip to content

Commit d953c5f

Browse files
committed
Fix bus error when saving Nifti data under same name it was loaded from.
See nipy/nibabel#492 for probable explanation
1 parent 770410f commit d953c5f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

quantiphyse/data/nifti.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ def __init__(self, fname):
6565
QpData.__init__(self, fname, grid, nvols, vol_unit=vol_units, vol_scale=vol_scale, fname=fname, metadata=metadata)
6666

6767
def raw(self):
68-
# NB: np.asarray convert data to an in-memory array instead of a numpy file memmap.
68+
# NB: copy() converts data to an in-memory array instead of a numpy file memmap.
6969
# Appears to improve speed drastically as well as stop a bug with accessing the subset of the array
7070
# memmap has been designed to save space on ram by keeping the array on the disk but does
7171
# horrible things with performance, and analysis especially when the data is on the network.
7272
if self.rawdata is None:
7373
nii = nib.load(self.fname)
74-
self.rawdata = np.asarray(nii.get_data())
74+
self.rawdata = nii.get_data().copy()
7575
self.rawdata = self._correct_dims(self.rawdata)
7676

7777
self.voldata = None

quantiphyse/test/qpd_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,5 +138,15 @@ def testSaveLoadMetadata(self):
138138
self.assertTrue(key in nifti_data.metadata)
139139
self.assertEqual(nifti_data.metadata[key], value)
140140

141+
def testLoadSaveSameName(self):
142+
qpd = NumpyData(self.floats4d, grid=self.grid, name="test")
143+
144+
tempdir = tempfile.mkdtemp(prefix="qp")
145+
fname = os.path.join(tempdir, "test.nii")
146+
nifti.save(qpd, fname)
147+
148+
nifti_data = nifti.NiftiData(fname)
149+
nifti.save(nifti_data, fname)
150+
141151
if __name__ == '__main__':
142152
unittest.main()

0 commit comments

Comments
 (0)