7
7
# http://arrayfire.com/licenses/BSD-3-Clause
8
8
########################################################
9
9
10
+ """
11
+ graphics functions for arrayfire
12
+ """
13
+
10
14
from .library import *
11
15
from .array import *
12
16
@@ -20,11 +24,27 @@ def __init__(self, r, c, title, cmap):
20
24
self .row = r
21
25
self .col = c
22
26
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.
24
41
25
- class window (object ):
42
+ title: optional: str. default: "ArrayFire".
43
+ - Specifies the title used for the window.
26
44
27
- def __init__ (self , width = None , height = None , title = None ):
45
+ """
46
+
47
+ def __init__ (self , width = 1280 , height = 720 , title = "ArrayFire" ):
28
48
self ._r = - 1
29
49
self ._c = - 1
30
50
self ._wnd = ct .c_longlong (0 )
@@ -41,43 +61,160 @@ def __init__(self, width=None, height=None, title=None):
41
61
ct .c_char_p (_title )))
42
62
43
63
def __del__ (self ):
64
+ """
65
+ Destroys the window when going out of scope.
66
+ """
44
67
safe_call (backend .get ().af_destroy_window (self ._wnd ))
45
68
46
69
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
+ """
47
83
safe_call (backend .get ().af_set_position (self ._wnd , ct .c_int (x ), ct .c_int (y )))
48
84
49
85
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
+ """
50
96
safe_call (backend .get ().af_set_title (self ._wnd , title ))
51
97
52
98
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
+ """
53
109
self ._cmap = cmap
54
110
55
111
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
+ """
56
125
_cell = _Cell (self ._r , self ._c , title , self ._cmap )
57
126
safe_call (backend .get ().af_draw_image (self ._wnd , img .arr , ct .pointer (_cell )))
58
127
59
128
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
+ """
60
144
_cell = _Cell (self ._r , self ._c , title , self ._cmap )
61
145
safe_call (backend .get ().af_draw_plot (self ._wnd , X .arr , Y .arr , ct .pointer (_cell )))
62
146
63
147
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
+ """
64
166
_cell = _Cell (self ._r , self ._c , title , self ._cmap )
65
167
safe_call (backend .get ().af_draw_hist (self ._wnd , X .arr ,
66
168
ct .c_double (max_val ), ct .c_double (min_val ),
67
169
ct .pointer (_cell )))
68
170
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 )))
71
186
72
187
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
+ """
73
193
safe_call (backend .get ().af_show (self ._wnd ))
74
194
75
195
def close (self ):
196
+ """
197
+ Close the window.
198
+ """
76
199
tmp = ct .c_bool (True )
77
200
safe_call (backend .get ().af_is_window_closed (ct .pointer (tmp ), self ._wnd ))
78
201
return tmp
79
202
80
203
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
+ """
81
218
if not isinstance (keys , tuple ):
82
219
raise IndexError ("Window expects indexing along two dimensions" )
83
220
if len (keys ) != 2 :
@@ -86,3 +223,5 @@ def __getitem__(self, keys):
86
223
raise IndexError ("Window expects the indices to be numbers" )
87
224
self ._r = keys [0 ]
88
225
self ._c = keys [1 ]
226
+
227
+ return self
0 commit comments