From c30edf5173823176fb1516208680d6087deb464c Mon Sep 17 00:00:00 2001 From: Gergely Nemeth Date: Wed, 21 May 2025 13:45:21 +0200 Subject: [PATCH 1/3] XYZ to imagestack reader --- pySNOM/readers.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pySNOM/readers.py b/pySNOM/readers.py index 04d14fb..f339407 100644 --- a/pySNOM/readers.py +++ b/pySNOM/readers.py @@ -340,3 +340,44 @@ def read(self): params["Scan"] = "Fourier Scan" return data, params + +class ImageStackXYZReader(Reader): + """Reads a list of images from the subfolders of the specified folder by loading the files that contain the pattern string int the filename""" + + def __init__(self, fullfilepath=None): + super().__init__(fullfilepath) + + def read(self): + + if self.filename is None: + raise ValueError('No folder specified') + else: + with open(self.filename, encoding="utf8") as f: + x = next(f) # header + x = x.strip() + x = x.split("\t") + + f.seek(0) + next(f) + datacols = np.arange(2, len(x)+2) + C_data = np.loadtxt(f, dtype="float", usecols=datacols) + + f.seek(0) + next(f) + + metacols = np.arange(0, 2) + meta = np.loadtxt( + f, + dtype={"names": ('Row','Column'), "formats": (float,float)}, + usecols=metacols, + ) + + Max_row = len(np.unique(meta["Row"])) + Max_col = len(np.unique(meta["Column"])) + Max_omega = len(x) + + image_stack = [] + for i in range(Max_omega): + image_stack.append(np.reshape(C_data[:, i], (Max_col, Max_row))) + + return image_stack, np.array(x) From fbadbf8240dc316aedb61d8d09bbeddc14c7e5a2 Mon Sep 17 00:00:00 2001 From: Gergely Nemeth Date: Wed, 21 May 2025 14:44:29 +0200 Subject: [PATCH 2/3] Convert x to float --- pySNOM/readers.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pySNOM/readers.py b/pySNOM/readers.py index f339407..382b575 100644 --- a/pySNOM/readers.py +++ b/pySNOM/readers.py @@ -380,4 +380,9 @@ def read(self): for i in range(Max_omega): image_stack.append(np.reshape(C_data[:, i], (Max_col, Max_row))) - return image_stack, np.array(x) + try: + x = np.array([float(xi) for xi in x]) + except ValueError: + x = None + + return image_stack, x From a03341d7bd5b71a0c3ca083a80fba1a94b8f5c9a Mon Sep 17 00:00:00 2001 From: Gergely Nemeth Date: Wed, 21 May 2025 14:44:51 +0200 Subject: [PATCH 3/3] Tests adn testdata --- pySNOM/datasets/test_xyz_read.xyz | 6 ++++++ pySNOM/tests/test_readers.py | 9 +++++++++ 2 files changed, 15 insertions(+) create mode 100644 pySNOM/datasets/test_xyz_read.xyz diff --git a/pySNOM/datasets/test_xyz_read.xyz b/pySNOM/datasets/test_xyz_read.xyz new file mode 100644 index 0000000..f8eaa32 --- /dev/null +++ b/pySNOM/datasets/test_xyz_read.xyz @@ -0,0 +1,6 @@ + 225 225.818 226.636 +0 0 36.8115 36.7974 36.7867 +1 0 37.6639 37.6841 37.7868 +2 0 37.4873 37.4568 34.9154 +3 0 36.66 36.6116 36.6104 +4 0 35.8762 35.8739 35.8703 diff --git a/pySNOM/tests/test_readers.py b/pySNOM/tests/test_readers.py index 20e1c9d..510860a 100644 --- a/pySNOM/tests/test_readers.py +++ b/pySNOM/tests/test_readers.py @@ -73,6 +73,15 @@ def test_legacy_nea_reader(self): np.testing.assert_array_equal(list(data.keys())[-1], "M") np.testing.assert_string_equal(params["Scan"], "Fourier Scan") + def test_xyz_reader(self): + f = "datasets/test_xyz_read.xyz" + file_reader = readers.ImageStackXYZReader(os.path.join(pySNOM.__path__[0], f)) + data, wn = file_reader.read() + + np.testing.assert_array_equal(wn, np.array([225, 225.818, 226.636])) + np.testing.assert_equal(np.shape(data), (3, 1, 5)) + np.testing.assert_array_equal(data[2], [[36.7867, 37.7868, 34.9154, 36.6104, 35.8703]]) + if __name__ == "__main__": unittest.main()