Skip to content

Commit 2b35c67

Browse files
committed
feat: adding grainstats as labels features for better integration with other plugins (compatibility still needs to be improved)
1 parent 41c8aff commit 2b35c67

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/napari_topostats/_widget_function.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,30 @@ def render_return_value(
682682
layout = QVBoxLayout(container)
683683
nm_checkbox = QCheckBox("Convert to nm")
684684
nm_checkbox.setChecked(False)
685+
685686
# Create table widget
686687
table = QTableWidget()
687688
table.setRowCount(len(df))
688689
table.setColumnCount(len(df.columns))
689690
table.setHorizontalHeaderLabels(df.columns.tolist())
691+
692+
if isinstance(original, Labels):
693+
# Create a copy of the dataframe with an extra row for the background for proper alignmemt of labels
694+
# This will not affect the original dataframe used for the table
695+
features_df = df.copy()
696+
features_df.index = features_df.index + 1
697+
if 0 not in features_df.index:
698+
# Get the first row to copy the columns and dtypes
699+
bg_row = features_df.iloc[[0]].copy()
700+
bg_row.index = [0]
701+
# Fill the row with NaN
702+
bg_row.loc[0] = np.nan
703+
if "grain_number" in bg_row.columns:
704+
# Set grain_number to -1 for the background row so it is different from real grains (0-indexed)
705+
bg_row["grain_number"] = -1
706+
features_df = pd.concat([bg_row, features_df])
707+
features_df["label_id"] = features_df.index
708+
original.features = features_df
690709
original.mode = Mode.PICK
691710
is_updating = False
692711

0 commit comments

Comments
 (0)