Repository: projectmesa/mesa

Issue:
Fixing batch_run bug
There may be a more elegant fix but this just checks to make sure there are agent variables being collected. If not, it just stores the model data.

Fixes #1112 

Apologies for not knowing how to just include the correct commit. :)



Discussion:
# [Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) Report
> Merging [#1113](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) (c5896ee) into [main](https://codecov.io/gh/projectmesa/mesa/commit/cda0495a4c7ca9656cbefd393d9a085b7cbf3367?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) (cda0495) will **decrease** coverage by `0.14%`.
> The diff coverage is `33.33%`.

> :exclamation: Current head c5896ee differs from pull request most recent head 2f6f90f. Consider uploading reports for the commit 2f6f90f to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/projectmesa/mesa/pull/1113/graphs/tree.svg?width=650&height=150&src=pr&token=OUHxz83iDi&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa)](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa)

```diff
@@            Coverage Diff             @@
##             main    #1113      +/-   ##
==========================================
- Coverage   89.47%   89.33%   -0.15%     
==========================================
  Files          19       19              
  Lines        1254     1256       +2     
  Branches      256      257       +1     
==========================================
  Hits         1122     1122              
- Misses         97       98       +1     
- Partials       35       36       +1     
```


| [Impacted Files](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa) | Coverage Δ | |
|---|---|---|
| [mesa/batchrunner.py](https://codecov.io/gh/projectmesa/mesa/pull/1113/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa#diff-bWVzYS9iYXRjaHJ1bm5lci5weQ==) | `91.55% <33.33%> (-0.60%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa). Last update [cda0495...2f6f90f](https://codecov.io/gh/projectmesa/mesa/pull/1113?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=projectmesa).

You need to squash all the 4 commits into 1.
The commit message needs to be more descriptive.
I created a new PR at #1115 and I'll close this one.

Resolving patch:
diff --git a/mesa/batchrunner.py b/mesa/batchrunner.py
--- a/mesa/batchrunner.py
+++ b/mesa/batchrunner.py
@@ -172,10 +172,15 @@ def _model_run_func(
     for step in steps:
         model_data, all_agents_data = _collect_data(model, step)
 
-        stepdata = [
-            {**{"Step": step}, **kwargs, **model_data, **agent_data}
-            for agent_data in all_agents_data
-        ]
+        # If there are agent_reporters, then create an entry for each agent
+        if all_agents_data:
+            stepdata = [
+                {**{"Step": step}, **kwargs, **model_data, **agent_data}
+                for agent_data in all_agents_data
+            ]
+        # If there is only model data, then create a single entry for the step
+        else:
+            stepdata = [{**{"Step": step}, **kwargs, **model_data}]
         data.extend(stepdata)
 
     return tuple(kwargs.values()), data


Test patch:
diff --git a/tests/test_batch_run.py b/tests/test_batch_run.py
--- a/tests/test_batch_run.py
+++ b/tests/test_batch_run.py
@@ -47,6 +47,7 @@ def __init__(
         variable_agent_param=None,
         fixed_model_param=None,
         schedule=None,
+        enable_agent_reporters=True,
         **kwargs
     ):
         super().__init__()
@@ -55,9 +56,13 @@ def __init__(
         self.variable_agent_param = variable_agent_param
         self.fixed_model_param = fixed_model_param
         self.n_agents = 3
+        if enable_agent_reporters:
+            agent_reporters = {"agent_id": "unique_id", "agent_local": "local"}
+        else:
+            agent_reporters = None
         self.datacollector = DataCollector(
             model_reporters={"reported_model_param": self.get_local_model_param},
-            agent_reporters={"agent_id": "unique_id", "agent_local": "local"},
+            agent_reporters=agent_reporters,
         )
         self.running = True
         self.init_agents()
@@ -121,5 +126,19 @@ def test_batch_run_with_params():
     )
 
 
+def test_batch_run_no_agent_reporters():
+    result = batch_run(MockModel, {"enable_agent_reporters": False})
+    print(result)
+    assert result == [
+        {
+            "RunId": 0,
+            "iteration": 0,
+            "Step": 1000,
+            "enable_agent_reporters": False,
+            "reported_model_param": 42,
+        }
+    ]
+
+
 def test_batch_run_single_core():
     batch_run(MockModel, {}, nr_processes=1, iterations=10)