Skip to content

Commit 4eb1a6e

Browse files
committed
enforce same size spatial node arrays
1 parent f08d6af commit 4eb1a6e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/trodes_to_nwb/convert.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def create_nwbs(
109109
output_dir: str = "/stelmo/nwb/raw",
110110
video_directory: str = "",
111111
convert_video: bool = False,
112+
fs_gui_dir: str = "",
112113
n_workers: int = 1,
113114
query_expression: str | None = None,
114115
disable_ptp: bool = False,
@@ -170,6 +171,8 @@ def pass_func(args):
170171
output_dir,
171172
video_directory,
172173
convert_video,
174+
fs_gui_dir,
175+
disable_ptp,
173176
behavior_only=behavior_only,
174177
)
175178
return True
@@ -198,6 +201,7 @@ def pass_func(args):
198201
output_dir,
199202
video_directory,
200203
convert_video,
204+
fs_gui_dir,
201205
disable_ptp,
202206
behavior_only=behavior_only,
203207
)
@@ -211,6 +215,7 @@ def _create_nwb(
211215
output_dir: str = "/stelmo/nwb/raw",
212216
video_directory: str = "",
213217
convert_video: bool = False,
218+
fs_gui_dir: str = "",
214219
disable_ptp: bool = False,
215220
behavior_only: bool = False,
216221
):
@@ -278,7 +283,6 @@ def _create_nwb(
278283
nwb_file, metadata, session_df, video_directory, convert_video
279284
)
280285
add_optogenetics(nwb_file, metadata, device_metadata)
281-
add_optogenetic_epochs(nwb_file, metadata)
282286

283287
if not behavior_only:
284288
add_electrode_groups(
@@ -312,6 +316,7 @@ def _create_nwb(
312316
session_df=session_df,
313317
neo_io=rec_dci.neo_io,
314318
)
319+
add_optogenetic_epochs(nwb_file, metadata, fs_gui_dir)
315320
logger.info("ADDING POSITION")
316321
# add position
317322
if disable_ptp:

src/trodes_to_nwb/convert_optogenetics.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,14 +321,46 @@ def add_optogenetic_epochs(
321321
)
322322

323323
# loop through each fsgui script, which can apply to multiple epochs
324+
rows = []
324325
for fs_gui_metadata in opto_epochs_metadata:
325326
new_rows = compile_opto_entries(
326327
fs_gui_metadata=fs_gui_metadata,
327328
nwbfile=nwbfile,
328329
file_dir=file_dir,
329330
)
330-
for row in new_rows:
331-
opto_epochs_table.add_row(**row)
331+
rows.extend(new_rows)
332+
333+
# ensure spatial geometry nodes have same shape in each row
334+
max_shape = [0, 0, 0]
335+
for row in rows:
336+
nodes = row.get("spatial_filter_region_node_coordinates_in_pixels", None)
337+
if nodes is not None:
338+
max_shape = [
339+
max(max_shape[0], nodes.shape[0]),
340+
max(max_shape[1], nodes.shape[1]),
341+
max(max_shape[2], nodes.shape[2]),
342+
]
343+
if max_shape != [0, 0, 0]:
344+
for row in rows:
345+
nodes = row.get("spatial_filter_region_node_coordinates_in_pixels", None)
346+
if nodes is None:
347+
row["spatial_filter_region_node_coordinates_in_pixels"] = (
348+
np.ones(max_shape) * np.nan
349+
)
350+
else:
351+
nodes = np.pad(
352+
nodes,
353+
(
354+
(0, max_shape[0] - nodes.shape[0]),
355+
(0, max_shape[1] - nodes.shape[1]),
356+
(0, max_shape[2] - nodes.shape[2]),
357+
),
358+
mode="empty",
359+
)
360+
row["spatial_filter_region_node_coordinates_in_pixels"] = nodes
361+
362+
for row in rows:
363+
opto_epochs_table.add_row(**row)
332364

333365
nwbfile.add_time_intervals(opto_epochs_table)
334366

0 commit comments

Comments
 (0)