imagewrapper.py 32.5 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python
#
# imagewrapper.py - The ImageWrapper class.
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
7
8
"""This module provides the :class:`ImageWrapper` class, which can be used
to manage data access to ``nibabel`` NIFTI images.
9

10
11
12
13
14

Terminology
-----------


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
There are some confusing terms used in this module, so it may be of use to
get their definitions straight:

  - *Coverage*:  The portion of an image that has been covered in the data
                 range calculation. The ``ImageWrapper`` keeps track of
                 the coverage for individual volumes within a 4D image (or
                 slices in a 3D image).

  - *Slice*:     Portion of the image data which is being accessed. A slice
                 comprises either a tuple of ``slice`` objects (or integers),
                 or a sequence of ``(low, high)`` tuples, specifying the
                 index range into each image dimension that is covered by
                 the slice.

  - *Expansion*: A sequence of ``(low, high)`` tuples, specifying an
                 index range into each image dimension, that is used to
                 *expand* the *coverage* of an image, based on a given set of
                 *slices*.
33
34
35
"""


36
37
import logging
import collections
38
import itertools as it
39

40
41
import numpy     as np
import nibabel   as nib
42
43
44
45
46
47
48
49

import fsl.utils.notifier as notifier


log = logging.getLogger(__name__)


class ImageWrapper(notifier.Notifier):
50
51
52
    """The ``ImageWrapper`` class is a convenience class which manages data
    access to ``nibabel`` NIFTI images. The ``ImageWrapper`` class can be
    used to:
53

54
55
56
    
      - Control whether the image is loaded into memory, or kept on disk
    
57
      - Incrementally update the known image data range, as more image
58
59
60
        data is read in.


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    *In memory or on disk?*

    The image data will be kept on disk, and accessed through the
    ``nibabel.Nifti1Image.dataobj`` array proxy, if:

     - The ``loadData`` parameter to :meth:`__init__` is ``False``.
     - The :meth:`loadData` method never gets called.
     - The image data is not modified (via :meth:`__setitem__`.

    If any of these conditions do not hold, the image data will be loaded into
    memory and accessed directly, via the ``nibabel.Nifti1Image.get_data``
    method.


    *Image dimensionality*

    
    The ``ImageWrapper`` abstracts away trailing image dimensions of length 1.
    This means that if the header for a NIFTI image specifies that the image
    has four dimensions, but the fourth dimension is of length 1, you do not
    need to worry about indexing that fourth dimension.


    *Data range*

    
    In order to avoid the computational overhead of calculating the image data
    range (its minimum/maximum values) when an image is first loaded in, an
    ``ImageWrapper`` incrementally updates the known image data range as data
    is accessed. The ``ImageWrapper`` keeps track of the image data _coverage_,
    the portion of the image which has already been considered in the data
    range calculation. When data from a region of the image not in the coverage
    is accessed, the coverage is expanded to include this region. The coverage
    is always expanded in a rectilinear manner, i.e. the coverage is always
    rectangular for a 2D image, or cuboid for a 3D image.

    
    For a 4D image, the ``ImageWrapper`` internally maintains a separate
    coverage and known data range for each 3D volume within the image. For a 3D
    image, separate coverages and data ranges are stored for each 2D slice.


103
104
105
106
    The ``ImageWrapper`` implements the :class:`.Notifier` interface.
    Listeners can register to be notified whenever the known image data range
    is updated. The data range can be accessed via the :attr:`dataRange`
    property.
107

108

109
110
111
112
113
114
115
116
117
118
    The ``ImageWrapper`` class uses the following functions (also defined in 
    this module) to keep track of the portion of the image that has currently
    been included in the data range calculation:

    .. autosummary::
       :nosignatures:

       sliceObjToSliceTuple
       sliceTupleToSliceObj
       sliceCovered
119
       calcExpansion
120
       adjustCoverage
121
122
    """

123
    
124
    def __init__(self, image, name=None, loadData=False, dataRange=None):
125
        """Create an ``ImageWrapper``.
126

127
        :arg image:     A ``nibabel.Nifti1Image``.
128

129
130
        :arg name:      A name for this ``ImageWrapper``, solely used for 
                        debug log messages.
131

132
133
134
135
136
137
        :arg loadData:  If ``True``, the image data is loaded into memory.
                        Otherwise it is kept on disk (and data access is
                        performed through the ``nibabel.Nifti1Image.dataobj``
                        array proxy).

        :arg dataRange: A tuple containing the initial ``(min, max)``  data
138
139
                        range to use. See the :meth:`reset` method for
                        important information about this parameter.
140
        """
141

142
143
144
        self.__image = image
        self.__name  = name

145
146
147
148
149
150
151
152
153
154
155
156
        # Save the number of 'real' dimensions,
        # that is the number of dimensions minus
        # any trailing dimensions of length 1
        self.__numRealDims = len(image.shape)
        for d in reversed(image.shape):
            if d == 1: self.__numRealDims -= 1
            else:      break

        # And save the number of
        # 'padding' dimensions too.
        self.__numPadDims = len(image.shape) - self.__numRealDims

157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
        # The internal state is stored
        # in these attributes - they're
        # initialised in the reset method.
        self.__range     = None
        self.__coverage  = None
        self.__volRanges = None
        self.__covered   = False

        self.reset(dataRange)

        if loadData:
            self.loadData()


    def reset(self, dataRange=None):
        """Reset the internal state and known data range of this
        ``ImageWrapper``.

        
        :arg dataRange: A tuple containing the initial ``(min, max)``  data
                        range to use. See the :meth:`reset` method.


        .. note:: The ``dataRange`` parameter is intended for situations where
                  the image data range is known (e.g. it was calculated
182
                  earlier, and the image is being re-loaded). If a
183
184
185
186
187
188
189
190
                  ``dataRange`` is passed in, it will *not* be overwritten by
                  any range calculated from the data, unless the calculated
                  data range is wider than the provided ``dataRange``. 
        """
        
        if dataRange is None:
            dataRange = None, None

191
        image =             self.__image
192
193
194
        ndims =             self.__numRealDims - 1
        nvols = image.shape[self.__numRealDims - 1]

195
196
        # The current known image data range. This
        # gets updated as more image data gets read.
197
        self.__range = dataRange
198

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
        # The coverage array is used to keep track of
        # the portions of the image which have been
        # considered in the data range calculation.
        # We use this coverage to avoid unnecessarily
        # re-calculating the data range on the same
        # part of the image.
        #
        # First of all, we're going to store a separate
        # 'coverage' for each 2D slice in the 3D image
        # (or 3D volume for 4D images). This effectively
        # means a seaprate coverage for each index in the
        # last 'real' image dimension (see above).
        # 
        # For each slice/volume, the the coverage is
        # stored as sequences of (low, high) indices, one
        # for each dimension in the slice/volume (e.g.
        # row/column for a slice, or row/column/depth
        # for a volume).
        #
218
        # All of these indices are stored in a numpy array:
219
220
        #   - first dimension:  low/high index
        #   - second dimension: image dimension
221
        #   - third dimension:  slice/volume index 
222
        self.__coverage = np.zeros((2, ndims, nvols), dtype=np.float32)
223

224
225
226
227
228
229
        # Internally, we calculate and store the
        # data range for each volume/slice/vector
        self.__volRanges = np.zeros((nvols, 2), dtype=np.float32)

        self.__coverage[ :] = np.nan
        self.__volRanges[:] = np.nan
230

231
232
233
234
235
        # This flag is set to true if/when the
        # full image data range becomes known
        # (i.e. when all data has been loaded in).
        self.__covered = False

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        
    @property
    def dataRange(self):
        """Returns the currently known data range as a tuple of ``(min, max)``
        values.
        """
        # If no image data has been accessed, we
        # default to whatever is stored in the
        # header (which may or may not contain
        # useful values).
        low, high = self.__range
        hdr       = self.__image.get_header()

        if low  is None: low  = float(hdr['cal_min'])
        if high is None: high = float(hdr['cal_max'])

        return low, high

    
    @property
    def covered(self):
        """Returns ``True`` if this ``ImageWrapper`` has read the entire
        image data, ``False`` otherwise.
        """
        return self.__covered


    def coverage(self, vol):
        """Returns the current image data coverage for the specified volume
        (for a 4D image, slice for a 3D image, or vector for a 2D images).

        :arg vol: Index of the volume/slice/vector to return the coverage
                  for.

        :returns: The coverage for the specified volume, as a ``numpy``
                  array of shape ``(nd, 2)``, where ``nd`` is the number
                  of dimensions in the volume.
273
274
275

        .. note:: If the specified volume is not covered, the returned array
                  will contain ``np.nan`` values.
276
        """
277
        return np.array(self.__coverage[..., vol])
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294

    
    def loadData(self):
        """Forces all of the image data to be loaded into memory.

        .. note:: This method will be called by :meth:`__init__` if its
                  ``loadData`` parameter is ``True``.
        """

        # If the data is not already loaded, this will
        # cause nibabel to load it. By default, nibabel
        # will cache the numpy array that contains the
        # image data, so subsequent calls to this
        # method will not overwrite any changes that
        # have been made to the data array.
        self.__image.get_data()

295

296
    def __getData(self, sliceobj, isTuple=False):
297
        """Retrieves the image data at the location specified by ``sliceobj``.
298
299
300
301
302
303

        :arg sliceobj: Something which can be used to slice an array, or
                       a sequence of (low, high) index pairs.

        :arg isTuple:  Set to ``True`` if ``sliceobj`` is a sequence of
                       (low, high) index pairs.
304
305
        """

306
        if isTuple:
307
            sliceobj = sliceTupleToSliceObj(sliceobj)
308

309
        # If the image has not been loaded
310
        # into memory, we can use the nibabel
311
312
313
314
315
316
317
318
319
320
        # ArrayProxy. Otheriwse if it is in
        # memory, we can access it directly.
        #
        # Furthermore, if it is in memory and
        # has been modified, the ArrayProxy
        # will give us out-of-date values (as
        # the ArrayProxy reads from disk). So
        # we have to read from the in-memory
        # array to get changed values.
        if self.__image.in_memory: return self.__image.get_data()[sliceobj]
321
        else:                      return self.__image.dataobj[   sliceobj]
322
323


324
325
326
327
328
329
330
    def __imageIsCovered(self):
        """Returns ``True`` if all portions of the image have been covered
        in the data range calculation, ``False`` otherwise.
        """
        
        shape  = self.__image.shape
        slices = zip([0] * len(shape), shape)
331
        return sliceCovered(slices, self.__coverage)
332

333

334
335
336
    def __expandCoverage(self, slices):
        """Expands the current image data range and coverage to encompass the
        given ``slices``.
337
        """
338

339
340
        _, expansions = calcExpansion(slices, self.__coverage)
        expansions    = collapseExpansions(expansions, self.__numRealDims - 1)
341
342
343
344
345

        log.debug('Updating image {} data range [slice: {}] '
                  '(current range: [{}, {}]; '
                  'number of expansions: {}; '
                  'current coverage: {})'.format(
346
                      self.__name,
347
                      slices,
348
349
                      self.__range[0],
                      self.__range[1],
350
                      len(expansions),
351
                      self.__coverage))
352
353
354
355
356
357
358
359
360

        # As we access the data for each expansions,
        # we want it to have the same dimensionality
        # as the full image, so we can access data
        # for each volume in the image separately.
        # So we squeeze out the padding dimensions,
        # but not the volume dimension.
        squeezeDims = tuple(range(self.__numRealDims,
                                  self.__numRealDims + self.__numPadDims))
361
        
362
363
364
365
366
        # The calcExpansion function splits up the
        # expansions on volumes - here we calculate
        # the min/max per volume/expansion, and
        # iteratively update the stored per-volume
        # coverage and data range.
367
368
369
370
371
372
373
374
        for i, exp in enumerate(expansions):

            data = self.__getData(exp, isTuple=True)
            data = data.squeeze(squeezeDims)

            vlo, vhi = exp[self.__numRealDims - 1]

            for vi, vol in enumerate(range(vlo, vhi)):
375

376
377
                oldvlo, oldvhi = self.__volRanges[vol, :]
                voldata        = data[..., vi]
378

379
380
                newvlo = float(np.nanmin(voldata))
                newvhi = float(np.nanmax(voldata))
381

382
383
                if (not np.isnan(oldvlo)) and oldvlo < newvlo: newvlo = oldvlo
                if (not np.isnan(oldvhi)) and oldvhi > newvhi: newvhi = oldvhi
384

385
386
387
388
389
                # Update the stored range and
                # coverage for each volume 
                self.__volRanges[vol, :]  = newvlo, newvhi
                self.__coverage[..., vol] = adjustCoverage(
                    self.__coverage[..., vol], exp)
390

391
392
393
        # Calculate the new known data
        # range over the entire image
        # (i.e. over all volumes).
394
395
        newmin = float(np.nanmin(self.__volRanges[:, 0]))
        newmax = float(np.nanmax(self.__volRanges[:, 1]))
396

397
        oldmin, oldmax = self.__range
398
        self.__range   = (newmin, newmax)
399
400
        self.__covered = self.__imageIsCovered()

401
402
        if any((oldmin is None, oldmax is None)) or \
           not np.all(np.isclose([oldmin, oldmax], [newmin, newmax])):
403
404
405
406
407
408
            log.debug('Image {} range changed: [{}, {}] -> [{}, {}]'.format(
                self.__name,
                oldmin,
                oldmax,
                newmin,
                newmax))
409
410
            self.notify()

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

    def __updateDataRangeOnRead(self, slices, data):
        """Called by :meth:`__getitem__`. Calculates the minimum/maximum
        values of the given data (which has been extracted from the portion of
        the image specified by ``slices``), and updates the known data range
        of the image.

        :arg slices: A tuple of tuples, each tuple being a ``(low, high)``
                     index pair, one for each dimension in the image. 
        
        :arg data:   The image data at the given ``slices`` (as a ``numpy``
                     array).
        """

        # TODO You could do something with
        #      the provided data to avoid
        #      reading it in again.

        self.__expandCoverage(slices)

431

432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
    def __updateDataRangeOnWrite(self, slices, data):
        """Called by :meth:`__setitem__`. Assumes that the image data has
        been changed (the data at ``slices`` has been replaced with ``data``.
        Updates the image data coverage, and known data range accordingly.

        :arg slices: A tuple of tuples, each tuple being a ``(low, high)``
                     index pair, one for each dimension in the image. 
        
        :arg data:   The image data at the given ``slices`` (as a ``numpy``
                     array). 
        """

        overlap = sliceOverlap(slices, self.__coverage)

        # If there's no overlap between the written
        # area and the current coverage, then it's
        # easy - we just expand the coverage to
        # include the newly written area.
        if overlap in (OVERLAP_SOME, OVERLAP_ALL):

            # If there is overlap between the written
            # area and the current coverage, things are
            # more complicated, because the portion of
            # the image that has been written over may
            # have contained the currently known data
            # minimum/maximum. We have no way of knowing
            # this, so we have to reset the coverage (on
            # the affected volumes), and recalculate the
            # data range.

            # TODO Could you store the location of the
            #      data minimum/maximum (in each volume),
            #      so you know whether resetting the
            #      coverage is necessary?
            lowVol, highVol = slices[self.__numRealDims - 1]

468
469
470
471
472
473
474
            log.debug('Image {} data written - clearing known data '
                      'range on volumes {} - {} (write slice: {})'.format(
                          self.__name,
                          lowVol,
                          highVol,
                          slices))

475
            for vol in range(lowVol, highVol):
476
477
                self.__coverage[:, :, vol]    = np.nan
                self.__volRanges[     vol, :] = np.nan
478
479
480

        self.__expandCoverage(slices)

481
            
482
    def __getitem__(self, sliceobj):
483
484
485
486
        """Returns the image data for the given ``sliceobj``, and updates
        the known image data range if necessary.

        :arg sliceobj: Something which can slice the image data.
487
488
        """

489
490
        log.debug('Getting image data: {}'.format(sliceobj))

491
492
493
        sliceobj = nib.fileslice.canonical_slicers(
            sliceobj, self.__image.shape)

494
        # TODO Cache 3D images for large 4D volumes, 
495
        #      so you don't have to hit the disk?
496

497
498
        data = self.__getData(sliceobj)

499
        if not self.__covered:
500

501
            slices = sliceObjToSliceTuple(sliceobj, self.__image.shape)
502

503
            if not sliceCovered(slices, self.__coverage):
504
                self.__updateDataRangeOnRead(slices, data)
505
506

        return data
507
508


509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
    def __setitem__(self, sliceobj, values):
        """Writes the given ``values`` to the image at the given ``sliceobj``.

        
        :arg sliceobj: Something which can be used to slice the array.
        :arg values:   Data to write to the image.

        
        .. note:: Modifying image data will cause the entire image to be
                  loaded into memory. 
        """

        sliceobj = nib.fileslice.canonical_slicers(sliceobj,
                                                   self.__image.shape)
        slices   = sliceObjToSliceTuple(           sliceobj,
                                                   self.__image.shape)

        # The image data has to be in memory
        # for the data to be changed. If it's
        # already in memory, this call won't
        # have any effect.
        self.loadData()

        self.__image.get_data()[sliceobj] = values
        self.__updateDataRangeOnWrite(slices, values)


536
537
538
def sliceObjToSliceTuple(sliceobj, shape):
    """Turns an array slice object into a tuple of (low, high) index
    pairs, one pair for each dimension in the given shape
539
540
541
542
543

    :arg sliceobj: Something which can be used to slice an array of shape
                   ``shape``.

    :arg shape:    Shape of the array being sliced.
544
545
546
547
    """

    indices = []

548
549
    # The sliceobj could be a single sliceobj
    # or integer, instead of a tuple
550
551
552
    if not isinstance(sliceobj, collections.Sequence):
        sliceobj = [sliceobj]

553
    # Turn e.g. array[6] into array[6, :, :]
554
555
556
557
    if len(sliceobj) != len(shape):
        missing  = len(shape) - len(sliceobj)
        sliceobj = list(sliceobj) + [slice(None) for i in range(missing)]

558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
    for dim, s in enumerate(sliceobj):

        # each element in the slices tuple should 
        # be a slice object or an integer
        if isinstance(s, slice): i = [s.start, s.stop]
        else:                    i = [s,       s + 1]

        if i[0] is None: i[0] = 0
        if i[1] is None: i[1] = shape[dim]

        indices.append(tuple(i))

    return tuple(indices)


573
574
def sliceTupleToSliceObj(slices):
    """Turns a sequence of (low, high) index pairs into a tuple of array
575
    ``slice`` objects.
576
577

    :arg slices: A sequence of (low, high) index pairs.
578
579
580
581
582
583
584
585
586
587
    """

    sliceobj = []

    for lo, hi in slices:
        sliceobj.append(slice(lo, hi, 1))

    return tuple(sliceobj)


588
589
590
591
def adjustCoverage(oldCoverage, slices): 
    """Adjusts/expands the given ``oldCoverage`` so that it covers the
    given set of ``slices``.

592
    :arg oldCoverage: A ``numpy`` array of shape ``(2, n)`` containing
593
594
                      the (low, high) index pairs for ``n`` dimensions of
                      a single slice/volume in the image.
595
    
596
597
598
599
    :arg slices:      A sequence of (low, high) index pairs. If ``slices``
                      contains more dimensions than are specified in
                      ``oldCoverage``, the trailing dimensions are ignored.

600
    :return: A ``numpy`` array containing the adjusted/expanded coverage.
601
602
    """

603
    newCoverage = np.zeros(oldCoverage.shape, dtype=np.uint32)
604

605
    for dim in range(oldCoverage.shape[1]):
606

607
608
        low,      high      = slices[        dim]
        lowCover, highCover = oldCoverage[:, dim]
609

610
611
        if np.isnan(lowCover)  or low  < lowCover:  lowCover  = low
        if np.isnan(highCover) or high > highCover: highCover = high
612

613
        newCoverage[:, dim] = lowCover, highCover
614
615
616

    return newCoverage

617

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
OVERLAP_ALL = 0
"""Indicates that the slice is wholly contained within the coverage.  This is
a return code for the :func:`sliceOverlap` function.
"""


OVERLAP_SOME = 1
"""Indicates that the slice partially overlaps with the coverage. This is a
return code for the :func:`sliceOverlap` function.
"""


OVERLAP_NONE = 2
"""Indicates that the slice does not overlap with the coverage. This is a
return code for the :func:`sliceOverlap` function.
"""


def sliceOverlap(slices, coverage):
    """Determines whether the given ``slices`` overlap with the given
    ``coverage``.

    :arg slices:    A sequence of (low, high) index pairs, assumed to cover
                    all image dimensions.
    :arg coverage:  A ``numpy`` array of shape ``(2, nd, nv)`` (where ``nd``
                    is the number of dimensions being covered, and ``nv`` is
                    the number of volumes (or vectors/slices) in the image,
                    which contains the (low, high) index pairs describing
                    the current image coverage.

    :returns: One of the following codes:
              .. autosummary::

              OVERLAP_ALL
              OVERLAP_SOME
              OVERLAP_NONE
    """

    numDims         = coverage.shape[1]
    lowVol, highVol = slices[numDims]

    # Overlap state is calculated for each volume
    overlapStates = np.zeros(highVol - lowVol)

    for i, vol in enumerate(range(lowVol, highVol)):

        state = OVERLAP_ALL

        for dim in range(numDims):

            lowCover, highCover = coverage[:, dim, vol]
            lowSlice, highSlice = slices[     dim] 

            # No coverage
            if np.isnan(lowCover) or np.isnan(highCover):
                state = OVERLAP_NONE
                break

            # The slice is contained within the
            # coverage on this dimension - check
            # the other dimensions.
            if lowSlice >= lowCover and highSlice <= highCover:
                continue

            # The slice does not overlap at all
            # with the coverage on this dimension
            # (or at all). No overlap - no need
            # to check the other dimensions.
            if lowSlice >= highCover or highSlice <= lowCover:
                state = OVERLAP_NONE
                break

            # There is some overlap between the
            # slice and coverage on this dimension
            # - check the other dimensions.
            state = OVERLAP_SOME
            
        overlapStates[i] = state

    if   np.any(overlapStates == OVERLAP_SOME): return OVERLAP_SOME
    elif np.all(overlapStates == OVERLAP_NONE): return OVERLAP_NONE
    elif np.all(overlapStates == OVERLAP_ALL):  return OVERLAP_ALL


def sliceCovered(slices, coverage):
703
704
    """Returns ``True`` if the portion of the image data calculated by
    the given ``slices` has already been calculated, ``False`` otherwise.
705

706
707
708
709
710
711
712
    :arg slices:    A sequence of (low, high) index pairs, assumed to cover
                    all image dimensions.
    :arg coverage:  A ``numpy`` array of shape ``(2, nd, nv)`` (where ``nd``
                    is the number of dimensions being covered, and ``nv`` is
                    the number of volumes (or vectors/slices) in the image,
                    which contains the (low, high) index pairs describing
                    the current image coverage.
713
714
    """

715
716
    numDims         = coverage.shape[1]
    lowVol, highVol = slices[numDims]
717
718
719

    for vol in range(lowVol, highVol):

720
        for dim in range(numDims):
721

722
723
            lowCover, highCover = coverage[:, dim, vol]
            lowSlice, highSlice = slices[     dim] 
724

725
            if np.isnan(lowCover) or np.isnan(highCover):
726
727
728
729
730
731
732
733
                return False

            if lowSlice  < lowCover:  return False
            if highSlice > highCover: return False

    return True


734
def calcExpansion(slices, coverage):
735
736
737
738
739
740
741
742
    """Calculates a series of *expansion* slices, which can be used to expand
    the given ``coverage`` so that it includes the given ``slices``.

    :arg slices:   Slices that the coverage needs to be expanded to cover.
    :arg coverage: Current image coverage.

    :returns: A list of volume indices, and a corresponding list of
              expansions.
743
744
    """

745
746
747
    numDims         = coverage.shape[1]
    padDims         = len(slices) - numDims - 1
    lowVol, highVol = slices[numDims] 
748
749

    expansions = []
750
    volumes    = []
751

752
753
754
755
756
757
758
759
760
761
    # Finish off an expansion by
    # adding indices for the vector/
    # slice/volume dimension, and for
    # 'padding' dimensions of size 1.
    def finishExpansion(exp, vol):
        exp.append((vol, vol + 1))
        for i in range(padDims):
            exp.append((0, 1))
        return exp
    
762
    for vol in range(lowVol, highVol):
763

764
765
766
        # No coverage of this volume - 
        # we need the whole slice.
        if np.any(np.isnan(coverage[:, :, vol])):
767
768
            exp = [(s[0], s[1]) for s in slices[:numDims]]
            exp = finishExpansion(exp, vol)
769
            volumes   .append(vol)
770
            expansions.append(exp)
771
772
            continue

773
774
775
776
777
778
779
780
        # First we'll figure out the index
        # range for each dimension that
        # needs to be added to the coverage.
        # We build a list of required ranges,
        # where each entry is a tuple
        # containing:
        #   (dimension, lowIndex, highIndex)
        reqRanges = []
781

782
        for dim in range(numDims):
783

784
            lowCover, highCover = coverage[:, dim, vol]
785
            lowSlice, highSlice = slices[     dim]
786

787
788
789
            # The slice covers a region
            # below the current coverage
            if lowCover - lowSlice > 0:
790
                reqRanges.append((dim, int(lowSlice), int(lowCover)))
791
792
793
794
                
            # The slice covers a region
            # above the current coverage
            if highCover - highSlice < 0:
795
                reqRanges.append((dim, int(highCover), int(highSlice)))
796
797

        # Now we generate an expansion for
798
799
        # each of those ranges.
        volExpansions = []
800
801
        for dimx, xlo, xhi in reqRanges:

802
            expansion = [[np.nan, np.nan] for d in range(numDims)]
803
804
805
806
807
808
809
810

            # The expansion for each
            # dimension will span the range
            # for that dimension...
            expansion[dimx][0] = xlo
            expansion[dimx][1] = xhi
                
            # And will span the union of
811
812
            # the coverage, and calculated
            # range for every other dimension.
813
814
815
816
817
            for dimy, ylo, yhi in reqRanges:
                if dimy == dimx:
                    continue

                yLowCover, yHighCover = coverage[:, dimy, vol]
818
                expLow,    expHigh    = expansion[  dimy]
819

820
821
822
823
824
825
826
827
                if np.isnan(expLow):  expLow  = yLowCover
                if np.isnan(expHigh): expHigh = yHighCover

                expLow  = min((ylo, yLowCover,  expLow))
                expHigh = max((yhi, yHighCover, expHigh))

                expansion[dimy][0] = expLow
                expansion[dimy][1] = expHigh
828

829
830
831
832
833
834
835
836
837
838
839
840

            # If no range exists for any of the
            # other dimensions, the range for
            # all expansions will be the current
            # coverage
            for dimy in range(numDims):
                if dimy == dimx:
                    continue

                if np.any(np.isnan(expansion[dimy])):
                    expansion[dimy] = coverage[:, dimy, vol]

841
842
            # Finish off this expansion
            expansion = finishExpansion(expansion, vol)
843

844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
            volumes.      append(vol)
            volExpansions.append(expansion)

        # We do a final run through all pairs
        # of expansions, and adjust their
        # range if they overlap with each other.
        for exp1, exp2 in it.product(volExpansions, volExpansions):

            # Check each dimension
            for dimx in range(numDims):

                xlo1, xhi1 = exp1[dimx]
                xlo2, xhi2 = exp2[dimx]

                # These expansions do not
                # overlap with each other
                # on this dimension (or at
                # all). No need to check
                # the other dimensions.
                if xhi1 <= xlo2: break
                if xlo1 >= xhi2: break

                # These expansions overlap on
                # this dimension - check to see
                # if exp1 is wholly contained
                # within exp2 in all other
                # dimensions.
                adjustable = True

                for dimy in range(numDims):

                    if dimy == dimx:
                        continue

                    ylo1, yhi1 = exp1[dimy]
                    ylo2, yhi2 = exp2[dimy]

                    # Exp1 is not contained within
                    # exp2 on another dimension -
                    # we can't reduce the overlap.
                    if ylo1 < ylo2 or yhi1 > yhi2:
                        adjustable = False
                        break

                # The x dimension range of exp1
                # can be reduced, as it is covered
                # by exp2.
                if adjustable:
                    if   xlo1 <  xlo2 and xhi1 <= xhi2 and xhi1 > xlo2:
                        xhi1 = xlo2

                    elif xlo1 >= xlo2 and xhi1 >  xhi2 and xlo1 < xhi2:
                        xlo1 = xhi2

                    exp1[dimx] = xlo1, xhi1

        expansions.extend(volExpansions)
901
902

    return volumes, expansions
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962


def collapseExpansions(expansions, numDims):
    """Scans through the given list of expansions (each assumed to pertain 
    to a single 3D image), and combines any which cover the same
    image area, and cover adjacent volumes.

    :args expansions: A list of expansion slices - see :func:`calcExpansions`.

    :args numDims:    Number of dimensions covered by each expansion,
                      not including the volume dimension (i.e. 3 for a 4D
                      image).

    :returns: A list of expansions, with equivalent expansions that cover
              adjacent images collapsed down.

    .. note:: For one expansion ``exp`` in the ``expansions`` list, this
              function assumes that the range at ``exp[numDims]`` contains
              the image to which ``exp`` pertains (i.e.
              ``exp[numDims] == (vol, vol + 1)``).
    """
    if len(expansions) == 0:
        return []

    commonExpansions = collections.OrderedDict()
    expansions       = sorted(expansions)

    for exp in expansions:

        vol        = exp[numDims][0]
        exp        = tuple(exp[:numDims])
        commonExps = commonExpansions.get(exp, None)
        
        if commonExps is None:
            commonExps            = []
            commonExpansions[exp] = commonExps

        for i, (vlo, vhi) in enumerate(commonExps):

            if vol >= vlo and vol < vhi:
                break
            
            elif vol == vlo - 1:
                commonExps[i] = vol, vhi
                break
            elif vol == vhi:
                commonExps[i] = vlo, vol + 1
                break
            
        else:
            commonExps.append((vol, vol + 1))
            
    collapsed = []

    for exp, volRanges in commonExpansions.items():
        for vlo, vhi in volRanges:
            newExp = list(exp) + [(vlo, vhi)]
            collapsed.append(newExp)

    return collapsed