# Resultbag Use a {term}`Resultbag` to store solved results in a `.jcmrb` file and access the stored entries later. A {term}`Resultbag entry ` uses {term}`project keys` and an optional {term}`JCMT pattern`. The `suffix_pattern` value selects one stored version for the same {term}`project`. It selects {term}`project support files ` with that suffix. It does not select a different project start file. ```{admonition} Resultbag value :class: highlight Using a Resultbag requires little additional setup. It can prevent repeated solves when you reuse results. For example, an analysis script can reuse completed results instead of recomputing them. ``` ## Create A Resultbag Import {class}`Resultbag ` from `jcmsuite.results` and pass the storage path. If the path has no suffix, {class}`Resultbag ` appends `.jcmrb`. The parent directory must exist. ```python import jcmsuite resultbag = jcmsuite.results.Resultbag("results.jcmrb") ``` The Resultbag closes automatically at the end of the script. You can also use a context manager to close the Resultbag at the end of a block. ```python with jcmsuite.results.Resultbag("results.jcmrb") as resultbag: print(resultbag.keys) ``` ## Use A Resultbag With {func}`jcmsuite.solve ` Pass a {class}`Resultbag ` to {func}`jcmsuite.solve ` with the `resultbag=` keyword argument. {func}`jcmsuite.solve ` reuses a stored entry when the project, project keys, JCMT pattern, and source files match. Set `recompute=True` to ignore a matching entry. When no layout file or layout template is selected, an existing `grid.jcm` beside the project is included among those source files. A changed grid then requires explicit recomputation; when a layout is selected, grid-only changes do not invalidate the entry. ```python import jcmsuite resultbag = jcmsuite.results.Resultbag("results.jcmrb") result = jcmsuite.solve( "project.jcmp", keys={"radius": 0.5}, jcmt_pattern="mie", resultbag=resultbag ) ``` For {term}`batched project keys`, {func}`jcmsuite.solve ` stores and reuses one entry for each set of project keys. For an {term}`ordered project bundle`, the complete bundle is one resultbag entry for each set of project keys. A result from one project is different from a result from an ordered project bundle. See the [Result API](../reference/results.md) for the exact return types. Set `recompute=True` to run the solve again instead of reusing matching resultbag entries. ## Inspect Stored Entries {attr}`resultbag.keys ` returns the project keys stored in the Resultbag. You cannot change this collection. Each set of project keys contains named values. ```python project_keys = resultbag.keys for stored_project_keys in project_keys: print(stored_project_keys) ``` Iterate over {meth}`resultbag.items() ` to access each stored set of project keys and its raw dictionary. Use {meth}`get_results ` when you need the current result classes. ```python for stored_project_keys, result in resultbag.items(): print(stored_project_keys, result) ``` ## Filter Entries Call {meth}`resultbag.filter ` to create a read-only query view. The view stores filter conditions and applies them when you read keys, items, suffixes, or results. It does not copy Resultbag entries. A resultbag entry must match every filter condition. You can add conditions to an existing query with another {meth}`filter ` call. ```python from jcmsuite.results.resultbag import between selected = resultbag.filter(material="Si") selected = selected.filter(radius=between(0.35, 0.45)) ``` Plain values must match exactly. Lists and tuples also match as complete values, regardless of length. Use {func}`between(lower, upper) ` for inclusive bounds on values that can be ordered. The query supports the same read operations as the resultbag: ```python selected.keys selected.items() selected.suffixes selected.get_results(None) ``` ## Access Stored Results Use {meth}`get_results ` to load selected entries. The `jcmt_pattern` argument selects the entry without a suffix when it is `None`. Pass a string to select the stored version with that JCMT pattern. ```python all_default_results = resultbag.get_results(None) one_result = resultbag.get_results({"material": "Si", "radius": 0.5}) mie_results = resultbag.get_results(None, jcmt_pattern="mie") ``` The `keys` argument accepts these forms: - `None`: select all entries visible in the resultbag or query. - One set of project keys: select one exact set. - Batched project keys: select an explicit sequence of sets. The return type depends on the selected entries: - One entry for one project returns {class}`SolveResult `. - Multiple entries for one project return {class}`BatchSolveResult `. - One entry for an ordered project bundle returns {class}`BundledSolveResult `. - Multiple entries for ordered project bundles return {class}`BundledBatchSolveResult `. When you select multiple resultbag entries, {class}`Resultbag ` builds a new batch result. The entries can come from separate solves. They do not need to come from one original set of batched project keys. The new result assigns its own indices. These indices do not preserve the original batch indices. Select entries from one project, or select entries from ordered project bundles. Do not mix these two entry types. A mixed selection raises {class}`InvalidConfigurationError `. ## Discover Suffixes Use {attr}`suffixes ` to discover the JCMT patterns visible in a resultbag or resultbag query. The value `None` represents the default unsuffixed entry. ```python patterns = resultbag.suffixes # Example: (None, "mie", "curvilinear_mie") ``` The collection is deterministic and reflects only the current view. Resultbag entries contain versioned dictionaries made from parsed result data. A dictionary stores named values. {meth}`get_results ` returns that data as current solve results. Result data loaded from a Resultbag reports `resultbag` as its source and does not retain project or result-directory paths. ## Advanced Management {attr}`resultbag.management ` provides advanced methods for maintaining a Resultbag and connecting it to solve execution. Use the read operations on this page for normal result analysis. See [Resultbag Management](management.md) for these methods and their API reference. ## Close A Resultbag Calling {meth}`close() ` is optional because the Resultbag closes when the Python program ends. Use it when you need to close the Resultbag earlier. ```python resultbag.close() ``` See the [Resultbag Tutorials](../tutorials/resultbag/index.md) for a complete reuse and filtering workflow. See the [Resultbag API](../reference/resultbag.md) for generated signatures and method descriptions. ## Resultbag Pages ```{toctree} :maxdepth: 1 :titlesonly: management ```