Skip to content

Instantly share code, notes, and snippets.

@ktnyt
Last active March 9, 2016 08:24
Show Gist options
  • Select an option

  • Save ktnyt/160ddd4353745786dad6 to your computer and use it in GitHub Desktop.

Select an option

Save ktnyt/160ddd4353745786dad6 to your computer and use it in GitHub Desktop.
Hippocampal Formation Mock
import brica1
from CA1Component import CA1Component
from CA3Component import CA3Component
from DGComponent import DGComponent
from ECComponent import ECComponent
class HFComponent(brica1.ComponentSet):
def __init__(self, params):
super(HFComponent, self).__init__()
ec = ECComponent(input=params['ec']['in']['input'])
dg = DGComponent()
ca3 = CA3Component()
ca1 = CA1Component()
## Create Ports
# EC Ports
ec.make_in_port('input', params['ec']['in']['input'])
ec.make_in_port('from_ca1', params['ec']['in']['from_ca1'])
ec.make_out_port('to_dg', params['ec']['out']['to_dg'])
ec.make_out_port('to_ca3', params['ec']['out']['to_ca3'])
ec.make_out_port('to_ca1', params['ec']['out']['to_ca1'])
ec.make_in_port('reward', 1)
# DG Ports
dg.make_in_port('from_ec', params['dg']['in']['from_ec'])
dg.make_out_port('to_ca3', params['dg']['out']['to_ca3'])
dg.make_in_port('reward', 1)
# CA3 Ports
ca3.make_in_port('from_dg', params['ca3']['in']['from_dg'])
ca3.make_in_port('from_ec', params['ca3']['in']['from_ec'])
ca3.make_out_port('to_ca3', params['ca3']['out']['to_ca3'])
ca3.make_in_port('reward', 1)
# CA1 Ports
ca1.make_in_port('from_ca3', params['ca1']['in']['from_ca3'])
ca1.make_in_port('from_ec', params['ca1']['in']['from_ec'])
ca1.make_out_port('output', params['ca1']['out']['output'])
ca1.make_out_port('to_ec', params['ca1']['out']['to_ec'])
ca1.make_in_port('reward', 1)
## Connect Components
# Connections from EC
brica1.connect((ec, 'to_dg'), (dg, 'from_ec'))
brica1.connect((ec, 'to_ca3'), (ca3, 'from_ec'))
brica1.connect((ec, 'to_ca1'), (ca1, 'from_ec'))
# Connections from DG
brica1.connect((dg, 'to_ca3'), (ca3, 'from_dg'))
# Connections from CA3
brica1.connect((ca3, 'to_ca1'), (ca1, 'from_ca3'))
# Connections from CA1
brica1.connect((ca1, 'to_ec'), (ec, 'from_ca1'))
## Input, Reward, Output
# HF Ports
self.make_in_port('input', params['ec']['in']['input'])
self.make_in_port('reward', 1)
self.make_out_port('output', params['ca1']['out']['output'])
## Aliases
# Input/Output
brica1.alias_in_port((self, 'input'), (ec, 'input'))
brica1.alias_out_port((self, 'output'), (ca1, 'output'))
# Rewards
brica1.alias_in_port((self, 'reward'), (ec, 'reward'))
brica1.alias_in_port((self, 'reward'), (dg, 'reward'))
brica1.alias_in_port((self, 'reward'), (ca3, 'reward'))
brica1.alias_in_port((self, 'reward'), (ca1, 'reward'))
## Add Components
self.add_component('ec', ec, 1)
self.add_component('dg', dg, 2)
self.add_component('ca3', ca3, 3)
self.add_component('ca1', ca1, 4)
class HFSupervisor(brica1.Supervisor):
def __init__(self, agent):
super(HFSupervisor, self).__init__(agent)
def step(self):
hf = agent.get_component('hf')
ec = hf.get_component('ec')
dg = hf.get_component('dg')
ca3 = hf.get_component('ca3')
ca1 = hf.get_component('ca1')
if ec.get_state('loss') < 0.001:
ec.set_state('lr') = 0.0001
if __name__ == '__main__':
params = {
'ec': {
'in': {
'input': 100,
'from_ca1': 100,
},
'out': {
'to_dg': 100,
'to_ca3': 100,
'to_ca1': 100
}
},
'dg': {
'in': {
'from_ec': 100,
},
'out': {
'to_ca3': 100
}
},
'ca3': {
'in': {
'from_dg': 100,
'from_ec': 100
},
'out': {
'to_ca1': 100
}
},
'ca1': {
'in': {
'from_ca3': 100,
'from_ec': 100
},
'out': {
'output': 100,
'to_ec': 100
}
}
}
hf = HFComponent(params)
agent = brica1.Agent()
scheduler = brica1.VirtualTimeSyncScheduler(agent, HFSupervisor)
agent.add_component('hf', hf)
scheduler.update()
for _ in range(1000):
scheduler.step()
output = agent.get_out_port('output').buffer
print output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment