Search in sources :

Example 26 with DefaultScriptableWorkflowStep

use of qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep in project qupath by qupath.

the class ImageDetailsPane method promptToSetPixelSize.

static boolean promptToSetPixelSize(ImageData<BufferedImage> imageData, boolean requestZSpacing) {
    var server = imageData.getServer();
    var hierarchy = imageData.getHierarchy();
    var selected = hierarchy.getSelectionModel().getSelectedObject();
    var roi = selected == null ? null : selected.getROI();
    PixelCalibration cal = server.getPixelCalibration();
    double pixelWidthMicrons = cal.getPixelWidthMicrons();
    double pixelHeightMicrons = cal.getPixelHeightMicrons();
    double zSpacingMicrons = cal.getZSpacingMicrons();
    // Use line or area ROI if possible
    if (!requestZSpacing && roi != null && !roi.isEmpty() && (roi.isArea() || roi.isLine())) {
        boolean setPixelHeight = true;
        boolean setPixelWidth = true;
        String message;
        String units = GeneralTools.micrometerSymbol();
        double pixelWidth = cal.getPixelWidthMicrons();
        double pixelHeight = cal.getPixelHeightMicrons();
        if (!Double.isFinite(pixelWidth))
            pixelWidth = 1;
        if (!Double.isFinite(pixelHeight))
            pixelHeight = 1;
        Double defaultValue = null;
        if (roi.isLine()) {
            setPixelHeight = roi.getBoundsHeight() != 0;
            setPixelWidth = roi.getBoundsWidth() != 0;
            message = "Enter selected line length";
            defaultValue = roi.getScaledLength(pixelWidth, pixelHeight);
        } else {
            message = "Enter selected ROI area";
            units = units + "^2";
            defaultValue = roi.getScaledArea(pixelWidth, pixelHeight);
        }
        // }
        if (Double.isNaN(defaultValue))
            defaultValue = 1.0;
        var params = new ParameterList().addDoubleParameter("inputValue", message, defaultValue, units, "Enter calibrated value in " + units + " for the selected ROI to calculate the pixel size").addBooleanParameter("squarePixels", "Assume square pixels", true, "Set the pixel width to match the pixel height");
        params.setHiddenParameters(setPixelHeight && setPixelWidth, "squarePixels");
        if (!Dialogs.showParameterDialog("Set pixel size", params))
            return false;
        Double result = params.getDoubleParameterValue("inputValue");
        setPixelHeight = setPixelHeight || params.getBooleanParameterValue("squarePixels");
        setPixelWidth = setPixelWidth || params.getBooleanParameterValue("squarePixels");
        // Double result = Dialogs.showInputDialog("Set pixel size", message, defaultValue);
        // if (result == null)
        // return false;
        double sizeMicrons;
        if (roi.isLine())
            sizeMicrons = result.doubleValue() / roi.getLength();
        else
            sizeMicrons = Math.sqrt(result.doubleValue() / roi.getArea());
        if (setPixelHeight)
            pixelHeightMicrons = sizeMicrons;
        if (setPixelWidth)
            pixelWidthMicrons = sizeMicrons;
    } else {
        // Prompt for all required values
        ParameterList params = new ParameterList().addDoubleParameter("pixelWidth", "Pixel width", pixelWidthMicrons, GeneralTools.micrometerSymbol(), "Enter the pixel width").addDoubleParameter("pixelHeight", "Pixel height", pixelHeightMicrons, GeneralTools.micrometerSymbol(), "Entry the pixel height").addDoubleParameter("zSpacing", "Z-spacing", zSpacingMicrons, GeneralTools.micrometerSymbol(), "Enter the spacing between slices of a z-stack");
        params.setHiddenParameters(server.nZSlices() == 1, "zSpacing");
        if (!Dialogs.showParameterDialog("Set pixel size", params))
            return false;
        if (server.nZSlices() != 1) {
            zSpacingMicrons = params.getDoubleParameterValue("zSpacing");
        }
        pixelWidthMicrons = params.getDoubleParameterValue("pixelWidth");
        pixelHeightMicrons = params.getDoubleParameterValue("pixelHeight");
    }
    if ((pixelWidthMicrons <= 0 || pixelHeightMicrons <= 0) || (server.nZSlices() > 1 && zSpacingMicrons <= 0)) {
        if (!Dialogs.showConfirmDialog("Set pixel size", "You entered values <= 0, do you really want to remove this pixel calibration information?")) {
            return false;
        }
        zSpacingMicrons = server.nZSlices() > 1 && zSpacingMicrons > 0 ? zSpacingMicrons : Double.NaN;
        if (pixelWidthMicrons <= 0 || pixelHeightMicrons <= 0) {
            pixelWidthMicrons = Double.NaN;
            pixelHeightMicrons = Double.NaN;
        }
    }
    if (QP.setPixelSizeMicrons(imageData, pixelWidthMicrons, pixelHeightMicrons, zSpacingMicrons)) {
        // Log for scripts
        WorkflowStep step;
        if (server.nZSlices() == 1) {
            var map = Map.of("pixelWidthMicrons", pixelWidthMicrons, "pixelHeightMicrons", pixelHeightMicrons);
            String script = String.format("setPixelSizeMicrons(%f, %f)", pixelWidthMicrons, pixelHeightMicrons);
            step = new DefaultScriptableWorkflowStep("Set pixel size " + GeneralTools.micrometerSymbol(), map, script);
        } else {
            var map = Map.of("pixelWidthMicrons", pixelWidthMicrons, "pixelHeightMicrons", pixelHeightMicrons, "zSpacingMicrons", zSpacingMicrons);
            String script = String.format("setPixelSizeMicrons(%f, %f, %f)", pixelWidthMicrons, pixelHeightMicrons, zSpacingMicrons);
            step = new DefaultScriptableWorkflowStep("Set pixel size " + GeneralTools.micrometerSymbol(), map, script);
        }
        imageData.getHistoryWorkflow().addStep(step);
        return true;
    } else
        return false;
}
Also used : DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep) WorkflowStep(qupath.lib.plugins.workflow.WorkflowStep) DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep) ParameterList(qupath.lib.plugins.parameters.ParameterList) PixelCalibration(qupath.lib.images.servers.PixelCalibration)

Example 27 with DefaultScriptableWorkflowStep

use of qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep in project qupath by qupath.

the class Commands method selectObjectsByClass.

/**
 * Select objects that are instances of a specified class, logging an appropriate method in the workflow.
 *
 * @param imageData
 * @param cls
 */
public static void selectObjectsByClass(final ImageData<?> imageData, final Class<? extends PathObject> cls) {
    if (cls == TMACoreObject.class)
        QP.selectTMACores(imageData.getHierarchy());
    else
        QP.selectObjectsByClass(imageData.getHierarchy(), cls);
    Map<String, String> params = Collections.singletonMap("Type", PathObjectTools.getSuitableName(cls, false));
    String method;
    if (cls == PathAnnotationObject.class)
        method = "selectAnnotations();";
    else if (cls == PathDetectionObject.class)
        method = "selectDetections();";
    else if (cls == TMACoreObject.class)
        method = "selectTMACores();";
    else if (cls == PathCellObject.class)
        method = "selectCells();";
    else if (cls == PathTileObject.class)
        method = "selectTiles();";
    else
        // TODO: Get a suitable name to disguise Java classes
        method = "selectObjectsByClass(" + cls.getName() + ");";
    WorkflowStep newStep = new DefaultScriptableWorkflowStep("Select objects by class", params, method);
    WorkflowStep lastStep = imageData.getHistoryWorkflow().getLastStep();
    if (newStep.equals(lastStep))
        imageData.getHistoryWorkflow().replaceLastStep(newStep);
    else
        imageData.getHistoryWorkflow().addStep(newStep);
}
Also used : DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep) PathDetectionObject(qupath.lib.objects.PathDetectionObject) WorkflowStep(qupath.lib.plugins.workflow.WorkflowStep) DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep) PathCellObject(qupath.lib.objects.PathCellObject)

Example 28 with DefaultScriptableWorkflowStep

use of qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep in project qupath by qupath.

the class Commands method selectAllObjects.

/**
 * Select all objects (excluding the root object) in the imageData.
 *
 * @param imageData
 */
public static void selectAllObjects(final ImageData<?> imageData) {
    // Select all objects
    QP.selectAllObjects(imageData.getHierarchy(), false);
    // Add this step to the history workflow
    Map<String, String> map = new HashMap<>();
    map.put("includeRootObject", "false");
    WorkflowStep newStep = new DefaultScriptableWorkflowStep("Select all objects", map, "selectAllObjects(false)");
    imageData.getHistoryWorkflow().addStep(newStep);
}
Also used : DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) WorkflowStep(qupath.lib.plugins.workflow.WorkflowStep) DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep)

Example 29 with DefaultScriptableWorkflowStep

use of qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep in project qupath by qupath.

the class PixelClassifierUI method promptToClassifyDetectionsByCentroid.

private static boolean promptToClassifyDetectionsByCentroid(ImageData<BufferedImage> imageData, PixelClassifier classifier, String classifierName) {
    Objects.requireNonNull(imageData);
    Objects.requireNonNull(classifier);
    PixelClassifierTools.classifyDetectionsByCentroid(imageData, classifier);
    if (classifierName != null) {
        imageData.getHistoryWorkflow().addStep(new DefaultScriptableWorkflowStep("Classify detections by centroid", String.format("classifyDetectionsByCentroid(\"%s\")", classifierName)));
    }
    return true;
}
Also used : DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep)

Example 30 with DefaultScriptableWorkflowStep

use of qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep in project qupath by qupath.

the class PixelClassifierUI method promptToAddMeasurements.

private static boolean promptToAddMeasurements(ImageData<BufferedImage> imageData, PixelClassifier classifier, String classifierName) {
    if (imageData == null) {
        Dialogs.showNoImageError("Pixel classifier");
        return false;
    }
    var hierarchy = imageData.getHierarchy();
    List<SelectionChoice> choices = buildChoiceList(imageData.getHierarchy(), SelectionChoice.values());
    SelectionChoice defaultChoice;
    if (choices.contains(SelectionChoice.CURRENT_SELECTION))
        defaultChoice = SelectionChoice.CURRENT_SELECTION;
    else if (choices.contains(SelectionChoice.ANNOTATIONS))
        defaultChoice = SelectionChoice.ANNOTATIONS;
    else
        defaultChoice = choices.get(0);
    var params = new ParameterList().addStringParameter("id", "Measurement name", classifierName == null ? "Classifier" : classifierName, "Choose a base name for measurements - this helps distinguish between measurements from different classifiers").addChoiceParameter("choice", "Select objects", defaultChoice, choices, "Select the objects");
    if (!Dialogs.showParameterDialog("Pixel classifier", params))
        return false;
    var measurementID = params.getStringParameterValue("id");
    var selectionChoice = (SelectionChoice) params.getChoiceParameterValue("choice");
    selectionChoice.handleSelection(imageData);
    var objectsToMeasure = hierarchy.getSelectionModel().getSelectedObjects();
    int n = objectsToMeasure.size();
    if (objectsToMeasure.isEmpty()) {
        objectsToMeasure = Collections.singleton(hierarchy.getRootObject());
        logger.info("Requesting measurements for image");
    } else if (n == 1)
        logger.info("Requesting measurements for one object");
    else
        logger.info("Requesting measurements for {} objects", n);
    if (PixelClassifierTools.addMeasurementsToSelectedObjects(imageData, classifier, measurementID)) {
        if (classifierName != null) {
            String idString = measurementID == null ? "null" : "\"" + measurementID + "\"";
            imageData.getHistoryWorkflow().addStep(new DefaultScriptableWorkflowStep("Pixel classifier measurements", String.format("addPixelClassifierMeasurements(\"%s\", %s)", classifierName, idString)));
        }
        return true;
    }
    return false;
}
Also used : DefaultScriptableWorkflowStep(qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep) ParameterList(qupath.lib.plugins.parameters.ParameterList)

Aggregations

DefaultScriptableWorkflowStep (qupath.lib.plugins.workflow.DefaultScriptableWorkflowStep)31 PathObject (qupath.lib.objects.PathObject)12 PathObjectHierarchy (qupath.lib.objects.hierarchy.PathObjectHierarchy)12 WorkflowStep (qupath.lib.plugins.workflow.WorkflowStep)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 Map (java.util.Map)10 Dialogs (qupath.lib.gui.dialogs.Dialogs)10 Collectors (java.util.stream.Collectors)9 QuPathGUI (qupath.lib.gui.QuPathGUI)9 BufferedImage (java.awt.image.BufferedImage)8 IOException (java.io.IOException)8 Arrays (java.util.Arrays)8 ImageData (qupath.lib.images.ImageData)8 File (java.io.File)7 StringProperty (javafx.beans.property.StringProperty)7 Logger (org.slf4j.Logger)7 LoggerFactory (org.slf4j.LoggerFactory)7 GeneralTools (qupath.lib.common.GeneralTools)7 Collections (java.util.Collections)6