views:

100

answers:

1

I have a few functions that return an array of data corresponding to parameters ranges.

Example: for a 2d array a, the a_{ij} value corresponds to the parameter set (param1_i, param2_j). How do I return the result and keep the parameter-value correspondence?

  • Calling the function for each and every of param1_i, para2_j and returning one value would take ages (far more efficient if you do it in one go)
  • Break the function into (many) smaller functions and make usage difficult? (the point is to get the values for a range of parameters, 1 value is completely useless)

The best I can come up with is make a new numpy dtype, for example for a 2d array:

tagged2d = np.dtype( [('vals', float, 1), ('params', float, (2,))] )

so that a['vals'][i,j] contains the values and a['params'][i,j] the corresponding parameters.

Any thoughts? Maybe I should just return 2 arrays, one with values, other with parameter tuples?

+1  A: 

I recommend your last suggestion... just return two arrays {'values': a, 'params':params}.

There are a few reasons for this.

  1. Primarily, your other solution (using dtype and recarrays) tangles too many things together. For example, what about quantities derived from a that correspond to the same parameters... do you make a new recarray and a new copy of the parameters for that? Even something as simple as 2*a becoming the salient quantity will require that you make difficult decisions.

  2. Recarrays have limitations and this is so easily solved in other ways that it's not worth accepting those limitations.

If you want an easier interrelation between the returned terms, you could put the items in a class. For example, you could have a method that takes a param pair and returns the corresponding result. This way, you wouldn't be limited by the recarray, and you could still construct whatever convenience relationship between the two that you like, and easily make backward-compatible change to behavior, etc.

tom10