# -*- coding: utf-8 -*- # This software and supporting documentation are distributed by # bioPICSEL # CEA/DSV/I²BM/MIRCen/LMN, Batiment 61, # 18, route du Panorama # 92265 Fontenay-aux-Roses # 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. ''' Utility classes and functions for BrainRAT toolbox. @author: Nicolas Souedet @organization: U{MIRCen} @license: U{CeCILL version 2} ''' __docformat__ = "epytext en" import sys, os, operator from decimal import Decimal def convertToDecimalList( l ): for i in xrange(len(l)): if type(l[i]) in (float, int) : l[i] = Decimal(str(l[i])) return l def getBorderInfo( image, width = ( 10, 10, 0 ), method = 'median' ): # Get border info to have a background value method = method.lower() command = [ 'AimsBorderInfo', '-i', '\"' + image + '\"', '-b', '\"' + str(width) + '\"', '-t', '\"' + method + '\"' ] f = os.popen( ' '.join( command ) ) borderValue = None for l in f.xreadlines(): s = len( method ) if l.startswith( method + ' :' ): borderValue = eval( l[ s + 2: ] ) break return borderValue # Process resampling information using mode # reffov must be defined in millimeters def getResamplingInfo( dims, voxelsizes, reffov = [], mode = -1, shape = 2 ): if len(dims) != len(voxelsizes) : raise Exception('Dimensions and sizes must contain the same number of elements.') else : count = len(dims) for i in xrange(len(voxelsizes)): voxelsizes[i] = convertToDecimalList(voxelsizes[i]) reffov = convertToDecimalList(reffov) # Complete reffov with missing default values for i in xrange(len(reffov), shape): reffov.append(None) # Initialize results rsp_dims = [] rsp_infos = [] for i in xrange(count): #rsp_infos.append( [ False for j in xrange(shape) ] ) rsp_infos.append( list() ) rsp_dims.append( list() ) # mode = -3 => force use of fov defined in reffov # mode = -2 => force use of minimum fov with reffov included # mode = -1 => force use of maximum fov with reffov included # mode >= 0 => use fov defined using dims[mode] and voxels[mode] if (mode == -2) or (mode == -1): if (mode == -2): # Process common fov sizes and resample all images using minimum fov op = operator.lt else: # Process common fov sizes and resample all images using maximum fov op = operator.gt for j in xrange(shape): if (reffov[j] is None) : # Get maximum or minimum image fov for i in xrange(count): if (reffov[j] is None) \ or op(( dims[i][j] * voxelsizes[i][j] ), reffov[j]) : reffov[j] = ( dims[i][j] * voxelsizes[i][j] ) elif mode >= 0 : # Mode is used as the index of reference image to using for fov resize for j in xrange(shape): # Get dims[mode] and voxelsizes[mode] to process reffov reffov[j] = ( dims[mode][j] * voxelsizes[mode][j] ) if mode is None : # No resize for i in xrange(count): rsp_dims[i] += dims[i] else : # reffov to resize all images for i in xrange(count): for j in xrange(shape): if ( j < len(reffov) ) \ and (not reffov[j] is None) \ and ( ( dims[i][j] * voxelsizes[i][j] ) != reffov[j] ): # Resample image using max fov size rsp_dims[i].append( int( ( reffov[j] ) / voxelsizes[i][j] ) ) else : # Do not resample for the dimensions rsp_dims[i].append( dims[i][j] ) for i in xrange(count): rsp_infos[i] += [(rsp_dims[i][j] != dims[i][j]) for j in xrange(shape)] return rsp_dims, rsp_infos