Skip to content

Commit 2ae6436

Browse files
authored
Merge pull request #111 from melissalinkert/resetters
Add reset methods for each option
2 parents 56764eb + 4be2a28 commit 2ae6436

File tree

3 files changed

+108
-18
lines changed

3 files changed

+108
-18
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ repositories {
3131
dependencies {
3232
implementation 'net.java.dev.jna:jna:5.10.0'
3333
implementation 'dev.zarr:jzarr:0.4.2'
34-
implementation 'info.picocli:picocli:4.2.0'
34+
implementation 'info.picocli:picocli:4.7.5'
3535
implementation 'me.tongfei:progressbar:0.9.0'
3636
implementation 'ome:formats-bsd:7.0.1'
3737
implementation 'com.glencoesoftware:bioformats2raw:0.8.0-rc1'

src/main/java/com/glencoesoftware/pyramid/PyramidFromDirectoryWriter.java

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ public class PyramidFromDirectoryWriter implements Callable<Void> {
123123
/** Where to write? */
124124
Path outputFilePath;
125125
Path inputDirectory;
126-
private volatile String logLevel = "WARN";
126+
private volatile String logLevel;
127127
private volatile boolean progressBars = false;
128128
boolean printVersion = false;
129-
CompressionType compression = CompressionType.LZW;
129+
CompressionType compression;
130130
CodecOptions compressionOptions;
131131
boolean legacy = false;
132-
int maxWorkers = Runtime.getRuntime().availableProcessors();
132+
int maxWorkers;
133133
boolean rgb = false;
134134

135135
private List<PyramidSeries> series = new ArrayList<PyramidSeries>();
@@ -148,7 +148,6 @@ public class PyramidFromDirectoryWriter implements Callable<Void> {
148148
* Construct a writer for performing the pyramid conversion.
149149
*/
150150
public PyramidFromDirectoryWriter() {
151-
tileQueue = new LimitedQueue<Runnable>(maxWorkers);
152151
}
153152

154153
/**
@@ -159,11 +158,17 @@ public PyramidFromDirectoryWriter() {
159158
@Parameters(
160159
index = "1",
161160
arity = "1",
162-
description = "Relative path to the output OME-TIFF file"
161+
description = "Relative path to the output OME-TIFF file",
162+
defaultValue = Option.NULL_VALUE
163163
)
164164
public void setOutputPath(String output) {
165165
// could be expanded to allow other output locations
166-
outputFilePath = Paths.get(output);
166+
if (output != null) {
167+
outputFilePath = Paths.get(output);
168+
}
169+
else {
170+
outputFilePath = null;
171+
}
167172
}
168173

169174
/**
@@ -174,11 +179,17 @@ public void setOutputPath(String output) {
174179
@Parameters(
175180
index = "0",
176181
arity = "1",
177-
description = "Directory containing pixel data to convert"
182+
description = "Directory containing pixel data to convert",
183+
defaultValue = Option.NULL_VALUE
178184
)
179185
public void setInputPath(String input) {
180186
// could be expanded to allow other input locations
181-
inputDirectory = Paths.get(input);
187+
if (input != null) {
188+
inputDirectory = Paths.get(input);
189+
}
190+
else {
191+
inputDirectory = null;
192+
}
182193
}
183194

184195
/**
@@ -208,7 +219,8 @@ public void setLogLevel(String level) {
208219
@Option(
209220
names = {"-p", "--progress"},
210221
description = "Print progress bars during conversion",
211-
help = true
222+
help = true,
223+
defaultValue = "false"
212224
)
213225
public void setProgressBars(boolean useProgressBars) {
214226
progressBars = useProgressBars;
@@ -223,7 +235,8 @@ public void setProgressBars(boolean useProgressBars) {
223235
@Option(
224236
names = "--version",
225237
description = "Print version information and exit",
226-
help = true
238+
help = true,
239+
defaultValue = "false"
227240
)
228241
public void setPrintVersionOnly(boolean versionOnly) {
229242
printVersion = versionOnly;
@@ -271,7 +284,8 @@ public void setCompression(CompressionType compressionType) {
271284
@Option(
272285
names = "--quality",
273286
converter = CompressionQualityConverter.class,
274-
description = "Compression quality"
287+
description = "Compression quality",
288+
defaultValue = Option.NULL_VALUE
275289
)
276290
public void setCompressionOptions(CodecOptions options) {
277291
compressionOptions = options;
@@ -286,7 +300,8 @@ public void setCompressionOptions(CodecOptions options) {
286300
*/
287301
@Option(
288302
names = "--legacy",
289-
description = "Write a Bio-Formats 5.9.x pyramid instead of OME-TIFF"
303+
description = "Write a Bio-Formats 5.9.x pyramid instead of OME-TIFF",
304+
defaultValue = "false"
290305
)
291306
public void setLegacyTIFF(boolean legacyTIFF) {
292307
legacy = legacyTIFF;
@@ -300,26 +315,36 @@ public void setLegacyTIFF(boolean legacyTIFF) {
300315
@Option(
301316
names = "--split",
302317
description =
303-
"Split output into one OME-TIFF file per OME Image/Zarr group"
318+
"Split output into one OME-TIFF file per OME Image/Zarr group",
319+
defaultValue = "false"
304320
)
305321
public void setSplitTIFFs(boolean split) {
306322
splitBySeries = split;
307323
}
308324

309325
/**
310326
* Set the maximum number of workers to use for converting tiles.
311-
* Defaults to the number of detected CPUs.
327+
* Defaults to 4 or the number of detected CPUs, whichever is smaller.
312328
*
313329
* @param workers maximum worker count
314330
*/
315331
@Option(
316332
names = "--max_workers",
317-
description = "Maximum number of workers (default: ${DEFAULT-VALUE})"
333+
description = "Maximum number of workers (default: ${DEFAULT-VALUE})",
334+
defaultValue = "4"
318335
)
319336
public void setMaxWorkers(int workers) {
320-
if (workers > 0) {
337+
int availableProcessors = Runtime.getRuntime().availableProcessors();
338+
int prevWorkers = getMaxWorkers();
339+
if (workers > availableProcessors) {
340+
maxWorkers = availableProcessors;
341+
}
342+
else if (workers > 0 && workers != maxWorkers) {
321343
maxWorkers = workers;
322344
}
345+
if (prevWorkers != maxWorkers) {
346+
tileQueue = new LimitedQueue<Runnable>(maxWorkers);
347+
}
323348
}
324349

325350
/**
@@ -332,7 +357,8 @@ public void setMaxWorkers(int workers) {
332357
@Option(
333358
names = "--rgb",
334359
description = "Attempt to write channels as RGB; " +
335-
"channel count must be a multiple of 3"
360+
"channel count must be a multiple of 3",
361+
defaultValue = "false"
336362
)
337363
public void setRGB(boolean isRGB) {
338364
rgb = isRGB;
@@ -342,13 +368,19 @@ public void setRGB(boolean isRGB) {
342368
* @return path to output data
343369
*/
344370
public String getOutputPath() {
371+
if (outputFilePath == null) {
372+
return null;
373+
}
345374
return outputFilePath.toString();
346375
}
347376

348377
/**
349378
* @return path to input data
350379
*/
351380
public String getInputPath() {
381+
if (inputDirectory == null) {
382+
return null;
383+
}
352384
return inputDirectory.toString();
353385
}
354386

src/test/java/com/glencoesoftware/raw2ometiff/test/ConversionTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,10 +661,68 @@ public void testOptionsAPI() throws Exception {
661661

662662
outputOmeTiff = output.resolve("output.ome.tiff");
663663
PyramidFromDirectoryWriter apiConverter = new PyramidFromDirectoryWriter();
664+
CommandLine cmd = new CommandLine(apiConverter);
665+
cmd.parseArgs();
666+
667+
apiConverter.setInputPath(output.toString());
668+
apiConverter.setOutputPath(outputOmeTiff.toString());
669+
apiConverter.setCompression(CompressionType.UNCOMPRESSED);
670+
apiConverter.setRGB(true);
671+
apiConverter.call();
672+
673+
iteratePixels();
674+
}
675+
676+
/**
677+
* Test resetting options to their default values.
678+
*/
679+
@Test
680+
public void testResetAPI() throws Exception {
681+
input = fake("sizeC", "12", "rgb", "3");
682+
assertBioFormats2Raw();
683+
684+
outputOmeTiff = output.resolve("output.ome.tiff");
685+
686+
// make sure default options are set
687+
PyramidFromDirectoryWriter apiConverter = new PyramidFromDirectoryWriter();
688+
CommandLine cmd = new CommandLine(apiConverter);
689+
cmd.parseArgs();
690+
691+
Assert.assertEquals(apiConverter.getInputPath(), null);
692+
Assert.assertEquals(apiConverter.getOutputPath(), null);
693+
Assert.assertEquals(apiConverter.getCompression(), CompressionType.LZW);
694+
Assert.assertEquals(apiConverter.getRGB(), false);
695+
Assert.assertEquals(apiConverter.getLegacyTIFF(), false);
696+
Assert.assertEquals(apiConverter.getSplitTIFFs(), false);
697+
698+
// override default options, as though to start a conversion
664699
apiConverter.setInputPath(output.toString());
665700
apiConverter.setOutputPath(outputOmeTiff.toString());
666701
apiConverter.setCompression(CompressionType.UNCOMPRESSED);
667702
apiConverter.setRGB(true);
703+
704+
// change our minds and reset the options to defaults again
705+
cmd.parseArgs();
706+
707+
Assert.assertEquals(apiConverter.getInputPath(), null);
708+
Assert.assertEquals(apiConverter.getOutputPath(), null);
709+
Assert.assertEquals(apiConverter.getCompression(), CompressionType.LZW);
710+
Assert.assertEquals(apiConverter.getRGB(), false);
711+
Assert.assertEquals(apiConverter.getLegacyTIFF(), false);
712+
Assert.assertEquals(apiConverter.getSplitTIFFs(), false);
713+
714+
// update options, make sure they were set, and actually convert
715+
apiConverter.setInputPath(output.toString());
716+
apiConverter.setOutputPath(outputOmeTiff.toString());
717+
apiConverter.setCompression(CompressionType.UNCOMPRESSED);
718+
apiConverter.setRGB(true);
719+
720+
Assert.assertEquals(apiConverter.getInputPath(), output.toString());
721+
Assert.assertEquals(apiConverter.getOutputPath(), outputOmeTiff.toString());
722+
Assert.assertEquals(apiConverter.getCompression(),
723+
CompressionType.UNCOMPRESSED);
724+
Assert.assertEquals(apiConverter.getRGB(), true);
725+
668726
apiConverter.call();
669727

670728
iteratePixels();

0 commit comments

Comments
 (0)