(* ::Package:: *)

Needs["ErrorBarPlots`"]


(* ::Section:: *)
(*Plot successrate over time*)


gsraw=Import[NotebookDirectory[]<>"data/graphdata_successrates_timeevol.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: avgtris
3: successrate
*)
nlabels=Map["n = "<>ToString[#]&,gdata[[1,All,1,1,1]]];
taulabels=Map["tau = "<>ToString[#]&,gdata[[All,1,1,1,2]]];


TwoAxisPlot[{f_, g_},{plotrange1_,plotrange2_}] := 
Module[{fgraph, ggraph, frange, grange, fticks, gticks, manualRanges},
manualRanges={plotrange1,plotrange2};
{fgraph, ggraph} = 
MapIndexed[
ListPlot[#1, Axes -> True, Joined->True,ImageSize->300,
PlotRange->manualRanges[[#2[[1]]]],
DataRange->{0,measureSkip*maxTime}
(* PlotStyle -> ColorData[1][#2[[1]]] *)
] &, {f, g}];
{frange, grange} = (PlotRange /. AbsoluteOptions[#, PlotRange])[[2]] & /@ {fgraph, ggraph};
fticks = N@FindDivisions[frange, 10]; 
gticks = Quiet@Transpose@{fticks, ToString[NumberForm[#, 2], StandardForm] & /@ Rescale[fticks, frange, grange]}; 
Show[fgraph, ggraph /. Graphics[graph_, s___] :> Graphics[GeometricTransformation[graph, RescalingTransform[{{0, 1}, grange}, {{0, 1}, frange}]], s],
Axes -> False, Frame -> True,
(*FrameStyle -> {ColorData[1] /@ {1, 2}, {Automatic, Automatic}}, *)
FrameTicks -> {{fticks, gticks}, {Automatic, Automatic}}]]


(* Test of plot function *)
TwoAxisPlot[{gdata[[1,1,{1,3},2]],gdata[[1,1,{1,3},3]]},{{0,12000},{0,100}}]


MeanFilter


(* For export *)
selectedData=gdata[[2,1]][[{1,3,4,5}]];
measureSkip=100;
minCount=Min[Map[Min[#[[2]]]&,selectedData]];
maxCount=Max[Map[Max[#[[2]]]&,selectedData]];
maxCount=Max[Map[1.5*Max[#[[2,-100;;-1]]]&,selectedData]];
maxTime=Max[Map[Length[#[[2]]]&,selectedData]];
(* maxTime=Round[30000/measureSkip]; *)
skipPts=Max[1,Round[maxTime/5000]]; (* Plotting every point is slow. Plot only once per `skipPts` timesteps *)
maxTime=Round[50000/measureSkip];
coarseData=Map[#[[2,1;;maxTime;;skipPts]]&,selectedData];
coarseData2=Map[#[[3,1;;maxTime;;skipPts]]/100&,selectedData];
coarseData3=Map[MeanFilter[#[[3]],6]/100&,selectedData];
labels=Map["{n,tau} = "<>ToString[#[[1]]]&,selectedData]
TwoAxisPlot[{coarseData,coarseData2},{{0,maxCount},{0,1}}]
plotTimeEvol1=ListPlot[coarseData,Joined->True,PlotRange->{0*minCount,maxCount},DataRange->{0,measureSkip*maxTime},Frame->True,FrameLabel->{"timesteps","number of triangles"},PlotLabel->"n = 1000, \[Tau]=2.2",ImageSize->300]
plotTimeEvol2=ListPlot[coarseData2,Joined->True,PlotRange->{0,1},DataRange->{0,measureSkip*maxTime},Frame->True,FrameLabel->{"timesteps","successrate"},PlotLabel->"n = 1000, \[Tau]=2.2",ImageSize->300,PlotStyle->Opacity[0.5]];
plotTimeEvol3=ListPlot[coarseData3,Joined->True,PlotRange->{0,1},DataRange->{0,measureSkip*maxTime}];
plotTimeEvol4=Show[plotTimeEvol2,plotTimeEvol3]
(* Map[ListPlot[#,Joined->True,PlotRange\[Rule]{minCount,maxCount},DataRange\[Rule]{0,maxTime}]&,coarseData] *)


Export[NotebookDirectory[]<>"plots/timeevol_22_successrate_triangles.pdf",plotTimeEvol1]
Export[NotebookDirectory[]<>"plots/timeevol_22_successrate.pdf",plotTimeEvol4]


(* ::Section:: *)
(*Plot #trianges vs some successrate*)


gsraw=Import[NotebookDirectory[]<>"data/graphdata_successrates.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: avgtris
3: successrate
*)
nlabels=Map["n = "<>ToString[#]&,gdata[[1,All,1,1,1]]];
taulabels=Map["\[Tau] = "<>ToString[#]&,gdata[[All,1,1,1,2]]];


allPlots=Map[Show[
ListPlot[#[[All,{3,2}]],
AxesOrigin->{0,0},
Frame->True,FrameLabel->{"successrate","average number of triangles"},
(*AspectRatio->Automatic,*)
PlotRange->{{0,1},Automatic},
ImageSize->Automatic,
PlotLabel->"n = "<>ToString[#[[1,1,1]]]<>" , \[Tau] = "<>ToString[#[[1,1,2]]],
PlotStyle->PointSize[0.0001]
],Plot[x,{x,1,10000}]]
&,gdata,{2}]


combiPlot=ListLogPlot[gdata[[{1,3,5,7,9},1,All,{3,2}]],
AxesOrigin->{0,0},
Frame->True,FrameLabel->{"successrate","average number of triangles"},
(*AspectRatio->Automatic,*)
PlotRange->Automatic,
ImageSize->300,
PlotLabel->"n = "<>ToString[gdata[[1,1]][[1,1,1]]],
PlotLegends->taulabels[[{1,3,5,7,9}]]
]


Export[NotebookDirectory[]<>"plots/successrate_correlations.pdf",combiPlot]
