use of org.apache.commons.math3.exception.NullArgumentException 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;
}
use of org.apache.commons.math3.exception.NullArgumentException in project FSensor by KalebKE.
the class RotationKalmanFilter method correct.
/**
* Correct the current state estimate with an actual measurement.
*
* @param z
* the measurement vector
* @throws NullArgumentException
* if the measurement vector is {@code null}
* @throws DimensionMismatchException
* if the dimension of the measurement vector does not fit
* @throws SingularMatrixException
* if the covariance matrix could not be inverted
*/
public void correct(final RealVector z) throws NullArgumentException, DimensionMismatchException, SingularMatrixException {
// sanity checks
MathUtils.checkNotNull(z);
if (z.getDimension() != measurementMatrix.getRowDimension()) {
throw new DimensionMismatchException(z.getDimension(), measurementMatrix.getRowDimension());
}
// S = H * P(k) * H' + R
RealMatrix s = measurementMatrix.multiply(errorCovariance).multiply(measurementMatrixT).add(measurementModel.getMeasurementNoise());
// Inn = z(k) - H * xHat(k)-
RealVector innovation = z.subtract(measurementMatrix.operate(stateEstimation));
// calculate gain matrix
// K(k) = P(k)- * H' * (H * P(k)- * H' + R)^-1
// K(k) = P(k)- * H' * S^-1
// instead of calculating the inverse of S we can rearrange the formula,
// and then solve the linear equation A x X = B with A = S', X = K' and
// B = (H * P)'
// K(k) * S = P(k)- * H'
// S' * K(k)' = H * P(k)-'
RealMatrix kalmanGain = new CholeskyDecomposition(s).getSolver().solve(measurementMatrix.multiply(errorCovariance.transpose())).transpose();
// update estimate with measurement z(k)
// xHat(k) = xHat(k)- + K * Inn
stateEstimation = stateEstimation.add(kalmanGain.operate(innovation));
// update covariance of prediction error
// P(k) = (I - K * H) * P(k)-
RealMatrix identity = MatrixUtils.createRealIdentityMatrix(kalmanGain.getRowDimension());
errorCovariance = identity.subtract(kalmanGain.multiply(measurementMatrix)).multiply(errorCovariance);
}
use of org.apache.commons.math3.exception.NullArgumentException in project GDSC-SMLM by aherbert.
the class CreateData method createPhotonDistribution.
/**
* Creates the photon distribution.
*
* @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.getPhotonDistribution())) {
// Get the distribution file
final String filename = ImageJUtils.getFilename("Photon_distribution", settings.getPhotonDistributionFile());
if (filename != null) {
settings.setPhotonDistributionFile(filename);
try (BufferedReader in = new BufferedReader(new UnicodeReader(new FileInputStream(new File(settings.getPhotonDistributionFile())), null))) {
final StoredDataStatistics stats = new StoredDataStatistics();
String str = in.readLine();
double val = 0.0d;
while (str != null) {
val = Double.parseDouble(str);
stats.add(val);
str = in.readLine();
}
if (stats.getSum() > 0) {
// Update the statistics to the desired mean.
final double scale = settings.getPhotonsPerSecond() / stats.getMean();
final 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;
final EmpiricalDistribution dist = new EmpiricalDistribution(binCount, new RandomGeneratorAdapter(createRandomGenerator()));
dist.load(values);
return dist;
}
} catch (final IOException | NullArgumentException | NumberFormatException ex) {
// Ignore
}
}
ImageJUtils.log("Failed to load custom photon distribution from file: %s. Default to fixed.", settings.getPhotonDistributionFile());
} else if (PHOTON_DISTRIBUTION[PHOTON_UNIFORM].equals(settings.getPhotonDistribution())) {
if (settings.getPhotonsPerSecond() < settings.getPhotonsPerSecondMaximum()) {
return new UniformRealDistribution(new RandomGeneratorAdapter(createRandomGenerator()), settings.getPhotonsPerSecond(), settings.getPhotonsPerSecondMaximum());
}
} else if (PHOTON_DISTRIBUTION[PHOTON_GAMMA].equals(settings.getPhotonDistribution())) {
final double scaleParameter = settings.getPhotonsPerSecond() / settings.getPhotonShape();
return new GammaDistribution(new RandomGeneratorAdapter(createRandomGenerator()), settings.getPhotonShape(), scaleParameter, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
} else if (PHOTON_DISTRIBUTION[PHOTON_CORRELATED].equals(settings.getPhotonDistribution())) {
// No distribution required
return null;
}
settings.setPhotonDistribution(PHOTON_DISTRIBUTION[PHOTON_FIXED]);
return null;
}
Aggregations