# -*- 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 * from registration import getTransformationManager import operator from copy import copy name='Generic Diffusion Importation' userLevel=0 signature = Signature( 'files', ListOf( ReadDiskItem( 'Raw DW Diffusion MR', 'Aims readable volume formats' ) ), 'bValuesAndOrientations', ReadDiskItem( 'Text file', 'Text file' ), 't2_diffusion', WriteDiskItem( 'Raw T2 Diffusion MR', 'Aims writable volume formats' ), 'dw_diffusion', WriteDiskItem( 'Raw DW Diffusion MR', 'Aims writable volume formats' ), 'dw_already_corrected', Boolean(), ) def switchCorrection( self, dw_already_corrected ): if dw_already_corrected: dwType = 'Non oriented DW Diffusion MR' else: dwType = 'Raw DW Diffusion MR' if dwType == self.signature[ 'dw_diffusion' ].type.name: return signature = copy( self.signature ) signature[ 'dw_diffusion' ] = WriteDiskItem( dwType, 'GIS image' ) self.changeSignature( signature ) def initialization( self ): self.linkParameters( 'dw_diffusion', 't2_diffusion' ) self.dw_already_corrected = False self.addLink( None, 'dw_already_corrected', self.switchCorrection ) self.signature[ 'files' ].databaseUserLevel = 2 self.signature[ 't2_diffusion' ].browseUserLevel = 3 self.signature[ 'dw_diffusion' ].browseUserLevel = 3 def execution( self, context ): #for v in self.files: #context.write( '', v, '' ) #context.write( v.attributes() ) #return volumesCount = reduce( operator.add, [ int( v.get( 'volume_dimension', search_header=True )[ 3 ] ) for v in self.files] ) bValues = [] orientations = [] lineNumber = 0 for line in open( self.bValuesAndOrientations.fullPath() ): lineNumber += 1 line = line.strip() if not line or line[ 0 ] == '#': continue if line == '-': # Volumes with negative b-value are ignored b, x, y, z = -1, 0, 0, 0 else: try: b, x, y, z = line.split() except: raise RuntimeError( _t_( 'Error in orientation file "%(file)s" on line %(line)d' ) % { 'file': self.bValuesAndOrientations.fullPath(), 'line': lineNumber } ) bValues.append( float( b ) ) orientations.append( [ float(x), float(y), float(z) ] ) if len( orientations ) != volumesCount: raise RuntimeError( _t_( 'The b-values and orientation file must have as many lines as the number of input volumes. Input was %(volumes)d volumes and %(lines)d lines.' % { 'volumes': volumesCount, 'lines': len( orientations ) } ) ) # Create temporary headers headers = [] volumeIndex = 0 for image in self.files: lastVolume = volumeIndex + image.get( 'volume_dimension' )[ 3 ] header = context.temporary( 'Text file' ) headers.append( header ) context.write( _t_('Building meta-info for data %s') % image.fullName() ) header.setMinf( 'bvalues', bValues[ volumeIndex : lastVolume ] ) header.setMinf( 'diffusion_gradient_orientations', orientations[ volumeIndex : lastVolume] ) volumeIndex = lastVolume context.write( _t_('Concatenating data') ) command = [ 'comistConcatenation', '-verbose', '-t2', self.t2_diffusion, '-dw', self.dw_diffusion, '-i' ] + self.files + [ '-m' ] + [ x.minfFileName() for x in headers ] apply( context.system, command ) transformManager = getTransformationManager() transformManager.createNewReferentialFor( self.t2_diffusion ) transformManager.copyReferential( self.t2_diffusion, self.dw_diffusion )