Manage Resultbag Entries

This tutorial uses dictionaries to manage Resultbag entries. A dictionary stores named values. The tutorial does not run a project.

Tutorial Operations

The tutorial creates its Resultbag and source file in a temporary directory. The directory is removed when the tutorial ends.

It demonstrates these operations:

  • Add a result and log for exact project keys and a JCMT pattern.

  • Inspect stored results and logs.

  • Replace a result by adding the same project keys and JCMT pattern again.

  • Store a second suffix variant for the same project keys.

  • Add, inspect, and clear ordered attempt logs.

  • Remove one suffix variant, remove matching project keys, and reset the resultbag.

Use the normal Resultbag read and query operations for analysis workflows. Use resultbag.management when you must maintain stored entries or connect them to solve execution. The management namespace is provided by ResultbagManagement.

Run the tutorial from the tutorial root:

python resultbag/management/run.py

Python File

from jcmsuite.results import Resultbag

KEYS = {"material": "Si", "radius": 0.5}
RESULT = {"status": "complete", "flux": 1.23}
LOG = {"message": "stored without a solve"}


resultbag = Resultbag("results.jcmrb")

management = resultbag.management

# Add one exact key and suffix variant, then inspect its payloads.
management.add(
    keys=KEYS,
    result=RESULT,
    log=LOG,
    jcmt_pattern="baseline",
)
print(management.check_result(KEYS, jcmt_pattern="baseline"))
print(management.get_result(KEYS, jcmt_pattern="baseline"))
print(management.get_log(KEYS, jcmt_pattern="baseline"))

# Adding the same key and suffix replaces the stored result.
management.add(
    keys=KEYS,
    result={"status": "complete", "flux": 1.24},
    log=LOG,
    jcmt_pattern="baseline",
)
print(management.get_result(KEYS, jcmt_pattern="baseline"))

# JCMT patterns keep variants for the same Project Keys separate.
management.add(keys=KEYS, result=RESULT, jcmt_pattern="refined")
print(resultbag.suffixes)

# Attempt logs are returned in ascending attempt-index order.
management.add_attempt_log(
    keys=KEYS,
    attempt_index=0,
    payload={"message": "first attempt failed"},
    jcmt_pattern="baseline",
)
management.add_attempt_log(
    keys=KEYS,
    attempt_index=1,
    payload={"message": "second attempt failed"},
    jcmt_pattern="baseline",
)
print(management.get_attempt_logs(KEYS, jcmt_pattern="baseline"))
management.clear_attempt_logs(KEYS, jcmt_pattern="baseline")
print(management.get_attempt_logs(KEYS, jcmt_pattern="baseline"))


management.remove_result(KEYS, jcmt_pattern="baseline")
management.remove(filter_function=lambda project_keys: project_keys["material"] == "Si")
print(resultbag.has_results())

management.add(keys=KEYS, result=RESULT)
management.reset()
print(resultbag.has_results())