Skip to content

Commit feb5f38

Browse files
introduce pre-commit (#38)
* add pre-commit dep * add ruff hook * format calculations.py * format all files * remove static analysis CI * remove fmt env and ruff feature
1 parent ea47f4e commit feb5f38

7 files changed

Lines changed: 34 additions & 55 deletions

File tree

.github/workflows/ci.yml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@ on:
66

77
jobs:
88

9-
static-analysis:
10-
name: Perform static analysis
11-
runs-on: ubuntu-latest
12-
timeout-minutes: 15
13-
steps:
14-
- name: Checkout
15-
uses: actions/checkout@v4
16-
- name: Set up Pixi
17-
uses: prefix-dev/setup-pixi@v0.8.1
18-
with:
19-
pixi-version: v0.40.2
20-
cache: false
21-
environments: fmt
22-
activate-environment: true
23-
- name: Run formatter and linter
24-
run: pixi run fmt
25-
269
docs:
2710
name: Generate documentation
2811
runs-on: ubuntu-latest

.pre-commit-config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
repos:
2+
- repo: https://github.com/astral-sh/ruff-pre-commit
3+
# Ruff version.
4+
rev: v0.12.0
5+
hooks:
6+
# Run the linter.
7+
- id: ruff-check
8+
args: [ --fix ]
9+
# Run the formatter.
10+
- id: ruff-format

pyproject.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ platforms = ["linux-64"]
4444
### pixi: features
4545
[tool.pixi.feature.py3]
4646
dependencies = {python = "3.*"}
47-
[tool.pixi.feature.ruff]
48-
dependencies = {ruff = "*"}
49-
tasks = {fmt = "ruff check"}
5047
[tool.pixi.feature.build]
5148
pypi-dependencies = {build = "*"}
5249
tasks = {build-dist = "python -m build"}
@@ -63,12 +60,12 @@ dependencies = {fans = "0.4.*"}
6360
[tool.pixi.dependencies]
6461
aiida-core = "2.6.*"
6562
h5py = "*"
63+
pre-commit = "*"
6664
[tool.pixi.pypi-dependencies]
6765
aiida-fans = { path = ".", editable = true }
6866

6967
### pixi: environments
7068
[tool.pixi.environments]
71-
fmt = { no-default-feature = true, features = ["py3", "ruff"] } # CI env
7269
dist = { no-default-feature = true, features = ["py3", "build"] } # CI env
7370
docs = { no-default-feature = true, features = ["py3", "sphinx"] } # CI env
7471
tutorial = { features = ["marimo", "fans"] }

src/aiida_fans/calculations.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ def define(cls, spec: CalcJobProcessSpec) -> None:
2626
spec.inputs["metadata"]["label"].default = "FANS"
2727
## Processing Power
2828
spec.inputs["metadata"]["options"]["withmpi"].default = True
29-
spec.inputs["metadata"]["options"]["resources"].default = {
30-
"num_machines": 1,
31-
"num_mpiprocs_per_machine": 4
32-
}
29+
spec.inputs["metadata"]["options"]["resources"].default = {"num_machines": 1, "num_mpiprocs_per_machine": 4}
3330
## Filenames
3431
spec.inputs["metadata"]["options"]["input_filename"].default = "input.json"
3532
spec.inputs["metadata"]["options"]["output_filename"].default = "output.h5"
@@ -72,14 +69,16 @@ def prepare_for_submission(self, folder: Folder) -> CalcInfo:
7269
"""Prepare the calculation for submission."""
7370
# Stashed Strategy:
7471
if self.options.stashed_microstructure:
75-
ms_filepath: Path = Path(self.inputs.code.computer.get_workdir()) / \
76-
"stash/microstructures" / \
77-
self.inputs.microstructure.file.filename
72+
ms_filepath: Path = (
73+
Path(self.inputs.code.computer.get_workdir())
74+
/ "stash/microstructures"
75+
/ self.inputs.microstructure.file.filename
76+
)
7877
# if microstructure does not exist in stash, make it
7978
if not ms_filepath.is_file():
8079
ms_filepath.parent.mkdir(parents=True, exist_ok=True)
81-
with self.inputs.microstructure.file.open(mode='rb') as source:
82-
with ms_filepath.open(mode='wb') as target:
80+
with self.inputs.microstructure.file.open(mode="rb") as source:
81+
with ms_filepath.open(mode="wb") as target:
8382
copyfileobj(source, target)
8483

8584
# input.json as dict
@@ -90,11 +89,11 @@ def prepare_for_submission(self, folder: Folder) -> CalcInfo:
9089
dump(input_dict, json, indent=4)
9190
# Fragmented Strategy:
9291
else:
93-
datasetname : str = self.inputs.microstructure.datasetname.value
94-
with folder.open("microstructure.h5","bw") as f_dest:
95-
with h5File(f_dest,"w") as h5_dest:
92+
datasetname: str = self.inputs.microstructure.datasetname.value
93+
with folder.open("microstructure.h5", "bw") as f_dest:
94+
with h5File(f_dest, "w") as h5_dest:
9695
with self.inputs.microstructure.file.open(mode="rb") as f_src:
97-
with h5File(f_src,'r') as h5_src:
96+
with h5File(f_src, "r") as h5_src:
9897
h5_src.copy(datasetname, h5_dest, name=datasetname)
9998

10099
# input.json as dict
@@ -117,8 +116,6 @@ def prepare_for_submission(self, folder: Folder) -> CalcInfo:
117116
calcinfo.local_copy_list = []
118117
calcinfo.remote_copy_list = []
119118
calcinfo.retrieve_list = [codeinfo.stdout_name, codeinfo.stderr_name]
120-
calcinfo.retrieve_temporary_list = [
121-
self.options.output_filename
122-
]
119+
calcinfo.retrieve_temporary_list = [self.options.output_filename]
123120

124121
return calcinfo

src/aiida_fans/helpers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def make_input_dict(job: CalcJob) -> dict[str, Any]:
1111
return {
1212
## Microstructure Definition
1313
"microstructure": {
14-
"filepath": None, # path to stashed microstructure, must be overwritten by impl
14+
"filepath": None, # path to stashed microstructure, must be overwritten by impl
1515
"datasetname": job.inputs.microstructure.datasetname.value,
16-
"L": job.inputs.microstructure.L.get_list()
16+
"L": job.inputs.microstructure.L.get_list(),
1717
},
1818
"results_prefix": job.inputs.metadata.options.results_prefix,
1919
## Problem Type and Material Model
@@ -26,14 +26,15 @@ def make_input_dict(job: CalcJob) -> dict[str, Any]:
2626
"error_parameters": {
2727
"measure": job.inputs.error_parameters.measure.value,
2828
"type": job.inputs.error_parameters.type.value,
29-
"tolerance": job.inputs.error_parameters.tolerance.value
29+
"tolerance": job.inputs.error_parameters.tolerance.value,
3030
},
3131
## Macroscale Loading Conditions
3232
"macroscale_loading": job.inputs.macroscale_loading.get_list(),
3333
## Results Specification
34-
"results": job.inputs.metadata.options.results
34+
"results": job.inputs.metadata.options.results,
3535
}
3636

37+
3738
def arraydata_equal(first: dict[str, ndarray], second: dict[str, ndarray]) -> bool:
3839
"""Return whether two dicts of arrays are roughly equal."""
3940
if first.keys() != second.keys():

src/aiida_fans/parsers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,15 @@ def __init__(self, node: CalcJobNode):
1919

2020
def parse(self, **kwargs) -> ExitCode | None:
2121
"""Parse outputs and store results as nodes."""
22-
output_path: Path = Path(kwargs["retrieved_temporary_folder"]) / self.node.get_option("output_filename") # type: ignore
22+
output_path: Path = Path(kwargs["retrieved_temporary_folder"]) / self.node.get_option("output_filename") # type: ignore
2323
if output_path.is_file():
2424
self.out("output", node=SinglefileData(output_path))
2525
else:
2626
return self.exit_codes.ERROR_MISSING_OUTPUT
2727

2828
with h5File(output_path) as h5:
2929
results = h5[
30-
self.node.inputs.microstructure.datasetname.value + \
31-
"_results/" + \
32-
self.node.get_option('results_prefix')
30+
self.node.inputs.microstructure.datasetname.value + "_results/" + self.node.get_option("results_prefix")
3331
]
3432
results.visititems(self.parse_h5)
3533

src/aiida_fans/utils.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ def compile_query(ins: dict[str, Any], qb: QueryBuilder) -> None:
132132
qb.append(cls=type(v), with_outgoing="calc", filters={"pk": v.pk})
133133

134134

135-
def execute_fans(
136-
mode: Literal["Submit", "Run"],
137-
inputs: dict[str, Any]
138-
):
135+
def execute_fans(mode: Literal["Submit", "Run"], inputs: dict[str, Any]):
139136
"""This utility function simplifies the process of executing aiida-fans jobs.
140137
141138
The only nodes you must provide are the `code` and `microstructure` inputs.
@@ -199,15 +196,11 @@ def execute_fans(
199196
submit(calcjob, inputs) # type: ignore
200197

201198

202-
def submit_fans(
203-
inputs: dict[str, Any]
204-
):
199+
def submit_fans(inputs: dict[str, Any]):
205200
"""See `execute_fans` for implementation and usage details."""
206201
execute_fans("Submit", inputs)
207202

208203

209-
def run_fans(
210-
inputs: dict[str, Any]
211-
):
204+
def run_fans(inputs: dict[str, Any]):
212205
"""See `execute_fans` for implementation and usage details."""
213206
execute_fans("Run", inputs)

0 commit comments

Comments
 (0)