dimensions = (32, 32)
can = ContinuousAttractorModule(dimensions)

"""
Explanation: Recurrent connections
End of explanation
"""


printOutputWeights(json.dumps({
    "dimensions": dimensions,
    "inputMatrices": dict((k, weights.tolist())
                          for k, weights in can.recurrentWeights.iteritems())
}))

"""
Explanation: Here are the outputs of each cell. Hover over a cell to see its inhibitory output. Red means strong inhibition.
(It might take a second to load)
End of explanation
"""


printInputWeights(json.dumps({
    "dimensions": dimensions,
    "inputMatrices": dict((k, weights.tolist())
                          for k, weights in can.recurrentWeights.iteritems())
}))

"""
Explanation: Here are the inputs to each cell. Hover over a cell to see its inhibitory input.
End of explanation
"""


def orientationExperiment():
    dimensions = (32,32)
    recording = {
        "dimensions": dimensions,
        "timesteps": [],
    }

    can = ContinuousAttractorModule(dimensions)

    for k, rates in can.firingRates.iteritems():
        rates[:] = np.random.rand(dimensions[0]*dimensions[1])

    for t in xrange(500):
        if t % 10 == 0:
            recording["timesteps"].append(
                dict((k, rates.tolist())
                     for k, rates in can.firingRates.iteritems()))

        can.step(np.array([0.0, 0.0]))

    recording["timesteps"].append(
        dict((k, rates.tolist())
             for k, rates in can.firingRates.iteritems()))
    
    return recording

"""
Explanation: Lattice orientation
End of explanation
"""


recording1 = orientationExperiment()

printSpikeRatesTimeline(json.dumps(recording1))

def orientationExperiment2():
    dimensions = (16,16)
    recording = {
        "dimensions": dimensions,
        "timesteps": [],
    }

    can = ContinuousAttractorModule(dimensions)

    for k, rates in can.firingRates.iteritems():
        rates[:] = np.random.rand(dimensions[0]*dimensions[1])
        
    for t in xrange(500):

        can.step(np.array([0.0, 0.0]))


    for t in xrange(1500):
        if t % 100 == 0:
            recording["timesteps"].append(
                dict((k, rates.tolist())
                     for k, rates in can.firingRates.iteritems()))

        can.step(np.array([1.5, 0.0]))

    recording["timesteps"].append(
        dict((k, rates.tolist())
             for k, rates in can.firingRates.iteritems()))
    
    return recording

recording2 = orientationExperiment2()

printSpikeRatesTimeline(json.dumps(recording2))

"""
Explanation: Sometimes the lattice is aligned with the x axis, and sometimes with the y axis. (It's random.)
End of explanation
"""


