Skip to content

Commit

Permalink
Merge pull request #14 from JesseMckinzie/correlation_v4
Browse files Browse the repository at this point in the history
Add correlogram
  • Loading branch information
sameeul authored Jun 12, 2024
2 parents 1271812 + 93b15f6 commit 8d836bf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
16 changes: 16 additions & 0 deletions docs/source/Examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ including annotations.

After clicking ``Sort``, the table will be sorted by the selected column. To sort by multiple columns, enter the columns into the box in order, separated by
spaces.

Creating a Correlogram
=====================================

To create a correlogram (or correlation matrix) of the feature calculations, first run nyxus on a set of image-segmentation pair(s). Next, click the ``Generate Correlogram``
button.

.. image:: https://github.com/PolusAI/napari-nyxus/raw/main/docs/source/img/correlation.png

After clicking this button, a new table will be adding to the napari viewer. To view this table, drag the table out of the napari viewer and expand the window. The result
is shown below.

.. image:: https://github.com/PolusAI/napari-nyxus/raw/main/docs/source/img/correlation_result.png

The result is a correlation matrix between each of the averaged features between every row of the feature calculation results. Note that the diagonal is 1 since a feature
is fully correlated to itself.
Binary file added docs/source/img/correlation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/img/correlation_result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 51 additions & 1 deletion napari_nyxus/nyx_napari.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,13 @@ def add_feature_calculation_table_options(self):
# add button for sorting columns
self.sort_button = QPushButton("Sort")
self.sort_button.clicked.connect(self._sort)

# add button for sorting columns
self.corr_button = QPushButton("Generate correlation")
self.corr_button.clicked.connect(self._get_correlation)

# add widgets for feature calculations table
widget_table.layout().addWidget(self.corr_button)

# add widgets for feature calculations table
widget_table.layout().addWidget(self.column_box)
Expand Down Expand Up @@ -662,5 +669,48 @@ def _get_maximum_text(self):

except:
return

def _get_correlation(self):

self.corr = self.result.iloc[:, 3:].corr()

self.win = FeaturesWidget()
scroll = QScrollArea()
layout = QVBoxLayout()
corr_table = QTableWidget()
scroll.setWidget(corr_table)
layout.addWidget(corr_table)
self.win.setLayout(layout)
self.win.setWindowTitle("Feature Results")

# Add DataFrame to widget window
corr_table.setColumnCount(len(self.corr.columns))
corr_table.setRowCount(len(self.corr.index))
corr_table.setHorizontalHeaderLabels(self.corr.columns)
corr_table.setHorizontalHeader(rotated_header.RotatedHeaderView(corr_table))
corr_table.setVerticalHeaderLabels(self.corr.columns)

row_height = corr_table.rowHeight(1)
column_width = corr_table.columnWidth(1)

width = min(row_height, column_width)

corr_table.horizontalHeader().setDefaultSectionSize(width)
corr_table.verticalHeader().setDefaultSectionSize(width)


for col in range(self.corr.shape[1]):

# Get the column data
column_data = self.corr.iloc[:, col]

# Map normalized values to colors using a specified colormap
colormap = plt.get_cmap('magma')
colors = (colormap(column_data) * 255).astype(int) # Multiply by 255 to convert to QColor range
# Set background color for each item in the column


for row in range(self.corr.shape[0]):
corr_table.setItem(row,col,QTableWidgetItem(''))
corr_table.item(row, col).setBackground(QColor(colors[row][0], colors[row][1], colors[row][2], colors[row][3]))

self.viewer.window.add_dock_widget(self.win)

0 comments on commit 8d836bf

Please sign in to comment.