-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVisualizeStack.java
More file actions
384 lines (316 loc) · 13.8 KB
/
VisualizeStack.java
File metadata and controls
384 lines (316 loc) · 13.8 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
package examples;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import org.scijava.ui.behaviour.KeyStrokeAdder;
import org.scijava.ui.behaviour.io.InputTriggerConfig;
import org.scijava.ui.behaviour.io.InputTriggerDescription;
import org.scijava.ui.behaviour.util.AbstractNamedAction;
import align.AlignTools;
import bdv.util.BdvFunctions;
import bdv.util.BdvOptions;
import bdv.util.BdvStackSource;
import bdv.viewer.DisplayMode;
import cmd.RenderImage;
import data.STData;
import data.STDataStatistics;
import data.STDataUtils;
import filter.FilterFactory;
import filter.MedianFilterFactory;
import filter.SingleSpotRemovingFilterFactory;
import gui.STDataAssembly;
import gui.bdv.AddedGene;
import gui.bdv.AddedGene.Rendering;
import ij.ImageJ;
import ij.ImagePlus;
import imglib2.StackedIterableRealInterval;
import imglib2.TransformedIterableRealInterval;
import io.Path;
import io.SpatialDataContainer;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.IterableRealInterval;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealRandomAccessible;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineTransform2D;
import net.imglib2.realtransform.RealViews;
import net.imglib2.realtransform.Scale3D;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.Intervals;
import net.imglib2.view.Views;
import render.Render;
import tools.BDVFlyThrough;
import tools.BDVFlyThrough.CallbackBDV;
public class VisualizeStack
{
/*
protected static double minRange = 0;
protected static double maxRange = 100;
protected static double min = 0.1;
protected static double max = 5.5;
*/
public static class STIMStack
{
public RealRandomAccessible< DoubleType > rra;
public Interval interval;
public double minDisplay, maxDisplay;
}
public static BdvStackSource<?> render2d( final STDataAssembly stdata )
{
final List< FilterFactory< DoubleType, DoubleType > > filterFactorys = new ArrayList<>();
//filterFactorys.add( new MedianFilterFactory<>( new DoubleType( 0 ), 50.0 ) );
//filterFactorys.add( new GaussianFilterFactory<>( new DoubleType( 0 ), 50.0, WeightType.BY_SUM_OF_WEIGHTS ) );
//filterFactorys.add( new MeanFilterFactory<>( new DoubleType( 0 ), 50.0 ) );
filterFactorys.add( new SingleSpotRemovingFilterFactory<>( new DoubleType( 0 ), stdata.statistics().getMedianDistance() * 1.5 ) );
final String gene = "Calm2";
final RealRandomAccessible< DoubleType > renderRRA = Render.getRealRandomAccessible( stdata, gene, 1.0, filterFactorys );
final Interval interval =
STDataUtils.getIterableInterval(
new TransformedIterableRealInterval<>(
stdata.data(),
stdata.transform() ) );
final double brightnessMin = 0.0;
final double brightnessMax = 0.5;
final double[] minmax = AddedGene.minmax( stdata.data().getExprData( gene ) );
double minDisplay = AddedGene.getDisplayMin( minmax[ 0 ], minmax[ 1 ], brightnessMin );
double maxDisplay = AddedGene.getDisplayMax( minmax[ 1 ], brightnessMax );
final BdvOptions options = BdvOptions.options().is2D().numRenderingThreads( Runtime.getRuntime().availableProcessors() / 2 );
BdvStackSource<?> bdv = BdvFunctions.show( renderRRA, interval, gene, options );
bdv.setDisplayRangeBounds( minDisplay, minmax[ 1 ] );
bdv.setDisplayRange( minDisplay, maxDisplay );
//bdv.setColor( new ARGBType( ARGBType.rgba( 255, 0, 0, 0 ) ) );
//bdv.getBdvHandle().getViewerPanel().setDisplayMode( DisplayMode.SINGLE );
//bdv.setCurrent();
return bdv;
}
public static RandomAccessibleInterval< DoubleType > render3d_IJ( final List< STDataAssembly > stdata, final String gene, final double scale )
{
final DoubleType outofbounds = new DoubleType( 0 );
final List< FilterFactory< DoubleType, DoubleType > > filterFactorys = new ArrayList<>();
filterFactorys.add( new MedianFilterFactory<>( new DoubleType( 0 ), 20.0 ) );
//filterFactorys.add( new GaussianFilterFactory<>( new DoubleType( 0 ), 50.0, WeightType.BY_SUM_OF_WEIGHTS ) );
//filterFactorys.add( new MeanFilterFactory<>( new DoubleType( 0 ), 50.0 ) );
filterFactorys.add( new SingleSpotRemovingFilterFactory<>( outofbounds, 30 ) );
final STIMStack stack = createStack( stdata, gene, outofbounds, 4.0, 0, 0.5, Rendering.Gauss, 1, filterFactorys );
if ( scale != 1.0 )
{
final AffineGet scaleTransform = new Scale3D( scale, scale, scale );
final RandomAccessible< DoubleType > scaledImg = RealViews.affine( stack.rra, scaleTransform );
return Views.interval( scaledImg, Intervals.smallestContainingInterval( Intervals.scale( Intervals.expand( stack.interval, 200, 2 ), scale ) ) );
}
else
{
return Views.interval( Views.raster( stack.rra ), stack.interval );
}
}
public static BdvStackSource< ? > render3d( final List< STDataAssembly > stdata )
{
final DoubleType outofbounds = new DoubleType( 0 );
final List< FilterFactory< DoubleType, DoubleType > > filterFactorys = new ArrayList<>();
filterFactorys.add( new MedianFilterFactory<>( new DoubleType( 0 ), 20.0 ) );
//filterFactorys.add( new GaussianFilterFactory<>( new DoubleType( 0 ), 50.0, WeightType.BY_SUM_OF_WEIGHTS ) );
//filterFactorys.add( new MeanFilterFactory<>( new DoubleType( 0 ), 50.0 ) );
filterFactorys.add( new SingleSpotRemovingFilterFactory<>( outofbounds, 30 ) );
final STIMStack stack = createStack( stdata, "Calm2", outofbounds, 4.0, 0, 0.5, Rendering.Gauss, 2, filterFactorys );
final Interval interval = stack.interval;
final BdvOptions options = BdvOptions.options().numRenderingThreads( Runtime.getRuntime().availableProcessors() );
BdvStackSource< ? > source = BdvFunctions.show( stack.rra, interval, "Calm2", options );
source.setDisplayRangeBounds( stack.minDisplay, stack.maxDisplay * 2 );
source.setDisplayRange( stack.minDisplay, stack.maxDisplay );
//source.getBdvHandle().getViewerPanel().setDisplayMode( DisplayMode.SINGLE );
//source.setCurrent();
return source;
}
public static void renderMovie3d( final List< STDataAssembly > stdata, final BdvStackSource< ? > source )
{
final List< String > genesToVisualize = new ArrayList<>();
genesToVisualize.add( "Actb" );
genesToVisualize.add( "Ubb" );
genesToVisualize.add( "Hpca" );
genesToVisualize.add( "Calm2" );
genesToVisualize.add( "Mbp" );
genesToVisualize.add( "Fth1" );
genesToVisualize.add( "Pcp4" );
genesToVisualize.add( "Ptgds" );
genesToVisualize.add( "Ttr" );
genesToVisualize.add( "Calm1" );
genesToVisualize.add( "Fkbp1a" );
final DoubleType outofbounds = new DoubleType( 0 );
setupRecordMovie(
source,
(i, oldSource) ->
{
if ( i % 20 == 0 && i <= 220 )
{
final int newGeneIndex = ( i == 220 ) ? 0 : i / 20;
final STIMStack stack =
createStack( stdata, genesToVisualize.get( newGeneIndex ), outofbounds, 4.0, 0, 0.5, Rendering.Gauss, 2.0, null );
BdvStackSource<?> newSource =
BdvFunctions.show(
stack.rra,
stack.interval,
genesToVisualize.get( newGeneIndex ),
BdvOptions.options().numRenderingThreads( Runtime.getRuntime().availableProcessors() ) );
newSource.setDisplayRange( stack.minDisplay, stack.maxDisplay );
newSource.getBdvHandle().getViewerPanel().setDisplayMode( DisplayMode.SINGLE );
if ( oldSource != null )
oldSource.close();
return newSource;
}
else
{
return oldSource;
} } );
}
/*
public static Pair< RealRandomAccessible< DoubleType >, Interval > createStack( final List< STDataAssembly > stdata, final String gene, final DoubleType outofbounds )
{
return createStack(stdata, gene, outofbounds, 5.0, null );
}
public static Pair< RealRandomAccessible< DoubleType >, Interval > createStack(
final List< STDataAssembly > stdata,
final String gene,
final DoubleType outofbounds,
final double spacingFactor,
final List< FilterFactory< DoubleType, DoubleType > > filterFactorys )
{
return createStack(stdata, gene, outofbounds, spacingFactor, 1.5, filterFactorys);
}
*/
public static STIMStack createStack(
final List< STDataAssembly > stdata,
final String gene,
final DoubleType outofbounds,
final double spacingFactor,
final double brightnessMin,
final double brightnessMax,
final Rendering renderType,
final double renderingFactor,
final List< FilterFactory< DoubleType, DoubleType > > filterFactorys )
{
final ArrayList< IterableRealInterval< DoubleType > > slices = new ArrayList<>();
double minDisplay = Double.MAX_VALUE;
double maxDisplay = -Double.MAX_VALUE;
for (STDataAssembly stdatum : stdata) {
final IterableRealInterval<DoubleType> data = Render.getRealIterable(stdatum, gene, filterFactorys);
final double[] minmax = AddedGene.minmax(data);
minDisplay = Math.min(minDisplay, AddedGene.getDisplayMin(minmax[0], minmax[1], brightnessMin));
maxDisplay = Math.max(maxDisplay, AddedGene.getDisplayMax(minmax[1], brightnessMax));
slices.add(data);
}
// we need to re-compute the statistics because the transformation might have changed it
final double medianDistance = new STDataStatistics( stdata.get( 0 ).data() ).getMedianDistance();
final double spacing = medianDistance * spacingFactor;
final Interval interval2d = STDataUtils.getCommonIterableInterval( slices );
final long padding = Math.round(Math.ceil(medianDistance * 3));
final long[] minI = new long[] { interval2d.min(0 ), interval2d.min(1 ), -padding};
final long[] maxI = new long[] { interval2d.max( 0 ), interval2d.max( 1 ), Math.round( ( stdata.size() - 1 ) * spacing ) + padding};
final Interval interval = new FinalInterval( minI, maxI );
final StackedIterableRealInterval< DoubleType > stack = new StackedIterableRealInterval<>( slices, spacing );
final STIMStack stimStack = new STIMStack();
stimStack.rra = RenderImage.createRRA( stack, medianDistance, renderType, renderingFactor );
stimStack.interval = interval;
stimStack.minDisplay = minDisplay;
stimStack.maxDisplay = maxDisplay;
return stimStack;
}
public static void setupRecordMovie( final BdvStackSource<?> bdvSource, final CallbackBDV callback )
{
final ActionMap ksActionMap = new ActionMap();
final InputMap ksInputMap = new InputMap();
// default input trigger config, disables "control button1" drag in bdv
// (collides with default of "move annotation")
final InputTriggerConfig config = new InputTriggerConfig(
Collections.singletonList(
new InputTriggerDescription(
new String[]{"not mapped"}, "drag rotate slow", "bdv")));
final KeyStrokeAdder ksKeyStrokeAdder = config.keyStrokeAdder(ksInputMap, "persistence");
new AbstractNamedAction( "Record movie" )
{
private static final long serialVersionUID = 3640052275162419689L;
@Override
public void actionPerformed(ActionEvent e)
{
new Thread( ()-> BDVFlyThrough.record( bdvSource, callback ) ).start();
}
public void register() {
put(ksActionMap);
ksKeyStrokeAdder.put(name(), "ctrl R" );
}
}.register();
new AbstractNamedAction( "Add Current Viewer Transform" )
{
private static final long serialVersionUID = 3620052275162419689L;
@Override
public void actionPerformed(ActionEvent e)
{
BDVFlyThrough.addCurrentViewerTransform( bdvSource.getBdvHandle().getViewerPanel() );
}
public void register() {
put(ksActionMap);
ksKeyStrokeAdder.put(name(), "ctrl A" );
}
}.register();
new AbstractNamedAction( "Clear All Viewer Transforms" )
{
private static final long serialVersionUID = 3620052275162419689L;
@Override
public void actionPerformed(ActionEvent e)
{
BDVFlyThrough.clearAllViewerTransform();
}
public void register() {
put(ksActionMap);
ksKeyStrokeAdder.put(name(), "ctrl X" );
}
}.register();
bdvSource.getBdvHandle().getKeybindings().addActionMap("persistence", ksActionMap);
bdvSource.getBdvHandle().getKeybindings().addInputMap("persistence", ksInputMap);
}
public static ImagePlus visualizeIJ(final List<STDataAssembly> puckData, final boolean useTransform )
{
final List< STData > data = new ArrayList<>();
final List< AffineTransform2D > transforms = new ArrayList<>();
for ( final STDataAssembly stda : puckData )
{
data.add( stda.data() );
transforms.add( useTransform ? stda.transform() : new AffineTransform2D() );
}
return AlignTools.visualizeList( data, transforms, AlignTools.defaultScale, Rendering.Gauss, AlignTools.defaultSmoothnessFactor, AlignTools.defaultGene, true );
}
public static void main( String[] args ) throws IOException
{
final ExecutorService service = Executors.newFixedThreadPool(8);
final List<STDataAssembly> puckData =
SpatialDataContainer.openForReading(Path.getPath() + "slide-seq-test.n5", service).openAllDatasets().stream()
.map(sdio ->
{try {return sdio.readData();} catch (IOException e) {throw new RuntimeException(e);}})
.collect(Collectors.toList());
new ImageJ();
// Display as full 3D ImageJ image
RandomAccessibleInterval<DoubleType> img = render3d_IJ( puckData, "Ptgds", 0.4 );
ImageJFunctions.show( img, Executors.newFixedThreadPool( 8 ) ).setTitle("Ptgds");
// Display as 2D slices (each slice a 2D image)
AlignTools.defaultGene = "Ptgds";//"Calm2";//"Mbp";//;
AlignTools.defaultScale = 0.1;
AlignTools.defaultSmoothnessFactor = 2.0;
ImagePlus imp = visualizeIJ( puckData, true );
imp.show();
// Display interactively with BDV
render2d( puckData.get( 0 ) );
// Display 3D interactively with BDV
BdvStackSource< ? > bdv = render3d( puckData );
service.shutdown();
}
}