Skip to content

Commit 522cebb

Browse files
committed
create intervalset from full dataframe with metadata when loading in an nwb file to ensure it is aligned correctly. also try an initial sort of an input dataframe to preserve metadata
1 parent 3f59ca9 commit 522cebb

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

pynapple/core/interval_set.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ def __init__(
197197
), """
198198
DataFrame must contain columns name "start" and "end" for start and end times.
199199
"""
200+
# try sorting the DataFrame by start times, preserving its end pair, as an effort to preserve metadata
201+
# since metadata would be dropped if starts and ends are sorted separately
202+
# note that if end times are still not sorted, metadata will be dropped
203+
if np.any(start["start"].diff() < 0):
204+
start = start.sort_values("start").reset_index(drop=True)
205+
200206
metadata = start.drop(columns=["start", "end"])
201207
end = start["end"].values.astype(np.float64)
202208
start = start["start"].values.astype(np.float64)

pynapple/io/interface_nwb.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ def _make_interval_set(obj, **kwargs):
8585
df = obj.to_dataframe()
8686

8787
if hasattr(df, "start_time") and hasattr(df, "stop_time"):
88-
data = nap.IntervalSet(start=df["start_time"], end=df["stop_time"])
89-
if df.shape[1] > 2:
90-
metadata = df.drop(columns=["start_time", "stop_time"])
91-
data.set_info(metadata)
88+
df = df.rename(columns={"start_time": "start", "stop_time": "end"})
89+
# create from full dataframe to ensure that metadata is associated correctly
90+
data = nap.IntervalSet(df)
9291
return data
9392

9493
else:

0 commit comments

Comments
 (0)