Skip to content

Commit

Permalink
Add API for direct heatmapping (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrhill authored Dec 6, 2023
1 parent ca102f9 commit adb9f72
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/heatmaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,23 @@ function heatmap(expl::Explanation; kwargs...)
)
end

"""
heatmap(input, analyzer)
Compute an `Explanation` for a given `input` using the method `analyzer` and visualize it
as a vision heatmap.
Any additional arguments and keyword arguments are passed to the analyzer.
Refer to [`analyze`](@ref) for more information on available keyword arguments.
To customize the heatmapping style, first compute an explanation using [`analyze`](@ref)
and then call [`heatmap`](@ref) on the explanation.
"""
function heatmap(input, analyzer::AbstractXAIMethod, args...; kwargs...)
expl = analyze(input, analyzer, args...; kwargs...)
return heatmap(expl)
end

#===============#
# Text Heatmaps #
#===============#
Expand Down
21 changes: 21 additions & 0 deletions test/test_api.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,24 @@ expl = analyzer(input, output_neuron)
# Ouput selection + add_batch_dim
expl = analyzer(input_vec, output_neuron; add_batch_dim=true)
@test expl.val == val[:, 1:1]

# Test direct heatmapping
input = rand(5, 5, 3, 1)

h1 = heatmap(analyze(input, analyzer))
h2 = heatmap(input, analyzer)
@test h1 == h2

h1 = heatmap(analyze(input, analyzer, 5))
h2 = heatmap(input, analyzer, 5)
@test h1 == h2

input = rand(5, 5, 3)

h1 = heatmap(analyze(input, analyzer; add_batch_dim=true))
h2 = heatmap(input, analyzer; add_batch_dim=true)
@test h1 == h2

h1 = heatmap(analyze(input, analyzer, 5; add_batch_dim=true))
h2 = heatmap(input, analyzer, 5; add_batch_dim=true)
@test h1 == h2

0 comments on commit adb9f72

Please sign in to comment.