#!/bin/bash -p

# Plot the statistics data gained with kdTreeWithStatistics.py
# Assumes that CWD = ANN/plot
# Generate also thumbnails and copy all PNgs to ~/Data/WWW/software/approximate_nn/plots


echo "Creating plots .."

for format in "pdf" "png"
do

	# plots with dimension on x-axis
	for points in "10000" "100000" "500000"
	do
		for eps in "0.1" "0.3" "1.0"
		do
			for distrib in "uniform" "correlated" "clustered"
			do

				if [[ ! -e dat/$distrib-$eps-$points.dat ]]
				then
					# skip this set of parameters
					continue
				fi

				if [[ $format = "pdf" ]]
				then
					terminal='fname "Helvetica" fsize 14 linewidth 5 size 15cm,11cm'
				else
					terminal='font "/Library/Fonts/Tahoma.ttf,14" linewidth 3'
				fi

				gnuplot << EOF

				set terminal $format $terminal

				set key left
				set xlabel "dimensions"
				set ylabel "fraction of leaves visited" offset 1

				set style data lines

				set output "$distrib-$eps-$points.$format"
				set title "$distrib distribution, eps = $eps, #points = $points"

				plot "dat/$distrib-$eps-$points.dat" using 1:(\$2/$points) title "exact NN", \
					 "dat/$distrib-$eps-$points.dat" using 1:(\$3/$points) title "approx NN"
EOF

			done

		done
	done


	# plots with epsilon on x-axis
	for points in "10000" "100000"
	do
		for dim in "9" "21" "30"
		do
			for distrib in "uniform" "correlated" "clustered"
			do

				if [[ ! -e dat/$distrib-dim=$dim-$points.dat ]]
				then
					# skip this set of parameters
					continue
				fi

				if [[ $format = "pdf" ]]
				then
					terminal='fname "Helvetica" fsize 14 linewidth 5 size 15cm,11cm'
				else
					terminal='font "/Library/Fonts/Tahoma.ttf,14" linewidth 3'
				fi

				gnuplot << EOF

				set terminal $format $terminal

				set key left bottom
				set xlabel "epsilon"
				set ylabel "fraction of leaves visited" offset 1
				set yrange [0:1]

				set style data lines

				set output "$distrib-dim=$dim-$points.$format"
				set title "$distrib distribution, dim = $dim, #points = $points"

				plot "dat/$distrib-dim=$dim-$points.dat" using 1:(\$2/$points) title "exact NN", \
					 "dat/$distrib-dim=$dim-$points.dat" using 1:(\$3/$points) title "approx NN"
EOF

			done

		done
	done


	# plots with N on x-axis
	for dim in "9" "21" "30"
	do
		for eps in "0.1" "0.3" "1.0"
		do
			for distrib in "uniform" "correlated" "clustered"
			do

				if [[ ! -e dat/$distrib-dim=$dim-eps=$eps.dat ]]
				then
					# skip this set of parameters
					continue
				fi

				if [[ $format = "pdf" ]]
				then
					terminal='fname "Helvetica" fsize 14 linewidth 5 size 15cm,11cm'
				else
					terminal='font "/Library/Fonts/Tahoma.ttf,14" linewidth 3'
				fi

				gnuplot << EOF

				set terminal $format $terminal

				set key left
				set xlabel "num points / 1000"
				set ylabel "num leaves visited / 1000" offset 1
				set y2label "fraction of leaves visited" offset -1
				set y2range [0:1]
				set y2tics 0, 0.2
				set ytics nomirror

				set style data lines

				set output "$distrib-dim=$dim-eps=$eps.$format"
				set title "$distrib distribution, dim = $dim, epsilon = $eps"

				plot "dat/$distrib-dim=$dim-eps=$eps.dat" using 1:(\$2/(\$1*1000)) axes x1y2 title "exact NN (fract)" \
						with lines linecolor rgb "orange", \
					 "dat/$distrib-dim=$dim-eps=$eps.dat" using 1:(\$2/1000) title "exact NN (abs)" \
					 	with lines linecolor rgb "red" , \
					 "dat/$distrib-dim=$dim-eps=$eps.dat" using 1:(\$3/(\$1*1000)) axes x1y2 title "approx NN (fract)" \
					 	with lines linecolor rgb "cyan", \
					 "dat/$distrib-dim=$dim-eps=$eps.dat" using 1:(\$3/1000) title "approx NN (abs)" \
					 	with lines linecolor rgb "green" 
EOF

			done

		done
	done



done

echo "Creating thumbnails .."

./genthumbnails

echo " Copying .."

./copy_imgs

