Skip to content

Commit

Permalink
Refactor image anisotropy check to handle headless execution. (#313)
Browse files Browse the repository at this point in the history
* Refactor image anisotropy check to handle headless execution.

* Update Modern/wrapperPlugins/src/main/java/org/bonej/wrapperPlugins/wrapperUtils/Common.java

Co-authored-by: Alessandro Felder <alessandrofelder@users.noreply.github.com>
  • Loading branch information
mdoube and alessandrofelder authored Apr 19, 2022
1 parent 711af13 commit c828a1b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import org.joml.Vector3dc;
import org.scijava.app.StatusService;
import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;
Expand Down Expand Up @@ -111,6 +112,8 @@ public class FitEllipsoidWrapper extends BoneJCommand {
private StatusService statusService;
@Parameter
private UIService uiService;
@Parameter
private LogService logService;

private List<Vector3d> points;

Expand Down Expand Up @@ -180,7 +183,7 @@ private void validateImage() {
cancelMacroSafe(this, NOT_3D_IMAGE);
return;
}
if (!Common.warnAnisotropy(inputImage, uiService)) {
if (!Common.warnAnisotropy(inputImage, uiService, logService)) {
cancel(null);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,8 @@ private void imageValidater() {
}
// Without anisotropyWarned the warning is shown twice
if (!anisotropyWarned) {
if (uiService.isHeadless() && ImagePlusUtil.anisotropy(inputImage) < 1E-3) {
logService.warn("Image is anisotropic, results are likely to be wrong.");
}
else {
//warnAnisotropy needs to be refactored to remove dependence on UI
if (!Common.warnAnisotropy(inputImage, uiService)) {
cancel(null);
}
if (!Common.warnAnisotropy(inputImage, uiService, logService)) {
cancel(null);
}
anisotropyWarned = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
import org.scijava.ItemIO;
import org.scijava.app.StatusService;
import org.scijava.command.Command;
import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.ui.UIService;
Expand Down Expand Up @@ -124,6 +125,8 @@ public class ThicknessWrapper extends BoneJCommand {
@Parameter
private UIService uiService;
@Parameter
private LogService logService;
@Parameter
private StatusService statusService;

private boolean foreground;
Expand Down Expand Up @@ -257,7 +260,7 @@ private void validateImage() {
}

if (!anisotropyWarned) {
if (!Common.warnAnisotropy(inputImage, uiService)) {
if (!Common.warnAnisotropy(inputImage, uiService, logService)) {
cancel(null);
}
anisotropyWarned = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,22 +118,29 @@ public static <C extends ComplexType<C>> ImgPlus<BitType> toBitTypeImgPlus(

/**
* Shows a warning dialog about image anisotropy, and asks if the user wants
* to continue.
* to continue. If the plugin is running in headless mode, execution continues anyway, and a warning is printed to the log.
*
* @param image the current image open in ImageJ.
* @param uiService used to display the warning dialog.
* @return true if user chose OK_OPTION, or image is not anisotropic. False if
* user chose 'cancel' or they closed the dialog.
* @param logService handles the warning text if the UI is headless
* @return true if user chose OK_OPTION, or image is not anisotropic, or
* execution is headless. False if user chose 'cancel' or they closed the dialog.
*/
public static boolean warnAnisotropy(final ImagePlus image,
final UIService uiService)
final UIService uiService, final LogService logService)
{
final double anisotropy = ImagePlusUtil.anisotropy(image);
if (anisotropy < 1E-3) {
return true;
}
final String anisotropyPercent = String.format("(%.1f %%)", anisotropy *
100.0);
if (uiService.isHeadless()) {
logService.warn("The image is anisotropic " +
anisotropyPercent + ". Continuing anyway, but results "
+ "may be unreliable");
return true;
}
return uiService.showDialog("The image is anisotropic " +
anisotropyPercent + ". Continue anyway?", WARNING_MESSAGE,
OK_CANCEL_OPTION) == OK_OPTION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public void testWarnAnisotropyReturnsFalseIfAnisotropicImageAndUserCancels() {
final UIService uiService = mock(UIService.class);
when(uiService.showDialog(anyString(), any(MessageType.class), any()))
.thenReturn(CANCEL_OPTION);

assertFalse(Common.warnAnisotropy(imagePlus, uiService));
final LogService logService = mock(LogService.class);
assertFalse(Common.warnAnisotropy(imagePlus, uiService, logService));
verify(uiService, timeout(1000)).showDialog(anyString(), any(
MessageType.class), any());
}
Expand All @@ -171,7 +171,8 @@ public void testWarnAnisotropyReturnsFalseIfAnisotropicImageAndUserCloses() {
when(uiService.showDialog(anyString(), any(MessageType.class), any()))
.thenReturn(CLOSED_OPTION);

assertFalse(Common.warnAnisotropy(imagePlus, uiService));
final LogService logService = mock(LogService.class);
assertFalse(Common.warnAnisotropy(imagePlus, uiService, logService));
verify(uiService, timeout(1000)).showDialog(anyString(), any(
MessageType.class), any());
}
Expand All @@ -187,7 +188,8 @@ public void testWarnAnisotropyReturnsTrueIfAnisotropicImageAndUserOK() {
when(uiService.showDialog(anyString(), any(MessageType.class), any()))
.thenReturn(OK_OPTION);

assertTrue(Common.warnAnisotropy(imagePlus, uiService));
final LogService logService = mock(LogService.class);
assertTrue(Common.warnAnisotropy(imagePlus, uiService, logService));
verify(uiService, timeout(1000)).showDialog(anyString(), any(
MessageType.class), any());
}
Expand All @@ -200,17 +202,23 @@ public void testWarnAnisotropyReturnsTrueIfIsotropicImage() {
when(imagePlus.getCalibration()).thenReturn(isotropic);
final UIService uiService = mock(UIService.class);

assertTrue(Common.warnAnisotropy(imagePlus, uiService));
final LogService logService = mock(LogService.class);
assertTrue(Common.warnAnisotropy(imagePlus, uiService, logService));
}

@Test(expected = NullPointerException.class)
public void testWarnAnisotropyThrowsNPEIfImageNull() {
Common.warnAnisotropy(null, mock(UIService.class));
Common.warnAnisotropy(null, mock(UIService.class), mock(LogService.class));
}

@Test(expected = NullPointerException.class)
public void testWarnAnisotropyThrowsNPEIfUIServiceNull() {
Common.warnAnisotropy(mock(ImagePlus.class), null);
Common.warnAnisotropy(mock(ImagePlus.class), null, mock(LogService.class));
}

@Test(expected = NullPointerException.class)
public void testWarnAnisotropyThrowsNPEIfLogServiceNull() {
Common.warnAnisotropy(mock(ImagePlus.class), mock(UIService.class), null);
}

@Test
Expand Down

0 comments on commit c828a1b

Please # to comment.