analysiscenter / cardio

CardIO is a library for data science research of heart signals
https://analysiscenter.github.io/cardio/
Apache License 2.0
248 stars 78 forks source link

Question about how to get the annotation prediction from pipeline #15

Closed Yang-LONG closed 6 years ago

Yang-LONG commented 6 years ago

Hello, I wonder how I can get the annotation prediction of the segmentation from the following code?

batch = (eds >> hmm_predict_pipeline("model_dump.dll", annot="hmm_annotation")).next_batch()
batch.show_ecg('A00001',start=0, end=5, annot="hmm_annotation")

The image can show them but I can't get them.

eds = EcgDataset(path='/home/long/cardio/cardio/tests/data/A*.hea',no_ext=True,sort=True)
print(eds.indices)
print('len(eds.indices)=',len(eds.indices))
from cardio.pipelines import hmm_predict_pipeline
print(type(hmm_predict_pipeline("model_dump.dll", annot="hmm_annotation")))
batch = (eds >> hmm_predict_pipeline("model_dump.dll", annot="hmm_annotation"))
print(type(batch))
batch = batch.next_batch()
batch.show_ecg('A00001',start=0, end=5, annot="hmm_annotation")

result:

['A00001' 'A00002' 'A00004' 'A00005' 'A00008' 'A00013']
len(eds.indices)= 6
<class 'cardio.dataset.dataset.pipeline.Pipeline'>
<class 'cardio.dataset.dataset.pipeline.Pipeline'>
#(image here)

I've tried from the 'show_ecg' source code:

import numpy as np
print(np.shape(batch.signal))
import cardio.core.ecg_batch_tools as bt
start = 0
end = 5
annot="hmm_annotation"
i = batch.get_pos(None,"hmm_annotation",'A00001')
print(i)
meta = batch.meta[i]
fs = meta["fs"]
print(fs)
signal_states = getattr(batch,annot)[i][start:end]
print(signal_states)
starts, ends = bt.find_intervals_borders(signal_states,bt.QRS_STATES)
print(starts,ends)
print((starts+start)/fs,(ends+start)/fs)

but I got strange result:

(6,)
0
300
[2 2 2 2 2]
[] []
[] []

Please help, thanks a lot!

alexanderkuvaev commented 6 years ago

Hi! The predicted annotations are stored in the batch component, specified in the annot argument of the hmm_predict_pipeline function. It is hmm_annotation in your case:

ECG_MASK = "../cardio/tests/data/A*.hea"  # specify a path to your signals here
MODEL_PATH = "./hmm_model.dill"  # specify a path to your model here

eds = EcgDataset(path=ECG_MASK, no_ext=True, sort=True)
pipeline = eds >> hmm_predict_pipeline(MODEL_PATH, annot="hmm_annotation")
batch = pipeline.next_batch()
batch.show_ecg("A00001", start=0, end=5, annot="hmm_annotation")
print(batch.hmm_annotation)

These printed annotations match the lengths of the corresponding input signals and contain predicted HMM states. Additionally, the pipeline calculates median lengths of PQ, QT and QRS intervals along with their borders and the heart rate value based on these annotations and stores them in the meta component:

META_KEYS = ["hr", "pq", "qt", "qrs", "qrs_segments", "p_segments", "t_segments"]
signal_meta = batch["A00001"].meta
print({key: signal_meta[key] for key in META_KEYS})
Yang-LONG commented 6 years ago

Yeah, it's solved! Thank you soooo much!:)