<jupyter_start><jupyter_text>Bottle Client Simulator<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
from scipy import signal
from datetime import datetime, timedelta<jupyter_output><empty_output><jupyter_text>1. Generating basic waveLet's simulate reading every 5 minutes for 24 hours, with `t` in seconds.<jupyter_code>MAX_TIME = 24*60*60
t = np.linspace(0, MAX_TIME, MAX_TIME//(5*60))
len(t)
def basic_wave(t, consumption):
    return (signal.square(consumption * (2 * np.pi * t) / MAX_TIME)+1) / 2
plt.plot(t, basic_wave(t, 3), '+')
plt.ylim(-0.1, 1.1)<jupyter_output><empty_output><jupyter_text>2. Introducing noise<jupyter_code>def noisy_wave(t, consumption, noise=0.05):
    return basic_wave(t, consumption) + np.random.uniform(-noise, noise, t.shape)
plt.plot(t, noisy_wave(t, 3), '+')
plt.ylim(-0.1, 1.1)<jupyter_output><empty_output><jupyter_text>3. Introducing random spikes First lets try to generate just one spike:<jupyter_code>def gauss(x, mu, sigma):
    return np.exp(-(x-mu)**2/(2.*sigma**2))
def gauss_spike(x):
    return gauss(x, np.random.uniform(0, MAX_TIME), MAX_TIME / np.random.uniform(20,200))
plt.plot(t, gauss_spike(t))
plt.ylim(-0.1, 1.1)<jupyter_output><empty_output><jupyter_text>Now, generate n-spikes (`n` random):<jupyter_code>def n_gauss_spikes(x):
    n = np.random.randint(4,12)
    result = np.zeros_like(x)
    for i in range(n):
        result += gauss_spike(x)
    return result
plt.plot(t, n_gauss_spikes(t))
plt.ylim(-0.1, 1.1)
def n_gauss_spikes_noise(x):
    return n_gauss_spikes(x) * np.random.uniform(-1, 1, x.shape)
plt.plot(t, n_gauss_spikes_noise(t), '+')
plt.ylim(-1.1, 1.1)<jupyter_output><empty_output><jupyter_text>Now let's introduce those spikes into our original wave:<jupyter_code>def spikey_wave(t, consumption, noise=0.05):
    return noisy_wave(t, consumption, noise=noise) + n_gauss_spikes_noise(t)
w = spikey_wave(t, 3)
fig, ax = plt.subplots(figsize=(8, 5), dpi=100)

ax.plot(t, w, '+')
ax.set(xlabel='Timestamp (Seconds)', ylabel='Weight Reading (Kg, equivalent to L)', title='Example of simulated weight data for 3L consumption over 24 hours')
ax.grid()

fig.savefig("plots/generated-weight-data.png")
plt.show()<jupyter_output><empty_output><jupyter_text>4. Generating temperatures<jupyter_code>def gen_temps(t, avg_temp=20, noise=0.05):
    return np.sin(2 * np.pi * t / MAX_TIME)*2 + np.sin(3 * np.pi * t / MAX_TIME)/2 + avg_temp + np.random.uniform(-noise, noise, t.shape)
plt.plot(t, gen_temps(t), '-')<jupyter_output><empty_output><jupyter_text>5. Final generator function<jupyter_code>def gen_data(max_time=24*60*60, sample_rate=5, consumption=3, noise=0.05, temperature=20):
    start_time = datetime.timestamp(datetime.now() - timedelta(seconds=max_time))
    
    t = np.linspace(0, max_time, max_time//(sample_rate*60))
    w = spikey_wave(t, consumption, noise=noise)
    temp = gen_temps(t, avg_temp=temperature, noise=noise)
 
    return t+start_time, w, temp
def gen_batches(data, batch_size):
    t, w, temp = data
    
    for i in range(0, len(t), batch_size):
        yield t[i:i+batch_size], w[i:i+batch_size], temp[i:i+batch_size]
data = gen_data(consumption=3, temperature=20)
plt.plot(data[0],data[1],'+')
for t_batch, w_batch, temp_batch in gen_batches(data, 20):
    payload = {
        'data': [
            {
                'time': t_batch[i],
                'weight': w_batch[i],
                'temp': temp_batch[i]
            } for i in range(len(t_batch))
        ]
    }
    import requests, json
    r = requests.post("http://localhost:8000/api/bottleUpdate?format=json", json=payload)
    resp = json.loads(r.text)
    print(resp)<jupyter_output>{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'status': 'updated', 'n_errors': 0}
{'fraction_non_alphanumeric': 0.17357328913094955, 'fraction_numerical': 0.02936301207672271, 'mean_word_length': 5.068965517241379, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '418272', 'license': 'MIT', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1635, 'n_words': 335, 'path': 'experiments/Bottle Client.ipynb', 'repo_name': 'duartegalvao/Smart-Bottle', 'size': '4223'}