3
#include <fff_onesample_stat.h>
4
#include <gsl/gsl_rng.h>
9
#define DEFAULT_FLAG FFF_ONESAMPLE_STUDENT
10
#define DEFAULT_BASE 0.0
11
#define DEFAULT_NSIMU 1
12
#define DEFAULT_MAGIC 0
13
#define DEFAULT_NITER 5
19
T = fffMat_onesample_stat( E, V, flagStat, base, nsimu, magic, niter );
21
magic < 0 ==> randomize permutations
22
magic >= 0 ==> produce deterministic permutations in the range [magic, magic+nsimu[
26
static void _set_simulation_parameters( int* flag_permute, int* flag_use_rng,
27
int* simu0, int* nsimu,
28
size_t* nsimu_max, int n,
33
*flag_permute = ( (*nsimu > 1) || (magic!=0) );
34
*flag_use_rng = ( magic < 0 );
42
if ( ( (size_t)(*nsimu) > *nsimu_max ) &&
43
( !(*flag_use_rng) ) )
49
void mexFunction( int nlhs, mxArray *plhs[],
50
int nrhs, const mxArray*prhs[] )
53
int flag = DEFAULT_FLAG;
54
double base = DEFAULT_BASE;
55
int nsimu = DEFAULT_NSIMU;
56
long int magic = DEFAULT_MAGIC;
57
int niter = DEFAULT_NITER;
58
gsl_vector **E, **V, **T;
59
int vox, nvox, nV, simu, simu0;
60
fff_onesample_stat* Stat;
61
fff_onesample_stat_mfx* StatMFX;
63
int flag_permute, flag_use_rng, flag_mfx = 0;
68
/* Check for proper number of arguments. */
70
mexErrMsgTxt("Two input arguments required.");
72
else if ( nlhs > 50 ) {
73
mexErrMsgTxt("Too many output arguments");
76
/* Get list of effect vectors */
77
E = gsl_vector_list_from_mxArray_new( prhs[0], &nvox );
79
/* Get list of associated variances */
80
V = gsl_vector_list_from_mxArray_new( prhs[1], &nV );
82
/* Test consistency between E and V */
83
if ( (nV != 0) && (nV != nvox) ) {
84
gsl_vector_list_from_mxArray_delete( E, nvox );
85
gsl_vector_list_from_mxArray_delete( V, nV );
86
mexErrMsgTxt("Incompatible effect and variance arrays.");
89
/* Statistic choice */
91
aux = mxGetPr( prhs[2] );
93
if ( flag >= MFX_OFFSET )
99
aux = mxGetPr( prhs[3] );
100
base = (double)(*aux);
103
/* Number of simulations */
105
aux = mxGetPr( prhs[4] );
111
aux = mxGetPr( prhs[5] );
112
magic = (long int)(*aux);
115
/* Number of EM iterations (MFX stats) */
117
aux = mxGetPr( prhs[6] );
122
/* Simulation parameters */
123
_set_simulation_parameters( &flag_permute, &flag_use_rng,
124
&simu0, &nsimu, &nsimu_max,
125
(int)E[0]->size, magic );
128
plhs[0] = mxCreateDoubleMatrix( nsimu, nvox, mxREAL );
130
/* Local structures */
131
T = gsl_vector_list_from_mxArray_new( plhs[0], &nvox );
133
Stat = fff_onesample_stat_new( (size_t)E[0]->size, (fff_onesample_stat_flag)flag, base );
135
StatMFX = fff_onesample_stat_mfx_new( (size_t)E[0]->size, (fff_onesample_stat_mfx_flag)(flag-MFX_OFFSET), base );
136
fff_onesample_stat_mfx_set_niter( StatMFX, (unsigned int)niter );
138
if ( flag_use_rng ) {
140
rng = gsl_rng_alloc( gsl_rng_default );
143
/* Compute stat for the original dataset */
146
for ( vox=0; vox<nvox; vox++ ) {
147
fff_onesample_stat_eval( Stat, E[vox] );
148
T[vox]->data[0] = Stat->stat;
151
for ( vox=0; vox<nvox; vox++ ) {
152
fff_onesample_stat_mfx_eval( StatMFX, E[vox], V[vox] );
153
T[vox]->data[0] = StatMFX->stat;
157
for ( simu=simu0; simu<nsimu; simu++ ) {
159
/* Determine permutation (magic number) */
161
magic_aux = gsl_rng_uniform_int( rng, nsimu_max );
163
magic_aux = (size_t)(simu + magic);
165
/* Loop over voxels */
167
Stat->magic = magic_aux;
168
for ( vox=0; vox<nvox; vox++ ) {
169
fff_onesample_stat_permute_and_eval( Stat, E[vox] );
170
T[vox]->data[simu] = Stat->stat;
174
StatMFX->magic = magic_aux;
175
for ( vox=0; vox<nvox; vox++ ) {
176
fff_onesample_stat_mfx_permute_and_eval( StatMFX, E[vox], V[vox] );
177
T[vox]->data[simu] = StatMFX->stat;
181
} /* End loop over simulations */
184
gsl_vector_list_from_mxArray_delete( E, nvox );
185
gsl_vector_list_from_mxArray_delete( V, nV );
186
gsl_vector_list_from_mxArray_delete( T, nvox );
188
fff_onesample_stat_delete( Stat );
190
fff_onesample_stat_mfx_delete( StatMFX );