nettracer3d 0.7.6__py3-none-any.whl → 0.7.8__py3-none-any.whl

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.
@@ -533,46 +533,30 @@ def assign_node_colors(node_list: List[int], labeled_array: np.ndarray) -> Tuple
533
533
  return rgba_array, node_to_color_names
534
534
 
535
535
  def assign_community_colors(community_dict: Dict[int, int], labeled_array: np.ndarray) -> Tuple[np.ndarray, Dict[int, str]]:
536
- """
537
- Assign distinct colors to communities and create an RGBA image.
538
-
539
- Args:
540
- community_dict: Dictionary mapping node IDs to community numbers
541
- labeled_array: 3D numpy array with labels corresponding to node IDs
536
+ """Ultra-fast version using lookup table approach."""
542
537
 
543
- Returns:
544
- Tuple of (RGBA-coded numpy array (H, W, D, 4), dictionary mapping nodes to color names)
545
- """
546
- # Get unique communities and their sizes
538
+ # Same setup as before
547
539
  communities = set(community_dict.values())
548
540
  community_sizes = Counter(community_dict.values())
549
-
550
- # Sort communities by size (descending)
551
541
  sorted_communities = sorted(communities, key=lambda x: community_sizes[x], reverse=True)
552
542
 
553
- # Generate distinct colors
554
543
  colors = generate_distinct_colors(len(communities))
544
+ colors_rgba = np.array([(r, g, b, 255) for r, g, b in colors], dtype=np.uint8)
555
545
 
556
- # Convert RGB colors to RGBA by adding alpha channel
557
- colors_rgba = [(r, g, b, 255) for r, g, b in colors] # Full opacity for colored regions
558
-
559
- # Create mapping from community to color
560
546
  community_to_color = {comm: colors_rgba[i] for i, comm in enumerate(sorted_communities)}
561
-
562
- # Create mapping from node ID to color
563
547
  node_to_color = {node: community_to_color[comm] for node, comm in community_dict.items()}
564
548
 
565
- # Create RGBA array (initialize with transparent background)
566
- rgba_array = np.zeros((*labeled_array.shape, 4), dtype=np.uint8)
549
+ # Create lookup table - this is the key optimization
550
+ max_label = max(max(labeled_array.flat), max(node_to_color.keys()) if node_to_color else 0)
551
+ color_lut = np.zeros((max_label + 1, 4), dtype=np.uint8) # Transparent by default
567
552
 
568
- # Assign colors to each voxel based on its label
569
- for label in np.unique(labeled_array):
570
- if label in node_to_color: # Skip background (usually label 0)
571
- mask = labeled_array == label
572
- for i in range(4): # RGBA channels
573
- rgba_array[mask, i] = node_to_color[label][i]
574
-
575
- # Convert the RGB portion of community_to_color back to RGB for color naming
553
+ for node_id, color in node_to_color.items():
554
+ color_lut[node_id] = color
555
+
556
+ # Single vectorized operation - this is much faster!
557
+ rgba_array = color_lut[labeled_array]
558
+
559
+ # Rest remains the same
576
560
  community_to_color_rgb = {k: tuple(v[:3]) for k, v in community_to_color.items()}
577
561
  node_to_color_names = convert_node_colors_to_names(community_to_color_rgb)
578
562