Skip to content

Commit e5c261b

Browse files
committed
Add CoclusteringCell part_indexes attribute for getting per-cell part indexes
closes #569
1 parent 17439c1 commit e5c261b

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

khiops/core/coclustering_results.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
module look at their ``to_dict`` methods which write Python dictionaries in the
4242
same format as the Khiops JSON reports.
4343
"""
44+
import functools
4445
import io
4546
import warnings
4647

@@ -447,6 +448,7 @@ def __init__(self, json_data=None):
447448
# Initialize cell
448449
cell = CoclusteringCell()
449450
cell.frequency = json_cell_frequencies[i]
451+
cell.part_indexes = part_indexes
450452

451453
# Initialize cell parts:
452454
# Retrieve part from its index in the partition, per dimension
@@ -490,19 +492,6 @@ def get_dimension(self, dimension_name):
490492

491493
def to_dict(self):
492494
"""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)
506495
report_summary = {
507496
"instances": self.instance_number,
508497
"cells": self.cell_number,
@@ -532,7 +521,11 @@ def to_dict(self):
532521
dimension.to_dict(report_type="hierarchy")
533522
for dimension in self.dimensions
534523
],
535-
"cellPartIndexes": cell_parts_indexes,
524+
"cellPartIndexes": functools.reduce(
525+
lambda part_indexes, cell: part_indexes + [cell.part_indexes],
526+
self.cells,
527+
[],
528+
),
536529
"cellFrequencies": [cell.frequency for cell in self.cells],
537530
}
538531
return report
@@ -1853,13 +1846,16 @@ class CoclusteringCell:
18531846
----------
18541847
parts : list of `CoclusteringDimensionPart`
18551848
Parts for each coclustering dimension.
1849+
part_indexes : list of int
1850+
Part indexes for each coclustering dimension.
18561851
frequency : int
18571852
Frequency of this cell.
18581853
"""
18591854

18601855
def __init__(self):
18611856
"""Constructs an instance with default attribute values"""
18621857
self.parts = []
1858+
self.part_indexes = []
18631859
self.frequency = 0
18641860

18651861
def write_line(self, writer): # pragma: no cover

0 commit comments

Comments
 (0)