diff --git a/docs/source/Examples.rst b/docs/source/Examples.rst index 516cefd..c4ec47c 100644 --- a/docs/source/Examples.rst +++ b/docs/source/Examples.rst @@ -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. \ No newline at end of file diff --git a/docs/source/img/correlation.png b/docs/source/img/correlation.png new file mode 100644 index 0000000..c0b61a9 Binary files /dev/null and b/docs/source/img/correlation.png differ diff --git a/docs/source/img/correlation_result.png b/docs/source/img/correlation_result.png new file mode 100644 index 0000000..372add1 Binary files /dev/null and b/docs/source/img/correlation_result.png differ diff --git a/napari_nyxus/nyx_napari.py b/napari_nyxus/nyx_napari.py index dfeb265..f3cc4e6 100644 --- a/napari_nyxus/nyx_napari.py +++ b/napari_nyxus/nyx_napari.py @@ -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) @@ -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 - \ No newline at end of file + 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) \ No newline at end of file