5656import org .janelia .colormipsearch .model .FileData ;
5757import org .janelia .colormipsearch .model .ProcessingType ;
5858import org .janelia .colormipsearch .results .GroupedItems ;
59+ import org .janelia .colormipsearch .results .ItemsHandling ;
5960import org .janelia .colormipsearch .results .MatchEntitiesGrouping ;
6061import org .slf4j .Logger ;
6162import 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
0 commit comments