|
4 | 4 | from tempfile import mkdtemp |
5 | 5 | from pathlib import Path |
6 | 6 | from unittest import TestCase |
| 7 | +from datetime import datetime |
7 | 8 |
|
8 | 9 | import numpy as np |
9 | 10 | from pynwb import NWBFile, NWBHDF5IO, TimeSeries |
10 | 11 | from pynwb.file import TimeIntervals |
11 | 12 | from pynwb.behavior import SpatialSeries, Position |
12 | 13 | from hdmf.common import DynamicTable |
| 14 | +from natsort import natsorted |
13 | 15 |
|
14 | 16 | from nwbinspector import ( |
15 | 17 | Importance, |
16 | 18 | check_small_dataset_compression, |
17 | 19 | check_regular_timestamps, |
18 | 20 | check_data_orientation, |
19 | 21 | check_timestamps_match_first_dimension, |
| 22 | + check_subject_exists, |
| 23 | + load_config, |
20 | 24 | ) |
21 | | -from nwbinspector.nwbinspector import inspect_all, inspect_nwb |
| 25 | +from nwbinspector import inspect_all, inspect_nwb |
22 | 26 | from nwbinspector.register_checks import Severity, InspectorMessage, register_check |
23 | 27 | from nwbinspector.utils import FilePathType, is_module_installed |
24 | 28 | from nwbinspector.tools import make_minimal_nwbfile |
@@ -144,7 +148,7 @@ def assertLogFileContentsEqual( |
144 | 148 | if ".nwb" in test_line: |
145 | 149 | # Transform temporary testing path and formatted to hardcoded fake path |
146 | 150 | str_loc = test_line.find(".nwb") |
147 | | - correction_str = test_line.replace(test_line[5 : str_loc - 8], "./") |
| 151 | + correction_str = test_line.replace(test_line[5 : str_loc - 8], "./") # noqa: E203 (black) |
148 | 152 | test_file_lines[line_number] = correction_str |
149 | 153 | self.assertEqual(first=test_file_lines[skip_first_n_lines:-1], second=true_file_lines) |
150 | 154 |
|
@@ -501,6 +505,71 @@ def test_inspect_nwb_manual_iteration_stop(self): |
501 | 505 | with self.assertRaises(expected_exception=StopIteration): |
502 | 506 | next(generator) |
503 | 507 |
|
| 508 | + def test_inspect_nwb_dandi_config(self): |
| 509 | + config_checks = [check_subject_exists] + self.checks |
| 510 | + test_results = list( |
| 511 | + inspect_nwb( |
| 512 | + nwbfile_path=self.nwbfile_paths[0], |
| 513 | + checks=config_checks, |
| 514 | + config=load_config(filepath_or_keyword="dandi"), |
| 515 | + ) |
| 516 | + ) |
| 517 | + true_results = [ |
| 518 | + InspectorMessage( |
| 519 | + message="Subject is missing.", |
| 520 | + importance=Importance.BEST_PRACTICE_SUGGESTION, |
| 521 | + check_function_name="check_subject_exists", |
| 522 | + object_type="NWBFile", |
| 523 | + object_name="root", |
| 524 | + location="/", |
| 525 | + file_path=self.nwbfile_paths[0], |
| 526 | + ), |
| 527 | + InspectorMessage( |
| 528 | + message="data is not compressed. Consider enabling compression when writing a dataset.", |
| 529 | + importance=Importance.BEST_PRACTICE_SUGGESTION, |
| 530 | + check_function_name="check_small_dataset_compression", |
| 531 | + object_type="TimeSeries", |
| 532 | + object_name="test_time_series_1", |
| 533 | + location="/acquisition/test_time_series_1", |
| 534 | + file_path=self.nwbfile_paths[0], |
| 535 | + ), |
| 536 | + InspectorMessage( |
| 537 | + message=( |
| 538 | + "TimeSeries appears to have a constant sampling rate. " |
| 539 | + "Consider specifying starting_time=1.2 and rate=2.0 instead of timestamps." |
| 540 | + ), |
| 541 | + importance=Importance.BEST_PRACTICE_VIOLATION, |
| 542 | + check_function_name="check_regular_timestamps", |
| 543 | + object_type="TimeSeries", |
| 544 | + object_name="test_time_series_2", |
| 545 | + location="/acquisition/test_time_series_2", |
| 546 | + file_path=self.nwbfile_paths[0], |
| 547 | + ), |
| 548 | + InspectorMessage( |
| 549 | + message=( |
| 550 | + "Data may be in the wrong orientation. " |
| 551 | + "Time should be in the first dimension, and is usually the longest dimension. " |
| 552 | + "Here, another dimension is longer." |
| 553 | + ), |
| 554 | + importance=Importance.CRITICAL, |
| 555 | + check_function_name="check_data_orientation", |
| 556 | + object_type="SpatialSeries", |
| 557 | + object_name="my_spatial_series", |
| 558 | + location="/processing/behavior/Position/my_spatial_series", |
| 559 | + file_path=self.nwbfile_paths[0], |
| 560 | + ), |
| 561 | + InspectorMessage( |
| 562 | + message="The length of the first dimension of data does not match the length of timestamps.", |
| 563 | + importance=Importance.CRITICAL, |
| 564 | + check_function_name="check_timestamps_match_first_dimension", |
| 565 | + object_type="TimeSeries", |
| 566 | + object_name="test_time_series_3", |
| 567 | + location="/acquisition/test_time_series_3", |
| 568 | + file_path=self.nwbfile_paths[0], |
| 569 | + ), |
| 570 | + ] |
| 571 | + self.assertCountEqual(first=test_results, second=true_results) |
| 572 | + |
504 | 573 |
|
505 | 574 | @pytest.mark.skipif(not HAVE_ROS3 or not HAVE_DANDI, reason="Needs h5py setup with ROS3.") |
506 | 575 | def test_dandiset_streaming(): |
@@ -561,3 +630,74 @@ def test_dandiset_streaming_cli_parallel(self): |
561 | 630 | f"> {console_output_file}" |
562 | 631 | ) |
563 | 632 | self.assertFileExists(path=self.tempdir / "test_nwbinspector_streaming_report_7.txt") |
| 633 | + |
| 634 | + |
| 635 | +class TestCheckUniqueIdentifiersPass(TestCase): |
| 636 | + maxDiff = None |
| 637 | + |
| 638 | + @classmethod |
| 639 | + def setUpClass(cls): |
| 640 | + cls.tempdir = Path(mkdtemp()) |
| 641 | + num_nwbfiles = 3 |
| 642 | + unique_id_nwbfiles = list() |
| 643 | + for j in range(num_nwbfiles): |
| 644 | + unique_id_nwbfiles.append(make_minimal_nwbfile()) |
| 645 | + |
| 646 | + cls.unique_id_nwbfile_paths = [str(cls.tempdir / f"unique_id_testing{j}.nwb") for j in range(num_nwbfiles)] |
| 647 | + for nwbfile_path, nwbfile in zip(cls.unique_id_nwbfile_paths, unique_id_nwbfiles): |
| 648 | + with NWBHDF5IO(path=nwbfile_path, mode="w") as io: |
| 649 | + io.write(nwbfile) |
| 650 | + |
| 651 | + @classmethod |
| 652 | + def tearDownClass(cls): |
| 653 | + rmtree(cls.tempdir) |
| 654 | + |
| 655 | + def test_check_unique_identifiers_pass(self): |
| 656 | + assert list(inspect_all(path=self.tempdir, select=["check_data_orientation"])) == [] |
| 657 | + |
| 658 | + |
| 659 | +class TestCheckUniqueIdentifiersFail(TestCase): |
| 660 | + maxDiff = None |
| 661 | + |
| 662 | + @classmethod |
| 663 | + def setUpClass(cls): |
| 664 | + cls.tempdir = Path(mkdtemp()) |
| 665 | + num_nwbfiles = 3 |
| 666 | + non_unique_id_nwbfiles = list() |
| 667 | + for j in range(num_nwbfiles): |
| 668 | + non_unique_id_nwbfiles.append( |
| 669 | + NWBFile( |
| 670 | + session_description="", |
| 671 | + identifier="not a unique identifier!", |
| 672 | + session_start_time=datetime.now().astimezone(), |
| 673 | + ) |
| 674 | + ) |
| 675 | + |
| 676 | + cls.non_unique_id_nwbfile_paths = [ |
| 677 | + str(cls.tempdir / f"non_unique_id_testing{j}.nwb") for j in range(num_nwbfiles) |
| 678 | + ] |
| 679 | + for nwbfile_path, nwbfile in zip(cls.non_unique_id_nwbfile_paths, non_unique_id_nwbfiles): |
| 680 | + with NWBHDF5IO(path=nwbfile_path, mode="w") as io: |
| 681 | + io.write(nwbfile) |
| 682 | + |
| 683 | + @classmethod |
| 684 | + def tearDownClass(cls): |
| 685 | + rmtree(cls.tempdir) |
| 686 | + |
| 687 | + def test_check_unique_identifiers_fail(self): |
| 688 | + assert list(inspect_all(path=self.tempdir, select=["check_data_orientation"])) == [ |
| 689 | + InspectorMessage( |
| 690 | + message=( |
| 691 | + "The identifier 'not a unique identifier!' is used across the .nwb files: " |
| 692 | + f"{natsorted([Path(x).name for x in self.non_unique_id_nwbfile_paths])}. " |
| 693 | + "The identifier of any NWBFile should be a completely unique value - " |
| 694 | + "we recommend using uuid4 to achieve this." |
| 695 | + ), |
| 696 | + importance=Importance.CRITICAL, |
| 697 | + check_function_name="check_unique_identifiers", |
| 698 | + object_type="NWBFile", |
| 699 | + object_name="root", |
| 700 | + location="/", |
| 701 | + file_path=str(self.tempdir), |
| 702 | + ) |
| 703 | + ] |
0 commit comments