Debian Codegraph @

docs/source/export.rst

raw
1 Export
2 ======
3
4 Export is a new feature in HDMF 2.0. You can use export to take a container that was read from a file and write it to
5 a different file, with or without modifications to the container in memory.
6 The in-memory container being exported will be written to the exported file as if it was never read from a file.
7
8 To export a container, first read the container from a file, then create a new
9 :py:class:`~hdmf.backends.hdf5.h5tools.HDF5IO` object for exporting the data, then call
10 :py:meth:`~hdmf.backends.hdf5.h5tools.HDF5IO.export` on the
11 :py:class:`~hdmf.backends.hdf5.h5tools.HDF5IO` object, passing in the IO object used to read the container
12 and optionally, the container itself, which may be modified in memory between reading and exporting.
13
14 For example:
15
16 .. code-block:: python
17
18 with HDF5IO(self.read_path, manager=manager, mode='r') as read_io:
19 with HDF5IO(self.export_path, mode='w') as export_io:
20 export_io.export(src_io=read_io)
21
22 FAQ
23 ---
24
25 Can I read a container from disk, modify it, and then export the modified container?
26 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27 Yes, you can export the in-memory container after modifying it in memory. The modifications will appear in the exported
28 file and not the read file.
29
30 - If the modifications are removals or additions of containers, then no special action must be taken, as long as the
31 container hierarchy is updated correspondingly.
32 - If the modifications are changes to attributes, then
33 :py:meth:`Container.set_modified() <hdmf.container.AbstractContainer.set_modified>` must be called
34 on the container before exporting.
35
36 .. code-block:: python
37
38 with HDF5IO(self.read_path, manager=manager, mode='r') as read_io:
39 container = read_io.read()
40 # ... # modify container
41 container.set_modified() # this may be necessary if the modifications are changes to attributes
42 with HDF5IO(self.export_path, mode='w') as export_io:
43 export_io.export(src_io=read_io, container=container)
44
45 .. note::
46
47 Modifications to :py:class:`h5py.Dataset <h5py.Dataset>` objects act *directly* on the read file on disk.
48 Changes are applied immediately and do not require exporting or writing the file. If you want to modify a dataset
49 only in the new file, than you should replace the whole object with a new array holding the modified data. To
50 prevent unintentional changes to the source file, the source file should be opened with ``mode='r'``.
51
52 Can I export a newly instantiated container?
53 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54 No, you can only export containers that have been read from a file. The ``src_io`` argument is required in
55 :py:meth:`HDMFIO.export <hdmf.backends.io.HDMFIO.export>`.
56
57 Can I read a container from disk and export only part of the container?
58 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59 It depends. You can only export the root container from a file. To export the root container without certain other
60 sub-containers in the hierarchy, you can remove those other containers before exporting. However, you cannot export
61 only a sub-container of the container hierarchy.
62
63 Can I write a newly instantiated container to two different files?
64 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
65 HDMF does not allow you to write a container that was not read from a file to two different files. For example, if you
66 instantiate container A and write it file 1 and then try to write it to file 2, an error will be raised. However, you
67 can read container A from file 1 and then export it to file 2, with or without modifications to container A in
68 memory.
69
70 What happens to links when I export?
71 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72 The exported file will not contain any links to the original file.
73
74 All links (such as internal links (i.e., HDF5 soft links) and links to other files (i.e., HDF5 external links))
75 will be preserved in the exported file.
76
77 If a link to an :py:class:`h5py.Dataset <h5py.Dataset>` in another file is added to the in-memory container after
78 reading it from file and then exported, then by default, the export process will create an external link to the
79 existing :py:class:`h5py.Dataset <h5py.Dataset>` object. To instead copy the data from the
80 :py:class:`h5py.Dataset <h5py.Dataset>` in another
81 file to the exported file, pass the keyword argument ``write_args={'link_data': False}`` to
82 :py:meth:`HDF5IO.export <hdmf.backends.hdf5.h5tools.HDF5IO.export>`. This is similar to passing the keyword argument
83 ``link_data=False`` to :py:meth:`HDF5IO.write <hdmf.backends.hdf5.h5tools.HDF5IO.write>` when writing a file with a
84 copy of externally linked datasets.
85
86 What happens to references when I export?
87 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88 References will be preserved in the exported file.
89 NOTE: Exporting a file involves loading into memory all datasets that contain references and attributes that are
90 references. The HDF5 reference IDs within an exported file may differ from the reference IDs in the original file.
91
92 What happens to object IDs when I export?
93 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
94 After exporting a container, the object IDs of the container and its child containers will be identical to the object
95 IDs of the read container and its child containers. The object ID of a container uniquely identifies the container
96 within a file, but should *not* be used to distinguish between two different files.
97
98 If you would like all object IDs to change on export, then first call the method
99 :py:meth:`generate_new_id <hdmf.container.AbstractContainer.generate_new_id>` on the root container to generate
100 a new set of IDs for the root container and all of its children, recursively. Then export the container with its
101 new IDs. Note: calling the :py:meth:`generate_new_id <hdmf.container.AbstractContainer.generate_new_id>` method
102 changes the object IDs of the containers in memory. These changes are not reflected in the original file from
103 which the containers were read unless the :py:meth:`HDF5IO.write <hdmf.backends.hdf5.h5tools.HDF5IO.write>`
104 method is subsequently called.
105
106 .. code-block:: python
107
108 with HDF5IO(self.read_path, manager=manager, mode='r') as read_io:
109 container = read_io.read()
110 container.generate_new_id()
111 with HDF5IO(self.export_path, mode='w') as export_io:
112 export_io.export(src_io=read_io, container=container)