Skip to content

Commit 4bda7bc

Browse files
committed
updated NaN handling
1 parent 7886b10 commit 4bda7bc

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

experiments/custom/triggers.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self, threshold: float
4747
4848
:param str interaction_type: Type of interaction ('proximity' or 'distance'). Proximity is checking for distances lower than threshold,
4949
while distance is checking for distances higher than threshold. Default: 'proximity'
50-
:param debug: Not used in this trigger
50+
:param debug: will add another reporting to reponse that is shown in the stream
5151
"""
5252

5353
self._threshold = threshold
@@ -74,15 +74,19 @@ def check_skeleton(self, skeletons: dict):
7474
active_coords = skeletons[self._active_animal][active_bp]
7575
for passive_bp in self._identification_dict['passive']['bp']:
7676
passive_coords = skeletons[self._passive_animal][passive_bp]
77-
#calculate distance for all combinations
78-
distance = calculate_distance(active_coords, passive_coords)
7977
temp_result = False
80-
if distance >= self._threshold and self._interaction_type == 'distance':
81-
temp_result = True
82-
elif distance < self._threshold and self._interaction_type == 'proximity':
83-
temp_result = True
78+
#calculate distance for all combinations if none of the coordinates are NaN
79+
if not any(np.isnan([*active_coords, *passive_coords])):
80+
distance = calculate_distance(active_coords, passive_coords)
81+
if distance >= self._threshold and self._interaction_type == 'distance':
82+
temp_result = True
83+
elif distance < self._threshold and self._interaction_type == 'proximity':
84+
temp_result = True
85+
else:
86+
pass
8487
else:
8588
pass
89+
8690
results.append(temp_result)
8791

8892
result = any(results)
@@ -92,15 +96,22 @@ def check_skeleton(self, skeletons: dict):
9296
if self._debug:
9397
active_point_x, active_point_y = skeletons[self._active_animal][self._active_bp[0]]
9498
passive_point_x, passive_point_y= skeletons[self._passive_animal][self._passive_bp[0]]
99+
if not any(np.isnan([active_point_x, active_point_y,passive_point_x, passive_point_y])):
100+
response_body = {'plot': {'text': dict(text=f'{self._interaction_type}: {result}',
101+
org= (20 , 20),
102+
color= color),
103+
'line': dict(pt1=(int(active_point_x), int(active_point_y)),
104+
pt2=(int(passive_point_x), int(passive_point_y)),
105+
color=color),
106+
}
107+
}
108+
else:
109+
response_body = {'plot': {'text': dict(text=f'{self._interaction_type}: {result}',
110+
org=(20,20),
111+
color=color)
112+
}
113+
}
95114

96-
response_body = {'plot': {'text': dict(text=f'{self._interaction_type}: {result}',
97-
org= (20 , 20),
98-
color= color),
99-
'line': dict(pt1=(int(active_point_x), int(active_point_y)),
100-
pt2=(int(passive_point_x), int(passive_point_y)),
101-
color=color),
102-
}
103-
}
104115
else:
105116
response_body = {'plot': {'text': dict(text=f'{self._interaction_type}: {result}',
106117
org=(20 , 20),

utils/poser.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ def handle_missing_bp(animal_skeletons: list):
362362
If HANDLE_MISSING is skip: the complete skeleton is removed (default);
363363
If HANDLE_MISSING is null: the missing coordinate is set to 0.0, not recommended for experiments
364364
where continuous monitoring of parameters is necessary.
365+
If HANDLE_MISSING is pass: the missing coordinate is left NaN. This is useful to keep identities, but can yield
366+
unexpected results down the line if NaN values are not caught.
367+
If HANDLE_MISSING is reset: the whole skeleton is set to NaN. This is useful to keep identities, but can yield
368+
unexpected results down the line if NaN values are not caught.
365369
366370
Missing skeletons will not be passed to the trigger, while resetting coordinates might lead to false results returned
367371
by triggers.
@@ -370,18 +374,26 @@ def handle_missing_bp(animal_skeletons: list):
370374
:param: animal_skeletons: list of skeletons returned by calculate skeleton
371375
:return animal_skeleton with handled missing values"""
372376

373-
#TODO: Handle missing instance in multiple animal approach to keep identity safe!
374-
375377
for skeleton in animal_skeletons:
376378
for bodypart, coordinates in skeleton.items():
377379
np_coords = np.array((coordinates))
378380
if any(np.isnan(np_coords)):
381+
if HANDLE_MISSING == 'pass':
382+
#do nothing
383+
pass
379384
if HANDLE_MISSING == 'skip':
385+
#remove the whole skeleton
380386
animal_skeletons.remove(skeleton)
381387
break
382388
elif HANDLE_MISSING == 'null':
389+
#remove replace coordinates with 0,0
383390
new_coordinates = np.nan_to_num(np_coords, copy = True)
384391
skeleton[bodypart] = tuple(new_coordinates)
392+
elif HANDLE_MISSING == 'reset':
393+
#reset complete skeleton to NaN, NaN
394+
reset_skeleton = {bp: (np.NaN, np.NaN) for bp in skeleton}
395+
animal_skeletons = [reset_skeleton if i == skeleton else i for i in animal_skeletons]
396+
break
385397
else:
386398
animal_skeletons.remove(skeleton)
387399
break

0 commit comments

Comments
 (0)