Code Duplication    Length = 40-51 lines in 3 locations

skchem/features/fingerprints.py 3 locations

@@ 159-209 (lines=51) @@
156
        self.use_bond_types = use_bond_types
157
        self.use_chirality = use_chirality
158
159
    def _transform_mol(self, mol):
160
161
        """Private method to transform a skchem molecule.
162
163
        Use `transform` for the public method, which genericizes the argument
164
        to iterables of mols.
165
166
        Args:
167
            mol (skchem.Mol): Molecule to calculate fingerprint for.
168
169
        Returns:
170
            np.array or dict:
171
                Fingerprint as an array (or a dict if sparse).
172
        """
173
174
        if self.as_bits and self.n_feats > 0:
175
176
            fp = GetMorganFingerprintAsBitVect(
177
                mol, self.radius, nBits=self.n_feats,
178
                useFeatures=self.use_features,
179
                useBondTypes=self.use_bond_types,
180
                useChirality=self.use_chirality)
181
182
            res = np.array(0)
183
            ConvertToNumpyArray(fp, res)
184
            res = res.astype(np.uint8)
185
186
        else:
187
188
            if self.n_feats <= 0:
189
190
                res = GetMorganFingerprint(
191
                    mol, self.radius,
192
                    useFeatures=self.use_features,
193
                    useBondTypes=self.use_bond_types,
194
                    useChirality=self.use_chirality)
195
196
                res = res.GetNonzeroElements()
197
                if self.as_bits:
198
                    res = {k: int(v > 0) for k, v in res.items()}
199
200
            else:
201
                res = GetHashedMorganFingerprint(
202
                    mol, self.radius, nBits=self.n_feats,
203
                    useFeatures=self.use_features,
204
                    useBondTypes=self.use_bond_types,
205
                    useChirality=self.use_chirality)
206
207
                res = np.array(list(res))
208
209
        return res
210
211
    @property
212
    def name(self):
@@ 325-371 (lines=47) @@
322
        self.as_bits = as_bits
323
        self.use_chirality = use_chirality
324
325
    def _transform_mol(self, mol):
326
327
        """Private method to transform a skchem molecule.
328
329
        Use transform` for the public method, which genericizes the argument to
330
        iterables of mols.
331
332
        Args:
333
            mol (skchem.Mol): Molecule to calculate fingerprint for.
334
335
        Returns:
336
            np.array or dict:
337
                Fingerprint as an array (or a dict if sparse).
338
        """
339
340
        if self.as_bits and self.n_feats > 0:
341
342
            fp = GetHashedAtomPairFingerprintAsBitVect(
343
                mol, nBits=self.n_feats, minLength=self.min_length,
344
                maxLength=self.max_length, includeChirality=self.use_chirality)
345
346
            res = np.array(0)
347
            ConvertToNumpyArray(fp, res)
348
            res = res.astype(np.uint8)
349
350
        else:
351
352
            if self.n_feats <= 0:
353
354
                res = GetAtomPairFingerprint(
355
                    mol, nBits=self.n_feats, minLength=self.min_length,
356
                    maxLength=self.max_length,
357
                    includeChirality=self.use_chirality)
358
359
                res = res.GetNonzeroElements()
360
                if self.as_bits:
361
                    res = {k: int(v > 0) for k, v in res.items()}
362
363
            else:
364
                res = GetHashedAtomPairFingerprint(
365
                    mol, nBits=self.n_feats, minLength=self.min_length,
366
                    maxLength=self.max_length,
367
                    includeChirality=self.use_chirality)
368
369
                res = np.array(list(res))
370
371
        return res
372
373
    @property
374
    def name(self):
@@ 422-461 (lines=40) @@
419
        super(TopologicalTorsionFeaturizer, self).__init__(n_jobs=n_jobs,
420
                                                           verbose=verbose)
421
422
    def _transform_mol(self, mol):
423
        """ Private method to transform a skchem molecule.
424
        Args:
425
            mol (skchem.Mol): Molecule to calculate fingerprint for.
426
427
        Returns:
428
            np.array or dict:
429
                Fingerprint as an array (or a dict if sparse).
430
        """
431
432
        if self.as_bits and self.n_feats > 0:
433
434
            fp = GetHashedTopologicalTorsionFingerprintAsBitVect(
435
                mol, nBits=self.n_feats, targetSize=self.target_size,
436
                includeChirality=self.use_chirality)
437
438
            res = np.array(0)
439
            ConvertToNumpyArray(fp, res)
440
            res = res.astype(np.uint8)
441
442
        else:
443
444
            if self.n_feats <= 0:
445
446
                res = GetTopologicalTorsionFingerprint(
447
                    mol, nBits=self.n_feats, targetSize=self.target_size,
448
                    includeChirality=self.use_chirality)
449
450
                res = res.GetNonzeroElements()
451
                if self.as_bits:
452
                    res = {k: int(v > 0) for k, v in res.items()}
453
454
            else:
455
                res = GetHashedTopologicalTorsionFingerprint(
456
                    mol, nBits=self.n_feats, targetSize=self.target_size,
457
                    includeChirality=self.use_chirality)
458
459
                res = np.array(list(res))
460
461
        return res
462
463
    @property
464
    def names(self):