geotrax.utils.config_utils¶
Loading the pipeline config and applying command-line overrides to it, plus resolving the detector model and its class names. Every key of the config is described in the config reference.
Loading configs¶
load_config
¶
Load a pipeline config file as a nested dict.
A file that is missing, unreadable, malformed or not a mapping stops the run with a named error
here, instead of an opaque traceback later from whichever stage first reads a missing section.
A key the file leaves out takes its value from the bundled default.yaml, so a config may hold
only the keys it changes, and a copy made before an upgrade keeps working with the keys added
since. Without that, a missing config-only key failed with a KeyError midway through a run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg_filepath
|
str | Path
|
Config path or bundled preset name, resolved with
|
required |
logger
|
Logger
|
Receives the errors and the applied overrides. |
required |
args
|
Namespace | None
|
Parsed CLI arguments whose |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The config, completed from the bundled defaults, with the overrides applied. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
The file cannot be loaded as a config, or an override is invalid. |
Examples:
Source code in geotrax/utils/config_utils.py
load_config_all
¶
Load the pipeline config for a stage, with every CLI override applied, and split it for its consumers.
The overrides are applied before the split, so every consumer and the saved run metadata see the
configuration the run actually used: --set first, then the dedicated flags through
sync_args_with_config. A scalar
ultralytics.classes is wrapped in a list, so consumers can always iterate it (or test it for
None), and ultralytics keys that Ultralytics has removed, such as keras in an older copied
config, are left out with a warning.
With needs_model=True, the model reference is resolved (and downloaded if needed), the class
names are resolved, and the active tracker block is written to a temporary YAML file, since
Ultralytics takes the tracker config as a file path. The file is removed at interpreter exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Parsed CLI arguments; |
required |
logger
|
Logger
|
A logger from |
required |
needs_model
|
bool
|
Set to |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
A dict with the keys |
Raises:
| Type | Description |
|---|---|
SystemExit
|
The config cannot be loaded, an override is invalid, or the model or tracker cannot be resolved. |
Source code in geotrax/utils/config_utils.py
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 | |
resolve_config_path
¶
Resolve a -c value to a config file.
Tries, in order, the path as given, the path relative to
ROOT_DIR, and the bundled geotrax/cfg/ folder. A
missing .yaml suffix and a legacy leading cfg/ are tolerated, so confident,
cfg/default.yaml and lenient.yaml all name bundled presets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg_filepath
|
str | Path
|
Config path or bundled preset name. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The first candidate that is a file, or |
Source code in geotrax/utils/config_utils.py
merge_bundled_defaults
¶
Fill the keys missing from a config block with the values from the bundled default.yaml.
load_config already fills every missing key this
way; this function gives the same fallback to a block read from a config dict that did not come
from it, such as extraction.sahi, which is read as a whole.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
section
|
dict | None
|
The block from the loaded config, possibly |
required |
path
|
str
|
Dotted path of the block, e.g. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A new dict with the bundled defaults overlaid by |
Source code in geotrax/utils/config_utils.py
Overrides¶
apply_cli_overrides
¶
apply_cli_overrides(cfg: dict, overrides: list | None, args: Namespace | None, logger: Logger) -> None
Apply --set KEY=VALUE overrides to a loaded config, in place.
KEY is matched against the loaded config, so a custom config with extra keys works too. It is
a full dotted path (ultralytics.conf) or any unambiguous tail of one (conf, matching.gpu).
VALUE is parsed as YAML and must be compatible with the type of the current value; ints and
floats are interchangeable, 0/1 are accepted for booleans, and a null key takes anything.
Nothing is written until every override is valid, so a rejected command line leaves the config
untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg
|
dict
|
Full loaded config, updated in place. |
required |
overrides
|
list | None
|
The |
required |
args
|
Namespace | None
|
Parsed CLI arguments. When they carry the parser's |
required |
logger
|
Logger
|
A logger from |
required |
Raises:
| Type | Description |
|---|---|
SystemExit
|
A key is unknown (close matches are suggested), ambiguous, a whole section, or also set by its dedicated flag, or a value has the wrong type. |
Examples:
cfg = load_config("default", logger)
apply_cli_overrides(cfg, ["conf=0.35", "matching.gpu=true"], None, logger)
Source code in geotrax/utils/config_utils.py
sync_args_with_config
¶
Reconcile the parsed CLI arguments with the config, in both directions.
Every config-backed flag defaults to None, so for each entry of the args._cfg_paths map
that the parser recorded:
- an argument that is still
Noneis filled from the config, or from the bundleddefault.yamlwhen a custom config lacks the key; - an argument that was given is written into the config.
The second direction makes the config dict authoritative: everything downstream, the saved
run-metadata YAML included, sees the configuration the run actually used. Flags marked
no_sync are skipped, since their own resolvers handle precedence. A dest listed in
args._cfg_pinned is always written, even when None; that is how batch clears
cut_frame_right in directory mode without the next stage reading it back from the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Parsed CLI arguments, as returned by the stage's |
required |
cfg
|
dict
|
Full loaded config, updated in place. |
required |
logger
|
Logger
|
Receives a line for each argument written into the config. |
required |
Source code in geotrax/utils/config_utils.py
backfill_args_from_config
¶
Fill every argument that was not given on the command line from a mapping.
Used by the tools/ scripts. The pipeline stages use
sync_args_with_config instead, which
also writes the given arguments back into the config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
args
|
Namespace
|
Parsed CLI arguments; an argument still |
required |
mapping
|
dict
|
Argument name to fallback value, usually read from a config section. |
required |
Source code in geotrax/utils/config_utils.py
Models and class names¶
resolve_model_path
¶
Resolve an extraction.model / --model value to a local weights file, downloading it if needed.
A Hugging Face reference is downloaded once into the standard Hub cache
(~/.cache/huggingface/hub, relocatable with HF_HOME or HF_HUB_CACHE) and revalidated by
etag on later runs, so it is not downloaded again. A local path is resolved with
resolve_asset_path and never downloaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_ref
|
str | Path
|
|
required |
logger
|
Logger
|
A logger from |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Local path of the weights. |
Raises:
| Type | Description |
|---|---|
SystemExit
|
A malformed reference, a failed download, or |
Examples:
resolve_model_path("hf://rfonod/geo-trax/geotrax_hbb_yolov8s_1920_v1.pt", logger)
resolve_model_path("hf://rfonod/geo-trax@v1/geotrax_hbb_yolov8s_1920_v1.pt", logger)
resolve_model_path("models/custom.pt", logger)
Source code in geotrax/utils/config_utils.py
resolve_class_names
¶
resolve_class_names(model_path: Path, cli_value: list | str | None, cfg_value: dict | str | None, classes: list | None, logger: Logger) -> tuple
Resolve the class-id to name mapping by precedence: CLI, config, model, then integer labels.
An override that cannot be read is logged and skipped, falling through to the next source.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
Path
|
Local weights file, read when neither override is given. |
required |
cli_value
|
list | str | None
|
|
required |
cfg_value
|
dict | str | None
|
|
required |
classes
|
list | None
|
The |
required |
logger
|
Logger
|
Receives the chosen source, or a warning on the integer fallback. |
required |
Returns:
| Type | Description |
|---|---|
tuple
|
|
Source code in geotrax/utils/config_utils.py
load_class_names_from_model
¶
Read the class names embedded in a YOLO weights file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_path
|
Path
|
Local weights file. |
required |
logger
|
Logger
|
Receives the outcome. |
required |
Returns:
| Type | Description |
|---|---|
dict | None
|
Class id to name, or |
Source code in geotrax/utils/config_utils.py
HF_PREFIX
module-attribute
¶
Scheme of a Hugging Face Hub model reference, e.g. hf://rfonod/geo-trax/geotrax_hbb_yolov8s_1920_v1.pt.
Paths¶
resolve_asset_path
¶
Resolve a relative asset path, such as local model weights, against the working directory and ROOT_DIR.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str | Path
|
Asset path. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
The path relative to |
Source code in geotrax/utils/config_utils.py
ROOT_DIR
module-attribute
¶
Parent of the geotrax package: the repository root in a source checkout, site-packages when installed.