@@ -829,6 +829,7 @@ def interpolate_bads(
829829 origin = "auto" ,
830830 method = None ,
831831 exclude = (),
832+ on_bad_position = "warn" ,
832833 verbose = None ,
833834 ):
834835 """Interpolate bad MEG and EEG channels.
@@ -874,6 +875,12 @@ def interpolate_bads(
874875 exclude : list | tuple
875876 The channels to exclude from interpolation. If excluded a bad
876877 channel will stay in bads.
878+ on_bad_position : "raise" | "warn" | "ignore"
879+ What to do when one or more sensor positions are invalid (zero or NaN).
880+ If ``"warn"`` or ``"ignore"``, channels with invalid positions will be
881+ filled with :data:`~numpy.nan`.
882+
883+ .. versionadded:: 1.12
877884 %(verbose)s
878885
879886 Returns
@@ -898,6 +905,32 @@ def interpolate_bads(
898905
899906 _check_preload (self , "interpolation" )
900907 _validate_type (method , (dict , str , None ), "method" )
908+
909+ # check for channels with invalid position(s)
910+ invalid_chs = []
911+ for ch in self .info ["bads" ]:
912+ loc = self .info ["chs" ][self .ch_names .index (ch )]["loc" ][:3 ]
913+ if np .allclose (loc , 0.0 , rtol = 0 , atol = 1e-16 ) or np .isnan (loc ).any ():
914+ invalid_chs .append (ch )
915+
916+ if invalid_chs :
917+ if on_bad_position == "raise" :
918+ msg = (
919+ f"Channel(s) { invalid_chs } have invalid sensor position(s). "
920+ "Interpolation cannot proceed correctly. If you want to "
921+ "continue despite missing positions, set "
922+ "on_bad_position='warn' or 'ignore', which outputs all "
923+ "NaN values (np.nan) for the interpolated channel(s)."
924+ )
925+ else :
926+ msg = (
927+ f"Channel(s) { invalid_chs } have invalid sensor position(s) "
928+ "and cannot be interpolated. The values of these channels "
929+ "will be all NaN. To ignore this warning, pass "
930+ "on_bad_position='ignore'."
931+ )
932+ _on_missing (on_bad_position , msg )
933+
901934 method = _handle_default ("interpolation_method" , method )
902935 ch_types = self .get_channel_types (unique = True )
903936 # figure out if we have "mag" for "meg", "hbo" for "fnirs", ... to filter the
0 commit comments