Ejemplo n.º 1
0
def by_labels(X, y):
    """Split up X into a list if Xs, one for each unique entry in y.

    Parameters
    ----------
    X : a 2d array-like (n_features x n_sample)
        The feature matrix
    y : a 1d array-like
        The labels for X

    Return
    ------
    Xs : seq-like, containing 2D arrays (n_feature x n_sample)
        The split up Xs
    ys : seq-like, containing 1D arrays
        The y matching each X in Xs
    """

    # ----`
    # Find all samples
    y_masks = []
    unique_y = sorted(np.unique(y))
    unique_y = unique_sorted_with_nan(unique_y)

    for y_i in unique_y:
        y_masks.append(np.str(y_i) == y)
    
    # And split each feature into seperate Xs
    Xs = []
    ys = []
    for mask in y_masks:
        Xs.append(X[mask,])
        ys.append(y[mask])

    return Xs, ys
Ejemplo n.º 2
0
    def __init__(self, y, indices=True):
        self.y = np.array(y)
        self.uniquey = sorted(list(set(self.y.tolist())))
        self.uniquey = unique_sorted_with_nan(self.unique_y)

        self.n = len(self.uniquey)
        self.indices = indices
Ejemplo n.º 3
0
def by_labels(X, y):
    """Split up X into a list if Xs, one for each unique entry in y.

    Parameters
    ----------
    X : a 2d array-like (n_features x n_sample)
        The feature matrix
    y : a 1d array-like
        The labels for X

    Return
    ------
    Xs : seq-like, containing 2D arrays (n_feature x n_sample)
        The split up Xs
    ys : seq-like, containing 1D arrays
        The y matching each X in Xs
    """

    # ----`
    # Find all samples
    y_masks = []
    unique_y = sorted(np.unique(y))
    unique_y = unique_sorted_with_nan(unique_y)

    for y_i in unique_y:
        y_masks.append(np.str(y_i) == y)

    # And split each feature into seperate Xs
    Xs = []
    ys = []
    for mask in y_masks:
        Xs.append(X[mask, ])
        ys.append(y[mask])

    return Xs, ys
Ejemplo n.º 4
0
    def __init__(self, y, indices=True):
        self.y = np.array(y)
        self.uniquey = sorted(list(set(self.y.tolist())))
        self.uniquey = unique_sorted_with_nan(self.unique_y)

        self.n = len(self.uniquey)
        self.indices = indices
Ejemplo n.º 5
0
def eva(X, y, trial_index, window, tr):
    """Average trials for each feature in X

    Parameters
     ----------
     X : 2D array-like (n_sample, n_feature)
         The data to decompose
     y : 1D array, None by default
         Sample labels for the data.  In y, np.nan and 'nan' values 
         are ignored.
     trial_index : 1D array (n_sample, )
         Each unique entry should match a trial.
     window : int 
         Trial length

     Return
     ------
     Xeva : a 2D arrays (n_feature*unique_y, window)
         The average trials
     feature_names : 1D array
         The names of the features (taken from y)
    """

    evas = []
    eva_names = []
    scaler = MinMaxScaler(feature_range=(0, 1))
    for j in range(X.shape[1]):
        Xtrials = []
        
        xj = X[:,j][:,np.newaxis]  ## Need 2D

        # Each feature into trials, rescale too
        Xtrial, feature_names = by_trial(xj, trial_index, window, y)
        Xtrial = scaler.fit_transform(Xtrial.astype(np.float))
        unique_fn = sorted(np.unique(feature_names))
        unique_fn = unique_sorted_with_nan(unique_fn)

        # and again by unique_y/fe]ature_names
        Xlabels, _ = by_labels(X=Xtrial.transpose(), y=feature_names)

        # put all that togthether
        Xtrials.extend([Xl.transpose() for Xl in Xlabels])

        # and average the trials then
        # name names.
        evas.extend([Xt.mean(axis=1) for Xt in Xtrials])
        eva_names.extend(unique_fn)

    # Reshape : (window, len(unique_y)*n_features)
    Xeva = np.vstack(evas).transpose()
    eva_names = np.asarray(eva_names)

    assert checkX(Xeva)
    assert Xeva.shape[0] == window, ("After EVA rows not equal to window")
    assert Xeva.shape[1] == len(unique_fn) * X.shape[1], ("After" 
        "EVA wrong number of features")
    assert eva_names.shape[0] == Xeva.shape[1], ("eva_names and Xeva" 
        "don't match")

    return Xeva, eva_names
Ejemplo n.º 6
0
def eva(X, y, trial_index, window, tr):
    """Average trials for each feature in X

    Parameters
     ----------
     X : 2D array-like (n_sample, n_feature)
         The data to decompose
     y : 1D array, None by default
         Sample labels for the data.  In y, np.nan and 'nan' values 
         are ignored.
     trial_index : 1D array (n_sample, )
         Each unique entry should match a trial.
     window : int 
         Trial length

     Return
     ------
     Xeva : a 2D arrays (n_feature*unique_y, window)
         The average trials
     feature_names : 1D array
         The names of the features (taken from y)
    """

    evas = []
    eva_names = []
    scaler = MinMaxScaler(feature_range=(0, 1))
    for j in range(X.shape[1]):
        Xtrials = []

        xj = X[:, j][:, np.newaxis]  ## Need 2D

        # Each feature into trials, rescale too
        Xtrial, feature_names = by_trial(xj, trial_index, window, y)
        Xtrial = scaler.fit_transform(Xtrial.astype(np.float))
        unique_fn = sorted(np.unique(feature_names))
        unique_fn = unique_sorted_with_nan(unique_fn)

        # and again by unique_y/fe]ature_names
        Xlabels, _ = by_labels(X=Xtrial.transpose(), y=feature_names)

        # put all that togthether
        Xtrials.extend([Xl.transpose() for Xl in Xlabels])

        # and average the trials then
        # name names.
        evas.extend([Xt.mean(axis=1) for Xt in Xtrials])
        eva_names.extend(unique_fn)

    # Reshape : (window, len(unique_y)*n_features)
    Xeva = np.vstack(evas).transpose()
    eva_names = np.asarray(eva_names)

    assert checkX(Xeva)
    assert Xeva.shape[0] == window, ("After EVA rows not equal to window")
    assert Xeva.shape[1] == len(unique_fn) * X.shape[1], (
        "After"
        "EVA wrong number of features")
    assert eva_names.shape[0] == Xeva.shape[1], ("eva_names and Xeva"
                                                 "don't match")

    return Xeva, eva_names
Ejemplo n.º 7
0
def fir(X, y, trial_index, window, tr):
    """ Average trials for each feature in X, using Burock's 
    (2000) method.
    
    Parameters
    ----------
    X : 2D array-like (n_sample, n_feature)
        The data to decompose
    y : 1D array, None by default
         Sample labels for the data. In y, np.nan and 'nan' values 
         are treated as baseline labels.
    trial_index : 1D array (n_sample, )
        Each unique entry should match a trial.
    window : int 
        Trial length

    Return
    ------
    Xfir : a 2D arrays (n_feature*unique_y, window)
        The average trials
    feature_names : 1D array
    """

    # Norm then pad.
    scaler = MinMaxScaler(feature_range=(0, 1))
    X = scaler.fit_transform(X.astype(np.float))
    X = np.vstack([X, np.ones((window, X.shape[1]), dtype=np.float)])

    # Save the org y names
    ynames = sorted(np.unique(y))
    ynames = unique_sorted_with_nan(ynames)

    # y becomes integers
    y = create_y(y)

    # Make the design matrix.
    dm = _create_dm(y, window)
    # dm DEBUG
    #import time
    #np.savetxt("dm-{0}".format(time.strftime("%m_%d_%Y_%H_%s_%m")), dm, fmt="%1.0f")
    dm = np.matrix(dm)

    # FIR!
    fir_names = []
    firs = []
    for j in range(X.shape[1]):
        x = np.matrix(X[:, j])
        fir = np.array(np.linalg.pinv(dm.T * dm) * dm.T * x.T)[0:-1]
        ## Drop dummy
        fir = fir.reshape(len(ynames) - 1, window)

        firs.append(fir)
        fir_names.extend(ynames[1:])  ## Drop nan/baseline

    Xfir = np.vstack(firs).transpose()
    fir_names = np.asarray(fir_names)

    assert checkX(Xfir)
    assert Xfir.shape[0] == window, ("After FIR rows not equal to window")
    assert Xfir.shape[1] == (len(ynames[1:]) *
                             X.shape[1]), ("After"
                                           "FIR wrong number of features")
    assert fir_names.shape[0] == Xfir.shape[1], ("fir_names and Xfir"
                                                 "don't match")

    return Xfir, fir_names
Ejemplo n.º 8
0
def fir(X, y, trial_index, window, tr):
    """ Average trials for each feature in X, using Burock's 
    (2000) method.
    
    Parameters
    ----------
    X : 2D array-like (n_sample, n_feature)
        The data to decompose
    y : 1D array, None by default
         Sample labels for the data. In y, np.nan and 'nan' values 
         are treated as baseline labels.
    trial_index : 1D array (n_sample, )
        Each unique entry should match a trial.
    window : int 
        Trial length

    Return
    ------
    Xfir : a 2D arrays (n_feature*unique_y, window)
        The average trials
    feature_names : 1D array
    """

    # Norm then pad.
    scaler = MinMaxScaler(feature_range=(0, 1))
    X = scaler.fit_transform(X.astype(np.float))
    X = np.vstack([X, np.ones((window, X.shape[1]), dtype=np.float)])

    # Save the org y names
    ynames = sorted(np.unique(y))
    ynames = unique_sorted_with_nan(ynames)
    
    # y becomes integers
    y = create_y(y)

    # Make the design matrix.
    dm = _create_dm(y, window)
    # dm DEBUG
    #import time
    #np.savetxt("dm-{0}".format(time.strftime("%m_%d_%Y_%H_%s_%m")), dm, fmt="%1.0f")
    dm = np.matrix(dm)
    
    # FIR!
    fir_names = []
    firs = []
    for j in range(X.shape[1]):
        x = np.matrix(X[:,j])
        fir = np.array(np.linalg.pinv(dm.T * dm) * dm.T * x.T)[0:-1] 
            ## Drop dummy
        fir = fir.reshape(len(ynames)-1, window)  

        firs.append(fir)
        fir_names.extend(ynames[1:])  ## Drop nan/baseline

    Xfir = np.vstack(firs).transpose()
    fir_names = np.asarray(fir_names)

    assert checkX(Xfir)
    assert Xfir.shape[0] == window, ("After FIR rows not equal to window")
    assert Xfir.shape[1] == (len(ynames[1:]) * X.shape[1]), ("After" 
        "FIR wrong number of features")
    assert fir_names.shape[0] == Xfir.shape[1], ("fir_names and Xfir" 
        "don't match")

    return Xfir, fir_names