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 all 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
84 changes: 84 additions & 0 deletions src/foapy/characteristics/ma/arigthmetic_mean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import numpy as np


def arigthmetic_mean(intervals):
"""
Arithmetic mean is calculated as the sum of
the elements of the sequence divided
by the number of elements in the sequence.

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

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

Examples
--------

----1----
>>> X = [
[1 4 4]
[1 3]
[3 1]
]
>>> b = arigthmetic_mean(X)
>>> b
[3 2 2]

----2----
>>> X = [
[1 1 4 4]
[3 1 3]
[5 3 1]
]
>>> b = arigthmetic_mean(X)
>>> b
[2.5 2.333 3]

----3----
>>> X = [
[1 4 4 1]
[1 3 4]
[3 1 2]
]
>>> b = arigthmetic_mean(X)
>>> b
[2.5 2.66 2]

----4----
>>> X = [
[4 1 3 3]
]
>>> b = arigthmetic_mean(X)
>>> b
[2.75]

----5----
>>> X = [[]]
>>> b = arigthmetic_mean(X)
>>> b
[0]

----6----
>>> X = [[1]]
>>> b = arigthmetic_mean(X)
>>> b
[1]

----7----
>>> X = [
[1 1 1 1 1]
]
>>> b = arigthmetic_mean(X)
>>> b
[1]

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

from foapy.characteristics.ma.depth import depth


def average_remoteness(intervals):
"""
Calculation of the average remoteness of a sequence:
The average remoteness is calculated as the depth
divided by the number of intervals in the
given congeneric sequence.

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

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

Examples
--------

----1----
>>> X = [
[1 4 4]
[1 3]
[3 1]
]
>>> b = average_remoteness(X)
>>> b
[1.3333 0.79248 0.79248]

----2----
>>> X = [
[1 1 4 4]
[3 1 3]
[5 3 1]
]
>>> b = average_remoteness(X)
>>> b
[1 1.05664 1.30229]

----3----
>>> X = [
[1 4 4 1]
[1 3 4]
[3 1 2]
]
>>> b = average_remoteness(X)
>>> b
[1 1.1949 0.8616]

----4----
>>> X = [
[4 1 3 3]
]
>>> b = average_remoteness(X)
>>> b
[1.2925]

----5----
>>> X = [[]]
>>> b = average_remoteness(X)
>>> b
[0]

----6----
>>> X = [[1]]
>>> b = average_remoteness(X)
>>> b
[0]

----7----
>>> X = [
[1 1 1 1 1]
]
>>> b = average_remoteness(X)
>>> b
[0]

"""
size = np.array([len(elem) for elem in intervals])
depth_seq = depth(intervals)
res = np.divide(depth_seq, size, out=np.zeros_like(depth_seq), where=size != 0)
return res
85 changes: 85 additions & 0 deletions src/foapy/characteristics/ma/depth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import numpy as np

from foapy.characteristics.ma.volume import volume


def depth(intervals):
"""
Calculation of the depth of a sequence:
Depth is calculated as the logarithm base 2 (log₂) of the volume,
where the volume is the product of the elements
of the intervals in the sequence.

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


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

Examples
--------

----1----
>>> X = [
[1 4 4]
[1 3]
[3 1]
]
>>> b = depth(X)
>>> b
[4 1.585 1.585]

----2----
>>> X = [
[1 1 4 4]
[3 1 3]
[5 3 1]
]
>>> b = depth(X)
>>> b
[4 3.1699 3.9069]

----3----
>>> X = [
[1 4 4 1]
[1 3 4]
[3 1 2]
]
>>> b = depth(X)
>>> b
[4 3.585 2.585]

----4----
>>> X = [
[4 1 3 3]
]
>>> b = depth(X)
>>> b
[5.1699]

----5----
>>> X = [[]]
>>> b = depth(X)
>>> b
[]

----6----
>>> X = [[1]]
>>> b = depth(X)
>>> b
[0]

----7----
>>> X = [
[1 1 1 1 1]
]
>>> b = depth(X)
>>> b
[0]

"""
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

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


def geometric_mean(intervals):
"""
Calculation of the geometric mean of a sequence:
The geometric mean is calculated as the nth root
of the product of the elements in the sequence,
where n is the number of elements in the sequence.

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

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

Examples
--------

----1----
>>> X = [
[1 4 4]
[1 3]
[3 1]
]
>>> b = geometric_mean(X)
>>> b
[2.5198 1.73205 1.73205]

----2----
>>> X = [
[1 1 4 4]
[3 1 3]
[5 3 1]
]
>>> b = geometric_mean(X)
>>> b
[2 2.08 2.466]

----3----
>>> X = [
[1 4 4 1]
[1 3 4]
[3 1 2]
]
>>> b = geometric_mean(X)
>>> b
[2 2.28942 1.8171]

----4----
>>> X = [
[4 1 3 3]
]
>>> b = geometric_mean(X)
>>> b
[2.449489]

----5----
>>> X = [[]]
>>> b = geometric_mean(X)
>>> b
[0]

----6----
>>> X = [[1]]
>>> b = geometric_mean(X)
>>> b
[0]

----7----
>>> X = [
[0 0 0 0 0]
]
>>> b = geometric_mean(X)
>>> b
[0]

"""
return np.asanyarray(
[
np.power(np.prod(line), 1 / len(line)) if len(line) != 0 else 0
for line in intervals
],
dtype=float,
)
Loading
Loading