00001 ##@package new_smoothgradient_rsf_integration 00002 # This is the second step in creating a linear operator. There are two substeps to this module. 00003 # - create a \ref slimpy_base.api.Plugins.slim2rsf.sfcommands.sfConverter "sfConverter" 00004 # subclass that defines how to use a SLIMpy operator with rsf commands. 00005 # - register the converter with the rsf plugin. 00006 # 00007 # @code 00008 # from numpy import ceil 00009 # from slimpy_base.api.Plugins.slim2rsf.sfCommandFactory import rsfCommandFactory 00010 # from slimpy_base.api.Plugins.slim2rsf.sfcommands.sfConverter import sfConverter 00011 # 00012 # ######################################################################## 00013 # # Define how smoothgradientx behaves 00014 # ######################################################################## 00015 # 00016 # # create a Converter class 00017 # class smoothgradientX_Converter( sfConverter ): 00018 # \""" 00019 # This is a mapping instance that maps a SLIMpy command into an object that 00020 # can be run. 00021 # There are three types of function this class should have. 00022 # - map 00023 # - trans 00024 # - constr 00025 # Each of these functions can have an optional '_adj' at the end of the name 00026 # if the operator's adjoint is different than the forward. 00027 # 00028 # \par map should change the arguments of the operator such that it can run as 00029 # an rsf command line. 00030 # \par trans defines how this operator affects the domain of the input. 00031 # this is also to calculate the range of the operator at initialization. 00032 # \par constr defines any run time error messages if the vector is not in the domain of 00033 # the operator. 00034 # 00035 # 00036 # \""" 00037 # # always use classmethod or static method 00038 # @classmethod 00039 # def map( cls, source, command ): 00040 # \""" 00041 # map a SLIMpy command to a rsf command 00042 # \""" 00043 # # the RSF bin and 'sf' will be automaticaly prepended to smoothgradient executable 00044 # command = cls.default_function( command, "./Msmoothgradient.py" ) 00045 # #change all python True/False objects to 'y'/'n' strings 00046 # command = cls.truefalseHelper( command ) 00047 # #map the adj flage to inv 00048 # command = cls.keywordmap( command, {'adj':'inv'} ) 00049 # #always must return a CommandPack instance 00050 # return cls.pack( command ) 00051 # 00052 # 00053 # @classmethod 00054 # def trans( cls, command, space, *spaces ): 00055 # 'define how this operator affect the space' 00056 # n1 = spaces[0]['n1'] 00057 # space['n1_smoothgradient']= n1 00058 # space["n1"] = int( ceil( n1/2. )+1 ) 00059 # space['data_type']='float' 00060 # return space 00061 # 00062 # @classmethod 00063 # def trans_adj( cls, command, space, *spaces ): 00064 # \""" 00065 # trans_adj will automatically be called in the case 00066 # where the command has an adj keyword that is true 00067 # \""" 00068 # n1 = spaces[0]['n1_smoothgradient'] 00069 # space['data_type']='float' 00070 # space['n1']= n1 00071 # return space 00072 # 00073 # @classmethod 00074 # def constr( cls, command, space ): 00075 # 'make sure the data on the forward command is float' 00076 # cls.match( space, data_type='float' ) 00077 # 00078 # 00079 # @classmethod 00080 # def constr_adj( cls, command, space ): 00081 # 'make sure the data on the adjoint command is float' 00082 # cls.match( space, data_type='float' ) 00083 # 00084 # 00085 # #=============================================================================== 00086 # # Add it to the list of existing rsf commands by 00087 # # by registering the converter with the rsf plugin. 00088 # #=============================================================================== 00089 # 00090 # factory = rsfCommandFactory() 00091 # # add the converter class to the factory 00092 # # now converter will be invoked when 00093 # # a linear operator with the 'name' attribute 00094 # # of smoothgradient 00095 # factory['smoothgradientX'] = smoothgradientX_Converter 00096 # @endcode 00097 00098 from numpy import ceil 00099 from slimpy_base.api.Plugins.slim2rsf.sfCommandFactory import rsfCommandFactory 00100 from slimpy_base.api.Plugins.slim2rsf.sfcommands.sfConverter import sfConverter 00101 00102 ######################################################################## 00103 # Define how smoothgradientx behaves 00104 ######################################################################## 00105 00106 # create a Converter class 00107 class smoothgradientX_Converter( sfConverter ): 00108 """ 00109 This is a mapping instance that maps a SLIMpy command into an object that 00110 can be run. 00111 There are three types of function this class should have. 00112 - map 00113 - trans 00114 - constr 00115 Each of these functions can have an optional '_adj' at the end of the name 00116 if the operator's adjoint is different than the forward. 00117 00118 \par map should change the arguments of the operator such that it can run as 00119 an rsf command line. 00120 \par trans defines how this operator affects the domain of the input. 00121 this is also to calculate the range of the operator at initialization. 00122 \par constr defines any run time error messages if the vector is not in the domain of 00123 the operator. 00124 00125 00126 """ 00127 # alwas use classmethod or static method 00128 @classmethod 00129 def map( cls, source, command ): 00130 """ 00131 map a SLIMpy command to a rsf command 00132 """ 00133 # the RSF bin and 'sf' will be automaticaly prepended to smoothgradient executable 00134 command = cls.default_function( command, "./Msmoothgradient.py" ) 00135 #change all python True/False objects to 'y'/'n' strings 00136 command = cls.truefalseHelper( command ) 00137 #map the adj flage to inv 00138 command = cls.keywordmap( command, {'adj':'inv'} ) 00139 #alwas must return a CommandPack instance 00140 return cls.pack( command ) 00141 00142 00143 @classmethod 00144 def trans( cls, command, space, *spaces ): 00145 'define how this operator affect the space' 00146 n1 = spaces[0]['n1'] 00147 space['n1_smoothgradient']= n1 00148 space["n1"] = int( ceil( n1/2. )+1 ) 00149 space['data_type']='float' 00150 return space 00151 00152 @classmethod 00153 def trans_adj( cls, command, space, *spaces ): 00154 """ 00155 trans_adj will automatically be called in the case 00156 where the command has an adj keyword that is true 00157 """ 00158 n1 = spaces[0]['n1_smoothgradient'] 00159 space['data_type']='float' 00160 space['n1']= n1 00161 return space 00162 00163 @classmethod 00164 def constr( cls, command, space ): 00165 'make sure the data on the forward command is float' 00166 cls.match( space, data_type='float' ) 00167 00168 00169 @classmethod 00170 def constr_adj( cls, command, space ): 00171 'make sure the data on the adjoint command is float' 00172 cls.match( space, data_type='float' ) 00173 00174 00175 00176 #=============================================================================== 00177 # Add it to the list of existing rsf commands by 00178 # by registering the converter with the rsf plugin. 00179 #=============================================================================== 00180 00181 factory = rsfCommandFactory() 00182 # add the converter class to the factory 00183 # now converter will be invoked when 00184 # a linear operator with the 'name' attribute 00185 # of smoothgradient 00186 factory['smoothgradientX'] = smoothgradientX_Converter