Result files¶
save_result(result, path) writes a FieldResult to a versioned JSON
file and load_result(path) reads it back; world_fingerprint(world)
is the hash of a world that every solve records in its provenance. A
parameter study can therefore store each solve and later check which
world, engine and package versions produced it.
Format¶
One UTF-8 JSON document (standard JSON: no NaN / Infinity tokens,
no pickle) with an envelope:
{
"format": "groundfield.result",
"schema_version": 1,
"written_by": "groundfield <version>",
"provenance": {"groundfield_version": "...", "engine": {...}, "world_fingerprint": "...", "solved_at_utc": "..."},
"result": {...}
}
result is the lossless JSON of the FieldResult: complex numbers are
strings, non-finite floats of typed fields are "Infinity" / "NaN"
strings, and metadata values JSON cannot express are tagged objects
(arrays with dtype and shape, tuples, complex numbers). provenance
repeats the solve provenance (groundfield, numpy, scipy, pydantic and
Python versions, platform, engine configuration, world fingerprint, UTC
time stamp) so it can be read without parsing the result.
Compatibility. load_result reads every schema version up to
RESULT_SCHEMA_VERSION and raises ResultFormatError for a newer one
instead of guessing. The loader reads only what the writer writes
(plain numeric and string arrays with bounded size and nesting), so a
file from an untrusted source cannot make it allocate arbitrary memory
or construct objects. Files are written atomically; overwrite=False
(default) refuses to replace an existing file.
The world is not part of the file. Save it alongside with
world.model_dump_json() (exact round trip) and compare it with
world_fingerprint(world) against provenance["world_fingerprint"].
Example¶
import groundfield as gf
world = gf.create_world(soil=gf.HomogeneousSoil(rho=100.0))
gf.create_electrode(world, "rod", name="g1", position=(0.0, 0.0, 0.0), length=3.0)
gf.create_source(world, attached_to="g1", magnitude=1.0)
result = gf.create_engine(frequencies=[50.0, 1000.0]).solve(world)
path = gf.save_result(result, "rod.result.json")
loaded = gf.load_result(path)
assert loaded.input_impedance() == result.input_impedance() # bit-identical
assert loaded.metadata["provenance"]["world_fingerprint"] == gf.world_fingerprint(world)
API reference¶
results ¶
Versioned on-disk format of a FieldResult.
A result file is one UTF-8 JSON document (standard JSON, no NaN /
Infinity tokens, no pickle) with an envelope::
{
"format": "groundfield.result",
"schema_version": 1,
"written_by": "groundfield <version>",
"provenance": {...}, # FieldResult.metadata["provenance"] or null
"result": {...} # FieldResult JSON (lossless, see below)
}
provenance repeats the solve provenance of the result
(groundfield / numpy / scipy / Python versions, platform, engine
configuration, world_fingerprint and the UTC timestamp), so it can
be read without parsing the result. It is null for results that
were not produced by Engine.solve (hand-built results).
The result body is FieldResult.model_dump_json(), which
round-trips losslessly: complex numbers are strings, non-finite floats
of typed fields are "Infinity" / "NaN" strings, and the
metadata uses tagged objects for values JSON cannot express (see
groundfield.solver.result).
Schema versions
schema_version increases whenever a reader of the previous version
could misread a file. load_result() reads every version up to
RESULT_SCHEMA_VERSION and raises ResultFormatError for
a newer one, instead of guessing.
The world itself is not part of the file; save it alongside with
world.model_dump_json() (exact round trip) and compare it with
world_fingerprint() against provenance["world_fingerprint"].
RESULT_FORMAT
module-attribute
¶
Value of the format key of a result file.
RESULT_SCHEMA_VERSION
module-attribute
¶
Schema version written by save_result().
ResultFormatError ¶
Bases: ValueError
A file is not a readable groundfield result file.
load_result ¶
Read a result file written by save_result().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
File to read. |
required |
Returns:
| Type | Description |
|---|---|
FieldResult
|
The stored result, equal to the one that was saved. |
Raises:
| Type | Description |
|---|---|
ResultFormatError
|
If the file is not UTF-8 JSON (or nests too deeply for the JSON
parser), not a groundfield result file, uses a schema version
newer than |
ValidationError
|
If the |
Notes
The reader accepts only what save_result() writes, so a file
from an untrusted source cannot make it allocate more than a small
multiple of the file size or create object, datetime or void
arrays. No pickle is involved at any point.
Source code in src/groundfield/io/results.py
save_result ¶
save_result(result: FieldResult, path: str | PathLike[str], *, overwrite: bool = False, indent: int | None = None) -> Path
Write a result to a versioned JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
FieldResult
|
The result to store. |
required |
path
|
str | PathLike[str]
|
Target file. The parent directory must exist. |
required |
overwrite
|
bool
|
Replace an existing file. Default |
False
|
indent
|
int | None
|
Indentation of the JSON text; |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
The written path. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
FileExistsError
|
If |
ValueError
|
If the result holds values that cannot be stored (the message names the metadata key). |
Notes
The file is written to a temporary file in the target directory and
moved into place with os.replace(), so an interrupted write
never leaves a truncated result file behind. Its permissions are
those of a plain open(path, "w"): a new file gets 0o666
reduced by the process umask (0o644 under the usual 022), a
replaced file keeps its mode.
Source code in src/groundfield/io/results.py
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 | |
world_fingerprint ¶
SHA-256 fingerprint of the canonical JSON of a world.
The canonical form is json.dumps(world.model_dump(mode="json"),
sort_keys=True, separators=(",", ":")). Pydantic writes floats in
their shortest round-trip form, so equal worlds give equal
fingerprints, also after a JSON round trip of the world. The
fingerprint depends on the world schema of the installed version:
renamed or added model fields change it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
world
|
World
|
The world to fingerprint. |
required |
Returns:
| Type | Description |
|---|---|
str
|
|