Skip to content

Commit 51e2833

Browse files
committed
Documentation and bug fixes for graphics.py
1 parent 049e509 commit 51e2833

File tree

2 files changed

+145
-6
lines changed

2 files changed

+145
-6
lines changed

arrayfire/graphics.py

+144-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
# http://arrayfire.com/licenses/BSD-3-Clause
88
########################################################
99

10+
"""
11+
graphics functions for arrayfire
12+
"""
13+
1014
from .library import *
1115
from .array import *
1216

@@ -20,11 +24,27 @@ def __init__(self, r, c, title, cmap):
2024
self.row = r
2125
self.col = c
2226
self.title = title if title is not None else ct.c_char_p()
23-
self.cmap = cmap
27+
self.cmap = cmap.value
28+
29+
class Window(object):
30+
"""
31+
Class to create the Window object.
32+
33+
Parameters
34+
----------
35+
36+
width: optional: int. default: 1280.
37+
- Specifies the width of the window in pixels.
38+
39+
height: optional: int. default: 720.
40+
- Specifies the height of the window in pixels.
2441
25-
class window(object):
42+
title: optional: str. default: "ArrayFire".
43+
- Specifies the title used for the window.
2644
27-
def __init__(self, width=None, height=None, title=None):
45+
"""
46+
47+
def __init__(self, width=1280, height=720, title="ArrayFire"):
2848
self._r = -1
2949
self._c = -1
3050
self._wnd = ct.c_longlong(0)
@@ -41,43 +61,160 @@ def __init__(self, width=None, height=None, title=None):
4161
ct.c_char_p(_title)))
4262

4363
def __del__(self):
64+
"""
65+
Destroys the window when going out of scope.
66+
"""
4467
safe_call(backend.get().af_destroy_window(self._wnd))
4568

4669
def set_pos(self, x, y):
70+
"""
71+
Set the position of window on the screen.
72+
73+
Parameters
74+
----------
75+
76+
x : int.
77+
Pixel offset from left.
78+
79+
y : int.
80+
Pixel offset from top
81+
82+
"""
4783
safe_call(backend.get().af_set_position(self._wnd, ct.c_int(x), ct.c_int(y)))
4884

4985
def set_title(self, title):
86+
"""
87+
Set the title of the window
88+
89+
Parameters
90+
----------
91+
92+
title : str.
93+
Title used for the current window.
94+
95+
"""
5096
safe_call(backend.get().af_set_title(self._wnd, title))
5197

5298
def set_colormap(self, cmap):
99+
"""
100+
Set the colormap for the window.
101+
102+
Parameters
103+
----------
104+
105+
cmap : af.COLORMAP.
106+
Set the colormap for the window.
107+
108+
"""
53109
self._cmap = cmap
54110

55111
def image(self, img, title=None):
112+
"""
113+
Display an arrayfire array as an image.
114+
115+
Paramters
116+
---------
117+
118+
img: af.Array.
119+
A 2 dimensional array for single channel image.
120+
A 3 dimensional array for 3 channel image.
121+
122+
title: str.
123+
Title used for the image.
124+
"""
56125
_cell = _Cell(self._r, self._c, title, self._cmap)
57126
safe_call(backend.get().af_draw_image(self._wnd, img.arr, ct.pointer(_cell)))
58127

59128
def plot(self, X, Y, title=None):
129+
"""
130+
Display a 2D Plot.
131+
132+
Paramters
133+
---------
134+
135+
X: af.Array.
136+
A 1 dimensional array containing X co-ordinates.
137+
138+
Y: af.Array.
139+
A 1 dimensional array containing Y co-ordinates.
140+
141+
title: str.
142+
Title used for the plot.
143+
"""
60144
_cell = _Cell(self._r, self._c, title, self._cmap)
61145
safe_call(backend.get().af_draw_plot(self._wnd, X.arr, Y.arr, ct.pointer(_cell)))
62146

63147
def hist(self, X, min_val, max_val, title=None):
148+
"""
149+
Display a histogram Plot.
150+
151+
Paramters
152+
---------
153+
154+
X: af.Array.
155+
A 1 dimensional array containing the histogram.
156+
157+
min_val: scalar.
158+
A scalar value specifying the lower bound of the histogram.
159+
160+
max_val: scalar.
161+
A scalar value specifying the upper bound of the histogram.
162+
163+
title: str.
164+
Title used for the histogram.
165+
"""
64166
_cell = _Cell(self._r, self._c, title, self._cmap)
65167
safe_call(backend.get().af_draw_hist(self._wnd, X.arr,
66168
ct.c_double(max_val), ct.c_double(min_val),
67169
ct.pointer(_cell)))
68170

69-
def grid(rows, cols):
70-
safe_call(af_grid(self._wnd, ct.c_int(rows), ct.c_int(cols)))
171+
def grid(self, rows, cols):
172+
"""
173+
Create a grid for sub plotting within the window.
174+
175+
Parameters
176+
----------
177+
178+
rows: int.
179+
Number of rows in the grid.
180+
181+
cols: int.
182+
Number of columns in the grid.
183+
184+
"""
185+
safe_call(backend.get().af_grid(self._wnd, ct.c_int(rows), ct.c_int(cols)))
71186

72187
def show(self):
188+
"""
189+
Force the window to display the contents.
190+
191+
Note: This is only needed when using the window as a grid.
192+
"""
73193
safe_call(backend.get().af_show(self._wnd))
74194

75195
def close(self):
196+
"""
197+
Close the window.
198+
"""
76199
tmp = ct.c_bool(True)
77200
safe_call(backend.get().af_is_window_closed(ct.pointer(tmp), self._wnd))
78201
return tmp
79202

80203
def __getitem__(self, keys):
204+
"""
205+
Get access to a specific grid location within the window.
206+
207+
Examples
208+
--------
209+
210+
>>> a = af.randu(5,5)
211+
>>> b = af.randu(5,5)
212+
>>> w = af.Window()
213+
>>> w.grid(1,2)
214+
>>> w[0, 0].image(a)
215+
>>> w[0, 1].image(b)
216+
>>> w.show()
217+
"""
81218
if not isinstance(keys, tuple):
82219
raise IndexError("Window expects indexing along two dimensions")
83220
if len(keys) != 2:
@@ -86,3 +223,5 @@ def __getitem__(self, keys):
86223
raise IndexError("Window expects the indices to be numbers")
87224
self._r = keys[0]
88225
self._c = keys[1]
226+
227+
return self

examples/plot2d.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
val = -math.pi
1919
X = math.pi * (2 * (af.range(POINTS) / POINTS) - 1)
2020

21-
win = af.window(512, 512, "2D Plot example using ArrayFire")
21+
win = af.Window(512, 512, "2D Plot example using ArrayFire")
2222
sign = 1.0
2323

2424
while not win.close():

0 commit comments

Comments
 (0)