Workflow sessions and normalization helpers for reproducible conversion work.
The pycsamt.session module provides a small provenance layer around
common file-conversion workflows. It is useful when a script, notebook, or
agent workflow produces intermediate objects that should be recorded in a
manifest without turning the session itself into the science API.
Use sessions for workflow bookkeeping. Use the science modules for scientific
work:
Context-managed capture of key transformation outputs.
A Session wraps selected conversion utilities and
transformers so that their results are automatically
registered in a small on-disk registry. This enables light
provenance tracking of what you produced during a workflow
(e.g., the output from to_edi() or from
AVGtoEDI / JtoEDI).
When the session starts, it temporarily monkey-patches the
following call sites to intercept and register outputs:
On exit, the original functions are restored and the manifest
is saved.
Parameters:
root (path-like) – Working directory that will hold a registry manifest and
any artifacts you add explicitly via the underlying
RegistryAPI.
manifest_name (str, optional) – Manifest filename under root. Defaults to
"manifest.json".
auto_capture (bool, optional) – If True (default), wrap the functions above so that
their outputs are registered with tags describing the
source call (e.g., "AVGtoEDI.transform").
capture_children (bool, optional) – If True, when a captured output is iterable (e.g., a
collection), the session attempts to also register each
child item, up to max_children. Defaults to
False.
max_children (int, optional) – Maximum number of iterable children to capture when
capture_children=True. Defaults to 256.
Variables:
root (pathlib.Path) – Absolute path to the working directory.
The session captures results, not inputs. To store inputs
explicitly, call reg.add_file() or
reg.add_object() on the session’s registry.
The capture is process-local and uses monkey-patching. In
multi-threaded or multi-process settings, consider isolate
usage or add explicit locking to your code.
>>> frompycsamt.sessionimportSession>>> frompycsamt.zonge.avgimportAVG>>> frompycsamt.core.transformersimportAVGtoEDI>>> withSession("work",auto_capture=True)asses:... avg=AVG.from_file("data/S01.AVG")... edi=AVGtoEDI().transform(avg)... # The result of transform is recorded automatically.... got=ses.reg.find(tag="AVGtoEDI.transform")
Capture children from iterable outputs (e.g., collections):
>>> frompycsamtimportsessionaspcs# alias form>>> frompycsamt.seg.collectionimportEDICollection>>> withpcs.Session("work",... auto_capture=True,... capture_children=True,... max_children=8)asses:... # Suppose `coll` is an iterable of EDI-like items.... coll=EDICollection([])# example only... ses.reg.add_object(coll,tags=["batch"])... # Children of `coll` would be recorded when captured.
Inspect what was recorded and persist:
>>> withSession("work")asses:... _=ses.reg.list()... # Saving also happens on context exit.>>> # The manifest `work/manifest.json` now reflects updates.
Normalize heterogeneous EM sources into a common form.
Normalize provides a small, file-system-friendly
facade that tries to convert various inputs (files and
objects) into a standard, iterable representation, favoring
EDICollection when possible.
It accepts AVG files or objects, Jones J files, single EDI
files, and already-built collections. When provided, a
topo_src is attached to AVG sources before conversion.
Optionally, normalized outputs are registered in a small
manifest via RegistryAPI.
Parameters:
root (path-like) – Working directory where a manifest is stored.
manifest_name (str, optional) – Manifest filename under root. Defaults to
"manifest.json".
topo_src (Any or None, optional) – Optional topography source to enrich AVG data before
conversion. It may be an object accepted by
AVG.add_topography or another object exposing a
frame attribute to assign as avg.topo.
auto_register (bool, optional) – If True (default), normalized outputs are recorded in
the registry with the tag "normalized".
Variables:
root (pathlib.Path) – Absolute path to the working directory.
If the source is an EDIFile, wrap it into a
one-item EDICollection.
If the source is an AVG (or AVG file path), try
to attach topo_src then transform with
AVGtoEDI.
If the source is a JFile (or J file path),
transform with JtoEDI.
If the source is a string or path, try loading an
EDIFile and wrap into a collection.
Otherwise, delegate to to_edi() and return an EDI-
like object or a collection when possible.
Errors while attaching topography or performing transforms
are handled defensively; the method falls back to the next
strategy or returns the best-effort result.
Examples
Normalize from an AVG file into an EDI collection:
>>> frompycsamt.sessionimportNormalize>>> withNormalize("work")asnz:... coll=nz.load("data/S01.AVG")... # 'coll' is typically an EDICollection
Attach topography before AVG→EDI:
>>> topo=object()# placeholder for a real topo source>>> withNormalize("work",topo_src=topo)asnz:... coll=nz.load("data/S02.AVG")
Normalize source into a common, iterable EM representation.
This method attempts to coerce the input into an
EDICollection when feasible. If the class was
constructed with auto_register=True, the resulting object
is registered in the manifest with the tag "normalized".
Parameters:
source (Any) – Input to normalize. May be an EDICollection,
EDIFile, AVG, JFile, a file
path to one of those, or another EM object supported by
to_edi().
Returns:
Usually an EDICollection. In fallback cases, an
EDI-like object produced by to_edi().
Context manager for recording workflow outputs in a registry manifest.
It can automatically capture selected conversion results from
to_edi and the AVG/J-to-EDI transformers.
Context manager that accepts heterogeneous sources such as AVG, Jones J,
EDI files, EDI-like objects, and collections, then returns an EDI-like
collection whenever possible.
The registry manifest is saved when the context exits. The captured result is
the transformer output, not the original input. If the raw input also needs to
be tracked, register it explicitly through ses.reg.
If the AVG object supports add_topography, that method is used. Otherwise,
the normalizer attempts a conservative fallback for objects exposing a
frame attribute.