Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions AFQ/tasks/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,107 @@ def rumba_params(
]


def _tournier07_to_descoteaux07(sh):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is the best way to do this. @36000 : I think you have some code that does this without the projection to a sphere? Could you please share?

"""
Project SH coefficients in the tournier07 basis (MRtrix3's, which
cs_dmri fits in) onto the legacy descoteaux07 basis pyAFQ uses
"""
sh_order = shm.order_from_ncoef(sh.shape[-1])
sphere = get_sphere(name="repulsion724")
B = shm.sh_to_sf_matrix(
sphere,
sh_order_max=sh_order,
basis_type="tournier07",
legacy=False,
return_inv=False,
)
_, invB = shm.sh_to_sf_matrix(sphere, sh_order_max=sh_order)
return sh @ (B @ invB)


@immlib.calc("ss3tcsd_params", "ss3tcsd_gm", "ss3tcsd_csf")
@as_file(
suffix=[
"_model-ss3tcsd_param-wm_dwimap.nii.gz",
"_model-ss3tcsd_param-gm_dwimap.nii.gz",
"_model-ss3tcsd_param-csf_dwimap.nii.gz",
],
subfolder="models",
)
@as_img
def ss3t_params(gtab, data, brain_mask, citations, ss3t_sh_order_max=8):
"""
full path to a nifti file containing
parameters for the SS3T CSD white matter fit,
full path to a nifti file containing
parameters for the SS3T CSD gray matter fit,
full path to a nifti file containing
parameters for the SS3T CSD cerebrospinal fluid fit

Parameters
----------
ss3t_sh_order_max : int, optional.
Spherical harmonic order to use for the SS3T CSD fit.
Default: 8

References
----------
.. [1] T. Dhollander and A. Connelly. A novel iterative approach
to reap the benefits of multi-tissue CSD from just
single-shell (+b=0) diffusion MRI data. ISMRM (2016), p. 3010
"""
try:
import cs_dmri
except ImportError as err:
raise ImportError(
"SS3T CSD requires cs_dmri: pip install 'pyAFQ[cs_dmri]'"
) from err
citations.add("dhollander2016novel")

dwi = cs_dmri.DWI(data, gtab, mask=nib.load(brain_mask).get_fdata())
responses = dwi.estimate_responses(lmax_wm=ss3t_sh_order_max)
logger.info("Fitting Single-Shell 3-Tissue CSD model...")
ss3t_fit = (
cs_dmri.SS3TModel(
dwi.gtab, responses, lmax_wm=ss3t_sh_order_max, bvec_frame="image"
)
.fit(dwi)
.mtnormalise()
)

sh_order = ss3t_fit.lmax_wm

def _get_meta(desc, sh_order, response):
return dict(
Model=dict(
Description=(
"Single-Shell 3-Tissue (SS3T) "
"Constrained Spherical Deconvolution (CSD)"
),
URL="https://github.com/PennLINC/cs_dmri",
),
Description=desc,
NonNegativity="constrained",
OrientationEncoding=dict(
EncodingAxis=3,
Reference="xyz",
SphericalHarmonicBasis="descoteaux",
SphericalHarmonicDegree=sh_order,
Type="sh",
),
ResponseFunction=dict(Coefficients=response.coeffs.tolist(), Type="zonal"),
)

return [
(
_tournier07_to_descoteaux07(ss3t_fit.wm),
_get_meta("White matter", sh_order, responses.wm),
),
(ss3t_fit.gm, _get_meta("Gray matter", 0, responses.gm)),
(ss3t_fit.csf, _get_meta("Cerebro-spinal fluid", 0, responses.csf)),
]


@immlib.calc("opdt_params", "opdt_gfa")
@as_file(
suffix=[
Expand Down Expand Up @@ -1529,6 +1630,7 @@ def get_data_plan(kwargs):
dki_cp,
dki_cs,
rumba_params,
ss3t_params,
csd_params,
get_bundle_dict,
]
Expand Down
39 changes: 39 additions & 0 deletions AFQ/tests/test_csd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import numpy.testing as npt
import pytest
from dipy.reconst import shm
from dipy.reconst.shm import calculate_max_order

from AFQ.models import csd
Expand Down Expand Up @@ -82,3 +83,41 @@ def test_afod():
)

npt.assert_allclose(aodf_pyafq, aodf_scilpy, atol=1e-6)


def test_tournier07_to_descoteaux07():
"""
The projection matches, on the sphere, cs_dmri's own export of an SS3T
fit to DIPY's descoteaux07 basis
"""
cs = pytest.importorskip("cs_dmri")
from dipy.io.peaks import load_pam

from AFQ.tasks.data import _tournier07_to_descoteaux07

shape, lmax = (5, 5, 4), 8
rng = np.random.default_rng(0)
wm = rng.normal(size=shape + (45,)).astype(np.float32)
responses = cs.ResponseSet(
wm=cs.TissueResponse(
np.array([[3.5, 0, 0, 0, 0], [1.2, -0.6, 0.2, -0.05, 0.01]]), lmax
),
gm=cs.TissueResponse(np.array([[3.0], [1.0]]), 0),
csf=cs.TissueResponse(np.array([[4.0], [0.3]]), 0),
)
# on a RAS+ grid cs_dmri's world frame is the image frame
fit = cs.SS3TFit(
wm=wm,
gm=np.full(shape, 0.05, np.float32),
csf=np.full(shape, 0.02, np.float32),
lmax_wm=lmax,
mask=np.ones(shape, bool),
affine=np.diag([2.0, 2.0, 2.0, 1.0]),
frame="world",
responses=responses,
)
with nbtmp.InTemporaryDirectory() as tmpdir:
fit.export(op.join(tmpdir, "fit.pam5"))
ref = load_pam(op.join(tmpdir, "fit.pam5")).shm_coeff
B, _ = shm.sh_to_sf_matrix(dpd.get_sphere(name="symmetric362"), sh_order_max=lmax)
npt.assert_allclose(_tournier07_to_descoteaux07(wm) @ B, ref @ B, atol=1e-4)
3 changes: 2 additions & 1 deletion AFQ/tractography/tractography.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def track(
The maximum length (mm) in a streamline. Default: 500
odf_model : str or Definition, optional
Can be either a string or Definition. If a string, it must be one of
{"DTI", "CSD", "DKI", "GQ", "RUMBA", "MSMT_AODF", "CSD_AODF", "MSMTCSD"}.
{"DTI", "CSD", "DKI", "GQ", "RUMBA", "MSMT_AODF", "CSD_AODF", "MSMTCSD",
"SS3TCSD"}.
If a Definition, we assume it is a definition of a file containing
Spherical Harmonics coefficients.
Default: "CSD_AODF"
Expand Down
9 changes: 9 additions & 0 deletions docs/source/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@ @article{jeurissen2014multi
publisher={Elsevier}
}

@inproceedings{dhollander2016novel,
title={A novel iterative approach to reap the benefits of multi-tissue {CSD} from just single-shell (+b=0) diffusion {MRI} data},
author={Dhollander, Thijs and Connelly, Alan},
booktitle={Proceedings of the International Society for Magnetic Resonance in Medicine},
volume={24},
pages={3010},
year={2016}
}

@article{smith2012anatomically,
title={Anatomically-constrained tractography: improved diffusion MRI streamlines tractography through effective use of anatomical information},
author={Smith, Robert E and Tournier, Jacques-Donald and Calamante, Fernando and Connelly, Alan},
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ aws =
boto3>=1.14.0
nn =
onnxruntime
cs_dmri =
cs_dmri
gpu =
cuslines[cu13]==2.2.3
onnxruntime-gpu
Expand All @@ -104,3 +106,4 @@ all =
%(afqbrowser)s
%(aws)s
%(nn)s
%(cs_dmri)s
Loading