From af10cb4c4d680069860be407f1a5d7d0acf031e9 Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Sun, 9 Aug 2015 18:54:56 +0200 Subject: [PATCH 1/5] provide the 'base' of self so that things like joblib can operate on spectral cubes, understanding that they are memmaps. For a related reason, lower dimensional slices should return _data[view], not filled_data[view], BUT this doesn't provide the right functionality so we want lower dimensional objects to implement their own cubelike masking --- spectral_cube/spectral_cube.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/spectral_cube/spectral_cube.py b/spectral_cube/spectral_cube.py index ca8f0cad4..0bf7b8468 100644 --- a/spectral_cube/spectral_cube.py +++ b/spectral_cube/spectral_cube.py @@ -242,6 +242,11 @@ def size(self): """ Number of elements in the cube """ return self._data.size + @property + def base(self): + """ The data type 'base' of the cube - useful for, e.g., joblib """ + return self._data.base + def __len__(self): return self.shape[0] @@ -943,7 +948,7 @@ def __getitem__(self, view): newwcs = self._wcs.sub([a for a in (1,2,3) if a not in [x+1 for x in intslices]]) - return OneDSpectrum(value=self.filled_data[view], + return OneDSpectrum(value=self._data[view], wcs=newwcs, copy=False, unit=self.unit, From 7883d2359caee38c37d6c89b90ab9bd95a3cdcfb Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Sat, 15 Aug 2015 23:24:53 +0200 Subject: [PATCH 2/5] add a memmap test, which is related to #226 --- spectral_cube/tests/test_spectral_cube.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spectral_cube/tests/test_spectral_cube.py b/spectral_cube/tests/test_spectral_cube.py index d2954301d..e4d61638d 100644 --- a/spectral_cube/tests/test_spectral_cube.py +++ b/spectral_cube/tests/test_spectral_cube.py @@ -601,6 +601,14 @@ def test_read_write_rountrip(tmpdir): # maximized assert cube._wcs.to_header_string() == cube2._wcs.to_header_string() +@pytest.mark.parametrize(('memmap', 'base'), + ((True, np.memmap), + (False, None))) +def test_read_memmap(memmap, base): + cube = SpectralCube.read(path('adv.fits'), memmap=memmap) + + assert cube.base == base + def _dummy_cube(): data = np.array([[[0, 1, 2, 3, 4]]]) From d67d84cdac82817091fd26f759356d64ee2c0df0 Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Sun, 2 Aug 2015 13:01:58 +0200 Subject: [PATCH 3/5] pass kwargs to fits reader --- spectral_cube/io/fits.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spectral_cube/io/fits.py b/spectral_cube/io/fits.py index e3701b69f..94263d171 100644 --- a/spectral_cube/io/fits.py +++ b/spectral_cube/io/fits.py @@ -96,7 +96,7 @@ def read_data_fits(input, hdu=None, **kwargs): return array_hdu.data, array_hdu.header -def load_fits_cube(input, hdu=0, meta={}): +def load_fits_cube(input, hdu=0, meta={}, **kwargs): """ Read in a cube from a FITS file using astropy. @@ -110,7 +110,7 @@ def load_fits_cube(input, hdu=0, meta={}): Metadata (can be inherited from other readers, for example) """ - data, header = read_data_fits(input, hdu=hdu) + data, header = read_data_fits(input, hdu=hdu, **kwargs) if 'BUNIT' in header: meta['BUNIT'] = header['BUNIT'] From c6097a1adda68a12528ee5a430d59aaac886aa91 Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Sat, 15 Aug 2015 23:31:31 +0200 Subject: [PATCH 4/5] correct the `base` checking tests --- spectral_cube/tests/test_spectral_cube.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spectral_cube/tests/test_spectral_cube.py b/spectral_cube/tests/test_spectral_cube.py index e4d61638d..8c4af4da4 100644 --- a/spectral_cube/tests/test_spectral_cube.py +++ b/spectral_cube/tests/test_spectral_cube.py @@ -2,6 +2,7 @@ import operator import itertools import warnings +import mmap # needed to test for warnings later warnings.simplefilter('always', UserWarning) @@ -602,12 +603,19 @@ def test_read_write_rountrip(tmpdir): assert cube._wcs.to_header_string() == cube2._wcs.to_header_string() @pytest.mark.parametrize(('memmap', 'base'), - ((True, np.memmap), + ((True, mmap.mmap), (False, None))) def test_read_memmap(memmap, base): cube = SpectralCube.read(path('adv.fits'), memmap=memmap) - assert cube.base == base + bb = cube.base + while hasattr(bb, 'base'): + bb = bb.base + + if base is None: + assert bb is None + else: + assert isinstance(bb, base) def _dummy_cube(): From d476726cdd479023a24d74f8710ef5ac7230a378 Mon Sep 17 00:00:00 2001 From: "Adam Ginsburg (keflavich)" Date: Sat, 15 Aug 2015 23:36:11 +0200 Subject: [PATCH 5/5] tiny whitespace tweak --- spectral_cube/masks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spectral_cube/masks.py b/spectral_cube/masks.py index d32d7fa51..dba6712b1 100644 --- a/spectral_cube/masks.py +++ b/spectral_cube/masks.py @@ -188,7 +188,8 @@ def __invert__(self): return InvertedMask(self) def __getitem__(self): - raise NotImplementedError("Slicing not supported by mask class {0}".format(self.__class__.__name__)) + raise NotImplementedError("Slicing not supported by mask class {0}" + .format(self.__class__.__name__)) def quicklook(self, view, wcs=None, filename=None, use_aplpy=True): '''