Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import random
- import dearpygui.dearpygui as dpg
- import numpy as np
- #import win32con
- #import win32console
- #import win32gui
- sequence = []
- # Проверка, что скрипт выполняется на Windows
- if os.name != "nt":
- input("This script only works on Windows.")
- os._exit(0)
- # Скрытие консольного окна
- # win32gui.ShowWindow(win32console.GetConsoleWindow(), win32con.SW_HIDE)
- # Параметры UI
- uiWidth = 800
- uiHeight = 600
- # Инициализация Dear PyGui
- dpg.create_context()
- bar_width = 70
- dpg_progress = 0.0 # динамический индикатор прогресса
- def generateBits():
- global sequence, dpg_progress, length
- result = ""
- input_value = dpg.get_value("input_text")
- if not input_value:
- result = "Sequence can't be empty"
- dpg.set_value("status_text", result)
- return
- try:
- length = int(input_value)
- if length > 0:
- sequence = [random.randint(0, 1) for i in range(length)]
- dpg_progress = 0.0
- for i in range(length):
- dpg_progress = (i + 1) / length
- result += "New sequence created"
- with open("numbers.txt", "w") as file:
- file.write(str(sequence))
- dpg.set_value("status_text", result)
- else:
- result = "Length of sequence should be more than 0"
- dpg.set_value("status_text", result)
- except ValueError:
- result = "Not correct sequence length"
- dpg.set_value("status_text", result)
- def test_sequence(rel):
- result = ""
- if not all(bit in (0, 1) for bit in sequence):
- raise ValueError("Последовательность должна состоять только из 0 и 1.")
- n = len(sequence)
- X = [2 * bit - 1 for bit in sequence]
- Sn = sum(X)
- S = abs(Sn) / (n ** 0.5)
- result += f"S: {str(S)} \n"
- if S <= 1.82138636:
- result += "S <= 1.82138636 \n"
- print("Последовательность прошла тест на случайность.")
- result += "The sequence passed the randomness test."
- dpg.set_value("status_text", result)
- dpg.set_value("creating_status", '')
- else:
- result += "S > 1.82138636 \n"
- print("Последовательность не прошла тест на случайность.")
- result += "The sequence not passed the randomness test."
- dpg.set_value("status_text", result)
- dpg.set_value("creating_status", '')
- def readBits():
- global sequence
- with open("numbers.txt", "r") as file:
- for line in file:
- sequence = line
- sequence = list(map(int, sequence[1:-1].split(', ')))
- return sequence
- rel = readBits()
- def test_identical_bits_sequence(rel):
- result = ""
- if not all(bit in (0, 1) for bit in sequence):
- raise ValueError("Последовательность должна состоять только из 0 и 1.")
- n = len(sequence)
- pi = sum(sequence) / n
- Vn = 1
- r = 0
- for k in range(n - 1):
- if sequence[k] != sequence[k + 1]:
- r += 1
- Vn += r
- S = abs(Vn - 2 * n * pi * (1 - pi)) / (2 * (2 ** 0.5) * n * pi * (1 - pi) ** 0.5)
- result += f"S: {str(S)} \n"
- if S <= 1.82138636:
- result += "S <= 1.82138636 \n"
- result += "The sequence passed the test on identical bits sequence."
- dpg.set_value("status_text", result)
- dpg.set_value("creating_status", '')
- else:
- result += "S > 1.82138636 \n"
- result += "The sequence not passed the test on identical bits sequence."
- dpg.set_value("status_text", result)
- dpg.set_value("creating_status", '')
- def extended_deviation_test(rel):
- result = ""
- x = 2 * np.array(sequence) - 1
- s = np.cumsum(x)
- s_prime = np.concatenate(([0], s, [0]))
- l = np.sum(s_prime == 0) - 1
- counts = np.zeros(19)
- for i in range(len(s_prime)):
- state = int(s_prime[i])
- if -9 <= state <= 9:
- counts[state + 9] += 1
- y = np.zeros(18)
- for j in range(18):
- if 2 * l * (4 * np.abs(j - 9) - 2) > 0:
- y[j] = np.abs(counts[j] - l) / np.sqrt(2 * l * (4 * np.abs(j - 9) - 2))
- else:
- y[j] = 0
- result += "Extended test for random deviations: \n"
- for j in range(-9, 9):
- result += f"State {j}: appears {counts[j + 9]} times, Statistic: {y[j + 9]:.4f} \n"
- states_not_passing = np.where(y > 1.82138636)[0] - 9
- states_not_passing_str = ', '.join([str(state) for state in states_not_passing])
- states_list = [state for state in states_not_passing_str.split(', ') if state]
- if np.all(y <= 1.82138636):
- result += "The sequence passed extended deviations test."
- dpg.set_value("status_text", result)
- dpg.set_value("creating_status", '')
- else:
- result += "The sequence not passed extended deviations test. \n"
- result += f"The following states did not pass the test: {states_list}"
- dpg.set_value("status_text", result)
- dpg.set_value("creating_status", '')
- with dpg.window(label='', width=uiWidth, height=uiHeight, no_collapse=False, no_move=True,
- no_resize=True, on_close=lambda: os._exit(0)) as window:
- dpg.add_spacer(width=45)
- dpg.add_text("Length of sequence")
- dpg.add_input_text(tag="input_text", default_value='0')
- # Создание группы для кнопок
- with dpg.group(horizontal=False):
- dpg.add_button(label="Generate", callback=generateBits)
- dpg.add_spacer(width=150)
- dpg.add_button(label="Test N1", callback=test_sequence)
- dpg.add_button(label="Test N2", callback=test_identical_bits_sequence)
- dpg.add_button(label="Test N3", callback=extended_deviation_test)
- dpg.add_spacer(width=350)
- dpg.add_text("", tag="status_text")
- dpg.add_text("", tag="creating_status")
- dpg.create_viewport(title='Lab1', width=uiWidth, height=uiHeight, decorated=True, resizable=False)
- dpg.show_viewport()
- dpg.setup_dearpygui()
- dpg.start_dearpygui()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement