Skip to content

Commit 058ad35

Browse files
committed
Added full matrix calculation and directory parsing
1 parent 359f02c commit 058ad35

File tree

4 files changed

+86
-4
lines changed

4 files changed

+86
-4
lines changed

cpm/parse.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
from cpm.exceptions import *
22
from cpm.models import DSM
3+
from os import listdir
4+
import re
5+
6+
7+
def parse_csv_dir(dir_path: str, pattern: str = None, delimiter: str = 'auto',
8+
encoding: str = 'utf-8', instigator: str = 'column') -> list[DSM]:
9+
"""
10+
Parse a directory of CSVs. A pattern for what the filename needs to include can be used
11+
as an inclusivity-filter.
12+
:param path:
13+
:param pattern:
14+
:param delimiter:
15+
:param encoding:
16+
:param instigator:
17+
:return:
18+
"""
19+
dsm_array = []
20+
p = None
21+
if pattern is not None:
22+
p = re.compile(pattern, re.DOTALL)
23+
24+
for filename in listdir(dir_path):
25+
if p and p.match(filename) is None:
26+
continue
27+
filepath = dir_path + '/' + filename
28+
print(filepath)
29+
dsm_array.append(parse_csv(filepath))
30+
31+
return dsm_array
332

433

534
def parse_csv(filepath: str, delimiter: str = 'auto', encoding: str = 'utf-8', instigator: str = 'column'):

cpm/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Union
2+
from cpm.models import ChangePropagationTree, DSM
3+
4+
5+
def calculate_risk_matrix(dsm_impact: DSM, dsm_likelihood: DSM, search_depth=4) \
6+
-> list[list[Union[float, str]]]:
7+
"""
8+
Run Change Propagation algorithm on entire DSM, and generate a risk matrix.
9+
:param dsm_impact:
10+
:param dsm_likelihood:
11+
:param search_depth:
12+
:return:
13+
"""
14+
15+
cpm: list[list[Union[float, str]]] = []
16+
17+
for l_index, lcol in enumerate(dsm_likelihood.columns):
18+
cpm.append([])
19+
20+
for i_index, icol in enumerate(dsm_impact.columns):
21+
cpt = ChangePropagationTree(i_index, l_index,
22+
dsm_impact=dsm_impact,
23+
dsm_likelihood=dsm_likelihood)
24+
cpt.propagate(search_depth=search_depth)
25+
cpm[l_index].append(cpt.get_risk())
26+
27+
return cpm

tests/test_propagation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ def test_risk_calculation():
1010
depth = 4
1111
res_mtx: list[list[float]] = []
1212

13-
for i, col in enumerate(dsm_p.columns):
13+
for i, _ in enumerate(dsm_p.columns):
1414
res_mtx.append([])
15-
for j, col in enumerate(dsm_p.columns):
15+
for j, _ in enumerate(dsm_p.columns):
1616
cpt = ChangePropagationTree(start_index=j, target_index=i, dsm_impact=dsm_i, dsm_likelihood=dsm_p)
1717
cpt.propagate(search_depth=depth)
1818
r = cpt.get_risk()
1919
res_mtx[i].append(r)
2020

2121
dsm_r = parse_csv('./tests/test-assets/dsm-cpx-answers-risks.csv')
2222

23-
for i, col in enumerate(dsm_r.columns):
24-
for j, col in enumerate(dsm_r.columns):
23+
for i, _ in enumerate(dsm_r.columns):
24+
for j, _ in enumerate(dsm_r.columns):
2525
if dsm_r.matrix[i][j] is None:
2626
continue
2727
assert abs(res_mtx[i][j] - dsm_r.matrix[i][j]) < 0.001

tests/test_utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
from cpm.parse import parse_csv
3+
from cpm.utils import calculate_risk_matrix
4+
5+
6+
def test_matrix_risk_calculation():
7+
dsm_p = parse_csv('./tests/test-assets/dsm-cpx-probs.csv')
8+
dsm_i = parse_csv('./tests/test-assets/dsm-cpx-imps.csv')
9+
dsm_answers = parse_csv('./tests/test-assets/dsm-cpx-answers-risks.csv')
10+
11+
depth = 4
12+
13+
dsm_risk = calculate_risk_matrix(dsm_i, dsm_p, search_depth=depth)
14+
15+
assert dsm_risk is not None
16+
assert len(dsm_risk) > 0
17+
18+
for i, _ in enumerate(dsm_risk):
19+
for j, _ in enumerate(dsm_risk):
20+
if i == j:
21+
continue
22+
23+
print(f'{dsm_answers.matrix[i][j]} {dsm_risk[i][j]}')
24+
if dsm_answers.matrix[i][j] in [None, 0] and dsm_risk[i][j] in [None, 0]:
25+
continue
26+
assert abs(dsm_answers.matrix[i][j] - dsm_risk[i][j]) < 0.001

0 commit comments

Comments
 (0)