Documentation for ToMeDa
tomeda.t12_create_dataverse_compatible_json
keeping_track_dict
module-attribute
keeping_track_dict: dict[str, int] = {}
logger
module-attribute
logger: TraceLogger = getLogger(__name__)
sibling_hashmap
module-attribute
sibling_hashmap = {}
Child
dataclass
Child(value: list[str], path: list[str])
path
instance-attribute
path: list[str]
value
instance-attribute
value: list[str]
NoMatchError
NoMatchError(message='No match found')
Bases: Exception
Source code in tomeda/t12_create_dataverse_compatible_json.py
85 86 |
|
TSVElementInfo
dataclass
TSVElementInfo(
typeName: str,
multiple: bool,
typeClass: Literal[
"primitive", "compound", "controlledVocabulary"
],
schema: str,
parent: str | None,
)
multiple
instance-attribute
multiple: bool
parent
instance-attribute
parent: str | None
schema
instance-attribute
schema: str
typeClass
instance-attribute
typeClass: Literal[
"primitive", "compound", "controlledVocabulary"
]
typeName
instance-attribute
typeName: str
TSVElements
dataclass
TSVElements(elements: dict[str, TSVElementInfo])
Contains the 'TSV_Element_Info' as dict. It is stored with the 'typeName' as key. This is done to ensure that the keys are unique and faster access
elements
instance-attribute
elements: dict[str, TSVElementInfo]
assemble_based_on_list_view
assemble_based_on_list_view(
tsv_data: TSVElements,
metadata: dict,
mapping: dict[str, str],
) -> dict[str, Any]
Assembles the metadata based on the list view of the TSV files.
Source code in tomeda/t12_create_dataverse_compatible_json.py
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 |
|
compare_strings
compare_strings(
strings_list: list[str],
) -> list[tuple[int, list[str]]]
Compares a list of strings and identifies the positions where the strings differ.
For each position in the strings where at least one string differs from the others, this function returns that position along with the characters from all strings at that position.
Parameters: - strings_list (list[str]): A list of strings to be compared.
Returns: - list[tuple[int, list[str]]]: A list of tuples where each tuple contains: 1. An integer representing the position in the string where a difference was found. 2. A list of characters (strings of length 1) from all input strings at that position.
Notes: - The function compares strings up to the length of the shortest string in the input list. - If the input list contains less than 2 strings, the function returns a default value of [(0, ["0"])].
Example:
compare_strings(["apple", "aplpe", "appme"]) [(2, ['p', 'l', 'p']), (3, ['l', 'p', 'm'])]
compare_strings(["apple"]) [(0, ['0'])]
Source code in tomeda/t12_create_dataverse_compatible_json.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
create_children
create_children(
dataset_key_: str, metadata, sibling: TSVElementInfo
) -> list[MetadataElement]
The goal of this function is to create a list of 'values' (level 1 Metadata Elements for a level 0 level Metadata Element.
This Path is choosen if the level 0 Metadata Element allows multiple values, and the child created here also allows multiple values.
The Problem is that if we only get 1 element, but multiple are defined in the metada file that we only get the first data.
So the 'multiple' two fold. - The level 0 Elements has multiple childen. - The the level 1 child has multiple values.
However, due to the flattening, a metadata element can occur multiple times, and depending on the position of this element in the final tsv structure we would need to differentiate, if the 'multi-match' leads to multiple childs
or multiple values within each child.
Q: To what metadata key will it be mapped? A: parent_name:child.typeName
Source code in tomeda/t12_create_dataverse_compatible_json.py
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 |
|
create_compound_metadata_element
create_compound_metadata_element(
dv_info: TSVElementInfo,
metadata,
tsv_data: TSVElements,
ignore_list: list[str],
cmp: list[str],
mapping_reverse_fixed: dict[str, str],
)
Creates a compound metadata element. This include the level0 'compound container' for all children and the children itself.
therefore a 'me' (child) as well as a 'me_level0' (the container) is created.
Source code in tomeda/t12_create_dataverse_compatible_json.py
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 |
|
create_metadata_block_entry
create_metadata_block_entry(
dv_info: TSVElementInfo,
dataset_key: str,
metadata,
tsv_data: TSVElements,
ignore_list: list[str],
mapping_reverse_fixed: dict[str, str],
)
Creates a level0 metadata element.
Source code in tomeda/t12_create_dataverse_compatible_json.py
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 |
|
create_primitive_metadata_element
create_primitive_metadata_element(
dv_info: TSVElementInfo, dataset_key: str, metadata
)
Source code in tomeda/t12_create_dataverse_compatible_json.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
|
extract_correct_value
extract_correct_value(value, hash_key: str) -> str
This function is used to extract the correct value from the list of values This happens if the level 0 element allows multiple values, but the child only allows one value.
Source code in tomeda/t12_create_dataverse_compatible_json.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
|
extract_data
extract_data(
tsv_file: Path, instructions: dict = None
) -> dict[str, dict[str, str]]
Extracts keys from a given TSV file.
Args: tsv_file (Path): The input TSV file path. instructions (dict, optional): A dictionary of keys to extract. Defaults to None.
Returns: list: A list of keys. If the key is not specified, the entry 'name' is used.
Source code in tomeda/t12_create_dataverse_compatible_json.py
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 |
|
find_change_idx
find_change_idx(input_list: list) -> list[int]
Source code in tomeda/t12_create_dataverse_compatible_json.py
195 196 197 198 199 200 |
|
fix_mapping
fix_mapping(input_: dict[str, str]) -> dict[str, str]
Source code in tomeda/t12_create_dataverse_compatible_json.py
210 211 212 213 214 215 216 |
|
get_multiple
get_multiple(row: dict, _=None) -> bool
Source code in tomeda/t12_create_dataverse_compatible_json.py
790 791 792 793 794 795 796 797 798 799 |
|
get_string_until_capital
get_string_until_capital(string: str) -> str
Source code in tomeda/t12_create_dataverse_compatible_json.py
203 204 205 206 207 |
|
get_typeclass
get_typeclass(row: dict, controlled_vocab=None) -> str
Source code in tomeda/t12_create_dataverse_compatible_json.py
781 782 783 784 785 786 787 |
|
get_value
get_value(dataset_key: str, metadata: dict) -> Child
Retrieves a value or values from the provided metadata using the specified dataset key.
Parameters: - dataset_key (str): The key path (dot-separated) to extract data from the metadata. - metadata (dict): The JSON-like dictionary from which data is extracted. - multiple (bool): If True, fetches all matching values. If False, only fetches the first match. - with_path (bool, optional): If True, also returns the full JSONPath of the value(s). Defaults to False.
Returns:
- str: If multiple
is False and with_path
is False, returns the value as a string.
- list[str]: If multiple
is True and with_path
is False, returns a list of matching values.
- tuple[str, str]: If multiple
is False and with_path
is True, returns a tuple of the value and its path.
- tuple[str, list[str]]: If multiple
is True and with_path
is True, returns a tuple of the list of values and their paths.
Raises: - NoMatchError: If no matches are found in the metadata for the provided dataset key.
Example:
data = {"a": {"b": [{"c": "value1"}, {"c": "value2"}]}} get_value("a.b.c", data, multiple=True) (['value1', 'value2'], ['$["a"]["b"][0]["c"]', '$["a"]["b"][1]["c"]'])
Notes: This function utilizes JSONPath to perform the data extraction from the provided metadata.
Source code in tomeda/t12_create_dataverse_compatible_json.py
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 |
|
is_controlled_vocab
is_controlled_vocab(
row: dict, controlled_vocab: list[dict[str, str]]
) -> bool
The Metadata validation ensures that the controlled vocabulary is a subset of the allowed values. This has not to be checked here.
Source code in tomeda/t12_create_dataverse_compatible_json.py
765 766 767 768 769 770 771 772 773 774 775 776 777 778 |
|
main
main(param: TomedaParameter) -> None
Source code in tomeda/t12_create_dataverse_compatible_json.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 |
|
read_controlled_vocab
read_controlled_vocab(tsv_file: Path) -> list[dict]
Source code in tomeda/t12_create_dataverse_compatible_json.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 |
|
read_dataset_metadata
read_dataset_metadata(path: Path) -> dict
Source code in tomeda/t12_create_dataverse_compatible_json.py
826 827 828 |
|
read_mapping_information
read_mapping_information(path: Path) -> dict
Source code in tomeda/t12_create_dataverse_compatible_json.py
831 832 833 |
|
read_tsv_data
read_tsv_data(tsv_folders: list[Path]) -> TSVElements
Source code in tomeda/t12_create_dataverse_compatible_json.py
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 |
|
remove_suffix
remove_suffix(s, suffix)
Source code in tomeda/t12_create_dataverse_compatible_json.py
89 90 91 92 |
|
write_mapped_dataset
write_mapped_dataset(
mapped_dataset, path: Path, overwrite: bool = False
)
Source code in tomeda/t12_create_dataverse_compatible_json.py
652 653 654 655 656 657 658 659 660 661 662 663 |
|