Skip to content

Commit

Permalink
Merge pull request #127 from efabless/queue-skip-params
Browse files Browse the repository at this point in the history
Queue and skip parameters via pattern matching
  • Loading branch information
mole99 authored Oct 10, 2024
2 parents 9f31695 + 5c19129 commit a932cc2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 37 deletions.
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# 2.5.1

## CLI

- Allow parameters to be specified via `--parameter` as a pattern
- Add `--skip-parameter` to skip certain parameters
- Applied after parameters have been queued via `--parameter`
- Also allows the use of patterns

# 2.5.0

## Common
Expand Down
2 changes: 1 addition & 1 deletion cace/__version__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
__version__ = '2.5.0'
__version__ = '2.5.1'

if __name__ == '__main__':
print(__version__, end='')
121 changes: 85 additions & 36 deletions cace/cace_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import signal
import logging
import argparse
from fnmatch import fnmatch
from datetime import timedelta

from rich.markdown import Markdown
from rich.progress import (
Progress,
Expand Down Expand Up @@ -139,7 +139,14 @@ def cli():
'--parameter',
nargs='+',
default=None,
help='run simulations on only the named parameters, by default run all parameters',
help='run simulations on only the named parameters, by default run all parameters. Has support for wildcards (*) to match parts of parameters.',
)
parser.add_argument(
'-sp',
'--skip-parameter',
nargs='+',
default=None,
help='list parameters that should be skipped. Has support for wildcards (*) to match parts of parameters.',
)
parser.add_argument(
'--parallel-parameters',
Expand Down Expand Up @@ -262,45 +269,87 @@ def cli():
# Get the start timestamp
timestamp_start = time.time()

# Get all available parameters
pnames = parameter_manager.get_all_pnames()

# Queued parameter names
queued_pnames = []

# Queue specified parameters
if args.parameter:
dbg(f'Running parameters: {args.parameter}')
dbg(f'Queuing parameters: {args.parameter}')

for pname in args.parameter:
parameter_manager.queue_parameter(
pname,
start_cb=lambda param, steps: start_parameter(
param, progress, task_ids, steps
),
step_cb=lambda param: step_parameter(
param, progress, task_ids
),
cancel_cb=lambda param: end_parameter(
param, progress, task_ids, task_id
),
end_cb=lambda param: end_parameter(
param, progress, task_ids, task_id
),
)
# Directly queue a parameter name
if pname in pnames:
queued_pnames.append(pname)
# Try to match pattern
else:
match_pnames = [
_pname for _pname in pnames if fnmatch(_pname, pname)
]

if not match_pnames:
err(f'{pname} does not match any parameters.')
err(f'Known parameters are: {", ".join(pnames)}')
sys.exit(1)

for match_pname in match_pnames:
queued_pnames.append(match_pname)
# Queue all parameters
else:
pnames = parameter_manager.get_all_pnames()
dbg(f'Running parameters: {pnames}')
for pname in pnames:
parameter_manager.queue_parameter(
pname,
start_cb=lambda param, steps: start_parameter(
param, progress, task_ids, steps
),
step_cb=lambda param: step_parameter(
param, progress, task_ids
),
cancel_cb=lambda param: end_parameter(
param, progress, task_ids, task_id
),
end_cb=lambda param: end_parameter(
param, progress, task_ids, task_id
),
)
queued_pnames = pnames

# Skip specified parameters
if args.skip_parameter:
dbg(f'Skipping parameters: {args.skip_parameter}')

for pname in args.skip_parameter:
# Directly remove a parameter name
if pname in queued_pnames:
queued_pnames.remove(pname)
# Try to match pattern
else:
match_pnames = [
_pname
for _pname in queued_pnames
if fnmatch(_pname, pname)
]

if not match_pnames:
err(f'{pname} does not match any queued parameters.')
err(f'Queued parameters are: {", ".join(queued_pnames)}')
sys.exit(1)

for match_pname in match_pnames:
queued_pnames.remove(match_pname)

if not queued_pnames:
err('No parameters specified to run.')
sys.exit(1)

info(f'Running parameters: {", ".join(queued_pnames)}')

for queued_pname in queued_pnames:
if not queued_pname in pnames:
err(f'Unknown parameter {queued_pname}.')
err(f'Known parameters are: {", ".join(pnames)}')
sys.exit(1)

for pname in queued_pnames:
parameter_manager.queue_parameter(
pname,
start_cb=lambda param, steps: start_parameter(
param, progress, task_ids, steps
),
step_cb=lambda param: step_parameter(param, progress, task_ids),
cancel_cb=lambda param: end_parameter(
param, progress, task_ids, task_id
),
end_cb=lambda param: end_parameter(
param, progress, task_ids, task_id
),
)

# Set the total number of parameters in the progress bar
progress.update(task_id, total=parameter_manager.num_queued_parameters())
Expand Down

0 comments on commit a932cc2

Please sign in to comment.