# Operations on multi-block dataset with Programmable Filter

**URL:** https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027
**Category:** ParaView Support
**Created:** [December 11, 2018, 9:18am UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027 "2018-12-11T09:18:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![pcrepier](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pcrepier](https://discourse.paraview.org/u/pcrepier)
#### Post date: [December 11, 2018, 9:18am UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/1 "2018-12-11T09:18:07Z")

</div>

Hi Everyone,

A post-processing script I made redefines a variable that may be wrongly defined by my solver. However, this value depends on an other scalar also present in the dataset.

For example:

```python
if scalar2 < 0.8:
    scalar1 = 50 * scalar3
else:
    scalar1 = 6000 * scalar3

```

I initially tried to do this using the calculator filter:

```auto
if(scalar2<0.8,50*scalar3,6000*scalar3)

```

But I turned out that the variable created may disappear from the dataset … (I have to report this bug)

So I tried to do that using a programmable filter, but I am getting stuck when I want to overwrite the values in the array. My dataset is usually a multi-block dataset and this seems not to be helping.

here is the script I used so far:

```python
import vtk
import vtk.numpy_interface.dataset_adapter as dsa
import vtk.numpy_interface.algorithms as algs
import numpy as np

data = dsa.WrapDataObject(inputs[0])

scalar1 = data.CellData["scalar1"]
scalar2 = data.CellData["scalar2"]
scalar3 = data.CellData["scalar3"]

newArray = scalar1 * 0
for i, val in enumerate(scalar1):
    if scalar2[i] < 0.8:
        newArray[i] = 50*scalar3[i]
    else:
        newArray[i] = 6000*scalar3[i]

output.CellData.append(newArray, 'newVal')

```

This obsvioulsy fails. Partly because my “newArray” is a composite array…

Anyone could help on this ?

Thanks in advance

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [December 11, 2018, 2:39pm UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/2 "2018-12-11T14:39:20Z")

</div>

I think you can do this with a **Python Calculator** filter. Set the expression to

`np.where(scalar2 < 0.8, 50 * scalar3, 6000*scalar3)`

Change the _Array Name_ parameter to “scalar1”, and you should get the desired behavior.

---

<div class="post-metadata">

### Author: ![pcrepier](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pcrepier](https://discourse.paraview.org/u/pcrepier)
#### Post date: [December 11, 2018, 3:06pm UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/3 "2018-12-11T15:06:21Z")

</div>

I tried your suggestion, but unfortunately, I have feeling that the condition I give always returns True whatever I am asking …

`np.where(scalar2 < 0.8, 50 * scalar3, 6000*scalar3)`  
gives the same result as  
`np.where(scalar2 > 0.8, 50 * scalar3, 6000*scalar3)`

But that should not be the case …  
scalar2 is a scalar defined between 0 and 1 by the way.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [December 11, 2018, 3:18pm UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/4 "2018-12-11T15:18:39Z")

</div>

There should definitely be a change when you switch from `<` to `>`. What is your output array named and how are you checking the results?

---

<div class="post-metadata">

### Author: ![pcrepier](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/f4b2a3/32.png) [@pcrepier](https://discourse.paraview.org/u/pcrepier)
#### Post date: [December 11, 2018, 3:22pm UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/5 "2018-12-11T15:22:48Z")

</div>

I am creating a new variable to check my result.

I made another check: merging all the blocks together. and this leads to the expected result!  
So there seem to be something going on with multi-block datasets.

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [December 11, 2018, 6:29pm UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/6 "2018-12-11T18:29:20Z")

</div>

> [@pcrepier](#):
>
> So there seem to be something going on with multi-block datasets.

Ah, indeed. The usual numpy functions do not work for `VTKCompositeDataArrays` that are available from multiblock datasets, but the numpy-like algorithms provided by `vtk.numpy_interface.algorithms` do. However, the `where` function provided in `vtk.numpy_interface.algorithms` is only the single-argument version, not the three-argument version I suggested, so merging the blocks is the only way to use this numpy expression currently.

---

<div class="post-metadata">

### Author: ![utkarsh.ayachit](https://discourse.paraview.org/user_avatar/discourse.paraview.org/utkarsh.ayachit/32/39_2.png) [@utkarsh.ayachit](https://discourse.paraview.org/u/utkarsh.ayachit)
#### Post date: [December 13, 2018, 3:44pm UTC](https://discourse.paraview.org/t/operations-on-multi-block-dataset-with-programmable-filter/1027/7 "2018-12-13T15:44:58Z")

</div>

> [@pcrepier](#):
>
> This obsvioulsy fails. Partly because my “newArray” is a composite array…

Here’s a way to fix your Python Programmable filter script to work with composite arrays:

```python
import paraview.vtk.numpy_interface.dataset_adapter as dsa
import paraview.vtk.numpy_interface.algorithms as algs
import numpy as np

data = dsa.WrapDataObject(inputs[0])

cd_scalar1 = data.CellData["scalar1"]
cd_scalar2 = data.CellData["scalar2"]
cd_scalar3 = data.CellData["scalar3"]

cd_newArray = cd_scalar1 * 0

for scalar1, scalar2, scalar3, newArray in zip(cd_scalar1.Arrays, cd_scalar2.Arrays, cd_scalar3.Arrays, cd_newArray.Arrays):
  # you may check any of the scalar*/newArray with `foo is dsa.NoneArray` to
  # handle blocks with the array missing, if any.
  for i, val in enumerate(scalar1):
      if scalar2[i] < 0.8:
          newArray[i] = 50*scalar3[i]
      else:
          newArray[i] = 6000*scalar3[i]

output.CellData.append(cd_newArray, 'newVal')

```
