Exemplo n.º 1
0
def gen_images(locs, features, n_gridpoints, normalize=True,
               augment=False, pca=False, std_mult=0.1, n_components=2, edgeless=False):
    """
    Generates EEG images given electrode locations in 2D space and multiple feature values for each electrode

    :param locs: An array with shape [n_electrodes, 2] containing X, Y
                        coordinates for each electrode.
    :param features: Feature matrix as [n_samples, n_features]
                                Features are as columns.
                                Features corresponding to each frequency band are concatenated.
                                (alpha1, alpha2, ..., beta1, beta2,...)
    :param n_gridpoints: Number of pixels in the output images
    :param normalize:   Flag for whether to normalize each band over all samples
    :param augment:     Flag for generating augmented images
    :param pca:         Flag for PCA based data augmentation
    :param std_mult     Multiplier for std of added noise
    :param n_components: Number of components in PCA to retain for augmentation
    :param edgeless:    If True generates edgeless images by adding artificial channels
                        at four corners of the image with value = 0 (default=False).
    :return:            Tensor of size [samples, colors, W, H] containing generated
                        images.
    """
    feat_array_temp = []
    nElectrodes = locs.shape[0]     # Number of electrodes
    # Test whether the feature vector length is divisible by number of electrodes
    assert features.shape[1] % nElectrodes == 0
    n_colors = int(features.shape[1] / nElectrodes)
    for c in range(n_colors):
        feat_array_temp.append(features[:, c * nElectrodes : nElectrodes * (c+1)])
    if augment:
        if pca:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c], std_mult, pca=True, n_components=n_components)
        else:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c], std_mult, pca=False, n_components=n_components)
    nSamples = features.shape[0]
    # Interpolate the values
    grid_x, grid_y = np.mgrid[
                     min(locs[:, 0]):max(locs[:, 0]):n_gridpoints*1j,
                     min(locs[:, 1]):max(locs[:, 1]):n_gridpoints*1j
                     ]
    temp_interp = []
    for c in range(n_colors):
        temp_interp.append(np.zeros([nSamples, n_gridpoints, n_gridpoints]))
    # Generate edgeless images
    if edgeless:
        min_x, min_y = np.min(locs, axis=0)
        max_x, max_y = np.max(locs, axis=0)
        locs = np.append(locs, np.array([[min_x, min_y], [min_x, max_y],[max_x, min_y],[max_x, max_y]]),axis=0)
        for c in range(n_colors):
            feat_array_temp[c] = np.append(feat_array_temp[c], np.zeros((nSamples, 4)), axis=1)
    # Interpolating
    for i in range(nSamples):
        for c in range(n_colors):
            temp_interp[c][i, :, :] = griddata(locs, feat_array_temp[c][i, :], (grid_x, grid_y),
                                    method='cubic', fill_value=np.nan)
        print('Interpolating {0}/{1}\r'.format(i+1, nSamples), end='\r')
    # Normalizing
    for c in range(n_colors):
        if normalize:
            temp_interp[c][~np.isnan(temp_interp[c])] = \
                scale(temp_interp[c][~np.isnan(temp_interp[c])])
        temp_interp[c] = np.nan_to_num(temp_interp[c])
    return np.swapaxes(np.asarray(temp_interp), 0, 1)     # swap axes to have [samples, colors, W, H]
Exemplo n.º 2
0
def gen_images(locs, features, nGridPoints, normalize=True,
               augment=False, pca=False, stdMult=0.1, n_components=2, edgeless=False):
    """
    Generates EEG images given electrode locations in 2D space and multiple feature values for each electrode
    :param loc: An array with shape [n_electrodes, 2] containing X, Y
                        coordinates for each electrode.
    :param features: Feature matrix as [n_samples, n_features+1]
                                Features are as columns.
                                Features corresponding to each frequency band are concatenated.
                                (alpha1, alpha2, ..., beta1, beta2,...)
    :param nGridPoints: Number of pixels in the output images
    :param normalize:   Flag for whether to normalize each feature over all samples
    :param augment:     Flag for generating augmented images
    :param pca:         Flag for PCA based data augmentation
    :param stdMult:     Standard deviation of noise for augmentation
    :param n_components:Number of components in PCA to retain for augmentation
    :param edgeless:    If True generates edgeless images by adding artificial channels
                        at four corners of the image with value = 0 (default=False).
    :return:            Tensor of size [samples, colors, W, H] containing generated
                        images.
    """

    feat_array_temp = []
    nElectrodes = locs.shape[0]     # Number of electrodes

    # Test whether the feature vector length is divisible by number of electrodes (last column is the labels)
    assert features.shape[1] % nElectrodes == 0
    n_colors = features.shape[1] / nElectrodes
    for c in range(n_colors):
        feat_array_temp.append(features[:, c * nElectrodes : nElectrodes * (c+1)])


    if augment:
        if pca:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c], stdMult, pca=True, n_components=n_components)
        else:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c], stdMult, pca=False, n_components=n_components)

    nSamples = features.shape[0]
    # Interpolate the values
    grid_x, grid_y = np.mgrid[
                     min(locs[:, 0]):max(locs[:, 0]):nGridPoints*1j,
                     min(locs[:, 1]):max(locs[:, 1]):nGridPoints*1j
                     ]
    temp_interp = []
    for c in range(n_colors):
        temp_interp.append(np.zeros([nSamples, nGridPoints, nGridPoints]))

    # Generate edgeless images
    if edgeless:
        min_x, min_y = np.min(locs, axis=0)
        max_x, max_y = np.max(locs, axis=0)
        locs = np.append(locs, np.array([[min_x, min_y], [min_x, max_y],[max_x, min_y],[max_x, max_y]]),axis=0)
        for c in range(n_colors):
            feat_array_temp[c] = np.append(feat_array_temp[c], np.zeros((nSamples, 4)), axis=1)

    for i in xrange(nSamples):
        for c in range(n_colors):
            temp_interp[c][i, :, :] = griddata(locs, feat_array_temp[c][i, :], (grid_x, grid_y),
                                    method='cubic', fill_value=np.nan)
        print 'Interpolating {0}/{1}\r'.format(i+1, nSamples),

    for c in range(n_colors):
        if normalize:
            temp_interp[c][~np.isnan(temp_interp[c])] = \
                scale(temp_interp[c][~np.isnan(temp_interp[c])])
        temp_interp[c] = np.nan_to_num(temp_interp[c])

    return np.swapaxes(np.asarray(temp_interp), 0, 1)     # swap axes to have [samples, colors, W, H]
Exemplo n.º 3
0
def gen_images(loc_filename, features_filename, nGridPoints,
               augment=False, pca=False, stdMult=0.1, n_components=2):
    """

    :param loc_filename: Address of the MAT file containing electrode coordinates.
                        An array with shape [n_electrodes, 2] containing X, Y
                        coordinates for each electrode.
    :param features_filename: Address of the MAT file containing features as columns and
                                last column as labels.
    :param nGridPoints: Number of pixels in the output images
    :param augment:     Flag for generating augmented images
    :param pca:         Flag for PCA based data augmentation
    :param stdMult:     Standard deviation of noise for augmentation
    :param n_components:Number of components in PCA to retain for augmentation
    :return:            Tensor of size [samples, colors, W, H] containing generated
                        images.
    """
    # Load electrode locations projected on a 2D surface
    mat = scipy.io.loadmat(loc_filename)
    locs = mat['proj'] * [1, -1]    # reverse the Y axis to have the front on the top
    nElectrodes = locs.shape[0]     # Number of electrodes

    # Load feature values
    mat = scipy.io.loadmat(features_filename)
    data = mat['features']
    feat_array_temp = []

    # Test whether the feature vector length is divisible by number of electrodes (last column is the labels)
    assert data.shape[1] % nElectrodes == 1
    n_colors = data.shape[1] / nElectrodes
    for c in range(n_colors):
        feat_array_temp.append(data[:, c * nElectrodes : nElectrodes * (c+1)])


    if augment:
        if pca:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c], stdMult, pca=True, n_components=n_components)
        else:
            for c in range(n_colors):
                feat_array_temp[c] = augment_EEG(feat_array_temp[c], stdMult, pca=False, n_components=n_components)

    labels = data[:, -1]
    nSamples = data.shape[0]
    # Interpolate the values
    grid_x, grid_y = np.mgrid[
                     min(locs[:, 0]):max(locs[:, 0]):nGridPoints*1j,
                     min(locs[:, 1]):max(locs[:, 1]):nGridPoints*1j
                     ]
    temp_interp = []
    for c in range(n_colors):
        temp_interp.append(np.zeros([nSamples, nGridPoints, nGridPoints]))

    for i in xrange(nSamples):
        for c in range(n_colors):
            temp_interp[c][i, :, :] = griddata(locs, feat_array_temp[c][i, :], (grid_x, grid_y),
                                    method='cubic', fill_value=np.nan)
        print 'Interpolating {0}/{1}\r'.format(i+1, nSamples),

    for c in range(n_colors):
        temp_interp[c][~np.isnan(temp_interp[c])] = \
            scale(temp_interp[c][~np.isnan(temp_interp[c])])
        temp_interp[c] = np.nan_to_num(temp_interp[c])

    return np.swapaxes(np.asarray(temp_interp), 0, 1)     # swap axes to have [samples, colors, W, H]
Exemplo n.º 4
0
)
data = mat['features']
labels = data[:, -1]
nSamples = data.shape[0]

timeFeatMat = []
for winNum in range(numTimeWin):
    print 'Processing window {0}/{1}'.format(winNum + 1, numTimeWin)
    thetaFeats = data[:, winNum * 192:winNum * 192 + 64]
    alphaFeats = data[:, winNum * 192 + 64:winNum * 192 + 128]
    betaFeats = data[:, winNum * 192 + 128:winNum * 192 + 192]

    if augment:
        if pca:
            thetaFeats = augment_EEG(thetaFeats,
                                     stdMult,
                                     pca=True,
                                     n_components=n_components)
            alphaFeats = augment_EEG(alphaFeats,
                                     stdMult,
                                     pca=True,
                                     n_components=n_components)
            betaFeats = augment_EEG(betaFeats,
                                    stdMult,
                                    pca=True,
                                    n_components=n_components)
        else:
            thetaFeats = augment_EEG(thetaFeats,
                                     stdMult,
                                     pca=False,
                                     n_components=n_components)
            alphaFeats = augment_EEG(alphaFeats,
Exemplo n.º 5
0
# ax = fig.add_subplot(111)
# ax.scatter(locs[:, 0],locs[:, 1])
# ax.set_xlabel('X'); ax.set_ylabel('Y')
# ax.set_title('Polar Projection')
# pl.ion(); pl.show()

# Load power values
mat = scipy.io.loadmat('Z:\CVPIA\Pouya\SDrive\SkyDrive\Documents\Proposal\Programs\Classifier\Datasets\WM_features_MSP.mat')
data = mat['features']
thetaFeats = data[:, :64]
alphaFeats = data[:, 64:128]
betaFeats = data[:, 128:192]

if augment:
    if pca:
        thetaFeats = augment_EEG(thetaFeats, stdMult, pca=True, n_components=n_components)
        alphaFeats = augment_EEG(alphaFeats, stdMult, pca=True, n_components=n_components)
        betaFeats = augment_EEG(betaFeats, stdMult, pca=True, n_components=n_components)
    else:
        thetaFeats = augment_EEG(thetaFeats, stdMult, pca=False, n_components=n_components)
        alphaFeats = augment_EEG(alphaFeats, stdMult, pca=False, n_components=n_components)
        betaFeats = augment_EEG(betaFeats, stdMult, pca=False, n_components=n_components)

labels = data[:, -1]
nSamples = data.shape[0]

# Interpolate the values
grid_x, grid_y = np.mgrid[
                 min(locs[:, 0]):max(locs[:, 0]):nGridPoints*1j,
                 min(locs[:, 1]):max(locs[:, 1]):nGridPoints*1j
                 ]