public static String readableFileSize(long size) { if (size <= 0) return "0"; final String[] units = new String[] {"B", "KB", "MB", "GB", "TB"}; int digitGroups = (int) (Math.log10(size) / Math.log10(1024)); return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups]; }
public LinePlotTest() { super(new BorderLayout()); setPreferredSize(new Dimension(800, 600)); setBackground(Color.WHITE); // load data from file ---------------------------------------------- // basemean, log2foldchange DataTable nonSigData = new DataTable(Double.class, Double.class); DataTable sigData = new DataTable(Double.class, Double.class); try (CSVReader reader = new CSVReader(new FileReader("rnaSeqExample.csv"))) { String[] nextLine; int row = 0; while ((nextLine = reader.readNext()) != null) { if (row != 0) { // needs to have a p-value if (nextLine[2].equals("NA")) continue; double baseMean = Double.parseDouble(nextLine[1]); double log2Change = Double.parseDouble(nextLine[2]); if (nextLine[6].equals("NA") || Double.parseDouble(nextLine[6]) > 0.05) { nonSigData.add(Math.log10(baseMean), log2Change); } else { sigData.add(Math.log10(baseMean), log2Change); } } row++; } } catch (IOException e) { e.printStackTrace(); } System.out.println("Non-significant: " + nonSigData.getRowCount()); System.out.println("Significant: " + sigData.getRowCount()); XYPlot plot = new XYPlot(nonSigData, sigData); // set point renderer for data PointRenderer points = new DefaultPointRenderer2D(); points.setShape(new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0)); points.setColor(NONSIG_COLOR); plot.setPointRenderer(nonSigData, points); PointRenderer pointsRed = new DefaultPointRenderer2D(); pointsRed.setShape(new Ellipse2D.Double(-2.0, -2.0, 4.0, 4.0)); pointsRed.setColor(SIG_COLOR); plot.setPointRenderer(sigData, pointsRed); // Display on screen add(new InteractivePanel(plot), BorderLayout.CENTER); }
private long normalizeMax(long l) { int exp = (int) Math.log10((double) l); long multiple = (long) Math.pow(10.0, exp); int i = (int) (l / multiple); l = (i + 1) * multiple; return l; }
/** * Returns the number of digits in x. * * @param x a whole number. * @return 1 if x is 0, otherwise the number of digits. */ public static int numDigits(int x) { if (x == 0) return 1; return (int) (Math.log10(x)) + 1; // 250 -> log250 = 2.something -> 2+1 -> 3 }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); int width = getWidth() - Border.left - Border.right; int height = getHeight() - Border.top - Border.bottom; g2.translate(Border.left, Border.top); // create x and y axes g2.setColor(Color.black); g2.drawLine(0, height, width, height); g2.drawLine(0, 0, 0, height); g2.setFont(g2.getFont().deriveFont(10.0f)); FontMetrics fm = g2.getFontMetrics(); if (xValues.length == 0 || target == null || xValues[xValues.length - 1] <= 0) { return; } double factor; String yText; Application app = Application.getInstance(); Shaking.Type t = app.getSpectrumParameter(); if (t == Shaking.Type.PSA) { factor = Application.EarthAcceleration1; yText = t.toString().toUpperCase() + ", g"; } else if (t == Shaking.Type.DRS) { factor = 100; yText = t.toString().toUpperCase() + ", cm"; } else { LOG.warn("unsupported spectrum parameter: " + t.toString()); return; } synchronized (target) { // determine min/max of x and y axis double xMin; double xMax; double dx = 0; if (logScale) { xMin = Math.log10(xValues[0]); xMax = Math.log10(xValues[xValues.length - 1]); } else { xMin = 0.0; xMax = xValues[xValues.length - 1]; } if (xMax > xMin) { dx = (double) width / (xMax - xMin); } double yMin = 0; double yMax = 0; double dy = 0; boolean first = true; for (double v : reference1) { if (first) { first = false; /*yMin =*/ yMax = v; } /*yMin = Math.min(yMin, v);*/ yMax = Math.max(yMax, v); } for (double v : reference2) { if (first) { first = false; /*yMin =*/ yMax = v; } /*yMin = Math.min(yMin, v);*/ yMax = Math.max(yMax, v); } for (Shaking s : target.spectralValues) { if (first) { first = false; /*yMin =*/ yMax = s.expectedSI * factor; } else { /*yMin = Math.min(yMin, s.expectedSI * factor);*/ yMax = Math.max(yMax, s.expectedSI * factor); } /* yMin = Math.min(yMin, s.expectedSI * factor); yMin = Math.min(yMin, s.percentile84 * factor); yMin = Math.min(yMin, s.percentile16 * factor); */ yMax = Math.max(yMax, s.expectedSI * factor); yMax = Math.max(yMax, s.percentile84 * factor); yMax = Math.max(yMax, s.percentile16 * factor); } if (yMax > yMin) { dy = (double) height / (yMax - yMin); } List<Point> expectedPoints = new ArrayList(); List<Point> percentile84Points = new ArrayList(); List<Point> percentile16Points = new ArrayList(); List<Point> ref1Points = new ArrayList(); List<Point> ref2Points = new ArrayList(); int x, y, iV; boolean isFreq = app.isUseFrequencies(); for (int i = 0; i < xValues.length; i++) { if (logScale) { x = (int) ((Math.log10(xValues[i]) - xMin) / (xMax - xMin) * width); } else { x = (int) ((xValues[i] - xMin) / (xMax - xMin) * width); } iV = isFreq ? xValues.length - i - 1 : i; if (iV < target.spectralValues.size()) { Shaking s = target.spectralValues.get(iV); y = (int) ((yMax - s.expectedSI * factor) * dy); expectedPoints.add(new Point(x, y)); y = (int) ((yMax - s.percentile84 * factor) * dy); percentile84Points.add(new Point(x, y)); y = (int) ((yMax - s.percentile16 * factor) * dy); percentile16Points.add(new Point(x, y)); } if (iV < reference1.size()) { y = (int) ((yMax - reference1.get(iV)) * dy); ref1Points.add(new Point(x, y)); } if (iV < reference2.size()) { y = (int) ((yMax - reference2.get(iV)) * dy); ref2Points.add(new Point(x, y)); } } // Y-axis String text; int halfAscent = fm.getAscent() / 2 - 1; double q = Math.log10(2 * (yMax - yMin) * 2 * fm.getHeight() / height); double rx = q - Math.floor(q); int d = rx < 0.3 ? 1 : rx > 0.7 ? 5 : 2; double tickStep = d * Math.pow(10, Math.floor(q - rx)); for (double v = 0; v < yMax; v += tickStep) { y = height - (int) (v * dy) - 1; g2.drawLine(0, y, -TickLength, y); text = Double.toString(((int) (v * 1000.0 + 0.5)) / 1000.0); int w = (int) fm.getStringBounds(text, null).getWidth(); g2.drawString(text, -TickLength - 3 - w, y + halfAscent); } // X-axis q = Math.log10(2 * (xMax - xMin) * 50 / width); rx = q - Math.floor(q); d = rx < 0.3 ? 1 : rx > 0.7 ? 5 : 2; tickStep = d * Math.pow(10, Math.floor(q - rx)); for (double v = xMin; v < xMax; v += tickStep) { x = (int) ((v - xMin) * dx); g2.drawLine(x, height, x, height + TickLength); text = String.format("%.2f", logScale ? Math.pow(10, v) : v); // String text = Double.toString(logScale ? Math.pow(10, v) : v);//((int) (v * 1000.0 + // 0.5)) / 1000.0); int w = (int) fm.getStringBounds(text, null).getWidth(); g2.drawString(text, x - w / 2, height + TickLength + 3 + fm.getAscent()); } // axis label g2.setFont(g2.getFont().deriveFont(14.0f)); AffineTransform orig = g2.getTransform(); int w = (int) ((height + fm.getStringBounds(yText, null).getWidth()) / 2); g2.translate(-Border.left + fm.getAscent() + 7, w); g2.rotate(-Math.PI / 2); g2.drawString(yText, 0, 0); g2.setTransform(orig); text = "vibration " + (isFreq ? "frequency, Hz" : "period, s"); g2.drawString( text, (int) ((width - fm.getStringBounds(text, null).getWidth()) / 2), height + TickLength + 3 + 2 * fm.getHeight()); g2.setClip(0, 0, width, height); g2.setStroke(DefaultStroke); g2.setColor(Ref1Color); drawGraph(g2, ref1Points); g2.setColor(Ref2Color); drawGraph(g2, ref2Points); g2.setStroke(PercentileStroke); g2.setColor(ShakingColor); drawGraph(g2, percentile16Points); drawGraph(g2, percentile84Points); g2.setStroke(DefaultStroke); drawGraph(g2, expectedPoints); } }