<commit_before>package org.bdgp.OpenHiCAMM.Modules;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bdgp.OpenHiCAMM.ImageLog.ImageLogRunner;
import org.bdgp.OpenHiCAMM.Logger;
import org.bdgp.OpenHiCAMM.ValidationError;
import org.bdgp.OpenHiCAMM.DB.Config;
import org.bdgp.OpenHiCAMM.DB.Image;
import org.bdgp.OpenHiCAMM.DB.ROI;
import org.bdgp.OpenHiCAMM.Modules.Interfaces.Configuration;
import org.bdgp.OpenHiCAMM.Modules.Interfaces.ImageLogger;
import org.bdgp.OpenHiCAMM.Modules.Interfaces.Module;
import org.json.JSONException;
import org.micromanager.utils.ImageUtils;
import org.micromanager.utils.MDUtils;

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.measure.ResultsTable;
import ij.plugin.filter.Analyzer;
import ij.process.ImageProcessor;
import mmcorej.TaggedImage;

public class CustomMacroROIFinder extends ROIFinder implements Module, ImageLogger {

    public CustomMacroROIFinder() {
        super();
    }

    @Override
    public List<ROI> process(Image image, TaggedImage taggedImage,
            Logger logger, ImageLogRunner imageLog,
            Map<String, Config> config) 
    {
        // get the image label
        String positionName = null;
        try { positionName = MDUtils.getPositionName(taggedImage.tags); } 
        catch (JSONException e) {throw new RuntimeException(e);} 
        String imageLabel = image.getLabel();
        String label = String.format("%s (%s)", positionName, imageLabel); 

        Config roiImageScaleFactorConf = config.get("roiImageScaleFactor");
        if (roiImageScaleFactorConf == null) throw new RuntimeException("Config value roiImageScaleFactor not found!");
        Double roiImageScaleFactor = new Double(roiImageScaleFactorConf.getValue());

        Config customMacroConf = config.get("customMacro");
        if (customMacroConf == null) throw new RuntimeException("Config value customMacro not found!");
        String customMacro = customMacroConf.getValue();

        ImageProcessor processor = ImageUtils.makeProcessor(taggedImage);
        ImagePlus imp = new ImagePlus(image.toString(), processor);
        if (roiImageScaleFactor != 1.0) {
            logger.fine(String.format("%s: Resizing", label));
            imp.getProcessor().setInterpolationMethod(ImageProcessor.BILINEAR);
            imp.setProcessor(imp.getTitle(), imp.getProcessor().resize(
                    (int)Math.floor(imp.getWidth() * roiImageScaleFactor), 
                    (int)Math.floor(imp.getHeight() * roiImageScaleFactor)));
        }
        imageLog.addImage(imp, "Resized");

        logger.info(String.format("Running custom macro macro:%n%s", customMacro));
        imp.show();
        IJ.runMacro(customMacro);
        ImagePlus modifiedImage1 = WindowManager.getImage(imp.getTitle());
        if (modifiedImage1 != null) {
            imp = modifiedImage1;
        }
        imageLog.addImage(imp, "Running macro");

        List<ROI> rois = new ArrayList<ROI>();
        ResultsTable rt = Analyzer.getResultsTable();

        // Get the objects and iterate through them
        logger.fine(String.format("ResultsTable Column Headings: %s", rt.getColumnHeadings()));
        for (int i=0; i < rt.getCounter(); i++) {
            double area = rt.getValue("Area", i) / (roiImageScaleFactor*roiImageScaleFactor); // area of the object
            double bx = rt.getValue("BX", i) / roiImageScaleFactor; // x of bounding box
            double by = rt.getValue("BY", i) / roiImageScaleFactor; // y of bounding box
            double width = rt.getValue("Width", i) / roiImageScaleFactor; // width of bounding box
            double height = rt.getValue("Height", i) / roiImageScaleFactor; // height of bounding box
            logger.finest(String.format(
                    "Found object: area=%.2f, bx=%.2f, by=%.2f, width=%.2f, height=%.2f",
                    area, bx, by, width, height));

            ROI roi = new ROI(image.getId(), 
                    (int)Math.floor(bx), 
                    (int)Math.floor(by), 
                    (int)Math.floor(bx+width), 
                    (int)Math.floor(by+height));
            rois.add(roi);
            logger.info(String.format("%s: Created new ROI record with width=%.2f, height=%.2f, area=%.2f: %s", 
                    label, width, height, area, roi));
            
            // Draw the ROI rectangle
            try {
                imp.setRoi((int)Math.floor(rt.getValue("BX", i)), 
                        (int)Math.floor(rt.getValue("BY", i)), 
                        (int)Math.floor(rt.getValue("Width", i)), 
                        (int)Math.floor(rt.getValue("Height", i)));
                IJ.setForegroundColor(255, 255, 0);
                IJ.run(imp, "Draw", "");
            }
            catch (Throwable e) { 
                // Sometimes this fails, but it's not really important, so ignore it
                logger.warning(String.format("Couldn't draw the ROI rectangle!: %s", e.getMessage()));
            }
        }
        // close the image
        imageLog.addImage(imp, "Adding ROIs");
        return rois;
    }

    @Override
    public Configuration configure() {
        return new Configuration() {<commit_msg>fix image log for images with no acquisition<commit_after>package org.bdgp.OpenHiCAMM.Modules;

import java.awt.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bdgp.OpenHiCAMM.ImageLog.ImageLogRunner;
import org.bdgp.OpenHiCAMM.Logger;
import org.bdgp.OpenHiCAMM.ValidationError;
import org.bdgp.OpenHiCAMM.DB.Config;
import org.bdgp.OpenHiCAMM.DB.Image;
import org.bdgp.OpenHiCAMM.DB.ROI;
import org.bdgp.OpenHiCAMM.Modules.Interfaces.Configuration;
import org.bdgp.OpenHiCAMM.Modules.Interfaces.ImageLogger;
import org.bdgp.OpenHiCAMM.Modules.Interfaces.Module;
import org.json.JSONException;
import org.micromanager.utils.ImageUtils;
import org.micromanager.utils.MDUtils;

import ij.IJ;
import ij.ImagePlus;
import ij.WindowManager;
import ij.measure.ResultsTable;
import ij.plugin.filter.Analyzer;
import ij.process.ImageProcessor;
import mmcorej.TaggedImage;

public class CustomMacroROIFinder extends ROIFinder implements Module, ImageLogger {

    public CustomMacroROIFinder() {
        super();
    }

    @Override
    public List<ROI> process(Image image, TaggedImage taggedImage,
            Logger logger, ImageLogRunner imageLog,
            Map<String, Config> config) 
    {
        // get the image label
        String positionName = null;
        try { positionName = MDUtils.getPositionName(taggedImage.tags); } 
        catch (JSONException e) {throw new RuntimeException(e);} 
        String imageLabel = image.getLabel();
        String label = String.format("%s (%s)", positionName, imageLabel); 

        Config roiImageScaleFactorConf = config.get("roiImageScaleFactor");
        if (roiImageScaleFactorConf == null) throw new RuntimeException("Config value roiImageScaleFactor not found!");
        Double roiImageScaleFactor = new Double(roiImageScaleFactorConf.getValue());

        Config customMacroConf = config.get("customMacro");
        if (customMacroConf == null) throw new RuntimeException("Config value customMacro not found!");
        String customMacro = customMacroConf.getValue();

        ImageProcessor processor = ImageUtils.makeProcessor(taggedImage);
        ImagePlus imp = new ImagePlus(image.toString(), processor);
        if (roiImageScaleFactor != 1.0) {
            logger.fine(String.format("%s: Resizing", label));
            imp.getProcessor().setInterpolationMethod(ImageProcessor.BILINEAR);
            imp.setProcessor(imp.getTitle(), imp.getProcessor().resize(
                    (int)Math.floor(imp.getWidth() * roiImageScaleFactor), 
                    (int)Math.floor(imp.getHeight() * roiImageScaleFactor)));
        }
        imageLog.addImage(imp, "Resized");

        logger.info(String.format("Running custom macro macro:%n%s", customMacro));
        imp.show();
        IJ.runMacro(customMacro);
        ImagePlus modifiedImage1 = WindowManager.getImage(imp.getTitle());
        if (modifiedImage1 != null) {
            imp = modifiedImage1;
        }
        imageLog.addImage(imp, "Running macro");

        List<ROI> rois = new ArrayList<ROI>();
        ResultsTable rt = Analyzer.getResultsTable();

        // Get the objects and iterate through them
        logger.fine(String.format("ResultsTable Column Headings: %s", rt.getColumnHeadings()));
        for (int i=0; i < rt.getCounter(); i++) {
            double area = rt.getValue("Area", i) / (roiImageScaleFactor*roiImageScaleFactor); // area of the object
            double bx = rt.getValue("BX", i) / roiImageScaleFactor; // x of bounding box
            double by = rt.getValue("BY", i) / roiImageScaleFactor; // y of bounding box
            double width = rt.getValue("Width", i) / roiImageScaleFactor; // width of bounding box
            double height = rt.getValue("Height", i) / roiImageScaleFactor; // height of bounding box
            logger.finest(String.format(
                    "Found object: area=%.2f, bx=%.2f, by=%.2f, width=%.2f, height=%.2f",
                    area, bx, by, width, height));

            ROI roi = new ROI(image.getId(), 
                    (int)Math.floor(bx), 
                    (int)Math.floor(by), 
                    (int)Math.floor(bx+width), 
                    (int)Math.floor(by+height));
            rois.add(roi);
            // Draw the ROI rectangle
            try {
                imp.setRoi((int)Math.floor(rt.getValue("BX", i)), 
                        (int)Math.floor(rt.getValue("BY", i)), 
                        (int)Math.floor(rt.getValue("Width", i)), 
                        (int)Math.floor(rt.getValue("Height", i)));
                IJ.setForegroundColor(255, 255, 0);
                IJ.run(imp, "Draw", "");
            }
            catch (Throwable e) { 
                // Sometimes this fails, but it's not really important, so ignore it
                logger.warning(String.format("Couldn't draw the ROI rectangle!: %s", e.getMessage()));
            }
        }
        // close the image
        imageLog.addImage(imp, "Adding ROIs");
        return rois;
    }

    @Override
    public Configuration configure() {
        return new Configuration() {
{'fraction_non_alphanumeric': 0.12004974172565525, 'fraction_numerical': 0.0037306294241438685, 'mean_word_length': 2.578028747433265, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '977905', 'n_tokens_mistral': 3061, 'n_tokens_neox': 2954, 'n_words': 662}