def test_shhs_200002(self):
        path = 'D:\\GitRepositories\\DSDM\\Period1\\github_ecg\\db_shhs\\edfs\\shhs1\\shhs1-200002.edf'
        ecg,_,_ = ap.SHHS_ReadDataset(path, 100)

        sample_length = 6000
        num_samp = int(np.ceil(len(ecg) / sample_length))

        result = []
        for j in range(num_samp):
            l = j * sample_length
            h = (j + 1) * sample_length

            if j == (num_samp - 1):
                break

            sample = ecg[l:h]

            validity = ap.isValidECGChunk(sample)
            result.append(validity)

        expected = np.array([True] * num_samp)

        invalid_elements = [56, 57, 59, 60, 81, 82, 85, 155, 156, 159, 160, 198, 202, 205, 225, 226, 229, 230, 231, 232]
        for i in invalid_elements:
            expected[i] = False
        expected[232:num_samp-1] = False # rest of array is invalid
        for i in range(expected.size):
            print (str(i) + ': ' + str(expected[i]))

        with open('DetectInvalildSignal_test_shhs_200002.txt', mode='w+',
                  newline='') as csv_file:
            fieldnames = \
                ['expected',
                 'result',
                 ]
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter='\t')
            for i in range(expected.size-1):
                writer.writerow({'expected': expected[i],
                                 'result': result[i],
                                 })
            number_errors = 0
            for i in range(expected.size - 1):
                if expected[i] != result[i]:
                    number_errors += 1
            writer.writerow({'expected': 'test_shhs_200002: '+ 'error_num=' + str(number_errors) + ', error[%]='+ str((number_errors/num_samp)*100),
                             'result': "",
                             })

        self.assertEqual(result, expected)
    def test_NoOverlaps_5Hz_To_1Hz_DontPreserveSize(self):
        input_apnea_signal = [];
        expected_apnea_signal = [];

        # create input signal (in 5 element blocks (5Hz) for better visibility)
        input_apnea_signal.extend([0, 0, 0, 0, 0]);
        input_apnea_signal.extend([1, 0, 0, 0, 0]);
        input_apnea_signal.extend([0, 1, 0, 0, 0]);
        input_apnea_signal.extend([0, 0, 0, 0, 0]);
        input_apnea_signal.extend([0, 0, 1, 0, 0]);
        input_apnea_signal.extend([0, 0, 0, 1, 0]);
        input_apnea_signal.extend([0, 0, 0, 0, 1]);
        input_apnea_signal.extend([0, 0, 0, 0, 0]);
        input_apnea_signal.extend([1, 1, 1, 1, 1]);
        input_apnea_signal.extend([0, 0, 0, 0, 0]);
        input_apnea_signal.extend([0, 1, 1, 1, 0]);
        input_apnea_signal.extend([1, 0, 1, 0, 1]);

        # create expected signal (in 1 element blocks (1Hz) for better visibility)
        expected_apnea_signal.extend([0]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([0]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([0]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([0]);
        expected_apnea_signal.extend([1]);
        expected_apnea_signal.extend([1]);

        output_apnea_signal = ap.ResampleAnnotations(input_apnea_signal, 5, 1, False, False, False);

        self.assertEqual(expected_apnea_signal, output_apnea_signal)
 def test_NoOverlapInterval_1(self):
     input = [0, 1, 1, 1, 0];
     expected_output = False;
     output = ap.IsFirstApneaOverlapInterval(input);
     self.assertEqual(expected_output, output);
 def test_OverlapInterval_4(self):
     input = [0, 1, 1, 1, 1];
     expected_output = True;
     output = ap.IsFirstApneaOverlapInterval(input);
     self.assertEqual(expected_output, output);
Example #5
0
def UCDDB_Functions():
    path = 'datasets\\db3_ucddb\\ucddb002_respevt.txt';

    apnea_signals = ap.UCDDB_LoadAnnonationsTXTFileRaw(path);
    print(apnea_signals);

    start_time = datetime.strptime('00:11:04', '%H:%M:%S')
    annotations_std = ap.UCDDB_LoadAnnonationsTXTFileStandardized(path, start_time=start_time, duration_in_seconds=7.65*60*60);
    print(annotations_std)

    annotations_std_binary = [not(type=='none') for type in annotations_std]

    resampled = ap.ResampleAnnotations(
        annotations=annotations_std_binary,
        source_sample_frequency=1,
        target_sample_frequency=(1 / 60),
        preserve_input_size=False,
        ignore_first_timeframe_during_overlap=False,
        ignore_short_apnea_in_timeframe=False)

    resampled_full_size = ap.ResampleAnnotations(
        annotations=annotations_std_binary,
        source_sample_frequency=1,
        target_sample_frequency=(1 / 60),
        preserve_input_size=True,
        ignore_first_timeframe_during_overlap=False,
        ignore_short_apnea_in_timeframe=False)

    resampled_full_size_IgnoreFirstOverlap = ap.ResampleAnnotations(
        annotations=annotations_std_binary,
        source_sample_frequency=1,
        target_sample_frequency=(1 / 60),
        preserve_input_size=True,
        ignore_first_timeframe_during_overlap=True,
        ignore_short_apnea_in_timeframe=False)

    plt.plot(resampled)

    apn_symbols = list()
    for element in resampled:
        symbol = 'N'
        if element == 1:
            symbol = 'A'
        apn_symbols.append(symbol)

    resampled_1Min = [element*60*128 for element in range(len(apn_symbols))]

    print('Write annotation file')
    print(resampled_1Min)
    print(apn_symbols)
    wfdb.wrann('ucddb002', 'apn', np.array(resampled_1Min), np.array(apn_symbols))

    with open('datasets\\db3_ucddb\\AnnotationsResampled\\ucddb002_AnnotationsInfo.txt', mode='w', newline='') as csv_file:
        fieldnames = \
            ['Sample',
             'DateTime',
             'Apnea yes/no',
             'Apnea Type',
             'Apnea yes/no Resampled',
             'Apnea yes/no Resampled Ignore First Overlap'
             ]
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter='\t')

        writer.writeheader()

        for i in range(len(annotations_std)):
            writer.writerow({'Sample': i,
                             'DateTime': (start_time + timedelta(seconds=i)),
                             'Apnea yes/no': annotations_std_binary[i],
                             'Apnea Type': annotations_std[i],
                             'Apnea yes/no Resampled': resampled_full_size[i],
                             'Apnea yes/no Resampled Ignore First Overlap': resampled_full_size_IgnoreFirstOverlap[i]
                             })

    return
Example #6
0
#    target_file_postfix='_Shorter_IgnoreOverlap',
#    source_file_portfix='_lifecard_100Hz',
#    preserve_input_size=False,
#    ignore_first_timeframe_during_overlap=True,
#    create_annotationfiles_as_ascii=False,
#    print_log=True
#)

#datasets = sorted(glob.glob('D:\\GitRepositories\\DSDM\\Period1\\github_ecg\\db_shhs\\edfs\\shhs1\\*.edf'))
#ap.SHHS_CreateRepresentationChunks(
#    paths_datasets=datasets,
#    dir_target ='D:\\GitRepositories\\DSDM\\Period1\\github_ecg\\db_shhs\\edfs\\shhs1\\Scalograms',
#    database=2,
#    target_freq=100,
#    sample_seconds=60,
#    print_log=True
#)


datasets = sorted(glob.glob('D:\GitRepositories\DSDM\Period1\github_ecg\db3_ucddb\Resampled\Record100Hz\\*.dat'))

ap.SHHS_CreateRepresentationChunks(
    paths_datasets=datasets,
    dir_target ='D:\GitRepositories\DSDM\Period1\github_ecg\db3_ucddb\Resampled\Record100Hz\\Scalograms',
    database=3,
    target_freq=100,
    sample_seconds=60,
    print_log=True
)

#GetApneaOccurencesApneaECG()
# DON'T CHANGE!
target_freq = 100
sample_seconds = 60

datasets = sorted(
    glob.glob(
        'D:\\GitRepositories\\DSDM\\Period1\\github_ecg\\db_shhs\\edfs\\shhs1\\*.edf'
    ))
#for i in range(6):
#    del datasets[0]
# use if True/False to activate/deactivate the code block without the need to comment/uncomment
if True:
    ap.createRepresentationChunks(
        paths_datasets=datasets,
        dir_target=
        'D:\\GitRepositories\\DSDM\\Period1\\github_ecg\\db_shhs\\edfs\\shhs1\\Scalograms1To125_BaselineCorrection',
        database=2,
        target_freq=target_freq,
        sample_seconds=sample_seconds,
        create_additional_info=True)

# its assumed that the .dat, .hea and .apn files are in the same directory
# the apnea files have the postfix '_Shorter_IgnoreOverlap'
# f.ex.: ucddb002_lifecard_100Hz.dat, ucddb002_lifecard_100Hz.hea,  ucddb002_lifecard_100Hz_Shorter_IgnoreOverlap.apn

if False:
    datasets = sorted(
        glob.glob(
            'D:\GitRepositories\DSDM\Period1\github_ecg\db3_ucddb\Resampled\Record100Hz\\*.dat'
        ))
    ap.createRepresentationChunks(
        paths_datasets=datasets,