Example usage for java.util NavigableMap lastKey

List of usage examples for java.util NavigableMap lastKey

Introduction

In this page you can find the example usage for java.util NavigableMap lastKey.

Prototype

K lastKey();

Source Link

Document

Returns the last (highest) key currently in this map.

Usage

From source file:sadl.modellearner.rtiplus.SimplePDRTALearner.java

/**
 * Calculates the maximum allowed size for an empty interval part when only few {@link TimedTail}s use this interval. The allowed size depends on the
 * parameter for the minimum amount of {@link TimedTail}s and the distance between the occupied slots.
 * /*from  www.  j  av  a  2s .c  o m*/
 * @param minData
 *            The minimum amount of {@link TimedTail}s
 * @return The maximum allowed size for an empty interval part
 */
private int getToleranceFewSlots(Interval in, int minData) {

    final NavigableMap<Integer, Collection<TimedTail>> tails = in.getTails().asMap();
    final int slots = tails.size();
    assert (slots > 0 && slots <= 2);
    if (slots == 1) {
        final int size = tails.firstEntry().getValue().size();
        if (size < (minData / 2.0)) {
            return (int) Math.ceil((in.getEnd() - in.getBegin() + 1) * 0.05);
        } else {
            return 0;
        }
    } else {
        final Integer t1Int = tails.firstKey();
        final int s1 = tails.get(t1Int).size();
        final Integer t2Int = tails.lastKey();
        final int s2 = tails.get(t2Int).size();
        final int t1 = t1Int.intValue();
        final int t2 = t2Int.intValue();
        final double perc = (double) (t2 - t1 - 1) / (double) (in.getEnd() - in.getBegin() - 1);
        if (s1 >= minData && s2 >= minData && perc >= 0.2) {
            return (int) Math.ceil((in.getEnd() - in.getBegin() + 1) * 0.05);
        } else if ((s1 >= minData || s2 >= minData) && perc >= 0.2) {
            return (int) Math.ceil((in.getEnd() - in.getBegin() + 1) * 0.075);
        } else {
            return (int) Math.ceil((t2 - t1 - 1) / 2.0);
        }
    }
}

From source file:umich.ms.batmass.gui.viewers.map2d.components.BaseMap2D.java

/**
 * Fills the map given a scan collection.
 * @param scans//w ww. ja v a  2s.com
 * @return True, if filling was done successfully.
 *         False if something bad happened, e.g. scanCollection didn't contain any scans between rtStart & rtEnd
 */
public boolean fillMapFromScans(IScanCollection scans) {
    int pixelsVertical = availableHeight;
    height = pixelsVertical;
    width = availableWidth;

    NavigableMap<Integer, IScan> scansByRtSpanAtMsLevel = scans.getScansByRtSpanAtMsLevel(rtLo, rtHi, msLevel);
    ;
    if (!precursorMzRange.equals(Map2DPanel.OPT_DISPLAY_ALL_MZ_REGIONS)) {
        // if only scans from specific precursor m/z window were requested
        IntervalST<Double, TreeMap<Integer, IScan>> precursorRanges = scans.getMapMsLevel2rangeGroups()
                .get(msLevel);
        if (precursorRanges != null) {
            IntervalST.Node<Double, TreeMap<Integer, IScan>> node = precursorRanges.get(precursorMzRange);
            if (node != null) {
                // these are all the scans at proper MS level and in proper precursor m/z range
                TreeMap<Integer, IScan> scansInMzRange = node.getValue();
                // now filter this TreeMap to only leave scans that are in our RT range
                Integer numLo = scansByRtSpanAtMsLevel.firstKey();
                Integer numHi = scansByRtSpanAtMsLevel.lastKey();
                numLo = scansInMzRange.ceilingKey(numLo);
                numHi = scansInMzRange.floorKey(numHi);
                scansByRtSpanAtMsLevel = scansInMzRange.subMap(numLo, true, numHi, true);
            }
        }
    }
    if (scansByRtSpanAtMsLevel == null || scansByRtSpanAtMsLevel.size() == 0) {
        initErrorFillingState();
        return false;
    }
    scanNumLo = scansByRtSpanAtMsLevel.firstKey();
    scanNumHi = scansByRtSpanAtMsLevel.lastKey();

    // compare the number of scans to available vertical pixels
    int scanCount = scansByRtSpanAtMsLevel.size();
    this.map = new double[height][width];
    this.mapRaw = new double[height][width];
    this.maxValInFullRow = new double[height];

    IScan scan;
    TreeMap<Integer, IScan> mapNum2scan = scans.getMapNum2scan();
    IScan[] scansToAverage = new IScan[4];
    ISpectrum spectrum;
    Integer mzIdxLo, mzIdxHi;
    int x, y;
    boolean hasProfile = true;
    double[] masses, intensities;
    filledRowIds = new int[scansByRtSpanAtMsLevel.size()];
    int idx = 0;
    double denoisingTimeCounter = 0;
    for (Map.Entry<Integer, IScan> num2scan : scansByRtSpanAtMsLevel.entrySet()) {
        scan = num2scan.getValue();
        if (doProfileModeGapFilling && !scan.isCentroided()) {
            hasProfile = true;
        }
        spectrum = null;
        try {
            spectrum = scan.fetchSpectrum();
        } catch (FileParsingException ex) {
            Exceptions.printStackTrace(ex);
        }
        if (spectrum == null) {
            continue;
        }

        y = extrapolateRtToY(scan.getRt());
        filledRowIds[idx] = y;
        idx++;
        if (y > this.map.length - 1) {
            OutputWndPrinter.printErr("DEBUG", String.format(
                    "BaseMap2D: (y > this.map.length-1) for scan #%d.\n" + "\ty=%d, len-1=%d, height=%d\n"
                            + "\trt=%.20f, rtStart=%.20f, rtEnd=%.20f, rtSpan=%.20f",
                    scan.getNum(), y, this.map.length - 1, height, scan.getRt(), rtLo, rtHi, rtSpan));
        }
        masses = spectrum.getMZs();
        intensities = spectrum.getIntensities();

        mzIdxLo = spectrum.findMzIdxCeiling(mzLo);
        mzIdxHi = spectrum.findMzIdxFloor(mzHi);
        if (mzIdxLo == null || mzIdxHi == null) {
            OutputWndPrinter.printErr("DEBUG", String.format(
                    "BaseMap2D: mzIdxLo or mzIdxHi were null for scan #%d. " + "Not filling the map from them.",
                    scan.getNum()));
            continue;
        }
        if (mzIdxLo < 0 || mzIdxLo > masses.length - 1) {
            OutputWndPrinter.printErr("DEBUG", String.format(
                    "BaseMap2D: (mzIdxLo < 0 || mzIdxLo > masses.length-1) for scan #%d", scan.getNum()));
        }
        if (mzIdxHi < 0 || mzIdxHi > masses.length - 1) {
            OutputWndPrinter.printErr("DEBUG", String.format(
                    "BaseMap2D: (mzIdxHi < 0 || mzIdxHi > masses.length-1) for scan #%d", scan.getNum()));
        }

        double denoiseThreshold = Double.NaN;
        boolean applyDenoise = isDoDenoise();
        if (applyDenoise) {
            long start = System.nanoTime();
            denoiseThreshold = findDenoiseThreshold(masses, intensities);
            double denoisingTime = (System.nanoTime() - start) / 1e6;
            denoisingTimeCounter = denoisingTimeCounter + denoisingTime;
            if (Double.isNaN(denoiseThreshold)) {
                applyDenoise = false;
            }
        }

        double maxInt = spectrum.getMaxInt();
        for (int i = mzIdxLo; i <= mzIdxHi; i++) {

            x = extrapolateMzToX(masses[i]);
            addPeakRaw(x, y, intensities[i]);

            if (applyDenoise && intensities[i] < denoiseThreshold) {
                continue;
            }
            if (x > this.map[0].length - 1) {
                OutputWndPrinter.printErr("DEBUG", String.format(
                        "BaseMap2D: (x > this.map[0].length-1) for scan #%d.\n"
                                + "\tx=%d, len-1=%d, width=%d,\n"
                                + "\ti=%d, masses[i]=%.20f, mzStart=%.20f, mzEnd=%.20f, mzSpan=%.20f",
                        scan.getNum(), x, this.map[0].length - 1, width, i, masses[i], mzLo, mzHi, mzSpan));
            }

            // boost if present in previous/next scan
            // boost if present in previous/next scan// boost if present in previous/next scan// boost if present in previous/next scan// boost if present in previous/next scan// boost if present in previous/next scan
            //                double curIntensity = intensities[i];
            //                final int maxScanSpan = 2000;
            //                int numScansDisplayed = scansByRtSpanAtMsLevel.size();
            //                if (false && numScansDisplayed <= maxScanSpan) {
            //                    double maxIntInVicinity;
            //                    double intensityUpdateFactor = 1;
            //                    double dm, dmPpm, dmUpdateFactor;
            //                    int maxIntIdx;
            //                    double[] curInts, curMzs;
            //
            //                    final int scanNumShift = 1;
            //                    final double ppmTolerance = 15d;
            //
            //                    if (scan.getNum() % 1000 == 0) {
            //                        System.out.printf("Averaging for scan %d\n", scan.getNum());
            //                    }
            //                    scansToAverage[0] = mapNum2scan.get(scan.getNum() - scanNumShift*2);
            //                    scansToAverage[1] = mapNum2scan.get(scan.getNum() - scanNumShift);
            //                    scansToAverage[2] = mapNum2scan.get(scan.getNum() + scanNumShift);
            //                    scansToAverage[3] = mapNum2scan.get(scan.getNum() + scanNumShift*2);
            //                    double curMass = masses[i];
            //
            //                    for (IScan avgScan : scansToAverage) {                        
            //                        if (avgScan == null) {
            //                            continue;
            //                        }
            //                        ISpectrum s = avgScan.getSpectrum();
            //                        if (s == null) {
            //                            continue;
            //                        }
            //                        int[] mzIdxs = s.findMzIdxsWithinPpm(curMass, ppmTolerance);
            //                        dm = Double.NEGATIVE_INFINITY;
            //                        dmUpdateFactor = 1;
            //                        intensityUpdateFactor = 1;
            //                        if (mzIdxs != null) {
            //                            curInts = s.getIntensities();
            //                            curMzs = s.getMZs();
            //                            maxIntIdx = -1;
            //                            maxIntInVicinity = Double.NEGATIVE_INFINITY;
            //                            for (int j = mzIdxs[0]; j <= mzIdxs[1]; j++) {
            //                                if (curInts[j] > maxIntInVicinity) {
            //                                    maxIntIdx = j;
            //                                }
            //                            }
            //                            if (maxIntIdx != -1) {
            //                                intensityUpdateFactor = curInts[maxIntIdx];
            //                                dm = Math.abs(curMass - curMzs[maxIntIdx]);
            //
            //                                dmPpm = dm / (curMass / 1e6d);
            //                                if (dmPpm > ppmTolerance) {
            //                                    dmUpdateFactor = 0d;
            //                                    throw new IllegalStateException("dmUpdateFactor set to zero, should not happen");
            //                                } else {
            //                                    dmUpdateFactor = (1 - Math.pow(dmPpm / ppmTolerance, 2d));
            //                                }
            //                            } else {
            //                                throw new IllegalStateException("Strange condition, should never be triggered");
            //                            }
            //                        } else {
            //                            // if masses in the vicinity not found, then penalize
            //                            // TODO: this should be dependent on the chosen distribution for mass deviations
            //                            //       see dmFactor
            //                            intensityUpdateFactor = 1;
            //                            dmUpdateFactor = (1 - Math.pow(0.5d, 2d));
            //                        }
            //                        
            //                        curIntensity = curIntensity * (intensityUpdateFactor * dmUpdateFactor);
            //                    }
            //                }

            //                addPeak(x, y, curIntensity);
            addPeak(x, y, intensities[i]);
            maxValInFullRow[y] = maxInt;
            //                if (curIntensity > 1e6) {
            //                    addPeak(x, y, curIntensity);
            //                }

        }

        if (hasProfile && doProfileModeGapFilling) {
            double pixelSizeMz = getMzSpan() / availableWidth;
            if (pixelSizeMz < 0.05) {
                fillProfileGaps(0, y, pixelSizeMz);
            }
        }
    }
    if (isDoDenoise()) {
        OutputWndPrinter.printErr("DEBUG", String.format("Denoising took on average: %.2fms (%d scans)\n",
                (denoisingTimeCounter) / scansByRtSpanAtMsLevel.size(), scansByRtSpanAtMsLevel.size()));
    }

    if (hasProfile) { // profile mode spectrum
        if (!doProfileModeGapFilling && doMzCloseZoomGapFilling) {
            applySavitzkyGolay(map);
        }
    } else { // !hasProfile => centroided spectrum
        if (doMzCloseZoomGapFilling) {
            applySavitzkyGolay(map);
        }
    }

    findMinMaxIntensities();

    // if we created the full-sized version of the map, then a lot of rows might
    // be zero, because no scan actually mapped to this row of pixels
    // so we just fill it with the same pixels as in the previous filled row.
    if (doInterpRt) {
        for (int filledRowIdx = 0; filledRowIdx < filledRowIds.length - 1; filledRowIdx++) {
            int rowLo = filledRowIds[filledRowIdx];
            int rowHi = filledRowIds[filledRowIdx + 1];
            for (int rowToFillIdx = rowLo + 1; rowToFillIdx < rowHi; rowToFillIdx++) {
                System.arraycopy(map[rowLo], 0, map[rowToFillIdx], 0, width);
                maxValInFullRow[rowToFillIdx] = maxValInFullRow[rowLo];
            }
        }
    }

    // add a tiny bit to the total intensity, allows not to care about
    // edge values when mapping intensities to colors.
    // Adding MIN_NORMAL, as totalIntensity shoule be a value > 1.0
    totalIntensityMax += 1e-8;
    return true;
}