Skip to content

Commit

Permalink
Unsure if this is sane
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed Mar 21, 2024
1 parent e869422 commit 21c679a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 42 deletions.
24 changes: 5 additions & 19 deletions sunpy/map/mapbase.py
Expand Up @@ -29,7 +29,7 @@
from astropy.coordinates import BaseCoordinateFrame, Longitude, SkyCoord, UnitSphericalRepresentation
from astropy.nddata import NDData
from astropy.utils.metadata import MetaData
from astropy.visualization import AsymmetricPercentileInterval, HistEqStretch, ImageNormalize
from astropy.visualization import HistEqStretch, ImageNormalize
from astropy.visualization.wcsaxes import Quadrangle, WCSAxes

# The next two are not used but are called to register functions with external modules
Expand All @@ -43,6 +43,7 @@
from sunpy.image.resample import resample as sunpy_image_resample
from sunpy.image.resample import reshape_image_to_4d_superpixel
from sunpy.image.transform import _get_transform_method, _rotation_function_names, affine_transform
from sunpy.map.maputils import _clip_interval, _handle_norm
from sunpy.sun import constants
from sunpy.time import is_time, parse_time
from sunpy.util import MetaDict, expand_list
Expand Down Expand Up @@ -2534,27 +2535,12 @@ def plot(self, annotate=True, axes=None, title=True, autoalign=False,
imshow_args.update(copy.deepcopy(imshow_kwargs))

if clip_interval is not None:
if len(clip_interval) == 2:
clip_percentages = clip_interval.to('%').value
vmin, vmax = AsymmetricPercentileInterval(*clip_percentages).get_limits(self.data)
else:
raise ValueError("Clip percentile interval must be specified as two numbers.")

vmin, vmax = _clip_interval(self.data, clip_interval)

Check warning on line 2538 in sunpy/map/mapbase.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapbase.py#L2538

Added line #L2538 was not covered by tests
imshow_args['vmin'] = vmin
imshow_args['vmax'] = vmax

msg = ('Cannot manually specify {0}, as the norm '
'already has {0} set. To prevent this error set {0} on '
'`m.plot_settings["norm"]` or the norm passed to `m.plot`.')
if (norm := imshow_args.get('norm', None)) is not None:
if 'vmin' in imshow_args:
if norm.vmin is not None:
raise ValueError(msg.format('vmin'))
norm.vmin = imshow_args.pop('vmin')
if 'vmax' in imshow_args:
if norm.vmax is not None:
raise ValueError(msg.format('vmax'))
norm.vmax = imshow_args.pop('vmax')
if norm:= imshow_args.get('norm', None) is not None:
_handle_norm(norm, imshow_args)

if self.mask is None:
data = self.data
Expand Down
27 changes: 4 additions & 23 deletions sunpy/map/mapsequence.py
Expand Up @@ -11,9 +11,9 @@
import numpy.ma as ma

import astropy.units as u
from astropy.visualization import AsymmetricPercentileInterval

from sunpy.map import GenericMap
from sunpy.map.maputils import _clip_interval, _handle_norm
from sunpy.util import expand_list
from sunpy.util.exceptions import warn_user
from sunpy.visualization import axis_labels_from_ctype, wcsaxes_compat
Expand Down Expand Up @@ -282,7 +282,6 @@ def plot(self, axes=None, resample=None, annotate=True,
Color map to be used in coloring the plot.
This will override the predefined value in ``plot_settings['cmap']``.
Returns
-------
`matplotlib.animation.FuncAnimation`
Expand Down Expand Up @@ -363,33 +362,15 @@ def updatefig(i, im, annotate, ani_data, removes):

im.set_array(ani_data[i].data)
im.set_cmap(kwargs.get('cmap', ani_data[i].plot_settings['cmap']))

norm = deepcopy(kwargs.get('norm', ani_data[i].plot_settings['norm']))

if clip_interval is not None:
if len(clip_interval) == 2:
clip_percentages = clip_interval.to('%').value
vmin, vmax = AsymmetricPercentileInterval(*clip_percentages).get_limits(ani_data[i].data)
else:
raise ValueError("Clip percentile interval must be specified as two numbers.")

vmin, vmax = _clip_interval(self.data, clip_interval)
norm.vmin=vmin
norm.vmax=vmax
_handle_norm(norm, kwargs)

Check warning on line 371 in sunpy/map/mapsequence.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/mapsequence.py#L367-L371

Added lines #L367 - L371 were not covered by tests

msg = ('Cannot manually specify {0}, as the norm '
'already has {0} set. To prevent this error set {0} on '
'`m.plot_settings["norm"]` or the norm passed to `m.plot`.')
if 'vmin' in kwargs:
if norm.vmin is not None:
raise ValueError(msg.format('vmin'))
norm.vmin = kwargs.get('vmin')
if 'vmax' in kwargs:
if norm.vmax is not None:
raise ValueError(msg.format('vmax'))
norm.vmax = kwargs.get('vmax')

# The following explicit call is for bugged versions of Astropy's
# ImageNormalize
# The following explicit call is for bugged versions of Astropy's ImageNormalize
norm.autoscale_None(ani_data[i].data)
im.set_norm(norm)

Expand Down
28 changes: 28 additions & 0 deletions sunpy/map/maputils.py
Expand Up @@ -8,6 +8,7 @@

import astropy.units as u
from astropy.coordinates import SkyCoord
from astropy.visualization import AsymmetricPercentileInterval

from sunpy.coordinates import Helioprojective, sun

Expand All @@ -20,6 +21,33 @@
'contains_coordinate', 'contains_solar_center',
'pixelate_coord_path']

def _clip_interval(data, clip_interval):
"""
Just a helper function to check the input and return the clip interval.
Avoids some code duplication.
"""
if len(clip_interval) != 2:
raise ValueError("Clip percentile interval must be specified as two numbers.")
clip_percentages = clip_interval.to('%').value
vmin, vmax = AsymmetricPercentileInterval(*clip_percentages).get_limits(data)
return vmin, vmax

Check warning on line 33 in sunpy/map/maputils.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/maputils.py#L29-L33

Added lines #L29 - L33 were not covered by tests

def _handle_norm(norm, imshow_args):
"""
Just a helper function to handle the norm of a map or map sequence.
Avoids some code duplication.
"""
msg = ('Cannot manually specify {0}, as the norm '
'already has {0} set. To prevent this error set {0} on '
'`m.plot_settings["norm"]` or the norm passed to `m.plot`.')
if 'vmin' in imshow_args:
if norm.vmin is not None:
raise ValueError(msg.format('vmin'))
norm.vmin = imshow_args.pop('vmin')

Check warning on line 46 in sunpy/map/maputils.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/maputils.py#L45-L46

Added lines #L45 - L46 were not covered by tests
if 'vmax' in imshow_args:
if norm.vmax is not None:
raise ValueError(msg.format('vmax'))
norm.vmax = imshow_args.pop('vmax')

Check warning on line 50 in sunpy/map/maputils.py

View check run for this annotation

Codecov / codecov/patch

sunpy/map/maputils.py#L48-L50

Added lines #L48 - L50 were not covered by tests

def all_pixel_indices_from_map(smap):
"""
Expand Down

0 comments on commit 21c679a

Please sign in to comment.