def split_multiatlas(atlas, output_prefix, pool=None): """ Take a multi-atlas with many ROIs and split it into binary masks for each ROI. output_prefix can be a container with integer index keys as in the atlas image and values the full output path. """ N = int( os.popen('MeasureMinMaxMean 3 %s' % atlas).read().split('[', 1)[-1].split( ']', 1)[0]) if pool is None: pool = BetterPool() cmds = [] for idx in xrange(N): if isinstance(output_prefix, str): output = '%s-%s.nii.gz' % (output_prefix, idx) else: try: output = output_prefix[idx] except (IndexError, KeyError): print 'Skipping %d' % idx continue cmds.append('ThresholdImage 3 %s %s %d %d' % (atlas, output, idx, idx)) pool.map(command, cmds)
def __init__(self, labels, pool=None): self.volume = {} self.overlap = {} if pool is None: pool = BetterPool() # TODO refactor using knowledge that it's a symmetric matrix of values # Determine pairwise overlap for label1 in labels: overlap_params = [] for label2 in labels: overlap_params.append((label1, label2)) datum = pool.map(overlap_c3d, overlap_params) vol1, vol2, over = zip(*datum) # This overwrites same value a lot, bit redundant self.volume = dict(zip(labels, vol2)) self.overlap[label1] = dict(zip(labels, over))
# Approximately Thomas's method, can debate about order of some elements, like VLP and MTT/VA # ['2-AV', '4-VA', '5-VLa', '6-VLP', '7-VPL', '8-Pul', '9-LGN', '10-MGN', '11-CM', '12-MD-Pf', '13-Hb', '14-MTT'] # methods = { # 'Thomas': ['8-Pul', '11-CM', '12-MD-Pf', '4-VA', '14-MTT', '2-AV', '13-Hb', '6-VLP', '5-VLa', '7-VPL', '9-LGN', '10-MGN'], # 'Fixed_Metric': ['2-AV', '6-VLP', '4-VA', '5-VLa', '8-Pul', '7-VPL', '9-LGN', '10-MGN', '12-MD-Pf', '11-CM', '13-Hb', '14-MTT'], # } find_num = re.compile('[0-9]+') if __name__ == '__main__': if len(sys.argv) < 3: print '%s <method: Numerical, Metric> <output_atlas> labels ...' % sys.argv[ 0] sys.exit(0) pool = BetterPool() method = sys.argv[1] out = sys.argv[2] labels = sys.argv[3:] if method == 'Numerical': print 'Using the input order' method_labels = labels elif method == 'Metric': # Use overlap metric based comparison print 'Calculating overlap metric weighted by volume' compare = CompareOverlap(labels, pool) method_labels = sorted(labels, cmp=compare) label_numbers = dict() for i, label in enumerate(labels): label_num = find_num.search(os.path.basename(label)) if label_num is None:
'-p', '--processes', nargs='?', default=None, const=None, type=int, help= 'number of parallel processes to use. If unspecified, automatically set to number of CPUs.' ) # parser.add_argument('-v', '--verbose', action='store_true', help='verbose mode') # parser.add_argument('-d', '--debug', action='store_true', help='debug mode, interactive prompts') # parser.add_argument('-R', '--right', action='store_true', help='segment right thalamus') if __name__ == '__main__': args = parser.parse_args() pool = BetterPool(args.processes) method = args.method out = args.output_path labels = args.labels if method == 'Numerical': print 'Using the input order' method_labels = labels elif method == 'Metric': # Use overlap metric based comparison print 'Calculating overlap metric weighted by volume' compare = CompareOverlap(labels, pool) method_labels = sorted(labels, cmp=compare) label_numbers = dict() for i, label in enumerate(labels): label_num = find_num.search(os.path.basename(label)) if label_num is None: