update main.py --test case to conform to new architecture. Update error handling in --config case.

This commit is contained in:
Pim Nelissen
2026-02-25 14:37:35 +01:00
parent 561fb1dca1
commit 5c9841190f

View File

@ -5,10 +5,17 @@ import sys
from pandas.errors import ParserError from pandas.errors import ParserError
from yaml import YAMLError from yaml import YAMLError
from pg_rad.exceptions.exceptions import MissingNestedKeyError from pg_rad.exceptions.exceptions import (
MissingConfigKeyError,
OutOfBoundsError,
DimensionError,
InvalidIsotopeError
)
from pg_rad.logger.logger import setup_logger from pg_rad.logger.logger import setup_logger
from pg_rad.inputparser.parser import ConfigParser
from pg_rad.landscape.director import LandscapeDirector from pg_rad.landscape.director import LandscapeDirector
from pg_rad.plotting.landscape_plotter import LandscapeSlicePlotter from pg_rad.plotting.result_plotter import ResultPlotter
from pg_rad.simulator.engine import SimulationEngine
def main(): def main():
@ -23,7 +30,7 @@ def main():
parser.add_argument( parser.add_argument(
"--test", "--test",
action="store_true", action="store_true",
help="Load and run the test landscape" help="Load and run the test landscape."
) )
parser.add_argument( parser.add_argument(
"--loglevel", "--loglevel",
@ -41,19 +48,52 @@ def main():
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
if args.test: if args.test:
landscape = LandscapeDirector().build_test_landscape() test_yaml = """
plotter = LandscapeSlicePlotter() name: Test landscape
plotter.plot(landscape, save=args.saveplot) speed: 8.33
acquisition_time: 1
path:
length: 1000
segments:
- straight
sources:
test_source:
activity_MBq: 1000
position: [500, 100, 0]
isotope: CS137
"""
cp = ConfigParser(test_yaml).parse()
landscape = LandscapeDirector.build_from_config(cp)
output = SimulationEngine(
landscape=landscape,
runtime_spec=cp.runtime,
sim_spec=cp.options
).simulate()
plotter = ResultPlotter(landscape, output)
plotter.plot()
elif args.config: elif args.config:
try: try:
landscape = LandscapeDirector.build_from_config(args.config) cp = ConfigParser(args.config).parse()
plotter = LandscapeSlicePlotter() landscape = LandscapeDirector.build_from_config(cp)
plotter.plot(landscape, save=args.saveplot)
output = SimulationEngine(
landscape=landscape,
runtime_spec=cp.runtime,
sim_spec=cp.options
).simulate()
plotter = ResultPlotter(landscape, output)
plotter.plot()
except ( except (
MissingNestedKeyError, MissingConfigKeyError,
KeyError, KeyError,
YAMLError YAMLError,
): ):
logger.critical( logger.critical(
"The provided config file is invalid. " "The provided config file is invalid. "
@ -61,6 +101,18 @@ def main():
"an explanation of how to define a config file." "an explanation of how to define a config file."
) )
sys.exit(1) sys.exit(1)
except (
OutOfBoundsError,
DimensionError,
InvalidIsotopeError
) as e:
logger.critical(e)
logger.critical(
"One or more items in config are not specified correctly. "
"Please consult this log and fix the problem."
)
sys.exit(1)
except ( except (
FileNotFoundError, FileNotFoundError,
ParserError ParserError