Skip to content

Add Hessian eigenvalues #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
e1678fb
added first version of hessian eigenvalues
hanslovsky Aug 3, 2016
032c525
implemented copy method
hanslovsky Aug 3, 2016
fda376d
Changes to Matrix classes and eigenvalue methods
hanslovsky Aug 3, 2016
ee213c3
copy() returning deep copy now
hanslovsky Aug 3, 2016
17dded2
added methods for passing result RandomAccesisbleIntervals
hanslovsky Aug 3, 2016
9aecda0
Added parallel version for gradient.
hanslovsky Aug 4, 2016
fbe5282
Use parallel gradient, pass number of threads
hanslovsky Aug 4, 2016
9b76979
removed time logs
hanslovsky Aug 4, 2016
b96cd39
added parrallel option to eigenvalues
hanslovsky Aug 4, 2016
00fc4a9
added author tags (PartialDerivative unclear)
hanslovsky Aug 4, 2016
a515974
Merge branch 'master' into hessian-eigenvalues
hanslovsky Aug 8, 2016
dc8c079
added documentation/comments
hanslovsky Aug 8, 2016
3074bee
Add HessianMatrixEigenValuesTest
hanslovsky Aug 8, 2016
e7b31ff
Add test for HessianMatrix (2D and 3D)
hanslovsky Aug 8, 2016
3b7b075
Remove consistency test for Hessian matrix from HessianMatrixEigenVal…
hanslovsky Aug 8, 2016
7fa9b69
Add @tpietzsch as author of PartialDerivative.java
hanslovsky Aug 8, 2016
d5871fd
Merge branch 'master' into hessian-eigenvalues
hanslovsky Aug 10, 2016
b72af5f
removed e-mails from author tags
hanslovsky Aug 10, 2016
80a2b9e
Merge remote-tracking branch 'origin/master' into hessian-eigenvalues
hanslovsky Feb 16, 2017
a7dd4d8
Simplify interfaces
hanslovsky Feb 16, 2017
5273d3c
Rename packages (more appropriately)
hanslovsky Feb 16, 2017
c925680
Use nTasks instead of nThreads
hanslovsky Feb 16, 2017
58f24bd
Add convenience methods for gaussian smoothing to Hessian matrix
hanslovsky Feb 16, 2017
a4e427c
Remove number of threads from nTasks JavaDoc
hanslovsky Feb 16, 2017
1040585
Make number of dimensions consistent
hanslovsky Feb 16, 2017
0ff0e19
Change EigenValue interface to accept ComplexType
hanslovsky Feb 22, 2017
4533ee0
Optimize EigenValue and avoid creation of object with every call to c…
hanslovsky Feb 22, 2017
1cc9f93
Remove bounds check for RealCompositeMatrix.getEntry
hanslovsky Feb 22, 2017
7f928eb
Relax interface for RealCompositeMatrix.data and EigenValue.compute f…
hanslovsky Feb 22, 2017
6489ec5
Add license header.
hanslovsky Feb 24, 2017
de07cf4
Add convenience method for scaling Hessian according to sigma.
hanslovsky Feb 24, 2017
fca300c
Add eigenvalue decomposition using ojAlgo
hanslovsky Mar 8, 2017
6d91ca2
Merge remote-tracking branch 'origin/master' into hessian-eigenvalues…
hanslovsky Apr 24, 2017
0b6b80f
Use ojAlgo instead of commons-math
hanslovsky Apr 24, 2017
ba28c27
Merge pull request #1 from hanslovsky/hessian-eigenvalues-ojalgo
hanslovsky Apr 24, 2017
f709b25
Fix formatting issues
hanslovsky Apr 24, 2017
cbfc6b5
Use gradientCentralDifference instead of gradientCentralDifference2
hanslovsky May 16, 2017
c6a1ad0
Use more concise and readable Util.min over streams.
hanslovsky May 16, 2017
1e85a8c
Make constructors with length parameters protected
hanslovsky May 16, 2017
0d6e352
Remove unused import java.util.Arrays
hanslovsky May 16, 2017
1b63be8
Specify linear representation of upper triangular Hessian in JavaDoc
hanslovsky May 16, 2017
7366220
Add JavaDoc to HessianMatrix.scaleHessianMatrix
hanslovsky May 16, 2017
c7608c4
Merge branch 'master' of github.com:imglib/imglib2-algorithm into hes…
hanslovsky May 16, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ Jean-Yves Tinevez and Michael Zinsmaier.</license.copyrightOwners>
<artifactId>trove4j</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>org.ojalgo</groupId>
<artifactId>ojalgo</artifactId>
<version>43.0</version>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
389 changes: 389 additions & 0 deletions src/main/java/net/imglib2/algorithm/gradient/HessianMatrix.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -34,20 +34,34 @@

package net.imglib2.algorithm.gradient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;

import net.imglib2.Cursor;
import net.imglib2.FinalInterval;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.numeric.NumericType;
import net.imglib2.util.Intervals;
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;

/**
* @author Tobias Pietzsch
* @author Philipp Hanslovsky
*
*/
public class PartialDerivative
{
// nice version...
/**
* Compute the partial derivative of source in a particular dimension.
*
*
* @param source
* source image, has to provide valid data in the interval of the
* gradient image plus a one pixel border in dimension.
Expand All @@ -69,10 +83,82 @@ public static < T extends NumericType< T > > void gradientCentralDifference2( fi
}
}

// parallel version...
/**
* Compute the partial derivative of source in a particular dimension.
*
* @param source
* source image, has to provide valid data in the interval of the
* gradient image plus a one pixel border in dimension.
* @param gradient
* output image
* @param dimension
* along which dimension the partial derivatives are computed
* @param nTasks
* Number of tasks for gradient computation.
* @param es
* {@link ExecutorService} providing workers for gradient
* computation. Service is managed (created, shutdown) by caller.
*/
public static < T extends NumericType< T > > void gradientCentralDifferenceParallel(
final RandomAccessible< T > source,
final RandomAccessibleInterval< T > gradient,
final int dimension,
final int nTasks,
final ExecutorService es ) throws InterruptedException, ExecutionException
{
final int nDim = source.numDimensions();
if ( nDim < 2 )
{
gradientCentralDifference( source, gradient, dimension );
return;
}

long dimensionMax = Long.MIN_VALUE;
int dimensionArgMax = -1;

for ( int d = 0; d < nDim; ++d )
{
final long size = gradient.dimension( d );
if ( d != dimension && size > dimensionMax )
{
dimensionMax = size;
dimensionArgMax = d;
}
}

final long stepSize = Math.max( dimensionMax / nTasks, 1 );
final long stepSizeMinusOne = stepSize - 1;
final long min = gradient.min( dimensionArgMax );
final long max = gradient.max( dimensionArgMax );

final ArrayList< Callable< Void > > tasks = new ArrayList<>();
for ( long currentMin = min, minZeroBase = 0; minZeroBase < dimensionMax; currentMin += stepSize, minZeroBase += stepSize )
{
final long currentMax = Math.min( currentMin + stepSizeMinusOne, max );
final long[] mins = new long[ nDim ];
final long[] maxs = new long[ nDim ];
gradient.min( mins );
gradient.max( maxs );
mins[ dimensionArgMax ] = currentMin;
maxs[ dimensionArgMax ] = currentMax;
final IntervalView< T > currentInterval = Views.interval( gradient, new FinalInterval( mins, maxs ) );
tasks.add( () -> {
gradientCentralDifference( source, currentInterval, dimension );
return null;
} );
}

final List< Future< Void > > futures = es.invokeAll( tasks );

for ( final Future< Void > f : futures )
f.get();
}

// fast version
/**
* Compute the partial derivative of source in a particular dimension.
*
*
* @param source
* source image, has to provide valid data in the interval of the
* gradient image plus a one pixel border in dimension.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2016 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imglib2.algorithm.gradient;

import java.util.function.ToDoubleFunction;

import net.imglib2.Interval;
import net.imglib2.Localizable;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessible;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.converter.AbstractConvertedRandomAccess;
import net.imglib2.converter.AbstractConvertedRandomAccessible;
import net.imglib2.type.Type;
import net.imglib2.type.operators.MulFloatingPoint;

/**
*
* @author Philipp Hanslovsky
*
* Multiply the value of a {@link RandomAccessible} depending on the
* position of the access.
*
*/
public class ScaleAsFunctionOfPosition< T extends Type< T > & MulFloatingPoint > extends AbstractConvertedRandomAccessible< T, T >
{

private final ToDoubleFunction< Localizable > scalingFunction;

public ScaleAsFunctionOfPosition( final RandomAccessibleInterval< T > source, final ToDoubleFunction< Localizable > scalingFunction )
{
super( source );
this.scalingFunction = scalingFunction;
}

@Override
public ScaledRandomAccess< T > randomAccess()
{
return new ScaledRandomAccess<>( source.randomAccess(), scalingFunction );
}

@Override
public AbstractConvertedRandomAccess< T, T > randomAccess( final Interval interval )
{
return randomAccess();
}

public static class ScaledRandomAccess< T extends Type< T > & MulFloatingPoint > extends AbstractConvertedRandomAccess< T, T >
{

private final T t;

private final ToDoubleFunction< Localizable > scalingFunction;

public ScaledRandomAccess( final RandomAccess< T > source, final ToDoubleFunction< Localizable > scalingFunction )
{
super( source );
this.t = source.get().createVariable();
this.scalingFunction = scalingFunction;
}

@Override
public T get()
{
t.set( source.get() );
t.mul( scalingFunction.applyAsDouble( source ) );
return t;
}

@Override
public ScaledRandomAccess< T > copy()
{
return new ScaledRandomAccess<>( source.copyRandomAccess(), scalingFunction );
}

}

}
88 changes: 88 additions & 0 deletions src/main/java/net/imglib2/algorithm/linalg/eigen/EigenValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2016 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imglib2.algorithm.linalg.eigen;

import net.imglib2.type.numeric.ComplexType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.view.composite.Composite;

/**
*
* Interface for handling different cases, e.g. square, symmetric, or 2
* dimensional tensors.
*
*/
public interface EigenValues< T extends RealType< T >, U extends ComplexType< U > >
{
public void compute( final Composite< T > matrix, final Composite< U > evs );

public default EigenValues< T, U > copy()
{
return this;
}

public static < T extends RealType< T >, U extends ComplexType< U > > EigenValues1D< T, U > oneDimensional()
{
return new EigenValues1D<>();
}

public static < T extends RealType< T >, U extends ComplexType< U > > EigenValues2DSquare< T, U > square2D()
{
return new EigenValues2DSquare<>();
}

public static < T extends RealType< T >, U extends ComplexType< U > > EigenValues2DSymmetric< T, U > symmetric2D()
{
return new EigenValues2DSymmetric<>();
}

public static < T extends RealType< T >, U extends ComplexType< U > > EigenValuesSquare< T, U > square( final int nDim )
{
return new EigenValuesSquare<>( nDim );
}

public static < T extends RealType< T >, U extends ComplexType< U > > EigenValuesSymmetric< T, U > symmetric( final int nDim )
{
return new EigenValuesSymmetric<>( nDim );
}

public static < T extends RealType< T >, U extends ComplexType< U > > EigenValues< T, U > invalid()
{
return ( m, evs ) -> {
throw new UnsupportedOperationException( "EigenValues not implemented yet!" );
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* #%L
* ImgLib2: a general-purpose, multidimensional image processing library.
* %%
* Copyright (C) 2009 - 2016 Tobias Pietzsch, Stephan Preibisch, Stephan Saalfeld,
* John Bogovic, Albert Cardona, Barry DeZonia, Christian Dietz, Jan Funke,
* Aivar Grislis, Jonathan Hale, Grant Harris, Stefan Helfrich, Mark Hiner,
* Martin Horn, Steffen Jaensch, Lee Kamentsky, Larry Lindsey, Melissa Linkert,
* Mark Longair, Brian Northan, Nick Perry, Curtis Rueden, Johannes Schindelin,
* Jean-Yves Tinevez and Michael Zinsmaier.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package net.imglib2.algorithm.linalg.eigen;

import net.imglib2.type.numeric.ComplexType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.view.composite.Composite;

public class EigenValues1D< T extends RealType< T >, U extends ComplexType< U > > implements EigenValues< T, U >
{
@Override
public void compute( final Composite< T > tensor, final Composite< U > evs )
{
evs.get( 0 ).setReal( tensor.get( 0 ).getRealDouble() );
}
}
Loading