Skip to content
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

Smarter Debug formatting for arrays #398

Closed
bluss opened this issue Dec 29, 2017 · 7 comments
Closed

Smarter Debug formatting for arrays #398

bluss opened this issue Dec 29, 2017 · 7 comments
Labels
enhancement good first issue A good issue to start contributing to ndarray! help wanted

Comments

@bluss
Copy link
Member

bluss commented Dec 29, 2017

  • Print only part of the array if it has too many elements
  • Select width and precision better, for a nice and readable display

inspiration:

array([[ 0.    ,  0.0001,  0.0002, ...,  0.0002,  0.0003,  0.0004],
       [ 0.1   ,  0.09  ,  0.1   , ...,  0.1   ,  0.1   ,  0.1   ],
       [ 0.0024,  0.0025,  0.0026, ...,  0.0026,  0.0027,  0.0028],
       ..., 
       [ 0.0024,  0.0025,  0.0026, ...,  0.0026,  0.0027,  0.0028],
       [ 0.0036,  0.0037,  0.0038, ...,  0.0038,  0.0039,  0.004 ],
       [ 0.0048,  0.0049,  0.005 , ...,  0.005 ,  0.0051,  0.0052]])
@LukeMathWalker LukeMathWalker added the good first issue A good issue to start contributing to ndarray! label Mar 18, 2019
@andrei-papou
Copy link
Contributor

andrei-papou commented Mar 21, 2019

Hi @bluss , @LukeMathWalker . I've never contributed to ndarray before and since it is a good first issue now I would like to pick this up. Could you please provide any guidance? Thanks.

@LukeMathWalker
Copy link
Member

LukeMathWalker commented Mar 21, 2019

Welcome @andrei-papou!

The Debug trait defines how a Rust variable is printed when using the {:?} format placeholder (see here for a more complete overview).

You can find ArrayBase's implementation of the Debug trait here - the bulk of the job takes place in the format_array function.
As you can see, it prints all the array elements to screen, which turn out to be quite noisy if you are working with big arrays or you are printing arrays from more than one spot in your program.

You can get a taste of what this looks like using 1-dimensional arrays:

extern crate ndarray;

use ndarray::Array;

fn main() {
    let v = Array::linspace(0., 1., 128);
    println!("{:?}", v);
}

This produces:

[0.0, 0.0078125, 0.015625, 0.0234375, 0.03125, 0.0390625, 0.046875, 0.0546875, 0.0625, 0.0703125, 0.078125, 0.0859375, 0.09375, 0.1015625, 0.109375, 0.1171875, 0.125, 0.1328125, 0.140625, 0.1484375, 0.15625, 0.1640625, 0.171875, 0.1796875, 0.1875, 0.1953125, 0.203125, 0.2109375, 0.21875, 0.2265625, 0.234375, 0.2421875, 0.25, 0.2578125, 0.265625, 0.2734375, 0.28125, 0.2890625, 0.296875, 0.3046875, 0.3125, 0.3203125, 0.328125, 0.3359375, 0.34375, 0.3515625, 0.359375, 0.3671875, 0.375, 0.3828125, 0.390625, 0.3984375, 0.40625, 0.4140625, 0.421875, 0.4296875, 0.4375, 0.4453125, 0.453125, 0.4609375, 0.46875, 0.4765625, 0.484375, 0.4921875, 0.5, 0.5078125, 0.515625, 0.5234375, 0.53125, 0.5390625, 0.546875, 0.5546875, 0.5625, 0.5703125, 0.578125, 0.5859375, 0.59375, 0.6015625, 0.609375, 0.6171875, 0.625, 0.6328125, 0.640625, 0.6484375, 0.65625, 0.6640625, 0.671875, 0.6796875, 0.6875, 0.6953125, 0.703125, 0.7109375, 0.71875, 0.7265625, 0.734375, 0.7421875, 0.75, 0.7578125, 0.765625, 0.7734375, 0.78125, 0.7890625, 0.796875, 0.8046875, 0.8125, 0.8203125, 0.828125, 0.8359375, 0.84375, 0.8515625, 0.859375, 0.8671875, 0.875, 0.8828125, 0.890625, 0.8984375, 0.90625, 0.9140625, 0.921875, 0.9296875, 0.9375, 0.9453125, 0.953125, 0.9609375, 0.96875, 0.9765625, 0.984375, 0.9921875, 1.0] shape=[129], strides=[1], layout=C | F (0x3), const ndim=1

It would look much better if it were to be something like:

[0.0, 0.0078125, 0.015625, ..., 0.984375, 0.9921875, 1.0] 
shape=[129], strides=[1], layout=C | F (0x3), const ndim=1

When you move to multi-dimensional arrays additional issues arise (e.g. alignment of array elements), but I'd say that a good starting point could be to reduce the number of printed elements adding ellipsis (...) to signal that something has been omitted to reduce visual noise.

I am happy to provide you with additional info if you need!

@andrei-papou
Copy link
Contributor

@LukeMathWalker Thanks a lot, everything is pretty clear! I'll submit a PR soon.

@dineshadepu
Copy link

Hi @LukeMathWalker. I am trying to contribute to ndarray. To get started I had a look at arrayformat file, which implements the printing. In that file I don't see where the format function is implemented. Is this in std lib? https://github.com/rust-ndarray/ndarray/blob/master/src/arrayformat.rs#L74.

@LukeMathWalker
Copy link
Member

Yes @dineshadepu, format is a macro in Rust's standard library - you can find the relevant documentation here.
Given that @andrei-papou is already working on this issue, may I suggest you to have a look at #597 for other needed contributions to the library?

@andrei-papou
Copy link
Contributor

@LukeMathWalker I've submitted a PR #606
Could you please take a look or assign someone as a reviewer? Thanks a lot.

@jturner314
Copy link
Member

Fixed by #606.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good first issue A good issue to start contributing to ndarray! help wanted
Projects
None yet
Development

No branches or pull requests

5 participants