map a numerical range

Solved!
delphi_jb
Level 1
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

0 Kudos
1 Solution
AlexT
Dataiker

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)

Screenshot 2021-11-30 at 14.08.57.png

. 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

Screenshot 2021-11-30 at 14.19.45.png

View solution in original post

2 Replies
AlexT
Dataiker

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)

Screenshot 2021-11-30 at 14.08.57.png

. 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

Screenshot 2021-11-30 at 14.19.45.png

delphi_jb
Level 1
Author

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 ๐Ÿ™‚