# numpy functions in Python Calculator

**URL:** https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735
**Category:** Development
**Tags:** python
**Created:** [August 22, 2023, 3:49pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735 "2023-08-22T15:49:39Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 22, 2023, 3:49pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/1 "2023-08-22T15:49:39Z")

</div>

Hi,

I’m using numpy methods in the `Python Calculator` but I’m getting different behavior depending on whether the input is a vtkDataSet or a vtkCompositeDataSet. The expression I’m trying out is:

```auto
pythonCalculator2.Expression = "numpy.swapaxes(Gradients, 1, 2)"

```

Note that Gradients is a 9 component array.

This works fine if the input is a vtkDataSet but has problems if the input is a vtkCompositeDataSet. So what I’m wondering is – should users should be allowed to use numpy like this in the Python Calculator? I don’t see anything in the documentation that says this is allowed or not allowed. I noticed that in `dataset_adapter.py` that `VTKArray` derives from numpy.ndarray but `VTKCompositeDataArray` derives from object.

Any thoughts on this?

Thanks,  
Andy

Note that if I don’t allow composite datasets in the vtkPythonCalculator class by commenting out:

```auto
    info->Append(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkCompositeDataSet");

```

in `vtkPythonCalculator::FillInputPortInformation` I’d get the same behavior here for `vtkDataSets` and `vtkCompositeDataSets` since then the pipeline would iterate through each block in a composite data set and have `vtkPythonCalculator` just operate on vtkDataSets.

Trying this out in an MR – [https://gitlab.kitware.com/paraview/paraview/-/merge\_requests/6452](https://gitlab.kitware.com/paraview/paraview/-/merge_requests/6452)

---

<div class="post-metadata">

### Author: ![berkgeveci](https://discourse.paraview.org/user_avatar/discourse.paraview.org/berkgeveci/32/1464_2.png) [@berkgeveci](https://discourse.paraview.org/u/berkgeveci)
#### Post date: [August 22, 2023, 4:31pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/2 "2023-08-22T16:31:56Z")

</div>

Unfortunately, this won’t work. For composite dataset arrays, we actually use an Python class that is not even a numpy array. We have custom functions that iterate over each array and call the underlying numpy function. So there has to be a swapaxes() version of that.

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 22, 2023, 5:01pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/3 "2023-08-22T17:01:14Z")

</div>

Yeah, `VTKCompositeDataArray` derives from `object` instead of `numpy.ndarray` so it’s not working in the `Python Calculator` when passed to numpy functions. If we do the change that’s in my MR though `Python Calculator` won’t ever operate on composite datasets though so then we could use numpy methods directly. Wouldn’t that be an improvement?

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [August 23, 2023, 7:36am UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/4 "2023-08-23T07:36:46Z")

</div>

If `vtkPythonCalculator` does not have a specific code path for `Composite` (I didn’t check), then your change make sense I think

---

<div class="post-metadata">

### Author: ![berkgeveci](https://discourse.paraview.org/user_avatar/discourse.paraview.org/berkgeveci/32/1464_2.png) [@berkgeveci](https://discourse.paraview.org/u/berkgeveci)
#### Post date: [August 23, 2023, 5:51pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/5 "2023-08-23T17:51:53Z")

</div>

Well, yes and no. If we do that, the global functions would stop working. There are many of those such as min, max, mean etc. So something like the following would stop working for partitioned datasets:

```auto
avar / mean(avar)

```

which is more important than supporting arbitrary numpy functions. The code path for adding support for numpy functions is pretty simple. It’s a simple as adding something like the following to algorithms.py:

```auto
mod = _make_dfunc(numpy.mod)

```

---

<div class="post-metadata">

### Author: ![nicolas.vuaille](https://discourse.paraview.org/user_avatar/discourse.paraview.org/nicolas.vuaille/32/5873_2.png) [@nicolas.vuaille](https://discourse.paraview.org/u/nicolas.vuaille)
#### Post date: [August 24, 2023, 7:24am UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/6 "2023-08-24T07:24:50Z")

</div>

Well, indeed … my bad.

So no better way than manually adding function as needed ?

---

<div class="post-metadata">

### Author: ![berkgeveci](https://discourse.paraview.org/user_avatar/discourse.paraview.org/berkgeveci/32/1464_2.png) [@berkgeveci](https://discourse.paraview.org/u/berkgeveci)
#### Post date: [August 24, 2023, 8:40pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/7 "2023-08-24T20:40:27Z")

</div>

We could probably add a wrapper that takes a numpy function. Something like:

```auto
apply_numpy(numpy.swapaxes, arrays, args...)

```

What do you think @Andy_Bauer ?

---

<div class="post-metadata">

### Author: ![Andy\_Bauer](https://discourse.paraview.org/user_avatar/discourse.paraview.org/andy_bauer/32/5442_2.png) [@Andy\_Bauer](https://discourse.paraview.org/u/Andy_Bauer)
#### Post date: [August 24, 2023, 8:47pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/8 "2023-08-24T20:47:06Z")

</div>

Yeah, I like that. I couldn’t quite figure out how to make it look but adding a numpy wrapper would be nice to get all of the numpy functionality wihout having to explicitly add it in.

---

<div class="post-metadata">

### Author: ![berkgeveci](https://discourse.paraview.org/user_avatar/discourse.paraview.org/berkgeveci/32/1464_2.png) [@berkgeveci](https://discourse.paraview.org/u/berkgeveci)
#### Post date: [August 29, 2023, 6:41pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/9 "2023-08-29T18:41:21Z")

</div>

@Andy_Bauer  
It turns out that this functionality already exists 🙂 I wrote it to implement the underlying looping. All you have to do is:

```auto
apply_ufunc(numpy.swapaxes, Gradients, (1, 2))

```

Give it a try.

---

<div class="post-metadata">

### Author: ![berkgeveci](https://discourse.paraview.org/user_avatar/discourse.paraview.org/berkgeveci/32/1464_2.png) [@berkgeveci](https://discourse.paraview.org/u/berkgeveci)
#### Post date: [August 29, 2023, 6:42pm UTC](https://discourse.paraview.org/t/numpy-functions-in-python-calculator/12735/10 "2023-08-29T18:42:08Z")

</div>

You can also use `apply_dfunc` when you have two arguments.
