I'm porting some code from R to python (note: in R lists start at the 1st, not 0th element) and instead of changing every place I access an array I want to create an child class of numpy.array so that, for example, the following code
import numpy
class array_starting_at_one(numpy.array):
???
def myfunc(A):
print A[1,1,1]
print A[1:3,1,:]
A = array_starting_at_one([[[1, 2, 3], [4, 5, 6]], [[11, 12, 13], [14, 15, 16]]])
myfunc(A)
outputs
1
[[ 1 2 3]
[11 12 13]]
Anyone know how to fill in the ??? in the above code?
Here's the solution I came up with:
import numpy
def adj(attr):
if attr==None:
return attr
else:
return attr-1
def adjust_slice(x):
if isinstance(x,int):
return x-1
elif isinstance(x,slice):
return slice(*[adj(attrib) for attrib in (x.start,x.stop,x.step)])
elif isinstance(x,list):
return slice(x[0]-1,x[-1]-1,1)
else:
raise Exception("Expected slice, list, or int.")
class array_starting_at_one(list):
def __init__(self,np_array):
self.np_array = numpy.array(np_array)
def __getitem__(self,i):
if isinstance(i,int):
i=i-1
elif isinstance(i,tuple):
i = tuple([adjust_slice(x) for x in i])
else:
return array_starting_at_one(self.np_array[adjust_slice(x)])
return self.np_array[i]
def __setitem__(self,i,y):
if isinstance(i,int):
self.np_array[i-1] = y
elif isinstance(i,tuple):
self.np_array[tuple([adjust_slice(x) for x in i])] = y
else:
self.np_array[adjust_slice(x)] = y
def __getslice__(self,i,j):
return array_starting_at_one(self.np_array[(i-1):(j-1)])
def __setslice__(self,i,j,y):
self.np_array[i-1:j-1]=y
def __repr__(self):
print self.np_array
def __str__(self):
return str(self.np_array)
It worked for what I needed it to, but I didn't take the time to test it too carefully, so please use with caution.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With