Skip to content

Commit 966444a

Browse files
Revert "feat: update read() to parse both dat formats"
This reverts commit adb29ce.
1 parent d3535b1 commit 966444a

File tree

1 file changed

+18
-54
lines changed

1 file changed

+18
-54
lines changed

rawtools/dat.py

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,15 @@ def determine_bit_depth(fp, dims):
6161
logging.warning(f"Unable to determine bit-depth of volume '{fp}'. Expected at <{expected_filesize}> bytes but found <{file_size}> bytes. Defaulting to unsigned 16-bit.")
6262
return 'uint16'
6363

64-
def __parse_object_filename(line, dat_format):
65-
if (dat_format == "Dragonfly"):
66-
pattern = r"\s+<ObjectFileName>(?P<filename>.*\.raw)<\/ObjectFileName>$"
67-
elif (dat_format == "NSI"):
68-
pattern = r"^ObjectFileName\:\s+(?P<filename>.*\.raw)$"
64+
def __parse_object_filename(line):
65+
pattern = r"^ObjectFileName\:\s+(?P<filename>.*\.raw)$"
6966

7067
match = re.match(pattern, line, flags=re.IGNORECASE)
7168
if match is not None:
7269
logging.debug(f"Match: {match}")
7370
return match.group('filename')
7471

75-
def __parse_resolution(line, dat_format):
72+
def __parse_resolution(line):
7673
"""Get the x, y, z dimensions of a volume.
7774
7875
Args:
@@ -83,11 +80,8 @@ def __parse_resolution(line, dat_format):
8380
8481
"""
8582
# logging.debug(line.strip())
86-
if (dat_format == "Dragonfly"):
87-
pattern = r'\s+<Resolution X="(?P<x>\d+)"\s+Y="(?P<y>\d+)"\s+Z="(?P<z>\d+)"'
88-
elif (dat_format == "NSI"):
89-
pattern_old = r'\s+<Resolution X="(?P<x>\d+)"\s+Y="(?P<y>\d+)"\s+Z="(?P<z>\d+)"'
90-
pattern = r'Resolution\:\s+(?P<x>\d+)\s+(?P<y>\d+)\s+(?P<z>\d+)'
83+
pattern_old = r'\s+<Resolution X="(?P<x>\d+)"\s+Y="(?P<y>\d+)"\s+Z="(?P<z>\d+)"'
84+
pattern = r'Resolution\:\s+(?P<x>\d+)\s+(?P<y>\d+)\s+(?P<z>\d+)'
9185

9286
# See if the DAT file is the newer version
9387
match = re.match(pattern, line, flags=re.IGNORECASE)
@@ -109,7 +103,7 @@ def __parse_resolution(line, dat_format):
109103
raise Exception(f"Unable to extract dimensions from DAT file. Found dimensions: '{dims}'.")
110104
return dims
111105

112-
def __parse_slice_thickness(line, dat_format):
106+
def __parse_slice_thickness(line):
113107
"""Get the x, y, z dimensions of a volume.
114108
115109
Args:
@@ -119,56 +113,31 @@ def __parse_slice_thickness(line, dat_format):
119113
(float, float, float): x, y, z real-world thickness in mm. Otherwise, returns None.
120114
121115
"""
122-
if (dat_format == "Dragonfly"):
123-
pattern = r"\s+<Spacing\s+X=\"(?P<xth>\d+\.\d+)\"\s+Y=\"(?P<yth>\d+\.\d+)\"\s+Z=\"(?P<zth>\d+\.\d+)\""
124-
elif (dat_format == "NSI"):
125-
pattern = r'\w+\:\s+(?P<xth>\d+\.\d+)\s+(?P<yth>\d+\.\d+)\s+(?P<zth>\d+\.\d+)'
126-
116+
pattern = r'\w+\:\s+(?P<xth>\d+\.\d+)\s+(?P<yth>\d+\.\d+)\s+(?P<zth>\d+\.\d+)'
127117
match = re.match(pattern, line, flags=re.IGNORECASE)
128118
if match is not None:
129119
logging.debug(f"Match: {match}")
120+
df = match.groupdict()
130121
dims = [ match.group('xth'), match.group('yth'), match.group('zth') ]
131-
if (dat_format == "Dragonfly"):
132-
# Change Dragonfly thickness units to match NSI format
133-
dims = [ (float(s)*1000) for s in dims ]
134-
elif (dat_format == "NSI"):
135-
dims = [ float(s) for s in dims ]
122+
dims = [ float(s) for s in dims ]
136123
if not dims or len(dims) != 3:
137124
raise Exception(f"Unable to extract slice thickness from DAT file: '{line}'. Found slice thickness: '{dims}'.")
138125
return dims
139126

140-
def __parse_format(line, dat_format):
141-
if (dat_format == "Dragonfly"):
142-
pattern = r"\s+<Format>(?P<format>\w+)<\/Format>$"
143-
elif (dat_format == "NSI"):
144-
pattern = r"^Format\:\s+(?P<format>\w+)$"
145-
127+
def __parse_format(line):
128+
pattern = r"^Format\:\s+(?P<format>\w+)$"
146129
match = re.match(pattern, line, flags=re.IGNORECASE)
147130
if match is not None:
148131
logging.debug(f"Match: {match}")
149132
return match.group('format')
150133

151-
def __parse_object_model(line, dat_format):
152-
if (dat_format == "Dragonfly"):
153-
pattern = r"\s+<Unit>(?P<object_model>\w+)<\/Unit>$"
154-
elif (dat_format == "NSI"):
155-
pattern = r"^ObjectModel\:\s+(?P<object_model>\w+)$"
156-
134+
def __parse_object_model(line):
135+
pattern = r"^ObjectModel\:\s+(?P<object_model>\w+)$"
157136
match = re.match(pattern, line, flags=re.IGNORECASE)
158137
if match is not None:
159138
logging.debug(f"Match: {match}")
160139
return match.group('object_model')
161140

162-
def __is_dragonfly_dat_format(line):
163-
pattern = r"^<\?xml\sversion=\"1\.0\"\?>$"
164-
match = re.match(pattern, line, flags=re.IGNORECASE)
165-
if match is not None:
166-
logging.debug(f"Match: {match}")
167-
return True
168-
169-
170-
171-
172141
def read(fp):
173142
"""Read a .DAT file
174143
Args:
@@ -177,29 +146,24 @@ def read(fp):
177146
dict: contents of .DAT file
178147
"""
179148
data = {}
180-
dat_format = "NSI"
181149
with open(fp, 'r') as ifp:
182150
# Parse the individual lines
183151
for line in ifp.readlines():
184152
line = line.strip()
185-
# Determine if format is NSI .dat or Dragonfly .dat
186-
if (__is_dragonfly_dat_format(line)):
187-
dat_format = "Dragonfly"
188-
189-
if (object_filename := __parse_object_filename(line, dat_format)) is not None:
153+
if (object_filename := __parse_object_filename(line)) is not None:
190154
data['ObjectFileName'] = object_filename
191155

192-
if (resolution := __parse_resolution(line, dat_format)) is not None:
156+
if (resolution := __parse_resolution(line)) is not None:
193157
data['xdim'], data['ydim'], data['zdim'] = resolution
194158
data['dimensions'] = resolution
195159

196-
if (thicknesses := __parse_slice_thickness(line, dat_format)) is not None:
160+
if (thicknesses := __parse_slice_thickness(line)) is not None:
197161
data['x_thickness'], data['y_thickness'], data['z_thickness'] = thicknesses
198162

199-
if (file_format := __parse_format(line, dat_format)) is not None:
163+
if (file_format := __parse_format(line)) is not None:
200164
data['Format'] = file_format
201165

202-
if (object_model := __parse_object_model(line, dat_format)) is not None:
166+
if (object_model := __parse_object_model(line)) is not None:
203167
data['model'] = object_model
204168

205169

0 commit comments

Comments
 (0)