Repository: pybamm-team/PyBaMM
Pull request #4798 (2025-01-23 23:29:38)

add cycle number as a key
# Description

Adds cycle number as a key for summary variables (#4765 )

I didn't do the "all summary variables" part as I wasn't sure what the best name for that function was

## Type of change

Please add a line in the relevant section of [CHANGELOG.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CHANGELOG.md) to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

- [ ] New feature (non-breaking change which adds functionality)
- [ ] Optimization (back-end change that speeds up the code)
- [ ] Bug fix (non-breaking change which fixes an issue)

# Key checklist:

- [ ] No style issues: `$ pre-commit run` (or `$ nox -s pre-commit`) (see [CONTRIBUTING.md](https://github.com/pybamm-team/PyBaMM/blob/develop/CONTRIBUTING.md#installing-and-using-pre-commit) for how to set this up to run automatically when committing locally, in just two lines of code)
- [ ] All tests pass: `$ python -m pytest` (or `$ nox -s tests`)
- [ ] The documentation builds: `$ python -m pytest --doctest-plus src` (or `$ nox -s doctests`)

You can run integration tests, unit tests, and doctests together at once, using `$ nox -s quick`.

## Further checks:

- [ ] Code is commented, particularly in hard-to-understand areas
- [ ] Tests added that prove fix is effective or that feature works


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pybamm-team/PyBaMM/pull/4798?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 98.69%. Comparing base [(`d4e884d`)](https://app.codecov.io/gh/pybamm-team/PyBaMM/commit/d4e884d6219b4b75fef2a236b57a13d78009e769?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team) to head [(`540bb5e`)](https://app.codecov.io/gh/pybamm-team/PyBaMM/commit/540bb5ea4715d618cfdf4dd43d9e64d515ec876a?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team).
> Report is 2 commits behind head on develop.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff            @@
##           develop    #4798   +/-   ##
========================================
  Coverage    98.69%   98.69%           
========================================
  Files          303      303           
  Lines        23256    23258    +2     
========================================
+ Hits         22953    22955    +2     
  Misses         303      303           
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pybamm-team/PyBaMM/pull/4798?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team).


Patch:
diff --git a/src/pybamm/solvers/summary_variable.py b/src/pybamm/solvers/summary_variable.py
index 812dea72d..c293ebd62 100644
--- a/src/pybamm/solvers/summary_variable.py
+++ b/src/pybamm/solvers/summary_variable.py
@@ -41,7 +41,7 @@ class SummaryVariables:
         self.user_inputs = user_inputs or {}
         self.esoh_solver = esoh_solver
         self._variables = {}  # Store computed variables
-        self.cycle_number = None
+        self.cycle_number = np.array([])
 
         model = solution.all_models[0]
         self._possible_variables = model.summary_variables  # minus esoh variables
@@ -122,6 +122,8 @@ class SummaryVariables:
         if key in self._variables:
             # return it if it exists
             return self._variables[key]
+        elif key == "Cycle number":
+            return self.cycle_number
         elif key not in self.all_variables:
             # check it's listed as a summary variable
             raise KeyError(f"Variable '{key}' is not a summary variable.")


Test patch:
diff --git a/tests/unit/test_solvers/test_summary_variables.py b/tests/unit/test_solvers/test_summary_variables.py
index 0132c42ea..c8c3ed194 100644
--- a/tests/unit/test_solvers/test_summary_variables.py
+++ b/tests/unit/test_solvers/test_summary_variables.py
@@ -167,5 +167,7 @@ class TestSummaryVariables:
         sol = sim.solve()
 
         assert len(sol.summary_variables.cycles) == 10
+        assert sol.summary_variables["Cycle number"][0] == 1
+        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)