{
  "slug": "Reading-Matlab--mat-format-Dataset-in-Python-f19c024bdb83",
  "title": "Reading Matlab .mat format Dataset in Python",
  "subtitle": "Cuff-Less Blood Pressure Estimation Data Set has a Matlab Dataset and I won’t use it in python code.",
  "excerpt": "Cuff-Less Blood Pressure Estimation Data Set has a Matlab Dataset and I won’t use it in python code.",
  "date": "2020-12-21",
  "tags": [
    "Python",
    "Machine Learning"
  ],
  "readingTime": "1 min",
  "url": "https://medium.com/@mobinshaterian/reading-matlab-mat-format-dataset-in-python-f19c024bdb83",
  "hero": "https://cdn-images-1.medium.com/max/800/1*mRfv8Xy1F6263TOJh9p8vg.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Reading Matlab .mat format Dataset in Python"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*mRfv8Xy1F6263TOJh9p8vg.png",
      "alt": "Reading Matlab .mat format Dataset in Python",
      "caption": "",
      "width": 1028,
      "height": 387
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Cuff-Less Blood Pressure Estimation Data Set has a Matlab Dataset and I won’t use it in python code."
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import pandas as pd"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import numpy as np"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import h5py"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import matplotlib.pyplot as plt"
    },
    {
      "type": "paragraph",
      "html": "This dataset using Matlab’s v7.3 mat file format furthermore the best solution to read this dataset is using h5py."
    },
    {
      "type": "code",
      "lang": "python",
      "code": "import h5py"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "filename = \"./data/Part_1.mat\""
    },
    {
      "type": "code",
      "lang": "text",
      "code": "with h5py.File(filename, \"r\") as f:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "# List all groups"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "print(f.keys() )"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "a_group_key = list(f.keys())[0]"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "# Get the data"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "data = list(f[a_group_key])"
    },
    {
      "type": "paragraph",
      "html": "the result shows the header file"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "<KeysViewHDF5 ['#refs#', 'Part_1']>"
    },
    {
      "type": "paragraph",
      "html": "and then working with a dataset as NumPy"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "f = h5py.File('./data/Part_1.mat','r')"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "test = f['Part_1']"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "st = test[0][0] # has 3000 element #<HDF5 dataset \"Part_1\": shape (3000, 1), type \"|O\">"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "st = test[0][0]"
    },
    {
      "type": "code",
      "lang": "python",
      "code": "obj=f[st]"
    },
    {
      "type": "paragraph",
      "html": "now obj is first pateint data"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "obj[:]"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "array([[ 1.75953079e+00,  6.70629552e+01, -6.06060606e-02],       [ 1.71847507e+00,  6.93586281e+01,\n-7.52688172e-02],       [ 1.68426197e+00,  7.53664529e+01, -7.03812317e-02],       ...,       [\n1.64418377e+00,  7.64410232e+01, -1.00195503e-01],       [ 1.60019550e+00,  8.18138747e+01,\n-9.04203324e-02],       [ 1.56598240e+00,  9.13873191e+01, -4.54545455e-02]])"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "obj.shape"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "(61000, 3)"
    },
    {
      "type": "paragraph",
      "html": "the dataset has 3 attributes"
    },
    {
      "type": "paragraph",
      "html": "1: PPG signal, FS=125Hz; photoplethysmograph from fingertip&nbsp;<br>2: ABP signal, FS=125Hz; invasive arterial blood pressure (mmHg)&nbsp;<br>3: ECG signal, FS=125Hz; electrocardiogram from channel II"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*_tCuHXDYbKKEQZufi1MscA.png",
      "alt": "PPG Signal",
      "caption": "PPG Signal",
      "width": 1159,
      "height": 578
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*7u0Uhtgu9gTztLXyPQcqGw.png",
      "alt": "ABP Signal",
      "caption": "ABP Signal",
      "width": 1159,
      "height": 580
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*V8CGMVoUyjt1rk5wCt6dpA.png",
      "alt": "ECG Channel 2 Signal",
      "caption": "ECG Channel 2 Signal",
      "width": 1340,
      "height": 451
    },
    {
      "type": "paragraph",
      "html": "<a href=\"https://archive.ics.uci.edu/ml/datasets/Cuff-Less+Blood+Pressure+Estimation\" target=\"_blank\" rel=\"noreferrer noopener\">UCI Dataset</a>"
    }
  ]
}
