Skip to content

Commit

Permalink
#562: add decorator for entity properties
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Dec 19, 2024
1 parent 9fdfe7a commit 4235d04
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/lbaf/Applications/LBAF_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,19 @@ def __print_qoi(self) -> int:
rank_qois = r.get_qois()
o = Object(seq_id=0)
object_qois = o.get_qois()
object_qois.update(o.get_entity_properties())

# Print QOI based on verbosity level
if verbosity > 0:
self.__logger.info("List of Implemented QOI:")
if verbosity == 1:
self.__logger.info("\tRank QOI:")
for name, _ in rank_qois.items():
self.__logger.info("\t" + name)
self.__logger.info("\t\t" + name)
elif verbosity > 1:
self.__logger.info("\tRank QOI:")
for name, _ in rank_qois.items():
self.__logger.info("\t" + name)
self.__logger.info("\t\t" + name)
self.__logger.info("")
self.__logger.info("\tObject QOI:")
for name, _ in object_qois.items():
Expand Down
26 changes: 12 additions & 14 deletions src/lbaf/IO/lbsVTDataWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ def __init__(
def __create_tasks(self, rank_id, objects, migratable):
"""Create per-object entries to be outputted to JSON."""
tasks = []
entity_qoi = ["id", "seq_id", "collection_id", "index"]
exclude_from_user_defined = ["packed_id", "load", "shared_id"]

for o in objects:
task_data = {
Expand All @@ -117,31 +115,31 @@ def __create_tasks(self, rank_id, objects, migratable):
"time": o.get_load()
}

entity_properties = o.get_entity_properties()
for prop_name, prop_getter in entity_properties.items():
if prop_getter() is not None and prop_name != "packed_id":
task_data["entity"][prop_name] = prop_getter()

unused_params = o.get_unused_params()
if unused_params:
task_data["entity"].update(unused_params)

task_data["entity"] = dict(sorted(task_data["entity"].items()))

user_defined = o.get_user_defined()
if user_defined:
task_data["user_defined"] = dict(sorted(user_defined.items()))
else:
task_data["user_defined"] = dict()
task_data["user_defined"] = {}

object_qois = o.get_qois()

for qoi_name, qoi_getter in object_qois.items():
if qoi_name in entity_qoi and qoi_getter() is not None:
task_data["entity"][qoi_name] = qoi_getter()
elif qoi_name not in (entity_qoi + exclude_from_user_defined):
if qoi_getter() is not None:
task_data["user_defined"][qoi_name] = qoi_getter()
else:
task_data["user_defined"][qoi_name] = -1

if o.get_shared_id() is not None:
task_data["user_defined"]["shared_id"] = o.get_shared_id()
else:
task_data["user_defined"]["shared_id"] = -1

task_data["entity"] = dict(sorted(task_data["entity"].items()))

# Append data for current object
tasks.append(task_data)

# Return created tasks on this rank
Expand Down
39 changes: 24 additions & 15 deletions src/lbaf/Model/lbsObject.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

from .lbsBlock import Block
from .lbsObjectCommunicator import ObjectCommunicator
from .lbsQOIDecorator import qoi
from .lbsQOIDecorator import qoi, property

Check warning on line 47 in src/lbaf/Model/lbsObject.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Redefining built-in 'property' (redefined-builtin)

class Object:
"""A class representing an object with load and communicator
Expand Down Expand Up @@ -158,22 +158,22 @@ def __init__(
def __repr__(self):
return f"Object id: {self.get_id()}, load: {self.__load}"

@qoi
@property
def get_id(self) -> int:
"""Return object bit-packed ID if available. Else return the object seq ID"""
return self.__packed_id if self.__packed_id is not None else self.__seq_id

@qoi
@property
def get_packed_id(self) -> Optional[int]:
"""Return object bit-packed ID (seq_id, home_id, migratable)."""
return self.__packed_id

@qoi
@property
def get_seq_id(self) -> Optional[int]:
"""Return object seq ID."""
return self.__seq_id

@qoi
@property
def get_collection_id(self) -> Optional[int]:
"""Return object collection ID (required for migratable objects)."""
return self.__collection_id
Expand All @@ -182,7 +182,7 @@ def set_collection_id(self, collection_id: Optional[int]):
""" Set object collection ID (required for migratable objects)."""
self.__collection_id = collection_id

@qoi
@property
def get_index(self) -> Optional[list]:
"""Return the object's index."""
return self.__index
Expand Down Expand Up @@ -295,20 +295,29 @@ def get_unused_params(self) -> dict:
"""Return all current unused parameters."""
return self.__unused_params

def __get_qoi_name(self, qoi_ftn) -> str:
def __get_property_or_qoi_name(self, ftn_name) -> str:
"""Return the QOI name from the given QOI getter function"""
qoi = qoi_ftn[4:] if qoi_ftn.startswith("get_") else qoi_ftn
if qoi == "size":
qoi = "object_memory"
elif qoi == "overhead":
qoi = "overhead_memory"
return qoi
name = ftn_name[4:] if ftn_name.startswith("get_") else ftn_name
if name == "size":
name = "object_memory"
elif name == "overhead":
name = "overhead_memory"
return name

def get_entity_properties(self) -> list:
"""Get all methods decorated with the 'property' decorator.
"""
property_methods : dict = {
self.__get_property_or_qoi_name(name): getattr(self, name)
for name in dir(self)
if callable(getattr(self, name)) and not name.startswith("__") and hasattr(getattr(self, name), "is_property") }
return property_methods

def get_qois(self) -> list:
"""Get all methods decorated with the QOI decorator.
"""Get all methods decorated with 'qoi' decorator.
"""
qoi_methods : dict = {
self.__get_qoi_name(name): getattr(self, name)
self.__get_property_or_qoi_name(name): getattr(self, name)
for name in dir(self)
if callable(getattr(self, name)) and not name.startswith("__") and hasattr(getattr(self, name), "is_qoi") }
return qoi_methods
8 changes: 8 additions & 0 deletions src/lbaf/Model/lbsQOIDecorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ def decorated_func(*args, **kwargs):
return return_value
decorated_func.is_qoi = True
return decorated_func

def property(func, *args, **kwargs):

Check warning on line 52 in src/lbaf/Model/lbsQOIDecorator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Redefining built-in 'property' (redefined-builtin)

Check warning on line 52 in src/lbaf/Model/lbsQOIDecorator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Unused argument 'args' (unused-argument)

Check warning on line 52 in src/lbaf/Model/lbsQOIDecorator.py

View workflow job for this annotation

GitHub Actions / code-quality (ubuntu-latest, 3.8)

Unused argument 'kwargs' (unused-argument)
"""Decorator function to wrap getters that will be used as entity properties"""
def decorated_func(*args, **kwargs):
return_value = func(*args, **kwargs)
return return_value
decorated_func.is_property = True
return decorated_func

0 comments on commit 4235d04

Please sign in to comment.