### Install PyInform from Source using Pip Source: https://elife-asu.github.io/PyInform/_sources/starting.rst.txt Install PyInform by cloning the repository and using pip to install from the local source. This method is recommended for installing from source. ```bash pip install . ``` -------------------------------- ### Install PyInform from Source for User Source: https://elife-asu.github.io/PyInform/_sources/starting.rst.txt Install PyInform from the local source for the current user only, without requiring administrator privileges. Use this if you lack sudo access or prefer a user-specific installation. ```bash pip install --user . ``` -------------------------------- ### Install PyInform from requirements.txt Source: https://elife-asu.github.io/PyInform/_sources/starting.rst.txt Install PyInform and its dependencies using the requirements file from the PyInform git repository. This is useful if you have cloned the repository. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install PyInform from Source in Editable Mode Source: https://elife-asu.github.io/PyInform/_sources/starting.rst.txt Install PyInform from the local source in editable mode. Changes made to the source code in the repository will be immediately reflected without needing reinstallation. This is ideal for development. ```bash pip install --editable . ``` -------------------------------- ### Install PyInform using Pip Source: https://elife-asu.github.io/PyInform/_sources/starting.rst.txt Use this command to install PyInform directly from the Python Package Index. Ensure you have pip installed and updated. ```bash pip install pyinform ``` -------------------------------- ### Compute Relative Entropy Examples Source: https://elife-asu.github.io/PyInform/_modules/pyinform/relativeentropy.html Examples demonstrating the calculation of relative entropy between two time series using different input sequences. ```python >>> xs = [0,1,0,0,0,0,0,0,0,1] >>> ys = [0,1,1,1,1,0,0,1,0,0] >>> relative_entropy(xs, ys) 0.27807190511263774 >>> relative_entropy(ys, xs) 0.3219280948873624 ``` ```python >>> xs = [0,0,0,0] >>> ys = [0,1,1,0] >>> relative_entropy(xs, ys) 1.0 >>> relative_entropy(ys, xs) nan ``` -------------------------------- ### Get Distribution Size Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Returns the size of the support of the distribution. ```APIDOC ## Method: __len__() ### Description Determine the size of the support of the distribution. ### Response - **size** (int) - The size of the support. ``` -------------------------------- ### Coalesce a time series Source: https://elife-asu.github.io/PyInform/_modules/pyinform/utils/coalesce.html Examples demonstrating how to reduce the base of a time series or normalize states to non-negative integers. ```python >>> utils.coalesce_series([2,9,2,9,9]) (array([0, 1, 0, 1, 1], dtype=int32), 2) ``` ```python >>> utils.coalesce_series([0,2,0,2,0,2]) (array([0, 1, 0, 1, 0, 1], dtype=int32), 2) ``` ```python >>> utils.coalesce_series([-8,2,6,-2,4]) (array([0, 2, 4, 1, 3], dtype=int32), 5) ``` -------------------------------- ### GET /dist/dump Source: https://elife-asu.github.io/PyInform/dist.html Computes and returns the empirical probability of all observable events as an array. ```APIDOC ## GET /dist/dump ### Description Computes the empirical probability of each observable event and returns the result as an array. ### Response #### Success Response (200) - **probabilities** (numpy.ndarray) - An array containing the empirical probabilities of all events. ### Errors - **ValueError**: Raised if the distribution is not valid. - **RuntimeError**: Raised if the dump fails in the C call. - **IndexError**: Raised if the event index is out of bounds. ``` -------------------------------- ### Get Distribution Size Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Returns the size (number of elements) of the distribution. Requires a pointer to the distribution. ```python _dist_size = _inform.inform_dist_size _dist_size.argtypes = [c_void_p] _dist_size.restype = c_ulong ``` -------------------------------- ### Get Total Observations in Dist Source: https://elife-asu.github.io/PyInform/dist.html Return the total number of observations made thus far. See also __len__(). ```python >>> d = Dist(5) >>> d.counts() 0 ``` ```python >>> d = Dist([1,0,3,2]) >>> d.counts() 6 ``` -------------------------------- ### Get Distribution Support Size Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Returns the number of possible observations (the size of the support) for the distribution. This is equivalent to the length of the underlying histogram. ```python len(Dist(5)) ``` ```python len(Dist([0,1,5])) ``` -------------------------------- ### Initialize Dist with Size Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Constructs a distribution with a zeroed support of the specified size. Use when the number of possible observations is known beforehand. ```python d = Dist(5) ``` -------------------------------- ### Initialize Dist Object Source: https://elife-asu.github.io/PyInform/dist.html Construct a distribution. Supports initialization with a zeroed support of a given size or from a list/numpy array representing the underlying support. ```python >>> d = Dist(5) >>> d = Dist([0,0,1,2]) ``` -------------------------------- ### Initialize Dist with Sequence Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Constructs a distribution using a list or numpy array as the underlying support. The values in the sequence are treated as initial counts. ```python d = Dist([0,0,1,2]) ``` -------------------------------- ### Initialize and Validate Distribution Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Demonstrates initializing a Distribution object and checking its validity. A distribution is valid if it's not empty and at least one observation has been made. ```python >>> d = Dist(5) >>> d.valid() False ``` ```python >>> d = Dist([0,0,0,1]) >>> d.valid() True ``` -------------------------------- ### GET /dist/event Source: https://elife-asu.github.io/PyInform/dist.html Retrieves the empirical probability of a specific event within the distribution. ```APIDOC ## GET /dist/event ### Description Returns the empirical probability of a specific event. ### Parameters #### Path Parameters - **event** (int) - Required - The index of the event to retrieve. ### Response #### Success Response (200) - **probability** (float) - The empirical probability of the event. ### Errors - **ValueError**: Raised if the distribution is not valid. - **IndexError**: Raised if the event index is out of bounds. ``` -------------------------------- ### Get Distribution Counts Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Returns the counts of the distribution. Requires a pointer to the distribution. ```python _dist_counts = _inform.inform_dist_counts _dist_counts.argtypes = [c_void_p] _dist_counts.restype = c_uint ``` -------------------------------- ### Get Observation Counts Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Returns the total number of observations made in the distribution. ```APIDOC ## Method: counts() ### Description Return the number of observations made thus far. ### Response - **count** (int) - The number of observations. ``` -------------------------------- ### Dist Class Initialization Source: https://elife-asu.github.io/PyInform/dist.html Constructs a distribution object. The support can be initialized with a specified size or from a given sequence. ```APIDOC ## Dist Class ### Description Dist is a class designed to represent empirical probability distributions, i.e., histograms, for cleanly logging observations of time series data. The premise behind this class is that it allows PyInform to define the standard entropy measures on distributions. This reduces functions such as `pyinform.activeinfo.active_info()` to building distributions and then applying standard entropy measures. ### `__init__` Construct a distribution. If the parameter `n` is an integer, the distribution is constructed with a zeroed support of size `n`. If `n` is a list or `numpy.ndarray`, the sequence is treated as the underlying support. ### Parameters * **n** (int, list or `numpy.ndarray`) – the support for the distribution ### Raises * **ValueError** – if support is empty or multidimensional * **MemoryError** – if memory allocation fails within the C call ### Examples ```python >>> d = Dist(5) >>> d = Dist([0,0,1,2]) ``` ``` -------------------------------- ### Distribution Creation and Copying Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Functions for creating a distribution from counts and copying existing distributions. ```APIDOC ## `inform_dist_create` ### Description Creates a distribution object from a list of counts. ### Method `_dist_create(POINTER(c_uint) counts, c_ulong n)` ### Parameters - **counts** (POINTER(c_uint)) - A pointer to an array of counts. - **n** (c_ulong) - The number of counts. ### Return Value - `c_void_p` - A pointer to the newly created distribution object. ## `inform_dist_copy` ### Description Copies an existing distribution object. ### Method `_dist_copy(c_void_p src, c_void_p dst)` ### Parameters - **src** (c_void_p) - A pointer to the source distribution object. - **dst** (c_void_p) - A pointer to the destination distribution object. ### Return Value - `c_void_p` - A pointer to the copied distribution object. ``` -------------------------------- ### Distribution Modification and Operations Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Functions to set element counts, tick a distribution, and get probabilities. ```APIDOC ## `inform_dist_set` ### Description Sets the count of a specific element in the distribution. ### Method `_dist_set(c_void_p dist, c_ulong index, c_uint count)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. - **index** (c_ulong) - The index of the element to set. - **count** (c_uint) - The new count for the element. ### Return Value - `c_uint` - The previous count of the element. ## `inform_dist_tick` ### Description Increments the count of a specific element in the distribution by one. ### Method `_dist_tick(c_void_p dist, c_ulong index)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. - **index** (c_ulong) - The index of the element to tick. ### Return Value - `c_uint` - The new count of the element. ## `inform_dist_prob` ### Description Calculates the probability of a specific element in the distribution. ### Method `_dist_prob(c_void_p dist, c_ulong index)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. - **index** (c_ulong) - The index of the element for which to calculate the probability. ### Return Value - `c_double` - The probability of the specified element. ``` -------------------------------- ### Observe events and access counts Source: https://elife-asu.github.io/PyInform/_sources/dist.rst.txt Demonstrates making incremental observations and then accessing specific event counts. Useful for verifying counts after a series of observations. ```python obs = [1, 1, 3, 5, 1, 3, 7, 9] d = Dist(max(obs) + 1) for event in obs: assert(d[event] == d.tick(event) - 1) list(d) d[3] d[7] ``` -------------------------------- ### Distribution Information Retrieval Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Functions to get the size, counts, validity, and individual elements of a distribution. ```APIDOC ## `inform_dist_size` ### Description Gets the size of the distribution. ### Method `_dist_size(c_void_p dist)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. ### Return Value - `c_ulong` - The size of the distribution. ## `inform_dist_counts` ### Description Gets the counts of the distribution. ### Method `_dist_counts(c_void_p dist)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. ### Return Value - `c_uint` - The counts of the distribution. ## `inform_dist_is_valid` ### Description Checks if the distribution is valid. ### Method `_dist_is_valid(c_void_p dist)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. ### Return Value - `c_bool` - True if the distribution is valid, False otherwise. ## `inform_dist_get` ### Description Gets the count of a specific element in the distribution. ### Method `_dist_get(c_void_p dist, c_ulong index)` ### Parameters - **dist** (c_void_p) - A pointer to the distribution object. - **index** (c_ulong) - The index of the element to retrieve. ### Return Value - `c_uint` - The count of the element at the specified index. ``` -------------------------------- ### Construct a distribution from observation counts Source: https://elife-asu.github.io/PyInform/dist.html Initialize a distribution using a list or NumPy array of observation counts. This creates a valid distribution. ```python d = Dist([0,0,1,2,1,0,0]) d.valid() True d.counts() 4 len(d) 7 ``` -------------------------------- ### Dist Class Methods Source: https://elife-asu.github.io/PyInform/genindex.html Documentation for the methods available in the `pyinform.dist.Dist` class. ```APIDOC ## `pyinform.dist.Dist` Class Methods ### `__getitem__()` #### Description Gets an item from the distribution. ### `__init__()` #### Description Initializes a new distribution. ### `__len__()` #### Description Returns the number of items in the distribution. ### `__setitem__()` #### Description Sets an item in the distribution. ### `copy()` #### Description Creates a copy of the distribution. ### `counts()` #### Description Returns the counts of items in the distribution. ### `dump()` #### Description Dumps the distribution to a file or stream. ### `probability()` #### Description Calculates the probability of an item in the distribution. ### `resize()` #### Description Resizes the distribution. ### `tick()` #### Description Increments the count for an item in the distribution. ### `valid()` #### Description Checks if the distribution is valid. ``` -------------------------------- ### Get Distribution Element Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Retrieves the count of a specific element in the distribution. Requires a pointer to the distribution and the index of the element. ```python _dist_get = _inform.inform_dist_get _dist_get.argtypes = [c_void_p, c_ulong] _dist_get.restype = c_uint ``` -------------------------------- ### Compute Active Information for Multiple Initial Conditions Source: https://elife-asu.github.io/PyInform/timeseries.html Calculate active information across multiple time series sequences, demonstrating both direct computation and manual averaging. ```python >>> active_info([[0,0,1,1,1,1,0,0,0], [1,0,0,1,0,0,1,0,0]], k=2) 0.35987902873686084 >>> active_info([[0,0,1,1,1,1,0,0,0], [1,0,0,1,0,0,1,0,0]], k=2, local=True) array([[ 0.80735492, -0.36257008, 0.63742992, 0.63742992, -0.77760758, 0.80735492, -1.19264508], [ 0.80735492, 0.80735492, 0.22239242, 0.80735492, 0.80735492, 0.22239242, 0.80735492]]) ``` ```python >>> import numpy as np >>> series = np.asarray([[0,0,1,1,1,1,0,0,0], [1,0,0,1,0,0,1,0,0]]) >>> np.apply_along_axis(active_info, 1, series, 2).mean() 0.5845395307173363 ``` ```python >>> ai = np.empty(len(series)) >>> for i, xs in enumerate(series): ... ai[i] = active_info(xs, k=2) ... >>> ai array([0.30595849, 0.86312057]) >>> ai.mean() 0.5845395307173363 ``` -------------------------------- ### Shannon Entropy Calculation Source: https://elife-asu.github.io/PyInform/_sources/dist.rst.txt Demonstrates how to calculate Shannon entropy using PyInform, both manually and with the provided entropy function. ```APIDOC ## Shannon Entropy Calculation ### Description This section illustrates how to compute the Shannon entropy of a distribution. It shows a manual calculation and the usage of the `pyinform.shannon.entropy` function. ### Methods #### Manual Calculation Iterate through the probabilities of the distribution and apply the Shannon entropy formula: H = -sum(p * log2(p)). #### `pyinform.shannon.entropy(dist)` - **Description**: Computes the Shannon entropy of a given `Dist` object. - **Parameters**: - `dist` (`Dist`): The distribution object. - **Returns**: (float) The calculated Shannon entropy. ### Request Example (Manual Calculation) ```python from math import log2 from pyinform.dist import Dist obs = [1, 0, 1, 2, 2, 1, 2, 3, 2, 2] d = Dist(max(obs) + 1) for event in obs: d.tick(event) h = 0. for p in d.dump(): h -= p * log2(p) print(h) # Output: 1.6854752972273344 ``` ### Request Example (Using `entropy` function) ```python from pyinform.dist import Dist from pyinform.shannon import entropy obs = [1, 0, 1, 2, 2, 1, 2, 3, 2, 2] d = Dist(max(obs) + 1) for event in obs: d.tick(event) print(entropy(d)) # Output: 1.6854752972273344 ``` ### Response Example (Success) ```json { "entropy_value": 1.6854752972273344 } ``` ``` -------------------------------- ### Get Distribution Probability Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Calculates and returns the probability of a specific element in the distribution. Requires a pointer to the distribution and the index of the element. ```python _dist_prob = _inform.inform_dist_prob _dist_prob.argtypes = [c_void_p, c_ulong] _dist_prob.restype = c_double ``` -------------------------------- ### Dist Class Methods Source: https://elife-asu.github.io/PyInform/dist.html Provides documentation for various methods of the Dist class, including size, element access, modification, resizing, copying, and validation. ```APIDOC ## Dist Class Methods ### `__len__` Determine the size of the support of the distribution. ### Returns * the size of the support (int) ### Examples ```python >>> len(Dist(5)) 5 >>> len(Dist([0,1,5])) 3 ``` ### `__getitem__` Get the number of observations made of `event`. ### Parameters * **event** (int) – the observed event ### Returns * the number of observations of `event` (int) ### Raises * **IndexError** – if `event < 0 or len(self) <= event` ### Examples ```python >>> d = Dist(2) >>> (d[0], d[1]) (0, 0) ``` ```python >>> d = Dist([0,1]) >>> (d[0], d[1]) (0, 1) ``` ### `__setitem__` Set the number of observations of `event` to `value`. If `value` is negative, then the observation count is set to zero. ### Parameters * **event** (int) – the observed event * **value** (int) – the number of observations ### Raises * **IndexError** – if `event < 0 or len(self) <= event` ### Examples ```python >>> d = Dist(2) >>> for i, _ in enumerate(d): ... d[i] = i*i ... >>> list(d) [0, 1] ``` ```python >>> d = Dist([0,1,2,3]) >>> for i, n in enumerate(d): ... d[i] = 2 * n ... >>> list(d) [0, 2, 4, 6] ``` ### `resize` Resize the support of the distribution in place. If the distribution… * **shrinks** - the last `len(self) - n` elements are lost, the rest are preserved * **grows** - the last `n - len(self)` elements are zeroed * **is unchanged** - well, that sorta says it all, doesn’t it? ### Parameters * **n** (int) – the desired size of the support ### Raises * **ValueError** – if the requested size is zero * **MemoryError** – if memory allocation fails in the C call ### Examples ```python >>> d = Dist(5) >>> d.resize(3) >>> len(d) 3 >>> d.resize(8) >>> len(d) 8 ``` ```python >>> d = Dist([1,2,3,4]) >>> d.resize(2) >>> list(d) [1, 2] >>> d.resize(4) >>> list(d) [1, 2, 0, 0] ``` ### `copy` Perform a deep copy of the distribution. ### Returns * the copied distribution (`pyinform.dist.Dist`) ### Examples ```python >>> d = Dist([1,2,3]) >>> e = d >>> e[0] = 3 >>> list(e) [3, 2, 3] >>> list(d) [3, 2, 3] ``` ```python >>> f = d.copy() >>> f[0] = 1 >>> list(f) [1, 2, 3] >>> list(d) [3, 2, 3] ``` ### `counts` Return the number of observations made thus far. ### Returns * the number of observations (int) ### Examples ```python >>> d = Dist(5) >>> d.counts() 0 ``` ```python >>> d = Dist([1,0,3,2]) >>> d.counts() 6 ``` ### `valid` Determine if the distribution is a valid probability distribution, i.e. if the support is not empty and at least one observation has been made. ### Returns * a boolean signifying that the distribution is valid (bool) ### Examples ```python >>> d = Dist(5) >>> d.valid() False ``` ```python >>> d = Dist([0,0,0,1]) >>> d.valid() True ``` ### `tick` Make a single observation of `event`, and return the total number of observations of said `event`. ### Parameters * **event** (int) – the observed event ### Returns * the total number of observations of `event` (int) ### Raises * **IndexError** – if `event < 0 or len(self) <= event` ### Examples ```python >>> d = Dist(5) >>> for i, _ in enumerate(d): ... assert(d.tick(i) == 1) ... >>> list(d) [1, 1, 1, 1, 1] ``` ```python >>> d = Dist([0,1,2,3]) >>> for i, _ in enumerate(d): ... assert(d.tick(i) == i + 1) ... >>> list(d) [1, 2, 3, 4] ``` ### `probability` Compute the empirical probability of an `event`. ### Parameters * **event** (int) – the observed event ### Returns * the empirical probability of the `event` (float) ### Examples ```python >>> d = Dist([1,1,1,1]) >>> for i, _ in enumerate(d): ... assert(d.probability(i) == 1./4) ... ``` ``` -------------------------------- ### Manual Averaging of Active Information Source: https://elife-asu.github.io/PyInform/_modules/pyinform/activeinfo.html Demonstrates how to manually compute and average active information across multiple initial conditions using `numpy.apply_along_axis` or a loop. This method can be used to verify results or for custom processing. ```python >>> import numpy as np >>> series = np.asarray([[0,0,1,1,1,1,0,0,0], [1,0,0,1,0,0,1,0,0]]) >>> np.apply_along_axis(active_info, 1, series, 2).mean() 0.5845395307173363 ``` ```python >>> ai = np.empty(len(series)) >>> for i, xs in enumerate(series): ... ai[i] = active_info(xs, k=2) ... >>> ai array([0.30595849, 0.86312057]) >>> ai.mean() 0.5845395307173363 ``` -------------------------------- ### Get Total Observations Count Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Returns the total number of observations recorded in the distribution. This is the sum of all counts in the distribution's support. ```python d = Dist(5) d.counts() ``` ```python d = Dist([1,0,3,2]) d.counts() ``` -------------------------------- ### PyInform Modules and Classes Source: https://elife-asu.github.io/PyInform/genindex.html Overview of the modules and classes available in the PyInform library. ```APIDOC ## Modules and Classes ### Modules - `pyinform.activeinfo` - `pyinform.blockentropy` - `pyinform.conditionalentropy` - `pyinform.dist` - `pyinform.entropyrate` - `pyinform.mutualinfo` - `pyinform.relativeentropy` - `pyinform.shannon` - `pyinform.transferentropy` - `pyinform.utils` - `pyinform.utils.binning` - `pyinform.utils.coalesce` - `pyinform.utils.encoding` ### Classes - `pyinform.dist.Dist` ``` -------------------------------- ### Calculate Transfer Entropy with Two Initial Conditions and Two Background Processes Source: https://elife-asu.github.io/PyInform/_modules/pyinform/transferentropy.html Compute transfer entropy for multiple initial conditions conditioned on multiple background processes. ```python >>> xs = [[1,1,0,1,0,1,1,0,0],[0,1,0,1,1,1,0,0,1]] >>> ys = [[1,1,1,0,1,1,1,0,0],[0,0,1,0,1,1,1,0,0]] >>> ws = [[[1,1,0,1,1,0,1,0,1],[1,1,1,0,1,1,1,1,0]], ... [[1,1,1,1,0,0,0,0,1],[0,0,0,1,1,1,1,0,1]]] >>> transfer_entropy(xs, ys, k=2) 0.5364125003090668 >>> transfer_entropy(xs, ys, k=2, condition=ws) 0.3396348215831049 >>> transfer_entropy(xs, ys, k=2, condition=ws, local=True) ``` -------------------------------- ### Dist Class Initialization Source: https://elife-asu.github.io/PyInform/_sources/dist.rst.txt Constructs an empirical distribution. It can be initialized with a specified number of unique observables or with a list of observation counts. ```APIDOC ## Dist Class Initialization ### Description Constructs an empirical distribution (histogram) representing observed frequencies of events. Can be initialized with a specified number of unique observables (resulting in an invalid distribution initially) or with a list/NumPy array of observation counts. ### Method `Dist(n)` or `Dist(counts)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **n** (int) - The number of unique observables. - **counts** (list or numpy.ndarray) - A list or array of observation counts. ### Request Example ```json { "n": 5 } ``` ```json { "counts": [0, 0, 1, 2, 1, 0, 0] } ``` ### Response #### Success Response (200) An initialized `Dist` object. #### Response Example ```json { "message": "Dist object created successfully" } ``` ``` -------------------------------- ### Track events and access counts Source: https://elife-asu.github.io/PyInform/dist.html Demonstrates how the Dist class tracks events and allows direct access to the count of a specific event. ```python obs = [1, 1, 3, 5, 1, 3, 7, 9] d = Dist(max(obs) + 1) for event in obs: assert(d[event] == d.tick(event) - 1) list(d) [0, 3, 0, 2, 0, 1, 0, 1, 0, 1] d[3] 2 d[7] 1 ``` -------------------------------- ### Make observations using indexing Source: https://elife-asu.github.io/PyInform/dist.html Assign values to distribution elements using standard indexing, similar to a list. This method updates the distribution's counts. ```python d = Dist(5) for i in range(len(d)): d[i] = i*i list(d) [0, 1, 4, 9, 16] ``` -------------------------------- ### Resize Dist Support Source: https://elife-asu.github.io/PyInform/dist.html Resize the support of the distribution in place. Shrinking loses elements, growing zeroes new elements, and unchanged size preserves existing elements. ```python >>> d = Dist(5) >>> d.resize(3) >>> len(d) 3 >>> d.resize(8) >>> len(d) 8 ``` ```python >>> d = Dist([1,2,3,4]) >>> d.resize(2) >>> list(d) [1, 2] >>> d.resize(4) >>> list(d) [1, 2, 0, 0] ``` -------------------------------- ### Calculate Transfer Entropy with Two Initial Conditions Source: https://elife-asu.github.io/PyInform/_modules/pyinform/transferentropy.html Compute transfer entropy using multiple initial conditions without background processes. ```python >>> xs = [[1,0,0,0,0,1,1,1,1], [1,1,1,1,0,0,0,1,1]] >>> ys = [[0,0,1,1,1,1,0,0,0], [1,0,0,0,0,1,1,1,0]] >>> transfer_entropy(xs, ys, k=2) 0.693536138896192 >>> transfer_entropy(xs, ys, k=2, local=True) array([[1.32192809, 0. , 0.73696559, 0.73696559, 1.32192809, 0. , 0.73696559], [0. , 0.73696559, 0.73696559, 1.32192809, 0. , 0.73696559, 1.32192809]]) ``` -------------------------------- ### Create Distribution Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Creates a new distribution from an array of counts. Requires a pointer to the counts and the number of counts. ```python _dist_create = _inform.inform_dist_create _dist_create.argtypes = [POINTER(c_uint), c_ulong] _dist_create.restype = c_void_p ``` -------------------------------- ### Copy Distribution Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Copies an existing distribution to a new memory location. Requires pointers to the source and destination. ```python _dist_copy = _inform.inform_dist_copy _dist_copy.argtypes = [c_void_p, c_void_p] _dist_copy.restype = c_void_p ``` -------------------------------- ### Configure Ctypes Interfaces Source: https://elife-asu.github.io/PyInform/_modules/pyinform/shannon.html Internal configuration of C function signatures for Shannon information measures. ```python _entropy = _inform.inform_shannon_entropy _entropy.argtypes = [c_void_p, c_double] _entropy.restype = c_double _mutual_info = _inform.inform_shannon_mi _mutual_info.argtypes = [c_void_p, c_void_p, c_void_p, c_double] _mutual_info.restype = c_double _conditional_entropy = _inform.inform_shannon_ce _conditional_entropy.argtypes = [c_void_p, c_void_p, c_double] _conditional_entropy.restype = c_double _conditional_mutual_info = _inform.inform_shannon_cmi _conditional_mutual_info.argtypes = [c_void_p, c_void_p, c_void_p, c_void_p, c_double] _conditional_mutual_info.restype = c_double _relative_entropy = _inform.inform_shannon_re _relative_entropy.argtypes = [c_void_p, c_void_p, c_double] _relative_entropy.restype = c_double ``` -------------------------------- ### Dist Class Constructor Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Constructs a new empirical probability distribution with a specified support size or initial support sequence. ```APIDOC ## Constructor: Dist(n) ### Description Constructs a distribution. If n is an integer, the distribution is initialized with a zeroed support of size n. If n is a list or numpy.ndarray, the sequence is treated as the underlying support. ### Parameters #### Request Body - **n** (int, list, or numpy.ndarray) - Required - The support for the distribution. ### Errors - **ValueError**: Raised if support is empty or multidimensional. - **MemoryError**: Raised if memory allocation fails within the C call. ``` -------------------------------- ### Module Functions Source: https://elife-asu.github.io/PyInform/genindex.html Documentation for various functions within the PyInform modules. ```APIDOC ## Module Functions ### `pyinform.activeinfo.active_info()` #### Description Calculates active information. ### `pyinform.utils.binning.bin_series()` #### Description Bins a time series. ### `pyinform.blockentropy.block_entropy()` #### Description Calculates block entropy. ### `pyinform.utils.coalesce.coalesce_series()` #### Description Coalesces a time series. ### `pyinform.conditionalentropy.conditional_entropy()` #### Description Calculates conditional entropy. ### `pyinform.shannon.conditional_mutual_info()` #### Description Calculates conditional mutual information. ### `pyinform.utils.encoding.decode()` #### Description Decodes a sequence. ### `pyinform.utils.encoding.encode()` #### Description Encodes a sequence. ### `pyinform.shannon.entropy()` #### Description Calculates Shannon entropy. ### `pyinform.entropyrate.entropy_rate()` #### Description Calculates the entropy rate. ### `pyinform.mutualinfo.mutual_info()` #### Description Calculates mutual information. ### `pyinform.shannon.mutual_info()` #### Description Calculates mutual information (alternative implementation). ### `pyinform.relativeentropy.relative_entropy()` #### Description Calculates relative entropy. ### `pyinform.shannon.relative_entropy()` #### Description Calculates relative entropy (alternative implementation). ### `pyinform.utils.binning.series_range()` #### Description Determines the range of a time series. ### `pyinform.transferentropy.transfer_entropy()` #### Description Calculates transfer entropy. ``` -------------------------------- ### Dist Class Methods Source: https://elife-asu.github.io/PyInform/_sources/dist.rst.txt Provides documentation for various methods of the Dist class, including accessing counts, checking validity, making observations, and calculating probabilities. ```APIDOC ## Dist Class Methods ### Description This section details the methods available for the `pyinform.dist.Dist` class, including initialization, observation handling, and probability calculations. ### Methods #### `__init__(self, n_or_counts)` - **Description**: Initializes a `Dist` object. Can take an integer `n` for the number of observables or a list/NumPy array `counts`. - **Parameters**: - `n_or_counts` (int or list/numpy.ndarray): Number of observables or initial counts. #### `__len__(self)` - **Description**: Returns the number of unique observables in the distribution. - **Returns**: (int) The number of observables. #### `__getitem__(self, key)` - **Description**: Retrieves the count for a specific event. - **Parameters**: - `key` (int): The event to retrieve the count for. - **Returns**: (int) The count of the event. #### `__setitem__(self, key, value)` - **Description**: Sets the count for a specific event. - **Parameters**: - `key` (int): The event to set the count for. - `value` (int): The new count for the event. #### `resize(self, n)` - **Description**: Resizes the distribution to accommodate `n` unique observables. - **Parameters**: - `n` (int): The new number of unique observables. #### `copy(self)` - **Description**: Creates a deep copy of the distribution. - **Returns**: (`Dist`) A copy of the distribution. #### `counts(self)` - **Description**: Returns the total number of observations made. - **Returns**: (int) The total count of observations. #### `valid(self)` - **Description**: Checks if the distribution is valid (i.e., has had at least one observation). - **Returns**: (bool) `True` if valid, `False` otherwise. #### `tick(self, event)` - **Description**: Increments the count for a given event and returns the new count. - **Parameters**: - `event` (int): The event to tick. - **Returns**: (int) The new count of the event. #### `probability(self, event)` - **Description**: Calculates the probability of a given event. - **Parameters**: - `event` (int): The event to calculate the probability for. - **Returns**: (float) The probability of the event. #### `dump(self)` - **Description**: Returns a NumPy array containing the probabilities of all events. - **Returns**: (numpy.ndarray) An array of probabilities. ### Request Example (Making Observations) ```python from pyinform.dist import Dist d = Dist(5) for i in range(len(d)): d[i] = i*i print(list(d)) # Output: [0, 1, 4, 9, 16] obs = [1, 0, 1, 2, 2, 1, 2, 3, 2, 2] d = Dist(max(obs) + 1) for event in obs: d.tick(event) print(list(d)) # Output: [1, 3, 5, 1] ``` ### Request Example (Probabilities) ```python from pyinform.dist import Dist d = Dist([3, 0, 1, 2]) print(d.probability(0)) # Output: 0.5 print(d.dump()) # Output: array([0.5, 0. , 0.16666667, 0.33333333]) ``` ### Response Example (Success) ```json { "message": "Method executed successfully" } ``` ``` -------------------------------- ### Bin time series using fixed size bins Source: https://elife-asu.github.io/PyInform/utils.html Bins a continuous time series using a specific bin size (step). ```python >>> utils.bin_series(xs, step=4.0) (array([2, 0, 1, 1, 2, 0, 1, 2, 2, 0, 1, 0, 0, 2, 0, 2, 0, 1, 0, 2], dtype=int32), 3, 4.0) >>> utils.bin_series(xs, step=2.0) (array([4, 1, 2, 3, 4, 1, 3, 4, 4, 1, 2, 1, 0, 4, 0, 4, 0, 2, 0, 4], dtype=int32), 5, 2.0) ``` -------------------------------- ### Resize Distribution Support Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Resizes the support of the distribution in place. If shrinking, trailing elements are lost. If growing, new elements are zeroed. Raises ValueError if the requested size is zero. ```python d = Dist(5) d.resize(3) len(d) ``` ```python d.resize(8) len(d) ``` ```python d = Dist([1,2,3,4]) d.resize(2) list(d) ``` ```python d.resize(4) list(d) ``` -------------------------------- ### Bin time series using thresholds Source: https://elife-asu.github.io/PyInform/utils.html Bins a continuous time series based on provided boundary thresholds. ```python >>> utils.bin_series(xs, bounds=[2.0, 7.5]) (array([2, 1, 1, 1, 2, 1, 1, 2, 2, 1, 1, 1, 0, 2, 0, 2, 0, 1, 1, 2], dtype=int32), 3, [2.0, 7.5]) >>> utils.bin_series(xs, bounds=[2.0]) (array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1], dtype=int32), 2, [2.0]) ``` -------------------------------- ### Configure ctypes for inform functions Source: https://elife-asu.github.io/PyInform/_modules/pyinform/utils/encoding.html Sets the argument types and return types for the C-based inform_encode and inform_decode functions. ```python _inform_encode = _inform.inform_encode _inform_encode.argtypes = [POINTER(c_int), c_ulong, c_int, POINTER(c_int)] _inform_encode.restype = c_int _inform_decode = _inform.inform_decode _inform_decode.argtypes = [c_int, c_int, POINTER(c_int), c_ulong, POINTER(c_int)] _inform_decode.restype = None ``` -------------------------------- ### Determine Dist Support Size Source: https://elife-asu.github.io/PyInform/dist.html Determine the size of the support of the distribution. See also counts(). ```python >>> len(Dist(5)) 5 >>> len(Dist([0,1,5])) 3 ``` -------------------------------- ### Compute block entropy for a single initial condition Source: https://elife-asu.github.io/PyInform/_modules/pyinform/blockentropy.html Calculate block entropy for a sequence with a specified block size k, optionally returning local entropy values. ```python >>> block_entropy([0,0,1,1,1,1,0,0,0], k=1) 0.9910760598382222 >>> block_entropy([0,0,1,1,1,1,0,0,0], k=1, local=True) array([[0.84799691, 0.84799691, 1.169925 , 1.169925 , 1.169925 , 1.169925 , 0.84799691, 0.84799691, 0.84799691]]) ``` ```python >>> block_entropy([0,0,1,1,1,1,0,0,0], k=2) 1.811278124459133 >>> block_entropy([0,0,1,1,1,1,0,0,0], k=2, local=True) array([[1.4150375, 3. , 1.4150375, 1.4150375, 1.4150375, 3. , 1.4150375, 1.4150375]]) ``` -------------------------------- ### Calculate Entropy Rate for Multiple Series Source: https://elife-asu.github.io/PyInform/_modules/pyinform/entropyrate.html This function handles multiple initial conditions. Pass a list of series and the history length 'k'. ```python >>> series = [[0,0,1,1,1,1,0,0,0], [1,0,0,1,0,0,1,0,0]] >>> entropy_rate(series, k=2) 0.6253491072973907 ``` -------------------------------- ### Bin Time Series by Fixed Step Size Source: https://elife-asu.github.io/PyInform/_modules/pyinform/utils/binning.html Bins a continuous time series using a fixed step size for each bin. Returns the binned series, the number of bins, and the step size. Useful when a system has a specific precision threshold. ```python import numpy as np # ... (previous imports and series_range function) def bin_series(series, b=None, step=None, bounds=None): """ Bin a continously-valued times series. The binning can be performed in any one of three ways. .. rubric:: 2. Fixed Size Bins The second type of binning produces bins of a specific size *step*. .. doctest:: utils >>> utils.bin_series(xs, step=4.0) (array([2, 0, 1, 1, 2, 0, 1, 2, 2, 0, 1, 0, 0, 2, 0, 2, 0, 1, 0, 2], dtype=int32), 3, 4.0) >>> utils.bin_series(xs, step=2.0) (array([4, 1, 2, 3, 4, 1, 3, 4, 4, 1, 2, 1, 0, 4, 0, 4, 0, 2, 0, 4], dtype=int32), 5, 2.0) As in the previous case the binned sequence, the number of bins, and the size of each bin are returned. This approach is appropriate when the system at hand has a particular sensitivity or precision, e.g. if the system is sensitive down to 5.0mV changes in potential. """ # ... (rest of the function implementation for b, step, bounds) ``` -------------------------------- ### Dump all probabilities to an array Source: https://elife-asu.github.io/PyInform/_sources/dist.rst.txt Outputs all calculated probabilities for the events in the distribution as a NumPy array. Useful for a quick overview of the distribution's probabilities. ```python d = Dist([3,0,1,2]) d.dump() ``` -------------------------------- ### Dump Distribution to Array Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Dumps the distribution data into a pre-allocated array of doubles. Requires a pointer to the distribution, a pointer to the array, and the size of the array. ```python _dist_dump = _inform.inform_dist_dump _dist_dump.argtypes = [c_void_p, POINTER(c_double), c_ulong] _dist_dump.restype = c_ulong ``` -------------------------------- ### Calculate Transfer Entropy with One Background Process Source: https://elife-asu.github.io/PyInform/_modules/pyinform/transferentropy.html Compute transfer entropy conditioned on a single background process. ```python >>> xs = [0,1,1,1,1,0,0,0,0] >>> ys = [0,0,1,1,1,1,0,0,0] >>> ws = [0,1,1,1,1,0,1,1,1] >>> transfer_entropy(xs, ys, k=2, condition=ws) 0.2857142857142857 >>> transfer_entropy(xs, ys, k=2, condition=ws, local=True) array([[1., 0., 0., 0., 0., 0., 1.]]) ``` -------------------------------- ### Allocate Distribution Memory Source: https://elife-asu.github.io/PyInform/_modules/pyinform/dist.html Allocates memory for a distribution. Requires the size of the distribution. ```python _dist_alloc = _inform.inform_dist_alloc _dist_alloc.argtypes = [c_ulong] _dist_alloc.restype = c_void_p ``` -------------------------------- ### Set observations using indexing Source: https://elife-asu.github.io/PyInform/_sources/dist.rst.txt Assigns observed counts to events using standard list-like indexing. Useful for setting initial or known counts for each event. ```python d = Dist(5) for i in range(len(d)): d[i] = i*i list(d) ```