.. _nctx.core: Utilities: nctx.core ==================== .. toctree:: :maxdepth: 2 :caption: Contents: 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 ------------------------------------- .. csv-table:: Summary :widths: 20, 50 ":py:func:`angular_distance `", "Obtain Angular Distance." ":py:func:`cosine_similarity `", "Obtain Cosine Similarity." ":py:func:`euclidean_distance `", "Obtain Euclidean Distance." ":py:func:`js_divergence `", "Obtain Jenson Shannon Divergence." ":py:func:`kl_divergence `", "Obtain Kullback Leibler Divergence." .. py:function:: 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. :param list1: First list :type list1: list :param list2: Second list :type list2: list :return: Angular Distance between the two lists :rtype: double :Example: >>> list1 = context_map.get_list(0) >>> list2 = [.1,.2,.3,.4,.5] >>> angular_distance(list1, list2) 0.18440190063093045 .. py:function:: 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. :param list1: First list :type list1: list :param list2: Second list :type list2: list :return: Cosine Similarity between the two lists :rtype: double :Example: >>> list1 = context_map.get_list(0) >>> list2 = [.1,.2,.3,.4,.5] >>> cosine_similarity(list1, list2) 0.8368374907292926 .. py:function:: 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. :param list1: First list :type list1: list :param list2: Second list :type list2: list :return: Euclidean Distance between the two lists :rtype: double :Example: >>> list1 = context_map.get_list(0) >>> list2 = [.1,.2,.3,.4,.5] >>> euclidean_distance(list1, list2) 0.4063153090787592 .. py:function:: 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. :param list1: First list :type list1: list :param list2: Second list :type list2: list :return: JS Divergence between the two lists :rtype: 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 .. py:function:: 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. :param list1: First list :type list1: list :param list2: Second list :type list2: list :return: KL Divergence between the two lists :rtype: 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 -------------- .. csv-table:: Summary :widths: 20, 50 ":py:class:`VecDouble `", "This serves as a container of a typed list to pass lists of float to Python." ":py:class:`VecInt `", "This serves as a container of a typed list to pass lists of int to Python." ":py:class:`VecStr `", "This serves as a container of a typed list to pass lists of str to Python." ":py:class:`VecULong `", "This serves as a container of a typed list to pass lists of unsigned long - numbers > 0 - to Python." .. py: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 .. py:method:: nctx.core.VecDouble.__contains__( (object)arg2) -> bool Not commented yet .. py:method:: nctx.core.VecDouble.__delitem__( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecDouble.__getitem__( (object)arg1, (object)arg2) -> object Not commented yet .. py:method:: nctx.core.VecDouble.__iter__( (object)arg1) -> object Not commented yet .. py:method:: nctx.core.VecDouble.__len__( ) -> int Not commented yet .. py:method:: nctx.core.VecDouble.__setitem__( (object)arg2, (object)arg3) -> None Not commented yet .. py:method:: nctx.core.VecDouble._max() -> float : Retrieve the highest possible value of the underlying data type. Important: This number is not neccessarily contained in the list! .. py:method:: nctx.core.VecDouble._min() -> float : Retrieve the lowest possible value of the underlying data type. Important: This number is not neccessarily contained in the list! .. py:method:: nctx.core.VecDouble.append( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecDouble.clear( ) -> None : Empty the property map .. py:method:: nctx.core.VecDouble.extend( (object)arg2) -> None Not commented yet .. py: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 .. py:method:: nctx.core.VecInt.__contains__( (object)arg2) -> bool Not commented yet .. py:method:: nctx.core.VecInt.__delitem__( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecInt.__getitem__( (object)arg1, (object)arg2) -> object Not commented yet .. py:method:: nctx.core.VecInt.__iter__( (object)arg1) -> object Not commented yet .. py:method:: nctx.core.VecInt.__len__( ) -> int Not commented yet .. py:method:: nctx.core.VecInt.__setitem__( (object)arg2, (object)arg3) -> None Not commented yet .. py:method:: nctx.core.VecInt._max() -> int : Retrieve the highest possible value of the underlying data type. Important: This number is not neccessarily contained in the list! .. py:method:: nctx.core.VecInt._min() -> int : Retrieve the lowest possible value of the underlying data type. Important: This number is not neccessarily contained in the list! .. py:method:: nctx.core.VecInt.append( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecInt.clear( ) -> None : Empty the property map .. py:method:: nctx.core.VecInt.extend( (object)arg2) -> None Not commented yet .. py: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 .. py:method:: nctx.core.VecStr.__contains__( (object)arg2) -> bool Not commented yet .. py:method:: nctx.core.VecStr.__delitem__( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecStr.__getitem__( (object)arg1, (object)arg2) -> object Not commented yet .. py:method:: nctx.core.VecStr.__iter__( (object)arg1) -> object Not commented yet .. py:method:: nctx.core.VecStr.__len__( ) -> int Not commented yet .. py:method:: nctx.core.VecStr.__setitem__( (object)arg2, (object)arg3) -> None Not commented yet .. py:method:: nctx.core.VecStr.append( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecStr.clear( ) -> None : Empty the property map .. py:method:: nctx.core.VecStr.extend( (object)arg2) -> None Not commented yet .. py: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 .. py:method:: nctx.core.VecULong.__contains__( (object)arg2) -> bool Not commented yet .. py:method:: nctx.core.VecULong.__delitem__( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecULong.__getitem__( (object)arg1, (object)arg2) -> object Not commented yet .. py:method:: nctx.core.VecULong.__iter__( (object)arg1) -> object Not commented yet .. py:method:: nctx.core.VecULong.__len__( ) -> int Not commented yet .. py:method:: nctx.core.VecULong.__setitem__( (object)arg2, (object)arg3) -> None Not commented yet .. py:method:: nctx.core.VecULong._max() -> int : Retrieve the highest possible value of the underlying data type. Important: This number is not neccessarily contained in the list! .. py:method:: nctx.core.VecULong._min() -> int : Retrieve the lowest possible value of the underlying data type. Important: This number is not neccessarily contained in the list! .. py:method:: nctx.core.VecULong.append( (object)arg2) -> None Not commented yet .. py:method:: nctx.core.VecULong.clear( ) -> None : Empty the property map .. py:method:: nctx.core.VecULong.extend( (object)arg2) -> None Not commented yet