lyagushka/test.py

75 lines
2.5 KiB
Python
Raw Normal View History

2024-04-09 07:47:42 +03:00
from pyagushka import Lyagushka
2024-03-01 13:51:42 +02:00
from randonautentropy import rndo
import json
2024-03-01 15:45:26 +02:00
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d
2024-03-01 13:51:42 +02:00
2024-03-01 15:45:26 +02:00
def generate_random_data(size=1024, max_value=100):
2024-03-01 13:51:42 +02:00
2024-03-01 15:45:26 +02:00
random_data = []
max_value_bytes = (max_value.bit_length() + 7) // 8
max_int_for_bytes = 2**(max_value_bytes * 8) - 1
min_bytes_needed = max_value_bytes * size
mod_cutoff = max_int_for_bytes - (max_int_for_bytes % max_value) - 1
2024-03-01 13:51:42 +02:00
2024-03-01 15:45:26 +02:00
# Populate the 'random_data' array
while len(random_data) < size:
hex_data = rndo.get(length=min_bytes_needed)
hex_chunks = list((hex_data[0+i:2 * max_value_bytes+i] for i in range(0, len(hex_data), 2 * max_value_bytes)))
for i in hex_chunks:
num = int(i, 16)
if num <= mod_cutoff and len(random_data) < size:
random_data.append( num % (max_value + 1) )
2024-03-01 13:51:42 +02:00
2024-03-01 15:45:26 +02:00
return random_data
2024-03-01 18:35:31 +02:00
def filter_by_z_score(data, z_score_threshold):
filtered_data = [item for item in data if item['z_score'] is not None and abs(item['z_score']) >= z_score_threshold]
return filtered_data
2024-03-01 15:45:26 +02:00
# load the random test data
2024-03-01 18:35:31 +02:00
# dataset = []
2024-03-01 15:45:26 +02:00
# with open('random_values.txt', 'r') as file:
# for line in file:
# random_data.append(int(line.strip()))
dataset = generate_random_data(1024, 1024)
dataset.sort()
2024-03-01 18:35:31 +02:00
with open('dataset.json', 'w') as r:
r.write(json.dumps(dataset, indent=4))
2024-03-01 15:45:26 +02:00
# calculate the anomalies in the data
2024-04-09 07:47:42 +03:00
zhaba = Lyagushka(dataset)
analysis_results = json.loads(zhaba.search(4.0, 7))
2024-03-01 18:35:31 +02:00
analysis_results = filter_by_z_score(analysis_results, 1.0)
with open('result.json', 'w') as r:
r.write(json.dumps(analysis_results, indent=4))
2024-03-01 15:45:26 +02:00
# Initialize plot
plt.figure(figsize=(10, 6))
2024-03-01 16:26:32 +02:00
# Color palette for clusters and gaps
colors = plt.cm.jet(np.linspace(0, 1, len(analysis_results)))
2024-03-01 15:45:26 +02:00
2024-03-01 16:26:32 +02:00
# Plot dataset points and assign colors based on cluster membership
for i, result in enumerate(analysis_results):
2024-03-01 15:45:26 +02:00
if result['num_elements'] > 0: # It's a cluster
2024-03-01 18:35:31 +02:00
for point in dataset:
2024-03-01 16:26:32 +02:00
plt.plot(point, 0, 'o', color=colors[i]) # Plot points in cluster with the same color
2024-03-01 15:45:26 +02:00
2024-03-01 16:26:32 +02:00
# Plot a line segment for the cluster/gap Z-score in the same color
2024-03-01 18:35:31 +02:00
start = result['start']
end = result['end']
2024-03-01 15:45:26 +02:00
z_score = result['z_score'] if result['z_score'] is not None else 0
2024-03-01 16:26:32 +02:00
plt.plot([start, end], [z_score, z_score], color=colors[i], linewidth=2)
2024-03-01 15:45:26 +02:00
# Enhancements for visualization
plt.xlabel('Integer Value')
plt.ylabel('Z-Score')
2024-03-01 16:26:32 +02:00
plt.title('Cluster and Gap Analysis')
2024-03-01 15:45:26 +02:00
plt.grid(True)
plt.show()