Package bar :: Module slides_aligner
[hide private]
[frames] | no frames]

Module slides_aligner

The module provides functions necessary to rescale and align slides to given reference coordinate system by modyfying transformation matrix for topmost g element.

Required input:

  1. SVG document to be modified,
  2. Reference scaling and offsets.

Workflow:

  1. Get initial transformation matrix from SVG,
  2. Calculate corrections basing on initial transformation matrix and reference matrix.
  3. Embed corrections as SVG transformation and put corrected transformation matrix as 3dBAR metedata

Output:

SVG drawing with reference scaling and offsets. Please note that scaling can be either positive and negative. When scaling is positive it means that stereotaxic axis has the same direction as image axis. When scaling is negative stereotaxic and image axes are oriented in opposite directions. Be very careful in sych case.

Import Graph
Import Graph

Functions [hide private]
 
makeAlignment((sxref, xref, syref, yref), currentTransformationMatrix, debugMode=False)
Function that manages aligning process.
 
getTransformationMatrix(transformatioMatrixTuple)
Extracts stereotaxic coordinate matrix from given SVG drawing metadata.
 
calculateAlignmentMatrix(refTuple, M, debugMode=False)
Calculates corretions for SVG drawing.
str
_applyTransformCorrection(TransformCorrection, refTuple)
Applies alignmetnt calculated by calculateAlignmentMatrix function and updates SVG 3dBAR metedata element:
 
calculateTransfFromMarkers((x1, y1), (x2, y2), (x1p, y1p), (x2p, y2p), z)
Variables [hide private]
  __package__ = 'bar'
  api_version = 1013
  argv = ['(imported)']
  builtin_module_names = ('__builtin__', '__main__', '_ast', '_b...
  byteorder = 'little'
  copyright = 'Copyright (c) 2001-2010 Python Software Foundatio...
  dont_write_bytecode = False
  exc_type = None
hash(x)
  exec_prefix = '/usr'
  executable = '/usr/bin/python'
  flags = sys.flags(debug=0, py3k_warning=0, division_warning=0,...
  float_info = sys.floatinfo(max=1.7976931348623157e+308, max_ex...
  hexversion = 33949168
  maxint = 9223372036854775807
  maxsize = 9223372036854775807
  maxunicode = 1114111
  meta_path = []
  modules = {'ConfigParser': <module 'ConfigParser' from '/usr/l...
  path = ['/home/pmajka/3dbrainatlases/lib/pymodules/python2.6',...
  path_hooks = [<type 'zipimport.zipimporter'>]
  path_importer_cache = {'': None, '/home/pmajka/3dbrainatlases'...
  platform = 'linux2'
  prefix = '/usr'
  py3kwarning = False
  pydebug = False
  stderr = <epydoc.docintrospecter._DevNull instance at 0x2685f38>
  stdin = <epydoc.docintrospecter._DevNull instance at 0x2685f38>
  stdout = <epydoc.docintrospecter._DevNull instance at 0x2685f38>
  subversion = ('CPython', 'tags/r265', '79063')
  version = '2.6.5 (r265:79063, Oct 1 2012, 22:04:36) \n[GCC 4....
  version_info = (2, 6, 5, 'final', 0)
  warnoptions = []
Function Details [hide private]

makeAlignment((sxref, xref, syref, yref), currentTransformationMatrix, debugMode=False)

 

Function that manages aligning process. Should be invoked from outside the class with SVG document as an argument. Modifies svgdom only.

Parameters:
  • xref (float) - Reference x coordinate of reference coordinate system origin ( x coordinate (in mm) of point (0,0) in image coordiantes).
  • yref (float) - y coordinate (in mm) of point (0,0) in image coordinates.
  • sxref (float) - Reference scaling in x direction. Increasing by one pixel in image coordinates means increasing by sxref in stereotaxic coordinates.
  • syref (float) - Reference scaling in y direction. Increasing by one pixel along image y coorginate means increasing by syref in stereotaxic coordinates.
  • debugMode (boolean) - If True full debug information would be printed. False by default.
Returns:
True, if changes were applied correctly. False, if changes were not applied.

getTransformationMatrix(transformatioMatrixTuple)

 

Extracts stereotaxic coordinate matrix from given SVG drawing metadata. It is assumed that in whole file there is only one tag named drba_transformationmatrix and it has content attribute in which matrix is stored as a tuple of four, float, comma-separated, values:

   \mathbf{M}_c= \begin{pmatrix}
   s_x   &  0    &  t_x \\ 
    0    &  s_y  &  t_y \\ 
    0    &  0    &  1
   \end{pmatrix}

   http://latex.codecogs.com/gif.latex?M=\begin{pmatrix}&space;s_x&space;&&space;0&space;&&space;t_x&space;\\&space;0&space;&&space;s_y&space;&&space;t_y&space;\\&space;0&space;&&space;0&space;&&space;1&space;\end{pmatrix}
Returns:
Matrix M as NumPy matrix

calculateAlignmentMatrix(refTuple, M, debugMode=False)

 

Calculates corretions for SVG drawing. Correction includes both: scaling and translation:

  1. cx, cy: corrections in x and y directions.
  2. csx, csy: scalings in x and y direction.

And few remarks (just to remain clear): Multiplication vector x by matrix M means going from image coordinate system to stereotaxic coordinate system.

Multiplication stereotaxic vector y by M^{-1} means switching from stereotaxic coordiante system to image coortdiante system.

Calcucating -1.(Mcorr.M) means:

  1. Transform matrix M by matrix Mcorr (we are still in stereotaxic coordinates).
  2. Transform result to image coordinates.

We use multiplication by inverted M because image is still in untransformed coordinate system.

Corrections are calculated in following way:

   \begin{matrix}
   c_x     & = & t_x - t_{x_{ref}} \\
   c_y     & = & t_y - t_{y_{ref}} \\
   c_{s_x} & = & \frac{s_{x_{ref}}}{s_x}\\
   c_{s_y} & = & \frac{s_{y_{ref}}}{s_y}\\
   \end{matrix}\right.

And then Mc matrix is created:

   \mathbf{M}_c=
   \begin{pmatrix}
   \frac{s_{x_{ref}}}{s_x} &           0               &  t_x - t_{x_{ref}} \\
   0                        &  \frac{s_{y_{ref}}}{s_y} &  t_y - t_{y_{ref}} \\
   0                        &           0               &  1
   \end{pmatrix}

The final correction is calculated using following formula:

   \begin{pmatrix}
   \frac{s_{x_{ref}}}{s_x}       & 0                          &            \frac{s_{x_{ref}} t_x - s_x t_{x_{ref}}}{{s_x}^{2}} \\
   0                              & \frac{s_{y_{ref}}}{s_y}   &            \frac{s_{y_{ref}}t_y - s_y t_{y_{ref}}}{{s_y}^{2}}\\
   0                              & 0                          & 1
   \end{pmatrix}
Parameters:
Returns:
3x3 numpy array C defined below

_applyTransformCorrection(TransformCorrection, refTuple)

 

Applies alignmetnt calculated by calculateAlignmentMatrix function and updates SVG 3dBAR metedata element:

  1. Put reference scaling and offets as 3dBAR transformation matrix
  2. Transform topmost element by TransformCorrection matrix.
Parameters:
  • refTuple (C(float, float, float, float)) - Reference transformation
  • TransformCorrection (3x3 numpy array) - Matrix with alignment correction
Returns: str
String that should be put into g element of slide in order to register the slide to reference tuple.

Variables Details [hide private]

builtin_module_names

Value:
('__builtin__',
 '__main__',
 '_ast',
 '_bisect',
 '_codecs',
 '_collections',
 '_functools',
 '_hashlib',
...

copyright

Value:
'''Copyright (c) 2001-2010 Python Software Foundation.
All Rights Reserved.

Copyright (c) 2000 BeOpen.com.
All Rights Reserved.

Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
...

flags

Value:
sys.flags(debug=0, py3k_warning=0, division_warning=0, division_new=0,\
 inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_\
site=0, no_site=0, ignore_environment=0, tabcheck=0, verbose=0, unicod\
e=0, bytes_warning=0, hash_randomization=0)

float_info

Value:
sys.floatinfo(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=30\
8, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15\
, mant_dig=53, epsilon=2.2204460492503131e-16, radix=2, rounds=1)

modules

Value:
{'ConfigParser': <module 'ConfigParser' from '/usr/lib/python2.6/Confi\
gParser.pyc'>,
 'DLFCN': <module 'DLFCN' from '/usr/lib/python2.6/plat-linux2/DLFCN.p\
yc'>,
 'FixTk': <module 'FixTk' from '/usr/lib/python2.6/lib-tk/FixTk.pyc'>,
 'Image': <module 'Image' from '/usr/lib/python2.6/dist-packages/PIL/I\
mage.pyc'>,
 'ImageColor': <module 'ImageColor' from '/usr/lib/python2.6/dist-pack\
...

path

Value:
['/home/pmajka/3dbrainatlases/lib/pymodules/python2.6',
 '/usr/bin',
 '/usr/local/lib/python2.6/dist-packages/pycallgraph-0.5.1-py2.6.egg',
 '/usr/local/lib/python2.6/dist-packages/Rtree-0.6.0-py2.6.egg',
 '/usr/local/lib/python2.6/dist-packages/python_graph_dot-1.7.0-py2.6.\
egg',
 '/usr/local/lib/python2.6/dist-packages/pydot-1.0.2-py2.6.egg',
 '/usr/local/lib/python2.6/dist-packages/python_graph_core-1.7.0-py2.6\
...

path_importer_cache

Value:
{'': None,
 '/home/pmajka/3dbrainatlases': None,
 '/home/pmajka/3dbrainatlases/lib/pymodules/python2.6': None,
 '/home/pmajka/3dbrainatlases/lib/pymodules/python2.6/': None,
 '/home/pmajka/3dbrainatlases/lib/pymodules/python2.6/bar': None,
 '/home/pmajka/3dbrainatlases/lib/pymodules/python2.6/bar/rec': None,
 '/opt/itk/lib/InsightToolkit/WrapITK/Python': None,
 '/usr/bin': None,
...

version

Value:
'''2.6.5 (r265:79063, Oct  1 2012, 22:04:36) 
[GCC 4.4.3]'''