Skip to content

Commit 8f9b67e

Browse files
committed
Improved input robustness
- Fixed issue where a non-zero likelihood could not be combined with zero impact. - Improved overall input robustness and clarified exception messages.
1 parent 3325fdf commit 8f9b67e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cpm/models.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,21 @@ def build_node_network(self, instigator: str) -> dict[int, 'GraphNode']:
4040
if i == j:
4141
continue
4242
# Ignore empty cells
43-
if not col:
43+
if col == "" or col is None:
4444
continue
45+
46+
numerical_value = 0.0
47+
48+
try:
49+
numerical_value = float(col)
50+
except ValueError:
51+
raise ValueError('Unexpected DSM cell value. Could not be parsed as a float. Control DSM input.')
52+
4553
# Add interaction to node network
4654
if instigator == 'column':
47-
network_dict[j].add_neighbour(network_dict[i], float(col)) # Assumes cell only contains a float.
55+
network_dict[j].add_neighbour(network_dict[i], numerical_value) # Assumes cell only contains a float.
4856
elif instigator == 'row':
49-
network_dict[i].add_neighbour(network_dict[j], float(col))
57+
network_dict[i].add_neighbour(network_dict[j], numerical_value)
5058
else:
5159
raise TypeError('Instigator needs to either be "column" or "row".')
5260

@@ -109,7 +117,11 @@ def get_risk(self):
109117
# These nodes are not connected.
110118
return 0
111119

112-
# Final connection is the only one where we care about impact.
120+
# If the impact is null, then return 0.
121+
if self.node.index not in self.parent.impact_node.neighbours:
122+
raise ValueError('Unexpected empty DSM cell. The final impact cell was null. Check if DSMs are valid.')
123+
124+
# Final connection is the only one where we care about impact
113125
return self.parent.impact_node.neighbours[self.node.index]
114126

115127
prob = 1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '1.0.8'
3+
VERSION = '1.0.9'
44
DESCRIPTION = 'Tool for calculating risk of change propagation in a system.'
55

66
with open("README.md", "r") as fh:

0 commit comments

Comments
 (0)