Skip to content

Commit

Permalink
ENH: Support structure ID in file format converter.
Browse files Browse the repository at this point in the history
  • Loading branch information
allemangD committed Mar 7, 2022
1 parent 6ca9f71 commit 26b770a
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 12 deletions.
1 change: 1 addition & 0 deletions Scripts/convert/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# most-recent versions first
version_order = [
'v0.2.1+2022.03.04',
'v0.2.0+2021.08.12',
'v0.1.1+2021.06.11',
'v0.1.0+2020.09.18',
Expand Down
17 changes: 15 additions & 2 deletions Scripts/convert/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect
from dataclasses import dataclass
from pathlib import Path
from typing import List, Tuple
from typing import List, Tuple, Optional

__all__ = ['Annotation', 'Document', 'Converter', 'versioned']

Expand All @@ -13,6 +13,19 @@
float, float, float, float,
float, float, float, float]

@dataclass
class Structure:
id: int
acronym: str

@dataclass
class Point:
position: Vector3f
structure: Optional[Structure] = None

def __iter__(self):
# enables unpacking like `x, y, z = point`
return iter(self.position)

@dataclass
class Annotation:
Expand Down Expand Up @@ -40,7 +53,7 @@ class Annotation:
)
"""A transformation matrix storing the orientation of the slicing plane."""

points: List[Vector3f] = dataclasses.field(default_factory=list)
points: List[Point] = dataclasses.field(default_factory=list)
"""Control point positions for the annotation markup."""


Expand Down
2 changes: 1 addition & 1 deletion Scripts/convert/versions/v0.0.0+2019.01.26.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def normalize(cls, data: dict):

ann.coordinate_system = 'LPS'
ann.points = [
(-p['x'], -p['y'], p['z']) # RAS → LPS conversion
model.Point((-p['x'], -p['y'], p['z'])) # RAS → LPS conversion
for p in dmark['Points']
]

Expand Down
2 changes: 1 addition & 1 deletion Scripts/convert/versions/v0.0.0+2020.04.16.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def normalize(cls, data: dict):

ann.coordinate_system = 'LPS'
ann.points = [
(-p['x'], -p['y'], p['z']) # RAS → LPS conversion
model.Point((-p['x'], -p['y'], p['z'])) # RAS → LPS conversion
for p in dmark['Points']
]

Expand Down
4 changes: 2 additions & 2 deletions Scripts/convert/versions/v0.0.0+2020.08.26.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def normalize(cls, data: dict):
ann.coordinate_units = dmark['coordinateUnits']

for point in dmark['controlPoints']:
ann.points.append(tuple(point['position']))
ann.points.append(model.Point(tuple(point['position'])))

doc.annotations.append(ann)

Expand All @@ -50,7 +50,7 @@ def specialize(cls, doc: model.Document):
'label': f'MarkupsClosedCurve-{i}',
'description': '',
'associatedNodeID': 'vtkMRMLScalarVolumeNode1',
'position': pt,
'position': pt.position,
'orientation': [-1.0, -0.0, -0.0,
-0.0, -1.0, -0.0,
+0.0, +0.0, +1.0],
Expand Down
4 changes: 2 additions & 2 deletions Scripts/convert/versions/v0.1.0+2020.09.18.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def normalize(cls, data: dict):
ann.coordinate_units = dmark['coordinateUnits']

for point in dmark['controlPoints']:
ann.points.append(tuple(point['position']))
ann.points.append(model.Point(tuple(point['position'])))

doc.annotations.append(ann)

Expand All @@ -49,7 +49,7 @@ def specialize(cls, doc: model.Document):
'controlPoints': [
{
'id': str(i),
'position': pt,
'position': pt.position,
'orientation': [-1.0, -0.0, -0.0,
-0.0, -1.0, -0.0,
+0.0, +0.0, +1.0]
Expand Down
4 changes: 2 additions & 2 deletions Scripts/convert/versions/v0.1.1+2021.06.11.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def normalize(cls, data: dict):
ann.coordinate_units = dmark['coordinateUnits']

for point in dmark['controlPoints']:
ann.points.append(tuple(point['position']))
ann.points.append(model.Point(tuple(point['position'])))

doc.annotations.append(ann)

Expand All @@ -48,7 +48,7 @@ def specialize(cls, doc: model.Document):
'controlPoints': [
{
'id': str(i),
'position': pt,
'position': pt.position,
'orientation': [-1.0, -0.0, -0.0,
-0.0, -1.0, -0.0,
+0.0, +0.0, +1.0]
Expand Down
4 changes: 2 additions & 2 deletions Scripts/convert/versions/v0.2.0+2021.08.12.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def normalize(cls, data: dict):
ann.coordinate_units = dmark['coordinateUnits']

for point in dmark['controlPoints']:
ann.points.append(tuple(point['position']))
ann.points.append(model.Point(tuple(point['position'])))

doc.annotations.append(ann)

Expand All @@ -48,7 +48,7 @@ def specialize(cls, doc: model.Document):
'controlPoints': [
{
'id': str(i),
'position': pt,
'position': pt.position,
'orientation': [-1.0, -0.0, -0.0,
-0.0, -1.0, -0.0,
+0.0, +0.0, +1.0]
Expand Down
85 changes: 85 additions & 0 deletions Scripts/convert/versions/v0.2.1+2022.03.04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import model


class Converter(model.Converter):
@classmethod
def normalize(cls, data: dict):
doc = model.Document()
doc.current_id = data['currentId']
doc.reference_view = data['referenceView']
doc.ontology = data['ontology']
doc.stepSize = data['stepSize']
doc.camera_position = tuple(data['cameraPosition'])
doc.camera_view_up = tuple(data['cameraViewUp'])

for dann in data['markups']:
dmark = dann['markup']

ann = model.Annotation()
ann.name = dann['name']
ann.orientation = dann['orientation']
ann.markup_type = dmark['type']

if ann.markup_type == 'ClosedCurve':
ann.representation_type = dann['representationType']
ann.thickness = dann['thickness']

ann.coordinate_system = dmark['coordinateSystem']
if 'coordinateUnits' in dmark:
ann.coordinate_units = dmark['coordinateUnits']

for point in dmark['controlPoints']:
position = tuple(point['position'])
structure = point.get('structure', None)
if structure:
structure = model.Structure(
structure['id'],
structure['acronym'],
)

ann.points.append(model.Point(position, structure))

doc.annotations.append(ann)

return doc

@classmethod
@model.versioned
def specialize(cls, doc: model.Document):
data = dict()
data['markups'] = [
{
'markup': {
'type': ann.markup_type,
'coordinateSystem': ann.coordinate_system,
'coordinateUnits': ann.coordinate_units,
'controlPoints': [
{
'id': str(i),
'position': pt.position,
'orientation': [-1.0, -0.0, -0.0,
-0.0, -1.0, -0.0,
+0.0, +0.0, +1.0],
'structure': {
'id': pt.structure.id,
'acronym': pt.structure.acronym
} if pt.structure else None
}
for i, pt in enumerate(ann.points, start=1)
],
},
'name': ann.name,
'orientation': ann.orientation,
'representationType': ann.representation_type,
'thickness': ann.thickness
}
for ann in doc.annotations
]
data['currentId'] = doc.current_id
data['referenceView'] = doc.reference_view
data['ontology'] = doc.ontology
data['stepSize'] = doc.stepSize
data['cameraPosition'] = doc.camera_position
data['cameraViewUp'] = doc.camera_view_up

return data

0 comments on commit 26b770a

Please sign in to comment.