int global_options(int argc, char **argv) { char *s; /* set program name */ setprogname(argv[0]); #ifndef NO_PIPED_COMMANDS /* command for compressing */ s = getenv("LVQSOM_COMPRESS_COMMAND"); if (s) compress_command = s; s = extract_parameter(argc, argv, "-compress_cmd", OPTION); if (s) compress_command = s; /* command for uncompressing */ s = getenv("LVQSOM_UNCOMPRESS_COMMAND"); if (s) uncompress_command = s; s = extract_parameter(argc, argv, "-uncompress_cmd", OPTION); if (s) uncompress_command = s; #endif /* NO_PIPED_COMMANDS */ /* string that identifies a vector component to be ignored in files */ s = getenv("LVQSOM_MASK_STR"); if (s) masked_string = s; s = extract_parameter(argc, argv, "-mask_str", OPTION); if (s) masked_string = s; if (extract_parameter(argc, argv, "-version", OPTION2)) fprintf(stderr, "Version: %s\n", get_version()); verbose(oatoi(extract_parameter(argc, argv, VERBOSE, OPTION), 1)); return 0; }
int main(int argc, char **argv) { long nod, buffer; float qerror; char *in_data_file; char *in_code_file; struct teach_params teach; int qmode; struct entries *data, *codes; global_options(argc, argv); if (extract_parameter(argc, argv, "-help", OPTION2)) { printhelp(); exit(0); } in_data_file = extract_parameter(argc, argv, IN_DATA_FILE, ALWAYS); in_code_file = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS); buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0); teach.radius = oatof(extract_parameter(argc, argv, TRAINING_RADIUS, OPTION), 1.0); qmode = oatoi(extract_parameter(argc, argv, "-qetype", OPTION), 0); label_not_needed(1); ifverbose(2) fprintf(stdout, "Input entries are read from file %s\n", in_data_file); data = open_entries(in_data_file); if (data == NULL) { fprintf(stderr, "Can't open datafile: %s\n", in_data_file); exit(1); } ifverbose(2) fprintf(stdout, "Codebook entries are read from file %s\n", in_code_file); codes = open_entries(in_code_file); if (codes == NULL) { fprintf(stderr, "Can't open code file '%s'\n", in_code_file); close_entries(data); exit(1); } if (codes->topol < TOPOL_HEXA) { fprintf(stderr, "File %s is not a map file\n", in_code_file); close_entries(data); close_entries(codes); exit(1); } if (data->dimension != codes->dimension) { fprintf(stderr, "Data and codebook vectors have different dimensions (%d != %d)", data->dimension, codes->dimension); close_entries(data); close_entries(codes); exit(1); } set_teach_params(&teach, codes, data, buffer); set_som_params(&teach); if (qmode > 0) qerror = find_qerror2(&teach); else qerror = find_qerror(&teach); nod = teach.data->num_entries; ifverbose(1) fprintf(stdout, "Quantization error of %s with map %s is %f per sample (%d samples)\n", in_data_file, in_code_file, qerror / (float) nod, nod); else
int main(int argc, char **argv) { char *in_name, *out_name, *s; struct umatrix *umat; struct eps_info einfo; int average = 0; int median = 0; FILE *out_fp; struct file_info *out_fi; global_options(argc, argv); if (extract_parameter(argc, argv, "-help", OPTION2)) { printhelp(); exit(0); } in_name = NULL; out_name = NULL; /* default page size is A4 */ set_paper(get_paper_by_id(PAPER_A4)); /* parse args */ if (extract_parameter(argc, argv, "-border", OPTION2)) doborder = 1; if (extract_parameter(argc, argv, "-portrait", OPTION2)) orientation = PORTRAIT; if (extract_parameter(argc, argv, "-landscape", OPTION2)) orientation = LANDSCAPE; if (extract_parameter(argc, argv, "-ps", OPTION2)) mode = OUTPUT_PS; if (extract_parameter(argc, argv, "-eps", OPTION2)) mode = OUTPUT_EPS; white_treshold = oatof(extract_parameter(argc, argv, "-W", OPTION), 1.0); black_treshold = oatof(extract_parameter(argc, argv, "-B", OPTION), 0.0); out_name = extract_parameter(argc, argv, "-o", OPTION); if (mode == 0) mode = guess_mode(out_name); if ((s = extract_parameter(argc, argv, "-font", OPTION))) fontname = s; fontsize = oatof(extract_parameter(argc, argv, "-fontsize", OPTION), -1.0); title = extract_parameter(argc, argv, "-title", OPTION); if (extract_parameter(argc, argv, "-notitle", OPTION2)) notitle = 1; if ((s = extract_parameter(argc, argv, "-paper", OPTION))) { struct paper_info *p = get_paper_by_name(s); if (p == NULL) { fprintf(stderr, "Unknown paper type: %s\n", s); exit(1); } set_paper(p); } if (extract_parameter(argc, argv, "-average", OPTION2)) average = 1; if (extract_parameter(argc, argv, "-median", OPTION2)) median = 1; if (extract_parameter(argc, argv, "-onlylabs", OPTION2)) drawblocks = 0; if (extract_parameter(argc, argv, "-nolabs", OPTION2)) drawlabels = 0; in_name = extract_parameter(argc, argv, IN_CODE_FILE, ALWAYS); if ((s = getenv("UMAT_HEADERFILE"))) headerfile = s; if ((s = extract_parameter(argc, argv, "-headerfile", OPTION))) headerfile = s; label_not_needed(1); if ((umat = read_map(in_name, 0, 0)) == NULL) { fprintf(stderr, "Can't load file\n"); exit(1); } calc_umatrix(umat, 0, 0); if (average) average_umatrix(umat); if (median) median_umatrix(umat); if (mode == 0) mode = OUTPUT_EPS; if (orientation == BEST) orientation = (umat->mxdim >= umat->mydim) ? LANDSCAPE : PORTRAIT; if ((out_fi = open_file(out_name, "w")) == NULL) { fprintf(stderr, "can't open output file\n"); free_umat(umat); exit(1); } out_fp = fi2fp(out_fi); einfo.umat = umat; einfo.title = title ? title : in_name; image_size(&einfo, 0); if (mode == OUTPUT_EPS) print_eps(out_fp, &einfo); else print_page(out_fp, &einfo); free_umat(umat); umat = NULL; if (out_fi) close_file(out_fi); return 0; }
int main(int argc, char **argv) { int not, bnot, error; int xdim, ydim; int topol, neigh; float alpha1, radius1; int fixed, weights; float alpha2, radius2; float qerror, qerrorb; char *in_data_file, *in_test_file, *out_code_file, *alpha_s; struct entries *data = NULL; struct entries *testdata = NULL; struct entries *codes = NULL; struct entries *codess = NULL; struct entries *tmp; struct teach_params params; long buffer, length1, length2, noc, nod; int qmode; struct typelist *type_tmp; error = 0; global_options(argc, argv); print_description(); not = get_int("Give the number of trials", 0); in_data_file = get_str("Give the input data file name"); in_test_file = get_str("Give the input test file name"); out_code_file = get_str("Give the output map file name"); topol = topol_type(get_str("Give the topology type")); if (topol == TOPOL_UNKNOWN) { ifverbose(2) fprintf(stderr, "Unknown topology type, using hexagonal\n"); topol = TOPOL_HEXA; } neigh = neigh_type(get_str("Give the neighborhood type")); if (neigh == NEIGH_UNKNOWN) { ifverbose(2) fprintf(stderr, "Unknown neighborhood type, using bubble\n"); neigh = NEIGH_BUBBLE; } xdim = get_int("Give the x-dimension", 0); ydim = get_int("Give the y-dimension", 0); length1 = get_int("Give the training length of first part", 0); alpha1 = get_float("Give the training rate of first part", 0.0); radius1 = get_float("Give the radius in first part", 0.0); length2 = get_int("Give the training length of second part", 0); alpha2 = get_float("Give the training rate of second part", 0.0); radius2 = get_float("Give the radius in second part", 0.0); printf("\n"); fixed = (int) oatoi(extract_parameter(argc, argv, FIXPOINTS, OPTION), 0); weights = (int) oatoi(extract_parameter(argc, argv, WEIGHTS, OPTION), 0); buffer = oatoi(extract_parameter(argc, argv, "-buffer", OPTION), 0); alpha_s = extract_parameter(argc, argv, "-alpha_type", OPTION); qmode = oatoi(extract_parameter(argc, argv, "-qetype", OPTION), 0); use_fixed(fixed); use_weights(weights); label_not_needed(1); ifverbose(2) fprintf(stderr, "Input entries are read from file %s\n", in_data_file); data = open_entries(in_data_file); if (data == NULL) { fprintf(stderr, "Can't open data file '%s'\n", in_data_file); error = 1; goto end; } set_buffer(data, buffer); ifverbose(2) fprintf(stderr, "Test entries are read from file %s\n", in_test_file); testdata = open_entries(in_test_file); if (testdata == NULL) { fprintf(stderr, "Can't open test data file '%s'\n", in_test_file); error = 1; goto end; } set_buffer(testdata, buffer); noc = xdim * ydim; if (noc <= 0) { fprintf(stderr, "Dimensions of map (%d %d) are incorrect\n", xdim, ydim); error = 1; goto end; } if (xdim < 0) { fprintf(stderr, "Dimensions of map (%d %d) are incorrect\n", xdim, ydim); error = 1; goto end; } if (alpha_s) { type_tmp = get_type_by_str(alpha_list, alpha_s); if (type_tmp->data == NULL) { fprintf(stderr, "Unknown alpha type %s\n", alpha_s); error = 1; goto end; } } else type_tmp = get_type_by_id(alpha_list, ALPHA_LINEAR); params.alpha_type = type_tmp->id; params.alpha_func = type_tmp->data; codess = NULL; qerrorb = FLT_MAX; bnot = 0; while (not) { init_random(not); ifverbose(2) fprintf(stderr, "Initializing codebook\n"); codes = randinit_codes(data, topol, neigh, xdim, ydim); if (codes == NULL) { fprintf(stderr, "Error initializing random codebook, aborting\n"); error = 1; goto end; } set_teach_params(¶ms, codes, NULL, 0); set_som_params(¶ms); params.data = data; params.length = length1; params.alpha = alpha1; params.radius = radius1; ifverbose(2) fprintf(stderr, "Training map, first part, rlen: %ld alpha: %f\n", params.length, params.alpha); codes = som_training(¶ms); params.length = length2; params.alpha = alpha2; params.radius = radius2; ifverbose(2) fprintf(stderr, "Training map, second part, rlen: %ld alpha: %f\n", params.length, params.alpha); codes = som_training(¶ms); params.data = testdata; ifverbose(2) fprintf(stderr, "Calculating quantization error\n"); if (qmode > 0) qerror = find_qerror2(¶ms); else qerror = find_qerror(¶ms); nod = testdata->num_entries; if (qerror < qerrorb) { qerrorb = qerror; bnot = not; tmp = codess; codess = codes; codes = tmp; } close_entries(codes); codes = NULL; ifverbose(1) fprintf(stderr, "%3d: %f\n", not, qerror/(float) nod); not--; } if (codess != NULL) { ifverbose(2) fprintf(stdout, "Codebook entries are saved to file %s\n", out_code_file); save_entries(codess, out_code_file); ifverbose(1) fprintf(stdout, "Smallest error with random seed %3d: %f\n", bnot, qerrorb/(float) nod); } end: if (codess) close_entries(codess); if (data) close_entries(data); if (testdata) close_entries(testdata); return(error); }
/* * Converts a PEM encoded file into its binary form * * RFC 1421 Privacy Enhancement for Electronic Mail, February 1993 * RFC 934 Message Encapsulation, January 1985 * * We no longer support decrypting PEM files - those can only come in via NSS */ err_t pemtobin(chunk_t *blob) { typedef enum { PEM_PRE = 0, PEM_MSG = 1, PEM_HEADER = 2, PEM_BODY = 3, PEM_POST = 4, PEM_ABORT = 5 } state_t; state_t state = PEM_PRE; chunk_t src = *blob; chunk_t dst = *blob; chunk_t line = empty_chunk; /* zero size of converted blob */ dst.len = 0; while (fetchline(&src, &line)) { if (state == PEM_PRE) { if (find_boundary("BEGIN", &line)) { state = PEM_MSG; } continue; } else { if (find_boundary("END", &line)) { state = PEM_POST; break; } if (state == PEM_MSG) { state = (memchr(line.ptr, ':', line.len) == NULL) ? PEM_BODY : PEM_HEADER; } if (state == PEM_HEADER) { chunk_t name = empty_chunk; chunk_t value = empty_chunk; /* an empty line separates HEADER and BODY */ if (line.len == 0) { state = PEM_BODY; continue; } /* we are looking for a name: value pair */ if (!extract_parameter(&name, &value, &line)) continue; if (match("Proc-Type", &name) && *value.ptr == '4') return "Proc-Type: encrypted files no longer supported outside of the NSS database, please import these into NSS"; else if (match("DEK-Info", &name)) return "DEK-Info: encrypted files no longer supported outside of the NSS database, please import these into NSS"; } else { /* state is PEM_BODY */ const char *ugh = NULL; size_t len = 0; chunk_t data; /* remove any trailing whitespace */ if (!extract_token(&data, ' ', &line)) data = line; ugh = ttodata((char *)data.ptr, data.len, 64, (char *)dst.ptr, blob->len - dst.len, &len); if (ugh) { DBG(DBG_PARSING, DBG_log(" %s", ugh)); state = PEM_ABORT; break; } else { dst.ptr += len; dst.len += len; } } } } /* set length to size of binary blob */ blob->len = dst.len; if (state != PEM_POST) return "file coded in unknown format, discarded"; return NULL; }