|
| 1 | +import datajoint as dj |
| 2 | + |
1 | 3 | from spyglass.common.common_behav import ( |
2 | 4 | PositionSource, |
3 | 5 | RawPosition, |
|
14 | 16 | from spyglass.common.common_nwbfile import Nwbfile |
15 | 17 | from spyglass.common.common_session import Session |
16 | 18 | from spyglass.common.common_task import TaskEpoch |
| 19 | +from spyglass.common.common_usage import InsertError |
17 | 20 | from spyglass.utils import logger |
18 | 21 |
|
19 | 22 |
|
20 | 23 | def populate_all_common(nwb_file_name): |
21 | | - # Insert session one by one |
22 | | - fp = [(Nwbfile & {"nwb_file_name": nwb_file_name}).proj()] |
23 | | - logger.info("Populate Session...") |
24 | | - Session.populate(fp) |
25 | | - |
26 | | - # If we use Kachery for data sharing we can uncomment the following two lines. TBD |
27 | | - # logger.info('Populate NwbfileKachery...') |
28 | | - # NwbfileKachery.populate() |
29 | | - |
30 | | - logger.info("Populate ElectrodeGroup...") |
31 | | - ElectrodeGroup.populate(fp) |
32 | | - |
33 | | - logger.info("Populate Electrode...") |
34 | | - Electrode.populate(fp) |
35 | | - |
36 | | - logger.info("Populate Raw...") |
37 | | - Raw.populate(fp) |
38 | | - |
39 | | - logger.info("Populate SampleCount...") |
40 | | - SampleCount.populate(fp) |
41 | | - |
42 | | - logger.info("Populate DIOEvents...") |
43 | | - DIOEvents.populate(fp) |
44 | | - |
45 | | - # sensor data (from analog ProcessingModule) is temporarily removed from NWBFile |
46 | | - # to reduce file size while it is not being used. add it back in by commenting out |
47 | | - # the removal code in spyglass/data_import/insert_sessions.py when ready |
48 | | - # logger.info('Populate SensorData') |
49 | | - # SensorData.populate(fp) |
50 | | - |
51 | | - logger.info("Populate TaskEpochs") |
52 | | - TaskEpoch.populate(fp) |
53 | | - logger.info("Populate StateScriptFile") |
54 | | - StateScriptFile.populate(fp) |
55 | | - logger.info("Populate VideoFile") |
56 | | - VideoFile.populate(fp) |
57 | | - logger.info("RawPosition...") |
58 | | - PositionSource.insert_from_nwbfile(nwb_file_name) |
59 | | - RawPosition.populate(fp) |
60 | | - |
61 | | - logger.info("Populate ImportedSpikeSorting...") |
| 24 | + """Insert all common tables for a given NWB file.""" |
62 | 25 | from spyglass.spikesorting.imported import ImportedSpikeSorting |
63 | 26 |
|
64 | | - ImportedSpikeSorting.populate(fp) |
| 27 | + key = [(Nwbfile & f"nwb_file_name LIKE '{nwb_file_name}'").proj()] |
| 28 | + tables = [ |
| 29 | + Session, |
| 30 | + # NwbfileKachery, # Not used by default |
| 31 | + ElectrodeGroup, |
| 32 | + Electrode, |
| 33 | + Raw, |
| 34 | + SampleCount, |
| 35 | + DIOEvents, |
| 36 | + # SensorData, # Not used by default. Generates large files |
| 37 | + RawPosition, |
| 38 | + TaskEpoch, |
| 39 | + StateScriptFile, |
| 40 | + VideoFile, |
| 41 | + PositionSource, |
| 42 | + RawPosition, |
| 43 | + ImportedSpikeSorting, |
| 44 | + ] |
| 45 | + error_constants = dict( |
| 46 | + dj_user=dj.config["database.user"], |
| 47 | + connection_id=dj.conn().connection_id, |
| 48 | + nwb_file_name=nwb_file_name, |
| 49 | + ) |
| 50 | + |
| 51 | + for table in tables: |
| 52 | + logger.info(f"Populating {table.__name__}...") |
| 53 | + try: |
| 54 | + table.populate(key) |
| 55 | + except Exception as e: |
| 56 | + InsertError.insert1( |
| 57 | + dict( |
| 58 | + **error_constants, |
| 59 | + table=table.__name__, |
| 60 | + error_type=type(e).__name__, |
| 61 | + error_message=str(e), |
| 62 | + error_raw=str(e), |
| 63 | + ) |
| 64 | + ) |
| 65 | + query = InsertError & error_constants |
| 66 | + if query: |
| 67 | + err_tables = query.fetch("table") |
| 68 | + logger.error( |
| 69 | + f"Errors occurred during population for {nwb_file_name}:\n\t" |
| 70 | + + f"Failed tables {err_tables}\n\t" |
| 71 | + + "See common_usage.InsertError for more details" |
| 72 | + ) |
| 73 | + return query.fetch("KEY") |
0 commit comments