Skip to content

Commit

Permalink
Test memory mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
sjperkins committed Mar 21, 2024
1 parent fbfa424 commit 3aaaf85
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions tests/test_xarrayfits.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Tests for `xarrayfits` package."""

from contextlib import ExitStack
import mmap
import os.path

from astropy.io import fits
Expand All @@ -14,6 +15,7 @@
import xarray

from xarrayfits import xds_from_fits
from xarrayfits.fits_proxy import FitsProxy


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -200,3 +202,34 @@ def test_distributed(beam_cube):
expected = np.arange(np.prod(xds.hdu0.shape)).reshape(xds.hdu0.shape)
assert_array_equal(expected, xds.hdu0.data)
assert xds.hdu0.data.chunks == ((100, 100, 57), (100, 100, 57), (15, 15, 2))


def test_memory_mapped(beam_cube):
with fits.open(beam_cube, memmap=True) as hdu_list:
hdu_list[0].data[:]
astropy_file = hdu_list.fileinfo(0)["file"]
assert isinstance(astropy_file._mmap, mmap.mmap)

proxy = FitsProxy(beam_cube)
assert proxy.is_memory_mapped
proxy.hdu_list[0].data[:]
astropy_file = proxy.hdu_list.fileinfo(0)["file"]
assert isinstance(astropy_file._mmap, mmap.mmap)

proxy = FitsProxy(beam_cube, memmap=True)
assert proxy.is_memory_mapped
proxy.hdu_list[0].data[:]
astropy_file = proxy.hdu_list.fileinfo(0)["file"]
assert isinstance(astropy_file._mmap, mmap.mmap)

proxy = FitsProxy(beam_cube, memmap=None)
assert proxy.is_memory_mapped
proxy.hdu_list[0].data[:]
astropy_file = proxy.hdu_list.fileinfo(0)["file"]
assert isinstance(astropy_file._mmap, mmap.mmap)

proxy = FitsProxy(beam_cube, memmap=False)
assert not proxy.is_memory_mapped
proxy.hdu_list[0].data[:]
astropy_file = proxy.hdu_list.fileinfo(0)["file"]
assert astropy_file._mmap is None
2 changes: 1 addition & 1 deletion xarrayfits/fits_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def from_reduce_args(filename, kw):

@property
def is_memory_mapped(self):
return self._kwargs.get("memmap", False)
return self._kwargs.get("memmap") in {None, True}

@property
def hdu_list(self):
Expand Down

0 comments on commit 3aaaf85

Please sign in to comment.