Skip to content

Commit

Permalink
Added time in seconds and minutes (not just quarter seconds)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyheather committed Nov 27, 2023
1 parent aba497b commit cec746f
Show file tree
Hide file tree
Showing 553 changed files with 9,825,764 additions and 9,825,734 deletions.
40 changes: 35 additions & 5 deletions 01_create_csv_database.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"\n",
"This converts the `.dat` and `.hea` files in the `data/` folder into `.csv` files stored in the `data_csv/` folder.\n",
"\n",
"Source: https://github.com/fabiom91/CTU-CHB_Physionet.org/blob/master/create_csv_database.ipynb"
"Code adapted from: https://github.com/fabiom91/CTU-CHB_Physionet.org/blob/master/create_csv_database.ipynb"
]
},
{
Expand All @@ -24,10 +24,11 @@
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import wfdb\n",
"import math\n",
"from os import listdir\n",
"import pandas as pd\n",
"from tqdm import tqdm\n",
"import wfdb\n",
"\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
Expand Down Expand Up @@ -62,15 +63,44 @@
" return rec_list\n",
"\n",
"\n",
"def consec_repeat(repeats, max_value, len):\n",
" '''\n",
" Creates list with consecutive numbers, where each number repeats a specified\n",
" amount of times\n",
" Inputs:\n",
" - repeats - int, number of times to repeat each number\n",
" - max_value - int, maximum number to reach\n",
" - len - int, length of list (as may be to a quarter second so not 4 end rep)\n",
" '''\n",
" # Round up max_value to nearest integer if required\n",
" max_value = math.ceil(max_value)\n",
" # Create list of repeated consecutive numbers\n",
" list = [x//repeats for x in range((max_value+1)*repeats)]\n",
" # Trim list to desired length (to deal with odd quarter seconds and with\n",
" # the zero-based indexing (whilst max_value will be one higher))\n",
" list = list[:len]\n",
" return(list)\n",
"\n",
"\n",
"def create_signals_database(rec):\n",
" '''\n",
" Read the signal files (with FHR and UC) and save to csv\n",
" Read the signal files (with FHR and UC), add tme in seconds and minutes,\n",
" and save to csv file.\n",
" Inputs:\n",
" - rec - name of record (e.g. '1347')\n",
" '''\n",
" # Read signal file and save to dataframe\n",
" sample = wfdb.rdsamp(\"data/%s\" % rec)\n",
" df = pd.DataFrame(sample[0], columns=['FHR','UC'])\n",
" df.index.name = 'quarter_second'\n",
" # Find the length of the record in quarter seconds, seconds and minutes\n",
" q_sec = len(df.index)\n",
" sec = q_sec/4\n",
" min = sec/60\n",
" # Add column with time in seconds and minutes\n",
" df['second'] = consec_repeat(repeats=4, max_value=int(sec), len=q_sec)\n",
" df['minute'] = consec_repeat(repeats=4*60, max_value=int(min), len=q_sec)\n",
" # Save to csv file\n",
" df.to_csv('data_csv/%s.csv' % rec)\n",
"\n",
"\n",
Expand Down Expand Up @@ -130,7 +160,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"100%|██████████| 1104/1104 [00:50<00:00, 21.65it/s]"
"100%|██████████| 1104/1104 [01:12<00:00, 15.27it/s]"
]
},
{
Expand Down
Loading

0 comments on commit cec746f

Please sign in to comment.