def test_get_signal_with_meta(self, setup_module_load): #pylint: disable=redefined-outer-name """ Testing get_signal_meta. """ # Arrange ppln = (bf.Pipeline().init_variable( name="signal", init_on_each_run=list).load( fmt='wfdb', components=["signal", "meta"]).flip_signals().update_variable( "signal", bf.B("signal"), mode='a').run(batch_size=2, shuffle=False, drop_last=False, n_epochs=1, lazy=True)) dtst = EcgDataset(setup_module_load[0]) # Act ppln_run = (dtst >> ppln).run() signal_var = ppln_run.get_variable("signal") # Assert assert len(signal_var) == 3 assert len(signal_var[0]) == 2 assert signal_var[0][0].shape == (1, 9000)
def predict_and_visualize(signal_path): predict_eds = EcgDataset(path=signal_path, no_ext=True, sort=True) full_predict_ppl = (predict_eds >> template_full_predict_ppl).run() signal = full_predict_ppl.get_variable("signals")[0][0][0][:2000].ravel() predictions = full_predict_ppl.get_variable("predictions_list")[0] parameters = full_predict_ppl.get_variable("parameters_list")[0] print(predictions) x = np.linspace(0.001, 0.999, 1000) y = np.zeros_like(x) for alpha in parameters: y += beta.pdf(x, *alpha) y /= len(parameters) fig, (ax1, ax2) = plt.subplots(1, 2, gridspec_kw={"width_ratios": [2.5, 1]}, figsize=(15, 4)) ax1.plot(signal) ax2.plot(x, y) ax2.fill_between(x, y, alpha=0.3) ax2.set_ylim(ymin=0)
def setup_class_dataset(request): """ Fixture to setup class to test EcgBatch methods in pipeline. """ path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/') ecg_dataset = EcgDataset(path=os.path.join(path, 'A*.hea'), no_ext=True, sort=True) def teardown_class_dataset(): """ Teardown class """ request.addfinalizer(teardown_class_dataset) return ecg_dataset
def setup_class_pipeline(request): """ Fixture to setup class to test EcgBatch methods in pipeline. """ path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data/') target_path = os.path.join(path, "REFERENCE.csv") ecg_pipeline = (EcgDataset(path=os.path.join(path, 'A*.hea'), no_ext=True, sort=True) .p .load(src=None, fmt="wfdb", components=["signal", "annotation", "meta"]) .load(src=target_path, fmt="csv", components=["target"])) def teardown_class_pipeline(): """ Teardown class """ request.addfinalizer(teardown_class_pipeline) return ecg_pipeline
import sys import os sys.path.append("..") import cardio.dataset as ds from cardio.dataset import B from cardio import EcgDataset filter_pipeline = (ds.Pipeline().load( fmt="wfdb", components=["signal", "meta"]).band_pass_signals(low=5, high=40)) PATH_TO_DATA = "C:/training2017" eds = EcgDataset(path=os.path.join(PATH_TO_DATA, "A*.hea"), no_ext=True, sort=True) (eds >> filter_pipeline).run(batch_size=len(eds), n_epochs=1) ## at this point the filtered ech is gone, batches are destroyed within Pipeline ## use pipeline variables to store data that will be used later filter_pipeline = (ds.Pipeline().init_variable( 'saved_batches', init_on_each_run=list).load(fmt="wfdb", components=[ "signal", "meta" ]).update_variable('saved_batches', B(), mode='a').band_pass_signals( low=5, high=40).update_variable('saved_batches', B(), mode='a')) filter_pipeline = (eds >> filter_pipeline).run(batch_size=len(eds), n_epochs=1) raw_batch, filtered_batch = filter_pipeline.get_variable('saved_batches') #raw_batch.show_ecg('A00001', start=10, end=15) #filtered_batch.show_ecg('A00001', start=10, end=15)
"session": { "config": tf.ConfigProto(gpu_options=gpu_options) }, "build": False, "load": { "path": MODEL_PATH }, } template_predict_ppl = (ds.Pipeline().init_model( "static", DirichletModel, name="dirichlet", config=model_config).init_variable( "predictions_list", init_on_each_run=list).load( fmt="wfdb", components=["signal", "meta"]).flip_signals().split_signals( 2048, 2048).predict_model("dirichlet", make_data=partial(concatenate_ecg_batch, return_targets=False), fetches="predictions", save_to=V("predictions_list"), mode="e").run(batch_size=BATCH_SIZE, shuffle=False, drop_last=False, n_epochs=1, lazy=True)) predict_eds = EcgDataset(path=signal_path, no_ext=True, sort=True) predict_ppl = (predict_eds >> template_predict_ppl).run() print(predict_ppl.get_variable("predictions_list"))
import sys import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tensorflow as tf from cardio import EcgDataset #from cardio.dataset import B, V, F from cardio.models.dirichlet_model import DirichletModel, concatenate_ecg_batch from cardio.pipelines import dirichlet_predict_pipeline signal_path = sys.argv[1] BASE_DIR = os.path.abspath(os.path.dirname(os.getcwd())) MODEL_PATH = 'model' BATCH_SIZE = 100 gpu_options = tf.GPUOptions(allow_growth=True) template_predict_ppl = dirichlet_predict_pipeline(model_path=MODEL_PATH, batch_size=BATCH_SIZE, gpu_options=gpu_options ) predict_eds = EcgDataset(path=signal_path) predict_ppl = (predict_eds >> template_predict_ppl).run() print(str(predict_ppl.get_variable("predictions_list")[0]).replace("'", "\""))
import sys, os sys.path.append('..') from cardio import EcgDataset from cardio.pipelines import hmm_preprocessing_pipeline, hmm_train_pipeline import warnings warnings.filterwarnings('ignore') SIGNALS_PATH = "C:/training2017/ECG/" SIGNALS_MASK = os.path.join(SIGNALS_PATH, "*.hea") dataset = EcgDataset(path=SIGNALS_MASK, no_ext=True, sort=True) print(SIGNALS_MASK) pipeline = hmm_preprocessing_pipeline() ppl_inits = (dataset >> pipeline).run() pipeline = hmm_train_pipeline(ppl_inits) ppl_train = (dataset >> pipeline).run() ppl_train.save_model("HMM", path="model_dump.dll") eds = EcgDataset(path="C:/training2017/A*.hea", no_ext=True, sort=True) batch = (eds >> hmm_predict_pipeline( "model_dump.dll", annot="hmm_annotation")).next_batch(batch_size=3) batch.show_ecg("A00001", start=10, end=15, annot="hmm_annotation")
import os import tensorflow from cardio import EcgDataset import pandas as pd PATH_TO_DATA = "C:/training2017" pds = EcgDataset(path=os.path.join(PATH_TO_DATA, "*.hea"), no_ext=True, sort=True) pds.cv_split(0.8, shuffle=True) from cardio.pipelines import dirichlet_train_pipeline os.environ["CUDA_VISIBLE_DEVICES"] = "0" AF_SIGNALS_REF = os.path.join(PATH_TO_DATA, "REFERENCE.csv") pipeline = dirichlet_train_pipeline(AF_SIGNALS_REF, batch_size=256, n_epochs=500) trained = (pds.train >> pipeline).run() model_path = "af_model_dump" trained.save_model("dirichlet", path=model_path) pipeline = dirichlet_predict_pipeline(model_path) res = (pds.test >> pipeline).run() prediction = res.get_variable("predictions_list") pd.options.display.float_format = '{:.2f}'.format
sys.path.append(os.path.join("..", "..", "..")) import cardio.dataset as ds from cardio import EcgDataset from cardio.dataset import B, V, F from cardio.models.dirichlet_model import DirichletModel, concatenate_ecg_batch from cardio.models.metrics import f1_score, classification_report, confusion_matrix sns.set("talk") os.environ["CUDA_VISIBLE_DEVICES"] = "0" SIGNALS_PATH = "C:\\training2017\\" SIGNALS_MASK = SIGNALS_PATH + "*.hea" LABELS_PATH = SIGNALS_PATH + "REFERENCE.csv" eds = EcgDataset(path=SIGNALS_MASK, no_ext=True, sort=True) eds.cv_split(0.8) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5, allow_growth=True) model_config = { "session": { "config": tf.ConfigProto(gpu_options=gpu_options) }, "input_shape": F(lambda batch: batch.signal[0].shape[1:]), "class_names": F(lambda batch: batch.label_binarizer.classes_), "loss": None, } N_EPOCH = 200