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/readers.py b/pySNOM/readers.py index 04d14fb..382b575 100644 --- a/pySNOM/readers.py +++ b/pySNOM/readers.py @@ -340,3 +340,49 @@ 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))) + + try: + x = np.array([float(xi) for xi in x]) + except ValueError: + x = None + + return image_stack, x 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()