VitalDB Python Library
Introduction
This document explains the VitalDB python library which helps the use of Vital file format and VitalDB data in Python language.
Installation
VitalDB Python Library can be installed using the package installer for python (PIP).
$ pip install vitaldb |
And then you need to import the vitaldb library.
import vitaldb |
Vital File API
You can read a track list from a vital file using vital_trks function.
vitaldb.vital_trks(ipath) Parameter list
Return value
|
You can read the samples(values) in a vital file using vital_recs function. It returns the samples in a numpy array.
vitaldb.vital_recs(ipath, track_names=None, interval=None, return_timestamp=False, return_datetime=False, return_pandas=False, exclude=None) Parameter list
Return value
|
The library contains a VitalFile class to read and write Vital File Format.
vitaldb.VitalFile(ipath, track_names=None, header_only=False, skip_records=None, exclude=None, userid=None, maxlen=None, interval=None) Parameter list
Return value
|
Sample Code
The following example code downloads a vital file with caseid of 1 to the current working directory.
vf = vitaldb.VitalFile(1) vf.to_vital('1.vital') |
Results:
You can open and visualize the downloaded vital file using the Vital Recorder.
The next sample code downloads the SNUADC/ECG_II track only. It is faster than the former example.
import vitaldb vf = vitaldb.VitalFile(1, ['SNUADC/ECG_II']) vf.to_vital('1.vital') |
Results:
vitaldb.read_vital(ipath, track_names=None, exclude=None, header_only=False, maxlen=None) Parameter list
Return value
|
vitaldb.read_csv(ipath, track_names=None, exclude=None, interval=None) Parameter list
Return value
|
vitaldb.read_wfdb(ipath, track_names=None, exclude=None, header_only=False) Parameter list
Return value
|
vitaldb.read_parquet(ipath, track_names=None, exclude=None) Parameter list
Return value
|
After loading the vital file, you can convert it to pandas or numpy format for data analysis.
vitaldb.VitalFile.to_numpy(track_names, interval, return_datetime=False, return_timestamp=False) Parameter list
|
Sample Code
The following code downloads arterial waveform from the first case of open dataset and show.
import vitaldb import matplotlib.pyplot as plt
track_names = ['SNUADC/ART'] vf = vitaldb.VitalFile(1, track_names) samples = vf.to_numpy(track_names, 1/100)
plt.figure(figsize=(20, 5)) plt.plot(samples[:, 0]) plt.show() |
vitaldb.VitalFile.to_pandas(track_names, interval, return_datetime=False, return_timestamp=False) Parameter list
|
You can save the vital files in several formats.
vitaldb.VitalFile.to_vital(opath, compresslevel=1)
Parameter list
|
vitaldb.VitalFile.to_csv(opath, track_names, interval, return_datetime=False, return_timestamp=False)
Parameter list
|
vitaldb.VitalFile.to_wfdb(opath, track_names=None, interval=None)
Parameter list
|
vitaldb.VitalFile.to_wav(opath, track_names, srate=None)
Parameter list
|
vitaldb.VitalFile.to_parquet(opath)
Parameter list
|
There are other functions for using and editing VitalFile.
vitaldb.VitalFile.crop(dtfrom=None, dtend=None)
Parameter list
|
vitaldb.VitalFile.get_track_names()
|
vitaldb.VitalFile.get_track_samples(dtname, interval)
Parameter list
|
vitaldb.VitalFile.remove_track(dtname)
Parameter list
|
vitaldb.VitalFile.add_track(dtname, recs, srate=0, unit=’’, mindisp=0, maxdisp=0) Parameter list
|
vitaldb.VitalFile.find_track(dtname)
Parameter list
|
Platform API
You can access the vital files from cloud storage or intranet server with the api module.
vitaldb.login(id, pw, host=None, port=None)
Parameter list
|
vitaldb.filelist(bedname=None, dtstart=None, dtend=None)
Parameter list
|
vitaldb.download(filename, localpath=None)
Parameter list
|
Sample code
The following sample code downloads all vital files with bedname and periods.
import vitaldb import pandas as pd import os # path to save downloaded files DOWNLOAD_DIR = "Download" SERVER_IP = 'xxx.xx.xxx.xxx' SERVER_PORT = '80' LOGIN_ID = '' # Please enter your login id LOGIN_PW = '' # Please enter your login password BED_NAME = "MICU" START_DATE = "2022-01-01" END_DATE = "2022-01-31" if vitaldb.login(LOGIN_ID, LOGIN_PW, SERVER_IP, SERVER_PORT): # login # check filelist filelist = vitaldb.filelist(bedname=BED_NAME, dtstart=START_DATE, dtend=END_DATE) print(pd.DataFrame(filelist)) if not os.path.exists(DOWNLOAD_DIR): os.mkdir(DOWNLOAD_DIR) # files load & download for file in filelist: print('downloading ' + file['filename'], end='...') localpath = DOWNLOAD_DIR + '/' + file['filename'] res = vitaldb.download(file['filename'], localpath) print('done') else: print('login error') |
Dataset API
You can use vitaldb open datasets with vitaldb python library.
Find cases in the VitalDB open dataset
caseids = vitaldb.find_cases(['ECG_II', 'ART']) len(caseids) |
Results: 3644
Read an open dataset case
vals = vitaldb.load_case(caseids[0], ['ECG_II','ART'], 1/100) print(vals)
ecg = vals[:,0] art = vals[:,1]
# plot import matplotlib.pyplot as plt plt.figure(figsize=(20,10)) plt.subplot(211) plt.plot(ecg[110000:111000], color='g') plt.subplot(212) plt.plot(art[110000:111000], color='r') plt.show() |
Results:
[[ nan nan]
[ nan nan]
[ nan nan]
...
[ 0.148893 -32.5087 ]
[ -0.325087 19.8266 ]
[ nan nan]]