3d data in Stan (cmdstan 2.30.1)

Hi all,

Thanks a lot for the nice tool. I need some feedback in regard to inserting 3d MRI data into Stan. See my model below :
The error that I receive is always for all 4 output csv files:

output-4.csv: Exception: variable does not exist; processing stage=data initialization; variable name=yy; base type=double (in ‘/Users/Documents/MATLAB/pat_test_one.stan’, line 5, column 0 to column 22)

The array that I try to insert is of dimensions: 268, 16, 6 as shown below… (268 voxels, 16 b values and 6 directions)

Is there something with my array[N,16,6] real yy
wrong, why the above error message shows up ?

Thanks !

error message in Matlab

Warning: Stan seems to have exited badly.

In StanFit/process_exit_failure (line 290)
In StanFit/process_exit (line 170)
In StanFit>@(src,evnt)process_exit(self,src,evnt) (line 71)
In processState/set.exitValue (line 30)
In processManager.pollTimerStop (line 508)
In timer/timercb (line 90)
Warning: Stan seems to have exited badly.
In StanFit/process_exit_failure (line 290)
In StanFit/process_exit (line 170)
In StanFit>@(src,evnt)process_exit(self,src,evnt) (line 71)
In processState/set.exitValue (line 30)
In processManager.pollTimerStop (line 508)
In timer/timercb (line 90)
Warning: Stan seems to have exited badly.
In StanFit/process_exit_failure (line 290)
In StanFit/process_exit (line 170)
In StanFit>@(src,evnt)process_exit(self,src,evnt) (line 71)
In processState/set.exitValue (line 30)
In processManager.pollTimerStop (line 508)
In timer/timercb (line 90)
Warning: Stan seems to have exited badly.
In StanFit/process_exit_failure (line 290)
In StanFit/process_exit (line 170)
In StanFit>@(src,evnt)process_exit(self,src,evnt) (line 71)
In processState/set.exitValue (line 30)
In processManager.pollTimerStop (line 508)
In timer/timercb (line 90)

code

y_dti = back3dti;

size(y_dti) 268,16,6

N = ni1;

model = StanModel(‘verbose’,true);
model.compile(‘stanc’);

dti_code = {
‘data {’
‘int<lower=0> N;’
'vector[16] x; ’ % 16 b-values
‘matrix[6,3] G;’ % gradient direction
‘array[N,16,6] real yy;’
‘}’

‘parameters {’
'vector[N] S0; ’
‘real <lower=0> sigma;’
'vector[N] Dxx; ’
'vector[N] Dxy; ’
'vector[N] Dxz; ’

‘}’

‘model {’

‘for (s in 1:N){’
‘Dxx[s] ~ uniform(-2.5e-3,2.5e-3);’ % for all Ns
‘Dxy[s] ~ uniform(-2.5e-3,2.5e-3);’
‘Dxz[s] ~ uniform(-2.5e-3,2.5e-3);’
‘Dyx[s] ~ uniform(-2.5e-3,2.5e-3);’

‘}’
‘sigma ~ uniform(0,10);’

'for (v in 1:N){'
'for (n in 1:16){'
'for (k in 1:6){'
    'yy[v,n,k] ~ normal(S0[v]*exp(-x[n]*(G[k,1]^2*Dxx[v] + G[k,2]^2*Dyy[v] + G[k,3]^2*Dzz[v] + 2*G[k,1]*G[k,2]*Dxy[v] + 2*G[k,1]*G[k,3]*Dxz[v] + 2*G[k,2]*G[k,3]*Dyz[v])),sigma);'

‘}’
‘}’
‘}’
‘}’
};

% for initialising parameters
model.set(‘model_code’,dti_code);
model.compile();

data_S = struct(‘N’,ni1,‘G’,g,‘yy’,y_dti,‘x’,bval);
fit_S = stan(‘fit’,model,‘data’,data_S,‘init’,struct(‘Dxx’,1.5e-3ones(ni1,1),‘Dxy’,1.5e-3ones(ni1,1),‘Dxz’,1.5e-3ones(ni1,1),‘Dyx’,1.5e-3ones(ni1,1),‘Dyy’,1.5e-3ones(ni1,1),‘Dyz’,1.5e-3ones(ni1,1),‘Dzx’,1.5e-3ones(ni1,1),‘Dzy’,1.5e-3ones(ni1,1),‘Dzz’,1.5e-3ones(ni1,1),‘S0’,150ones(ni1,1),‘sigma’,5));

Hi and welcome. What version of Stan are you using? The array synax you use to declare yy has only existed in Stan since version 2.26.something. See here CmdStan 2.27.0 release candidate

Hi and thanks for your remark, I use 2.30.1.
Any ideas ?
Thanks in advance
I tried all different array methodologies from the 4.2 Array and Dimension Function Chapter for the insertion of the 3d data, but all bring the same result that the variable yy does not exist

The solution is here: the rdump.m needs to be modified, as MatlabStan does not recognise 3D+ arrays

hi @magiceule19, I’m sorry to say that matlab stan is not being actively supported, and, as indicated above, the Stan language syntax you are trying to use is not recognized by the version of Stan used by matlab stan.

if possible, I recommend using CmdStanPy or CmdStanR.

a far more ambitious project would be to create a new Stan wrapper for Matlab, following the CmdStanPy and CmdStanR patterns.

It seems like MatlabStan is cmdstan-based, so it should be a decent starting place still if someone was interesting in maintaining an interface

Glad to see you got some responses, it’s a shame the Matlab Stan wrapper isn’t maintained. If you have any interest in updating a fork, you might be able to bring the official version up to date. Maintaining a limited CmdStan-based wrapper isn’t a huge effort if you are using it on an ongoing basis…