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

hvPlot plots are not serializable #894

Open
ahuang11 opened this issue Dec 23, 2024 · 0 comments
Open

hvPlot plots are not serializable #894

ahuang11 opened this issue Dec 23, 2024 · 0 comments

Comments

@ahuang11
Copy link
Contributor

ahuang11 commented Dec 23, 2024

I thought this would work with #848

import panel as pn
from lumen.views import Panel
import holoviews as hv

column = pn.Column(pn.pane.HoloViews(
    hv.Curve([0, 1])
))
Panel(object=column).to_spec()

Traceback (most recent call last):
  File "/Users/ahuang/repos/panel/panel/io/server.py", line 138, in wrapped
    return await func(*args, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/repos/lumen/lumen/ai/coordinator.py", line 245, in use_suggestion
    await agent.respond(
  File "/Users/ahuang/repos/lumen/lumen/ai/agents.py", line 988, in respond
    spec = view.to_spec()
           ^^^^^^^^^^^^^^
  File "/Users/ahuang/repos/lumen/lumen/views/base.py", line 723, in to_spec
    spec['object'] = self._serialize_object(self.object)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/repos/lumen/lumen/views/base.py", line 707, in _serialize_object
    self._serialize_object(child, objects=objects, refs=refs, depth=depth+1)
  File "/Users/ahuang/repos/lumen/lumen/views/base.py", line 711, in _serialize_object
    value = obj.param.serialize_value(p)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/site-packages/param/parameterized.py", line 2601, in serialize_value
    return serializer.serialize_parameter_value(self_or_cls, pname)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/site-packages/param/serializer.py", line 139, in serialize_parameter_value
    return cls.dumps(pobj.param[pname].serialize(value))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/site-packages/param/serializer.py", line 81, in dumps
    return json.dumps(obj)
           ^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/Users/ahuang/miniconda3/envs/lumen/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Curve is not JSON serializable
from pydoc import text
import numpy as np
import pandas as pd
import lumen.ai as lmai

import param
import hvplot.pandas
import numpy as np
import panel as pn
import pandas as pd
import lumen.ai as lmai
from lumen.sources.duckdb import DuckDBSource


class WindAnalysis(lmai.Analysis):
    """
    Calculates the wind speed and direction from the u and v components of wind,
    displaying both the table and meteogram.
    """

    columns = param.List(default=["time", "u", "v"])

    def __call__(self, pipeline):
        df = pipeline.data

        df["wind_speed"] = np.sqrt(
            df["u"] ** 2 + df["v"] ** 2
        )
        # Calculate wind direction in degrees (from north)
        df["wind_direction"] = np.degrees(
            np.arctan2(df["u"], df["v"])
        )
        # Ensure wind direction is in the range [0, 360)
        df["wind_direction"] = (df["wind_direction"] + 360) % 360

        overlay = df.hvplot.line(
            x="time",
            y="wind_speed",
            title="Wind Speed",
            ylabel="Wind Speed (m/s)",
        )
        return pn.Column(
            pn.pane.HoloViews(overlay),
            pn.widgets.Tabulator(df)
        )


llm = lmai.llm.OpenAI()
uv_df = pd.DataFrame({
    "time": pd.date_range('2024-11-11', '2024-11-22'),
    "u": np.random.rand(12),
    "v": np.random.rand(12)
})
source = lmai.memory["source"] = DuckDBSource.from_df({"uv_df": uv_df})
analysis_agent = lmai.agents.AnalysisAgent(analyses=[WindAnalysis])
ui = lmai.ExplorerUI(llm=llm, agents=[analysis_agent])
ui.servable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant