Skip to content

Commit

Permalink
Release 0.1.0.2
Browse files Browse the repository at this point in the history
* qSigmoid now accept a numpy array as input.
* Made some tests for qSigmoid.
  • Loading branch information
yan committed Sep 3, 2017
1 parent 85cf5e2 commit c8e1fb3
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 123 deletions.
220 changes: 130 additions & 90 deletions .idea/workspace.xml

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
History
=======

0.1.0.1 (2017-08-24)
0.1.0.2 (2017-09-03)
------------------

* qSigmoid now accept a numpy array as input.
* Made some tests for qSigmoid.

0.1.0.1 (2017-08-31)
------------------

* First release on PyPI.
2 changes: 1 addition & 1 deletion qstatistic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

__author__ = """Yan Anderson Siriano Duarte"""
__email__ = 'yan_asd@hotmail.com'
__version__ = '0.1.0.1'
__version__ = '0.1.0.2'
71 changes: 46 additions & 25 deletions qstatistic/qstatistic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import math
import numpy as np


class qExponential:
Expand All @@ -9,11 +10,11 @@ def __init__(self, x, q):

def calc(self):
if self.q == 1:
return math.exp(self.x)
return np.exp(self.x)
else:
temp = 1 + (1 - self.q)*self.x
if temp > 0:
return math.pow(temp, 1/(1 - self.q))
return np.power(temp, 1/(1 - self.q))
else:
return 0

Expand All @@ -26,9 +27,9 @@ def __init__(self, x, q):
def calc(self):
if self.x >= 0:
if self.q == 1:
return math.log(self.x)
return np.log(self.x)
else:
return (math.pow(self.x, 1 - self.q) - 1) / (1 - self.q)
return (np.power(self.x, 1 - self.q) - 1) / (1 - self.q)
else:
raise ValueError('Undefined')

Expand All @@ -45,52 +46,72 @@ def __init__(self, x, B, q):

# q exponential function e^-Bx²
self.e = qExponential(
x=-self.B*math.pow(self.x, 2),
x=-self.B*np.power(self.x, 2),
q=self.q
).calc()

# Normalization factor Cq
if self.q == 1:
self.Cq = math.sqrt(math.pi)
self.Cq = np.sqrt(np.pi)
else:
if self.q < 1:
self.Cq = 2*math.sqrt(math.pi)*math.gamma(1/(1-q))*(1/((3-q)*math.sqrt(1-q)*math.gamma((3-q)/(2*(1-q)))))
self.Cq = 2*np.sqrt(np.pi)*math.gamma(1/(1-q))*(1/((3-q)*np.sqrt(1-q)*math.gamma((3-q)/(2*(1-q)))))
else:
self.Cq = math.sqrt(math.pi)*math.gamma((3-q)/(2*(q-1)))*(1/(math.sqrt(q-1)*math.gamma(1/(q-1))))
self.Cq = np.sqrt(np.pi)*math.gamma((3-q)/(2*(q-1)))*(1/(np.sqrt(q-1)*math.gamma(1/(q-1))))

def calc(self):
return (math.sqrt(self.B)/self.Cq)*self.e
return (np.sqrt(self.B)/self.Cq)*self.e


# class qEntropy:

class qSigmoide:
def __init__(self, L, I, B, a, q):
class qSigmoid:
def __init__(self, I, L, B, a, q):
"""
Sigmoid Extended Function
:param I: Input Value
:param L: Maximum luminance value
:param B: Luminance value sought in the application
:param a: Range of luminance values around B
:param q:
"""
self.L = L
self.I = I
self.B = B
self.a = a
self.q = q

self.D = math.fabs((self.I - self.B)/self.a) + 0.0001
self.D = np.absolute((self.I - self.B)/self.a) + 0.0001

# In case of 'q' equals one, the result will be the traditional sigmoid function.
# Otherwise, we must invert D if (1 + (1 - self.q) * self.D < 0).
if self.q != 1:
if type(self.D) == np.ndarray:
# In case of input be an array invert only the points that satisfy the condition
self.D[(1 + (1 - self.q) * self.D) < 0] = -1 / self.D[(1 + (1 - self.q) * self.D) < 0]
elif type(self.D) == int or type(self.D) == float:
if (1 + (1 - self.q) * self.D) < 0:
self.D = -1 / self.D

def calc(self):
def get_D(self):
return self.D

def calc(self):
if self.q == 1:
result = self.L - (self.L / (1 + math.exp(-self.D)))
result = self.L - (self.L / (1 + np.exp(-self.D)))
else:
if (1 + (1 - self.q)*self.D) < 0:
self.D = -1 / self.D
result = self.L / math.pow(1 + (1 - self.q)*self.D, 1 / (1 - self.q))

if result <= 0:
result = 0
result = self.L / np.power(1 + (1 - self.q)*self.D, 1 / (1 - self.q))

if result == float('inf'):
result = self.L
# If result is < 0 or infinity then we must set to 0 or L value.
if type(result) == np.ndarray:
result[result < 0] = 0
result[result == float('inf')] = self.L
elif result == int or result == float:
result = 0 if result < 0 else result
result = self.L if result == float('inf') else result

return [result, self.D]
return result


class qWeibull:
Expand All @@ -102,7 +123,7 @@ def __init__(self, x, q, lamb, k):

def calc(self):
if self.x >= 0:
return (2 - self.q)*(self.k/self.lamb)*math.pow(self.x/self.lamb, self.k - 1) * \
qExponential(x=-math.pow(self.x/self.lamb, self.k), q=self.q).calc()
return (2 - self.q)*(self.k/self.lamb)*np.power(self.x/self.lamb, self.k - 1) * \
qExponential(x=-np.power(self.x/self.lamb, self.k), q=self.q).calc()
else:
return 0
3 changes: 1 addition & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ flake8==3.4.1
tox==2.7.0
coverage==4.4.1
Sphinx==1.6.3


numpy==1.13.1
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.1.0.1
current_version = 0.1.0.2
commit = True
tag = True

Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

requirements = [
# TODO: put package requirements here
'numpy==1.13.1'
]

setup_requirements = [
Expand All @@ -21,11 +22,12 @@

test_requirements = [
# TODO: put package test requirements here
'numpy==1.13.1'
]

setup(
name='qstatistic',
version='0.1.0.1',
version='0.1.0.2',
description="qstatistic Library.",
long_description=readme + '\n\n' + history,
author="Yan Anderson Siriano Duarte",
Expand Down
48 changes: 46 additions & 2 deletions tests/test_qstatistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@


import unittest

from qstatistic import qstatistic
import numpy as np
from qstatistic import qstatistic as qs


class TestQstatistic(unittest.TestCase):
Expand All @@ -20,3 +20,47 @@ def tearDown(self):

def test_000_something(self):
"""Test something."""

def test_qSigmoid_point(self):
result = qs.qSigmoid(
L=255,
I=128,
B=128,
a=15,
q=0.35
).calc()

self.assertEqual(result, 254.97450210358875)

def test_qSigmoid_1d(self):
array = np.round(np.random.rand(10)*255)
L = np.max(array)
B = L/2
a = (L-B)/4
q = 0.35

result = qs.qSigmoid(L=L, I=array, B=B, a=a, q=q).calc()

for idx in range(array.shape[0]):
self.assertEqual(result[idx], qs.qSigmoid(L=L, I=array[idx], B=B, a=a, q=q).calc())

self.assertEqual(array.shape, result.shape)

def test_qSigmoid_2d(self):
array = np.round(np.random.rand(10, 10)*255)
L = np.max(array)
B = L/2
a = (L-B)/4
q = 0.35

result = qs.qSigmoid(L=L, I=array, B=B, a=a, q=q).calc()

print(result.shape)

for lin in range(array.shape[0]):
for col in range(array.shape[1]):
self.assertEqual(result[lin, col], qs.qSigmoid(L=L, I=array[lin, col], B=B, a=a, q=q).calc())

self.assertEqual(array.shape, result.shape)

print(result.shape)

0 comments on commit c8e1fb3

Please sign in to comment.