Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Allow custom labels in field2bytes #404

Merged
merged 2 commits into from
Jul 14, 2022

Conversation

birm
Copy link
Contributor

@birm birm commented Jun 30, 2022

Fixes #399

Take custom labels into account in encoding, should they exist by temporarily creating a larger version of ann_label_table including custom labels.

@birm birm force-pushed the custom_label_field2bytes branch from 4fcf271 to 99a4737 Compare July 5, 2022 19:07
Copy link
Member

@cx1111 cx1111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, thank you.

Could you please add a test case?

@cx1111
Copy link
Member

cx1111 commented Jul 5, 2022

Also, I'm curious to know: why do you want to write WFDB annotation files?

Trying to get a gauge of what's useful and where to focus future efforts, thanks.

@nanli-emory
Copy link

Also, I'm curious to know: why do you want to write WFDB annotation files?

Trying to get a gauge of what's useful and where to focus future efforts, thanks.

We were trying to write the custom beat labels into WFDB annotation format.

import wfdb
import numpy as np
import pandas as pd
ann_idx = np.array([0, 1000, 2000, 3000, 4000, 5000, 6000, 7000])
ann_label_store = np.array([ 4, 4, 1, 2, 2, 3, 2, 3])
ann_chan = np.array([3, 1, 2, 3, 4, 5, 6, 7])
ann_custom_labels = {'label_store': [1, 2, 3, 4], 'symbol': ['v', 'l', 'r', 'z'], 'description': ['pvc', 'lbbb', 'rbbb', 'pac']}
ann_custom_labels = pd.DataFrame(data=ann_custom_labels)
wfdb.wrann('A0001', 'atr', ann_idx, chan=ann_chan, label_store=ann_label_store,custom_labels=ann_custom_labels)

However, there is an error as follows.

Traceback (most recent call last):
  File "test.py", line 10, in <module>
    wfdb.wrann('A0001', 'atr', ann_idx, chan=ann_chan, label_store=ann_label_store,custom_labels=ann_custom_labels)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wfdb/io/annotation.py", line 1525, in wrann
    annotation.wrann(write_fs=True, write_dir=write_dir)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wfdb/io/annotation.py", line 292, in wrann
    self.wr_ann_file(write_fs=write_fs, write_dir=write_dir)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wfdb/io/annotation.py", line 798, in wr_ann_file
    core_bytes = self.calc_core_bytes()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wfdb/io/annotation.py", line 937, in calc_core_bytes
    data_bytes.append(field2bytes('samptype', [sampdiff[i], self.symbol[i]]))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/wfdb/io/annotation.py", line 1369, in field2bytes
    typecode = ann_label_table.loc[ann_label_table['symbol']==value[1], 'label_store'].values[0]
IndexError: index 0 is out of bounds for axis 0 with size 0

After we checked the code and found out the python code never use custom_labels to store the symbol/labels.

So my colleague tries to fix the bug that allows storing custom labels in WFDB annotation format.

The test code that we use:

import wfdb
import numpy as np
import pandas as pd

ann_idx = np.array([1, 1000, 2000, 3000, 4000, 5000, 6000, 7000])
ann_chan = np.array([3, 1, 2, 3, 4, 5, 6, 7])
# write custom labels
ann_label_store = np.array([ 4, 4, 1, 2, 2, 3, 2, 3])
ann_custom_labels = {'label_store': [1, 2, 3, 4], 'symbol': ['v','l','r','z'], 'description':['pvc','lbbb','rbbb','pac']}
ann_custom_labels = pd.DataFrame(data=ann_custom_labels)
wfdb.wrann('A0001', 'atr', ann_idx, chan=ann_chan, custom_labels=ann_custom_labels, label_store=ann_label_store)

# read
ann = wfdb.rdann('A0001', 'atr')
print(ann.sample)
print(ann.custom_labels)
print(ann.symbol)
print(ann.label_store)
print(ann.chan)

After fixed the result:

[   1 1000 2000 3000 4000 5000 6000 7000]
   label_store symbol description
1            1      v         pvc
2            2      l        lbbb
3            3      r        rbbb
4            4      z         pac
['z', 'z', 'v', 'l', 'l', 'r', 'l', 'r']
None
[3 1 2 3 4 5 6 7]

@birm
Copy link
Contributor Author

birm commented Jul 12, 2022

Thank you. I've added a test case to the pytest suite in f01a151. Please let me know if this needs some code style changes, or anything else.

Copy link
Member

@cx1111 cx1111 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@cx1111 cx1111 merged commit 3c30345 into MIT-LCP:main Jul 14, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to use wfdb.wrann with custom_labels and label_store to write the custom labels?
3 participants