Skip to content

Commit 6d0632a

Browse files
committed
Add methods for getting per-cell part indexes
- add `CoclusteringCell.get_part_indexes_for_dimensions` to get the part indexes of the current cell instance, for the specified list of dimensions; - add `CoclusteringDimension.get_part_indexes_of_cells` to get the part indexes of the specified cells, for the current dimension instance; - add `CoclusteringReport.get_cell_part_indexes` to get the part indexes for all dimensions, for each cell; - add `CoclusteringReport.get_per_dimension_cell_part_indexes` to get the part indexes of each cell, for each dimension; returns the transposition of the return value of the `CoclusteringReport.get_cell_part_indexes` method.
1 parent 17439c1 commit 6d0632a

2 files changed

Lines changed: 90 additions & 14 deletions

File tree

khiops/core/coclustering_results.py

Lines changed: 77 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,43 @@ def get_dimension(self, dimension_name):
488488
"""
489489
return self._dimensions_by_name[dimension_name]
490490

491+
def get_cell_part_indexes(self):
492+
"""Returns all cell part indexes
493+
494+
Returns
495+
-------
496+
list of list of int
497+
The list of lists of cell part indexes for all dimensions, for each
498+
cell.
499+
"""
500+
cell_part_indexes = []
501+
for cell in self.cells:
502+
cell_part_indexes.append(
503+
cell.get_part_indexes_for_dimensions(self.dimensions)
504+
)
505+
return cell_part_indexes
506+
507+
def get_per_dimension_cell_part_indexes(self):
508+
"""Returns per-dimension part indexes for all cells
509+
510+
Returns
511+
-------
512+
list of list of int
513+
The list of lists of cell part indexes for for all cells, for each dimension.
514+
515+
.. note::
516+
The result of this method is the transposition of the return value
517+
of the `get_cell_part_indexes` method.
518+
"""
519+
per_dimension_part_indexes = []
520+
for dimension in self.dimensions:
521+
per_dimension_part_indexes.append(
522+
dimension.get_part_indexes_of_cells(self.cells)
523+
)
524+
return per_dimension_part_indexes
525+
491526
def to_dict(self):
492527
"""Transforms this instance to a dict with the Khiops JSON file structure"""
493-
# Compute cellPartIndexes
494-
cell_parts_indexes = []
495-
for cell in self.cells:
496-
cell_part_indexes = []
497-
for cell_part in cell.parts:
498-
for dimension in self.dimensions:
499-
for dimension_part_index, dimension_part in enumerate(
500-
dimension.parts
501-
):
502-
if cell_part == dimension_part:
503-
cell_part_indexes.append(dimension_part_index)
504-
break
505-
cell_parts_indexes.append(cell_part_indexes)
506528
report_summary = {
507529
"instances": self.instance_number,
508530
"cells": self.cell_number,
@@ -532,7 +554,7 @@ def to_dict(self):
532554
dimension.to_dict(report_type="hierarchy")
533555
for dimension in self.dimensions
534556
],
535-
"cellPartIndexes": cell_parts_indexes,
557+
"cellPartIndexes": self.get_cell_part_indexes(),
536558
"cellFrequencies": [cell.frequency for cell in self.cells],
537559
}
538560
return report
@@ -1075,6 +1097,24 @@ def get_part(self, part_name):
10751097
"""
10761098
return self._parts_by_name[part_name]
10771099

1100+
def get_part_indexes_of_cells(self, cells):
1101+
"""Returns part indexes of the specified cells, for the current dimension
1102+
1103+
Parameters
1104+
----------
1105+
cells : list of `CoclusteringCell`
1106+
1107+
Returns
1108+
-------
1109+
list of list of int
1110+
The list of list of part indexes of the specified cells, for all
1111+
parts of the current dimension.
1112+
"""
1113+
part_indexes = []
1114+
for cell in cells:
1115+
part_indexes.extend(cell.get_part_indexes_for_dimensions([self]))
1116+
return part_indexes
1117+
10781118
def get_cluster(self, cluster_name):
10791119
"""Returns the specified cluster
10801120
@@ -1862,6 +1902,29 @@ def __init__(self):
18621902
self.parts = []
18631903
self.frequency = 0
18641904

1905+
def get_part_indexes_for_dimensions(self, dimensions):
1906+
"""Returns cell part indexes for the specified dimensions
1907+
1908+
Parameters
1909+
----------
1910+
dimensions : list of `CoclusteringDimension`
1911+
List of coclustering dimensions for which the cell part indexes must
1912+
be determined.
1913+
1914+
Returns
1915+
-------
1916+
list of int
1917+
The list of the cell part indexes for the specified dimensions.
1918+
"""
1919+
part_indexes = []
1920+
for part in self.parts:
1921+
for dimension in dimensions:
1922+
for dimension_part_index, dimension_part in enumerate(dimension.parts):
1923+
if part == dimension_part:
1924+
part_indexes.append(dimension_part_index)
1925+
break
1926+
return part_indexes
1927+
18651928
def write_line(self, writer): # pragma: no cover
18661929
"""Writes a line of the instance's report to a writer object
18671930

tests/test_core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
# pylint: disable=protected-access
3535

3636

37+
def _transpose(a_list):
38+
"""Returns the transposition of a list of lists"""
39+
return [list(x) for x in zip(*a_list)]
40+
41+
3742
class KhiopsCoreIOTests(unittest.TestCase):
3843
"""Tests the reading/writing of files for the core module classes/functions"""
3944

@@ -196,6 +201,14 @@ def test_coclustering_results(self):
196201
results, ref_json_report
197202
)
198203

204+
# Test that results.get_per_dimension_cell_part_indexes is the
205+
# transposition of results.get_cell_part_indexes
206+
report = results.coclustering_report
207+
self.assertEqual(
208+
report.get_per_dimension_cell_part_indexes(),
209+
_transpose(report.get_cell_part_indexes()),
210+
)
211+
199212
def test_binary_dictionary_domain(self):
200213
"""Test binary dictionary write"""
201214
# Set the test paths

0 commit comments

Comments
 (0)