Exemple #1
0
/**
  * The local efficiency is the global efficiency computed on the
  *  neighborhood of the node, and is related to the clustering coefficient.
  *
  *  Inputs:     W,              weighted undirected or directed connection matrix
  *                              (all weights in W must be between 0 and 1)
  *  Output:     Eloc,           local efficiency (vector)

  *  Notes:
  *      The  efficiency is computed using an auxiliary connection-length
  *  matrix L, defined as L_ij = 1/W_ij for all nonzero L_ij; This has an
  *  intuitive interpretation, as higher connection weights intuitively
  *  correspond to shorter lengths.
  *      The weighted local efficiency broadly parallels the weighted
  *  clustering coefficient of Onnela et al. (2005) and distinguishes the
  *  influence of different paths based on connection weights of the
  *  corresponding neighbors to the node in question. In other words, a path
  *  between two neighbors with strong connections to the node in question
  *  contributes more to the local efficiency than a path between two weakly
  *  connected neighbors. Note that this weighted variant of the local
  *  efficiency is hence not a strict generalization of the binary variant.

  *  Algorithm:  Dijkstra's algorithm

  *  References: Latora and Marchiori (2001) Phys Rev Lett 87:198701.
  *              Onnela et al. (2005) Phys Rev E 71:065103
  *              Fagiolo (2007) Phys Rev E 76:026107.
  *              Rubinov M, Sporns O (2010) NeuroImage 52:1059-69
  */
rowvec Connectome::localEfficiency(const mat &W)
{
    int n = W.n_rows;
    mat A = abs(sign(W)), L = 1/W, e ,se;
    L.diag().fill(0);
    rowvec Eloc = zeros(1,n), w1, sw, sa;
    colvec w2;
    uvec V;
    double numer = 0, denom = 0;

    for (int u =0;u<n;++u) {
        V = find( A.row(u)+ trans(A.col(u)) != 0 );
        w1 = arma::pow(W(uvec(1).fill(u),V),(1.0/3.0));
        w2 = arma::pow(W(V,uvec(1).fill(u)),(1.0/3.0));
        sw = w1 + trans(w2);
        // e=distance_inv_wei(L(V,V));
        e = 1/pathLength(L(V,V));
        e(find(e == datum::inf)).fill(0);

        se = arma::pow(e,(1.0/3.0)) + arma::pow(trans(e),(1.0/3.0));
        numer = accu((trans(sw)*sw)%se)/2.0;

        if (numer!=0.0) {
            sa = A(uvec(1).fill(u),V) + trans(A(V,uvec(1).fill(u)));
            denom = qPow(sum(sa),2.0)-accu(sa%sa) ;
            Eloc(u) = numer/denom;
        }
    }
    return Eloc;
}
Exemple #2
0
const glsl_type *
glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
{
   if (base_type == GLSL_TYPE_VOID)
      return void_type;

   if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
      return error_type;

   /* Treat GLSL vectors as Nx1 matrices.
    */
   if (columns == 1) {
      switch (base_type) {
      case GLSL_TYPE_UINT:
	 return uvec(rows);
      case GLSL_TYPE_INT:
	 return ivec(rows);
      case GLSL_TYPE_FLOAT:
	 return vec(rows);
      case GLSL_TYPE_BOOL:
	 return bvec(rows);
      default:
	 return error_type;
      }
   } else {
      if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
	 return error_type;

      /* GLSL matrix types are named mat{COLUMNS}x{ROWS}.  Only the following
       * combinations are valid:
       *
       *   1 2 3 4
       * 1
       * 2   x x x
       * 3   x x x
       * 4   x x x
       */
#define IDX(c,r) (((c-1)*3) + (r-1))

      switch (IDX(columns, rows)) {
      case IDX(2,2): return mat2_type;
      case IDX(2,3): return mat2x3_type;
      case IDX(2,4): return mat2x4_type;
      case IDX(3,2): return mat3x2_type;
      case IDX(3,3): return mat3_type;
      case IDX(3,4): return mat3x4_type;
      case IDX(4,2): return mat4x2_type;
      case IDX(4,3): return mat4x3_type;
      case IDX(4,4): return mat4_type;
      default: return error_type;
      }
   }

   assert(!"Should not get here.");
   return error_type;
}
Exemple #3
0
/**
  * D = pathLength(G);

  * The distance matrix contains lengths of shortest paths between all
  * pairs of nodes. An entry (u,v) represents the length of shortest path
  * from node u to node v. The average shortest path length is the
  * characteristic path length of the network.

  *     Input:      G,      weighted directed/undirected connection matrix
  *     Output:     D,      distance matrix

  * The input matrix must be a mapping from weight to distance. For
  * instance, in a weighted correlation network, higher correlations are
  * more naturally interpreted as shorter distances, and the input matrix
  * should consequently be some inverse of the connectivity matrix.
  *    Lengths between disconnected nodes are set to Inf.
  *    Lengths on the main diagonal are set to 0.
  * Algorithm: Dijkstra's algorithm.
*/
mat Connectome::pathLength(const mat &G)
{
    uint n = G.n_rows,v=0;
    mat D = mat(n,n).fill(datum::inf), G1, t;
    D.diag().fill(0);
    uvec S, V, W, tt;
    for (uint u=0;u<n;++u) {
        S = linspace<uvec>(0,n-1,n);
        G1 = G;
        V = uvec(1).fill(u);

        while (true) {
            // instead of replacing indices by 0 like S(V)=0;
            // we declare all indices then remove the V indeces
            // from S. Notice that it is assured that tt should
            // be one element since indices don't repeat
            for (int i=0;i<V.n_elem;++i) {
                tt = find(S == V(i),1);
                if (!tt.is_empty())
                    S.shed_row(tt(0));
            }
            G1.cols(V).fill(0);
            for (uint j = 0;j<V.n_elem;++j) {
                v = V(j);
                W = find(G1.row(v)>0);
                D(uvec(1).fill(u),W) = arma::min(D(uvec(1).fill(u),W),
                                                 D(u,v)+G1(uvec(1).fill(v),W));
            }
            t = D(uvec(1).fill(u),S);
            if (t.is_empty() || !is_finite(t.min()))
                break;
            V = find( D.row(u) == t.min());
        }
    }
    return D;
}
int ColorLUT::generate(ColorModel *model, const Frame8 &frame, const RectA &region)
{
    Fpoint meanVal;
    float angle, pangle, pslope, meanSat;
    float yi, istep, s, xsat, sat;
    int result;

    m_hpixels = (HuePixel *)maxMalloc(sizeof(HuePixel)*CL_HPIXEL_MAX_SIZE, &m_hpixelSize);
    if (m_hpixels==NULL)
        return -1; // not enough memory

    m_hpixelSize /= sizeof(HuePixel);

    map(frame, region);
    mean(&meanVal);
    angle = atan2(meanVal.m_y, meanVal.m_x);
    Fpoint uvec(cos(angle), sin(angle));

    Line hueLine(tan(angle), 0.0);

    pangle = angle + PI/2; // perpendicular angle
    pslope = tan(pangle); // perpendicular slope
    Line pLine(pslope, meanVal.m_y - pslope*meanVal.m_x); // perpendicular line through mean

    // upper hue line
    istep = fabs(m_iterateStep/uvec.m_x);
    yi = iterate(hueLine, istep);
    yi += fabs(m_hueTol*yi); // extend
    model->m_hue[0].m_yi = yi;
    model->m_hue[0].m_slope = hueLine.m_slope;

    // lower hue line
    yi = iterate(hueLine, -istep);
    yi -= fabs(m_hueTol*yi); // extend
    model->m_hue[1].m_yi = yi;
    model->m_hue[1].m_slope = hueLine.m_slope;

    // inner sat line
    s = sign(uvec.m_y);
    istep = s*fabs(m_iterateStep/cos(pangle));
    yi = iterate(pLine, -istep);
    yi -= s*fabs(m_satTol*(yi-pLine.m_yi)); // extend
    xsat = yi/(hueLine.m_slope-pslope); // x value where inner sat line crosses hue line
    Fpoint minsatVec(xsat, xsat*hueLine.m_slope); // vector going to inner sat line
    sat = dot(uvec, minsatVec); // length of line
    meanSat = dot(uvec, meanVal);
    if (sat < m_minSat) // if it's too short, we need to extend
    {
        minsatVec.m_x = uvec.m_x*m_minSat;
        minsatVec.m_y = uvec.m_y*m_minSat;
        yi = minsatVec.m_y - pslope*minsatVec.m_x;
    }
    model->m_sat[0].m_yi = yi;
    model->m_sat[0].m_slope = pslope;

    // outer sat line
    yi = iterate(pLine, istep);
    yi += s*fabs(m_maxSatRatio*m_satTol*(yi-pLine.m_yi)); // extend
    model->m_sat[1].m_yi = yi;
    model->m_sat[1].m_slope = pslope;

    // swap if outer sat line is greater than inner sat line
    // Arbitrary convention, but we need it to be consistent to test membership (checkBounds)
    if (model->m_sat[1].m_yi>model->m_sat[0].m_yi)
    {
        Line tmp = model->m_sat[0];
        model->m_sat[0] = model->m_sat[1];
        model->m_sat[1] = tmp;
    }

    free(m_hpixels);

    // calculate goodness
    result = (meanSat-m_minSat)*100/64 + 10; // 64 because it's half of our range
    if (result<0)
        result = 0;
    if (result>100)
        result = 100;

    return result;
}
Exemple #5
0
/**
  * Node betweenness centrality is the fraction of all shortest paths in
  * the network that contain a given node. Nodes with high values of
  * betweenness centrality participate in a large number of shortest paths.
  *
  *     Input:      G,      weighted (directed/undirected) connection matrix.
  *     Output:     BC,     node betweenness centrality vector.
  *                 EBC,    edge betweenness centrality matrix.
  *
  * Notes:
  *    The input matrix must be a mapping from weight to distance. For
  * instance, in a weighted correlation network, higher correlations are
  * more naturally interpreted as shorter distances, and the input matrix
  * should consequently be some inverse of the connectivity matrix.
  *    Betweenness centrality may be normalised to [0,1] via BC/[(N-1)(N-2)]
  *
  * Reference: Brandes (2001) J Math Sociol 25:163-177.
  */
rowvec Connectome::betweenessCentrality(const mat &G, mat &EBC)
{
    uint n = G.n_rows,q = n-1,v=0,w=0;
    double Duw,DPvw;
    vec t;
    rowvec BC = zeros(1,n),D,NP,DP;
    uvec S,Q,V,tt,W;
    mat G1;
    umat P;
    EBC = zeros<mat>(n,n);

    for (uint u = 0; u<n;++u) {
        D = rowvec(1,n).fill(datum::inf); D(u) = 0;
        NP = zeros(1,n); NP(u) = 1;
        S = linspace<uvec>(0,n-1,n);
        P = zeros<umat>(n,n);
        Q = zeros<uvec>(n);
        q = n-1;
        G1 = G;
        V = uvec(1).fill(u);
        while (true) {
            // instead of replacing indices by 0 like S(V)=0;
            // we declare all indices then remove the V indeces
            // from S. Notice that it is assured that tt should
            // be one element since indices don't repeat
            for (int i=0;i<V.n_elem;++i) {
                tt = find(S == V(i),1);
                if (!tt.is_empty())
                    S.shed_row(tt(0));
            }
            G1.cols(V).fill(0);
            for (uint i=0; i<V.n_elem;++i) {
                v = V(i);
                Q(q) = v; --q;
                W = find( G1.row(v) != 0);
                for (uint j = 0;j<W.n_elem;++j) {
                    w = W(j);
                    Duw = D(v)+G1(v,w);
                    if (Duw<D(w)) {
                        D(w) = Duw;
                        NP(w) = NP(v);
                        P.row(w).fill(0);
                        P(w,v) = 1;
                    }
                    else if (Duw == D(w)) {
                        NP(w) += NP(v);
                        P(w,v) = 1;
                    }
                }
            }

            if (S.is_empty())
                break;
            t = D(S);
            if ( !is_finite(t.min()) ){
                // the number of inf elements is assumed to be always = q
                Q.subvec(0,q) = find(D == datum::inf);
                break;
            }
            V = find(D == t.min());
        }
        DP = zeros(1,n);
        for (uint i=0; i<Q.n_elem-1;++i) {
            w = Q(i);
            BC(w) += DP(w);
            tt = find(P.row(w) != 0);
            for (uint j=0; j<tt.n_elem;++j) {
                v = tt(j);
                DPvw = (1+DP(w))*NP(v)/NP(w);
                DP(v) += DPvw;
                EBC(v,w) += DPvw;
            }
        }
    }
    return BC;
}
void AnalysisResults::AddParent(QString key, uword start_row, uword end_row)
{
    parent_rows_[key] = uvec({start_row, end_row});
}