Skip to content

Commit

Permalink
Fixed broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherSpelt committed Nov 20, 2024
1 parent 2975896 commit d44e51d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion amt/schema/ai_act_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class AiActProfile(BaseModel):
type: str | None = Field(default=None)
open_source: str | None = Field(default=None)
risk_category: str | None = Field(default=None)
publication_category: str | None = Field(default=None)
systemic_risk: str | None = Field(default=None)
transparency_obligations: str | None = Field(default=None)
role: list[str] | str | None = Field(default=None)
Expand Down
2 changes: 1 addition & 1 deletion amt/services/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def create(self, algorithm_new: AlgorithmNew) -> Algorithm:
ai_act_profile = AiActProfile(
type=algorithm_new.type,
open_source=algorithm_new.open_source,
risk_category=algorithm_new.publication_category,
publication_category=algorithm_new.publication_category,
systemic_risk=algorithm_new.systemic_risk,
transparency_obligations=algorithm_new.transparency_obligations,
role=algorithm_new.role,
Expand Down
24 changes: 19 additions & 5 deletions amt/services/task_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def is_requirement_applicable(requirement: Requirement, ai_act_profile: AiActPro
- Always applicable requirements automatically return True.
- For the 'role' attribute, handles compound values like
"gebruiksverantwoordelijke + aanbieder".
- For the 'systemic_risk' attribute, handles the old name 'publication_category'.
- A requirement is applicable if all specified attributes match or have no
specific restrictions.
"""
Expand All @@ -25,15 +26,22 @@ def is_requirement_applicable(requirement: Requirement, ai_act_profile: AiActPro

# We can assume the ai_act_profile field always contains exactly 1 element.
requirement_profile = requirement.ai_act_profile[0]
comparison_attrs = ["type", "risk_category", "type", "open_source", "systemic_risk", "transparency_obligations"]
comparison_attrs = [
"type",
"risk_category",
"type",
"open_source",
"systemic_risk",
"transparency_obligations",
]

for attr in comparison_attrs:
requirement_attr_values = getattr(requirement_profile, attr, [])

if not requirement_attr_values:
continue

input_value = _parse_attribute_values(attr, getattr(ai_act_profile, attr))
input_value = _parse_attribute_values(attr, ai_act_profile)

if not input_value & {attr_value.value for attr_value in requirement_attr_values}:
return False
Expand Down Expand Up @@ -65,8 +73,14 @@ async def get_requirements_and_measures(
return applicable_requirements, applicable_measures


def _parse_attribute_values(attr: str, raw_input_value: str) -> set[str]:
def _parse_attribute_values(attr: str, ai_act_profile: AiActProfile) -> set[str]:
"""
Helper function needed in `is_requirement_applicable`, handling special case for `role`.
Helper function needed in `is_requirement_applicable`, handling special case for `role`
and `publication_category`.
"""
return {raw_input_value} if attr != "role" else {s.strip() for s in raw_input_value.split("+")}
if attr == "role":
return {s.strip() for s in getattr(ai_act_profile, attr, "").split("+")}
if attr == "risk_category":
return {getattr(ai_act_profile, "publication_category", "")}

return {getattr(ai_act_profile, attr, "")}
2 changes: 1 addition & 1 deletion tests/api/routes/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ async def test_post_new_algorithms_write_system_card(
ai_act_profile = AiActProfile(
type=algorithm_new.type,
open_source=algorithm_new.open_source,
risk_category=algorithm_new.publication_category,
publication_category=algorithm_new.publication_category,
systemic_risk=algorithm_new.systemic_risk,
transparency_obligations=algorithm_new.transparency_obligations,
role=algorithm_new.role,
Expand Down
18 changes: 9 additions & 9 deletions tests/schema/test_schema_ai_act_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ def test_ai_act_profile_schema_create_new():
algorithm_new = AiActProfile(
type="AI-systeem",
open_source="open-source",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
systemic_risk="systeemrisico",
transparency_obligations="transparantieverplichtingen",
role="aanbieder",
)
assert algorithm_new.type == "AI-systeem"
assert algorithm_new.open_source == "open-source"
assert algorithm_new.risk_category == "hoog-risico AI"
assert algorithm_new.publication_category == "hoog-risico AI"
assert algorithm_new.systemic_risk == "systeemrisico"
assert algorithm_new.transparency_obligations == "transparantieverplichtingen"
assert algorithm_new.role == "aanbieder"
Expand All @@ -23,14 +23,14 @@ def test_ai_act_profile_schema_create_new_no_role():
algorithm_new = AiActProfile(
type="AI-systeem",
open_source="open-source",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
systemic_risk="systeemrisico",
transparency_obligations="transparantieverplichtingen",
role=None,
)
assert algorithm_new.type == "AI-systeem"
assert algorithm_new.open_source == "open-source"
assert algorithm_new.risk_category == "hoog-risico AI"
assert algorithm_new.publication_category == "hoog-risico AI"
assert algorithm_new.systemic_risk == "systeemrisico"
assert algorithm_new.transparency_obligations == "transparantieverplichtingen"
assert algorithm_new.role is None
Expand All @@ -40,14 +40,14 @@ def test_ai_act_profile_schema_create_new_empty_role_list():
algorithm_new = AiActProfile(
type="AI-systeem",
open_source="open-source",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
systemic_risk="systeemrisico",
transparency_obligations="transparantieverplichtingen",
role=[],
)
assert algorithm_new.type == "AI-systeem"
assert algorithm_new.open_source == "open-source"
assert algorithm_new.risk_category == "hoog-risico AI"
assert algorithm_new.publication_category == "hoog-risico AI"
assert algorithm_new.systemic_risk == "systeemrisico"
assert algorithm_new.transparency_obligations == "transparantieverplichtingen"
assert algorithm_new.role is None
Expand All @@ -57,14 +57,14 @@ def test_ai_act_profile_schema_create_new_double_role():
algorithm_new = AiActProfile(
type="AI-systeem",
open_source="open-source",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
systemic_risk="systeemrisico",
transparency_obligations="transparantieverplichtingen",
role=["aanbieder", "gebruiksverantwoordelijke"],
)
assert algorithm_new.type == "AI-systeem"
assert algorithm_new.open_source == "open-source"
assert algorithm_new.risk_category == "hoog-risico AI"
assert algorithm_new.publication_category == "hoog-risico AI"
assert algorithm_new.systemic_risk == "systeemrisico"
assert algorithm_new.transparency_obligations == "transparantieverplichtingen"
assert algorithm_new.role == "aanbieder + gebruiksverantwoordelijke"
Expand All @@ -75,7 +75,7 @@ def test_ai_act_profile_schema_create_new_too_many_roles():
AiActProfile(
type="AI-systeem",
open_source="open-source",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
systemic_risk="systeemrisico",
transparency_obligations="transparantieverplichtingen",
role=["aanbieder", "gebruiksverantwoordelijke", "I am too much of a role"],
Expand Down
8 changes: 4 additions & 4 deletions tests/services/test_task_registry_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_is_requirement_applicable_with_profile():
# given
ai_act_profile = AiActProfile(
type="AI-systeem",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
role="gebruiksverantwoordelijke",
open_source="open-source",
systemic_risk="systeemrisico",
Expand Down Expand Up @@ -112,7 +112,7 @@ async def test_is_requirement_applicable_with_profile_with_2_roles():
# given
ai_act_profile = AiActProfile(
type="AI-model voor algemene doeleinden",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
role="aanbieder + gebruiksverantwoordelijke",
open_source="geen open-source",
systemic_risk="systeemrisico",
Expand Down Expand Up @@ -154,7 +154,7 @@ async def test_is_requirement_applicable_with_non_matching_profile():
# given
ai_act_profile = AiActProfile(
type="AI-model voor algemene doeleinden",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
role="aanbieder + gebruiksverantwoordelijke",
open_source="geen open-source",
systemic_risk="systeemrisico",
Expand Down Expand Up @@ -196,7 +196,7 @@ async def test_is_requirement_applicable_with_matching_profile():
# given
ai_act_profile = AiActProfile(
type="AI-model voor algemene doeleinden",
risk_category="hoog-risico AI",
publication_category="hoog-risico AI",
role="aanbieder",
open_source="geen open-source",
systemic_risk="systeemrisico",
Expand Down

0 comments on commit d44e51d

Please sign in to comment.