#########THINGS TO DO ON STARTUP>>>>>>>>>>>
#   cd("/Users/Patrick/Documents/GitHub/SuperResolution/src")
#   press ] and type activate SuperResolution
#   CMD+A -> CMD+Enter to run SuperResolution file

##  NOTE
#   CMD+/ .. comment line
#   Ctrl+A, Ctrl+K .. delete line in terminal
#   Shift+CMD+[ or ] .. switch between open tabs/files in atom
#   Ctrl+1 or 2 .. switch between file and terminal
#   for more Keybinds check Atom settings
#

# │ import Pkg;
# │ Pkg.add("Conda")
# │ Conda.update()
# │ import Conda
# │ Pkg.build("PyPlot")

# I'd recommend just doing this kind of thing:
# using Plots
# pyplot() # this loads the `PyPlot` module
# # run your code that uses pyplot, ether using the Plots API, or the PyPlot API via `PyPlot.xxx`

# gr()
# # run your code that uses the default Plots backend, which is GR

#   Main.SuperResolution.SEGMENTtest to access the segementation
#   length(Main.SuperResolution.SEGMENTtest.idx) number of valid segments
#
#   Main.SuperResolution.EVALtest to access the test set clustering
#   Main.SuperResolution.EVALtest.K_ground_truth known number of clusters
#   Main.SuperResolution.EVALtest.diff_WK_max_idx estimated number of clusters
#   Main.SuperResolution.EVALtest.wModel[1].wkld distance matrix of segment 1
#   Main.SuperResolution.EVALtest.gapSR[1].diff_WK differences of WKLD
#   Main.SuperResolution.EVALtest.RefModel[2].K evolution for segment 2
#
#   Main.SuperResolution.EVALtest.distSR[1].Distance[1].singleDist_SR[1]
#
#   evalSR.jl   <-  clusGAP.jl  <-  clusSR.jl(DistanceSR)
##  NOTE
#   if the solveIDX is 11 for segment 1, the estimated number of clusters in this segment is
#   size(Main.SuperResolution.EVALtest.wModel[1].wkld[11],1)
#
#   if the solveIDX is 9 for segment 2, the estimated number of clusters in this segment is
#   size(Main.SuperResolution.EVALtest.wModel[2].wkld[9],1)
##  NOTE
# using Plots
# using Statistics
# tuP=0
# sel=5
#
# bar(Main.SuperResolution.EVALtest.wkldSR[sel].wkldMin)
# for i=(sel+1):5#length(Main.SuperResolution.EVALtest.wkldSR)
#     display(bar!(Main.SuperResolution.EVALtest.wkldSR[i].wkldMin))
# end
# savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/01_ORI_KLC_wkldMin.pdf")
#
# sel=5
# bar(Main.SuperResolution.EVALtest.wkldSR[sel].wkldMinDelta, alpha=0.2)
# for i=(sel+1):6#length(Main.SuperResolution.EVALtest.wkldSR)
#     display(bar!(Main.SuperResolution.EVALtest.wkldSR[i].wkldMinDelta, alpha=0.2))
# end
# savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/01_ORI_KLC_wkldMinDelta.pdf")
# #

# BarPlots=map(i->bar(Main.SuperResolution.EVALtest.wkldSR[i].wkldMinDelta, title="$(i)"),1:length(Main.SuperResolution.EVALtest.wkldSR))
# plot(BarPlots..., legend=false) #layout=(3,2)
# savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/01_ORI_KLC_wkldMinDeltaSERIES.pdf")

# tmp=map(i->length(Main.SuperResolution.EVALtest.wkldSR[i].wkldMinDelta[Main.SuperResolution.EVALtest.wkldSR[i].wkldMinDelta.>quantile(Main.SuperResolution.EVALtest.wkldSR[i].wkldMinDelta, 0.95)])+1,1:length(Main.SuperResolution.EVALtest.wkldSR))

# #
# # plot(   bar(Main.SuperResolution.EVALtest.wkldSR[1].wkldMinDelta[2:end]./Main.SuperResolution.EVALtest.wkldSR[1].wkldMinDelta[1:end-1], title="1"),
# #         bar(Main.SuperResolution.EVALtest.wkldSR[2].wkldMinDelta[2:end]./Main.SuperResolution.EVALtest.wkldSR[2].wkldMinDelta[1:end-1], title="2"),
# #         bar(Main.SuperResolution.EVALtest.wkldSR[3].wkldMinDelta[2:end]./Main.SuperResolution.EVALtest.wkldSR[3].wkldMinDelta[1:end-1], title="3"),
# #         bar(Main.SuperResolution.EVALtest.wkldSR[4].wkldMinDelta[2:end]./Main.SuperResolution.EVALtest.wkldSR[4].wkldMinDelta[1:end-1], title="4"),
# #         bar(Main.SuperResolution.EVALtest.wkldSR[5].wkldMinDelta[2:end]./Main.SuperResolution.EVALtest.wkldSR[5].wkldMinDelta[1:end-1], title="5"),
# #         bar(Main.SuperResolution.EVALtest.wkldSR[6].wkldMinDelta[2:end]./Main.SuperResolution.EVALtest.wkldSR[6].wkldMinDelta[1:end-1], title="6"), layout=(3,2), legend=false   )
# # savefig("/Users/Patrick/Documents/GitHub/SuperResolution/src/SR_Plots/01_ORI_KLC_wkldMinSERIES.pdf")
# # #
# plot(minimum.(Main.SuperResolution.EVALtest.wModel[1].wkld[3:end-tuP]))
# plot!(minimum.(Main.SuperResolution.EVALtest.wModel[2].wkld[3:end-tuP]))
# plot(median.(Main.SuperResolution.EVALtest.wModel[1].wkld)[2:(end-tuP)])
# plot!(median.(Main.SuperResolution.EVALtest.wModel[2].wkld)[2:(end-tuP)])
#
# b1=map(i->Main.SuperResolution.EVALtest.wModel[1].wkld[i].<10^4,2:(size(Main.SuperResolution.EVALtest.wModel[1].wkld,1)-tuP))
# c1=map(i->median(Main.SuperResolution.EVALtest.wModel[1].wkld[i][b1[i-1]]),2:(size(Main.SuperResolution.EVALtest.wModel[1].wkld,1)-tuP))
# plot(c1)
#
# b2=map(i->Main.SuperResolution.EVALtest.wModel[2].wkld[i].<10^4,2:(size(Main.SuperResolution.EVALtest.wModel[2].wkld,1)-tuP))
# c2=map(i->median(Main.SuperResolution.EVALtest.wModel[2].wkld[i][b2[i-1]]),2:(size(Main.SuperResolution.EVALtest.wModel[2].wkld,1)-tuP))
# plot!(c2)
#
# b1=map(i->Main.SuperResolution.EVALtest.wModel[1].wkld[i].<10^4,2:(size(Main.SuperResolution.EVALtest.wModel[1].wkld,1)-tuP))
# c1=map(i->mean(Main.SuperResolution.EVALtest.wModel[1].wkld[i][b1[i-1]]),2:(size(Main.SuperResolution.EVALtest.wModel[1].wkld,1)-tuP))
# plot(c1)
#
# b2=map(i->Main.SuperResolution.EVALtest.wModel[2].wkld[i].<10^4,2:(size(Main.SuperResolution.EVALtest.wModel[2].wkld,1)-tuP))
# c2=map(i->mean(Main.SuperResolution.EVALtest.wModel[2].wkld[i][b2[i-1]]),2:(size(Main.SuperResolution.EVALtest.wModel[2].wkld,1)-tuP))
# plot!(c2)
#
# sel=1
# b3=map(i->Main.SuperResolution.EVALtest.wModel[sel].wkld[i].<10^4,2:(size(Main.SuperResolution.EVALtest.wModel[sel].wkld,1)-tuP))
# c3=map(i->median(Main.SuperResolution.EVALtest.wModel[sel].wkld[i][b3[i-1]]),2:(size(Main.SuperResolution.EVALtest.wModel[sel].wkld,1)-tuP))
# plot(c3)
#


# Main.SuperResolution.EVALtest.wModel[1].geom[14]
# Main.SuperResolution.EVALtest.wModel[1].weight[14]
#
# num=size(Main.SuperResolution.EVALtest.wModel,1)
#
# wkldFiltered=Array{Array{BitArray{2},1}}(undef,num)
# wkldMean=Array{Array{Float64,1},1}(undef,num)
# for k in 1:num
#     wkldFiltered[k]=map(i->Main.SuperResolution.EVALtest.wModel[k].wkld[i].<10^4,2:(size(Main.SuperResolution.EVALtest.wModel[k].wkld,1)-tuP))
#     wkldMean[k]=map(i->mean(Main.SuperResolution.EVALtest.wModel[k].wkld[i][wkldFiltered[k][i-1]]),2:(size(Main.SuperResolution.EVALtest.wModel[k].wkld,1)-tuP))
# end

#########TO DO>>>>>>>>>>>
#   - safe the SEGMENTtest segmentation in a variable so it does not need to be
#   processed every time
#
#   - find a new way to validate properly; e.g. correctly assigned members
#   (evalSR.jl, obj.corrDetecRate_GAP=...)
#
#   - transfer plot funtions from old files to new repo
#
#   - implement GAP-statistics for minKLD changes like used in RStudio
#   - allow for an additional parameter in GAP-statistics like in RStudio
#
########################################################################



module SuperResolution
global RESTR_SEG=2
global modINPUT=4
global fileINPUT_range=1:5

cd(dirname(@__FILE__))
using Statistics
using DataFrames
using Distances
using Random
using Dates
using CSV
using Distributions
using StatsPlots

global DATUM=Dates.format(now(),"yymmdd")

include("clusGAP.jl")
include("clusSR.jl")
include("evalSR.jl")
include("fctSR.jl")
include("importSR.jl")
include("plotSR.jl")
include("segmentSR.jl")
include("modelSR.jl")

greet()=print("\n\nSuper Resolution - Blinking Artifacts\n")
greet()
global filename1=Array{String,1}(undef,6)
global filename1[1]="ORI_SIM/locs_gamma.csv" # simulation data with framenumbers
global filename1[2]="ORI_SIM/locs_H57.csv"
global filename1[3]="ORI_SIM/locs_KT3.csv"
global filename1[4]="ORI_SIM/locs_PSCFP2.csv"
global filename1[5]="TET_SIM/locs_tet.csv"
global filename1[6]="MON_SIM/locs_mon.csv"
global filepath1 = map(i->joinpath(@__DIR__, filename1[i]),1:length(filename1))

global filename2=Array{String,1}(undef,6)
global filename2[1]="ORI_SIM/labels_gamma.csv"    # ground truth
global filename2[2]="ORI_SIM/labels_H57.csv"
global filename2[3]="ORI_SIM/labels_KT3.csv"
global filename2[4]="ORI_SIM/labels_PSCFP2.csv"
global filename2[5]="TET_SIM/labels_tet.csv"
global filename2[6]="MON_SIM/labels_mon.csv"
global filepath2 = map(i->joinpath(@__DIR__, filename2[i]),1:length(filename2))
# pStr1=filepath1
# pStr2=filepath2
# pStr1="/Users/Patrick/Documents/GitHub/SuperResolution/src/locs_gamma_conf.csv"
# pStr2="/Users/Patrick/Documents/GitHub/SuperResolution/src/labels_gamma.csv"
################################################################################
################################################################################


DIAGNOSTICS=true
if DIAGNOSTICS==true
        print("DIAGNOSTICS = true")
        valid_DF=Array{DataFrame,1}(undef, length(fileINPUT_range))
        error_DF=Array{DataFrame,1}(undef, length(fileINPUT_range))

        error_DF = map(i-> CSV.read(joinpath(@__DIR__,string("/Users/Patrick/Documents/GitHub/SuperResolution/src/VALID_OUT/210919fileINPUT",fileINPUT_range[i],"-modINPUT4-NUM_SEG_150-MSE_standW.csv")),
                                        types=[String,Float64,Float64],
                                        header=["idx","clus_vs_mol_MSE","clus_vs_mol_MAE"], datarow=2, DataFrame), fileINPUT_range)
        lengths = map(i->length(error_DF[i].clus_vs_mol_MAE),fileINPUT_range)
        idx = vcat(map(i->repeat([i],inner=lengths[i])[:],fileINPUT_range)...)
        errors = vcat(map(i->error_DF[i].clus_vs_mol_MAE,fileINPUT_range)...)
        # valid_DF = map(i-> CSV.read(joinpath(@__DIR__,string("/Users/Patrick/Documents/GitHub/SuperResolution/src/VALID_OUT/210919fileINPUT",fileINPUT_range[i],"-modINPUT4-NUM_SEG_150-MSE_standW.csv")),
        #                                 types=[String,Float64,Float64],
        #                                 header=["idx","clus_vs_mol_MSE","clus_vs_mol_MAE"], datarow=2, DataFrame), fileINPUT_range)
        
        Plots.boxplot(idx,errors,legend=false)
        Plots.savefig(joinpath(@__DIR__,string("DIAGNOSTICS/",DATUM,"_fileINPUT$(fileINPUT_range)-modINPUT$(modINPUT)-DIAGNOSTICS.pdf")))
        Plots.savefig(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"_fileINPUT$(fileINPUT_range)-modINPUT$(modINPUT)-DIAGNOSTICS.pdf")))
        PyPlot.close("all")
else
        for fileINPUT=fileINPUT_range
                # 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
                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


                global DataSim, RealLocs=importSR(filepath1[fileINPUT],filepath2[fileINPUT])
                # using SuperResolution
                #SEG_SPLIT=100 splits the data into 100x100=10^4 segments of equal size
                # SEG_SPLIT=200;
                SEGMENT_TMP_IMPORT=SuperResolution.SegmentSR(1,SuperResolution.DataSim,SuperResolution.RealLocs); #segment data
                SEG_SIZE_NM=SEGMENT_TMP_IMPORT.bounds_overall.nm_x/sqrt(SEGMENT_TMP_IMPORT.N[1])
                print("*** fileINPUT = $(fileINPUT) ***\n")
                print("*** SEG_SIZE_NM = $(SEG_SIZE_NM) ***\n")
                # SEG_SIZE_NM=100.0
                SEG_SPLIT=Int64(ceil(maximum([SEGMENT_TMP_IMPORT.bounds_overall.nm_x,SEGMENT_TMP_IMPORT.bounds_overall.nm_y])/SEG_SIZE_NM)); # 25nm^2 segments
                global SEGMENTtest=SuperResolution.SegmentSR(SEG_SPLIT,SuperResolution.DataSim,SuperResolution.RealLocs); #segment data
                print("*** $(length(SuperResolution.SEGMENTtest.idx)) valid Segments ***\n")
                # NUM_SEG=2 #sets a test set of 10 segments for clustering
                NUM_SEG=minimum([length(SuperResolution.SEGMENTtest.idx),RESTR_SEG]);   #eval all or minimum number of valid segments
                # NUM_SEG=length(SuperResolution.SEGMENTtest.idx);   #eval all valid segments
                rng=MersenneTwister(2);
                SEG_RESTR=unique(rand(rng,SEGMENTtest.idx,NUM_SEG));

                global EVALtest=SuperResolution.EvalSR(SEGMENTtest,SEG_RESTR,modINPUT);
        ## CSV OUTPUT
                CSV.write(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"_fileINPUT$(fileINPUT)-modINPUT$(modINPUT)-NUM_SEG$(NUM_SEG)-absChange_standW.csv")),EVALtest.VALID_DF,header=true)
                CSV.write(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"fileINPUT$(fileINPUT)-modINPUT$(modINPUT)-NUM_SEG$(NUM_SEG)-MSE_standW.csv")),EVALtest.error_DF,header=true)
                map(i->CSV.write(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"_fileINPUT$(fileINPUT)-modINPUT$(modINPUT)-SEG_SIZE_NM$(Int64(floor(SEG_SIZE_NM)))-NUM_SEG$(NUM_SEG)-locID_DF_$(i)_standW.csv")),EVALtest.locID_DF[i],header=true),1:length(EVALtest._selectIDX))
                map(i->CSV.write(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"_fileINPUT$(fileINPUT)-modINPUT$(modINPUT)-SEG_SIZE_NM$(Int64(floor(SEG_SIZE_NM)))-NUM_SEG$(NUM_SEG)-evo_DF_$(i)_standW.csv")),EVALtest.evo_DF[i],header=true),1:length(EVALtest._selectIDX))
        ## PLOTS
                using Plots
                global BarPlots=Array{Plots.Plot{Plots.GRBackend}}(undef,length(EVALtest.wkldSR))
                plots_per_page=16
                num_of_plots=Int64(ceil(length(EVALtest.wkldSR)/plots_per_page))
                for k=1:num_of_plots
                        for i=(1+(k-1)*plots_per_page):(minimum([k*plots_per_page,length(EVALtest.wkldSR)]))#length(EVALtest.wkldSR)
                                BarPlots[i]=Plots.bar(EVALtest.wkldSR[i].wkldMinDelta, title="part($(EVALtest._selectIDX[i][1])-$(EVALtest._selectIDX[i][2])) - $(getproperty(EVALtest, select_mode[modINPUT])[i])/$(EVALtest.K_ground_truth[i])/$(EVALtest.N_observations[i])",
                                titlefont=font(8,"Computer Modern"))
                                Plots.plot!([quantile(EVALtest.wkldSR[i].wkldMinDelta,0.95)],seriestype="hline")
                        end
                        Plots.plot(BarPlots[(1+(k-1)*plots_per_page):(minimum([k*plots_per_page,length(EVALtest.wkldSR)]))]..., legend=false) #layout=(3,2)
                        Plots.savefig(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"_fileINPUT$(fileINPUT)-modINPUT$(modINPUT)-SEG_SIZE_NM$(Int64(floor(SEG_SIZE_NM)))-NUM_SEG$(NUM_SEG)-absChange_standW_$(k)of$(num_of_plots).pdf")))
                        PyPlot.close("all")
                end
                plotSR(SEGMENTtest,EVALtest,fileINPUT,modINPUT)
                PyPlot.close("all")
        end
end
end
# valid_DF=Array{DataFrame,1}(undef, length(fileINPUT_range))
# error_DF=Array{DataFrame,1}(undef, length(fileINPUT_range))

# error_DF = map(i-> CSV.read(joinpath(@__DIR__,string("/Users/Patrick/Documents/GitHub/SuperResolution/src/VALID_OUT/210919fileINPUT",fileINPUT_range[i],"-modINPUT4-NUM_SEG_150-MSE_standW.csv")),
#                                 types=[String,Float64,Float64],
#                                 header=["idx","clus_vs_mol_MSE","clus_vs_mol_MAE"], datarow=2, DataFrame), fileINPUT_range)

# # valid_DF = map(i-> CSV.read(joinpath(@__DIR__,string("/Users/Patrick/Documents/GitHub/SuperResolution/src/VALID_OUT/210919fileINPUT",fileINPUT_range[i],"-modINPUT4-NUM_SEG_150-MSE_standW.csv")),
# #                                 types=[String,Float64,Float64],
# #                                 header=["idx","clus_vs_mol_MSE","clus_vs_mol_MAE"], datarow=2, DataFrame), fileINPUT_range)
# using StatsPlots
# Plots.boxplot()
# Plots.savefig(joinpath(@__DIR__,string("VALID_OUT/",DATUM,"_fileINPUT$(fileINPUT)-modINPUT$(modINPUT)-SEG_SIZE_NM$(Int64(floor(SEG_SIZE_NM)))-NUM_SEG$(NUM_SEG)-absChange_standW_$(k)of$(num_of_plots).pdf")))
# PyPlot.close("all")

################################################################################
################################################################################
# EMP2=EVALtest;
# #tmp2=SEGMENTtest;
# #
# asdf=map(i->sum(SEGMENTtest.N[CartesianIndex(SEGMENTtest.idx[i])]),1:NUM_SEG)
# asdf2=map(i->(EVALtest.ground_truth_K-EVALtest.diff_WK_max_idx)[i]/EVALtest.ground_truth_K[i],1:NUM_SEG)
# plot([EVALtest.ground_truth_K-EVALtest.diff_WK_max_idx,log.(asdf),asdf2])
# plot(asdf2)
# #
##
# tmp2=SuperResolution.ModelSR(SEGMENTtest,(8,8),1)  #for segment create all models  WKLD
# tmp22=SuperResolution.ModelSR(SEGMENTtest,(8,8),2)  #for segment create all models KLD
# tmp3=SuperResolution.DistanceSR(tmp2,1)    #cluster WKLD
# tmp33=SuperResolution.DistanceSR(tmp2,2)   #cluster KLD
# tmp4=SuperResolution.clusGAP(tmp2,tmp22,1)  #Gap statistic
# tmp44=SuperResolution.clusGAP(tmp2,tmp22,2)  #Gap statistic
# #
# using SuperResolution
# SEGMENTtest=SuperResolution.SegmentSR(10,SuperResolution.DataSim,SuperResolution.RealLocs) #segment data
# tmp2=SuperResolution.ModelSR(SEGMENTtest,(2,3),1)  #for segment create all models  WKLD
# tmp22=SuperResolution.ModelSR(SEGMENTtest,(2,3),2)  #for segment create all models KLD
# tmp3=SuperResolution.DistanceSR(tmp2,1)    #cluster WKLD
# tmp33=SuperResolution.DistanceSR(tmp2,2)   #cluster KLD
# tmp4=SuperResolution.clusGAP(tmp2,tmp22,1)  #Gap statistic
# tmp44=SuperResolution.clusGAP(tmp2,tmp22,2)  #Gap statistic
# #
# id1=1
# id2=9
# tmp2=SuperResolution.ModelSR(SEGMENTtest,(id1,id2),1)  #for segment create all models  WKLD
# tmp22=SuperResolution.ModelSR(SEGMENTtest,(id1,id2),2)  #for segment create all models KLD
# tmp3=SuperResolution.DistanceSR(tmp2,1)    #cluster WKLD
# tmp33=SuperResolution.DistanceSR(tmp2,2)   #cluster KLD
# tmp4=SuperResolution.clusGAP(tmp2,tmp22,1)  #Gap statistic
# tmp44=SuperResolution.clusGAP(tmp2,tmp22,2)  #Gap statistic
#
# asdf=tmp4.W_K[1:(end-1)]-tmp4.W_K[2:end]
# plot(asdf)
#
# #
# scatter([SEGMENTtest.MU[2,3][:,1],tmp2.loc[end-5][:,1]],[SEGMENTtest.MU[2,3][:,2],tmp2.loc[end-5][:,2]])
# scatter(SEGMENTtest.MU[2,3][:,1],SEGMENTtest.MU[2,3][:,2])
# scatter(tmp2.loc[end-5][:,1],tmp2.loc[end-5][:,2])
# asdf=tmp4.W_K[1:(end-1)]-tmp4.W_K[2:end]
# plot(asdf)
#             K_SR::Int64
#             N_SR::Int64
#             val_bool::BitArray{1}
#             val_bool2::Array{BitArray{1},1}
#             singleDist_SR::Array{Array{Float64,2},1}
#             loc_SR::Array{Array{Array,1},1}
#             frame_SR::Array{Array{Int64,1},1}
#             cov_SR::Array{Array{Array{Float64,2},1},1}
#             D_SR::Array{Float64,1}
#             W_K::Float64
#             weight_SR::Array{Array{Float64,2},1}
# SEGMENTtest=SuperResolution.SegmentSR(10,SuperResolution.DataSim,SuperResolution.RealLocs)
# SAMPLE1=SuperResolution.ctrans(rand(tmp2.model[end-1],60))
# gather_X1=hcat(SEGMENTtest.loc[8,8][:,2],SAMPLE1[:,1])
# scatter(SuperResolution.𝕐sim[:,1],SuperResolution.𝕐sim[:,2])
# scatter(SEGMENTtest.loc[8,8][:,1],SEGMENTtest.loc[8,8][:,2])
# tmp2=SuperResolution.ModelSR(SEGMENTtest,(8,8))
# tmp3=(map(i->mean(tmp2.weight[i]),2:(length(tmp2.weight)-1)))
# tmp4=(map(i->mean(tmp2.param[i]),1:(length(tmp2.param)-1)))
# plot(tmp3)
# plot(tmp4)
# scatter(SEGMENTtest.loc[8,8][:,1],SEGMENTtest.loc[8,8][:,2])
# plot(contourf(exp(abs.(tmp2.wkld[end-8]-tmp3.wkld[end-8]))))
# global NN=20	# partition of space in (NN-1)^2 parts
#
# global evalErrSINGLE=[[0.0],]
# global evalErrGLOBAL=[[0.0],]
# global evalmeanErrGLOBAL=[0.0,]
# global evalcountErrGLOBAL=[0.0,]
# global evalcountErrGLOBAL2=[0,]
# #global evalSEED=[0,]
#
# global partN_valid=[0,]
# global partN2_valid=[0,]
#
# global estim_mol_numberGLOBAL=[0,]
# global sim_mol_numberGLOBAL=[0,]
# global obsGLOBAL=[0,]
#
# global 𝕐estAll=Array{Float64,2}(undef, 0,2)
# global 𝕐simAll=Matrix{Float64}(DataSim[[4,5],])
#
# global 𝕄simAll= Vector{Int64}(DataSim[1,])
# global 𝛍simAll= Matrix{Float64}(undef,length(unique(𝕄simAll)),2)
# #For unique molecule numbers compute means (needed for evaluation)
# for i=1:length(unique(𝕄simAll))
# 	global 𝛍simAll[i,:]=mean(𝕐simAll[𝕄simAll .== unique(𝕄simAll)[i],1:2]',dims=2)[:]
# end

