Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre commit and migrate to ruff #232

Merged
merged 4 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .flake8

This file was deleted.

2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# activated pre-commit
7fb3b8f437d608fbea8edab417d2bba10b58bcc0
7 changes: 3 additions & 4 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ jobs:
with:
python-version: 3.8

- name: Lint with flake8
- name: Lint with ruff
run: |
python -m pip install --upgrade pip
python -m pip install flake8
flake8
python -m pip install --upgrade pip pre-commit
pre-commit run ruff

pip-build:
needs: style
Expand Down
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
ci:
autoupdate_commit_msg: "chore: update pre-commit hooks"
autofix_commit_msg: "style: pre-commit fixes"

repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.254"
hooks:
- id: ruff
args: [--ignore=E741, --exclude=__init__.py]
3 changes: 0 additions & 3 deletions benchmarks/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def time_solve_model(self):


class SmallPack:

timeout = 60

def setup(self):
Expand Down Expand Up @@ -47,7 +46,6 @@ def time_discharge_2cpu(self):


class MediumPack:

timeout = 120

def setup(self):
Expand Down Expand Up @@ -80,7 +78,6 @@ def time_discharge_2cpu(self):


class LargePack:

timeout = 600

def setup(self):
Expand Down
35 changes: 28 additions & 7 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ If you're already familiar with our workflow, maybe have a quick look at the [pr

Fork the repository and create a pull request. Github actions should check that tests are passing.

### Installing and using pre-commit

`liionpack` uses a set of `pre-commit` hooks and the `pre-commit` bot to format and prettify the codebase. The hooks can be installed locally using -

```bash
pip install pre-commit
pre-commit install
```

This would run the checks every time a commit is created locally. The checks will only run on the files modified by that commit, but the checks can be triggered for all the files using -

```bash
pre-commit run --all-files
```

If you would like to skip the failing checks and push the code for further discussion, use the `--no-verify` option with `git commit`.

## Workflow

We use [GIT](https://en.wikipedia.org/wiki/Git) and [GitHub](https://en.wikipedia.org/wiki/GitHub) to coordinate our work. When making any kind of update, we try to follow the procedure below.
Expand Down Expand Up @@ -44,14 +61,18 @@ You now have everything you need to start making changes!

liionpack follows the [PEP8 recommendations](https://www.python.org/dev/peps/pep-0008/) for coding style. These are very common guidelines, and community tools have been developed to check how well projects implement them.

### Flake8
### Ruff

We use [flake8](http://flake8.pycqa.org/en/latest/) to check our PEP8 adherence. To try this on your system, navigate to the liionpack directory in a console and type
We use [ruff](https://github.com/charliermarsh/ruff) to check our PEP8 adherence. To try this on your system, navigate to the liionpack directory in a console and type

```bash
flake8
python -m pip install pre-commit
pre-commit run ruff
```

ruff is configured inside the file `pre-commit-config.yaml`, allowing us to ignore some errors. If you think this should be added or removed, please submit an [issue](#issues)

When you commit your changes they will be checked against ruff automatically (see [infrastructure](#infrastructure)).

### Black

Expand All @@ -67,7 +88,7 @@ black {source_file_or_directory}

If you want to use black in your editor, you may need to change the max line length in your editor settings.

Even when code has been formatted by black, you should still make sure that it adheres to the PEP8 standard set by [Flake8](#flake8).
Even when code has been formatted by black, you should still make sure that it adheres to the PEP8 standard set by [Ruff](#ruff).

### Naming

Expand Down Expand Up @@ -173,7 +194,7 @@ wherever code is called that uses that citation (for example, in functions or in

## Benchmarks

A benchmark suite is located in the `benchmarks` directory at the root of the PyBaMM project. These benchmarks can be run using [airspeed velocity](https://asv.readthedocs.io/en/stable/) (`asv`).
A benchmark suite is located in the `benchmarks` directory at the root of the liionpack project. These benchmarks can be run using [airspeed velocity](https://asv.readthedocs.io/en/stable/) (`asv`).

### Running the benchmarks
First of all, you'll need `asv` installed:
Expand All @@ -198,7 +219,7 @@ asv run commit_ID..develop
```
Further information on how to run benchmarks with `asv` can be found in the documentation at [Using airspeed velocity](https://asv.readthedocs.io/en/stable/using.html).

`asv` is configured using a file `asv.conf.json` located at the root of the PyBaMM repository. See the [asv reference](https://asv.readthedocs.io/en/stable/reference.html) for details on available settings and options.
`asv` is configured using a file `asv.conf.json` located at the root of the liionpack repository. See the [asv reference](https://asv.readthedocs.io/en/stable/reference.html) for details on available settings and options.

Benchmark results are stored in a directory `results/` at the location of the configuration file. There is one result file per commit, per machine.

Expand All @@ -214,7 +235,7 @@ then, to view the website:
asv preview
```

Current benchmarks over PyBaMM's history can be viewed at https://pybamm-team.github.io/liionpack-bench/
Current benchmarks over liionpack's history can be viewed at https://pybamm-team.github.io/liionpack-bench/

### Adding benchmarks

Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies:
- ipython
- jupyter
- sympy
- flake8
- ruff
- pip
- pip:
- pybamm>=23.2
Expand Down
40 changes: 22 additions & 18 deletions examples/basic_16p2s.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import os

lp.set_logging_level('NOTICE')
lp.set_logging_level("NOTICE")

# Define parameters
Np = 16
Expand All @@ -19,31 +19,35 @@
netlist = lp.setup_circuit(Np=Np, Ns=Ns)

# Define additional output variables
output_variables = [
'Volume-averaged cell temperature [K]']
output_variables = ["Volume-averaged cell temperature [K]"]

# Define a cycling experiment using PyBaMM
experiment = pybamm.Experiment([
f'Charge at {Iapp} A for 30 minutes',
'Rest for 15 minutes',
f'Discharge at {Iapp} A for 30 minutes',
'Rest for 30 minutes'],
period='10 seconds')
experiment = pybamm.Experiment(
[
f"Charge at {Iapp} A for 30 minutes",
"Rest for 15 minutes",
f"Discharge at {Iapp} A for 30 minutes",
"Rest for 30 minutes",
],
period="10 seconds",
)

# Define the PyBaMM parameters
parameter_values = pybamm.ParameterValues("Chen2020")
inputs = {"Total heat transfer coefficient [W.m-2.K-1]": np.ones(Np * Ns) * 10}

# Solve the pack
output = lp.solve(netlist=netlist,
sim_func=lp.thermal_simulation,
parameter_values=parameter_values,
experiment=experiment,
output_variables=output_variables,
initial_soc=0.5,
inputs=inputs,
nproc=os.cpu_count(),
manager='casadi')
output = lp.solve(
netlist=netlist,
sim_func=lp.thermal_simulation,
parameter_values=parameter_values,
experiment=experiment,
output_variables=output_variables,
initial_soc=0.5,
inputs=inputs,
nproc=os.cpu_count(),
manager="casadi",
)

# Plot the pack and individual cell results
lp.plot_pack(output)
Expand Down
4 changes: 2 additions & 2 deletions examples/big_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import os
import pickle

lp.log_to_file('logger_info')
lp.log_to_file("logger_info")


if __name__ == "__main__":
Expand Down Expand Up @@ -41,5 +41,5 @@
nproc=os.cpu_count(),
)

with open('output.pickle', 'wb') as handle:
with open("output.pickle", "wb") as handle:
pickle.dump(output, handle, protocol=pickle.HIGHEST_PROTOCOL)
6 changes: 4 additions & 2 deletions examples/drive_cycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
experiment = pybamm.Experiment(
operating_conditions=["Run US06 (A)"],
period="1 minute",
drive_cycles={"US06": drive_cycle})
drive_cycles={"US06": drive_cycle},
)

# PyBaMM parameters
parameter_values = pybamm.ParameterValues("Chen2020")
Expand All @@ -37,7 +38,8 @@
netlist=netlist,
parameter_values=parameter_values,
experiment=experiment,
initial_soc=0.5)
initial_soc=0.5,
)

# Plot results
lp.plot_output(output)
Expand Down
8 changes: 5 additions & 3 deletions examples/drive_cycle_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@
experiment=experiment,
output_variables=output_variables,
initial_soc=init_SoC,
manager='casadi',
nproc=8)
manager="casadi",
nproc=8,
)

parameter_values = pybamm.ParameterValues("Chen2020")

sim = pybamm.Simulation(
model=pybamm.lithium_ion.SPM(),
experiment=experiment,
parameter_values=parameter_values)
parameter_values=parameter_values,
)

sol = sim.solve(initial_soc=init_SoC)

Expand Down
31 changes: 18 additions & 13 deletions examples/paper_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,32 @@

# Define some additional variables to output
output_variables = [
'X-averaged negative particle surface concentration [mol.m-3]',
'X-averaged positive particle surface concentration [mol.m-3]',
"X-averaged negative particle surface concentration [mol.m-3]",
"X-averaged positive particle surface concentration [mol.m-3]",
]

# Cycling experiment, using PyBaMM
experiment = pybamm.Experiment([
"Charge at 5 A for 30 minutes",
"Rest for 15 minutes",
"Discharge at 5 A for 30 minutes",
"Rest for 30 minutes"],
period="10 seconds")
experiment = pybamm.Experiment(
[
"Charge at 5 A for 30 minutes",
"Rest for 15 minutes",
"Discharge at 5 A for 30 minutes",
"Rest for 30 minutes",
],
period="10 seconds",
)

# PyBaMM battery parameters
parameter_values = pybamm.ParameterValues("Chen2020")

# Solve the pack problem
output = lp.solve(netlist=netlist,
parameter_values=parameter_values,
experiment=experiment,
output_variables=output_variables,
initial_soc=0.5)
output = lp.solve(
netlist=netlist,
parameter_values=parameter_values,
experiment=experiment,
output_variables=output_variables,
initial_soc=0.5,
)

# Display the results
lp.plot_output(output, color="white")
Expand Down
37 changes: 21 additions & 16 deletions examples/save_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,36 @@
netlist = lp.setup_circuit(Np=Np, Ns=Ns)

# Define additional output variables
output_variables = ['Volume-averaged cell temperature [K]']
output_variables = ["Volume-averaged cell temperature [K]"]

# Define a cycling experiment using PyBaMM
experiment = pybamm.Experiment([
f'Charge at {Iapp} A for 30 minutes',
'Rest for 15 minutes',
f'Discharge at {Iapp} A for 30 minutes',
'Rest for 30 minutes'],
period='10 seconds')
experiment = pybamm.Experiment(
[
f"Charge at {Iapp} A for 30 minutes",
"Rest for 15 minutes",
f"Discharge at {Iapp} A for 30 minutes",
"Rest for 30 minutes",
],
period="10 seconds",
)

# Define the PyBaMM parameters
chemistry = pybamm.parameter_sets.Chen2020
parameter_values = pybamm.ParameterValues(chemistry=chemistry)
inputs = {"Total heat transfer coefficient [W.m-2.K-1]": np.ones(Np * Ns) * 10}

# Solve the pack
output = lp.solve(netlist=netlist,
sim_func=lp.thermal_simulation,
parameter_values=parameter_values,
experiment=experiment,
output_variables=output_variables,
initial_soc=0.5,
inputs=inputs,
nproc=os.cpu_count(),
manager='casadi')
output = lp.solve(
netlist=netlist,
sim_func=lp.thermal_simulation,
parameter_values=parameter_values,
experiment=experiment,
output_variables=output_variables,
initial_soc=0.5,
inputs=inputs,
nproc=os.cpu_count(),
manager="casadi",
)

# Save simulation output to CSV files
lp.save_to_csv(output)
Expand Down
2 changes: 1 addition & 1 deletion examples/step_external_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
inputs=inputs,
nproc=2,
initial_soc=0.5,
setup_only=True
setup_only=True,
)


Expand Down
Loading