Skip to content

Commit

Permalink
Add Updater to 24.11.7 (#660)
Browse files Browse the repository at this point in the history
* Add updater from 24.11.6 to 24.11.7 to handle missing private_attribute_id of PointArray

* Move compare_dicts, _lists and _values to updater_utils

* Reduce the size of reference simulation json for unit test
  • Loading branch information
angranl-flex authored Jan 16, 2025
1 parent 2297e7b commit 11f8f4c
Show file tree
Hide file tree
Showing 9 changed files with 708 additions and 54 deletions.
44 changes: 43 additions & 1 deletion flow360/component/simulation/framework/updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,56 @@
import re

from ....exceptions import Flow360NotImplementedError, Flow360RuntimeError
from .entity_base import generate_uuid
from .updater_utils import compare_dicts


def _no_update(params_as_dict):
return params_as_dict


def _24_11_6_to_24_11_7_update(params_as_dict):
# Check if PointArray has private_attribute_id. If not, generate the uuid and assign the id
# to all occurance of the same PointArray
if params_as_dict.get("outputs") is None:
return params_as_dict

point_array_list = []
for output in params_as_dict["outputs"]:
if output.get("entities", None):
for entity in output["entities"]["stored_entities"]:
if (
entity.get("private_attribute_entity_type_name") == "PointArray"
and entity.get("private_attribute_id") is None
):
new_uuid = generate_uuid()
entity["private_attribute_id"] = new_uuid
point_array_list.append(entity)

if params_as_dict["private_attribute_asset_cache"].get("project_entity_info"):
for idx, draft_entity in enumerate(
params_as_dict["private_attribute_asset_cache"]["project_entity_info"]["draft_entities"]
):
if draft_entity.get("private_attribute_entity_type_name") != "PointArray":
continue
for point_array in point_array_list:
if compare_dicts(
dict1=draft_entity,
dict2=point_array,
ignore_keys=["private_attribute_id"],
):
params_as_dict["private_attribute_asset_cache"]["project_entity_info"][
"draft_entities"
][idx] = point_array
continue

return params_as_dict


UPDATE_MAP = [
("24.11.*", "24.11.*", _no_update),
("24.11.^([0-5])$", "24.11.6", _no_update),
("24.11.6", "24.11.7", _24_11_6_to_24_11_7_update),
("24.11.7", "24.11.*", _no_update),
]


Expand Down
56 changes: 56 additions & 0 deletions flow360/component/simulation/framework/updater_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""Utiliy functions for updater"""

from numbers import Number

import numpy as np


def compare_dicts(dict1, dict2, atol=1e-15, rtol=1e-10, ignore_keys=None):
"""Check two dictionaries are same or not"""
if ignore_keys is None:
ignore_keys = set()

# Filter out the keys to be ignored
dict1_filtered = {k: v for k, v in dict1.items() if k not in ignore_keys}
dict2_filtered = {k: v for k, v in dict2.items() if k not in ignore_keys}

if dict1_filtered.keys() != dict2_filtered.keys():
print(f"dict keys not equal, dict1 {dict1_filtered.keys()}, dict2 {dict2_filtered.keys()}")
return False

for key in dict1_filtered:
value1 = dict1_filtered[key]
value2 = dict2_filtered[key]

if not compare_values(value1, value2, atol, rtol, ignore_keys):
print(f"dict value of key {key} not equal dict1 {dict1[key]}, dict2 {dict2[key]}")
return False

return True


def compare_values(value1, value2, atol=1e-15, rtol=1e-10, ignore_keys=None):
"""Check two values are same or not"""
if isinstance(value1, Number) and isinstance(value2, Number):
return np.isclose(value1, value2, rtol, atol)
if isinstance(value1, dict) and isinstance(value2, dict):
return compare_dicts(value1, value2, atol, rtol, ignore_keys)
if isinstance(value1, list) and isinstance(value2, list):
return compare_lists(value1, value2, atol, rtol, ignore_keys)
return value1 == value2


def compare_lists(list1, list2, atol=1e-15, rtol=1e-10, ignore_keys=None):
"""Check two lists are same or not"""
if len(list1) != len(list2):
return False

if list1 and not isinstance(list1[0], dict):
list1, list2 = sorted(list1), sorted(list2)

for item1, item2 in zip(list1, list2):
if not compare_values(item1, item2, atol, rtol, ignore_keys):
print(f"list value not equal list1 {item1}, list2 {item2}")
return False

return True
Loading

0 comments on commit 11f8f4c

Please # to comment.