Skip to content

Commit

Permalink
Merge pull request #509 from jamesmartini/develop_entropy506
Browse files Browse the repository at this point in the history
Develop entropy506
  • Loading branch information
GavinHuttley committed Jan 30, 2020
2 parents a5c1296 + 5bde5c6 commit a5e7784
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/cogent3/core/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,19 @@ def __init__(self, data, motifs, row_indices=None):
axis = 0 if self.array.ndim == 1 else 1
validate_freqs_array(self.array, axis=axis)

def entropy(self):
"""Shannon entropy per position using log2"""
def entropy_terms(self):
"""Returns
-------
entropies : array
Has same dimension as self.array with
safe log operation applied.
"""
entropies = safe_p_log_p(self.array)
return entropies.sum(axis=1)
return self.template.wrap(entropies)

def entropy(self):
"""Shannon entropy per position using safe log2"""
return self.entropy_terms().row_sum()

def information(self):
"""returns information as -max_entropy - entropy"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_core/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ def test_construct_fails(self):
with self.assertRaises(ValueError):
got = MotifFreqsArray(data, "AB")

def test_entropy_terms(self):
"""Checks entropy_terms works correctly"""
data = [[0.25, 0.25, 0.25, 0.25], [0.5, 0.5, 0, 0]]
got = MotifFreqsArray(array(data), "ABCD")
entropy_terms = got.entropy_terms()
expect = [[0.5, 0.5, 0.5, 0.5], [0.5, 0.5, 0, 0]]
assert_allclose(entropy_terms.array, expect)

def test_entropy(self):
"""calculates entripies correctly"""
data = [[0.25, 0.25, 0.25, 0.25], [0.5, 0.5, 0, 0]]
Expand Down

0 comments on commit a5e7784

Please sign in to comment.