Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor on Connections #33

Merged
merged 16 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/test/configs/l2_3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from conex.behaviors.neurons.neuron_types.lif_neurons import AELIF, ELIF
from conex.nn.Config.layer_config import LayerConfig
from conex.nn.config.layer_config import LayerConfig
from pymonntorch import *


Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/l2_3_l5.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conex.nn.Config.connection_config import Layer2LayerConnectionConfig
from conex.nn.config.connection_config import Layer2LayerConnectionConfig


class l2_3_l5(Layer2LayerConnectionConfig):
Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/l2_3_rep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conex.nn.Config.connection_config import Layer2PopConnectionConfig
from conex.nn.config.connection_config import Layer2PopConnectionConfig


class l2_3_rep(Layer2PopConnectionConfig):
Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/l4.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from conex.behaviors.neurons.neuron_types.lif_neurons import LIF, ELIF
from conex.nn.Config.layer_config import LayerConfig
from conex.nn.config.layer_config import LayerConfig
from pymonntorch import *


Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/l4_l2_3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conex.nn.Config.connection_config import Layer2LayerConnectionConfig
from conex.nn.config.connection_config import Layer2LayerConnectionConfig


class l4_l2_3(Layer2LayerConnectionConfig):
Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/l5.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from conex.behaviors.neurons.neuron_types.lif_neurons import ELIF
from conex.nn.Config.layer_config import LayerConfig
from conex.nn.config.layer_config import LayerConfig
from pymonntorch import *


Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/l5_l2_3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conex.nn.Config.connection_config import Layer2LayerConnectionConfig
from conex.nn.config.connection_config import Layer2LayerConnectionConfig


class l5_l2_3(Layer2LayerConnectionConfig):
Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/save_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from Example.test.configs.l4_l2_3 import *
from Example.test.configs.l5_l2_3 import *

from conex.nn.Config.base_config import BaseConfig
from conex.nn.config.base_config import BaseConfig

if __name__ == "__main__":
config_type = "json" # json
Expand Down
2 changes: 1 addition & 1 deletion Example/test/configs/sens_l4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from conex.nn.Config.connection_config import Pop2LayerConnectionConfig
from conex.nn.config.connection_config import Pop2LayerConnectionConfig


class sens_l4(Pop2LayerConnectionConfig):
Expand Down
288 changes: 288 additions & 0 deletions Example/visualization/connections_types/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
import math
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

CONNECTION_COLOR = {
"Apical": "orange",
"Proximal": "green",
"Distal": "purple",
"NoConnection": "Black",
}

from conex.nn.connections import *


def make_handle():
apical = mpatches.Patch(color=CONNECTION_COLOR["Apical"], label="Apical")
proximal = mpatches.Patch(color=CONNECTION_COLOR["Proximal"], label="Proximal")
distal = mpatches.Patch(color=CONNECTION_COLOR["Distal"], label="Distal")
return [proximal, apical, distal]


def make_layer(name, bottom_left, width, height, radius):
layer = mpatches.Rectangle(bottom_left, width, height, fc=(1, 1, 1, 0), ec="black")
start_x, start_y = bottom_left
width_sep = (width - 4 * radius) / 3
height_sep = (height - 2 * radius) / 2
exc = mpatches.Circle(
(start_x + width_sep + radius, start_y + height_sep + radius),
radius=radius,
fc=(1, 1, 1, 0),
ec="blue",
)
inh = mpatches.Circle(
(start_x + 2 * width_sep + 3 * radius, start_y + height_sep + radius),
radius=radius,
fc=(1, 1, 1, 0),
ec="red",
)
offset_self_connection = radius * math.sqrt(3) / 2
exc_exc = mpatches.FancyArrowPatch(
posA=(
start_x + width_sep + radius - offset_self_connection,
start_y + height_sep + radius + (radius / 2),
),
posB=(
start_x + width_sep + radius - offset_self_connection,
start_y + height_sep + radius - (radius / 2),
),
arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8",
connectionstyle="arc3, rad=2",
fc=(1, 1, 1, 0),
ec=CONNECTION_COLOR[LAYER_CONNECTION_TYPE[("exc", "exc")]],
)
inh_inh = mpatches.FancyArrowPatch(
posA=(
start_x + 2 * width_sep + 3 * radius + offset_self_connection,
start_y + height_sep + radius + (radius / 2),
),
posB=(
start_x + 2 * width_sep + 3 * radius + offset_self_connection,
start_y + height_sep + radius - (radius / 2),
),
arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8",
connectionstyle="arc3, rad=-2",
fc=(1, 1, 1, 0),
ec=CONNECTION_COLOR[LAYER_CONNECTION_TYPE[("inh", "inh")]],
)
exc_inh = mpatches.FancyArrowPatch(
posA=(
start_x + width_sep + radius + offset_self_connection,
start_y + height_sep + radius + (radius / 2),
),
posB=(
start_x + 2 * width_sep + 3 * radius - offset_self_connection,
start_y + height_sep + radius + (radius / 2),
),
arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8",
connectionstyle="arc3, rad=-0.7",
fc=(1, 1, 1, 0),
ec=CONNECTION_COLOR[LAYER_CONNECTION_TYPE[("exc", "inh")]],
)
inh_exc = mpatches.FancyArrowPatch(
posA=(
start_x + 2 * width_sep + 3 * radius - offset_self_connection,
start_y + height_sep + radius - (radius / 2),
),
posB=(
start_x + width_sep + radius + offset_self_connection,
start_y + height_sep + radius - (radius / 2),
),
arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8",
connectionstyle="arc3, rad=-0.7",
fc=(1, 1, 1, 0),
ec=CONNECTION_COLOR[LAYER_CONNECTION_TYPE[("inh", "exc")]],
)
layer_text = plt.text(
x=start_x + 0.01, y=start_y + 0.01, s=name
) # TODO use patch like object
return {
"layer": layer,
"exc": exc,
"inh": inh,
"exc_exc": exc_exc,
"inh_inh": inh_inh,
"exc_inh": exc_inh,
"inh_exc": inh_exc,
"layer_text": layer_text,
}


def make_cortical_column(name, bottom_left, width, height, radius):
layer_names = ["L6", "L5", "L4", "L2_3"]
pop_names = ["exc", "inh"]
height_step = height / len(layer_names)
start_x, start_y = bottom_left
layers = {
name: make_layer(
name,
(start_x, start_y + index * height_step),
width,
height=height_step,
radius=radius,
)
for index, name in enumerate(layer_names)
}

cc_text = plt.text(x=start_x + 0.01, y=start_y + height + 0.01, s=name)

intra_column_synapse = {}
for i, src in enumerate(layer_names):
for j, dst in enumerate(layer_names):
if src != dst:
for src_type in pop_names:
for dst_type in pop_names:
if (
INTRA_COLUMN_CONNECTION_TYPE[(src, dst, src_type, dst_type)]
== no_connection
):
continue
# src_cen_x, src_cen_y = layers[src][src_type].center
# dst_cen_x, dst_cen_y = layers[dst][dst_type].center
# dx , dy = dst_cen_x-src_cen_x ,dst_cen_y-src_cen_y
# distnat = math.sqrt(dx**2 + dy**2)
# dxr = radius*dx/distnat
# dyr = radius*dy/distnat
# posA=(src_cen_x + dxr, src_cen_y + dyr)
# posB=(dst_cen_x - dxr, dst_cen_y - dyr)
synapse = mpatches.FancyArrowPatch(
posA=layers[src][src_type].center,
posB=layers[dst][dst_type].center,
arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8",
connectionstyle=f"arc3, rad=-0.3",
fc=(1, 1, 1, 0),
ec=CONNECTION_COLOR[
INTRA_COLUMN_CONNECTION_TYPE[
(src, dst, src_type, dst_type)
]
],
)
intra_column_synapse[(src, dst, src_type, dst_type)] = synapse

return {
"layers": layers,
"intra_column_synapse": intra_column_synapse,
"cc_text": cc_text,
}


def make_two_cortical_column(names, bottom_left, width, height, radius, distance):
cc1 = make_cortical_column(names[0], bottom_left, width, height, radius)
start_x, start_y = bottom_left
cc2 = make_cortical_column(
names[1], (start_x + width + distance, start_y), width, height, radius
)

layer_names = ["L6", "L5", "L4", "L2_3"]
pop_names = ["exc", "inh"]

inter_column_synapse = {}
for i, src in enumerate(layer_names):
for j, dst in enumerate(layer_names):
for src_type in pop_names:
for dst_type in pop_names:
if (
INTER_COLUMN_CONNECTION_TYPE[(src, dst, src_type, dst_type)]
== no_connection
):
continue
synapse = mpatches.FancyArrowPatch(
posA=cc1["layers"][src][src_type].center,
posB=cc2["layers"][dst][dst_type].center,
arrowstyle="Simple, tail_width=0.5, head_width=4, head_length=8",
connectionstyle=f"arc3, rad=-0.3",
fc=(1, 1, 1, 0),
ec=CONNECTION_COLOR[
INTER_COLUMN_CONNECTION_TYPE[(src, dst, src_type, dst_type)]
],
)
inter_column_synapse[(src, dst, src_type, dst_type)] = synapse
return {"cc1": cc1, "cc2": cc2, "inter_column_synapse": inter_column_synapse}


if __name__ == "__main__":
# Single layer
fig, ax = plt.subplots(figsize=(6, 6))
for label, value in make_layer("L4", (0.3, 0.3), 0.4, 0.2, 0.05).items():
ax.add_artist(value)

ax.set_axis_off()
ax.legend(handles=make_handle())
plt.show()

# Single column
fig, ax = plt.subplots(figsize=(8, 8))
cc = make_cortical_column("CC1", (0.1, 0.1), 0.4, 0.8, 0.05)

for layer in ["L2_3", "L4", "L5", "L6"]:
for patch in cc["layers"][layer].values():
ax.add_artist(patch)

for syn in cc["intra_column_synapse"].values():
ax.add_artist(syn)

ax.set_axis_off()
ax.legend(handles=make_handle())
plt.show()

# single column showing layer by layer connection:
for src_layer in ["L2_3", "L4", "L5", "L6"]:
fig, ax = plt.subplots(figsize=(8, 8))
cc = make_cortical_column("cc1", (0.1, 0.1), 0.4, 0.8, 0.05)

for layer in ["L2_3", "L4", "L5", "L6"]:
for key, patch in cc["layers"][layer].items():
if key not in ["layer", "layer_text"]:
patch.set_alpha(0.3)
ax.add_artist(patch)

for key, syn in cc["intra_column_synapse"].items():
if key[0] == src_layer:
ax.add_artist(syn)

ax.set_axis_off()
ax.legend(handles=make_handle())
plt.show()

# two cortical column
fig, ax = plt.subplots(figsize=(8, 8))
ccs = make_two_cortical_column(["CC1", "CC2"], (0.05, 0.1), 0.3, 0.6, 0.05, 0.1)

for cc in [ccs["cc1"], ccs["cc2"]]:
for layer in ["L2_3", "L4", "L5", "L6"]:
for patch in cc["layers"][layer].values():
ax.add_artist(patch)

for syn in cc["intra_column_synapse"].values():
ax.add_artist(syn)

for syn in ccs["inter_column_synapse"].values():
ax.add_artist(syn)

ax.set_axis_off()
ax.legend(handles=make_handle())
plt.show()

# two cortical column showing layer by layer connection
for src_layer in ["L2_3", "L4", "L5", "L6"]:
fig, ax = plt.subplots(figsize=(8, 8))
ccs = make_two_cortical_column(["CC1", "CC2"], (0.05, 0.1), 0.3, 0.6, 0.05, 0.1)

for cc in [ccs["cc1"], ccs["cc2"]]:
for layer in ["L2_3", "L4", "L5", "L6"]:
for key, patch in cc["layers"][layer].items():
if key not in ["layer", "layer_text"]:
patch.set_alpha(0.3)
ax.add_artist(patch)

for syn in cc["intra_column_synapse"].values():
syn.set_alpha(0.1)
ax.add_artist(syn)

for key, syn in ccs["inter_column_synapse"].items():
if key[0] == src_layer:
ax.add_artist(syn)

ax.set_axis_off()
ax.legend(handles=make_handle())
plt.show()
2 changes: 1 addition & 1 deletion conex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

from conex.nn.Structure import *
from conex.nn.Modules import *
from conex.nn.Config import *
from conex.nn.config import *
9 changes: 9 additions & 0 deletions conex/behaviors/layer/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
class SpikeNdDataset(Behavior):
"""
This behavior ease loading dataset as spikes for `InputLayer`.

Args:
dataloader (Dataloader): A pytorch dataloader kind returning up to a triole of (sensory, location, label).
ndim_sensory (int): Sensory's number of dimension refering to a single instance.
ndim_location (int): Location's number of dimension refering to a single instance.
have_location (bool): Whether dataloader returns location input.
have_sensory (bool): Whether dataloader returns sensory input.
have_label (bool): Whether dataloader returns label of input.
loop (bool): If True, dataloader repeats.
"""

def initialize(self, layer):
Expand Down
Loading