Skip to content

Commit

Permalink
#562: fix pylint warnings; clean up obj qoi getter
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Dec 20, 2024
1 parent 4235d04 commit b595d8f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 47 deletions.
1 change: 0 additions & 1 deletion src/lbaf/Applications/LBAF_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ 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:
Expand Down
5 changes: 1 addition & 4 deletions src/lbaf/Execution/lbsAlgorithmBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@
#@HEADER
#
import abc
import os
from typing import Set, List
from typing import List

from ..IO.lbsStatistics import compute_function_statistics
from ..Model.lbsRank import Rank
from ..Model.lbsObject import Object
from ..Model.lbsPhase import Phase
from ..Model.lbsWorkModelBase import WorkModelBase
from ..Utils.lbsLogging import Logger
Expand Down
1 change: 0 additions & 1 deletion src/lbaf/Execution/lbsInformAndTransferAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#
import random
import time
from typing import Set
from logging import Logger

from .lbsAlgorithmBase import AlgorithmBase
Expand Down
2 changes: 1 addition & 1 deletion src/lbaf/IO/lbsVTDataReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def _populate_rank(self, phase_id: int, rank_id: int) -> Tuple[Rank,dict]:

# Add communications to the object
rank_comm = {}
if (communications := phase.get("communications")):
if (communications := phase.get("communications")): # pylint:disable=W0631:undefined-loop-variable
if phase_id in self.__communications_dict:
self.__communications_dict[phase_id][rank_id] = communications
else:
Expand Down
9 changes: 5 additions & 4 deletions src/lbaf/IO/lbsVTDataWriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def __create_tasks(self, rank_id, objects, migratable):
"time": o.get_load()
}

entity_properties = o.get_entity_properties()
entity_properties = o.get_qois("entity_property")
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()
Expand All @@ -132,7 +132,7 @@ def __create_tasks(self, rank_id, objects, migratable):
else:
task_data["user_defined"] = {}

object_qois = o.get_qois()
object_qois = o.get_qois("qoi")
for qoi_name, qoi_getter in object_qois.items():
if qoi_getter() is not None:
task_data["user_defined"][qoi_name] = qoi_getter()
Expand All @@ -155,15 +155,16 @@ def __create_task_data(self, rank: Rank):
key=lambda x: x.get("entity").get(
"id", x.get("entity").get("seq_id")))

def __find_object_rank(self, phase: Phase, object: Object):
def __find_object_rank(self, phase: Phase, obj: Object):
"""Determine which rank owns the object."""
for r in phase.get_ranks():
if object in r.get_objects():
if obj in r.get_objects():
return r

# If this point is reached the object could not be found
self.__logger.error(
f"Object id {object} cannot be located in any rank of phase {phase.get_id()}")
raise SystemExit(1)

def __get_communications(self, phase: Phase, rank: Rank):

Check notice on line 169 in src/lbaf/IO/lbsVTDataWriter.py

View workflow job for this annotation

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

Too many local variables (19/15) (too-many-locals)

Check notice on line 169 in src/lbaf/IO/lbsVTDataWriter.py

View workflow job for this annotation

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

Too many branches (15/12) (too-many-branches)
"""Create communication entries to be outputted to JSON."""
Expand Down
8 changes: 1 addition & 7 deletions src/lbaf/Model/lbsBlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,10 @@ def __init__(
o_ids = set()

# Block index
# if not isinstance(b_id, int) or isinstance(b_id, bool):
# raise TypeError(
# f"b_id: incorrect type {type(b_id)}")
self.__index = int(b_id)

# Rank to which block is initially assigned
if not isinstance(h_id, int) or isinstance(h_id, bool):
raise TypeError(
f"h_id: incorrect type {type(h_id)}")
self.__home_id = h_id
self.__home_id = int(h_id)

# Nonnegative size required to for memory footprint of this block
if not isinstance(size, float) or size < 0.0:
Expand Down
49 changes: 29 additions & 20 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, property
from .lbsQOIDecorator import qoi, entity_property

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}"

@property
@entity_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

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

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

@property
@entity_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

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

def __get_property_or_qoi_name(self, ftn_name) -> str:
def __get_qoi_name(self, ftn_name) -> str:
"""Return the QOI name from the given QOI getter function"""
name = ftn_name[4:] if ftn_name.startswith("get_") else ftn_name
if name == "size":
Expand All @@ -304,20 +304,29 @@ def __get_property_or_qoi_name(self, ftn_name) -> str:
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 'qoi' decorator.
def __get_qoi_type(self, qoi_type: str) -> list:
"""
Returns a dict of the specified qoi_type (either 'qoi' or
'entity_property'.)"""
attr = f"is_{qoi_type}"
qoi_methods : dict = {
self.__get_property_or_qoi_name(name): getattr(self, name)
self.__get_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") }
if callable(getattr(self, name)) and \
not name.startswith("__") and \
hasattr(getattr(self, name), attr) }
return qoi_methods

def get_qois(self, qoi_type=None):
"""Get all methods with specified 'qoi_type' decorator.
Params:
- qoi_type: None (default) returns all decorated functions.
"entity_property" returns all entity properties
"qoi" returns all derived quantities of interest
"""
if qoi_type is not None:
return self.__get_qoi_type(qoi_type)
qois = self.__get_qoi_type("qoi")
qois.update(self.__get_qoi_type("entity_property"))
return qois
4 changes: 2 additions & 2 deletions src/lbaf/Model/lbsQOIDecorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def decorated_func(*args, **kwargs):
decorated_func.is_qoi = True
return decorated_func

def property(func, *args, **kwargs):
def entity_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)

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
decorated_func.is_entity_property = True
return decorated_func
6 changes: 4 additions & 2 deletions src/lbaf/Model/lbsRank.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def get_migratable_objects(self) -> set:

@qoi
def get_number_of_migratable_objects(self) -> int:
"""Return the number of migratable objects assigned to rank."""
return len(self.__migratable_objects)

def add_sentinel_object(self, o: Object) -> None:
Expand All @@ -236,6 +237,7 @@ def get_sentinel_objects(self) -> set:

@qoi
def get_number_of_sentinel_objects(self) -> int:
"""Return the number of sentinel objects assigned to rank."""
return len(self.__sentinel_objects)

def get_object_ids(self) -> list:
Expand Down Expand Up @@ -340,8 +342,8 @@ def get_max_memory_usage(self) -> float:

def __get_qoi_name(self, qoi_ftn) -> str:
"""Return the QOI name from the given QOI getter function"""
qoi = qoi_ftn[4:] if qoi_ftn.startswith("get_") else qoi_ftn
return qoi.replace("number_of", "num")
qoi_name = qoi_ftn[4:] if qoi_ftn.startswith("get_") else qoi_ftn
return qoi_name.replace("number_of", "num")

def get_qois(self) -> list:
"""Get all methods decorated with the QOI decorator.
Expand Down
5 changes: 0 additions & 5 deletions tests/unit/Model/test_lbs_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ def setUp(self):
self.block = Block(self.b_id, self.h_id, self.size, self.o_ids)

def test_lbs_block_initialization(self):
wrong_hid = 1.0
with self.assertRaises(TypeError) as err:
block_wrong_hid = Block(b_id=1, h_id=wrong_hid)
self.assertEqual(str(err.exception), f"h_id: incorrect type {type(wrong_hid)}")

wrong_sizes = [1, -1.0]
for wrong_size in wrong_sizes:
with self.assertRaises(TypeError) as err:
Expand Down

0 comments on commit b595d8f

Please sign in to comment.