Edit dataset value in h5 file

1,073 views
Skip to first unread message

Hannah Ow

unread,
Apr 9, 2019, 11:08:52 PM4/9/19
to h5py
Hi All,

I try to edit .h5 file dataset value to round off the decimal place. Below is two function i use to write and check for the changes.
write() if to change the decimal place and print it in console, the data get changes as expected. Then execute run() again to make sure the dataset value is change, but unfortunately it not change at all.
I use r+ mode to make change the .h5 file due to there is existing value inside it.

def write(): 
        wf = h5py.File('model.h5', 'r+')
        G1 = wf.get('Conv1')
        G1
        G1_item = list(G1.items())
        print('Items in Group1:',G1_item)
        G1_2 = G1.get('Conv1')
        G2_items = list (G1_2.items())
        print('Items in G1_2:',G2_items)
        ds_conv1 = G1_2.get('kernel:0')
        dsconv1= np.array(ds_conv1)
        print(dsconv1[0,0,0,0])
        dsconv1_r = np.around(dsconv1, decimals=7)
        dsconv1[...]=dsconv1_r
        print (dsconv1[0,0,0,0])
        print(dsconv1)
        print (wf.mode) 
        wf.flush()
        wf.close()

write()
#
#
def run():
      f = h5py.File('model.h5', 'r')
      #base_items = list(f.items())
      #print('Items in the base directory:',base_items)
      G3 = f.get('Conv1')
      G3_item = list(G3.items())
      print('Items in Group1:',G3_item)
      G3_2 = G3.get('Conv1')
      G4_items = list (G3_2.items())
      print('Items in G3_2:',G4_items)
      ds_conv3 = G3_2.get('kernel:0')
      dsconv3= np.array(ds_conv3)
      print(dsconv3)
      
run()


I need this help urgently.

Thank in advance.

Regards,
Hannah

Richard McGrath

unread,
Apr 10, 2019, 12:54:05 AM4/10/19
to h5...@googlegroups.com
Hannah,

I believe the write() function should open the file with w+, not r+.

-Rich M.

--
You received this message because you are subscribed to the Google Groups "h5py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h5py+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hannah Ow

unread,
Apr 10, 2019, 1:01:55 AM4/10/19
to h5...@googlegroups.com
Hi Rich M. ,

w+ is Invalid mode; the mode allow must be one of r, r+, w, w-, x, a

image.png

Any other advice ? 

Thank
Hannah

Richard McGrath

unread,
Apr 10, 2019, 1:11:07 AM4/10/19
to h5...@googlegroups.com
Yes sorry, w mode.

Kai Mühlbauer

unread,
Apr 10, 2019, 1:13:22 AM4/10/19
to h5...@googlegroups.com
Hi Hannah,

It seems you just change the retrieved variable within python but not linked it back to the Hdf5 file.

You would need to do something along these lines:

`wf[group][subgrp][var][:] = newarr`

HTH,
Kai
--
Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.

Hannah Ow

unread,
Apr 10, 2019, 1:29:48 AM4/10/19
to h5py

Hi Kai,

Thank for the advice. The changes is save with your suggestion! 


Thank
Hannah
-Rich M.

To unsubscribe from this group and stop receiving emails from it, send an email to h5...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "h5py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to h5...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kai Muehlbauer

unread,
Apr 10, 2019, 2:48:35 AM4/10/19
to h5...@googlegroups.com
Hi Hannah,

glad it worked!

I'm not that deep into h5py but it might also work if you subset the
h5py variable like that:

var = wf[group][subgrp][hdfvar]
var[indices] = new_values
wf.flush()
wf.close()

This way you only have the var linked to the hdf-var and operate
directly on the hdfvar-dataset, without the need to have extra arrays
and memory operations.

Cheers,
Kai

Am 10.04.19 um 07:29 schrieb Hannah Ow:
>
> Hi Kai,
>
> Thank for the advice. The changes is save with your suggestion! 
>
>
> Thank
> Hannah
>
> On Wednesday, April 10, 2019 at 1:13:22 PM UTC+8, Kai Mühlbauer wrote:
>
> Hi Hannah,
>
> It seems you just change the retrieved variable within python but
> not linked it back to the Hdf5 file.
>
> You would need to do something along these lines:
>
> `wf[group][subgrp][var][:] = newarr`
>
> HTH,
> Kai
>
> Am 10. April 2019 07:01:42 MESZ schrieb Hannah Ow
> <hanna...@gmail.com <javascript:>>:
>
> Hi Rich M. ,
>
> w+ is Invalid mode; the mode allow must be one of r, r+, w, w-, x, a
>
> image.png
>
> Any other advice ? 
>
> Thank
> Hannah
>
> On Wed, Apr 10, 2019 at 12:54 PM Richard McGrath
> <freakin...@gmail.com <javascript:>> wrote:
>
> Hannah,
>
> I believe the write() function should open the file with w+,
> not r+.
>
> -Rich M.
>
> On Tue, Apr 9, 2019, 9:08 PM Hannah Ow <hanna...@gmail.com
> <javascript:>> wrote:
>
> Hi All,
>
> I try to edit .h5 file dataset value to round off the
> decimal place. Below is two function i use to write and
> check for the changes.
> *write()* if to change the decimal place and print it in
> console, the data get changes as expected. Then execute
> *run()* again to make sure the dataset value is change,
> but unfortunately it not change at all.
> I use r+ mode to make change the .h5 file due to there
> is existing value inside it.
>
> /def write(): /
> /        wf = h5py.File('model.h5', 'r+')/
> /        G1 = wf.get('Conv1')
> /
> /        G1/
> /        G1_item = list(G1.items())/
> /        print('Items in Group1:',G1_item)/
> /        G1_2 = G1.get('Conv1')/
> /        G2_items = list (G1_2.items())/
> /        print('Items in G1_2:',G2_items)/
> /        ds_conv1 = G1_2.get('kernel:0')/
> /        dsconv1= np.array(ds_conv1)
> /
> /        print(dsconv1[0,0,0,0])/
> /        dsconv1_r = np.around(dsconv1, decimals=7)/
> /        dsconv1[...]=dsconv1_r
> /
> /        print (dsconv1[0,0,0,0])/
> /        print(dsconv1)/
> /        print (wf.mode) /
> /        wf.flush()/
> /        wf.close()/
> /
> /
> /write()
> /
> /#/
> /#/
> /def run():/
> /      f = h5py.File('model.h5', 'r')
> /
> /      #base_items = list(f.items())/
> /      #print('Items in the base directory:',base_items)/
> /      G3 = f.get('Conv1')/
> /      G3_item = list(G3.items())/
> /      print('Items in Group1:',G3_item)/
> /      G3_2 = G3.get('Conv1')/
> /      G4_items = list (G3_2.items())/
> /      print('Items in G3_2:',G4_items)/
> /      ds_conv3 = G3_2.get('kernel:0')/
> /      dsconv3= np.array(ds_conv3)/
> /      print(dsconv3)
> /
> /      /
> /run()/
> /
> /
> /
> /
> I need this help urgently.
>
> Thank in advance.
>
> Regards,
> Hannah
>
> --
> You received this message because you are subscribed to
> the Google Groups "h5py" group.
> To unsubscribe from this group and stop receiving emails
> from it, send an email to h5...@googlegroups.com
> <javascript:>.
> For more options, visit
> https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the
> Google Groups "h5py" group.
> To unsubscribe from this group and stop receiving emails
> from it, send an email to h5...@googlegroups.com <javascript:>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> Diese Nachricht wurde von meinem Android-Gerät mit K-9 Mail gesendet.
>
> --
> You received this message because you are subscribed to the Google
> Groups "h5py" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to h5py+uns...@googlegroups.com
> <mailto:h5py+uns...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

--
Kai Muehlbauer
Institute for Geosciences and Meteorology University of Bonn
Auf dem Huegel 20 | +49 228 739083
D-53121 Bonn | kai.mue...@uni-bonn.de
Reply all
Reply to author
Forward
0 new messages