void segment::segment_sequence(const dMatrix src_M, dMatrix& dst_M, Beliefs E, double c, int *num_ccs, int **sizes) { int i, j, t, seqLength, dimH; seqLength = src_M.getWidth(); dimH = E.belStates[0].getLength(); // Build a graph std::vector<edge> edges; for( t=0; t<seqLength-1; t++ ) { edge e(t,t+1,diff_unary(E,t,t+1,dimH)); edges.push_back(e); } // segment universe u(seqLength); // make a disjoint-set forest segment_graph(u, seqLength, seqLength-1, edges, c); *num_ccs = u.num_sets(); int *elt_labels, *seg_sizes, **seg_elts; elt_labels = new int[seqLength]; seg_sizes = new int[u.num_sets()]; seg_elts = new int*[u.num_sets()]; u.get_segments(seqLength, elt_labels, seg_sizes, seg_elts); for(i=0; i<u.num_sets(); i++) printf("[%d]", seg_sizes[i]); printf("\n"); //getchar(); if( sizes ) { delete[] *sizes; *sizes = new int[u.num_sets()]; memcpy(*sizes,seg_sizes,u.num_sets()*sizeof(int)); } // Generate observations for the new layer dst_M.resize(u.num_sets(), src_M.getHeight()); for( i=0; i<src_M.getHeight(); i++ ) for( j=0; j<src_M.getWidth(); j++ ) dst_M.addValue(i,elt_labels[j],src_M.getValue(i,j)); for( i=0; i<dst_M.getHeight(); i++ ) for( j=0; j<dst_M.getWidth(); j++ ) dst_M(i,j) = dst_M(i,j) / seg_sizes[j]; // Clean up and return delete[] elt_labels; elt_labels = 0; delete[] seg_sizes; seg_sizes = 0; for(i=0; i<u.num_sets(); i++) { delete[] seg_elts[i]; seg_elts[i] = 0; } delete[] seg_elts; seg_elts = 0; }
double segment::diff_unary(const dMatrix M, int c1, int c2) { double val = 0; for( int r=0; r<M.getHeight(); r++ ) val += fabs( M(r,c1) - M(r,c2) ); return val; }
void segment::segment_sequence(const dMatrix src_M, dMatrix& dst_M, std::vector<Beliefs> E, double c, int *num_ccs, int**labels) { int i, j, t, seqLength, dimH, dimY; seqLength = src_M.getWidth(); dimH = E[0].belStates[0].getLength(); dimY = (int)E.size(); std::vector<Beliefs> logE; getLogE(E, logE); // Build a graph std::vector<edge> edges; for( t=0; t<seqLength-1; t++ ) { edge e(t,t+1,diff_unary(E,t,t+1,dimH,dimY)); edges.push_back(e); } // segment universe u(seqLength); // make a disjoint-set forest segment_graph(u, seqLength, seqLength-1, edges, c); *num_ccs = u.num_sets(); int *elt_labels, *seg_sizes, **seg_elts; elt_labels = new int[seqLength]; seg_sizes = new int[u.num_sets()]; seg_elts = new int*[u.num_sets()]; u.get_segments(seqLength, elt_labels, seg_sizes, seg_elts); // Optionally, save elt_labels to labels if( labels!=0 ) memcpy(*labels, elt_labels, sizeof(int)*seqLength); // Generate observations for the new layer dst_M.resize(u.num_sets(), src_M.getHeight()); for( i=0; i<src_M.getHeight(); i++ ) for( j=0; j<src_M.getWidth(); j++ ) dst_M.addValue(i,elt_labels[j],src_M.getValue(i,j)); for( i=0; i<dst_M.getHeight(); i++ ) for( j=0; j<dst_M.getWidth(); j++ ) dst_M(i,j) = dst_M(i,j) / seg_sizes[j]; // Clean up and return delete[] elt_labels; elt_labels = 0; delete[] seg_sizes; seg_sizes = 0; for(i=0; i<u.num_sets(); i++) { delete[] seg_elts[i]; seg_elts[i] = 0; } delete[] seg_elts; seg_elts = 0; }