Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yang-Sun with adding video and dlc! #15

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Yang-Sun with adding video and dlc! #15

wants to merge 7 commits into from

Conversation

yangsunhwang
Copy link
Collaborator

No description provided.

Copy link
Owner

@calderast calderast left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor changes - mostly renaming things to use snake case (lowercase separated by underscores) for stylistic consistency. I think we can probably remove adjustment of video timestamps and just do that in the alignment code after this.

TBH after doing a bunch of suggested changes the file got hard to read and then I didn't run it to test it bc I had already made the changes. Maybe accept the changes you think are good and we will do another review pass so it is less confusing with little suggestions everywhere

Comment on lines +23 to +24
# Convert arduino timestamps to corresponding photosmetry sample number
video_timestamps = adjust_video_timestamps(video_timestamps, photometry_start_in_arduino_time)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Convert arduino timestamps to corresponding photosmetry sample number
video_timestamps = adjust_video_timestamps(video_timestamps, photometry_start_in_arduino_time)
# Adjust all arduino timestamps so the photometry starts at time zero
video_timestamps = np.subtract(video_timestamps, photometry_start_in_arduino_time)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove the adjust_video_timestamps function and do the subtraction directly because it's just one line

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that I reread this code I don't think we even need to do this adjustment here - we use camera FPS for velocity/acceleration etc and all we do with video timestamps is return them, so we can do this adjustment later in the combined alignment code. IMO we should just return raw video timestamps?

Comment on lines +153 to +159
def adjust_video_timestamps(video_timestamps: list, photometry_start_in_arduino_time: float):
"""Convert video timestamps to corresponding sample number."""
# Adjust all arduino timestamps so the photometry starts at time zero
video_timestamps = np.subtract(video_timestamps, photometry_start_in_arduino_time)

return video_timestamps

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def adjust_video_timestamps(video_timestamps: list, photometry_start_in_arduino_time: float):
"""Convert video timestamps to corresponding sample number."""
# Adjust all arduino timestamps so the photometry starts at time zero
video_timestamps = np.subtract(video_timestamps, photometry_start_in_arduino_time)
return video_timestamps

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove this bc we replaced it with the single line above

Comment on lines +16 to +17
# Get pixelsPerCm based on the date of the data collected
pixelsPerCm = assign_pixels_per_cm(metadata["date"])
Copy link
Owner

@calderast calderast Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add option to assign pixels_per_cm in metadata for flexibility in case the maze was moved that day or something. Also make it all uppercase to match convention for constants

Suggested change
# Get pixelsPerCm based on the date of the data collected
pixelsPerCm = assign_pixels_per_cm(metadata["date"])
# If pixels_per_cm exists in metadata, use that value
if "pixels_per_cm" in metadata["video"]:
PIXELS_PER_CM = metadata["video"]["pixels_per_cm"]
# Otherwise, assign it based on the date of the experiment
else:
PIXELS_PER_CM = assign_pixels_per_cm(metadata["date"])

video_timestamps = adjust_video_timestamps(video_timestamps, photometry_start_in_arduino_time)

# Read x and y position data and calculate velocity and acceleration
x, y, velocity, acceleration = read_dlc(deeplabcut_file_path, phot_dlc = phot_dlc, cutoff = 0.9, cam_fps = 15, pixelsPerCm)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x, y, velocity, acceleration = read_dlc(deeplabcut_file_path, phot_dlc = phot_dlc, cutoff = 0.9, cam_fps = 15, pixelsPerCm)
x, y, velocity, acceleration = read_dlc(deeplabcut_file_path, phot_dlc = phot_dlc, cutoff = 0.9, cam_fps = 15, pixels_per_cm = PIXELS_PER_CM)

Comment on lines +33 to +39
Assigns pixelsPerCm based on the provided date string in mmddyyyy format.
- date_str (str): Date string in 'mmddyyyy' format, e.g., '11122022'.

3.14 if video is before IM-1594(before 01012023). 2.3 before (01112024). 2.688 after (old maze)

Returns:
- float: The corresponding pixelsPerCm value.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make PIXELS_PER_CM uppercase in function documentation

Suggested change
Assigns pixelsPerCm based on the provided date string in mmddyyyy format.
- date_str (str): Date string in 'mmddyyyy' format, e.g., '11122022'.
3.14 if video is before IM-1594(before 01012023). 2.3 before (01112024). 2.688 after (old maze)
Returns:
- float: The corresponding pixelsPerCm value.
Assigns constant PIXELS_PER_CM based on the provided date string in MMDDYYYY format.
PIXELS_PER_CM is 3.14 if video is before IM-1594 (before 01012023), 2.3 before (01112024), or 2.688 after (old maze)
Args:
- date_str (str): Date string in MMDDYYYY format, e.g., '11122022'.
Returns:
- float: The corresponding PIXELS_PER_CM value.

Position data is under the column names: cap_back and cap_front.
Cap_bak is the back of the rat implant (red), and cap_front is the front of the rat implant (green)

After reading the position data, the position data is used to calculate velocity and acceleration based on the camera fps and pixelsPerCm
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
After reading the position data, the position data is used to calculate velocity and acceleration based on the camera fps and pixelsPerCm
After reading the position data, the position data is used to calculate velocity and acceleration based on the camera fps and pixels_per_cm

Comment on lines +89 to +91
pixelJumpCutoff = 30 * pixelsPerCm
position.loc[position.x.notnull(),['x','y']] = detect_and_replace_jumps(
position.loc[position.x.notnull(),['x','y']].values,pixelJumpCutoff)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pixelJumpCutoff = 30 * pixelsPerCm
position.loc[position.x.notnull(),['x','y']] = detect_and_replace_jumps(
position.loc[position.x.notnull(),['x','y']].values,pixelJumpCutoff)
pixel_jump_cutoff = 30 * pixels_per_cm
position.loc[position.x.notnull(),['x','y']] = detect_and_replace_jumps(
position.loc[position.x.notnull(),['x','y']].values,pixel_jump_cutoff)

position.loc[:,['x','y']] = fill_missing_gaps(position.loc[:,['x','y']].values)

# Calculate velocity and acceleration
velocity, acceleration = calculate_velocity_acceleration(position['x'].values, position['y'].values,fps = cam_fps, pixel_to_cm = pixelsPerCm)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
velocity, acceleration = calculate_velocity_acceleration(position['x'].values, position['y'].values,fps = cam_fps, pixel_to_cm = pixelsPerCm)
velocity, acceleration = calculate_velocity_acceleration(position['x'].values, position['y'].values,fps = cam_fps, pixels_per_cm = pixels_per_cm)

Comment on lines +102 to +114
def detect_and_replace_jumps(coordinates, pixelJumpCutoff):
"""
Detect and replace jumps in the position data that are bigger than pixelJumpCutoff (default30 cm)
Jumps are replaced with NaN
"""
n = len(coordinates)
jumps = []

# Calculate Euclidean distances between consecutive points
distances = np.linalg.norm(coordinates[1:] - coordinates[:-1], axis=1)

# Find positions where the distance exceeds the threshold: pixelJumpCutoff
jump_indices = np.where(distances > pixelJumpCutoff)[0] + 1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def detect_and_replace_jumps(coordinates, pixelJumpCutoff):
"""
Detect and replace jumps in the position data that are bigger than pixelJumpCutoff (default30 cm)
Jumps are replaced with NaN
"""
n = len(coordinates)
jumps = []
# Calculate Euclidean distances between consecutive points
distances = np.linalg.norm(coordinates[1:] - coordinates[:-1], axis=1)
# Find positions where the distance exceeds the threshold: pixelJumpCutoff
jump_indices = np.where(distances > pixelJumpCutoff)[0] + 1
def detect_and_replace_jumps(coordinates, pixel_jump_cutoff):
"""
Detect and replace jumps in the position data that are bigger than pixel_jump_cutoff (default 30 cm)
Jumps are replaced with NaN
"""
n = len(coordinates)
jumps = []
# Calculate Euclidean distances between consecutive points
distances = np.linalg.norm(coordinates[1:] - coordinates[:-1], axis=1)
# Find positions where the distance exceeds the threshold pixel_jump_cutoff
jump_indices = np.where(distances > pixel_jump_cutoff)[0] + 1

Comment on lines +160 to +166
def calculate_velocity_acceleration(x, y, fps, pixel_to_cm=1):
"""
Calculate velocity and acceleration based on the camera fps and pixelsPerCm
"""
# convert pixel to cm
x_cm = x * pixel_to_cm
y_cm = y * pixel_to_cm
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit confused about why this was pixel_to_cm - I don't think that should be different from pixels_per_cm? I changed it to be same bc thats how we use it. Also removed the default because I don't think default of 1 makes sense?

Suggested change
def calculate_velocity_acceleration(x, y, fps, pixel_to_cm=1):
"""
Calculate velocity and acceleration based on the camera fps and pixelsPerCm
"""
# convert pixel to cm
x_cm = x * pixel_to_cm
y_cm = y * pixel_to_cm
def calculate_velocity_acceleration(x, y, fps, pixels_per_cm):
"""
Calculate velocity and acceleration based on the camera fps and pixels_per_cm
"""
# convert pixel to cm
x_cm = x * pixels_per_cm
y_cm = y * pixels_per_cm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants