diff --git a/src/main/java/net/preibisch/mvrecon/fiji/datasetmanager/FileListDatasetDefinition.java b/src/main/java/net/preibisch/mvrecon/fiji/datasetmanager/FileListDatasetDefinition.java index b309aaa0..371aa800 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/datasetmanager/FileListDatasetDefinition.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/datasetmanager/FileListDatasetDefinition.java @@ -1290,12 +1290,10 @@ public int compare(Group< ViewDescription > o1, Group< ViewDescription > o2) IOFunctions.println( "(" + new Date( System.currentTimeMillis() ) + "): HDF5 resave finished." ); - net.preibisch.mvrecon.fiji.ImgLib2Temp.Pair< SpimData2, List< String > > result = Resave_HDF5.createXMLObject( data, new ArrayList<>(data.getSequenceDescription().getViewDescriptions().keySet()), params, progressWriter, true ); + data = Resave_HDF5.createXMLObject( data, new ArrayList<>(data.getSequenceDescription().getViewDescriptions().keySet()), params, progressWriter, true ); // ensure progressbar is gone progressWriter.setProgress( 1.0 ); - - data = result.getA(); } else if (resaveAsN5) { diff --git a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_HDF5.java b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_HDF5.java index 563a9830..bf15fbd3 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_HDF5.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_HDF5.java @@ -22,15 +22,12 @@ */ package net.preibisch.mvrecon.fiji.plugin.resave; -import java.text.ParseException; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; import bdv.export.ExportMipmapInfo; import bdv.export.ProgressWriter; @@ -40,26 +37,17 @@ import ij.plugin.PlugIn; import mpicbg.spim.data.SpimDataException; import mpicbg.spim.data.generic.sequence.BasicViewSetup; -import mpicbg.spim.data.registration.ViewRegistration; -import mpicbg.spim.data.registration.ViewRegistrations; -import mpicbg.spim.data.sequence.MissingViews; -import mpicbg.spim.data.sequence.SequenceDescription; import mpicbg.spim.data.sequence.TimePoint; -import mpicbg.spim.data.sequence.TimePoints; -import mpicbg.spim.data.sequence.TimePointsPattern; import mpicbg.spim.data.sequence.ViewDescription; import mpicbg.spim.data.sequence.ViewId; import mpicbg.spim.data.sequence.ViewSetup; import net.imglib2.Dimensions; import net.preibisch.legacy.io.IOFunctions; -import net.preibisch.mvrecon.fiji.ImgLib2Temp.Pair; -import net.preibisch.mvrecon.fiji.ImgLib2Temp.ValuePair; import net.preibisch.mvrecon.fiji.plugin.queryXML.LoadParseQueryXML; import net.preibisch.mvrecon.fiji.plugin.resave.Generic_Resave_HDF5.ParametersResaveHDF5; import net.preibisch.mvrecon.fiji.spimdata.SpimData2; import net.preibisch.mvrecon.fiji.spimdata.XmlIoSpimData2; -import net.preibisch.mvrecon.fiji.spimdata.interestpoints.ViewInterestPointLists; -import net.preibisch.mvrecon.fiji.spimdata.interestpoints.ViewInterestPoints; +import net.preibisch.mvrecon.process.resave.SpimData2Tools; public class Resave_HDF5 implements PlugIn { @@ -102,20 +90,17 @@ public void run( final String arg0 ) final List< ViewId > viewIds = SpimData2.getAllViewIdsSorted( data, xml.getViewSetupsToProcess(), xml.getTimePointsToProcess() ); // write hdf5 - Generic_Resave_HDF5.writeHDF5( reduceSpimData2( data, viewIds ), params, progressWriter ); + Generic_Resave_HDF5.writeHDF5( SpimData2Tools.reduceSpimData2( data, viewIds ), params, progressWriter ); // write xml sequence description if ( !params.onlyRunSingleJob || params.jobId == 0 ) { try { - final Pair< SpimData2, List< String > > result = createXMLObject( data, viewIds, params, progressWriter, false ); + final SpimData2 newSpimData = createXMLObject( data, viewIds, params, progressWriter, false ); - xml.getIO().save( result.getA(), params.seqFile.getAbsolutePath() ); + xml.getIO().save( newSpimData, params.seqFile.getAbsolutePath() ); progressWriter.setProgress( 0.95 ); - - // copy the interest points if they exist - Resave_TIFF.copyInterestPoints( xml.getData().getBasePath(), params.getSeqFile().getParentFile(), result.getB() ); } catch ( SpimDataException e ) { @@ -192,103 +177,33 @@ public static boolean loadDimensions( final SpimData2 spimData, final List< View return loadedDimensions; } - /** - * Reduces a given SpimData2 to the subset of timepoints and viewsetups as selected by the user, including the original imgloader. - * - * @param oldSpimData - the original SpimData - * @param viewIds - the views to keep - * @return - reduced SpimData2 - */ - public static SpimData2 reduceSpimData2( final SpimData2 oldSpimData, final List< ViewId > viewIds ) + + public static SpimData2 createXMLObject( + final SpimData2 spimData, + final List< ViewId > viewIds, + final ParametersResaveHDF5 params, + final ProgressWriter progressWriter, + final boolean useRightAway ) { - final TimePoints timepoints; + // Re-assemble a new SpimData object containing the subset of viewsetups and timepoints selected + final SpimData2 newSpimData; + + boolean isEqual = false; try { - timepoints = new TimePointsPattern( Resave_TIFF.listAllTimePoints( SpimData2.getAllTimePointsSorted( oldSpimData, viewIds ) ) ); + isEqual = spimData.getBasePathURI().equals( params.seqFile.getParentFile().toURI() ) || params.seqFile.getParent().equals( spimData.getBasePath().getName() ); } - catch (ParseException e) + catch ( Exception e ) { - IOFunctions.println( "Automatically created list of timepoints failed to parse. This should not happen, really :) -- " + e ); - IOFunctions.println( "Here is the list: " + Resave_TIFF.listAllTimePoints( SpimData2.getAllTimePointsSorted( oldSpimData, viewIds ) ) ); - e.printStackTrace(); - return null; + isEqual = false; } - final List< ViewSetup > viewSetupsToProcess = SpimData2.getAllViewSetupsSorted( oldSpimData, viewIds ); - - // a hashset for all viewsetups that remain - final Set< ViewId > views = new HashSet< ViewId >(); - - for ( final ViewId viewId : viewIds ) - views.add( new ViewId( viewId.getTimePointId(), viewId.getViewSetupId() ) ); - - final MissingViews oldMissingViews = oldSpimData.getSequenceDescription().getMissingViews(); - final HashSet< ViewId > missingViews = new HashSet< ViewId >(); - - if( oldMissingViews != null && oldMissingViews.getMissingViews() != null ) - for ( final ViewId id : oldMissingViews.getMissingViews() ) - if ( views.contains( id ) ) - missingViews.add( id ); - - // add the new missing views!!! - for ( final TimePoint t : timepoints.getTimePointsOrdered() ) - for ( final ViewSetup v : viewSetupsToProcess ) - { - final ViewId viewId = new ViewId( t.getId(), v.getId() ); - - if ( !views.contains( viewId ) ) - missingViews.add( viewId ); - } - - // instantiate the sequencedescription - final SequenceDescription sequenceDescription = new SequenceDescription( timepoints, viewSetupsToProcess, oldSpimData.getSequenceDescription().getImgLoader(), new MissingViews( missingViews ) ); - - // re-assemble the registrations - final Map< ViewId, ViewRegistration > oldRegMap = oldSpimData.getViewRegistrations().getViewRegistrations(); - final Map< ViewId, ViewRegistration > newRegMap = new HashMap< ViewId, ViewRegistration >(); - - for ( final ViewId viewId : oldRegMap.keySet() ) - if ( views.contains( viewId ) ) - newRegMap.put( viewId, oldRegMap.get( viewId ) ); - - final ViewRegistrations viewRegistrations = new ViewRegistrations( newRegMap ); - - // re-assemble the interestpoints and a list of filenames to copy - final Map< ViewId, ViewInterestPointLists > oldInterestPoints = oldSpimData.getViewInterestPoints().getViewInterestPoints(); - final Map< ViewId, ViewInterestPointLists > newInterestPoints = new HashMap< ViewId, ViewInterestPointLists >(); - - for ( final ViewId viewId : oldInterestPoints.keySet() ) - if ( views.contains( viewId ) ) - newInterestPoints.put( viewId, oldInterestPoints.get( viewId ) ); - - final ViewInterestPoints viewsInterestPoints = new ViewInterestPoints( newInterestPoints ); - - //TODO: copy PSFs - - final SpimData2 newSpimData = new SpimData2( - oldSpimData.getBasePathURI(), - sequenceDescription, - viewRegistrations, - viewsInterestPoints, - oldSpimData.getBoundingBoxes(), - oldSpimData.getPointSpreadFunctions(), - oldSpimData.getStitchingResults(), - oldSpimData.getIntensityAdjustments() ); - - return newSpimData; - } + if ( isEqual ) + newSpimData = SpimData2Tools.reduceSpimData2( spimData, viewIds ); + else + newSpimData = SpimData2Tools.reduceSpimData2( spimData, viewIds, params.seqFile.getParentFile().toURI() ); - public static Pair< SpimData2, List< String > > createXMLObject( - final SpimData2 spimData, - final List< ViewId > viewIds, - final ParametersResaveHDF5 params, - final ProgressWriter progressWriter, - final boolean useRightAway ) - { - // Re-assemble a new SpimData object containing the subset of viewsetups and timepoints selected - final List< String > filesToCopy = new ArrayList< String >(); - final SpimData2 newSpimData = Resave_TIFF.assemblePartialSpimData2( spimData, viewIds, params.seqFile.getParentFile().toURI(), filesToCopy ); final ArrayList< Partition > partitions = Generic_Resave_HDF5.getPartitions( newSpimData, params ); final Hdf5ImageLoader hdf5Loader; @@ -301,6 +216,6 @@ public static Pair< SpimData2, List< String > > createXMLObject( newSpimData.getSequenceDescription().setImgLoader( hdf5Loader ); newSpimData.setBasePath( params.seqFile.getParentFile() ); - return new ValuePair< SpimData2, List< String > >( newSpimData, filesToCopy ); + return newSpimData; } } diff --git a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java index f0e277dd..dfb17577 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_N5.java @@ -52,6 +52,7 @@ import net.preibisch.mvrecon.fiji.spimdata.SpimData2; import net.preibisch.mvrecon.fiji.spimdata.XmlIoSpimData2; import net.preibisch.mvrecon.process.resave.N5ResaveTools; +import net.preibisch.mvrecon.process.resave.SpimData2Tools; import util.URITools; public class Resave_N5 implements PlugIn @@ -84,7 +85,7 @@ public static SpimData2 resaveN5( final ParametersResaveN5 n5Params, final boolean saveXML ) { - final SpimData2 sdReduced = Resave_HDF5.reduceSpimData2( data, vidsToResave.stream().collect( Collectors.toList() ) ); + final SpimData2 sdReduced = SpimData2Tools.reduceSpimData2( data, vidsToResave.stream().collect( Collectors.toList() ) ); final ProgressWriter progressWriter = new ProgressWriterIJ(); progressWriter.out().println( new Date( System.currentTimeMillis() ) + ": Saving " + n5Params.n5URI ); diff --git a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_TIFF.java b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_TIFF.java index b48a41d7..af2c2dda 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_TIFF.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_TIFF.java @@ -25,46 +25,25 @@ import static mpicbg.spim.data.generic.sequence.ImgLoaderHints.LOAD_COMPLETELY; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; -import java.text.ParseException; -import java.util.ArrayList; import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; -import java.util.Map; -import java.util.Set; import bdv.export.ProgressWriter; import fiji.util.gui.GenericDialogPlus; import ij.plugin.PlugIn; import mpicbg.spim.data.SpimData; import mpicbg.spim.data.SpimDataException; -import mpicbg.spim.data.registration.ViewRegistration; -import mpicbg.spim.data.registration.ViewRegistrations; -import mpicbg.spim.data.sequence.MissingViews; -import mpicbg.spim.data.sequence.SequenceDescription; -import mpicbg.spim.data.sequence.TimePoint; -import mpicbg.spim.data.sequence.TimePoints; -import mpicbg.spim.data.sequence.TimePointsPattern; import mpicbg.spim.data.sequence.ViewDescription; import mpicbg.spim.data.sequence.ViewId; -import mpicbg.spim.data.sequence.ViewSetup; import net.imglib2.RandomAccessibleInterval; import net.preibisch.legacy.io.IOFunctions; -import net.preibisch.mvrecon.fiji.ImgLib2Temp.Pair; -import net.preibisch.mvrecon.fiji.ImgLib2Temp.ValuePair; import net.preibisch.mvrecon.fiji.plugin.queryXML.LoadParseQueryXML; import net.preibisch.mvrecon.fiji.spimdata.SpimData2; import net.preibisch.mvrecon.fiji.spimdata.imgloaders.StackImgLoaderIJ; -import net.preibisch.mvrecon.fiji.spimdata.interestpoints.ViewInterestPoints; import net.preibisch.mvrecon.process.export.Save3dTIFF; +import net.preibisch.mvrecon.process.resave.SpimData2Tools; import util.URITools; public class Resave_TIFF implements PlugIn @@ -113,14 +92,11 @@ public void run( final String arg0 ) // write the XML try { - final Pair< SpimData2, List< String > > result = createXMLObject( data, viewIds, params ); + final SpimData2 newSpimData = createXMLObject( data, viewIds, params ); progressWriter.setProgress( 0.95 ); // write the XML - lpq.getIO().save( result.getA(), file.getAbsolutePath() ); - - // copy the interest points if they exist - copyInterestPoints( data.getBasePath(), file.getParentFile(), result.getB() ); + lpq.getIO().save( newSpimData, file.getAbsolutePath() ); } catch ( SpimDataException e ) { @@ -134,44 +110,6 @@ public void run( final String arg0 ) } } - public static void copyInterestPoints( final File srcBase, final File destBase, final List< String > filesToCopy ) - { - // test if source and target directory are identical, if so stop - String from = srcBase.getAbsolutePath(); - String to = destBase.getAbsolutePath(); - - from = from.replace( "/./", "/" ); - to = to.replace( "/./", "/" ); - - if ( from.endsWith( "/." ) ) - from = from.substring( 0, from.length() - 2 ); - - if ( to.endsWith( "/." ) ) - to = to.substring( 0, to.length() - 2 ); - - if ( new File( from ).getAbsolutePath().equals( new File( to ).getAbsolutePath() ) ) - return; - - final File src = new File( srcBase, "interestpoints" ); - - if ( src.exists() ) - { - final File target = new File( destBase, "interestpoints" ); - - IOFunctions.println( new Date( System.currentTimeMillis() ) + ": Interestpoint directory exists. Copying '" + src + "' >>> '" + target + "'" ); - - try - { - copyFolder( src, target, filesToCopy ); - } - catch (IOException e) - { - IOFunctions.println( new Date( System.currentTimeMillis() ) + ": FAILED to copying '" + src + "' >>> '" + target + "': " + e ); - e.printStackTrace(); - } - } - } - /** * @return - the parameters if it is specifying a locally mounted share, otherwise null */ @@ -276,7 +214,7 @@ public static void writeTIFF( final SpimData spimData, final List< ViewId > view } - public static Pair< SpimData2, List< String > > createXMLObject( final SpimData2 spimData, final List< ViewId > viewIds, final ParametersResaveAsTIFF params ) + public static SpimData2 createXMLObject( final SpimData2 spimData, final List< ViewId > viewIds, final ParametersResaveAsTIFF params ) { int layoutTP = 0, layoutChannels = 0, layoutIllum = 0, layoutAngles = 0, layoutTiles = 0; String filename = "img"; @@ -323,164 +261,30 @@ public static Pair< SpimData2, List< String > > createXMLObject( final SpimData2 filename += ".zip"; // Re-assemble a new SpimData object containing the subset of viewsetups and timepoints selected - final List< String > filesToCopy = new ArrayList< String >(); - final SpimData2 newSpimData = assemblePartialSpimData2( spimData, viewIds, new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParentFile().toURI(), filesToCopy ); - - final StackImgLoaderIJ imgLoader = new StackImgLoaderIJ( - new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParentFile(), - filename, - layoutTP, layoutChannels, layoutIllum, layoutAngles, layoutTiles, newSpimData.getSequenceDescription() ); - newSpimData.getSequenceDescription().setImgLoader( imgLoader ); - - return new ValuePair< SpimData2, List< String > >( newSpimData, filesToCopy ); - } - - public static String listAllTimePoints( final List timePointsToProcess ) - { - String t = "" + timePointsToProcess.get( 0 ).getId(); - - for ( int i = 1; i < timePointsToProcess.size(); ++i ) - t += ", " + timePointsToProcess.get( i ).getId(); - - return t; - } - - public static void copyFolder( final File src, final File dest, final List< String > filesToCopy ) throws IOException - { - if ( src.isDirectory() ) - { - if( !dest.exists() ) - dest.mkdir(); - - for ( final String file : src.list() ) - copyFolder( new File( src, file ), new File( dest, file ), filesToCopy ); - } - else - { - boolean contains = false; - - for ( int i = 0; i < filesToCopy.size() && !contains; ++i ) - if ( src.getName().contains( filesToCopy.get( i ) ) ) - contains = true; - - if ( contains ) - { - final InputStream in = new FileInputStream( src ); - final OutputStream out = new FileOutputStream( dest ); + final SpimData2 newSpimData; + final URI newBasePath = new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParentFile().toURI(); - final byte[] buffer = new byte[ 65535 ]; - - int length; - - while ( ( length = in.read(buffer) ) > 0 ) - out.write(buffer, 0, length); - - in.close(); - out.close(); - } - } - } - - /** - * Assembles a new SpimData2 based on the subset of timepoints and viewsetups as selected by the user. - * The imgloader is still not set here. - * - * It also fills up a list of filesToCopy from the interestpoints directory if it is not null. - * - * @param spimData - the source SpimData - * @param filesToCopy - list to be filled with files to copy - * @param viewIds - view subset to resave - * @param basePath - the base path - * @return new SpimData - */ - public static SpimData2 assemblePartialSpimData2( final SpimData2 spimData, final List< ? extends ViewId > viewIds, final URI basePath, final List< String > filesToCopy ) - { - final TimePoints timepoints; + boolean isEqual = false; try { - timepoints = new TimePointsPattern( listAllTimePoints( SpimData2.getAllTimePointsSorted( spimData, viewIds ) ) ); + isEqual = spimData.getBasePathURI().equals( newBasePath ); } - catch (ParseException e) + catch ( Exception e ) { - IOFunctions.println( "Automatically created list of timepoints failed to parse. This should not happen, really :) -- " + e ); - IOFunctions.println( "Here is the list: " + listAllTimePoints( SpimData2.getAllTimePointsSorted( spimData, viewIds ) ) ); - e.printStackTrace(); - return null; + isEqual = false; } - final List< ViewSetup > setups = SpimData2.getAllViewSetupsSorted( spimData, viewIds ); - - // a hashset for all viewsetups that remain - final Set< ViewId > views = new HashSet< ViewId >(); - - for ( final ViewId viewId : viewIds ) - views.add( new ViewId( viewId.getTimePointId(), viewId.getViewSetupId() ) ); - - final MissingViews oldMissingViews = spimData.getSequenceDescription().getMissingViews(); - final HashSet< ViewId > missingViews = new HashSet< ViewId >(); - - if ( oldMissingViews != null && oldMissingViews.getMissingViews() != null ) - for ( final ViewId id : oldMissingViews.getMissingViews() ) - if ( views.contains( id ) ) - missingViews.add( id ); - - // add the new missing views!!! - for ( final TimePoint t : timepoints.getTimePointsOrdered() ) - for ( final ViewSetup v : setups ) - { - final ViewId viewId = new ViewId( t.getId(), v.getId() ); - - if ( !views.contains( viewId ) ) - missingViews.add( viewId ); - } - - // instantiate the sequencedescription - final SequenceDescription sequenceDescription = new SequenceDescription( timepoints, setups, null, new MissingViews( missingViews ) ); + if ( isEqual ) + newSpimData = SpimData2Tools.reduceSpimData2( spimData, viewIds ); + else + newSpimData = SpimData2Tools.reduceSpimData2( spimData, viewIds, newBasePath ); - // re-assemble the registrations - final Map< ViewId, ViewRegistration > oldRegMap = spimData.getViewRegistrations().getViewRegistrations(); - final Map< ViewId, ViewRegistration > newRegMap = new HashMap< ViewId, ViewRegistration >(); - - for ( final ViewId viewId : oldRegMap.keySet() ) - if ( views.contains( viewId ) ) - newRegMap.put( viewId, oldRegMap.get( viewId ) ); - - final ViewRegistrations viewRegistrations = new ViewRegistrations( newRegMap ); - - /* - // re-assemble the interestpoints and a list of filenames to copy - final Map< ViewId, ViewInterestPointLists > oldInterestPoints = spimData.getViewInterestPoints().getViewInterestPoints(); - final Map< ViewId, ViewInterestPointLists > newInterestPoints = new HashMap< ViewId, ViewInterestPointLists >(); - - for ( final ViewId viewId : oldInterestPoints.keySet() ) - if ( views.contains( viewId ) ) - { - final ViewInterestPointLists ipLists = oldInterestPoints.get( viewId ); - newInterestPoints.put( viewId, ipLists ); - - if ( filesToCopy != null ) - { - // get also all the filenames that we need to copy - for ( final InterestPoints ipl : ipLists.getHashMap().values() ) - filesToCopy.add( ipl.getFile().getName() ); - } - } - - final ViewInterestPoints viewsInterestPoints = new ViewInterestPoints( newInterestPoints );*/ - - //TODO: copy interestpoints - //TODO: copy PSFs - - final SpimData2 newSpimData = new SpimData2( - basePath, - sequenceDescription, - viewRegistrations, - new ViewInterestPoints(), - spimData.getBoundingBoxes(), - spimData.getPointSpreadFunctions(), - spimData.getStitchingResults(), - spimData.getIntensityAdjustments()); + final StackImgLoaderIJ imgLoader = new StackImgLoaderIJ( + new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParentFile(), + filename, + layoutTP, layoutChannels, layoutIllum, layoutAngles, layoutTiles, newSpimData.getSequenceDescription() ); + newSpimData.getSequenceDescription().setImgLoader( imgLoader ); return newSpimData; } diff --git a/src/main/java/net/preibisch/mvrecon/fiji/spimdata/explorer/popup/ResavePopup.java b/src/main/java/net/preibisch/mvrecon/fiji/spimdata/explorer/popup/ResavePopup.java index 47b42667..244b598d 100644 --- a/src/main/java/net/preibisch/mvrecon/fiji/spimdata/explorer/popup/ResavePopup.java +++ b/src/main/java/net/preibisch/mvrecon/fiji/spimdata/explorer/popup/ResavePopup.java @@ -26,7 +26,6 @@ import java.awt.event.ActionListener; import java.io.File; import java.net.URI; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -42,7 +41,6 @@ import mpicbg.spim.data.sequence.ViewId; import mpicbg.spim.data.sequence.ViewSetup; import net.preibisch.legacy.io.IOFunctions; -import net.preibisch.mvrecon.fiji.ImgLib2Temp.Pair; import net.preibisch.mvrecon.fiji.plugin.resave.Generic_Resave_HDF5; import net.preibisch.mvrecon.fiji.plugin.resave.ParametersResaveN5; import net.preibisch.mvrecon.fiji.plugin.resave.ProgressWriterIJ; @@ -53,6 +51,7 @@ import net.preibisch.mvrecon.fiji.spimdata.SpimData2; import net.preibisch.mvrecon.fiji.spimdata.explorer.ExplorerWindow; import net.preibisch.mvrecon.fiji.spimdata.explorer.FilteredAndGroupedExplorerPanel; +import net.preibisch.mvrecon.process.resave.SpimData2Tools; import util.URITools; public class ResavePopup extends JMenu implements ExplorerWindowSetable @@ -196,14 +195,14 @@ else if (choice == JOptionPane.YES_OPTION) Resave_TIFF.writeTIFF( data, viewIds, new File( URITools.removeFilePrefix( params.getXMLPath() ) ).getParent(), params.compress, progressWriter ); // write the XML - final Pair< SpimData2, List< String > > result = Resave_TIFF.createXMLObject( data, viewIds, params ); + final SpimData2 newSpimData = Resave_TIFF.createXMLObject( data, viewIds, params ); progressWriter.setProgress( 1.01 ); // copy the interest points is not necessary as we overwrite the XML if they exist // Resave_TIFF.copyInterestPoints( data.getBasePath(), new File( params.xmlFile ).getParentFile(), result.getB() ); // replace the spimdata object - panel.setSpimData( result.getA() ); + panel.setSpimData( newSpimData ); panel.updateContent(); panel.saveXML(); } @@ -266,15 +265,15 @@ else if ( index == 2 || index == 3 ) // HDF5, compressed HDF5 0, Double.NaN, Double.NaN ); // write hdf5 - Generic_Resave_HDF5.writeHDF5( Resave_HDF5.reduceSpimData2( data, viewIds ), params, progressWriter ); + Generic_Resave_HDF5.writeHDF5( SpimData2Tools.reduceSpimData2( data, viewIds ), params, progressWriter ); - final Pair< SpimData2, List< String > > result = Resave_HDF5.createXMLObject( data, viewIds, params, progressWriter, true ); + final SpimData2 newSpimData = Resave_HDF5.createXMLObject( data, viewIds, params, progressWriter, true ); // copy the interest points is not necessary as we overwrite the XML if they exist // Resave_TIFF.copyInterestPoints( xml.getData().getBasePath(), params.getSeqFile().getParentFile(), result.getB() ); // replace the spimdata object - panel.setSpimData( result.getA() ); + panel.setSpimData( newSpimData ); panel.updateContent(); progressWriter.setProgress( 1.0 ); diff --git a/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java b/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java new file mode 100644 index 00000000..c8c79494 --- /dev/null +++ b/src/main/java/net/preibisch/mvrecon/process/resave/SpimData2Tools.java @@ -0,0 +1,215 @@ +package net.preibisch.mvrecon.process.resave; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.text.ParseException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import mpicbg.spim.data.registration.ViewRegistration; +import mpicbg.spim.data.registration.ViewRegistrations; +import mpicbg.spim.data.sequence.MissingViews; +import mpicbg.spim.data.sequence.SequenceDescription; +import mpicbg.spim.data.sequence.TimePoint; +import mpicbg.spim.data.sequence.TimePoints; +import mpicbg.spim.data.sequence.TimePointsPattern; +import mpicbg.spim.data.sequence.ViewId; +import mpicbg.spim.data.sequence.ViewSetup; +import net.preibisch.legacy.io.IOFunctions; +import net.preibisch.mvrecon.fiji.spimdata.SpimData2; +import net.preibisch.mvrecon.fiji.spimdata.interestpoints.CorrespondingInterestPoints; +import net.preibisch.mvrecon.fiji.spimdata.interestpoints.InterestPoint; +import net.preibisch.mvrecon.fiji.spimdata.interestpoints.InterestPoints; +import net.preibisch.mvrecon.fiji.spimdata.interestpoints.ViewInterestPointLists; +import net.preibisch.mvrecon.fiji.spimdata.interestpoints.ViewInterestPoints; +import net.preibisch.mvrecon.fiji.spimdata.pointspreadfunctions.PointSpreadFunctions; + +public class SpimData2Tools +{ + + /** + * Reduces a given SpimData2 to the subset of timepoints and viewsetups as selected by the user, + * including the original imgloader and keeping the basepath (i.e. interest points still work) + * + * Note: PSF's will be lost. + * + * @param oldSpimData - the original SpimData + * @param viewIds - the views to keep + * @return - reduced SpimData2 + */ + public static SpimData2 reduceSpimData2( final SpimData2 oldSpimData, final List< ViewId > viewIds ) + { + return reduceSpimData2( oldSpimData, viewIds, null ); + } + + /** + * Reduces a given SpimData2 to the subset of timepoints and viewsetups as selected by the user, + * including the original imgloader and changing the base path (you still need to save to materialize the returned object!) + * + * Note: PSF's will be lost. + * + * @param oldSpimData - the original SpimData + * @param viewIds - the views to keep + * @param basePath - the new base path (can be null); if you set a new base path it will load all interest points so the new SpimData2 object can be saved including these points + * @return - reduced SpimData2 + */ + public static SpimData2 reduceSpimData2( final SpimData2 oldSpimData, final List< ViewId > viewIds, final URI basePath ) + { + final TimePoints timepoints; + + try + { + timepoints = new TimePointsPattern( listAllTimePoints( SpimData2.getAllTimePointsSorted( oldSpimData, viewIds ) ) ); + } + catch (ParseException e) + { + IOFunctions.println( "Automatically created list of timepoints failed to parse. This should not happen, really :) -- " + e ); + IOFunctions.println( "Here is the list: " + listAllTimePoints( SpimData2.getAllTimePointsSorted( oldSpimData, viewIds ) ) ); + e.printStackTrace(); + return null; + } + + final List< ViewSetup > viewSetupsToProcess = SpimData2.getAllViewSetupsSorted( oldSpimData, viewIds ); + + // a hashset for all viewsetups that remain + final Set< ViewId > views = new HashSet< ViewId >(); + + for ( final ViewId viewId : viewIds ) + views.add( new ViewId( viewId.getTimePointId(), viewId.getViewSetupId() ) ); + + final MissingViews oldMissingViews = oldSpimData.getSequenceDescription().getMissingViews(); + final HashSet< ViewId > missingViews = new HashSet< ViewId >(); + + if( oldMissingViews != null && oldMissingViews.getMissingViews() != null ) + for ( final ViewId id : oldMissingViews.getMissingViews() ) + if ( views.contains( id ) ) + missingViews.add( id ); + + // add the new missing views!!! + for ( final TimePoint t : timepoints.getTimePointsOrdered() ) + for ( final ViewSetup v : viewSetupsToProcess ) + { + final ViewId viewId = new ViewId( t.getId(), v.getId() ); + + if ( !views.contains( viewId ) ) + missingViews.add( viewId ); + } + + // instantiate the sequencedescription + final SequenceDescription sequenceDescription = new SequenceDescription( timepoints, viewSetupsToProcess, oldSpimData.getSequenceDescription().getImgLoader(), new MissingViews( missingViews ) ); + + // re-assemble the registrations + final Map< ViewId, ViewRegistration > oldRegMap = oldSpimData.getViewRegistrations().getViewRegistrations(); + final Map< ViewId, ViewRegistration > newRegMap = new HashMap< ViewId, ViewRegistration >(); + + for ( final ViewId viewId : oldRegMap.keySet() ) + if ( views.contains( viewId ) ) + newRegMap.put( viewId, oldRegMap.get( viewId ) ); + + final ViewRegistrations viewRegistrations = new ViewRegistrations( newRegMap ); + + // re-assemble the interestpoints and a list of filenames to copy + final Map< ViewId, ViewInterestPointLists > oldInterestPoints = oldSpimData.getViewInterestPoints().getViewInterestPoints(); + final Map< ViewId, ViewInterestPointLists > newInterestPoints = new HashMap< ViewId, ViewInterestPointLists >(); + + oldInterestPoints.forEach( (viewId, ipLists) -> + { + if ( views.contains( viewId ) ) + { + if ( basePath != null ) + { + final ViewInterestPointLists ipListsNew = new ViewInterestPointLists(viewId.getTimePointId(), viewId.getViewSetupId() ); + + ipLists.getHashMap().forEach( (label,interestpoints) -> + { + final List points = interestpoints.getInterestPointsCopy(); + final List corr = interestpoints.getCorrespondingInterestPointsCopy(); + + final InterestPoints interestpointsNew = InterestPoints.newInstance( basePath, viewId, label ); + interestpointsNew.setInterestPoints( points ); + interestpointsNew.setCorrespondingInterestPoints( corr ); + + ipListsNew.addInterestPointList( label, interestpointsNew ); + } ); + + newInterestPoints.put( viewId, ipListsNew ); + } + else + { + // if the basepath doesn't change we can keep interestpoints as-is + newInterestPoints.put( viewId, ipLists ); + } + } + }); + + final ViewInterestPoints viewsInterestPoints = new ViewInterestPoints( newInterestPoints ); + + //TODO: copy PSFs? + + final SpimData2 newSpimData = new SpimData2( + basePath == null ? oldSpimData.getBasePathURI() : basePath, + sequenceDescription, + viewRegistrations, + viewsInterestPoints, + oldSpimData.getBoundingBoxes(), + new PointSpreadFunctions(), //oldSpimData.getPointSpreadFunctions() + oldSpimData.getStitchingResults(), + oldSpimData.getIntensityAdjustments() ); + + return newSpimData; + } + + public static String listAllTimePoints( final List timePointsToProcess ) + { + String t = "" + timePointsToProcess.get( 0 ).getId(); + + for ( int i = 1; i < timePointsToProcess.size(); ++i ) + t += ", " + timePointsToProcess.get( i ).getId(); + + return t; + } + + public static void copyFolder( final File src, final File dest, final List< String > filesToCopy ) throws IOException + { + if ( src.isDirectory() ) + { + if( !dest.exists() ) + dest.mkdir(); + + for ( final String file : src.list() ) + copyFolder( new File( src, file ), new File( dest, file ), filesToCopy ); + } + else + { + boolean contains = false; + + for ( int i = 0; i < filesToCopy.size() && !contains; ++i ) + if ( src.getName().contains( filesToCopy.get( i ) ) ) + contains = true; + + if ( contains ) + { + final InputStream in = new FileInputStream( src ); + final OutputStream out = new FileOutputStream( dest ); + + final byte[] buffer = new byte[ 65535 ]; + + int length; + + while ( ( length = in.read(buffer) ) > 0 ) + out.write(buffer, 0, length); + + in.close(); + out.close(); + } + } + } +}