Skip to content

Commit

Permalink
Merge pull request #204 from BIOP/refactor-register
Browse files Browse the repository at this point in the history
Big refactoring where registration classes and plugins have been moved in the upstream repo bigdataviewer-biop-tools

The goal was to reuse the nicer registration plugins of ABBA in Warpy instead of creating new ones
  • Loading branch information
NicoKiaru authored Jun 19, 2024
2 parents 619fdf9 + 5ce96c7 commit 2831d9a
Show file tree
Hide file tree
Showing 36 changed files with 63 additions and 2,003 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@

<!-- bdv playground related dependencies -->
<bigdataviewer-playground.version>0.10.1</bigdataviewer-playground.version>
<bigdataviewer-image-loaders.version>0.7.1</bigdataviewer-image-loaders.version>
<bigdataviewer-biop-tools.version>0.9.4</bigdataviewer-biop-tools.version>
<bigdataviewer-image-loaders.version>0.7.2</bigdataviewer-image-loaders.version>
<bigdataviewer-biop-tools.version>0.10.0</bigdataviewer-biop-tools.version>
<bigdataviewer-selector.version>0.2.6</bigdataviewer-selector.version>

<!-- maths -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.epfl.biop.atlas.aligner;

import ch.epfl.biop.atlas.aligner.plugin.RegistrationPluginHelper;
import ch.epfl.biop.registration.plugin.RegistrationPluginHelper;
import ch.epfl.biop.sourceandconverter.processor.SourcesIdentity;
import ch.epfl.biop.sourceandconverter.processor.SourcesProcessComposer;
import ch.epfl.biop.sourceandconverter.processor.SourcesProcessor;
Expand Down
65 changes: 16 additions & 49 deletions src/main/java/ch/epfl/biop/atlas/aligner/MultiSlicePositioner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import bdv.viewer.SourceAndConverter;
import ch.epfl.biop.atlas.aligner.action.*;
import ch.epfl.biop.atlas.aligner.adapter.*;
import ch.epfl.biop.atlas.aligner.plugin.ExternalABBARegistrationPlugin;
import ch.epfl.biop.atlas.aligner.plugin.IABBARegistrationPlugin;
import ch.epfl.biop.atlas.aligner.plugin.RegistrationPluginHelper;
import ch.epfl.biop.registration.plugin.ExternalRegistrationPlugin;
import ch.epfl.biop.registration.plugin.IRegistrationPlugin;
import ch.epfl.biop.registration.plugin.RegistrationPluginHelper;
import ch.epfl.biop.atlas.struct.Atlas;
import ch.epfl.biop.registration.Registration;
import ch.epfl.biop.sourceandconverter.processor.*;
Expand Down Expand Up @@ -690,36 +690,6 @@ public static Map<String,String> convertToString(Context ctx, Map<String, Object
return convertedParams;
}

public void registerSelectedSlices(Command command,
SourcesProcessor preprocessFixed,
SourcesProcessor preprocessMoving) {
registerSelectedSlices(command,
preprocessFixed,
preprocessMoving,
new HashMap<>()
);
}

/**
* Main function which triggers registration of the selected slices
* @param command the ui command
* @param preprocessFixed how fixed sources need to be preprocessed before being registered
* @param preprocessMoving how moving sources need to be preprocessed before being registered
* @param parameters parameters used for the registration - all objects will be converted
* to String using the scijava {@link ConvertService}. They need to be strings
* to be serialized
*/
public void registerSelectedSlices(Command command,
SourcesProcessor preprocessFixed,
SourcesProcessor preprocessMoving,
Map<String,Object> parameters) {
registerSelectedSlices(RegistrationPluginHelper.registrationFromUI(scijavaCtx,command.getClass()),
preprocessFixed,
preprocessMoving,
parameters
);
}

/**
* Main function which triggers registration of the selected slices
* @param registrationClass the kind of registration which should be started
Expand All @@ -729,16 +699,16 @@ public void registerSelectedSlices(Command command,
* to String using the scijava {@link ConvertService}. They need to be strings
* to be serialized
*/
public void registerSelectedSlices(Class<? extends IABBARegistrationPlugin> registrationClass,
public void registerSelectedSlices(Class<? extends IRegistrationPlugin> registrationClass,
SourcesProcessor preprocessFixed,
SourcesProcessor preprocessMoving,
Map<String,Object> parameters) {

PluginService ps = scijavaCtx.getService(PluginService.class);
Supplier<? extends IABBARegistrationPlugin> pluginSupplier =
Supplier<? extends IRegistrationPlugin> pluginSupplier =
() -> {
try {
return (IABBARegistrationPlugin) ps.getPlugin(registrationClass).createInstance();
return (IRegistrationPlugin) ps.getPlugin(registrationClass).createInstance();
} catch (InstantiableException e) {
e.printStackTrace();
return null;
Expand Down Expand Up @@ -780,7 +750,7 @@ public void registerSelectedSlices(String registrationPluginName,
* to String using the scijava {@link ConvertService}. They need to be strings
* to be serialized
*/
public void registerSelectedSlices(Supplier<? extends IABBARegistrationPlugin> registrationPluginSupplier,
public void registerSelectedSlices(Supplier<? extends IRegistrationPlugin> registrationPluginSupplier,
SourcesProcessor preprocessFixed,
SourcesProcessor preprocessMoving,
Map<String,Object> parameters) {
Expand All @@ -797,14 +767,11 @@ public void registerSelectedSlices(Supplier<? extends IABBARegistrationPlugin> r

for (SliceSources slice : getSelectedSlices()) {
logger.debug("Starting slice registration for "+slice.getName());
IABBARegistrationPlugin registration = registrationPluginSupplier.get();
IRegistrationPlugin registration = registrationPluginSupplier.get();
if (registration!=null) {
logger.debug("\t slice registration for "+slice.getName()+"- set context");
registration.setScijavaContext(scijavaCtx);

logger.debug("\t slice registration for "+slice.getName()+"- setSliceInfo");
registration.setSliceInfo(new SliceInfo(this, slice));

// Sends parameters to the registration
logger.debug("\t slice registration for "+slice.getName()+"- setRegistrationParameters");
registration.setRegistrationParameters(convertToString(scijavaCtx, parameters));
Expand Down Expand Up @@ -915,15 +882,15 @@ public Gson getGsonStateSerializer(List<SourceAndConverter> serialized_sources)
PluginService pluginService = scijavaCtx.getService(PluginService.class);

// Creates adapter for all registration plugins
RegistrationAdapter registrationAdapter = new RegistrationAdapter(scijavaCtx, this);
pluginService.getPluginsOfType(IABBARegistrationPlugin.class).forEach(registrationPluginClass -> {
IABBARegistrationPlugin plugin = pluginService.createInstance(registrationPluginClass);
RegistrationAdapter registrationAdapter = new RegistrationAdapter(scijavaCtx);
pluginService.getPluginsOfType(IRegistrationPlugin.class).forEach(registrationPluginClass -> {
IRegistrationPlugin plugin = pluginService.createInstance(registrationPluginClass);
factoryRegistrations.registerSubtype(plugin.getClass());
gsonbuilder.registerTypeHierarchyAdapter(plugin.getClass(), registrationAdapter);
});

factoryRegistrations.registerSubtype(ExternalABBARegistrationPlugin.class);
gsonbuilder.registerTypeHierarchyAdapter(ExternalABBARegistrationPlugin.class, registrationAdapter);
factoryRegistrations.registerSubtype(ExternalRegistrationPlugin.class);
gsonbuilder.registerTypeHierarchyAdapter(ExternalRegistrationPlugin.class, registrationAdapter);

// For sources processor

Expand Down Expand Up @@ -1456,23 +1423,23 @@ public SliceInfo(MultiSlicePositioner mp, SliceSources slice) {

//---------------------- For PyImageJ extensions

static final Map<String, Supplier<? extends IABBARegistrationPlugin>> externalRegistrationPlugins = new HashMap<>();
static final Map<String, Supplier<? extends IRegistrationPlugin>> externalRegistrationPlugins = new HashMap<>();

/**
* Register an external registration plugin, for instance performed by a python function
* through PyImageJ
* @param name of the registration plugin
* @param pluginSupplier the thing that makes a new plugin of this kind
*/
public static void registerRegistrationPlugin(String name, Supplier<? extends IABBARegistrationPlugin> pluginSupplier) {
public static void registerRegistrationPlugin(String name, Supplier<? extends IRegistrationPlugin> pluginSupplier) {
externalRegistrationPlugins.put(name, pluginSupplier);
}

public static boolean isExternalRegistrationPlugin(String name) {
return externalRegistrationPlugins.containsKey(name);
}

public static Supplier<? extends IABBARegistrationPlugin> getExternalRegistrationPluginSupplier(String name) {
public static Supplier<? extends IRegistrationPlugin> getExternalRegistrationPluginSupplier(String name) {
return externalRegistrationPlugins.get(name);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.biop.atlas.aligner;

import bdv.viewer.SourceAndConverter;
import ch.epfl.biop.atlas.aligner.plugin.RegistrationPluginHelper;
import ch.epfl.biop.registration.plugin.RegistrationPluginHelper;
import ch.epfl.biop.registration.Registration;
import ch.epfl.biop.sourceandconverter.processor.SourcesProcessor;
import org.slf4j.Logger;
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/ch/epfl/biop/atlas/aligner/SliceSources.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import bdv.viewer.Interpolation;
import bdv.viewer.Source;
import bdv.viewer.SourceAndConverter;
import ch.epfl.biop.atlas.aligner.plugin.RegistrationPluginHelper;
import ch.epfl.biop.registration.plugin.RegistrationPluginHelper;
import ch.epfl.biop.atlas.mouse.allen.ccfv3.command.AllenBrainAdultMouseAtlasCCF2017Command;
import ch.epfl.biop.atlas.struct.AtlasHelper;
import ch.epfl.biop.atlas.struct.AtlasNode;
Expand Down Expand Up @@ -61,7 +61,6 @@
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.position.FunctionRandomAccessible;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.realtransform.BoundingBoxEstimation;
import net.imglib2.realtransform.InvertibleRealTransform;
import net.imglib2.realtransform.InvertibleRealTransformSequence;
import net.imglib2.realtransform.RealTransform;
Expand Down Expand Up @@ -113,10 +112,6 @@
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Collectors;

import static net.imglib2.realtransform.BoundingBoxEstimation.Method.CORNERS;
import static net.imglib2.realtransform.BoundingBoxEstimation.Method.FACES;
import static net.imglib2.realtransform.BoundingBoxEstimation.Method.VOLUME;


/**
* Class which contains the current registered SourceAndConverter array
Expand Down Expand Up @@ -1827,7 +1822,7 @@ public void popRasterSlice() {
registered_sacs = sourcesNonRasterized.pop();
}

public static class RegistrationAndSources {
private static class RegistrationAndSources {

final Registration reg;
final SourceAndConverter[] sacs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.epfl.biop.atlas.aligner.adapter;

import ch.epfl.biop.atlas.aligner.MultiSlicePositioner;
import ch.epfl.biop.atlas.aligner.plugin.ExternalABBARegistrationPlugin;
import ch.epfl.biop.registration.plugin.ExternalRegistrationPlugin;
import ch.epfl.biop.registration.Registration;
import com.google.gson.*;
import org.scijava.Context;
Expand All @@ -20,10 +20,8 @@ public class RegistrationAdapter implements JsonSerializer<Registration>,

final Context scijavacontext;

final MultiSlicePositioner msp;
public RegistrationAdapter(Context context, MultiSlicePositioner mp) {
public RegistrationAdapter(Context context) {
this.scijavacontext = context;
this.msp = mp;
}

@Override
Expand All @@ -32,7 +30,7 @@ public Registration deserialize(JsonElement json, Type typeOfT, JsonDeserializat
Registration registration;

logger.debug("Fetching registration plugin "+typeOfT.getTypeName());
if (typeOfT.getTypeName().equals(ExternalABBARegistrationPlugin.class.getName())) {
if (typeOfT.getTypeName().equals(ExternalRegistrationPlugin.class.getName())) {
String registrationTypeName = json
.getAsJsonObject()
.get("external_type")
Expand All @@ -50,7 +48,7 @@ public Registration deserialize(JsonElement json, Type typeOfT, JsonDeserializat
registration.setRegistrationParameters(context.deserialize(json.getAsJsonObject().get("parameters"), Map.class));
return registration;
} catch (InstantiableException e) {
msp.errlog.accept("Unrecognized registration plugin "+typeOfT.getTypeName());
logger.error("Unrecognized registration plugin "+typeOfT.getTypeName());
e.printStackTrace();
return null;
}
Expand All @@ -64,7 +62,7 @@ public JsonElement serialize(Registration registration, Type typeOfSrc, JsonSeri
logger.debug("With transform "+registration.getTransform());
logger.debug("And parameters "+registration.getRegistrationParameters());
if (MultiSlicePositioner.isExternalRegistrationPlugin(registration.getRegistrationTypeName())) {
obj.addProperty("type", ExternalABBARegistrationPlugin.class.getSimpleName());
obj.addProperty("type", ExternalRegistrationPlugin.class.getSimpleName());
obj.addProperty("external_type", registration.getRegistrationTypeName());
} else {
obj.addProperty("type", registration.getRegistrationTypeName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.epfl.biop.atlas.aligner.MultiSlicePositioner;
import ch.epfl.biop.atlas.aligner.SliceSources;
import ch.epfl.biop.atlas.aligner.action.MarkActionSequenceBatchAction;
import ch.epfl.biop.registration.sourceandconverter.mirror.MirrorXRegistration;
import ch.epfl.biop.sourceandconverter.processor.SourcesIdentity;
import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
Expand Down Expand Up @@ -38,7 +39,7 @@ public void run() {
parameters.put("mirror_side", mirror_side);

new MarkActionSequenceBatchAction(mp).runRequest();
mp.registerSelectedSlices(this,
mp.registerSelectedSlices(MirrorXRegistration.class,
new SourcesIdentity(),
new SourcesIdentity(),
parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package ch.epfl.biop.atlas.aligner.command;

import ch.epfl.biop.registration.sourceandconverter.bigwarp.SacBigWarp2DRegistration;
import org.scijava.command.Command;
import org.scijava.plugin.Plugin;

import java.util.HashMap;

@Plugin(type = Command.class,
menuPath = "Plugins>BIOP>Atlas>Multi Image To Atlas>Align>ABBA - BigWarp Registration",
description = "Uses BigWarp for in plane registration of selected slices")
public class RegisterSlicesBigWarpCommand extends RegistrationMultiChannelCommand {

public void runValidated() {
mp.registerSelectedSlices(this, getFixedFilter(), getMovingFilter());
mp.registerSelectedSlices(SacBigWarp2DRegistration.class, getFixedFilter(), getMovingFilter(), new HashMap<>());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import ch.epfl.biop.atlas.aligner.RegisterSliceAction;
import ch.epfl.biop.atlas.aligner.SliceSources;
import ch.epfl.biop.atlas.aligner.action.MarkActionSequenceBatchAction;
import ch.epfl.biop.atlas.aligner.plugin.IABBARegistrationPlugin;
import ch.epfl.biop.registration.plugin.IRegistrationPlugin;
import ch.epfl.biop.java.utilities.TempDirectory;
import ch.epfl.biop.quicknii.QuickNIIExporter;
import ch.epfl.biop.quicknii.QuickNIISeries;
Expand Down Expand Up @@ -392,11 +392,9 @@ protected void affineTransformInPlane(final List<SliceSources> slices, double n

//logger.debug("Slice QuickNii "+i+" correspond to initial slice "+iSliceSource);

IABBARegistrationPlugin registration = (IABBARegistrationPlugin)
IRegistrationPlugin registration = (IRegistrationPlugin)
pluginService.getPlugin(AffineRegistration.class).createInstance();
registration.setScijavaContext(ctx);

registration.setSliceInfo(new MultiSlicePositioner.SliceInfo(mp, slices.get(iSliceSource)));
Map<String,Object> parameters = new HashMap<>();

AffineTransform3D inPlaneTransform = new AffineTransform3D();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.epfl.biop.atlas.aligner.command;

import ch.epfl.biop.registration.sourceandconverter.affine.Elastix2DAffineRegistration;
import ch.epfl.biop.scijava.command.source.register.ElastixHelper;
import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
Expand Down Expand Up @@ -40,7 +41,7 @@ public void runValidated() {
parameters.put("background_offset_value_fixed", background_offset_value_fixed);
parameters.put("pxSizeInCurrentUnit", pixel_size_micrometer/1000.0);

mp.registerSelectedSlices(this,
mp.registerSelectedSlices(Elastix2DAffineRegistration.class,
getFixedFilter(),
getMovingFilter(),
parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.epfl.biop.atlas.aligner.command;

import ch.epfl.biop.registration.sourceandconverter.spline.Elastix2DSplineRegistration;
import ch.epfl.biop.scijava.command.source.register.ElastixHelper;
import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
Expand Down Expand Up @@ -51,7 +52,7 @@ public void runValidated() {
parameters.put("background_offset_value_fixed", background_offset_value_fixed);
parameters.put("pxSizeInCurrentUnit", pixel_size_micrometer/1000.0);

mp.registerSelectedSlices(this,
mp.registerSelectedSlices(Elastix2DSplineRegistration.class,
getFixedFilter(),
getMovingFilter(),
parameters);
Expand Down
Loading

0 comments on commit 2831d9a

Please sign in to comment.