<jupyter_start><jupyter_text>Case ReaderThe `CaseReader` object is provided to read case recordings, regardless of which case recorder was used. Currently, OpenMDAO only implements `SqliteCaseRecorder`, therefore all the examples will make use of this recorder. Other types of case recorders are expected to be supported in the future.  CaseReader ConstructorThe call signature for the `CaseReader` constructor is:```{eval-rst}    .. automethod:: openmdao.recorders.sqlite_reader.SqliteCaseReader.__init__        :noindex:```  Determining What Sources and Variables Were RecordedThe `CaseReader` object provides methods to determine which objects in the original problem were sourcesfor for the recorded cases and what variables they recorded. Sources can include the problem, driver, components and solvers.The `list_sources` method provides a list of the names of objects that are the sources of recorded datain the file.```{eval-rst}.. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.list_sources    :noindex:```The complementary `list_source_vars` method will provide a list of the input and output variables recordedfor a given source.```{eval-rst}.. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.list_source_vars    :noindex:```Here is an example of their usage:<jupyter_code>import openmdao.api as om
from openmdao.test_suite.components.sellar_feature import SellarMDA

import numpy as np

# define Sellar MDA problem
prob = om.Problem(model=SellarMDA())

model = prob.model
model.add_design_var('z', lower=np.array([-10.0, 0.0]),
                          upper=np.array([10.0, 10.0]))
model.add_design_var('x', lower=0.0, upper=10.0)
model.add_objective('obj')
model.add_constraint('con1', upper=0.0)
model.add_constraint('con2', upper=0.0)

prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-9, disp=False)

# add recorder to the driver, model and solver
recorder = om.SqliteRecorder('cases.sql')

prob.driver.add_recorder(recorder)
model.add_recorder(recorder)
model.nonlinear_solver.add_recorder(recorder)

# run the problem
prob.setup()
prob.set_solver_print(0)
prob.run_driver()
prob.cleanup()

cr = om.CaseReader('cases.sql')
sources = cr.list_sources()
assert sorted(sources) == ['driver', 'root', 'root.nonlinear_solver']
driver_vars = cr.list_source_vars('driver')
assert driver_vars['inputs'] == []
assert set(driver_vars['outputs']) == {'con1', 'con2', 'obj', 'x', 'z'}
assert driver_vars['residuals'] == []
model_vars = cr.list_source_vars('root')
assert set(model_vars['inputs']) == {'x', 'y1', 'y2', 'z'}
assert set(model_vars['outputs']) == {'con1', 'con2', 'obj', 'x', 'y1', 'y2', 'z'}
assert set(model_vars['residuals']) == {'con1', 'con2', 'obj', 'x', 'y1', 'y2', 'z'}
solver_vars = cr.list_source_vars('root.nonlinear_solver')
assert set(solver_vars['inputs']) == {'x', 'y1', 'y2', 'z'}
assert set(solver_vars['outputs']) == {'con1', 'con2', 'obj', 'x', 'y1', 'y2', 'z'}
assert solver_vars['residuals'] == []<jupyter_output><empty_output><jupyter_text>Case NamesThe `CaseReader` provides access to `Case` objects, each of which encapsulates a data point recorded byone of the sources.`Case` objects are uniquely identified in a case recorder file by their case names. A case name is a string.As an example, here is a case name:    'rank0:ScipyOptimize_SLSQP|1|root._solve_nonlinear|1'The first part of the case name indicates which rank or process that the case was recorded from. The remainder of the case name shows the hierarchical path to the object that was recorded along with the iteration counts for each object along the path. It follows a pattern of repeated pairs of    - object name ( problem, driver, system, or solver )    - iteration countThese are separated by the `|` character.So in the given example, the case is:    - from rank 0    - the first iteration of the driver, `ScipyOptimize_SLSQP`    - the first execution of the `root` system which is the top-level model  Getting Names of the CasesThe `list_cases` method returns the names of the cases in the order in whichthe cases were executed. You can optionally request cases only from a specific `source`.```{eval-rst}.. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.list_cases    :noindex:```There are two optional arguments to the `list_cases` method that affect what is returned.    - recurse: causes the returned value to include child cases.    - flat: works in conjunction with the `recurse` argument to determine if the returned      results are in the form of a list or nested dict. If recurse=True, flat=False, and there      are child cases, then the returned value is a nested ordered dict. Otherwise, it is a list.  Accessing CasesGetting information from the cases is a two-step process. First, you need to get access to the Case object and then you can call a variety of methods on the Case object to get values from it. The second step is described on the [Getting Data from a Case](case_reader_data.ipynb) page.There are two methods used to get a specific `Case`:    - get_cases    - get_case  Accessing Cases Using get_cases MethodThe `get_cases` method provides a quick and easy way to iterate over all the cases.```{eval-rst}.. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.get_cases    :noindex:```This method is similar to the `list_cases` method in that it has the two optional arguments`recurse` and `flat` to control what is returned as described above.Here is an example of its usage:<jupyter_code>import openmdao.api as om
from openmdao.test_suite.components.sellar_feature import SellarMDA

import numpy as np

prob = om.Problem(model=SellarMDA())

model = prob.model
model.add_design_var('z', lower=np.array([-10.0, 0.0]),
                          upper=np.array([10.0, 10.0]))
model.add_design_var('x', lower=0.0, upper=10.0)
model.add_objective('obj')
model.add_constraint('con1', upper=0.0)
model.add_constraint('con2', upper=0.0)

driver = prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-5)
driver.add_recorder(om.SqliteRecorder('cases.sql'))

prob.setup()
prob.set_solver_print(0)
prob.run_driver()
prob.cleanup()
cr = om.CaseReader('cases.sql')
cases = cr.get_cases()
for case in cases:
    print(case.name, sorted(case.outputs))
assert len(cases) == driver.iter_count
for i, case in enumerate(cases):
    assert case.name == f"rank0:ScipyOptimize_SLSQP|{i}", f"unexpected case name: {case.name}"
    assert sorted(case.outputs) == ['con1', 'con2', 'obj', 'x', 'z']<jupyter_output><empty_output><jupyter_text>Accessing Cases Using get_case MethodThe `get_case` method returns a `Case` object given a case name.```.. automethod:: openmdao.recorders.base_case_reader.BaseCaseReader.get_case    :noindex:```You can use the `get_case` method to get a specific case from the list of case namesreturned by `list_cases` as shown here:<jupyter_code>case_names = cr.list_cases()
assert len(case_names) == driver.iter_count
for i, case_name in enumerate(case_names):
    assert case_name == f"rank0:ScipyOptimize_SLSQP|{i}", f"unexpected case name: {case_name}"
# access a Case by name (e.g. first case)
case = cr.get_case("rank0:ScipyOptimize_SLSQP|0")
print(case.name, sorted(case.outputs))
assert case.name == "rank0:ScipyOptimize_SLSQP|0", f"unexpected case name: {case.name}"
# access a Case by index (e.g. first case)
case = cr.get_case(0)
print(case.name, sorted(case.outputs))
assert case.name == "rank0:ScipyOptimize_SLSQP|0", f"unexpected case name: {case.name}"
# access a Case by index (e.g. last case)
case = cr.get_case(-1)
print(case.name, sorted(case.outputs))
assert case.name == f"rank0:ScipyOptimize_SLSQP|{driver.iter_count-1}", f"unexpected case name: {case.name}"
# get each case by looping over case names
for name in case_names:
    case = cr.get_case(name)
    print(case.name, sorted(case.outputs))<jupyter_output><empty_output><jupyter_text>Processing a Nested Dictionary of Its Child CasesThe following example demonstrates selecting a case from a case list and processing a nesteddictionary of its child cases.<jupyter_code>import openmdao.api as om
from openmdao.test_suite.components.sellar_feature import SellarMDA

import numpy as np

# define Sellar MDA problem
prob = om.Problem(model=SellarMDA())

model = prob.model
model.add_design_var('z', lower=np.array([-10.0, 0.0]),
                          upper=np.array([10.0, 10.0]))
model.add_design_var('x', lower=0.0, upper=10.0)
model.add_objective('obj')
model.add_constraint('con1', upper=0.0)
model.add_constraint('con2', upper=0.0)

prob.driver = om.ScipyOptimizeDriver(optimizer='SLSQP', tol=1e-5)

# add recorder to the driver, model and solver
recorder = om.SqliteRecorder('cases.sql')

prob.driver.add_recorder(recorder)
model.add_recorder(recorder)
model.nonlinear_solver.add_recorder(recorder)

# run the problem
prob.setup()
prob.set_solver_print(0)
prob.run_driver()
prob.cleanup()
cr = om.CaseReader('cases.sql')

# get the last driver case
driver_cases = cr.list_cases('driver')
# get a recursive dict of child cases of the last driver case
last_driver_case = driver_cases[-1]
cases = cr.get_cases(last_driver_case, recurse=True, flat=False)

# display selected information from nested dict of cases
def print_cases(cases, indent=0):
    for case, children in cases.items():
        print(indent*' ', case.source, '-', case.name.split('.')[-1], sorted(case.outputs))
        if children:
            print_cases(children, indent+2)
            
print_cases(cases)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11707829742511823, 'fraction_numerical': 0.01208617971623752, 'mean_word_length': 5.289491077329808, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '186446', 'license': 'Apache-2.0', 'n_tokens_mistral': 3102, 'n_tokens_neox': 3003, 'n_words': 954, 'path': 'openmdao/docs/openmdao_book/features/recording/case_reader.ipynb', 'repo_name': 'sebasgo/OpenMDAO', 'size': '9515'}