Source code for biogeme.expressions.named_expression
"""Decorator for the derivative calculation, so that the names of the variables are associated with their values.:author: Michel Bierlaire:date: Sat Apr 20 08:06:54 2024"""fromfunctoolsimportwrapsfrombiogeme.expressionsimportExpressionfrombiogeme.function_outputimportFunctionOutput,NamedFunctionOutput,convert_to_dict
[docs]defnamed_function_output(func):""" Decorator to convert a FunctionOutput into a NamedFunctionOutput, using explicitly passed beta indices. The decorated function must now accept an `indices` keyword argument. """@wraps(func)defwrapper(self:Expression,*args,indices=None,**kwargs):ifindicesisNone:raiseValueError("Parameter 'indices' must be provided to use @named_function_output.")# Call the original function/methodresult:FunctionOutput=func(self,*args,**kwargs)ifnotisinstance(result,FunctionOutput):raiseTypeError(f'Expected function {func.__name__} to return FunctionOutput, got {type(result).__name__} instead.')the_result=NamedFunctionOutput(function=result.function,gradient=convert_to_dict(result.gradient,indices),hessian=convert_to_dict([convert_to_dict(row,indices)forrowinresult.hessian],indices,),)returnthe_resultreturnwrapper