/** * actually recalculates the first average (necessary when we change the average settings) * * @param d */ public void recalculateAverage1() { // average1=new TimeSeries("av1", FixedMillisecond.class); average1.delete(0, average1.getItemCount() - 1); for (int z = 0; z < x1.getItemCount(); z++) { try { double v = 0; // List l1=x1.getItems(); for (int i = z; i < z + av1; i++) { v += x1.getValue(x1.getItemCount() - (av1 - i)).doubleValue(); } average1.add(x1.getTimePeriod(z), v / av1); } catch (Exception e) { // e.printStackTrace(); // System.out.println(timestamp.toString()); } } }
public void calcAverage1(int d, Date timestamp) { try { double v = 0; // List l1=x1.getItems(); for (int i = d; i < d + av1; i++) { v += x1.getValue(x1.getItemCount() - (av1 - i)).doubleValue(); } average1.add(new FixedMillisecond(timestamp), v / av1); } catch (Exception e) { // e.printStackTrace(); // System.out.println(timestamp.toString()); } }
/** * Tests the correct output of a DataSet to a TimeSeries by outputting it, then iterating through * TimeSeries object checking the correct values were stored/output. */ public void testOutput() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException, InstantiationException { // Constants used to determine size of test int NUMBER_OF_TIME_PERIODS = 10; String TIME_VARIABLE = "t"; // Set up array for expected results double expectedValue[] = new double[NUMBER_OF_TIME_PERIODS]; // We'll set up periods starting from today RegularTimePeriod period = new Day(); // Create a test DataSet for output // - note that only one independent variable (the time variable) // will be output. This is expected. DataSet dataSet = new DataSet(); dataSet.setTimeVariable(TIME_VARIABLE); for (int d = 0; d < NUMBER_OF_TIME_PERIODS; d++) { double value = (double) d; DataPoint obs = new Observation(value); obs.setIndependentValue(TIME_VARIABLE, period.getMiddleMillisecond()); dataSet.add(obs); period = period.next(); expectedValue[d] = value; } assertEquals( "Checking only one independent variable exists in dataSet", 1, dataSet.getIndependentVariables().length); assertEquals( "Checking dataSet has correct number of entries", NUMBER_OF_TIME_PERIODS, dataSet.size()); // Create TimeSeriesOutputter and use it to output dataSet TimeSeries timeSeries = new TimeSeries("test"); TimeSeriesOutputter outputter = new TimeSeriesOutputter(timeSeries, period.getClass()); outputter.output(dataSet); assertEquals( "Checking number of items in time series", NUMBER_OF_TIME_PERIODS, timeSeries.getItemCount()); // Reset period to start checking from today onwards period = new Day(); for (int d = 0; d < NUMBER_OF_TIME_PERIODS; d++) { TimeSeriesDataItem dataItem = timeSeries.getDataItem(d); period = dataItem.getPeriod(); assertNotNull("Checking time period", period); long timeValue = period.getMiddleMillisecond(); assertTrue( "Checking time periods match", (double) timeValue >= period.getFirstMillisecond() && (double) timeValue <= period.getLastMillisecond()); assertEquals( "Checking values for period " + dataItem.getPeriod() + " match", expectedValue[d], dataItem.getValue().doubleValue(), TOLERANCE); period = period.next(); } }