Coverage Control Library
Loading...
Searching...
No Matches
io_utils.py
Go to the documentation of this file.
1# This file is part of the CoverageControl library
2#
3# Author: Saurav Agarwal
4# Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
5# Repository: https://github.com/KumarRobotics/CoverageControl
6#
7# Copyright (c) 2024, Saurav Agarwal
8#
9# The CoverageControl library is free software: you can redistribute it and/or
10# modify it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or (at your
12# option) any later version.
13#
14# The CoverageControl library is distributed in the hope that it will be
15# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17# Public License for more details.
18#
19# You should have received a copy of the GNU General Public License along with
20# CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
21
22
24
25"""
26The module provides utility functions for loading data from files
27"""
28
29import os
30import sys
31import pathlib
32
33import torch
34import yaml
35
36if sys.version_info[1] < 11:
37 import tomli as tomllib
38else:
39 import tomllib
40
41
42class IOUtils:
43 """
44 Class provides the following utility functions:
45 - load_tensor
46 - load_yaml
47 - load_toml
48 """
49
50 @staticmethod
51 def sanitize_path(path_str: str) -> str:
52 """
53 Function to sanitize a path string
54 """
55
56 return os.path.normpath(os.path.expanduser(os.path.expandvars(path_str)))
57
58 @staticmethod
59 def load_tensor(path: (str, pathlib.Path)) -> torch.tensor:
60 """
61 Function to load a tensor from a file
62 Can load tensors from jit script format files
63
64 Args:
65 path (str): Path to the file
66
67 Returns:
68 tensor: The loaded tensor
69 None: If the file does not exist
70
71 Raises:
72 FileNotFoundError: If the file does not exist
73 """
74
75 if isinstance(path, pathlib.Path):
76 path = str(path)
77 # Throw error if path does not exist
78 path = IOUtils.sanitize_path(path)
79
80 if not os.path.exists(path):
81 raise FileNotFoundError(f"IOUtils::load_tensor: File not found: {path}")
82 # Load data
83 data = torch.load(path, weights_only=True)
84 # Extract tensor if data is in jit script format
85
86 if isinstance(data, torch.jit.ScriptModule):
87 tensor = list(data.parameters())[0]
88 else:
89 tensor = data
90
91 return tensor
92
93 @staticmethod
94 def load_yaml(path: str) -> dict:
95 """
96 Function to load a yaml file
97
98 Args:
99 path (str): Path to the file
100
101 Returns:
102 data: The loaded data
103
104 Raises:
105 FileNotFoundError: If the file does not exist
106 """
107
108 path = IOUtils.sanitize_path(path)
109 # Throw error if path does not exist
110
111 if not os.path.exists(path):
112 raise FileNotFoundError(f"IOUtils::load_yaml File not found: {path}")
113 # Load data
114 with open(path, "rb") as f:
115 data = yaml.load(f, Loader=yaml.FullLoader)
116
117 return data
118
119 @staticmethod
120 def load_toml(path: str) -> dict: # Throw error if path does not exist
121 """
122 Function to load a toml file
123 """
124 path = IOUtils.sanitize_path(path)
126 if not os.path.exists(path):
127 raise FileNotFoundError(f"IOUtils::load_toml: File not found: {path}")
128 # Load data
129 with open(path, "rb") as f:
130 data = tomllib.load(f)
131
132 return data
Class provides the following utility functions:
Definition io_utils.py:50
str sanitize_path(str path_str)
Function to sanitize a path string.
Definition io_utils.py:56
torch.tensor load_tensor((str, pathlib.Path) path)
Function to load a tensor from a file Can load tensors from jit script format files.
Definition io_utils.py:75
dict load_toml(str path)
Function to load a toml file.
Definition io_utils.py:125
dict load_yaml(str path)
Function to load a yaml file.
Definition io_utils.py:108