GP conditional prediction when GP was construced inside user defined function

I have a model where I end up generating a lot of GPs, so I made a function:

def do_gp(name):
    lengthscale = pm.Weibull(f'{name}_lengthscale', alpha=2, beta=1) 
    cov = pm.gp.cov.ExpQuad(1, ls=lengthscale, active_dims=[0]) 
    gp = pm.gp.Latent(cov_func = cov)
    out = gp.prior(f'{name}_gp_samples', X=time[:,None])
    return(out)

Used as:

with pm.Model() as model:
    time = pm.MutableData('time', time_np)
    a_samples = do_gp('a')
    b_samples = do_gp('b')
    # etc

But now I’d like to use the gp.conditional() function for each such created gp to obtain posterior predictive samples and I can’t quite discern how to go about doing that when each of the gp objects were declared inside a function as I’ve done. Any suggestions?

I think you have to return the GPs from the helper functions

Is there a way to assign them as named objects perchance? I’ll eventually be doing some nesting and I’d like to use the name argument to set the name of the gp instances

You can always do python stuff. Global variables, attach stuff to PyMC models, wrap everything in a fancy class… and so on