Repository: spikeinterface/spikeinterface
Issue: Tracking `channel_indices` when loading from IBL and removing bad channels

Hi, 

I was following the preprocessing steps on the docs page here: https://spikeinterface.readthedocs.io/en/latest/how_to/analyse_neuropixels.html#preprocess-the-recording

Specifically my code was:

```python
from spikeinterface.preprocessing import highpass_filter, phase_shift, common_reference, whiten, detect_bad_channels
from spikeinterface.extractors import read_cbin_ibl

num_samples = 30000

rec_raw = read_cbin_ibl(bin_file.parent).frame_slice(0, num_samples)
rec_hp = highpass_filter(rec_raw)
bad_channel_ids, channel_labels = si.preprocessing.detect_bad_channels(rec_hp)
rec_rm = rec_hp.remove_channels(bad_channel_ids)
rec_ps = phase_shift(rec_rm)
rec_cr = common_reference(rec_ps,  operator="median", reference="global")
rec = rec_cr
```

When trying to access `rec.get_traces()`, I get the following error:

```python
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[3], line 9
      7 rec_cr = common_reference(rec_ps,  operator="median", reference="global")
      8 rec = rec_cr
----> 9 rec.get_traces().shape

File ~/Documents/PYTHON/int-brain-lab/spikeinterface/src/spikeinterface/core/baserecording.py:278, in BaseRecording.get_traces(self, segment_index, start_frame, end_frame, channel_ids, order, return_scaled, cast_unsigned)
    276 channel_indices = self.ids_to_indices(channel_ids, prefer_slice=True)
    277 rs = self._recording_segments[segment_index]
--> 278 traces = rs.get_traces(start_frame=start_frame, end_frame=end_frame, channel_indices=channel_indices)
    279 if order is not None:
    280     assert order in ["C", "F"]

File ~/Documents/PYTHON/int-brain-lab/spikeinterface/src/spikeinterface/preprocessing/common_reference.py:146, in CommonReferenceRecordingSegment.get_traces(self, start_frame, end_frame, channel_indices)
    144 def get_traces(self, start_frame, end_frame, channel_indices):
    145     # need input trace
--> 146     all_traces = self.parent_recording_segment.get_traces(start_frame, end_frame, slice(None))
    147     all_traces = all_traces.astype(self.dtype)
    148     self.temp = np.zeros((all_traces.shape[0],), dtype=all_traces.dtype)

File ~/Documents/PYTHON/int-brain-lab/spikeinterface/src/spikeinterface/preprocessing/phase_shift.py:98, in PhaseShiftRecordingSegment.get_traces(self, start_frame, end_frame, channel_indices)
     86 # this return a copy with margin  + taper on border always
     87 traces_chunk, left_margin, right_margin = get_chunk_with_margin(
     88     self.parent_recording_segment,
     89     start_frame,
   (...)
     95     window_on_margin=True,
     96 )
---> 98 traces_shift = apply_fshift_sam(traces_chunk, self.sample_shifts[channel_indices], axis=0)
     99 # traces_shift = apply_fshift_ibl(traces_chunk, self.sample_shifts, axis=0)
    101 traces_shift = traces_shift[left_margin:-right_margin, :]

File ~/Documents/PYTHON/int-brain-lab/spikeinterface/src/spikeinterface/preprocessing/phase_shift.py:129, in apply_fshift_sam(sig, sample_shifts, axis)
    127 else:
    128     shifts = omega[np.newaxis, :] * sample_shifts[:, np.newaxis]
--> 129 sig_shift = np.fft.irfft(sig_f * np.exp(-1j * shifts), n=n, axis=axis)
    130 return sig_shift

ValueError: operands could not be broadcast together with shapes (16201,384) (16201,382) 
```

I managed to trace this back to here, where the `channel_indices` recorded after the channel slice should have been propagated back. The method here accepts `channel_indices` but doesn't do anything with them. 

https://github.com/SpikeInterface/spikeinterface/blob/4bae9864c40c3fccb9624094c85b54cbd2230d6e/src/spikeinterface/extractors/cbin_ibl.py#L122-L132

I managed to resolve this by just changing the return to 

```python
return traces[:, channel_indices]
``` 


Changed files:
src/spikeinterface/extractors/cbin_ibl.py

diff --git a/src/spikeinterface/extractors/cbin_ibl.py b/src/spikeinterface/extractors/cbin_ibl.py
index 44983515..8416e213 100644
--- a/src/spikeinterface/extractors/cbin_ibl.py
+++ b/src/spikeinterface/extractors/cbin_ibl.py
@@ -124,12 +124,14 @@ class CBinIblRecordingSegment(BaseRecordingSegment):
             start_frame = 0
         if end_frame is None:
             end_frame = self.get_num_samples()
+        if channel_indices is None:
+            channel_indices = slice(None)
 
         traces = self._cbuffer[start_frame:end_frame]
         if not self._load_sync_channel:
             traces = traces[:, :-1]
 
-        return traces
+        return traces[:, channel_indices]
 
 
 read_cbin_ibl = define_function_from_class(source_class=CompressedBinaryIblExtractor, name="read_cbin_ibl")