From 7fb3b8f437d608fbea8edab417d2bba10b58bcc0 Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Tue, 7 Mar 2023 23:15:00 +0530 Subject: [PATCH 1/3] Add pre-commit and ruff --- .flake8 | 3 -- .pre-commit-config.yaml | 15 ++++++++++ benchmarks/benchmarks.py | 3 -- examples/basic_16p2s.py | 40 +++++++++++++++------------ examples/big_circuit.py | 4 +-- examples/drive_cycle.py | 6 ++-- examples/drive_cycle_comparison.py | 8 ++++-- examples/paper_example.py | 31 ++++++++++++--------- examples/save_output.py | 37 ++++++++++++++----------- examples/step_external_variable.py | 2 +- examples/thermal_external.py | 25 +++++++++-------- liionpack/netlist_utils.py | 4 +-- liionpack/protocols.py | 2 -- liionpack/solvers.py | 10 ++----- liionpack/utils.py | 14 +++++----- tests/integration/test_1p1s.py | 22 ++++++++------- tests/integration/test_all_solvers.py | 4 +-- tests/unit/test_netlist_utils.py | 2 +- tests/unit/test_notebooks.py | 1 - tests/unit/test_simulations.py | 5 ++-- tests/unit/test_utils.py | 37 ++++++++++++------------- 21 files changed, 149 insertions(+), 126 deletions(-) delete mode 100644 .flake8 create mode 100644 .pre-commit-config.yaml diff --git a/.flake8 b/.flake8 deleted file mode 100644 index 5c1a3fb8..00000000 --- a/.flake8 +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -max-line-length = 89 -ignore = F403 F401 W503 E741 E203 W605 \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..d35257c9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -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] diff --git a/benchmarks/benchmarks.py b/benchmarks/benchmarks.py index b9f02b2a..3139ffd5 100644 --- a/benchmarks/benchmarks.py +++ b/benchmarks/benchmarks.py @@ -14,7 +14,6 @@ def time_solve_model(self): class SmallPack: - timeout = 60 def setup(self): @@ -47,7 +46,6 @@ def time_discharge_2cpu(self): class MediumPack: - timeout = 120 def setup(self): @@ -80,7 +78,6 @@ def time_discharge_2cpu(self): class LargePack: - timeout = 600 def setup(self): diff --git a/examples/basic_16p2s.py b/examples/basic_16p2s.py index de25cb9c..5199d827 100644 --- a/examples/basic_16p2s.py +++ b/examples/basic_16p2s.py @@ -8,7 +8,7 @@ import numpy as np import os -lp.set_logging_level('NOTICE') +lp.set_logging_level("NOTICE") # Define parameters Np = 16 @@ -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) diff --git a/examples/big_circuit.py b/examples/big_circuit.py index d6917136..6c9a0d36 100644 --- a/examples/big_circuit.py +++ b/examples/big_circuit.py @@ -7,7 +7,7 @@ import os import pickle -lp.log_to_file('logger_info') +lp.log_to_file("logger_info") if __name__ == "__main__": @@ -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) diff --git a/examples/drive_cycle.py b/examples/drive_cycle.py index 166fed35..d419d8ca 100644 --- a/examples/drive_cycle.py +++ b/examples/drive_cycle.py @@ -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") @@ -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) diff --git a/examples/drive_cycle_comparison.py b/examples/drive_cycle_comparison.py index 03bb4642..6b5a9d6f 100644 --- a/examples/drive_cycle_comparison.py +++ b/examples/drive_cycle_comparison.py @@ -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) diff --git a/examples/paper_example.py b/examples/paper_example.py index 09521962..31b8b998 100644 --- a/examples/paper_example.py +++ b/examples/paper_example.py @@ -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") diff --git a/examples/save_output.py b/examples/save_output.py index 414d8f32..8647a4f4 100644 --- a/examples/save_output.py +++ b/examples/save_output.py @@ -17,15 +17,18 @@ 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 @@ -33,15 +36,17 @@ 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) diff --git a/examples/step_external_variable.py b/examples/step_external_variable.py index 229d6eaa..8bb4c8ca 100644 --- a/examples/step_external_variable.py +++ b/examples/step_external_variable.py @@ -53,7 +53,7 @@ inputs=inputs, nproc=2, initial_soc=0.5, - setup_only=True + setup_only=True, ) diff --git a/examples/thermal_external.py b/examples/thermal_external.py index 79c6b703..0ae9f644 100644 --- a/examples/thermal_external.py +++ b/examples/thermal_external.py @@ -6,7 +6,8 @@ import pybamm import numpy as np import matplotlib.pyplot as plt -plt.close('all') + +plt.close("all") # Generate the netlist @@ -15,13 +16,11 @@ # Define some additional variables to output output_variables = [ "Volume-averaged cell temperature [K]", - "Volume-averaged total heating [W.m-3]" + "Volume-averaged total heating [W.m-3]", ] # Cycling experiment, using PyBaMM -experiment = pybamm.Experiment([ - "Discharge at 5 A for 5 minutes"], - period="10 seconds") +experiment = pybamm.Experiment(["Discharge at 5 A for 5 minutes"], period="10 seconds") # PyBaMM battery parameters parameter_values = pybamm.ParameterValues("Chen2020") @@ -29,13 +28,15 @@ # Solve the pack problem temps = np.ones(4) * 300 + np.arange(4) * 10 inputs = {"Input temperature [K]": temps} -output = lp.solve(netlist=netlist, - sim_func=lp.thermal_external, - inputs=inputs, - parameter_values=parameter_values, - experiment=experiment, - output_variables=output_variables, - initial_soc=0.5) +output = lp.solve( + netlist=netlist, + sim_func=lp.thermal_external, + inputs=inputs, + parameter_values=parameter_values, + experiment=experiment, + output_variables=output_variables, + initial_soc=0.5, +) # Display the results lp.plot_output(output, color="white") diff --git a/liionpack/netlist_utils.py b/liionpack/netlist_utils.py index 3cb2a6fe..63f2ea9a 100644 --- a/liionpack/netlist_utils.py +++ b/liionpack/netlist_utils.py @@ -114,7 +114,7 @@ def setup_circuit( V=4.2, plot=False, terminals="left", - configuration="parallel-strings" + configuration="parallel-strings", ): """ Define a netlist from a number of batteries in parallel and series @@ -757,7 +757,7 @@ def write_netlist(netlist, filename): """ lines = ["* " + filename] - for (i, r) in netlist.iterrows(): + for i, r in netlist.iterrows(): line = r.desc + " " + _fn(r.node1) + " " + _fn(r.node2) + " " + str(r.value) lines.append(line) lines.append(".op") diff --git a/liionpack/protocols.py b/liionpack/protocols.py index fe7bfbe9..a9df22c6 100644 --- a/liionpack/protocols.py +++ b/liionpack/protocols.py @@ -2,8 +2,6 @@ # Experimental protocol # -import numpy as np - def generate_protocol_from_experiment(experiment, flatten=True): """ diff --git a/liionpack/solvers.py b/liionpack/solvers.py index b2f79c27..976a2470 100644 --- a/liionpack/solvers.py +++ b/liionpack/solvers.py @@ -282,7 +282,7 @@ def __init__( # "Total stepping time " + str(np.around(toc - sim_start_time, 3)) + "s" # ) # lp.logger.notice( - # "Time per step " + str(np.around((toc - sim_start_time) / Nsteps, 3)) + "s" + # "Time per step " + str(np.around((toc - sim_start_time) / Nsteps, 3)) + "s" # ) # return self.all_output @@ -351,9 +351,7 @@ def solve( # Handle the inputs self.inputs = inputs - self.inputs_dict = lp.build_inputs_dict( - self.shm_i_app[0, :], self.inputs, None - ) + self.inputs_dict = lp.build_inputs_dict(self.shm_i_app[0, :], self.inputs, None) # Solver specific setup self.setup_actors(nproc, self.inputs_dict, initial_soc) # Get the initial state of the system @@ -434,9 +432,7 @@ def _step(self, step, updated_inputs): # for the next step I_app = I_batt[:] * -1 self.shm_i_app[step + 1, :] = I_app - self.inputs_dict = lp.build_inputs_dict( - I_app, self.inputs, updated_inputs - ) + self.inputs_dict = lp.build_inputs_dict(I_app, self.inputs, updated_inputs) # 06 Check if voltage limits are reached and terminate if np.any(temp_v < self.v_cut_lower): lp.logger.warning("Low voltage limit reached") diff --git a/liionpack/utils.py b/liionpack/utils.py index a7e88494..4e637502 100644 --- a/liionpack/utils.py +++ b/liionpack/utils.py @@ -102,7 +102,7 @@ def add_events_to_model(model): return model -def save_to_csv(output, path='./csv-results'): +def save_to_csv(output, path="./csv-results"): """ Save simulation output to a CSV file for each output variable. @@ -126,11 +126,11 @@ def save_to_csv(output, path='./csv-results'): # Save simulation output to CSV files for k, v in output.items(): - filename = k.replace(' ', '_') + '.csv' - np.savetxt(path / filename, v, delimiter=', ') + filename = k.replace(" ", "_") + ".csv" + np.savetxt(path / filename, v, delimiter=", ") -def save_to_npy(output, path='./npy-results'): +def save_to_npy(output, path="./npy-results"): """ Save simulation output to NumPy `.npy` files where each file represents an output variable. @@ -155,11 +155,11 @@ def save_to_npy(output, path='./npy-results'): # Save simulation output to npy files for k, v in output.items(): - filename = k.replace(' ', '_') + '.npy' + filename = k.replace(" ", "_") + ".npy" np.save(path / filename, v) -def save_to_npzcomp(output, path='.'): +def save_to_npzcomp(output, path="."): """ Save simulation output to a compressed NumPy `output.npz` file. The saved file is a dictionary-like object where each key represents a simulation @@ -185,5 +185,5 @@ def save_to_npzcomp(output, path='.'): path.mkdir(exist_ok=True) # Save simulation output to a compressed npz file - filename = 'output.npz' + filename = "output.npz" np.savez_compressed(path / filename, **output) diff --git a/tests/integration/test_1p1s.py b/tests/integration/test_1p1s.py index b87d9b5b..e00ae423 100644 --- a/tests/integration/test_1p1s.py +++ b/tests/integration/test_1p1s.py @@ -21,15 +21,15 @@ def test_consistent_results_1_step(self): ) # Solve pack output = lp.solve( - netlist=netlist, - parameter_values=parameter_values, - experiment=experiment + netlist=netlist, parameter_values=parameter_values, experiment=experiment ) parameter_values = pybamm.ParameterValues("Chen2020") - sim = pybamm.Simulation(model=pybamm.lithium_ion.SPM(), - parameter_values=parameter_values, - experiment=experiment) + sim = pybamm.Simulation( + model=pybamm.lithium_ion.SPM(), + parameter_values=parameter_values, + experiment=experiment, + ) sol = sim.solve() a = output["Terminal voltage [V]"].flatten() @@ -61,13 +61,15 @@ def test_consistent_results_2_step(self): netlist=netlist, parameter_values=parameter_values, experiment=experiment, - initial_soc=SoC + initial_soc=SoC, ) parameter_values = pybamm.ParameterValues("Chen2020") - sim = pybamm.Simulation(model=pybamm.lithium_ion.SPM(), - parameter_values=parameter_values, - experiment=experiment) + sim = pybamm.Simulation( + model=pybamm.lithium_ion.SPM(), + parameter_values=parameter_values, + experiment=experiment, + ) sol = sim.solve(initial_soc=SoC) a = output["Terminal voltage [V]"].flatten() diff --git a/tests/integration/test_all_solvers.py b/tests/integration/test_all_solvers.py index 3b55e1f3..a0e29b7e 100644 --- a/tests/integration/test_all_solvers.py +++ b/tests/integration/test_all_solvers.py @@ -27,7 +27,7 @@ def test_consistent_results_1_step(self): experiment=experiment, inputs=None, nproc=1, - manager="casadi" + manager="casadi", ) # Solve pack with ray b = lp.solve( @@ -36,7 +36,7 @@ def test_consistent_results_1_step(self): experiment=experiment, inputs=None, nproc=1, - manager="ray" + manager="ray", ) v_a = a["Terminal voltage [V]"] diff --git a/tests/unit/test_netlist_utils.py b/tests/unit/test_netlist_utils.py index a33d6df4..24aad9ac 100644 --- a/tests/unit/test_netlist_utils.py +++ b/tests/unit/test_netlist_utils.py @@ -137,7 +137,7 @@ def test_lcapy_circuit(self): def test_write_netlist(self): net = lp.setup_circuit(Np=1, Ns=2, Rb=1e-4, Rc=1e-2, Ri=1e-3, V=2.0, I=10.0) cwd = os.getcwd() - temp = os.path.join(cwd, 'temp.txt') + temp = os.path.join(cwd, "temp.txt") lp.write_netlist(net, temp) assert os.path.isfile(temp) os.remove(temp) diff --git a/tests/unit/test_notebooks.py b/tests/unit/test_notebooks.py index ef7deba5..287fbc76 100644 --- a/tests/unit/test_notebooks.py +++ b/tests/unit/test_notebooks.py @@ -14,7 +14,6 @@ class TestNotebooks(unittest.TestCase): def test_notebooks(self): examples_folder = os.path.join(lp.ROOT_DIR, "docs", "examples") for filename in os.listdir(examples_folder): - if os.path.splitext(filename)[1] == ".ipynb": print("-" * 80) print("Testing notebook:", filename) diff --git a/tests/unit/test_simulations.py b/tests/unit/test_simulations.py index c4234bf7..cadb4255 100644 --- a/tests/unit/test_simulations.py +++ b/tests/unit/test_simulations.py @@ -11,8 +11,9 @@ def test_basic_simulation(self): def test_thermal_simulation(self): sim = lp.thermal_simulation() - sim.solve([0, 1800], - inputs={"Total heat transfer coefficient [W.m-2.K-1]": 1.0}) + sim.solve( + [0, 1800], inputs={"Total heat transfer coefficient [W.m-2.K-1]": 1.0} + ) assert sim.__class__ == pybamm.Simulation # def test_thermal_external(self): diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 76daa70a..e94746de 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -7,11 +7,10 @@ class utilsTest(unittest.TestCase): - def setUp(self): currents = [4, 5.2, 8, 10] volts = np.array([[3.5, 3.6], [3.54, 3.58]]) - output = {'currents': currents, 'volts': volts} + output = {"currents": currents, "volts": volts} self.currents = currents self.volts = volts @@ -41,50 +40,50 @@ def test_build_inputs_dict(self): assert len(in_dict) == 2 def test_save_to_csv(self): - lp.save_to_csv(self.output, path='.') + lp.save_to_csv(self.output, path=".") - with open('currents.csv', 'r') as f: + with open("currents.csv", "r") as f: current = float(f.readline()) self.assertEqual(self.currents[0], current) - with open('volts.csv', 'r') as f: - volts = list(map(float, f.readline().split(', '))) + with open("volts.csv", "r") as f: + volts = list(map(float, f.readline().split(", "))) self.assertEqual(self.volts[0][0], volts[0]) def test_save_to_npy(self): - lp.save_to_npy(self.output, path='.') + lp.save_to_npy(self.output, path=".") - currents = np.load('currents.npy') + currents = np.load("currents.npy") self.assertEqual(self.currents[0], currents[0]) - volts = np.load('volts.npy') + volts = np.load("volts.npy") self.assertEqual(self.volts[0, 0], volts[0, 0]) def test_save_to_npz(self): - lp.save_to_npzcomp(self.output, path='.') + lp.save_to_npzcomp(self.output, path=".") - output = np.load('output.npz') - currents = output['currents'] + output = np.load("output.npz") + currents = output["currents"] self.assertEqual(self.currents[0], currents[0]) - output = np.load('output.npz') - volts = output['volts'] + output = np.load("output.npz") + volts = output["volts"] self.assertEqual(self.volts[0, 0], volts[0, 0]) def tearDown(self): - path = pathlib.Path('currents.csv') + path = pathlib.Path("currents.csv") path.unlink(missing_ok=True) - path = pathlib.Path('volts.csv') + path = pathlib.Path("volts.csv") path.unlink(missing_ok=True) - path = pathlib.Path('currents.npy') + path = pathlib.Path("currents.npy") path.unlink(missing_ok=True) - path = pathlib.Path('volts.npy') + path = pathlib.Path("volts.npy") path.unlink(missing_ok=True) - path = pathlib.Path('output.npz') + path = pathlib.Path("output.npz") path.unlink(missing_ok=True) From f8c03ee1594e0134522bbd0fbb1321359d449735 Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Tue, 7 Mar 2023 23:18:19 +0530 Subject: [PATCH 2/3] Update docs + preserve git blame --- .git-blame-ignore-revs | 2 ++ .github/workflows/test_on_push.yml | 7 +++---- docs/contributing.md | 31 +++++++++++++++++++++++++----- environment.yml | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 .git-blame-ignore-revs diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 00000000..2afd1142 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# activated pre-commit +7fb3b8f437d608fbea8edab417d2bba10b58bcc0 diff --git a/.github/workflows/test_on_push.yml b/.github/workflows/test_on_push.yml index c08d1c58..221f852a 100644 --- a/.github/workflows/test_on_push.yml +++ b/.github/workflows/test_on_push.yml @@ -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 diff --git a/docs/contributing.md b/docs/contributing.md index 419a3592..19b9051c 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -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 + +`PyBaMM` 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. @@ -44,20 +61,24 @@ 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 PyBaMM 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 We use [black](https://black.readthedocs.io/en/stable/) to automatically configure our code to adhere to PEP8. Black can be used in two ways: -1. Command line: navigate to the liionpack directory in a console and type +1. Command line: navigate to the PyBaMM directory in a console and type ```bash black {source_file_or_directory} @@ -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 diff --git a/environment.yml b/environment.yml index 938fdb0b..93bb2a6f 100644 --- a/environment.yml +++ b/environment.yml @@ -27,7 +27,7 @@ dependencies: - ipython - jupyter - sympy - - flake8 + - ruff - pip - pip: - pybamm>=23.2 From c574babafee3d79f45ad4dd66c728bd08cf3d51f Mon Sep 17 00:00:00 2001 From: Saransh Chopra Date: Wed, 8 Mar 2023 00:05:17 +0530 Subject: [PATCH 3/3] PyBaMM -> liionpack --- docs/contributing.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/docs/contributing.md b/docs/contributing.md index 19b9051c..afd6ffce 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -10,7 +10,7 @@ Fork the repository and create a pull request. Github actions should check that ### Installing and using pre-commit -`PyBaMM` 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 - +`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 @@ -63,7 +63,7 @@ liionpack follows the [PEP8 recommendations](https://www.python.org/dev/peps/pep ### Ruff -We use [ruff](https://github.com/charliermarsh/ruff) to check our PEP8 adherence. To try this on your system, navigate to the PyBaMM 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 python -m pip install pre-commit @@ -78,7 +78,7 @@ When you commit your changes they will be checked against ruff automatically (se We use [black](https://black.readthedocs.io/en/stable/) to automatically configure our code to adhere to PEP8. Black can be used in two ways: -1. Command line: navigate to the PyBaMM directory in a console and type +1. Command line: navigate to the liionpack directory in a console and type ```bash black {source_file_or_directory} @@ -194,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: @@ -219,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. @@ -235,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