• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vanvalenlab / deepcell-label / 4030421366

pending completion
4030421366

Pull #392

github

GitHub
Merge f32c6d60c into afe72b53d
Pull Request #392: Implement cell types machine, basic controls, and canvas rendering

334 of 697 branches covered (47.92%)

Branch coverage included in aggregate %.

52 of 248 new or added lines in 24 files covered. (20.97%)

65 existing lines in 2 files now uncovered.

2484 of 3864 relevant lines covered (64.29%)

1105.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/frontend/src/Project/Canvas/ToolCanvas/CellTypeCanvas.js
1
// Canvas when visualizing cell types
2
import { useSelector } from '@xstate/react';
3
import { useEffect, useRef } from 'react';
4
import {
5
  useAlphaGpu,
6
  useArrays,
7
  useCanvas,
8
  useCellMatrix,
9
  useCellTypes,
10
  useImage,
11
  useLabeled,
12
  useSelectedCell,
13
} from '../../ProjectContext';
14

15
function CellTypeCanvas({ setBitmaps }) {
NEW
16
  const canvas = useCanvas();
×
NEW
17
  const width = useSelector(canvas, (state) => state.context.width);
×
NEW
18
  const height = useSelector(canvas, (state) => state.context.height);
×
19

NEW
20
  const labeled = useLabeled();
×
NEW
21
  const feature = useSelector(labeled, (state) => state.context.feature);
×
22

NEW
23
  const image = useImage();
×
NEW
24
  const t = useSelector(image, (state) => state.context.t);
×
25

NEW
26
  const arrays = useArrays();
×
NEW
27
  const labeledArray = useSelector(
×
28
    arrays,
NEW
29
    (state) => state.context.labeled && state.context.labeled[feature][t]
×
30
  );
31

NEW
32
  const cell = useSelectedCell();
×
33

NEW
34
  const cellTypes = useCellTypes();
×
NEW
35
  const colorMap = useSelector(cellTypes, (state) => state.context.colorMap);
×
36

NEW
37
  const cellMatrix = useCellMatrix();
×
38

NEW
39
  const gpu = useAlphaGpu();
×
NEW
40
  const kernelRef = useRef();
×
41

NEW
42
  useEffect(() => {
×
NEW
43
    const kernel = gpu.createKernel(
×
44
      `function (data, cell, cells, numLabels, numValues, colorMap) {
45
        const x = this.thread.x;
46
        const y = this.constants.h - 1 - this.thread.y;
47
        const value = data[y][x];
48
        let north = value;
49
        let south = value;
50
        let east = value;
51
        let west = value;
52
        if (x !== 0) {
53
          north = data[y][x - 1];
54
        }
55
        if (x !== this.constants.w - 1) {
56
          south = data[y][x + 1];
57
        }
58
        if (y !== 0) {
59
          west = data[y - 1][x];
60
        }
61
        if (y !== this.constants.h - 1) {
62
          east = data[y + 1][x];
63
        }
64
        let outlineOpacity = 1;
65
        if (value < numValues) {
66
          for (let i = 0; i < numLabels; i++) {
67
            if (cells[value][i] === 1) {
68
              if (cells[north][i] === 0 || cells[south][i] === 0 || cells[west][i] === 0 || cells[east][i] === 0
69
                  || north >= numValues || south >= numValues || west >= numValues || east >= numValues)
70
              {
71
                if (i === cell) {
72
                  this.color(1, 1, 1, 1);
73
                }
74
                else {
75
                  const [r, g, b, a] = colorMap[i];
76
                  this.color(r, g, b, a);
77
                }
78
              }
79
              else {
80
                if (colorMap[i][3] !== 0) {
81
                  const [r, g, b, a] = colorMap[i];
82
                  this.color(r, g, b, 0.3);
83
                }
84
              }
85
            }
86
          }
87
        }
88
      }`,
89
      {
90
        constants: { w: width, h: height },
91
        output: [width, height],
92
        graphical: true,
93
        dynamicArguments: true,
94
      }
95
    );
NEW
96
    kernelRef.current = kernel;
×
97
  }, [gpu, width, height]);
98

NEW
99
  useEffect(() => {
×
NEW
100
    const kernel = kernelRef.current;
×
101

NEW
102
    if (labeledArray && cellMatrix) {
×
NEW
103
      const numValues = cellMatrix.length;
×
NEW
104
      const numLabels = cellMatrix[0].length;
×
NEW
105
      kernel(labeledArray, cell, cellMatrix, numLabels, numValues, colorMap);
×
106
      // Rerender the parent canvas
NEW
107
      createImageBitmap(kernel.canvas).then((bitmap) => {
×
NEW
108
        setBitmaps((bitmaps) => ({ ...bitmaps, types: bitmap }));
×
109
      });
110
    }
111
  }, [labeledArray, cell, cellMatrix, colorMap, setBitmaps, width, height]);
112

NEW
113
  useEffect(
×
NEW
114
    () => () =>
×
NEW
115
      setBitmaps((bitmaps) => {
×
NEW
116
        const { types, ...rest } = bitmaps;
×
NEW
117
        return rest;
×
118
      }),
119
    [setBitmaps]
120
  );
121

NEW
122
  return null;
×
123
}
124

125
export default CellTypeCanvas;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc