#!/bin/sh
## \section{lvq\_classify (shell script)}
## 
## \subsection*{Purpose}
## Classify with the LVQ (Learning Vector Quantizing)
##
## \subsection*{Description}
## For large images, run:"
## \begin{verbatim}
##   setenv TMPDIR <dir>"
## \end{verbatim}
## first, where {\tt <dir>} is a directory with plenty of free space."
## 
## \subsection*{Usage}
## \begin{verbatim}
## lvq_classify [-dec] 
##    [-sn structure number <integer>]
##    [-ff feature file <filename>]
##    [-tf tag file <filename>]
##    [-cf classified file <filename>]
##    [-sf statistics file <filename>]
##    [-perc percent of features for 
##           training <float>]
##    [-noc number of codebook vectors 
##          <integer>]
##    [-rlen run length <integer>]
## \end{verbatim}
## \begin{description}
##   \item[-U]
## 	Print usage information
##   \item[-sn]
## 	filter structure number
##   \item[-dec]
## 	decimate the filter output?
##   \item[-ff]
## 	feature file (HIPS image (sequence))
##   \item[-tf]
## 	tag file (HIPS image)
##   \item[-mf]
## 	mask file (mask out non-training regions) (HIPS image)
##   \item[-cf]
## 	classified (output) file (HIPS image)
##   \item[-sf]
## 	output statistics file
##   \item[-perc]
##	percentage of feature vectors to use for training (default 6)
##   \item[-noc]
##	number of codebook vectors (default 600)
##   \item[-rlen]
##	run length (default 20000)
## \end{description}
## 
## \subsection*{Date created}
## March 1994

# Default parameters:
DEC=FALSE		# don't use decimation of QMF filter
PERC=6
NOC=600
RLEN=20000
MASK_FILE=""

# Temporary files:
TRAIN_LVQ_DATA=`tempnam`
EVAL_LVQ_DATA=`tempnam`
CLASS_LVQ_DATA=`tempnam`
CODEBOOK=`tempnam`



print_usage ()
{
  # Print usage
  echo "lvq_classify"
  echo "\tOptions:"
  echo "\t  -U		-- print usage"
  echo "\t  -sn ##	-- filter structure number"
  echo "\t  -dec @@   	-- decimated feature file (TRUE or FALSE)"
  echo "\t  -ff @@  	-- input feature file"
  echo "\t  -tf @@  	-- input tag file"
  echo "\t  -mf @@	-- input mask file (mask out non-training regions)"
  echo "\t  -cf @@  	-- output classified file"
  echo "\t  -sf @@  	-- output statistics file"
  echo "\t  -perc ##	-- percent of features to use in training (default 6)"
  echo "\t  -noc ##	-- number of vectors in the codebook (default 600)"
  echo "\t  -rlen ##	-- run length of training (default 20000)"
  exit 1
}


clean ()
{
  rm -f $TRAIN_LVQ_DATA $EVAL_LVQ_DATA $CLASS_LVQ_DATA $CODEBOOK
}



train ()
{
  echo "+-----------------------------------------------------------------+"
  echo "| Train the neural network                                        |"
  echo "+-----------------------------------------------------------------+"

  # Convert from HIPS to LVQ data format
  if [ ${DEC} = TRUE ] ; then
    HIPS2LVQ_PARAM="-sn ${STRUCT}"
    DEC_FACT=`mindec -sn ${STRUCT}`
  else
    HIPS2LVQ_PARAM=""
    DEC_FACT=1
  fi
  if [ "${MASK_FILE}" = "" ] ; then
    hips2lvq ${HIPS2LVQ_PARAM} -p ${PERC} -train $TRAIN_LVQ_DATA \
     -out $EVAL_LVQ_DATA $FEATURE_FILE $TAG_FILE
  else
    # First generate the training data file and a dummy evaluation datafile,
    # and then generate a full datafile without class labels
    hips2lvq ${HIPS2LVQ_PARAM} \
	-m ${MASK_FILE} -p ${PERC} -train $TRAIN_LVQ_DATA \
	-out $EVAL_LVQ_DATA $FEATURE_FILE $TAG_FILE
    hips2lvq ${HIPS2LVQ_PARAM} \
	-out $EVAL_LVQ_DATA $FEATURE_FILE 
  fi

  # Train the LVQ classifier
  eveninit -din $TRAIN_LVQ_DATA -cout $CODEBOOK -noc ${NOC}
  mindist -cin $CODEBOOK
  balance -din $TRAIN_LVQ_DATA -cin $CODEBOOK -cout $CODEBOOK
  olvq1 -din $TRAIN_LVQ_DATA -cin $CODEBOOK -cout $CODEBOOK \
    -rlen ${RLEN}
}



classification ()
{
  echo "+-----------------------------------------------------------------+"
  echo "| Use the trained network to classify                             |"
  echo "+-----------------------------------------------------------------+"
  classify -din $EVAL_LVQ_DATA -cin $CODEBOOK -dout $CLASS_LVQ_DATA 
  CLASS_ROWS=`hsize -rows $TAG_FILE`
  CLASS_ROWS=`calc ${CLASS_ROWS}/${DEC_FACT}`
  CLASS_COLS=`hsize -cols $TAG_FILE`
  CLASS_COLS=`calc ${CLASS_COLS}/${DEC_FACT}`
  lvq2hips -s ${CLASS_ROWS} ${CLASS_COLS} $CLASS_LVQ_DATA | \
	htob | enlarge -s $DEC_FACT > ${CLASS_FILE}
}



summary ()
{
  echo "	Classifier			LVQ"   	      >  ${STAT_FILE}
  echo "	Samples used for training       ${PERC}%"     >> ${STAT_FILE}
  echo "	Codebook vectors                ${NOC}"       >> ${STAT_FILE}
  echo "	Training runlength              ${RLEN}"      >> ${STAT_FILE}
  echo "	Classified datafile:            ${CLASS_FILE}">> ${STAT_FILE}
}




# M A I N   P R O G R A M


until [ $# -eq 0 ]; do
  case "${1}" in
    -U		) shift; print_usage ;;
    -sn		) shift; STRUCT="${1}"; shift;;
    -dec	) shift; if [ "${1}" = "TRUE" -o "${1}" = "FALSE" ] ; then
			   DEC=${1}; shift ;
			 else
			   DEC=TRUE
			 fi;;
    -perc	) shift; PERC=${1}; shift;;
    -noc	) shift; NOC=${1}; shift;;
    -rlen	) shift; RLEN=${1}; shift;;
    -ff		) shift; FEATURE_FILE=${1}; shift;;
    -tf		) shift; TAG_FILE=${1}; shift;;
    -mf		) shift; MASK_FILE=${1}; shift;;
    -cf		) shift; CLASS_FILE=${1}; shift;;
    -sf		) shift; STAT_FILE=${1}; shift;;
    *		) shift; print_usage ;;
  esac
done

train
classification
summary
clean
