Ejemplo n.º 1
0
def convolve_models(model, kernel, mode='convolve_fft', **kwargs):
    """
    Convolve two models using `~astropy.convolution.convolve_fft`.

    Parameters
    ----------
    model : `~astropy.modeling.core.Model`
        Functional model
    kernel : `~astropy.modeling.core.Model`
        Convolution kernel
    mode : str
        Keyword representing which function to use for convolution.
            * 'convolve_fft' : use `~astropy.convolution.convolve_fft` function.
            * 'convolve' : use `~astropy.convolution.convolve`.
    **kwargs : dict
        Keyword arguments to me passed either to `~astropy.convolution.convolve`
        or `~astropy.convolution.convolve_fft` depending on ``mode``.

    Returns
    -------
    default : `~astropy.modeling.core.CompoundModel`
        Convolved model
    """

    if mode == 'convolve_fft':
        operator = SPECIAL_OPERATORS.add('convolve_fft', partial(convolve_fft, **kwargs))
    elif mode == 'convolve':
        operator = SPECIAL_OPERATORS.add('convolve', partial(convolve, **kwargs))
    else:
        raise ValueError(f'Mode {mode} is not supported.')

    return CompoundModel(operator, model, kernel)
Ejemplo n.º 2
0
def test_fix_inputs_invalid():
    g1 = Gaussian2D(1, 0, 0, 1, 2)
    with pytest.raises(ValueError):
        fix_inputs(g1, {'x0': 0, 0: 0})

    with pytest.raises(ValueError):
        fix_inputs(g1, (0, 1))

    with pytest.raises(ValueError):
        fix_inputs(g1, {3: 2})

    with pytest.raises(ValueError):
        fix_inputs(g1, {np.int32(3): 2})

    with pytest.raises(ValueError):
        fix_inputs(g1, {np.int64(3): 2})

    with pytest.raises(ValueError):
        fix_inputs(g1, {'w': 2})

    with pytest.raises(ModelDefinitionError):
        CompoundModel('#', g1, g1)

    with pytest.raises(ValueError):
        gg1 = fix_inputs(g1, {0: 1})
        gg1(2, y=2)

    with pytest.raises(ValueError):
        gg1 = fix_inputs(g1, {np.int32(0): 1})
        gg1(2, y=2)

    with pytest.raises(ValueError):
        gg1 = fix_inputs(g1, {np.int64(0): 1})
        gg1(2, y=2)
Ejemplo n.º 3
0
    def from_tree_tagged(cls, node, ctx):
        tag = node._tag[node._tag.rfind('/') + 1:]
        tag = tag[:tag.rfind('-')]
        oper = _tag_to_method_mapping[tag]
        left = yamlutil.tagged_tree_to_custom_tree(node['forward'][0], ctx)
        if not isinstance(left, Model):
            raise TypeError("Unknown model type '{0}'".format(
                node['forward'][0]._tag))
        right = yamlutil.tagged_tree_to_custom_tree(node['forward'][1], ctx)
        if not isinstance(right, Model) and \
            not (oper == 'fix_inputs' and isinstance(right, dict)):
            raise TypeError("Unknown model type '{0}'".format(
                node['forward'][1]._tag))
        if oper == 'fix_inputs':
            right = dict(zip(right['keys'], right['values']))
            model = CompoundModel('fix_inputs', left, right)
        else:
            model = getattr(left, oper)(right)

        model = cls._from_tree_base_transform_members(model, node, ctx)
        model.map_parameters()
        return model
Ejemplo n.º 4
0
    def from_tree_tagged(cls, node, ctx):
        tag = node._tag[node._tag.rfind('/') + 1:]
        tag = tag[:tag.rfind('-')]
        oper = _tag_to_method_mapping[tag]
        left = node['forward'][0]
        if not isinstance(left, Model):
            raise TypeError(f"Unknown model type '{node['forward'][0]._tag}'")
        right = node['forward'][1]
        if (not isinstance(right, Model)
                and not (oper == 'fix_inputs' and isinstance(right, dict))):
            raise TypeError(f"Unknown model type '{node['forward'][1]._tag}'")
        if oper == 'fix_inputs':
            right = dict(zip(right['keys'], right['values']))
            model = CompoundModel('fix_inputs', left, right)
        else:
            model = getattr(left, oper)(right)

        return cls._from_tree_base_transform_members(model, node, ctx)
Ejemplo n.º 5
0
 def from_tree_transform(self, node):
     tag = node._tag[node._tag.rfind('/') + 1:]
     tag = tag[:tag.rfind('-')]
     oper = _tag_to_method_mapping[tag]
     left = node['forward'][0]
     if not isinstance(left, Model):
         raise TypeError("Unknown model type '{0}'".format(
             node['forward'][0]._tag))
     right = node['forward'][1]
     if (not isinstance(right, Model)
             and not (oper == 'fix_inputs' and isinstance(right, dict))):
         raise TypeError("Unknown model type '{0}'".format(
             node['forward'][1]._tag))
     if oper == 'fix_inputs':
         right = dict(zip(right['keys'], right['values']))
         model = CompoundModel('fix_inputs', left, right)
     else:
         model = getattr(left, oper)(right)
     return model
Ejemplo n.º 6
0
    def from_yaml_tree_transform(self, node, tag, ctx):
        from astropy.modeling.core import Model, CompoundModel

        oper = _TAG_NAME_TO_MODEL_METHOD[tag.rsplit("/", 1)[-1].rsplit("-", 1)[0]]

        left = node["forward"][0]
        if not isinstance(left, Model):
            raise TypeError("Unknown model type '{0}'".format(
                node["forward"][0]._tag))

        right = node["forward"][1]
        if (not isinstance(right, Model) and
                not (oper == "fix_inputs" and isinstance(right, dict))):
            raise TypeError("Unknown model type '{0}'".format(
                node["forward"][1]._tag))

        if oper == "fix_inputs":
            right = dict(zip(right["keys"], right["values"]))
            model = CompoundModel("fix_inputs", left, right)
        else:
            model = getattr(left, oper)(right)

        return model