Skip to content

biocam gain calculation bug when using numpy >= 2.0.0. #1883

Description

@kevindoran

Describe the bug
There can be an error in the gain calculation when loading biocam v3 files.
At neo/rawio/biocamrawio.py line 253, there is an expression:

gain = (max_uv - min_uv) / (2**bit_depth)

bit_depth here can be a numpy.uint8. In numpy 2.0 or later, if bit_depth is a numpy.uint8, then 2**bit_depth will also be a numpy.uint8 and 2**bit_depth will overflow for anything higher than 7.

The line could be fixed as:

gain = (max_uv - min_uv) / (2**int(bit_depth))

Or, fix it at the point of reading (~line 216) by casting to int there.

To Reproduce

import numpy as np
import h5py
import neo

path = "minimal_v3.brw"
n_ch = 4
n_frames = 10
with h5py.File(path, "w") as f:
    rv = f.create_group("3BRecInfo/3BRecVars")
    rv.create_dataset("BitDepth",        data=np.array([12], dtype=np.uint8))
    rv.create_dataset("MaxVolt",         data=np.array([4125.0]))
    rv.create_dataset("MinVolt",         data=np.array([-4125.0]))
    rv.create_dataset("NRecFrames",      data=np.array([n_frames], dtype=np.int64))
    rv.create_dataset("SamplingRate",    data=np.array([17852.77]))
    rv.create_dataset("SignalInversion", data=np.array([1], dtype=np.int32))
    f.create_dataset("3BRecInfo/3BMeaStreams/Raw/Chs",
                     data=np.arange(2*n_ch, dtype=np.int32).reshape(n_ch, 2))
    f.create_dataset("3BData/Raw", data=np.zeros(n_ch*n_frames, dtype=np.uint16))
    f["3BData"].attrs["Version"] = 102

r = neo.rawio.BiocamRawIO(filename=path)
r.parse_header()
gain = r.header["signal_channels"]["gain"][0]

print(f"gain: {gain}, expected ~2.01")

Expected behaviour
Gain should not be inf. Gain calculation should not be affected by dtype of bit depth.

Environment:

  • Ubuntu 22.04
  • Python 3.10
  • neo 0.14.5
  • numpy 2.2.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions