00001 """
00002 Parameter class is equivalent to a header file for data
00003 """
00004
00005 __copyright__ = """
00006 Copyright 2008 Sean Ross-Ross
00007 """
00008 __license__ = """
00009 This file is part of SLIMpy .
00010
00011 SLIMpy is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU Lesser General Public License as published by
00013 the Free Software Foundation, either version 3 of the License, or
00014 (at your option) any later version.
00015
00016 SLIMpy is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 GNU Lesser General Public License for more details.
00020
00021 You should have received a copy of the GNU Lesser General Public License
00022 along with SLIMpy . If not, see <http://www.gnu.org/licenses/>.
00023 """
00024
00025
00026 class _param( dict ):
00027 """
00028 parameter is a simple tracking method to pass Metadata without
00029 being bound to each specific datatype
00030 """
00031
00032 def makeContaner( self, command=None ):
00033 """
00034 Make a new container from the current parameters
00035
00036 """
00037 return self["plugin"]( parameters=self, command=command )
00038
00039 def newParameter( self, **keys ):
00040 """
00041 Make a new parameter updated with the new keys, 'keys'
00042 """
00043 x = param( self )
00044 x.update( keys )
00045 return self.__class__( **x )
00046
00047 def getParameters( self ):
00048 """
00049 for consistency returns this instance
00050 """
00051 return self
00052
00053 def copy( self ):
00054 return param( dict.copy( self ) )
00055
00056 def shape( self ):
00057 """
00058 Returns a list of the dimensions of the image of the underlying vector
00059 """
00060 shp = []
00061
00062 N = 1
00063 while self.has_key( "n%s" %N ):
00064 val = self["n%s" %N]
00065 if val < 1:
00066 raise TypeError( "shape parameter does not conform to SLIMpy standard:\n"
00067 "Should be an int greater than 0" )
00068 shp.append( val )
00069 N += 1
00070
00071 if N is 1:
00072 raise TypeError( "space has no shape\n"
00073 "Should contain values of n1, n2, etc." )
00074
00075 i = len( shp ) -1
00076 while i > 1 and shp[i] is 1:
00077 shp.pop( -1 )
00078 i -= 1
00079
00080 return shp
00081
00082
00083 def _get_size(self):
00084 mul = lambda x,y:x*y
00085 prod = lambda shape:reduce( mul, shape, 1 )
00086 shape = self.shape_
00087 if not shape:
00088 return 0
00089 else:
00090 return prod(shape)
00091
00092 size = property( _get_size )
00093
00094 def union(self,*E,**kw):
00095 '''
00096 par.union(par2 [, par3 ...] ,**kw ) --> param
00097 returns a new space that is a subspace of par and
00098 par2 ... and kw
00099 '''
00100 if kw:
00101 new = self._unionhelper(kw)
00102 else:
00103 new =self
00104
00105 for kw in E:
00106 new = new._unionhelper(kw)
00107
00108 return new
00109
00110 def _unionhelper( self, kw ):
00111 '''
00112 returns a new param that is
00113 '''
00114
00115 set_self = set(self.keys() )
00116 set_other = set(kw.keys() )
00117
00118
00119 union = set_self.union(set_other)
00120 diff1 = set_self.difference( set_other )
00121 diff2 = set_other.difference( set_self )
00122
00123 new = param()
00124 for key in union:
00125 v1 = self[key];v2 = kw[key]
00126 if not self[key] == kw[key]:
00127 raise ValueError("item self[%(key)s] != other[%(key)s] ; %(v1)s != %(v2)s")
00128 new[key] = self[key]
00129
00130 for key in diff1:
00131 new[key] = self[key]
00132 for key in diff2:
00133 new[key] = kw[key]
00134
00135 def is_subspace(self,other ):
00136
00137 key_set = set( self.keys() )
00138 key_set_other = set( other.keys() )
00139
00140
00141 issubset = key_set_other.issubset( key_set )
00142
00143 if not issubset:
00144 return False
00145
00146 for key in key_set_other:
00147 if not self[key] == other[key]:
00148 return False
00149
00150 return True