Skip to content

Commit

Permalink
Merge 04c071b into 64351a0
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Mar 5, 2024
2 parents 64351a0 + 04c071b commit b313fb8
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 178 deletions.
3 changes: 0 additions & 3 deletions armi/bookkeeping/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ def defineEntryPoints():
from armi.cli import database

entryPoints = []
# Disabling ConvertDB because there is no other format to convert between. The
# entry point is rather general so leaving this here so we don't forget about it
# entryPoints.append(database.ConvertDB)
entryPoints.append(database.ExtractInputs)
entryPoints.append(database.InjectInputs)
entryPoints.append(visualization.VisFileEntryPoint)
Expand Down
83 changes: 0 additions & 83 deletions armi/bookkeeping/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,10 @@
location, without having to compose the full model.
"""
import os
from typing import Optional, List, Tuple

from armi import runLog

# re-export package components for easier import
from armi.bookkeeping.db.permissions import Permissions
from armi.bookkeeping.db.database3 import Database3
from armi.bookkeeping.db.databaseInterface import DatabaseInterface
from armi.bookkeeping.db.compareDB3 import compareDatabases
Expand Down Expand Up @@ -154,87 +152,6 @@ def loadOperator(pathToDb, loadCycle, loadNode, allowMissing=False):
return o


def convertDatabase(
inputDBName: str,
outputDBName: Optional[str] = None,
outputVersion: Optional[str] = None,
nodes: Optional[List[Tuple[int, int]]] = None,
):
"""
Convert database files between different versions.
Parameters
----------
inputDB
name of the complete hierarchy database
outputDB
name of the output database that should be consistent with XTView
outputVersion
version of the database to convert to. Defaults to latest version
nodes
optional list of specific (cycle,node)s to convert
"""
dbIn = databaseFactory(inputDBName, permission=Permissions.READ_ONLY_FME)

if dbIn.version == outputVersion:
runLog.important(
"The input database ({}) appears to already be in the desired "
"format ({})".format(inputDBName, dbIn.version)
)
return

outputDBName = outputDBName or "-converted".join(os.path.splitext(inputDBName))
dbOut = databaseFactory(
outputDBName, permission=Permissions.CREATE_FILE_TIE, version=outputVersion
)
# each DB load resets the verbosity to that of the run. Here we allow
# conversion users to overpower it.
conversionVerbosity = runLog.getVerbosity()
runLog.extra(f"Converting {dbIn} to DB version {outputVersion}")
with dbIn, dbOut:
dbNodes = list(dbIn.genTimeSteps())

if nodes is not None and any(node not in dbNodes for node in nodes):
raise RuntimeError(
"Some of the requested nodes are not in the source database.\n"
"Requested: {}\n"
"Present: {}".format(nodes, dbNodes)
)

# Making the bold assumption that we are working with HDF5
h5In = _getH5File(dbIn)
h5Out = _getH5File(dbOut)
dbOut.writeInputsToDB(None, *dbIn.readInputsFromDB())

for cycle, timeNode in dbNodes:
if nodes is not None and (cycle, timeNode) not in nodes:
continue
runLog.extra(f"Converting cycle={cycle}, timeNode={timeNode}")
timeStepsInOutDB = set(dbOut.genTimeSteps())
r = dbIn.load(cycle, timeNode)
if (r.p.cycle, r.p.timeNode) in timeStepsInOutDB:
runLog.warning(
"Time step ({}, {}) is already in the output DB. This "
"is probably due to repeated cycle/timeNode in the source DB; "
"deleting the existing time step and re-writing".format(
r.p.cycle, r.p.timeNode
)
)
del dbOut[r.p.cycle, r.p.timeNode, None]
runLog.setVerbosity(conversionVerbosity)
dbOut.writeToDB(r)

for auxPath in dbIn.genAuxiliaryData((cycle, timeNode)):
name = next(reversed(auxPath.split("/")))
auxOutPath = dbOut.getAuxiliaryDataPath((cycle, timeNode), name)
runLog.important(
"Copying auxiliary data for time ({}, {}): {} -> {}".format(
cycle, timeNode, auxPath, auxOutPath
)
)
h5In.copy(auxPath, h5Out, name=auxOutPath)


def _getH5File(db):
"""Return the underlying h5py File that provides the backing storage for a database.
Expand Down
59 changes: 0 additions & 59 deletions armi/cli/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,13 @@
"""Entry point into ARMI for manipulating output databases."""
import os
import pathlib
import re

from armi import context
from armi import runLog
from armi.cli.entryPoint import EntryPoint
from armi.utils.textProcessors import resolveMarkupInclusions


class ConvertDB(EntryPoint):
"""Convert databases between different versions."""

name = "convert-db"
mode = context.Mode.BATCH

def addOptions(self):
self.parser.add_argument("h5db", help="Input database path", type=str)
self.parser.add_argument(
"--output-name", "-o", help="output database name", type=str, default=None
)
self.parser.add_argument(
"--output-version",
help=(
"output database version. '2' or 'xtview' for older XTView database; '3' "
"for new format."
),
type=str,
default=None,
)

self.parser.add_argument(
"--nodes",
help="An optional list of time nodes to migrate. Should look like "
"`(1,0)(1,1)(1,2)`, etc",
type=str,
default=None,
)

def parse_args(self, args):
EntryPoint.parse_args(self, args)
if self.args.output_version is None:
self.args.output_version = "3"
elif self.args.output_version.lower() == "xtview":
self.args.output_version = "2"

if self.args.nodes is not None:
self.args.nodes = [
(int(cycle), int(node))
for cycle, node in re.findall(r"\((\d+),(\d+)\)", self.args.nodes)
]

def invoke(self):
from armi.bookkeeping.db import convertDatabase

if self.args.nodes is not None:
runLog.info(
"Converting the following time nodes: {}".format(self.args.nodes)
)

convertDatabase(
self.args.h5db,
outputDBName=self.args.output_name,
outputVersion=self.args.output_version,
nodes=self.args.nodes,
)


class ExtractInputs(EntryPoint):
"""
Recover input files from a database file.
Expand Down
34 changes: 2 additions & 32 deletions armi/cli/tests/test_runEntryPoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from armi.cli.checkInputs import CheckInputEntryPoint, ExpandBlueprints
from armi.cli.clone import CloneArmiRunCommandBatch, CloneSuiteCommand
from armi.cli.compareCases import CompareCases, CompareSuites
from armi.cli.database import ConvertDB, ExtractInputs, InjectInputs
from armi.cli.database import ExtractInputs, InjectInputs
from armi.cli.entryPoint import EntryPoint
from armi.cli.migrateInputs import MigrateInputs
from armi.cli.modify import ModifyCaseSettingsCommand
Expand All @@ -46,7 +46,7 @@ def test_entryPointInitialization(self):
entryPoints = getEntireFamilyTree(EntryPoint)

# Comparing to a minimum number of entry points, in case more are added.
self.assertGreater(len(entryPoints), 16)
self.assertGreater(len(entryPoints), 15)

for e in entryPoints:
entryPoint = e()
Expand Down Expand Up @@ -191,36 +191,6 @@ def test_compareSuitesBasics(self):
self.assertIsNone(cs.args.weights)


class TestConvertDB(unittest.TestCase):
def test_convertDbBasics(self):
cdb = ConvertDB()
cdb.addOptions()
cdb.parse_args(["/path/to/fake.h5"])

self.assertEqual(cdb.name, "convert-db")
self.assertEqual(cdb.args.output_version, "3")
self.assertIsNone(cdb.args.nodes)

# Since the file is fake, invoke() should exit early.
with mockRunLogs.BufferLog() as mock:
cdb.args.nodes = [1, 2, 3]
with self.assertRaises(ValueError):
cdb.invoke()
self.assertIn("Converting the", mock.getStdout())

def test_convertDbOutputVersion(self):
cdb = ConvertDB()
cdb.addOptions()
cdb.parse_args(["/path/to/fake.h5", "--output-version", "XtView"])
self.assertEqual(cdb.args.output_version, "2")

def test_convertDbOutputNodes(self):
cdb = ConvertDB()
cdb.addOptions()
cdb.parse_args(["/path/to/fake.h5", "--nodes", "(1,2)"])
self.assertEqual(cdb.args.nodes, [(1, 2)])


class TestExpandBlueprints(unittest.TestCase):
def test_expandBlueprintsBasics(self):
ebp = ExpandBlueprints()
Expand Down
3 changes: 2 additions & 1 deletion doc/release/0.3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ API Changes
#. Removing unused argument from ``Block.coords()``. (`PR#1651 <https://github.com/terrapower/armi/pull/1651>`_)
#. Removing unused method ``HexGrid.allPositionsInThird()``. (`PR#1655 <https://github.com/terrapower/armi/pull/1655>`_)
#. Removed unused methods: ``Reactor.getAllNuclidesIn()``, ``plotTriangleFlux()``. (`PR#1656 <https://github.com/terrapower/armi/pull/1656>`_)
#. Removing all references to database versions pre-``Database3``. (`PR#1658 <https://github.com/terrapower/armi/pull/1658>`_)
#. TBD

Bug Fixes
Expand All @@ -24,7 +25,7 @@ Bug Fixes

Changes that Affect Requirements
--------------------------------
#. (`PR#1651 <https://github.com/terrapower/armi/pull/1651>`_) - Very minor change to ``Block.coords()``, removing unused argument.
#. Very minor change to ``Block.coords()``, removing unused argument. (`PR#1651 <https://github.com/terrapower/armi/pull/1651>`_)
#. TBD


Expand Down

0 comments on commit b313fb8

Please sign in to comment.