Skip to content

Commit

Permalink
rename superpixel to rebin
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed May 8, 2024
1 parent 306d6d7 commit cf83800
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion docs/tutorial/maps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ For example, we can combine 4 pixels in each dimension such that our new superpi
:include-source:
:context: close-figs

my_super_submap = my_submap.superpixel((5,5)*u.pixel)
my_super_submap = my_submap.rebin((5,5)*u.pixel)

fig = plt.figure()
ax = fig.add_subplot(projection=my_super_submap)
Expand Down
18 changes: 9 additions & 9 deletions examples/map/map_resampling_and_superpixels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
===============
How to resample a map using the resample method, which implements interpolation, or
using superpixels, which combines pixels.
using rebin, which combines pixels.
"""
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -37,19 +37,19 @@

##############################################################################
# Another way to reduce the angular resolution of the map is by using the
# :meth:`~sunpy.map.GenericMap.superpixel` method, which combines pixels.
# The superpixel dimensions do not need to be square, and the intensity of
# each superpixel defaults to the sum of the constituent pixels. For example,
# :meth:`~sunpy.map.GenericMap.rebin` method, which can combine pixels.
# The rebin dimensions do not need to be square, and the intensity of
# each rebin defaults to the sum of the constituent pixels. For example,
# you can reduce the AIA map resolution by a factor of 16 by specifying 16x16
# superpixels.
# bin size.

superpixel_size = [16, 16] * u.pixel
aia_superpixel_map = aia_map.superpixel(superpixel_size)
rebin_size = [16, 16] * u.pixel
aia_rebin_map = aia_map.rebin(rebin_size)

##############################################################################
# Let's plot the result.

fig = plt.figure()
ax = fig.add_subplot(projection=aia_superpixel_map)
aia_superpixel_map.plot(axes=ax)
ax = fig.add_subplot(projection=aia_rebin_map)
aia_rebin_map.plot(axes=ax)
plt.show()
2 changes: 0 additions & 2 deletions sunpy/map/mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,8 +1198,6 @@ def _parse_submap_coord_input(self, bottom_left, top_right, width, height):

return tuple(u.Quantity(self.wcs.world_to_pixel(corners), u.pix).T)

# #### Visualization #### #

@property
def cmap(self):
return self.plotter.cmap
Expand Down
58 changes: 29 additions & 29 deletions sunpy/map/tests/test_mapbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,38 +919,38 @@ def test_resample_simple_map(simple_map, sample_method, new_dimensions):
simple_map.meta['crpix1'] = 1.5
simple_map.meta['crpix2'] = 1.5
assert list(simple_map.reference_pixel) == [0.5 * u.pix, 0.5 * u.pix]
# Make the superpixel map
# Make the rebin map
new_dims = (9, 6) * u.pix
resamp_map = simple_map.resample(new_dims, method=sample_method)
# Reference pixel should change, but reference coordinate should not
assert list(resamp_map.reference_pixel) == [2.5 * u.pix, 1.5 * u.pix]
assert resamp_map.reference_coordinate == simple_map.reference_coordinate


def test_superpixel_simple_map(simple_map):
def test_rebin_simple_map(simple_map):
# Put the reference pixel at the top-right of the bottom-left pixel
simple_map.meta['crpix1'] = 1.5
simple_map.meta['crpix2'] = 1.5
assert list(simple_map.reference_pixel) == [0.5 * u.pix, 0.5 * u.pix]
# Make the superpixel map
# Make the rebin map
new_dims = (2, 2) * u.pix
superpix_map = simple_map.superpixel(new_dims)
superpix_map = simple_map.rebin(new_dims)
# Reference pixel should change, but reference coordinate should not
assert list(superpix_map.reference_pixel) == [0 * u.pix, 0 * u.pix]
assert superpix_map.reference_coordinate == simple_map.reference_coordinate

# Check that offset works
superpix_map = simple_map.superpixel(new_dims, offset=[1, 2] * u.pix)
superpix_map = simple_map.rebin(new_dims, offset=[1, 2] * u.pix)

Check warning on line 943 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L943

Added line #L943 was not covered by tests
# Reference pixel should change, but reference coordinate should not
assert u.allclose(list(superpix_map.reference_pixel),
[-0.5 * u.pix, -1 * u.pix])
assert superpix_map.reference_coordinate == simple_map.reference_coordinate


@pytest.mark.parametrize('f', [np.sum, np.mean])
def test_superpixel_dims_values(aia171_test_map, f):
def test_rebin_dims_values(aia171_test_map, f):
dimensions = (2, 2) * u.pix
superpix_map = aia171_test_map.superpixel(dimensions, func=f)
superpix_map = aia171_test_map.rebin(dimensions, func=f)

# Check dimensions of new map
old_dims = u.Quantity(aia171_test_map.shape)
Expand All @@ -965,8 +965,8 @@ def test_superpixel_dims_values(aia171_test_map, f):

@pytest.mark.parametrize(("f", "dimensions"), [(np.sum, (2, 3)*u.pix),
(np.mean, (3, 2)*u.pix)])
def test_superpixel_metadata(generic_map, f, dimensions):
superpix_map = generic_map.superpixel(dimensions, func=f)
def test_rebin_metadata(generic_map, f, dimensions):
superpix_map = generic_map.rebin(dimensions, func=f)

scale_x, scale_y = dimensions.value

Expand All @@ -989,48 +989,48 @@ def test_superpixel_metadata(generic_map, f, dimensions):
assert superpix_map.meta[key] == generic_map.meta[key]


def test_superpixel_masked(aia171_test_map_with_mask):
def test_rebin_masked(aia171_test_map_with_mask):
input_dims = aia171_test_map_with_mask.shape
dimensions = (2, 2) * u.pix
# Test that the mask is respected
superpix_map = aia171_test_map_with_mask.superpixel(dimensions)
superpix_map = aia171_test_map_with_mask.rebin(dimensions)
assert superpix_map.mask is not None
# Check the shape of the mask
expected_shape = input_dims * (1 * u.pix / dimensions)
assert np.all(superpix_map.mask.shape == expected_shape)

# Test that the offset is respected
superpix_map = aia171_test_map_with_mask.superpixel(dimensions, offset=(1, 1) * u.pix)
superpix_map = aia171_test_map_with_mask.rebin(dimensions, offset=(1, 1) * u.pix)

Check warning on line 1003 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1003

Added line #L1003 was not covered by tests
assert superpix_map.shape[0] == expected_shape[1] - 1 #* u.pix
assert superpix_map.shape[1] == expected_shape[0] - 1 #* u.pix

dimensions = (7, 9) * u.pix
superpix_map = aia171_test_map_with_mask.superpixel(dimensions, offset=(4, 4) * u.pix)
superpix_map = aia171_test_map_with_mask.rebin(dimensions, offset=(4, 4) * u.pix)

Check warning on line 1008 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1008

Added line #L1008 was not covered by tests
expected_shape = np.round(input_dims * (1 * u.pix / dimensions))
assert superpix_map.shape[0] == expected_shape[1] - 1 #* u.pix
assert superpix_map.shape[1] == expected_shape[0] - 1 #* u.pix


def test_superpixel_units(generic_map):
def test_rebin_units(generic_map):
new_dims = (2, 2) * u.pix
super1 = generic_map.superpixel(new_dims)
super2 = generic_map.superpixel(new_dims.to(u.kpix))
super1 = generic_map.rebin(new_dims)
super2 = generic_map.rebin(new_dims.to(u.kpix))

Check warning on line 1017 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1017

Added line #L1017 was not covered by tests
assert super1.meta == super2.meta

offset = (1, 2) * u.pix
super1 = generic_map.superpixel(new_dims, offset=offset)
super2 = generic_map.superpixel(new_dims, offset=offset.to(u.kpix))
super1 = generic_map.rebin(new_dims, offset=offset)
super2 = generic_map.rebin(new_dims, offset=offset.to(u.kpix))

Check warning on line 1022 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1021-L1022

Added lines #L1021 - L1022 were not covered by tests
assert super1.meta == super2.meta


def test_superpixel_fractional_inputs(generic_map):
super1 = generic_map.superpixel((2, 3) * u.pix)
super2 = generic_map.superpixel((2.2, 3.2) * u.pix)
def test_rebin_fractional_inputs(generic_map):
super1 = generic_map.rebin((2, 3) * u.pix)
super2 = generic_map.rebin((2.2, 3.2) * u.pix)

Check warning on line 1028 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1028

Added line #L1028 was not covered by tests
assert np.all(super1.data == super2.data)
assert super1.meta == super2.meta


@pytest.mark.parametrize('method', ['resample', 'superpixel'])
@pytest.mark.parametrize('method', ['resample', 'rebin'])
@settings(
max_examples=10,
# Lots of draws can be discarded when checking matrix is non-singular
Expand All @@ -1042,7 +1042,7 @@ def test_resample_rotated_map_pc(pc, method):
smap = make_simple_map()

smap.meta.update(pc)
# Check superpixel with a rotated map with unequal resampling
# Check rebin with a rotated map with unequal resampling
new_dims = (1, 2) * u.pix
new_map = getattr(smap, method)(new_dims)
# Coordinate of the lower left corner should not change
Expand All @@ -1051,7 +1051,7 @@ def test_resample_rotated_map_pc(pc, method):
new_map.wcs.pixel_to_world(*ll_pix)).to(u.arcsec) < 1e-8 * u.arcsec


@pytest.mark.parametrize('method', ['resample', 'superpixel'])
@pytest.mark.parametrize('method', ['resample', 'rebin'])
@settings(
max_examples=10,
# Lots of draws can be discarded when checking matrix is non-singular
Expand All @@ -1065,7 +1065,7 @@ def test_resample_rotated_map_cd(cd, method):
smap.meta.update(cd)
for key in ['cdelt1', 'cdelt2', 'pc1_1', 'pc1_2', 'pc2_1', 'pc2_2']:
del smap.meta[key]
# Check superpixel with a rotated map with unequal resampling
# Check rebin with a rotated map with unequal resampling
new_dims = (1, 2) * u.pix
new_map = getattr(smap, method)(new_dims)
# Coordinate of the lower left corner should not change
Expand All @@ -1074,9 +1074,9 @@ def test_resample_rotated_map_cd(cd, method):
new_map.wcs.pixel_to_world(*ll_pix)).to(u.arcsec) < 1e-8 * u.arcsec


def test_superpixel_err(generic_map):
def test_rebin_err(generic_map):
with pytest.raises(ValueError, match="Offset is strictly non-negative."):
generic_map.superpixel((2, 2) * u.pix, offset=(-2, 2) * u.pix)
generic_map.rebin((2, 2) * u.pix, offset=(-2, 2) * u.pix)


def calc_new_matrix(angle):
Expand Down Expand Up @@ -1663,7 +1663,7 @@ def test_rsun_meters_no_warning_for_hgs(heliographic_test_map):
@figure_test
def test_rotation_rect_pixelated_data(aia171_test_map):
aia_map = sunpy.map.Map(aia171_test_map)
rect_map = aia_map.superpixel([2, 1] * u.pix, func=np.mean)
rect_map = aia_map.rebin([2, 1] * u.pix, func=np.mean)

Check warning on line 1666 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1666

Added line #L1666 was not covered by tests
rect_rot_map = rect_map.rotate(30 * u.deg)
rect_rot_map.peek()

Expand Down Expand Up @@ -1700,7 +1700,7 @@ def test_draw_contours_with_transform(sample_171, sample_hmi):
@figure_test
def test_derotating_nonpurerotation_pcij(aia171_test_map, method):
# The following map has a a PCij matrix that is not a pure rotation
weird_map = aia171_test_map.rotate(30*u.deg).superpixel([2, 1]*u.pix)
weird_map = aia171_test_map.rotate(30*u.deg).rebin([2, 1]*u.pix)

Check warning on line 1703 in sunpy/map/tests/test_mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_mapbase.py#L1703

Added line #L1703 was not covered by tests

# De-rotating the map by its PCij matrix should result in a normal-looking map
derotated_map = weird_map.rotate(method=method)
Expand Down
4 changes: 2 additions & 2 deletions sunpy/map/tests/test_mapbase_dask.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def test_dask_array_repr(aia171_test_dask_map):
@pytest.mark.parametrize(("func", "args"), [
pytest.param("reproject_to", {"wcs": aia_wcs}, marks=pytest.mark.xfail(reason="reproject is not dask aware")),
pytest.param("resample", {"dimensions": (100, 100)*u.pix}, marks=pytest.mark.xfail()),
pytest.param("rotate", {}, marks=pytest.mark.xfail(reason="nanmedian in Dask doesn't support our use")),
("superpixel", {"dimensions": (10, 10)*u.pix}),
pytest.param("rotate", {}, marks=pytest.mark.xfail(reason="nanmedian in dask doesn't support our use")),
("rebin", {"dimensions": (10, 10)*u.pix}),
("submap", {"bottom_left": (100, 100)*u.pixel, "width": 10*u.pixel, "height": 10*u.pixel}),
])
def test_method_preserves_dask_array(aia171_test_map, aia171_test_dask_map, func, args):
Expand Down
2 changes: 1 addition & 1 deletion sunpy/map/tests/test_mapsequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def mapsequence_all_the_same_some_have_masks(aia171_test_map, aia171_test_map_wi
def mapsequence_different(aia171_test_map):
""" Mapsequence allows that the size of the image data in each map be
different. This mapsequence contains such maps."""
return sunpy.map.Map([aia171_test_map, aia171_test_map.superpixel((4, 4) * u.pix)], sequence=True)
return sunpy.map.Map([aia171_test_map, aia171_test_map.rebin((4, 4) * u.pix)], sequence=True)


def test_all_maps_same_shape(mapsequence_all_the_same, mapsequence_different):
Expand Down
12 changes: 6 additions & 6 deletions sunpy/map/tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ def test_plot_masked_aia171(aia171_test_map_with_mask):


@figure_test
def test_plot_aia171_superpixel(aia171_test_map):
aia171_test_map.superpixel((3, 2) * u.pix, offset=(4, 4) * u.pix).plot()
def test_plot_aia171_rebin(aia171_test_map):
aia171_test_map.rebin((3, 2) * u.pix, offset=(4, 4) * u.pix).plot()

Check warning on line 173 in sunpy/map/tests/test_plotting.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_plotting.py#L173

Added line #L173 was not covered by tests


@figure_test
Expand All @@ -191,10 +191,10 @@ def test_plot_resample(carrington_map):


@figure_test
def test_plot_superpixel(carrington_map):
def test_plot_rebin(carrington_map):
# Test that super-pixelling a map preserves the coordinate system correctly.
# The two plots should have identical coordinate grids
superpix = carrington_map.superpixel([2, 2] * u.pix)
superpix = carrington_map.rebin([2, 2] * u.pix)

Check warning on line 197 in sunpy/map/tests/test_plotting.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_plotting.py#L197

Added line #L197 was not covered by tests

plt.figure()
ax1 = plt.subplot(121, projection=carrington_map)
Expand All @@ -208,8 +208,8 @@ def test_plot_superpixel(carrington_map):


@figure_test
def test_plot_masked_aia171_superpixel(aia171_test_map_with_mask):
aia171_test_map_with_mask.superpixel(
def test_plot_masked_aia171_rebin(aia171_test_map_with_mask):
aia171_test_map_with_mask.rebin(

Check warning on line 212 in sunpy/map/tests/test_plotting.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/tests/test_plotting.py#L212

Added line #L212 was not covered by tests
(9, 7) * u.pix, offset=(4, 4) * u.pix).plot()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"sunpy.map.tests.test_plotting.test_quadrangle_aia17_pix_top_right": "e55f63d480032009779aa5c1742aff30694587f8bee2f8201c7c64192880cecb",
"sunpy.map.tests.test_plotting.test_quadrangle_aia17_pix_top_right_different_axes": "893064c44b850aea39b24b2a94af10af340745c0e32b867033b0f14684bf337f",
"sunpy.map.tests.test_plotting.test_plot_masked_aia171": "8acfaf714eca9295e1d4ecd17e6eec62c8fc991cba45eeb0ba6d2efaa492d29b",
"sunpy.map.tests.test_plotting.test_plot_aia171_superpixel": "1c133f41c740f4974b8dd27477a247a64c7f8e387d1d8d8c9b4f42ca0387b2ac",
"sunpy.map.tests.test_plotting.test_plot_aia171_rebin": "1c133f41c740f4974b8dd27477a247a64c7f8e387d1d8d8c9b4f42ca0387b2ac",
"sunpy.map.tests.test_plotting.test_plot_resample": "351baa39339b9f7d206232b9c2fb777d7edf965e1554040171ac40e3a1f5ec62",
"sunpy.map.tests.test_plotting.test_plot_superpixel": "4fcd7af5690bbb5a1ae65ab2bf80db31aa41f240456cddaf49c6132fd459b973",
"sunpy.map.tests.test_plotting.test_plot_masked_aia171_superpixel": "ffabe0ca544b93c8084af36edb5b293cdce613d13b73ddeaa906ddf2617da34b",
"sunpy.map.tests.test_plotting.test_plot_rebin": "4fcd7af5690bbb5a1ae65ab2bf80db31aa41f240456cddaf49c6132fd459b973",
"sunpy.map.tests.test_plotting.test_plot_masked_aia171_rebin": "ffabe0ca544b93c8084af36edb5b293cdce613d13b73ddeaa906ddf2617da34b",
"sunpy.map.tests.test_plotting.test_draw_contours_aia": "c81ebc6eda92aab0e792d533b867e1f6a7791dbfb475b9c241a122c3eb53e4a9",
"sunpy.map.tests.test_plotting.test_draw_contours_different_wcs": "5f2abc715e899ad9b9425acfe1beb41a915423b93737781c348dfa0b99de8180",
"sunpy.map.tests.test_plotting.test_draw_contours_aia_fill": "9b12d674a94acd33b025536bc74ac88735599702684dec3d6510f6b1b39a605b",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"sunpy.map.tests.test_plotting.test_quadrangle_aia17_pix_top_right": "586d82cd41587965becb6793956d2855d96da84a4791acc7de5e52b6f77e6ad0",
"sunpy.map.tests.test_plotting.test_quadrangle_aia17_pix_top_right_different_axes": "47f6153e288bcc952fc5e0e8600d180550f3f20ef69d7b6090208b511b81eed6",
"sunpy.map.tests.test_plotting.test_plot_masked_aia171": "738b6d265e9a6c534204f44912048566289c59f89aa8d98345969db835f7e834",
"sunpy.map.tests.test_plotting.test_plot_aia171_superpixel": "02f8b342f79e2395052ee851cb785fc0f208c10a26bb6a6547e71e4da12d9a6c",
"sunpy.map.tests.test_plotting.test_plot_aia171_rebin": "02f8b342f79e2395052ee851cb785fc0f208c10a26bb6a6547e71e4da12d9a6c",
"sunpy.map.tests.test_plotting.test_plot_resample": "df6313df2fbf65d1f9aaf018677772c05750dcbd3e0098f34ca41cc40e5d8815",
"sunpy.map.tests.test_plotting.test_plot_superpixel": "45ebdec700a7699f7aaea6e86b22c049c65ed3c457aa2e0f5b53d135b9f54c79",
"sunpy.map.tests.test_plotting.test_plot_masked_aia171_superpixel": "854f60a854e0f3166762c94831445b83f5402a8aa70e34013616f7e4074476fe",
"sunpy.map.tests.test_plotting.test_plot_rebin": "45ebdec700a7699f7aaea6e86b22c049c65ed3c457aa2e0f5b53d135b9f54c79",
"sunpy.map.tests.test_plotting.test_plot_masked_aia171_rebin": "854f60a854e0f3166762c94831445b83f5402a8aa70e34013616f7e4074476fe",
"sunpy.map.tests.test_plotting.test_draw_contours_aia": "90f83a5c24fdd88a1d14807034ba2b4ee1644d3f53745935eb8e537a2acc2b9a",
"sunpy.map.tests.test_plotting.test_draw_contours_different_wcs": "9cbb12775ed1da71226dcc500f434bdf0ece76d54894942fb34c1805d8d11de0",
"sunpy.map.tests.test_plotting.test_draw_contours_aia_fill": "abd8a02404ddcd6a4490b6b4a7d7bb0987fec7fa74cec9224bf58dcbad19985b",
Expand Down

0 comments on commit cf83800

Please sign in to comment.