molfeat.utils
Cache¶
CacheList
¶
Proxy for supporting search using a list of cache
Source code in molfeat/utils/cache.py
685 686 687 688 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 |
|
__call__(*args, **kwargs)
¶
Compute the features for a list of molecules and save them to the cache
Source code in molfeat/utils/cache.py
724 725 726 727 728 729 730 731 |
|
__contains__(key)
¶
Check whether a key is in the cache Args: key: key to check in the cache
Source code in molfeat/utils/cache.py
698 699 700 701 702 703 |
|
__iter__()
¶
Iterate over all the caches
Source code in molfeat/utils/cache.py
709 710 711 |
|
__len__()
¶
Return the length of the cache
Source code in molfeat/utils/cache.py
705 706 707 |
|
__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
713 714 715 716 717 718 719 720 721 722 |
|
clear(*args, **kwargs)
¶
Clear all the caches and make them inaccesible
Source code in molfeat/utils/cache.py
733 734 735 736 |
|
fetch(mols)
¶
Get the representation for a single
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mols |
List[Union[Mol, str]]
|
list of molecules |
required |
Source code in molfeat/utils/cache.py
770 771 772 773 774 775 776 777 778 779 780 781 |
|
get(key, default=None)
¶
Get the cached value for a specific key Args: key: key to get default: default value to return when the key is not found
Source code in molfeat/utils/cache.py
742 743 744 745 746 747 748 749 750 751 752 |
|
items()
¶
Return iterator of key, values in the cache
Source code in molfeat/utils/cache.py
762 763 764 |
|
keys()
¶
Get list of keys in the cache
Source code in molfeat/utils/cache.py
754 755 756 |
|
to_dict()
¶
Convert current cache to a dictionary
Source code in molfeat/utils/cache.py
766 767 768 |
|
values()
¶
Get list of values in the cache
Source code in molfeat/utils/cache.py
758 759 760 |
|
DataCache
¶
Bases: _Cache
Molecular features caching system that cache computed values in memory for reuse later
Source code in molfeat/utils/cache.py
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 |
|
__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[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
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 |
|
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
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
|
load_from_file(filepath)
classmethod
¶
Load a datache from a file (including remote file)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[PathLike, str]
|
path to the file to load |
required |
Source code in molfeat/utils/cache.py
377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
save_to_file(filepath)
¶
Save the cache to a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[PathLike, str]
|
path to the file to save |
required |
Source code in molfeat/utils/cache.py
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
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
361 362 363 364 365 366 367 368 369 370 |
|
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
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 |
|
__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[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
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 |
|
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
506 507 508 509 510 511 512 513 514 515 |
|
items()
¶
Return iterator of key, values in the cache
Source code in molfeat/utils/cache.py
517 518 519 520 521 |
|
load_from_file(filepath, **kwargs)
classmethod
¶
Load a FileCache from a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[PathLike, str]
|
path to the file to load |
required |
kwargs |
keyword arguments to pass to the constructor |
{}
|
Source code in molfeat/utils/cache.py
574 575 576 577 578 579 580 581 582 583 |
|
save_to_file(filepath=None, file_type=None, **kwargs)
¶
Save the cache to a file
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Optional[Union[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
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 |
|
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
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 |
|
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
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 |
|
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
560 561 562 563 564 565 566 567 568 569 570 571 572 |
|
MPDataCache
¶
Bases: DataCache
A datacache that supports multiprocessing natively
Source code in molfeat/utils/cache.py
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 |
|
__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
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 |
|
MolToKey
¶
Convert a molecule to a key
Source code in molfeat/utils/cache.py
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
__call__(mol)
¶
Convert a molecule object to a key that can be used for the cache system
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mol |
Mol
|
input molecule object |
required |
Source code in molfeat/utils/cache.py
68 69 70 71 72 73 74 75 76 77 78 |
|
__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
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
|
from_state_dict(state)
staticmethod
¶
Load a MolToKey object from a state dict.
Source code in molfeat/utils/cache.py
93 94 95 96 |
|
to_state_dict()
¶
Serialize MolToKey to a state dict.
Source code in molfeat/utils/cache.py
80 81 82 83 84 85 86 87 88 89 90 91 |
|
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[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
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 |
|
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
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
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
87 88 89 90 91 92 93 94 95 |
|
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
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
|
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
98 99 100 101 102 103 104 105 106 107 108 |
|
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
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 |
|
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
58 59 60 61 62 63 64 65 66 67 68 |
|
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
111 112 113 114 115 116 117 118 119 120 121 122 |
|
is_callable(func)
¶
Check if func is a function or a callable
Source code in molfeat/utils/commons.py
33 34 35 36 37 |
|
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
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 |
|
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
355 356 357 358 359 360 361 362 363 364 |
|
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[FloatTensor]
|
List of adjacency graph, each of size (n_i, n_i). |
required |
batch_x |
List[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
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 |
|
requires_conformer(calculator)
¶
Decorator for any descriptor calculator that requires conformers
Source code in molfeat/utils/commons.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
requires_standardization(calculator=None, *, disconnect_metals=True, remove_salt=True, **standardize_kwargs)
¶
Decorator for any descriptor calculator that required standardization of the molecules Args: calculator: calculator to wrap disconnect_metals: whether to force metal disconnection remove_salt: whether to remove salt from the molecule
Source code in molfeat/utils/commons.py
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 |
|
sha256sum(filepath)
¶
Return the sha256 sum hash of a file or a directory
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
Union[str, PathLike]
|
The path to the file to compute the MD5 hash on. |
required |
Source code in molfeat/utils/commons.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
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
367 368 369 370 371 372 373 374 375 376 |
|
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¶
as_numpy_array_if_possible(arr, dtype)
¶
Convert an input array to a numpy datatype if possible Args: arr: input array dtype: optional numpy datatype
Source code in molfeat/utils/datatype.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
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
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 |
|
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
20 21 22 23 24 25 26 27 28 29 30 31 |
|
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
170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
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
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
|
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
157 158 159 160 161 162 163 164 165 166 167 |
|
is_null(obj)
¶
Check if an obj is null (nan, None or array of nan)
Source code in molfeat/utils/datatype.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
|
to_fp(arr, bitvect=True, sparse=False)
¶
Convert numpy array to fingerprint
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arr |
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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
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
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 |
|
to_sparse(x, dtype=None)
¶
Converts dense tensor x to sparse format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
tensor to convert |
required |
dtype |
dtype
|
Enforces new data type for the output. If None, it keeps the same datatype as x (Default: None) |
None
|
Returns: new torch.sparse Tensor
Source code in molfeat/utils/datatype.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
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 |
ndarray
|
Numpy array to convert to tensor type |
required |
gpu |
bool optional
|
Whether to move tensor to gpu. Default False |
False
|
dtype |
dtype
|
Enforces new data type for the output |
None
|
Returns:
Type | Description |
---|---|
New torch.Tensor |
Source code in molfeat/utils/datatype.py
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 |
|
Pooling¶
BartPooler
¶
Bases: Module
Default Bart pooler as implemented in huggingface transformers The Bart pooling function focusing on the eos token ([EOS]) to get a sentence representation.
Source code in molfeat/utils/pooler.py
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 |
|
forward(h, inputs=None, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[Tensor]
|
inputs tokens to the bart underlying model |
None
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
BertPooler
¶
Bases: Module
Default Bert pooler as implemented in huggingface transformers The bert pooling function focuses on a projection of the first token ([CLS]) to get a sentence representation.
Source code in molfeat/utils/pooler.py
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 153 154 |
|
forward(h, inputs=None, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[Tensor]
|
optional input that has been provided to the underlying bert model |
None
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
|
GPTPooler
¶
Bases: Module
Default GPT pooler as implemented in huggingface transformers The GPT pooling function focusing on the last non-padding token given sequence length to get a sentence representation.
Source code in molfeat/utils/pooler.py
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 |
|
forward(h, inputs=None, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[Tensor]
|
inputs tokens to the bart underlying model |
None
|
Returns:
Name | Type | Description |
---|---|---|
pooled_output |
Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
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 |
|
HFPooler
¶
Bases: Module
Default Pooler based on Molfeat Pooling layer
Source code in molfeat/utils/pooler.py
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 |
|
forward(h, inputs=None, mask=None, ignore_padding=True, **kwargs)
¶
Forward pass of the pooling layer
Parameters:
Name | Type | Description | Default |
---|---|---|---|
h |
Tensor
|
hidden representation of the input sequence to pool over |
required |
inputs |
Optional[Tensor]
|
optional input that has been provided to the underlying bert model |
None
|
mask |
Optional[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 |
Tensor
|
pooled representation of the input sequence |
Source code in molfeat/utils/pooler.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
Pooling
¶
Bases: 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 77 78 |
|
__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 |
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 77 78 |
|
get_default_hgf_pooler(name, config, **kwargs)
¶
Get default HuggingFace pooler based on the model name Args: name: name of the model config: config of the model 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 |
|
__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 |
|