JetBrains / lets-plot

Multiplatform plotting library based on the Grammar of Graphics
https://lets-plot.org
MIT License
1.54k stars 49 forks source link

Improve how Jax array is handled #1136

Closed sunnyayyl closed 1 month ago

sunnyayyl commented 1 month ago
import jax.numpy as jnp
import lets_plot as lp
import numpy

(
    lp.ggplot(
        {"x": list(jnp.arange(0, 10)), "y": numpy.arange(0, 10)},
        lp.aes(x="x", y="y"),
    )

    + lp.geom_line()
).show()

test y axis is the expected behavior with numpy x axis is the behavior when used with jax (jax: https://jax.readthedocs.io/en/latest/index.html)

alshan commented 1 month ago

Hi, in the example above, you are passing a list of arrays. When the elements in the list are anything but numeric values, Lets-Plot treats them as discrete (a.k.a. categorical) values and creates a discrete axis where the labels are simple string representations of the elements in the list.

A bare jax array, however, will most likely not be recognized as a collection of numerics either. Did you try this?

sunnyayyl commented 1 month ago

I know I can use numpy.asarray, but it would be nice if the conversion can be done implicitly, so I guess this is more of a feature request.

alshan commented 1 month ago

Sure, thanks for the feature request.

sunnyayyl commented 1 month ago

I'll edit the title to better reflect that this issue is a feature request. Thanks for your help!

sunnyayyl commented 1 month ago

I had a look at the source code, would this issue be solved by adding to this part? https://github.com/JetBrains/lets-plot/blob/master/python-package%2Flets_plot%2F_type_utils.py

sunnyayyl commented 1 month ago

Something on the line of

try:
   import jax.numpy as jnp
except:
   jnp=None

if (jnp and isinstance(v, jnp.ndarray))
   return _standardize_value(v.tolist())

(Edit: annoyingly, I will not be next to a computer for the next few days, so I can't test this)

alshan commented 1 month ago

This looks promising, thanx!

sunnyayyl commented 1 month ago

I'm currently working on this, I will try and open a pull request when I am done

sunnyayyl commented 1 month ago

plot