Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused map iter_by_pix and iter_by_coord methods #2206

Merged
merged 1 commit into from Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 1 addition & 21 deletions docs/maps/index.rst
Expand Up @@ -405,29 +405,9 @@ of the original map will be copied over to the projected map.

.. _mapiter:

Iterating on a Map
Iterating by image
------------------

Iterating over a map can be performed with the `~Map.iter_by_coord` and
`~Map.iter_by_pix` methods. These return an iterator that traverses the map
returning (value, coordinate) pairs with map and pixel coordinates,
respectively. The optional ``buffersize`` argument can be used to split the
iteration into chunks of a given size. The following example illustrates how
one can use this method to fill a map with a 2D Gaussian:

.. code:: python

import numpy as np
from astropy.coordinates import SkyCoord
from gammapy.maps import Map

m = Map.create(binsz=0.05, map_type='wcs', width=10.0)
for val, coord in m.iter_by_coord(buffersize=10000):
skydir = SkyCoord(coord[0],coord[1], unit='deg')
sep = skydir.separation(m.geom.center_skydir).deg
new_val = np.exp(-sep**2/2.0)
m.set_by_coord(coord, new_val)

For maps with non-spatial dimensions the `~Map.iter_by_image` method can be used
to loop over image slices. The image plane index `idx` is returned in data order,
so that the data array can be indexed directly. Here is an example for an in-place
Expand Down
52 changes: 1 addition & 51 deletions gammapy/maps/base.py
Expand Up @@ -9,7 +9,7 @@
from astropy.utils.misc import InheritDocstrings
from astropy.io import fits
from .geom import pix_tuple_to_idx, MapCoord
from .utils import unpack_seq, INVALID_VALUE
from .utils import INVALID_VALUE
from ..utils.scripts import make_path

__all__ = ["Map"]
Expand Down Expand Up @@ -340,56 +340,6 @@ def iter_by_image(self):
for idx in np.ndindex(self.geom.shape_axes):
yield self.data[idx[::-1]], idx[::-1]

def iter_by_pix(self, buffersize=1):
"""Iterate over elements of the map.

Generator yielding tuples with values and pixel coordinates.

Parameters
----------
buffersize : int
Set the size of the buffer. The map will be returned in
chunks of the given size.

Returns
-------
val : `~numpy.ndarray`
Map values.
pix : tuple
Tuple of pixel coordinates.
"""
pix = list(self.geom.get_idx(flat=True))
vals = self.data[np.isfinite(self.data)]
x = [vals] + pix
return unpack_seq(
np.nditer(x, flags=["external_loop", "buffered"], buffersize=buffersize)
)

def iter_by_coord(self, buffersize=1):
"""Iterate over elements of the map.

Generator yielding tuples with values and map coordinates.

Parameters
----------
buffersize : int
Set the size of the buffer. The map will be returned in
chunks of the given size.

Returns
-------
val : `~numpy.ndarray`
Map values.
coords : tuple
Tuple of map coordinates.
"""
coords = list(self.geom.get_coord(flat=True))
vals = self.data[np.isfinite(self.data)]
x = [vals] + coords
return unpack_seq(
np.nditer(x, flags=["external_loop", "buffered"], buffersize=buffersize)
)

@abc.abstractmethod
def sum_over_axes(self, keepdims=False):
"""Reduce to a 2D image by summing over non-spatial dimensions."""
Expand Down
6 changes: 0 additions & 6 deletions gammapy/maps/hpxsparse.py
Expand Up @@ -149,12 +149,6 @@ def _make_cols(self, header, conv):
def iter_by_image(self):
raise NotImplementedError

def iter_by_pix(self):
raise NotImplementedError

def iter_by_coord(self):
raise NotImplementedError

def sum_over_axes(self):
raise NotImplementedError

Expand Down
15 changes: 0 additions & 15 deletions gammapy/maps/tests/test_hpxmap.py
Expand Up @@ -228,21 +228,6 @@ def test_hpxmap_fill_by_coord(nside, nested, coordsys, region, axes, sparse):
assert_allclose(m.get_by_coord(coords), 2.0 * coords[1])


@pytest.mark.parametrize(
("nside", "nested", "coordsys", "region", "axes"), hpx_test_geoms
)
def test_hpxmap_iter(nside, nested, coordsys, region, axes):
m = HpxNDMap(
HpxGeom(nside=nside, nest=nested, coordsys=coordsys, region=region, axes=axes)
)
coords = m.geom.get_coord(flat=True)
m.fill_by_coord(coords, coords[0])
for vals, pix in m.iter_by_pix(buffersize=100):
assert_allclose(vals, m.get_by_pix(pix))
for vals, coords in m.iter_by_coord(buffersize=100):
assert_allclose(vals, m.get_by_coord(coords))


@pytest.mark.parametrize(
("nside", "nested", "coordsys", "region", "axes"), hpx_test_geoms
)
Expand Down
16 changes: 0 additions & 16 deletions gammapy/maps/tests/test_wcsnd.py
Expand Up @@ -318,22 +318,6 @@ def test_wcsndmap_interp_by_coord_fill_value():
assert_allclose(m.interp_by_coord((99, 0)), 42)


@pytest.mark.parametrize(
("npix", "binsz", "coordsys", "proj", "skydir", "axes"), wcs_test_geoms
)
def test_wcsndmap_iter(npix, binsz, coordsys, proj, skydir, axes):
geom = WcsGeom.create(
npix=npix, binsz=binsz, proj=proj, coordsys=coordsys, axes=axes
)
m = WcsNDMap(geom)
coords = m.geom.get_coord()
m.fill_by_coord(coords, coords[0])
for vals, pix in m.iter_by_pix(buffersize=100):
assert_allclose(vals, m.get_by_pix(pix))
for vals, coords in m.iter_by_coord(buffersize=100):
assert_allclose(vals, m.get_by_coord(coords))


@pytest.mark.parametrize(
("npix", "binsz", "coordsys", "proj", "skydir", "axes"), wcs_test_geoms
)
Expand Down
19 changes: 0 additions & 19 deletions gammapy/maps/utils.py
Expand Up @@ -87,25 +87,6 @@ def interp_to_order(interp):
return order_map.get(interp, None)


def unpack_seq(seq, n=1):
"""Utility to unpack the first N values of a tuple or list.

Remaining values are put into a single list which is the last element of the
return value. This partially simulates the extended unpacking
functionality available in Python 3.

Parameters
----------
seq : list or tuple
Input sequence to be unpacked.
n : int
Number of elements of ``seq`` to unpack. Remaining elements
are put into a single tuple.
"""
for row in seq:
yield [e for e in row[:n]] + [row[n:]]


def find_bands_hdu(hdu_list, hdu):
"""Discover the extension name of the BANDS HDU.

Expand Down