molfeat.utils
Cache¶
CacheList
¶
Proxy for supporting search using a list of cache
Source code in molfeat/utils/cache.py
689 690 691 692 693 694 695 696 697 698 699 700 701 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 |
|
caches = caches
instance-attribute
¶
__call__(*args, **kwargs)
¶
Compute the features for a list of molecules and save them to the cache
Source code in molfeat/utils/cache.py
728 729 730 731 732 733 734 735 |
|
__contains__(key)
¶
Check whether a key is in the cache
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Any
|
key to check in the cache |
required |
Source code in molfeat/utils/cache.py
702 703 704 705 706 707 |
|
__getitem__(key)
¶
Source code in molfeat/utils/cache.py
695 696 697 698 699 700 |
|
__init__(*caches)
¶
Source code in molfeat/utils/cache.py
692 693 |
|
__iter__()
¶
Iterate over all the caches
Source code in molfeat/utils/cache.py
713 714 715 |
|
__len__()
¶
Return the length of the cache
Source code in molfeat/utils/cache.py
709 710 711 |
|
__setitem__(key, item)
¶
Add an item to the cache
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Any
|
input key to set |
required |
item |
Any
|
value of the key to set |
required |
Source code in molfeat/utils/cache.py
717 718 719 720 721 722 723 724 725 726 |
|
clear(*args, **kwargs)
¶
Clear all the caches and make them inaccesible
Source code in molfeat/utils/cache.py
737 738 739 740 |
|
fetch(mols)
¶
Get the representation for a single
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mols |
List[Union[rdchem.Mol, str]]
|
list of molecules |
required |
Source code in molfeat/utils/cache.py
774 775 776 777 778 779 780 781 782 783 784 785 |
|
get(key, default=None)
¶
Get the cached value for a specific key
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
key to get |
required | |
default |
Optional[Any]
|
default value to return when the key is not found |
None
|
Source code in molfeat/utils/cache.py
746 747 748 749 750 751 752 753 754 755 756 |
|
items()
¶
Return iterator of key, values in the cache
Source code in molfeat/utils/cache.py
766 767 768 |
|
keys()
¶
Get list of keys in the cache
Source code in molfeat/utils/cache.py
758 759 760 |
|
to_dict()
¶
Convert current cache to a dictionary
Source code in molfeat/utils/cache.py
770 771 772 |
|
update(new_cache)
¶
Source code in molfeat/utils/cache.py
742 743 744 |
|
values()
¶
Get list of values in the cache
Source code in molfeat/utils/cache.py
762 763 764 |
|
DataCache
¶
Bases: _Cache
Molecular features caching system that cache computed values in memory for reuse later
Source code in molfeat/utils/cache.py
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 |
|
cache = {}
instance-attribute
¶
cache_file = cache_file
instance-attribute
¶
clear_on_exit = clear_on_exit
instance-attribute
¶
delete_on_exit = delete_on_exit
instance-attribute
¶
__init__(name, n_jobs=-1, mol_hasher=None, verbose=False, cache_file=None, delete_on_exit=False, clear_on_exit=True)
¶
Precomputed fingerprint caching callback
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the cache |
required |
n_jobs |
int
|
number of parallel jobs to use when performing any computation |
-1
|
mol_hasher |
Optional[Union[Callable, str, MolToKey]]
|
function to use to hash molecules. If not provided, `dm.unique_id`` is used by default |
None
|
verbose |
Union[bool, int]
|
whether to print progress. Default to False |
False
|
cache_file |
Optional[Union[os.PathLike, bool]]
|
Cache location. Defaults to None, which will use in-memory caching. |
None
|
delete_on_exit |
bool
|
Whether to delete the cache file on exit. Defaults to False. |
False
|
clear_on_exit |
bool
|
Whether to clear the cache on exit of the interpreter. Default to True |
True
|
Source code in molfeat/utils/cache.py
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 |
|
clear(delete=False)
¶
Clear cache memory if needed. Note that a cleared cache cannot be used anymore
Parameters:
Name | Type | Description | Default |
---|---|---|---|
delete |
bool
|
whether to delete the cache file if on disk |
False
|
Source code in molfeat/utils/cache.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
load_from_file(filepath)
classmethod
¶
Load a datache from a file (including remote file)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[os.PathLike, str]
|
path to the file to load |
required |
kwargs |
keyword arguments to pass to the constructor |
required |
Source code in molfeat/utils/cache.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
|
save_to_file(filepath)
¶
Save the cache to a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[os.PathLike, str]
|
path to the file to save |
required |
Source code in molfeat/utils/cache.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
|
update(new_cache)
¶
Update the cache with new values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_cache |
Mapping[Any, Any]
|
new cache with items to use to update current cache |
required |
Source code in molfeat/utils/cache.py
364 365 366 367 368 369 370 371 372 373 |
|
FileCache
¶
Bases: _Cache
Read only cache that holds in precomputed data in a pickle, csv or h5py file.
The convention used requires the 'keys' and 'values' columns when the input file needs to be loaded as a dataframe.
Source code in molfeat/utils/cache.py
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 |
|
SUPPORTED_TYPES = ['pickle', 'pkl', 'csv', 'parquet', 'pq', 'hdf5', 'h5']
class-attribute
¶
cache = {}
instance-attribute
¶
cache_file = cache_file
instance-attribute
¶
clear_on_exit = clear_on_exit
instance-attribute
¶
file_type = file_type
instance-attribute
¶
parquet_kwargs = parquet_kwargs or {}
instance-attribute
¶
__init__(cache_file, name=None, mol_hasher=None, n_jobs=None, verbose=False, file_type='parquet', clear_on_exit=True, parquet_kwargs=None)
¶
Precomputed fingerprint caching callback
Note
Do not pickle this object, instead use the provided saving methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cache_file |
Union[os.PathLike, str]
|
Cache location. Can be a local file or a remote file |
required |
name |
Optional[str]
|
optional name to give the cache |
None
|
mol_hasher |
Optional[Union[Callable, str, MolToKey]]
|
function to use to hash molecules. If not provided, |
None
|
n_jobs |
Optional[int]
|
number of parallel jobs to use when performing any computation |
None
|
verbose |
Union[bool, int]
|
whether to print information about the cache |
False
|
clear_on_exit |
bool
|
whether to clear the cache on exit of the interpreter |
True
|
file_type |
str
|
File type that was provided. One of "csv", "pickle", "hdf5" and "parquet" For "csv" and "parquet", we expect columns "keys" and "values" For a pickle, we expect either a mapping or a dataframe with "keys" and "values" columns |
'parquet'
|
parquet_kwargs |
Optional[Dict[Any, Any]]
|
Argument to pass to the parquet reader. |
None
|
Source code in molfeat/utils/cache.py
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 |
|
clear()
¶
Clear cache memory at exit and close any open file Note that a cleared cache cannot be used anymore !
Source code in molfeat/utils/cache.py
510 511 512 513 514 515 516 517 518 519 |
|
from_state_dict(state, override_args=None)
staticmethod
¶
Source code in molfeat/utils/cache.py
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 |
|
items()
¶
Return iterator of key, values in the cache
Source code in molfeat/utils/cache.py
521 522 523 524 525 |
|
load_from_file(filepath, **kwargs)
classmethod
¶
Load a FileCache from a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[os.PathLike, str]
|
path to the file to load |
required |
kwargs |
keyword arguments to pass to the constructor |
{}
|
Source code in molfeat/utils/cache.py
578 579 580 581 582 583 584 585 586 587 |
|
save_to_file(filepath=None, file_type=None, **kwargs)
¶
Save the cache to a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Optional[Union[os.PathLike, str]]
|
path to the file to save. If None, the cache is saved to the original file. |
None
|
file_type |
Optional[str]
|
format used to save the cache to file one of "pickle", "csv", "hdf5", "parquet". If None, the original file type is used. |
None
|
kwargs |
keyword arguments to pass to the serializer to disk (e.g to pass to pd.to_csv or pd.to_parquet) |
{}
|
Source code in molfeat/utils/cache.py
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 |
|
to_dataframe(pack_bits=False)
¶
Convert the cache to a dataframe. The converted dataframe would have keys
and values
columns
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pack_bits |
bool
|
whether to pack the values columns into bits. By using molfeat.utils.commons.unpack_bits, the values column can be reloaded as an array |
False
|
Source code in molfeat/utils/cache.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
to_state_dict(save_to_file=True)
¶
Serialize the cache to a state dict.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save_to_file |
bool
|
whether to save the cache to file. |
True
|
Source code in molfeat/utils/cache.py
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 |
|
update(new_cache)
¶
Update the cache with new values
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_cache |
Mapping[Any, Any]
|
new cache with items to use to update current cache |
required |
Source code in molfeat/utils/cache.py
564 565 566 567 568 569 570 571 572 573 574 575 576 |
|
MPDataCache
¶
Bases: DataCache
A datacache that supports multiprocessing natively
Source code in molfeat/utils/cache.py
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 |
|
__init__(name=None, n_jobs=-1, mol_hasher=None, verbose=False, clear_on_exit=False)
¶
Multiprocessing datacache that save cache into a shared memory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
Optional[str]
|
name of the cache |
None
|
n_jobs |
int
|
number of parallel jobs to use when performing any computation |
-1
|
mol_hasher |
Optional[Union[Callable, str, MolToKey]]
|
function to use to hash molecules. If not provided, `dm.unique_id`` is used by default |
None
|
verbose |
Union[bool, int]
|
whether to print progress. Default to False |
False
|
clear_on_exit |
bool
|
Whether to clear the cache on exit. Default is False to allow sharing the cache content |
False
|
Source code in molfeat/utils/cache.py
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 |
|
MolToKey
¶
Convert a molecule to a key
Source code in molfeat/utils/cache.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 |
|
SUPPORTED_HASH_FN = {'dm.unique_id': dm.unique_id, 'dm.to_inchikey': dm.to_inchikey}
class-attribute
¶
hash_fn = self.SUPPORTED_HASH_FN[hash_fn]
instance-attribute
¶
hash_name = hash_fn
instance-attribute
¶
__call__(mol)
¶
Convert a molecule object to a key that can be used for the cache system
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
rdchem.Mol
|
input molecule object |
required |
Source code in molfeat/utils/cache.py
69 70 71 72 73 74 75 76 77 78 79 80 |
|
__init__(hash_fn='dm.unique_id')
¶
Init function for molecular key generator.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hash_fn |
Optional[Union[Callable, str]]
|
hash function to use for the molecular key |
'dm.unique_id'
|
Source code in molfeat/utils/cache.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
from_state_dict(state)
staticmethod
¶
Load a MolToKey object from a state dict.
Source code in molfeat/utils/cache.py
95 96 97 98 |
|
to_state_dict()
¶
Serialize MolToKey to a state dict.
Source code in molfeat/utils/cache.py
82 83 84 85 86 87 88 89 90 91 92 93 |
|
Common utils¶
Common utility functions
align_conformers(mols, ref_id=0, copy=True, conformer_id=-1)
¶
Align a list of molecules to a reference molecule.
Note: consider adding me to datamol
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mols |
List[dm.Mol]
|
List of molecules to align. All the molecules must have a conformer. |
required |
ref_id |
int
|
Index of the reference molecule. By default, the first molecule in the list will be used as reference. |
0
|
copy |
bool
|
Whether to copy the molecules before performing the alignement. |
True
|
conformer_id |
int
|
Conformer id to use. |
-1
|
Returns:
Name | Type | Description |
---|---|---|
mols | The aligned molecules. |
|
scores | The score of the alignement. |
Source code in molfeat/utils/commons.py
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 |
|
concat_dict(prop_dict, new_name, order=None)
¶
Concat properties in dict into a single key dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
prop_dict |
dict
|
Input dict of property names and their computed values |
required |
new_name |
str
|
new name under which the concatenated property dict will be returned |
required |
order |
Optional[Iterable[str]]
|
Optional list of key that specifies the order in which concatenation should be done. Sorting list by default |
None
|
Returns:
Name | Type | Description |
---|---|---|
dict | dictionary of concatenated output values with a single key corresponding to new_name |
Source code in molfeat/utils/commons.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
|
ensure_picklable(fn)
¶
Ensure a function is picklable
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
Callable
|
function to be pickled |
required |
Source code in molfeat/utils/commons.py
75 76 77 78 79 80 81 82 83 |
|
filter_arguments(fn, params)
¶
Filter the argument of a function to only retain the valid ones
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
Callable
|
Function for which arguments will be checked |
required |
params |
dict
|
key-val dictionary of arguments to pass to the input function |
required |
Returns:
Name | Type | Description |
---|---|---|
params_filtered |
dict
|
dict of filtered arguments for the function |
Source code in molfeat/utils/commons.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
|
fn_to_hex(fn)
¶
Pickle an object and return its hex representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fn |
object to pickle |
required |
Returns:
Name | Type | Description |
---|---|---|
str | hex representation of object |
Source code in molfeat/utils/commons.py
86 87 88 89 90 91 92 93 94 95 96 |
|
fold_count_fp(fp, dim=2 ** 10, binary=False)
¶
Fast folding of a count fingerprint to the specified dimension
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fp |
Iterable
|
iterable fingerprint |
required |
dim |
int
|
dimension of the folded array if not provided. Defaults to 2**10. |
2 ** 10
|
binary |
bool
|
whether to fold into a binary array or take use a count vector |
False
|
Returns:
Name | Type | Description |
---|---|---|
folded | returns folded array to the provided dimension |
Source code in molfeat/utils/commons.py
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 |
|
get_class_name(cls)
¶
Get class full name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cls |
Type
|
name of the class |
required |
Source code in molfeat/utils/commons.py
46 47 48 49 50 51 52 53 54 55 56 |
|
hex_to_fn(hex)
¶
Load a hex string as a callable. Raise error on fail
Parameters:
Name | Type | Description | Default |
---|---|---|---|
hex |
str
|
hex string to load as a callable |
required |
Returns:
Name | Type | Description |
---|---|---|
callable | callable loaded from the hex string |
Source code in molfeat/utils/commons.py
99 100 101 102 103 104 105 106 107 108 109 110 |
|
one_hot_encoding(val, allowable_set, encode_unknown=False, dtype=int)
¶
Converts a single value to a one-hot vector.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
val |
int
|
class to be converted into a one hot vector |
required |
allowable_set |
Iterable
|
a list or 1D array of allowed choices for val to take |
required |
dtype |
Callable
|
data type of the the return. Default = int. |
int
|
encode_unknown |
bool
|
whether to map inputs not in allowable set to an additional last element. |
False
|
Returns:
Type | Description |
---|---|
A numpy 1D array of length len(allowable_set) + 1 |
Source code in molfeat/utils/commons.py
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 |
|
pack_bits(obj, protocol=4)
¶
Pack an object into a bits representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
object to pack |
required |
Returns:
Name | Type | Description |
---|---|---|
bytes | byte-packed version of object |
Source code in molfeat/utils/commons.py
343 344 345 346 347 348 349 350 351 352 |
|
pack_graph(batch_G, batch_x)
¶
Pack a batch of graph and atom features into a single graph
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch_G |
List[torch.FloatTensor]
|
List of adjacency graph, each of size (n_i, n_i). |
required |
batch_x |
List[torch.FloatTensor]
|
List of atom feature matrices, each of size (n_i, F), F being the number of features |
required |
Returns:
Type | Description |
---|---|
new_batch_G, new_batch_x: torch.LongTensor 2D, torch.Tensor 2D
This tuple represents a new arbitrary graph that contains the whole batch,
and the corresponding atom feature matrix. new_batch_G has a size (N, N), with :math: |
Source code in molfeat/utils/commons.py
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 |
|
requires_conformer(calculator)
¶
Decorator for any descriptor calculator that requires conformers
Source code in molfeat/utils/commons.py
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
requires_standardization(calculator=None, *, disconnect_metals=True, remove_salt=True, **standardize_kwargs)
¶
Decorator for any descriptor calculator that required standardization of the molecules
Parameters:
Name | Type | Description | Default |
---|---|---|---|
calculator |
Callable
|
calculator to wrap |
None
|
disconnect_metals |
bool
|
whether to force metal disconnection |
True
|
remove_salt |
bool
|
whether to remove salt from the molecule |
True
|
Source code in molfeat/utils/commons.py
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 |
|
sha256sum(filepath)
¶
Return the sha256 sum hash of a file or a directory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[str, os.PathLike]
|
The path to the file to compute the MD5 hash on. |
required |
Source code in molfeat/utils/commons.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
unpack_bits(bvalues)
¶
Pack an object into a bits representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bvalues |
bytes to be unpacked |
required |
Returns:
Name | Type | Description |
---|---|---|
obj | object that was packed |
Source code in molfeat/utils/commons.py
355 356 357 358 359 360 361 362 363 364 |
|
Require module¶
check(module, min_version=None, max_version=None)
cached
¶
Check if module is available for import
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module |
str
|
name of the module to check |
required |
min_version |
Optional[str]
|
optional minimum version string to check |
None
|
max_version |
Optional[str]
|
optional maximum version string to check |
None
|
Source code in molfeat/utils/requires.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
mock(name)
¶
Mock a function to raise an error
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
str
|
name of the module or function to mock |
required |
Source code in molfeat/utils/requires.py
38 39 40 41 42 43 44 45 |
|
Datatype Conversion¶
cast(fp, dtype=None, columns=None)
¶
Change the datatype of a list of input array
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fp |
array
|
Input array to cast (2D) |
required |
dtype |
Optional[Callable]
|
datatype to cast to |
None
|
columns |
Optional[Iterable]
|
column names for pandas dataframe |
None
|
Source code in molfeat/utils/datatype.py
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 |
|
ensure_explicit(x)
¶
Ensure that the input vector is not a sparse bit vector
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Union[SparseBitVect, ExplicitBitVect]
|
input vector |
required |
Returns:
Name | Type | Description |
---|---|---|
converted | ExplicitBitVect if input is SparseBitVec, else input as is |
Source code in molfeat/utils/datatype.py
18 19 20 21 22 23 24 25 26 27 28 29 |
|
is_dtype_bitvect(dtype)
¶
Verify if the dtype is a bitvect type
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtype |
callable
|
The dtype of a value. E.g. np.int32, str, torch.float |
required |
Returns:
Type | Description |
---|---|
A boolean saying if the dtype is a torch dtype |
Source code in molfeat/utils/datatype.py
168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
is_dtype_numpy(dtype)
¶
Verify if the dtype is a numpy dtype
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtype |
callable
|
The dtype of a value. E.g. np.int32, str, torch.float |
required |
Returns A boolean saying if the dtype is a numpy dtype
Source code in molfeat/utils/datatype.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
is_dtype_tensor(dtype)
¶
Verify if the dtype is a torch dtype
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dtype |
callable
|
The dtype of a value. E.g. np.int32, str, torch.float |
required |
Returns:
Type | Description |
---|---|
A boolean saying if the dtype is a torch dtype |
Source code in molfeat/utils/datatype.py
155 156 157 158 159 160 161 162 163 164 165 |
|
is_null(obj)
¶
Check if an obj is null (nan, None or array of nan)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_null |
bool
|
whether the input obj is null |
required |
Source code in molfeat/utils/datatype.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
to_fp(arr, bitvect=True, sparse=False)
¶
Convert numpy array to fingerprint
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
np.ndarray
|
Numpy array to convert to bitvec |
required |
bitvect |
bool
|
whether to assume the data is a bitvect or intvect |
True
|
sparse |
bool
|
whether to convert to sparse bit vect |
False
|
Returns:
Name | Type | Description |
---|---|---|
fp | RDKit bit vector |
Source code in molfeat/utils/datatype.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
to_numpy(x, copy=False, dtype=None)
¶
Convert a tensor to numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Object
|
The Python object to convert. |
required |
copy |
bool
|
Whether to copy the memory. By default, if a tensor is already on CPU, the Numpy array will be a view of the tensor. |
False
|
dtype |
callable
|
Optional type to cast the values to |
None
|
Returns:
Type | Description |
---|---|
A new Python object with the same structure as |
|
arrays. Not supported type are left as reference in the new object. |
Source code in molfeat/utils/datatype.py
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 |
|
to_sparse(x, dtype=None)
¶
Converts dense tensor x to sparse format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
torch.Tensor
|
tensor to convert |
required |
dtype |
torch.dtype
|
Enforces new data type for the output. If None, it keeps the same datatype as x (Default: None) |
None
|
Returns:
Type | Description |
---|---|
new torch.sparse Tensor |
Source code in molfeat/utils/datatype.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
to_tensor(x, gpu=False, dtype=None)
¶
Convert a numpy array to tensor. The tensor type will be the same as the original array, unless specify otherwise
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
numpy.ndarray
|
Numpy array to convert to tensor type |
required |
gpu |
bool optional
|
Whether to move tensor to gpu. Default False |
False
|
dtype |
torch.dtype
|
Enforces new data type for the output |
None
|
Returns:
Type | Description |
---|---|
New torch.Tensor |
Source code in molfeat/utils/datatype.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
Pooling¶
BartPooler
¶
Bases: nn.Module
Default Bart pooler as implemented in huggingface transformers The Bart pooling function focusing on the first token ([CLS]) to get a sentence representation.
Source code in molfeat/utils/pooler.py
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 |
|
config = config
instance-attribute
¶
__init__(config, **kwargs)
¶
Source code in molfeat/utils/pooler.py
161 162 163 |
|
forward(h, inputs=None, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
torch.Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[torch.Tensor]
|
inputs tokens to the bart underlying model |
None
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
torch.Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
|
BertPooler
¶
Bases: nn.Module
Default Bert pooler as implemented in huggingface transformers The bert pooling function focuses on the first token ([CLS]) to get a sentence representation.
Source code in molfeat/utils/pooler.py
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 146 147 148 149 150 151 152 |
|
activation = nn.Tanh() if activation is None else activation
instance-attribute
¶
config = config
instance-attribute
¶
dense = nn.Linear(hidden_size, hidden_size)
instance-attribute
¶
random_seed = random_seed
instance-attribute
¶
__init__(config, activation=None, random_seed=None, **kwargs)
¶
Source code in molfeat/utils/pooler.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
forward(h, inputs=None, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
torch.Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[torch.Tensor]
|
optional input that has been provided to the underlying bert model |
None
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
torch.Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
GPTPooler
¶
Bases: nn.Module
Default GPT pooler as implemented in huggingface transformers The Bart pooling function focusing on the first token ([CLS]) to get a sentence representation.
Source code in molfeat/utils/pooler.py
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 |
|
config = config
instance-attribute
¶
pad_token_id = config.get('pad_token_id')
instance-attribute
¶
__init__(config, **kwargs)
¶
Source code in molfeat/utils/pooler.py
190 191 192 193 |
|
forward(h, inputs=None, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
torch.Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[torch.Tensor]
|
inputs tokens to the bart underlying model |
None
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
torch.Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
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 |
|
HFPooler
¶
Bases: nn.Module
Default Pooler based on Molfeat Pooling layer
Source code in molfeat/utils/pooler.py
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 |
|
config = config
instance-attribute
¶
pooling = Pooling(dim=dim, name=name)
instance-attribute
¶
__init__(config, dim=1, name='mean', **kwargs)
¶
Source code in molfeat/utils/pooler.py
82 83 84 85 |
|
forward(h, inputs=None, mask=None, ignore_padding=True, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
torch.Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[torch.Tensor]
|
optional input that has been provided to the underlying bert model |
None
|
mask |
Optional[torch.Tensor]
|
optional mask to use in place of computing the padding specific mask |
None
|
ignore_padding |
bool
|
whether to ignore padding tokens when pooling |
True
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
torch.Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
|
Pooling
¶
Bases: nn.Module
Perform simple pooling on a tensor over one dimension
Source code in molfeat/utils/pooler.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
SUPPORTED_POOLING = ['mean', 'avg', 'max', 'sum', 'clf', None]
class-attribute
¶
dim = dim
instance-attribute
¶
name = name
instance-attribute
¶
__init__(dim=1, name='max')
¶
Pooling for embeddings
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dim |
int
|
dimension to pool over, default is 1 |
1
|
name |
str
|
pooling type. Default is 'mean'. |
'max'
|
Source code in molfeat/utils/pooler.py
40 41 42 43 44 45 46 47 48 49 50 |
|
forward(x, indices=None, mask=None)
¶
Perform a pooling operation on the input tensor
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
input tensor to pull over |
required | |
indices |
List[int]
|
Subset of indices to pool over. Defaults to None for all indices. |
None
|
mask |
torch.Tensor
|
binary mask to apply when pooling. Defaults to None, which is a matrix of 1. If mask is provided it takes precedence over indices. |
None
|
Source code in molfeat/utils/pooler.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
get_default_hgf_pooler(name, config, **kwargs)
¶
Get default HuggingFace pooler based on the model name
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name |
name of the model |
required | |
config |
config of the model |
required | |
kwargs |
additional arguments to pass to the pooler |
{}
|
Source code in molfeat/utils/pooler.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Mol Format Converters¶
SmilesConverter
¶
Molecule line notation conversion from smiles to selfies or inchi
Source code in molfeat/utils/converters.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
SUPPORTED_LINE_NOTATIONS = ['none', 'smiles', 'selfies', 'inchi']
class-attribute
¶
converter = None
instance-attribute
¶
target = target
instance-attribute
¶
__init__(target=None)
¶
Convert input smiles to a target line notation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
str
|
target representation. |
None
|
Source code in molfeat/utils/converters.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
decode(inp)
¶
Decode inputs into smiles
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inp |
str
|
input representation to decode |
required |
Source code in molfeat/utils/converters.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
encode(smiles)
¶
Encode a input smiles into target line notation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
smiles |
str
|
input smiles to encode |
required |
Source code in molfeat/utils/converters.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|