Skip to content

Commit bc56ee6

Browse files
authored
Edit to umap functions to allow for less than 3 components, docs (#11)
* minor edits to umap fncs * added pcutoff for umap, set x,y to 0, all keyp. op
1 parent 62e7708 commit bc56ee6

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ dlc2kinematics.plot_3d_pca_reconstruction(df_vel, n_components=10, framenumber=5
110110

111111
### UMAP Embeddings
112112
``` python
113-
data = dlc2kinematics.compute_umap(df, key=['LeftForelimb', 'RightForelimb'], chunk_length=30, fit_transform=True, n_neighbors=30, n_components=3,metric="euclidean")
113+
embedding, transformed_data = dlc2kinematics.compute_umap(df, key=['LeftForelimb', 'RightForelimb'], chunk_length=30, fit_transform=True, n_neighbors=30, n_components=3,metric="euclidean")
114114

115-
dlc2kinematics.plot_umap(data, size=5, alpha=1, color="indigo", figsize=(10, 6))
115+
dlc2kinematics.plot_umap(transformed_data, size=5, alpha=1, color="indigo", figsize=(10, 6))
116116
```
117117

118118
## Contributing

dlc2kinematics/mainfxns.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ def extract_kinematic_synergies(
193193

194194
def compute_umap(
195195
df,
196-
key,
196+
keypoints=None,
197+
pcutoff=0.6,
197198
chunk_length=30,
198199
fit_transform=True,
199200
n_neighbors=30,
@@ -234,7 +235,9 @@ def compute_umap(
234235
----------
235236
df: original dataframe df, _, _ = dlc2kinematics.load_data(DLC_2D_file)
236237
237-
key: list of limbs of interests ex: key = ['LeftForelimb', 'RightForelimb']
238+
keypoints: list of limbs of interests ex: key = ['LeftForelimb', 'RightForelimb'], if None, all bodyparts are taken into account
239+
240+
pcutoff: likelihood at which the keypoints are kept for analysis, if under pcutoff, set coord to 0
238241
239242
chunk_length: Number of frames per segment. #TODO: add the posibility of a sliding window?
240243
@@ -299,11 +302,17 @@ def compute_umap(
299302

300303
# pandas dataframe managing
301304
df_clean = df.copy()
302-
df_clean.columns = df_clean.columns.droplevel(
303-
0
304-
) # Drop scorer row to make it easier to navigate in the df table
305-
key2 = ["x", "y"]
306-
df_limbs = df_clean.loc[:, (key, key2)] # Drop likelihood column
305+
306+
if keypoints is None: # If no keypoints specified, use all
307+
keypoints = df_clean.columns.get_level_values('bodyparts').unique().to_list()
308+
309+
df_limbs = df_clean.loc[:, pd.IndexSlice[:, keypoints]]
310+
311+
temp = df_limbs.stack(level=['scorer', 'bodyparts']) # Stack with likelihood, x, y
312+
temp.loc[temp['likelihood'] < pcutoff, ['x','y']] = 0.0 # Set values under pcutoff to 0.0 to exclude
313+
unstacked_temp = temp.unstack(level=['scorer', 'bodyparts']) # Unstack again
314+
unstacked_temp.reorder_levels(['scorer','bodyparts','coords'], axis=1).reindex_like(df_limbs) # Re-index like original df
315+
307316
n_frames, n_bodyparts = df_limbs.shape
308317
n_chunks = n_frames // chunk_length
309318

dlc2kinematics/plotting.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,19 @@ def plot_umap(Y, size=5, alpha=1, color="indigo", figsize=(10, 6)):
273273
>>> c = np.random.rand(Y.shape[0],3)
274274
>>> dlc2kinematics.plot_umap(Y, 5, 1, c)
275275
"""
276-
277-
fig = plt.figure(figsize=figsize)
278-
ax = Axes3D(fig)
279-
ax.view_init(0, 20)
280-
ax.dist = 8
281-
ax.scatter(Y[:, 0], Y[:, 2], Y[:, 1], alpha=alpha, s=size, c=color)
276+
277+
if Y.shape[1] < 2:
278+
print("Please pick at least 2 components for plotting.")
279+
else:
280+
fig, ax = plt.subplots(figsize=figsize)
281+
282+
if Y.shape[1] > 2:
283+
ax = Axes3D(fig)
284+
ax.view_init(0, 20)
285+
ax.dist = 8
286+
ax.scatter(Y[:, 0], Y[:, 1], Y[:, 2], alpha=alpha, s=size, c=color)
287+
else:
288+
ax.scatter(Y[:, 0], Y[:, 1], alpha=alpha, s=size, c=color)
289+
290+
291+

0 commit comments

Comments
 (0)