Search in sources :

Example 1 with EmpiricalDistribution

use of org.apache.commons.math3.random.EmpiricalDistribution in project GDSC-SMLM by aherbert.

the class CreateData method createPhotonDistribution.

/**
	 * @return A photon distribution loaded from a file of floating-point values with the specified population mean.
	 */
private RealDistribution createPhotonDistribution() {
    if (PHOTON_DISTRIBUTION[PHOTON_CUSTOM].equals(settings.photonDistribution)) {
        // Get the distribution file
        String filename = Utils.getFilename("Photon_distribution", settings.photonDistributionFile);
        if (filename != null) {
            settings.photonDistributionFile = filename;
            try {
                InputStream is = new FileInputStream(new File(settings.photonDistributionFile));
                BufferedReader in = new BufferedReader(new UnicodeReader(is, null));
                StoredDataStatistics stats = new StoredDataStatistics();
                try {
                    String str = null;
                    double val = 0.0d;
                    while ((str = in.readLine()) != null) {
                        val = Double.parseDouble(str);
                        stats.add(val);
                    }
                } finally {
                    in.close();
                }
                if (stats.getSum() > 0) {
                    // Update the statistics to the desired mean.
                    double scale = (double) settings.photonsPerSecond / stats.getMean();
                    double[] values = stats.getValues();
                    for (int i = 0; i < values.length; i++) values[i] *= scale;
                    // TODO - Investigate the limits of this distribution. 
                    // How far above and below the input data will values be generated.
                    // Create the distribution using the recommended number of bins
                    final int binCount = stats.getN() / 10;
                    EmpiricalDistribution dist = new EmpiricalDistribution(binCount, createRandomGenerator());
                    dist.load(values);
                    return dist;
                }
            } catch (IOException e) {
            // Ignore
            } catch (NullArgumentException e) {
            // Ignore 
            } catch (NumberFormatException e) {
            // Ignore
            }
        }
        Utils.log("Failed to load custom photon distribution from file: %s. Default to fixed.", settings.photonDistributionFile);
    } else if (PHOTON_DISTRIBUTION[PHOTON_UNIFORM].equals(settings.photonDistribution)) {
        if (settings.photonsPerSecond < settings.photonsPerSecondMaximum) {
            UniformRealDistribution dist = new UniformRealDistribution(createRandomGenerator(), settings.photonsPerSecond, settings.photonsPerSecondMaximum);
            return dist;
        }
    } else if (PHOTON_DISTRIBUTION[PHOTON_GAMMA].equals(settings.photonDistribution)) {
        final double scaleParameter = settings.photonsPerSecond / settings.photonShape;
        GammaDistribution dist = new GammaDistribution(createRandomGenerator(), settings.photonShape, scaleParameter, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
        return dist;
    } else if (PHOTON_DISTRIBUTION[PHOTON_CORRELATED].equals(settings.photonDistribution)) {
        // No distribution required
        return null;
    }
    settings.photonDistribution = PHOTON_DISTRIBUTION[PHOTON_FIXED];
    return null;
}
Also used : EmpiricalDistribution(org.apache.commons.math3.random.EmpiricalDistribution) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StoredDataStatistics(gdsc.core.utils.StoredDataStatistics) UniformRealDistribution(org.apache.commons.math3.distribution.UniformRealDistribution) UnicodeReader(gdsc.core.utils.UnicodeReader) IOException(java.io.IOException) NullArgumentException(org.apache.commons.math3.exception.NullArgumentException) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) GammaDistribution(org.apache.commons.math3.distribution.GammaDistribution) CustomGammaDistribution(org.apache.commons.math3.distribution.CustomGammaDistribution)

Example 2 with EmpiricalDistribution

use of org.apache.commons.math3.random.EmpiricalDistribution in project lucene-solr by apache.

the class EmpiricalDistributionEvaluator method evaluate.

public Tuple evaluate(Tuple tuple) throws IOException {
    if (subEvaluators.size() != 1) {
        throw new IOException("Empirical dist expects 1 column as a parameters");
    }
    StreamEvaluator colEval1 = subEvaluators.get(0);
    List<Number> numbers1 = (List<Number>) colEval1.evaluate(tuple);
    double[] column1 = new double[numbers1.size()];
    for (int i = 0; i < numbers1.size(); i++) {
        column1[i] = numbers1.get(i).doubleValue();
    }
    Arrays.sort(column1);
    EmpiricalDistribution empiricalDistribution = new EmpiricalDistribution();
    empiricalDistribution.load(column1);
    Map map = new HashMap();
    StatisticalSummary statisticalSummary = empiricalDistribution.getSampleStats();
    map.put("max", statisticalSummary.getMax());
    map.put("mean", statisticalSummary.getMean());
    map.put("min", statisticalSummary.getMin());
    map.put("stdev", statisticalSummary.getStandardDeviation());
    map.put("sum", statisticalSummary.getSum());
    map.put("N", statisticalSummary.getN());
    map.put("var", statisticalSummary.getVariance());
    return new EmpiricalDistributionTuple(empiricalDistribution, column1, map);
}
Also used : EmpiricalDistribution(org.apache.commons.math3.random.EmpiricalDistribution) StatisticalSummary(org.apache.commons.math3.stat.descriptive.StatisticalSummary) HashMap(java.util.HashMap) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with EmpiricalDistribution

use of org.apache.commons.math3.random.EmpiricalDistribution in project lucene-solr by apache.

the class HistogramEvaluator method evaluate.

public List<Map> evaluate(Tuple tuple) throws IOException {
    StreamEvaluator colEval1 = subEvaluators.get(0);
    List<Number> numbers1 = (List<Number>) colEval1.evaluate(tuple);
    double[] column1 = new double[numbers1.size()];
    for (int i = 0; i < numbers1.size(); i++) {
        column1[i] = numbers1.get(i).doubleValue();
    }
    int bins = 10;
    if (subEvaluators.size() == 2) {
        StreamEvaluator binsEval = subEvaluators.get(1);
        Number binsNum = (Number) binsEval.evaluate(tuple);
        bins = binsNum.intValue();
    }
    EmpiricalDistribution empiricalDistribution = new EmpiricalDistribution(bins);
    empiricalDistribution.load(column1);
    List<Map> binList = new ArrayList();
    List<SummaryStatistics> summaries = empiricalDistribution.getBinStats();
    for (SummaryStatistics statisticalSummary : summaries) {
        Map map = new HashMap();
        map.put("max", statisticalSummary.getMax());
        map.put("mean", statisticalSummary.getMean());
        map.put("min", statisticalSummary.getMin());
        map.put("stdev", statisticalSummary.getStandardDeviation());
        map.put("sum", statisticalSummary.getSum());
        map.put("N", statisticalSummary.getN());
        map.put("var", statisticalSummary.getVariance());
        binList.add(map);
    }
    return binList;
}
Also used : EmpiricalDistribution(org.apache.commons.math3.random.EmpiricalDistribution) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

EmpiricalDistribution (org.apache.commons.math3.random.EmpiricalDistribution)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 StoredDataStatistics (gdsc.core.utils.StoredDataStatistics)1 UnicodeReader (gdsc.core.utils.UnicodeReader)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 CustomGammaDistribution (org.apache.commons.math3.distribution.CustomGammaDistribution)1 GammaDistribution (org.apache.commons.math3.distribution.GammaDistribution)1 UniformRealDistribution (org.apache.commons.math3.distribution.UniformRealDistribution)1 NullArgumentException (org.apache.commons.math3.exception.NullArgumentException)1 StatisticalSummary (org.apache.commons.math3.stat.descriptive.StatisticalSummary)1 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)1