コード例 #1
0
def TrainingModels(target_label, model_file_name, training_list):
    '''Randomly select num_training records to train, and test others.'''
    qt = QTloader()
    record_list = qt.getreclist()
    testing_list = list(set(record_list) - set(training_list))

    random_forest_config = dict(max_depth=10)
    walker = RandomWalker(target_label=target_label,
                          random_forest_config=random_forest_config,
                          random_pattern_file_name=os.path.join(
                              os.path.dirname(model_file_name),
                              'random_pattern.json'))

    start_time = time.time()
    for record_name in training_list:
        print 'Collecting features from record %s.' % record_name
        sig = qt.load(record_name)
        walker.collect_training_data(sig['sig'], qt.getExpert(record_name))
    print 'random forest start training(%s)...' % target_label
    walker.training()
    print 'trianing used %.3f seconds' % (time.time() - start_time)

    import joblib
    start_time = time.time()
    walker.save_model(model_file_name)
    print 'Serializing model time cost %f' % (time.time() - start_time)
コード例 #2
0
def TrainingModels(target_label, model_file_name, training_list):
    '''Randomly select num_training records to train, and test others.
    CP: Characteristic points
    '''
    qt = QTloader()
    record_list = qt.getreclist()
    testing_list = list(set(record_list) - set(training_list))

    random_forest_config = dict(max_depth=10)
    walker = RandomWalker(target_label=target_label,
                          random_forest_config=random_forest_config,
                          random_pattern_file_name=os.path.join(
                              os.path.dirname(model_file_name),
                              'random_pattern.json'))

    start_time = time.time()
    for record_name in training_list:
        CP_file_name = os.path.join(
            '/home/alex/code/Python/EcgCharacterPointMarks', target_label,
            '%s_poslist.json' % record_name)

        # Add expert marks
        expert_marks = qt.getExpert(record_name)
        CP_marks = [x for x in expert_marks if x[1] == target_label]
        if len(CP_marks) == 0:
            continue

        # Add manual labels if possible
        if os.path.exists(CP_file_name) == True:
            with open(CP_file_name, 'r') as fin:
                CP_info = json.load(fin)
                poslist = CP_info['poslist']
                if len(poslist) == 0:
                    continue
                CP_marks.extend(zip(poslist, [
                    target_label,
                ] * len(poslist)))

        print 'Collecting features from record %s.' % record_name
        sig = qt.load(record_name)
        walker.collect_training_data(sig['sig'], CP_marks)
    print 'random forest start training(%s)...' % target_label
    walker.training()
    print 'trianing used %.3f seconds' % (time.time() - start_time)

    import joblib
    start_time = time.time()
    walker.save_model(model_file_name)
    print 'Serializing model time cost %f' % (time.time() - start_time)
コード例 #3
0
def TrainingModels_Changgeng(target_label, model_file_name):
    '''Randomly select num_training records to train, and test others.
    CP: Characteristic points
    '''
    from changgengLoader import ECGLoader
    ecg = ECGLoader(500, current_folderpath)

    random_forest_config = dict(max_depth=10)
    walker = RandomWalker(target_label=target_label,
                          random_forest_config=random_forest_config,
                          random_pattern_file_name=os.path.join(
                              os.path.dirname(model_file_name),
                              'random_pattern.json'))

    start_time = time.time()

    for record_ind in xrange(0, len(ecg.P_faillist)):
        record_name = ecg.P_faillist[record_ind]
        CP_file_name = os.path.join(current_folderpath, 'data', 'labels',
                                    target_label,
                                    '%s_poslist.json' % record_name)

        CP_marks = []

        # Add manual labels if possible
        if os.path.exists(CP_file_name) == True:
            with open(CP_file_name, 'r') as fin:
                print 'Collecting features from record %s.' % record_name
                CP_info = json.load(fin)
                poslist = CP_info['poslist']
                if len(poslist) == 0:
                    continue
                CP_marks.extend(zip(poslist, [
                    target_label,
                ] * len(poslist)))

        sig = ecg.load(record_ind)

        walker.collect_training_data(sig[0], CP_marks)
    print 'random forest start training(%s)...' % target_label
    walker.training()
    print 'trianing used %.3f seconds' % (time.time() - start_time)

    import joblib
    start_time = time.time()
    walker.save_model(model_file_name)
    print 'Serializing model time cost %f' % (time.time() - start_time)
コード例 #4
0
def TrainingModels(target_label, model_file_name, training_list):
    '''Randomly select num_training records to train, and test others.'''
    qt = QTloader()
    record_list = qt.getreclist()
    testing_list = list(set(record_list) - set(training_list))

    random_forest_config = dict(max_depth=10)
    walker = RandomWalker(target_label=target_label,
                          random_forest_config=random_forest_config,
                          random_pattern_file_name=os.path.join(
                              os.path.dirname(model_file_name),
                              'random_pattern.json'))

    start_time = time.time()
    for record_name in training_list:
        Tonset_file_name = os.path.join(
            '/home/alex/code/Python/Tonset/results',
            '%s_poslist.json' % record_name)
        if os.path.exists(Tonset_file_name) == True:
            with open(Tonset_file_name, 'r') as fin:
                Tonset_info = json.load(fin)
                poslist = Tonset_info['poslist']
                if len(poslist) == 0:
                    continue
                Tonset_marks = zip(poslist, [
                    'Tonset',
                ] * len(poslist))
        else:
            expert_marks = qt.getExpert(record_name)
            Tonset_marks = [x for x in expert_marks if x[1] == 'Tonset']
            if len(Tonset_marks) == 0:
                continue

        print 'Collecting features from record %s.' % record_name
        sig = qt.load(record_name)
        walker.collect_training_data(sig['sig'], Tonset_marks)
    print 'random forest start training(%s)...' % target_label
    walker.training()
    print 'trianing used %.3f seconds' % (time.time() - start_time)

    import joblib
    start_time = time.time()
    walker.save_model(model_file_name)
    print 'Serializing model time cost %f' % (time.time() - start_time)
コード例 #5
0
def TrainingModels_Changgeng(target_label, model_file_name):
    '''Randomly select num_training records to train, and test others.
    CP: Characteristic points
    '''

    import glob
    annot_jsonIDs = glob.glob(
        os.path.join(current_folderpath, 'data', 'labels', target_label,
                     '*.json'))
    annot_jsonIDs = [os.path.split(x)[-1] for x in annot_jsonIDs]
    annot_jsonIDs = [x.split('.')[0] for x in annot_jsonIDs]
    # skip failed records
    faillist = [
        8999, 8374, 6659, 6655, 6059, 5395, 1401, 1269, 737, 75, 9524, 9476
    ]
    faillist = [str(x) for x in faillist]
    annot_jsonIDs = list(set(annot_jsonIDs) - set(faillist))

    from changgengLoader import ECGLoader
    ecg = ECGLoader(500, current_folderpath)

    random_forest_config = dict(max_depth=10)
    walker = RandomWalker(target_label=target_label,
                          random_forest_config=random_forest_config,
                          random_pattern_file_name=os.path.join(
                              os.path.dirname(model_file_name),
                              'random_pattern.json'))

    start_time = time.time()

    for record_ind in xrange(0, len(annot_jsonIDs)):
        record_name = annot_jsonIDs[record_ind]
        CP_file_name = os.path.join(current_folderpath, 'data', 'labels',
                                    target_label, '%s.json' % record_name)

        CP_marks = []

        # Add manual labels if possible
        if os.path.exists(CP_file_name) == True:
            with open(CP_file_name, 'r') as fin:
                CP_info = json.load(fin)
                poslist = CP_info['poslist']
                poslist = [int(x / 2) for x in poslist]
                mat_file_name = CP_info['mat_file_name']
                if len(poslist) == 0:
                    continue
                CP_marks.extend(zip(poslist, [
                    target_label,
                ] * len(poslist)))

        print 'Collecting features from record %s.' % record_name
        sig = ecg.load(record_name)
        raw_sig = sig[0]

        import scipy.signal
        resampled_sig = scipy.signal.resample_poly(raw_sig, 1, 2)
        raw_sig = resampled_sig
        # debug
        # plt.figure(1)
        # plt.plot(raw_sig, label = 'signal')
        # plt.plot(xrange(0, len(raw_sig), 2), resampled_sig, label = 'resmaple')
        # plt.legend()
        # plt.grid(True)
        # plt.title(record_name)
        # plt.show()

        walker.collect_training_data(raw_sig, CP_marks)

    # Add QT training samples
    # ContinueAddQtTrainingSamples(walker, target_label)

    print 'random forest start training(%s)...' % target_label
    walker.training()
    print 'trianing used %.3f seconds' % (time.time() - start_time)

    import joblib
    start_time = time.time()
    walker.save_model(model_file_name)
    print 'Serializing model time cost %f' % (time.time() - start_time)