Skip to content

Commit

Permalink
reverse order of class init args: unit is higher priority
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Sep 6, 2023
1 parent a5559ee commit 38e9a4a
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 57 deletions.
4 changes: 2 additions & 2 deletions pioreactor/actions/leader/experiment_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def execute_experiment_profile(profile_filename: str, dry_run: bool = False) ->
timer.cancel()
logger.info(f"Exiting profile {profile.experiment_profile_name} early.")
else:
logger.info(f"Finished at commands in profile {profile.experiment_profile_name}.")
logger.info(f"Finished executing profile {profile.experiment_profile_name}.")


@click.group(name="experiment_profile")
Expand All @@ -242,6 +242,6 @@ def click_execute_experiment_profile(filename: str, dry_run: bool) -> None:
@click.argument("filename", type=click.Path(exists=True))
def click_verify_experiment_profile(filename: str) -> None:
"""
(leader only) Verify an experiment profile.
(leader only) Verify an experiment profile for correctness.
"""
load_and_verify_profile_file(filename)
33 changes: 23 additions & 10 deletions pioreactor/automations/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

from . import dosing
from . import events
from . import led
from . import temperature
from .dosing.base import DosingAutomationJob
from .dosing.base import DosingAutomationJobContrib
from .led.base import LEDAutomationJob
from .led.base import LEDAutomationJobContrib
from .temperature.base import TemperatureAutomationJob
from .temperature.base import TemperatureAutomationJobContrib
from pioreactor.background_jobs.subjobs import BackgroundSubJob

DISALLOWED_AUTOMATION_NAMES = {
"config",
}


class BaseAutomationJob(BackgroundSubJob):
automation_name = "base_automation_job"

def __init__(self, unit: str, experiment: str):
super(BaseAutomationJob, self).__init__(unit, experiment)

if self.automation_name in DISALLOWED_AUTOMATION_NAMES:
raise NameError(f"{self.automation_name} is not allowed.")

self.add_to_published_settings(
"latest_event",
{
"datatype": "AutomationEvent",
"settable": False,
},
)
2 changes: 1 addition & 1 deletion pioreactor/automations/dosing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def __init__(
),
**kwargs,
) -> None:
super(DosingAutomationJob, self).__init__(unit=unit, experiment=experiment)
super(DosingAutomationJob, self).__init__(unit, experiment)
self.skip_first_run = skip_first_run

self._latest_settings_started_at = current_utc_datetime()
Expand Down
14 changes: 3 additions & 11 deletions pioreactor/automations/led/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from pioreactor import structs
from pioreactor import types as pt
from pioreactor.actions.led_intensity import led_intensity
from pioreactor.automations import BaseAutomationJob
from pioreactor.automations import events
from pioreactor.background_jobs.led_control import LEDController
from pioreactor.background_jobs.subjobs import BackgroundSubJob
from pioreactor.pubsub import QOS
from pioreactor.utils import is_pio_job_running
from pioreactor.utils.timing import current_utc_datetime
Expand All @@ -30,7 +30,7 @@ def brief_pause() -> float:
return d


class LEDAutomationJob(BackgroundSubJob):
class LEDAutomationJob(BaseAutomationJob):
"""
This is the super class that LED automations inherit from. The `run` function will
execute every `duration` minutes (selected at the start of the program), and call the `execute` function
Expand Down Expand Up @@ -79,22 +79,14 @@ def __init__(
skip_first_run: bool = False,
**kwargs,
) -> None:
super(LEDAutomationJob, self).__init__(unit=unit, experiment=experiment)
super(LEDAutomationJob, self).__init__(unit, experiment)

self.skip_first_run = skip_first_run
self._latest_settings_started_at: datetime = current_utc_datetime()
self.latest_normalized_od_at: datetime = current_utc_datetime()
self.latest_growth_rate_at: datetime = current_utc_datetime()
self.edited_channels: set[pt.LedChannel] = set()

self.add_to_published_settings(
"latest_event",
{
"datatype": "AutomationEvent",
"settable": False,
},
)

self.set_duration(duration)

def on_init_to_ready(self):
Expand Down
2 changes: 1 addition & 1 deletion pioreactor/automations/temperature/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(
temperature_control_parent: TemperatureController,
**kwargs,
) -> None:
super(TemperatureAutomationJob, self).__init__(unit=unit, experiment=experiment)
super(TemperatureAutomationJob, self).__init__(unit, experiment)

self.latest_normalized_od_at: datetime = current_utc_datetime()
self.latest_growth_rate_at: datetime = current_utc_datetime()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ def __init__(self, **kwargs) -> None:
self.update_heater(0)

def execute(self) -> NoEvent:
self.update_heater(0)
if self.heater_duty_cycle != 0:
self.update_heater(0)
return NoEvent()
35 changes: 17 additions & 18 deletions pioreactor/background_jobs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
T = t.TypeVar("T")
BJT = t.TypeVar("BJT", bound="_BackgroundJob")

# these are used elsewhere in our software
DISALLOWED_JOB_NAMES = {
"run",
"dosing_events",
"leds",
"led_change_events",
"unit_label",
"pwm",
}


def cast_bytes_to_type(value: bytes, type_: str):
try:
Expand Down Expand Up @@ -90,17 +100,6 @@ def __call__(cls, *args, **kwargs):
return obj


# these are used elsewhere in our software
DISALLOWED_JOB_NAMES = {
"run",
"dosing_events",
"leds",
"led_change_events",
"unit_label",
"pwm",
}


class _BackgroundJob(metaclass=PostInitCaller):

"""
Expand Down Expand Up @@ -249,7 +248,7 @@ class _BackgroundJob(metaclass=PostInitCaller):
# See pt.PublishableSetting type
published_settings: dict[str, pt.PublishableSetting] = dict()

def __init__(self, experiment: str, unit: str, source: str = "app") -> None:
def __init__(self, unit: str, experiment: str, source: str = "app") -> None:
if self.job_name in DISALLOWED_JOB_NAMES:
raise ValueError("Job name not allowed.")
if not self.job_name.islower():
Expand Down Expand Up @@ -958,17 +957,17 @@ class BackgroundJob(_BackgroundJob):
Native jobs should inherit from this class.
"""

def __init__(self, experiment: str, unit: str) -> None:
super().__init__(experiment=experiment, unit=unit, source="app")
def __init__(self, unit: str, experiment: str) -> None:
super().__init__(unit, experiment, source="app")


class BackgroundJobContrib(_BackgroundJob):
"""
Plugin jobs should inherit from this class.
"""

def __init__(self, experiment: str, unit: str, plugin_name: str) -> None:
super().__init__(experiment=experiment, unit=unit, source=plugin_name)
def __init__(self, unit: str, experiment: str, plugin_name: str) -> None:
super().__init__(unit, experiment, source=plugin_name)


class BackgroundJobWithDodging(_BackgroundJob):
Expand Down Expand Up @@ -1165,5 +1164,5 @@ class BackgroundJobWithDodgingContrib(BackgroundJobWithDodging):
Plugin jobs should inherit from this class.
"""

def __init__(self, experiment: str, unit: str, plugin_name: str) -> None:
super().__init__(source=plugin_name, experiment=experiment, unit=unit)
def __init__(self, unit: str, experiment: str, plugin_name: str) -> None:
super().__init__(unit=unit, experiment=experiment, source=plugin_name)
9 changes: 6 additions & 3 deletions pioreactor/background_jobs/leader/mqtt_to_db_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ class MqttToDBStreamer(BackgroundJob):
_inserts_in_last_60s = 0

def __init__(
self, topics_to_tables: list[TopicToParserToTable], unit: str, experiment: str
self,
unit: str,
experiment: str,
topics_to_tables: list[TopicToParserToTable],
) -> None:
super().__init__(experiment=experiment, unit=unit)
super().__init__(unit, experiment)
self.logger.debug(f'Streaming MQTT data to {config["storage"]["database"]}.')
self.sqliteworker = Sqlite3Worker(
config["storage"]["database"], max_queue_size=250, raise_on_error=False
Expand Down Expand Up @@ -459,7 +462,7 @@ def register_source_to_sink(t2p2t: TopicToParserToTable | list[TopicToParserToTa

def start_mqtt_to_db_streaming() -> MqttToDBStreamer:
source_to_sinks = add_default_source_to_sinks()
return MqttToDBStreamer(source_to_sinks, experiment=UNIVERSAL_EXPERIMENT, unit=get_unit_name())
return MqttToDBStreamer(get_unit_name(), UNIVERSAL_EXPERIMENT, source_to_sinks)


@click.command(name="mqtt_to_db_streaming")
Expand Down
2 changes: 1 addition & 1 deletion pioreactor/background_jobs/stirring.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def poll(self, poll_for_seconds: float) -> Optional[structs.MeasuredRPM]:
if self.rpm_calculator is None:
return None

recent_rpm = round(self.rpm_calculator(poll_for_seconds), 1)
recent_rpm = round(self.rpm_calculator(poll_for_seconds), 2)

self._measured_rpm = recent_rpm
self.measured_rpm = structs.MeasuredRPM(
Expand Down
2 changes: 1 addition & 1 deletion pioreactor/background_jobs/temperature_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def infer_temperature(self) -> None:

try:
self.temperature = Temperature(
temperature=self.approximate_temperature(features),
temperature=round(self.approximate_temperature(features), 2),
timestamp=current_utc_datetime(),
)

Expand Down
2 changes: 1 addition & 1 deletion pioreactor/tests/test_dosing_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
from pioreactor import exc
from pioreactor import pubsub
from pioreactor import structs
from pioreactor.automations import DosingAutomationJob
from pioreactor.automations import events
from pioreactor.automations.dosing.base import AltMediaFractionCalculator
from pioreactor.automations.dosing.base import DosingAutomationJob
from pioreactor.automations.dosing.base import VialVolumeCalculator
from pioreactor.automations.dosing.pid_morbidostat import PIDMorbidostat
from pioreactor.automations.dosing.silent import Silent
Expand Down
10 changes: 5 additions & 5 deletions pioreactor/tests/test_mqtt_to_db_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def parse_setting(topic, payload) -> dict:
)
]

with m2db.MqttToDBStreamer(parsers, unit=unit, experiment=exp):
with m2db.MqttToDBStreamer(unit, exp, parsers):
with collect_all_logs_of_level("ERROR", unit, exp) as bucket:
t = TestJob(unit=unit, experiment=exp)
t.clean_up()
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_updated_heater_dc() -> None:
),
]

with m2db.MqttToDBStreamer(parsers, unit=unit, experiment=exp):
with m2db.MqttToDBStreamer(unit, exp, parsers):
sleep(1)
publish(
f"pioreactor/{unit}/test/temperature_automation/latest_event",
Expand Down Expand Up @@ -122,7 +122,7 @@ def test_dosing_events_land_in_db() -> None:
),
]

with m2db.MqttToDBStreamer(parsers, unit=unit, experiment=exp):
with m2db.MqttToDBStreamer(unit, exp, parsers):
from pioreactor.actions.pump import add_media

add_media(
Expand Down Expand Up @@ -197,7 +197,7 @@ def test_kalman_filter_entries() -> None:
)
]

m = m2db.MqttToDBStreamer(parsers, unit=unit, experiment=exp)
m = m2db.MqttToDBStreamer(unit, exp, parsers)

# let data collect
sleep(10)
Expand Down Expand Up @@ -259,7 +259,7 @@ def parse_setting(topic, payload) -> dict:
)
]

with m2db.MqttToDBStreamer(parsers, unit=unit, experiment=exp):
with m2db.MqttToDBStreamer(unit, exp, parsers):
with collect_all_logs_of_level("ERROR", unit, exp) as bucket:
t = TestJob(unit=unit, experiment=exp)
t.clean_up()
Expand Down
4 changes: 3 additions & 1 deletion pioreactor/utils/streaming_calculations.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,6 @@ def publish_pid_stats(self) -> None:
"job_name": self.job_name,
"target_name": self.target_name,
}
self.client.publish(f"pioreactor/{self.unit}/{self.experiment}/pid_log", dumps(to_send))
self.client.publish(
f"pioreactor/{self.unit}/{self.experiment}/pid_log/{self.target_name}", dumps(to_send)
)
2 changes: 1 addition & 1 deletion pioreactor/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

# Append "dev" if a dev version
# Append "rc0" if a rc version
__version__ = "23.8.29"
__version__ = "23.8.29dev"


def _get_hardware_version() -> tuple[int, int] | tuple[int, int, str]:
Expand Down

0 comments on commit 38e9a4a

Please sign in to comment.