(* ::Package:: *)

Needs["ErrorBarPlots`"]


(* ::Section:: *)
(*Data import*)


gsraw=Import[NotebookDirectory[]<>"data/graphdata_ccm_initialtris.m"];
(* gsraw=SortBy[gsraw,{#[[1,1]]&,#[[1,2]]&}]; (* Sort by n and then by tau. The {} forces a *stable* sort because otherwise Mathematica sorts also on triangle count and other things. *) *)


gdata=GatherBy[gsraw,{#[[1,2]]&,#[[1,1]]&}];
(* Data format: *)
(* gdata[[ tau index, n index, run index , datatype index ]] *)
(* datatype index:
1: {n,tau}
2: avgtriangles when mixed
3: {GCM1 starting triangles average, GCM1 number of successes} <-- CCMu: get new highest degree vertex every time
4: {GCM2 starting triangles average, GCM2 number of successes} <-- CCMb: finish vertex completely
*)
nlabels=Map["n = "<>ToString[#]&,gdata[[1,All,1,1,1]]];
taulabels=Map["\[Tau] = "<>ToString[#]&,gdata[[All,1,1,1,2]]];


(* ::Subsection:: *)
(*New data format import*)


gsraw=Import[NotebookDirectory[]<>"data/graphdata_ccm_constructionrate.m"];
(* gsraw=SortBy[gsraw,{#[[1,1]]&,#[[1,2]]&}]; (* Sort by n and then by tau. The {} forces a *stable* sort because otherwise Mathematica sorts also on triangle count and other things. *) *)


gdata2=GatherBy[gsraw,{#[[1,2]]&,#[[1,1]]&}];
(* Data format: *)
(* gdata[[ tau index, n index, run index , datatype index ]] *)
(* datatype index:
1: {n,tau}
3: CCMdu construction rate <-- CCMdu: get new highest degree vertex every time
4: CCMd  construction rate <-- CCMd: finish vertex completely
*)


(* ::Section:: *)
(*Greedy configuration model*)


(* ::Subsection:: *)
(*Distribution of initial #triangles for GCM1,GCM2,EG compared to average #triangles.*)


 (* Consider all runs *)
getIt[x_,avg_]:=If[x[[2]]>=5, x[[1]]/avg,0]
getAverage[run_]:=Module[{avg},
    avg=run[[2]];
    If[avg>0,
    { getIt[run[[3]],avg], getIt[run[[4]],avg] }
    , {3,3}]
]
getTotalStats[runs_]:=Transpose[Map[getAverage,runs]];
totalStats=Map[getTotalStats,gdata,{2}];


(* Yellow: CCMu (take new highest everytime *)
(* Blue: CCMb (finish highest first, more similar to EG) *)
histogramsTotal=Map[Histogram[#,{0.05},PlotRange->{{-0.5,2},Automatic},ImageSize->300,AxesOrigin->{0,0}]&,totalStats,{2}];


TableForm[histogramsTotal,TableHeadings->{taulabels,nlabels}]


(* ::Subsubsection:: *)
(*Exporting plots*)


makeHistogram[datasets_,n_,tau_]:=Histogram[datasets,{0.05},"Probability",
PlotRange->{{0,1.3},{0,0.5}},
ImageSize->300,AxesOrigin->{0,0},
AspectRatio->3/5,
Frame->True,
FrameLabel->{"fraction of average #triangles at CCM start","frequency"},
ChartLegends->Placed[{"CCMu   avg = "<>ToString[NumberForm[N[Mean[datasets[[1]]]],2]],"CCMb   avg = "<>ToString[NumberForm[N[Mean[datasets[[2]]]],2]]},Center],
(*LabelingFunction->(Placed[NumberForm[#,{2,3}],Above]&),*)
(*LabelingFunction\[Rule](Placed[If[#2[[2]]\[Equal]1,NumberForm[#1,{2,3}],""],Above]&),*)
PlotLabel->n<>", "<>tau];
histogramsTotal=MapIndexed[makeHistogram[#1,nlabels[[#2[[2]]]],taulabels[[#2[[1]]]]]&,totalStats,{2}];


tauIndices={1,5,9};
nIndices={1};
(* TableForm[histogramsTotal[[tauIndices,nIndices]],TableHeadings->{taulabels[[tauIndices]],nlabels[[nIndices]]}]*)
plotgrid1=GraphicsGrid[histogramsTotal[[tauIndices,nIndices]],ImageSize->350,ItemAspectRatio->3/5]


Export[NotebookDirectory[]<>"plots/ccm_initialtris.pdf",plotgrid1]


(* ::Subsection:: *)
(*GCM1 vs GCM2 success rates*)


successrates=Map[{#[[3,2]]/100,#[[4,2]]/100}&,gdata,{3}];
successrates=Map[Transpose,successrates,{2}];
successratesDelta=Map[#[[3,2]]-#[[4,2]]&,gdata,{3}];
successratesAvg=Map[{Mean[#[[1]]],Mean[#[[2]]]}&,successrates,{2}];

rateHistograms=Map[Histogram[#,{0.1},"Probability",PlotRange->{{0,1},Automatic}]&,successrates,{2}];
rateDeltaHistograms=Map[Histogram[#,{10},"Probability",PlotRange->{{-100,100},Automatic}]&,successratesDelta,{2}];


TableForm[rateHistograms,TableHeadings->{taulabels,nlabels}]
TableForm[rateDeltaHistograms,TableHeadings->{taulabels,nlabels}]
(*TableForm[Transpose[rateHistograms],TableHeadings->{nlabels,taulabels}]*)


(* For export *)
makeHistogram2[datasets_,label_]:=Histogram[datasets,{0.1},"Probability",
PlotRange->{{0,1},{0,1}},
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCM construction","frequency"},
ChartLegends->Placed[{"CCMu   avg = "<>ToString[NumberForm[N[Mean[datasets[[1]]]],2]],"CCMb   avg = "<>ToString[NumberForm[N[Mean[datasets[[2]]]],2]]},Center],(*LabelingFunction->(Placed[NumberForm[#,{2,3}],Above]&),*)
(*LabelingFunction\[Rule](Placed[If[#2[[2]]\[Equal]1,NumberForm[#1,{2,3}],""],Above]&),*)
PlotLabel->label];

plot1=makeHistogram2[successrates[[1,1]],"n = 1000, \[Tau] = 2.1"]
plot2=makeHistogram2[successrates[[5,1]],"n = 1000, \[Tau] = 2.5"]
plot3=makeHistogram2[successrates[[9,1]],"n = 1000, \[Tau] = 2.9"]
(* columnplot1=GraphicsColumn[{plot1,plot2,plot3}] *)


Export[NotebookDirectory[]<>"plots/ccm_construction_successrate.pdf",columnplot1]


Export[NotebookDirectory[]<>"plots/ccm_construction_successrate1.pdf",plot1]
Export[NotebookDirectory[]<>"plots/ccm_construction_successrate5.pdf",plot2]
Export[NotebookDirectory[]<>"plots/ccm_construction_successrate9.pdf",plot3]


(* ::Section:: *)
(*CCMu rates only*)


successrates=Map[#[[3,2]]/100&,gdata,{3}];
successrates[[1,1]]=gdata2[[1,1,All,2]]; (* New datafile test *)
successrates=Map[Clip[#,{0,0.99999}]&,successrates,{3}];
legends=Map["\[Tau] = "<>ToString[#[[1,1,2]]]<>" ; avg = "<>ToString[NumberForm[N[Mean[#[[All,3,2]]/100]],3]]&,gdata,{2}];

datasets={successrates[[1,1]], successrates[[5,1]], successrates[[9,1]]};
selectedLegends={legends[[1,1]],legends[[5,1]],legends[[9,1]]};

plot1=Histogram[datasets,{-0.025,1.025,0.05},"Probability",
PlotRange->{{0,1},{0,1}},
ChartStyle->Directive[FaceForm[Opacity[0.8]],EdgeForm[Thickness[0.001]]],
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n(distribution over sampled degree sequences)","Probability"},
ChartLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"]

histogramlist = Map[Last[HistogramList[#,{-0.05,1.05,0.1},"Probability"]]&,datasets];
histogramlist = Transpose[histogramlist];
(* labels=ConstantArray["",Ceiling[1/0.05]];
labels[[2;;-1;;2]]=Map[ToString,Range[0.1,1,0.1]]; *)
labels=Map[ToString,Range[0,1,0.1]];
plot2=BarChart[histogramlist,
PlotRange->{All,{0,1}},
BarSpacing->{None,Medium},
ChartLabels->{labels,None},
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n(distribution over sampled degree sequences)","Probability"},
ChartLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"]

plot3=SmoothHistogram[datasets,{0.01,"Gaussian"},"PDF",
PlotRange->{{0,1},All},
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n(distribution over sampled degree sequences)","PDF"},
PlotLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"]

(*
Table[
SmoothHistogram[datasets,{0.01,weightKernel},"PDF",
PlotRange->{{0,1},All},
(*ChartStyle\[Rule]Directive[FaceForm[Opacity[0.5]],EdgeForm[Thickness[0.007]]],*)
ImageSize->300,AxesOrigin->{0,0},
Frame->True,
FrameLabel->{"successrate of CCMdu construction\n over sampled degree sequences","CDF"},
PlotLegends->Placed[selectedLegends,Center],
PlotLabel->"n = 1000"],{weightKernel,{"Biweight","Gaussian","Rectangular","Cosine","SemiCircle"}}]
*)


Export[NotebookDirectory[]<>"plots/ccm_construction_successrates.pdf",plot2]
