-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
core.py
476 lines (389 loc) · 14.8 KB
/
core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import abc
import copy
import operator
import numpy as np
from regions.core.metadata import RegionMeta, RegionVisual
from regions.core.pixcoord import PixCoord
from regions.core.registry import RegionsRegistry
__all__ = ['Region', 'PixelRegion', 'SkyRegion']
__doctest_skip__ = ['Region.serialize', 'Region.write']
class Region(abc.ABC):
"""
Base class for all regions.
"""
_params = ()
def copy(self, **changes):
"""
Make an independent (deep) copy.
"""
fields = list(self._params) + ['meta', 'visual']
for field in fields:
if field not in changes:
changes[field] = copy.deepcopy(getattr(self, field))
return self.__class__(**changes)
def __repr__(self):
prefix = f'{self.__class__.__name__}'
cls_info = []
if self._params is not None:
for param in self._params:
if param == 'text':
# place quotes around text value
keyval = f'{param}={getattr(self, param)!r}'
else:
keyval = f'{param}={getattr(self, param)}'
cls_info.append(keyval)
cls_info = ', '.join(cls_info)
return f'<{prefix}({cls_info})>'
def __str__(self):
cls_info = [('Region', self.__class__.__name__)]
if self._params is not None:
for param in self._params:
if param == 'text':
# place quotes around text value
keyval = (param, repr(getattr(self, param)))
else:
keyval = (param, getattr(self, param))
cls_info.append(keyval)
return '\n'.join([f'{key}: {val}' for key, val in cls_info])
def __eq__(self, other):
"""
Equality operator for Region.
All Region properties are compared for strict equality except
for Quantity parameters, which allow for different units if they
are directly convertible.
"""
if not isinstance(other, self.__class__):
return False
meta_params = ['meta', 'visual']
self_params = list(self._params) + meta_params
other_params = list(other._params) + meta_params
# check that both have identical parameters
if self_params != other_params:
return False
# now check the parameter values
# Note that Quantity comparisons allow for different units
# if they directly convertible (e.g., 1. * u.deg == 60. * u.arcmin)
try:
for param in self_params:
# np.any is used for SkyCoord array comparisons
if np.any(getattr(self, param) != getattr(other, param)):
return False
except TypeError:
# TypeError is raised from SkyCoord comparison when they do
# not have equivalent frames. Here return False instead of
# the TypeError.
return False
return True
def __ne__(self, other):
"""
Inequality operator for Region.
"""
return not (self == other)
@abc.abstractmethod
def intersection(self, other):
"""
Return a region representing the intersection of this region
with ``other``.
"""
raise NotImplementedError
@abc.abstractmethod
def symmetric_difference(self, other):
"""
Return the union of the two regions minus any areas contained in
the intersection of the two regions.
"""
raise NotImplementedError
@abc.abstractmethod
def union(self, other):
"""
Return a region representing the union of this region with
``other``.
"""
raise NotImplementedError
def __and__(self, other):
return self.intersection(other)
def __or__(self, other):
return self.union(other)
def __xor__(self, other):
return self.symmetric_difference(other)
@classmethod
def get_formats(cls):
"""
Get the registered I/O formats as a Table.
"""
return RegionsRegistry.get_formats(Region)
def write(self, filename, format=None, overwrite=False, **kwargs):
"""
Write the region to a region file in the specified format.
This method allows writing a file in many supported data
formats, e.g.,::
>>> reg.write('new_regions.reg', format='ds9')
>>> reg.write('new_regions.crtf', format='crtf')
>>> reg.write('new_regions.fits', format='fits')
A list of the available formats for `~regions.Region` is
available using::
>>> from regions import Region
>>> Region.get_formats()
Parameters
----------
filename : str
The filename or URL of the file to write.
format : str, optional
The file format specifier.
overwrite : bool, optional
If True, overwrite the output file if it exists. Raises an
`OSError` if False and the output file exists. Default is
False.
**kwargs : dict, optional
Keyword arguments passed to the data writer.
"""
return RegionsRegistry.write([self], filename,
Region, format=format,
overwrite=overwrite, **kwargs)
def serialize(self, format=None, **kwargs):
"""
Serialize the region to a region string or table.
This method allows serializing regions in many supported data
formats, e.g.,::
>>> reg1_str = reg.serialize(format='ds9')
>>> reg2_str = reg.serialize(format='crtf')
>>> reg3_tbl = reg.serialize(format='fits')
A list of the available formats for `~regions.Region` is
available using::
>>> from regions import Region
>>> Region.get_formats()
Parameters
----------
format : str, optional
The file format specifier.
**kwargs : dict, optional
Keyword arguments passed to the data serializer.
"""
return RegionsRegistry.serialize([self], Region, format=format,
**kwargs)
class PixelRegion(Region):
"""
Base class for all regions defined in pixel coordinates.
"""
meta = RegionMeta()
visual = RegionVisual()
def intersection(self, other):
"""
Return a region representing the intersection of this region
with ``other``.
"""
from regions.core.compound import CompoundPixelRegion
return CompoundPixelRegion(region1=self, region2=other,
operator=operator.and_)
def symmetric_difference(self, other):
"""
Return the union of the two regions minus any areas contained in
the intersection of the two regions.
"""
from regions.core.compound import CompoundPixelRegion
return CompoundPixelRegion(region1=self, region2=other,
operator=operator.xor)
def union(self, other):
"""
Return a region representing the union of this region with
``other``.
"""
from regions.core.compound import CompoundPixelRegion
return CompoundPixelRegion(region1=self, region2=other,
operator=operator.or_)
@abc.abstractmethod
def contains(self, pixcoord):
"""
Check whether a position or positions fall inside the region.
Parameters
----------
pixcoord : `~regions.PixCoord`
The position or positions to check.
"""
raise NotImplementedError
def __contains__(self, coord):
if not coord.isscalar:
raise ValueError(f'coord must be scalar. coord={coord}')
return self.contains(coord)
@abc.abstractmethod
def to_sky(self, wcs):
"""
Return a region defined in sky coordinates.
Parameters
----------
wcs : `~astropy.wcs.WCS`
The world coordinate system transformation to use to convert
from pixels to sky coordinates.
Returns
-------
sky_region : `~regions.SkyRegion`
The sky region.
"""
raise NotImplementedError
@property
@abc.abstractmethod
def area(self):
"""
The exact analytical area of the region shape.
"""
raise NotImplementedError
@property
@abc.abstractmethod
def bounding_box(self):
"""
The minimal bounding box (in integer pixel coordinates) that
contains the region.
"""
raise NotImplementedError
@abc.abstractmethod
def to_mask(self, mode='center', subpixels=5):
"""
Return a mask for the region.
Parameters
----------
mode : {'center', 'exact', 'subpixels'}, optional
The method used to determine the overlap of the region on
the pixel grid. Not all options are available for all region
types. Note that the more precise methods are generally
slower. The following methods are available:
* ``'center'``:
A pixel is considered to be entirely in or out of the
region depending on whether its center is in or out of
the region. The returned mask will contain values only
of 0 (out) and 1 (in).
* ``'exact'`` (default):
The exact fractional overlap of the region and each
pixel is calculated. The returned mask will contain
values between 0 and 1.
* ``'subpixel'``:
A pixel is divided into subpixels (see the
``subpixels`` keyword), each of which are considered
to be entirely in or out of the region depending
on whether its center is in or out of the region.
If ``subpixels=1``, this method is equivalent to
``'center'``. The returned mask will contain values
between 0 and 1.
subpixels : int, optional
For the ``'subpixel'`` mode, resample pixels by this factor
in each dimension. That is, each pixel is divided into
``subpixels ** 2`` subpixels.
Returns
-------
mask : `~regions.RegionMask`
A mask for the region.
"""
raise NotImplementedError
@staticmethod
def _validate_mode(mode, subpixels):
valid_modes = ('center', 'exact', 'subpixels')
if mode not in valid_modes:
raise ValueError(f'Invalid mask mode: {mode} (should be one '
f'of {valid_modes}')
if mode == 'subpixels':
if not isinstance(subpixels, int) or subpixels <= 0:
raise ValueError(f'Invalid subpixels value: {subpixels} '
'(should be a strictly positive integer)')
@abc.abstractmethod
def as_artist(self, origin=(0, 0), **kwargs):
"""
Convert to matplotlib patch object for this region.
Parameters
----------
origin : array_like, optional
The ``(x, y)`` pixel position of the origin of the displayed
image.
**kwargs : dict
Any keyword arguments accepted by
`~matplotlib.patches.Patch`.
Returns
-------
patch : `~matplotlib.patches.Patch`
A matplotlib patch.
"""
raise NotImplementedError
def plot(self, origin=(0, 0), ax=None, **kwargs):
"""
Plot the region on a matplotlib `~matplotlib.axes.Axes`
instance.
Parameters
----------
origin : array_like, optional
The ``(x, y)`` pixel position of the origin of the displayed
image.
ax : `~matplotlib.axes.Axes` or `None`, optional
The matplotlib axes on which to plot. If `None`, then the
current `~matplotlib.axes.Axes` instance is used.
**kwargs : dict
Any keyword arguments accepted by
`~matplotlib.patches.Patch`.
Returns
-------
artist : `matplotlib.artist.Artist`
The matplotlib artist (typically a `~matplotlib.patches.Patch`
object) for the plotted region. The artist can be used, for
example, when adding a plot legend.
"""
import matplotlib.pyplot as plt
if ax is None:
ax = plt.gca()
artist = self.as_artist(origin=origin, **kwargs)
ax.add_artist(artist)
return artist
class SkyRegion(Region):
"""
Base class for all regions defined in celestial coordinates.
"""
def intersection(self, other):
"""
Return a region representing the intersection of this region
with ``other``.
"""
from regions.core.compound import CompoundSkyRegion
return CompoundSkyRegion(region1=self, region2=other,
operator=operator.and_)
def symmetric_difference(self, other):
"""
Return the union of the two regions minus any areas contained in
the intersection of the two regions.
"""
from regions.core.compound import CompoundSkyRegion
return CompoundSkyRegion(region1=self, region2=other,
operator=operator.xor)
def union(self, other):
"""
Return a region representing the union of this region with
``other``.
"""
from regions.core.compound import CompoundSkyRegion
return CompoundSkyRegion(region1=self, region2=other,
operator=operator.or_)
def contains(self, skycoord, wcs):
"""
Check whether a sky coordinate falls inside the region.
Parameters
----------
skycoord : `~astropy.coordinates.SkyCoord`
The position or positions to check.
wcs : `~astropy.wcs.WCS`
The world coordinate system transformation to use to convert
between sky and pixel coordinates.
"""
pixel_region = self.to_pixel(wcs)
pixcoord = PixCoord.from_sky(skycoord, wcs)
return pixel_region.contains(pixcoord)
@abc.abstractmethod
def to_pixel(self, wcs):
"""
Return the equivalent region defined in pixel coordinates.
Parameters
----------
wcs : `~astropy.wcs.WCS`
The world coordinate system transformation to use to convert
between sky and pixel coordinates.
Returns
-------
pixel_region : `~regions.PixelRegion`
A pixel region.
"""
raise NotImplementedError