Skip to content
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ repos:
- id: mixed-line-ending
exclude: ^\.idea/.*\.xml$
- id: name-tests-test
exclude: testing_utils.py
- id: pretty-format-json
args:
- --autofix
Expand Down
54 changes: 41 additions & 13 deletions simpeg_drivers/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,30 +612,38 @@
dconf.set(scheduler="threads", pool=ThreadPool(n_cpu))

@classmethod
def start(cls, filepath: str | Path | InputFile):
def start(
cls, filepath: str | Path | InputFile, driver_class=None, **kwargs
) -> InversionDriver:
"""
Start the inversion driver.

:param filepath: Path to the input file or InputFile object.
:param driver_class: Optional driver class to use instead of the default.
:param kwargs: Additional keyword arguments for InputFile read_ui_json.

:return: InversionDriver instance with the specified parameters.
"""
if isinstance(filepath, InputFile):
ifile = filepath
else:
ifile = InputFile.read_ui_json(filepath)
ifile = InputFile.read_ui_json(filepath, **kwargs)

forward_only = ifile.data["forward_only"]
inversion_type = ifile.ui_json.get("inversion_type", None)

driver_class = cls.driver_class_from_name(
inversion_type, forward_only=forward_only
)

with ifile.data["geoh5"].open(mode="r+"):
params = driver_class._options_class.build(ifile)
driver = driver_class(params)
if driver_class is None:
driver = cls.from_input_file(ifile)
else:
with ifile.data["geoh5"].open(mode="r+"):
params = driver_class._options_class.build(ifile)
driver = driver_class(params)

Check warning on line 637 in simpeg_drivers/driver.py

View check run for this annotation

Codecov / codecov/patch

simpeg_drivers/driver.py#L635-L637

Added lines #L635 - L637 were not covered by tests

driver.run()

return driver

@staticmethod
def driver_class_from_name(
name: str, forward_only: bool = False
) -> InversionDriver:
) -> type[InversionDriver]:
if name not in DRIVER_MAP:
msg = f"Inversion type {name} is not supported."
msg += f" Valid inversions are: {(*list(DRIVER_MAP),)}."
Expand All @@ -649,6 +657,26 @@
module = __import__(mod_name, fromlist=[class_name])
return getattr(module, class_name)

@classmethod
def from_input_file(cls, ifile: InputFile) -> InversionDriver:
forward_only = ifile.data["forward_only"]
inversion_type = ifile.ui_json.get("inversion_type", None)
if inversion_type is None:
raise GeoAppsError(

Check warning on line 665 in simpeg_drivers/driver.py

View check run for this annotation

Codecov / codecov/patch

simpeg_drivers/driver.py#L665

Added line #L665 was not covered by tests
"Key/value 'inversion_type' not found in the input file. "
"Please specify the inversion type in the UI JSON."
)

driver_class = cls.driver_class_from_name(
inversion_type, forward_only=forward_only
)

with ifile.data["geoh5"].open(mode="r+"):
params = driver_class._options_class.build(ifile)
driver = driver_class(params)

return driver


class InversionLogger:
def __init__(self, logfile, driver):
Expand Down
2 changes: 1 addition & 1 deletion tests/data_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from simpeg_drivers.potential_fields.magnetic_vector.options import (
MVIInversionOptions,
)
from simpeg_drivers.utils.testing import Geoh5Tester, setup_inversion_workspace
from tests.testing_utils import Geoh5Tester, setup_inversion_workspace


def get_mvi_params(tmp_path: Path, **kwargs) -> MVIInversionOptions:
Expand Down
2 changes: 1 addition & 1 deletion tests/driver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from simpeg_drivers.options import ActiveCellsOptions
from simpeg_drivers.potential_fields import GravityInversionOptions
from simpeg_drivers.potential_fields.gravity.driver import GravityInversionDriver
from simpeg_drivers.utils.testing import setup_inversion_workspace
from tests.testing_utils import setup_inversion_workspace


def test_smallness_terms(tmp_path: Path):
Expand Down
275 changes: 275 additions & 0 deletions tests/legacy/v0.2.1/direct_current_forward_2d.ui.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
{
"version": "0.2.0-alpha.1",
"title": "Direct Current (DC) 2D Forward",
"icon": "PotentialElectrode",
"documentation": "https://mirageoscience-simpeg-drivers.readthedocs-hosted.com/en/stable/intro.html",
"conda_environment": "simpeg_drivers",
"run_command": "simpeg_drivers.driver",
"geoh5": "",
"monitoring_directory": "",
"workspace_geoh5": "",
"inversion_type": "direct current 2d",
"forward_only": true,
"data_object": {
"main": true,
"group": "Survey",
"label": "Object",
"meshType": "{275ecee9-9c24-4378-bf94-65f3c5fbe163}",
"value": ""
},
"z_from_topo": {
"group": "Survey",
"main": true,
"label": "Surface survey",
"tooltip": "Uncheck if borehole data is present",
"value": true
},
"line_object": {
"association": "Cell",
"dataType": "Referenced",
"group": "Survey",
"main": true,
"label": "Line ID",
"parent": "data_object",
"value": "",
"tooltip": "Selects the data representing the different lines in the survey."
},
"line_id": {
"group": "Survey",
"main": true,
"min": 1,
"label": "Line number",
"value": 1,
"tooltip": "Selects the line of data to be processed."
},
"receivers_radar_drape": {
"association": [
"Cell",
"Vertex"
],
"dataType": "Float",
"group": "Data pre-processing",
"label": "Z radar offset",
"tooltip": "Apply a non-homogeneous offset to survey object from radar channel.",
"optional": true,
"parent": "data_object",
"value": "",
"enabled": false,
"visible": false
},
"receivers_offset_z": {
"group": "Data pre-processing",
"label": "Z static offset",
"optional": true,
"enabled": false,
"value": 0.0,
"visible": false
},
"gps_receivers_offset": "",
"potential_channel_bool": true,
"mesh": {
"group": "Mesh and models",
"main": true,
"optional": true,
"enabled": false,
"label": "Mesh",
"meshType": "{c94968ea-cf7d-11eb-b8bc-0242ac130003}",
"value": ""
},
"u_cell_size": {
"min": 0.0,
"group": "Mesh and models",
"dependency": "mesh",
"dependencyType": "disabled",
"main": true,
"enabled": true,
"label": "Easting core cell size (m)",
"value": 25.0
},
"v_cell_size": {
"min": 0.0,
"group": "Mesh and models",
"dependency": "mesh",
"dependencyType": "disabled",
"main": true,
"enabled": true,
"label": "Northing core cell size (m)",
"value": 25.0
},
"depth_core": {
"min": 0.0,
"group": "Mesh and models",
"dependency": "mesh",
"dependencyType": "disabled",
"main": true,
"enabled": true,
"label": "Depth of core (m)",
"value": 500.0
},
"horizontal_padding": {
"min": 0.0,
"group": "Mesh and models",
"dependency": "mesh",
"dependencyType": "disabled",
"main": true,
"enabled": true,
"label": "Horizontal padding (m)",
"value": 1000.0
},
"vertical_padding": {
"min": 0.0,
"group": "Mesh and models",
"dependency": "mesh",
"dependencyType": "disabled",
"main": true,
"label": "Vertical padding (m)",
"value": 1000.0
},
"expansion_factor": {
"main": true,
"group": "Mesh and models",
"dependency": "mesh",
"dependencyType": "disabled",
"label": "Expansion factor",
"value": 1.1
},
"model_type": {
"choiceList": [
"Conductivity (S/m)",
"Resistivity (Ohm-m)"
],
"main": true,
"group": "Mesh and models",
"label": "Model units",
"tooltip": "Select the units of the model.",
"value": "Conductivity (S/m)"
},
"starting_model": {
"association": "Cell",
"dataType": "Float",
"group": "Mesh and models",
"main": true,
"isValue": true,
"parent": "mesh",
"label": "Value(s)",
"property": "",
"value": 0.001
},
"topography_object": {
"main": true,
"group": "Topography",
"label": "Topography",
"meshType": [
"{202c5db1-a56d-4004-9cad-baafd8899406}",
"{6a057fdc-b355-11e3-95be-fd84a7ffcb88}",
"{f26feba3-aded-494b-b9e9-b2bbcbe298e1}",
"{48f5054a-1c5c-4ca4-9048-80f36dc60a06}",
"{b020a277-90e2-4cd7-84d6-612ee3f25051}"
],
"value": "",
"optional": true,
"enabled": true,
"tooltip": "Select a topography object to define the active cells for inversion."
},
"topography": {
"association": [
"Vertex",
"Cell"
],
"dataType": "Float",
"group": "Topography",
"main": true,
"optional": true,
"enabled": false,
"label": "Elevation channel",
"tooltip": "Set elevation from channel. If not set the topography will be set from the geometry of the selected 'topography' object.",
"parent": "topography_object",
"dependency": "topography_object",
"dependencyType": "enabled",
"value": "",
"verbose": 2
},
"active_model": {
"association": "Cell",
"dataType": [
"Referenced",
"Boolean",
"Integer"
],
"group": "Topography",
"main": true,
"enabled": false,
"dependency": "topography_object",
"dependencyType": "disabled",
"label": "Active model",
"tooltip": "Provide the active cell boolean model directly if topography not set.",
"parent": "mesh",
"value": ""
},
"output_tile_files": false,
"parallelized": {
"group": "Compute",
"label": "Use parallelization",
"value": true,
"visible": false
},
"n_cpu": {
"min": 1,
"group": "Compute",
"dependency": "parallelized",
"dependencyType": "enabled",
"optional": true,
"enabled": false,
"label": "Number of CPUs",
"value": 1,
"visible": false
},
"tile_spatial": 1,
"max_chunk_size": {
"min": 0,
"group": "Compute",
"optional": true,
"enabled": true,
"label": "Maximum chunk size (Mb)",
"value": 128,
"verbose": 3,
"visible": false,
"tooltip": "Limit the chunk size used by Dask for distributed computation."
},
"chunk_by_rows": {
"group": "Compute",
"label": "Chunk by rows",
"value": true,
"verbose": 3,
"visible": false
},
"out_group": {
"label": "SimPEG group",
"value": "",
"groupType": "{55ed3daf-c192-4d4b-a439-60fa987fe2b8}",
"group": "Drag-and-drop options",
"visible": true,
"optional": true,
"enabled": false,
"tooltip": "Optionally set the SimPEG group to which results will be saved."
},
"generate_sweep": {
"label": "Generate sweep file",
"group": "Python run preferences",
"main": true,
"value": false,
"tooltip": "Generates a file for sweeping parameters instead of running the application."
},
"distributed_workers": "",
"gradient_type": {
"choiceList": [
"total",
"components"
],
"group": "Sparse/blocky model",
"label": "Gradient type",
"value": "total",
"verbose": 3,
"tooltip": "Choose whether the IRLS weights for regularization terms are updated using total or partial gradients."
}
}
Loading