int init_sr(sr_data* sr_p, double *sasa, const coord_t *xyz, const double *r, double probe_radius, int n_points) { int n_atoms = freesasa_coord_n(xyz); coord_t *srp = test_points(n_points); if (srp == NULL) return fail_msg("Failed to initialize test points."); //store parameters and reference arrays sr_data sr = {.n_atoms = n_atoms, .n_points = n_points, .probe_radius = probe_radius, .xyz = xyz, .srp = srp, .sasa = sasa}; sr.r = malloc(sizeof(double)*n_atoms); sr.r2 = malloc(sizeof(double)*n_atoms); if (sr.r == NULL || sr.r2 == NULL) { freesasa_coord_free(srp); free(sr.r); free(sr.r2); return mem_fail(); } for (int i = 0; i < n_atoms; ++i) { sr.r[i] = r[i] + probe_radius; sr.r2[i] = sr.r[i]*sr.r[i]; } //calculate distances sr.nb = freesasa_nb_new(xyz,sr.r); if (sr.nb == NULL) { freesasa_coord_free(srp); free(sr.r); free(sr.r2); return mem_fail(); } *sr_p = sr; return FREESASA_SUCCESS; }
int init_sr(sr_data *sr, double *sasa, const coord_t *xyz, const double *r, double probe_radius, int n_points) { int n_atoms = freesasa_coord_n(xyz); coord_t *srp = test_points(n_points); if (srp == NULL) return fail_msg("Failed to initialize test points."); //store parameters and reference arrays sr->n_atoms = n_atoms; sr->n_points = n_points; sr->probe_radius = probe_radius; sr->xyz = xyz; sr->srp = srp; sr->sasa = sasa; sr->nb = NULL; sr->r = malloc(sizeof(double)*n_atoms); sr->r2 = malloc(sizeof(double)*n_atoms); if (sr->r == NULL || sr->r2 == NULL) goto cleanup; for (int i = 0; i < n_atoms; ++i) { double ri = r[i] + probe_radius; sr->r[i] = ri; sr->r2[i] = ri * ri; } //calculate distances sr->nb = freesasa_nb_new(xyz, sr->r); if (sr->nb == NULL) goto cleanup; return FREESASA_SUCCESS; cleanup: release_sr(sr); return mem_fail(); }
//============================================================================ // main //============================================================================ int main() { //******************************************************* cout << "---------[Program start]----------" << endl; /* if (argc < 3) { fprintf(stderr, "Usage: mnist <k> <mnist path> [<nr train images> <nr test images>]\n"); return EXIT_FAILURE; } */ int k = 5; // TODO atoi(argv[1]); #ifdef PRINT cout << "k = " << k << endl; #endif string base = "/tmp/images/"; // TODO string base(argv[2]); LabelIterator *trainLabels = openLabels(base + "train-labels-idx1-ubyte"); ImageIterator *trainImages = openImages(base + "train-images-idx3-ubyte"); LabelIterator *testLabels = openLabels(base + "t10k-labels-idx1-ubyte"); ImageIterator *testImages = openImages(base + "t10k-images-idx3-ubyte"); if (trainLabels == 0 || trainImages == 0 || testLabels == 0 || testImages == 0) { cout << "One or more files couldn't be found! Make sure that following files are in the directory given as argument:\n" << "\ttrain-labels-idx1-ubyte\n" << "\ttrain-images-idx3-ubyte\n" << "\tt10k-labels-idx1-ubyte\n" << "\tt10k-images-idx3-ubyte\n" << endl; exit(-1); } /* trainLabels->count = 1000; trainImages->count = 1000; testLabels->count = 1000; testImages->count = 1000; */ unsigned char label; unsigned char *image; Points<unsigned char, unsigned char> training_points(trainImages->count, trainImages->rows * trainImages->columns); Points<unsigned char, unsigned char> test_points(testImages->count, testImages->rows * testImages->columns); #ifdef PRINT cout << trainImages->count << " " << trainImages->rows*trainImages->columns << endl; cout << "size: " << trainImages->rows*trainImages->columns << "\tcount: " << trainImages->count << endl; #endif int i = 0; Point<unsigned char, unsigned char> *trainPoint; while (hasNextLabel(trainLabels) && hasNextImage(trainImages)) { image = nextImage(trainImages); label = nextLabel(trainLabels); trainPoint = training_points.getPoint(i++); imageToPoint(trainPoint, label, image, (trainImages->columns *trainImages->rows)); #ifdef PRINT if ((i % 1000) == 0) cout << i << " training images loaded." << endl; #endif delete trainPoint; free(image); } i = 0; Point<unsigned char, unsigned char> *testPoint; while (hasNextLabel(testLabels) && hasNextImage(testImages)) { image = nextImage(testImages); label = nextLabel(testLabels); testPoint = test_points.getPoint(i++); imageToPoint(testPoint, label, image, (testImages->columns * testImages->rows)); #ifdef PRINT if ((i % 1000) == 0) cout << i << " test images loaded." << endl; #endif delete testPoint; free(image); } //******************************************************** unsigned char *result = classify(k, test_points, training_points); if (result != 0) { int errors = 0; for (int i = 0; i < test_points.getCount(); i++) { if (result[i] != test_points.getLabel(i)[0]) { errors++; } } float error_rate = (float) errors / (float) test_points.getCount(); printf("PPE:\t %d out of %d inaccurate classifications\n", errors, test_points.getCount()); printf("PPE:\t Error rate is %f\n", error_rate); free(result); } printf("PPE:\t Program finished\n"); return (0); }