def test_train_and_test_dnn(self):
        self.clear_model()
        self.model_flags.dnn_regressor = 'fullyconnected'
        num_input_channels = 32
        test_brain_data = TestBrainData(
            'input',
            'output',
            self.model_flags.frame_rate,
            final_batch_size=self.model_flags.batch_size,
            pre_context=self.model_flags.pre_context,
            post_context=self.model_flags.post_context,
            repeat_count=1)
        (response,
         speech) = self.create_simulated_eeg_data(num_input_channels,
                                                  mode='train',
                                                  num_output_channels=1)
        test_brain_data.preserve_test_data(response, speech)
        train_dataset = test_brain_data.create_dataset('train')

        dnn_model = decoding.create_brain_model(self.model_flags,
                                                train_dataset)
        (train_results,
         test_results) = decoding.train_and_test(self.model_flags,
                                                 test_brain_data,
                                                 dnn_model,
                                                 epochs=10)
        logging.info('test_train_and_test_dnn training results: %s',
                     train_results)
        logging.info('test_train_and_test_dnn testing results: %s',
                     test_results)
        self.assertGreater(test_results['pearson_correlation_first'], 0.97)
    def test_train_and_test_cca(self):
        self.clear_model()
        self.model_flags.dnn_regressor = 'cca'
        self.model_flags.pre_context = 2
        self.model_flags.post_context = 3
        self.model_flags.input2_field = 'speech'
        test_brain_data = TestBrainData(
            'eeg',
            'none',
            self.model_flags.frame_rate,
            final_batch_size=self.model_flags.batch_size,
            pre_context=self.model_flags.pre_context,
            post_context=self.model_flags.post_context,
            in2_fields=self.model_flags.input2_field,
            in2_pre_context=self.model_flags.pre_context,
            in2_post_context=self.model_flags.post_context,
            repeat_count=1)
        num_input_channels = 32
        (response, speech) = self.create_simulated_eeg_data(
            num_input_channels,
            mode='train',
            num_output_channels=1,
            simulated_noise_level=0.0,
            simulated_unattended_gain=0.00)
        test_brain_data.preserve_test_data(response, 0 * response[:, 0:1],
                                           speech)
        train_dataset = test_brain_data.create_dataset('train')

        self.model_flags.cca_dimensions = 4
        cca_model = decoding.create_brain_model(self.model_flags,
                                                train_dataset)
        (train_results,
         test_results) = decoding.train_and_test(self.model_flags,
                                                 test_brain_data, cca_model)
        logging.info('test_train_and_test_cca training results: %s',
                     train_results)
        logging.info('test_train_and_test_cca testing results: %s',
                     test_results)
        # This test was for 3.8 when CCA was summarized with sum, and now it is 0.75
        # because the test was flakey.
        self.assertGreater(abs(test_results['cca_pearson_correlation_first']),
                           0.75)

        # Now test the correlation and LDA training code. The piece below is a
        # separate test, but depends on the setup above, so it is added here.
        self.model_flags.dnn_regressor = 'cca'
        dprime, decoder = decoding.train_lda_model(test_brain_data, cca_model,
                                                   self.model_flags)
        print('train_lda_model got a dprime of', dprime)
        self.assertGreater(dprime, 0.7)  # Conservative, just testing plumbing.
        self.assertIsInstance(decoder, infer_decoder.Decoder)
Ejemplo n.º 3
0
def get_brain_model(test_dataset, my_flags):
  """Creates a brain model object for this regression test.

  Args:
    test_dataset: Some data we can use to set the input sizes.  Not otherwise
      read
    my_flags: A DecodingOptions object that we need for setting the model
      parameters.
  Returns:
    A BrainModel object, ready for training.
  """
  if not isinstance(my_flags, decoding.DecodingOptions):
    raise TypeError('Get_brain_model needs a DecodingOptions object, ' +
                    'not %s.' % type(my_flags))

  test_brain_model = decoding.create_brain_model(my_flags, test_dataset)
  return test_brain_model
    def test_create_brain_model(self):
        test_brain_data = TestBrainData(
            'eeg',
            'none',
            self.fs,
            in2_post_context=3,  # Needed for CCA
        )
        (response,
         speech) = self.create_simulated_eeg_data(2,
                                                  mode='train',
                                                  num_output_channels=1)
        test_brain_data.preserve_test_data(response, speech)
        test_dataset = test_brain_data.create_dataset('train')

        with self.assertRaisesRegex(TypeError,
                                    'Model_flags must be a DecodingOptions'):
            decoding.create_brain_model(42, test_dataset)
        with self.assertRaisesRegex(TypeError,
                                    'input_dataset must be a tf.data'):
            decoding.create_brain_model(self.model_flags, 42)

        with self.subTest(name='fullyconnected'):
            fully_flags = attr.evolve(self.model_flags)
            fully_flags.dnn_regressor = 'fullyconnected'
            self.assertTrue(
                decoding.create_brain_model(fully_flags, test_dataset))
        with self.subTest(name='linear'):
            linear_flags = attr.evolve(self.model_flags)
            linear_flags.dnn_regressor = 'linear'
            self.assertTrue(
                decoding.create_brain_model(linear_flags, test_dataset))
        with self.subTest(name='cca'):
            cca_flags = attr.evolve(self.model_flags)
            cca_flags.dnn_regressor = 'cca'
            self.assertTrue(
                decoding.create_brain_model(cca_flags, test_dataset))