|
28 | 28 | ) |
29 | 29 | from dandischema.models import Dandiset as DandisetMeta |
30 | 30 | from dateutil.tz import tzutc |
| 31 | +from hdmf.common import DynamicTable |
| 32 | +import numpy as np |
31 | 33 | from pydantic import ByteSize |
| 34 | +from pynwb import NWBHDF5IO, NWBFile, TimeSeries |
32 | 35 | import pytest |
33 | 36 | import requests |
34 | 37 | from semantic_version import Version |
@@ -471,6 +474,239 @@ def test_time_extract_gest() -> None: |
471 | 474 | ) |
472 | 475 |
|
473 | 476 |
|
| 477 | +@pytest.mark.ai_generated |
| 478 | +def test_session_duration_extraction(tmp_path: Path) -> None: |
| 479 | + """Test that session duration is extracted and included in Session activity""" |
| 480 | + # Create a test NWB file with TimeSeries data |
| 481 | + nwb_path = tmp_path / "test_duration.nwb" |
| 482 | + session_start = datetime(2020, 1, 1, 12, 0, 0, tzinfo=tzutc()) |
| 483 | + |
| 484 | + nwbfile = NWBFile( |
| 485 | + session_description="test session for duration", |
| 486 | + identifier="test_duration_123", |
| 487 | + session_start_time=session_start, |
| 488 | + ) |
| 489 | + |
| 490 | + # Add a TimeSeries that spans 100 seconds (timestamps from 0 to 100) |
| 491 | + data = np.random.rand(1000) |
| 492 | + timestamps = np.linspace(0, 100, 1000) |
| 493 | + ts1 = TimeSeries(name="timeseries1", data=data, unit="volts", timestamps=timestamps) |
| 494 | + nwbfile.add_acquisition(ts1) |
| 495 | + |
| 496 | + # Add another TimeSeries using starting_time and rate |
| 497 | + # This one goes from 50s to 150s (100 samples at 1 Hz) |
| 498 | + data2 = np.random.rand(100) |
| 499 | + ts2 = TimeSeries( |
| 500 | + name="timeseries2", data=data2, unit="volts", starting_time=50.0, rate=1.0 |
| 501 | + ) |
| 502 | + nwbfile.add_acquisition(ts2) |
| 503 | + |
| 504 | + # Write the file |
| 505 | + with NWBHDF5IO(str(nwb_path), "w") as io: |
| 506 | + io.write(nwbfile) |
| 507 | + |
| 508 | + # Extract metadata |
| 509 | + from ..metadata.nwb import get_metadata, nwb2asset |
| 510 | + |
| 511 | + metadata = get_metadata(nwb_path) |
| 512 | + |
| 513 | + # Check that session_end_time was calculated |
| 514 | + assert "session_start_time" in metadata |
| 515 | + assert "session_end_time" in metadata |
| 516 | + |
| 517 | + # Calculate duration - should be 150 seconds (max) - 0 seconds (min) |
| 518 | + duration = ( |
| 519 | + metadata["session_end_time"] - metadata["session_start_time"] |
| 520 | + ).total_seconds() |
| 521 | + assert abs(duration - 150.0) < 1.0 # Allow small floating point errors |
| 522 | + |
| 523 | + # Check that Session activity includes endDate |
| 524 | + asset = nwb2asset(nwb_path, digest=DUMMY_DANDI_ETAG) |
| 525 | + assert asset.wasGeneratedBy is not None |
| 526 | + |
| 527 | + # Find Session activities |
| 528 | + sessions = [act for act in asset.wasGeneratedBy if act.schemaKey == "Session"] |
| 529 | + assert len(sessions) > 0 |
| 530 | + |
| 531 | + session = sessions[0] |
| 532 | + assert session.startDate is not None |
| 533 | + assert session.endDate is not None |
| 534 | + assert session.startDate == metadata["session_start_time"] |
| 535 | + assert session.endDate == metadata["session_end_time"] |
| 536 | + |
| 537 | + |
| 538 | +@pytest.mark.ai_generated |
| 539 | +def test_session_duration_with_trials(tmp_path: Path) -> None: |
| 540 | + """Test that session duration includes trials table timestamps""" |
| 541 | + # Create a test NWB file with trials |
| 542 | + nwb_path = tmp_path / "test_duration_trials.nwb" |
| 543 | + session_start = datetime(2020, 1, 1, 12, 0, 0, tzinfo=tzutc()) |
| 544 | + |
| 545 | + nwbfile = NWBFile( |
| 546 | + session_description="test session with trials", |
| 547 | + identifier="test_trials_123", |
| 548 | + session_start_time=session_start, |
| 549 | + ) |
| 550 | + |
| 551 | + # Add a TimeSeries that spans from 10 to 50 seconds |
| 552 | + data = np.random.rand(400) |
| 553 | + timestamps = np.linspace(10, 50, 400) |
| 554 | + ts = TimeSeries(name="timeseries1", data=data, unit="volts", timestamps=timestamps) |
| 555 | + nwbfile.add_acquisition(ts) |
| 556 | + |
| 557 | + # Add trials that extend the session to 200 seconds |
| 558 | + nwbfile.add_trial_column( |
| 559 | + name="correct", description="whether the trial was correct" |
| 560 | + ) |
| 561 | + nwbfile.add_trial(start_time=5.0, stop_time=15.0, correct=True) |
| 562 | + nwbfile.add_trial(start_time=20.0, stop_time=30.0, correct=False) |
| 563 | + nwbfile.add_trial(start_time=100.0, stop_time=200.0, correct=True) |
| 564 | + |
| 565 | + # Write the file |
| 566 | + with NWBHDF5IO(str(nwb_path), "w") as io: |
| 567 | + io.write(nwbfile) |
| 568 | + |
| 569 | + # Extract metadata |
| 570 | + from ..metadata.nwb import get_metadata, nwb2asset |
| 571 | + |
| 572 | + metadata = get_metadata(nwb_path) |
| 573 | + |
| 574 | + # Check that session_end_time was calculated |
| 575 | + assert "session_start_time" in metadata |
| 576 | + assert "session_end_time" in metadata |
| 577 | + |
| 578 | + # Calculate duration - should be 200 (max from trials) - 5 (min from trials) = 195 seconds |
| 579 | + duration = ( |
| 580 | + metadata["session_end_time"] - metadata["session_start_time"] |
| 581 | + ).total_seconds() |
| 582 | + assert abs(duration - 195.0) < 1.0 # Allow small floating point errors |
| 583 | + |
| 584 | + # Check that Session activity includes endDate |
| 585 | + asset = nwb2asset(nwb_path, digest=DUMMY_DANDI_ETAG) |
| 586 | + assert asset.wasGeneratedBy is not None |
| 587 | + |
| 588 | + # Find Session activities |
| 589 | + sessions = [act for act in asset.wasGeneratedBy if act.schemaKey == "Session"] |
| 590 | + assert len(sessions) > 0 |
| 591 | + |
| 592 | + session = sessions[0] |
| 593 | + assert session.startDate is not None |
| 594 | + assert session.endDate is not None |
| 595 | + assert session.startDate == metadata["session_start_time"] |
| 596 | + assert session.endDate == metadata["session_end_time"] |
| 597 | + |
| 598 | + |
| 599 | +@pytest.mark.ai_generated |
| 600 | +def test_session_duration_with_units(tmp_path: Path) -> None: |
| 601 | + """Test that session duration includes spike_times from Units table""" |
| 602 | + # Create a test NWB file with Units table |
| 603 | + nwb_path = tmp_path / "test_duration_units.nwb" |
| 604 | + session_start = datetime(2020, 1, 1, 12, 0, 0, tzinfo=tzutc()) |
| 605 | + |
| 606 | + nwbfile = NWBFile( |
| 607 | + session_description="test session with units", |
| 608 | + identifier="test_units_123", |
| 609 | + session_start_time=session_start, |
| 610 | + ) |
| 611 | + |
| 612 | + # Add a simple TimeSeries that spans from 10 to 30 seconds |
| 613 | + data = np.random.rand(200) |
| 614 | + timestamps = np.linspace(10, 30, 200) |
| 615 | + ts = TimeSeries(name="timeseries1", data=data, unit="volts", timestamps=timestamps) |
| 616 | + nwbfile.add_acquisition(ts) |
| 617 | + |
| 618 | + # Add Units with spike_times that extend session to 250 seconds |
| 619 | + # Unit 1: spikes from 5s to 100s |
| 620 | + # Unit 2: spikes from 50s to 250s |
| 621 | + nwbfile.add_unit(spike_times=np.array([5.0, 10.0, 20.0, 50.0, 100.0])) |
| 622 | + nwbfile.add_unit(spike_times=np.array([50.0, 100.0, 150.0, 200.0, 250.0])) |
| 623 | + |
| 624 | + # Write the file |
| 625 | + with NWBHDF5IO(str(nwb_path), "w") as io: |
| 626 | + io.write(nwbfile) |
| 627 | + |
| 628 | + # Extract metadata |
| 629 | + from ..metadata.nwb import get_metadata |
| 630 | + |
| 631 | + metadata = get_metadata(nwb_path) |
| 632 | + |
| 633 | + # Check that session_end_time was calculated |
| 634 | + assert "session_start_time" in metadata |
| 635 | + assert "session_end_time" in metadata |
| 636 | + |
| 637 | + # Duration should be 250 (max spike) - 5 (min spike) = 245 seconds |
| 638 | + duration = ( |
| 639 | + metadata["session_end_time"] - metadata["session_start_time"] |
| 640 | + ).total_seconds() |
| 641 | + assert abs(duration - 245.0) < 1.0 # Allow small floating point errors |
| 642 | + |
| 643 | + |
| 644 | +@pytest.mark.ai_generated |
| 645 | +def test_session_duration_with_events(tmp_path: Path) -> None: |
| 646 | + """Test that session duration includes timestamp/duration from DynamicTable""" |
| 647 | + # Create a test NWB file with a DynamicTable containing timestamp and duration |
| 648 | + nwb_path = tmp_path / "test_duration_events.nwb" |
| 649 | + session_start = datetime(2020, 1, 1, 12, 0, 0, tzinfo=tzutc()) |
| 650 | + |
| 651 | + nwbfile = NWBFile( |
| 652 | + session_description="test session with events", |
| 653 | + identifier="test_events_123", |
| 654 | + session_start_time=session_start, |
| 655 | + ) |
| 656 | + |
| 657 | + # Add a simple TimeSeries that spans from 5 to 20 seconds |
| 658 | + data = np.random.rand(150) |
| 659 | + timestamps = np.linspace(5, 20, 150) |
| 660 | + ts = TimeSeries(name="timeseries1", data=data, unit="volts", timestamps=timestamps) |
| 661 | + nwbfile.add_acquisition(ts) |
| 662 | + |
| 663 | + # Create a DynamicTable with timestamp and duration columns (similar to EventsTable) |
| 664 | + |
| 665 | + events_table = DynamicTable( |
| 666 | + name="events", |
| 667 | + description="test events with timestamps and durations", |
| 668 | + ) |
| 669 | + events_table.add_column( |
| 670 | + name="timestamp", |
| 671 | + description="event timestamps", |
| 672 | + ) |
| 673 | + events_table.add_column( |
| 674 | + name="duration", |
| 675 | + description="event durations", |
| 676 | + ) |
| 677 | + |
| 678 | + # Add events: event at 3s lasting 2s (ends at 5s) |
| 679 | + # event at 100s lasting 80s (ends at 180s) |
| 680 | + events_table.add_row(timestamp=3.0, duration=2.0) |
| 681 | + events_table.add_row(timestamp=100.0, duration=30.0) |
| 682 | + events_table.add_row(timestamp=150.0, duration=10.0) |
| 683 | + |
| 684 | + # Add the table to a processing module |
| 685 | + processing_module = nwbfile.create_processing_module( |
| 686 | + name="behavior", description="behavioral data" |
| 687 | + ) |
| 688 | + processing_module.add(events_table) |
| 689 | + |
| 690 | + # Write the file |
| 691 | + with NWBHDF5IO(str(nwb_path), "w") as io: |
| 692 | + io.write(nwbfile) |
| 693 | + |
| 694 | + # Extract metadata |
| 695 | + from ..metadata.nwb import get_metadata |
| 696 | + |
| 697 | + metadata = get_metadata(nwb_path) |
| 698 | + |
| 699 | + # Check that session_end_time was calculated |
| 700 | + assert "session_start_time" in metadata |
| 701 | + assert "session_end_time" in metadata |
| 702 | + |
| 703 | + # Duration should be 180 (100 + 80, max end) - 3 (min timestamp) = 177 seconds |
| 704 | + duration = ( |
| 705 | + metadata["session_end_time"] - metadata["session_start_time"] |
| 706 | + ).total_seconds() |
| 707 | + assert abs(duration - 157.0) < 1.0 # Allow small floating point errors |
| 708 | + |
| 709 | + |
474 | 710 | @mark_xfail_ontobee |
475 | 711 | @mark.skipif_no_network |
476 | 712 | @pytest.mark.obolibrary |
|
0 commit comments