Skip to content

nwb.py

imaging_session_to_nwb(session_key, include_raw_data=False, lab_key=None, project_key=None, protocol_key=None, nwbfile_kwargs=None)

Main function for converting calcium imaging data to NWB.

Parameters:

Name Type Description Default
session_key dict

key from Session table.

required
include_raw_data bool

Optional. Default False. Include the raw data from

False
lab_key dict

Optional key to add metadata from Element Lab.

None
project_key dict

Optional key to add metadata from Element Lab.

None
protocol_key dict

Optional key to add metadata from Element Lab.

None
nwbfile_kwargs dict

Optional. If Element Session is not used, this argument is required and must be a dictionary containing 'session_description' (str), 'identifier' (str), and 'session_start_time' (datetime), the required minimal data for instantiating an NWBFile object. If element-session is being used, this argument can optionally be used to overwrite NWBFile fields.

None

Returns: nwbfile (NWBFile): nwb file

Source code in element_calcium_imaging/export/nwb/nwb.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def imaging_session_to_nwb(
    session_key,
    include_raw_data=False,
    lab_key=None,
    project_key=None,
    protocol_key=None,
    nwbfile_kwargs=None,
):
    """Main function for converting calcium imaging data to NWB.

    Args:
        session_key (dict): key from Session table.
        include_raw_data (bool): Optional. Default False. Include the raw data from
        source. `ScanImage`, `Scanbox`, and `PrairieView` are supported.
        lab_key (dict): Optional key to add metadata from Element Lab.
        project_key (dict): Optional key to add metadata from Element Lab.
        protocol_key (dict): Optional key to add metadata from Element Lab.
        nwbfile_kwargs (dict): Optional. If Element Session is not used, this argument
            is required and must be a dictionary containing 'session_description' (str),
            'identifier' (str), and 'session_start_time' (datetime), the required
             minimal data for instantiating an NWBFile object. If element-session is
             being used, this argument can optionally be used to overwrite NWBFile
             fields.
    Returns:
        nwbfile (NWBFile): nwb file
    """

    session_to_nwb = getattr(imaging._linking_module, "session_to_nwb", False)

    if session_to_nwb:
        nwb_file = session_to_nwb(
            session_key,
            lab_key=lab_key,
            project_key=project_key,
            protocol_key=protocol_key,
            additional_nwbfile_kwargs=nwbfile_kwargs,
        )
    else:
        nwb_file = NWBFile(**nwbfile_kwargs)

    if include_raw_data:
        _create_raw_data_nwbfile(session_key, linked_nwb_file=nwb_file)
        if not nwb_file.imaging_planes:
            _add_scan_to_nwb(session_key, nwbfile=nwb_file)

    else:
        _add_scan_to_nwb(session_key, nwbfile=nwb_file)
    _add_image_series_to_nwb(
        session_key, imaging_plane=nwb_file.imaging_planes["ImagingPlane"]
    )
    _add_segmentation_data_to_nwb(
        session_key,
        nwbfile=nwb_file,
        imaging_plane=nwb_file.imaging_planes["ImagingPlane"],
    )

    return nwb_file

write_nwb(nwbfile, fname, check_read=True)

Export NWBFile

Parameters:

Name Type Description Default
nwbfile NWBFile

nwb file

required
fname str

Absolute path including *.nwb extension.

required
check_read bool

If True, PyNWB will try to read the produced NWB file and ensure that it can be read.

True
Source code in element_calcium_imaging/export/nwb/nwb.py
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
def write_nwb(nwbfile, fname, check_read=True):
    """Export NWBFile

    Args:
        nwbfile (NWBFile): nwb file
        fname (str): Absolute path including `*.nwb` extension.
        check_read (bool): If True, PyNWB will try to read the produced NWB file and
            ensure that it can be read.
    """
    with NWBHDF5IO(fname, "w") as io:
        io.write(nwbfile)

    if check_read:
        with NWBHDF5IO(fname, "r") as io:
            io.read()
    logger.info("File saved successfully")