Beispiel #1
0
def _make_test_data(shape, input_dtype):
    data = np.random.random_sample(shape)
    if data.ndim == 3 and data.shape[-1] == 4:
        # RGBA - make alpha fully opaque
        data[..., -1] = 1.0
    max_val = max_for_dtype(input_dtype)
    if max_val != 1:
        data *= max_val
    data = data.astype(input_dtype)
    return data
Beispiel #2
0
def _make_test_data(shape, input_dtype):
    one_third = shape[0] // 3
    two_third = 2 * one_third
    data = np.zeros(shape, dtype=np.float64)
    # 0.00 | 1.00 | 0.00
    # 0.50 | 0.00 | 0.25
    # 0.00 | 0.00 | 0.00
    data[:, :one_third, one_third:two_third] = 1.0
    data[:, one_third:two_third, :one_third] = 0.5
    data[:, one_third:two_third, two_third:] = 0.25
    max_val = max_for_dtype(input_dtype)
    if max_val != 1:
        data *= max_val
    data = data.astype(input_dtype)
    return data
Beispiel #3
0
def test_volume_clims_and_gamma(texture_format, input_dtype, clim_on_init):
    """Test volume visual with clims and gamma on shader.

    Test is parameterized based on ``texture_format`` and should produce
    relatively the same results for each format.

    Currently just using np.ones since the angle of view made more complicated samples
    challenging, but this confirms gamma and clims works in the shader.
    The VolumeVisual defaults to the "grays" colormap so although we compare
    data using RGBA arrays, each R/G/B channel should be the same.

    """
    size = (40, 40)
    if texture_format == '__dtype__':
        texture_format = input_dtype
    np.random.seed(0)  # make tests the same every time
    data = _make_test_data(size[:1] * 3, input_dtype)
    clim = (0, 1)
    new_clim = (0.3, 0.8)
    max = max_for_dtype(input_dtype)
    if max != 1:
        clim = (clim[0] * max, clim[1] * max)
        new_clim = (new_clim[0] * max, new_clim[1] * max)

    kwargs = {}
    if clim_on_init:
        kwargs['clim'] = clim
    with TestingCanvas(size=size, bgcolor="k") as c:
        v = c.central_widget.add_view(border_width=0)
        volume = scene.visuals.Volume(data,
                                      interpolation='nearest',
                                      parent=v.scene,
                                      method='mip',
                                      texture_format=texture_format,
                                      **kwargs)
        v.camera = 'arcball'
        v.camera.fov = 0
        v.camera.scale_factor = 40.0
        v.camera.center = (19.5, 19.5, 19.5)

        rendered = c.render()
        _dtype = rendered.dtype
        shape_ratio = rendered.shape[0] // data.shape[0]
        rendered1 = downsample(rendered, shape_ratio,
                               axis=(0, 1)).astype(_dtype)
        predicted = data.max(axis=1)
        compare_render(predicted, rendered1)

        # adjust contrast limits
        volume.clim = new_clim
        rendered2 = downsample(c.render(), shape_ratio,
                               axis=(0, 1)).astype(_dtype)
        scaled_data = (np.clip(data, new_clim[0], new_clim[1]) -
                       new_clim[0]) / (new_clim[1] - new_clim[0])
        predicted = scaled_data.max(axis=1)
        compare_render(predicted, rendered2, previous_render=rendered)

        # adjust gamma
        volume.gamma = 2
        rendered3 = downsample(c.render(), shape_ratio,
                               axis=(0, 1)).astype(_dtype)
        predicted = (scaled_data**2).max(axis=1)
        compare_render(predicted, rendered3, previous_render=rendered2)
Beispiel #4
0
def _get_orig_and_new_clims(input_dtype):
    new_clim = (0.3, 0.8)
    max_val = max_for_dtype(input_dtype)
    if np.issubdtype(input_dtype, np.integer):
        new_clim = (int(new_clim[0] * max_val), int(new_clim[1] * max_val))
    return (0, max_val), new_clim