Skip to content

Export to nwb #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 54 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1b684d3
export session to nwb
bendichter Dec 15, 2021
2d1f76d
Merge branch 'main' into export_to_nwb
bendichter Dec 15, 2021
bb73c58
Update nwb.py
bendichter Dec 15, 2021
79bdcc6
Update element_session/export/nwb.py
bendichter Dec 16, 2021
e069b75
Update element_session/export/nwb.py
bendichter Dec 16, 2021
5e4be2f
user `dja` -> `anaconda`
CBroz1 Dec 17, 2021
48db607
Update element_session/export/nwb.py
bendichter Dec 17, 2021
62b3927
Update element_session/export/nwb.py
bendichter Dec 17, 2021
7cc7988
Update element_session/export/nwb.py
bendichter Dec 17, 2021
22ac715
Update requirements.txt
bendichter Dec 28, 2021
8e03909
Update element_session/export/nwb.py
bendichter Dec 29, 2021
7eda08d
Update element_session/export/nwb.py
bendichter Dec 29, 2021
233535a
simpler imports
bendichter Jan 7, 2022
24ce00e
Update element_session/export/nwb.py
bendichter Jan 25, 2022
e03604a
Update element_session/export/nwb.py
bendichter Jan 25, 2022
dc1d67a
Update element_session/export/nwb.py
bendichter Jan 25, 2022
b7e7577
Update element_session/export/nwb.py
bendichter Jan 25, 2022
49022ac
* import subject_to_nwb from element_animal
bendichter Jan 26, 2022
469e145
remove timezone stuff
bendichter Jan 26, 2022
c9ac2e5
Update element_session/export/nwb.py
bendichter Jan 26, 2022
c9c874e
* add lab, project, and protocol keys
bendichter Jan 26, 2022
9672ad4
Merge remote-tracking branch 'bendichter/export_to_nwb' into export_t…
bendichter Jan 26, 2022
b25bdf6
Update element_session/session.py
bendichter Jan 27, 2022
480b99e
fix subject lookup
bendichter Jan 30, 2022
f1284de
import session from workflow
bendichter Jan 30, 2022
c750ce2
* handle experimenters
bendichter Jan 30, 2022
f85ee55
pass session_key to subject
bendichter Jan 30, 2022
263ed7a
add test
bendichter Feb 9, 2022
c89c172
Rebased. See Details
CBroz1 Jan 28, 2022
7c82d16
Update element_session/export/nwb.py
bendichter Feb 10, 2022
85912ba
Update element_session/export/nwb.py
bendichter Feb 10, 2022
957884b
Update element_session/export/test_nwb.py
bendichter Feb 10, 2022
f1de431
Update element_session/export/test_nwb.py
bendichter Feb 10, 2022
f039d25
Avoid linking_module issues. See details.
CBroz1 Feb 11, 2022
7ec8115
nwb function specification in linking_module
Feb 15, 2022
478602c
Merge pull request #3 from ttngu207/chris-nwb
CBroz1 Feb 15, 2022
414e388
modify docstring
CBroz1 Feb 16, 2022
17c3d52
fix test indenting
bendichter Feb 22, 2022
2d8a2af
Merge branch 'main' into export_to_nwb
bendichter Feb 22, 2022
093ddd8
Merge remote-tracking branch 'cbroz1/ben' into export_to_nwb
bendichter Feb 28, 2022
b85ee53
merge with linking module
bendichter Feb 28, 2022
2dc67b6
Merge remote-tracking branch 'bendichter/export_to_nwb' into export_t…
bendichter Feb 28, 2022
1da9a60
merge with linking module
bendichter Mar 1, 2022
f90c9da
rmv tests (they are moved to workflow-array-ephys)
bendichter Mar 1, 2022
8bbaa8e
Merge remote-tracking branch 'dj/main' into export_to_nwb
bendichter Mar 1, 2022
d806913
add init
bendichter Mar 4, 2022
99fcb8f
Update element_session/export/nwb.py
bendichter Mar 14, 2022
9f751bc
Update element_session/export/nwb.py
bendichter Mar 14, 2022
0aed4b3
Update element_session/export/nwb.py
bendichter Mar 14, 2022
eaefef9
Update element_session/export/nwb.py
bendichter Mar 14, 2022
5605270
Update element_session/session_with_id.py
bendichter Mar 14, 2022
49ad6fe
Update element_session/export/nwb.py
bendichter Mar 14, 2022
5ca6470
Update element_session/export/nwb.py
bendichter Mar 14, 2022
89abe1c
Update element_session/export/nwb.py
bendichter Mar 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
107 changes: 107 additions & 0 deletions element_session/export/nwb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import datetime
from uuid import uuid4
import pynwb

from .. import session

assert session.schema.is_activated()


def session_to_nwb(
session_key: dict,
lab_key=None,
project_key=None,
protocol_key=None,
additional_nwbfile_kwargs=None,
):
"""Gather session- and subject-level metadata and use it to create an NWBFile. If
there is no subject_to_nwb export function in the current namespace, subject_id will
be inferred from the set of primary attributes in the Subject table upstream of Session.

Parameters
----------
session_key: dict,
e.g.,
{
'subject': 'subject5',
'session_datetime': datetime.datetime(2020, 5, 12, 4, 13, 7),
}
lab_key, project_key, protocol_key: dict, optional
Used to gather additional optional metadata.
additional_nwbfile_kwargs: dict, optional
Optionally overwrite or add fields to NWBFile.

Returns
-------
pynwb.NWBFile

mappings:
session.Session::KEY -> NWBFile.session_id
session.Session::session_datetime -> NWBFile.session_start_time
session.SessionNote::session_note -> NWBFile.session_description
session.SessionExperimenter::user -> NWBFile.experimenter

subject.Subject::subject -> NWBFile.subject.subject_id
subject.Subject::sex -> NWBFile.subject.sex

lab.Lab::institution -> NWBFile.institution
lab.Lab::lab_name -> NWBFile.lab

lab.Protocol::protocol -> NWBFile.protocol
lab.Protocol::protocol_description -> NWBFile.notes

lab.Project::project_description -> NWBFile.experiment_description
lab.ProjectKeywords.keyword -> NWBFile.keywords
lab.ProjectPublication.publication -> NWBFile.related_publications

"""

# ensure only one session key is entered
session_key = (session.Session & session_key).fetch1("KEY")

session_identifier = {
k: v.isoformat() if isinstance(v, datetime.datetime) else v
for k, v in session_key.items()
}

nwbfile_kwargs = dict(
session_id="_".join(session_identifier.values()), identifier=str(uuid4()),
)

session_info = (session.Session & session_key).join(session.SessionNote, left=True).fetch1()

nwbfile_kwargs.update(
session_start_time=session_info["session_datetime"].astimezone(
datetime.timezone.utc
)
)

nwbfile_kwargs.update(session_description=session_info.get("session_note", ""))

experimenters =(session.SessionExperimenter & session_key).fetch("user")

nwbfile_kwargs.update(experimenter=list(experimenters) or None)

subject_to_nwb = getattr(session._linking_module, "subject_to_nwb", False)

if subject_to_nwb:
nwbfile_kwargs.update(subject=subject_to_nwb(session_key))
else:
subject_id = '_'.join((getattr(session._linking_module, 'Subject') & session_key).fetch1('KEY').values())
nwbfile_kwargs.update(subject=pynwb.file.Subject(subject_id=subject_id))

if any([lab_key, project_key, protocol_key]):
element_lab_to_nwb_dict = getattr(session._linking_module, "element_lab_to_nwb_dict", {})

nwbfile_kwargs.update(
element_lab_to_nwb_dict(
lab_key=lab_key,
project_key=project_key,
protocol_key=protocol_key,
)
)

if additional_nwbfile_kwargs is not None:
nwbfile_kwargs.update(additional_nwbfile_kwargs)

return pynwb.NWBFile(**nwbfile_kwargs)
4 changes: 2 additions & 2 deletions element_session/session_with_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import inspect

schema = dj.schema()

_linking_module = None


Expand Down Expand Up @@ -46,7 +45,7 @@ class Session(dj.Manual):
---
session_datetime: datetime
"""


@schema
class SessionDirectory(dj.Manual):
Expand All @@ -72,6 +71,7 @@ class SessionNote(dj.Manual):
-> Session
---
session_note: varchar(1024)

"""


Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
datajoint>=0.13.0
datajoint>=0.13.0
pynwb