def test_get_bond_is_in_same_ring_one_hot(self): bonds = self.mol.GetBonds() one_hot = get_bond_is_in_same_ring_one_hot(bonds[0]) assert one_hot == [0.0] bonds = self.mol_benzene.GetBonds() one_hot = get_bond_is_in_same_ring_one_hot(bonds[0]) assert one_hot == [1.0]
def _edge_features(self, mol: RDKitMol, path_atoms: Tuple[int, ...], ring_info) -> np.ndarray: """Computes the edge features for a given pair of nodes. Parameters ---------- mol : : RDKitMol RDKit molecule instance. path_atoms: tuple Shortest path between the given pair of nodes. ring_info: list Different rings that contain the pair of atoms """ features = [] path_bonds = [] path_length = len(path_atoms) for path_idx in range(path_length - 1): bond = mol.GetBondBetweenAtoms(path_atoms[path_idx], path_atoms[path_idx + 1]) if bond is None: import warnings warnings.warn('Valid idx of bonds must be passed') path_bonds.append(bond) for path_idx in range(self.max_length): if path_idx < len(path_bonds): bond_type = get_bond_type_one_hot(path_bonds[path_idx]) conjugacy = get_bond_is_conjugated_one_hot( path_bonds[path_idx]) ring_attach = get_bond_is_in_same_ring_one_hot( path_bonds[path_idx]) features.append( np.concatenate([bond_type, conjugacy, ring_attach])) else: features.append(np.zeros(6)) if path_length + 1 > self.max_length: path_length = self.max_length + 1 position_feature = np.zeros(self.max_length + 2) position_feature[path_length] = 1 features.append(position_feature) if ring_info: rfeat = [ one_hot_encode(r, allowable_set=self.RING_TYPES) for r in ring_info ] # The 1.0 float value represents True Boolean rfeat = [1.0] + np.any(rfeat, axis=0).tolist() features.append(rfeat) else: # This will return a boolean vector with all entries False features.append( [0.0] + one_hot_encode(ring_info, allowable_set=self.RING_TYPES)) return np.concatenate(features, axis=0)
def _construct_bond_feature(bond: RDKitBond) -> np.ndarray: """Construct a bond feature from a RDKit bond object. Parameters --------- bond: rdkit.Chem.rdchem.Bond RDKit bond object Returns ------- np.ndarray A one-hot vector of the bond feature. """ bond_type = get_bond_type_one_hot(bond) same_ring = get_bond_is_in_same_ring_one_hot(bond) conjugated = get_bond_is_conjugated_one_hot(bond) stereo = get_bond_stereo_one_hot(bond) return np.concatenate([bond_type, same_ring, conjugated, stereo])
def _construct_bond_feature(bond: RDKitBond) -> List[float]: """Construct a bond feature from a RDKit bond object. Parameters --------- bond: rdkit.Chem.rdchem.Bond RDKit bond object Returns ------- List[float] A one-hot vector of the bond feature. """ bond_type = get_bond_type_one_hot(bond) same_ring = get_bond_is_in_same_ring_one_hot(bond) conjugated = get_bond_is_conjugated_one_hot(bond) stereo = get_bond_stereo_one_hot(bond) return bond_type + same_ring + conjugated + stereo