SpecMerger
dcmspec.spec_merger.SpecMerger
Merges multiple DICOM specification models.
The SpecMerger class provides methods to combine and enrich DICOM SpecModel objects, supporting both path-based and node-based merging strategies. This is useful for workflows where you need to sequentially merge two or more models, such as enriching PS3.3 module attributes models with definitions from the PS3.6 data elements dictionary, or combining a PS3.3 specification with a PS3.4 SOP class and then enriching with an IHE profile specification.
Source code in src/dcmspec/spec_merger.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
|
__init__(config=None, model_store=None, logger=None)
Initialize the SpecMerger.
Sets up the logger for the merger. If no logger is provided, a default logger is created. If no model_store is provided, defaults to JSONSpecStore.
PARAMETER | DESCRIPTION |
---|---|
config
|
Configuration object. If None, a default Config is created.
TYPE:
|
model_store
|
Store for loading and saving models. Defaults to JSONSpecStore.
TYPE:
|
logger
|
Logger instance to use. If None, a default logger is created.
TYPE:
|
Source code in src/dcmspec/spec_merger.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
|
merge_many(models, method, match_by, attribute_names=None, merge_attrs_list=None, json_file_name=None, force_update=False, ignore_module_level=False)
Merge a sequence of DICOM SpecModel objects using the specified merge method, with optional caching.
This method merges a list of models in order, applying either path-based or node-based merging at each step. You can specify different attribute names and lists of attributes to merge for each step, allowing for flexible, multi-stage enrichment of DICOM models. If json_file_name is provided, the merged model will be cached to that file, and loaded from cache if available and force_update is False.
PARAMETER | DESCRIPTION |
---|---|
models
|
The models to merge, in order.
TYPE:
|
method
|
Merge method to use ("matching_path" or "matching_node").
TYPE:
|
match_by
|
"name" to match by node name, "attribute" to match by a specific attribute.
TYPE:
|
attribute_names
|
List of attribute names to use for each merge step. Each entry corresponds to a merge operation between two models. Required if match_by="attribute". If match_by="name", can be None.
TYPE:
|
merge_attrs_list
|
List of lists of attribute names to merge for each merge step. Each entry corresponds to a merge operation between two models.
TYPE:
|
json_file_name
|
If provided, cache/load the merged model to/from this file.
TYPE:
|
force_update
|
If True, always perform the merge and overwrite the cache.
TYPE:
|
ignore_module_level
|
If True, skip the module level when matching paths (only applies to path-based merging).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SpecModel
|
The final merged SpecModel instance.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If models is empty, method is unknown, or attribute_names/merge_attrs_list have incorrect length, or if attribute_names is not set when match_by="attribute". |
Note
- For path-based merging of DICOM attributes, it is recommended to use match_by="attribute" and attribute_names=["elem_tag", ...] for robust, tag-based matching.
- For node-based merging or special cases, match_by="name" can be used and attribute_names may be None.
Source code in src/dcmspec/spec_merger.py
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 |
|
merge_node(model1, model2, match_by='name', attribute_name=None, merge_attrs=None, json_file_name=None, force_update=False)
Merge two DICOM SpecModel objects using the node merge method, with optional caching.
This is a convenience method that calls merge_many with two models.
PARAMETER | DESCRIPTION |
---|---|
model1
|
The first model.
TYPE:
|
model2
|
The second model to merge with the first.
TYPE:
|
match_by
|
"name" to match by node name, "attribute" to match by a specific attribute.
TYPE:
|
attribute_name
|
The attribute name to use for matching.
TYPE:
|
merge_attrs
|
List of attribute names to merge from the other model's node.
TYPE:
|
json_file_name
|
If provided, cache/load the merged model to/from this file.
TYPE:
|
force_update
|
If True, always perform the merge and overwrite the cache.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SpecModel
|
The merged SpecModel instance.
TYPE:
|
Source code in src/dcmspec/spec_merger.py
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 |
|
merge_path(model1, model2, match_by='attribute', attribute_name='elem_tag', merge_attrs=None, json_file_name=None, force_update=False, ignore_module_level=False)
Merge two DICOM SpecModel objects using the path merge method, with optional caching.
This is a convenience method that calls merge_many with two models.
By default, this method matches nodes by their DICOM tag (attribute_name="elem_tag") using path-based merging (match_by="attribute"). This is the recommended and robust approach for DICOM attribute-level merging, as DICOM tags are unique and consistent identifiers.
PARAMETER | DESCRIPTION |
---|---|
model1
|
The first model.
TYPE:
|
model2
|
The second model to merge with the first.
TYPE:
|
match_by
|
"attribute" (default, recommended) to match by a specific attribute (DICOM tag), or "name" to match by node name.
TYPE:
|
attribute_name
|
The attribute name to use for matching (default: "elem_tag").
TYPE:
|
merge_attrs
|
List of attribute names to merge from the other model's node.
TYPE:
|
json_file_name
|
If provided, cache/load the merged model to/from this file.
TYPE:
|
force_update
|
If True, always perform the merge and overwrite the cache.
TYPE:
|
ignore_module_level
|
If True, skip the module level when matching paths.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SpecModel
|
The merged SpecModel instance.
TYPE:
|
Note
For DICOM attribute-level merging, the default (match_by="attribute", attribute_name="elem_tag") is strongly recommended. Only use match_by="name" for special cases where tag-based matching is not possible.
Source code in src/dcmspec/spec_merger.py
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 |
|
merge_path_with_default(model1, model2, match_by='name', attribute_name=None, merge_attrs=None, default_attr='elem_type', default_value='3', default_value_func=None, json_file_name=None, force_update=False, ignore_module_level=False)
Merge two DICOM SpecModel objects by path, and set a default value for missing attributes.
This method merges two models using the path-based merge strategy (matching nodes by their
hierarchical path and by DICOM tag, i.e., match_by="attribute", attribute_name="elem_tag" by default),
and then sets default_attr
to default_value
for any node in the merged model that does not already
have that attribute.
This is especially useful for workflows where you want to enrich a normalized IOD model (e.g., from DICOM PS3.3) with a service attribute model (e.g., from DICOM PS3.4 or an IHE profile), and you want to ensure that all nodes in the merged model have a value for the Type attribute.
Use case
- Merging a DICOM PS3.3 normalized IOD attributes specification (e.g., built with IODSpecBuilder) with a DICOM PS3.4 DIMSE SCU or SCP attributes specification (e.g., built with ServiceAttributeModel and select_dimse/select_role). After merging, any node present in the normalized IOD model but missing from the service attribute model will have its "Type" (or other specified attribute) set to the default value (e.g., "3"), ensuring the merged model is complete and ready for further processing or export.
PARAMETER | DESCRIPTION |
---|---|
model1
|
The first model (e.g., normalized IOD).
TYPE:
|
model2
|
The second model (e.g., service attribute model).
TYPE:
|
match_by
|
"attribute" (default, recommended) to match by a specific attribute (DICOM tag), or "name" to match by node name.
TYPE:
|
attribute_name
|
The attribute name to use for matching (default: "elem_tag").
TYPE:
|
merge_attrs
|
List of attribute names to merge from the other model's node.
TYPE:
|
default_attr
|
The attribute to set if missing (default: "elem_type").
TYPE:
|
default_value
|
The value to set for missing attributes (default: "3").
TYPE:
|
default_value_func
|
A function to determine the default value for missing attributes.
If provided, it will be called as
TYPE:
|
json_file_name
|
If provided, cache/load the merged model to/from this file.
TYPE:
|
force_update
|
If True, always perform the merge and overwrite the cache.
TYPE:
|
ignore_module_level
|
If True, skip the module level when matching paths.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SpecModel
|
The merged SpecModel instance with default values set for missing attributes.
TYPE:
|
Note
For DICOM attribute-level merging, the default (match_by="attribute", attribute_name="elem_tag") is strongly recommended. Only use match_by="name" for special cases where tag-based matching is not possible.
Source code in src/dcmspec/spec_merger.py
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 |
|