Advertisement
100hahahaha

Untitled

Nov 15th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.21 KB | None | 0 0
  1. import os
  2. import random
  3. import dearpygui.dearpygui as dpg
  4. import numpy as np
  5. #import win32con
  6. #import win32console
  7. #import win32gui
  8.  
  9.  
  10. sequence = []
  11.  
  12. # Проверка, что скрипт выполняется на Windows
  13. if os.name != "nt":
  14.     input("This script only works on Windows.")
  15.     os._exit(0)
  16.  
  17. # Скрытие консольного окна
  18. # win32gui.ShowWindow(win32console.GetConsoleWindow(), win32con.SW_HIDE)
  19.  
  20. # Параметры UI
  21. uiWidth = 800
  22. uiHeight = 600
  23.  
  24. # Инициализация Dear PyGui
  25. dpg.create_context()
  26.  
  27. bar_width = 70
  28. dpg_progress = 0.0  # динамический индикатор прогресса
  29.  
  30.  
  31.  
  32.  
  33.  
  34. def generateBits():
  35.     global sequence, dpg_progress, length
  36.     result = ""
  37.     input_value = dpg.get_value("input_text")
  38.     if not input_value:
  39.         result = "Sequence can't be empty"
  40.         dpg.set_value("status_text", result)
  41.         return
  42.     try:
  43.         length = int(input_value)
  44.         if length > 0:
  45.             sequence = [random.randint(0, 1) for i in range(length)]
  46.             dpg_progress = 0.0
  47.             for i in range(length):
  48.                 dpg_progress = (i + 1) / length
  49.  
  50.             result += "New sequence created"
  51.             with open("numbers.txt", "w") as file:
  52.                 file.write(str(sequence))
  53.             dpg.set_value("status_text", result)
  54.         else:
  55.             result = "Length of sequence should be more than 0"
  56.             dpg.set_value("status_text", result)
  57.     except ValueError:
  58.         result = "Not correct sequence length"
  59.         dpg.set_value("status_text", result)
  60.  
  61.  
  62. def test_sequence(rel):
  63.     result = ""
  64.     if not all(bit in (0, 1) for bit in sequence):
  65.         raise ValueError("Последовательность должна состоять только из 0 и 1.")
  66.  
  67.     n = len(sequence)
  68.     X = [2 * bit - 1 for bit in sequence]
  69.     Sn = sum(X)
  70.     S = abs(Sn) / (n ** 0.5)
  71.     result += f"S: {str(S)} \n"
  72.     if S <= 1.82138636:
  73.         result += "S <= 1.82138636 \n"
  74.         print("Последовательность прошла тест на случайность.")
  75.         result += "The sequence passed the randomness test."
  76.         dpg.set_value("status_text", result)
  77.         dpg.set_value("creating_status", '')
  78.  
  79.     else:
  80.         result += "S > 1.82138636 \n"
  81.         print("Последовательность не прошла тест на случайность.")
  82.         result += "The sequence not passed the randomness test."
  83.         dpg.set_value("status_text", result)
  84.         dpg.set_value("creating_status", '')
  85.  
  86.  
  87. def readBits():
  88.     global sequence
  89.     with open("numbers.txt", "r") as file:
  90.         for line in file:
  91.             sequence = line
  92.         sequence = list(map(int, sequence[1:-1].split(', ')))
  93.     return sequence
  94.  
  95.  
  96. rel = readBits()
  97.  
  98.  
  99. def test_identical_bits_sequence(rel):
  100.     result = ""
  101.     if not all(bit in (0, 1) for bit in sequence):
  102.         raise ValueError("Последовательность должна состоять только из 0 и 1.")
  103.  
  104.     n = len(sequence)
  105.     pi = sum(sequence) / n
  106.     Vn = 1
  107.     r = 0
  108.  
  109.     for k in range(n - 1):
  110.         if sequence[k] != sequence[k + 1]:
  111.             r += 1
  112.     Vn += r
  113.  
  114.     S = abs(Vn - 2 * n * pi * (1 - pi)) / (2 * (2 ** 0.5) * n * pi * (1 - pi) ** 0.5)
  115.     result += f"S: {str(S)} \n"
  116.     if S <= 1.82138636:
  117.         result += "S <= 1.82138636 \n"
  118.         result += "The sequence passed the test on identical bits sequence."
  119.         dpg.set_value("status_text", result)
  120.         dpg.set_value("creating_status", '')
  121.  
  122.     else:
  123.         result += "S > 1.82138636 \n"
  124.         result += "The sequence not passed the test on identical bits sequence."
  125.         dpg.set_value("status_text", result)
  126.         dpg.set_value("creating_status", '')
  127.  
  128.  
  129. def extended_deviation_test(rel):
  130.     result = ""
  131.     x = 2 * np.array(sequence) - 1
  132.     s = np.cumsum(x)
  133.     s_prime = np.concatenate(([0], s, [0]))
  134.     l = np.sum(s_prime == 0) - 1
  135.  
  136.     counts = np.zeros(19)
  137.     for i in range(len(s_prime)):
  138.         state = int(s_prime[i])
  139.         if -9 <= state <= 9:
  140.             counts[state + 9] += 1
  141.  
  142.     y = np.zeros(18)
  143.     for j in range(18):
  144.         if 2 * l * (4 * np.abs(j - 9) - 2) > 0:
  145.             y[j] = np.abs(counts[j] - l) / np.sqrt(2 * l * (4 * np.abs(j - 9) - 2))
  146.         else:
  147.             y[j] = 0
  148.  
  149.     result += "Extended test for random deviations: \n"
  150.     for j in range(-9, 9):
  151.         result += f"State {j}: appears {counts[j + 9]} times, Statistic: {y[j + 9]:.4f} \n"
  152.     states_not_passing = np.where(y > 1.82138636)[0] - 9
  153.     states_not_passing_str = ', '.join([str(state) for state in states_not_passing])
  154.     states_list = [state for state in states_not_passing_str.split(', ') if state]
  155.  
  156.     if np.all(y <= 1.82138636):
  157.         result += "The sequence passed extended deviations test."
  158.         dpg.set_value("status_text", result)
  159.         dpg.set_value("creating_status", '')
  160.  
  161.     else:
  162.         result += "The sequence not passed extended deviations test. \n"
  163.         result += f"The following states did not pass the test: {states_list}"
  164.         dpg.set_value("status_text", result)
  165.         dpg.set_value("creating_status", '')
  166.  
  167.  
  168. with dpg.window(label='', width=uiWidth, height=uiHeight, no_collapse=False, no_move=True,
  169.                 no_resize=True, on_close=lambda: os._exit(0)) as window:
  170.     dpg.add_spacer(width=45)
  171.     dpg.add_text("Length of sequence")
  172.     dpg.add_input_text(tag="input_text", default_value='0')
  173.  
  174.     # Создание группы для кнопок
  175.     with dpg.group(horizontal=False):
  176.         dpg.add_button(label="Generate", callback=generateBits)
  177.         dpg.add_spacer(width=150)
  178.         dpg.add_button(label="Test N1", callback=test_sequence)
  179.         dpg.add_button(label="Test N2", callback=test_identical_bits_sequence)
  180.         dpg.add_button(label="Test N3", callback=extended_deviation_test)
  181.         dpg.add_spacer(width=350)
  182.  
  183.     dpg.add_text("", tag="status_text")
  184.     dpg.add_text("", tag="creating_status")
  185.  
  186. dpg.create_viewport(title='Lab1', width=uiWidth, height=uiHeight, decorated=True, resizable=False)
  187. dpg.show_viewport()
  188. dpg.setup_dearpygui()
  189. dpg.start_dearpygui()
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement