simple auto-node #108
Replies: 3 comments 24 replies
-
I want to make the general remark that often when you want to automatically parse functions into nodes, you are already at a level of detail that should not be exposed to this level of visualization. However, there are still many cases where this can be useful, but it should rather be seen as a starting point to generate a template code that can then be optimized further, instead of a complete start-to-finish workflow. This should be easy to write, for some inspiration something like the following should get you starting quickly: import inspect
def node_from_function(name: str, call: str, obj: object, color: str):
"""Create a node class definition from a given function.
Parameters
----------
name: str
Becomes the title of the node.
call: str
Represents the full function call without parentheses, e.g. `math.sqrt`
obj: object
The function object. Its souce will be parsed using the inspect module,
which might not work for some functions, expecially those of C bindings.
color: str
The color hex code the node will get.
Returns
-------
node_def: str
The code defining the node class.
"""
try:
sig = inspect.getfullargspec(obj)
except Exception as e:
raise Exception(f"Could not parse source of {name}.")
inputs = '\n'.join([f"NodeInputBP('{param_name}')," for param_name in sig.args])
node_name = f'{name}_Node'
node_def = f"""
class {node_name}(Node):
\"\"\"{obj.__doc__}\"\"\"
title = '{name}'
init_inputs = [
{inputs}
]
init_outputs = [
NodeOutputBP(),
]
color = '{color}'
def update_event(self, inp=-1):
self.set_output_val(0, {call}({
', '.join([f'self.input({i})' for i in range(len(sig.args))])
}))
"""
return node_def E.g. class sqrt_Node(Node):
"""Return the square root of x."""
title = 'sqrt'
init_inputs = [
NodeInputBP('x'),
]
init_outputs = [
NodeOutputBP(),
]
color = '#aabbcc'
def update_event(self, inp=-1):
self.set_output_val(0, math.sqrt(self.input(0))) This code uses |
Beta Was this translation helpful? Give feedback.
-
I'll probably hand-code a visualize() node that draws a graphical representation of data-driven objects such as numpy.ndarrays, or torch.tensor |
Beta Was this translation helpful? Give feedback.
-
@leon-thomm It should theoretically work for every function(even non-python ones), but the (non-python) nodes will have nameless arguments. |
Beta Was this translation helpful? Give feedback.
-
I'm working on making a node lib for this (mostly integrating already-existing libraries into Ryven) and I don't feel like defining each of them by hand, so I was thinking that maybe you could add a simple-auto-node method
The simplest definition of a node is as follows:
I started making an algorithm that takes in a function and outputs something like this, but I never finished it. Plus,such an algorithm would make prototyping much, much quicker.
Beta Was this translation helpful? Give feedback.
All reactions