Skip to content

Ensure data is a copy in outputs

Martin Lange requested to merge test-outputs-copy into main

Purpose

Currently, it is possible to push the same data instance multiple times. When modifying that instance inplace in the source component, this causes changes of the data downstream.
We should prevent that.

Fixes #82 (closed).

Action taken

  • Add tests to check that data pulled from outputs is a copy of the pushed data

Problems

The failing tests show that data is not copied if pushed units and expected units are equal, for both xarray and numpy arrays.

What I did in the tests is set a value in the source data array, and check that it does not change in the pulled target data. But it does.

Possible solutions

Not sure if we should really force copy in to_xarray() or Output.push_data(), as it might be unnecessary. Maybe outputs could check if successive data has the same underlying data (i.e. object identity), and complain?

!218 (merged) Implements this check. Does not fix the problem for CallbackOutput though.

Alternatively, we could use np.may_share_memory() to check in to_xarray() or Output.push_data() if copying is required, and do so if.

Implemented in !219 (closed).

Edited by Martin Lange

Merge request reports

Loading