Skip to content

Instantly share code, notes, and snippets.

@alisterburt
Created November 16, 2024 00:12
Show Gist options
  • Save alisterburt/15b996f58f64384ca9c9b32edca1b79a to your computer and use it in GitHub Desktop.
Save alisterburt/15b996f58f64384ca9c9b32edca1b79a to your computer and use it in GitHub Desktop.
parse warp grids in python
from lxml import etree
json_file = "/Users/burta2/data/fakery/MotionAndCTF2D/average/20170629_00021_frameImage_motion.json"
# <GridMovementX Width="1" Height="1" Depth="3" MarginX="0" MarginY="0" MarginZ="0">
# <Node X="0" Y="0" Z="0" Value="-5.1639977" />
# <Node X="0" Y="0" Z="1" Value="0.7089546" />
# <Node X="0" Y="0" Z="2" Value="4.455043" />
# </GridMovementX>
# <GridMovementY Width="1" Height="1" Depth="3" MarginX="0" MarginY="0" MarginZ="0">
# <Node X="0" Y="0" Z="0" Value="5.4425087" />
# <Node X="0" Y="0" Z="1" Value="-0.63774544" />
# <Node X="0" Y="0" Z="2" Value="-4.8047633" />
# </GridMovementY>
# <GridLocalMovementX Width="1" Height="1" Depth="1" MarginX="0" MarginY="0" MarginZ="0">
# <Node X="0" Y="0" Z="0" Value="0.008896833" />
# </GridLocalMovementX>
tree = etree.parse(xml_file)
root = tree.getroot()
# get motion grid
gy = root.find('./GridMovementY')
gx = root.find('./GridMovementX')
ny = gy.findall('./Node')
nx = gx.findall('./Node')
d, h, w = gx.attrib['Depth'], gx.attrib['Height'], gx.attrib['Width']
d, h, w = int(d), int(h), int(w)
motion_grid = np.zeros(shape=(d, h, w, 2)) # (d, h, w, yx)
for node_y, node_x in zip(ny, nx):
z, y, x = node_y.attrib['Z'], node_y.attrib['Y'], node_y.attrib['X']
z, y, x = int(z), int(y), int(x)
y_value, x_value = node_y.attrib['Value'], node_x.attrib['Value']
y_value, x_value = float(y_value), float(x_value)
motion_grid[z, y, x] = (y_value, x_value)
print(motion_grid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment