Skip to content

Commit

Permalink
Fix DeepSlice wrapper - exposes better its API (breaking for DeepSlice)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoKiaru committed Jan 8, 2024
1 parent 3778781 commit c98020d
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 84 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ On top of this, some converters that facilite the transmission of data to these
* [I.A. Cellpose Virtual Environment](#ia-cellpose-virtual-environment)
* [I.A.2. Conda installation](#ia2-conda-installation)
* [I.A.2.a. Windows](#ia2a-windows)
* [Enable conda command outside conda prompt](#enable-conda-command-outside-conda-prompt)
* [Conda cellpose-GPU](#conda-cellpose-gpu)
* [I.A.2.b. Mac](#ia2b-mac)
* [I.A.2.c. Linux](#ia2c-linux)
* [I.B. Fiji - Cellpose wrapper](#ib-fiji---cellpose-wrapper)
Expand All @@ -28,13 +30,16 @@ On top of this, some converters that facilite the transmission of data to these
* [I.A.1. More on venv installation](#ia1-more-on-venv-installation)
* [I.A.2. More on conda installation](#ia2-more-on-conda-installation)
* [I.A.2.a. Windows](#ia2a-windows-1)
* [Enable conda command outside conda prompt](#enable-conda-command-outside-conda-prompt-1)
* [Conda StarDist-GPU](#conda-stardist-gpu)
* [I.A.2.b. MAC OSX](#ia2b-mac-osx)
* [I.B. Fiji - StarDist3D wrapper](#ib-fiji---stardist3d-wrapper)
* [II. Using Fiji - StarDist3D wrapper](#ii-using-fiji---stardist3d-wrapper)
* [DeepSlice](#deepslice)
* [I. Installation](#i-installation-2)
* [I.A. Conda installation](#ia-conda-installation)
* [I.A.2.a. Windows](#ia2a-windows-2)
* [Enable conda command outside conda prompt](#enable-conda-command-outside-conda-prompt-2)
* [I.A.2.b. Mac](#ia2b-mac-1)
* [I.A.2.c. Linux](#ia2c-linux-1)
* [II. Using Fiji - DeepSlice wrapper](#ii-using-fiji---deepslice-wrapper)
Expand Down Expand Up @@ -300,9 +305,16 @@ You need to install DeepSlice in a conda env that has python 3.7 (versions above
```
conda create --name deep_slice python=3.7
conda activate deep_slice
pip install DeepSlice
pip install deepslice==1.1.5
```

If you don't want to pin a deepslice version, replace the last line by:

```
pip install deepslice
```


##### I.A.2.a. Windows
**NOTE** : if you rely on conda, the DeepSlice wrapper requires to enable the conda command outside of conda prompt, [_cf_ installation instructions](#enable-conda-command-outside-conda-prompt).

Expand Down
98 changes: 36 additions & 62 deletions src/main/java/ch/epfl/biop/wrappers/deepslice/DeepSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
Expand All @@ -26,23 +27,17 @@ public class DeepSlice {
public static String keyPrefix = DeepSlice.class.getName() + ".";

static String defaultEnvDirPath = "C:/Users/username/.conda/envs/deepslice";//"E:/conda-envs/CellPoseGPU3";
static String defaultEnvType = "conda";
static String defaultVersion = "1.1.5";
static String defaultVersion = "1.1.5.1";

public static String envDirPath = Prefs.get(keyPrefix + "envDirPath", defaultEnvDirPath);
public static String envType = Prefs.get(keyPrefix + "envType", defaultEnvType);

public static String version = Prefs.get(keyPrefix + "version", defaultVersion);

public static void setEnvDirPath(File f) {
DeepSlice.envDirPath = f.getAbsolutePath();
Prefs.set(keyPrefix + "envDirPath", envDirPath);
}

public static void setEnvType(String envType) {
DeepSlice.envType = envType;
Prefs.set(keyPrefix + "envType", envType);
}

public static void setVersion(String version) {
DeepSlice.version = version; // RAAAH!
Prefs.set(keyPrefix + "version", version);
Expand Down Expand Up @@ -99,70 +94,49 @@ static void execute(List<String> options, Consumer<InputStream> outputHandler) t
}

List<String> cmd = new ArrayList<>();
List<String> start_cmd = null ;

// Get the prefs about the env type
String envType = Prefs.get(keyPrefix + "envType", DeepSlice.envType);
List<String> start_cmd;

// start terminal
if (IJ.isWindows()) {
start_cmd= Arrays.asList("cmd.exe", "/C");
} else if ( IJ.isMacOSX() || IJ.isLinux()) {
start_cmd = Arrays.asList("bash", "-c");
} else {
IJ.error("OS unrecognized!!");
start_cmd = Collections.singletonList("");
}
cmd.addAll( start_cmd );

List<String> conda_activate_cmd;

// Depending of the env type
if (envType.equals("conda")) {
List<String> conda_activate_cmd;

if (IJ.isWindows()) {
// Activate the conda env
conda_activate_cmd = Arrays.asList("CALL", Conda.getWindowsCondaCommand(), "activate", envDirPath);
cmd.addAll(conda_activate_cmd);
// After starting the env we can now use deepslice
cmd.add("&");// to have a second command
List<String> cellpose_args_cmd = Arrays.asList("python", "-Xutf8", getDeepSliceCLIScriptPath());
cmd.addAll(cellpose_args_cmd);
// input options
cmd.addAll(options);

} else if ( IJ.isMacOSX() || IJ.isLinux()) {
// instead of conda activate (so much headache!!!) specify the python to use
String python_path = envDirPath+separatorChar+"bin"+separatorChar+"python";
List<String> cellpose_args_cmd = new ArrayList<>(Arrays.asList( python_path , getDeepSliceCLIScriptPath()));
cellpose_args_cmd.addAll(options);

// convert to a string
cellpose_args_cmd = cellpose_args_cmd.stream().map(s -> {
if (s.trim().contains(" "))
return "\"" + s.trim() + "\"";
return s;
}).collect(Collectors.toList());
// The last part needs to be sent as a single string, otherwise it does not run
String cmdString = cellpose_args_cmd.toString().replace(",","");

// finally add to cmd
cmd.add(cmdString.substring(1, cmdString.length()-1));
}

} else if (envType.equals("venv")) { // venv

if (IJ.isWindows()) {
List<String> venv_activate_cmd = Arrays.asList(new File(envDirPath, "Scripts/activate").toString());
cmd.addAll(venv_activate_cmd);
cmd.add("&");// to have a second command
List<String> cellpose_args_cmd = Arrays.asList("python", "-Xutf8", getDeepSliceCLIScriptPath());
cmd.addAll(cellpose_args_cmd);
cmd.addAll(options);

} else if ( IJ.isMacOSX() || IJ.isLinux()) {
throw new UnsupportedOperationException("Mac/Unix not supported yet with virtual environment. Please try conda instead.");
}
if (IJ.isWindows()) {
// Activate the conda env
conda_activate_cmd = Arrays.asList("CALL", Conda.getWindowsCondaCommand(), "activate", envDirPath);
cmd.addAll(conda_activate_cmd);
// After starting the env we can now use deepslice
cmd.add("&");// to have a second command
List<String> cellpose_args_cmd = Arrays.asList("python", "-Xutf8", getDeepSliceCLIScriptPath());
cmd.addAll(cellpose_args_cmd);
// input options
cmd.addAll(options);

} else {
throw new UnsupportedOperationException("Virtual env type unrecognized!");
} else if ( IJ.isMacOSX() || IJ.isLinux()) {
// instead of conda activate (so much headache!!!) specify the python to use
String python_path = envDirPath+separatorChar+"bin"+separatorChar+"python";
List<String> cellpose_args_cmd = new ArrayList<>(Arrays.asList( python_path , getDeepSliceCLIScriptPath()));
cellpose_args_cmd.addAll(options);

// convert to a string
cellpose_args_cmd = cellpose_args_cmd.stream().map(s -> {
if (s.trim().contains(" "))
return "\"" + s.trim() + "\"";
return s;
}).collect(Collectors.toList());
// The last part needs to be sent as a single string, otherwise it does not run
String cmdString = cellpose_args_cmd.toString().replace(",","");

// finally add to cmd
cmd.add(cmdString.substring(1, cmdString.length()-1));
}

System.out.println(cmd.toString().replace(",", ""));
Expand Down Expand Up @@ -192,7 +166,7 @@ public void run() {
if (exitValue != 0) {
System.out.println("Runner " + envDirPath + " exited with value " + exitValue + ". Please check output above for indications of the problem.");
} else {
System.out.println(envType + " , " + envDirPath + " run finished");
System.out.println("conda , " + envDirPath + " run finished");
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
public class DeepSliceTaskSettings {
public String model;
public String input_folder, output_folder;
public boolean ensemble, section_numbers, propagate_angles, enforce_index_order;
public int enforce_index_spacing;
public boolean ensemble, section_numbers, propagate_angles;
public boolean enforce_index_order;
public boolean use_enforce_index_spacing;
public String enforce_index_spacing;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public void run() throws Exception {
if (settings.section_numbers) options.add("--section_numbers");
if (settings.propagate_angles) options.add("--propagate_angles");
if (settings.enforce_index_order) options.add("--enforce_index_order");
if (settings.enforce_index_spacing>0) {
if (settings.use_enforce_index_spacing) {
options.add("--enforce_index_spacing");
options.add(Integer.toString(settings.enforce_index_spacing));
options.add(settings.enforce_index_spacing);
}

DeepSlice.execute(options, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Plugin(type = Command.class, menuPath = "Plugins>BIOP>DeepSlice>DeepSlice (folder)",
description = "Runs DeepSlice locally on a folder of your choice.")
public class DeepSliceFolderCommand implements Command{
public class DeepSliceFolderCommand implements Command {
@Parameter(style = "directory")
File input_folder;

Expand All @@ -24,20 +24,24 @@ public class DeepSliceFolderCommand implements Command{
@Parameter(description = "try with and without ensemble to find the model which best works for you")
boolean ensemble = false;

@Parameter(description = "if you have section numbers included in the filename as _sXXX specify this")
@Parameter(description = "if you have section numbers included in the filename as _sXXX set this to true")
boolean section_number = false;

@Parameter(description = "if you would like to normalise the angles")
boolean propagate_angle = false;

@Parameter(description = "to reorder your sections according to the section")
@Parameter(description = "order your sections according to the section number (section number should be present)")
boolean enforce_index_order = false;

@Parameter(description = "use enforce index spacing")
boolean use_enforce_index_spacing = false;

@Parameter(description = "alternative to enforce_index_order: if you know the\n" +
" precise spacing (ie; 1, 2, 4, indicates that section 3\n" +
" has been left out of the series), then you can set the\n" +
" section thickness in microns with this parameter")
int enforce_index_spacing = -1;
" precise spacing (ie; 1, 2, 4, indicates that section 3\n" +
" has been left out of the series), then you can set the\n" +
" section thickness in microns with this parameter, or type 'None' " +
" if you want DeepSlice to guess the spacing")
String enforce_index_spacing = "None";

@Override
public void run() {
Expand All @@ -47,6 +51,7 @@ public void run() {
if (output_folder!=null) settings.output_folder = output_folder.getAbsolutePath();
settings.propagate_angles = propagate_angle;
settings.section_numbers = section_number;
settings.use_enforce_index_spacing = use_enforce_index_spacing;
settings.enforce_index_spacing = enforce_index_spacing;
settings.enforce_index_order = enforce_index_order;
settings.ensemble = ensemble;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ch.epfl.biop.wrappers.deepslice.ij2commands;

import ch.epfl.biop.wrappers.BiopWrappersCheck;
import ch.epfl.biop.wrappers.cellpose.Cellpose;
import ch.epfl.biop.wrappers.deepslice.DeepSlice;
import org.scijava.command.Command;
import org.scijava.log.LogService;
Expand All @@ -24,19 +23,13 @@ public class DeepSlicePrefsSet implements Command {
@Parameter(style = "directory", persist = false)
File deepSliceEnvDirectory = new File(DeepSlice.envDirPath);

//@Parameter(required = true, choices = {"conda", "venv"})
String envType = "conda";//DeepSlice.envType;


@Parameter(choices = {"1.1.5"}, persist = false)
@Parameter(choices = {"1.1.5.1"}, persist = false)
String version = DeepSlice.version;

@Override
public void run() {

DeepSlice.setEnvDirPath(deepSliceEnvDirectory);
DeepSlice.setEnvType(envType);

DeepSlice.setVersion(version);

if (ls != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ def parse_arguments():
# alternatively if you know the precise spacing (ie; 1, 2, 4, indicates that section 3 has been left out of the series) Then you can use
# Furthermore if you know the exact section thickness in microns this can be included instead of None
if args.enforce_index_spacing is not None:
model.enforce_index_spacing(section_thickness=args.enforce_index_spacing)
if args.enforce_index_spacing=="None":
model.enforce_index_spacing(section_thickness=None)
else:
model.enforce_index_spacing(section_thickness=float(args.enforce_index_spacing))

# now we save which will produce a json file which can be placed in the same directory as your images and then opened with QuickNII.

# saves json only
Expand Down
Loading

0 comments on commit c98020d

Please sign in to comment.