Discussion:
Numpy Arrays to Structure Array or Table
David Reed
2013-08-08 00:58:07 UTC
Permalink
Hi there,

I have some generic functions that take time series data with 2 numpy array
arguments, time and value, and return 2 numpy arrays of time and value.

I would like to place these arrays into a Numpy structured array or
directly into a new pytables table with fields, time and value.

Now Ive found I could do this:

t, v = some_func(t, v)

A = np.empty(len(t), dtype=[('time', np.float64), ('value',
np.float64)])

A['time'] = t
A['value'] = v

hfile.createTable(grp, 'signal', description=A)
hfile.flush()

But this seems rather clunky and inefficient. Any suggestions to make this
repackaging a little smoother?
Anthony Scopatz
2013-08-08 07:05:34 UTC
Permalink
Hi David,

I think that you can do what you want in one, rather long line:

hfile.createTable(grp, 'signal', description=np.array(zip(some_func(t, v)),
dtype=[('time', np.float64), ('value', np.float64)]))

Or two nicer lines:

arr = np.array(zip(some_func(t, v)), dtype=[('time', np.float64), ('value',
np.float64)])
hfile.createTable(grp, 'signal', description=arr)

zip() is your friend =). If zip is too slow and you don't want to make
more than one copy, you could try something like this:

temparr = np.array(some_func(t, v)).T
arr = np.view(temparr, dtype=[('time', np.float64), ('value', np.float64)])

This really only works because both columns have the same dtype.

Of course, you can always keep basically what you have and loop through the
column names programmaticly:

for name, col in zip(A.dtype.names, some_func(t, v)):
A[name] = col

I hope this helps!

Be Well
Anthony
Post by David Reed
Hi there,
I have some generic functions that take time series data with 2 numpy
array arguments, time and value, and return 2 numpy arrays of time and
value.
I would like to place these arrays into a Numpy structured array or
directly into a new pytables table with fields, time and value.
t, v = some_func(t, v)
A = np.empty(len(t), dtype=[('time', np.float64), ('value',
np.float64)])
A['time'] = t
A['value'] = v
hfile.createTable(grp, 'signal', description=A)
hfile.flush()
But this seems rather clunky and inefficient. Any suggestions to make
this repackaging a little smoother?
------------------------------------------------------------------------------
Get 100% visibility into Java/.NET code with AppDynamics Lite!
It's a free troubleshooting tool designed for production.
Get down to code-level detail for bottlenecks, with <2% overhead.
Download for free and get started troubleshooting in minutes.
http://pubads.g.doubleclick.net/gampad/clk?id=48897031&iu=/4140/ostg.clktrk
_______________________________________________
Pytables-users mailing list
https://lists.sourceforge.net/lists/listinfo/pytables-users
Loading...