|
18 | 18 | import numpy as np |
19 | 19 |
|
20 | 20 |
|
21 | | -def _resolve_cli_expression(expression, namespace): |
22 | | - """Resolve a CLI expression against an explicit namespace. |
23 | | -
|
24 | | - Parameters |
25 | | - ---------- |
26 | | - expression : str |
27 | | - The user-supplied CLI expression. |
28 | | - namespace : dict |
29 | | - The explicit namespace allowed during evaluation. |
30 | | -
|
31 | | - Returns |
32 | | - ------- |
33 | | - object |
34 | | - The resolved class or instance. |
35 | | - """ |
36 | | - return eval(expression, {"__builtins__": {}}, namespace) |
37 | | - |
38 | | - |
39 | | -def _baseline_namespace(): |
40 | | - """Return the baseline classes supported by the CLI.""" |
41 | | - from diffpy.srmise.baselines.arbitrary import Arbitrary |
42 | | - from diffpy.srmise.baselines.fromsequence import FromSequence |
43 | | - from diffpy.srmise.baselines.nanospherical import NanoSpherical |
44 | | - from diffpy.srmise.baselines.polynomial import Polynomial |
45 | | - |
46 | | - return { |
47 | | - "Arbitrary": Arbitrary, |
48 | | - "FromSequence": FromSequence, |
49 | | - "NanoSpherical": NanoSpherical, |
50 | | - "Polynomial": Polynomial, |
51 | | - } |
52 | 21 |
|
53 | 22 |
|
54 | | -def _peakfunction_namespace(): |
55 | | - """Return the peak-function classes supported by the CLI.""" |
56 | | - from diffpy.srmise.peaks.gaussian import Gaussian |
57 | | - from diffpy.srmise.peaks.gaussianoverr import GaussianOverR |
58 | | - from diffpy.srmise.peaks.terminationripples import TerminationRipples |
59 | | - |
60 | | - return { |
61 | | - "Gaussian": Gaussian, |
62 | | - "GaussianOverR": GaussianOverR, |
63 | | - "TerminationRipples": TerminationRipples, |
64 | | - } |
65 | | - |
66 | | - |
67 | | -def _modelevaluator_namespace(): |
68 | | - """Return the model evaluators supported by the CLI.""" |
69 | | - from diffpy.srmise.modelevaluators.aic import AIC |
70 | | - from diffpy.srmise.modelevaluators.aicc import AICc |
71 | | - |
72 | | - return { |
73 | | - "AIC": AIC, |
74 | | - "AICc": AICc, |
75 | | - } |
76 | | - |
77 | 23 |
|
78 | 24 | def main(): |
79 | 25 | """Default SrMise entry-point.""" |
@@ -490,21 +436,15 @@ def main(): |
490 | 436 |
|
491 | 437 | if options.peakfunction: |
492 | 438 | try: |
493 | | - options.peakfunction = _resolve_cli_expression( |
494 | | - options.peakfunction, |
495 | | - _peakfunction_namespace(), |
496 | | - ) |
| 439 | + options.peakfunction = eval("peaks." + options.peakfunction) |
497 | 440 | except Exception as err: |
498 | 441 | print(err) |
499 | 442 | print("Could not create peak function '%s'. Exiting." % options.peakfunction) |
500 | 443 | return |
501 | 444 |
|
502 | 445 | if options.modelevaluator: |
503 | 446 | try: |
504 | | - options.modelevaluator = _resolve_cli_expression( |
505 | | - options.modelevaluator, |
506 | | - _modelevaluator_namespace(), |
507 | | - ) |
| 447 | + options.modelevaluator = eval("modelevaluators." + options.modelevaluator) |
508 | 448 | except Exception as err: |
509 | 449 | print(err) |
510 | 450 | print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator) |
@@ -546,16 +486,12 @@ def main(): |
546 | 486 |
|
547 | 487 | bl = NanoSpherical() |
548 | 488 | options.baseline = parsepars(bl, options.bspherical) |
549 | | - elif options.baseline: |
550 | | - try: |
551 | | - options.baseline = _resolve_cli_expression( |
552 | | - options.baseline, |
553 | | - _baseline_namespace(), |
554 | | - ) |
555 | | - except Exception as err: |
556 | | - print(err) |
557 | | - print("Could not create baseline '%s'. Exiting." % options.baseline) |
558 | | - return |
| 489 | + try: |
| 490 | + options.baseline = eval("baselines." + options.baseline) |
| 491 | + except Exception as err: |
| 492 | + print(err) |
| 493 | + print("Could not create baseline '%s'. Exiting." % options.baseline) |
| 494 | + return |
559 | 495 |
|
560 | 496 | filename = args[0] |
561 | 497 |
|
|
0 commit comments