contract.harp¶
HarpRegister ¶
HarpRegister(
name: str,
*,
description: Optional[str] = None,
reader_params: Optional[TReaderParams] = None,
**kwargs,
)
Bases: DataStream[DataFrame, HarpRegisterParams]
Harp device register data stream provider.
A data stream implementation for reading Harp device register data using the Harp Python library.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
DataStream
|
Base class for data stream providers. |
required |
Source code in src/contraqctor/contract/base.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
|
name
property
¶
name: str
Get the name of the data stream.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Name identifier of the data stream. |
resolved_name
property
¶
resolved_name: str
Get the full hierarchical name of the data stream.
Generates a path-like name showing the stream's position in the hierarchy, using '::' as a separator between parent and child names.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The fully resolved name including all parent names. |
description
property
¶
parent
property
¶
parent: Optional[DataStream]
Get the parent data stream.
Returns:
Type | Description |
---|---|
Optional[DataStream]
|
Optional[DataStream]: Parent data stream, or None if this is a root stream. |
is_collection
property
¶
is_collection: bool
Check if this data stream is a collection of other streams.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if this is a collection stream, False otherwise. |
reader_params
property
¶
reader_params: TReaderParams
Get the parameters for the data reader.
Returns:
Name | Type | Description |
---|---|---|
TReaderParams |
TReaderParams
|
Parameters for the data reader. |
has_data
property
¶
has_data: bool
Check if the data stream has loaded data.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if data has been loaded, False otherwise. |
data
property
¶
data: TData
Get the loaded data.
Returns:
Name | Type | Description |
---|---|---|
TData |
TData
|
The loaded data. |
Raises:
Type | Description |
---|---|
ValueError
|
If data has not been loaded yet. |
read ¶
read(
reader_params: Optional[HarpRegisterParams] = None,
) -> DataFrame
Read register data from Harp binary files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reader_params
|
Optional[HarpRegisterParams]
|
Parameters for register reading configuration. |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the register data. |
Raises:
Type | Description |
---|---|
ValueError
|
If reader parameters are not set. |
Source code in src/contraqctor/contract/harp.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
from_register_reader
classmethod
¶
from_register_reader(
name: str,
reg_reader: RegisterReader,
params: HarpRegisterParams = _DEFAULT_HARP_READER_PARAMS,
) -> Self
Create a HarpRegister data stream from a RegisterReader.
Factory method to create a HarpRegister instance from an existing Harp RegisterReader object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name for the register data stream. |
required |
reg_reader
|
RegisterReader
|
Harp RegisterReader object. |
required |
params
|
HarpRegisterParams
|
Parameters for register reading configuration. |
_DEFAULT_HARP_READER_PARAMS
|
Returns:
Name | Type | Description |
---|---|---|
HarpRegister |
Self
|
Newly created HarpRegister data stream. |
Source code in src/contraqctor/contract/harp.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
|
bind_reader_params ¶
bind_reader_params(params: TReaderParams) -> Self
Bind reader parameters to the data stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
TReaderParams
|
Parameters to bind to the data stream's reader. |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The data stream instance for method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
If reader parameters have already been set. |
Source code in src/contraqctor/contract/base.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
at ¶
at(name: str) -> DataStream
Get a child data stream by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the child data stream to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
DataStream |
DataStream
|
The child data stream with the given name. |
Raises:
Type | Description |
---|---|
NotImplementedError
|
If the data stream does not support child access. |
Examples:
# Access stream in a collection
collection = data_collection.load()
temp_stream = collection.at("temperature")
# Or using dictionary-style syntax
humidity_stream = collection["humidity"]
Source code in src/contraqctor/contract/base.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|
load ¶
load() -> Self
Load data into the data stream.
Reads data from the source and stores it in the data stream.
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The data stream instance for method chaining. |
Examples:
from contraqctor.contract import csv
# Create and load a CSV stream
params = csv.CsvParams(path="data/measurements.csv")
csv_stream = csv.Csv("measurements", reader_params=params)
csv_stream.load()
# Access the data
df = csv_stream.data
print(f"Loaded {len(df)} rows")
Source code in src/contraqctor/contract/base.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
load_all ¶
load_all(
strict: bool = False,
) -> list[tuple[DataStream, Exception], None, None]
Recursively load this data stream and all child streams.
Performs depth-first traversal to load all streams in the hierarchy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict
|
bool
|
If True, raises exceptions immediately; otherwise collects and returns them. |
False
|
Returns:
Name | Type | Description |
---|---|---|
list |
list[tuple[DataStream, Exception], None, None]
|
List of tuples containing streams and exceptions that occurred during loading. |
Raises:
Type | Description |
---|---|
Exception
|
If strict is True and an exception occurs during loading. |
Examples:
# Load all streams and handle errors
errors = collection.load_all(strict=False)
if errors:
for stream, error in errors:
print(f"Error loading {stream.name}: {error}")
Source code in src/contraqctor/contract/base.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
DeviceYmlByWhoAmI ¶
Bases: _DeviceYmlSource
Device YAML source that finds the file using WhoAmI value.
Specifies that the device YAML should be obtained by looking up a device by its WhoAmI identifier.
Attributes:
Name | Type | Description |
---|---|---|
method |
Literal['whoami']
|
Fixed as "whoami". |
who_am_i |
Annotated[int, Field(ge=0, le=9999, description='WhoAmI value')]
|
The WhoAmI value of the device (0-9999). |
DeviceYmlByFile ¶
Bases: _DeviceYmlSource
Device YAML source that specifies a file path.
Specifies that the device YAML should be loaded from a local file path.
Attributes:
Name | Type | Description |
---|---|---|
method |
Literal['file']
|
Fixed as "file". |
path |
Optional[PathLike | str]
|
Optional path to the device YAML file. If None, assumes "device.yml" in the data directory. |
DeviceYmlByUrl ¶
Bases: _DeviceYmlSource
Device YAML source that fetches from a URL.
Specifies that the device YAML should be downloaded from a URL.
Attributes:
Name | Type | Description |
---|---|---|
method |
Literal['http']
|
Fixed as "http". |
url |
AnyHttpUrl
|
HTTP URL to download the device YAML file from. |
DeviceYmlByRegister0 ¶
Bases: _DeviceYmlSource
Device YAML source that infers from register 0 file.
Specifies that the device YAML should be determined by finding and reading the WhoAmI value from register 0 files.
Attributes:
Name | Type | Description |
---|---|---|
method |
Literal['register0']
|
Fixed as "register0". |
register0_glob_pattern |
List[str]
|
List of glob patterns to locate register 0 files. |
HarpDeviceParams
dataclass
¶
HarpDeviceParams(path: PathLike)
Bases: FilePathBaseParam
Parameters for Harp device data reading.
Defines parameters for locating and reading Harp device data.
Attributes:
Name | Type | Description |
---|---|---|
device_yml_hint |
DeviceYmlSource
|
Source for the device YAML configuration file. |
include_common_registers |
bool
|
Whether to include common registers. Defaults to True. |
keep_type |
bool
|
Whether to preserve type information. Defaults to True. |
epoch |
Optional[datetime]
|
Reference datetime for timestamp calculations. If provided, timestamps are converted to datetime. |
HarpDevice ¶
HarpDevice(
name: str,
*,
description: Optional[str] = None,
reader_params: Optional[TReaderParams] = None,
**kwargs,
)
Bases: DataStreamCollectionBase[HarpRegister, HarpDeviceParams]
Harp device data stream collection provider.
A data stream collection for accessing all registers of a Harp device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
DataStreamCollectionBase
|
Base class for data stream collection providers. |
required |
Examples:
from contraqctor.contract.harp import HarpDevice, HarpDeviceParams, DeviceYmlByWhoAmI
# Create and load a device stream
params = HarpDeviceParams(
path="behavior.harp",
device_yml_hint=DeviceYmlByWhoAmI(who_am_i=1216)
)
behavior = HarpDevice("behavior", reader_params=params).load()
# Access registers
digital_input = behavior["DigitalInputState"].data
adc = behavior["AnalogData"].data
Source code in src/contraqctor/contract/base.py
317 318 319 320 321 322 323 324 325 326 327 |
|
device_reader
property
¶
device_reader: DeviceReader
Get the underlying Harp device reader.
Returns:
Type | Description |
---|---|
DeviceReader
|
harp.reader.DeviceReader: Harp device reader for accessing raw device functionality. |
Raises:
Type | Description |
---|---|
ValueError
|
If the device reader has not been initialized. |
name
property
¶
name: str
Get the name of the data stream.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Name identifier of the data stream. |
resolved_name
property
¶
resolved_name: str
Get the full hierarchical name of the data stream.
Generates a path-like name showing the stream's position in the hierarchy, using '::' as a separator between parent and child names.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The fully resolved name including all parent names. |
description
property
¶
parent
property
¶
parent: Optional[DataStream]
Get the parent data stream.
Returns:
Type | Description |
---|---|
Optional[DataStream]
|
Optional[DataStream]: Parent data stream, or None if this is a root stream. |
is_collection
property
¶
is_collection: bool
Check if this data stream is a collection of other streams.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if this is a collection stream, False otherwise. |
reader_params
property
¶
reader_params: TReaderParams
Get the parameters for the data reader.
Returns:
Name | Type | Description |
---|---|---|
TReaderParams |
TReaderParams
|
Parameters for the data reader. |
has_data
property
¶
has_data: bool
Check if the data stream has loaded data.
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if data has been loaded, False otherwise. |
data
property
¶
data: TData
Get the loaded data.
Returns:
Name | Type | Description |
---|---|---|
TData |
TData
|
The loaded data. |
Raises:
Type | Description |
---|---|
ValueError
|
If data has not been loaded yet. |
read ¶
read(
reader_params: Optional[TReaderParams] = None,
) -> TData
Read data using the configured reader.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reader_params
|
Optional[TReaderParams]
|
Optional parameters to override the default reader parameters. |
None
|
Returns:
Name | Type | Description |
---|---|---|
TData |
TData
|
Data read from the source. |
Raises:
Type | Description |
---|---|
ValueError
|
If reader parameters are not set. |
Source code in src/contraqctor/contract/base.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
|
bind_reader_params ¶
bind_reader_params(params: TReaderParams) -> Self
Bind reader parameters to the data stream.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
params
|
TReaderParams
|
Parameters to bind to the data stream's reader. |
required |
Returns:
Name | Type | Description |
---|---|---|
Self |
Self
|
The data stream instance for method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
If reader parameters have already been set. |
Source code in src/contraqctor/contract/base.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
|
at ¶
at(name: str) -> TDataStream
Get a child data stream by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Name of the child data stream to retrieve. |
required |
Returns:
Name | Type | Description |
---|---|---|
TDataStream |
TDataStream
|
The child data stream with the given name. |
Raises:
Type | Description |
---|---|
ValueError
|
If data has not been loaded yet. |
KeyError
|
If no child stream with the given name exists. |
Source code in src/contraqctor/contract/base.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
|
load ¶
load()
Load data for this collection.
Overrides the base method to add validation that loaded data is a list of DataStreams.
Returns:
Name | Type | Description |
---|---|---|
Self |
The collection instance for method chaining. |
Raises:
Type | Description |
---|---|
ValueError
|
If loaded data is not a list of DataStreams. |
Source code in src/contraqctor/contract/base.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
load_all ¶
load_all(
strict: bool = False,
) -> list[tuple[DataStream, Exception], None, None]
Recursively load this data stream and all child streams.
Performs depth-first traversal to load all streams in the hierarchy.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
strict
|
bool
|
If True, raises exceptions immediately; otherwise collects and returns them. |
False
|
Returns:
Name | Type | Description |
---|---|---|
list |
list[tuple[DataStream, Exception], None, None]
|
List of tuples containing streams and exceptions that occurred during loading. |
Raises:
Type | Description |
---|---|
Exception
|
If strict is True and an exception occurs during loading. |
Examples:
# Load all streams and handle errors
errors = collection.load_all(strict=False)
if errors:
for stream, error in errors:
print(f"Error loading {stream.name}: {error}")
Source code in src/contraqctor/contract/base.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
|
iter_all ¶
iter_all() -> Generator[DataStream, None, None]
Iterator for all child data streams, including nested collections.
Implements a depth-first traversal of the stream hierarchy.
Yields:
Name | Type | Description |
---|---|---|
DataStream |
DataStream
|
All recursively yielded child data streams. |
Source code in src/contraqctor/contract/base.py
436 437 438 439 440 441 442 443 444 445 446 447 448 |
|
fetch_yml_from_who_am_i ¶
Fetch a device YAML file based on its WhoAmI identifier.
Looks up the device in the WhoAmI registry and downloads its YAML file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
who_am_i
|
int
|
WhoAmI identifier of the device. |
required |
release
|
str
|
Git branch or tag to use for fetching the YAML file. |
'main'
|
Returns:
Type | Description |
---|---|
BytesIO
|
io.BytesIO: Memory buffer containing the device YAML content. |
Raises:
Type | Description |
---|---|
KeyError
|
If the WhoAmI identifier is not found in the registry. |
ValueError
|
If required repository information is missing or YAML file cannot be found. |
Source code in src/contraqctor/contract/harp.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
fetch_who_am_i_list
cached
¶
fetch_who_am_i_list(
url: str = "https://raw.githubusercontent.com/harp-tech/whoami/main/whoami.yml",
) -> Dict[int, Any]
Fetch and parse the Harp WhoAmI registry.
Downloads and parses the WhoAmI registry YAML file from GitHub. Results are cached for efficiency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
URL to the WhoAmI registry YAML file. |
'https://raw.githubusercontent.com/harp-tech/whoami/main/whoami.yml'
|
Returns:
Type | Description |
---|---|
Dict[int, Any]
|
Dict[int, Any]: Dictionary mapping WhoAmI identifiers to device information. |
Source code in src/contraqctor/contract/harp.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|