00001 """
00002 Element-wise command converters
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 from slimpy_base.api.Plugins.slim2rsf.sfCommandFactory import rsfCommandFactory
00026 from slimpy_base.api.Plugins.slim2rsf.sfcommands.sfConverter import sfConverter
00027 from os.path import join
00028 from slimpy_base.Core.Command.Drivers.ooc_driver import OutOfCoreDriver
00029 from slimpy_base.Core.Interface.node import Source
00030 from pdb import set_trace
00031
00032 class sfmathFixture( sfConverter ):
00033 mathstr = ""
00034
00035 @classmethod
00036 def funcmap( cls ):
00037
00038 sfmath = join( cls.env['slimvars']['RSFBIN'], 'sfmath' )
00039 return OutOfCoreDriver( sfmath )
00040
00041 @classmethod
00042 def map( cls, source, command ):
00043
00044 command = cls.default_function( command, "math" )
00045 command = cls.place_adder( command )
00046
00047 command = cls.keep( command, ['output', 'vec'] )
00048 command = cls.mathhelper( command )
00049
00050
00051
00052 return cls.pack( command )
00053
00054
00055 @classmethod
00056 def mathhelper( cls, command ):
00057
00058 output = cls.mathstr
00059 kparams = command.kparams
00060 params= command.params
00061
00062 if kparams.has_key( 'vec' ):
00063 if '${val}'in output:
00064 kparams['output'] = output.replace( "${val}", 'vec' )
00065 else:
00066 if '${val}'in output:
00067 val = params.pop()
00068 if isinstance(val, Source):
00069 command.add_other_dep( val )
00070 val = val.data
00071 kparams['output'] = output.replace( "${val}", str( val ) )
00072 else:
00073 kparams['output'] = output
00074
00075 return command
00076
00077 @classmethod
00078 def constr( cls, command, *params ):
00079 cls.eqShape(command, params )
00080 cls.eqType( command, params )
00081 flt_or_cmpx = params[0]['data_type'] in ['float','complex']
00082 assert flt_or_cmpx , "data must be either 'float' or 'complex'"
00083 return
00084
00085 @classmethod
00086 def trans( cls, command, space, *Spaces ):
00087 return space
00088
00089 class all( object ):
00090 """
00091 container class for converters
00092 """
00093
00094 class radd( sfmathFixture ):
00095 mathstr = 'input+${val}'
00096 class add( sfmathFixture ):
00097 mathstr = '${val}+input'
00098 class mul( sfmathFixture ):
00099 mathstr = '${val}*input'
00100 class weightoper( sfmathFixture ):
00101 mathstr = '${val}*input'
00102 class rmul( sfmathFixture ):
00103 mathstr = 'input*${val}'
00104 class sub( sfmathFixture ):
00105 mathstr = 'input-${val}'
00106 class rsub( sfmathFixture ):
00107 mathstr = '${val}-input'
00108 class div( sfmathFixture ):
00109 mathstr = 'input/${val}'
00110 class rdiv( sfmathFixture ):
00111 mathstr = '${val}/input'
00112 class neg( sfmathFixture ):
00113 mathstr = '-input'
00114 class abs( sfmathFixture ):
00115 mathstr = 'abs(input)'
00116 class pow( sfmathFixture ):
00117 mathstr = 'input^${val}'
00118 class conj( sfmathFixture ):
00119 mathstr = 'conj(input)'
00120
00121 class real( sfConverter ):
00122 @classmethod
00123 def trans( cls, command, space, *spaces ):
00124
00125 space = cls.change( space, data_type='float' )
00126 return space
00127
00128 class create( sfConverter ):
00129 @classmethod
00130 def map( cls, source, command ):
00131
00132 c1, c2 = cls.split( command )
00133 c1 = cls.default_function( c1, 'math' )
00134 c1 = cls.keywordmap( c1, {'out':'output', 'data_type':'type'} )
00135 c1 = cls.keep( c1, ['output', 'type', 'n1', 'n2', 'n3', 'n4', 'n5'] )
00136
00137 c2 = cls.default_function( c2, 'put' )
00138 c2 = cls.discard( c2, ['Curvelet', 'out', 'esize', 'data_format', 'data_type', 'n1', 'n2', 'n3', 'n4', 'n5'] )
00139
00140
00141
00142
00143
00144
00145 clist = [c1]
00146 if c2.kparams:
00147 for key in c2.kparams:
00148 if c2[key].__str__()[0] == "(":
00149 c2[key] = c2[key].__str__().strip('()').replace(' ','')
00150 clist.append( c2 )
00151
00152 return cls.pack( clist )
00153
00154 class create_noise( sfConverter ):
00155 @classmethod
00156 def map( cls, source, command ):
00157
00158 c1, c2 = cls.split( command )
00159 c3, c4 = cls.split( c2 )
00160
00161 c1 = cls.default_function( c1, 'math' )
00162 c1 = cls.keywordmap( c1, {'out':'output', 'data_type':'type'} )
00163 c1 = cls.keep( c1, ['output', 'type', 'n1', 'n2', 'n3', 'n4', 'n5'] )
00164
00165 clist = [c1]
00166
00167 c2 = cls.default_function( c2, 'put' )
00168 c2 = cls.discard( c2, ['Curvelet', 'out', 'esize', 'data_format', 'data_type', 'n1', 'n2', 'n3', 'n4', 'n5', 'mean', 'seed'] )
00169
00170 if c2.kparams:
00171 for key in c2.kparams:
00172 if c2[key].__str__()[0] == "(":
00173 c2[key] = c2[key].__str__().strip('()').replace(' ','')
00174 clist.append( c2 )
00175
00176 data_type = c1['type']
00177 c1['type'] = 'float'
00178 c1['output'] = 0
00179
00180 c4 = cls.keep( c4, ['mean','seed'] )
00181 c4 = cls.default_function( c4, 'noise' )
00182 clist.append(c4)
00183
00184 if data_type == 'complex':
00185 c3 = cls.keep( c3, [] )
00186 c3 = cls.default_function( c3, 'rtoc' )
00187 clist.append(c3)
00188
00189
00190 return cls.pack( clist )
00191
00192 class fdct2vects( sfConverter ):
00193 @classmethod
00194 def map( cls, source, command ):
00195 command = cls.default_function( command )
00196 command = cls.truefalseHelper( command )
00197 cInSpace = command.kparams.pop('cInSpace')
00198 sizes,cparams = gen_sizes_file(command, cInSpace )
00199
00200 command.kparams.pop('nba')
00201 command.kparams.pop('nbs')
00202 command.kparams.pop('ac')
00203
00204 wedgconstr = command.kparams.pop( 'wedgconstr' )
00205 if command.kparams['mode'] == 'zang':
00206 command.kparams['nba2zL'] = wedgconstr[0]
00207 command.kparams['nba2zR'] = wedgconstr[1]
00208
00209 return cls.pack( command, stdin=sizes )
00210
00211 @classmethod
00212 def trans( cls, command, space, *spaces ):
00213 space['data_type'] = "float"
00214 return space
00215
00216 class thr( sfConverter ):
00217 @classmethod
00218 def constr( cls, command, *Spaces ):
00219 if command.kparams.has_key( 'thr' ):
00220 assert command.kparams['thr'] > 0
00221 assert command.kparams.has_key( 'mode' )
00222 assert command.kparams['mode'] in ['hard', 'soft', 'nng']
00223
00224 @classmethod
00225 def trans( cls, command, space, *spaces ):
00226 return space
00227
00228 class noise( sfConverter ):
00229 @classmethod
00230 def trans( cls, command, space, *spaces ):
00231 return space
00232
00233 class cmplx( sfConverter ):
00234 @classmethod
00235 def map( cls, src, cmd ):
00236 cmd = cls.default_function( cmd, "cmplx" )
00237 return cls.pack( cmd, stdin=None )
00238
00239 @classmethod
00240 def trans( cls, command, space, *spaces ):
00241
00242 space = cls.change( space, data_type='complex' )
00243 return space
00244
00245 class reshape( sfConverter ):
00246
00247 @classmethod
00248 def constr( cls, command, *Spaces ):
00249 assert command.kparams.has_key( 'shape' )
00250
00251 prod = lambda seq: reduce(lambda x,y:x*y, seq ,1 )
00252
00253 assert prod( command['shape'] ) == prod( Spaces[0].shape )
00254
00255 @classmethod
00256 def trans( cls, cmd, space, *spaces ):
00257 d = lambda val,i,dct: dct.__setitem__('n%s' %i,val)
00258 shape = cmd.kparams['shape']
00259 [ d(val,i+1,space) for i,val in enumerate(shape ) ]
00260 return space
00261
00262 @classmethod
00263 def map( cls, src, cmd ):
00264 d = lambda val,i,dct: dct.__setitem__('n%s' %i,val)
00265 cmd = cls.default_function( cmd, "put" )
00266 shape = cmd.kparams.pop('shape')
00267 [ d(val,i+1,cmd.kparams) for i,val in enumerate(shape ) ]
00268 return cls.pack( cmd )
00269
00270 class cat( sfConverter ):
00271 @classmethod
00272 def trans( cls, cmd, space, *spaces ):
00273
00274 axis = cmd.kparams['axis']
00275 nX = "n%s" %(axis)
00276 nXval = sum( [spc[nX] for spc in spaces ] )
00277 space[ nX ] = nXval
00278 return space
00279
00280 class spike( sfConverter ):
00281 @classmethod
00282 def map( cls, src, cmd ):
00283 cmd = cls.default_function( cmd, "spike" )
00284
00285 k = cmd.pop_kw('k',None)
00286 if k:
00287 for X,kX in enumerate(k):
00288 cmd['k%s' %(X+1)] = kX+1
00289
00290 return cls.pack( cmd )
00291
00292 @classmethod
00293 def trans( cls, cmd, space, *spaces ):
00294 return space
00295
00296 sffactory = rsfCommandFactory()
00297 sffactory.addallfrom( all )