Issue with damage state functions code

Hi there,

I am a masters student at UC and am relatively new to Riskscape. I am trying to run an impact assessment on buildings from modelled debris flows with the wizard and have been having trouble trying to get it to run.

I haven’t had any problems with just running the simple is_exposed function but whenever I try running the function I have created with damage state equations and losses I get this error message:

Some problems were detected with the value given:
There was an unexpected error while running Riskscape. Set --show-stacktrace to show detailed error information.

I have copied my function code and project.ini code below

Any help is appreciated!

Cheers,
Hanna

[project]
description = Exposure and impact for buildings using impact pressure from Flow-R output
auto-import = true

[bookmark Marlborough_buildings (exposure-layer)]
location = Data/Marlborough_buildings.shp

[bookmark hazard_ip (hazard-layer)]
location = Data/hazard_ip.tif

[bookmark Marlborough_studyarea (area-layer)]
location = Data/Marlborough_studyarea.shp

[function exposure_risk]
location = functions/exposure_risk.py
argument-types = [buildings: anything, hazard: nullable(floating)]
return-type = text

Function:

ID = 'exposure_risk'
DESCRIPTION = 'Debris flow vulnerability function. Impact pressure for International Literature all building type '

ARGUMENT_TYPES = ['building', Nullable.of(Types.FLOATING)]

RETURN_TYPE = Struct.of('hazard_IP', Types.FLOATING) \
                .and('damage_ratio', Types.FLOATING) \
                .and('loss', Types.INTEGER)
                
def function(building, hazard):
    replacement_cost = Marlborough_buildings('rep_cost')
    e = 2.71828182846
   damage_ratio = 0.000
   hazard_ip = 0.0
    if hazard is not None and hazard > 0.0:
        exposed = True
	else:
        if Construction_Code = 4: 
           damage_ratio=1.129*(1-e(-0.007*hazard)^1.530)
        if Construction_Code = 3: 
           damage_ratio=1.067*(1-e(-0.002*hazard)^2.146)
 	if Construction_Code = 2: 
           damage_ratio=1.020*(1-e(-0.00004*hazard)^3.654)
	if Construction_Code = 1: 
           damage_ratio=1-e^((-0.0005*hazard)^1.690)
	hazard_ip = hazard

    else:
        hazard_ip = 0.0

    loss = damage_ratio * replacement_cost
    return {
        'hazard_ip': hazard_ip,
        'damage_ratio': damage_ratio,
        'loss': loss
        
    }

Hi Hanna,

Can you repeat running the model with the --show-stacktrace option (you can use -e for short).

E.g. if you have a saved wizard model that you are running, the command would be riskscape -e model run MODELNAME

Or if you are running it via the wizard each time, then you’d use riskscape -e wizard.

You should then get a stack trace displayed. Paste the stack-trace in a post.

It looks like there are a few errors in your Python code. You might find it easier to use test your code out directly in Python before trying to run it in a model. Although I don’t think this is the root cause of the problem.

In general, your function should look more like this, but the damage_ratio lines still aren’t valid Python and need a little work.

def function(building, hazard):
	replacement_cost = building['rep_cost']
	Construction_Code = building['Construction_Code']
	e = 2.71828182846
	damage_ratio = 0.000
	hazard_ip = 0.0
	if hazard is not None and hazard > 0.0:
		if Construction_Code == 4: 
			damage_ratio=1.129*(1-e(-0.007 * hazard)^1.530)
		if Construction_Code == 3: 
			damage_ratio=1.067*(1-e(-0.002 * hazard)^2.146)
		if Construction_Code == 2: 
			damage_ratio=1.020*(1-e(-0.00004 * hazard)^3.654)
		if Construction_Code == 1: 
			damage_ratio=1-e^((-0.0005 * hazard)^1.690)
			hazard_ip = hazard

	loss = damage_ratio * replacement_cost
	return {
		'hazard_ip': hazard_ip,
		'damage_ratio': damage_ratio,
		'loss': loss
	}

You should be able to get rid of the ARGUMENT_TYPES/etc at the top of the file. And try to avoid mixing spaces and tabs.

Hope that helps.
Tim

Hi Tim,

Thank you for your quick reply! Here is the stack trace from the fixed code you replied with. I will keep trouble shooting this weekend to try and get it running :slight_smile:

Cheers,
Hanna

Some problems were detected with the value given:
[ERROR] function 'exposure_risk' (from P:\RiskScape_Projects\Building impacts\project.ini) is not valid
  - Problems found with INI file section '[function exposure_risk]' in file:///P:/RiskScape_Projects/Building%20impacts/project.ini
    - Failed to create function from file:///P:/RiskScape_Projects/Building%20impacts/functions/exposure_risk.py
      - Could not create Jython function
        - NameError: name 'Struct' is not defined - File "file:///P:/RiskScape_Projects/Building%20impacts/functions/exposure_risk.py", line 4```

Hi Hanna,

That doesn’t look like it’s using the new code, it looks like it’s still using your original code. Maybe double-check that you are editing and saving the correct file as well.

Cheers,
Tim

Hi Hanna - did you manage to get your model up and running?

Cheers,
Nick