Skip to content

Commit

Permalink
Merge pull request #12 from yasinthanvickneswaran/main
Browse files Browse the repository at this point in the history
Latest stable with graphs
  • Loading branch information
pgleeson authored Jul 18, 2024
2 parents 3d640ac + af1f7ed commit 9321820
Show file tree
Hide file tree
Showing 123 changed files with 1,445 additions and 7,766 deletions.
115 changes: 81 additions & 34 deletions cect/Comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ def get_cell_link(cell_name, html=False):
return cell_name


def get_2d_graph_markdown(reader_name, view_name, connectome, synclass, indent=" "):

fig = connectome.to_plotly_graph_fig(synclass)

asset_filename = "assets/%s_%s_%s_graph.json" % (
reader_name,
view_name,
synclass.replace(" ", "_"),
)

with open("./docs/%s" % asset_filename, "w") as asset_file:
asset_file.write(fig.to_json())

if np.sum(connectome.connections[synclass]) == 0:
return "\n%sNo connections of type **%s** in the **%s** for **%s**...\n" % (
indent,
synclass,
view_name,
reader_name,
)

return '\n%s```plotly\n%s---8<-- "./%s"\n%s```\n' % (
indent,
indent,
asset_filename,
indent,
)


def get_matrix_markdown(reader_name, view_name, connectome, synclass, indent=" "):
fig = connectome.to_plotly_matrix_fig(synclass)

Expand Down Expand Up @@ -206,52 +235,70 @@ def get_matrix_markdown(reader_name, view_name, connectome, synclass, indent="
)

if reader_name in reader_pages:
filename = "docs/%s.md" % reader_pages[reader_name]

with open(filename, "w") as f:
f.write("## %s\n" % reader_name)
matrix_filename = "docs/%s.md" % reader_pages[reader_name]
graph_filename = "docs/%s_graph.md" % reader_pages[reader_name]

for filename in [matrix_filename, graph_filename]:

with open(filename, "w") as f:

matrix = filename==matrix_filename

f.write("## %s\n" % reader_name)
f.write("%s\n\n" % READER_DESCRIPTION)

f.write("[View as matrix](../%s/index.html){ .md-button } [View as graph](../%s_graph/index.html){ .md-button }\n\n" % (reader_pages[reader_name], reader_pages[reader_name]))

f.write("%s\n" % READER_DESCRIPTION)
if connectome is not None:
from ConnectomeView import ALL_VIEWS

if connectome is not None:
from ConnectomeView import ALL_VIEWS
indent = " "

indent = " "
for view in ALL_VIEWS:
cv = connectome.get_connectome_view(view)

for view in ALL_VIEWS:
cv = connectome.get_connectome_view(view)
f.write('=== "%s"\n' % view.name)

f.write('=== "%s"\n' % view.name)
for sc in view.synclass_sets:
f.write(indent + '=== "%s"\n' % sc)

for sc in view.synclass_sets:
f.write(indent + '=== "%s"\n' % sc)
if matrix:
f.write(
get_matrix_markdown(
reader_name, view.name, cv, sc, indent=indent + indent
)
)

else:

f.write(
get_matrix_markdown(
reader_name, view.name, cv, sc, indent=indent + indent
)
)
f.write(
get_2d_graph_markdown(
reader_name, view.name, cv, sc, indent=indent + indent
)
)


cell_types = {
"Neurons": preferred,
"Missing neurons": missing_preferred,
"Muscles": muscles,
"Other cells": not_in_preferred,
}
cell_types = {
"Neurons": preferred,
"Missing neurons": missing_preferred,
"Muscles": muscles,
"Other cells": not_in_preferred,
}

for t in cell_types:
f.write("\n### %s (%i)\n" % (t, len(cell_types[t])))
if len(cell_types[t]) > 0:
f.write("<details><summary>Full list of %s</summary>\n" % t)
ss = sorted(cell_types[t])
for n in ss:
f.write("%s" % (get_cell_link(n, True)))
if n is not ss[-1]:
f.write(" | ")
for t in cell_types:
f.write("\n### %s (%i)\n" % (t, len(cell_types[t])))
if len(cell_types[t]) > 0:
f.write("<details><summary>Full list of %s</summary>\n" % t)
ss = sorted(cell_types[t])
for n in ss:
f.write("%s" % (get_cell_link(n, True)))
if n is not ss[-1]:
f.write(" | ")

f.write("\n</details>\n")
f.write("\n</details>\n")

print_("Written page: %s" % filename)
print_("Written page: %s" % filename)

all_data[ref] = [
len(preferred),
Expand Down
81 changes: 80 additions & 1 deletion cect/ConnectomeDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import numpy as np


class ConnectomeDataset:
DEFAULT_DTYPE = np.float64

Expand Down Expand Up @@ -115,6 +114,7 @@ def summary(self):
)
return info


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

Expand All @@ -129,6 +129,85 @@ def to_plotly_matrix_fig(self, synclass, color_continuous_scale=DEFAULT_COLORMAP
)

return fig

def to_plotly_graph_fig(self, synclass):
conn_array = self.connections[synclass]
import plotly.graph_objects as go
import networkx as nx

G = nx.Graph(conn_array)
pos = nx.spring_layout(G)


node_x = [pos[i][0] for i in G.nodes()]
node_y = [pos[i][1] for i in G.nodes()]

edge_x = []
edge_y = []

for edge in G.edges():
x0, y0 = pos[edge[0]]
x1, y1 = pos[edge[1]]
edge_x.append(x0)
edge_x.append(x1)
edge_x.append(None)
edge_y.append(y0)
edge_y.append(y1)
edge_y.append(None)

# Add nodes to the figure
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
mode='lines',
text=self.nodes,
line=dict(color='red', width=1),
hoverinfo="none")

node_adjacencies = []
node_text = []
for node, adjacencies in enumerate(G.adjacency()):
node_adjacencies.append(len(adjacencies[1]))

for i, node_value in enumerate(self.nodes):
num_connections = node_adjacencies[i]
node_text.append(f"{node_value}<br>Number of connections: {num_connections}")

node_trace = go.Scatter(x=node_x, y=node_y,
mode='markers',
text=self.nodes,
marker=dict(
showscale=True,
colorscale='YlGnBu',
reversescale=True,
color=[],
size=10,
colorbar=dict(
thickness=15,
title='Node Connections',
xanchor='left',
titleside='right'
),
line_width=2),
hoverinfo="text",
)

node_trace.marker.color = node_adjacencies
node_trace.text = node_text


fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
showlegend=False,
hovermode="closest",
margin=dict(b=20,l=5,r=5,t=40),
xaxis=dict(showgrid=False, zeroline=False),
yaxis=dict(showgrid=False, zeroline=False),
)

)


return fig


if __name__ == "__main__":
Expand Down
25 changes: 24 additions & 1 deletion cect/ConnectomeReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,29 @@
"MVR23",
"MVR24",
"MVULVA",
"pm1",
"pm2D",
"pm2VL",
"pm2VR",
"pm3VL",
"pm3VR",
"pm4",
"pm5D",
"pm5VR",
"pm5VL",
"pm6",
"pm6D",
"pm6VR",
"pm6VL",
"pm7D",
"pm7VL",
"pm7VR",
"pm8",
"mc1",
"mc2dl",
"mc2dr",
"mc2V",
"mc3V",
]

PHARYNX_CELLS = [
Expand Down Expand Up @@ -454,7 +477,7 @@ def convert_to_preferred_muscle_name(muscle):


def get_all_muscle_prefixes():
return ["pm", "vm", "um", "BWM-D", "BWM-V", "LegacyBodyWallMuscles", "vBWM", "dBWM"]
return ["pm", "vm", "um", "BWM-D", "BWM-V", "LegacyBodyWallMuscles", "vBWM", "dBWM","mc"]


def get_body_wall_muscle_prefixes():
Expand Down
17 changes: 11 additions & 6 deletions cect/Cook2019DataReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def get_synclass(cell, syntype):
return "GABA"
return "Acetylcholine"

def get_instance():
return Cook2019DataReader()

class Cook2019DataReader(ConnectomeDataset):
spreadsheet_location = os.path.dirname(os.path.abspath(__file__)) + "/data/"
Expand Down Expand Up @@ -203,17 +205,20 @@ def read_muscle_data(self):

return neurons, muscles, conns

tdr_instance = get_instance()
read_data = tdr_instance.read_data
read_muscle_data = tdr_instance.read_muscle_data

def main():
cdr = Cook2019DataReader()
read_data = cdr.read_data
read_muscle_data = cdr.read_muscle_data

cells, neuron_conns = read_data(include_nonconnected_cells=True)
neurons2muscles, muscles, muscle_conns = read_muscle_data()
def main():
cells, neuron_conns = tdr_instance.read_data(include_nonconnected_cells=True)
neurons2muscles, muscles, muscle_conns = tdr_instance.read_muscle_data()

analyse_connections(cells, neuron_conns, neurons2muscles, muscles, muscle_conns)

print_(" -- Finished analysing connections using: %s" % os.path.basename(__file__))

print(tdr_instance.summary())

if __name__ == "__main__":
main()
Loading

0 comments on commit 9321820

Please sign in to comment.