Skip to content

Commit

Permalink
Numpy 2.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Nov 8, 2024
1 parent 241d8bc commit 14fee7d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
23 changes: 14 additions & 9 deletions brainbox/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

def frame_diff(frame1, frame2):
"""
Outputs pythagorean distance between two frames
Outputs pythagorean distance between two frames.
:param frame1: A numpy array of pixels with a shape of either (m, n, 3) or (m, n)
:param frame2: A numpy array of pixels with a shape of either (m, n, 3) or (m, n)
:return: An array with a shape equal to the input frames
Expand All @@ -14,17 +15,21 @@ def frame_diff(frame1, frame2):
raise ValueError('Frames must have the same shape')
diff32 = np.float32(frame1) - np.float32(frame2)
if frame1.ndim == 3:
norm32 = (np.sqrt(diff32[:, :, 0] ** 2 + diff32[:, :, 1] ** 2 + diff32[:, :, 2] ** 2) /
np.sqrt(255 ** 2 * 3))
norm32 = np.float32(
np.sqrt(diff32[:, :, 0] ** 2 + diff32[:, :, 1] ** 2 + diff32[:, :, 2] ** 2) /
np.sqrt(255 ** 2 * 3)
)
else:
norm32 = np.sqrt(diff32 ** 2 * 3) / np.sqrt(255 ** 2 * 3)
return np.uint8(norm32 * 255)
norm32 = np.float32(np.sqrt(diff32 ** 2 * 3) / np.sqrt(255 ** 2 * 3))
return np.uint8(np.round(norm32 * 255))


def frame_diffs(frames, diff=1):
"""
Return the difference between frames. May also take difference between more than 1 frames.
Values are normalized between 0-255.
Return the difference between frames.
May also take difference between more than 1 frames. Values are normalized between 0-255.
:param frames: Array or list of frames, where each frame is either (y, x) or (y, x, 3).
:param diff: Take difference between frames N and frames N + diff.
:return: uint8 array with shape (n-diff, y, x).
Expand All @@ -35,9 +40,9 @@ def frame_diffs(frames, diff=1):
diff32 = frames[diff:] - frames[:-diff]
# Normalize
if frames.ndim == 4:
norm32 = np.sqrt((diff32 ** 2).sum(axis=3)) / np.sqrt(255 ** 2 * 3)
norm32 = np.sqrt((diff32 ** 2).sum(axis=3)) / np.sqrt(255 ** 2 * 3).astype(np.float32)
else:
norm32 = np.sqrt(diff32 ** 2 * 3) / np.sqrt(255 ** 2 * 3)
norm32 = np.sqrt(diff32 ** 2 * 3) / np.sqrt(255 ** 2 * 3).astype(np.float32)
return np.uint8(norm32 * 255)


Expand Down
10 changes: 5 additions & 5 deletions ibllib/io/extractors/ephys_passive.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,10 @@ def _extract_passiveAudio_intervals(audio: dict, rig_version: str) -> Tuple[np.a
noiseOff_times = soundOff_times[diff > 0.3]

# append with nans
toneOn_times = np.r_[toneOn_times, np.full((NTONES - len(toneOn_times)), np.NAN)]
toneOff_times = np.r_[toneOff_times, np.full((NTONES - len(toneOff_times)), np.NAN)]
noiseOn_times = np.r_[noiseOn_times, np.full((NNOISES - len(noiseOn_times)), np.NAN)]
noiseOff_times = np.r_[noiseOff_times, np.full((NNOISES - len(noiseOff_times)), np.NAN)]
toneOn_times = np.r_[toneOn_times, np.full((NTONES - len(toneOn_times)), np.nan)]
toneOff_times = np.r_[toneOff_times, np.full((NTONES - len(toneOff_times)), np.nan)]
noiseOn_times = np.r_[noiseOn_times, np.full((NNOISES - len(noiseOn_times)), np.nan)]
noiseOff_times = np.r_[noiseOff_times, np.full((NNOISES - len(noiseOff_times)), np.nan)]

else:
# Get all sound onsets and offsets
Expand Down Expand Up @@ -676,7 +676,7 @@ def _extract(self, sync_collection: str = 'raw_ephys_data', task_collection: str
else:
# If we don't have task replay then we set the treplay intervals to NaN in our passivePeriods_df dataset
passiveGabor_df, passiveStims_df = (None, None)
passivePeriods_df.taskReplay = np.NAN
passivePeriods_df.taskReplay = np.nan

if plot:
f, ax = plt.subplots(1, 1)
Expand Down

0 comments on commit 14fee7d

Please sign in to comment.