Object array and object with array approach
A sample object class
In [56]: class MyObj(object):
....: def __init__(self, par1,par2):
....: self.par1=par1
....: self.par2=par2
An array of those objects - little more than a list with an array wrapper
In [57]: objects=np.array([MyObj(1,2),MyObj(3,4),MyObj(2,3),MyObj(10,11)])
In [58]: objects
Out[58]:
array([<__main__.MyObj object at 0xb31b196c>,
<__main__.MyObj object at 0xb31b116c>,
<__main__.MyObj object at 0xb31b13cc>,
<__main__.MyObj object at 0xb31b130c>], dtype=object)
`subset`` type of selection:
In [59]: [obj.par1 for obj in objects[1:-1]]
Out[59]: [3, 2]
Another class that can contain such an array. This is simpler than defining an array subclass:
In [60]: class MyObjs(object):
....: def __init__(self,anArray):
....: self.data=anArray
....: def par1(self):
....: return [obj.par1 for obj in self.data]
In [61]: Obs = MyObjs(objects)
In [62]: Obs.par1()
Out[62]: [1, 3, 2, 10]
subset2
type of selection:
In [63]: Obs.par1()[1:-1]
Out[63]: [3, 2]
For now par1
is a method, but could made a property, permitting Obs.par1[1:-1]
syntax.
If par1
returned an array instead of a list, indexing would be more powerful.
If MyObjs
had a __getitem__
method, then it could be indexed with
Obs[1:-1]
That method could be defined in various ways, though the simplest is to apply the indexing 'slice' to the 'data':
def __getitem__(self, *args):
# not tested
return MyObjs(self.data.__getitem(*args))
I'm focusing just on syntax, not on efficiency. In general numpy arrays of general objects is not very fast or powerful. Such arrays are basically lists of pointers to the objects.
Structured array and recarray version
Another possiblity is np.recarray
. Another poster was just asking about their names. They essentially are structured array where fields can be accessed as attributes.
With a structured array definition:
In [64]: dt = np.dtype([('par1', int), ('par2', int)])
In [66]: Obj1 = np.array([(1,2),(3,4),(2,3),(10,11)], dtype=dt)
In [67]: Obj1
Out[67]:
array([(1, 2), (3, 4), (2, 3), (10, 11)],
dtype=[('par1', '<i4'), ('par2', '<i4')])
In [68]: Obj1['par1'][1:-1]
Out[68]: array([3, 2])
In [69]: Obj1[1:-1]['par1']
Out[69]: array([3, 2])
or as recarray
In [79]: Objrec=np.rec.fromrecords(Obj1,dtype=dt)
In [80]: Objrec.par1
Out[80]: array([ 1, 3, 2, 10])
In [81]: Objrec.par1[1:-1]
Out[81]: array([3, 2])
In [82]: Objrec[1:-1].par1
Out[82]: array([3, 2])