Skip to content

Commit

Permalink
Merge branch 'enh/slicing_methods'
Browse files Browse the repository at this point in the history
  • Loading branch information
DBerke committed Oct 20, 2022
2 parents d956a42 + dade5e1 commit 64acbc0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
10 changes: 10 additions & 0 deletions astrodata/nddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,13 @@ def transpose(self):
mask=None if self.mask is None else self.mask.T, wcs=new_wcs,
meta=self.meta, copy=False
)

def _slice(self, item):
"""Additionally slice things like OBJMASK"""
kwargs = super()._slice(item)
if 'other' in kwargs['meta']:
kwargs['meta'] = deepcopy(self.meta)
for k, v in kwargs['meta']['other'].items():
if isinstance(v, np.ndarray) and v.shape == self.shape:
kwargs['meta']['other'][k] = v[item]
return kwargs
11 changes: 11 additions & 0 deletions astrodata/tests/test_nddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,16 @@ def test_access_to_other_planes_when_windowed(testnd):
assert len(ndwindow.OBJCAT) == 3


# Basically the same test as above but using slicing.
def test_access_to_other_planes_when_sliced(testnd):
ndwindow = testnd[1:, 1:]
assert ndwindow.data.shape == (4, 4)
assert ndwindow.data[0, 0] == testnd.shape[1] + 1
assert ndwindow.OBJMASK.shape == (4, 4)
assert ndwindow.OBJMASK[0, 0] == testnd.shape[1] + 1
assert isinstance(ndwindow.OBJCAT, Table)
assert len(ndwindow.OBJCAT) == 3


if __name__ == '__main__':
pytest.main()
17 changes: 10 additions & 7 deletions gempy/gemini/gemini_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ def mark_history(adinput=None, keyword=None, primname=None, comment=None):


def measure_bg_from_image(ad, sampling=10, value_only=False, gaussfit=True,
separate_ext=True, ignore_mask=False):
separate_ext=True, ignore_mask=False, section=None):
"""
Return background value, and its std deviation, as measured directly
from pixels in the SCI image. DQ plane are used (if they exist)
Expand All @@ -1683,6 +1683,8 @@ def measure_bg_from_image(ad, sampling=10, value_only=False, gaussfit=True,
return information for each extension, rather than the whole AD?
ignore_mask : bool
if True, ignore the mask and OBJMASK
section: slice/None
region to use for statistics
Returns
-------
Expand All @@ -1704,17 +1706,18 @@ def measure_bg_from_image(ad, sampling=10, value_only=False, gaussfit=True,
flags = None
# Use DQ and OBJMASK to flag pixels
if not single and not separate_ext:
bg_data = np.array([ext.data for ext in ad]).ravel()
bg_data = np.array([ext.data[section] for ext in ad]).ravel()
if not ignore_mask:
flags = np.array([ext.mask | getattr(ext, 'OBJMASK', 0)
if ext.mask is not None
else getattr(ext, 'OBJMASK', np.empty_like(ext.data, dtype=bool))
for ext in ad]).ravel()
else getattr(ext, 'OBJMASK', np.zeros_like(ext.data, dtype=bool))
for ext in ad])[section].ravel()
else:
if not ignore_mask:
flags = ext.mask | getattr(ext, 'OBJMASK', 0) if ext.mask is not None \
else getattr(ext, 'OBJMASK', None)
bg_data = ext.data.ravel()
flags = ((ext.mask | getattr(ext, 'OBJMASK', 0))[section]
if ext.mask is not None else
ext.OBJMASK[section] if hasattr(ext, 'OBJMASK') else None)
bg_data = ext.data[section].ravel()

if flags is None:
bg_data = bg_data[::sampling]
Expand Down
36 changes: 30 additions & 6 deletions gempy/gemini/tests/test_gemini_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
('N20180118S0344.fits', 1.32),
]

# For making a fake test image, and testing it.
datavalue = 10000

caltype_data = [
('arc', 'Arc spectrum'),
Expand All @@ -25,6 +27,16 @@
('flat', 'Flat Frame'),
]

@pytest.fixture
def fake_image(astrofaker):
np.random.seed(4) # A number that works with all the later tests
ad = astrofaker.create('NIRI', ['IMAGE'])
ad.init_default_extensions()
ad[0].data += datavalue
ad.add_poisson_noise()

return ad


@pytest.mark.dragons_remote_data
@pytest.mark.parametrize("caltype, obj", caltype_data)
Expand Down Expand Up @@ -74,18 +86,30 @@ def test_fit_continuum_slit_image(fname, fwhm, change_working_dir):


@pytest.mark.parametrize("gaussfit", [True, False])
def test_measure_bg_from_image_fake(gaussfit, astrofaker):
datavalue = 10000
ad = astrofaker.create('NIRI', ['IMAGE'])
ad.init_default_extensions()
ad[0].data += datavalue
ad.add_poisson_noise()
def test_measure_bg_from_image_fake(gaussfit, fake_image):

ad = fake_image
mean, stddev, nsamples = gt.measure_bg_from_image(ad[0], gaussfit=gaussfit)
assert abs(mean - datavalue) < 1
if gaussfit: # stddev unreliable for non-Gaussfit
assert abs(stddev - np.sqrt(datavalue / ad[0].gain())) < 0.5


@pytest.mark.parametrize("section", [(slice(0, -1), slice(0, -1)),
(slice(None, 512), slice(512, None)),
(slice(None, None), slice(None, None)),
(slice(-512, None), slice(None, -512))])
@pytest.mark.parametrize("gaussfit", [True, False])
def test_measure_bg_from_image_fake_sections(section, gaussfit, fake_image):

ad = fake_image
mean, stddev, nsamples = gt.measure_bg_from_image(ad[0], gaussfit=gaussfit,
section=section)
assert abs(mean - datavalue) < 1
if gaussfit: # stddev unreliable for non-Gaussfit
assert abs(stddev - np.sqrt(datavalue / ad[0].gain())) < 0.5


@pytest.mark.dragons_remote_data
@pytest.mark.parametrize("gaussfit", [True, False])
def test_measure_bg_from_image_real(gaussfit):
Expand Down

0 comments on commit 64acbc0

Please sign in to comment.