Skip to content

Commit 7adbe26

Browse files
authored
Merge pull request #34 from MiraGeoscience/release/0.1.6
merge Release/0.1.6 to main
2 parents 3c78371 + 7502a0b commit 7adbe26

16 files changed

+352
-345
lines changed

.github/workflows/issue_to_jira.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
# Additional fields in JSON format
2929
fields: '{"components": [{"name": "param-sweeps"}]}'
3030
- name: Post JIRA link
31-
uses: peter-evans/create-or-update-comment@v2
31+
uses: peter-evans/create-or-update-comment@v3
3232
with:
3333
# The number of the issue or pull request in which to create a comment.
3434
issue-number: ${{ github.event.issue.number }}

.github/workflows/pr_add_jira_summary.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
PR_TITLE: ${{ github.event.pull_request.title }}
2020
run: >
2121
echo $HEAD_REF $PR_TITLE
22-
| echo "::set-output name=issue_key::$(grep -os "\b\(GA\|GEOPY\)-[0-9]\+" | head -n1)"
22+
| echo "{issue_key}=$(grep -os "\b\(GA\|GEOPY\)-[0-9]\+" | head -n1)" >> "$GITHUB_OUTPUT"
2323
- name: Get JIRA summary
2424
id: get_jira_summary
2525
if: ${{ steps.find_jira_key.outputs.issue_key }}
@@ -31,7 +31,7 @@ jobs:
3131
-H "Authorization: Basic $JIRA_BASIC_AUTH"
3232
-H "Content-Type: application/json"
3333
"$JIRA_BASE_URL/rest/api/2/issue/${{ steps.find_jira_key.outputs.issue_key }}"
34-
| echo "::set-output name=summary::$(jq -r '.fields.summary')"
34+
| echo "summary=$(jq -r '.fields.summary')" >> "$GITHUB_OUTPUT"
3535
- name: Add comment
3636
if: ${{ steps.find_jira_key.outputs.issue_key }}
3737
env:

.pre-commit-config.yaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ ci:
1010

1111
repos:
1212
- repo: https://github.com/psf/black
13-
rev: 23.1.0
13+
rev: 23.9.1
1414
hooks:
1515
- id: black
1616
types: [text]
@@ -29,25 +29,30 @@ repos:
2929
types: [text]
3030
types_or: [python, pyi]
3131
- repo: https://github.com/PyCQA/flake8
32-
rev: 6.0.0
32+
rev: 6.1.0
3333
hooks:
3434
- id: flake8
3535
types: [text]
3636
types_or: [python, pyi]
3737
- repo: https://github.com/asottile/pyupgrade
38-
rev: v3.3.1
38+
rev: v3.13.0
3939
hooks:
4040
- id: pyupgrade
4141
args: [--py38-plus]
4242
types: [text]
4343
types_or: [python, pyi]
4444
- repo: https://github.com/pre-commit/mirrors-mypy
45-
rev: v1.1.1
45+
rev: v1.5.1
4646
hooks:
4747
- id: mypy
4848
additional_dependencies: [types-toml]
4949
args: [--ignore-missing-imports, --scripts-are-modules, --show-error-context,
5050
--show-column-numbers]
51+
- repo: https://github.com/codingjoe/relint
52+
rev: 3.1.0
53+
hooks:
54+
- id: relint
55+
args: [-W] # to fail on warnings
5156
- repo: local
5257
hooks:
5358
- id: pylint
@@ -66,7 +71,7 @@ repos:
6671
types_or: [python, pyi]
6772
exclude: ^(devtools/|docs/|setup.py)
6873
- repo: https://github.com/codespell-project/codespell
69-
rev: v2.2.4
74+
rev: v2.2.5
7075
hooks:
7176
- id: codespell
7277
exclude: (\.ipynb$|^\.github/workflows/issue_to_jira.yml$)
@@ -89,7 +94,7 @@ repos:
8994
- id: mixed-line-ending
9095
- id: name-tests-test
9196
- repo: https://github.com/rstcheck/rstcheck
92-
rev: v6.1.2
97+
rev: v6.2.0
9398
hooks:
9499
- id: rstcheck
95100
additional_dependencies: [sphinx]

.relint.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- name: No import os.path
2+
pattern: 'from os import\b.*\bpath\b'
3+
hint: Use from pathlib import Path
4+
filePattern: .*\.pyi?
5+
error: false
6+
- name: No os.path
7+
pattern: '\bos\.path\b'
8+
hint: Use pathlib.Path
9+
filePattern: .*\.pyi?
10+
error: false

devtools/check-copyright.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
# Copyright (c) 2023 Mira Geoscience Ltd.
3+
# Copyright (c) 2024 Mira Geoscience Ltd.
44
#
55
# This file is part of param-sweeps.
66
#

param_sweeps/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Copyright (c) 2023 Mira Geoscience Ltd.
1+
# Copyright (c) 2024 Mira Geoscience Ltd.
22
#
33
# This file is part of param-sweeps.
44
#
55
# param-sweeps is distributed under the terms and conditions of the MIT License
66
# (see LICENSE file at the root of this source code package).
77

88

9-
__version__ = "0.1.5"
9+
__version__ = "0.1.6"

param_sweeps/constants.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
# Copyright (c) 2023 Mira Geoscience Ltd.
1+
# Copyright (c) 2024 Mira Geoscience Ltd.
22
#
33
# This file is part of param-sweeps.
44
#
55
# param-sweeps is distributed under the terms and conditions of the MIT License
66
# (see LICENSE file at the root of this source code package).
77

8-
default_ui_json = {
8+
from __future__ import annotations
9+
10+
from typing import Any
11+
12+
default_ui_json: dict[str, Any] = {
913
"title": "Parameter sweep",
1014
"worker_uijson": None,
1115
"run_command": "param_sweeps.driver",

param_sweeps/driver.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 Mira Geoscience Ltd.
1+
# Copyright (c) 2024 Mira Geoscience Ltd.
22
#
33
# This file is part of param-sweeps.
44
#
@@ -12,10 +12,11 @@
1212
import inspect
1313
import itertools
1414
import json
15-
import os
1615
import uuid
1716
from dataclasses import dataclass
1817
from inspect import signature
18+
from io import BytesIO
19+
from pathlib import Path
1920
from typing import Any
2021

2122
import numpy as np
@@ -44,6 +45,9 @@ def from_input_file(cls, ifile: InputFile):
4445
cls_fields = list(signature(cls).parameters)
4546
base_params, app_params = {}, {}
4647

48+
if ifile.data is None:
49+
raise ValueError("Input file data is empty.")
50+
4751
for param, value in ifile.data.items():
4852
if param in cls_fields:
4953
base_params[param] = value
@@ -92,10 +96,17 @@ def parameter_sets(self) -> dict:
9296
class SweepDriver:
9397
"""Sweeps parameters of a worker driver."""
9498

95-
def __init__(self, params):
99+
def __init__(self, params: SweepParams):
100+
if params.geoh5 is None:
101+
raise ValueError("Workspace must be specified.")
102+
96103
self.params: SweepParams = params
97-
self.workspace = params.geoh5
98-
self.working_directory = os.path.dirname(self.workspace.h5file)
104+
self.workspace: Workspace = params.geoh5
105+
106+
if isinstance(self.workspace.h5file, BytesIO) or self.workspace.h5file is None:
107+
raise ValueError("Workspace must be saved to disk.")
108+
109+
self.working_directory = str(Path(self.workspace.h5file).parent)
99110
lookup = self.get_lookup()
100111
self.write_files(lookup)
101112

@@ -126,8 +137,8 @@ def get_lookup(self):
126137

127138
def update_lookup(self, lookup: dict, gather_first: bool = False):
128139
"""Updates lookup with new entries. Ensures any previous runs are incorporated."""
129-
lookup_path = os.path.join(self.working_directory, "lookup.json")
130-
if os.path.exists(lookup_path) and gather_first: # In case restarting
140+
lookup_path = Path(self.working_directory) / "lookup.json"
141+
if lookup_path.is_file() and gather_first: # In case restarting
131142
with open(lookup_path, encoding="utf8") as file:
132143
lookup.update(json.load(file))
133144

@@ -145,10 +156,8 @@ def write_files(self, lookup):
145156
if trial["status"] != "pending":
146157
continue
147158

148-
filepath = os.path.join(
149-
os.path.dirname(workspace.h5file), f"{name}.ui.geoh5"
150-
)
151-
with Workspace(filepath) as iter_workspace:
159+
filepath = Path(workspace.h5file).parent / f"{name}.ui.geoh5"
160+
with Workspace.create(filepath) as iter_workspace:
152161
ifile.data.update(
153162
dict(
154163
{key: val for key, val in trial.items() if key != "status"},
@@ -161,7 +170,7 @@ def write_files(self, lookup):
161170
obj.copy(parent=iter_workspace, copy_children=True)
162171

163172
ifile.name = f"{name}.ui.json"
164-
ifile.path = os.path.dirname(workspace.h5file)
173+
ifile.path = str(Path(workspace.h5file).parent)
165174
ifile.write_ui_json()
166175
lookup[name]["status"] = "written"
167176

@@ -170,13 +179,13 @@ def write_files(self, lookup):
170179
def run(self):
171180
"""Execute a sweep."""
172181

173-
lookup_path = os.path.join(self.working_directory, "lookup.json")
182+
lookup_path = Path(self.working_directory) / "lookup.json"
174183
with open(lookup_path, encoding="utf8") as file:
175184
lookup = json.load(file)
176185

177186
for name, trial in lookup.items():
178187
ifile = InputFile.read_ui_json(
179-
os.path.join(self.working_directory, f"{name}.ui.json")
188+
Path(self.working_directory) / f"{name}.ui.json"
180189
)
181190
status = trial.pop("status")
182191
if status != "complete":
@@ -189,6 +198,8 @@ def run(self):
189198

190199
def call_worker(ifile: InputFile):
191200
"""Runs the worker for the sweep parameters contained in 'ifile'."""
201+
if ifile.data is None:
202+
raise ValueError("Input file data is empty.")
192203

193204
run_cmd = ifile.data["run_command"]
194205
module = importlib.import_module(run_cmd)
@@ -204,9 +215,9 @@ def filt(member: Any) -> bool:
204215
driver.start(ifile.path_name)
205216

206217

207-
def file_validation(filepath):
218+
def file_validation(filepath: str | Path):
208219
"""Validate file."""
209-
if filepath.endswith("ui.json"):
220+
if "".join(Path(filepath).suffixes) == ".ui.json":
210221
try:
211222
InputFile.read_ui_json(filepath)
212223
except BaseValidationError as err:
@@ -217,7 +228,7 @@ def file_validation(filepath):
217228
raise OSError(f"File argument {filepath} must have extension 'ui.json'.")
218229

219230

220-
def main(file_path):
231+
def main(file_path: str | Path):
221232
"""Run the program."""
222233

223234
file_validation(file_path)
@@ -234,4 +245,4 @@ def main(file_path):
234245
parser.add_argument("file", help="File with ui.json format.")
235246

236247
args = parser.parse_args()
237-
main(os.path.abspath(args.file))
248+
main(Path(args.file).resolve(strict=True))

param_sweeps/generate.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 Mira Geoscience Ltd.
1+
# Copyright (c) 2024 Mira Geoscience Ltd.
22
#
33
# This file is part of param-sweeps.
44
#
@@ -8,9 +8,9 @@
88
from __future__ import annotations
99

1010
import argparse
11-
import os
1211
import re
1312
from copy import deepcopy
13+
from pathlib import Path
1414

1515
from geoh5py.ui_json import InputFile
1616

@@ -30,12 +30,10 @@ def generate(
3030
:param update_values: Updates for sweep files parameters
3131
"""
3232

33-
file = os.path.abspath(worker)
34-
ifile = InputFile.read_ui_json(file)
35-
sweepfile = InputFile(
36-
ui_json=deepcopy(default_ui_json), validation_options={"disabled": True}
37-
)
38-
sweepfile.data.update({"worker_uijson": worker})
33+
file_path = Path(worker).resolve(strict=True)
34+
ifile = InputFile.read_ui_json(file_path)
35+
sweepfile = InputFile(ui_json=deepcopy(default_ui_json), validate=False)
36+
sweepfile.data.update({"worker_uijson": str(worker)})
3937
if update_values:
4038
sweepfile.data.update(**update_values)
4139

@@ -48,15 +46,15 @@ def generate(
4846
sweepfile.ui_json.update(forms)
4947

5048
sweepfile.data["geoh5"] = ifile.data["geoh5"]
51-
dirname = os.path.dirname(file)
52-
filename = os.path.basename(file)
49+
dirpath = file_path.parent
50+
filename = file_path.name
5351
filename = filename.rstrip("ui.json")
5452
filename = re.sub(r"\._sweep$", "", filename)
5553
# filename = filename.rstrip("_sweep")
5654
filename = f"{filename}_sweep.ui.json"
5755

58-
print(f"Writing sweep file to: {os.path.join(dirname, filename)}")
59-
sweepfile.write_ui_json(name=filename, path=dirname)
56+
print(f"Writing sweep file to: {dirpath / filename}")
57+
sweepfile.write_ui_json(name=filename, path=dirpath)
6058

6159

6260
def sweep_forms(param: str, value: int | float) -> dict:

param_sweeps/sample_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2023 Mira Geoscience Ltd.
1+
# Copyright (c) 2024 Mira Geoscience Ltd.
22
#
33
# This file is part of param-sweeps.
44
#

0 commit comments

Comments
 (0)