#!/bin/sh

echo '               --------- OUTLINE -----------'
echo 'This is a program that uses the "analyze" utility to create a'
echo 'reduced dimensionality data set from an original data set. The original'
echo 'data set has its components analyzed (pca or cda). The user can'
echo 'interactively select a range of components to have data projected'
echo 'thru to produce a new data set. Plots can be made of the projection'
echo 'vectors (principal components), the projected data, and the histograms'
echo 'of the projected data.'


# useful stuff you might want to change
matlab=matlab4
gnuplot=gnuplot
temp_dir=.
temp_prefix=$temp_dir/rm_me.$$
temp_index=0
rm=/bin/rm
cpp=/lib/cpp

if [ $# -ne 5 ]
then
echo 'Usage: DataReduce <df file> <input xdim> <input ydim> <target xdim> <target ydim>'
exit
fi

# args
df_file=`echo $1 | sed 's/\.df//'`
i_xdim=$2
i_ydim=$3
t_xdim=$4
t_ydim=$5

new_temp_file() {
	temp_file=$temp_prefix.$temp_index ;
	temp_index=`expr $temp_index + 1` ;
}


analysis=""
while [ "$analysis" != "pca" -a "$analysis" != "cda" ]
do
 echo
 echo "Please input analysis method (cda/pca):"
 read analysis
done

echo
echo "Analysis: $analysis"
echo "Data: $df_file.df"
echo "Input dimensions: [$i_xdim x $i_ydim]"
echo "Target dimensions: [$t_xdim x $t_ydim]"
echo


if [ "$analysis" = "pca" ]
then
	component_suffix=pc
	values_suffix=pvs
fi

if [ "$analysis" = "cda" ]
then
	component_suffix=cv
	values_suffix=cvs
fi

echo
echo "Computing $analysis for $df_file"
analyze -d $df_file.df -P $df_file -$analysis \
	-InputsXdim $i_xdim -InputsYdim $i_ydim \
	-TargetsXdim $t_xdim -TargetsYdim $t_ydim 

echo
user_input=y
n_vals=10
n_components=`am2gnuplot -s < $df_file.$values_suffix | wc -l | sed 's/ //g'`
while [ "$user_input" != "n" ]
do
	echo "The first $n_vals principal values are:"
	am2gnuplot -s < $df_file.$values_suffix | cat | nl | head -$n_vals
	echo "Would you like to view those again (perhaps more)? (y/n)"
	read user_input
	if [ "$user_input" != "n" ]
	then
		echo "How many values do you want to see?"
		read n_vals
	fi
done

# preview? ...you can add other graphics packages here!

echo "Would you like to view any components? (y/n)"
read user_input
if [ "$user_input" = "y" ]
then

 # preview w/gnuplot
 user_input=y
 while [ "$user_input" != "n" ]
 do
	echo "Would you like to view a component with gnuplot? (y/n)"
	read user_input
	if [ "$user_input" = "y" ]
	then
		echo
		echo "Please input a component number [1-$n_components]:"
		read number
		if [ -r $df_file.$number.$component_suffix ]
		then
			new_temp_file ;
			am2gnuplot < $df_file.$number.$component_suffix > $temp_file
			if [ $i_ydim -eq 1 ]
			then
				plot_function="plot \"$temp_file\" with impulses ;"
				new_temp_file ;
				echo "set nokey" > $temp_file
			else
				plot_function="splot \"$temp_file\" with lines ;"
				new_temp_file ;
				echo "set nokey" > $temp_file
				echo "set parametric ; set contour ;" >> $temp_file
#				echo "set nosurface ; set view 0,0 ;" >> $temp_file
			fi
			echo "set title \"$df_file $component_suffix #$number\" " >> $temp_file
			echo "$plot_function" >> $temp_file
			echo "pause 3600 ;" >> $temp_file
			$gnuplot < $temp_file &
			processes="$processes $!"
		else
			echo "Unable to read $df_file.$number.$component_suffix"
		fi
	fi

 done # done gnuplot

 # preview w/matlab
 user_input=y
 while [ "$user_input" != "n" ]
 do
	echo "Would you like to view a component with matlab? (y/n)"
	read user_input
	if [ "$user_input" = "y" ]
	then
		echo
		echo "Please input a component number [1-$n_components]:"
		read number
		if [ -r $df_file.$number.$component_suffix ]
		then
			new_temp_file ;
			am2matlab < $df_file.$number.$component_suffix > $temp_file.mat
			load="load $temp_file.mat" 
			if [ $i_ydim -eq 1 ]
			then
				plot_function="bar(am2matlab0);"
			else
				plot_function="colormap(gray); image(am2matlab0);"
			fi
			new_temp_file; 
			echo "$load" > $temp_file
			echo "$plot_function" >> $temp_file
			echo "title('$df_file $component_suffix #$number')" >> $temp_file
			echo "pause(3600) ;" >> $temp_file
			$matlab < $temp_file &
			processes="$processes $!"
		else
			echo "Unable to read $df_file.$number.$component_suffix"
		fi
	fi

 done # done matlab

 # preview w/mathematica
 user_input=y
 while [ "$user_input" != "n" ]
 do
	echo "Would you like to view a component with mathematica? (y/n)"
	read user_input
	if [ "$user_input" = "y" ]
	then
		mathdir=$NNTOOLS/contrib/Mathematica
		echo
		echo "Please input a component number [1-$n_components]:"
		read number
		if [ -r $df_file.$number.$component_suffix ]
		then
			if [ $i_ydim -eq 1 ]
			then
				(cat $df_file.$number.$component_suffix | $mathdir/MathPlotLine) &
			else
				(cat $df_file.$number.$component_suffix | $mathdir/MathPlotDensity) &
			fi
			# not necessary 'cause mathematica leaves up the window on exit
			# processes="$processes $!"
		else
			echo "Unable to read $df_file.$number.$component_suffix"
		fi
	fi

 done # done mathematica

fi # done preview

echo ; echo "Do you want to project data thru the component vectors? (y/n)"
read user_input
if [ "$user_input" = "y" ]
then

 range=0
 while [ $range -lt 1 -o $range -gt $n_components ]
 do
	echo "Please input the range of principal components"
	echo "to project the data though [1-$n_components]:"
	read range
 done

 prj=""
 while true
 do
	echo ; 
	ls *.df
	echo ; echo "Please input a .df to project ( <done> if finished ):"
	read prj
	prj=`echo $prj | sed 's/\.df//'`

	# finished
	if [ "$prj" = "done" ]
	then
		break
	fi


	echo ; echo "Do you want to histogram the projections? (y/n):"
	read histogram
	if [ "$histogram" = "y" ]
	then
		echo ; 
		echo "If you do not know the max/min you want for the histograms"
		echo "then type -autohistogram <nbin> which will use the max/min in the"
		echo "projections. If you know the max/min you want for the histograms"
		echo "then type -histogram <nbins> <min> <max>, where <nbins> is the"
		echo "number of bins in the histogram:"
		read histogram
		have_histograms=true
	else
		histogram=""
	fi

	if [ -r $prj.df ] 
	then

		analyze -project -d $prj.df -range $range -P $df_file -S $component_suffix \
			$histogram \
			-InputsXdim $i_xdim -InputsYdim $i_ydim \
			-TargetsXdim $t_xdim -TargetsYdim $t_ydim 


		echo ; echo
		echo "Moving $df_file.$component_suffix.prj to $prj.$component_suffix.prj"
		mv $df_file.$component_suffix.prj $prj.$component_suffix.prj
		if [ "$histogram" != "" ]
		then

		echo "Moving $df_file.$component_suffix.hst to $prj.$component_suffix.hst"
		mv $df_file.$component_suffix.hst $prj.$component_suffix.hst

		echo "Moving $df_file.$component_suffix.norm.hst to $prj.$component_suffix.norm.hst"
		mv $df_file.$component_suffix.norm.hst $prj.$component_suffix.norm.hst

		fi

	else
		echo "Unable to read $prj.df!"
	fi # done w/df	
 done

 echo "Your new data files will have dimensionality: [$range x 1]"

fi # end project


while true
do

	echo ; echo "Do you want to view your projection distributions with gnuplot? (y/n)"
	read user_input

	if [ "$user_input" = "n" ]
	then
		break
	fi

	echo
	echo "Please input 1st component number [1-$n_components]:"
	read first_component
	if [ "$first_component" = "" ]
	then
		break
	fi

	echo
	echo "Please input 2nd component number [1-$n_components]:"
	read second_component
	if [ "$second_component" = "" ]
	then
		break
	fi

	echo
	echo "Please input 3rd component number [1-$n_components, <cr> for 2d]:"
	read third_component


	echo
	ls *.$component_suffix.prj
	echo "Please input a list of files to plot:"
	read prj_files
	if [ "$prj_files" = "" ]
	then
		break
	fi

	if [ "$third_component" = "" ] # 2d
	then
		plotsuffix="$first_component.$second_component"
		cutfields='{print $'"$first_component\"\t\""'$'"$second_component}"
		plot_function="plot "
		style="set data style linespoints ;"
	else
		plotsuffix="$first_component.$second_component.$third_component"
		cutfields='{print $'"$first_component\"\t\""'$'"$second_component\"\t\""'$'"$third_component}"
		plot_function="splot "
		parametric="set parametric ;"
		style="set data style linespoints ;"
	fi

	for f in $prj_files
	do
			new_temp_file; 
			am2ascii < $f | awk "$cutfields" > $temp_file
			plot_function="$plot_function \"$temp_file\" t \"$f\","
	done

	new_temp_file; 
	echo "set title \"Projections thru $first_component $second_component $third_component\" " > $temp_file
	echo "$style" >> $temp_file
	echo "$parametric" >> $temp_file
	echo "$plot_function" | sed 's/,$//' >> $temp_file
	echo "pause 3600 ;" >> $temp_file
	$gnuplot < $temp_file &
	processes="$processes $!"

done # view projections

while true
do
	if [ ! "$have_histograms" ]
	then
		break
	fi

	echo ; echo "Do you want to view your histograms with gnuplot? (y/n)"
	read user_input

	if [ "$user_input" = "n" ]
	then
		break
	fi

	echo "Your histograms files are:"
	ls *.$component_suffix*hst
	echo
	echo "Please input the files you would like to see plotted together:"
	read hist_files


	echo
	echo "Please input a component number [1-$range]:"
	read number

	plot_function="plot "
	for hist_file in $hist_files
	do

	 if [ -r $hist_file ]
	 then

		new_temp_file; 
		am2ascii < $hist_file > $temp_file
		
		plot_function="$plot_function \"$temp_file\" using $number t \"$hist_file\" with lines,"
	 else
			echo "Unable to read $hist_file"
			break
	 fi

	done # each hist file

	new_temp_file; 
	echo "set title \"$component_suffix #$number\" " > $temp_file
	echo "$plot_function" | sed 's/,$//' >> $temp_file
	echo "pause 3600 ;" >> $temp_file
	$gnuplot < $temp_file &

	processes="$processes $!"

done # view hist

# kill off any plotting processes/ delete temp files
if [ "$processes" != "" ]
then
 echo ; echo "Kill all graphics processes? (y/n):"
 read user_input
 if [ "$user_input" != "n" ]
 then
 	for p in $processes
 	do
			# echo $p
			kill -9 $p
 	done
	# clean up
	$rm -f $temp_prefix*
 fi # end kill
fi # end ask kill


echo ; echo
echo '           --------- Now what? -----------'
echo 'Now that you have analyzed the data you should'
echo 'consider the following:'
echo '      - if the projection distributions and histograms'
echo '        indicate that there is separation, then the'
echo '        projection vectors (principal components) might'
echo '        make good feature detectors, or good initial weight'
echo '        values for neural network nodes connecting to the'
echo '        input of the network.'
echo '      - if you did pca and there were many small eigen values'
echo '        (principal values) then your data has much redundancy'
echo '        and you should use the projected data as input to'
echo '        a classifier because the dimensionality has been'
echo '        reduced.'
echo 'To translate a projection file produced from "analyze"'
echo 'into a Type1 file recognized by "bpmake" generated neural'
echo 'network simulations use "am2raw" which strips the header'
echo 'off the file leaving a flat file of single precision floats.'
echo 'For example:  am2raw < mydata.prj > mydata.type1'
echo ; echo ;









