Skip to content

Commit

Permalink
some modifications to code formating
Browse files Browse the repository at this point in the history
  • Loading branch information
behrouzvia committed Jan 26, 2024
1 parent 9d67d62 commit 31455dd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
22 changes: 12 additions & 10 deletions shimmingtoolbox/cli/mask.py
Expand Up @@ -305,22 +305,24 @@ def sct(fname_input, fname_output, contrast, centerline, file_centerline, brain,


@mask_cli.command(context_settings=CONTEXT_SETTINGS,
help="Create a mask to shim single voxel MRS."
"voxel position and size can be directly given or these info can be read "
"from the twix raw-data."
help="Create a mask to shim single voxel MRS. "
"Voxel position and size can be directly given or these info can be read "
"from the twix raw-data. "
"The mask is stored by default under the name 'mask_mrs.nii.gz' in the output "
"folder. Return an output nifti file to be used as a mask for MRS shimming.")
@click.option('-i', '--input', 'fname_input', type=click.Path(), required=True,
@click.option('-i', '--input', 'fname_input', type=click.Path(), required=True,
help="Input path of the fieldmap to be shimmed.")
@click.option('-r', '--raw_data', type=click.Path(),
help="Input path of the of the twix raw-data (supported extention .dat) [optional]")
@click.option('-o', '--output', type=click.Path(), default=os.path.join(os.curdir, 'mask_mrs.nii.gz'), show_default=True,
help="Name of the output mask. Supported extensions are .nii or .nii.gz. (default: "
help="Input path of the of the twix raw-data (supported extention .dat)")
@click.option('-o', '--output', type=click.Path(), default=os.path.join(os.curdir, 'mask_mrs.nii.gz'),
show_default=True, help="Name of the output mask. Supported extensions are .nii or .nii.gz. (default: "
"(os.curdir, 'mask_mrs.nii.gz'))")
@click.option('-c', '--center', nargs=3, type=click.FLOAT, help="voxel's center position in mm of the x, y and z of the scanner's coordinate [optional]")
@click.option('-s', '--size', nargs=3, type=click.FLOAT, help="voxel size in mm of the x, y and z of the scanner's coordinate")
@click.option('-c', '--center', nargs=3, type=click.FLOAT, help="Voxel's center position in mm of the x, y and z of "
"the scanner's coordinate")
@click.option('-s', '--size', nargs=3, type=click.FLOAT, help="Voxel size in mm of the x, y and z of the scanner's "
"coordinate")
@click.option('--verbose', type=click.Choice(['info', 'debug']), default='info', help="Be more verbose")
def mrs_mask(fname_input, output, raw_data, center, size, verbose):
def mrs(fname_input, output, raw_data, center, size, verbose):

# Set all loggers
set_all_loggers(verbose)
Expand Down
50 changes: 24 additions & 26 deletions shimmingtoolbox/masking/mask_mrs.py
Expand Up @@ -19,11 +19,10 @@ def mask_mrs(fname_input, raw_data, center, size):
Create a mask to shim single voxel MRS
Args:
fname_input (str): Input path of the fieldmap to be shimmed (supported extention .nii and .nii.gz) [required]
raw_data (str): Input path of the of the twix raw-data (supported extention .dat) [optional]
center (float): voxel's center position in mm of the x, y and z of the scanner's coordinate [optional]
size (float): voxel size in mm of the x, y and z of the scanner's coordinate [optional]
fname_input (str): Input path of the fieldmap to be shimmed (supported extention .nii and .nii.gz)
raw_data (str): Input path of the of the twix raw-data (supported extention .dat)
center (float): voxel's center position in mm of the x, y and z of the scanner's coordinate
size (float): voxel size in mm of the x, y and z of the scanner's coordinate
Returns:
numpy.ndarray: Cubic mask with same dimensions as MRS voxel.
"""
Expand All @@ -34,29 +33,28 @@ def mask_mrs(fname_input, raw_data, center, size):
if raw_data is None:
logger.info(" The raw_data is not provided, creating the mask with the given voxel position and size info")
if center is None or size is None:
raise TypeError('As the raw_data is not provided, the voxel position and size must be given to create the mask (See: st_mask mrs-mask -h)')
raise TypeError('The raw_data is not provided; the voxel position and size are required to proceed')
else:
mrs_voxel_size = size
logger.debug('center:',center )
logger.debug('center:', center)
scanner_coordinate = np.array(center + (1,))
logger.debug(f"Scanner position is: {scanner_coordinate}")

else:
logger.info("Reading the twix raw_data")
run_subprocess(['spec2nii', 'twix', raw_data, '-e', 'image'])
name_nii, ext = splitext(raw_data)
nii = nib.load(name_nii + '.nii.gz')
header_twix = nii.header
affine = nii.affine
position_sag = header_twix['qoffset_x']
position_cor = header_twix['qoffset_y']
position_tra = header_twix['qoffset_z']
logger.debug(f"twix header: {header_twix}")
logger.debug(f"affine: {affine}")
scanner_coordinate = np.array([position_sag, position_cor, position_tra, 1])
logger.debug(f"Scanner position is: {scanner_coordinate}")
mrs_voxel_size = header_twix['pixdim'][1:4]

logger.info("Reading the twix raw_data")
run_subprocess(['spec2nii', 'twix', raw_data, '-e', 'image'])
name_nii, ext = splitext(raw_data)
nii = nib.load(name_nii + '.nii.gz')
header_twix = nii.header
affine = nii.affine
position_sag = header_twix['qoffset_x']
position_cor = header_twix['qoffset_y']
position_tra = header_twix['qoffset_z']
logger.debug(f"twix header: {header_twix}")
logger.debug(f"affine: {affine}")
scanner_coordinate = np.array([position_sag, position_cor, position_tra, 1])
logger.debug(f"Scanner position is: {scanner_coordinate}")
mrs_voxel_size = header_twix['pixdim'][1:4]

logger.debug('mrs_voxel_size is:', mrs_voxel_size)
fmap_nii = nib.load(fname_input)
Expand All @@ -69,17 +67,17 @@ def mask_mrs(fname_input, raw_data, center, size):
voxel_position = np.round(voxel_position)
logger.debug('voxel_position:', voxel_position)

# The given coordinate (I, J, K) is the voxel's center position.
I, J, K = map(int, voxel_position[:3])
# The given coordinate (i, j, k) is the voxel's center position.
i, j, k = map(int, voxel_position[:3])
fmap_resolution = fmap_header['pixdim'][1:4]

# To create a mask, the distance from the center of the MRS voxel to it's edges should be calculated based on number of fieldmap voxels.
# The distance from the center of the MRS voxel to it's edges is calculated based on number of fieldmap voxels.
half_voxel_distances = np.ceil(np.divide(mrs_voxel_size, 2 * fmap_resolution)).astype(int)
sd = half_voxel_distances

# create a zero mask with the same size as the input fieldmap to be shimmed.
mask = np.zeros(fmap_array.shape)

# Change the MRS voxel position to have 1 value.
mask[I - sd[0]:I + sd[0], J - sd[1]:J + sd[1], K - sd[2]:K + sd[2]] = 1
mask[i - sd[0]:i + sd[0], j - sd[1]:j + sd[1], k - sd[2]:k + sd[2]] = 1
return mask

0 comments on commit 31455dd

Please sign in to comment.