clip()#
relationalai.std.math
#clip(value: Number|Producer, lower: Number|Producer, upper: Number|Producer) -> Expression
Restricts value
to be within the specified bounds lower
and upper
.
If value
, lower
, or upper
is a Producer
, clip()
filters out any non-numeric values from the producer.
Must be called in a rule or query context.
Parameters#
Name | Type | Description |
---|---|---|
value | Producer or Python Number object | The number to restrict within the specified bounds. |
lower | Producer or Python Number object | The lower bound for the value. |
upper | Producer or Python Number object | The upper bound for the value. |
Returns#
An Expression
object.
Example#
Use clip()
to restrict values within a specified range:
#import relationalai as rai
from relationalai.std import math
# =====
# SETUP
# =====
model = rai.Model("MyModel")
Account = model.Type("Account")
with model.rule():
Account.add(id=1).set(balance=100)
Account.add(id=2).set(balance=-50)
Account.add(id=3).set(balance=300)
Account.add(id=4).set(balance="INVALID") # Non-numeric balance
# =======
# EXAMPLE
# =======
# Set a clipped_balance property for each account, restricting balance between 0 and 200.
with model.rule():
account = Account()
account.set(clipped_balance=math.clip(account.balance, 0, 200))
# Since clip() filters out non-numeric values, the clipped_balance property
# is not set for the account with id=4.
with model.query() as select:
account = Account()
response = select(account.id, account.balance, account.clipped_balance)
print(response.results)
# id balance clipped_balance
# 0 1 100 100
# 1 2 -50 0
# 2 3 300 200
# 3 4 INVALID NaN