utils#
Small I/O and bookkeeping helpers: atomic JSON writes, boolean-string parsing, and the task-list / run-status tracking that makes long benchmark sweeps resumable.
- medvision_bm.utils.utils.atomic_write_json(json_path, data, indent=4)[source]#
Write
dataas JSON tojson_pathatomically.Writes to a temp file in the same directory, fsyncs it, then
os.replace-es it over the target. If the write fails (e.g. ENOSPC on a full disk), the original file is left untouched instead of being truncated to 0 bytes, which is what opening the target directly with mode"w"would do.- Parameters:
json_path (str) – Destination path for the JSON file.
data – Any JSON-serializable object to write.
indent (int) – Indentation passed to
json.dump. Defaults to4.
- Raises:
BaseException – Re-raises any error raised while writing or replacing the file, after removing the partial temp file.
- medvision_bm.utils.utils.str2bool(v)[source]#
Coerce a string (or bool) to a boolean, for use as an
argparsetype.- Parameters:
v – A boolean, or a string such as
"yes","true","1","no","false", or"0"(case-insensitive).- Returns:
Truefor truthy tokens andFalsefor falsy tokens. A value that is already aboolis returned unchanged.- Return type:
bool
- Raises:
argparse.ArgumentTypeError – If
vis not a recognized boolean token.
- medvision_bm.utils.utils.set_cuda_num_processes()[source]#
Determine the number of GPU processes to launch from the CUDA environment.
Reads
CUDA_VISIBLE_DEVICES. When it is unset, all GPUs reported bytorch.cuda.device_count()are used; otherwise the number of device ids listed in the variable is used (at least 1).- Returns:
The number of processes to run, one per visible GPU.
- Return type:
int
- medvision_bm.utils.utils.update_task_status(json_path, model_name, task_name)[source]#
Mark a (model, task) pair as completed in a JSON tracking file.
Loads the existing status file (creating the parent directory and treating a missing file as empty), sets
data[model_name][task_name]toTrue, and writes the file back atomically.- Parameters:
json_path (str) – Path to the JSON tracking file.
model_name (str) – Model whose status is updated.
task_name (str) – Task to mark as completed.
- Returns:
Always
False; the return value is unused by callers.- Return type:
bool
- medvision_bm.utils.utils.load_tasks(json_file_path)[source]#
Load task names from a JSON file mapping task names to their definitions.
- Parameters:
json_file_path (str) – Path to a JSON file whose top-level keys are task names.
- Returns:
The task names (the top-level keys), in file order.
- Return type:
list
- medvision_bm.utils.utils.load_tasks_status(tasks_status_file, model_name)[source]#
Return the completion-status mapping for a single model.
- Parameters:
tasks_status_file (str) – Path to the JSON status file. A missing file is treated as empty.
model_name (str) – Model whose status entry is returned.
- Returns:
Mapping of task name to completion flag for
model_name, or an empty dict if the file or the model entry is absent.- Return type:
dict
- Raises:
ValueError – If the status file exists but cannot be read or parsed.