<s> """<STR_LIT>""" <EOL> from six . moves import reduce <EOL> import warnings <EOL> from copy import copy <EOL> from numpy import inf , isinf , isfinite <EOL> from . import bounds as mbounds <EOL> class BaseParameter ( object ) : <EOL> """<STR_LIT>""" <EOL> fixed = True <EOL> fittable = False <EOL> discrete = False <EOL> _bounds = mbounds . Unbounded ( ) <EOL> name = None <EOL> def parameters ( self ) : <EOL> return [ self ] <EOL> def pmp ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . bounds = mbounds . Bounded ( * mbounds . pmp ( self . value , * args ) ) <EOL> return self <EOL> def pm ( self , * args ) : <EOL> """<STR_LIT>""" <EOL> self . bounds = mbounds . Bounded ( * mbounds . pm ( self . value , * args ) ) <EOL> return self <EOL> def dev ( self , std , mean = <NUM_LIT:0> , limits = None , sigma = None , mu = None ) : <EOL> """<STR_LIT>""" <EOL> if sigma is not None or mu is not None : <EOL> warnings . warn ( DeprecationWarning ( "<STR_LIT>" ) ) <EOL> if sigma is not None : std = sigma <EOL> if mu is not None : mean = mu <EOL> if mean is None : <EOL> mean = self . value <EOL> if limits is None : <EOL> self . bounds = mbounds . Normal ( mean , std ) <EOL> else : <EOL> self . bounds = mbounds . BoundedNormal ( mean , std , limits ) <EOL> return self <EOL> def pdf ( self , dist ) : <EOL> """<STR_LIT>""" <EOL> self . bounds = mbounds . Distribution ( dist ) <EOL> return self <EOL> def range ( self , low , high ) : <EOL> """<STR_LIT>""" <EOL> self . bounds = mbounds . init_bounds ( ( low , high ) ) <EOL> return self <EOL> def soft_range ( self , low , high , std ) : <EOL> """<STR_LIT>""" <EOL> self . bounds = mbounds . SoftBounded ( low , high , std ) <EOL> @ property <EOL> def bounds ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _bounds <EOL> @ bounds . setter <EOL> def bounds ( self , b ) : <EOL> if self . fittable : <EOL> self . fixed = ( b is None ) <EOL> self . _bounds = b <EOL> def __call__ ( self ) : <EOL> return self . value <EOL> def __gt__ ( self , other ) : <EOL> return ConstraintGT ( self , other ) <EOL> def __ge__ ( self , other ) : <EOL> return ConstraintGE ( self , other ) <EOL> def __le__ ( self , other ) : <EOL> return ConstraintLE ( self , other ) <EOL> def __lt__ ( self , other ) : <EOL> return ConstraintLT ( self , other ) <EOL> def __add__ ( self , other ) : <EOL> return OperatorAdd ( self , other ) <EOL> def __sub__ ( self , other ) : <EOL> return OperatorSub ( self , other ) <EOL> def __mul__ ( self , other ) : <EOL> return OperatorMul ( self , other ) <EOL> def __div__ ( self , other ) : <EOL> return OperatorDiv ( self , other ) <EOL> def __pow__ ( self , other ) : <EOL> return OperatorPow ( self , other ) <EOL> def __radd__ ( self , other ) : <EOL> return OperatorAdd ( other , self ) <EOL> def __rsub__ ( self , other ) : <EOL> return OperatorSub ( other , self ) <EOL> def __rmul__ ( self , other ) : <EOL> return OperatorMul ( other , self ) <EOL> def __rdiv__ ( self , other ) : <EOL> return OperatorDiv ( other , self ) <EOL> def __rpow__ ( self , other ) : <EOL> return OperatorPow ( other , self ) <EOL> def __abs__ ( self ) : <EOL> return _abs ( self ) <EOL> def __neg__ ( self ) : <EOL> return self * - <NUM_LIT:1> <EOL> def __pos__ ( self ) : <EOL> return self <EOL> def __float__ ( self ) : <EOL> return float ( self . value ) <EOL> __truediv__ = __div__ <EOL> __rtruediv__ = __rdiv__ <EOL> def nllf ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . bounds . nllf ( self . value ) <EOL> def residual ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . bounds . residual ( self . value ) <EOL> def valid ( self ) : <EOL> """<STR_LIT>""" <EOL> return not isinf ( self . nllf ( ) ) <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" % ( self , self . value , self . bounds ) <EOL> def __str__ ( self ) : <EOL> name = self . name if self . name is not None else '<STR_LIT:?>' <EOL> return name <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self <EOL> class Constant ( BaseParameter ) : <EOL> """<STR_LIT>""" <EOL> fittable = False <EOL> fixed = True <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _value <EOL> def __init__ ( self , value , name = None ) : <EOL> self . _value = value <EOL> self . name = name <EOL> class Parameter ( BaseParameter ) : <EOL> """<STR_LIT>""" <EOL> fittable = True <EOL> @ classmethod <EOL> def default ( cls , value , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , BaseParameter ) : <EOL> return value <EOL> else : <EOL> return cls ( value , ** kw ) <EOL> def set ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . value = value <EOL> def clip_set ( self , value ) : <EOL> """<STR_LIT>""" <EOL> low , high = self . bounds . limits <EOL> self . value = min ( max ( value , low ) , high ) <EOL> def __init__ ( self , value = None , bounds = None , fixed = None , name = None , ** kw ) : <EOL> if bounds is None : <EOL> try : <EOL> lo , hi = value <EOL> warnings . warn ( DeprecationWarning ( "<STR_LIT>" ) ) <EOL> bounds = lo , hi <EOL> value = None <EOL> except TypeError : <EOL> pass <EOL> if fixed is None : <EOL> fixed = ( bounds is None ) <EOL> bounds = mbounds . init_bounds ( bounds ) <EOL> if value is None : <EOL> value = bounds . start_value ( ) <EOL> limits = kw . get ( '<STR_LIT>' , ( - inf , inf ) ) <EOL> for k , v in kw . items ( ) : <EOL> setattr ( self , k , v ) <EOL> def clip ( x , a , b ) : <EOL> return min ( max ( x , a ) , b ) <EOL> self . bounds = bounds <EOL> self . bounds . limits = ( clip ( self . bounds . limits [ <NUM_LIT:0> ] , * limits ) , <EOL> clip ( self . bounds . limits [ <NUM_LIT:1> ] , * limits ) ) <EOL> self . value = value <EOL> self . fixed = fixed <EOL> self . name = name <EOL> def randomize ( self , rng = None ) : <EOL> """<STR_LIT>""" <EOL> self . value = self . bounds . rand ( rng if rng is not None else mbounds . RNG ) <EOL> def feasible ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . limits [ <NUM_LIT:0> ] <= self . value <= self . limits [ <NUM_LIT:1> ] <EOL> class Reference ( Parameter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , attr , ** kw ) : <EOL> self . obj = obj <EOL> self . attr = attr <EOL> kw . setdefault ( '<STR_LIT:name>' , "<STR_LIT:.>" . join ( [ obj . __class__ . __name__ , attr ] ) ) <EOL> Parameter . __init__ ( self , ** kw ) <EOL> @ property <EOL> def value ( self ) : <EOL> return getattr ( self . obj , self . attr ) <EOL> @ value . setter <EOL> def value ( self , value ) : <EOL> setattr ( self . obj , self . attr , value ) <EOL> class ParameterSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reference , names = None ) : <EOL> """<STR_LIT>""" <EOL> self . names = names <EOL> self . reference = reference <EOL> self . parameters = [ copy ( reference ) for _ in names ] <EOL> for p , n in zip ( self . parameters , names ) : <EOL> p . name = "<STR_LIT:U+0020>" . join ( ( n , p . name ) ) <EOL> self . reference . fittable = False <EOL> def __getitem__ ( self , i ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> i = self . names . index ( i ) <EOL> except ValueError : <EOL> pass <EOL> return self . parameters [ i ] <EOL> def __setitem__ ( self , i , v ) : <EOL> try : <EOL> i = self . names . index ( i ) <EOL> except ValueError : <EOL> pass <EOL> self . parameters [ i ] = v <EOL> def __iter__ ( self ) : <EOL> return iter ( self . parameters ) <EOL> def __len__ ( self ) : <EOL> return len ( self . parameters ) <EOL> def set_model ( self , index ) : <EOL> """<STR_LIT>""" <EOL> self . reference . value = self . parameters [ index ] . value <EOL> @ property <EOL> def values ( self ) : <EOL> return [ p . value for p in self . parameters ] <EOL> @ values . setter <EOL> def values ( self , values ) : <EOL> for p , v in zip ( self . parameters , values ) : <EOL> p . value = v <EOL> def range ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> for p in self . parameters : <EOL> p . range ( * args , ** kw ) <EOL> def pm ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> for p in self . parameters : <EOL> p . pm ( * args , ** kw ) <EOL> def pmp ( self , * args , ** kw ) : <EOL> """<STR_LIT>""" <EOL> for p in self . parameters : <EOL> p . pmp ( * args , ** kw ) <EOL> class FreeVariables ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , names = None , ** kw ) : <EOL> if names is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . names = names <EOL> self . _parametersets = dict ( ( k , ParameterSet ( v , names = names ) ) <EOL> for k , v in kw . items ( ) ) <EOL> def __getstate__ ( self ) : <EOL> return self . __dict__ <EOL> def __setstate__ ( self , state ) : <EOL> self . __dict__ = state <EOL> def __getattr__ ( self , k ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . _parametersets [ k ] <EOL> except KeyError : <EOL> raise AttributeError ( '<STR_LIT>' % k ) <EOL> def parameters ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( ( k , v . parameters ) for k , v in self . _parametersets . items ( ) ) <EOL> def set_model ( self , i ) : <EOL> """<STR_LIT>""" <EOL> for p in self . _parametersets . values ( ) : <EOL> p . set_model ( i ) <EOL> class Constraint : <EOL> """<STR_LIT>""" <EOL> def __bool__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> __nonzero__ = __bool__ <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def _gen_constraint ( name , op ) : <EOL> """<STR_LIT>""" <EOL> return '''<STR_LIT>''' % dict ( name = name , op = op ) <EOL> exec ( _gen_constraint ( '<STR_LIT>' , '<STR_LIT:>>' ) ) <EOL> exec ( _gen_constraint ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> exec ( _gen_constraint ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> exec ( _gen_constraint ( '<STR_LIT>' , '<STR_LIT:<>' ) ) <EOL> exec ( _gen_constraint ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> exec ( _gen_constraint ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def _gen_binop ( name , op ) : <EOL> """<STR_LIT>""" <EOL> return '''<STR_LIT>''' % dict ( name = name , op = op ) <EOL> exec ( _gen_binop ( '<STR_LIT>' , '<STR_LIT:+>' ) ) <EOL> exec ( _gen_binop ( '<STR_LIT>' , '<STR_LIT:->' ) ) <EOL> exec ( _gen_binop ( '<STR_LIT>' , '<STR_LIT:*>' ) ) <EOL> exec ( _gen_binop ( '<STR_LIT>' , '<STR_LIT:/>' ) ) <EOL> exec ( _gen_binop ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def substitute ( a ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( a , BaseParameter ) : <EOL> return float ( a . value ) <EOL> elif isinstance ( a , tuple ) : <EOL> return tuple ( substitute ( v ) for v in a ) <EOL> elif isinstance ( a , list ) : <EOL> return [ substitute ( v ) for v in a ] <EOL> elif isinstance ( a , dict ) : <EOL> return dict ( ( k , substitute ( v ) ) for k , v in a . items ( ) ) <EOL> else : <EOL> return a <EOL> class Function ( BaseParameter ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = [ '<STR_LIT>' , '<STR_LIT:args>' , '<STR_LIT>' ] <EOL> def __init__ ( self , op , * args , ** kw ) : <EOL> self . name = kw . pop ( '<STR_LIT:name>' , None ) <EOL> self . op , self . args , self . kw = op , args , kw <EOL> def parameters ( self ) : <EOL> deps = flatten ( ( self . args , self . kw ) ) <EOL> res = [ ] <EOL> for p in deps : <EOL> res . extend ( p . parameters ( ) ) <EOL> return res <EOL> def _value ( self ) : <EOL> return self . op ( * substitute ( self . args ) , ** substitute ( self . kw ) ) <EOL> value = property ( _value ) <EOL> def __getstate__ ( self ) : <EOL> return self . name , self . op , self . args , self . kw <EOL> def __setstate__ ( self , state ) : <EOL> self . name , self . op , self . args , self . kw = state <EOL> def __str__ ( self ) : <EOL> if self . name is not None : <EOL> name = self . name <EOL> else : <EOL> args = [ str ( v ) for v in self . args ] <EOL> kw = [ str ( k ) + "<STR_LIT:=>" + str ( v ) for k , v in self . kw . items ( ) ] <EOL> name = self . op . __name__ + "<STR_LIT:(>" + "<STR_LIT:U+002CU+0020>" . join ( args + kw ) + "<STR_LIT:)>" <EOL> return "<STR_LIT>" % ( name , self . value ) <EOL> def function ( op ) : <EOL> """<STR_LIT>""" <EOL> def function_generator ( * args , ** kw ) : <EOL> return Function ( op , * args , ** kw ) <EOL> function_generator . __name__ = op . __name__ <EOL> function_generator . __doc__ = op . __doc__ <EOL> return function_generator <EOL> _abs = function ( abs ) <EOL> def flatten ( s ) : <EOL> if isinstance ( s , ( tuple , list ) ) : <EOL> return reduce ( lambda a , b : a + flatten ( b ) , s , [ ] ) <EOL> elif isinstance ( s , set ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif isinstance ( s , dict ) : <EOL> return reduce ( lambda a , b : a + flatten ( s [ b ] ) , sorted ( s . keys ( ) ) , [ ] ) <EOL> elif isinstance ( s , BaseParameter ) : <EOL> return [ s ] <EOL> elif s is None : <EOL> return [ ] <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % ( type ( s ) , s ) ) <EOL> def format ( p , indent = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( p , dict ) and p != { } : <EOL> res = [ ] <EOL> for k in sorted ( p . keys ( ) ) : <EOL> if k . startswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> s = format ( p [ k ] , indent + <NUM_LIT:2> ) <EOL> label = "<STR_LIT:U+0020>" * indent + "<STR_LIT:.>" + k <EOL> if s . endswith ( '<STR_LIT:\n>' ) : <EOL> res . append ( label + "<STR_LIT:\n>" + s ) <EOL> else : <EOL> res . append ( label + "<STR_LIT>" + s + '<STR_LIT:\n>' ) <EOL> if '<STR_LIT>' in p : <EOL> res . append ( format ( p [ '<STR_LIT>' ] , indent ) ) <EOL> return "<STR_LIT>" . join ( res ) <EOL> elif isinstance ( p , list ) and p != [ ] : <EOL> res = [ ] <EOL> for k , v in enumerate ( p ) : <EOL> s = format ( v , indent + <NUM_LIT:2> ) <EOL> label = "<STR_LIT:U+0020>" * indent + "<STR_LIT>" % k <EOL> if s . endswith ( '<STR_LIT:\n>' ) : <EOL> res . append ( label + '<STR_LIT:\n>' + s ) <EOL> else : <EOL> res . append ( label + '<STR_LIT>' + s + '<STR_LIT:\n>' ) <EOL> return "<STR_LIT>" . join ( res ) <EOL> elif isinstance ( p , Parameter ) : <EOL> if p . fixed : <EOL> bounds = "<STR_LIT>" <EOL> else : <EOL> bounds = "<STR_LIT>" % p . bounds . limits <EOL> return "<STR_LIT>" % ( p . value , str ( p ) , bounds ) <EOL> elif isinstance ( p , BaseParameter ) : <EOL> return str ( p ) <EOL> else : <EOL> return "<STR_LIT:None>" <EOL> def summarize ( pars , sorted = False ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> if sorted : <EOL> pars = sorted ( pars , cmp = lambda x , y : cmp ( x . name , y . name ) ) <EOL> for p in pars : <EOL> if not isfinite ( p . value ) : <EOL> bar = "<STR_LIT>" <EOL> else : <EOL> position = int ( p . bounds . get01 ( p . value ) * <NUM_LIT> ) <EOL> bar = [ '<STR_LIT:.>' ] * <NUM_LIT:10> <EOL> if position < <NUM_LIT:0> : <EOL> bar [ <NUM_LIT:0> ] = '<STR_LIT:<>' <EOL> elif position > <NUM_LIT:9> : <EOL> bar [ <NUM_LIT:9> ] = '<STR_LIT:>>' <EOL> else : <EOL> bar [ position ] = '<STR_LIT:|>' <EOL> output . append ( "<STR_LIT>" % <EOL> ( p . name , "<STR_LIT>" . join ( bar ) , p . value , p . bounds ) ) <EOL> return "<STR_LIT:\n>" . join ( output ) <EOL> def unique ( s ) : <EOL> """<STR_LIT>""" <EOL> pars = flatten ( s ) <EOL> pars = pars + flatten ( [ p . parameters ( ) for p in pars ] ) <EOL> result = [ ] <EOL> for p in pars : <EOL> if not any ( p is q for q in result ) : <EOL> result . append ( p ) <EOL> return result <EOL> def fittable ( s ) : <EOL> """<STR_LIT>""" <EOL> return [ p for p in unique ( s ) if not p . fittable ] <EOL> def varying ( s ) : <EOL> """<STR_LIT>""" <EOL> return [ p for p in unique ( s ) if not p . fixed ] <EOL> def randomize ( s ) : <EOL> """<STR_LIT>""" <EOL> for p in s : <EOL> p . value = p . bounds . random ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> def current ( s ) : <EOL> return [ p . value for p in s ] <EOL> class IntegerParameter ( Parameter ) : <EOL> discrete = True <EOL> def _get_value ( self ) : <EOL> return self . _value <EOL> def _set_value ( self , value ) : <EOL> self . _value = int ( value ) <EOL> value = property ( _get_value , _set_value ) <EOL> class Alias ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , attr , p = None , name = None ) : <EOL> self . obj = obj <EOL> self . attr = attr <EOL> if name is None : <EOL> name = "<STR_LIT:.>" . join ( [ obj . __class__ . __name__ , attr ] ) <EOL> self . p = Parameter . default ( p , name = name ) <EOL> def update ( self ) : <EOL> setattr ( self . obj , self . attr , self . p . value ) <EOL> def parameters ( self ) : <EOL> return self . p . parameters ( ) </s>
