Skip to content

Commit f4b2506

Browse files
author
jngaravitoc
committed
adding basis utils
1 parent a5bc29e commit f4b2506

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

EXPtools/utils/basis.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
TODO: Add plotting functions
3+
"""
4+
5+
import os
6+
import yaml
7+
import pyEXP
8+
9+
def write_basis(basis, basis_name):
10+
"""
11+
Write a basis configuration dictionary to a YAML file.
12+
13+
Parameters
14+
----------
15+
basis : dict
16+
Dictionary containing the basis configuration.
17+
conf_name : str
18+
Name of the YAML file to write. If the provided name does not
19+
end with `.yaml`, the extension is automatically appended.
20+
21+
Returns
22+
-------
23+
str
24+
The final filename used to save the YAML configuration.
25+
"""
26+
# Ensure file has .yaml extension
27+
if not basis_name.endswith(".yaml"):
28+
basis_name += ".yaml"
29+
30+
# Write to file
31+
with open(basis_name, "w") as file:
32+
yaml.dump(basis, file, default_flow_style=False, sort_keys=False)
33+
34+
return 0
35+
36+
def load_basis(conf_name):
37+
"""
38+
Load a basis configuration from a YAML file and initialize a Basis object.
39+
40+
Parameters
41+
----------
42+
conf_name : str
43+
Path to the YAML configuration file. If the provided filename does not
44+
end with `.yaml`, the extension is automatically appended.
45+
46+
Returns
47+
-------
48+
basis : pyEXP.basis.Basis
49+
An initialized Basis object created from the configuration.
50+
51+
Raises
52+
------
53+
FileNotFoundError
54+
If the specified YAML file does not exist.
55+
"""
56+
57+
# Check file existence
58+
if not os.path.exists(conf_name):
59+
raise FileNotFoundError(f"Configuration file not found: {conf_name}")
60+
61+
# Load YAML safely
62+
with open(conf_name, "r") as f:
63+
config = yaml.load(f, Loader=yaml.FullLoader)
64+
65+
# Build basis from configuration
66+
basis = pyEXP.basis.Basis.factory(config)
67+
return basis
68+
69+

0 commit comments

Comments
 (0)