Skip to content

Commit

Permalink
Merge pull request #31 from Bradley-Karat/patch-1
Browse files Browse the repository at this point in the history
Update get_phase_encode_txt.py

Adding support for different vendors json naming conventions (ex. PhaseEncodingDirection vs. PhaseEncodingAxis)

And EstimatedEffectiveEchoSpacing

Note that the PhaseEncodingAxis tag in the json file never includes the direction (ie is just i,j,k, never with a '-' in front). So for rev phase encoded acquisitions would still need to find the direction some other way..
  • Loading branch information
akhanf authored Jan 23, 2023
2 parents b7d795e + 6d3f1a3 commit 6b2a256
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions snakedwi/workflow/scripts/get_phase_encode_txt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import json
import numpy as np


# load nifti
bzero = nib.load(snakemake.input.bzero_nii)

Expand All @@ -15,8 +14,13 @@

imsize = np.array(bzero.header.get_data_shape())

phenc_axis = json_dwi["PhaseEncodingDirection"][0] # either i, j, k

if "PhaseEncodingDirection" in json_dwi:
phenc_axis = json_dwi["PhaseEncodingDirection"][0]
phenc_string = "PhaseEncodingDirection"
elif "PhaseEncodingAxis" in json_dwi:
phenc_axis = json_dwi["PhaseEncodingAxis"][0] # either i, j, k
phenc_string = "PhaseEncodingAxis"

if phenc_axis == "i":
vec = np.array([1, 0, 0])
elif phenc_axis == "j":
Expand All @@ -25,28 +29,34 @@
vec = np.array([0, 0, 1])

# print(f'vec: {vec}')
# print(f'imsize: {imsize}')
# print(f'imsize: {imsize}')f

numPhaseEncodes = imsize[np.where(vec > 0)]

# print(f'numPhaseEncodes: {numPhaseEncodes}')

# check for i-, j-, k-; flip to -1 if so..
if len(json_dwi["PhaseEncodingDirection"]) == 2:
if json_dwi["PhaseEncodingDirection"][1] == "-":
if len(json_dwi[phenc_string]) == 2:
if json_dwi[phenc_string][1] == "-":
vec[np.where(vec > 0)] = -1

if not "EffectiveEchoSpacing" in json_dwi:
if "EffectiveEchoSpacing" in json_dwi:
phenc_line = np.hstack(
[vec, np.array(json_dwi["EffectiveEchoSpacing"] * numPhaseEncodes)]
)
elif "EstimatedEffectiveEchoSpacing" in json_dwi:
print("Estimtated Effective Echo Spacing found, using that")
phenc_line = np.hstack(
[vec, np.array(json_dwi["EstimatedEffectiveEchoSpacing"] * numPhaseEncodes)]
)
else:
print("EffectiveEchoSpacing not defined in JSON, using default value")
json_dwi["EffectiveEchoSpacing"] = snakemake.config[
"default_effective_echo_spacing"
]

json_dwi["EffectiveEchoSpacing"] = snakemake.config["default_effective_echo_spacing"]

# create the phenc_line row
phenc_line = np.hstack(
[vec, np.array(json_dwi["EffectiveEchoSpacing"] * numPhaseEncodes)]
)

#phenc_line = np.hstack(
# [vec, np.array(json_dwi["EffectiveEchoSpacing"] * numPhaseEncodes)]
#)

# replicate to the number of volumes, if it is 4d
if len(imsize) == 4:
Expand Down

0 comments on commit 6b2a256

Please sign in to comment.