| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reports; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Collection; |
| 5 | import java.util.Iterator; |
| 6 | |
| 7 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
| 8 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.CdfReportItem; |
| 9 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.HistogramReportItem; |
| 10 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.IReportItem; |
| 11 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.PdfReportItem; |
| 12 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.RCommandRReportItem; |
| 13 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.StaticTextReportItem; |
| 14 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.reportitems.AbstractPlotReportItem.LegendPosition; |
| 15 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.utils.RConnection; |
| 16 | import de.uka.ipd.sdq.sensorframework.visualisation.rvisualisation.utils.REngineHelper; |
| 17 | |
| 18 | /**View for comparing distributions. |
| 19 | * @author groenda, rathfeld, martens |
| 20 | */ |
| 21 | public class DistributionComparisonReport extends RReport { |
| 22 | |
| 23 | /** {@inheritDoc} |
| 24 | */ |
| 25 | @Override |
| 26 | public ArrayList<IReportItem> prepareReportItems( |
| 27 | final Collection<SensorAndMeasurements> c, final RConnection t) { |
| 28 | ArrayList<IReportItem> items = new ArrayList<IReportItem>(); |
| 29 | items.add(new StaticTextReportItem( |
| 30 | "R-Report for comparing distributions.", true)); |
| 31 | |
| 32 | PdfReportItem densityPlot = |
| 33 | new PdfReportItem("Probability Density Function", "Time"); |
| 34 | HistogramReportItem histPlot = |
| 35 | new HistogramReportItem("Histogram", "Time"); |
| 36 | CdfReportItem cdfPlot=new CdfReportItem("Cumulative Distribution Function","Time"); |
| 37 | items.add(densityPlot); |
| 38 | items.add(histPlot); |
| 39 | items.add(cdfPlot); |
| 40 | Iterator<SensorAndMeasurements> it = c.iterator(); |
| 41 | SensorAndMeasurements[] sm = new SensorAndMeasurements[c.size()]; |
| 42 | // enable Legends by defining the position |
| 43 | if (c.size()>1){ |
| 44 | densityPlot.setLegendPos(LegendPosition.topright); |
| 45 | cdfPlot.setLegendPos(LegendPosition.bottomright); |
| 46 | } |
| 47 | for (int i = 0; i < sm.length ; i++) { |
| 48 | sm[i] = it.next(); |
| 49 | String sensorName = REngineHelper.storeMeasurementsInRVector(sm[i], i, |
| 50 | TimeseriesData.TIMESPAN, t); |
| 51 | |
| 52 | densityPlot.addData(sm[i].getSensor().getSensorName(), |
| 53 | "SensorID:" +sm[i].getSensor().getSensorID(), sensorName); |
| 54 | histPlot.addData(sm[i].getSensor().getSensorName(), |
| 55 | "SensorID:" +sm[i].getSensor().getSensorID(), sensorName); |
| 56 | cdfPlot.addData(sm[i].getSensor().getSensorName(), |
| 57 | "SensorID:" +sm[i].getSensor().getSensorID(), sensorName); |
| 58 | String rCommand = "length(" + sensorName + ")\n"; |
| 59 | items.add(new RCommandRReportItem(rCommand, |
| 60 | "Number of observations of Sensor " |
| 61 | + sm[i].getSensor().getSensorName())); |
| 62 | rCommand = "mean(" + sensorName + ")\n"; |
| 63 | items.add(new RCommandRReportItem(rCommand, "Mean of Sensor " |
| 64 | + sm[i].getSensor().getSensorName())); |
| 65 | rCommand = "sd(" + sensorName + ")\n"; |
| 66 | items.add(new RCommandRReportItem(rCommand, |
| 67 | "Standard-Deviation of Sensor " |
| 68 | + sm[i].getSensor().getSensorName())); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * This report needs at least two sensors to be compared. |
| 73 | */ |
| 74 | if (c.size() != 2) { |
| 75 | items.add(new StaticTextReportItem( |
| 76 | "KS test and Chi^ Test are only abailable if two sensors are added to this report. You added " |
| 77 | + c.size() + (c.size() == 1 ? " sensor." : " sensors.") |
| 78 | + " Use the property sheet to add or delete sensors.", |
| 79 | false)); |
| 80 | return items; |
| 81 | } else { |
| 82 | items.add(new StaticTextReportItem("Comparing sensors " |
| 83 | + sm[0].getSensor().getSensorName() + " and " |
| 84 | + sm[1].getSensor().getSensorName(), false)); |
| 85 | |
| 86 | items.add( new StaticTextReportItem("KS test.", true)); |
| 87 | //t.execute("ks <- ks.test(sensor0,sensor1)\n "); |
| 88 | |
| 89 | items.add(new RCommandRReportItem( |
| 90 | "ks.test(sensor0,sensor1)$method", |
| 91 | "The applied test for the sensors")); |
| 92 | items.add(new RCommandRReportItem( |
| 93 | "ks.test(sensor0,sensor1)$statistic", |
| 94 | "The value of the test statistics")); |
| 95 | items.add(new RCommandRReportItem( |
| 96 | "ks.test(sensor0,sensor1)$p.value", |
| 97 | "The p-value of the test")); |
| 98 | |
| 99 | items.add(new StaticTextReportItem("Chi^2 test.", true)); |
| 100 | //t.execute("chisq <- chisq.test(sensor0,sensor1)\n "); |
| 101 | |
| 102 | /* |
| 103 | * The vectors must have the same length for the chi square test, |
| 104 | * so use the shorter vector's length. |
| 105 | * Additionally, Chi square test cannot handle lots of data. Thus, |
| 106 | * only compare the first 2500 values or less if the sensor |
| 107 | * contains less data. |
| 108 | */ |
| 109 | int max = (sm[0].getMeasurements().size() < sm[1].getMeasurements() |
| 110 | .size() ? sm[0].getMeasurements().size() : sm[1] |
| 111 | .getMeasurements().size()); |
| 112 | max = (max > 2500) ? 2500 : max; |
| 113 | |
| 114 | |
| 115 | items.add(new RCommandRReportItem( |
| 116 | "chisq.test(sensor0[1:" + max + "],sensor1[1:" + max |
| 117 | + "])$method", "The applied test for the sensors")); |
| 118 | items.add(new RCommandRReportItem( |
| 119 | "chisq.test(sensor0[1:" + max + "],sensor1[1:" |
| 120 | + max + "])$statistic", |
| 121 | "The value of the test statistics")); |
| 122 | items.add(new RCommandRReportItem( |
| 123 | "chisq.test(sensor0[1:" + max + "],sensor1[1:" |
| 124 | + max + "])$p.value", "The p-value of the test")); |
| 125 | } |
| 126 | return items; |
| 127 | |
| 128 | } |
| 129 | |
| 130 | } |