Skip to content

Commit 016bee9

Browse files
authored
Merge pull request #105 from LorenFrankLab/add_pos_post
Add pos post
2 parents 8db322d + c3bc00c commit 016bee9

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/trodes_to_nwb/convert_position.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,18 @@ def add_position(
785785

786786
epoch_to_camera_ids = pd.concat(df).set_index("epoch").sort_index()
787787

788-
position = Position(name="position")
789-
790788
# Make a processing module for behavior and add to the nwbfile
791789
if not "behavior" in nwb_file.processing:
792790
nwb_file.create_processing_module(
793791
name="behavior", description="Contains all behavior-related data"
794792
)
793+
794+
if "position" not in nwb_file.processing["behavior"].data_interfaces:
795+
position = Position(name="position")
796+
nwb_file.processing["behavior"].add(position)
797+
else:
798+
position = nwb_file.processing["behavior"]["position"]
799+
795800
# get epoch data to seperate dio timestamps into epochs
796801
if (not ptp_enabled) and (not len(nwb_file.epochs)):
797802
raise ValueError(
@@ -903,8 +908,6 @@ def add_position(
903908
)
904909
)
905910

906-
nwb_file.processing["behavior"].add(position)
907-
908911

909912
def convert_h264_to_mp4(file: str, video_directory: str) -> str:
910913
"""

src/trodes_to_nwb/tests/test_convert_position.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pandas as pd
66
import pytest
77
from pynwb import NWBHDF5IO, TimeSeries
8+
from pynwb.behavior import BehavioralEvents, Position
89

910
from trodes_to_nwb import convert, convert_rec_header, convert_yaml
1011
from trodes_to_nwb.convert_dios import add_dios
@@ -207,7 +208,7 @@ def test_correct_timestamps_for_camera_to_mcu_lag():
207208
np.allclose(corrected_camera_systime, expected_corrected_camera_systime)
208209

209210

210-
def test_add_position():
211+
def test_add_position(prior_position=False):
211212
probe_metadata = [data_path / "tetrode_12.5.yml"]
212213

213214
# make session_df
@@ -226,8 +227,17 @@ def test_add_position():
226227
# make nwb file
227228
nwbfile = convert_yaml.initialize_nwb(metadata, rec_header)
228229

230+
# Optional test: add position data to nwbfile before running add_position
231+
if prior_position:
232+
nwbfile.create_processing_module(
233+
name="behavior", description="Contains all behavior-related data"
234+
)
235+
position = Position(name="position")
236+
nwbfile.processing["behavior"].add(position)
237+
229238
# run add_position and prerequisite functions
230239
convert_yaml.add_cameras(nwbfile, metadata)
240+
231241
add_position(nwbfile, metadata, session_df, rec_header)
232242

233243
# Check that the objects were properly added
@@ -294,6 +304,10 @@ def test_add_position():
294304
os.remove(filename)
295305

296306

307+
def test_add_position_preexisting():
308+
test_add_position(prior_position=True)
309+
310+
297311
from trodes_to_nwb.convert_position import read_trodes_datafile
298312

299313

0 commit comments

Comments
 (0)