-
Notifications
You must be signed in to change notification settings - Fork 262
RF: Circumvents a deprecation warning from np.fromstring
#702
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
Thanks. Looks like Any chance you could swap out |
Any chance you could swap out numpy.fromstring() in the rest of the
codebase
<https://github.com/nipy/nibabel/search?q=fromstring&unscoped_q=fromstring>,
while you're at it?
Yep. Will do.
|
As anticipated, pre-release numpy doesn't allow setting this array to writeable. Do you know if there is any fix to this? |
Codecov Report
@@ Coverage Diff @@
## master #702 +/- ##
==========================================
+ Coverage 88.85% 88.88% +0.03%
==========================================
Files 93 93
Lines 11454 11452 -2
Branches 1894 1892 -2
==========================================
+ Hits 10177 10179 +2
+ Misses 936 933 -3
+ Partials 341 340 -1
Continue to review full report at Codecov.
|
OK. I believe that remaining appearances of |
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.
Great! Thanks. We can be a little more efficient with the TrkFile
header.
As for the other calls, as long as tests pass and coverage hits them, I think we should be safe.
nibabel/streamlines/trk.py
Outdated
@@ -559,8 +559,8 @@ def _read_header(fileobj): | |||
|
|||
# Read the header in one block. | |||
header_str = f.read(header_2_dtype.itemsize) | |||
header_rec = np.fromstring(string=header_str, dtype=header_2_dtype) | |||
|
|||
header_rec = np.frombuffer(buffer=bytearray(header_str), |
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 still makes a copy from header_str
to bytearray(header_str)
. Instead, we can create a bytearray and read directly into it:
header_buf = bytearray(header_2_dtype.itemsize)
n_read = f.readinto(header_buf)
# Maybe if n_read != header_2_dtype.itemsize: raise HeaderError(...)
header_rec = np.frombuffer(buffer=header_buf, dtype=header_2_dtype)
But the `Opener` object used here doesn't have a `readinto` buffered read
method implemented, as far as I can understand.
…On Mon, Dec 17, 2018 at 3:04 PM Chris Markiewicz ***@***.***> wrote:
***@***.**** commented on this pull request.
Great! Thanks. We can be a little more efficient with the TrkFile header.
As for the other calls, as long as tests pass and coverage hits them, I
think we should be safe.
------------------------------
In nibabel/streamlines/trk.py
<#702 (comment)>:
> @@ -559,8 +559,8 @@ def _read_header(fileobj):
# Read the header in one block.
header_str = f.read(header_2_dtype.itemsize)
- header_rec = np.fromstring(string=header_str, dtype=header_2_dtype)
-
+ header_rec = np.frombuffer(buffer=bytearray(header_str),
This still makes a copy from header_str to bytearray(header_str).
Instead, we can create a bytearray and read directly into it:
header_buf = bytearray(header_2_dtype.itemsize)
n_read = f.readinto(header_buf)
# Maybe if n_read != header_2_dtype.itemsize: raise HeaderError(...)
header_rec = np.frombuffer(buffer=header_buf, dtype=header_2_dtype)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#702 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAHPNsDQ1FR_g7ByQLFhjOP0Rst0FtEsks5u6CMFgaJpZM4ZXJsu>
.
|
Oh, scratch that. Now it does :-)
…On Mon, Dec 17, 2018 at 3:29 PM Ariel Rokem ***@***.***> wrote:
But the `Opener` object used here doesn't have a `readinto` buffered read
method implemented, as far as I can understand.
On Mon, Dec 17, 2018 at 3:04 PM Chris Markiewicz ***@***.***>
wrote:
> ***@***.**** commented on this pull request.
>
> Great! Thanks. We can be a little more efficient with the TrkFile header.
>
> As for the other calls, as long as tests pass and coverage hits them, I
> think we should be safe.
> ------------------------------
>
> In nibabel/streamlines/trk.py
> <#702 (comment)>:
>
> > @@ -559,8 +559,8 @@ def _read_header(fileobj):
>
> # Read the header in one block.
> header_str = f.read(header_2_dtype.itemsize)
> - header_rec = np.fromstring(string=header_str, dtype=header_2_dtype)
> -
> + header_rec = np.frombuffer(buffer=bytearray(header_str),
>
> This still makes a copy from header_str to bytearray(header_str).
> Instead, we can create a bytearray and read directly into it:
>
> header_buf = bytearray(header_2_dtype.itemsize)
> n_read = f.readinto(header_buf)
> # Maybe if n_read != header_2_dtype.itemsize: raise HeaderError(...)
> header_rec = np.frombuffer(buffer=header_buf, dtype=header_2_dtype)
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <#702 (review)>,
> or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAHPNsDQ1FR_g7ByQLFhjOP0Rst0FtEsks5u6CMFgaJpZM4ZXJsu>
> .
>
|
@@ -209,6 +209,9 @@ def fileno(self): | |||
def read(self, *args, **kwargs): | |||
return self.fobj.read(*args, **kwargs) | |||
|
|||
def readinto(self, *args, **kwargs): | |||
return self.fobj.readinto(*args, **kwargs) | |||
|
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, nice. I hadn't realized we didn't already have this...
Any suggestions on how to test that? I got a bit lost in test_openers
…On Mon, Dec 17, 2018 at 5:05 PM Chris Markiewicz ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In nibabel/openers.py
<#702 (comment)>:
> @@ -209,6 +209,9 @@ def fileno(self):
def read(self, *args, **kwargs):
return self.fobj.read(*args, **kwargs)
+ def readinto(self, *args, **kwargs):
+ return self.fobj.readinto(*args, **kwargs)
+
Oh, nice. I hadn't realized we didn't already have this...
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#702 (review)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAHPNgB6TGERGdJtaTQja80Ho4NDwqFtks5u6D9wgaJpZM4ZXJsu>
.
|
I think this function is as close as we come to fully testing the API for equivalence: nibabel/nibabel/tests/test_openers.py Lines 198 to 213 in a960b92
|
Looks like we have similar issues in other places as well. Let me see if I can use similar approaches in these other places as well... |
Awesome, thanks. In #700, I'll be fixing the |
OK. I'll look into those. |
Sorry: I have not been able to resolve these other spots and might not have time to work on this any time soon. If it's OK, I would suggest merging this one, and then keep working on the other ones on another PR. |
``` /Users/arokem/.virtualenvs/afq/lib/python3.7/site-packages/nibabel/streamlines/trk.py:562: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead header_rec = np.fromstring(string=header_str, dtype=header_2_dtype) ```
This is because newer numpy doesn't allow to change the writeable flag.
a696d0d
to
6924a56
Compare
That's great. Thanks! |
I am seeing this warning:
I believe this PR fixes that.