def setUp(self):
   super(SpectraPredictorTest, self).setUp()
   self.np_fingerprint_input = np.ones((2, 4096))
   self.np_mol_weight_input = np.reshape(np.array([18., 16.]), (2, 1))
   self.test_data_directory = test_utils.test_dir("testdata/")
   self.temp_dir = tempfile.mkdtemp(dir=absltest.get_default_test_tmpdir())
   self.test_file_short = os.path.join(self.test_data_directory,
                                       "test_2_mend.sdf")
    def setUp(self):
        """Sets up a dataset json for regular, baseline, and all_predicted cases."""
        super(MoleculeEstimatorTest, self).setUp()
        self.test_data_directory = test_utils.test_dir('testdata/')
        record_file = os.path.join(self.test_data_directory,
                                   'test_14_record.gz')

        self.num_eval_examples = parse_sdf_utils.parse_info_file(
            record_file)['num_examples']
        self.temp_dir = tempfile.mkdtemp(
            dir=absltest.get_default_test_tmpdir())
        self.default_dataset_config_file = os.path.join(
            self.temp_dir, 'dataset_config.json')
        self.baseline_dataset_config_file = os.path.join(
            self.temp_dir, 'baseline_dataset_config.json')
        self.all_predicted_dataset_config_file = os.path.join(
            self.temp_dir, 'all_predicted_dataset_config.json')

        dataset_names = [
            ds_constants.SPECTRUM_PREDICTION_TRAIN_KEY,
            ds_constants.SPECTRUM_PREDICTION_TEST_KEY,
            ds_constants.LIBRARY_MATCHING_OBSERVED_KEY,
            ds_constants.LIBRARY_MATCHING_PREDICTED_KEY,
            ds_constants.LIBRARY_MATCHING_QUERY_KEY
        ]

        default_dataset_config = {key: [record_file] for key in dataset_names}
        default_dataset_config[
            ds_constants.TRAINING_SPECTRA_ARRAY_KEY] = os.path.join(
                self.test_data_directory, 'test_14.spectra_library.npy')
        with tf.gfile.Open(self.default_dataset_config_file, 'w') as f:
            json.dump(default_dataset_config, f)

        # Test estimator behavior when predicted set is empty
        baseline_dataset_config = dict([
            (key, [record_file]) if
            key != ds_constants.LIBRARY_MATCHING_PREDICTED_KEY else (key, [])
            for key in dataset_names
        ])
        baseline_dataset_config[
            ds_constants.TRAINING_SPECTRA_ARRAY_KEY] = os.path.join(
                self.test_data_directory, 'test_14.spectra_library.npy')
        with tf.gfile.Open(self.baseline_dataset_config_file, 'w') as f:
            json.dump(baseline_dataset_config, f)

        # Test estimator behavior when observed set is empty
        all_predicted_dataset_config = dict([
            (key, [record_file])
            if key != ds_constants.LIBRARY_MATCHING_OBSERVED_KEY else (key, [])
            for key in dataset_names
        ])
        all_predicted_dataset_config[
            ds_constants.TRAINING_SPECTRA_ARRAY_KEY] = os.path.join(
                self.test_data_directory, 'test_14.spectra_library.npy')
        with tf.gfile.Open(self.all_predicted_dataset_config_file, 'w') as f:
            json.dump(all_predicted_dataset_config, f)
    def setUp(self):
        test_data_directory = test_utils.test_dir('testdata/')
        self.temp_dir = tempfile.mkdtemp(
            dir=absltest.get_default_test_tmpdir())
        test_sdf_file_large = os.path.join(test_data_directory,
                                           'test_14_mend.sdf')
        test_sdf_file_small = os.path.join(test_data_directory,
                                           'test_2_mend.sdf')

        max_atoms = ms_constants.MAX_ATOMS
        self.mol_list_large = parse_sdf_utils.get_sdf_to_mol(
            test_sdf_file_large, max_atoms=max_atoms)
        self.mol_list_small = parse_sdf_utils.get_sdf_to_mol(
            test_sdf_file_small, max_atoms=max_atoms)
        self.inchikey_dict_large = train_test_split_utils.make_inchikey_dict(
            self.mol_list_large)
        self.inchikey_dict_small = train_test_split_utils.make_inchikey_dict(
            self.mol_list_small)
        self.inchikey_list_large = self.inchikey_dict_large.keys()
        self.inchikey_list_small = self.inchikey_dict_small.keys()
예제 #4
0
  def setUp(self):
    super(ParseSdfUtilsTest, self).setUp()
    self.test_data_directory = test_utils.test_dir('testdata/')
    self.test_file_long = os.path.join(self.test_data_directory,
                                       'test_14_mend.sdf')
    self.test_file_short = os.path.join(self.test_data_directory,
                                        'test_2_mend.sdf')
    self.temp_dir = tempfile.mkdtemp(dir=absltest.get_default_test_tmpdir())

    # Expected result for list of molecule dicts
    self.expected_mol_dicts = [{
        fmap_constants.NAME: 'Methane, diazo-',
        fmap_constants.INCHIKEY: 'YXHKONLOYHBTNS-UHFFFAOYSA-N',
        fmap_constants.MOLECULAR_FORMULA: 'CH2N2',
        fmap_constants.SMILES: 'C=[N+]=[N-]',
        'parsed_smiles': [28, 18, 81, 51, 4, 83, 18, 81, 51, 5, 83],
        fmap_constants.SMILES_TOKEN_LIST_LENGTH: 11
    }, {
        fmap_constants.NAME: (
            '(4-(4-Chlorphenyl)-3-morpholino-pyrrol-2-yl)-butenedioic acid,'
            ' dimethyl ester'),
        fmap_constants.INCHIKEY:
            'PNYUDNYAXSEACV-RVDMUPIBSA-N',
        fmap_constants.MOLECULAR_FORMULA:
            'C20H21ClN2O5',
        fmap_constants.SMILES:
            'COC(=O)/C=C(/C(=O)OC)c1[nH]cc(-c2ccc(Cl)cc2)c1N1CCOCC1',
        'parsed_smiles': [
            28, 55, 28, 2, 18, 55, 3, 7, 28, 18, 28, 2, 7, 28, 2, 18, 55, 3, 55,
            28, 3, 84, 9, 81, 85, 40, 83, 84, 84, 2, 5, 84, 10, 84, 84, 84, 2,
            31, 3, 84, 84, 10, 3, 84, 9, 51, 9, 28, 28, 55, 28, 28, 9
        ],
        fmap_constants.SMILES_TOKEN_LIST_LENGTH:
            53,
    }]
    for mol_dict in self.expected_mol_dicts:
      token_arr = mol_dict['parsed_smiles']
      sequence_length = mol_dict[
          fmap_constants.SMILES_TOKEN_LIST_LENGTH]
      mol_dict['parsed_smiles'] = np.pad(
          token_arr, (0, ms_constants.MAX_TOKEN_LIST_LENGTH - sequence_length),
          'constant')

    mol_weights = [42.0217981, 404.1139]
    atom_weights_list = [[12.011, 14.007, 14.007], [
        12.011, 15.999, 12.011, 15.999, 12.011, 12.011, 12.011, 15.999, 15.999,
        12.011, 12.011, 14.007, 12.011, 12.011, 12.011, 12.011, 12.011, 12.011,
        35.453, 12.011, 12.011, 12.011, 14.007, 12.011, 12.011, 15.999, 12.011,
        12.011
    ]]
    atom_ids_list = [[6, 7, 7], [
        6, 8, 6, 8, 6, 6, 6, 8, 8, 6, 6, 7, 6, 6, 6, 6, 6, 6, 17, 6, 6, 6, 7, 6,
        6, 8, 6, 6
    ]]
    adjacency_matrix_list = [
        np.array(
            [
                0., 2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0.,
                2., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0.
            ],
            dtype='int32'),
        np.array(
            [
                0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,
                1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 2.,
                1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 2., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 2., 0., 1., 0., 0., 0.,
                1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 2., 1., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 2., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 1., 0., 0., 0., 0., 0., 4., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 4., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 4., 0., 4., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 4., 0., 4., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 4., 0., 1., 0., 0., 0., 0., 0., 0., 4., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 1., 0., 4., 0., 0., 0., 0., 4., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                4., 0., 4., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 4.,
                0., 4., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 4., 0.,
                1., 4., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 4., 0., 0., 4., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 4., 0., 0., 0., 0., 4., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                4., 0., 0., 4., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 1.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 1., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                1., 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
                0., 0., 0., 0.
            ],
            dtype='int32')
    ]
    mass_spec_peak_locs = [[22, 23, 24, 25, 26, 27, 28, 30, 31, 32], [
        32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50,
        51
    ]]
    mass_spec_peak_intensities = [[
        110, 220, 999, 25, 12, 58, 179, 22, 110, 425
    ], [
        12, 7, 28, 999, 57, 302, 975, 8, 53, 176, 99, 122, 117, 155, 9, 7, 6,
        28, 59
    ]]

    # Special hparams so that expected arrays can be smaller.
    self.hparams = tf.contrib.training.HParams(
        intensity_power=1.0,
        max_atoms=30,
        max_mass_spec_peak_loc=60,
        eval_batch_size=len(self.expected_mol_dicts))

    for i in range(len(self.expected_mol_dicts)):
      self.expected_mol_dicts[i][
          fmap_constants.MOLECULE_WEIGHT] = mol_weights[i]
      self.expected_mol_dicts[i][
          fmap_constants.ATOM_WEIGHTS] = np.pad(
              np.array(atom_weights_list[i]),
              (0, self.hparams.max_atoms - len(atom_weights_list[i])),
              'constant')
      self.expected_mol_dicts[i][fmap_constants.ATOM_IDS] = np.pad(
          np.array(atom_ids_list[i]),
          (0, self.hparams.max_atoms - len(atom_ids_list[i])), 'constant')
      self.expected_mol_dicts[i][fmap_constants.ADJACENCY_MATRIX] = (
          adjacency_matrix_list[i])
      self.expected_mol_dicts[i][fmap_constants.DENSE_MASS_SPEC] = (
          feature_utils.make_dense_mass_spectra(
              mass_spec_peak_locs[i], mass_spec_peak_intensities[i],
              self.hparams.max_mass_spec_peak_loc))