Skip to content

Commit

Permalink
divide main function
Browse files Browse the repository at this point in the history
  • Loading branch information
aoymt committed Oct 30, 2024
1 parent 46b7ff6 commit b17d7ce
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
1 change: 1 addition & 0 deletions src/odatse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
from ._runner import Runner
from . import algorithm
from ._main import main
from ._initialize import initialize

__version__ = "3.0-dev"
41 changes: 41 additions & 0 deletions src/odatse/_initialize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import odatse

def initialize():
import argparse

parser = argparse.ArgumentParser(
description=(
"Data-analysis software of quantum beam "
"diffraction experiments for 2D material structure"
)
)
parser.add_argument("inputfile", help="input file with TOML format")
parser.add_argument("--version", action="version", version=odatse.__version__)

mode_group = parser.add_mutually_exclusive_group()
mode_group.add_argument("--init", action="store_true", help="initial start (default)")
mode_group.add_argument("--resume", action="store_true", help="resume intterupted run")
mode_group.add_argument("--cont", action="store_true", help="continue from previous run")

parser.add_argument("--reset_rand", action="store_true", default=False, help="new random number series in resume or continue mode")

args = parser.parse_args()


if args.init is True:
run_mode = "initial"
elif args.resume is True:
run_mode = "resume"
if args.reset_rand is True:
run_mode = "resume-resetrand"
elif args.cont is True:
run_mode = "continue"
if args.reset_rand is True:
run_mode = "continue-resetrand"
else:
run_mode = "initial" # default

info = odatse.Info.from_file(args.inputfile)
# info.algorithm.update({"run_mode": run_mode})

return info, run_mode
56 changes: 4 additions & 52 deletions src/odatse/_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,10 @@

import sys
import odatse
import odatse.mpi

def main():
import argparse

parser = argparse.ArgumentParser(
description=(
"Data-analysis software of quantum beam "
"diffraction experiments for 2D material structure"
)
)
parser.add_argument("inputfile", help="input file with TOML format")
parser.add_argument("--version", action="version", version=odatse.__version__)

mode_group = parser.add_mutually_exclusive_group()
mode_group.add_argument("--init", action="store_true", help="initial start (default)")
mode_group.add_argument("--resume", action="store_true", help="resume intterupted run")
mode_group.add_argument("--cont", action="store_true", help="continue from previous run")

parser.add_argument("--reset_rand", action="store_true", default=False, help="new random number series in resume or continue mode")

args = parser.parse_args()

file_name = args.inputfile
info = odatse.Info.from_file(file_name)

algname = info.algorithm["name"]
if algname == "mapper":
from .algorithm.mapper_mpi import Algorithm
elif algname == "minsearch":
from .algorithm.min_search import Algorithm
elif algname == "exchange":
from .algorithm.exchange import Algorithm
elif algname == "pamc":
from .algorithm.pamc import Algorithm
elif algname == "bayes":
from .algorithm.bayes import Algorithm
else:
print(f"ERROR: Unknown algorithm ({algname})")
sys.exit(1)
info, run_mode = odatse.initialize()
alg_module = odatse.algorithm.choose_algorithm(info.algorithm["name"])

solvername = info.solver["name"]
if solvername == "analytical":
Expand All @@ -65,20 +29,8 @@ def main():
print(f"ERROR: Unknown solver ({solvername})")
sys.exit(1)

if args.init is True:
run_mode = "initial"
elif args.resume is True:
run_mode = "resume"
if args.reset_rand is True:
run_mode = "resume-resetrand"
elif args.cont is True:
run_mode = "continue"
if args.reset_rand is True:
run_mode = "continue-resetrand"
else:
run_mode = "initial" # default

solver = Solver(info)
runner = odatse.Runner(solver, info)
alg = Algorithm(info, runner, run_mode=run_mode)
alg = alg_module.Algorithm(info, runner, run_mode=run_mode)

result = alg.main()
15 changes: 15 additions & 0 deletions src/odatse/algorithm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,18 @@
# along with this program. If not, see http://www.gnu.org/licenses/.

from ._algorithm import AlgorithmBase

def choose_algorithm(name):
alg_table = {
"mapper": "mapper_mpi",
}

try:
import importlib
alg_name = "odatse.algorithm.{}".format(alg_table.get(name, name))
alg_module = importlib.import_module(alg_name)
except ModuleNotFoundError as e:
print("ERROR: {}".format(e))
sys.exit(1)

return alg_module

0 comments on commit b17d7ce

Please sign in to comment.