@Override public ImageServiceBean createBeanFromPreferences() { ImageServiceBean imageServiceBean = new ImageServiceBean(); if (Platform.getPreferencesService() != null) { // Normally IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, "org.dawnsci.plotting"); imageServiceBean.setOrigin( ImageOrigin.forLabel(store.getString(BasePlottingConstants.ORIGIN_PREF))); imageServiceBean.setHistogramType( HistoType.forLabel(store.getString(BasePlottingConstants.HISTO_PREF))); imageServiceBean.setMinimumCutBound( HistogramBound.fromString(store.getString(BasePlottingConstants.MIN_CUT))); imageServiceBean.setMaximumCutBound( HistogramBound.fromString(store.getString(BasePlottingConstants.MAX_CUT))); imageServiceBean.setNanBound( HistogramBound.fromString(store.getString(BasePlottingConstants.NAN_CUT))); imageServiceBean.setLo(store.getDouble(BasePlottingConstants.HISTO_LO)); imageServiceBean.setHi(store.getDouble(BasePlottingConstants.HISTO_HI)); try { IPaletteService pservice = ServiceLoader.getPaletteService(); if (pservice != null) { final String scheme = store.getString(BasePlottingConstants.COLOUR_SCHEME); if (store.getBoolean(BasePlottingConstants.USE_PALETTE_FUNCTIONS)) { FunctionContainer container = pservice.getFunctionContainer(scheme); if (container != null) { imageServiceBean.setFunctionObject(container); } else { imageServiceBean.setPalette(pservice.getDirectPaletteData(scheme)); } } else { // if 8-bit, set direct palette, otherwise set palette functions. PaletteData pd; try { pd = pservice.getDirectPaletteData(scheme); } catch (final IllegalArgumentException e) { // scheme does not exist final String defaultScheme = pservice.getColorSchemes().iterator().next(); pd = pservice.getDirectPaletteData(defaultScheme); store.setValue(BasePlottingConstants.COLOUR_SCHEME, defaultScheme); } imageServiceBean.setPalette(pd); } } } catch (Exception e) { // Ignored } } else { // Hard code something imageServiceBean.setOrigin(ImageOrigin.TOP_LEFT); imageServiceBean.setHistogramType(HistoType.OUTLIER_VALUES); imageServiceBean.setMinimumCutBound(HistogramBound.DEFAULT_MINIMUM); imageServiceBean.setMaximumCutBound(HistogramBound.DEFAULT_MAXIMUM); imageServiceBean.setNanBound(HistogramBound.DEFAULT_NAN); imageServiceBean.setLo(00.01); imageServiceBean.setHi(99.99); imageServiceBean.setPalette(makeJetPalette()); } return imageServiceBean; }
/** * Fast statistics as a rough guide - this is faster than Dataset.getMin() and getMax() which may * cache but slows the opening of images too much. The return array[2] was added in "Updated for * Diffraction Tool." commit, but no trace of such usage. However it should not be removed, * because it is useful as return array[3]. * * @param bean * @return [0] = min [1] = max(=mean*constant) [2] = mean [3] max */ public double[] getFastStatistics(ImageServiceBean bean) { Dataset image = getImageLoggedData(bean); if (bean.getHistogramType() == HistoType.OUTLIER_VALUES && !bean.isLogColorScale()) { double[] ret = null; try { double[] stats = Stats.outlierValues(image, bean.getLo(), bean.getHi(), -1); ret = new double[] {stats[0], stats[1], -1}; } catch (IllegalArgumentException iae) { bean.setLo(10); bean.setHi(90); double[] stats = Stats.outlierValues(image, bean.getLo(), bean.getHi(), -1); ret = new double[] {stats[0], stats[1], -1}; } if (bean.isLogColorScale() && ret != null) { ret = new double[] {Math.pow(10, ret[0]), Math.pow(10, ret[1]), -1}; } return ret; } double min = Double.MAX_VALUE; double max = -Double.MAX_VALUE; double sum = 0.0; int size = 0; BooleanDataset mask = bean.getMask() != null ? (BooleanDataset) DatasetUtils.cast(bean.getMask(), Dataset.BOOL) : null; // Big loop warning: final IndexIterator it = image.getIterator(); final IndexIterator mit = mask == null ? null : mask.getIterator(); while (it.hasNext()) { final double val = image.getElementDoubleAbs(it.index); if (mit != null && mit.hasNext()) { if (!mask.getElementBooleanAbs(mit.index)) { continue; // Masked! } } if (Double.isNaN(val)) continue; if (!bean.isInBounds(val)) continue; sum += val; if (val < min) min = val; if (val > max) max = val; size++; } double retMax = Double.NaN; double retExtra = Double.NaN; if (bean.getHistogramType() == HistoType.MEDIAN) { double median = Double.NaN; try { median = ((Number) Stats.median(image)).doubleValue(); // SLOW } catch (Exception ne) { median = ((Number) Stats.median(image.cast(Dataset.INT16))).doubleValue(); // SLOWER } retMax = 2 * median; retExtra = median; } else { // Use mean based histo double mean = sum / size; retMax = (Math.E) * mean; // Not statistical, E seems to be better than 3... retExtra = mean; } if (retMax > max) retMax = max; if (bean.isLogColorScale()) { return new double[] {Math.pow(10, min), Math.pow(10, retMax), Math.pow(10, retExtra)}; } return new double[] {min, retMax, retExtra, max}; }