nettracer3d 1.0.1__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. {nettracer3d-1.0.1/src/nettracer3d.egg-info → nettracer3d-1.0.2}/PKG-INFO +4 -3
  2. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/README.md +3 -2
  3. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/pyproject.toml +1 -1
  4. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/neighborhoods.py +145 -36
  5. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/nettracer.py +14 -2
  6. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/nettracer_gui.py +328 -18
  7. {nettracer3d-1.0.1 → nettracer3d-1.0.2/src/nettracer3d.egg-info}/PKG-INFO +4 -3
  8. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/LICENSE +0 -0
  9. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/setup.cfg +0 -0
  10. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/__init__.py +0 -0
  11. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/cellpose_manager.py +0 -0
  12. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/community_extractor.py +0 -0
  13. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/excelotron.py +0 -0
  14. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/modularity.py +0 -0
  15. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/morphology.py +0 -0
  16. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/network_analysis.py +0 -0
  17. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/network_draw.py +0 -0
  18. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/node_draw.py +0 -0
  19. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/painting.py +0 -0
  20. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/proximity.py +0 -0
  21. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/run.py +0 -0
  22. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/segmenter.py +0 -0
  23. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/segmenter_GPU.py +0 -0
  24. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/simple_network.py +0 -0
  25. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d/smart_dilate.py +0 -0
  26. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d.egg-info/SOURCES.txt +0 -0
  27. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d.egg-info/dependency_links.txt +0 -0
  28. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d.egg-info/entry_points.txt +0 -0
  29. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d.egg-info/requires.txt +0 -0
  30. {nettracer3d-1.0.1 → nettracer3d-1.0.2}/src/nettracer3d.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nettracer3d
3
- Version: 1.0.1
3
+ Version: 1.0.2
4
4
  Summary: Scripts for intializing and analyzing networks from segmentations of three dimensional images.
5
5
  Author-email: Liam McLaughlin <liamm@wustl.edu>
6
6
  Project-URL: Documentation, https://nettracer3d.readthedocs.io/en/latest/
@@ -110,6 +110,7 @@ McLaughlin, L., Zhang, B., Sharma, S. et al. Three dimensional multiscalar neuro
110
110
 
111
111
  NetTracer3D was developed by Liam McLaughlin while working under Dr. Sanjay Jain at Washington University School of Medicine.
112
112
 
113
- -- Version 1.0.1 Updates --
113
+ -- Version 1.0.2 Updates --
114
114
 
115
- * Bug fixes, mainly
115
+ * Minor fixes
116
+ * Added ability to generate violin plots using the table generated from merging node identities, showing the relative expression of markers for multiple channels for the nodes belonging to some channel or community/neighborhood
@@ -65,6 +65,7 @@ McLaughlin, L., Zhang, B., Sharma, S. et al. Three dimensional multiscalar neuro
65
65
 
66
66
  NetTracer3D was developed by Liam McLaughlin while working under Dr. Sanjay Jain at Washington University School of Medicine.
67
67
 
68
- -- Version 1.0.1 Updates --
68
+ -- Version 1.0.2 Updates --
69
69
 
70
- * Bug fixes, mainly
70
+ * Minor fixes
71
+ * Added ability to generate violin plots using the table generated from merging node identities, showing the relative expression of markers for multiple channels for the nodes belonging to some channel or community/neighborhood
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "nettracer3d"
3
- version = "1.0.1"
3
+ version = "1.0.2"
4
4
  authors = [
5
5
  { name="Liam McLaughlin", email="liamm@wustl.edu" },
6
6
  ]
@@ -8,7 +8,8 @@ from matplotlib.colors import LinearSegmentedColormap
8
8
  from sklearn.cluster import DBSCAN
9
9
  from sklearn.neighbors import NearestNeighbors
10
10
  import matplotlib.colors as mcolors
11
-
11
+ from collections import Counter
12
+ from . import community_extractor
12
13
 
13
14
 
14
15
  import os
@@ -992,60 +993,63 @@ def create_node_heatmap(node_intensity, node_centroids, shape=None, is_3d=True,
992
993
  node_to_intensity[node_id] = node_intensity_clean[node_id]
993
994
 
994
995
  # Create colormap function (RdBu_r - red for high, blue for low, yellow/white for middle)
995
- def intensity_to_rgb(intensity, min_val, max_val):
996
- """Convert intensity value to RGB using RdBu_r colormap logic, centered at 0"""
996
+ def intensity_to_rgba(intensity, min_val, max_val):
997
+ """Convert intensity value to RGBA using RdBu_r colormap logic, centered at 0"""
997
998
 
998
999
  # Handle edge case where all values are the same
999
1000
  if max_val == min_val:
1000
1001
  if intensity == 0:
1001
- return np.array([255, 255, 255], dtype=np.uint8) # White for 0
1002
+ return np.array([255, 255, 255, 0], dtype=np.uint8) # Transparent white for 0
1002
1003
  elif intensity > 0:
1003
- return np.array([255, 200, 200], dtype=np.uint8) # Light red for positive
1004
+ return np.array([255, 200, 200, 255], dtype=np.uint8) # Opaque light red for positive
1004
1005
  else:
1005
- return np.array([200, 200, 255], dtype=np.uint8) # Light blue for negative
1006
+ return np.array([200, 200, 255, 255], dtype=np.uint8) # Opaque light blue for negative
1006
1007
 
1007
1008
  # Find the maximum absolute value for symmetric scaling around 0
1008
1009
  max_abs = max(abs(min_val), abs(max_val))
1009
1010
 
1010
- # If max_abs is 0, everything is 0, so return white
1011
+ # If max_abs is 0, everything is 0, so return transparent
1011
1012
  if max_abs == 0:
1012
- return np.array([255, 255, 255], dtype=np.uint8) # White
1013
+ return np.array([255, 255, 255, 0], dtype=np.uint8) # Transparent white
1013
1014
 
1014
1015
  # Normalize intensity to -1 to 1 range, centered at 0
1015
1016
  normalized = intensity / max_abs
1016
1017
  normalized = np.clip(normalized, -1, 1)
1017
1018
 
1018
1019
  if normalized > 0:
1019
- # Positive values: white to red (intensity 0 = white, max positive = red)
1020
+ # Positive values: white to red (intensity 0 = transparent, max positive = red)
1020
1021
  r = 255
1021
1022
  g = int(255 * (1 - normalized))
1022
1023
  b = int(255 * (1 - normalized))
1024
+ alpha = 255 # Fully opaque for all non-zero values
1023
1025
  elif normalized < 0:
1024
- # Negative values: white to blue (intensity 0 = white, max negative = blue)
1026
+ # Negative values: white to blue (intensity 0 = transparent, max negative = blue)
1025
1027
  r = int(255 * (1 + normalized))
1026
1028
  g = int(255 * (1 + normalized))
1027
1029
  b = 255
1030
+ alpha = 255 # Fully opaque for all non-zero values
1028
1031
  else:
1029
- # Exactly 0: white
1030
- r, g, b = 255, 255, 255
1032
+ # Exactly 0: transparent
1033
+ r, g, b, alpha = 255, 255, 255, 0
1031
1034
 
1032
- return np.array([r, g, b], dtype=np.uint8)
1033
-
1034
- # Create lookup table for RGB colors
1035
+ return np.array([r, g, b, alpha], dtype=np.uint8)
1036
+
1037
+ # Modified usage in your main function:
1038
+ # Create lookup table for RGBA colors (note the 4 channels now)
1035
1039
  max_label = max(max(labeled_array.flat), max(node_to_intensity.keys()) if node_to_intensity else 0)
1036
- color_lut = np.zeros((max_label + 1, 3), dtype=np.uint8) # Default to black (0,0,0)
1037
-
1038
- # Fill lookup table with RGB colors based on intensity
1040
+ color_lut = np.zeros((max_label + 1, 4), dtype=np.uint8) # Default to transparent (0,0,0,0)
1041
+
1042
+ # Fill lookup table with RGBA colors based on intensity
1039
1043
  for node_id, intensity in node_to_intensity.items():
1040
- rgb_color = intensity_to_rgb(intensity, min_intensity, max_intensity)
1041
- color_lut[int(node_id)] = rgb_color
1042
-
1044
+ rgba_color = intensity_to_rgba(intensity, min_intensity, max_intensity)
1045
+ color_lut[int(node_id)] = rgba_color
1046
+
1043
1047
  # Apply lookup table to labeled array - single vectorized operation
1044
1048
  if is_3d:
1045
- # Return full 3D RGB array [Z, Y, X, 3]
1049
+ # Return full 3D RGBA array [Z, Y, X, 4]
1046
1050
  heatmap_array = color_lut[labeled_array]
1047
1051
  else:
1048
- # Return 2D RGB array
1052
+ # Return 2D RGBA array
1049
1053
  if labeled_array.ndim == 3:
1050
1054
  # Take middle slice for 2D representation
1051
1055
  middle_slice = labeled_array.shape[0] // 2
@@ -1053,7 +1057,7 @@ def create_node_heatmap(node_intensity, node_centroids, shape=None, is_3d=True,
1053
1057
  else:
1054
1058
  # Already 2D
1055
1059
  heatmap_array = color_lut[labeled_array]
1056
-
1060
+
1057
1061
  return heatmap_array
1058
1062
 
1059
1063
  else:
@@ -1122,19 +1126,124 @@ def create_node_heatmap(node_intensity, node_centroids, shape=None, is_3d=True,
1122
1126
  plt.tight_layout()
1123
1127
  plt.show()
1124
1128
 
1125
- # Example usage:
1126
- if __name__ == "__main__":
1127
- # Sample data for demonstration
1128
- sample_dict = {
1129
- 'category_A': np.array([0.1, 0.5, 0.8, 0.3, 0.9]),
1130
- 'category_B': np.array([0.7, 0.2, 0.6, 0.4, 0.1]),
1131
- 'category_C': np.array([0.9, 0.8, 0.2, 0.7, 0.5])
1132
- }
1129
+ def create_violin_plots(data_dict, graph_title="Violin Plots"):
1130
+ """
1131
+ Create violin plots from dictionary data with distinct colors.
1133
1132
 
1134
- sample_id_set = ['feature_1', 'feature_2', 'feature_3', 'feature_4', 'feature_5']
1133
+ Parameters:
1134
+ data_dict (dict): Dictionary where keys are column headers (strings) and
1135
+ values are lists of floats
1136
+ graph_title (str): Title for the overall plot
1137
+ """
1138
+ if not data_dict:
1139
+ print("No data to plot")
1140
+ return
1135
1141
 
1136
- # Create the heatmap
1137
- fig, ax = plot_dict_heatmap(sample_dict, sample_id_set,
1138
- title="Sample Heatmap Visualization")
1142
+ # Prepare data
1143
+ labels = list(data_dict.keys())
1144
+ data_lists = list(data_dict.values())
1139
1145
 
1146
+ # Generate colors using the community color strategy
1147
+ try:
1148
+ # Create a mock community dict for color generation
1149
+ mock_community_dict = {i: i+1 for i in range(len(labels))} # No outliers for simplicity
1150
+
1151
+ # Get distinct colors
1152
+ n_colors = len(labels)
1153
+ colors_rgb = community_extractor.generate_distinct_colors(n_colors)
1154
+
1155
+ # Sort by data size for consistent color assignment (like community sizes)
1156
+ data_sizes = [(i, len(data_lists[i])) for i in range(len(data_lists))]
1157
+ sorted_indices = sorted(data_sizes, key=lambda x: (-x[1], x[0]))
1158
+
1159
+ # Create color mapping
1160
+ colors = []
1161
+ for i, _ in sorted_indices:
1162
+ color_idx = sorted_indices.index((i, _))
1163
+ if color_idx < len(colors_rgb):
1164
+ # Convert RGB (0-255) to matplotlib format (0-1)
1165
+ rgb_normalized = tuple(c/255.0 for c in colors_rgb[color_idx])
1166
+ colors.append(rgb_normalized)
1167
+ else:
1168
+ colors.append('gray') # Fallback color
1169
+
1170
+ # Reorder colors to match original label order
1171
+ final_colors = ['gray'] * len(labels)
1172
+ for idx, (original_idx, _) in enumerate(sorted_indices):
1173
+ final_colors[original_idx] = colors[idx]
1174
+
1175
+ except Exception as e:
1176
+ print(f"Color generation failed, using default colors: {e}")
1177
+ # Fallback to default matplotlib colors
1178
+ final_colors = plt.cm.Set3(np.linspace(0, 1, len(labels)))
1179
+
1180
+ # Create the plot
1181
+ fig, ax = plt.subplots(figsize=(max(8, len(labels) * 1.5), 6))
1182
+
1183
+ # Create violin plots
1184
+ violin_parts = ax.violinplot(data_lists, positions=range(len(labels)),
1185
+ showmeans=False, showmedians=True, showextrema=True)
1186
+
1187
+ # Color the violins
1188
+ for i, pc in enumerate(violin_parts['bodies']):
1189
+ if i < len(final_colors):
1190
+ pc.set_facecolor(final_colors[i])
1191
+ pc.set_alpha(0.7)
1192
+
1193
+ # Color the other violin elements
1194
+ for partname in ('cbars', 'cmins', 'cmaxes', 'cmedians'):
1195
+ if partname in violin_parts:
1196
+ violin_parts[partname].set_edgecolor('black')
1197
+ violin_parts[partname].set_linewidth(1)
1198
+
1199
+ # Add data points as scatter plot overlay with much lower transparency
1200
+ """
1201
+ for i, data in enumerate(data_lists):
1202
+ y = data
1203
+ # Add some jitter to x positions for better visibility
1204
+ x = np.random.normal(i, 0.04, size=len(y))
1205
+ ax.scatter(x, y, alpha=0.2, s=15, color='black', edgecolors='none', zorder=3) # No borders, more transparent
1206
+ """
1207
+
1208
+ # Calculate reasonable y-axis limits to focus on the bulk of the data
1209
+ all_data = [val for sublist in data_lists for val in sublist]
1210
+ if all_data:
1211
+ # Use percentiles to exclude extreme outliers from the view
1212
+ y_min = np.percentile(all_data, 5) # 5th percentile
1213
+ y_max = np.percentile(all_data, 95) # 95th percentile
1214
+
1215
+ # Add some padding
1216
+ y_range = y_max - y_min
1217
+ y_padding = y_range * 0.15
1218
+ ax.set_ylim(y_min - y_padding, y_max + y_padding)
1219
+
1220
+ # Add IQR and median text annotations BELOW the violins
1221
+ for i, data in enumerate(data_lists):
1222
+ if len(data) > 0:
1223
+ q1, median, q3 = np.percentile(data, [25, 50, 75])
1224
+ iqr = q3 - q1
1225
+
1226
+ # Position text below the violin (using current y-axis limits)
1227
+ y_min_current = ax.get_ylim()[0]
1228
+ y_text = y_min_current - (ax.get_ylim()[1] - ax.get_ylim()[0]) * 0.15
1229
+
1230
+ ax.text(i, y_text, f'Median: {median:.2f}\nIQR: {iqr:.2f}',
1231
+ horizontalalignment='center', fontsize=8,
1232
+ bbox=dict(boxstyle='round,pad=0.3', facecolor='white', alpha=0.8))
1233
+
1234
+ # Customize the plot
1235
+ ax.set_xticks(range(len(labels)))
1236
+ ax.set_xticklabels(labels, rotation=45, ha='right')
1237
+ ax.set_title(graph_title, fontsize=14, fontweight='bold')
1238
+ ax.set_ylabel('Normalized Values (Z-score-like)', fontsize=12)
1239
+ ax.grid(True, alpha=0.3)
1240
+
1241
+ # Add a horizontal line at y=0 (the identity centerpoint)
1242
+ ax.axhline(y=0, color='red', linestyle='--', alpha=0.5, linewidth=1,
1243
+ label='Identity Centerpoint')
1244
+ ax.legend(loc='upper right')
1245
+
1246
+ # Adjust layout to prevent label cutoff and accommodate bottom text
1247
+ plt.subplots_adjust(bottom=0.2) # Extra space for bottom text
1248
+ plt.tight_layout()
1140
1249
  plt.show()
@@ -35,6 +35,7 @@ from . import proximity
35
35
  from skimage.segmentation import watershed as water
36
36
 
37
37
 
38
+
38
39
  #These next several methods relate to searching with 3D objects by dilating each one in a subarray around their neighborhood although I don't explicitly use this anywhere... can call them deprecated although I may want to use them later again so I have them still written out here.
39
40
 
40
41
 
@@ -6120,14 +6121,25 @@ class Network_3D:
6120
6121
 
6121
6122
  for node, iden in self.node_identities.items():
6122
6123
 
6123
- if iden == root:
6124
+ if iden == root: # Standard behavior
6124
6125
 
6125
6126
  root_set.append(node)
6126
6127
 
6127
- elif (iden == targ) or (targ == 'All Others (Excluding Self)'):
6128
+ elif '[' in iden and root != "All (Excluding Targets)": # For multiple nodes
6129
+ if root in iden:
6130
+ root_set.append(node)
6131
+
6132
+ elif (iden == targ) or (targ == 'All Others (Excluding Self)'): # The other group
6128
6133
 
6129
6134
  compare_set.append(node)
6130
6135
 
6136
+ elif '[' in iden: # The other group, for multiple nodes
6137
+ if targ in iden:
6138
+ compare_set.append(node)
6139
+
6140
+ elif root == "All (Excluding Targets)": # If not assigned to the other group but the comprehensive root option is used
6141
+ root_set.append(node)
6142
+
6131
6143
  if root == targ:
6132
6144
 
6133
6145
  compare_set = root_set
@@ -511,12 +511,8 @@ class ImageViewerWindow(QMainWindow):
511
511
  data = df.iloc[:, 0].tolist() # First column as list
512
512
  value = None
513
513
 
514
- self.format_for_upperright_table(
515
- data=data,
516
- metric=metric,
517
- value=value,
518
- title=title
519
- )
514
+ df = self.format_for_upperright_table(data=data, metric=metric, value=value, title=title)
515
+ return df
520
516
  else:
521
517
  # Multiple columns: create dictionary as before
522
518
  # First column header (for metric parameter)
@@ -542,12 +538,8 @@ class ImageViewerWindow(QMainWindow):
542
538
  value = value[0]
543
539
 
544
540
  # Call the parent method
545
- self.format_for_upperright_table(
546
- data=data_dict,
547
- metric=metric,
548
- value=value,
549
- title=title
550
- )
541
+ df = self.format_for_upperright_table(data=data_dict, metric=metric, value=value, title=title)
542
+ return df
551
543
 
552
544
  QMessageBox.information(
553
545
  self,
@@ -4592,6 +4584,8 @@ class ImageViewerWindow(QMainWindow):
4592
4584
  rad_action.triggered.connect(self.show_rad_dialog)
4593
4585
  inter_action = stats_menu.addAction("Calculate Node < > Edge Interaction")
4594
4586
  inter_action.triggered.connect(self.show_interaction_dialog)
4587
+ violin_action = stats_menu.addAction("Show Identity Violins/UMAP")
4588
+ violin_action.triggered.connect(self.show_violin_dialog)
4595
4589
  overlay_menu = analysis_menu.addMenu("Data/Overlays")
4596
4590
  degree_action = overlay_menu.addAction("Get Degree Information")
4597
4591
  degree_action.triggered.connect(self.show_degree_dialog)
@@ -5047,6 +5041,8 @@ class ImageViewerWindow(QMainWindow):
5047
5041
  for column in range(table.model().columnCount(None)):
5048
5042
  table.resizeColumnToContents(column)
5049
5043
 
5044
+ return df
5045
+
5050
5046
  except:
5051
5047
  pass
5052
5048
 
@@ -6068,7 +6064,7 @@ class ImageViewerWindow(QMainWindow):
6068
6064
 
6069
6065
  if self.shape == self.channel_data[channel_index].shape:
6070
6066
  preserve_zoom = (self.ax.get_xlim(), self.ax.get_ylim())
6071
- self.shape = self.channel_data[channel_index].shape
6067
+ self.shape = (self.channel_data[channel_index].shape[0], self.channel_data[channel_index].shape[1], self.channel_data[channel_index].shape[2])
6072
6068
  if self.shape[1] * self.shape[2] > 3000 * 3000 * self.downsample_factor:
6073
6069
  self.throttle = True
6074
6070
  else:
@@ -6826,6 +6822,10 @@ class ImageViewerWindow(QMainWindow):
6826
6822
  dialog = InteractionDialog(self)
6827
6823
  dialog.exec()
6828
6824
 
6825
+ def show_violin_dialog(self):
6826
+ dialog = ViolinDialog(self)
6827
+ dialog.show()
6828
+
6829
6829
  def show_degree_dialog(self):
6830
6830
  dialog = DegreeDialog(self)
6831
6831
  dialog.exec()
@@ -8356,7 +8356,7 @@ class MergeNodeIdDialog(QDialog):
8356
8356
  result = {key: np.array([d[key] for d in id_dicts]) for key in all_keys}
8357
8357
 
8358
8358
 
8359
- self.parent().format_for_upperright_table(result, 'NodeID', good_list, 'Mean Intensity')
8359
+ self.parent().format_for_upperright_table(result, 'NodeID', good_list, 'Mean Intensity (Save this Table for "Analyze -> Stats -> Show Violins")')
8360
8360
  if umap:
8361
8361
  my_network.identity_umap(result)
8362
8362
 
@@ -8364,7 +8364,7 @@ class MergeNodeIdDialog(QDialog):
8364
8364
  QMessageBox.information(
8365
8365
  self,
8366
8366
  "Success",
8367
- "Node Identities Merged. New IDs represent presence of corresponding img foreground with +, absence with -. Please save your new identities as csv, then use File -> Load -> Load From Excel Helper to bulk search and rename desired combinations. (Press Help [above] for more info)"
8367
+ "Node Identities Merged. New IDs represent presence of corresponding img foreground with +, absence with -. If desired, please save your new identities as csv, then use File -> Load -> Load From Excel Helper to bulk search and rename desired combinations. If desired, please save the outputted mean intensity table to use with 'Analyze -> Stats -> Show Violins'. (Press Help [above] for more info)"
8368
8368
  )
8369
8369
 
8370
8370
  self.accept()
@@ -9143,12 +9143,16 @@ class NearNeighDialog(QDialog):
9143
9143
  if my_network.node_identities is not None:
9144
9144
 
9145
9145
  self.root = QComboBox()
9146
- self.root.addItems(list(set(my_network.node_identities.values())))
9146
+ roots = list(set(my_network.node_identities.values()))
9147
+ roots.sort()
9148
+ roots.append("All (Excluding Targets)")
9149
+ self.root.addItems(roots)
9147
9150
  self.root.setCurrentIndex(0)
9148
9151
  identities_layout.addRow("Root Identity to Search for Neighbor's IDs?", self.root)
9149
9152
 
9150
9153
  self.targ = QComboBox()
9151
9154
  neighs = list(set(my_network.node_identities.values()))
9155
+ neighs.sort()
9152
9156
  neighs.append("All Others (Excluding Self)")
9153
9157
  self.targ.addItems(neighs)
9154
9158
  self.targ.setCurrentIndex(0)
@@ -9273,6 +9277,10 @@ class NearNeighDialog(QDialog):
9273
9277
  except:
9274
9278
  targ = None
9275
9279
 
9280
+ if root == "All (Excluding Targets)" and targ == 'All Others (Excluding Self)':
9281
+ root = None
9282
+ targ = None
9283
+
9276
9284
  heatmap = self.map.isChecked()
9277
9285
  threed = self.threed.isChecked()
9278
9286
  numpy = self.numpy.isChecked()
@@ -9892,6 +9900,266 @@ class InteractionDialog(QDialog):
9892
9900
  print(f"Error finding interactions: {e}")
9893
9901
 
9894
9902
 
9903
+ class ViolinDialog(QDialog):
9904
+
9905
+ def __init__(self, parent=None):
9906
+
9907
+ super().__init__(parent)
9908
+
9909
+ QMessageBox.critical(
9910
+ self,
9911
+ "Notice",
9912
+ "Please select spreadsheet (Should be table output of 'File -> Images -> Node Identities -> Assign Node Identities from Overlap with Other Images'. Make sure to save that table as .csv/.xlsx and then load it here to use this.)"
9913
+ )
9914
+
9915
+ try:
9916
+ try:
9917
+ self.df = self.parent().load_file()
9918
+ except:
9919
+ return
9920
+
9921
+ self.backup_df = copy.deepcopy(self.df)
9922
+ # Get all identity lists and normalize the dataframe
9923
+ identity_lists = self.get_all_identity_lists()
9924
+ self.df = self.normalize_df_with_identity_centerpoints(self.df, identity_lists)
9925
+
9926
+ self.setWindowTitle("Violin Parameters")
9927
+ self.setModal(False)
9928
+
9929
+ layout = QFormLayout(self)
9930
+
9931
+ if my_network.node_identities is not None:
9932
+
9933
+ self.idens = QComboBox()
9934
+ all_idens = list(set(my_network.node_identities.values()))
9935
+ idens = []
9936
+ for iden in all_idens:
9937
+ if '[' not in iden:
9938
+ idens.append(iden)
9939
+ idens.sort()
9940
+ idens.insert(0, "None")
9941
+ self.idens.addItems(idens)
9942
+ self.idens.setCurrentIndex(0)
9943
+ layout.addRow("Return Identity Violin Plots?", self.idens)
9944
+
9945
+ if my_network.communities is not None:
9946
+ self.coms = QComboBox()
9947
+ coms = list(set(my_network.communities.values()))
9948
+ coms.sort()
9949
+ coms.insert(0, "None")
9950
+ coms = [str(x) for x in coms]
9951
+ self.coms.addItems(coms)
9952
+ self.coms.setCurrentIndex(0)
9953
+ layout.addRow("Return Neighborhood/Community Violin Plots?", self.coms)
9954
+
9955
+ # Add Run button
9956
+ run_button = QPushButton("Show Z-score-like Violin")
9957
+ run_button.clicked.connect(self.run)
9958
+ layout.addWidget(run_button)
9959
+
9960
+ run_button2 = QPushButton("Show Z-score UMAP")
9961
+ run_button2.clicked.connect(self.run2)
9962
+ layout.addWidget(run_button2)
9963
+ except:
9964
+ QTimer.singleShot(0, self.close)
9965
+
9966
+ def get_all_identity_lists(self):
9967
+ """
9968
+ Get all identity lists for normalization purposes.
9969
+
9970
+ Returns:
9971
+ dict: Dictionary where keys are identity names and values are lists of node IDs
9972
+ """
9973
+ identity_lists = {}
9974
+
9975
+ # Get all unique identities
9976
+ all_identities = set()
9977
+ import ast
9978
+ for item in my_network.node_identities:
9979
+ try:
9980
+ parse = ast.literal_eval(my_network.node_identities[item])
9981
+ if isinstance(parse, (list, tuple, set)):
9982
+ all_identities.update(parse)
9983
+ else:
9984
+ all_identities.add(str(parse))
9985
+ except:
9986
+ all_identities.add(str(my_network.node_identities[item]))
9987
+
9988
+ # For each identity, get the list of nodes that have it
9989
+ for identity in all_identities:
9990
+ iden_list = []
9991
+ for item in my_network.node_identities:
9992
+ try:
9993
+ parse = ast.literal_eval(my_network.node_identities[item])
9994
+ if identity in parse:
9995
+ iden_list.append(item)
9996
+ except:
9997
+ if identity == str(my_network.node_identities[item]):
9998
+ iden_list.append(item)
9999
+
10000
+ if iden_list: # Only add if we found nodes
10001
+ identity_lists[identity] = iden_list
10002
+
10003
+ return identity_lists
10004
+
10005
+ def normalize_df_with_identity_centerpoints(self, df, identity_lists):
10006
+ """
10007
+ Normalize the entire dataframe using identity-specific centerpoints.
10008
+ Uses Z-score-like normalization with identity centerpoint as the "mean".
10009
+
10010
+ Parameters:
10011
+ df (pd.DataFrame): Original dataframe
10012
+ identity_lists (dict): Dictionary where keys are identity names and values are lists of node IDs
10013
+
10014
+ Returns:
10015
+ pd.DataFrame: Normalized dataframe
10016
+ """
10017
+ # Make a copy to avoid modifying the original dataframe
10018
+ df_copy = df.copy()
10019
+
10020
+ # Set the first column as the index (row headers)
10021
+ df_copy = df_copy.set_index(df_copy.columns[0])
10022
+
10023
+ # Convert all remaining columns to float type (batch conversion)
10024
+ df_copy = df_copy.astype(float)
10025
+
10026
+ # First, calculate the centerpoint for each column by finding the median across all identity groups
10027
+ column_centerpoints = {}
10028
+
10029
+ for column in df_copy.columns:
10030
+ centerpoint = None
10031
+
10032
+ for identity, node_list in identity_lists.items():
10033
+ # Get nodes that exist in both the identity list and the dataframe
10034
+ valid_nodes = [node for node in node_list if node in df_copy.index]
10035
+ if valid_nodes and ((str(identity) == str(column)) or str(identity) == f'{str(column)}+'):
10036
+ # Get the median value for this identity in this column
10037
+ identity_min = df_copy.loc[valid_nodes, column].median()
10038
+ centerpoint = identity_min
10039
+ break # Found the match, no need to continue
10040
+
10041
+ if centerpoint is not None:
10042
+ # Use the identity-specific centerpoint
10043
+ column_centerpoints[column] = centerpoint
10044
+ else:
10045
+ # Fallback: if no matching identity, use column median
10046
+ column_centerpoints[column] = df_copy[column].median()
10047
+
10048
+ # Now normalize each column using Z-score-like calculation with identity centerpoint
10049
+ df_normalized = df_copy.copy()
10050
+ for column in df_copy.columns:
10051
+ centerpoint = column_centerpoints[column]
10052
+ # Calculate standard deviation of the column
10053
+ std_dev = df_copy[column].std()
10054
+
10055
+ if std_dev > 0: # Avoid division by zero
10056
+ # Z-score-like: (value - centerpoint) / std_dev
10057
+ df_normalized[column] = (df_copy[column] - centerpoint) / std_dev
10058
+ else:
10059
+ # If std_dev is 0, just subtract centerpoint
10060
+ df_normalized[column] = df_copy[column] - centerpoint
10061
+
10062
+ # Convert back to original format with first column as regular column
10063
+ df_normalized = df_normalized.reset_index()
10064
+
10065
+ return df_normalized
10066
+
10067
+ def run(self):
10068
+
10069
+ def df_to_dict_by_rows(df, row_indices):
10070
+ """
10071
+ Convert a pandas DataFrame to a dictionary by selecting specific rows.
10072
+ No normalization - dataframe is already normalized.
10073
+
10074
+ Parameters:
10075
+ df (pd.DataFrame): DataFrame with first column as row headers, remaining columns contain floats
10076
+ row_indices (list): List of values from the first column representing rows to include
10077
+
10078
+ Returns:
10079
+ dict: Dictionary where keys are column headers and values are lists of column values (as floats)
10080
+ for the specified rows
10081
+ """
10082
+ # Make a copy to avoid modifying the original dataframe
10083
+ df_copy = df.copy()
10084
+
10085
+ # Set the first column as the index (row headers)
10086
+ df_copy = df_copy.set_index(df_copy.columns[0])
10087
+
10088
+ # Mask the dataframe to include only the specified rows
10089
+ masked_df = df_copy.loc[row_indices]
10090
+
10091
+ # Create empty dictionary
10092
+ result_dict = {}
10093
+
10094
+ # For each column, add the column header as key and column values as list
10095
+ for column in masked_df.columns:
10096
+ result_dict[column] = masked_df[column].tolist()
10097
+
10098
+ return result_dict
10099
+
10100
+ from . import neighborhoods
10101
+
10102
+ if self.idens.currentIndex() != 0:
10103
+
10104
+ iden = self.idens.currentText()
10105
+ iden_list = []
10106
+ import ast
10107
+
10108
+ for item in my_network.node_identities:
10109
+
10110
+ try:
10111
+ parse = ast.literal_eval(my_network.node_identities[item])
10112
+ if iden in parse:
10113
+ iden_list.append(item)
10114
+ except:
10115
+ if iden == item:
10116
+ iden_list.append(item)
10117
+
10118
+ violin_dict = df_to_dict_by_rows(self.df, iden_list)
10119
+
10120
+ neighborhoods.create_violin_plots(violin_dict, graph_title=f"Z-Score-like Channel Intensities of Identity {iden}, {len(iden_list)} Nodes")
10121
+
10122
+
10123
+ if self.coms.currentIndex() != 0:
10124
+
10125
+ com = self.coms.currentText()
10126
+
10127
+ com_dict = n3d.invert_dict(my_network.communities) # Fixed: should be communities
10128
+
10129
+ com_list = com_dict[int(com)]
10130
+
10131
+ violin_dict = df_to_dict_by_rows(self.df, com_list)
10132
+
10133
+ neighborhoods.create_violin_plots(violin_dict, graph_title=f"Z-Score-like Channel Intensities of Community/Neighborhood {com}, {len(com_list)} Nodes")
10134
+
10135
+
10136
+ def run2(self):
10137
+ def df_to_dict(df):
10138
+ # Make a copy to avoid modifying the original dataframe
10139
+ df_copy = df.copy()
10140
+
10141
+ # Set the first column as the index (row headers)
10142
+ df_copy = df_copy.set_index(df_copy.columns[0])
10143
+
10144
+ # Convert all remaining columns to float type (batch conversion)
10145
+ df_copy = df_copy.astype(float)
10146
+
10147
+ # Create the result dictionary
10148
+ result_dict = {}
10149
+ for row_idx in df_copy.index:
10150
+ result_dict[row_idx] = df_copy.loc[row_idx].tolist()
10151
+
10152
+ return result_dict
10153
+
10154
+ try:
10155
+ umap_dict = df_to_dict(self.backup_df)
10156
+ my_network.identity_umap(umap_dict)
10157
+ except:
10158
+ pass
10159
+
10160
+
10161
+
10162
+
9895
10163
  class DegreeDialog(QDialog):
9896
10164
 
9897
10165
 
@@ -12931,16 +13199,58 @@ class GrayWaterDialog(QDialog):
12931
13199
  run_button.clicked.connect(self.run_watershed)
12932
13200
  layout.addRow(run_button)
12933
13201
 
13202
+ def wait_for_threshold_processing(self):
13203
+ """
13204
+ Opens ThresholdWindow and waits for user to process the image.
13205
+ Returns True if completed, False if cancelled.
13206
+ The thresholded image will be available in the main window after completion.
13207
+ """
13208
+ # Create event loop to wait for user
13209
+ loop = QEventLoop()
13210
+ result = {'completed': False}
13211
+
13212
+ # Create the threshold window
13213
+ thresh_window = ThresholdWindow(self.parent(), 0)
13214
+
13215
+
13216
+ # Connect signals
13217
+ def on_processing_complete():
13218
+ result['completed'] = True
13219
+ loop.quit()
13220
+
13221
+ def on_processing_cancelled():
13222
+ result['completed'] = False
13223
+ loop.quit()
13224
+
13225
+ thresh_window.processing_complete.connect(on_processing_complete)
13226
+ thresh_window.processing_cancelled.connect(on_processing_cancelled)
13227
+
13228
+ # Show window and wait
13229
+ thresh_window.show()
13230
+ thresh_window.raise_()
13231
+ thresh_window.activateWindow()
13232
+
13233
+ # Block until user clicks "Apply Threshold & Continue" or "Cancel"
13234
+ loop.exec()
13235
+
13236
+ # Clean up
13237
+ thresh_window.deleteLater()
13238
+
13239
+ return result['completed']
13240
+
12934
13241
  def run_watershed(self):
12935
13242
 
12936
13243
  try:
12937
13244
 
13245
+ self.accept()
13246
+ print("Please threshold foreground, or press cancel/skip if not desired:")
13247
+ self.wait_for_threshold_processing()
13248
+ data = self.parent().channel_data[self.parent().active_channel]
13249
+
12938
13250
  min_intensity = float(self.min_intensity.text()) if self.min_intensity.text().strip() else None
12939
13251
 
12940
13252
  min_peak_distance = int(self.min_peak_distance.text()) if self.min_peak_distance.text().strip() else 1
12941
13253
 
12942
- data = self.parent().channel_data[self.parent().active_channel]
12943
-
12944
13254
  data = n3d.gray_watershed(data, min_peak_distance, min_intensity)
12945
13255
 
12946
13256
  self.parent().load_channel(self.parent().active_channel, data, data = True, preserve_zoom = (self.parent().ax.get_xlim(), self.parent().ax.get_ylim()))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nettracer3d
3
- Version: 1.0.1
3
+ Version: 1.0.2
4
4
  Summary: Scripts for intializing and analyzing networks from segmentations of three dimensional images.
5
5
  Author-email: Liam McLaughlin <liamm@wustl.edu>
6
6
  Project-URL: Documentation, https://nettracer3d.readthedocs.io/en/latest/
@@ -110,6 +110,7 @@ McLaughlin, L., Zhang, B., Sharma, S. et al. Three dimensional multiscalar neuro
110
110
 
111
111
  NetTracer3D was developed by Liam McLaughlin while working under Dr. Sanjay Jain at Washington University School of Medicine.
112
112
 
113
- -- Version 1.0.1 Updates --
113
+ -- Version 1.0.2 Updates --
114
114
 
115
- * Bug fixes, mainly
115
+ * Minor fixes
116
+ * Added ability to generate violin plots using the table generated from merging node identities, showing the relative expression of markers for multiple channels for the nodes belonging to some channel or community/neighborhood
File without changes
File without changes