Skip to content

Commit

Permalink
ADD: NDVar.var()
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbrodbeck committed Jan 31, 2017
1 parent 6f0674d commit 7b00600
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
31 changes: 31 additions & 0 deletions eelbrain/_data_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from collections import Iterator, OrderedDict, Sequence
from copy import deepcopy
from fnmatch import fnmatchcase
from functools import partial
import itertools
from itertools import chain, izip
from keyword import iskeyword
Expand Down Expand Up @@ -4633,6 +4634,36 @@ def threshold(self, v, tail=1, name=None):
return NDVar(np.where(idx, self.x, 0), self.dims, info,
name or self.name)

def var(self, dims=(), ddof=0, **regions):
"""Compute the variance over given dimensions
Parameters
----------
dims : str | tuple of str | boolean NDVar
Dimensions over which to operate. A str is used to specify a single
dimension, a tuple of str to specify several dimensions, None to
compute the sum over all dimensions.
An boolean NDVar with the same dimensions as the data can be used
to compute the variance in specific elements (if the data has a case
dimension, the variance is computed for each case).
*regions*
Regions over which to aggregate. For example, to get the variance
between time=0.1 and time=0.2, use ``ndvar.var(time=(0.1, 0.2))``.
ddof : int
Degrees of freedom (default 0; see :func:`numpy.var`).
name : str
Name of the output NDVar (default is the current name).
Returns
-------
var : NDVar | Var | float
The variance over specified dimensions. Returns a Var if only the
case dimension remains, and a float if the function collapses over
all data.
"""
return self._aggregate_over_dims(dims, regions,
partial(np.var, ddof=ddof))


def extrema(x, axis=0):
"Extract the extreme values in x"
Expand Down
10 changes: 10 additions & 0 deletions eelbrain/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,16 @@ def test_ndvar_summary_methods():
assert_array_equal(x.min(idxsub), xsub.min(idxsub))
assert_array_equal(x.min(idx1d), x.x[:, idx1d.x].min(1))

eq_(x.var(), x.x.var())
eq_(x.var(ddof=1), x.x.var(ddof=1))
assert_array_equal(x.var(dim), x.x.var(axis))
assert_array_equal(x.var(dims, ddof=1), x.x.var(axes, ddof=1))
assert_array_equal(x.var(idx0), [x_[idx0.x].var() for x_ in x.x])
assert_array_equal(x.var(idx), [x_[i].var() for x_, i in izip(x.x, idx.x)])
assert_array_equal(x0.var(idx0), x0.x[idx0.x].var())
assert_array_equal(x.var(idxsub), xsub.var(idxsub))
assert_array_equal(x.var(idx1d), x.x[:, idx1d.x].var(1))

eq_(x.std(), x.x.std())
assert_array_equal(x.std(dim), x.x.std(axis))
assert_array_equal(x.std(dims), x.x.std(axes))
Expand Down

0 comments on commit 7b00600

Please sign in to comment.