This is an automated email from the ASF dual-hosted git repository.
yhu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new de9204989d6 Fix PostCommit Python Examples Spark/Direct jobs (#33781)
de9204989d6 is described below
commit de9204989d66c920b31cd6758e362994e670fb34
Author: Vitaly Terentyev <[email protected]>
AuthorDate: Tue Jan 28 20:56:52 2025 +0400
Fix PostCommit Python Examples Spark/Direct jobs (#33781)
* Fix distribopt_test.py for NumPy 2
* Fix distribopt.py for NumPy 2
---
sdks/python/apache_beam/examples/complete/distribopt.py | 17 ++++++++++++++++-
.../apache_beam/examples/complete/distribopt_test.py | 5 ++++-
2 files changed, 20 insertions(+), 2 deletions(-)
diff --git a/sdks/python/apache_beam/examples/complete/distribopt.py
b/sdks/python/apache_beam/examples/complete/distribopt.py
index 89c312fcbf5..304a89cd100 100644
--- a/sdks/python/apache_beam/examples/complete/distribopt.py
+++ b/sdks/python/apache_beam/examples/complete/distribopt.py
@@ -221,13 +221,28 @@ class OptimizeGrid(beam.PTransform):
# Run L-BFGS-B optimizer
result = minimize(lambda x: np.sum(sim.simulate(x)), x0, bounds=bounds)
- return result.x.tolist(), sim.simulate(result.x)
+
+ # Ensure result.x is always a list, regardless of NumPy version
+ x_values = result.x if isinstance(result.x, list) else result.x.tolist()
+
+ # Ensure simulation output is also properly converted
+ costs = sim.simulate(result.x)
+ costs = costs if isinstance(costs, list) else costs.tolist()
+
+ return x_values, costs
def process(self, element):
mapping_identifier, greenhouse = element[0]
crops, quantities = zip(*element[1])
sim = Simulator(quantities)
optimum, costs = self._optimize_production_parameters(sim)
+
+ # Ensure NumPy arrays are converted to lists before yielding
+ if isinstance(optimum, np.ndarray):
+ optimum = optimum.tolist()
+ if isinstance(costs, np.ndarray):
+ costs = costs.tolist()
+
solution = (mapping_identifier, (greenhouse, optimum))
yield pvalue.TaggedOutput('solution', solution)
for crop, cost, quantity in zip(crops, costs, quantities):
diff --git a/sdks/python/apache_beam/examples/complete/distribopt_test.py
b/sdks/python/apache_beam/examples/complete/distribopt_test.py
index b9d50741026..a7b02d6a25d 100644
--- a/sdks/python/apache_beam/examples/complete/distribopt_test.py
+++ b/sdks/python/apache_beam/examples/complete/distribopt_test.py
@@ -77,8 +77,11 @@ class DistribOptimizationTest(unittest.TestCase):
# Only 1 result
self.assertEqual(len(lines), 1)
+ # Handle NumPy string representation before parsing
+ cleaned_line = lines[0].replace("np.str_('", "'").replace("')", "'")
+
# parse result line and verify optimum
- optimum = make_tuple(lines[0])
+ optimum = make_tuple(cleaned_line)
self.assertAlmostEqual(optimum['cost'], 454.39597, places=3)
self.assertDictEqual(optimum['mapping'], EXPECTED_MAPPING)
production = optimum['production']