pynwb icon indicating copy to clipboard operation
pynwb copied to clipboard

Cannot read file after nwbfile.copy() with Units

Open rly opened this issue 4 years ago • 3 comments

Description

When reading a file generated from a shallow copy copy() on an NWBFile with a Units table, I get the error:

  File "c:\users\ryan\documents\nwb\hdmf\src\hdmf\build\objectmapper.py", line 1007, in construct
    override = self.__get_override_carg(argname, builder, manager)
  File "c:\users\ryan\documents\nwb\hdmf\src\hdmf\build\objectmapper.py", line 467, in __get_override_carg
    return func(self, *remaining_args)
  File "c:\users\ryan\documents\nwb\pynwb\src\pynwb\io\misc.py", line 16, in resolution_carg
    return builder['spike_times'].attributes.get('resolution')
AttributeError: 'LinkBuilder' object has no attribute 'attributes'

Steps to Reproduce

from pynwb import NWBFile, NWBHDF5IO
from datetime import datetime
import math


nwbfile = NWBFile(session_description='session_description',
                  identifier='identifier',
                  session_start_time=datetime.now().astimezone())

device = nwbfile.create_device('Device')

electrode_group = nwbfile.create_electrode_group(
    name='ElectrodeGroup',
    description='',
    location='',
    device=device
)

nwbfile.add_electrode(x=math.nan,
                      y=math.nan,
                      z=math.nan,
                      imp=math.nan,
                      location='none',
                      filtering='none',
                      group=electrode_group)

nwbfile.add_unit(electrodes=(0,),
                 spike_times=(1., 2., 3.))

filename = 'nwbfile.nwb'
copy_filename = 'nwbfile_copy.nwb'

with NWBHDF5IO(filename, 'w') as io:
    io.write(nwbfile)

with NWBHDF5IO(filename, 'r') as io:
    read_nwbfile = io.read()
    copy_nwbfile = read_nwbfile.copy()

    with NWBHDF5IO(copy_filename, mode='w', manager=io.manager) as write_io:
        write_io.write(copy_nwbfile)

with NWBHDF5IO(copy_filename, 'r') as io:
    copy_nwbfile = io.read()

Environment

Python Executable: Python
Python Version: Python 3.7
Operating System: Windows
HDMF Version: dev

Checklist

  • [x] Have you ensured the feature or change was not already reported?
  • [x] Have you included a brief and descriptive title?
  • [x] Have you included a clear description of the problem you are trying to solve?
  • [x] Have you included a minimal code snippet that reproduces the issue you are encountering?
  • [x] Have you checked our Contributing document?

rly avatar Apr 15 '20 03:04 rly

Hi @rly, I was wondering if there is a known workaround for this?

I'm running into the same issue when trying to follow a similar process to the tutorial to add analysis data to NWB.

I instead directly append to the original file and that seems to have worked. I was just wondering if there was any suggestion for copying files with units. Just because a new file with extra analysis would probably be preferable to appending to the data file.

Thank you so much for the package and any help you can give! Best - Sean

seankmartin avatar Jun 09 '22 10:06 seankmartin

It would seem I answered my own question, by using the export feature. It appears that this duplicates the raw data, so probably isn't feasible for very large files.

But anyway, for reference, in case it helps someone else; Instead of opening the nwbfile in append mode and then writing back to the same file; Open the nwbfile in read mode, and then create a new NWBHDF5IO in write mode. Then use this new io to export to a new file, like: new_io.export(src_io=read_io, nwbfile=processed_nwbfile)

seankmartin avatar Jun 10 '22 14:06 seankmartin

Hi @rly , I'm also running into the same issue @seankmartin described above. Sean's work around using the export feature seems to work but it kind of defeats the purpose of linking files for scratch data, especially with large datasets. Do you know of any solutions to this issue?

slcalgin avatar Jun 29 '23 19:06 slcalgin