Skip to content

Commit

Permalink
Tweak fitting solvers for performance, add calculateWeights fn to Opt…
Browse files Browse the repository at this point in the history
…imizingFittingSolver to make extending easier
  • Loading branch information
nathanielsherry committed Mar 23, 2024
1 parent da66ee7 commit d39f88a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

public class MultisamplingOptimizingFittingSolver extends OptimizingFittingSolver {

public static final int MULTISAMPLE_COUNT = 8;

@Override
public String pluginName() {
return "MultiSampling";
Expand Down Expand Up @@ -52,7 +54,7 @@ public FittingResultSetView solve(FittingSolverContext inputCtx) {
int counter = 0;
double[] scalings = new double[size];
EvaluationSpace eval = new EvaluationSpace(permCtx.data.size());
while (counter <= 10) {
while (counter <= MULTISAMPLE_COUNT) {
Collections.shuffle(permCtx.curves, new Random(12345654321l));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

public class OptimizingFittingSolver implements FittingSolver {

protected double costFnPrecision = 0.01d;
protected double costFnPrecision = 0.015d;

@Override
public String pluginName() {
Expand Down Expand Up @@ -60,15 +60,19 @@ public FittingResultSetView solve(FittingSolverContext ctx) {
return getEmptyResult(ctx);
}

double[] weights = calculateWeights(ctx);
return evaluate(weights, ctx);

}

public double[] calculateWeights(FittingSolverContext ctx) {
EvaluationSpace eval = new EvaluationSpace(ctx.data.size());
MultivariateFunction cost = getCostFunction(ctx, eval);
double[] guess = getInitialGuess(ctx);

PointValuePair result = optimizeCostFunction(cost, guess, costFnPrecision);

double[] scalings = result.getPoint();
return evaluate(scalings, ctx);

return result.getPoint();
}

protected FittingResultSet getEmptyResult(FittingSolverContext ctx) {
Expand Down Expand Up @@ -135,7 +139,7 @@ public static double[] getInitialGuess(FittingSolverContext ctx) {

protected MultivariateFunction getCostFunction(FittingSolverContext ctx, EvaluationSpace eval) {
return new MultivariateFunction() {

@Override
public double value(double[] point) {

Expand All @@ -151,7 +155,7 @@ public double value(double[] point) {
float score = score(point, ctx, eval.residual);
if (containsNegatives > 0) {
return score * (1f+containsNegatives);
}
}
return score;

}
Expand Down Expand Up @@ -194,8 +198,9 @@ private float score(double[] point, FittingSolverContext ctx, Spectrum residual)
float[] ra = residual.backingArray();
float score = 0;
int length = ctx.channels.length;
int[] channels = ctx.channels;
for (int i = 0; i < length; i++) {
float value = ra[ctx.channels[i]];
float value = ra[channels[i]];

//Negative values mean that we've fit more signal than exists
//We penalize this to prevent making up data where none exists.
Expand Down

0 comments on commit d39f88a

Please sign in to comment.