-
Notifications
You must be signed in to change notification settings - Fork 10
Extended time sensitivity #192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akiaei
wants to merge
30
commits into
ELIFE-ASU:master
Choose a base branch
from
akiaei:ExtendedTime-Sensitivity
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
bc4588e
Not much here. Mostly comments
ab96e09
First attempt at implementation
Myles-Damon d975436
New testing notebook & fixed mistakes which stopped the code from runnin
63ca11b
The C-Sensitivity isn't right for c=1. IDK what is wrong
Myles-Damon 2a47369
Merge branch 'master' into Derrida-Plot-Branch
tamart21 736cf6a
Fixed C-Sensitivity by using distance instead of indicator function
6463211
Merge branch 'master' into Myles-Branch-C-Sensitivity
2e3ce46
Merge pull request #1 from akiaei/Myles-Branch-C-Sensitivity
akiaei 3bad83d
Merge branch 'master' into Derrida-Plot-Branch
tamart21 854e31c
Just added some of the in-code documentation to C-sens functions
ba040a9
Merge remote-tracking branch 'origin/master' into ExtendedTime-Sensit…
2a4fd1b
be0ccca
Fixed a latent overflow issue w/ np.pow and cleaned up derrida-plot()
20f7d7c
Updated sensitivity and average_sensitivity to account for a timestep…
11f5ecc
Merge branch 'Derrida-Plot-Branch' into ExtendedTime-Sensitivity
9cd3d5c
Added Extended-Time plot method
wtopping e2d2528
360d6d7
Merge branch 'ExtendedTime-Sensitivity' into Extended-Time-Plot
wtopping be418f8
de11df3
Addressed issues brought up in the PR response
Myles-Damon f55e4e0
Update neet/boolean/sensitivity.py
Myles-Damon a6d303a
mostly cosmetic changes tbh
Myles-Damon 5ecaa2a
ef63099
Merge branch 'Myles-Branch-C-Sensitivity' into ExtendedTime-Sensitivity
9fdb2f7
57b7533
31cb3db
Merge remote-tracking branch 'Original-Elife-ASU/master' into Extende…
12f70eb
4287161
:shirt: Satisfy autopep8
dglmoore 54fdf51
:hammer::heavy_check_mark: Bring sensitivity tests to 100%
dglmoore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,13 @@ | |
| import copy | ||
| import numpy as np | ||
| import numpy.linalg as linalg | ||
| import math | ||
| import itertools as itt | ||
| import matplotlib.pyplot as plt | ||
|
|
||
| """ | ||
| .. import matplotlib.pyplot as plt | ||
| """ | ||
|
|
||
|
|
||
| class SensitivityMixin(object): | ||
|
|
@@ -35,7 +42,7 @@ class SensitivityMixin(object): | |
| network models. | ||
| """ | ||
|
|
||
| def sensitivity(self, state, transitions=None): | ||
| def sensitivity(self, state, transitions=None, timesteps=1): | ||
| """ | ||
| Compute the Boolean sensitivity at a given network state. | ||
|
|
||
|
|
@@ -83,17 +90,21 @@ def sensitivity(self, state, transitions=None): | |
| """ | ||
| encoder = self._unsafe_encode | ||
| distance = self.distance | ||
| neighbors = self.hamming_neighbors(state) | ||
|
|
||
| nextState = self.update(state) | ||
| neighbors = self.hamming_neighbors(state, c=1) | ||
| #neighbors_copy = [neighbor.copy() for neighbor in neighbors] | ||
| for t in range(timesteps): | ||
| nextState = self.update(state) | ||
|
|
||
| # count sum of differences found in neighbors of the original | ||
| s = 0. | ||
|
|
||
| for neighbor in neighbors: | ||
| if transitions is not None: | ||
| newState = transitions[encoder(neighbor)] | ||
| else: | ||
| newState = self._unsafe_update(neighbor) | ||
| for t in range(timesteps): | ||
| if transitions is not None: | ||
| newState = transitions[encoder(neighbor)] | ||
| neighbor = newState | ||
| else: | ||
| newState = self._unsafe_update(neighbor) | ||
| s += distance(newState, nextState) | ||
|
|
||
| return s / self.size | ||
|
|
@@ -438,7 +449,7 @@ def lambdaQ(self, **kwargs): | |
| Q = self.average_difference_matrix(**kwargs) | ||
| return max(abs(linalg.eigvals(Q))) | ||
|
|
||
| def average_sensitivity(self, states=None, weights=None, calc_trans=True): | ||
| def average_sensitivity(self, states=None, weights=None, calc_trans=True, timesteps=1): | ||
| """ | ||
| Calculate average Boolean network sensitivity, as defined in | ||
| [Shmulevich2004]_. | ||
|
|
@@ -479,8 +490,243 @@ def average_sensitivity(self, states=None, weights=None, calc_trans=True): | |
|
|
||
| .. seealso:: :func:`sensitivity` | ||
| """ | ||
| if(timesteps==1): | ||
| Q = self.average_difference_matrix(states=states, weights=weights, calc_trans=calc_trans) | ||
| return np.sum(Q) / self.size | ||
| else: | ||
| total = 0 | ||
| if calc_trans: | ||
| decoder = self.decode | ||
| trans = list(map(decoder, self.transitions)) | ||
| else: | ||
| trans = None | ||
| if states is not None: | ||
| # print("states: ", states) | ||
| for state in states: | ||
| # print("state: ", state) | ||
| val = self.sensitivity(state, trans, timesteps) | ||
| # print(" val= ", val) | ||
| total += val | ||
| avg = total/ (len(states)) | ||
| return avg | ||
| else: | ||
| for state in self: | ||
| # print("state: ", state) | ||
| # for t in range(1, timesteps) : | ||
| val = self.sensitivity(state, trans, timesteps) | ||
| # print(" val= ", val) | ||
| total += val | ||
| avg = total/ self.volume | ||
| return avg | ||
|
|
||
| def c_sensitivity(self, state, transitions=None, c=1): | ||
|
|
||
| assert (isinstance(c, int)),"c needs to be an integer" | ||
| assert(c >= 0), "the value of c needs to be greater than or equal to zero" | ||
| assert (c <= self.size),"the value of c needs to be between 0 and the size of the network" | ||
|
|
||
|
|
||
|
|
||
| """ | ||
| C-Sensitivity modification of the regular sensitivity function. | ||
|
|
||
| The c-sensitivity of :math:`f(x_1, //ldots, x_n)` at :math:`x` is defined as the number of | ||
| c-Hamming neighbors of :math:`x` on which the function value is different from its value on :math:`x`. That is, | ||
|
|
||
| :param state: a single network state | ||
| :type state: list, numpy.ndarray | ||
| :param transitions: precomputed state transitions (*optional*) | ||
| :type transitions: list, numpy.ndarray, None | ||
| :return: the C-sensitivity at the provided state | ||
|
|
||
| """ | ||
|
|
||
| encoder = self._unsafe_encode | ||
| distance = self.distance | ||
| state_copy = copy.copy(state) | ||
| nextState = self.update(state) | ||
|
|
||
| """ | ||
| Returns an iterator for each vector I which is a strict subset of {1,...,n} and where |I| = c | ||
| note: if c = 0, this will return an empty tuple | ||
| """ | ||
|
|
||
| I_comb_iter = itt.combinations(range(self.size), c) | ||
|
|
||
| """ | ||
| Generator function which returns a new hamming neighbor | ||
| Each hamming neighbor is simply the product of self.state XOR I | ||
| """ | ||
| def c_hamming_neighbors(self, state, c): | ||
| try: | ||
| nxt = next(I_comb_iter) | ||
| XORed = copy.copy(state_copy) | ||
| for i in nxt: | ||
| XORed[i] ^= 1 | ||
| return XORed | ||
| except StopIteration: | ||
| return None | ||
|
|
||
|
|
||
| """ | ||
| #OK, so I messed with the function and it's only ~kinda~ a generator function now... | ||
| It's automatically advanced in the for loop, which | ||
| acts as a "try: next(neighbors); catch StopIteration:". This behavior is built | ||
| into Python and is idiomatic. | ||
| """ | ||
|
|
||
| s = 0. | ||
| neighbors_copy = []#uses quite a bit of memory and should be removed from code once unit testing is completed | ||
| copy_counter = 0 | ||
|
|
||
| neighbor = c_hamming_neighbors(self,state,c) | ||
| while neighbor is not None: | ||
| neighbors_copy.append(copy.copy(neighbor)) | ||
| if transitions is not None: | ||
| newState = transitions[encoder(neighbor)] | ||
| else: | ||
| newState = self._unsafe_update(neighbor) | ||
|
|
||
| # the paper which describes c-sensitivity uses an indicator function | ||
| # instead of a distance function. Distance will be used here instead of the indicator. | ||
|
|
||
| if distance(newState, nextState) > 0: | ||
| if c == 0: | ||
| # The distance between 0-hamming neighbors should | ||
| # be 0, since a 0-hamming neighbor is just the | ||
| # original state/vector | ||
| print("This shouldn't print. 0-hamming neighbors should not diverge.") | ||
| print("c: ", c) | ||
| print("1. neighbor: ", neighbors_copy[copy_counter]) | ||
| print("2. state: ", state_copy,"\n") | ||
| print("1. newState: ", newState) | ||
| print("2. nextState: ", nextState,"\n\n") | ||
| s += distance(newState, nextState) | ||
| copy_counter += 1 | ||
| neighbor = c_hamming_neighbors(self,state,c) | ||
| return s / copy_counter | ||
|
|
||
|
|
||
| def average_c_sensitivity(self, states=None, calc_trans=True, c=1): | ||
|
|
||
| """ | ||
| Simple acts as a for-loop which does some precomputation before generating | ||
| all possible states of the network (maintaining topology and connections, | ||
| just changing the initial node values to all possible combinations of active | ||
| nodes) | ||
| Each generated state's c-sensitivity is summed and then divided by the total | ||
| number of generated states. | ||
| """ | ||
|
|
||
|
|
||
| """ | ||
|
|
||
| :param states: a set of network states | ||
| :type states: list, numpy.ndarray | ||
| :param transitions: precomputed state transitions (*optional*) | ||
| :type transitions: list, numpy.ndarray, None | ||
| :param calc_trans: pre-compute all state transitions; ignored if | ||
| ``states`` or ``weights`` is ``None`` | ||
| :type calc_trans: bool | ||
| :return: the sensitivity averaged over all possible | ||
| states of the network | ||
|
|
||
| if states is not None: | ||
| num_states = 0 | ||
| states = list(states) | ||
| #print("type of states: ", type(states)) | ||
| #print("len of states: ", type(states)) | ||
| #print("len of states: ", len(states)) | ||
| #print("len of states: ", len(states)) | ||
| if calc_trans: | ||
| decoder = self.decode | ||
| trans = list(map(decoder, self.transitions)) | ||
| else: | ||
| trans = None | ||
|
|
||
| """ | ||
|
|
||
|
|
||
| s = 0 | ||
|
|
||
| # optionally pre-calculate transitions | ||
| if calc_trans: | ||
| decoder = self.decode | ||
| trans = list(map(decoder, self.transitions)) | ||
| else: | ||
| trans = None | ||
|
|
||
|
|
||
| if states is not None: | ||
| # Iterate through all provided states and sum | ||
| # the distances between them and their c-hamming-neighbors | ||
| # after a single time-step (one synchronous transition) | ||
| for state in states: | ||
| s += self.c_sensitivity(state, trans, c) | ||
|
|
||
| # get the average of s by diving the sum by the | ||
| # number of states considered | ||
| s = s / len(states) | ||
|
|
||
|
|
||
| else: | ||
| # Generate all possible states | ||
| # and sum the distances between them and each of their | ||
| # c-hamming-neighbors in the next time step (after one | ||
| # synchronous transition) | ||
| for state in self: | ||
| s += self.c_sensitivity(state, trans, c) | ||
|
|
||
|
|
||
| # Deprecated implementation I'm keeping around while we implement unit testing. Will be removed after | ||
| # equivalence/correctness of the above (2 line) implementation is established | ||
| """for n in range(self.size): | ||
| state_gen = itt.combinations(range(self.size),n) | ||
| for state in state_gen: | ||
| state_array = [0 for x in range(self.size)] | ||
| for index in state: | ||
| state_array[index] = 1 | ||
| s += self.c_sensitivity(state_array, trans, c)""" | ||
|
|
||
| # Average s by diving the sum by the | ||
| # total number of possible states (2^n) | ||
| # s = s / np.power(2, self.size) | ||
| s = s / self.volume | ||
| """ | ||
| s is now the average C-Sensitivity of f and must lie in the interval [0, (n choose c)] | ||
| where n is the size of the network. | ||
| """ | ||
|
|
||
| upper_bound = math.factorial(self.size) / (math.factorial(c) * math.factorial(self.size - c)) | ||
| if s > upper_bound or s < 0: | ||
| raise RuntimeError('This value of S should not be possible and the code is therefore wrong') | ||
|
|
||
| return s | ||
| #yield s / upper_bound # yields the normalized average c-sensitivity | ||
|
|
||
| def derrida_plot(self, max_c=None):#, transitions=None): #X-Axis = c value, Y-Axis = output of Average_c_sensitivity | ||
| if max_c is None: | ||
| max_c = self.size | ||
|
|
||
| plt.title('Derrida Plot') | ||
| y_vals = [] | ||
|
|
||
| """if transitions is not None: | ||
| for x in range(max_c): | ||
| y_vals.append(self.Average_c_sensitivity(self, transitions)) | ||
| else: | ||
| for x in range(max_c): | ||
| y_vals.append(self.Average_c_sensitivity(self))""" | ||
| for x in range(max_c): | ||
| y_vals.append(self.Average_c_sensitivity(states=None, calc_trans=True, c=x)) | ||
|
|
||
| print(y_vals) | ||
| def Extended_Time_Plot(self, max_timesteps=4, transitions=None): #X-Axis = c value, Y-Axis = output of Extended_Time | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See previous suggestion. |
||
| if max_c is None: | ||
| max_c = self.size | ||
|
|
||
| Q = self.average_difference_matrix(states=states, weights=weights, | ||
| calc_trans=calc_trans) | ||
| plt.title('Extended_Time_Plot') | ||
| y_vals = [] | ||
|
|
||
| return np.sum(Q) / self.size | ||
| for x in range(max_timesteps): | ||
| y_vals.append(self.average_sensitivity(states=None, weights=None, calc_trans=True, timesteps=x)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, it seems this plot isn't actually plotting the data! You'll need to call
plt.plotwithxandycoordinates, and it might be good to add some default labels on the axes.I'm thinking something like...
By returning the figure and axes, end users can further modify the plots before they save them.