Commit 6e98bd26 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'bf/annotations-render' into 'master'

BF: adjust annotations load/save so that it can handle uncreated canvases (required for render)

See merge request fsl/fsleyes/fsleyes!346
parents 411b942a b8a8bd2c
......@@ -97,6 +97,7 @@ import os
import shlex
import logging
import pathlib
import collections
from typing import Dict, List, Union
......@@ -235,9 +236,13 @@ def saveAnnotations(ortho : orthopanel.OrthoPanel,
"""Saves annotations on the canvases of the :class:`.OrthoPanel` to the
specified ``filename``.
"""
annots = {'X' : ortho.getXCanvas().getAnnotations().annotations,
'Y' : ortho.getYCanvas().getAnnotations().annotations,
'Z' : ortho.getZCanvas().getAnnotations().annotations}
annots = {}
canvases = {'X' : ortho.getXCanvas(),
'Y' : ortho.getYCanvas(),
'Z' : ortho.getZCanvas()}
for label, canvas in canvases.items():
if canvas is not None:
annots[label] = canvas.getAnnotations().annotations
with open(filename, 'wt') as f:
f.write(serialiseAnnotations(annots))
......@@ -252,9 +257,13 @@ def loadAnnotations(ortho : orthopanel.OrthoPanel,
with open(filename, 'rt') as f:
s = f.read().strip()
annots = {'X' : ortho.getXCanvas().getAnnotations(),
'Y' : ortho.getYCanvas().getAnnotations(),
'Z' : ortho.getZCanvas().getAnnotations()}
annots = {}
canvases = {'X' : ortho.getXCanvas(),
'Y' : ortho.getYCanvas(),
'Z' : ortho.getZCanvas()}
for label, canvas in canvases.items():
if canvas is not None:
annots[label] = canvas.getAnnotations()
allObjs = deserialiseAnnotations(s, annots)
......@@ -331,7 +340,7 @@ def deserialiseAnnotations(
:returns: Dictionary of ``{canvas : [AnnotationObject]}`` mappings.
"""
objs = {'X' : [], 'Y' : [], 'Z' : []}
objs = collections.defaultdict(list)
for line in s.split('\n'):
try:
......@@ -382,8 +391,10 @@ def deserialiseAnnotation(
kvpairs = dict([kv.split('=') for kv in shlex.split(kvpairs)])
if canvas not in 'XYZ':
raise ValueError('Canvas is not one of X, Y, '
'or Z ({})'.format(canvas))
raise ValueError(f'Canvas is not one of X, Y, or Z ({canvas})')
if canvas not in annots:
raise ValueError('Skipping annotation for non-existent '
f'canvas ({canvas}): {s}')
for k, v in kvpairs.items():
kvpairs[k] = parsers.get(k, float)(v)
......
......@@ -867,15 +867,19 @@ class MockOrthoPanel:
for three :class:`SliceCanvas` instances.
"""
def __init__(self, canvases):
self.canvases = canvases
self.canvases = {}
for canvas in canvases:
zax = 'XYZ'[canvas.opts.zax]
self.canvases[zax] = canvas
def getGLCanvases(self):
return self.canvases
return list(self.canvases.values())
def getXCanvas(self):
return self.canvases[0]
return self.canvases.get('X', None)
def getYCanvas(self):
return self.canvases[1]
return self.canvases.get('Y', None)
def getZCanvas(self):
return self.canvases[2]
return self.canvases.get('Z', None)
if __name__ == '__main__':
......
......@@ -72,6 +72,11 @@ mesh_ref mesh_l_thal.vtk -mc 0 1 0 -r mesh_ref -a 50
-a annotations.txt 3d
# github.com/pauldmccarthy/fsleyes/issues/104
-a annotations.txt -xh 3d
-a annotations.txt -yh 3d
-a annotations.txt -zh 3d
# show location
-sl X -ls 24 3d
-sl X 3d
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment