# Replace negative values with 0 (thresholding) in a metric

#1

Hi,

I am trying to replace the following simple custom expression engine function with a built-in:

``````function customThreshold(series) {

var data = series.data();

for (var i = 0; i < series.count(); i++) {
if (data[i] < 0) {
data[i] = 0;
}
}

return series;
}
``````

The metric using it is simply `customThreshold(MySimpleMetric)`. It works fine but I see the engine is spending some time in this function for large evaluation intervals so I’m looking for a more native way to do it that could (maybe) be faster.

0 Likes

#2

Try this:
`expression: "max(0, MySimpleMetric)"`

0 Likes

#4

I thought it would work but there’s a side effect: `max(0, MySimpleMetric)` resets the missing() to 0 (per the logic you described here) which breaks some additional logic.

Unless this can be fixed I need to find another way.

`(MySimpleMetric >= 0) * MySimpleMetric` would work but I’m not sure if it’s a big gain from my `customThreshold` function.

0 Likes

#5

available(MySimpleMetric) ? max(0, MySimpleMetric) : null

But we should probably carry over missing information. @rohit.sureka what do you think?

0 Likes

#6

I tried `max(makeMissing(100, 0), MySimpleMetric)` but it seems that `max()` is ignoring fully missing values:

`max(makeMissing(100, 0), identity(-1))` returns `identity(-1)`

0 Likes

#7

@romain.juban Since we are picking the max value, we are transforming the signal and the new signal can deterministically say what the max value is and hence there is no missing information. I believe what you want to write is:

`MySimpleMetric >= 0 ? MySimpleMetric : null`

@lerela - as much as possible you should use the existing expression / metric engine, since the expressions are compiled and will be extremely fast in comparison to any custom java script code.

So your expression `(MySimpleMetric >= 0) * MySimpleMetric` will be a significant gain from the custom java script function too.

0 Likes

#8

What if you have the value 3 with missing at 100? When you take the max of that and 0 is it going to take 0 or 3?

0 Likes