# print what's done
set -x

# exemplary training batch file for P(0,1)=0.5 binary automaton

# Neurons
N=128

# Merge
M=.5

# uncomment to compile, if wanted:
# make

# exemplary training 1000000 training, 1000000 testing symbols
# replace 0 by -1
# add header line with dimension before training starts
# invoke mng with learning rate 0.03 and initial context influence of 0.001
awk -vls="\n" -vno=2000000 -f bauto_5_5.awk |\
sed s/0/-1/ |\
awk 'BEGIN{print "#1"} 1' - |\
./mng 1000000 50000 $N .03 -1 `./neighsize 1000000 $N -1 .05 |\
awk 'NR==1{print $NF}'` $M .001 0 > output_bauto$N.dat 2> details_training_bauto$N.dat

# extract neuron activations
awk '/^#A/ {$1=""; print}' details_training_bauto$N.dat > activation_training_bauto$N.dat

# extract map entropy values in gnuplot
# > plot 'entropies_bauto128.dat' u 0:1 # plot fake 'entropy' values
# > plot 'entropies_bauto128.dat' u 0:2 # plot entropy-driven context influence
cat details_training_bauto$N.dat | grep entro | awk '{$1=$2="";print}' > entropies_bauto$N.dat

# extract the final network (all neurons)
awk '/test/{exit}; 1' output_bauto$N.out|tail -`expr $N + 2` | head -$N > neurons_bauto$N.dat

# extract receptive field from testing section (last 1000000 examples)
awk 'p==1{print} /testing/{p=1}' output_bauto$N.out | awk '1+sub(/-1/,"0",$1)' | awk -f rfield.awk > output_bauto$N\_rf.dat

# convert strings into gnuplotable tree format
# in gnuplot (there, replace $N by number of neurons):
# > plot 'output_bauto$N_rf.dat.plt.dat' w l, 'output_bauto$N_rf.dat.xplt.dat' w p
. batomaton2baum.sh output_bauto$N\_rf.dat

# play completion sound
echo -e '\a' > /dev/tty

# if desired, do a good compression of very redundant output
bzip2 -f -9 output_bauto$N.dat

set +x
