-
Notifications
You must be signed in to change notification settings - Fork 49
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
convert_to_unit
a unyt_array
may cause problem in previously created slices.
#552
Comments
Thanks for reporting this !
I think they would be expected to follow the change: array slices are views into the original array's memory so any mutation on the array should be reflected in views.
most definitely. Would you like to try and fix this yourself ? |
Actually, this is another case of "mixing units with integral datatypes doesn't work". import numpy as np
def convert_to_float_inplace(arr):
new_dtype = f"f{arr.dtype.itemsize}"
if arr.dtype.kind in 'ui':
# copy original array
new_values = arr.astype(new_dtype)
arr.dtype = new_dtype
np.copyto(arr, new_values)
arr = np.arange(5)
arr_slice = arr[1:3]
convert_to_float_inplace(arr)
print(arr)
print(arr_slice) outputs
the values as printed on the last line are not random, rather, they are the result of interpreting floating point memory as integers. |
I see now that the issue I raised is more of a NumPy issue than a unyt issue. However, I apologize for falling into an XY problem. The original reason that led me to bring up this issue is as follows: from unyt import unyt_array as YTArray
arr = YTArray([1.,2.,3.,4.,5.], "m")
arr2 = arr[:2]
arr.convert_to_units("cm")
arr2
# unyt_array([100., 200.], 'm')
# The numerical values in the slice changed, but the units remained the same.
arr = YTArray([1.,2.,3.,4.,5.], "m")
arr2 = arr[:2]
arr2.convert_to_units("cm")
arr
# unyt_array([100., 200., 3., 4., 5.], 'm')
# The units remained, and only the numbers within the slice were affected. Of course, I understand that this behavior is entirely inherited from Personally, I would expect that if a I'm not sure if this expectation is reasonable or how challenging it would be to implement. However, even if it's not feasible, wouldn't it be better to raise warnings for such operations? |
no need to, that's a very easy mistake to make !
This is likely impossible in the general case: an array doesn't hold references to its views so it cannot propagate the modification. Although views are detectable at runtime, they only have one reference to their base array, and couldn't possibly propagate modification beyond that (admitting that it is desirable in the first place).
One thing that might be possible is to raise a a warning or an exception when a view is modified in place (views are detectable at runtime). However that would need to be done consistently throughout the whole API (that may be a substantial amount of work), and raising an exception would be a breaking change, so I think we'd need to go with a warning. I would happily review a pull request implementing such a warning, but I will not invest my own free time building it. In short, I would recommend avoiding in-place operations if you can afford it, or being extra careful around them if you can't, but I'm not convinced that we can do much about it on our side. |
Description
If I have a
unyt_array
and have created some slices from it, modifying the original array withconvert_to_unit
will cause those slices to produce incorrect results, and vice versa.From a design perspective, I'm not sure whether them should update accordingly or remain unchanged, but the current behavior is definitely problematic.
What I Did
The text was updated successfully, but these errors were encountered: