Skip to content

Commit 5a2e722

Browse files
committed
Added ability to export as xarray.
1 parent 2e8ed90 commit 5a2e722

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/pystrata/logic_tree.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from typing import Any, Union
99

1010
import numpy as np
11+
import xarray as xr
1112

1213
AlternativeValueType = Union[str, int, float, tuple[str]]
1314

@@ -151,6 +152,22 @@ def __iter__(self):
151152
if a.weight > 0:
152153
yield Realization(self.name, a.value, a.weight, a.params)
153154

155+
def to_xarray(self, dim_name: str, name: str = "") -> xr.DataArray:
156+
"""
157+
Convert the node to a numpy array.
158+
159+
Returns
160+
-------
161+
xr.DataArray
162+
A dataarray of the alternative values.
163+
"""
164+
return xr.DataArray(
165+
np.array([a.weight for a in self.alts]),
166+
dims=(dim_name,),
167+
coords={dim_name: [a.value for a in self.alts]},
168+
name=name,
169+
)
170+
154171
@property
155172
def options(self):
156173
"""

0 commit comments

Comments
 (0)