From 1bd9cb6c0f4c0882bccb6a39b618655c080bbc63 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Mallo Date: Fri, 16 Oct 2020 15:55:52 +0200 Subject: [PATCH] Move the plot script to R --- garlic/fig/nbody/test.nix | 65 --------------------------- garlic/fig/nbody/test/default.nix | 62 ++++++++++++++++++++++++++ garlic/fig/nbody/test/plot.R | 74 +++++++++++++++++++++++++++++++ overlay.nix | 2 +- 4 files changed, 137 insertions(+), 66 deletions(-) delete mode 100644 garlic/fig/nbody/test.nix create mode 100644 garlic/fig/nbody/test/default.nix create mode 100644 garlic/fig/nbody/test/plot.R diff --git a/garlic/fig/nbody/test.nix b/garlic/fig/nbody/test.nix deleted file mode 100644 index 37810f9..0000000 --- a/garlic/fig/nbody/test.nix +++ /dev/null @@ -1,65 +0,0 @@ -{ - stdenv -, gnuplot -, jq -, experiments -, garlicTools -, getExpResult -, writeText -}: - -with garlicTools; -with stdenv.lib; - -let - experiment = builtins.elemAt experiments 0; - expResult = getExpResult { - garlicTemp = "/tmp/garlic-temp"; - inherit experiment; - }; - #set xrange [16:1024] - plotScript = writeText "plot.plg" '' - set terminal png size 800,800 - set output 'out.png' - set xrange [*:*] - - set nokey - set logscale x 2 - set logscale y 2 - set grid - - set xlabel "blocksize" - set ylabel "time (s)" - - plot filename using 1:2 with points - ''; - -in stdenv.mkDerivation { - name = "plot"; - phases = [ "installPhase" ]; - buildInputs = [ jq gnuplot ]; - preferLocalBuild = true; - dontPatchShebangs = true; - installPhase = '' - mkdir $out - for unit in ${expResult}/*/*; do - name=$(basename $unit) - log="$unit/stdout.log" - conf="$unit/garlic_config.json" - bs=$(jq .blocksize $conf) - awk "/^time /{print $bs, \$2}" $log >> $out/data.csv - done - gnuplot -e "filename='$out/data.csv'" ${plotScript} - cp out.png $out/out.png - ''; - #installPhase = '' - # mkdir $out - # for unit in ${expResult}/*/*; do - # name=$(basename $unit) - # log="$unit/stdout.log" - # bs=$(jq .blocksize $log) - # awk "/^time /{print $bs, \$2}" $log >> $out/data.csv - # done - #''; - #gnuplot -e "filename='$out/data.csv'" ${plotScript} -} diff --git a/garlic/fig/nbody/test/default.nix b/garlic/fig/nbody/test/default.nix new file mode 100644 index 0000000..b23aba1 --- /dev/null +++ b/garlic/fig/nbody/test/default.nix @@ -0,0 +1,62 @@ +{ + stdenv +, gnuplot +, jq +, experiments +, garlicTools +, getExpResult +, writeText +, rWrapper +, rPackages +}: + +with garlicTools; +with stdenv.lib; + +let + experiment = builtins.elemAt experiments 0; + expResult = getExpResult { + garlicTemp = "/tmp/garlic-temp"; + trebuchetStage = experiment; + experimentStage = getExperimentStage experiment; + }; + + customR = rWrapper.override { + packages = with rPackages; [ tidyverse ]; + }; + + plotScript = ./plot.R; + +in stdenv.mkDerivation { + name = "plot"; + buildInputs = [ jq gnuplot customR ]; + preferLocalBuild = true; + dontPatchShebangs = true; + + inherit expResult; + + src = ./.; + + buildPhase = '' + echo "using results ${expResult}" + + substituteAllInPlace plot.R + + for unit in ${expResult}/*/*; do + name=$(basename $unit) + log="$unit/stdout.log" + conf="$unit/garlic_config.json" + bs=$(jq .blocksize $conf) + awk "/^time /{print $bs, \$2}" $log >> data.csv + done + + Rscript plot.R + ''; + + installPhase = '' + mkdir $out + ln -s ${expResult} $out/result + cp *.png $out/ + cp data.csv $out/ + ''; +} diff --git a/garlic/fig/nbody/test/plot.R b/garlic/fig/nbody/test/plot.R new file mode 100644 index 0000000..1a6da21 --- /dev/null +++ b/garlic/fig/nbody/test/plot.R @@ -0,0 +1,74 @@ +library(ggplot2) +library(dplyr) +library(scales) + +# Load the dataset +df=read.table("data.csv", col.names=c("blocksize", "time")) + +bs_unique = unique(df$blocksize) +nbs=length(bs_unique) + +# Normalize the time by the median +D=group_by(df, blocksize) %>% mutate(tnorm = time / median(time) - 1) + +ppi=300 +h=5 +w=5 +png("box.png", width=w*ppi, height=h*ppi, res=ppi) + +# Create the plot with the normalized time vs blocksize +p = ggplot(D, aes(x=blocksize, y=tnorm)) + + + # Labels + labs(x="Blocksize", y="Normalized time", + title="Nbody granularity", + subtitle="@expResult@") + + + # Center the title + #theme(plot.title = element_text(hjust = 0.5)) + + + # Black and white mode (useful for printing) + #theme_bw() + + + # Draw boxplots + geom_boxplot(aes(group=blocksize)) + + + # Use log2 scale in x + scale_x_continuous(trans=log2_trans(), + breaks=bs_unique) + + + scale_y_continuous(breaks = scales::pretty_breaks(n = 10)) + + + # Add the maximum allowed error lines + geom_hline(yintercept=c(-0.01, 0.01), + linetype="dashed", color="red") + +# Render the plot +print(p) + +# Save the png image +dev.off() + +D=group_by(df, blocksize) %>% mutate(tnorm = time / median(time) - 1) + +png("scatter.png", width=w*ppi, height=h*ppi, res=ppi) + +# Create the plot with the normalized time vs blocksize +p = ggplot(D, aes(x=blocksize, y=time)) + + + labs(x="Blocksize", y="Time (s)", + title="Nbody granularity", + subtitle="@expResult@") + + + geom_point( + #position=position_jitter(width=0.2, heigh=0) + shape=21, size=1.5) + + scale_x_continuous(trans=log2_trans(), + breaks=bs_unique) + + scale_y_continuous(trans=log2_trans()) + +# Render the plot +print(p) + +# Save the png image +dev.off() diff --git a/overlay.nix b/overlay.nix index e4007c9..843e3cb 100644 --- a/overlay.nix +++ b/overlay.nix @@ -295,7 +295,7 @@ let # Figures generated from the experiments fig = { nbody = { - test = callPackage ./garlic/fig/nbody/test.nix { + test = callPackage ./garlic/fig/nbody/test/default.nix { experiments = [ self.bsc.garlic.exp.nbody.tampi ];