Skip to content

Commit 09bb204

Browse files
fix cython and tests
1 parent 8bc1dea commit 09bb204

File tree

7 files changed

+28
-25
lines changed

7 files changed

+28
-25
lines changed

controller/src/controller/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ class SerialCommPacketTypes(IntEnum):
237237

238238
NUM_INITIAL_MAG_PACKETS_TO_DROP = 2
239239

240-
241240
# Stimulation
242241
STIM_MAX_ABSOLUTE_CURRENT_MICROAMPS = int(100e3)
243242
STIM_MAX_ABSOLUTE_VOLTAGE_MILLIVOLTS = int(1.2e3)

controller/src/controller/subsystems/instrument_comm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,8 +633,8 @@ def _log_dur_since_events(self) -> None:
633633

634634
@dataclass
635635
class StimDataBuffers:
636-
raw: dict[int, NDArray[(2, Any), int]] = {}
637-
reduced: dict[int, NDArray[(2, Any), int]] = {}
636+
raw: dict[int, NDArray[(2, Any), int]] = dataclasses.field(default_factory=dict)
637+
reduced: dict[int, NDArray[(2, Any), int]] = dataclasses.field(default_factory=dict)
638638

639639

640640
class DataStreamManager:

controller/src/controller/utils/data_parsing_cy.pyx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ from ..constants import SERIAL_COMM_PAYLOAD_INDEX
88
from ..constants import SERIAL_COMM_CHECKSUM_LENGTH_BYTES
99
from ..constants import SERIAL_COMM_DATA_SAMPLE_LENGTH_BYTES
1010
from ..constants import SERIAL_COMM_MAGIC_WORD_BYTES
11+
from ..constants import NUM_MAG_SENSORS_PER_WELL
1112
from ..constants import SERIAL_COMM_PACKET_METADATA_LENGTH_BYTES
1213
from ..constants import SERIAL_COMM_PACKET_REMAINDER_SIZE_LENGTH_BYTES
1314
from ..constants import SERIAL_COMM_TIME_OFFSET_LENGTH_BYTES
@@ -42,7 +43,7 @@ DEF NUM_CHANNELS_PER_SENSOR = 3
4243

4344
# these values exist only for importing the constants defined above into the python test suite
4445
SERIAL_COMM_MAGIC_WORD_LENGTH_BYTES_CY = MAGIC_WORD_LEN
45-
SERIAL_COMM_NUM_CHANNELS_PER_SENSOR_CY = NUM_CHANNELS_PER_SENSOR
46+
NUM_CHANNELS_PER_MAG_SENSOR_CY = NUM_CHANNELS_PER_SENSOR
4647

4748
# convert python constants to C types
4849
cdef char[MAGIC_WORD_LEN + 1] MAGIC_WORD = SERIAL_COMM_MAGIC_WORD_BYTES + bytes(1)
@@ -54,13 +55,15 @@ cdef int MIN_PACKET_SIZE = SERIAL_COMM_PACKET_METADATA_LENGTH_BYTES
5455

5556
cdef int SERIAL_COMM_TIME_OFFSET_LENGTH_BYTES_C_INT = SERIAL_COMM_TIME_OFFSET_LENGTH_BYTES
5657
cdef int SERIAL_COMM_DATA_SAMPLE_LENGTH_BYTES_C_INT = SERIAL_COMM_DATA_SAMPLE_LENGTH_BYTES
57-
cdef int SERIAL_COMM_NUM_CHANNELS_PER_SENSOR_C_INT = NUM_CHANNELS_PER_SENSOR
58+
cdef int NUM_CHANNELS_PER_MAG_SENSOR_C_INT = NUM_CHANNELS_PER_SENSOR
59+
cdef int NUM_MAG_SENSORS_PER_WELL_C_INT = NUM_MAG_SENSORS_PER_WELL
5860

5961
cdef int SERIAL_COMM_PAYLOAD_INDEX_C_INT = SERIAL_COMM_PAYLOAD_INDEX
6062
cdef int SERIAL_COMM_STIM_STATUS_PACKET_TYPE_C_INT = SerialCommPacketTypes.STIM_STATUS
6163

6264

6365
cdef int TOTAL_NUM_WELLS_C_INT = NUM_WELLS
66+
cdef int TOTAL_NUM_SENSORS_C_INT = TOTAL_NUM_WELLS_C_INT * NUM_MAG_SENSORS_PER_WELL_C_INT
6467

6568

6669
cdef packed struct Packet:
@@ -203,7 +206,7 @@ cpdef dict parse_magnetometer_data(
203206
cdef int magnetometer_data_packet_len = len(mag_data_packet_bytes) // num_mag_data_packets
204207

205208
cdef int num_time_offsets = TOTAL_NUM_SENSORS_C_INT
206-
cdef int num_data_channels = TOTAL_NUM_SENSORS_C_INT * SERIAL_COMM_NUM_CHANNELS_PER_SENSOR_C_INT
209+
cdef int num_data_channels = TOTAL_NUM_SENSORS_C_INT * NUM_CHANNELS_PER_MAG_SENSOR_C_INT
207210

208211
# arrays for storing parsed data
209212
time_indices = np.empty(num_mag_data_packets, dtype=np.uint64, order="C")
@@ -233,14 +236,14 @@ cpdef dict parse_magnetometer_data(
233236
for sensor in range(TOTAL_NUM_SENSORS_C_INT):
234237
time_offsets_view[time_offset_arr_idx, data_packet_idx] = sensor_data_ptr.time_offset
235238
time_offset_arr_idx += 1
236-
for channel in range(SERIAL_COMM_NUM_CHANNELS_PER_SENSOR_C_INT):
239+
for channel in range(NUM_CHANNELS_PER_MAG_SENSOR_C_INT):
237240
data_view[channel_arr_idx, data_packet_idx] = sensor_data_ptr.data_points[channel]
238241
channel_arr_idx += 1
239242
# shift SensorData ptr by appropriate amount
240243
sensor_data_ptr = <SensorData *> (
241244
(<uint8_t *> sensor_data_ptr)
242245
+ SERIAL_COMM_TIME_OFFSET_LENGTH_BYTES_C_INT
243-
+ (SERIAL_COMM_NUM_CHANNELS_PER_SENSOR_C_INT * SERIAL_COMM_DATA_SAMPLE_LENGTH_BYTES_C_INT)
246+
+ (NUM_CHANNELS_PER_MAG_SENSOR_C_INT * SERIAL_COMM_DATA_SAMPLE_LENGTH_BYTES_C_INT)
244247
)
245248
# increment idxs
246249
bytes_idx += magnetometer_data_packet_len

controller/tests/subsystems/test_instrument_comm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
@pytest.fixture(scope="function", name="test_instrument_comm_obj")
2424
def fixture__test_instrument_comm_obj(mocker):
25-
ic = InstrumentComm(asyncio.Queue(), asyncio.Queue())
25+
ic = InstrumentComm(*[asyncio.Queue() for _ in range(4)])
2626
yield ic
2727
# TODO any teardown needed here?
2828

@@ -69,7 +69,7 @@ async def test_InstrumentComm__creates_connection_to_real_instrument_correctly(
6969

7070
assert test_instrument_comm_obj._instrument is mocked_aioserial.return_value
7171

72-
assert test_instrument_comm_obj._to_monitor_queue.get_nowait() == {
72+
assert test_instrument_comm_obj._comm_to_monitor_queue.get_nowait() == {
7373
"command": "get_board_connection_status",
7474
"in_simulation_mode": False,
7575
}
@@ -100,7 +100,7 @@ async def test_InstrumentComm__creates_connection_to_virtual_instrument_correctl
100100
mocked_vic_init.assert_called_once_with(test_instrument_comm_obj._instrument)
101101
mocked_vic_connect.assert_awaited_once_with(test_instrument_comm_obj._instrument)
102102

103-
assert test_instrument_comm_obj._to_monitor_queue.get_nowait() == {
103+
assert test_instrument_comm_obj._comm_to_monitor_queue.get_nowait() == {
104104
"command": "get_board_connection_status",
105105
"in_simulation_mode": True,
106106
}

controller/tests/test_main.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,20 @@ async def test_main__creates_Server_and_runs_correctly(patch_run_tasks, patch_su
276276
async def test_main__creates_InstrumentComm_and_runs_correctly(
277277
patch_run_tasks, patch_subsystem_inits, mocker
278278
):
279-
spied_create_queues = mocker.spy(main, "create_system_comm_queues")
279+
spied_create_comm_queues = mocker.spy(main, "create_system_comm_queues")
280+
spied_create_data_queues = mocker.spy(main, "create_system_data_queues")
280281

281282
await main.main([])
282283

283-
expected_queues = spied_create_queues.spy_return
284+
expected_comm_queues = spied_create_comm_queues.spy_return
285+
expected_data_queues = spied_create_data_queues.spy_return
284286

285287
patch_subsystem_inits["instrument_comm"].assert_called_once_with(
286-
mocker.ANY, expected_queues["to"]["instrument_comm"], expected_queues["from"]["instrument_comm"]
288+
mocker.ANY,
289+
expected_comm_queues["to"]["instrument_comm"],
290+
expected_comm_queues["from"]["instrument_comm"],
291+
expected_data_queues["main"],
292+
expected_data_queues["file_writer"],
287293
)
288294

289295

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
11
# -*- coding: utf-8 -*-
2-
3-
DEFAULT_SAMPLING_PERIOD = 10000 # valid as of 4/12/22
42
MICROSECONDS_PER_CENTIMILLISECOND = 10
5-
6-
SERIAL_COMM_NUM_CHANNELS_PER_SENSOR = 3
7-
SERIAL_COMM_NUM_SENSORS_PER_WELL = 3

virtual-instrument/src/virtual_instrument/virtual_instrument.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@
1515
from uuid import UUID
1616
from zlib import crc32
1717

18+
from controller.constants import DEFAULT_MAG_SAMPLING_PERIOD
1819
from controller.constants import GOING_DORMANT_HANDSHAKE_TIMEOUT_CODE
1920
from controller.constants import MAX_MC_REBOOT_DURATION_SECONDS
2021
from controller.constants import MICRO_TO_BASE_CONVERSION
2122
from controller.constants import MICROS_PER_MILLIS
23+
from controller.constants import NUM_CHANNELS_PER_MAG_SENSOR
24+
from controller.constants import NUM_MAG_SENSORS_PER_WELL
2225
from controller.constants import SERIAL_COMM_CHECKSUM_LENGTH_BYTES
2326
from controller.constants import SERIAL_COMM_HANDSHAKE_PERIOD_SECONDS
2427
from controller.constants import SERIAL_COMM_HANDSHAKE_TIMEOUT_SECONDS
@@ -57,10 +60,7 @@
5760
from stdlib_utils import InfiniteProcess
5861
from stdlib_utils import resource_path
5962

60-
from .constants import DEFAULT_SAMPLING_PERIOD
6163
from .constants import MICROSECONDS_PER_CENTIMILLISECOND
62-
from .constants import SERIAL_COMM_NUM_CHANNELS_PER_SENSOR
63-
from .constants import SERIAL_COMM_NUM_SENSORS_PER_WELL
6464
from .exceptions import SerialCommInvalidSamplingPeriodError
6565
from .exceptions import SerialCommTooManyMissedHandshakesError
6666
from .exceptions import UnrecognizedSerialCommPacketTypeError
@@ -253,7 +253,7 @@ def _handle_boot_up_config(self, reboot: bool = False) -> None:
253253
self._reset_start_time()
254254
self._reboot_time_secs = None
255255
self._status_codes = [SERIAL_COMM_OKAY_CODE] * (self._num_wells + 2)
256-
self._sampling_period_us = DEFAULT_SAMPLING_PERIOD
256+
self._sampling_period_us = DEFAULT_MAG_SAMPLING_PERIOD
257257
self._adc_readings = [(self.default_adc_reading, self.default_adc_reading)] * self._num_wells
258258
self._stim_info = {}
259259
self._is_stimulating = False
@@ -620,8 +620,8 @@ def _create_magnetometer_data_payload(self) -> bytes:
620620
SERIAL_COMM_MODULE_ID_TO_WELL_IDX[module_id] + 1
621621
)
622622
# add data points
623-
well_sensor_data = time_offset + (data_value.tobytes() * SERIAL_COMM_NUM_CHANNELS_PER_SENSOR)
624-
well_data = well_sensor_data * SERIAL_COMM_NUM_SENSORS_PER_WELL
623+
well_sensor_data = time_offset + (data_value.tobytes() * NUM_CHANNELS_PER_MAG_SENSOR)
624+
well_data = well_sensor_data * NUM_MAG_SENSORS_PER_WELL
625625
magnetometer_data_payload += well_data
626626
return magnetometer_data_payload
627627

0 commit comments

Comments
 (0)