Exemplo n.º 1
0
static void
xcreate (mxArray *mnode, int i, VlHIKMNode *node)
{
  int node_K                = vl_ikm_get_K (node->filter) ;
  int M                     = vl_ikm_get_ndims (node->filter) ;
  vl_ikm_acc const *centers = vl_ikm_get_centers (node->filter) ;

  mxArray *mcenters ;

  mcenters = mxCreateNumericMatrix (M, node_K, mxINT32_CLASS, mxREAL);
  memcpy (mxGetPr(mcenters), centers, sizeof(vl_ikm_acc) * M * node_K) ;
  mxSetField (mnode, i, "centers", mcenters) ;

  if (node->children) {
    mxArray * msub ;
    const char * field_names[] = {"centers", "sub" } ;
    mwSize dims [2] ;
    int k ;

    dims[0] = 1 ;
    dims[1] = node_K ;

    msub = mxCreateStructArray (2, dims, 2, field_names) ;

    for (k = 0 ; k < node_K ; ++k) {
      xcreate (msub, k, node -> children [k]) ;
    }

    mxSetField (mnode, i, "sub", msub) ;
  }
}
Exemplo n.º 2
0
static VlHIKMNode *
xcreate (VlHIKMTree *tree, mxArray const *mnode, int i)
{
  mxArray const *mcenters, *msub ;
  VlHIKMNode *node ;
  vl_size M, node_K ;
  vl_uindex k ;

  /* sanity checks */
  mcenters = mxGetField(mnode, i, "centers") ;
  msub     = mxGetField(mnode, i, "sub") ;

  if (!mcenters                                 ||
      mxGetClassID (mcenters) != mxINT32_CLASS  ||
      !vlmxIsMatrix (mcenters, -1, -1)             ) {
    mexErrMsgTxt("NODE.CENTERS must be a INT32 matrix.") ;
  }

  M      = mxGetM (mcenters) ;
  node_K = mxGetN (mcenters) ;

  if (node_K > (vl_size)tree->K) {
    mexErrMsgTxt("A node has more clusters than TREE.K.") ;
  }

  if (tree->M < 0) {
    tree->M = M ;
  } else if (M != (vl_size)tree->M) {
    mexErrMsgTxt("A node CENTERS field has inconsistent dimensionality.") ;
  }

  node           = mxMalloc (sizeof(VlHIKMNode)) ;
  node->filter   = vl_ikm_new (tree->method) ;
  node->children = 0 ;

  vl_ikm_init (node->filter, mxGetData(mcenters), M, node_K) ;

  /* has any childer? */
  if (msub) {

    /* sanity checks */
    if (mxGetClassID (msub) != mxSTRUCT_CLASS) {
      mexErrMsgTxt("NODE.SUB must be a MATLAB structure array.") ;
    }
    if (mxGetNumberOfElements (msub) != node_K) {
      mexErrMsgTxt("NODE.SUB size must correspond to NODE.CENTERS.") ;
    }

    node-> children = mxMalloc (sizeof(VlHIKMNode *) * node_K) ;
    for(k = 0 ; k < node_K ; ++ k) {
      node-> children [k] = xcreate (tree, msub, k) ;
    }
  }
  return node ;
}
Exemplo n.º 3
0
static VlHIKMTree*
python_to_hikm(VlHIKMTree_python & inTree, int method_type)
{
	VlHIKMTree *tree = new VlHIKMTree;
	tree-> depth = inTree.depth;
	tree-> K = inTree.K;
	tree-> M = -1; /* to be initialized later */
	tree-> method = method_type;
	tree-> root = xcreate(tree, inTree);
	return tree;
}
Exemplo n.º 4
0
mxArray *
hikm_to_matlab (VlHIKMTree * tree)
{
  int K      = vl_hikm_get_K (tree) ;
  int depth  = vl_hikm_get_depth (tree) ;
  mwSize  dims [2] = {1, 1} ;
  mxArray *mtree ;
  const char *field_names[] = {"K", "depth", "centers", "sub"} ;

  /* Create the main struct array */
  mtree = mxCreateStructArray
    (2, dims, NFIELDS(field_names), field_names) ;
  mxSetField (mtree, 0, "K",      mxCreateDoubleScalar (K)) ;
  mxSetField (mtree, 0, "depth",  mxCreateDoubleScalar (depth)) ;
  if (tree->root) xcreate (mtree, 0, tree->root) ;
  return mtree;
}
Exemplo n.º 5
0
static VlHIKMNode *
xcreate(VlHIKMTree *tree, VlHIKMTree_python & inTree)
{
	VlHIKMNode *node;
	int M, node_K, k;

	PyArrayObject * centers = (PyArrayObject *) inTree.centers;
	M = centers->dimensions[0];
	node_K = centers->dimensions[1];

	if (node_K > tree->K) {
		printf("%d / %d\n", node_K, tree->K);
		printf("A node has more clusters than TREE.K.\n");
	}

	if (tree->M < 0) {
		tree->M = M;
	} else if (M != tree->M) {
		printf("%d / %d\n", M, tree->M);
		printf("A node CENTERS field has inconsistent dimensionality.\n");
	}

	node = new VlHIKMNode;
	node->filter = vl_ikm_new(tree->method);
	node->children = 0;

	vl_ikm_init(node->filter, (vl_ikm_acc*) centers->data, M, node_K);

	/* has any childer? */
	if (inTree.sub.size() > 0) {

		/* sanity checks */
		if (inTree.sub.size() != node_K) {
			printf("%d, %d\n", (int)inTree.sub.size(), node_K);
			printf("NODE.SUB size must correspond to NODE.CENTERS.\n");
		}

		node-> children = new VlHIKMNode*[node_K];
		for (k = 0; k < node_K; ++k) {
			PyArrayObject * centers = (PyArrayObject *) inTree.sub[k].centers;
			node-> children[k] = xcreate(tree, inTree.sub[k]);
		}
	}
	return node;
}
Exemplo n.º 6
0
void nohup_main(void)
{
  xsignal(SIGHUP, SIG_IGN);
  if (isatty(1)) {
    close(1);
    if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
        S_IRUSR|S_IWUSR ))
    {
      char *temp = getenv("HOME");

      temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
      xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
      free(temp);
    }
  }
  if (isatty(0)) {
    close(0);
    xopen_stdio("/dev/null", O_RDONLY);
  }
  xexec(toys.optargs);
}
Exemplo n.º 7
0
/* Create a locked file. */
int
createlock(
    const char *path, int flags, uid_t uid, gid_t gid, mode_t mode, u_int locks)
{
	int	fd, saved_errno;

	if (mklock(locks, path) != 0)
		return (-1);
	if ((fd = xcreate(path, flags, uid, gid, mode)) == -1)
		goto error;
	if (lockfd(locks, fd) != 0)
		goto error;

	return (fd);

error:
	saved_errno = errno;
	close(fd);
	rmlock(locks, path);
	errno = saved_errno;
	return (-1);
}
Exemplo n.º 8
0
/* Make lock file. */
int
mklock(u_int locks, const char *path)
{
	char	lock[MAXPATHLEN];
	int	fd;

	if (!(locks & LOCK_DOTLOCK))
		return (0);

	if (ppath(lock, sizeof lock, "%s.lock", path) != 0)
		return (-1);

	fd = xcreate(lock, O_WRONLY, -1, -1, S_IRUSR|S_IWUSR);
	if (fd == -1) {
		if (errno == EEXIST)
			errno = EAGAIN;
		return (-1);
	}
	close(fd);

	cleanup_register(lock);
	return (0);
}
Exemplo n.º 9
0
static VlHIKMTree*  
matlab_to_hikm (mxArray const *mtree, int method_type)
{
  VlHIKMTree *tree ;
  mxArray *mK, *mdepth ;
  int K = 0, depth = 0;

  VL_USE_MATLAB_ENV ;

  if (mxGetClassID (mtree) != mxSTRUCT_CLASS) {
    mexErrMsgTxt("TREE must be a MATLAB structure.") ;
  }
  
  mK       = mxGetField(mtree, 0, "K") ;
  mdepth   = mxGetField(mtree, 0, "depth") ;
  
  if (!mK                        ||
      !uIsRealScalar (mK)        ||
      (K = (int) *mxGetPr (mK)) < 1) {
    mexErrMsgTxt("TREE.K must be a DOUBLE not smaller than one.") ;
  }
  
  if (!mdepth                    ||
      !uIsRealScalar (mdepth)    ||
      (depth = (int) *mxGetPr (mdepth)) < 1) {
    mexErrMsgTxt("TREE.DEPTH must be a DOUBLE not smaller than one.") ;
  }
  
  tree         = mxMalloc (sizeof(VlHIKMTree)) ;  
  tree-> depth = depth ;
  tree-> K     = K ;
  tree-> M     = -1 ; /* to be initialized later */
  tree-> method= method_type ;
  tree-> root  = xcreate (tree, mtree, 0) ;
  return tree ;
}