Skip to content

Commit 361f887

Browse files
committed
Make common kwargs for NDCube.to_nddata explicit in call signature.
1 parent 9c9572d commit 361f887

File tree

1 file changed

+66
-23
lines changed

1 file changed

+66
-23
lines changed

ndcube/ndcube.py

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
except ImportError:
4949
pass
5050

51+
COPY = object()
52+
5153

5254
class NDCubeABC(astropy.nddata.NDDataBase):
5355

@@ -1461,33 +1463,63 @@ def fill_masked(self, fill_value, uncertainty_fill_value=None, unmask=False, fil
14611463
self.mask = False
14621464
return None
14631465

1464-
def to_nddata(self, *, nddata_type=NDData, **kwargs):
1466+
def to_nddata(self,
1467+
*,
1468+
data=COPY,
1469+
wcs=COPY,
1470+
uncertainty=COPY,
1471+
mask=COPY,
1472+
unit=COPY,
1473+
meta=COPY,
1474+
psf=COPY,
1475+
extra_coords=COPY,
1476+
global_coords=COPY,
1477+
nddata_type=NDData,
1478+
**kwargs,
1479+
):
14651480
"""
14661481
Constructs new type instance with the same attribute values as this `~ndcube.NDCube`.
14671482
1483+
Attribute values can be altered on the output object by setting a kwarg with the new
1484+
value, e.g. ``data=new_data``.
14681485
Any attributes not supported by the new class (``nddata_type``), will be discarded.
1469-
Values of supported attributes can be altered on the output object by setting a
1470-
kwarg with the new value, e.g. ``data=new_data``.
14711486
14721487
Parameters
14731488
----------
1474-
nddata_type:
1489+
data: array-like, optional
1490+
Data array of new instance. Default is to use data of this instance.
1491+
wcs: `astropy.wcs.wcsapi.BaseLowLevelWCS`, `astropy.wcs.wcsapi.BaseHighLevelWCS`, optional
1492+
WCS object of new instance. Default is to use data of this instance.
1493+
uncertainty: Any, optional
1494+
Uncertainy object of new instance. Default is to use data of this instance.
1495+
mask: Any, optional
1496+
Mask object of new instance. Default is to use data of this instance.
1497+
unit: Any, optional
1498+
Unit of new instance. Default is to use data of this instance.
1499+
meta: dict-like, optional
1500+
Metadata object of new instance. Default is to use data of this instance.
1501+
psf: Any, optional
1502+
PSF object of new instance. Default is to use data of this instance.
1503+
extra_coords: `ndcubeextra_coords.ExtraCoordsABC`, optional
1504+
Extra coords object of new instance. Default is to use data of this instance.
1505+
global_coords: `ndcube.global_coords.GlobalCoordsABC`, optional
1506+
WCS object of new instance. Default is to use data of this instance.
1507+
nddata_type: Any, optional
14751508
The type of the returned object. Must be a subclass of `~astropy.nddata.NDData`
14761509
or a class that behaves like one. Default=`~astropy.nddata.NDData`.
1477-
14781510
kwargs:
1479-
Inputs to the ``nddata_type`` constructor that should differ from the values
1480-
stored in attributes of this instance. For example, to set different data values
1481-
on the returned object, set a kwarg ``data=new_data``, where ``new_data`` is an
1482-
an array of compatible shape and dtype. Note that kwargs given by the user and
1483-
attributes on this instance that are not supported by the ``nddata_type``
1484-
constructor are ignored.
1511+
Additional inputs to the ``nddata_type`` constructor that should differ from,
1512+
or are not represented by, the attributes of this instance. For example, to
1513+
set different data values on the returned object, set a kwarg ``data=new_data``,
1514+
where ``new_data`` is an array of compatible shape and dtype. Note that kwargs
1515+
given by the user and attributes on this instance that are not supported by the
1516+
``nddata_type`` constructor are ignored.
14851517
14861518
Returns
14871519
-------
1488-
new_nddata: object
1489-
An object of class given by ``nddata_type`` with the same attribute values as
1490-
this `~ndcube.NDCube` instance, except for any alterations specified by the
1520+
new_nddata: Any
1521+
The new instance of class given by ``nddata_type`` with the same attribute values
1522+
as this `~ndcube.NDCube` instance, except for any alterations specified by the
14911523
kwargs.
14921524
14931525
Examples
@@ -1498,22 +1530,33 @@ def to_nddata(self, *, nddata_type=NDData, **kwargs):
14981530
>>> nddata_without_coords = cube.to_nddata(wcs=None) # doctest: +SKIP
14991531
"""
15001532
# Build dictionary of new attribute values from this NDCube instance
1501-
# and update with user-defined kwargs.
1502-
new_kwargs = {key.strip("_"): value for key, value in self.__dict__.items()}
1503-
new_kwargs.update(kwargs)
1533+
# and update with user-defined kwargs. Remove any kwargs not set by user.
1534+
user_kwargs = {"data": data,
1535+
"wcs": wcs,
1536+
"uncertainty": uncertainty,
1537+
"mask": mask,
1538+
"unit": unit,
1539+
"meta": meta,
1540+
"psf": psf,
1541+
"extra_coords": extra_coords,
1542+
"global_coords": global_coords}
1543+
user_kwargs = {key: value for key, value in user_kwargs.items() if value is not COPY}
1544+
user_kwargs.update(kwargs)
1545+
all_kwargs = {key.strip("_"): value for key, value in self.__dict__.items()}
1546+
all_kwargs.update(user_kwargs)
15041547
extra_coords, global_coords = None, None
15051548
if nddata_type is NDCube:
1506-
extra_coords = new_kwargs.pop("extra_coords")
1507-
global_coords = new_kwargs.pop("global_coords")
1549+
extra_coords = all_kwargs.pop("extra_coords")
1550+
global_coords = all_kwargs.pop("global_coords")
15081551
# Inspect call signature of new_nddata class and
15091552
# remove unsupported items from new_kwargs.
15101553
nddata_sig = inspect.signature(nddata_type).parameters.keys()
1511-
new_kwarg_keys = list(new_kwargs.keys())
1512-
for key in new_kwarg_keys:
1554+
all_kwarg_keys = list(all_kwargs.keys())
1555+
for key in all_kwarg_keys:
15131556
if key not in nddata_sig:
1514-
del new_kwargs[key]
1557+
del all_kwargs[key]
15151558
# Construct and return new instance.
1516-
new_nddata = nddata_type(**new_kwargs)
1559+
new_nddata = nddata_type(**all_kwargs)
15171560
if extra_coords:
15181561
extra_coords._ndcube = new_nddata
15191562
new_nddata._extra_coords = extra_coords

0 commit comments

Comments
 (0)