Skip to content

Commit

Permalink
Updated chi_parse with chi_run commands
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Dec 20, 2023
1 parent 1a9be3f commit 8e60555
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 96 deletions.
103 changes: 50 additions & 53 deletions chi_pet/chi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import os
from pathlib import Path
# Main functions
from .chi_parser import chi_parser
from .chi_parse import chi_parse
from .chi_run import ChiRun
from .chi_node import ChiNode
from . import chi_lib as clib
# Analysis

Expand All @@ -34,28 +36,28 @@ def read_opts(self):
self.opts.states = list(yd.keys())

def run(self):
wd = self.opts.workdir # Shortcut for work directory
run_not_path = wd / "run.not"
if self.opts.launch != "NOLAUNCH":
# If no sim dirs are given find them all in simulations
if self.opts.launch == []:
self.opts.launch = clib.find_dirs(wd / "simulations")
# If no dirs were found return with warning
if self.opts.launch == []:
print(" No sim directories were found our given. ")
return
# Find run.not and delete
try:
run_not_path.unlink()
except OSError:
print("WARNING: 'run.not' was not found in workdir.",
" Might want to go searching for it.")
if self.opts.command == "launch":
# # If no sim dirs are given find them all in simulations
# if self.opts.launch == []:
# self.opts.launch = clib.find_dirs(wd / "simulations")
# # If no dirs were found return with warning
# if self.opts.launch == []:
# print(" No sim directories were found our given. ")
# return
# # Find run.not and delete
# try:
# run_not_path.unlink()
# except OSError:
# print("WARNING: 'run.not' was not found in workdir.",
# " Might want to go searching for it.")
# Create run.ing in workdir
# ChiLaunch(simdirs=self.opts.launch, opts=self.opts)
# running_path = wd / "run.ing"
# running_path.touch()
pass

elif self.opts.create:
elif self.opts.command == 'create':
# TODO NEXT implement this functionality
pass
# run_not_path.touch()
# # touch(os.path.join(wd, "run.not"))
Expand All @@ -65,49 +67,44 @@ def run(self):
# c = ChiCreate(self.opts, self.opts.workdir)
# c.Create(self.opts.create)

elif self.opts.shotgun:
pass
# c = ChiCreate(self.opts, self.opts.workdir)
# c.Create(self.opts.shotgun)
# elif self.opts.particleswarmcreate:
# pass
# # c = ChiParticleSwarm(self.opts, self.opts.workdir, 0)
# # c.Create(self.opts.particleswarmcreate)

elif self.opts.particleswarmcreate:
pass
# c = ChiParticleSwarm(self.opts, self.opts.workdir, 0)
# c.Create(self.opts.particleswarmcreate)
# elif self.opts.geneticalgorithmcreate:
# pass
# # c = ChiGeneticAlgorithm(self.opts, self.opts.workdir, 0)
# # c.Create(self.opts.geneticalgorithmcreate)

elif self.opts.geneticalgorithmcreate:
pass
# c = ChiGeneticAlgorithm(self.opts, self.opts.workdir, 0)
# c.Create(self.opts.geneticalgorithmcreate)

elif self.opts.run:
pass
# c = ChiRun(self.opts)
# c.Run(self.opts)

elif self.opts.prep:
pass
# leaf_lst = clib.find_leaf_dirs(self.opts.workdir)
# for leaf_dir in leaf_lst:
# if self.opts.args_file:
# shutil.copy(self.opts.args_file, leaf_dir)
# for s in self.opts.states:
# clib.touch(leaf_dir / f'sim.{s}')

elif self.opts.remove:
elif self.opts.command == 'run':
pass
# leaf_lst = clib.find_leaf_dirs(self.opts.workdir)
# for leaf_dir in leaf_lst:
# for fn in self.opts.remove:
# path = leaf_dir / fn
# if path.exists():
# path.unlink()
c = ChiRun(self.opts)
c.Run(self.opts)

# elif self.opts.prep:
# pass
# # leaf_lst = clib.find_leaf_dirs(self.opts.workdir)
# # for leaf_dir in leaf_lst:
# # if self.opts.args_file:
# # shutil.copy(self.opts.args_file, leaf_dir)
# # for s in self.opts.states:
# # clib.touch(leaf_dir / f'sim.{s}')

# elif self.opts.remove:
# pass
# # leaf_lst = clib.find_leaf_dirs(self.opts.workdir)
# # for leaf_dir in leaf_lst:
# # for fn in self.opts.remove:
# # path = leaf_dir / fn
# # if path.exists():
# # path.unlink()


def main():
"""Main function of chi_pet
"""
opts = chi_parser()
opts = chi_parse()
chi = Chi(opts)


Expand Down
39 changes: 16 additions & 23 deletions chi_pet/chi_parser.py → chi_pet/chi_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# TODO NEXT add parser test


def chi_parser():
def chi_parse():
parser = argparse.ArgumentParser(prog='chi_pet.py')

parser.add_argument('-n', type=int, default=10,
Expand All @@ -26,11 +26,11 @@ def chi_parser():
parser.add_argument('-s', '--states', nargs='+', type=str,
help='Name of all the states the simulation will run eg. start, build, analyze, etc.')

# PREP options
# TODO add prep functionality
parser.add_argument('-P', '--prep', action='store_true',
help='Prepares sims to run with either states specified with -s or all argument states in -a ARG_FILES')
help='Prepares simulationss to run with either states specified with -s or all argument states in --args_file.')

# REMOVE options
# TODO add remove functionality
parser.add_argument('-rm', '--remove', nargs='+', metavar='FILE', type=str,
help='Removes FILEs from seed directories.')

Expand All @@ -53,19 +53,15 @@ def chi_parser():
create_parser.add_argument('-ny', '--non_yaml', nargs='+', default=[], type=str,
help='Will add non-yaml files to seed directories when creating directory structure. (Used with create parser only)')

# LAUNCH options only
parser.add_argument('-L', '--launch', nargs='*', default='NOLAUNCH', type=str, metavar='DIRS',
help='Launches all the seed directories in DIRS list. If no list is\
given all sim directories in the "simulations" directory will be launched.')
run_parser = subparsers.add_parser(
'run', help='Run a simulation pipeline defined in args yaml file in a singular seed directory. Requires the --args_file option defined.')

# # CREATE options only
# parser.add_argument('-C', '--create', metavar='PARAM_FILE',
# nargs='+', type=str, default=[],
# help='Creates seed directories with simulation structure that can be launched with ChiLaunch.py.')
launch_parser = subparsers.add_parser(
'launch', help='Launch or create launching script to run simulations in seed directories.')

# parser.add_argument('-S', '--shotgun', metavar='PARAM_FILE',
# nargs='+', type=str,
# help='Creates seed directories with simulation structure that can be launched with ChiLaunch.py. PARAM_FILEs are copied into seed directories with ChiParams chosen according to the random distribution specified. Need -n to specify the number of random variants (default=10).')
# parser.add_argument('-L', '--launch', nargs='*', default='NOLAUNCH', type=str, metavar='DIRS',
# help='Launches all the seed directories in DIRS list. If no list is\
# given all sim directories in the "simulations" directory will be launched.')

# parser.add_argument('-PSC', '--particleswarmcreate', metavar='PARAM_FILE',
# nargs='+', type=str, default=[],
Expand All @@ -81,14 +77,11 @@ def chi_parser():

opts = parser.parse_args()

if opts.command == 'run' and opts.args_file is None:
parser.error("'run' requires the '--args_file' option.")

# If growing a chi tree, turn param file path strings into a pathlib list
create_args = [opts.create,
opts.shotgun,
opts.particleswarmcreate,
opts.geneticalgorithmcreate]
opts.param_file_paths = []

for plist in create_args:
opts.param_file_paths += plist
if opts.command == 'create':
opts.param_file_paths = [opts.files]

return opts
42 changes: 23 additions & 19 deletions chi_pet/chi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .chi_lib import load_yaml_in_order, dump_yaml_in_order
import argparse
from pathlib import Path
from .chi_parse import chi_parse

'''
Name: ChiMain.py
Expand All @@ -14,21 +15,6 @@
'''


def run_parse_args():
parser = argparse.ArgumentParser(prog='Chi.py')
parser.add_argument('-d', '--workdir', type=str, required=True,
help='Name of the working directory where simulation will be run')
parser.add_argument('-a', '--args_file', type=str,
help='Name file that holds the program argument list.')
# parser.add_argument('-p', '--program', type=str, required=True,
# help='Name of program that will be run')
parser.add_argument('-s', '--states', nargs='+', type=str, required=True,
help='Name of all the states the simulation will run eg. start, build, analyze, etc.')

opts = parser.parse_args()
return opts


def run_args(workdir, state, args):
action = state + '-ing'
action_file = Path('.' + action)
Expand All @@ -52,7 +38,7 @@ class ChiRun(object):
def __init__(self, opts):
self.opts = opts

def Run(self, opts):
def run(self, opts):
workdir_path = Path(opts.workdir)
args_file_path = workdir_path / opts.args_file
if not workdir_path.exists():
Expand All @@ -74,16 +60,34 @@ def Run(self, opts):
sim_action = Path('sim.{}'.format(k))

if k in opts.states:
if run_args(opts.workdir, k, args):
if self.run_args(opts.workdir, k, args):
(opts.workdir / '.error').touch()
elif sim_action.exists():
sim_action.unlink()

@classmethod
def run_args(workdir, state, args):
action = state + '-ing'
action_file = Path('.' + action)
print("Started {} sim using args {}".format(action, args))
sys.stdout.flush()
if workdir.exists():
os.chdir(workdir)
action_file.touch()
status = run(args)
action_file.unlink()
if status.returncode:
print(f"Run failed: State {state} did not succeed.")
return status.returncode
else:
print(
f"Run failed: could not find work directory {workdir} to do action {state}.")
return 1


if __name__ == '__main__':

# TODO NEXT add this as a subparser to chi_pet.py
opts = run_parse_args()
opts = chi_parse()

c = ChiRun(opts)
c.Run(opts)
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUN apt update && \
RUN git clone --recursive https://github.com/lamsoa729/chi_pet.git /root/chi_pet && \
pip install -e /root/chi_pet

# TODO NEXT add continuous integration
# TODO add continuous integration



0 comments on commit 8e60555

Please sign in to comment.