klinker
KlinkerBlockManager
Class for handling of blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
blocks |
DataFrame
|
dataframe with blocks. |
required |
Examples:
>>> from klinker import KlinkerBlockManager
>>> kbm = KlinkerBlockManager.from_dict({ "block1": [[1,3,4],[3,4,5]], "block2": [[3,4,5],[5,6]]}, dataset_names=("A","B"))
>>> kbm.blocks.compute()
A B
block1 [1, 3, 4] [3, 4, 5]
block2 [3, 4, 5] [5, 6]
>>> kbm["block1"].compute()
A B
block1 [1, 3, 4] [3, 4, 5]
>>> len(kbm)
2
>>> set(kbm.all_pairs())
{(4, 4), (5, 5), (3, 4), (1, 5), (4, 3), (4, 6), (1, 4), (4, 5), (3, 3), (5, 6), (3, 6), (1, 3), (3, 5)}
>>> kbm.block_sizes
block1 6
block2 5
Name: block_sizes, dtype: int64
>>> kbm.mean_block_size
5.5
>>> kbm.to_dict()
{'block1': ([1, 3, 4], [3, 4, 5]), 'block2': ([3, 4, 5], [5, 6])}
```
Source code in klinker/data/blocks.py
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 |
|
block_sizes: pd.DataFrame
property
Sizes of blocks
mean_block_size: float
property
Mean size of all blocks.
all_pairs()
Get all pairs
Returns:
Type | Description |
---|---|
Generator[Tuple[Union[int, str], ...], None, None]
|
Generator that creates all pairs, from blocks (including duplicates). |
Source code in klinker/data/blocks.py
422 423 424 425 426 427 428 429 430 |
|
combine(this, other)
classmethod
Combine blocks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
this |
KlinkerBlockManager
|
one block manager to combine |
required |
other |
KlinkerBlockManager
|
other block manager to combine |
required |
Returns:
Type | Description |
---|---|
KlinkerBlockManager
|
Combined KlinkerBlockManager |
Examples:
>>> from klinker import KlinkerBlockManager
>>> kbm = KlinkerBlockManager.from_dict({"block1": [[1,3,4],[3,4,5]], "block2": [[3,4,5],[5,6]]}, dataset_names=("A","B"))
>>> kbm2 = KlinkerBlockManager.from_dict({"block3": [[7,4],[12,8]]}, dataset_names=("A","B"))
>>> kbm_merged = KlinkerBlockManager.combine(kbm, kbm2)
>>> kbm_merged.blocks.compute()
A B
block1 [1, 3, 4] [3, 4, 5]
block2 [3, 4, 5] [5, 6]
block3 [7, 4] [12, 8]
Source code in klinker/data/blocks.py
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 |
|
entity_pairs(entity_id, column_id)
Get all pairs where this entity shows up.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity_id |
Union[str, int]
|
Union[str, int]: Entity id. |
required |
column_id |
int
|
int: Whether entity belongs to left (0) or right (1) dataset. |
required |
Returns:
Type | Description |
---|---|
Generator[Tuple[Union[int, str], ...], None, None]
|
Generator for these pairs. |
Source code in klinker/data/blocks.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
find_blocks(entity_id, column_id)
Find blocks where entity id belongs to.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
entity_id |
Union[str, int]
|
Union[str, int]: Entity id. |
required |
column_id |
int
|
int: Whether entity belongs to left (0) or right (1) dataset. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
Blocks where entity id belongs to. |
Source code in klinker/data/blocks.py
388 389 390 391 392 393 394 395 396 397 398 |
|
from_dict(block_dict, dataset_names=('left', 'right'), npartitions=1, **kwargs)
classmethod
Parameters:
Name | Type | Description | Default |
---|---|---|---|
block_dict |
Dict[BlockIdTypeVar, Tuple[List[EntityIdTypeVar], List[EntityIdTypeVar]]]
|
Dictionary with block information. |
required |
dataset_names |
Tuple[str, str]
|
Tuple[str, str]: Tuple of dataset names. |
('left', 'right')
|
npartitions |
int
|
int: Partitions used for dask. |
1
|
**kwargs |
Passed to |
{}
|
Returns:
Type | Description |
---|---|
KlinkerBlockManager
|
Blocks as KlinkerBlockManager |
Examples:
>>> from klinker import KlinkerBlockManager
>>> kbm = KlinkerBlockManager.from_dict({"block1": [[1,3,4],[3,4,5]], "block2": [[3,4,5],[5,6]]}, dataset_names=("A","B"))
Source code in klinker/data/blocks.py
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 |
|
from_pandas(df, npartitions=1, **kwargs)
classmethod
Create from pandas.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
pd.DataFrame: DataFrame |
required |
npartitions |
int
|
int: Partitions for dask |
1
|
**kwargs |
Passed to |
{}
|
Returns:
Type | Description |
---|---|
KlinkerBlockManager
|
Blocks as KlinkerBlockManager |
Examples:
>>> import pandas as pd
>>> from klinker import KlinkerBlockManager
>>> pd_blocks = pd.DataFrame({'A': {'block1': [1, 3, 4], 'block2': [3, 4, 5]}, 'B': {'block1': [3, 4, 5], 'block2': [5, 6]}})
>>> kbm = KlinkerBlockManager.from_pandas(pd_blocks)
Source code in klinker/data/blocks.py
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
|
read_parquet(path, calculate_divisions=True, **kwargs)
classmethod
Read blocks from parquet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
Union[str, pathlib.Path]: Path where blocks are stored. |
required |
calculate_divisions |
bool
|
bool: Calculate index divisions. |
True
|
**kwargs |
Passed to |
{}
|
Returns:
Type | Description |
---|---|
KlinkerBlockManager
|
Blocks as KlinkerBlockManager |
Source code in klinker/data/blocks.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
|
to_dict()
Return blocks as dict.
Returns:
Type | Description |
---|---|
Dict[Union[str, int], Tuple[Union[str, int], Union[str, int]]]
|
The dict has block names as keys and a tuple of sets of entity ids. |
Source code in klinker/data/blocks.py
376 377 378 379 380 381 382 383 384 385 386 |
|
to_parquet(path, **kwargs)
Write blocks as parquet file(s).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Union[str, Path]
|
Union[str, pathlib.Path]: Where to write. |
required |
**kwargs |
passed to the parquet function |
{}
|
Source code in klinker/data/blocks.py
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
KlinkerDaskFrame
Bases: DataFrame
, AbstractKlinkerFrame
Parallel KlinkerFrame.
Please don't use the __init__
method but rather from_dask_dataframe
for
initialisation!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dsk |
The dask graph to compute this KlinkerFrame |
required | |
name |
The key prefix that specifies which keys in the dask comprise this particular KlinkerFrame |
required | |
meta |
An empty klinkerframe object with names, dtypes, and indices matching the expected output. |
required | |
divisions |
Values along which we partition our blocks on the index |
required |
Returns:
Type | Description |
---|---|
KlinkerDaskFrame |
Examples:
>>> import pandas as pd
>>> from klinker.data import KlinkerDaskFrame
>>> import dask.dataframe as dd
>>> df = dd.from_pandas(pd.DataFrame([("1","John", "Doe"),("2","Jane","Doe")],columns=["id","first name", "surname"]),npartitions=1)
>>> kdf = KlinkerDaskFrame.from_dask_dataframe(df, table_name="A", id_col="id")
>>> kdf
Dask KlinkerDaskFrame Structure:
id first name surname
npartitions=1
0 object object object
1 ... ... ...
Dask Name: KlinkerPandasFrame, 2 graph layers
Table Name: A, id_col: id
Source code in klinker/data/enhanced_df.py
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 |
|
non_id_columns: List[str]
property
All columns which are not id_col
concat_values()
Concatenate attribute values.
Returns:
Type | Description |
---|---|
Series
|
dd.Series with concatenated values and id_col as index. |
Examples:
>>> import pandas as pd
>>> from klinker.data import KlinkerDaskFrame
>>> import dask.dataframe as dd
>>> df = dd.from_pandas(pd.DataFrame([("1","John", "Doe"),("2","Jane","Doe")],columns=["id","first name", "surname"]),npartitions=1)
>>> kdf = KlinkerDaskFrame.from_dask_dataframe(df, table_name="A", id_col="id")
>>> kdf.concat_values().compute()
id
1 John Doe
2 Jane Doe
Name: A, dtype: object
Source code in klinker/data/enhanced_df.py
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 |
|
from_dask_dataframe(df, table_name, id_col, meta=no_default, construction_class=KlinkerPandasFrame)
classmethod
Create KlinkDaskFrame from dask dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
dd.DataFrame: Dask dataframe. |
required |
table_name |
str
|
str: Name of dataset. |
required |
id_col |
str
|
str: Column where entity_ids are stored |
required |
meta |
meta for dask |
no_default
|
|
construction_class |
Type[KlinkerPandasFrame]
|
Either :class: |
KlinkerPandasFrame
|
Returns:
Type | Description |
---|---|
KlinkerDaskFrame
|
KlinkerDaskFrame |
Examples:
>>> import pandas as pd
>>> from klinker.data import KlinkerDaskFrame
>>> import dask.dataframe as dd
>>> df = dd.from_pandas(pd.DataFrame([("1","John", "Doe"),("2","Jane","Doe")],columns=["id","first name", "surname"]),npartitions=1)
>>> kdf = KlinkerDaskFrame.from_dask_dataframe(df, table_name="A", id_col="id")
>>> kdf
Dask KlinkerDaskFrame Structure:
id first name surname
npartitions=1
0 object object object
1 ... ... ...
Dask Name: KlinkerPandasFrame, 2 graph layers
Table Name: A, id_col: id
Source code in klinker/data/enhanced_df.py
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 |
|
KlinkerDataset
dataclass
Helper class to hold info of benchmark datasets.
Source code in klinker/data/ea_dataset.py
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 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 |
|
from_sylloge(dataset, clean=False)
classmethod
Create a klinker dataset from sylloge dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset |
EADataset
|
EADataset: Sylloge dataset. |
required |
clean |
bool
|
bool: Clean attribute information. |
False
|
Returns:
Type | Description |
---|---|
KlinkerDataset
|
klinker dataset |
Examples:
>>> # doctest: +SKIP
>>> from sylloge import MovieGraphBenchmark
>>> ds = KlinkerDataset.from_sylloge(MovieGraphBenchmark())
Source code in klinker/data/ea_dataset.py
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 65 66 67 68 69 70 71 72 73 74 75 76 |
|
sample(size)
Get a sample of the dataset.
Note
Currently this only takes the first n entities of the gold standard.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size |
int
|
int: size of the sample |
required |
Returns:
Type | Description |
---|---|
KlinkerDataset
|
sampled klinker dataset |
Examples:
>>> # doctest: +SKIP
>>> from sylloge import MovieGraphBenchmark
>>> ds = KlinkerDataset.from_sylloge(MovieGraphBenchmark())
>>> sampled = ds.sample(10)
Source code in klinker/data/ea_dataset.py
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 |
|