Misc Examples

First we need to create our NWB object, we’ll use this generic one

 1from simply_nwb import SimpleNWB
 2import pendulum
 3from pynwb.file import Subject
 4
 5
 6def nwb_gen():
 7    return SimpleNWB.create_nwb(
 8        # Required
 9        session_description="Mouse cookie eating session",
10        # Subtract 1 year so we don't run into the 'NWB start time is at a greater date than current' issue
11        session_start_time=pendulum.now().subtract(years=1),
12        experimenter=["Schmoe, Joe"],
13        lab="Felsen Lab",
14        experiment_description="Gave a mouse a cookie",
15
16        # Optional
17        identifier="cookie_0",
18        subject=Subject(**{
19            "subject_id": "1",
20            "age": "P90D",  # ISO-8601 for 90 days duration
21            "strain": "TypeOfMouseGoesHere",  # If no specific used, 'Wild Strain'
22            "description": "Mouse#2 idk",
23            "sex": "M",  # M - Male, F - Female, U - unknown, O - other
24            # NCBI Taxonomy link or Latin Binomial (e.g.'Rattus norvegicus')
25            "species": "http://purl.obolibrary.org/obo/NCBITaxon_10116",
26        }),
27        session_id="session0",
28        institution="CU Anschutz",
29        keywords=["mouse"],
30
31        # related_publications="DOI::LINK GOES HERE FOR RELATED PUBLICATIONS"
32    )

Here are some of the many misc functions and how to use them

  1import numpy as np
  2from dict_plus.utils.simpleflatten import SimpleFlattener
  3
  4from simply_nwb import SimpleNWB
  5from simply_nwb.transforms import plaintext_metadata_read, csv_load_dataframe, yaml_read_file
  6from simply_nwb.util import panda_df_to_dyn_table
  7import pandas as pd
  8from gen_nwb import nwb_gen
  9
 10
 11def pkl_test():
 12    import pickle
 13    fn = "../data/output.pkl"
 14    fp = open(fn, "rb")
 15    data = pickle.load(fp)
 16    nwb = nwb_gen()
 17    SimpleNWB.processing_add_dict(
 18            nwb,
 19            processed_name="asdf",
 20            processed_description="asdf",
 21            data_dict=SimpleFlattener(simple_types=[np.ndarray, type(None)]).flatten(data),
 22            uneven_columns=True)
 23    # nwb.processing["misc"]["asdf_eyePositionUncorrected"]["eyePositionUncorrected"].data[:]
 24    tw = 2
 25
 26
 27def util_test():
 28    # Note: This CSV isn't formatted correctly, so it will look weird when loaded
 29    r = panda_df_to_dyn_table(pd_df=csv_load_dataframe("../data/20230414_unitR2_session002_leftCam-0000DLC_resnet50_licksNov3shuffle1_1030000.csv"), table_name="test_table",
 30                              description="test description")
 31    tw = 2
 32
 33
 34def csv_test():
 35    # Loads in well, but file isn't exactly in CSV format, still a test
 36    r = csv_load_dataframe("../data/20230414_unitR2_session002_leftCam-0000DLC_resnet50_licksNov3shuffle1_1030000.csv")
 37    tw = 2
 38
 39
 40def yaml_test():
 41    r = yaml_read_file("../data/20230414_unitR2_session002_metadata.yaml")
 42    tw = 2
 43
 44
 45def plaintext_metadata_test():
 46    r = plaintext_metadata_read("../data/metadata.txt")
 47    tw = 2
 48
 49
 50def nwb_processing_module_df():
 51    nwb = nwb_gen()
 52
 53    d = pd.DataFrame.from_dict({"data1": [1, 2, 3, 4, 5], "data2": ["a", "b", "c", "d", "e"]})
 54
 55    SimpleNWB.processing_add_dataframe(
 56        nwb,
 57        processed_name="ProcessedData",
 58        processed_description="Test processing",
 59        data=d
 60    )
 61
 62    # Extra to test multiple adds
 63    SimpleNWB.processing_add_dataframe(
 64        nwb,
 65        processed_name="ProcessedData2",
 66        processed_description="Test processing",
 67        data=d
 68    )
 69
 70    t = nwb.processing["misc"]["ProcessedData"]["data1"]
 71    return nwb, []
 72
 73
 74def nwb_processing_module_dict():
 75    nwb = nwb_gen()
 76
 77    SimpleNWB.processing_add_dict(
 78        nwb,
 79        processed_name="ProcessedDictData1",
 80        processed_description="Test processing",
 81        data_dict={"data1": [1, 2, 3, 4, 5], "data2": ["a", "b", "c", "d", "e"]},
 82        uneven_columns=False
 83    )
 84    t = nwb.processing["misc"]["ProcessedDictData1"]["data1"][:]
 85
 86    SimpleNWB.processing_add_dict(
 87        nwb,
 88        processed_name="ProcessedDictData2",
 89        processed_description="Test processing",
 90        data_dict={"data1": [1, 2, 3], "data2": ["a", "b", "c", "d", "e"], "aa": 5},
 91        uneven_columns=True
 92    )
 93    # Because of uneven columns, each key is separate
 94    t = nwb.processing["misc"]["ProcessedDictData2_data1"]["data1"][:]
 95    t = nwb.processing["misc"]["ProcessedDictData2_data2"]["data2"][:]
 96    t = nwb.processing["misc"]["ProcessedDictData2_aa"]["aa"][:]
 97
 98    # Add another dict
 99    SimpleNWB.processing_add_dict(
100        nwb,
101        processed_name="ProcessedDictData2",
102        processed_description="Test processing",
103        data_dict={"data1": [1, 2, 3, 4, 5], "data2": ["a", "b", "c", "d", "e"]},
104        uneven_columns=False
105    )
106    t = nwb.processing["misc"]["ProcessedDictData2"]["data2"][:]
107    contents = nwb.processing["misc"].containers
108
109    # Ignore the check_single_row test since could be an edgecase
110    return nwb, ["check_single_row"]