Skip to content

Commit 4f4dc66

Browse files
committed
Refactor write_data in TextImageFormat to be more generic and improve error handling
1 parent df844c2 commit 4f4dc66

1 file changed

Lines changed: 9 additions & 12 deletions

File tree

sigima_/io/image/formats.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,23 +105,20 @@ def read_data(filename: str) -> np.ndarray:
105105

106106
@staticmethod
107107
def write_data(filename: str, data: np.ndarray) -> None:
108-
"""Write data to file
108+
"""Write data to file.
109109
110110
Args:
111-
filename: File name
112-
data: Image array data
111+
filename: File name.
112+
data: Image array data.
113113
"""
114-
if data.dtype in (
115-
np.int8,
116-
np.uint8,
117-
np.int16,
118-
np.uint16,
119-
np.int32,
120-
np.uint32,
121-
):
114+
if np.issubdtype(data.dtype, np.integer):
122115
fmt = "%d"
123-
else:
116+
elif np.issubdtype(data.dtype, np.floating):
124117
fmt = "%.18e"
118+
else:
119+
raise NotImplementedError(
120+
f"Writing data of type {data.dtype} to text file is not supported."
121+
)
125122
ext = osp.splitext(filename)[1]
126123
if ext.lower() in (".txt", ".asc", ""):
127124
np.savetxt(filename, data, fmt=fmt)

0 commit comments

Comments
 (0)