Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Avoid temporaries when preparing step plots. #7454
Conversation
| + steps = np.zeros((1 + len(args), 2 * len(x) - 1)) | ||
| + steps[0, 0::2] = x | ||
| + steps[0, 1::2] = steps[0, 0::2][:-1] | ||
| + for out, y in zip(steps[1:], args): |
tacaswell
Nov 14, 2016
Owner
This is trading memory duplication for a python for-loop. Did you bench mark this it terms of time?
anntzer
Nov 14, 2016
Contributor
Actually the previous implementation also had a for loop over the arguments during validation (line 2314 args = tuple(np.asanyarray(y) for y in args)). So we're even on that.
| - x = np.asanyarray(x) | ||
| - if x.ndim != 1: | ||
| - raise ValueError("x must be 1 dimensional") | ||
| - if len(args) == 0: |
tacaswell
Nov 14, 2016
Owner
Looks like this no longer raises if there are no y-values. I think I am
tacaswell
added this to the
2.1 (next point release)
milestone
Nov 14, 2016
| - cbook._step_validation(np.arange(12), 'a') | ||
| - | ||
| - with pytest.raises(ValueError): | ||
| - cbook._step_validation(np.arange(12)) |
|
I am mostly |
|
Looks like the tuple return is legacy of how the step logic got factored out the line code. I wrote it and no longer remember why. +0.5 on removing the All of the widows test are failing on conda-build related issues. |
tacaswell
changed the title from
Avoid temporaries when preparing step plots. to [MRG+1] Avoid temporaries when preparing step plots.
Nov 14, 2016
| - return tuple(steps) | ||
| + steps = np.zeros((1 + len(args), 2 * len(x) - 1)) | ||
| + steps[0, 0::2] = x | ||
| + steps[0, 1::2] = steps[0, 0::2][:-1] |
anntzer
Nov 14, 2016
Contributor
I find this easier to read -- otherwise can you tell immediately whether it should be steps[0, 0:-1:2] or steps[0, 0:-2:2]? Also, I'm not writing x[-1] because that slicing may be not so cheap (I'm looking at you xarray). Same reasons apply below.
QuLogic
Nov 14, 2016
Member
Either one works, so I guess that's just up to you...
I'm not sure to which x[-1] you are referring in the second sentence?
| - return tuple(steps) | ||
| + steps = np.zeros((1 + len(args), 2 * len(x) - 1)) | ||
| + steps[0, 0::2] = x | ||
| + steps[0, 1::2] = steps[0, 0::2][1:] |
| @@ -7,32 +7,29 @@ | ||
| from __future__ import (absolute_import, division, print_function, | ||
| unicode_literals) | ||
| -import six |
anntzer
Nov 14, 2016
Contributor
The PEP8 order is stdlib (warnings), third-party (numpy, six) and internals (everything else). Do we prefer always putting six first? (I don't really care.)
|
Can we remove them without a deprecation period? :-) |
|
They have only been there from 1.5.0 on so relatively young. If @anntzer is concerned, I am perfectly happy to leave the imports in place |
|
Too late, they're dead now :-) |
| @@ -0,0 +1,6 @@ | ||
| +Functions removed from the `lines` module |
|
One major issue with this is that it is going to strip units and cast everything to a float. attn @dopplershift |
|
That (casting everything to a float) was already the case before? |
|
This is true. Should not hold the PR over that, but we will probably have to address the units sooner or later. |
|
So long as it's no worse than before, it's fine by me. I'll just keep this in the back of my mind until I need a step plot. |
| + steps[0, 0::2] = x | ||
| + steps[0, 1::2] = steps[0, 0::2][:-1] | ||
| + steps[1:, 0::2] = args | ||
| + steps[1:, 1::2] = steps[1:, ::2][:, 1:] |
QuLogic
Nov 14, 2016
•
Member
This is one place where I find two slices more confusing.
Start at 2 and take every other one, is much clearer than take every other one and start at 1 (which is really 2 from the original array).
anntzer commentedNov 14, 2016
The previous implementation of
pts_to_{pre,mid,post}step was fairlyunefficient: it allocated a large array during validation (
vstack),then a second array to build the return tuple, then converted the
columns to a tuple, then immediately converted the tuple back to a new
array at the call site (to construct a Path). Instead, just create a
single array and work with it all along.
Also some minor related cleanups (moving imports in lines.py, in
particular suggesting to not expose the individual
pts_to_*stepfunctions anymore (they are not directly used, only via the
STEP_LOOKUP_MAPmapping).