#! /bin/bash
# Last edited on 2023-11-29 11:14:00 by stolfi

# Does a preliminary principal component analysis of the
# filtered EEG runs of one or more subjects.

fdir="flt-runs-B"             # Directory where the filtered runs live.
frameset="all_phase_frames"   # What set of frames are we analyzing.

# Image basis for electrode influence
bpref="imgbasis/280_320_e128"  # Prefix for electrode image basis files.
btype=03                       # Type of electrode image basis.

vmax=160 # Max sample value for electrode plots.

function do_subj_pca() {

  sid="$1"; shift;  # Subject ID number (3 digits, zero padded).

  opref="${fdir}/s${sid}_${frameset}"
  xfile="${opref}.txt"

  # Collects all fixation and stimulus frames of filtered
  # runs froms subject ${sid}. Writes the collected frames
  # to file ${xfile}.
  # Then runs PCA on that file and writes the components
  # to files 

  echo "=== subject ${sid} ===" 1>&2
  
  echo "collecting fixation and stimulus frames..." 1>&2
  
  # Collect the frames proper:
  tfile="/tmp/.xfr"  # Temporary file with frames but no headers.
  rm -f ${tfile}
  for rfile in `ls ${fdir}/s${sid}/r?????.txt | sort` ; do
    extract_phase_frames.gawk -v phase="FX" ${rfile} >> ${tfile}
    extract_phase_frames.gawk -v phase="ST" ${rfile} >> ${tfile}
  done

  # Fake a minimal header:
  rm -f ${xfile}
  nt=`cat ${tfile} | egrep -v -e '[=]' | wc -l`
  printf "nt = %d\n" "${nt}" >> ${xfile} 
  egrep -e '^(nc|channels|ne|fsmp) *[=]' "${fdir}/s${sid}/r00101.txt" >> ${xfile}  
  cat ${tfile} >> ${xfile} 
  
  # Cleanup:
  rm -f ${tfile}

  echo "running PCA on the collected frames..." 1>&2

  # Runs PCA on file ${xfile}:
  nmeeg_correl \
      -outPrefix ${opref} \
      -zeroMean T \
      -maxComps 10 \
    < ${xfile}

  echo "generating principal component images..." 1>&2
  
  for cfile in ${opref}_P???_eig.txt ; do
    cpref="${cfile%.*}_b${btype}" # Prefix for this principal component.
    cimage="${cpref}_f000000.png"
    nmeeg_animate \
        -basis ${bpref} ${btype} \
        -skip 0 -read 1 \
        -outPrefix ${cpref} \
      < ${cfile}
    # display -title '%d/%f' ${cimage}
  done

  echo "removing the first two PCAs..." 1>&2

  # runs=( 00103 00229 00425 00829 )
  runs=( 00229 )
  for rid in ${runs[@]} ; do
    ppref=${fdir}/s${sid}
    rpref=${fdir}/s${sid}/r${rid}
    rfile="${rpref}.txt"
    
    nmeeg_comp_analysis \
      -pattern BL0 ${ppref}_${frameset}_P000_eig.txt \
      -pattern H10 ${ppref}_${frameset}_P001_eig.txt \
      -normalize \
      -writeComp BL0 ${rpref}_BL0.txt = BL0 \
      -writeComp H10 ${rpref}_H10.txt = H10 \
      -writeComp BLH ${rpref}_BLH.txt = BL0 H10 \
      -delete BL0 H10 \
      < ${rpref}.txt \
      > ${rpref}_RES.txt
      
    display -title '%d/%f' ${rpref}.png
    nmeeg_plot_channels.sh SHOW ${rpref}_BL0 0 ${vmax} 0 9999  0 0  1400 500

    nmeeg_plot_channels.sh SHOW ${rpref}_H10 0 ${vmax} 0 9999  0 0  1400 500 
    plot_run_spectrum.sh SHOW ${rpref}_H10 0 127  50  500

    nmeeg_plot_channels.sh SHOW ${rpref}_BLH 0 ${vmax} 0 9999  0 0  1400 500 

    nmeeg_plot_channels.sh SHOW ${rpref}_RES 0 ${vmax} 0 9999  0 0  1400 500 
    plot_run_spectrum.sh SHOW ${rpref}_RES 0 127  50  500

  done

}

do_subj_pca 021
