コード例 #1
0
ファイル: t_calc.py プロジェクト: FNNDSC/scripts
    def sub(master, tracks, outputFile=None, verbose=False, threadName="Global"):
        """
    Subtract tracks from master. Both parameters are nibabel.trackvis.streamlines objects.
    
    Calculation cost: O(M*N)
    
    Returns the result as a nibabel.trackvis.streamlines object or writes it to the file system if an outputFile is specified.
    """
        masterSizeBefore = len(master)

        subtractedCount = 0

        # O(M*N)
        for t in xrange(masterSizeBefore):

            if subtractedCount == len(tracks):
                # no way we can subtract more.. stop the loop
                return master

            c.debug(
                threadName
                + ": Looking for more tracks to subtract.. [Check #"
                + str(t)
                + "/"
                + str(masterSizeBefore)
                + "]",
                verbose,
            )

            if master[t] == -1:
                # this fiber was already removed, skip to next one
                continue

            for u in xrange(len(tracks)):

                if tracks[u] == -1:
                    # this fiber was already removed, skip to next one
                    continue

                # compare fiber
                if [p for points in master[t][0] for p in points] == [p for points in tracks[u][0] for p in points]:
                    # fibers are equal, set them as dirty
                    master[t] = -1
                    tracks[u] = -1
                    subtractedCount += 1
                    # ... and jump out
                    break

        master = filter(lambda t: t != -1, master)

        if not outputFile:
            return master
        else:
            # write it out to disk
            io.saveTrk(outputFile, master, None, None, True)
コード例 #2
0
    def sub(master,
            tracks,
            outputFile=None,
            verbose=False,
            threadName='Global'):
        '''
    Subtract tracks from master. Both parameters are nibabel.trackvis.streamlines objects.
    
    Calculation cost: O(M*N)
    
    Returns the result as a nibabel.trackvis.streamlines object or writes it to the file system if an outputFile is specified.
    '''
        masterSizeBefore = len(master)

        subtractedCount = 0

        # O(M*N)
        for t in xrange(masterSizeBefore):

            if subtractedCount == len(tracks):
                # no way we can subtract more.. stop the loop
                return master

            c.debug(
                threadName +
                ': Looking for more tracks to subtract.. [Check #' + str(t) +
                '/' + str(masterSizeBefore) + ']', verbose)

            if master[t] == -1:
                # this fiber was already removed, skip to next one
                continue

            for u in xrange(len(tracks)):

                if tracks[u] == -1:
                    # this fiber was already removed, skip to next one
                    continue

                # compare fiber
                if [p for points in master[t][0] for p in points
                    ] == [p for points in tracks[u][0] for p in points]:
                    # fibers are equal, set them as dirty
                    master[t] = -1
                    tracks[u] = -1
                    subtractedCount += 1
                    # ... and jump out
                    break

        master = filter(lambda t: t != -1, master)

        if not outputFile:
            return master
        else:
            # write it out to disk
            io.saveTrk(outputFile, master, None, None, True)
コード例 #3
0
ファイル: t_calc.py プロジェクト: FNNDSC/scripts
    def run(self, input, output, mode, verbose, jobs):

        if len(input) < 2:
            c.error("Please specify at least two *.trk files as input!")
            sys.exit(2)

        if os.path.exists(output):
            # abort if file already exists
            c.error("File " + str(output) + " already exists..")
            c.error("Aborting..")
            sys.exit(2)

        jobs = int(jobs)

        if jobs < 1 or jobs > 32:
            jobs = 1

        # load 'master'
        mTracks = io.loadTrk(input[0])

        # copy the tracks and the header from the 'master'
        c.info("Master is " + input[0])
        outputTracks = mTracks[0]
        c.info("Number of tracks: " + str(len(outputTracks)))
        header = mTracks[1]

        # remove the first input
        input.pop(0)

        if mode == "add":
            #
            # ADD
            #

            for i in input:
                iTracks = io.loadTrk(i)

                # add the tracks
                c.debug("Adding " + str(len(iTracks[0])) + " tracks from " + i + " to master..", verbose)
                outputTracks = TrackvisCalcLogic.add(outputTracks, iTracks[0])

            c.debug("Number of output tracks after final addition: " + str(len(outputTracks)), verbose)

        elif mode == "sub":
            #
            # SUB
            #

            c.debug("Using " + str(jobs) + " threads..", verbose)

            mergedOutputTracks = outputTracks[:]

            for i in input:
                iTracks = io.loadTrk(i)

                # subtract the tracks
                c.info("Subtracting " + i + " (" + str(len(iTracks[0])) + " tracks) from master..")

                #
                # THREADED COMPONENT
                #
                numberOfThreads = jobs
                c.info("Splitting master into " + str(jobs) + " pieces..")
                splittedOutputTracks = u.split_list(mergedOutputTracks, 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):
                    # mark thread as alive
                    a[n] = True
                    # fire the thread and give it a filename based on the number
                    tmpFile = tempfile.mkstemp(".trk", "t_calc")[1]
                    f[n] = tmpFile
                    t[n] = Process(
                        target=TrackvisCalcLogic.sub,
                        args=(splittedOutputTracks[n][:], iTracks[0][:], tmpFile, verbose, "Thread-" + str(n + 1)),
                    )
                    c.info("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
                #
                c.info("All Threads done!")

                c.info("Merging output..")
                # now read all the created tempFiles and merge'em to one
                # first thread output is the master here
                tmpMaster = f[0]
                tMasterTracks = io.loadTrk(tmpMaster)
                for tmpFileNo in xrange(1, len(f)):
                    tTracks = io.loadTrk(f[tmpFileNo])

                    # add them
                    mergedOutputTracks = TrackvisCalcLogic.add(tMasterTracks[0], tTracks[0])

                c.info("Merging done!")

            # some stats
            c.info("Number of output tracks after final removal: " + str(len(mergedOutputTracks)))
            outputTracks = mergedOutputTracks

        # now save the outputTracks
        io.saveTrk(output, outputTracks, header)

        c.info("All done!")
コード例 #4
0
    def run(self, input, output, mode, verbose, jobs):

        if len(input) < 2:
            c.error('Please specify at least two *.trk files as input!')
            sys.exit(2)

        if os.path.exists(output):
            # abort if file already exists
            c.error('File ' + str(output) + ' already exists..')
            c.error('Aborting..')
            sys.exit(2)

        jobs = int(jobs)

        if jobs < 1 or jobs > 32:
            jobs = 1

        # load 'master'
        mTracks = io.loadTrk(input[0])

        # copy the tracks and the header from the 'master'
        c.info('Master is ' + input[0])
        outputTracks = mTracks[0]
        c.info('Number of tracks: ' + str(len(outputTracks)))
        header = mTracks[1]

        # remove the first input
        input.pop(0)

        if mode == 'add':
            #
            # ADD
            #

            for i in input:
                iTracks = io.loadTrk(i)

                # add the tracks
                c.debug(
                    'Adding ' + str(len(iTracks[0])) + ' tracks from ' + i +
                    ' to master..', verbose)
                outputTracks = TrackvisCalcLogic.add(outputTracks, iTracks[0])

            c.debug(
                'Number of output tracks after final addition: ' +
                str(len(outputTracks)), verbose)

        elif mode == 'sub':
            #
            # SUB
            #

            c.debug('Using ' + str(jobs) + ' threads..', verbose)

            mergedOutputTracks = outputTracks[:]

            for i in input:
                iTracks = io.loadTrk(i)

                # subtract the tracks
                c.info('Subtracting ' + i + ' (' + str(len(iTracks[0])) +
                       ' tracks) from master..')

                #
                # THREADED COMPONENT
                #
                numberOfThreads = jobs
                c.info('Splitting master into ' + str(jobs) + ' pieces..')
                splittedOutputTracks = u.split_list(mergedOutputTracks,
                                                    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):
                    # mark thread as alive
                    a[n] = True
                    # fire the thread and give it a filename based on the number
                    tmpFile = tempfile.mkstemp('.trk', 't_calc')[1]
                    f[n] = tmpFile
                    t[n] = Process(target=TrackvisCalcLogic.sub,
                                   args=(splittedOutputTracks[n][:],
                                         iTracks[0][:], tmpFile, verbose,
                                         'Thread-' + str(n + 1)))
                    c.info("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
                #
                c.info("All Threads done!")

                c.info("Merging output..")
                # now read all the created tempFiles and merge'em to one
                # first thread output is the master here
                tmpMaster = f[0]
                tMasterTracks = io.loadTrk(tmpMaster)
                for tmpFileNo in xrange(1, len(f)):
                    tTracks = io.loadTrk(f[tmpFileNo])

                    # add them
                    mergedOutputTracks = TrackvisCalcLogic.add(
                        tMasterTracks[0], tTracks[0])

                c.info("Merging done!")

            # some stats
            c.info('Number of output tracks after final removal: ' +
                   str(len(mergedOutputTracks)))
            outputTracks = mergedOutputTracks

        # now save the outputTracks
        io.saveTrk(output, outputTracks, header)

        c.info('All done!')
コード例 #5
0
ファイル: fyborg.py プロジェクト: FNNDSC/fyborg
def fyborg( trkFile, outputTrkFile, actions, *args ):

  if not actions:
    c.error( "We gotta do something.." )
    return

  showDebug = 'debug' in args

  singleThread = 'singlethread' in args

  c.debug( "trkFile:" + str( trkFile ), showDebug )
  c.debug( "outputTrkFile:" + str( outputTrkFile ), showDebug )
  c.debug( "args:" + str( args ), showDebug )



  # load trk file
  s = io.loadTrk( trkFile )
  tracks = s[0]
  origHeader = s[1]
  tracksHeader = numpy.copy( s[1] )
  numberOfScalars = origHeader['n_scalars']
  scalars = origHeader['scalar_name'].tolist()
  numberOfTracks = origHeader['n_count']

  # show some file informations
  printTrkInfo( tracksHeader, trkFile )

  # grab the scalarNames
  scalarNames = []
  for a in actions:
    if a.scalarName() != 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()
  c.info( 'Splitting master into ' + str( numberOfThreads ) + ' pieces..' )
  splittedOutputTracks = u.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=fyborgLooper_, args=( splittedOutputTracks[n][:], tracksHeader, tmpFile, __actions, showDebug, n + 1 ) )
    c.info( "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
  #
  c.info( "All Threads done!" )

  #
  # Merging stage
  #
  c.info( "Merging tracks.." )

  outputTracks = []
  # now read all the created tempFiles and merge'em to one
  for tmpFileNo in xrange( 0, len( f ) ):
    tTracks = io.loadTrk( f[tmpFileNo] )

    # add them
    outputTracks.extend( tTracks[0] )

  c.info( "Merging done!" )

  io.saveTrk( outputTrkFile, outputTracks, tracksHeader, None, True )

  c.info( "All done!" )
コード例 #6
0
ファイル: fyborg.py プロジェクト: FNNDSC/fyborg
def fyborgLooper_( tracks, tracksHeader, outputTrkFile, actions, showDebug, threadNumber ):

  import numpy

  numberOfTracks = len( tracks )

  # the buffer for the new tracks
  newTracks = []

  # now loop through the tracks
  for tCounter, t in enumerate( tracks ):

    # some debug stats
    c.debug( 'Thread-' + str( threadNumber ) + ': Processing ' + str( tCounter + 1 ) + '/' + str( numberOfTracks ), showDebug )

    # generate a unique ID for this track
    uniqueId = str( threadNumber ) + str( tCounter )

    tCoordinates = t[0]
    tScalars = t[1]

    # buffer for fiberScalars
    _fiberScalars = {}

    # first round: mapping per fiber
    # .. execute each action and buffer return value (scalar)
    for a in actions:
      value = a.scalarPerFiber( uniqueId, tCoordinates, tScalars )
      _fiberScalars[a.scalarName()] = value

    #
    # Coordinate Loop
    #
    # buffer for coordinate scalars)    
    scalars = []

    # second round: mapping per coordinate
    for cCounter, coords in enumerate( tCoordinates ):

      _coordScalars = {}
      _mergedScalars = [] # this is the actual buffer for ordered fiber and coord scalars merged together

      # .. execute each action and buffer return value (scalar)
      for a in actions:
        value = a.scalarPerCoordinate( uniqueId, coords[0], coords[1], coords[2] ) # pass x,y,z
        _coordScalars[a.scalarName()] = value

      # now merge the old scalars and the fiber and coord scalars
      # this preserves the ordering of the configured actions
      if tScalars != None:
        _mergedScalars.extend( tScalars[cCounter] )

      for a in actions:
        value = _fiberScalars[a.scalarName()]
        if value != FyAction.NoScalar:
          _mergedScalars.append( value )
        else:
          # no fiber scalar, check if there is a coord scalar
          value = _coordScalars[a.scalarName()]
          if value != FyAction.NoScalar:
            _mergedScalars.append( value )

      # attach scalars
      scalars.append( _mergedScalars )

    # validate the fibers using the action's validate methods
    validator = []
    for a in actions:
      validator.append( a.validate( uniqueId ) )

    if all( validator ):
      # this is a valid fiber
      # .. add the new track with the coordinates, the new scalar array and the properties
      newScalars = numpy.asarray( scalars )
      newTracks.append( ( t[0], newScalars, t[2] ) )

  # save everything
  io.saveTrk( outputTrkFile, newTracks, tracksHeader, None, True )