COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
MCLConvert.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <vector>
4#include <numeric>
5#include <algorithm>
6#include <string.h>
7#include <assert.h>
8using namespace std;
9
10template <typename T>
11vector<size_t> sortIndices(const vector<T> &v) {
12
13 // initialize original index locations
14 vector<size_t> idx(v.size());
15 iota(idx.begin(), idx.end(), 0);
16
17 // sort indexes based on comparing values in v
18 sort(idx.begin(), idx.end(),
19 [&v](size_t i1, size_t i2) {return v[i1] > v[i2];});
20
21 return idx;
22}
23
24
25// simply order vertices by considering isolated vertices
26vector<int64_t> MCLOrder(string fname)
27{
28 ifstream mtxfile (fname);
29 string line;
30 int64_t m, n, nnz;
31 int64_t v1, v2;
32 double val;
33 vector<int64_t> mclorder;
34 if (mtxfile.is_open())
35 {
36 while(mtxfile.peek()=='%') getline (mtxfile,line); // ignore comments
37 mtxfile >> m >> n >> nnz;
38 assert(m==n);
39 vector<bool> nonisolated (m,false);
40 int64_t count = 0;
41 while( mtxfile >> v1 >> v2 >> val)
42 {
43 if(!nonisolated[v1]) {nonisolated[v1] = true; count++;}
44 if(!nonisolated[v2]) {nonisolated[v2] = true; count++;}
45 }
46 mtxfile.close();
47
48
49 cout << "vertices: " << m << " and non isolated vertices: " << count << endl;
50 if(m != count)
51 {
52 mclorder.resize(count);
53 for(int64_t i=0, j=0; i<m ; i++)
54 {
55 if(nonisolated[i])
56 mclorder[j++] = i;
57 }
58
59 }
60 }
61 return mclorder; // empty here
62
63}
64
65void convert(string ifname, string ofname, string sort = "revsize")
66{
67 ifstream infile (ifname);
68 int64_t item, clustID;
69 int64_t nclust = 0;
70 vector<vector<int64_t>> clusters;
71 if (infile.is_open())
72 {
73 infile >> item >> clustID; // get rid of the header;
74 while(infile >> item >> clustID)
75 {
76 nclust = max(nclust, clustID+1); // because clustID is zero-based
77 if(clustID >= clusters.size())
78 {
79 clusters.resize(clustID * 2 + 1);
80 }
81 clusters[clustID].push_back(item);
82 }
83 infile.close();
84 }
85 else
86 {
87 cout << "Unable to open " << ifname << endl;
88 return;
89 }
90
91 cout << "Number of clusters from HipMCL: " << nclust << endl;
92
93 ofstream outfile (ofname);
94 if(sort == "revsize")
95 {
96 vector<int64_t> clustSize(nclust, 0);
97 for(int64_t i=0; i< nclust; i++)
98 clustSize[i] = clusters[i].size();
99 vector<size_t> sidx = sortIndices(clustSize);
100 if (outfile.is_open())
101 {
102 for(int64_t i=0; i<nclust; i++)
103 {
104 int64_t cl = sidx[i];
105 for(uint64_t j=0; j<clusters[cl].size() ; j++)
106 {
107 outfile << clusters[cl][j] << "\t";
108 }
109 outfile << endl;
110 }
111 outfile.close();
112 }
113 else cout << "Unable to open " << ofname << endl;
114 }
115 else
116 {
117 if (outfile.is_open())
118 {
119 for(int64_t i=0; i<nclust; i++)
120 {
121 for(uint64_t j=0; j<clusters[i].size() ; j++)
122 {
123 outfile << clusters[i][j] << "\t";
124 }
125 outfile << endl;
126 }
127 outfile.close();
128 }
129 else cout << "Unable to open " << ofname << endl;
130 }
131
132
133
134}
135
136int main(int argc, char* argv[])
137{
138
139 string ifilename = "";
140 string ofilename = "";
141 string sort = "revsize";
142
143 cout << "Reformatting HipMCL output to MCL format.....\n";
144 if(argc < 4)
145 {
146 cout << "Usage: ./mclconvert -M <IN_FILENAME> -o <OUT_FILENAME>(required)\n";
147 cout << "-sort <Sort clusters by their sizes> (default:revsize)\n";
148 cout << "Example: ./mclconvert -M input.mtx" << endl;
149 return -1;
150 }
151
152 for (int i = 1; i < argc; i++)
153 {
154 if (strcmp(argv[i],"-M")==0)
155 {
156 ifilename = string(argv[i+1]);
157 printf("Input filename: %s",ifilename.c_str());
158 }
159 else if (strcmp(argv[i],"-o")==0)
160 {
161 ofilename = string(argv[i+1]);
162 printf("Output filename: %s",ofilename.c_str());
163 }
164 else if (strcmp(argv[i],"-sort")==0)
165 {
166 sort = string(argv[i + 1]);
167 printf("\nSorting clusters by their size (revsize or none)? :%s",sort.c_str());
168 }
169 }
170 printf("\n");
171 convert(ifilename, ofilename, sort);
172 return 0;
173}
string ofname
int main()
Definition Driver.cpp:12
void convert(string ifname, string ofname, string sort="revsize")
vector< size_t > sortIndices(const vector< T > &v)
vector< int64_t > MCLOrder(string fname)
int size
Definition common.h:20
void iota(_ForwardIter __first, _ForwardIter __last, T __value)