def classify(inputfile='slpdb.mat', epoch_length=30, make_plot=True, save=True, outputfile='classify.txt'):
    # Load datas
    dataset = loadData(inputfile)
    ECG = groupByEpoch(dataset['ECG'], 250, epoch_length)
    Resp_in, Resp_out = edr.main(dataset)

    # Feature dictionary construction
    d_test = dictionaryInitialization()
    d_test = dico_construction(ECG, Resp_in, Resp_out, d_test)
    Xarr_norm = createNormalizedDataframe(d_test)

    # Predecition
    model = joblib.load('model.pkl')
    pred_res = list()
    for j in range(0, len(ECG)):
        l = str(model.predict(Xarr_norm[j])[0])
        pred_res.append(l)

    if make_plot:
        # Plot result
        plot(pred_res, dataset['labels'], epoch_length)

    if save:
        # Write prediction into text file
        savePredictions(pred_res, outputfile)
def classify(inputfile='slpdb.mat',
             epoch_length=30,
             make_plot=True,
             save=True,
             outputfile='classify.txt'):
    # Load datas
    dataset = loadData(inputfile)
    ECG = groupByEpoch(dataset['ECG'], 250, epoch_length)
    Resp_in, Resp_out = edr.main(dataset)

    # Feature dictionary construction
    d_test = dictionaryInitialization()
    d_test = dico_construction(ECG, Resp_in, Resp_out, d_test)
    Xarr_norm = createNormalizedDataframe(d_test)

    # Predecition
    model = joblib.load('model.pkl')
    pred_res = list()
    for j in range(0, len(ECG)):
        l = str(model.predict(Xarr_norm[j])[0])
        pred_res.append(l)

    if make_plot:
        # Plot result
        plot(pred_res, dataset['labels'], epoch_length)

    if save:
        # Write prediction into text file
        savePredictions(pred_res, outputfile)
Exemplo n.º 3
0
from data import loadData, groupByEpoch
from models import createNormalizedDataframe
from feature_construction import dictionaryInitialization, dico_construction
from sklearn.svm import SVC
from sklearn.externals import joblib
from edr import main

# Load datas
ECG = np.array([])
labels = np.array([])
binarylabels = np.array([])
for file in os.listdir("traindata"):
    if file.endswith(".mat"):
        dataset = loadData('traindata/' + file)
        ECG = np.append(ECG, dataset['ECG'])
        Resp_in, Resp_out = main(dataset)
        labels = np.append(labels, dataset['labels'])
        binarylabels = np.append(binarylabels, dataset['binarylabels'])
X = groupByEpoch(ECG)

# Train dictionary construction
d_train = dictionaryInitialization()
d_train = dico_construction(X, Resp_in, Resp_out, d_train)
y = np.array(labels)
Xarr_norm = createNormalizedDataframe(d_train)
Xarr_norm = np.nan_to_num(Xarr_norm).reshape((len(Xarr_norm), 1))

# Compute model and write to file
model = SVC(kernel='linear').fit(Xarr_norm, y)
joblib.dump(model, 'model.pkl')
from models import createNormalizedDataframe
from feature_construction import dictionaryInitialization, dico_construction
from sklearn.svm import SVC
from sklearn.externals import joblib
from edr import main


# Load datas
ECG = np.array([])
labels = np.array([])
binarylabels = np.array([])
for file in os.listdir("traindata"):
    if file.endswith(".mat"):
        dataset = loadData('traindata/' + file)
        ECG = np.append(ECG, dataset['ECG'])
        Resp_in, Resp_out = main(dataset)
        labels = np.append(labels, dataset['labels'])
        binarylabels = np.append(binarylabels, dataset['binarylabels'])
X = groupByEpoch(ECG)

# Train dictionary construction
d_train = dictionaryInitialization()
d_train = dico_construction(X, Resp_in, Resp_out, d_train)
y = np.array(labels)
Xarr_norm = createNormalizedDataframe(d_train)
Xarr_norm = np.nan_to_num(Xarr_norm).reshape((len(Xarr_norm), 1))

# Compute model and write to file
model = SVC(kernel='linear').fit(Xarr_norm, y)
joblib.dump(model, 'model.pkl')