Thread overview
Pretty-printing D arrays with Mir
May 31
jmh530
May 31
mw
Jun 01
jmh530
May 31
I often print arrays to see how they look and their contents.
NumPy has a nice way of pretty-printing the arrays, and I was lacking this in D.
For the sake of practice, I wrote a small package. It uses mir.ndslice but works for both standard D arrays and Mir Slices.

import pretty_array;
import mir.ndslice;
import std.stdio;

void main() {
    auto b = [2, 2, 6].iota!int(1).fuse;
    b.prettyArr.writeln;
}
┌                   ┐
│┌                 ┐│
││ 1  2  3  4  5  6││
││ 7  8  9 10 11 12││
│└                 ┘│
│┌                 ┐│
││13 14 15 16 17 18││
││19 20 21 22 23 24││
│└                 ┘│
└                   ┘

https://github.com/tastyminerals/pretty_d_array

There are of course a couple of things to finish like floating precision and small number suppression.
Still, hope somebody will find it handy.



May 31
On Sunday, 31 May 2020 at 22:40:09 UTC, tastyminerals wrote:
> I often print arrays to see how they look and their contents.
> NumPy has a nice way of pretty-printing the arrays, and I was lacking this in D.
> For the sake of practice, I wrote a small package. It uses mir.ndslice but works for both standard D arrays and Mir Slices.
>
> import pretty_array;
> import mir.ndslice;
> import std.stdio;
>
> void main() {
>     auto b = [2, 2, 6].iota!int(1).fuse;
>     b.prettyArr.writeln;
> }
> ┌                   ┐
> │┌                 ┐│
> ││ 1  2  3  4  5  6││
> ││ 7  8  9 10 11 12││
> │└                 ┘│
> │┌                 ┐│
> ││13 14 15 16 17 18││
> ││19 20 21 22 23 24││
> │└                 ┘│
> └                   ┘
>
> https://github.com/tastyminerals/pretty_d_array
>
> There are of course a couple of things to finish like floating precision and small number suppression.
> Still, hope somebody will find it handy.

Interesting.

I had done some work in 2018 for numir format facilities. At the time mir didn’t have a way to do @nogc formatting, but it does now. It might be interesting to either revisit that or think about getting this into mir.
May 31
On Sunday, 31 May 2020 at 23:10:44 UTC, jmh530 wrote:
> On Sunday, 31 May 2020 at 22:40:09 UTC, tastyminerals wrote:
>> I often print arrays to see how they look and their contents.
>> NumPy has a nice way of pretty-printing the arrays, and I was lacking this in D.

>> ┌                   ┐
>> │┌                 ┐│
>> ││ 1  2  3  4  5  6││
>> ││ 7  8  9 10 11 12││
>> │└                 ┘│
>> │┌                 ┐│
>> ││13 14 15 16 17 18││
>> ││19 20 21 22 23 24││
>> │└                 ┘│
>> └                   ┘
>>
>> https://github.com/tastyminerals/pretty_d_array


> Interesting.
>
> I had done some work in 2018 for numir format facilities. At the time mir didn’t have a way to do @nogc formatting, but it does now. It might be interesting to either revisit that or think about getting this into mir.


Excellent!
👍 D is fun to work with everyday!

I literally, oh no, (obviously :-) visually envision Mir will beat numpy one day!


June 01
On Sunday, 31 May 2020 at 23:10:44 UTC, jmh530 wrote:
> On Sunday, 31 May 2020 at 22:40:09 UTC, tastyminerals wrote:
>> I often print arrays to see how they look and their contents.
>> NumPy has a nice way of pretty-printing the arrays, and I was lacking this in D.
>> For the sake of practice, I wrote a small package. It uses mir.ndslice but works for both standard D arrays and Mir Slices.
>>
>> import pretty_array;
>> import mir.ndslice;
>> import std.stdio;
>>
>> void main() {
>>     auto b = [2, 2, 6].iota!int(1).fuse;
>>     b.prettyArr.writeln;
>> }
>> ┌                   ┐
>> │┌                 ┐│
>> ││ 1  2  3  4  5  6││
>> ││ 7  8  9 10 11 12││
>> │└                 ┘│
>> │┌                 ┐│
>> ││13 14 15 16 17 18││
>> ││19 20 21 22 23 24││
>> │└                 ┘│
>> └                   ┘
>>
>> https://github.com/tastyminerals/pretty_d_array
>>
>> There are of course a couple of things to finish like floating precision and small number suppression.
>> Still, hope somebody will find it handy.
>
> Interesting.
>
> I had done some work in 2018 for numir format facilities. At the time mir didn’t have a way to do @nogc formatting, but it does now. It might be interesting to either revisit that or think about getting this into mir.

I see. It depends on how much work is needed for any of the options, right?

For now, I think having a function that does the job suffices for me at least. Since I always printed tensors in Python to see what's going on, I was lacking the same functionality in Mir.
I don't code in D on a daily basis but still try to learn by doing small stuff. I think the current implementation is far from being included into anything without rigorous code review but would be glad to see better slice formatting in Mir.

June 01
On Monday, 1 June 2020 at 19:51:34 UTC, tastyminerals wrote:
> [snip]
>
> I see. It depends on how much work is needed for any of the options, right?
>
> For now, I think having a function that does the job suffices for me at least. Since I always printed tensors in Python to see what's going on, I was lacking the same functionality in Mir.
> I don't code in D on a daily basis but still try to learn by doing small stuff. I think the current implementation is far from being included into anything without rigorous code review but would be glad to see better slice formatting in Mir.

Yeah, mir is kind of bare bones for some stuff.

I had meant to include the link before
https://github.com/libmir/numir/pull/10
If you look at some of the unittests you can see how it's different. I separated out the different tensors with extra lines between them, which isn't as easy to read. However, I think bigger than a 3-d tensor is relatively uncommon. It might be some work to get it to work since all the functions would need to be re-written to take into account the mir's new format facilities, but I probably wouldn't have to change the unittests too much.

I just tried to use the mir.format for the first time (code below). The documentation is a bit lacking at this point. The `print` function is like `put` function for an output range but with some additional functionality I don't understand yet. I think for it to be used completely in a @nogc manner, you would need a @nogc `writeln` function as well.

/+dub.sdl:
dependency "mir-algorithm" version="*"
+/
import mir.format;
import mir.appender: ScopedBuffer;
import std.stdio: writeln;

void main() {
    FormatSpec formatSpec;
    formatSpec.format = 'f';
    formatSpec.precision = 3;

    auto x = withFormat(1.5f, formatSpec);
    ScopedBuffer!char w;
    w.print(x);
    w.data.writeln;
}
June 02
On Sunday, 31 May 2020 at 22:40:09 UTC, tastyminerals wrote:
> I often print arrays to see how they look and their contents.
> NumPy has a nice way of pretty-printing the arrays, and I was lacking this in D.
> For the sake of practice, I wrote a small package. It uses mir.ndslice but works for both standard D arrays and Mir Slices.
>
> [...]

I added the package to code.dlang.org (thanks to John-Colvin reminding me).
The repo url is updated: https://github.com/tastyminerals/pretty-d-array
June 11
On Monday, 1 June 2020 at 21:25:02 UTC, jmh530 wrote:
> On Monday, 1 June 2020 at 19:51:34 UTC, tastyminerals wrote:
>> [...]
>
> Yeah, mir is kind of bare bones for some stuff.
>
> I had meant to include the link before
> https://github.com/libmir/numir/pull/10
> If you look at some of the unittests you can see how it's different. I separated out the different tensors with extra lines between them, which isn't as easy to read. However, I think bigger than a 3-d tensor is relatively uncommon. It might be some work to get it to work since all the functions would need to be re-written to take into account the mir's new format facilities, but I probably wouldn't have to change the unittests too much.
>
> [...]

I see. I shall take a look, thanks.
June 11
On Sunday, 31 May 2020 at 22:40:09 UTC, tastyminerals wrote:
> I often print arrays to see how they look and their contents.
> NumPy has a nice way of pretty-printing the arrays, and I was lacking this in D.
> For the sake of practice, I wrote a small package. It uses mir.ndslice but works for both standard D arrays and Mir Slices.
>
> [...]

Updated the package and README. Now, you can control the floating precision, scientific notation and other formatting params.