[ase-users] Appending Ulm-file does not work
Simon Lamowski
simon.lamowski at uni-konstanz.de
Thu Mar 1 14:55:03 CET 2018
Dear Jens Jørgen,
thanks a lot! I change my example to reflect your answer and it worked.
I think, the documentation could be amended to make thinks more clear,
what is possible with the ulm-files.
Here is a suggestion for an addition to the Documentation
(https://wiki.fysik.dtu.dk/ase/ase/io/ulm.html):
An advanced example
The following script creates a ulm-file, containing two small lists,
closes the file and appends it later with an array.
>>> import numpy as np
>>> import ase.io.ulm as ulm
>>>
>>>
>>> ##write lists
... w = ulm.open('test.ulm', 'w')
>>> w.write(smalllist = [1, 1])
>>> w.write(list2 = [0, 2])
>>> w.close()
>>>
>>> ##append file with an array
... bigarray = np.arange(12,dtype=float).reshape((4, 3))
>>> print('bigarray\n', bigarray)
bigarray
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]
[ 9. 10. 11.]]
>>> w2 = ulm.Writer('test.ulm', 'a')
>>> w2.add_array('bigarray', bigarray.shape, bigarray.dtype)
>>> for i in range(bigarray.shape[0]):
... w2.fill(bigarray[i])
...
>>> w2.close()
>>>
The reader opens the file again and prints the data, which was written
first:
>>> ## open file again: this gives only the data from the
...
>>> R = ulm.Reader('test.ulm')
>>> d = R.asdict()
>>> print(d.keys() )
dict_keys(['list2', 'smalllist'])
To get all the data, it is possible to iterate over the items in the file.
>>> ## print all data in the file
... for r in ulm.Reader('test.ulm'):
... dct = r.asdict()
... for k in dct.keys():
... print(k)
... print(dct[k])
...
...
list2
[0 2]
smalllist
[1 1]
bigarray
[[ 0. 1. 2.]
[ 3. 4. 5.]
[ 6. 7. 8.]
[ 9. 10. 11.]]
>>>
The different parts (items) of the file are numbered by the index
argument. Or in the usual way to get items:
>>> r[0].asdict().keys()
dict_keys(['list2', 'smalllist'])
>>> r.close()
The last command closes the file.
---
Thanks again.
Best regards,
Simon
On 01-03-2018 09:01, Jens Jørgen Mortensen wrote:
> On 02/28/2018 04:48 PM, Simon Lamowski via ase-users wrote:
>> Hello all!
>>
>> I was testing to append an ulm-file and found that it did not work.
>> Hence I attached a minimal example.
>>
>> Since I do not understand why it is not working, I hope that someone,
>> who is more familiar with the code can fix it.
>>
>> By the way, the add_array and fill is working, if the file is not closed
>> and reopened.
>>
>> Here are the versions, I use:
>> numpy: 1.14.0
>> scipy: 1.0.0
>> ase: 3.15.0
>> python: 3.4.6
>>
>> It fails as well for versions:
>> scipy: 0.19.1
>> ase: 3.13.0-python34
>> numpy: 1.13.2
>>
>> If you now a workaround, please answer.
>
> When you first write to an ulm file, you write to the block with
> index=0. When you open it in append mode and write some more things,
> that will go to the next block with index=1.
>
> You can look inside an ulm file with:
>
> [jensj at jordan ~]$ ase ulm test.ulm
> test.ulm (tag: "", 2 items)
> item #0:
> {
> smalllist: [1 1]}
> item #1:
> {
> bigarray: <ndarray shape=(4, 3) dtype=float64>}
>
> If you change your example to "reader = ulm.Reader('test.ulm', index=1)"
> and comment out the "print(dct['smalllist'])" line, then it should work.
>
> Jens Jørgen
>
>>
>>
>> Best regards
>>
>> Simon Lamowski
>>
>>
>>
>> _______________________________________________
>> ase-users mailing list
>> ase-users at listserv.fysik.dtu.dk
>> https://listserv.fysik.dtu.dk/mailman/listinfo/ase-users
>
--
Simon Lamowski
PhD student
AG Pauly
visitor address:
University of Konstanz
Department of Physics
Universitätsstraße 10
Room Z1018
D-78464 Konstanz
Germany
postal address:
Simon Lamowski
University of Konstanz
Department of Physics
P.O. Box 699
D-78457 Konstanz
Germany
Phone: +49 (0) 7531 88 3827
email: simon.lamowski at uni-konstanz.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 473 bytes
Desc: OpenPGP digital signature
URL: <http://listserv.fysik.dtu.dk/pipermail/ase-users/attachments/20180301/51bee09b/attachment.sig>
More information about the ase-users
mailing list