Using the Library: Advanced

This is a collection of tutorials illustrating some of the more advanced uses of the NWBInspector

Yielding and Iterating

Both the inspect_all and inspect_nwbfile functions return generators. That is, they do not actually run any checks on any NWBFile until the user performs an iteration command on them. The simplest way of doing this is to cast the generator as a list, i.e., list(inspect_nwbfile(...)) which will automatically complete all checks.

However, if a user chooses, they can harness these generators in more sophisticated ways. If you want to stop the checks early, the following will run the inspectors until the first InspectorMessage is returned:

results_generator = inspect_nwbfile(nwbfile_path="path_to_single_nwbfile")

first_message = next(results_generator)

This will return either the first InspectorMessage, or it will raise a StopIteration error. This error can be caught in the following manner

results_generator = inspect_nwbfile(nwbfile_path="path_to_single_nwbfile")

try:
    first_message = next(results_generator)
except StopIteration:
    print("There are no messages!")

Of course, the generator can be treated like any other iterable as well, such as with for loops

results_generator = inspect_nwbfile(nwbfile_path="path_to_single_nwbfile")

for message in results_generator:
    print(message)

Fetching and inspecting individual DANDI assets

While the section explaining basic steaming of a dandiset covered the simplest and most convenient usage of the streaming feature applied to an entire Dandiset, you might want to inspect only a single file.

In this case, we will use the nwbinspector.inspect_dandi_file_path() function.

from nwbinspector import inspect_dandi_file_path

dandiset_id = "000004"
dandi_file_path = "sub-P17HMH/sub-P17HMH_ses-20080501_ecephys+image.nwb"

messages = list(inspect_dandi_file_path(dandi_file_path=dandi_file_path, dandiset_id=dandiset_id))

Just like nwbinspector.inspect_dandiset(), this function accepts a dandiset_version.

In case your NWB file is accessible via some other cloud URL, you can also use the nwbinspector.inspect_url() function.

from nwbinspector import inspect_url

url = "https://dandiarchive.s3.amazonaws.com/blobs/3d7/39a/3d739ac0-10fb-41ef-80be-f1479cec44c0"

messages = list(inspect_url(url=url))

Format Reports

Reports aggregate messages into a readable form.

from nwbinspector.inspector_tools import format_messages

print("\n".join(format_messages(messages, levels=["importance", "file_path"])))

The levels argument can be altered to change the nesting structure of the report. Any combination and order of InspectorMessage attributes can be utilized to produce a more easily readable structure.