PineForge HPO 1.11.0
Native hyperparameter optimization for PineForge strategies
Loading...
Searching...
No Matches
pineforge::hpo::Dataset Class Referencefinal

Immutable owned or file-mapped OHLCV input shared by independent trial executions. More...

#include <pineforge/hpo/dataset.hpp>

Public Member Functions

 Dataset (std::vector< pf_bar_t > bars)
 Takes ownership of validated bars.
 
const pf_bar_t * data () const noexcept
 Returns a const pointer suitable for PineForge backtest calls.
 
std::size_t size () const noexcept
 Returns the number of bars.
 
bool empty () const noexcept
 Returns whether the dataset contains no bars.
 
const std::vector< pf_bar_t > & bars () const
 Returns the owning vector for CSV or vector-backed input.
 
void verify_unchanged () const
 Compares the mapped file's open descriptor's st_dev, st_ino, st_size, st_mtim and st_ctim to their load-time values, using full seconds+nanoseconds timestamps.
 

Static Public Member Functions

static Dataset load_csv (const std::filesystem::path &path)
 Loads timestamp,open,high,low,close,volume columns from a CSV file.
 
static Dataset load_bin (const std::filesystem::path &path, BinaryVerification verification=BinaryVerification::Full)
 Maps a headerless little-endian pf_bar_t/v1 file read-only, without copying its bars.
 

Detailed Description

Immutable owned or file-mapped OHLCV input shared by independent trial executions.

The public API intentionally exposes only const access even though the legacy PineForge C ABI accepts pf_bar_t* rather than const pf_bar_t*. Timestamps must be strictly increasing and owned OHLCV values must be finite. Binary inputs follow the engine's volume contract: nonnegative or NaN, with finite OHLC prices. Never rewrite a mapped file in place: publish by rename/os.replace. In-place rewriting or truncation is unsupported. The native runner compares the open descriptor's st_dev, st_ino, st_size, st_mtim and st_ctim against their load-time values at the end of a run, including the timeout path, before publishing results. Timestamps use the full seconds+nanoseconds values the filesystem reports. Changes to device, inode, size or mtime are refused. When only ctime moves (chmod, chown, xattr, an extra hard link, or rename-over), the mapped bytes are re-hashed against the sidecar digest parsed at load and accepted only if equal; without that digest a ctime change is refused. In Sidecar mode the bytes were not compared to the digest at load, so even a metadata-only change can refuse when the re-hash finds that they never matched. The run's results are computed only from the bytes verified at load: a change that the mapped bytes show is refused (the held mapping is re-hashed against the sidecar digest when ctime moved). On platforms where a private read-only mapping does not show later in-place writes (observed on macOS), the run keeps using the original verified bytes and is accepted. This also applies to a same-size rewrite followed by exact mtime restoration (touch -r, os.utime with ns=): POSIX writes and mtime updates advance ctime. Residual: a rewrite that is fully restored (content and mtime) before the end-of-run re-hash can go undetected if trials read the changed pages in between; the guard covers changes still present at the end of the run, and the scenario needs a deliberate local writer. Detection resolution is the filesystem's timestamp granularity: coarse ctime can miss a rewrite within one tick that keeps size, mtime and ctime unchanged. Truncation can cause an unrecoverable SIGBUS before that check and terminate the process without failure JSON.

Definition at line 44 of file dataset.hpp.

Constructor & Destructor Documentation

◆ Dataset()

pineforge::hpo::Dataset::Dataset ( std::vector< pf_bar_t >  bars)
explicit

Takes ownership of validated bars.

Exceptions
std::invalid_argumentfor non-finite values or non-increasing timestamps.

Member Function Documentation

◆ bars()

const std::vector< pf_bar_t > & pineforge::hpo::Dataset::bars ( ) const

Returns the owning vector for CSV or vector-backed input.

Exceptions
std::logic_errorfor mapped input; use data()/size() for either format.

◆ data()

const pf_bar_t * pineforge::hpo::Dataset::data ( ) const
inlinenoexcept

Returns a const pointer suitable for PineForge backtest calls.

Definition at line 75 of file dataset.hpp.

◆ empty()

bool pineforge::hpo::Dataset::empty ( ) const
inlinenoexcept

Returns whether the dataset contains no bars.

Definition at line 81 of file dataset.hpp.

References size().

◆ load_bin()

static Dataset pineforge::hpo::Dataset::load_bin ( const std::filesystem::path &  path,
BinaryVerification  verification = BinaryVerification::Full 
)
static

Maps a headerless little-endian pf_bar_t/v1 file read-only, without copying its bars.

Requires a regular file containing 1..2147483647 records of 48 bytes and an appended regular .json sidecar (at most 1 MiB) with format pf_bar_t/v1, integer record_size 48, string symbol, canonical tf matching [1-9][0-9]{0,4}|[1-9][0-9]{0,3}[DWMS], integer count equal to file size/48, and integer first_ts/last_ts equal to the mapped endpoints. Booleans are not integers. sha256 must contain exactly 64 lowercase hexadecimal characters. Full verification (the default) compares SHA-256 of the entire mapped file; Sidecar skips only the digest comparison. Both modes scan values and timestamp order before returning. Symbol/tf are descriptive, not compared to the run configuration; extra keys are allowed. Copies share ownership of the mapping and its open descriptor until the last is freed. Publish files by write-then-rename, never by rewriting or truncating a mapped file.

Exceptions
std::runtime_errorfor invalid files, sidecars, bars, or a big-endian host.

◆ load_csv()

static Dataset pineforge::hpo::Dataset::load_csv ( const std::filesystem::path &  path)
static

Loads timestamp,open,high,low,close,volume columns from a CSV file.

Header matching is ASCII case-insensitive, blank data lines are ignored, and quoted CSV fields are supported. The result contains at least one bar.

Exceptions
std::runtime_errorwhen the file cannot be read or its CSV data is invalid.
std::invalid_argumentwhen parsed bars violate Dataset invariants.

◆ size()

std::size_t pineforge::hpo::Dataset::size ( ) const
inlinenoexcept

Returns the number of bars.

Definition at line 79 of file dataset.hpp.

Referenced by empty().

◆ verify_unchanged()

void pineforge::hpo::Dataset::verify_unchanged ( ) const

Compares the mapped file's open descriptor's st_dev, st_ino, st_size, st_mtim and st_ctim to their load-time values, using full seconds+nanoseconds timestamps.

A no-op for owned input. Changes to device, inode, size or mtime are refused. If only ctime moves (chmod, chown, xattr, an extra hard link, or rename-over), re-hashes the held mapped bytes against the sidecar digest parsed at load and accepts only equality; without a digest a ctime change is refused. Replacing the path never reopens the new file. In Sidecar mode the bytes were not compared to the digest at load, so a ctime-only change can refuse if the bytes never matched it. The run's results are computed only from the bytes verified at load: a change that the mapped bytes show is refused (the held mapping is re-hashed against the sidecar digest when ctime moved). On platforms where a private read-only mapping does not show later in-place writes (observed on macOS), the run keeps using the original verified bytes and is accepted. This also applies to a same-size rewrite followed by exact mtime restoration, as POSIX writes and mtime updates advance ctime. Detection resolution is the filesystem's timestamp granularity: coarse ctime can miss a rewrite within one tick that keeps size, mtime and ctime unchanged. Truncation can cause an unrecoverable SIGBUS before the check.

Exceptions
std::runtime_errorwith code hpo_dataset_invalid when the file changed while mapped or its descriptor cannot be checked.

The documentation for this class was generated from the following file: