Example #1
0
def mark_by_threshold(dataset, DV='MEG', threshold=2e-12, above=False, below=None,
                      target='accept'):
    """
    Marks epochs based on a threshold criterion (any sensor exceeding the
    threshold at any time)

    above: True, False, None
        How to mark segments that exceed the threshold: True->good;
        False->bad; None->don't change

    below:
        Same as ``above`` but for segments that do not exceed the threshold

    threshold : float
        The threshold value.
        1.25e-11: detect saturated channels
        2e-12: conservative final rejection

    target : factor or str
        Factor (or its name) in which the result is stored. If ``var`` is
        a string and the dataset does not contain that factor, it is
        created.

    """
    if isinstance(DV, basestring):
        DV = dataset[DV]

    # get the factor on which to store results
    if _data.isfactor(target) or _data.isvar(target):
        assert len(target) == dataset.N
    elif isinstance(target, basestring):
        if target in dataset:
            target = dataset[target]
        else:
            x = _np.ones(dataset.N, dtype=bool)
            target = _data.var(x, name=target)
            dataset.add(target)
    else:
        raise ValueError("target needs to be a factor")

    # do the thresholding
    if _data.isndvar(DV):
        for ID in xrange(dataset.N):
            data = DV[ID]
            v = _np.max(_np.abs(data.x))

            if v > threshold:
                if above is not None:
                    target[ID] = above
            elif below is not None:
                target[ID] = below
    else:
        for ID in xrange(dataset.N):
            v = DV[ID]

            if v > threshold:
                if above is not None:
                    target[ID] = above
            elif below is not None:
                target[ID] = below
Example #2
0
    def __init__(self, Y, X, match=None, sub=None, match_func=np.mean, ds=None):
        """
        Parameters
        ----------

        Y : var, ndvar
            dependent measurement
        X : categorial
            factor or interaction
        match :
            factor on which cases are matched (i.e. subject for a repeated
            measures comparisons). If several data points with the same
            case fall into one cell of X, they are combined using
            match_func. If match is not None, celltable.groups contains the
            {Xcell -> [match values of data points], ...} mapping corres-
            ponding to self.data
        sub : bool array
            Bool array of length N specifying which cases to include
        match_func : callable
            see match
        ds : dataset
            If a dataset is specified, input items (Y / X / match / sub) can
            be str instead of data-objects, in which case they will be
            retrieved from the dataset.


        Examples
        --------

        Split a repeated-measure variable Y into cells defined by the
        interaction of A and B::

            >>> c = celltable(Y, A % B, match=subject)

        """
        if isinstance(Y, basestring):
            Y = ds.eval(Y)
        if isinstance(X, basestring):
            X = ds.eval(X)
        if isinstance(match, basestring):
            match = ds[match]
        if isinstance(sub, basestring):
            sub = ds.eval(sub)

        if _data.iscategorial(Y) or _data.isndvar(Y):
            if sub is not None:
                Y = Y[sub]
        else:
            Y = _data.asvar(Y, sub)

        if X is not None:
            X = _data.ascategorial(X, sub)

        if match:
            match = _data.asfactor(match, sub)
            assert len(match) == len(Y)
            self.groups = {}

        # save args
        self.X = X
        self.Y = Y
        self.sub = sub
        self.match = match

        # extract cells and cell data
        self.data = {}
        self.data_indexes = {}
        if X is None:
            self.data[None] = Y
            self.data_indexes[None] = np.ones(len(Y), dtype=bool)
            self.cells = [None]
            return

        self.cells = X.cells

        for cell in self.cells:
            self.data_indexes[cell] = cell_index = X == cell
            newdata = Y[cell_index]
            if match:
                group = match[cell_index]
                values = group.cells

                # sort
                if len(values) < len(group):
                    newdata = newdata.compress(group, func=match_func)
                    group = _data.factor(values, name=group.name)
                else:
                    group_ids = [group == v for v in values]
                    sort_arg = np.sum(group_ids * np.arange(len(values)), axis=0)
                    newdata = newdata[sort_arg]
                    group = group[sort_arg]

                self.groups[cell] = group

            self.data[cell] = newdata

        if match:
            # determine which cells compare values for dependent values on
            # match_variable
            #            n_cells = len(self.indexes)
            #            self.within = np.empty((n_cells, n_cells), dtype=bool)
            self.within = {}
            for cell1 in self.cells:
                for cell2 in self.cells:
                    if cell1 == cell2:
                        pass
                    else:
                        v = self.groups[cell1] == self.groups[cell2]
                        if v is not False:
                            v = all(v)
                        self.within[cell1, cell2] = v
                        self.within[cell2, cell1] = v
            self.all_within = np.all(self.within.values())
        else:
            self.all_within = False