Skip to content

Commit

Permalink
Renaming Database3 to Database, but preserving the API
Browse files Browse the repository at this point in the history
  • Loading branch information
john-science committed Oct 18, 2024
1 parent ecfac84 commit 4ba0e5a
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 74 deletions.
8 changes: 5 additions & 3 deletions armi/bookkeeping/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@
from armi import runLog

# re-export package components for easier import
from armi.bookkeeping.db.database import Database
from armi.bookkeeping.db.database3 import Database3
from armi.bookkeeping.db.databaseInterface import DatabaseInterface
from armi.bookkeeping.db.compareDB3 import compareDatabases
from armi.bookkeeping.db.factory import databaseFactory


__all__ = [
"Database",
"Database3",
"DatabaseInterface",
"compareDatabases",
Expand Down Expand Up @@ -98,7 +100,7 @@ def loadOperator(
The time node to load the reactor from.
statePointName: str
State point name at the end, E.G. `EOC` or `EOL`.
Full name would be C0N2EOC, see database3.getH5GroupName
Full name would be C0N2EOC, see database.getH5GroupName
allowMissing : bool
Whether to emit a warning, rather than crash if reading a database
with undefined parameters. Default False.
Expand Down Expand Up @@ -138,7 +140,7 @@ def loadOperator(
"of the database."
)

db = Database3(pathToDb, "r")
db = Database(pathToDb, "r")
with db:
# init Case here as it keeps track of execution time and assigns a reactor
# attribute. This attribute includes the time it takes to initialize the reactor
Expand Down Expand Up @@ -181,7 +183,7 @@ def _getH5File(db):
All this being said, we are probably violating this already with genAuxiliaryData,
but we have to start somewhere.
"""
if isinstance(db, Database3):
if isinstance(db, Database):
return db.h5db
else:
raise TypeError("Unsupported Database type ({})!".format(type(db)))
15 changes: 7 additions & 8 deletions armi/bookkeeping/db/compareDB3.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
import numpy as np

from armi import runLog
from armi.bookkeeping.db import database3
from armi.bookkeeping.db.database3 import Database3
from armi.bookkeeping.db import database
from armi.bookkeeping.db.database import Database
from armi.bookkeeping.db.factory import databaseFactory
from armi.bookkeeping.db.permissions import Permissions
from armi.reactor.composites import ArmiObject
Expand Down Expand Up @@ -179,7 +179,7 @@ def compareDatabases(
with OutputWriter(outputName) as out:
ref = databaseFactory(refFileName, Permissions.READ_ONLY_FME)
src = databaseFactory(srcFileName, Permissions.READ_ONLY_FME)
if not isinstance(ref, Database3) or not isinstance(src, Database3):
if not isinstance(ref, Database) or not isinstance(src, Database):
raise TypeError(
"This database comparer only knows how to deal with database version "
"3; received {} and {}".format(type(ref), type(src))
Expand Down Expand Up @@ -363,8 +363,8 @@ def _diffSpecialData(
return

try:
src = database3.unpackSpecialData(srcData[()], srcData.attrs, paramName)
ref = database3.unpackSpecialData(refData[()], refData.attrs, paramName)
src = database.unpackSpecialData(srcData[()], srcData.attrs, paramName)
ref = database.unpackSpecialData(refData[()], refData.attrs, paramName)
except Exception:
runLog.error(
f"Unable to unpack special data for paramName {paramName}. "
Expand All @@ -380,9 +380,8 @@ def _diffSpecialData(
diffResults.addDiff(compName, paramName, np.inf, np.inf, np.inf)
return

# make sure not to try to compare empty arrays. Numpy is mediocre at
# these; they are super degenerate and cannot participate in concatenation.
# Why?
# Make sure not to try to compare empty arrays. Numpy is mediocre at these;
# they are super degenerate and cannot participate in concatenation.
if 0 not in dSrc.shape:
# Use the mean of the two to calc relative error. This is more robust to
# changes that cause one of the values to be zero, while the other is
Expand Down
30 changes: 14 additions & 16 deletions armi/bookkeeping/db/databaseInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from armi import context
from armi import interfaces
from armi import runLog
from armi.bookkeeping.db.database3 import Database3, getH5GroupName
from armi.bookkeeping.db.database import Database, getH5GroupName
from armi.reactor.parameters import parameterDefinitions
from armi.reactor.composites import ArmiObject
from armi.bookkeeping.db.typedefs import History, Histories
Expand Down Expand Up @@ -118,11 +118,10 @@ def initDB(self, fName: Optional[os.PathLike] = None):

if self.cs["reloadDBName"].lower() == str(self._dbPath).lower():
raise ValueError(
"It appears that reloadDBName is the same as the case "
"title. This could lead to data loss! Rename the reload DB or the "
"case."
"It appears that reloadDBName is the same as the case title. "
"This could lead to data loss! Rename the reload DB or the case."
)
self._db = Database3(self._dbPath, "w")
self._db = Database(self._dbPath, "w")
self._db.open()

# Grab geomString here because the DB-level has no access to the reactor or
Expand Down Expand Up @@ -206,7 +205,7 @@ def interactDistributeState(self) -> None:
if context.MPI_RANK > 0:
# DB may not exist if distribute state is called early.
if self._dbPath is not None and os.path.exists(self._dbPath):
self._db = Database3(self._dbPath, "r")
self._db = Database(self._dbPath, "r")
self._db.open()

def distributable(self):
Expand All @@ -227,7 +226,7 @@ def prepRestartRun(self):
This method loads the state of a reactor from a particular point in time
from a standard ARMI
:py:class:`Database <armi.bookkeeping.db.database3.Database3>`. This is a
:py:class:`Database <armi.bookkeeping.db.database.Database>`. This is a
major use-case for having ARMI databases in the first case. And restarting
from such a database is easy, you just need to set a few settings::
Expand All @@ -251,7 +250,7 @@ def prepRestartRun(self):
startCycle = self.cs["startCycle"]
startNode = self.cs["startNode"]

with Database3(reloadDBName, "r") as inputDB:
with Database(reloadDBName, "r") as inputDB:
loadDbCs = inputDB.loadCS()

# pull the history up to the cycle/node prior to `startCycle`/`startNode`
Expand Down Expand Up @@ -312,12 +311,12 @@ def _getLoadDB(self, fileName):
if self._db is not None and fileName == self._db._fileName:
yield self._db
elif os.path.exists(fileName):
yield Database3(fileName, "r")
yield Database(fileName, "r")
else:
if self._db is not None:
yield self._db
if os.path.exists(self.cs["reloadDBName"]):
yield Database3(self.cs["reloadDBName"], "r")
yield Database(self.cs["reloadDBName"], "r")

def loadState(self, cycle, timeNode, timeStepName="", fileName=None):
"""
Expand Down Expand Up @@ -371,15 +370,14 @@ def getHistory(
"""
Get historical parameter values for a single object.
This is mostly a wrapper around the same function on the ``Database3`` class,
This is mostly a wrapper around the same function on the ``Database`` class,
but knows how to return the current value as well.
See Also
--------
Database3.getHistory
Database.getHistory
"""
# make a copy so that we can potentially remove timesteps without affecting the
# caller
# make a copy so that we can potentially remove timesteps without affecting the caller
timeSteps = copy.copy(timeSteps)
now = (self.r.p.cycle, self.r.p.timeNode)
nowRequested = timeSteps is None
Expand Down Expand Up @@ -411,12 +409,12 @@ def getHistories(
"""
Get historical parameter values for one or more objects.
This is mostly a wrapper around the same function on the ``Database3`` class,
This is mostly a wrapper around the same function on the ``Database`` class,
but knows how to return the current value as well.
See Also
--------
Database3.getHistories
Database.getHistories
"""
now = (self.r.p.cycle, self.r.p.timeNode)
nowRequested = timeSteps is None
Expand Down
16 changes: 7 additions & 9 deletions armi/bookkeeping/db/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import pathlib
from typing import Optional

from armi.bookkeeping.db.database3 import Database3
import h5py

from armi.bookkeeping.db.database import Database
from armi.bookkeeping.db import permissions


Expand All @@ -30,7 +32,7 @@ def databaseFactory(dbName: str, permission: str, version: Optional[str] = None)
permission: str
String defining permission, `r` for read only. See armi.bookeeping.db.permissions
version: str, optional
Version of database you want to read or write. In many cases ARMI will
Version of database you want to read or write. In most cases ARMI will
auto-detect. For advanced users.
Notes
Expand All @@ -41,8 +43,6 @@ def databaseFactory(dbName: str, permission: str, version: Optional[str] = None)
interrogate the type of the returned object to figure out to do based on whatever it
needs.
"""
import h5py

dbPath = pathlib.Path(dbName)

# if it's not an hdf5 file, we dont even know where to start...
Expand Down Expand Up @@ -74,19 +74,17 @@ def databaseFactory(dbName: str, permission: str, version: Optional[str] = None)
)

if majorversion == "3":
return Database3(dbPath, permission)
return Database(dbPath, permission)

raise ValueError("Unable to determine Database version for {}".format(dbName))

elif permission in permissions.Permissions.write:
majorversion = version.split(".")[0] if version else "3"
if majorversion == "2":
raise ValueError(
'Database version 2 ("XTView database") is no longer '
"supported. To migrate to a newer version, use version 0.1.5 to "
"migrate."
"supported. To migrate to a newer version, use version 0.1.5 to migrate."
)
if majorversion == "3":
return Database3(dbPath, permission)
return Database(dbPath, permission)

return None
10 changes: 5 additions & 5 deletions armi/bookkeeping/db/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
When interacting with the database file, the :py:class:`Layout` class is used to help
map the hierarchical Composite Reactor Model to the flat representation in
:py:class:`Database3 <armi.bookkeeping.db.database3.Database3>`.
:py:class:`Database <armi.bookkeeping.db.database.Database>`.
This module also stores packing/packing tools to support
:py:class:`Database3 <armi.bookkeeping.db.database3.Database3>`, as well as datbase
:py:class:`Database <armi.bookkeeping.db.database.Database>`, as well as datbase
versioning information.
"""

Expand All @@ -44,7 +44,7 @@
from armi.reactor.reactors import Core
from armi.reactor.reactors import Reactor

# Here we store the Database3 version information.
# Here we store the Database version information.
DB_MAJOR = 3
DB_MINOR = 4
DB_VERSION = f"{DB_MAJOR}.{DB_MINOR}"
Expand Down Expand Up @@ -95,7 +95,7 @@ class Layout:
"""
The Layout class describes the hierarchical layout of the Composite Reactor model
in a flat representation for
:py:class:`Database3 <armi.bookkeeping.db.database3.Database3>`.
:py:class:`Database <armi.bookkeeping.db.database.Database>`.
A Layout is built by starting at the root of a composite tree and recursively
appending each node in the tree to a list of data. So the data will be ordered by
Expand Down Expand Up @@ -138,7 +138,7 @@ def __init__(self, version: Tuple[int, int], h5group=None, comp=None):
# There is a minor asymmetry here in that before writing to the DB, this is
# truly a flat list of tuples. However when reading, this may contain lists of
# tuples, which represent MI locations. This comes from the fact that we map the
# tuples to Location objects in Database3._compose, but map from Locations to
# tuples to Location objects in Database._compose, but map from Locations to
# tuples in Layout._createLayout. Ideally we would handle both directions in the
# same place so this can be less surprising. Resolving this would require
# changing the interface of the various pack/unpack functions, which have
Expand Down
4 changes: 2 additions & 2 deletions armi/bookkeeping/db/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import unittest

from armi import context
from armi.bookkeeping.db import database3
from armi.bookkeeping.db import database
from armi.bookkeeping.db import layout
from armi.reactor import grids
from armi.utils.directoryChangers import TemporaryDirectoryChanger
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_locationPackingOldVersion(self):
def test_close(self):
intendedFileName = "xyz.h5"

db = database3.Database3(intendedFileName, "w")
db = database.Database(intendedFileName, "w")
self.assertEqual(db._fileName, intendedFileName)
self.assertIsNone(db._fullPath) # this isn't set until the db is opened

Expand Down
4 changes: 2 additions & 2 deletions armi/bookkeeping/mainInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from armi import operators
from armi import runLog
from armi import utils
from armi.bookkeeping.db.database3 import Database3
from armi.bookkeeping.db.database import Database
from armi.settings.fwSettings.globalSettings import CONF_COPY_FILES_FROM
from armi.settings.fwSettings.globalSettings import CONF_COPY_FILES_TO
from armi.utils import pathTools
Expand Down Expand Up @@ -171,7 +171,7 @@ def interactEveryNode(self, cycle, node):
# skip at BOL because interactBOL handled it.
pass
else:
with Database3(self.cs["reloadDBName"], "r") as db:
with Database(self.cs["reloadDBName"], "r") as db:
r = db.load(cycle, node, self.cs)

self.o.reattach(r, self.cs)
Expand Down
4 changes: 2 additions & 2 deletions armi/bookkeeping/visualization/tests/test_vis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pyevtk.vtk import VtkTetra

from armi import settings
from armi.bookkeeping.db import Database3
from armi.bookkeeping.db import Database
from armi.bookkeeping.visualization import utils
from armi.bookkeeping.visualization import vtk
from armi.bookkeeping.visualization import xdmf
Expand Down Expand Up @@ -100,7 +100,7 @@ def test_dumpReactorVtk(self):
def test_dumpReactorXdmf(self):
# This does a lot, and is hard to verify. at least make sure it doesn't crash
with TemporaryDirectoryChanger(dumpOnException=False):
db = Database3("testDatabase.h5", "w")
db = Database("testDatabase.h5", "w")
with db:
db.writeToDB(self.r)
dumper = xdmf.XdmfDumper("testVtk", inputName="testDatabase.h5")
Expand Down
14 changes: 6 additions & 8 deletions armi/bookkeeping/visualization/vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,21 @@
vertices than needed. Some fancy canned algorithms probably exist to do this, and it
wouldn't be too difficult to do here either. Also future work, but probably not super
important unless dealing with really big meshes.
"""

from typing import Dict, Any, List, Optional, Set, Tuple

import numpy as np
from pyevtk.vtk import VtkGroup

from armi import runLog
from armi.bookkeeping.db import database
from armi.bookkeeping.visualization import dumper
from armi.bookkeeping.visualization import utils
from armi.reactor import assemblies
from armi.reactor import blocks
from armi.reactor import composites
from armi.reactor import reactors
from armi.reactor import parameters
from armi.bookkeeping.db import database3
from armi.bookkeeping.visualization import dumper
from armi.bookkeeping.visualization import utils
from armi.reactor import reactors


class VtkDumper(dumper.VisFileDumper):
Expand Down Expand Up @@ -109,7 +107,7 @@ def dumpState(
blockData = _collectObjectData(blks, includeParams, excludeParams)
assemData = _collectObjectData(assems, includeParams, excludeParams)
# block number densities are special, since they arent stored as params
blockNdens = database3.collectBlockNumberDensities(blks)
blockNdens = database.collectBlockNumberDensities(blks)
# we need to copy the number density vectors to guarantee unit stride, which
# pyevtk requires. Kinda seems like something it could do for us, but oh well.
blockNdens = {key: np.array(value) for key, value in blockNdens.items()}
Expand Down Expand Up @@ -180,7 +178,7 @@ def _collectObjectData(
continue

try:
data = database3.replaceNonesWithNonsense(data, pDef.name, nones=nones)
data = database.replaceNonesWithNonsense(data, pDef.name, nones=nones)
except (ValueError, TypeError):
# Looks like we have some weird data. We might be able to handle it
# with more massaging, but probably not visualizable anyhow
Expand Down
Loading

0 comments on commit 4ba0e5a

Please sign in to comment.