# #1...	Dissimilarity changes
# ##########################################
include("fctSR.jl")
using PyCall
using PyPlot
mutable struct plotSR
    modus::Int64
    file::Int64
    # SEG::SuperResolution.SegmentSR
    # EVAL::SuperResolution.EvalSR

    function plotSR(SEG::SuperResolution.SegmentSR,EVAL::SuperResolution.EvalSR,fileINPUT,modINPUT=5)
        obj=new()
        obj.modus=modINPUT  # 1..original method, 2..AIC ?
        obj.file=fileINPUT
        select_mode=[   :K_KL_estimated_by_mean,                    #   modINPUT=1
                        :K_KL_estimated_by_median,                  #   modINPUT=2
                        :K_KL_estimated_by_max,                     #   modINPUT=3
                        :K_KL_estimated_by_minDelta,                #   modINPUT=4
                        :K_KL_estimated_by_95quantile]              #   modINPUT=5

        print_mode=[    "K_KL_estimated_by_mean",                    #1
                        "K_KL_estimated_by_median",                  #2
                        "K_KL_estimated_by_max",                     #3
                        "K_KL_estimated_by_minDelta",                #4
                        "K_KL_estimated_by_95quantile"]              #5

        H=heatSR(SEG.loc)
        #getfield(EVAL.wModel[1],fieldnames(Main.SuperResolution.ModelSR)[3])
        for IDX in EVAL._selectIDX
            IDX_norm=cartIDX(EVAL._selectIDX,IDX)
            tmpidx=collect(1:length(IDX_norm))[IDX_norm]
            #collect(fieldnames(SuperResolution.ModelSR) .== Symbol(:dim))
            print("*** plotting IDX = $(IDX) = $(tmpidx) ***\n")
            estimatedlocs=EVAL.wModel[IDX_norm][1].loc[Int64.(length.(EVAL.wModel[IDX_norm][1].loc)./2).==getproperty(EVAL, select_mode[modINPUT])[IDX_norm]][1]
            estimatedmolid=EVAL.wModel[IDX_norm][1].member[Int64.(length.(EVAL.wModel[IDX_norm][1].loc)./2).==getproperty(EVAL, select_mode[modINPUT])[IDX_norm]][1]
            estimatedmolid=relabel(estimatedmolid)
            ################################
            size_geom=exp.(exp.(exp.(getproperty.(EVAL.wModel[IDX_norm][1].geom[Int64.(length.(EVAL.wModel[IDX_norm][1].loc)./2).==getproperty(EVAL, select_mode[modINPUT])[IDX_norm]][1], :p))))
            ################################
            plt.figure()
            plt.title("fileINPUT$(obj.file)_modINPUT$(obj.modus)_part($(IDX[1])-$(IDX[2]))_standW_obs$(SEG.N[CartesianIndex(IDX)])_est$(getproperty(EVAL, select_mode[modINPUT])[IDX_norm])_GrndTrth$(EVAL.K_ground_truth[IDX_norm])")
            plt.scatter(SEG.loc[CartesianIndex(IDX)][:,1],SEG.loc[CartesianIndex(IDX)][:,2],label="data",marker="o",
                            s=log.(SEG.loc[CartesianIndex(IDX)][:,3].-minimum(SEG.loc[CartesianIndex(IDX)][:,3])),
                            c=estimatedmolid,alpha=.2)
            plt.scatter(SEG.MU[CartesianIndex(IDX)][:,1],SEG.MU[CartesianIndex(IDX)][:,2],label="means[GrndTrth]",color="green",marker="x",s=50.0,alpha=0.50)
            plt.scatter(SEG.ground_truth[CartesianIndex(IDX)][:,1],SEG.ground_truth[CartesianIndex(IDX)][:,2],label="loc[GrndTrth]",color="orange",marker="x",s=50.0,alpha=.50)
            plt.scatter(estimatedlocs[:,1],estimatedlocs[:,2],label="loc[est]",color="red",marker=".",s=10 .* size_geom,alpha=0.50)
            plt.legend(loc="best")
            aD=plt.gca()
            aD.set_xlabel("x [nm]")
            aD.set_ylabel("y [nm]")
            PyPlot.savefig(joinpath(@__DIR__,string("SR_Plots/",DATUM,"fileINPUT$(obj.file)_modINPUT$(obj.modus)_part($(IDX[1])-$(IDX[2]))_standW_obs$(SEG.N[CartesianIndex(IDX)])_est$(getproperty(EVAL, select_mode[modINPUT])[IDX_norm])_GrndTrth$(EVAL.K_ground_truth[IDX_norm]).pdf")),format="pdf",dpi=300)
        end
    end
end
# #using Plots
# X=Main.SuperResolution.SEGMENTtest.loc
# H=heatSR(X)
#
# Hhist1=map(i->sum(H[i,:]),1:size(H,1))
# Hhist2=map(i->sum(H[:,i]),1:size(H,1))
# n=size(H,1)
# plt[:figure]()
# plt[:title]("part$(1)-$(n)_obs$(sum(H))_est$(0)-sim$(0)")
# plt[:bar](collect(1:n),1 .* Hhist1[:],label="colSum",color="blue",alpha=0.5)#[end-tmpSelect:end],label="minKLD",color="blue",alpha=0.5)
# plt[:bar](collect(1:n),1 .* Hhist2[:],label="rowSum",color="green",alpha=0.5)#[end-tmpSelect:end],label="Δ minKLD",color="green",alpha=0.5)
# plt[:legend](loc="best")
# scl=Int64(ceil(sqrt(n))/2)
# lb=map(i->collect(1:n)[scl*i],1:Int64(floor(n/scl)))
# plt[:xticks](lb,fontsize=3)
# aD=plt[:gca]()
# aD[:set_xlabel]("col/row")
# aD[:set_ylabel]("sum")
# savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/ORI_KLC_part$(1)-$(n)_obs$(sum(H))_est$(0)-sim$(0).pdf")
# # ##########################################
# plt[:figure]()
# plt[:title]("part$(partN)-$(partN2)_obs$(N)_est$(solvNumClust)-sim$(size(unique(𝕄sim),1))")
# plt[:scatter](𝕐sim[:,1],𝕐sim[:,2],label="molID[est]",marker="o",s=30.0,alpha=.2, c=Acoll[solvIDX])
# plt[:scatter](𝛍sim[:,1],𝛍sim[:,2],label="means[sim]",color="green",marker="x",s=50.0,alpha=0.50)
# plt[:scatter](ℝsim[:,1],ℝsim[:,2],label="loc[sim]",color="orange",marker="x",s=50.0,alpha=.50)
# plt[:scatter](𝕐simclAll[solvIDX][:,1],𝕐simclAll[solvIDX][:,2],label="loc[est]",color="red",marker=".",s=10.0,alpha=1.0)
# plt[:legend](loc="best")
# map(i->annotate(AfinalString[i],
# xy=[𝕐simclAll[solvIDX][i,1];𝕐simclAll[solvIDX][i,2]],
# xytext=[𝕐simclAll[solvIDX][i,1];𝕐simclAll[solvIDX][i,2]],fontsize=2.0),1:uniqueLength)
# aD=plt[:gca]()
# aD[:set_xlabel]("x [nm]")
# aD[:set_ylabel]("y [nm]")
# savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/ORI_KLC_part$(partN)-$(partN2)_obs$(N)_est$(solvNumClust)-sim$(size(unique(𝕄sim),1)).pdf",format="pdf",dpi=300)
#
#
# # using pyplot() make the above syntax plt[:figure]() unfunctional..
# pyplot()
# HM=heatmap(H)
# savefig(HM,"/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/01_ORI_KLC_heatmap.pdf")

# #2...	2D Model
# ##########################################



#
#
# using PyCall
# using PyPlot
#
# function plotSR(SEG::SuperResolution.SegmentSR,RESTR_SEG::Array{Tuple{Int64,Int64},1})
# #1...Dissimilarity changes
# ##########################################
# #heatmap for observations in over segmented space
# X=SEG.loc
# H=heatSR(X)
# pyplot()
# heatmap(H)
# savefig(HM,"/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/01_ORI_KLC_heatmap.pdf",format="pdf",dpi=300)
#
# Hhist1=map(i->sum(H[i,:]),1:size(H,1))
# Hhist2=map(i->sum(H[:,i]),1:size(H,1))
#
# n=size(H,1)
#
# pyplot()
# heatmap(H)
# savefig(HM,"/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/ORI_KLC_heatmap.pdf",format="pdf",dpi=300)
#
# plt[:figure]()
# plt[:title]("part$(1)-$(n)_obs$(sum(H))_est$(0)-sim$(0)")
# #plt[:bar](xRangeSel,1 .* maxKLDeval[end-tmpSelect:end],label="maxKLD",color="red",alpha=0.5)
# #plt[:bar](xRangeSel,1 .* meanKLDeval[end-tmpSelect:end],label="meanKLD",color="grey",alpha=0.5)
# plt[:bar](collect(1:n),1 .* Hhist1[:],label="colSum",color="blue",alpha=0.5)#[end-tmpSelect:end],label="minKLD",color="blue",alpha=0.5)
# plt[:bar](collect(1:n),1 .* Hhist2[:],label="rowSum",color="green",alpha=0.5)#[end-tmpSelect:end],label="Δ minKLD",color="green",alpha=0.5)
# plt[:legend](loc="best")
# #lb=collect(1:n)
# scl=Int64(ceil(sqrt(n))/2)
# lb=map(i->collect(1:n)[scl*i],1:Int64(floor(n/scl)))
# plt[:xticks](lb,fontsize=3)
# #lb=map(i->"$(Knum[i])",collect(1:(ι))[1:end])#[end-tmpSelect:end])
# #plt[:xticks](xRangeSel,lb,fontsize=10.0/log(length(xRangeSel)))
# aD=plt[:gca]()
# aD[:set_xlabel]("col/row")
# aD[:set_ylabel]("sum")
# savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/ORI_KLC_part$(1)-$(n)_obs$(sum(H))_est$(0)-sim$(0).pdf",format="pdf",dpi=300)
# ##########################################
# 	plt[:figure]()
# 	plt[:title]("part$(partN)-$(partN2)_obs$(N)_est$(solvNumClust)-sim$(size(unique(𝕄sim),1))")
# 	#plt[:bar](xRangeSel,1 .* maxKLDeval[end-tmpSelect:end],label="maxKLD",color="red",alpha=0.5)
# 	#plt[:bar](xRangeSel,1 .* meanKLDeval[end-tmpSelect:end],label="meanKLD",color="grey",alpha=0.5)
# 	plt[:bar](xRangeSel,1 .* minKLDeval[:],label="minKLD",color="blue",alpha=0.5)#[end-tmpSelect:end],label="minKLD",color="blue",alpha=0.5)
# 	plt[:bar](xRangeSel,1 .* abs_minKLDeval[:],label="Δ minKLD",color="green",alpha=0.5)#[end-tmpSelect:end],label="Δ minKLD",color="green",alpha=0.5)
# 	plt[:legend](loc="best")
# 	lb=map(i->"$(Knum[i])",collect(1:(ι))[1:end])#[end-tmpSelect:end])
# 	lb=lb[1:Int64(ceil(log(length(lb)))):end]
# 	plt[:xticks](xRangeSel[1:Int64(ceil(log(length(xRangeSel)))):end],lb,fontsize=10.0/log(length(xRangeSel)))
# 	#lb=map(i->"$(Knum[i])",collect(1:(ι))[1:end])#[end-tmpSelect:end])
# 	#plt[:xticks](xRangeSel,lb,fontsize=10.0/log(length(xRangeSel)))
# 	aD=plt[:gca]()
# 	aD[:set_xlabel]("κ")
# 	aD[:set_ylabel]("KLDvalue")
# 	savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/ORI_KLC_part$(partN)-$(partN2)_obs$(N)_est$(solvNumClust)-sim$(size(unique(𝕄sim),1))_EVAL.pdf",format="pdf",dpi=300)
#
# 	#EVALUATE ERRORS FOR EACH CLUSTER
# 	#detection of molecules per Tetramere
# 	#pairwise evaluation sim data vs. computed results
#
# 	#generate string of cluster IDs for plotting
# 	uniqueLength=length(unique(Acoll[solvIDX-1]))
# 	AfinalString=map(i->string(Aall[solvIDX-1][i]),1:uniqueLength)
# #2...Solution 2D Plot with corresponding cluster coloring
# 	plt[:figure]()
# 	plt[:title]("part$(partN)-$(partN2)_obs$(N)_est$(solvNumClust)-sim$(size(unique(𝕄sim),1))")
# 	plt[:scatter](𝕐sim[:,1],𝕐sim[:,2],label="molID[est]",marker="o",s=30.0,alpha=.2, c=Acoll[solvIDX])
# 	plt[:scatter](𝛍sim[:,1],𝛍sim[:,2],label="means[sim]",color="green",marker="x",s=50.0,alpha=0.50)
# 	plt[:scatter](ℝsim[:,1],ℝsim[:,2],label="loc[sim]",color="orange",marker="x",s=50.0,alpha=.50)
# 	plt[:scatter](𝕐simclAll[solvIDX][:,1],𝕐simclAll[solvIDX][:,2],label="loc[est]",color="red",marker=".",s=10.0,alpha=1.0)
# 	plt[:legend](loc="best")
# 	map(i->annotate(AfinalString[i],
# 	xy=[𝕐simclAll[solvIDX][i,1];𝕐simclAll[solvIDX][i,2]],
# 	xytext=[𝕐simclAll[solvIDX][i,1];𝕐simclAll[solvIDX][i,2]],fontsize=2.0),1:uniqueLength)
# 	aD=plt[:gca]()
# 	aD[:set_xlabel]("x [nm]")
# 	aD[:set_ylabel]("y [nm]")
# 	savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/ORI_KLC_part$(partN)-$(partN2)_obs$(N)_est$(solvNumClust)-sim$(size(unique(𝕄sim),1)).pdf",format="pdf",dpi=300)
#
# 	return DataSim, RealLoc
# end
#
# export importSR

export plotSR
