Skip to content

BUG: The last line of formatArray is not always wrapped correctly - #10172

Merged
ahaldane merged 1 commit into
numpy:masterfrom
eric-wieser:format-array-linewrap
Dec 8, 2017
Merged

ahaldane merged 1 commit into
numpy:masterfrom
eric-wieser:format-array-linewrap

Conversation

@eric-wieser

Copy link
Copy Markdown
Member

The old behaviour is kept under the legacy='1.13' flag, as usual

@eric-wieser eric-wieser added this to the 1.14.0 release milestone Dec 8, 2017
@eric-wieser
eric-wieser requested a review from ahaldane December 8, 2017 03:42
@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

Some related things I still don't like:

>>> np.set_printoptions(linewidth=18)
>>> np.full(8, fill_value=2)  # last ) is past linewidth
array([2, 2, 2, 2,
       2, 2, 2, 2])
>>> print str(np.full(18, fill_value=2))  # 2nd to last row has fewer elements
[2 2 2 2 2 2 2 2 2
 2 2 2 2 2 2 2 2
 2]

Do you think this can be fixed by subtracting one from the requested linewidth everywhere but the last line, to account for the final ]? And maybe array_repr should additionally subtract 1 from the linewidth it passes to array2string, to account for the final ).

Comment thread numpy/core/arrayprint.py Outdated

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.

This line and a few others from the last PR are longer than 80 chars. Rename max_line_lento something shorter?

@eric-wieser

eric-wieser commented Dec 8, 2017 •

Copy link
Copy Markdown
Member Author

And maybe array_repr should additionally subtract 1 from the linewidth it passes to array2string, to account for the final ).

This is in my pipeline

2nd to last row has fewer elements

Ugh I was just thinking about that when separator="", and had convinced myself we didn't care. But it clearly happens for " " too.

The rules need to be a bit more complex, because presumably we want

array([1, 1,  #this comma aligns with the ]
       1, 1])
[1 1
 1 1
 1 1]  # here the 1 must be forced onto a new line, even if it fits on the old one (else the last `]` will not fit)

As expected, this patch is not correct.

@ahaldane

ahaldane commented Dec 8, 2017 •

Copy link
Copy Markdown
Member

Yeah locally I just tried this, and it fixes by peeves.

Rename max_line_width to width to fix 80 char lines. Then in _formatArray at the start I define nwidth = max(width - len(separator), width - len(']')), and replace all uses of max_line_width by nwidth, except for the trailing line in the rank -1 case where it just width.

Then I also subtract 1 in the call to array2string in array_repr`.

(feel free to choose better variable names! Also I think my computation of nwidth is not right..)

@ahaldane

ahaldane commented Dec 8, 2017 •

Copy link
Copy Markdown
Member

Hmm for the array_repr trailing ) I need to be more careful since that's only a problem if skipdtype is true. But you said its in your pipeline so I'll leave that stuff to you.

Edit: Never mind, I think subtracting 1 is sufficient since in the skipdtype=True case we add a trailing ), and in the skipdtype=False case we add a trailing ,, so the trailing part is 1 char in both cases.

@eric-wieser

Copy link
Copy Markdown
Member Author

That's pretty similar to what I have locally, but keeping the 1.13 compatibility is hard.

Perhaps for ease of correctness, I should just lift formatArray from before these changes, and have two copies?

@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

Yeah if you are being forced to add little legacy branches everywhere, at some point it is easier to just make a separate function for the legacy case, like you did for maskedarrays.

@eric-wieser
eric-wieser force-pushed the format-array-linewrap branch from 35a17d6 to 90921ce Compare December 8, 2017 04:55
The old behaviour is kept under the legacy='1.13' flag, as usual

See tests for the changes.
@eric-wieser

Copy link
Copy Markdown
Member Author

Actually wasn't too bad, I was misreading the failure - it was actually the non-legacy testcase, which did need an update

@eric-wieser
eric-wieser force-pushed the format-array-linewrap branch from 90921ce to e7f84cd Compare December 8, 2017 04:58
Comment thread numpy/core/arrayprint.py
def _extendLine(s, line, word, max_line_len, next_line_prefix):
if len((line + word).rstrip()) > max_line_len:
def _extendLine(s, line, word, line_width, next_line_prefix):
if len(line) + len(word) > line_width:

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.

We're not passing the separator in any more, so trailing spaces are "real"

[('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',)
('1970-01-01T00:00:00',) ('1970-01-01T00:00:00',) ( 'NaT',)
( 'NaT',) ( 'NaT',) ( 'NaT',)
( 'NaT',)]""")

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.

This is only 74 characters, but if the last line had 2 more items, it would be 76, which would be too long. It's easier to play it safe, rather than being greedy and having to backtrack.

@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

Looks good, I'm ready to merge if tests pass.

We're saving the array repr changes for another PR, right? Once all these changes are done we should add an entry in the release note list of repr changes.

@eric-wieser

eric-wieser commented Dec 8, 2017 •

Copy link
Copy Markdown
Member Author

I've got two more changes I'd like to make:

  1. A suffix argument to array2string, used to reserve space on the right
  2. Reducing the line width by 1 for each level of recursion, to fix BUG: Linewidth is not respected #10170

Do you think we can work in parallel without these conflicting? Or shall I just shove them both into the next PR?

@eric-wieser

Copy link
Copy Markdown
Member Author

Yeah if you are being forced to add little legacy branches everywhere, at some point it is easier to just make a separate function for the legacy case, like you did for maskedarrays.

If after 1.14.0 is released, it turns out we missed something, then I think that is what we should do for 1.14.1.

@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

Do you think we can work in parallel without these conflicting? Or shall I just shove them both into the next PR?

I'm not sure what you mean by parallel, but I'm happy to do whatever is easiest for you. Add them as commits here, make them separate PRs, etc.

@eric-wieser

Copy link
Copy Markdown
Member Author

Parallel as in you taking one of them.

@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

All right I'll try the suffix arg. I'll base on this PR.

@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

Wait.. what's the difference between suffix space, and simply reducing max_line_width?

@eric-wieser

eric-wieser commented Dec 8, 2017 •

Copy link
Copy Markdown
Member Author

It means that array2string handles it, rather than array_repr. As an end user, you can't handle it without explicitly asking for the line width, otherwise

@eric-wieser

Copy link
Copy Markdown
Member Author

(tests pass)

@ahaldane

ahaldane commented Dec 8, 2017

Copy link
Copy Markdown
Member

All right, merging.

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.

2 participants