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

Added new features to the ndcube._add_ method #794

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

PCJY
Copy link
Contributor

@PCJY PCJY commented Dec 11, 2024

PR Description

ndcube/ndcube.py Outdated Show resolved Hide resolved
ndcube/ndcube.py Outdated Show resolved Hide resolved
ndcube/ndcube.py Outdated
Comment on lines 930 to 931
# addition
new_data = self.data + value_data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addition should be done as part of the masked array addition. You've already done this below, you just need to extract the added data from the results as well as the mask.

ndcube/ndcube.py Outdated
Comment on lines 950 to 952
return self._new_instance(
data=new_data, uncertainty=new_uncertainty, mask=new_mask
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having a separate return here for the NDData case, I think we should build a dictionary of kwargs that we can give self._new_instance, here. So, you can create an empty kwargs dictionary at the start of the method, and add the new data, uncertainty, etc. in the relevant place, e.g.

kwargs["uncertainty"] = new_uncertainty

Then the final line of the method would become

return self._new_instance(**kwargs)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this doesn't make sense

@nabobalis nabobalis added this to the 2.4.0 milestone Dec 18, 2024
ndcube/ndcube.py Outdated
if self.uncertainty is not None and value.uncertainty is not None:
new_uncertainty = self.uncertainty.propagate(
np.add, value.uncertainty, correlation=0
np.add, value.uncertainty, result_data = value.data, correlation=0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The result_data needs to be the result of the operation. So, assuming you moved the addition of the datas using the masked array to before the uncertainty propagation, you could do:

Suggested change
np.add, value.uncertainty, result_data = value.data, correlation=0
np.add, value.uncertainty, result_data = kwargs["data"], correlation=0

ndcube/ndcube.py Outdated
Comment on lines 1061 to 1070
# combine mask
self_ma = np.ma.MaskedArray(self.data, mask=self.mask)
value_ma = np.ma.MaskedArray(value_data, mask=value.mask)

# addition
result_ma = self_ma + value_ma
new_mask = result_ma.mask

# extract new mask and new data
kwargs["mask"] = result_ma.mask
kwargs["data"] = result_ma.data
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in above comment, I think it makes sense to do this before the uncertainty propagation so you can use the kwargs["data"] value in that propagation.

ndcube/ndcube.py Outdated
kwargs["data"] = result_ma.data

# return the new NDCube instance
return self._new_instance(**kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this line to the end of the method and use the kwargs approach when handling the other cases, e.g. Quantity. So, for example, L1082 would become:

kwargs["data"] = self.data + value.to_value(cube_unit)

@DanRyanIrish
Copy link
Member

A changelog file needs to be added.

And your branch needs to be updated with the latest version of main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants