molfeat.trans.base
BaseFeaturizer
¶
Bases: BaseEstimator
Molecule featurizer base class that needs to be implemented by all featurizers. This featurizer is compatible with scikit-learn estimators and thus can be plugged into a pipeline
Source code in molfeat/trans/base.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
n_jobs
property
writable
¶
Get the number of concurrent jobs to run with this featurizer
copy()
¶
Return a copy of this object.
Source code in molfeat/trans/base.py
116 117 118 119 120 121 122 |
|
get_collate_fn(*args, **kwargs)
¶
Get collate function of this featurizer. In the implementation of this function you should set the relevant attributes or argument of the underlying collate function (e.g via functools.partial) and return the function itself
Returns:
Name | Type | Description |
---|---|---|
fn |
Collate function for pytorch or None |
Source code in molfeat/trans/base.py
136 137 138 139 140 141 142 143 144 145 |
|
preprocess(inputs, labels=None)
¶
Preprocess input
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
list
|
inputs to preprocess |
required |
labels |
Optional[list]
|
labels to preprocess (optional) |
None
|
Returns:
Name | Type | Description |
---|---|---|
processed |
pre-processed input list |
Source code in molfeat/trans/base.py
124 125 126 127 128 129 130 131 132 133 134 |
|
set_params(**params)
¶
Set the parameters of this estimator.
Returns:
Name | Type | Description |
---|---|---|
self |
estimator instance |
Source code in molfeat/trans/base.py
103 104 105 106 107 108 109 110 111 112 113 114 |
|
MoleculeTransformer
¶
Bases: TransformerMixin
, BaseFeaturizer
Base class for molecular data transformer such as Fingerprinter etc. If you create a subclass of this featurizer, you will need to make sure that the input argument of the init are kept as is in the object attributes.
Note
The transformer supports a variety of datatype, they are only enforced when passing the
enforce_dtype=True
attributes in __call__
. For pandas dataframes, use 'pandas'|'df'|'dataframe'|pd.DataFrame
Using a custom Calculator
You can use your own calculator for featurization. It's recommended to subclass molfeat.calc.base.SerializableCalculator
If you calculator also implements a batch_compute
method, it will be used for batch featurization and parallelization options will be passed to it.
Source code in molfeat/trans/base.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
|
columns
property
¶
Get the list of columns for this molecular descriptor
Returns:
Name | Type | Description |
---|---|---|
columns |
list
|
Name of the columns of the descriptor |
__call__(mols, enforce_dtype=True, ignore_errors=False, **kwargs)
¶
Calculate features for molecules. Using call, instead of transform. If ignore_error is True, a list of features and valid ids are returned. Note that most Transfomers allow you to specify a return datatype.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mols |
List[Union[Mol, str]]
|
Mol or SMILES of the molecules to be transformed |
required |
enforce_dtype |
bool
|
whether to enforce the instance dtype in the generated fingerprint |
True
|
ignore_errors |
bool
|
Whether to ignore errors during featurization or raise an error. |
False
|
kwargs |
Named parameters for the transform method |
{}
|
Returns:
Name | Type | Description |
---|---|---|
feats |
list of valid features |
|
ids |
all valid molecule positions that did not failed during featurization. Only returned when ignore_errors is True. |
Source code in molfeat/trans/base.py
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
|
__init__(featurizer, n_jobs=1, verbose=False, dtype=None, parallel_kwargs=None, **params)
¶
Mol transformer base class
Parameters:
Name | Type | Description | Default |
---|---|---|---|
featurizer |
Union[str, Callable]
|
featurizer to use |
required |
n_jobs |
int
|
Number of job to run in parallel. Defaults to 1. |
1
|
verbose |
bool
|
Verbosity level. Defaults to True. |
False
|
dtype |
callable
|
Output data type. Defaults to None, where numpy arrays are returned. |
None
|
parallel_kwargs |
dict
|
Optional kwargs to pass to the dm.parallelized function. Defaults to None. |
None
|
Source code in molfeat/trans/base.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
__len__()
¶
Compute featurizer length
Source code in molfeat/trans/base.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
|
batch_transform(transformer, mols, batch_size=256, n_jobs=None, concatenate=True, progress=True, leave_progress=False, **parallel_kwargs)
staticmethod
¶
Batched computation of featurization of a list of molecules
Parameters:
Name | Type | Description | Default |
---|---|---|---|
transformer |
Callable
|
Fingerprint transformer |
required |
mols |
List[Union[Mol, str]]
|
List of molecules to featurize |
required |
batch_size |
int
|
Batch size |
256
|
n_jobs |
Optional[int]
|
number of jobs to run in parallel |
None
|
concatenate |
bool
|
Whether to concatenate the results or return the list of batched results |
True
|
progress |
bool
|
whether to show progress bar |
True
|
leave_progress |
bool
|
whether to leave progress bar after completion |
False
|
parallel_kwargs |
additional arguments to pass to dm.parallelized |
{}
|
Returns:
Type | Description |
---|---|
List of batches |
Source code in molfeat/trans/base.py
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
|
fit(X, y=None, **fit_params)
¶
Fit the current transformer on given dataset.
The goal of fitting is for example to identify nan columns values that needs to be removed from the dataset
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X |
List[Union[Mol, str]]
|
input list of molecules |
required |
y |
list
|
Optional list of molecular properties. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
self |
MolTransformer instance after fitting |
Source code in molfeat/trans/base.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
|
from_state_dict(state, override_args=None)
staticmethod
¶
Reload a featurizer from a state dict.
Source code in molfeat/trans/base.py
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
|
to_state_dict()
¶
Serialize the featurizer to a state dict.
Source code in molfeat/trans/base.py
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
|
transform(mols, ignore_errors=False, **kwargs)
¶
Compute the features for a set of molecules.
Note
Note that depending on the ignore_errors
argument, all failed
featurization (caused whether by invalid smiles or error during
data transformation) will be substitued by None features for the
corresponding molecule. This is done, so you can find the positions
of these molecules and filter them out according to your own logic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mols |
List[Union[Mol, str]]
|
a list containing smiles or mol objects |
required |
ignore_errors |
bool
|
Whether to silently ignore errors |
False
|
Returns:
Name | Type | Description |
---|---|---|
features |
a list of features for each molecule in the input set |
Source code in molfeat/trans/base.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
PrecomputedMolTransformer
¶
Bases: MoleculeTransformer
Convenience class for storing precomputed features.
Source code in molfeat/trans/base.py
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
|
__getstate__()
¶
Get the state for pickling
Source code in molfeat/trans/base.py
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 |
|
__init__(cache=None, cache_dict=None, cache_key=None, *args, featurizer=None, state_path=None, **kwargs)
¶
Transformer that supports precomputation of features. You can either pass an empty cache or a pre-populated cache
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache |
Optional[Union[_Cache, Mapping[Any, Any], CacheList]]
|
a datastructure of type mapping that maps each molecule to the precomputed features |
None
|
cache_dict |
Optional[Dict[str, Union[_Cache, Mapping[Any, Any], CacheList]]]
|
A dictionary of cache objects. This is a convenient structure when use multiple datacache for model selection. |
None
|
cache_key |
Optional[str]
|
The key of cache object to use. |
None
|
featurizer |
Optional[Union[MoleculeTransformer, str]]
|
optional featurizer used to compute the features of values not in the cache. Either the featurizer object or a string. |
None
|
state_path |
Optional[str]
|
optional state file path used to initiate the transformer object at the initialization |
None
|
Source code in molfeat/trans/base.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 |
|
to_state_dict(save_to_file=True)
¶
Serialize a PrecomputedMolTransformer object to a state dict.
Notes
- The base_featurizer must be set or a ValueError will be raised.
- The cache must be a FileCache object or a ValueError will be raised.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save_to_file |
bool
|
whether to save the cache to file. |
True
|
Source code in molfeat/trans/base.py
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
|
update(feat_dict)
¶
Fill the cache with new set of features for the molecules in mols.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feat_dict |
Mapping[Any, Any]
|
A dictionary of molecules to features. |
required |
Source code in molfeat/trans/base.py
790 791 792 793 794 795 796 797 |
|