def loop( fibers_file, volume, lh_mesh_file, rh_mesh_file, neighbors, singleThread=False ): # load trk file s = nibabel.trackvis.read( fibers_file ) tracks = s[0] # load meshes lh_verts, lh_faces = nibabel.freesurfer.read_geometry( lh_mesh_file ) rh_verts, rh_faces = nibabel.freesurfer.read_geometry( rh_mesh_file ) # load volume and get qForm matrix _image = nibabel.load( volume ) qForm = _image.get_header().get_qform() qFormM = numpy.matrix( qForm ) # # THREADED COMPONENT # if singleThread: numberOfThreads = 1 else: numberOfThreads = 1#3#multiprocessing.cpu_count() print 'Splitting master into ' + str( numberOfThreads ) + ' pieces..' splittedOutputTracks = Utility.split_list( tracks[:], numberOfThreads ) # list of threads t = [None] * numberOfThreads # list of alive flags a = [None] * numberOfThreads # list of matrix tempFiles mf = [None] * numberOfThreads for n in xrange( numberOfThreads ): # mark thread as alive a[n] = True # also create a temp file for the hdf5 file matrix_file = tempfile.mkstemp( '.h5', 'fyborg' )[1] mf[n] = matrix_file t[n] = Process( target=LooperWithMatrix._looper_, args=( splittedOutputTracks[n][:], lh_mesh_file, rh_mesh_file, int(neighbors), qFormM, matrix_file, n + 1 ) ) print "Starting Thread-" + str( n + 1 ) + "..." t[n].start() allDone = False while not allDone: time.sleep( 1 ) for n in xrange( numberOfThreads ): a[n] = t[n].is_alive() if not any( a ): # if no thread is alive allDone = True # # END OF THREADED COMPONENT # print "All Threads done!" #print mf return mf
def loop(fibers_file, fibers_output_file, actions, singleThread=False): # load trk file s = nibabel.trackvis.read( fibers_file ) tracks = s[0] origHeader = s[1] tracksHeader = numpy.copy( s[1] ) numberOfScalars = origHeader['n_scalars'] scalars = origHeader['scalar_name'].tolist() numberOfTracks = origHeader['n_count'] # grab the scalarNames scalarNames = [] for a in actions: if a.scalarName() != _actions.FyAction.NoScalar: scalarNames.append( a.scalarName() ) # increase the number of scalars tracksHeader['n_scalars'] += len( scalarNames ) # .. attach the new scalar names for i in range( len( scalarNames ) ): tracksHeader['scalar_name'][numberOfScalars + i] = scalarNames[i] # # THREADED COMPONENT # if singleThread: numberOfThreads = 1 else: numberOfThreads = multiprocessing.cpu_count() print 'Splitting master into ' + str( numberOfThreads ) + ' pieces..' splittedOutputTracks = Utility.split_list( tracks[:], numberOfThreads ) # list of threads t = [None] * numberOfThreads # list of alive flags a = [None] * numberOfThreads # list of tempFiles f = [None] * numberOfThreads for n in xrange( numberOfThreads ): # configure actions __actions = [] for act in actions: __actions.append( act ) # mark thread as alive a[n] = True # fire the thread and give it a filename based on the number tmpFile = tempfile.mkstemp( '.trk', 'fyborg' )[1] f[n] = tmpFile t[n] = Process( target=Looper._looper_, args=( splittedOutputTracks[n][:], tracksHeader, tmpFile, __actions, n + 1 ) ) print "Starting Thread-" + str( n + 1 ) + "..." t[n].start() allDone = False while not allDone: time.sleep( 1 ) for n in xrange( numberOfThreads ): a[n] = t[n].is_alive() if not any( a ): # if no thread is alive allDone = True # # END OF THREADED COMPONENT # print "All Threads done!" # # Merging stage # print "Merging tracks..", f outputTracks = [] # now read all the created tempFiles and merge'em to one for tmpFileNo in xrange( 0, len( f ) ): tTracks = nibabel.trackvis.read( f[tmpFileNo] ) # add them outputTracks.extend( tTracks[0] ) print "Merging done!" nibabel.trackvis.write( fibers_output_file, outputTracks, tracksHeader ) print "All done!"