Skip to content

MAINT/BUG: Simplify _formatArray, fixing array_repr(matrix) in the process - #10164

Merged
ahaldane merged 3 commits into
numpy:masterfrom
eric-wieser:formatArray-recurse
Dec 7, 2017
Merged

ahaldane merged 3 commits into
numpy:masterfrom
eric-wieser:formatArray-recurse

Conversation

@eric-wieser

@eric-wieser eric-wieser commented Dec 6, 2017 •

Copy link
Copy Markdown
Member

Final rework of #10123, which in turn reworked #9816.

Now:

  • Has one recursive base case
  • Is fewer lines of code
  • Does not rstrip the result of format_function (none of the builtin ones have trailing spaces anyway)
  • Recurses in the index, instead of the value, which handles dtype=object arrays without the need for the rank argument. This fixes np.array_repr fails on np.matrix objects #8442 at the same time.

Comment thread numpy/core/arrayprint.py Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Something for another PR - also pass next_line_len = line_len - 2, which solves the problem of np.zeros((1,)*30 + (100,)) being far wider than max_line_len (guarded by legacy)

Comment thread numpy/core/arrayprint.py Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

All of these cases were simply trying to encode "add a hanging indent, but not on the first line". For 1d cases, we already do that by just adding it, and removing it at the end. Doing the same thing here makes things a lot clearer.

…ocess

Now:
 * Has one recursive base case
 * Is fewer lines of code
 * Does not rstrip the result of format_function (none of the builtin ones have trailing spaces anyway)
 * Recurses in the index, instead of the value, which handles `dtype=object` arrays without the need for the rank argument. This fixes numpy#8442.
Comment thread numpy/core/arrayprint.py

def _extendLine(s, line, word, max_line_len, next_line_prefix):
if len(line.rstrip()) + len(word.rstrip()) >= max_line_len:
if len((line + word).rstrip()) > max_line_len:

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Stripping line made no sense here. We remain compatible by also fixing the comparison, since in practice line had exactly one trailing space (<snip>, ' or <snip> '), except when a custom separator was passed.

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks very nice! Trivial nitpick can be ignored if nobody else has anything

Comment thread numpy/core/arrayprint.py
next_hanging_indent = hanging_indent + ' '

a_len = a.shape[axis]
show_summary = summary_insert and 2*edge_items < a_len

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Trivial, but while you are at it, could you flip: a_len > 2*edge_items (unless for you that gives cognitive dissonance; for me, one just calculated a_len, so it is obvious it should be compared to something)

def __rpow__(self, other):
return NotImplemented

def __repr__(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yay! Lovely to see code disappear.

@charris

charris commented Dec 7, 2017

Copy link
Copy Markdown
Member

Is this something that should be included in 1.14?

@eric-wieser

Copy link
Copy Markdown
Member Author

Doesn't really matter of this makes it to 1.14: matrices being broken is not new.

Having said that, there are some tweaks to line-wrapping I'd like to make on top of this - namely, actually respecting the max line width. That would belong in 1.14, but probably isn't worth holding up the release for

@ahaldane

ahaldane commented Dec 7, 2017

Copy link
Copy Markdown
Member

LGTM.

This is the kind of code where tiny details about how exactly a particular space gets added can subtly change the repr in corner cases, making it hard to review thoroughly. But I read the code trying to catch these and it seemed fine, and in my experience the unit tests have been good at catching changes in printing.

@eric-wieser

Copy link
Copy Markdown
Member Author

I'll try to add some tests

@ahaldane

ahaldane commented Dec 7, 2017

Copy link
Copy Markdown
Member

I just meant that the already-existing tests made me more confident, but more tests are always welcome.

@mhvk

mhvk commented Dec 7, 2017

Copy link
Copy Markdown
Contributor

My sense would to please try to get all formatting changes in by 1.14 - I really would prefer not to edit doctests for a while!!

@eric-wieser - if it helps get the final things in, please just go ahead and merge.

@ahaldane

ahaldane commented Dec 7, 2017

Copy link
Copy Markdown
Member

I think Eric isn't working on new tests right now, and I'm not sure we need more.

How about we merge in 15 minutes?

@mhvk

mhvk commented Dec 7, 2017

Copy link
Copy Markdown
Contributor

That sounds good. Now in 8 minutes I guess.

@mhvk mhvk added this to the 1.14.0 release milestone Dec 7, 2017
@eric-wieser

eric-wieser commented Dec 7, 2017 •

Copy link
Copy Markdown
Member Author

We'll need the tests for the changes I'm proposing come next anyway (arrays that are one character away from line-wrapping in either direction. But you're correct, I will not be working on this for another 4 hours.

@ahaldane

ahaldane commented Dec 7, 2017

Copy link
Copy Markdown
Member

OK, let's merge then. Thanks Eric!

@ahaldane
ahaldane merged commit 9a4b692 into numpy:master Dec 7, 2017
@eric-wieser

Copy link
Copy Markdown
Member Author

Remaining work is at #10170

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

np.array_repr fails on np.matrix objects

4 participants