/
RunPLCM.java
225 lines (183 loc) · 7.32 KB
/
RunPLCM.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
/*
This file is part of jLCM - see https://github.com/martinkirch/jlcm/
Copyright 2013,2014 Martin Kirchgessner, Vincent Leroy, Alexandre Termier, Sihem Amer-Yahia, Marie-Christine Rousset, Université Joseph Fourier and CNRS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
or see the LICENSE.txt file joined with this program.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package fr.liglab.jlcm;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import fr.liglab.jlcm.internals.ExplorationStep;
import fr.liglab.jlcm.io.AllFISConverter;
import fr.liglab.jlcm.io.FileCollector;
import fr.liglab.jlcm.io.FileCollectorWithIDMapper;
import fr.liglab.jlcm.io.FileWithStringIDsIterable;
import fr.liglab.jlcm.io.MultiThreadedFileCollector;
import fr.liglab.jlcm.io.NullCollector;
import fr.liglab.jlcm.io.PatternSortCollector;
import fr.liglab.jlcm.io.PatternsCollector;
import fr.liglab.jlcm.io.PatternsWriter;
import fr.liglab.jlcm.io.StdOutCollector;
import fr.liglab.jlcm.util.MemoryPeakWatcherThread;
import fr.liglab.jlcm.util.ProgressWatcherThread;
/**
* jLCM as a command-line utility. Invoke without arguments to print the manual.
*/
public class RunPLCM {
protected static long chrono;
public static void main(String[] args) throws Exception {
Options options = new Options();
CommandLineParser parser = new PosixParser();
options.addOption("a", false, "Output all frequent itemsets, not only closed ones");
options.addOption(
"b",
false,
"Benchmark mode : patterns are not outputted at all (in which case OUTPUT_PATH is ignored)");
options.addOption("h", false, "Show help");
options.addOption(
"m",
false,
"Give peak memory usage after mining (instanciates a watcher thread that periodically triggers garbage collection)");
options.addOption("s", false, "Sort items in outputted patterns, in ascending order");
options.addOption("S", false, "Use arbitrary strings as item IDs (space-separated)");
options.addOption("t", true, "How many threads will be launched (defaults to your machine's processors count)");
options.addOption("v", false, "Enable verbose mode, which logs every extension of the empty pattern");
options.addOption("V", false,
"Enable ultra-verbose mode, which logs every pattern extension (use with care: it may produce a LOT of output)");
try {
CommandLine cmd = parser.parse(options, args);
if (cmd.getArgs().length < 2 || cmd.getArgs().length > 3 || cmd.hasOption('h')) {
printMan(options);
} else {
standalone(cmd);
}
} catch (ParseException e) {
printMan(options);
}
}
public static void printMan(Options options) {
String syntax = "java fr.liglab.mining.RunPLCM [OPTIONS] INPUT_PATH MINSUP [OUTPUT_PATH]";
String header = "\nIf OUTPUT_PATH is missing, patterns are printed to standard output.\nOptions are :";
String footer = "Version 1.7 - Copyright 2013,2014,2015,2016 Martin Kirchgessner, Vincent Leroy, Alexandre Termier, "
+ "Sihem Amer-Yahia, Marie-Christine Rousset, Université Joseph Fourier and CNRS";
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(80, syntax, header, options, footer);
}
public static void standalone(CommandLine cmd) {
String[] args = cmd.getArgs();
int minsup = Integer.parseInt(args[1]);
MemoryPeakWatcherThread memoryWatch = null;
String outputPath = null;
if (args.length >= 3) {
outputPath = args[2];
}
if (cmd.hasOption('m')) {
memoryWatch = new MemoryPeakWatcherThread();
memoryWatch.start();
}
chrono = System.currentTimeMillis();
ExplorationStep initState;
Map<Integer, String> itemsIdMap = null;
if (cmd.hasOption('S')) {
FileWithStringIDsIterable reader = new FileWithStringIDsIterable(args[0]);
initState = new ExplorationStep(minsup, reader);
reader.close();
Map<String, Integer> rawMap = reader.getMap();
itemsIdMap = new HashMap<Integer, String>(rawMap.size());
for (Entry<String, Integer> entry : rawMap.entrySet()) {
itemsIdMap.put(entry.getValue(), entry.getKey());
}
} else {
initState = new ExplorationStep(minsup, args[0]);
}
long loadingTime = System.currentTimeMillis() - chrono;
System.err.println("Dataset loaded in " + loadingTime + "ms");
if (cmd.hasOption('V')) {
ExplorationStep.verbose = true;
ExplorationStep.ultraVerbose = true;
} else if (cmd.hasOption('v')) {
ExplorationStep.verbose = true;
}
int nbThreads = Runtime.getRuntime().availableProcessors();
if (cmd.hasOption('t')) {
nbThreads = Integer.parseInt(cmd.getOptionValue('t'));
}
PatternsCollector collector = instanciateCollector(cmd, outputPath, nbThreads, itemsIdMap);
PLCM miner = new PLCM(collector, nbThreads, new ProgressWatcherThread());
chrono = System.currentTimeMillis();
miner.lcm(initState);
chrono = System.currentTimeMillis() - chrono;
Map<String, Long> additionalCounters = new HashMap<String, Long>();
additionalCounters.put("miningTime", chrono);
additionalCounters.put("outputtedPatterns", collector.close());
additionalCounters.put("loadingTime", loadingTime);
additionalCounters.put("avgPatternLength", (long) collector.getAveragePatternLength());
if (memoryWatch != null) {
memoryWatch.interrupt();
additionalCounters.put("maxUsedMemory", memoryWatch.getMaxUsedMemory());
}
System.err.println(miner.toString(additionalCounters));
}
/**
* Parse command-line arguments to instantiate the right collector
*
* @param nbThreads
* @param itemsIdMap can be null when not needed
*/
private static PatternsCollector instanciateCollector(CommandLine cmd, String outputPath,
int nbThreads, Map<Integer, String> itemsIdMap) {
PatternsCollector collector = null;
if (cmd.hasOption('b')) {
collector = new NullCollector();
} else {
PatternsWriter writer = null;
if (outputPath != null) {
try {
if (nbThreads > 1) {
if (itemsIdMap == null) {
writer = new MultiThreadedFileCollector(outputPath, nbThreads);
} else {
writer = new MultiThreadedFileCollector(outputPath, nbThreads, itemsIdMap);
}
} else {
if (itemsIdMap == null) {
writer = new FileCollector(outputPath);
} else {
writer = new FileCollectorWithIDMapper(outputPath, itemsIdMap);
}
}
} catch (IOException e) {
e.printStackTrace(System.err);
System.err.println("Aborting mining.");
System.exit(1);
}
} else {
writer = new StdOutCollector();
}
collector = writer;
if (cmd.hasOption('a')) {
collector = new AllFISConverter(writer);
}
if (cmd.hasOption('s')) {
collector = new PatternSortCollector(collector);
}
}
return collector;
}
}