This repository was archived by the owner on Sep 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.py
More file actions
436 lines (361 loc) · 15.2 KB
/
array.py
File metadata and controls
436 lines (361 loc) · 15.2 KB
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
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
106
107
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""The array module provides support for one dimensional arrays as well as
multinational arrays. The main class in this module is the Array class. The
other classes act as support class to Array class. For this reason the Array
class can directly be imported from the ARgorithmToolkit library without having
to import from the array module Both work:
>>> arr = ARgorithmToolkit.Array(name='arr',algo=algo,data=test_data)
>>> arr = ARgorithmToolkit.array.Array(name='arr',algo=algo,data=test_data)
"""
import numpy as np
from ARgorithmToolkit.utils import State, StateSet, ARgorithmError, ARgorithmStructure
from ARgorithmToolkit.encoders import serialize
def check_dimensions(data):
"""This function is an internal function that helps verify the dimensions
of array from user input.
Args:
data : data is a multi-dimensional list or tuple
Raises:
ARgorithmError: if data is not of correct format , it raises an ARgorithmError
"""
if not isinstance(data,list) and not isinstance(data,tuple):
return 1
check = -1
try:
for x in data:
if check == -1:
check = check_dimensions(x)
else:
assert check == check_dimensions(x)
return len(data)
except Exception as ex:
raise ARgorithmError('please pass array of fixed dimensions') from ex
class ArrayState:
"""This class is used to generate states for various actions performed on
the ``ARgorithmToolkit.array.Array`` object.
Attributes:
name (str) : Name of the object for which the states are generated
_id (str) : id of the object for which the states are generated
"""
def __init__(self,name,_id):
self.name = name
self._id = _id
def array_declare(self,body,comments=""):
"""Generates the `array_declare` state when an instance of Array class
is created.
Args:
body: The contents of the array that are to be sent along with the state
comments (optional): The comments that are supposed to rendered with the state for descriptive purpose. Defaults to "".
Returns:
ARgorithmToolkit.utils.State: returns the ``array_declare`` state for the respective array mentioned
"""
state_type = "array_declare"
state_def = {
"id": self._id,
"variable_name" : self.name,
"body" : body.tolist()
}
return State(
state_type=state_type,
state_def=state_def,
comments=comments
)
def array_iter(self,body,index,value=None,last_value=None,comments=""):
"""Generates the `array_iter` state when a particular index of array
has been accessed.
Args:
body: The contents of the array that are to be sent along with the state
index : The index of array that has been accessed
value (optional): The current value at array[index] if __setitem__(self, key, value) was called.
last_value (optional): The current value at array[index] if __setitem__(self, key, value) was called.
comments (optional): The comments that are supposed to rendered with the state for descriptive purpose. Defaults to "".
Returns:
ARgorithmToolkit.utils.State: returns the ``array_iter`` state for the respective array mentioned
"""
state_type = "array_iter"
state_def = {
"id" : self._id,
"variable_name" : self.name,
"body" : body.tolist(),
"index" : index
}
if not (last_value is None):
state_def["value"] = value
state_def["last_value"] = last_value
return State(
state_type=state_type,
state_def=state_def,
comments=comments
)
def array_swap(self,body,indexes,comments=""):
"""Generates the ``array_swap`` state when values at two indexes of
array are being swapped.
Args:
body: The contents of the array that are to be sent along with the state
indexes : The indexes that are supposed to be swapped
comments (optional):The comments that are supposed to rendered with the state for descriptive purpose. Defaults to "".
Returns:
ARgorithmToolkit.utils.State: returns the ``array_swap`` state for the respective array mentioned
"""
state_type = "array_swap"
state_def = {
"id" : self._id,
"variable_name" : self.name,
"body" : body.tolist(),
"index1" : indexes[0],
"index2" : indexes[1]
}
return State(
state_type=state_type,
state_def=state_def,
comments=comments
)
def array_compare(self,body,indexes,comments=""):
"""Generates the ``array_compare`` state when values at two indexes of
array are being compared.
Args:
body: The contents of the array that are to be sent along with the state
indexes : The indexes that are supposed to be compared
comments (optional):The comments that are supposed to rendered with the state for descriptive purpose. Defaults to "".
Returns:
ARgorithmToolkit.utils.State: returns the ``array_compare`` state for the respective array mentioned
"""
state_type = "array_compare"
state_def = {
"id" : self._id,
"variable_name" : self.name,
"body" : body.tolist(),
"index1" : indexes[0],
"index2" : indexes[1]
}
return State(
state_type=state_type,
state_def=state_def,
comments=comments
)
class ArrayIterator:
"""This class is a generator that is returned each time an array has to be
iterated.
Yields:
element of Array
Raises:
AssertionError: If not declared with an instance of ARgorithmToolkit.array.Array
"""
def __init__(self,array):
assert isinstance(array,Array)
self.array = array
self._index = 0
self.size = len(array)
def __next__(self):
if self._index == self.size:
raise StopIteration
v = self.array[self._index]
self._index += 1
return v
@serialize
class Array(ARgorithmStructure):
"""The Array class used to emulate multidimensional arrays that can be
rendered in the ARgorithm Application as series of blocks.
Attributes:
name (str): name given to the rendered block in augmented reality. Essential. Should not be altered after initialisation
algo (ARgorithmToolkit.utils.StateSet): The stateset that will store the states generated by the instance of Array Class
data (list or tuple,optional): The value of array if user wants a predefined value. Defaults to None.
shape (tuple,optional): The shape of the array. Neccessary if data is not given. Gets overwritten if data is given.
fill (dtype,optional): Neccessary if shape is given. Fills the array with the fill character. Defaults to 0.
dtype (type,optional): Datatype of array element.
comments (str,optional): Description of instance of array and its applications that will be rendered during the ``array_declare`` state.
Raises:
ARgorithmError: raised if name is not given or Stateset if not provided
Examples:
This is an example of array being declared using predefined values.
>>> algo = ARgorithmToolkit.StateSet()
>>> test_data = [[1,2,3],[4,5,6]]
>>> arr = ARgorithmToolkit.Array(name='arr',algo=algo,data=test_data)
>>> arr
Array([[1, 2, 3],[4, 5, 6]])
This is an example of array being declared with shape and fill
>>> algo = ARgorithmToolkit.StateSet()
>>> arr = ARgorithmToolkit.Array(name='arr',algo=algo,shape=(2,3),fill=7)
>>> arr
Array([[7, 7, 7],[7, 7, 7]])
The array generated supports all the functionality of regular array
>>> len(arr)
2
>>> arr.shape()
(2,3)
>>> arr[1]
Array([7, 7, 7])
>>> arr[1][2]
7
>>> arr[1,2]
7
>>> for subarr in arr:
... for elem in subarr:
... print(elem)
7
7
7
7
7
7
"""
def __init__(self, name:str, algo:StateSet, data=None, shape=None, fill=0, dtype=int, comments=""):
try:
assert isinstance(name,str)
self.state_generator = ArrayState(name, str(id(self)))
except Exception as ex:
raise ARgorithmError('Give valid name to data structure') from ex
try:
assert isinstance(algo, StateSet)
self.algo = algo
except Exception as ex:
raise ARgorithmError("array structure needs a reference of template to store states") from ex
if data is not None:
check_dimensions(data)
self.body = np.array(data)
self.dtype = self.body.dtype
state = self.state_generator.array_declare(self.body,comments)
self.algo.add_state(state)
return
self.dtype = dtype
self.body = np.full(fill_value = fill, shape=shape, dtype=dtype)
state = self.state_generator.array_declare(self.body,comments)
self.algo.add_state(state)
def __len__(self):
"""returns length of array when processed by len() function.
Returns:
int: length of array or first dimension of array if it is multidimensional
Example:
>>> len(arr)
2
"""
return len(self.body)
def shape(self):
"""Get shape of array. As shown in above example.
Returns:
tuple: shape of array as a tuple
Example:
>>> arr.shape()
(2,3)
"""
return (self.body.shape) if isinstance(self.body.shape,tuple) else self.body.shape
def __getitem__(self, key, comments=""):
"""overloading the item access operator to generate states and create
more instances of ARgorithmToolkit Array if subarray is accessed.
Args:
key (index or slice):
comments (str, optional): Comments for descriptive purpose. Defaults to "".
Raises:
ARgorithmError: Raised if key is invalid
Returns:
element or subarray: depending on key , the returned object can be an element or an sub-array
Examples:
>>> arr[1,2]
6
"""
try:
if isinstance(key,slice):
name = f"{self.state_generator.name}_sub"
return Array(name=name , algo=self.algo , data=self.body[key] , comments=comments)
if isinstance(key,int) and len(self.body.shape)==1:
state = self.state_generator.array_iter(body=self.body, index=key, comments=comments)
self.algo.add_state(state)
return self.body[key]
if isinstance(key,int) or len(key) < len(self.shape()):
name = f"{self.state_generator.name}_sub"
state = self.state_generator.array_iter(body=self.body, index=key, comments=comments)
self.algo.add_state(state)
return Array(name=name, algo=self.algo, data=self.body[key], comments=comments)
state = self.state_generator.array_iter(body=self.body, index=key, comments=comments)
self.algo.add_state(state)
return self.body[key]
except Exception as ex:
raise ARgorithmError(f"invalid index error : {str(ex)}") from ex
def __setitem__(self, key, value):
"""Overload element write operation to trigger state.
Args:
key (index): index where element is written
value (dtype): value of element that is written
Example:
>>> arr
Array([[1, 2, 3],[4, 5, 6]])
>>> arr[1,2] = 0
>>> arr
Array([[1, 2, 3],[4, 5, 0]])
"""
last_value = self.body[key]
self.body[key] = value
state = self.state_generator.array_iter(body=self.body, index=key, value=value, last_value=last_value, comments=f'Writing {value} at index {key}')
self.algo.add_state(state)
def __iter__(self):
"""Generates a iterator object to iterate the array along its first
dimension.
Returns:
ArrayIterator: Iterator object
Example:
>>> [x for x in arr]
[[1,2,3],[4,5,6]]
"""
return ArrayIterator(self)
def compare(self,index1,index2,func=None,comments=""):
"""compares elements at 2 indexes of array.
Args:
index1 (index): The index of first element to be compared
index2 (index): The index of second element to be compared
func (function, optional): [description] The comparision function to be used , defaults to difference
comments (str, optional): Any comments to describe comparision
Returns:
Result of comparision operation
Example:
>>> arr.compare((0,0),(1,1))
-4
"""
item1 = self.body[index1]
item2 = self.body[index2]
state = self.state_generator.array_compare(self.body,(index1,index2),comments)
self.algo.add_state(state)
if func is None:
def default_comparator(item1, item2):
return item1-item2
func = default_comparator
return func(item1, item2)
def swap(self,index1,index2,comments=""):
"""swaps elements at 2 indexes of array.
Args:
index1 (index): The index of first element to be swapped
index2 (index): The index of second element to be swapped
comments (str, optional): Any comments to describe swap
Example:
>>> arr
Array([[1, 2, 3],[4, 5, 6]])
>>> arr.swap((0,2),(1,2))
>>> arr
Array([[1, 2, 6],[4, 5, 3]])
Note:
Do not try to swap subarrays in multidimensional arrays. It will lead to unexpected results
"""
self.body[index1], self.body[index2] = self.body[index2], self.body[index1]
state = self.state_generator.array_swap(self.body, (index1, index2) ,comments)
self.algo.add_state(state)
def tolist(self):
"""Returns array as multidimensional list.
Returns:
list: multidimensional python list containing value of array
Example:
>>> arr.tolist()
[[1,2,3],[4,5,6]]
Note:
The list generated is a normal python list so will not listen and store states. If you want to do that , store the list in the ARgorithmToolkit.vector.Vector object
"""
return self.body.tolist()
def __str__(self):
"""String conversion for Array.
Returns:
str: String describing Array
"""
return f"Array({self.tolist().__str__()})"
def __repr__(self):
"""Return representation for shell outputs.
Returns:
str: shell representation for array
"""
return f"Array({self.tolist().__repr__()})"