Skip to content

Commit

Permalink
Updating loading of Brittin dataset, emsuring symm conns added
Browse files Browse the repository at this point in the history
  • Loading branch information
pgleeson committed Dec 13, 2024
1 parent 5de9c5d commit feb7921
Show file tree
Hide file tree
Showing 21 changed files with 2,244 additions and 2,159 deletions.
49 changes: 36 additions & 13 deletions cect/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@


def test_bilaterals():
from cect.White_whole import get_instance
from cect.BrittinDataReader import get_instance

"""
from cect.TestDataReader import get_instance
from cect.White_whole import get_instance
from cect.WormNeuroAtlasMAReader import get_instance
from cect.WormNeuroAtlasFuncReader import get_instance
from cect.Cook2019HermReader import get_instance
from cect.Cook2019MaleReader import get_instance
from cect.WitvlietDataReader1 import get_instance
from cect.WitvlietDataReader8 import get_instance
from cect.RipollSanchezDataReader import get_instance
from cect.TestDataReader import get_instance
from cect.Cook2020DataReader import get_instance
from cect.ConnectomeView import NONPHARYNGEAL_NEURONS_VIEW as view
from cect.ConnectomeView import PHARYNX_VIEW as view"""
from cect.ConnectomeView import PHARYNX_VIEW as view
from cect.ConnectomeView import NONPHARYNGEAL_NEURONS_VIEW as view"""
from cect.ConnectomeView import NEURONS_VIEW as view

print(
Expand Down Expand Up @@ -52,19 +58,27 @@ def array_info(conn_array):
new_conn_array = np.zeros([dim, dim], dtype=np.float64)
array_info(new_conn_array)

for synclass in ["Chemical Exc", "Electrical"]:
for synclass in [
"Chemical Exc",
"Electrical",
"Contact",
"Functional",
"Extrasynaptic",
]:
print(" Adding conns of type: %s" % synclass)
conns_cs = cds2.connections[synclass]

array_info(conns_cs)
for i in range(len(conns_cs)):
for j in range(len(conns_cs[i])):
new_conn_array[i, j] = new_conn_array[i, j] or conns_cs[i, j] > 0
if (
i != j
): # Kim et al 2024: Self-connections are treated as nonexistent (∀𝐴𝑖𝑖 = 0).
new_conn_array[i, j] = new_conn_array[i, j] or conns_cs[i, j] > 0

amal = "CS+GJ"
print("Amalgamated array:")
array_info(new_conn_array)
cds2.connections[amal] = new_conn_array

# fig = cds2.to_plotly_hive_plot_fig(list(view.synclass_sets.keys())[0], view)
# fig = cds2.to_plotly_graph_fig(synclass, view)
Expand All @@ -73,7 +87,12 @@ def array_info(conn_array):
conn_count = 0
symm_conn_count = 0

POS_NEG_COLORMAP2 = ["red", "pink", "white", "lightblue", "blue"]
POS_ZERO_NEG_COLORMAP2 = ["red", "pink", "white", "lightblue", "blue"]
POS_ZERO_COLORMAP2 = ["white", "lightblue", "blue"]

colormap = POS_ZERO_COLORMAP2

scaled_conn_array = np.array(new_conn_array)

for i in range(len(new_conn_array)):
pre = cds2.nodes[i]
Expand All @@ -84,26 +103,30 @@ def array_info(conn_array):
w = new_conn_array[i][j]
if w != 0:
print(f"Connection {conn_count}:\t{pre}->{post} ({w})")
assert pre is not post
w_ = new_conn_array[cds2.nodes.index(pre_), cds2.nodes.index(post_)]
print(f" - Mirror:\t{pre_}->{post_} ({w_})\n")
symm_conn_count += w_
if w_ == 0:
new_conn_array[i][j] = 0.5
new_conn_array[cds2.nodes.index(pre_), cds2.nodes.index(post_)] = -1
scaled_conn_array[i][j] = 0.5
scaled_conn_array[
cds2.nodes.index(pre_), cds2.nodes.index(post_)
] = -1
colormap = POS_ZERO_NEG_COLORMAP2
conn_count += 1

print(
f"Of {(len(new_conn_array)**2)} possible edges, {conn_count} are connected, {symm_conn_count} are mirrored {'%.2f'%(100*symm_conn_count/conn_count)}% "
)

cds2.connections[amal] = scaled_conn_array

# import plotly.io as pio
# pio.renderers.default = "browser"
if "-nogui" not in sys.argv:
# cds2.connection_number_plot

fig = cds2.to_plotly_matrix_fig(
amal, view, POS_NEG_COLORMAP2, bold_bilaterals=True
)
fig = cds2.to_plotly_matrix_fig(amal, view, colormap, bold_bilaterals=True)

fig.show()

Expand Down
34 changes: 32 additions & 2 deletions cect/BrittinDataReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from cect.ConnectomeDataset import ConnectomeDataset
from cect.ConnectomeDataset import get_dataset_source_on_github
from cect.Cells import is_one_of_bilateral_pair
from cect.Cells import get_contralateral_neuron

import os
from openpyxl import load_workbook
Expand Down Expand Up @@ -40,7 +42,7 @@ def __init__(self, reference_graph):

cells, neuron_conns = self.read_data()
for conn in neuron_conns:
self.add_connection_info(conn)
self.add_connection_info(conn, check_overwritten_connections=True)

def read_data(self):
cells = []
Expand All @@ -50,6 +52,9 @@ def read_data(self):

sheet = wb.get_sheet_by_name(self.reference_graph)

bilaterals = []
single_cells = []

print_("Opened sheet %s in Excel file: %s" % (sheet, filename))
# print(dir(sheet))

Expand All @@ -64,15 +69,40 @@ def read_data(self):
syntype = "Contact"
synclass = "%s%s" % (self.reference_graph, row[3].value)
synclass = "Contact"

ci = ConnectionInfo(pre, post, num, syntype, synclass)
# print("Adding %s" % ci)
conns.append(ci)
ci = ConnectionInfo(post, pre, num, syntype, synclass)
conns.append(ci)

if pre not in cells:
cells.append(pre)
if post not in cells:
cells.append(post)

pre_ = get_contralateral_neuron(pre)
post_ = get_contralateral_neuron(post)
ci_ = ConnectionInfo(pre_, post_, num, syntype, synclass)
conns.append(ci_)
ci_ = ConnectionInfo(post_, pre_, num, syntype, synclass)
conns.append(ci_)

if pre_ not in cells:
cells.append(pre_)
if post_ not in cells:
cells.append(post_)

for cell in cells:
if is_one_of_bilateral_pair(cell):
bilaterals.append(cell)
else:
single_cells.append(cell)

print_(
"Finished processing %i cells, %i bilateral and %i single cells: %s"
% (len(cells), len(bilaterals), len(single_cells), sorted(single_cells))
)

return cells, conns


Expand Down
9 changes: 4 additions & 5 deletions cect/CellInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,7 @@ def generate_cell_info_pages(connectomes):
conns_to_pep = "???"
conns_from_func = "???"
conns_to_func = "???"
conns_from_cont = "???"
conns_to_cont = "???"
conns_cont = "???"
conns_gj = "???"

tables_md = ""
Expand Down Expand Up @@ -304,7 +303,7 @@ def generate_cell_info_pages(connectomes):
if cds_name == reference_func:
conns_to_func = _get_top_list(conns, max_conn_cells)
if cds_name == reference_cont:
conns_to_cont = _get_top_list(conns, max_conn_cells)
conns_cont = _get_top_list(conns, max_conn_cells)

for c in conns:
cc = get_cell_internal_link(
Expand Down Expand Up @@ -353,7 +352,7 @@ def generate_cell_info_pages(connectomes):
if cds_name == reference_func:
conns_from_func = _get_top_list(conns, max_conn_cells)
if cds_name == reference_cont:
conns_from_cont = _get_top_list(conns, max_conn_cells)
pass # same as to...

for c in conns:
cc = get_cell_internal_link(
Expand Down Expand Up @@ -402,7 +401,7 @@ def generate_cell_info_pages(connectomes):
</tr><tr>
<td>&nbsp;</td> <td colspan="5" align="middle">\u2195</td>
</tr><tr>
<td><b><a href="#membrane-contacts-to-{cell.lower()}" title="Contactome from {reference_cont}">Contactomic</a></b></td> <td colspan="5" align="middle"><b>IN:</b> {conns_to_cont}<br/><b>OUT:</b> {conns_from_cont}</td>
<td><b><a href="#membrane-contacts-to-{cell.lower()}" title="Contactome from {reference_cont}">Contactomic</a></b></td> <td colspan="5" align="middle">{conns_cont}</td>
</tr>
</table>
Expand Down
49 changes: 40 additions & 9 deletions cect/ConnectomeDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cect.Cells import get_standard_color
from cect.Cells import is_bilateral_left
from cect.Cells import is_bilateral_right
from cect.Cells import is_one_of_bilateral_pair
from cect.Cells import are_bilateral_pair
from cect.Cells import SENSORY_NEURONS_NONPHARYNGEAL_COOK
from cect.Cells import INTERNEURONS_NONPHARYNGEAL_COOK
Expand All @@ -16,6 +17,7 @@
from cect.Cells import is_known_muscle
from cect.Cells import is_pharyngeal_cell
from cect.Cells import is_known_cell
from cect.Cells import get_SIM_class

import numpy as np
import math
Expand All @@ -24,8 +26,6 @@
import pprint
import random

from cect.Cells import get_SIM_class

random.seed(10)


Expand Down Expand Up @@ -101,7 +101,9 @@ def to_networkx_graph(self, synclass, view=None):

return Gn

def add_connection_info(self, conn: ConnectionInfo):
def add_connection_info(
self, conn: ConnectionInfo, check_overwritten_connections: bool = False
):
if self.verbose:
print_("---- Adding: %s" % conn)

Expand Down Expand Up @@ -131,6 +133,26 @@ def add_connection_info(self, conn: ConnectionInfo):
pre_index = self.nodes.index(conn.pre_cell)
post_index = self.nodes.index(conn.post_cell)

if conn_array[pre_index, post_index] != 0:
print_(
"Preexisting connection (%i conns already) at (%i,%i) - %s..."
% (len(self.connection_infos), pre_index, post_index, conn)
)
if conn_array[pre_index, post_index] != conn.number:
info = (
" *** Existing connection at (%i,%i), was: %s, setting to: %s"
% (
pre_index,
post_index,
conn_array[pre_index, post_index],
conn.number,
)
)
if check_overwritten_connections:
raise Exception(info)
else:
print_(info)

conn_array[pre_index, post_index] = conn.number

if self.verbose:
Expand Down Expand Up @@ -320,9 +342,13 @@ def summary(self):
)
return info

def to_plotly_matrix_fig(self, synclass, view, color_continuous_scale=None):
import plotly.express as px

def to_plotly_matrix_fig(
self,
synclass: str,
view: str,
color_continuous_scale: bool = None,
bold_bilaterals: bool = False,
):
conn_array = self.connections[synclass]

zmin = np.min(conn_array)
Expand All @@ -345,12 +371,15 @@ def to_plotly_matrix_fig(self, synclass, view, color_continuous_scale=None):
)

def get_color_html(color, node):
return f'<span style="color:{color};">{node}</span>'
font_weight = ""
if bold_bilaterals and is_one_of_bilateral_pair(node):
font_weight = "font-weight:bold;"
return f'<span style="color:{color};{font_weight}">{node}</span>'

node_colors = [
(
view.get_node_set(node).color
if view.has_color()
if view is not None and view.has_color()
else get_standard_color(node)
)
for node in self.nodes
Expand All @@ -363,6 +392,8 @@ def get_color_html(color, node):
get_color_html(color, node) for node, color in zip(self.nodes, node_colors)
]

import plotly.express as px

fig = px.imshow(
conn_array,
labels=dict(x="Postsynaptic", y="Presynaptic", color="Weight"),
Expand Down Expand Up @@ -392,7 +423,7 @@ def get_color_html(color, node):
other_line = False

for i, node_value in enumerate(self.nodes):
if not view.get_node_set(node_value).is_one_cell():
if view is not None and not view.get_node_set(node_value).is_one_cell():
break

if not sens_line and node_value in SENSORY_NEURONS_NONPHARYNGEAL_COOK:
Expand Down
Loading

0 comments on commit feb7921

Please sign in to comment.