Skip to content

Commit 8a8abd6

Browse files
Update README and velocity_calculation.py to enhance parameter definitions and add optional exclusions for body parts in velocity plots
1 parent 57cf9d1 commit 8a8abd6

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ Before running `velocity_calculation.py`, define the following parameters at the
3838
| `pixel_size` | `float` | Physical size of one pixel, in mm. Use `1` if physical calibration is not required. <br> *Example:* `0.05` for 50 µm/pixel |
3939
| `spatial_unit` | `str` | Unit for the spatial scale of velocity. Only used for labeling plots. <br> *Example:* `"mm"` |
4040
| `likelihood_threshold`| `float` | Minimum DeepLabCut likelihood score to consider a detection as valid. Values range from `0` to `1`. <br> *Example:* `0.9` |
41-
| `movement_threshold` | `float` | Velocity threshold (in px/s or mm/s) to classify a body part as moving. <br> *Example:* `50` |
41+
| `movement_threshold` | `float` | Velocity threshold (in px/s or mm/s) to classify a body part as moving. <br> *Example:* `50` |
42+
| `ylim` | `float` or `None` | Sets the y-axis limit of the velocity plot. <br>`None` for automatic scaling; numeric (e.g., `1000`) for fixed scaling. |
43+
| `bodypart_not_to_plot` | `list[str]` or `None` | List of body parts to exclude from velocity plots. Set to `None` to include all. <br>Example: `['center', 'tail']` |
4244
| `bodypart_groups` | `dict` or `None` | Optional dictionary grouping body parts into named groups for group-wise velocity and movement analysis. <br> *Example:* `{'head': ['nose', 'neck'], 'rear': ['tail_base']}` |
4345
| `time_intervals` | `dict` or `None` | Optional dictionary defining named time intervals (in frames) for separate analysis over subsegments. <br> *Example:* `{'baseline': [0, 2499], 'stimulus': [2500, 4999]}` |
4446

47+
4548
**Notes**:
4649
* Leave `bodypart_groups` and `time_intervals` as `None` if you do not want to use grouping or interval-based analysis. When enabled, the script will compute separate statistics per group and/or per interval and save them in distinct output files.
4750
* `bodypart_groups` is useful for analyzing the velocity of specific body parts, such as the head or tail, and can be used to compare their movement patterns.

velocity_calculation.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
# %% DEFINE PATH AND PARAMETERS (ADJUST HERE)
2424

2525
# set your data and results paths here:
26-
DATA_PATH = "./input_data/"
27-
RESULTS_PATH = "./results/"
26+
DATA_PATH = "/Users/husker/Workspace/Emine/DLC cFC/Data Test/"
27+
RESULTS_PATH = "/Users/husker/Workspace/Emine/DLC cFC/results/"
2828

2929
# define frame rate and time step:
3030
frame_rate = 30 # fps
@@ -42,21 +42,40 @@
4242
# threshold, you can filter out low-confidence points.
4343

4444
# define a threshold for movement detection:
45-
movement_threshold = 100 # px/frame; note, if you set pixel_size to 1, this is in px/s;
45+
movement_threshold = 500 # px/frame; note, if you set pixel_size to 1, this is in px/s;
4646
# if you set pixel_size to a value other than 1, this is in spatial_unit/s;
4747
# this threshold is used to determine whether a body part is moving or not;
4848
# if the velocity is above this threshold, the body part is considered to be moving;
4949
# if the velocity is below this threshold, the body part is considered to be not moving;
5050

51+
# set (optional!) y-axis limit for velocity plot:
52+
ylim = None # DON'T CHANGE THIS LINE
53+
#
54+
# uncomment if you want to set a fixed y-axis limit for the velocity plot:
55+
#
56+
# ylim = 1000 # set to a value, e.g., 1000, for fixed scaling;
57+
#
58+
# note: this is useful if you want to compare the velocity plots of different files;
59+
# if you set ylim to None, the y-axis limit will be automatically scaled to the data;
60+
61+
62+
# define bodyparts to be excluded from the velocity plot:
63+
bodypart_not_to_plot = None # DO NOT CHANGE THIS LINE
64+
#
65+
# uncomment if you want to exclude some body parts from the velocity plot:
66+
#
67+
# bodypart_not_to_plot = ['ear_L', 'ear_R'] # set to a list of body parts to be excluded from the velocity plot;
68+
69+
5170
# define bodypart-groups:
5271
# initialize bodypart_groups as None if not defined:
5372
bodypart_groups = None # DON'T CHANGE THIS LINE
5473
#
55-
# uncomment the following if you want to group body parts together:
74+
# uncomment if you want to group body parts together:
5675
#
5776
# bodypart_groups = {
58-
# 'group1': ['bodypart1', 'bodypart2'],
59-
# 'group2': ['bodypart3']}
77+
# 'head': ['nose', 'ear_L', 'ear_R', 'neck'],
78+
# 'body': ['center']}
6079
#
6180
# grouping body parts together can be useful if you want to assess
6281
# moving/non moving only for a subset of body parts, e.g., for all
@@ -68,10 +87,11 @@
6887
#
6988
# uncomment if you want to separate the analysis into time intervals:
7089
#
71-
# time_intervals = {
72-
# 'interval1': [0, 2499], # in frames
73-
# 'interval2': [2500, 12499], # in frames
74-
# 'interval3': [12500, 17000]} # in frames
90+
time_intervals = {
91+
'pre-shock': [0, 7200], # in frames
92+
'after_shock1': [7200, 11050], # in frames
93+
'after_shock2': [11051, 14700],
94+
'after_shock3': [14701, 17500]} # in frames
7595
#
7696
# note: if you define time intervals, the analysis will be additionally performed for each
7797
# interval separately; the results will be saved in separate CSV files for each interval;
@@ -177,7 +197,10 @@
177197
#fig, ax = plt.subplots(2, 1, figsize=(12, 8), sharex=True)
178198
ax[0].plot(frames_array,x, label=body_part+' x', c=colors[body_part_i])
179199
ax[0].plot(frames_array,y, label=body_part+' y', c=colors[body_part_i], alpha=0.5)
180-
ax[1].plot(velocity, label=body_part, c=colors[body_part_i])
200+
if bodypart_not_to_plot is not None:
201+
if body_part not in bodypart_not_to_plot:
202+
ax[1].plot(velocity, label=body_part, c=colors[body_part_i])
203+
181204
# indicate with a shaded area the frames where the body part is moving; to do so, filter for consecutive True values:
182205
moving_frames = velocity_df[body_part + '_moving']
183206
moving_frames_diff = np.diff(moving_frames.astype(int))
@@ -224,6 +247,8 @@
224247
ax[1].axhline(movement_threshold, color='r', linestyle='--', label=f'movement threshold\n({movement_threshold} {unit_snippet}/s)')
225248
ax[1].legend(loc='upper right')
226249
ax[1].set_xlim(0, len(velocity_df))
250+
if ylim is not None:
251+
ax[1].set_ylim(0, ylim)
227252
# change y-axis to log scale:
228253
#ax[1].set_yscale('log')
229254
ax[1].set_title(f"body parts movement velocity $v=\\sqrt{{v_x^2 + v_y^2}}$, with $v_{{x/y}}=\\frac{{\\Delta x/y}}{{\\Delta t}}$")

0 commit comments

Comments
 (0)