-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
282 lines (228 loc) · 9.01 KB
/
cli.py
File metadata and controls
282 lines (228 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import logging
import sys
from pathlib import Path
import click
import numpy as np
from rich import console, traceback
import waveform_editor
from waveform_editor.configuration import WaveformConfiguration
from waveform_editor.exporter import ConfigurationExporter
from waveform_editor.util import times_from_csv
logger = logging.getLogger(__name__)
def _excepthook(type_, value, tb):
logger.debug("Suppressed traceback:", exc_info=(type_, value, tb))
# Only display the last traceback frame:
if tb is not None:
while tb.tb_next:
tb = tb.tb_next
rich_tb = traceback.Traceback.from_exception(type_, value, tb, extra_lines=0)
console.Console(stderr=True).print(rich_tb)
@click.group("waveform-editor", invoke_without_command=True, no_args_is_help=True)
@click.option("--version", is_flag=True, help="Show version information")
@click.option("-v", "--verbose", count=True, help="Show verbose output")
def cli(version, verbose):
"""The Waveform Editor command line interface.
Please use one of the available commands listed below. You can get help for each
command by executing:
waveform-editor <command> --help
"""
loglevel = logging.WARNING
if verbose == 1:
loglevel = logging.INFO
elif verbose > 1:
loglevel = logging.DEBUG
logging.basicConfig(level=loglevel)
if verbose <= 1:
# Limit the traceback to 1 item: avoid scaring CLI users with long traceback
# prints and let them focus on the actual error message
sys.excepthook = _excepthook
if version:
print_version()
def print_version():
"""Print version information of the waveform editor."""
click.echo(f"Waveform editor version: {waveform_editor.__version__}")
def parse_linspace(ctx, param, value):
"""Parse linspace string into a tuple."""
if not value:
return None
try:
start_str, stop_str, num_str = value.split(",")
return float(start_str), float(stop_str), int(num_str)
except Exception as e:
raise click.BadParameter(
"Must be in the format: start,stop,num (e.g. 0,5,6)"
) from e
@cli.command("gui")
@click.argument("file", type=click.Path(exists=True, dir_okay=False), required=False)
def launch_gui(file):
"""Launch the Waveform Editor GUI using Panel.
\b
Arguments:
file: Waveform file to load on startup.
"""
# Use local imports to avoid loading the full GUI dependencies for the other CLI use
# cases:
import panel as pn
from waveform_editor.gui.main import WaveformEditorGui
def app():
gui = WaveformEditorGui()
if file is not None:
gui.load_yaml_from_file(Path(file))
return gui
try:
pn.serve(app, threaded=True)
except Exception as e:
logger.error(f"Failed to launch GUI: {e}")
@cli.command("export-ids")
@click.argument("yaml", type=click.Path(exists=True))
@click.argument("uri", type=str)
@click.option("--csv", type=click.Path(exists=False))
@click.option("--linspace", callback=parse_linspace)
def export_ids(yaml, uri, csv, linspace):
"""Export waveform data to an IDS.
\b
Arguments:
yaml: Path to the waveform YAML file.
uri: URI of the output Data Entry.
\b
Options:
csv: CSV file containing a custom time array.
linspace: linspace containing start, stop and num values, e.g. 0,3,4
Note: The csv containing the time values should be formatted as a single row,
delimited by commas, For example: `1,2,3,4,5`.
"""
if not csv and not linspace:
raise click.UsageError("Either --csv or --linspace must be provided")
exporter = create_exporter(yaml, csv, linspace)
exporter.to_ids(uri)
@cli.command("export-png")
@click.argument("yaml", type=click.Path(exists=True))
@click.argument("output_dir", type=click.Path(exists=False))
@click.option("--csv", type=click.Path(exists=False))
@click.option("--linspace", callback=parse_linspace)
def export_png(yaml, output_dir, csv, linspace):
"""Export waveform data to a PNG file.
\b
Arguments:
yaml: Path to the waveform YAML file.
output_dir: Path to output directory where the PNG files will be saved.
\b
Options:
csv: CSV file containing a custom time array.
linspace: linspace containing start, stop and num values, e.g. 0,3,4
Note: The csv containing the time values should be formatted as a single row,
delimited by commas, For example: `1,2,3,4,5`.
"""
exporter = create_exporter(yaml, csv, linspace)
output_path = Path(output_dir)
exporter.to_png(output_path)
@cli.command("export-csv")
@click.argument("yaml", type=click.Path(exists=True))
@click.argument("output_csv", type=click.Path(exists=False))
@click.option("--csv", type=click.Path(exists=False))
@click.option("--linspace", callback=parse_linspace)
def export_csv(yaml, output_csv, csv, linspace):
"""Export waveform data to a CSV file.
\b
Arguments:
yaml: Path to the waveform YAML file.
output_csv: Path to output CSV file.
\b
Options:
csv: CSV file containing a custom time array.
linspace: linspace containing start, stop and num values, e.g. 0,3,4
Note: The csv containing the time values should be formatted as a single row,
delimited by commas, For example: `1,2,3,4,5`.
"""
if not csv and not linspace:
raise click.UsageError("Either --csv or --linspace must be provided")
exporter = create_exporter(yaml, csv, linspace)
output_path = Path(output_csv)
exporter.to_csv(output_path)
@cli.command("export-pcssp-xml")
@click.argument("yaml", type=click.Path(exists=True))
@click.argument("output_xml", type=click.Path(exists=False))
@click.option("--csv", type=click.Path(exists=False))
@click.option("--linspace", callback=parse_linspace)
def export_pcssp_xml(yaml, output_xml, csv, linspace):
"""Export waveform data to a PCSSP XML file.
\b
Arguments:
yaml: Path to the waveform YAML file.
output_xml: Path to output XML file.
\b
Options:
csv: CSV file containing a custom time array.
linspace: linspace containing start, stop and num values, e.g. 0,3,4
Note: The csv containing the time values should be formatted as a single row,
delimited by commas, For example: `1,2,3,4,5`.
"""
if not csv and not linspace:
raise click.UsageError("Either --csv or --linspace must be provided")
exporter = create_exporter(yaml, csv, linspace)
output_path = Path(output_xml)
exporter.to_pcssp_xml(output_path)
@cli.command("actor")
def actor():
"""Run the MUSCLE3 actor.
This command does not accept any options or arguments: configuration of the actor is
done through MUSCLE3 settings. Please have a look at the documentation for more
details: https://waveform-editor.readthedocs.io/
"""
try:
import libmuscle # noqa: F401
except ImportError as exc:
raise RuntimeError(
"The muscle3 python package is required to run the waveform-editor actor."
) from exc
from waveform_editor.muscle3 import waveform_actor
waveform_actor()
def create_exporter(yaml, csv, linspace):
"""Read a YAML file from disk, load it into a WaveformConfiguration and create a
ConfigurationExporter using the given times.
Args:
yaml: The YAML file to load into a configuration.
csv: CSV file containing time values.
linspace: Tuple containing the start, stop and number of the linspace.
Returns:
The ConfigurationExporter of the loaded YAML file.
"""
if csv and linspace:
raise click.UsageError("Cannot provide both --csv and --linspace.")
elif csv:
times = times_from_csv(csv)
elif linspace:
start = linspace[0]
stop = linspace[1]
num = linspace[2]
times = np.linspace(start, stop, num)
else:
times = None
config = WaveformConfiguration()
load_config(config, Path(yaml))
exporter = ConfigurationExporter(config, times)
return exporter
def load_config(config: WaveformConfiguration, filepath: Path) -> None:
"""Load the YAML file from disk with the provided configuration.
Args:
config: configuration to load the file with
filepath: Path to the yaml file
"""
if not filepath.is_file():
raise ValueError(f"Cannot find waveform configuration file '{filepath}'")
logging.debug("Loading waveform configuration from %s", filepath)
config.clear()
config.load_yaml(filepath)
if config.load_error: # Set when the YAML could not be parsed
raise RuntimeError(f"Could not load waveforms: {config.load_error}")
# Warn for any waveform with issues
for name, group in config.waveform_map.items():
waveform = group[name]
if waveform.annotations:
details = "\n".join(
"- " + item["text"].replace("\n", "\n ").strip()
for item in waveform.annotations
)
logger.warning("Found issues with waveform '%s':\n%s", name, details)
if __name__ == "__main__":
cli()