Module import Error

Hi all,

When trying to run the sundials-compiled PyStan (following @ahartikainen’s help on the Stan Discourse) I’ve had some trouble compiling my pystan model.

functions {

  real[] SIR(real t, real[] y, real[] theta, 
             real[] x_r, int[] x_i) {

      real S = y[1];
      real I = y[2];
      real R = y[3];
      
      real beta = theta[1];
      real gamma = theta[2];
      
      real dS_dt = -beta * I * S;
      real dI_dt =  beta * I * S - gamma * I;
      real dR_dt =  gamma * I;
      
      return {dS_dt, dI_dt, dR_dt};
  }

}
data {
  int<lower = 1> n_obs;   // number of days observed
  int<lower = 1> n_pop;   // population size
  int y[n_obs];           // data, total number of infected individuals
  real t0;                // initial time point 
  real ts[n_obs];         // time points observed

  int<lower=0> n_pred;    // number of cases to predict forward
  real ts_pred[n_pred];   // future time points

}

transformed data {
  real x_r[0];
  int x_i[0];
  int n_states = 3;
  
  
}

parameters {
  real<lower = 0> theta[2];       // theta[1]: beta, theta[2]: gamma
  real<lower = 1e-6, upper = 1> S0;  // initial fraction of susceptible individuals
}

transformed parameters{
  real<lower = -1e-6, upper = 1> y_hat[n_obs, n_states]; // ODE solutions
  real<lower = 0, upper = 1> y_init[n_states]; // initial SIR fractions
  real<lower = 0> lambda[n_obs]; 
  
  y_init[1] = S0 - 1e-6;
  y_init[2] = 1 - S0 + 1e-6;
  y_init[3] = 0;
  //y_hat = integrate_ode_rk45(SIR, y_init, t0, ts, theta, x_r, x_i);
  y_hat = integrate_ode_bdf(SIR, y_init, t0, ts, theta, x_r, x_i);
  
  for (i in 1:n_obs) {
        lambda[i] = ((1-2e-6)*y_hat[i, 2] + 1e-6)* n_pop;
  }
}

model {
  theta ~ lognormal(0, 1);
  S0 ~ beta(1, 1);
  y ~ poisson(lambda);
}

generated quantities {
  real R_0 = theta[1] / theta[2];   // Basic reproduction number

  vector[n_pred] y_pred;
  vector[n_pred] lambda_pred;
  real y_init_pred[3] = y_hat[n_obs, ]; // New initial conditions
  real t0_pred = ts[n_obs];             // New time zero is the last observed time
  real y_hat_pred[n_pred, 3];
  
  y_hat_pred = integrate_ode_rk45(SIR, y_init_pred, t0_pred, ts_pred, theta, x_r, x_i);
  
  for (i in 1:n_pred) {
    lambda_pred[i] = ((1-2e-6)*y_hat_pred[i, 2] + 1e-6) * n_pop;
    y_pred[i] = poisson_rng(lambda_pred[i]);
  }



}

When I try compiling my model:

stan_sm = pystan.StanModel(
    model_name='sir_model',
    model_code=sir_model,
    verbose=True
)

I get the following error:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-7-e12ef6b38546> in <module>
      2     model_name='sir_model',
      3     model_code=sir_model,
----> 4     verbose=True
      5 )

~/anaconda3/envs/pystan_stiff/lib/python3.6/site-packages/pystan/model.py in __init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args, allow_undefined, include_dirs, includes)
    524                 os.dup2(orig_stderr, sys.stderr.fileno())
    525 
--> 526         self.module = load_module(self.module_name, lib_dir)
    527         self.module_filename = os.path.basename(self.module.__file__)
    528         # once the module is in memory, we no longer need the file on disk

~/anaconda3/envs/pystan_stiff/lib/python3.6/site-packages/pystan/model.py in load_module(module_name, module_path)
    109         pyximport.install()
    110         sys.path.append(module_path)
--> 111         return __import__(module_name)
    112     else:
    113         import imp

ImportError: dlopen(/var/folders/cz/pj6fdbtj3fn_fylbgqhx86hjlyt4ss/T/pystan__7lohdvk/stanfit4sir_model_22bf53890dbdc229524a10734025e572_3085698279385622801.cpython-36m-darwin.so, 2): Symbol not found: _fkbjac_
  Referenced from: /var/folders/cz/pj6fdbtj3fn_fylbgqhx86hjlyt4ss/T/pystan__7lohdvk/stanfit4sir_model_22bf53890dbdc229524a10734025e572_3085698279385622801.cpython-36m-darwin.so
  Expected in: flat namespace
 in /var/folders/cz/pj6fdbtj3fn_fylbgqhx86hjlyt4ss/T/pystan__7lohdvk/stanfit4sir_model_22bf53890dbdc229524a10734025e572_3085698279385622801.cpython-36m-darwin.so

On my terminal, additionally, I see:

ld: warning: -pie being ignored. It is only used when linking a main executable
ld: warning: ignoring file /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd, file was built for unsupported file format ( 0x2D 0x2D 0x2D 0x20 0x21 0x74 0x61 0x70 0x69 0x2D 0x74 0x62 0x64 0x2D 0x76 0x33 ) which is not the architecture being linked (x86_64): /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/lib/libSystem.tbd

Additionally, output for the CC version of clang is as below:

clang version 4.0.1 (tags/RELEASE_401/final)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
InstalledDir: /anaconda3/envs/pystan_stiff/bin

Output of gcc --version:

gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
  • Operating System=Mac OS X
  • Python Version=3.6
  • PyStan Version=2.19.1.1

Hi, Wondering if there are any suggestions for how I could resolve this?

There might be some conflict between the compilers / libc. Can you try to create a new conda env?

conda create -n stan python=3.7 -c conda-forge