From f024e66fd13fc3c4fb251291b69c36ad9ff5c8d8 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Fri, 20 Apr 2018 21:59:16 +0100 Subject: [PATCH] JAL-591 slider scale factor now derived from min-max range --- src/jalview/gui/OptsAndParamsPage.java | 239 ++++++++++---------- src/jalview/hmmer/HMMERParamStore.java | 2 - .../ws/params/simple/LogarithmicParameter.java | 13 +- 3 files changed, 119 insertions(+), 135 deletions(-) diff --git a/src/jalview/gui/OptsAndParamsPage.java b/src/jalview/gui/OptsAndParamsPage.java index 0d12347..fcb293c 100644 --- a/src/jalview/gui/OptsAndParamsPage.java +++ b/src/jalview/gui/OptsAndParamsPage.java @@ -20,9 +20,6 @@ */ package jalview.gui; -import static jalview.ws.params.simple.LogarithmicParameter.LOGSLIDERSCALE; - -import jalview.bin.Cache; import jalview.util.MessageManager; import jalview.ws.params.ArgumentI; import jalview.ws.params.OptionI; @@ -33,6 +30,7 @@ import jalview.ws.params.simple.LogarithmicParameter; import jalview.ws.params.simple.StringParameter; import java.awt.BorderLayout; +import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; @@ -304,7 +302,11 @@ public class OptsAndParamsPage public class ParamBox extends JPanel implements ChangeListener, ActionListener, MouseListener { - private static final float SLIDERSCALE = 1000f; + /* + * parameter values (or their logs) are multiplied by this + * scaling factor to ensure an integer range for the slider + */ + private int sliderScaleFactor = 1; boolean isLogarithmicParameter; @@ -349,17 +351,34 @@ public class OptsAndParamsPage finfo = parm.getFurtherDetails(); validator = parm.getValidValue(); parameter = parm; + + isLogarithmicParameter = parm instanceof LogarithmicParameter; + if (validator != null) { - isIntegerParameter = validator.getType() == ValueType.Integer; - } - else if (parameter.getPossibleValues() != null) - { - isChoiceParameter = true; + ValueType type = validator.getType(); + isIntegerParameter = type == ValueType.Integer; + + /* + * ensure slider has an integer range corresponding to + * the min-max range of the parameter + */ + if (validator.getMin() != null && !isIntegerParameter) + { + double min = validator.getMin().doubleValue(); + double max = validator.getMax().doubleValue(); + if (isLogarithmicParameter) + { + min = Math.log(min); + max = Math.log(max); + } + sliderScaleFactor = (int) (1000000 / (max - min)); + // todo scaleMin, scaleMax could also be final fields + } } - if (parm instanceof LogarithmicParameter) + else { - isLogarithmicParameter = true; + isChoiceParameter = parameter.getPossibleValues() != null; } if (compact) @@ -582,15 +601,11 @@ public class OptsAndParamsPage @Override public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub - } @Override public void mouseExited(MouseEvent e) { - // TODO Auto-generated method stub - } @Override @@ -605,8 +620,6 @@ public class OptsAndParamsPage @Override public void mouseReleased(MouseEvent e) { - // TODO Auto-generated method stub - } @Override @@ -619,18 +632,19 @@ public class OptsAndParamsPage /* * set (int or float formatted) text field value */ - valueField.setText(isIntegerParameter ? String.valueOf(slider.getValue()) - : String.valueOf(slider.getValue() / SLIDERSCALE)); + valueField.setText(isIntegerParameter + ? String.valueOf(slider.getValue()) + : formatDouble( + slider.getValue() / (float) sliderScaleFactor)); } else { - double base = ((LogarithmicParameter) parameter).getBase(); - double value = Math.pow(base, slider.getValue() / LOGSLIDERSCALE); + double value = Math.pow(Math.E, + slider.getValue() / (double) sliderScaleFactor); valueField.setText(formatDouble(value)); } checkIfModified(); } - } /** @@ -642,10 +656,22 @@ public class OptsAndParamsPage */ String formatDouble(double value) { - String format = value < 0.001 ? "%3.3e" : "%3.3f"; + String format = value < 0.001 ? "%3.1E" : "%3.3f"; return String.format(format, value); } + /** + * Formats a number as integer or float (3dp) or scientific notation (1dp) + * + * @param n + * @return + */ + String formatNumber(Number n) + { + return n instanceof Integer ? String.valueOf(n.intValue()) + : formatDouble(n.doubleValue()); + } + void updateControls(ParameterI parm) { adjusting = true; @@ -679,7 +705,7 @@ public class OptsAndParamsPage } } }); - valueField.setPreferredSize(new Dimension(60, 25)); + valueField.setPreferredSize(new Dimension(65, 25)); controlsPanel.add(slider, BorderLayout.WEST); controlsPanel.add(valueField, BorderLayout.EAST); } @@ -696,7 +722,6 @@ public class OptsAndParamsPage choicebox.addItem(val); } } - if (parm.getValue() != null) { choicebox.setSelectedItem(parm.getValue()); @@ -704,18 +729,7 @@ public class OptsAndParamsPage } else { - if (parm instanceof LogarithmicParameter) - { - double base = ((LogarithmicParameter) parm).getBase(); - // double value = Math.pow(base, - // Double.parseDouble(parm.getValue()) / LOGSLIDERSCALE); - double value = Double.parseDouble(parm.getValue()); - valueField.setText(formatDouble(value)); - } - else - { - valueField.setText(parm.getValue()); - } + valueField.setText(parm.getValue()); } } lastVal = updateSliderFromValueField(); @@ -740,43 +754,50 @@ public class OptsAndParamsPage { if (validator == null) { - if (!isChoiceParameter) + if (isChoiceParameter) { - slider.setVisible(false); - return valueField.getText().trim(); + return choicebox.getSelectedItem(); } - else + slider.setVisible(false); + return valueField.getText().trim(); + } + + valueField.setText(valueField.getText().trim()); + + /* + * ensure not outside min-max range + * TODO: provide some visual indicator if limit reached + */ + try + { + valueField.setBackground(Color.WHITE); + double d = Double.parseDouble(valueField.getText()); + if (validator.getMin() != null + && validator.getMin().doubleValue() > d) { - return choicebox.getSelectedItem(); + valueField.setText(formatNumber(validator.getMin())); } + if (validator.getMax() != null + && validator.getMax().doubleValue() < d) + { + valueField.setText(formatNumber(validator.getMax())); + } + } catch (NumberFormatException e) + { + valueField.setBackground(Color.yellow); + return Float.NaN; } + if (isIntegerParameter) { int iVal = 0; try { - valueField.setText(valueField.getText().trim()); iVal = Integer.valueOf(valueField.getText()); - - /* - * ensure not outside min-max range - * TODO: provide some visual indicator if limit reached - */ - if (validator.getMin() != null - && validator.getMin().intValue() > iVal) - { - iVal = validator.getMin().intValue(); - valueField.setText(String.valueOf(iVal)); - } - if (validator.getMax() != null - && validator.getMax().intValue() < iVal) - { - iVal = validator.getMax().intValue(); - valueField.setText(String.valueOf(iVal)); - } } catch (Exception e) { - Cache.log.error(e.getMessage()); + valueField.setBackground(Color.yellow); + return Integer.valueOf(0); } if (validator.getMin() != null && validator.getMax() != null) @@ -791,43 +812,28 @@ public class OptsAndParamsPage } return new Integer(iVal); } - else if (isLogarithmicParameter) + + if (isLogarithmicParameter) { double dVal = 0d; try { - valueField.setText(valueField.getText().trim()); double eValue = Double.valueOf(valueField.getText()); - - dVal = Math.log(eValue) - / Math.log(((LogarithmicParameter) parameter).getBase()) - * LOGSLIDERSCALE; - - /* - * ensure not outside min-max range - * TODO: provide some visual indicator if limit reached - */ - if (validator.getMin() != null - && validator.getMin().doubleValue() > dVal) - { - dVal = validator.getMin().doubleValue(); - valueField.setText(formatDouble(eValue)); - } - if (validator.getMax() != null - && validator.getMax().doubleValue() < dVal) - { - dVal = validator.getMax().doubleValue(); - valueField.setText(formatDouble(eValue)); - } + dVal = Math.log(eValue) * sliderScaleFactor; } catch (Exception e) { + // shouldn't be possible here + valueField.setBackground(Color.yellow); + return Double.NaN; } - if (validator.getMin() != null && validator.getMax() != null) { + double scaleMin = Math.log(validator.getMin().doubleValue()) + * sliderScaleFactor; + double scaleMax = Math.log(validator.getMax().doubleValue()) + * sliderScaleFactor; slider.getModel().setRangeProperties((int) (dVal), 1, - (int) (validator.getMin().doubleValue()), - 1 + (int) (validator.getMax().doubleValue()), true); + (int) scaleMin, 1 + (int) scaleMax, true); } else { @@ -835,47 +841,30 @@ public class OptsAndParamsPage } return new Double(dVal); } + + float fVal = 0f; + try + { + fVal = Float.valueOf(valueField.getText()); + } catch (Exception e) + { + return Float.valueOf(0f); // shouldn't happen + } + if (validator.getMin() != null && validator.getMax() != null) + { + float scaleMin = validator.getMin().floatValue() + * sliderScaleFactor; + float scaleMax = validator.getMax().floatValue() + * sliderScaleFactor; + slider.getModel().setRangeProperties( + (int) (fVal * sliderScaleFactor), 1, (int) scaleMin, + 1 + (int) scaleMax, true); + } else { - float fVal = 0f; - try - { - valueField.setText(valueField.getText().trim()); - fVal = Float.valueOf(valueField.getText()); - - /* - * ensure not outside min-max range - * TODO: provide some visual indicator if limit reached - */ - if (validator.getMin() != null - && validator.getMin().floatValue() > fVal) - { - fVal = validator.getMin().floatValue(); - valueField.setText(String.valueOf(fVal)); - } - if (validator.getMax() != null - && validator.getMax().floatValue() < fVal) - { - fVal = validator.getMax().floatValue(); - valueField.setText(String.valueOf(fVal)); - } - } catch (Exception e) - { - } - - if (validator.getMin() != null && validator.getMax() != null) - { - slider.getModel().setRangeProperties((int) (fVal * SLIDERSCALE), - 1, (int) (validator.getMin().floatValue() * SLIDERSCALE), - 1 + (int) (validator.getMax().floatValue() * SLIDERSCALE), - true); - } - else - { - slider.setVisible(false); - } - return new Float(fVal); + slider.setVisible(false); } + return new Float(fVal); } } diff --git a/src/jalview/hmmer/HMMERParamStore.java b/src/jalview/hmmer/HMMERParamStore.java index 9c52f97..6f1f51f 100644 --- a/src/jalview/hmmer/HMMERParamStore.java +++ b/src/jalview/hmmer/HMMERParamStore.java @@ -156,8 +156,6 @@ public final class HMMERParamStore implements ParamDatastoreI */ args.add(new LogarithmicParameter( MessageManager.getString("label.dom_e_value"), - // MessageManager.getString("label.dom_e_value_desc"), false, -3d, - // -37.92977945, 1d, 10d)); MessageManager.getString("label.dom_e_value_desc"), false, 1D, 1E-38, 10D, 10D)); /* diff --git a/src/jalview/ws/params/simple/LogarithmicParameter.java b/src/jalview/ws/params/simple/LogarithmicParameter.java index b127183..01744e7 100644 --- a/src/jalview/ws/params/simple/LogarithmicParameter.java +++ b/src/jalview/ws/params/simple/LogarithmicParameter.java @@ -8,19 +8,16 @@ import jalview.ws.params.ValueConstrainI; * logarithmic scale * * @author TZVanaalten - * */ public class LogarithmicParameter extends Option implements ParameterI { - public static final double LOGSLIDERSCALE = 1000000D; - final double defval; final double min; final double max; - final double base; + final double base; // todo is this even needed? @Override public ValueConstrainI getValidValue() @@ -64,8 +61,8 @@ public class LogarithmicParameter extends Option implements ParameterI super(name, description, required, String.valueOf(defValue), null, null, null); defval = defValue; - this.min = min;// * LOGSLIDERSCALE; - this.max = max;// * LOGSLIDERSCALE; + this.min = min; + this.max = max; this.base = base; } @@ -76,8 +73,8 @@ public class LogarithmicParameter extends Option implements ParameterI super(name, description, required, String.valueOf(defValue), String.valueOf(value), null, null); defval = defValue; - this.min = min;// * LOGSLIDERSCALE; - this.max = max;// * LOGSLIDERSCALE; + this.min = min; + this.max = max; this.base = base; } -- 1.7.10.2