Skip to content

Commit

Permalink
Bug fix to correct difference tabulation (#1040)
Browse files Browse the repository at this point in the history
* print out parameter name with warning

* Bug fix to correct difference tabulation when src has parameters not in ref

* Add unit test related to bug fix

* Fix small bug in string processing for name argument

* Black format

* Fix & simplify test that relied on bug to pass
  • Loading branch information
opotowsky committed Dec 21, 2022
1 parent 15a710f commit 6bd85c6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
17 changes: 8 additions & 9 deletions armi/bookkeeping/db/compareDB3.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,14 @@ def _compareSets(
src: set, ref: set, out: OutputWriter, name: Optional[str] = None
) -> int:
nDiffs = 0
printName = "" if name is None else name + " "
if ref - src:
nDiffs += len(ref - src)
out.writeln(
"ref has {}not in src: {}".format(name + " " or "", list(ref - src))
)
out.writeln("ref has {}not in src: {}".format(printName, list(ref - src)))

if src - ref:
nDiffs += len(ref - src)
out.writeln(
"src has {}not in ref: {}".format(name + " " or "", list(src - ref))
)
nDiffs += len(src - ref)
out.writeln("src has {}not in ref: {}".format(printName, list(src - ref)))

return nDiffs

Expand Down Expand Up @@ -493,8 +490,10 @@ def _compareComponentData(

if srcSpecial ^ refSpecial:
out.writeln(
"Could not compare data because one uses special formatting, "
"and the other does not. Ref: {} Src: {}".format(refSpecial, srcSpecial)
"Could not compare data for parameter {} because one uses special "
"formatting, and the other does not. Ref: {} Src: {}".format(
paramName, refSpecial, srcSpecial
)
)
diffResults.addDiff(
refGroup.name, paramName, numpy.inf, numpy.inf, numpy.inf
Expand Down
36 changes: 22 additions & 14 deletions armi/bookkeeping/db/tests/test_comparedb3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import numpy as np

from armi.bookkeeping.db.compareDB3 import (
_compareSets,
_compareAuxData,
_diffSimpleData,
_diffSpecialData,
Expand Down Expand Up @@ -51,6 +52,16 @@ def test_outputWriter(self):
txt = open(fileName, "r").read()
self.assertIn("Rubber", txt)

def test_compareSets(self):
shorter = set({1, 2, 3})
longer = set({1, 2, 3, 4})
fileName = "fakeOutWriter.txt"
with OutputWriter(fileName) as out:
nDiffs = _compareSets(shorter, longer, out, name="number")
self.assertEqual(nDiffs, 1)
nDiffs = _compareSets(longer, shorter, out, name="number")
self.assertEqual(nDiffs, 1)

def test_diffResultsBasic(self):
# init an instance of the class
dr = DiffResults(0.01)
Expand Down Expand Up @@ -123,38 +134,34 @@ def test_compareDatabaseSim(self):

# create two DBs, identical but for file names
dbs = []
for nCycles in range(1, 3):
for lenCycle in range(1, 3):
# build some test data
days = 100 * nCycles
cycles = [
{"step days": [days, days], "power fractions": [1, 0.5]}
] * nCycles
days = 100
cs = o.cs.modified(
newSettings={
"nCycles": nCycles,
"cycles": cycles,
"cycles": [
{"step days": [days, days], "power fractions": [1, 0.5]}
],
"reloadDBName": "something_fake.h5",
}
)

# create the tests DB
dbi = DatabaseInterface(r, cs)
dbi.initDB(fName=self._testMethodName + str(nCycles) + ".h5")
dbi.initDB(fName=self._testMethodName + str(lenCycle) + ".h5")
db = dbi.database

# populate the db with something
for cycle, node in (
(cycle, node) for cycle in range(nCycles + 1) for node in range(2)
):
r.p.cycle = cycle
r.p.cycle = 0
for node in range(2):
r.p.timeNode = node
r.p.cycleLength = days * 2
r.p.cycleLength = days * lenCycle
db.writeToDB(r)

# validate the file exists, and force it to be readable again
b = h5py.File(db._fullPath, "r")
dbKeys = sorted(b.keys())
self.assertEqual(len(dbKeys), 2 * (nCycles + 1) + 1)
self.assertEqual(len(dbKeys), 3)
self.assertIn("inputs", dbKeys)
self.assertIn("c00n00", dbKeys)
self.assertEqual(
Expand All @@ -168,6 +175,7 @@ def test_compareDatabaseSim(self):
# end-to-end validation that comparing a photocopy database works
diffs = compareDatabases(dbs[0]._fullPath, dbs[1]._fullPath)
self.assertEqual(len(diffs.diffs), 456)
# Cycle length is only diff (x3)
self.assertEqual(diffs.nDiffs(), 3)

def test_diffSpecialData(self):
Expand Down

0 comments on commit 6bd85c6

Please sign in to comment.