|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | """ |
4 | | -Utility operations provided for scil_image_math.py and scil_connectivity_math.py |
| 4 | +Utility operations provided for scil_image_math.py |
| 5 | +and scil_connectivity_math.py |
5 | 6 | They basically act as wrappers around numpy to avoid installing MRtrix/FSL |
6 | 7 | to apply simple operations on nibabel images or numpy arrays. |
7 | 8 | """ |
@@ -54,7 +55,7 @@ def get_image_ops(): |
54 | 55 | """Get a dictionary of all functions relating to image operations""" |
55 | 56 | image_ops = get_array_ops() |
56 | 57 | image_ops.update(OrderedDict([ |
57 | | - ('concatenate', concat), |
| 58 | + ('concatenate', concatenate), |
58 | 59 | ('dilation', dilation), |
59 | 60 | ('erosion', erosion), |
60 | 61 | ('closing', closing), |
@@ -83,6 +84,13 @@ def _validate_imgs(*imgs): |
83 | 84 | raise ValueError('Not all inputs have the same shape!') |
84 | 85 |
|
85 | 86 |
|
| 87 | +def _validate_imgs_concat(*imgs): |
| 88 | + """Make sure that all inputs are images.""" |
| 89 | + for img in imgs: |
| 90 | + if not isinstance(img, nib.Nifti1Image): |
| 91 | + raise ValueError('Inputs are not all images') |
| 92 | + |
| 93 | + |
86 | 94 | def _validate_length(input_list, length, at_least=False): |
87 | 95 | """Make sure the the input list has the right number of arguments |
88 | 96 | (length).""" |
@@ -499,20 +507,30 @@ def invert(input_list, ref_img): |
499 | 507 | return output_data |
500 | 508 |
|
501 | 509 |
|
502 | | -def concat(input_list, ref_img): |
| 510 | +def concatenate(input_list, ref_img): |
503 | 511 | """ |
504 | | - concat: IMGs |
505 | | - Concatenate a list of 3D images into a single 4D image. |
| 512 | + concatenate: IMGs |
| 513 | + Concatenate a list of 3D and 4D images into a single 4D image. |
506 | 514 | """ |
507 | | - _validate_imgs(*input_list, ref_img) |
508 | | - if len(input_list[0].header.get_data_shape()) != 3: |
509 | | - raise ValueError('Concatenate require 3D arrays.') |
| 515 | + |
| 516 | + _validate_imgs_concat(*input_list, ref_img) |
| 517 | + if len(input_list[0].header.get_data_shape()) > 4: |
| 518 | + raise ValueError('Concatenate require 3D or 4D arrays.') |
510 | 519 |
|
511 | 520 | input_data = [] |
512 | 521 | for img in input_list: |
| 522 | + |
513 | 523 | data = img.get_fdata(dtype=np.float64) |
514 | | - input_data.append(data) |
| 524 | + |
| 525 | + if len(img.header.get_data_shape()) == 4: |
| 526 | + data = np.rollaxis(data, 3) |
| 527 | + for i in range(0, len(data)): |
| 528 | + input_data.append(data[i]) |
| 529 | + else: |
| 530 | + input_data.append(data) |
| 531 | + |
515 | 532 | img.uncache() |
| 533 | + |
516 | 534 | return np.rollaxis(np.stack(input_data), axis=0, start=4) |
517 | 535 |
|
518 | 536 |
|
|
0 commit comments