|
6 | 6 |
|
7 | 7 | import datajoint as dj |
8 | 8 | import numpy as np |
| 9 | +from datajoint.user_tables import UserTable |
9 | 10 |
|
| 11 | +from spyglass.utils.dj_chains import PERIPHERAL_TABLES |
10 | 12 | from spyglass.utils.logging import logger |
11 | 13 | from spyglass.utils.nwb_helper_fn import get_nwb_file |
12 | 14 |
|
@@ -110,6 +112,26 @@ def dj_replace(original_table, new_values, key_column, replace_column): |
110 | 112 | return original_table |
111 | 113 |
|
112 | 114 |
|
| 115 | +def get_fetching_table_from_stack(stack): |
| 116 | + """Get all classes from a stack of tables.""" |
| 117 | + classes = set() |
| 118 | + for frame_info in stack: |
| 119 | + locals_dict = frame_info.frame.f_locals |
| 120 | + for obj in locals_dict.values(): |
| 121 | + if not isinstance(obj, UserTable): |
| 122 | + continue # skip non-tables |
| 123 | + if (name := obj.full_table_name) in PERIPHERAL_TABLES: |
| 124 | + continue # skip common_nwbfile tables |
| 125 | + classes.add(name) |
| 126 | + if len(classes) > 1: |
| 127 | + logger.warn( |
| 128 | + f"Multiple classes found in stack: {classes}. " |
| 129 | + "Please submit a bug report with the snippet used." |
| 130 | + ) |
| 131 | + classes = None # predict only one but not sure, so return None |
| 132 | + return next(iter(classes)) if classes else None |
| 133 | + |
| 134 | + |
113 | 135 | def get_nwb_table(query_expression, tbl, attr_name, *attrs, **kwargs): |
114 | 136 | """Get the NWB file name and path from the given DataJoint query. |
115 | 137 |
|
@@ -150,6 +172,11 @@ def get_nwb_table(query_expression, tbl, attr_name, *attrs, **kwargs): |
150 | 172 | query_expression * tbl.proj(nwb2load_filepath=attr_name) |
151 | 173 | ).fetch(file_name_str) |
152 | 174 |
|
| 175 | + if which == "analysis": # log access of analysis files to log table |
| 176 | + AnalysisNwbfile().increment_access( |
| 177 | + nwb_files, table=get_fetching_table_from_stack(inspect.stack()) |
| 178 | + ) |
| 179 | + |
153 | 180 | return nwb_files, file_path_fn |
154 | 181 |
|
155 | 182 |
|
@@ -185,6 +212,7 @@ def fetch_nwb(query_expression, nwb_master, *attrs, **kwargs): |
185 | 212 | nwb_files, file_path_fn = get_nwb_table( |
186 | 213 | query_expression, tbl, attr_name, *attrs, **kwargs |
187 | 214 | ) |
| 215 | + |
188 | 216 | for file_name in nwb_files: |
189 | 217 | file_path = file_path_fn(file_name) |
190 | 218 | if not os.path.exists(file_path): # retrieve the file from kachery. |
|
0 commit comments