<jupyter_start><jupyter_text>PhysioNet/Computing in Cardiology Challenge 2020 Classification of 12-lead ECGs Explore Heart Rate Classification  Setup Notebook<jupyter_code># Import 3rd party libraries
import os
import sys
import json
import numpy as np
import pandas as pd
import matplotlib.pylab as plt

# Import local Libraries
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(os.getcwd()))))
from kardioml import DATA_PATH

# Configure Notebook
import warnings
warnings.filterwarnings('ignore')
%matplotlib inline
%load_ext autoreload
%autoreload 2<jupyter_output><empty_output><jupyter_text>Dataset A<jupyter_code># Set dataset
dataset = 'A'

# Get list of JSONs
files = [file for file in os.listdir(os.path.join(DATA_PATH, dataset, 'formatted')) if 'json' in file]

# Extract heart rate data
data = list()

# Arrhythmias
hr_arrhythmias = ['bradycardia', 'sinus tachycardia', 'sinus bradycardia']

# Loop through files
for file in files:
    
    # Load file
    file = json.load(open(os.path.join(DATA_PATH, dataset, 'formatted', file)))
    
    # Get label
    for hr_arrhythmia in hr_arrhythmias:
        
        if file['labels_full']:
            if hr_arrhythmia in file['labels_full']:
                label = hr_arrhythmia
            else:
                label = 'other'

            data.append({'filename': file['filename'], 'dataset': dataset, 'label': label, 'hr': np.float(file['hr'])})
            
# Create DataFrame
data_df_a = pd.DataFrame(data)

# View DataFrame
data_df_a.head()
# Plot age distribution
fig = plt.figure(figsize=(15, 5), facecolor='w')
fig.subplots_adjust(wspace=0, hspace=0.05)
ax = plt.subplot2grid((1, 1), (0, 0))

ax.set_title('Dataset: {}'.format(dataset))
ax.hist(data_df_a['hr'][data_df_a['label'] == 'other'].values, bins=30, facecolor='g', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='other')
ax.hist(data_df_a['hr'][data_df_a['label'] == 'bradycardia'].values, bins=30, facecolor='m', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='bradycardia')
ax.hist(data_df_a['hr'][data_df_a['label'] == 'sinus tachycardia'].values, bins=30, facecolor='c', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus tachycardia')
ax.hist(data_df_a['hr'][data_df_a['label'] == 'sinus bradycardia'].values, bins=30, facecolor='b', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus bradycardia')

plt.legend()
ax.set_xlabel('Heart Rate, BPM', fontsize=24)
ax.set_ylabel('Frequency', fontsize=24)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
# ax.set_xlim([0, 110])

plt.show()<jupyter_output><empty_output><jupyter_text>Dataset B<jupyter_code># Set dataset
dataset = 'B'

# Get list of JSONs
files = [file for file in os.listdir(os.path.join(DATA_PATH, dataset, 'formatted')) if 'json' in file]

# Extract heart rate data
data = list()

# Arrhythmias
hr_arrhythmias = ['bradycardia', 'sinus tachycardia', 'sinus bradycardia']

# Loop through files
for file in files:
    
    # Load file
    file = json.load(open(os.path.join(DATA_PATH, dataset, 'formatted', file)))
    
    # Get label
    for hr_arrhythmia in hr_arrhythmias:
        
        if file['labels_full']:
            if hr_arrhythmia in file['labels_full']:
                label = hr_arrhythmia
            else:
                label = 'other'

            data.append({'filename': file['filename'], 'dataset': dataset, 'label': label, 'hr': np.float(file['hr'])})
            
# Create DataFrame
data_df_b = pd.DataFrame(data)

# View DataFrame
data_df_b.head()
# Plot age distribution
fig = plt.figure(figsize=(15, 5), facecolor='w')
fig.subplots_adjust(wspace=0, hspace=0.05)
ax = plt.subplot2grid((1, 1), (0, 0))

ax.set_title('Dataset: {}'.format(dataset))
ax.hist(data_df_b['hr'][data_df_b['label'] == 'other'].values, bins=30, facecolor='g', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='other')
ax.hist(data_df_b['hr'][data_df_b['label'] == 'bradycardia'].values, bins=30, facecolor='m', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='bradycardia')
ax.hist(data_df_b['hr'][data_df_b['label'] == 'sinus tachycardia'].values, bins=30, facecolor='c', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus tachycardia')
ax.hist(data_df_b['hr'][data_df_b['label'] == 'sinus bradycardia'].values, bins=30, facecolor='b', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus bradycardia')

plt.legend()
ax.set_xlabel('Heart Rate, BPM', fontsize=24)
ax.set_ylabel('Frequency', fontsize=24)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
# ax.set_xlim([0, 110])

plt.show()<jupyter_output><empty_output><jupyter_text>Dataset C<jupyter_code># Set dataset
dataset = 'C'

# Get list of JSONs
files = [file for file in os.listdir(os.path.join(DATA_PATH, dataset, 'formatted')) if 'json' in file]

# Extract heart rate data
data = list()

# Arrhythmias
hr_arrhythmias = ['bradycardia', 'sinus tachycardia', 'sinus bradycardia']

# Loop through files
for file in files:
    
    # Load file
    file = json.load(open(os.path.join(DATA_PATH, dataset, 'formatted', file)))
    
    # Get label
    for hr_arrhythmia in hr_arrhythmias:
        
        if file['labels_full']:
            if hr_arrhythmia in file['labels_full']:
                label = hr_arrhythmia
            else:
                label = 'other'

            data.append({'filename': file['filename'], 'dataset': dataset, 'label': label, 'hr': np.float(file['hr'])})
            
# Create DataFrame
data_df_c = pd.DataFrame(data)

# View DataFrame
data_df_c.head()
# Plot age distribution
fig = plt.figure(figsize=(15, 5), facecolor='w')
fig.subplots_adjust(wspace=0, hspace=0.05)
ax = plt.subplot2grid((1, 1), (0, 0))

ax.set_title('Dataset: {}'.format(dataset))
ax.hist(data_df_c['hr'][data_df_c['label'] == 'other'].values, bins=30, facecolor='g', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='other')
ax.hist(data_df_c['hr'][data_df_c['label'] == 'bradycardia'].values, bins=30, facecolor='m', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='bradycardia')
ax.hist(data_df_c['hr'][data_df_c['label'] == 'sinus tachycardia'].values, bins=30, facecolor='c', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus tachycardia')
ax.hist(data_df_c['hr'][data_df_c['label'] == 'sinus bradycardia'].values, bins=30, facecolor='b', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus bradycardia')

plt.legend()
ax.set_xlabel('Heart Rate, BPM', fontsize=24)
ax.set_ylabel('Frequency', fontsize=24)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
# ax.set_xlim([0, 110])

plt.show()<jupyter_output><empty_output><jupyter_text>Dataset D<jupyter_code># Set dataset
dataset = 'D'

# Get list of JSONs
files = [file for file in os.listdir(os.path.join(DATA_PATH, dataset, 'formatted')) if 'json' in file]

# Extract heart rate data
data = list()

# Arrhythmias
hr_arrhythmias = ['bradycardia', 'sinus tachycardia', 'sinus bradycardia']

# Loop through files
for file in files:
    
    # Load file
    file = json.load(open(os.path.join(DATA_PATH, dataset, 'formatted', file)))
    
    # Get label
    for hr_arrhythmia in hr_arrhythmias:
        
        if file['labels_full']:
            if hr_arrhythmia in file['labels_full']:
                label = hr_arrhythmia
            else:
                label = 'other'

            data.append({'filename': file['filename'], 'dataset': dataset, 'label': label, 'hr': np.float(file['hr'])})
            
# Create DataFrame
data_df_d = pd.DataFrame(data)

# View DataFrame
data_df_d.head()
# Plot age distribution
fig = plt.figure(figsize=(15, 5), facecolor='w')
fig.subplots_adjust(wspace=0, hspace=0.05)
ax = plt.subplot2grid((1, 1), (0, 0))

ax.set_title('Dataset: {}'.format(dataset))
ax.hist(data_df_d['hr'][data_df_d['label'] == 'other'].values, bins=30, facecolor='g', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='other')
ax.hist(data_df_d['hr'][data_df_d['label'] == 'bradycardia'].values, bins=30, facecolor='m', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='bradycardia')
ax.hist(data_df_d['hr'][data_df_d['label'] == 'sinus tachycardia'].values, bins=30, facecolor='c', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus tachycardia')
ax.hist(data_df_d['hr'][data_df_d['label'] == 'sinus bradycardia'].values, bins=30, facecolor='b', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus bradycardia')

plt.legend()
ax.set_xlabel('Heart Rate, BPM', fontsize=24)
ax.set_ylabel('Frequency', fontsize=24)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
# ax.set_xlim([0, 110])

plt.show()<jupyter_output><empty_output><jupyter_text>Dataset E<jupyter_code># Set dataset
dataset = 'E'

# Get list of JSONs
files = [file for file in os.listdir(os.path.join(DATA_PATH, dataset, 'formatted')) if 'json' in file]

# Extract heart rate data
data = list()

# Arrhythmias
hr_arrhythmias = ['bradycardia', 'sinus tachycardia', 'sinus bradycardia']

# Loop through files
for file in files:
    
    # Load file
    file = json.load(open(os.path.join(DATA_PATH, dataset, 'formatted', file)))
    
    # Get label
    for hr_arrhythmia in hr_arrhythmias:
        
        if file['labels_full']:
            if hr_arrhythmia in file['labels_full']:
                label = hr_arrhythmia
            else:
                label = 'other'

            data.append({'filename': file['filename'], 'dataset': dataset, 'label': label, 'hr': np.float(file['hr'])})
            
# Create DataFrame
data_df_e = pd.DataFrame(data)

# View DataFrame
data_df_e.head()
# Plot age distribution
fig = plt.figure(figsize=(15, 5), facecolor='w')
fig.subplots_adjust(wspace=0, hspace=0.05)
ax = plt.subplot2grid((1, 1), (0, 0))

ax.set_title('Dataset: {}'.format(dataset))
ax.hist(data_df_e['hr'][data_df_e['label'] == 'other'].values, bins=30, facecolor='g', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='other')
ax.hist(data_df_e['hr'][data_df_e['label'] == 'bradycardia'].values, bins=30, facecolor='m', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='bradycardia')
ax.hist(data_df_e['hr'][data_df_e['label'] == 'sinus tachycardia'].values, bins=30, facecolor='c', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus tachycardia')
ax.hist(data_df_e['hr'][data_df_e['label'] == 'sinus bradycardia'].values, bins=30, facecolor='b', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus bradycardia')

plt.legend()
ax.set_xlabel('Heart Rate, BPM', fontsize=24)
ax.set_ylabel('Frequency', fontsize=24)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
# ax.set_xlim([0, 110])

plt.show()<jupyter_output><empty_output><jupyter_text>Dataset F<jupyter_code># Set dataset
dataset = 'F'

# Get list of JSONs
files = [file for file in os.listdir(os.path.join(DATA_PATH, dataset, 'formatted')) if 'json' in file]

# Extract heart rate data
data = list()

# Arrhythmias
hr_arrhythmias = ['bradycardia', 'sinus tachycardia', 'sinus bradycardia']

# Loop through files
for file in files:
    
    # Load file
    file = json.load(open(os.path.join(DATA_PATH, dataset, 'formatted', file)))
    
    # Get label
    for hr_arrhythmia in hr_arrhythmias:
        
        if file['labels_full']:
            if hr_arrhythmia in file['labels_full']:
                label = hr_arrhythmia
            else:
                label = 'other'

            data.append({'filename': file['filename'], 'dataset': dataset, 'label': label, 'hr': np.float(file['hr'])})
            
# Create DataFrame
data_df_f = pd.DataFrame(data)

# View DataFrame
data_df_f.head()
# Plot age distribution
fig = plt.figure(figsize=(15, 5), facecolor='w')
fig.subplots_adjust(wspace=0, hspace=0.05)
ax = plt.subplot2grid((1, 1), (0, 0))

ax.set_title('Dataset: {}'.format(dataset))
ax.hist(data_df_f['hr'][data_df_f['label'] == 'other'].values, bins=30, facecolor='g', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='other')
ax.hist(data_df_f['hr'][data_df_f['label'] == 'bradycardia'].values, bins=30, facecolor='m', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='bradycardia')
ax.hist(data_df_f['hr'][data_df_f['label'] == 'sinus tachycardia'].values, bins=30, facecolor='c', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus tachycardia')
ax.hist(data_df_f['hr'][data_df_f['label'] == 'sinus bradycardia'].values, bins=30, facecolor='b', 
        edgecolor=[0.7, 0.7, 0.7], linewidth=1.2, alpha=0.5, label='sinus bradycardia')

plt.legend()
ax.set_xlabel('Heart Rate, BPM', fontsize=24)
ax.set_ylabel('Frequency', fontsize=24)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
# ax.set_xlim([0, 110])

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1752728961833243, 'fraction_numerical': 0.03414105442440195, 'mean_word_length': 4.270501835985312, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '504194', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 5191, 'n_tokens_neox': 4871, 'n_words': 969, 'path': 'notebooks/Goodfellow/heart_rate_classification.ipynb', 'repo_name': 'Seb-Good/physionet-challenge-2020', 'size': '12917'}