Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if file.endswith(".dat"):
dat_files.append(file)
mit_records = [w.replace(".dat", "") for w in dat_files]
results = np.zeros((len(mit_records), 5), dtype=int)
i = 0
for record in mit_records:
progress = int(i/float(len(mit_records))*100.0)
print("MITDB progress: %i%%" % progress)
sig, fields = wfdb.rdsamp(self.mitdb_dir+'/'+record)
unfiltered_ecg = sig[:, 0]
ann = wfdb.rdann(str(self.mitdb_dir+'/'+record), 'atr')
anno = _tester_utils.sort_MIT_annotations(ann)
r_peaks = detector(unfiltered_ecg)
delay = _tester_utils.calcMedianDelay(r_peaks, unfiltered_ecg, max_delay_in_samples)
if delay > 1:
TP, FP, FN = _tester_utils.evaluate_detector(r_peaks, anno, delay, tol=tolerance)
TN = len(unfiltered_ecg)-(TP+FP+FN)
results[i, 0] = int(record)
results[i, 1] = TP
results[i, 2] = FP
results[i, 3] = FN
results[i, 4] = TN
max_delay_in_samples = 350 / 5
dat_files = []
for file in os.listdir(self.mitdb_dir):
if file.endswith(".dat"):
dat_files.append(file)
mit_records = [w.replace(".dat", "") for w in dat_files]
results = np.zeros((len(mit_records), 5), dtype=int)
i = 0
for record in mit_records:
progress = int(i/float(len(mit_records))*100.0)
print("MITDB progress: %i%%" % progress)
sig, fields = wfdb.rdsamp(self.mitdb_dir+'/'+record)
unfiltered_ecg = sig[:, 0]
ann = wfdb.rdann(str(self.mitdb_dir+'/'+record), 'atr')
anno = _tester_utils.sort_MIT_annotations(ann)
r_peaks = detector(unfiltered_ecg)
delay = _tester_utils.calcMedianDelay(r_peaks, unfiltered_ecg, max_delay_in_samples)
if delay > 1:
TP, FP, FN = _tester_utils.evaluate_detector(r_peaks, anno, delay, tol=tolerance)
TN = len(unfiltered_ecg)-(TP+FP+FN)
results[i, 0] = int(record)
results[i, 1] = TP
- patient001/
- patient002/
- ...
"""
#==============================================================================
# Extracting
#==============================================================================
data={"Control": {}, "Patient": {}}
participants = [x for x in os.listdir("./data/") if 'patient' in x]
for participant in participants:
files = os.listdir("./data/" + participant)
if len([x for x in files if '.dat' in x]) > 0:
file = [x for x in files if '.dat' in x][0].split(".")[0]
signals, info = wfdb.rdsamp("data/" + participant + "/" + file)
signals = pd.DataFrame(signals)
signals.columns = info["sig_name"]
data_participant = {}
data_participant["Signals"] = signals
data_participant["sampling_rate"] = info["fs"]
for key in info["comments"]:
try:
data_participant[key.split(": ")[0]] = key.split(": ")[1]
except IndexError:
data_participant[key.split(":")[0]] = np.nan
if data_participant["Reason for admission"] in ["n/a", "Healthy control"]:
def generate_raw_db(self):
"""Generate the raw version of the MIT-BIH Atrial Fibrillation database in the 'raw' folder."""
print('Generating Raw MIT-BIH Atrial Fibrillation Database ...')
# Download database
wfdb.dl_database(self.db_name, self.raw_path)
# Get list of recordings
self.record_ids = [file.split('.')[0] for file in os.listdir(self.raw_path) if '.dat' in file]
print('Complete!\n')
def generate_raw_db(self):
"""Generate the raw version of the MIT-BIH Normal Sinus Rhythm database in the 'raw' folder."""
print('Generating Raw MIT-BIH Normal Sinus Rhythm Database ...')
# Download database
wfdb.dl_database(self.db_name, self.raw_path)
# Get list of recordings
self.record_ids = [file.split('.')[0] for file in os.listdir(self.raw_path) if '.dat' in file]
print('Complete!\n')
sampto = None#int(60. * 1 * 360)
sampfrom = 0#int(15. * 360)
sensitivity = []
precision = []
for subject in zip(records, annotations):
#for i in selection:
#
# subject = zip(records, annotations)[i]
print('processing subject {}'.format(subject[1][-7:-4]))
data = wfdb.rdrecord(subject[0][:-4], sampto=sampto)
annotation = wfdb.rdann(subject[1][:-4], 'atr',
sampfrom=sampfrom,
sampto=sampto)
sfreq = data.fs
ecg = data.p_signal[:, 0]
manupeaks = annotation.sample
#algopeaks = peaks_signal(ecg, sfreq)
algopeaks = rpeaks(ecg, sfreq)
# tolerance for match between algorithmic and manual annotation (in sec)
tolerance = 0.05
comparitor = compare_annotations(manupeaks, algopeaks,
int(np.rint(tolerance * sfreq)))
tp = comparitor.tp
fp = comparitor.fp
def make_dataset(records, width, savepath):
""" Inside an array """
# Prepare containers
signals, labels = [], []
# Iterate files
for path in records:
print 'Processing file:', path
record = wf.rdsamp(path)
annotations = wf.rdann(path, 'atr')
# Extract pure signals
data = record.p_signals
# Convert each channel into labeled fragments
signal, label = convert_data(data, annotations, width)
# Cumulate
signals.append(signal)
labels.append(label)
# Convert to one huge numpy.array
signals = np.vstack(signals)
labels = np.vstack(labels)
# Write to disk
def read_file(file, participant):
"""Utility function
"""
# Get signal
data = pd.DataFrame({"ECG": wfdb.rdsamp(file[:-4])[0][:, 0]})
data["Participant"] = "MIT-Arrhythmia_%.2i" %(participant)
data["Sample"] = range(len(data))
data["Sampling_Rate"] = 360
data["Database"] = "MIT-Arrhythmia-x" if "x_mitdb" in file else "MIT-Arrhythmia"
# getting annotations
anno = wfdb.rdann(file[:-4], 'atr')
anno = np.unique(anno.sample[np.in1d(anno.symbol, ['N', 'L', 'R', 'B', 'A', 'a', 'J', 'S', 'V', 'r', 'F', 'e', 'j', 'n', 'E', '/', 'f', 'Q', '?'])])
anno = pd.DataFrame({"Rpeaks": anno})
anno["Participant"] = "MIT-Arrhythmia_%.2i" %(participant)
anno["Sampling_Rate"] = 360
anno["Database"] = "MIT-Arrhythmia-x" if "x_mitdb" in file else "MIT-Arrhythmia"
return data, anno
def show_path(path):
""" As a plot """
# Read in the data
record = wf.rdsamp(path)
annotation = wf.rdann(path, 'atr')
data = record.p_signals
cha = data[:, 0]
print 'Channel type:', record.signame[0]
times = np.arange(len(cha), dtype = float)
times /= record.fs
plt.plot(times, cha)
plt.xlabel('Time [s]')
plt.show()
def _get_sections(self):
"""Collect continuous arrhythmia sections."""
# Empty dictionary for arrhythmia sections
sections = list()
# Loop through records
for record_id in self.record_ids:
# Import recording
record = wfdb.rdrecord(os.path.join(self.raw_path, record_id))
# Import annotations
annotation = wfdb.rdann(os.path.join(self.raw_path, record_id), 'atr')
# Get sample frequency
fs = record.__dict__['fs']
# Get waveform
waveform = record.__dict__['p_signal']
# labels
labels = [label[1:] for label in annotation.__dict__['aux_note']]
# Samples
sample = annotation.__dict__['sample']
# Loop through labels and collect sections
for idx, label in enumerate(labels):