1

Does anybody know if there is a way to make use of python decorators like functools (@cache) for Python tool in Alteryx? Looking to implement a repetitive function evaluation in python and caching the results would save enormously.

I have seen use cases for something like below in Python, but I wonder how would you achieve similar performance in Alteryx using Python.


from functools import lru_cache
import numpy as np


def estimate_error(x):
    t = 1 / (1 + 0.33 * abs(x))
    pre_erf = (((1.06 * t - 1.45) * t + 1.42) * t - 0.29) * t + 0.25
    erf = np.sign(x) * (1 - pre_erf * t * np.exp(- x * x))
    return erf


@lru_cache(maxsize=1024)
def fast_ln_sf(x, scale, shape):
    return 0.5 - 0.5 * estimate_error(0.7 * (np.log(x) - np.log(scale)) / shape)

I intend to use this function as an objective for my optimization problem, meaning this function gets evaluated at least 1000s of times if I have high number of variables.

The use case for this function is of the type:



def obj(x, *args):
    a = args[0].to_numpy()
    s = args[1].to_numpy()
    c = args[2].to_numpy()
    n = args[3]

    sum_ = 0
    for i in range(n):
    sum _ += (a * fast_ln_sf(x[i], s, c))
    return -1 * sum_


solution = scipy.optimize.minimize(obj, args = [df.a, df.b, df.s, df.c, df.shape[0]])


  • 1
    There are a bunch of typos or basic logic errors in the code you've shown, but it's unclear to me if that's code you're actually trying to use, or just some throw away example code. Have you actually tried solving the issue with this code? If so, what happened? Are you getting errors? Wrong answers? Give details! If this were a pure Python question, I'd point out each issue with your code (needing to use `functools.lru_cache` instead of just `lru_cache` for example), but that may not be useful if the code isn't actually intended to be used as is. – Blckknght Aug 25 '23 at 21:58
  • I have updated the code to almost my current use case. In essence, I am trying to compute a non-linear objective for my optimization model and this uses the fast_ln_sf, which makes the computations complex. – Aadarsh Gupta Aug 29 '23 at 15:14

0 Answers0