check_conforms¶
Defined in fynance.core
- check_conforms(obj, protocol, *, T=64, N=3, seed=0)[source]
Smoke-run protocol’s methods on obj with seeded synthetic data.
Designs one canonical smoke input per protocol from the actual method set in
fynance.core.protocols:FeatureTransform:fit(X)thentransform(X)on a(T, N)array;transformmust return anumpy.ndarraywhose leading (time) axis has lengthT.SignalModel:fit(X, y)thenpredict(X);predictmust return anumpy.ndarrayof shape(T,).Allocator:__call__on a(T, N)return/covariance-like matrix; must return anumpy.ndarrayof shape(N,)(one weight per asset).CostModel:__call__on a(T, N)weight path; must return anumpy.ndarrayof shape(T,)(one cost per step).Metric:__call__on a(T,)return curve; must return a plain scalar (int/float/numpy scalar), not an array.DataSource: not smoke-run. Its only method,load(*args, **kwargs), is an open-ended I/O port (the one boundary protocol that touches the outside world) – there is no synthetic input this function can fabricate that would exercise a real adapter. Always raisesValueErrorpointing at callingload()directly on a concrete instance instead.
Any other object passed as protocol (not one of the six seams above) also raises
ValueError: there is no registered smoke recipe for it.- Parameters:
- objobject
Candidate instance to check.
- protocoltype
One of the
typing.Protocolclasses fromfynance.core.protocols.- Tint, optional
Synthetic time length. Default is 64.
- Nint, optional
Synthetic asset/feature count. Default is 3.
- seedint, optional
Seed of the synthetic data generator. Default is 0.
- Returns:
- None
Nothing on success.
- Raises:
- AssertionError
If obj does not structurally conform to protocol, or one of its methods returns a value of the wrong type/shape/dtype. The message names the offending method and the expected vs. actual type/shape.
- ValueError
If protocol is
DataSource(I/O port, see above), or is not one of the protocols this function knows how to smoke-test.
Examples
>>> import numpy as np >>> from fynance.core import FeatureTransform >>> from fynance.core.checks import check_conforms >>> class ZScore: ... def fit(self, X): ... self.mean_ = X.mean(axis=0) ... self.std_ = X.std(axis=0) ... return self ... def transform(self, X): ... return (X - self.mean_) / self.std_ >>> check_conforms(ZScore(), FeatureTransform) >>> class BrokenTransform(ZScore): ... def transform(self, X): ... return list(X.mean(axis=0)) >>> try: ... check_conforms(BrokenTransform(), FeatureTransform) ... except AssertionError as e: ... print(str(e)) transform() returned list, expected numpy.ndarray