Skip to content

Commit 5150175

Browse files
authored
Fix fractfact_opt for Python3 (#10)
* Add issue #9 test * Fix integer division * Fix np.array data hashing * Assert returned value
1 parent 766e810 commit 5150175

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

pyDOE3/doe_factorial.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def fracfact_opt(n_factors, n_erased, max_attempts=0):
406406
def n_comb(n, k):
407407
if k <= 0 or n <= 0 or k > n:
408408
return 0
409-
return math.factorial(n) / (math.factorial(k) * math.factorial(n - k))
409+
return math.factorial(n) // (math.factorial(k) * math.factorial(n - k))
410410

411411
if n_factors > 20:
412412
raise ValueError("Design too big, use 20 factors or less")
@@ -503,8 +503,8 @@ def fracfact_aliasing(design):
503503
for combination in all_combinations:
504504
contrast = np.prod(design[:, combination], axis=1)
505505
contrast.flags.writeable = False
506-
aliases[contrast.data] = aliases.get(contrast.data, [])
507-
aliases[contrast.data].append(combination)
506+
aliases[contrast.data.tobytes()] = aliases.get(contrast.data.tobytes(), [])
507+
aliases[contrast.data.tobytes()].append(combination)
508508

509509
aliases_list = []
510510
for alias in aliases.values():

tests/test_factorial.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from pyDOE3.doe_factorial import fullfact
2+
from pyDOE3.doe_factorial import fracfact_opt, fullfact
33
from pyDOE3.doe_factorial import ff2n
44
from pyDOE3.doe_factorial import fracfact
55
from pyDOE3.doe_factorial import fracfact_by_res
@@ -98,3 +98,23 @@ def test_factorial6(self):
9898
]
9999
actual = fracfact_by_res(6, 3)
100100
np.testing.assert_allclose(actual, expected)
101+
102+
def test_issue_9(self):
103+
ffo_doe = fracfact_opt(4, 1)
104+
self.assertEqual(ffo_doe[0], "a b c abc")
105+
self.assertEqual(
106+
ffo_doe[1],
107+
[
108+
"a = bcd",
109+
"b = acd",
110+
"c = abd",
111+
"d = abc",
112+
"ab = cd",
113+
"ac = bd",
114+
"ad = bc",
115+
"abcd",
116+
],
117+
)
118+
np.testing.assert_array_equal(
119+
ffo_doe[2], np.array([0.0, 0.0, 3.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0])
120+
)

0 commit comments

Comments
 (0)