Switch to unified view

a b/CLI/MusculoskeletalAnalysisCLITools/crop.py
1
import numpy as np
2
3
def crop(*args):
4
    """Crops a series of ndarrays to the range of nonzero values in the first array for each dimension."""
5
    template = args[0]
6
    # Get the coodinates of all nonzero points in the first input as a tuple of arrays
7
    coords = np.nonzero(template)
8
    sliceC = ()
9
    # For each dimension, get a slice from the smallest to largest values
10
    for i, c in enumerate(coords):
11
        low = min(c)
12
        high = max(c)+1
13
        # Leave 1 space between shape and new edge unless the shape was already on the edge
14
        if low > 0:
15
            low==low-1
16
        if high < np.shape(template)[i]:
17
            high = high+1
18
        sliceC += slice(low , high),
19
    croppedArgs = ()
20
    # For each input, apply the slice and return the results
21
    for a in args:
22
        croppedArgs += a[sliceC],
23
    return croppedArgs