/*
  File name: ddset.c
  Created by: Ljubomir Buturovic
  Created: 09/11/2002
  Purpose: various display functions.
*/

/*
  Copyright 2004 Ljubomir J. Buturovic

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation files
  (the "Software"), to deal in the Software without restriction,
  including without limitation the rights to use, copy, modify, merge,
  publish, distribute, sublicense, and/or sell copies of the Software,
  and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.
*/

static char rcsid[] = "$Id: ddset.c,v 1.21 2005/04/07 02:00:06 ljubomir Exp $";

#include <stdlib.h>
#include <string.h>
#include "ddset.h"
#include "pau.h"
#include "lau.h"

static void print_filename(int mode, char *fname, int nv)
{
  int  len;
  char *space;

  /*
    Calculate the length of the trailing space filler.
  */
  len = PCP_QLEN-strlen(fname)-24;
  if (mode == 2)
    len -= 1;
  space = str_create(len, ' ');
  if (mode == 1)
    printf("| File");
  else
    printf("| Class");
  printf(" %s: %5d vectors.", fname, nv);
  if (space)
    printf("%s|\n", space);
  else
    printf("|\n");
  free(space);
}

/*
  Display status of training and test datasets.
*/
void dstatus(void)
{
  int  i;
  FILE *outdev;

  outdev = stdout;

  clear_screen();
  inverse_video();
  printf(PCP_LINE);
  printf(PCP_EMPTY_LINE);
  printf("|                          T E S T   D A T A S E T                           |\n");
  printf(PCP_EMPTY_LINE);
  if (teds != (struct dataset *) 0)
    {
      printf("| Number of vectors: %6d                                                  |\n", teds->nv);
      printf("| Number of features: %5d                                                  |\n", teds->d);
      printf("| Number of files: %8d                                                  |\n", teds->c);
      printf("| File cardinalities:                                                        |\n");
      for (i = 0; i < teds->c; i++)
	print_filename(1, teds->fnames[i], teds->nd[i]);
    }
  else
    printf("| Test data set undefined.                                                   |\n");
  printf(PCP_EMPTY_LINE);
  printf(PCP_LINE);

  reset_video();
  printf("\n");
  inverse_video();
  printf(PCP_LINE);
  printf(PCP_EMPTY_LINE);
  printf("|                      T R A I N I N G   D A T A S E T                       |\n");
  printf(PCP_EMPTY_LINE);
  if (tds != (struct dataset *) 0)
    {
      printf("| Number of vectors: %6d                                                  |\n", tds->nv);
      printf("| Number of features: %5d                                                  |\n", tds->d);
      printf("| Number of classes: %6d                                                  |\n", tds->c);
      printf("| Class cardinalities:                                                       |\n");
      for (i = 0; i < tds->c; i++)
	print_filename(2, tds->fnames[i], tds->nd[i]);
    }
  else
    printf("| Training dataset undefined.                                                |\n");
  printf(PCP_EMPTY_LINE);
  printf(PCP_LINE);

  reset_video();
  pwait();
}

static void display_vectors(int type)
{
  int  i;
  int  j;
  int  k;
  int  m;
  int  idx;
  int  length;
  int  cnt;
  char *str;
  char *title;
  struct dataset *dset;

  clear_screen();
  inverse_video();
  printf(PCP_LINE);
  printf(PCP_EMPTY_LINE);
  if (type == 0)
    printf("|                      T R A I N I N G   D A T A S E T                       |\n");
  else
    printf("|                           T E S T   D A T A S E T                          |\n");
  printf(PCP_EMPTY_LINE);
  printf(PCP_LINE);
  if (type == 0)
    dset = tds;
  else
    dset = teds;
  if (dset)
    {
      idx = 0;
      length = 16;
      str = malloc(length+1);
      memset(str, ' ', length-1);
      str[length-1] = '|';
      str[length] = '\0';
      for (i = 0; i < dset->c; i++)
	{
	  printf(PCP_LINE);
	  title = malloc(strlen("C L A S S  ")+strlen(dset->fnames[i])+1);
	  sprintf(title, "C L A S S  %s", dset->fnames[i]);
	  printf("|");
	  center_line(stdout, title, 76);
	  printf("|\n");
	  free(title);
	  printf(PCP_LINE);
	  for (j = 0; j < dset->nd[i]; j++)
	    {
	      for (k = 0; k < dset->d; k++)
		{
		  if (k == 0)
		    printf("| %10d |", idx+1);
		  else if ((k % 4) == 0)
		    printf("|            |");
		  printf(" %13.6f |", dset->x[idx][k]);
		  if (((k+1) % 4) == 0)
		    printf("\n");
		  else if (k == dset->d-1)
		    {
		      cnt = 4-(k+1)%4;
		      for (m = 0; m < cnt; m++)
			printf("%s", str);
		      printf("\n");
		    }
		}
	      idx++;
#if 0
	      if (dset->d > 4)
		printf(PCP_LINE);
#endif
	    }
	}
      free(str);
      printf(PCP_LINE);
    }
}

/*
  Display dataset.
*/
void ddset(void)
{
  int  type;
  int  min_range;
  int  max_range;
  char *msg;

  clear_screen();
  inverse_video();
  msg = malloc((PCP_QLEN+1)*sizeof(char));
  min_range = 0;
  max_range = 1;
  sprintf(msg, " Display training (%d) or test (%d) dataset [%d]:", 
	  min_range, max_range, min_range);
  cursor_on();
  type = input_integer(stdin, stdout, msg, PCP_QLEN, &min_range, &min_range, 
		       &max_range);
  cursor_off();
  free(msg);
  display_vectors(type);
  reset_video();
  pwait();
}

/*
  Display eigenvalues and condition numbers.
*/
void ddevl(int c, int d, float **evlc)
{
  int   i;
  int   j;
  float cnd;

  clear_screen();
  inverse_video();
  printf(PCP_XLINE);
  printf(PCP_EMPTY_LINE);
  printf("|             C L A S S   C O V A R I A N C E   M A T R I C E S              |\n");
  printf("|     E I G E N V A L U E S   A N D   C O N D I T I O N   N U M B E R S      |\n");
  printf(PCP_EMPTY_LINE);
  printf(PCP_XLINE);
  for (i = 0; i < c; i++)
    {
      for (j = 0; j < d; j++)
	printf("| Class: %8d | Eigenvalue %8d: %12.5g                        |\n", i+1, j+1, evlc[i][j]);
      cnd = evlc[i][0]/evlc[i][d-1];
      printf("| Class: %8d | Condition number:    %12.5g                        |\n", i+1, cnd);
      printf(PCP_XLINE);
    }
  reset_video();
}



syntax highlighted by Code2HTML, v. 0.9.1