Skip to content

Commit

Permalink
Added CSV writing to IMU Experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyselk committed Aug 22, 2023
1 parent 17e5383 commit 706d2d1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/imu_experiment/imu_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ def set_setup_task(
builder = (
ImuExperimentBuilder()
.set_query_task(QueryTaskWidgetBuilder().set_kafka_manager(manager))
.set_setup_task(SetupTaskWidgetBuilder())
.set_setup_task(SetupTaskWidgetBuilder().set_kafka_manager(manager))
)
build_and_launch_window_single_process(builder)
37 changes: 36 additions & 1 deletion examples/imu_experiment/src/widgets/setup_task_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from natKit.client.gui.pyqt6.widget import Task, TaskBuilder, ExperimentBuilder
from natKit.client.gui.pyqt6.window import ExperimentWindow
from natKit.common.kafka import KafkaManager
from natKit.api import ImuDataSchema

from .imu_plot_widget import ImuPlotWidget
from .imu_connection_widget import ImuConnectionWidget
Expand All @@ -16,15 +18,18 @@

from typing import NoReturn

from time import sleep


class SetupTaskWidget(Task):
""" """

onNext = pyqtSignal()
onPrev = pyqtSignal()

def __init__(self, *args, **kwargs):
def __init__(self, kafka_manager: KafkaManager, *args, **kwargs):
super().__init__(*args, **kwargs)
self.kafka_manager = kafka_manager
self.imu_streams = ImuStreams()

def set_imu_streams(self, imu_streams) -> NoReturn:
Expand Down Expand Up @@ -59,6 +64,12 @@ def setup(self):
self._previous_task_button, alignment=Qt.AlignmentFlag.AlignLeft
)

self.write_to_csv_button = QPushButton(text="Write CSV")
self.write_to_csv_button.clicked.connect(self.write_csv)
button_layout.addWidget(
self.write_to_csv_button, alignment=Qt.AlignmentFlag.AlignCenter
)

self._next_task_button = QPushButton(text="Data Collection ->")
self._next_task_button.clicked.connect(lambda: self.onNext.emit())
button_layout.addWidget(
Expand All @@ -69,13 +80,33 @@ def setup(self):

self.setLayout(self.layout)

def write_csv(self):
streams_and_names = self.imu_streams.get_streams_and_names()
print("Writing to CSV...")
for stream, name in streams_and_names:
new_stream = self.kafka_manager.create_new_stream_from_stream(stream)
while not new_stream.stream_ready:
sleep(0.01)
sleep(0.1)

with open(name + ".csv", "w") as f:
f.write(ImuDataSchema.csv_header() + "\n")
while True:
data = new_stream.read_data()
if data is None:
break
f.write(data.to_csv() + "\n")
print("Finished writing to CSV")


class SetupTaskWidgetBuilder(TaskBuilder):
def __init__(self) -> NoReturn:
super().__init__()
self.kafka_manager = None

def build(self) -> Task:
return SetupTaskWidget(
kafka_manager=self.kafka_manager,
name=self.name,
stages=self.stages,
inter_stage_interval=self.inter_stage_interval,
Expand All @@ -84,3 +115,7 @@ def build(self) -> Task:
events=self.events,
duration_events=self.duration_events,
)

def set_kafka_manager(self, manager: KafkaManager):
self.kafka_manager = manager
return self
25 changes: 25 additions & 0 deletions natKit/api/src/python/natKit/api/schemas/imu_data_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ def deserialize(encoder: Encoder, msg: bytes) -> Schema:
assert 0, "{} does not support {} encoding".format(
ImuDataSchema.get_name(), encoder.get_name()
)

@staticmethod
def csv_header() -> str:
header = [
"timestamp",
"data0",
"data1",
"data2",
"data3",
"data4",
"data5",
"data6",
"data7",
"data8",
"calibration",
]
return ",".join(header)

def to_csv(self) -> str:
data = [0 for i in range(11)]
data[0] = str(self.timestamp)
for i, datum in enumerate(self.data):
data[i + 1] = str(datum)
data[10] = str(self.calibration)
return ",".join(data)
5 changes: 5 additions & 0 deletions natKit/common/src/python/natKit/common/kafka/topic_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ def _create_stream(self, meta_topic_string: str, data_topic_string: str):
data_messenger=self.create_messenger(data_topic_string),
)

def create_new_stream_from_stream(self, stream: Stream) -> Stream:
return self._create_stream(
stream.get_meta_topic_string(), stream.get_data_topic_string()
)

def find_streams(self):
topic_string_pairs = StreamHelper.find_topic_stream_pairs(
self.query_topic_names()
Expand Down

0 comments on commit 706d2d1

Please sign in to comment.