From df0fe8265328acacdda04028c5b04248174dd032 Mon Sep 17 00:00:00 2001 From: nissu99 Date: Sun, 17 Nov 2024 15:33:07 +0530 Subject: [PATCH 01/18] Updated docs and check_model param --- docs/getting_started.md | 10 ++ docs/migration_guide.md | 11 ++ docs/tutorials/visualization_tutorial.ipynb | 134 ++++++++++---------- mesa/visualization/solara_viz.py | 2 + 4 files changed, 93 insertions(+), 64 deletions(-) diff --git a/docs/getting_started.md b/docs/getting_started.md index b928599b5f4..3f8156ad02c 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -180,6 +180,16 @@ Mesa now uses a new browser-based visualization system called SolaraViz. This al > **Note:** SolaraViz is experimental and still in active development for Mesa 3.0. While we attempt to minimize them, there might be API breaking changes between Mesa 3.0 and 3.1. There won't be breaking changes between Mesa 3.0.x patch releases. +> **Note:** SolaraViz instantiates new models using `**model_parameters.value`, so all model inputs must be keyword arguments. + +Ensure your model's `__init__` method accepts keyword arguments matching the `model_params` keys. + +```python +class MyModel(Model): + def __init__(self, n_agents): + # Initialize the model with N agents +``` + The core functionality for building your own visualizations resides in the [`mesa.visualization`](apis/visualization) namespace Here's a basic example of how to set up a visualization: diff --git a/docs/migration_guide.md b/docs/migration_guide.md index 1422d272b47..8ae1124c2e5 100644 --- a/docs/migration_guide.md +++ b/docs/migration_guide.md @@ -259,6 +259,17 @@ the import from mesa.experimental. Otherwise here is a list of things you need t Previously SolaraViz was initialized by providing a `model_cls` and a `model_params`. This has changed to expect a model instance `model`. You can still provide (user-settable) `model_params`, but only if users should be able to change them. It is now also possible to pass in a "reactive model" by first calling `model = solara.reactive(model)`. This is useful for notebook environments. It allows you to pass the model to the SolaraViz Module, but continue to use the model. For example calling `model.value.step()` (notice the extra .value) will automatically update the plots. This currently only automatically works for the step method, you can force visualization updates by calling `model.value.force_update()`. +### Model Initialization with Keyword Arguments + +With the introduction of SolaraViz in Mesa 3.0, models are now instantiated using `**model_parameters.value`. This means all inputs for initializing a new model must be keyword arguments. Ensure your model's `__init__` method accepts keyword arguments matching the keys in `model_params`. + +```python +class MyModel(mesa.Model): + def __init__(self, n_agents, seed=None): + super().__init__(seed=seed) + # Initialize the model with N agents +``` + #### Default space visualization Previously we included a default space drawer that you could configure with an `agent_portrayal` function. You now have to explicitly create a space drawer with the `agent_portrayal` function diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index f28ece08308..64dd0890b7c 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -3,7 +3,9 @@ { "cell_type": "markdown", "metadata": {}, - "source": "# Visualization Tutorial" + "source": [ + "# Visualization Tutorial" + ] }, { "cell_type": "markdown", @@ -51,10 +53,10 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import mesa\n", "print(f\"Mesa version: {mesa.__version__}\")\n", @@ -66,10 +68,10 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "def agent_portrayal(agent):\n", " return {\n", @@ -79,15 +81,17 @@ ] }, { - "metadata": {}, "cell_type": "markdown", - "source": "In addition to the portrayal method, we instantiate the model parameters, some of which are modifiable by user inputs. In this case, the number of agents, N, is specified as a slider of integers." + "metadata": {}, + "source": [ + "In addition to the portrayal method, we instantiate the model parameters, some of which are modifiable by user inputs. In this case, the number of agents, N, is specified as a slider of integers." + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "model_params = {\n", " \"n\": {\n", @@ -104,8 +108,8 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "Next, we instantiate the visualization object which (by default) displays the grid containing the agents, and timeseries of values computed by the model's data collector. In this example, we specify the Gini coefficient.\n", "\n", @@ -118,13 +122,13 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Create initial model instance\n", - "model1 = MoneyModel(50, 10, 10)\n", + "model1 = MoneyModel(n=50, width=10, height=10) #keyword arguments\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")\n", @@ -140,8 +144,8 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Part 2 - Dynamic Agent Representation \n", "\n", @@ -155,10 +159,10 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import mesa\n", "print(f\"Mesa version: {mesa.__version__}\")\n", @@ -169,10 +173,10 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "def agent_portrayal(agent):\n", " size = 10\n", @@ -197,13 +201,13 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Create initial model instance\n", - "model1 = MoneyModel(50, 10, 10)\n", + "model1 = MoneyModel(n=50, width=10, height=10)\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")\n", @@ -219,8 +223,8 @@ ] }, { - "metadata": {}, "cell_type": "markdown", + "metadata": {}, "source": [ "## Part 3 - Custom Components \n", "\n", @@ -236,10 +240,10 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "import mesa\n", "print(f\"Mesa version: {mesa.__version__}\")\n", @@ -253,10 +257,10 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "def agent_portrayal(agent):\n", " size = 10\n", @@ -281,15 +285,17 @@ ] }, { - "metadata": {}, "cell_type": "markdown", - "source": "Next, we update our solara frontend to use this new component" + "metadata": {}, + "source": [ + "Next, we update our solara frontend to use this new component" + ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "@solara.component\n", "def Histogram(model):\n", @@ -306,13 +312,13 @@ ] }, { - "metadata": {}, "cell_type": "code", - "outputs": [], "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Create initial model instance\n", - "model1 = MoneyModel(50, 10, 10)\n", + "model1 = MoneyModel(n=50, width=10, height=10)\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")" @@ -320,42 +326,42 @@ }, { "cell_type": "code", + "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2024-10-29T19:38:49.471838Z", "start_time": "2024-10-29T19:38:47.897295Z" } }, - "source": [ - "page = SolaraViz(\n", - " model1,\n", - " components=[SpaceGraph, GiniPlot, Histogram],\n", - " model_params=model_params,\n", - " name=\"Boltzmann Wealth Model\",\n", - ")\n", - "# This is required to render the visualization in the Jupyter notebook\n", - "page" - ], "outputs": [ { "data": { - "text/plain": [ - "Cannot show ipywidgets in text" - ], + "application/vnd.jupyter.widget-view+json": { + "model_id": "bc71b89ee5684038a194eee4c36f4a4c", + "version_major": 2, + "version_minor": 0 + }, "text/html": [ "Cannot show widget. You probably want to rerun the code cell above (Click in the code cell, and press Shift+Enter +)." ], - "application/vnd.jupyter.widget-view+json": { - "version_major": 2, - "version_minor": 0, - "model_id": "bc71b89ee5684038a194eee4c36f4a4c" - } + "text/plain": [ + "Cannot show ipywidgets in text" + ] }, "metadata": {}, "output_type": "display_data" } ], - "execution_count": 12 + "source": [ + "page = SolaraViz(\n", + " model1,\n", + " components=[SpaceGraph, GiniPlot, Histogram],\n", + " model_params=model_params,\n", + " name=\"Boltzmann Wealth Model\",\n", + ")\n", + "# This is required to render the visualization in the Jupyter notebook\n", + "page" + ] }, { "cell_type": "markdown", @@ -366,35 +372,35 @@ }, { "cell_type": "code", + "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2024-10-29T19:38:49.505725Z", "start_time": "2024-10-29T19:38:49.472599Z" } }, - "source": [ - "Histogram(model1)" - ], "outputs": [ { "data": { - "text/plain": [ - "Cannot show ipywidgets in text" - ], + "application/vnd.jupyter.widget-view+json": { + "model_id": "0491f167a1434a92b78535078bd082a8", + "version_major": 2, + "version_minor": 0 + }, "text/html": [ "Cannot show widget. You probably want to rerun the code cell above (Click in the code cell, and press Shift+Enter +)." ], - "application/vnd.jupyter.widget-view+json": { - "version_major": 2, - "version_minor": 0, - "model_id": "0491f167a1434a92b78535078bd082a8" - } + "text/plain": [ + "Cannot show ipywidgets in text" + ] }, "metadata": {}, "output_type": "display_data" } ], - "execution_count": 13 + "source": [ + "Histogram(model1)" + ] }, { "cell_type": "markdown", diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py index 1f3c8036888..d5ffb4719a3 100644 --- a/mesa/visualization/solara_viz.py +++ b/mesa/visualization/solara_viz.py @@ -412,6 +412,8 @@ def _check_model_params(init_func, model_params): """ model_parameters = inspect.signature(init_func).parameters for name in model_parameters: + if name not in model_parameters: + raise TypeError(f"The parameter '{name}' is not accepted as a keyword argument in the model's __init__ method.") if ( model_parameters[name].default == inspect.Parameter.empty and name not in model_params From 78ffc74337175ead8521d58a63acfde6a817f323 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 16 Nov 2024 22:08:38 +0000 Subject: [PATCH 02/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/visualization/solara_viz.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py index d5ffb4719a3..51fb4763860 100644 --- a/mesa/visualization/solara_viz.py +++ b/mesa/visualization/solara_viz.py @@ -413,7 +413,9 @@ def _check_model_params(init_func, model_params): model_parameters = inspect.signature(init_func).parameters for name in model_parameters: if name not in model_parameters: - raise TypeError(f"The parameter '{name}' is not accepted as a keyword argument in the model's __init__ method.") + raise TypeError( + f"The parameter '{name}' is not accepted as a keyword argument in the model's __init__ method." + ) if ( model_parameters[name].default == inspect.Parameter.empty and name not in model_params From 44c04ed4a100a1fad204020afd9fad3ad29b3603 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:34:57 +0530 Subject: [PATCH 03/18] Update docs/getting_started.md Co-authored-by: Jan Kwakkel --- docs/getting_started.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/getting_started.md b/docs/getting_started.md index 3f8156ad02c..83b83eaee40 100644 --- a/docs/getting_started.md +++ b/docs/getting_started.md @@ -186,9 +186,9 @@ Ensure your model's `__init__` method accepts keyword arguments matching the `mo ```python class MyModel(Model): - def __init__(self, n_agents): + def __init__(self, n_agents=10, seed=None): + super().__init__(seed=seed) # Initialize the model with N agents -``` The core functionality for building your own visualizations resides in the [`mesa.visualization`](apis/visualization) namespace From 66541c711a56f1126cb5a1bc10ec39e0af05856e Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:35:18 +0530 Subject: [PATCH 04/18] Update docs/migration_guide.md Co-authored-by: Jan Kwakkel --- docs/migration_guide.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/migration_guide.md b/docs/migration_guide.md index 8ae1124c2e5..28bd90c4a46 100644 --- a/docs/migration_guide.md +++ b/docs/migration_guide.md @@ -265,7 +265,7 @@ With the introduction of SolaraViz in Mesa 3.0, models are now instantiated usin ```python class MyModel(mesa.Model): - def __init__(self, n_agents, seed=None): + def __init__(self, n_agents=10, seed=None): super().__init__(seed=seed) # Initialize the model with N agents ``` From a22cae6a6211bb9a04d6223b548c2f38a2aaca20 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:35:31 +0530 Subject: [PATCH 05/18] Update docs/tutorials/visualization_tutorial.ipynb Co-authored-by: Jan Kwakkel --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index 64dd0890b7c..78ef665fa9d 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -128,7 +128,7 @@ "outputs": [], "source": [ "# Create initial model instance\n", - "model1 = MoneyModel(n=50, width=10, height=10) #keyword arguments\n", + "model = MoneyModel(n=50, width=10, height=10) #keyword arguments\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")\n", From b32ca6f0096eb9e82fa32048a5a4745bb2a3eb28 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:35:46 +0530 Subject: [PATCH 06/18] Update docs/tutorials/visualization_tutorial.ipynb Co-authored-by: Jan Kwakkel --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index 78ef665fa9d..1b1f7924261 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -207,7 +207,7 @@ "outputs": [], "source": [ "# Create initial model instance\n", - "model1 = MoneyModel(n=50, width=10, height=10)\n", + "model = MoneyModel(n=50, width=10, height=10)\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")\n", From dc3c7ead8b939ae38d29492d027bc3eb82877f1a Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:35:55 +0530 Subject: [PATCH 07/18] Update docs/tutorials/visualization_tutorial.ipynb Co-authored-by: Jan Kwakkel --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index 1b1f7924261..dd772872a37 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -318,7 +318,7 @@ "outputs": [], "source": [ "# Create initial model instance\n", - "model1 = MoneyModel(n=50, width=10, height=10)\n", + "model = MoneyModel(n=50, width=10, height=10)\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")" From 7ea09de034887801f66b6267b5baaba5a42307e8 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 17 Nov 2024 18:36:33 +0530 Subject: [PATCH 08/18] Update docs/tutorials/visualization_tutorial.ipynb Co-authored-by: Jan Kwakkel --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index dd772872a37..b75c35b0012 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -354,7 +354,7 @@ ], "source": [ "page = SolaraViz(\n", - " model1,\n", + " model,\n", " components=[SpaceGraph, GiniPlot, Histogram],\n", " model_params=model_params,\n", " name=\"Boltzmann Wealth Model\",\n", From c25ba269a80869a08fa151f9ecee6c5e05c38c97 Mon Sep 17 00:00:00 2001 From: nissu99 Date: Tue, 19 Nov 2024 23:36:09 +0530 Subject: [PATCH 09/18] new test case added for arguments and keyword arguments in solara --- mesa/visualization/solara_viz.py | 17 +++++++++++++---- tests/test_solara_viz.py | 16 ++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py index 51fb4763860..9a15a45f228 100644 --- a/mesa/visualization/solara_viz.py +++ b/mesa/visualization/solara_viz.py @@ -411,11 +411,20 @@ def _check_model_params(init_func, model_params): ValueError: If a parameter is not valid for the model's initialization function """ model_parameters = inspect.signature(init_func).parameters + + has_var_positional = any( + param.kind == inspect.Parameter.VAR_POSITIONAL + for param in model_parameters.values() + ) + has_var_keyword = any( + param.kind == inspect.Parameter.VAR_KEYWORD + for param in model_parameters.values() + ) + + if has_var_positional and has_var_keyword: + raise ValueError("Models with both *args and **kwargs are not supported") + for name in model_parameters: - if name not in model_parameters: - raise TypeError( - f"The parameter '{name}' is not accepted as a keyword argument in the model's __init__ method." - ) if ( model_parameters[name].default == inspect.Parameter.empty and name not in model_params diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index 67a72dcba9b..d7f91713b6f 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -213,3 +213,19 @@ def __init__(self, **kwargs): # Test empty params dict raises ValueError if required params with pytest.raises(ValueError, match="Missing required model parameter"): _check_model_params(ModelWithOnlyRequired.__init__, {}) + + +# Test to check if params are arguments and keyword arguments +def test_check_model_params_with_both_args_and_kwargs(): + """Test that _check_model_params raises ValueError when both *args and **kwargs are present in model initialization.""" + + class ModelWithArgsAndKwargs: + def __init__(self, param1, *args, **kwargs): + pass + + model_params = {"param1": 1} + + with pytest.raises( + ValueError, match="Models with both \\*args and \\*\\*kwargs are not supported" + ): + _check_model_params(ModelWithArgsAndKwargs.__init__, model_params) From 2a0242221092ca258bb7b0a7899d64fc5bc91583 Mon Sep 17 00:00:00 2001 From: nissu99 Date: Mon, 18 Nov 2024 09:49:40 +0530 Subject: [PATCH 10/18] tests for arguments and keyword arguments solaraViz --- tests/test_solara_viz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index d7f91713b6f..ca8dbd9ee1e 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -194,6 +194,7 @@ def __init__(self, **kwargs): # test hat kwargs are accepted even if no model_params are specified _check_model_params(ModelWithKwargs.__init__, {}) + # Test invalid parameter name raises ValueError with pytest.raises(ValueError, match="Invalid model parameter: invalid_param"): _check_model_params( From 36d7196d6d10aa1263601d15272533e7bffa45fa Mon Sep 17 00:00:00 2001 From: nissu99 Date: Tue, 19 Nov 2024 22:59:34 +0530 Subject: [PATCH 11/18] added a new test for arguments and keyword arguments in solara --- ...ilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx | Bin 0 -> 69632 bytes tests/test_solara_viz.py | 1 - 2 files changed, 1 deletion(-) create mode 100644 .coverage.animesh-HP-Pavilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx diff --git a/.coverage.animesh-HP-Pavilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx b/.coverage.animesh-HP-Pavilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx new file mode 100644 index 0000000000000000000000000000000000000000..560a0bd8e5d9b8a49fd66b0b3f4a8be75bac6151 GIT binary patch literal 69632 zcmeI5Yi!%r6~`%wdOsL9bye3*%%n{mC$<&GZPGN&!%3Vpah^`nH0@fJlCEtw6seGu z?F0xmrfV~781`iY)~)EU0UHLadvwEw0mDA*-2ofce%OF**oS^8x(#bFU@Zo;KzA-F zN|Y_7q@*IPcLZD3gZIyKe&^hC$$Kg5%;{s2LHHR}D~Sd_&a7er0cIc1GYpe}-yZmN zZc%u^I#1wl!1cJ*!vr%r|Cu!R5R;00mf;>xpX5eT&!smeUrCkXpH4m>72^eX18+zG z2_OL^@c&J~Je`VW`}zXct{P%qCWa;!h-TlX4@^%^oSo#)PV7H6$=k2-n-VTJ7m0K`KHkgUo_NS(8el zcA39OE)QE9RQG0(MZg~Rp~TQrA5TQHt5*lE z6U!%2E5J{(!5es??#huF5Gtrzky@YEM5QoC^kH7Cd00@v&PBuaR#QtkL*->jvD{R2 z$&gfqCyS&|F-WnggToY>a|3v8Z$Rq_>d+H*%j&*dbn1g#0y<*#+Ul@bC)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+!hHS z0VME26FAJ8J)TNw!>Y=%#iD~g<|$mu0hW%dNFV6H9QR_;ejgK9E^}N9JoUw*2_I($ z&9xd9i}p>k=AeJ#F4LAR7VY&h(Rl+48y1W9_&B+0uIUQv?gv?Obw@r|7mFU~IIi`@ zqFo&uRb4E)e}Xl`{=IYY*}0cB1D+HbFFSRveN7%@&15_6-Itoi+7xNHfHvVdK#iU> zyMQ+Cd2*vstA71+uGOW>Y0nEIEv1`XY@IpInrqsKgUe!9R;2c`X0#18mqgU-F}ZUQ zZ#Wksp00v-COX=kC0h2;7)S7qc4vu}DcUpC{XXh#b!pH(v0tv|d{p&L|Lb2ZBwe9e zbCfl+o}yh_|FcEwpUiR)uJuvb8eFbR+3W^9_vV&1)n@B>&wZn%mW$Rp&mDM)TGJ1K z)IML+dyX|n+6h8aStlMnKE~&Ucr;bDLvu@40o0;!H7{{U5#1MdI; literal 0 HcmV?d00001 diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index ca8dbd9ee1e..d7f91713b6f 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -194,7 +194,6 @@ def __init__(self, **kwargs): # test hat kwargs are accepted even if no model_params are specified _check_model_params(ModelWithKwargs.__init__, {}) - # Test invalid parameter name raises ValueError with pytest.raises(ValueError, match="Invalid model parameter: invalid_param"): _check_model_params( From e62ef3b83a2c42200105f12da455a920705564c8 Mon Sep 17 00:00:00 2001 From: nissu99 Date: Tue, 19 Nov 2024 23:39:07 +0530 Subject: [PATCH 12/18] new test in solara for checking arguments and keyword arguments --- ...ilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx | Bin 69632 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .coverage.animesh-HP-Pavilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx diff --git a/.coverage.animesh-HP-Pavilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx b/.coverage.animesh-HP-Pavilion-Gaming-Laptop-15-ec0xxx.19901.XXGyvLfx deleted file mode 100644 index 560a0bd8e5d9b8a49fd66b0b3f4a8be75bac6151..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 69632 zcmeI5Yi!%r6~`%wdOsL9bye3*%%n{mC$<&GZPGN&!%3Vpah^`nH0@fJlCEtw6seGu z?F0xmrfV~781`iY)~)EU0UHLadvwEw0mDA*-2ofce%OF**oS^8x(#bFU@Zo;KzA-F zN|Y_7q@*IPcLZD3gZIyKe&^hC$$Kg5%;{s2LHHR}D~Sd_&a7er0cIc1GYpe}-yZmN zZc%u^I#1wl!1cJ*!vr%r|Cu!R5R;00mf;>xpX5eT&!smeUrCkXpH4m>72^eX18+zG z2_OL^@c&J~Je`VW`}zXct{P%qCWa;!h-TlX4@^%^oSo#)PV7H6$=k2-n-VTJ7m0K`KHkgUo_NS(8el zcA39OE)QE9RQG0(MZg~Rp~TQrA5TQHt5*lE z6U!%2E5J{(!5es??#huF5Gtrzky@YEM5QoC^kH7Cd00@v&PBuaR#QtkL*->jvD{R2 z$&gfqCyS&|F-WnggToY>a|3v8Z$Rq_>d+H*%j&*dbn1g#0y<*#+Ul@bC)F*pl(d@uL;QA#i zQ79fEFBy96LA)VpYA?4)9r}LjyrI3MF-d4Il4V|V{zHP0coHEZD&3Wi{WJYgWeS1Ud-R0%xQ%eD1toATr_C=!E{{FzVxRplLXjT728&b%LWRo)Lyj3EiUeTOFa-$;Y zw5V4}r;k+M0e42Ck?cf&D|f2f31ig4rrSHWu@0#jx_1KDE-5jy-jIZGrl8hdpdHWNi-J`;@Bd5)7+! z*wXKJUgZbpM4eV4eUQ(qs!T-19vh&=5gqm^>AYpb@=W2HD>1rEt{Odg_ilV z(CM811rJIkQ7Kl(D>@9@s!;oB=cN0wiwHF22cc8y&_$PMI-=EgX~lB%N6Wy3s+Po} z-Q8(WT#5^GWSN{j8XXi3Vn`*z(@iX6ti)Onb-thxcoW9bp~29N(tt)w7^C;}EICFG z<;;OVG`nR>z^qg|qg`h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Ucr;bDLvu@40o0;!H7{{U5#1MdI; From c4b864d1dfd68421a3fda0e6fe3dfe5f13526bf6 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:27:42 +0530 Subject: [PATCH 13/18] changes model to model1 --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index b75c35b0012..c3ddecaa3c1 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -128,7 +128,7 @@ "outputs": [], "source": [ "# Create initial model instance\n", - "model = MoneyModel(n=50, width=10, height=10) #keyword arguments\n", + "model1 = MoneyModel(n=50, width=10, height=10) #keyword arguments\n", "\n", "SpaceGraph = make_space_component(agent_portrayal)\n", "GiniPlot = make_plot_component(\"Gini\")\n", From c31470e86f76f6682e9111bfe77d2cbd16d1356b Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:01:02 +0530 Subject: [PATCH 14/18] model name corrected --- docs/tutorials/visualization_tutorial.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/visualization_tutorial.ipynb b/docs/tutorials/visualization_tutorial.ipynb index c3ddecaa3c1..48794f15542 100644 --- a/docs/tutorials/visualization_tutorial.ipynb +++ b/docs/tutorials/visualization_tutorial.ipynb @@ -399,7 +399,7 @@ } ], "source": [ - "Histogram(model1)" + "Histogram(model)" ] }, { From f853308952613157b74bff24e5bfa2bc189f9679 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 8 Dec 2024 20:04:07 +0530 Subject: [PATCH 15/18] var_keyword removed. --- mesa/visualization/solara_viz.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py index aceacbb44d4..2f88d199984 100644 --- a/mesa/visualization/solara_viz.py +++ b/mesa/visualization/solara_viz.py @@ -428,14 +428,10 @@ def _check_model_params(init_func, model_params): param.kind == inspect.Parameter.VAR_POSITIONAL for param in model_parameters.values() ) - has_var_keyword = any( - param.kind == inspect.Parameter.VAR_KEYWORD - for param in model_parameters.values() - ) - - if has_var_positional and has_var_keyword: - raise ValueError("Models with both *args and **kwargs are not supported") - + + if has_var_positional: + raise ValueError("Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value" ) + for name in model_parameters: if ( model_parameters[name].default == inspect.Parameter.empty From 4b57543a4ee985e4e0ad7f0386fb93913c5b4bce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:34:14 +0000 Subject: [PATCH 16/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- mesa/visualization/solara_viz.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py index 2f88d199984..ee62c8b0156 100644 --- a/mesa/visualization/solara_viz.py +++ b/mesa/visualization/solara_viz.py @@ -428,10 +428,12 @@ def _check_model_params(init_func, model_params): param.kind == inspect.Parameter.VAR_POSITIONAL for param in model_parameters.values() ) - - if has_var_positional: - raise ValueError("Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value" ) - + + if has_var_positional: + raise ValueError( + "Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value" + ) + for name in model_parameters: if ( model_parameters[name].default == inspect.Parameter.empty From 38dce53a112524258249157858e915dd3c001401 Mon Sep 17 00:00:00 2001 From: Animesh Rawat <131552285+nissu99@users.noreply.github.com> Date: Sun, 8 Dec 2024 22:25:36 +0530 Subject: [PATCH 17/18] test updated --- tests/test_solara_viz.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index d7f91713b6f..c22f8ce32cc 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -215,17 +215,15 @@ def __init__(self, **kwargs): _check_model_params(ModelWithOnlyRequired.__init__, {}) -# Test to check if params are arguments and keyword arguments -def test_check_model_params_with_both_args_and_kwargs(): - """Test that _check_model_params raises ValueError when both *args and **kwargs are present in model initialization.""" - - class ModelWithArgsAndKwargs: - def __init__(self, param1, *args, **kwargs): + #test that _check_model_params raises ValueError when *args are present +def test_check_model_params_with_args_only(): + """Test that _check_model_params raises ValueError when *args are present.""" + + class ModelWithArgsOnly: + def __init__(self, param1, *args): pass model_params = {"param1": 1} - with pytest.raises( - ValueError, match="Models with both \\*args and \\*\\*kwargs are not supported" - ): - _check_model_params(ModelWithArgsAndKwargs.__init__, model_params) + with pytest.raises(ValueError, match= "Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value"): + _check_model_params(ModelWithArgsOnly.__init__, model_params) From 5f7cccaa38519388f615fcc9857ed2ad856f3798 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 8 Dec 2024 16:55:43 +0000 Subject: [PATCH 18/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_solara_viz.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_solara_viz.py b/tests/test_solara_viz.py index c22f8ce32cc..3b8d82fb7bc 100644 --- a/tests/test_solara_viz.py +++ b/tests/test_solara_viz.py @@ -215,15 +215,18 @@ def __init__(self, **kwargs): _check_model_params(ModelWithOnlyRequired.__init__, {}) - #test that _check_model_params raises ValueError when *args are present +# test that _check_model_params raises ValueError when *args are present def test_check_model_params_with_args_only(): """Test that _check_model_params raises ValueError when *args are present.""" - + class ModelWithArgsOnly: def __init__(self, param1, *args): pass model_params = {"param1": 1} - with pytest.raises(ValueError, match= "Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value"): + with pytest.raises( + ValueError, + match="Mesa's visualization requires the use of keyword arguments to ensure the parameters are passed to Solara correctly. Please ensure all model parameters are of form param=value", + ): _check_model_params(ModelWithArgsOnly.__init__, model_params)