11import warnings
2+ from datetime import datetime
3+ from uuid import uuid4
4+ import os
5+ import numpy as np
6+
7+ from dateutil import tz
28
39from pynwb .resources import HERD
10+ from pynwb .file import Subject
11+ from pynwb import NWBHDF5IO , NWBFile
412from pynwb .testing import TestCase
513
614
715class TestNWBContainer (TestCase ):
16+ def setUp (self ):
17+ self .path = "resources_file.nwb"
18+ self .export_path = "export_file.nwb"
19+
20+ def tearDown (self ):
21+ for path in [self .path , self .export_path ]:
22+ if os .path .isfile (path ):
23+ os .remove (path )
24+
825 def test_constructor (self ):
926 """
1027 Test constructor
@@ -17,3 +34,178 @@ def test_constructor(self):
1734 )
1835 er = HERD ()
1936 self .assertIsInstance (er , HERD )
37+
38+ def test_nwbfile_init_herd (self ):
39+ session_start_time = datetime (2018 , 4 , 25 , 2 , 30 , 3 , tzinfo = tz .gettz ("US/Pacific" ))
40+ herd = HERD ()
41+ nwbfile = NWBFile (
42+ session_description = "A Person undergoing brain pokes." ,
43+ identifier = str (uuid4 ()),
44+ session_start_time = session_start_time ,
45+ external_resources = herd
46+ )
47+ self .assertTrue (isinstance (nwbfile .external_resources , HERD ))
48+
49+ def test_nwbfile_set_herd (self ):
50+ session_start_time = datetime (2018 , 4 , 25 , 2 , 30 , 3 , tzinfo = tz .gettz ("US/Pacific" ))
51+ herd = HERD ()
52+ nwbfile = NWBFile (
53+ session_description = "A Person undergoing brain pokes." ,
54+ identifier = str (uuid4 ()),
55+ session_start_time = session_start_time ,
56+ )
57+ nwbfile .external_resources = herd
58+ self .assertTrue (isinstance (nwbfile .external_resources , HERD ))
59+ self .assertEqual (nwbfile .external_resources .parent , nwbfile )
60+
61+ def test_resources_roundtrip (self ):
62+ session_start_time = datetime (2018 , 4 , 25 , 2 , 30 , 3 , tzinfo = tz .gettz ("US/Pacific" ))
63+
64+ nwbfile = NWBFile (
65+ session_description = "A Person undergoing brain pokes." ,
66+ identifier = str (uuid4 ()),
67+ session_start_time = session_start_time ,
68+ )
69+ subject = Subject (
70+ subject_id = "001" ,
71+ age = "26" ,
72+ description = "human 5" ,
73+ species = 'Homo sapiens' ,
74+ sex = "M" ,
75+ )
76+
77+ nwbfile .subject = subject
78+ herd = HERD ()
79+ nwbfile .external_resources = herd
80+
81+ nwbfile .external_resources .add_ref (container = nwbfile .subject ,
82+ key = nwbfile .subject .species ,
83+ entity_id = "NCBI_TAXON:9606" ,
84+ entity_uri = 'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606' )
85+
86+ with NWBHDF5IO (self .path , "w" ) as io :
87+ io .write (nwbfile )
88+
89+ with NWBHDF5IO (self .path , "r" ) as io :
90+ read_nwbfile = io .read ()
91+ self .assertEqual (
92+ read_nwbfile .external_resources .keys [:],
93+ np .array (
94+ [[(b'Homo sapiens' ,)]],
95+ dtype = [('key' , 'O' )]
96+ )
97+ )
98+
99+ self .assertEqual (
100+ read_nwbfile .external_resources .entities [:],
101+ np .array (
102+ [
103+ ('NCBI_TAXON:9606' ,
104+ 'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606' )
105+ ],
106+ dtype = [('entity_id' , 'O' ), ('entity_uri' , 'O' )]
107+ )
108+ )
109+
110+ self .assertEqual (
111+ read_nwbfile .external_resources .objects [:],
112+ np .array (
113+ [
114+ (0 ,
115+ subject .object_id ,
116+ 'Subject' ,
117+ '' ,
118+ '' )
119+ ],
120+ dtype = [
121+ ('files_idx' , '<u4' ),
122+ ('object_id' , 'O' ),
123+ ('object_type' , 'O' ),
124+ ('relative_path' , 'O' ),
125+ ('field' , 'O' )
126+ ]
127+ )
128+ )
129+
130+ def test_link_resources (self ):
131+ """
132+ Note: Make sure that the internal HERD is not overwritten on export.
133+ """
134+ session_start_time = datetime (2018 , 4 , 25 , 2 , 30 , 3 , tzinfo = tz .gettz ("US/Pacific" ))
135+
136+ nwbfile = NWBFile (
137+ session_description = "A Person undergoing brain pokes." ,
138+ identifier = str (uuid4 ()),
139+ session_start_time = session_start_time ,
140+ )
141+ subject = Subject (
142+ subject_id = "001" ,
143+ age = "26" ,
144+ description = "human 5" ,
145+ species = 'Homo sapiens' ,
146+ sex = "M" ,
147+ )
148+
149+ nwbfile .subject = subject
150+ herd = HERD ()
151+ nwbfile .external_resources = herd
152+
153+ nwbfile .external_resources .add_ref (container = nwbfile .subject ,
154+ key = nwbfile .subject .species ,
155+ entity_id = "NCBI_TAXON:9606" ,
156+ entity_uri = 'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606' )
157+
158+ with NWBHDF5IO (self .path , "w" ) as io :
159+ io .write (nwbfile )
160+
161+ with NWBHDF5IO (self .path , mode = 'r' ) as read_io :
162+ read_nwbfile = read_io .read ()
163+ read_nwbfile .link_resources (HERD ())
164+
165+ self .assertEqual (read_nwbfile .external_resources .keys .data , [])
166+ self .assertEqual (read_nwbfile .external_resources .entities .data , [])
167+ self .assertEqual (read_nwbfile .external_resources .objects .data , [])
168+
169+ with NWBHDF5IO (self .export_path , mode = 'w' ) as export_io :
170+ export_io .export (src_io = read_io , nwbfile = read_nwbfile )
171+
172+ with NWBHDF5IO (self .export_path , mode = 'r' ) as read_export_io :
173+ read_export_nwbfile = read_export_io .read ()
174+ self .assertEqual (
175+ read_export_nwbfile .external_resources .keys [:],
176+ np .array (
177+ [[(b'Homo sapiens' ,)]],
178+ dtype = [('key' , 'O' )]
179+ )
180+ )
181+
182+ self .assertEqual (
183+ read_export_nwbfile .external_resources .entities [:],
184+ np .array (
185+ [
186+ ('NCBI_TAXON:9606' ,
187+ 'https://www.ncbi.nlm.nih.gov/Taxonomy/Browser/wwwtax.cgi?mode=Info&id=9606' )
188+ ],
189+ dtype = [('entity_id' , 'O' ), ('entity_uri' , 'O' )]
190+ )
191+ )
192+
193+ self .assertEqual (
194+ read_export_nwbfile .external_resources .objects [:],
195+ np .array (
196+ [
197+ (0 ,
198+ subject .object_id ,
199+ 'Subject' ,
200+ '' ,
201+ '' )
202+ ],
203+ dtype = [
204+ ('files_idx' , '<u4' ),
205+ ('object_id' , 'O' ),
206+ ('object_type' , 'O' ),
207+ ('relative_path' , 'O' ),
208+ ('field' , 'O' )
209+ ]
210+ )
211+ )
0 commit comments