Skip to content

Concatenating recordings causes TypeError: unsupported operand type(s) for %: 'NoneType' and 'int' #3159

Closed

Description

@MarinManuel

When trying to save concatenated files using save_to_zarr, I encountered an Exception:

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'

However, this exception crops up also when trying to work on the object in memory, I don't believe the issue is with save_to_zarr but rather with the concatenation.

You can reproduce the error easily:

> si.__version__
0.101.0rc0
rec1 = si.generate_recording(num_channels=384, sampling_frequency=30000.,
                               durations=[1.0], set_probe=True)
rec2 = si.generate_recording(num_channels=384, sampling_frequency=30000.,
                               durations=[2.0], set_probe=True)
rec = si.concatenate_recordings([rec1, rec2])
rec1.save_to_zarr('rec1.zarr')  # works
rec2.save_to_zarr('rec2.zarr')  # works
rec.save_to_zarr('rec.zarr')  # fails

produces:

TypeError                                 Traceback (most recent call last)
Cell In[5], line 8
      6 rec1.save_to_zarr('rec1.zarr')
      7 rec2.save_to_zarr('rec2.zarr')
----> 8 rec.save_to_zarr('rec.zarr')

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/base.py:1047, in BaseExtractor.save_to_zarr(self, name, folder, overwrite, storage_options, channel_chunk_size, verbose, **save_kwargs)
   1045 save_kwargs["storage_options"] = storage_options
   1046 save_kwargs["channel_chunk_size"] = channel_chunk_size
-> 1047 cached = self._save(format="zarr", verbose=verbose, **save_kwargs)
   1048 cached = read_zarr(zarr_path)
   1050 return cached

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/baserecording.py:562, in BaseRecording._save(self, format, verbose, **save_kwargs)
    560     zarr_path = kwargs.pop("zarr_path")
    561     storage_options = kwargs.pop("storage_options")
--> 562     ZarrRecordingExtractor.write_recording(
    563         self, zarr_path, storage_options, verbose=verbose, **kwargs, **job_kwargs
    564     )
    565     cached = ZarrRecordingExtractor(zarr_path, storage_options)
    567 elif format == "nwb":
    568     # TODO implement a format based on zarr

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/zarrextractors.py:118, in ZarrRecordingExtractor.write_recording(recording, folder_path, storage_options, **kwargs)
    113 @staticmethod
    114 def write_recording(
    115     recording: BaseRecording, folder_path: str | Path, storage_options: dict | None = None, **kwargs
    116 ):
    117     zarr_root = zarr.open(str(folder_path), mode="w", storage_options=storage_options)
--> 118     add_recording_to_zarr_group(recording, zarr_root, **kwargs)

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/zarrextractors.py:382, in add_recording_to_zarr_group(recording, zarr_group, verbose, auto_cast_uint, dtype, **kwargs)
    380 compressor_traces = compressor_by_dataset.get("traces", global_compressor)
    381 filters_traces = filters_by_dataset.get("traces", global_filters)
--> 382 add_traces_to_zarr(
    383     recording=recording,
    384     zarr_group=zarr_group,
    385     dataset_paths=dataset_paths,
    386     compressor=compressor_traces,
    387     filters=filters_traces,
    388     dtype=dtype,
    389     channel_chunk_size=channel_chunk_size,
    390     auto_cast_uint=auto_cast_uint,
    391     verbose=verbose,
    392     **job_kwargs,
    393 )
    395 # save probe
    396 if recording.get_property("contact_vector") is not None:

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/zarrextractors.py:509, in add_traces_to_zarr(recording, zarr_group, dataset_paths, channel_chunk_size, dtype, compressor, filters, verbose, auto_cast_uint, **job_kwargs)
    505 init_args = (recording, zarr_datasets, dtype, cast_unsigned)
    506 executor = ChunkRecordingExecutor(
    507     recording, func, init_func, init_args, verbose=verbose, job_name="write_zarr_recording", **job_kwargs
    508 )
--> 509 executor.run()

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/job_tools.py:405, in ChunkRecordingExecutor.run(self)
    403 worker_ctx = self.init_func(*self.init_args)
    404 for segment_index, frame_start, frame_stop in all_chunks:
--> 405     res = self.func(segment_index, frame_start, frame_stop, worker_ctx)
    406     if self.handle_returns:
    407         returns.append(res)

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/zarrextractors.py:537, in _write_zarr_chunk(segment_index, start_frame, end_frame, worker_ctx)
    534 cast_unsigned = worker_ctx["cast_unsigned"]
    536 # apply function
--> 537 traces = recording.get_traces(
    538     start_frame=start_frame, end_frame=end_frame, segment_index=segment_index, cast_unsigned=cast_unsigned
    539 )
    540 traces = traces.astype(dtype)
    541 zarr_dataset[start_frame:end_frame, :] = traces

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/baserecording.py:339, in BaseRecording.get_traces(self, segment_index, start_frame, end_frame, channel_ids, order, return_scaled, cast_unsigned)
    337 num_samples = rs.get_num_samples()
    338 end_frame = int(min(end_frame, num_samples)) if end_frame is not None else num_samples
--> 339 traces = rs.get_traces(start_frame=start_frame, end_frame=end_frame, channel_indices=channel_indices)
    340 if order is not None:
    341     assert order in ["C", "F"]

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/segmentutils.py:194, in ProxyConcatenateRecordingSegment.get_traces(self, start_frame, end_frame, channel_indices)
    191 seg_start = self.cumsum_length[i]
    192 if i == i0:
    193     # first
--> 194     traces_chunk = rec_seg.get_traces(start_frame - seg_start, None, channel_indices)
    195     all_traces.append(traces_chunk)
    196 elif i == i1:
    197     # last

File /work/pi_mmanuel_uri_edu/src/spikeinterface/src/spikeinterface/core/generate.py:1146, in NoiseGeneratorRecordingSegment.get_traces(self, start_frame, end_frame, channel_indices)
   1139 def get_traces(
   1140     self,
   1141     start_frame: Union[int, None] = None,
   1142     end_frame: Union[int, None] = None,
   1143     channel_indices: Union[List, None] = None,
   1144 ) -> np.ndarray:
   1145     start_frame_within_block = start_frame % self.noise_block_size
-> 1146     end_frame_within_block = end_frame % self.noise_block_size
   1147     num_samples = end_frame - start_frame
   1149     traces = np.empty(shape=(num_samples, self.num_channels), dtype=self.dtype)

TypeError: unsupported operand type(s) for %: 'NoneType' and 'int'```

Activity

  1. added
    bug
    generators
    on Jul 6, 2024
  2. h-mayorquin commented on Jul 8, 2024

    @h-mayorquin
    Contributor

    Mmm I see that @alejoe91 solution on #3161 makes sense but clearly the signature of NoiseGeneratorRecordingSegment is wrong because it can't really take None as a start_frame and stop_frame.

    So maybe we should fix that as well but we need to make a decision I think:

    Thinking about this, this is something that we did not discuss in #1989. Linking a comment to that issue:
    #1989 (comment)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggenerators

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions