Commit d38238db authored by Philipp Arras's avatar Philipp Arras
Browse files

Add helper functions for determining types

parent 36f0e20c
......@@ -38,6 +38,7 @@ from .plot import Plot
__all__ = ['PS_field', 'power_analyze', 'create_power_operator',
'create_harmonic_smoothing_operator', 'from_random',
'full', 'makeField',
'is_fieldlike', 'is_linearization', 'is_operator',
'makeDomain', 'get_signal_variance', 'makeOp', 'domain_union',
'get_default_codomain', 'single_plot', 'exec_time',
'calculate_position'] + list(pointwise.ptw_dict.keys())
......@@ -526,3 +527,33 @@ def calculate_position(operator, output):
kl, _ = minimizer(kl)
pos = kl.position
return pos
def is_operator(obj):
"""Checks if object is operator-like.
Note
----
A simple `isinstance(obj, ift.Operator)` does give the expected
result because, e.g., :class:`~nifty7.field.Field` inherits from
:class:`~nifty7.operators.operator.Operator`.
"""
return isinstance(obj, Operator) and obj.val is None and obj.jac is None
def is_linearization(obj):
"""Checks if object is linearization-like."""
return isinstance(obj, Operator) and obj.val is not None and obj.jac is not None
def is_fieldlike(obj):
"""Checks if object is field-like.
Note
----
A simple `isinstance(obj, ift.Field)` does give the expected
result because users might have implemented another class which
behaves field-like but is not an instance of
:class:`~nifty7.field.Field`.
"""
return isinstance(obj, Operator) and obj.val is not None and obj.jac is None
......@@ -69,3 +69,19 @@ def test_calc_pos(mf, cplx):
fld = op(0.1 * ift.from_random(op.domain, 'normal'))
pos = ift.calculate_position(op, fld)
ift.extra.assert_allclose(op(pos), fld, 1e-1, 1e-1)
def test_isinstance_helpers():
dom = ift.RGSpace(12, harmonic=True)
op = ift.ScalingOperator(dom, 12.)
fld = ift.full(dom, 0.)
lin = ift.Linearization.make_var(fld)
assert not ift.is_fieldlike(op)
assert not ift.is_fieldlike(lin)
assert ift.is_fieldlike(fld)
assert not ift.is_linearization(op)
assert ift.is_linearization(lin)
assert not ift.is_linearization(fld)
assert ift.is_operator(op)
assert not ift.is_operator(lin)
assert not ift.is_operator(fld)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment