Skip to content

Commit

Permalink
[DAR-2973][External] Creation and import of text item-level properties (
Browse files Browse the repository at this point in the history
#946)

* Creation and import of text item-level properties

* Improved unit test coverage
  • Loading branch information
JBWilkie authored Oct 29, 2024
1 parent 1a17d18 commit 92002c0
Show file tree
Hide file tree
Showing 22 changed files with 308 additions and 44 deletions.
3 changes: 2 additions & 1 deletion darwin/future/data_objects/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ def to_create_endpoint(
"name": True,
"type": True,
"required": True,
"property_values": {"__all__": {"value", "color", "type"}},
"description": True,
"granularity": True,
}
if self.type != "text":
include_fields["property_values"] = {"__all__": {"value", "color", "type"}}
if self.granularity != PropertyGranularity.item:
if self.annotation_class_id is None:
raise ValueError("annotation_class_id must be set")
Expand Down
68 changes: 30 additions & 38 deletions darwin/importer/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,21 @@ def _serialize_item_level_properties(
for item_property_value in item_property_values:
item_property = team_item_properties_lookup[item_property_value["name"]]
item_property_id = item_property.id
item_property_value_id = next(
(
pv.id
for pv in item_property.property_values or []
if pv.value == item_property_value["value"]
),
None,
)
if (
item_property.type == "single_select"
or item_property.type == "multi_select"
):
item_property_value_id = next(
(
pv.id
for pv in item_property.property_values or []
if pv.value == item_property_value["value"]
),
None,
)
value = {"id": item_property_value_id}
elif item_property.type == "text":
value = {"text": item_property_value["value"]}
actors: List[dt.DictFreeForm] = []
actors.extend(
_handle_annotators(
Expand All @@ -406,7 +413,7 @@ def _serialize_item_level_properties(
{
"actors": actors,
"property_id": item_property_id,
"value": {"id": item_property_value_id},
"value": value,
}
)

Expand Down Expand Up @@ -798,39 +805,19 @@ def _import_properties(
_get_team_properties_annotation_lookup(client, dataset.team)
)

# Create or update item-level properties from annotations
item_property_creations_from_metadata, item_property_updates_from_metadata = (
_create_update_item_properties(
_normalize_item_properties(item_properties),
team_item_properties_lookup,
client,
)
# Update item-level properties from annotations
_, item_properties_to_update_from_annotations = _create_update_item_properties(
_normalize_item_properties(item_properties),
team_item_properties_lookup,
client,
)

properties_to_create = item_property_creations_from_metadata
properties_to_update = item_property_updates_from_metadata

if properties_to_create:
console.print(f"Creating {len(properties_to_create)} properties:", style="info")
for full_property in properties_to_create:
if full_property.granularity.value == "item":
console.print(
f"- Creating item-level property '{full_property.name}' of type: {full_property.type}"
)
console.print(
f"- Creating property '{full_property.name}' of type {full_property.type}",
)
prop = client.create_property(
team_slug=full_property.slug, params=full_property
)
created_properties.append(prop)

if item_property_updates_from_metadata:
if item_properties_to_update_from_annotations:
console.print(
f"Performing {len(item_property_updates_from_metadata)} property update(s):",
f"Performing {len(item_properties_to_update_from_annotations)} property update(s):",
style="info",
)
for full_property in item_property_updates_from_metadata:
for full_property in item_properties_to_update_from_annotations:
if full_property.granularity.value == "item":
console.print(
f"- Updating item-level property '{full_property.name}' with new value: {full_property.property_values[0].value}"
Expand Down Expand Up @@ -966,7 +953,8 @@ def _normalize_item_properties(
for item_prop in item_properties:
name = item_prop["name"]
value = item_prop["value"]
normalized_properties[name]["property_values"].append({"value": value})
if value:
normalized_properties[name]["property_values"].append({"value": value})

return normalized_properties

Expand Down Expand Up @@ -996,7 +984,11 @@ def _create_update_item_properties(

# If the property exists in the team, check that all values are present
if item_prop_name in team_item_properties_lookup:

t_prop = team_item_properties_lookup[item_prop_name]
# If the property is a text property it won't have predefined values, so continue
if t_prop.type == "text":
continue
t_prop_values = [
prop_val.value for prop_val in t_prop.property_values or []
]
Expand Down
5 changes: 4 additions & 1 deletion darwin/path_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def is_properties_enabled(
if _cls.get("properties"):
return metadata_path
for _item_level_property in metadata_item_level_properties:
if _item_level_property.get("property_values"):
if (
_item_level_property.get("property_values")
or _item_level_property["type"] == "text"
):
return metadata_path

return False
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "test_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@
}
],
"granularity": "item"
},
{
"name": "new_item_level_property_text",
"type": "text",
"description": "",
"required": false,
"property_values": [],
"granularity": "item"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,10 @@
{
"name": "test_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@
{
"name": "new_item_level_property_single_select",
"value": "1"
},
{
"name": "new_item_level_property_text",
"value": "sample text"
}
]
}
9 changes: 5 additions & 4 deletions e2e_tests/setup_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ def create_item_level_property(
"name": name,
"type": item_level_property_type,
"granularity": "item",
"property_values": [
}
if item_level_property_type in ["single_select", "multi_select"]:
payload["property_values"] = [
{"color": "rgba(255,92,0,1.0)", "value": "1"},
{"color": "rgba(255,92,0,1.0)", "value": "2"},
],
}
]
response = requests.post(url, json=payload, headers=headers)
parsed_response = response.json()
return E2EItemLevelProperty(
Expand Down Expand Up @@ -593,7 +594,7 @@ def setup_item_level_properties(config: ConfigValues) -> List[E2EItemLevelProper
item_level_properties: List[E2EItemLevelProperty] = []

print("Setting up item-level properties")
item_level_property_types = ["single_select", "multi_select"]
item_level_property_types = ["single_select", "multi_select", "text"]
try:
for item_level_property_type in item_level_property_types:
try:
Expand Down
Loading

0 comments on commit 92002c0

Please sign in to comment.