Skip to content

Commit 71f9b2c

Browse files
Add new sample data and test for file utils
1 parent 1cc595e commit 71f9b2c

File tree

4 files changed

+68
-10
lines changed

4 files changed

+68
-10
lines changed

synaptic_reconstruction/file_utils.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ def read_voxel_size(path: str) -> Dict[str, float] | None:
5959
return voxel_size
6060

6161

62-
# TODO: double check axis ordering with elf
6362
def read_mrc(path: str) -> Tuple[np.ndarray, Dict[str, float]]:
6463
"""Read data and voxel size from mrc/rec file.
6564
@@ -73,11 +72,8 @@ def read_mrc(path: str) -> Tuple[np.ndarray, Dict[str, float]]:
7372
with mrcfile.open(path, permissive=True) as mrc:
7473
voxel_size = _parse_voxel_size(mrc.voxel_size)
7574
data = np.asarray(mrc.data[:])
75+
assert data.ndim in (2, 3)
7676

7777
# Transpose the data to match python axis order.
78-
if data.ndim == 3:
79-
data = np.flip(data, axis=1)
80-
else:
81-
data = np.flip(data, axis=0)
82-
78+
data = np.flip(data, axis=1) if data.ndim == 3 else np.flip(data, axis=0)
8379
return data, voxel_size

synaptic_reconstruction/napari.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ contributions:
2727
- id: synaptic_reconstruction.sample_data_tem_2d
2828
python_name: synaptic_reconstruction.sample_data:sample_data_tem_2d
2929
title: Load TEM 2D sample data
30+
- id: synaptic_reconstruction.sample_data_tem_tomo
31+
python_name: synaptic_reconstruction.sample_data:sample_data_tem_tomo
32+
title: Load TEM Tomo sample data
3033

3134
readers:
3235
- command: synaptic_reconstruction.file_reader
@@ -50,3 +53,6 @@ contributions:
5053
- command: synaptic_reconstruction.sample_data_tem_2d
5154
display_name: TEM 2D Sample Data
5255
key: synapse-net-tem-2d
56+
- command: synaptic_reconstruction.sample_data_tem_tomo
57+
display_name: TEM Tomo Sample Data
58+
key: synapse-net-tem-tomo

synaptic_reconstruction/sample_data.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ def get_sample_data(name: str) -> str:
88
"""Get the filepath to SynapseNet sample data, stored as mrc file.
99
1010
Args:
11-
name: The name of the sample data. Currently, we only provide the 'tem_2d' sample data.
11+
name: The name of the sample data. Currently, we only provide 'tem_2d' and 'tem_tomo'.
1212
1313
Returns:
1414
The filepath to the downloaded sample data.
1515
"""
1616
registry = {
1717
"tem_2d.mrc": "3c6f9ff6d7673d9bf2fd46c09750c3c7dbb8fa1aa59dcdb3363b65cc774dcf28",
18+
"tem_tomo.mrc": "24af31a10761b59fa6ad9f0e763f8f084304e4f31c59b482dd09dde8cd443ed7",
1819
}
1920
urls = {
2021
"tem_2d.mrc": "https://owncloud.gwdg.de/index.php/s/5sAQ0U4puAspcHg/download",
22+
"tem_tomo.mrc": "https://owncloud.gwdg.de/index.php/s/NeP7gOv76Vj26lm/download",
2123
}
2224
key = f"{name}.mrc"
2325

@@ -36,9 +38,17 @@ def get_sample_data(name: str) -> str:
3638
return file_path
3739

3840

39-
def sample_data_tem_2d():
40-
file_path = get_sample_data("tem_2d")
41+
def _sample_data(name):
42+
file_path = get_sample_data(name)
4143
data, voxel_size = read_mrc(file_path)
4244
metadata = {"file_path": file_path, "voxel_size": voxel_size}
43-
add_image_kwargs = {"name": "tem_2d", "metadata": metadata, "colormap": "gray"}
45+
add_image_kwargs = {"name": name, "metadata": metadata, "colormap": "gray"}
4446
return [(data, add_image_kwargs)]
47+
48+
49+
def sample_data_tem_2d():
50+
return _sample_data("tem_2d")
51+
52+
53+
def sample_data_tem_tomo():
54+
return _sample_data("tem_tomo")

test/test_file_utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
3+
import numpy as np
4+
from elf.io import open_file
5+
from synaptic_reconstruction.sample_data import get_sample_data
6+
7+
8+
class TestFileUtils(unittest.TestCase):
9+
10+
def test_read_mrc_2d(self):
11+
from synaptic_reconstruction.file_utils import read_mrc
12+
13+
file_path = get_sample_data("tem_2d")
14+
data, voxel_size = read_mrc(file_path)
15+
16+
with open_file(file_path, "r") as f:
17+
data_exp = f["data"][:]
18+
19+
self.assertTrue(data.shape, data_exp.shape)
20+
self.assertTrue(np.allclose(data, data_exp))
21+
22+
resolution = 0.592
23+
self.assertTrue(np.isclose(voxel_size["x"], resolution))
24+
self.assertTrue(np.isclose(voxel_size["y"], resolution))
25+
self.assertTrue(np.isclose(voxel_size["z"], 0.0))
26+
27+
def test_read_mrc_3d(self):
28+
from synaptic_reconstruction.file_utils import read_mrc
29+
30+
file_path = get_sample_data("tem_tomo")
31+
data, voxel_size = read_mrc(file_path)
32+
33+
with open_file(file_path, "r") as f:
34+
data_exp = f["data"][:]
35+
36+
self.assertTrue(data.shape, data_exp.shape)
37+
self.assertTrue(np.allclose(data, data_exp))
38+
39+
resolution = 1.554
40+
self.assertTrue(np.isclose(voxel_size["x"], resolution))
41+
self.assertTrue(np.isclose(voxel_size["y"], resolution))
42+
self.assertTrue(np.isclose(voxel_size["z"], resolution))
43+
44+
45+
if __name__ == "__main__":
46+
unittest.main()

0 commit comments

Comments
 (0)