Skip to content
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

ma_characteristics #47

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
24 changes: 24 additions & 0 deletions src/foapy/characteristics/ma/arigthmetic_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np


def arigthmetic_mean(intervals):
"""
Calculation volume of sequence.
Volume is the product of the elements of the intervals of the sequence.

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------


"""

return np.asanyarray([np.sum(line) / len(line) for line in intervals])
29 changes: 29 additions & 0 deletions src/foapy/characteristics/ma/average_remoteness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import numpy as np

from foapy.characteristics.ma.depth import depth


def average_remoteness(intervals):
"""
Calculation geometric of sequence.
Average remoteness is the Depth divide by number of intervals
in the given congeneric sequence.

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------


"""

size = np.asanyarray([len(elem) for elem in intervals])
res = depth(intervals) / size
return res
24 changes: 24 additions & 0 deletions src/foapy/characteristics/ma/depth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np

from foapy.characteristics.ma.volume import volume


def depth(intervals):
"""
Calculation depth of sequence.
Depth is the log2 of the volume.

Parameters
----------
X: two-dimensional array
Source array sequence.


Returns
-------
result: array.

Examples
--------
"""
return np.asanyarray([np.log2(line) for line in volume(intervals)])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace volume with sum log2

22 changes: 22 additions & 0 deletions src/foapy/characteristics/ma/entropy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import numpy as np


def entropy(intervals):
"""

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------


"""

return np.asanyarray([np.log2(line.mean()) for line in intervals])
goruha marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions src/foapy/characteristics/ma/geometric_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import numpy as np


def geometric_mean(intervals):
"""
Calculation geometric of sequence.
Geometric mean is the square root of the product sequence.

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------


"""

return np.asanyarray([np.power(np.prod(line), 1 / len(line)) for line in intervals])
28 changes: 28 additions & 0 deletions src/foapy/characteristics/ma/periodicity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np

from foapy.characteristics.ma.arigthmetic_mean import arigthmetic_mean
from foapy.characteristics.ma.geometric_mean import geometric_mean


def periodicity(intervals):
"""
Calculation geometric of sequence.
Periodicity is the substraction between entropy and average remoteness
in the given congeneric sequence.

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------


"""

return np.divide(geometric_mean(intervals), arigthmetic_mean(intervals))
28 changes: 28 additions & 0 deletions src/foapy/characteristics/ma/uniformity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np

from foapy.characteristics.ma.average_remoteness import average_remoteness
from foapy.characteristics.ma.entropy import entropy


def uniformity(intervals):
"""
Calculation geometric of sequence.
Uniformity is the substraction between entropy and average remoteness
in the given congeneric sequence.

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------


"""

return np.subtract(entropy(intervals), average_remoteness(intervals))
64 changes: 64 additions & 0 deletions src/foapy/characteristics/ma/volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import numpy as np


def volume(intervals):
"""
Calculation volume of sequence.
Volume is the product of the elements of the intervals of the sequence.

Parameters
----------
X: two-dimensional array
Source array sequence.

Returns
-------
result: array.

Examples
--------

----1----
>>> a = ["B", "B", "A", "A", "C", "B", "A", "C", "C", "B"]
>>> b = volume(X, binding.start, mode.lossy)
>>> b
[16 3 3]

----2----
>>> a = ["B", "B", "A", "A", "C", "B", "A", "C", "C", "B"]
>>> b = volume(X, binding.start, mode.normal)
>>> b
[16 12 6]

----3----
>>> a = ["B", "B", "A", "A", "C", "B", "A", "C", "C", "B"]
>>> b = volume(X, binding.end, mode.normal)
>>> b
[16 36 30]

----4----
>>> a = ["B", "B", "A", "A", "C", "B", "A", "C", "C", "B"]
>>> b = volume(X, binding.start, mode.redundant)
>>> b
[16 36 30]

----5----
>>> a = ["B", "B", "A", "A", "C", "B", "A", "C", "C", "B"]
>>> b = volume(X, binding.start, mode.cycle)
>>> b
[16 18 18]

----6----
>>> a = ["B", "A"]
>>> b = volume(X, binding.start, mode.lossy)
>>> b
[1 1]

----7----
>>> a = ["B", "A", "C", "D"]
>>> b = volume(X, binding.start, mode.lossy)
>>> b
[1 1 1 1]
"""

return np.asanyarray([np.prod(line) for line in intervals])
67 changes: 67 additions & 0 deletions tests/test_characteristics/test_ma_arighmetic_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from unittest import TestCase

import numpy as np
import numpy.ma as ma

from foapy.characteristics.ma.arigthmetic_mean import arigthmetic_mean
from foapy.constants_intervals import binding as binding_constant
from foapy.constants_intervals import mode as mode_constant
from foapy.ma.intervals import intervals
from foapy.ma.order import order


class TestMaArigthmeticMean(TestCase):
"""
Test list for volume calculate
"""

def test_calculate_start_lossy_arigthmetic_mean(self):
X = ma.masked_array([2, 4, 2, 2, 4])
order_seq = order(X)
intervals_seq = intervals(
order_seq, binding_constant.start, mode_constant.normal
)
expected = np.array([1.3333, 2.5])
exists = arigthmetic_mean(intervals_seq)
epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))

def test_calculate_start_normal_arigthmetic_mean(self):
X = ma.masked_array([1, 2, 3])
order_seq = order(X)
intervals_seq = intervals(
order_seq, binding_constant.start, mode_constant.normal
)
expected = np.array([1, 2, 3])
exists = arigthmetic_mean(intervals_seq)

epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))

def test_calculate_end_normal_arigthmetic_mean(self):
X = ma.masked_array([1, 2, 3])
order_seq = order(X)
intervals_seq = intervals(order_seq, binding_constant.end, mode_constant.normal)
expected = np.array([3, 2, 1])
exists = arigthmetic_mean(intervals_seq)

epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))

def test_calculate_start_redunant_depth(self):
X = ["B", "B", "B", "A", "A", "B", "B", "A", "B", "B"]
mask = [1, 1, 1, 0, 0, 1, 1, 0, 1, 1]
masked_X = ma.masked_array(X, mask)
order_seq = order(masked_X)
intervals_seq = intervals(
order_seq, binding_constant.start, mode_constant.lossy
)
expected = np.array([1.5, 2]) # incorrect test
exists = arigthmetic_mean(intervals_seq)
epsilon = 0.01
print(exists)
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))
63 changes: 63 additions & 0 deletions tests/test_characteristics/test_ma_average_remoteness.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from unittest import TestCase

import numpy as np
import numpy.ma as ma

from foapy.characteristics.ma.average_remoteness import average_remoteness
from foapy.constants_intervals import binding as binding_constant
from foapy.constants_intervals import mode as mode_constant
from foapy.ma.intervals import intervals
from foapy.ma.order import order


class TestMaAverageRemoteness(TestCase):
"""
Test list for average remoteness calculate
"""

def test_calculate_start_normal_average_remoteness(self):
X = ma.masked_array([2, 4, 2, 2, 4])
order_seq = order(X)
intervals_seq = intervals(
order_seq, binding_constant.start, mode_constant.normal
)
expected = np.array([0.3333333, 1.29248])
exists = average_remoteness(intervals_seq)

epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))

def test_calculate_start_average_remoteness_2(self):
X = ma.masked_array([1, 2, 3])
order_seq = order(X)
intervals_seq = intervals(
order_seq, binding_constant.start, mode_constant.normal
)
expected = np.array([0, 1, 1.5849625])
exists = average_remoteness(intervals_seq)
epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))

def test_calculate_end_normal_average_remoteness(self):
X = ma.masked_array([1, 2, 3])
order_seq = order(X)
intervals_seq = intervals(order_seq, binding_constant.end, mode_constant.normal)
expected = np.array([1.5849625, 1, 0])
exists = average_remoteness(intervals_seq)
epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))

def test_calculate_start_lossy_empty_values_average_remoteness(self):
X = ma.masked_array([])
order_seq = order(X)
intervals_seq = intervals(
order_seq, binding_constant.start, mode_constant.lossy
)
expected = np.array([])
exists = average_remoteness(intervals_seq)
epsilon = 0.01
diff = np.absolute(expected - exists)
self.assertTrue(np.all(diff < epsilon))
Loading
Loading