Skip to content

Commit

Permalink
new temp inference algo
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed May 9, 2024
1 parent 4036523 commit 236dfc1
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 76 deletions.
4 changes: 2 additions & 2 deletions pioreactor/background_jobs/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import subprocess
from contextlib import suppress
from shlex import join # https://docs.python.org/3/library/shlex.html#shlex.quote
from shlex import quote # https://docs.python.org/3/library/shlex.html#shlex.quote
from shlex import join
from shlex import quote
from threading import Thread
from time import sleep
from typing import Any
Expand Down
55 changes: 28 additions & 27 deletions pioreactor/background_jobs/temperature_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,42 +567,43 @@ def approximate_temperature_2_0(features: dict[str, Any]) -> float:
if features["previous_heater_dc"] == 0:
return features["time_series_of_temp"][-1]

X = features["time_series_of_temp"]
X = [features["previous_heater_dc"]] + features["time_series_of_temp"]

# normalize to ~1.0, as we do this in training.
X = [x / 35.0 for x in X]

# add in non-linear features
X.append(X[0] ** 2)
X.append(X[0] ** 0.5)
X.append(X[1] ** 2)
X.append(X[20] * X[0])

coefs = [
22.44950549,
-47.26707134,
-31.07494909,
-41.36629363,
-12.17348368,
55.03067504,
22.91692162,
35.96733783,
37.86185926,
49.00325047,
43.72159562,
47.12556322,
30.780167,
34.25574632,
21.12142635,
1.77795003,
-0.74499359,
-46.5411805,
-38.20418079,
-59.83658558,
-65.3550903,
-3.36141664,
-34.61894608,
-1.37221255e01,
1.50807347e02,
1.52808570e01,
-7.17124615e01,
-8.15352596e01,
-5.82053398e01,
-8.49915201e01,
-3.69729300e01,
-8.51994806e-02,
1.12635670e01,
3.37434235e01,
3.36348041e01,
4.25731033e01,
6.72551219e01,
8.37883314e01,
6.29508694e01,
4.95735854e01,
1.86594862e01,
3.12848519e-01,
-3.82815596e01,
-5.62834504e01,
-9.27840943e01,
-7.62113224e00,
8.18877406e00,
]

intercept = 13.358217809582857
intercept = -6.171633633597331

def dot_product(vec1: list, vec2: list) -> float:
if len(vec1) != len(vec2):
Expand Down
184 changes: 137 additions & 47 deletions pioreactor/tests/test_temperature_approximation_2_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,70 +11,160 @@ def setup_class(self):
def test_temperature_approximation_if_constant(self) -> None:
# TODO: we should add some constants like this to the dataset.

for temp in range(20, 45):
for temp in range(25, 35):
features = {
"room_temp": 22.0,
"previous_heater_dc": 10, # should be nonzero to not short circuit the if previous_heater_dc == 0 line.
"previous_heater_dc": 0.001, # should be nonzero to not short circuit the if previous_heater_dc == 0 line.
"time_series_of_temp": 21 * [float(temp)],
}
assert abs(temp - self.t.approximate_temperature_2_0(features)) < 0.30

def test_temperature_approximation1(self) -> None:
features = {
"previous_heater_dc": 11.52,
"previous_heater_dc": 45.57,
"room_temp": 22.0,
"time_series_of_temp": [
32.989583333333336,
32.520833333333336,
32.21875,
31.9375,
31.75,
31.5625,
31.385416666666668,
31.25,
31.135416666666668,
31.010416666666668,
30.9375,
30.875,
30.802083333333332,
30.71875,
30.6875,
30.625,
30.5625,
30.510416666666668,
30.5,
30.4375,
30.416666666666668,
48.427083333333336,
46.520833333333336,
45.208333333333336,
44.135416666666664,
43.270833333333336,
42.552083333333336,
41.9375,
41.416666666666664,
40.958333333333336,
40.5625,
40.21875,
39.9375,
39.625,
39.427083333333336,
39.1875,
39.0,
38.8125,
38.625,
38.5,
38.3125,
38.1875,
],
}
assert 29.9 <= self.t.approximate_temperature_2_0(features) <= 30.1
assert abs(38.9 - self.t.approximate_temperature_2_0(features)) < 0.5

def test_temperature_approximation2(self) -> None:
features = {
"previous_heater_dc": 12.14,
"previous_heater_dc": 12.53,
"room_temp": 22.0,
"time_series_of_temp": [
35.385416666666664,
34.864583333333336,
34.510416666666664,
34.166666666666664,
33.84375,
33.65625,
33.395833333333336,
33.21875,
33.010416666666664,
32.885416666666664,
32.770833333333336,
32.614583333333336,
32.489583333333336,
32.364583333333336,
32.229166666666664,
32.177083333333336,
32.114583333333336,
32.03125,
31.979166666666668,
31.927083333333332,
31.822916666666668,
],
}
assert abs(31.85 - self.t.approximate_temperature_2_0(features)) < 0.5

def test_temperature_approximation3(self) -> None:
features = {
"previous_heater_dc": 46.12,
"room_temp": 22.0,
"time_series_of_temp": [
48.552083333333336,
46.604166666666664,
45.270833333333336,
44.208333333333336,
43.322916666666664,
42.59375,
41.96875,
41.4375,
41.0,
40.604166666666664,
40.25,
39.9375,
39.677083333333336,
39.4375,
39.1875,
39.0,
38.8125,
38.625,
38.5,
38.3125,
38.1875,
],
}
assert abs(38.94220102733567 - self.t.approximate_temperature_2_0(features)) < 0.5

def test_temperature_approximation4(self) -> None:
features = {
"previous_heater_dc": 27.31,
"room_temp": 22.0,
"time_series_of_temp": [
42.28125,
41.1875,
40.322916666666664,
39.614583333333336,
39.010416666666664,
38.46875,
37.989583333333336,
37.583333333333336,
37.208333333333336,
36.84375,
36.59375,
36.291666666666664,
36.0625,
35.791666666666664,
35.583333333333336,
35.385416666666664,
35.197916666666664,
35.041666666666664,
34.885416666666664,
34.75,
34.625,
],
}
assert abs(34.1204 - self.t.approximate_temperature_2_0(features)) < 0.5

def test_temperature_approximation5(self) -> None:
features = {
"previous_heater_dc": 14.791,
"room_temp": 22.0,
"time_series_of_temp": [
33.125,
32.666666666666664,
32.3125,
32.0625,
31.8125,
34.75,
34.1875,
33.760416666666664,
33.4375,
33.1875,
32.9375,
32.739583333333336,
32.5625,
32.385416666666664,
32.25,
32.125,
32.020833333333336,
31.9375,
31.822916666666668,
31.75,
31.6875,
31.625,
31.4375,
31.3125,
31.1875,
31.0625,
30.979166666666668,
30.875,
30.8125,
30.75,
30.6875,
30.625,
30.5625,
30.510416666666668,
30.479166666666668,
30.4375,
30.40625,
31.5625,
31.5,
31.447916666666668,
31.395833333333332,
],
}
assert 29.9 <= self.t.approximate_temperature_2_0(features) <= 30.1
assert abs(30.94587 - self.t.approximate_temperature_2_0(features)) < 0.5

0 comments on commit 236dfc1

Please sign in to comment.