|
| 1 | +module TextHeatmapsXAIBaseExt |
| 2 | + |
| 3 | +using TextHeatmaps, XAIBase |
| 4 | + |
| 5 | +struct HeatmapConfig |
| 6 | + colorscheme::Symbol |
| 7 | + reduce::Symbol |
| 8 | + rangescale::Symbol |
| 9 | +end |
| 10 | + |
| 11 | +const DEFAULT_COLORSCHEME = :seismic |
| 12 | +const DEFAULT_REDUCE = :sum |
| 13 | +const DEFAULT_RANGESCALE = :centered |
| 14 | +const DEFAULT_HEATMAP_PRESET = HeatmapConfig( |
| 15 | + DEFAULT_COLORSCHEME, DEFAULT_REDUCE, DEFAULT_RANGESCALE |
| 16 | +) |
| 17 | + |
| 18 | +const HEATMAP_PRESETS = Dict{Symbol,HeatmapConfig}( |
| 19 | + :attribution => HeatmapConfig(:seismic, :sum, :centered), |
| 20 | + :sensitivity => HeatmapConfig(:grays, :norm, :extrema), |
| 21 | + :cam => HeatmapConfig(:jet, :sum, :extrema), |
| 22 | +) |
| 23 | + |
| 24 | +# Select HeatmapConfig preset based on heatmapping style in Explanation |
| 25 | +function get_heatmapping_config(heatmap::Symbol) |
| 26 | + return get(HEATMAP_PRESETS, heatmap, DEFAULT_HEATMAP_PRESET) |
| 27 | +end |
| 28 | + |
| 29 | +# Override HeatmapConfig preset with keyword arguments |
| 30 | +function get_heatmapping_config(expl::Explanation; kwargs...) |
| 31 | + c = get_heatmapping_config(expl.heatmap) |
| 32 | + |
| 33 | + colorscheme = get(kwargs, :colorscheme, c.colorscheme) |
| 34 | + rangescale = get(kwargs, :rangescale, c.rangescale) |
| 35 | + reduce = get(kwargs, :reduce, c.reduce) |
| 36 | + return HeatmapConfig(colorscheme, reduce, rangescale) |
| 37 | +end |
| 38 | + |
| 39 | +""" |
| 40 | + heatmap(explanation, text) |
| 41 | +
|
| 42 | +Visualize [`Explanation`](@ref) from XAIBase as text heatmap. |
| 43 | +Text should be a vector containing vectors of strings, one for each input in the batched explanation. |
| 44 | +
|
| 45 | +## Keyword arguments |
| 46 | +- `colorscheme::Union{ColorScheme,Symbol}`: color scheme from ColorSchemes.jl. |
| 47 | + Defaults to `:$DEFAULT_COLORSCHEME`. |
| 48 | +- `rangescale::Symbol`: selects how the color channel reduced heatmap is normalized |
| 49 | + before the color scheme is applied. Can be either `:extrema` or `:centered`. |
| 50 | + Defaults to `:$DEFAULT_RANGESCALE` for use with the default color scheme `:$DEFAULT_COLORSCHEME`. |
| 51 | +""" |
| 52 | +function TextHeatmaps.heatmap( |
| 53 | + expl::Explanation, texts::AbstractVector{<:AbstractVector{<:AbstractString}}; kwargs... |
| 54 | +) |
| 55 | + ndims(expl.val) != 2 && throw( |
| 56 | + ArgumentError( |
| 57 | + "To heatmap text, `explanation.val` must be 2D array of shape `(input_length, batchsize)`. Got array of shape $(size(x)) instead.", |
| 58 | + ), |
| 59 | + ) |
| 60 | + batchsize = size(expl.val, 2) |
| 61 | + textsize = length(texts) |
| 62 | + batchsize != textsize && throw( |
| 63 | + ArgumentError("Batchsize $batchsize doesn't match number of texts $textsize.") |
| 64 | + ) |
| 65 | + |
| 66 | + c = get_heatmapping_config(expl; kwargs...) |
| 67 | + return [ |
| 68 | + TextHeatmaps.heatmap(v, t; colorscheme=c.colorscheme, rangescale=c.rangescale) for |
| 69 | + (v, t) in zip(eachcol(expl.val), texts) |
| 70 | + ] |
| 71 | +end |
| 72 | + |
| 73 | +function TextHeatmaps.heatmap( |
| 74 | + expl::Explanation, text::AbstractVector{<:AbstractString}; kwargs... |
| 75 | +) |
| 76 | + return heatmap(expl, [text]; kwargs...) |
| 77 | +end |
| 78 | + |
| 79 | +end # module |
0 commit comments