Skip to content

Commit 2069672

Browse files
committed
Fix df filtering
1 parent 7214755 commit 2069672

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

superannotate/analytics/common.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import glob
12
import json
23
import logging
34
from pathlib import Path
4-
import glob
55

66
import pandas as pd
7+
78
from ..exceptions import SABaseException
9+
810
logger = logging.getLogger("superannotate-python-sdk")
911

1012

@@ -29,7 +31,12 @@ def df_to_annotations(df, output_dir):
2931
image_height = None
3032
image_width = None
3133
image_df = df[df["imageName"] == image]
32-
image_annotation = []
34+
image_annotation = {
35+
"instances": [],
36+
"metadata": {},
37+
"tags": [],
38+
"comments": []
39+
}
3340
instances = image_df["instanceId"].dropna().unique()
3441
for instance in instances:
3542
instance_df = image_df[image_df["instanceId"] == instance]
@@ -64,27 +71,31 @@ def df_to_annotations(df, output_dir):
6471
"name": row["attributeName"]
6572
}
6673
)
67-
image_annotation.append(instance_annotation)
74+
image_annotation["instances"].append(instance_annotation)
6875
image_width = image_width or instance_df.iloc[0]["imageWidth"]
6976
image_height = image_height or instance_df.iloc[0]["imageHeight"]
7077
image_pinned = image_pinned or instance_df.iloc[0]["imagePinned"]
7178
image_status = image_status or instance_df.iloc[0]["imageStatus"]
7279

7380
comments = image_df[image_df["type"] == "comment"]
7481
for _, comment in comments.iterrows():
75-
comment_json = {"type": "comment"}
82+
comment_json = {}
7683
comment_json.update(comment["meta"])
84+
comment_json["correspondence"] = comment_json["comments"]
85+
del comment_json["comments"]
7786
comment_json["resolved"] = comment["commentResolved"]
78-
image_annotation.append(comment_json)
87+
image_annotation["comments"].append(comment_json)
7988

80-
meta = {
81-
"type": "meta",
89+
tags = image_df[image_df["type"] == "tag"]
90+
for _, tag in tags.iterrows():
91+
image_annotation["tags"].append(tag["tag"])
92+
93+
image_annotation["metadata"] = {
8294
"width": int(image_width),
8395
"height": int(image_height),
8496
"status": image_status,
8597
"pinned": bool(image_pinned)
8698
}
87-
image_annotation.append(meta)
8899
json.dump(
89100
image_annotation,
90101
open(output_dir / f"{image}___{project_suffix}", "w"),
@@ -281,10 +292,10 @@ def __get_user_metadata(annotation):
281292
comment_meta = {
282293
"x": annotation["x"],
283294
"y": annotation["y"],
284-
"comments": annotation["comments"]
295+
"comments": annotation["correspondence"]
285296
}
286297
annotation_dict = {
287-
"type": annotation_type,
298+
"type": "comment",
288299
"meta": comment_meta,
289300
"commentResolved": comment_resolved,
290301
}
@@ -294,11 +305,7 @@ def __get_user_metadata(annotation):
294305
__append_annotation(annotation_dict)
295306
if include_tags:
296307
for annotation in annotation_json["tags"]:
297-
annotation_tag = annotation["name"]
298-
annotation_dict = {
299-
"type": annotation_type,
300-
"tag": annotation_tag
301-
}
308+
annotation_dict = {"type": "tag", "tag": annotation}
302309
annotation_dict.update(image_metadata)
303310
__append_annotation(annotation_dict)
304311
for annotation in annotation_json["instances"]:

tests/test_filter_instances.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,17 @@ def test_df_to_annotations_full(tmpdir):
215215
tmpdir = Path(tmpdir)
216216

217217
df = sa.aggregate_annotations_as_df(
218-
PROJECT_DIR, include_classes_wo_annotations=True, include_comments=True
218+
PROJECT_DIR,
219+
include_classes_wo_annotations=True,
220+
include_comments=True,
221+
include_tags=True
219222
)
220223
sa.df_to_annotations(df, tmpdir)
221224
df_new = sa.aggregate_annotations_as_df(
222-
tmpdir, include_classes_wo_annotations=True, include_comments=True
225+
tmpdir,
226+
include_classes_wo_annotations=True,
227+
include_comments=True,
228+
include_tags=True
223229
)
224230
for project in sa.search_projects("test df to annotations 4"):
225231
sa.delete_project(project)

0 commit comments

Comments
 (0)