Skip to content

Commit

Permalink
BUG: migrate away from broken optional dependency (pyregion -> regions)
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoceros committed Dec 5, 2022
1 parent 9466e2e commit 0ce9b4e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions doc/source/cookbook/fits_radio_cubes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"source": [
"This notebook demonstrates some of the capabilities of yt on some FITS \"position-position-spectrum\" cubes of radio data.\n",
"\n",
"Note that it depends on some external dependencies, including `astropy` and `pyregion`."
"Note that it depends on some external dependencies, including `astropy` and `regions`."
]
},
{
Expand Down Expand Up @@ -413,7 +413,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we can also take an existing [ds9](http://ds9.si.edu/site/Home.html) region and use it to create a \"cut region\" as well, using `ds9_region` (the [pyregion](https://pyregion.readthedocs.io/) package needs to be installed for this):"
"Finally, we can also take an existing [ds9](http://ds9.si.edu/site/Home.html) region and use it to create a \"cut region\" as well, using `ds9_region` (the [regions](https://astropy-regions.readthedocs.io/) package needs to be installed for this):"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/source/cookbook/fits_xray_images.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, we can also take an existing [ds9](http://ds9.si.edu/site/Home.html) region and use it to create a \"cut region\", using `ds9_region` (the [pyregion](https://pyregion.readthedocs.io) package needs to be installed for this):"
"Finally, we can also take an existing [ds9](http://ds9.si.edu/site/Home.html) region and use it to create a \"cut region\", using `ds9_region` (the [regions](https://astropy-regions.readthedocs.io/) package needs to be installed for this):"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion doc/source/examining/loading_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ and add them to the field registry for the dataset ``ds``.
This function takes a `ds9 <http://ds9.si.edu/site/Home.html>`_ region and
creates a "cut region" data container from it, that can be used to select
the cells in the FITS dataset that fall within the region. To use this
functionality, the `pyregion <https://github.com/astropy/pyregion/>`_
functionality, the `regions <https://github.com/astropy/regions/>`_
package must be installed.

.. code-block:: python
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ doc =
jinja2<3.1.0 # see https://github.com/readthedocs/readthedocs.org/issues/9037
jupyter-client<7.0
nbconvert==5.6.1
pyregion
pyx>=0.15
regions>=0.7
runnotebook
scipy<=1.7.1 # see https://github.com/yt-project/yt/issues/3966 and https://github.com/scipy/scipy/issues/16602
sphinx==3.1.2
Expand Down
17 changes: 12 additions & 5 deletions yt/frontends/fits/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def create_spectral_slabs(filename, slab_centers, slab_width, **kwargs):

def ds9_region(ds, reg, obj=None, field_parameters=None):
r"""
Create a data container from a ds9 region file. Requires the pyregion
package (https://pyregion.readthedocs.io/en/latest/) to be installed.
Create a data container from a ds9 region file. Requires the regions
package (https://astropy-regions.readthedocs.io/) to be installed.
Parameters
----------
Expand All @@ -157,19 +157,26 @@ def ds9_region(ds, reg, obj=None, field_parameters=None):
>>> circle_region = ds9_region(ds, "circle.reg")
>>> print(circle_region.quantities.extrema("flux"))
"""
import pyregion
from regions import Regions

from yt.frontends.fits.api import EventsFITSDataset

if os.path.exists(reg):
r = pyregion.open(reg)
method = Regions.read
else:
r = pyregion.parse(reg)
method = Regions.parse
r = method(reg, format="ds9").regions[0]

reg_name = reg
header = ds.wcs_2d.to_header()
# The FITS header only contains WCS-related keywords
header["NAXIS1"] = nx = ds.domain_dimensions[ds.lon_axis]
header["NAXIS2"] = ny = ds.domain_dimensions[ds.lat_axis]

# it seems that the equivalent code with regions would involve something along the lines of
# mask = r.to_mask()
# however this method isn't implemented for e.g. RectanleSkyRegion, which is used in our examples
# see https://github.com/astropy/regions/issues/490
filter = r.get_filter(header=header)
mask = filter.mask((ny, nx)).transpose()
if isinstance(ds, EventsFITSDataset):
Expand Down

0 comments on commit 0ce9b4e

Please sign in to comment.