-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
225 additions
and
57 deletions.
There are no files selected for viewing
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
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
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,109 @@ | ||
from .batch import Batch | ||
from .batch_request import BatchRequest | ||
from .neuroglancer.event import step_next | ||
from .neuroglancer.event import wait_for_step | ||
from .neuroglancer.visualize import visualize | ||
from .neuroglancer.add_layer import add_layer | ||
|
||
# from .nodes import BatchProvider | ||
|
||
import neuroglancer | ||
|
||
from abc import ABC | ||
from typing import Optional | ||
|
||
|
||
class Observer(ABC): | ||
def __init__(self, name, pipeline): | ||
self.name = name | ||
self.pipeline = pipeline | ||
|
||
def update(self, request_or_batch: BatchRequest or Batch): | ||
""" | ||
Take a BatchRequest or Batch and update the observer's state with | ||
their contents | ||
""" | ||
pass | ||
|
||
def add_source(self, *args, **kwargs): | ||
""" | ||
Add a source to the observer. This is a no-op for observers that do not | ||
provide an array source. | ||
""" | ||
pass | ||
|
||
|
||
class NeuroglancerObserver(Observer): | ||
def __init__(self, name, pipeline, host="0.0.0.0", port=0): | ||
super().__init__(name, pipeline) | ||
self.host = host | ||
self.port = port | ||
|
||
neuroglancer.set_server_bind_address(self.host, self.port) | ||
self.viewer = neuroglancer.Viewer() | ||
self.viewer.actions.add("continue", step_next) | ||
|
||
with self.viewer.config_state.txn() as s: | ||
s.input_event_bindings.data_view["keyt"] = "continue" | ||
|
||
with self.viewer.txn() as s: | ||
s.layout = neuroglancer.row_layout( | ||
[ | ||
neuroglancer.column_layout( | ||
[ | ||
neuroglancer.LayerGroupViewer(layers=[]), | ||
neuroglancer.LayerGroupViewer(layers=[]), | ||
] | ||
), | ||
] | ||
) | ||
|
||
print(self.viewer) | ||
print("Hit T in neuroglancer viewer to step through the pipeline") | ||
|
||
def update(self, request_or_batch: BatchRequest or Batch, node: Optional = None): | ||
visualize(self.viewer, request_or_batch) | ||
string = self.pipeline.to_string(bold=node) | ||
print( | ||
"\r" | ||
+ ( | ||
"REQUESTING: " | ||
if isinstance(request_or_batch, BatchRequest) | ||
else "PROVIDING: " | ||
) | ||
+ string | ||
+ " " * 2, | ||
end="", | ||
) | ||
# print(self.pipeline.to_string(bold=node)) | ||
wait_for_step() | ||
|
||
def add_source( | ||
self, | ||
array, | ||
name, | ||
): | ||
spatial_dim_names = ["t", "z", "y", "x"] | ||
channel_dim_names = ["b^", "c^"] | ||
opacity = None | ||
shader = None | ||
rgb_channels = None | ||
color = None | ||
visible = True | ||
value_scale_factor = 1.0 | ||
units = "nm" | ||
with self.viewer.txn() as s: | ||
add_layer( | ||
s, | ||
array, | ||
name, | ||
spatial_dim_names, | ||
channel_dim_names, | ||
opacity, | ||
shader, | ||
rgb_channels, | ||
color, | ||
visible, | ||
value_scale_factor, | ||
units, | ||
) |
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,64 @@ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import random | ||
import zarr | ||
import torch | ||
from skimage import data | ||
from skimage import filters | ||
|
||
# make sure we all see the same | ||
torch.manual_seed(1961923) | ||
np.random.seed(1961923) | ||
random.seed(1961923) | ||
|
||
# open a sample image (channels first) | ||
raw_data = data.astronaut().transpose(2, 0, 1) | ||
|
||
# create some dummy "ground-truth" to train on | ||
gt_data = filters.gaussian(raw_data[0], sigma=3.0) > 0.75 | ||
gt_data = gt_data[np.newaxis, :].astype(np.float32) | ||
|
||
# store image in zarr container | ||
f = zarr.open("sample_data.zarr", "w") | ||
f["raw"] = raw_data | ||
f["raw"].attrs["resolution"] = (1, 1) | ||
f["ground_truth"] = gt_data | ||
f["ground_truth"].attrs["resolution"] = (1, 1) | ||
|
||
import gunpowder as gp | ||
|
||
# declare arrays to use in the pipeline | ||
raw = gp.ArrayKey("RAW") | ||
gt = gp.ArrayKey("GT") | ||
|
||
# create "pipeline" consisting only of a data source | ||
source = gp.ZarrSource( | ||
"sample_data.zarr", # the zarr container | ||
{raw: "raw", gt: "ground_truth"}, # which dataset to associate to the array key | ||
{ | ||
raw: gp.ArraySpec(interpolatable=True), | ||
gt: gp.ArraySpec(interpolatable=False), | ||
}, # meta-information | ||
) | ||
pipeline = source | ||
pipeline += gp.Normalize(raw) | ||
pipeline += gp.RandomLocation() | ||
pipeline += gp.DeformAugment( | ||
gp.Coordinate(5, 5), | ||
gp.Coordinate(2, 2), | ||
graph_raster_voxel_size=gp.Coordinate(1, 1), | ||
) | ||
|
||
# formulate a request for "raw" | ||
request = gp.BatchRequest() | ||
request.add(raw, gp.Coordinate(64, 64), gp.Coordinate(1, 1)) | ||
request.add(gt, gp.Coordinate(32, 32), gp.Coordinate(1, 1)) | ||
|
||
# build the pipeline... | ||
with gp.build_neuroglancer(pipeline): | ||
for _ in range(10): | ||
# ...and request a batch | ||
batch = pipeline.request_batch(request) | ||
|
||
# show the content of the batch | ||
print(f"batch returned: {batch}") |