Wrapping of Morpheme C library

From the base class Structure, each subclass must define a _fields_ attribute. _fields_ must be a list of 2-tuples, containing a field name and a field type. The field type must be a ctypes type like c_int, or any other derived ctypes type: structure, union, array, pointer.

Important

Importing the C libraries according to the running OS is done by: wrapping/clib.py.

Defined ctypes structures

We use the ctypes library to map C-object structure to Python classes for the following three types of objects:

  • cBalMatrix maps the C-structure used to represent matrix;
  • cBalImage & cVT_Image map the C-structures used to represent image;
  • cBalTrsf maps the C-structure used to represent transformation;

cBalMatrix structure

Module defining cBalMatrix object ctypes structure.

class timagetk.wrapping.c_bal_matrix.cBalMatrix[source]

Matrix ctypes structure as defined in bal-matrix.h.

Definition of cBalMatrix structure derived from the Structure class defined in the ctypes module.

Notes

See build-scons/include/blockmatching/bal-matrix.h:

typedef struct {
  int l,c;
  double *m;
} _MATRIX;
l

number of rows

Type:c_int
c

number of column

Type:c_int
m

array defining the matrix

Type:POINTER(c_double)

cBalImage structure

Module defining cBalImage object ctypes structure.

class timagetk.wrapping.c_bal_image.cBalImage[source]

Image ctypes structure as defined in bal-image.h.

Definition of cBalImage structure derived from the Structure class defined in the ctypes module.

Notes

See build-scons/include/blockmatching/bal-image.h:

typedef struct {
  size_t ncols;         /* Number of columns (X dimension) */
  size_t nrows;         /* Number of rows (Y dimension) */
  size_t nplanes;       /* Number of planes (Z dimension) */
  size_t vdim;          /* Vector size */
  bufferType c_uint;
  void *data;        /* Generic pointer on image data buffer.
                        This pointer has to be casted in proper data type
                        depending on the type field */
  void ***array;     /* Generic 3D array pointing on each image element.
                        This pointer has to be casted in proper data type
                        depending on the type field */

  typeVoxelSize vx;          /* real voxel size in X dimension */
  typeVoxelSize vy;          /* real voxel size in Y dimension */
  typeVoxelSize vz;          /* real voxel size in Z dimension */

  char *name;
} bal_image;
ncols

Number of columns (X dimension)

Type:c_size_t
nrows

Number of rows (Y dimension)

Type:c_size_t
nplanes

Number of planes (Z dimension)

Type:c_size_t
vdim

Vector size

Type:c_size_t
type

bufferType

Type:c_uint
data

Generic pointer on image data buffer

Type:c_void_p
array

Generic 3D array pointing on each image element

Type:c_void_p
vx

Real voxel size in X dimension

Type:typeVoxelSize
vy

Real voxel size in Y dimension

Type:typeVoxelSize
vz

Real voxel size in Z dimension

Type:typeVoxelSize
name

name of the image

Type:c_char_p

cBalTrsf structure

Module defining cBalTrsf object ctypes structure.

class timagetk.wrapping.c_bal_trsf.cBalTrsf[source]

Transformation ctypes structure as defined in bal-transformation.h.

Definition of cBalTrsf structure derived from the Structure class defined in the ctypes module.

Notes

See build-scons/include/blockmatching/bal-transformation.h:

typedef struct {

  enumTypeTransfo type;
  cBalMatrix mat;
  enumUnitTransfo transformation_unit;
  bal_image vx;
  bal_image vy;
  bal_image vz;

  /* for transformation averaging
   */
  float weight;
  float error;

} bal_transformation;
type

type of transformation (see TRSF_TYPE_DICT in bal_trsf.py)

Type:c_int
mat

linear transformation matrix (see cBalMatrix in c_bal_matrix.py)

Type:cBalMatrix
transformation_unit

unit of transformation matrix (see TRSF_UNIT_DICT in bal_trsf.py)

Type:c_int
vx

non-linear transformation matrix in x direction (see cBalImage in c_bal_image.py)

Type:cBalImage
vy

non-linear transformation matrix in y direction (see cBalImage in c_bal_image.py)

Type:cBalImage
vz

non-linear transformation matrix in z direction (see cBalImage in c_bal_image.py)

Type:cBalImage
weight

weight for transformation averaging

Type:c_float
error

error in transformation averaging

Type:c_float

cVT_Image structure

class timagetk.wrapping.c_vt_image.cVT_Image[source]

Image ctypes structure as defined in vt_image.h.

Definition of cVT_Image structure derived from the Structure class defined in the ctypes module.

Notes

See build-scons/include/libvt/vt_image.h:

typedef struct vt_image {
  char name[STRINGLENGTH];
  ImageType type;
  vt_4vsize dim; /* dimension of image */
  vt_fpt siz;  /* voxel size */
  vt_fpt off;  /* translation or offset */
  vt_fpt rot;  /* rotation */
  vt_ipt ctr;
  CpuType cpu;
  void ***array;
  void *buf;

  /** User defined strings array. The user can use any internal purpose string.
      Each string is written at then end of header after a '#' character. */
  char **user;
  /** Number of user defined strings */
  unsigned int nuser;


} vt_image;
name

name of the image

Type:c_char
type

type of the image

Type:c_uint
dim

dimension of the image

Type:vt_4vsize
siz

voxelsize of the image

Type:vt_fpt
off

translation or offset

Type:vt_fpt
rot

rotation

Type:vt_fpt
ctr

???

Type:vt_ipt
cpu

type of CPU

Type:c_uint
array

array defining the image

Type:c_void_p
buf

User defined strings array, can be any internal purpose string.

Type:c_void_p
user

Each string is written at then end of header after a ‘#’ character.

Type:c_char_p
nuser

Number of user defined strings

Type:c_uint

Defined classes

A python implementation of the previously mapped C-structure is available.

Matrix class

class timagetk.wrapping.bal_matrix.BalMatrix(np_array=None, c_bal_matrix=None, **kwargs)[source]

Class representing matrix objects.

Parameters:
  • np_array (numpy.array, optional) – numpy array to use when creating BalMatrix instance
  • c_bal_matrix (cBalMatrix, optional) – C-instance to use when creating BalMatrix instance
Other Parameters:
 
  • l (int) – number of lines
  • c (int) – number of columns

Notes

Other parameters are used by:

  • np_array_to_c_bal_matrix if np_array is given;
  • new_c_bal_matrix if no parameters are given;

Example

>>> import numpy as np
>>> from timagetk.wrapping import BalMatrix
>>> # Manually create identity quaternion:
>>> arr = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
>>> # Initialize from ``numpy.array`` instance:
>>> bmat = BalMatrix(np_array=arr)
>>> bmat.to_np_array()
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])
>>> # Initialize from ``cBalMatrix`` instance:
>>> from timagetk.wrapping.bal_matrix import np_array_to_c_bal_matrix
>>> cbmat = np_array_to_c_bal_matrix(arr)
>>> bmat = BalMatrix(c_bal_matrix=cbmat)
>>> bmat.to_np_array()
array([[ 1.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  0.],
       [ 0.,  0.,  0.,  1.]])
c_display(name='')[source]

Print information about the object as found in the allocated memory.

Parameters:name (str) – name of the transformation, used for printing
Returns:information about the BalMatrix object
Return type:str
c_ptr

Get the pointer to the C object cBalMatrix.

c_struct

Get the cBalMatrix hidden attribute.

Returns:the hidden attribute _c_bal_matrix
Return type:cBalMatrix
free()[source]

Free memory allocated to object.

to_np_array(dtype=None)[source]

Convert to a numpy array.

Parameters:dtype (str|np.dtype) – type of the numpy array to return, np.float16 by default
Returns:converted instance
Return type:numpy.array

Image class

class timagetk.wrapping.bal_image.BalImage(spatial_image=None, c_bal_image=None, **kwargs)[source]

Class representing image objects.

Parameters:
  • np_array (numpy.array, optional) – numpy array to use when creating BalImage instance
  • c_bal_matrix (cBalMatrix, optional) – C-instance to use when creating BalImage instance
Other Parameters:
 
  • name (str) – name of the image to initialize
  • shape (list) – shape of the array, ie. size of each axis
  • voxelsize (int) – voxelsize in y-direction
  • np_type (str) – bit-depth encoding of the data in numpy style
  • vt_type (str) – bit-depth encoding of the data in VT style
  • vdim (int) – here set to 1

Notes

Other parameters are used by:
  • spatial_image_to_c_bal_image if np_array is given;
  • new_c_bal_image if no parameters are given;

Example

>>> from timagetk.util import data_path
>>> from timagetk.io import imread
>>> from timagetk.wrapping import BalImage
>>> from timagetk.wrapping.bal_image import spatial_image_to_c_bal_image
>>> # Load test ``SpatialImage``:
>>> img_path = data_path('time_0_cut.inr')
>>> sp_img = imread(img_path)
>>> # Convert it to a ``cBalImage`` type:
>>> c_bal_image = spatial_image_to_c_bal_image(sp_img)
>>> # Initialize from ``SpatialImage`` instance:
>>> bimg = BalImage(spatial_image=sp_img)
>>> # Test equality with original ``SpatialImage`` instance:
>>> sp_img.equal_array(bimg.to_spatial_image())
True
>>> # Initialize from ``cBalImage`` instance:
>>> from timagetk.wrapping.bal_image import spatial_image_to_c_bal_image
>>> cbimg = spatial_image_to_c_bal_image(sp_img)
>>> bimg = BalImage(c_bal_image=cbimg)
>>> sp_img.equal_array(bimg.to_spatial_image())
True
c_display(name='')[source]

Print information about the object as found in the allocated memory.

Parameters:name (str) – name of the transformation, used for printing
Returns:information about the BalImage object
Return type:str
c_ptr

Get the pointer to the C object cBalImage.

c_struct

Get the cBalImage hidden attribute.

Returns:the hidden attribute _c_bal_image
Return type:cBalImage
free()[source]

Free memory allocated to object.

to_spatial_image(**kwargs)[source]

Convert to a SpatialImage.

Returns:converted instance
Return type:SpatialImage

Transformation class

class timagetk.wrapping.bal_trsf.BalTrsf(trsf_type=None, trsf_unit=None, c_bal_trsf=None)[source]

Class representing transformation objects.

Warning

This class do not yet follow bal_naming_convention and behaviour, it is just a container without memory management.

Notes

Yo can only pass pointers in constructors. So to create transformations, initialise matrix or images outside the constructor and pass pointers (mat.c_ptr, image.c_ptr) to constructor. Parameters trsf_type & trsf_unit are used only if c_bal_trsf is not specified (None).

Parameters:
  • trsf_type (int or str, optional) – if given, type of transformation to initialise, see TRSF_TYPE_DICT
  • trsf_unit (int or str, optional) – if given, unit of transformation to initialise, see TRSF_UNIT_DICT
  • c_bal_trsf (cBalTrsf or BalTrsf, optional) – if given, existing ctypes structure or BalTrsf object

Example

>>> from __future__ import print_function
>>> import numpy as np
>>> from timagetk.wrapping import BalTrsf
>>> from timagetk.wrapping.bal_matrix import np_array_to_c_bal_matrix
>>> # Initialize 3D rigid ``BalTrsf`` instance, in real units:
>>> trsf = BalTrsf(trsf_type='RIGID_3D', trsf_unit='REAL_UNIT')
>>> print(trsf.get_type())
>>> 'RIGID_3D'
>>> print(trsf.get_unit())
>>> 'REAL_UNIT'
>>> # Manually create identity quaternion:
>>> quaternion = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])
>>> cbal_mat = np_array_to_c_bal_matrix(quaternion)
>>> trsf = BalTrsf(c_bal_trsf=cbal_mat)
mat

Quaternion describing the linear transformation. None if vectorfield transformation.

Type:numpy.array
vx, vy, vz

Displacement matrix in x, y and z for non-linear transformation. None if linear transformation.

Type:numpy.array
trsf_unit

unit of the transformation, available units are defined in TRSF_UNIT_DICT

Type:int
trsf_type

type of the transformation, available types are defined in TRSF_TYPE_DICT

Type:int
c_display(name='')[source]

Print information about the object as found in the allocated memory.

Parameters:name (str) – name of the transformation, used for printing
Returns:information about the BalTrsf object
Return type:str
c_ptr

Get the pointer to the C object.

c_struct

Get the cBalTrsf hidden attribute.

Returns:the hidden attribute _c_bal_trsf
Return type:cBalTrsf
free()[source]

Free memory allocated to object.

get_type()[source]

Returns the type of transformation matrix as a string.

Convert the integer attribute trsf_type into a human readable type.

get_unit()[source]

Returns the unit of transformation matrix as a string.

Convert the integer attribute trsf_unit into a human readable unit.

has_linear_matrix()[source]

Tests if a linear matrix is defined under self._c_bal_trsf.mat.

Returns:True if defined, False if None (NULL pointer)
Return type:bool
has_nonlinear_matrix()[source]

Tests if a linear matrix is defined under self._c_bal_trsf.vx.

Returns:True if defined, False if None (NULL pointer)
Return type:bool
is_linear()[source]

Test if the transformation matrix is of type ‘Linear’.

Linear transformation matrix are obtained from those types:

  • TRANSLATION_2D, TRANSLATION_3D;
  • TRANSLATION_SCALING_2D, TRANSLATION_SCALING_3D;
  • RIGID_2D, RIGID_3D;
  • SIMILITUDE_2D, SIMILITUDE_3D;
  • AFFINE_2D, AFFINE_3D.
Returns:is_linear – True if of type ‘Linear’, else False
Return type:bool
is_vectorfield()[source]

Test if the transformation matrix is of type ‘VectorField’.

Non-linear transformation matrix are obtained from those types:

  • VECTORFIELD_2D, VECTORFIELD_3D;
Returns:is_vectorfield – True if of type ‘VectorField’, else False
Return type:bool
read(path)[source]

Read a transformation given in path.

Warning

This function:

  • can not differentiate between ‘AFFINE’ and ‘RIGID’ transformation, default is ‘AFFINE’!
  • can not differentiate between ‘REAL’ and ‘VOXEL’ transformation unit for linear transformations, default is ‘REAL_UNIT’!
  • will most likely result in a Segmentation error (core dumped), on some linux systems, when loading non-linear deformation!
Parameters:path (str) – file path to the transformation to read
write(path)[source]

Write a transformation under given path.

Warning

Using this function on:

  • an empty instance, ‘ie.’ initialized as BalTrsf(), will NOT write anything!
  • an almost empty instance, ‘eg.’ initialized as BalTrsf(), will most likely result in a Core dumped!
  • a linear transformation will not save its type (affine/rigid) or unit (real/voxel)!
Parameters:path (str) – file path to the transformation to write

VT_Image class

class timagetk.wrapping.vt_image.VT_Image(sp_img)[source]

Class VT_Image

Parameters:sp_img (SpatialImage) – image and metadata to use when creating VT_Image instance

Example

>>> from timagetk.util import data_path
>>> from timagetk.io import imread
>>> from timagetk.wrapping.vt_image import VT_Image
>>> # Load test ``SpatialImage``:
>>> img_path = data_path('time_0_cut.inr')
>>> sp_img = imread(img_path)
>>> # Instanciate a ``VT_Image``:
>>> vim = VT_Image(sp_img)
>>> sp_img.equal_array(vim.to_spatial_image())
True
vt_image

ctypes structure

Type:cVT_Image
_data

given SpatialImage instance, image and metadata

Type:SpatialImage
c_display(name='')[source]

Print information about the object as found in the allocated memory.

Parameters:name (str) – name used for information printing

Example

>>> from timagetk.wrapping.vt_image import VT_Image
>>> from timagetk.io import imread, imsave
>>> from timagetk.util import data_path
>>> # input image path
>>> img_path = data_path('time_0_cut.inr')
>>> # .inr format to SpatialImage
>>> sp_img = imread(img_path)
>>> vt_img = VT_Image(sp_img)
>>> vt_img.c_display()
'time_0_cut.inr' information:
  - image buffer: 0x7f6240f26010
  - image array: 0x55e5aeb237e0
  - dimensions [x y z] = 340 280 50
  - voxel size [x y z] = 0.235358 0.235358 0.227175
  - image type is: UCHAR
  - min = 0 , max = 255 , mean = 36.458957
c_ptr

Get the pointer to the cVT_Image instance.

Returns:
Return type:cVT_Image
c_struct

Get the cVT_Image attribute.

Returns:the attribute vt_image
Return type:cVT_Image
free()[source]

Free memory allocated to object.

get_vt_image()[source]

Returns a cVT_Image structure instance from ctypes.

Returns:instance found under the vt_image attribute.
Return type:cVT_Image
to_spatial_image()[source]

Get the VT_image as a SpatialImage

Returns:self._data – SpatialImage associated to the VT_Image
Return type:SpatialImage

Useful methods: creation and conversion

Instance creation and conversion

To ease-up working with these objects we have defined several initialization and conversion methods.

numpy.array <–> cBalMatrix / BalMatrix

To easily convert numpy.array objects into cBalMatrix / BalMatrix instances, or the other way around, use these methods:

bal_matrix.bal_matrix_to_np_array(dtype=None)

Convert cBalMatrix to numpy array.

Parameters:
  • c_or_bal_matrix (cBalMatrix) – C object to convert into numpy.array
  • dtype (str|np.dtype) – type of the numpy array to return, np.float16 by default
Returns:

np_array – converted array

Return type:

numpy.array

Example

>>> import numpy as np
>>> from timagetk.wrapping.bal_matrix import bal_matrix_to_np_array
>>> from timagetk.wrapping.bal_matrix import np_array_to_c_bal_matrix
>>> arr = np.random.random_sample((4, 2))
>>> arr
array([[ 0.80724603,  0.33081692],
       [ 0.20047332,  0.76125283],
       [ 0.91918492,  0.99985815],
       [ 0.90476838,  0.85771965]])
>>> c_bal_matrix = np_array_to_c_bal_matrix(arr)
>>> bal_matrix_to_np_array(c_bal_matrix)
array([[ 0.80724603,  0.33081692],
       [ 0.20047332,  0.76125283],
       [ 0.91918492,  0.99985815],
       [ 0.90476838,  0.85771965]])
bal_matrix.np_array_to_c_bal_matrix(**kwargs)

Convert numpy.array to cBalMatrix.

Parameters:

np_array (numpy.array) – numpy array to convert as a cBalMatrix

Other Parameters:
 
  • l (int) – number of lines
  • c (int) – number of columns
Returns:

c_bal_matrix – converted numpy.array instance

Return type:

cBalMatrix

Notes

Keyword arguments defined in ‘other parameters’ will be updated by those found by inspection of given np_array with np_array_to_bal_matrix_fields.

Example

>>> import numpy as np
>>> from timagetk.wrapping.bal_matrix import np_array_to_c_bal_matrix
>>> from timagetk.wrapping.bal_matrix import bal_matrix_to_np_array
>>> arr = np.random.random_sample((4, 2))
>>> arr
array([[ 0.35506291,  0.99067097],
       [ 0.85754132,  0.79715911],
       [ 0.57996506,  0.49254318],
       [ 0.21778762,  0.57746731]])
>>> c_bal_matrix = np_array_to_c_bal_matrix(arr)
>>> c_bal_matrix.l
2
>>> c_bal_matrix.c
4
>>> bal_matrix_to_np_array(c_bal_matrix)
array([[ 0.35506291,  0.99067097],
       [ 0.85754132,  0.79715911],
       [ 0.57996506,  0.49254318],
       [ 0.21778762,  0.57746731]])
Raises:TypeError – If input np_array is not a numpy.array instance.

SpatialImage <–> cBalImage / BalImage

To easily convert SpatialImage objects into cBalImage / BalImage instances, or the other way around, use these methods:

bal_image.bal_image_to_spatial_image()

Convert cBalImage to SpatialImage.

Parameters:c_or_bal_image (cBalImage) – C object to convert into SpatialImage
Returns:new instance obtained by conversion of cBalImage
Return type:SpatialImage

Example

>>> import numpy as np
>>> from timagetk.util import data_path
>>> from timagetk.io import imread
>>> from timagetk.wrapping.bal_image import spatial_image_to_c_bal_image
>>> img_path = data_path('time_0_cut.inr')
>>> sp_img = imread(img_path)
>>> c_bal_image = spatial_image_to_c_bal_image(sp_img)
>>> # Test equality of given ``numpy.array`` to allocated ``cBalMatrix``
>>> from timagetk.wrapping.bal_image import bal_image_to_spatial_image
>>> np.array_equal(bal_image_to_spatial_image(c_bal_image), sp_img)
bal_image.spatial_image_to_c_bal_image(**kwargs)

Convert SpatialImage to cBalImage.

Parameters:

spatial_image (SpatialImage) – object to allocate in a cBalImage instance

Other Parameters:
 
  • name (str) – name of the image to initialize
  • shape (list) – shape of the array, ie. size of each axis
  • voxelsize (int) – voxelsize in y-direction
  • np_type (str) – bit-depth encoding of the data in numpy style
  • vt_type (str) – bit-depth encoding of the data in VT style
  • vdim (int) – here set to 1
Returns:

c_bal_image – converted SpatialImage instance

Return type:

cBalImage

Example

>>> from __future__ import print_function
>>> from timagetk.util import data_path
>>> from timagetk.io import imread
>>> from timagetk.wrapping.bal_image import spatial_image_to_c_bal_image
>>> img_path = data_path('time_0_cut.inr')
>>> sp_img = imread(img_path)
>>> c_bal_image = spatial_image_to_c_bal_image(sp_img)
>>> print(sp_img.shape)
>>> print(c_bal_image.ncols, c_bal_image.nrows, c_bal_image.nplanes)
Raises:TypeError – If input spatial_image is not a SpatialImage instance.

BalTrsf initialisation

To easily create a BalTrsf from a quaternion defining a linear deformation, rigid or affine, use one of the following methods:

bal_trsf.np_array_to_rigid_trsf(trsf_unit='REAL_UNIT')

Create a rigid BalTrsf from a numpy array.

The numpy array must be a quaternion describing a rigid transformation

Parameters:
  • np_array (numpy.array) – quaternion describing the rigid transformation
  • trsf_unit (int or str, optional) – defines if the quaternion in expressed in real (“REAL_UNIT”), by default, or voxel units (“VOXEL_UNIT”)
Returns:

rigid transformation

Return type:

BalTrsf

Example

>>> import numpy as np
>>> from timagetk.wrapping.bal_trsf import np_array_to_rigid_trsf
>>> arr = np.random.random_sample((4, 4))
>>> trsf = np_array_to_rigid_trsf(arr)
>>> print(trsf.get_unit())
'REAL_UNIT'
>>> print(trsf.get_type())
'RIGID_3D'
>>> print(trsf.is_linear())
True
>>> trsf.c_display('test')
type of 'test' is RIGID_3D
transformation unit is in real units
   0.438030221014404    0.357154839192758    0.227787771639948    0.031846050398104
   0.239656746138632    0.498627779703492    0.768720227202931    0.449667299588282
   0.587327485769069    0.145162665186915    0.424790275453152    0.005316127646503
   0.293384023207115    0.532391710948782    0.853227217373506    0.457796981063500
bal_trsf.np_array_to_affine_trsf(trsf_unit='REAL_UNIT')

Create an affine BalTrsf from a numpy array.

The numpy array must be a quaternion describing an affine transformation

Parameters:
  • np_array (numpy.array) – quaternion describing the affine transformation
  • trsf_unit (int or str, optional) – defines if the quaternion in expressed in real unit (“REAL_UNIT”), by default, or voxel unit (“VOXEL_UNIT”)
Returns:

affine transformation

Return type:

BalTrsf

Example

>>> import numpy as np
>>> from timagetk.wrapping.bal_trsf import np_array_to_affine_trsf
>>> arr = np.random.random_sample((4, 4))
>>> trsf = np_array_to_affine_trsf(arr)
>>> print(trsf.get_unit())
'REAL_UNIT'
>>> print(trsf.get_type())
'AFFINE_3D'
>>> print(trsf.is_linear())
True
>>> trsf.c_display('test')
type of 'test' is AFFINE_3D
transformation unit is in real units
   0.833440359505476    0.117967030337482    0.639281681539620    0.989473005721904
   0.503056486652696    0.643971409647213    0.829838922556072    0.116883038428201
   0.086115555445802    0.404529497498842    0.617357824753340    0.528172757938566
   0.441608426544292    0.884445137670544    0.838913838120309    0.692011691783862

Bit conversion

To ease the type conversion (bit depth and size) between the different objects, ie. numpy, “Bal*” & “VT*”, we provide the following functions:

timagetk.wrapping.type_conversion.np_type_to_vt_type(dtype)[source]

Convert numpy.dtype into known VT types.

Notes

Known VT types are defined as attributes in class ImageType.

Parameters:dtype (numpy.dtype) – numpy.dtype to convert
Returns:_type – known VT type
Return type:ctypes
Raises:TypeError – if given dtype is not mapped to a known ImageType attributes.
timagetk.wrapping.type_conversion.vt_type_to_np_type(vt_type)[source]

Convert vt_type into numpy.dtype.

Notes

Known VT types are defined in class ImageType.

Parameters:vt_type (ImageType) – vt type to convert
Returns:known dtype to use with numpy arrays
Return type:numpy.dtype
Raises:TypeError – if given vt_type is not mapped to a known numpy.dtype.
timagetk.wrapping.type_conversion.vt_type_to_c_type(vt_type)[source]

Convert vt_type into ctypes.

Notes

Known ctypes are: c_ubyte, c_byte, c_short, c_ushort, c_uint, c_int, c_ulong, c_long, c_float and c_double.

Parameters:vt_type (ImageType) – vt type to convert
Returns:known ctypes
Return type:ctypes
Raises:TypeError – if given vt_type is not mapped to a known ctypes.
timagetk.wrapping.type_conversion.morpheme_type_arg(image)[source]

Convert type of input image into arguments for wrapped C libraries.

Parameters:image (numpy.array or SpatialImage) – image or array to use to convert type
Returns:argument to pass to wrapped C libraries
Return type:str
Raises:TypeError – if type is not defined in type conversion global variable VT_TO_ARG.