| 
 | 1 | +from pathlib import Path  | 
 | 2 | + | 
 | 3 | +import imageio.v3 as imageio  | 
 | 4 | +import napari  | 
 | 5 | +import pandas as pd  | 
 | 6 | + | 
 | 7 | +from ..file_utils import read_mrc  | 
 | 8 | + | 
 | 9 | + | 
 | 10 | +def _create_pools(vesicles, table):  | 
 | 11 | +    label_ids, pool_colors = table.label.values, table.color.values  | 
 | 12 | + | 
 | 13 | +    pools = vesicles  | 
 | 14 | +    colormap = {label_id: color for label_id, color in zip(label_ids, pool_colors)}  | 
 | 15 | +    colormap[None] = [0, 0, 0, 0]  | 
 | 16 | + | 
 | 17 | +    return pools, colormap  | 
 | 18 | + | 
 | 19 | + | 
 | 20 | +def _visualize_vesicle_pools(input_path, vesicle_path, table_path, segmentation_paths):  | 
 | 21 | +    # Load the tomogram data, including scale information.  | 
 | 22 | +    data, voxel_size = read_mrc(input_path)  | 
 | 23 | +    axes = "zyx" if data.ndim == 3 else "yx"  | 
 | 24 | +    scale = tuple(float(voxel_size[ax]) for ax in axes)  | 
 | 25 | +    print("Loading data with scale", scale, "nanometer")  | 
 | 26 | + | 
 | 27 | +    # Load the vesicle layer.  | 
 | 28 | +    vesicles = imageio.imread(vesicle_path)  | 
 | 29 | + | 
 | 30 | +    # Load the table with the pool assignments.  | 
 | 31 | +    # Create and add the pool layer.  | 
 | 32 | +    table = pd.read_excel(table_path)  | 
 | 33 | +    pools, colormap = _create_pools(vesicles, table)  | 
 | 34 | + | 
 | 35 | +    viewer = napari.Viewer()  | 
 | 36 | +    viewer.add_image(data, scale=scale)  | 
 | 37 | +    viewer.add_labels(vesicles, scale=scale)  | 
 | 38 | +    viewer.add_labels(pools, scale=scale, name="pools", colormap=colormap)  | 
 | 39 | + | 
 | 40 | +    # Add the additional segmentations.  | 
 | 41 | +    if segmentation_paths is not None:  | 
 | 42 | +        for seg_path in segmentation_paths:  | 
 | 43 | +            name = Path(seg_path).stem  | 
 | 44 | +            seg = imageio.imread(seg_path)  | 
 | 45 | +            viewer.add_labels(seg, name=name, scale=scale)  | 
 | 46 | + | 
 | 47 | +    # FIXME something is wrong here.  | 
 | 48 | +    # Add the scale bar.  | 
 | 49 | +    # @magicgui(call_button="Add Scale Bar")  | 
 | 50 | +    # def add_scale_bar(v: napari.Viewer):  | 
 | 51 | +    #     v.scale_bar.visible = True  | 
 | 52 | +    #     v.scale_bar.unit = "nm"  | 
 | 53 | +    # viewer.window.add_dock_widget(add_scale_bar)  | 
 | 54 | + | 
 | 55 | +    napari.run()  | 
0 commit comments