-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
# Convert arduino timestamps to corresponding photosmetry sample number | ||
video_timestamps = adjust_video_timestamps(video_timestamps, photometry_start_in_arduino_time) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
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
# Get pixelsPerCm based on the date of the data collected | ||
pixelsPerCm = assign_pixels_per_cm(metadata["date"]) |
There was a problem hiding this comment.
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
# 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
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. |
There was a problem hiding this comment.
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
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
pixelJumpCutoff = 30 * pixelsPerCm | ||
position.loc[position.x.notnull(),['x','y']] = detect_and_replace_jumps( | ||
position.loc[position.x.notnull(),['x','y']].values,pixelJumpCutoff) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
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 |
There was a problem hiding this comment.
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?
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 |
No description provided.