Ejemplo n.º 1
0
int find_winner_euc(struct entries *codes, struct data_entry *sample, 
			struct winner_info *win, int knn)
{
  struct data_entry *codetmp;
  int dim, i, masked;
  float diffsf, diff, difference;
  eptr p;

  dim = codes->dimension;
  win->index = -1;
  win->winner = NULL;
  win->diff = -1.0;
  
  /* Go through all code vectors */
  codetmp = rewind_entries(codes, &p);
  diffsf = FLT_MAX;
  
  while (codetmp != NULL) {
    difference = 0.0;
    masked = 0;

    /* Compute the distance between codebook and input entry */
    for (i = 0; i < dim; i++)
      {
	if ((sample->mask != NULL) && (sample->mask[i] != 0))
	  {
	    masked++;
	    continue; /* ignore vector components that have 1 in mask */
	  }
	diff = codetmp->points[i] - sample->points[i];
	difference += diff * diff;
	if (difference > diffsf) break;
      }
    
    if (masked == dim)
      return 0; /* can't calculate winner, empty data vector */
    
    /* If distance is smaller than previous distances */
    if (difference < diffsf) {
      win->winner = codetmp;
      win->index = p.index;
      win->diff = difference;
      diffsf = difference;
    }
    
    codetmp = next_entry(&p);
  }
  
  if (win->index < 0)
    ifverbose(3)
      fprintf(stderr, "find_winner_euc: can't find winner\n");

  return 1; /* number of neighbours */
}
Ejemplo n.º 2
0
int find_winner_euc2(struct entries *codes, struct data_entry *sample, 
		     struct winner_info *win, int knn)
{
  struct data_entry *codetmp;
  int dim, i;
  float diffsf, diff, difference, *s, *c;
  eptr p;

  if (sample->mask != NULL)
    return find_winner_euc(codes, sample, win, knn);

  dim = codes->dimension;
  win->index = -1;
  win->winner = NULL;
  win->diff = -1.0;
  
  /* Go through all code vectors */
  codetmp = rewind_entries(codes, &p);
  diffsf = FLT_MAX;
  
  while (codetmp != NULL) {
    difference = 0.0;

    /* Compute the distance between codebook and input entry */
    c = codetmp->points; s = sample->points;
    for (i = 0; i < dim; i++)
      {
	diff = *c++ - *s++;
	difference += diff * diff;
      }
    
    /* If distance is smaller than previous distances */
    if (difference < diffsf) {
      win->winner = codetmp;
      win->index = p.index;
      win->diff = difference;
      diffsf = difference;
    }
    
    codetmp = next_entry(&p);
  }
  
  if (win->index < 0)
    ifverbose(3)
      fprintf(stderr, "find_winner_euc: can't find winner\n");

  return 1; /* number of neighbours */
}
Ejemplo n.º 3
0
int print_eps(FILE *fp, struct eps_info *einfo)
{
  float color;
  float xstep, ystep, radius;
  int x, y, numlabs, i;
  char *label;
  eptr p;
  char *select_topol_s, *draw_block_s, *start_row_s, *end_row_s;
  struct data_entry *dtmp;
  struct umatrix *umat = einfo->umat;

  end_row_s = "NL";

  switch (umat->topol) 
    {
    case TOPOL_HEXA:
      select_topol_s = "topol_hexa";
      draw_block_s = "H";
      start_row_s = "XSH";
      break;
    case TOPOL_RECT:
      select_topol_s = "topol_rect";
      draw_block_s = "R";
      start_row_s = "XSR";
      break;
    default:
      fprintf(stderr, "can't print topology %d\n", umat->topol);
      return 0;
      break;
    }

  /*  radius = eps_width / (2.0 * sqrt(3) * (float)umat->mxdim); */
  xstep = einfo->xstep;
  ystep = einfo->ystep;
  radius = einfo->radius;

  /* print eps headers */
  fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n");
  fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", 
	  (int)ceil(einfo->width), (int)ceil(einfo->height));
  
  fprintf(fp, "%%%%Title: %s\n%%%%Creator: umat V1.0\n", 
	  ps_string_filter(einfo->title));
  
  fprintf(fp, "%%%%CreationDate: %s", ps_string_filter(get_date()));
  fprintf(fp, "%%%%Pages: 0\n");
  fprintf(fp, "%%%%DocumentFonts: %s\n%%%%DocumentNeededFonts: %s\n",
	  fontname, fontname);
  fprintf(fp, "%%%%EndComments\n");
  
  /* COPY HEADER */
  if (print_header(fp, headerfile))
    return 1; /* error */

  fprintf(fp, "/radius %f def\n/xstep %f def\n/ystep %f def\n",
	  radius, xstep, ystep);

  /* font selection */
  fprintf(fp, "%%%%IncludeFont: %s\n", fontname);
  fprintf(fp, "/fontname /%s def\n", fontname);
  if (fontsize > 0.0) 
    fprintf(fp, "/fontsize %f def\n", fontsize);
  fprintf(fp, "selfont\n");
  
  fprintf(fp, "/doborder %s def\n", doborder ? "true" : "false");
  /* print umat */

  fprintf(fp, "/wt %f def /bt %f def\n", white_treshold, black_treshold);

  fprintf(fp, "/y 0 def\n/xoff %f def\n/yoff %f def\n", 
	  einfo->x0, (einfo->height - einfo->y0));

  if (drawblocks)
    for (y = 0; y < umat->uydim; y++)
      {
	fprintf(fp, "%s ", start_row_s);
	for (x = 0; x < umat->uxdim; x++)
	  fprintf(fp, "%d %s ", (int)(100 * umat->uvalue[x][y]), draw_block_s);
	fprintf(fp, "%s\n", end_row_s);
      }
  
  /* print labels */

  fprintf(fp, "/y 0 def\n/xoff %f def\n/yoff %f def\n", 
	  einfo->x0, (einfo->height - einfo->y0));

  if (drawlabels)
    {
      dtmp = rewind_entries(umat->codes, &p);
      
	/*---------------------------------------------------------------------------
	* Modificado para reportar los datos de la umatrix en formato legible
	* para Nams ( Nucleic Acid Modeling System ). El formato tiene la siguiente
	* forma:
	*         --------------------------------------------
	*       |  <type>   |  <i>   |  <j>    |    <value>   |
	*        --------------------------------------------
	* Siendo type = { Hexagon o Label }, i, j, las cooredenadas sobre el mapa
	* y value y string para el caso de type = Label y un double para el caso
	* de type = Hexagon
	* 
	* Author :
	*     Nestor Aguirre 
	*     Univerisdad Nacional de Colombia
	*     Grupo de Química Teórica
	*     Nov. de 2005
	*/
	
	FILE* tmp ;
	tmp = fopen( "tmp/umatrixdata.dat", "w" ) ;
	
	/*-------------------------------------------------
	* Se imprimen los hexagonos
	*/
	int i = 0 ;
	for ( i = 0; i < umat->uxdim; i++ ){
		int j = 0 ;
		for ( j = 0; j < umat->uydim; j++ ){
			fprintf( tmp, "Hexagon   %5d   %5d   %10.3f\n", i, j, umat->uvalue[i][j] );
		}
	}
	//-------------------------------------------------
	      
	for (y = 0; y < umat->mydim; y++){
		fprintf(fp, "%s ", start_row_s);
		
		for (x = 0; x < umat->mxdim; x++){
			
			numlabs = dtmp->num_labs;
			
			if (!drawblocks)
				color = 100;
			else
				color = (umat->uvalue[2 * x][2 * y] * 100);
			
			if (numlabs){
				if (numlabs == 1){
					/* one label */
					label = find_conv_to_lab(get_entry_label(dtmp));
					fprintf(fp, "(%s) %d LAB ", ps_string_filter(label), (int)color);
					
					/*-----------------------------------------------------------
					* Se imprime el label
					*/
					fprintf( tmp, "Label     %5d   %5d   %s\n", x, y, label );
					//-----------------------------------------------------------
				}
				else{
					/* multiple labels */
					for (i = 0; i < numlabs; i++){
						label = find_conv_to_lab(get_entry_labels(dtmp, i));
						
						if (label == LABEL_EMPTY) {
							numlabs = i;
							break;
						}
						
						fprintf(fp, "(%s) ", ps_string_filter(label));
					}
					
					fprintf(fp, "%d %d ML ", numlabs, (int)color);
					
					/*-------------------------------------------------------------
					* Se imprime el label:
					*
					* Hay que reformarlo ya que solo se reporta el ultimo
					* valor de los posibles labels en caso que haya muchos
					*/
					fprintf( tmp, "Label     %5d   %5d   %s\n", x, y, label );
					//-----------------------------------------------------------
				}
			}else{
				fprintf(fp, "%d LN ", (int)color);
			}
			
			dtmp = next_entry(&p);
		}
		
		/* do newline twice because labels are printed on every other row */
		fprintf(fp, "%s %s\n", end_row_s, end_row_s);
	}
	
	fclose(tmp) ;
	//---------------------------------------------------------------------------
	
    }
	
  fprintf(fp, "end\n");
  fprintf(fp, "%% end of EPS object\n");

  return 0;
}
Ejemplo n.º 4
0
int print_eps(FILE *fp, struct eps_info *einfo)
{
  float color;
  float xstep, ystep, radius;
  int x, y, numlabs, i;
  char *label;
  eptr p;
  char *select_topol_s, *draw_block_s, *start_row_s, *end_row_s;
  struct data_entry *dtmp;
  struct umatrix *umat = einfo->umat;

  end_row_s = "NL";

  switch (umat->topol) 
    {
    case TOPOL_HEXA:
      select_topol_s = "topol_hexa";
      draw_block_s = "H";
      start_row_s = "XSH";
      break;
    case TOPOL_RECT:
      select_topol_s = "topol_rect";
      draw_block_s = "R";
      start_row_s = "XSR";
      break;
    default:
      fprintf(stderr, "can't print topology %d\n", umat->topol);
      return 0;
      break;
    }

  /*  radius = eps_width / (2.0 * sqrt(3) * (float)umat->mxdim); */
  xstep = einfo->xstep;
  ystep = einfo->ystep;
  radius = einfo->radius;

  /* print eps headers */
  fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n");
  fprintf(fp, "%%%%BoundingBox: 0 0 %d %d\n", 
	  (int)ceil(einfo->width), (int)ceil(einfo->height));
  
  fprintf(fp, "%%%%Title: %s\n%%%%Creator: umat V1.0\n", 
	  ps_string_filter(einfo->title));
  
  fprintf(fp, "%%%%CreationDate: %s", ps_string_filter(get_date()));
  fprintf(fp, "%%%%Pages: 0\n");
  fprintf(fp, "%%%%DocumentFonts: %s\n%%%%DocumentNeededFonts: %s\n",
	  fontname, fontname);
  fprintf(fp, "%%%%EndComments\n");
  
  /* COPY HEADER */
  if (print_header(fp, headerfile))
    return 1; /* error */

  fprintf(fp, "/radius %f def\n/xstep %f def\n/ystep %f def\n",
	  radius, xstep, ystep);

  /* font selection */
  fprintf(fp, "%%%%IncludeFont: %s\n", fontname);
  fprintf(fp, "/fontname /%s def\n", fontname);
  if (fontsize > 0.0) 
    fprintf(fp, "/fontsize %f def\n", fontsize);
  fprintf(fp, "selfont\n");
  
  fprintf(fp, "/doborder %s def\n", doborder ? "true" : "false");
  /* print umat */

  fprintf(fp, "/wt %f def /bt %f def\n", white_treshold, black_treshold);

  fprintf(fp, "/y 0 def\n/xoff %f def\n/yoff %f def\n", 
	  einfo->x0, (einfo->height - einfo->y0));

  if (drawblocks)
    for (y = 0; y < umat->uydim; y++)
      {
	fprintf(fp, "%s ", start_row_s);
	for (x = 0; x < umat->uxdim; x++)
	  fprintf(fp, "%d %s ", (int)(100 * umat->uvalue[x][y]), draw_block_s);
	fprintf(fp, "%s\n", end_row_s);
      }
  
  /* print labels */

  fprintf(fp, "/y 0 def\n/xoff %f def\n/yoff %f def\n", 
	  einfo->x0, (einfo->height - einfo->y0));

  if (drawlabels)
    {
      dtmp = rewind_entries(umat->codes, &p);
      
      for (y = 0; y < umat->mydim; y++)
	{
	  fprintf(fp, "%s ", start_row_s);
	  for (x = 0; x < umat->mxdim; x++)
	    {
	      numlabs = dtmp->num_labs;
	      if (!drawblocks)
		color = 100;
	      else
		color = (umat->uvalue[2 * x][2 * y] * 100);
	      
	      if (numlabs)
		{
		  if (numlabs == 1)
		    {
		      /* one label */
		      label = find_conv_to_lab(get_entry_label(dtmp));
		      fprintf(fp, "(%s) %d LAB ", 
			      ps_string_filter(label), (int)color);
		    }
		  else
		    {
		      /* multiple labels */
		      for (i = 0; i < numlabs; i++)
			{
			  label = find_conv_to_lab(get_entry_labels(dtmp, i));
			  
			  if (label == LABEL_EMPTY) 
			    {
			      numlabs = i;
			      break;
			    }
			  
			  fprintf(fp, "(%s) ", ps_string_filter(label));
			}
		      
		      fprintf(fp, "%d %d ML ", numlabs, (int)color);
		    }
		}
	      else
		fprintf(fp, "%d LN ", (int)color);
	      dtmp = next_entry(&p);
	    }
	  /* do newline twice because labels are printed on every other row */
	  fprintf(fp, "%s %s\n", end_row_s, end_row_s);
	}
    }
  fprintf(fp, "end\n");
  fprintf(fp, "%% end of EPS object\n");

  return 0;
}
Ejemplo n.º 5
0
int save_snapshot(struct teach_params *teach, long iter)
{
  struct entries *codes = teach->codes;
  struct snapshot_info *shot = teach->snapshot;
  struct data_entry *entry;
  eptr p;
  char filename[1024]; /* hope this is enough */
  struct file_info *fi = NULL;
  int retcode = 0;
  int bg = shot->flags & SNAPFLAG_BACKGROUND;
  int ko = shot->flags & SNAPFLAG_KEEPOPEN;

  shot->counter++;
  if (ko)
    if ((fi = shot->fi) == NULL)
      {
	if ((shot->fi = open_file(shot->filename, "w")) == NULL)
	  return 1;
	
	fi = shot->fi;
      }
    else
      fi = shot->fi;

#ifndef NO_BACKGROUND_SNAP
  if (bg)
    {
      if (shot->pid > 0)
	{
	  int statptr;
	  if (!(shot->flags & SNAPFLAG_NOWAIT))
	    {
	      /* fprintf(stderr, "Snap: waiting for pid %d\n", (int)shot->pid); */
	      /* wait for previous child before forking a new one */
	      waitpid(shot->pid, &statptr, 0);
	      shot->pid = -1;
	    }
	}

      if ((shot->pid = fork()) < 0)
	{
	  perror("save_snapshot(background):");
	  return 1;
	}
      if (shot->pid == 0)
	{
	  /* fprintf(stderr, "saving snapshot\n"); */
	}
      else
	{
	  /* fprintf(stderr, "forked pid %d\n", (int)shot->pid); */
	  return 0;
	}
    }
#endif /* NO_BACKGROUND_SNAP */

  if (fi == NULL)
    {
      /* make filename */
      sprintf(filename, shot->filename, iter);
      if ((fi = open_file(filename, "w")) == NULL)
	return 1;
    }

  if (ko)
    fprintf(fi2fp(fi), "#start %d\n", shot->counter);

  if (write_header(fi, codes))
    {
      fprintf(stderr, "save_snapshot: Error writing headers\n");
      close_file(shot->fi);
      shot->fi = NULL;
      return 1;
    }

  /* open file for writing */

  ifverbose(3)
    fprintf(stderr, "saving snapshot: file '%s', type '%s'\n", filename, 
	    get_str_by_id(snapshot_list, shot->type));

  fprintf(fi2fp(fi), "#SNAPSHOT FILE\n#iterations: %ld/%ld\n",
	  iter, teach->length);

  for (entry = rewind_entries(codes, &p); entry != NULL; entry = next_entry(&p))
    {
      if (write_entry(fi, codes, entry))
	{
	  fprintf(stderr, "save_entries: Error writing entry, aborting\n");
	  retcode = 1;
	  break;
	}
    }

  if (ko)
    {
      fprintf(fi2fp(fi), "#end\n");
      fflush(fi2fp(fi));
    }
  else
    close_file(fi);
  
#ifndef NO_BACKGROUND_SNAP
  if (bg)
    exit(retcode);
  else
#endif
    return(retcode);
}
Ejemplo n.º 6
0
int find_winner_knn2(struct entries *codes, struct data_entry *sample, 
		     struct winner_info *win, int knn)
{
  struct data_entry *codetmp;
  int dim, i, j;
  float difference, diff, *s, *c;
  eptr p;

  if (sample->mask != NULL)
    return find_winner_knn(codes, sample, win, knn);

  if (knn == 1) /* might be a little faster */
    return find_winner_euc2(codes, sample, win, 1);

  dim = codes->dimension;
  
  for (i = 0; i < knn; i++)
    {
      win[i].index = -1;
      win[i].winner = NULL;
      win[i].diff = FLT_MAX;
    }
  /* Go through all code vectors */

  codetmp = rewind_entries(codes, &p);
  
  while (codetmp != NULL) {
    difference = 0.0;
    
    /* Compute the distance between codebook and input entry */
    c = codetmp->points; s = sample->points;
    for (i = 0; i < dim; i++)
      {
	diff = *c++ - *s++;
	difference += diff * diff;
      }

    /* If distance is smaller than previous distances */
    for (i = 0; (i < knn) && (difference > win[i].diff); i++);

    if (i < knn) 
      {
	for (j = knn - 1; j > i; j--)
	  {
	    win[j].diff = win[j - 1].diff;
	    win[j].index = win[j - 1].index;
	    win[j].winner = win[j - 1].winner;
	  }

	win[i].diff = difference;
	win[i].index = p.index;
	win[i].winner = codetmp;
      }
    
    codetmp = next_entry(&p);
  }
  
  if (win->index < 0)
    ifverbose(3)
      fprintf(stderr, "find_winner_knn: can't find winner\n");

  return knn; /* number of neighbours */
}
Ejemplo n.º 7
0
int find_winner_knn(struct entries *codes, struct data_entry *sample, 
		    struct winner_info *win, int knn)
{
  struct data_entry *codetmp;
  int dim, i, j, masked;
  float difference, diff;
  eptr p;

  if (knn == 1) /* might be a little faster */
    return find_winner_euc(codes, sample, win, 1);

  dim = codes->dimension;
  
  for (i = 0; i < knn; i++)
    {
      win[i].index = -1;
      win[i].winner = NULL;
      win[i].diff = FLT_MAX;
    }
  /* Go through all code vectors */

  codetmp = rewind_entries(codes, &p);
  
  while (codetmp != NULL) {
    difference = 0.0;
    
    masked = 0;
    /* Compute the distance between codebook and input entry */
    for (i = 0; i < dim; i++)
      {
	/* pitaisiko ottaa huomioon myos codebookissa olevat?? */
	if ((sample->mask != NULL) && (sample->mask[i] != 0))
	  {
	    masked++;
	    continue; /* ignore vector components that have 1 in mask */
	  }
	diff = codetmp->points[i] - sample->points[i];
	difference += diff * diff;
	if (difference > win[knn-1].diff) break;
      }

    if (masked == dim)
      return 0;
    
    /* If distance is smaller than previous distances */
    for (i = 0; (i < knn) && (difference > win[i].diff); i++);

    if (i < knn) 
      {
	for (j = knn - 1; j > i; j--)
	  {
	    win[j].diff = win[j - 1].diff;
	    win[j].index = win[j - 1].index;
	    win[j].winner = win[j - 1].winner;
	  }

	win[i].diff = difference;
	win[i].index = p.index;
	win[i].winner = codetmp;
      }
    
    codetmp = next_entry(&p);
  }
  
  if (win->index < 0)
    ifverbose(3)
      fprintf(stderr, "find_winner_knn: can't find winner\n");

  return knn; /* number of neighbours */
}