Skip to content

Commit

Permalink
further refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
orgua committed May 7, 2023
1 parent b92bc31 commit 5aef732
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 27 deletions.
1 change: 1 addition & 0 deletions software/python-package/setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ zip_safe = True
include_package_data = True
python_requires = >= 3.8
install_requires =
shepherd-core
click
click-config-file
numpy
Expand Down
20 changes: 11 additions & 9 deletions software/python-package/shepherd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
from .logger import logger
from .logger import set_verbose_level
from .shepherd_debug import ShepherdDebug
from .shepherd_emulator import Emulator
from .shepherd_emulator import ShepherdEmulator
from .shepherd_io import ShepherdIOException
from .shepherd_recorder import Recorder
from .shepherd_sharedmem import DataBuffer
from .shepherd_harvester import ShepherdHarvester
from .shared_memory import DataBuffer
from .sysfs_interface import check_sys_access
from .target_io import TargetIO
from .virtual_harvester_config import T_vHrv
Expand All @@ -57,11 +57,11 @@
"set_verbose_level",
"get_verbose_level",
"logger",
"Recorder",
"Emulator",
"ShepherdHarvester",
"ShepherdEmulator",
"ShepherdDebug",
"run_emulator",
"run_recorder",
"run_harvester",
]


Expand All @@ -86,7 +86,7 @@ def retrieve_calibration(use_default_cal: bool = False) -> CalibrationData:
return CalibrationData.from_default()


def run_recorder(
def run_harvester(
output_path: Path,
duration: Optional[float] = None,
harvester: Optional[T_vHrv] = None,
Expand Down Expand Up @@ -134,7 +134,9 @@ def run_recorder(
10**9 * samples_per_buffer // sysfs_interface.get_buffer_period_ns()
)

recorder = Recorder(shepherd_mode=mode, harvester=harvester, calibration=cal_data)
recorder = ShepherdHarvester(
shepherd_mode=mode, harvester=harvester, calibration=cal_data
)
log_writer = LogWriter(
file_path=store_path,
calibration_data=cal_data,
Expand Down Expand Up @@ -331,7 +333,7 @@ def run_emulator(
for _, dsv, dsc in log_reader.read_buffers(end_n=fifo_buffer_size)
]

emu = Emulator(
emu = ShepherdEmulator(
shepherd_mode=mode, # TODO: this should not be needed anymore
initial_buffers=init_buffers,
calibration_recording=log_reader.get_calibration_data(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __len__(self):
return min(self.voltage.size, self.current.size)


class SharedMem:
class SharedMemory:
"""Represents shared RAM used to exchange data between PRUs and userspace.
A large area of contiguous memory is allocated through remoteproc. The PRUs
Expand Down
4 changes: 2 additions & 2 deletions software/python-package/shepherd/shepherd_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from .calibration import CalibrationData
from .logger import logger
from .shepherd_io import ShepherdIO
from .shepherd_sharedmem import DataBuffer
from .shared_memory import DataBuffer
from .virtual_harvester_config import VirtualHarvesterConfig
from .virtual_source_config import T_vSrc
from .virtual_source_config import VirtualSourceConfig


class Emulator(ShepherdIO):
class ShepherdEmulator(ShepherdIO):
"""API for emulating data with shepherd.
Provides an easy to use, high-level interface for emulating data with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from .virtual_harvester_config import VirtualHarvesterConfig


class Recorder(ShepherdIO):
"""API for recording data with shepherd.
class ShepherdHarvester(ShepherdIO):
"""API for recording a harvest with shepherd.
Provides an easy-to-use, high-level interface for recording data with
shepherd. Configures all hardware and initializes the communication
Expand Down
8 changes: 4 additions & 4 deletions software/python-package/shepherd/shepherd_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from . import sysfs_interface as sfs
from .calibration import CalibrationData
from .calibration import cal_component_list
from .shepherd_sharedmem import SharedMem
from .shared_memory import SharedMemory
from .sysfs_interface import check_sys_access
from .virtual_harvester_config import VirtualHarvesterConfig
from .virtual_source_config import VirtualSourceConfig
Expand Down Expand Up @@ -97,7 +97,7 @@ def __init__(self, mode: str):
self.samples_per_buffer = 0
self.buffer_period_ns = 0
self.n_buffers = 0
self.shared_mem: SharedMem
self.shared_mem: SharedMemory

def __del__(self):
ShepherdIO._instance = None
Expand Down Expand Up @@ -201,7 +201,7 @@ def reinitialize_prus(self) -> None:
sfs.wait_for_state("idle", 5)

def refresh_shared_mem(self):
if hasattr(self, "shared_mem") and isinstance(self.shared_mem, SharedMem):
if hasattr(self, "shared_mem") and isinstance(self.shared_mem, SharedMemory):
self.shared_mem.__exit__()

# Ask PRU for base address of shared mem (reserved with remoteproc)
Expand All @@ -226,7 +226,7 @@ def refresh_shared_mem(self):
self._buffer_period = self.buffer_period_ns / 1e9
logger.debug("Buffer period: \t\t%.3f s", self._buffer_period)

self.shared_mem = SharedMem(
self.shared_mem = SharedMemory(
self.mem_address,
self.mem_size,
self.n_buffers,
Expand Down
4 changes: 2 additions & 2 deletions software/python-package/tests/test_emulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import yaml

from shepherd import CalibrationData
from shepherd import Emulator
from shepherd import ShepherdEmulator
from shepherd import LogWriter
from shepherd import ShepherdDebug
from shepherd import ShepherdIOException
Expand Down Expand Up @@ -70,7 +70,7 @@ def emulator(request, shepherd_up, shp_reader, virtsource_settings_yml):
DataBuffer(voltage=dsv, current=dsc)
for _, dsv, dsc in shp_reader.read_buffers(end_n=fifo_buffer_size)
]
emu = Emulator(
emu = ShepherdEmulator(
calibration_recording=CalibrationData(shp_reader.get_calibration_data()),
calibration_emulator=CalibrationData.from_default(),
initial_buffers=init_buffers,
Expand Down
10 changes: 5 additions & 5 deletions software/python-package/tests/test_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from shepherd import CalibrationData
from shepherd import LogWriter
from shepherd import Recorder
from shepherd import run_recorder
from shepherd import ShepherdHarvester
from shepherd import run_harvester


@pytest.fixture(params=["harvester"]) # TODO: there is a second mode now
Expand All @@ -29,7 +29,7 @@ def log_writer(tmp_path, mode):

@pytest.fixture()
def recorder(request, shepherd_up, mode):
rec = Recorder(shepherd_mode=mode)
rec = ShepherdHarvester(shepherd_mode=mode)
request.addfinalizer(rec.__del__)
rec.__enter__()
request.addfinalizer(rec.__exit__)
Expand All @@ -38,7 +38,7 @@ def recorder(request, shepherd_up, mode):

@pytest.mark.hardware
def test_instantiation(shepherd_up):
rec = Recorder()
rec = ShepherdHarvester()
rec.__enter__()
assert rec is not None
rec.__exit__()
Expand All @@ -61,7 +61,7 @@ def test_recorder(log_writer, recorder):
def test_record_fn(tmp_path, shepherd_up):
output = tmp_path / "rec.h5"
start_time = int(time.time() + 10)
run_recorder(
run_harvester(
output_path=output,
duration=10,
force_overwrite=True,
Expand Down
4 changes: 2 additions & 2 deletions software/python-package/tests_manual/testbench_longrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from shepherd import logger
from shepherd import run_emulator
from shepherd import run_recorder
from shepherd import run_harvester

# run with
# sudo python3 /opt/shepherd/software/python-package/tests_manual/testbench_longrun.py
Expand All @@ -16,7 +16,7 @@

if not file_rec.exists():
logger.info("Start harvesting")
run_recorder(
run_harvester(
output_path=file_rec,
duration=duration,
force_overwrite=True,
Expand Down

0 comments on commit 5aef732

Please sign in to comment.