Skip to content

Commit

Permalink
consolidate methods for creating new SpimData2 objects from existing …
Browse files Browse the repository at this point in the history
…ones
  • Loading branch information
StephanPreibisch committed Sep 3, 2024
1 parent 62209b8 commit 04c870a
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 334 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
131 changes: 23 additions & 108 deletions src/main/java/net/preibisch/mvrecon/fiji/plugin/resave/Resave_HDF5.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand Down
Loading

0 comments on commit 04c870a

Please sign in to comment.