Skip to content

Commit b0a8ae3

Browse files
authored
Fix initiating duration arrays with numpy array (#39)
* Fix initiating duration arrays with numpy array * Fix test
1 parent 1266bc4 commit b0a8ae3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

oqpy/classical_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ def __init__(
374374
array_base_type = base_type_instance.type_cls()
375375

376376
# Automatically handle Duration array.
377-
if base_type is DurationVar and kwargs["init_expression"]:
377+
if base_type is DurationVar and kwargs["init_expression"] is not None:
378378
kwargs["init_expression"] = (make_duration(i) for i in kwargs["init_expression"])
379379

380380
super().__init__(

tests/test_directives.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ def test_array_declaration():
163163
multidim = ArrayVar[FloatVar[32], 3, 2](
164164
name="multiDim", init_expression=[[1.1, 1.2], [2.1, 2.2], [3.1, 3.2]]
165165
)
166+
npinit = ArrayVar(
167+
name="npinit",
168+
init_expression=np.array([0, 1, 2, 4]) * 1e-9,
169+
dimensions=[11],
170+
base_type=DurationVar,
171+
)
166172

167-
vars = [b, i, i55, u, x, y, ang, comp, comp55, ang_partial, simple, multidim]
173+
vars = [b, i, i55, u, x, y, ang, comp, comp55, ang_partial, simple, multidim, npinit]
168174

169175
prog = oqpy.Program(version=None)
170176
prog.declare(vars)
@@ -189,6 +195,7 @@ def test_array_declaration():
189195
array[angle[32], 2] ang_part = {pi, pi / 2};
190196
array[float[64], 5] no_init;
191197
array[float[32], 3, 2] multiDim = {{1.1, 1.2}, {2.1, 2.2}, {3.1, 3.2}};
198+
array[duration, 11] npinit = {0.0ns, 1.0ns, 2.0ns, 4.0ns};
192199
i[1] = 0;
193200
i[idx] = val;
194201
"""

0 commit comments

Comments
 (0)