Skip to content

Conversation

@rly
Copy link
Contributor

@rly rly commented Jul 24, 2019

Fix #65 and adds generally useful functionality

This adds a Container.__deepcopy__ function which:

  1. resets the parent to None
  2. resets the container_source to None
  3. sets a new object_id
  4. sets modified to True
  5. sets all children's parent to itself

@codecov
Copy link

codecov bot commented Jul 24, 2019

Codecov Report

Merging #108 into dev will increase coverage by 0.13%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #108      +/-   ##
==========================================
+ Coverage   67.04%   67.17%   +0.13%     
==========================================
  Files          24       24              
  Lines        4870     4890      +20     
  Branches     1103     1106       +3     
==========================================
+ Hits         3265     3285      +20     
  Misses       1243     1243              
  Partials      362      362
Impacted Files Coverage Δ
src/hdmf/container.py 90.72% <100%> (+1.97%) ⬆️
src/hdmf/data_utils.py 78% <100%> (+0.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4157770...dc0dcfb. Read the comment docs.

1 similar comment
@codecov
Copy link

codecov bot commented Jul 24, 2019

Codecov Report

Merging #108 into dev will increase coverage by 0.13%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #108      +/-   ##
==========================================
+ Coverage   67.04%   67.17%   +0.13%     
==========================================
  Files          24       24              
  Lines        4870     4890      +20     
  Branches     1103     1106       +3     
==========================================
+ Hits         3265     3285      +20     
  Misses       1243     1243              
  Partials      362      362
Impacted Files Coverage Δ
src/hdmf/container.py 90.72% <100%> (+1.97%) ⬆️
src/hdmf/data_utils.py 78% <100%> (+0.26%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4157770...dc0dcfb. Read the comment docs.

# resolve HDMFDataset after and separately because DataIO can wrap an HDMFDataset
for k, v in cp.__dict__.items():
if isinstance(v, HDMFDataset):
setattr(cp, k, v.dataset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On read v.dataset may be and h5py.Dataset with references. I think the resolution for this would be something like

if len(v) > 0 and isinstance(v[0], Container): 
    setattr(cp, k, [find_copied_container(c) for c in v])
else:
    setattr(cp, k, v.dataset)

where find_copied_container would need to replace the reference with the new copy. Not sure, what would happen if you tried to call deepcopy on, e.g., a single DynamicTable that contains references, as in this case those containers may not have been copied yet by deepcopy.

Also, I'm not sure what happens when the container that is being copied was loaded from a file. In this case you may have h5py.Dataset objects that would need to be replaced as well (or are those all wrapped?).

@francois-ecm
Copy link

Hello,
In the laboratory where I am working (INMED) we are trying to copy a NWB file and modify/add meta-data in the copy, for example to correct a mistake. However it is currently quite hard to do that, and this deepcopy function could be a great help !

@oruebel
Copy link
Contributor

oruebel commented Aug 20, 2019

@francois-ecm thanks for the feedback. I agree, the deepcopy function would be a good way for doing this. We are still working through some cases on the deepcopy functionality. Given other ongoing items I'd expect that it may take a few more weeks for this PR.

For appending to a file PyNWB currently supports: 1) adding new containers (e.g,. TimeSeries), and 2) updating existing fields stored in h5py.Datasets. If that is what you need to do, then the following may be a possible solution for your case in the meantime:

from shutil import copyfile
from pynwb import NWBHDF5IO

# Copy the file
src_file = "path/myfile"
dst_file = "path/mycopy"
copyfile(src_file, dst_file)

# Open the copy and update data
f = NWBHDF5IO(dst_file, 'a')
nf = f.read()
....  # Update dataset or add containers
f.write(nf)

Updating of attributes is currently not yet supported. I'm not sure how hard it would be to make this work, but if that is what you need to do, then I'd suggest to open an issue describing your use-case so that we can discuss this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overwriting file with object already in file creates broken link and failure to write

3 participants