Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bc4588e
Not much here. Mostly comments
Oct 27, 2019
ab96e09
First attempt at implementation
Myles-Damon Oct 28, 2019
d975436
New testing notebook & fixed mistakes which stopped the code from runnin
Oct 28, 2019
63ca11b
The C-Sensitivity isn't right for c=1. IDK what is wrong
Myles-Damon Oct 29, 2019
736cf6a
Fixed C-Sensitivity by using distance instead of indicator function
Nov 5, 2019
6463211
Merge branch 'master' into Myles-Branch-C-Sensitivity
Nov 6, 2019
2e3ce46
Merge pull request #1 from akiaei/Myles-Branch-C-Sensitivity
akiaei Nov 6, 2019
a798857
Merge remote-tracking branch 'Original-Elife-ASU/master'
Feb 23, 2020
81ff8c7
:hammer: Create the neet.boolean.random submodule
dglmoore Mar 5, 2020
5bbbd99
:green_heart: Remove CI testing on Python2.7
dglmoore Mar 5, 2020
5124c7c
Merge remote-tracking branch 'Original-Elife-ASU/random' into randomN…
Mar 19, 2020
25691f5
Mar 24, 2020
735e63a
Added basic value/type error checking for constraints
Apr 9, 2020
3b7a4de
Added more constraint test cases
Apr 9, 2020
061c5d6
Finished constraint tests except for generic topological and dynamical
Apr 9, 2020
ce3b0db
Incomplete list of tests for dynamic randomizers
Myles-Damon Apr 23, 2020
6d6d427
Finished constraints testing
Apr 23, 2020
6e1549d
incomplete unit tests for topology
wtopping Apr 23, 2020
d0b56b3
forgot to upload these unit tests
Myles-Damon Apr 27, 2020
7d22227
Fix extra_requires in setup.py
dglmoore Feb 19, 2021
8d46eb4
Omit .eggs/* from green test coverage report
dglmoore Feb 19, 2021
828cbfb
Fix numpy deprecation warnings
dglmoore Feb 20, 2021
91d06e5
Merge pull request #199 from akiaei/randomNetTests
dglmoore Feb 20, 2021
feb3eda
Remove randomization tests
dglmoore Feb 24, 2021
f69ed1e
Include neet.boolean.random during install
dglmoore Dec 9, 2021
f3ea675
Remove c_sensitivity functions
dglmoore Dec 9, 2021
a694050
Unit test neet.boolean.random.constraints
dglmoore Dec 9, 2021
c772bcc
Add tests for neet.boolean.random.topology
dglmoore Dec 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ version: 0.1.0.{build}

environment:
matrix:
- PYTHON: "C:\\Python27-x64"
- PYTHON: "C:\\Python35-x64"
- PYTHON: "C:\\Python36-x64"
- PYTHON: "C:\\Python37-x64"
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ os:
# - osx

python:
- "2.7"
- "3.5"
- "3.6"
- "3.7"
Expand Down
1 change: 1 addition & 0 deletions neet/boolean/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
from .wtnetwork import WTNetwork # noqa
from .logicnetwork import LogicNetwork # noqa
from .sensitivity import SensitivityMixin # noqa
from . import random # noqa
4 changes: 4 additions & 0 deletions neet/boolean/random/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import constraints # noqa
from . import randomizer # noqa
from . import topology # noqa
from . import dynamics # noqa
281 changes: 281 additions & 0 deletions neet/boolean/random/constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
import neet
import networkx as nx
import numpy as np
from abc import ABCMeta, abstractmethod


class ConstraintError(Exception):
"""
A constraint was applied to an invalid object.
"""
pass


class AbstractConstraint(object, metaclass=ABCMeta):
"""
An abstract class representing a constraint used for rejection testing.
"""
@abstractmethod
def satisfies(self, net):
"""
Test a provided network against the constraint.

:param net: a network to test
:returns: ``True`` if the constraint is satisfied
"""
return True


class TopologicalConstraint(AbstractConstraint):
"""
An abstract class representing a constraint on the topology of a network.
"""
@abstractmethod
def satisfies(self, graph):
"""
Test a provided graph against the constraint.

:param graph: a graph to test
:type graph: nx.DiGraph
:returns: ``True`` if the constraint is satisfied
:raises TypeError: if the graph is not a networkx DiGraph
"""
if not isinstance(graph, nx.DiGraph):
raise TypeError('only directed graphs are testable with topological constraints')
return super().satisfies(graph)


class DynamicalConstraint(AbstractConstraint):
"""
An abstract class representing a constraint on the dynamics of a network.
"""
@abstractmethod
def satisfies(self, net):
"""
Test a provided net against the constraint.

:param net: a network to test
:type net: neet.Network
:returns: ``True`` if the constraint is satisfied
:raises TypeError: if the network is not a neet.Network
"""
if not isinstance(net, neet.Network):
raise TypeError('only neet networks are testable with dynamical constraints')
return super().satisfies(net)


class HasExternalNodes(TopologicalConstraint):
def __init__(self, target):
"""
An topological constraint requiring a specific number of external
nodes, i.e. a specific number of nodes with no incomming edges.

If ``target`` is a directed graph, this constraint will require
networks to have the same number of external nodes as the ``target``.

Alternativly, ``target`` can be a non-negative integer.

:param target: the target number of external nodes
:type target: nx.DiGraph or integer
"""
if isinstance(target, int):
if target < 0:
raise ValueError('the target number of external nodes must be non-negative')
num_external = target
elif isinstance(target, nx.DiGraph):
num_external = self.__count_external(target)
else:
raise TypeError('target must be either an integer or nx.DiGraph')

self.num_external = num_external

def __count_external(self, g):
"""
Count the number of external nodes in a directed graph.
"""
return np.count_nonzero([d == 0 for _, d in g.in_degree()])

def satisfies(self, graph):
"""
This constraint is only satisfied if the provided graph has
``self.num_external``-many external nodes.

:param graph: a graph to test
:type graph: nx.DiGraph
:returns: ``True`` if the digraph as the desired number of external
nodes
"""
if super().satisfies(graph):
return self.__count_external(graph) == self.num_external


class IsConnected(TopologicalConstraint):
"""
Ensure that the resulting graph is (weakly) connected.
"""
def satisfies(self, graph):
"""
This constraint is only satisfied if the provided graph as is weakly
connected.

:param graph: a graph to test
:type graph: nx.DiGraph
:returns: ``True`` if the digraph as the desired number of external
nodes
"""
if super().satisfies(graph):
try:
return nx.is_weakly_connected(graph)
except nx.exception.NetworkXException as err:
raise ConstraintError() from err


class IsIrreducible(DynamicalConstraint):
"""
Ensure that all dynamical nodes have irreducible functions.
"""
def satisfies(self, network):
"""
This constraint is only satisfied if every node's function logically
depends on each of it's incoming neighbors.

:param network: a network to test
:type network: neet.boolean.LogicNetwork
:returns: ``True`` if every node's function is irredicible
:raises NotImplementedError: if the network is not a
neet.boolean.LogicNetwork
"""
if super().satisfies(network):
if not isinstance(network, neet.boolean.LogicNetwork):
raise NotImplementedError()

for idx in range(network.size):
for neighbor_in in network.neighbors_in(idx):
if not network.is_dependent(idx, neighbor_in):
return False
return True


class HasCanalizingNodes(DynamicalConstraint):
def __init__(self, target):
"""
A dynamical constraint requiring that a specific number of nodes be
canalizing, i.e. a specific number of nodes are canalizing on at least
one input.

If ``target`` is a Neet network, this constraint will require that
networks to have the same number of canalizing nodes as ``target``.

Alternatively, ``target`` can be a non-negative integer.

:param target: the target number of canalizing nodes
:type target: neet.boolean.LogicNetwork or integer
:raises NotImplementedError: if the provied target is nei
"""
if isinstance(target, int):
if target < 0:
raise ValueError('the target number of canalizing nodes must be non-negative')
num_canalizing = target
elif isinstance(target, neet.Network):
num_canalizing = self.__count_canalizing_nodes(target)
else:
raise TypeError('target must be either an integer or a neet.Network')

self.num_canalizing = num_canalizing

def __count_canalizing_nodes(self, network):
"""
Count the number of canalizing nodes in a network.
"""
return len(network.canalizing_nodes())

def satisfies(self, network):
"""
This constraint is only satisfied if the provided network has
``self.num_canalizing``-many canalizing nodes.
"""
if super().satisfies(network):
return self.__count_canalizing_nodes(network) == self.num_canalizing


class GenericTopological(TopologicalConstraint):
def __init__(self, test):
"""
A generic constraint defined in terms of a callable.

:param test: a user-specified test
:type test: callable
"""
if not callable(test):
raise TypeError('test must be callable')
self.test = test

def satisfies(self, net):
"""
Test a provided network against a generic constraint.

:param net: a network to test
:returns: ``True`` if the constraint is satisified
"""
if super().satisfies(net):
return self.test(net)


class GenericDynamical(DynamicalConstraint):
def __init__(self, test):
"""
A generic constraint defined in terms of a callable.

:param test: a user-specified test
:type test: callable
"""
if not callable(test):
raise TypeError('test must be callable')
self.test = test

def satisfies(self, net):
"""
Test a provided network against a generic constraint.

:param net: a network to test
:returns: ``True`` if the constraint is satisified
"""
if super().satisfies(net):
return self.test(net)


class NodeConstraint(DynamicalConstraint):
"""
Constraints which operate on nodes (functions) of dynamical networks.
"""
pass


class GenericNodeConstraint(DynamicalConstraint):
def __init__(self, test):
if not callable(test):
raise TypeError('test must be callable')
self.test = test

def satisfies(self, net):
if super().satisfies(net):
return self.test(net)


class IrreducibleNode(NodeConstraint):
def satisfies(self, conditions):
if len(conditions) == 0:
return True
k = list(map(len, conditions))[0]
for i in range(k):
counter = {} # type: ignore
for state in conditions:
state_sans_source = state[:i] + state[i + 1:]
if int(state[i]) == 1:
counter[state_sans_source] = counter.get(state_sans_source, 0) + 1
else:
counter[state_sans_source] = counter.get(state_sans_source, 0) - 1

if not any(counter.values()):
return False
return True
Loading