Skip to content

Viewer¤

Interactive Volume Rendering in an iPython environment.

vape4d.viewer ¤

VAPE_URL module-attribute ¤
VAPE_URL = get(
    "VAPE_URL", "https://keksboter.github.io/vape4d"
)
ViewerSettings dataclass ¤
Source code in vape4d/viewer.py
48
49
50
51
52
53
54
55
56
57
58
@dataclass(unsafe_hash=True)
class ViewerSettings:
    width: int
    height: int
    background_color: tuple
    show_colormap_editor: bool
    show_volume_info: bool
    vmin: Optional[float]
    vmax: Optional[float]
    distance_scale: float
    duration: Optional[float] = None
width instance-attribute ¤
width: int
height instance-attribute ¤
height: int
background_color instance-attribute ¤
background_color: tuple
show_colormap_editor instance-attribute ¤
show_colormap_editor: bool
show_volume_info instance-attribute ¤
show_volume_info: bool
vmin instance-attribute ¤
vmin: Optional[float]
vmax instance-attribute ¤
vmax: Optional[float]
distance_scale instance-attribute ¤
distance_scale: float
duration class-attribute instance-attribute ¤
duration: Optional[float] = None
__init__ ¤
__init__(
    width: int,
    height: int,
    background_color: tuple,
    show_colormap_editor: bool,
    show_volume_info: bool,
    vmin: Optional[float],
    vmax: Optional[float],
    distance_scale: float,
    duration: Optional[float] = None,
) -> None
VolumeViewer ¤

Bases: DisplayObject

Source code in vape4d/viewer.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class VolumeViewer(DisplayObject):
    def __init__(self, data: np.ndarray, colormap: Colormap, settings: ViewerSettings):
        super(VolumeViewer, self).__init__(
            data={"volume": data, "cmap": colormap, "settings": settings}
        )

    def _repr_html_(self):

        data = self.data["volume"]
        colormap = self.data["cmap"]
        settings = self.data["settings"]
        buffer = io.BytesIO()
        np.save(buffer, data.astype(np.float32))
        data_code = base64.b64encode(buffer.getvalue())

        buffer2 = io.BytesIO()
        colormap_data = colormap(np.linspace(0, 1, 256)).astype(np.float32)
        np.save(buffer2, colormap_data)
        cmap_code = base64.b64encode(buffer2.getvalue())

        canvas_id = f"vape4d_canvas_{str(random.randint(0,2**32))}"
        html_code = _TEMPLATE_IFRAME.format(
            canvas_id=canvas_id,
            data_code=data_code.decode("utf-8"),
            cmap_code=cmap_code.decode("utf-8"),
            canvas_width=settings.width,
            canvas_height=settings.height,
            settings_json=json.dumps(dataclasses.asdict(settings)),
            viewer_url=VAPE_URL,
        )
        return html_code

    def __html__(self):
        return self._repr_html_()
__init__ ¤
__init__(
    data: ndarray,
    colormap: Colormap,
    settings: ViewerSettings,
)
Source code in vape4d/viewer.py
109
110
111
112
def __init__(self, data: np.ndarray, colormap: Colormap, settings: ViewerSettings):
    super(VolumeViewer, self).__init__(
        data={"volume": data, "cmap": colormap, "settings": settings}
    )
__html__ ¤
__html__()
Source code in vape4d/viewer.py
140
141
def __html__(self):
    return self._repr_html_()
viewer ¤
viewer(
    data: ndarray,
    colormap: Optional[Colormap] = None,
    width: int = 800,
    height: int = 600,
    background_color=(0.0, 0.0, 0.0, 1.0),
    show_colormap_editor=False,
    show_volume_info=False,
    vmin=None,
    vmax=None,
    distance_scale=1.0,
    duration=None,
)

summary

Parameters:

Name Type Description Default
data ndarray

volume data of shape [T,C, D, H, W]

required
colormap Optional[Colormap]

description. Defaults to matplotlib default colormap.

None
width int

viewer width. Defaults to 800.

800
height int

viewer height. Defaults to 600.

600
background_color tuple

background color in renderer. Defaults to black.

(0.0, 0.0, 0.0, 1.0)
show_colormap_editor bool

show the transfer function editor. Defaults to False.

False
show_volume_info bool

show the volume info window. Defaults to False.

False
vmin float

all values in data are clamped to this value. Defaults to minimum value in data.

None
vmax float

all values in data are clamped to this value. Defaults to maximum value in data.

None
distance_scale float

distance scale used for rendering. Defaults to 1.0.

1.0
duration _type_

duration of one animation cycle. Defaults to 5 seconds.

None
Source code in vape4d/viewer.py
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def viewer(
    data: np.ndarray,
    colormap: Optional[Colormap] = None,
    width: int = 800,
    height: int = 600,
    background_color=(0.0, 0.0, 0.0, 1.0),
    show_colormap_editor=False,
    show_volume_info=False,
    vmin=None,
    vmax=None,
    distance_scale=1.0,
    duration=None,
):
    """_summary_

    Args:
        data (np.ndarray): volume data of shape [T,C, D, H, W]
        colormap (Optional[Colormap], optional): _description_. Defaults to matplotlib default colormap.
        width (int, optional): viewer width. Defaults to 800.
        height (int, optional): viewer height. Defaults to 600.
        background_color (tuple, optional): background color in renderer. Defaults to black.
        show_colormap_editor (bool, optional): show the transfer function editor. Defaults to False.
        show_volume_info (bool, optional): show the volume info window. Defaults to False.
        vmin (float, optional): all values in data are clamped to this value. Defaults to minimum value in data.
        vmax (float, optional):  all values in data are clamped to this value. Defaults to maximum value in data.
        distance_scale (float, optional): distance scale used for rendering. Defaults to 1.0.
        duration (_type_, optional): duration of one animation cycle. Defaults to 5 seconds.
    """
    if colormap is None:
        colormap = plt.get_cmap()
    return VolumeViewer(
        data,
        colormap,
        ViewerSettings(
            width,
            height,
            background_color,
            show_colormap_editor,
            show_volume_info,
            vmin,
            vmax,
            distance_scale,
            duration,
        ),
    )

vape4d.VolumeViewer ¤

Bases: DisplayObject

Source code in vape4d/viewer.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
class VolumeViewer(DisplayObject):
    def __init__(self, data: np.ndarray, colormap: Colormap, settings: ViewerSettings):
        super(VolumeViewer, self).__init__(
            data={"volume": data, "cmap": colormap, "settings": settings}
        )

    def _repr_html_(self):

        data = self.data["volume"]
        colormap = self.data["cmap"]
        settings = self.data["settings"]
        buffer = io.BytesIO()
        np.save(buffer, data.astype(np.float32))
        data_code = base64.b64encode(buffer.getvalue())

        buffer2 = io.BytesIO()
        colormap_data = colormap(np.linspace(0, 1, 256)).astype(np.float32)
        np.save(buffer2, colormap_data)
        cmap_code = base64.b64encode(buffer2.getvalue())

        canvas_id = f"vape4d_canvas_{str(random.randint(0,2**32))}"
        html_code = _TEMPLATE_IFRAME.format(
            canvas_id=canvas_id,
            data_code=data_code.decode("utf-8"),
            cmap_code=cmap_code.decode("utf-8"),
            canvas_width=settings.width,
            canvas_height=settings.height,
            settings_json=json.dumps(dataclasses.asdict(settings)),
            viewer_url=VAPE_URL,
        )
        return html_code

    def __html__(self):
        return self._repr_html_()
__init__ ¤
__init__(
    data: ndarray,
    colormap: Colormap,
    settings: ViewerSettings,
)
Source code in vape4d/viewer.py
109
110
111
112
def __init__(self, data: np.ndarray, colormap: Colormap, settings: ViewerSettings):
    super(VolumeViewer, self).__init__(
        data={"volume": data, "cmap": colormap, "settings": settings}
    )
__html__ ¤
__html__()
Source code in vape4d/viewer.py
140
141
def __html__(self):
    return self._repr_html_()

vape4d.ViewerSettings dataclass ¤

Source code in vape4d/viewer.py
48
49
50
51
52
53
54
55
56
57
58
@dataclass(unsafe_hash=True)
class ViewerSettings:
    width: int
    height: int
    background_color: tuple
    show_colormap_editor: bool
    show_volume_info: bool
    vmin: Optional[float]
    vmax: Optional[float]
    distance_scale: float
    duration: Optional[float] = None
width instance-attribute ¤
width: int
height instance-attribute ¤
height: int
background_color instance-attribute ¤
background_color: tuple
show_colormap_editor instance-attribute ¤
show_colormap_editor: bool
show_volume_info instance-attribute ¤
show_volume_info: bool
vmin instance-attribute ¤
vmin: Optional[float]
vmax instance-attribute ¤
vmax: Optional[float]
distance_scale instance-attribute ¤
distance_scale: float
duration class-attribute instance-attribute ¤
duration: Optional[float] = None
__init__ ¤
__init__(
    width: int,
    height: int,
    background_color: tuple,
    show_colormap_editor: bool,
    show_volume_info: bool,
    vmin: Optional[float],
    vmax: Optional[float],
    distance_scale: float,
    duration: Optional[float] = None,
) -> None