Skip to content

Commit

Permalink
Merge branch 'nb_4D_fix' into 'dev'
Browse files Browse the repository at this point in the history
Fixes for merging Nifty slices/volumes into volumes/4D

See merge request CyclotronResearchCentre/Public/bidstools/bidsme/bidsme!8
  • Loading branch information
nbeliy committed Mar 6, 2024
2 parents 2f0425a + 09ff84e commit d7607a3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Fixed
- Plugin/Nibabel: crash in Convert3Dto4D when removing merged files

### Added
- Plugin/Nibabel: Convert3Dto4D will also remove json files, not only nifti
- Plugin/Nibabel: Convert3Dto4D will conserve the written data scale, if all merged files have the same slope and intercept

### Changed
- Plugin/Nibabel: Convert3Dto4D will produce an error when trying merging of 2-file nifti (hdr/img)


## [1.6.2] - 2024-03-04

### Fixed
Expand Down
37 changes: 30 additions & 7 deletions bidsme/plugins/tools/Nibabel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import nibabel

from bidsme.Modules import baseModule
from bidsme.tools import tools

logger = logging.getLogger(__name__)

Expand All @@ -38,15 +39,18 @@ def Convert3Dto4D(outfolder: str,
check_affines: bool = True, axis: int = None) -> str:
"""
Concat nii images from recording into one 4D image, using
nibabel.funcs.concat_images function
nibabel.funcs.concat_images function. The scaling parameters
of Nifti will be preserved if all files has the same slope and
intercept, otherwise, the scaling will be recalculated.
It will work only on single file nii images, not with hdr/img pair.
If number of files in recording is too small (0, 1, <=skip)
a warning will be shown and no concatination will be performed
Original 3D images are removed, and filelist in recording
is adapted.
Original 3D images are removed.
This function is intended to be run in SequennceEndEP, in order
This function is intended to be run in SequenceEndEP, in order
to avoid conflicts due to file removal
Resulting file will be named as first file used to concatenation
Expand Down Expand Up @@ -103,10 +107,29 @@ def Convert3Dto4D(outfolder: str,
f_list = [file for file in f_list
if os.path.exists(file)]

img = nibabel.funcs.concat_images(f_list,
imgs = [nibabel.load(f) for f in f_list]
slope = imgs[0].dataobj.slope
inter = imgs[0].dataobj.inter

for img in imgs[1:]:
if slope != img.dataobj.slope or inter != img.dataobj.inter:
logger.warning('Concatenation of images of different scale. '
'Will recalculate scale for all files.')
slope = None
inter = None
break

img = nibabel.funcs.concat_images(imgs,
check_affines=check_affines,
axis=axis)
for file in recording.files:
os.remove(os.path.join(outfolder, file))
img.header.set_slope_inter(slope, inter)
img.to_filename(f_list[0])

for file in f_list[1:]:
os.remove(file)

aux_file = tools.change_ext(file, "json")
if os.path.isfile(aux_file):
os.remove(aux_file)

return f_list[0]

0 comments on commit d7607a3

Please sign in to comment.