-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Continues work started in Julia-XAI/XAIBase.jl#16 and Julia-XAI/VisionHeatmaps.jl#7 by moving `heatmap` methods on `Explanation` type to TextHeatmaps.jl via package extensions on XAIBase.
- Loading branch information
Showing
15 changed files
with
187 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,27 @@ | ||
name = "TextHeatmaps" | ||
uuid = "2dd6718a-6083-4824-b9f7-90e4a57f72d2" | ||
authors = ["Adrian Hill <gh@adrianhill.de>"] | ||
version = "1.1.0" | ||
version = "1.2.0-DEV" | ||
|
||
[deps] | ||
ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" | ||
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" | ||
Crayons = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f" | ||
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93" | ||
Requires = "ae029012-a4dd-5104-9daa-d747884805df" | ||
XAIBase = "9b48221d-a747-4c1b-9860-46a1d8ba24a7" | ||
|
||
[weakdeps] | ||
XAIBase = "9b48221d-a747-4c1b-9860-46a1d8ba24a7" | ||
|
||
[extensions] | ||
TextHeatmapsXAIBaseExt = "XAIBase" | ||
|
||
[compat] | ||
ColorSchemes = "3" | ||
Colors = "0.12" | ||
Crayons = "4" | ||
FixedPointNumbers = "0.8" | ||
Requires = "1" | ||
XAIBase = "3" | ||
julia = "1.6" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module TextHeatmapsXAIBaseExt | ||
|
||
using TextHeatmaps, XAIBase | ||
|
||
struct HeatmapConfig | ||
colorscheme::Symbol | ||
reduce::Symbol | ||
rangescale::Symbol | ||
end | ||
|
||
const DEFAULT_COLORSCHEME = :seismic | ||
const DEFAULT_REDUCE = :sum | ||
const DEFAULT_RANGESCALE = :centered | ||
const DEFAULT_HEATMAP_PRESET = HeatmapConfig( | ||
DEFAULT_COLORSCHEME, DEFAULT_REDUCE, DEFAULT_RANGESCALE | ||
) | ||
|
||
const HEATMAP_PRESETS = Dict{Symbol,HeatmapConfig}( | ||
:attribution => HeatmapConfig(:seismic, :sum, :centered), | ||
:sensitivity => HeatmapConfig(:grays, :norm, :extrema), | ||
:cam => HeatmapConfig(:jet, :sum, :extrema), | ||
) | ||
|
||
# Select HeatmapConfig preset based on heatmapping style in Explanation | ||
function get_heatmapping_config(heatmap::Symbol) | ||
return get(HEATMAP_PRESETS, heatmap, DEFAULT_HEATMAP_PRESET) | ||
end | ||
|
||
# Override HeatmapConfig preset with keyword arguments | ||
function get_heatmapping_config(expl::Explanation; kwargs...) | ||
c = get_heatmapping_config(expl.heatmap) | ||
|
||
colorscheme = get(kwargs, :colorscheme, c.colorscheme) | ||
rangescale = get(kwargs, :rangescale, c.rangescale) | ||
reduce = get(kwargs, :reduce, c.reduce) | ||
return HeatmapConfig(colorscheme, reduce, rangescale) | ||
end | ||
|
||
""" | ||
heatmap(explanation, text) | ||
Visualize [`Explanation`](@ref) from XAIBase as text heatmap. | ||
Text should be a vector containing vectors of strings, one for each input in the batched explanation. | ||
## Keyword arguments | ||
- `colorscheme::Union{ColorScheme,Symbol}`: color scheme from ColorSchemes.jl. | ||
Defaults to `:$DEFAULT_COLORSCHEME`. | ||
- `rangescale::Symbol`: selects how the color channel reduced heatmap is normalized | ||
before the color scheme is applied. Can be either `:extrema` or `:centered`. | ||
Defaults to `:$DEFAULT_RANGESCALE` for use with the default color scheme `:$DEFAULT_COLORSCHEME`. | ||
""" | ||
function TextHeatmaps.heatmap( | ||
expl::Explanation, texts::AbstractVector{<:AbstractVector{<:AbstractString}}; kwargs... | ||
) | ||
ndims(expl.val) != 2 && throw( | ||
ArgumentError( | ||
"To heatmap text, `explanation.val` must be 2D array of shape `(input_length, batchsize)`. Got array of shape $(size(x)) instead.", | ||
), | ||
) | ||
batchsize = size(expl.val, 2) | ||
textsize = length(texts) | ||
batchsize != textsize && throw( | ||
ArgumentError("Batchsize $batchsize doesn't match number of texts $textsize.") | ||
) | ||
|
||
c = get_heatmapping_config(expl; kwargs...) | ||
return [ | ||
TextHeatmaps.heatmap(v, t; colorscheme=c.colorscheme, rangescale=c.rangescale) for | ||
(v, t) in zip(eachcol(expl.val), texts) | ||
] | ||
end | ||
|
||
function TextHeatmaps.heatmap( | ||
expl::Explanation, text::AbstractVector{<:AbstractString}; kwargs... | ||
) | ||
return heatmap(expl, [text]; kwargs...) | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[97;48;2;0;0;0mTest[0m [97;48;2;113;113;112mText[0m [30;48;2;255;255;253mHeatmap[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[30;48;2;255;255;253manother[0m [97;48;2;113;113;112mdummy[0m [97;48;2;0;0;0minput[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[97;48;2;255;85;85mTest[0m [97;48;2;212;0;0mText[0m [97;48;2;128;0;0mHeatmap[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[97;48;2;0;0;76mTest[0m [30;48;2;255;255;255mText[0m [97;48;2;128;0;0mHeatmap[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[97;48;2;128;0;0manother[0m [97;48;2;170;0;0mdummy[0m [97;48;2;212;0;0minput[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[97;48;2;128;0;0manother[0m [30;48;2;255;255;255mdummy[0m [97;48;2;0;0;76minput[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
words = ["Test", "TextHeatmaps"] | ||
val = [4.2, -1.0] | ||
|
||
colorscheme = TextHeatmaps.seismic | ||
cmin = get(colorscheme, 0) # red | ||
cmax = get(colorscheme, 1) # blue | ||
|
||
# Test default ColorScheme seismic | ||
h = heatmap(val, words) | ||
@test h.colors[1] ≈ cmax | ||
@test h.colors[2] != cmin | ||
@test_reference "references/seismic_centered.txt" repr("text/plain", h) | ||
@test_reference "references/seismic_centered_html.txt" repr("text/html", h) | ||
|
||
h = heatmap(val, words; rangescale=:extrema) | ||
@test h.colors[1] ≈ cmax | ||
@test h.colors[2] ≈ cmin | ||
@test_reference "references/seismic_extrema.txt" repr("text/plain", h) | ||
|
||
# Test other colorschemes | ||
colorscheme = ColorSchemes.inferno | ||
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:centered) | ||
@test_reference "references/inferno_centered.txt" repr("text/plain", h) | ||
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:extrema) | ||
@test_reference "references/inferno_extrema.txt" repr("text/plain", h) | ||
|
||
# Test colorscheme symbols | ||
colorscheme = :inferno | ||
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:centered) | ||
@test_reference "references/inferno_centered.txt" repr("text/plain", h) | ||
h = heatmap(val, words; colorscheme=colorscheme, rangescale=:extrema) | ||
@test_reference "references/inferno_extrema.txt" repr("text/plain", h) | ||
|
||
# Test errors | ||
@test_throws ArgumentError heatmap(val, ["Test", "Text", "Heatmaps"]) | ||
# Test inner constructor | ||
@test_throws ArgumentError TextHeatmaps.TextHeatmap(val, words, [cmin, cmax, cmax]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using XAIBase | ||
|
||
val = output = [1 6; 2 5; 3 4] | ||
text = [["Test", "Text", "Heatmap"], ["another", "dummy", "input"]] | ||
output_selection = [CartesianIndex(1, 2), CartesianIndex(3, 4)] # irrelevant | ||
expl = Explanation(val, output, output_selection, :Gradient, :sensitivity) | ||
h = heatmap(expl, text) | ||
@test_reference "references/Gradient1.txt" repr("text/plain", h[1]) | ||
@test_reference "references/Gradient2.txt" repr("text/plain", h[2]) | ||
|
||
expl = Explanation( | ||
val[:, 1:1], output[:, 1:1], output_selection[1], :Gradient, :sensitivity | ||
) | ||
h = heatmap(expl, text[1]) | ||
@test_reference "references/Gradient1.txt" repr("text/plain", only(h)) | ||
|
||
expl = Explanation(val, output, output_selection, :LRP, :attribution) | ||
h = heatmap(expl, text) | ||
@test_reference "references/LRP1.txt" repr("text/plain", h[1]) | ||
@test_reference "references/LRP2.txt" repr("text/plain", h[2]) | ||
|
||
h = heatmap(expl, text; rangescale=:extrema) | ||
@test_reference "references/LRP1_extrema.txt" repr("text/plain", h[1]) | ||
@test_reference "references/LRP2_extrema.txt" repr("text/plain", h[2]) |