Skip to content

Commit 0557a85

Browse files
committed
Handle the case of invalid slot names in cardinality_scope.
Explicitly look for invalid slot names in the `scope` list given to the `infer_cardinality` method, and forcibly remove them. The presence of invalid slot names does _not_ change the result of cardinality computations, but explicitly looking for such slots allows us to do two things: (1) warn users that they're using invalid slot names; (2) avoid writing invalid slot names in the `cardinality_scope` slot.
1 parent 8ac7dc8 commit 0557a85

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/sssom/util.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,12 @@ def infer_cardinality(self, scope: List[str] = None) -> None:
422422
subjects_by_object: dict[str, set[str]] = {} # Unique subjects for any given object
423423
objects_by_subject: dict[str, set[str]] = {} # Unique objects for any given subject
424424

425+
schema = SSSOMSchemaView()
426+
unknown_slots = [slot for slot in scope if slot not in schema.mapping_slots]
427+
if len(unknown_slots) > 0:
428+
logging.warning(f"Ignoring invalid slot name(s): {unknown_slots}.")
429+
scope = list(set(scope) - set(unknown_slots))
430+
425431
# Helper function to transform a row into a string that represents
426432
# a subject (or object) in a given scope; `side` is either `subject`
427433
# or `object`.

tests/test_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from sssom_schema import slots as SSSOM_Slots
1313

1414
from sssom.constants import (
15+
CARDINALITY_SCOPE,
1516
CREATOR_ID,
1617
MAPPING_CARDINALITY,
1718
OBJECT_ID,
@@ -623,3 +624,13 @@ def test_infer_scoped_cardinality(self) -> None:
623624
msdf.infer_cardinality(["object_source"])
624625
expected = ["1:1", "1:1", "1:1", "1:1", "1:1", "1:1"]
625626
self.assertEqual(expected, list(msdf.df[MAPPING_CARDINALITY].values))
627+
628+
msdf.infer_cardinality(["object_source", "not_a_valid_slot_name"])
629+
# should yield the same result as above
630+
self.assertEqual(expected, list(msdf.df[MAPPING_CARDINALITY].values))
631+
632+
msdf.infer_cardinality(["not_a_valid_slot_name"])
633+
# should be equivalent to an empty scope
634+
expected = ["1:n", "1:n", "1:n", "1:n", "1:n", "1:n"]
635+
self.assertEqual(expected, list(msdf.df[MAPPING_CARDINALITY].values))
636+
self.assertNotIn(CARDINALITY_SCOPE, msdf.df.columns)

0 commit comments

Comments
 (0)