Mercurial > dive4elements > river
diff artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixCalculation.java @ 9099:850ce16034e9
2.3.4.1.10 Berechnung mit Start-km > End-km
author | gernotbelger |
---|---|
date | Mon, 28 May 2018 13:22:45 +0200 |
parents | 6650485c2c9b |
children | 202fd59b4f21 |
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixCalculation.java Mon May 28 10:56:57 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixCalculation.java Mon May 28 13:22:45 2018 +0200 @@ -8,30 +8,6 @@ package org.dive4elements.river.artifacts.model.fixings; -import org.dive4elements.artifacts.common.utils.StringUtils; - -import org.dive4elements.river.artifacts.access.FixAccess; - -import org.dive4elements.river.artifacts.math.fitting.Function; -import org.dive4elements.river.artifacts.math.fitting.FunctionFactory; - -import org.dive4elements.river.artifacts.model.Calculation; -import org.dive4elements.river.artifacts.model.CalculationResult; -import org.dive4elements.river.artifacts.model.FixingsColumn; -import org.dive4elements.river.artifacts.model.FixingsColumnFactory; - -import org.dive4elements.river.artifacts.model.FixingsOverview.Fixing.Filter; - -import org.dive4elements.river.artifacts.model.FixingsOverview.Fixing; -import org.dive4elements.river.artifacts.model.FixingsOverview.IdsFilter; - -import org.dive4elements.river.artifacts.model.FixingsOverview; -import org.dive4elements.river.artifacts.model.FixingsOverviewFactory; -import org.dive4elements.river.artifacts.model.Parameters; - -import org.dive4elements.river.utils.DoubleUtil; -import org.dive4elements.river.utils.KMIndex; - import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -39,99 +15,104 @@ import java.util.Map; import org.apache.log4j.Logger; +import org.dive4elements.artifacts.common.utils.StringUtils; +import org.dive4elements.river.artifacts.access.FixAccess; +import org.dive4elements.river.artifacts.math.fitting.Function; +import org.dive4elements.river.artifacts.math.fitting.FunctionFactory; +import org.dive4elements.river.artifacts.model.Calculation; +import org.dive4elements.river.artifacts.model.CalculationResult; +import org.dive4elements.river.artifacts.model.FixingsColumn; +import org.dive4elements.river.artifacts.model.FixingsColumnFactory; +import org.dive4elements.river.artifacts.model.FixingsOverview; +import org.dive4elements.river.artifacts.model.FixingsOverview.Fixing; +import org.dive4elements.river.artifacts.model.FixingsOverview.Fixing.Filter; +import org.dive4elements.river.artifacts.model.FixingsOverview.IdsFilter; +import org.dive4elements.river.artifacts.model.FixingsOverviewFactory; +import org.dive4elements.river.artifacts.model.Parameters; +import org.dive4elements.river.utils.DoubleUtil; +import org.dive4elements.river.utils.KMIndex; /** Calculation base class for fix. */ -public abstract class FixCalculation -extends Calculation -{ +public abstract class FixCalculation extends Calculation { private static Logger log = Logger.getLogger(FixCalculation.class); public static final double EPSILON = 1e-4; - public static final String [] STANDARD_COLUMNS = { - "km", "chi_sqr", "max_q", "std-dev" - }; + public static final String[] STANDARD_COLUMNS = { "km", "chi_sqr", "max_q", "std-dev" }; protected static class FitResult { - protected Parameters parameters; - protected KMIndex<QWD []> referenced; - protected KMIndex<QWI []> outliers; + protected Parameters parameters; + protected KMIndex<QWD[]> referenced; + protected KMIndex<QWI[]> outliers; public FitResult() { } - public FitResult( - Parameters parameters, - KMIndex<QWD []> referenced, - KMIndex<QWI []> outliers - ) { + public FitResult(final Parameters parameters, final KMIndex<QWD[]> referenced, final KMIndex<QWI[]> outliers) { this.parameters = parameters; this.referenced = referenced; - this.outliers = outliers; + this.outliers = outliers; } public Parameters getParameters() { - return parameters; + return this.parameters; } - public KMIndex<QWD []> getReferenced() { - return referenced; + public KMIndex<QWD[]> getReferenced() { + return this.referenced; } - public KMIndex<QWI []> getOutliers() { - return outliers; + public KMIndex<QWI[]> getOutliers() { + return this.outliers; } } // class FitResult - /** Helper class to bundle the meta information of a column - * and the real data. + /** + * Helper class to bundle the meta information of a column + * and the real data. */ protected static class Column { protected Fixing.Column meta; protected FixingsColumn data; - protected int index; + protected int index; public Column() { } - public Column(Fixing.Column meta, FixingsColumn data, int index) { - this.meta = meta; - this.data = data; + public Column(final Fixing.Column meta, final FixingsColumn data, final int index) { + this.meta = meta; + this.data = data; this.index = index; } public Date getDate() { - return meta.getStartTime(); + return this.meta.getStartTime(); } public String getDescription() { - return meta.getDescription(); + return this.meta.getDescription(); } public int getIndex() { - return index; + return this.index; } public int getId() { - return meta.getId(); + return this.meta.getId(); } - public boolean getQW( - double km, - double [] qs, - double [] ws, - int index - ) { - qs[index] = data.getQ(km); - return data.getW(km, ws, index); + public boolean getQW(final double km, final double[] qs, final double[] ws, final int index) { + qs[index] = this.data.getQ(km); + return this.data.getW(km, ws, index); } - public boolean getQW(double km, double [] wq) { - data.getW(km, wq, 0); - if (Double.isNaN(wq[0])) return false; - wq[1] = data.getQ(km); + public boolean getQW(final double km, final double[] wq) { + this.data.getW(km, wq, 0); + if (Double.isNaN(wq[0])) + return false; + wq[1] = this.data.getQ(km); return !Double.isNaN(wq[1]); } } // class Column @@ -144,49 +125,46 @@ protected Map<Integer, Column> columns; public ColumnCache() { - columns = new HashMap<Integer, Column>(); + this.columns = new HashMap<>(); } - public Column getColumn(Fixing.Column meta) { - Integer key = meta.getId(); - Column column = columns.get(key); + public Column getColumn(final Fixing.Column meta) { + final Integer key = meta.getId(); + Column column = this.columns.get(key); if (column == null) { - FixingsColumn data = FixingsColumnFactory - .getInstance() - .getColumnData(meta); + final FixingsColumn data = FixingsColumnFactory.getInstance().getColumnData(meta); if (data != null) { - column = new Column(meta, data, columns.size()); - columns.put(key, column); + column = new Column(meta, data, this.columns.size()); + this.columns.put(key, column); } } return column; } } // class ColumnCache - - protected String river; - protected double from; - protected double to; - protected double step; + protected String river; + protected double from; + protected double to; + protected double step; protected boolean preprocessing; - protected String function; - protected int [] events; - protected int qSectorStart; - protected int qSectorEnd; + protected String function; + protected int[] events; + protected int qSectorStart; + protected int qSectorEnd; public FixCalculation() { } - public FixCalculation(FixAccess access) { - String river = access.getRiverName(); - Double from = access.getFrom(); - Double to = access.getTo(); - Double step = access.getStep(); - String function = access.getFunction(); - int [] events = access.getEvents(); - Integer qSectorStart = access.getQSectorStart(); - Integer qSectorEnd = access.getQSectorEnd(); - Boolean preprocessing = access.getPreprocessing(); + public FixCalculation(final FixAccess access) { + final String river = access.getRiverName(); + final Double from = access.getLowerKm(); + final Double to = access.getUpperKm(); + final Double step = access.getStep(); + final String function = access.getFunction(); + final int[] events = access.getEvents(); + final Integer qSectorStart = access.getQSectorStart(); + final Integer qSectorEnd = access.getQSectorEnd(); + final Boolean preprocessing = access.getPreprocessing(); if (river == null) { addProblem("fix.missing.river"); @@ -225,57 +203,51 @@ } if (!hasProblems()) { - this.river = river; - this.from = from; - this.to = to; - this.step = step; - this.function = function; - this.events = events; - this.qSectorStart = qSectorStart; - this.qSectorEnd = qSectorEnd; + this.river = river; + this.from = from; + this.to = to; + this.step = step; + this.function = function; + this.events = events; + this.qSectorStart = qSectorStart; + this.qSectorEnd = qSectorEnd; this.preprocessing = preprocessing; } } - protected static String toString( - String [] parameterNames, - double [] values - ) { - StringBuilder sb = new StringBuilder(); + protected static String toString(final String[] parameterNames, final double[] values) { + final StringBuilder sb = new StringBuilder(); for (int i = 0; i < parameterNames.length; ++i) { - if (i > 0) sb.append(", "); + if (i > 0) + sb.append(", "); sb.append(parameterNames[i]).append(": ").append(values[i]); } return sb.toString(); } - - /** Create filter to accept only the chosen events. - * This factored out out to be overwritten. + /** + * Create filter to accept only the chosen events. + * This factored out out to be overwritten. */ protected Filter createFilter() { - return new IdsFilter(events); + return new IdsFilter(this.events); } - protected List<Column> getEventColumns( - FixingsOverview overview, - ColumnCache cc - ) { - FixingsColumnFactory fcf = FixingsColumnFactory.getInstance(); + protected List<Column> getEventColumns(final FixingsOverview overview, final ColumnCache cc) { + final FixingsColumnFactory fcf = FixingsColumnFactory.getInstance(); - Filter filter = createFilter(); - - List<Fixing.Column> metas = overview.filter(null, filter); + final Filter filter = createFilter(); - List<Column> columns = new ArrayList<Column>(metas.size()); + final List<Fixing.Column> metas = overview.filter(null, filter); - for (Fixing.Column meta: metas) { + final List<Column> columns = new ArrayList<>(metas.size()); - Column data = cc.getColumn(meta); + for (final Fixing.Column meta : metas) { + + final Column data = cc.getColumn(meta); if (data == null) { addProblem("fix.cannot.load.data"); - } - else { + } else { columns.add(data); } } @@ -284,12 +256,8 @@ } // Fit a function to the given points from fixation. - protected FitResult doFitting( - FixingsOverview overview, - ColumnCache cc, - Function func - ) { - boolean debug = log.isDebugEnabled(); + protected FitResult doFitting(final FixingsOverview overview, final ColumnCache cc, final Function func) { + final boolean debug = log.isDebugEnabled(); final List<Column> eventColumns = getEventColumns(overview, cc); @@ -298,26 +266,19 @@ return null; } - final double [] qs = new double[eventColumns.size()]; - final double [] ws = new double[qs.length]; - final boolean [] interpolated = new boolean[ws.length]; + final double[] qs = new double[eventColumns.size()]; + final double[] ws = new double[qs.length]; + final boolean[] interpolated = new boolean[ws.length]; - Fitting.QWDFactory qwdFactory = new Fitting.QWDFactory() { + final Fitting.QWDFactory qwdFactory = new Fitting.QWDFactory() { @Override - public QWD create(double q, double w) { + public QWD create(final double q, final double w) { // Check all the event columns for close match // and take the description and the date from meta. for (int i = 0; i < qs.length; ++i) { - if (Math.abs(qs[i]-q) < EPSILON - && Math.abs(ws[i]-w) < EPSILON) { - Column column = eventColumns.get(i); - return new QWD( - qs[i], ws[i], - column.getDescription(), - column.getDate(), - interpolated[i], - 0d, - column.getId()); // Use database id here + if (Math.abs(qs[i] - q) < EPSILON && Math.abs(ws[i] - w) < EPSILON) { + final Column column = eventColumns.get(i); + return new QWD(qs[i], ws[i], column.getDescription(), column.getDate(), interpolated[i], 0d, column.getId()); // Use database id here } } log.warn("cannot find column for (" + q + ", " + w + ")"); @@ -325,35 +286,33 @@ } }; - Fitting fitting = new Fitting(func, qwdFactory, preprocessing); + final Fitting fitting = new Fitting(func, qwdFactory, this.preprocessing); - String [] parameterNames = func.getParameterNames(); + final String[] parameterNames = func.getParameterNames(); - Parameters results = - new Parameters( - StringUtils.join(STANDARD_COLUMNS, parameterNames)); + final Parameters results = new Parameters(StringUtils.join(STANDARD_COLUMNS, parameterNames)); boolean invalid = false; - double [] kms = DoubleUtil.explode(from, to, step / 1000.0); + final double[] kms = DoubleUtil.explode(this.from, this.to, this.step / 1000.0); if (debug) { log.debug("number of kms: " + kms.length); } - KMIndex<QWI []> outliers = new KMIndex<QWI []>(); - KMIndex<QWD []> referenced = new KMIndex<QWD []>(kms.length); + final KMIndex<QWI[]> outliers = new KMIndex<>(); + final KMIndex<QWD[]> referenced = new KMIndex<>(kms.length); - int kmIndex = results.columnIndex("km"); - int chiSqrIndex = results.columnIndex("chi_sqr"); - int maxQIndex = results.columnIndex("max_q"); - int stdDevIndex = results.columnIndex("std-dev"); - int [] parameterIndices = results.columnIndices(parameterNames); + final int kmIndex = results.columnIndex("km"); + final int chiSqrIndex = results.columnIndex("chi_sqr"); + final int maxQIndex = results.columnIndex("max_q"); + final int stdDevIndex = results.columnIndex("std-dev"); + final int[] parameterIndices = results.columnIndices(parameterNames); int numFailed = 0; - for (int i = 0; i < kms.length; ++i) { - double km = kms[i]; + for (final double km2 : kms) { + final double km = km2; // Fill Qs and Ws from event columns. for (int j = 0; j < ws.length; ++j) { @@ -369,7 +328,7 @@ continue; } - QWD [] refs = fitting.referencedToArray(); + final QWD[] refs = fitting.referencedToArray(); referenced.add(km, refs); @@ -377,17 +336,17 @@ outliers.add(km, fitting.outliersToArray()); } - int row = results.newRow(); - double [] values = fitting.getParameters(); + final int row = results.newRow(); + final double[] values = fitting.getParameters(); results.set(row, kmIndex, km); results.set(row, chiSqrIndex, fitting.getChiSquare()); results.set(row, stdDevIndex, fitting.getStandardDeviation()); - results.set(row, maxQIndex, fitting.getMaxQ()); + results.set(row, maxQIndex, fitting.getMaxQ()); invalid |= results.set(row, parameterIndices, values); if (debug) { - log.debug("km: "+km+" " + toString(parameterNames, values)); + log.debug("km: " + km + " " + toString(parameterNames, values)); } } @@ -404,22 +363,17 @@ outliers.sort(); referenced.sort(); - return new FitResult( - results, - referenced, - outliers); + return new FitResult(results, referenced, outliers); } public CalculationResult calculate() { - FixingsOverview overview = - FixingsOverviewFactory.getOverview(river); + final FixingsOverview overview = FixingsOverviewFactory.getOverview(this.river); if (overview == null) { addProblem("fix.no.overview.available"); } - Function func = FunctionFactory.getInstance() - .getFunction(function); + final Function func = FunctionFactory.getInstance().getFunction(this.function); if (func == null) { addProblem("fix.invalid.function.name"); @@ -428,13 +382,13 @@ if (hasProblems()) { return new CalculationResult(this); } - CalculationResult result = innerCalculate(overview, func); + final CalculationResult result = innerCalculate(overview, func); if (result != null) { // Workaraound to deal with same dates in data set - Object o = result.getData(); + final Object o = result.getData(); if (o instanceof FixResult) { - FixResult fr = (FixResult)o; + final FixResult fr = (FixResult) o; fr.makeReferenceEventsDatesUnique(); fr.remapReferenceIndicesToRank(); } @@ -443,9 +397,6 @@ return result; } - protected abstract CalculationResult innerCalculate( - FixingsOverview overview, - Function function - ); + protected abstract CalculationResult innerCalculate(FixingsOverview overview, Function function); } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :