-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileParser.java
398 lines (363 loc) · 11.5 KB
/
FileParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import java.io.*;
import java.util.*;
import java.lang.*;
public class FileParser {
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
boolean empty = true;
int count = 0;
int readChars = 0;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
count++;
}
}
}
return (count == 0 && !empty) ? 1 : count;
}
finally {is.close();}
}
public static void readConfig(String configFile) {
try {
BufferedReader br = new BufferedReader(new FileReader(configFile));
String currentLine;
while ((currentLine = br.readLine()) != null) {
currentLine = currentLine.trim();
if (currentLine.equals("")) continue; // empty line
if (currentLine.charAt(0) == '#') continue; // comment
String[] tokens = currentLine.split("=");
String x = tokens[0].trim(), y = tokens[1].trim();
switch (x) {
case "dirR":
Main.outFilenameR = y;
break;
case "dirTheta":
Main.outFilenameTheta = y;
break;
case "dir1DTheta":
Main.outFilename1DTheta = y;
break;
case "A":
double _a = Double.parseDouble(y);
if (_a <= 0) throw new NumberFormatException();
Main.paramA = _a;
break;
case "B":
double _b = Double.parseDouble(y);
if (_b <= 0) throw new NumberFormatException();
Main.paramB = _b;
break;
case "C":
double _c = Double.parseDouble(y);
if (_c >= 0) throw new NumberFormatException();
Main.paramC = _c;
break;
case "K":
int _k = Integer.parseInt(y);
if (_k <= 0) throw new NumberFormatException();
Main.K = _k;
break;
case "thetaReg":
double _tr = Double.parseDouble(y);
if (_tr < 0 || _tr >= 1) throw new NumberFormatException();
Main.thetaReg = _tr;
break;
}
}
} catch (IOException e) {
System.out.println("I/O Error");
e.printStackTrace();
System.exit(0);
} catch (NumberFormatException e) {
System.out.println("Parameter Error");
e.printStackTrace();
System.exit(0);
}
}
/**
* readCSVDict:
* build a map (original ID: new ID in memory) and an inverted map
*/
public static int readCSVDict(String filename1, String filename2, Map<String, Integer> map, Map<Integer, String> invMap)
throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader(filename1));
try {
int newID = 0;
String currentLine;
int lineCount = 0;
while ((currentLine = br.readLine()) != null) {
if (Main.verbose && lineCount % 1000000 == 0) // print every 1M records
System.out.println(lineCount);
lineCount += 1;
// each line: sampleID, OTU_ID, weigth
String[] tokens = currentLine.split("\t");
String x = tokens[0];
String y = tokens[1];
if (x == "" || y == "")
continue;
if (!map.containsKey(x)) {
map.put(x, newID); newID += 1;
}
if (!map.containsKey(y)) {
map.put(y, newID); newID += 1;
}
}
// set inverse map
for (Map.Entry<String, Integer> e: map.entrySet()) {
invMap.put(e.getValue(), e.getKey());
}
return map.size();
} finally { br.close(); }
}
/*
* readCSVGraphApprox: read csv graph into a list of (x,y)'s
* WITH a dictionary in memory; weight indicated by WEIGHTED.
*
* Negative samples are approximate, in other words, non-existing links
* may actually overlap with existing links.
*
*/
public static int readCSVGraphApprox(
String filename, Map<String, Integer> map,
List<Integer> edgeSources, List<Integer> edgeTargets, List<Integer> weights,
int N, int[] negDict, Map<Integer, Integer> outDegree, boolean WEIGHTED
) throws FileNotFoundException, IOException {
Map<Integer, Integer> inDegree = new HashMap<Integer, Integer>(); // in-degree of every node
for (int i = 0; i < N; i++) outDegree.put(i, 0);
for (int i = 0; i < N; i++) inDegree.put(i, 0);
Random rand = new Random(0);
BufferedReader br = new BufferedReader(new FileReader(filename));
int count = 0;
try {
String currentLine = "";
int lineCount = 0;
while ((currentLine = br.readLine()) != null) {
// print every 1M records
if (Main.verbose && lineCount % 1000000 == 0) System.out.println(lineCount);
lineCount += 1;
String[] tokens = currentLine.split("\t");
if (tokens[0] == "" || tokens[1] == "") continue;
int x = map.get(tokens[0]), y = map.get(tokens[1]);
int w = 1;
if (WEIGHTED) w = Integer.parseInt(tokens[2]);
edgeSources.add(x); edgeTargets.add(y); weights.add(w);
// update out-degree, in-degree and #edges
outDegree.put(x, outDegree.get(x)+w);
inDegree.put(y, inDegree.get(y)+w);
count++;
}
} finally { br.close(); }
// build negative samples dictionary
if (Main.verbose) System.out.println("[Info] Building negative sample dictionary");
int negDictSize = negDict.length;
double cur_sum = 0, tot_sum = 0, part = 0;
int n = 0;
double power = 0.75;
for (int i = 0; i < N; i++)
//tot_sum += Math.pow(inDegree.get(i), power);
tot_sum += Math.pow(outDegree.get(i), power);
//tot_sum += 1;
for (int i = 0; i < negDictSize; i++) {
if (1.0 * (i+1) / negDictSize > part) {
//cur_sum += Math.pow(inDegree.get(n), power);
cur_sum += Math.pow(outDegree.get(n), power);
//cur_sum += 1;
part = cur_sum / tot_sum;
n++;
}
negDict[i] = n-1;
}
if (N != n) System.out.printf("N = %d, n = %d\n", N, n); // should be equal to N
// node with no out links (out_degree = 0)
/*
int nol = 0;
for (int i = 0; i < N; i++) if (outDegree.get(i) == 0) {
int repeat = 1;
for (int _z = 0; _z < repeat; _z++) {
int j = negDict[rand.nextInt(negDictSize)];
//int j = rand.nextInt(N);
edgeSources.add(i); edgeTargets.add(j); weights.add(1);
count++;
}
nol++;
outDegree.put(i, repeat);
}
System.out.println("nol = " + nol);
*/
// build negative links
for (int e = 0; e < count; e++) {
int i = edgeSources.get(e);
if (outDegree.get(i) == 0) {
e--; continue;
}
int w = weights.get(e);
int j = negDict[rand.nextInt(negDictSize)];
edgeSources.add(i);
edgeTargets.add(j);
weights.add(w);
}
return count;
}
/**
* readCSVGraph: read csv graph into a list of (x,y)'s
* WITH a dictionary in memory; weight indicated by WEIGHTED
*/
public static int readCSVGraph(
String filename, Map<String, Integer> map,
List<Integer> edgeSources, List<Integer> edgeTargets, List<Integer> weights,
int N, int[] negDict, Map<Integer, Integer> outDegree, boolean WEIGHTED
) throws FileNotFoundException, IOException {
Map<Integer, Integer> inDegree = new HashMap<Integer, Integer>(); // in-degree of every node
for (int i = 0; i < N; i++) outDegree.put(i, 0);
for (int i = 0; i < N; i++) inDegree.put(i, 0);
Map<Long, List<Integer>> pairMap = new HashMap<Long, List<Integer>>(); // (x,y) -> indexes
Set<Long> secondPairMap = new HashSet<Long>();
Random rand = new Random(0);
BufferedReader br = new BufferedReader(new FileReader(filename));
int count = 0;
try {
String currentLine = "";
int lineCount = 0;
while ((currentLine = br.readLine()) != null) {
// print every 1M records
if (Main.verbose && lineCount % 1000000 == 0) System.out.println(lineCount);
lineCount += 1;
String[] tokens = currentLine.split("\t");
if (tokens[0] == "" || tokens[1] == "") continue;
if (tokens[0] == tokens[1]) continue;
int x = map.get(tokens[0]), y = map.get(tokens[1]);
int w = 1;
if (WEIGHTED) w = Integer.parseInt(tokens[2]);
edgeSources.add(x); edgeTargets.add(y); weights.add(w);
// update out-degree and in-degree
outDegree.put(x, outDegree.get(x)+w);
inDegree.put(y, inDegree.get(y)+w);
count++;
}
} finally { br.close(); }
// build negative samples dictionary
if (Main.verbose) System.out.println("[Info] Building negative sample dictionary");
int negDictSize = negDict.length;
double cur_sum = 0, tot_sum = 0, part = 0;
int n = 0;
double power = 0.75;
for (int i = 0; i < N; i++)
//tot_sum += Math.pow(inDegree.get(i), power);
tot_sum += Math.pow(outDegree.get(i), power);
//tot_sum += 1;
for (int i = 0; i < negDictSize; i++) {
if (1.0 * (i+1) / negDictSize > part) {
//cur_sum += Math.pow(inDegree.get(n), power);
cur_sum += Math.pow(outDegree.get(n), power);
//cur_sum += 1;
part = cur_sum / tot_sum;
n++;
}
negDict[i] = n-1;
}
if (N != n) System.out.printf("N = %d, n = %d\n", N, n); // should be equal to N
// node with no out links (out_degree = 0)
///*
int nol = 0;
for (int i = 0; i < N; i++) if (outDegree.get(i) == 0) {
int repeat = 1;
for (int _z = 0; _z < repeat; _z++) {
int j = negDict[rand.nextInt(negDictSize)];
//int j = rand.nextInt(N);
edgeSources.add(i);
edgeTargets.add(j);
weights.add(1);
count++;
}
nol++;
outDegree.put(i, repeat);
}
//*/
// build negative links
for (int e = 0; e < count; e++) {
int i = edgeSources.get(e);
int w = weights.get(e);
while (true) {
int j = negDict[rand.nextInt(negDictSize)];
long xyid = i * N + j;
if (!pairMap.containsKey(xyid)) {
edgeSources.add(i); edgeTargets.add(j); weights.add(w);
break;
}
}
}
return count;
}
/**
* output_2d
* output to file
* arr_s.get(t) is a n*1 array
*/
public static void output_2d_1(double[] arr, String filename, Map<Integer, String> invMap, boolean constraint)
throws IOException {
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
try {
for (int i = 0; i < arr.length; i++) {
double v = arr[i];
if (constraint) {
while (v < 0) v += 2 * Math.PI;
while (v > 2 * Math.PI) v -= 2 * Math.PI;
}
String newline = invMap.get(i) + "\t" + Double.toString(v);
writer.printf("%s\n", newline);
}
} finally {
writer.close();
}
return;
}
public static void output_2d_2(double[][] arr, String filename, Map<Integer, String> invMap)
throws IOException {
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
try {
for (int i = 0; i < arr.length; i++) {
String newline = invMap.get(i);
for (int k = 0; k < Main.K; k++) {
newline = newline + "\t" + Double.toString(arr[i][k]);
}
writer.printf("%s\n", newline);
}
} finally {
writer.close();
}
return;
}
/* arr_s.get(t) is a n*1 array */
public static void
output_1d(List<double[]> arr_s, String fileDir) {
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(fileDir)))) {
for (int i = 0; i < arr_s.size(); i++) {
double[] arr = arr_s.get(i);
writer.printf("%d ", i);
for (int j = 0; j < arr.length; j++) {
writer.printf("%f ", arr[j]);
}
writer.printf("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}
/* arr_s.get(t) is a n*1 array */
public static void
output(Map<Integer, Integer> id_map, String fileDir) {
try (PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(fileDir)))) {
for (Map.Entry<Integer, Integer> e: id_map.entrySet()) {
int globalID = e.getKey();
int localID = e.getValue();
writer.printf("%d %d\n", globalID, localID);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}