neu_glance.shaders

GLSL shaders for annotation layers, and the rule for choosing one.

A shader lives in the viewer state, not in the annotation source, so it ships in a link.

A shader naming a ``prop_`` the source does not declare fails to compile, and the layer draws nothing, with the error shown only in the layer’s shader tab. pick_shader() therefore checks a shader’s properties against the source’s info and refuses a mismatch.

Attributes

Exceptions

ShaderProblem

A shader was named that this source cannot feed, or cannot be found.

Functions

as_hex_color(color[, what]) → str

A colour as #rrggbb, which is the only form a shaderControl takes.

image_shader(name, *[, kind, channels]) → str | None

GLSL for an image-like layer, or None for neuroglancer's own default.

pick_shader(info, name[, read_bytes]) → tuple[str | None, str | None]

(shader source, why) for an annotation layer.

segment_color(segment_id[, seed]) → str

#rrggbb neuroglancer gives segment_id with colour seed seed.

shader_color_control(name, *[, kind, channels]) → str

The uicontrol a caller's color should set, for the shader that will be chosen.

synapse_colorings(declared) → list[str]

The checkboxes of SYNAPSE_COLORINGS a source declaring declared gets.

synapse_filters(declared) → list[str]

The checkboxes of SYNAPSE_FILTERS a source declaring declared gets.

synapse_shader([declared]) → str

The synapse shader for a source whose properties are declared.

value_color(value[, seed]) → str

A stable #rrggbb for a property value, in neuroglancer's segment palette: the value

Module Contents

exception neu_glance.shaders.ShaderProblem

Bases: RuntimeError

A shader was named that this source cannot feed, or cannot be found.

Initialize self. See help(type(self)) for accurate signature.

neu_glance.shaders.as_hex_color(color: Any, what: str = 'color') → str

A colour as #rrggbb, which is the only form a shaderControl takes.

Accepts:

  • "#ff0000" or "ff0000";

  • a name from BUILTIN_COLORS ("red", "cyan", …);

  • any other name matplotlib knows ("tab:blue"), if matplotlib is installed;

  • a 3-sequence, read as 0..1 if every value is at most 1, else as 0..255 (so (1, 1, 1) is white).

neu_glance.shaders.image_shader(name: str | None, *, kind: str = 'image', channels: int = 1) → str | None

GLSL for an image-like layer, or None for neuroglancer’s own default.

name is a built-in name, "none", or None for the default for kind: grayscale for an image, probability for a probability map, and rgb for any 3-channel array. A shader whose channel count does not match the volume raises.

neu_glance.shaders.pick_shader(info: Mapping[str, Any], name: str | None, read_bytes=None) → tuple[str | None, str | None]

(shader source, why) for an annotation layer.

name is a built-in name, a path to a GLSL file, "none", or None for the first built-in whose properties the source declares. read_bytes reads a shader file (default neu_vol.location.read_bytes()).

neu_glance.shaders.segment_color(segment_id: int, seed: int = 0) → str

#rrggbb neuroglancer gives segment_id with colour seed seed.

A port of its SegmentColorHash.compute, the same function the synapse shader’s id_color implements in GLSL.

neu_glance.shaders.shader_color_control(name: str | None, *, kind: str = 'image', channels: int = 1) → str

The uicontrol a caller’s color should set, for the shader that will be chosen.

Raises when that shader has no single colour, since neuroglancer would silently ignore the control.

neu_glance.shaders.synapse_colorings(declared: Iterable[str]) → list[str]

The checkboxes of SYNAPSE_COLORINGS a source declaring declared gets.

neu_glance.shaders.synapse_filters(declared: Iterable[str]) → list[str]

The checkboxes of SYNAPSE_FILTERS a source declaring declared gets.

neu_glance.shaders.synapse_shader(declared: Iterable[str] = ()) → str

The synapse shader for a source whose properties are declared.

show_pre / show_post toggle the two endpoint markers, and the line is drawn only when both are on and show_line is ticked. A color_by_* checkbox is added for each colouring the properties allow; the first one ticked wins.

The confidence test must stay discard-if-below. An unknown confidence is NaN, which fails every comparison, so discard if conf < min keeps those synapses at any threshold while keep if conf >= min would hide them all. Unknown confidence usually means a synapse added by a proofreader, among the most trustworthy.

neu_glance.shaders.value_color(value: str, seed: int = 0) → str

A stable #rrggbb for a property value, in neuroglancer’s segment palette: the value is hashed to a 64-bit id (blake2b), which segment_color() colours.

neu_glance.shaders.BUILTIN_COLORS
neu_glance.shaders.COLORMAP_SHADER = Multiline-String
Show Value
"""#uicontrol invlerp normalized
#uicontrol vec3 low_color color(default="#000060")
#uicontrol vec3 high_color color(default="#ffe000")
#uicontrol float threshold slider(min=0.0, max=1.0, default=0.0, step=0.01)
#uicontrol float opacity slider(min=0.0, max=1.0, default=1.0, step=0.01)

void main() {
  float v = normalized();
  // v != v is true only for NaN: a voxel with no prediction is not drawn.
  if (v != v || v < threshold) discard;
  emitRGBA(vec4(mix(low_color, high_color, v), opacity));
}
"""
neu_glance.shaders.GRAYSCALE_SHADER = Multiline-String
Show Value
"""#uicontrol invlerp normalized
#uicontrol float gamma slider(min=0.05, max=3.0, default=1.0, step=0.05)

void main() {
  float v = pow(normalized(), gamma);
  emitGrayscale(v);
}
"""
neu_glance.shaders.IMAGE_SHADERS: dict[str, dict[str, Any]]
neu_glance.shaders.PROBABILITY_SHADER = Multiline-String
Show Value
"""#uicontrol invlerp normalized
#uicontrol vec3 color color(default="#ff0000")
#uicontrol float threshold slider(min=0.0, max=1.0, default=0.0, step=0.01)
#uicontrol float gain slider(min=0.0, max=4.0, default=1.0, step=0.05)

void main() {
  float v = normalized();
  // v != v is true only for NaN: a voxel with no prediction is not drawn.
  if (v != v || v < threshold) discard;
  emitRGBA(vec4(color, clamp(v * gain, 0.0, 1.0)));
}
"""
neu_glance.shaders.RGB_SHADER = Multiline-String
Show Value
"""#uicontrol vec3 color0 color(default="#ff2000")
#uicontrol vec3 color1 color(default="#00c000")
#uicontrol vec3 color2 color(default="#2060ff")
#uicontrol float gain0 slider(min=0.0, max=4.0, default=1.0, step=0.05)
#uicontrol float gain1 slider(min=0.0, max=4.0, default=1.0, step=0.05)
#uicontrol float gain2 slider(min=0.0, max=4.0, default=1.0, step=0.05)
#uicontrol float opacity slider(min=0.0, max=1.0, default=1.0, step=0.01)

void main() {
  vec3 rgb = color0 * (toNormalized(getDataValue(0)) * gain0)
           + color1 * (toNormalized(getDataValue(1)) * gain1)
           + color2 * (toNormalized(getDataValue(2)) * gain2);
  emitRGBA(vec4(clamp(rgb, 0.0, 1.0), opacity));
}
"""
neu_glance.shaders.SHADERS: dict[str, dict[str, Any]]
neu_glance.shaders.SYNAPSE_COLORINGS: tuple[tuple[str, tuple[str, ...], str], ...]
neu_glance.shaders.SYNAPSE_FILTERS: tuple[tuple[str, tuple[str, ...], str], ...] = (('hide_unlisted', ('pre_listed', 'post_listed'), 'if (hide_unlisted && prop_pre_listed() == 0u...
neu_glance.shaders.SYNAPSE_SHADER = ''