Repository: pybamm-team/PyBaMM (python)

Issue:
Add a method to get a dict of _all_ summary variables
As of https://github.com/pybamm-team/PyBaMM/pull/4621, summary variables are now evaluated lazily to improve performance. However, it has made getting a data frame of all the summary results a little clunky. E.g. instead of `df = pd.DataFrame(sim.solution.summary_variables)` you now need to do
```python3
summary_variables = sim.solution.summary_variables
all_summary_variables = {
    k: summary_variables[k] for k in summary_variables.all_variables
}
all_summary_variables["Cycle number"] = list(
    summary_variables.cycle_number
)
df = pd.DataFrame(all_summary_variables)
```
or something equivalent. I propose we add an `all_summary_variables` attribute that returns the same as `all_summary_variables` in the above snippet. Or just make it so that `summary_variables["Cycle number"]` returns `list(summary_variables.cycle_number)`.  This would (almost) be consistent with `Solution` where you can do `sol.t` or `sol["Time [s]"].data`.

@pipliggins let me know if this is sensible or if I'm missing something from the new `SummaryVariables` class.

Pull request:
Add 'get_summary_variables' function to SummaryVariables
Adds a function to return a dictionary of all the summary variables + cycle number from the SummaryVariables class

Fixes #4765

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fd944874e..ac422425c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,7 @@
 
 ## Features
 
+- Added 'get_summary_variables' to return dictionary of computed summary variables ([#4824](https://github.com/pybamm-team/PyBaMM/pull/4824))
 - Added support for particle size distributions combined with particle mechanics. ([#4807](https://github.com/pybamm-team/PyBaMM/pull/4807))
 
 # [v25.1.1](https://github.com/pybamm-team/PyBaMM/tree/v25.1.1) - 2025-01-20
diff --git a/src/pybamm/solvers/summary_variable.py b/src/pybamm/solvers/summary_variable.py
index c293ebd62..4c3da92a4 100644
--- a/src/pybamm/solvers/summary_variable.py
+++ b/src/pybamm/solvers/summary_variable.py
@@ -188,3 +188,9 @@ class SummaryVariables:
             ) from error
 
         return esoh_sol
+
+    def get_summary_variables(self):
+        """
+        Computes and returns all the summary values + cycle number, as a dictionary
+        """
+        return {k: self[k] for k in [*self.all_variables, "Cycle number"]}


Test patch:
diff --git a/tests/unit/test_solvers/test_summary_variables.py b/tests/unit/test_solvers/test_summary_variables.py
index c8c3ed194..c2344b91e 100644
--- a/tests/unit/test_solvers/test_summary_variables.py
+++ b/tests/unit/test_solvers/test_summary_variables.py
@@ -171,3 +171,13 @@ class TestSummaryVariables:
         assert sol.summary_variables["Cycle number"][9] == 10
         assert len(sol.summary_variables["x_100"]) == 10
         assert np.isclose(sol.summary_variables["x_100"][0], 0.9493)
+
+    def test_summary_vars_get_all_variables(self):
+        # no esoh
+        sum_vars, _ = self.create_sum_vars()
+
+        summary_vars = sum_vars.get_summary_variables()
+
+        assert isinstance(summary_vars, dict)
+        assert list(summary_vars.keys()) == ["2c", "Change in 2c", "Cycle number"]
+        np.isclose(summary_vars["2c"], 0.735758)