Skip to content

Allow min / max / tie constraint removal for modeling Parameter #1078

Closed

Description

@cdeil

I think currently it is not possible to un-tie a parameter?
At the moment when I try to set model.parameter.tied = False indicate it should be un-tied I get an AssertionError:

    @tied.setter
    def tied(self, val):
>       assert callable(val), "Tied must be a callable"
E       AssertionError: Tied must be a callable

The same applies to trying to remove a min or max constraint from a Parameter, i.e. setting model.parameter.min = None gives an

    @min.setter
    def min(self, val):
>       assert isinstance(val, numbers.Number), "Min value must be a number"
E       AssertionError: Min value must be a number

Another minor thing: the constructor arguments minvalue and maxvalue should be changed to min and max to make them consistent with and easier to remember with the min and max properties.

@iguananaut mentioned he is currently working on the Parameter and Model class, so we should wait for #980 before fixing these issues (if they aren't resolved there).

Activity

  1. embray commented on May 10, 2013

    @embray
    Member

    Hmm. First of all if this is going to throw an error at all it should be a TypeError, not an AssertionError. Second of all, does it make sense to be able to modify these constraints, or does it make more sense to create a new model instance with different constraints?

    I haven't paid much attention to this issue until recently so I'm not clear on whether it's preferable for Model objects to be very tweakable, or what. Though I suppose, as long as you can modify the parameter values I guess you should be able to modify these constraints as well. And in that case I agree it should be possible to 'unset' them with None or False. (in the meantime you could just override them with a noop like lambda v: v).

  2. cdeil commented on May 10, 2013

    @cdeil
    MemberAuthor

    @iguananaut It's very common to fix / free parameters in interactive fitting sessions to eventually achieve convergence for multi-parameter fits.

  3. nden commented on May 10, 2013

    @nden
    Contributor

    @iguananaut Yes, this is a bug in the current master

  4. embray commented on May 10, 2013

    @embray
    Member

    Yeah, okay. That makes sense.

  5. cdeil commented on Aug 8, 2013

    @cdeil
    MemberAuthor

    I think this feature request has been implemented by @nden in the meantime.
    Everyone happy how it works now?

    The only think left would be to change the AssertionError into a TypeError and to add some documentation for this feature?
    Do we want more unit tests with some complicated setting / fitting / unsetting / fitting / ... sequences to make sure the system can't get into a bad state?
    If that's all that's missing to close this issue I can make a PR.

    In [1]: from astropy.modeling.models import Gaussian1DModel
    
    In [2]: gauss = Gaussian1DModel(amplitude=1, stddev=2, mean=3)
    
    In [3]: gauss.bounds
    Out[3]: {'amplitude': (None, None), 'mean': (None, None), 'stddev': (None, None)}
    
    In [4]: gauss.tied
    Out[4]: {'amplitude': False, 'mean': False, 'stddev': False}
    
    In [5]: gauss.amplitude
    Out[5]: 1.0
    
    In [6]: gauss.amplitude.min
    
    In [7]: gauss.amplitude.min = 10
    
    In [8]: gauss.amplitude.min = None
    
    In [9]: gauss.amplitude.tied
    Out[9]: False
    
    In [10]: gauss.amplitude.tied = True
    ERROR: AssertionError: Tied must be a callable [astropy.modeling.parameters]
    ---------------------------------------------------------------------------
    AssertionError                            Traceback (most recent call last)
    <ipython-input-10-0810b990d983> in <module>()
    ----> 1 gauss.amplitude.tied = True
    
    /Users/deil/Library/Python/2.7/lib/python/site-packages/astropy-0.3.dev4991-py2.7-macosx-10.8-x86_64.egg/astropy/modeling/parameters.pyc in tied(self, val)
        198         Tie a parameter.
        199         """
    --> 200         assert callable(val) or val is False, "Tied must be a callable"
        201         self._tied = val
        202 
    
    AssertionError: Tied must be a callable
    
    In [11]: gauss.amplitude.tied = False
    
    In [12]: gauss.amplitude.tied = lambda x : x
    
    In [13]: gauss.amplitude.tied
    Out[13]: <function __main__.<lambda>>
    
    In [14]: gauss.amplitude.tied = False
    
    In [15]: gauss.tied
    Out[15]: {'amplitude': False, 'mean': False, 'stddev': False}
    
  6. nden commented on Oct 5, 2013

    @nden
    Contributor

    This has been fixed in #1243 and tests were added to test_constraints.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions