Python scipy.ndimage.mean() Examples

The following are 23 code examples of scipy.ndimage.mean(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module scipy.ndimage , or try the search function .
Example #1
Source File: try_catdata.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def labelmeanfilter_nd(y, x):
   # requires integer labels
   # from mailing list scipy-user 2009-02-11
   # adjusted for 2d x with column variables

   labelsunique = np.arange(np.max(y)+1)
   labmeansdata = []
   labmeans = []

   for xx in x.T:
      labelmeans = np.array(ndimage.mean(xx, labels=y, index=labelsunique))
      labmeansdata.append(labelmeans[y])
      labmeans.append(labelmeans)
   # group count:
   labelcount = np.array(ndimage.histogram(y, labelsunique[0], labelsunique[-1]+1,
                        1, labels=y, index=labelsunique))

   # returns array of lable/group counts and of label/group means
   #         and label/group means for each original observation
   return labelcount, np.array(labmeans), np.array(labmeansdata).T 
Example #2
Source File: try_catdata.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def labelmeanfilter_nd(y, x):
   # requires integer labels
   # from mailing list scipy-user 2009-02-11
   # adjusted for 2d x with column variables

   labelsunique = np.arange(np.max(y)+1)
   labmeansdata = []
   labmeans = []

   for xx in x.T:
      labelmeans = np.array(ndimage.mean(xx, labels=y, index=labelsunique))
      labmeansdata.append(labelmeans[y])
      labmeans.append(labelmeans)
   # group count:
   labelcount = np.array(ndimage.histogram(y, labelsunique[0], labelsunique[-1]+1,
                        1, labels=y, index=labelsunique))

   # returns array of lable/group counts and of label/group means
   #         and label/group means for each original observation
   return labelcount, np.array(labmeans), np.array(labmeansdata).T 
Example #3
Source File: test_measurements.py    From Computable with MIT License 6 votes vote down vote up
def test_stat_funcs_2d():
    """Apply the stat funcs to a 2-d array."""
    a = np.array([[5,6,0,0,0], [8,9,0,0,0], [0,0,0,3,5]])
    lbl = np.array([[1,1,0,0,0], [1,1,0,0,0], [0,0,0,2,2]])

    mean = ndimage.mean(a, labels=lbl, index=[1, 2])
    assert_array_equal(mean, [7.0, 4.0])

    var = ndimage.variance(a, labels=lbl, index=[1, 2])
    assert_array_equal(var, [2.5, 1.0])

    std = ndimage.standard_deviation(a, labels=lbl, index=[1, 2])
    assert_array_almost_equal(std, np.sqrt([2.5, 1.0]))

    med = ndimage.median(a, labels=lbl, index=[1, 2])
    assert_array_equal(med, [7.0, 4.0])

    min = ndimage.minimum(a, labels=lbl, index=[1, 2])
    assert_array_equal(min, [5, 3])

    max = ndimage.maximum(a, labels=lbl, index=[1, 2])
    assert_array_equal(max, [9, 5]) 
Example #4
Source File: scene_processing.py    From kite with GNU General Public License v3.0 6 votes vote down vote up
def apply(self):
        sc = self.scene
        org = self.original
        factor = self.factor

        sx, sy = sc.displacement.shape
        gx, gy = num.ogrid[0:sx, 0:sy]
        regions = sy/factor * (gx/factor) + gy/factor
        indices = num.arange(regions.max() + 1)

        def block_downsample(arr):
            res = ndimage.mean(
                arr,
                labels=regions,
                index=indices)
            res.shape = (sx/factor, sy/factor)
            return res

        sc.displacement = block_downsample(sc.displacement)
        sc.theta = block_downsample(sc.theta)
        sc.phi = block_downsample(sc.phi)
        sc.frame.dLat = org['frame.dLat'] * self.factor
        sc.frame.dLon = org['frame.dLat'] * self.factor 
Example #5
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_stat_funcs_2d():
    a = np.array([[5,6,0,0,0], [8,9,0,0,0], [0,0,0,3,5]])
    lbl = np.array([[1,1,0,0,0], [1,1,0,0,0], [0,0,0,2,2]])

    mean = ndimage.mean(a, labels=lbl, index=[1, 2])
    assert_array_equal(mean, [7.0, 4.0])

    var = ndimage.variance(a, labels=lbl, index=[1, 2])
    assert_array_equal(var, [2.5, 1.0])

    std = ndimage.standard_deviation(a, labels=lbl, index=[1, 2])
    assert_array_almost_equal(std, np.sqrt([2.5, 1.0]))

    med = ndimage.median(a, labels=lbl, index=[1, 2])
    assert_array_equal(med, [7.0, 4.0])

    min = ndimage.minimum(a, labels=lbl, index=[1, 2])
    assert_array_equal(min, [5, 3])

    max = ndimage.maximum(a, labels=lbl, index=[1, 2])
    assert_array_equal(max, [9, 5]) 
Example #6
Source File: suppixpool_orig.py    From superpixPool with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs): 
        img, labels = inputs
        outputs = []

        for batch in xrange(img.shape[0]):
            batchOut = []
            for classIx in xrange(img.shape[1]):
                # outputs.append(ndimage.maximum(img[classIx, :,:,:], labels=labels, index= np.unique(labels)))

                batchOut.append(ndimage.mean(img[batch, classIx, :,:,:], labels=labels[batch,:,:,:], index=range(labels[batch,:,:,:].max()+1)).astype(img.dtype))
            outputs.append(batchOut)
        return np.array(outputs), 
Example #7
Source File: try_catdata.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def labelmeanfilter(y, x):
   # requires integer labels
   # from mailing list scipy-user 2009-02-11
   labelsunique = np.arange(np.max(y)+1)
   labelmeans = np.array(ndimage.mean(x, labels=y, index=labelsunique))
   # returns label means for each original observation
   return labelmeans[y]

#groupcount: i.e. number of observation by group/label
#np.array(ndimage.histogram(yrvs[:,0],0,10,1,labels=yrvs[:,0],index=np.unique(yrvs[:,0]))) 
Example #8
Source File: supvoxpool.py    From superpixPool with MIT License 5 votes vote down vote up
def forward_cpu(self, inputs): 
        img, labels = inputs
        outputs = []

        for batch in xrange(img.shape[0]):
            batchOut = []
            for classIx in xrange(img.shape[1]):
                # outputs.append(ndimage.maximum(img[classIx, :,:,:], labels=labels, index= np.unique(labels)))

                batchOut.append(ndimage.mean(img[batch, classIx, :,:,:], labels=labels[batch,:,:,:], index=range(labels[batch,:,:,:].max()+1)).astype(img.dtype))
            outputs.append(batchOut)
        return np.array(outputs), 
Example #9
Source File: try_catdata.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def groupsstats_dummy(y, x, nonseq=0):
    if x.ndim == 1:
        # use groupsstats_1d
        x = x[:,np.newaxis]
    dummy = cat2dummy(y, nonseq=nonseq)
    countgr = dummy.sum(0, dtype=float)
    meangr = np.dot(x.T,dummy)/countgr
    meandata = np.dot(dummy,meangr.T) # category/group means as array in shape of x
    xdevmeangr = x - meandata  # deviation from category/group mean
    vargr = np.dot((xdevmeangr * xdevmeangr).T, dummy) / countgr
    return meangr, vargr, xdevmeangr, countgr 
Example #10
Source File: try_catdata.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def groupsstats_1d(y, x, labelsunique):
    '''use ndimage to get fast mean and variance'''
    labelmeans = np.array(ndimage.mean(x, labels=y, index=labelsunique))
    labelvars = np.array(ndimage.var(x, labels=y, index=labelsunique))
    return labelmeans, labelvars 
Example #11
Source File: try_catdata.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def labelmeanfilter_str(ys, x):
    # works also for string labels in ys, but requires 1D
    # from mailing list scipy-user 2009-02-11
    unil, unilinv = np.unique(ys, return_index=False, return_inverse=True)
    labelmeans = np.array(ndimage.mean(x, labels=unilinv, index=np.arange(np.max(unil)+1)))
    arr3 = labelmeans[unilinv]
    return arr3 
Example #12
Source File: try_catdata.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def labelmeanfilter(y, x):
   # requires integer labels
   # from mailing list scipy-user 2009-02-11
   labelsunique = np.arange(np.max(y)+1)
   labelmeans = np.array(ndimage.mean(x, labels=y, index=labelsunique))
   # returns label means for each original observation
   return labelmeans[y]

#groupcount: i.e. number of observation by group/label
#np.array(ndimage.histogram(yrvs[:,0],0,10,1,labels=yrvs[:,0],index=np.unique(yrvs[:,0]))) 
Example #13
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_mean04():
    labels = np.array([[1, 2], [2, 4]], np.int8)
    olderr = np.seterr(all='ignore')
    try:
        for type in types:
            input = np.array([[1, 2], [3, 4]], type)
            output = ndimage.mean(input, labels=labels,
                                            index=[4, 8, 2])
            assert_array_almost_equal(output[[0,2]], [4.0, 2.5])
            assert_(np.isnan(output[1]))
    finally:
        np.seterr(**olderr) 
Example #14
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_mean03():
    labels = np.array([1, 2])
    for type in types:
        input = np.array([[1, 2], [3, 4]], type)
        output = ndimage.mean(input, labels=labels,
                                        index=2)
        assert_almost_equal(output, 3.0) 
Example #15
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_mean02():
    labels = np.array([1, 0], bool)
    input = np.array([[1, 2], [3, 4]], bool)
    output = ndimage.mean(input, labels=labels)
    assert_almost_equal(output, 1.0) 
Example #16
Source File: test_measurements.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_mean01():
    labels = np.array([1, 0], bool)
    for type in types:
        input = np.array([[1, 2], [3, 4]], type)
        output = ndimage.mean(input, labels=labels)
        assert_almost_equal(output, 2.0) 
Example #17
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_mean04():
    "mean 4"
    labels = np.array([[1, 2], [2, 4]], np.int8)
    olderr = np.seterr(all='ignore')
    try:
        for type in types:
            input = np.array([[1, 2], [3, 4]], type)
            output = ndimage.mean(input, labels=labels,
                                            index=[4, 8, 2])
            assert_array_almost_equal(output[[0,2]], [4.0, 2.5])
            assert_(np.isnan(output[1]))
    finally:
        np.seterr(**olderr) 
Example #18
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_mean03():
    "mean 3"
    labels = np.array([1, 2])
    for type in types:
        input = np.array([[1, 2], [3, 4]], type)
        output = ndimage.mean(input, labels=labels,
                                        index=2)
        assert_almost_equal(output, 3.0) 
Example #19
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_mean02():
    "mean 2"
    labels = np.array([1, 0], bool)
    input = np.array([[1, 2], [3, 4]], bool)
    output = ndimage.mean(input, labels=labels)
    assert_almost_equal(output, 1.0) 
Example #20
Source File: test_measurements.py    From Computable with MIT License 5 votes vote down vote up
def test_mean01():
    "mean 1"
    labels = np.array([1, 0], bool)
    for type in types:
        input = np.array([[1, 2], [3, 4]], type)
        output = ndimage.mean(input, labels=labels)
        assert_almost_equal(output, 2.0) 
Example #21
Source File: try_catdata.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def groupsstats_dummy(y, x, nonseq=0):
    if x.ndim == 1:
        # use groupsstats_1d
        x = x[:,np.newaxis]
    dummy = cat2dummy(y, nonseq=nonseq)
    countgr = dummy.sum(0, dtype=float)
    meangr = np.dot(x.T,dummy)/countgr
    meandata = np.dot(dummy,meangr.T) # category/group means as array in shape of x
    xdevmeangr = x - meandata  # deviation from category/group mean
    vargr = np.dot((xdevmeangr * xdevmeangr).T, dummy) / countgr
    return meangr, vargr, xdevmeangr, countgr 
Example #22
Source File: try_catdata.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def groupsstats_1d(y, x, labelsunique):
    '''use ndimage to get fast mean and variance'''
    labelmeans = np.array(ndimage.mean(x, labels=y, index=labelsunique))
    labelvars = np.array(ndimage.var(x, labels=y, index=labelsunique))
    return labelmeans, labelvars 
Example #23
Source File: try_catdata.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def labelmeanfilter_str(ys, x):
    # works also for string labels in ys, but requires 1D
    # from mailing list scipy-user 2009-02-11
    unil, unilinv = np.unique(ys, return_index=False, return_inverse=True)
    labelmeans = np.array(ndimage.mean(x, labels=unilinv, index=np.arange(np.max(unil)+1)))
    arr3 = labelmeans[unilinv]
    return arr3