Skip to content

Commit

Permalink
Added argument parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lamsoa729 committed Dec 21, 2023
1 parent 8e60555 commit ab9b782
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 47 deletions.
8 changes: 4 additions & 4 deletions chi_pet/chi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import os
from pathlib import Path
# Main functions
from .chi_parse import chi_parse
from .chi_parse import parse_chi_options
from .chi_run import ChiRun
from .chi_node import ChiNode
from . import chi_lib as clib
Expand All @@ -25,7 +25,7 @@ def __init__(self, opts):

self.opts = opts
self.read_opts()
self.run()
self.execute()

def read_opts(self):
if not self.opts.workdir:
Expand All @@ -35,7 +35,7 @@ def read_opts(self):
yd = clib.create_dict_from_yaml_file(self.opts.args_file)
self.opts.states = list(yd.keys())

def run(self):
def execute(self):
if self.opts.command == "launch":
# # If no sim dirs are given find them all in simulations
# if self.opts.launch == []:
Expand Down Expand Up @@ -104,7 +104,7 @@ def run(self):
def main():
"""Main function of chi_pet
"""
opts = chi_parse()
opts = parse_chi_options()
chi = Chi(opts)


Expand Down
44 changes: 18 additions & 26 deletions chi_pet/chi_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@

from pathlib import Path

# TODO NEXT add parser test
# TODO NTEST add parser test


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

parser.add_argument('-n', type=int, default=10,
help='Number of different parameter sets used.')
parser.add_argument('-a', '--args_file', type=str,
help='Name file that holds the program argument list. (Used with --create option and --launch option)')
parser.add_argument('-d', '--workdir', type=str,
help='Name of the working directory where simulation will be run. Used with --run option only)')
parser.add_argument('-s', '--states', nargs='+', type=str,
help='Name of all the states the simulation will run eg. start, build, analyze, etc.')
parent_parser = argparse.ArgumentParser(add_help=False)

parent_parser.add_argument('-a', '--args_file', type=Path,
help='Name file that holds the program argument list. (Used with --create option and --launch option)')
parent_parser.add_argument('-d', '--workdir', type=Path,
help='Name of the working directory where simulation will be run. Used with --run option only)')
parent_parser.add_argument('-s', '--states', nargs='+', type=str,
help='Name of all the states the simulation will run eg. start, build, analyze, etc.')

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

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

subparsers = parser.add_subparsers(dest='command')

# CREATE options
create_parser = subparsers.add_parser(
'create', help='Create a simulation directory structure using various strategies.')
'create', parents=[parent_parser], help='Create a simulation directory structure using various strategies.')

create_parser.add_argument('files',
metavar='PARAM_FILE(S)', nargs='+', type=str, default=[],
create_parser.add_argument('param_files',
metavar='PARAM_FILE(S)', nargs='+', type=Path, default=[],
help='List of yaml files to be combined into a single yaml file and varied using Chi-Pet.')

# create_parser.add_argument('-A', '--algorithm', type=str, default='scan',
Expand All @@ -50,14 +50,14 @@ def chi_parse():
create_parser.add_argument('-r', '--replace', default=False, action='store_true',
help='Replace simulation file instead of throwing and error if file already exists.(Used with create parser only)')

create_parser.add_argument('-ny', '--non_yaml', nargs='+', default=[], type=str,
create_parser.add_argument('-ny', '--non_yaml', nargs='+', default=[], type=Path,
help='Will add non-yaml files to seed directories when creating directory structure. (Used with create parser only)')

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.')
'run', parents=[parent_parser], help='Run a simulation pipeline defined in args yaml file in a singular seed directory. Requires the --args_file option defined.')

launch_parser = subparsers.add_parser(
'launch', help='Launch or create launching script to run simulations in seed directories.')
'launch', parents=[parent_parser], help='Launch or create launching script to run simulations in seed directories.')

# 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\
Expand All @@ -71,17 +71,9 @@ def chi_parse():
# nargs='+', type=str, default=[],
# help='Genetic Algorithm Optimization Creation. 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 population members (default=10).')

# RUN options only
parser.add_argument('-R', '--run', action="store_true",
help='Runs a singular seed directory. Need --args_file.')

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
if opts.command == 'create':
opts.param_file_paths = [opts.files]

return opts
35 changes: 35 additions & 0 deletions tests/test_chi_parse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
"""Tests for `chi_pet` package."""

import argparse
from pathlib import Path
import pytest
import sys

from unittest import mock
from chi_pet.chi_parse import parse_chi_options

# @pytest.mark.parametrize("command,arg_dict", [
# ('create', 'value1'),
# ('run', 'value2'),
# # Add more tuples for more test cases
# ])


def test_chi_create_argument_parsing():
# Setup
sys.argv = ['chi', 'create', 'param1.yaml',
'param2.yaml', '-a', 'args.yaml']
# Test
opts = parse_chi_options()
# Assert
assert opts.command == 'create'
for pf in sys.argv[2:4]:
assert Path(pf) in opts.param_files


def test_chi_run_argument_parsing():
# Setup
sys.argv = ['chi', 'run', '-a', 'args.yaml']
opts = parse_chi_options()
assert opts.args_file == Path('args.yaml')
17 changes: 0 additions & 17 deletions tests/test_chi_pet.py

This file was deleted.

0 comments on commit ab9b782

Please sign in to comment.