Skip to content

Commit

Permalink
Add new sample data and test for file utils
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Dec 6, 2024
1 parent 1cc595e commit 71f9b2c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 10 deletions.
8 changes: 2 additions & 6 deletions synaptic_reconstruction/file_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def read_voxel_size(path: str) -> Dict[str, float] | None:
return voxel_size


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

# Transpose the data to match python axis order.
if data.ndim == 3:
data = np.flip(data, axis=1)
else:
data = np.flip(data, axis=0)

data = np.flip(data, axis=1) if data.ndim == 3 else np.flip(data, axis=0)
return data, voxel_size
6 changes: 6 additions & 0 deletions synaptic_reconstruction/napari.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ contributions:
- id: synaptic_reconstruction.sample_data_tem_2d
python_name: synaptic_reconstruction.sample_data:sample_data_tem_2d
title: Load TEM 2D sample data
- id: synaptic_reconstruction.sample_data_tem_tomo
python_name: synaptic_reconstruction.sample_data:sample_data_tem_tomo
title: Load TEM Tomo sample data

readers:
- command: synaptic_reconstruction.file_reader
Expand All @@ -50,3 +53,6 @@ contributions:
- command: synaptic_reconstruction.sample_data_tem_2d
display_name: TEM 2D Sample Data
key: synapse-net-tem-2d
- command: synaptic_reconstruction.sample_data_tem_tomo
display_name: TEM Tomo Sample Data
key: synapse-net-tem-tomo
18 changes: 14 additions & 4 deletions synaptic_reconstruction/sample_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ def get_sample_data(name: str) -> str:
"""Get the filepath to SynapseNet sample data, stored as mrc file.
Args:
name: The name of the sample data. Currently, we only provide the 'tem_2d' sample data.
name: The name of the sample data. Currently, we only provide 'tem_2d' and 'tem_tomo'.
Returns:
The filepath to the downloaded sample data.
"""
registry = {
"tem_2d.mrc": "3c6f9ff6d7673d9bf2fd46c09750c3c7dbb8fa1aa59dcdb3363b65cc774dcf28",
"tem_tomo.mrc": "24af31a10761b59fa6ad9f0e763f8f084304e4f31c59b482dd09dde8cd443ed7",
}
urls = {
"tem_2d.mrc": "https://owncloud.gwdg.de/index.php/s/5sAQ0U4puAspcHg/download",
"tem_tomo.mrc": "https://owncloud.gwdg.de/index.php/s/NeP7gOv76Vj26lm/download",
}
key = f"{name}.mrc"

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


def sample_data_tem_2d():
file_path = get_sample_data("tem_2d")
def _sample_data(name):
file_path = get_sample_data(name)
data, voxel_size = read_mrc(file_path)
metadata = {"file_path": file_path, "voxel_size": voxel_size}
add_image_kwargs = {"name": "tem_2d", "metadata": metadata, "colormap": "gray"}
add_image_kwargs = {"name": name, "metadata": metadata, "colormap": "gray"}
return [(data, add_image_kwargs)]


def sample_data_tem_2d():
return _sample_data("tem_2d")


def sample_data_tem_tomo():
return _sample_data("tem_tomo")
46 changes: 46 additions & 0 deletions test/test_file_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import unittest

import numpy as np
from elf.io import open_file
from synaptic_reconstruction.sample_data import get_sample_data


class TestFileUtils(unittest.TestCase):

def test_read_mrc_2d(self):
from synaptic_reconstruction.file_utils import read_mrc

file_path = get_sample_data("tem_2d")
data, voxel_size = read_mrc(file_path)

with open_file(file_path, "r") as f:
data_exp = f["data"][:]

self.assertTrue(data.shape, data_exp.shape)
self.assertTrue(np.allclose(data, data_exp))

resolution = 0.592
self.assertTrue(np.isclose(voxel_size["x"], resolution))
self.assertTrue(np.isclose(voxel_size["y"], resolution))
self.assertTrue(np.isclose(voxel_size["z"], 0.0))

def test_read_mrc_3d(self):
from synaptic_reconstruction.file_utils import read_mrc

file_path = get_sample_data("tem_tomo")
data, voxel_size = read_mrc(file_path)

with open_file(file_path, "r") as f:
data_exp = f["data"][:]

self.assertTrue(data.shape, data_exp.shape)
self.assertTrue(np.allclose(data, data_exp))

resolution = 1.554
self.assertTrue(np.isclose(voxel_size["x"], resolution))
self.assertTrue(np.isclose(voxel_size["y"], resolution))
self.assertTrue(np.isclose(voxel_size["z"], resolution))


if __name__ == "__main__":
unittest.main()

0 comments on commit 71f9b2c

Please sign in to comment.