16.1.5. The Function Cookbook¶
A categorized quick-reference to the functions you will reach for most often,
with the signature, what it does, and which shipped programs use it (so you
can read a real caller). All of these are declared in 3ddata.h /
mrilib.h and available once you #include "mrilib.h".
Opening, loading, and writing datasets¶
Call |
What it does |
|---|---|
|
Open a dataset header from any AFNI-recognized name (BRIK/HEAD, NIFTI,
sub-brick selectors |
|
Read/mmap the voxel data (macro over |
|
Force |
|
Free voxel data, keep the header. |
|
Free the whole dataset. |
|
Write to disk; format follows the prefix. |
|
Overwrite-guard the write (see Anatomy of an AFNI Program). |
|
Clone header (same grid), no data. |
|
Set prefix, datum, nvals, TR, type, … |
|
Build an empty dataset from a geometry string. |
Seen together in src/3dToyProg.c; the open/load idiom is in essentially
every 3d* program.
Getting voxel data in and out¶
Because sub-bricks are individually typed and scaled, prefer the “give it to me
as float” routines over walking DSET_ARRAY by hand.
Call |
What it does |
|---|---|
|
One sub-brick |
|
One voxel’s time series as an |
|
One voxel’s time series into your |
|
Many voxels’ series at once. |
|
Write a time series into voxel |
|
Install |
|
Copy+scale |
THD_extract_series is one of the most-used routines in the tree –
3dTcorrelate, 3dDetrend, 3dDeconvolve, 3dTshift,
3dToutcount, 3ddelay, 3dfim+ all read per-voxel time courses with
it.
Whole-dataset time series: the MRI_vectim¶
When you need every in-mask voxel’s time series at once, converting the whole
dataset to an MRI_vectim is far more cache-friendly than repeated
THD_extract_series calls. A vectim is time-first (each voxel’s series
is contiguous), the opposite of a dataset’s space-first layout.
MRI_vectim *mv = THD_dset_to_vectim( dset, mask, ignore ) ; /* ignore = pts to skip */
for( iv=0 ; iv < mv->nvec ; iv++ ){
float *ts = VECTIM_PTR(mv, iv) ; /* this voxel's mv->nvals-long series */
...
}
THD_vectim_normalize(mv) ; /* L2-normalize each series in place */
VECTIM_destroy(mv) ;
mv->ivec[iv] is the 1D voxel index that row iv came from, so you can map
results back into a dataset. This structure is the backbone of AFNI’s
correlation machinery: 3dAutoTcorrelate, 3dBandpass, 3dECM,
3dDegreeCentrality, 3dLFCD, 3dLSS and InstaCorr/GroupInCorr all
build a vectim (normalizing it once) and then reduce correlations to dot
products.
Time-series and correlation primitives¶
Call |
What it does |
|---|---|
|
Normalize |
|
Same, applied to every row of a vectim. |
|
Pearson correlation of two series. |
|
Double-precision variant. |
|
Weighted Pearson. |
If both inputs are already unit-normalized (e.g. from THD_vectim_normalize),
the Pearson correlation is just their dot product – which is exactly the
shortcut InstaCorr and the *Tcorr* programs exploit. For spectral work,
csfft_cox(sign, nfft, complex_array) is the in-place FFT used by
src/3dToyProg.c’s toy_tsfunc and by 3dPeriodogram.
Masks and clustering¶
Call |
What it does |
|---|---|
|
|
|
Auto brain mask (the |
|
Count the set voxels. |
|
Nonzero if grids differ (always check a
|
THD_automask is reused by 3dAllineate, 3dBandpass,
3dBlurToFWHM, 3dBrickStat and many more – prefer it to rolling your
own threshold.
Statistics: p-values and thresholds¶
AFNI attaches a statistic type and its parameters (degrees of freedom,
etc.) to each sub-brick, so a t-stat brick knows how to become a p-value. The
type is one of the FUNC_*_TYPE codes (3ddata.h):
Code |
Value |
Statistic |
Aux parameters needed |
|---|---|---|---|
|
2 |
correlation coeff. |
samples, fit, ort params (3) |
|
3 |
Student t |
DOF (1) |
|
4 |
F |
numerator, denominator DOF (2) |
|
5 |
normal / Z |
none (0) |
|
6 |
chi-square |
DOF (1) |
|
7 |
incomplete beta |
a, b (2) |
|
8 |
binomial |
ntrial, prob (2) |
|
9 |
gamma |
shape, scale (2) |
|
10 |
Poisson |
mean (1) |
The number of aux parameters per code is the FUNC_need_stat_aux[] table;
their meaning is FUNC_label_stat_aux[].
Convert statistic <-> p-value (stataux is the parameter array, e.g. a
one-element {dof} for a t-test):
float p = THD_stat_to_pval ( stat, FUNC_TT_TYPE, stataux ) ; /* stat -> p */
float t = THD_pval_to_stat ( pval, FUNC_TT_TYPE, stataux ) ; /* p -> stat */
float z = THD_stat_to_zscore( stat, FUNC_TT_TYPE, stataux ) ; /* stat -> Z */
Work directly from a dataset sub-brick – pull the code and aux from the header instead of hand-assembling them:
int code = DSET_BRICK_STATCODE(dset, iv) ; /* 0 if not a stat brick */
float *aux = DSET_BRICK_STATAUX (dset, iv) ;
float p = THD_stat_to_pval( val, code, aux ) ;
/* or, the whole thing in one call (rickr, 2023): */
float thr = THD_volume_pval_to_thresh( dset, iv, 0.001, 1 ) ; /* p -> threshold */
These conversions live in thd_statpval.c (thin wrappers over the routines
in mri_stats.c) and are used by 3dFDR, 3dClustCount, 3dTcorrMap,
3dECM and the AFNI GUI’s threshold slider.
Tag your own output as a statistic so downstream tools (and the GUI’s
p-value readout) understand it – use the EDIT_BRICK_TO_FI* macros:
EDIT_BRICK_TO_FITT(oset, iv, ndof) ; /* mark brick iv as t, ndof DOF */
EDIT_BRICK_TO_FIFT(oset, iv, ndof, ddof) ; /* F-stat */
EDIT_BRICK_TO_FICT(oset, iv, ndof) ; /* chi-square */
EDIT_BRICK_TO_FIZT(oset, iv) ; /* Z */
EDIT_BRICK_TO_NOSTAT(oset, iv) ; /* clear stat info */
1D (text column) I/O¶
AFNI’s .1D files (whitespace-delimited columns: regressors, motion
parameters, ROI averages) are read into an MRI_IMAGE of floats, stored
column-major (nx = rows/time, ny = columns).
MRI_IMAGE *im = mri_read_1D( "motion.1D" ) ; /* also accepts '[..]{..}' selectors */
if( im == NULL ) ERROR_exit("cannot read 1D") ;
float *far = MRI_FLOAT_PTR(im) ;
/* value at row r, column c: */
float v = far[ r + c*im->nx ] ;
...
mri_write_1D( "out.1D", im ) ;
mri_free(im) ;
Like dataset names, mri_read_1D honors selectors – file.1D'[2]' for one
column, file.1D'[0..3]' for a range, and transpose with a trailing '.
Used by 1dplot, 3dDeconvolve (regressors), 1d_tool.py’s C
companions, and anywhere a program takes a -1D input.
Provenance and attributes¶
Call |
What it does |
|---|---|
|
Append this command to the header log. |
|
Carry the input’s history forward. |
|
Name a sub-brick. |
|
Attach a custom string attribute. |
|
Attach int / float attributes. |
Custom “private” attributes survive a write/read round-trip but are not propagated when another program copies the dataset – use them for your own data, not as an interchange contract.
Where to read real callers¶
Dataset creation, navigation, volume/voxel ops, FFT callback:
src/3dToyProg.c(start here).vectim+ correlation:src/3dTcorrelate.c,src/3dAutoTcorrelate.c,src/3dBandpass.c.p-value / statistic conversion:
src/3dFDR.c,src/thd_statpval.c.NIML over sockets:
src/3dGroupInCorr.c,src/plugout_drive.c.Surfaces:
src/SUMA/SUMA_SurfMeasures.c,src/SUMA/SUMA_3dVol2Surf.c.