forked from ocean-data-factory-sweden/kso-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht8_utils.py
More file actions
1214 lines (1036 loc) · 41.2 KB
/
Copy patht8_utils.py
File metadata and controls
1214 lines (1036 loc) · 41.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# base imports
import os
import requests
import random
import pandas as pd
import numpy as np
import json
import logging
from io import BytesIO
from base64 import b64encode
# module imports
import kso_utils.db_utils as db_utils
from kso_utils.koster_utils import filter_bboxes, process_clips_koster
from kso_utils.spyfish_utils import process_clips_spyfish
import kso_utils.tutorials_utils as tutorials_utils
import kso_utils.project_utils as project_utils
# widget imports
from IPython.display import HTML, display, clear_output
import ipywidgets as widgets
from itables import show
from PIL import Image as PILImage, ImageDraw
import imagesize
from jupyter_bbox_widget import BBoxWidget
# Logging
logging.basicConfig()
logging.getLogger().setLevel(logging.INFO)
#### Set up ####
def choose_agg_parameters(subject_type: str):
"""
> This function creates a set of sliders that allow you to set the parameters for the aggregation
algorithm
:param subject_type: The type of subject you are aggregating. This can be either "frame" or "video"
:type subject_type: str
:return: the values of the sliders.
Aggregation threshold: (0-1) Minimum proportion of citizen scientists that agree in their classification of the clip/frame.
Min numbers of users: Minimum number of citizen scientists that need to classify the clip/frame.
Object threshold (0-1): Minimum proportion of citizen scientists that agree that there is at least one object in the frame.
IOU Epsilon (0-1): Minimum area of overlap among the classifications provided by the citizen scientists so that they will be considered to be in the same cluster.
Inter user agreement (0-1): The minimum proportion of users inside a given cluster that must agree on the frame annotation for it to be accepted.
"""
agg_users = widgets.FloatSlider(
value=0.8,
min=0,
max=1.0,
step=0.1,
description="Aggregation threshold:",
disabled=False,
continuous_update=False,
orientation="horizontal",
readout=True,
readout_format=".1f",
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
display(agg_users)
min_users = widgets.IntSlider(
value=3,
min=1,
max=15,
step=1,
description="Min numbers of users:",
disabled=False,
continuous_update=False,
orientation="horizontal",
readout=True,
readout_format="d",
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
display(min_users)
if subject_type == "frame":
agg_obj = widgets.FloatSlider(
value=0.8,
min=0,
max=1.0,
step=0.1,
description="Object threshold:",
disabled=False,
continuous_update=False,
orientation="horizontal",
readout=True,
readout_format=".1f",
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
display(agg_obj)
agg_iou = widgets.FloatSlider(
value=0.5,
min=0,
max=1.0,
step=0.1,
description="IOU Epsilon:",
disabled=False,
continuous_update=False,
orientation="horizontal",
readout=True,
readout_format=".1f",
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
display(agg_iou)
agg_iua = widgets.FloatSlider(
value=0.8,
min=0,
max=1.0,
step=0.1,
description="Inter user agreement:",
disabled=False,
continuous_update=False,
orientation="horizontal",
readout=True,
readout_format=".1f",
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
display(agg_iua)
return agg_users, min_users, agg_obj, agg_iou, agg_iua
else:
return agg_users, min_users
def choose_workflows(workflows_df: pd.DataFrame):
"""
It creates a dropdown menu for the user to choose a workflow name, a dropdown menu for the user to
choose a subject type, and a dropdown menu for the user to choose a workflow version
:param workflows_df: a dataframe containing the workflows you want to choose from
:type workflows_df: pd.DataFrame
"""
layout = widgets.Layout(width="auto", height="40px") # set width and height
# Display the names of the workflows
workflow_name = widgets.Dropdown(
options=workflows_df.display_name.unique().tolist(),
value=workflows_df.display_name.unique().tolist()[0],
description="Workflow name:",
disabled=False,
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
layout=layout,
)
# Display the type of subjects
subj_type = widgets.Dropdown(
options=["frame", "clip"],
value="clip",
description="Subject type:",
disabled=False,
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
layout=layout,
)
workflow_version, versions = choose_w_version(workflows_df, workflow_name.value)
def on_change(change):
with out:
if change["name"] == "value":
clear_output()
workflow_version.options = choose_w_version(
workflows_df, change["new"]
)[1]
workflow_name.observe(on_change)
out = widgets.Output()
display(out)
workflow_name.observe(on_change)
return workflow_name, subj_type, workflow_version
class WidgetMaker(widgets.VBox):
def __init__(self, workflows_df: pd.DataFrame):
"""
The function creates a widget that allows the user to select which workflows to run
:param workflows_df: the dataframe of workflows
"""
self.workflows_df = workflows_df
self.widget_count = widgets.IntText(
description="Number of workflows:",
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
self.bool_widget_holder = widgets.HBox(
layout=widgets.Layout(
width="100%", display="inline-flex", flex_flow="row wrap"
)
)
children = [
self.widget_count,
self.bool_widget_holder,
]
self.widget_count.observe(self._add_bool_widgets, names=["value"])
super().__init__(children=children)
def _add_bool_widgets(self, widg):
num_bools = widg["new"]
new_widgets = []
for _ in range(num_bools):
new_widget = choose_workflows(self.workflows_df)
for wdgt in new_widget:
wdgt.description = wdgt.description + f" #{_}"
new_widgets.extend(new_widget)
self.bool_widget_holder.children = tuple(new_widgets)
@property
def checks(self):
return {w.description: w.value for w in self.bool_widget_holder.children}
def choose_w_version(workflows_df: pd.DataFrame, workflow_id: str):
"""
It takes a workflow ID and returns a dropdown widget with the available versions of the workflow
:param workflows_df: a dataframe containing the workflows available in the Galaxy instance
:param workflow_id: The name of the workflow you want to run
:return: A tuple containing the widget and the list of versions available.
"""
# Estimate the versions of the workflow available
versions_available = (
workflows_df[workflows_df.display_name == workflow_id].version.unique().tolist()
)
if len(versions_available) > 1:
# Display the versions of the workflow available
w_version = widgets.Dropdown(
options=list(map(float, versions_available)),
value=float(versions_available[0]),
description="Minimum workflow version:",
disabled=False,
display="flex",
flex_flow="column",
align_items="stretch",
style={"description_width": "initial"},
)
else:
raise ValueError("There are no versions available for this workflow.")
# display(w_version)
return w_version, list(map(float, versions_available))
def get_workflow_ids(workflows_df: pd.DataFrame, workflow_names: list):
# The function that takes a list of workflow names and returns a list of workflow
# ids.
return [
workflows_df[workflows_df.display_name == wf_name].workflow_id.unique()[0]
for wf_name in workflow_names
]
def get_classifications(
workflow_dict: dict,
workflows_df: pd.DataFrame,
subj_type: str,
class_df: pd.DataFrame,
db_path: str,
project: project_utils.Project,
):
"""
It takes in a dictionary of workflows, a dataframe of workflows, the type of subject (frame or
clip), a dataframe of classifications, the path to the database, and the project name. It returns a
dataframe of classifications
:param workflow_dict: a dictionary of the workflows you want to retrieve classifications for. The
keys are the workflow names, and the values are the workflow IDs, workflow versions, and the minimum
number of classifications per subject
:type workflow_dict: dict
:param workflows_df: the dataframe of workflows from the Zooniverse project
:type workflows_df: pd.DataFrame
:param subj_type: "frame" or "clip"
:param class_df: the dataframe of classifications from the database
:param db_path: the path to the database file
:param project: the name of the project on Zooniverse
:return: A dataframe with the classifications for the specified project and workflow.
"""
names, workflow_versions = [], []
for i in range(0, len(workflow_dict), 3):
names.append(list(workflow_dict.values())[i])
workflow_versions.append(list(workflow_dict.values())[i + 2])
workflow_ids = get_workflow_ids(workflows_df, names)
# Filter classifications of interest
classes = []
for id, version in zip(workflow_ids, workflow_versions):
class_df = class_df[
(class_df.workflow_id == id) & (class_df.workflow_version >= version)
].reset_index(drop=True)
classes.append(class_df)
classes_df = pd.concat(classes)
# Add information about the subject
# Create connection to db
conn = db_utils.create_connection(db_path)
if subj_type == "frame":
# Query id and subject type from the subjects table
subjects_df = pd.read_sql_query(
"SELECT id, subject_type, \
https_location, filename, frame_number, movie_id FROM subjects \
WHERE subject_type=='frame'",
conn,
)
else:
# Query id and subject type from the subjects table
subjects_df = pd.read_sql_query(
"SELECT id, subject_type, \
https_location, filename, clip_start_time, movie_id FROM subjects \
WHERE subject_type=='clip'",
conn,
)
# Ensure id format matches classification's subject_id
classes_df["subject_ids"] = classes_df["subject_ids"].astype("Int64")
subjects_df["id"] = subjects_df["id"].astype("Int64")
# Add subject information based on subject_ids
classes_df = pd.merge(
classes_df,
subjects_df,
how="left",
left_on="subject_ids",
right_on="id",
)
if classes_df[["subject_type", "https_location"]].isna().any().any():
# Exclude classifications from missing subjects
filtered_class_df = classes_df.dropna(
subset=["subject_type", "https_location"], how="any"
).reset_index(drop=True)
# Report on the issue
logging.info(
f"There are {(classes_df.shape[0]-filtered_class_df.shape[0])}"
f" classifications out of {classes_df.shape[0]}"
f" missing subject info. Maybe the subjects have been removed from Zooniverse?"
)
classes_df = filtered_class_df
logging.info("Zooniverse classifications have been retrieved")
return classes_df
def aggregrate_labels(raw_class_df: pd.DataFrame, agg_users: float, min_users: int):
"""
> This function takes a dataframe of classifications and returns a dataframe of classifications that
have been filtered by the number of users that classified each subject and the proportion of users
that agreed on their annotations
:param raw_class_df: the dataframe of all the classifications
:param agg_users: the proportion of users that must agree on a classification for it to be included
in the final dataset
:param min_users: The minimum number of users that must have classified a subject for it to be
included in the final dataset
:return: a dataframe with the aggregated labels.
"""
# Calculate the number of users that classified each subject
raw_class_df["n_users"] = raw_class_df.groupby("subject_ids")[
"classification_id"
].transform("nunique")
# Select classifications with at least n different user classifications
raw_class_df = raw_class_df[raw_class_df.n_users >= min_users].reset_index(
drop=True
)
# Calculate the proportion of unique classifications (it can have multiple annotations) per subject
raw_class_df["class_n"] = raw_class_df.groupby(["subject_ids", "label"])[
"classification_id"
].transform("nunique")
# Calculate the proportion of users that agreed on their annotations
raw_class_df["class_prop"] = raw_class_df.class_n / raw_class_df.n_users
# Select annotations based on agreement threshold
agg_class_df = raw_class_df[raw_class_df.class_prop >= agg_users].reset_index(
drop=True
)
# Calculate the proportion of unique classifications aggregated per subject
agg_class_df["class_n_agg"] = agg_class_df.groupby(["subject_ids"])[
"label"
].transform("nunique")
return agg_class_df
def aggregrate_classifications(
df: pd.DataFrame, subj_type: str, project: project_utils.Project, agg_params
):
"""
We take the raw classifications and process them to get the aggregated labels
:param df: the raw classifications dataframe
:param subj_type: the type of subject, either "frame" or "clip"
:param project: the project object
:param agg_params: list of parameters for the aggregation
:return: the aggregated classifications and the raw classifications.
"""
logging.info("Aggregrating the classifications")
# We take the raw classifications and process them to get the aggregated labels.
if subj_type == "frame":
# Get the aggregration parameters
if not isinstance(agg_params, list):
agg_users, min_users, agg_obj, agg_iou, agg_iua = [
i.value for i in agg_params
]
else:
agg_users, min_users, agg_obj, agg_iou, agg_iua = agg_params
# Report selected parameters
logging.info(
f"Aggregation parameters are: Agg. threshold "
f"{agg_users} "
f"Min. users "
f"{min_users} "
f"Obj threshold "
f"{agg_obj} "
f"IOU "
f"{agg_iou} "
f"Int. agg. "
f"{agg_iua} "
)
# Process the raw classifications
raw_class_df = process_frames(df, project.Project_name)
# Aggregrate frames based on their labels
agg_labels_df = aggregrate_labels(raw_class_df, agg_users, min_users)
# Get rid of the "empty" labels if other species are among the volunteer consensus
agg_labels_df = agg_labels_df[
~((agg_labels_df["class_n_agg"] > 1) & (agg_labels_df["label"] == "empty"))
]
# Select frames aggregrated only as empty
agg_labels_df_empty = agg_labels_df[agg_labels_df["label"] == "empty"]
agg_labels_df_empty = agg_labels_df_empty.rename(
columns={"frame_number": "start_frame"}
)
agg_labels_df_empty = agg_labels_df_empty[
[
"label",
"subject_ids",
"x",
"y",
"w",
"h",
]
]
# Temporary exclude frames aggregrated as empty
agg_labels_df = agg_labels_df[agg_labels_df["label"] != "empty"]
# Map the position of the annotation parameters
col_list = list(agg_labels_df.columns)
x_pos, y_pos, w_pos, h_pos, user_pos, subject_id_pos = (
col_list.index("x"),
col_list.index("y"),
col_list.index("w"),
col_list.index("h"),
col_list.index("user_name"),
col_list.index("subject_ids"),
)
# Get prepared annotations
new_rows = []
if agg_labels_df["frame_number"].isnull().all():
group_cols = ["subject_ids", "label"]
else:
group_cols = ["subject_ids", "label", "frame_number"]
for name, group in agg_labels_df.groupby(group_cols):
if "frame_number" in group_cols:
subj_id, label, start_frame = name
total_users = agg_labels_df[
(agg_labels_df.subject_ids == subj_id)
& (agg_labels_df.label == label)
& (agg_labels_df.frame_number == start_frame)
]["user_name"].nunique()
else:
subj_id, label = name
start_frame = np.nan
total_users = agg_labels_df[
(agg_labels_df.subject_ids == subj_id)
& (agg_labels_df.label == label)
]["user_name"].nunique()
# Filter bboxes using IOU metric (essentially a consensus metric)
# Keep only bboxes where mean overlap exceeds this threshold
indices, new_group = filter_bboxes(
total_users=total_users,
users=[i[user_pos] for i in group.values],
bboxes=[
np.array([i[x_pos], i[y_pos], i[w_pos], i[h_pos]])
for i in group.values
],
obj=agg_obj,
eps=agg_iou,
iua=agg_iua,
)
subject_ids = [i[subject_id_pos] for i in group.values[indices]]
for ix, box in zip(subject_ids, new_group):
new_rows.append(
(
label,
start_frame,
ix,
)
+ tuple(box)
)
agg_class_df = pd.DataFrame(
new_rows,
columns=[
"label",
"start_frame",
"subject_ids",
"x",
"y",
"w",
"h",
],
)
agg_class_df["subject_type"] = "frame"
agg_class_df["label"] = agg_class_df["label"].apply(
lambda x: x.split("(")[0].strip()
)
# Add the frames aggregated as "empty"
agg_class_df = pd.concat([agg_class_df, agg_labels_df_empty])
# Select the aggregated labels
agg_class_df = agg_class_df[
["subject_ids", "label", "x", "y", "w", "h"]
].drop_duplicates()
# Add the http info
agg_class_df = pd.merge(
agg_class_df,
raw_class_df[
["subject_ids", "https_location", "subject_type"]
].drop_duplicates(),
how="left",
on="subject_ids",
)
else:
# Get the aggregration parameters
if not isinstance(agg_params, list):
agg_users, min_users = [i.value for i in agg_params]
else:
agg_users, min_users = agg_params
# Process the raw classifications
raw_class_df = process_clips(df, project)
# Aggregrate clips based on their labels
agg_class_df = aggregrate_labels(raw_class_df, agg_users, min_users)
# Extract the median of the second where the animal/object is and number of animals
agg_class_df = agg_class_df.groupby(
["subject_ids", "https_location", "subject_type", "label"], as_index=False
)
agg_class_df = pd.DataFrame(agg_class_df[["how_many", "first_seen"]].median().round(0))
# Add username info to raw class
raw_class_df = pd.merge(
raw_class_df,
df[["classification_id", "user_name"]],
how="left",
on="classification_id",
)
print(
agg_class_df.shape[0],
"classifications aggregated out of",
df.subject_ids.nunique(),
"unique subjects available",
)
return agg_class_df, raw_class_df
def process_clips(df: pd.DataFrame, project: project_utils.Project):
"""
This function takes a dataframe of classifications and returns a dataframe of annotations
:param df: the dataframe of classifications
:type df: pd.DataFrame
:param project: the name of the project you want to download data from
:return: A dataframe with the classification_id, label, how_many, first_seen, https_location,
subject_type, and subject_ids.
"""
# Create an empty list
rows_list = []
# Loop through each classification submitted by the users
for index, row in df.iterrows():
# Load annotations as json format
annotations = json.loads(row["annotations"])
# Select the information from the species identification task
if project.Project_name == "Koster_Seafloor_Obs":
rows_list = process_clips_koster(
annotations, row["classification_id"], rows_list
)
# Check if the Zooniverse project is the Spyfish
if project.Project_name == "Spyfish_Aotearoa":
rows_list = process_clips_spyfish(
annotations, row["classification_id"], rows_list
)
# Create a data frame with annotations as rows
annot_df = pd.DataFrame(
rows_list, columns=["classification_id", "label", "first_seen", "how_many"]
)
# Specify the type of columns of the df
annot_df["how_many"] = pd.to_numeric(annot_df["how_many"])
annot_df["first_seen"] = pd.to_numeric(annot_df["first_seen"])
# Add subject id to each annotation
annot_df = pd.merge(
annot_df,
df.drop(columns=["annotations"]),
how="left",
on="classification_id",
)
# Select only relevant columns
annot_df = annot_df[
[
"classification_id",
"label",
"how_many",
"first_seen",
"https_location",
"subject_type",
"subject_ids",
]
]
return pd.DataFrame(annot_df)
def launch_table(agg_class_df: pd.DataFrame, subject_type: str):
"""
It takes in a dataframe of aggregated classifications and a subject type, and returns a dataframe
with the columns "subject_ids", "label", "how_many", and "first_seen"
:param agg_class_df: the dataframe that you want to launch
:param subject_type: "clip" or "subject"
"""
if subject_type == "clip":
a = agg_class_df[["subject_ids", "label", "how_many", "first_seen"]]
else:
a = agg_class_df
return a
def process_frames(df: pd.DataFrame, project_name: str):
"""
It takes a dataframe of classifications and returns a dataframe of annotations
:param df: the dataframe containing the classifications
:type df: pd.DataFrame
:param project_name: The name of the project you want to download data from
:return: A dataframe with the following columns:
classification_id, x, y, w, h, label, https_location, filename, subject_type, subject_ids,
frame_number, user_name, movie_id
"""
# Create an empty list
rows_list = []
# Loop through each classification submitted by the users and flatten them
for index, row in df.iterrows():
# Load annotations as json format
annotations = json.loads(row["annotations"])
# Select the information from all the labelled animals (e.g. task = T0)
for ann_i in annotations:
if ann_i["task"] == "T0":
if ann_i["value"] == []:
# Specify the frame was classified as empty
choice_i = {
"classification_id": row["classification_id"],
"x": None,
"y": None,
"w": None,
"h": None,
"label": "empty",
}
rows_list.append(choice_i)
else:
# Select each species annotated and flatten the relevant answers
for i in ann_i["value"]:
choice_i = {
"classification_id": row["classification_id"],
"x": int(i["x"]) if "x" in i else None,
"y": int(i["y"]) if "y" in i else None,
"w": int(i["width"]) if "width" in i else None,
"h": int(i["height"]) if "height" in i else None,
"label": str(i["tool_label"])
if "tool_label" in i
else None,
}
rows_list.append(choice_i)
# Create a data frame with annotations as rows
flat_annot_df = pd.DataFrame(
rows_list, columns=["classification_id", "x", "y", "w", "h", "label"]
)
# Add other classification information to the flatten classifications
annot_df = pd.merge(
flat_annot_df,
df,
how="left",
on="classification_id",
)
# Select only relevant columns
annot_df = annot_df[
[
"classification_id",
"x",
"y",
"w",
"h",
"label",
"https_location",
"filename",
"subject_type",
"subject_ids",
"frame_number",
"user_name",
"movie_id",
]
]
return pd.DataFrame(annot_df)
def draw_annotations_in_frame(im: PILImage.Image, class_df_subject: pd.DataFrame):
"""
> The function takes an image and a dataframe of annotations and returns the image with the
annotations drawn on it
:param im: the image object of type PILImage
:param class_df_subject: a dataframe containing the annotations for a single subject
:return: The image with the annotations
"""
# Calculate image size
dw, dh = im._size
# Draw rectangles of each annotation
img1 = ImageDraw.Draw(im)
# Merge annotation info into a tuple
class_df_subject["vals"] = class_df_subject[["x", "y", "w", "h"]].values.tolist()
for index, row in class_df_subject.iterrows():
# Specify the vals object
vals = row.vals
# Adjust annotantions to image size
vals_adjusted = tuple(
[
int(vals[0]),
int(vals[1]),
int((vals[0] + vals[2])),
int((vals[1] + vals[3])),
]
)
# Draw annotation
img1.rectangle(vals_adjusted, width=2)
return im
def view_subject(subject_id: int, class_df: pd.DataFrame, subject_type: str):
"""
It takes a subject id, a dataframe containing the annotations for that subject, and the type of
subject (clip or frame) and returns an HTML object that can be displayed in a notebook
:param subject_id: The subject ID of the subject you want to view
:type subject_id: int
:param class_df: The dataframe containing the annotations for the class of interest
:type class_df: pd.DataFrame
:param subject_type: The type of subject you want to view. This can be either "clip" or "frame"
:type subject_type: str
"""
if subject_id in class_df.subject_ids.tolist():
# Select the subject of interest
class_df_subject = class_df[class_df.subject_ids == subject_id].reset_index(
drop=True
)
# Get the location of the subject
subject_location = class_df_subject["https_location"].unique()[0]
else:
raise Exception("The reference data does not contain media for this subject.")
if len(subject_location) == 0:
raise Exception("Subject not found in provided annotations")
# Get the HTML code to show the selected subject
if subject_type == "clip":
html_code = f"""
<html>
<div style="display: flex; justify-content: space-around">
<div>
<video width=500 controls>
<source src={subject_location} type="video/mp4">
</video>
</div>
<div>{class_df_subject[['label','first_seen','how_many']].value_counts().sort_values(ascending=False).to_frame().to_html()}</div>
</div>
</html>"""
elif subject_type == "frame":
# Read image
response = requests.get(subject_location)
im = PILImage.open(BytesIO(response.content))
# if label is not empty draw rectangles
if class_df_subject.label.unique()[0] != "empty":
# Create a temporary image with the annotations drawn on it
im = draw_annotations_in_frame(im, class_df_subject)
# Remove previous temp image if exist
if os.access(".", os.W_OK):
temp_image_path = "temp.jpg"
else:
temp_image_path = "/cephyr/NOBACKUP/groups/snic2021-6-9/tmp_dir/temp.jpg"
if os.path.exists(temp_image_path):
os.remove(temp_image_path)
# Save the new image
im.save(temp_image_path)
# Load image data (used to enable viewing in Colab)
img = open(temp_image_path, "rb").read()
data_url = "data:image/jpeg;base64," + b64encode(img).decode()
html_code = f"""
<html>
<div style="display: flex; justify-content: space-around">
<div>
<img src={data_url} type="image/jpeg" width=500>
</img>
</div>
<div>{class_df_subject[['label','colour']].value_counts().sort_values(ascending=False).to_frame().to_html()}</div>
</div>
</html>"""
else:
Exception("Subject type not supported.")
return HTML(html_code)
def launch_viewer(class_df: pd.DataFrame, subject_type: str):
"""
> This function takes a dataframe of classifications and a subject type (frame or video) and
displays a dropdown menu of subjects of that type. When a subject is selected, it displays the
subject and the classifications for that subject
:param class_df: The dataframe containing the classifications
:type class_df: pd.DataFrame
:param subject_type: The type of subject you want to view. This can be either "frame" or "video"
:type subject_type: str
"""
# If subject is frame assign a color to each label
if subject_type == "frame":
# Create a list of unique labels
list_labels = class_df.label.unique().tolist()
# Generate a list of random colors for each label
random_color_list = []
for index, item in enumerate(list_labels):
random_color_list = random_color_list + [
"#" + "".join([random.choice("ABCDEF0123456789") for i in range(6)])
]
# Add a column with the color for each label
class_df["colour"] = class_df.apply(
lambda row: random_color_list[list_labels.index(row.label)], axis=1
)
# Select the subject
options = tuple(
class_df[class_df["subject_type"] == subject_type]["subject_ids"]
.apply(int)
.apply(str)
.unique()
)
subject_widget = widgets.Combobox(
options=options,
description="Subject id:",
ensure_option=True,
disabled=False,
)
main_out = widgets.Output()
display(subject_widget, main_out)
# Display the subject and classifications on change
def on_change(change):
with main_out:
a = view_subject(int(change["new"]), class_df, subject_type)
clear_output()
display(a)
subject_widget.observe(on_change, names="value")
def explore_classifications_per_subject(class_df: pd.DataFrame, subject_type: str):
"""
> This function takes a dataframe of classifications and a subject type (clip or frame) and displays
the classifications for a given subject
:param class_df: the dataframe of classifications
:type class_df: pd.DataFrame
:param subject_type: "clip" or "frame"
"""
# Select the subject
subject_widget = widgets.Combobox(
options=tuple(class_df.subject_ids.apply(int).apply(str).unique()),
description="Subject id:",
ensure_option=True,
disabled=False,
)
main_out = widgets.Output()
display(subject_widget, main_out)
# Display the subject and classifications on change
def on_change(change):
with main_out:
a = class_df[class_df.subject_ids == int(change["new"])]
if subject_type == "clip":
a = a[
[
"classification_id",
"user_name",
"label",
"how_many",
"first_seen",
]
]
else: