pycsamt.core.mixins#

Reusable mixins for creating and manipulating transfer-function bundles.

Functions

bundle_from_edi(obj)

Build a TFBundle from an EDI-like object.

Classes

BundleContainerMixin()

Mixin for containers of items that implement to_bundle.

BundleMixin()

Mixin for single items convertible to and from bundles.

pycsamt.core.mixins.bundle_from_edi(obj)[source]#

Build a TFBundle from an EDI-like object.

The function probes common attribute names found in EDI objects and populates a neutral bundle. It tolerates missing fields and does not enforce array types.

Parameters:

obj (Any) – EDI-like object. Attributes such as freq, z, z_err, tipper, rho, phase, station, lat, lon, elev, azimuth are searched with several conventional aliases.

Returns:

A bundle with fields filled where data could be found.

Return type:

TFBundle

Notes

If the tipper has shape (n, 1, 2) it is normalized to (n, 2). Unknown shapes are left unchanged.

Examples

>>> from pycsamt.core.mixins import bundle_from_edi
>>> class E:
...     def __init__(self):
...         self.freq = [1.0, 2.0]
...         self.phase = [45.0, 50.0]
...         self.rho = [100.0, 120.0]
...
>>> b = bundle_from_edi(E())
>>> b.freq, b.phase[0]
([1.0, 2.0], 45.0)
class pycsamt.core.mixins.BundleMixin[source]#

Bases: CoreObject

Mixin for single items convertible to and from bundles.

Subclasses implement to_bundle and from_bundle to map between their internal representation and the neutral TFBundle.

Notes

to_edi delegates to pycsamt.core.base.to_edi(), which uses the adapter registry to pick the right converter.

Examples

>>> from pycsamt.core.mixins import BundleMixin
>>> from pycsamt.core.base import TFBundle
>>>
>>> class Item(BundleMixin):
...     def __init__(self, b): self._b = b
...     def to_bundle(self): return self._b
...     @classmethod
...     def from_bundle(cls, b): return cls(b)
...
>>> b = TFBundle(freq=[1.0], rho=[100.], phase=[45.])
>>> it = Item.from_bundle(b)
>>> isinstance(it.to_bundle(), TFBundle)
True
to_bundle()[source]#

Return a TFBundle representation. Must be implemented by subclasses.

Return type:

TFBundle

classmethod from_bundle(bundle)[source]#

Construct an instance from a TFBundle.

Returns a new instance of the implementing class.

Must be implemented by subclasses.

Parameters:

bundle (TFBundle)

static ensure_station_name(name, station_id)[source]#

Validate or synthesize a station name via policy.

Parameters:
  • name (str or None) – Preferred station/site name if available.

  • station_id (str, int or None) – Identifier used to synthesize a name when needed.

Returns:

Validated or synthetic name.

Return type:

str

See also

pycsamt.core.base.ensure_station

Underlying helper with policy lookup.

to_edi(*, key=None, **k)[source]#

Convert self to EDI or EDICollection via dispatch.

Parameters:
  • key (str, optional) – Adapter key override (e.g. 'avg', 'j').

  • **k (Any) – Extra keyword arguments forwarded to the adapter.

Returns:

EDI object or EDICollection, depending on adapter.

Return type:

Any

Notes

Dispatch is handled by pycsamt.core.base.to_edi(). A suitable adapter must have been registered, otherwise a runtime error is raised.

classmethod from_edi(edi_obj)[source]#

Construct instance(s) from an EDI object or a collection.

If edi_obj looks like a collection, every element is converted to a TFBundle and fed to from_bundle. Otherwise the single object is converted and fed to from_bundle.

Parameters:

edi_obj (Any) – EDI object or an iterable of EDI objects.

Returns:

Instance or list of instances of the implementing class.

Return type:

Any

Examples

>>> from pycsamt.core.mixins import BundleMixin, bundle_from_edi
>>> class I(BundleMixin):
...     def __init__(self, b): self._b = b
...     def to_bundle(self): return self._b
...     @classmethod
...     def from_bundle(cls, b): return cls(b)
...
>>> e = type('E', (), {'freq':[1.0], 'rho':[100.], 'phase':[45.]})()
>>> obj = I.from_edi(e)
class pycsamt.core.mixins.BundleContainerMixin[source]#

Bases: BundleMixin

Mixin for containers of items that implement to_bundle.

This mixin provides iteration over bundles and a convenience method to convert all items to an EDI collection using the adapter dispatch.

Notes

Detection of children is permissive: if the container has an items() method, values are inspected; otherwise the container is iterated directly.

Examples

>>> from pycsamt.core.mixins import BundleContainerMixin
>>> from pycsamt.core.base import TFBundle
>>>
>>> class Bag(dict, BundleContainerMixin):
...     pass
...
>>> _ = Bag({0: type('X',(),{'to_bundle':lambda s: TFBundle()})()})
>>> list(_.iter_bundles())[0].__class__.__name__
'TFBundle'
iter_bundles()[source]#

Yield contained items as TFBundle objects.

Return type:

Iterator[TFBundle]

to_edi_collection(*, key=None, **k)[source]#

Convert all child bundles through adapter dispatch.

Parameters:
  • key (str, optional) – Adapter key override for all children.

  • **k (Any) – Extra keyword arguments forwarded to the adapter.

Returns:

A list of EDI objects. Callers may wrap this into an actual EDICollection.

Return type:

list of Any

Examples

>>> from pycsamt.core.mixins import BundleContainerMixin
>>> # Requires registered adapters to actually run:
>>> # objs = MyContainer(...).to_edi_collection()