# Path: gala/imio.py
# def read_image_stack(fn, *args, **kwargs):
# def write_image_stack(npy_vol, fn, **kwargs):
# def pil_to_numpy(img):
# def read_multi_page_tif(fn, crop=[None]*6):
# def write_png_image_stack(npy_vol, fn, axis=-1, bitdepth=None):
# def extract_segments(seg, ids):
# def write_vtk(ar, fn, spacing=[1.0, 1.0, 1.0]):
# def read_vtk(fin):
# def read_h5_stack(fn, group='stack', crop=[None]*6, **kwargs):
# def compute_sp_to_body_map(sps, bodies):
# def write_mapped_segmentation(superpixel_map, sp_to_body_map, fn, 
#                               sp_group='stack', sp_to_body_group='transforms'):
# def read_mapped_segmentation(fn, 
#                              sp_group='stack', sp_to_body_group='transforms'):
# def apply_segmentation_map(superpixels, sp_to_body_map):
# def read_mapped_segmentation_raw(fn, 
#                              sp_group='stack', sp_to_body_group='transforms'):
# def write_h5_stack(npy_vol, fn, group='stack', compression=None, chunks=None,
#                    shuffle=None, attrs=None):
# def ucm_to_raveler(ucm, sp_threshold=0.0, body_threshold=0.1, **kwargs):
# def segs_to_raveler(sps, bodies, min_size=0, do_conn_comp=False, sps_out=None):
# def raveler_serial_section_map(nd_map, min_size=0, do_conn_comp=False, 
#                                                     globally_unique_ids=True):
# def serial_section_map(nd_map, min_size=0, do_conn_comp=False, 
#                                                     globally_unique_ids=True):
#         def label_fct(a):
#     def remove_small(a):
# def write_to_raveler(sps, sp_to_segment, segment_to_body, directory, gray=None,
#                     raveler_dir='/usr/local/raveler-hdf', nproc_contours=16,
#                     body_annot=None):
#             def call(arglist):
# def raveler_output_shortcut(svs, seg, gray, outdir, sps_out=None):
# def raveler_body_annotations(orphans, non_traversing=None):
# def write_json(annot, fn='annotations-body.json', directory=None):
# def raveler_rgba_to_int(im, ignore_alpha=True):
# def raveler_to_labeled_volume(rav_export_dir, get_glia=False, 
#                         use_watershed=False, probability_map=None, crop=None):
# def write_ilastik_project(images, labels, fn, label_names=None):
# def write_ilastik_batch_volume(im, fn):
# def read_prediction_from_ilastik_batch(fn, **kwargs):
# def read_cremi(fn, datasets=['/volumes/raw', '/volumes/labels/neuron_ids']):
# def write_cremi(data_dict, fn, resolution=(40., 4., 4.)):


from gala import imio
from skimage._shared._tempfile import temporary_file
import h5py
import numpy as np


def test_cremi_roundtrip():
    raw_image = np.random.randint(256, size=(5, 100, 100), dtype=np.uint8)
    labels = np.random.randint(4096, size=raw_image.shape, dtype=np.uint64)
    for ax in range(labels.ndim):
        labels.sort(axis=ax)  # try to get something vaguely contiguous. =P
    with temporary_file('.hdf') as fout:
        imio.write_cremi({'/volumes/raw': raw_image,
                          '/volumes/labels/neuron_ids': labels}, fout)

# Path: gala/imio.py
# def read_image_stack(fn, *args, **kwargs):
# def write_image_stack(npy_vol, fn, **kwargs):
# def pil_to_numpy(img):
# def read_multi_page_tif(fn, crop=[None]*6):
# def write_png_image_stack(npy_vol, fn, axis=-1, bitdepth=None):
# def extract_segments(seg, ids):
# def write_vtk(ar, fn, spacing=[1.0, 1.0, 1.0]):
# def read_vtk(fin):
# def read_h5_stack(fn, group='stack', crop=[None]*6, **kwargs):
# def compute_sp_to_body_map(sps, bodies):
# def write_mapped_segmentation(superpixel_map, sp_to_body_map, fn, 
#                               sp_group='stack', sp_to_body_group='transforms'):
# def read_mapped_segmentation(fn, 
#                              sp_group='stack', sp_to_body_group='transforms'):
# def apply_segmentation_map(superpixels, sp_to_body_map):
# def read_mapped_segmentation_raw(fn, 
#                              sp_group='stack', sp_to_body_group='transforms'):
# def write_h5_stack(npy_vol, fn, group='stack', compression=None, chunks=None,
#                    shuffle=None, attrs=None):
# def ucm_to_raveler(ucm, sp_threshold=0.0, body_threshold=0.1, **kwargs):
# def segs_to_raveler(sps, bodies, min_size=0, do_conn_comp=False, sps_out=None):
# def raveler_serial_section_map(nd_map, min_size=0, do_conn_comp=False, 
#                                                     globally_unique_ids=True):
# def serial_section_map(nd_map, min_size=0, do_conn_comp=False, 
#                                                     globally_unique_ids=True):
#         def label_fct(a):
#     def remove_small(a):
# def write_to_raveler(sps, sp_to_segment, segment_to_body, directory, gray=None,
#                     raveler_dir='/usr/local/raveler-hdf', nproc_contours=16,
#                     body_annot=None):
#             def call(arglist):
# def raveler_output_shortcut(svs, seg, gray, outdir, sps_out=None):
# def raveler_body_annotations(orphans, non_traversing=None):
# def write_json(annot, fn='annotations-body.json', directory=None):
# def raveler_rgba_to_int(im, ignore_alpha=True):
# def raveler_to_labeled_volume(rav_export_dir, get_glia=False, 
#                         use_watershed=False, probability_map=None, crop=None):
# def write_ilastik_project(images, labels, fn, label_names=None):
# def write_ilastik_batch_volume(im, fn):
# def read_prediction_from_ilastik_batch(fn, **kwargs):
# def read_cremi(fn, datasets=['/volumes/raw', '/volumes/labels/neuron_ids']):
# def write_cremi(data_dict, fn, resolution=(40., 4., 4.)):


# Path: tests/test_imio.py
from gala import imio
from skimage._shared._tempfile import temporary_file
import h5py
import numpy as np



def test_cremi_roundtrip():
    raw_image = np.random.randint(256, size=(5, 100, 100), dtype=np.uint8)
    labels = np.random.randint(4096, size=raw_image.shape, dtype=np.uint64)
    for ax in range(labels.ndim):
        labels.sort(axis=ax)  # try to get something vaguely contiguous. =P
    with temporary_file('.hdf') as fout:
        imio.write_cremi({'/volumes/raw': raw_image,
                          '/volumes/labels/neuron_ids': labels}, fout)

        raw_in, lab_in = imio.read_cremi(fout)