File: complex_image.py

package info (click to toggle)
python-vispy 0.15.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,868 kB
  • sloc: python: 59,799; javascript: 6,800; makefile: 69; sh: 6
file content (80 lines) | stat: -rw-r--r-- 2,260 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
# vispy: gallery 30
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------
"""
Complex image data
==================

Simple use of SceneCanvas to display an image consisting of complex numbers.

The left and right arrow keys can be used to cycle the view between the real,
imaginary, phase, or magnitude of the data.

"""
import sys

import numpy as np
from vispy import app, scene


def complex_ramp(size=512, phase_range=(-np.pi, np.pi), mag_range=(0, 10)):
    """Returns a complex array where X ramps phase and Y ramps magnitude."""
    p0, p1 = phase_range
    phase_ramp = np.linspace(p0, p1 - 1 / size, size)
    m0, m1 = mag_range
    mag_ramp = np.linspace(m1, m0 + 1 / size, size)
    phase_ramp, mag_ramp = np.meshgrid(phase_ramp, mag_ramp)
    return (mag_ramp * np.exp(1j * phase_ramp)).astype(np.complex64)


canvas = scene.SceneCanvas(keys="interactive",
                           title="Complex number view: phase")
canvas.size = 512, 512
canvas.show()

# Set up a viewbox to display the image with interactive pan/zoom
view = canvas.central_widget.add_view()

# Create the image
img_data = complex_ramp()

# View it with "complex_mode=imaginary"
image = scene.visuals.ComplexImage(img_data, parent=view.scene, complex_mode="phase")

# Set 2D camera (the camera will scale to the contents in the scene)
view.camera = scene.PanZoomCamera(aspect=1)
view.camera.set_range()
view.camera.zoom(1)


complex_modes = [
    "real",
    "imaginary",
    "magnitude",
    "phase",
]
mode_index = 3


@canvas.connect
def on_key_press(event):
    global mode_index
    if event.key not in ['Left', 'Right']:
        return

    if event.key == 'Right':
        step = 1
    else:
        step = -1
    mode_index = (mode_index + step) % len(complex_modes)
    complex_mode = complex_modes[mode_index]
    image.complex_mode = complex_mode
    canvas.title = f'Complex number view: {complex_mode}'
    canvas.update()


if __name__ == "__main__" and sys.flags.interactive == 0:
    app.run()