DerivaModel
The DerivaModel class provides schema introspection and manipulation capabilities for Deriva catalogs. It handles table relationships, associations, and catalog structure management.
Model module for DerivaML.
This module provides catalog and database model classes, as well as handle wrappers for ERMrest model objects and annotation builders.
Key components: - DerivaModel: Schema analysis utilities - DatabaseModel: SQLite database from BDBag - SchemaBuilder/SchemaORM: Create ORM from Deriva Model (Phase 1) - DataLoader: Fill database from data source (Phase 2) - DataSource: Protocol for data sources (BagDataSource, CatalogDataSource) - ForeignKeyOrderer: Compute FK-safe insertion order
Lazy imports are used for DatabaseModel and DerivaMLDatabase to avoid circular imports with the dataset module.
Aggregate
Bases: str, Enum
Aggregation functions for pseudo-columns.
Used when a pseudo-column follows an inbound foreign key and returns multiple values that need to be aggregated.
Attributes:
| Name | Type | Description |
|---|---|---|
MIN |
Minimum value |
|
MAX |
Maximum value |
|
CNT |
Count of values |
|
CNT_D |
Count of distinct values |
|
ARRAY |
Array of all values |
|
ARRAY_D |
Array of distinct values |
Example
Count related records
pc = PseudoColumn( ... source=[InboundFK("domain", "Sample_Subject_fkey"), "RID"], ... aggregate=Aggregate.CNT, ... markdown_name="Sample Count" ... )
Get distinct values as array
pc = PseudoColumn( ... source=[InboundFK("domain", "Tag_Item_fkey"), "Name"], ... aggregate=Aggregate.ARRAY_D, ... markdown_name="Tags" ... )
Source code in src/deriva_ml/model/annotations.py
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 | |
ArrayUxMode
Bases: str, Enum
Display modes for array values in pseudo-columns.
Controls how arrays of values are rendered in the UI.
Attributes:
| Name | Type | Description |
|---|---|---|
RAW |
Raw array display |
|
CSV |
Comma-separated values |
|
OLIST |
Ordered (numbered) list |
|
ULIST |
Unordered (bulleted) list |
Example
pc = PseudoColumn( ... source=[InboundFK("domain", "Tag_Item_fkey"), "Name"], ... aggregate=Aggregate.ARRAY, ... display=PseudoColumnDisplay(array_ux_mode=ArrayUxMode.CSV) ... )
Source code in src/deriva_ml/model/annotations.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
BagDataSource
DataSource implementation for BDBag directories.
Reads data from CSV files in a bag's data/ directory. Handles asset URL localization via fetch.txt.
Example
source = BagDataSource(Path("/path/to/bag"))
List available tables
print(source.list_available_tables())
Get data for a table
for row in source.get_table_data("Image"): print(row["Filename"])
Source code in src/deriva_ml/model/data_sources.py
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 | |
__init__
__init__(
bag_path: Path,
model: Model | None = None,
asset_localization: bool = True,
)
Initialize from a bag path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bag_path
|
Path
|
Path to BDBag directory. |
required |
model
|
Model | None
|
Optional ERMrest Model for schema info. If not provided, will try to load from bag's schema.json. |
None
|
asset_localization
|
bool
|
Whether to localize asset URLs to local paths using fetch.txt mapping. |
True
|
Source code in src/deriva_ml/model/data_sources.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 131 132 133 134 | |
get_row_count
get_row_count(
table: Table | str,
) -> int
Get the number of rows across all CSV files for a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of data rows (excluding headers). |
Source code in src/deriva_ml/model/data_sources.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | |
get_table_data
get_table_data(
table: Table | str,
) -> Iterator[dict[str, Any]]
Read table data from CSV files.
Nested datasets may produce multiple CSV files for the same table at different directory depths. This method yields rows from all of them so that the full dataset (including parent and child records) is loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name. |
required |
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary per row with column names as keys. |
Source code in src/deriva_ml/model/data_sources.py
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 | |
has_table
has_table(table: Table | str) -> bool
Check if CSV exists for table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if CSV file exists for this table. |
Source code in src/deriva_ml/model/data_sources.py
244 245 246 247 248 249 250 251 252 253 254 | |
list_available_tables
list_available_tables() -> list[str]
List all CSV files in data directory.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of table names (without .csv extension). |
Source code in src/deriva_ml/model/data_sources.py
256 257 258 259 260 261 262 | |
CatalogDataSource
DataSource implementation for remote Deriva catalog.
Fetches data via ERMrest API / datapath with pagination support.
Example
catalog = server.connect_ermrest(catalog_id) source = CatalogDataSource(catalog, schemas=['domain', 'deriva-ml'])
List available tables
print(source.list_available_tables())
Get data for a table
for row in source.get_table_data("Image"): print(row["Filename"])
Source code in src/deriva_ml/model/data_sources.py
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 | |
__init__
__init__(
catalog: ErmrestCatalog,
schemas: list[str],
batch_size: int = 1000,
)
Initialize from catalog connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog
|
ErmrestCatalog
|
ERMrest catalog connection. |
required |
schemas
|
list[str]
|
Schemas to fetch data from. |
required |
batch_size
|
int
|
Number of rows per API request. |
1000
|
Source code in src/deriva_ml/model/data_sources.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
get_row_count
get_row_count(
table: Table | str,
) -> int
Get the number of rows in a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name. |
required |
Returns:
| Type | Description |
|---|---|
int
|
Number of rows in the table. |
Source code in src/deriva_ml/model/data_sources.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
get_table_data
get_table_data(
table: Table | str,
) -> Iterator[dict[str, Any]]
Fetch table data via ERMrest API.
Uses pagination to handle large tables efficiently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name. |
required |
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary per row with column names as keys. |
Source code in src/deriva_ml/model/data_sources.py
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 | |
has_table
has_table(table: Table | str) -> bool
Check if table exists in catalog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if table exists in configured schemas. |
Source code in src/deriva_ml/model/data_sources.py
404 405 406 407 408 409 410 411 412 413 | |
list_available_tables
list_available_tables() -> list[str]
List all tables in configured schemas.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of fully-qualified table names (schema.table). |
Source code in src/deriva_ml/model/data_sources.py
415 416 417 418 419 420 421 422 423 424 425 426 427 | |
ColumnDisplay
dataclass
Bases: AnnotationBuilder
Column-display annotation builder.
Controls how column values are rendered.
Example
cd = ColumnDisplay() cd.default(ColumnDisplayOptions( ... pre_format=PreFormat(format="%.2f") ... ))
Markdown link
cd = ColumnDisplay() cd.default(ColumnDisplayOptions( ... markdown_pattern="Link" ... ))
Source code in src/deriva_ml/model/annotations.py
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 | |
compact
compact(
options: ColumnDisplayOptions,
) -> "ColumnDisplay"
Set options for compact view.
Source code in src/deriva_ml/model/annotations.py
1132 1133 1134 | |
default
default(
options: ColumnDisplayOptions,
) -> "ColumnDisplay"
Set default options.
Source code in src/deriva_ml/model/annotations.py
1128 1129 1130 | |
detailed
detailed(
options: ColumnDisplayOptions,
) -> "ColumnDisplay"
Set options for detailed view.
Source code in src/deriva_ml/model/annotations.py
1136 1137 1138 | |
set_context
set_context(
context: str,
options: ColumnDisplayOptions | str,
) -> "ColumnDisplay"
Set options for a context.
Source code in src/deriva_ml/model/annotations.py
1119 1120 1121 1122 1123 1124 1125 1126 | |
ColumnDisplayOptions
dataclass
Options for displaying a column in a specific context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pre_format
|
PreFormat | None
|
Pre-formatting options |
None
|
markdown_pattern
|
str | None
|
Template for rendering |
None
|
template_engine
|
TemplateEngine | None
|
Template engine to use |
None
|
column_order
|
list[SortKey] | Literal[False] | None
|
Sort order, or False to disable |
None
|
Source code in src/deriva_ml/model/annotations.py
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 | |
DataLoader
Loads data into a database with FK ordering.
Phase 2 of the two-phase database creation pattern. Takes a SchemaORM (from Phase 1) and populates it from a DataSource.
Automatically orders tables by FK dependencies to ensure referential integrity during loading.
Example
Phase 1: Create ORM
orm = SchemaBuilder(model, schemas).build()
Phase 2: Fill with data from bag
source = BagDataSource(bag_path) loader = DataLoader(orm, source) counts = loader.load_tables() # All tables print(f"Loaded {sum(counts.values())} total rows")
Or load specific tables
counts = loader.load_tables(['Subject', 'Image'])
With progress callback
def on_progress(table, count, total): print(f"Loaded {table}: {count} rows") loader.load_tables(progress_callback=on_progress)
Source code in src/deriva_ml/model/data_loader.py
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 | |
__init__
__init__(
schema_orm: SchemaORM,
data_source: DataSource,
)
Initialize the loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_orm
|
SchemaORM
|
ORM structure from SchemaBuilder. |
required |
data_source
|
DataSource
|
Source of data to load (BagDataSource, CatalogDataSource, etc.). |
required |
Source code in src/deriva_ml/model/data_loader.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
get_load_order
get_load_order(
tables: list[str | Table]
| None = None,
) -> list[str]
Get the FK-safe load order for tables without loading.
Useful for previewing or manually controlling load order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
list[str | Table] | None
|
Tables to order. If None, orders all available. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of table names in safe insertion order. |
Source code in src/deriva_ml/model/data_loader.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
load_table
load_table(
table: str | Table,
on_conflict: str = "ignore",
batch_size: int = 1000,
) -> int
Load a single table (without FK ordering).
Use this when you know the dependencies are already satisfied or for loading a single table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str | Table
|
Table to load. |
required |
on_conflict
|
str
|
Conflict handling strategy. |
'ignore'
|
batch_size
|
int
|
Rows per batch. |
1000
|
Returns:
| Type | Description |
|---|---|
int
|
Number of rows loaded. |
Source code in src/deriva_ml/model/data_loader.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
load_tables
load_tables(
tables: list[str | Table]
| None = None,
on_conflict: str = "ignore",
batch_size: int = 1000,
progress_callback: Callable[
[str, int, int], None
]
| None = None,
) -> dict[str, int]
Load data into specified tables with FK ordering.
Tables are automatically ordered by FK dependencies to ensure referenced tables are populated first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
list[str | Table] | None
|
Tables to load. If None, loads all tables that have data in the source. |
None
|
on_conflict
|
str
|
How to handle duplicate keys: - "ignore": Skip rows with duplicate keys (default) - "replace": Replace existing rows - "error": Raise error on duplicates |
'ignore'
|
batch_size
|
int
|
Number of rows per insert batch. |
1000
|
progress_callback
|
Callable[[str, int, int], None] | None
|
Optional callback(table_name, rows_loaded, total_tables) called after each table is loaded. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Dict mapping table names to row counts loaded. |
Source code in src/deriva_ml/model/data_loader.py
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 | |
validate_load_order
validate_load_order(
tables: list[str | Table],
) -> list[tuple[str, str, str]]
Validate that tables can be loaded in the given order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
list[str | Table]
|
Ordered list of tables. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str]]
|
List of FK violations as (table, missing_dep, fk_name) tuples. |
list[tuple[str, str, str]]
|
Empty if order is valid. |
Source code in src/deriva_ml/model/data_loader.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
DataSource
Bases: Protocol
Protocol for data sources that can fill a database.
Implementations provide data for populating SQLite tables from different sources (bags, remote catalogs, etc.).
This is used with DataLoader in Phase 2 of the two-phase pattern.
Source code in src/deriva_ml/model/data_sources.py
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 | |
get_table_data
get_table_data(
table: Table | str,
) -> Iterator[dict[str, Any]]
Yield rows for a table as dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name to get data for. |
required |
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary per row with column names as keys. |
Source code in src/deriva_ml/model/data_sources.py
49 50 51 52 53 54 55 56 57 58 59 60 61 | |
has_table
has_table(table: Table | str) -> bool
Check if this source has data for the table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | str
|
Table object or name to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if data is available for this table. |
Source code in src/deriva_ml/model/data_sources.py
63 64 65 66 67 68 69 70 71 72 | |
list_available_tables
list_available_tables() -> list[str]
List tables with available data.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of table names (may include schema prefix). |
Source code in src/deriva_ml/model/data_sources.py
74 75 76 77 78 79 80 | |
DerivaModel
Augmented interface to deriva model class.
This class provides a number of DerivaML specific methods that augment the interface in the deriva model class.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
ERMRest model for the catalog. |
|
catalog |
ErmrestCatalog
|
ERMRest catalog for the model. |
hostname |
Hostname of the ERMRest server. |
|
ml_schema |
The ML schema name for the catalog. |
|
domain_schemas |
Frozenset of all domain schema names in the catalog. |
|
default_schema |
The default schema for table creation operations. |
Source code in src/deriva_ml/model/catalog.py
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 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 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 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 | |
chaise_config
property
chaise_config: dict[str, Any]
Return the chaise configuration.
__init__
__init__(
model: Model,
ml_schema: str = ML_SCHEMA,
domain_schemas: str
| set[str]
| None = None,
default_schema: str | None = None,
)
Create and initialize a DerivaModel instance.
This method will connect to a catalog and initialize schema configuration. This class is intended to be used as a base class on which domain-specific interfaces are built.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
The ERMRest model for the catalog. |
required |
ml_schema
|
str
|
The ML schema name. |
ML_SCHEMA
|
domain_schemas
|
str | set[str] | None
|
Optional explicit set of domain schema names. If None, auto-detects all non-system schemas. |
None
|
default_schema
|
str | None
|
The default schema for table creation operations. If None and there is exactly one domain schema, that schema is used as default. If there are multiple domain schemas, default_schema must be specified. |
None
|
Source code in src/deriva_ml/model/catalog.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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
apply
apply() -> None
Call ERMRestModel.apply
Source code in src/deriva_ml/model/catalog.py
671 672 673 674 675 676 | |
asset_metadata
asset_metadata(
table: str | Table,
) -> set[str]
Return the metadata columns for an asset table.
Source code in src/deriva_ml/model/catalog.py
637 638 639 640 641 642 643 644 | |
asset_metadata_columns
asset_metadata_columns(
table: str | Table,
) -> list[Column]
Return Column objects for the asset-metadata columns of table.
Like :meth:asset_metadata but returns the :class:Column
instances (not just names) so callers can inspect attributes
such as nullok. Results are sorted by column name for
deterministic iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str | Table
|
Asset table name or Table object. |
required |
Returns:
| Type | Description |
|---|---|
list[Column]
|
Sorted list of Column objects. |
Raises:
| Type | Description |
|---|---|
DerivaMLTableTypeError
|
If |
Source code in src/deriva_ml/model/catalog.py
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 | |
create_table
create_table(
table_def: TableDefinition,
schema: str | None = None,
) -> Table
Create a new table from TableDefinition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_def
|
TableDefinition
|
Table definition (dataclass or dict). |
required |
schema
|
str | None
|
Schema to create the table in. If None, uses default_schema. |
None
|
Returns:
| Type | Description |
|---|---|
Table
|
The newly created Table. |
Raises:
| Type | Description |
|---|---|
DerivaMLException
|
If no schema specified and default_schema is not set. |
Note: @validate_call removed because TableDefinition is now a dataclass from deriva.core.typed and Pydantic validation doesn't work well with dataclass fields.
Source code in src/deriva_ml/model/catalog.py
2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 | |
find_assets
find_assets(
with_metadata: bool = False,
) -> list[Table]
Return the list of asset tables in the current model
Source code in src/deriva_ml/model/catalog.py
556 557 558 | |
find_association
find_association(
table1: Table | str,
table2: Table | str,
) -> tuple[Table, Column, Column]
Given two tables, return an association table that connects the two and the two columns used to link them..
Source code in src/deriva_ml/model/catalog.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | |
find_features
find_features(
table: TableInput | None = None,
) -> Iterable[Feature]
List features in the catalog.
If a table is specified, returns only features for that table. If no table is specified, returns all features across all tables in the catalog.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TableInput | None
|
Optional table to find features for. If None, returns all features in the catalog. |
None
|
Returns:
| Type | Description |
|---|---|
Iterable[Feature]
|
An iterable of Feature instances describing the features. |
Source code in src/deriva_ml/model/catalog.py
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 | |
find_vocabularies
find_vocabularies() -> list[Table]
Return a list of all controlled vocabulary tables in domain and ML schemas.
Source code in src/deriva_ml/model/catalog.py
560 561 562 563 564 565 566 567 | |
from_cached
classmethod
from_cached(
schema_dict: dict,
*,
catalog,
ml_schema: str = ML_SCHEMA,
domain_schemas: "str | set[str] | None" = None,
default_schema: "str | None" = None,
) -> "DerivaModel"
Construct a DerivaModel from a cached ermrest /schema dict.
No network is touched. The catalog argument is passed to
deriva-py's Model(catalog, model_doc) constructor as the
first positional argument; in offline mode it will be a
:class:~deriva_ml.core.catalog_stub.CatalogStub, in online
mode it is a real ErmrestCatalog. DerivaModel.__init__
then reads the catalog back off model.catalog as usual.
This replicates what Model.fromcatalog(catalog) does
online — the online call fetches
catalog.get("/schema").json() and passes the result to
Model(catalog, dict). Here we pass in the already-cached
dict from :class:~deriva_ml.core.schema_cache.SchemaCache.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_dict
|
dict
|
The JSON payload from a previous
|
required |
catalog
|
The catalog object to associate with the model.
Pass a real |
required | |
ml_schema
|
str
|
ML schema name (default |
ML_SCHEMA
|
domain_schemas
|
'str | set[str] | None'
|
Optional explicit set of domain schema names. If None, auto-detects all non-system schemas from the cached dict. |
None
|
default_schema
|
'str | None'
|
Optional default schema name. |
None
|
Returns:
| Type | Description |
|---|---|
'DerivaModel'
|
A |
'DerivaModel'
|
reconstructed from the dict. |
Source code in src/deriva_ml/model/catalog.py
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 | |
get_schema_description
get_schema_description(
include_system_columns: bool = False,
) -> dict[str, Any]
Return a JSON description of the catalog schema structure.
Provides a structured representation of the domain and ML schemas including tables, columns, foreign keys, and relationships. Useful for understanding the data model structure programmatically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
include_system_columns
|
bool
|
If True, include RID, RCT, RMT, RCB, RMB columns. Default False to reduce output size. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with schema structure: |
dict[str, Any]
|
{ "domain_schemas": ["schema_name1", "schema_name2"], "default_schema": "schema_name1", "ml_schema": "deriva-ml", "schemas": { "schema_name": { "tables": { "TableName": { "comment": "description", "is_vocabulary": bool, "is_asset": bool, "is_association": bool, "columns": [...], "foreign_keys": [...], "features": [...] } } } } |
dict[str, Any]
|
} |
Source code in src/deriva_ml/model/catalog.py
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 | |
is_asset
is_asset(
table_name: TableInput,
) -> bool
True if the specified table is a proper asset table.
Delegates to Table.is_asset() from deriva-py which checks: - Required columns exist (URL, Filename, Length, MD5) - URL, Length, MD5 are NOT NULL - URL has the asset annotation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
TableInput
|
str | Table |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the specified table is a proper asset table. |
Source code in src/deriva_ml/model/catalog.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
is_association
is_association(
table_name: str | Table,
unqualified: bool = True,
pure: bool = True,
min_arity: int = 2,
max_arity: int = 2,
) -> bool | set[str] | int
Check the specified table to see if it is an association table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str | Table
|
param unqualified: |
required |
pure
|
bool
|
return: (Default value = True) |
True
|
table_name
|
str | Table
|
str | Table: |
required |
unqualified
|
bool
|
(Default value = True) |
True
|
Returns:
Source code in src/deriva_ml/model/catalog.py
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
is_dataset_rid
is_dataset_rid(
rid: RID, deleted: bool = False
) -> bool
Check if a given RID is a dataset RID.
Source code in src/deriva_ml/model/catalog.py
678 679 680 681 682 683 684 685 686 687 688 689 690 | |
is_domain_schema
is_domain_schema(
schema_name: str,
) -> bool
Check if a schema is a domain schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_name
|
str
|
Name of the schema to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the schema is a domain schema. |
Source code in src/deriva_ml/model/catalog.py
271 272 273 274 275 276 277 278 279 280 | |
is_system_schema
is_system_schema(
schema_name: str,
) -> bool
Check if a schema is a system or ML schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema_name
|
str
|
Name of the schema to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the schema is a system or ML schema. |
Source code in src/deriva_ml/model/catalog.py
260 261 262 263 264 265 266 267 268 269 | |
is_vocabulary
is_vocabulary(
table_name: TableInput,
) -> bool
Check if a given table is a controlled vocabulary table.
Delegates to Table.is_vocabulary() in deriva-py, which enforces both
the required column names AND their types (ermrest_curie, ermrest_uri,
text, markdown). The type check is stricter than a column-name-only
check — a table with an ID column of the wrong type correctly
returns False here where the legacy name-only implementation would
have returned True.
Mirrors :meth:is_asset, which already delegates to Table.is_asset().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
TableInput
|
An ERMrest Table object or the name of the table. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the table has the structure of a controlled vocabulary, |
bool
|
False otherwise. |
Raises:
| Type | Description |
|---|---|
DerivaMLException
|
if the table doesn't exist. |
Source code in src/deriva_ml/model/catalog.py
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
list_dataset_element_types
list_dataset_element_types() -> (
list[Table]
)
Lists the data types of elements contained within a dataset.
This method analyzes the dataset and identifies the data types for all elements within it. It is useful for understanding the structure and content of the dataset and allows for better manipulation and usage of its data.
Returns:
| Type | Description |
|---|---|
list[Table]
|
list[str]: A list of strings where each string represents a data type |
list[Table]
|
of an element found in the dataset. |
Source code in src/deriva_ml/model/catalog.py
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 | |
lookup_feature
lookup_feature(
table: TableInput, feature_name: str
) -> Feature
Lookup the named feature associated with the provided table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TableInput
|
param feature_name: |
required |
table
|
TableInput
|
str | Table: |
required |
feature_name
|
str
|
str: |
required |
Returns:
| Type | Description |
|---|---|
Feature
|
A Feature class that represents the requested feature. |
Raises:
| Type | Description |
|---|---|
DerivaMLException
|
If the feature cannot be found. |
Source code in src/deriva_ml/model/catalog.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | |
name_to_table
name_to_table(
table: TableInput,
) -> Table
Return the table object corresponding to the given table name.
Searches domain schemas first (in sorted order), then ML schema, then WWW. If the table name appears in more than one schema, returns the first match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
TableInput
|
A ERMRest table object or a string that is the name of the table. |
required |
Returns:
| Type | Description |
|---|---|
Table
|
Table object. |
Raises:
| Type | Description |
|---|---|
DerivaMLException
|
If the table doesn't exist in any searchable schema. |
Source code in src/deriva_ml/model/catalog.py
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 | |
vocab_columns
vocab_columns(
table_name: TableInput,
) -> dict[str, str]
Return mapping from canonical vocab column name to actual column name.
Canonical names are TitleCase (Name, ID, URI, Description, Synonyms). Actual names reflect the table's schema — could be lowercase for FaceBase-style catalogs or TitleCase for DerivaML-native tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
TableInput
|
A table object or the name of the table. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dict mapping canonical name to actual column name in the table. |
dict[str, str]
|
E.g. |
dict[str, str]
|
or |
Source code in src/deriva_ml/model/catalog.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | |
Display
dataclass
Bases: AnnotationBuilder
Display annotation for tables and columns.
Controls the display name, description/tooltip, and how null values and foreign key links are rendered. Can be applied to both tables and columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
Display name shown in the UI (mutually exclusive with markdown_name) |
None
|
markdown_name
|
str | None
|
Markdown-formatted display name (mutually exclusive with name) |
None
|
name_style
|
NameStyle | None
|
Styling options for automatic name formatting |
None
|
comment
|
str | None
|
Description text shown as tooltip/help text |
None
|
show_null
|
dict[str, bool | str] | None
|
How to display null values, per context |
None
|
show_foreign_key_link
|
dict[str, bool] | None
|
Whether to show FK values as links, per context |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both name and markdown_name are provided |
Example
Basic display name::
>>> display = Display(name="Research Subjects") # doctest: +SKIP
>>> handle.set_annotation(display)
With description/tooltip::
>>> display = Display(
... name="Subjects",
... comment="Individuals enrolled in research studies"
... )
Markdown-formatted name::
>>> display = Display(markdown_name="**Bold** _Italic_ Name")
Context-specific null display::
>>> from deriva_ml.model import CONTEXT_COMPACT, CONTEXT_DETAILED
>>> display = Display(
... name="Value",
... show_null={
... CONTEXT_COMPACT: False, # Hide nulls in lists
... CONTEXT_DETAILED: '"N/A"' # Show "N/A" string
... }
... )
Control foreign key link display::
>>> display = Display(
... name="Subject",
... show_foreign_key_link={CONTEXT_COMPACT: False}
... )
Source code in src/deriva_ml/model/annotations.py
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 | |
Facet
dataclass
A facet definition for filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | list[str | InboundFK | OutboundFK] | None
|
Path to source data |
None
|
sourcekey
|
str | None
|
Reference to named source |
None
|
markdown_name
|
str | None
|
Display name |
None
|
comment
|
str | None
|
Description |
None
|
entity
|
bool | None
|
Whether this is an entity facet |
None
|
open
|
bool | None
|
Start expanded |
None
|
ux_mode
|
FacetUxMode | None
|
UI mode (choices, ranges, check_presence) |
None
|
bar_plot
|
bool | None
|
Show bar plot |
None
|
choices
|
list[Any] | None
|
Preset choice values |
None
|
ranges
|
list[FacetRange] | None
|
Preset range values |
None
|
not_null
|
bool | None
|
Filter to non-null values |
None
|
hide_null_choice
|
bool | None
|
Hide "null" option |
None
|
hide_not_null_choice
|
bool | None
|
Hide "not null" option |
None
|
n_bins
|
int | None
|
Number of bins for histogram |
None
|
Source code in src/deriva_ml/model/annotations.py
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 | |
FacetList
dataclass
A list of facets for filtering (visible_columns.filter).
Example
facets = FacetList([ ... Facet(source="Species", open=True), ... Facet(source="Age", ux_mode=FacetUxMode.RANGES) ... ])
Source code in src/deriva_ml/model/annotations.py
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 | |
add
add(facet: Facet) -> 'FacetList'
Add a facet to the list.
Source code in src/deriva_ml/model/annotations.py
1271 1272 1273 1274 | |
FacetRange
dataclass
A range for facet filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min
|
float | None
|
Minimum value |
None
|
max
|
float | None
|
Maximum value |
None
|
min_exclusive
|
bool | None
|
Exclude min value |
None
|
max_exclusive
|
bool | None
|
Exclude max value |
None
|
Source code in src/deriva_ml/model/annotations.py
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 | |
FacetUxMode
Bases: str, Enum
UX modes for facet filters in the search panel.
Controls how users interact with a facet filter.
Attributes:
| Name | Type | Description |
|---|---|---|
CHOICES |
Checkbox list for selecting values |
|
RANGES |
Range slider/inputs for numeric or date ranges |
|
CHECK_PRESENCE |
Check if value exists or is null |
Example
Choice-based facet
Facet(source="Status", ux_mode=FacetUxMode.CHOICES)
Range-based facet for numeric values
Facet(source="Age", ux_mode=FacetUxMode.RANGES)
Check presence (has value / no value)
Facet(source="Notes", ux_mode=FacetUxMode.CHECK_PRESENCE)
Source code in src/deriva_ml/model/annotations.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
ForeignKeyOrderer
Computes insertion order for tables based on FK dependencies.
Uses topological sort to ensure referenced tables are populated before tables that reference them. Handles cycles by either raising an error or breaking them.
Example
orderer = ForeignKeyOrderer(model, schemas=['domain', 'deriva-ml'])
Get insertion order
tables_to_fill = ['Image', 'Subject', 'Diagnosis'] ordered = orderer.get_insertion_order(tables_to_fill)
Returns: ['Subject', 'Image', 'Diagnosis']
Get all tables in safe order
all_ordered = orderer.get_insertion_order()
Get FK dependencies for a table
deps = orderer.get_dependencies('Image')
Returns: {'Subject', 'Dataset', ...}
Source code in src/deriva_ml/model/fk_orderer.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 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 | |
__init__
__init__(
model: Model, schemas: list[str]
)
Initialize the orderer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
ERMrest Model object. |
required |
schemas
|
list[str]
|
Schemas to consider for FK relationships. |
required |
Source code in src/deriva_ml/model/fk_orderer.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
find_cycles
find_cycles() -> list[list[str]]
Find all FK dependency cycles in the schema.
Returns:
| Type | Description |
|---|---|
list[list[str]]
|
List of cycles, each cycle is a list of table keys. |
Source code in src/deriva_ml/model/fk_orderer.py
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 | |
get_all_tables
get_all_tables() -> list[DerivaTable]
Get all tables in configured schemas.
Returns:
| Type | Description |
|---|---|
list[Table]
|
List of all Table objects. |
Source code in src/deriva_ml/model/fk_orderer.py
353 354 355 356 357 358 359 360 361 362 363 | |
get_deletion_order
get_deletion_order(
tables: list[str | Table]
| None = None,
handle_cycles: bool = True,
) -> list[DerivaTable]
Compute FK-safe deletion order for the given tables.
Returns tables in reverse dependency order - tables that are referenced should be deleted last.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
list[str | Table] | None
|
Tables to order. If None, orders all tables in schemas. |
None
|
handle_cycles
|
bool
|
If True, break cycles. If False, raise on cycles. |
True
|
Returns:
| Type | Description |
|---|---|
list[Table]
|
Ordered list of Table objects (delete from first to last). |
Source code in src/deriva_ml/model/fk_orderer.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
get_dependencies
get_dependencies(
table: str | Table,
) -> set[DerivaTable]
Get tables that this table depends on (FK targets).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str | Table
|
Table name or object. |
required |
Returns:
| Type | Description |
|---|---|
set[Table]
|
Set of tables that must be populated before this table. |
Source code in src/deriva_ml/model/fk_orderer.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
get_dependents
get_dependents(
table: str | Table,
) -> set[DerivaTable]
Get tables that depend on this table (FK sources).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str | Table
|
Table name or object. |
required |
Returns:
| Type | Description |
|---|---|
set[Table]
|
Set of tables that reference this table. |
Source code in src/deriva_ml/model/fk_orderer.py
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 | |
get_insertion_order
get_insertion_order(
tables: list[str | Table]
| None = None,
handle_cycles: bool = True,
) -> list[DerivaTable]
Compute FK-safe insertion order for the given tables.
Returns tables ordered so that all FK dependencies are satisfied when inserting in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
list[str | Table] | None
|
Tables to order. If None, orders all tables in schemas. |
None
|
handle_cycles
|
bool
|
If True, break cycles by removing edges. If False, raise CycleError on cycles. |
True
|
Returns:
| Type | Description |
|---|---|
list[Table]
|
Ordered list of Table objects (insert from first to last). |
Raises:
| Type | Description |
|---|---|
CycleError
|
If handle_cycles=False and cycles exist. |
Source code in src/deriva_ml/model/fk_orderer.py
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 | |
validate_insertion_order
validate_insertion_order(
tables: list[str | Table],
) -> list[tuple[str, str, str]]
Validate that a list of tables can be inserted in order.
Checks each table to ensure all its FK dependencies are satisfied by tables earlier in the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tables
|
list[str | Table]
|
Ordered list of tables to validate. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, str, str]]
|
List of (table, missing_dependency, fk_name) tuples for |
list[tuple[str, str, str]]
|
any unsatisfied dependencies. Empty list if valid. |
Source code in src/deriva_ml/model/fk_orderer.py
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 | |
InboundFK
dataclass
An inbound foreign key path step for pseudo-column source paths.
Use this when following a foreign key FROM another table TO the current table. This is common when counting or aggregating related records.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
str
|
Schema name containing the FK constraint |
required |
constraint
|
str
|
Foreign key constraint name |
required |
Example
Count images related to a subject (Image has FK to Subject)::
>>> # In Subject table, count related images
>>> pc = PseudoColumn(
... source=[InboundFK("domain", "Image_Subject_fkey"), "RID"],
... aggregate=Aggregate.CNT,
... markdown_name="Image Count"
... )
Source code in src/deriva_ml/model/annotations.py
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 | |
NameStyle
dataclass
Styling options for automatic display name formatting.
Applied to table or column names when no explicit display name is set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
underline_space
|
bool | None
|
Replace underscores with spaces (e.g., "First_Name" -> "First Name") |
None
|
title_case
|
bool | None
|
Apply title case formatting (e.g., "firstname" -> "Firstname") |
None
|
markdown
|
bool | None
|
Render the name as markdown |
None
|
Example
Transform "Subject_ID" to "Subject Id" with title case
display = Display( ... name_style=NameStyle(underline_space=True, title_case=True) ... )
Source code in src/deriva_ml/model/annotations.py
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 | |
to_dict
to_dict() -> dict[str, bool]
Convert to dictionary, excluding None values.
Source code in src/deriva_ml/model/annotations.py
316 317 318 319 320 321 322 323 324 325 | |
OutboundFK
dataclass
An outbound foreign key path step for pseudo-column source paths.
Use this when following a foreign key FROM the current table TO another table. This is common when displaying values from referenced tables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
str
|
Schema name containing the FK constraint |
required |
constraint
|
str
|
Foreign key constraint name |
required |
Example
Show species name from a related Species table::
>>> # Subject has FK to Species, display Species.Name
>>> pc = PseudoColumn(
... source=[OutboundFK("domain", "Subject_Species_fkey"), "Name"],
... markdown_name="Species"
... )
Chain multiple outbound FKs::
>>> # Image -> Subject -> Species
>>> pc = PseudoColumn(
... source=[
... OutboundFK("domain", "Image_Subject_fkey"),
... OutboundFK("domain", "Subject_Species_fkey"),
... "Name"
... ],
... markdown_name="Species"
... )
Source code in src/deriva_ml/model/annotations.py
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 | |
PreFormat
dataclass
Pre-formatting options for column values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str | None
|
Printf-style format string (e.g., "%.2f") |
None
|
bool_true_value
|
str | None
|
Display value for True |
None
|
bool_false_value
|
str | None
|
Display value for False |
None
|
Source code in src/deriva_ml/model/annotations.py
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 | |
PseudoColumn
dataclass
A pseudo-column definition for visible columns and foreign keys.
Pseudo-columns display computed values, values from related tables, or custom markdown patterns. They appear as columns in table views but are not actual database columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
str | list[str | InboundFK | OutboundFK] | None
|
Path to source data. Can be: - A column name (string) - A list of FK path steps ending with a column name |
None
|
sourcekey
|
str | None
|
Reference to a named source in source-definitions annotation |
None
|
markdown_name
|
str | None
|
Display name for the column (supports markdown) |
None
|
comment
|
str | Literal[False] | None
|
Description/tooltip text (or False to hide) |
None
|
entity
|
bool | None
|
Whether this represents an entity (affects rendering) |
None
|
aggregate
|
Aggregate | None
|
Aggregation function when source returns multiple values |
None
|
self_link
|
bool | None
|
Make the value a link to the current row |
None
|
display
|
PseudoColumnDisplay | None
|
Display formatting options |
None
|
array_options
|
dict[str, Any] | None
|
Options for array aggregates (max_length, order) |
None
|
Note
source and sourcekey are mutually exclusive. Use source for inline definitions, sourcekey to reference pre-defined sources.
Raises:
| Type | Description |
|---|---|
ValueError
|
If both source and sourcekey are provided |
Example
Simple column with custom display name::
>>> PseudoColumn(source="Internal_ID", markdown_name="ID")
Outbound FK traversal (display value from referenced table)::
>>> # Subject has FK to Species - show Species.Name
>>> PseudoColumn(
... source=[OutboundFK("domain", "Subject_Species_fkey"), "Name"],
... markdown_name="Species"
... )
Inbound FK with aggregation (count related records)::
>>> # Count images pointing to this subject
>>> PseudoColumn(
... source=[InboundFK("domain", "Image_Subject_fkey"), "RID"],
... aggregate=Aggregate.CNT,
... markdown_name="Images"
... )
Multi-hop FK path::
>>> # Image -> Subject -> Species
>>> PseudoColumn(
... source=[
... OutboundFK("domain", "Image_Subject_fkey"),
... OutboundFK("domain", "Subject_Species_fkey"),
... "Name"
... ],
... markdown_name="Species"
... )
With custom display formatting::
>>> PseudoColumn(
... source="URL",
... display=PseudoColumnDisplay(
... markdown_pattern="[Download]({{{_value}}})",
... show_foreign_key_link=False
... )
... )
Array aggregate with display options::
>>> PseudoColumn(
... source=[InboundFK("domain", "Tag_Item_fkey"), "Name"],
... aggregate=Aggregate.ARRAY_D,
... display=PseudoColumnDisplay(array_ux_mode=ArrayUxMode.CSV),
... markdown_name="Tags"
... )
Source code in src/deriva_ml/model/annotations.py
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 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 | |
PseudoColumnDisplay
dataclass
Display options for a pseudo-column.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown_pattern
|
str | None
|
Handlebars/mustache template |
None
|
template_engine
|
TemplateEngine | None
|
Template engine to use |
None
|
show_foreign_key_link
|
bool | None
|
Show as clickable link |
None
|
array_ux_mode
|
ArrayUxMode | None
|
How to render array values |
None
|
column_order
|
list[SortKey] | Literal[False] | None
|
Sort order for the column, or False to disable |
None
|
wait_for
|
list[str] | None
|
Template variables to wait for before rendering |
None
|
Source code in src/deriva_ml/model/annotations.py
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 | |
SchemaBuilder
Creates SQLAlchemy ORM from a Deriva catalog model.
Phase 1 of the two-phase database creation pattern. This class handles only schema/ORM creation - no data loading.
The Model can come from either a live catalog or a schema.json file: - From catalog: model = catalog.getCatalogModel() - From file: model = Model.fromfile("file-system", "path/to/schema.json")
Example
Create ORM from catalog model
model = catalog.getCatalogModel() builder = SchemaBuilder(model, schemas=['domain', 'deriva-ml']) orm = builder.build()
Create ORM from schema file
model = Model.fromfile("file-system", "schema.json") builder = SchemaBuilder(model, schemas=['domain'], database_path="local.db") orm = builder.build()
Use the ORM
ImageClass = orm.get_orm_class("Image") with Session(orm.engine) as session: images = session.query(ImageClass).all()
Clean up
orm.dispose()
Source code in src/deriva_ml/model/schema_builder.py
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 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 | |
__init__
__init__(
model: Model,
schemas: list[str],
database_path: Path
| str = ":memory:",
)
Initialize the schema builder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Model
|
ERMrest Model object (from catalog or schema.json file). |
required |
schemas
|
list[str]
|
List of schema names to include in the ORM. |
required |
database_path
|
Path | str
|
Path to SQLite database file. Use ":memory:" for in-memory database (default). If a Path or string is provided, separate .db files will be created for each schema. |
':memory:'
|
Source code in src/deriva_ml/model/schema_builder.py
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | |
build
build() -> SchemaORM
Build the SQLAlchemy ORM structure.
Creates SQLite tables from the ERMrest schema and generates ORM classes via SQLAlchemy automap.
Returns:
| Type | Description |
|---|---|
SchemaORM
|
SchemaORM object containing engine, metadata, Base, and utilities. |
Note
In-memory databases (database_path=":memory:") do not support SQLite schema attachments, so all tables will be created in a single database without schema prefixes in table names.
Source code in src/deriva_ml/model/schema_builder.py
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 | |
SchemaORM
Container for SQLAlchemy ORM components.
Provides access to the ORM structure and utility methods for table/class lookup. This is the result of Phase 1 (SchemaBuilder).
Attributes:
| Name | Type | Description |
|---|---|---|
engine |
SQLAlchemy Engine for database connections. |
|
metadata |
SQLAlchemy MetaData with table definitions. |
|
Base |
SQLAlchemy automap base for ORM classes. |
|
model |
ERMrest Model the ORM was built from. |
|
schemas |
List of schema names included. |
|
use_schemas |
Whether schema prefixes are used (False for in-memory). |
Source code in src/deriva_ml/model/schema_builder.py
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 | |
__del__
__del__() -> None
Cleanup resources when garbage collected.
Best-effort. __del__ runs at unpredictable points, including
interpreter shutdown when SQLAlchemy module-level globals
(registries, engines) may already be partially torn down. In
that race we'd see AttributeError: 'NoneType' object has no
attribute '_dispose_registries' printed via Exception
ignored in: — benign but noisy enough to make every short
script look like it failed. Swallow everything here; the
explicit dispose() callable from __exit__ and from
callers still raises normally.
Source code in src/deriva_ml/model/schema_builder.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | |
__enter__
__enter__() -> 'SchemaORM'
Context manager entry.
Source code in src/deriva_ml/model/schema_builder.py
451 452 453 | |
__exit__
__exit__(
exc_type, exc_val, exc_tb
) -> bool
Context manager exit - dispose resources.
Source code in src/deriva_ml/model/schema_builder.py
455 456 457 458 | |
__init__
__init__(
engine: Engine,
metadata: MetaData,
Base: AutomapBase,
model: Model,
schemas: list[str],
class_prefix: str,
use_schemas: bool = True,
)
Initialize SchemaORM container.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
engine
|
Engine
|
SQLAlchemy Engine. |
required |
metadata
|
MetaData
|
SQLAlchemy MetaData with tables. |
required |
Base
|
AutomapBase
|
Automap base with ORM classes. |
required |
model
|
Model
|
Source ERMrest Model. |
required |
schemas
|
list[str]
|
Schemas that were included. |
required |
class_prefix
|
str
|
Prefix used for ORM class names. |
required |
use_schemas
|
bool
|
Whether schema prefixes are used (False for in-memory). |
True
|
Source code in src/deriva_ml/model/schema_builder.py
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 | |
dispose
dispose() -> None
Dispose of SQLAlchemy resources.
Call this when done with the database to properly clean up connections. After calling dispose(), the instance should not be used further.
Source code in src/deriva_ml/model/schema_builder.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
find_table
find_table(table_name: str) -> SQLTable
Find a table by name.
Handles both schema.table format and schema_table format (for in-memory databases).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Table name, with or without schema prefix. Can be "schema.table", "schema_table", or just "table". |
required |
Returns:
| Type | Description |
|---|---|
Table
|
SQLAlchemy Table object. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If table not found. |
Source code in src/deriva_ml/model/schema_builder.py
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 | |
get_association_class
get_association_class(
left_cls: Type[Any],
right_cls: Type[Any],
) -> tuple[Any, Any, Any] | None
Find an association class connecting two ORM classes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
left_cls
|
Type[Any]
|
First ORM class. |
required |
right_cls
|
Type[Any]
|
Second ORM class. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Any, Any, Any] | None
|
Tuple of (association_class, left_relationship, right_relationship), |
tuple[Any, Any, Any] | None
|
or None if no association found. |
Source code in src/deriva_ml/model/schema_builder.py
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_orm_class
get_orm_class(
table_name: str,
) -> Any | None
Get the ORM class for a table by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Table name, with or without schema prefix. |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
SQLAlchemy ORM class for the table. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If table not found. |
Source code in src/deriva_ml/model/schema_builder.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
get_orm_class_for_table
get_orm_class_for_table(
table: Table | Table | str,
) -> Any | None
Get the ORM class for a table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table | Table | str
|
SQLAlchemy Table, Deriva Table, or table name. |
required |
Returns:
| Type | Description |
|---|---|
Any | None
|
SQLAlchemy ORM class, or None if not found. |
Source code in src/deriva_ml/model/schema_builder.py
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 | |
get_table_contents
get_table_contents(
table: str,
) -> Generator[
dict[str, Any], None, None
]
Retrieve all rows from a table as dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
Table name (with or without schema prefix). |
required |
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary for each row with column names as keys. |
Source code in src/deriva_ml/model/schema_builder.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
is_association_table
staticmethod
is_association_table(
table_class,
min_arity: int = 2,
max_arity: int = 2,
unqualified: bool = True,
pure: bool = True,
no_overlap: bool = True,
return_fkeys: bool = False,
)
Check if an ORM class represents an association table.
An association table links two or more tables through foreign keys, with a composite unique key covering those foreign keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_class
|
SQLAlchemy ORM class to check. |
required | |
min_arity
|
int
|
Minimum number of foreign keys (default 2). |
2
|
max_arity
|
int
|
Maximum number of foreign keys (default 2). |
2
|
unqualified
|
bool
|
If True, reject associations with extra key columns. |
True
|
pure
|
bool
|
If True, reject associations with extra non-key columns. |
True
|
no_overlap
|
bool
|
If True, reject associations with shared FK columns. |
True
|
return_fkeys
|
bool
|
If True, return the foreign keys instead of arity. |
False
|
Returns:
| Type | Description |
|---|---|
|
If return_fkeys=False: Integer arity if association, False otherwise. |
|
|
If return_fkeys=True: Set of foreign keys if association, False otherwise. |
Source code in src/deriva_ml/model/schema_builder.py
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 | |
list_tables
list_tables() -> list[str]
List all tables in the database.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of fully-qualified table names (schema.table), sorted. |
Source code in src/deriva_ml/model/schema_builder.py
175 176 177 178 179 180 181 182 183 | |
SortKey
dataclass
A sort key for row ordering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
Column name to sort by |
required |
descending
|
bool
|
Sort in descending order (default False) |
False
|
Example
SortKey("Name") # Ascending SortKey("Created", descending=True) # Descending
Source code in src/deriva_ml/model/annotations.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
to_dict
to_dict() -> dict[str, Any] | str
Convert to dict or string (if ascending).
Source code in src/deriva_ml/model/annotations.py
433 434 435 436 437 | |
TableDisplay
dataclass
Bases: AnnotationBuilder
Table-display annotation builder.
Controls table-level display options like row naming and ordering.
Example
td = TableDisplay() td.row_name(row_markdown_pattern="{{{Name}}} ({{{Species}}})") td.compact(row_order=[SortKey("Name")])
Source code in src/deriva_ml/model/annotations.py
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 | |
compact
compact(
options: TableDisplayOptions,
) -> "TableDisplay"
Set options for compact (list) view.
Source code in src/deriva_ml/model/annotations.py
1011 1012 1013 | |
default
default(
options: TableDisplayOptions,
) -> "TableDisplay"
Set default options.
Source code in src/deriva_ml/model/annotations.py
1019 1020 1021 | |
detailed
detailed(
options: TableDisplayOptions,
) -> "TableDisplay"
Set options for detailed (record) view.
Source code in src/deriva_ml/model/annotations.py
1015 1016 1017 | |
row_name
row_name(
row_markdown_pattern: str,
template_engine: TemplateEngine
| None = None,
) -> "TableDisplay"
Set row name pattern (used in foreign key dropdowns, etc.).
Source code in src/deriva_ml/model/annotations.py
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 | |
set_context
set_context(
context: str,
options: TableDisplayOptions
| str
| None,
) -> "TableDisplay"
Set options for a context.
Source code in src/deriva_ml/model/annotations.py
988 989 990 991 992 993 994 995 | |
TableDisplayOptions
dataclass
Options for a single table display context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row_order
|
list[SortKey] | None
|
Sort order for rows |
None
|
page_size
|
int | None
|
Number of rows per page |
None
|
row_markdown_pattern
|
str | None
|
Template for row names |
None
|
page_markdown_pattern
|
str | None
|
Template for page header |
None
|
separator_markdown
|
str | None
|
Template between rows |
None
|
prefix_markdown
|
str | None
|
Template before rows |
None
|
suffix_markdown
|
str | None
|
Template after rows |
None
|
template_engine
|
TemplateEngine | None
|
Template engine for patterns |
None
|
collapse_toc_panel
|
bool | None
|
Collapse TOC panel |
None
|
hide_column_headers
|
bool | None
|
Hide column headers |
None
|
Source code in src/deriva_ml/model/annotations.py
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
TemplateEngine
Bases: str, Enum
Template engine for markdown patterns.
Attributes:
| Name | Type | Description |
|---|---|---|
HANDLEBARS |
Use Handlebars.js templating (recommended, more features) |
|
MUSTACHE |
Use Mustache templating (simpler, fewer features) |
Example
display = PseudoColumnDisplay( ... markdown_pattern="{{{Name}}}", ... template_engine=TemplateEngine.HANDLEBARS ... )
Source code in src/deriva_ml/model/annotations.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |
VisibleColumns
dataclass
Bases: AnnotationBuilder
Visible-columns annotation builder.
Controls which columns appear in different UI contexts and their order. This is one of the most commonly used annotations for customizing the Chaise interface.
Column entries can be: - Column names (strings): "Name", "RID", "Description" - Foreign key references: fk_constraint("schema", "constraint_name") - Pseudo-columns: PseudoColumn(...) for computed/derived values
Contexts:
- compact: Table/list views (search results, data browser)
- detailed: Single record view (full record page)
- entry: Create/edit forms
- entry/create: Create form only
- entry/edit: Edit form only
- *: Default for all contexts
Example
Basic column lists for different contexts::
>>> vc = VisibleColumns()
>>> vc.compact(["RID", "Name", "Status"])
>>> vc.detailed(["RID", "Name", "Status", "Description", "Created"])
>>> vc.entry(["Name", "Status", "Description"])
>>> handle.set_annotation(vc)
Method chaining::
>>> vc = (VisibleColumns()
... .compact(["RID", "Name"])
... .detailed(["RID", "Name", "Description"])
... .entry(["Name", "Description"]))
Including foreign key references::
>>> vc = VisibleColumns()
>>> vc.compact([
... "RID",
... "Name",
... fk_constraint("domain", "Subject_Species_fkey"),
... ])
With pseudo-columns for computed values::
>>> vc = VisibleColumns()
>>> vc.compact([
... "RID",
... "Name",
... PseudoColumn(
... source=[InboundFK("domain", "Sample_Subject_fkey"), "RID"],
... aggregate=Aggregate.CNT,
... markdown_name="Samples"
... ),
... ])
Context inheritance (reference another context)::
>>> vc = VisibleColumns()
>>> vc.compact(["RID", "Name"])
>>> vc.set_context("compact/brief", "compact") # Inherit from compact
With faceted search (filter context)::
>>> vc = VisibleColumns()
>>> vc.compact(["RID", "Name", "Status"])
>>> facets = FacetList()
>>> facets.add(Facet(source="Status", open=True))
>>> vc._contexts["filter"] = facets.to_dict()
Source code in src/deriva_ml/model/annotations.py
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 | |
compact
compact(
columns: list[ColumnEntry],
) -> "VisibleColumns"
Set columns for compact (list) view.
Source code in src/deriva_ml/model/annotations.py
822 823 824 | |
default
default(
columns: list[ColumnEntry],
) -> "VisibleColumns"
Set default columns for all contexts.
Source code in src/deriva_ml/model/annotations.py
842 843 844 | |
detailed
detailed(
columns: list[ColumnEntry],
) -> "VisibleColumns"
Set columns for detailed (record) view.
Source code in src/deriva_ml/model/annotations.py
826 827 828 | |
entry
entry(
columns: list[ColumnEntry],
) -> "VisibleColumns"
Set columns for entry (create/edit) forms.
Source code in src/deriva_ml/model/annotations.py
830 831 832 | |
entry_create
entry_create(
columns: list[ColumnEntry],
) -> "VisibleColumns"
Set columns for create form only.
Source code in src/deriva_ml/model/annotations.py
834 835 836 | |
entry_edit
entry_edit(
columns: list[ColumnEntry],
) -> "VisibleColumns"
Set columns for edit form only.
Source code in src/deriva_ml/model/annotations.py
838 839 840 | |
set_context
set_context(
context: str,
columns: list[ColumnEntry] | str,
) -> "VisibleColumns"
Set columns for a context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
str
|
Context name (e.g., "compact", "detailed", "*") |
required |
columns
|
list[ColumnEntry] | str
|
List of columns, or string referencing another context |
required |
Returns:
| Type | Description |
|---|---|
'VisibleColumns'
|
Self for chaining |
Source code in src/deriva_ml/model/annotations.py
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 | |
VisibleForeignKeys
dataclass
Bases: AnnotationBuilder
Visible-foreign-keys annotation builder.
Controls which related tables appear in the UI via inbound foreign keys.
Example
vfk = VisibleForeignKeys() vfk.detailed([ ... fk_constraint("domain", "Image_Subject_fkey"), ... fk_constraint("domain", "Diagnosis_Subject_fkey") ... ])
Source code in src/deriva_ml/model/annotations.py
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 | |
default
default(
foreign_keys: list[ForeignKeyEntry],
) -> "VisibleForeignKeys"
Set default foreign keys for all contexts.
Source code in src/deriva_ml/model/annotations.py
897 898 899 | |
detailed
detailed(
foreign_keys: list[ForeignKeyEntry],
) -> "VisibleForeignKeys"
Set foreign keys for detailed view.
Source code in src/deriva_ml/model/annotations.py
893 894 895 | |
set_context
set_context(
context: str,
foreign_keys: list[ForeignKeyEntry]
| str,
) -> "VisibleForeignKeys"
Set foreign keys for a context.
Source code in src/deriva_ml/model/annotations.py
884 885 886 887 888 889 890 891 | |
__getattr__
__getattr__(name: str)
Lazy import for DatabaseModel and DerivaMLDatabase.
Source code in src/deriva_ml/model/__init__.py
110 111 112 113 114 115 116 117 118 119 120 | |
fk_constraint
fk_constraint(
schema: str, constraint: str
) -> list[str]
Create a foreign key constraint reference for visible-columns.
Use this in visible-columns to include a foreign key column (showing the referenced row's name/link). This is different from InboundFK/OutboundFK which are used inside PseudoColumn source paths.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
str
|
Schema name containing the FK constraint |
required |
constraint
|
str
|
Foreign key constraint name |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
[schema, constraint] list for use in visible-columns |
Example
Include a foreign key in visible columns::
>>> vc = VisibleColumns()
>>> vc.compact([
... "RID",
... "Name",
... fk_constraint("domain", "Subject_Species_fkey"), # Shows Species
... ])
This is equivalent to the raw format::
>>> vc.compact(["RID", "Name", ["domain", "Subject_Species_fkey"]])
Source code in src/deriva_ml/model/annotations.py
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 | |