Example #1
0
    def __init__(self,
                 Bc_path,
                 feture_path,
                 speck_path,
                 Kfold_path,
                 max_windows=None,
                 logfile_path=None):
        """

        :param Bc_path:
        :param feture_path: feture vektors dataset
        :param speck_path: spectrograms dataset
        :param Kfold_path: path to K-fold.json fille
        :param max_windows:  limet numbers of files for debugging purpes. WARNING the program would still show to numbers of files in your fold
        :param logfile_path: path to folder where log files is to be saved
        """
        self.max_windows = max_windows
        self.logfile_path = logfile_path

        self.prepros = preprossingPipeline(mac=False, BC_datapath=Bc_path)
        self.feture_path = feture_path
        self.speck_path = speck_path

        with open(os.path.join(os.getcwd(), Kfold_path), "r") as read_file:
            self.Kfold = json.load(read_file)
    def __init__(self,
                 Bc_path,
                 feture_path,
                 speck_path,
                 inner_splits=5,
                 outer_splits=5,
                 max_files=None,
                 logfile_path=None):
        self.innerK = inner_splits
        self.outerK = outer_splits
        self.max_files = max_files
        self.logfile_path = logfile_path
        text_file = open("filenames.txt", "r")
        x = text_file.read().splitlines(
        )  #Read 126 files, first 100 is usable, the next 26 is not usable
        text_file.close()

        text_file = open("isUsable.txt", "r")
        y = text_file.read().splitlines(
        )  #Read 126 files, first 100 is usable, the next 26 is not usable
        text_file.close()

        self.prepros = preprossingPipeline(mac=False, BC_datapath=Bc_path)
        self.feture_path = feture_path
        self.speck_path = speck_path

        self.outerloop(x, y)
Example #3
0
    def __init__(self,
                 feature_path,
                 spectrograms_path,
                 BC_datapath,
                 Kfold_path,
                 type,
                 annotation="Is Eeg Usable For Clinical Purposes"):
        """"
        annotation="Quality Of Eeg" or "Is Eeg Usable For Clinical Purposes"
        """

        self.get_data = preprossingPipeline(BC_datapath=BC_datapath)

        self.LDA = None  #LDA model
        self.PCA = None  #PCA model
        self.TSNE = None  #TSNE model
        self.max_windows = 30
        self.max_files = 10
        self.le = preprocessing.LabelEncoder()
        with open(os.path.join(os.getcwd(), Kfold_path), "r") as read_file:
            Kfold = json.load(read_file)

        trainNames = Kfold[f"train_0"]
        testNames = Kfold[f"test_0"]

        self.type = type

        if type == "Feture_test":
            self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_vectors(
                trainNames, feature_path, annotation)
            self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_vectors(
                testNames, feature_path, annotation)
            self.le.fit(np.append(self.filenames_train, self.filenames_test))
            self.X_train = scale_data(self.X_train)
            self.X_test = scale_data(self.X_test)

        if type == "Spectrograms_test":
            self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_vectors(
                trainNames, spectrograms_path, annotation)
            self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_vectors(
                testNames, spectrograms_path, annotation)
            self.le.fit(np.append(self.filenames_train, self.filenames_test))
            self.X_train = scale_data(self.X_train)
            self.X_test = scale_data(self.X_test)

        if type == "Spectrograms_balance":
            self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_balance(
                5, spectrograms_path, annotation)
            self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_balance(
                26, spectrograms_path, annotation)
            self.le.fit(self.filenames_test)
        if type == "Feature_balance":
            self.X_test, self.Y_test, self.filenames_test, self.test_id = self.get_balance(
                5, feature_path, annotation)
            self.X_train, self.Y_train, self.filenames_train, self.train_id = self.get_balance(
                26, feature_path, annotation)
            self.le.fit(self.filenames_test)
def plot_comand(feature_path, path_pca, BC_datapath, newplot=True):
    pca = pickle.load(open(path_pca, 'rb'))
    get_data = preprossingPipeline(BC_datapath=BC_datapath)
    feature_vectors, labels, filenames, window_id = get_data.make_label(
        quality=None, is_usable=None, max_files=10, path=feature_path)
    while True:
        print("What you want to plot, options pca or")
        command = input()
        if command == "pca":
            feature_vectors, labels, filenames, window_id = get_data.make_label(
                quality=None, is_usable=None, max_files=10, path=feature_path)
            scaled_feature_vectors = scale_data(feature_vectors)
            pca_vectors = pca.transform(scaled_feature_vectors)
            print("what components ex 0 1")
            components = input()
            components = components.split(" ")
            plot_pca_interactiv(pca_vectors, labels, window_id, components[0],
                                components[1])

        if command == "break":
            break

        if command == "spec" or command == "EEG":
            print("Insert index")
            idx = int(input())
            if newplot == False:
                get_data.plot_window(window_id[idx][0],
                                     window_id[idx][1],
                                     type=command)
            else:
                data, time, chanels = get_data.plot_window(window_id[idx][0],
                                                           window_id[idx][1],
                                                           type=command,
                                                           plot=True)
                data = np.array(data).T
                df = pd.DataFrame(data, columns=chanels, index=time)
                fig = make_subplots(rows=len(chanels), cols=1)
                for i, ch in enumerate(chanels):
                    fig.add_trace(px.line(df, y=ch, x=df.index),
                                  row=(i + 1),
                                  col=1)
                fig = px.line(df, y=chanels[0], x=df.index)
                fig.show()
            x = model.avgpool(x)
            x = torch.flatten(x, 1)
            tempFeatureVec = model.classifer[0:4](x)
            #tempFeatureVec=tensor_window[i].reshape(1,-1)
            featureVec = tempFeatureVec
        else:
            x = model.features(tensor_window[i].unsqueeze(0).float())
            x = model.avgpool(x)
            x = torch.flatten(x, 1)
            tempFeatureVec = model.classifer[0:4](x)
            #tempFeatureVec = tensor_window[i].reshape(1,-1)
            featureVec = torch.cat((featureVec, tempFeatureVec), 1)
    return featureVec.detach()


C=preprossingPipeline(BC_datapath=r"C:\Users\Andre\Desktop\Fagproject\Data\BC",mac=False)
fileNames=C.edfDict.keys()
wdir=r"C:\Users\Andre\Desktop\Fagproject\Feture_vectors_new"
path_new=r'D:\spectograms_rgb'
i=0
model = trained_model_RGB
model.eval()
for file in fileNames:
    if os.path.exists(wdir+r'/spectograms/'+file)==True:
        pass
    else:
        #try:
            windowVec = 0
            tensor, _, _, _ = C.make_label_cnn(make_from_filenames=[file], path='/Volumes/B/spectograms_rgb')
            tensor.requires_grad_(requires_grad=False)
            print(file)
Example #6
0
    j2 = np.asarray(j2[1, :], dtype='int64')
    n1 = np.sum(j1[:n_train_files1])
    n2 = np.sum(j2[:n_train_files2])
    wt1 = windows1[:n1, :, :, :]
    wt2 = windows2[:n2, :, :, :]
    w1 = torch.cat((wt1, wt2))
    ww1 = windows1[n1:, :, :, :]
    ww2 = windows2[n2:, :, :, :]
    w2 = torch.cat((ww1, ww2))
    l1 = labels1[:n1] + labels2[:n2]
    l2 = labels1[n1:] + labels2[n2:]
    wid = window_idx_full1[n1:] + window_idx_full2[n2:]
    train_windows, train_labels = shuffle(w1, l1)
    test_windows, test_labels, test_id = shuffle(w2, l2, wid)
    return train_windows.detach(), test_windows.detach(
    ), train_labels, test_labels, test_id


C = preprossingPipeline(
    BC_datapath=
    r"C:\Users\johan\iCloudDrive\DTU\KID\4. semester\Fagprojekt\Data\dataEEG")
path_s = r'D:\spectograms_rgb'
_, X_valid, _, Y_valid, windows_id = split_dataset(C,
                                                   path_s,
                                                   N=120,
                                                   train_split=80,
                                                   max_windows=10,
                                                   num_channels=14)
torch.save(X_valid, 'test_set.pt')
np.save('test_labels.npy', np.asarray(Y_valid))
np.save('test_windows.npy', np.asarray(windows_id))
Example #7
0
    wt1 = windows1[:n4, :, :, :]
    wt2 = windows3[:n3, :, :, :]
    w1 = torch.cat((wt1, wt2))
    ww1 = windows1[n1:, :, :, :]
    ww2 = windows2[n2:, :, :, :]
    w2 = torch.cat((ww1, ww2))
    l1 = labels4[:n4] + labels3[:n3]
    l2 = labels1[n1:] + labels2[n2:]
    wid = window_idx_full1[n1:] + window_idx_full2[n2:]
    train_windows, train_labels = shuffle(w1, l1)
    test_windows, test_labels, test_id = shuffle(w2, l2, wid)
    return train_windows.detach(), test_windows.detach(
    ), train_labels, test_labels, test_id


C = preprossingPipeline(BC_datapath=r"/work3/s173934/Fagprojekt/dataEEG")
path_s = r'/work3/s173934/Fagprojekt/spectograms_rgb'
criterion = nn.CrossEntropyLoss()
X_train, X_valid, Y_train, Y_valid, windows_id = split_dataset_balanced(
    C, path_s, N=120, train_split=80, max_windows=20, num_channels=14)
modelA = VGG16()
freeze_parameters(modelA, feature_extracting=True)
list2 = np.array(list_of_features(modelA))
modelB = VGG16()
freeze_parameters(modelB, feature_extracting=True)
for i in range(2):
    PATH = '/zhome/87/9/127623/FagprojektBALANCEDTESTS'
    if i == 0:
        activation_list = np.array([28, 29, 30, 31])
        grad_parameters(modelA, list(list2[activation_list]))
        optimizer = optim.Adam(modelA.parameters(), lr=0.0005)
Example #8
0
import os

from Preprossering.PreprosseringPipeline import preprossingPipeline

pca = pickle.load(
    open(r'C:\Users\Andreas\Desktop\KID\Fagproject\PCA_TSNE/PCA.sav', 'rb'))
TSNE = pickle.load(
    open(r'C:\Users\Andreas\Desktop\KID\Fagproject\PCA_TSNE/TSNE.sav', 'rb'))
tsne_fv = np.load(
    r'C:\Users\Andreas\Desktop\KID\Fagproject\PCA_TSNE\tsne_features.npy')
pca_fv = np.load(
    r'C:\Users\Andreas\Desktop\KID\Fagproject\PCA_TSNE\pca_features.npy')

#C = preprossingPipeline(
#    BC_datapath=r"/Users/villadsstokbro/Dokumenter/DTU/KID/3. semester/Fagprojekt/BrainCapture/dataEEG", mac=True)
C = preprossingPipeline(r"C:\Users\Andreas\Desktop\KID\Fagproject\Data\BC")
fileNames = C.edfDict.keys()
i = 0
#Change wdir to the directory of the folder 'feature_vectors'
wdir = os.getcwd()
for filename in fileNames:
    if not os.path.exists(wdir + r'/feature_vectors/' + filename + '.npy'):
        pass
    else:
        if i == 0:
            featureVec = np.load(wdir + r'/feature_vectors/' + filename +
                                 '.npy')
            featureVectors = featureVec
        else:
            featureVec = np.load(wdir + r'/feature_vectors/' + filename +
                                 '.npy')
from Preprossering.PreprosseringPipeline import preprossingPipeline
import os
import numpy as np

model = VGG16_NoSoftmax_OneChannel()
model.eval()
C=preprossingPipeline(BC_datapath=r"/Users/villadsstokbro/Dokumenter/DTU/KID/3. semester/Fagprojekt/BrainCapture/dataEEG",mac=True)
fileNames=C.edfDict.keys()
wdir="/Users/villadsstokbro/Dokumenter/DTU/KID/3. semester/Fagprojekt"


for file in fileNames:
    if os.path.exists(wdir+r'/spectograms_allch/'+file+".npy")==True:
        np.load(wdir+r'/feature_vectors/'+file+".npy)
    else:
        spec = C.get_spectrogram(file)
        break






Example #10
0
from loadPretrainedCNN import VGG16_NoSoftmax_OneChannel, VGG16_NoSoftmax_RGB, fetchImage
#Responsible: Mads Christian
import torch
from Preprossering.PreprosseringPipeline import preprossingPipeline, getFeatureVec
if __name__ == "__main__":
    model = VGG16_NoSoftmax_OneChannel()
    model.eval()

    C = preprossingPipeline(
        mac=True,
        BC_datapath=
        r"C:\Users\Mads-\Documents\Universitet\4. Semester\02466 Fagprojekt - Bachelor i kunstig intelligens og data\dataEEG"
    )
    fileNames = C.edfDict

    randomFile = fileNames.__iter__().__next__()
    spec = C.get_spectrogram(randomFile)
    randomWindow = spec.__iter__().__next__()
    featureVec = getFeatureVec(spec[randomWindow], model)

    #Generate a pretrained VGG model
    # model = VGG16_NoSoftmax_OneChannel()
    # model_rgb = VGG16_NoSoftmax_RGB()
    # path = r'C:\Users\Mads-_uop20qq\Documents\fagprojekt\Fagprojekt2020\testSpektrograms\test3_or_above1_0.JPG'
    # img = fetchImage(path) #Spectrograms should have dim: [3,224,224] for VGG_NoSoftmax_RGB
    # img2 = img[1,:,:] #Spectrograms should have dim: [224,224] for VGG_NoSoftmax_OneChannel
    #
    # model.eval()
    # model_rgb.eval()
    #
    # out1 = model(img2.unsqueeze(0).unsqueeze(0).float()) #input should be tensor with dim: [1,1,224,224] corresponding to [batchsize, channels, breadth, width], [output 1x4096]