## Sign up to take part

Registered users can ask their own questions, contribute to discussions, and be part of the Community!

This website uses cookies. By clicking OK, you consent to the use of cookies. Read our cookie policy.

Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Registered users can ask their own questions, contribute to discussions, and be part of the Community!

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

Solved!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

map a numerical range

Hello everybody,

I would need your ideas for an advanced use of the "Formula" process 😀

the needed:

**in a "prepare receipt", and more particularly in a "Formula" process, I would have liked to be able to create a custom function.**

Is it possible ?

Why this ?

==> I need to create a special math function, which maps one numeric range to another numeric range, similar to Arduino's MAP function for those who know)

Thank you

Operating system used: **Windows - Linux Subsystem**

1 Solution

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Hi,

If you are referring to something like.

https://www.arduino.cc/reference/en/language/functions/math/map/

One option is using python functions are available directly in the prepare recipe.

The columns for x, in_min in_max , out_min and out_max need to be replaced with the actual column names in your dataset instead of of "num_1" , "num_2" in the code below.

```
def _map(x, in_min, in_max, out_min, out_max):
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
def process(row):
y = _map(int(row["num_1"]),int(row["num_2"]), int(row["num_3"]), int(row["num_4"]), int(row["num_5"]))
return (y)
```

. Let me know if that helps or if you are looking for something else.

This can also be done with a DSS formula in the above example it would be :

(num_1 - num_2) * (num_5 - num_4)/ (num_3 - num_2) + num_4

Solutions shown first - Read whole discussion

2 Replies

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

Hi,

If you are referring to something like.

https://www.arduino.cc/reference/en/language/functions/math/map/

One option is using python functions are available directly in the prepare recipe.

The columns for x, in_min in_max , out_min and out_max need to be replaced with the actual column names in your dataset instead of of "num_1" , "num_2" in the code below.

```
def _map(x, in_min, in_max, out_min, out_max):
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
def process(row):
y = _map(int(row["num_1"]),int(row["num_2"]), int(row["num_3"]), int(row["num_4"]), int(row["num_5"]))
return (y)
```

. Let me know if that helps or if you are looking for something else.

This can also be done with a DSS formula in the above example it would be :

(num_1 - num_2) * (num_5 - num_4)/ (num_3 - num_2) + num_4

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

AlexT, your solution was the right one.

In fact, I had already written the whole formula with the "FORMULA" process. So my idea was to create an additional "MAP" function to use with "Formula".

But in the end, I completely rewrote my formula with the Python process, which works fine for him.

Thanks for the help 🙂