Search in sources :

Example 1 with DiscreteUniformSampler

use of org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler in project GDSC-SMLM by aherbert.

the class CreateData method validateCameraOptions.

private void validateCameraOptions() {
    final CameraType cameraType = settings.getCameraType();
    final boolean isCcd = CalibrationProtosHelper.isCcdCameraType(cameraType);
    if (isCcd) {
        if (cameraType == CameraType.EMCCD) {
            ParameterUtils.isPositive("EM gain", settings.getEmGain());
        }
        ParameterUtils.isPositive("Camera gain", settings.getCameraGain());
        ParameterUtils.isPositive("Read noise", settings.getReadNoise());
        final double noiseRange = settings.getReadNoise() * settings.getCameraGain() * 4;
        ParameterUtils.isEqualOrAbove("Bias must prevent clipping the read noise (@ +/- 4 StdDev) so ", settings.getBias(), noiseRange);
        cameraModel = createCcdCameraModel();
    } else if (cameraType == CameraType.SCMOS) {
        // Load the model
        cameraModel = CameraModelManager.load(settings.getCameraModelName());
        if (cameraModel == null) {
            throw new IllegalArgumentException("Unknown camera model for name: " + settings.getCameraModelName());
        }
        // Check the width is above the selected size
        Rectangle modelBounds = cameraModel.getBounds();
        final int size = settings.getSize();
        if (modelBounds.width < size || modelBounds.height < size) {
            throw new IllegalArgumentException(String.format("Camera model bounds [x=%d,y=%d,width=%d,height=%d] are smaller than " + "simulation size [%d]", modelBounds.x, modelBounds.y, modelBounds.width, modelBounds.height, size));
        }
        // Ask for a crop
        if (modelBounds.width > size || modelBounds.height > size) {
            final GenericDialog gd = new GenericDialog(TITLE);
            // @formatter:off
            ImageJUtils.addMessage(gd, "WARNING:\n \nCamera model bounds\n[x=%d,y=%d,width=%d,height=%d]\n" + "are larger than the simulation size [=%d].\n \nCrop the model?", modelBounds.x, modelBounds.y, modelBounds.width, modelBounds.height, size);
            // @formatter:on
            gd.addCheckbox("Random_crop", settings.getRandomCrop());
            final int upperx = modelBounds.x + modelBounds.width - size;
            final int uppery = modelBounds.y + modelBounds.height - size;
            gd.addSlider("Origin_x", modelBounds.x, upperx, MathUtils.clip(modelBounds.x, upperx, settings.getOriginX()));
            gd.addSlider("Origin_y", modelBounds.y, uppery, MathUtils.clip(modelBounds.y, uppery, settings.getOriginY()));
            gd.addHelp(HelpUrls.getUrl(helpKey));
            gd.showDialog();
            if (gd.wasCanceled()) {
                throw new IllegalArgumentException("Unknown camera model crop");
            }
            settings.setRandomCrop(gd.getNextBoolean());
            settings.setOriginX((int) gd.getNextNumber());
            settings.setOriginY((int) gd.getNextNumber());
            SettingsManager.writeSettings(settings.build());
            int ox;
            int oy;
            if (settings.getRandomCrop()) {
                final UniformRandomProvider rng = createRandomGenerator();
                ox = new DiscreteUniformSampler(rng, modelBounds.x, upperx).sample();
                oy = new DiscreteUniformSampler(rng, modelBounds.y, uppery).sample();
            } else {
                ox = settings.getOriginX();
                oy = settings.getOriginY();
            }
            final Rectangle bounds = new Rectangle(ox, oy, size, size);
            cameraModel = cameraModel.crop(bounds, false);
            modelBounds = cameraModel.getBounds();
            if (modelBounds.width != size || modelBounds.height != size) {
                throw new IllegalArgumentException("Failed to crop camera model to bounds: " + bounds);
            }
        }
    } else {
        throw new IllegalArgumentException("Unsupported camera type: " + CalibrationProtosHelper.getName(cameraType));
    }
}
Also used : DiscreteUniformSampler(org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler) GenericDialog(ij.gui.GenericDialog) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) Rectangle(java.awt.Rectangle) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider) CameraType(uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType)

Example 2 with DiscreteUniformSampler

use of org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler in project GDSC-SMLM by aherbert.

the class NucleusMask method createMask.

private void createMask() {
    diameter = settings.getDiameter();
    nmPerPixel = settings.getNmPerPixel();
    nmPerSlice = settings.getNmPerSlice();
    // Create the dimensions using the scale.
    // Scale diameter in um to nm
    final int radius = (int) Math.ceil(diameter * 500 / nmPerPixel);
    final int radiusz = (int) Math.ceil(diameter * 500 / nmPerSlice);
    final int inc = 2 * radius + 1;
    final int incz = 2 * radiusz + 1;
    final int maxx = settings.getFieldWidth();
    final int maxy = maxx;
    final int ditherHeight = (settings.getYDither() > 0) ? (int) Math.ceil(settings.getYDither() * 1000 / nmPerPixel) : 0;
    final int ditherDepth = (settings.getZDither() > 0) ? (int) Math.ceil(settings.getZDither() * 1000 / nmPerSlice) : 0;
    final int maxz = ditherDepth + incz;
    final ImageStack stack = new ImageStack(maxx, maxy, maxz);
    byte[] mask = new byte[maxx * maxy];
    for (int z = 0; z < maxz; z++) {
        mask = (z == 0) ? mask : mask.clone();
        stack.setPixels(mask, z + 1);
    }
    if (settings.getMode() == 0) {
        final ImageStack stack2 = createEllipsoid(inc, inc, incz);
        // Dither
        int cx = radius;
        final int lowerz = (maxz - ditherDepth) / 2;
        final int upperz = (maxz + ditherDepth) / 2;
        final UniformRandomProvider rng = UniformRandomProviders.create();
        final DiscreteUniformSampler ditherSampler = new DiscreteUniformSampler(rng, 0, ditherHeight);
        final DiscreteUniformSampler zSampler = new DiscreteUniformSampler(rng, lowerz, upperz);
        while (cx < maxx) {
            final int xloc = cx - radius;
            int cy = radius + ditherSampler.sample();
            while (cy < maxy) {
                final int yloc = cy - radius;
                final int offset = zSampler.sample() - radiusz;
                for (int slice = 1; slice <= stack2.getSize(); slice++) {
                    final int i = slice + offset;
                    if (i < 1 || i > maxz) {
                        continue;
                    }
                    final ImageProcessor ip = stack.getProcessor(i);
                    final ImageProcessor ip2 = stack2.getProcessor(slice);
                    ip.copyBits(ip2, xloc, yloc, Blitter.MAX);
                }
                cy += inc + 1 + ditherSampler.sample();
            }
            cx += inc + 1;
        }
    }
    // The final image will have a scale added to it.
    imp = ImageJUtils.display(TITLE, stack);
    calibrate(imp);
    imp.setSlice(maxz / 2);
    if (settings.getMode() == 1) {
        // Allow mouse click to draw spheres
        final MouseAdapter ml = createMouseAdapter();
        imp.getCanvas().addMouseListener(ml);
        final NonBlockingExtendedGenericDialog gd = new NonBlockingExtendedGenericDialog(TITLE);
        gd.addHelp(HelpUrls.getUrl("nucleus-mask"));
        gd.addMessage("Click the image to add a sphere");
        gd.addNumericField("Diameter", diameter, 2, 6, "um");
        gd.addDialogListener(this::dialogItemChanged);
        gd.hideCancelButton();
        gd.setOKLabel("Close");
        gd.addHelp(HelpUrls.getUrl("nucleus-mask"));
        gd.showDialog();
        imp.getCanvas().removeMouseListener(ml);
        if (diameter != settings.getDiameter()) {
            settings.setDiameter(diameter);
            SettingsManager.writeSettings(settings);
        }
    }
}
Also used : DiscreteUniformSampler(org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler) ImageProcessor(ij.process.ImageProcessor) ImageStack(ij.ImageStack) MouseAdapter(java.awt.event.MouseAdapter) NonBlockingExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog) UniformRandomProvider(org.apache.commons.rng.UniformRandomProvider)

Aggregations

UniformRandomProvider (org.apache.commons.rng.UniformRandomProvider)2 DiscreteUniformSampler (org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler)2 ImageStack (ij.ImageStack)1 GenericDialog (ij.gui.GenericDialog)1 ImageProcessor (ij.process.ImageProcessor)1 Rectangle (java.awt.Rectangle)1 MouseAdapter (java.awt.event.MouseAdapter)1 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)1 NonBlockingExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.NonBlockingExtendedGenericDialog)1 CameraType (uk.ac.sussex.gdsc.smlm.data.config.CalibrationProtos.CameraType)1