Skip to content

long_field_names option when writing neo blocks in .mat files #1636

Closed
@TommasoLambresa

Description

@TommasoLambresa

Describe the bug
We're trying to load an Intan .rhs file using neo.io.IntanIO module and then writing it into a .mat file using neo.io.NeoMatlabIO.
When doing this, the editor raises "ValueError: Field names are restricted to 31 characters" relating to the scipy.io.savemat function. This is because the write method within NeoMatlabIO object doesn't pass any additional argument than the block to the write_block method, even if the function accepts **kargs which remains unused within it.
The issue could be easily solved by adding the argument long_field_names=True as input to scipy.io.savemat function, which expands to 64 the maximum number of characters for a field.

To Reproduce
The code that we are using is:

from neo import Block
from neo.io import IntanIO, NeoMatlabIO

r = IntanIO(filename='my/rhs/file')
w = NeoMatlabIO(filename='convertedfile.mat')
blocks = r.read()
w.write(blocks[0]) 

the analogsig object within the block contains an annotation called desired_impedance_test_frequency which has 32 characters, causing the issue.

Expected behaviour
We propose a minimal modification to the write_block method within the NeoMatlabIO class:

  • adding the **kargs as input to scipy.io.savemat: this won't cause any issue because it remains unused within the code and will allow to pass any argument accepted by the savemat function directly in the write method at the begininng, that in our case is long_field_names=True.

We have tested and with this modification all works as expected.
A brief description of the proposed changes to enhance clarity:

class NeoMatlabIO(BaseIO):

...

    def write_block(self, bl, **kargs):
        """
        Arguments:
            bl: the block to be saved
        """
        import scipy.io

       ...

        scipy.io.savemat(self.filename, {"block": bl_struct}, oned_as="row", **kargs)

this reflected in our code in:

from neo import Block
from neo.io import IntanIO, NeoMatlabIO

r = IntanIO(filename='my/rhs/file')
w = NeoMatlabIO(filename='convertedfile.mat')
blocks = r.read()
w.write(blocks[0], long_field_names = True) 

Environment:

  • OS: Windows 11 Pro
  • Python 3.10.14
  • Neo 0.14.0
  • NumPy 2.2.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions