Skip to content

Support more algebraic/graph operations on Diagram #1191

@ethho

Description

@ethho

Feature Request

Problems

Construct Null Diagram

It is not simple to create the null datajoint.Diagram, which we define as a Diagram with no nodes or edges, and notate conventionally as $K_0$. It should be simple to construct $K_0$ using Diagram.__init__; ideally, this would be the default behavior if no source is passed, or if the source is null.

sum() Diagrams

In addition, while it is possible to __add__ two Diagrams, it is not straightforward to join a list of Diagrams. The current approaches include:

diags: list[dj.Diagram] = [diag1, diag2, ...]

# for loop accumulation
full = diags[0]
for diag in diags[1::]:
    full += diag

# accumulate in comprehension (ew)
full = diags[0]
[full := full + diag for diag in diags[1::]]

# sum() accumulation
full = sum(diags[1::], diags[0])

Ideally, one should be able to simply:

full = sum(diags)

but this is not supported because Diagram.__radd__ is not implemented.

Check Diagram Equality

It would be nice to implement Diagram.__eq__ so that one can check diag1 == diag2:

def __eq__(self, other: dj.Diagram, approx=False) -> bool:
    # Check other attributes, like `nodes_to_show` and `type(source)`
    
    if approx:
        return networkx.algorithms.faster_could_be_isomorphic(self, other)
    else:
        return networkx.algorithms.is_isomorphic(self, other)

Requirements

d1 = dj.Diagram()
d2 = dj.Diagram()
d3 = dj.Diagram(d2)
d4 = dj.Diagram(empty_schema)
d5 = dj.Diagram(non_empty_schema)
d6 = dj.Diagram(table)
diags = [d1, d2, d3, d4, d5, d6]
assert d1 == d2 == d3 == d4
assert d5 != d1
assert d5 != d6
assert sum(diags) == d5 + d6

Justification

It should be easier to work with multiple Diagrams, especially if #1190 is not implemented. For example, lack of support for $K_0$ is troublesome in the works-api implementation, since we must create _placeholder schemas for empty pipelines.

Alternative Considerations

#1190 covers some of the use cases where these operations would be used.

Related Errors

#1190 also proposes changes to datajoint.Diagram.__init__.

Environment

  • OS: Linux (relevant for all)
  • Python Version: 3.9 (relevant for all)
  • MySQL Version: 8.0 (relevant for all)
  • MySQL Deployment Strategy: remote (percona-qa.datajoint.io)
  • DataJoint Version: 0.14.3

Additional Research and Context

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIndicates new improvements

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions