|
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, |
@@ -628,3 +630,74 @@ def test_dandiset_streaming_cli_parallel(self): |
628 | 630 | f"> {console_output_file}" |
629 | 631 | ) |
630 | 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