-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Remove unnessary numpy lookups for int, float, complex, bool and str #6603
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
Conversation
|
Hi there @MSeifert04 👋 - thanks for the pull request! I'm just a friendly 🤖 that checks for issues related to the changelog and making sure that this pull request is milestoned and labelled correctly. This is mainly intended for the maintainers, so if you are not a maintainer you can ignore this, and a maintainer will let you know if any action is required on your part 😃. Everything looks good from my point of view! 👍 If there are any issues with this message, please report them here |
|
Now at the right PR: @MSeifert04 - I didn't look at everything, but |
|
|
||
|
|
||
| NUMS = [1, 1., np.float(1.), np.float32(1.), np.float64(1.)] | ||
| NUMS = [1, 1., np.float32(1.), np.float64(1.)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one of the cases where I wasn't sure what was intended. Given that np.float(1.) is identical to 1. I simply removed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
astropy/table/tests/test_column.py
Outdated
| ndarray or MaskedArray, even in the case of a multi-dim column. | ||
| """ | ||
| integer_types = (int, np.int) | ||
| integer_types = [int] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one was a bit weird. Was it indentded to test np.int_?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked back at #3095, and, yes, this is definitely meant to test all possible integer types, so replace this with np.int_. (The actual code looks for subclasses of int and np.integer)
|
This touches too many sub-packages of various functionalities. I recommend that we keep a check list of sub-packages being touched and whether each is approved by its lead/deputy maintainer; Just to be on the safe side. |
|
Actually the changes don't change anything, except that they removes some obvious redundancy. But a checklist is probably in order. 😄 |
|
Just want to ensure due diligence to ensure that the redundancy wasn't added on purpose. But if I am not making sense, then feel free to ignore. 😅 |
|
Well, given that However it's impossible to be really sure on that. But I can re-add the redundancy so the PR doesn't change anything except for the |
astropy/utils/misc.py
Outdated
| if isinstance(obj, (np.ndarray, np.number)): | ||
| return obj.tolist() | ||
| elif isinstance(obj, (complex, np.complex)): | ||
| elif isinstance(obj, (complex, np.complexfloating)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if that check is correct or if I should rather have removed the second type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, dear. I added this in #552 to support Cone Search. I'll have to find time to dig through the 181 comments to see why I wrote this the way I did...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, on second thought I should just remove the second type. Currently it was only checking for complex, complex. But np.complexfloating would be already catched by the np.number isinstance check (np.complexfloating is a subclass of np.number) above so it's likewise a no-op.
| ('U4', STRING_TYPE_NAMES[(True, 'U')] + '4'), | ||
| (np.void, 'void'), | ||
| (np.int32, 'int32'), | ||
| (np.bool, 'bool'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was identical to the next line so I removed it.
astropy/table/column.py
Outdated
| - Python non-string type (float, int, bool) | ||
| - Numpy non-string type (e.g. np.float32, np.int64, np.bool) | ||
| - Numpy non-string type (e.g. np.float32, np.int64, bool) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should've been np.bool_ given that it refers to the NumPy type.
|
I think I commented all non-trivial changes made here. The other changes were rather trivial: |
mhvk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MSeifert04 - coordinates is all OK, and for cosmology most of the dtype can be removed (those in tests are OK).
astropy/cosmology/core.py
Outdated
|
|
||
| # Tcmb may have units | ||
| self._Tcmb0 = u.Quantity(Tcmb0, unit=u.K, dtype=np.float) | ||
| self._Tcmb0 = u.Quantity(Tcmb0, unit=u.K, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be removed altogether (the default is float64, which is what one would want here).
astropy/cosmology/core.py
Outdated
|
|
||
| # Hubble parameter at z=0, km/s/Mpc | ||
| self._H0 = u.Quantity(H0, unit=u.km / u.s / u.Mpc, dtype=np.float) | ||
| self._H0 = u.Quantity(H0, unit=u.km / u.s / u.Mpc, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here dtype can be removed as well.
astropy/cosmology/core.py
Outdated
| # Only massless | ||
| return u.Quantity(np.zeros(self._nmasslessnu), u.eV, | ||
| dtype=np.float) | ||
| dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here.
astropy/cosmology/core.py
Outdated
| # Only massive | ||
| return u.Quantity(self._massivenu_mass, u.eV, | ||
| dtype=np.float) | ||
| dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here...
astropy/cosmology/core.py
Outdated
| numass = np.append(np.zeros(self._nmasslessnu), | ||
| self._massivenu_mass.value) | ||
| return u.Quantity(numass, u.eV, dtype=np.float) | ||
| return u.Quantity(numass, u.eV, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here again.
astropy/cosmology/core.py
Outdated
| z = np.asarray(z) | ||
| if self._Onu0 == 0: | ||
| return np.zeros(np.asanyarray(z).shape, dtype=np.float) | ||
| return np.zeros(np.asanyarray(z).shape, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove dtype here too.
astropy/cosmology/core.py
Outdated
| else: | ||
| return prefac * self._Neff *\ | ||
| np.ones(np.asanyarray(z).shape, dtype=np.float) | ||
| np.ones(np.asanyarray(z).shape, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ones, float is also the default.
astropy/cosmology/core.py
Outdated
| return -1.0 | ||
| else: | ||
| return -1.0 * np.ones(np.asanyarray(z).shape, dtype=np.float) | ||
| return -1.0 * np.ones(np.asanyarray(z).shape, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same.
astropy/cosmology/core.py
Outdated
| return 1. | ||
| else: | ||
| return np.ones(np.asanyarray(z).shape, dtype=np.float) | ||
| return np.ones(np.asanyarray(z).shape, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
astropy/cosmology/core.py
Outdated
| return self._w0 | ||
| else: | ||
| return self._w0 * np.ones(np.asanyarray(z).shape, dtype=np.float) | ||
| return self._w0 * np.ones(np.asanyarray(z).shape, dtype=float) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
|
@taldcroft Could you have a look? Especially the table test which checked |
mhvk
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked through all remaining stuff and think it is OK modulo the comments for table and io.misc. I think this is ready to go in with those taken into account.
astropy/table/tests/test_column.py
Outdated
| ndarray or MaskedArray, even in the case of a multi-dim column. | ||
| """ | ||
| integer_types = (int, np.int) | ||
| integer_types = [int] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked back at #3095, and, yes, this is definitely meant to test all possible integer types, so replace this with np.int_. (The actual code looks for subclasses of int and np.integer)
|
|
||
| # See explanation in _refresh_table_as_needed for these conditions | ||
| auto_max_age = (conf.auto_max_age if conf.auto_max_age is not None | ||
| else np.finfo(np.float).max) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is OK.
|
|
||
|
|
||
| NUMS = [1, 1., np.float(1.), np.float32(1.), np.float64(1.)] | ||
| NUMS = [1, 1., np.float32(1.), np.float64(1.)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
astropy/io/misc/tests/test_hdf5.py
Outdated
| ALL_DTYPES = [np.uint8, np.uint16, np.uint32, np.uint64, np.int8, | ||
| np.int16, np.int32, np.int64, np.float32, np.float64, | ||
| np.bool, '|S3'] | ||
| bool, '|S3'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't really matter, but I think this more logically is np.bool_ (or is alias, np.bool8)
astropy/io/misc/tests/test_yaml.py
Outdated
| np.float(2.0), np.float64(), | ||
| np.complex(3, 4), np.complex_(3 + 4j), | ||
| 2.0, np.float64(), | ||
| complex(3, 4), np.complex_(3 + 4j), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just 3 + 4j?
astropy/io/misc/yaml.py
Outdated
| AstropyDumper.add_representer(np.bool_, | ||
| yaml.representer.SafeRepresenter.represent_bool) | ||
| for np_type in [np.int_, np.int, np.intc, np.intp, np.int8, np.int16, np.int32, | ||
| for np_type in [np.int_, int, np.intc, np.intp, np.int8, np.int16, np.int32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here int can be removed, since yaml already has it built in.
astropy/io/misc/yaml.py
Outdated
| AstropyDumper.add_representer(np_type, | ||
| yaml.representer.SafeRepresenter.represent_int) | ||
| for np_type in [np.float_, np.float, np.float16, np.float32, np.float64, | ||
| for np_type in [np.float_, float, np.float16, np.float32, np.float64, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same for float
| AstropyDumper.add_representer(np_type, | ||
| yaml.representer.SafeRepresenter.represent_float) | ||
| for np_type in [np.complex_, np.complex, np.complex64, np.complex128]: | ||
| for np_type in [np.complex_, complex, np.complex64, np.complex128]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But not for complex since this is a custom representer.
|
Well I admit I did not realize that |
Yes, that's the main driver for the PR. It's not actually about the lookup cost or if NumPy will ever deprecate them (probably not). However as can be seen by the existing "complicated" cases it has been leading to unintended behavior, almost "problems". For example testing the wrong thing, overwriting the YAML writer for plain |
|
Agreed with @MSeifert04 here - I didn't know either that |
It is unnecessary because it subclasses np.number which would go into the if above. Also use np.bool_ in table column docs because it explicitly lists it as NumPy type.
|
OK, fair enough. |
|
OK, this seems all done, so merging. Thanks, @MSeifert04! |
|
Thanks everyone for the reviews and comments. 👍 |
As discussed in astropy#6603 (and numpy/numpy#6103) it's unnecessary and misleading to use the Python types from the NumPy namespace. With this PR this uses the default NumPy dtype corresponding to these Python types.
…py_lookup Remove unnessary numpy lookups for int, float, complex, bool and str
…py_lookup Remove unnessary numpy lookups for int, float, complex, bool and str
The
np.int,np.float, etc. are just aliases for the Python builtins so we really shouldn't be using them. Note that NumPy recently did the same (based on this issue).The aliases are:
np.objectnp.strnp.intnp.floatnp.complexnp.boolBut not the ones that end with an underscore (
np.float_) or a number (np.int32) ornp.intpandnp.intcwhich are actually different.Note that the PR probably fixed "too much". I just replaced the
np.in front of these or removed it altogether where it didn't make sense. However, in some cases it could just be that the author wanted to usenp.*_with the trailing underscore. So this probably requires a careful review.Checklist: