Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy: Array of class instances

This might be a dumb question, but say i want to build a program from bottom-up like so:

class Atom(object):
    def __init__(self):
        '''
        Constructor
        '''
    def atom(self, foo, bar):
        #...with foo and bar being arrays of atom Params of lengths m & n
        "Do what atoms do"
        return atom_out

...i can put my instances in a dictionary:

class Molecule(Atom):
    def __init__(self):


    def structure(self, a, b):
        #a = 2D array of size (num_of_atoms, m); 'foo' Params for each atom
        #b = 2D array of size (num_of_atoms, n); 'bar' Params for each atom

        unit = self.atom()
        fake_array = {"atom1": unit(a[0], b[0]),
                      "atom2": unit(a[1], b[1]),
                       :                      :                    :
                       :                      :                    :}

    def chemicalBonds(self, this, that, theother):
        :                         :                      :
        :                         :                      :

My question is, is there a way to do this with numpy arrays so that each element in "real_array" would be an instance of atom--i.e., the output of the individual computations of atom function? I can extend this to class Water(molecule): which would perform fast numpy operations on the large structure and chemicalBonds outputs, hence the need for arrays...Or is it the case that i'm going about this the wrong way?

Also if i am on the right track, i'd appreciate if you wanted to throw in any tips on how to structure a "hierarchical program" like this, as i'm not sure i'm doing the above correctly and recently discovered that i don't know what i'm doing.

Thanks in advance.

like image 648
Noob Saibot Avatar asked Mar 16 '13 20:03

Noob Saibot


1 Answers

The path to hell is paved with premature optimization... As a beginner in python, focus on your program and what is supposed to do, once it is doing it too slowly you can ask focused questions about how to make it do it faster. I would stick with learning python's intrinsic data structures for managing your objects. You can implement your algorithms using using numpy arrays with standard data types if you are doing large array operations. Once you have some working code you can do performance testing to determine where you need optimization.

Numpy does allow you to create arrays of objects, and I will give you enough rope to hang yourself with below, but creating an ecosystem of tools to operate on those arrays of objects is not a trivial undertaking. You should first work with python data structures (buy Beazley's essential python reference), then with numpy's built in types, then creating your own compound numpy types. As a last resort, use the object type from the example below.

Good luck!

David

import numpy

class Atom(object):
    def atoms_method(self, foo, bar):
        #...with foo and bar being arrays of Paramsof length m & n
        atom_out = foo + bar
        return atom_out


array = numpy.ndarray((10,),dtype=numpy.object)

for i in xrange(10):
    array[i] = Atom()

for i in xrange(10):
    print array[i].atoms_method(i, 5)
like image 132
David Avatar answered Sep 19 '22 07:09

David