Skip to content

Commit 6d7ad7c

Browse files
authored
Merge pull request #1 from ttngu207/chris-nwb
nwb function specification in linking_module
2 parents 2595dae + bf5e82a commit 6d7ad7c

File tree

1 file changed

+17
-38
lines changed
  • element_array_ephys/export/nwb

1 file changed

+17
-38
lines changed

element_array_ephys/export/nwb/nwb.py

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
from spikeinterface import extractors
1414
from tqdm import tqdm
1515

16+
from .. import probe, ephys
17+
18+
assert probe.schema.is_activated() and ephys.schema.is_activated()
19+
1620

1721
class DecimalEncoder(json.JSONEncoder):
1822
def default(self, o):
@@ -502,23 +506,14 @@ def add_ephys_lfp_from_source_to_nwb(
502506

503507
def ecephys_session_to_nwb(
504508
session_key,
505-
subject_id=None,
506509
raw=True,
507510
spikes=True,
508511
lfp="source",
509512
end_frame=None,
510513
lab_key=None,
511514
project_key=None,
512515
protocol_key=None,
513-
nwbfile_kwargs=None,
514-
# workflow_module='workflow_array_ephys.pipeline',
515-
# skull_reference='',
516-
lab_schema_name=(dj.config['custom']['database.prefix']+'lab'),
517-
subject_schema_name=(dj.config['custom']['database.prefix']+'subject'),
518-
session_schema_name=(dj.config['custom']['database.prefix']+'session'),
519-
ephys_schema_name=(dj.config['custom']['database.prefix']+'ephys'),
520-
probe_schema_name=(dj.config['custom']['database.prefix']+'probe'),
521-
ephys_root_data_dir=(dj.config.get('custom', {}).get('ephys_root_data_dir', None))):
516+
nwbfile_kwargs=None):
522517
"""
523518
Main function for converting ephys data to NWB
524519
@@ -546,43 +541,28 @@ def ecephys_session_to_nwb(
546541
- If element-session is being used, this argument can optionally be used to add over overwrite NWBFile fields.
547542
"""
548543

549-
global ephys, probe
550-
ephys = dj.create_virtual_module('ephys',ephys_schema_name)
551-
probe = dj.create_virtual_module('probe',probe_schema_name)
552-
553544
try:
554-
import element_session
555-
from element_session import session
556-
from element_session.export.nwb import session_to_nwb
557-
if not element_session.session.schema.is_activated():
558-
session = dj.create_virtual_module('session', session_schema_name)
559-
HAVE_ELEMENT_SESSION = True
560-
except ModuleNotFoundError:
561-
HAVE_ELEMENT_SESSION = False
562-
563-
if HAVE_ELEMENT_SESSION:
545+
session_to_nwb = getattr(ephys._linking_module, 'session_to_nwb')
546+
except AttributeError:
547+
if not (
548+
isinstance(nwbfile_kwargs, dict)
549+
and {"session_description", "identifier", "session_start_time"}.issubset(nwbfile_kwargs)):
550+
raise ValueError(
551+
"If session_to_nwb is not provided, you must include nwbfile_kwargs as a dictionary."
552+
"Required fields are 'session_description' (str), 'identifier' (str), and 'session_start_time' (datetime)")
553+
nwbfile = pynwb.NWBFile(**nwbfile_kwargs)
554+
else:
564555
nwbfile = session_to_nwb(
565556
session_key,
566-
lab_schema_name=lab_schema_name,
567-
subject_schema_name=subject_schema_name,
568-
session_schema_name=session_schema_name,
569-
subject_id=subject_id,
570557
lab_key=lab_key,
571558
project_key=project_key,
572559
protocol_key=protocol_key,
573560
additional_nwbfile_kwargs=nwbfile_kwargs,
574561
)
575-
else:
576-
if not (
577-
isinstance(nwbfile_kwargs, dict)
578-
and {"session_description", "identifier", "session_start_time"}.issubset(nwbfile_kwargs)):
579-
raise ValueError(
580-
"If element-session is not activated, you must include nwbfile_kwargs as a dictionary."
581-
"Required fields are 'session_description' (str), 'identifier' (str), and 'session_start_time' (datetime)")
582-
nwbfile = pynwb.NWBFile(**nwbfile_kwargs)
562+
563+
ephys_root_data_dir = ephys.get_ephys_root_data_dir()
583564

584565
if raw:
585-
assert ephys_root_data_dir, 'Need to supply root directory for retrieving raw files'
586566
add_ephys_recording_to_nwb(session_key, ephys_root_data_dir=ephys_root_data_dir,
587567
nwbfile=nwbfile, end_frame=end_frame)
588568

@@ -593,7 +573,6 @@ def ecephys_session_to_nwb(
593573
add_ephys_lfp_from_dj_to_nwb(session_key, nwbfile)
594574

595575
if lfp == "source":
596-
assert ephys_root_data_dir, 'Need to supply root directory for retrieving raw files'
597576
add_ephys_lfp_from_source_to_nwb(session_key, ephys_root_data_dir=ephys_root_data_dir,
598577
nwbfile=nwbfile, end_frame=end_frame)
599578

0 commit comments

Comments
 (0)