# This software and supporting documentation are distributed by # Institut Federatif de Recherche 49 # CEA/NeuroSpin, Batiment 145, # 91191 Gif-sur-Yvette cedex # France # # This software is governed by the CeCILL license version 2 under # French law and abiding by the rules of distribution of free software. # You can use, modify and/or redistribute the software under the # terms of the CeCILL license version 2 as circulated by CEA, CNRS # and INRIA at the following URL "http://www.cecill.info". # # As a counterpart to the access to the source code and rights to copy, # modify and redistribute granted by the license, users are provided only # with a limited warranty and the software's author, the holder of the # economic rights, and the successive licensors have only limited # liability. # # In this respect, the user's attention is drawn to the risks associated # with loading, using, modifying and/or developing or reproducing the # software by the user in light of its specific status of free software, # that may mean that it is complicated to manipulate, and that also # therefore means that it is reserved for developers and experienced # professionals having in-depth computer knowledge. Users are therefore # encouraged to load and test the software's suitability as regards their # requirements in conditions enabling the security of their systems and/or # data to be ensured and, more generally, to use and operate it in the # same conditions as regards security. # # The fact that you are presently reading this means that you have had # knowledge of the CeCILL license version 2 and that you accept its terms. from brainvisa.processes import * import shfjGlobals, os, tempfile, shutil import registration name = 'Diffusion to Extended AC/PC referential' userLevel = 2 signature=Signature( 't1_mri', ReadDiskItem( "Raw T1 MRI", \ 'Aims readable volume formats' ), 'commissure_coordinates', ReadDiskItem( 'Commissure coordinates', \ 'Commissure coordinates'), 't2_diffusion', ReadDiskItem( 'Raw T2 Diffusion MR', \ 'Aims readable volume formats' ), 'dw_diffusion', ReadDiskItem( 'Corrected DW Diffusion MR', \ 'Aims readable volume formats' ), 't2_diffusion_to_t1_anatomy_transform', WriteDiskItem( 'Transform T2 Diffusion MR to Raw T1 MRI', \ 'Transformation matrix' ), 't1_anatomy_to_t2_diffusion_transform', WriteDiskItem( 'Transform Raw T1 MRI to T2 Diffusion MR', \ 'Transformation matrix' ), 't1_anatomy_to_acpc_t2_diffusion_transform', WriteDiskItem( 'Transform Raw T1 MRI to AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 't2_diffusion_to_acpc_t2_diffusion_transform', WriteDiskItem( 'Transform T2 Diffusion MR to AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 't1_anatomy_to_eacpc_t2_diffusion_transform', WriteDiskItem( 'Transform Raw T1 MRI to Extended AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 'acpc_t2_diffusion_to_eacpc_t2_diffusion_transform', WriteDiskItem( 'Transform AC/PC T2 Diffusion MR to Extended AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 't2_diffusion_to_eacpc_t2_diffusion_transform', WriteDiskItem( 'Transform T2 Diffusion MR to Extended AC/PC T2 Diffusion MR', \ 'Transformation matrix' ), 'eacpc_t1_mri', WriteDiskItem( 'T1 MRI AC/PC oriented', \ 'Aims writable volume formats' ), 'eacpc_t2_diffusion', WriteDiskItem( 'Extended AC/PC T2 Diffusion MR', \ 'Aims writable volume formats' ), 'eacpc_dw_diffusion', WriteDiskItem( 'Extended AC/PC DW Diffusion MR', \ 'Aims writable volume formats' ), ) def selectTransformation(self, source=None, destination=None): att={} if source is not None: att=source.hierarchyAttributes() elif destination is not None: att=destination.hierarchyAttributes() if source is not None: att["source"]=source if destination is not None: att["destination"]=destination return att def initialization( self ): self.linkParameters( 'commissure_coordinates', 't1_mri' ) self.linkParameters( 't2_diffusion', 't1_mri' ) self.linkParameters( 'dw_diffusion', 't2_diffusion' ) self.addLink( 't2_diffusion_to_t1_anatomy_transform', ['t2_diffusion', 't1_mri' ], self.selectTransformation) self.addLink( 't1_anatomy_to_t2_diffusion_transform', ['t1_mri', 't2_diffusion' ], self.selectTransformation ) self.addLink( 't1_anatomy_to_acpc_t2_diffusion_transform', ['t1_mri', 't2_diffusion' ], self.selectTransformation ) self.linkParameters( 't2_diffusion_to_acpc_t2_diffusion_transform', 't2_diffusion' ) self.linkParameters( 'acpc_t2_diffusion_to_eacpc_t2_diffusion_transform', 't2_diffusion' ) self.linkParameters( 't2_diffusion_to_eacpc_t2_diffusion_transform', 't2_diffusion' ) self.addLink( 't1_anatomy_to_eacpc_t2_diffusion_transform', ['t1_mri', 't2_diffusion' ], self.selectTransformation ) self.linkParameters( 'eacpc_t1_mri', 't1_mri' ) self.linkParameters( 'eacpc_t2_diffusion', 't2_diffusion' ) self.linkParameters( 'eacpc_dw_diffusion', 'dw_diffusion' ) def execution( self, context ): trManager = registration.getTransformationManager() # estimating T1 referential to AC/PC referential transform acpcReferential = trManager.findOrCreateReferential( 'AC/PC T2 Diffusion MR referential', self.t2_diffusion ) if acpcReferential is None: context.warning( _t_( 'Cannot save AC/PC T2 Diffusion MR referential. Some information about referentials and transformations will not be saved.' ) ) context.system( 'AimsTalairachTransform', '-apc', self.commissure_coordinates.fullPath(), '-ns', '-o', self.t1_anatomy_to_acpc_t2_diffusion_transform.fullPath() ) if acpcReferential is not None: trManager.setNewTransformationInfo( self.t1_anatomy_to_acpc_t2_diffusion_transform, source_referential = self.t1_mri, destination_referential = acpcReferential ) # estimating T2 diff referential to T1 referential and its inverse context.system( 'AimsMIRegister', '-r', self.t1_mri.fullPath(), '-t', self.t2_diffusion.fullPath(), '--dir', self.t2_diffusion_to_t1_anatomy_transform.fullPath(), '--inv', self.t1_anatomy_to_t2_diffusion_transform.fullPath(), '--pdfcalc', 'pv', '--error', '0.001', '--refstartpyr', '1' ) trManager.setNewTransformationInfo( self.t1_anatomy_to_t2_diffusion_transform, source_referential = self.t1_mri, destination_referential = self.t2_diffusion ) trManager.setNewTransformationInfo( self.t2_diffusion_to_t1_anatomy_transform, source_referential = self.t2_diffusion, destination_referential = self.t1_mri ) # composition T2/T1 transform and T1/AC-PC transform context.system( 'AimsComposeTransformation', '-i', self.t1_anatomy_to_acpc_t2_diffusion_transform.fullPath(), '-j', self.t2_diffusion_to_t1_anatomy_transform.fullPath(), '-o', self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath() ) if acpcReferential is not None: trManager.setNewTransformationInfo( self.t2_diffusion_to_acpc_t2_diffusion_transform, source_referential = self.t2_diffusion, destination_referential = acpcReferential ) # reading and building transformation for T2 to AC/PC f = open( self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath() ) lines = f.readlines() f.close() translation_t2 = [ 0.0, 0.0, 0.0 ] rotation_t2 = [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] tmp = string.split( lines[ 0 ] ) translation_t2[ 0 ] = float( tmp[ 0 ] ) translation_t2[ 1 ] = float( tmp[ 1 ] ) translation_t2[ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 1 ] ) rotation_t2[ 0 ][ 0 ] = float( tmp[ 0 ] ) rotation_t2[ 0 ][ 1 ] = float( tmp[ 1 ] ) rotation_t2[ 0 ][ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 2 ] ) rotation_t2[ 1 ][ 0 ] = float( tmp[ 0 ] ) rotation_t2[ 1 ][ 1 ] = float( tmp[ 1 ] ) rotation_t2[ 1 ][ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 3 ] ) rotation_t2[ 2 ][ 0 ] = float( tmp[ 0 ] ) rotation_t2[ 2 ][ 1 ] = float( tmp[ 1 ] ) rotation_t2[ 2 ][ 2 ] = float( tmp[ 2 ] ) # reading and building transformation for T1 to AC/PC f = open( self.t1_anatomy_to_acpc_t2_diffusion_transform.fullPath() ) lines = f.readlines() f.close() translation_t1 = [ 0.0, 0.0, 0.0 ] rotation_t1 = [ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] tmp = string.split( lines[ 0 ] ) translation_t1[ 0 ] = float( tmp[ 0 ] ) translation_t1[ 1 ] = float( tmp[ 1 ] ) translation_t1[ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 1 ] ) rotation_t1[ 0 ][ 0 ] = float( tmp[ 0 ] ) rotation_t1[ 0 ][ 1 ] = float( tmp[ 1 ] ) rotation_t1[ 0 ][ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 2 ] ) rotation_t1[ 1 ][ 0 ] = float( tmp[ 0 ] ) rotation_t1[ 1 ][ 1 ] = float( tmp[ 1 ] ) rotation_t1[ 1 ][ 2 ] = float( tmp[ 2 ] ) tmp = string.split( lines[ 3 ] ) rotation_t1[ 2 ][ 0 ] = float( tmp[ 0 ] ) rotation_t1[ 2 ][ 1 ] = float( tmp[ 1 ] ) rotation_t1[ 2 ][ 2 ] = float( tmp[ 2 ] ) # calculating transformation of bounding box for T2 image volume_dimension_t2 = self.t2_diffusion.get( 'volume_dimension' ) voxel_size_t2 = self.t2_diffusion.get( 'voxel_size' ) volume_dimension_dw = self.dw_diffusion.get( 'volume_dimension' ) voxel_size_dw = self.dw_diffusion.get( 'voxel_size' ) p1_t2 = [ 0.0, 0.0, 0.0 ] p2_t2 = [ ( volume_dimension_t2[ 0 ] - 1.0 ) * voxel_size_t2[ 0 ], 0.0, 0.0 ] p3_t2 = [ ( volume_dimension_t2[ 0 ] - 1.0 ) * voxel_size_t2[ 0 ], \ ( volume_dimension_t2[ 1 ] - 1.0 ) * voxel_size_t2[ 1 ], 0.0 ] p4_t2 = [ 0.0, ( volume_dimension_t2[ 1 ] - 1.0 ) * voxel_size_t2[ 1 ], 0.0 ] p5_t2 = [ 0.0, 0.0, ( volume_dimension_t2[ 2 ] - 1.0 ) * voxel_size_t2[ 2 ] ] p6_t2 = [ ( volume_dimension_t2[ 0 ] - 1.0 ) * voxel_size_t2[ 0 ], 0.0, \ ( volume_dimension_t2[ 2 ] - 1.0 ) * voxel_size_t2[ 2 ] ] p7_t2 = [ ( volume_dimension_t2[ 0 ] - 1.0 ) * voxel_size_t2[ 0 ], \ ( volume_dimension_t2[ 1 ] - 1.0 ) * voxel_size_t2[ 1 ], \ ( volume_dimension_t2[ 2 ] - 1.0 ) * voxel_size_t2[ 2 ] ] p8_t2 = [ 0.0, ( volume_dimension_t2[ 1 ] - 1.0 ) * voxel_size_t2[ 1 ], \ ( volume_dimension_t2[ 2 ] - 1.0 ) * voxel_size_t2[ 2 ] ] p1_t2_prime = [ 0.0, 0.0, 0.0 ] p2_t2_prime = [ 0.0, 0.0, 0.0 ] p3_t2_prime = [ 0.0, 0.0, 0.0 ] p4_t2_prime = [ 0.0, 0.0, 0.0 ] p5_t2_prime = [ 0.0, 0.0, 0.0 ] p6_t2_prime = [ 0.0, 0.0, 0.0 ] p7_t2_prime = [ 0.0, 0.0, 0.0 ] p8_t2_prime = [ 0.0, 0.0, 0.0 ] p1_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p1_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p1_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p1_t2[ 2 ] + \ translation_t2[ 0 ] p1_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p1_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p1_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p1_t2[ 2 ] + \ translation_t2[ 1 ] p1_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p1_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p1_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p1_t2[ 2 ] + \ translation_t2[ 2 ] p2_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p2_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p2_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p2_t2[ 2 ] + \ translation_t2[ 0 ] p2_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p2_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p2_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p2_t2[ 2 ] + \ translation_t2[ 1 ] p2_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p2_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p2_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p2_t2[ 2 ] + \ translation_t2[ 2 ] p3_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p3_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p3_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p3_t2[ 2 ] + \ translation_t2[ 0 ] p3_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p3_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p3_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p3_t2[ 2 ] + \ translation_t2[ 1 ] p3_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p3_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p3_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p3_t2[ 2 ] + \ translation_t2[ 2 ] p4_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p4_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p4_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p4_t2[ 2 ] + \ translation_t2[ 0 ] p4_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p4_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p4_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p4_t2[ 2 ] + \ translation_t2[ 1 ] p4_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p4_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p4_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p4_t2[ 2 ] + \ translation_t2[ 2 ] p5_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p5_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p5_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p5_t2[ 2 ] + \ translation_t2[ 0 ] p5_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p5_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p5_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p5_t2[ 2 ] + \ translation_t2[ 1 ] p5_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p5_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p5_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p5_t2[ 2 ] + \ translation_t2[ 2 ] p6_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p6_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p6_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p6_t2[ 2 ] + \ translation_t2[ 0 ] p6_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p6_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p6_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p6_t2[ 2 ] + \ translation_t2[ 1 ] p6_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p6_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p6_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p6_t2[ 2 ] + \ translation_t2[ 2 ] p7_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p7_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p7_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p7_t2[ 2 ] + \ translation_t2[ 0 ] p7_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p7_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p7_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p7_t2[ 2 ] + \ translation_t2[ 1 ] p7_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p7_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p7_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p7_t2[ 2 ] + \ translation_t2[ 2 ] p8_t2_prime[ 0 ] = rotation_t2[ 0 ][ 0 ] * p8_t2[ 0 ] + \ rotation_t2[ 0 ][ 1 ] * p8_t2[ 1 ] + \ rotation_t2[ 0 ][ 2 ] * p8_t2[ 2 ] + \ translation_t2[ 0 ] p8_t2_prime[ 1 ] = rotation_t2[ 1 ][ 0 ] * p8_t2[ 0 ] + \ rotation_t2[ 1 ][ 1 ] * p8_t2[ 1 ] + \ rotation_t2[ 1 ][ 2 ] * p8_t2[ 2 ] + \ translation_t2[ 1 ] p8_t2_prime[ 2 ] = rotation_t2[ 2 ][ 0 ] * p8_t2[ 0 ] + \ rotation_t2[ 2 ][ 1 ] * p8_t2[ 1 ] + \ rotation_t2[ 2 ][ 2 ] * p8_t2[ 2 ] + \ translation_t2[ 2 ] # calculating transformation of bounding box for T1 image volume_dimension_t1 = self.t1_mri.get( 'volume_dimension' ) voxel_size_t1 = self.t1_mri.get( 'voxel_size' ) p1_t1 = [ 0.0, 0.0, 0.0 ] p2_t1 = [ ( volume_dimension_t1[ 0 ] - 1.0 ) * voxel_size_t1[ 0 ], 0.0, 0.0 ] p3_t1 = [ ( volume_dimension_t1[ 0 ] - 1.0 ) * voxel_size_t1[ 0 ], \ ( volume_dimension_t1[ 1 ] - 1.0 ) * voxel_size_t1[ 1 ], 0.0 ] p4_t1 = [ 0.0, ( volume_dimension_t1[ 1 ] - 1.0 ) * voxel_size_t1[ 1 ], 0.0 ] p5_t1 = [ 0.0, 0.0, ( volume_dimension_t1[ 2 ] - 1.0 ) * voxel_size_t1[ 2 ] ] p6_t1 = [ ( volume_dimension_t1[ 0 ] - 1.0 ) * voxel_size_t1[ 0 ], 0.0, \ ( volume_dimension_t1[ 2 ] - 1.0 ) * voxel_size_t1[ 2 ] ] p7_t1 = [ ( volume_dimension_t1[ 0 ] - 1.0 ) * voxel_size_t1[ 0 ], \ ( volume_dimension_t1[ 1 ] - 1.0 ) * voxel_size_t1[ 1 ], \ ( volume_dimension_t1[ 2 ] - 1.0 ) * voxel_size_t1[ 2 ] ] p8_t1 = [ 0.0, ( volume_dimension_t1[ 1 ] - 1.0 ) * voxel_size_t1[ 1 ], \ ( volume_dimension_t1[ 2 ] - 1.0 ) * voxel_size_t1[ 2 ] ] p1_t1_prime = [ 0.0, 0.0, 0.0 ] p2_t1_prime = [ 0.0, 0.0, 0.0 ] p3_t1_prime = [ 0.0, 0.0, 0.0 ] p4_t1_prime = [ 0.0, 0.0, 0.0 ] p5_t1_prime = [ 0.0, 0.0, 0.0 ] p6_t1_prime = [ 0.0, 0.0, 0.0 ] p7_t1_prime = [ 0.0, 0.0, 0.0 ] p8_t1_prime = [ 0.0, 0.0, 0.0 ] p1_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p1_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p1_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p1_t1[ 2 ] + \ translation_t1[ 0 ] p1_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p1_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p1_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p1_t1[ 2 ] + \ translation_t1[ 1 ] p1_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p1_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p1_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p1_t1[ 2 ] + \ translation_t1[ 2 ] p2_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p2_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p2_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p2_t1[ 2 ] + \ translation_t1[ 0 ] p2_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p2_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p2_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p2_t1[ 2 ] + \ translation_t1[ 1 ] p2_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p2_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p2_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p2_t1[ 2 ] + \ translation_t1[ 2 ] p3_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p3_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p3_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p3_t1[ 2 ] + \ translation_t1[ 0 ] p3_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p3_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p3_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p3_t1[ 2 ] + \ translation_t1[ 1 ] p3_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p3_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p3_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p3_t1[ 2 ] + \ translation_t1[ 2 ] p4_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p4_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p4_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p4_t1[ 2 ] + \ translation_t1[ 0 ] p4_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p4_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p4_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p4_t1[ 2 ] + \ translation_t1[ 1 ] p4_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p4_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p4_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p4_t1[ 2 ] + \ translation_t1[ 2 ] p5_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p5_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p5_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p5_t1[ 2 ] + \ translation_t1[ 0 ] p5_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p5_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p5_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p5_t1[ 2 ] + \ translation_t1[ 1 ] p5_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p5_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p5_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p5_t1[ 2 ] + \ translation_t1[ 2 ] p6_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p6_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p6_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p6_t1[ 2 ] + \ translation_t1[ 0 ] p6_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p6_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p6_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p6_t1[ 2 ] + \ translation_t1[ 1 ] p6_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p6_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p6_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p6_t1[ 2 ] + \ translation_t1[ 2 ] p7_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p7_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p7_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p7_t1[ 2 ] + \ translation_t1[ 0 ] p7_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p7_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p7_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p7_t1[ 2 ] + \ translation_t1[ 1 ] p7_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p7_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p7_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p7_t1[ 2 ] + \ translation_t1[ 2 ] p8_t1_prime[ 0 ] = rotation_t1[ 0 ][ 0 ] * p8_t1[ 0 ] + \ rotation_t1[ 0 ][ 1 ] * p8_t1[ 1 ] + \ rotation_t1[ 0 ][ 2 ] * p8_t1[ 2 ] + \ translation_t1[ 0 ] p8_t1_prime[ 1 ] = rotation_t1[ 1 ][ 0 ] * p8_t1[ 0 ] + \ rotation_t1[ 1 ][ 1 ] * p8_t1[ 1 ] + \ rotation_t1[ 1 ][ 2 ] * p8_t1[ 2 ] + \ translation_t1[ 1 ] p8_t1_prime[ 2 ] = rotation_t1[ 2 ][ 0 ] * p8_t1[ 0 ] + \ rotation_t1[ 2 ][ 1 ] * p8_t1[ 1 ] + \ rotation_t1[ 2 ][ 2 ] * p8_t1[ 2 ] + \ translation_t1[ 2 ] # calculating coordinates of transformed bouding box xList = [ p1_t2_prime[ 0 ], p2_t2_prime[ 0 ], p3_t2_prime[ 0 ], p4_t2_prime[ 0 ], \ p5_t2_prime[ 0 ], p6_t2_prime[ 0 ], p7_t2_prime[ 0 ], p8_t2_prime[ 0 ], \ p1_t1_prime[ 0 ], p2_t1_prime[ 0 ], p3_t1_prime[ 0 ], p4_t1_prime[ 0 ], \ p5_t1_prime[ 0 ], p6_t1_prime[ 0 ], p7_t1_prime[ 0 ], p8_t1_prime[ 0 ] ] yList = [ p1_t2_prime[ 1 ], p2_t2_prime[ 1 ], p3_t2_prime[ 1 ], p4_t2_prime[ 1 ], \ p5_t2_prime[ 1 ], p6_t2_prime[ 1 ], p7_t2_prime[ 1 ], p8_t2_prime[ 1 ], \ p1_t1_prime[ 1 ], p2_t1_prime[ 1 ], p3_t1_prime[ 1 ], p4_t1_prime[ 1 ], \ p5_t1_prime[ 1 ], p6_t1_prime[ 1 ], p7_t1_prime[ 1 ], p8_t1_prime[ 1 ] ] zList = [ p1_t2_prime[ 2 ], p2_t2_prime[ 2 ], p3_t2_prime[ 2 ], p4_t2_prime[ 2 ], \ p5_t2_prime[ 2 ], p6_t2_prime[ 2 ], p7_t2_prime[ 2 ], p8_t2_prime[ 2 ], \ p1_t1_prime[ 2 ], p2_t1_prime[ 2 ], p3_t1_prime[ 2 ], p4_t1_prime[ 2 ], \ p5_t1_prime[ 2 ], p6_t1_prime[ 2 ], p7_t1_prime[ 2 ], p8_t1_prime[ 2 ] ] xList.sort() yList.sort() zList.sort() xMin = xList[ 0 ] yMin = yList[ 0 ] zMin = zList[ 0 ] xMax = xList[ 15 ] yMax = yList[ 15 ] zMax = zList[ 15 ] context.write( 'xMin, xMax : ', xMin, ', ', xMax ) context.write( 'yMin, yMax : ', yMin, ', ', yMax ) context.write( 'zMin, zMax : ', zMin, ', ', zMax ) xOffset = -xMin yOffset = -yMin zOffset = -zMin xPointToAddToDiff = int( ( xMax - xMin ) / voxel_size_t2[ 0 ] ) + 1 \ - volume_dimension_t2[ 0 ] yPointToAddToDiff = int( ( yMax - yMin ) / voxel_size_t2[ 1 ] ) + 1 \ - volume_dimension_t2[ 1 ] zPointToAddToDiff = int( ( zMax - zMin ) / voxel_size_t2[ 2 ] ) + 1 \ - volume_dimension_t2[ 2 ] xPointToAddToAnat = int( ( xMax - xMin ) / voxel_size_t1[ 0 ] ) + 1 \ - volume_dimension_t1[ 0 ] yPointToAddToAnat = int( ( yMax - yMin ) / voxel_size_t1[ 1 ] ) + 1 \ - volume_dimension_t1[ 1 ] zPointToAddToAnat = int( ( zMax - zMin ) / voxel_size_t1[ 2 ] ) + 1 \ - volume_dimension_t1[ 2 ] context.write( 'xPointToAddToDiff : ', xPointToAddToDiff ) context.write( 'yPointToAddToDiff : ', yPointToAddToDiff ) context.write( 'zPointToAddToDiff : ', zPointToAddToDiff ) context.write( 'xPointToAddToAnat : ', xPointToAddToAnat ) context.write( 'yPointToAddToAnat : ', yPointToAddToAnat ) context.write( 'zPointToAddToAnat : ', zPointToAddToAnat ) extACPCReferential = trManager.findOrCreateReferential( 'Extended AC/PC T2 Diffusion MR referential', self.t2_diffusion ) if extACPCReferential is None: context.warning( _t_( 'Cannot save extended AC/PC referential. Some information about referentials and transformations will not be saved.' ) ) # saving AC-PC to Extended AC-PC transform f = open( self.acpc_t2_diffusion_to_eacpc_t2_diffusion_transform.fullPath(), 'w' ) print >> f, xOffset, ' ', yOffset, ' ', zOffset print >> f, 1, ' ', 0, ' ', 0 print >> f, 0, ' ', 1, ' ', 0 print >> f, 0, ' ', 0, ' ', 1 f.close() if acpcReferential is not None and extACPCReferential is not None: trManager.setNewTransformationInfo( self.acpc_t2_diffusion_to_eacpc_t2_diffusion_transform, source_referential = acpcReferential, destination_referential = extACPCReferential ) # composition T2/AC-PC transform and AC-PC/eAC-PC transform context.system( 'AimsComposeTransformation', '-i', self.acpc_t2_diffusion_to_eacpc_t2_diffusion_transform.fullPath(), '-j', self.t2_diffusion_to_acpc_t2_diffusion_transform.fullPath(), '-o', self.t2_diffusion_to_eacpc_t2_diffusion_transform.fullPath() ) if extACPCReferential is not None: trManager.setNewTransformationInfo( self.t2_diffusion_to_eacpc_t2_diffusion_transform, source_referential = self.t2_diffusion, destination_referential = extACPCReferential ) # composition T1/AC-PC transform and AC-PC/eAC-PC transform context.system( 'AimsComposeTransformation', '-i', self.acpc_t2_diffusion_to_eacpc_t2_diffusion_transform.fullPath(), '-j', self.t1_anatomy_to_acpc_t2_diffusion_transform.fullPath(), '-o', self.t1_anatomy_to_eacpc_t2_diffusion_transform.fullPath() ) if extACPCReferential is not None: trManager.setNewTransformationInfo( self.t1_anatomy_to_eacpc_t2_diffusion_transform, source_referential = self.t1_mri, destination_referential = extACPCReferential ) # evaluating new geometries new_volume_dimension_t2 = [ volume_dimension_t2[ 0 ] + xPointToAddToDiff, \ volume_dimension_t2[ 1 ] + yPointToAddToDiff, \ volume_dimension_t2[ 2 ] + zPointToAddToDiff, \ 1 ] new_volume_dimension_dw = [ volume_dimension_dw[ 0 ] + xPointToAddToDiff, \ volume_dimension_dw[ 1 ] + yPointToAddToDiff, \ volume_dimension_dw[ 2 ] + zPointToAddToDiff, \ volume_dimension_dw[ 3 ] ] new_volume_dimension_t1 = [ volume_dimension_t1[ 0 ] + xPointToAddToAnat, \ volume_dimension_t1[ 1 ] + yPointToAddToAnat, \ volume_dimension_t1[ 2 ] + zPointToAddToAnat, \ 1 ] # creating T2 temporary template file tmpDirectory = tempfile.mktemp() os.mkdir( tmpDirectory ) templateT2NameWoExtension = tmpDirectory + '/' + 'templateT2' templateT2Name = templateT2NameWoExtension + '.dim' f = open( templateT2Name, 'w' ) print >> f, new_volume_dimension_t2[ 0 ], ' ', \ new_volume_dimension_t2[ 1 ], ' ', \ new_volume_dimension_t2[ 2 ], ' ', \ new_volume_dimension_t2[ 3 ] print >> f, '-type S16' print >> f, '-dx ', voxel_size_t2[ 0 ], ' ', \ '-dy ', voxel_size_t2[ 1 ], ' ', \ '-dz ', voxel_size_t2[ 2 ], ' ', \ '-dt ', voxel_size_t2[ 3 ] f.close() # creating DW temporary template file templateDWNameWoExtension = tmpDirectory + '/' + 'templateDW' templateDWName = templateDWNameWoExtension + '.dim' f = open( templateDWName, 'w' ) print >> f, new_volume_dimension_dw[ 0 ], ' ', \ new_volume_dimension_dw[ 1 ], ' ', \ new_volume_dimension_dw[ 2 ], ' ', \ new_volume_dimension_dw[ 3 ] print >> f, '-type S16' print >> f, '-dx ', voxel_size_dw[ 0 ], ' ', \ '-dy ', voxel_size_dw[ 1 ], ' ', \ '-dz ', voxel_size_dw[ 2 ], ' ', \ '-dt ', voxel_size_dw[ 3 ] f.close() # creating t1 temporary template file templateT1NameWoExtension = tmpDirectory + '/' + 'templateT1' templateT1Name = templateT1NameWoExtension + '.dim' f = open( templateT1Name, 'w' ) print >> f, new_volume_dimension_t1[ 0 ], ' ', \ new_volume_dimension_t1[ 1 ], ' ', \ new_volume_dimension_t1[ 2 ], ' ', \ new_volume_dimension_t1[ 3 ] print >> f, '-type S16' print >> f, '-dx ', voxel_size_t1[ 0 ], ' ', \ '-dy ', voxel_size_t1[ 1 ], ' ', \ '-dz ', voxel_size_t1[ 2 ], ' ', \ '-dt ', voxel_size_t1[ 3 ] f.close() # resampling T1 Anatomy to Extended AC/PC referential context.system( 'AimsResample', '-t', 3, '-i', self.t1_mri.fullPath(), '-r', templateT1NameWoExtension, '-m', self.t1_anatomy_to_eacpc_t2_diffusion_transform.fullPath(), '-o', self.eacpc_t1_mri.fullPath() ) if extACPCReferential is not None: trManager.copyReferential( extACPCReferential, self.eacpc_t1_mri ) # resampling T2 diffusion to Extended AC/PC referential context.system( 'AimsResample', '-t', 3, '-i', self.t2_diffusion.fullPath(), '-r', templateT2NameWoExtension, '-m', self.t2_diffusion_to_eacpc_t2_diffusion_transform.fullPath(), '-o', self.eacpc_t2_diffusion.fullPath() ) # removing negative values context.system( 'AimsThreshold', '-i', self.eacpc_t2_diffusion.fullPath(), '-o', self.eacpc_t2_diffusion.fullPath(), '-m', 'gt', '-t', 0 ) if extACPCReferential is not None: trManager.copyReferential( extACPCReferential, self.eacpc_t2_diffusion ) # resampling corrected DW diffusion to ACPC referential context.system( 'AimsResample', '-t', 3, '-i', self.dw_diffusion.fullPath(), '-r', templateDWNameWoExtension, '-m', self.t2_diffusion_to_eacpc_t2_diffusion_transform.fullPath(), '-o', self.eacpc_dw_diffusion.fullPath() ) if extACPCReferential is not None: trManager.copyReferential( extACPCReferential, self.eacpc_dw_diffusion ) if os.path.exists( tmpDirectory ) : shutil.rmtree( tmpDirectory ) # copying T1 meta info header to resampled T1 eacpc_t1_minf = self.t1_mri.minf().copy() eacpc_t1_minf[ 'volume_dimension' ] = new_volume_dimension_t1 eacpc_t1_minf[ 'sizeX' ] = new_volume_dimension_t1[ 0 ] eacpc_t1_minf[ 'sizeY' ] = new_volume_dimension_t1[ 1 ] eacpc_t1_minf[ 'sizeZ' ] = new_volume_dimension_t1[ 2 ] eacpc_t1_minf.pop( 'referential', None ) self.eacpc_t1_mri.updateMinf( eacpc_t1_minf ) # copying T2 meta info header to resampled T2 eacpc_t2_minf = self.t2_diffusion.minf().copy() eacpc_t2_minf[ 'volume_dimension' ] = new_volume_dimension_t2 eacpc_t2_minf[ 'sizeX' ] = new_volume_dimension_t2[ 0 ] eacpc_t2_minf[ 'sizeY' ] = new_volume_dimension_t2[ 1 ] eacpc_t2_minf[ 'sizeZ' ] = new_volume_dimension_t2[ 2 ] eacpc_t2_minf.pop( 'referential', None ) self.eacpc_t2_diffusion.updateMinf( eacpc_t2_minf ) # copying DW meta info header to resampled DW and applying transform # to diffusion gradient orientations eacpc_dw_minf = self.dw_diffusion.minf().copy() eacpc_dw_minf[ 'volume_dimension' ] = new_volume_dimension_dw eacpc_dw_minf[ 'sizeX' ] = new_volume_dimension_dw[ 0 ] eacpc_dw_minf[ 'sizeY' ] = new_volume_dimension_dw[ 1 ] eacpc_dw_minf[ 'sizeZ' ] = new_volume_dimension_dw[ 2 ] eacpc_dw_minf.pop( 'referential', None ) orientations = eacpc_dw_minf[ 'diffusion_gradient_orientations' ] for o in range( len( orientations ) ): orientations[ o ] = [ rotation_t2[ 0 ][ 0 ] * orientations[ o ][ 0 ] + \ rotation_t2[ 0 ][ 1 ] * orientations[ o ][ 1 ] + \ rotation_t2[ 0 ][ 2 ] * orientations[ o ][ 2 ], \ rotation_t2[ 1 ][ 0 ] * orientations[ o ][ 0 ] + \ rotation_t2[ 1 ][ 1 ] * orientations[ o ][ 1 ] + \ rotation_t2[ 1 ][ 2 ] * orientations[ o ][ 2 ], \ rotation_t2[ 2 ][ 0 ] * orientations[ o ][ 0 ] + \ rotation_t2[ 2 ][ 1 ] * orientations[ o ][ 1 ] + \ rotation_t2[ 2 ][ 2 ] * orientations[ o ][ 2 ] ] eacpc_dw_minf[ 'diffusion_gradient_orientations' ] = orientations self.eacpc_dw_diffusion.updateMinf( eacpc_dw_minf )