Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards better inference: bits → nibbles #3808

Draft
wants to merge 57 commits into
base: main
Choose a base branch
from
Draft

Conversation

originalsouth
Copy link
Contributor

@originalsouth originalsouth commented Nov 6, 2024

Changes

Bits → Nibbles

Issue link

N/A

Demo

T.B.D.

QA notes

T.B.D.


Code Checklist

  • All the commits in this PR are properly PGP-signed and verified.
  • This PR only contains functionality relevant to the issue.
  • I have written unit tests for the changes or fixes I made.
  • I have checked the documentation and made changes where necessary.
  • I have performed a self-review of my code and refactored it to the best of my abilities.
  • Tickets have been created for newly discovered issues.
  • For any non-trivial functionality, I have added integration and/or end-to-end tests.
  • I have informed others of any required .env changes files if required and changed the .env-dist accordingly.
  • I have included comments in the code to elaborate on what is not self-evident from the code itself, including references to issues and discussions online, or implicit behavior of an interface.

Checklist for code reviewers:

Copy-paste the checklist from the docs/source/templates folder into your comment.


Checklist for QA:

Copy-paste the checklist from the docs/source/templates folder into your comment.

originalsouth and others added 30 commits August 27, 2024 09:31
…uler from recreating already deleted oois trhough affirmations
…cheduler_from_reacreating_already_deleted_oois_through_affirmations' into feature/nibbles
@originalsouth originalsouth added octopoes Issues related to octopoes bits labels Nov 6, 2024
@originalsouth originalsouth changed the title Better inference: bits → nibbles Towards better inference: bits → nibbles Nov 6, 2024
Copy link
Contributor

@ammar92 ammar92 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! I've provided mostly Python related feedback since I missed the discussion about Nibbles. I'd love to know more about it, why is it better than bits, and what are the main differences? Perhaps we can meet offline for coffee soon?

NIBBLES_DIR = Path(__file__).parent
NIBBLE_ATTR_NAME = "NIBBLE"
NIBBLE_FUNC_NAME = "nibble"
logger = getLogger(__name__)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're now using structlog's logger for logging. See other files for example, but looks pretty much like this:

import structlog

logger = structlog.get_logger(__name__)

return False

def __hash__(self):
return hash(str(self.ooi_type) + self.relation_path if self.relation_path else "\0")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be tricky, since both instances with an empty or unset relation_path could potentially yield the same hash. I suggest using the following:

Suggested change
return hash(str(self.ooi_type) + self.relation_path if self.relation_path else "\0")
return hash(bytes(self.ooi_type) + bytes(self.relation_path))

return hash(str(self.ooi_type) + self.relation_path if self.relation_path else "\0")


class NibbleDefinition:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering, what is the reason this isn't implemented as an e.g. Pydantic class but instead as a POJO-like class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow the Pydantic class does not work well with the importlib yielding the payload... not sure why but it fixed the issues so I moved on -- perhaps hoping one day you would fix it ;)

U = TypeVar("U")


def otype(ooi: OOI) -> type[OOI]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def otype(ooi: OOI) -> type[OOI]:
def object_type(ooi: OOI) -> type[OOI]:

or ooi_type

return type_by_name(ooi.get_ooi_type())


def mergewith(func: Callable[[set[T], set[T]], set[T]], d1: dict[U, set[T]], d2: dict[U, set[T]]) -> dict[U, set[T]]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def mergewith(func: Callable[[set[T], set[T]], set[T]], d1: dict[U, set[T]], d2: dict[U, set[T]]) -> dict[U, set[T]]:
def merge_with(func: Callable[[set[T], set[T]], set[T]], d1: dict[U, set[T]], d2: dict[U, set[T]]) -> dict[U, set[T]]:



def dummy(network: Network):
global NMAX
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
global NMAX

if os.environ.get("CI") != "1":
pytest.skip("Needs XTDB multinode container.", allow_module_level=True)

NMAX = 13
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a more descriptive name here? I'm guessing it has to do with the network name length, so maybe MAX_NAME_LENGTH? (or MAX_NETWORK_NAME_LENGTH)


def _cleared(self, ooi: OOI, valid_time: datetime) -> bool:
ooi_level = self.scan_profile_repository.get(ooi.reference, valid_time).level.value
target_nibbles = list(filter(lambda x: type(ooi) in x.signature, self.nibbles))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since any works on iterables, you don't have to create a list out of the filter object, since the filter object is also an iterable

Suggested change
target_nibbles = list(filter(lambda x: type(ooi) in x.signature, self.nibbles))
target_nibbles = filter(lambda x: type(ooi) in x.signature, self.nibbles)

retval: dict[OOI, dict[str, set[OOI]]] = {}
blockset = set(stack)
self.objects_by_type_cache = {}
if self._cleared(stack[-1], valid_time):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if stack can be empty, but if not it should probably be checked before using it

return any(nibble.min_scan_level < ooi_level for nibble in target_nibbles)

def infer(self, stack: list[OOI], valid_time: datetime) -> dict[OOI, dict[str, set[OOI]]]:
retval: dict[OOI, dict[str, set[OOI]]] = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
retval: dict[OOI, dict[str, set[OOI]]] = {}
return_value: dict[OOI, dict[str, set[OOI]]] = {}

or maybe

Suggested change
retval: dict[OOI, dict[str, set[OOI]]] = {}
inference: dict[OOI, dict[str, set[OOI]]] = {}

@originalsouth originalsouth removed their assignment Nov 13, 2024
Copy link

sonarcloud bot commented Nov 14, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
31.6% Coverage on New Code (required ≥ 80%)
9.8% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bits octopoes Issues related to octopoes
Projects
Status: Incoming features / Need assessment
Development

Successfully merging this pull request may close these issues.

4 participants