Skip to content

Commit

Permalink
Changes to FTA module. Improved comments and debug.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbolisetti committed Nov 6, 2019
1 parent aa2235e commit 6fd8964
Show file tree
Hide file tree
Showing 12 changed files with 330 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ peacock_run_exe_tmp.i
*.py
*.pyc
!python/**/*.py
**/.ipynb_checkpoints

# Allow certain files in gold directories
!**/gold/*.e
Expand Down
418 changes: 277 additions & 141 deletions python/mastodonutils/FTA.py

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion python/mastodonutils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
"""General utilities for Mastodon"""
from .FTA import Quantification
5 changes: 5 additions & 0 deletions python/mastodonutils/test/logic1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TE,OR,IE3,IE4
IE4,OR,C4
IE3,OR,C3,IE2
IE2,AND,C2,IE1
IE1,OR,C1
4 changes: 4 additions & 0 deletions python/mastodonutils/test/logic1_bas_events_LNORM.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
C1,LNORM,1.88,0.5
C2,LNORM,3.78,0.79
C3,LNORM,2.33,0.76
C4,LNORM,3.66,0.45
4 changes: 4 additions & 0 deletions python/mastodonutils/test/logic1_bas_events_NORM.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
C1,NORM,0.01,0.003
C2,NORM,0.02,0.003
C3,NORM,0.03,0.003
C4,NORM,0.04,0.003
7 changes: 7 additions & 0 deletions python/mastodonutils/test/logic2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
TOP, AND, GATE1, GATE2
GATE1, OR, FT-N/m-1, FT-N/m-2, FT-N/m-3
GATE2, OR, B1, B3, B4
GATE3, OR, B2, B4
FT-N/m-1, AND, GATE3, B3, B5
FT-N/m-2, AND, GATE3, B1
FT-N/m-3, AND, B3, B5, B1
5 changes: 5 additions & 0 deletions python/mastodonutils/test/logic2_bas_events_NORM.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
B1,NORM,0.01,0.003
B2,NORM,0.02,0.003
B3,NORM,0.03,0.003
B4,NORM,0.04,0.003
B5,NORM,0.05,0.003
5 changes: 5 additions & 0 deletions python/mastodonutils/test/logic2_bas_events_PE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
B1,PE,0.01
B2,PE,0.02
B3,PE,0.03
B4,PE,0.04
B5,PE,0.05
2 changes: 1 addition & 1 deletion python/mastodonutils/test/test_fault_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def testTreeFromFile(self):
"""
Test creating tree from list.
"""
tree = FaultTree('events.txt')
tree = FaultTree('logic2.txt')
self.assertTree(tree)
self.assertMocus(tree.mocus)

Expand Down
37 changes: 20 additions & 17 deletions python/mastodonutils/test/test_quantification.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ def testInputs(self):
"""
# File Inputs
with self.assertRaises(IOError) as cm:
Quantification(logic='not_a_valid_filename.txt', bas_events='not_a_valid_filename.txt',
Quantification(name='test', logic='not_a_valid_filename.txt', basic_events='not_a_valid_filename.txt',
hazard='not_a_valid_filename.txt')
self.assertIn('does not exist', str(cm.exception))

with self.assertRaises(TypeError) as cm:
Quantification(logic=42, bas_events=42, hazard=42)
Quantification(name='test', logic=42, basic_events=42, hazard=42)
self.assertIn("must be a filename or a list.", str(cm.exception))

# Inputs for Fragility
inputs = Quantification('SSI_logic.txt', 'bas_events_ln.txt', analysis='Fragility',
inputs = Quantification('test', 'logic1.txt', 'logic1_bas_events_LNORM.txt', analysis='Fragility',
hazard='hazard.txt', IM=[0.1, 4], nbins=15)
self.assertEqual(inputs.logic, [['TE', 0, ['IE3', 'IE4']], ['IE4', 0, ['C4']],
['IE3', 0, ['C3', 'IE2']], ['IE2', 1, ['C2', 'IE1']],
Expand All @@ -40,23 +40,23 @@ def testInputs(self):
self.assertEqual(inputs.nbins, 15)

with self.assertRaises(TypeError) as cm:
Quantification('SSI_logic.txt', 'bas_events_ln.txt',
Quantification('testInputs', 'logic1.txt', 'logic1_bas_events_LNORM.txt',
analysis='Fragility', hazard='hazard.txt', IM=42)
self.assertEqual("The supplied items of IM range must be a list.", str(cm.exception))

with self.assertRaises(TypeError) as cm:
Quantification('SSI_logic.txt', 'bas_events_ln.txt',
Quantification('testInputs', 'logic1.txt', 'logic1_bas_events_LNORM.txt',
analysis='Fragility', hazard='hazard.txt', nbins=[42])
self.assertEqual("The supplied value of nbins must be an integer.", str(cm.exception))

with self.assertRaises(ValueError) as cm:
Quantification('SSI_logic.txt', 'bas_events_ln.txt',
Quantification('testInputs', 'logic1.txt', 'logic1_bas_events_LNORM.txt',
analysis='Fragility', hazard='hazard.txt', nbins=-15)
self.assertEqual("The supplied value of nbins must be a +ve integer.",
str(cm.exception))

# Inputs for Risk
inputs = Quantification('events.txt', 'bas_events.txt', analysis='Risk')
# Inputs for Risk analysis (not fragility)
inputs = Quantification('testInputs', 'logic2.txt', 'logic2_bas_events_PE.txt', analysis='Risk')
self.assertEqual(inputs.logic, [['TOP', 1, ['GATE1', 'GATE2']],
['GATE1', 0, ['FT-N/m-1', 'FT-N/m-2', 'FT-N/m-3']],
['GATE2', 0, ['B1', 'B3', 'B4']],
Expand All @@ -72,30 +72,33 @@ def testInputs(self):
self.assertEqual(inputs.nsamp, 1)
self.assertEqual(inputs.seed, None)

inputs = Quantification('events.txt', 'bas_events.txt', analysis='Risk',
# Testing for input errors making sure parameters are input correctly
inputs = Quantification('testInputs', 'logic2.txt', 'logic2_bas_events_PE.txt', analysis='Risk',
uncertainty=True, nsamp=1000, seed=436546754)
self.assertEqual(inputs.name, 'testInputs')
self.assertEqual(inputs.uncertainty, True)
self.assertEqual(inputs.nsamp, 1000)
self.assertEqual(inputs.seed, 436546754)

# Testing for input type errors and value errors
with self.assertRaises(TypeError) as cm:
Quantification('events.txt', 'bas_events.txt', analysis='Risk',
Quantification('testInputs', 'logic2.txt', 'logic2_bas_events_PE.txt', analysis='Risk',
uncertainty=True, nsamp=[1], seed=42.1)
self.assertEqual("The supplied value of nsamp must be an integer.", str(cm.exception))

with self.assertRaises(TypeError) as cm:
Quantification('events.txt', 'bas_events.txt', analysis='Risk',
Quantification('testInputs', 'logic2.txt', 'logic2_bas_events_PE.txt', analysis='Risk',
uncertainty=True, nsamp=10, seed=42.0)
self.assertEqual("The supplied value of seed must be an integer.", str(cm.exception))

with self.assertRaises(ValueError) as cm:
Quantification('events.txt', 'bas_events.txt', analysis='Risk',
Quantification('testInputs', 'logic2.txt', 'logic2_bas_events_PE.txt', analysis='Risk',
uncertainty=True, nsamp=-10, seed=42.0)
self.assertEqual("The supplied value of nsamp must be a +ve integer.",
str(cm.exception))

with self.assertRaises(ValueError) as cm:
Quantification('events.txt', 'bas_events.txt', analysis='Risk',
Quantification('testInputs', 'logic2.txt', 'logic2_bas_events_PE.txt', analysis='Risk',
uncertainty=True, nsamp=10, seed=-42)
self.assertEqual("The supplied value of seed must be a +ve integer.",
str(cm.exception))
Expand All @@ -104,7 +107,7 @@ def testTOPrisk(self):
"""
Function for asserting FTA top event risk.
"""
fta = Quantification('events.txt', 'bas_events_norm.txt', analysis='Risk')
fta = Quantification('testTOPrisk', 'logic2.txt', 'logic2_bas_events_NORM.txt', analysis='Risk')
self.assertRisk(fta)
self.assertIMratio(fta.be_im_ratio)
self.assertIMdiff(fta.be_im_diff)
Expand All @@ -114,11 +117,11 @@ def assertRisk(self, fta):
Helper to make sure the FTA top event risk is correct.
"""
# upper bound
self.assertEqual(fta.toprisk[0], 0.000704853860316601)
self.assertEqual(fta.toprisk_1[0], 0.000704853860316601)
# rare event
self.assertEqual(fta.toprisk[1], 0.0007050000000000001)
self.assertEqual(fta.toprisk_1[1], 0.0007050000000000001)
# min-max
self.assertEqual(fta.toprisk[2], 0.0006940240000000001)
self.assertEqual(fta.toprisk_1[2], 0.0006940240000000001)

def assertIMratio(self, imratio):
"""
Expand Down
1 change: 1 addition & 0 deletions python/mastodonutils/test/tests
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
[./Event]
type = PythonUnitTest
input = test_event.py
python = 2
[../]
[./FaultTree]
type = PythonUnitTest
Expand Down

0 comments on commit 6fd8964

Please sign in to comment.