Skip to content

Commit

Permalink
BlockDataSolveParameters creates Worker given a BlockData object
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanPreibisch committed Aug 16, 2023
1 parent ad6f2ef commit 7563f18
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -13,6 +12,7 @@
import org.janelia.render.client.newsolver.blocksolveparameters.FIBSEMAlignmentParameters;
import org.janelia.render.client.newsolver.setup.AffineSolverSetup;
import org.janelia.render.client.newsolver.setup.RenderSetup;
import org.janelia.render.client.newsolver.solvers.Worker;
import org.janelia.render.client.newsolver.solvers.affine.AffineAlignBlockWorker;

import mpicbg.models.Affine2D;
Expand All @@ -24,6 +24,8 @@ public class AffineDistributedSolver
{
final AffineSolverSetup cmdLineSetup;
final RenderSetup renderSetup;
BlockCollection< ?, AffineModel2D, ? extends FIBSEMAlignmentParameters< ?, ? >, ZBlockFactory > col;
ZBlockFactory blockFactory;

public AffineDistributedSolver(
final AffineSolverSetup cmdLineSetup,
Expand Down Expand Up @@ -80,16 +82,18 @@ public static void main( final String[] args ) throws IOException
// Note: different setups can be used if specific things need to be done for the solve or certain blocks
final AffineDistributedSolver solverSetup = new AffineDistributedSolver( cmdLineSetup, renderSetup );

final ArrayList< ? extends AffineAlignBlockWorker<?, ?, ZBlockFactory > > workers =
// create all block instances
final BlockCollection< ?, AffineModel2D, ?, ZBlockFactory > blockCollection =
solverSetup.setupSolve( cmdLineSetup.blockModel(), cmdLineSetup.stitchingModel() );

final ExecutorService taskExecutor = Executors.newFixedThreadPool( cmdLineSetup.threadsGlobal );

taskExecutor.submit( () ->
workers.parallelStream().forEach( worker ->
blockCollection.allBlocks().parallelStream().forEach( block ->
{
try
{
Worker<?, AffineModel2D, ?, ZBlockFactory> worker = block.solveTypeParameters().createWorker( block, solverSetup.col.maxId() + 1, cmdLineSetup.threadsWorker );
worker.run();
}
catch (IOException | ExecutionException | InterruptedException | NoninvertibleModelException e)
Expand All @@ -100,17 +104,22 @@ public static void main( final String[] args ) throws IOException
}));

taskExecutor.shutdown();

for ( final AffineAlignBlockWorker<?, ?, ZBlockFactory > worker : workers )
/*
for ( final Worker<?, ?, ZBlockFactory > worker : workers )
{
List<?> blockData = worker.getBlockDataList();
ArrayList< ? extends BlockData< ?, AffineModel2D, ?, ? > > blockData = worker.getBlockDataList();
}
// avoid duplicate id assigned while splitting solveitems in the workers
// but do keep ids that are smaller or equal to the maxId of the initial solveset
final int maxId = SolveTools.fixIds( this.allItems, solverSetup.col.maxId() );
System.out.println( workers.get( 0 ).getBlockDataList().size() );
*/
}

public < M extends Model< M > & Affine2D< M >, S extends Model< S > & Affine2D< S > >
ArrayList< AffineAlignBlockWorker<M, S, ZBlockFactory > > setupSolve(
BlockCollection< M, AffineModel2D, FIBSEMAlignmentParameters< M, S >, ZBlockFactory > setupSolve(
final M blockModel,
final S stitchingModel )
{
Expand All @@ -119,22 +128,17 @@ ArrayList< AffineAlignBlockWorker<M, S, ZBlockFactory > > setupSolve(
//
final ZBlockFactory blockFactory = setupBlockFactory();

//
// setup FIB-SEM solve parameters
//
final FIBSEMAlignmentParameters< M, S > solveParams =
setupSolveParameters( blockModel, stitchingModel );
this.blockFactory = blockFactory;

//
// create all blocks
//
final BlockCollection< M, AffineModel2D, FIBSEMAlignmentParameters< M, S >, ZBlockFactory > col =
setupBlockCollection( blockFactory, solveParams );
setupBlockCollection( blockFactory, blockModel, stitchingModel );

//
// create workers
//
return createWorkers( col );
this.col = col;

return col;
}

protected ZBlockFactory setupBlockFactory()
Expand Down Expand Up @@ -181,10 +185,17 @@ protected < M extends Model< M > & Affine2D< M >, S extends Model< S > & Affine2

protected < M extends Model< M > & Affine2D< M >, S extends Model< S > & Affine2D< S > > BlockCollection< M, AffineModel2D, FIBSEMAlignmentParameters< M, S >, ZBlockFactory > setupBlockCollection(
final ZBlockFactory blockFactory,
final FIBSEMAlignmentParameters< M, S > solveParams )
final M blockModel,
final S stitchingModel )
{
//
// setup FIB-SEM solve parameter object
//
final FIBSEMAlignmentParameters< M, S > defaultSolveParams =
setupSolveParameters( blockModel, stitchingModel );

final BlockCollection< M, AffineModel2D, FIBSEMAlignmentParameters< M, S >, ZBlockFactory > col =
blockFactory.defineBlockCollection( rtsc -> solveParams );
blockFactory.defineBlockCollection( rtsc -> defaultSolveParams );

return col;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package org.janelia.render.client.newsolver;

import java.util.List;
import java.util.ArrayList;

import org.janelia.render.client.newsolver.blockfactories.BlockFactory;
import org.janelia.render.client.newsolver.blocksolveparameters.BlockDataSolveParameters;

import mpicbg.models.CoordinateTransform;
import mpicbg.models.Model;

public class BlockCollection< M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, P >, F extends BlockFactory< F > >
public class BlockCollection< M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, R, P >, F extends BlockFactory< F > >
{
final List< BlockData< M, R, P, F > > blockDataList;
final ArrayList< BlockData< M, R, P, F > > blockDataList;
final int minId, maxId;

public BlockCollection( final List< BlockData< M, R, P, F > > blockDataList )
public BlockCollection( final ArrayList< BlockData< M, R, P, F > > blockDataList )
{
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Expand All @@ -31,5 +31,5 @@ public BlockCollection( final List< BlockData< M, R, P, F > > blockDataList )

public int minId() { return minId; }
public int maxId() { return maxId; }
public List< BlockData< M, R, P, F > > allBlocks() { return blockDataList; }
}
public ArrayList< BlockData< M, R, P, F > > allBlocks() { return blockDataList; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* @param <P> - the solve parameters
* @param <F> - the block factory
*/
public class BlockData< M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, P >, F extends BlockFactory< F > > implements Serializable
public class BlockData< M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, R, P >, F extends BlockFactory< F > > implements Serializable
{
private static final long serialVersionUID = -6491517262420660476L;

Expand Down Expand Up @@ -102,6 +102,8 @@ public ArrayList< Function< Double, Double > > createWeightFunctions()

public HashMap< Integer, HashSet< String > > zToTileId() { return zToTileId; }

public void assignUpdatedId( final int id ) { this.id = id; }

/**
* Fetches basic data for all TileSpecs
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public abstract class BlockFactory< F extends BlockFactory< F > > implements Ser
{
private static final long serialVersionUID = 5919345114414922447L;

public abstract <M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, P > > BlockCollection< M, R, P, F > defineBlockCollection(
final ParameterProvider< M, P > blockSolveParameterProvider );
public abstract <M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, R, P > > BlockCollection< M, R, P, F > defineBlockCollection(
final ParameterProvider< M, R, P > blockSolveParameterProvider );

public abstract ArrayList< Function< Double, Double > > createWeightFunctions( final BlockData< ?, ?, ?, F > block );
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import org.janelia.alignment.spec.ResolvedTileSpecCollection;
import org.janelia.render.client.newsolver.blocksolveparameters.BlockDataSolveParameters;

import mpicbg.models.CoordinateTransform;
import mpicbg.models.Model;

public interface ParameterProvider < M extends Model< M >, P extends BlockDataSolveParameters< M, P > >
public interface ParameterProvider < M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, R, P > >
{
public P create( ResolvedTileSpecCollection rtsc );
default public BlockDataSolveParameters< ?,? > basicParameters() { return create( null ); }
default public BlockDataSolveParameters< ?,?,? > basicParameters() { return create( null ); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ public ZBlockFactory( final int minZ, final int maxZ, final int blockSize, final
this.minBlockSize = minBlockSize;
}

@SuppressWarnings("unchecked")
@Override
public <M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters<M,P>> BlockCollection<M, R, P, ZBlockFactory> defineBlockCollection(
final ParameterProvider< M, P > blockSolveParameterProvider )
public <M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters<M,R,P>> BlockCollection<M, R, P, ZBlockFactory> defineBlockCollection(
final ParameterProvider< M, R, P > blockSolveParameterProvider )
{
final List< ZBlockInit > initBlocks = defineBlockLayout( minZ, maxZ, blockSize, minBlockSize );

final BlockDataSolveParameters< ?,? > basicParameters = blockSolveParameterProvider.basicParameters();
final BlockDataSolveParameters< ?,?,? > basicParameters = blockSolveParameterProvider.basicParameters();

//
// fetch metadata from render
Expand All @@ -56,7 +55,7 @@ public <M extends Model< M >, R extends CoordinateTransform, P extends BlockData
basicParameters.project() );


final List< BlockData< M, R, P, ZBlockFactory > > blockDataList = new ArrayList<>();
final ArrayList< BlockData< M, R, P, ZBlockFactory > > blockDataList = new ArrayList<>();

// for each block, we know the z-range
for ( final ZBlockInit initBlock : initBlocks )
Expand Down Expand Up @@ -175,6 +174,7 @@ public static List< ZBlockInit > defineBlockLayout( final int minZ, final int ma
return Stream.concat( leftSets.stream(), rightSets.stream() ).collect( Collectors.toList() );
}

@SuppressWarnings("unchecked")
@Override
public ArrayList<Function<Double, Double>> createWeightFunctions( final BlockData<?, ?, ?, ZBlockFactory > block)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import java.io.Serializable;

import org.janelia.render.client.newsolver.BlockData;
import org.janelia.render.client.newsolver.blockfactories.BlockFactory;
import org.janelia.render.client.newsolver.solvers.Worker;

import mpicbg.models.CoordinateTransform;
import mpicbg.models.Model;

/**
Expand All @@ -10,7 +15,7 @@
*
* @param <M> - the result model type
*/
public abstract class BlockDataSolveParameters< M extends Model< M >, B extends BlockDataSolveParameters< M, B > > implements Serializable
public abstract class BlockDataSolveParameters< M extends Model< M >, R extends CoordinateTransform, B extends BlockDataSolveParameters< M, R, B > > implements Serializable
{
private static final long serialVersionUID = -813404780882760053L;

Expand Down Expand Up @@ -41,4 +46,9 @@ public BlockDataSolveParameters(
public String stack() { return stack; }

public M blockSolveModel() { return blockSolveModel; }

public abstract < F extends BlockFactory< F > > Worker< M, R, B, F > createWorker(
final BlockData< M, R, B, F > blockData,
final int startId,
final int threadsWorker );
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
import java.util.List;
import java.util.function.Function;

import org.janelia.render.client.newsolver.BlockData;
import org.janelia.render.client.newsolver.blockfactories.BlockFactory;
import org.janelia.render.client.newsolver.solvers.Worker;
import org.janelia.render.client.newsolver.solvers.affine.AffineAlignBlockWorker;

import mpicbg.models.Affine2D;
import mpicbg.models.AffineModel2D;
import mpicbg.models.Model;

/**
Expand All @@ -13,7 +19,7 @@
* @param <B> the final block solve type (the result)
* @param <S> the stitching-first type
*/
public class FIBSEMAlignmentParameters< M extends Model< M > & Affine2D< M >, S extends Model< S > & Affine2D< S > > extends BlockDataSolveParameters< M, FIBSEMAlignmentParameters< M, S> >
public class FIBSEMAlignmentParameters< M extends Model< M > & Affine2D< M >, S extends Model< S > & Affine2D< S > > extends BlockDataSolveParameters< M, AffineModel2D, FIBSEMAlignmentParameters< M, S > >
{
private static final long serialVersionUID = 4247180309556813829L;
public enum PreAlign { NONE, TRANSLATION, RIGID }
Expand Down Expand Up @@ -130,4 +136,12 @@ public FIBSEMAlignmentParameters(
public int maxIterationsStitching() { return maxIterationsStitching; }
public int maxPlateauWidthStitching() { return maxPlateauWidthStitching; }

@Override
public < F extends BlockFactory< F > > Worker< M, AffineModel2D, FIBSEMAlignmentParameters<M, S >, F > createWorker(
final BlockData< M, AffineModel2D, FIBSEMAlignmentParameters< M, S >, F > blockData,
final int startId,
final int threadsWorker )
{
return new AffineAlignBlockWorker<>( blockData, startId, threadsWorker );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@

import mpicbg.models.Affine1D;
import mpicbg.models.Model;

import org.janelia.render.client.newsolver.BlockData;
import org.janelia.render.client.newsolver.blockfactories.BlockFactory;
import org.janelia.render.client.newsolver.solvers.Worker;
import org.janelia.render.client.newsolver.solvers.affine.AffineAlignBlockWorker;
import org.janelia.render.client.newsolver.solvers.intensity.AffineIntensityCorrectionBlockWorker;
import org.janelia.render.client.parameter.IntensityAdjustParameters;

import java.io.IOException;
import java.util.List;

/**
Expand All @@ -13,7 +20,7 @@
* @param <M> the final block solve type (the result)
*/
public class FIBSEMIntensityCorrectionParameters<M extends Model<M> & Affine1D<M>>
extends BlockDataSolveParameters<M, FIBSEMIntensityCorrectionParameters<M>> {
extends BlockDataSolveParameters<M, M, FIBSEMIntensityCorrectionParameters<M>> {
private static final long serialVersionUID = -5349107301431384524L;

final private List<Double> blockOptimizerLambdasTranslation;
Expand Down Expand Up @@ -55,4 +62,20 @@ public FIBSEMIntensityCorrectionParameters(
public double renderScale() { return intensityParameters.renderScale; }
public int numCoefficients() { return intensityParameters.numCoefficients; }
public Integer zDistance() { return intensityParameters.zDistance; }

@Override
public <F extends BlockFactory<F>> Worker<M, M, FIBSEMIntensityCorrectionParameters<M>, F> createWorker(
final BlockData<M, M, FIBSEMIntensityCorrectionParameters<M>, F> blockData,
final int startId,
final int threadsWorker )
{
try
{
return new AffineIntensityCorrectionBlockWorker<>( blockData, startId, threadsWorker );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @param <P> - the solve parameters
* @param <F> - the block factory
*/
public abstract class Worker < M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, P >, F extends BlockFactory< F > >
public abstract class Worker < M extends Model< M >, R extends CoordinateTransform, P extends BlockDataSolveParameters< M, R, P >, F extends BlockFactory< F > >
{
// for assigning new id's when splitting BlockData
final protected int startId;
Expand Down
Loading

0 comments on commit 7563f18

Please sign in to comment.