Skip to content

Commit 4ae5b67

Browse files
committed
NF - draft framework for testing CSA readers
1 parent 96aca04 commit 4ae5b67

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

nibabel/dicom/csareader.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
''' CSA header reader from SPM spec
2+
3+
'''
4+
from struct import unpack
5+
6+
7+
class CSAReadError(Exception):
8+
pass
9+
10+
11+
def read(fileobj):
12+
''' Read CSA header from fileobj
13+
14+
Parameters
15+
----------
16+
fileobj : file-like
17+
file-like object implementing ``read, seek, tell`. The data is
18+
taken to start at position 0 and end at the end of the file.
19+
20+
Returns
21+
-------
22+
header : dict
23+
header information
24+
tags : sequence
25+
sequence of tags from header
26+
'''
27+
fileobj.seek(0)
28+
hdr_id = fileobj.read(4)
29+
hdr = {}
30+
tags = []
31+
if hdr_id != 'SV10':
32+
hdr['type'] = 1
33+
hdr['n_tags'], = unpack('<i', hdr_id)
34+
else:
35+
hdr['type'] = 2
36+
hdr['unused1'] = fileobj.read(4)
37+
hdr['n_tags'], = unpack('<I', fileobj.read(4))
38+
hdr['unused3'], = unpack('<I', fileobj.read(4))
39+
return hdr, tags

nibabel/dicom/tests/test_csareader.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
""" Testing Siemens CSA header reader
2+
"""
3+
import os
4+
from os.path import join as pjoin
5+
6+
import numpy as np
7+
8+
import dipy.io.csareader as csa
9+
10+
from nose.tools import assert_true, assert_false, \
11+
assert_equal, assert_raises
12+
13+
from numpy.testing import assert_array_equal, assert_array_almost_equal
14+
15+
from dipy.testing import parametric
16+
17+
18+
data_path = pjoin(os.path.dirname(__file__), 'data')
19+
20+
CSA2_B0 = pjoin(data_path, 'csa2_b0.bin')
21+
CSA2_B1000 = pjoin(data_path, 'csa2_b1000.bin')
22+
23+
24+
@parametric
25+
def test_csa():
26+
csa_f = open(CSA2_B0, 'rb')
27+
hdr, tags = csa.read(csa_f)
28+
yield assert_equal(hdr['type'], 2)
29+
yield assert_equal(hdr['n_tags'], 83)
30+
yield assert_equal(len(tags), 83)
31+

0 commit comments

Comments
 (0)