Metadata Generation¶
Tools for generating CSVW-EO metadata from datasets.
Main API¶
csvw_eo.make_metadata_from_data
¶
CSVW-EO Metadata Generator.
This module generates CSVW-EO metadata from a CSV dataset. It automatically infers column datatypes, detects dependencies, builds partitions for categorical and numeric attributes, and computes contribution bounds relative to a defined privacy unit.
The output metadata follows the CSVW and CSVW-EO conventions used for privacy-preserving data synthesis and differential privacy pipelines.
attach_partitions_to_column(df: pd.DataFrame, column_meta: ColumnMetadata, column_name: str, privacy_unit: str, continuous_partitions: dict[str, list[Any]], col_contrib_level: ContributionLevel) -> None
¶
Compute and attach partition metadata for a column.
Depending on the contribution level, partitions may be stored either as full partition objects (partition-level contributions) or as a simplified list of public partition keys (column-level contributions).
Categorical columns are partitioned by unique values, while numeric columns are discretized using provided bin boundaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataset. |
required |
column_meta
|
ColumnMetadata
|
Metadata object that will be updated with partition information. |
required |
column_name
|
str
|
Name of the column being partitioned. |
required |
privacy_unit
|
str
|
Column representing the privacy unit. |
required |
continuous_partitions
|
dict[str, list[Any]]
|
Mapping of numeric column names to bin boundaries. |
required |
col_contrib_level
|
ContributionLevel
|
Contribution granularity applied to the column. |
required |
Returns:
| Type | Description |
|---|---|
None
|
The function modifies |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
build_base_column_group_kwargs(col_group: list[str], partitions_meta: list[MultiColumnPartition]) -> dict[str, Any]
¶
Build the default keyword arguments for a column group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col_group
|
list[str]
|
List of column names belonging to the column group. |
required |
partitions_meta
|
list[MultiColumnPartition]
|
Metadata describing the available multi-column partitions. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing the default configuration for the column group, including partition information and invariants. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
build_column_metadata(df: pd.DataFrame, column_name: str, privacy_unit: str, continuous_partitions: dict[str, list[Any]], fine_contributions_level: dict[str, ContributionLevel], default_contributions_level: ContributionLevel, with_dependencies: bool) -> ColumnMetadata
¶
Construct metadata for a single column.
This function infers column properties and computes metadata fields required by CSVW-EO, including datatype inference, nullability, dependencies, fixed-per-entity attributes, and optional contribution partitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataset. |
required |
column_name
|
str
|
Name of the column being processed. |
required |
privacy_unit
|
str
|
Name of the column representing the privacy unit. |
required |
continuous_partitions
|
dict[str, list[Any]]
|
Mapping of numeric column names to partition bin boundaries. |
required |
fine_contributions_level
|
dict[str, str]
|
Mapping specifying per-column contribution levels. default_contributions_level : ContributionLevel
Default contribution level applied when a column is not explicitly
listed in |
required |
default_contributions_level
|
ContributionLevel
|
Default contribution bound level used when a column is not present
in |
required |
with_dependencies
|
bool
|
Whether to compute and attach dependency information for the column. |
required |
Returns:
| Type | Description |
|---|---|
ColumnMetadata
|
Metadata object describing the column according to the CSVW-EO specification. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | |
build_partitions(df: pd.DataFrame, privacy_unit: str, column_specs: list[dict[str, Any]]) -> list[Partition]
¶
Build CSVW-EO partitions and compute contribution bounds per partition.
This function groups the dataset according to the provided column specifications and calculates metadata required by CSVW-EO, including maximum partition size and per-privacy-unit contribution bounds.
Numeric columns are first discretized into bins before grouping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataset. |
required |
privacy_unit
|
str
|
Column name representing the privacy unit (e.g., patient_id). |
required |
column_specs
|
list of dict
|
Specifications describing how each column should be partitioned. Each specification must contain: - "name": column name - "kind": either "categorical" or "numeric" Optional keys: - "bins": list of numeric or datetime boundaries (for numeric columns) - "is_datetime": bool indicating datetime values Example¶[ {"name": "species", "kind": "categorical"}, {"name": "age", "kind": "numeric", "bins": [0, 10, 20, 30]} ] |
required |
Returns:
| Type | Description |
|---|---|
list of dict
|
A list of CSVW-EO partition metadata objects. Each entry contains:
|
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.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 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 | |
get_column_level_contribution(partitions_meta: list[SingleColumnPartition] | list[MultiColumnPartition]) -> tuple[int, int, int]
¶
Compute maximum contribution over all partition of column.
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
418 419 420 421 422 423 424 425 426 | |
get_continuous_bounds(series: pd.Series) -> tuple[T, T]
¶
Compute minimum and maximum values for continuous columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Input series containing continuous numeric values. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
(min_value, max_value) |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
get_multi_group_partitions(df: pd.DataFrame, col_group: list[str], continuous_partitions: dict[str, list[Any]], privacy_unit: str) -> list[MultiColumnPartition]
¶
Generate multi-column partitions for a group of columns.
Columns are classified as either continuous or categorical depending on the provided partition configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataframe containing the data to partition. |
required |
col_group
|
list[str]
|
List of column names used for grouping. |
required |
continuous_partitions
|
dict[str, list[Any]]
|
Mapping of continuous column names to their partition bins. |
required |
privacy_unit
|
str
|
Name of the privacy unit column. |
required |
Returns:
| Type | Description |
|---|---|
list[MultiColumnPartition]
|
List of generated multi-column partitions. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
identify_dependency(df: pd.DataFrame, column_name: str, max_mapping_keys: int = 25, max_mapping_values: int = 10) -> list[Dependency]
¶
Detect dependencies between columns.
This includes: - inequality relationships - deterministic mappings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataframe used for dependency detection. |
required |
column_name
|
str
|
Target column. |
required |
max_mapping_keys
|
int
|
Maximum allowed keys in mapping. |
25
|
max_mapping_values
|
int
|
Maximum allowed values in a key in a mapping. |
10
|
Returns:
| Type | Description |
|---|---|
list
|
Dependency descriptions. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
main() -> None
¶
Command-line entry point for generating CSVW-EO metadata.
This function parses command-line arguments, loads the input CSV dataset, performs basic datatype inference (including datetime detection), and generates CSVW-EO metadata describing the dataset structure, privacy unit, contribution bounds, and optional partitions.
The resulting metadata is written as a JSON file.
Command-line arguments
csv_file : str Path to the input CSV dataset.
--output : str, optional Output JSON file where the generated metadata will be written. Default is "metadata.json".
--privacy-unit : str Name of the column representing the privacy unit (e.g., patient_id).
--with_dependencies: bool. Default is True.
--continuous_partitions : str, optional JSON string specifying bin boundaries for continuous columns.
--column_groups : str, optional JSON string specifying groups of columns for joint partitioning.
--default_contributions_level : {"table", "table_with_keys", "column", "partition"}, optional Default contribution bound level applied to columns.
--fine_contributions_level : str, optional JSON string specifying column-specific contribution levels.
Notes
Datetime inference is attempted automatically for all columns by
attempting to parse values using pandas.to_datetime.
The generated metadata conforms to the CSVW-EO specification and can be used by downstream privacy-preserving data synthesis systems.
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
make_categorical_partitions(df: pd.DataFrame, privacy_unit: str, column_name: str) -> list[SingleColumnPartition]
¶
Generate partitions for a categorical column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataframe containing the data to partition. |
required |
privacy_unit
|
str
|
Name of the privacy unit column. |
required |
column_name
|
str
|
Name of the categorical column. |
required |
Returns:
| Type | Description |
|---|---|
list[SingleColumnPartition]
|
List of generated partitions for the categorical column. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
make_column_groups(df: pd.DataFrame, column_groups: list[list[str]], fine_contributions_level: dict[str, ContributionLevel], default_contributions_level: ContributionLevel, continuous_partitions: dict[str, list[Any]], privacy_unit: str) -> list[ColumnGroupMetadata]
¶
Build CSVW-EO metadata for column groups.
A column group represents a set of columns that should be treated jointly when defining contribution bounds and partitions. Partitions are computed over the joint values of the columns in the group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataset. |
required |
column_groups
|
list[list[str]]
|
List of column groups. Each group is a list of column names that should be treated jointly. |
required |
fine_contributions_level
|
dict[str, ContributionLevel]
|
Mapping specifying contribution bound levels for specific columns.
Values must be either |
required |
default_contributions_level
|
ContributionLevel
|
Default contribution bound level used when a column is not present
in |
required |
continuous_partitions
|
dict[str, list[Any]]
|
Mapping of numeric column names to bin boundaries used for generating partitions. |
required |
privacy_unit
|
str
|
Name of the column representing the privacy unit (e.g., user_id). |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
A list of CSVW-EO column group metadata dictionaries including partition definitions and contribution bounds. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.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 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 | |
make_metadata_from_data(df: pd.DataFrame, privacy_unit: str, with_dependencies: bool = True, continuous_partitions: dict[str, list[Any]] | None = None, column_groups: list[list[str]] | None = None, default_contributions_level: str = 'table', fine_contributions_level: dict[str, str] | None = None) -> dict[str, Any]
¶
Generate CSVW-EO metadata from a dataset and return JSON-serializable dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataset. |
required |
with_dependencies
|
bool
|
Boolean if add dependencies between columns |
True
|
privacy_unit
|
str
|
Column identifying the privacy unit. |
required |
continuous_partitions
|
dict
|
Numeric partition boundaries. |
None
|
column_groups
|
list
|
Column groups to generate joint partitions. |
None
|
default_contributions_level
|
str
|
Default contribution level ("table", "column", "partition"). |
'table'
|
fine_contributions_level
|
dict
|
Per-column override for contribution level. |
None
|
Returns:
| Type | Description |
|---|---|
TableMetadata
|
CSVW-EO metadata structure as a dataclass. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
make_numeric_partitions(df: pd.DataFrame, privacy_unit: str, column_name: str, bounds: list[Any]) -> list[SingleColumnPartition]
¶
Generate partitions for a numeric column using predefined bounds.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
Input dataframe containing the data to partition. |
required |
privacy_unit
|
str
|
Name of the privacy unit column. |
required |
column_name
|
str
|
Name of the numeric column. |
required |
bounds
|
list[Any]
|
List of partition boundaries used to define bins. |
required |
Returns:
| Type | Description |
|---|---|
list[SingleColumnPartition]
|
List of generated partitions for the numeric column. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
make_predicate(spec: dict[str, Any], value: Any) -> Predicate
¶
Build a Predicate object from a column specification and a partition value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict
|
Column specification containing "kind" and optionally "is_datetime". |
required |
value
|
Any
|
Partition value, either a category or a numeric interval. |
required |
Returns:
| Type | Description |
|---|---|
Predicate
|
Dataclass representing the partition predicate. |
Source code in csvw-eo-library/src/csvw_eo/make_metadata_from_data.py
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 | |
Supporting Utilities¶
csvw_eo.datatypes
¶
CSVW-EO DataTypes Utilities.
ColumnKind
¶
Bases: StrEnum
Partition Kind.
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
12 13 14 15 16 | |
DataTypes
¶
Bases: StrEnum
Precise column types for metadata.
Source code in csvw-eo-library/src/csvw_eo/datatypes.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 | |
DataTypesGroups
¶
Bases: StrEnum
Column types main groups for metadata.
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
22 23 24 25 26 27 28 29 30 | |
infer_xmlschema_datatype(series: pd.Series) -> DataTypes
¶
Infer the most appropriate XML Schema datatype for a pandas series.
The inference considers pandas dtypes, string parsing, and fallback heuristics for object types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Input column to analyze. |
required |
Returns:
| Type | Description |
|---|---|
DataTypes
|
Inferred XML Schema datatype. |
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
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 | |
is_categorical(series: pd.Series, max_unique: int = 20) -> bool
¶
Determine whether a series should be treated as categorical.
A series is considered categorical if it is of a categorical-like type (string/boolean) or has a small number of unique values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Input column. |
required |
max_unique
|
int
|
Maximum number of unique values allowed for categorical inference. |
20
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the series is categorical, False otherwise. |
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
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 | |
is_continuous(series: pd.Series, max_unique: int = 20) -> bool
¶
Determine whether a column should be modeled as continuous.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Input column. |
required |
max_unique
|
int
|
Maximum number of unique values to treat as categorical-like. |
20
|
Returns:
| Type | Description |
|---|---|
bool
|
True if the column should be treated as continuous. |
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
is_date(value: str) -> bool
¶
Infer if value is a date in YYYY-MM-DD format.
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
101 102 103 104 105 106 107 108 109 | |
is_datetime(value: str) -> bool
¶
Infer if value is a datetime (ISO 8601 format).
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
112 113 114 115 116 117 118 119 120 | |
refine_integer_type(series: pd.Series) -> DataTypes
¶
Refine an integer series into a more specific XML Schema datatype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
series
|
Series
|
Input integer series. |
required |
Returns:
| Type | Description |
|---|---|
DataTypes
|
The most specific integer subtype (e.g., positiveInteger, negativeInteger, or integer). |
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
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 | |
to_pandas_dtype(csvw_type: DataTypes) -> str
¶
Convert a CSVW XML Schema datatype to a pandas dtype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvw_type
|
DataTypes
|
XML Schema datatype. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Equivalent pandas dtype string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the datatype is missing or invalid. |
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
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 | |
to_snsql_datatype(csvw_type: DataTypes) -> str
¶
Convert a CSVW XML Schema datatype to a SmartNoise SQL datatype.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
csvw_type
|
DataTypes
|
XML Schema datatype. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Equivalent SmartNoise SQL datatype. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the datatype is missing or invalid. |
Source code in csvw-eo-library/src/csvw_eo/datatypes.py
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 | |