Skip to content

Commit e218b15

Browse files
Merge pull request #138 from NeurodataWithoutBorders/check_session_start_time
[Add Check]: attempt to check for 'default' start times
2 parents f284065 + 1f9cb38 commit e218b15

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

docs/best_practices/nwbfile_metadata.rst

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,25 @@ File Organization
1111

1212
.. _best_practice_global_time_reference:
1313

14-
Global Time Reference
15-
~~~~~~~~~~~~~~~~~~~~~
14+
Global Date and Time Reference
15+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1616

17-
An :nwb-schema:ref:`sec-NWBFile` can have two primary time references. The global time reference for all objects in the
18-
:nwb-schema:ref:`sec-NWBFile` is the ``timestamps_reference_time``. By default, this is also set to the
19-
``session_start_time``, but when writing multiple NWBFiles that are all designed to align
20-
to the same time reference, the ``session_start_time`` may be set separately from the explicitly specified common
21-
``timestamps_reference_time`` used across all of the NWBFiles.
17+
An :nwb-schema:ref:`sec-NWBFile` can have two primary time references. The global date and time reference for all
18+
objects in the :nwb-schema:ref:`sec-NWBFile` is the ``timestamps_reference_time``. By default, this is set to the
19+
``session_start_time``, but when writing multiple NWBFiles that are all designed to align to the same time reference,
20+
the ``timestamp_reference_time`` used across all of the NWBFiles may be set separately from the ``session_start_time``.
2221

2322
All time-related data in the NWBFile should be synchronized to the ``timestamps_reference_time`` so that future users
2423
are able to understand the timing of all events contained within the NWBFile.
2524

25+
Given the importance of this field within an :nwb-schema:ref:`sec-NWBFile`, is it critical that it be set to a proper
26+
value. Default values should generally not be used for this field. If the true date is unknown, use your
27+
best guess. If the exact start time is unknown, then it is fine to simply set it to midnight on that date.
28+
29+
30+
Check functions: :py:meth:`~nwbinspector.checks.nwbfile_metadata.check_session_start_time_old_date`,
31+
:py:meth:`~nwbinspector.checks.nwbfile_metadata.check_session_start_time_future_date`,
32+
2633

2734

2835
.. _best_practice_acquisition_and_processing:

nwbinspector/checks/nwbfile_metadata.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Check functions that examine general NWBFile metadata."""
22
import re
3+
from datetime import datetime
34

45
from pynwb import NWBFile, ProcessingModule
56
from pynwb.file import Subject
@@ -15,6 +16,27 @@
1516
PROCESSING_MODULE_CONFIG = ["ophys", "ecephys", "icephys", "behavior", "misc", "ogen", "retinotopy"]
1617

1718

19+
@register_check(importance=Importance.BEST_PRACTICE_SUGGESTION, neurodata_type=NWBFile)
20+
def check_session_start_time_old_date(nwbfile: NWBFile):
21+
"""Check if the session_start_time was set to an appropriate value."""
22+
if nwbfile.session_start_time <= datetime(1980, 1, 1).astimezone():
23+
return InspectorMessage(
24+
message=(
25+
f"The session_start_time ({nwbfile.session_start_time}) may not be set to the true date of the "
26+
"recording."
27+
)
28+
)
29+
30+
31+
@register_check(importance=Importance.CRITICAL, neurodata_type=NWBFile)
32+
def check_session_start_time_future_date(nwbfile: NWBFile):
33+
"""Check if the session_start_time was set to an appropriate value."""
34+
if nwbfile.session_start_time >= datetime.now().astimezone():
35+
return InspectorMessage(
36+
message=f"The session_start_time ({nwbfile.session_start_time}) is set to a future date and time."
37+
)
38+
39+
1840
@register_check(importance=Importance.BEST_PRACTICE_SUGGESTION, neurodata_type=NWBFile)
1941
def check_experimenter(nwbfile: NWBFile):
2042
"""Check if an experimenter has been added for the session."""

tests/unit_tests/test_nwbfile_metadata.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from uuid import uuid4
2-
from datetime import datetime
2+
from datetime import datetime, timezone
33

44
from pynwb import NWBFile, ProcessingModule
55
from pynwb.file import Subject
@@ -18,6 +18,8 @@
1818
check_subject_age,
1919
check_subject_species,
2020
check_processing_module_name,
21+
check_session_start_time_old_date,
22+
check_session_start_time_future_date,
2123
PROCESSING_MODULE_CONFIG,
2224
)
2325
from nwbinspector.tools import make_minimal_nwbfile
@@ -26,6 +28,47 @@
2628
minimal_nwbfile = make_minimal_nwbfile()
2729

2830

31+
def test_check_session_start_time_old_date_pass():
32+
assert check_session_start_time_old_date(minimal_nwbfile) is None
33+
34+
35+
def test_check_session_start_time_old_date_fail():
36+
nwbfile = NWBFile(
37+
session_description="",
38+
identifier=str(uuid4()),
39+
session_start_time=datetime(1970, 1, 1, 0, 0, 0, 0, timezone.utc),
40+
)
41+
assert check_session_start_time_old_date(nwbfile) == InspectorMessage(
42+
message="The session_start_time (1970-01-01 00:00:00+00:00) may not be set to the true date of the recording.",
43+
importance=Importance.BEST_PRACTICE_SUGGESTION,
44+
check_function_name="check_session_start_time_old_date",
45+
object_type="NWBFile",
46+
object_name="root",
47+
location="/",
48+
)
49+
50+
51+
def test_check_session_start_time_future_date_pass():
52+
nwbfile = NWBFile(session_description="", identifier=str(uuid4()), session_start_time=datetime(2010, 1, 1))
53+
assert check_session_start_time_future_date(nwbfile) is None
54+
55+
56+
def test_check_session_start_time_future_date_fail():
57+
nwbfile = NWBFile(
58+
session_description="",
59+
identifier=str(uuid4()),
60+
session_start_time=datetime(2030, 1, 1, 0, 0, 0, 0, timezone.utc),
61+
)
62+
assert check_session_start_time_future_date(nwbfile) == InspectorMessage(
63+
message="The session_start_time (2030-01-01 00:00:00+00:00) is set to a future date and time.",
64+
importance=Importance.CRITICAL,
65+
check_function_name="check_session_start_time_future_date",
66+
object_type="NWBFile",
67+
object_name="root",
68+
location="/",
69+
)
70+
71+
2972
def test_check_experimenter():
3073
assert check_experimenter(minimal_nwbfile) == InspectorMessage(
3174
message="Experimenter is missing.",

0 commit comments

Comments
 (0)