def test_simpleBlockExtraction(self): for params in TestBlockExtraction.PARAMS: strategy = SimpleBlockingStrategy([1] * len(params.aryshape)) # dummy splits; not used here n = reduce(lambda x, y: x * y, params.aryshape) ary = arange(n, dtype="int16").reshape(params.aryshape) key, val = strategy.extractBlockFromImage(ary, params.blockslices, params.timepoint, params.ntimepoints) expectedSlices = [slice(params.timepoint, params.timepoint + 1, 1)] + list(params.blockslices) expectedAry = expand_dims(ary[params.blockslices], axis=0) assert_equals(params.timepoint, key.temporalKey) assert_equals(params.ntimepoints, key.origShape[0]) assert_equals(tuple(params.aryshape), tuple(key.origShape[1:])) assert_equals(tuple(expectedSlices), tuple(key.imgSlices)) assert_true(array_equal(expectedAry, val))
def test_simpleBlockExtraction(self): for params in TestBlockExtraction.PARAMS: strategy = SimpleBlockingStrategy([1]*len(params.aryshape)) # dummy splits; not used here n = reduce(lambda x, y: x*y, params.aryshape) ary = arange(n, dtype='int16').reshape(params.aryshape) key, val = strategy.extractBlockFromImage(ary, params.blockslices, params.timepoint, params.ntimepoints) expectedSlices = [slice(params.timepoint, params.timepoint+1, 1)] + list(params.blockslices) expectedAry = expand_dims(ary[params.blockslices], axis=0) assert_equals(params.timepoint, key.temporalKey) assert_equals(params.ntimepoints, key.origShape[0]) assert_equals(tuple(params.aryshape), tuple(key.origShape[1:])) assert_equals(tuple(expectedSlices), tuple(key.imgSlices)) assert_true(array_equal(expectedAry, val))
def test_fromStackToSeriesWithPack(self): ary = arange(8, dtype=dtypeFunc('int16')).reshape((2, 4)) filename = os.path.join(self.outputdir, "test.stack") ary.tofile(filename) image = ImagesLoader(self.sc).fromStack(filename, dims=(4, 2)) strategy = SimpleBlockingStrategy.generateFromBlockSize(image, "150M") series = image.toBlocks(strategy).toSeries() seriesVals = series.collect() seriesAry = series.pack() # check ordering of keys assert_equals((0, 0), seriesVals[0][0]) # first key assert_equals((1, 0), seriesVals[1][0]) # second key assert_equals((2, 0), seriesVals[2][0]) assert_equals((3, 0), seriesVals[3][0]) assert_equals((0, 1), seriesVals[4][0]) assert_equals((1, 1), seriesVals[5][0]) assert_equals((2, 1), seriesVals[6][0]) assert_equals((3, 1), seriesVals[7][0]) # check dimensions tuple is reversed from numpy shape assert_equals(ary.shape[::-1], series.dims.count) # check that values are in original order collectedVals = array([kv[1] for kv in seriesVals], dtype=dtypeFunc('int16')).ravel() assert_true(array_equal(ary.ravel(), collectedVals)) # check that packing returns transpose of original array assert_true(array_equal(ary.T, seriesAry))
def test_fromStackToSeriesWithPack(self): ary = arange(8, dtype=dtypeFunc("int16")).reshape((2, 4)) filename = os.path.join(self.outputdir, "test.stack") ary.tofile(filename) image = ImagesLoader(self.sc).fromStack(filename, dims=(4, 2)) strategy = SimpleBlockingStrategy.generateFromBlockSize(image, "150M") series = image.toBlocks(strategy).toSeries() seriesVals = series.collect() seriesAry = series.pack() # check ordering of keys assert_equals((0, 0), seriesVals[0][0]) # first key assert_equals((1, 0), seriesVals[1][0]) # second key assert_equals((2, 0), seriesVals[2][0]) assert_equals((3, 0), seriesVals[3][0]) assert_equals((0, 1), seriesVals[4][0]) assert_equals((1, 1), seriesVals[5][0]) assert_equals((2, 1), seriesVals[6][0]) assert_equals((3, 1), seriesVals[7][0]) # check dimensions tuple is reversed from numpy shape assert_equals(ary.shape[::-1], series.dims.count) # check that values are in original order collectedVals = array([kv[1] for kv in seriesVals], dtype=dtypeFunc("int16")).ravel() assert_true(array_equal(ary.ravel(), collectedVals)) # check that packing returns transpose of original array assert_true(array_equal(ary.T, seriesAry))
def test_roundtripConvertToSeries(self): imagepath = findSourceTreeDir("utils/data/fish/tif-stack") images = ImagesLoader(self.sc).fromTif(imagepath) strategy = SimpleBlockingStrategy.generateFromBlockSize(images, blockSize=76 * 20) self._run_tst_roundtripConvertToSeries(images, strategy)
def _run_tst_splitCalc(blockSize, image, expectedSplits, expectedSize, testIdx=-1): strategy = SimpleBlockingStrategy.generateFromBlockSize(image, blockSize) splits = strategy.splitsPerDim avgSize = strategy.calcAverageBlockSize() assert_equals(tuple(expectedSplits), tuple(splits), msg="Failure in test %i, expected splits %s != actual splits %s" % (testIdx, tuple(expectedSplits), tuple(splits))) assert_true(isclose(expectedSize, avgSize, rtol=0.001), msg="Failure in test %i, expected avg size %g not close to actual size %g" % (testIdx, expectedSize, avgSize))
def test_splitsAndPix(self): def genSlicesForPix(pix, size): slices = [] st = 0 while st < size: en = min(st + pix, size) slices.append(slice(st, en, 1)) st = en return slices def genSlicesForSplits(splits, size): slices = [] blocksize = size / splits # integer division blockrem = size % splits st = 0 while st < size: en = st + blocksize if blockrem > 0: en += 1 blockrem -= 1 en = min(en, size) slices.append(slice(st, en, 1)) st = en return slices Params = namedtuple( "Params", "unitsPerDim units image padding expPix expSplits expSlices0") PARAMS = \ [Params((5, 5, 1), "pix", MockImage((15, 15, 3), 2, "uint8"), 0, (5, 5, 1), None, genSlicesForPix(5, 15)), Params((5, 5, 2), "pix", MockImage((7, 7, 3), 2, "uint8"), 0, (5, 5, 2), None, genSlicesForPix(5, 7)), Params((2, 2, 2), "s", MockImage((7, 7, 3), 2, "uint8"), 0, None, (2, 2, 2), genSlicesForSplits(2, 7)), Params((5, 5, 1), "pix", MockImage((15, 15, 3), 2, "uint8"), 2, (5, 5, 1), None, genSlicesForPix(5, 15)), Params((5, 5, 2), "pix", MockImage((7, 7, 3), 2, "uint8"), 2, (5, 5, 2), None, genSlicesForPix(5, 7)), Params((2, 2, 2), "s", MockImage((7, 7, 3), 2, "uint8"), 2, None, (2, 2, 2), genSlicesForSplits(2, 7))] for params in PARAMS: if params.padding: strat = PaddedBlockingStrategy(params.unitsPerDim, units=params.units, padding=params.padding) else: strat = SimpleBlockingStrategy(params.unitsPerDim, params.units) strat.setSource(params.image) if params.expPix: assert_equals(tuple(params.expPix), tuple(strat._pixPerDim)) else: assert_true(strat._pixPerDim is None) if params.expSplits: assert_equals(tuple(params.expSplits), tuple(strat._splitsPerDim)) else: assert_true(strat._splitsPerDim is None) assert_equals(params.expSlices0, strat._slices[0])
def test_roundtripThroughBlocks(self): strategy = SimpleBlockingStrategy((2, 2, 2), units="s") self._run_tst_roundtripThroughBlocks(strategy)
def test_toSeriesWithSplitsAndPack(self): strategy = SimpleBlockingStrategy((1, 2), units="s") self._run_tst_toSeriesWithSplitsAndPack(strategy)
def test_roundtripConvertToSeries(self): imagepath = findSourceTreeDir("utils/data/fish/images") images = ImagesLoader(self.sc).fromTif(imagepath) strategy = SimpleBlockingStrategy.generateFromBlockSize(images, blockSize=76 * 20) self._run_tst_roundtripConvertToSeries(images, strategy)