Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 6 commits into from
Dec 5, 2016
Merged

Additional cleanups #7547

merged 6 commits into from
Dec 5, 2016

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Dec 1, 2016

  • Call min/max directly on a generator or on two arguments rather than via a list.
  • Remove old __cmp__ method.
  • Use reshape instead of assigning to shape at least when it looks more legible.
  • Use np.min instead of amin, np.max (amax), np.abs (fabs, absolute), np.conj (conjugate).
  • dict/set comprehensions and set literals where appropriate.

Copy link
Member

@QuLogic QuLogic left a 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))
Copy link
Member

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?

Copy link
Contributor Author

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing parentheses in shape.

Copy link
Contributor Author

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
Copy link
Member

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)?

Copy link
Contributor Author

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))
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed below too.

Copy link
Contributor Author

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().

Copy link
Member

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.

Copy link
Member

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))
Copy link
Member

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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more dict comprehension?

Copy link
Contributor Author

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):
Copy link
Member

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...

Copy link
Contributor Author

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.

Copy link
Member

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)
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Contributor Author

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
Copy link
Member

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
>>> 

Copy link
Contributor Author

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...

@anntzer anntzer force-pushed the additional-cleanups branch 4 times, most recently from 78b4bcc to 7c2bc4c Compare December 3, 2016 22:14
@codecov-io
Copy link

Current coverage is 61.92% (diff: 60.86%)

Merging #7547 into master will increase coverage by <.01%

@@             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          

Powered by Codecov. Last update e66d2f1...7c2bc4c

@@ -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))
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for complex values.

@anntzer anntzer force-pushed the additional-cleanups branch from 7c2bc4c to f1b25e6 Compare December 4, 2016 22:25
@anntzer
Copy link
Contributor Author

anntzer commented Dec 4, 2016

(rebased following conflicts with sticky edges PR)

@QuLogic
Copy link
Member

QuLogic commented Dec 5, 2016

AppVeyor failure looks transient.

@QuLogic QuLogic merged commit 409d8b7 into matplotlib:master Dec 5, 2016
@QuLogic QuLogic added this to the 2.1 (next point release) milestone Dec 5, 2016
@anntzer anntzer deleted the additional-cleanups branch December 5, 2016 03:19
@jklymak jklymak mentioned this pull request Dec 14, 2017
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants