Example #1
0
int main_vcfsom(int argc, char *argv[])
{
	int c;
	args_t *args     = (args_t*) calloc(1,sizeof(args_t));
	args->argc       = argc; args->argv = argv;
    args->nbin       = 20;
    args->learn      = 1.0;
    args->bmu_th     = 0.9;
    args->nfold      = 5;
    args->rand_seed  = 1;
    args->ndim       = 2;
    args->bad_class  = 1;
    args->good_class = 2;
    args->merge      = MERGE_AVG;
    args->train_bad  = 1;

	static struct option loptions[] = 
	{
		{"help",0,0,'h'},
		{"prefix",1,0,'p'},
		{"ntrain-sites",1,0,'n'},
		{"random-seed",1,0,'r'},
		{"bmu-threshold",1,0,'b'},
		{"exclude-bad",0,0,'e'},
		{"learning-rate",1,0,'l'},
		{"size",1,0,'s'},
		{"som-dimension",1,0,'d'},
		{"nfold",1,0,'f'},
		{"merge",1,0,'m'},
		{"train",0,0,'t'},
		{"classify",0,0,'c'},
		{0,0,0,0}
	};
	while ((c = getopt_long(argc, argv, "htcp:n:r:b:l:s:f:d:m:e",loptions,NULL)) >= 0) {
		switch (c) {
            case 'e': args->train_bad = 0; break;
            case 'm': 
                if ( !strcmp(optarg,"min") ) args->merge = MERGE_MIN;
                else if ( !strcmp(optarg,"max") ) args->merge = MERGE_MAX;
                else if ( !strcmp(optarg,"avg") ) args->merge = MERGE_AVG;
                else error("The -m method not recognised: %s\n", optarg);
                break;
            case 'p': args->prefix = optarg; break;
            case 'n': args->ntrain = atoi(optarg); break;
            case 'r': args->rand_seed = atoi(optarg); break;
            case 'b': args->bmu_th = atof(optarg); break;
            case 'l': args->learn = atof(optarg); break;
            case 's': args->nbin = atoi(optarg); break;
            case 'f': args->nfold = atoi(optarg); break;
            case 'd': 
                args->ndim = atoi(optarg); 
                if ( args->ndim<2 ) error("Expected -d >=2, got %d\n", args->ndim);
                if ( args->ndim>3 ) fprintf(stderr,"Warning: This will take a long time and is not going to make the results better: -d %d\n", args->ndim);
                break;
            case 't': args->action = SOM_TRAIN; break;
            case 'c': args->action = SOM_CLASSIFY; break;
			case 'h': 
			case '?': usage();
			default: error("Unknown argument: %s\n", optarg);
		}
	}

    if ( !args->rand_seed ) args->rand_seed = time(NULL);
    if ( argc!=optind+1 ) usage();
    args->fname = argv[optind];
    init_data(args);

    if ( args->action == SOM_TRAIN ) do_train(args);
    else if ( args->action == SOM_CLASSIFY ) do_classify(args);

    destroy_data(args);
	free(args);
	return 0;
}
Example #2
0
/* Gateway function */
void mexFunction(int nlhs, mxArray_t *plhs[],
		 int nrhs, const mxArray_t *prhs[])
{
    double *y;
    mxArray_t *f_dimensions, *f_numcategories, *obj, *classifier;
    int dimensions, data_len, categories;

    /* Check that we have 2 RHS arguments */
    if (nrhs != 2) {
	mexErrMsgTxt("classify: There must be exactly two arguments");
	return;
    }
    
    /* Check that we have zero or one LHS arguments */
    if ((nlhs < 0) || (nlhs > 1)) {
	mexErrMsgTxt("classify: Too many output arguments");
	return;
    }

    if (nlhs == 0) return;

    (const mxArray_t *)obj = prhs[0];

    /* Turn it into a BOOST object */
    call_matlab_1_1(&obj, "as_boost", prhs[0]);

    /* NOTE: This is a hack, but seems necessary as MATLAB won't seem to
       recognise that obj has an inherited DIMENSIONS method from the
       CLASSIFIER ancestor. */
    call_matlab_1_1(&classifier, "as_classifier", obj);
    
    /* Get our number of dimensions */
    call_matlab_1_1(&f_dimensions, "dimensions", classifier);
    dimensions = (int)mxGetScalar(f_dimensions);
    mxDestroyArray(f_dimensions);

    /* Ask MATLAB to tell us how many categories are in our dataset */
    call_matlab_1_1(&f_numcategories, "numcategories", classifier);
    categories = (int)mxGetScalar(f_numcategories);
    mxDestroyArray(f_numcategories);
    mxDestroyArray(classifier);

    /* Get our X variable.  Make sure its a matrix, and get its number of
       rows and columns. */
    if (!mxIsClass(prhs[1], "double")) {
	mexErrMsgTxt("classify: x must be a double array");
	return;
    }

    data_len = mxGetM(prhs[1]);

    /* Check that the number of dimensions matches */
    if (dimensions != mxGetN(prhs[1])) {
	mexErrMsgTxt("classify: dimensions of x and obj don't match");
	return;
    }

    /* Create storage for our output */
    plhs[0] = mxCreateDoubleMatrix(data_len, 1, mxREAL);
    if (plhs[0] == NULL) {
	mexErrMsgTxt("classify: out of memory allocating Y!");
	return;
    }

    y = mxGetPr(plhs[0]);

    /* Call our computational routine */
    do_classify(obj, prhs[1], y, data_len, categories);
}