check_numberdealers/plot_numberdealers.py

96 lines
2.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from datetime import timezone, datetime
2024-04-25 21:31:47 +02:00
from numberdealers import parse_numberdealers
import matplotlib.pyplot as plt
import numpy as np
def xy(messages):
x = []
y = []
for msg in messages:
2024-04-25 21:31:47 +02:00
# x.append(datetime.fromtimestamp(msg.create_at / 1000, timezone.utc))
x.append(msg.create_at / 1000)
y.append(msg.recognized_number)
return x, y
def find(xs, condition):
for i, x in enumerate(xs):
if condition(x):
return i
2024-04-25 21:31:47 +02:00
def abline(slope, intercept, point1_x, point2_x):
"""Plot a line from slope and intercept"""
axes = plt.gca()
2024-04-25 21:31:47 +02:00
x_vals = np.array([point1_x, point2_x])
y_vals = slope * x_vals + intercept
2024-04-25 21:31:47 +02:00
plt.plot(x_vals, y_vals, linewidth=1, color='grey')
def main():
import sys
2024-04-25 21:31:47 +02:00
if len(sys.argv) <= 2:
print("Usage: plot_numberdealers.py <numberdealers.json> <numberdealers-ng.json>")
return 1
with open(sys.argv[1], encoding="utf-8") as fh:
2024-11-17 23:05:03 +01:00
numbers_og, _errors = parse_numberdealers.parse(fh, parse_numberdealers.ErrorStyle.NUMBERDEALERS)
with open(sys.argv[2], encoding="utf-8") as fh:
2024-11-17 23:05:03 +01:00
numbers_ng, _errors = parse_numberdealers.parse(fh, parse_numberdealers.ErrorStyle.NUMBERDEALERS_NG)
2024-11-17 23:05:03 +01:00
start_at_numberdealers_ng_number = 1
start_of_current_slope = find(numbers_og, lambda msg: msg.create_at >= numbers_ng[start_at_numberdealers_ng_number].create_at)
2024-04-25 21:31:47 +02:00
# a = (y2-y1) / (x2-x1)
slope_og = (
numbers_og[-1].recognized_number - numbers_og[start_of_current_slope].recognized_number
) / (
numbers_og[-1].create_at/1000 - numbers_og[start_of_current_slope].create_at/1000
)
# b = y - a x
intercept_og = numbers_og[-1].recognized_number - slope_og * numbers_og[-1].create_at/1000
slope_ng = (
2024-11-17 23:05:03 +01:00
numbers_ng[-1].recognized_number - numbers_ng[start_at_numberdealers_ng_number].recognized_number
2024-04-25 21:31:47 +02:00
) / (
2024-11-17 23:05:03 +01:00
numbers_ng[-1].create_at/1000 - numbers_ng[start_at_numberdealers_ng_number].create_at/1000
2024-04-25 21:31:47 +02:00
)
intercept_ng = numbers_ng[-1].recognized_number - slope_ng * numbers_ng[-1].create_at/1000
# a1 x + b1 = a2 x + b2
# (a1 - a2) x = b2 - b1
# x = (b2 - b1) / (a1 - a2)
intercept_x = (intercept_ng - intercept_og) / (slope_og - slope_ng)
intercept_y = slope_ng * intercept_x + intercept_ng
fig, ax = plt.subplots(figsize=(15, 6))
ax.plot(*xy(numbers_og))
ax.plot(*xy(numbers_ng))
2024-04-25 21:31:47 +02:00
abline(slope_og, intercept_og, numbers_og[-1].create_at/1000, intercept_x)
abline(slope_ng, intercept_ng, numbers_ng[-1].create_at/1000, intercept_x)
plt.margins(x=0)
locs = np.append(
np.linspace(numbers_og[0].create_at/1000, numbers_ng[0].create_at/1000, 6),
[numbers_ng[-1].create_at/1000, intercept_x]
)
# func = lambda x: x < intercept_x
# locs = np.append(locs[func(locs)][:-1], [intercept_x])
labels = ["{:%Y-%m-%d}".format(datetime.fromtimestamp(x, timezone.utc)) for x in locs]
2024-11-17 23:05:03 +01:00
labels[-2] = ""
2024-04-25 21:31:47 +02:00
plt.xticks(locs, labels)
plt.grid()
# plt.show()
plt.savefig("plot.png")
return 0
if __name__ == "__main__":
2024-04-25 21:31:47 +02:00
import sys
sys.exit(main())