# \[Python\] Calculator formulae

**URL:** https://discourse.paraview.org/t/python-calculator-formulae/7427
**Category:** Development
**Created:** [June 16, 2021, 2:37pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427 "2021-06-16T14:37:03Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 16, 2021, 2:37pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/1 "2021-06-16T14:37:03Z")

</div>

In line with the recent improvements to the Programmable filters and python editor [[1]](https://blog.kitware.com/new-paraview-python-editor-features/), we are thinking of improving the calculator and python calculator formulaes managements.

One could think to rely on the python editor in the same way as the programmable filters, however, this has a few issues:

1. Both calculator supports only one liner, not in line with a text editor
2. Calculator formulaes are not python, which would be confusing to edit in a python editor.

So here is what I have in mind:

1. Make the formula line edit as wide as possible for both calculator
2. Make the formula line edit multiline with automatic wrapping so that long one liner can be seen whole
3. Add a setting to store and manage named python and calculator one liner. Think “Tools-\>Formulae Manager”.
4. Let the user access these formulae in a searchable combo box in the [python] calculator
5. Let the user add the current formula in a new saved formula
6. Let the user open the Formulae Manager from the [python] calculator

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 16, 2021, 2:38pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/2 "2021-06-16T14:38:11Z")

</div>

@utkarsh.ayachit @cory.quammen

---

<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: [June 16, 2021, 2:47pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/3 "2021-06-16T14:47:07Z")

</div>

That sounds like a nice feature. I don’t have any major concerns about what you’ve described.

One additional idea - it might be nice for users to be able to import and export formulae to others

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 16, 2021, 2:49pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/4 "2021-06-16T14:49:05Z")

</div>

Most definitely !

---

<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: [June 16, 2021, 3:33pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/5 "2021-06-16T15:33:01Z")

</div>

This sounds great. I suggest not using the term formulae as the definition does not exactly fit: “A statement, especially an equation, of a fact, rule, principle, or other logical relation.” I would suggest using something like “calculation” or “expression”.  
Another related suggestion is that we have a pull-down menu for the Python calculator that lists the available functions. Or some similar way for people to discover what is possible.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 16, 2021, 3:36pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/6 "2021-06-16T15:36:44Z")

</div>

> I suggest not using the term formulae as the definition does not exactly fit: “A statement, especially an equation, of a fact, rule, principle, or other logical relation.” I would suggest using something like “calculation” or “expression”.

Good point

> Another related suggestion is that we have a pull-down menu for the Python calculator that lists the available functions. Or some similar way for people to discover what is possible.

This is point 4 🙂

---

<div class="post-metadata">

### Author: ![wascott](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/w/8e8cbc/32.png) [@wascott](https://discourse.paraview.org/u/wascott)
#### Post date: [June 16, 2021, 4:04pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/7 "2021-06-16T16:04:20Z")

</div>

+1  
@vgweirs

---

<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: [June 16, 2021, 4:45pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/8 "2021-06-16T16:45:26Z")

</div>

OK I am confused 🙂 When we say Formulae, are we talking about user level expressions or built-in functions? For example, is it gradient(foo) / mag(gradient(foo)) or is it just gradient()? I assumed it was the first. I was suggesting a drop-down menu for the second also.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 17, 2021, 7:23am UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/9 "2021-06-17T07:23:30Z")

</div>

> When we say Formulae, are we talking about user level expressions or built-in functions? For example, is it gradient(foo) / mag(gradient(foo)) or is it just gradient()? I assumed it was the first. I was suggesting a drop-down menu for the second also.

User defined expression. `gradient(foo) / mag(gradient(foo))`

The calculator already have built-in methods that the user can click on.  
The python calculator do not have that but rely on the python/numpy knowledge of the user.  
We do not plan to improve this in this context.

---

<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: [June 17, 2021, 1:52pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/10 "2021-06-17T13:52:00Z")

</div>

Thanks for the clarification.

> The python calculator do not have that but rely on the python/numpy knowledge of the user.

The problem is that we have a bunch of VTK-level functions available to the calculator that are not listed anywhere (I think). For example, who knows that we have functions like max\_per\_block()? I always have to look it up in the algorithms.py module… I totally get that it is out of scope for this work but I wanted to put it in writing so that we are aware that this is an issue.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 17, 2021, 3:22pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/11 "2021-06-17T15:22:13Z")

</div>

I’ve recently udpated the doc, but the list of function is not yet complete:

[https://docs.paraview.org/en/latest/UsersGuide/filteringData.html?highlight=python%20calculator#functions](https://docs.paraview.org/en/latest/UsersGuide/filteringData.html?highlight=python%20calculator#functions)

It should indeed contains all our custom defined methods.

---

<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: [June 17, 2021, 3:34pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/12 "2021-06-17T15:34:55Z")

</div>

This is great! I didn’t know.

---

<div class="post-metadata">

### Author: ![Sandeep\_Jella](https://discourse.paraview.org/user_avatar/discourse.paraview.org/sandeep_jella/32/5021_2.png) [@Sandeep\_Jella](https://discourse.paraview.org/u/Sandeep_Jella)
#### Post date: [June 19, 2021, 1:25pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/13 "2021-06-19T13:25:26Z")

</div>

All I can say is ‘’‘Thank you!’’’. This will be a most welcome feature. It really helps with quick checks involving (somewhat) longer expressions involving vector algebra/calculus.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 30, 2021, 8:28am UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/14 "2021-06-30T08:28:30Z")

</div>

We will move forward with this developpement.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [October 19, 2021, 9:42am UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/15 "2021-10-19T09:42:04Z")

</div>

This has been postponed sadly.

---

<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: [June 3, 2022, 7:39am UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/16 "2022-06-03T07:39:25Z")

</div>

Hi there,

The developpments has started, here are some preview:

> **Expression Manager**
>
> ![ExpressionDialog|598x500](upload://zoCJ3uo7Qbjb71W24Ankx6RpFxi.gif)

> **Filter Integration**
>
> ![ExpresssionChooser|598x500](upload://3RMpfw9qzoW7fetVLkwf5QVfYgr.gif)

This is still work in progress, but feedbacks are welcom !

Specially on the integration in the properties panel. We added three buttons around the main text input: reuse expression, store expression and open manager.  
The preview suggest them under the text line, so the expression list will not pop over the current text, but not sure if it is a good UX.

---

<div class="post-metadata">

### Author: ![LEKIEN](https://discourse.paraview.org/user_avatar/discourse.paraview.org/lekien/32/7492_2.png) [@LEKIEN](https://discourse.paraview.org/u/LEKIEN)
#### Post date: [June 3, 2022, 3:47pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/17 "2022-06-03T15:47:53Z")

</div>

Good job, @nicolas.vuaille, to be continue. 😉

**My first remark**  
It would be nice to be able to give a **name** to an expression/formula because very quickly the readability becomes difficult because of the complex (name with description ?).

**My second remark**  
Some same complex calculations can apply to different fields of values. This is the case, among other things, for complex unit changes. It would be nice to be able to define an expression/formula with one or more value field names passed as parameters.  
For realize the idea:  
name: _toto_  
expression: _$1 \* g1/($1 - g2)_  
description: $1 describe the first name of the variable in parameter, g1 and g2 is field data (global, cell or point)  
usage : _toto(Val)_, Val the name of a field data

**My third remark:**  
To be able to introduce one formula into another.  
For realize the idea: _$1/toto($1)_

Thanks for your attention.

---

<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: [June 7, 2022, 8:44am UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/18 "2022-06-07T08:44:37Z")

</div>

Hi @LEKIEN ,  
Thanks for your feedback!

Adding a name to the expression is indeed a good idea and easy to implement, we will do that.

Looking at the next remarks, I feel like it means implementing a scripting language. It is a lot of work that we are not going to do for this first implementation.

---

<div class="post-metadata">

### Author: ![LEKIEN](https://discourse.paraview.org/user_avatar/discourse.paraview.org/lekien/32/7492_2.png) [@LEKIEN](https://discourse.paraview.org/u/LEKIEN)
#### Post date: [June 7, 2022, 11:56am UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/19 "2022-06-07T11:56:10Z")

</div>

Hi @nicolas.vuaille

“Looking at the next remarks, I feel like it means implementing a scripting language. It is a lot of work that we are not going to do for this first implementation.”  
It is true that it could look like that.

In a first approach, I was thinking of something very simple (not optimized) of macro type like in c/c++ language.

Before the command execution phase, a preprocessing phase would be introduced. It would consist of looping as long as we encounter an expression name (with parameter) to replace it with the corresponding expression.

If I have the expressions:  
name: toto, expression: $1 \* g1/($1 - g2)  
name: titi, expression: $1/toto($1)  
I want to build the new quantity:  
titi(FOO)/toto(BOO)  
the preprocessing phase would then give:  
step1: (FOO/toto(FOO))/toto(BOO)  
step2: (FOO/(FOO \* g1/(FOO - g2)))/toto(BOO)  
step3: (FOO/(FOO \* g1/(FOO - g2)))/(BOO - g1/(BOO - g2))  
step4: no expression to develop  
this expression is then executed:  
(FOO/(FOO \* g1/(FOO - g2)))/(BOO - g1/(BOO - g2))

In order to facilitate expression detection, one can consider prefixing with $. If $ followed by a number then it is an input parameter, otherwise it is the name of an expression.

It’s not totally a scripting language. 😉

But, for the Python calculator aspect, each expression can be seen as a Python method with its parameters.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [June 7, 2022, 12:08pm UTC](https://discourse.paraview.org/t/python-calculator-formulae/7427/20 "2022-06-07T12:08:58Z")

</div>

> [@LEKIEN](#):
>
> For the Python calculator aspect, each expression can be seen as a Python method with its parameters.

That is a good point that should be considered in future developement.

[Next page](https://discourse.paraview.org/t/python-calculator-formulae/7427.md?page=2)
