Beispiel #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
Beispiel #2
0
    def __init__(self, Y, X, match=None, sub=None, match_func=np.mean):
        """
        divides Y into cells defined by X
        
        Y       dependent measurement
        X       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 of length N specifying which cases to include
        match_func:  see match
        
        
        e.g.
        >>> c = S.celltable(Y, A%B, match=subject)
        
        """
        if _data.isfactor(Y):
            if sub is not None:
                Y = Y[sub]
        else:
            Y = _data.asvar(Y, sub)
        
        X = _data.ascategorial(X, sub)
        assert X.N == Y.N
        
        if match:
            match = _data.asfactor(match, sub)
            assert match.N == Y.N
            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 = {}
        self.cells = X.cells
        self.indexes = sorted(X.cells.keys())
        for cell in self.indexes:
            sub = X==cell
            self.data_indexes[cell] = sub
            newdata = Y.x[sub]
            if match:
                # get match ids
                group = match.x[sub]
                occurring_ids = np.unique(group)
                
                # sort
                if len(occurring_ids) < len(group):
                    newdata = np.array([match_func(newdata[group==ID]) 
                                        for ID in occurring_ids])
                    group = occurring_ids
                else:
                    sort_arg = np.argsort(group)
                    group = group[sort_arg]
                    newdata = newdata[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.indexes:
                for cell2 in self.indexes:
                    if cell1==cell2:
                        self.within[cell1,cell2] = True
                    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.within = self.all_within = False