Skip to content

Commit cf22772

Browse files
committed
Run both Khiops and Khiops Coclustering with MPI
closes #380
1 parent 12c99a2 commit cf22772

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

khiops/core/internals/runner.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,8 @@ def raw_run(self, tool_name, command_line_args=None, use_mpi=True, trace=False):
11191119
)
11201120

11211121
# Build command line arguments
1122-
# Nota: Khiops Coclustering is executed without MPI
11231122
khiops_process_args = []
1124-
if tool_name == "khiops" and use_mpi:
1123+
if use_mpi:
11251124
khiops_process_args += self._mpi_command_args
11261125
khiops_process_args += [self._tool_path(tool_name)]
11271126
if command_line_args:

tests/test_khiops_integrations.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# which is available at https://spdx.org/licenses/BSD-3-Clause-Clear.html or #
55
# see the "LICENSE.md" file for more details. #
66
######################################################################################
7-
"""Tests for executing fit multiple times on multi-table data"""
7+
"""Various integration tests"""
88

99
import os
1010
import platform
@@ -13,8 +13,10 @@
1313
import subprocess
1414
import tempfile
1515
import unittest
16+
from unittest.mock import MagicMock, patch
1617

1718
import khiops.core as kh
19+
import khiops.core.internals.filesystems as fs
1820
from khiops.core.exceptions import KhiopsEnvironmentError
1921
from khiops.core.internals.runner import KhiopsLocalRunner
2022
from khiops.extras.docker import KhiopsDockerRunner
@@ -208,6 +210,67 @@ def test_runner_environment_initialization(self):
208210

209211
self.assertEqual(env_khiops_api_mode, "true")
210212

213+
def test_khiops_and_khiops_coclustering_are_run_with_mpi(self):
214+
"""Test that MODL and MODL_Coclustering are run with MPI"""
215+
216+
# Get current runner
217+
runner = kh.get_runner()
218+
219+
# Get path to the Iris dataset
220+
iris_data_dir = fs.get_child_path(runner.samples_dir, "Iris")
221+
222+
# Create the subprocess.Popen mock
223+
mock_popen = MagicMock()
224+
mock_popen.return_value.__enter__.return_value.communicate.return_value = (
225+
b"",
226+
b"",
227+
)
228+
mock_popen.return_value.__enter__.return_value.returncode = 0
229+
230+
# Run Khiops through an API function, using the mocked Popen, to capture
231+
# its arguments
232+
with patch("subprocess.Popen", mock_popen):
233+
kh.check_database(
234+
fs.get_child_path(iris_data_dir, "Iris.kdic"),
235+
"Iris",
236+
fs.get_child_path(iris_data_dir, "Iris.txt"),
237+
)
238+
239+
# Check that the mocked Popen call arguments list starts with the MPI
240+
# arguments, followed by the Khiops command
241+
expected_command_args = runner.mpi_command_args + [runner.khiops_path]
242+
self.assertTrue(len(mock_popen.call_args.args) > 0)
243+
self.assertTrue(len(mock_popen.call_args.args[0]) > len(expected_command_args))
244+
self.assertEqual(
245+
mock_popen.call_args.args[0][: len(expected_command_args)],
246+
expected_command_args,
247+
)
248+
249+
# Run Khiops Coclustering through an API function, using the mocked Popen
250+
# to capture its arguments
251+
# Nest context managers for Python 3.8 compatibility
252+
with patch("subprocess.Popen", mock_popen):
253+
with tempfile.TemporaryDirectory() as temp_dir:
254+
kh.train_coclustering(
255+
fs.get_child_path(iris_data_dir, "Iris.kdic"),
256+
"Iris",
257+
fs.get_child_path(iris_data_dir, "Iris.txt"),
258+
["SepalLength", "PetalLength"],
259+
fs.get_child_path(temp_dir, "IrisCoclusteringResults.khcj"),
260+
)
261+
262+
# Check that the mocked Popen call arguments list starts with the MPI
263+
# arguments, followed by the Khiops Coclustering command
264+
expected_command_args = runner.mpi_command_args + [
265+
runner.khiops_coclustering_path
266+
]
267+
self.assertTrue(len(mock_popen.call_args.args) > 0)
268+
self.assertTrue(len(mock_popen.call_args.args[0]) > len(expected_command_args))
269+
self.assertEqual(
270+
mock_popen.call_args.args[0][: len(expected_command_args)],
271+
expected_command_args,
272+
)
273+
211274

212275
class KhiopsMultitableFitTests(unittest.TestCase):
213276
"""Test if Khiops estimator can be fitted on multi-table data"""

0 commit comments

Comments
 (0)