Skip to content

Instantly share code, notes, and snippets.

@DavidEWarrenPhD
Created October 10, 2019 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DavidEWarrenPhD/ceb344408440ea396a1fece839d0c9ce to your computer and use it in GitHub Desktop.
Save DavidEWarrenPhD/ceb344408440ea396a1fece839d0c9ce to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import os
import numpy as np
import pydicom as dicom
PHYSIO_DATA_TAG = '7fe11010'
def main(path, outdir=None):
'''Main function.'''
if outdir is None:
outdir, _ = os.path.split(path)
d = dicom.read_file(path)
data = d[int(PHYSIO_DATA_TAG, 16)]
raw = data.value
length_bytes = len(raw)
rows = d.AcquisitionNumber
columns = length_bytes / rows
num_files = columns / 1024
length_file = int(len(raw) / num_files)
arrs = [raw[i:i + length_file] for i in range(0, len(raw), length_file)]
for arr in arrs:
data_length = int(np.array(arr[:4]).view(np.uint32))
fn_length = int(np.array(arr[4:8]).view(np.uint32))
fn = arr[8:(8+fn_length)].decode('ascii')
out_path = os.path.join(outdir, fn)
print(fn)
data = arr[1024:(1024 + data_length)]
with open(out_path, 'wb') as f:
f.write(data)
def parse_args():
'''Parse arguments.'''
parser = argparse.ArgumentParser(add_help=False)
reqarg = parser.add_argument_group(title='required arguments')
optarg = parser.add_argument_group(title='optional arguments')
reqarg.add_argument('--physio', help='path of physio DICOM file',
required=True)
optarg.add_argument('-h', '--help', action='help',
default=argparse.SUPPRESS,
help='Show this help message and exit')
optarg.add_argument('--outdir', help='directory for output',
required=False)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(args.physio, args.outdir)
@marcelzwiers
Copy link

Ooops, I didn't know you started this but I wanted a python version too and ported the matlab code. I figured you might find this interesting:
https://github.com/CMRR-C2P/MB/blob/master/readCMRRPhysio.py

I integrated it in and may develop it further in the context of my bidscoin tool:
https://github.com/Donders-Institute/bidscoin/tree/master/bidscoin

@DavidEWarrenPhD
Copy link
Author

DavidEWarrenPhD commented Feb 12, 2021

Very cool, glad that someone else did the hard part!

PS I wrote this up at my blog, too: https://david-e-warren.me/blog/physiological-data-from-mri/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment