diff --git a/src/probeinterface/io.py b/src/probeinterface/io.py index cd855afb..8280cd3d 100644 --- a/src/probeinterface/io.py +++ b/src/probeinterface/io.py @@ -554,17 +554,38 @@ def read_3brain(file: Union[str, Path], mea_pitch: float = 42, electrode_width: -------- probe : Probe object + Notes + ----- + In case of multiple wells, the function will return the probe of the first plate. + """ file = Path(file).absolute() assert file.is_file() h5py = import_safely("h5py") rf = h5py.File(file, "r") + if "3BRecInfo" in rf.keys(): # brw v3.x + # get channel positions + channels = rf["3BRecInfo/3BMeaStreams/Raw/Chs"][:] + rows = channels["Row"] - 1 + cols = channels["Col"] - 1 + else: # brw v4.x + num_channels = None + for key in rf: + if key.startswith("Well_"): + num_channels = len(rf[key]["StoredChIdxs"]) + break + assert num_channels is not None, "No Well found in the file" + + num_channels_x = num_channels_y = int(np.sqrt(num_channels)) + if num_channels_x * num_channels_y != num_channels: + raise RuntimeError( + f"Electrode configuration is not a square. Cannot determine configuration of the MEA " + f"plate with {num_channels} channels" + ) + rows = np.repeat(range(num_channels_x), num_channels_y) + cols = np.tile(range(num_channels_y), num_channels_x) - # get channel positions - channels = rf["3BRecInfo/3BMeaStreams/Raw/Chs"][:] - rows = channels["Row"] - 1 - cols = channels["Col"] - 1 positions = np.vstack((rows, cols)).T * mea_pitch probe = Probe(ndim=2, si_units="um")