Hi all -
I’ve recently done some experimenting in how to expose functions written in a Stan model to Python. I’ve cleaned up the results of these and put them up here: GitHub - WardBrian/pybind_expose_stan_fns: Exposing Stan functions in Python. This is modeled after the similar functionality Rok has built for cmdstanr users.
A Stan model like
functions {
real my_log1p_exp(real x) {
return log1p_exp(x);
}
real array_fun(array[] real a) {
return sum(a);
}
real int_array_fun(array[] int a) {
return sum(a);
}
vector my_vector_mul_by_5(vector x) {
vector[num_elements(x)] result = x * 5.0;
return result;
}
int int_only_multiplication(int a, int b) {
return a * b;
}
real test_lgamma(real x) {
return lgamma(x);
}
// test special functions
void test_lp(real a) {
a ~ normal(0, 1);
}
real test_rng(real a) {
return normal_rng(a, 1);
}
real test_lpdf(real a, real b) {
return normal_lpdf(a | b, 1);
}
void test_printing(){
print("hi there!");
}
}
Gets compiled to a Python module:
>>> import numpy as np
>>> import basic
>>> dir(basic)
['StanAccumulator', 'StanRNG', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__',
'array_fun', 'int_array_fun', 'int_only_multiplication', 'my_log1p_exp', 'my_vector_mul_by_5', 'test_lgamma', 'test_lp', 'test_lpdf', 'test_printing', 'test_rng']
>>> basic.my_vector_mul_by_5(np.arange(1,5))
array([ 5., 10., 15., 20.])
This is not a fully production-ready effort, but it works for all kinds of user defined functions on Ubuntu. If someone wants to try it out on Mac I’d love to hear how successful it is or not, and I’m sure we could tweak it to work. I’m not confident this kind of thing will ever work on Windows natively, but happy to try it.