Skip to content

Commit 51bd200

Browse files
author
David Freese
committed
ENH: provide more helpful error messages for most things
1 parent d2195dd commit 51bd200

File tree

5 files changed

+34
-19
lines changed

5 files changed

+34
-19
lines changed

doc/commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ Creates a spherical source.
371371

372372
### rect_src
373373
```
374-
rect_src [center xyz] [size xyz] [size xyz]
374+
rect_src [center xyz] [size xyz] [axis xyz] [activity]
375375
```
376376
Creates a rectangular source. The axis defines the direction of z size.
377377

include/Gray/Gray/Command.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Command {
2727
std::string WarningMsg() const;
2828
void MarkWarning(const std::string& warn_msg);
2929
bool IsWarning() const;
30+
std::string Original() const;
3031
bool operator==(const std::string& val) const;
3132
std::string Join() const;
3233
std::string JoinAll() const;
@@ -54,6 +55,7 @@ class Command {
5455
std::string err_msg;
5556
bool is_warning = false;
5657
std::string warn_msg;
58+
std::string original;
5759
};
5860

5961
#endif // COMMAND_H

python/gray/io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,5 @@ def save_mapping_file(filename, mapping):
7676
7777
'''
7878
with open(filename, 'w') as fid:
79-
print >>fid, ' '.join(mapping.dtype.names)
79+
fid.write(' '.join(mapping.dtype.names) + '\n')
8080
np.savetxt(fid, mapping, fmt='%d')

src/Gray/Command.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
#include "Gray/Gray/String.h"
1818

1919
Command::Command(const std::string& line) :
20-
tokens(String::Split(line.substr(0, line.find_first_of("#")), " "))
20+
tokens(String::Split(line.substr(0, line.find_first_of("#")), " ")),
21+
original(line)
2122
{}
2223

2324
std::string Command::ErrorMsg() const {
@@ -46,6 +47,10 @@ bool Command::IsWarning() const {
4647
return (is_warning);
4748
}
4849

50+
std::string Command::Original() const {
51+
return (original);
52+
}
53+
4954
bool Command::operator==(const std::string& val) const {
5055
if (tokens.empty()) {
5156
return (false);

src/Gray/Load.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ bool Load::SceneCommands(
258258
result &= Load::SceneCommand(cmd, sources, scene, det_array, config);
259259
if (cmd.IsError()) {
260260
std::cerr << "Error in " << cmd.filename << ", line "
261-
<< cmd.line << ": " << cmd.ErrorMsg() << "\n";
261+
<< cmd.line << ":\n\"" << cmd.Original() << "\"\n"
262+
<< cmd.ErrorMsg() << "\n";
262263
} else if (cmd.IsWarning()) {
263264
std::cout << "Warning in " << cmd.filename << ", line "
264265
<< cmd.line << ": " << cmd.WarningMsg() << "\n";
@@ -302,7 +303,7 @@ bool Load::SceneCommand(
302303
// the input file.
303304
polygon_verts.push_back(point);
304305
if (!status) {
305-
cmd.MarkError("Invalid vertex point");
306+
cmd.MarkError("vertex point format: [x] [y] [z]");
306307
return (true);
307308
}
308309
if ((int)polygon_verts.size() == no_polygon_verts) {
@@ -323,7 +324,8 @@ bool Load::SceneCommand(
323324
axis.x, axis.y, axis.z,
324325
outer_radius, inner_radius, height))
325326
{
326-
cmd.MarkError("Invalid values for ann_cyl");
327+
cmd.MarkError("format: ann_cyl [center xyz] [axis xyz]"
328+
" [outer_radius] [inner_radius] [height]");
327329
return (false);
328330
}
329331
int det_id = -1;
@@ -344,7 +346,8 @@ bool Load::SceneCommand(
344346
axis.x, axis.y, axis.z,
345347
radius1, radius2, height))
346348
{
347-
cmd.MarkError("Invalid values for elliptic_cyl");
349+
cmd.MarkError("format: elliptic_cyl [center xyz] [axis xyz]"
350+
" [radius1] [radius2] [height]");
348351
return (false);
349352
}
350353
std::unique_ptr<ViewableCylinder> vc(new ViewableCylinder());
@@ -370,7 +373,8 @@ bool Load::SceneCommand(
370373
if (!cmd.parse(center.x, center.y, center.z,
371374
axis.x, axis.y, axis.z, radius, height, activity))
372375
{
373-
cmd.MarkError("Invalid values for cyl_src");
376+
cmd.MarkError("format: cyl_src [center xyz] [axis xyz]"
377+
"[radius] [height] [activity]");
374378
return (false);
375379
}
376380
cur_matrix.Transform(&center);
@@ -383,7 +387,7 @@ bool Load::SceneCommand(
383387
VectorR3 center;
384388
double radius, activity;
385389
if (!cmd.parse(center.x, center.y, center.z, radius, activity)) {
386-
cmd.MarkError("Invalid values for sp_src");
390+
cmd.MarkError("format: sp_src [center xyz] [radius] [activity]");
387391
return (false);
388392
}
389393
cur_matrix.Transform(&center);
@@ -398,7 +402,8 @@ bool Load::SceneCommand(
398402
size.x, size.y, size.z,
399403
axis.x, axis.y, axis.z, activity))
400404
{
401-
cmd.MarkError("Invalid values for rect_src");
405+
cmd.MarkError(
406+
"rect_src format [center xyz] [size xyz] [axis xyz] [act]");
402407
return (false);
403408
}
404409
cur_matrix.Transform(&center);
@@ -412,7 +417,7 @@ bool Load::SceneCommand(
412417
VectorR3 center;
413418
double activity;
414419
if (!cmd.parse(center.x, center.y, center.z, activity)) {
415-
cmd.MarkError("Invalid values for pt_src");
420+
cmd.MarkError("format: pt_src [center xyz] [activity]");
416421
return (false);
417422
}
418423
cur_matrix.Transform(&center);
@@ -430,7 +435,8 @@ bool Load::SceneCommand(
430435
if (!cmd.parse(center.x, center.y, center.z,
431436
size.x, size.y, size.z, filename, activity))
432437
{
433-
cmd.MarkError("Invalid values for voxel_src");
438+
cmd.MarkError("format: voxel_src [center xyz] [size xyz]"
439+
" [filename] [activity]");
434440
return (false);
435441
}
436442
// Make the file relative to whichever file in which the command was
@@ -534,7 +540,7 @@ bool Load::SceneCommand(
534540
} else if (cmd == "m") {
535541
std::string mat_name;
536542
if (!cmd.parse(mat_name)) {
537-
cmd.MarkError("Incorrect syntax for material");
543+
cmd.MarkError("format: m [material name]");
538544
return (false);
539545
} else if (!scene.HasMaterial(mat_name)) {
540546
cmd.MarkError("Invalid material: " + mat_name);
@@ -553,7 +559,7 @@ bool Load::SceneCommand(
553559
} else if (cmd == "t") {
554560
VectorR3 trans;
555561
if (!cmd.parse(trans.x, trans.y, trans.z)) {
556-
cmd.MarkError("Invalid values for t (transltate)");
562+
cmd.MarkError("format: t [shift xyz]");
557563
return (false);
558564
}
559565
// This doesn't seem to be the most obvious, or correct way to apply
@@ -586,7 +592,7 @@ bool Load::SceneCommand(
586592
VectorR3 axis;
587593
double degree;
588594
if (!cmd.parse(axis.x, axis.y, axis.z, degree)) {
589-
cmd.MarkError("Invalid values for raxis");
595+
cmd.MarkError("format: raxis [axis xyz] [angle (deg)]");
590596
return (false);
591597
}
592598
const double theta = degree * (M_PI / 180.0);
@@ -596,7 +602,7 @@ bool Load::SceneCommand(
596602
VectorR3 center;
597603
double radius;
598604
if (!cmd.parse(center.x, center.y, center.z, radius)) {
599-
cmd.MarkError("Invalid values for sphere");
605+
cmd.MarkError("sphere format: [center xyz] [radius]");
600606
return (false);
601607
}
602608
std::unique_ptr<ViewableSphere> s(new ViewableSphere(center, radius));
@@ -610,7 +616,8 @@ bool Load::SceneCommand(
610616
if (!cmd.parse(center.x, center.y, center.z,
611617
axis.x, axis.y, axis.z, radius, height))
612618
{
613-
cmd.MarkError("Invalid values for cyl");
619+
cmd.MarkError("cyl format [center xyz] [axis xyz]"
620+
" [radius] [height]");
614621
return (false);
615622
}
616623
std::unique_ptr<ViewableCylinder> vc(new ViewableCylinder());
@@ -644,7 +651,7 @@ bool Load::SceneCommand(
644651
} else if (cmd == "k") {
645652
VectorR3 center, size;
646653
if (!cmd.parse(center.x, center.y, center.z, size.x, size.y, size.z)) {
647-
cmd.MarkError("Invalid values for rectangle");
654+
cmd.MarkError("k format: [center xyz] [size xyz]");
648655
return (false);
649656
}
650657

@@ -669,7 +676,8 @@ bool Load::SceneCommand(
669676
step.x, step.y, step.z,
670677
size.x, size.y, size.z))
671678
{
672-
cmd.MarkError("Invalid values for array");
679+
cmd.MarkError("array format: [center xyz] [no steps xyz]"
680+
"[pitch xyz] [size xyz]");
673681
return (false);
674682
}
675683

0 commit comments

Comments
 (0)