Skip to content

Commit

Permalink
Merge pull request #755 from borondics/real_coordinates
Browse files Browse the repository at this point in the history
[ENH] Real space coordinates for GSF images
  • Loading branch information
markotoplak authored Oct 4, 2024
2 parents 90130cd + edddbca commit 22a97b3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 26 deletions.
12 changes: 8 additions & 4 deletions orangecontrib/spectroscopy/io/gsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@ def reader_gsf(file_path):

meta["XRes"] = XR = int(meta["XRes"])
meta["YRes"] = YR = int(meta["YRes"])
meta["XReal"] = float(meta.get("XReal", 1))
meta["YReal"] = float(meta.get("YReal", 1))
meta["XOffset"] = float(meta.get("XOffset", 0))
meta["YOffset"] = float(meta.get("YOffset", 0))
meta["XReal"] = XReal = float(meta.get("XReal", 1))
meta["YReal"] = YReal = float(meta.get("YReal", 1))
meta["XOffset"] = XOffset = float(meta.get("XOffset", 0))
meta["YOffset"] = YOffset = float(meta.get("YOffset", 0))
meta["Title"] = meta.get("Title", None)
meta["XYUnits"] = meta.get("XYUnits", None)
meta["ZUnits"] = meta.get("ZUnits", None)

X = np.fromfile(f, dtype='float32', count=XR*YR).reshape(XR, YR)

XRr = np.arange(XR)
# TODO change this to the NeaSCAN orientation
YRr = np.arange(YR-1, -1, -1) # needed to have the same orientation as in Gwyddion

XRr = XOffset*1E6 + (XReal*1E6/XR) * XRr
YRr = YOffset*1E6 + (YReal*1E6/YR) * YRr

X = X.reshape((meta["YRes"], meta["XRes"]) + (1,))

return X, XRr, YRr
Expand Down
1 change: 1 addition & 0 deletions orangecontrib/spectroscopy/io/neaspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ def read_spectra(self):
features, final_data, meta_data = _spectra_from_image(X, np.array([1]), XRr, YRr)

meta_data.attributes["measurement.signaltype"] = signal_type
# TODO add all the meta info here from the Gwyddion header

return features, final_data, meta_data

Expand Down
30 changes: 17 additions & 13 deletions orangecontrib/spectroscopy/tests/test_owhyper.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,41 +234,43 @@ def test_select_all(self):
self.assertIsNone(out, None)

# select specific points
self.widget.imageplot.select_square(QPointF(9.4, 9.4), QPointF(11.6, 10.6))
self.widget.imageplot.select_square(QPointF(53.20, 30.0), QPointF(53.21, 30.03))
out = self.get_output("Selection")
np.testing.assert_equal(out.metas, [[10, 10], [11, 10]])
np.testing.assert_almost_equal(out.metas,
[[53.2043, 30.0185], [53.2043, 30.0085]],
decimal=3)
np.testing.assert_equal([o[out.domain["Group"]].value for o in out],
["G1", "G1"])

def test_select_polygon_as_rectangle(self):
# rectangle and a polygon need to give the same results
self.send_signal("Data", self.whitelight)
wait_for_image(self.widget)
self.widget.imageplot.select_square(QPointF(5, 5), QPointF(15, 10))
self.widget.imageplot.select_square(QPointF(53, 30), QPointF(54, 31))
out = self.get_output("Selection")
self.widget.imageplot.select_polygon([QPointF(5, 5), QPointF(15, 5), QPointF(15, 10),
QPointF(5, 10), QPointF(5, 5)])
self.widget.imageplot.select_polygon([QPointF(53, 30), QPointF(53, 31), QPointF(54, 31),
QPointF(54, 30), QPointF(53, 30)])
outpoly = self.get_output("Selection")
self.assertEqual(list(out), list(outpoly))

def test_select_click(self):
self.send_signal("Data", self.whitelight)
wait_for_image(self.widget)
self.widget.imageplot.select_by_click(QPointF(1, 2))
self.widget.imageplot.select_by_click(QPointF(53.2443, 30.6984))
out = self.get_output("Selection")
np.testing.assert_equal(out.metas, [[1, 2]])
np.testing.assert_almost_equal(out.metas, [[53.2443, 30.6984]], decimal=3)

def test_select_click_multiple_groups(self):
data = self.whitelight
self.send_signal("Data", data)
wait_for_image(self.widget)
self.widget.imageplot.select_by_click(QPointF(1, 2))
self.widget.imageplot.select_by_click(QPointF(53.2, 30))
with hold_modifiers(self.widget, Qt.ShiftModifier):
self.widget.imageplot.select_by_click(QPointF(2, 2))
self.widget.imageplot.select_by_click(QPointF(53.4, 30))
with hold_modifiers(self.widget, Qt.ShiftModifier):
self.widget.imageplot.select_by_click(QPointF(3, 2))
self.widget.imageplot.select_by_click(QPointF(53.6, 30))
with hold_modifiers(self.widget, Qt.ControlModifier):
self.widget.imageplot.select_by_click(QPointF(4, 2))
self.widget.imageplot.select_by_click(QPointF(53.8, 30))
out = self.get_output(self.widget.Outputs.annotated_data)
self.assertEqual(len(out), 20000) # have a data table at the output
newvars = out.domain.variables + out.domain.metas
Expand All @@ -278,15 +280,17 @@ def test_select_click_multiple_groups(self):
out = out[np.asarray(np.flatnonzero(
out.transform(Orange.data.Domain([group_at])).X != unselected))]
self.assertEqual(len(out), 4)
np.testing.assert_equal([o["map_x"].value for o in out], [1, 2, 3, 4])
np.testing.assert_almost_equal([o["map_x"].value for o in out],
[53.1993, 53.3993, 53.5993, 53.7993],
decimal=3)
np.testing.assert_equal([o[group_at].value for o in out], ["G1", "G2", "G3", "G3"])
out = self.get_output(self.widget.Outputs.selected_data)
np.testing.assert_equal([o[out.domain["Group"]].value for o in out],
["G1", "G2", "G3", "G3"])

# remove one element
with hold_modifiers(self.widget, Qt.AltModifier):
self.widget.imageplot.select_by_click(QPointF(1, 2))
self.widget.imageplot.select_by_click(QPointF(53.2, 30))
out = self.get_output(self.widget.Outputs.selected_data)
np.testing.assert_equal(len(out), 3)
np.testing.assert_equal([o[out.domain["Group"]].value for o in out],
Expand Down
13 changes: 7 additions & 6 deletions orangecontrib/spectroscopy/tests/test_owspectralseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,20 @@ def test_unknown(self):

def test_select_click(self):
self.send_signal("Data", self.whitelight)
self.widget.imageplot.select_by_click(QPointF(1, 2))
self.widget.imageplot.select_by_click(QPointF(0, 1))
# work by indices
out = self.get_output("Selection")
np.testing.assert_equal(out.metas[:, 0], 1)
np.testing.assert_equal(out.metas[:, 1], 99)
np.testing.assert_almost_equal(out.metas[:, 0], 53.0693, decimal=3)
np.testing.assert_almost_equal(out.metas[:, 1], 30.7085, decimal=3)
np.testing.assert_equal(out.Y, 0) # selection group
# select a feature
self.widget.imageplot.attr_x = "map_x"
self.widget.imageplot.update_attr()
self.widget.imageplot.select_by_click(QPointF(1, 2))
self.widget.imageplot.select_by_click(QPointF(53.1, 1))
out = self.get_output("Selection")
np.testing.assert_equal(out.metas[:, 0], 1)
np.testing.assert_equal(out.metas[:, 1], list(reversed(np.arange(100))))
np.testing.assert_almost_equal(out.metas[:, 0], 53.099327, decimal=3)
np.testing.assert_almost_equal(out.metas[:, 1],
self.whitelight[::200].metas[:, 1], decimal=3)
np.testing.assert_equal(out.Y, 0) # selection group

def test_single_update_view(self):
Expand Down
9 changes: 6 additions & 3 deletions orangecontrib/spectroscopy/tests/test_readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,16 @@ def test_open_2d(self):
self.assertEqual(data.X.shape, (20000, 1))
# check some pixel vaules
self.assertAlmostEqual(data.X[235,0], 1.2788502, 7)
np.testing.assert_array_equal(data.metas[235], [35, 98])
np.testing.assert_almost_equal(data.metas[235],
[53.2443, 30.6984], decimal=3)

self.assertAlmostEqual(data.X[1235,0], 1.2770579, 7)
np.testing.assert_array_equal(data.metas[1235], [35, 93])
np.testing.assert_almost_equal(data.metas[1235],
[53.2443, 30.6484], decimal=3)

self.assertAlmostEqual(data.X[11235,0], 1.2476133, 7)
np.testing.assert_array_equal(data.metas[11235], [35, 43])
np.testing.assert_almost_equal(data.metas[11235],
[53.2443, 30.1484], decimal=3)


class TestNea(unittest.TestCase):
Expand Down

0 comments on commit 22a97b3

Please sign in to comment.