-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3103413
commit 0356aad
Showing
6 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Built-in Imports | ||
from typing import Dict, Any, Optional, IO | ||
import pathlib | ||
|
||
# Third-party Imports | ||
|
||
# Internal Import | ||
from .record import Record | ||
|
||
|
||
class TextRecord(Record): | ||
def __init__( | ||
self, | ||
dir: pathlib.Path, | ||
name: str, | ||
): | ||
"""Construct a text file Record. | ||
Args: | ||
dir (pathlib.Path): The directory to store the snap shots of data. | ||
name (str): The name of the ``Record``. | ||
suffix (str): The suffix of the text file. Defaults to "txt". | ||
""" | ||
super().__init__() | ||
|
||
# Saving the Record attributes | ||
self.dir = dir | ||
self.name = name | ||
self.first_frame = False | ||
self.file_handler: Optional[IO[str]] = None | ||
|
||
def write(self, data_chunk: Dict[str, Any]): | ||
if not self.first_frame: | ||
self.file_handler = (self.dir / f"{self.name}.{data_chunk['suffix']}").open( | ||
"w" | ||
) | ||
self.first_frame = True | ||
|
||
text_data = data_chunk["data"] | ||
assert self.file_handler is not None | ||
self.file_handler.write(text_data) | ||
|
||
def close(self): | ||
if self.file_handler is not None: | ||
self.file_handler.close() | ||
self.file_handler = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from .data_nodes import TextNode | ||
|
||
# Built-in Imports | ||
import os | ||
import pathlib | ||
import time | ||
import uuid | ||
|
||
# Third-party | ||
import pytest | ||
|
||
# Internal Imports | ||
import chimerapy.engine as cpe | ||
from chimerapy.engine.records.text_record import TextRecord | ||
from chimerapy.engine.networking.async_loop_thread import AsyncLoopThread | ||
from chimerapy.engine.eventbus import EventBus, Event | ||
|
||
logger = cpe._logger.getLogger("chimerapy-engine") | ||
|
||
# Constants | ||
CWD = pathlib.Path(os.path.abspath(__file__)).parent.parent | ||
TEST_DATA_DIR = CWD / "data" | ||
|
||
|
||
@pytest.fixture | ||
def text_node(): | ||
|
||
# Create a node | ||
text_n = TextNode(name="text_n", logdir=TEST_DATA_DIR) | ||
|
||
return text_n | ||
|
||
|
||
def test_text_record(): | ||
|
||
# Check that the image was created | ||
expected_text_path = TEST_DATA_DIR / "test-5.log" | ||
try: | ||
os.rmdir(expected_text_path.parent) | ||
except OSError: | ||
... | ||
|
||
# Create the record | ||
text_r = TextRecord(dir=TEST_DATA_DIR, name="test-5") | ||
|
||
data = [ | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, " | ||
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n", | ||
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi " | ||
"ut aliquip ex ea commodo consequat.\n", | ||
] | ||
|
||
# Write to image file | ||
for i in range(5): | ||
print("\n".join(data)) | ||
text_chunk = { | ||
"uuid": uuid.uuid4(), | ||
"name": "test-5", | ||
"suffix": "log", | ||
"data": "".join(data), | ||
"dtype": "text", | ||
} | ||
text_r.write(text_chunk) | ||
|
||
# Check that the image was created | ||
assert expected_text_path.exists() | ||
|
||
with expected_text_path.open("r") as jlf: | ||
for idx, line in enumerate(jlf): | ||
assert line.strip() == (data[idx % len(data)]).strip() | ||
|
||
|
||
def test_node_save_text_stream(text_node): | ||
|
||
# Event Loop | ||
thread = AsyncLoopThread() | ||
thread.start() | ||
eventbus = EventBus(thread=thread) | ||
|
||
# Check that the image was created | ||
expected_text_path = pathlib.Path(text_node.state.logdir) / "test.text" | ||
try: | ||
os.rmdir(expected_text_path.parent) | ||
except OSError: | ||
... | ||
|
||
# Stream | ||
text_node.run(blocking=False, eventbus=eventbus) | ||
|
||
# Wait to generate files | ||
eventbus.send(Event("start")).result() | ||
logger.debug("Finish start") | ||
eventbus.send(Event("record")).result() | ||
logger.debug("Finish record") | ||
time.sleep(3) | ||
eventbus.send(Event("stop")).result() | ||
logger.debug("Finish stop") | ||
|
||
text_node.shutdown() | ||
|
||
# Check that the image was created | ||
assert expected_text_path.exists() |