Beispiel #1
0
def classify():
    global model, verbose
    print("-" * 30)
    print("Sleep Ensemble Classification")
    print("Current model:", model.name)
    print("-" * 30)
    print("Select files to classify")
    print("This model is configured to accept", model.reader.standard)
    filepaths = ask_filenames(filetypes=model.reader.filetypes)
    if len(filepaths) == 0:
        print("You didn't select any files! Returning you to the main menu")
        return
    print("Select where classifications should go:")
    destination = ask_directory()
    if destination is None or not os.path.isdir(destination):
        print(
            "You didn't select a destination! Returning you to the main menu")
        return
    save_input = yes_no_loop(
        "Do you also want to export processed data alongside classifications?")
    print("Identified", len(filepaths), "files to classify")
    if verbose == 1: jobs = trange(len(filepaths))
    else: jobs = range(len(filepaths))
    for i in jobs:
        if verbose > 1: print("Classifying", filepaths[i])
        data, _ = model.read(filepaths[i], labels=False)
        name = Path(filepaths[i]).stem
        ds = model.process(data, None, name)
        p = model.predict(ds.data)
        result = Dataset(label_names=['PREDICTION'], labels=p.reshape(-1, 1))
        if save_input:
            result = ds.concatenate(result)
        result.name = name + "-predictions"
        if verbose > 1: print("Writing results")
        result.write(destination)
    print("Completed classification jobs!")
Beispiel #2
0
def validate():
    global model, verbose
    print("-" * 30)
    print("Sleep Ensemble Validation")
    print("Current model:", model.name)
    print("-" * 30)
    print("Select files to use for validation:")
    print("This model is configured to accept", model.reader.standard)
    filepaths = ask_filenames(filetypes=model.reader.filetypes)
    if len(filepaths) == 0:
        print("You didn't select any files! Returning you to the main menu")
        return
    save_input = yes_no_loop("Do you want to export the results?")
    if save_input:
        print("Select where results should go:")
        destination = ask_directory()
        if destination is None or not os.path.isdir(destination):
            print(
                "You didn't select a destination! Returning you to the main menu"
            )
            return
    print("Identified", len(filepaths), "files for validation")
    req_train = yes_no_loop(
        "Do you wish to train and validate via cross-validation (y) or just validate (n)?"
    )
    ds, data, labels = [], [], []
    if verbose == 1: jobs = trange(len(filepaths))
    else: jobs = range(len(filepaths))
    for i in jobs:
        if verbose > 1: print("Reading", filepaths[i])
        d, l = model.read(filepaths[i], labels=True)
        name = Path(filepaths[i]).stem
        ds_ = model.process(d, l, name)
        ds.append(ds_)
        data.append(ds_.data)
        labels.append(ds_.labels)
    if req_train:
        p, Y_hat = model.cross_validate(data, labels)
    else:
        p, Y_hat = model.predict(data)
    if p[0].ndim != 1:
        p_ = []
        start = 0
        for i in range(len(data)):
            p_.append(p[start:start + len(data[i])])
            start = start + len(data[i])
        p = p_
    score = model.score(p, labels)
    print("Overall Score:", score)
    p_overall = np.concatenate(p)
    labels_overall = np.concatenate(labels)
    print_report(p_overall.reshape(-1), labels_overall.reshape(-1))
    if save_input:
        if verbose > 1: print("Writing results")
        for i in range(len(p)):
            r = np.concatenate((Y_hat[i], p[i].reshape(-1, 1)), axis=1)
            result = Dataset(label_names=['AW', 'QW', 'NR', 'R', 'P'],
                             labels=r)
            result = ds[i].concatenate(result)
            result.name = ds[i].name + "-validated-" + "%.4f" % score[i]
            result.write(destination)
    print("Completed validation")