-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #333 from alan-turing-institute/development
MILESTONE: Merging `development` into `main` 🎉
- Loading branch information
Showing
53 changed files
with
2,895 additions
and
1,033 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,94 @@ | ||
import click | ||
import napari | ||
|
||
import pandas as pd | ||
import numpy as np | ||
|
||
from grace.base import GraphAttrs | ||
from grace.io.image_dataset import mrc_reader | ||
from pathlib import Path | ||
|
||
from grace.base import GraphAttrs | ||
from grace.io.image_dataset import FILETYPES | ||
|
||
# Expects the image data & H5 node positions in the same folder. | ||
# Use identical naming convention for files & specify whole path to mrc file: | ||
# e.g. /Users/kulicna/Desktop/dataset/shape_squares/MRC_Synthetic_File_000.mrc | ||
|
||
IMAGE_PATH = Path( | ||
input( | ||
"Enter absolute path to your file " | ||
"(e.g. /Users/path/to/your/data/image.mrc, omit ''): " | ||
) | ||
# Define a click command to input the file name directly: | ||
@click.command(name="Napari Annotator") | ||
@click.option( | ||
"--image_path", | ||
type=click.Path(exists=True), | ||
help="Path to the image to open in napari annotator", | ||
) | ||
NODES_PATH = Path(str(IMAGE_PATH).replace(".mrc", ".h5")) | ||
@click.option( | ||
"--show_features", | ||
type=bool, | ||
default=True, | ||
help="Show feature representation of individual nodes", | ||
) | ||
def run_napari_annotator( | ||
image_path: str | Path, | ||
show_features: bool = False, | ||
) -> None: | ||
"""Function to open an image & annotate it in napari. | ||
image_data = mrc_reader(IMAGE_PATH) | ||
nodes_data = pd.read_hdf(NODES_PATH) | ||
Parameters | ||
---------- | ||
image_path : str | Path | ||
Absolute filename of the image to be opened. | ||
Expects the image data & H5 node positions in the same folder. | ||
Use identical naming convention for these files to pair them up. | ||
show_features : bool | ||
Whether to display node features stored in the `h5` file. | ||
Defaults to False. | ||
points = np.asarray(nodes_data.loc[:, [GraphAttrs.NODE_Y, GraphAttrs.NODE_X]]) | ||
# features = { | ||
# GraphAttrs.NODE_FEATURES: | ||
# [np.squeeze(f.numpy()) for f in nodes_data.loc[:, "features"]] | ||
# } | ||
features = None | ||
data_name = f"{IMAGE_PATH.stem}" | ||
Notes | ||
----- | ||
- expected file organisation: | ||
/path/to/your/image/MRC_Synthetic_File_000.mrc | ||
...identical to... | ||
/path/to/your/nodes/MRC_Synthetic_File_000.h5 | ||
""" | ||
# Process the image data + load nodes: | ||
suffix = str(image_path).split(".")[-1] | ||
assert suffix in FILETYPES, f"Choose these filetypes: {FILETYPES.keys()}" | ||
|
||
mn, mx = np.min(image_data), np.max(image_data) | ||
image_reader = FILETYPES[suffix] | ||
image_data = image_reader(Path(image_path)) | ||
|
||
viewer = napari.Viewer() | ||
img_layer = viewer.add_image( | ||
image_data, name=data_name, contrast_limits=(mn, mx) | ||
) | ||
pts_layer = viewer.add_points( | ||
points, features=features, size=32, name=f"nodes_{data_name}" | ||
) | ||
nodes_path = image_path.replace(".mrc", ".h5") | ||
nodes_data = pd.read_hdf(Path(nodes_path)) | ||
|
||
data_name = f"{Path(image_path).stem}" | ||
|
||
# Start a napari window: | ||
viewer = napari.Viewer() | ||
mn, mx = np.min(image_data), np.max(image_data) | ||
viewer.add_image(image_data, name=data_name, contrast_limits=(mn, mx)) | ||
|
||
# Locate the nodes as points: | ||
points = np.asarray( | ||
nodes_data.loc[:, [GraphAttrs.NODE_Y, GraphAttrs.NODE_X]] | ||
) | ||
|
||
# Process the node features: | ||
if show_features is True: | ||
features = { | ||
"node_central_pixel_value": [ | ||
image_data[int(point[0]), int(point[1])] for point in points | ||
] | ||
} | ||
else: | ||
features = None | ||
|
||
viewer.add_points( | ||
points, features=features, size=32, name=f"nodes_{data_name}" | ||
) | ||
|
||
viewer.window.add_plugin_dock_widget( | ||
plugin_name="grace", widget_name="GRACE" | ||
) | ||
napari.run() | ||
|
||
_, widget = viewer.window.add_plugin_dock_widget( | ||
plugin_name="grace", widget_name="GRACE" | ||
) | ||
|
||
if __name__ == "__main__": | ||
# The napari event loop needs to be run under here to allow the window | ||
# to be spawned from a Python script | ||
napari.run() | ||
run_napari_annotator() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from grace import styling # noqa: F401 |
Oops, something went wrong.