Skip to content

Commit 07a5e8d

Browse files
committed
Use "copy" not True
1 parent e24727d commit 07a5e8d

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

ndcube/ndcube.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,13 +1465,13 @@ def fill_masked(self, fill_value, uncertainty_fill_value=None, unmask=False, fil
14651465

14661466
def to_nddata(self,
14671467
*,
1468-
data=True,
1469-
wcs=True,
1470-
uncertainty=True,
1471-
mask=True,
1472-
unit=True,
1473-
meta=True,
1474-
psf=True,
1468+
data="copy",
1469+
wcs="copy",
1470+
uncertainty="copy",
1471+
mask="copy",
1472+
unit="copy",
1473+
meta="copy",
1474+
psf="copy",
14751475
nddata_type=NDData,
14761476
**kwargs,
14771477
):
@@ -1482,7 +1482,7 @@ def to_nddata(self,
14821482
this object, values can be altered on the output object by
14831483
setting a kwarg with the new value, e.g. ``data=new_data``.
14841484
Custom attributes on this class can be passed by setting that
1485-
keyword to `True`, for example ``mycube.to_nddata(spam=True)``
1485+
keyword to `"copy"`, for example ``mycube.to_nddata(spam="copy")``
14861486
is the equivalent of setting
14871487
``mycube.to_nddata(spam=mycube.spam)``.
14881488
Any attributes not supported by the new class
@@ -1510,9 +1510,10 @@ def to_nddata(self,
15101510
kwargs:
15111511
Additional inputs to the ``nddata_type`` constructor. For example, to
15121512
set different data values on the returned object, set a kwarg ``data=new_data``,
1513-
where ``new_data`` is an array of compatible shape and dtype. Note that kwargs
1514-
given by the user and attributes on this instance that are not supported by the
1515-
``nddata_type`` constructor are ignored.
1513+
where ``new_data`` is an array of compatible shape and dtype.
1514+
Other keyword arguments can be specified to copy custom
1515+
attributes with the value ``"copy"``, for example
1516+
``global_coords="copy"``.
15161517
15171518
Returns
15181519
-------
@@ -1543,8 +1544,10 @@ def to_nddata(self,
15431544
"meta": meta,
15441545
"psf": psf,
15451546
**kwargs}
1546-
# If any are True then copy by reference
1547-
user_kwargs = {key: getattr(self, key) if value is True else value for key, value in user_kwargs.items()}
1547+
# If any are "copy" then copy by reference
1548+
user_kwargs = {key: getattr(self, key)
1549+
if isinstance(value, str) and value == "copy" else value
1550+
for key, value in user_kwargs.items()}
15481551
# Construct and return new instance.
15491552
return nddata_type(**user_kwargs)
15501553

ndcube/tests/test_ndcube.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ def test_to_nddata_type_ndcube(ndcube_2d_ln_lt_uncert_ec):
272272
ndc = ndcube_2d_ln_lt_uncert_ec
273273
ndc.global_coords.add("wavelength", "em.wl", 100*u.nm)
274274
new_data = ndc.data * 2
275-
output = ndc.to_nddata(data=new_data, extra_coords=True, global_coords=True, nddata_type=NDCube)
275+
output = ndc.to_nddata(data=new_data, extra_coords="copy", global_coords="copy", nddata_type=NDCube)
276276
assert type(output) is NDCube
277277
assert (output.data == new_data).all()
278278
helpers.assert_extra_coords_equal(output.extra_coords, ndc.extra_coords)
@@ -288,7 +288,7 @@ def __init__(self, data, *, spam=None, **kwargs):
288288
super().__init__(data, **kwargs)
289289
self.spam = spam
290290

291-
new_ndd = ndc.to_nddata(spam=True, nddata_type=MyNDData)
291+
new_ndd = ndc.to_nddata(spam="copy", nddata_type=MyNDData)
292292
assert new_ndd.spam == "Eggs"
293293
assert new_ndd.data is ndc.data
294294
assert new_ndd.wcs is ndc.wcs

0 commit comments

Comments
 (0)