use of org.apache.commons.lang.math.IntRange in project incubator-systemml by apache.
the class DMLDebugger method runSystemMLDebugger.
/**
* Controls the communication between debugger CLI and DML runtime.
*/
@SuppressWarnings("deprecation")
public synchronized void runSystemMLDebugger() {
debuggerUI.setOptions();
debuggerUI.getDebuggerCLI();
Thread runtime = new Thread(DMLRuntime);
boolean isRuntimeInstruction = false;
while (!quit) {
try {
//get debugger function from CLI
getCommand();
if (cmd != null) {
isRuntimeInstruction = false;
//check for help
if (cmd.hasOption("h")) {
debuggerUI.getDebuggerCLI();
} else //check for exit
if (cmd.hasOption("q")) {
synchronized (DMLDebugger.class) {
quit = true;
}
runtime.stop();
} else if (cmd.hasOption("r")) {
if (currEC != null) {
System.out.println("Runtime has already started. Try \"s\" to go to next line, or \"c\" to continue running your DML script.");
} else {
currEC = preEC;
runtime.start();
isRuntimeInstruction = true;
}
} else if (cmd.hasOption("c")) {
if (currEC == null)
System.out.println("Runtime has not been started. Try \"r\" to start DML runtime execution.");
else if (!runtime.isAlive()) {
System.err.println("Invalid debug state.");
//System.out.println("Runtime terminated. Try \"-c\" to recompile followed by \"r\" to restart DML runtime execution.");
} else {
System.out.println("Resuming DML script execution ...");
preEC.getDebugState().setCommand(null);
runtime.resume();
isRuntimeInstruction = true;
}
} else if (cmd.hasOption("si")) {
if (!runtime.isAlive()) {
currEC = preEC;
runtime.start();
isRuntimeInstruction = true;
}
preEC.getDebugState().setCommand("step_instruction");
runtime.resume();
isRuntimeInstruction = true;
} else if (cmd.hasOption("s")) {
if (!runtime.isAlive()) {
currEC = preEC;
runtime.start();
isRuntimeInstruction = true;
}
preEC.getDebugState().setCommand("step_line");
runtime.resume();
isRuntimeInstruction = true;
} else if (cmd.hasOption("b")) {
int lineNumber = dbFunctions.getValue(cmd.getOptionValues("b"), lines.length);
if (lineNumber > 0) {
if (DMLBreakpointManager.getBreakpoint(lineNumber) == null)
System.out.println("Sorry, a breakpoint cannot be inserted at line " + lineNumber + ". Please try a different line number.");
else {
if (DMLBreakpointManager.getBreakpoint(lineNumber).getBPInstructionStatus() != BPINSTRUCTION_STATUS.INVISIBLE) {
System.out.format("Breakpoint at line %d already exists.\n", lineNumber);
} else {
dbprog.accessBreakpoint(lineNumber, 0, BPINSTRUCTION_STATUS.ENABLED);
}
}
}
} else if (cmd.hasOption("d")) {
int lineNumber = dbFunctions.getValue(cmd.getOptionValues("d"), lines.length);
if (lineNumber > 0 && DMLBreakpointManager.getBreakpoint(lineNumber) != null && DMLBreakpointManager.getBreakpoint(lineNumber).getBPInstructionStatus() != BPINSTRUCTION_STATUS.INVISIBLE) {
dbprog.accessBreakpoint(lineNumber, 1, BPINSTRUCTION_STATUS.INVISIBLE);
} else {
System.out.println("Sorry, a breakpoint cannot be deleted at line " + lineNumber + ". Please try a different line number.");
}
} else if (cmd.hasOption("i")) {
String[] infoOptions = cmd.getOptionValues("i");
if (infoOptions == null || infoOptions.length == 0) {
System.err.println("The command \"info\" requires option. Try \"info break\" or \"info frame\".");
} else if (infoOptions[0].trim().equals("break")) {
dbFunctions.listBreakpoints(DMLBreakpointManager.getBreakpoints());
} else if (infoOptions[0].trim().equals("frame")) {
if (!runtime.isAlive())
System.err.println("Runtime has not been started. Try \"r\" or \"s\" to start DML runtime execution.");
else
dbFunctions.printCallStack(currEC.getDebugState().getCurrentFrame(), currEC.getDebugState().getCallStack());
} else {
System.err.println("Invalid option for command \"info\". Try \"info break\" or \"info frame\".");
}
} else if (cmd.hasOption("p")) {
String[] pOptions = cmd.getOptionValues("p");
if (pOptions == null || pOptions.length != 1) {
System.err.println("Incorrect options for command \"print\"");
} else {
String varName = pOptions[0].trim();
if (runtime.isAlive()) {
if (varName.contains("[")) {
// matrix with index: can be cell or column or row
try {
String variableNameWithoutIndices = varName.split("\\[")[0].trim();
String indexString = (varName.split("\\[")[1].trim()).split("\\]")[0].trim();
String rowIndexStr = "";
String colIndexStr = "";
if (indexString.startsWith(",")) {
colIndexStr = indexString.split(",")[1].trim();
} else if (indexString.endsWith(",")) {
rowIndexStr = indexString.split(",")[0].trim();
} else {
rowIndexStr = indexString.split(",")[0].trim();
colIndexStr = indexString.split(",")[1].trim();
}
int rowIndex = -1;
int colIndex = -1;
if (!rowIndexStr.isEmpty()) {
rowIndex = Integer.parseInt(rowIndexStr);
}
if (!colIndexStr.isEmpty()) {
colIndex = Integer.parseInt(colIndexStr);
}
dbFunctions.print(currEC.getDebugState().getVariables(), variableNameWithoutIndices, "value", rowIndex, colIndex);
} catch (Exception indicesException) {
System.err.println("Incorrect format for \"p\". If you are trying to print matrix variable M, you can use M[1,] or M[,1] or M[1,1] (without spaces).");
}
} else {
// Print entire matrix
dbFunctions.print(currEC.getDebugState().getVariables(), varName, "value", -1, -1);
}
} else
System.err.println("Runtime has not been started. Try \"r\" or \"s\" to start DML runtime execution.");
}
} else if (cmd.hasOption("whatis")) {
String[] pOptions = cmd.getOptionValues("whatis");
if (pOptions == null || pOptions.length != 1) {
System.err.println("Incorrect options for command \"whatis\"");
} else {
String varName = pOptions[0].trim();
dbFunctions.print(currEC.getDebugState().getVariables(), varName, "metadata", -1, -1);
}
} else if (cmd.hasOption("set")) {
String[] pOptions = cmd.getOptionValues("set");
if (pOptions == null || pOptions.length != 2) {
System.err.println("Incorrect options for command \"set\"");
} else {
try {
if (pOptions[0].contains("[")) {
String[] paramsToSetMatrix = new String[4];
paramsToSetMatrix[0] = pOptions[0].split("\\[")[0].trim();
String indexString = (pOptions[0].split("\\[")[1].trim()).split("\\]")[0].trim();
paramsToSetMatrix[1] = indexString.split(",")[0].trim();
paramsToSetMatrix[2] = indexString.split(",")[1].trim();
paramsToSetMatrix[3] = pOptions[1].trim();
dbFunctions.setMatrixCell(currEC.getDebugState().getVariables(), paramsToSetMatrix);
} else {
dbFunctions.setScalarValue(currEC.getDebugState().getVariables(), pOptions);
}
} catch (Exception exception1) {
System.out.println("Only scalar variable or a matrix cell available in current frame can be set in current version.");
}
}
} else if (cmd.hasOption("l")) {
String[] pOptions = cmd.getOptionValues("l");
String[] argsForRange = new String[2];
int currentPC = 1;
if (runtime.isAlive()) {
currentPC = currEC.getDebugState().getPC().getLineNumber();
}
IntRange range = null;
if (pOptions == null) {
// Print first 10 lines
range = new IntRange(currentPC, Math.min(lines.length, currentPC + 10));
} else if (pOptions.length == 1 && pOptions[0].trim().toLowerCase().equals("all")) {
// Print entire program
range = new IntRange(1, lines.length);
} else if (pOptions.length == 2 && pOptions[0].trim().toLowerCase().equals("next")) {
int numLines = 10;
try {
numLines = Integer.parseInt(pOptions[1]);
} catch (Exception e1) {
}
argsForRange[0] = "" + currentPC;
argsForRange[1] = "" + Math.min(lines.length, numLines + currentPC);
range = dbFunctions.getRange(argsForRange, lines.length);
} else if (pOptions.length == 2 && pOptions[0].trim().toLowerCase().equals("prev")) {
int numLines = 10;
try {
numLines = Integer.parseInt(pOptions[1]);
} catch (Exception e1) {
}
argsForRange[0] = "" + Math.max(1, currentPC - numLines);
argsForRange[1] = "" + currentPC;
range = dbFunctions.getRange(argsForRange, lines.length);
}
if (range == null) {
System.err.println("Incorrect usage of command \"l\". Try \"l\" or \"l all\" or \"l next 5\" or \"l prev 5\".");
} else {
if (range.getMinimumInteger() > 0) {
dbFunctions.printLines(lines, range);
} else {
System.err.println("Sorry no lines that can be printed. Try \"l\" or \"l all\" or \"l next 5\" or \"l prev 5\".");
}
}
} else if (cmd.hasOption("li")) {
String[] pOptions = cmd.getOptionValues("li");
String[] argsForRange = new String[2];
int currentPC = 1;
if (runtime.isAlive()) {
currentPC = currEC.getDebugState().getPC().getLineNumber();
}
IntRange range = null;
if (pOptions == null) {
// Print first 10 lines
range = new IntRange(currentPC, Math.min(lines.length, currentPC + 10));
} else if (pOptions.length == 1 && pOptions[0].trim().toLowerCase().equals("all")) {
// Print entire program
range = new IntRange(1, lines.length);
} else if (pOptions.length == 2 && pOptions[0].trim().toLowerCase().equals("next")) {
int numLines = 10;
try {
numLines = Integer.parseInt(pOptions[1]);
} catch (Exception e1) {
}
argsForRange[0] = "" + currentPC;
argsForRange[1] = "" + Math.min(lines.length, numLines + currentPC);
range = dbFunctions.getRange(argsForRange, lines.length);
} else if (pOptions.length == 2 && pOptions[0].trim().toLowerCase().equals("prev")) {
int numLines = 10;
try {
numLines = Integer.parseInt(pOptions[1]);
} catch (Exception e1) {
}
argsForRange[0] = "" + Math.max(1, currentPC - numLines);
argsForRange[1] = "" + currentPC;
range = dbFunctions.getRange(argsForRange, lines.length);
}
if (range == null) {
System.err.println("Incorrect usage of command \"li\". Try \"li\" or \"li all\" or \"li next 5\" or \"li prev 5\".");
} else {
if (range.getMinimumInteger() > 0) {
dbFunctions.printInstructions(lines, dbprog.getDMLInstMap(), range, false);
} else {
System.err.println("Sorry no lines that can be printed. Try \"li\" or \"li all\" or \"li next 5\" or \"li prev 5\".");
}
}
} else if (cmd.hasOption("set_scalar")) {
if (!runtime.isAlive())
System.err.println("Runtime has not been started. Try \"r\" to start DML runtime execution.");
else
dbFunctions.setScalarValue(currEC.getDebugState().getVariables(), cmd.getOptionValues("set_scalar"));
} else if (cmd.hasOption("m")) {
String varname = dbFunctions.getValue(cmd.getOptionValues("m"));
if (runtime.isAlive())
dbFunctions.printMatrixVariable(currEC.getDebugState().getVariables(), varname);
else
System.err.println("Runtime has not been started. Try \"r\" to start DML runtime execution.");
} else if (cmd.hasOption("x")) {
if (!runtime.isAlive())
System.err.println("Runtime has not been started. Try \"r\" to start DML runtime execution.");
else {
dbFunctions.printMatrixCell(currEC.getDebugState().getVariables(), cmd.getOptionValues("x"));
}
} else if (cmd.hasOption("set_cell")) {
if (!runtime.isAlive())
System.err.println("Runtime has not been started. Try \"r\" to start DML runtime execution.");
else {
dbFunctions.setMatrixCell(currEC.getDebugState().getVariables(), cmd.getOptionValues("set_cell"));
}
} else {
System.err.println("Undefined command. Try \"help\".");
}
//block until runtime suspends execution or terminates
//while(runtime.isAlive() && !currEC.getProgram().isStopped()) {
// To avoid race condition between submitting job and
wait(300);
//System.out.println(">> Before while");
while (isRuntimeInstruction && !currEC.getDebugState().canAcceptNextCommand()) {
if (quit) {
break;
} else {
//wait
wait(300);
}
}
}
wait(300);
} catch (Exception e) {
System.err.println("Error processing debugger command. Try \"help\".");
}
}
}
use of org.apache.commons.lang.math.IntRange in project gatk by broadinstitute.
the class CreatePanelOfNormalsIntegrationTest method assertBasicPoNAssumptions.
private void assertBasicPoNAssumptions(final File ponFile, final File initialTargetsFileUsedToCreatePoN) {
try (final HDF5File ponHDF5File = new HDF5File(ponFile)) {
final HDF5PCACoveragePoN pon = new HDF5PCACoveragePoN(ponHDF5File);
Assert.assertTrue(pon.getTargets().size() >= pon.getPanelTargets().size());
Assert.assertTrue(pon.getRawTargets().size() > pon.getTargets().size());
Assert.assertTrue(pon.getTargetNames().size() == pon.getTargets().size());
Assert.assertTrue(pon.getPanelTargetNames().size() == pon.getPanelTargetNames().size());
Assert.assertTrue(pon.getRawTargetNames().size() == pon.getRawTargetNames().size());
if (initialTargetsFileUsedToCreatePoN != null) {
final TargetCollection<Target> tc = TargetArgumentCollection.readTargetCollection(initialTargetsFileUsedToCreatePoN);
Assert.assertEquals(pon.getRawTargets().size(), tc.targetCount());
// Check that the raw targets are the same
Assert.assertTrue(IntStream.of(new IntRange(0, pon.getRawTargets().size() - 1).toArray()).boxed().map(i -> pon.getRawTargets().get(i).equals(tc.target(i))).allMatch(t -> t));
}
}
}
use of org.apache.commons.lang.math.IntRange in project gatk-protected by broadinstitute.
the class ConvertGSVariantsToSegments method calculateLog10CallQuality.
private double calculateLog10CallQuality(final double[] log10Probs, final CopyNumberTriState call) {
final IntRange callCopyNumberRange;
switch(call) {
case NEUTRAL:
callCopyNumberRange = new IntRange(neutralCopyNumber, neutralCopyNumber);
break;
case DELETION:
callCopyNumberRange = new IntRange(0, neutralCopyNumber - 1);
break;
case DUPLICATION:
callCopyNumberRange = new IntRange(neutralCopyNumber + 1, log10Probs.length - 1);
break;
default:
throw new GATKException("unexpected call");
}
// We aggregate the probs of any copy number that that does not correspond
final double log10OneMinusProbCall = MathUtils.approximateLog10SumLog10(MathUtils.log10SumLog10(log10Probs, 0, Math.min(callCopyNumberRange.getMinimumInteger(), log10Probs.length)), MathUtils.log10SumLog10(log10Probs, callCopyNumberRange.getMaximumInteger() + 1, log10Probs.length));
final double log10ProbTotal = MathUtils.log10SumLog10(log10Probs);
return log10OneMinusProbCall - log10ProbTotal;
}
use of org.apache.commons.lang.math.IntRange in project gatk-protected by broadinstitute.
the class CreatePanelOfNormalsIntegrationTest method assertBasicPoNAssumptions.
private void assertBasicPoNAssumptions(final File ponFile, final File initialTargetsFileUsedToCreatePoN) {
try (final HDF5File ponHDF5File = new HDF5File(ponFile)) {
final HDF5PCACoveragePoN pon = new HDF5PCACoveragePoN(ponHDF5File);
Assert.assertTrue(pon.getTargets().size() >= pon.getPanelTargets().size());
Assert.assertTrue(pon.getRawTargets().size() > pon.getTargets().size());
Assert.assertTrue(pon.getTargetNames().size() == pon.getTargets().size());
Assert.assertTrue(pon.getPanelTargetNames().size() == pon.getPanelTargetNames().size());
Assert.assertTrue(pon.getRawTargetNames().size() == pon.getRawTargetNames().size());
if (initialTargetsFileUsedToCreatePoN != null) {
final TargetCollection<Target> tc = TargetArgumentCollection.readTargetCollection(initialTargetsFileUsedToCreatePoN);
Assert.assertEquals(pon.getRawTargets().size(), tc.targetCount());
// Check that the raw targets are the same
Assert.assertTrue(IntStream.of(new IntRange(0, pon.getRawTargets().size() - 1).toArray()).boxed().map(i -> pon.getRawTargets().get(i).equals(tc.target(i))).allMatch(t -> t));
}
}
}
use of org.apache.commons.lang.math.IntRange in project gatk by broadinstitute.
the class ConvertGSVariantsToSegments method calculateLog10CallQuality.
private double calculateLog10CallQuality(final double[] log10Probs, final CopyNumberTriState call) {
final IntRange callCopyNumberRange;
switch(call) {
case NEUTRAL:
callCopyNumberRange = new IntRange(neutralCopyNumber, neutralCopyNumber);
break;
case DELETION:
callCopyNumberRange = new IntRange(0, neutralCopyNumber - 1);
break;
case DUPLICATION:
callCopyNumberRange = new IntRange(neutralCopyNumber + 1, log10Probs.length - 1);
break;
default:
throw new GATKException("unexpected call");
}
// We aggregate the probs of any copy number that that does not correspond
final double log10OneMinusProbCall = MathUtils.approximateLog10SumLog10(MathUtils.log10SumLog10(log10Probs, 0, Math.min(callCopyNumberRange.getMinimumInteger(), log10Probs.length)), MathUtils.log10SumLog10(log10Probs, callCopyNumberRange.getMaximumInteger() + 1, log10Probs.length));
final double log10ProbTotal = MathUtils.log10SumLog10(log10Probs);
return log10OneMinusProbCall - log10ProbTotal;
}
Aggregations