Utilities: nctx.core

The examples for the functions all build on the same graph and context_map:

>>> from nctx.core import *
>>> import nctx.directed as nctx_directed
>>> g = nctx_directed.Graph()
>>> context_map = nctx_directed.PropertyMapVecDouble(g, "context")
>>> nctx_directed.read_graphml(g, "source/bcde.ctx.dir.graphml", [context_map])

Context Utilities: Distance functions

Summary
angular_distance Obtain Angular Distance.
cosine_similarity Obtain Cosine Similarity.
euclidean_distance Obtain Euclidean Distance.
js_divergence Obtain Jenson Shannon Divergence.
kl_divergence Obtain Kullback Leibler Divergence.
nctx.core.angular_distance((object)list1, (object)list2)float :

Obtain Angular Distance.

A straightforward implementation of Angular Distance.

The Angular Distance is a metric based on the KL Divergence. See https://en.wikipedia.org/wiki/Cosine_similarity#Angular_distance_and_similarity for more information. Both lists feeded into this function must have the same length and contain only numeric values.

Parameters:
  • list1 (list) – First list
  • list2 (list) – Second list
Returns:

Angular Distance between the two lists

Return type:

double

Example:
>>> list1 = context_map.get_list(0)
>>> list2 = [.1,.2,.3,.4,.5]
>>> angular_distance(list1, list2)
0.18440190063093045
nctx.core.cosine_similarity((object)list1, (object)list2)float :

Obtain Cosine Similarity.

A straightforward implementation of Cosine Similarity.

See https://en.wikipedia.org/wiki/Cosine_similarity for more information. Both lists feeded into this function must have the same length and contain only numeric values.

Parameters:
  • list1 (list) – First list
  • list2 (list) – Second list
Returns:

Cosine Similarity between the two lists

Return type:

double

Example:
>>> list1 = context_map.get_list(0)
>>> list2 = [.1,.2,.3,.4,.5]
>>> cosine_similarity(list1, list2)
0.8368374907292926
nctx.core.euclidean_distance((object)list1, (object)list2)float :

Obtain Euclidean Distance.

A straightforward implementation of Euclidean Distance.

See https://en.wikipedia.org/wiki/Euclidean_distance for more information. Both lists feeded into this function must have the same length and contain only numeric values.

Parameters:
  • list1 (list) – First list
  • list2 (list) – Second list
Returns:

Euclidean Distance between the two lists

Return type:

double

Example:
>>> list1 = context_map.get_list(0)
>>> list2 = [.1,.2,.3,.4,.5]
>>> euclidean_distance(list1, list2)
0.4063153090787592
nctx.core.js_divergence((object)list1, (object)list2)float :

Obtain Jenson Shannon Divergence.

A straightforward implementation of JS Divergence. Note that both lists must be probability distributions, i.e. they sum to one. This is not checked internally and will result in obscure results if not taken care of.

The JS Divergence is based on the KL Divergence. Its square root yields a metric. See https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence for more information. Both lists feeded into this function must have the same length and contain only numeric values.

Parameters:
  • list1 (list) – First list
  • list2 (list) – Second list
Returns:

JS Divergence between the two lists

Return type:

double

Example:
>>> import numpy as np
>>> list1 = np.abs(context_map[0])
>>> list2 = [.1,.2,.3,.4,.5]
>>> list1 = list1/np.sum(list1)
>>> list2 = list2/np.sum(list2)
>>> js_divergence(list1, list2)
0.11794226754027952
nctx.core.kl_divergence((object)list1, (object)list2)float :

Obtain Kullback Leibler Divergence.

A straightforward implementation of KL Divergence. Note that both lists must be probability distributions, i.e. they sum to one. This is not checked internally and will result in obscure results if not taken care of.

The KL Divergence does not fulfil triangle inequality and, hence, is no metric. See https://en.wikipedia.org/wiki/Kullback%E2%80%93Leibler_divergence for more information. Both lists feeded into this function must have the same length and contain only numeric values.

Parameters:
  • list1 (list) – First list
  • list2 (list) – Second list
Returns:

KL Divergence between the two lists

Return type:

double

Example:
>>> import numpy as np
>>> list1 = np.abs(context_map[0])
>>> list2 = [.1,.2,.3,.4,.5]
>>> list1 = list1/np.sum(list1)
>>> list2 = list2/np.sum(list2)
>>> kl_divergence(list1, list2)
0.38236213356053317

Helper Classes

Summary
VecDouble This serves as a container of a typed list to pass lists of float to Python.
VecInt This serves as a container of a typed list to pass lists of int to Python.
VecStr This serves as a container of a typed list to pass lists of str to Python.
VecULong This serves as a container of a typed list to pass lists of unsigned long - numbers > 0 - to Python.
class nctx.core.VecDouble

This serves as a container of a typed list to pass lists of float to Python. It is not meant to be instatiated from Python. If necessary, this container can be converted to a Python list or a numpy array easily.

Example:
>>> context_v0 = context_map[0]
>>> print(type(context_v0).__name__)
VecDouble
>>> context_v0 = list(context_v0)
>>> print(type(context_v0).__name__)
list
>>> import numpy as np
>>> context_v0 = np.asarray(context_v0)
>>> print(type(context_v0).__name__)
ndarray
__contains__((object)arg2)bool

Not commented yet

__delitem__((object)arg2)None

Not commented yet

__getitem__((object)arg1, (object)arg2)object

Not commented yet

__iter__((object)arg1)object

Not commented yet

__len__()int

Not commented yet

__setitem__((object)arg2, (object)arg3)None

Not commented yet

_max()float :

Retrieve the highest possible value of the underlying data type. Important: This number is not neccessarily contained in the list!

_min()float :

Retrieve the lowest possible value of the underlying data type. Important: This number is not neccessarily contained in the list!

append((object)arg2)None

Not commented yet

clear()None :

Empty the property map

extend((object)arg2)None

Not commented yet

class nctx.core.VecInt

This serves as a container of a typed list to pass lists of int to Python. It is not meant to be instatiated from Python. If necessary, this container can be converted to a Python list or a numpy array easily.

Example:
>>> context_v0 = context_map[0]
>>> print(type(context_v0).__name__)
VecDouble
>>> context_v0 = list(context_v0)
>>> print(type(context_v0).__name__)
list
>>> import numpy as np
>>> context_v0 = np.asarray(context_v0)
>>> print(type(context_v0).__name__)
ndarray
__contains__((object)arg2)bool

Not commented yet

__delitem__((object)arg2)None

Not commented yet

__getitem__((object)arg1, (object)arg2)object

Not commented yet

__iter__((object)arg1)object

Not commented yet

__len__()int

Not commented yet

__setitem__((object)arg2, (object)arg3)None

Not commented yet

_max()int :

Retrieve the highest possible value of the underlying data type. Important: This number is not neccessarily contained in the list!

_min()int :

Retrieve the lowest possible value of the underlying data type. Important: This number is not neccessarily contained in the list!

append((object)arg2)None

Not commented yet

clear()None :

Empty the property map

extend((object)arg2)None

Not commented yet

class nctx.core.VecStr

This serves as a container of a typed list to pass lists of str to Python. It is not meant to be instatiated from Python. If necessary, this container can be converted to a Python list or a numpy array easily.

Example:
>>> context_v0 = context_map[0]
>>> print(type(context_v0).__name__)
VecDouble
>>> context_v0 = list(context_v0)
>>> print(type(context_v0).__name__)
list
>>> import numpy as np
>>> context_v0 = np.asarray(context_v0)
>>> print(type(context_v0).__name__)
ndarray
__contains__((object)arg2)bool

Not commented yet

__delitem__((object)arg2)None

Not commented yet

__getitem__((object)arg1, (object)arg2)object

Not commented yet

__iter__((object)arg1)object

Not commented yet

__len__()int

Not commented yet

__setitem__((object)arg2, (object)arg3)None

Not commented yet

append((object)arg2)None

Not commented yet

clear()None :

Empty the property map

extend((object)arg2)None

Not commented yet

class nctx.core.VecULong

This serves as a container of a typed list to pass lists of unsigned long - numbers > 0 - to Python. It is not meant to be instatiated from Python. If necessary, this container can be converted to a Python list or a numpy array easily.

Example:
>>> context_v0 = context_map[0]
>>> print(type(context_v0).__name__)
VecDouble
>>> context_v0 = list(context_v0)
>>> print(type(context_v0).__name__)
list
>>> import numpy as np
>>> context_v0 = np.asarray(context_v0)
>>> print(type(context_v0).__name__)
ndarray
__contains__((object)arg2)bool

Not commented yet

__delitem__((object)arg2)None

Not commented yet

__getitem__((object)arg1, (object)arg2)object

Not commented yet

__iter__((object)arg1)object

Not commented yet

__len__()int

Not commented yet

__setitem__((object)arg2, (object)arg3)None

Not commented yet

_max()int :

Retrieve the highest possible value of the underlying data type. Important: This number is not neccessarily contained in the list!

_min()int :

Retrieve the lowest possible value of the underlying data type. Important: This number is not neccessarily contained in the list!

append((object)arg2)None

Not commented yet

clear()None :

Empty the property map

extend((object)arg2)None

Not commented yet