Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: migrate away from broken optional dependency (pyregion -> regions) #4235

Merged
merged 1 commit into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -414,7 +414,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
26 changes: 17 additions & 9 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,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().to_image((header["NAXIS1"], header["NAXIS2"])).astype(bool)

if isinstance(ds, EventsFITSDataset):
prefix = "event_"
else:
Expand Down
7 changes: 7 additions & 0 deletions yt/utilities/on_demand_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down