Example – sliding a window across a larger input.

Input:
1.0 2.0 3.0 4.0 5.0 6.0 7.0 
8.0 9.0 10.0 11.0 12.0 13.0 14.0 
15.0 16.0 17.0 18.0 19.0 20.0 21.0 
22.0 23.0 24.0 25.0 26.0 27.0 28.0 
29.0 30.0 31.0 32.0 33.0 34.0 35.0 

Sliding across input with window 2x3, stepsize 1.

Window #1, UL corner at (1, 0)
1.0 2.0 
8.0 9.0 
15.0 16.0 

Window #2, UL corner at (2, 0)
2.0 3.0 
9.0 10.0 
16.0 17.0 

Window #3, UL corner at (3, 0)
3.0 4.0 
10.0 11.0 
17.0 18.0 

Window #4, UL corner at (4, 0)
4.0 5.0 
11.0 12.0 
18.0 19.0 

Window #5, UL corner at (5, 0)
5.0 6.0 
12.0 13.0 
19.0 20.0 

Window #6, UL corner at (0, 1)
6.0 7.0 
13.0 14.0 
20.0 21.0 

Window #7, UL corner at (1, 1)
8.0 9.0 
15.0 16.0 
22.0 23.0 

Window #8, UL corner at (2, 1)
9.0 10.0 
16.0 17.0 
23.0 24.0 

Window #9, UL corner at (3, 1)
10.0 11.0 
17.0 18.0 
24.0 25.0 

Window #10, UL corner at (4, 1)
11.0 12.0 
18.0 19.0 
25.0 26.0 

Window #11, UL corner at (5, 1)
12.0 13.0 
19.0 20.0 
26.0 27.0 

Window #12, UL corner at (0, 2)
13.0 14.0 
20.0 21.0 
27.0 28.0 

Window #13, UL corner at (1, 2)
15.0 16.0 
22.0 23.0 
29.0 30.0 

Window #14, UL corner at (2, 2)
16.0 17.0 
23.0 24.0 
30.0 31.0 

Window #15, UL corner at (3, 2)
17.0 18.0 
24.0 25.0 
31.0 32.0 

Window #16, UL corner at (4, 2)
18.0 19.0 
25.0 26.0 
32.0 33.0 

Window #17, UL corner at (5, 2)
19.0 20.0 
26.0 27.0 
33.0 34.0 

Window #18, UL corner at (0, 3)
20.0 21.0 
27.0 28.0 
34.0 35.0 

Window #19, UL corner at (1, 3)
22.0 23.0 
29.0 30.0 
29.0 30.0 

Window #20, UL corner at (2, 3)
23.0 24.0 
30.0 31.0 
30.0 31.0 

Window #21, UL corner at (3, 3)
24.0 25.0 
31.0 32.0 
31.0 32.0 

Window #22, UL corner at (4, 3)
25.0 26.0 
32.0 33.0 
32.0 33.0 

Window #23, UL corner at (5, 3)
26.0 27.0 
33.0 34.0 
33.0 34.0 

Window #24, UL corner at (0, 4)
27.0 28.0 
34.0 35.0 
34.0 35.0

Source code:


/*
 * Copyright (c) 2016 Emphysic LLC.  All rights reserved.
 */

package com.emphysic.myriad.core.examples;

import com.emphysic.myriad.core.data.io.Dataset;
import com.emphysic.myriad.core.data.ops.SlidingWindowOperation;

/**
 * Demonstrates use of the "sliding window" algorithm to examine small subsets of a larger Dataset.
 * Created by ccoughlin on 9/12/2016.
 */
public class SlidingWindow {

    public static void main(String[] args) throws Exception {
        int numRows = 5;
        int numCols = 7;
        Dataset inp = new Dataset(numCols, numRows);
        double val = 1;
        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < numCols; j++) {
                inp.set(j, i, val);
                val++;
            }
        }
        System.out.println("Input:");
        inp.prettyPrint(System.out);
        System.out.println("");

        int step = 1;
        int windowWidth = 2;
        int windowHeight = 3;
        System.out.println("Sliding across input with window " + windowWidth + "x" + windowHeight + ", stepsize " + step + ".\n");
        SlidingWindowOperation slider = new SlidingWindowOperation(step, windowWidth, windowHeight);
        Dataset res;
        int win = 1;
        while ((res = slider.run(inp)) != null) {
            System.out.println("Window #" + win + ", UL corner at (" + slider.getXoffset() + ", " + slider.getYoffset() + ")");
            res.prettyPrint(System.out);
            System.out.println();
            win++;
        }
    }
}