A simple library to build easily interpretable computational constructs similar to a Turing machine over a graph, where states are combinations of a graph's (typed) nodes; an example use would be a transparent backend logic which navigates an ontology
Project description
A simple library to build easily interpretable computational constructs similar to Turing machines over graphs, where states are combinations of a graph’s (typed) nodes; an example use would be a transparent backend logic which navigates an ontology
Installation
pip install Graph_State_Machine
Description
This package implements a computational construct similar to a Turing machine over a graph, where states are node combinations (though more information may be stored) and where the arbitrary transition function can update both state and graph. Note that this last arbitrariness makes the system Turing complete since it allows implementing a Turing machine with it (achieved by defining the graph to be a linear array and the state as a tuple of node name and “head” state).
Given a graph with typed nodes and a state object from which a list of nodes can be extracted (by an optional Selector function), the construct applies two arbitrary functions to perform a step:
- Scanner
A generalised neighbourhood function, which scans the graph “around” the state nodes and returns a scored list of nodes for further processing; additional and optional arguments can be included, e.g. to filter by type
- Step
A function to process the scan result and thus update the state and possibly the graph itself
This computational construct is different from a finite state machine on a graph and from a graph cellular automaton, but it shares some similarities with both in that it generalises some of their features for the benefit of human ease of design and readability. For example, a GSM’s graph generalises a finite state machine’s state graph by allowing the combinations of nodes to represent state, and the scanner function is just a generalisation of a graph cellular automaton’s neighbourhood function in both domain and codomain. As previously mentioned, it is closer to a Turing machine on a graph than either of the above, one whose programming is split between the internal state rules and the graph topology, thus allowing programs to be simpler and with a more easily readable state.
Besides pure academic exploration of the construct, some possible uses of it are:
implementing backend logics which are best represented by graphs, e.g. an “expert system”
pathing through ontologies by entity proximity or similarity
Design
(Inspecting the package __init__.py imports is a quick and useful exercise in understanding the overall structure, while the following is a less concise version of the content of types.py)
Formalising the above description using library terminology, the constructor of the main class (GSM) takes the following arguments:
- Graph
A graph object with typed nodes (wrapping a NetworkX graph), with utility methods so that it can be built from shorthand notation (structured edge lists), check its own consistency, self-display and extend itself by joining up with another with common nodes (exact ontology matching)
- State
The initial state; the default type is a simple list of nodes (strings), but it can be anything as long as the used Scanner function is designed to handle it and a function to extract a list of strings from it is provided as the Selector argument
- Scanner (Graph -> List[Node] -> ... -> List[Tuple[Node, Any]])
A function taking in a list of state nodes to use to determine next-step candidates; arbitrary additional arguments, optional or required, may be present after Graph and List[Node], for example focussing the scan on specific node types; these extra arguments can be passed through the step methods either named (dictionary) or unnamed (list)
- Updater (State -> Graph -> ScanResult -> Tuple[State, Graph])
A function taking in the current state and graph along with the result of a node scan and returns the updated state and graph
- Selector (State -> List[Node])
A function to extract from the state the list of nodes which should be fed to the Scanner
A simple example of node-list state with non-identity Selector is a GSM which only takes the last “visited” node into account, and going one step further, an intuitive example of State which is not a simple node-list is a dictionary of node-lists only some subsets of which are considered for graph exploration (and others for state updating), e.g. keeping track of which nodes were initial state and which ones were added by steps.
Simple default constructor functions for this State type are provided: dict_fields_getter (for selector), which takes in the list of fields to concatenate, and list_in_dict_accumulator (for Updater), which takes in the single field to update.
Note: since the underlying object is a NetworkX graph, arbitrary node and edge attributes can be used to enhance the processing functions.
Simple Example
A GSM which determines the appropriate R linear regression function and distribution family from labelled data features:
Define a numerical data-type ontology graph in the typed edge-list shorthand which Graph accepts along with ready-made Networkx graphs, making use of two simple notation helper functions
Create a default-settings GSM with it and a simple starting state
Ask it to perform steps focussing on the node types of ‘Distribution’, ‘Methodology Function’ and ‘Family Implementation’, which in this context just means finding the most appropriate of each
from Graph_State_Machine import *
_shorthand_graph = {
'Distribution': {
'Normal': ['stan_glm', 'glm', 'gaussian'],
'Binomial': ['stan_glm', 'glm', 'binomial'],
'Multinomial': ['stan_polr', 'polr_tolerant', 'multinom'],
'Poisson': ['stan_glm', 'glm', 'poisson'],
'Beta': ['stan_betareg', 'betareg'],
'gamma': ['stan_glm', 'glm', 'Gamma'],
'Inverse Gaussian': ['stan_glm', 'glm', 'inverse.gaussian']
},
'Family Implementation': strs_as_keys(['binomial', 'poisson', 'Gamma', 'gaussian', 'inverse.gaussian']),
'Methodology Function': strs_as_keys(['glm', 'betareg', 'polr_tolerant', 'multinom', 'stan_glm', 'stan_betareg', 'stan_polr']),
'Data Feature': adjacencies_lossy_reverse({ # Reverse-direction definition here since more readable i.e. defining the contents of the lists
'Binomial': ['Binary', 'Integer', '[0,1]', 'Boolean'],
'Poisson': ['Non-Negative', 'Integer', 'Consecutive', 'Counts-Like'],
'Multinomial': ['Factor', 'Consecutive', 'Non-Negative', 'Integer'],
'Normal': ['Integer', 'Real', '+ and -'],
'Beta': ['Real', '[0,1]'],
'gamma': ['Non-Negative', 'Integer', 'Real', 'Non-Zero'],
'Inverse Gaussian': ['Non-Negative', 'Integer', 'Real', 'Non-Zero'],
'polr_tolerant': ['Consecutive']
})
}
gsm = GSM(Graph(_shorthand_graph), ['Non-Negative', 'Non-Zero', 'Integer']) # Default function-arguments
gsm.plot()
# gsm.plot(layout = nx.shell_layout, radial_labels = True)
# gsm.plot(plotly = False)
gsm.consecutive_steps(dict(node_types = ['Distribution']), dict(node_types = ['Family Implementation']))
# Perform 2 steps, giving one optional argument (incidentally, the first one) for each step,
# i.e. the (singleton) list of types to focus on
# gsm.consecutive_steps([['Distribution']], [['Family Implementation']]) # Unnamed-arguments version of the above
# gsm.parallel_steps([['Distribution']], [['Family Implementation']]) # Parallel version, warning of failure for 'Family Implementation'
print(gsm.log[-2], '\n') # Can check the log for details of the second-last step, where a tie occurs.
# Ties are rare, and the default Updater only picks one result, but arbitrary action may be taken
print(gsm._scan(['Methodology Function']), '\n') # Can also peek ahead at the intermediate value of a possible next step
gsm.step(['Methodology Function']) # Perform the step
gsm.step(['NON EXISTING TYPE']) # Trigger a warning and no State changes
print(gsm.log[-1], '\n') # The failed step is also logged
print(gsm)
The ‘Methodology Function’ scan above is peeked at before its step to show that there is a tie between a Frequentist and a Bayesian method. This is a trivial example (in that the simple addition could have been there from the beginning) of where a broader graph could be attached by gsm.extend_with(...) and new state introduced in order to resolve the tie.
Note that ties need not really be resolved as long as the Updater function’s behaviour is what the user expects since it is not limited in functionality; it could select a random option, all, some or none of them, it could adjust the graph itself or terminate execution.
Plotting
The default plot layout and backend are Kamada-Kawai and Plotly (as in the image above), but arbitrary layouts can be provided, and the NetworkX-generated pyplot plotting is also available. Here are some alternative plotting possibilities:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for Graph_State_Machine-2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 759afff89ff6f4d1d7ad1da15770d7d4c325d290084981a6918d3356130b49e5 |
|
MD5 | 48cfb2a437d676f100eae32cb31fba8b |
|
BLAKE2b-256 | 26721151d1fba037ba2dbfac752c5a88309ab97f350108a20aa1366ebaa4c2bf |