Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion suspect/io/dicom.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def load_dicom(filename):

# versions of pydicom >2.0.0 require explicit conversion from bytestring to list
if type(dataset[0x5600, 0x0020].value) == bytes:
data_iter = iter(np.fromstring(dataset[0x5600, 0x0020].value, dtype=np.float32))
data_iter = iter(np.frombuffer(dataset[0x5600, 0x0020].value, dtype=np.float32))

elif type(dataset[0x5600, 0x0020].value) == list:
data_iter = iter(dataset[0x5600, 0x0020].value)
Expand Down
42 changes: 36 additions & 6 deletions suspect/io/twix.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from contextlib import contextmanager
from suspect import MRSData, transformation_matrix, rotation_matrix

import struct
import numpy
#import quaternion
import re
import io
import os

# This file largely relies on information from Siemens regarding the structure
# of the TWIX file formats. Most of the parameters that are read use the same
Expand Down Expand Up @@ -97,10 +100,14 @@ def get_meta_regex(regex_list, header_string, convert=1, default=None):

Parameters
----------
regex_list : List of regex string
header_string : TWIX header string
convert : Unit convertion, if value is number. Defaults to 1 (means no convertion)
default : Default value if match found, but value is empty. Defaults to None.
regex_list : list
List of regex string
header_string : string
TWIX header string
convert : int, optional
Unit convertion, if value is number. Defaults to 1 (means no convertion)
default
Default value if match found, but value is empty. Defaults to None.

Returns
-------
Expand Down Expand Up @@ -502,10 +509,33 @@ def load_twix_vd(fin, builder):
# move the file pointer to the start of the next scan
fin.seek(initial_position + DMA_length)

def load_twix(source_file, buffering=io.DEFAULT_BUFFER_SIZE):
"""
Load TWIX data.

def load_twix(filename):
with open(filename, 'rb') as fin:
Parameters
----------
source_file : str or file-like
File path of TWIX file

Returns
-------
suspect.MRSData

"""
@contextmanager
def open_if_filepath(file_or_path, mode='r', **kwargs):
"""Context manager to open a file if argument is string"""
if isinstance(file_or_path, (str, os.PathLike)):
f = open(file_or_path, mode, **kwargs)
try:
yield f
finally:
f.close()
else:
yield file_or_path

with open_if_filepath(source_file, 'rb') as fin:
# we can tell the type of file from the first two uints in the header
first_uint, second_uint = struct.unpack("II", fin.read(8))

Expand Down
4 changes: 4 additions & 0 deletions tests/test_mrs/test_twix.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_veriofile():
[0, 0, 0, 1]]
))

# Test loading via file-like object and ensure same result
with open("tests/test_data/siemens/twix_vd.dat", "rb") as f:
data_from_binary_stream = suspect.io.load_twix(f)
assert numpy.all(data == data_from_binary_stream)
#def test_skyra():
# data = suspect.io.load_twix("tests/test_data/twix_vd_csi.dat")
# assert data.np == 2048
Expand Down