aiidateam / aiida-core

The official repository for the AiiDA code
https://aiida-core.readthedocs.io
Other
436 stars 190 forks source link

Workflow introspection #4749

Open chrisjsewell opened 3 years ago

chrisjsewell commented 3 years ago

In workchains we have the define method, which states the inputs, outputs, and logic flow (aka outline), and you can document it with https://github.com/aiidateam/aiida-core/blob/dca50f47a340ffa14302d7bd0714ba86227b4b57/aiida/sphinxext/process.py#L48

To visualise an exising provenance graph, we also have https://github.com/aiidateam/aiida-core/blob/dca50f47a340ffa14302d7bd0714ba86227b4b57/aiida/tools/visualization/graph.py#L349

However, what we cannot do is create an automated graph visualisation of a Workchain's (recursive) logic flow (without running it). I think this would be nice, to be able to quickly visualise what a workchain does.

To do this you would need to be able to somehow identify and load nested Workchains/CalcJob, e.g. with

class MultiplyAddWorkChain(WorkChain):
    """WorkChain to multiply two numbers and add a third, for testing and demonstration purposes."""

    @classmethod
    def define(cls, spec):
        """Specify inputs and outputs."""
        super().define(spec)
        spec.input('x', valid_type=Int)
        spec.input('y', valid_type=Int)
        spec.input('z', valid_type=Int)
        spec.input('code', valid_type=Code)
        spec.outline(
            cls.multiply,
            cls.add,
            cls.validate_result,
            cls.result,
        )

    ...

    def add(self):
        """Add two numbers using the `ArithmeticAddCalculation` calculation job plugin."""
        inputs = {'x': self.ctx.product, 'y': self.inputs.z, 'code': self.inputs.code}
        future = self.submit(ArithmeticAddCalculation, **inputs)

you would need to step through the outline methods, identify that add contains the submission of ArithmeticAddCalculation, and load up this class to analyse its define method, ... (potentially recursively if the submitted process is also a WorkChain)

Not trivial but I think worth considering

ConradJohnston commented 3 years ago

I'm very supportive of this.

I would ponder though - what would you expect a graph to look like if there's something like a while_ step in define?

It may be that you can't go that far without an example or reference (intermediate) result and at this point I think there's overlap with the aiida-testing project where the caching mechanism is exploited.