@@ -0,0 +1,2446 @@
+#!/usr/bin/env python%0A# -*- coding: utf-8 -*-%0A%0A%0Afrom __future__ import print_function%0A%0Aimport glob%0Aimport os%0Aimport re%0A%0Aimport argparse%0Aimport h5py%0Aimport numpy as np%0Aimport pytoml as toml%0A%0Afrom diluvian.util import get_nonzero_aabb%0A%0A%0Adef create_dataset_conf_from_files(path, file_pattern, name_regex, name_format, mask_bounds=True):%0A    pathspec = path + file_pattern%0A    name_regex = re.compile(name_regex)%0A%0A    datasets = %5B%5D%0A%0A    for pathname in glob.iglob(pathspec):%0A        filename = os.path.basename(pathname)%0A        name = name_format.format(*name_regex.match(filename).groups())%0A        ds = %7B%0A                'name': name,%0A                'hdf5_file': pathname,%0A                'image_dataset': 'volumes/raw',%0A                'label_dataset': 'volumes/labels/neuron_ids',%0A                'mask_dataset': 'volumes/labels/mask',%0A                'resolution': %5B40, 4, 4%5D,%0A        %7D%0A%0A        if mask_bounds:%0A            print('Finding mask bounds for %7B%7D'.format(filename))%0A            f = h5py.File(pathname, 'r')%0A            d = f%5Bds%5B'mask_dataset'%5D%5D%0A            mask_data = d%5B:%5D%0A            mask_min, mask_max = get_nonzero_aabb(mask_data)%0A%0A            ds%5B'mask_bounds'%5D = %5Bmask_min, mask_max%5D%0A            f.close()%0A%0A        datasets.append(ds)%0A%0A    return %7B'dataset': datasets%7D%0A%0A%0Aif __name__ == '__main__':%0A    parser = argparse.ArgumentParser(description='Create a dataset TOML from a directory of HDF5 files.')%0A%0A    parser.add_argument(%0A        '--file-pattern', dest='file_pattern', default='sample_%5BABC%5D*hdf',%0A        help='Glob for HDF5 volume filenames.')%0A    parser.add_argument(%0A        '--name-regex', dest='name_regex', default=r'sample_(%5BABC%5D)(.*).hdf',%0A        help='Regex for extracting volume name from filenames.')%0A    parser.add_argument(%0A        '--name-format', dest='name_format', default='Sample %7B%7D (%7B%7D)',%0A        help='Format string for creating volume names from name regex matches.')%0A    parser.add_argument(%0A        'path', default=None,%0A        help='Path to the HDF5 volume files.')%0A    parser.add_argument(%0A        'dataset_file', default=None,%0A        help='Name for the TOML dataset file that will be created.')%0A%0A    args = parser.parse_args()%0A%0A    conf = create_dataset_conf_from_files(args.path, args.file_pattern, args.name_regex, args.name_format)%0A    print('Found %7B%7D datasets.'.format(len(conf%5B'dataset'%5D)))%0A%0A    with open(args.dataset_file, 'wb') as tomlfile:%0A        tomlfile.write(toml.dumps(conf))%0A

Add old script for creating dataset TOML from HDF5