Skip to content

Commit

Permalink
file: support loading a misclassified Kymo
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Oct 27, 2023
1 parent 1b86b19 commit a9c3ff4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Added more options for plotting color channels for images:
* Shortcuts `"r"`, `"g"`, and `"b"` can now be used for plotting single color channels in addition to `"red"`, `"green"`, and `"blue"`.
* Two-channel visualizations can be plotted using `"rg"`, `"gb"`, or `"rb"`.
* Handle kymographs that were accidentally stored in the `Scan` field.

#### Improvements

Expand Down
11 changes: 10 additions & 1 deletion lumicks/pylake/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,16 @@ def try_from_dataset(*args):
@property
def kymos(self) -> Dict[str, Kymo]:
"""Kymos stored in the file"""
return self._get_object_dictionary("Kymograph", Kymo)

# Due to an error in an earlier version of Bluelake, some Kymographs were stored in the
# `Scan` field. This reads those using a fallback mechanism.
scan_kymos = {
key: item
for key, item in self._get_object_dictionary("Scan", Kymo).items()
if item._metadata.num_axes == 1
}

return scan_kymos | self._get_object_dictionary("Kymograph", Kymo)

@property
def point_scans(self) -> Dict[str, Scan]:
Expand Down
2 changes: 2 additions & 0 deletions lumicks/pylake/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Scan(ConfocalImage, VideoExport, FrameIndex):

def __init__(self, name, file, start, stop, metadata):
super().__init__(name, file, start, stop, metadata)
if self._metadata.num_axes == 1:
raise RuntimeError("1D scans are not supported")
if self._metadata.num_axes > 2:
raise RuntimeError("3D scans are not supported")

Expand Down
16 changes: 16 additions & 0 deletions lumicks/pylake/tests/test_file/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,19 @@ def h5_file_invalid_version(tmpdir_factory):
mock_file.attrs["File format version"] = 254

return mock_file


@pytest.fixture(scope="module", params=[MockDataFile_v2])
def h5_kymo_as_scan(tmpdir_factory, request):
mock_class = request.param

tmpdir = tmpdir_factory.mktemp("pylake")
mock_file = mock_class(tmpdir.join("%s.h5" % mock_class.__class__.__name__))
mock_file.write_metadata()

json_kymo = generate_scan_json([{"axis": 1, "num of pixels": 4, "pixel size (nm)": 191.0}])
ds = mock_file.make_json_data("Scan", "Kymo1", json_kymo)
ds.attrs["Start time (ns)"] = np.int64(20e9)
ds.attrs["Stop time (ns)"] = np.int64(100e9)

return mock_file.file
12 changes: 12 additions & 0 deletions lumicks/pylake/tests/test_file/test_file_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,15 @@ def test_notes(h5_file):
assert note.text == "Note content"
assert note.start == 100
assert note.stop == 100


def test_kymos_in_scans(h5_kymo_as_scan):
"""Tests whether Kymos accidentally put in Scan are loaded correctly"""
f = pylake.File.from_h5py(h5_kymo_as_scan)

assert len(f.kymos) == 1
assert f.kymos["Kymo1"].name == "Kymo1"

with pytest.warns():
scan_dict = f.scans
assert not scan_dict

0 comments on commit a9c3ff4

Please sign in to comment.