Skip to content

Commit c99c0c5

Browse files
authored
Add comments to io/file.py (#1925)
* Add comments to io/file.py * Update changelog
1 parent 3c5fa48 commit c99c0c5

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Documentation and tutorial enhancements
66
- Simplified the introduction to NWB tutorial. @rly [#1914](https://github.com/NeurodataWithoutBorders/pynwb/pull/1914)
77
- Simplified the ecephys and ophys tutorials. [#1915](https://github.com/NeurodataWithoutBorders/pynwb/pull/1915)
8+
- Add comments to `src/pynwb/io/file.py` to improve developer documentation. @rly [#1925](https://github.com/NeurodataWithoutBorders/pynwb/pull/1925)
89

910
### Bug fixes
1011
- Fixed use of `channel_conversion` in `TimeSeries` `get_data_in_units`. @rohanshah [1923](https://github.com/NeurodataWithoutBorders/pynwb/pull/1923)

src/pynwb/io/file.py

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ def __init__(self, spec):
127127

128128
@ObjectMapper.object_attr('scratch_datas')
129129
def scratch_datas(self, container, manager):
130+
"""Set the value for the 'scratch_datas' spec on NWBFile to a list of ScratchData objects.
131+
132+
Used when writing (building) the NWBFile container to a file.
133+
134+
The 'scratch' group can contain both groups and datasets. This mapping function
135+
is used when writing the value for the 'scratch_datas' spec (ScratchData type
136+
-- see __init__ above). The value is set to a list of all ScratchData
137+
objects in the 'scratch' field of the NWBFile container.
138+
"""
130139
scratch = container.scratch
131140
ret = list()
132141
for s in scratch.values():
@@ -136,6 +145,15 @@ def scratch_datas(self, container, manager):
136145

137146
@ObjectMapper.object_attr('scratch_containers')
138147
def scratch_containers(self, container, manager):
148+
"""Set the value for the 'scratch_containers' spec on NWBFile to a list of non-ScratchData objects.
149+
150+
Used when writing (building) the NWBFile container to a file.
151+
152+
The 'scratch' group can contain both groups and datasets. This mapping function
153+
is used when writing the value for the 'scratch_containers' spec (NWBContainers
154+
and DynamicTable type -- see __init__ above). The value is set to a list of all non-ScratchData
155+
objects in the 'scratch' field of the NWBFile container.
156+
"""
139157
scratch = container.scratch
140158
ret = list()
141159
for s in scratch.values():
@@ -145,6 +163,14 @@ def scratch_containers(self, container, manager):
145163

146164
@ObjectMapper.constructor_arg('scratch')
147165
def scratch(self, builder, manager):
166+
"""Set the constructor arg for 'scratch' to a tuple of objects.
167+
168+
Used when constructing the NWBFile container from a written file.
169+
170+
The 'scratch' group can contain both groups and datasets. This mapping function
171+
is used to construct the contained groups and datasets and put them into a single
172+
field 'scratch' on the NWBFile container for user convenience.
173+
"""
148174
scratch = builder.get('scratch')
149175
ret = list()
150176
if scratch is not None:
@@ -156,28 +182,54 @@ def scratch(self, builder, manager):
156182

157183
@ObjectMapper.constructor_arg('session_start_time')
158184
def dateconversion(self, builder, manager):
185+
"""Set the constructor arg for 'session_start_time' to a datetime object.
186+
187+
Used when constructing the NWBFile container from a written file.
188+
189+
Dates are read into builders as strings and are parsed into datetime objects
190+
for user convenience and consistency with how they are written.
191+
"""
159192
datestr = builder.get('session_start_time').data
160193
date = dateutil_parse(datestr)
161194
return date
162195

163196
@ObjectMapper.constructor_arg('timestamps_reference_time')
164197
def dateconversion_trt(self, builder, manager):
198+
"""Set the constructor arg for 'timestamps_reference_time' to a datetime object.
199+
200+
Used when constructing the NWBFile container from a written file.
201+
202+
Dates are read into builders as strings and are parsed into datetime objects
203+
for user convenience and consistency with how they are written.
204+
"""
165205
datestr = builder.get('timestamps_reference_time').data
166206
date = dateutil_parse(datestr)
167207
return date
168208

169209
@ObjectMapper.constructor_arg('file_create_date')
170210
def dateconversion_list(self, builder, manager):
211+
"""Set the constructor arg for 'file_create_date' to a datetime object.
212+
213+
Used when constructing the NWBFile container from a written file.
214+
215+
Dates are read into builders as strings and are parsed into datetime objects
216+
for user convenience and consistency with how they are written.
217+
"""
171218
datestr = builder.get('file_create_date').data
172219
dates = list(map(dateutil_parse, datestr))
173220
return dates
174221

175-
@ObjectMapper.constructor_arg('file_name')
176-
def name(self, builder, manager):
177-
return builder.name
178-
179222
@ObjectMapper.constructor_arg('experimenter')
180223
def experimenter_carg(self, builder, manager):
224+
"""Set the constructor arg for 'experimenter' to a tuple if the builder value is a string.
225+
226+
Used when constructing the NWBFile container from a written file.
227+
228+
In early versions of the NWB 2 schema, 'experimenter' was specified as a string.
229+
Then it was changed to be a 1-D array of strings. This mapping function is necessary
230+
to allow reading of both data where 'experimenter' was specified as a string and data
231+
where 'experimenter' was specified as an array.
232+
"""
181233
ret = None
182234
exp_bldr = builder['general'].get('experimenter')
183235
if exp_bldr is not None:
@@ -189,13 +241,30 @@ def experimenter_carg(self, builder, manager):
189241

190242
@ObjectMapper.object_attr('experimenter')
191243
def experimenter_obj_attr(self, container, manager):
244+
"""Change the value for the field 'experimenter' on NWBFile to a tuple if it is a string.
245+
246+
Used when writing (building) the NWBFile container to a file.
247+
248+
In early versions of the NWB 2 schema, 'experimenter' was specified as a string.
249+
Then it was changed to be a 1-D array of strings. This mapping function is necessary
250+
for writing a valid 'experimenter' array if it is a string in the NWBFile container.
251+
"""
192252
ret = None
193253
if isinstance(container.experimenter, str):
194254
ret = (container.experimenter,)
195255
return ret
196256

197257
@ObjectMapper.constructor_arg('related_publications')
198258
def publications_carg(self, builder, manager):
259+
"""Set the constructor arg for 'related_publications' to a tuple if the builder value is a string.
260+
261+
Used when constructing the NWBFile container from a written file.
262+
263+
In early versions of the NWB 2 schema, 'related_publications' was specified as a string.
264+
Then it was changed to be a 1-D array of strings. This mapping function is necessary
265+
to allow reading of both data where 'related_publications' was specified as a string and data
266+
where 'related_publications' was specified as an array.
267+
"""
199268
ret = None
200269
pubs_bldr = builder['general'].get('related_publications')
201270
if pubs_bldr is not None:
@@ -207,6 +276,14 @@ def publications_carg(self, builder, manager):
207276

208277
@ObjectMapper.object_attr('related_publications')
209278
def publication_obj_attr(self, container, manager):
279+
"""Change the value for the field 'related_publications' on NWBFile to a tuple if it is a string.
280+
281+
Used when writing (building) the NWBFile container to a file.
282+
283+
In early versions of the NWB 2 schema, 'related_publications' was specified as a string.
284+
Then it was changed to be a 1-D array of strings. This mapping function is necessary
285+
for writing a valid 'related_publications' array if it is a string in the NWBFile container.
286+
"""
210287
ret = None
211288
if isinstance(container.related_publications, str):
212289
ret = (container.related_publications,)
@@ -218,6 +295,13 @@ class SubjectMap(ObjectMapper):
218295

219296
@ObjectMapper.constructor_arg('date_of_birth')
220297
def dateconversion(self, builder, manager):
298+
"""Set the constructor arg for 'date_of_birth' to a datetime object.
299+
300+
Used when constructing the Subject container from a written file.
301+
302+
Dates are read into builders as strings and are parsed into datetime objects
303+
for user convenience and consistency with how they are written.
304+
"""
221305
dob_builder = builder.get('date_of_birth')
222306
if dob_builder is None:
223307
return
@@ -228,6 +312,18 @@ def dateconversion(self, builder, manager):
228312

229313
@ObjectMapper.constructor_arg("age__reference")
230314
def age_reference_none(self, builder, manager):
315+
"""Set the constructor arg for 'age__reference' to "unspecified" for NWB files < 2.6, else "birth".
316+
317+
Used when constructing the Subject container from a written file.
318+
319+
NWB schema 2.6.0 introduced a new optional attribute 'reference' on the 'age' dataset with a default
320+
value of "birth". When data written with NWB versions < 2.6 are read, 'age__reference' is set to
321+
"unspecified" in the Subject constructor. "unspecified" is a special non-None placeholder value
322+
that is handled specially in Subject.__init__ to distinguish it from no value being provided by the
323+
user. When data written with NWB versions >= 2.6 are read, 'age__reference' is set to the default
324+
value, "birth", in the Subject constructor (this is not strictly necessary because Subject.__init__
325+
has default value "birth" for 'age__reference').
326+
"""
231327
age_builder = builder.get("age")
232328
age_reference = None
233329
if age_builder is not None:

0 commit comments

Comments
 (0)