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

Plot methods that don't work with units #3947

Open
eliascarv opened this issue Jun 7, 2024 · 3 comments
Open

Plot methods that don't work with units #3947

eliascarv opened this issue Jun 7, 2024 · 3 comments
Labels
bug Collection contains multiple issues Makie Backend independent issues (Makie core) plot Related to plot object units aka dim converts

Comments

@eliascarv
Copy link

eliascarv commented Jun 7, 2024

I made a list of some plot methods that I use that don't work with units.
MWE:

using GLMakie
using Unitful
using StaticArrays
using GLMakie.GeometryBasics

#---------
# SCATTER
#---------

# 2D
x = rand(10) * u"m"
y = rand(10) * u"m"
# positions
tuples = collect(zip(x, y))
svectors = SVector.(x, y)
points = Point.(x, y)

scatter(x, y) # works
scatter(tuples) # doesn't work
scatter(svectors) # doesn't work
scatter(points) # doesn't work

# 3D
x = rand(10) * u"m"
y = rand(10) * u"m"
z = rand(10) * u"m"
# positions
tuples = collect(zip(x, y, z))
svectors = SVector.(x, y, z)
points = Point.(x, y, z)

scatter(x, y, z) # works, but doesn't show units on the axis
scatter(tuples) # doesn't work
scatter(svectors) # doesn't work
scatter(points) # doesn't work

#--------
# ARROWS
#--------

# 2D
x = rand(10) * u"m"
y = rand(10) * u"m"
u = rand(10) * u"m"
v = rand(10) * u"m"

points = Point.(x, y)
directions = Vec.(u, v)

arrows(x, y, u, v) # doesn't work
arrows(points, directions) # doesn't work

# 3D
x = rand(10) * u"m"
y = rand(10) * u"m"
z = rand(10) * u"m"
u = rand(10) * u"m"
v = rand(10) * u"m"
w = rand(10) * u"m"

points = Point.(x, y, z)
directions = Vec.(u, v, w)

arrows(x, y, z, u, v, w) # doesn't work
arrows(points, directions) # doesn't work

#------
# POLY
#------

x = [0.0, 2.0, 3.0, 1.0] * u"m"
y = [0.0, 0.0, 1.0, 1.0] * u"m"
points = Point.(x, y)
polygon = Polygon(points) # doesn't work

poly(points) # doesn't work

#-------
# LINES
#-------

# 2D
x = (0:0.01:10) * u"m"
y = 5u"m" .* sin.(0:0.01:10)
# positions
tuples = collect(zip(x, y))
svectors = SVector.(x, y)
points = Point.(x, y)

lines(x, y) # works
lines(tuples) # doesn't work
lines(svectors) # doesn't work
lines(points) # doesn't work

# 3D
x = (0:0.01:10) * u"m"
y = 5u"m" .* sin.(0:0.01:10)
z = 5u"m" .* cos.(0:0.01:10)
# positions
tuples = collect(zip(x, y, z))
svectors = SVector.(x, y, z)
points = Point.(x, y, z)

lines(x, y, z) # incorrect plot
lines(tuples) # doesn't work
lines(svectors) # doesn't work
lines(points) # doesn't work

#------
# MESH
#------

x = [0.0, 1.0, 0.0, 0.0] * u"m"
y = [0.0, 0.0, 1.0, 0.0] * u"m"
z = [0.0, 0.0, 0.0, 1.0] * u"m"
tuples = collect(zip(x, y, z))
svectors = SVector.(x, y, z)
points = Point.(x, y, z)

faces = [
  1 2 3
  1 2 4
  2 3 4
  1 3 4
]

mesh(tuples, faces) # doesn't work
mesh(svectors, faces) # doesn't work
mesh(points, faces) # doesn't work

#-------------
# MESHSCATTER
#-------------

x = cos.(1:0.5:20) * u"m"
y = sin.(1:0.5:20) * u"m"
z = LinRange(0, 3, length(x)) * u"m"

meshscatter(x, y, z) # incorrect plot

marker = Rect3((-1.0u"m", -1.0u"m", -1.0u"m"), (1.0u"m", 1.0u"m", 1.0u"m"))
meshscatter(x, y, z, marker=marker) # doesn't work

#-------
# IMAGE
#-------

x = (0u"m", 10u"m")
y = (0u"m", 10u"m")
img = rand(10, 10)
image(x, y, img) # doesn't work
@eliascarv eliascarv added the bug label Jun 7, 2024
@juliohm
Copy link
Member

juliohm commented Jun 21, 2024

We are trying to make use of the new unit support in Makie, but can't do it currently because of the issues listed above. Is there a specific place in the source code that we can look into to help fix all of them?

@ffreyer
Copy link
Collaborator

ffreyer commented Jun 22, 2024

Most of the plot object setup starts in interfaces.jl. For units it should be this:

dim_converted = try_dim_convert(P, PTrait, user_attributes, args_obs, deregister)

Which continues in
function try_dim_convert(P::Type{<:Plot}, PTrait::ConversionTrait, user_attributes, args_obs::Tuple, deregister)
# Only 2 and 3d conversions are supported, and only
if !(length(args_obs) in (2, 3))
return args_obs
end
converts = to_value(get!(() -> DimConversions(), user_attributes, :dim_conversions))
return ntuple(length(args_obs)) do i
arg = args_obs[i]
argval = to_value(arg)
# We only convert if we have a conversion struct (which isn't NoDimConversion),
# or if we we should dim_convert
if !isnothing(converts[i]) || should_dim_convert(P, argval) || should_dim_convert(PTrait, argval)
return convert_dim_observable(converts, i, arg, deregister)
end
return arg
end
end

and the other files in that folder. I'd guess you'll need to add a bunch of should_dim_convert() and convert_dim_value() methods.

@asinghvi17 asinghvi17 added the units aka dim converts label Jun 23, 2024
@juliohm
Copy link
Member

juliohm commented Aug 26, 2024

Thank you @ffreyer for sharing directions. We didn't have time to look into it. Any help is appreciated.

@ffreyer ffreyer added Makie Backend independent issues (Makie core) plot Related to plot object Collection contains multiple issues labels Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Collection contains multiple issues Makie Backend independent issues (Makie core) plot Related to plot object units aka dim converts
Projects
None yet
Development

No branches or pull requests

4 participants