-
Notifications
You must be signed in to change notification settings - Fork 0
/
WitnessMatrix.java
95 lines (81 loc) · 3.08 KB
/
WitnessMatrix.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
import edu.stanford.math.plex.*;
import java.io.*;
import java.util.*;
class WitnessMatrix {
public static void main(String[] args) throws Exception {
if(args.length != 5) {
System.out.println("Usage: WitnessMatrix data-file output-slug maxdim landmarkcount epsilon\n\nUse epsilon == 0 to use the computed Rmax instead.");
System.exit(-1);
}
String filename, slug;
Integer maxdim, landmarkcount;
Double epsilon;
filename = args[0];
slug = args[1];
maxdim = Integer.parseInt(args[2]);
landmarkcount = Integer.parseInt(args[3]);
epsilon = Double.parseDouble(args[4]);
// Read in dataset
File datafile = null;
BufferedReader br = null;
String line = null;
StringTokenizer st = null;
ArrayList<ArrayList<Double>> al = new ArrayList<ArrayList<Double>>();
int nL=0, nT=0;
datafile = new File(filename);
br = new BufferedReader(new FileReader(datafile));
while( (line=br.readLine()) != null) {
nT = 0;
ArrayList<Double> entry = new ArrayList<Double>();
st = new StringTokenizer(line, ",");
while (st.hasMoreTokens()) {
entry.add(Double.parseDouble(st.nextToken()));
nT++;
}
al.add(entry);
nL++;
}
double[][] data = new double[nL][nT];
int i=0;
for(ArrayList<Double> el : al) {
int j=0;
for(Double entry : el) {
data[i][j] = entry.doubleValue();
j++;
}
i++;
}
// Construct DistanceData
DistanceData ead = new DistanceData(data);
System.out.print("Point count: ");
System.out.println(ead.count());
// Construct WitnessComplex
int[] lms = WitnessStream.makeMaxMinLandmarks(ead, landmarkcount);
double rmax = WitnessStream.estimateRmax(ead, lms);
System.out.print("Estimated Rmax: ");
System.out.println(rmax);
WitnessStream rips;
if(epsilon == 0)
rips = Plex.WitnessStream(0.0001, maxdim, rmax, lms, ead);
else
rips = Plex.WitnessStream(0.0001, maxdim, epsilon, lms, ead);
System.out.print("Witness complex of size: ");
System.out.println(rips.size());
// Compute persistent homology
PersistenceInterval.Float[] intervals = Plex.Persistence().computeIntervals(rips,false,13);
// Write out everything
for(i=0; i<maxdim; i++) {
FileWriter output = new FileWriter(slug + "-" + Integer.toString(i) + ".dgm");
BufferedWriter out = new BufferedWriter(output);
for(PersistenceInterval.Float pif : intervals) {
if(pif.dimension == i) {
out.write(Double.toString(pif.start));
out.write(",");
out.write(Double.toString(pif.end));
out.newLine();
}
}
out.close();
}
}
}