-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathPointMatchClient.java
More file actions
597 lines (472 loc) · 22.6 KB
/
PointMatchClient.java
File metadata and controls
597 lines (472 loc) · 22.6 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
package org.janelia.render.client;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParametersDelegate;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import mpicbg.imagefeatures.Feature;
import mpicbg.imagefeatures.FloatArray2DSIFT;
import org.janelia.alignment.RenderParameters;
import org.janelia.alignment.match.CanvasFeatureExtractor;
import org.janelia.alignment.match.CanvasFeatureMatcher;
import org.janelia.alignment.match.CanvasId;
import org.janelia.alignment.match.CanvasIdWithRenderContext;
import org.janelia.alignment.match.CanvasMatchResult;
import org.janelia.alignment.match.CanvasMatches;
import org.janelia.alignment.match.MontageRelativePosition;
import org.janelia.alignment.match.parameters.FeatureExtractionParameters;
import org.janelia.alignment.match.parameters.FeatureRenderClipParameters;
import org.janelia.alignment.match.parameters.MatchDerivationParameters;
import org.janelia.alignment.spec.LayoutData;
import org.janelia.alignment.spec.TileSpec;
import org.janelia.render.client.parameter.CommandLineParameters;
import org.janelia.render.client.parameter.MatchWebServiceParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Java client for generating SIFT point matches for one or more canvas (e.g. tile) pairs.
*
* @author Eric Trautman
*/
public class PointMatchClient {
public enum CanvasGroupIdAlgorithm {
/** Assign canvas group id based upon the sectionId value of the first rendered tile. */
FIRST_TILE_SECTION_ID,
/** Assign canvas group id based upon the z value of the first rendered tile. */
FIRST_TILE_Z,
/** Assign canvas group id based upon the match collection name. */
COLLECTION
}
public enum CanvasIdAlgorithm {
/** Assign canvas id based upon the id of the first rendered tile. */
FIRST_TILE_ID,
/** Assign canvas id based upon the z value of the first rendered tile. */
FIRST_TILE_Z,
/** Assign canvas id based upon the derived canvas name (e.g. c_00001). */
CANVAS_NAME
}
public enum RenderFileFormat {
JPG,
PNG,
TIF
}
public static class Parameters extends CommandLineParameters {
@ParametersDelegate
MatchWebServiceParameters matchClient = new MatchWebServiceParameters();
@ParametersDelegate
public MatchDerivationParameters match = new MatchDerivationParameters();
@ParametersDelegate
FeatureExtractionParameters featureExtraction = new FeatureExtractionParameters();
@ParametersDelegate
FeatureRenderClipParameters clip = new FeatureRenderClipParameters();
@Parameter(
names = "--firstCanvasPosition",
description = "When clipping, identifies the relative position of the first canvas to the second canvas"
)
public MontageRelativePosition firstCanvasPosition;
@Parameter(
names = "--renderScale",
description = "Render canvases at this scale"
)
public Double renderScale = 1.0;
@Parameter(names = "--fillWithNoise",
description = "Fill each canvas image with noise before rendering to improve point match derivation",
arity = 1)
public boolean fillWithNoise = true;
@Parameter(
names = "--renderFileFormat",
description = "Format for saved canvases (only relevant if debugDirectory is specified)"
)
public RenderFileFormat renderFileFormat = RenderFileFormat.JPG;
@Parameter(
names = "--numberOfThreads",
description = "Number of threads to use for processing"
)
public int numberOfThreads = 1;
@Parameter(
names = "--matchStorageFile",
description = "File to store matches (omit if matches should be stored through web service)"
)
public String matchStorageFile = null;
@Parameter(
names = "--canvasGroupIdAlgorithm",
description = "Algorithm for deriving canvas group ids"
)
public CanvasGroupIdAlgorithm canvasGroupIdAlgorithm = CanvasGroupIdAlgorithm.FIRST_TILE_SECTION_ID;
@Parameter(
names = "--canvasIdAlgorithm",
description = "Algorithm for deriving canvas ids"
)
public CanvasIdAlgorithm canvasIdAlgorithm = CanvasIdAlgorithm.FIRST_TILE_ID;
@Parameter(
names = "--debugDirectory",
description = "Directory to save rendered canvases for debugging (omit to keep rendered data in memory only)"
)
public String debugDirectory = null;
File validatedDebugDirectory = null;
@Parameter(
description = "canvas_1_URL canvas_2_URL [canvas_p_URL canvas_q_URL] ... (each URL pair identifies render parameters for canvas pairs)",
required = true)
public List<String> renderParameterUrls;
/**
* @param matchId derived match id for canvas.
*
* @return file for debug save of rendered canvas.
*/
File getCanvasFile(final String matchId) {
File canvasFile = null;
if (validatedDebugDirectory != null) {
canvasFile = new File(validatedDebugDirectory,
matchId + "." + renderFileFormat.toString().toLowerCase());
}
return canvasFile;
}
/**
* @param renderParameters render parameters used to generate the current canvas.
*
* @return match group id derived using the {@link #canvasGroupIdAlgorithm}.
*/
String getCanvasGroupId(final RenderParameters renderParameters) {
String matchGroupId = null;
if (renderParameters.hasTileSpecs()) {
switch (canvasGroupIdAlgorithm) {
case FIRST_TILE_SECTION_ID:
matchGroupId = getTileSectionId(renderParameters.getTileSpecs().get(0), matchClient.collection);
break;
case FIRST_TILE_Z:
matchGroupId = getTileZId(renderParameters.getTileSpecs().get(0), matchClient.collection);
break;
}
}
if (matchGroupId == null) {
matchGroupId = matchClient.collection;
}
return matchGroupId;
}
/**
* @param renderParameters render parameters used to generate the current canvas.
* @param canvasName index based name for the current canvas (e.g. c_00001).
*
* @return match id derived using the {@link #canvasIdAlgorithm}.
*/
String getCanvasId(final RenderParameters renderParameters,
final String canvasName) {
String matchId = null;
if (renderParameters.hasTileSpecs()) {
switch (canvasIdAlgorithm) {
case FIRST_TILE_ID:
matchId = renderParameters.getTileSpecs().get(0).getTileId();
break;
case FIRST_TILE_Z:
matchId = getTileZId(renderParameters.getTileSpecs().get(0), canvasName);
break;
}
}
if (matchId == null) {
matchId = canvasName;
}
return matchId;
}
private String getTileSectionId(final TileSpec tileSpec,
final String defaultValue) {
String sectionId = defaultValue;
final LayoutData layout = tileSpec.getLayout();
if (layout != null) {
sectionId = layout.getSectionId();
}
return sectionId;
}
private String getTileZId(final TileSpec tileSpec,
final String defaultValue) {
String zId = defaultValue;
final Double z = tileSpec.getZ();
if (z != null) {
zId = String.valueOf(z);
if (zId.indexOf('.') == -1) {
zId = zId + ".0";
}
}
return zId;
}
}
/**
* @param args see {@link Parameters} for command line argument details.
*/
public static void main(final String[] args) {
final ClientRunner clientRunner = new ClientRunner(args) {
@Override
public void runClient(final String[] args) throws Exception {
final Parameters parameters = new Parameters();
parameters.parse(args);
LOG.info("runClient: entry, parameters={}", parameters);
final PointMatchClient client = new PointMatchClient(parameters);
client.extractFeatures();
final List<CanvasMatches> canvasMatchesList = client.deriveMatches();
client.saveMatches(canvasMatchesList);
}
};
clientRunner.run();
}
private final Parameters parameters;
private final Map<String, CanvasData> canvasUrlToDataMap;
private final RenderDataClient renderDataClient;
PointMatchClient(final Parameters clientParameters)
throws IllegalArgumentException {
if ((clientParameters.renderParameterUrls.size() % 2) != 0) {
throw new IllegalArgumentException("odd number of canvas URLs specified, URLs must be paired");
}
this.parameters = clientParameters;
if (clientParameters.debugDirectory != null) {
try {
clientParameters.validatedDebugDirectory = new File(clientParameters.debugDirectory).getCanonicalFile();
if (! clientParameters.validatedDebugDirectory.exists()) {
if (! clientParameters.validatedDebugDirectory.mkdirs()) {
throw new IllegalArgumentException(
"failed to create debugDirectory " + clientParameters.validatedDebugDirectory);
}
}
} catch (final IOException e) {
throw new IllegalArgumentException(
"invalid debugDirectory '" + clientParameters.debugDirectory + "' specified", e);
}
}
if (parameters.clip.hasValue()) {
if (clientParameters.renderParameterUrls.size() != 2) {
throw new IllegalArgumentException("clipping is only supported for single pair runs");
}
if (parameters.firstCanvasPosition == null) {
throw new IllegalArgumentException("--firstCanvasPosition must be specified for clipping");
}
}
this.canvasUrlToDataMap = new LinkedHashMap<>(clientParameters.renderParameterUrls.size() * 2);
for (final String canvasUrlString : clientParameters.renderParameterUrls) {
if (! this.canvasUrlToDataMap.containsKey(canvasUrlString)) {
this.canvasUrlToDataMap.put(canvasUrlString, new CanvasData(canvasUrlString,
parameters.renderScale,
canvasUrlToDataMap.size(),
clientParameters));
}
}
this.renderDataClient = new RenderDataClient(clientParameters.matchClient.baseDataUrl,
clientParameters.matchClient.owner,
clientParameters.matchClient.collection);
}
/**
* Extract features from distinct set of canvases.
*/
void extractFeatures() throws Exception {
LOG.info("extractFeatures: entry, extracting from {} canvases", canvasUrlToDataMap.size());
final List<CanvasFeatureExtractorThread> extractorList = new ArrayList<>(canvasUrlToDataMap.size());
for (final String canvasUrl : canvasUrlToDataMap.keySet()) {
extractorList.add(new CanvasFeatureExtractorThread(canvasUrlToDataMap.get(canvasUrl),
parameters));
}
if (parameters.numberOfThreads > 1) {
for (final CanvasFeatureExtractorThread extractorThread : extractorList) {
extractorThread.start();
}
for (final CanvasFeatureExtractorThread extractorThread : extractorList) {
LOG.info("extractFeatures: waiting for {} to finish ...", extractorThread);
extractorThread.join();
}
} else {
for (final CanvasFeatureExtractorThread extractorThread : extractorList) {
//noinspection CallToThreadRun
extractorThread.run();
}
}
LOG.info("extractFeatures: exit");
}
/**
* Derive point matches for each canvas pair and write results.
*/
List<CanvasMatches> deriveMatches() throws Exception {
LOG.info("deriveMatches: entry, extracting from {} canvases", canvasUrlToDataMap.size());
final List<CanvasFeatureMatcherThread> matcherList = new ArrayList<>(parameters.renderParameterUrls.size());
final CanvasFeatureMatcher matcher = new CanvasFeatureMatcher(parameters.match,
parameters.renderScale);
String pUrlString;
String qUrlString;
for (int i = 1; i < parameters.renderParameterUrls.size(); i = i + 2) {
pUrlString = parameters.renderParameterUrls.get(i - 1);
qUrlString = parameters.renderParameterUrls.get(i);
matcherList.add(new CanvasFeatureMatcherThread(canvasUrlToDataMap.get(pUrlString),
canvasUrlToDataMap.get(qUrlString),
matcher));
}
if (parameters.numberOfThreads > 1) {
for (final CanvasFeatureMatcherThread matcherThread : matcherList) {
matcherThread.start();
}
for (final CanvasFeatureMatcherThread matcherThread : matcherList) {
LOG.info("extractFeatures: waiting for {} to finish ...", matcherThread);
matcherThread.join();
}
} else {
for (final CanvasFeatureMatcherThread matcherThread : matcherList) {
//noinspection CallToThreadRun
matcherThread.run();
}
}
final List<CanvasMatches> canvasMatchesList = new ArrayList<>(matcherList.size());
for (final CanvasFeatureMatcherThread matcherThread : matcherList) {
matcherThread.addMatchesToList(canvasMatchesList);
}
LOG.info("deriveMatches: exit");
return canvasMatchesList;
}
private void saveMatches(final List<CanvasMatches> canvasMatchesList) throws Exception {
LOG.info("saveMatches: entry, canvasMatchesList.size={}", canvasMatchesList.size());
final List<CanvasMatches> filteredCanvasMatchesList = new ArrayList<>(canvasMatchesList.size());
for (final CanvasMatches canvasMatches : canvasMatchesList) {
if (canvasMatches.size() > 0) {
filteredCanvasMatchesList.add(canvasMatches);
}
}
final int numberOfPairsWithoutMatches = canvasMatchesList.size() - filteredCanvasMatchesList.size();
if (numberOfPairsWithoutMatches > 0) {
LOG.info("saveMatches: filtered out {} pairs with no matches", numberOfPairsWithoutMatches);
}
if (filteredCanvasMatchesList.size() > 0) {
CanvasMatches canvasMatches;
if (parameters.matchStorageFile != null) {
final Path storagePath = Paths.get(parameters.matchStorageFile);
try (final BufferedWriter writer = Files.newBufferedWriter(storagePath, StandardCharsets.UTF_8)) {
writer.write("[\n");
for (int i = 0; i < filteredCanvasMatchesList.size(); i++) {
if (i > 0) {
writer.write(",\n");
}
canvasMatches = filteredCanvasMatchesList.get(i);
writer.write(canvasMatches.toJson());
}
writer.write("\n]\n");
}
} else {
renderDataClient.saveMatches(filteredCanvasMatchesList);
}
} else {
LOG.info("saveMatches: no pairs have matches so there is nothing to save");
}
LOG.info("saveMatches: exit");
}
/**
* Helper class to hold data (render parameters, features, etc.) for each canvas.
*/
public static class CanvasData {
private final RenderParameters renderParameters;
private final double renderScale;
private final CanvasId canvasId;
private List<Feature> featureList;
CanvasData(final String canvasUrl,
final double renderScale,
final int canvasIndex,
final Parameters clientParameters) {
final MontageRelativePosition relativePosition;
if (clientParameters.clip.hasValue()) {
relativePosition = canvasIndex == 0 ?
clientParameters.firstCanvasPosition :
clientParameters.firstCanvasPosition.getOpposite();
} else {
relativePosition = null;
}
final CanvasId loaderCanvasId = new CanvasId("canvas",
String.valueOf(canvasIndex),
relativePosition);
final CanvasIdWithRenderContext canvasIdWithRenderContext =
new CanvasIdWithRenderContext(loaderCanvasId,
"feature_0",
canvasUrl,
clientParameters.clip.clipWidth,
clientParameters.clip.clipHeight);
this.renderParameters = canvasIdWithRenderContext.loadRenderParameters();
this.renderParameters.setScale(renderScale);
this.renderScale = renderScale;
final String groupId = clientParameters.getCanvasGroupId(this.renderParameters);
final String canvasName = "c_" + String.format("%05d", canvasIndex);
final String id = clientParameters.getCanvasId(this.renderParameters, canvasName);
this.canvasId = new CanvasId(groupId, id, relativePosition);
this.canvasId.setClipOffsets(canvasIdWithRenderContext.getClipOffsets());
this.featureList = null;
}
void setFeatureList(final List<Feature> featureList) {
this.featureList = featureList;
}
@Override
public String toString() {
return canvasId.getGroupId() + "__" + canvasId.getId();
}
}
/**
* Thread wrapper that allows feature extraction to be done in parallel.
*/
private static class CanvasFeatureExtractorThread extends Thread {
private final CanvasData canvasData;
private final File renderFile;
private final CanvasFeatureExtractor extractor;
CanvasFeatureExtractorThread(final CanvasData canvasData,
final Parameters clientParameters) {
this.canvasData = canvasData;
this.renderFile = clientParameters.getCanvasFile(canvasData.canvasId.getId());
final FloatArray2DSIFT.Param siftParameters = new FloatArray2DSIFT.Param();
siftParameters.fdSize = clientParameters.featureExtraction.fdSize;
siftParameters.steps = clientParameters.featureExtraction.steps;
this.extractor = new CanvasFeatureExtractor(siftParameters,
clientParameters.featureExtraction.minScale,
clientParameters.featureExtraction.maxScale);
}
@Override
public void run() {
canvasData.setFeatureList(
extractor.extractFeatures(canvasData.renderParameters,
renderFile));
}
@Override
public String toString() {
return "CanvasFeatureExtractorThread{" + canvasData.canvasId + '}';
}
}
/**
* Thread wrapper that allows match derivation to be done in parallel.
*/
private static class CanvasFeatureMatcherThread extends Thread {
private final CanvasData pCanvasData;
private final CanvasData qCanvasData;
private final CanvasFeatureMatcher matcher;
private CanvasMatchResult matchResult;
CanvasFeatureMatcherThread(final CanvasData pCanvasData,
final CanvasData qCanvasData,
final CanvasFeatureMatcher matcher) {
this.pCanvasData = pCanvasData;
this.qCanvasData = qCanvasData;
this.matcher = matcher;
}
@Override
public void run() {
matchResult = matcher.deriveMatchResult(pCanvasData.featureList, qCanvasData.featureList);
}
void addMatchesToList(final List<CanvasMatches> targetList) {
matchResult.addInlierMatchesToList(pCanvasData.canvasId.getGroupId(),
pCanvasData.canvasId.getId(),
qCanvasData.canvasId.getGroupId(),
qCanvasData.canvasId.getId(),
pCanvasData.renderScale,
pCanvasData.canvasId.getClipOffsets(),
qCanvasData.canvasId.getClipOffsets(),
targetList);
}
@Override
public String toString() {
return "CanvasFeatureMatcherThread{" + pCanvasData.canvasId + "__" + qCanvasData.canvasId + '}';
}
}
private static final Logger LOG = LoggerFactory.getLogger(PointMatchClient.class);
}