Tidy up these figure generators
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
from ulab import numpy as np
|
||||
import random
|
||||
import msgpack
|
||||
from io import BytesIO
|
||||
|
||||
data = np.array([[random.uniform(-200, 200), random.uniform(-200, 200), random.uniform(0, 360)] for i in range(20)], dtype=np.int16)
|
||||
print(data)
|
||||
print(data.shape)
|
||||
buffer = BytesIO()
|
||||
msgpack.pack({"offset": 10, "poses": data.tolist()}, buffer)
|
||||
buffer.seek(0)
|
||||
print(len(buffer.getvalue())) # json is 618 bytes, msgpack is 595 bytes. msgpack is 3.7% smaller
|
||||
print(buffer.getvalue())
|
||||
|
||||
import json
|
||||
as_int = np.array(data, dtype=np.int16)
|
||||
as_int_json = json.dumps({"offset": 10, "poses": as_int.tolist()})
|
||||
|
||||
|
||||
## on pc
|
||||
import msgpack
|
||||
import numpy as np
|
||||
raw_data = "bytes from robot"
|
||||
data = msgpack.unpackb(raw_data)
|
||||
poses = np.array(data["poses"])
|
||||
print(poses)
|
||||
# Avoiding the text could make it smaller.
|
||||
# From https://learn.adafruit.com/introducing-the-adafruit-bluefruit-le-uart-friend/hardware
|
||||
# Note that we do not recommend using higher baudrates than 9600 because the nRF51 UART can drop characters!
|
||||
# Is the complexity of the msgpack worth the 3.7% size reduction?
|
||||
|
||||
@@ -7,8 +7,13 @@ population_size = 100000
|
||||
|
||||
def make_uniform_series_plot(n):
|
||||
uniform_series = np.array(
|
||||
[sum(random.uniform(0, 1) for _ in range(n))/n for _ in range(population_size)])
|
||||
plt.hist(uniform_series, bins=200, histtype='step', label=f"n={n}")
|
||||
[
|
||||
sum(random.uniform(0, 1) for _ in range(n)) / n
|
||||
for _ in range(population_size)
|
||||
]
|
||||
)
|
||||
plt.hist(uniform_series, bins=200, histtype="step", label=f"n={n}")
|
||||
|
||||
|
||||
make_uniform_series_plot(1)
|
||||
make_uniform_series_plot(2)
|
||||
|
||||
@@ -5,6 +5,7 @@ distribution_size = (2, 500)
|
||||
uniform_dist = np.random.uniform(low=-1.5, high=1.5, size=distribution_size)
|
||||
gauss_dist = np.random.normal(loc=0.0, scale=0.5, size=distribution_size)
|
||||
|
||||
|
||||
def prepare_scatter(ax, data):
|
||||
ax.set_xlim(-1.5, 1.5)
|
||||
ax.set_ylim(-1.5, 1.5)
|
||||
@@ -13,21 +14,22 @@ def prepare_scatter(ax, data):
|
||||
data_sd = data.std(1)
|
||||
data_mean = data.mean(1)
|
||||
print(data_mean, data_sd)
|
||||
circle = plt.Circle(data_mean, data_sd[0], color='r', fc=(1,0.6,0.6,0.3))
|
||||
circle = plt.Circle(data_mean, data_sd[0], color="r", fc=(1, 0.6, 0.6, 0.3))
|
||||
ax.add_patch(circle)
|
||||
|
||||
|
||||
fig = plt.figure()
|
||||
gs = fig.add_gridspec(2, 2)
|
||||
uniform_scatter = fig.add_subplot(gs[0, 0])
|
||||
gauss_scatter = fig.add_subplot(gs[0, 1])
|
||||
uniform_hist = fig.add_subplot(gs[1, 0], sharex = uniform_scatter)
|
||||
gauss_hist = fig.add_subplot(gs[1, 1], sharex = gauss_scatter)
|
||||
uniform_hist = fig.add_subplot(gs[1, 0], sharex=uniform_scatter)
|
||||
gauss_hist = fig.add_subplot(gs[1, 1], sharex=gauss_scatter)
|
||||
# fig, ((uniform_scatter, gauss_scatter), (uniform_hist, gauss_hist)) = plt.subplots(2, 2)
|
||||
prepare_scatter(uniform_scatter, uniform_dist)
|
||||
prepare_scatter(gauss_scatter, gauss_dist)
|
||||
|
||||
# Plot the histograms
|
||||
bins=16
|
||||
bins = 16
|
||||
uniform_hist.hist(uniform_dist[0], bins=bins)
|
||||
gauss_hist.hist(gauss_dist[0], bins=bins)
|
||||
# fig.tight_layout()
|
||||
|
||||
Reference in New Issue
Block a user