Relmath debug

Notebook for debugging stuff - to see some relevant stuff, see relmath-demo.ipynb


In [1]:
get_ipython().display_formatter.formatters


Out[1]:
{'application/javascript': <IPython.core.formatters.JavascriptFormatter at 0x7febe0057a20>,
 'application/json': <IPython.core.formatters.JSONFormatter at 0x7febe0057908>,
 'application/pdf': <IPython.core.formatters.PDFFormatter at 0x7febe0057860>,
 'image/jpeg': <IPython.core.formatters.JPEGFormatter at 0x7febe0057898>,
 'image/png': <IPython.core.formatters.PNGFormatter at 0x7febe0057828>,
 'image/svg+xml': <IPython.core.formatters.SVGFormatter at 0x7febe00577f0>,
 'text/html': <IPython.core.formatters.HTMLFormatter at 0x7febe00577b8>,
 'text/latex': <IPython.core.formatters.LatexFormatter at 0x7febe00578d0>,
 'text/markdown': <IPython.core.formatters.MarkdownFormatter at 0x7febe0057390>,
 'text/plain': <IPython.core.formatters.PlainTextFormatter at 0x7febe0057780>}

In [2]:
import math

def normalize(m):
    for row in m.g:
        norm = math.sqrt(sum((x**2 for x in row)))
        for i in range(len(row)):
            row[i] = row[i] / norm

In [3]:
getattr("ciao", "upper")()


Out[3]:
'CIAO'

Nodes and Links should be supplied to the Graph mark.

Node attributes

Attribute Type Description Default
label str node label mandatory attribute
label_display {center, outside, none} label display options center
shape {circle, ellipse, rect} node shape circle
shape_attrs dict node SVG attributes {'r': 15}

Link Attributes

Attribute Type Description Default
source int source node index mandatory attribute
target int target node index mandatory attribute
value float value of the link. Use np.nan if you do not want a link -

Link data can also be supplied using a link_matrix which is a numpy array of shape (n, n) where n is the number of nodes in the graph

Link Data can be passed in through 3 data attributes -

  1. link_data - list of dicts (e.g. [{'source': 0, 'target': 1, 'value': 10}, {'source': 2, 'target': 1, 'value': 20},...]
  2. link_matrix - 2-d numpy array of shape (n, n) where n is the number of nodes
  3. link_color - 2-d numpy array of shape (n, n) where n is the number of nodes. This attribute can be used to encode the link_color by passing in a link_color scale


In [15]:
import numpy as np
from bqplot import *
from bqplot.marks import Graph
from ipywidgets import Layout, VBox, HBox
from relmath import * 
from relmath_bq import * 

State.s.push_env()

with quote():
    
    M = Rel([
                [0.2, 0.7, 0, 0],
                [0.9,0,0.5,0],
                [0.9,0,0,0]
            ], 
            ['a','b','c'], 
            ['x','y','z','w'])

    
    E = M * M.T
    
#print(E)
#print(E.simp())
E.to_bq()



In [7]:
import math

def normalize(m):
    for row in m.g:
        norm = math.sqrt(sum((x.val**2 for x in row)))
        for i in range(len(row)):
            row[i] = RD(row[i].val / norm)

with quote():

    A = Rel([
                [0.7, 0.7, 0, 0.6],
                [0.9,0.2,0.5,0.3],
                [0.9,0,0,0.6]
            ], 
            ['Mario','Luigi','Bowser'], 
            ['cinema','musica','sport','scommesse'])
    normalize(A)
    
    M = A * A.T
    MAT = A * A.T.simp()
    
print(MAT)
print(M.simp())
M.to_bq()


╒════════╤══════════╤══════════╤══════════╤═══════════╕ ╒═══════════╤══════════╤══════════╤══════════╕
│        │ cinema   │ musica   │ sport    │ scommesse │ │           │ Mario    │ Luigi    │ Bowser   │
├────────┼──────────┼──────────┼──────────┼───────────┤ ├───────────┼──────────┼──────────┼──────────┤
│ Mario  │ 0.60 ... │ 0.60 ... │ 0.0      │ 0.52 ...  │ │ cinema    │ 0.60 ... │ 0.83 ... │ 0.83 ... │
├────────┼──────────┼──────────┼──────────┼───────────┤ ├───────────┼──────────┼──────────┼──────────┤
│ Luigi  │ 0.83 ... │ 0.18 ... │ 0.46 ... │ 0.28 ...  │*│ musica    │ 0.60 ... │ 0.18 ... │ 0.0      │
├────────┼──────────┼──────────┼──────────┼───────────┤ ├───────────┼──────────┼──────────┼──────────┤
│ Bowser │ 0.83 ... │ 0.0      │ 0.0      │ 0.55 ...  │ │ sport     │ 0.0      │ 0.46 ... │ 0.0      │
╘════════╧══════════╧══════════╧══════════╧═══════════╛ ├───────────┼──────────┼──────────┼──────────┤
                                                        │ scommesse │ 0.52 ... │ 0.28 ... │ 0.55 ... │
                                                        ╘═══════════╧══════════╧══════════╧══════════╛
                                                       

╒════════╤══════════╤══════════╤══════════╕
│        │ Mario    │ Luigi    │ Bowser   │
├────────┼──────────┼──────────┼──────────┤
│ Mario  │ 1.00 ... │ 0.75 ... │ 0.79 ... │
├────────┼──────────┼──────────┼──────────┤
│ Luigi  │ 0.75 ... │ 1.0      │ 0.84 ... │
├────────┼──────────┼──────────┼──────────┤
│ Bowser │ 0.79 ... │ 0.84 ... │ 1.00 ... │
╘════════╧══════════╧══════════╧══════════╛
xs.max = None
ys.max = 4.0
xs.max = None
ys.max = 3.0

In [ ]:
math.sqrt(0.87)

In [ ]:
math.sqrt((0.5**2 )*4)

x1^2 + x2^2 + x3^2 = 1


In [12]:
from relmath import *
from relmath_bq import *

State.s.push_env()

with quote():

    A = Rel([
                [0.1, 0.6, 0.3, 0.0, 0.2, 0.6],
                [0.0, 0.8, 0.2, 0.0, 0.2, 0.6],
                [0.0, 0.0, 0.1, 0.9, 0.2, 0.6]
            ], 
            ['Mario','Luigi','Bowser'], 
            ['Movies','Music','Sport','Kidnapping','a','b'])

    M = A * A.T
    MAT = A * A.T.simp()
    
print(MAT)
Ms = M.simp()
print(Ms)

    
M.to_bq()


A ╒════════════╤═══════╤═══════╤════════╕
  │            │ Mario │ Luigi │ Bowser │
  ├────────────┼───────┼───────┼────────┤
  │ Movies     │ 0.1   │ 0.0   │ 0.0    │
  ├────────────┼───────┼───────┼────────┤
  │ Music      │ 0.6   │ 0.8   │ 0.0    │
  ├────────────┼───────┼───────┼────────┤
 *│ Sport      │ 0.3   │ 0.2   │ 0.1    │
  ├────────────┼───────┼───────┼────────┤
  │ Kidnapping │ 0.0   │ 0.0   │ 0.9    │
  ├────────────┼───────┼───────┼────────┤
  │ a          │ 0.2   │ 0.2   │ 0.2    │
  ├────────────┼───────┼───────┼────────┤
  │ b          │ 0.6   │ 0.6   │ 0.6    │
  ╘════════════╧═══════╧═══════╧════════╛
 

Ms
xs.max = None
ys.max = None
xs.max = None
ys.max = None

In [ ]: