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

BUG: fix unit conversion for array with dtype int8 #197

Merged
merged 3 commits into from
May 28, 2022

Conversation

neutrinoceros
Copy link
Member

@neutrinoceros neutrinoceros commented Aug 11, 2021

This dtype was apparently overlooked in PR #56

import unyt as u
a = u.unyt_array(1, "cm", dtype="int8")
a.to("km") 

fails with

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-fe344d50e9a4> in <module>
      1 import unyt as u
      2 a = u.unyt_array(1, "cm", dtype="int8")
----> 3 a.in_units("km")

~/dev/python/yt-project/unyt/unyt/array.py in in_units(self, units, equivalence, **kwargs)
    845                     )
    846             new_dtypekind = "c" if self.dtype.kind == "c" else "f"
--> 847             new_dtype = np.dtype(new_dtypekind + str(dsize))
    848             ret = np.asarray(self.ndview * conversion_factor, dtype=new_dtype)
    849             if offset:

TypeError: data type 'f1' not understood

the root cause is that float8 dtype doesn't exist. The fix is to force minimal itemsize in bytes to 2, rather than copying it directly from the input dtype, so input array with dtype int8 gets converted to float16.

While I'm at it I also patched unyt_array.convert_to_units so that it raises a clearer error message in case the user attempts to convert a int8 array in place (which isn't possible, I think)

@neutrinoceros neutrinoceros added the bug Something isn't working label Aug 11, 2021
@neutrinoceros neutrinoceros force-pushed the hotfix_support_int8 branch 6 times, most recently from e5be382 to 57e3766 Compare August 11, 2021 13:40
@neutrinoceros neutrinoceros marked this pull request as ready for review August 11, 2021 13:47
@neutrinoceros neutrinoceros changed the title FIX: fix a bug where unyt_array instance with dtype int8 fail to convert BUG: fix a bug where unyt_array instance with dtype int8 fail to convert Sep 13, 2021
@neutrinoceros neutrinoceros changed the title BUG: fix a bug where unyt_array instance with dtype int8 fail to convert BUG: fix unit conversion for array with dtype int8 Sep 13, 2021
@neutrinoceros
Copy link
Member Author

@ngoldbaum does this look okay to you ?

@jzuhone jzuhone merged commit eb511a2 into yt-project:master May 28, 2022
@neutrinoceros neutrinoceros deleted the hotfix_support_int8 branch May 28, 2022 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants