Ejemplo n.º 1
0
vector<V3DLONG> V_NeuronSWC::unique_nid()
{
	vector<V3DLONG> res;	if (row.size()<1) return res;
	res.push_back(row.at(0).n);
	for (V3DLONG i=1;i<row.size();i++)
	{
		V3DLONG cur_nid = V3DLONG(row.at(i).n);
		if (value_in_vector(res, cur_nid)<0)
			res.push_back(cur_nid);
	}
	return res;
}
Ejemplo n.º 2
0
vector<V_NeuronSWC_coord> V_NeuronSWC::unique_ncoord()
{
	vector<V_NeuronSWC_coord> res;	if (row.size()<1) return res;
	V_NeuronSWC_coord cur_coord; cur_coord.set(row.at(0).x, row.at(0).y, row.at(0).z);
	res.push_back(cur_coord);
	for (V3DLONG i=1;i<row.size();i++)
	{
		cur_coord.set(row.at(i).x, row.at(i).y, row.at(i).z);
		if (value_in_vector(res, cur_coord)<0)
			res.push_back(cur_coord);
	}
	return res;
}
Ejemplo n.º 3
0
V_SWCNodes V_NeuronSWC::unique_nodes_info()
{
	V_SWCNodes res;	if (row.size()<1) return res;
	res.nid_array.push_back(row.at(0).n);
	res.ncoord_array.push_back(row.at(0).get_coord());
	res.nid_ipos_lut[res.nid_array.at(0)] = 0;

	for (V3DLONG i=1;i<row.size();i++)
	{
		V3DLONG cur_nid = V3DLONG(row.at(i).n);
		V_NeuronSWC_coord cur_ncoord = row.at(i).get_coord();

		V3DLONG ind_nid = value_in_vector(res.nid_array, cur_nid);
		V3DLONG ind_nccord = value_in_vector(res.ncoord_array, cur_ncoord);
		if (ind_nid==ind_nccord && ind_nid>=0 && ind_nccord>=0)
		{
			res.nid_array.push_back(cur_nid);
			res.ncoord_array.push_back(cur_ncoord);
			res.nid_ipos_lut[cur_nid] = ind_nid;
		}
	}
	return res;
}
Ejemplo n.º 4
0
bool join_two_V_NeuronSWC_old(V_NeuronSWC & destination_swc, V_NeuronSWC & subject_swc)
{
    if (subject_swc.nrows()<=0)
    {
        qDebug("subject_swc is empty in join_two_V_NeuronSWC()");
        return false;
    }

    if (destination_swc.nrows()<=0) {
        destination_swc = subject_swc;
        return true;
    }
    else
    {
        if (!simplify_V_NeuronSWC_nodeindex(destination_swc))
        {
            qDebug("inconsistent destination neuron structure found in join_two_V_NeuronSWC()");
            return false; //some error happens
        }

        V_NeuronSWC_unit v;
        V3DLONG i=0;
        destination_swc.printInfo();
        //one problem I don't consider now is that the nodes (3D locations) in a neuron reconstruction may duplicated. This should be simplified later too

        V3DLONG n0 = destination_swc.nrows();

        map <V3DLONG, V3DLONG> sub_index_map = unique_V_NeuronSWC_nodeindex(subject_swc);
        V3DLONG *ipos=0;
        ipos=new V3DLONG [subject_swc.nrows()];
        qDebug()<<"start ==================== map "<<subject_swc.name.c_str();
        for (i=0; i<subject_swc.nrows(); i++)
        {
            ipos[i] = destination_swc.getFirstIndexof3DPos(subject_swc.row.at(i));
            if (ipos[i]>=0) qDebug("**detect an overlapping node [sub ind = %d, dest_ind=%d]",i,ipos[i]);
            sub_index_map[subject_swc.row.at(i).data[0]] = (ipos[i]>=0) ? destination_swc.row.at(ipos[i]).data[0] : (sub_index_map[subject_swc.row.at(i).data[0]]+n0);
            qDebug()<<"map "<<subject_swc.row.at(i).data[0]<<" to "<<sub_index_map[subject_swc.row.at(i).data[0]];
        }
        qDebug()<<"finish ==================== map ";

        for (i=0; i<subject_swc.nrows(); i++)
        {
            v = subject_swc.row.at(i);
            qDebug()<<i<<"before : "<<v.data[0]<<" "<<v.data[1]<<" "<<v.data[2]<<" "<<v.data[3]<<" "<<v.data[4]<<" "<<v.data[5]<<" "<<v.data[6];

            v.data[0] = sub_index_map[v.data[0]];
            if (ipos[i]>=0) //overlap
            {
                if (v.data[6]>=0) //non-root
                {
                    if (ipos[V3DLONG(v.data[6])]<0) //if the parent overlap as well, then do no add
                    {
                        v.data[6] = sub_index_map[v.data[6]]; //if the overalp node is not a root (thus has its own parent, then inherit)
                        destination_swc.append(v); //in this case, still need to add this swc unit into the dest_swc
                    }
                }
                else //root
                {
                    v.data[6] = destination_swc.row.at(ipos[i]).data[6]; //if the overlap node is a root for subject seg, then copy the parent in dest_swc
                    qDebug()<<"**omit one overlap node **";
                    //in this case, no need to add this swc unit into the dest_swc
                }
            }
            else //if not an overlap node, should just add (but the node indexes should be updated)
            {
                if (v.data[6]>=0)
                {
                    v.data[6] = sub_index_map[v.data[6]]; //if the overalp node is not a root (thus has its own parent, then inherit)
                    destination_swc.append(v);
                }
                else
                {
                    //if the overlap node is a root for subject seg, then copy the parent in dest_swc
                    qDebug()<<"**copy a non-overlapping root node **";
                    destination_swc.append(v);	//in this case, still need to add as it is not overlap
                }
            }
            qDebug()<<i<<"after : "<<v.data[0]<<" "<<v.data[1]<<" "<<v.data[2]<<" "<<v.data[3]<<" "<<v.data[4]<<" "<<v.data[5]<<" "<<v.data[6];
        }

        //search for all root nodes
        destination_swc.printInfo();
        vector < vector<V3DLONG> > des_rootnode_overlap_info;
        vector <V3DLONG> overlap_root_array;
        V_NeuronSWC newswc = destination_swc;
        newswc.row.clear();
        for (i=0; i<destination_swc.row.size(); i++) //note that I search the entire merged destination, so that the part originally from subject will get searched again
        {
            v = destination_swc.row.at(i);
            if (v.parent<0)
            {
                vector <V3DLONG> mypos = destination_swc.getAllIndexof3DPos(v, i);
                if (mypos.size()>1)
                {
                    v3d_msg("Detect a root node that has the same coordinates of more than one other neuron nodes. Your data may have error. ");
                }

                if (mypos.size()>0) //overlap root, remove
                {
                    vector <V3DLONG> mypos1;
                    for (V3DLONG j=0; j<mypos.size(); j++)
                    {
                        if (destination_swc.row.at(mypos.at(j)).parent<0) //if it is a root
                        {
                            if (destination_swc.row.at(mypos.at(j)).n!=v.n) //then it is not a redundant row, -- maybe there is some other nodes link to this one. then do not add it to the newswc
                            {
                                destination_swc.row.at(mypos.at(j)).n = v.n;
                            }
                        }
                        else //the overlap one is not a root
                        {
                            if (destination_swc.row.at(mypos.at(j)).n!=v.n)
                                mypos1.push_back(destination_swc.row.at(mypos.at(j)).n);
                            //if equal, do nothing (which means remove this root-row. In this case, no node mapping is needed, but the root should be deleted
                        }
                    }

                    if (mypos1.size()>0)
                    {
                        overlap_root_array.push_back(v.n);
                        des_rootnode_overlap_info.push_back(mypos1);
                        //remove the current row by not copy to the new structure

                        qDebug()<<"remove overlap root node"<<v.n;
                    }
                    //newswc.append(v);
                }
                else //non-overlapping root, keep
                {
                    newswc.append(v);
                }
            }
            else //non_root, keep
            {
                newswc.append(v);
            }
        }

        for (i=0; i<overlap_root_array.size(); i++)
            qDebug()<<"overlapping root array "<<overlap_root_array.at(i);

        for (i=0; i<newswc.row.size(); i++) //now merge & update
        {
            v = newswc.row.at(i);
            V3DLONG cur_parent = V3DLONG(v.parent);
            V3DLONG mytmp = value_in_vector(overlap_root_array, cur_parent);
            if (mytmp>=0)
            {
                qDebug()<<"cur node "<<v.n<< " cur parent "<<v.parent << "new parent " << des_rootnode_overlap_info.at(mytmp).at(0);
                newswc.row.at(i).parent = des_rootnode_overlap_info.at(mytmp).at(0);

                for (V3DLONG j=1; j<des_rootnode_overlap_info.at(mytmp).size(); j++)
                {
                    v.parent = des_rootnode_overlap_info.at(mytmp).at(j);
                    newswc.append(v); //append some new records if one root mapped to multiple other non-root nodes
                }
            }
        }
        destination_swc = newswc; //update

        //save a temp swc file
        FILE * fp = fopen("/Users/pengh/temp/testaaa.swc", "wt");
        if (fp)
        {
            fprintf(fp, "#name %s\n", "test123.swc");
            fprintf(fp, "#comment %s\n", "jointed neuron");

            fprintf(fp, "#n,type,x,y,z,radius,parent\n");
            for (i=0; i<destination_swc.row.size(); i++)
            {
                v = destination_swc.row.at(i);
                fprintf(fp, "%ld %ld %5.3f %5.3f %5.3f %5.3f %ld\n", V3DLONG(v.data[0]), V3DLONG(v.data[1]), v.data[2], v.data[3], v.data[4], v.data[5], V3DLONG(v.data[6]));
            }

            fclose(fp);
        }

        if(ipos) {
            delete []ipos;
            ipos=0;
        }
    }

    destination_swc.b_jointed = true; //091029 RZC
    return true;
}
Ejemplo n.º 5
0
bool join_two_V_NeuronSWC(V_NeuronSWC & destination_swc, V_NeuronSWC & subject_swc)
{
    if (subject_swc.nrows()<=0) {
        qDebug("subject_swc is empty in join_two_V_NeuronSWC()");
        return false;
    }
    if (destination_swc.nrows()<=0) {
        destination_swc = subject_swc;
        return true;
    }

    //step 0: to simplify the des neuron, to make the joining simpler
    if (!simplify_V_NeuronSWC_nodeindex(destination_swc))
    {
        qDebug("inconsistent destination neuron structure found in join_two_V_NeuronSWC()");
        return false; //some error happens
    }

    //first just concatenate records
    V_NeuronSWC_unit v, vp;
    V3DLONG i=0, j=0;

    V3DLONG nr_des = destination_swc.nrows();
    for (i=0; i<subject_swc.nrows(); i++)
    {
        v = subject_swc.row.at(i);
        v.n += nr_des;
        if (v.parent>=0) v.parent += nr_des;
        destination_swc.append(v);
    }
    nr_des = destination_swc.nrows();

    //then produce the adjacency matrix
    vector<V_NeuronSWC_coord> unpos = destination_swc.unique_ncoord();
    V3DLONG N = unpos.size();

    map <V3DLONG, V3DLONG> nid_row_lut;
    for (i=0; i<nr_des; i++) {
        nid_row_lut[destination_swc.row.at(i).n] = i;
    }

    map <V3DLONG, V3DLONG> row_unid_lut;
    for (i=0; i<nr_des; i++)
    {
        V_NeuronSWC_coord c = destination_swc.row.at(i).get_coord();
        V3DLONG ic = value_in_vector(unpos, c);
        if (ic<0)
        {
            v3d_msg("Indexing error! Check data in join_two_V_NeuronSWC.");
            return false;
        }
        row_unid_lut[i] = ic;
    }

    vector < vector <V3DLONG> > adjm;
    vector <V3DLONG> plist;
    for (i=0; i<N; i++) adjm.push_back(plist); //initialize as empty

    vector < vector <float> > adjm_radius;
    vector <float> tmpr;
    for (i=0; i<N; i++) adjm_radius.push_back(tmpr); //initialize as empty

    vector < vector <float> > adjm_type;
    vector <float> tmpt;
    for (i=0; i<N; i++) adjm_type.push_back(tmpt); //initialize as empty

    for (i=0; i<nr_des; i++)
    {
        v = destination_swc.row.at(i);
        V3DLONG iv = row_unid_lut[i];

        if (v.parent>=0)
        {
            vp = destination_swc.row.at(nid_row_lut[v.parent]);
            V3DLONG ip =  row_unid_lut[nid_row_lut[v.parent]];
            adjm.at(iv).push_back(ip); //add it anyway, will find unique one later. Indeed even unnecessary to find the unique ones, because the repeated record will merge automatically
            adjm_radius.at(iv).push_back(float(v.r));
            adjm_type.at(iv).push_back(float(v.type));
            //need to record neuron edge type and radius as well later
        }
        else
        {
            //do nothing about the parent, i.e. this is a root for this record, and thus do not add to the parent list at all
            //but still update type and radius
            adjm_radius.at(iv).push_back(float(v.r));
            adjm_type.at(iv).push_back(float(v.type));
        }
    }

    //no need to find unique ones

    //then re-produce a new swc
    V_NeuronSWC newswc = destination_swc;
    newswc.row.clear();
    for (i=0; i<N; i++)
    {
        vector <V3DLONG> & cur_pa = adjm.at(i);
        vector <float> & cur_r = adjm_radius.at(i);
        vector <float> & cur_t = adjm_type.at(i);
        V_NeuronSWC_coord & cur_coord = unpos.at(i);

        if (cur_pa.size()<=0) //a root
        {
            v.n = i;
            v.type = double(cur_t.back()); //direct use the last one, which means the later one overwrite the first one
            v.x = cur_coord.x;
            v.y = cur_coord.y;
            v.z = cur_coord.z;
            v.r = cur_r.back(); //direct use the last one, which means the later one overwrite the first one
            v.parent = -1;
            newswc.append(v);
        }
        else
        {
            for (j=0; j<cur_pa.size(); j++)
            {
                v.n = i;
                v.type = double(cur_t.at(j)); //allow he later one overwrite the earlier one(s) if there are redundant records
                v.x = cur_coord.x;
                v.y = cur_coord.y;
                v.z = cur_coord.z;
                v.r = cur_r.at(j); //so I allow the later one overwrite the earlier one(s) if there are redundant records
                v.parent = cur_pa.at(j);
                newswc.append(v);
            }
        }
    }


    //copy the results
    destination_swc = newswc;
    destination_swc.b_jointed = true;
    return true;
}