Search in sources :

Example 1 with TaskFactory

use of ini.trakem2.parallel.TaskFactory in project TrakEM2 by trakem2.

the class ContrastEnhancerWrapper method apply.

public boolean apply(final Collection<Displayable> patches_) {
    if (null == patches_)
        return false;
    // Create appropriate patch list
    ArrayList<Patch> patches = new ArrayList<Patch>();
    for (final Displayable d : patches_) {
        if (d.getClass() == Patch.class)
            patches.add((Patch) d);
    }
    if (0 == patches.size())
        return false;
    // Check that all images are of the same size and type
    Patch firstp = (Patch) patches.get(0);
    final int ptype = firstp.getType();
    final double pw = firstp.getOWidth();
    final double ph = firstp.getOHeight();
    for (final Patch p : patches) {
        if (p.getType() != ptype) {
            // can't continue
            Utils.log("Can't homogenize histograms: images are not all of the same type.\nFirst offending image is: " + p);
            return false;
        }
        if (!equalize && 0 == stats_mode && p.getOWidth() != pw || p.getOHeight() != ph) {
            Utils.log("Can't homogenize histograms: images are not all of the same size.\nFirst offending image is: " + p);
            return false;
        }
    }
    try {
        if (equalize) {
            for (final Patch p : patches) {
                if (Thread.currentThread().isInterrupted())
                    return false;
                p.appendFilters(new IFilter[] { new EqualizeHistogram() });
                /*
					p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3);
					ImageProcessor ip = p.getImageProcessor().duplicate(); // a throw-away copy
					if (this.from_existing_min_and_max) {
						ip.setMinAndMax(p.getMin(), p.getMax());
					}
					ce.equalize(ip);
					p.setMinAndMax(ip.getMin(), ip.getMax());
					*/
                // submit for regeneration
                p.getProject().getLoader().decacheImagePlus(p.getId());
                regenerateMipMaps(p);
            }
            return true;
        }
        // Else, call stretchHistogram with an appropriate stats object
        final ImageStatistics stats;
        if (1 == stats_mode) {
            // use each image independent stats
            stats = null;
        } else if (0 == stats_mode) {
            // use stack statistics
            final ArrayList<Patch> sub = new ArrayList<Patch>();
            if (use_full_stack) {
                sub.addAll(patches);
            } else {
                // build stack statistics, ordered by stdDev
                final SortedMap<Stats, Patch> sp = Collections.synchronizedSortedMap(new TreeMap<Stats, Patch>());
                Process.progressive(patches, new TaskFactory<Patch, Stats>() {

                    public Stats process(final Patch p) {
                        if (Thread.currentThread().isInterrupted())
                            return null;
                        ImagePlus imp = p.getImagePlus();
                        p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 2);
                        Stats s = new Stats(imp.getStatistics());
                        sp.put(s, p);
                        return s;
                    }
                });
                if (Thread.currentThread().isInterrupted())
                    return false;
                final ArrayList<Patch> a = new ArrayList<Patch>(sp.values());
                final int count = a.size();
                if (count < 3) {
                    sub.addAll(a);
                } else if (3 == count) {
                    // the middle one
                    sub.add(a.get(1));
                } else if (4 == count) {
                    sub.addAll(a.subList(1, 3));
                } else if (count > 4) {
                    int first = (int) (count / 4.0 + 0.5);
                    int last = (int) (count / 4.0 * 3 + 0.5);
                    sub.addAll(a.subList(first, last));
                }
            }
            stats = new StackStatistics(new PatchStack(sub.toArray(new Patch[sub.size()]), 1));
        } else {
            stats = reference_stats;
        }
        final Calibration cal = patches.get(0).getLayer().getParent().getCalibrationCopy();
        Process.progressive(patches, new TaskFactory<Patch, Object>() {

            public Object process(final Patch p) {
                if (Thread.currentThread().isInterrupted())
                    return null;
                p.getProject().getLoader().releaseToFit(p.getOWidth(), p.getOHeight(), p.getType(), 3);
                // a throw-away copy
                ImageProcessor ip = p.getImageProcessor().duplicate();
                if (ContrastEnhancerWrapper.this.from_existing_min_and_max) {
                    ip.setMinAndMax(p.getMin(), p.getMax());
                }
                ImageStatistics st = stats;
                if (null == stats) {
                    Utils.log2("Null stats, using image's self");
                    st = ImageStatistics.getStatistics(ip, Measurements.MIN_MAX, cal);
                }
                ce.stretchHistogram(ip, saturated, st);
                // This is all we care about from stretching the histogram:
                p.setMinAndMax(ip.getMin(), ip.getMax());
                regenerateMipMaps(p);
                return null;
            }
        });
    } catch (Exception e) {
        IJError.print(e);
        return false;
    }
    return true;
}
Also used : Displayable(ini.trakem2.display.Displayable) ArrayList(java.util.ArrayList) StackStatistics(ij.process.StackStatistics) Calibration(ij.measure.Calibration) TreeMap(java.util.TreeMap) ImagePlus(ij.ImagePlus) ImageProcessor(ij.process.ImageProcessor) ImageStatistics(ij.process.ImageStatistics) SortedMap(java.util.SortedMap) TaskFactory(ini.trakem2.parallel.TaskFactory) EqualizeHistogram(ini.trakem2.imaging.filters.EqualizeHistogram) Patch(ini.trakem2.display.Patch)

Example 2 with TaskFactory

use of ini.trakem2.parallel.TaskFactory in project TrakEM2 by trakem2.

the class AbstractAffineTile2D method pairOverlappingTiles.

/**
 * Search a {@link List} of {@link AbstractAffineTile2D Tiles} for
 * overlapping pairs.  Adds the pairs into tilePairs.
 *
 * @param tiles
 * @param tilePairs
 */
public static final <AAT extends AbstractAffineTile2D<?>> void pairOverlappingTiles(final List<AAT> tiles, final List<AbstractAffineTile2D<?>[]> tilePairs, final boolean sloppyOverlapTest) {
    final HashSet<Patch> visited = new HashSet<Patch>();
    final ArrayList<AbstractAffineTile2D<?>[]> tilePairCandidates = new ArrayList<AbstractAffineTile2D<?>[]>();
    /* LUT for tiles */
    final Hashtable<Patch, AAT> lut = new Hashtable<Patch, AAT>();
    for (final AAT tile : tiles) lut.put(tile.patch, tile);
    for (int a = 0; a < tiles.size(); ++a) {
        final AbstractAffineTile2D<?> ta = tiles.get(a);
        final Patch pa = ta.patch;
        visited.add(pa);
        final Layer la = pa.getLayer();
        for (final Displayable d : la.getDisplayables(Patch.class, pa.getBoundingBox())) {
            final Patch pb = (Patch) d;
            if (visited.contains(pb))
                continue;
            final AAT tb = lut.get(pb);
            if (tb == null)
                continue;
            tilePairCandidates.add(new AbstractAffineTile2D<?>[] { ta, tb });
        }
    }
    if (sloppyOverlapTest)
        tilePairs.addAll(tilePairCandidates);
    else {
        // TODO Fix this and use what the user wants to provide
        final ExecutorService exec = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        final ArrayList<Future<?>> futures = new ArrayList<Future<?>>();
        for (final AbstractAffineTile2D<?>[] tatb : tilePairCandidates) {
            futures.add(exec.submit(new Runnable() {

                @Override
                public void run() {
                    if (tatb[0].intersects(tatb[1]))
                        synchronized (tilePairs) {
                            tilePairs.add(tatb);
                        }
                }
            }));
        }
        try {
            for (final Future<?> f : futures) f.get();
        } catch (final InterruptedException e) {
            e.printStackTrace();
        } catch (final ExecutionException e) {
            e.printStackTrace();
        } finally {
            exec.shutdown();
        }
    }
// // 1. Precompute the Area of each tile's Patch
// final HashMap< Patch, Pair< AAT, Area > > m = new HashMap< Patch, Pair< AAT, Area > >();
// // A lazy collection of pairs, computed in parallel ahead of consumption
// final Iterable< Pair< AAT, Area > > ts =
// new ParallelMapping< AAT, Pair< AAT, Area > >(
// tiles,
// new TaskFactory< AAT, Pair< AAT, Area > >() {
// @Override
// public Pair< AAT, Area > process( final AAT tile ) {
// return new Pair< AAT, Area >( tile, tile.patch.getArea() );
// }
// }
// );
// for ( final Pair< AAT, Area > t : ts) {
// m.put( t.a.patch, t );
// }
// 
// // 2. Obtain the list of tile pairs, at one list per tile
// final Iterable< List<AbstractAffineTile2D< ? >[]> > pairsList =
// new ParallelMapping< AAT, List<AbstractAffineTile2D< ? >[]> >(
// tiles,
// new TaskFactory< AAT, List<AbstractAffineTile2D< ? >[]> >() {
// @Override
// public List<AbstractAffineTile2D< ? >[]> process( final AAT ta ) {
// final Area a;
// synchronized (m) {
// a = m.get( ta.patch ).b;
// }
// final ArrayList<AbstractAffineTile2D< ? >[]> seq = new ArrayList<AbstractAffineTile2D< ? >[]>();
// for ( final Patch p : ta.patch.getLayer().getIntersecting( ta.patch, Patch.class ) ) {
// if ( p == ta.patch )
// continue;
// final Pair< AAT, Area > pair;
// synchronized (m) {
// pair =  m.get(p);
// }
// // Check that the Patch is among those to consider in the alignment
// if ( null != pair ) {
// // Check that the Patch visible pixels overlap -- may not if it has an alpha mask or coordinate transform
// if ( M.intersects( a, pair.b ) ) {
// seq.add( new AbstractAffineTile2D< ? >[]{ ta, pair.a });
// }
// }
// }
// return seq;
// }
// }
// );
// 
// for (final List<AbstractAffineTile2D<?>[]> list: pairsList) {
// tilePairs.addAll(list);
// }
}
Also used : Displayable(ini.trakem2.display.Displayable) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) Layer(ini.trakem2.display.Layer) Point(mpicbg.models.Point) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Patch(ini.trakem2.display.Patch) HashSet(java.util.HashSet)

Example 3 with TaskFactory

use of ini.trakem2.parallel.TaskFactory in project TrakEM2 by trakem2.

the class Merger method compare.

/**
 * Take two projects and find out what is different among them,
 *  independent of id.
 */
public static final void compare(final Project p1, final Project p2) {
    Utils.log("Be warned: only Treeline, AreaTree and Connector are considered at the moment.");
    final LayerSet ls1 = p1.getRootLayerSet(), ls2 = p2.getRootLayerSet();
    final Collection<ZDisplayable> zds1 = ls1.getZDisplayables(), zds2 = ls2.getZDisplayables();
    final HashSet<Class<?>> accepted = new HashSet<Class<?>>();
    accepted.add(Treeline.class);
    accepted.add(AreaTree.class);
    accepted.add(Connector.class);
    final HashMap<Displayable, List<Change>> matched = new HashMap<Displayable, List<Change>>();
    final HashSet<ZDisplayable> empty1 = new HashSet<ZDisplayable>(), empty2 = new HashSet<ZDisplayable>();
    final HashSet<ZDisplayable> unmatched1 = new HashSet<ZDisplayable>(), unmatched2 = new HashSet<ZDisplayable>(zds2);
    // Remove instances of classes not accepted
    for (final Iterator<ZDisplayable> it = unmatched2.iterator(); it.hasNext(); ) {
        ZDisplayable zd = it.next();
        if (!accepted.contains(zd.getClass())) {
            it.remove();
            continue;
        }
        if (zd.isDeletable()) {
            it.remove();
            empty2.add(zd);
        }
    }
    zds2.removeAll(empty2);
    final AtomicInteger counter = new AtomicInteger(0);
    // or at least one or more that are similar in that they have some nodes in common.
    try {
        ini.trakem2.parallel.Process.unbound(zds1, new TaskFactory<ZDisplayable, Object>() {

            @Override
            public Object process(final ZDisplayable zd1) {
                Utils.showProgress(counter.getAndIncrement() / (float) zds1.size());
                if (!accepted.contains(zd1.getClass())) {
                    Utils.log("Ignoring: [A] " + zd1);
                    return null;
                }
                if (zd1.isDeletable()) {
                    synchronized (empty1) {
                        empty1.add(zd1);
                    }
                    return null;
                }
                final List<Change> cs = new ArrayList<Change>();
                for (final ZDisplayable zd2 : zds2) {
                    // Same class?
                    if (zd1.getClass() != zd2.getClass())
                        continue;
                    if (zd1 instanceof Tree<?> && zd2 instanceof Tree<?>) {
                        Change c = compareTrees(zd1, zd2);
                        if (c.hasSimilarNodes()) {
                            cs.add(c);
                            if (1 == cs.size()) {
                                synchronized (matched) {
                                    matched.put(zd1, cs);
                                }
                            }
                            synchronized (unmatched2) {
                                unmatched2.remove(zd2);
                            }
                        }
                        // debug
                        if (zd1.getId() == zd2.getId()) {
                            Utils.log("zd1 #" + zd1.getId() + " is similar to #" + zd2.getId() + ": " + c.hasSimilarNodes());
                        }
                    }
                }
                if (cs.isEmpty()) {
                    synchronized (unmatched1) {
                        unmatched1.add(zd1);
                    }
                }
                return null;
            }
        });
    } catch (Exception e) {
        IJError.print(e);
    }
    // reset
    Utils.showProgress(1);
    Utils.log("matched.size(): " + matched.size());
    makeGUI(p1, p2, empty1, empty2, matched, unmatched1, unmatched2);
}
Also used : ZDisplayable(ini.trakem2.display.ZDisplayable) Displayable(ini.trakem2.display.Displayable) LayerSet(ini.trakem2.display.LayerSet) HashMap(java.util.HashMap) ZDisplayable(ini.trakem2.display.ZDisplayable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Tree(ini.trakem2.display.Tree) AreaTree(ini.trakem2.display.AreaTree) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

Displayable (ini.trakem2.display.Displayable)3 ArrayList (java.util.ArrayList)3 Patch (ini.trakem2.display.Patch)2 HashSet (java.util.HashSet)2 ImagePlus (ij.ImagePlus)1 Calibration (ij.measure.Calibration)1 ImageProcessor (ij.process.ImageProcessor)1 ImageStatistics (ij.process.ImageStatistics)1 StackStatistics (ij.process.StackStatistics)1 AreaTree (ini.trakem2.display.AreaTree)1 Layer (ini.trakem2.display.Layer)1 LayerSet (ini.trakem2.display.LayerSet)1 Tree (ini.trakem2.display.Tree)1 ZDisplayable (ini.trakem2.display.ZDisplayable)1 EqualizeHistogram (ini.trakem2.imaging.filters.EqualizeHistogram)1 TaskFactory (ini.trakem2.parallel.TaskFactory)1 HashMap (java.util.HashMap)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 SortedMap (java.util.SortedMap)1