What is a 'λ-Scoped(x, scope={})'

When trying to run the pipeline below

input(relation: 'testcsv', name: 'exposure')
->
select({
	create_continuous(
	[0.0,0.1,0.2],
	[0,500,1000]) as testCurve
})
->
select({apply_continuous(testCurve, [250,750])})

I am getting the error

Failed to validate model for execution
  - Failed to validate 'select({create_continuous([0.0, 0.1, 0.2], [0, 500, 1000]) as testCurve})' step for execution
    - Failed to validate expression '{create_continuous([0.0, 0.1, 0.2], [0, 500, 1000]) as testCurve}' against input type {exposure=>{}}
      - Problems found with 'create_continuous' riskscape function
        - Type mismatch for apply-to. Expected 'λ-Scoped(x, scope={})' but found 'List[Integer]'

What is a ‘λ-Scoped(x, scope={})’ and how can I use apply_continuous on a list?

My expected output was
0.05
0.15

Kia ora,

That’s a scoped lambda type. There’s an explanation in the docs about what RiskScape lambda expressions are here:
https://riskscape.org.nz/docs/intermediate/write-expressions.html#lambda-expressions

The ‘scoped’ part just means the lambda expression can access any other attributes that are accessible for that particular pipeline step (e.g. exposure attributes).

The create_continuous() function expects a lambda expression that will yield a y value for a given x value. (Replace x and y with domain-appropriate modelling terminology, e.g. x=flood depth, y=DR). For example, a simple example of a lambda expression might be: x -> if(x > 500, then: 0.2, else: 0.1).

This is not exactly what you want here. What you want is more like ‘given a predetermined x value, return a predetermined y value’. The simplest way to do this in a pipeline is via a ‘lookup table’. Here’s an example:

input(value: { x_point: [0.0,500.0,1000.0], y_point: [0.0,0.1,0.2] })
->
unnest([x_point, y_point]) as foo
->
group({ to_lookup_table(key: x_point, value: y_point, options: { unique: true }) as lookup_table })
->
select({
	create_continuous(
	[0.0,500.0,1000.0],
	x -> lookup(lookup_table, x)) as testCurve
})
->
select({
        apply_continuous(testCurve, 250) as y250,
        apply_continuous(testCurve, 750) as y750
})

Hope that helps.

Tim

Thanks Tim that is working well.

Is there a way I can reuse the key from the lookup table when we create the curve? I tried

select({
	create_continuous(
	lookup_table.key,
	x -> lookup(lookup_table, x)) as testCurve
})

as well as foo.x_point

but neither are working

Probably the simplest way is to use a model parameter for the x points in the curve, e.g.

input(value: { x_point: $interpolation_points, y_point: [0.0,0.1,0.2] })
->
unnest([x_point, y_point]) as foo
->
group({ to_lookup_table(key: x_point, value: y_point, options: { unique: true }) as lookup_table })
->
select({
	create_continuous(
  	  $interpolation_points,
	  x -> lookup(lookup_table, x)) as testCurve
})
->
select({
        apply_continuous(testCurve, 250) as y250,
        apply_continuous(testCurve, 750) as y750
})

That assume you’re running this as a model (rather than using the riskscape pipeline evaluate command). E.g.

[model example_curve]
framework = pipeline
location = foo.txt
param.interpolation_points = [0.0,500.0,1000.0]

Hope that helps.