# -*- 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. # Library imports import os, os.path, copy, math # Brainvisa imports from brainvisa.processes import * # Aims file formats from brainvisa import shelltools import shfjGlobals # Import brainrat libraries import brainrat.uicheck as uicheck # Process information name = 'RenameImageSeries' userLevel = 0 signature = Signature( 'Input_ListOf_2dImage', ListOf( ReadDiskItem( '2D Image', 'Biology 2D image formats' ) ), 'Series_Name_Prefix', String(), 'Digit_Number', Integer(), 'Format_Extension', String(), 'Output_Directory', ReadDiskItem( 'Directory', 'Directory' ), 'Output_ListOf_2dImage', ListOf( WriteDiskItem( '2D Image', 'Biology 2D image formats' ) ) ) def setDefaultOutput_ListOf_2dImage(self, obj, value) : if self.Input_ListOf_2dImage and (len(self.Input_ListOf_2dImage) > 0) and self.Series_Name_Prefix is not None : # Generate default output list of 2d image name OutputList = list() filesCount = len( self.Input_ListOf_2dImage ) directoryPath = os.path.dirname( self.Input_ListOf_2dImage[0].fullPath() ) # Check parameters if self.Digit_Number is None : return if ( ( self.Output_Directory is None ) or ( len( self.Output_Directory.fullPath() ) == 0 ) or ( not os.path.exists( self.Output_Directory.fullPath() ) ) ) : Output_DirectoryPath = directoryPath else : Output_DirectoryPath = self.Output_Directory.fullPath() # Create sorted file list using numbers contained in files name filelist = list() for file in self.Input_ListOf_2dImage : # Get numbers from file name foundnumbers = [ int(value) for value in re.findall( '\d+', file.fullPath() ) ] # Add the found numbers for the file name filelist += [ foundnumbers + [ file ] ] # Sort the list using found numbers in file names filelist.sort() for index in xrange( filesCount ): nameIn = filelist[index][-1].fullPath() suffix = str(index + 1).rjust( self.Digit_Number , '0' ) nameOut = os.path.join( Output_DirectoryPath, self.Series_Name_Prefix + suffix + self.Format_Extension ) OutputList.append(nameOut) return OutputList def input_ListOf_2dImageChanged( self, Input_ListOf_2dImage ): # Check if the dynamic of the file needed to be rescaled during conversion if ( not Input_ListOf_2dImage is None ) and ( len( Input_ListOf_2dImage ) > 0 ) and self.Format_Extension is None : # Set the format extension of the first file in the serie self.Format_Extension = os.path.splitext( unicode( Input_ListOf_2dImage[ 0 ] ) )[ 1 ] def initialization( self ): # Sets default parameters value self.Digit_Number = 3 self.Output_ListOf_2dImage = None # Add links self.addLink(None, 'Input_ListOf_2dImage' , self.input_ListOf_2dImageChanged ) self.linkParameters( 'Output_ListOf_2dImage', [ 'Input_ListOf_2dImage', 'Series_Name_Prefix', 'Digit_Number', 'Format_Extension', 'Output_Directory' ], self.setDefaultOutput_ListOf_2dImage ) def execution( self, context ): try : # Write start message filesCount = len( self.Input_ListOf_2dImage ) context.write( _t_( 'Renaming files [ count : %s ]' ) % filesCount, ' ...' ) for index in xrange( filesCount ): nameIn = self.Input_ListOf_2dImage[index].fullPath() nameOut = self.Output_ListOf_2dImage[index].fullPath() #nameIn = filelist[index][-1].fullPath() #suffix = str(index + 1).rjust( self.Digit_Number , '0' ) #nameOut = os.path.join( Output_DirectoryPath, self.Series_Name_Prefix + suffix + self.Format_Extension ) context.write( '--> ', _t_( 'Slice number : %s' ) % index, ', \'%s\' -> \'%s\'' % ( os.path.basename( nameIn ), os.path.basename( nameOut ) ) ) #shelltools.cp( nameIn, nameOut ) context.system( 'AimsFileConvert', '-i', nameIn, '-o', nameOut ) except Exception, error : context.error( _t_( uicheck.messages[ 'processingerrormessage' ] ) )