Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pySNOM/datasets/test_xyz_read.xyz
Original file line number Diff line number Diff line change
@@ -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
46 changes: 46 additions & 0 deletions pySNOM/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 9 additions & 0 deletions pySNOM/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()