API Reference
Complete reference for all pyhfm functions, classes, and modules.
Main Functions
pyhfm.read_hfm(file_path, *, return_metadata=False, config=None)
Read and parse an HFM data file.
This is the main entry point for reading Heat Flow Meter (HFM) data files. The function returns a PyArrow table with embedded metadata by default, or optionally returns a tuple of (metadata, table) for more detailed access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the HFM file (.tst format) |
required |
return_metadata
|
bool
|
If True, return (metadata, table) tuple instead of just table |
False
|
config
|
dict[str, Any] | None
|
Optional configuration overrides for parsing |
None
|
Returns:
| Type | Description |
|---|---|
Table | tuple[FileMetadata, Table]
|
PyArrow table with embedded metadata, or tuple of (metadata, table) |
Table | tuple[FileMetadata, Table]
|
if return_metadata=True |
Raises:
| Type | Description |
|---|---|
HFMFileError
|
If file cannot be read or doesn't exist |
HFMParsingError
|
If file parsing fails |
HFMUnsupportedFormatError
|
If file format is not supported |
HFMValidationError
|
If data validation fails |
Examples:
Basic usage:
>>> import polars as pl
>>> table = read_hfm("sample.tst")
>>> print(table.schema)
>>> print(pl.from_arrow(table))
Access metadata separately:
>>> metadata, table = read_hfm("sample.tst", return_metadata=True)
>>> print(metadata["sample_id"])
>>> print(metadata["type"])
Custom configuration:
Source code in src/pyhfm/api/loaders.py
Core Modules
Parser
pyhfm.core.parser.HFMParser
Main parser for HFM data files.
This class maintains backward compatibility while delegating to the new modular FileParser architecture.
Source code in src/pyhfm/core/parser.py
Attributes
config
property
Access to parser configuration for backward compatibility.
Methods:
__init__(config=None)
Initialize HFM parser.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any] | None
|
Optional configuration overrides |
None
|
parse_file(file_path)
Parse an HFM file and return PyArrow table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to HFM file |
required |
Returns:
| Type | Description |
|---|---|
Table
|
PyArrow table with embedded metadata |
Raises:
| Type | Description |
|---|---|
HFMFileError
|
If file cannot be read |
HFMUnsupportedFormatError
|
If file format not supported |
HFMParsingError
|
If parsing fails |
Source code in src/pyhfm/core/parser.py
Data Extractor
pyhfm.extractors.data_extractor.DataExtractor
Extracts tabular data from HFM metadata.
Source code in src/pyhfm/extractors/data_extractor.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 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 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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | |
Methods:
__init__(config=None)
Initialize data extractor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
dict[str, Any] | None
|
Optional configuration overrides |
None
|
Source code in src/pyhfm/extractors/data_extractor.py
extract_data(metadata)
Extract data from metadata and return PyArrow table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
FileMetadata
|
HFM metadata dictionary |
required |
Returns:
| Type | Description |
|---|---|
Table
|
PyArrow table with measurement data |
Raises:
| Type | Description |
|---|---|
HFMDataExtractionError
|
If data extraction fails |
Source code in src/pyhfm/extractors/data_extractor.py
API Module
Loaders
pyhfm.api.loaders
Main API for loading HFM data files.
Classes
Functions:
read_hfm(file_path, *, return_metadata=False, config=None)
Read and parse an HFM data file.
This is the main entry point for reading Heat Flow Meter (HFM) data files. The function returns a PyArrow table with embedded metadata by default, or optionally returns a tuple of (metadata, table) for more detailed access.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str | Path
|
Path to the HFM file (.tst format) |
required |
return_metadata
|
bool
|
If True, return (metadata, table) tuple instead of just table |
False
|
config
|
dict[str, Any] | None
|
Optional configuration overrides for parsing |
None
|
Returns:
| Type | Description |
|---|---|
Table | tuple[FileMetadata, Table]
|
PyArrow table with embedded metadata, or tuple of (metadata, table) |
Table | tuple[FileMetadata, Table]
|
if return_metadata=True |
Raises:
| Type | Description |
|---|---|
HFMFileError
|
If file cannot be read or doesn't exist |
HFMParsingError
|
If file parsing fails |
HFMUnsupportedFormatError
|
If file format is not supported |
HFMValidationError
|
If data validation fails |
Examples:
Basic usage:
>>> import polars as pl
>>> table = read_hfm("sample.tst")
>>> print(table.schema)
>>> print(pl.from_arrow(table))
Access metadata separately:
>>> metadata, table = read_hfm("sample.tst", return_metadata=True)
>>> print(metadata["sample_id"])
>>> print(metadata["type"])
Custom configuration:
Source code in src/pyhfm/api/loaders.py
main()
Command-line interface for reading HFM files.
Usage
pyhfm
Source code in src/pyhfm/api/loaders.py
Exceptions
pyhfm.exceptions
Custom exceptions for HFM data processing.
Classes
HFMError
Bases: Exception
Base exception for all HFM-related errors.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None)
Initialize HFM error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
Source code in src/pyhfm/exceptions.py
HFMValidationWarning
Bases: UserWarning
Warning for recoverable inconsistencies found while parsing HFM files.
Emitted (via warnings.warn) when a file can still be parsed but its
contents look suspicious — e.g. the number of parsed setpoints does not
match the declared Number of Setpoints: header, or setpoint numbering
restarts mid-file because an interrupted test was resumed.
Source code in src/pyhfm/exceptions.py
HFMParsingError
Bases: HFMError
Raised when HFM file parsing fails.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None, line_number=None)
Initialize parsing error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
line_number
|
int | None
|
Optional line number where error occurred |
None
|
Source code in src/pyhfm/exceptions.py
HFMValidationError
Bases: HFMError
Raised when HFM data validation fails.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None, field_name=None, invalid_value=None)
Initialize validation error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
field_name
|
str | None
|
Optional name of the invalid field |
None
|
invalid_value
|
str | float | int | None
|
Optional invalid value that caused the error |
None
|
Source code in src/pyhfm/exceptions.py
HFMMetadataError
Bases: HFMError
Raised when metadata extraction fails.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None, missing_fields=None)
Initialize metadata error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
missing_fields
|
list[str] | None
|
Optional list of missing required fields |
None
|
Source code in src/pyhfm/exceptions.py
HFMDataExtractionError
Bases: HFMError
Raised when data extraction from metadata fails.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None, measurement_type=None, setpoint=None)
Initialize data extraction error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
measurement_type
|
str | None
|
Optional measurement type (conductivity, heat_capacity) |
None
|
setpoint
|
int | None
|
Optional setpoint number that caused the error |
None
|
Source code in src/pyhfm/exceptions.py
HFMFileError
Bases: HFMError
Raised when file operations fail.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None, operation=None)
Initialize file error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
operation
|
str | None
|
Optional operation that failed (read, write, detect_encoding) |
None
|
Source code in src/pyhfm/exceptions.py
HFMUnsupportedFormatError
Bases: HFMError
Raised when file format is not supported.
Source code in src/pyhfm/exceptions.py
Methods:
__init__(message, file_path=None, detected_format=None, supported_formats=None)
Initialize unsupported format error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error description |
required |
file_path
|
str | None
|
Optional path to file that caused the error |
None
|
detected_format
|
str | None
|
Optional detected file format |
None
|
supported_formats
|
list[str] | None
|
Optional list of supported formats |
None
|
Source code in src/pyhfm/exceptions.py
Constants and Configuration
pyhfm.constants
Constants and configuration for HFM data processing.
Classes
HFMType
FileMetadata
Bases: TypedDict
Structured metadata for HFM files.
Source code in src/pyhfm/constants.py
TemperatureData
CalibrationData
ResultsData
ThermalEquilibriumData
Bases: TypedDict
Thermal equilibrium criteria data.
Source code in src/pyhfm/constants.py
SetpointData
Bases: TypedDict
Complete setpoint data structure.
Source code in src/pyhfm/constants.py
CompiledPatterns
dataclass
Pre-compiled regex patterns for maximum efficiency.
Source code in src/pyhfm/constants.py
HFMParsingConfig
dataclass
Configuration for HFM file parsing.
Source code in src/pyhfm/constants.py
ColumnConfig
dataclass
Configuration for data table columns.
Source code in src/pyhfm/constants.py
Methods:
__post_init__()
Initialize default column schemas.
Source code in src/pyhfm/constants.py
ValidationConfig
dataclass
Configuration for data validation.
Source code in src/pyhfm/constants.py
Utilities
pyhfm.utils
Utility functions to replace labetl dependencies.
Functions:
detect_encoding(file_path)
Detect the encoding of a text file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the file to analyze |
required |
Returns:
| Type | Description |
|---|---|
str
|
Detected encoding name (e.g., "utf-8", "ascii", "iso-8859-1") |
str
|
Returns "utf-8" as fallback if detection fails |
Source code in src/pyhfm/utils.py
get_hash(file_path)
Calculate SHA-256 hash of a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the file to hash |
required |
Returns:
| Type | Description |
|---|---|
str
|
Hexadecimal SHA-256 hash string |
Raises:
| Type | Description |
|---|---|
OSError
|
If file cannot be read |
Source code in src/pyhfm/utils.py
set_metadata(table, tbl_meta=None, col_meta=None)
Set metadata on a PyArrow table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
Table
|
PyArrow table to add metadata to |
required |
tbl_meta
|
dict[str, Any] | None
|
Table-level metadata to add |
None
|
col_meta
|
dict[str, Any] | None
|
Column-level metadata to add (column name -> metadata dict) |
None
|
Returns:
| Type | Description |
|---|---|
Table
|
New PyArrow table with metadata attached |