Esempio n. 1
0
    def test_fortran_write(self):
        # Create Fortran contiguous array
        signals = np.random.randint(-2048, 2048, [4, 5000000])
        signals = np.asfortranarray(signals)
        # Write
        highlevel.write_edf_quick(self.edfplus_data_file,
                                  signals.astype(np.int32),
                                  sfreq=250,
                                  digital=True)
        # Read and check
        signals2, _, _ = highlevel.read_edf(self.edfplus_data_file,
                                            digital=True,
                                            verbose=True)
        np.testing.assert_allclose(signals, signals2)

        # Create Fortran contiguous list
        signals = [np.random.randint(-2048, 2048, (5000000, ))] * 4
        # Write
        highlevel.write_edf_quick(self.edfplus_data_file,
                                  signals,
                                  sfreq=250,
                                  digital=True)
        # Read and check
        signals2, _, _ = highlevel.read_edf(self.edfplus_data_file,
                                            digital=True,
                                            verbose=True)
        np.testing.assert_allclose(signals, signals2)
Esempio n. 2
0
 def test_read_write_accented(self):
     signals = np.random.rand(3, 256*60)
     highlevel.write_edf_quick(self.test_accented, signals, sfreq=256)
     signals2, _, _ = highlevel.read_edf(self.test_accented)
     
     np.testing.assert_allclose(signals, signals2, atol=0.00002)
     # if os.name!='nt':
     self.assertTrue(os.path.isfile(self.test_accented), 'File does not exist')
Esempio n. 3
0
 def test_quick_write(self):
     signals = np.random.randint(-2048, 2048, [3, 256*60])
     highlevel.write_edf_quick(self.edfplus_data_file, signals.astype(np.int32), sfreq=256, digital=True)
     signals2, _, _ = highlevel.read_edf(self.edfplus_data_file, digital=True)
     np.testing.assert_allclose(signals, signals2)
     signals = np.random.rand(3, 256*60)
     highlevel.write_edf_quick(self.edfplus_data_file, signals, sfreq=256)
     signals2, _, _ = highlevel.read_edf(self.edfplus_data_file)
     np.testing.assert_allclose(signals, signals2, atol=0.00002)
Esempio n. 4
0
 def test_read_unicode(self):
     signals = np.random.rand(3, 256*60)
     success = highlevel.write_edf_quick(self.edfplus_data_file, signals, sfreq=256)
     self.assertTrue(success)
     shutil.copy(self.edfplus_data_file, self.test_unicode)
     signals2, _, _ = highlevel.read_edf(self.test_unicode)
     self.assertTrue(os.path.isfile(self.test_unicode), 'File does not exist')
Esempio n. 5
0
 def test_read_write_decimal_sample_rates(self):
     signals = np.random.randint(-2048, 2048, [3, 256 * 60])
     highlevel.write_edf_quick(self.edfplus_data_file,
                               signals.astype(np.int32),
                               sfreq=8.5,
                               digital=True)
     signals2, _, _ = highlevel.read_edf(self.edfplus_data_file,
                                         digital=True,
                                         verbose=True)
     np.testing.assert_allclose(signals, signals2)
     signals = np.random.rand(3, 256 * 60)
     highlevel.write_edf_quick(self.edfplus_data_file,
                               signals,
                               sfreq=8.5,
                               digital=False)
     signals2, _, _ = highlevel.read_edf(self.edfplus_data_file,
                                         digital=False,
                                         verbose=True)
     np.testing.assert_allclose(signals, signals2, atol=0.0001)
Esempio n. 6
0
    def test_read_write_accented(self):
        signals = np.random.rand(3, 256 * 60)
        highlevel.write_edf_quick(self.test_accented, signals, sfreq=256)
        signals2, _, _ = highlevel.read_edf(self.test_accented)

        np.testing.assert_allclose(signals, signals2, atol=0.00002)
Esempio n. 7
0
def ECG_writer(signal_sqi, file_name, file_type, info = None):
    """

    Parameters
    ----------
    signal_sqi : SignalSQI object containing signals, sampling rate and sqi
        
    file_name : name of file to write, with extension. For edf file_type,
        
    possible extensions are edf, edf+, bdf, bdf+. For mit file_type, :
        
    possible extensions are... :
        
    file_type : edf or mit or csv
        
    info : list
        In case of writing edf file: A list containing signal_headers and
        header (in
        order). signal_headers is a list of dict with one signal header for
        each signal channel. header (dict) contain ecg file information.
        In case of writing wfdb record (mit file): A dict containing header
        as defined in .hea file.
        (Default value = None)

    Returns
    -------

    
    """
    signals = signal_sqi.signals
    sampling_rate = signal_sqi.sampling_rate
    start_datetime = signal_sqi.start_datetime
    assert isinstance(sampling_rate, int) or isinstance(sampling_rate,
                                                        float), \
        'sampling rate must be either int or float'
    if file_type == 'edf':
        signals = signals.transpose()
        if info is not None:
            signal_headers = info[1]
            header = info[0]
            annotations = header['annotations']
            # issue https://github.com/holgern/pyedflib/issues/119
            for i in range(len(annotations)):
                if isinstance(header['annotations'][i][1], bytes):
                    header['annotations'][i][1] = \
                        float(str(header['annotations'][i][1], 'utf-8'))
            highlevel.write_edf(file_name, signals, signal_headers,
                                header, file_type = -1, digital = False,
                                block_size = -1)
        else:
            highlevel.write_edf_quick(file_name, signals, sampling_rate)
        return os.path.isfile(file_name)
    if file_type == 'mit':
        if info is None:
            raise Exception("Header dict needed")
        else:
            wrsamp(record_name = file_name.split('/')[-1],
                   fs = sampling_rate,
                   units = info['units'],
                   sig_name = info['sig_name'],
                   p_signal = signals,
                   base_date = info['base_date'],
                   base_time = info['base_time'],
                   comments = info['comments'],
                   write_dir = '/'.join(file_name.split('/')[:-1]))
        return glob.glob(file_name + '.*')
    if file_type == 'csv':
        timestamps = generate_timestamp(start_datetime, sampling_rate,
                                        signals.shape[0])
        signals = pd.DataFrame(np.hstack((np.array(timestamps).reshape(-1, 1),
                                          signals)))
        signals.to_csv(path_or_buf = file_name, index = False, header = True)
        return os.path.isfile(file_name)