Skip to content

Commit

Permalink
Distance Matrix: Show negative distances using divergent palettes
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Feb 3, 2023
1 parent 0dec358 commit df24757
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions Orange/widgets/utils/distmatrixmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from Orange.misc import DistMatrix
from Orange.widgets import gui
from Orange.widgets.utils import colorpalettes
from Orange.widgets.utils.itemdelegates import FixedFormatNumericColumnDelegate


Expand All @@ -17,10 +18,19 @@ class LabelData(NamedTuple):


class DistMatrixModel(QAbstractTableModel):
_brushes = np.array([QBrush(QColor.fromHsv(120, int(i / 255 * 170), 255))
for i in range(256)])

_diverging_brushes = np.array([
QBrush(col) for col in
colorpalettes.ContinuousPalettes['diverging_tritanopic_cwr_75_98_c20'
].qcolors])

def __init__(self):
super().__init__()
self.distances: Optional[DistMatrix] = None
self.colors: Optional[np.ndarray] = None
self.brushes: Optional[np.ndarray] = None
self.__header_data: Dict[Any, Optional[LabelData]] = {
Qt.Horizontal: LabelData(),
Qt.Vertical: LabelData()}
Expand All @@ -35,12 +45,19 @@ def set_data(self, distances):
self.distances = distances
self.__header_data = dict.fromkeys(self.__header_data, LabelData())
if distances is None:
self.__span = self.colors = None
self.__span = self.colors = self.brushes = None
return
self.__span = span = np.max(distances)
minc = min(0, np.min(distances))
maxc = np.max(distances)
if minc < 0:
self.__span = max(-minc, maxc)
self.brushes = self._diverging_brushes
self.colors = 127 + (distances / self.__span * 128).astype(int)
else:
self.__span = maxc
self.brushes = self._brushes
self.colors = (distances / self.__span * 255).astype(int)

self.colors = \
(distances * (170 / span if span > 1e-10 else 0)).astype(int)
self.__zero_diag = \
distances.is_symmetric() and np.allclose(np.diag(distances), 0)
self.endResetModel()
Expand Down Expand Up @@ -73,7 +90,7 @@ def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole and not (self.__zero_diag and row == col):
return float(self.distances[row, col])
if role == Qt.BackgroundRole:
return QBrush(QColor.fromHsv(120, self.colors[row, col], 255))
return self.brushes[self.colors[row, col]]
if role == Qt.ForegroundRole:
return QColor(Qt.black) # the background is light-ish
if role == FixedFormatNumericColumnDelegate.ColumnDataSpanRole:
Expand Down

0 comments on commit df24757

Please sign in to comment.