-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluation_metrics.py
84 lines (78 loc) · 2.55 KB
/
evaluation_metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Author: Zhengyang Li
# Email: zhengyang.li@connect.polyu.hk, lzy95@uw.edu
# Date: 2023-04-22
# Description: This file contains the evaluation metrics for community resilience.
import numpy as np
def get_survival_rate(resource, t):
"""
Method:
Given the time stamp, calculate the survival rate.
Parameters:
resource: np.array
The resource array.
t: float
The time stamp.
Returns:
survival_rate: float
"""
return np.sum(resource >= t) / len(resource)
def get_survival_curve(resource: list or np.array, time_stamps: list or np.array) -> np.array:
"""
Method:
Given the time stamps, calculate the survival curve.
Parameters:
resource: np.array
The resource array.
time_stamps: list or np.array
The time stamps.
Returns:
survival_curve: np.array
"""
resource = np.array(resource) # Convert resource to a NumPy array
survival_curve = np.sum(resource[:, np.newaxis] >= time_stamps, axis=0)
return survival_curve
def get_expected_survival_curve(scenario_survival_curves: np.array) -> np.array:
"""
Method:
Given the scenario survival curves, calculate the expected survival curve.
Parameters:
scenario_survival_curves: np.array. row is the scenario, column is the time stamp.
The scenario survival curves.
Returns:
expected_survival_curve: np.array
"""
expected_survival_curve = np.zeros(scenario_survival_curves.shape[1])
for i in range(0, scenario_survival_curves.shape[1]):
expected_survival_curve[i] = np.mean(scenario_survival_curves[:, i])
return expected_survival_curve
def get_resilience(x:list, y:list):
"""
Method:
Given the lower bound, upper bound, and function values, calculate the resilience.
Parameters:
x: list
The x values.
y: list
The function values.
Returns:
resilience: float
"""
resilience = 0
intervals = np.array(x[1:]) - np.array(x[:-1])
func_vals = (np.array(y[:-1]) + np.array(y[1:]))/2
resilience = np.sum(intervals * func_vals)
return resilience
def get_resilience_loss(x:list, y:list):
"""
Method:
Given the lower bound, upper bound, and function values, calculate the resilience loss.
Parameters:
x: list
The x values.
y: list
The function values.
Returns:
resilience_loss: float
"""
interval = x[-1] - x[0]
return interval * y[0] - get_resilience(x, y)