Workflow

petrophys.workflow.custom.Workflow

Also available as petrophys.workflow.Workflow

class Workflow(name='Custom workflow', steps=(), *, description='')[source]

An ordered, reusable recipe of interpretation steps.

Add built-in steps from petrophys.workflow.steps and your own workflow_step() functions, check the recipe against a well, run it on one well or many, and save it as JSON to share. Steps run in the order they were added; each can use the curves of the well and of earlier steps.

Parameters:
  • name (str, default "Custom workflow") – Name shown in traces, tables and exported files.

  • steps (iterable of Step, optional) – Initial steps, added in order.

  • description (str, optional) – Free-text description saved with the workflow.

Raises:

WorkflowValidationError – If two steps share a name, or a step needs a curve that only a later step produces.

See also

petrophys.interpret

The prebuilt workflow that chooses methods automatically.

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("Brent Group quick look", [
...     steps.clay_volume(method="gr", gr_clean=20, gr_shale=110),
...     steps.porosity(method="density", matrix_density=2.65),
...     steps.formation_water(rw=0.07, rw_temp_f=230),
...     steps.saturation(model="archie", a=1, m=2, n=2),
...     steps.net_pay(vcl_cutoff=0.35, phie_cutoff=0.10, sw_cutoff=0.60),
... ])
>>> len(wf)
5
>>> run = wf.run(pp.read(pp.sample("30_6-5")))
>>> run.summary(top=2841, bottom=2958)["net_thickness"]["depth_unit"]
'm'

Attributes

steps

The steps, in run order.

Methods

add

Append a step.

check

Check what each step needs against a well, before running anything.

copy

Return an independent copy, optionally renamed.

describe

Table of the steps: what each needs, produces and is configured with.

from_dict

Rebuild a workflow from to_dict() output.

insert

Insert a step before or after a named step.

load

Load a workflow saved with save().

remove

Remove the step called name.

replace

Replace the step called name with another step, in place.

run

Run the workflow on one well.

run_many

Run the workflow on many wells, collecting failures per well.

save

Save the workflow as JSON.

to_dict

Return the workflow as a JSON-serializable dictionary.

property steps: Tuple[Step, ...]

The steps, in run order.

add(step)[source]

Append a step.

Parameters:

step (Step) – A configured step, e.g. steps.porosity(method="density").

Returns:

This workflow, so calls can be chained.

Return type:

Workflow

Raises:
  • TypeError – If step is not a Step (for example a factory that was not called).

  • WorkflowValidationError – If the name is already used, or an earlier step needs a curve that only this step produces.

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo").add(steps.clay_volume()).add(steps.porosity())
>>> [step.name for step in wf]
['clay_volume', 'porosity']
insert(step, *, before=None, after=None)[source]

Insert a step before or after a named step.

Parameters:
  • step (Step) – The step to insert.

  • before (str) – Name of an existing step; give exactly one.

  • after (str) – Name of an existing step; give exactly one.

Returns:

This workflow.

Return type:

Workflow

Raises:
  • KeyError – If the named step does not exist.

  • TypeError – If neither or both of before and after are given.

  • WorkflowValidationError – If the result has duplicate names or out-of-order dependencies.

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.clay_volume(), steps.porosity()])
>>> _ = wf.insert(steps.temperature(), before="clay_volume")
>>> [step.name for step in wf]
['temperature', 'clay_volume', 'porosity']
replace(name, step)[source]

Replace the step called name with another step, in place.

Parameters:
  • name (str) – Name of the step to replace.

  • step (Step) – The new step; it may have a different name.

Returns:

This workflow.

Return type:

Workflow

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.clay_volume(method="gr")])
>>> wf.replace("clay_volume", steps.clay_volume(method="minimum"))["clay_volume"]
steps.clay_volume(method='minimum')
remove(name)[source]

Remove the step called name.

Returns:

This workflow.

Return type:

Workflow

Parameters:

name (str)

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.clay_volume(), steps.porosity()])
>>> [step.name for step in wf.remove("porosity")]
['clay_volume']
copy(name=None)[source]

Return an independent copy, optionally renamed.

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> base = Workflow("base", [steps.clay_volume()])
>>> variant = base.copy("variant").add(steps.porosity())
>>> len(base), len(variant)
(1, 2)
Parameters:

name (str | None)

Return type:

Workflow

describe()[source]

Table of the steps: what each needs, produces and is configured with.

Returns:

One row per step, numbered from 1, with columns Step, Kind, Requires, Produces and Parameters (the parameters set explicitly; "defaults" when none were).

Return type:

DataFrame

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.formation_water(rw=0.07, rw_temp_f=230)])
>>> wf.describe().loc[1, "Parameters"]
'rw=0.07, rw_temp_f=230'
check(well)[source]

Check what each step needs against a well, before running anything.

Outputs of earlier steps count as available. A step that cannot run also blocks the steps that need its outputs; those rows say which step is the cause.

Parameters:

well (Well or path) – The well to check (not modified).

Returns:

One row per requirement with columns Step, Requires, Met (bool), Source ("well (RDEP)", "step porosity" or "missing") and Note (why it is unmet and how to fix it). A step without requirements has one row, (nothing).

Return type:

DataFrame

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.saturation(model="archie")])
>>> table = wf.check(pp.read(pp.sample("30_6-5")))
>>> table.loc[~table["Met"], "Requires"].tolist()
['PHIE', 'RW']
run(well, *, strict=True, params=None, inplace=False)[source]

Run the workflow on one well.

Parameters:
  • well (Well or path) – The well, or a well file to read with petrophys.read().

  • strict (bool, default True) – If True, raise WorkflowValidationError before any calculation when a step lacks an input, and WorkflowStepError if a step fails. If False, skip steps that cannot run (recording the reason in steps_skipped) and record failed steps in errors.

  • params (dict, optional) – One-off parameter changes by step name, e.g. {"saturation": {"n": 2.2}}. The workflow itself is unchanged.

  • inplace (bool, default False) – If False, the well is copied and yours is left unchanged.

Returns:

The interpreted well, the step trace, skipped steps, warnings and artifacts.

Return type:

WorkflowRunResult

Raises:

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [
...     steps.clay_volume(),
...     steps.porosity(),
...     steps.formation_water(rw=0.07, rw_temp_f=230),
...     steps.saturation(model="archie"),
... ])
>>> well = pp.read(pp.sample("30_6-5"))
>>> run = wf.run(well, params={"saturation": {"n": 2.2}})
>>> run.status, run.steps[-1].parameters["n"]
('complete', 2.2)
run_many(wells, *, strict=True, formations=None, params=None, pattern='*.las')[source]

Run the workflow on many wells, collecting failures per well.

Parameters:
  • wells (folder, path, Well, or iterable of Wells and paths) – A folder is expanded to the files matching pattern. Files are read with petrophys.read(); Well objects are copied.

  • strict (bool, default True) – As in run(), per well: a well that cannot run, or whose step fails, is recorded in errors and the batch continues.

  • formations (path or DataFrame, optional) – Formation tops attached to every well (a multi-well table is matched by well name), for per-formation summaries.

  • params (dict, optional) – One-off parameter changes by step name, applied to every well.

  • pattern (str, default "*.las") – File pattern used when wells is a folder.

Returns:

results by well name, errors by well name, and WorkflowBatchResult.summary_table().

Return type:

WorkflowBatchResult

Raises:

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("vcl", [steps.clay_volume(method="gr")])
>>> batch = wf.run_many([pp.sample("HW-3"), pp.sample("HW-4")])
>>> list(batch.results), batch.errors
(['HW-3', 'HW-4'], {})
to_dict(*, include_version=True)[source]

Return the workflow as a JSON-serializable dictionary.

Parameters:

include_version (bool, default True) – Include the petrophys version that wrote it.

Returns:

schema, schema_version, name, description and steps (each {"name", "kind", "params"}).

Return type:

dict

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> Workflow("demo", [steps.porosity(method="density")]).to_dict()["steps"]
[{'name': 'porosity', 'kind': 'porosity', 'params': {'method': 'density'}}]
classmethod from_dict(data)[source]

Rebuild a workflow from to_dict() output.

Every step is re-created through its factory, so parameters are validated again.

Warning

A custom step is stored as "module:function" and rebuilt by importing that module, which runs its code. Only load workflows from sources you trust, as you would only run their Python files.

Parameters:

data (mapping) – A workflow dictionary.

Return type:

Workflow

Raises:

ValueError – If the dictionary is not a workflow, was written by a newer format, or a step cannot be rebuilt (unknown kind, invalid parameters, or a custom step that cannot be imported).

Examples

>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.net_pay(sw_cutoff=0.5)])
>>> Workflow.from_dict(wf.to_dict()) == wf
True
save(path)[source]

Save the workflow as JSON.

Parameters:

path (str or Path) – Output file; parent folders are created.

Returns:

The written file.

Return type:

Path

Raises:

ValueError – If a custom step is defined inside a function and so could never be imported again.

Warns:

UserWarning – If a custom step is defined in __main__ (a script or notebook): the file then loads only where that function exists.

Examples

>>> import tempfile, pathlib
>>> import petrophys as pp
>>> from petrophys.workflow import Workflow, steps
>>> wf = Workflow("demo", [steps.clay_volume(method="gr")])
>>> with tempfile.TemporaryDirectory() as folder:
...     path = wf.save(pathlib.Path(folder) / "demo.json")
...     Workflow.load(path) == wf
True
classmethod load(path)[source]

Load a workflow saved with save().

Warning

Custom steps are rebuilt by importing their modules; only load workflow files you trust (see from_dict()).

Parameters:

path (str or Path) – A workflow JSON file.

Return type:

Workflow

Raises: