<commit_before>package net.imagej.ops.threshold.localPhansalkar;

import net.imagej.ops.ComputerOp;
import net.imagej.ops.OpService;
import net.imagej.ops.Ops;
import net.imagej.ops.Ops.Stats.Mean;
import net.imagej.ops.Ops.Stats.StdDev;
import net.imagej.ops.threshold.LocalThresholdMethod;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Pair;

import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
 * This is a modification of Sauvola's thresholding method to deal with low
 * contrast images.
 * 
 * In this algorithm the threshold is computed as t =
 * mean*(1+p*exp(-q*mean)+k*((stdev/r)-1)) for an image that is normalized to
 * [0, 1].
 * 
 * Phansalkar recommends k = 0.25, r = 0.5, p = 2 and q = 10. In the current
 * implementation, the values of p and q are fixed but can be implemented as
 * additional parameters.
 * 
 * Originally implemented from Phansalkar's paper description by <NAME>
 * (http://fiji.sc/Auto_Local_Threshold#Phansalkar).
 * 
 * Phansalskar N. et al. Adaptive local thresholding for detection of nuclei in
 * diversity stained cytology images. International Conference on Communications
 * and Signal Processing (ICCSP), 2011, 218 - 220.
 * 
 * @author <NAME> <<EMAIL>>
 */
@Plugin(type = Ops.Threshold.LocalPhansalkar.class, name = Ops.Threshold.LocalPhansalkar.NAME)
public class LocalPhansalkar<T extends RealType<T>> extends LocalThresholdMethod<T>
	implements Ops.Threshold.LocalPhansalkar
{

	@Parameter(required = false)
	private double k = 0.25;

	@Parameter(required = false)
	private double r = 0.5;

	@Parameter
	private OpService ops;

	private ComputerOp<Iterable<T>, DoubleType> mean;
	private ComputerOp<Iterable<T>, DoubleType> stdDeviation;

	private double p = 2.0;
	private double q = 10.0;

	@Override
	public void initialize() {
		mean = ops().computer(Mean.class, new DoubleType(), in().getB());
		stdDeviation = ops().computer(StdDev.class, new DoubleType(), in().getB());
	}

	@Override
	public void compute(final Pair<T, Iterable<T>> input, final BitType output) {

		final DoubleType meanValue = new DoubleType();
		mean.compute(input.getB(), meanValue);<commit_msg>Added FIXME about combined op for mean and stddev computations<commit_after>package net.imagej.ops.threshold.localPhansalkar;

import net.imagej.ops.ComputerOp;
import net.imagej.ops.OpService;
import net.imagej.ops.Ops;
import net.imagej.ops.Ops.Stats.Mean;
import net.imagej.ops.Ops.Stats.StdDev;
import net.imagej.ops.threshold.LocalThresholdMethod;
import net.imglib2.type.logic.BitType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Pair;

import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

/**
 * This is a modification of Sauvola's thresholding method to deal with low
 * contrast images.
 * 
 * In this algorithm the threshold is computed as t =
 * mean*(1+p*exp(-q*mean)+k*((stdev/r)-1)) for an image that is normalized to
 * [0, 1].
 * 
 * Phansalkar recommends k = 0.25, r = 0.5, p = 2 and q = 10. In the current
 * implementation, the values of p and q are fixed but can be implemented as
 * additional parameters.
 * 
 * Originally implemented from Phansalkar's paper description by <NAME>
 * (http://fiji.sc/Auto_Local_Threshold#Phansalkar).
 * 
 * Phansalskar N. et al. Adaptive local thresholding for detection of nuclei in
 * diversity stained cytology images. International Conference on Communications
 * and Signal Processing (ICCSP), 2011, 218 - 220.
 * 
 * @author <NAME> <<EMAIL>>
 */
@Plugin(type = Ops.Threshold.LocalPhansalkar.class, name = Ops.Threshold.LocalPhansalkar.NAME)
public class LocalPhansalkar<T extends RealType<T>> extends LocalThresholdMethod<T>
	implements Ops.Threshold.LocalPhansalkar
{

	@Parameter(required = false)
	private double k = 0.25;

	@Parameter(required = false)
	private double r = 0.5;

	@Parameter
	private OpService ops;

	// FIXME: Faster calculation of mean and std-dev
	private ComputerOp<Iterable<T>, DoubleType> mean;
	private ComputerOp<Iterable<T>, DoubleType> stdDeviation;

	private double p = 2.0;
	private double q = 10.0;

	@Override
	public void initialize() {
		mean = ops().computer(Mean.class, new DoubleType(), in().getB());
		stdDeviation = ops().computer(StdDev.class, new DoubleType(), in().getB());
	}

	@Override
	public void compute(final Pair<T, Iterable<T>> input, final BitType output) {

		final DoubleType meanValue = new DoubleType();
		mean.compute(input.getB(), meanValue);
{'fraction_non_alphanumeric': 0.117162074202647, 'fraction_numerical': 0.015621609893686265, 'mean_word_length': 4.677339901477833, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '6818707', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1462, 'n_words': 423}