diff --git a/docs/tutorials/intro_tutorial.ipynb b/docs/tutorials/intro_tutorial.ipynb index 1ca0777c..0c4dd450 100644 --- a/docs/tutorials/intro_tutorial.ipynb +++ b/docs/tutorials/intro_tutorial.ipynb @@ -33,21 +33,27 @@ " (You can use the table of contents button on the left side of the interface to skip to any specific part)\n", "\n", "\n", - "Users can use Google Colab (Please ensure you run the Colab dependency import cell) \n", + "Users can use Google Colab\\* (Please ensure you run the Colab dependency import cell- below) \n", "\n", - "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-geo/blob/main/docs/tutorials/intro_tutorial.ipynb)\n" + "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/projectmesa/mesa-geo/blob/main/docs/tutorials/intro_tutorial.ipynb)\n", + "\n", + "\\*Based on a recent Google Colab update currently, Solara visuals are not rendering. However, the link still provides a easy way to download the jupyter file. \n", + "\n", + "You can also [download the file directly from GitHub](https://github.com/projectmesa/mesa-geo/blob/main/docs/tutorials/intro_tutorial.ipynb)\n" ] }, { - "cell_type": "raw", - "id": "c0c702b3-c307-4f00-9530-468e29f53184", + "cell_type": "code", + "execution_count": null, + "id": "0b0dc4bb-cf8a-4c21-82aa-4a1cc43f1871", "metadata": {}, + "outputs": [], "source": [ - "#Run this if in colab\n", + "#Run this if in colab or if you need to install mesa and mesa-geo in your local environment. \n", "!pip install mesa --quiet\n", "!pip install mesa-geo --quiet\n", "!mkdir -p data\n", - "!wget -P data https://raw.githubusercontent.com/projectmesa/mesa-geo/tree/main/docs/tutorials/data/TorontoNeighbourhoods.geojson" + "!wget -P data https://raw.githubusercontent.com/projectmesa/mesa-geo/main/docs/tutorials/data/TorontoNeighbourhoods.geojson" ] }, { @@ -336,6 +342,7 @@ " self, pop_size=30, mobility_range=500, init_infection=0.2, exposure_dist=500, max_infection_risk=0.2,\n", " max_recovery_time=5\n", " ):\n", + " super().__init__()\n", " self.schedule = mesa.time.RandomActivationByType(self)\n", " self.space = mg.GeoSpace(warn_crs_conversion=False)\n", " \n", @@ -769,6 +776,7 @@ " self, pop_size=30, mobility_range=500, init_infection=0.2, exposure_dist=500, max_infection_risk=0.2,\n", " max_recovery_time=5\n", " ):\n", + " super().__init__()\n", " #Scheduler\n", " self.schedule = mesa.time.RandomActivationByType(self)\n", " #Space\n", @@ -1075,7 +1083,7 @@ }, "outputs": [], "source": [ - "page = mgv.GeoJupyterViz(\n", + "page = mgv.GeoSolaraViz(\n", " GeoSIR,\n", " model_params,\n", " measures= [[\"infected\", \"susceptible\", \"recovered\", \"dead\"], [\"safe\", \"hotspot\"]],\n", @@ -1105,7 +1113,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.3" + "version": "3.12.5" } }, "nbformat": 4, diff --git a/mesa_geo/visualization/__init__.py b/mesa_geo/visualization/__init__.py index 256d887d..71dfd6fe 100644 --- a/mesa_geo/visualization/__init__.py +++ b/mesa_geo/visualization/__init__.py @@ -1,5 +1,5 @@ # Import specific classes or functions from the modules -from mesa_geo.visualization.geojupyter_viz import GeoJupyterViz -from mesa_geo.visualization.leaflet_viz import LeafletViz +from .geosolara_viz import GeoJupyterViz, GeoSolaraViz +from .leaflet_viz import LeafletViz -__all__ = ["GeoJupyterViz", "LeafletViz"] +__all__ = ["GeoJupyterViz", "GeoSolaraViz", "LeafletViz"] diff --git a/mesa_geo/visualization/geojupyter_viz.py b/mesa_geo/visualization/geosolara_viz.py similarity index 94% rename from mesa_geo/visualization/geojupyter_viz.py rename to mesa_geo/visualization/geosolara_viz.py index 7b9d6960..6c594ae6 100644 --- a/mesa_geo/visualization/geojupyter_viz.py +++ b/mesa_geo/visualization/geosolara_viz.py @@ -1,8 +1,8 @@ import matplotlib.pyplot as plt -import mesa.experimental.components.matplotlib as components_matplotlib +import mesa.visualization.components.matplotlib as components_matplotlib import solara import xyzservices.providers as xyz -from mesa.experimental import jupyter_viz as jv +from mesa.visualization import solara_viz as sv from solara.alias import rv import mesa_geo.visualization.leaflet_viz as leaflet_viz @@ -91,7 +91,7 @@ def Card( @solara.component -def GeoJupyterViz( +def GeoSolaraViz( model_class, model_params, measures=None, @@ -151,7 +151,7 @@ def GeoJupyterViz( current_step = solara.use_reactive(0) # 1. Set up model parameters - user_params, fixed_params = jv.split_model_params(model_params) + user_params, fixed_params = sv.split_model_params(model_params) model_parameters, set_model_parameters = solara.use_state( {**fixed_params, **{k: v.get("value") for k, v in user_params.items()}} ) @@ -201,13 +201,13 @@ def handle_change_model_params(name: str, value: any): if measures: layout_types += [{"Measure": elem} for elem in range(len(measures))] - grid_layout_initial = jv.make_initial_grid_layout(layout_types=layout_types) + grid_layout_initial = sv.make_initial_grid_layout(layout_types=layout_types) grid_layout, set_grid_layout = solara.use_state(grid_layout_initial) with solara.Sidebar(): with solara.Card("Controls", margin=1, elevation=2): - jv.UserInputs(user_params, on_change=handle_change_model_params) - jv.ModelController(model, play_interval, current_step, reset_counter) + sv.UserInputs(user_params, on_change=handle_change_model_params) + sv.ModelController(model, play_interval, current_step, reset_counter) with solara.Card("Progress", margin=1, elevation=2): solara.Markdown(md_text=f"####Step - {current_step}") @@ -234,3 +234,6 @@ def handle_change_model_params(name: str, value: any): draggable=True, on_grid_layout=set_grid_layout, ) + + +GeoJupyterViz = GeoSolaraViz diff --git a/tests/test_GeoJupyterViz.py b/tests/test_GeoSolaraViz.py similarity index 70% rename from tests/test_GeoJupyterViz.py rename to tests/test_GeoSolaraViz.py index 1a646475..ad0f732b 100644 --- a/tests/test_GeoJupyterViz.py +++ b/tests/test_GeoSolaraViz.py @@ -3,14 +3,14 @@ import solara -from mesa_geo.visualization.geojupyter_viz import Card, GeoJupyterViz +from mesa_geo.visualization.geosolara_viz import Card, GeoSolaraViz class TestGeoViz(unittest.TestCase): - @patch("mesa_geo.visualization.geojupyter_viz.rv.CardTitle") - @patch("mesa_geo.visualization.geojupyter_viz.rv.Card") - @patch("mesa_geo.visualization.geojupyter_viz.components_matplotlib.PlotMatplotlib") - @patch("mesa_geo.visualization.geojupyter_viz.leaflet_viz.map") + @patch("mesa_geo.visualization.geosolara_viz.rv.CardTitle") + @patch("mesa_geo.visualization.geosolara_viz.rv.Card") + @patch("mesa_geo.visualization.geosolara_viz.components_matplotlib.PlotMatplotlib") + @patch("mesa_geo.visualization.geosolara_viz.leaflet_viz.map") def test_card_function( self, mock_map, @@ -31,7 +31,7 @@ def test_card_function( layout_type = {"Map": "default", "Measure": "Measure1"} with patch( - "mesa_geo.visualization.geojupyter_viz.rv.Card", return_value=MagicMock() + "mesa_geo.visualization.geosolara_viz.rv.Card", return_value=MagicMock() ) as mock_rv_card: _ = Card( model, @@ -53,19 +53,19 @@ def test_card_function( ) # mock_PlotMatplotlib.assert_called_once() - @patch("mesa_geo.visualization.geojupyter_viz.solara.GridDraggable") - @patch("mesa_geo.visualization.geojupyter_viz.solara.Sidebar") - @patch("mesa_geo.visualization.geojupyter_viz.solara.Card") - @patch("mesa_geo.visualization.geojupyter_viz.solara.Markdown") - @patch("mesa_geo.visualization.geojupyter_viz.jv.ModelController") - @patch("mesa_geo.visualization.geojupyter_viz.jv.UserInputs") - @patch("mesa_geo.visualization.geojupyter_viz.jv.split_model_params") - @patch("mesa_geo.visualization.geojupyter_viz.solara.use_memo") - @patch("mesa_geo.visualization.geojupyter_viz.solara.use_reactive") - @patch("mesa_geo.visualization.geojupyter_viz.solara.use_state") - @patch("mesa_geo.visualization.geojupyter_viz.solara.AppBarTitle") - @patch("mesa_geo.visualization.geojupyter_viz.solara.AppBar") - @patch("mesa_geo.visualization.geojupyter_viz.leaflet_viz.MapModule") + @patch("mesa_geo.visualization.geosolara_viz.solara.GridDraggable") + @patch("mesa_geo.visualization.geosolara_viz.solara.Sidebar") + @patch("mesa_geo.visualization.geosolara_viz.solara.Card") + @patch("mesa_geo.visualization.geosolara_viz.solara.Markdown") + @patch("mesa_geo.visualization.geosolara_viz.sv.ModelController") + @patch("mesa_geo.visualization.geosolara_viz.sv.UserInputs") + @patch("mesa_geo.visualization.geosolara_viz.sv.split_model_params") + @patch("mesa_geo.visualization.geosolara_viz.solara.use_memo") + @patch("mesa_geo.visualization.geosolara_viz.solara.use_reactive") + @patch("mesa_geo.visualization.geosolara_viz.solara.use_state") + @patch("mesa_geo.visualization.geosolara_viz.solara.AppBarTitle") + @patch("mesa_geo.visualization.geosolara_viz.solara.AppBar") + @patch("mesa_geo.visualization.geosolara_viz.leaflet_viz.MapModule") def test_geojupyterviz_function( self, mock_MapModule, # noqa: N803 @@ -98,7 +98,7 @@ def test_geojupyterviz_function( mock_use_memo.return_value = MagicMock() solara.render( - GeoJupyterViz( + GeoSolaraViz( model_class=model_class, model_params=model_params, measures=measures,