How to copy a dataset object to a different hdf5 file using pytables or h5py?












0















I have selected specific hdf5 datasets and want to copy them to a new hdf5 file. I could find some tutorials on copying between two files, but what if you have just created a new file and you want to copy datasets to the file? I thought the way below would work, but it doesn't. Are there any simple ways to do this?



>>> dic_oldDataset['old_dataset']
<HDF5 dataset "old_dataset": shape (333217,), type "|V14">

>>> new_file = h5py.File('new_file.h5', 'a')
>>> new_file.create_group('new_group')

>>> new_file['new_group']['new_dataset'] = dic_oldDataset['old_dataset']


RuntimeError: Unable to create link (interfile hard links are not allowed)









share|improve this question


















  • 1





    new_file['new_group'].create_dataset('name', data=dic_oldDataset['old_dataset'][:]. In other words, make a new dataset in the group, and fill it with the old dataset, or with the array loaded from the old. The [:] loads the dataset into an array; I have to test to see whether it is really needed.

    – hpaulj
    Nov 24 '18 at 7:15
















0















I have selected specific hdf5 datasets and want to copy them to a new hdf5 file. I could find some tutorials on copying between two files, but what if you have just created a new file and you want to copy datasets to the file? I thought the way below would work, but it doesn't. Are there any simple ways to do this?



>>> dic_oldDataset['old_dataset']
<HDF5 dataset "old_dataset": shape (333217,), type "|V14">

>>> new_file = h5py.File('new_file.h5', 'a')
>>> new_file.create_group('new_group')

>>> new_file['new_group']['new_dataset'] = dic_oldDataset['old_dataset']


RuntimeError: Unable to create link (interfile hard links are not allowed)









share|improve this question


















  • 1





    new_file['new_group'].create_dataset('name', data=dic_oldDataset['old_dataset'][:]. In other words, make a new dataset in the group, and fill it with the old dataset, or with the array loaded from the old. The [:] loads the dataset into an array; I have to test to see whether it is really needed.

    – hpaulj
    Nov 24 '18 at 7:15














0












0








0








I have selected specific hdf5 datasets and want to copy them to a new hdf5 file. I could find some tutorials on copying between two files, but what if you have just created a new file and you want to copy datasets to the file? I thought the way below would work, but it doesn't. Are there any simple ways to do this?



>>> dic_oldDataset['old_dataset']
<HDF5 dataset "old_dataset": shape (333217,), type "|V14">

>>> new_file = h5py.File('new_file.h5', 'a')
>>> new_file.create_group('new_group')

>>> new_file['new_group']['new_dataset'] = dic_oldDataset['old_dataset']


RuntimeError: Unable to create link (interfile hard links are not allowed)









share|improve this question














I have selected specific hdf5 datasets and want to copy them to a new hdf5 file. I could find some tutorials on copying between two files, but what if you have just created a new file and you want to copy datasets to the file? I thought the way below would work, but it doesn't. Are there any simple ways to do this?



>>> dic_oldDataset['old_dataset']
<HDF5 dataset "old_dataset": shape (333217,), type "|V14">

>>> new_file = h5py.File('new_file.h5', 'a')
>>> new_file.create_group('new_group')

>>> new_file['new_group']['new_dataset'] = dic_oldDataset['old_dataset']


RuntimeError: Unable to create link (interfile hard links are not allowed)






python hdf5 h5py pytables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 6:22









maynullmaynull

486414




486414








  • 1





    new_file['new_group'].create_dataset('name', data=dic_oldDataset['old_dataset'][:]. In other words, make a new dataset in the group, and fill it with the old dataset, or with the array loaded from the old. The [:] loads the dataset into an array; I have to test to see whether it is really needed.

    – hpaulj
    Nov 24 '18 at 7:15














  • 1





    new_file['new_group'].create_dataset('name', data=dic_oldDataset['old_dataset'][:]. In other words, make a new dataset in the group, and fill it with the old dataset, or with the array loaded from the old. The [:] loads the dataset into an array; I have to test to see whether it is really needed.

    – hpaulj
    Nov 24 '18 at 7:15








1




1





new_file['new_group'].create_dataset('name', data=dic_oldDataset['old_dataset'][:]. In other words, make a new dataset in the group, and fill it with the old dataset, or with the array loaded from the old. The [:] loads the dataset into an array; I have to test to see whether it is really needed.

– hpaulj
Nov 24 '18 at 7:15





new_file['new_group'].create_dataset('name', data=dic_oldDataset['old_dataset'][:]. In other words, make a new dataset in the group, and fill it with the old dataset, or with the array loaded from the old. The [:] loads the dataset into an array; I have to test to see whether it is really needed.

– hpaulj
Nov 24 '18 at 7:15












2 Answers
2






active

oldest

votes


















1














Answer 1 (using h5py):

This creates a simple structured array to populate the first dataset in the first file.
The data is then read from that dataset and copied to the second file using my_array.



import h5py, numpy as np

arr = np.array([(1,'a'), (2,'b')],
dtype=[('foo', int), ('bar', 'S1')])
print (arr.dtype)

h5file1 = h5py.File('test1.h5', 'w')
h5file1.create_dataset('/ex_group1/ex_ds1', data=arr)
print (h5file1)

my_array=h5file1['/ex_group1/ex_ds1']

h5file2 = h5py.File('test2.h5', 'w')
h5file2.create_dataset('/exgroup2/ex_ds2', data=my_array)
print (h5file2)

h5file1.close()
h5file2.close()





share|improve this answer


























  • Thank you for your detailed explanation! Have a good day!

    – maynull
    Dec 8 '18 at 13:42



















1














Answer 2 (using pytables):

This follows the same process as above with pytables functions. It creates the same simple structured array to populate the first dataset in the first file. The data is then read from that dataset and copied to the second file using my_array.



import tables, numpy as np

arr = np.array([(1,'a'), (2,'b')],
dtype=[('foo', int), ('bar', 'S1')])
print (arr.dtype)
h5file1 = tables.open_file('test1.h5', mode = 'w', title = 'Test file')
my_group = h5file1.create_group('/', 'ex_group1', 'Example Group')
my_table = h5file1.create_table(my_group, 'ex_ds1', None, 'Example dataset', obj=arr)
print (h5file1)

my_array=my_table.read()

h5file2 = tables.open_file('test2.h5', mode = 'w', title = 'Test file')
h5file2.create_table('/exgroup2', 'ex_ds2', createparents=True, obj=my_array)
print (h5file2)

h5file1.close()
h5file2.close()





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455713%2fhow-to-copy-a-dataset-object-to-a-different-hdf5-file-using-pytables-or-h5py%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Answer 1 (using h5py):

    This creates a simple structured array to populate the first dataset in the first file.
    The data is then read from that dataset and copied to the second file using my_array.



    import h5py, numpy as np

    arr = np.array([(1,'a'), (2,'b')],
    dtype=[('foo', int), ('bar', 'S1')])
    print (arr.dtype)

    h5file1 = h5py.File('test1.h5', 'w')
    h5file1.create_dataset('/ex_group1/ex_ds1', data=arr)
    print (h5file1)

    my_array=h5file1['/ex_group1/ex_ds1']

    h5file2 = h5py.File('test2.h5', 'w')
    h5file2.create_dataset('/exgroup2/ex_ds2', data=my_array)
    print (h5file2)

    h5file1.close()
    h5file2.close()





    share|improve this answer


























    • Thank you for your detailed explanation! Have a good day!

      – maynull
      Dec 8 '18 at 13:42
















    1














    Answer 1 (using h5py):

    This creates a simple structured array to populate the first dataset in the first file.
    The data is then read from that dataset and copied to the second file using my_array.



    import h5py, numpy as np

    arr = np.array([(1,'a'), (2,'b')],
    dtype=[('foo', int), ('bar', 'S1')])
    print (arr.dtype)

    h5file1 = h5py.File('test1.h5', 'w')
    h5file1.create_dataset('/ex_group1/ex_ds1', data=arr)
    print (h5file1)

    my_array=h5file1['/ex_group1/ex_ds1']

    h5file2 = h5py.File('test2.h5', 'w')
    h5file2.create_dataset('/exgroup2/ex_ds2', data=my_array)
    print (h5file2)

    h5file1.close()
    h5file2.close()





    share|improve this answer


























    • Thank you for your detailed explanation! Have a good day!

      – maynull
      Dec 8 '18 at 13:42














    1












    1








    1







    Answer 1 (using h5py):

    This creates a simple structured array to populate the first dataset in the first file.
    The data is then read from that dataset and copied to the second file using my_array.



    import h5py, numpy as np

    arr = np.array([(1,'a'), (2,'b')],
    dtype=[('foo', int), ('bar', 'S1')])
    print (arr.dtype)

    h5file1 = h5py.File('test1.h5', 'w')
    h5file1.create_dataset('/ex_group1/ex_ds1', data=arr)
    print (h5file1)

    my_array=h5file1['/ex_group1/ex_ds1']

    h5file2 = h5py.File('test2.h5', 'w')
    h5file2.create_dataset('/exgroup2/ex_ds2', data=my_array)
    print (h5file2)

    h5file1.close()
    h5file2.close()





    share|improve this answer















    Answer 1 (using h5py):

    This creates a simple structured array to populate the first dataset in the first file.
    The data is then read from that dataset and copied to the second file using my_array.



    import h5py, numpy as np

    arr = np.array([(1,'a'), (2,'b')],
    dtype=[('foo', int), ('bar', 'S1')])
    print (arr.dtype)

    h5file1 = h5py.File('test1.h5', 'w')
    h5file1.create_dataset('/ex_group1/ex_ds1', data=arr)
    print (h5file1)

    my_array=h5file1['/ex_group1/ex_ds1']

    h5file2 = h5py.File('test2.h5', 'w')
    h5file2.create_dataset('/exgroup2/ex_ds2', data=my_array)
    print (h5file2)

    h5file1.close()
    h5file2.close()






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 25 '18 at 2:20

























    answered Nov 24 '18 at 22:36









    kcw78kcw78

    345210




    345210













    • Thank you for your detailed explanation! Have a good day!

      – maynull
      Dec 8 '18 at 13:42



















    • Thank you for your detailed explanation! Have a good day!

      – maynull
      Dec 8 '18 at 13:42

















    Thank you for your detailed explanation! Have a good day!

    – maynull
    Dec 8 '18 at 13:42





    Thank you for your detailed explanation! Have a good day!

    – maynull
    Dec 8 '18 at 13:42













    1














    Answer 2 (using pytables):

    This follows the same process as above with pytables functions. It creates the same simple structured array to populate the first dataset in the first file. The data is then read from that dataset and copied to the second file using my_array.



    import tables, numpy as np

    arr = np.array([(1,'a'), (2,'b')],
    dtype=[('foo', int), ('bar', 'S1')])
    print (arr.dtype)
    h5file1 = tables.open_file('test1.h5', mode = 'w', title = 'Test file')
    my_group = h5file1.create_group('/', 'ex_group1', 'Example Group')
    my_table = h5file1.create_table(my_group, 'ex_ds1', None, 'Example dataset', obj=arr)
    print (h5file1)

    my_array=my_table.read()

    h5file2 = tables.open_file('test2.h5', mode = 'w', title = 'Test file')
    h5file2.create_table('/exgroup2', 'ex_ds2', createparents=True, obj=my_array)
    print (h5file2)

    h5file1.close()
    h5file2.close()





    share|improve this answer




























      1














      Answer 2 (using pytables):

      This follows the same process as above with pytables functions. It creates the same simple structured array to populate the first dataset in the first file. The data is then read from that dataset and copied to the second file using my_array.



      import tables, numpy as np

      arr = np.array([(1,'a'), (2,'b')],
      dtype=[('foo', int), ('bar', 'S1')])
      print (arr.dtype)
      h5file1 = tables.open_file('test1.h5', mode = 'w', title = 'Test file')
      my_group = h5file1.create_group('/', 'ex_group1', 'Example Group')
      my_table = h5file1.create_table(my_group, 'ex_ds1', None, 'Example dataset', obj=arr)
      print (h5file1)

      my_array=my_table.read()

      h5file2 = tables.open_file('test2.h5', mode = 'w', title = 'Test file')
      h5file2.create_table('/exgroup2', 'ex_ds2', createparents=True, obj=my_array)
      print (h5file2)

      h5file1.close()
      h5file2.close()





      share|improve this answer


























        1












        1








        1







        Answer 2 (using pytables):

        This follows the same process as above with pytables functions. It creates the same simple structured array to populate the first dataset in the first file. The data is then read from that dataset and copied to the second file using my_array.



        import tables, numpy as np

        arr = np.array([(1,'a'), (2,'b')],
        dtype=[('foo', int), ('bar', 'S1')])
        print (arr.dtype)
        h5file1 = tables.open_file('test1.h5', mode = 'w', title = 'Test file')
        my_group = h5file1.create_group('/', 'ex_group1', 'Example Group')
        my_table = h5file1.create_table(my_group, 'ex_ds1', None, 'Example dataset', obj=arr)
        print (h5file1)

        my_array=my_table.read()

        h5file2 = tables.open_file('test2.h5', mode = 'w', title = 'Test file')
        h5file2.create_table('/exgroup2', 'ex_ds2', createparents=True, obj=my_array)
        print (h5file2)

        h5file1.close()
        h5file2.close()





        share|improve this answer













        Answer 2 (using pytables):

        This follows the same process as above with pytables functions. It creates the same simple structured array to populate the first dataset in the first file. The data is then read from that dataset and copied to the second file using my_array.



        import tables, numpy as np

        arr = np.array([(1,'a'), (2,'b')],
        dtype=[('foo', int), ('bar', 'S1')])
        print (arr.dtype)
        h5file1 = tables.open_file('test1.h5', mode = 'w', title = 'Test file')
        my_group = h5file1.create_group('/', 'ex_group1', 'Example Group')
        my_table = h5file1.create_table(my_group, 'ex_ds1', None, 'Example dataset', obj=arr)
        print (h5file1)

        my_array=my_table.read()

        h5file2 = tables.open_file('test2.h5', mode = 'w', title = 'Test file')
        h5file2.create_table('/exgroup2', 'ex_ds2', createparents=True, obj=my_array)
        print (h5file2)

        h5file1.close()
        h5file2.close()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 22:40









        kcw78kcw78

        345210




        345210






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455713%2fhow-to-copy-a-dataset-object-to-a-different-hdf5-file-using-pytables-or-h5py%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen