eche
ClusterHelper
Convenience class for entity clusters.
The :class:ClusterHelper
class holds a dict mapping entities to the respective cluster_id
and a dict with cluster_id mapping to entity sets.
The :meth:.add()
and :meth:.remove()
keep the respective dicts in sync.
Examples:
>>> from eche import ClusterHelper
>>> ch = ClusterHelper([{"a1", "b1"}, {"a2", "b2"}])
>>> print(ch.clusters)
{0: {'a1', 'b1'}, 1: {'a2', 'b2'}}
Add an element to a cluster
>>> ch.add_to_cluster(0, "c1")
>>> print(ch.clusters)
{0: {'a1', 'b1', 'c1'}, 1: {'a2', 'b2'}}
Add a new cluster
>>> ch.add({"e2", "f1", "c3"})
>>> print(ch.clusters)
{0: {'a1', 'b1', 'c1'}, 1: {'a2', 'b2'}, 2: {'f1', 'e2', 'c3'}}
Remove an element from a cluster
>>> ch.remove("b1")
>>> print(ch.clusters)
{0: {'a1', 'c1'}, 1: {'a2', 'b2'}, 2: {'f1', 'e2', 'c3'}}
Transitively connected clusters are automatically resolved
>>> ch = ClusterHelper([{"a", "b"}, {"b", "c"}])
>>> print(ch.clusters)
{0: {'a', 'b', 'c'}}
The contains function is smartly overloaded. You can check if an entity is in the ClusterHelper
>>> "a1" in ch
True
If a cluster is present
>>> {"c1","a1"} in ch
True
And even if a link exists or not
>>> ("f1","e2") in ch
True
>>> ("a1","e2") in ch
False
To know the cluster id of an entity you can look it up with
>>> ch.elements["a1"]
0
To get members of a cluster either use
>>> ch.members(0)
{'a1', 'b1', 'c1'}
or simply
>>> ch[0]
{'a1', 'b1', 'c1'}
Source code in eche/cluster.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 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 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 146 147 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 |
|
number_of_links
property
Return the total number of links.
__contains__(key)
Check if entities/links/clusters are contained.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Either entity id, Tuple with two entities to check for a link between entities, or a clusters as set of entity ids |
required |
Source code in eche/cluster.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
|
__getitem__(key)
Get cluster members.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
cluster id |
required |
Returns:
Type | Description |
---|---|
Cluster members |
Source code in eche/cluster.py
238 239 240 241 242 243 244 245 246 247 |
|
__init__(data=None)
Initialize a ClusterHelper object with clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Clusters either as list of sets, or dict with links as key, value pairs, or dict with cluster id and set of members |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
if data is not dict or list |
ValueError
|
For dict[cluster_id,member_set] if overlaps between clusters |
Note
Will try to merge clusters transitively if necessary.
Source code in eche/cluster.py
144 145 146 147 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 |
|
__setitem__(key, value)
Not Implemented.
Source code in eche/cluster.py
284 285 286 287 288 |
|
add(new_entry, c_id=None)
Add a new cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_entry |
Set
|
New cluster as set |
required |
c_id |
Optional[int]
|
Cluster id that will be assigned. |
None
|
If None, the next largest cluster id will be assigned Assuming cluster ids are integers
Raises:
Type | Description |
---|---|
ValueError
|
If entity id already present in other cluster Or if new cluster id cannot be inferred automatically by incrementing |
Source code in eche/cluster.py
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 |
|
add_link(e1, e2)
Add a new entity link.
Either adds a link to an existing entity or creates a new cluster with both.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
e1 |
str
|
Id of one entity that will be linked |
required |
e2 |
str
|
Id of other entity that will be linked |
required |
Returns:
Type | Description |
---|---|
Union[int, bool]
|
Id of cluster that was created, or |
Union[int, bool]
|
of existing cluster that was enhanced |
Union[int, bool]
|
Returns False if link already was present |
Source code in eche/cluster.py
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 |
|
add_to_cluster(c_id, new_entity)
Add an entity to a cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c_id |
int
|
Cluster id where entity will be added |
required |
new_entity |
str
|
Id of new entity |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If cluster id unknown |
ValueError
|
If entity already belongs to other cluster |
Source code in eche/cluster.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
all_pairs(key=None)
Get all entity pairs of a specific cluster or of all clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
cluster id, or if None, provides pairs of all clusters. |
None
|
Returns:
Type | Description |
---|---|
Iterable[Tuple[Any, Any]]
|
Generator that produces the wanted pairs. |
Source code in eche/cluster.py
213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
clone()
Create a clone of this object.
Returns:
Type | Description |
---|---|
ClusterHelper
|
cloned ClusterHelper |
Source code in eche/cluster.py
465 466 467 468 469 470 471 472 473 474 |
|
from_file(path, has_cluster_id=False, sep=',')
classmethod
Create ClusterHelper from file.
Expects entities seperated by sep
, with each line representing a cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, PathLike]
|
path to file containing entity clusters |
required |
has_cluster_id |
bool
|
if True, the first entry in each line is used as cluster id |
False
|
sep |
seperator |
','
|
Returns:
Type | Description |
---|---|
ClusterHelper
|
ClusterHelper |
Source code in eche/cluster.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
|
from_numpy(numpy_links)
classmethod
Create a ClusterHelper from a 2D numpy array (i.e. binary entity links).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numpy_links |
ndarray
|
binary links in 2D numpy array |
required |
Returns:
Type | Description |
---|---|
ClusterHelper
|
"ClusterHelper": |
Source code in eche/cluster.py
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
|
from_zipped_file(path, inner_path, has_cluster_id=False, sep=',', encoding='utf8')
classmethod
Read an inner link file from a zip archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, PathLike]
|
The path to the zip archive |
required |
inner_path |
str
|
The path inside the zip archive to the link file |
required |
has_cluster_id |
bool
|
if True, the first entry in each line is used as cluster id |
False
|
sep |
str
|
seperator |
','
|
encoding |
str
|
used to decode the data |
'utf8'
|
Returns:
Type | Description |
---|---|
ClusterHelper
|
ClusterHelper |
Source code in eche/cluster.py
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 |
|
get(key, value=None)
Return cluster's element or default value.
Tries to return the cluster with the cluster id == key. If None is found return provided value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
Searched cluster id. |
required | |
value |
Default value to return in case id is not present. |
None
|
Returns:
Type | Description |
---|---|
Cluster with provided cluster_id. |
Source code in eche/cluster.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|
info()
Return general information about this object.
Returns:
Type | Description |
---|---|
str
|
information about number of entities and clusters |
Source code in eche/cluster.py
182 183 184 185 186 187 188 189 190 191 192 193 |
|
links(key, always_return_set=False)
Get entities linked to this entity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
str
|
entity id |
required |
always_return_set |
bool
|
If True, return set even if only one entity is contained |
False
|
Returns:
Type | Description |
---|---|
Union[str, Set[str]]
|
Either the id of the single linked entity or a set of |
Union[str, Set[str]]
|
ids if there is more than one link |
Union[str, Set[str]]
|
If always_return_set is True, will always return set |
Source code in eche/cluster.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
|
members(key)
Get members of a cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key |
cluster id |
required |
Returns:
Type | Description |
---|---|
Set
|
cluster members |
Source code in eche/cluster.py
227 228 229 230 231 232 233 234 235 236 |
|
merge(c1, c2, new_id=None)
Merge two clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c1 |
int
|
Id of one cluster to merge |
required |
c2 |
int
|
Id of other cluster to merge |
required |
new_id |
Optional[int]
|
New id of cluster, if None take c1 |
None
|
Returns:
Type | Description |
---|---|
int
|
cluster id of merged cluster |
Raises:
Type | Description |
---|---|
ValueError
|
If cluster id(s) do not exist |
Source code in eche/cluster.py
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 |
|
remove(entry)
Remove an entity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entry |
str
|
entity to remove |
required |
Source code in eche/cluster.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
remove_cluster(cluster_id)
Remove an entire cluster with the given cluster id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cluster_id |
id of the cluster to remove |
required |
Source code in eche/cluster.py
447 448 449 450 451 452 453 454 455 456 |
|
sample(n, seed=None)
Sample n clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
Number of clusters to return. |
required |
seed |
Optional[int]
|
Seed for randomness |
None
|
Returns:
Type | Description |
---|---|
ClusterHelper with n clusters. |
Source code in eche/cluster.py
290 291 292 293 294 295 296 297 298 299 300 301 |
|
to_file(path, write_cluster_id=False)
Write clusters to file.
Each cluster line is consists of comma-seperated entities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, PathLike]
|
Where to write the clusters. |
required |
write_cluster_id |
bool
|
If True, first entry of each line is the cluster id |
False
|
Source code in eche/cluster.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 |
|
to_numpy()
Return binary links as numpy array.
Returns:
Type | Description |
---|---|
ndarray
|
binary links |
Source code in eche/cluster.py
583 584 585 586 587 588 589 590 591 |
|
PrefixedClusterHelper
Bases: ClusterHelper
ClusterHelper which uses prefixes, to associate entities with datasets.
Examples:
>>> from eche import PrefixedClusterHelper
>>> prefixes = OrderedDict({"ds1": "foo:", "ds2": "bar:", "ds3": "baz:"})
>>> clusters = {
... 0: {"foo:a", "bar:b", "bar:c", "baz:a"},
... 1: {"foo:d", "foo:e", "baz:b"},
... 2: {"foo:f", "foo:g", "bar:h", "bar:i"},
... }
>>> ch = PrefixedClusterHelper(ds_prefixes=prefixes, data=clusters)
Only entities with known prefix can be added
>>> ch.add_to_cluster(0, "unknown:b")
ValueError: "unknown:b" does not start with any known prefix: ['foo:', 'bar:', 'baz:']
You can ask for entity pairs of a binary combination of datasets The tuples are ordered as the supplied dataset tuples implies i.e. all the first tuple entries are entities of the first dataset
>>> list(ch.pairs_in_ds_tuple(("ds1","ds3")))
[('foo:a', 'baz:a'), ('foo:d', 'baz:b'), ('foo:e', 'baz:b')]
Intradataset links can also be returned
>>> list(ch.intra_dataset_pairs("ds2"))
[('bar:c', 'bar:b'), ('bar:i', 'bar:h')]
To get all entities of a single dataset you can use
>>> list(ch.get_ds_entities("ds1"))
["foo:a", "foo:d", "foo:e", "foo:g", "foo:f"]
Source code in eche/cluster.py
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 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 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 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
|
number_of_intra_links: Tuple[int, ...]
property
Returns number of intra-dataset links for each dataset.
number_of_no_intra_links: int
property
Returns number of links without intra-dataset links.
add(new_entry, c_id=None)
Add a new cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_entry |
Set
|
New cluster as set |
required |
c_id |
Optional[int]
|
Cluster id that will be assigned. |
None
|
If None, the next largest cluster id will be assigned Assuming cluster ids are integers
Raises:
Type | Description |
---|---|
ValueError
|
If entity id already present in other cluster Or if new cluster id cannot be inferred automatically by incrementing Or if prefix is unknown |
Source code in eche/cluster.py
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 |
|
add_link(e1, e2)
Add a new entity link.
Either adds a link to an existing entity or creates a new cluster with both.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
e1 |
str
|
Id of one entity that will be linked |
required |
e2 |
str
|
Id of other entity that will be linked |
required |
Returns:
Type | Description |
---|---|
Union[int, bool]
|
Id of cluster that was created, or |
Union[int, bool]
|
of existing cluster that was enhanced |
Union[int, bool]
|
Returns False if link already was present |
Raises:
Type | Description |
---|---|
ValueError
|
If prefix is unknown |
Source code in eche/cluster.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
|
add_to_cluster(c_id, new_entity)
Add an entity to a cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c_id |
int
|
Cluster id where entity will be added |
required |
new_entity |
str
|
Id of new entity |
required |
Raises:
Type | Description |
---|---|
KeyError
|
If cluster id or prefix is unknown |
ValueError
|
If entity already belongs to other cluster |
Source code in eche/cluster.py
728 729 730 731 732 733 734 735 736 737 738 739 740 |
|
all_pairs_no_intra()
Returns known links without intra-dataset pairs (with canonical ordering).
Returns:
Type | Description |
---|---|
Generator[Tuple[str, str], None, None]
|
Generator that produces known links without intra-dataset pairs. |
Source code in eche/cluster.py
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
|
from_file(path, has_cluster_id=False, sep=',', ds_prefixes=_EMPTY_DEFAULT_DS_PREF)
classmethod
Create PrefixedClusterHelper from file.
Expects entities seperated by sep
, with each line representing a cluster.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, PathLike]
|
path to file containing entity clusters |
required |
has_cluster_id |
bool
|
if True, the first entry in each line is used as cluster id |
False
|
sep |
seperator |
','
|
|
ds_prefixes |
OrderedDict[str, str]
|
Dataset prefixes |
_EMPTY_DEFAULT_DS_PREF
|
Returns:
Type | Description |
---|---|
PrefixedClusterHelper
|
PrefixedClusterHelper |
Source code in eche/cluster.py
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 |
|
from_numpy(numpy_links, ds_prefixes=_EMPTY_DEFAULT_DS_PREF)
classmethod
Create a PrefixedClusterHelper from a 2D numpy array.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
numpy_links |
ndarray
|
binary links in 2D numpy array |
required |
ds_prefixes |
OrderedDict[str, str]
|
Dataset prefixes |
_EMPTY_DEFAULT_DS_PREF
|
Returns:
Type | Description |
---|---|
PrefixedClusterHelper
|
"PrefixedClusterHelper": |
Source code in eche/cluster.py
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 |
|
from_zipped_file(path, inner_path, has_cluster_id=False, sep=',', encoding='utf8', ds_prefixes=_EMPTY_DEFAULT_DS_PREF)
classmethod
Read an inner link file from a zip archive.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, PathLike]
|
The path to the zip archive |
required |
inner_path |
str
|
The path inside the zip archive to the link file |
required |
has_cluster_id |
bool
|
if True, the first entry in each line is used as cluster id |
False
|
sep |
str
|
seperator |
','
|
encoding |
str
|
used to decode the data |
'utf8'
|
ds_prefixes |
OrderedDict[str, str]
|
Dataset prefixes |
_EMPTY_DEFAULT_DS_PREF
|
Returns:
Type | Description |
---|---|
PrefixedClusterHelper
|
"PrefixedClusterHelper": |
Source code in eche/cluster.py
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
|
get_ds_entities(ds_name)
Get all entities belonging to the given dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds_name |
str
|
Name of dataset |
required |
Returns:
Type | Description |
---|---|
Generator[str, None, None]
|
Generator producing entity ids of the given dataset |
Source code in eche/cluster.py
742 743 744 745 746 747 748 749 750 751 752 753 754 |
|
intra_dataset_pairs(ds_name)
Returns known links inside given dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds_name |
str
|
Dataset for which to find links |
required |
Returns:
Type | Description |
---|---|
Generator[Tuple[str, str], None, None]
|
Generator that produces known links inside given dataset. |
Source code in eche/cluster.py
787 788 789 790 791 792 793 794 795 796 797 798 |
|
pairs_in_ds_tuple(ds_tuple)
Returns known links between two given datasets.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ds_tuple |
Tuple[str, str]
|
Dataset tuple |
required |
Returns:
Type | Description |
---|---|
Generator[Tuple[str, str], None, None]
|
Generator that produces known links between two given datasets. |
Source code in eche/cluster.py
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 |
|
connected_components(clusters)
Create connected components from iterable of known clusters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
clusters |
Iterable[Collection[T]]
|
Known clusters |
required |
Returns:
Type | Description |
---|---|
Generator[Set[T], None, None]
|
Generator of connected components |
Source code in eche/graph_based_clustering.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|