-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathSmartSPIM.java
More file actions
509 lines (407 loc) · 17.1 KB
/
Copy pathSmartSPIM.java
File metadata and controls
509 lines (407 loc) · 17.1 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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*-
* #%L
* Software for the reconstruction of multi-view microscopic acquisitions
* like Selective Plane Illumination Microscopy (SPIM) Data.
* %%
* Copyright (C) 2012 - 2025 Multiview Reconstruction developers.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package net.preibisch.mvrecon.fiji.datasetmanager;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import fiji.util.gui.GenericDialogPlus;
import ij.ImagePlus;
import mpicbg.spim.data.SpimDataIOException;
import mpicbg.spim.data.registration.ViewRegistrations;
import mpicbg.spim.data.sequence.Angle;
import mpicbg.spim.data.sequence.Channel;
import mpicbg.spim.data.sequence.FinalVoxelDimensions;
import mpicbg.spim.data.sequence.Illumination;
import mpicbg.spim.data.sequence.ImgLoader;
import mpicbg.spim.data.sequence.MissingViews;
import mpicbg.spim.data.sequence.SequenceDescription;
import mpicbg.spim.data.sequence.Tile;
import mpicbg.spim.data.sequence.TimePoint;
import mpicbg.spim.data.sequence.TimePoints;
import mpicbg.spim.data.sequence.ViewSetup;
import mpicbg.spim.data.sequence.VoxelDimensions;
import net.imglib2.Dimensions;
import net.imglib2.FinalDimensions;
import net.imglib2.util.Pair;
import net.imglib2.util.Util;
import net.imglib2.util.ValuePair;
import net.preibisch.legacy.io.IOFunctions;
import net.preibisch.mvrecon.fiji.plugin.util.GUIHelper;
import net.preibisch.mvrecon.fiji.spimdata.SpimData2;
import net.preibisch.mvrecon.fiji.spimdata.boundingbox.BoundingBoxes;
import net.preibisch.mvrecon.fiji.spimdata.imgloaders.SmartSPIMImgLoader;
import net.preibisch.mvrecon.fiji.spimdata.intensityadjust.IntensityAdjustments;
import net.preibisch.mvrecon.fiji.spimdata.interestpoints.ViewInterestPoints;
import net.preibisch.mvrecon.fiji.spimdata.pointspreadfunctions.PointSpreadFunctions;
import net.preibisch.mvrecon.fiji.spimdata.stitchingresults.StitchingResults;
import util.URITools;
public class SmartSPIM implements MultiViewDatasetDefinition
{
public static String defaultMetadataFile = "";
public static boolean defaultConfirmFiles = true;
@Override
public String getTitle()
{
return "SmartSPIM Datasets";
}
@Override
public String getExtendedDescription()
{
return "This datset definition supports folder structures saved by SmartSPIM's.";
}
@Override
public SpimData2 createDataset( final String xmlFileName )
{
final Pair< URI, Boolean > md = queryMetaDataFile();
if ( md == null )
return null;
final SmartSPIMMetaData metadata = parseMetaDataFile( md.getA() );
if ( metadata == null )
return null;
if ( !populateImageSize( metadata, md.getB() ) )
return null;
// assemble timepints, viewsetups, missingviews and the imgloader
final TimePoints timepoints = this.createTimePoints( metadata );
final ArrayList< ViewSetup > setups = this.createViewSetups( metadata, 1.0/10.0 );
final MissingViews missingViews = new MissingViews( new ArrayList<>() );
// instantiate the sequencedescription
final SequenceDescription sequenceDescription = new SequenceDescription( timepoints, setups, null, missingViews );
final ImgLoader imgLoader = new SmartSPIMImgLoader( metadata, sequenceDescription );
sequenceDescription.setImgLoader( imgLoader );
// get the minimal resolution of all calibrations
final double minResolution = Math.min( metadata.xyRes, metadata.zRes );
IOFunctions.println( "Minimal resolution in all dimensions is: " + minResolution );
IOFunctions.println( "(The smallest resolution in any dimension; the distance between two pixels in the output image will be that wide)" );
// create calibration + translation view registrations
final ViewRegistrations viewRegistrations =
DatasetCreationUtils.createViewRegistrations( sequenceDescription.getViewDescriptions(), minResolution );
// create the initial view interest point object
final ViewInterestPoints viewInterestPoints = new ViewInterestPoints();
// finally create the SpimData itself based on the sequence description and the view registration
final SpimData2 spimData = new SpimData2( metadata.dir, sequenceDescription, viewRegistrations, viewInterestPoints, new BoundingBoxes(), new PointSpreadFunctions(), new StitchingResults(), new IntensityAdjustments() );
return spimData;
}
/*
* Creates the {@link TimePoints} for the {@link SpimData} object
*/
protected TimePoints createTimePoints( final SmartSPIMMetaData meta )
{
final ArrayList< TimePoint > timepoints = new ArrayList< TimePoint >();
timepoints.add( new TimePoint( 0 ) );
return new TimePoints( timepoints );
}
/*
*
* @param meta the metadata object
* @param scaleFactor somehow the foldernames are not in um but 1/10 of um, so we need to scale by 1/10 to get approx positions
* @return
*/
protected ArrayList< ViewSetup > createViewSetups( final SmartSPIMMetaData meta, final double scaleFactor )
{
final ArrayList< Channel > channels = new ArrayList< Channel >();
for ( int c = 0; c < meta.channels.size(); ++c )
channels.add( new Channel( c, Integer.toString( meta.channels.get( c ).getA() ) ) );
final ArrayList< Tile > tiles = new ArrayList<>();
int i = 0;
for ( int y = 0; y < meta.yTileLocations.size(); ++y )
for ( int x = 0; x < meta.xTileLocations.size(); ++x )
{
final Tile tile = new Tile( i, "x" + x + "_y" + y );
tile.setLocation( new double[] { meta.xTileLocations.get( x ) * scaleFactor, meta.yTileLocations.get( y ) * scaleFactor, 0 } );
tiles.add( tile );
++i;
}
final ArrayList< ViewSetup > viewSetups = new ArrayList< ViewSetup >();
for ( final Channel c : channels )
for ( final Tile t : tiles )
{
final VoxelDimensions voxelSize = new FinalVoxelDimensions( "micrometer", meta.xyRes, meta.xyRes, meta.zRes );
final Dimensions dim = new FinalDimensions( meta.dimensions );
viewSetups.add( new ViewSetup( viewSetups.size(), null, dim, voxelSize, t, c, new Angle( 0 ), new Illumination( 0 ) ) );
}
return viewSetups;
}
public static boolean populateImageSize( final SmartSPIMMetaData metadata, final boolean confirmAllImages )
{
metadata.dimensions = null;
metadata.sortedFileNames = null;
for ( int channel = 0; channel < metadata.channels.size(); ++ channel )
for ( int xTile = 0; xTile < metadata.xTileLocations.size(); ++ xTile)
for ( int yTile = 0; yTile < metadata.yTileLocations.size(); ++ yTile)
{
final URI imageDir = metadata.folderFor(
metadata.channels.get( channel ),
metadata.xTileLocations.get( xTile ),
metadata.yTileLocations.get( yTile ) );
IOFunctions.println( "Directory: " + imageDir );
final Pair< long[], List< String > > stackData =
metadata.loadImageSize( channel, xTile, yTile );
final long[] dimensions = stackData.getA();
IOFunctions.println( "dimensions: " + Util.printCoordinates( dimensions ) );
if ( dimensions == null )
return false;
if ( metadata.dimensions == null )
{
metadata.dimensions = dimensions;
metadata.sortedFileNames = stackData.getB();
}
if ( !confirmAllImages )
return true;
if ( !Arrays.equals( metadata.dimensions, dimensions ) )
{
IOFunctions.println( "dimensions are not equal. Stopping: " + Util.printCoordinates( dimensions ) + ", " + Util.printCoordinates( metadata.dimensions ) );
return false;
}
if ( !areListsEqual( metadata.sortedFileNames, stackData.getB() ) )
{
IOFunctions.println( "file names are not equal. Stopping." );
return false;
}
}
return true;
}
public static SmartSPIMMetaData parseMetaDataFile( final URI md )
{
IOFunctions.println( "Parsing: " + md + " ... ");
final SmartSPIMMetaData metadata;
try
{
metadata = new SmartSPIMMetaData( md );
}
catch (SpimDataIOException e)
{
IOFunctions.println( "Cannot extract directory for '" + md + "': " + e );
e.printStackTrace();
return null;
}
if ( !URITools.isFile( md ) )
{
IOFunctions.println( "So far only local file systems are supported.");
return null;
}
final File file = new File( md );
if ( !file.exists() )
{
IOFunctions.println( "Error: " + file + " does not exist.");
return null;
}
// Open the file using a GsonReader
try (JsonReader reader = new JsonReader( new FileReader( file ) ) )
{
final Gson gson = new Gson();
final JsonElement e = gson.fromJson(reader, (JsonElement.class ));
final JsonElement session_config = e.getAsJsonObject().get("session_config");
final JsonElement tile_config = e.getAsJsonObject().get("tile_config");
// we cannot directly de-serialize the session_config because it contains special characters in the field names
final Map<String, JsonElement> sessionMap = session_config.getAsJsonObject().asMap();
for ( final Entry< String, JsonElement > entry : sessionMap.entrySet() )
{
if ( entry.getKey().contains( "Z step" ) )
metadata.zRes = Double.parseDouble( entry.getValue().getAsString() );
if ( entry.getKey().contains( "m/pix" ) )
metadata.xyRes = Double.parseDouble( entry.getValue().getAsString() );
}
IOFunctions.println( "resolution: " + metadata.xyRes + " x " + metadata.xyRes + " x " + metadata.zRes + " um/px." );
final Type typeTileConfig = new TypeToken<HashMap<String, SmartSPIM_Tile>>() {}.getType();
HashMap<String, SmartSPIM_Tile> tiles = gson.fromJson(tile_config, typeTileConfig);
IOFunctions.println( "number of tiles: " + tiles.size() );
metadata.channels = SmartSPIM_Tile.channels( tiles.values() );
metadata.xTileLocations = SmartSPIM_Tile.xTileLocations( tiles.values() );
metadata.yTileLocations = SmartSPIM_Tile.yTileLocations( tiles.values() );
metadata.zOffsets = SmartSPIM_Tile.zOffsets( tiles.values() );
IOFunctions.println( "channels: " );
metadata.channels.forEach( p -> IOFunctions.println( "\t" + SmartSPIM_Tile.channelToFolderName( p ) ) );
IOFunctions.println( "x tile locations: " );
metadata.xTileLocations.forEach( x -> IOFunctions.println( "\t" + x ) );
IOFunctions.println( "y tile locations: " );
metadata.yTileLocations.forEach( y -> IOFunctions.println( "\t" + y ) );
IOFunctions.println( "z offset(s): " );
metadata.zOffsets.forEach( y -> IOFunctions.println( "\t" + y ) );
if ( metadata.zOffsets.size() != 1 )
{
IOFunctions.println( "multiple z offsets not supported yet. please contact developers." );
return null;
}
}
catch (IOException e)
{
IOFunctions.println("Error reading the file: " + e.getMessage());
return null;
}
return metadata;
}
protected Pair< URI, Boolean > queryMetaDataFile()
{
GenericDialogPlus gd = new GenericDialogPlus( "Define SmartSPIM Dataset" );
gd.addFileField( "SmartSPIM_metadata.json file", defaultMetadataFile, 50 );
gd.addCheckbox( "Confirm_presence of all folders & files", defaultConfirmFiles );
gd.addMessage( "Note: for now we assume the same dimensions of all tiles & channels", GUIHelper.smallStatusFont, GUIHelper.neutral );
gd.showDialog();
if ( gd.wasCanceled() )
return null;
final URI metaDataFile = URITools.toURI( defaultMetadataFile = gd.getNextString() );
final boolean confirmFiles = defaultConfirmFiles = gd.getNextBoolean();
return new ValuePair<>( metaDataFile, confirmFiles );
}
@Override
public MultiViewDatasetDefinition newInstance()
{
return new SmartSPIM();
}
public static boolean areListsEqual( final List< String > array1, final List< String > array2 )
{
if (array1 == null || array2 == null)
return false;
if (array1.size() != array2.size() )
return false;
for ( int i = 0; i < array1.size(); ++i )
if ( !array1.get( i ).equals(array2.get( i )) )
return false;
return true;
}
public static class SmartSPIMMetaData
{
final public URI metadataFile, dir;
public long[] dimensions;
public List< String > sortedFileNames;
public double xyRes = -1;
public double zRes = -1;
public List<Pair<Integer, Integer>> channels;
public List<Long> xTileLocations;
public List<Long> yTileLocations;
public List<Long> zOffsets;
public SmartSPIMMetaData( final URI metadataFile ) throws SpimDataIOException
{
this.metadataFile = metadataFile;
this.dir = URITools.getParentURI( this.metadataFile );
}
public URI folderFor( final Pair<Integer, Integer> channel, final long xTile, final long yTile )
{
return folderFor( dir, channel, xTile, yTile );
}
public List< String > sortedSlicesFor( final Pair<Integer, Integer> channel, final long xTile, final long yTile )
{
final URI dir = folderFor( channel, xTile, yTile );
return sortedSlicesFor( dir );
}
public ImagePlus loadImage( final Pair<Integer, Integer> channel, final long xTile, final long yTile, final String fileName )
{
return loadImage( dir, channel, xTile, yTile, fileName );
}
protected Pair< long[], List< String > > loadImageSize( final int channel, final int xTile, final int yTile )
{
return loadImageSize( this, channel, xTile, yTile );
}
public List< String > sortedSlicesFor( final URI dir )
{
return Arrays
.asList(new File(dir).list((directory, name) -> name.toLowerCase().matches("\\d+\\.tif{1,2}")))
.stream().sorted()
.collect(Collectors.toList());
}
protected static Pair< long[], List< String > > loadImageSize( final SmartSPIMMetaData metadata, final int channel, final int xTile, final int yTile )
{
final URI imageDir = metadata.folderFor(
metadata.channels.get( channel ),
metadata.xTileLocations.get( xTile ),
metadata.yTileLocations.get( yTile ) );
final List<String> files = metadata.sortedSlicesFor( imageDir );
final ImagePlus imp = SmartSPIMMetaData.loadImage( imageDir, files.get( 0 ) );
if ( imp.getProcessor() == null )
{
IOFunctions.println( "Failed to load image. Stopping." );
return null;
}
final long[] dim = new long[] { imp.getWidth(), imp.getHeight(), files.size() };
imp.close();
return new ValuePair<>( dim, files );
}
public static URI folderFor( final URI dir, final Pair<Integer, Integer> channel, final long xTile, final long yTile )
{
return dir.resolve( SmartSPIM_Tile.channelToFolderName( channel ) + "/" + xTile + "/" + xTile + "_" + yTile + "/" );
}
public static ImagePlus loadImage( final URI dir, final Pair<Integer, Integer> channel, final long xTile, final long yTile, final String fileName )
{
final URI imageDir = SmartSPIMMetaData.folderFor( dir, channel, xTile, yTile );
return loadImage( imageDir, fileName );
}
public static ImagePlus loadImage( final URI imageDir, final String fileName )
{
final File file = new File( imageDir.resolve( fileName ) );
final ImagePlus imp = new ImagePlus( file.getAbsolutePath() );
return imp;
}
}
private static class SmartSPIM_Tile
{
public long X;
public long Y;
public long Z;
public int Laser;
public int Side;
public int Exposure;
public int Skip;
public int Filter;
public static List< Long > xTileLocations( final Collection< SmartSPIM_Tile > tiles )
{
return tiles.stream().map( t -> t.X ).distinct().sorted().collect(Collectors.toList());
}
public static List< Long > yTileLocations( final Collection< SmartSPIM_Tile > tiles )
{
return tiles.stream().map( t -> t.Y ).distinct().sorted().collect(Collectors.toList());
}
public static List< Long > zOffsets( final Collection< SmartSPIM_Tile > tiles )
{
return tiles.stream().map( t -> t.Z ).distinct().sorted().collect(Collectors.toList());
}
public static List< Pair< Integer, Integer > > channels( final Collection< SmartSPIM_Tile > tiles )
{
return tiles.stream().map( t -> new ValuePair<>( t.Laser, t.Filter ) ).distinct().sorted( (o1,o2) -> o1.a.compareTo( o2.a ) ).collect(Collectors.toList());
}
public static String channelToFolderName( final Pair< Integer, Integer > channel )
{
return "Ex_" + channel.getA() + "_Ch" + channel.getB();
}
}
public static void main( String[] args )
{
//parseMetaDataFile( URITools.toURI("/Users/preibischs/Documents/Janelia/Projects/BigStitcher/SmartSPIM/metadata.json"));
SmartSPIMMetaData metadata =
parseMetaDataFile( URITools.toURI( "/Volumes/johnsonlab/LM/20241031_11_59_44_RJ_mouse_2_vDisco_hindleg_right_Destripe_DONE/metadata.json") );
//populateImageSize( metadata, true );
}
}