# Request for input: VTK/Python API

**URL:** https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132
**Category:** Development
**Created:** [January 23, 2024, 5:29pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132 "2024-01-23T17:29:05Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![berk.geveci](https://discourse.vtk.org/user_avatar/discourse.vtk.org/berk.geveci/32/3146_2.png) [@berk.geveci](https://discourse.vtk.org/u/berk.geveci)
#### Post date: [January 23, 2024, 5:29pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/1 "2024-01-23T17:29:05Z")

</div>

Hi all,

In this [post](https://discourse.vtk.org/t/more-pythonic-vtk-wrapping/13092), I introduced some of the changes we are making to the VTK wrappers. One of these is the introduction of an operator to easily connect sources and filters. I initially proposed using `>>`. Later `|` was suggested:

```auto
(source >> vtkContourFilter(contours=[10,20]) >> vtkShrinkFilter()).execute()

```

vs

```auto
(source | vtkContourFilter(contours=[10,20]) | vtkShrinkFilter()).execute()

```

Can we have an informal vote on which one is better?

Thanks.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [January 23, 2024, 7:12pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/2 "2024-01-23T19:12:13Z")

</div>

I go back and forth on this.

The pipe | symbol is well known especially for linux users. The direction of data flow is implicit.

The \>\> symbol points in the direction of data flow which is a neat bit of information to add in, especially for beginners. Also the \>\> is more visually apparent. So \>\> is my vote, unless there is some weird python syntax or pattern that gets in the way etc.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 25, 2024, 7:40pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/3 "2024-01-25T19:40:51Z")

</div>

Python already overrides “`|`” for a different purpose:

```python
>>> {1,2,3} | {3,4,5}
{1, 2, 3, 4, 5}

```

My vote is for “`>>`”, because it’s a rarely used operator. Plus all the reasons that Will gave.

But I’m also in favor of something that takes a list of filters (and data objects) and generates a new algorithm object that represents a chunk of pipeline:

```python
pipeline = vtkPipeline([vtkContourFilter(contours=[10,20]), vtkShrinkFilter()])
output = pipeline1.execute(input_data)

output = vtkPipeline([input_data, pipeline]).execute()

```

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [January 25, 2024, 11:09pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/4 "2024-01-25T23:09:08Z")

</div>

I’ve implemented `>>` using the number protocol (`tp_as_number`). fyi, it’s possible to have both `>>` and `|` 🙂 But, right now, only `>>` is implemented.

See [TestAlgorithmNumberProtocol.py](https://gitlab.kitware.com/jaswant.panchumarti/vtk/-/blob/python-property-wrapping/Wrapping/Python/Testing/Python/TestAlgorithmNumberProtocol.py).

In the MR, you can see a large number of operators which are all currently null. We’ve a lot to pick from.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 26, 2024, 3:30am UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/5 "2024-01-26T03:30:00Z")

</div>

> [@jaswantp](#):
>
> it’s possible to have both `>>` and `|`

Obviously so. But just because you can, doesn’t mean you should 🙂

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [January 26, 2024, 5:19am UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/6 "2024-01-26T05:19:15Z")

</div>

How do you specify whics output port is connected to which input connection?

I would not introduce a new method of specifying VTK filter connections if that method only works filters that have a single input and a single output.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [January 26, 2024, 1:11pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/7 "2024-01-26T13:11:34Z")

</div>

> [@lassoan](#):
>
> How do you specify whics output port is connected to which input connection?

What we want is a simple syntax for describing a graph. The nodes are the filters, each node has properties. The edges are the connections, each edge has properties to specify the output port number and the input port number.

Note than when we create a filter we can set a name for the filter with `SetObjectName()`. So we can easily set names for the nodes in the graph.

---

<div class="post-metadata">

### Author: ![berk.geveci](https://discourse.vtk.org/user_avatar/discourse.vtk.org/berk.geveci/32/3146_2.png) [@berk.geveci](https://discourse.vtk.org/u/berk.geveci)
#### Post date: [January 26, 2024, 1:41pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/8 "2024-01-26T13:41:52Z")

</div>

> [@lassoan](#):
>
> How do you specify whics output port is connected to which input connection?
> 
> I would not introduce a new method of specifying VTK filter connections if that method only works filters that have a single input and a single output.

I agree. I have been thinking about this. The simplest solution that I can think of is:

```auto
OutputPort(aFilter, 1) >> anotherFilter
aFilter >> InputPort(anotherFilter, 1)
aFilter >> Append(appendFilter)

```

Implementing these in Python is trivial.

I do have a related question. Let’s say that we implement some utility functions/classes like this and we want them to be included when one includes the vtkCommonExecutionModel module. How would one do that? Change the vtkCommonExecutionModel to something like this?

vtkCommonExecutionModel.py

```auto
from .vtkCommonExecutionModelCompiled # this is the .so file. We need to rename it I guess?

def OutputPort(...):
    ...

```

---

<div class="post-metadata">

### Author: ![jaswantp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/jaswantp/32/10046_2.png) [@jaswantp](https://discourse.vtk.org/u/jaswantp)
#### Post date: [January 26, 2024, 4:51pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/9 "2024-01-26T16:51:57Z")

</div>

> [@berk.geveci](#):
>
> I do have a related question. Let’s say that we implement some utility functions/classes like this and we want them to be included when one includes the vtkCommonExecutionModel module. How would one do that? Change the vtkCommonExecutionModel to something like this?
> 
> vtkCommonExecutionModel.py

We can provide those functions from the .so file instead of doing it in python. Just like `execute` is generated in vtkAlgorithmPython.cxx, the wrapper would generate new functions in C++ that return an output port for an algorithm.

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [January 28, 2024, 2:34am UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/10 "2024-01-28T02:34:22Z")

</div>

> [@berk.geveci](#):
>
> The simplest solution that I can think of is:
> 
> ```auto
> OutputPort(aFilter, 1) >> anotherFilter
> aFilter >> InputPort(anotherFilter, 1)
> aFilter >> Append(appendFilter)
> 
> ```
> 
> Implementing these in Python is trivial.

would something like this be possible?

```auto
AFilter().output_port(1) >> BFilter()
AFilter() >> BFilter(input_port=1, ...)

```

---

<div class="post-metadata">

### Author: ![berk.geveci](https://discourse.vtk.org/user_avatar/discourse.vtk.org/berk.geveci/32/3146_2.png) [@berk.geveci](https://discourse.vtk.org/u/berk.geveci)
#### Post date: [January 28, 2024, 2:05pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/11 "2024-01-28T14:05:10Z")

</div>

In the current version, I have:

```auto
filter1 >> SelectPorts(filter2, input_port=1, output_port=1) >> filter3

```

I am not happy with this either. We can do (and I like):

```auto
AFilter().output_port(1) >> BFilter()

```

This is not feasible:

```auto
AFilter() >> BFilter(input_port=1, ...)

```

If the input\_port is passed as a constructor argument, it would become intrinsic to the filter which affects future connections. Also, I like the idea of everything flowing left to right (meaning, the input port selection should come before the filter). So how about this:

```auto
AFilter() >> input_port(1, BFilter())

```

In its full complexity, this would look like:

```auto
AFilter() >> input_port(1, BFilter()).output_port(1) >> CFilter()

```

Btw, this way of connecting things would not be used much, I would guess. This would likely be more common:

```auto
line = vtkLineSource(...).execute()
dataset >> vtkStreamTracer(source_data = line)

```

or

```auto
line_source = vtkLineSource(...)
dataset >> vtkStreamTracer(source_connection = line_source.output_port)

```

---

<div class="post-metadata">

### Author: ![marcomusy](https://discourse.vtk.org/user_avatar/discourse.vtk.org/marcomusy/32/95_2.png) [@marcomusy](https://discourse.vtk.org/u/marcomusy)
#### Post date: [January 29, 2024, 1:04pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/12 "2024-01-29T13:04:38Z")

</div>

I like

```auto
AFilter() >> input_port(1, BFilter()).output_port(2) >> CFilter()

```

another “visually symmetric” options could be:

```auto
AFilter() >> select_ports(1, BFilter(), 2) >> CFilter()

```

---

<div class="post-metadata">

### Author: ![berk.geveci](https://discourse.vtk.org/user_avatar/discourse.vtk.org/berk.geveci/32/3146_2.png) [@berk.geveci](https://discourse.vtk.org/u/berk.geveci)
#### Post date: [January 29, 2024, 3:39pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/13 "2024-01-29T15:39:11Z")

</div>

> `AFilter() >> select_ports(1, BFilter(), 2) >> CFilter()`

I like this a lot. Not only because it’s concise and clear but also it would be a lot less disruptive to implement.

---

<div class="post-metadata">

### Author: ![omdaniel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/omdaniel/32/8538_2.png) [@omdaniel](https://discourse.vtk.org/u/omdaniel)
#### Post date: [February 29, 2024, 7:35pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/14 "2024-02-29T19:35:10Z")

</div>

Have you considered a method chaining approach that doesn’t introduct a new operator by using a function to wrap the filter, such as…

```auto
(ported(AFilter())
    .port(1, BFilter())
    .port(2, CFilter())
    )

```

this doesn’t introduce any new methods to the filters themselves

---

<div class="post-metadata">

### Author: ![omdaniel](https://discourse.vtk.org/user_avatar/discourse.vtk.org/omdaniel/32/8538_2.png) [@omdaniel](https://discourse.vtk.org/u/omdaniel)
#### Post date: [February 29, 2024, 8:00pm UTC](https://discourse.vtk.org/t/request-for-input-vtk-python-api/13132/15 "2024-02-29T20:00:57Z")

</div>

Perhaps a monad like object that wraps the filter would be better

```auto
(Ported(Afilter())
    .port(1, BFilter())
    .port(2, CFilter())
    ).filter()

```
