Skip to content

Commit

Permalink
Add optional include_archived parameter to layer_names,
Browse files Browse the repository at this point in the history
image_layers, segmentation_layers, and annotation_layers.
Fixes CAVEconnectome#46.
  • Loading branch information
JoeStrout committed Aug 27, 2024
1 parent 1780e15 commit b29e3e5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
28 changes: 20 additions & 8 deletions src/nglui/parser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,80 @@ def _is_spelunker_state(state):
return "dimension" in state.keys()


def layer_names(state):
def layer_names(state, include_archived = True):
"""Get all layer names in the state
Parameters
----------
state : dict
Neuroglancer state as a JSON dict
include_archived : bool
Whether to include archived layers
Returns
-------
names : list
List of layer names
"""
return [l["name"] for l in state["layers"]]
return [l["name"] for l in state["layers"] if \
(include_archived or l.get('archived', False) == False)]


def image_layers(state):
def image_layers(state, include_archived = True):
"""Get all image layer names in the state
Parameters
----------
state : dict
Neuroglancer state as a JSON dict
include_archived : bool
Whether to include archived layers
Returns
-------
names : list
List of layer names
"""
return [l["name"] for l in state["layers"] if l["type"] == "image"]
return [l["name"] for l in state["layers"] if l["type"] == "image" \
and (include_archived or l.get('archived', False) == False)]


def segmentation_layers(state):
def segmentation_layers(state, include_archived = True):
"""Get all segmentation layer names in the state
Parameters
----------
state : dict
Neuroglancer state as a JSON dict
include_archived : bool
Whether to include archived layers
Returns
-------
names : list
List of layer names
"""
return [l["name"] for l in state["layers"] if l["type"] in SEGMENTATION_LAYER_TYPES]
return [l["name"] for l in state["layers"] if l["type"] in SEGMENTATION_LAYER_TYPES \
and (include_archived or l.get('archived', False) == False)]


def annotation_layers(state):
def annotation_layers(state, include_archived = True):
"""Get all annotation layer names in the state
Parameters
----------
state : dict
Neuroglancer state as a JSON dict
include_archived : bool
Whether to include archived layers
Returns
-------
names : list
List of layer names
"""
return [l["name"] for l in state["layers"] if l["type"] == "annotation"]
return [l["name"] for l in state["layers"] if l["type"] == "annotation" \
and (include_archived or l.get('archived', False) == False)]


def tag_dictionary(state, layer_name):
Expand Down
17 changes: 15 additions & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,29 @@

def test_layer_names(test_state):
layer_names = parser.layer_names(test_state)
assert len(layer_names) == 6
for name in ['imagery-old1', 'imagery', 'imagery-old2', '72c5', 'segments', 'synapses']:
assert name in layer_names

layer_names = parser.layer_names(test_state, False)
assert len(layer_names) == 3
assert "synapses" in layer_names

for name in ['imagery', 'segments', 'synapses']:
assert name in layer_names

def test_layers(test_state):
img_layer = parser.image_layers(test_state)
assert len(img_layer) == 3
assert img_layer == ["imagery-old1", "imagery", "imagery-old2"]

img_layer = parser.image_layers(test_state, False)
assert len(img_layer) == 1
assert img_layer[0] == "imagery"

seg_layers = parser.segmentation_layers(test_state)
assert len(seg_layers) == 2
assert seg_layers == ["72c5", "segments"]

seg_layers = parser.segmentation_layers(test_state, False)
assert len(seg_layers) == 1
assert seg_layers[0] == "segments"

Expand Down
24 changes: 24 additions & 0 deletions tests/testdata/test_state.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
{
"layers": [
{
"source": "precomputed://gs://neuroglancer/pinky100_v0/son_of_alignment_v13_rechunked",
"type": "image",
"blend": "default",
"shaderControls": {},
"name": "imagery-old1",
"archived": true
},
{
"source": "precomputed://gs://neuroglancer/pinky100_v0/son_of_alignment_v15_rechunked",
"type": "image",
"blend": "default",
"shaderControls": {},
"name": "imagery"
},
{
"source": "precomputed://gs://neuroglancer/pinky100_v0/son_of_alignment_v14_rechunked",
"type": "image",
"blend": "default",
"shaderControls": {},
"name": "imagery-old2",
"archived": true
},
{
"type": "segmentation",
"source": "precomputed://gs://microns_public_datasets/pinky100_v184/seg",
"tab": "source",
"segments": [],
"name": "72c5",
"archived": true
},
{
"source": "precomputed://gs://microns_public_datasets/pinky100_v185/seg",
"type": "segmentation",
Expand Down

0 comments on commit b29e3e5

Please sign in to comment.