MIT-LCP / wfdb-python

Native Python WFDB package
MIT License
730 stars 298 forks source link

Convert the dataset in hdf5 format to hea format #462

Open rdyan0053 opened 1 year ago

rdyan0053 commented 1 year ago

Hi, I want to convert the ECG dataset, code 15% into hea format:

wfdb.wrsamp(record_name=filename, fs=fs,
                units=['mV', 'mV', 'mV', 'mV', 'mV', 'mV', 'mV', 'mV', 'mV', 'mV', 'mV', 'mV'],
                sig_name=['I', 'II', 'III', 'aVR', 'aVL', 'aVF', 'V1', 'V2', 'V3', 'V4', 'V5', 'V6'], p_signal=data,
                write_dir=write_dir)

But, the dtype of data ndarray: {4096, 12} in code 15% is float32 and raise IndexError: IndexError: Channel 1 contain values outside allowed range [-2147483648, 2147483647] for fmt 32.

What should I do for the error?

Div12345 commented 3 months ago

Hi,

I think I've encountered a similar issue.

This is the error I get -

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[5], line 33
     30 print(f"Sampling Frequency: {fs}")
     32 # Write the record
---> 33 wrsamp(record_name=base_name, fs=fs, units=units, sig_name=sig_name, p_signal=p_signal, write_dir=record_dir)
     35 # Handling annotations if available
     36 if 'markers' in loaded_data:

File [~\.conda\envs\wfdb_test\Lib\site-packages\wfdb\io\record.py:2968](http://localhost:8888/lab/tree/wfdb_files/~/.conda/envs/wfdb_test/Lib/site-packages/wfdb/io/record.py#line=2967), in wrsamp(record_name, fs, units, sig_name, p_signal, d_signal, fmt, adc_gain, baseline, comments, base_time, base_date, base_datetime, write_dir)
   2966 record.set_defaults()
   2967 # Write the record files - header and associated dat
-> 2968 record.wrsamp(write_dir=write_dir)

File [~\.conda\envs\wfdb_test\Lib\site-packages\wfdb\io\record.py:939](http://localhost:8888/lab/tree/wfdb_files/~/.conda/envs/wfdb_test/Lib/site-packages/wfdb/io/record.py#line=938), in Record.wrsamp(self, expanded, write_dir)
    935 self.wrheader(write_dir=write_dir, expanded=expanded)
    936 if self.n_sig > 0:
    937     # Perform signal validity and cohesion checks, and write the
    938     # associated dat files.
--> 939     self.wr_dats(expanded=expanded, write_dir=write_dir)

File [~\.conda\envs\wfdb_test\Lib\site-packages\wfdb\io\_signal.py:158](http://localhost:8888/lab/tree/wfdb_files/~/.conda/envs/wfdb_test/Lib/site-packages/wfdb/io/_signal.py#line=157), in SignalMixin.wr_dats(self, expanded, write_dir)
    152     self.check_field("d_signal")
    154 # Check the cohesion of the d_signal field against the other
    155 # fields used to write the header.  (Note that for historical
    156 # reasons, this doesn't actually check any of the optional
    157 # header fields.)
--> 158 self.check_sig_cohesion([], expanded)
    160 # Write each of the specified dat files
    161 self.wr_dat_files(expanded=expanded, write_dir=write_dir)

File [~\.conda\envs\wfdb_test\Lib\site-packages\wfdb\io\_signal.py:264](http://localhost:8888/lab/tree/wfdb_files/~/.conda/envs/wfdb_test/Lib/site-packages/wfdb/io/_signal.py#line=263), in SignalMixin.check_sig_cohesion(self, write_fields, expanded)
    262     chmax = max(self.d_signal[:, ch])
    263     if (chmin < dmin) or (chmax > dmax):
--> 264         raise IndexError(
    265             "Channel "
    266             + str(ch)
    267             + " contain values outside allowed range ["
    268             + str(dmin)
    269             + ", "
    270             + str(dmax)
    271             + "] for fmt "
    272             + str(fmt)
    273         )
    275 # Ensure that the checksums and initial value fields match the digital signal (if the fields are present)
    276 if self.n_sig > 0:

IndexError: Channel 0 contain values outside allowed range [-2147483648, 2147483647] for fmt 32

I checked the min and max values of my p_signal and they seem to be well within the range -

max = array([-7.44786116e+01,  9.99969482e+00,  9.99969482e+00,  9.99969482e+00,
        9.99969482e+00,  9.99969482e+00,  1.86553955e+00,  1.44958496e-01,
        3.41796875e-02,  2.77832031e+00])

I would very much appreciate some guidance if something should be corrected. Is this something to do with the dtype of my array - if so I also did try type casting to float32 and it still didn't make a difference.

Thanks, Divyesh

Div12345 commented 3 months ago

Hey @tompollard , I'm trying to get a dataset to be submitted to physionet and I'm trying to get into wfdb format. I would greatly appreciate your help with this issue or if you need any more information.

Thanks, Divyesh

wzxiaobai commented 2 months ago

Hey @tompollard , I'm trying to get a dataset to be submitted to physionet and I'm trying to get into wfdb format. I would greatly appreciate your help with this issue or if you need any more information.

Thanks, Divyesh

using fmt=['16'], wfdb.wrsamp(signame, fs=fs, sig_name=channel_itos, p_signal=data, units=['mV']len(channel_itos), fmt = ['16']len(channel_itos), write_dir=sigfolder)