Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Yuryatin authored Sep 9, 2022
1 parent ff344fd commit 0789af3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions multipartitegraph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from api import *
68 changes: 68 additions & 0 deletions multipartitegraph/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import copy
import string
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


class myNode:
def __init__(self, x, y, _id):
self.x = x
self.y = y
self.id = _id
self.label = _id.upper()
self.next = []

def plot(self, ax):
ax.text(self.x, self.y, self.label, transform=ax.transAxes, fontsize=18, bbox=dict(boxstyle='circle', facecolor='wheat', alpha=1))

def add_child(self, x):
self.next.append(x)

def remove_child(self, x):
self.next.remove(x)


class Net:
def __init__(self, arrays:list):
self.nodes = dict()
self.arrays = copy.deepcopy(arrays)
self.xdim = len(arrays) + 1
self.ydim = np.zeros(self.xdim, dtype=np.int64)
self.pivot = pd.DataFrame(columns=['source', 'target', 'linked'], dtype=('str', np.int64))
for i, array in enumerate(arrays):
if i:
try:
np.dot(arrays[i-1].values, array.values)
except:
raise Error("the dimentions of the matrices %d and %d dimentions don't match each other" % (i, i+1))
self.ydim[i] = array.shape[0]
self.pivot = pd.concat([self.pivot, array.reset_index().melt(id_vars='source', var_name='target', value_name='linked')])
self.ydim[i+1] = array.shape[1]
max_y = 0
for i in range(self.xdim):
max_y = max(max_y, self.ydim[i])
odd = bool(max_y % 2)
self.pivot = self.pivot.loc[self.pivot['linked']==1, ['source', 'target']].set_index('source')
self.x_coord = np.linspace(0.05, 0.95, self.xdim)
self.y_coords = []
for x in range(self.xdim):
if odd == bool(self.ydim[x] % 2):
self.y_coords.append(np.linspace(0.1, 0.9, self.ydim[x]))
else:
self.y_coords.append(np.linspace(0.1, 0.9, self.ydim[x] * 2 + 1)[1::2])
for y in range(self.ydim[x]):
_id = string.ascii_lowercase[x] + str(y+1)
self.nodes[_id] = myNode(self.x_coord[x], self.y_coords[x][y], _id)
for row in range(self.pivot.shape[0]):
self.nodes[self.pivot.index[row]].next.append(self.nodes[self.pivot.iloc[row]['target']])


def plot(self):
self.fig, self.ax = plt.subplots()
for i in self.nodes:
self.nodes[i].plot(self.ax)
for nextNode in self.nodes[i].next:
self.ax.plot([self.nodes[i].x, nextNode.x], [self.nodes[i].y, nextNode.y], 'k-', lw=2, transform=self.ax.transAxes)
plt.axis('off')
plt.show()
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[metadata]
description-file = README.md
license_files=LICENSE
14 changes: 14 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from distutils.core import setup

setup(
name = 'multipartitegraph',
packages = ['multipartitegraph'],
version = '0.0.1',
license = 'MIT',
description = 'This package draws multipartite graph with fixed levels from a list of matrix dataframes',
url = 'https://github.com/yuryatin/multipartitegraph',
download_url = 'https://github.com/yuryatin/multipartitegraph/archive/refs/tags/v0.0.1.tar.gz',
keywords = ['multipartite', 'graph', 'fixed', 'levels', 'plotting'],
classifiers = [],
install_requires = ['copy','string','numpy','pandas','matplotlib']
)

0 comments on commit 0789af3

Please sign in to comment.