MCPcopy Create free account
hub / github.com/Project-MONAI/MONAI / load_bundle_config

Function load_bundle_config

monai/bundle/utils.py:171–243  ·  view source on GitHub ↗

Load the metadata and nominated configuration files from a MONAI bundle without loading the network itself. This function will load the information from the bundle, which can be a directory or a zip file containing a directory or a Torchscript bundle, and return the parser object with

(bundle_path: str, *config_names: str, **load_kw_args: Any)

Source from the content-addressed store, hash-verified

169
170
171def load_bundle_config(bundle_path: str, *config_names: str, **load_kw_args: Any) -> Any:
172 """
173 Load the metadata and nominated configuration files from a MONAI bundle without loading the network itself.
174
175 This function will load the information from the bundle, which can be a directory or a zip file containing a
176 directory or a Torchscript bundle, and return the parser object with the information. This saves having to load
177 the model if only the information is wanted, and can work on any sort of bundle format.
178
179 Args:
180 bundle_path: path to the bundle directory or zip file
181 config_names: names of configuration files with extensions to load, should not be full paths but just name+ext
182 load_kw_args: keyword arguments to pass to the ConfigParser object when loading
183
184 Returns:
185 ConfigParser object containing the parsed information
186 """
187
188 from monai.bundle.config_parser import ConfigParser # avoids circular import
189
190 parser = ConfigParser()
191
192 if not os.path.exists(bundle_path):
193 raise ValueError(f"Cannot find bundle file/directory '{bundle_path}'")
194
195 # bundle is a directory, read files directly
196 if os.path.isdir(bundle_path):
197 conf_data = []
198 parser.read_meta(f=os.path.join(bundle_path, "configs", "metadata.json"), **load_kw_args)
199
200 for cname in config_names:
201 cpath = os.path.join(bundle_path, "configs", cname)
202 if not os.path.exists(cpath):
203 raise ValueError(f"Cannot find config file '{cpath}'")
204
205 conf_data.append(cpath)
206
207 parser.read_config(f=conf_data, **load_kw_args)
208 else:
209 # bundle is a zip file which is either a zipped directory or a Torchscript archive
210
211 name, _ = os.path.splitext(os.path.basename(bundle_path))
212
213 archive = zipfile.ZipFile(bundle_path, "r")
214
215 all_files = archive.namelist()
216
217 zip_meta_name = f"{name}/configs/metadata.json"
218
219 if zip_meta_name in all_files:
220 prefix = f"{name}/configs/" # zipped directory location for files
221 else:
222 zip_meta_name = f"{name}/extra/metadata.json"
223 prefix = f"{name}/extra/" # Torchscript location for files
224
225 meta_json = json.loads(archive.read(zip_meta_name))
226 parser.read_meta(f=meta_json)
227
228 for cname in config_names:

Callers 3

test_load_config_dirMethod · 0.90
test_load_config_zipMethod · 0.90
test_load_config_tsMethod · 0.90

Calls 5

read_metaMethod · 0.95
read_configMethod · 0.95
ConfigParserClass · 0.90
appendMethod · 0.45
readMethod · 0.45

Tested by 3

test_load_config_dirMethod · 0.72
test_load_config_zipMethod · 0.72
test_load_config_tsMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…