/* This function reads in a discription of a graph and loads it into * a graph discription structure. The file format is asci were the * first token is the number of vericies followed by verticy labels * and guess x, y coordinates. This is followed by the number of edges * and the indexes of the vericies that they connect. */ void saveGraphDiscription(const char* filename, GraphDiscription gd){ FILE* file; int i; file = fopen(filename,"w"); DEBUG_CHECK_1ARG(file,"Error opening file: %s", filename); /* Read the number of vericies in the file */ fprintf(file,"%d\n",(gd->numVert)); /* allocate space for vericies and load discriptions */ for(i = 0; i < gd->numVert; i++){ fprintf(file, "%s %f %f\n", gd->vertLabels[i], (gd->verts[i].x), (gd->verts[i].y)); } /* Read the number of edges in the file */ fprintf(file,"%d\n",(gd->numEdge)); /* allocate space for vericies and load discriptions */ for(i = 0; i < gd->numEdge; i++){ fprintf(file, "%d %d\n",(gd->edges[i].vert1), (gd->edges[i].vert2)); } fclose(file); }
/* This function reads in a discription of a graph and loads it into * a graph discription structure. The file format is asci were the * first token is the number of vericies followed by verticy labels * and guess x, y coordinates. This is followed by the number of edges * and the indexes of the vericies that they connect. */ GraphDiscription readGraphDiscription(const char* filename){ FILE* file; int i; GraphDiscription gd = ALLOCATE(graph_discription,1); DEBUG_CHECK(gd,"Error allocating memory"); file = fopen(filename,"r"); DEBUG_CHECK_1ARG(file,"Error opening file: %s", filename); /* Read the number of vericies in the file */ DEBUG_CHECK_1ARG(fscanf(file,"%d",&(gd->numVert)) == 1, "Error: could not determin the number of verticies in file %s", filename); /* allocate space for vericies and load discriptions */ gd->verts = ALLOCATE(Vert,gd->numVert); gd->vertLabels = ALLOCATE(char*,gd->numVert); gd->bunch = ALLOCATE(JetBunch,gd->numVert); for(i = 0; i < gd->numVert; i++){ gd->vertLabels[i] = ALLOCATE(char,LABELMAX); DEBUG_CHECK_2ARG(fscanf(file, "%s %lf %lf", gd->vertLabels[i], &(gd->verts[i].x), &(gd->verts[i].y)) == 3, "Error Parsing vertex %d in file: %s", i, filename); gd->bunch[i] = makeJetBunch(); } /* Read the number of edges in the file */ DEBUG_CHECK_1ARG(fscanf(file,"%d",&(gd->numEdge)) == 1, "Error: could not determin the number of edges in file %s", filename); /* allocate space for vericies and load discriptions */ gd->edges = ALLOCATE(Edge,gd->numEdge); for(i = 0; i < gd->numEdge; i++){ DEBUG_CHECK_2ARG(fscanf(file, "%d %d",&(gd->edges[i].vert1), &(gd->edges[i].vert2)) == 2, "Error Parsing vertex %d in file: %s", i, filename); } fclose(file); return gd; }
void sortSubjectsBySimilarityToProbe (char *probe, ListOfStrings subjects, char *distanceMatrix, int *indices) { int i, j, nDistances; DistanceMeasure *distances = NULL; DistanceMeasure *toSort = NULL; int nSubjects = countStrings (subjects); /* If we are using a distance matrix, then we load up the * list of distances from the probe image. This allows us * to lookup the distances from the probe quickly */ if (distanceMatrix) { Tokenizer tok; FILE *f = fopen (makePath (distanceMatrix, probe), "r"); void *distanceList = NULL; DEBUG_CHECK_1ARG (f, "Unable to open file %s in scores directory", makePath (distanceMatrix, probe)); tokenizerInit (&tok, tokenizerStreamReader, f); while (!tokenizerEndOfFile (&tok)) { DistanceMeasure m; m.subject = strdup (tokenizerGetWord (&tok)); m.distance = atof (tokenizerGetWord (&tok)); listAccumulate (&distanceList, &m, sizeof (DistanceMeasure)); } fclose (f); distances = (DistanceMeasure*) listToArray (&distanceList, sizeof (DistanceMeasure), (size_t*)&nDistances); } /* Copy the list of names into the intermediate data structure that allows us to sort the * subjects by distance to the probe. */ toSort = (DistanceMeasure*) malloc (nSubjects * sizeof (DistanceMeasure)); for (j = 0; j < nSubjects; j++) { toSort[j].subject = subjects[j]; toSort[j].distance = 0.0; toSort[j].index = j; } /* Read distances between probe and every other image in the subject list. * As a special case, a subject is said to be infinitely far away from him/herself. * Random scores are used when a distanceMatrix is not provided */ for (j = 0; j < nSubjects; j++) { if (strcmp (subjects[j], probe) == 0) { /* Probe and subject are the same. Say they are far apart since * we don't want to treat an image and itself as being two replicates * of a person */ toSort[j].distance = HUGE; } else if (distanceMatrix != NULL) { /* Look for the subject in the list of distances and return assign the * score */ toSort[j].distance = HUGE; for (i = 0; i < nDistances; ++i) if (strcmp (distances[i].subject, toSort[j].subject) == 0) toSort[j].distance = distances[i].distance; } else { /* If we are not using a distance matrix, then choose * a random value */ toSort[j].distance = ((double) rand()) / RAND_MAX; } } /* Now sort the list by similarity to the probe */ qsort (toSort, nSubjects, sizeof (DistanceMeasure), distanceMeasureComparator); /* Copy the data back into the subject list or return the permuted indices */ if (indices == NULL) for (j = 0; j < nSubjects; j++) { subjects[j] = toSort[j].subject; } else for (j = 0; j < nSubjects; j++) { indices[j] = toSort[j].index; } /* Clean up */ if (distanceMatrix) { for (i = 0; i < nDistances; ++i) free (distances[i].subject); free (distances); } free (toSort); }