Skip to content

Commit

Permalink
Merge pull request #80 from mobie/fix-trafos
Browse files Browse the repository at this point in the history
Extend transformation functionality
  • Loading branch information
constantinpape authored Jul 19, 2022
2 parents 9156286 + ef02338 commit e84a0d7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
21 changes: 20 additions & 1 deletion examples/create_mobie_project.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
"# good choices are usually (1, 512, 512) for 2d data and (64, 64, 64) for 3d data\n",
"# - scale_factors: the scale factors used for downsampling the input when creating the image pyramid\n",
"# this needs to be a list, where each entry specifies the scale factors for the 3 axes.\n",
"# Note that axes are always listed in the order ZYX here (in the java implementation of mobie / big-data-viewer the axis convention is XYZ).\n",
"# Note that axes are listed in the order ZYX for the resolution, chunks and scale factors\n",
"# (in the java implementation of mobie / big-data-viewer the axis convention is XYZ).\n",
"# Also note that the values for all three axes (ZYX) need to be specified. In the case of 2d data, the value\n",
"# for Z should be set to 1.\n",
"unit = \"nanometer\"\n",
Expand Down Expand Up @@ -157,6 +158,8 @@
"# the 2d em overview. This is achieved via an affine transformation,\n",
"# that has been determined externally and will be applied on the fly by big-data-viewer.\n",
"# Each affine transformation contains 12 parameters.\n",
"# Note that the transformations here have to be specified in the 'native' axis order for bdv/mobie\n",
"# this means they are given in XYZ order, unlike the other parameters that are given in ZYX\n",
"transformations = [\n",
" [5.098000335693359, 0.0, 0.0, 54413.567834472655,\n",
" 0.0, 5.098000335693359, 0.0, 51514.319843292236,\n",
Expand All @@ -166,6 +169,22 @@
" 0.0, 0.0, 5.098000335693359, 0.0]\n",
"]\n",
"\n",
"# if you have transformations that were determined with some tool in python, they are usually given in the ZYX order.\n",
"# you can use the following code to translate them to the correct XYZ order:\n",
"\n",
"# # assume 'trafo' is a transformation in zyx axis order:\n",
"# from mobie.utils import transformation_to_xyz\n",
"# trafo = transformation_to_xyz(trafo)\n",
"\n",
"# it might also be necessary to change the transformation order\n",
"# (this is because some tools specify transformations in forward order, some in backward order)\n",
"# in this case use\n",
"# trafo = transformation_to_xyz(trafo, invert=True)\n",
"\n",
"# (in practice you probably need to figure this out with trial and error)\n",
"# (sorry if this is confusing, but automatically changing this for the transformations can lead to \n",
"# a lot of subtle errors, so I decided against doing this)\n",
"\n",
"# add the two tomograms\n",
"for name, trafo in zip(tomo_names, transformations):\n",
" im_name = os.path.splitext(name)[0]\n",
Expand Down
2 changes: 1 addition & 1 deletion mobie/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "0.3.0"
__version__ = "0.3.2"
SPEC_VERSION = "0.2.0"
15 changes: 15 additions & 0 deletions mobie/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from copy import deepcopy

import elf.transformation as trafo_helper
import mobie.metadata as metadata
from cluster_tools.cluster_tasks import BaseClusterTask
from elf.io import open_file
Expand Down Expand Up @@ -246,3 +247,17 @@ def write_global_config(config_folder,

with open(conf_path, "w") as f:
json.dump(global_config, f)


def transformation_to_xyz(transform, invert=False):
"""Convert a transformation from zyx coordinates (python default)
to xyz coordinates (expected by mobie).
Arguments:
transform [list, np.ndarray] - the transformation parameters (12 values = upper 3 rows of affine matrix)
invert [bool] - whether to invert the transformation.
This can be necessary because e.g. scipy uses the different transformation direction (default: False)
"""
trafo = trafo_helper.parameters_to_matrix(transform)
trafo = trafo_helper.native_to_bdv(trafo, invert=invert)
return trafo
15 changes: 11 additions & 4 deletions mobie/xml_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import warnings
import xml.etree.ElementTree as ET
import numpy as np
from pybdv.metadata import get_data_path, indent_xml, get_bdv_format
from pybdv.metadata import write_affine

from pybdv.metadata import get_data_path, indent_xml, get_bdv_format, get_resolution, write_affine


def copy_xml_with_abspath(xml_in, xml_out):
Expand Down Expand Up @@ -121,5 +122,11 @@ def update_transformation_parameter(xml_path, parameter):
raise ValueError("Expected all affine transformation with 12 parameters.")
else:
raise ValueError(f"Invalid affine transformation {parameter}")
write_affine(xml_path, setup_id=0, affine=parameter,
overwrite=True, timepoint=0)
resolution = get_resolution(xml_path, setup_id=0)
if np.product(resolution) != 1:
warnings.warn(
f"The xml file at {xml_path} has the resolution {resolution}."
"The corresponding transformation will be over-written,"
"make sure to factor it in with the transformation you have specified."
)
write_affine(xml_path, setup_id=0, affine=parameter, overwrite=True, timepoint=0)

0 comments on commit e84a0d7

Please sign in to comment.