Source code for spm1d.io
'''
Saving and loading results as HDF5 files.
'''
# Copyright (C) 2026 Todd Pataky
def _load_from_h5group(group):
'''
Load a saved result from an open HDF5 group.
'''
if 'islist' in group.attrs.keys(): # a multi-effect result such as an ANOVA or MANOVA
from . _results.lists import SPMList
return SPMList._from_h5group( group )
spm = None
dim = int( group.attrs['dim'] )
isinference = bool( group.attrs['isinference'] )
if (dim==0):
if isinference:
from . _results.spmi import SPMi0D
spm = SPMi0D._from_h5group( group )
else:
from . _results.spm import SPM0D
spm = SPM0D._from_h5group( group )
elif (dim==1):
if isinference:
from . _results.spmi import SPMi1D
spm = SPMi1D._from_h5group( group )
else:
from . _results.spm import SPM1D
spm = SPM1D._from_h5group( group )
return spm
[docs]
def load(fpath):
'''
Load a test result from an HDF5 file.
:Parameters:
- *fpath* --- HDF5 file path
:Returns:
- SPM (before or after inference) or list of SPMs (multi-effect designs)
:Example:
Load the inference results saved in the ``save`` example.
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.PlantarArchAngle()
>>> y, A = dataset.y, dataset.x # (20,101) array and (20,) array
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> spm1d.io.save( 'ttest_paired.h5', spmi )
>>> loaded = spm1d.io.load( 'ttest_paired.h5' )
>>> print( loaded )
SPM{t} (1D) inference
fn_name : ttest_paired
STAT : T
z : (101,) array
df : (1, 9)
Smoothness estimates:
fwhm : 23.819
lkc : 6.991
resels : (1, 4.19835)
Inference results:
method : rft
isparametric : True
alpha : 0.05
dirn : 0
zc : 3.82866
zmax : 6.552
h0reject : True
p_zmax : 0.002
p_set : 0.040
p_cluster : [0.040]
p_point : None
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
import h5py
from . util._serial import H5Group
from . exceptions import SPM1DFileError
try:
f = h5py.File(fpath, 'r')
except OSError as e:
raise SPM1DFileError( f'The given file could not be opened as an HDF5 file: {fpath}' ) from e
with f:
if 'spm' not in f:
raise SPM1DFileError( f'The given HDF5 file holds no spm1d result: {fpath}' )
spm = _load_from_h5group( H5Group( f['spm'] ) )
return spm
[docs]
def save(fpath, obj):
'''
Save a test result to an HDF5 file.
:Parameters:
- *fpath* --- HDF5 file path
- *obj* --- SPM (before or after inference) or list of SPMs
:Returns:
- None
:Example:
Save the inference results of a paired t test on the plantar arch angle
dataset and then load them again.
>>> import spm1d
>>> import jikudata as jd
>>> dataset = jd.PlantarArchAngle()
>>> y, A = dataset.y, dataset.x # (20,101) array and (20,) array
>>> y0, y1 = y[ A == 0 ], y[ A == 1 ]
>>> spmi = spm1d.stats.ttest_paired( y0, y1 ).inference( 0.05 )
>>> spm1d.io.save( 'ttest_paired.h5', spmi )
>>> loaded = spm1d.io.load( 'ttest_paired.h5' )
>>> print( type( loaded ).__name__, loaded.h0reject )
SPMi1D True
The example datasets require the jiku-data package:
``pip install "spm1d[data]"``.
'''
from . _results._base import _SPM, _SPMListBase
from . _results.lists import SPMPostHocList
if isinstance(obj, SPMPostHocList) or not isinstance(obj, (_SPM, _SPMListBase)):
from . exceptions import SPM1DTypeError
raise SPM1DTypeError( 'spm1d.io.save supports SPM objects and SPM lists (the result of a '
'multi-effect design). The given value is of type %s.' % type(obj).__name__ )
import h5py
from . util._serial import H5Group
with h5py.File(fpath, 'w') as f:
group = H5Group( f.create_group('spm') )
obj.write_to_h5group( group )
# an unknown public name raises SPM1DAttributeError with a suggestion (see spm1d._attr)
from . _attr import module_getattr as _module_getattr
__getattr__ = _module_getattr( __name__, globals(), hints={
'loadmat' : 'The v0.4 function was removed. Please use "scipy.io.loadmat".',
'savemat' : 'The v0.4 function was removed. Please use "scipy.io.savemat".',
'loadtxt' : 'The v0.4 function was removed. Please use "numpy.loadtxt".',
'savetxt' : 'The v0.4 function was removed. Please use "numpy.savetxt".',
'loadspm' : 'The v0.4 function was removed. Please use "spm1d.io.load".',
'savespm' : 'The v0.4 function was removed. Please use "spm1d.io.save".',
} )