# -*- coding: utf-8 -*- # 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 import math from brainvisa.data.qtgui.neuroDataGUI import * from brainvisa import registration name = 'Fiber Tracking' userLevel=1 signature=Signature( 'model_type', Choice( "DTI", "QBall" ), 'algorithm', Choice( "Likelihood", "Best choice", "Probabilistic"), 'interpolation', Choice( "Raw data", "Directions", "None" ), 't2_diffusion', ReadDiskItem( 'T2 Diffusion MR', shfjGlobals.aimsVolumeFormats ), 'dw_diffusion', ReadDiskItem( 'DW Diffusion MR', shfjGlobals.aimsVolumeFormats ), ) def buildNewSignature( self, model, algo ): paramSignature = [ 'model_type', Choice( "DTI", "QBall" ) ] paramSignature += [ 'algorithm', Choice( "Likelihood", "Best choice", "Probabilistic") ] if model=='QBall': interpolationChoices=("Directions", "None",) else: interpolationChoices=( "Raw data", "None", ) if self.interpolation not in interpolationChoices: self.interpolation=None paramSignature += [ 'interpolation', Choice( *interpolationChoices ) ] paramSignature += [ 't2_diffusion', ReadDiskItem( 'T2 Diffusion MR', shfjGlobals.aimsVolumeFormats ) ] paramSignature += [ 'dw_diffusion', ReadDiskItem( 'DW Diffusion MR', shfjGlobals.aimsVolumeFormats ) ] if ( model == "QBall"): paramSignature += ['model', ReadDiskItem('QBall model', 'Bucket' )] paramSignature += ['orientations', ReadDiskItem('Any Type', getAllFormats() )] else: paramSignature += ['model', ReadDiskItem('DTI model', 'Bucket' )] paramSignature += ['starting_points', ReadDiskItem( 'Tracking regions', getDiskItemType( 'ROI' ).formats )] paramSignature += ['starting_points_transformation', ReadDiskItem( 'Transformation matrix','Transformation matrix' )] paramSignature += ['tracking_mask', ReadDiskItem( 'Diffusion mask', 'Aims readable volume formats')] if ( algo == "Probabilistic" or algo == "Best choice" ): paramSignature += ['alpha_map', WriteDiskItem('Diffusion Anisotropy', 'Aims writable volume formats' )] paramSignature += ['points_per_voxel', Integer()] paramSignature += ['max_angle', Float()] paramSignature += ['step_length', Float()] paramSignature += ['bundles', WriteDiskItem( 'Fascicles bundles', 'Aims bundles' )] paramSignature += ['density_map', WriteDiskItem('Diffusion Density Map', 'Aims writable volume formats' )] paramSignature += ['storing_step', Integer()] paramSignature += ['dimT_density_map', Integer()] paramSignature += ['sizeT_density_map', Integer()] signature = Signature( *paramSignature ) # Set optional parameters signature[ 'starting_points_transformation' ].mandatory = False signature['density_map'].mandatory=False signature['storing_step'].mandatory=False signature['dimT_density_map'].mandatory=False signature['sizeT_density_map'].mandatory=False signature['density_map'].userLevel=1 signature[ 'storing_step' ].userLevel = 1 signature[ 'dimT_density_map' ].userLevel = 1 signature[ 'sizeT_density_map' ].userLevel = 1 if ( model == "QBall" ): signature['orientations'].mandatory=False signature['t2_diffusion'].mandatory=False signature['dw_diffusion'].mandatory=False else: signature['model'].mandatory=False # Change the signature self.changeSignature( signature ) if self.interpolation is None: self.interpolation=interpolationChoices[0] # link Parameters self.linkParameters( 'dw_diffusion', 't2_diffusion' ) self.linkParameters( 'model', 't2_diffusion' ) self.linkParameters( 'starting_points', 'model' ) self.linkParameters( 'tracking_mask', 'starting_points' ) #signature[ 'starting_points_transformation' ].mandatory = False self.linkParameters( 'step_length', 'tracking_mask', computeDefaultStepLength ) self.linkParameters( 'points_per_voxel', 'algorithm', initializePointsPerVoxel ) self.linkParameters( 'max_angle', 'algorithm', initializeMaxAngle ) if ( algo == "Probabilistic" or algo == "Best choice" ): self.linkParameters( 'alpha_map', 'tracking_mask' ) self.linkParameters( 'bundles', 'starting_points' ) self.linkParameters( 'density_map', 'bundles' ) self.storing_step = 1 self.dimT_density_map = 50 self.sizeT_density_map = 5 def computeDefaultStepLength( values, process ): if values.tracking_mask is not None: dim = values.tracking_mask.get( 'voxel_size' ) if dim is not None: dim = dim[ :3 ] return float( min( *dim ) ) / 2.0 return None def initializePointsPerVoxel( values, process ): if ( values.algorithm == "Likelihood" ): return 1 if ( values.algorithm == "Probabilistic" ): return 20 if ( values.algorithm == "Best choice" ): return 5 return None def initializeMaxAngle( values, process ): #if getattr( values, 'bucket_file', None ) is not None: #orientations = values.bucket_file.get( 'qball_orientations' ) #if orientations is not None: #maxRadian = orientationsSets().get( len( orientations ) ) #if maxRadian is not None: #maxRadian = maxRadian[ 1 ] #return maxRadian * 180 / math.pi if ( values.algorithm == "Likelihood" ): return 45 if ( values.algorithm == "Probabilistic" ): return 30 if ( values.algorithm == "Best choice" ): return 30 return None def initialization( self ): self.buildNewSignature( self.model_type, self.algorithm ) self.addLink( None, [ 'model_type', 'algorithm' ], self.buildNewSignature ) def execution( self, context ): if self.model_type=="DTI" and self.algorithm=="Likelihood" and self.interpolation=="Directions": raise ValueError( _t_( 'You can not use Directions interpolation with DTI model and likelihood algorithm !') ) if self.model_type=="QBall" and self.interpolation=="Raw data": raise ValueError( _t_( 'You can not use raw data interpolation with QBall Model !') ) if self.model_type=="QBall" and self.model is None: raise ValueError( _t_( 'With QBall Model, You must provide bucket file' ) ) # Creation d un fichier temporaire contenant les parametres de la commande comistTracking optionFile = context.temporary( 'Text file' ) fid= open( optionFile.fullPath(), 'w' ) fid.writelines( '-verbose\n' ) fid.writelines( '-m\n' ) fid.writelines( self.tracking_mask.fullPath() + '\n' ) fid.writelines( '-cm\n' ) fid.writelines( self.tracking_mask.fullPath() + '\n' ) fid.writelines( '-roi\n' ) fid.writelines( self.tracking_mask.fullPath() + '\n' ) fid.writelines( '-roi_in\n' ) fid.writelines( self.starting_points.fullPath() + '\n' ) fid.writelines( '-n\n' ) fid.writelines( str(self.points_per_voxel) + '\n' ) fid.writelines( '-b\n' ) fid.writelines( self.bundles.fullPath() + '\n' ) fid.writelines( '-d\n' ) fid.writelines( self.density_map.fullPath() + '\n' ) if self.model_type=='DTI' and self.t2_diffusion is not None: if self.dw_diffusion is not None: fid.writelines( '-t2\n' ) fid.writelines( self.t2_diffusion.fullPath() + '\n' ) fid.writelines( '-dw\n' ) fid.writelines( self.dw_diffusion.fullPath() + '\n' ) if self.model_type=='QBall' and self.model is not None: fid.writelines( '-f\n' ) fid.writelines( self.model.fullPath() + '\n' ) #if ( ( self.algorithm != 'Likelihood' or self.model == 'QBall') #and self.orientation_file is not None ): if self.model_type == 'QBall': if self.orientations is not None: orientation_file=self.orientations else: orientations = self.model.get( 'qball_orientations' ) if orientations is None: raise RuntimeError( '"qball_orientations" are missing from model' ) else: orientation_file = context.temporary( 'Text file' ) f = open( orientation_file.fullPath(), 'w' ) print >> f, len( orientations ) for x, y, z in orientations: print >> f, x, y, z f.close() fid.writelines( '-ori\n' ) fid.writelines( orientation_file.fullPath() + '\n' ) if self.storing_step is not None: fid.writelines( '-s\n' ) fid.writelines( str(self.storing_step) + '\n' ) if self.dimT_density_map is not None: fid.writelines( '-dimT\n' ) fid.writelines( str(self.dimT_density_map) + '\n' ) if self.sizeT_density_map is not None: fid.writelines( '-sizeT\n' ) fid.writelines( str(self.sizeT_density_map) + '\n' ) maximum_length = 10000 fid.writelines( '-maxl\n' ) fid.writelines( str(maximum_length) + '\n' ) if self.algorithm != 'Likelihood' and self.alpha_map is not None: if self.alpha_map.isReadable(): fid.writelines( '--map_in\n' ) else: fid.writelines( '--map_out\n' ) fid.writelines( self.alpha_map.fullPath() + '\n' ) if self.model_type=='DTI': fid.writelines( '-DTI\n' ) if self.algorithm == "Likelihood": fid.writelines( '-likely\n' ) if self.algorithm == "Probabilistic": fid.writelines( '-proba\n' ) if self.algorithm == "Best choice": fid.writelines( '-proba\n' ) fid.writelines( '-bc\n' ) if self.interpolation == "None": fid.writelines( '-noInt\n' ) if self.interpolation == "Directions": fid.writelines( '-dInt\n' ) if self.starting_points_transformation is not None: fid.writelines( '-rt\n' ) fid.writelines( self.starting_points_transformation.fullPath() + '\n' ) if self.step_length is not None: fid.writelines( '-step\n' ) fid.writelines( str(self.step_length) + '\n' ) if self.max_angle is not None: fid.writelines( '-a\n' ) fid.writelines( str(self.max_angle / 180 * math.pi) + '\n' ) fid.close() command = [ 'comistTracking', '--optionsfile', optionFile.fullPath() ] context.log( 'comistTracking parameters', html = '
' + htmlEscape( open(optionFile.fullPath()).read() ) + '', icon = 'icon_system.png' ) context.system( *command ) # Set referentials of new objects trManager = registration.getTransformationManager() if self.signature.has_key( 'alpha_map' ): trManager.copyReferential( self.t2_diffusion, self.alpha_map ) trManager.copyReferential( self.t2_diffusion, self.bundles ) trManager.copyReferential( self.t2_diffusion, self.density_map )