User supplied function

Most alignment programs use external functions that perform poorly defined operations on reference 2-D or 3-D averages before each refinement cycle. These functions are are located in user_functions.py. Note that input is passed by the calling program as a list ad cannot be easily changed. Output has to be exactly as shown in examples below; otherwise, the program will crash. Alternatively, they can be also placed outside of SPARX system in a user's area.

2-D

from EMAN2_cppwrap import *
from global_def import *

ref_ali2d_counter = -1
def ref_ali2d( ref_data ):
        from utilities    import print_msg
        from filter       import fit_tanh, filt_tanl
        from utilities    import center_2D
        #  Prepare the reference in 2D alignment, i.e., low-pass filter and center.
        #  Input: list ref_data
        #   0 - mask
        #   1 - center flag
        #   2 - raw average
        #   3 - fsc result
        #  Output: filtered, centered, and masked reference image
        #  apply filtration (FRC) to reference image:
        global  ref_ali2d_counter
        ref_ali2d_counter += 1
        print_msg("ref_ali2d   #%6d\n"%(ref_ali2d_counter))
        fl, aa = fit_tanh(ref_data[3])
        msg = "Tangent filter:  cut-off frequency = %10.3f        fall-off = %10.3f\n"%(fl, aa)
        print_msg(msg)
        tavg = filt_tanl(ref_data[2], fl, aa)
        cs = [0.0]*2
        tavg, cs[0], cs[1] = center_2D(tavg, ref_data[1])
        if(ref_data[1] > 0):
                msg = "Center x =      %10.3f        Center y       = %10.3f\n"%(cs[0], cs[1])
                print_msg(msg)
        return  tavg, cs

3-D

from EMAN2_cppwrap import *
from global_def import *

def ref_ali3d( ref_data ):
        from utilities      import print_msg
        from filter         import fit_tanh, filt_tanl
        from fundamentals   import fshift
        from morphology     import threshold
        #  Prepare the reference in 3D alignment, i.e., low-pass filter and center.
        #  Input: list ref_data
        #   0 - mask
        #   1 - center flag
        #   2 - raw average
        #   3 - fsc result
        #  Output: filtered, centered, and masked reference image
        #  apply filtration (FSC) to reference image:

        print_msg("ref_ali3d\n")
        cs = [0.0]*3


        stat = Util.infomask(ref_data[2], ref_data[0], False)
        volf = ref_data[2] - stat[0]
        Util.mul_scalar(volf, 1.0/stat[1])
        #volf = threshold(volf)
        Util.mul_img(volf, ref_data[0])
        fl, aa = fit_tanh(ref_data[3])
        msg = "Tangent filter:  cut-off frequency = %10.3f        fall-off = %10.3f\n"%(fl, aa)
        print_msg(msg)
        volf = filt_tanl(volf, fl, aa)
        if ref_data[1] == 1:
                cs = volf.phase_cog()
                msg = "Center x = %10.3f        Center y = %10.3f        Center z = %10.3f\n"%(cs[0], cs[1], cs[2])
                print_msg(msg)
                volf  = fshift(volf, -cs[0], -cs[1], -cs[2])
        return  volf, cs

Dynamic loading of functions

Loading user functions defined elsewhere is now possible as well. Setting the commandline option --function=[/home/justus,foo,bar] will result in loading the function bar() from the module foo.py residing in the directory /home/justus. Note that the interface of the function will not be checked, so if the number of parameters passed to the function or their type do not match, the program will crash with an exception as described above. Also note that while module name is foo.py, there is no extension in passing its name in brackets!

user-supplied-function (last edited 2023-07-01 13:12:56 by localhost)