"""1998 LeNet-5 mechanism reference. Standard library; no training or checkpoint.
Run python examples/lenet.py --json for deterministic JS/Python parity values.
The original uses learned averaging, partial C3 and Euclidean RBF outputs.
"""
import argparse
import json
import math

# region: convolution
def convolve(image, kernel):
    """Valid cross-correlation, the convention commonly called convolution."""
    k = len(kernel)
    return [[sum(kernel[dy][dx] * image[y+dy][x+dx]
                 for dy in range(k) for dx in range(k))
             for x in range(len(image[0])-k+1)]
            for y in range(len(image)-k+1)]
# endregion: convolution

# region: subsampling
def squash(x):
    return 1.7159 * math.tanh(2*x/3)


def subsample(values, weight=1.0, bias=0.0):
    """Average, learned coefficient and bias, then scaled tanh. Not max-pool."""
    return squash(weight * sum(values)/len(values) + bias)
# endregion: subsampling

# region: connections
C3 = [[0,1,2], [1,2,3], [2,3,4], [3,4,5], [0,4,5], [0,1,5],
      [0,1,2,3], [1,2,3,4], [2,3,4,5], [0,3,4,5], [0,1,4,5],
      [0,1,2,5], [0,1,3,4], [1,2,4,5], [0,2,3,5], [0,1,2,3,4,5]]


def connection_count():
    """One 5x5 kernel for each connected input map, plus one bias per output."""
    return sum(25*len(inputs)+1 for inputs in C3)
# endregion: connections

# region: rbf
def rbf(features, prototypes):
    """Squared distances, with smaller output indicating a closer prototype."""
    return [sum((x-p)**2 for x,p in zip(features, prototype))
            for prototype in prototypes]
# endregion: rbf


def main():
    image = [[1 if x in (2,3) else 0 for x in range(6)] for _ in range(6)]
    kernel = [[-1,0,1]]*3
    assert connection_count() == 1516
    assert math.isclose(subsample([1,1,1,1]), squash(1))
    assert rbf([1,-1], [[1,-1],[-1,1]]) == [0,8]
    report = {'convolution': convolve(image,kernel), 'subsample': subsample([0,1,1,0]),
              'connections': connection_count(), 'rbf': rbf([0.5,-0.5],[[1,-1],[-1,1]])}
    parser=argparse.ArgumentParser();parser.add_argument('--json',action='store_true');args=parser.parse_args()
    print(json.dumps(report) if args.json else 'LeNet mechanism self-checks passed. '+str(report))

if __name__ == '__main__':
    main()
