Skip to content

Commit

Permalink
Fix bus error when saving Nifti data under same name it was loaded from.
Browse files Browse the repository at this point in the history
See nipy/nibabel#492 for probable explanation
  • Loading branch information
mcraig-ibme committed Apr 8, 2019
1 parent 770410f commit d953c5f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions quantiphyse/data/nifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ def __init__(self, fname):
QpData.__init__(self, fname, grid, nvols, vol_unit=vol_units, vol_scale=vol_scale, fname=fname, metadata=metadata)

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

self.voldata = None
Expand Down
10 changes: 10 additions & 0 deletions quantiphyse/test/qpd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,15 @@ def testSaveLoadMetadata(self):
self.assertTrue(key in nifti_data.metadata)
self.assertEqual(nifti_data.metadata[key], value)

def testLoadSaveSameName(self):
qpd = NumpyData(self.floats4d, grid=self.grid, name="test")

tempdir = tempfile.mkdtemp(prefix="qp")
fname = os.path.join(tempdir, "test.nii")
nifti.save(qpd, fname)

nifti_data = nifti.NiftiData(fname)
nifti.save(nifti_data, fname)

if __name__ == '__main__':
unittest.main()

0 comments on commit d953c5f

Please sign in to comment.