-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSpotAndDivisionCountChart.java
More file actions
471 lines (386 loc) · 17.4 KB
/
SpotAndDivisionCountChart.java
File metadata and controls
471 lines (386 loc) · 17.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*-
* #%L
* mastodon-tomancak
* %%
* Copyright (C) 2018 - 2025 Tobias Pietzsch
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.mastodon.mamut.tomancak.divisioncount;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;
import net.miginfocom.swing.MigLayout;
import org.apache.commons.lang3.tuple.Triple;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.mastodon.mamut.ProjectModel;
import org.mastodon.model.SelectionListener;
import org.scijava.prefs.PrefService;
public class SpotAndDivisionCountChart extends JFrame implements SelectionListener
{
private static final String SPOT_COLOR = "spotColor";
private static final String DIVISION_COLOR = "divisionColor";
private static final String SPOT_COUNT_VISIBILITY = "spotCountVisibility";
private static final String DIVISION_COUNT_VISIBILITY = "divisionCountVisibility";
private static final String SPOT_COUNT_AVERAGE_VISIBILITY = "spotCountAverageVisibility";
private static final String DIVISION_COUNT_AVERAGE_VISIBILITY = "divisionCountAverageVisibility";
private static final String SPOT_COUNT_SLIDING_AVERAGE_WINDOW_SIZE = "spotCountSlidingAverageWindowSize";
private static final String DIVISION_COUNT_SLIDING_AVERAGE_WINDOW_SIZE = "divisionCountSlidingAverageWindowSize";
private static final String ONLY_SELECTED_SPOTS = "onlySelectedSpots";
private static final int SPOT_COUNT_DEFAULT_COLOR = new Color( 230, 159, 0 ).getRGB(); // Dark Orange
private static final int DIVISION_COUNT_DEFAULT_COLOR = new Color( 86, 180, 233 ).getRGB(); // Light Blue
private static final int DEFAULT_SLIDING_WINDOW_SIZE = 10;
public static final String GROW_X = "growx";
private Color spotCountColor; // Dark Orange
private Color divisionCountColor; // Light Blue
private static final String TITLE = "Spot and Division Counts over Time";
private static final String SPOTS_COUNT_SERIES_NAME = "Spot Counts";
private static final String DIVISION_COUNT_SERIES_NAME = "Division Counts";
private final XYPlot plot;
private final NumberAxis leftAxis;
private final NumberAxis rightAxis;
private int spotWindowSize;
private int divisionWindowSize;
private boolean onlySelectedSpots;
private final PrefService prefs;
private final ProjectModel projectModel;
private XYSeriesCollection spotCountsSeries;
private XYSeriesCollection divisionCountsSeries;
SpotAndDivisionCountChart( final ProjectModel projectModel, final PrefService prefs )
{
this.projectModel = projectModel;
this.projectModel.getSelectionModel().listeners().add( this );
this.prefs = prefs;
this.spotCountColor = new Color(
prefs.getInt( SpotAndDivisionCountChart.class, SPOT_COLOR, SPOT_COUNT_DEFAULT_COLOR ) );
this.divisionCountColor = new Color(
prefs.getInt( SpotAndDivisionCountChart.class, DIVISION_COLOR, DIVISION_COUNT_DEFAULT_COLOR ) );
this.spotWindowSize =
prefs.getInt( SpotAndDivisionCountChart.class, SPOT_COUNT_SLIDING_AVERAGE_WINDOW_SIZE, DEFAULT_SLIDING_WINDOW_SIZE );
this.divisionWindowSize =
prefs.getInt( SpotAndDivisionCountChart.class, DIVISION_COUNT_SLIDING_AVERAGE_WINDOW_SIZE, DEFAULT_SLIDING_WINDOW_SIZE );
this.onlySelectedSpots = prefs.getBoolean( SpotAndDivisionCountChart.class, ONLY_SELECTED_SPOTS, false );
updateChartData();
JFreeChart chart = ChartFactory.createXYLineChart(
TITLE,
"Timepoints",
null,
spotCountsSeries,
PlotOrientation.VERTICAL,
true,
true,
false
);
plot = chart.getXYPlot();
// Customize the left y-axis (Spot Counts)
leftAxis = ( NumberAxis ) plot.getRangeAxis();
customizeAxis( leftAxis, SPOTS_COUNT_SERIES_NAME, spotCountColor );
plot.setRangeAxis( 0, leftAxis );
plot.setDataset( 0, spotCountsSeries );
plot.mapDatasetToRangeAxis( 0, 0 );
// Add the right y-axis (Division Counts)
rightAxis = new NumberAxis();
customizeAxis( rightAxis, DIVISION_COUNT_SERIES_NAME, divisionCountColor );
plot.setRangeAxis( 1, rightAxis );
plot.setDataset( 1, divisionCountsSeries );
plot.mapDatasetToRangeAxis( 1, 1 );
XYLineAndShapeRenderer spotCountRenderer = createRenderer(
spotCountColor, new Rectangle2D.Double( -2, -2, 4, 4 ) );
plot.setRenderer( 0, spotCountRenderer );
XYLineAndShapeRenderer divisionCountRenderer = createRenderer(
divisionCountColor, new Ellipse2D.Double( -2, -2, 4, 4 ) );
plot.setRenderer( 1, divisionCountRenderer );
// Set up the chart panel
ChartPanel chartPanel = new ChartPanel( chart );
chartPanel.setPreferredSize( new Dimension( 800, 600 ) );
// Add color chooser and visibility controls
JPanel controlPanel = createControlPanel();
// Set up the frame layout
setLayout( new BorderLayout() );
add( chartPanel, BorderLayout.CENTER );
add( controlPanel, BorderLayout.SOUTH );
// Set up the frame
setTitle( TITLE );
setSize( 800, 700 );
setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
setLocationRelativeTo( null );
repaint();
}
private void updateChartData()
{
List< Triple< Integer, Integer, Integer > > counts =
SpotAndDivisionCount.getSpotAndDivisionsPerTimepoint( projectModel, this.onlySelectedSpots );
double[] timepoints = counts.stream().mapToDouble( Triple::getLeft ).toArray();
double[] spotCounts = counts.stream().mapToDouble( Triple::getMiddle ).toArray();
double[] divisionCounts = counts.stream().mapToDouble( Triple::getRight ).toArray();
spotCountsSeries = createSeries( timepoints, spotCounts, SPOTS_COUNT_SERIES_NAME, spotWindowSize );
divisionCountsSeries = createSeries( timepoints, divisionCounts, DIVISION_COUNT_SERIES_NAME, divisionWindowSize );
if ( plot != null )
{
plot.setDataset( 0, spotCountsSeries );
plot.setDataset( 1, divisionCountsSeries );
}
}
public static void show( final ProjectModel projectModel, final PrefService prefService )
{
SpotAndDivisionCountChart chart = new SpotAndDivisionCountChart( projectModel, prefService );
chart.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
chart.setVisible( true );
}
/**
* Creates a control panel with color choosers and checkboxes for visibility controls.
*/
private JPanel createControlPanel()
{
JPanel controlPanel = new JPanel( new MigLayout( "fill, wrap 5", "[grow]", "[]10[]10[]10[]10[]" ) );
// Spot-related controls
JButton spotColorButton = new JButton( "Choose Spot Color" );
spotColorButton.addActionListener( e -> {
Color newColor = JColorChooser.showDialog( null, "Choose Spot Color", spotCountColor );
if ( newColor != null )
{
spotCountColor = newColor;
prefs.put( SpotAndDivisionCountChart.class, SPOT_COLOR, spotCountColor.getRGB() );
updateChartColors();
}
} );
boolean spotCountVisibility = prefs.getBoolean( SpotAndDivisionCountChart.class, SPOT_COUNT_VISIBILITY, true );
boolean spotCountAverageVisibility = prefs.getBoolean( SpotAndDivisionCountChart.class, SPOT_COUNT_AVERAGE_VISIBILITY, true );
JCheckBox showSpotCounts = new JCheckBox( "Show Spot Counts", spotCountVisibility );
JCheckBox showSpotAverage = new JCheckBox( "Show Spot Sliding Average", spotCountAverageVisibility );
showSpotCounts.addActionListener( e -> {
plot.getRenderer( 0 ).setSeriesVisible( 0, showSpotCounts.isSelected() );
prefs.put( SpotAndDivisionCountChart.class, SPOT_COUNT_VISIBILITY, showSpotCounts.isSelected() );
updateAxisVisibility();
} );
showSpotAverage.addActionListener( e -> {
plot.getRenderer( 0 ).setSeriesVisible( 1, showSpotAverage.isSelected() );
prefs.put( SpotAndDivisionCountChart.class, SPOT_COUNT_AVERAGE_VISIBILITY, showSpotAverage.isSelected() );
updateAxisVisibility();
} );
JSpinner spotWindowSpinner = new JSpinner( new SpinnerNumberModel( spotWindowSize, 1, Integer.MAX_VALUE, 1 ) );
spotWindowSpinner.addChangeListener( e -> {
spotWindowSize = ( int ) spotWindowSpinner.getValue();
prefs.put( SpotAndDivisionCountChart.class, SPOT_COUNT_SLIDING_AVERAGE_WINDOW_SIZE, spotWindowSize );
updateChartData();
} );
// Division-related controls
JButton divisionColorButton = new JButton( "Choose Division Color" );
divisionColorButton.addActionListener( e -> {
Color newColor = JColorChooser.showDialog( null, "Choose Division Color", divisionCountColor );
if ( newColor != null )
{
divisionCountColor = newColor;
prefs.put( SpotAndDivisionCountChart.class, DIVISION_COLOR, divisionCountColor.getRGB() );
updateChartColors();
}
} );
boolean divisionCountVisibility = prefs.getBoolean( SpotAndDivisionCountChart.class, DIVISION_COUNT_VISIBILITY, true );
boolean divisionAverageVisibility = prefs.getBoolean( SpotAndDivisionCountChart.class, DIVISION_COUNT_AVERAGE_VISIBILITY, true );
JCheckBox showDivisionCounts = new JCheckBox( "Show Division Counts", divisionCountVisibility );
JCheckBox showDivisionAverage = new JCheckBox( "Show Division Sliding Average", divisionAverageVisibility );
showDivisionCounts.addActionListener( e -> {
plot.getRenderer( 1 ).setSeriesVisible( 0, showDivisionCounts.isSelected() );
prefs.put( SpotAndDivisionCountChart.class, DIVISION_COUNT_VISIBILITY, showDivisionCounts.isSelected() );
updateAxisVisibility();
} );
showDivisionAverage.addActionListener( e -> {
plot.getRenderer( 1 ).setSeriesVisible( 1, showDivisionAverage.isSelected() );
prefs.put( SpotAndDivisionCountChart.class, DIVISION_COUNT_AVERAGE_VISIBILITY, showDivisionAverage.isSelected() );
updateAxisVisibility();
} );
JSpinner divisionWindowSpinner = new JSpinner( new SpinnerNumberModel( divisionWindowSize, 1, Integer.MAX_VALUE, 1 ) );
divisionWindowSpinner.addChangeListener( e -> {
divisionWindowSize = ( int ) divisionWindowSpinner.getValue();
prefs.put( SpotAndDivisionCountChart.class, DIVISION_COUNT_SLIDING_AVERAGE_WINDOW_SIZE, divisionWindowSize );
updateChartData();
} );
JRadioButton allSpots = new JRadioButton( "All spots", !onlySelectedSpots );
JRadioButton selectedSpots = new JRadioButton( "Only selected spots", onlySelectedSpots );
ButtonGroup group = new ButtonGroup();
group.add( allSpots );
group.add( selectedSpots );
allSpots.addActionListener( e -> {
onlySelectedSpots = false;
prefs.put( SpotAndDivisionCountChart.class, ONLY_SELECTED_SPOTS, onlySelectedSpots );
updateChartData();
repaint();
} );
selectedSpots.addActionListener( e -> {
this.onlySelectedSpots = true;
prefs.put( SpotAndDivisionCountChart.class, ONLY_SELECTED_SPOTS, onlySelectedSpots );
updateChartData();
repaint();
} );
// Add components to the control panel
controlPanel.add( spotColorButton, GROW_X );
controlPanel.add( showSpotCounts, GROW_X );
controlPanel.add( showSpotAverage, GROW_X );
controlPanel.add( new JLabel( "Window Size:" ), "align right" );
controlPanel.add( spotWindowSpinner, "wmax 50" );
controlPanel.add( divisionColorButton, GROW_X );
controlPanel.add( showDivisionCounts, GROW_X );
controlPanel.add( showDivisionAverage, GROW_X );
controlPanel.add( new JLabel( "Window Size:" ), "align right" );
controlPanel.add( divisionWindowSpinner, "wmax 50" );
controlPanel.add( allSpots, GROW_X );
controlPanel.add( selectedSpots, "span, growx" );
// Add description
controlPanel.add( new JLabel(
"<html>This windows shows the number of spots and divisions at each timepoint together with a sliding average.<br>A division is defined as a spot with more than one outgoing edge.</html>" ),
"span 5" );
plot.getRenderer( 0 ).setSeriesVisible( 0, spotCountVisibility );
plot.getRenderer( 0 ).setSeriesVisible( 1, spotCountAverageVisibility );
plot.getRenderer( 1 ).setSeriesVisible( 0, divisionCountVisibility );
plot.getRenderer( 1 ).setSeriesVisible( 1, divisionAverageVisibility );
updateAxisVisibility();
return controlPanel;
}
/**
* Updates the chart colors based on the user's selection.
*/
private void updateChartColors()
{
// Update the renderer colors
XYLineAndShapeRenderer spotCountRenderer = createRenderer(
spotCountColor, new Rectangle2D.Double( -2, -2, 4, 4 ) );
plot.setRenderer( 0, spotCountRenderer );
XYLineAndShapeRenderer divisionCountRenderer = createRenderer(
divisionCountColor, new Ellipse2D.Double( -2, -2, 4, 4 ) );
plot.setRenderer( 1, divisionCountRenderer );
// Update axis label colors
customizeAxis( leftAxis, SPOTS_COUNT_SERIES_NAME, spotCountColor );
customizeAxis( rightAxis, DIVISION_COUNT_SERIES_NAME, divisionCountColor );
// Repaint the chart
repaint();
}
/**
* Updates the visibility of the axis labels based on the state of the checkboxes.
*/
private void updateAxisVisibility()
{
updateAxisVisibility( plot.getRenderer( 0 ), leftAxis, SPOTS_COUNT_SERIES_NAME );
updateAxisVisibility( plot.getRenderer( 1 ), rightAxis, DIVISION_COUNT_SERIES_NAME );
repaint();
}
private void updateAxisVisibility( final XYItemRenderer renderer, final NumberAxis axis,
final String labelIfVisible )
{
Boolean countVisible = renderer.getSeriesVisible( 0 );
Boolean averageVisible = renderer.getSeriesVisible( 1 );
boolean isCountVisible = countVisible != null && countVisible;
boolean isAverageVisible = averageVisible != null && averageVisible;
boolean showAxis = isCountVisible || isAverageVisible;
axis.setLabel( showAxis ? labelIfVisible : null );
axis.setVisible( showAxis );
}
private XYLineAndShapeRenderer createRenderer( final Color color, final Shape shape )
{
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
// Configure raw data points
renderer.setSeriesPaint( 0, color );
renderer.setSeriesShape( 0, shape );
renderer.setSeriesShapesVisible( 0, true );
renderer.setSeriesLinesVisible( 0, false );
// Configure sliding average line
renderer.setSeriesPaint( 1, color );
renderer.setSeriesShapesVisible( 1, false );
renderer.setSeriesLinesVisible( 1, true );
renderer.setSeriesStroke( 1, new BasicStroke( 2.0f ) );
return renderer;
}
/**
* Customizes an axis with bold font and a specific color.
*
* @param axis The axis to customize.
* @param label The label for the axis.
* @param color The color for the axis font.
*/
private void customizeAxis( final NumberAxis axis, final String label, final Color color )
{
axis.setLabel( label );
axis.setLabelFont( new Font( Font.DIALOG, Font.BOLD, 14 ) );
axis.setLabelPaint( color );
axis.setTickLabelFont( new Font( Font.DIALOG, Font.BOLD, 12 ) );
axis.setTickLabelPaint( color );
}
private static XYSeriesCollection createSeries( final double[] xValues, final double[] yValues, final String seriesName,
final int windowSize )
{
XYSeries originalSeries = new XYSeries( seriesName );
XYSeries smoothedSeries = new XYSeries( "Sliding Average" );
for ( int i = 0; i < xValues.length; i++ )
originalSeries.add( xValues[ i ], yValues[ i ] );
double[] smoothedValues = calculateSlidingAverage( yValues, windowSize );
for ( int i = 0; i < xValues.length; i++ )
smoothedSeries.add( xValues[ i ], smoothedValues[ i ] );
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries( originalSeries );
dataset.addSeries( smoothedSeries );
return dataset;
}
private static double[] calculateSlidingAverage( double[] values, int windowSize )
{
double[] result = new double[ values.length ];
for ( int i = 0; i < values.length; i++ )
{
int start = Math.max( 0, i - windowSize + 1 );
double sum = 0;
for ( int j = start; j <= i; j++ )
sum += values[ j ];
result[ i ] = sum / ( i - start + 1 );
}
return result;
}
@Override
public void selectionChanged()
{
updateChartData();
}
}