def _cochlear_trim_sai_marginals(filename_and_indexes): try: filename, norm_segstart, norm_segend, audio_id, NAP_detail = filename_and_indexes sai_video_filename = '{}_sai_video_{}'.format(filename, NAP_detail) if os.path.isfile('{}.npy'.format(sai_video_filename)): return sai_video_filename if NAP_detail == 'high': try: NAP = utils.csv_to_array(filename + 'cochlear' + NAP_detail) except: NAP = brain.cochlear(filename, stride=1, rate=44100, apply_filter=0, suffix='cochlear' + NAP_detail) if NAP_detail == 'low': try: NAP = utils.csv_to_array(filename + 'cochlear' + NAP_detail) except: NAP = brain.cochlear( filename, stride=IO.NAP_STRIDE, rate=IO.NAP_RATE, apply_filter=0, suffix='cochlear' + NAP_detail ) # Seems to work best, in particular when they are all the same. num_channels = NAP.shape[1] input_segment_width = 2048 sai_params = CreateSAIParams(num_channels=num_channels, input_segment_width=input_segment_width, trigger_window_width=input_segment_width, sai_width=1024) sai = pysai.SAI(sai_params) NAP = utils.trim_right(NAP[np.int(np.rint(NAP.shape[0] * norm_segstart)):np. int(np.rint(NAP.shape[0] * norm_segend))], threshold=.05) sai_video = [ np.copy(sai.RunSegment(input_segment.T)) for input_segment in utils.chunks(NAP, input_segment_width) ] del NAP np.save(sai_video_filename, np.array([sai_rectangles(frame) for frame in sai_video])) return sai_video_filename except: print utils.print_exception( 'Calculation SAI video failed for file {}, NAP detail {}'.format( filename, NAP_detail)) return False
def testInputSegmentWidthIsLargerThanBuffer(self): params = CreateSAIParams(num_channels=2, sai_width=10, input_segment_width=200, trigger_window_width=200) sai = pysai.SAI(params) params.trigger_window_width = params.input_segment_width // 10 self.assertGreater( params.input_segment_width, params.num_triggers_per_frame * params.trigger_window_width) self.assertRaises(AssertionError, sai.Redesign, params)
def testInputSegmentWidthSmallerThanTriggerWindow(self): """Tests small hop between segments.""" num_channels = 1 total_input_samples = 20 period = 5 full_input = CreatePulseTrain(num_channels, total_input_samples, period) num_frames = 4 input_segment_width = total_input_samples // num_frames sai_params = CreateSAIParams(num_channels=num_channels, input_segment_width=input_segment_width, trigger_window_width=total_input_samples, sai_width=15) self.assertLess(sai_params.input_segment_width, sai_params.trigger_window_width) sai_params.future_lags = 0 # Only compute past lags. self.assertGreaterEqual(period, input_segment_width) sai = pysai.SAI(sai_params) for i in range(num_frames): segment = (full_input[:, i * input_segment_width:(i + 1) * input_segment_width]) sai_frame = sai.RunSegment(segment) print("Frame {}\nInput\n{}\nOutput\n{}".format( i, segment, sai_frame)) self.assertNotEqual(np.abs(segment).sum(), 0) # Since the input segment is never all zero, there should always # be a peak at zero lag. sai_channel = sai_frame[0, :] self.assertTrue(HasPeakAt(sai_channel, len(sai_channel) - 1)) if i == 0: # Since the pulse train period is larger than the input segment # size, the first input segment will only see a single impulse, # most of the SAI will be zero. np.testing.assert_allclose(sai_channel[:len(sai_channel) - 1], np.zeros(len(sai_channel) - 1), 1e-9) if i == num_frames - 1: # By the last frame, the SAI's internal buffer will have # accumulated the full input signal, so the resulting image # should contain kPeriod peaks. for j in range(len(sai_channel) - 1, 0, -period): self.assertTrue(HasPeakAt(sai_channel, j))
def testInputWidthDoesntMatchInputSegmentWidth(self): num_channels = 2 input_segment_width = 10 segment = CreatePulseTrain(num_channels, input_segment_width, period=4) sai_width = 20 expected_input_segment_width = input_segment_width - 1 sai_params = CreateSAIParams( num_channels=num_channels, sai_width=sai_width, input_segment_width=expected_input_segment_width, trigger_window_width=sai_width + 1) self.assertNotEqual(sai_params.input_segment_width, input_segment_width) sai = pysai.SAI(sai_params) self.assertRaises(AssertionError, sai.RunSegment, segment)
def testMatchesMatlabOnBinauralData(self): test_name = "binaural_test" input_segment_width = 882 num_channels = 71 # The Matlab CARFAC output is transposed compared to the C++. input_segment = LoadMatrix(test_name + "-matlab-nap1.txt", input_segment_width, num_channels).transpose() sai_params = CreateSAIParams(num_channels=num_channels, input_segment_width=input_segment_width, trigger_window_width=input_segment_width, sai_width=500) sai = pysai.SAI(sai_params) sai_frame = sai.RunSegment(input_segment) expected_sai_frame = LoadMatrix(test_name + "-matlab-sai1.txt", num_channels, sai_params.sai_width) np.testing.assert_allclose(expected_sai_frame, sai_frame, rtol=1e-5) WriteMatrix(test_name + "-py-sai1.txt", sai_frame)
def _RunMultiChannelPulseTrainTest(self, period, num_channels): input_segment_width = 38 segment = CreatePulseTrain(num_channels, input_segment_width, period) sai_params = CreateSAIParams(num_channels=num_channels, input_segment_width=input_segment_width, trigger_window_width=input_segment_width, sai_width=15) sai_params.future_lags = 0 # Only compute past lags. sai = pysai.SAI(sai_params) sai_frame = sai.RunSegment(segment) # The output should have peaks at the same positions, regardless of # input phase. for i in range(num_channels): sai_channel = sai_frame[i, :] for j in range(len(sai_channel) - 1, 0, -period): print(i, j, sai_channel, HasPeakAt(sai_channel, j)) self.assertTrue(HasPeakAt(sai_channel, j)) print("Input\n{}\nOutput\n{}".format(segment, sai_frame))