Skip to content

Commit 2410e7d

Browse files
committed
Edit nexus reader to be slice by slice
1 parent 4ea85f1 commit 2410e7d

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/idvc/io.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,20 +1226,38 @@ def save_nxs_as_raw(nexus_file, dataset_path, raw_file):
12261226
Parameters:
12271227
-----------
12281228
nexus_file: Path to the input NeXus file.
1229+
dataset_path: str
1230+
Internal path in the NeXus file to the dataset.
12291231
raw_file: Path to the output RAW file.
12301232
"""
12311233
with h5py.File(nexus_file, "r") as f:
12321234
if dataset_path not in f:
12331235
raise ValueError(f"Dataset '{dataset_path}' not found in {nexus_file}.")
12341236
data = f[dataset_path]
12351237
original_dtype = data.dtype
1236-
m, M = numpy.nanmin(data), numpy.nanmax(data)
1237-
if M - m == 0:
1238-
raise ValueError("Data is constant, cannot scale to uint16")
12391238

1240-
if original_dtype not in [numpy.uint8, numpy.uint16, numpy.int8, numpy.int16]:
1239+
if original_dtype in [numpy.uint8, numpy.uint16, numpy.int8, numpy.int16]:
1240+
with open(os.path.abspath(raw_file), 'wb') as f:
1241+
for i in range(data.shape[0]):
1242+
slice_data = data[i:i+1]
1243+
slice_data.tofile(f)
1244+
return
1245+
1246+
else:
1247+
for i in range(data.shape[0]):
1248+
slice_data = data[i:i+1]
1249+
if i == 0:
1250+
m, M = numpy.nanmin(slice_data), numpy.nanmax(slice_data)
1251+
else:
1252+
m = min(m, numpy.nanmin(slice_data))
1253+
M = max(M, numpy.nanmax(slice_data))
1254+
if M - m == 0:
1255+
raise ValueError("Data is constant, cannot scale to uint16")
1256+
12411257
convert_to_dtype = numpy.uint16
1242-
data = ((data.astype(numpy.float32) - m) / (M - m)) * numpy.iinfo(convert_to_dtype).max
1243-
1244-
data.astype(numpy.uint16).tofile(raw_file)
1258+
with open(os.path.abspath(raw_file), 'wb') as f:
1259+
for i in range(data.shape[0]):
1260+
slice_data = data[i:i+1]
1261+
slice_data = ((slice_data.astype(numpy.float32) - m) / (M - m)) * numpy.iinfo(convert_to_dtype).max
1262+
slice_data.astype(numpy.uint16).tofile(f)
12451263

0 commit comments

Comments
 (0)