Skip to content

Commit

Permalink
Rearrange Functions and Implement Better Logging
Browse files Browse the repository at this point in the history
Rearranged all of the functions that are put within classes that aren't part of the same theme. Also improve the logging capabilities that is available for the plugin, having it properly generate loggers and work within CI.
  • Loading branch information
AvocadoMoon committed Apr 25, 2024
1 parent 4cb7f4f commit 1091191
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 96 deletions.
14 changes: 12 additions & 2 deletions view-simulation-results/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@
<dependency>
<groupId>org.janelia.saalfeldlab</groupId>
<artifactId>n5-imglib2</artifactId>
<version>6.0.0</version>
<version>7.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.janelia.saalfeldlab/n5-aws-s3 -->
<dependency>
<groupId>org.janelia.saalfeldlab</groupId>
<artifactId>n5-aws-s3</artifactId>
<version>4.0.0</version>
<version>4.1.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.janelia.saalfeldlab/n5-universe -->
Expand Down Expand Up @@ -182,6 +182,16 @@
<artifactId>scijava-log-slf4j</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>


</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,24 @@ public void run() {
exportTable = new N5ExportTable();
initializeLogService();
// N5ImageHandler.logService.setLevel(LogService.DEBUG);
N5ExportTable exportTable = new N5ExportTable(this);
if(N5ImageHandler.logService == null){
N5ImageHandler.logService = new SLF4JLogService();
}
N5ImageHandler.logService.setLevel(LogService.DEBUG);
exportTable.displayExportTable();
}

public void displayN5Dataset(ImagePlus imagePlus) throws IOException {
if (this.vGui.openMemoryCheckBox.isSelected()){
ImagePlus memoryImagePlus = new Duplicator().run(imagePlus);
memoryImagePlus.show();
}
else{
imagePlus.show();
}
public static Logger getLogger(Class classToLog){
return logService.subLogger(classToLog.getCanonicalName(), LogService.DEBUG);
}

public static LogService getLogger(){
if (logService == null){
logService = new SLF4JLogService();
public static void initializeLogService(){
if (N5ImageHandler.logService == null){
N5ImageHandler.logService = new SLF4JLogService();
N5ImageHandler.logService.initialize();
}
return logService;
}

public static void main(String[] args) {
N5ImageHandler n5ImageHandler = new N5ImageHandler();
initializeLogService();
n5ImageHandler.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.AmazonS3URI;
import com.google.gson.GsonBuilder;
import ij.ImagePlus;
import ij.plugin.Duplicator;
import net.imglib2.cache.img.CachedCellImg;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.numeric.real.DoubleType;
import org.janelia.saalfeldlab.n5.N5FSReader;
import org.janelia.saalfeldlab.n5.N5KeyValueReader;
import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.imglib2.N5Utils;
import org.janelia.saalfeldlab.n5.s3.AmazonS3KeyValueAccess;
import org.janelia.saalfeldlab.n5.s3.N5AmazonS3Reader;
import org.scijava.log.LogService;
import org.scijava.log.Logger;
import org.vcell.N5.UI.N5ExportTable;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
Expand All @@ -35,7 +42,7 @@ public class SimResultsLoader {
private String dataSetChosen;
private String userSetFileName;

private final LogService logService = N5ImageHandler.getLogger();
private static final Logger logger = N5ImageHandler.getLogger(SimResultsLoader.class);

public SimResultsLoader(){

Expand All @@ -48,7 +55,7 @@ public SimResultsLoader(String stringURI, String userSetFileName){
}

public void createS3Client(String url, HashMap<String, String> credentials, HashMap<String, String> endpoint){
logService.debug("Creating S3 Client");
logger.debug("Creating S3 Client");
AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard();
URI uri = URI.create(url);

Expand Down Expand Up @@ -76,7 +83,7 @@ public void createS3Client(String url, HashMap<String, String> credentials, Hash
}
//creds not null, but region is
this.s3Client = s3ClientBuilder.withRegion(s3URI.getRegion()).build();
logService.debug("Created S3 Client With Modern URL");
logger.debug("Created S3 Client With Modern URL");
}
// otherwise assume it is one of our URLs
// http://vcell:8000/bucket/object/object2
Expand All @@ -87,7 +94,7 @@ public void createS3Client(String url, HashMap<String, String> credentials, Hash
s3ClientBuilder.withPathStyleAccessEnabled(true);
s3ClientBuilder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(uri.getScheme() + "://" + uri.getAuthority(), "site2-low"));
this.s3Client = s3ClientBuilder.build();
logService.debug("Created S3 Client With Legacy URL");
logger.debug("Created S3 Client With Legacy URL");
}
}
//When creating client's try to make one for an Amazon link, otherwise use our custom url scheme
Expand All @@ -106,7 +113,7 @@ public ArrayList<String> getS3N5DatasetList() throws IOException {

public ArrayList<String> getN5DatasetList() throws IOException {
// auto closes reader
logService.debug("Getting List of N5 Datasets");
logger.debug("Getting List of N5 Datasets");
try (N5FSReader n5Reader = new N5FSReader(selectedLocalFile.getPath())) {
String[] metaList = n5Reader.deepList("/");
ArrayList<String> fList = new ArrayList<>();
Expand All @@ -115,7 +122,7 @@ public ArrayList<String> getN5DatasetList() throws IOException {
fList.add(s);
};
}
logService.debug("Got List of N5 Datasets");
logger.debug("Got List of N5 Datasets");
return fList;
}
}
Expand All @@ -130,13 +137,15 @@ public ImagePlus getImgPlusFromLocalN5File() throws IOException {
}

public ImagePlus getImgPlusFromN5File() throws IOException {
// AmazonS3KeyValueAccess amazonS3KeyValueAccess = new AmazonS3KeyValueAccess(s3Client, bucketName, false);
// N5KeyValueReader n5KeyValueReader = new N5KeyValueReader(amazonS3KeyValueAccess, s3ObjectKey, new GsonBuilder(), true);
AmazonS3KeyValueAccess amazonS3KeyValueAccess = new AmazonS3KeyValueAccess(s3Client, bucketName, false);
N5KeyValueReader n5AmazonS3Reader = new N5KeyValueReader(amazonS3KeyValueAccess, s3ObjectKey, new GsonBuilder(), true);

logService.debug("Reading N5 File Into ImageJ ImagePlus Class");
N5AmazonS3Reader n5AmazonS3Reader = new N5AmazonS3Reader(s3Client, bucketName, "/" + s3ObjectKey);
// N5AmazonS3Reader n5AmazonS3Reader = new N5AmazonS3Reader(s3Client, bucketName, "/" + s3ObjectKey);
long start = System.currentTimeMillis();
logger.debug("Reading N5 File Into Virtual Image");
ImagePlus imagePlus = ImageJFunctions.wrap((CachedCellImg<DoubleType, ?>) N5Utils.open(n5AmazonS3Reader, dataSetChosen), userSetFileName);
logService.debug("Read N5 File Into ImageJ");
long end = System.currentTimeMillis();
logger.debug("Read N5 File Into ImageJ taking: " + ((end - start) / 1000) + "s");
return imagePlus;
}

Expand All @@ -155,4 +164,36 @@ public void setURI(URI uri){
public void setDataSetChosen(String dataSetChosen) {
this.dataSetChosen = dataSetChosen;
}

public static void openN5FileDataset(ArrayList<SimResultsLoader> filesToOpen, boolean openInMemory){
N5ExportTable.enableCriticalButtons(false);
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Thread openN5FileDataset = new Thread(() -> {
try{
for(SimResultsLoader simResultsLoader: filesToOpen){
simResultsLoader.createS3Client();
ImagePlus imagePlus = simResultsLoader.getImgPlusFromN5File();
if(openInMemory){
long start = System.currentTimeMillis();
logger.debug("Loading Virtual N5 File Into Memory");
imagePlus = new Duplicator().run(imagePlus);
long end = System.currentTimeMillis();
logger.debug("Loaded Virtual N5 File Into Memory taking: " + ((end - start)/ 1000) + "s");
}
imagePlus.show();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
N5ExportTable.exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
N5ExportTable.enableCriticalButtons(true);
}
});
}
});
openN5FileDataset.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.google.gson.GsonBuilder;
import ij.ImagePlus;
import ij.plugin.Duplicator;
import org.scijava.log.LogService;
import org.scijava.log.Logger;
import org.vcell.N5.ExportDataRepresentation;
import org.vcell.N5.N5ImageHandler;
import org.vcell.N5.SimResultsLoader;
Expand Down Expand Up @@ -32,30 +32,30 @@
import java.util.Stack;

public class N5ExportTable implements ActionListener, ListSelectionListener {
private JDialog exportTableDialog;
public static JDialog exportTableDialog;
private N5ExportTableModel n5ExportTableModel;
private ParameterTableModel parameterTableModel;
private JTable parameterTable;
private JTable exportListTable;

private JButton open;
private JButton copyLink;
private JButton refreshButton;
private JButton useN5Link;
private JButton questionMark;
private JCheckBox openInMemory;
private static JButton open;
private static JButton copyLink;
private static JButton refreshButton;
private static JButton useN5Link;
private static JButton questionMark;
public static JCheckBox openInMemory;
private JCheckBox todayInterval;
private JCheckBox monthInterval;
private JCheckBox yearlyInterval;
private JCheckBox anyInterval;
private JTextPane variableTextPanel;
private Border lowerEtchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
private RemoteFileSelection remoteFileSelection;
private final Border lowerEtchedBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
private static RemoteFileSelection remoteFileSelection;
private final int paneWidth = 800;

private final LogService logService = N5ImageHandler.getLogger();
private final Logger logger = N5ImageHandler.getLogger(N5ExportTable.class);

public N5ExportTable(N5ImageHandler n5ImageHandler){
public N5ExportTable(){
remoteFileSelection = new RemoteFileSelection();
}

Expand Down Expand Up @@ -230,47 +230,14 @@ private JPanel topPanel(){
return topBar;
}

public void enableCriticalButtons(boolean enable){
public static void enableCriticalButtons(boolean enable){
useN5Link.setEnabled(enable);
open.setEnabled(enable);
refreshButton.setEnabled(enable);
copyLink.setEnabled(enable);
remoteFileSelection.submitS3Info.setEnabled(enable);
}

public void openN5FileDataset(ArrayList<SimResultsLoader> filesToOpen, boolean openInMemory){
enableCriticalButtons(false);
exportTableDialog.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Thread openN5FileDataset = new Thread(() -> {
enableCriticalButtons(false);
try{
for(SimResultsLoader simResultsLoader: filesToOpen){
logService.debug("Creating S3 Client from Table");
simResultsLoader.createS3Client();
ImagePlus imagePlus = simResultsLoader.getImgPlusFromN5File();
logService.debug("Got ImagePlus in Table");
if(openInMemory){
logService.debug("Loading Image Into Memory");
imagePlus = new Duplicator().run(imagePlus);
logService.debug("Loaded Image Into Memory");
}
imagePlus.show();
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
exportTableDialog.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
enableCriticalButtons(true);
}
});
}
});
openN5FileDataset.start();
}

@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(open)){
Expand All @@ -280,7 +247,7 @@ public void actionPerformed(ActionEvent e) {
SimResultsLoader simResultsLoader = new SimResultsLoader(uri, n5ExportTableModel.getRowData(row).savedFileName);
filesToOpen.add(simResultsLoader);
}
openN5FileDataset(filesToOpen, openInMemory.isSelected());
SimResultsLoader.openN5FileDataset(filesToOpen, openInMemory.isSelected());
} else if (e.getSource().equals(copyLink)) {
ExportDataRepresentation.SimulationExportDataRepresentation selectedRow = n5ExportTableModel.getRowData(exportListTable.getSelectedRow());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Expand All @@ -291,10 +258,6 @@ public void actionPerformed(ActionEvent e) {
new HelpExplanation().displayHelpMenu();
} else if (e.getSource().equals(useN5Link)) {
remoteFileSelection.setVisible(true);
} else if (e.getSource().equals(remoteFileSelection.submitS3Info)) {
SimResultsLoader simResultsLoader = new SimResultsLoader(remoteFileSelection.getS3URL(), "");
openN5FileDataset(new ArrayList<SimResultsLoader>(){{add(simResultsLoader);}}, openInMemory.isSelected());
remoteFileSelection.setVisible(false);
}
}

Expand Down
Loading

0 comments on commit 1091191

Please sign in to comment.