google/neuroglancer

Programmatically Changing Displayed Segments in a Segmentation Layer (JS)

Closed this issue · 3 comments

I'm developing an ontology traverser for a brain atlas, and I'm looking to programmatically change the displayed segments in a segmentation layer.

I've tried the following approach, but it's not working as expected:

temp_state = viewer.state.toJSON();
temp_state['layers'][10]['segments'] = ['16429','18022'];
viewer.state.restoreState(temp_state);

This call returns undefined and does not update the viewer state. It's liekly that I might be missing something obvious or taking the wrong approach. Any assistance in understanding how to correctly manipulate the segments would be greatly appreciated!

Try this:

with viewer.txn() as s:
    s.layers['my-layer-name'].segments = ['16429', '18022']

Edit: Oops, that's an example for the Python API. Now I notice that you mentioned JS in the title. Someone else will need to chime in.

Your example code works for me, at least in my browser's developer tools console...

temp_state = viewer.state.toJSON();
temp_state['layers'][1]['segments'] = ['26650', '11022'];
viewer.state.restoreState(temp_state);

(It does return undefined, but the viewer updates correctly.)


One thing to double-check: The index of the layer (e.g. temp_state['layers'][10] should NOT match what you see in the UI, since obviously the JSON layers list starts with item 0, whereas the UI starts counting at 1. Are you sure you're updating the correct layer? And be sure you're looking at the complete layer list (including "archived" layers, not just the ones shown at the top of the screen).


BTW, I notice that neuroglancer's own JSON editing UI also calls reset() before applying the new state, though I don't know why that's necessary:

private applyChanges() {
if (this.parsedValue !== null) {
this.viewer.state.reset();
this.viewer.state.restoreState(this.parsedValue);
}
this.applyButton.disabled = true;
}

Ah! working! @stuarteberg - I had two state variables that were very close to the same name and was updating with the incorrect one. Was also thrown off by the undefined response. interesting about the reset.

This has to be the one of the most helpful, quickest replying community. Thanks much!