Skip to content

Commit

Permalink
DEV: filter ratings that are within 1/3 of the range
Browse files Browse the repository at this point in the history
  • Loading branch information
jungheejung committed Mar 9, 2024
1 parent a00b5d5 commit 027cad7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
29 changes: 27 additions & 2 deletions spacetop_prep/events/bidsify_cue_cognitive.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):

return angles_deg

def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):
# Calculate distance from the origin (xcenter, ycenter)
distance = np.sqrt((df[x_col] - xcenter)**2 + (ycenter - df[y_col])**2)

# Initialize angles array with NaN or another default value for points too close to the origin
angles_deg = np.full(df.shape[0], np.nan)

# Only calculate angles for points more than 80 units away from the origin
mask = distance > 80
angles = np.arctan2((ycenter - df.loc[mask, y_col]), (df.loc[mask, x_col] - xcenter))

# Adjust the angle so it's between 0 and π radians
angles = np.pi - angles

# Convert angles to degrees and ensure they are positive
angles_deg[mask] = np.abs(np.degrees(angles))

# Ensure all angles fall within the 0 to 180 range
angles_deg = angles_deg % 360
angles_deg[angles_deg > 180] = 360 - angles_deg[angles_deg > 180]

return angles_deg


bids_dir = '/Users/h/Documents/projects_local/1076_spacetop' # the top directory of datalad
code_dir = '/Users/h/Documents/projects_local/1076_spacetop/code' # where this code live
source_dir = '/Users/h/Documents/projects_local/1076_spacetop/sourcedata'# where the source behavioral directory lives
Expand Down Expand Up @@ -122,6 +146,7 @@ def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):

except FileNotFoundError as e:
logger.warning(str(e))
logger.warning(f"_______ {sub_bids} {ses_bids} {run_bids} {runtype} _______Trajectory preproc is empty")
continue
except Exception as e:
# This catches any other exceptions that might occur
Expand All @@ -142,9 +167,9 @@ def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):
# traj_df['adjusted_outcomeangle_degrees'] = traj_df['outcomeangle_degrees'] % 180

# 3-2. Calculate the angle in radians and then convert to degrees
traj_df['expectangle_degrees'] = calc_adjusted_angle_df(
traj_df['adjusted_expectangle_degrees'] = calc_adjusted_angle_df(
traj_df, 'expectrating_end_x', 'expectrating_end_y', trajectory_x, trajectory_y)
traj_df['outcomeangle_degrees'] = calc_adjusted_angle_df(
traj_df['adjusted_outcomeangle_degrees'] = calc_adjusted_angle_df(
traj_df, 'outcomerating_end_x', 'outcomerating_end_y', trajectory_x, trajectory_y)


Expand Down
29 changes: 27 additions & 2 deletions spacetop_prep/events/bidsify_cue_vicarious.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):

return angles_deg

def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):
# Calculate distance from the origin (xcenter, ycenter)
distance = np.sqrt((df[x_col] - xcenter)**2 + (ycenter - df[y_col])**2)

# Initialize angles array with NaN or another default value for points too close to the origin
angles_deg = np.full(df.shape[0], np.nan)

# Only calculate angles for points more than 80 units away from the origin
mask = distance > 80
angles = np.arctan2((ycenter - df.loc[mask, y_col]), (df.loc[mask, x_col] - xcenter))

# Adjust the angle so it's between 0 and π radians
angles = np.pi - angles

# Convert angles to degrees and ensure they are positive
angles_deg[mask] = np.abs(np.degrees(angles))

# Ensure all angles fall within the 0 to 180 range
angles_deg = angles_deg % 360
angles_deg[angles_deg > 180] = 360 - angles_deg[angles_deg > 180]

return angles_deg


bids_dir = '/Users/h/Documents/projects_local/1076_spacetop' # the top directory of datalad
code_dir = '/Users/h/Documents/projects_local/1076_spacetop/code' # where this code live
source_dir = '/Users/h/Documents/projects_local/1076_spacetop/sourcedata'# where the source behavioral directory lives
Expand Down Expand Up @@ -121,6 +145,7 @@ def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):

except FileNotFoundError as e:
logger.warning(str(e))
logger.warning(f"{sub_bids}_{ses_bids}_task-cue_{run_bids} Trajectory preproc is empty")
continue
except Exception as e:
# This catches any other exceptions that might occur
Expand All @@ -142,9 +167,9 @@ def calc_adjusted_angle_df(df, x_col, y_col, xcenter, ycenter):

# traj_df['adjusted_expectangle_degrees'] = traj_df['expectangle_degrees'] % 180
# traj_df['adjusted_outcomeangle_degrees'] = traj_df['outcomeangle_degrees'] % 180
traj_df['expectangle_degrees'] = calc_adjusted_angle_df(
traj_df['adjusted_expectangle_degrees'] = calc_adjusted_angle_df(
traj_df, 'expectrating_end_x', 'expectrating_end_y', trajectory_x, trajectory_y)
traj_df['outcomeangle_degrees'] = calc_adjusted_angle_df(
traj_df['adjusted_outcomeangle_degrees'] = calc_adjusted_angle_df(
traj_df, 'outcomerating_end_x', 'outcomerating_end_y', trajectory_x, trajectory_y)


Expand Down

0 comments on commit 027cad7

Please sign in to comment.