[Nexus] 2D character arrays in python binding?

Carlos Pascual Izarra carlos.pascual at cells.es
Tue Feb 3 11:39:49 GMT 2009


On Friday 30 January 2009 20:17:23 Paul Kienzle wrote:
>
> The natural numpy translation of the above example is as a length 3
> array of type '|S10', not a 3x10 array of '|S1'.  If you do
> ascontiguousarray(['a','perfect','day'],'|S10') for example, then it
> will record the correct information.  This should scale to
> multidimensional string arrays if they are needed.

I totally agree.

Please see mynapi.py attached (diff also attached). I only modified _pinput() 
and _poutput(). One might want to also work on the put/get attributes in a 
similar way, but, as far as I can tell this works well with putdata(), 
putslab() and getdata() / getslab()... although I only tried with HDF5.

It has the following new features:
-transparently supports lists (or other iterables including numpy arrays) of 
strings. The strings need not be of the same length on input (see next 
point).
-it automatically pads (or crops) the strings according to the shape passed to 
_pinput().
-It detects shape mismatches between the length of the list and the shape 
passed to _pinput()
-It covers the input and output requirements in my last comment in 
http://trac.nexusformat.org/code/ticket/156

Here is a demo script (play with the shape to see pad and crop):
{{{
import mynapi as nxs
filenames=['bar1','loooooong bar2','bar3']
f=nxs.open('foo.h5','w5')
f.makegroup("entry","NXentry")
f.opengroup("entry","NXentry")
f.makegroup("data","NXdata")
f.opengroup("data","NXdata")
f.makedata('filenames','char',[3,30])
f.opendata('filenames')
f.putdata(filenames)
a= f.getdata()
f.closedata()
f.closegroup()
f.show()
f.close()
print a
print a.dtype
}}}

Cheers,

Carlos

-- 
+---------------------------------------+
 Carlos Pascual Izarra
 Scientific Software Contact 
 Computing Division
 Cells / Alba Synchrotron
 Campus Universitari de Bellaterra (UAB)
 08193 Bellaterra (Barcelona)
 E-mail: carlos.pascual at cells.es
 Phone: +34 93 592 4428
+---------------------------------------+
-------------- next part --------------
A non-text attachment was scrubbed...
Name: mynapi.py
Type: application/x-python
Size: 44555 bytes
Desc: not available
Url : http://lists.nexusformat.org/pipermail/nexus/attachments/20090203/b95e09e0/attachment-0002.bin 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch-napi.py_4rc2.diff
Type: text/x-diff
Size: 3094 bytes
Desc: not available
Url : http://lists.nexusformat.org/pipermail/nexus/attachments/20090203/b95e09e0/attachment-0003.bin 


More information about the NeXus mailing list