Skip to content

Commit 31c873e

Browse files
bugfix for search_attribute_matching_name
1 parent 035b719 commit 31c873e

3 files changed

Lines changed: 100 additions & 36 deletions

File tree

energyml-utils/example/attic/arrays_test.py

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
from energyml.utils.storage_interface import EnergymlStorageInterface
1818
from energyml.utils.epc import Epc
1919
from energyml.utils.epc_stream import EpcStreamReader, RelsUpdateMode
20-
from energyml.utils.introspection import get_obj_title, search_attribute_matching_name, get_object_attribute
20+
from energyml.utils.introspection import (
21+
get_obj_title,
22+
search_attribute_matching_name,
23+
get_object_attribute,
24+
search_attribute_matching_name_with_path,
25+
)
2126
from energyml.resqml.v2_2.resqmlv2 import Point3DLatticeArray
2227
from energyml.eml.v2_3.commonv2 import TimeSeries
2328
from energyml.eml.v2_1.commonv2 import TimeSeries as TimeSeries21
@@ -326,29 +331,79 @@ def read_props_and_cbt(
326331
print("\n")
327332

328333

334+
def read_trset(
335+
epc_path: str = "rc/epc/testingPackageCpp22.epc", trset_uuid: str = "6e678338-3b53-49b6-8801-faee493e0c42"
336+
) -> List[AbstractMesh]:
337+
epc = Epc.read_file(f"{epc_path}", read_rels_from_files=False, recompute_rels=False)
338+
339+
trset = epc.get_object_by_uuid(trset_uuid)[0]
340+
# print(trset)
341+
# print(epc.get_h5_file_paths(trset))
342+
343+
meshes = read_mesh_object(energyml_object=trset, workspace=epc)
344+
345+
return meshes
346+
347+
348+
def print_tuple_list(tuple_list: List[tuple]) -> None:
349+
for t in tuple_list:
350+
print(t)
351+
352+
353+
def read_pointset(
354+
epc_path: str = "rc/epc/testingPackageCpp22.epc", pointset_uuid: str = "fbc5466c-94cd-46ab-8b48-2ae2162b372f"
355+
) -> List[AbstractMesh]:
356+
# epc = Epc.read_file(f"{epc_path}", read_rels_from_files=False, recompute_rels=False)
357+
epc = EpcStreamReader(
358+
epc_file_path=epc_path,
359+
rels_update_mode=RelsUpdateMode.MANUAL,
360+
)
361+
362+
pointset = epc.get_object_by_uuid(pointset_uuid)[0]
363+
# print(pointset)
364+
# print(epc.get_h5_file_paths(pointset))
365+
# meshes = []
366+
meshes = read_mesh_object(energyml_object=pointset, workspace=epc)
367+
368+
# logging.debug("=" * 40)
369+
# print_tuple_list(search_attribute_matching_name_with_path(pointset, r"NodePatch.[\d]+.Geometry.Points"))
370+
# logging.debug("=" * 40)
371+
# print_tuple_list(
372+
# search_attribute_matching_name_with_path(pointset, r"NodePatchGeometry.[\d]+.Points")
373+
# ) # resqml 2.0.1
374+
# logging.debug("=" * 40)
375+
376+
return meshes
377+
378+
329379
if __name__ == "__main__":
330380
logging.basicConfig(level=logging.DEBUG)
331381

332382
# meshes = read_grid()
333383
# meshes = read_polyline()
334384
# meshes = read_wellbore_frame_repr()
335385
# meshes = read_representation_set_representation()
336-
337-
# for m in meshes:
338-
# print("=" * 40)
339-
# print(f"Mesh identifier: {m.identifier}")
340-
# print("points:")
341-
# print(np.array(m.point_list))
342-
343-
# if isinstance(m, SurfaceMesh):
344-
# print("face indices:")
345-
# print(np.array(m.faces_indices))
346-
# elif isinstance(m, PolylineSetMesh):
347-
# print("line indices:")
348-
# try:
349-
# print(np.array(m.line_indices))
350-
# except Exception as e:
351-
# print(m.line_indices)
352-
# raise e
353-
354-
read_props_and_cbt()
386+
# meshes = read_trset()
387+
meshes = read_pointset()
388+
389+
print(f"Number of meshes read: {len(meshes)}")
390+
391+
if meshes:
392+
for m in meshes:
393+
print("=" * 40)
394+
print(f"Mesh identifier: {m.identifier}")
395+
print("points:")
396+
print(np.array(m.point_list))
397+
398+
if isinstance(m, SurfaceMesh):
399+
print("face indices:")
400+
print(np.array(m.faces_indices))
401+
elif isinstance(m, PolylineSetMesh):
402+
print("line indices:")
403+
try:
404+
print(np.array(m.line_indices))
405+
except Exception as e:
406+
print(m.line_indices)
407+
raise e
408+
409+
# read_props_and_cbt()

energyml-utils/src/energyml/utils/data/mesh.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,16 @@ def read_point_representation(
269269

270270
patch_idx = 0
271271
total_size = 0
272-
for (
273-
points_path_in_obj,
274-
points_obj,
275-
) in search_attribute_matching_name_with_path(
272+
273+
patches_geom = search_attribute_matching_name_with_path(
276274
energyml_object, r"NodePatch.[\d]+.Geometry.Points"
277275
) + search_attribute_matching_name_with_path( # resqml 2.0.1
278276
energyml_object, r"NodePatchGeometry.[\d]+.Points"
279-
): # resqml 2.2
277+
)
278+
logging.debug(f"Found {len(patches_geom)} patches for point representation")
279+
logging.debug(f"\t=> {patches_geom}")
280+
281+
for points_path_in_obj, points_obj in patches_geom:
280282
points = read_array(
281283
energyml_array=points_obj,
282284
root_obj=energyml_object,
@@ -644,12 +646,16 @@ def read_triangulated_set_representation(
644646
point_offset = 0
645647
patch_idx = 0
646648
total_size = 0
647-
for patch_path, patch in search_attribute_matching_name_with_path(
649+
650+
patches = search_attribute_matching_name_with_path(
648651
energyml_object,
649-
"\\.*Patch.\\d+",
652+
"\\w*Patch.\\d+",
650653
deep_search=False,
651654
search_in_sub_obj=False,
652-
):
655+
)
656+
# logging.debug(f"Found {len(patches)} patches for triangulated set representation")
657+
658+
for patch_path, patch in patches:
653659
crs = None
654660
try:
655661
crs = get_crs_obj(

energyml-utils/src/energyml/utils/introspection.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -397,16 +397,16 @@ def get_all_matching_class_attribute_name(
397397
Example : "\\w*.Version" --> ["object_version", "ObjectVersion", "obj_version", ...]
398398
This method doesn't only transform to snake case but search into the obj class attributes (or dict keys)
399399
"""
400-
matching_names = []
400+
matching_names = set()
401401
if isinstance(cls, dict):
402402
for name in cls.keys():
403403
if snake_case(name) == snake_case(attribute_name):
404-
matching_names.append(name)
404+
matching_names.add(name)
405405
pattern = re.compile(attribute_name, flags=re_flags)
406406
for name in cls.keys():
407407
if pattern.match(name):
408-
matching_names.append(name)
409-
return matching_names
408+
matching_names.add(name)
409+
return list(matching_names)
410410
else:
411411
class_fields = get_class_fields(cls)
412412
try:
@@ -415,7 +415,7 @@ def get_all_matching_class_attribute_name(
415415
if snake_case(name) == snake_case(attribute_name) or (
416416
hasattr(cf, "metadata") and "name" in cf.metadata and cf.metadata["name"] == attribute_name
417417
):
418-
matching_names.append(name)
418+
matching_names.add(name)
419419

420420
# search regex after to avoid shadowing perfect match
421421
pattern = re.compile(attribute_name, flags=re_flags)
@@ -424,12 +424,12 @@ def get_all_matching_class_attribute_name(
424424
if pattern.match(name) or (
425425
hasattr(cf, "metadata") and "name" in cf.metadata and pattern.match(cf.metadata["name"])
426426
):
427-
matching_names.append(name)
427+
matching_names.add(name)
428428
except Exception as e:
429429
logging.error(f"Failed to get attribute {attribute_name} from class {cls}")
430430
logging.error(e)
431431

432-
return matching_names
432+
return list(matching_names)
433433

434434

435435
def get_matching_class_attribute_name(
@@ -919,7 +919,7 @@ def search_attribute_matching_name_with_path(
919919
# next_match = ".".join(attrib_list[1:])
920920
current_match, next_match = path_next_attribute(name_rgx)
921921
if current_match is None:
922-
logging.error(f"Attribute name regex '{name_rgx}' is invalid.")
922+
# logging.error(f"Attribute name regex '{name_rgx}' is invalid.")
923923
return []
924924
res = []
925925

@@ -949,15 +949,18 @@ def search_attribute_matching_name_with_path(
949949
else:
950950
not_match_path_and_obj.append((f"{current_path}{k}", s_o))
951951
elif not is_primitive(obj):
952+
current_match = current_match.replace("\\.", ".")
952953
# logging.debug(f"searching {current_match} in {type(obj)} with path {current_path} and next match {next_match}")
953954
match_values = get_all_matching_class_attribute_name(obj, current_match, re_flags)
954955
for match_value in match_values:
956+
# logging.debug(f"\tmatch found : {match_value}")
955957
match_path_and_obj.append(
956958
(
957959
f"{current_path}{match_value}",
958960
get_object_attribute_no_verif(obj, match_value),
959961
)
960962
)
963+
# logging.debug("f------")
961964
for att_name in get_class_attributes(obj):
962965
if att_name not in match_values:
963966
not_match_path_and_obj.append(

0 commit comments

Comments
 (0)