%%% R code to generate the input data files from corresponding simulation logs
% R> setwd("~/Work/tensorPredictors")
% R>
% R> sim <- read.csv("sim/sim-tsir-20231123T1155.csv")
% R>
% R> aggr <- aggregate(sim[startsWith(names(sim), "dist")], sim[c("rho", "order", "beta.version")], mean)
% R>
% R> write.table(aggr, file = "LaTeX/plots/aggr-tsir.csv", row.names = FALSE, quote = FALSE)
\documentclass[border=0cm]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{bm}

\definecolor{gmlm}{RGB}{0,0,0}
% \definecolor{mgcca}{RGB}{86,180,233}
\definecolor{tsir}{RGB}{0,158,115}
\definecolor{sir}{RGB}{86,180,233}
% \definecolor{hopca}{RGB}{230,159,0}
% \definecolor{pca}{RGB}{240,228,66}
% \definecolor{lpca}{RGB}{0,114,178}
% \definecolor{clpca}{RGB}{213,94,0}

\pgfplotsset{
    compat=newest,
    every axis/.style={
        % grid,
        % grid style={gray, dotted},
        3d box = complete*,
        xtick = {0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8},
        ytick = {2, 3, 4},
        ymin = 1.5, ymax = 4.5,
        ztick = {0.2, 0.4, 0.6, 0.8, 1},
        zmin = 0, zmax = 1,
        xlabel = $\rho$,
        ylabel = $r$
    }
}
\tikzset{
    legend entry/.style={
        mark = *,
        mark size = 1pt,
        mark indices = {2},
        line width=0.8pt
    }
}

\newcommand{\addarea}[4]{
    \addplot3[
        fill = #1,
        draw = none,
        fill opacity = 0.65,
        y filter/.append expression={
            \thisrow{order} == #3 && \thisrow{beta.version} == #2 ? #4 : NaN
        }
    ] table [x = rho, y expr = NaN, z = dist.subspace.#1] {aggr-tsir.csv} \closedcycle;
    \addplot3[
        color = #1,
        line width = 0.8pt,
        y filter/.append expression={
            \thisrow{order} == #3 && \thisrow{beta.version} == #2 ? #4 : NaN
        }
    ] table [x = rho, y expr = NaN, z = dist.subspace.#1] {aggr-tsir.csv};
}

\begin{document}
\begin{tikzpicture}[>=latex]

    \begin{axis}[
        name=v1
    ]
        \addarea{sir}{1}{4}{4.05};
        \addarea{tsir}{1}{4}{4.0};
        \addarea{gmlm}{1}{4}{3.95};

        \addarea{sir}{1}{3}{3.05};
        \addarea{tsir}{1}{3}{3.0};
        \addarea{gmlm}{1}{3}{2.95};

        \addarea{sir}{1}{2}{2.05};
        \addarea{tsir}{1}{2}{2.0};
        \addarea{gmlm}{1}{2}{1.95};
    \end{axis}
    \node[anchor = south] at (v1.north) {V1};

    \begin{axis}[
        name=v2,
        xshift = 8cm
    ]
        \addarea{sir}{2}{4}{4.05};
        \addarea{tsir}{2}{4}{4.0};
        \addarea{gmlm}{2}{4}{3.95};

        \addarea{sir}{2}{3}{3.05};
        \addarea{tsir}{2}{3}{3.0};
        \addarea{gmlm}{2}{3}{2.95};

        \addarea{sir}{2}{2}{2.05};
        \addarea{tsir}{2}{2}{2.0};
        \addarea{gmlm}{2}{2}{1.95};
    \end{axis}
    \node[anchor = south] at (v2.north) {V2};

    \matrix[anchor = north] at (current bounding box.south) {
        \fill[color = gmlm, opacity = 0.65] (0, -.1) rectangle (.6, .1); \draw[color=gmlm, line width = 0.8pt] (0, .1) -- (.6, .1); & \node[anchor=west] {GMLM}; &
        \fill[color = tsir, opacity = 0.65] (0, -.1) rectangle (.6, .1); \draw[color=tsir, line width = 0.8pt] (0, .1) -- (.6, .1); & \node[anchor=west] {TSIR}; &
        \fill[color = sir,  opacity = 0.65] (0, -.1) rectangle (.6, .1); \draw[color=sir,  line width = 0.8pt] (0, .1) -- (.6, .1); & \node[anchor=west] {SIR}; \\
    };

    \node[anchor = south, rotate = 90, yshift = 1.5em] at (v1.west) {Subspace Distance $d(\boldsymbol{B}, \widehat{\boldsymbol{B}})$};
    \node[anchor = south, rotate = -90, yshift = 1.5em] at (v2.east) {\phantom{Subspace Distance $d(\boldsymbol{B}, \widehat{\boldsymbol{B}})$}};

\end{tikzpicture}
\end{document}
