"""
import numpy as np
A = np.array([[0, 1, 1, 0, 0, 0],
              [1, 0, 1, 0, 0, 0],
              [1, 1, 0, 1, 0, 0],
              [0, 0, 1, 0, 1, 1],
              [0, 0, 0, 1, 0, 1],
              [0, 0, 0, 1, 1, 0]], dtype=float)

import matplotlib.pyplot as plt

import networkx as nx
g = nx.from_numpy_matrix(A)
layout = nx.spring_layout(g, pos=nx.circular_layout(g))
nx.draw(g, pos=layout,with_labels=True, node_color='white')
#plt.show()

d = np.sum(A, axis=0)
D = np.diag(d)
L = D - A
val, Vec = np.linalg.eigh(L)
idx_lambda3 = np.argmin(np.abs(val - 3))
v3 = Vec[:, idx_lambda3]
plt.plot(np.sort(val), linestyle='-', marker='o');


f = Vec[:, np.argsort(val)[1]]
plt.plot(f, linestyle='-', marker='o');
colors = ['orange' if eigv > 0 else 'gray' for eigv in f]
nx.draw(g, pos=layout, with_labels=True, node_color=colors)
plt.show()
"""

import numpy as np
import re, ast
import matplotlib.pyplot as plt
import random

def to_np(string):
    string = re.sub('\s+', '', string)
    return np.array(ast.literal_eval(string))

file = open('data.txt').readlines()
Chem = to_np(file[0][:-1])
Gap = to_np(file[1][:-1])
neuron_ids = to_np(file[2][:-1])
neuron_types = to_np(file[3])


neuron_ids = [""] * 166
Chem = to_np(open('in.txt').readline())
Gap = Chem
neuron_types = neuron_types[:166]

A = Chem + Gap
C = (A + A.T) / 2

degrees = np.sum(C, axis=0)
D = np.diag(degrees)

L = D - C

from scipy import linalg
b = np.sum(C * np.sign(A - A.T), axis=1)
z = linalg.pinv(L) @ b

Dinv2 = np.diag(1 / np.sqrt(degrees))
Q = Dinv2 @ L @ Dinv2

val, Vec = linalg.eig(Q)

smallest_first = np.argsort(val)
val = val[smallest_first]
Vec = Vec[:, smallest_first]

x = Dinv2 @ Vec[:, 1]

from matplotlib.colors import ListedColormap
from matplotlib.collections import LineCollection


def plot_connectome(x_coords, y_coords, conn_matrix, *,
                    labels=(), types=None, type_names=('',),
                    xlabel='', ylabel=''):
    """Plot neurons as points connected by lines.

    Neurons can have different types (up to 6 distinct colors).

    Parameters
    ----------
    x_coords, y_coords : array of float, shape (N,)
        The x-coordinates and y-coordinates of the neurons.
    conn_matrix : array or sparse matrix of float, shape (N, N)
        The connectivity matrix, with non-zero entry (i, j) if and only
        if node i and node j are connected.
    labels : array-like of string, shape (N,), optional
        The names of the nodes.
    types : array of int, shape (N,), optional
        The type (e.g. sensory neuron, interneuron) of each node.
    type_names : array-like of string, optional
        The name of each value of `types`. For example, if a 0 in
        `types` means "sensory neuron", then `type_names[0]` should
        be "sensory neuron".
    xlabel, ylabel : str, optional
        Labels for the axes.
    """
    if types is None:
        types = np.zeros(x_coords.shape, dtype=int)
    ntypes = len(np.unique(types))
    colors = plt.rcParams['axes.prop_cycle'][:ntypes].by_key()['color']
    cmap = ListedColormap(colors)

    fig, ax = plt.subplots()

    # plot neuron locations:
    for neuron_type in range(ntypes):
        plotting = (types == neuron_type)
        pts = ax.scatter(x_coords[plotting], y_coords[plotting],
                         c=cmap(neuron_type), s=4, zorder=1)
        pts.set_label(type_names[neuron_type])

    # add text labels:
    for x, y, label in zip(x_coords, y_coords, labels):
        ax.text(x, y, '   ' + label,
                verticalalignment='center', fontsize=3, zorder=2)

    # plot edges
    pre, post = np.nonzero(conn_matrix)
    links = np.array([[x_coords[pre], x_coords[post]],
                      [y_coords[pre], y_coords[post]]]).T
    ax.add_collection(LineCollection(links, color='lightgray',
                                     lw=0.3, alpha=0.5, zorder=0))

    ax.legend(scatterpoints=3, fontsize=6)

    ax.set_xlabel(xlabel, fontsize=8)
    ax.set_ylabel(ylabel, fontsize=8)

    plt.show()

plot_connectome(x, z, C, labels=neuron_ids, types=neuron_types,
                type_names=['sensory neurons', 'interneurons',
                            'motor neurons'],
                xlabel='Affinity eigenvector 1', ylabel='Processing depth')

9d4db946bc4eea98588abcfba2c3dd954886b4c6
<mask token>


def to_np(string):
    string = re.sub('\\s+', '', string)
    return np.array(ast.literal_eval(string))


<mask token>


def plot_connectome(x_coords, y_coords, conn_matrix, *, labels=(), types=
    None, type_names=('',), xlabel='', ylabel=''):
    """Plot neurons as points connected by lines.

    Neurons can have different types (up to 6 distinct colors).

    Parameters
    ----------
    x_coords, y_coords : array of float, shape (N,)
        The x-coordinates and y-coordinates of the neurons.
    conn_matrix : array or sparse matrix of float, shape (N, N)
        The connectivity matrix, with non-zero entry (i, j) if and only
        if node i and node j are connected.
    labels : array-like of string, shape (N,), optional
        The names of the nodes.
    types : array of int, shape (N,), optional
        The type (e.g. sensory neuron, interneuron) of each node.
    type_names : array-like of string, optional
        The name of each value of `types`. For example, if a 0 in
        `types` means "sensory neuron", then `type_names[0]` should
        be "sensory neuron".
    xlabel, ylabel : str, optional
        Labels for the axes.
    """
    if types is None:
        types = np.zeros(x_coords.shape, dtype=int)
    ntypes = len(np.unique(types))
    colors = plt.rcParams['axes.prop_cycle'][:ntypes].by_key()['color']
    cmap = ListedColormap(colors)
    fig, ax = plt.subplots()
    for neuron_type in range(ntypes):
        plotting = types == neuron_type
        pts = ax.scatter(x_coords[plotting], y_coords[plotting], c=cmap(
            neuron_type), s=4, zorder=1)
        pts.set_label(type_names[neuron_type])
    for x, y, label in zip(x_coords, y_coords, labels):
        ax.text(x, y, '   ' + label, verticalalignment='center', fontsize=3,
            zorder=2)
    pre, post = np.nonzero(conn_matrix)
    links = np.array([[x_coords[pre], x_coords[post]], [y_coords[pre],
        y_coords[post]]]).T
    ax.add_collection(LineCollection(links, color='lightgray', lw=0.3,
        alpha=0.5, zorder=0))
    ax.legend(scatterpoints=3, fontsize=6)
    ax.set_xlabel(xlabel, fontsize=8)
    ax.set_ylabel(ylabel, fontsize=8)
    plt.show()


<mask token>

<mask token>


def to_np(string):
    string = re.sub('\\s+', '', string)
    return np.array(ast.literal_eval(string))


<mask token>


def plot_connectome(x_coords, y_coords, conn_matrix, *, labels=(), types=
    None, type_names=('',), xlabel='', ylabel=''):
    """Plot neurons as points connected by lines.

    Neurons can have different types (up to 6 distinct colors).

    Parameters
    ----------
    x_coords, y_coords : array of float, shape (N,)
        The x-coordinates and y-coordinates of the neurons.
    conn_matrix : array or sparse matrix of float, shape (N, N)
        The connectivity matrix, with non-zero entry (i, j) if and only
        if node i and node j are connected.
    labels : array-like of string, shape (N,), optional
        The names of the nodes.
    types : array of int, shape (N,), optional
        The type (e.g. sensory neuron, interneuron) of each node.
    type_names : array-like of string, optional
        The name of each value of `types`. For example, if a 0 in
        `types` means "sensory neuron", then `type_names[0]` should
        be "sensory neuron".
    xlabel, ylabel : str, optional
        Labels for the axes.
    """
    if types is None:
        types = np.zeros(x_coords.shape, dtype=int)
    ntypes = len(np.unique(types))
    colors = plt.rcParams['axes.prop_cycle'][:ntypes].by_key()['color']
    cmap = ListedColormap(colors)
    fig, ax = plt.subplots()
    for neuron_type in range(ntypes):
        plotting = types == neuron_type
        pts = ax.scatter(x_coords[plotting], y_coords[plotting], c=cmap(
            neuron_type), s=4, zorder=1)
        pts.set_label(type_names[neuron_type])
    for x, y, label in zip(x_coords, y_coords, labels):
        ax.text(x, y, '   ' + label, verticalalignment='center', fontsize=3,
            zorder=2)
    pre, post = np.nonzero(conn_matrix)
    links = np.array([[x_coords[pre], x_coords[post]], [y_coords[pre],
        y_coords[post]]]).T
    ax.add_collection(LineCollection(links, color='lightgray', lw=0.3,
        alpha=0.5, zorder=0))
    ax.legend(scatterpoints=3, fontsize=6)
    ax.set_xlabel(xlabel, fontsize=8)
    ax.set_ylabel(ylabel, fontsize=8)
    plt.show()


plot_connectome(x, z, C, labels=neuron_ids, types=neuron_types, type_names=
    ['sensory neurons', 'interneurons', 'motor neurons'], xlabel=
    'Affinity eigenvector 1', ylabel='Processing depth')

<mask token>


def to_np(string):
    string = re.sub('\\s+', '', string)
    return np.array(ast.literal_eval(string))


file = open('data.txt').readlines()
Chem = to_np(file[0][:-1])
Gap = to_np(file[1][:-1])
neuron_ids = to_np(file[2][:-1])
neuron_types = to_np(file[3])
neuron_ids = [''] * 166
Chem = to_np(open('in.txt').readline())
Gap = Chem
neuron_types = neuron_types[:166]
A = Chem + Gap
C = (A + A.T) / 2
degrees = np.sum(C, axis=0)
D = np.diag(degrees)
L = D - C
<mask token>
b = np.sum(C * np.sign(A - A.T), axis=1)
z = linalg.pinv(L) @ b
Dinv2 = np.diag(1 / np.sqrt(degrees))
Q = Dinv2 @ L @ Dinv2
val, Vec = linalg.eig(Q)
smallest_first = np.argsort(val)
val = val[smallest_first]
Vec = Vec[:, smallest_first]
x = Dinv2 @ Vec[:, 1]
<mask token>


def plot_connectome(x_coords, y_coords, conn_matrix, *, labels=(), types=
    None, type_names=('',), xlabel='', ylabel=''):
    """Plot neurons as points connected by lines.

    Neurons can have different types (up to 6 distinct colors).

    Parameters
    ----------
    x_coords, y_coords : array of float, shape (N,)
        The x-coordinates and y-coordinates of the neurons.
    conn_matrix : array or sparse matrix of float, shape (N, N)
        The connectivity matrix, with non-zero entry (i, j) if and only
        if node i and node j are connected.
    labels : array-like of string, shape (N,), optional
        The names of the nodes.
    types : array of int, shape (N,), optional
        The type (e.g. sensory neuron, interneuron) of each node.
    type_names : array-like of string, optional
        The name of each value of `types`. For example, if a 0 in
        `types` means "sensory neuron", then `type_names[0]` should
        be "sensory neuron".
    xlabel, ylabel : str, optional
        Labels for the axes.
    """
    if types is None:
        types = np.zeros(x_coords.shape, dtype=int)
    ntypes = len(np.unique(types))
    colors = plt.rcParams['axes.prop_cycle'][:ntypes].by_key()['color']
    cmap = ListedColormap(colors)
    fig, ax = plt.subplots()
    for neuron_type in range(ntypes):
        plotting = types == neuron_type
        pts = ax.scatter(x_coords[plotting], y_coords[plotting], c=cmap(
            neuron_type), s=4, zorder=1)
        pts.set_label(type_names[neuron_type])
    for x, y, label in zip(x_coords, y_coords, labels):
        ax.text(x, y, '   ' + label, verticalalignment='center', fontsize=3,
            zorder=2)
    pre, post = np.nonzero(conn_matrix)
    links = np.array([[x_coords[pre], x_coords[post]], [y_coords[pre],
        y_coords[post]]]).T
    ax.add_collection(LineCollection(links, color='lightgray', lw=0.3,
        alpha=0.5, zorder=0))
    ax.legend(scatterpoints=3, fontsize=6)
    ax.set_xlabel(xlabel, fontsize=8)
    ax.set_ylabel(ylabel, fontsize=8)
    plt.show()


plot_connectome(x, z, C, labels=neuron_ids, types=neuron_types, type_names=
    ['sensory neurons', 'interneurons', 'motor neurons'], xlabel=
    'Affinity eigenvector 1', ylabel='Processing depth')

<mask token>
import numpy as np
import re, ast
import matplotlib.pyplot as plt
import random


def to_np(string):
    string = re.sub('\\s+', '', string)
    return np.array(ast.literal_eval(string))


file = open('data.txt').readlines()
Chem = to_np(file[0][:-1])
Gap = to_np(file[1][:-1])
neuron_ids = to_np(file[2][:-1])
neuron_types = to_np(file[3])
neuron_ids = [''] * 166
Chem = to_np(open('in.txt').readline())
Gap = Chem
neuron_types = neuron_types[:166]
A = Chem + Gap
C = (A + A.T) / 2
degrees = np.sum(C, axis=0)
D = np.diag(degrees)
L = D - C
from scipy import linalg
b = np.sum(C * np.sign(A - A.T), axis=1)
z = linalg.pinv(L) @ b
Dinv2 = np.diag(1 / np.sqrt(degrees))
Q = Dinv2 @ L @ Dinv2
val, Vec = linalg.eig(Q)
smallest_first = np.argsort(val)
val = val[smallest_first]
Vec = Vec[:, smallest_first]
x = Dinv2 @ Vec[:, 1]
from matplotlib.colors import ListedColormap
from matplotlib.collections import LineCollection


def plot_connectome(x_coords, y_coords, conn_matrix, *, labels=(), types=
    None, type_names=('',), xlabel='', ylabel=''):
    """Plot neurons as points connected by lines.

    Neurons can have different types (up to 6 distinct colors).

    Parameters
    ----------
    x_coords, y_coords : array of float, shape (N,)
        The x-coordinates and y-coordinates of the neurons.
    conn_matrix : array or sparse matrix of float, shape (N, N)
        The connectivity matrix, with non-zero entry (i, j) if and only
        if node i and node j are connected.
    labels : array-like of string, shape (N,), optional
        The names of the nodes.
    types : array of int, shape (N,), optional
        The type (e.g. sensory neuron, interneuron) of each node.
    type_names : array-like of string, optional
        The name of each value of `types`. For example, if a 0 in
        `types` means "sensory neuron", then `type_names[0]` should
        be "sensory neuron".
    xlabel, ylabel : str, optional
        Labels for the axes.
    """
    if types is None:
        types = np.zeros(x_coords.shape, dtype=int)
    ntypes = len(np.unique(types))
    colors = plt.rcParams['axes.prop_cycle'][:ntypes].by_key()['color']
    cmap = ListedColormap(colors)
    fig, ax = plt.subplots()
    for neuron_type in range(ntypes):
        plotting = types == neuron_type
        pts = ax.scatter(x_coords[plotting], y_coords[plotting], c=cmap(
            neuron_type), s=4, zorder=1)
        pts.set_label(type_names[neuron_type])
    for x, y, label in zip(x_coords, y_coords, labels):
        ax.text(x, y, '   ' + label, verticalalignment='center', fontsize=3,
            zorder=2)
    pre, post = np.nonzero(conn_matrix)
    links = np.array([[x_coords[pre], x_coords[post]], [y_coords[pre],
        y_coords[post]]]).T
    ax.add_collection(LineCollection(links, color='lightgray', lw=0.3,
        alpha=0.5, zorder=0))
    ax.legend(scatterpoints=3, fontsize=6)
    ax.set_xlabel(xlabel, fontsize=8)
    ax.set_ylabel(ylabel, fontsize=8)
    plt.show()


plot_connectome(x, z, C, labels=neuron_ids, types=neuron_types, type_names=
    ['sensory neurons', 'interneurons', 'motor neurons'], xlabel=
    'Affinity eigenvector 1', ylabel='Processing depth')

"""
import numpy as np
A = np.array([[0, 1, 1, 0, 0, 0],
              [1, 0, 1, 0, 0, 0],
              [1, 1, 0, 1, 0, 0],
              [0, 0, 1, 0, 1, 1],
              [0, 0, 0, 1, 0, 1],
              [0, 0, 0, 1, 1, 0]], dtype=float)

import matplotlib.pyplot as plt

import networkx as nx
g = nx.from_numpy_matrix(A)
layout = nx.spring_layout(g, pos=nx.circular_layout(g))
nx.draw(g, pos=layout,with_labels=True, node_color='white')
#plt.show()

d = np.sum(A, axis=0)
D = np.diag(d)
L = D - A
val, Vec = np.linalg.eigh(L)
idx_lambda3 = np.argmin(np.abs(val - 3))
v3 = Vec[:, idx_lambda3]
plt.plot(np.sort(val), linestyle='-', marker='o');


f = Vec[:, np.argsort(val)[1]]
plt.plot(f, linestyle='-', marker='o');
colors = ['orange' if eigv > 0 else 'gray' for eigv in f]
nx.draw(g, pos=layout, with_labels=True, node_color=colors)
plt.show()
"""

import numpy as np
import re, ast
import matplotlib.pyplot as plt
import random

def to_np(string):
    string = re.sub('\s+', '', string)
    return np.array(ast.literal_eval(string))

file = open('data.txt').readlines()
Chem = to_np(file[0][:-1])
Gap = to_np(file[1][:-1])
neuron_ids = to_np(file[2][:-1])
neuron_types = to_np(file[3])


neuron_ids = [""] * 166
Chem = to_np(open('in.txt').readline())
Gap = Chem
neuron_types = neuron_types[:166]

A = Chem + Gap
C = (A + A.T) / 2

degrees = np.sum(C, axis=0)
D = np.diag(degrees)

L = D - C

from scipy import linalg
b = np.sum(C * np.sign(A - A.T), axis=1)
z = linalg.pinv(L) @ b

Dinv2 = np.diag(1 / np.sqrt(degrees))
Q = Dinv2 @ L @ Dinv2

val, Vec = linalg.eig(Q)

smallest_first = np.argsort(val)
val = val[smallest_first]
Vec = Vec[:, smallest_first]

x = Dinv2 @ Vec[:, 1]

from matplotlib.colors import ListedColormap
from matplotlib.collections import LineCollection


def plot_connectome(x_coords, y_coords, conn_matrix, *,
                    labels=(), types=None, type_names=('',),
                    xlabel='', ylabel=''):
    """Plot neurons as points connected by lines.

    Neurons can have different types (up to 6 distinct colors).

    Parameters
    ----------
    x_coords, y_coords : array of float, shape (N,)
        The x-coordinates and y-coordinates of the neurons.
    conn_matrix : array or sparse matrix of float, shape (N, N)
        The connectivity matrix, with non-zero entry (i, j) if and only
        if node i and node j are connected.
    labels : array-like of string, shape (N,), optional
        The names of the nodes.
    types : array of int, shape (N,), optional
        The type (e.g. sensory neuron, interneuron) of each node.
    type_names : array-like of string, optional
        The name of each value of `types`. For example, if a 0 in
        `types` means "sensory neuron", then `type_names[0]` should
        be "sensory neuron".
    xlabel, ylabel : str, optional
        Labels for the axes.
    """
    if types is None:
        types = np.zeros(x_coords.shape, dtype=int)
    ntypes = len(np.unique(types))
    colors = plt.rcParams['axes.prop_cycle'][:ntypes].by_key()['color']
    cmap = ListedColormap(colors)

    fig, ax = plt.subplots()

    # plot neuron locations:
    for neuron_type in range(ntypes):
        plotting = (types == neuron_type)
        pts = ax.scatter(x_coords[plotting], y_coords[plotting],
                         c=cmap(neuron_type), s=4, zorder=1)
        pts.set_label(type_names[neuron_type])

    # add text labels:
    for x, y, label in zip(x_coords, y_coords, labels):
        ax.text(x, y, '   ' + label,
                verticalalignment='center', fontsize=3, zorder=2)

    # plot edges
    pre, post = np.nonzero(conn_matrix)
    links = np.array([[x_coords[pre], x_coords[post]],
                      [y_coords[pre], y_coords[post]]]).T
    ax.add_collection(LineCollection(links, color='lightgray',
                                     lw=0.3, alpha=0.5, zorder=0))

    ax.legend(scatterpoints=3, fontsize=6)

    ax.set_xlabel(xlabel, fontsize=8)
    ax.set_ylabel(ylabel, fontsize=8)

    plt.show()

plot_connectome(x, z, C, labels=neuron_ids, types=neuron_types,
                type_names=['sensory neurons', 'interneurons',
                            'motor neurons'],
                xlabel='Affinity eigenvector 1', ylabel='Processing depth')
