window
The window operator produces a result array where each output cell is some aggregate function calculated over the cells contained in a N-dimensional rectangle around the corresponding cell in the source array.
Synopsis
window(array, dim1_preceding, dim1_following, [dim2_low,dim2_high[, ...]], aggregate1(attr_name1)[,aggrgegate2(attr_name2)[, ...]])
Summary
The window operator computes one or more aggregate functions of specified attributes over a fixed-size moving window. The result array has the same size and number of cells as the source array.
Inputs
The window operator takes the following arguments:
array: A source array.
dimN_preceding: For each dimension, 1 ... n, these arguments specify the number of cells before the current cell to include in the window.
dimN_following: For each dimension, 1 ... n, these argument specifiy the number of cells after of the current cell to include in the window.
aggregateN(attr_nameN): The aggregate function to use to compute the aggregate of the specified attribute.
Example
To calculate a running average for a 3x3 window on a 4x3 array, do the following:
Create an array M with constant values on each diagonal:
AFL% create array M <val:double>[row=1:4; col=1:3]; AFL% store(build(M, row+col), M);The output is:
{row,col} val {1,1} 2 {1,2} 3 {1,3} 4 {2,1} 3 {2,2} 4 {2,3} 5 {3,1} 4 {3,2} 5 {3,3} 6 {4,1} 5 {4,2} 6 {4,3} 7Return the average (mean) values over a 3×3 rectangle moving over M. The center of the rectangle is the left corner of the input.
AFL% window(M, 0, 2, 0, 2, avg(val));
The output is:{row,col} val_avg {1,1} 4 {1,2} 4.5 {1,3} 5 {2,1} 5 {2,2} 5.5 {2,3} 6 {3,1} 5.5 {3,2} 6 {3,3} 6.5 {4,1} 6 {4,2} 6.5 {4,3} 7
When the Window goes Beyond the End of the Array
To see what happens when the window goes beyond the end of the array, do the following:
Consider the following array, A:
[(),(2),(3),(4),(5)]Use a window that goes outside the boundary of the array, and join it to A, to construct a new array that has a lag:
AFL% join(A,window(A,1,0,first_value(x) as lag));
The output is:i,x,lag 2,2,2 3,3,2 4,4,3 5,5,4