def filtering( self, inputs, outputs, length, cortex_only ): ''' Filter the mapped fibers. ''' # check if we have all required input data # we need at least: # - outputs['fibers_mapped'] == Track file in T1 space with mapped scalars if not os.path.exists( outputs['fibers_mapped'] ): c.error( Colors.RED + 'Could not find ' + Colors.YELLOW + outputs['fibers_mapped'] + Colors.RED + ' but we really need it to start with stage 4!!' + Colors._CLEAR ) sys.exit( 2 ) # find the order of the mapped scalars header = io.loadTrkHeaderOnly( outputs['fibers_mapped'] ) scalars = list( header['scalar_name'] ) # split the length range length = length.split( ' ' ) min_length = int( length[0] ) max_length = int( length[1] ) # length filtering c.info( Colors.YELLOW + ' Filtering ' + Colors.PURPLE + 'fiber length' + Colors.YELLOW + ' to be ' + Colors.PURPLE + '>' + str( min_length ) + ' and <' + str( max_length ) + Colors.YELLOW + ' for ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped'] )[1] + Colors.YELLOW + ' and store as ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) fyborg.fyborg( outputs['fibers_mapped'], outputs['fibers_mapped_length_filtered'], [fyborg.FyFilterLengthAction( scalars.index( 'length' ), min_length, max_length )] ) header = io.loadTrkHeaderOnly( outputs['fibers_mapped_length_filtered'] ) new_count = header['n_count'] c.info( Colors.YELLOW + ' Number of tracks after ' + Colors.PURPLE + 'length filtering' + Colors.YELLOW + ': ' + str( new_count ) + Colors.YELLOW + Colors._CLEAR ) if cortex_only: # special cortex filtering c.info( Colors.YELLOW + ' Filtering for ' + Colors.PURPLE + 'valid cortex structures' + Colors.YELLOW + ' in ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered'] )[1] + Colors.YELLOW + ' and store as ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered_cortex_only'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) c.info( Colors.PURPLE + ' Conditions for valid fibers:' + Colors._CLEAR ) c.info( Colors.PURPLE + ' 1.' + Colors.YELLOW + ' The fiber track has to pass through the cerebral white matter. (Label values: ' + Colors.PURPLE + '[2, 41]' + Colors.YELLOW + ')' + Colors._CLEAR ) c.info( Colors.PURPLE + ' 2.' + Colors.YELLOW + ' The fiber track shall only touch sub-cortical structures not more than ' + Colors.PURPLE + '5 times' + Colors.YELLOW + '. (Label values: ' + Colors.PURPLE + '[10, 49, 16, 28, 60, 4, 43]' + Colors.YELLOW + ')' + Colors._CLEAR ) c.info( Colors.PURPLE + ' 3.' + Colors.YELLOW + ' The track shall not pass through the corpus callosum (Labels: ' + Colors.PURPLE + '[251, 255]' + Colors.YELLOW + ') and end in the same hemisphere (Labels: ' + Colors.PURPLE + '[1000-1035]' + Colors.YELLOW + ' for left, ' + Colors.PURPLE + '[2000-2035]' + Colors.YELLOW + ' for right).' + Colors._CLEAR ) fyborg.fyborg( outputs['fibers_mapped_length_filtered'], outputs['fibers_mapped_length_filtered_cortex_only'], [fyborg.FyFilterCortexAction( scalars.index( 'segmentation' ) )] ) header = io.loadTrkHeaderOnly( outputs['fibers_mapped_length_filtered_cortex_only'] ) new_count = header['n_count'] c.info( Colors.YELLOW + ' Number of tracks after ' + Colors.PURPLE + 'cortex filtering' + Colors.YELLOW + ': ' + str( new_count ) + Colors.YELLOW + Colors._CLEAR ) c.info( Colors.YELLOW + ' Copied filtered tracks from ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered_cortex_only'] )[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split( outputs['fibers_final'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) shutil.copyfile( outputs['fibers_mapped_length_filtered_cortex_only'], outputs['fibers_final'] ) else: c.info( Colors.YELLOW + ' Info: ' + Colors.PURPLE + 'Cortical _and_ sub-cortical structures ' + Colors.YELLOW + 'will be included..' + Colors._CLEAR ) c.info( Colors.YELLOW + ' Copied filtered tracks from ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered'] )[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split( outputs['fibers_final'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) shutil.copyfile( outputs['fibers_mapped_length_filtered'], outputs['fibers_final'] )
def mapping( self, inputs, outputs, radius ): ''' Map all detected scalar volumes to each fiber. ''' # check if we have all required input data # we need at least: # - outputs['fibers'] == Track file in T1 space # - outputs['segmentation'] == Label Map if not os.path.exists( outputs['fibers'] ): c.error( Colors.RED + 'Could not find ' + Colors.YELLOW + outputs['fibers'] + Colors.RED + ' but we really need it to start with stage 3!!' + Colors._CLEAR ) sys.exit( 2 ) if not os.path.exists( outputs['segmentation'] ): c.error( Colors.RED + 'Could not find ' + Colors.YELLOW + outputs['segmentation'] + Colors.RED + ' but we really need it to start with stage 3!!' + Colors._CLEAR ) sys.exit( 2 ) actions = [] for i in inputs: if i == 'fibers' or i == 'segmentation' or i == 'T1' or i == 'b0': # we do not map these continue if not os.path.exists( outputs[i + '_T1_space'] ): # we can't map this since we didn't find the file continue # for normal scalars: append it to the actions actions.append( fyborg.FyMapAction( i, outputs[i + '_T1_space'] ) ) c.info( Colors.YELLOW + ' Configuring mapping of ' + Colors.PURPLE + os.path.split( outputs[i + '_T1_space'] )[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split( outputs['fibers'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) # now the segmentation with the lookaround radius actions.append( fyborg.FyRadiusMapAction( 'segmentation', outputs['segmentation'], radius ) ) c.info( Colors.YELLOW + ' Configuring mapping of ' + Colors.PURPLE + os.path.split( outputs['segmentation'] )[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split( outputs['fibers'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) # and also the fiber length actions.append( fyborg.FyLengthAction() ) c.info( Colors.YELLOW + ' Configuring mapping of ' + Colors.PURPLE + 'fiber length' + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split( outputs['fibers'] )[1] + Colors.YELLOW + '!' + Colors._CLEAR ) # run, forest, run!! c.info( Colors.YELLOW + ' Performing configured mapping for ' + Colors.PURPLE + os.path.split( outputs['fibers'] )[1] + Colors.YELLOW + ' and storing as ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped'] )[1] + Colors.YELLOW + ' (~ 30 minutes)!' + Colors._CLEAR ) if self.__debug: fyborg.fyborg( outputs['fibers'], outputs['fibers_mapped'], actions, 'debug' ) else: fyborg.fyborg( outputs['fibers'], outputs['fibers_mapped'], actions )
def filtering(self, inputs, outputs, length, cortex_only): ''' Filter the mapped fibers. ''' # check if we have all required input data # we need at least: # - outputs['fibers_mapped'] == Track file in T1 space with mapped scalars if not os.path.exists(outputs['fibers_mapped']): c.error(Colors.RED + 'Could not find ' + Colors.YELLOW + outputs['fibers_mapped'] + Colors.RED + ' but we really need it to start with stage 4!!' + Colors._CLEAR) sys.exit(2) # find the order of the mapped scalars header = io.loadTrkHeaderOnly(outputs['fibers_mapped']) scalars = list(header['scalar_name']) # split the length range length = length.split(' ') min_length = int(length[0]) max_length = int(length[1]) # length filtering c.info(Colors.YELLOW + ' Filtering ' + Colors.PURPLE + 'fiber length' + Colors.YELLOW + ' to be ' + Colors.PURPLE + '>' + str(min_length) + ' and <' + str(max_length) + Colors.YELLOW + ' for ' + Colors.PURPLE + os.path.split(outputs['fibers_mapped'])[1] + Colors.YELLOW + ' and store as ' + Colors.PURPLE + os.path.split(outputs['fibers_mapped_length_filtered'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) fyborg.fyborg(outputs['fibers_mapped'], outputs['fibers_mapped_length_filtered'], [ fyborg.FyFilterLengthAction(scalars.index('length'), min_length, max_length) ]) header = io.loadTrkHeaderOnly(outputs['fibers_mapped_length_filtered']) new_count = header['n_count'] c.info(Colors.YELLOW + ' Number of tracks after ' + Colors.PURPLE + 'length filtering' + Colors.YELLOW + ': ' + str(new_count) + Colors.YELLOW + Colors._CLEAR) if cortex_only: # special cortex filtering c.info( Colors.YELLOW + ' Filtering for ' + Colors.PURPLE + 'valid cortex structures' + Colors.YELLOW + ' in ' + Colors.PURPLE + os.path.split(outputs['fibers_mapped_length_filtered'])[1] + Colors.YELLOW + ' and store as ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered_cortex_only'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) c.info(Colors.PURPLE + ' Conditions for valid fibers:' + Colors._CLEAR) c.info( Colors.PURPLE + ' 1.' + Colors.YELLOW + ' The fiber track has to pass through the cerebral white matter. (Label values: ' + Colors.PURPLE + '[2, 41]' + Colors.YELLOW + ')' + Colors._CLEAR) c.info( Colors.PURPLE + ' 2.' + Colors.YELLOW + ' The fiber track shall only touch sub-cortical structures not more than ' + Colors.PURPLE + '5 times' + Colors.YELLOW + '. (Label values: ' + Colors.PURPLE + '[10, 49, 16, 28, 60, 4, 43]' + Colors.YELLOW + ')' + Colors._CLEAR) c.info( Colors.PURPLE + ' 3.' + Colors.YELLOW + ' The track shall not pass through the corpus callosum (Labels: ' + Colors.PURPLE + '[251, 255]' + Colors.YELLOW + ') and end in the same hemisphere (Labels: ' + Colors.PURPLE + '[1000-1035]' + Colors.YELLOW + ' for left, ' + Colors.PURPLE + '[2000-2035]' + Colors.YELLOW + ' for right).' + Colors._CLEAR) fyborg.fyborg( outputs['fibers_mapped_length_filtered'], outputs['fibers_mapped_length_filtered_cortex_only'], [fyborg.FyFilterCortexAction(scalars.index('segmentation'))]) header = io.loadTrkHeaderOnly( outputs['fibers_mapped_length_filtered_cortex_only']) new_count = header['n_count'] c.info(Colors.YELLOW + ' Number of tracks after ' + Colors.PURPLE + 'cortex filtering' + Colors.YELLOW + ': ' + str(new_count) + Colors.YELLOW + Colors._CLEAR) c.info( Colors.YELLOW + ' Copied filtered tracks from ' + Colors.PURPLE + os.path.split( outputs['fibers_mapped_length_filtered_cortex_only'])[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split(outputs['fibers_final'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) shutil.copyfile( outputs['fibers_mapped_length_filtered_cortex_only'], outputs['fibers_final']) else: c.info(Colors.YELLOW + ' Info: ' + Colors.PURPLE + 'Cortical _and_ sub-cortical structures ' + Colors.YELLOW + 'will be included..' + Colors._CLEAR) c.info(Colors.YELLOW + ' Copied filtered tracks from ' + Colors.PURPLE + os.path.split(outputs['fibers_mapped_length_filtered'])[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split(outputs['fibers_final'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) shutil.copyfile(outputs['fibers_mapped_length_filtered'], outputs['fibers_final'])
def mapping(self, inputs, outputs, radius): ''' Map all detected scalar volumes to each fiber. ''' # check if we have all required input data # we need at least: # - outputs['fibers'] == Track file in T1 space # - outputs['segmentation'] == Label Map if not os.path.exists(outputs['fibers']): c.error(Colors.RED + 'Could not find ' + Colors.YELLOW + outputs['fibers'] + Colors.RED + ' but we really need it to start with stage 3!!' + Colors._CLEAR) sys.exit(2) if not os.path.exists(outputs['segmentation']): c.error(Colors.RED + 'Could not find ' + Colors.YELLOW + outputs['segmentation'] + Colors.RED + ' but we really need it to start with stage 3!!' + Colors._CLEAR) sys.exit(2) actions = [] for i in inputs: if i == 'fibers' or i == 'segmentation' or i == 'T1' or i == 'b0' or i == 'T1toB0matrix' or i == 'b0_resampled': # we do not map these continue if not os.path.exists(outputs[i]): # we can't map this since we didn't find the file continue # for normal scalars: append it to the actions actions.append(fyborg.FyMapAction(i, outputs[i])) c.info(Colors.YELLOW + ' Configuring mapping of ' + Colors.PURPLE + os.path.split(outputs[i])[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split(outputs['fibers'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) # now the segmentation with the lookaround radius actions.append( fyborg.FyRadiusMapAction('segmentation', outputs['segmentation'], radius)) c.info(Colors.YELLOW + ' Configuring mapping of ' + Colors.PURPLE + os.path.split(outputs['segmentation'])[1] + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split(outputs['fibers'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) # and also the fiber length actions.append(fyborg.FyLengthAction()) c.info(Colors.YELLOW + ' Configuring mapping of ' + Colors.PURPLE + 'fiber length' + Colors.YELLOW + ' to ' + Colors.PURPLE + os.path.split(outputs['fibers'])[1] + Colors.YELLOW + '!' + Colors._CLEAR) # run, forest, run!! c.info(Colors.YELLOW + ' Performing configured mapping for ' + Colors.PURPLE + os.path.split(outputs['fibers'])[1] + Colors.YELLOW + ' and storing as ' + Colors.PURPLE + os.path.split(outputs['fibers_mapped'])[1] + Colors.YELLOW + ' (~ 30 minutes)!' + Colors._CLEAR) if self.__debug: fyborg.fyborg(outputs['fibers'], outputs['fibers_mapped'], actions, 'debug') else: fyborg.fyborg(outputs['fibers'], outputs['fibers_mapped'], actions)
save_image(img, volFile) # trk file fibers = [] # 2,5,6 # 3,5,7 # 2,6,7 # 8,7,3 # 9,5,4 points = np.array([[2, 5, 6], [3, 5, 7], [2, 6, 7], [8, 7, 3], [9, 5, 4]], dtype=np.float32) fibers.append((points, None, None)) io.saveTrk(trkFile, fibers, None, None, True) # with fyborg fyborg.fyborg(trkFile, mappedTrkFile, [fyborg.FyMapAction('test', volFile)]) # now validate s = io.loadTrk(mappedTrkFile) tracks = s[0] origHeader = s[1] scalars = tracks[0][1] print scalars[0], '==', testArr[2][5][6] print scalars[1], '==', testArr[3][5][7] print scalars[2], '==', testArr[2][6][7] print scalars[3], '==', testArr[8][7][3] print scalars[4], '==', testArr[9][5][4]
# trk file fibers = [] # 2,5,6 # 3,5,7 # 2,6,7 # 8,7,3 # 9,5,4 points = np.array( [[2, 5, 6], [3, 5, 7], [2, 6, 7], [8, 7, 3], [9, 5, 4]], dtype=np.float32 ) fibers.append( ( points, None, None ) ) io.saveTrk( trkFile, fibers, None, None, True ) # with fyborg fyborg.fyborg( trkFile, mappedTrkFile, [fyborg.FyMapAction( 'test', volFile )] ) # now validate s = io.loadTrk( mappedTrkFile ) tracks = s[0] origHeader = s[1] scalars = tracks[0][1] print scalars[0], '==', testArr[2][5][6] print scalars[1], '==', testArr[3][5][7] print scalars[2], '==', testArr[2][6][7] print scalars[3], '==', testArr[8][7][3] print scalars[4], '==', testArr[9][5][4]