def makePDFBaseline(name, fnc, **dbattrs): '''Helper function for registering Python function as a PDFBaseline. This is required for using Python function as PDFCalculator.baseline. name -- unique string name for registering Python function in the global registry of PDFBaseline types. This will be the string identifier for the createByType factory. fnc -- Python function of a floating point argument and optional float parameters. The parameters need to be registered as double attributes in the functor class. The function fnc must be picklable and it must return a float. dbattrs -- optional float parameters of the wrapped function. These will be registered as double attributes in the functor class. The wrapped function must be callable as fnc(x, **dbattrs). Make sure to pick attribute names that do not conflict with other PDFCalculator attributes. Return an instance of the new PDFBaseline class. Example: # Python baseline function def fshiftedline(x, aline, bline): return aline * x + bline # wrap it as a PDFBaseline and register as a "shiftedline" type makePDFBaseline("shiftedline", fshiftedline, aline=-1, bline=0) baseline = PDFBaseline.createByType("shiftedline") print map(baseline, range(5)) # use it in PDFCalculator pdfc = PDFCalculator() pdfc.baseline = baseline # or pdfc.setBaselineByType("shiftedline") ''' from diffpy.srreal.wraputils import _wrapAsRegisteredUnaryFunction return _wrapAsRegisteredUnaryFunction(PDFBaseline, name, fnc, **dbattrs)
def makePDFEnvelope(name, fnc, replace=False, **dbattrs): '''Helper function for registering Python function as a PDFEnvelope. This is required for using Python function as PDFCalculator envelope. name -- unique string name for registering Python function in the global registry of PDFEnvelope types. This will be the string identifier for the createByType factory. fnc -- Python function of a floating point argument and optional float parameters. The parameters need to be registered as double attributes in the functor class. The function fnc must be picklable and it must return a float. replace -- when set replace any PDFEnvelope type already registered under the name. Otherwise raise RuntimeError when the name is taken. dbattrs -- optional float parameters of the wrapped function. These will be registered as double attributes in the functor class. The wrapped function must be callable as fnc(x, **dbattrs). Make sure to pick attribute names that do not conflict with other PDFCalculator attributes. Return an instance of the new PDFEnvelope class. Example: # Python envelope function def fexpdecay(x, expscale, exptail): from math import exp return expscale * exp(-x / exptail) # wrap it as a PDFEnvelope and register as a "expdecay" type makePDFEnvelope("expdecay", fexpdecay, expscale=5, exptail=4) envelope = PDFEnvelope.createByType("expdecay") print map(envelope, range(9)) # use it in PDFCalculator pdfc = PDFCalculator() pdfc.addEnvelope(envelope) # or pdfc.addEnvelope("expdecay") ''' from diffpy.srreal.wraputils import _wrapAsRegisteredUnaryFunction rv = _wrapAsRegisteredUnaryFunction(PDFEnvelope, name, fnc, replace=replace, **dbattrs) return rv