dbdicom.Series.set_ndarray#

Series.set_ndarray(array: ndarray, coords: dict | None = None, slice: dict | None = None)[source]#

Assign new pixel data with a new numpy.ndarray.

Parameters:
  • array (np.ndarray) – array with new pixel data.

  • coords (dict, optional) – Provide coordinates for the array, using a dictionary where the keys list the dimensions, and the values are provided as 1D or meshgrid arrays of coordinates. If data already exist at the specified coordinates, these will be overwritten. If not, the new data will be added to the series.

  • slice (dict, optional) – Provide a slice of existing data that will be overwritten with the new array. The format is the same as the dictionary of coordinates, except that the slice is identified by indices rather than values.

Raises:
  • ValueError – if neither coords or slice or provided, if both are provided, or if the dimensions in coords or slice does not match up with the dimensions of the array.

  • IndexError – when attempting to set a slice in an empty array, or when the indices in slice are out of range of the existing coordinates.

See also

ndarray()

Example

Create a zero-filled array, describing 8 MRI slices each measured at 3 flip angles and 2 repetition times:

>>> coords = {
...    'SliceLocation': np.arange(8),
...    'FlipAngle': [2, 15, 30],
...    'RepetitionTime': [2.5, 5.0],
... }
>>> series = db.zeros((128,128,8,3,2), coords)

Retrieve the array and check that it is populated with zeros:

>>> array = series.ndarray(dims=tuple(coords))
>>> print(np.mean(array))
0.0

Now overwrite the values with a new array of ones in a new shape:

>>> new_shape = (128,128,8)
>>> new_coords = {
...     'SliceLocation': np.arange(8),
... }
>>> ones = np.ones(new_shape)
>>> series.set_ndarray(ones, coords=new_coords)

Retrieve the new array and check shape:

>>> array = series.ndarray(dims=tuple(new_coords))
>>> print(array.shape)
(128,128,8)

Check that the value is overwritten:

>>> print(np.mean(array))
1.0