def reader_function_path_list(path): """Take a path or list of paths and return a list of LayerData tuples. Readers are expected to return data as a list of tuples, where each tuple is (data, [add_kwargs, [layer_type]]), "add_kwargs" and "layer_type" are both optional. Parameters ---------- path : str or list of str Path to file, or list of paths. Returns ------- layer_data : list of tuples A list of LayerData tuples where each tuple in the list contains (data, metadata, layer_type), where data is a numpy array, metadata is a dict of keyword arguments for the corresponding viewer.add_* method in napari, and layer_type is a lower-case string naming the type of layer. Both "meta", and "layer_type" are optional. napari will default to layer_type=="image" if not provided """ if not isinstance(path, list): path_obj = Path(path) if path_obj.is_dir(): files = list(filter(lambda x: x.is_file(), path_obj.iterdir())) image_io = itk.ImageIOFactory.CreateImageIO( str(files[0]), itk.CommonEnums.IOFileMode_ReadMode) if isinstance(image_io, itk.GDCMImageIO): # DICOMs are handled specially -- we identify a single series from # all the files in the directory and ensure that they are sorted # spatially # itk.imread assumes a DICOM series if a directory is passed image = itk.imread(path) else: file_strings = [str(f) for f in files] image = itk.imread(file_strings) else: # handle both a string and a list of strings image = itk.imread(path) else: # handle both a string and a list of strings image = itk.imread(path) image_layer = image_layer_from_image(image) components = image.GetNumberOfComponentsPerPixel() if components == 1: channel_axis = None else: channel_axis = image_layer.data.ndim - 1 # optional kwargs for the corresponding viewer.add_* method add_kwargs = { 'channel_axis': channel_axis, 'rgb': image_layer.rgb, 'metadata': image_layer.metadata, 'scale': image_layer.scale, 'translate': image_layer.translate, } layer_type = "image" # optional, default is "image" return [(image_layer.data, add_kwargs, layer_type)]
def test_image_layer_from_image_translate(): data = np.random.randint(256, size=(10, 10), dtype=np.uint8) image = itk.image_view_from_array(data) origin = [1.1, 2.2] image.SetOrigin(origin) image_layer = itk_napari_conversion.image_layer_from_image(image) assert np.array_equal(data, image_layer.data) assert np.allclose(np.array(origin)[::-1], image_layer.translate)
def test_image_layer_from_image_scale(): data = np.random.randint(256, size=(10, 10), dtype=np.uint8) image = itk.image_view_from_array(data) spacing = [1.1, 2.2] image.SetSpacing(spacing) image_layer = itk_napari_conversion.image_layer_from_image(image) assert np.array_equal(data, image_layer.data) assert np.allclose(np.array(spacing)[::-1], image_layer.scale)
def test_image_layer_from_image_metadata(): data = np.random.randint(256, size=(10, 10), dtype=np.uint8) image = itk.image_view_from_array(data) image["wookies"] = 7 image["units"] = "mm" image_layer = itk_napari_conversion.image_layer_from_image(image) assert np.array_equal(data, image_layer.data) assert image_layer.metadata["wookies"] == 7 assert image_layer.metadata["units"] == "mm"
def test_image_layer_from_image(): data = np.random.randint(256, size=(10, 10), dtype=np.uint8) image = itk.image_view_from_array(data) image_layer = itk_napari_conversion.image_layer_from_image(image) assert np.array_equal(data, image_layer.data)
def test_image_layer_from_image_rgb(): data = np.random.randint(256, size=(10, 10, 3), dtype=np.uint8) image = itk.image_view_from_array(data, itk.RGBPixel[itk.UC]) image_layer = itk_napari_conversion.image_layer_from_image(image) assert np.array_equal(data, image_layer.data) assert image_layer.rgb is True