Ejemplo n.º 1
0
    def test_brain_trial(self):
        trial_name = 'subj01_1ksamples.wav'
        trial = ingest.BrainTrial(trial_name)
        self.assertEqual(trial.trial_name, trial_name.split('.')[0])

        trial.load_sound(trial_name, sound_dir=self._test_dir)
        brain_data_file_object = ingest.EdfBrainDataFile('sample.edf')
        trial.load_brain_data(self._test_dir, brain_data_file_object)

        summary = trial.summary_string()
        self.assertIn('103 EEG channels', summary)
        self.assertIn('with 66s of eeg data', summary)
        self.assertIn('1.00006s of audio data', summary)

        channels = [c for c in trial.iterate_brain_channels()]
        self.assertLen(channels, 103)
        self.assertIn('TRIG', [c.name for c in channels])

        trial.compute_intensity()
        trial.compute_spectrogram()
        scalp_channel_names = (
            'Fp1, Fp2, F3, F4, F7, F8, C3, C4, T7, T8, P3, P4, '
            'P7, P8, O1, O2').split(', ')
        trial.assemble_brain_data(scalp_channel_names)

        tf_dir = tempfile.mkdtemp()
        tf_file = trial.write_data_as_tfrecords(tf_dir)
        count = ingest.count_tfrecords(tf_file)
        print('Found tfrecords:', count)
        feature_dict = ingest.discover_feature_shapes(tf_file)
        print('Feature dict is:', feature_dict)
        self.assertIn('spectrogram', feature_dict)
        self.assertIn('loudness', feature_dict)
        self.assertIn('eeg', feature_dict)
        self.assertEqual(feature_dict['eeg'].shape, [
            len(scalp_channel_names),
        ])

        (count, errors) = ingest.count_tfrecords(tf_file)
        print('count_tfrecords:', count, errors)
        self.assertEqual(count, 64)
        self.assertEqual(errors, 0)
Ejemplo n.º 2
0
    def test_brain_memory_experiment2(self):
        fs = 16000
        audio_len = fs
        audio_data = np.random.randn(audio_len)

        frame_sr = 100
        channel_one = np.arange(2 * frame_sr)  # Use ints for easier debugging
        channel_two = np.arange(2 * frame_sr) + 200
        eeg_data = {'C1': channel_one, 'C2': channel_two}
        df = ingest.MemoryBrainDataFile(eeg_data, frame_sr)

        trial_two_name = 'trial_2'
        experiment_dict = {
            trial_two_name: [{
                'audio_data': audio_data,
                'audio_sr': fs
            }, df],
        }
        experiment = ingest.BrainExperiment(experiment_dict,
                                            self._test_dir,
                                            self._test_dir,
                                            frame_rate=frame_sr)
        self.assertTrue(experiment)
        experiment.load_all_data(self._test_dir, self._test_dir)
        summary = experiment.summary()
        self.assertIn('Found 1 trials', summary)
        self.assertIn('Trial trial_2: 2 EEG channels with 2s of eeg data',
                      summary)
        for trial in experiment.iterate_trials():
            trial.compute_intensity()
            trial.fix_eeg_offset(1.0)
            trial.assemble_brain_data(list(eeg_data.keys()))
            # Master copy of EEG data has moved from brain_data to audio_features dict
            brain_data = trial.audio_features['eeg']
            # Now the eeg size is shorter, due to fix_eeg_offset above.
            self.assertEqual(brain_data.shape, (frame_sr, 2))
        tmp_dir = '/tmp'
        experiment.write_all_data(tmp_dir)
        tf_file = os.path.join(tmp_dir, trial_two_name + '.tfrecords')

        (count, error) = ingest.count_tfrecords(tf_file)
        self.assertEqual(error, 0)
        self.assertEqual(count, frame_sr)

        file_data = ingest.read_tfrecords(tf_file)
        print('Read in data and found keys:', list(file_data.keys()))
        self.assertIn('eeg', file_data)
        self.assertIn('loudness', file_data)

        np.testing.assert_allclose(
            file_data['eeg'],
            np.hstack((np.reshape(channel_one[frame_sr:], (-1, 1)),
                       np.reshape(channel_two[frame_sr:], (-1, 1)))))
    def test_brain_trial(self):
        trial_name = 'meg/subj01_1ksamples.wav'
        trial = ingest.BrainTrial(trial_name)
        self.assertEqual(trial.trial_name, trial_name.split('.')[0])

        trial.load_sound(trial_name, sound_dir=self._test_dir)
        brain_data_file_object = ingest.EdfBrainDataFile('sample.edf')
        trial.load_brain_data(self._test_dir, brain_data_file_object)

        print(trial.summary_string)
        summary = trial.summary_string()
        self.assertIn('103 EEG channels', summary)
        self.assertIn('with 66s of eeg data', summary)
        self.assertIn('1.00006s of audio data', summary)

        channels = [c for c in trial.iterate_brain_channels()]
        self.assertLen(channels, 103)
        self.assertIn('TRIG', [c.name for c in channels])

        scalp_channel_names = (
            'TRIG, Fp2, F3, F4, F7, F8, C3, C4, T7, T8, P3, P4, '
            'P7, P8, O1, O2').split(', ')
        self.assertNotIn('eeg', trial.model_features)
        trial.assemble_brain_data(scalp_channel_names)
        self.assertIn('eeg', trial.model_features)
        self.assertLen(trial.model_features['eeg'][0, :],
                       len(scalp_channel_names))
        print('trial.model_features channel 0:',
              trial.model_features['eeg'][:100, 0])
        print('trial.model_features channel last:',
              trial.model_features['eeg'][:100, -1])

        tf_dir = tempfile.mkdtemp()
        tf.io.gfile.makedirs(os.path.join(tf_dir, 'meg'))
        tf_file = trial.write_data_as_tfrecords(tf_dir)

        feature_dict = ingest.discover_feature_shapes(tf_file)
        print('Feature dict is:', feature_dict)
        self.assertIn('eeg', feature_dict)
        self.assertEqual(feature_dict['eeg'].shape, [
            len(scalp_channel_names),
        ])

        (_, errors) = ingest.count_tfrecords(tf_file)
        self.assertEqual(errors, 0)

        with self.assertRaises(ValueError):
            trial.assemble_brain_data('TRIG, FOO, BAR')
        with self.assertRaises(ValueError):
            trial.assemble_brain_data(['TRIG', 'TRIG', 'F3'])
    def test_brain_memory_experiment(self):
        fs = 16000
        audio_len = 2 * fs
        audio_data = np.random.randn(audio_len, 1)

        frame_sr = 100
        eeg_len = 2 * frame_sr
        channel_one = np.arange(eeg_len)  # Use ints for easier debugging
        channel_two = np.arange(eeg_len) + 200
        eeg_data = collections.OrderedDict(
            (('C1', channel_one), ('C2', channel_two)))
        df = ingest.MemoryBrainDataFile(eeg_data, frame_sr)

        trial_two_name = 'trial_2'
        experiment_dict = {
            trial_two_name: [{
                'audio_data': audio_data,
                'audio_sr': fs
            }, df],
        }
        experiment = ingest.BrainExperiment(experiment_dict,
                                            self._test_dir,
                                            self._test_dir,
                                            frame_rate=frame_sr)
        self.assertTrue(experiment)
        experiment.load_all_data(self._test_dir, self._test_dir)
        summary = experiment.summary()
        self.assertIn('Found 1 trials', summary)
        self.assertIn('Trial trial_2: 2 EEG channels with 2s of eeg data',
                      summary)
        for trial in experiment.iterate_trials():
            trial.assemble_brain_data(list(eeg_data.keys()))
            # Master copy of EEG data has moved from brain_data to model_features dict
            brain_data = trial.model_features['eeg']
            self.assertEqual(brain_data.shape, (eeg_len, 2))
        tmp_dir = flags.FLAGS.test_tmpdir or '/tmp'
        all_ingested_files = experiment.write_all_data(tmp_dir)
        self.assertLen(all_ingested_files, 1)
        tf_file = os.path.join(tmp_dir, trial_two_name + '.tfrecords')

        (_, error) = ingest.count_tfrecords(tf_file)
        self.assertEqual(error, 0)

        file_data = ingest.read_tfrecords(tf_file)
        self.assertIn('eeg', file_data)

        np.testing.assert_allclose(
            file_data['eeg'],
            np.hstack((np.reshape(channel_one[:eeg_len], (-1, 1)),
                       np.reshape(channel_two[:eeg_len], (-1, 1)))))