diff --git a/suspect/io/dicom.py b/suspect/io/dicom.py index 6a7ede4..9c4d6a7 100644 --- a/suspect/io/dicom.py +++ b/suspect/io/dicom.py @@ -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) diff --git a/suspect/io/twix.py b/suspect/io/twix.py index 1ffd471..753ad17 100644 --- a/suspect/io/twix.py +++ b/suspect/io/twix.py @@ -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 @@ -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 ------- @@ -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)) diff --git a/tests/test_mrs/test_twix.py b/tests/test_mrs/test_twix.py index f840ebe..af988e9 100644 --- a/tests/test_mrs/test_twix.py +++ b/tests/test_mrs/test_twix.py @@ -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