Skip to content
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

Alpha feedback #22

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ public static boolean verifyOutputDirectoryIsEmpty(String potential) {
return true;
}

YesNoCancelDialog dialog = new YesNoCancelDialog(
YesNoDialog dialog = new YesNoDialog(
IJ.getInstance(),
"Output Directory Not Empty",
"The output directory is not empty. Continue and overwrite?",
"The output directory is not empty and this is currently a requirement for the plugin. Would you like to continue and overwrite? If you are unsure, simply make a new directory when selecting your output directory!",
"Delete Contents", "Cancel");
if (dialog.cancelPressed()) {
if (!dialog.yesPressed()) {
return false;
}

Expand Down
59 changes: 59 additions & 0 deletions src/main/java/uk/ac/franciscrickinstitute/twombli/Outputs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package uk.ac.franciscrickinstitute.twombli;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;

public class Outputs {

public static void generateSummaries(Path twombli_csv_path, double alignment, int dimension, Path anamorfSummaryPath, Path hdmSummaryPath, Path gapAnalysisPath, boolean doHeader, boolean gapAnalysis, Path gap_csv_path) {
// Write to our twombli summary
try {
List<String> lines = new ArrayList<>();
List<String> anamorfEntries = Files.readAllLines(anamorfSummaryPath);
List<String> hdmEntries = Files.readAllLines(hdmSummaryPath);

// Conditionally write out our header
if (doHeader) {
String headerItems = anamorfEntries.get(0);
String[] hdmHeaderItems = hdmEntries.get(0).split(",");
String header = headerItems + "," + hdmHeaderItems[hdmHeaderItems.length - 1] + ",Alignment (Coherency [%]),Size";
lines.add(header);
}

// Get the data
String anamorfData = anamorfEntries.get(anamorfEntries.size() - 1);
String[] hdmData = hdmEntries.get(hdmEntries.size() - 1).split(",");
double hdmValue = 1 - Double.parseDouble(hdmData[hdmData.length - 1]);
lines.add(anamorfData + "," + hdmValue + "," + alignment + "," + dimension);

// Write
Files.write(twombli_csv_path, lines, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
}

catch (IOException e) {
throw new RuntimeException(e);
}

// Write to our gap analysis summary
if (gapAnalysis) {
try (BufferedReader reader = Files.newBufferedReader(gapAnalysisPath);
BufferedWriter writer = Files.newBufferedWriter(gap_csv_path, StandardOpenOption.CREATE, StandardOpenOption.APPEND)) {

String line;
while ((line = reader.readLine()) != null) {
writer.write(line.replace(" ", ","));
writer.newLine(); // Ensure proper newline after each row
}

} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.ac.franciscrickinstitute.twombli;

public interface ProgressCancelListener {
void handleProgressBarCancelled();
}
110 changes: 110 additions & 0 deletions src/main/java/uk/ac/franciscrickinstitute/twombli/ProgressDialog.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package uk.ac.franciscrickinstitute.twombli;

import java.awt.*;
import java.awt.event.*;

import ij.gui.GUI;

public class ProgressDialog extends Dialog implements WindowListener {

private final int maxProgress;
private final ProgressCanvas progressCanvas;
private final ProgressCancelListener progressCancelListener;

public ProgressDialog(Frame parent, String title, int maxProgress, ProgressCancelListener listener) {
super(parent, title, true);
this.maxProgress = maxProgress;
this.progressCancelListener = listener;

this.setModal(false);

// Create a canvas to draw the progress bar
this.progressCanvas = new ProgressCanvas(this.maxProgress);
this.progressCanvas.setSize(300, 30);

// Set up the dialog layout
setLayout(new BorderLayout());
add(this.progressCanvas, BorderLayout.CENTER);

// Create a cancel button
Button cancelButton = new Button("Cancel");
cancelButton.addActionListener(e -> this.handleProgressBarCancelled());
add(cancelButton, BorderLayout.SOUTH);

GUI.scale(this);
pack();
GUI.centerOnImageJScreen(this);
this.setVisible(true);
}

// Method to update the progress bar
public void updateProgress(int newProgress) {
this.progressCanvas.setProgress(newProgress);
}

private void handleProgressBarCancelled() {
this.progressCancelListener.handleProgressBarCancelled();
this.dispose();
}

@Override
public void windowOpened(WindowEvent e) {}

@Override
public void windowClosing(WindowEvent e) {}

@Override
public void windowClosed(WindowEvent e) {
this.handleProgressBarCancelled();
}

@Override
public void windowIconified(WindowEvent e) {}

@Override
public void windowDeiconified(WindowEvent e) {}

@Override
public void windowActivated(WindowEvent e) {}

@Override
public void windowDeactivated(WindowEvent e) {}

// Canvas for drawing the progress bar
private class ProgressCanvas extends Canvas {
private final int maxProgress;
private int currentProgress = 0;

public ProgressCanvas(int maxProgress) {
this.maxProgress = maxProgress;
this.currentProgress = 0;
}

public void setProgress(int progress) {
this.currentProgress = progress;
repaint();
}

@Override
public void paint(Graphics g) {
// Clear the canvas
g.setColor(Color.WHITE);
g.fillRect(0, 0, getWidth(), getHeight());

// Draw the progress bar background
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());

// Draw the progress based on the current value
g.setColor(Color.GREEN);
int barWidth = (int) ((getWidth() * currentProgress) / (float) maxProgress);
g.fillRect(0, 0, barWidth, getHeight());

// Draw the progress percentage
g.setColor(Color.BLACK);
int percent = (int) ((currentProgress / (float) maxProgress) * 100);
String progressText = percent + "%";
g.drawString(progressText, getWidth() / 2 - g.getFontMetrics().stringWidth(progressText) / 2, getHeight() / 2 + 5);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package uk.ac.franciscrickinstitute.twombli;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down Expand Up @@ -143,49 +145,12 @@ private void generateSummaries() {
// Gather our basic info
String filePrefix = runner.filePrefix;
double alignment = runner.alignment;
double dimension = runner.dimension;
int dimension = runner.dimension;
Path anamorfSummaryPath = Paths.get(this.outputPath, "masks", filePrefix + "_results.csv");
Path hdmSummaryPath = Paths.get(this.outputPath, "hdm_csvs", filePrefix + "_ResultsHDM.csv");
Path gapAnalysisPath = Paths.get(this.outputPath, "gap_analysis", filePrefix + "_gaps.csv");

// Write to our twombli summary
try {
List<String> lines = new ArrayList<>();
List<String> anamorfEntries = Files.readAllLines(anamorfSummaryPath);
List<String> hdmEntries = Files.readAllLines(hdmSummaryPath);

// Conditionally write out our header
if (doHeader) {
String headerItems = anamorfEntries.get(0);
String[] hdmHeaderItems = hdmEntries.get(0).split(",");
String header = headerItems + "," + hdmHeaderItems[hdmHeaderItems.length - 1] + ",Alignment (Coherency [%]),Size";
lines.add(header);
doHeader = false;
}

// Get the data
String anamorfData = anamorfEntries.get(anamorfEntries.size() - 1);
String[] hdmData = hdmEntries.get(hdmEntries.size() - 1).split(",");
double hdmValue = 1 - Double.parseDouble(hdmData[hdmData.length - 1]);
lines.add(anamorfData + "," + hdmValue + "," + alignment + "," + dimension);

// Write
Files.write(twombliOutputPath, lines, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
}

catch (IOException e) {
throw new RuntimeException(e);
}

// Write to our gap analysis summary
if (this.performGapAnalysis) {
try {
List<String> lines = Files.readAllLines(gapAnalysisPath);
Files.write(gapsOutputPath, lines, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Outputs.generateSummaries(twombliOutputPath, alignment, dimension, anamorfSummaryPath, hdmSummaryPath, gapAnalysisPath, doHeader, this.performGapAnalysis, gapsOutputPath);
doHeader = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.ac.franciscrickinstitute.twombli;

import javax.swing.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,21 @@ public class TWOMBLIRunner implements Command {
@Parameter
public int minimumGapDiameter = 0;

@Parameter(type = ItemIO.OUTPUT)
public ImagePlus output;

@Parameter(type = ItemIO.OUTPUT)
@Parameter(type=ItemIO.OUTPUT)
public double alignment;

@Parameter(type = ItemIO.OUTPUT)
@Parameter(type=ItemIO.OUTPUT)
public int dimension;

@Parameter(type=ItemIO.OUTPUT)
public ImagePlus maskImage;

@Parameter(type=ItemIO.OUTPUT)
public ImagePlus hdmImage;

@Parameter(type=ItemIO.OUTPUT)
public ImagePlus gapImage;

// Magic number declarations
private static final double HIGH_CONTRAST_THRESHOLD = 120.0;
private static final double LOW_CONTRAST_THRESHOLD = 0.0;
Expand Down Expand Up @@ -188,16 +194,14 @@ public void run() {
ImagePlus maskImage = IJ.openImage(new File(finalMaskImagePath).getAbsolutePath());
this.runOrientationJ(maskImage);

// Close non-essential windows
// this.closeNonImages();

// Gap analysis
maskImage = IJ.openImage(new File(finalMaskImagePath).getAbsolutePath());
this.performGapAnalysis(gapAnalysisDirectory, maskImage);

// Output handling
// this.closeNonImages();
this.output = maskImage;
this.maskImage = maskImage;
this.hdmImage = IJ.openImage(hdmDirectory + File.separator + this.filePrefix + "_hdm.png");
this.gapImage = IJ.openImage(gapAnalysisDirectory + File.separator + this.filePrefix + "_gap.png");
}

private void detectRidges(ImagePlus inputImage, String maskImage) {
Expand Down Expand Up @@ -317,7 +321,9 @@ private void runAnamorf(String maskImageDirectory) {
File propsFile = new File(this.anamorfPropertiesFile);
props.loadFromXML(Files.newInputStream(propsFile.toPath()));
}
} catch (IOException ex) {
}

catch (IOException ex) {
ex.printStackTrace();
return;
}
Expand Down Expand Up @@ -468,18 +474,20 @@ private void performGapAnalysis(String gapAnalysisDirectory, ImagePlus maskImage
File individualGapAnalysisFile = new File(individualGapAnalysisFilePath);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(individualGapAnalysisFile))) {
bw.write(this.filePrefix + " " + mean + " " + standardDeviation + " " + fivePercentile + " " + fiftyPercentile + " " + ninetyFivePercentile);
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}

// Write the array to file
String individualGapAnalysisArrayFilePath = gapAnalysisDirectory + File.separator + this.filePrefix + "_area_arrays.csv";
String individualGapAnalysisArrayFilePath = gapAnalysisDirectory + File.separator + this.filePrefix + "_area_arrays.csv";
File individualGapAnalysisArrayFile = new File(individualGapAnalysisArrayFilePath);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(individualGapAnalysisArrayFile))) {
for (double area : areas) {
bw.write(area + "\n");
}
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
WindowManager.setTempCurrentImage(null);
Expand Down
Loading
Loading