bart reshape#

===============================================================================================================

The bart reshape command is used in BART to reshape multi-dimensional arrays by changing the shape of the input data according to specified dimensions. It allows you to change the size of one or more dimensions of the data without changing the total number of elements.

Where we can view the full usage string and optional arguments with the -h flag.

!bart reshape -h
Usage: reshape flags dim1 ... dimN <input> <output> 

Reshape selected dimensions.

-h  help

where:

flags: A bitmask specifying which dimensions you want to reshape.

dim1 dim2 ... dimN: The size of each dimension.

<input>: The input file in BART’s .cfl format that you want to reshape.

<output>: The output file where the reshaped data will be saved.

Example for Matrix (Using Bash)#

Example 1.1#

Generate a array with values from 1 to 8#

!bart vec $(seq 1 8) array 
!bart show array
+1.000000e+00+0.000000e+00i	+2.000000e+00+0.000000e+00i	+3.000000e+00+0.000000e+00i	+4.000000e+00+0.000000e+00i	+5.000000e+00+0.000000e+00i	+6.000000e+00+0.000000e+00i	+7.000000e+00+0.000000e+00i	+8.000000e+00+0.000000e+00i

Reshape the array to Dimension as 2 x 2 x 2#

!bart reshape $(bart bitmask 0 1 2) 2 2 2 array matrix_array 
!bart show matrix_array
+1.000000e+00+0.000000e+00i	+2.000000e+00+0.000000e+00i
+3.000000e+00+0.000000e+00i	+4.000000e+00+0.000000e+00i
+5.000000e+00+0.000000e+00i	+6.000000e+00+0.000000e+00i
+7.000000e+00+0.000000e+00i	+8.000000e+00+0.000000e+00i

We can see the dimension for our new matrix (matrix_array) become 2 x 2 x 2

!bart show -m matrix_array
Type: complex float
Dimensions: 16
AoD:	2	2	2	1	1	1	1	1	1	1	1	1	1	1	1	1

Example 1.2#

Reshape the array to Demension 4 x 2#

!bart reshape $(bart bitmask 0 1) 4 2 array matrix_array_1 
!bart show matrix_array_1
+1.000000e+00+0.000000e+00i	+2.000000e+00+0.000000e+00i	+3.000000e+00+0.000000e+00i	+4.000000e+00+0.000000e+00i
+5.000000e+00+0.000000e+00i	+6.000000e+00+0.000000e+00i	+7.000000e+00+0.000000e+00i	+8.000000e+00+0.000000e+00i

We can see the dimension for our new matrix (matrix_array_1) become 4 x 2

!bart show -m matrix_array_1
Type: complex float
Dimensions: 16
AoD:	4	2	1	1	1	1	1	1	1	1	1	1	1	1	1	1

Example for Images (in Python Kernel)#

Use BART’s reshape command to change the dimensions of the image matrix.

# Importing the required libraries
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

import cfl
from bart import bart

1. Generate a multi-coil image using the phantom simulation tool in BART:#

Generate a multi-coil image with size 128x128 and 8 coils.

Note the convention is that the coil dimension is dimension 3

multi_coil_image = bart(1, 'phantom -x 128 -s 8')
print(multi_coil_image.shape)
(128, 128, 1, 8)

The number of coils is located in dimension ‘3’.

We are trying to reshape Dimension ‘1’ & Dimension ‘3’ and the corresponding bitmask for dimension ‘1’ and ‘3’ is calculated to be “10”.

!bart bitmask 1 3 
10

2. Reshape multi-coil images to a flat image using the reshape tool in BART:#

Reshape the Dimension ‘1’ from ‘128’ to ‘128 x 8 = 1024’

Reshape the Dimension ‘3’ from ‘8’ to ‘1’

image_flat = bart(1, 'reshape 10 1024 1', multi_coil_image)
# Visualizing image_flat using Matplotlib
plt.figure(figsize=(15,20))
plt.imshow(abs(image_flat), cmap='gray')
plt.title('Multi Coils Flat Image')
Text(0.5, 1.0, 'Multi Coils Flat Image')
_images/f9568659e405bc717535a6883ae75de8453d6fbf1c2b16ca176e875ffd6835b7.png