pycsamt.core.mixins#
Reusable mixins for creating and manipulating transfer-function bundles.
Functions
|
Build a |
Classes
Mixin for containers of items that implement |
|
Mixin for single items convertible to and from bundles. |
- pycsamt.core.mixins.bundle_from_edi(obj)[source]#
Build a
TFBundlefrom 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,azimuthare searched with several conventional aliases.- Returns:
A bundle with fields filled where data could be found.
- Return type:
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:
CoreObjectMixin for single items convertible to and from bundles.
Subclasses implement
to_bundleandfrom_bundleto map between their internal representation and the neutralTFBundle.Notes
to_edidelegates topycsamt.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
TFBundlerepresentation. Must be implemented by subclasses.- Return type:
- 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:
- Returns:
Validated or synthetic name.
- Return type:
See also
pycsamt.core.base.ensure_stationUnderlying helper with policy lookup.
- to_edi(*, key=None, **k)[source]#
Convert
selfto EDI or EDICollection via dispatch.- Parameters:
- Returns:
EDI object or EDICollection, depending on adapter.
- Return type:
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_objlooks like a collection, every element is converted to aTFBundleand fed tofrom_bundle. Otherwise the single object is converted and fed tofrom_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:
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:
BundleMixinMixin 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'
- to_edi_collection(*, key=None, **k)[source]#
Convert all child bundles through adapter dispatch.
- Parameters:
- Returns:
A list of EDI objects. Callers may wrap this into an actual
EDICollection.- Return type:
Examples
>>> from pycsamt.core.mixins import BundleContainerMixin >>> # Requires registered adapters to actually run: >>> # objs = MyContainer(...).to_edi_collection()