@Test public void testGetSpectrum() { IDataset spectrum = gridScanMap.getSpectrum(0, 0); Dataset d = DatasetUtils.convertToDataset(spectrum); assertEquals(d.getElementDoubleAbs(0), 0, 0); assertEquals(d.getElementDoubleAbs(d.getSize() - 1), d.getSize() - 1, 0); }
@Override public DataReductionInfo export(DataReductionSlice drslice) throws Exception { final Collection<IRegion> regions = getPlottingSystem().getRegions(); for (IRegion region : regions) { if (!isRegionTypeSupported(region.getRegionType())) continue; final RectangularROI bounds = (RectangularROI) region.getROI(); if (bounds == null) continue; if (!region.isVisible()) continue; final int yInc = bounds.getPoint()[1] < bounds.getEndPoint()[1] ? 1 : -1; final int xInc = bounds.getPoint()[0] < bounds.getEndPoint()[0] ? 1 : -1; final Dataset slice = DatasetUtils.convertToDataset( drslice .getData() .getSlice( new int[] {(int) bounds.getPoint()[1], (int) bounds.getPoint()[0]}, new int[] {(int) bounds.getEndPoint()[1], (int) bounds.getEndPoint()[0]}, new int[] {yInc, xInc})); slice.setName(region.getName().replace(' ', '_')); drslice.appendData(slice); } return new DataReductionInfo(Status.OK_STATUS); }
protected Dataset createZoom( final IImageTrace image, IRegion region, IROI rbs, boolean tryUpdate, boolean isDrag, IProgressMonitor monitor) { if (!(region.getROI() instanceof RectangularROI)) return null; final RectangularROI bounds = (RectangularROI) (rbs == null ? region.getROI() : rbs); if (bounds == null) return null; if (!region.isVisible()) return null; if (monitor.isCanceled()) return null; final int yInc = bounds.getPoint()[1] < bounds.getEndPoint()[1] ? 1 : -1; final int xInc = bounds.getPoint()[0] < bounds.getEndPoint()[0] ? 1 : -1; Dataset im = DatasetUtils.convertToDataset(image.getData()); Dataset slice = DatasetUtils.convertToDataset(ToolUtils.getClippedSlice(im, bounds)); slice.setName(region.getName()); // Calculate axes to have real values not size Dataset yLabels = null; Dataset xLabels = null; if (image.getAxes() != null && image.getAxes().size() > 0) { Dataset xl = DatasetUtils.convertToDataset(image.getAxes().get(0)); if (xl != null) xLabels = ZoomTool.getLabelsFromLabels(xl, bounds, 0); Dataset yl = DatasetUtils.convertToDataset(image.getAxes().get(1)); if (yl != null) yLabels = ZoomTool.getLabelsFromLabels(yl, bounds, 1); } if (yLabels == null) yLabels = DatasetFactory.createRange( IntegerDataset.class, bounds.getPoint()[1], bounds.getEndPoint()[1], yInc); if (xLabels == null) xLabels = DatasetFactory.createRange( IntegerDataset.class, bounds.getPoint()[0], bounds.getEndPoint()[0], xInc); final IImageTrace zoom_trace = (IImageTrace) profilePlottingSystem.updatePlot2D( slice, Arrays.asList(new IDataset[] {xLabels, yLabels}), monitor); registerTraces(region, Arrays.asList(new ITrace[] {zoom_trace})); Display.getDefault() .syncExec( new Runnable() { public void run() { zoom_trace.setPaletteData(image.getPaletteData()); } }); return slice; }
/** * Get the logged image value. * * @param bean * @return a dataset that can be absolute, if complex, and also be logged according to bean * Package private for testing */ /* package */ Dataset getImageLoggedDataCalc(ImageServiceBean bean) { Dataset ret = DatasetUtils.convertToDataset(bean.getImage()); if (ret.isComplex()) { ret = Maths.abs(ret); } if (bean.isLogColorScale()) { double offset = bean.getLogOffset(); if (!Double.isNaN(offset) && !Double.isInfinite(offset)) { ret = Maths.subtract(ret, offset); } ret = Maths.log10(ret); } return ret; }
private void checkMetadata(NXentry entry, List<ScanMetadata> scanMetadataList) { for (ScanMetadata scanMetadata : scanMetadataList) { MetadataType type = scanMetadata.getType(); NXobject object = getNexusObjectForMetadataType(entry, type); Map<String, Object> metadataFields = scanMetadata.getFields(); for (String metadataFieldName : metadataFields.keySet()) { Object expectedValue = scanMetadata.getFieldValue(metadataFieldName); Dataset dataset = DatasetUtils.convertToDataset(object.getDataset(metadataFieldName)); assertNotNull(dataset); assertEquals(1, dataset.getSize()); assertEquals(DTypeUtils.getDTypeFromObject(expectedValue), dataset.getDType()); assertEquals(expectedValue, dataset.getObjectAbs(0)); } } }
protected OperationData process(IDataset input, IMonitor monitor) throws OperationException { double d = (2 * Math.PI) / model.getqValue(); double p = (2 * Math.PI) / (model.getqValue() + model.getqDelta()); double m = (2 * Math.PI) / (model.getqValue() - model.getqDelta()); IDiffractionMetadata dm = getFirstDiffractionMetadata(input); if (dm == null) throw new OperationException(this, "No calibration information!"); IParametricROI[] inOut = new IParametricROI[2]; IParametricROI conic = (IParametricROI) DSpacing.conicFromDSpacing( dm.getDetector2DProperties(), dm.getDiffractionCrystalEnvironment(), d); inOut[0] = (IParametricROI) DSpacing.conicFromDSpacing( dm.getDetector2DProperties(), dm.getDiffractionCrystalEnvironment(), m); inOut[1] = (IParametricROI) DSpacing.conicFromDSpacing( dm.getDetector2DProperties(), dm.getDiffractionCrystalEnvironment(), p); PolylineROI points = PeakFittingEllipseFinder.findPointsOnConic( DatasetUtils.convertToDataset(input), null, conic, inOut, 256, null); double rms = -1; double[] semi = new double[2]; double[] point = new double[2]; double ang = 0; if (points != null && points.getNumberOfPoints() < 3) { EllipticalFitROI efroi = PowderRingsUtils.fitAndTrimOutliers(null, points, 2, false); rms = efroi.getRMS(); semi = efroi.getSemiAxes(); point = efroi.getPoint(); ang = efroi.getAngleDegrees(); } Dataset r = DatasetFactory.createFromObject(new double[] {rms}); r.setName("rms"); Dataset ax = DatasetFactory.createFromObject(semi); ax.setName("semi-axes"); Dataset po = DatasetFactory.createFromObject(point); po.setName("centre"); Dataset a = DatasetFactory.createFromObject(new double[] {ang}); a.setName("angle"); return new OperationData(input, new Serializable[] {r, ax, po, a}); }
static Dataset getLabelsFromLabels(Dataset xl, RectangularROI bounds, int axisIndex) { try { int fromIndex = (int) bounds.getPoint()[axisIndex]; int toIndex = (int) bounds.getEndPoint()[axisIndex]; int step = toIndex > fromIndex ? 1 : -1; final Dataset slice = xl.getSlice(new int[] {fromIndex}, new int[] {toIndex}, new int[] {step}); return slice; } catch (Exception ne) { return null; } }
/** * - * @param axes specify real and imaginary coordinates as two 1d datasets * * @return a list containing a dataset of recursion limits */ @Override public List<Dataset> value(IDataset... axes) { Dataset xaxis, yaxis; if (axes.length < 2) { throw new IllegalArgumentException("Need two axes"); } xaxis = DatasetUtils.convertToDataset(axes[0]); yaxis = DatasetUtils.convertToDataset(axes[1]); if (xaxis.getRank() != 1 || yaxis.getRank() != 1) { throw new IllegalArgumentException("Need both axes to be 1d datasets"); } IntegerDataset count = DatasetFactory.zeros(IntegerDataset.class, yaxis.getShapeRef()[0], xaxis.getShapeRef()[0]); List<Dataset> result = new ArrayList<Dataset>(); final IndexIterator iy = yaxis.getIterator(); int n = 0; while (iy.hasNext()) { final double iv = yaxis.getElementDoubleAbs(iy.index); final IndexIterator ix = xaxis.getIterator(); while (ix.hasNext()) { final double rv = xaxis.getElementDoubleAbs(ix.index); double x = 0, y = 0; int c = -1; do { double t = x * x - y * y + rv; y = 2. * x * y + iv; x = t; } while (++c < maxRecursion && x * x + y * y <= 4.); count.setAbs(n++, c); } } result.add(count); return result; }
@Override protected OperationData process(IDataset input, IMonitor monitor) { RectangularROI box = model.getBox(); Dataset in1 = BoxSlicerRodScanUtils.rOIBox(input, monitor, box.getIntLengths(), box.getIntPoint()); if (g2 == null) g2 = new Polynomial2D(model.getFitPower()); if ((int) Math.pow(model.getFitPower() + 1, 2) != g2.getNoOfParameters()) g2 = new Polynomial2D(model.getFitPower()); Dataset[] fittingBackground = BoxSlicerRodScanUtils.LeftRightTopBottomBoxes( input, monitor, box.getIntLengths(), box.getIntPoint(), model.getBoundaryBox()); Dataset offset = DatasetFactory.ones(fittingBackground[2].getShape(), Dataset.FLOAT64); Dataset intermediateFitTest = Maths.add(offset, fittingBackground[2]); Dataset matrix = LinearLeastSquaresServicesForSXRD.polynomial2DLinearLeastSquaresMatrixGenerator( model.getFitPower(), fittingBackground[0], fittingBackground[1]); DoubleDataset test = (DoubleDataset) LinearAlgebra.solveSVD(matrix, intermediateFitTest); double[] params = test.getData(); DoubleDataset in1Background = g2.getOutputValues0( params, box.getIntLengths(), model.getBoundaryBox(), model.getFitPower()); IndexIterator it = in1Background.getIterator(); while (it.hasNext()) { double v = in1Background.getElementDoubleAbs(it.index); if (v < 0) in1Background.setObjectAbs(it.index, 0); } Dataset pBackgroundSubtracted = Maths.subtract(in1, in1Background, null); pBackgroundSubtracted.setName("pBackgroundSubtracted"); IndexIterator it1 = pBackgroundSubtracted.getIterator(); while (it1.hasNext()) { double q = pBackgroundSubtracted.getElementDoubleAbs(it1.index); if (q < 0) pBackgroundSubtracted.setObjectAbs(it1.index, 0); } Dataset output = DatasetUtils.cast(pBackgroundSubtracted, Dataset.FLOAT64); output.setName("Region of Interest, polynomial background removed"); return new OperationData(output); }
/** * @param datasets input 2D dataset * @return one 2D dataset */ @Override public List<Dataset> value(IDataset... datasets) { if (datasets.length == 0) return null; List<Dataset> result = new ArrayList<Dataset>(); for (IDataset ids : datasets) { Dataset ds = DatasetUtils.convertToDataset(ids); // check if input is 2D int[] s = ds.getShape(); if (s.length != 2) return null; // find extent of projected dataset int[] pos = new int[2]; int[] start = pshape.clone(); int[] stop = new int[2]; qToPixel(qspace.qFromPixelPosition(0, 0), pos); adjustBoundingBox(start, stop, pos); qToPixel(qspace.qFromPixelPosition(s[0], 0), pos); adjustBoundingBox(start, stop, pos); qToPixel(qspace.qFromPixelPosition(0, s[1]), pos); adjustBoundingBox(start, stop, pos); qToPixel(qspace.qFromPixelPosition(s[0], s[1]), pos); adjustBoundingBox(start, stop, pos); stop[0]++; stop[1]++; // iterate over bounding box in project dataset // find image point // calculate interpolated value // store running average // (could have done this using a slice iterator) Vector3d qy = new Vector3d(); Vector3d q = new Vector3d(); Vector3d t = new Vector3d(); double delta, value; int i = 0, n; for (int y = start[0]; y < stop[0]; y++) { qy.scale((y + roff) * cdel, col); for (int x = start[1]; x < stop[1]; x++) { q.scaleAdd((x + coff) * rdel, row, qy); qspace.pixelPosition(q, t); n = (int) (count.getElementLongAbs(i) + 1); value = image.getElementDoubleAbs(i); delta = Maths.interpolate(ds, t.y, t.x) - value; image.setObjectAbs(i, value + (delta / n)); count.setObjectAbs(i, n); i++; } } result.add(image); result.add(count); } return result; }
/** * Perform a two-dimensional interpolation * * @param oldx an IDataset containing a 1D array of X-values, sorted in increasing order, * corresponding to the first dimension of <code>oldxy</code> * @param oldy an IDataset containing a 1D array of Y-values, sorted in increasing order, * corresponding to the second dimension of <code>oldxy</code> * @param oldxy an IDataset containing a 2D grid of interpolation points * @param newx an IDataset containing a 1D array of X-values that will be sent to the * interpolating function * @param newy an IDataset containing a 1D array of Y-values that will be sent to the * interpolating function * @param interpolator an instance of {@link * org.apache.commons.math3.analysis.interpolation.BivariateGridInterpolator} * @param output_type an {@link BicubicInterpolationOutput} that will determine how <code>newx * </code> and <code>newy</code> will be interpreted, and therefore whether a 1D or 2D Dataset * will be returned. * @return rank 1 or 2 Dataset, depending on <code>output_type}</code> * @throws NonMonotonicSequenceException * @throws NumberIsTooSmallException */ public static Dataset interpolate( IDataset oldx, IDataset oldy, IDataset oldxy, IDataset newx, IDataset newy, BivariateGridInterpolator interpolator, BicubicInterpolationOutput output_type) throws NonMonotonicSequenceException, NumberIsTooSmallException { // check shapes if (oldx.getRank() != 1) throw new IllegalArgumentException("oldx Shape must be 1D"); if (oldy.getRank() != 1) throw new IllegalArgumentException("oldy Shape must be 1D"); if (oldxy.getRank() != 2) throw new IllegalArgumentException("oldxy Shape must be 2D"); if (oldx.getShape()[0] != oldxy.getShape()[0]) throw new IllegalArgumentException("oldx Shape must match oldxy Shape[0]"); if (oldy.getShape()[0] != oldxy.getShape()[1]) throw new IllegalArgumentException("oldy Shape must match oldxy Shape[1]"); if (newx.getRank() != 1) throw new IllegalArgumentException("newx Shape must be 1D"); if (newy.getRank() != 1) throw new IllegalArgumentException("newx Shape must be 1D"); if (output_type == BicubicInterpolationOutput.ONED && newy.getSize() != newx.getSize()) throw new IllegalArgumentException( "newx and newy Size must be identical when expecting a rank 1 dataset result"); DoubleDataset oldx_dd = (DoubleDataset) DatasetUtils.cast(oldx, Dataset.FLOAT64); DoubleDataset oldy_dd = (DoubleDataset) DatasetUtils.cast(oldy, Dataset.FLOAT64); DoubleDataset oldxy_dd = (DoubleDataset) DatasetUtils.cast(oldxy, Dataset.FLOAT64); // unlike in Interpolation1D, we will not be sorting here, as it just too complicated // the user will be responsible for ensuring the arrays are properly sorted // oldxy_dd needs to be transformed into a double[][] array // this call may throw an exception that needs handling by the calling method BivariateFunction func = interpolator.interpolate( oldx_dd.getData(), oldy_dd.getData(), convertDoubleDataset2DtoPrimitive(oldxy_dd)); Dataset rv = null; if (output_type == BicubicInterpolationOutput.ONED) { rv = DatasetFactory.zeros(new int[] {newx.getSize()}, Dataset.FLOAT64); for (int i = 0; i < newx.getSize(); i++) { double val = 0.0; try { val = func.value(newx.getDouble(i), newy.getDouble(i)); rv.set(val, i); } catch (OutOfRangeException e) { rv.set(0.0, i); } } } else if (output_type == BicubicInterpolationOutput.TWOD) { rv = DatasetFactory.zeros(new int[] {newx.getSize(), newy.getSize()}, Dataset.FLOAT64); for (int i = 0; i < newx.getSize(); i++) { for (int j = 0; j < newy.getSize(); j++) { double val = 0.0; try { val = func.value(newx.getDouble(i), newy.getDouble(j)); rv.set(val, i, j); } catch (OutOfRangeException e) { rv.set(0.0, i, j); } } } } rv.setName(oldxy.getName() + "_interpolated"); return rv; }
@Override protected Collection<? extends ITrace> createProfile( final IImageTrace image, IRegion region, IROI rbs, boolean tryUpdate, boolean isDrag, IProgressMonitor monitor) { if (monitor.isCanceled()) return null; if (image == null) return null; if ((region.getRegionType() != RegionType.BOX) && (region.getRegionType() != RegionType.PERIMETERBOX)) return null; Dataset slice = createZoom(image, region, rbs, tryUpdate, isDrag, monitor); Dataset yData = slice.sum(0); yData.setName("Intensity"); Dataset xData = slice.sum(1); xData.setName("Intensity"); final RectangularROI bounds = (RectangularROI) (rbs == null ? region.getROI() : rbs); final Dataset y_indices = DatasetFactory.createRange( bounds.getPoint()[0], bounds.getPoint()[0] + bounds.getLength(0), 1, Dataset.FLOAT); y_indices.setName("X Location"); topSystem.updatePlot1D(y_indices, Arrays.asList(new IDataset[] {yData}), monitor); topSystem.repaint(); final Dataset x_indices = DatasetFactory.createRange( bounds.getPoint()[1] + bounds.getLength(1), bounds.getPoint()[1], -1, Dataset.FLOAT); x_indices.setName("Y Location"); final Collection<ITrace> right = rightSystem.updatePlot1D(xData, Arrays.asList(new IDataset[] {x_indices}), monitor); rightSystem.repaint(); Display.getDefault() .syncExec( new Runnable() { @Override public void run() { topSystem.setTitle(""); rightSystem.setTitle(""); ILineTrace line = (ILineTrace) right.iterator().next(); line.setTraceColor(ColorConstants.red); } }); return profilePlottingSystem.getTraces(); }
/** * getImageData(...) provides an image in a given palette data and origin. Faster than getting a * resolved image * * <p>This method should be thread safe. */ public ImageData getImageData(ImageServiceBean bean) { ImageOrigin origin = bean.getOrigin(); if (origin == null) origin = ImageOrigin.TOP_LEFT; // orientate the image Dataset oImage = DatasetUtils.rotate90(DatasetUtils.convertToDataset(bean.getImage()), origin.ordinal()); Dataset image = oImage; if (image instanceof RGBDataset) { return SWTImageUtils.createImageData( (RGBDataset) image, 0, 255, null, null, null, false, false, false); } createMaxMin(bean); double max = getMax(bean); double min = getMin(bean); double maxCut = getMaxCut(bean); double minCut = getMinCut(bean); // now deal with the log if needed if (bean.isLogColorScale()) { image = DatasetUtils.rotate90(getImageLoggedData(bean), origin.ordinal()); max = Math.log10(max); // note createMaxMin() -> getFastStatistics() -> getImageLogged() which ensures min >= 0 min = Math.log10(min); maxCut = Math.log10(maxCut); // no guarantees for minCut though minCut = minCut <= 0 ? Double.NEGATIVE_INFINITY : Math.log10(minCut); } if (oImage.isComplex()) { // handle complex datasets by creating RGB dataset Dataset hue = Maths.angle(oImage, true); Dataset value = DatasetUtils.rotate90(getImageLoggedData(bean), origin.ordinal()); double maxmax = Math.max(Math.abs(max), Math.abs(min)); if (max - min > Math.ulp(maxmax)) { value.isubtract(min); value.imultiply(1. / (max - min)); } else { value.imultiply(1. / maxmax); } image = RGBDataset.createFromHSV(hue, null, value); return SWTImageUtils.createImageData(image, 0, 255, null, null, null, false, false, false); } if (bean.getFunctionObject() != null && bean.getFunctionObject() instanceof FunctionContainer) { final FunctionContainer fc = (FunctionContainer) bean.getFunctionObject(); // TODO This does not support masking or cut bounds for zingers and dead pixels. return SWTImageUtils.createImageData( image, min, max, fc.getRedFunc(), fc.getGreenFunc(), fc.getBlueFunc(), fc.isInverseRed(), fc.isInverseGreen(), fc.isInverseBlue()); } return SWTImageUtils.createImageData(min, max, minCut, maxCut, image, bean); }
/** * 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}; }
public void clearDataset() { image.fill(0); count.fill(0); }
public void setY(final Dataset y) { this.min = y.min().doubleValue(); this.max = y.min().doubleValue(); }