calculate_field#

geodesic.boson.calculate_field(new_field, expression, skip_if_null=False, skip_if_nan=False, fill_value=None, input_collection=None, output_collection=None)[source]#

Creates a new field in the search response based on an expression.

The fields in the expression must be numeric fields.

Parameters:
  • new_field (str) – name of the new field to create

  • expression (str) – expression to calculate the new field

  • skip_if_null (bool) – whether to skip the calculation if any of the fields in the expression do not exist. Useful when not every feature contains a property used in the expression. Defaults to False.

  • fill_value (float | None) – value to fill the new field with if the expression is not calculated. Defaults to None.

  • skip_if_nan (bool) – skip the calculation on any field where the expression evaluates to NaN

  • input_collection (str, optional) – name of the input collection to apply transform to. Defaults to None (all collections)

  • output_collection (str, optional) – name of the virtual collection that the transform will be applied to. Defaults to None (no virtual collection created)

Returns:

middleware to calculate a new field in the search response

Return type:

Middleware

Examples

>>> # Calculate a new field called "sum" that is the sum of "field_1" and "field_2"
>>> transform = calculate_field(
...     new_field="sum",
...     expression="field_1 + field_2"
... )
>>> # Calculate a new field called "plus_one" that is "field_1" + 1
>>> transform = calculate_field(
...     new_field="plus_one",
...     expression="field_1 + 1"
... )
>>> # Calculate a new field called "div" that is "(field_1 + 5) / field_2"
>>> transform = calculate_field(
...     new_field="div",
...     expression="(field_1 + 5) / field_2",
...     skip_if_null=True
... )
>>> # Calculate a new field log scale which uses the log function "10 * log10(field_1)"
>>> transform = calculate_field(
...     new_field="loc_scale_field_1",
...     expression="10 * log10(field_1)",
...     skip_if_nan=True, # If log10(0) is attempted the feature will be skipped
... )

Functions:

  • exp(x) - e^x

  • sqrt(x) - square root of x

  • log(x, b) - log of x with base b, if no base is provided the default is e

  • log10(x) - log base 10 of x

  • abs(x) - absolute value of x

  • sin(x) - sine of x

  • cos(x) - cosine of x

  • tan(x) - tangent of x

  • floor(x) - rounds x down to nearest integer

  • ceil(x) - rounds x up to nearest integer

  • round(x) - rounds x up or down to nearest integer

  • pow(x, y) - x to the power of y

  • min(x, y, z) - smallest value of x, y, z - takes 2 or more arguments

  • max(x, y, z) - largest value of x, y, z - takes 2 or more arguments

  • clamp(x, min, max) - returns x unless x is greater than max or less than min