def extractBlockFromImage(self, imgAry, blockSlices, timepoint, numTimepoints): padSlices = [] actualPadding = [] for coreSlice, pad, l in zip(blockSlices, self.padding, imgAry.shape): # normalize 'None' values to appropriate positions normStart, normStop, normStep = getStartStopStep(coreSlice, l) start = max(0, normStart - pad) stop = min(l, normStop + pad) startPadSize = normStart - start stopPadSize = stop - normStop padSlices.append(slice(start, stop, normStep)) actualPadding.append((startPadSize, stopPadSize)) # calculate "core" slices into values array based on actual size of padding vals = imgAry[padSlices] coreValSlices = [slice(0, 1, 1)] # start with slice for time for actualPad, l in zip(actualPadding, vals.shape): actualStartPad, actualStopPad = actualPad coreValSlices.append(slice(actualStartPad, l - actualStopPad, 1)) # add additional "time" dimension onto front of val val = expand_dims(imgAry[padSlices], axis=0) origShape = [numTimepoints] + list(imgAry.shape) imgSlices = [slice(timepoint, timepoint + 1, 1)] + list(blockSlices) padSlices = [slice(timepoint, timepoint + 1, 1)] + padSlices return PaddedBlockGroupingKey(origShape, padSlices, imgSlices, tuple(val.shape), coreValSlices), val
def extractBlockFromImage(self, imgAry, blockSlices, timepoint, numTimepoints): padSlices = [] actualPadding = [] for coreSlice, pad, l in zip(blockSlices, self.padding, imgAry.shape): # normalize 'None' values to appropriate positions normStart, normStop, normStep = getStartStopStep(coreSlice, l) start = max(0, normStart-pad) stop = min(l, normStop+pad) startPadSize = normStart - start stopPadSize = stop - normStop padSlices.append(slice(start, stop, normStep)) actualPadding.append((startPadSize, stopPadSize)) # calculate "core" slices into values array based on actual size of padding vals = imgAry[padSlices] coreValSlices = [slice(0, 1, 1)] # start with slice for time for actualPad, l in zip(actualPadding, vals.shape): actualStartPad, actualStopPad = actualPad coreValSlices.append(slice(actualStartPad, l-actualStopPad, 1)) # add additional "time" dimension onto front of val val = expand_dims(imgAry[padSlices], axis=0) origShape = [numTimepoints] + list(imgAry.shape) imgSlices = [slice(timepoint, timepoint+1, 1)] + list(blockSlices) padSlices = [slice(timepoint, timepoint+1, 1)] + padSlices return PaddedBlockGroupingKey(origShape, padSlices, imgSlices, tuple(val.shape), coreValSlices, self.pixelsPerDim), val
def combiningFunction(self, blockNumAndCollectedSeries): blockNum, collectedSeries = blockNumAndCollectedSeries blockSlices = self._slicesProduct[blockNum] imgShape = [self.nimages] + list(self.dims) vecShape = [ -1 ] + [1] * len(self.dims) # needed to coerce vector broadcast to work key = BlockGroupingKey( tuple(imgShape), tuple([slice(0, self.nimages, 1)] + list(blockSlices))) aryShape = [self.nimages] arySpatialOffsets = [] for blockSlice, refSize in zip(blockSlices, self.dims): start, stop, _ = getStartStopStep(blockSlice, refSize) aryShape.append(stop - start) arySpatialOffsets.append(start) ary = zeros(aryShape, dtype=self.dtype) for seriesKey, seriesAry in collectedSeries: arySlices = [slice(0, self.nimages)] + \ [slice(i-offset, i-offset+1) for (i, offset) in zip(seriesKey, arySpatialOffsets)] # this is an inefficient assignment, # since the first dimension of ary is not contiguous in memory: ary[arySlices] = seriesAry.reshape(vecShape) return key, ary
def combiningFunction(self, blockNumAndCollectedSeries): blockNum, collectedSeries = blockNumAndCollectedSeries blockSlices = self._slicesProduct[blockNum] imgShape = [self.nimages] + list(self.dims) vecShape = [-1] + [1] * len(self.dims) # needed to coerce vector broadcast to work key = BlockGroupingKey(tuple(imgShape), tuple([slice(0, self.nimages, 1)] + list(blockSlices))) aryShape = [self.nimages] arySpatialOffsets = [] for blockSlice, refSize in zip(blockSlices, self.dims): start, stop, _ = getStartStopStep(blockSlice, refSize) aryShape.append(stop - start) arySpatialOffsets.append(start) ary = zeros(aryShape, dtype=self.dtype) for seriesKey, seriesAry in collectedSeries: arySlices = [slice(0, self.nimages)] + \ [slice(i-offset, i-offset+1) for (i, offset) in zip(seriesKey, arySpatialOffsets)] # this is an inefficient assignment, # since the first dimension of ary is not contiguous in memory: ary[arySlices] = seriesAry.reshape(vecShape) return key, ary