/* @(#)process.c 1.1 12/9/92 */ /* * File : process.c * * Input : Performance log file from learn.c (using the "user collect" * command). * Output : File containing the average performance statistic. * File containing the overall activity statistic. * * Usage : process [] * * Input: .out (output from NSL) * Output: .act - overall activity measure by trial * .perf - performance measure by trial * * = the number of columns * = low pass filter decay * = number of trials to average at the end of the * experiment to obtain the "end activity" to which all * other trials are compared to obtain the average activity * measure. * * * Andrew H. Fagg * Center for Neural Engineering * Computer Science Department * University of Southern California * Henry Salvatori #300 * Los Angeles, CA 90089-0781 * * ahfagg@pollux.usc.edu * * * HISTORY * Date Author Description * -------- ----------------- -------------------------------- * 12/05/92 Andrew H. Fagg Doc update. * 01/15/92 Andrew H. Fagg Original. * */ #include #include #include "process.h" struct trial_type trials[MAX_TRIALS]; /* * float dot(trials, i1, i2, num_units) * * Compute the dot product between trials and . * */ float dot(trials, i1, i2, num_units) struct trial_type *trials; int i1; int i2; int num_units; { int j; float total; total = 0.0; for(j = 0; j < num_units; ++j) total += trials[i1].activation[j] * trials[i2].activation[j]; return(total); } /* * output_stats(fp1, fp2, trials, num_units, num_trials, filter) * * Output the average performance and average activity statistics * to the appropriate file. * */ output_stats(fp1, fp2, trials, num_units, num_trials, filter) FILE *fp1, *fp2; struct trial_type *trials; int num_units; int num_trials; float filter; { int i; int j; float performance; /* Activity: dot product between current */ /* trial and last trial. */ for(i = 0; i < num_trials; ++i) fprintf(fp1, "%f\n", dot(trials, i, num_trials, num_units)); performance = 0.0; /* Performance : low-pass filter of */ /* correctness signal. */ for(i = 0; i < num_trials; ++i) { performance = filter * performance + (1 - filter) * trials[i].correct; fprintf(fp2, "%f\n", performance); } } /* * compute_average(trials, num_units, num_average, num_trials) * * Average the last trials in the experiment to * be used in computing the overall activity measure. This * average vector is deposited in the th element of * the trial list and is dotted with every other element in the * trial list (in output_stats()). * */ compute_average(trials, num_units, num_average, num_trials) struct trial_type *trials; int num_units; int num_average; { int i; int trial; for(i = 0; i < num_units; ++i) /* Zero the average vector */ trials[num_trials].activation[i] = 0.0; /* Sum the last num_average vectors. */ for(trial = num_trials - num_average; trial < num_trials; ++trial) { /* Sum each element. */ for(i = 0; i < num_units; ++i) { trials[num_trials].activation[i] += trials[trial].activation[i]; } } /* Normalize */ for(i = 0; i < num_units; ++i) trials[num_trials].activation[i] /= (float) num_average; } /* * process_file(fp, fp1, fp2, num_units, filter, num_average) * * Load in the log file from the simulation and compute the * statistics. * */ process_file(fp, fp1, fp2, num_units, filter, num_average) FILE *fp, *fp1, *fp2; int num_units; float filter; int num_average; { int i; int j; char temp; /* Loop for each trial. */ for(i = 0; i < MAX_TRIALS; ++i) { /* Check for end of file. */ if(fscanf(fp, "%*s %*s %*s %c %*f", &temp) != 1) break; /* Correct or incorrect? */ trials[i].correct = temp == '+'; /* Load unit activities. */ for(j = 0; j < num_units; ++j) { if(fscanf(fp, "%f", &trials[i].activation[j]) != 1) { printf("Error reading activation %d.\n", j); exit(1); } } } /* Compute end of experiment activity. */ compute_average(trials, num_units, num_average, i); /* Compute and output statistics. */ output_stats(fp1, fp2, trials, num_units, i, filter); } /* * main(argc, argv) * * Parse args and open files. * */ main(argc, argv) int argc; char *argv[]; { int num_units; float filter; FILE *fp, *fp1, *fp2; char buf[100]; int num_average; /* Check for correct number of args. */ if(argc != 4 && argc != 5) { printf("Usage : process []\n"); exit(1); } /* Open input file. */ sprintf(buf, "%s.out", argv[1]); if((fp = fopen(buf, "r")) == NULL) { printf("Error opening %s for reading.\n", buf); exit(1); } /* Open activity file. */ sprintf(buf, "%s.act", argv[1]); if((fp1 = fopen(buf, "w")) == NULL) { printf("Error opening %s for writing.\n", buf); exit(1); } /* Open performance file. */ sprintf(buf, "%s.perf", argv[1]); if((fp2 = fopen(buf, "w")) == NULL) { printf("Error opening %s for writing.\n", buf); exit(1); } /* Parse number of units. */ if(sscanf(argv[2], "%d", &num_units) != 1) { printf("Error reading num_set_units.\n"); exit(1); } /* Parse filter parameter. */ if(sscanf(argv[3], "%f", &filter) != 1) { printf("Error reading filter_level.\n"); exit(1); } /* Parse number to average. */ if(argc == 5) { if(sscanf(argv[4], "%d", &num_average) != 1) { printf("Error reading num_average.\n"); exit(1); } } else /* Default */ num_average = 1; /* Do the work. */ process_file(fp, fp1, fp2, num_units, filter, num_average); /* Clean up */ fclose(fp); fclose(fp1); fclose(fp2); exit(0); }