-
Notifications
You must be signed in to change notification settings - Fork 880
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
api reorganization #2447
api reorganization #2447
Conversation
Performance benchmarks:
|
On the first question I would go for 2), generic API with specific backends. Also I have been thinking about possibly removing Altair completely for 3.0 and then add the backend keyword and the Altair drawing functions in 3.1. This would give us more time to have a proper Altair implementation for all spaces and probably also adding interactive elements to the Altair plots, e.g. clicking on agents to get additional information/ change the model. This isn't hard to do in Altair, but I would like to have some time to have this truly stable. |
We were broadly in agreement to declare |
Was there already an agreement on that? Then that is fine as well. I proposed that somewhere, but the alternative was to have a good component API, which you single handedly provided in the last days :D |
I think @EwoutH agreed with your suggestion. I want to keep it experimental, with the understanding that we aim to implement Altair with a compatible API. We will only break the API it if we can't find a clean solution for altair within the confines of the API established for matplotlib. I'll do more reorganization of the code, such as moving |
Ok, I have reorganized things further.
Note that some of the stuff is implemented with an eye towards the altair rework. If @EwoutH agrees, it would make sense to add the experimental warnings as well (I can do it here or in a follow up PR). I think this PR is ready for review. |
I can dive into this Monday, if you would like to move forward before then I trust both your judgement. |
What's the minimal input you currently need to unblock this PR? |
A few simple points:
|
I found that the names became rather long if I did not do it, but I am fine with changing it.
Not sure I follow. At a minimum, the name
I followed @Corvince's suggestion based on my question at the start of this PR. I am fine with dropping it and only keeping the backend-specific ones. The benefit, however, of doing it this way is that it enforces us to have a common API for both backends. This is something that is quite desirable from a user point of view. I haven't bothered with comprehensive type hinting yet. I started doing it using |
Sounds it was well thought out, if you and Vincent align, I'm good |
Ok, let's see what @Corvince makes of this PR. |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
I agree with this PR very much. On question 2) I would agree to promote On matplotlib vs mpl: I agree that matplotlib names become a bit unwieldy due to their length. But I also think mpl is something to stumple over. Still I think mpl is the bit better solution, since I don't expect it being used that much directly. API-wise I think a simple user-facing solution is to just use Following this logic it might also make sense to define draw_space outside of mpl_space_drawing, since from a user-perspecive its more important to describe what we want to do (draw space) than to describe how we want to do it (using matplotlib) |
Unrelated, and I wanted to create a separate PR but its hard to get the timing right with respect to the reorganizing of the files:
|
for more information, see https://pre-commit.ci
Ok, I have moved I think that only leaves open a choice on |
good point, I have made that change here. |
Thanks for the work here. It is pretty similar to the refactoring part in #2453, with the main differences in API styles (OO vs. functional). Some comments from my side - On the matplotlib plotting functions:
On the SolaraViz components:
I think we overall agree on most of the stuff. Personally I don't have strong preferences on either OO-style or functional style. In summary, we could perhaps view the SolaraViz feature as some sort of "frontend" while matplotlib and altair are its "backend". So we could have in the future (apologies for the poor drawing) graph LR
A(SolaraViz) --- B(matplotlib)
A(SolaraViz) --- C(altair)
A(SolaraViz) --- D(other_backend)
E(VideoViz) --- B(matplotlib)
F(StreamlitViz) --- B(matplotlib)
The "frontend"s share common API in their creations: page = SolaraViz(
model,
[make_space_component(agent_portrayal=agent_portrayal),
make_measure_component("happy")],
)
viz = VideoViz(
model,
[make_space_component(agent_portrayal=agent_portrayal),
make_measure_component("happy")],
) but differ in how there are used: page # simply use the page to render Solara webpage
viz.record(steps=50, filepath="schelling.mp4") |
Thanks for the feedback @wang-boyu. Some quick reactions
I checked the code but at the moment there is no detailed measure plotting helper functions in matplotlib_components.py. If they are added in the future, I agree, that whatever is useful beyond solara should be in a seperate matplotlib/altair plotting file.
I agree. That's why I removed them from the
I want to hash out an OO version but not for MESA 3.0. I also completely agree on the altair/matplotlib/plotly(?) backend idea. |
|
||
|
||
@solara.component | ||
def PlotMatplotlib( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked the code but at the moment there is no detailed measure plotting helper functions in matplotlib_components.py.
@quaquel Sorry I meant this function. In #2453 it got changed to
fig, ax = plt.subplots()
renderer = MeasureRendererMatplotlib(
measure=measure, post_process=post_process, save_format=save_format
)
renderer.draw(model, ax)
where MeasureRendererMatplotlib
is extracted into matplotlib drawing file (equivalent to the mpl_space_drawing.py
file here). I thought that something similar could be done here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok that clarifies it. Indeed, the body of PlotMatplotlib
could be separated into its own helper function. However, at the moment, I don't see a lot of value in doing it. Its just a simple if else
block for handling the different kinds of possible arguments. I suggest we leave it here for now. Once this is merged, and it would be helpful for VideoViz, we can allways move it over.
A broader issue is that, at the moment, out of the box plotting is limited to line plots over time. I want to explore and see if there are default plots that we might want to support directly. Moreover, at the moment, the line plotting is tightly coupled to the datacollector. I want to find a way to separate these completely. In short, ample work to still be done after this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fully agree here with @quaquel . Currently there isn't much added benefit, but we should eventually provide not just line graphs and be independent of the data collector
Yes, I would like to get @Corvince's perspective on the |
I don't think it would be redundant. I expect |
Was there anything else to comment on from my side before we move on? |
No this is it. Thanks!
These are good points. I'll think about it a bit more this morning and merge it later today. |
From the diff, it looks good, and I have no objections. Really appreciate the work on this from all of you. |
Do we need to update the migration guide after this PR? |
good point, the guide is still valid but will raise some deprecation warnings so better to update it. |
(But note that doc updates can still be submitted after the release, which work-wise should be prioritized) |
docs default to the stable branch, so ideally we get it in (or have to to patch releases just for docs). |
|
This reorganizes the component code by separating solara components (in
altair_components
andmatplotlib_components
) and the more broadly useful space drawing functions (mpl_space_drawing
).I have a few questions/thoughts at this point:
We renamed the Matplotlib components to
make_space_component
andmake_plot_component
. Neither name indicates clearly that these are Matplotlib specific. There are at least 3 solutions:mpl
or so to their namemake_space_component
andmake_plot_components
and add a backend kwarg to control whether Matplotlib or Altair is used. I like this from an API point of view. It gives the user a single entry point when creating components, abstracting the backend (i.e., the plotting library) away. However it also suggests that both backends will always behave in the same way, and I am not sure that we can guarantee that and even whether that is desirable.altair_components
is being refactored.I kept the mpl_space_drawing module in
mesa.visualization.components
. It might make more sense to promote this tomesa.visualization
since they are also useful outside of solara components.I added the
mpl_space_drawing
module to the visualization docs but subsumed it under Matplotlib-based components. Depending on how we handle the previous questions, the docs might need to be reorganized a bit.Closes #2434.