diff --git a/doc/source/cookbook/fits_radio_cubes.ipynb b/doc/source/cookbook/fits_radio_cubes.ipynb index c34eafbb0f9..889ee3aacaa 100644 --- a/doc/source/cookbook/fits_radio_cubes.ipynb +++ b/doc/source/cookbook/fits_radio_cubes.ipynb @@ -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`." ] }, { @@ -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):" ] }, { diff --git a/doc/source/cookbook/fits_xray_images.ipynb b/doc/source/cookbook/fits_xray_images.ipynb index d0b73f201a8..9124658317d 100644 --- a/doc/source/cookbook/fits_xray_images.ipynb +++ b/doc/source/cookbook/fits_xray_images.ipynb @@ -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):" ] }, { diff --git a/doc/source/examining/loading_data.rst b/doc/source/examining/loading_data.rst index bcda9eb14de..a06a1c97c67 100644 --- a/doc/source/examining/loading_data.rst +++ b/doc/source/examining/loading_data.rst @@ -1232,7 +1232,7 @@ and add them to the field registry for the dataset ``ds``. This function takes a `ds9 `_ 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 `_ +functionality, the `regions `_ package must be installed. .. code-block:: python diff --git a/setup.cfg b/setup.cfg index 7c4b5f4d36e..af6f7ab2b35 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/yt/frontends/fits/misc.py b/yt/frontends/fits/misc.py index 29d5381de2c..7a105d5cc2d 100644 --- a/yt/frontends/fits/misc.py +++ b/yt/frontends/fits/misc.py @@ -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 ---------- @@ -157,21 +157,29 @@ 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.utilities.on_demand_imports import _astropy + + WCS = _astropy.WCS 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] - filter = r.get_filter(header=header) - mask = filter.mask((ny, nx)).transpose() + header["NAXIS1"] = ds.domain_dimensions[ds.lon_axis] + header["NAXIS2"] = ds.domain_dimensions[ds.lat_axis] + + pixreg = r.to_pixel(WCS(header)) + mask = pixreg.to_mask().data.astype(bool) + if isinstance(ds, EventsFITSDataset): prefix = "event_" else: diff --git a/yt/utilities/on_demand_imports.py b/yt/utilities/on_demand_imports.py index 6f593b59b40..cdda96537cb 100644 --- a/yt/utilities/on_demand_imports.py +++ b/yt/utilities/on_demand_imports.py @@ -135,6 +135,13 @@ def wcsaxes(self): self.log return wcsaxes + @safe_import + def WCS(self): + from astropy.wcs import WCS + + self.log + return WCS + _astropy = astropy_imports()