Skip to content

NHP DiffusionPreprocessing#388

Open
TakJim wants to merge 28 commits into
Washington-University:masterfrom
RIKEN-BCIL:NHP_DiffusionPreprocessing
Open

NHP DiffusionPreprocessing#388
TakJim wants to merge 28 commits into
Washington-University:masterfrom
RIKEN-BCIL:NHP_DiffusionPreprocessing

Conversation

@TakJim

@TakJim TakJim commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

This PR adds NHP-adapted versions of the DiffusionPreprocessing pipeline scripts.

These scripts are the current working versions.

  • Integration with the existing HCP DiffusionPreprocessing scripts has not yet been done and will be addressed in subsequent commits.

Files Added

  • DiffusionPreprocessing/DiffPreprocPipelineNHP.sh
  • DiffusionPreprocessing/DiffPreprocPipeline_EddyNHP.sh
  • DiffusionPreprocessing/DiffPreprocPipeline_PostEddyNHP.sh
  • DiffusionPreprocessing/DiffPreprocPipeline_PreEddyNHP.sh

TakJim added 2 commits March 16, 2026 18:19
Add NHP-adapted versions of the four DiffusionPreprocessing pipeline scripts:
- DiffPreprocPipelineNHP.sh
- DiffPreprocPipeline_EddyNHP.sh
- DiffPreprocPipeline_PostEddyNHP.sh
- DiffPreprocPipeline_PreEddyNHP.sh
- Replace --specieslabel (numeric) with --species (string, default "Human")
  matching PreFreeSurfer PR Washington-University#350 pattern
- Add SPECIES to SpeciesLabel case mapping for NHP sub-scripts
- Branch on SPECIES=="Human" vs !="Human" for PreEddy, Eddy, PostEddy:
  Human path calls original .sh with all HCP params (100% backward compat)
  NHP path calls *NHP.sh with NHP-specific params
- validate_scripts(): retain all Human checks incl run_eddy.sh,
  add NHP script checks conditionally
- Add --usephasezero pass-through to PreEddyNHP.sh
- glibc workaround scoped to NHP path only
Comment thread DiffusionPreprocessing/DiffPreprocPipeline.sh Outdated
Comment thread DiffusionPreprocessing/DiffPreprocPipeline.sh Outdated
Comment thread DiffusionPreprocessing/DiffPreprocPipelineNHP.sh Outdated
TakJim added 5 commits March 24, 2026 06:52
…ddy.sh

- Add NHP options: --species, --specieslabel, --truepatientposition,
  --scannerpatientposition, --usephasezero
- Add SPECIES branching for input data reorient (CorrectVolumeOrientation
  + Rotate_bvecs.sh for NHP, simple imcp for Human)
- Add SPECIES branching for basic_preproc (NHP uses basic_preproc.sh,
  Human uses norm_intensity + best_b0/sequence)
- Add SPECIES branching for topup (NHP: optional T2w phase-zero insertion
  + run_topupNHP.sh, Human: run_topup.sh)
- Update main DiffPreprocPipeline.sh to call unified PreEddy for both
  Human and NHP, removing the PreEddyNHP.sh dispatch
- Add --species option to select Human or NHP eddy path
- Human: run_eddy.sh with full GPU/CUDA support (unchanged)
- NHP: run_eddyNHP.sh with -g flag and JacobianResampling marker
- Update main DiffPreprocPipeline.sh to call unified Eddy for both
  Human and NHP, removing the EddyNHP.sh dispatch
…tEddy.sh

- Add NHP options: --species, --specieslabel, --wmprojabs
- SPECIES branching for eddy_postproc (Human: eddy_postproc.sh,
  NHP: eddy_postprocNHP.sh with SpeciesLabel)
- SPECIES branching for DiffusionToStructural (Human: DiffusionToStructural.sh,
  NHP: DiffusionToStructuralNHP.sh with --fsbbrdiff and --wmprojabs)
- SPECIES branching for eddy log copy and cnr_maps handling
- Update main DiffPreprocPipeline.sh to call unified PostEddy for both
  Human and NHP, removing the PostEddyNHP.sh dispatch
- All four Diffusion sub-scripts are now unified
The NHP-specific logic has been fully merged into the existing
HCP DiffPreprocPipeline.sh / _PreEddy.sh / _Eddy.sh / _PostEddy.sh
via SPECIES branching, so the separate NHP-suffixed entry points
(DiffPreprocPipelineNHP.sh and its PreEddy/Eddy/PostEddyNHP counterparts)
are no longer needed.
Per review feedback (coalsont), the glibc-2.14 path should not be in
LD_LIBRARY_PATH by default, and if a site needs this workaround it
belongs in their edited SetUpHCPPipeline.sh / Examples batch, not in
a pipeline script.
@mmilch01

Copy link
Copy Markdown
Contributor

Please add the example batch file NHP.

@TakJim

TakJim commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

Added Examples/Scripts/DiffusionPreprocessingBatchNHP.sh in 35a60c4.

Modeled on PreFreeSurferPipelineBatchNHP.sh:

  • CLI args --StudyFolder / --Subject / --Species / --runlocal
  • Sources SetUpSPECIES.sh (does not source SetUpFSNHP.sh — keeps FS-specific setup out of the diffusion pipeline per the PR NHP fMRIVolume #372 discussion)
  • Reads per-subject parameters from ${StudyFolder}/${Subject}/RawData/hcppipe_conf.txt (DiffPosData, DiffNegData, DiffEchoSpacingSec, DiffPEdir, DiffTruePatientPosition, DiffScannerPatientPosition, UseDWIPhaseZero, DiffTopupConfig, DiffResamp, ExtraEddyArgs) with a commented manual-fallback block
  • Passes NHP options to the unified DiffPreprocPipeline.sh: --species, --wmprojabs, --truepatientposition, --scannerpatientposition, --usephasezero, plus optional --resamp, --topup-config-file, and repeatable --extra-eddy-arg (for enabling eddy slice-to-volume correction, etc.)

Species-specific default values (e.g., DiffWMProjAbs) are expected to come from SetUpSPECIES.sh; the batch provides an override path via hcppipe_conf.txt. Happy to adjust naming or restructure if the maintainers prefer a different convention.

Comment thread Examples/Scripts/DiffusionPreprocessingBatchNHP.sh Outdated
Comment thread Examples/Scripts/DiffusionPreprocessingBatchNHP.sh
Comment thread DiffusionPreprocessing/DiffPreprocPipeline_Eddy.sh Outdated
Comment thread DiffusionPreprocessing/DiffPreprocPipeline_PostEddy.sh Outdated
Comment thread DiffusionPreprocessing/DiffPreprocPipeline_PostEddy.sh Outdated
Comment thread Examples/Scripts/DiffusionPreprocessingBatchNHP.sh
TakJim added 11 commits April 28, 2026 12:06
- Set EnvironmentScript at top with other batch settings, drop fallback default
  (coalsont review on PR Washington-University#388)
- Move manual fallback block out of else-branch so it can be uncommented
  regardless of hcppipe_conf.txt presence (glasserm suggestion on PR Washington-University#388)
Adds --species option (default 'Human'). When non-Human:
- append --data_is_shelled (NHP data often not perfectly shelled)
- when --resamp=lsr, also append --fep (required for LSR on NHP)

Towards integrating run_eddyNHP.sh into run_eddy.sh per glasserm review
on PR Washington-University#388 (DiffPreprocPipeline_Eddy.sh:246).
Removes the Human/NHP if/else that called run_eddyNHP.sh separately. Now
both species go through run_eddy.sh, gated by --species=$SPECIES. The
JacobianResampling marker (used by PostEddy to detect LSR vs JAC) is kept
behind an explicit SPECIES != Human guard since it is NHP-only behavior.

Towards integrating run_eddyNHP.sh into run_eddy.sh per glasserm review on
PR Washington-University#388 (DiffPreprocPipeline_Eddy.sh:246).
Adds 5th positional arg BetSpeciesLabel (default 0 = Human). When >=1,
uses bet4animal with a species-specific bet fraction (0.3/0.4/0.5/0.6
for chimp/macaque/marmoset/other) and -z $BetSpeciesLabel, instead of
standard 'bet -f 0.1'.

Towards integrating eddy_postprocNHP.sh into eddy_postproc.sh per
glasserm review on PR Washington-University#388 (DiffPreprocPipeline_PostEddy.sh:192).
Two NHP-only behaviors gated by BetSpeciesLabel != 0:
- LSR resampling path: when JacobianResampling marker is absent, eddy has
  already combined Pos/Neg with LSR; skip eddy_quad QC and copy bvals/
  bvecs/data through directly.
- CNR map NaN rescue: spline-interpolated CNR maps can carry NaN on
  low-SNR NHP data; fall back to sinc interpolation if 'fslstats -m'
  reports NaN.

Towards integrating eddy_postprocNHP.sh into eddy_postproc.sh per
glasserm review on PR Washington-University#388 (DiffPreprocPipeline_PostEddy.sh:192).
Removes the Human/NHP if/else that called eddy_postprocNHP.sh
separately. Now both species go through eddy_postproc.sh, with
SpeciesLabel passed as the 5th positional arg (0 = Human, >=1 = NHP).
Longitudinal Human skip path is preserved.

Also drops the validate_scripts checks for the now-unified NHP variants
(eddy_postprocNHP.sh and DiffusionToStructuralNHP.sh — the latter is
removed here for symmetry; the call-site dispatch for
DiffusionToStructural is updated in a follow-up commit).

Towards integrating eddy_postprocNHP.sh per glasserm review on
PR Washington-University#388 (DiffPreprocPipeline_PostEddy.sh:192).
…brdiff

Adds three NHP-tunable getopt1 options (defaults selected so the Human
path is unchanged):
- --species (default 'Human')
- --wmprojabs (default 2 mm; species-specific WM projection absolute
  distance for FreeSurfer bbregister)
- --fsbbrdiff (default TRUE; set NONE to skip FS bbregister on low-
  quality NHP diffusion data)

Behavioral changes that consume these options follow in subsequent
commits.

Towards integrating DiffusionToStructuralNHP.sh into
DiffusionToStructural.sh per glasserm review on PR Washington-University#388
(DiffPreprocPipeline_PostEddy.sh:195).
For NHP (SPECIES != Human), run dtifit and use the predicted b=0 image
(dti_S0) as the registration moving image instead of raw nodif. This
gives more robust bbregister initialization on low-SNR diffusion data
(TH 2016).

Human path is unchanged (regimg=nodif).

Towards integrating DiffusionToStructuralNHP.sh into the human version.
Two NHP-only behaviors gated by SPECIES != 'Human':
- bbregister extra args: switch from --bold to --dti contact mode and,
  when WMProjAbs < 2, append '--wm-proj-abs $WMProjAbs --brute1max 2
  --brute1delta 2' (species-tuned white-matter projection depth).
- FSBBRDIFF=NONE fallback: skip FS bbregister + tkregister2 entirely
  and use the FLIRT BBR result (regimg2T1w_initII.mat) as diff2str.mat.
  Recommended for low-quality data or fieldmap-corrected data (TH).

Human path is unchanged (uses --bold and the existing bbregister call).

Towards integrating DiffusionToStructuralNHP.sh into the human version.
Removes the Human/NHP if/else that called DiffusionToStructuralNHP.sh
separately. Both species now go through DiffusionToStructural.sh, with
--species/--wmprojabs/--fsbbrdiff passed unconditionally (the NHP-only
flags are no-ops on the Human path).

Also refreshes the validate_scripts comment in DiffPreprocPipeline.sh
to reflect that no separate *NHP.sh variants exist anymore.

Towards integrating DiffusionToStructuralNHP.sh per glasserm review on
PR Washington-University#388 (DiffPreprocPipeline_PostEddy.sh:195).
Per Matt issuecomment-4297611428 and Tim 4/23 NHP Pipelines MTG
discussion: the FSBBRDIFF and DiffWMProjAbs settings were
inadvertently dropped during a conflict resolution on PR Washington-University#364.
They belong in this diffusion PR, not in the FreeSurfer PR.

Restored 5 species sections matching the pre-conflict state at
commit 9ae9e97:

  Human         FSBBRDIFF=TRUE  DiffWMProjAbs="2"
  Chimp         FSBBRDIFF=TRUE  DiffWMProjAbs="1"
  Macaque       FSBBRDIFF=TRUE  DiffWMProjAbs="0.7"
  Marmoset      FSBBRDIFF=TRUE  DiffWMProjAbs="0.5"
  NightMonkey   FSBBRDIFF=TRUE  DiffWMProjAbs="0.3"

Verified the restored block content matches the reference
9ae9e97 byte-for-byte for all 5 species.
TakJim added 2 commits April 30, 2026 04:52
DiffPreprocPipeline_PreEddy.sh L500 calls run_topupNHP.sh on
the NHP path, but the script was missing from this branch
after the unification work. Copy it verbatim from the BCIL
NHP staging tree (scripts/run_topupNHP.sh, 105 lines, 100755).

This restores NHP runtime: bet4animal-based hifi b0 brain
extraction with species-tuned BetSpeciesLabel + betfraction
that the unified run_topup.sh (Human-only bet -f 0.2) doesn't
cover. No Human-side change.
…ffusionToStructural

Per Matt 4/23 NHP Pipelines MTG TODO E: bring the NHP-aware
versions of the three dependency scripts into the PR Washington-University#388
branch by merging the integration-draft branch:

  4ca1c38 run_eddy.sh: add --species and NHP-specific eddy flags
  5488e58 DiffPreprocPipeline_Eddy.sh: unify SPECIES dispatch via run_eddy.sh
  9dbd649 eddy_postproc.sh: add BetSpeciesLabel for NHP brain extraction
  412d707 eddy_postproc.sh: add NHP LSR-resampling branch and CNR NaN rescue
  c6d6297 DiffPreprocPipeline_PostEddy.sh: unify eddy_postproc dispatch
  07461f5 DiffusionToStructural.sh: add NHP options --species/--wmprojabs/--fsbbrdiff
  4772528 DiffusionToStructural.sh: use dti_S0 as registration target for NHP
  e97495e DiffusionToStructural.sh: add NHP FS BBR options and fallback path
  ae7b97d DiffPreprocPipeline_PostEddy.sh: unify DiffusionToStructural dispatch
  9cffd8d DiffusionPreprocessing: add run_topupNHP.sh

The NHP path now references unified scripts gated by --species,
with run_topupNHP.sh restored verbatim to satisfy the existing
PreEddy.sh dispatch. No conflicts: this branch's 6ac2c21 only
touched Examples/Scripts/SetUpSPECIES.sh.
@TakJim

TakJim commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

Done in 6ac2c21. Restored the FSBBRDIFF=TRUE and DiffWMProjAbs= blocks for all 5 species (Human, Chimp, Macaque, Marmoset, NightMonkey) in Examples/Scripts/SetUpSPECIES.sh, matching the pre-conflict state at 9ae9e97 byte-for-byte.

@glasserm

Copy link
Copy Markdown
Contributor

@coalsont why are there merge conflicts?

Comment thread DiffusionPreprocessing/DiffPreprocPipeline_PreEddy.sh Outdated
@coalsont

Copy link
Copy Markdown
Member

@coalsont why are there merge conflicts?

Because I added a few lines to the end of SetUpSPECIES in master, which also has the diffusion BBR parameters added in this PR.

TakJim added 4 commits May 13, 2026 11:48
…mmented-out config-dispatch block

- Remove commented-out SPECIES config-dispatch block (deprecated config selection now lives upstream in TopupConfig)
- Drop deprecated_topup_config_file branches (variable was never defined; those code paths would have failed at runtime)
- Drop partopupdir indirection (was immediately overwritten to $FSLDIR/bin); call topup directly via $FSLDIR/bin
- No behavior change for paths that were actually exercised
…=0 default)

- Add optional 3rd arg SpeciesLabel (defaults to 0 = Human, preserving the
  existing 2-arg signature for Human callers)
- SpeciesLabel=0 path is byte-identical to the previous Human pathway
- SpeciesLabel!=0 path is the cleaned-up NHP pathway from run_topupNHP.sh:
  parallel topup, T2w-phase-zero variant when Pos_Neg_NoZero_b0 is present,
  per-species BET fraction, bet4animal
- Caller update and run_topupNHP.sh removal follow in the next commit
…opup.sh

- Update NHP branch call site to use run_topup.sh with SpeciesLabel arg
- Remove run_topupNHP.sh existence check (script no longer exists)
- Delete DiffusionPreprocessing/scripts/run_topupNHP.sh
…rfer convention)

Match the PreFreeSurfer/ACPCAlignment.sh and BrainExtraction_FNIRTbased.sh
pattern: receive SPECIES as an argument and convert internally to the
numeric SpeciesLabel used by bet4animal -z, instead of having the caller
pre-compute and pass SpeciesLabel.

- run_topup.sh: 3rd arg is now SPECIES (defaults to Human). Internal
  case-statement derives SpeciesLabel using the same mapping as
  PreFreeSurfer scripts (Human=0, Chimp=1, Macaque=2, Marmoset=3,
  NightMonkey=4).
- DiffPreprocPipeline_PreEddy.sh L497: pass ${SPECIES} instead of
  ${SpeciesLabel}.
- The 2-arg invocation (Human pathway) on L477 continues to work since
  SPECIES defaults to Human inside run_topup.sh.
Comment thread DiffusionPreprocessing/DiffPreprocPipeline_PreEddy.sh Outdated
Comment thread DiffusionPreprocessing/DiffPreprocPipeline_PreEddy.sh Outdated
TakJim added 3 commits May 26, 2026 15:07
Add the combined NHP basic_preproc script as basic_preprocNHP.sh
(naming consistent with eddy_postprocNHP/run_eddyNHP/run_topupNHP).
Update DiffPreprocPipeline_PreEddy.sh NHP branch to call and validate
basic_preprocNHP.sh instead of basic_preproc.sh.
Drop the Human/non-Human branch in DiffPreprocPipeline_PreEddy.sh so all
species share the split basic_preproc_{norm_intensity,best_b0,sequence}.sh
scripts. NHP-specific behaviour is handled via $SPECIES inside run_topup.sh
(now always passed) and the T2w phase-zero block, which is now explicitly
gated to non-Human. Delete the obsolete combined basic_preprocNHP.sh.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants