File size: 2,003 Bytes
60e41bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from pathlib import Path

from waterleaf.models import TaxonCandidate, VisualAnalysis
from waterleaf.services.identification import IdentificationService


class FakeVision:
    def analyze_images(self, image_paths):
        assert image_paths == [Path("plant.jpg")]
        return VisualAnalysis(
            traits=["purple flower spikes", "narrow gray-green leaves"],
            proposed_names=["Lavandula angustifolia", "Salvia officinalis"],
            is_container=True,
            size_label="medium",
        )

    def rerank(self, image_paths, visual, candidates):
        assert all(candidate.taxon_key for candidate in candidates)
        return [
            {"taxon_key": "lavender", "confidence": 0.91, "rationale": "Flower and leaf match"},
            {"taxon_key": "sage", "confidence": 0.22, "rationale": "Leaf color only"},
            {"taxon_key": "invented", "confidence": 0.99, "rationale": "Must be ignored"},
        ]


class FakeTaxonomy:
    def suggest(self, query, limit=5):
        if query == "Lavandula angustifolia":
            return [
                TaxonCandidate(
                    taxon_key="lavender",
                    scientific_name="Lavandula angustifolia",
                    common_name="English lavender",
                )
            ]
        if query == "Salvia officinalis":
            return [
                TaxonCandidate(
                    taxon_key="sage",
                    scientific_name="Salvia officinalis",
                    common_name="Common sage",
                )
            ]
        return []


def test_identification_only_returns_grounded_reranked_candidates():
    service = IdentificationService(vision=FakeVision(), taxonomy=FakeTaxonomy())

    result = service.identify([Path("plant.jpg")])

    assert [candidate.taxon_key for candidate in result.candidates] == [
        "lavender",
        "sage",
    ]
    assert result.candidates[0].confidence == 0.91
    assert result.visual.is_container is True