|
4 | 4 | export FactorCompute |
5 | 5 | const FactorCompute = FactorDFG |
6 | 6 |
|
| 7 | +#TODO maybe keep for Bloblet type assert later. |
7 | 8 | const MetadataTypes = Union{ |
8 | 9 | Int, |
9 | 10 | Float64, |
@@ -424,3 +425,205 @@ function getCoordinates( |
424 | 425 | X = ManifoldsBase.log(M, p0, p) |
425 | 426 | return ManifoldsBase.get_coordinates(M, p0, X, basis) |
426 | 427 | end |
| 428 | + |
| 429 | +# old merge and copy functions, will be replaced by cleaner merge!, sync!, and copyto! functions |
| 430 | + |
| 431 | +# """ |
| 432 | +# $(SIGNATURES) |
| 433 | +# Merger sourceDFG to destDFG given an optional list of variables and factors and distance. |
| 434 | +# Notes: |
| 435 | +# - Nodes already in the destination graph are updated from sourceDFG. |
| 436 | +# - Orphaned factors (where the subgraph does not contain all the related variables) are not included. |
| 437 | +# Related: |
| 438 | +# - [`copyGraph!`](@ref) |
| 439 | +# - [`buildSubgraph`](@ref) |
| 440 | +# - [`listNeighborhood`](@ref) |
| 441 | +# - [`deepcopyGraph`](@ref) |
| 442 | +# """ |
| 443 | + |
| 444 | +##============================================================================== |
| 445 | +## Copy Functions #TODO replace with sync |
| 446 | +##============================================================================== |
| 447 | + |
| 448 | +""" |
| 449 | + $(SIGNATURES) |
| 450 | +Common function for copying nodes from one graph into another graph. |
| 451 | +This is overridden in specialized implementations for performance. |
| 452 | +Orphaned factors are not added, with a warning if verbose. |
| 453 | +Set `overwriteDest` to overwrite existing variables and factors in the destination DFG. |
| 454 | +NOTE: `copyGraphMetadata` is deprecated – use agent/graph Bloblets instead. |
| 455 | +Related: |
| 456 | +- [`deepcopyGraph`](@ref) |
| 457 | +- [`deepcopyGraph!`](@ref) |
| 458 | +- [`buildSubgraph`](@ref) |
| 459 | +- [`listNeighborhood`](@ref) |
| 460 | +- [`mergeGraph!`](@ref) |
| 461 | +""" |
| 462 | +# |
| 463 | +function copyGraph!( |
| 464 | + destDFG::AbstractDFG, |
| 465 | + sourceDFG::AbstractDFG, |
| 466 | + variableLabels::AbstractVector{Symbol} = listVariables(sourceDFG), |
| 467 | + factorLabels::AbstractVector{Symbol} = listFactors(sourceDFG); |
| 468 | + copyGraphMetadata::Bool = false, |
| 469 | + overwriteDest::Bool = false, |
| 470 | + deepcopyNodes::Bool = false, |
| 471 | + verbose::Bool = false, |
| 472 | + showprogress::Bool = verbose, |
| 473 | +) |
| 474 | + # Split into variables and factors |
| 475 | + sourceVariables = getVariables(sourceDFG, variableLabels) |
| 476 | + sourceFactors = getFactors(sourceDFG, factorLabels) |
| 477 | + # Now we have to add all variables first, |
| 478 | + @showprogress desc = "copy variables" enabled = showprogress for variable in |
| 479 | + sourceVariables |
| 480 | + |
| 481 | + variableCopy = deepcopyNodes ? deepcopy(variable) : variable |
| 482 | + if !hasVariable(destDFG, variable.label) |
| 483 | + addVariable!(destDFG, variableCopy) |
| 484 | + elseif overwriteDest |
| 485 | + mergeVariable!(destDFG, variableCopy) |
| 486 | + else |
| 487 | + throw(LabelExistsError("Variable", variable.label)) |
| 488 | + end |
| 489 | + end |
| 490 | + # And then all factors to the destDFG. |
| 491 | + @showprogress desc = "copy factors" enabled = showprogress for factor in sourceFactors |
| 492 | + # Get the original factor variables (we need them to create it) |
| 493 | + sourceFactorVariableIds = collect(factor.variableorder) |
| 494 | + # Find the labels and associated variables in our new subgraph |
| 495 | + factVariableIds = Symbol[] |
| 496 | + for variable in sourceFactorVariableIds |
| 497 | + if hasVariable(destDFG, variable) |
| 498 | + push!(factVariableIds, variable) |
| 499 | + end |
| 500 | + end |
| 501 | + # Only if we have all of them should we add it (otherwise strange things may happen on evaluation) |
| 502 | + if length(factVariableIds) == length(sourceFactorVariableIds) |
| 503 | + factorCopy = deepcopyNodes ? deepcopy(factor) : factor |
| 504 | + if !hasFactor(destDFG, factor.label) |
| 505 | + addFactor!(destDFG, factorCopy) |
| 506 | + elseif overwriteDest |
| 507 | + mergeFactor!(destDFG, factorCopy) |
| 508 | + else |
| 509 | + throw(LabelExistsError("Factor", factor.label)) |
| 510 | + end |
| 511 | + elseif verbose |
| 512 | + @warn "Factor $(factor.label) will be an orphan in the destination graph, and therefore not added." |
| 513 | + end |
| 514 | + end |
| 515 | + |
| 516 | + if copyGraphMetadata |
| 517 | + error( |
| 518 | + "copyGraphMetadata keyword has been removed – metadata APIs were replaced by Bloblets. " * |
| 519 | + "Copy agent/graph Bloblets manually before calling copyGraph!", |
| 520 | + ) |
| 521 | + end |
| 522 | + return nothing |
| 523 | +end |
| 524 | + |
| 525 | +""" |
| 526 | + $(SIGNATURES) |
| 527 | +Copy nodes from one graph into another graph by making deepcopies. |
| 528 | +see [`copyGraph!`](@ref) for more detail. |
| 529 | +Related: |
| 530 | +- [`deepcopyGraph`](@ref) |
| 531 | +- [`buildSubgraph`](@ref) |
| 532 | +- [`listNeighborhood`](@ref) |
| 533 | +- [`mergeGraph!`](@ref) |
| 534 | +""" |
| 535 | +# |
| 536 | +function deepcopyGraph!( |
| 537 | + destDFG::AbstractDFG, |
| 538 | + sourceDFG::AbstractDFG, |
| 539 | + variableLabels::Vector{Symbol} = ls(sourceDFG), |
| 540 | + factorLabels::Vector{Symbol} = lsf(sourceDFG); |
| 541 | + kwargs..., |
| 542 | +) |
| 543 | + return copyGraph!( |
| 544 | + destDFG, |
| 545 | + sourceDFG, |
| 546 | + variableLabels, |
| 547 | + factorLabels; |
| 548 | + deepcopyNodes = true, |
| 549 | + kwargs..., |
| 550 | + ) |
| 551 | +end |
| 552 | + |
| 553 | +""" |
| 554 | + $(SIGNATURES) |
| 555 | +Copy nodes from one graph into a new graph by making deepcopies. |
| 556 | +see [`copyGraph!`](@ref) for more detail. |
| 557 | +Related: |
| 558 | +- [`deepcopyGraph!`](@ref) |
| 559 | +- [`buildSubgraph`](@ref) |
| 560 | +- [`listNeighborhood`](@ref) |
| 561 | +- [`mergeGraph!`](@ref) |
| 562 | +""" |
| 563 | +# |
| 564 | +function deepcopyGraph( |
| 565 | + ::Type{T}, |
| 566 | + sourceDFG::AbstractDFG, |
| 567 | + variableLabels::Vector{Symbol} = ls(sourceDFG), |
| 568 | + factorLabels::Vector{Symbol} = lsf(sourceDFG); |
| 569 | + graphLabel::Symbol = Symbol(getGraphLabel(sourceDFG), "_cp_$(string(uuid4())[1:6])"), |
| 570 | + kwargs..., |
| 571 | +) where {T <: AbstractDFG} |
| 572 | + destDFG = T(; |
| 573 | + solverParams = getSolverParams(sourceDFG), |
| 574 | + graph = sourceDFG.graph, |
| 575 | + agent = sourceDFG.agent, |
| 576 | + graphLabel, |
| 577 | + ) |
| 578 | + copyGraph!( |
| 579 | + destDFG, |
| 580 | + sourceDFG, |
| 581 | + variableLabels, |
| 582 | + factorLabels; |
| 583 | + deepcopyNodes = true, |
| 584 | + kwargs..., |
| 585 | + ) |
| 586 | + return destDFG |
| 587 | +end |
| 588 | + |
| 589 | +function mergeGraph!( |
| 590 | + destDFG::AbstractDFG, |
| 591 | + sourceDFG::AbstractDFG, |
| 592 | + variableLabels::Vector{Symbol}, |
| 593 | + factorLabels::Vector{Symbol} = lsf(sourceDFG), |
| 594 | + distance::Int = 0; |
| 595 | + solvableFilter = nothing, |
| 596 | + tagsFilter = nothing, |
| 597 | + kwargs..., |
| 598 | +) |
| 599 | + Base.depwarn( |
| 600 | + """ |
| 601 | + mergeGraph! with variableLabels, factorLabels, and distance is deprecated. |
| 602 | + For now use function composition ending with mergeGraph!, |
| 603 | + syncGraph!(Coming soon) will replace this functionality. |
| 604 | + """, |
| 605 | + :mergeGraph!, |
| 606 | + ) |
| 607 | + # find neighbors at distance to add |
| 608 | + sourceVariables, sourceFactors = listNeighborhood( |
| 609 | + sourceDFG, |
| 610 | + union(variableLabels, factorLabels), |
| 611 | + distance; |
| 612 | + solvableFilter, |
| 613 | + tagsFilter, |
| 614 | + ) |
| 615 | + |
| 616 | + copyGraph!( |
| 617 | + destDFG, |
| 618 | + sourceDFG, |
| 619 | + sourceVariables, |
| 620 | + sourceFactors; |
| 621 | + deepcopyNodes = true, |
| 622 | + overwriteDest = true, |
| 623 | + kwargs..., |
| 624 | + ) |
| 625 | + |
| 626 | + return destDFG |
| 627 | +end |
| 628 | + |
| 629 | +@deprecate buildSubgraph(args...; kwargs...) getSubgraph(args...; kwargs...) |
0 commit comments