def main(n_instances, curr_instance):
    images_with_no_labels = []
    skip_count = 0
    images = recursive_glob(img_dir, in_image_search)

    N = len(images)
    Nstep = np.ceil(float(N) / n_instances)
    for_start = int((curr_instance - 1) * Nstep)
    for_end = int(for_start + Nstep)
    if for_end > N:
        for_end = N
    print('for_start:for_end is {}:{}'.format(for_start, for_end))

    for idx in range(for_start, for_end):
        image = images[idx]
        if ('.d' in image) or ('.H' in image):
            print('SKIP ', image)
            skip_count += 1
            continue
        basename = os.path.splitext(
            os.path.basename(image.replace(in_image_search, '')))[0]
        basename = basename.replace('PANCREAS_', '')
        #label = recursive_glob2(label_dir,basename,in_label_search)
        label = [os.path.join(label_dir, basename + in_label_search)]
        if len(label) != 1:
            raise ValueError('No unique label found for {} ({})'.format(
                image, len(label)))
            print('[WARNING] No unique label found for {} ({})'.format(
                image, len(label)))
            images_with_no_labels.append(image)
            label = None
        else:
            label = label[0]
        print('image: {}'.format(image))
        print('label: {}'.format(label))
        prepare(image, label, output_root)
        ####prepare2(image,label,output_root)

    print('{} images_with_no_labels:'.format(len(images_with_no_labels)))
    for i in images_with_no_labels:
        print(i)
    print('Skipped {} of {} images'.format(skip_count, N))
## Input output
img_dir = '/mnt/dataset/m-organs/averaged-training-images'
img_search = '.nii.gz'
label_dir = '/mnt/dataset/m-organs/averaged-training-labels'
label_search = '.nii.gz'
output_root = '/root/output'

######################## FUNCTIONS ###############################
import os
import sys
from deploy_cascade import deploy_cascade
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from data import recursive_glob, recursive_glob2

images_with_no_labels = []
images = recursive_glob(img_dir, img_search)

#DEVICE=None # CPU only mode
DEVICE = 0
for image in images:
    basename = os.path.splitext(os.path.basename(image))[0]
    basename = basename.replace('series', '')
    if label_dir is not None:
        label = recursive_glob2(label_dir, basename, '.raw')
    else:
        label = []
    if len(label) != 1:
        print('[WARNING] No unique label found for {}'.format(image))
        images_with_no_labels.append(image)
        label = None
    else:
def main(n_instances, curr_instance):
    label_files = recursive_glob(in_label_dir, in_label_search)

    N = len(label_files)
    if N > 0:
        if not os.path.isdir(out_dir):
            os.makedirs(out_dir)
        #flist = open(out_list, 'w', 0)
        flist = open(out_list, 'a', 0)  #append to existing
    else:
        raise ValueError('No files found!')

    Nstep = np.ceil(float(N) / n_instances)
    for_start = int((curr_instance - 1) * Nstep)
    for_end = int(for_start + Nstep)
    if for_end > N:
        for_end = N
    print('for_start:for_end is {}:{}'.format(for_start, for_end))

    converted = 0
    already_computed = 0
    img_pair_not_found = 0
    skip_invalid = 0
    failed_count = 0
    for i, label_file in enumerate(label_files[for_start:for_end]):
        print('{} of {}: {}'.format(i + 1, N, label_file))
        basename = os.path.split(label_file.replace(in_label_search, ''))[1]
        basename = basename[0:basename.find('_')].lower()
        img_file = recursive_glob2(in_image_dir, basename, in_image_search)[0]
        curr_dir = img_file.replace(in_image_dir,
                                    out_dir).replace(in_image_search, '')
        FOUND = False
        mask_file = None
        if LOAD_MASK:
            mask_file = recursive_glob2(in_mask_dir, in_mask_search, basename)
            if len(mask_file) != 1:
                FOUND = False
                img_pair_not_found += 1
                print('  WARNING: no mask found for {}\n in {} with {} search'.
                      format(label_file, in_mask_dir, basename))
                mask_file = None
                continue

        curr_dir = curr_dir.replace('.nii.gz', '')
        curr_dir = curr_dir.replace('.nii', '')
        out_file = curr_dir + '/' + os.path.split(curr_dir)[1] + out_suffix
        if not os.path.isfile(out_file):
            print(100 * '=')
            try:
                if LOAD_MASK:
                    convert_image_and_label_to_h5(img_file, label_file,
                                                  out_file, mask_file[0])
                else:
                    convert_image_and_label_to_h5(img_file, label_file,
                                                  out_file)
                    converted += 1
            except Exception as inst:
                print type(inst)  # the exception instance
                print inst.args  # arguments stored in .args
                print inst  # __str__ allows args to be printed directly
                failed_count += 1
                sys.exit()
        else:
            already_computed += 1
            print('  Already computed ({}): {}'.format(already_computed,
                                                       out_file))
            flist.write('{}\n'.format(out_file))
        FOUND = True
        if not FOUND:
            img_pair_not_found += 1
            print('  WARNING: no image found for {}'.format(label_file))

    print('Converted {} of {} file pairs.'.format(converted, N))
    print('Already converted {} of {} file pairs.'.format(already_computed, N))
    print('No suitable image found {} of {} file pairs.'.format(
        img_pair_not_found, N))
    print('Skip/invalid {} of {} file pairs.'.format(skip_invalid, N))
    print('Convert failed {} of {} file pairs.'.format(failed_count, N))
    flist.close()