Skip to content

BUG: Make np.array_repr work on matrix objects - #10123

Closed
eric-wieser wants to merge 3 commits into
numpy:masterfrom
eric-wieser:fix-matrix-repr
Closed

eric-wieser wants to merge 3 commits into
numpy:masterfrom
eric-wieser:fix-matrix-repr

Conversation

@eric-wieser

@eric-wieser eric-wieser commented Nov 29, 2017 •

Copy link
Copy Markdown
Member

Use indices not array in the recursion, so that subclasses cannot break it.

Rework of #9816, so fixes gh-8442, gh-5875

@eric-wieser

eric-wieser commented Nov 29, 2017 •

Copy link
Copy Markdown
Member Author

Adding to 1.14 due to the second commit being another repr change. If the rest of this PR holds that up, it can be made stanadalone

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.

Arguably as a result of this change, _leading_trailing didn't need fixing - but the cleanup above seems better anyway.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm actually surprised this wasn't up here before. I would have thought _get_format_function would complain about getting subclasses, and only when the array size was large.

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.

In order for this to matter, you had to pass an array where size > threshold, but no single axis is larger than 2 * edgeitems. I doubt that was tested.

@eric-wieser eric-wieser changed the title BUG: Make _leading_trailing work on matrix objects BUG: Make np.array_repr work on matrix objects Nov 29, 2017
@ahaldane

Copy link
Copy Markdown
Member

OK I get it.

In both the first and third commit you replace code that worked by recurively indexing the array at each dimension, by code which recursively generates an nd-index and then at the end indexes the array with it. This solves the problem that trying to recursively index a matrix dimension-by-dimension doesn't work since it regenerates new dimensions.

Comment thread numpy/core/arrayprint.py Outdated

@ahaldane ahaldane Nov 30, 2017 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would things break if you called this on data instead of on a? If not, then the changes below to _formatArray are also unnecessary. Though, like for _leading_trailing, the'yre prettier or more performant.

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.

It depends what you mean by break. Calling this on MaskedArray currently warns and converts masked to nan / errors on int. With your proposed change, it would silently discard the mask.

Ideally, we'd allow the formatters to see the subclasses if they want to, but that's out of scope for 1.14

@eric-wieser

Copy link
Copy Markdown
Member Author

Yep, your summary looks correct.

In both the first and third commit

I'll extract the second commit, since it probably needs a test

@eric-wieser

Copy link
Copy Markdown
Member Author

Actually this is a pile of tiny and mostly independent things, which I've spun off three new PRs for (#10129, #10130, #10131).

I'll revisit the rest of this once those are merged. I think you're right, the changes to _formatArray are not the easiest fix, and I'm not sure they're an improvement for readability either.

@ahaldane

ahaldane commented Dec 1, 2017

Copy link
Copy Markdown
Member

array_repr of matrices is not yet fixed after those PRs were merged.

I think we still need to pass a plain ndarray to _formatArray, eg by making the argument be asarray(a) instead of a. Or, we need to include the _formatArray changes in this PR.

@eric-wieser

eric-wieser commented Dec 1, 2017 •

Copy link
Copy Markdown
Member Author

I'll rebase this once #10143 is merged, at which point it will just be the recursion change in _formatArray. done

You're right though, passing the base class array would work too. I can't immediately think of why that wouldn't be a good idea - and it is currently a little weird that Formatter.__init__ gets the subclass, but Formatter.__call__ does not.

The actual motivation for these PRs was to try and fix the column alignment of MaskedArrays by letting them register their own custom formatter - in which case, one step would be to actually remove the asarray, not add another one. But maybe we should get there via adding one first, and then push all the asarray calls down into the Formatters later.

@eric-wieser

eric-wieser commented Dec 6, 2017 •

Copy link
Copy Markdown
Member Author

@ahaldane: does this cleanup look like an improvement?

Arguments for this approach:

  • Fewer ndarray wrappers constructed
  • Works on subclasses even with weird indexing behaviour

Arguments against:

  • (subjectively) less readable

What I don't care about is whether this is actually the best fix to #8442 - just whether this is a desirable refactor. As you point out above, that issue might be best addressed by just passing in the base class.

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.

Now only one base case!

@eric-wieser

eric-wieser commented Dec 6, 2017 •

Copy link
Copy Markdown
Member Author

Actually, let's split this in two one more time... - Nope, actually doing this within the index=... change is harder, because of decay from 0d to scalars

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.

The rank variable was a workaround for dealing with dtype=object arrays, where arr[i].ndim > arr.ndim is possible, and an external counter is needed. By indexing the array only once, we avoid that confusion.

…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.
@eric-wieser

Copy link
Copy Markdown
Member Author

Closing in favor of a more descriptive PR title with a clean slate of comments, and moving my inline comments into the commit message. Sorry for the noise!

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

2 participants