diff --git a/field.py b/field.py index 79984f373c6dc50a2a7613a1fe27aa69eb3b9e48..6a00e61f5c226fa96574af7b4c0e4e69c444b881 100644 --- a/field.py +++ b/field.py @@ -61,12 +61,8 @@ class Field(object): the outer loop loops over dimension 2, the second outer over dimension 1, and the inner loop over dimension 0. Also allowed: the strings 'numpy' (0,1,..d) or 'reverseNumpy' (d, ..., 1, 0) """ - if isinstance(layout, str) and (layout == 'numpy' or layout.lower() == 'c'): - layout = tuple(range(spatialDimensions)) - elif isinstance(layout, str) and (layout == 'reverseNumpy' or layout.lower() == 'f'): - layout = tuple(reversed(range(spatialDimensions))) - if len(layout) != spatialDimensions: - raise ValueError("Layout") + if isinstance(layout, str): + layout = layoutStringToTuple(layout, dim=spatialDimensions) shapeSymbol = IndexedBase(TypedSymbol(Field.SHAPE_PREFIX + fieldName, Field.SHAPE_DTYPE), shape=(1,)) strideSymbol = IndexedBase(TypedSymbol(Field.STRIDE_PREFIX + fieldName, Field.STRIDE_DTYPE), shape=(1,)) totalDimensions = spatialDimensions + indexDimensions @@ -409,6 +405,22 @@ def createNumpyArrayWithLayout(shape, layout): return res +def layoutStringToTuple(layoutStr, dim): + if layoutStr in ('fzyx', 'zyxf') and dim != 4: + if dim == 3: + return tuple(reversed(range(dim))) + else: + raise ValueError("Layout descriptor " + layoutStr + " only valid for dimension 4, not %d" % (dim,)) + + if layoutStr == "fzyx" or layoutStr == 'f' or layoutStr == 'reverseNumpy': + return tuple(reversed(range(dim))) + elif layoutStr == 'c' or layoutStr == 'numpy': + return tuple(range(dim)) + elif layoutStr == 'zyxf': + return tuple(reversed(range(dim-1))) + (dim,) + raise ValueError("Unknown layout descriptor " + layoutStr) + + def normalizeLayout(layout): """Takes a layout tuple and subtracts the minimum from all entries""" minEntry = min(layout) diff --git a/slicing.py b/slicing.py index 15060b69d0781360d644a220f10748be0ccb3a79..6af0ab6859e71f3e3eb9a3a7542504c413d64a6d 100644 --- a/slicing.py +++ b/slicing.py @@ -19,6 +19,8 @@ def normalizeSlice(slices, sizes): for s, size in zip(slices, sizes): if type(s) is int: + if s < 0: + s = size + s result.append(s) continue if type(s) is float: