This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
forked from imnp/pygestalt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapbook.py
101 lines (82 loc) · 4.88 KB
/
scrapbook.py
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
#just a scrapbook of code that might want to reference even if it's been taken out.
#raising errors
if templateName: errorMessage = str(self.keyName) + " not found in template " + templateName + "."
else: errorMessage = str(self.keyName) + " not found in template."
raise KeyError(errorMessage)
def _getItem_(self, positionTuple, subArray = None):
"""Internal recursive method for indexing into a multi-dimensional array.
positionTuple -- this is provided by the Python interpreter when the array is indexed
subArray -- this is only used internally for the recursive algorithm
Note that the intended pattern is that this method gets called by __getitem__. It is broken out
because __getitem__ might be overridden by child classes such as geometry.matrix to enforce
a particular dimensionality of the result.
"""
if type(positionTuple) != tuple: positionTuple = (positionTuple,) #catches condition when only one index is provided
if subArray == None: #starting the algorithm
subArray = list(self) #need to convert to a list to avoid a circular reference
arraySize = self.getSize() #a tuple containing the sizes of each dimension of the array
arrayDimension = self.getDimension()
if type(positionTuple[-1]) != slice and len(positionTuple) < arrayDimension:
#the requested index is smaller than the array dimension and isn't requesting a slice at the end
#try to increase dimensionality of position tuple to match array, to the extent that the array is
# excessively dimensional (i.e. an index of (x,) into a 1xN array.
for size in arraySize:
if size == 1:
positionTuple = (0,) + positionTuple
else:
break
if len(positionTuple) != arrayDimension:
raise errors.ArrayError("Index Error - array is larger than indices provided, and cannot minimize array to match.")
positionIndex = positionTuple[0]
positionRemainder = positionTuple[1:]
if type(positionIndex) == slice: #asking for an array slice
if positionRemainder == (): #user didn't ask for more than the slice... good!
return array(subArray)
else:
slicedArrayList = [[self._getItem_(positionRemainder, arraySlice)] for arraySlice in subArray]
return array(slicedArrayList)
else: #asking for an indexed value:
try:
indexedValue = subArray[positionIndex]
except:
raise errors.ArrayError("Index Error - index out of bounds of array")
return False
if isinstance(indexedValue, list): #need to index deeper into the array
if positionRemainder == (): #no index provided!
raise errors.ArrayError("Index Error - array is larger than number of indices provided")
return False
else:
return self._getItem_(positionRemainder, indexedValue)
else: #end of array
if positionRemainder == (): #no extra indices requested, good!
return indexedValue #finally return the value
else:
raise errors.ArrayError("Index Error - array is smaller than number of indices provided")
return False
#check if array has more dimensions than indices provide.
inputArraySize = arraySize(inputArray)
if len(inputArraySize) > len(indexTuple): #Ignore as many initial dimensions with a size of 1 as possible.
possibleDimensionsToIgnore = inputArraySize[:(len(inputArraySize)-len(indexTuple))]
print "PossibleDimensionsToIgnore" + str(possibleDimensionsToIgnore)
dimensionsToIgnore = ()
for dimension in possibleDimensionsToIgnore:
if dimension == 1:
dimensionsToIgnore += (0,)
else:
break
def __getitem__(self, positionTuple):
"""Returns an individual item or a slice in the matrix.
positionTuple -- this is provided by the Python interpreter when the array is indexed
This method over-rides the __getitem__ method of the array type, so that it can return a matrix rather than an array.
To index into a specific cell of a matrix, simply index as myMatrix[1,2] to retrieve item in row 1, column 2.
To index an entire subarray, index as myArray[1,:] to retrieve all of row 1.
If the number of indices in positionTuple is less than the dimensionality of the array,
and if no slices are used, then indexing starts at the first dimension whose size is greater than 1.
This is to support compatibility between various formats of arrays, i.e. a 1D array and a 2D 1xN array.
"""
getItemResult = self._getItem_(positionTuple)
if type(getItemResult) == array:
return matrix(getItemResult)
else:
return getItemResult
indexTuple = dimensionsToIgnore