Skip to content

Commit 903a878

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 903a878

2 files changed

Lines changed: 91 additions & 14 deletions

File tree

khiops/core/coclustering_results.py

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -488,21 +488,44 @@ 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
514+
dimension.
515+
516+
.. note::
517+
The result of this method is the transposition of the return value
518+
of the `get_cell_part_indexes` method.
519+
"""
520+
per_dimension_part_indexes = []
521+
for dimension in self.dimensions:
522+
per_dimension_part_indexes.append(
523+
dimension.get_part_indexes_of_cells(self.cells)
524+
)
525+
return per_dimension_part_indexes
526+
491527
def to_dict(self):
492528
"""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)
506529
report_summary = {
507530
"instances": self.instance_number,
508531
"cells": self.cell_number,
@@ -532,7 +555,7 @@ def to_dict(self):
532555
dimension.to_dict(report_type="hierarchy")
533556
for dimension in self.dimensions
534557
],
535-
"cellPartIndexes": cell_parts_indexes,
558+
"cellPartIndexes": self.get_cell_part_indexes(),
536559
"cellFrequencies": [cell.frequency for cell in self.cells],
537560
}
538561
return report
@@ -1075,6 +1098,24 @@ def get_part(self, part_name):
10751098
"""
10761099
return self._parts_by_name[part_name]
10771100

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

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

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 get_per_dimension_cell_part_indexes is the
205+
# transposition of 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)