diff --git a/test/REQUIRE b/test/REQUIRE index 62fc744..f5c7e27 100644 --- a/test/REQUIRE +++ b/test/REQUIRE @@ -1,4 +1,4 @@ Test -Plots +Makie BenchmarkTools CSV diff --git a/test/spatial_pooler_test.jl b/test/spatial_pooler_test.jl index 54625c1..44e40ed 100644 --- a/test/spatial_pooler_test.jl +++ b/test/spatial_pooler_test.jl @@ -7,6 +7,7 @@ module HTMt using BenchmarkTools using CSV using Printf +using Makie import Random.seed! seed!(0) @@ -26,8 +27,10 @@ display_evaluation(t,sp,sp_activity,spDims)= begin #heatmap(@> sp.b.a_Tmean reshape(64,32))|> display #heatmap(sp.proximalSynapses.synapses)|> display end -process_data!(encHistory,spHistory,encANDspHistory,tN,data,encParams,sp)= - for t in 1:tN +function process_data!(encHistory,spHistory,encANDspHistory,tN,data,encParams,sp) + AbstractPlotting.inline!(true) + scene= Scene() + for t in 1:tN÷4 z,a= _process_sp(t,tN,data,encParams,sp,display_evaluation) encHistory[:,t]= z; spHistory[:,t]= a # when were the most similar SDRs to z,a in history? These indices correspond to times @@ -36,9 +39,10 @@ process_data!(encHistory,spHistory,encANDspHistory,tN,data,encParams,sp)= encOnlyIdx= setdiff(similarEncIdx, similarSpIdx) spOnlyIdx= setdiff(similarSpIdx, similarEncIdx) encANDspHistory[t]= (encANDsp= intersect(similarEncIdx,similarSpIdx), Nenc= length(similarEncIdx)) - t%10==0 && plot_ts_similarEncSp(t,data.power_hourly_kw, - encOnlyIdx,spOnlyIdx,encANDspHistory) + t%80==0 && plot_ts_similarEncSp(t,data.power_hourly_kw, + encOnlyIdx,spOnlyIdx,encANDspHistory,scene) end +end # Define Spatial Pooler inputDims= ((16*25,5*25,3*25),) @@ -59,7 +63,7 @@ sp= SpatialPooler(SPParams( data,tN= read_gympower() encParams= initenc_powerDay(data.power_hourly_kw, data.hour, data.is_weekend, encoder_size=inputDims[1], w=(21,27,27)) -using Plots; gr() + encHistory= falses(map(sum,inputDims)|>prod,tN) spHistory= falses(spDims|>prod,tN) encANDspHistory= Vector{NamedTuple{(:encANDsp,:Nenc),Tuple{Vector{Int},Int}}}(undef,tN) diff --git a/test/utils/utils.jl b/test/utils/utils.jl index 302a13f..79c53ef 100644 --- a/test/utils/utils.jl +++ b/test/utils/utils.jl @@ -30,28 +30,35 @@ top_similar_sdr(sdrHist,k)= begin end -plot_ts_similarEncSp(t,ts,encOnly,spOnly,encANDspHistory)= - if t>1 _plot_ts_similarEncSP(t,ts,encOnly,spOnly,encANDspHistory) - end -function _plot_ts_similarEncSP(t,ts,encOnly,spOnly,encANDspHistory) +plot_ts_similarEncSp(t,ts,encOnly,spOnly,encANDspHistory,scene::Scene=Scene())= + t>1 ? _plot_ts_similarEncSP(scene,t,ts,encOnly,spOnly,encANDspHistory) : nothing +function _plot_ts_similarEncSP(scene,t,ts,encOnly,spOnly,encANDspHistory) graph_ts()= begin - graph_ts= plot(ts, label="power", dpi=192, ylims=(minimum(ts),maximum(ts))) - scatter!(graph_ts, encOnly, ts[encOnly], label="encoding only") - scatter!(graph_ts, spOnly, ts[spOnly], label="SP only") - scatter!(graph_ts, encANDspHistory[t][1], ts[encANDspHistory[t][1]], - label="overlapping encoding & SP", legendfontsize=6) - vline!(graph_ts,[t], label="") - title!(graph_ts, "Spatial Pooler mapping property evaluation", titlefont=font(12)) + axislims= FRect(1, 0.9minimum(ts), length(ts), 1.05maximum(ts)-0.9minimum(ts)) + graph_ts= lines(ts, color=:blue, limits=axislims) + scatter!(graph_ts, encOnly, ts[encOnly], markersize=18, alpha=0.8, color=:red) + scatter!(graph_ts, spOnly, ts[spOnly], markersize=18, alpha=0.8, color=:yellow) + scatter!(graph_ts, encANDspHistory[t][1], ts[encANDspHistory[t][1]], markersize=18, + alpha=0.8, color=:green) + lines!(graph_ts,[t,t],[minimum(ts),maximum(ts)], limits=axislims) + lgd= legend([graph_ts[2],graph_ts[3],graph_ts[4],graph_ts[5]], + ["power", "encoding only", "SP only", "overlapping"], + camera=campixel!, raw=true) + graph_ts= vbox(graph_ts,lgd) return graph_ts end graph_ovp(overlapFraction)= begin - graph_ovp= plot(1:t,overlapFraction*100, label="", linestyle=:dash, shape=:circle, - msize=3, mcolor=:red, mscolor=:green, dpi=192, - ylabel="overlap%",ylims=(0,100),xlims=(1,length(ts))) - title!(graph_ovp, "Percentage of overlapping encoder and SP SDRs", titlefont=font(12)) + axislims= FRect(1,0,length(ts),101) + graph_ovp= lines(1:t,overlapFraction*100, linestyle=:dash, color=:blue, limits=axislims) + scatter!(graph_ovp, 1:t,overlapFraction*100, markersize=8, alpha=0.2, transparency=true, + strokewidth=3.0, color=:red, limits=axislims) + #ylabel="overlap%",ylims=(0,100),xlims=(1,length(ts))) return graph_ovp end overlapFraction= map(x->length(x.encANDsp)/x.Nenc, encANDspHistory[1:t]) overlapFraction[isnan.(overlapFraction)].= 1.0 - plot(graph_ts(),graph_ovp(overlapFraction), layout=(2,1))|> display + #t1="Spatial Pooler mapping property evaluation" + #t2="Percentage of overlapping encoder and SP SDRs" + scene= hbox(graph_ovp(overlapFraction), graph_ts())|> display + scene end