ggebbie / OceanGreensFunctionMethods.jl

Green's Function Methods for Tracer Timescales and Pathways in Ocean Models
MIT License
5 stars 2 forks source link

wasted screen real estate with DimArray with units #22

Closed ggebbie closed 2 months ago

ggebbie commented 2 months ago

A 3x3 DimArray with units on the parent matrix cannot be fully displayed to screen in a Pluto notebook.

rafaqz commented 2 months ago

Do you have a screen shot? I don't really use Pluto so I don't run into this

ggebbie commented 2 months ago

A 3x3 DimArray has a large offset between the Dimension label and numerical values such that the output needs to hide a column to fit on the screen.

screenshot-2024-09-07-14:11:18

edit: looks like I can simplify my statement with Vol = fill(Vol0, model_dims)

edit2: the units are uniform and could be written "outside" of the matrix instead of applied to each element. This would save space.

ggebbie commented 2 months ago

For a variable without units, all entries of the 3x3 matrix can be viewed on screen. screenshot-2024-09-07-14:11:45

Changing the printing style via ENV["UNITFUL_FANCY_EXPONENTS"] = true
or false makes no difference.

rafaqz commented 2 months ago

Interesting, it could be DD, but I would guess that Unitful.jl is not telling Julia how to print unitful quantities properly in a mixed array. Notice that the string is offset to the end of the whole number rather than the decimal point like it is without units. That's just the Base.print_matrix behaviour of units, not something I specifically wrote for DD.

Probably DD should stop using these base matrix display methods, as they're buggy like this, but it's a bit of work to write the layout logic from scratch.

rafaqz commented 2 months ago

Yes, looks like this is unitful. To demonstrate with a mixed unit matrix: the decimal points should line up, but we have the same horrible effect as in DD printing.

julia> using Unitful

julia> [1.99809802u"s" 2.339098; 1.0 2.0u"km/hr"]
2×2 Matrix{Quantity{Float64}}:
 1.9981 s        2.3391
      1.0  2.0 km hr^-1

And thats because they don't define a Base.alignment method so it hits the fallback for Number, which puts everything to the left of the decimal point. This fixes the problem for me:

function Base.alignment(io::IO, x::Unitful.Quantity)
    total = Base.alignment_from_show(io, x)[1]
    parent = Base.alignment(io, ustrip(x))
    return parent[1], parent[2] + total - sum(parent)
end

So:

julia> [1.99809802u"s" 2.339098; 1.0 2.0u"km/hr"]
2×2 Matrix{Quantity{Float64}}:
 1.9981 s  2.3391
 1.0       2.0 km hr^-1

And DD printing looks fine to me too.

ggebbie commented 2 months ago

Looks really nice now. I think this will make a big impact for those who are trying to follow my Pluto notebook, plus it will help emphasize the usefulness of DimensionalData.jl. Much appreciated!

Updated printing: screenshot-2024-09-07-22:07:37