Skip to content

Commit

Permalink
Bug Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenang committed Jan 7, 2018
1 parent f924c65 commit 2f1e196
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 7 deletions.
19 changes: 16 additions & 3 deletions RandomExcursions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ def random_excursions_test(binary_data:str, verbose=False, state=1):
count += 1
print('DEBUG END.')

states = ['-4', '-3', '-2', '-1', '+1', '+2', '+3', '+4',]
result = []
count = 0
for item in p_values:
result.append((x_values[count], xObs[count], item, (item >= 0.01)))
result.append((states[count], x_values[count], xObs[count], item, (item >= 0.01)))
count += 1

return result
Expand All @@ -113,13 +114,16 @@ def variant_test(binary_data:str, verbose=False):
cumulative_sum = cumsum(sum_int)

li_data = []
index = []
for count in sorted(set(cumulative_sum)):
if abs(count) <= 9:
index.append(count)
li_data.append([count, len(where(cumulative_sum == count)[0])])

j = RandomExcursions.get_frequency(li_data, 0) + 1

p_values = []
for count in range(-9, 9 + 1):
for count in (sorted(set(index))):
if not count == 0:
den = sqrt(2 * j * (4 * abs(count) - 2))
p_values.append(erfc(abs(RandomExcursions.get_frequency(li_data, count) - j) / den))
Expand All @@ -129,6 +133,7 @@ def variant_test(binary_data:str, verbose=False):
for data in li_data:
if data[0] == 0:
li_data.remove(data)
index.remove(0)
break
count += 1

Expand All @@ -144,10 +149,18 @@ def variant_test(binary_data:str, verbose=False):
count += 1
print('DEBUG END.')


states = []
for item in index:
if item < 0:
states.append(str(item))
else:
states.append('+' + str(item))

result = []
count = 0
for item in p_values:
result.append((li_data[count][0], li_data[count][1], item, (item >= 0.01)))
result.append((states[count], li_data[count][0], li_data[count][1], item, (item >= 0.01)))
count += 1

return result
Expand Down
9 changes: 9 additions & 0 deletions Tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Tools:

@staticmethod
def string_to_binary(input:str):
binary = []
for char in input:
binary.append(bin(ord(char))[2:])

return ''.join(binary)
8 changes: 4 additions & 4 deletions test_e.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
print('\t\t STATE \t\t\t xObs \t\t\t\t P-Value \t\t\t Conclusion')

for item in result:
print('\t\t', repr(item[0]).rjust(4), '\t\t', item[1], '\t\t', repr(item[2]).ljust(14), '\t\t',
(item[3] >= 0.01))
print('\t\t', repr(item[0]).rjust(4), '\t\t', item[2], '\t\t', repr(item[3]).ljust(14), '\t\t',
(item[4] >= 0.01))

result = RandomExcursions.variant_test(binary_data[:1000000])

print('2.15. Random Excursion Variant Test:\t\t\t\t\t\t')
print('\t\t STATE \t\t COUNTS \t\t\t P-Value \t\t Conclusion')
for item in result:
print('\t\t', repr(item[0]).rjust(4), '\t\t', item[1], '\t\t', repr(item[2]).ljust(14), '\t\t',
(item[3] >= 0.01))
print('\t\t', repr(item[0]).rjust(4), '\t\t', item[2], '\t\t', repr(item[3]).ljust(14), '\t\t',
(item[4] >= 0.01))
53 changes: 53 additions & 0 deletions test_url_01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
from Tools import Tools

from FrequencyTest import FrequencyTest as ft
from RunTest import RunTest as rt
from Matrix import Matrix as mt
from Spectral import SpectralTest as st
from TemplateMatching import TemplateMatching as tm
from Universal import Universal as ut
from Complexity import ComplexityTest as ct
from Serial import Serial as serial
from ApproximateEntropy import ApproximateEntropy as aet
from CumulativeSum import CumulativeSums as cst
from RandomExcursions import RandomExcursions as ret

test_type = ['01. Frequency Test (Monobit)', '02. Frequency Test within a Block', '03. Run Test',
'04. Longest Run of Ones in a Block', '05. Binary Matrix Rank Test',
'06. Discrete Fourier Transform (Spectral) Test',
'07. Non-Overlapping Template Matching Test',
'08. Overlapping Template Matching Test', '09. Maurer\'s Universal Statistical test',
'10. Linear Complexity Test', '11. Serial test', '12. Approximate Entropy Test',
'13. Cummulative Sums (Forward) Test', '14. Cummulative Sums (Reverse) Test',
'15. Random Excursions Test', '16. Random Excursions Variant Test']

test_function = {
0:ft.monobit_test,
1:ft.block_frequency,
2:rt.run_test,
3:rt.longest_one_block_test,
4:mt.binary_matrix_rank_text,
5:st.sepctral_test,
6:tm.non_overlapping_test,
7:tm.overlapping_patterns,
8:ut.statistical_test,
9:ct.linear_complexity_test,
10:serial.serial_test,
11:aet.approximate_entropy_test,
12:cst.cumulative_sums_test,
13:cst.cumulative_sums_test,
14:ret.random_excursions_test,
15:ret.variant_test
}

handle = open(os.path.join(os.getcwd(), 'data', 'test_data_01.txt'))
count = 0

for item in handle:
binary = Tools.string_to_binary(item)
print(item, Tools.string_to_binary(item), binary)
count = 0
for test in test_function:
print(test_type[count%len(test_type)], test_function[count](binary))
count += 1

0 comments on commit 2f1e196

Please sign in to comment.