00001 """
00002 Helper functions for the 2d and 3d Curvelet Converter
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 from slimpy_base.api.Plugins.slim2rsf.rsfContainer import rsf_data_container
00027 from slimpy_base.Environment.InstanceManager import InstanceManager
00028 from os.path import join, isfile
00029 from string import Template
00030 from subprocess import Popen, PIPE
00031 from slimpy_base.Core.Command.Drivers.Unix_Pipes import Unix_Pipes
00032
00033
00034
00035 __env__ = InstanceManager()
00036
00037 SFFDCT2 = lambda:join( __env__['slimvars']['RSFBIN'] , 'sffdct2' )
00038
00039 SFMATH = lambda:join( __env__['slimvars']['RSFBIN'] , 'sfmath' )
00040
00041 MATHCMD_2D = Template( "true | ${math} n1=${n1} n2=${n2} output=0" )
00042 MAPONLY_2D = Template( "${SFFDCT} nbs=${nbs} "
00043 "nba=${nba} ac=${ac} maponly=y > ${sizes}" )
00044
00045 curvelets = {}
00046
00047 def GetShapeInv( command ,space):
00048
00049 nba = command['nba']
00050 nbs = command['nbs']
00051 ac = command['ac']
00052
00053
00054 key = (nba,nbs,ac)
00055 if curvelets.has_key(key):
00056 ck = curvelets[key]
00057 mshape = tuple(space.shape)
00058 if ck.has_key( mshape ):
00059 return ck[ mshape ]
00060
00061 raise Exception( "could not predict sizes" )
00062
00063 def GetShapeFwd( command ,space ):
00064
00065
00066 nba = command['nba']
00067 nbs = command['nbs']
00068 ac = command['ac']
00069
00070 n1 = space.shape[0]
00071 n2 = space.shape[1]
00072
00073 key = (nba,nbs,ac)
00074 if curvelets.has_key(key):
00075 ck = curvelets[key]
00076 mshape = tuple(space.shape)
00077 if ck.has_key( mshape ):
00078 return ck[ mshape ]
00079
00080 if not isfile( SFFDCT2() ):
00081 raise EnvironmentError( "could not find file 'sffdct2' " )
00082 if not isfile( SFMATH() ):
00083 raise EnvironmentError( "could not find file 'sfmath' " )
00084
00085
00086 sizes = join ( __env__['slimvars']['globaltmpdir'] , 'sizes.rsf' )
00087
00088 MATH = MATHCMD_2D.substitute( math=SFMATH(), n1=n1, n2=n2 )
00089 cmd = MAPONLY_2D.substitute( SFFDCT=SFFDCT2(), nba=nba, nbs=nbs, ac=ac, sizes=sizes )
00090
00091 sizes_command = Unix_Pipes.CreateComand([MATH,cmd], 'localhost',
00092 is_local=[None,False],
00093 is_tmp=[None,True] )
00094
00095 print >> __env__['record']( 5, 'cmd','sizes' ) , sizes_command
00096 p0 = Popen( sizes_command, shell=True, stderr=PIPE )
00097
00098 retcode = p0.wait()
00099 if retcode:
00100 out = p0.stderr.read()
00101 raise Exception( "Command: '%(cmd)s' failed\n%(out)s" %vars() )
00102
00103 sizes_cont = rsf_data_container( sizes )
00104 shape = sizes_cont.params.shape
00105
00106 sizes_cont.rm()
00107
00108 ck = curvelets.setdefault( key, {} )
00109 ck[ (n1,n2) ] = shape
00110 ck[ tuple(shape) ] = (n1,n2)
00111 return shape
00112
00113
00114
00115
00116
00117 SFFDCT3 = lambda:join( __env__['slimvars']['RSFBIN'] , 'sffdct3' )
00118 MATHCMD_3D = Template( "true | ${math} n1=${n1} n2=${n2} n3=${n3} output=0" )
00119 MAPONLY_3D = Template( "${SFFDCT} nbs=${nbs} nbd=${nbd} "
00120 "ac=${ac} maponly=y > ${sizes}" )
00121
00122 curvelets3 = {}
00123
00124
00125 def GetShapeInv3d( command ,space):
00126
00127 nbd = command['nbd']
00128 nbs = command['nbs']
00129 ac = command['ac']
00130
00131
00132 key = (nbd,nbs,ac)
00133 if curvelets.has_key(key):
00134 ck = curvelets[key]
00135 mshape = tuple(space.shape)
00136 if ck.has_key( mshape ):
00137 return ck[ mshape ]
00138
00139 raise Exception( "could not predict sizes" )
00140
00141 def GetShapeFwd3d( command ,space ):
00142
00143
00144 nbd = command['nbd']
00145 nbs = command['nbs']
00146 ac = command['ac']
00147
00148 n1 = space.shape[0]
00149 n2 = space.shape[1]
00150 n3 = space.shape[2]
00151
00152 key = (nbd,nbs,ac)
00153 if curvelets.has_key(key):
00154 ck = curvelets[key]
00155 mshape = tuple(space.shape)
00156 if ck.has_key( mshape ):
00157 return ck[ mshape ]
00158
00159 if not isfile( SFFDCT3() ):
00160 raise EnvironmentError( "could not find file 'sffdct2' " )
00161 if not isfile( SFMATH() ):
00162 raise EnvironmentError( "could not find file 'sfmath' " )
00163
00164
00165 sizes = join ( __env__['slimvars']['globaltmpdir'] , 'sizes3d.rsf' )
00166
00167 MATH = MATHCMD_3D.substitute( math=SFMATH(), n1=n1, n2=n2, n3=n3 )
00168 cmd = MAPONLY_3D.substitute( SFFDCT=SFFDCT3(), nbd=nbd, nbs=nbs, ac=ac, sizes=sizes )
00169
00170 sizes_command = Unix_Pipes.CreateComand([MATH,cmd], 'localhost',
00171 is_local=[None,False],
00172 is_tmp=[None,True] )
00173
00174 print >> __env__['record']( 5, 'cmd','sizes' ) , sizes_command
00175 p0 = Popen( sizes_command, shell=True, stderr=PIPE )
00176
00177 retcode = p0.wait()
00178 if retcode:
00179 out = p0.stderr.read()
00180 raise Exception( "Command: '%(cmd)s' failed\n%(out)s" %vars() )
00181
00182 sizes_cont = rsf_data_container( sizes )
00183 shape = sizes_cont.params.shape
00184
00185 sizes_cont.rm()
00186
00187 ck = curvelets.setdefault( key, {} )
00188 ck[ tuple(space.shape) ] = shape
00189 ck[ tuple(shape) ] = tuple(space.shape)
00190 return shape
00191
00192