-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Additional cleanups #7547
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
Additional cleanups #7547
Conversation
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's long, but I don't see too many major issues.
print(border) | ||
print(' '.join(['symbol'.ljust(maxfunc), 'description'.ljust(maxdoc)])) | ||
print('{:<{}} {:<{}}'.format('symbol', maxfunc, 'description', maxdoc)) |
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.
print
inserts a space, so print('symbol.ljust(maxfunc), 'description'.ljust(maxdoc))
would have worked, right?
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.
Yes but that looks a bit "unintended" to me.
Z = np.arange(10000.0) | ||
Z.shape = 100, 100 | ||
Z[:, 50:] = 1. | ||
Z = np.arange(10000).reshape(100, 100) |
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.
Missing parentheses in shape.
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.
fixed
fig = plt.figure(frameon=False) | ||
|
||
Z1 = np.array(([0, 1]*4 + [1, 0]*4)*4) | ||
Z1.shape = (8, 8) # chessboard | ||
Z1 = np.add.outer(range(8), range(8)) % 2 # chessboard |
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.
What does np.add.outer
do (it works, but I've never seen it used that way)?
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 makes the first argument a column array and the second a row array, broadcasts them and applies the ufunc.
@@ -9,8 +9,7 @@ | |||
|
|||
# Data are 256x256 16 bit integers | |||
dfile = cbook.get_sample_data('s1045.ima.gz') | |||
im = np.fromstring(dfile.read(), np.uint16).astype(float) | |||
im.shape = (256, 256) | |||
im = np.fromstring(dfile.read(), np.uint16).reshape((256, 256)) |
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.
No astype
?
Also, can write np.fromfile(dfile, np.uint16)
instead of the two step process.
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.
you can imshow an integer array just as well.
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.
Does the one below this still need astype
, then?
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.
fixed below too.
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.
Actually, fromfile doesn't work with gzip.open'ed files (it seems to try to construct an array from the compressed representation). Reverting to read()
.
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.
Ah, unfortunate. Based on np.fromfile
seeming to need fileno
(since it doesn't work with BytesIO
), it looks like it tries to stream from the fd directly, which GzipFile
points at the underlying file object. In a way, it's a half-a-bug in both coming together to make one.
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.
Left a comment on numpy/numpy#7713; we'll see where it goes...
@@ -16,8 +16,7 @@ | |||
|
|||
# Load the MRI data (256x256 16 bit integers) | |||
dfile = cbook.get_sample_data('s1045.ima.gz') | |||
im = np.fromstring(dfile.read(), np.uint16).astype(float) | |||
im.shape = (256, 256) | |||
im = np.fromstring(dfile.read(), np.uint16).astype(float).reshape((256, 256)) |
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 np.fromfile
comment as above.
for val in six.itervalues(self.alphaStates)])) | ||
self.writeObject( | ||
self.alphaStateObject, | ||
{val[0]: val[1] for val in six.itervalues(self.alphaStates)}) | ||
self.writeHatches() | ||
self.writeGouraudTriangles() | ||
xobjects = dict(x[1:] for x in six.itervalues(self._images)) |
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.
One more dict comprehension?
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.
yes
@@ -1408,15 +1408,15 @@ def finddir(o, match, case=False): | |||
|
|||
def reverse_dict(d): |
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.
Could probably be used in a couple places you've updated...
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'd rather deprecate this function... I think everyone can understand what {v: k for k, v in d.items()}
does and this way you don't have to go and check whether the function does something funny with duplicates.
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.
No argument there.
@@ -2078,7 +2078,7 @@ def unmasked_index_ranges(mask, compressed=True): | |||
# The ls_mapper maps short codes for line style to their full name used | |||
# by backends | |||
# The reverse mapper is for mapping full names to short ones | |||
ls_mapper_r = dict([(ls[1], ls[0]) for ls in _linestyles]) | |||
ls_mapper_r = reverse_dict(ls_mapper) |
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.
Can _linestyles
be dropped and ls_mapper
written as a literal now?
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.
yes
theta *= (np.pi / 180.0) | ||
theta.shape = (theta.shape[0], 1) # for broadcasting | ||
theta = ma.masked_invalid(np.deg2rad(self.angles)).filled(0) | ||
theta = theta.reshape((-1, 1)) # for broadcasting |
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.
Was the change of indent intended?
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.
no, fixed.
exponent -= 1 | ||
scale = max([self.numticks-1, 1]) ** (-exponent) | ||
exponent = round(math.log10(vmax - vmin) | ||
/ math.log10(max(self.numticks - 1, 1))) - 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.
I tested some completely arbitrary numbers that perhaps will never exist in these lines, but I'm not sure the result is the same. Perhaps I missed something:
>>> vr = np.linspace(1e-8, 1000, 100000) # vmax - vmin
>>> numticks = np.arange(30) # I expect 0 is invalid, but no matter...
>>> exponent, remainder = divmod(np.log10(vr[:, None]), np.log10(np.maximum(numticks - 1, 1))[None, :])
__main__:1: RuntimeWarning: invalid value encountered in floor_divide
__main__:1: RuntimeWarning: invalid value encountered in remainder
>>> exponent[remainder < 0.5] -= 1
__main__:1: RuntimeWarning: invalid value encountered in less
>>> exponent2 = np.round(np.log10(vr)[:, None] / np.log10(np.maximum(numticks - 1, 1))[None, :]) - 1
>>> exponent
array([[ nan, nan, nan, ..., -7., -6., -6.],
[ nan, nan, nan, ..., -2., -2., -2.],
[ nan, nan, nan, ..., -2., -2., -2.],
...,
[ nan, nan, nan, ..., 1., 1., 1.],
[ nan, nan, nan, ..., 1., 1., 1.],
[ nan, nan, nan, ..., 1., 1., 1.]])
>>> exponent2
array([[-inf, -inf, -inf, ..., -7., -7., -7.],
[-inf, -inf, -inf, ..., -2., -2., -2.],
[-inf, -inf, -inf, ..., -2., -2., -2.],
...,
[ inf, inf, inf, ..., 1., 1., 1.],
[ inf, inf, inf, ..., 1., 1., 1.],
[ inf, inf, inf, ..., 1., 1., 1.]])
>>> np.sum(exponent == exponent2)
2388308
>>> np.size(exponent)
3000000
# Percentage the same including nan/inf:
>>> np.sum(exponent == exponent2)/np.size(exponent)
0.79610266666666663
>>> mask = np.isfinite(exponent)
>>> np.all(mask == np.isfinite(exponent2))
True
>>> np.sum(exponent[mask] == exponent2[mask])
2388308
# Percentage of finite values that are the same:
>>> np.sum(exponent[mask] == exponent2[mask]) / np.sum(mask)
0.88455851851851852
>>>
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.
You are correct that this is different: the previous implementation compares the remainder with 0.5 whereas I thought it was comparing it with half the divisor. My guess is that the previous version was incorrect.
However I can also just revert to the previous version...
78b4bcc
to
7c2bc4c
Compare
Current coverage is 61.92% (diff: 60.86%)@@ master #7547 diff @@
==========================================
Files 173 173
Lines 56177 56125 -52
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
- Hits 34784 34757 -27
+ Misses 21393 21368 -25
Partials 0 0
|
@@ -2100,7 +2098,7 @@ def l2norm(a): | |||
|
|||
Implemented as a separate function (not a call to :func:`norm` for speed). | |||
""" | |||
return np.sqrt(np.sum(np.absolute(a)**2)) | |||
return np.sqrt(np.sum(np.abs(a) ** 2)) |
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.
np.abs(a) ** 2
== a ** 2
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 for complex values.
7c2bc4c
to
f1b25e6
Compare
(rebased following conflicts with sticky edges PR) |
AppVeyor failure looks transient. |
__cmp__
method.reshape
instead of assigning toshape
at least when it looks more legible.np.min
instead ofamin
,np.max
(amax
),np.abs
(fabs
,absolute
),np.conj
(conjugate
).