<s> '''<STR_LIT>''' <EOL> from __future__ import division <EOL> import re <EOL> import math <EOL> from math import sqrt , log <EOL> import copy <EOL> import warnings <EOL> __version_info__ = ( <NUM_LIT:1> , <NUM_LIT:9> ) <EOL> __version__ = '<STR_LIT:.>' . join ( map ( str , __version_info__ ) ) <EOL> __author__ = '<STR_LIT>' <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> def set_doc ( doc_string ) : <EOL> """<STR_LIT>""" <EOL> def set_doc_string ( func ) : <EOL> func . __doc__ = doc_string <EOL> return func <EOL> return set_doc_string <EOL> CONSTANT_TYPES = ( float , int , complex ) <EOL> def deprecation ( message ) : <EOL> '''<STR_LIT>''' <EOL> warnings . warn ( message , DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> try : <EOL> import numpy <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> CONSTANT_TYPES += ( numpy . number , ) <EOL> def correlated_values ( nom_values , covariance_mat , tags = None ) : <EOL> """<STR_LIT>""" <EOL> if tags is None : <EOL> tags = ( None , ) * len ( nom_values ) <EOL> ( variances , transform ) = numpy . linalg . eigh ( covariance_mat ) <EOL> variances [ variances < <NUM_LIT:0> ] = <NUM_LIT:0.> <EOL> variables = tuple ( <EOL> Variable ( <NUM_LIT:0> , sqrt ( variance ) , tag ) <EOL> for ( variance , tag ) in zip ( variances , tags ) ) <EOL> values_funcs = tuple ( <EOL> AffineScalarFunc ( value , dict ( zip ( variables , coords ) ) ) <EOL> for ( coords , value ) in zip ( transform , nom_values ) ) <EOL> return values_funcs <EOL> __all__ . append ( '<STR_LIT>' ) <EOL> def correlated_values_norm ( values_with_std_dev , correlation_mat , <EOL> tags = None ) : <EOL> '''<STR_LIT>''' <EOL> ( nominal_values , std_devs ) = numpy . transpose ( values_with_std_dev ) <EOL> return correlated_values ( <EOL> nominal_values , <EOL> correlation_mat * std_devs * std_devs [ numpy . newaxis ] . T , <EOL> tags ) <EOL> __all__ . append ( '<STR_LIT>' ) <EOL> class NotUpcast ( Exception ) : <EOL> '<STR_LIT>' <EOL> def to_affine_scalar ( x ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( x , AffineScalarFunc ) : <EOL> return x <EOL> if isinstance ( x , CONSTANT_TYPES ) : <EOL> return AffineScalarFunc ( x , { } ) <EOL> raise NotUpcast ( "<STR_LIT>" <EOL> "<STR_LIT>" % type ( x ) ) <EOL> def partial_derivative ( f , param_num ) : <EOL> """<STR_LIT>""" <EOL> def partial_derivative_of_f ( * args , ** kws ) : <EOL> """<STR_LIT>""" <EOL> param_kw = None <EOL> if '<STR_LIT>' in kws : <EOL> param_kw = kws . pop ( '<STR_LIT>' ) <EOL> shifted_args = list ( args ) <EOL> shifted_kws = { } <EOL> for k , v in kws . items ( ) : <EOL> shifted_kws [ k ] = v <EOL> step = <NUM_LIT> <EOL> if param_kw in shifted_kws : <EOL> step = step * abs ( shifted_kws [ param_kw ] ) <EOL> elif param_num < len ( shifted_args ) : <EOL> step = step * abs ( shifted_args [ param_num ] ) <EOL> if param_kw in shifted_kws : <EOL> shifted_kws [ param_kw ] += step <EOL> elif param_num < len ( shifted_args ) : <EOL> shifted_args [ param_num ] += step <EOL> shifted_f_plus = f ( * shifted_args , ** shifted_kws ) <EOL> if param_kw in shifted_kws : <EOL> shifted_kws [ param_kw ] -= <NUM_LIT:2> * step <EOL> elif param_num < len ( shifted_args ) : <EOL> shifted_args [ param_num ] -= <NUM_LIT:2> * step <EOL> shifted_f_minus = f ( * shifted_args , ** shifted_kws ) <EOL> return ( shifted_f_plus - shifted_f_minus ) / <NUM_LIT:2> / step <EOL> return partial_derivative_of_f <EOL> class NumericalDerivatives ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , function ) : <EOL> """<STR_LIT>""" <EOL> self . _function = function <EOL> def __getitem__ ( self , n ) : <EOL> """<STR_LIT>""" <EOL> return partial_derivative ( self . _function , n ) <EOL> def wrap ( f , derivatives_iter = None ) : <EOL> """<STR_LIT>""" <EOL> if derivatives_iter is None : <EOL> derivatives_iter = NumericalDerivatives ( f ) <EOL> else : <EOL> try : <EOL> len ( derivatives_iter ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> derivatives_iter = [ <EOL> partial_derivative ( f , k ) if derivative is None <EOL> else derivative <EOL> for ( k , derivative ) in enumerate ( derivatives_iter ) ] <EOL> @ set_doc ( """<STR_LIT>""" % ( f . __name__ , f . __doc__ ) ) <EOL> def f_with_affine_output ( * args , ** kwargs ) : <EOL> try : <EOL> old_funcs = map ( to_affine_scalar , args ) <EOL> aff_funcs = [ to_affine_scalar ( a ) for a in args ] <EOL> aff_kws = kwargs <EOL> aff_varkws = [ ] <EOL> for key , val in kwargs . items ( ) : <EOL> if isinstance ( val , Variable ) : <EOL> aff_kws [ key ] = to_affine_scalar ( val ) <EOL> aff_varkws . append ( key ) <EOL> except NotUpcast : <EOL> if any ( isinstance ( arg , AffineScalarFunc ) for arg in args ) : <EOL> return NotImplemented <EOL> else : <EOL> return f ( * args , ** kwargs ) <EOL> args_values = [ e . nominal_value for e in aff_funcs ] <EOL> kw_values = { } <EOL> for key , val in aff_kws . items ( ) : <EOL> kw_values [ key ] = val <EOL> if key in aff_varkws : <EOL> kw_values [ key ] = val . nominal_value <EOL> f_nominal_value = f ( * args_values , ** kw_values ) <EOL> variables = set ( ) <EOL> for expr in aff_funcs : <EOL> variables |= set ( expr . derivatives ) <EOL> for vname in aff_varkws : <EOL> variables |= set ( aff_kws [ vname ] . derivatives ) <EOL> if not variables or isinstance ( f_nominal_value , bool ) : <EOL> return f_nominal_value <EOL> derivatives_wrt_args = [ ] <EOL> for ( arg , derivative ) in zip ( aff_funcs , derivatives_iter ) : <EOL> derivatives_wrt_args . append ( derivative ( * args_values , ** aff_kws ) <EOL> if arg . derivatives <EOL> else <NUM_LIT:0> ) <EOL> kws_values = [ ] <EOL> for vname in aff_varkws : <EOL> kws_values . append ( aff_kws [ vname ] . nominal_value ) <EOL> for ( vname , derivative ) in zip ( aff_varkws , derivatives_iter ) : <EOL> derivatives_wrt_args . append ( derivative ( __param__kw__ = vname , <EOL> ** kw_values ) <EOL> if aff_kws [ vname ] . derivatives <EOL> else <NUM_LIT:0> ) <EOL> derivatives_wrt_vars = dict ( ( var , <NUM_LIT:0.> ) for var in variables ) <EOL> for ( func , f_derivative ) in zip ( aff_funcs , derivatives_wrt_args ) : <EOL> for ( var , func_derivative ) in func . derivatives . items ( ) : <EOL> derivatives_wrt_vars [ var ] += f_derivative * func_derivative <EOL> for ( vname , f_derivative ) in zip ( aff_varkws , derivatives_wrt_args ) : <EOL> func = aff_kws [ vname ] <EOL> for ( var , func_derivative ) in func . derivatives . items ( ) : <EOL> derivatives_wrt_vars [ var ] += f_derivative * func_derivative <EOL> return AffineScalarFunc ( f_nominal_value , derivatives_wrt_vars ) <EOL> f_with_affine_output . __name__ = f . __name__ <EOL> return f_with_affine_output <EOL> def _force_aff_func_args ( func ) : <EOL> """<STR_LIT>""" <EOL> def op_on_upcast_args ( x , y ) : <EOL> """<STR_LIT>""" % func . __name__ <EOL> try : <EOL> y_with_uncert = to_affine_scalar ( y ) <EOL> except NotUpcast : <EOL> return NotImplemented <EOL> else : <EOL> return func ( x , y_with_uncert ) <EOL> return op_on_upcast_args <EOL> def _eq_on_aff_funcs ( self , y_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> difference = self - y_with_uncert <EOL> return not ( difference . _nominal_value or difference . std_dev ( ) ) <EOL> def _ne_on_aff_funcs ( self , y_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> return not _eq_on_aff_funcs ( self , y_with_uncert ) <EOL> def _gt_on_aff_funcs ( self , y_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> return self . _nominal_value > y_with_uncert . _nominal_value <EOL> def _ge_on_aff_funcs ( self , y_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> return ( _gt_on_aff_funcs ( self , y_with_uncert ) <EOL> or _eq_on_aff_funcs ( self , y_with_uncert ) ) <EOL> def _lt_on_aff_funcs ( self , y_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> return self . _nominal_value < y_with_uncert . _nominal_value <EOL> def _le_on_aff_funcs ( self , y_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> return ( _lt_on_aff_funcs ( self , y_with_uncert ) <EOL> or _eq_on_aff_funcs ( self , y_with_uncert ) ) <EOL> class AffineScalarFunc ( object ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , nominal_value , derivatives ) : <EOL> """<STR_LIT>""" <EOL> self . _nominal_value = float ( nominal_value ) <EOL> self . derivatives = derivatives <EOL> @ property <EOL> def nominal_value ( self ) : <EOL> "<STR_LIT>" <EOL> return self . _nominal_value <EOL> def __nonzero__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self != <NUM_LIT:0.> <EOL> __eq__ = _force_aff_func_args ( _eq_on_aff_funcs ) <EOL> __ne__ = _force_aff_func_args ( _ne_on_aff_funcs ) <EOL> __gt__ = _force_aff_func_args ( _gt_on_aff_funcs ) <EOL> __ge__ = _force_aff_func_args ( _ge_on_aff_funcs ) <EOL> __lt__ = _force_aff_func_args ( _lt_on_aff_funcs ) <EOL> __le__ = _force_aff_func_args ( _le_on_aff_funcs ) <EOL> def error_components ( self ) : <EOL> """<STR_LIT>""" <EOL> error_components = { } <EOL> for ( variable , derivative ) in self . derivatives . items ( ) : <EOL> error_components [ variable ] = abs ( derivative * variable . _std_dev ) <EOL> return error_components <EOL> def std_dev ( self ) : <EOL> """<STR_LIT>""" <EOL> return sqrt ( sum ( <EOL> delta ** <NUM_LIT:2> for delta in self . error_components ( ) . values ( ) ) ) <EOL> def _general_representation ( self , to_string ) : <EOL> """<STR_LIT>""" <EOL> ( nominal_value , std_dev ) = ( self . _nominal_value , self . std_dev ( ) ) <EOL> return ( "<STR_LIT>" % ( to_string ( nominal_value ) , to_string ( std_dev ) ) <EOL> if std_dev <EOL> else to_string ( nominal_value ) ) <EOL> def __repr__ ( self ) : <EOL> return self . _general_representation ( repr ) <EOL> def __str__ ( self ) : <EOL> return self . _general_representation ( str ) <EOL> def std_score ( self , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return ( value - self . _nominal_value ) / self . std_dev ( ) <EOL> except ZeroDivisionError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> """<STR_LIT>""" <EOL> return AffineScalarFunc ( <EOL> self . _nominal_value , <EOL> dict ( ( copy . deepcopy ( var ) , deriv ) <EOL> for ( var , deriv ) in self . derivatives . items ( ) ) ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> obj_slot_values = dict ( ( k , getattr ( self , k ) ) for k in <EOL> AffineScalarFunc . __slots__ ) <EOL> return obj_slot_values <EOL> def __setstate__ ( self , data_dict ) : <EOL> """<STR_LIT>""" <EOL> for ( name , value ) in data_dict . items ( ) : <EOL> setattr ( self , name , value ) <EOL> UFloat = AffineScalarFunc <EOL> def get_ops_with_reflection ( ) : <EOL> """<STR_LIT>""" <EOL> derivatives_list = { <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT:y>" , "<STR_LIT:x>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> '<STR_LIT>' : ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> } <EOL> ops_with_reflection = { } <EOL> for ( op , derivatives ) in derivatives_list . items ( ) : <EOL> ops_with_reflection [ op ] = [ <EOL> eval ( "<STR_LIT>" % expr ) for expr in derivatives ] <EOL> ops_with_reflection [ "<STR_LIT:r>" + op ] = [ <EOL> eval ( "<STR_LIT>" % expr ) for expr in reversed ( derivatives ) ] <EOL> return ops_with_reflection <EOL> _ops_with_reflection = get_ops_with_reflection ( ) <EOL> _modified_operators = [ ] <EOL> _modified_ops_with_reflection = [ ] <EOL> def add_operators_to_AffineScalarFunc ( ) : <EOL> """<STR_LIT>""" <EOL> simple_numerical_operators_derivatives = { <EOL> '<STR_LIT>' : lambda x : <NUM_LIT:1.> if x >= <NUM_LIT:0> else - <NUM_LIT:1.> , <EOL> '<STR_LIT>' : lambda x : - <NUM_LIT:1.> , <EOL> '<STR_LIT>' : lambda x : <NUM_LIT:1.> , <EOL> '<STR_LIT>' : lambda x : <NUM_LIT:0.> <EOL> } <EOL> for ( op , derivative ) in ( <EOL> simple_numerical_operators_derivatives . items ( ) ) : <EOL> attribute_name = "<STR_LIT>" % op <EOL> try : <EOL> setattr ( AffineScalarFunc , attribute_name , <EOL> wrap ( getattr ( float , attribute_name ) , <EOL> [ derivative ] ) ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> _modified_operators . append ( op ) <EOL> for ( op , derivatives ) in _ops_with_reflection . items ( ) : <EOL> attribute_name = '<STR_LIT>' % op <EOL> try : <EOL> setattr ( AffineScalarFunc , attribute_name , <EOL> wrap ( getattr ( float , attribute_name ) , derivatives ) ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> _modified_ops_with_reflection . append ( op ) <EOL> for coercion_type in ( '<STR_LIT>' , '<STR_LIT:int>' , '<STR_LIT>' , '<STR_LIT:float>' ) : <EOL> def raise_error ( self ) : <EOL> raise TypeError ( "<STR_LIT>" <EOL> '<STR_LIT>' <EOL> % ( self . __class__ , coercion_type ) ) <EOL> setattr ( AffineScalarFunc , '<STR_LIT>' % coercion_type , raise_error ) <EOL> add_operators_to_AffineScalarFunc ( ) <EOL> class Variable ( AffineScalarFunc ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , value , std_dev , tag = None ) : <EOL> """<STR_LIT>""" <EOL> value = float ( value ) <EOL> super ( Variable , self ) . __init__ ( value , { self : <NUM_LIT:1.> } ) <EOL> assert std_dev >= <NUM_LIT:0> , "<STR_LIT>" <EOL> self . _std_dev = std_dev <EOL> self . tag = tag <EOL> def set_std_dev ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _std_dev = value <EOL> def _general_representation ( self , to_string ) : <EOL> """<STR_LIT>""" <EOL> num_repr = super ( Variable , self ) . _general_representation ( to_string ) <EOL> return ( num_repr if ( ( self . tag is None ) or ( to_string != repr ) ) <EOL> else "<STR_LIT>" % ( self . tag , num_repr ) ) <EOL> def __hash__ ( self ) : <EOL> return id ( self ) <EOL> def __copy__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return Variable ( self . nominal_value , self . std_dev ( ) , self . tag ) <EOL> def __deepcopy__ ( self , memo ) : <EOL> """<STR_LIT>""" <EOL> return self . __copy__ ( ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> obj_slot_values = dict ( ( k , getattr ( self , k ) ) for k in self . __slots__ ) <EOL> obj_slot_values . update ( AffineScalarFunc . __getstate__ ( self ) ) <EOL> return obj_slot_values <EOL> def __setstate__ ( self , data_dict ) : <EOL> """<STR_LIT>""" <EOL> for ( name , value ) in data_dict . items ( ) : <EOL> setattr ( self , name , value ) <EOL> def nominal_value ( x ) : <EOL> """<STR_LIT>""" <EOL> return x . nominal_value if isinstance ( x , AffineScalarFunc ) else x <EOL> def std_dev ( x ) : <EOL> """<STR_LIT>""" <EOL> return x . std_dev ( ) if isinstance ( x , AffineScalarFunc ) else <NUM_LIT:0.> <EOL> def covariance_matrix ( nums_with_uncert ) : <EOL> """<STR_LIT>""" <EOL> covariance_matrix = [ ] <EOL> for ( i1 , expr1 ) in enumerate ( nums_with_uncert ) : <EOL> derivatives1 = expr1 . derivatives <EOL> vars1 = set ( derivatives1 ) <EOL> coefs_expr1 = [ ] <EOL> for ( i2 , expr2 ) in enumerate ( nums_with_uncert [ : i1 + <NUM_LIT:1> ] ) : <EOL> derivatives2 = expr2 . derivatives <EOL> coef = <NUM_LIT:0.> <EOL> for var in vars1 . intersection ( derivatives2 ) : <EOL> coef += ( derivatives1 [ var ] * derivatives2 [ var ] * var . _std_dev ** <NUM_LIT:2> ) <EOL> coefs_expr1 . append ( coef ) <EOL> covariance_matrix . append ( coefs_expr1 ) <EOL> for ( i , covariance_coefs ) in enumerate ( covariance_matrix ) : <EOL> covariance_coefs . extend ( covariance_matrix [ j ] [ i ] <EOL> for j in range ( i + <NUM_LIT:1> , len ( covariance_matrix ) ) ) <EOL> return covariance_matrix <EOL> try : <EOL> import numpy <EOL> except ImportError : <EOL> pass <EOL> else : <EOL> def correlation_matrix ( nums_with_uncert ) : <EOL> '''<STR_LIT>''' <EOL> cov_mat = numpy . array ( covariance_matrix ( nums_with_uncert ) ) <EOL> std_devs = numpy . sqrt ( cov_mat . diagonal ( ) ) <EOL> return cov_mat / std_devs / std_devs [ numpy . newaxis ] . T <EOL> __all__ . append ( '<STR_LIT>' ) <EOL> POSITIVE_DECIMAL_UNSIGNED = r'<STR_LIT>' <EOL> NUMBER_WITH_UNCERT_RE_STR = '''<STR_LIT>''' % ( POSITIVE_DECIMAL_UNSIGNED , POSITIVE_DECIMAL_UNSIGNED ) <EOL> NUMBER_WITH_UNCERT_RE = re . compile ( <EOL> "<STR_LIT>" % NUMBER_WITH_UNCERT_RE_STR , re . VERBOSE ) <EOL> def parse_error_in_parentheses ( representation ) : <EOL> """<STR_LIT>""" <EOL> match = NUMBER_WITH_UNCERT_RE . search ( representation ) <EOL> if match : <EOL> ( sign , main_int , main_dec , uncert_int , uncert_dec , <EOL> exponent ) = match . groups ( ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % representation ) <EOL> value = float ( '<STR_LIT>' . join ( ( sign or '<STR_LIT>' , <EOL> main_int , <EOL> main_dec or '<STR_LIT>' , <EOL> exponent or '<STR_LIT>' ) ) ) <EOL> if uncert_int is None : <EOL> uncert_int = '<STR_LIT:1>' <EOL> if uncert_dec is not None : <EOL> uncert = float ( "<STR_LIT>" % ( uncert_int , uncert_dec or '<STR_LIT>' ) ) <EOL> else : <EOL> num_digits_after_period = ( <NUM_LIT:0> if main_dec is None <EOL> else len ( main_dec ) - <NUM_LIT:1> ) <EOL> uncert = int ( uncert_int ) / <NUM_LIT:10> ** num_digits_after_period <EOL> uncert *= float ( "<STR_LIT>" % ( exponent or '<STR_LIT>' ) ) <EOL> return ( value , uncert ) <EOL> def str_to_number_with_uncert ( representation ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ( value , uncert ) = representation . split ( '<STR_LIT>' ) <EOL> except ValueError : <EOL> parsed_value = parse_error_in_parentheses ( representation ) <EOL> else : <EOL> try : <EOL> parsed_value = ( float ( value ) , float ( uncert ) ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % representation ) <EOL> return parsed_value <EOL> def ufloat ( representation , tag = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( representation , basestring ) : <EOL> representation = str_to_number_with_uncert ( representation ) <EOL> if tag is not None : <EOL> assert isinstance ( tag , ( str , unicode ) ) , "<STR_LIT>" <EOL> return Variable ( * representation , ** { '<STR_LIT>' : tag } ) </s>
