3
#include <fff_glm_mfx.h>
4
#include <gsl/gsl_rng.h>
9
#define DEFAULT_BASE 0.0
10
#define DEFAULT_NSIMU 1
11
#define DEFAULT_MAGIC 0
12
#define DEFAULT_NITER 5
17
T = fffMat_glm_mfx_stat( E, V, X, C, base, nsimu, magic, niter );
19
magic < 0 ==> randomize permutations
20
magic >= 0 ==> produce deterministic permutations in the range [magic, magic+nsimu[
24
static void _set_simulation_parameters( int* flag_permute, int* flag_use_rng,
25
int* simu0, int* nsimu,
26
size_t* nsimu_max, int n,
31
*flag_permute = ( (*nsimu > 1) || (magic!=0) );
32
*flag_use_rng = ( magic < 0 );
40
if ( ( (size_t)(*nsimu) > *nsimu_max ) &&
41
( !(*flag_use_rng) ) )
47
void mexFunction( int nlhs, mxArray *plhs[],
48
int nrhs, const mxArray*prhs[] )
51
int flag = DEFAULT_FLAG;
52
double base = DEFAULT_BASE;
53
int nsimu = DEFAULT_NSIMU;
54
long int magic = DEFAULT_MAGIC;
55
int niter = DEFAULT_NITER;
56
gsl_vector **E, **V, **T;
57
int vox, nvox, nV, simu, simu0;
58
fff_onesample_stat* Stat;
59
fff_onesample_stat_mfx* StatMFX;
61
int flag_permute, flag_use_rng, flag_mfx = 0;
66
/* Check for proper number of arguments. */
68
mexErrMsgTxt("Two input arguments required.");
70
else if ( nlhs > 50 ) {
71
mexErrMsgTxt("Too many output arguments");
74
/* Get list of effect vectors */
75
E = gsl_vector_list_from_mxArray_new( prhs[0], &nvox );
77
/* Get list of associated variances */
78
V = gsl_vector_list_from_mxArray_new( prhs[1], &nV );
80
/* Test consistency between E and V */
81
if ( (nV != 0) && (nV != nvox) ) {
82
gsl_vector_list_from_mxArray_delete( E, nvox );
83
gsl_vector_list_from_mxArray_delete( V, nV );
84
mexErrMsgTxt("Incompatible effect and variance arrays.");
87
/* Statistic choice */
89
aux = mxGetPr( prhs[2] );
91
if ( flag >= MFX_OFFSET )
97
aux = mxGetPr( prhs[3] );
98
base = (double)(*aux);
101
/* Number of simulations */
103
aux = mxGetPr( prhs[4] );
109
aux = mxGetPr( prhs[5] );
110
magic = (long int)(*aux);
113
/* Number of EM iterations (MFX stats) */
115
aux = mxGetPr( prhs[6] );
120
/* Simulation parameters */
121
_set_simulation_parameters( &flag_permute, &flag_use_rng,
122
&simu0, &nsimu, &nsimu_max,
123
(int)E[0]->size, magic );
126
plhs[0] = mxCreateDoubleMatrix( nsimu, nvox, mxREAL );
128
/* Local structures */
129
T = gsl_vector_list_from_mxArray_new( plhs[0], &nvox );
131
Stat = fff_onesample_stat_new( (size_t)E[0]->size, (fff_onesample_stat_flag)flag, base );
133
StatMFX = fff_onesample_stat_mfx_new( (size_t)E[0]->size, (fff_onesample_stat_mfx_flag)(flag-MFX_OFFSET), base );
134
fff_onesample_stat_mfx_set_niter( StatMFX, (unsigned int)niter );
136
if ( flag_use_rng ) {
138
rng = gsl_rng_alloc( gsl_rng_default );
141
/* Compute stat for the original dataset */
144
for ( vox=0; vox<nvox; vox++ ) {
145
fff_onesample_stat_eval( Stat, E[vox] );
146
T[vox]->data[0] = Stat->stat;
149
for ( vox=0; vox<nvox; vox++ ) {
150
fff_onesample_stat_mfx_eval( StatMFX, E[vox], V[vox] );
151
T[vox]->data[0] = StatMFX->stat;
155
for ( simu=simu0; simu<nsimu; simu++ ) {
157
/* Determine permutation (magic number) */
159
magic_aux = gsl_rng_uniform_int( rng, nsimu_max );
161
magic_aux = (size_t)(simu + magic);
163
/* Loop over voxels */
165
Stat->magic = magic_aux;
166
for ( vox=0; vox<nvox; vox++ ) {
167
fff_onesample_stat_permute_and_eval( Stat, E[vox] );
168
T[vox]->data[simu] = Stat->stat;
172
StatMFX->magic = magic_aux;
173
for ( vox=0; vox<nvox; vox++ ) {
174
fff_onesample_stat_mfx_permute_and_eval( StatMFX, E[vox], V[vox] );
175
T[vox]->data[simu] = StatMFX->stat;
179
} /* End loop over simulations */
182
gsl_vector_list_from_mxArray_delete( E, nvox );
183
gsl_vector_list_from_mxArray_delete( V, nV );
184
gsl_vector_list_from_mxArray_delete( T, nvox );
186
fff_onesample_stat_delete( Stat );
188
fff_onesample_stat_mfx_delete( StatMFX );