<s> import Utils , Maps , numpy as np , scipy . sparse as sp <EOL> class Property ( object ) : <EOL> name = '<STR_LIT>' <EOL> doc = '<STR_LIT>' <EOL> defaultVal = None <EOL> defaultInvProp = False <EOL> def __init__ ( self , doc , ** kwargs ) : <EOL> self . doc = doc <EOL> Utils . setKwargs ( self , ** kwargs ) <EOL> @ property <EOL> def propertyLink ( self ) : <EOL> "<STR_LIT>" <EOL> return getattr ( self , '<STR_LIT>' , None ) <EOL> @ propertyLink . setter <EOL> def propertyLink ( self , value ) : <EOL> assert type ( value ) is tuple and len ( value ) == <NUM_LIT:2> and type ( value [ <NUM_LIT:0> ] ) is str and issubclass ( value [ <NUM_LIT:1> ] , Maps . IdentityMap ) , '<STR_LIT>' % self . name <EOL> self . _propertyLink = value <EOL> def _getMapProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> return getattr ( self , '<STR_LIT>' % prop . name , None ) <EOL> def fset ( self , val ) : <EOL> if prop . propertyLink is not None : <EOL> linkName , linkMap = prop . propertyLink <EOL> assert getattr ( self , '<STR_LIT>' % linkName , None ) is None , '<STR_LIT>' <EOL> setattr ( self , '<STR_LIT>' % prop . name , val ) <EOL> return property ( fget = fget , fset = fset , doc = prop . doc ) <EOL> def _getIndexProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> return getattr ( self , '<STR_LIT>' % prop . name , slice ( None ) ) <EOL> def fset ( self , val ) : <EOL> setattr ( self , '<STR_LIT>' % prop . name , val ) <EOL> return property ( fget = fget , fset = fset , doc = prop . doc ) <EOL> def _getProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> mapping = getattr ( self , '<STR_LIT>' % prop . name ) <EOL> if mapping is None and prop . propertyLink is None : <EOL> return prop . defaultVal <EOL> if mapping is None and prop . propertyLink is not None : <EOL> linkName , linkMapClass = prop . propertyLink <EOL> linkMap = linkMapClass ( None ) <EOL> if getattr ( self , '<STR_LIT>' % linkName , None ) is None : <EOL> return prop . defaultVal <EOL> m = getattr ( self , '<STR_LIT:%s>' % linkName ) <EOL> return linkMap * m <EOL> m = getattr ( self , '<STR_LIT>' % prop . name ) <EOL> return mapping * m <EOL> return property ( fget = fget ) <EOL> def _getModelDerivProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> mapping = getattr ( self , '<STR_LIT>' % prop . name ) <EOL> if mapping is None and prop . propertyLink is None : <EOL> return None <EOL> if mapping is None and prop . propertyLink is not None : <EOL> linkName , linkMapClass = prop . propertyLink <EOL> linkedMap = getattr ( self , '<STR_LIT>' % linkName ) <EOL> if linkedMap is None : <EOL> return None <EOL> linkMap = linkMapClass ( None ) * linkedMap <EOL> m = getattr ( self , '<STR_LIT:%s>' % linkName ) <EOL> return linkMap . deriv ( m ) <EOL> m = getattr ( self , '<STR_LIT>' % prop . name ) <EOL> return mapping . deriv ( m ) <EOL> return property ( fget = fget ) <EOL> def _getModelProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> mapping = getattr ( self , '<STR_LIT>' % prop . name ) <EOL> if mapping is None : <EOL> return None <EOL> index = getattr ( self . propMap , '<STR_LIT>' % prop . name ) <EOL> return self . vector [ index ] <EOL> return property ( fget = fget ) <EOL> def _getModelProjProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> mapping = getattr ( self , '<STR_LIT>' % prop . name ) <EOL> if mapping is None : <EOL> return None <EOL> inds = getattr ( self . propMap , '<STR_LIT>' % prop . name ) <EOL> if type ( inds ) is slice : <EOL> inds = range ( * inds . indices ( self . nP ) ) <EOL> nI , nP = len ( inds ) , self . nP <EOL> return sp . csr_matrix ( ( np . ones ( nI ) , ( range ( nI ) , inds ) ) , shape = ( nI , nP ) ) <EOL> return property ( fget = fget ) <EOL> def _getModelMapProperty ( self ) : <EOL> prop = self <EOL> def fget ( self ) : <EOL> return getattr ( self . propMap , '<STR_LIT>' % prop . name , None ) <EOL> return property ( fget = fget ) <EOL> class PropModel ( object ) : <EOL> def __init__ ( self , propMap , vector ) : <EOL> self . propMap = propMap <EOL> self . vector = vector <EOL> assert len ( self . vector ) == self . nP <EOL> @ property <EOL> def nP ( self ) : <EOL> inds = [ ] <EOL> if getattr ( self , '<STR_LIT>' , None ) is None : <EOL> for name in self . propMap . _properties : <EOL> index = getattr ( self . propMap , '<STR_LIT>' % name , None ) <EOL> if index is not None : <EOL> if type ( index ) is slice : <EOL> inds += range ( * index . indices ( len ( self . vector ) ) ) <EOL> else : <EOL> inds += list ( index ) <EOL> self . _nP = len ( set ( inds ) ) <EOL> return self . _nP <EOL> def __contains__ ( self , val ) : <EOL> return val in self . propMap <EOL> _PROPMAPCLASSREGISTRY = { } <EOL> class _PropMapMetaClass ( type ) : <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> assert name . endswith ( '<STR_LIT>' ) , '<STR_LIT>' <EOL> _properties = { } <EOL> for base in bases : <EOL> for baseProp in getattr ( base , '<STR_LIT>' , { } ) : <EOL> _properties [ baseProp ] = base . _properties [ baseProp ] <EOL> keys = [ key for key in attrs ] <EOL> for attr in keys : <EOL> if isinstance ( attrs [ attr ] , Property ) : <EOL> attrs [ attr ] . name = attr <EOL> attrs [ attr + '<STR_LIT>' ] = attrs [ attr ] . _getMapProperty ( ) <EOL> attrs [ attr + '<STR_LIT>' ] = attrs [ attr ] . _getIndexProperty ( ) <EOL> _properties [ attr ] = attrs [ attr ] <EOL> attrs . pop ( attr ) <EOL> attrs [ '<STR_LIT>' ] = _properties <EOL> defaultInvProps = [ ] <EOL> for p in _properties : <EOL> prop = _properties [ p ] <EOL> if prop . defaultInvProp : <EOL> defaultInvProps += [ p ] <EOL> if prop . propertyLink is not None : <EOL> assert prop . propertyLink [ <NUM_LIT:0> ] in _properties , "<STR_LIT>" % ( prop . name , prop . propertyLink [ <NUM_LIT:0> ] ) <EOL> if len ( defaultInvProps ) > <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' % defaultInvProps ) <EOL> newClass = super ( _PropMapMetaClass , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> newClass . PropModel = cls . createPropModelClass ( newClass , name , _properties ) <EOL> _PROPMAPCLASSREGISTRY [ name ] = newClass <EOL> return newClass <EOL> def createPropModelClass ( self , name , _properties ) : <EOL> attrs = dict ( ) <EOL> for attr in _properties : <EOL> prop = _properties [ attr ] <EOL> attrs [ attr ] = prop . _getProperty ( ) <EOL> attrs [ attr + '<STR_LIT>' ] = prop . _getModelMapProperty ( ) <EOL> attrs [ attr + '<STR_LIT>' ] = prop . _getModelProjProperty ( ) <EOL> attrs [ attr + '<STR_LIT>' ] = prop . _getModelProperty ( ) <EOL> attrs [ attr + '<STR_LIT>' ] = prop . _getModelDerivProperty ( ) <EOL> return type ( name . replace ( '<STR_LIT>' , '<STR_LIT>' ) , ( PropModel , ) , attrs ) <EOL> class PropMap ( object ) : <EOL> __metaclass__ = _PropMapMetaClass <EOL> def __init__ ( self , mappings ) : <EOL> """<STR_LIT>""" <EOL> if type ( mappings ) is dict : <EOL> assert np . all ( [ k in [ '<STR_LIT>' , '<STR_LIT>' ] for k in mappings ] ) , '<STR_LIT>' <EOL> self . setup ( mappings [ '<STR_LIT>' ] , slices = mappings [ '<STR_LIT>' ] ) <EOL> elif type ( mappings ) is list : <EOL> self . setup ( mappings ) <EOL> elif isinstance ( mappings , Maps . IdentityMap ) : <EOL> self . setup ( [ ( self . defaultInvProp , mappings ) ] ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def setup ( self , maps , slices = None ) : <EOL> """<STR_LIT>""" <EOL> assert np . all ( [ <EOL> type ( m ) is tuple and <EOL> len ( m ) == <NUM_LIT:2> and <EOL> type ( m [ <NUM_LIT:0> ] ) is str and <EOL> m [ <NUM_LIT:0> ] in self . _properties and <EOL> isinstance ( m [ <NUM_LIT:1> ] , Maps . IdentityMap ) <EOL> for m in maps ] ) , "<STR_LIT>" % ( '<STR_LIT:U+002CU+0020>' . join ( [ "<STR_LIT>" % ( p , p ) for p in self . _properties ] ) ) <EOL> if slices is None : <EOL> slices = dict ( ) <EOL> else : <EOL> assert np . all ( [ <EOL> s in self . _properties and <EOL> ( type ( slices [ s ] ) in [ slice , list ] or isinstance ( slices [ s ] , np . ndarray ) ) <EOL> for s in slices ] ) , '<STR_LIT>' <EOL> self . clearMaps ( ) <EOL> nP = <NUM_LIT:0> <EOL> for name , mapping in maps : <EOL> setattr ( self , '<STR_LIT>' % name , mapping ) <EOL> setattr ( self , '<STR_LIT>' % name , slices . get ( name , slice ( nP , nP + mapping . nP ) ) ) <EOL> nP += mapping . nP <EOL> self . nP = nP <EOL> @ property <EOL> def defaultInvProp ( self ) : <EOL> for name in self . _properties : <EOL> p = self . _properties [ name ] <EOL> if p . defaultInvProp : <EOL> return p . name <EOL> def clearMaps ( self ) : <EOL> for name in self . _properties : <EOL> setattr ( self , '<STR_LIT>' % name , None ) <EOL> setattr ( self , '<STR_LIT>' % name , None ) <EOL> def __call__ ( self , vec ) : <EOL> return self . PropModel ( self , vec ) <EOL> def __contains__ ( self , val ) : <EOL> activeMaps = [ name for name in self . _properties if getattr ( self , '<STR_LIT>' % name ) is not None ] <EOL> return val in activeMaps </s>
