Skip to content

Commit 3f8ba7d

Browse files
committed
option to read matches for a specified number of mips not just one at a time
1 parent fc3d25c commit 3f8ba7d

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

colormipsearch-tools/src/main/java/org/janelia/colormipsearch/cmd/CalculateGradientScoresCmd.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.janelia.colormipsearch.model.FileData;
5757
import org.janelia.colormipsearch.model.ProcessingType;
5858
import org.janelia.colormipsearch.results.GroupedItems;
59+
import org.janelia.colormipsearch.results.ItemsHandling;
5960
import org.janelia.colormipsearch.results.MatchEntitiesGrouping;
6061
import org.slf4j.Logger;
6162
import org.slf4j.LoggerFactory;
@@ -97,6 +98,9 @@ static class CalculateGradientScoresArgs extends AbstractGradientScoresArgs {
9798
arity = 0)
9899
boolean cancelExistingGradientScores = false;
99100

101+
@Parameter(names = {"--mips-matches-read-size", "-mrs" }, description = "Number of MIPs for which matches will be read at once")
102+
int mipsMatchesReadSize = 1;
103+
100104
CalculateGradientScoresArgs(CommonArgs commonArgs) {
101105
super(commonArgs);
102106
}
@@ -267,8 +271,8 @@ List<CDMatchEntity<M, T>> startAllGradientScores() {
267271
int size = maskIdsToProcess.size();
268272

269273
LOG.info("Collect matches to calculate all gradient scores for {} masks: {}", size, CmdUtils.elemsAsShortenString(maskIdsToProcess, 10, m -> m));
270-
List<GroupedItems<M, CDMatchEntity<M, T>>> matchesToBeScoredGroupedByMask = maskIdsToProcess.stream().parallel()
271-
.flatMap(maskId -> getCDMatchesForMaskMipID(cdMatchesReader, maskId).stream())
274+
List<GroupedItems<M, CDMatchEntity<M, T>>> matchesToBeScoredGroupedByMask = ItemsHandling.partitionCollection(maskIdsToProcess, args.mipsMatchesReadSize).entrySet().stream()
275+
.flatMap(indexedPartition -> getCDMatchesForMaskMipIDs(cdMatchesReader, indexedPartition.getValue()).stream())
272276
.collect(Collectors.toList());
273277

274278
long nMatches = matchesToBeScoredGroupedByMask.stream()
@@ -316,9 +320,9 @@ List<CDMatchEntity<M, T>> startAllGradientScores() {
316320
}
317321

318322
private <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity>
319-
List<GroupedItems<M, CDMatchEntity<M, T>>> getCDMatchesForMaskMipID(NeuronMatchesReader<CDMatchEntity<M, T>> cdsMatchesReader, String maskCDMipId) {
323+
List<GroupedItems<M, CDMatchEntity<M, T>>> getCDMatchesForMaskMipIDs(NeuronMatchesReader<CDMatchEntity<M, T>> cdsMatchesReader, Collection<String> maskCDMipIds) {
320324
long startTime = System.currentTimeMillis();
321-
LOG.info("Read all color depth matches for {}", maskCDMipId);
325+
LOG.info("Read all color depth matches for {} mips: {}", maskCDMipIds.size(), maskCDMipIds);
322326
ScoresFilter neuronsMatchScoresFilter = new ScoresFilter();
323327
if (args.pctPositivePixels > 0) {
324328
neuronsMatchScoresFilter.addSScore("matchingPixelsRatio", args.pctPositivePixels / 100);
@@ -327,7 +331,7 @@ List<GroupedItems<M, CDMatchEntity<M, T>>> getCDMatchesForMaskMipID(NeuronMatche
327331
args.alignmentSpace,
328332
new DataSourceParam()
329333
.setAlignmentSpace(args.alignmentSpace)
330-
.addMipID(maskCDMipId)
334+
.addMipIDs(maskCDMipIds)
331335
.addDatasets(args.maskDatasets)
332336
.addTags(args.maskTags)
333337
.addAnnotations(args.maskAnnotations)
@@ -350,23 +354,28 @@ List<GroupedItems<M, CDMatchEntity<M, T>>> getCDMatchesForMaskMipID(NeuronMatche
350354
/*from*/0,
351355
/*nRecords*/-1,
352356
/*readPageSize*/0);
353-
LOG.debug("Found {} color depth matches for {} in {}ms",
354-
allCDMatches.size(), maskCDMipId, System.currentTimeMillis() - startTime);
357+
LOG.debug("Found {} color depth matches for {} mips in {}ms",
358+
allCDMatches.size(), maskCDMipIds.size(), System.currentTimeMillis() - startTime);
355359
if (args.cancelExistingGradientScores) {
356360
allCDMatches.forEach(CDMatchEntity::resetGradientScores);
357361
long nScoresUpdated = resetShapeScores(allCDMatches);
358-
LOG.debug("Reset gradient {} scores for {} matches for {} in {}ms",
359-
nScoresUpdated, allCDMatches.size(), maskCDMipId, System.currentTimeMillis() - startTime);
362+
LOG.info("Reset gradient {} scores for {} matches for {} MIPs in {}ms",
363+
nScoresUpdated, allCDMatches.size(), maskCDMipIds.size(), System.currentTimeMillis() - startTime);
360364
}
361-
// select best matches to process
362-
List<CDMatchEntity<M, T>> bestMatches = ColorMIPProcessUtils.selectBestMatches(
363-
allCDMatches,
364-
args.numberOfBestLines,
365-
args.numberOfBestSamplesPerLine,
366-
args.numberOfBestMatchesPerSample
367-
);
368-
logBestMatches(maskCDMipId, bestMatches, allCDMatches);
369-
return MatchEntitiesGrouping.groupMatchesByMaskID(bestMatches);
365+
List<GroupedItems<M, CDMatchEntity<M, T>>> allCDMatchesByMaskId = MatchEntitiesGrouping.groupMatchesByMaskID(allCDMatches);
366+
allCDMatchesByMaskId.forEach(maskMatches -> {
367+
// select best matches to process
368+
List<CDMatchEntity<M, T>> bestMaskMatches = ColorMIPProcessUtils.selectBestMatches(
369+
maskMatches.getItems(),
370+
args.numberOfBestLines,
371+
args.numberOfBestSamplesPerLine,
372+
args.numberOfBestMatchesPerSample
373+
);
374+
logBestMatches(maskMatches.getKey().getEntityId(), bestMaskMatches, maskMatches.getItems());
375+
// only leave the best matches for processing
376+
maskMatches.setItems(bestMaskMatches);
377+
});
378+
return allCDMatchesByMaskId;
370379
}
371380

372381
/**
@@ -395,12 +404,14 @@ private <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity> NeuronM
395404
}
396405
}
397406

398-
private <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity> void logBestMatches(String maskMipID,
407+
private <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity> void logBestMatches(Number maskID,
399408
List<CDMatchEntity<M, T>> bestMaskCDMatches,
400409
List<CDMatchEntity<M, T>> allMaskCDMatches) {
401410
if (LOG.isDebugEnabled()) {
402411
// log best lines
403-
String maskName = bestMaskCDMatches.stream().findFirst().map(m -> m.getMaskImage().getPublishedName()).orElse(maskMipID + "(no matches)");
412+
String maskName = bestMaskCDMatches.stream().findFirst()
413+
.map(m -> String.format("%s:%s:%s", m.getMaskImage().getPublishedName(), m.getMaskImage().getMipId(), m.getMaskImageRefId()))
414+
.orElse(maskID + "(no matches)");
404415
Map<String, Set<String>> targetSamplesByPublishedNames = bestMaskCDMatches.stream()
405416
.collect(Collectors.groupingBy(
406417
m -> m.getMatchedImage().getPublishedName(),
@@ -421,7 +432,7 @@ private <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity> void lo
421432
totalSamplesCount, lineWithMaxSamples, maxSamplesCount
422433
);
423434
} else {
424-
LOG.info("Selected {} best color depth matches for {} out of {} total matches", bestMaskCDMatches.size(), maskMipID, allMaskCDMatches.size());
435+
LOG.info("Selected {} best color depth matches for {} out of {} total matches", bestMaskCDMatches.size(), maskID, allMaskCDMatches.size());
425436
}
426437
}
427438

colormipsearch-tools/src/main/java/org/janelia/colormipsearch/cmd/cdsprocess/ColorMIPProcessUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import org.janelia.colormipsearch.results.ScoredEntry;
1010

1111
public class ColorMIPProcessUtils {
12-
public static <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity> List<CDMatchEntity<M, T>> selectBestMatches(List<CDMatchEntity<M, T>> CDMatches,
12+
public static <M extends AbstractNeuronEntity, T extends AbstractNeuronEntity> List<CDMatchEntity<M, T>> selectBestMatches(List<CDMatchEntity<M, T>> cdMatchEntities,
1313
int topLineMatches,
1414
int topSamplesPerLine,
1515
int topMatchesPerSample) {
1616
List<ScoredEntry<List<CDMatchEntity<M, T>>>> topRankedLineMatches = ItemsHandling.selectTopRankedElements(
17-
CDMatches,
17+
cdMatchEntities,
1818
match -> match.getMatchedImage().getPublishedName(), // group by published name
1919
CDMatchEntity::getMatchingPixels, // use pixel matching score
2020
topLineMatches,

0 commit comments

Comments
 (0)