@@ -79,6 +79,20 @@ def _load_passive_session_fixtures(session_path: str) -> dict:
7979 return fixture
8080
8181
82+ def _load_task_protocol (session_path : str ) -> str :
83+ """Find the IBL rig version used for the session
84+
85+ :param session_path: the path to a session
86+ :type session_path: str
87+ :return: ibl rig task protocol version
88+ :rtype: str
89+ """
90+ settings = rawio .load_settings (session_path )
91+ ses_ver = settings ["IBLRIG_VERSION_TAG" ]
92+
93+ return ses_ver
94+
95+
8296def _load_passive_stim_meta () -> dict :
8397 """load_passive_stim_meta Loads the passive protocol metadata
8498
@@ -321,38 +335,72 @@ def _extract_passiveValve_intervals(bpod: dict) -> np.array:
321335
322336 # check all values are within bpod tolerance of 100µs
323337 assert np .allclose (
324- valveOff_times - valveOn_times , valveOff_times [0 ] - valveOn_times [0 ], atol = 0.0001
338+ valveOff_times - valveOn_times , valveOff_times [1 ] - valveOn_times [1 ], atol = 0.0001
325339 ), "Some valve outputs are longer or shorter than others"
326340
327341 return np .array ([(x , y ) for x , y in zip (valveOn_times , valveOff_times )])
328342
329343
330- def _extract_passiveAudio_intervals (audio : dict ) -> Tuple [np .array , np .array ]:
331- # Get Tone and Noise cue intervals
344+ def _extract_passiveAudio_intervals (audio : dict , rig_version : str ) -> Tuple [np .array , np .array ]:
345+
346+ # make an exception for task version = 6.2.5 where things are strange but data is recoverable
347+ if rig_version == '6.2.5' :
348+ # Get all sound onsets and offsets
349+ soundOn_times = audio ["times" ][audio ["polarities" ] > 0 ]
350+ soundOff_times = audio ["times" ][audio ["polarities" ] < 0 ]
332351
333- # Get all sound onsets and offsets
334- soundOn_times = audio [ "times" ][ audio [ "polarities" ] > 0 ]
335- soundOff_times = audio [ "times" ][ audio [ "polarities" ] < 0 ]
336- # Check they are the correct number
337- assert len ( soundOn_times ) == NTONES + NNOISES , "Wrong number of sound ONSETS"
338- assert len ( soundOff_times ) == NTONES + NNOISES , "Wrong number of sound OFFSETS"
352+ # Have a couple that are wayyy too long!
353+ time_threshold = 10
354+ diff = soundOff_times - soundOn_times
355+ stupid = np . where ( diff > time_threshold )[ 0 ]
356+ NREMOVE = len ( stupid )
357+ not_stupid = np . where ( diff < time_threshold )[ 0 ]
339358
340- diff = soundOff_times - soundOn_times
341- # Tone is ~100ms so check if diff < 0.3
342- toneOn_times = soundOn_times [diff <= 0.3 ]
343- toneOff_times = soundOff_times [diff <= 0.3 ]
344- # Noise is ~500ms so check if diff > 0.3
345- noiseOn_times = soundOn_times [diff > 0.3 ]
346- noiseOff_times = soundOff_times [diff > 0.3 ]
359+ assert len (soundOn_times ) == NTONES + NNOISES - NREMOVE , "Wrong number of sound ONSETS"
360+ assert len (soundOff_times ) == NTONES + NNOISES - NREMOVE , "Wrong number of sound OFFSETS"
347361
348- assert len (toneOn_times ) == NTONES
349- assert len (toneOff_times ) == NTONES
350- assert len (noiseOn_times ) == NNOISES
351- assert len (noiseOff_times ) == NNOISES
362+ soundOn_times = soundOn_times [not_stupid ]
363+ soundOff_times = soundOff_times [not_stupid ]
352364
353- # Fixed delays from soundcard ~500µs
354- np .allclose (toneOff_times - toneOn_times , 0.1 , atol = 0.0006 )
355- np .allclose (noiseOff_times - noiseOn_times , 0.5 , atol = 0.0006 )
365+ diff = soundOff_times - soundOn_times
366+ # Tone is ~100ms so check if diff < 0.3
367+ toneOn_times = soundOn_times [diff <= 0.3 ]
368+ toneOff_times = soundOff_times [diff <= 0.3 ]
369+ # Noise is ~500ms so check if diff > 0.3
370+ noiseOn_times = soundOn_times [diff > 0.3 ]
371+ noiseOff_times = soundOff_times [diff > 0.3 ]
372+
373+ # append with nans
374+ toneOn_times = np .r_ [toneOn_times , np .full ((NTONES - len (toneOn_times )), np .NAN )]
375+ toneOff_times = np .r_ [toneOff_times , np .full ((NTONES - len (toneOff_times )), np .NAN )]
376+ noiseOn_times = np .r_ [noiseOn_times , np .full ((NNOISES - len (noiseOn_times )), np .NAN )]
377+ noiseOff_times = np .r_ [noiseOff_times , np .full ((NNOISES - len (noiseOff_times )), np .NAN )]
378+
379+ else :
380+ # Get all sound onsets and offsets
381+ soundOn_times = audio ["times" ][audio ["polarities" ] > 0 ]
382+ soundOff_times = audio ["times" ][audio ["polarities" ] < 0 ]
383+
384+ # Check they are the correct number
385+ assert len (soundOn_times ) == NTONES + NNOISES , "Wrong number of sound ONSETS"
386+ assert len (soundOff_times ) == NTONES + NNOISES , "Wrong number of sound OFFSETS"
387+
388+ diff = soundOff_times - soundOn_times
389+ # Tone is ~100ms so check if diff < 0.3
390+ toneOn_times = soundOn_times [diff <= 0.3 ]
391+ toneOff_times = soundOff_times [diff <= 0.3 ]
392+ # Noise is ~500ms so check if diff > 0.3
393+ noiseOn_times = soundOn_times [diff > 0.3 ]
394+ noiseOff_times = soundOff_times [diff > 0.3 ]
395+
396+ assert len (toneOn_times ) == NTONES
397+ assert len (toneOff_times ) == NTONES
398+ assert len (noiseOn_times ) == NNOISES
399+ assert len (noiseOff_times ) == NNOISES
400+
401+ # Fixed delays from soundcard ~500µs
402+ np .allclose (toneOff_times - toneOn_times , 0.1 , atol = 0.0006 )
403+ np .allclose (noiseOff_times - noiseOn_times , 0.5 , atol = 0.0006 )
356404
357405 passiveTone_intervals = np .append (
358406 toneOn_times .reshape ((len (toneOn_times ), 1 )),
@@ -444,8 +492,10 @@ def extract_task_replay(
444492 bpod = ephys_fpga .get_sync_fronts (sync , sync_map ["bpod" ], tmin = treplay [0 ])
445493 passiveValve_intervals = _extract_passiveValve_intervals (bpod )
446494
495+ task_version = _load_task_protocol (session_path )
447496 audio = ephys_fpga .get_sync_fronts (sync , sync_map ["audio" ], tmin = treplay [0 ])
448- passiveTone_intervals , passiveNoise_intervals = _extract_passiveAudio_intervals (audio )
497+ passiveTone_intervals , passiveNoise_intervals = _extract_passiveAudio_intervals (audio ,
498+ task_version )
449499
450500 passiveStims_df = np .concatenate (
451501 [passiveValve_intervals , passiveTone_intervals , passiveNoise_intervals ], axis = 1
@@ -493,8 +543,10 @@ def extract_replay_debug(
493543 passiveValve_intervals = _extract_passiveValve_intervals (bpod )
494544 plot_valve_times (passiveValve_intervals , ax = ax )
495545
546+ task_version = _load_task_protocol (session_path )
496547 audio = ephys_fpga .get_sync_fronts (sync , sync_map ["audio" ], tmin = treplay [0 ])
497- passiveTone_intervals , passiveNoise_intervals = _extract_passiveAudio_intervals (audio )
548+ passiveTone_intervals , passiveNoise_intervals = _extract_passiveAudio_intervals (audio ,
549+ task_version )
498550 plot_audio_times (passiveTone_intervals , passiveNoise_intervals , ax = ax )
499551
500552 passiveStims_df = np .concatenate (
0 commit comments